if_ti.c revision 199559
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 199559 2009-11-19 22:06:40Z jhb $");
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
14398849Sken
14445386Swpaul/*
14545386Swpaul * Various supported device vendors/types and their names.
14645386Swpaul */
14745386Swpaul
14845386Swpaulstatic struct ti_type ti_devs[] = {
14945386Swpaul	{ ALT_VENDORID,	ALT_DEVICEID_ACENIC,
15063702Swpaul		"Alteon AceNIC 1000baseSX Gigabit Ethernet" },
15163699Swpaul	{ ALT_VENDORID,	ALT_DEVICEID_ACENIC_COPPER,
15263702Swpaul		"Alteon AceNIC 1000baseT Gigabit Ethernet" },
15345386Swpaul	{ TC_VENDORID,	TC_DEVICEID_3C985,
15445386Swpaul		"3Com 3c985-SX Gigabit Ethernet" },
15545386Swpaul	{ NG_VENDORID, NG_DEVICEID_GA620,
15664139Swpaul		"Netgear GA620 1000baseSX Gigabit Ethernet" },
15764139Swpaul	{ NG_VENDORID, NG_DEVICEID_GA620T,
15864139Swpaul		"Netgear GA620 1000baseT Gigabit Ethernet" },
15945386Swpaul	{ SGI_VENDORID, SGI_DEVICEID_TIGON,
16045386Swpaul		"Silicon Graphics Gigabit Ethernet" },
16156206Swpaul	{ DEC_VENDORID, DEC_DEVICEID_FARALLON_PN9000SX,
16256206Swpaul		"Farallon PN9000SX Gigabit Ethernet" },
16345386Swpaul	{ 0, 0, NULL }
16445386Swpaul};
16545386Swpaul
16698849Sken
16798849Skenstatic	d_open_t	ti_open;
16898849Skenstatic	d_close_t	ti_close;
16998849Skenstatic	d_ioctl_t	ti_ioctl2;
17098849Sken
17198849Skenstatic struct cdevsw ti_cdevsw = {
172126080Sphk	.d_version =	D_VERSION,
173153281Sscottl	.d_flags =	0,
174111815Sphk	.d_open =	ti_open,
175111815Sphk	.d_close =	ti_close,
176111815Sphk	.d_ioctl =	ti_ioctl2,
177111815Sphk	.d_name =	"ti",
17898849Sken};
17998849Sken
180142407Simpstatic int ti_probe(device_t);
181142407Simpstatic int ti_attach(device_t);
182142407Simpstatic int ti_detach(device_t);
183142407Simpstatic void ti_txeof(struct ti_softc *);
184142407Simpstatic void ti_rxeof(struct ti_softc *);
18545386Swpaul
186142407Simpstatic void ti_stats_update(struct ti_softc *);
187153982Syongaristatic int ti_encap(struct ti_softc *, struct mbuf **);
18845386Swpaul
189142407Simpstatic void ti_intr(void *);
190142407Simpstatic void ti_start(struct ifnet *);
191153770Syongaristatic void ti_start_locked(struct ifnet *);
192142407Simpstatic int ti_ioctl(struct ifnet *, u_long, caddr_t);
193142407Simpstatic void ti_init(void *);
194153770Syongaristatic void ti_init_locked(void *);
195142407Simpstatic void ti_init2(struct ti_softc *);
196142407Simpstatic void ti_stop(struct ti_softc *);
197199559Sjhbstatic void ti_watchdog(void *);
198173839Syongaristatic int ti_shutdown(device_t);
199142407Simpstatic int ti_ifmedia_upd(struct ifnet *);
200142407Simpstatic void ti_ifmedia_sts(struct ifnet *, struct ifmediareq *);
20145386Swpaul
202142407Simpstatic u_int32_t ti_eeprom_putbyte(struct ti_softc *, int);
203142407Simpstatic u_int8_t	ti_eeprom_getbyte(struct ti_softc *, int, u_int8_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
210153770Syongaristatic void ti_mem_read(struct ti_softc *, u_int32_t, u_int32_t, void *);
211153770Syongaristatic void ti_mem_write(struct ti_softc *, u_int32_t, u_int32_t, void *);
212153770Syongaristatic void ti_mem_zero(struct ti_softc *, u_int32_t, u_int32_t);
213142407Simpstatic int ti_copy_mem(struct ti_softc *, u_int32_t, u_int32_t, caddr_t, int, int);
214142407Simpstatic int ti_copy_scratch(struct ti_softc *, u_int32_t, u_int32_t, caddr_t,
215142407Simp		int, int, int);
216142407Simpstatic int ti_bcopy_swap(const void *, void *, size_t, ti_swap_type);
217142407Simpstatic void ti_loadfw(struct ti_softc *);
218142407Simpstatic void ti_cmd(struct ti_softc *, struct ti_cmd_desc *);
219142407Simpstatic void ti_cmd_ext(struct ti_softc *, struct ti_cmd_desc *, caddr_t, int);
220142407Simpstatic void ti_handle_events(struct ti_softc *);
221153396Sscottlstatic int ti_alloc_dmamaps(struct ti_softc *);
222153396Sscottlstatic void ti_free_dmamaps(struct ti_softc *);
223153396Sscottlstatic int ti_alloc_jumbo_mem(struct ti_softc *);
22498849Sken#ifdef TI_PRIVATE_JUMBOS
225142407Simpstatic void *ti_jalloc(struct ti_softc *);
226142407Simpstatic void ti_jfree(void *, void *);
22798849Sken#endif /* TI_PRIVATE_JUMBOS */
228142407Simpstatic int ti_newbuf_std(struct ti_softc *, int, struct mbuf *);
229142407Simpstatic int ti_newbuf_mini(struct ti_softc *, int, struct mbuf *);
230142407Simpstatic int ti_newbuf_jumbo(struct ti_softc *, int, struct mbuf *);
231142407Simpstatic int ti_init_rx_ring_std(struct ti_softc *);
232142407Simpstatic void ti_free_rx_ring_std(struct ti_softc *);
233142407Simpstatic int ti_init_rx_ring_jumbo(struct ti_softc *);
234142407Simpstatic void ti_free_rx_ring_jumbo(struct ti_softc *);
235142407Simpstatic int ti_init_rx_ring_mini(struct ti_softc *);
236142407Simpstatic void ti_free_rx_ring_mini(struct ti_softc *);
237142407Simpstatic void ti_free_tx_ring(struct ti_softc *);
238142407Simpstatic int ti_init_tx_ring(struct ti_softc *);
23945386Swpaul
240142407Simpstatic int ti_64bitslot_war(struct ti_softc *);
241142407Simpstatic int ti_chipinit(struct ti_softc *);
242142407Simpstatic int ti_gibinit(struct ti_softc *);
24345386Swpaul
24498849Sken#ifdef TI_JUMBO_HDRSPLIT
24599013Speterstatic __inline void ti_hdr_split	(struct mbuf *top, int hdr_len,
24699013Speter					     int pkt_len, int idx);
24798849Sken#endif /* TI_JUMBO_HDRSPLIT */
24898849Sken
24949011Swpaulstatic device_method_t ti_methods[] = {
25049011Swpaul	/* Device interface */
25149011Swpaul	DEVMETHOD(device_probe,		ti_probe),
25249011Swpaul	DEVMETHOD(device_attach,	ti_attach),
25349011Swpaul	DEVMETHOD(device_detach,	ti_detach),
25449011Swpaul	DEVMETHOD(device_shutdown,	ti_shutdown),
25549011Swpaul	{ 0, 0 }
25649011Swpaul};
25749011Swpaul
25849011Swpaulstatic driver_t ti_driver = {
25951455Swpaul	"ti",
26049011Swpaul	ti_methods,
26149011Swpaul	sizeof(struct ti_softc)
26249011Swpaul};
26349011Swpaul
26449011Swpaulstatic devclass_t ti_devclass;
26549011Swpaul
266113506SmdoddDRIVER_MODULE(ti, pci, ti_driver, ti_devclass, 0, 0);
267113506SmdoddMODULE_DEPEND(ti, pci, 1, 1, 1);
268113506SmdoddMODULE_DEPEND(ti, ether, 1, 1, 1);
26949011Swpaul
27045386Swpaul/*
27145386Swpaul * Send an instruction or address to the EEPROM, check for ACK.
27245386Swpaul */
27345386Swpaulstatic u_int32_t ti_eeprom_putbyte(sc, byte)
27445386Swpaul	struct ti_softc		*sc;
27545386Swpaul	int			byte;
27645386Swpaul{
277153770Syongari	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 */
31945386Swpaulstatic u_int8_t ti_eeprom_getbyte(sc, addr, dest)
32045386Swpaul	struct ti_softc		*sc;
32145386Swpaul	int			addr;
32245386Swpaul	u_int8_t		*dest;
32345386Swpaul{
324153770Syongari	int			i;
32545386Swpaul	u_int8_t		byte = 0;
32645386Swpaul
32745386Swpaul	EEPROM_START;
32845386Swpaul
32945386Swpaul	/*
33045386Swpaul	 * Send write control code to EEPROM.
33145386Swpaul	 */
33245386Swpaul	if (ti_eeprom_putbyte(sc, EEPROM_CTL_WRITE)) {
333162321Sglebius		device_printf(sc->ti_dev,
334150719Sjhb		    "failed to send write command, status: %x\n",
335150719Sjhb		    CSR_READ_4(sc, TI_MISC_LOCAL_CTL));
336131654Sbms		return (1);
33745386Swpaul	}
33845386Swpaul
33945386Swpaul	/*
34045386Swpaul	 * Send first byte of address of byte we want to read.
34145386Swpaul	 */
34245386Swpaul	if (ti_eeprom_putbyte(sc, (addr >> 8) & 0xFF)) {
343162321Sglebius		device_printf(sc->ti_dev, "failed to send address, status: %x\n",
344150719Sjhb		    CSR_READ_4(sc, TI_MISC_LOCAL_CTL));
345131654Sbms		return (1);
34645386Swpaul	}
34745386Swpaul	/*
34845386Swpaul	 * Send second byte address of byte we want to read.
34945386Swpaul	 */
35045386Swpaul	if (ti_eeprom_putbyte(sc, addr & 0xFF)) {
351162321Sglebius		device_printf(sc->ti_dev, "failed to send address, status: %x\n",
352150719Sjhb		    CSR_READ_4(sc, TI_MISC_LOCAL_CTL));
353131654Sbms		return (1);
35445386Swpaul	}
35545386Swpaul
35645386Swpaul	EEPROM_STOP;
35745386Swpaul	EEPROM_START;
35845386Swpaul	/*
35945386Swpaul	 * Send read control code to EEPROM.
36045386Swpaul	 */
36145386Swpaul	if (ti_eeprom_putbyte(sc, EEPROM_CTL_READ)) {
362162321Sglebius		device_printf(sc->ti_dev,
363150719Sjhb		    "failed to send read command, status: %x\n",
364150719Sjhb		    CSR_READ_4(sc, TI_MISC_LOCAL_CTL));
365131654Sbms		return (1);
36645386Swpaul	}
36745386Swpaul
36845386Swpaul	/*
36945386Swpaul	 * Start reading bits from EEPROM.
37045386Swpaul	 */
37145386Swpaul	TI_CLRBIT(sc, TI_MISC_LOCAL_CTL, TI_MLC_EE_TXEN);
37245386Swpaul	for (i = 0x80; i; i >>= 1) {
37345386Swpaul		TI_SETBIT(sc, TI_MISC_LOCAL_CTL, TI_MLC_EE_CLK);
37445386Swpaul		DELAY(1);
37545386Swpaul		if (CSR_READ_4(sc, TI_MISC_LOCAL_CTL) & TI_MLC_EE_DIN)
37645386Swpaul			byte |= i;
37745386Swpaul		TI_CLRBIT(sc, TI_MISC_LOCAL_CTL, TI_MLC_EE_CLK);
37845386Swpaul		DELAY(1);
37945386Swpaul	}
38045386Swpaul
38145386Swpaul	EEPROM_STOP;
38245386Swpaul
38345386Swpaul	/*
38445386Swpaul	 * No ACK generated for read, so just return byte.
38545386Swpaul	 */
38645386Swpaul
38745386Swpaul	*dest = byte;
38845386Swpaul
389131654Sbms	return (0);
39045386Swpaul}
39145386Swpaul
39245386Swpaul/*
39345386Swpaul * Read a sequence of bytes from the EEPROM.
39445386Swpaul */
395102336Salfredstatic int
396102336Salfredti_read_eeprom(sc, dest, off, cnt)
39745386Swpaul	struct ti_softc		*sc;
39845386Swpaul	caddr_t			dest;
39945386Swpaul	int			off;
40045386Swpaul	int			cnt;
40145386Swpaul{
40245386Swpaul	int			err = 0, i;
40345386Swpaul	u_int8_t		byte = 0;
40445386Swpaul
40545386Swpaul	for (i = 0; i < cnt; i++) {
40645386Swpaul		err = ti_eeprom_getbyte(sc, off + i, &byte);
40745386Swpaul		if (err)
40845386Swpaul			break;
40945386Swpaul		*(dest + i) = byte;
41045386Swpaul	}
41145386Swpaul
412131654Sbms	return (err ? 1 : 0);
41345386Swpaul}
41445386Swpaul
41545386Swpaul/*
416153770Syongari * NIC memory read function.
417153770Syongari * Can be used to copy data from NIC local memory.
41845386Swpaul */
419102336Salfredstatic void
420153770Syongariti_mem_read(sc, addr, len, buf)
42145386Swpaul	struct ti_softc		*sc;
42245386Swpaul	u_int32_t		addr, len;
423153770Syongari	void			*buf;
42445386Swpaul{
42545386Swpaul	int			segptr, segsize, cnt;
426153770Syongari	char			*ptr;
42745386Swpaul
42845386Swpaul	segptr = addr;
42945386Swpaul	cnt = len;
43045386Swpaul	ptr = buf;
43145386Swpaul
432131654Sbms	while (cnt) {
43345386Swpaul		if (cnt < TI_WINLEN)
43445386Swpaul			segsize = cnt;
43545386Swpaul		else
43645386Swpaul			segsize = TI_WINLEN - (segptr % TI_WINLEN);
43745386Swpaul		CSR_WRITE_4(sc, TI_WINBASE, (segptr & ~(TI_WINLEN - 1)));
438153770Syongari		bus_space_read_region_4(sc->ti_btag, sc->ti_bhandle,
439153770Syongari		    TI_WINDOW + (segptr & (TI_WINLEN - 1)), (u_int32_t *)ptr,
440153770Syongari		    segsize / 4);
441153770Syongari		ptr += segsize;
44245386Swpaul		segptr += segsize;
44345386Swpaul		cnt -= segsize;
44445386Swpaul	}
44545386Swpaul}
44645386Swpaul
447153770Syongari
448153770Syongari/*
449153770Syongari * NIC memory write function.
450153770Syongari * Can be used to copy data into NIC local memory.
451153770Syongari */
452153770Syongaristatic void
453153770Syongariti_mem_write(sc, addr, len, buf)
454153770Syongari	struct ti_softc		*sc;
455153770Syongari	u_int32_t		addr, len;
456153770Syongari	void			*buf;
457153770Syongari{
458153770Syongari	int			segptr, segsize, cnt;
459153770Syongari	char			*ptr;
460153770Syongari
461153770Syongari	segptr = addr;
462153770Syongari	cnt = len;
463153770Syongari	ptr = buf;
464153770Syongari
465153770Syongari	while (cnt) {
466153770Syongari		if (cnt < TI_WINLEN)
467153770Syongari			segsize = cnt;
468153770Syongari		else
469153770Syongari			segsize = TI_WINLEN - (segptr % TI_WINLEN);
470153770Syongari		CSR_WRITE_4(sc, TI_WINBASE, (segptr & ~(TI_WINLEN - 1)));
471153770Syongari		bus_space_write_region_4(sc->ti_btag, sc->ti_bhandle,
472153770Syongari		    TI_WINDOW + (segptr & (TI_WINLEN - 1)), (u_int32_t *)ptr,
473153770Syongari		    segsize / 4);
474153770Syongari		ptr += segsize;
475153770Syongari		segptr += segsize;
476153770Syongari		cnt -= segsize;
477153770Syongari	}
478153770Syongari}
479153770Syongari
480153770Syongari/*
481153770Syongari * NIC memory read function.
482153770Syongari * Can be used to clear a section of NIC local memory.
483153770Syongari */
484153770Syongaristatic void
485153770Syongariti_mem_zero(sc, addr, len)
486153770Syongari	struct ti_softc		*sc;
487153770Syongari	u_int32_t		addr, len;
488153770Syongari{
489153770Syongari	int			segptr, segsize, cnt;
490153770Syongari
491153770Syongari	segptr = addr;
492153770Syongari	cnt = len;
493153770Syongari
494153770Syongari	while (cnt) {
495153770Syongari		if (cnt < TI_WINLEN)
496153770Syongari			segsize = cnt;
497153770Syongari		else
498153770Syongari			segsize = TI_WINLEN - (segptr % TI_WINLEN);
499153770Syongari		CSR_WRITE_4(sc, TI_WINBASE, (segptr & ~(TI_WINLEN - 1)));
500153770Syongari		bus_space_set_region_4(sc->ti_btag, sc->ti_bhandle,
501153770Syongari		    TI_WINDOW + (segptr & (TI_WINLEN - 1)), 0, segsize / 4);
502153770Syongari		segptr += segsize;
503153770Syongari		cnt -= segsize;
504153770Syongari	}
505153770Syongari}
506153770Syongari
50798849Skenstatic int
50898849Skenti_copy_mem(sc, tigon_addr, len, buf, useraddr, readdata)
50998849Sken	struct ti_softc		*sc;
51098849Sken	u_int32_t		tigon_addr, len;
51198849Sken	caddr_t			buf;
51298849Sken	int			useraddr, readdata;
51398849Sken{
51498849Sken	int		segptr, segsize, cnt;
51598849Sken	caddr_t		ptr;
51698849Sken	u_int32_t	origwin;
51798849Sken	u_int8_t	tmparray[TI_WINLEN], tmparray2[TI_WINLEN];
51898849Sken	int		resid, segresid;
51998849Sken	int		first_pass;
52098849Sken
521153770Syongari	TI_LOCK_ASSERT(sc);
522153770Syongari
52398849Sken	/*
52498849Sken	 * At the moment, we don't handle non-aligned cases, we just bail.
52598849Sken	 * If this proves to be a problem, it will be fixed.
52698849Sken	 */
52798849Sken	if ((readdata == 0)
52898849Sken	 && (tigon_addr & 0x3)) {
529162321Sglebius		device_printf(sc->ti_dev, "%s: tigon address %#x isn't "
530162321Sglebius		    "word-aligned\n", __func__, tigon_addr);
531162321Sglebius		device_printf(sc->ti_dev, "%s: unaligned writes aren't "
532162321Sglebius		    "yet supported\n", __func__);
533131654Sbms		return (EINVAL);
53498849Sken	}
53598849Sken
53698849Sken	segptr = tigon_addr & ~0x3;
53798849Sken	segresid = tigon_addr - segptr;
53898849Sken
53998849Sken	/*
54098849Sken	 * This is the non-aligned amount left over that we'll need to
54198849Sken	 * copy.
54298849Sken	 */
54398849Sken	resid = len & 0x3;
54498849Sken
54598849Sken	/* Add in the left over amount at the front of the buffer */
54698849Sken	resid += segresid;
54798849Sken
54898849Sken	cnt = len & ~0x3;
54998849Sken	/*
55098849Sken	 * If resid + segresid is >= 4, add multiples of 4 to the count and
55198849Sken	 * decrease the residual by that much.
55298849Sken	 */
55398849Sken	cnt += resid & ~0x3;
55498849Sken	resid -= resid & ~0x3;
55598849Sken
55698849Sken	ptr = buf;
55798849Sken
55898849Sken	first_pass = 1;
55998849Sken
56098849Sken	/*
56198849Sken	 * Save the old window base value.
56298849Sken	 */
56398849Sken	origwin = CSR_READ_4(sc, TI_WINBASE);
56498849Sken
565131654Sbms	while (cnt) {
56698849Sken		bus_size_t ti_offset;
56798849Sken
56898849Sken		if (cnt < TI_WINLEN)
56998849Sken			segsize = cnt;
57098849Sken		else
57198849Sken			segsize = TI_WINLEN - (segptr % TI_WINLEN);
57298849Sken		CSR_WRITE_4(sc, TI_WINBASE, (segptr & ~(TI_WINLEN - 1)));
57398849Sken
57498849Sken		ti_offset = TI_WINDOW + (segptr & (TI_WINLEN -1));
57598849Sken
57698849Sken		if (readdata) {
57798849Sken
57898849Sken			bus_space_read_region_4(sc->ti_btag,
57998849Sken						sc->ti_bhandle, ti_offset,
58098849Sken						(u_int32_t *)tmparray,
58198849Sken						segsize >> 2);
58298849Sken			if (useraddr) {
58398849Sken				/*
58498849Sken				 * Yeah, this is a little on the kludgy
58598849Sken				 * side, but at least this code is only
58698849Sken				 * used for debugging.
58798849Sken				 */
58898849Sken				ti_bcopy_swap(tmparray, tmparray2, segsize,
58998849Sken					      TI_SWAP_NTOH);
59098849Sken
591153281Sscottl				TI_UNLOCK(sc);
59298849Sken				if (first_pass) {
59398849Sken					copyout(&tmparray2[segresid], ptr,
59498849Sken						segsize - segresid);
59598849Sken					first_pass = 0;
59698849Sken				} else
59798849Sken					copyout(tmparray2, ptr, segsize);
598153281Sscottl				TI_LOCK(sc);
59998849Sken			} else {
60098849Sken				if (first_pass) {
60198849Sken
60298849Sken					ti_bcopy_swap(tmparray, tmparray2,
60398849Sken						      segsize, TI_SWAP_NTOH);
604153281Sscottl					TI_UNLOCK(sc);
60598849Sken					bcopy(&tmparray2[segresid], ptr,
60698849Sken					      segsize - segresid);
607153281Sscottl					TI_LOCK(sc);
60898849Sken					first_pass = 0;
60998849Sken				} else
61098849Sken					ti_bcopy_swap(tmparray, ptr, segsize,
61198849Sken						      TI_SWAP_NTOH);
61298849Sken			}
61398849Sken
61498849Sken		} else {
61598849Sken			if (useraddr) {
616153281Sscottl				TI_UNLOCK(sc);
61798849Sken				copyin(ptr, tmparray2, segsize);
618153281Sscottl				TI_LOCK(sc);
61998849Sken				ti_bcopy_swap(tmparray2, tmparray, segsize,
62098849Sken					      TI_SWAP_HTON);
62198849Sken			} else
62298849Sken				ti_bcopy_swap(ptr, tmparray, segsize,
62398849Sken					      TI_SWAP_HTON);
62498849Sken
62598849Sken			bus_space_write_region_4(sc->ti_btag,
62698849Sken						 sc->ti_bhandle, ti_offset,
62798849Sken						 (u_int32_t *)tmparray,
62898849Sken						 segsize >> 2);
62998849Sken		}
63098849Sken		segptr += segsize;
63198849Sken		ptr += segsize;
63298849Sken		cnt -= segsize;
63398849Sken	}
63498849Sken
63598849Sken	/*
63698849Sken	 * Handle leftover, non-word-aligned bytes.
63798849Sken	 */
63898849Sken	if (resid != 0) {
63998849Sken		u_int32_t	tmpval, tmpval2;
64098849Sken		bus_size_t	ti_offset;
64198849Sken
64298849Sken		/*
64398849Sken		 * Set the segment pointer.
64498849Sken		 */
64598849Sken		CSR_WRITE_4(sc, TI_WINBASE, (segptr & ~(TI_WINLEN - 1)));
64698849Sken
64798849Sken		ti_offset = TI_WINDOW + (segptr & (TI_WINLEN - 1));
64898849Sken
64998849Sken		/*
65098849Sken		 * First, grab whatever is in our source/destination.
65198849Sken		 * We'll obviously need this for reads, but also for
65298849Sken		 * writes, since we'll be doing read/modify/write.
65398849Sken		 */
65498849Sken		bus_space_read_region_4(sc->ti_btag, sc->ti_bhandle,
65598849Sken					ti_offset, &tmpval, 1);
65698849Sken
65798849Sken		/*
65898849Sken		 * Next, translate this from little-endian to big-endian
65998849Sken		 * (at least on i386 boxes).
66098849Sken		 */
66198849Sken		tmpval2 = ntohl(tmpval);
66298849Sken
66398849Sken		if (readdata) {
66498849Sken			/*
66598849Sken			 * If we're reading, just copy the leftover number
66698849Sken			 * of bytes from the host byte order buffer to
66798849Sken			 * the user's buffer.
66898849Sken			 */
669153281Sscottl			if (useraddr) {
670153281Sscottl				TI_UNLOCK(sc);
67198849Sken				copyout(&tmpval2, ptr, resid);
672153281Sscottl				TI_LOCK(sc);
673153281Sscottl			} else
67498849Sken				bcopy(&tmpval2, ptr, resid);
67598849Sken		} else {
67698849Sken			/*
67798849Sken			 * If we're writing, first copy the bytes to be
67898849Sken			 * written into the network byte order buffer,
67998849Sken			 * leaving the rest of the buffer with whatever was
68098849Sken			 * originally in there.  Then, swap the bytes
68198849Sken			 * around into host order and write them out.
68298849Sken			 *
68398849Sken			 * XXX KDM the read side of this has been verified
68498849Sken			 * to work, but the write side of it has not been
68598849Sken			 * verified.  So user beware.
68698849Sken			 */
687153281Sscottl			if (useraddr) {
688153281Sscottl				TI_UNLOCK(sc);
68998849Sken				copyin(ptr, &tmpval2, resid);
690153281Sscottl				TI_LOCK(sc);
691153281Sscottl			} else
69298849Sken				bcopy(ptr, &tmpval2, resid);
69398849Sken
69498849Sken			tmpval = htonl(tmpval2);
69598849Sken
69698849Sken			bus_space_write_region_4(sc->ti_btag, sc->ti_bhandle,
69798849Sken						 ti_offset, &tmpval, 1);
69898849Sken		}
69998849Sken	}
70098849Sken
70198849Sken	CSR_WRITE_4(sc, TI_WINBASE, origwin);
70298849Sken
703131654Sbms	return (0);
70498849Sken}
70598849Sken
70698849Skenstatic int
70798849Skenti_copy_scratch(sc, tigon_addr, len, buf, useraddr, readdata, cpu)
70898849Sken	struct ti_softc		*sc;
70998849Sken	u_int32_t		tigon_addr, len;
71098849Sken	caddr_t			buf;
71198849Sken	int			useraddr, readdata;
71298849Sken	int			cpu;
71398849Sken{
71498849Sken	u_int32_t	segptr;
71598849Sken	int		cnt;
71698849Sken	u_int32_t	tmpval, tmpval2;
71798849Sken	caddr_t		ptr;
71898849Sken
719153770Syongari	TI_LOCK_ASSERT(sc);
720153770Syongari
72198849Sken	/*
72298849Sken	 * At the moment, we don't handle non-aligned cases, we just bail.
72398849Sken	 * If this proves to be a problem, it will be fixed.
72498849Sken	 */
72598849Sken	if (tigon_addr & 0x3) {
726162321Sglebius		device_printf(sc->ti_dev, "%s: tigon address %#x "
727162321Sglebius		    "isn't word-aligned\n", __func__, tigon_addr);
728131654Sbms		return (EINVAL);
72998849Sken	}
73098849Sken
73198849Sken	if (len & 0x3) {
732162321Sglebius		device_printf(sc->ti_dev, "%s: transfer length %d "
733162321Sglebius		    "isn't word-aligned\n", __func__, len);
734131654Sbms		return (EINVAL);
73598849Sken	}
73698849Sken
73798849Sken	segptr = tigon_addr;
73898849Sken	cnt = len;
73998849Sken	ptr = buf;
74098849Sken
74198849Sken	while (cnt) {
74298849Sken		CSR_WRITE_4(sc, CPU_REG(TI_SRAM_ADDR, cpu), segptr);
74398849Sken
74498849Sken		if (readdata) {
74598849Sken			tmpval2 = CSR_READ_4(sc, CPU_REG(TI_SRAM_DATA, cpu));
74698849Sken
74798849Sken			tmpval = ntohl(tmpval2);
74898849Sken
74998849Sken			/*
75098849Sken			 * Note:  I've used this debugging interface
75198849Sken			 * extensively with Alteon's 12.3.15 firmware,
75298849Sken			 * compiled with GCC 2.7.2.1 and binutils 2.9.1.
75398849Sken			 *
75498849Sken			 * When you compile the firmware without
75598849Sken			 * optimization, which is necessary sometimes in
75698849Sken			 * order to properly step through it, you sometimes
757131652Sbms			 * read out a bogus value of 0xc0017c instead of
75898849Sken			 * whatever was supposed to be in that scratchpad
75998849Sken			 * location.  That value is on the stack somewhere,
76098849Sken			 * but I've never been able to figure out what was
76198849Sken			 * causing the problem.
76298849Sken			 *
76398849Sken			 * The address seems to pop up in random places,
76498849Sken			 * often not in the same place on two subsequent
76598849Sken			 * reads.
76698849Sken			 *
76798849Sken			 * In any case, the underlying data doesn't seem
76898849Sken			 * to be affected, just the value read out.
76998849Sken			 *
77098849Sken			 * KDM, 3/7/2000
77198849Sken			 */
77298849Sken
77398849Sken			if (tmpval2 == 0xc0017c)
774162321Sglebius				device_printf(sc->ti_dev, "found 0xc0017c at "
775162321Sglebius				    "%#x (tmpval2)\n", segptr);
77698849Sken
77798849Sken			if (tmpval == 0xc0017c)
778162321Sglebius				device_printf(sc->ti_dev, "found 0xc0017c at "
779162321Sglebius				    "%#x (tmpval)\n", segptr);
78098849Sken
78198849Sken			if (useraddr)
78298849Sken				copyout(&tmpval, ptr, 4);
78398849Sken			else
78498849Sken				bcopy(&tmpval, ptr, 4);
78598849Sken		} else {
78698849Sken			if (useraddr)
78798849Sken				copyin(ptr, &tmpval2, 4);
78898849Sken			else
78998849Sken				bcopy(ptr, &tmpval2, 4);
79098849Sken
79198849Sken			tmpval = htonl(tmpval2);
79298849Sken
79398849Sken			CSR_WRITE_4(sc, CPU_REG(TI_SRAM_DATA, cpu), tmpval);
79498849Sken		}
79598849Sken
79698849Sken		cnt -= 4;
79798849Sken		segptr += 4;
79898849Sken		ptr += 4;
79998849Sken	}
80098849Sken
801131654Sbms	return (0);
80298849Sken}
80398849Sken
80498849Skenstatic int
80598849Skenti_bcopy_swap(src, dst, len, swap_type)
80698849Sken	const void	*src;
80798849Sken	void		*dst;
80898849Sken	size_t		len;
80998849Sken	ti_swap_type	swap_type;
81098849Sken{
81198849Sken	const u_int8_t *tmpsrc;
81298849Sken	u_int8_t *tmpdst;
81398849Sken	size_t tmplen;
81498849Sken
81598849Sken	if (len & 0x3) {
816106627Sjhb		printf("ti_bcopy_swap: length %zd isn't 32-bit aligned\n",
81798849Sken		       len);
818131654Sbms		return (-1);
81998849Sken	}
82098849Sken
82198849Sken	tmpsrc = src;
82298849Sken	tmpdst = dst;
82398849Sken	tmplen = len;
82498849Sken
82598849Sken	while (tmplen) {
82698849Sken		if (swap_type == TI_SWAP_NTOH)
82798849Sken			*(u_int32_t *)tmpdst =
82898849Sken				ntohl(*(const u_int32_t *)tmpsrc);
82998849Sken		else
83098849Sken			*(u_int32_t *)tmpdst =
83198849Sken				htonl(*(const u_int32_t *)tmpsrc);
83298849Sken
83398849Sken		tmpsrc += 4;
83498849Sken		tmpdst += 4;
83598849Sken		tmplen -= 4;
83698849Sken	}
83798849Sken
838131654Sbms	return (0);
83998849Sken}
84098849Sken
84145386Swpaul/*
84245386Swpaul * Load firmware image into the NIC. Check that the firmware revision
84345386Swpaul * is acceptable and see if we want the firmware for the Tigon 1 or
84445386Swpaul * Tigon 2.
84545386Swpaul */
846102336Salfredstatic void
847102336Salfredti_loadfw(sc)
84845386Swpaul	struct ti_softc		*sc;
84945386Swpaul{
850153770Syongari
851153770Syongari	TI_LOCK_ASSERT(sc);
852153770Syongari
853131654Sbms	switch (sc->ti_hwrev) {
85445386Swpaul	case TI_HWREV_TIGON:
85545386Swpaul		if (tigonFwReleaseMajor != TI_FIRMWARE_MAJOR ||
85645386Swpaul		    tigonFwReleaseMinor != TI_FIRMWARE_MINOR ||
85745386Swpaul		    tigonFwReleaseFix != TI_FIRMWARE_FIX) {
858162321Sglebius			device_printf(sc->ti_dev, "firmware revision mismatch; "
859150719Sjhb			    "want %d.%d.%d, got %d.%d.%d\n",
86045386Swpaul			    TI_FIRMWARE_MAJOR, TI_FIRMWARE_MINOR,
86145386Swpaul			    TI_FIRMWARE_FIX, tigonFwReleaseMajor,
86245386Swpaul			    tigonFwReleaseMinor, tigonFwReleaseFix);
86345386Swpaul			return;
86445386Swpaul		}
865153770Syongari		ti_mem_write(sc, tigonFwTextAddr, tigonFwTextLen, tigonFwText);
866153770Syongari		ti_mem_write(sc, tigonFwDataAddr, tigonFwDataLen, tigonFwData);
867153770Syongari		ti_mem_write(sc, tigonFwRodataAddr, tigonFwRodataLen,
868153770Syongari		    tigonFwRodata);
869153770Syongari		ti_mem_zero(sc, tigonFwBssAddr, tigonFwBssLen);
870153770Syongari		ti_mem_zero(sc, tigonFwSbssAddr, tigonFwSbssLen);
87145386Swpaul		CSR_WRITE_4(sc, TI_CPU_PROGRAM_COUNTER, tigonFwStartAddr);
87245386Swpaul		break;
87345386Swpaul	case TI_HWREV_TIGON_II:
87445386Swpaul		if (tigon2FwReleaseMajor != TI_FIRMWARE_MAJOR ||
87545386Swpaul		    tigon2FwReleaseMinor != TI_FIRMWARE_MINOR ||
87645386Swpaul		    tigon2FwReleaseFix != TI_FIRMWARE_FIX) {
877162321Sglebius			device_printf(sc->ti_dev, "firmware revision mismatch; "
878150719Sjhb			    "want %d.%d.%d, got %d.%d.%d\n",
87945386Swpaul			    TI_FIRMWARE_MAJOR, TI_FIRMWARE_MINOR,
88045386Swpaul			    TI_FIRMWARE_FIX, tigon2FwReleaseMajor,
88145386Swpaul			    tigon2FwReleaseMinor, tigon2FwReleaseFix);
88245386Swpaul			return;
88345386Swpaul		}
884153770Syongari		ti_mem_write(sc, tigon2FwTextAddr, tigon2FwTextLen,
885153770Syongari		    tigon2FwText);
886153770Syongari		ti_mem_write(sc, tigon2FwDataAddr, tigon2FwDataLen,
887153770Syongari		    tigon2FwData);
888153770Syongari		ti_mem_write(sc, tigon2FwRodataAddr, tigon2FwRodataLen,
889153770Syongari		    tigon2FwRodata);
890153770Syongari		ti_mem_zero(sc, tigon2FwBssAddr, tigon2FwBssLen);
891153770Syongari		ti_mem_zero(sc, tigon2FwSbssAddr, tigon2FwSbssLen);
89245386Swpaul		CSR_WRITE_4(sc, TI_CPU_PROGRAM_COUNTER, tigon2FwStartAddr);
89345386Swpaul		break;
89445386Swpaul	default:
895162321Sglebius		device_printf(sc->ti_dev,
896150719Sjhb		    "can't load firmware: unknown hardware rev\n");
89745386Swpaul		break;
89845386Swpaul	}
89945386Swpaul}
90045386Swpaul
90145386Swpaul/*
90245386Swpaul * Send the NIC a command via the command ring.
90345386Swpaul */
904102336Salfredstatic void
905102336Salfredti_cmd(sc, cmd)
90645386Swpaul	struct ti_softc		*sc;
90745386Swpaul	struct ti_cmd_desc	*cmd;
90845386Swpaul{
909153982Syongari	int			index;
91045386Swpaul
91145386Swpaul	index = sc->ti_cmd_saved_prodidx;
91245386Swpaul	CSR_WRITE_4(sc, TI_GCR_CMDRING + (index * 4), *(u_int32_t *)(cmd));
91345386Swpaul	TI_INC(index, TI_CMD_RING_CNT);
91445386Swpaul	CSR_WRITE_4(sc, TI_MB_CMDPROD_IDX, index);
91545386Swpaul	sc->ti_cmd_saved_prodidx = index;
91645386Swpaul}
91745386Swpaul
91845386Swpaul/*
91945386Swpaul * Send the NIC an extended command. The 'len' parameter specifies the
92045386Swpaul * number of command slots to include after the initial command.
92145386Swpaul */
922102336Salfredstatic void
923102336Salfredti_cmd_ext(sc, cmd, arg, len)
92445386Swpaul	struct ti_softc		*sc;
92545386Swpaul	struct ti_cmd_desc	*cmd;
92645386Swpaul	caddr_t			arg;
92745386Swpaul	int			len;
92845386Swpaul{
929153982Syongari	int			index;
930153770Syongari	int			i;
93145386Swpaul
93245386Swpaul	index = sc->ti_cmd_saved_prodidx;
93345386Swpaul	CSR_WRITE_4(sc, TI_GCR_CMDRING + (index * 4), *(u_int32_t *)(cmd));
93445386Swpaul	TI_INC(index, TI_CMD_RING_CNT);
93545386Swpaul	for (i = 0; i < len; i++) {
93645386Swpaul		CSR_WRITE_4(sc, TI_GCR_CMDRING + (index * 4),
93745386Swpaul		    *(u_int32_t *)(&arg[i * 4]));
93845386Swpaul		TI_INC(index, TI_CMD_RING_CNT);
93945386Swpaul	}
94045386Swpaul	CSR_WRITE_4(sc, TI_MB_CMDPROD_IDX, index);
94145386Swpaul	sc->ti_cmd_saved_prodidx = index;
94245386Swpaul}
94345386Swpaul
94445386Swpaul/*
94545386Swpaul * Handle events that have triggered interrupts.
94645386Swpaul */
947102336Salfredstatic void
948102336Salfredti_handle_events(sc)
94945386Swpaul	struct ti_softc		*sc;
95045386Swpaul{
95145386Swpaul	struct ti_event_desc	*e;
95245386Swpaul
95345386Swpaul	if (sc->ti_rdata->ti_event_ring == NULL)
95445386Swpaul		return;
95545386Swpaul
95645386Swpaul	while (sc->ti_ev_saved_considx != sc->ti_ev_prodidx.ti_idx) {
95745386Swpaul		e = &sc->ti_rdata->ti_event_ring[sc->ti_ev_saved_considx];
958153770Syongari		switch (TI_EVENT_EVENT(e)) {
95945386Swpaul		case TI_EV_LINKSTAT_CHANGED:
960153770Syongari			sc->ti_linkstat = TI_EVENT_CODE(e);
961153770Syongari			if (sc->ti_linkstat == TI_EV_CODE_LINK_UP)
962162321Sglebius				device_printf(sc->ti_dev, "10/100 link up\n");
963153770Syongari			else if (sc->ti_linkstat == TI_EV_CODE_GIG_LINK_UP)
964162321Sglebius				device_printf(sc->ti_dev, "gigabit link up\n");
965153770Syongari			else if (sc->ti_linkstat == TI_EV_CODE_LINK_DOWN)
966162321Sglebius				device_printf(sc->ti_dev, "link down\n");
96745386Swpaul			break;
96845386Swpaul		case TI_EV_ERROR:
969153770Syongari			if (TI_EVENT_CODE(e) == TI_EV_CODE_ERR_INVAL_CMD)
970162321Sglebius				device_printf(sc->ti_dev, "invalid command\n");
971153770Syongari			else if (TI_EVENT_CODE(e) == TI_EV_CODE_ERR_UNIMP_CMD)
972162321Sglebius				device_printf(sc->ti_dev, "unknown command\n");
973153770Syongari			else if (TI_EVENT_CODE(e) == TI_EV_CODE_ERR_BADCFG)
974162321Sglebius				device_printf(sc->ti_dev, "bad config data\n");
97545386Swpaul			break;
97645386Swpaul		case TI_EV_FIRMWARE_UP:
97745386Swpaul			ti_init2(sc);
97845386Swpaul			break;
97945386Swpaul		case TI_EV_STATS_UPDATED:
98045386Swpaul			ti_stats_update(sc);
98145386Swpaul			break;
98245386Swpaul		case TI_EV_RESET_JUMBO_RING:
98345386Swpaul		case TI_EV_MCAST_UPDATED:
98445386Swpaul			/* Who cares. */
98545386Swpaul			break;
98645386Swpaul		default:
987162321Sglebius			device_printf(sc->ti_dev, "unknown event: %d\n",
988153770Syongari			    TI_EVENT_EVENT(e));
98945386Swpaul			break;
99045386Swpaul		}
99145386Swpaul		/* Advance the consumer index. */
99245386Swpaul		TI_INC(sc->ti_ev_saved_considx, TI_EVENT_RING_CNT);
99345386Swpaul		CSR_WRITE_4(sc, TI_GCR_EVENTCONS_IDX, sc->ti_ev_saved_considx);
99445386Swpaul	}
99545386Swpaul}
99645386Swpaul
997153396Sscottlstatic int
998153396Sscottlti_alloc_dmamaps(struct ti_softc *sc)
999153396Sscottl{
1000153396Sscottl	int i;
1001153396Sscottl
1002153396Sscottl	for (i = 0; i < TI_TX_RING_CNT; i++) {
1003153982Syongari		sc->ti_cdata.ti_txdesc[i].tx_m = NULL;
1004153982Syongari		sc->ti_cdata.ti_txdesc[i].tx_dmamap = 0;
1005153396Sscottl		if (bus_dmamap_create(sc->ti_mbuftx_dmat, 0,
1006153982Syongari				      &sc->ti_cdata.ti_txdesc[i].tx_dmamap))
1007153396Sscottl			return (ENOBUFS);
1008153396Sscottl	}
1009153396Sscottl	for (i = 0; i < TI_STD_RX_RING_CNT; i++) {
1010153396Sscottl		if (bus_dmamap_create(sc->ti_mbufrx_dmat, 0,
1011153396Sscottl				      &sc->ti_cdata.ti_rx_std_maps[i]))
1012153396Sscottl			return (ENOBUFS);
1013153396Sscottl	}
1014153396Sscottl
1015153396Sscottl	for (i = 0; i < TI_JUMBO_RX_RING_CNT; i++) {
1016153396Sscottl		if (bus_dmamap_create(sc->ti_jumbo_dmat, 0,
1017153396Sscottl				      &sc->ti_cdata.ti_rx_jumbo_maps[i]))
1018153396Sscottl			return (ENOBUFS);
1019153396Sscottl	}
1020153396Sscottl	for (i = 0; i < TI_MINI_RX_RING_CNT; i++) {
1021153396Sscottl		if (bus_dmamap_create(sc->ti_mbufrx_dmat, 0,
1022153396Sscottl				      &sc->ti_cdata.ti_rx_mini_maps[i]))
1023153396Sscottl			return (ENOBUFS);
1024153396Sscottl	}
1025153396Sscottl
1026153396Sscottl	return (0);
1027153396Sscottl}
1028153396Sscottl
1029153396Sscottlstatic void
1030153396Sscottlti_free_dmamaps(struct ti_softc *sc)
1031153396Sscottl{
1032153396Sscottl	int i;
1033153396Sscottl
1034153770Syongari	if (sc->ti_mbuftx_dmat)
1035153770Syongari		for (i = 0; i < TI_TX_RING_CNT; i++)
1036153982Syongari			if (sc->ti_cdata.ti_txdesc[i].tx_dmamap) {
1037153770Syongari				bus_dmamap_destroy(sc->ti_mbuftx_dmat,
1038153982Syongari				    sc->ti_cdata.ti_txdesc[i].tx_dmamap);
1039153982Syongari				sc->ti_cdata.ti_txdesc[i].tx_dmamap = 0;
1040153770Syongari			}
1041153396Sscottl
1042153770Syongari	if (sc->ti_mbufrx_dmat)
1043153770Syongari		for (i = 0; i < TI_STD_RX_RING_CNT; i++)
1044153770Syongari			if (sc->ti_cdata.ti_rx_std_maps[i]) {
1045153770Syongari				bus_dmamap_destroy(sc->ti_mbufrx_dmat,
1046153770Syongari				    sc->ti_cdata.ti_rx_std_maps[i]);
1047153770Syongari				sc->ti_cdata.ti_rx_std_maps[i] = 0;
1048153770Syongari			}
1049153396Sscottl
1050153770Syongari	if (sc->ti_jumbo_dmat)
1051153770Syongari		for (i = 0; i < TI_JUMBO_RX_RING_CNT; i++)
1052153770Syongari			if (sc->ti_cdata.ti_rx_jumbo_maps[i]) {
1053153770Syongari				bus_dmamap_destroy(sc->ti_jumbo_dmat,
1054153770Syongari				    sc->ti_cdata.ti_rx_jumbo_maps[i]);
1055153770Syongari				sc->ti_cdata.ti_rx_jumbo_maps[i] = 0;
1056153770Syongari			}
1057153770Syongari	if (sc->ti_mbufrx_dmat)
1058153770Syongari		for (i = 0; i < TI_MINI_RX_RING_CNT; i++)
1059153770Syongari			if (sc->ti_cdata.ti_rx_mini_maps[i]) {
1060153770Syongari				bus_dmamap_destroy(sc->ti_mbufrx_dmat,
1061153770Syongari				    sc->ti_cdata.ti_rx_mini_maps[i]);
1062153770Syongari				sc->ti_cdata.ti_rx_mini_maps[i] = 0;
1063153770Syongari			}
1064153396Sscottl}
1065153396Sscottl
106698849Sken#ifdef TI_PRIVATE_JUMBOS
106798849Sken
106845386Swpaul/*
106945386Swpaul * Memory management for the jumbo receive ring is a pain in the
107045386Swpaul * butt. We need to allocate at least 9018 bytes of space per frame,
107145386Swpaul * _and_ it has to be contiguous (unless you use the extended
107245386Swpaul * jumbo descriptor format). Using malloc() all the time won't
107345386Swpaul * work: malloc() allocates memory in powers of two, which means we
107445386Swpaul * would end up wasting a considerable amount of space by allocating
107545386Swpaul * 9K chunks. We don't have a jumbo mbuf cluster pool. Thus, we have
107645386Swpaul * to do our own memory management.
107745386Swpaul *
107845386Swpaul * The driver needs to allocate a contiguous chunk of memory at boot
107945386Swpaul * time. We then chop this up ourselves into 9K pieces and use them
108045386Swpaul * as external mbuf storage.
108145386Swpaul *
108245386Swpaul * One issue here is how much memory to allocate. The jumbo ring has
108345386Swpaul * 256 slots in it, but at 9K per slot than can consume over 2MB of
108445386Swpaul * RAM. This is a bit much, especially considering we also need
108545386Swpaul * RAM for the standard ring and mini ring (on the Tigon 2). To
108645386Swpaul * save space, we only actually allocate enough memory for 64 slots
108745386Swpaul * by default, which works out to between 500 and 600K. This can
108845386Swpaul * be tuned by changing a #define in if_tireg.h.
108945386Swpaul */
109045386Swpaul
1091102336Salfredstatic int
1092102336Salfredti_alloc_jumbo_mem(sc)
109345386Swpaul	struct ti_softc		*sc;
109445386Swpaul{
109545386Swpaul	caddr_t			ptr;
1096153770Syongari	int			i;
109745386Swpaul	struct ti_jpool_entry   *entry;
109845386Swpaul
1099153396Sscottl	/*
1100153396Sscottl	 * Grab a big chunk o' storage.  Since we are chopping this pool up
1101153396Sscottl	 * into ~9k chunks, there doesn't appear to be a need to use page
1102153396Sscottl	 * alignment.
1103153396Sscottl	 */
1104153288Sscottl	if (bus_dma_tag_create(sc->ti_parent_dmat,	/* parent */
1105153396Sscottl				1, 0,			/* algnmnt, boundary */
1106153396Sscottl				BUS_SPACE_MAXADDR,	/* lowaddr */
1107153288Sscottl				BUS_SPACE_MAXADDR,	/* highaddr */
1108153288Sscottl				NULL, NULL,		/* filter, filterarg */
1109153288Sscottl				TI_JMEM,		/* maxsize */
1110153288Sscottl				1,			/* nsegments */
1111153288Sscottl				TI_JMEM,		/* maxsegsize */
1112153288Sscottl				0,			/* flags */
1113153288Sscottl				NULL, NULL,		/* lockfunc, lockarg */
1114153288Sscottl				&sc->ti_jumbo_dmat) != 0) {
1115153396Sscottl		device_printf(sc->ti_dev, "Failed to allocate jumbo dmat\n");
1116153288Sscottl		return (ENOBUFS);
1117153288Sscottl	}
111845386Swpaul
1119153288Sscottl	if (bus_dmamem_alloc(sc->ti_jumbo_dmat,
1120153288Sscottl			     (void**)&sc->ti_cdata.ti_jumbo_buf,
1121153288Sscottl			     BUS_DMA_NOWAIT, &sc->ti_jumbo_dmamap) != 0) {
1122153396Sscottl		device_printf(sc->ti_dev, "Failed to allocate jumbo memory\n");
1123131654Sbms		return (ENOBUFS);
112445386Swpaul	}
112545386Swpaul
112645386Swpaul	SLIST_INIT(&sc->ti_jfree_listhead);
112745386Swpaul	SLIST_INIT(&sc->ti_jinuse_listhead);
112845386Swpaul
112945386Swpaul	/*
113045386Swpaul	 * Now divide it up into 9K pieces and save the addresses
113167405Sbmilekic	 * in an array.
113245386Swpaul	 */
113345386Swpaul	ptr = sc->ti_cdata.ti_jumbo_buf;
113445386Swpaul	for (i = 0; i < TI_JSLOTS; i++) {
113567405Sbmilekic		sc->ti_cdata.ti_jslots[i] = ptr;
113667405Sbmilekic		ptr += TI_JLEN;
1137131652Sbms		entry = malloc(sizeof(struct ti_jpool_entry),
113845386Swpaul			       M_DEVBUF, M_NOWAIT);
113945386Swpaul		if (entry == NULL) {
1140153396Sscottl			device_printf(sc->ti_dev, "no memory for jumbo "
1141150719Sjhb			    "buffer queue!\n");
1142131654Sbms			return (ENOBUFS);
114345386Swpaul		}
114445386Swpaul		entry->slot = i;
114545386Swpaul		SLIST_INSERT_HEAD(&sc->ti_jfree_listhead, entry, jpool_entries);
114645386Swpaul	}
114745386Swpaul
1148131654Sbms	return (0);
114945386Swpaul}
115045386Swpaul
115145386Swpaul/*
115245386Swpaul * Allocate a jumbo buffer.
115345386Swpaul */
115445386Swpaulstatic void *ti_jalloc(sc)
115545386Swpaul	struct ti_softc		*sc;
115645386Swpaul{
1157131655Sbms	struct ti_jpool_entry	*entry;
1158131652Sbms
115945386Swpaul	entry = SLIST_FIRST(&sc->ti_jfree_listhead);
1160131652Sbms
116145386Swpaul	if (entry == NULL) {
1162162321Sglebius		device_printf(sc->ti_dev, "no free jumbo buffers\n");
1163131654Sbms		return (NULL);
116445386Swpaul	}
116545386Swpaul
116645386Swpaul	SLIST_REMOVE_HEAD(&sc->ti_jfree_listhead, jpool_entries);
116745386Swpaul	SLIST_INSERT_HEAD(&sc->ti_jinuse_listhead, entry, jpool_entries);
1168131654Sbms	return (sc->ti_cdata.ti_jslots[entry->slot]);
116945386Swpaul}
117045386Swpaul
117145386Swpaul/*
117245386Swpaul * Release a jumbo buffer.
117345386Swpaul */
1174102336Salfredstatic void
1175102336Salfredti_jfree(buf, args)
117699058Salfred	void			*buf;
117764837Sdwmalone	void			*args;
117845386Swpaul{
117945386Swpaul	struct ti_softc		*sc;
1180131655Sbms	int			i;
1181131655Sbms	struct ti_jpool_entry	*entry;
118245386Swpaul
118345386Swpaul	/* Extract the softc struct pointer. */
118467405Sbmilekic	sc = (struct ti_softc *)args;
118545386Swpaul
118645386Swpaul	if (sc == NULL)
118767405Sbmilekic		panic("ti_jfree: didn't get softc pointer!");
118845386Swpaul
118945386Swpaul	/* calculate the slot this buffer belongs to */
119067405Sbmilekic	i = ((vm_offset_t)buf
119145386Swpaul	     - (vm_offset_t)sc->ti_cdata.ti_jumbo_buf) / TI_JLEN;
119245386Swpaul
119345386Swpaul	if ((i < 0) || (i >= TI_JSLOTS))
119445386Swpaul		panic("ti_jfree: asked to free buffer that we don't manage!");
119545386Swpaul
119664837Sdwmalone	entry = SLIST_FIRST(&sc->ti_jinuse_listhead);
119764837Sdwmalone	if (entry == NULL)
119864837Sdwmalone		panic("ti_jfree: buffer not in use!");
119964837Sdwmalone	entry->slot = i;
120064837Sdwmalone	SLIST_REMOVE_HEAD(&sc->ti_jinuse_listhead, jpool_entries);
120164837Sdwmalone	SLIST_INSERT_HEAD(&sc->ti_jfree_listhead, entry, jpool_entries);
120245386Swpaul}
120345386Swpaul
1204153396Sscottl#else
1205153396Sscottl
1206153396Sscottlstatic int
1207153396Sscottlti_alloc_jumbo_mem(sc)
1208153396Sscottl	struct ti_softc		*sc;
1209153396Sscottl{
1210153396Sscottl
1211153396Sscottl	/*
1212153396Sscottl	 * The VM system will take care of providing aligned pages.  Alignment
1213153396Sscottl	 * is set to 1 here so that busdma resources won't be wasted.
1214153396Sscottl	 */
1215153396Sscottl	if (bus_dma_tag_create(sc->ti_parent_dmat,	/* parent */
1216153396Sscottl				1, 0,			/* algnmnt, boundary */
1217153396Sscottl				BUS_SPACE_MAXADDR,	/* lowaddr */
1218153396Sscottl				BUS_SPACE_MAXADDR,	/* highaddr */
1219153396Sscottl				NULL, NULL,		/* filter, filterarg */
1220153396Sscottl				PAGE_SIZE * 4 /*XXX*/,	/* maxsize */
1221153396Sscottl				4,			/* nsegments */
1222153396Sscottl				PAGE_SIZE,		/* maxsegsize */
1223153396Sscottl				0,			/* flags */
1224153396Sscottl				NULL, NULL,		/* lockfunc, lockarg */
1225153396Sscottl				&sc->ti_jumbo_dmat) != 0) {
1226153396Sscottl		device_printf(sc->ti_dev, "Failed to allocate jumbo dmat\n");
1227153396Sscottl		return (ENOBUFS);
1228153396Sscottl	}
1229153396Sscottl
1230153396Sscottl	return (0);
1231153396Sscottl}
1232153396Sscottl
123398849Sken#endif /* TI_PRIVATE_JUMBOS */
123445386Swpaul
123545386Swpaul/*
123645386Swpaul * Intialize a standard receive ring descriptor.
123745386Swpaul */
1238102336Salfredstatic int
1239102336Salfredti_newbuf_std(sc, i, m)
124045386Swpaul	struct ti_softc		*sc;
124145386Swpaul	int			i;
124245386Swpaul	struct mbuf		*m;
124345386Swpaul{
1244153396Sscottl	bus_dmamap_t		map;
1245153396Sscottl	bus_dma_segment_t	segs;
124645386Swpaul	struct mbuf		*m_new = NULL;
124745386Swpaul	struct ti_rx_desc	*r;
1248153396Sscottl	int			nsegs;
124945386Swpaul
1250153396Sscottl	nsegs = 0;
125149036Swpaul	if (m == NULL) {
1252111119Simp		MGETHDR(m_new, M_DONTWAIT, MT_DATA);
125387846Sluigi		if (m_new == NULL)
1254131654Sbms			return (ENOBUFS);
125545386Swpaul
1256111119Simp		MCLGET(m_new, M_DONTWAIT);
125745386Swpaul		if (!(m_new->m_flags & M_EXT)) {
125845386Swpaul			m_freem(m_new);
1259131654Sbms			return (ENOBUFS);
126045386Swpaul		}
126149036Swpaul		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
126249036Swpaul	} else {
126349036Swpaul		m_new = m;
126449036Swpaul		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
126549036Swpaul		m_new->m_data = m_new->m_ext.ext_buf;
126645386Swpaul	}
126745386Swpaul
126848597Swpaul	m_adj(m_new, ETHER_ALIGN);
126945386Swpaul	sc->ti_cdata.ti_rx_std_chain[i] = m_new;
127045386Swpaul	r = &sc->ti_rdata->ti_rx_std_ring[i];
1271153396Sscottl	map = sc->ti_cdata.ti_rx_std_maps[i];
1272153396Sscottl	if (bus_dmamap_load_mbuf_sg(sc->ti_mbufrx_dmat, map, m_new, &segs,
1273153396Sscottl				    &nsegs, 0))
1274153396Sscottl		return (ENOBUFS);
1275153396Sscottl	if (nsegs != 1)
1276153396Sscottl		return (ENOBUFS);
1277153396Sscottl	ti_hostaddr64(&r->ti_addr, segs.ds_addr);
1278153396Sscottl	r->ti_len = segs.ds_len;
127945386Swpaul	r->ti_type = TI_BDTYPE_RECV_BD;
128045386Swpaul	r->ti_flags = 0;
1281147256Sbrooks	if (sc->ti_ifp->if_hwassist)
128258698Sjlemon		r->ti_flags |= TI_BDFLAG_TCP_UDP_CKSUM | TI_BDFLAG_IP_CKSUM;
128345386Swpaul	r->ti_idx = i;
128445386Swpaul
1285153396Sscottl	bus_dmamap_sync(sc->ti_mbufrx_dmat, map, BUS_DMASYNC_PREREAD);
1286131654Sbms	return (0);
128745386Swpaul}
128845386Swpaul
128945386Swpaul/*
129045386Swpaul * Intialize a mini receive ring descriptor. This only applies to
129145386Swpaul * the Tigon 2.
129245386Swpaul */
1293102336Salfredstatic int
1294102336Salfredti_newbuf_mini(sc, i, m)
129545386Swpaul	struct ti_softc		*sc;
129645386Swpaul	int			i;
129745386Swpaul	struct mbuf		*m;
129845386Swpaul{
1299153396Sscottl	bus_dma_segment_t	segs;
1300153396Sscottl	bus_dmamap_t		map;
130145386Swpaul	struct mbuf		*m_new = NULL;
130245386Swpaul	struct ti_rx_desc	*r;
1303153396Sscottl	int			nsegs;
130445386Swpaul
1305153396Sscottl	nsegs = 0;
130649036Swpaul	if (m == NULL) {
1307111119Simp		MGETHDR(m_new, M_DONTWAIT, MT_DATA);
130845386Swpaul		if (m_new == NULL) {
1309131654Sbms			return (ENOBUFS);
131045386Swpaul		}
131149036Swpaul		m_new->m_len = m_new->m_pkthdr.len = MHLEN;
131249036Swpaul	} else {
131349036Swpaul		m_new = m;
131449036Swpaul		m_new->m_data = m_new->m_pktdat;
131549036Swpaul		m_new->m_len = m_new->m_pkthdr.len = MHLEN;
131645386Swpaul	}
131749036Swpaul
131848597Swpaul	m_adj(m_new, ETHER_ALIGN);
131945386Swpaul	r = &sc->ti_rdata->ti_rx_mini_ring[i];
132045386Swpaul	sc->ti_cdata.ti_rx_mini_chain[i] = m_new;
1321153396Sscottl	map = sc->ti_cdata.ti_rx_mini_maps[i];
1322153396Sscottl	if (bus_dmamap_load_mbuf_sg(sc->ti_mbufrx_dmat, map, m_new, &segs,
1323153396Sscottl				    &nsegs, 0))
1324153396Sscottl		return (ENOBUFS);
1325153396Sscottl	if (nsegs != 1)
1326153396Sscottl		return (ENOBUFS);
1327153396Sscottl	ti_hostaddr64(&r->ti_addr, segs.ds_addr);
1328153396Sscottl	r->ti_len = segs.ds_len;
132945386Swpaul	r->ti_type = TI_BDTYPE_RECV_BD;
133045386Swpaul	r->ti_flags = TI_BDFLAG_MINI_RING;
1331147256Sbrooks	if (sc->ti_ifp->if_hwassist)
133258698Sjlemon		r->ti_flags |= TI_BDFLAG_TCP_UDP_CKSUM | TI_BDFLAG_IP_CKSUM;
133345386Swpaul	r->ti_idx = i;
133445386Swpaul
1335153396Sscottl	bus_dmamap_sync(sc->ti_mbufrx_dmat, map, BUS_DMASYNC_PREREAD);
1336131654Sbms	return (0);
133745386Swpaul}
133845386Swpaul
133998849Sken#ifdef TI_PRIVATE_JUMBOS
134098849Sken
134145386Swpaul/*
134245386Swpaul * Initialize a jumbo receive ring descriptor. This allocates
134345386Swpaul * a jumbo buffer from the pool managed internally by the driver.
134445386Swpaul */
1345102336Salfredstatic int
1346102336Salfredti_newbuf_jumbo(sc, i, m)
134745386Swpaul	struct ti_softc		*sc;
134845386Swpaul	int			i;
134945386Swpaul	struct mbuf		*m;
135045386Swpaul{
1351153396Sscottl	bus_dmamap_t		map;
135245386Swpaul	struct mbuf		*m_new = NULL;
135345386Swpaul	struct ti_rx_desc	*r;
1354153396Sscottl	int			nsegs;
1355153396Sscottl	bus_dma_segment_t	segs;
135645386Swpaul
135749036Swpaul	if (m == NULL) {
135845386Swpaul		caddr_t			*buf = NULL;
135945386Swpaul
136045386Swpaul		/* Allocate the mbuf. */
1361111119Simp		MGETHDR(m_new, M_DONTWAIT, MT_DATA);
136245386Swpaul		if (m_new == NULL) {
1363131654Sbms			return (ENOBUFS);
136445386Swpaul		}
136545386Swpaul
136645386Swpaul		/* Allocate the jumbo buffer */
136745386Swpaul		buf = ti_jalloc(sc);
136845386Swpaul		if (buf == NULL) {
136945386Swpaul			m_freem(m_new);
1370162321Sglebius			device_printf(sc->ti_dev, "jumbo allocation failed "
1371150719Sjhb			    "-- packet dropped!\n");
1372131654Sbms			return (ENOBUFS);
137345386Swpaul		}
137445386Swpaul
137545386Swpaul		/* Attach the buffer to the mbuf. */
137664837Sdwmalone		m_new->m_data = (void *) buf;
137764837Sdwmalone		m_new->m_len = m_new->m_pkthdr.len = TI_JUMBO_FRAMELEN;
137867405Sbmilekic		MEXTADD(m_new, buf, TI_JUMBO_FRAMELEN, ti_jfree,
137968621Sbmilekic		    (struct ti_softc *)sc, 0, EXT_NET_DRV);
138049036Swpaul	} else {
138149036Swpaul		m_new = m;
138249036Swpaul		m_new->m_data = m_new->m_ext.ext_buf;
138349036Swpaul		m_new->m_ext.ext_size = TI_JUMBO_FRAMELEN;
138445386Swpaul	}
138545386Swpaul
138649780Swpaul	m_adj(m_new, ETHER_ALIGN);
138745386Swpaul	/* Set up the descriptor. */
138845386Swpaul	r = &sc->ti_rdata->ti_rx_jumbo_ring[i];
138945386Swpaul	sc->ti_cdata.ti_rx_jumbo_chain[i] = m_new;
1390153396Sscottl	map = sc->ti_cdata.ti_rx_jumbo_maps[i];
1391153396Sscottl	if (bus_dmamap_load_mbuf_sg(sc->ti_jumbo_dmat, map, m_new, &segs,
1392153396Sscottl				    &nsegs, 0))
1393153396Sscottl		return (ENOBUFS);
1394153396Sscottl	if (nsegs != 1)
1395153396Sscottl		return (ENOBUFS);
1396153396Sscottl	ti_hostaddr64(&r->ti_addr, segs.ds_addr);
1397153396Sscottl	r->ti_len = segs.ds_len;
139845386Swpaul	r->ti_type = TI_BDTYPE_RECV_JUMBO_BD;
139945386Swpaul	r->ti_flags = TI_BDFLAG_JUMBO_RING;
1400147256Sbrooks	if (sc->ti_ifp->if_hwassist)
140158698Sjlemon		r->ti_flags |= TI_BDFLAG_TCP_UDP_CKSUM | TI_BDFLAG_IP_CKSUM;
140245386Swpaul	r->ti_idx = i;
140345386Swpaul
1404153396Sscottl	bus_dmamap_sync(sc->ti_jumbo_dmat, map, BUS_DMASYNC_PREREAD);
1405131654Sbms	return (0);
140645386Swpaul}
140745386Swpaul
140898849Sken#else
140998849Sken
141098849Sken#if (PAGE_SIZE == 4096)
141198849Sken#define NPAYLOAD 2
141298849Sken#else
141398849Sken#define NPAYLOAD 1
1414131652Sbms#endif
141598849Sken
141698849Sken#define TCP_HDR_LEN (52 + sizeof(struct ether_header))
141798849Sken#define UDP_HDR_LEN (28 + sizeof(struct ether_header))
141898849Sken#define NFS_HDR_LEN (UDP_HDR_LEN)
1419104401Salfredstatic int HDR_LEN =  TCP_HDR_LEN;
142098849Sken
142198849Sken
1422131655Sbms/*
1423131655Sbms * Initialize a jumbo receive ring descriptor. This allocates
1424131655Sbms * a jumbo buffer from the pool managed internally by the driver.
1425131655Sbms */
142698849Skenstatic int
142798849Skenti_newbuf_jumbo(sc, idx, m_old)
1428131655Sbms	struct ti_softc		*sc;
1429131655Sbms	int			idx;
1430131655Sbms	struct mbuf		*m_old;
143198849Sken{
1432153396Sscottl	bus_dmamap_t		map;
143398849Sken	struct mbuf		*cur, *m_new = NULL;
143498849Sken	struct mbuf		*m[3] = {NULL, NULL, NULL};
143598849Sken	struct ti_rx_desc_ext	*r;
143698849Sken	vm_page_t		frame;
1437138424Salc	static int		color;
143898849Sken				/* 1 extra buf to make nobufs easy*/
1439138424Salc	struct sf_buf		*sf[3] = {NULL, NULL, NULL};
144098849Sken	int			i;
1441153396Sscottl	bus_dma_segment_t	segs[4];
1442153396Sscottl	int			nsegs;
144398849Sken
144498849Sken	if (m_old != NULL) {
144598849Sken		m_new = m_old;
144698849Sken		cur = m_old->m_next;
144798849Sken		for (i = 0; i <= NPAYLOAD; i++){
144898849Sken			m[i] = cur;
144998849Sken			cur = cur->m_next;
145098849Sken		}
145198849Sken	} else {
145298849Sken		/* Allocate the mbufs. */
1453111119Simp		MGETHDR(m_new, M_DONTWAIT, MT_DATA);
145498849Sken		if (m_new == NULL) {
1455162321Sglebius			device_printf(sc->ti_dev, "mbuf allocation failed "
1456150719Sjhb			    "-- packet dropped!\n");
145798849Sken			goto nobufs;
145898849Sken		}
1459111119Simp		MGET(m[NPAYLOAD], M_DONTWAIT, MT_DATA);
146098849Sken		if (m[NPAYLOAD] == NULL) {
1461162321Sglebius			device_printf(sc->ti_dev, "cluster mbuf allocation "
1462162321Sglebius			    "failed -- packet dropped!\n");
146398849Sken			goto nobufs;
146498849Sken		}
1465111119Simp		MCLGET(m[NPAYLOAD], M_DONTWAIT);
146698849Sken		if ((m[NPAYLOAD]->m_flags & M_EXT) == 0) {
1467162321Sglebius			device_printf(sc->ti_dev, "mbuf allocation failed "
1468150719Sjhb			    "-- packet dropped!\n");
146998849Sken			goto nobufs;
147098849Sken		}
147198849Sken		m[NPAYLOAD]->m_len = MCLBYTES;
147298849Sken
147398849Sken		for (i = 0; i < NPAYLOAD; i++){
1474111119Simp			MGET(m[i], M_DONTWAIT, MT_DATA);
147598849Sken			if (m[i] == NULL) {
1476162321Sglebius				device_printf(sc->ti_dev, "mbuf allocation "
1477162321Sglebius				    "failed -- packet dropped!\n");
147898849Sken				goto nobufs;
147998849Sken			}
1480138424Salc			frame = vm_page_alloc(NULL, color++,
1481138424Salc			    VM_ALLOC_INTERRUPT | VM_ALLOC_NOOBJ |
1482138424Salc			    VM_ALLOC_WIRED);
1483138424Salc			if (frame == NULL) {
1484162321Sglebius				device_printf(sc->ti_dev, "buffer allocation "
1485150719Sjhb				    "failed -- packet dropped!\n");
148698849Sken				printf("      index %d page %d\n", idx, i);
1487131655Sbms				goto nobufs;
148898849Sken			}
1489138424Salc			sf[i] = sf_buf_alloc(frame, SFB_NOWAIT);
1490138424Salc			if (sf[i] == NULL) {
1491138424Salc				vm_page_lock_queues();
1492138424Salc				vm_page_unwire(frame, 0);
1493138424Salc				vm_page_free(frame);
1494138424Salc				vm_page_unlock_queues();
1495162321Sglebius				device_printf(sc->ti_dev, "buffer allocation "
1496150719Sjhb				    "failed -- packet dropped!\n");
1497138424Salc				printf("      index %d page %d\n", idx, i);
1498138424Salc				goto nobufs;
1499138424Salc			}
150098849Sken		}
150198849Sken		for (i = 0; i < NPAYLOAD; i++){
1502131655Sbms		/* Attach the buffer to the mbuf. */
1503138424Salc			m[i]->m_data = (void *)sf_buf_kva(sf[i]);
150498849Sken			m[i]->m_len = PAGE_SIZE;
1505138424Salc			MEXTADD(m[i], sf_buf_kva(sf[i]), PAGE_SIZE,
1506175872Sphk			    sf_buf_mext, (void*)sf_buf_kva(sf[i]), sf[i],
1507175872Sphk			    0, EXT_DISPOSABLE);
150898849Sken			m[i]->m_next = m[i+1];
150998849Sken		}
151098849Sken		/* link the buffers to the header */
151198849Sken		m_new->m_next = m[0];
151298849Sken		m_new->m_data += ETHER_ALIGN;
151398849Sken		if (sc->ti_hdrsplit)
151498849Sken			m_new->m_len = MHLEN - ETHER_ALIGN;
151598849Sken		else
1516131655Sbms			m_new->m_len = HDR_LEN;
151798849Sken		m_new->m_pkthdr.len = NPAYLOAD * PAGE_SIZE + m_new->m_len;
151898849Sken	}
151998849Sken
152098849Sken	/* Set up the descriptor. */
152198849Sken	r = &sc->ti_rdata->ti_rx_jumbo_ring[idx];
152298849Sken	sc->ti_cdata.ti_rx_jumbo_chain[idx] = m_new;
1523153396Sscottl	map = sc->ti_cdata.ti_rx_jumbo_maps[i];
1524153396Sscottl	if (bus_dmamap_load_mbuf_sg(sc->ti_jumbo_dmat, map, m_new, segs,
1525153396Sscottl				    &nsegs, 0))
1526153396Sscottl		return (ENOBUFS);
1527153396Sscottl	if ((nsegs < 1) || (nsegs > 4))
1528153396Sscottl		return (ENOBUFS);
1529153396Sscottl	ti_hostaddr64(&r->ti_addr0, segs[0].ds_addr);
153098849Sken	r->ti_len0 = m_new->m_len;
153198849Sken
1532153396Sscottl	ti_hostaddr64(&r->ti_addr1, segs[1].ds_addr);
153398849Sken	r->ti_len1 = PAGE_SIZE;
153498849Sken
1535153396Sscottl	ti_hostaddr64(&r->ti_addr2, segs[2].ds_addr);
153698849Sken	r->ti_len2 = m[1]->m_ext.ext_size; /* could be PAGE_SIZE or MCLBYTES */
153798849Sken
153898849Sken	if (PAGE_SIZE == 4096) {
1539153396Sscottl		ti_hostaddr64(&r->ti_addr3, segs[3].ds_addr);
154098849Sken		r->ti_len3 = MCLBYTES;
154198849Sken	} else {
154298849Sken		r->ti_len3 = 0;
154398849Sken	}
1544131655Sbms	r->ti_type = TI_BDTYPE_RECV_JUMBO_BD;
154598849Sken
1546131655Sbms	r->ti_flags = TI_BDFLAG_JUMBO_RING|TI_RCB_FLAG_USE_EXT_RX_BD;
154798849Sken
1548147256Sbrooks	if (sc->ti_ifp->if_hwassist)
154998849Sken		r->ti_flags |= TI_BDFLAG_TCP_UDP_CKSUM|TI_BDFLAG_IP_CKSUM;
155098849Sken
1551131655Sbms	r->ti_idx = idx;
155298849Sken
1553153396Sscottl	bus_dmamap_sync(sc->ti_jumbo_dmat, map, BUS_DMASYNC_PREREAD);
1554131655Sbms	return (0);
155598849Sken
1556131655Sbmsnobufs:
155798849Sken
155898849Sken	/*
155998849Sken	 * Warning! :
156098849Sken	 * This can only be called before the mbufs are strung together.
1561131652Sbms	 * If the mbufs are strung together, m_freem() will free the chain,
156298849Sken	 * so that the later mbufs will be freed multiple times.
156398849Sken	 */
1564131655Sbms	if (m_new)
1565131655Sbms		m_freem(m_new);
156698849Sken
1567131655Sbms	for (i = 0; i < 3; i++) {
1568131655Sbms		if (m[i])
1569131655Sbms			m_freem(m[i]);
1570138424Salc		if (sf[i])
1571138424Salc			sf_buf_mext((void *)sf_buf_kva(sf[i]), sf[i]);
1572131655Sbms	}
1573131655Sbms	return (ENOBUFS);
157498849Sken}
157598849Sken#endif
157698849Sken
157798849Sken
157898849Sken
157945386Swpaul/*
158045386Swpaul * The standard receive ring has 512 entries in it. At 2K per mbuf cluster,
158145386Swpaul * that's 1MB or memory, which is a lot. For now, we fill only the first
158245386Swpaul * 256 ring entries and hope that our CPU is fast enough to keep up with
158345386Swpaul * the NIC.
158445386Swpaul */
1585102336Salfredstatic int
1586102336Salfredti_init_rx_ring_std(sc)
158745386Swpaul	struct ti_softc		*sc;
158845386Swpaul{
1589153770Syongari	int			i;
159045386Swpaul	struct ti_cmd_desc	cmd;
159145386Swpaul
159245386Swpaul	for (i = 0; i < TI_SSLOTS; i++) {
159345386Swpaul		if (ti_newbuf_std(sc, i, NULL) == ENOBUFS)
1594131654Sbms			return (ENOBUFS);
159545386Swpaul	};
159645386Swpaul
159745386Swpaul	TI_UPDATE_STDPROD(sc, i - 1);
159848597Swpaul	sc->ti_std = i - 1;
159945386Swpaul
1600131654Sbms	return (0);
160145386Swpaul}
160245386Swpaul
1603102336Salfredstatic void
1604102336Salfredti_free_rx_ring_std(sc)
160545386Swpaul	struct ti_softc		*sc;
160645386Swpaul{
1607153770Syongari	bus_dmamap_t		map;
1608153770Syongari	int			i;
160945386Swpaul
161045386Swpaul	for (i = 0; i < TI_STD_RX_RING_CNT; i++) {
161145386Swpaul		if (sc->ti_cdata.ti_rx_std_chain[i] != NULL) {
1612153770Syongari			map = sc->ti_cdata.ti_rx_std_maps[i];
1613153770Syongari			bus_dmamap_sync(sc->ti_mbufrx_dmat, map,
1614153770Syongari			    BUS_DMASYNC_POSTREAD);
1615153770Syongari			bus_dmamap_unload(sc->ti_mbufrx_dmat, map);
161645386Swpaul			m_freem(sc->ti_cdata.ti_rx_std_chain[i]);
161745386Swpaul			sc->ti_cdata.ti_rx_std_chain[i] = NULL;
161845386Swpaul		}
161945386Swpaul		bzero((char *)&sc->ti_rdata->ti_rx_std_ring[i],
162045386Swpaul		    sizeof(struct ti_rx_desc));
162145386Swpaul	}
162245386Swpaul}
162345386Swpaul
1624102336Salfredstatic int
1625102336Salfredti_init_rx_ring_jumbo(sc)
162645386Swpaul	struct ti_softc		*sc;
162745386Swpaul{
1628153770Syongari	int			i;
162945386Swpaul	struct ti_cmd_desc	cmd;
163045386Swpaul
163163699Swpaul	for (i = 0; i < TI_JUMBO_RX_RING_CNT; i++) {
163245386Swpaul		if (ti_newbuf_jumbo(sc, i, NULL) == ENOBUFS)
1633131654Sbms			return (ENOBUFS);
163445386Swpaul	};
163545386Swpaul
163645386Swpaul	TI_UPDATE_JUMBOPROD(sc, i - 1);
163748597Swpaul	sc->ti_jumbo = i - 1;
163845386Swpaul
1639131654Sbms	return (0);
164045386Swpaul}
164145386Swpaul
1642102336Salfredstatic void
1643102336Salfredti_free_rx_ring_jumbo(sc)
164445386Swpaul	struct ti_softc		*sc;
164545386Swpaul{
1646153770Syongari	bus_dmamap_t		map;
1647153770Syongari	int			i;
164845386Swpaul
164945386Swpaul	for (i = 0; i < TI_JUMBO_RX_RING_CNT; i++) {
165045386Swpaul		if (sc->ti_cdata.ti_rx_jumbo_chain[i] != NULL) {
1651153770Syongari			map = sc->ti_cdata.ti_rx_jumbo_maps[i];
1652153770Syongari			bus_dmamap_sync(sc->ti_jumbo_dmat, map,
1653153770Syongari			    BUS_DMASYNC_POSTREAD);
1654153770Syongari			bus_dmamap_unload(sc->ti_jumbo_dmat, map);
165545386Swpaul			m_freem(sc->ti_cdata.ti_rx_jumbo_chain[i]);
165645386Swpaul			sc->ti_cdata.ti_rx_jumbo_chain[i] = NULL;
165745386Swpaul		}
165845386Swpaul		bzero((char *)&sc->ti_rdata->ti_rx_jumbo_ring[i],
165945386Swpaul		    sizeof(struct ti_rx_desc));
166045386Swpaul	}
166145386Swpaul}
166245386Swpaul
1663102336Salfredstatic int
1664102336Salfredti_init_rx_ring_mini(sc)
166545386Swpaul	struct ti_softc		*sc;
166645386Swpaul{
1667153770Syongari	int			i;
166845386Swpaul
166945386Swpaul	for (i = 0; i < TI_MSLOTS; i++) {
167045386Swpaul		if (ti_newbuf_mini(sc, i, NULL) == ENOBUFS)
1671131654Sbms			return (ENOBUFS);
167245386Swpaul	};
167345386Swpaul
167445386Swpaul	TI_UPDATE_MINIPROD(sc, i - 1);
167548597Swpaul	sc->ti_mini = i - 1;
167645386Swpaul
1677131654Sbms	return (0);
167845386Swpaul}
167945386Swpaul
1680102336Salfredstatic void
1681102336Salfredti_free_rx_ring_mini(sc)
168245386Swpaul	struct ti_softc		*sc;
168345386Swpaul{
1684153770Syongari	bus_dmamap_t		map;
1685153770Syongari	int			i;
168645386Swpaul
168745386Swpaul	for (i = 0; i < TI_MINI_RX_RING_CNT; i++) {
168845386Swpaul		if (sc->ti_cdata.ti_rx_mini_chain[i] != NULL) {
1689153770Syongari			map = sc->ti_cdata.ti_rx_mini_maps[i];
1690153770Syongari			bus_dmamap_sync(sc->ti_mbufrx_dmat, map,
1691153770Syongari			    BUS_DMASYNC_POSTREAD);
1692153770Syongari			bus_dmamap_unload(sc->ti_mbufrx_dmat, map);
169345386Swpaul			m_freem(sc->ti_cdata.ti_rx_mini_chain[i]);
169445386Swpaul			sc->ti_cdata.ti_rx_mini_chain[i] = NULL;
169545386Swpaul		}
169645386Swpaul		bzero((char *)&sc->ti_rdata->ti_rx_mini_ring[i],
169745386Swpaul		    sizeof(struct ti_rx_desc));
169845386Swpaul	}
169945386Swpaul}
170045386Swpaul
1701102336Salfredstatic void
1702102336Salfredti_free_tx_ring(sc)
170345386Swpaul	struct ti_softc		*sc;
170445386Swpaul{
1705153982Syongari	struct ti_txdesc	*txd;
1706153770Syongari	int			i;
170745386Swpaul
170845386Swpaul	if (sc->ti_rdata->ti_tx_ring == NULL)
170945386Swpaul		return;
171045386Swpaul
171145386Swpaul	for (i = 0; i < TI_TX_RING_CNT; i++) {
1712153982Syongari		txd = &sc->ti_cdata.ti_txdesc[i];
1713153982Syongari		if (txd->tx_m != NULL) {
1714153982Syongari			bus_dmamap_sync(sc->ti_mbuftx_dmat, txd->tx_dmamap,
1715153770Syongari			    BUS_DMASYNC_POSTWRITE);
1716153982Syongari			bus_dmamap_unload(sc->ti_mbuftx_dmat, txd->tx_dmamap);
1717153982Syongari			m_freem(txd->tx_m);
1718153982Syongari			txd->tx_m = NULL;
171945386Swpaul		}
172045386Swpaul		bzero((char *)&sc->ti_rdata->ti_tx_ring[i],
172145386Swpaul		    sizeof(struct ti_tx_desc));
172245386Swpaul	}
172345386Swpaul}
172445386Swpaul
1725102336Salfredstatic int
1726102336Salfredti_init_tx_ring(sc)
172745386Swpaul	struct ti_softc		*sc;
172845386Swpaul{
1729153982Syongari	struct ti_txdesc	*txd;
1730153982Syongari	int			i;
1731153982Syongari
1732153982Syongari	STAILQ_INIT(&sc->ti_cdata.ti_txfreeq);
1733153982Syongari	STAILQ_INIT(&sc->ti_cdata.ti_txbusyq);
1734153982Syongari	for (i = 0; i < TI_TX_RING_CNT; i++) {
1735153982Syongari		txd = &sc->ti_cdata.ti_txdesc[i];
1736153982Syongari		STAILQ_INSERT_TAIL(&sc->ti_cdata.ti_txfreeq, txd, tx_q);
1737153982Syongari	}
173848011Swpaul	sc->ti_txcnt = 0;
173945386Swpaul	sc->ti_tx_saved_considx = 0;
1740153778Sscottl	sc->ti_tx_saved_prodidx = 0;
174145386Swpaul	CSR_WRITE_4(sc, TI_MB_SENDPROD_IDX, 0);
1742131654Sbms	return (0);
174345386Swpaul}
174445386Swpaul
174545386Swpaul/*
174645386Swpaul * The Tigon 2 firmware has a new way to add/delete multicast addresses,
174745386Swpaul * but we have to support the old way too so that Tigon 1 cards will
174845386Swpaul * work.
174945386Swpaul */
1750105219Sphkstatic void
1751102336Salfredti_add_mcast(sc, addr)
175245386Swpaul	struct ti_softc		*sc;
175345386Swpaul	struct ether_addr	*addr;
175445386Swpaul{
175545386Swpaul	struct ti_cmd_desc	cmd;
175645386Swpaul	u_int16_t		*m;
175745386Swpaul	u_int32_t		ext[2] = {0, 0};
175845386Swpaul
175945386Swpaul	m = (u_int16_t *)&addr->octet[0];
176045386Swpaul
1761131654Sbms	switch (sc->ti_hwrev) {
176245386Swpaul	case TI_HWREV_TIGON:
176345386Swpaul		CSR_WRITE_4(sc, TI_GCR_MAR0, htons(m[0]));
176445386Swpaul		CSR_WRITE_4(sc, TI_GCR_MAR1, (htons(m[1]) << 16) | htons(m[2]));
176545386Swpaul		TI_DO_CMD(TI_CMD_ADD_MCAST_ADDR, 0, 0);
176645386Swpaul		break;
176745386Swpaul	case TI_HWREV_TIGON_II:
176845386Swpaul		ext[0] = htons(m[0]);
176945386Swpaul		ext[1] = (htons(m[1]) << 16) | htons(m[2]);
177045386Swpaul		TI_DO_CMD_EXT(TI_CMD_EXT_ADD_MCAST, 0, 0, (caddr_t)&ext, 2);
177145386Swpaul		break;
177245386Swpaul	default:
1773162321Sglebius		device_printf(sc->ti_dev, "unknown hwrev\n");
177445386Swpaul		break;
177545386Swpaul	}
177645386Swpaul}
177745386Swpaul
1778105219Sphkstatic void
1779102336Salfredti_del_mcast(sc, addr)
178045386Swpaul	struct ti_softc		*sc;
178145386Swpaul	struct ether_addr	*addr;
178245386Swpaul{
178345386Swpaul	struct ti_cmd_desc	cmd;
178445386Swpaul	u_int16_t		*m;
178545386Swpaul	u_int32_t		ext[2] = {0, 0};
178645386Swpaul
178745386Swpaul	m = (u_int16_t *)&addr->octet[0];
178845386Swpaul
1789131654Sbms	switch (sc->ti_hwrev) {
179045386Swpaul	case TI_HWREV_TIGON:
179145386Swpaul		CSR_WRITE_4(sc, TI_GCR_MAR0, htons(m[0]));
179245386Swpaul		CSR_WRITE_4(sc, TI_GCR_MAR1, (htons(m[1]) << 16) | htons(m[2]));
179345386Swpaul		TI_DO_CMD(TI_CMD_DEL_MCAST_ADDR, 0, 0);
179445386Swpaul		break;
179545386Swpaul	case TI_HWREV_TIGON_II:
179645386Swpaul		ext[0] = htons(m[0]);
179745386Swpaul		ext[1] = (htons(m[1]) << 16) | htons(m[2]);
179845386Swpaul		TI_DO_CMD_EXT(TI_CMD_EXT_DEL_MCAST, 0, 0, (caddr_t)&ext, 2);
179945386Swpaul		break;
180045386Swpaul	default:
1801162321Sglebius		device_printf(sc->ti_dev, "unknown hwrev\n");
180245386Swpaul		break;
180345386Swpaul	}
180445386Swpaul}
180545386Swpaul
180645386Swpaul/*
180745386Swpaul * Configure the Tigon's multicast address filter.
180845386Swpaul *
180945386Swpaul * The actual multicast table management is a bit of a pain, thanks to
181045386Swpaul * slight brain damage on the part of both Alteon and us. With our
181145386Swpaul * multicast code, we are only alerted when the multicast address table
181245386Swpaul * changes and at that point we only have the current list of addresses:
181345386Swpaul * we only know the current state, not the previous state, so we don't
181445386Swpaul * actually know what addresses were removed or added. The firmware has
181545386Swpaul * state, but we can't get our grubby mits on it, and there is no 'delete
181645386Swpaul * all multicast addresses' command. Hence, we have to maintain our own
181745386Swpaul * state so we know what addresses have been programmed into the NIC at
181845386Swpaul * any given time.
181945386Swpaul */
1820102336Salfredstatic void
1821102336Salfredti_setmulti(sc)
182245386Swpaul	struct ti_softc		*sc;
182345386Swpaul{
182445386Swpaul	struct ifnet		*ifp;
182545386Swpaul	struct ifmultiaddr	*ifma;
182645386Swpaul	struct ti_cmd_desc	cmd;
182745386Swpaul	struct ti_mc_entry	*mc;
182845386Swpaul	u_int32_t		intrs;
182945386Swpaul
1830153770Syongari	TI_LOCK_ASSERT(sc);
1831153770Syongari
1832147256Sbrooks	ifp = sc->ti_ifp;
183345386Swpaul
183445386Swpaul	if (ifp->if_flags & IFF_ALLMULTI) {
183545386Swpaul		TI_DO_CMD(TI_CMD_SET_ALLMULTI, TI_CMD_CODE_ALLMULTI_ENB, 0);
183645386Swpaul		return;
183745386Swpaul	} else {
183845386Swpaul		TI_DO_CMD(TI_CMD_SET_ALLMULTI, TI_CMD_CODE_ALLMULTI_DIS, 0);
183945386Swpaul	}
184045386Swpaul
184145386Swpaul	/* Disable interrupts. */
184245386Swpaul	intrs = CSR_READ_4(sc, TI_MB_HOSTINTR);
184345386Swpaul	CSR_WRITE_4(sc, TI_MB_HOSTINTR, 1);
184445386Swpaul
184545386Swpaul	/* First, zot all the existing filters. */
184671999Sphk	while (SLIST_FIRST(&sc->ti_mc_listhead) != NULL) {
184771999Sphk		mc = SLIST_FIRST(&sc->ti_mc_listhead);
184845386Swpaul		ti_del_mcast(sc, &mc->mc_addr);
184945386Swpaul		SLIST_REMOVE_HEAD(&sc->ti_mc_listhead, mc_entries);
185045386Swpaul		free(mc, M_DEVBUF);
185145386Swpaul	}
185245386Swpaul
185345386Swpaul	/* Now program new ones. */
1854195049Srwatson	if_maddr_rlock(ifp);
185572084Sphk	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
185645386Swpaul		if (ifma->ifma_addr->sa_family != AF_LINK)
185745386Swpaul			continue;
185845386Swpaul		mc = malloc(sizeof(struct ti_mc_entry), M_DEVBUF, M_NOWAIT);
1859144165Ssam		if (mc == NULL) {
1860162321Sglebius			device_printf(sc->ti_dev,
1861162321Sglebius			    "no memory for mcast filter entry\n");
1862144165Ssam			continue;
1863144165Ssam		}
186445386Swpaul		bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
186545386Swpaul		    (char *)&mc->mc_addr, ETHER_ADDR_LEN);
186645386Swpaul		SLIST_INSERT_HEAD(&sc->ti_mc_listhead, mc, mc_entries);
186745386Swpaul		ti_add_mcast(sc, &mc->mc_addr);
186845386Swpaul	}
1869195049Srwatson	if_maddr_runlock(ifp);
187045386Swpaul
187145386Swpaul	/* Re-enable interrupts. */
187245386Swpaul	CSR_WRITE_4(sc, TI_MB_HOSTINTR, intrs);
187345386Swpaul}
187445386Swpaul
187545386Swpaul/*
187645386Swpaul * Check to see if the BIOS has configured us for a 64 bit slot when
187745386Swpaul * we aren't actually in one. If we detect this condition, we can work
187845386Swpaul * around it on the Tigon 2 by setting a bit in the PCI state register,
187945386Swpaul * but for the Tigon 1 we must give up and abort the interface attach.
188045386Swpaul */
188145386Swpaulstatic int ti_64bitslot_war(sc)
188245386Swpaul	struct ti_softc		*sc;
188345386Swpaul{
188445386Swpaul	if (!(CSR_READ_4(sc, TI_PCI_STATE) & TI_PCISTATE_32BIT_BUS)) {
188545386Swpaul		CSR_WRITE_4(sc, 0x600, 0);
188645386Swpaul		CSR_WRITE_4(sc, 0x604, 0);
188745386Swpaul		CSR_WRITE_4(sc, 0x600, 0x5555AAAA);
188845386Swpaul		if (CSR_READ_4(sc, 0x604) == 0x5555AAAA) {
188945386Swpaul			if (sc->ti_hwrev == TI_HWREV_TIGON)
1890131654Sbms				return (EINVAL);
189145386Swpaul			else {
189245386Swpaul				TI_SETBIT(sc, TI_PCI_STATE,
189345386Swpaul				    TI_PCISTATE_32BIT_BUS);
1894131654Sbms				return (0);
189545386Swpaul			}
189645386Swpaul		}
189745386Swpaul	}
189845386Swpaul
1899131654Sbms	return (0);
190045386Swpaul}
190145386Swpaul
190245386Swpaul/*
190345386Swpaul * Do endian, PCI and DMA initialization. Also check the on-board ROM
190445386Swpaul * self-test results.
190545386Swpaul */
1906102336Salfredstatic int
1907102336Salfredti_chipinit(sc)
190845386Swpaul	struct ti_softc		*sc;
190945386Swpaul{
191045386Swpaul	u_int32_t		cacheline;
191145386Swpaul	u_int32_t		pci_writemax = 0;
191298849Sken	u_int32_t		hdrsplit;
191345386Swpaul
191445386Swpaul	/* Initialize link to down state. */
191545386Swpaul	sc->ti_linkstat = TI_EV_CODE_LINK_DOWN;
191645386Swpaul
1917147256Sbrooks	if (sc->ti_ifp->if_capenable & IFCAP_HWCSUM)
1918147256Sbrooks		sc->ti_ifp->if_hwassist = TI_CSUM_FEATURES;
191983630Sjlemon	else
1920147256Sbrooks		sc->ti_ifp->if_hwassist = 0;
192158698Sjlemon
192245386Swpaul	/* Set endianness before we access any non-PCI registers. */
1923153770Syongari#if 0 && BYTE_ORDER == BIG_ENDIAN
192445386Swpaul	CSR_WRITE_4(sc, TI_MISC_HOST_CTL,
192545386Swpaul	    TI_MHC_BIGENDIAN_INIT | (TI_MHC_BIGENDIAN_INIT << 24));
192645386Swpaul#else
192745386Swpaul	CSR_WRITE_4(sc, TI_MISC_HOST_CTL,
192845386Swpaul	    TI_MHC_LITTLEENDIAN_INIT | (TI_MHC_LITTLEENDIAN_INIT << 24));
192945386Swpaul#endif
193045386Swpaul
193145386Swpaul	/* Check the ROM failed bit to see if self-tests passed. */
193245386Swpaul	if (CSR_READ_4(sc, TI_CPU_STATE) & TI_CPUSTATE_ROMFAIL) {
1933162321Sglebius		device_printf(sc->ti_dev, "board self-diagnostics failed!\n");
1934131654Sbms		return (ENODEV);
193545386Swpaul	}
193645386Swpaul
193745386Swpaul	/* Halt the CPU. */
193845386Swpaul	TI_SETBIT(sc, TI_CPU_STATE, TI_CPUSTATE_HALT);
193945386Swpaul
194045386Swpaul	/* Figure out the hardware revision. */
1941131654Sbms	switch (CSR_READ_4(sc, TI_MISC_HOST_CTL) & TI_MHC_CHIP_REV_MASK) {
194245386Swpaul	case TI_REV_TIGON_I:
194345386Swpaul		sc->ti_hwrev = TI_HWREV_TIGON;
194445386Swpaul		break;
194545386Swpaul	case TI_REV_TIGON_II:
194645386Swpaul		sc->ti_hwrev = TI_HWREV_TIGON_II;
194745386Swpaul		break;
194845386Swpaul	default:
1949162321Sglebius		device_printf(sc->ti_dev, "unsupported chip revision\n");
1950131654Sbms		return (ENODEV);
195145386Swpaul	}
195245386Swpaul
195345386Swpaul	/* Do special setup for Tigon 2. */
195445386Swpaul	if (sc->ti_hwrev == TI_HWREV_TIGON_II) {
195545386Swpaul		TI_SETBIT(sc, TI_CPU_CTL_B, TI_CPUSTATE_HALT);
195676033Swpaul		TI_SETBIT(sc, TI_MISC_LOCAL_CTL, TI_MLC_SRAM_BANK_512K);
195745386Swpaul		TI_SETBIT(sc, TI_MISC_CONF, TI_MCR_SRAM_SYNCHRONOUS);
195845386Swpaul	}
195945386Swpaul
196098849Sken	/*
196198849Sken	 * We don't have firmware source for the Tigon 1, so Tigon 1 boards
196298849Sken	 * can't do header splitting.
196398849Sken	 */
196498849Sken#ifdef TI_JUMBO_HDRSPLIT
196598849Sken	if (sc->ti_hwrev != TI_HWREV_TIGON)
196698849Sken		sc->ti_hdrsplit = 1;
196798849Sken	else
1968162321Sglebius		device_printf(sc->ti_dev,
1969150719Sjhb		    "can't do header splitting on a Tigon I board\n");
197098849Sken#endif /* TI_JUMBO_HDRSPLIT */
197198849Sken
197245386Swpaul	/* Set up the PCI state register. */
197345386Swpaul	CSR_WRITE_4(sc, TI_PCI_STATE, TI_PCI_READ_CMD|TI_PCI_WRITE_CMD);
197445386Swpaul	if (sc->ti_hwrev == TI_HWREV_TIGON_II) {
197545386Swpaul		TI_SETBIT(sc, TI_PCI_STATE, TI_PCISTATE_USE_MEM_RD_MULT);
197645386Swpaul	}
197745386Swpaul
197845386Swpaul	/* Clear the read/write max DMA parameters. */
197945386Swpaul	TI_CLRBIT(sc, TI_PCI_STATE, (TI_PCISTATE_WRITE_MAXDMA|
198045386Swpaul	    TI_PCISTATE_READ_MAXDMA));
198145386Swpaul
198245386Swpaul	/* Get cache line size. */
198345386Swpaul	cacheline = CSR_READ_4(sc, TI_PCI_BIST) & 0xFF;
198445386Swpaul
198545386Swpaul	/*
198645386Swpaul	 * If the system has set enabled the PCI memory write
198745386Swpaul	 * and invalidate command in the command register, set
198845386Swpaul	 * the write max parameter accordingly. This is necessary
198945386Swpaul	 * to use MWI with the Tigon 2.
199045386Swpaul	 */
199145386Swpaul	if (CSR_READ_4(sc, TI_PCI_CMDSTAT) & PCIM_CMD_MWIEN) {
1992131654Sbms		switch (cacheline) {
199345386Swpaul		case 1:
199445386Swpaul		case 4:
199545386Swpaul		case 8:
199645386Swpaul		case 16:
199745386Swpaul		case 32:
199845386Swpaul		case 64:
199945386Swpaul			break;
200045386Swpaul		default:
200145386Swpaul		/* Disable PCI memory write and invalidate. */
200245386Swpaul			if (bootverbose)
2003162321Sglebius				device_printf(sc->ti_dev, "cache line size %d"
2004162321Sglebius				    " not supported; disabling PCI MWI\n",
2005150719Sjhb				    cacheline);
200645386Swpaul			CSR_WRITE_4(sc, TI_PCI_CMDSTAT, CSR_READ_4(sc,
200745386Swpaul			    TI_PCI_CMDSTAT) & ~PCIM_CMD_MWIEN);
200845386Swpaul			break;
200945386Swpaul		}
201045386Swpaul	}
201145386Swpaul
201245386Swpaul	TI_SETBIT(sc, TI_PCI_STATE, pci_writemax);
201345386Swpaul
201445386Swpaul	/* This sets the min dma param all the way up (0xff). */
201545386Swpaul	TI_SETBIT(sc, TI_PCI_STATE, TI_PCISTATE_MINDMA);
201645386Swpaul
201798849Sken	if (sc->ti_hdrsplit)
201898849Sken		hdrsplit = TI_OPMODE_JUMBO_HDRSPLIT;
201998849Sken	else
202098849Sken		hdrsplit = 0;
202198849Sken
202245386Swpaul	/* Configure DMA variables. */
202345386Swpaul#if BYTE_ORDER == BIG_ENDIAN
202445386Swpaul	CSR_WRITE_4(sc, TI_GCR_OPMODE, TI_OPMODE_BYTESWAP_BD |
202545386Swpaul	    TI_OPMODE_BYTESWAP_DATA | TI_OPMODE_WORDSWAP_BD |
202645386Swpaul	    TI_OPMODE_WARN_ENB | TI_OPMODE_FATAL_ENB |
202798849Sken	    TI_OPMODE_DONT_FRAG_JUMBO | hdrsplit);
202898849Sken#else /* BYTE_ORDER */
202945386Swpaul	CSR_WRITE_4(sc, TI_GCR_OPMODE, TI_OPMODE_BYTESWAP_DATA|
203045386Swpaul	    TI_OPMODE_WORDSWAP_BD|TI_OPMODE_DONT_FRAG_JUMBO|
203198849Sken	    TI_OPMODE_WARN_ENB|TI_OPMODE_FATAL_ENB | hdrsplit);
203298849Sken#endif /* BYTE_ORDER */
203345386Swpaul
203445386Swpaul	/*
203545386Swpaul	 * Only allow 1 DMA channel to be active at a time.
203645386Swpaul	 * I don't think this is a good idea, but without it
203745386Swpaul	 * the firmware racks up lots of nicDmaReadRingFull
203858698Sjlemon	 * errors.  This is not compatible with hardware checksums.
203945386Swpaul	 */
2040147256Sbrooks	if (sc->ti_ifp->if_hwassist == 0)
204158698Sjlemon		TI_SETBIT(sc, TI_GCR_OPMODE, TI_OPMODE_1_DMA_ACTIVE);
204245386Swpaul
204345386Swpaul	/* Recommended settings from Tigon manual. */
204445386Swpaul	CSR_WRITE_4(sc, TI_GCR_DMA_WRITECFG, TI_DMA_STATE_THRESH_8W);
204545386Swpaul	CSR_WRITE_4(sc, TI_GCR_DMA_READCFG, TI_DMA_STATE_THRESH_8W);
204645386Swpaul
204745386Swpaul	if (ti_64bitslot_war(sc)) {
2048162321Sglebius		device_printf(sc->ti_dev, "bios thinks we're in a 64 bit slot, "
2049150719Sjhb		    "but we aren't");
2050131654Sbms		return (EINVAL);
205145386Swpaul	}
205245386Swpaul
2053131654Sbms	return (0);
205445386Swpaul}
205545386Swpaul
205645386Swpaul/*
205745386Swpaul * Initialize the general information block and firmware, and
205845386Swpaul * start the CPU(s) running.
205945386Swpaul */
2060102336Salfredstatic int
2061102336Salfredti_gibinit(sc)
206245386Swpaul	struct ti_softc		*sc;
206345386Swpaul{
206445386Swpaul	struct ti_rcb		*rcb;
206545386Swpaul	int			i;
206645386Swpaul	struct ifnet		*ifp;
2067143903Sscottl	uint32_t		rdphys;
206845386Swpaul
2069153770Syongari	TI_LOCK_ASSERT(sc);
2070153770Syongari
2071147256Sbrooks	ifp = sc->ti_ifp;
2072143903Sscottl	rdphys = sc->ti_rdata_phys;
207345386Swpaul
207445386Swpaul	/* Disable interrupts for now. */
207545386Swpaul	CSR_WRITE_4(sc, TI_MB_HOSTINTR, 1);
207645386Swpaul
2077143903Sscottl	/*
2078143903Sscottl	 * Tell the chip where to find the general information block.
2079143903Sscottl	 * While this struct could go into >4GB memory, we allocate it in a
2080143903Sscottl	 * single slab with the other descriptors, and those don't seem to
2081143903Sscottl	 * support being located in a 64-bit region.
2082143903Sscottl	 */
208345386Swpaul	CSR_WRITE_4(sc, TI_GCR_GENINFO_HI, 0);
2084143903Sscottl	CSR_WRITE_4(sc, TI_GCR_GENINFO_LO, rdphys + TI_RD_OFF(ti_info));
208545386Swpaul
208645386Swpaul	/* Load the firmware into SRAM. */
208745386Swpaul	ti_loadfw(sc);
208845386Swpaul
208945386Swpaul	/* Set up the contents of the general info and ring control blocks. */
209045386Swpaul
209145386Swpaul	/* Set up the event ring and producer pointer. */
209245386Swpaul	rcb = &sc->ti_rdata->ti_info.ti_ev_rcb;
209345386Swpaul
2094143903Sscottl	TI_HOSTADDR(rcb->ti_hostaddr) = rdphys + TI_RD_OFF(ti_event_ring);
209545386Swpaul	rcb->ti_flags = 0;
209645386Swpaul	TI_HOSTADDR(sc->ti_rdata->ti_info.ti_ev_prodidx_ptr) =
2097143903Sscottl	    rdphys + TI_RD_OFF(ti_ev_prodidx_r);
209845386Swpaul	sc->ti_ev_prodidx.ti_idx = 0;
209945386Swpaul	CSR_WRITE_4(sc, TI_GCR_EVENTCONS_IDX, 0);
210045386Swpaul	sc->ti_ev_saved_considx = 0;
210145386Swpaul
210245386Swpaul	/* Set up the command ring and producer mailbox. */
210345386Swpaul	rcb = &sc->ti_rdata->ti_info.ti_cmd_rcb;
210445386Swpaul
210545386Swpaul	TI_HOSTADDR(rcb->ti_hostaddr) = TI_GCR_NIC_ADDR(TI_GCR_CMDRING);
210645386Swpaul	rcb->ti_flags = 0;
210745386Swpaul	rcb->ti_max_len = 0;
210845386Swpaul	for (i = 0; i < TI_CMD_RING_CNT; i++) {
210945386Swpaul		CSR_WRITE_4(sc, TI_GCR_CMDRING + (i * 4), 0);
211045386Swpaul	}
211145386Swpaul	CSR_WRITE_4(sc, TI_GCR_CMDCONS_IDX, 0);
211245386Swpaul	CSR_WRITE_4(sc, TI_MB_CMDPROD_IDX, 0);
211345386Swpaul	sc->ti_cmd_saved_prodidx = 0;
211445386Swpaul
211545386Swpaul	/*
211645386Swpaul	 * Assign the address of the stats refresh buffer.
211745386Swpaul	 * We re-use the current stats buffer for this to
211845386Swpaul	 * conserve memory.
211945386Swpaul	 */
212045386Swpaul	TI_HOSTADDR(sc->ti_rdata->ti_info.ti_refresh_stats_ptr) =
2121143903Sscottl	    rdphys + TI_RD_OFF(ti_info.ti_stats);
212245386Swpaul
212345386Swpaul	/* Set up the standard receive ring. */
212445386Swpaul	rcb = &sc->ti_rdata->ti_info.ti_std_rx_rcb;
2125143903Sscottl	TI_HOSTADDR(rcb->ti_hostaddr) = rdphys + TI_RD_OFF(ti_rx_std_ring);
212645386Swpaul	rcb->ti_max_len = TI_FRAMELEN;
212745386Swpaul	rcb->ti_flags = 0;
2128147256Sbrooks	if (sc->ti_ifp->if_hwassist)
212958698Sjlemon		rcb->ti_flags |= TI_RCB_FLAG_TCP_UDP_CKSUM |
213058698Sjlemon		     TI_RCB_FLAG_IP_CKSUM | TI_RCB_FLAG_NO_PHDR_CKSUM;
213145386Swpaul	rcb->ti_flags |= TI_RCB_FLAG_VLAN_ASSIST;
213245386Swpaul
213345386Swpaul	/* Set up the jumbo receive ring. */
213445386Swpaul	rcb = &sc->ti_rdata->ti_info.ti_jumbo_rx_rcb;
2135143903Sscottl	TI_HOSTADDR(rcb->ti_hostaddr) = rdphys + TI_RD_OFF(ti_rx_jumbo_ring);
213698849Sken
213798849Sken#ifdef TI_PRIVATE_JUMBOS
213849036Swpaul	rcb->ti_max_len = TI_JUMBO_FRAMELEN;
213945386Swpaul	rcb->ti_flags = 0;
214098849Sken#else
214198849Sken	rcb->ti_max_len = PAGE_SIZE;
214298849Sken	rcb->ti_flags = TI_RCB_FLAG_USE_EXT_RX_BD;
214398849Sken#endif
2144147256Sbrooks	if (sc->ti_ifp->if_hwassist)
214558698Sjlemon		rcb->ti_flags |= TI_RCB_FLAG_TCP_UDP_CKSUM |
214658698Sjlemon		     TI_RCB_FLAG_IP_CKSUM | TI_RCB_FLAG_NO_PHDR_CKSUM;
214745386Swpaul	rcb->ti_flags |= TI_RCB_FLAG_VLAN_ASSIST;
214845386Swpaul
214945386Swpaul	/*
215045386Swpaul	 * Set up the mini ring. Only activated on the
215145386Swpaul	 * Tigon 2 but the slot in the config block is
215245386Swpaul	 * still there on the Tigon 1.
215345386Swpaul	 */
215445386Swpaul	rcb = &sc->ti_rdata->ti_info.ti_mini_rx_rcb;
2155143903Sscottl	TI_HOSTADDR(rcb->ti_hostaddr) = rdphys + TI_RD_OFF(ti_rx_mini_ring);
215651352Swpaul	rcb->ti_max_len = MHLEN - ETHER_ALIGN;
215745386Swpaul	if (sc->ti_hwrev == TI_HWREV_TIGON)
215845386Swpaul		rcb->ti_flags = TI_RCB_FLAG_RING_DISABLED;
215945386Swpaul	else
216045386Swpaul		rcb->ti_flags = 0;
2161147256Sbrooks	if (sc->ti_ifp->if_hwassist)
216258698Sjlemon		rcb->ti_flags |= TI_RCB_FLAG_TCP_UDP_CKSUM |
216358698Sjlemon		     TI_RCB_FLAG_IP_CKSUM | TI_RCB_FLAG_NO_PHDR_CKSUM;
216445386Swpaul	rcb->ti_flags |= TI_RCB_FLAG_VLAN_ASSIST;
216545386Swpaul
216645386Swpaul	/*
216745386Swpaul	 * Set up the receive return ring.
216845386Swpaul	 */
216945386Swpaul	rcb = &sc->ti_rdata->ti_info.ti_return_rcb;
2170143903Sscottl	TI_HOSTADDR(rcb->ti_hostaddr) = rdphys + TI_RD_OFF(ti_rx_return_ring);
217145386Swpaul	rcb->ti_flags = 0;
217245386Swpaul	rcb->ti_max_len = TI_RETURN_RING_CNT;
217345386Swpaul	TI_HOSTADDR(sc->ti_rdata->ti_info.ti_return_prodidx_ptr) =
2174143903Sscottl	    rdphys + TI_RD_OFF(ti_return_prodidx_r);
217545386Swpaul
217645386Swpaul	/*
217745386Swpaul	 * Set up the tx ring. Note: for the Tigon 2, we have the option
217845386Swpaul	 * of putting the transmit ring in the host's address space and
217945386Swpaul	 * letting the chip DMA it instead of leaving the ring in the NIC's
218045386Swpaul	 * memory and accessing it through the shared memory region. We
218145386Swpaul	 * do this for the Tigon 2, but it doesn't work on the Tigon 1,
218245386Swpaul	 * so we have to revert to the shared memory scheme if we detect
218345386Swpaul	 * a Tigon 1 chip.
218445386Swpaul	 */
218545386Swpaul	CSR_WRITE_4(sc, TI_WINBASE, TI_TX_RING_BASE);
218645386Swpaul	bzero((char *)sc->ti_rdata->ti_tx_ring,
218745386Swpaul	    TI_TX_RING_CNT * sizeof(struct ti_tx_desc));
218845386Swpaul	rcb = &sc->ti_rdata->ti_info.ti_tx_rcb;
218945386Swpaul	if (sc->ti_hwrev == TI_HWREV_TIGON)
219045386Swpaul		rcb->ti_flags = 0;
219145386Swpaul	else
219245386Swpaul		rcb->ti_flags = TI_RCB_FLAG_HOST_RING;
219345386Swpaul	rcb->ti_flags |= TI_RCB_FLAG_VLAN_ASSIST;
2194147256Sbrooks	if (sc->ti_ifp->if_hwassist)
219558698Sjlemon		rcb->ti_flags |= TI_RCB_FLAG_TCP_UDP_CKSUM |
219658698Sjlemon		     TI_RCB_FLAG_IP_CKSUM | TI_RCB_FLAG_NO_PHDR_CKSUM;
219745386Swpaul	rcb->ti_max_len = TI_TX_RING_CNT;
219845386Swpaul	if (sc->ti_hwrev == TI_HWREV_TIGON)
219945386Swpaul		TI_HOSTADDR(rcb->ti_hostaddr) = TI_TX_RING_BASE;
220045386Swpaul	else
2201143903Sscottl		TI_HOSTADDR(rcb->ti_hostaddr) = rdphys + TI_RD_OFF(ti_tx_ring);
220245386Swpaul	TI_HOSTADDR(sc->ti_rdata->ti_info.ti_tx_considx_ptr) =
2203143903Sscottl	    rdphys + TI_RD_OFF(ti_tx_considx_r);
220445386Swpaul
2205153770Syongari	bus_dmamap_sync(sc->ti_rdata_dmat, sc->ti_rdata_dmamap,
2206153770Syongari	    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
2207153770Syongari
220845386Swpaul	/* Set up tuneables */
220998849Sken#if 0
221045386Swpaul	if (ifp->if_mtu > (ETHERMTU + ETHER_HDR_LEN + ETHER_CRC_LEN))
221145386Swpaul		CSR_WRITE_4(sc, TI_GCR_RX_COAL_TICKS,
221245386Swpaul		    (sc->ti_rx_coal_ticks / 10));
221345386Swpaul	else
221498849Sken#endif
221545386Swpaul		CSR_WRITE_4(sc, TI_GCR_RX_COAL_TICKS, sc->ti_rx_coal_ticks);
221645386Swpaul	CSR_WRITE_4(sc, TI_GCR_TX_COAL_TICKS, sc->ti_tx_coal_ticks);
221745386Swpaul	CSR_WRITE_4(sc, TI_GCR_STAT_TICKS, sc->ti_stat_ticks);
221845386Swpaul	CSR_WRITE_4(sc, TI_GCR_RX_MAX_COAL_BD, sc->ti_rx_max_coal_bds);
221945386Swpaul	CSR_WRITE_4(sc, TI_GCR_TX_MAX_COAL_BD, sc->ti_tx_max_coal_bds);
222045386Swpaul	CSR_WRITE_4(sc, TI_GCR_TX_BUFFER_RATIO, sc->ti_tx_buf_ratio);
222145386Swpaul
222245386Swpaul	/* Turn interrupts on. */
222345386Swpaul	CSR_WRITE_4(sc, TI_GCR_MASK_INTRS, 0);
222445386Swpaul	CSR_WRITE_4(sc, TI_MB_HOSTINTR, 0);
222545386Swpaul
222645386Swpaul	/* Start CPU. */
222745386Swpaul	TI_CLRBIT(sc, TI_CPU_STATE, (TI_CPUSTATE_HALT|TI_CPUSTATE_STEP));
222845386Swpaul
2229131654Sbms	return (0);
223045386Swpaul}
223145386Swpaul
2232143903Sscottlstatic void
2233143903Sscottlti_rdata_cb(void *arg, bus_dma_segment_t *segs, int nseg, int error)
2234143903Sscottl{
2235143903Sscottl	struct ti_softc *sc;
2236143903Sscottl
2237143903Sscottl	sc = arg;
2238143903Sscottl	if (error || nseg != 1)
2239143903Sscottl		return;
2240143903Sscottl
2241143903Sscottl	/*
2242143903Sscottl	 * All of the Tigon data structures need to live at <4GB.  This
2243143903Sscottl	 * cast is fine since busdma was told about this constraint.
2244143903Sscottl	 */
2245153770Syongari	sc->ti_rdata_phys = segs[0].ds_addr;
2246143903Sscottl	return;
2247143903Sscottl}
2248143903Sscottl
224945386Swpaul/*
225045386Swpaul * Probe for a Tigon chip. Check the PCI vendor and device IDs
225145386Swpaul * against our list and return its name if we find a match.
225245386Swpaul */
2253102336Salfredstatic int
2254102336Salfredti_probe(dev)
225549011Swpaul	device_t		dev;
225645386Swpaul{
225745386Swpaul	struct ti_type		*t;
225845386Swpaul
225945386Swpaul	t = ti_devs;
226045386Swpaul
2261131654Sbms	while (t->ti_name != NULL) {
226249011Swpaul		if ((pci_get_vendor(dev) == t->ti_vid) &&
226349011Swpaul		    (pci_get_device(dev) == t->ti_did)) {
226449011Swpaul			device_set_desc(dev, t->ti_name);
2265142398Simp			return (BUS_PROBE_DEFAULT);
226649011Swpaul		}
226745386Swpaul		t++;
226845386Swpaul	}
226945386Swpaul
2270131654Sbms	return (ENXIO);
227145386Swpaul}
227245386Swpaul
227398849Skenstatic int
2274102336Salfredti_attach(dev)
227549011Swpaul	device_t		dev;
227645386Swpaul{
227745386Swpaul	struct ifnet		*ifp;
227845386Swpaul	struct ti_softc		*sc;
2279150719Sjhb	int			error = 0, rid;
2280147256Sbrooks	u_char			eaddr[6];
228145386Swpaul
228249011Swpaul	sc = device_get_softc(dev);
2283150719Sjhb	sc->ti_unit = device_get_unit(dev);
2284153396Sscottl	sc->ti_dev = dev;
228545386Swpaul
228693818Sjhb	mtx_init(&sc->ti_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK,
2287153770Syongari	    MTX_DEF);
2288199559Sjhb	callout_init_mtx(&sc->ti_watchdog, &sc->ti_mtx, 0);
2289113609Snjl	ifmedia_init(&sc->ifmedia, IFM_IMASK, ti_ifmedia_upd, ti_ifmedia_sts);
2290147805Sscottl	ifp = sc->ti_ifp = if_alloc(IFT_ETHER);
2291147805Sscottl	if (ifp == NULL) {
2292150719Sjhb		device_printf(dev, "can not if_alloc()\n");
2293147805Sscottl		error = ENOSPC;
2294147805Sscottl		goto fail;
2295147805Sscottl	}
2296147256Sbrooks	sc->ti_ifp->if_capabilities = IFCAP_HWCSUM |
2297118454Ssimokawa	    IFCAP_VLAN_HWTAGGING | IFCAP_VLAN_MTU;
2298147256Sbrooks	sc->ti_ifp->if_capenable = sc->ti_ifp->if_capabilities;
229969583Swpaul
230045386Swpaul	/*
230145386Swpaul	 * Map control/status registers.
230245386Swpaul	 */
230372813Swpaul	pci_enable_busmaster(dev);
230445386Swpaul
230549011Swpaul	rid = TI_PCI_LOMEM;
2306127135Snjl	sc->ti_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
2307178588Smarius	    RF_ACTIVE);
230849011Swpaul
230949011Swpaul	if (sc->ti_res == NULL) {
2310150719Sjhb		device_printf(dev, "couldn't map memory\n");
231149011Swpaul		error = ENXIO;
231245386Swpaul		goto fail;
231345386Swpaul	}
231445386Swpaul
231549035Swpaul	sc->ti_btag = rman_get_bustag(sc->ti_res);
231649035Swpaul	sc->ti_bhandle = rman_get_bushandle(sc->ti_res);
231749035Swpaul
231849011Swpaul	/* Allocate interrupt */
231949011Swpaul	rid = 0;
2320131652Sbms
2321127135Snjl	sc->ti_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
232249011Swpaul	    RF_SHAREABLE | RF_ACTIVE);
232345386Swpaul
232449011Swpaul	if (sc->ti_irq == NULL) {
2325150719Sjhb		device_printf(dev, "couldn't map interrupt\n");
232649011Swpaul		error = ENXIO;
232745386Swpaul		goto fail;
232845386Swpaul	}
232945386Swpaul
233045386Swpaul	if (ti_chipinit(sc)) {
2331150719Sjhb		device_printf(dev, "chip initialization failed\n");
233249011Swpaul		error = ENXIO;
233345386Swpaul		goto fail;
233445386Swpaul	}
233545386Swpaul
233645386Swpaul	/* Zero out the NIC's on-board SRAM. */
2337153770Syongari	ti_mem_zero(sc, 0x2000, 0x100000 - 0x2000);
233845386Swpaul
233945386Swpaul	/* Init again -- zeroing memory may have clobbered some registers. */
234045386Swpaul	if (ti_chipinit(sc)) {
2341150719Sjhb		device_printf(dev, "chip initialization failed\n");
234249011Swpaul		error = ENXIO;
234345386Swpaul		goto fail;
234445386Swpaul	}
234545386Swpaul
234645386Swpaul	/*
234745386Swpaul	 * Get station address from the EEPROM. Note: the manual states
234845386Swpaul	 * that the MAC address is at offset 0x8c, however the data is
234945386Swpaul	 * stored as two longwords (since that's how it's loaded into
235072645Sasmodai	 * the NIC). This means the MAC address is actually preceded
235145386Swpaul	 * by two zero bytes. We need to skip over those.
235245386Swpaul	 */
2353147256Sbrooks	if (ti_read_eeprom(sc, eaddr,
235445386Swpaul				TI_EE_MAC_OFFSET + 2, ETHER_ADDR_LEN)) {
2355150719Sjhb		device_printf(dev, "failed to read station address\n");
235649011Swpaul		error = ENXIO;
235745386Swpaul		goto fail;
235845386Swpaul	}
235945386Swpaul
236045386Swpaul	/* Allocate the general information block and ring buffers. */
2361166165Smarius	if (bus_dma_tag_create(bus_get_dma_tag(dev),	/* parent */
2362143903Sscottl				1, 0,			/* algnmnt, boundary */
2363143903Sscottl				BUS_SPACE_MAXADDR,	/* lowaddr */
2364143903Sscottl				BUS_SPACE_MAXADDR,	/* highaddr */
2365143903Sscottl				NULL, NULL,		/* filter, filterarg */
2366143903Sscottl				BUS_SPACE_MAXSIZE_32BIT,/* maxsize */
2367143903Sscottl				0,			/* nsegments */
2368143903Sscottl				BUS_SPACE_MAXSIZE_32BIT,/* maxsegsize */
2369143903Sscottl				0,			/* flags */
2370143903Sscottl				NULL, NULL,		/* lockfunc, lockarg */
2371143903Sscottl				&sc->ti_parent_dmat) != 0) {
2372150719Sjhb		device_printf(dev, "Failed to allocate parent dmat\n");
2373143903Sscottl		error = ENOMEM;
2374143903Sscottl		goto fail;
2375143903Sscottl	}
237645386Swpaul
2377143903Sscottl	if (bus_dma_tag_create(sc->ti_parent_dmat,	/* parent */
2378143903Sscottl				PAGE_SIZE, 0,		/* algnmnt, boundary */
2379143903Sscottl				BUS_SPACE_MAXADDR_32BIT,/* lowaddr */
2380143903Sscottl				BUS_SPACE_MAXADDR,	/* highaddr */
2381143903Sscottl				NULL, NULL,		/* filter, filterarg */
2382143903Sscottl				sizeof(struct ti_ring_data),	/* maxsize */
2383143903Sscottl				1,			/* nsegments */
2384143903Sscottl				sizeof(struct ti_ring_data),	/* maxsegsize */
2385143903Sscottl				0,			/* flags */
2386143903Sscottl				NULL, NULL,		/* lockfunc, lockarg */
2387143903Sscottl				&sc->ti_rdata_dmat) != 0) {
2388150719Sjhb		device_printf(dev, "Failed to allocate rdata dmat\n");
2389143903Sscottl		error = ENOMEM;
239045386Swpaul		goto fail;
239145386Swpaul	}
239245386Swpaul
2393143903Sscottl	if (bus_dmamem_alloc(sc->ti_rdata_dmat, (void**)&sc->ti_rdata,
2394143903Sscottl			     BUS_DMA_NOWAIT, &sc->ti_rdata_dmamap) != 0) {
2395150719Sjhb		device_printf(dev, "Failed to allocate rdata memory\n");
2396143903Sscottl		error = ENOMEM;
2397143903Sscottl		goto fail;
2398143903Sscottl	}
2399143903Sscottl
2400143903Sscottl	if (bus_dmamap_load(sc->ti_rdata_dmat, sc->ti_rdata_dmamap,
2401143903Sscottl			    sc->ti_rdata, sizeof(struct ti_ring_data),
2402143903Sscottl			    ti_rdata_cb, sc, BUS_DMA_NOWAIT) != 0) {
2403150719Sjhb		device_printf(dev, "Failed to load rdata segments\n");
2404143903Sscottl		error = ENOMEM;
2405143903Sscottl		goto fail;
2406143903Sscottl	}
2407143903Sscottl
240845386Swpaul	bzero(sc->ti_rdata, sizeof(struct ti_ring_data));
240945386Swpaul
241045386Swpaul	/* Try to allocate memory for jumbo buffers. */
241145386Swpaul	if (ti_alloc_jumbo_mem(sc)) {
2412150719Sjhb		device_printf(dev, "jumbo buffer allocation failed\n");
241349011Swpaul		error = ENXIO;
241445386Swpaul		goto fail;
241545386Swpaul	}
241645386Swpaul
2417153396Sscottl	if (bus_dma_tag_create(sc->ti_parent_dmat,	/* parent */
2418153396Sscottl				1, 0,			/* algnmnt, boundary */
2419153396Sscottl				BUS_SPACE_MAXADDR,	/* lowaddr */
2420153396Sscottl				BUS_SPACE_MAXADDR,	/* highaddr */
2421153396Sscottl				NULL, NULL,		/* filter, filterarg */
2422153396Sscottl				MCLBYTES * TI_MAXTXSEGS,/* maxsize */
2423153396Sscottl				TI_MAXTXSEGS,		/* nsegments */
2424153396Sscottl				MCLBYTES,		/* maxsegsize */
2425153396Sscottl				0,			/* flags */
2426153396Sscottl				NULL, NULL,		/* lockfunc, lockarg */
2427153396Sscottl				&sc->ti_mbuftx_dmat) != 0) {
2428153396Sscottl		device_printf(dev, "Failed to allocate rdata dmat\n");
2429153396Sscottl		error = ENOMEM;
2430153396Sscottl		goto fail;
2431153396Sscottl	}
2432153396Sscottl
2433153396Sscottl	if (bus_dma_tag_create(sc->ti_parent_dmat,	/* parent */
2434153396Sscottl				1, 0,			/* algnmnt, boundary */
2435153396Sscottl				BUS_SPACE_MAXADDR,	/* lowaddr */
2436153396Sscottl				BUS_SPACE_MAXADDR,	/* highaddr */
2437153396Sscottl				NULL, NULL,		/* filter, filterarg */
2438153396Sscottl				MCLBYTES,		/* maxsize */
2439153396Sscottl				1,			/* nsegments */
2440153396Sscottl				MCLBYTES,		/* maxsegsize */
2441153396Sscottl				0,			/* flags */
2442153396Sscottl				NULL, NULL,		/* lockfunc, lockarg */
2443153396Sscottl				&sc->ti_mbufrx_dmat) != 0) {
2444153396Sscottl		device_printf(dev, "Failed to allocate rdata dmat\n");
2445153396Sscottl		error = ENOMEM;
2446153396Sscottl		goto fail;
2447153396Sscottl	}
2448153396Sscottl
2449153396Sscottl	if (ti_alloc_dmamaps(sc)) {
2450153396Sscottl		device_printf(dev, "dma map creation failed\n");
2451153396Sscottl		error = ENXIO;
2452153396Sscottl		goto fail;
2453153396Sscottl	}
2454153396Sscottl
245563699Swpaul	/*
245663699Swpaul	 * We really need a better way to tell a 1000baseTX card
245763699Swpaul	 * from a 1000baseSX one, since in theory there could be
245863699Swpaul	 * OEMed 1000baseTX cards from lame vendors who aren't
245963699Swpaul	 * clever enough to change the PCI ID. For the moment
246063699Swpaul	 * though, the AceNIC is the only copper card available.
246163699Swpaul	 */
246263699Swpaul	if (pci_get_vendor(dev) == ALT_VENDORID &&
246363699Swpaul	    pci_get_device(dev) == ALT_DEVICEID_ACENIC_COPPER)
246463699Swpaul		sc->ti_copper = 1;
246564139Swpaul	/* Ok, it's not the only copper card available. */
246664139Swpaul	if (pci_get_vendor(dev) == NG_VENDORID &&
246764139Swpaul	    pci_get_device(dev) == NG_DEVICEID_GA620T)
246864139Swpaul		sc->ti_copper = 1;
246963699Swpaul
247045386Swpaul	/* Set default tuneable values. */
247145386Swpaul	sc->ti_stat_ticks = 2 * TI_TICKS_PER_SEC;
247298849Sken#if 0
247345386Swpaul	sc->ti_rx_coal_ticks = TI_TICKS_PER_SEC / 5000;
247498849Sken#endif
247598849Sken	sc->ti_rx_coal_ticks = 170;
247645386Swpaul	sc->ti_tx_coal_ticks = TI_TICKS_PER_SEC / 500;
247745386Swpaul	sc->ti_rx_max_coal_bds = 64;
247898849Sken#if 0
247945386Swpaul	sc->ti_tx_max_coal_bds = 128;
248098849Sken#endif
248198849Sken	sc->ti_tx_max_coal_bds = 32;
248245386Swpaul	sc->ti_tx_buf_ratio = 21;
248345386Swpaul
248445386Swpaul	/* Set up ifnet structure */
248545386Swpaul	ifp->if_softc = sc;
2486121816Sbrooks	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
2487153281Sscottl	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
248845386Swpaul	ifp->if_ioctl = ti_ioctl;
248945386Swpaul	ifp->if_start = ti_start;
249045386Swpaul	ifp->if_init = ti_init;
2491176413Sremko	ifp->if_baudrate = 1000000000;
249245386Swpaul	ifp->if_mtu = ETHERMTU;
249345386Swpaul	ifp->if_snd.ifq_maxlen = TI_TX_RING_CNT - 1;
249445386Swpaul
249545386Swpaul	/* Set up ifmedia support. */
249663699Swpaul	if (sc->ti_copper) {
249763699Swpaul		/*
249863699Swpaul		 * Copper cards allow manual 10/100 mode selection,
249963699Swpaul		 * but not manual 1000baseTX mode selection. Why?
250063699Swpaul		 * Becuase currently there's no way to specify the
250163699Swpaul		 * master/slave setting through the firmware interface,
250263699Swpaul		 * so Alteon decided to just bag it and handle it
250363699Swpaul		 * via autonegotiation.
250463699Swpaul		 */
250563699Swpaul		ifmedia_add(&sc->ifmedia, IFM_ETHER|IFM_10_T, 0, NULL);
250663699Swpaul		ifmedia_add(&sc->ifmedia,
250763699Swpaul		    IFM_ETHER|IFM_10_T|IFM_FDX, 0, NULL);
250863699Swpaul		ifmedia_add(&sc->ifmedia, IFM_ETHER|IFM_100_TX, 0, NULL);
250963699Swpaul		ifmedia_add(&sc->ifmedia,
251063699Swpaul		    IFM_ETHER|IFM_100_TX|IFM_FDX, 0, NULL);
251195673Sphk		ifmedia_add(&sc->ifmedia, IFM_ETHER|IFM_1000_T, 0, NULL);
251263699Swpaul		ifmedia_add(&sc->ifmedia,
251395673Sphk		    IFM_ETHER|IFM_1000_T|IFM_FDX, 0, NULL);
251463699Swpaul	} else {
251563699Swpaul		/* Fiber cards don't support 10/100 modes. */
251663699Swpaul		ifmedia_add(&sc->ifmedia, IFM_ETHER|IFM_1000_SX, 0, NULL);
251763699Swpaul		ifmedia_add(&sc->ifmedia,
251863699Swpaul		    IFM_ETHER|IFM_1000_SX|IFM_FDX, 0, NULL);
251963699Swpaul	}
252045386Swpaul	ifmedia_add(&sc->ifmedia, IFM_ETHER|IFM_AUTO, 0, NULL);
252145386Swpaul	ifmedia_set(&sc->ifmedia, IFM_ETHER|IFM_AUTO);
252245386Swpaul
252345386Swpaul	/*
252498849Sken	 * We're assuming here that card initialization is a sequential
252598849Sken	 * thing.  If it isn't, multiple cards probing at the same time
252698849Sken	 * could stomp on the list of softcs here.
252798849Sken	 */
252898849Sken
252998849Sken	/* Register the device */
253098849Sken	sc->dev = make_dev(&ti_cdevsw, sc->ti_unit, UID_ROOT, GID_OPERATOR,
253198849Sken			   0600, "ti%d", sc->ti_unit);
2532120980Sphk	sc->dev->si_drv1 = sc;
253398849Sken
253498849Sken	/*
253563090Sarchie	 * Call MI attach routine.
253645386Swpaul	 */
2537147256Sbrooks	ether_ifattach(ifp, eaddr);
253845386Swpaul
2539113609Snjl	/* Hook interrupt last to avoid having to lock softc */
2540153281Sscottl	error = bus_setup_intr(dev, sc->ti_irq, INTR_TYPE_NET|INTR_MPSAFE,
2541166901Spiso	   NULL, ti_intr, sc, &sc->ti_intrhand);
2542112872Snjl
2543112872Snjl	if (error) {
2544150719Sjhb		device_printf(dev, "couldn't set up irq\n");
2545112872Snjl		goto fail;
2546112872Snjl	}
2547112872Snjl
254845386Swpaulfail:
2549153770Syongari	if (error)
2550112872Snjl		ti_detach(dev);
2551112872Snjl
2552131654Sbms	return (error);
255345386Swpaul}
255445386Swpaul
255598849Sken/*
2556113609Snjl * Shutdown hardware and free up resources. This can be called any
2557113609Snjl * time after the mutex has been initialized. It is called in both
2558113609Snjl * the error case in attach and the normal detach case so it needs
2559113609Snjl * to be careful about only freeing resources that have actually been
2560113609Snjl * allocated.
2561113609Snjl */
2562102336Salfredstatic int
2563102336Salfredti_detach(dev)
256449011Swpaul	device_t		dev;
256549011Swpaul{
256649011Swpaul	struct ti_softc		*sc;
256749011Swpaul	struct ifnet		*ifp;
256849011Swpaul
256949011Swpaul	sc = device_get_softc(dev);
2570144407Sscottl	if (sc->dev)
2571144407Sscottl		destroy_dev(sc->dev);
2572112930Sphk	KASSERT(mtx_initialized(&sc->ti_mtx), ("ti mutex not initialized"));
2573147256Sbrooks	ifp = sc->ti_ifp;
2574199559Sjhb	if (device_is_attached(dev)) {
2575199559Sjhb		ether_ifdetach(ifp);
2576199559Sjhb		TI_LOCK(sc);
2577153770Syongari		ti_stop(sc);
2578199559Sjhb		TI_UNLOCK(sc);
2579199559Sjhb	}
258049011Swpaul
2581113609Snjl	/* These should only be active if attach succeeded */
2582199559Sjhb	callout_drain(&sc->ti_watchdog);
2583199559Sjhb	bus_generic_detach(dev);
2584153770Syongari	ti_free_dmamaps(sc);
2585113609Snjl	ifmedia_removeall(&sc->ifmedia);
258649011Swpaul
2587153288Sscottl#ifdef TI_PRIVATE_JUMBOS
2588153288Sscottl	if (sc->ti_cdata.ti_jumbo_buf)
2589153288Sscottl		bus_dmamem_free(sc->ti_jumbo_dmat, sc->ti_cdata.ti_jumbo_buf,
2590153288Sscottl		    sc->ti_jumbo_dmamap);
2591153396Sscottl#endif
2592153288Sscottl	if (sc->ti_jumbo_dmat)
2593153288Sscottl		bus_dma_tag_destroy(sc->ti_jumbo_dmat);
2594153396Sscottl	if (sc->ti_mbuftx_dmat)
2595153396Sscottl		bus_dma_tag_destroy(sc->ti_mbuftx_dmat);
2596153396Sscottl	if (sc->ti_mbufrx_dmat)
2597153396Sscottl		bus_dma_tag_destroy(sc->ti_mbufrx_dmat);
2598143903Sscottl	if (sc->ti_rdata)
2599143903Sscottl		bus_dmamem_free(sc->ti_rdata_dmat, sc->ti_rdata,
2600143903Sscottl				sc->ti_rdata_dmamap);
2601143903Sscottl	if (sc->ti_rdata_dmat)
2602143903Sscottl		bus_dma_tag_destroy(sc->ti_rdata_dmat);
2603143903Sscottl	if (sc->ti_parent_dmat)
2604143903Sscottl		bus_dma_tag_destroy(sc->ti_parent_dmat);
2605112872Snjl	if (sc->ti_intrhand)
2606112872Snjl		bus_teardown_intr(dev, sc->ti_irq, sc->ti_intrhand);
2607112872Snjl	if (sc->ti_irq)
2608112872Snjl		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->ti_irq);
2609112872Snjl	if (sc->ti_res) {
2610112872Snjl		bus_release_resource(dev, SYS_RES_MEMORY, TI_PCI_LOMEM,
2611112872Snjl		    sc->ti_res);
2612112872Snjl	}
2613151297Sru	if (ifp)
2614151297Sru		if_free(ifp);
261549011Swpaul
261667087Swpaul	mtx_destroy(&sc->ti_mtx);
261749011Swpaul
2618131654Sbms	return (0);
261949011Swpaul}
262049011Swpaul
262198849Sken#ifdef TI_JUMBO_HDRSPLIT
262245386Swpaul/*
262398849Sken * If hdr_len is 0, that means that header splitting wasn't done on
262498849Sken * this packet for some reason.  The two most likely reasons are that
262598849Sken * the protocol isn't a supported protocol for splitting, or this
262698849Sken * packet had a fragment offset that wasn't 0.
262798849Sken *
262898849Sken * The header length, if it is non-zero, will always be the length of
262998849Sken * the headers on the packet, but that length could be longer than the
263098849Sken * first mbuf.  So we take the minimum of the two as the actual
2631131652Sbms * length.
263298849Sken */
263398849Skenstatic __inline void
263498849Skenti_hdr_split(struct mbuf *top, int hdr_len, int pkt_len, int idx)
263598849Sken{
263698849Sken	int i = 0;
263798849Sken	int lengths[4] = {0, 0, 0, 0};
263898849Sken	struct mbuf *m, *mp;
263998849Sken
264098849Sken	if (hdr_len != 0)
264198849Sken		top->m_len = min(hdr_len, top->m_len);
264298849Sken	pkt_len -= top->m_len;
264398849Sken	lengths[i++] = top->m_len;
264498849Sken
264598849Sken	mp = top;
264698849Sken	for (m = top->m_next; m && pkt_len; m = m->m_next) {
264798849Sken		m->m_len = m->m_ext.ext_size = min(m->m_len, pkt_len);
264898849Sken		pkt_len -= m->m_len;
264998849Sken		lengths[i++] = m->m_len;
265098849Sken		mp = m;
265198849Sken	}
265298849Sken
265398849Sken#if 0
265498849Sken	if (hdr_len != 0)
265598849Sken		printf("got split packet: ");
265698849Sken	else
265798849Sken		printf("got non-split packet: ");
2658131652Sbms
265998849Sken	printf("%d,%d,%d,%d = %d\n", lengths[0],
266098849Sken	    lengths[1], lengths[2], lengths[3],
266198849Sken	    lengths[0] + lengths[1] + lengths[2] +
266298849Sken	    lengths[3]);
266398849Sken#endif
266498849Sken
266598849Sken	if (pkt_len)
266698849Sken		panic("header splitting didn't");
2667131652Sbms
266898849Sken	if (m) {
266998849Sken		m_freem(m);
267098849Sken		mp->m_next = NULL;
267198849Sken
267298849Sken	}
267398849Sken	if (mp->m_next != NULL)
267498849Sken		panic("ti_hdr_split: last mbuf in chain should be null");
267598849Sken}
267698849Sken#endif /* TI_JUMBO_HDRSPLIT */
267798849Sken
267898849Sken/*
267945386Swpaul * Frame reception handling. This is called if there's a frame
268045386Swpaul * on the receive return list.
268145386Swpaul *
268245386Swpaul * Note: we have to be able to handle three possibilities here:
268345386Swpaul * 1) the frame is from the mini receive ring (can only happen)
268445386Swpaul *    on Tigon 2 boards)
268545386Swpaul * 2) the frame is from the jumbo recieve ring
268645386Swpaul * 3) the frame is from the standard receive ring
268745386Swpaul */
268845386Swpaul
2689102336Salfredstatic void
2690102336Salfredti_rxeof(sc)
269145386Swpaul	struct ti_softc		*sc;
269245386Swpaul{
2693153396Sscottl	bus_dmamap_t		map;
269445386Swpaul	struct ifnet		*ifp;
269548597Swpaul	struct ti_cmd_desc	cmd;
269645386Swpaul
2697122689Ssam	TI_LOCK_ASSERT(sc);
2698122689Ssam
2699147256Sbrooks	ifp = sc->ti_ifp;
270045386Swpaul
2701131654Sbms	while (sc->ti_rx_saved_considx != sc->ti_return_prodidx.ti_idx) {
270245386Swpaul		struct ti_rx_desc	*cur_rx;
270345386Swpaul		u_int32_t		rxidx;
270445386Swpaul		struct mbuf		*m = NULL;
270545386Swpaul		u_int16_t		vlan_tag = 0;
270645386Swpaul		int			have_tag = 0;
270745386Swpaul
270845386Swpaul		cur_rx =
270945386Swpaul		    &sc->ti_rdata->ti_rx_return_ring[sc->ti_rx_saved_considx];
271045386Swpaul		rxidx = cur_rx->ti_idx;
271145386Swpaul		TI_INC(sc->ti_rx_saved_considx, TI_RETURN_RING_CNT);
271245386Swpaul
271345386Swpaul		if (cur_rx->ti_flags & TI_BDFLAG_VLAN_TAG) {
271445386Swpaul			have_tag = 1;
271577058Sphk			vlan_tag = cur_rx->ti_vlan_tag & 0xfff;
271645386Swpaul		}
271745386Swpaul
271845386Swpaul		if (cur_rx->ti_flags & TI_BDFLAG_JUMBO_RING) {
271998849Sken
272045386Swpaul			TI_INC(sc->ti_jumbo, TI_JUMBO_RX_RING_CNT);
272145386Swpaul			m = sc->ti_cdata.ti_rx_jumbo_chain[rxidx];
272245386Swpaul			sc->ti_cdata.ti_rx_jumbo_chain[rxidx] = NULL;
2723153396Sscottl			map = sc->ti_cdata.ti_rx_jumbo_maps[rxidx];
2724153396Sscottl			bus_dmamap_sync(sc->ti_jumbo_dmat, map,
2725153396Sscottl			    BUS_DMASYNC_POSTREAD);
2726153396Sscottl			bus_dmamap_unload(sc->ti_jumbo_dmat, map);
272745386Swpaul			if (cur_rx->ti_flags & TI_BDFLAG_ERROR) {
272845386Swpaul				ifp->if_ierrors++;
272945386Swpaul				ti_newbuf_jumbo(sc, sc->ti_jumbo, m);
273045386Swpaul				continue;
273145386Swpaul			}
273248597Swpaul			if (ti_newbuf_jumbo(sc, sc->ti_jumbo, NULL) == ENOBUFS) {
273348597Swpaul				ifp->if_ierrors++;
273448597Swpaul				ti_newbuf_jumbo(sc, sc->ti_jumbo, m);
273548597Swpaul				continue;
273648597Swpaul			}
273798849Sken#ifdef TI_PRIVATE_JUMBOS
2738131655Sbms			m->m_len = cur_rx->ti_len;
273998849Sken#else /* TI_PRIVATE_JUMBOS */
274098849Sken#ifdef TI_JUMBO_HDRSPLIT
274198849Sken			if (sc->ti_hdrsplit)
274298849Sken				ti_hdr_split(m, TI_HOSTADDR(cur_rx->ti_addr),
274398849Sken					     cur_rx->ti_len, rxidx);
274498849Sken			else
274598849Sken#endif /* TI_JUMBO_HDRSPLIT */
2746131655Sbms			m_adj(m, cur_rx->ti_len - m->m_pkthdr.len);
274798849Sken#endif /* TI_PRIVATE_JUMBOS */
274845386Swpaul		} else if (cur_rx->ti_flags & TI_BDFLAG_MINI_RING) {
274945386Swpaul			TI_INC(sc->ti_mini, TI_MINI_RX_RING_CNT);
275045386Swpaul			m = sc->ti_cdata.ti_rx_mini_chain[rxidx];
275145386Swpaul			sc->ti_cdata.ti_rx_mini_chain[rxidx] = NULL;
2752153396Sscottl			map = sc->ti_cdata.ti_rx_mini_maps[rxidx];
2753153396Sscottl			bus_dmamap_sync(sc->ti_mbufrx_dmat, map,
2754153396Sscottl			    BUS_DMASYNC_POSTREAD);
2755153396Sscottl			bus_dmamap_unload(sc->ti_mbufrx_dmat, map);
275645386Swpaul			if (cur_rx->ti_flags & TI_BDFLAG_ERROR) {
275745386Swpaul				ifp->if_ierrors++;
275845386Swpaul				ti_newbuf_mini(sc, sc->ti_mini, m);
275945386Swpaul				continue;
276045386Swpaul			}
276148597Swpaul			if (ti_newbuf_mini(sc, sc->ti_mini, NULL) == ENOBUFS) {
276248597Swpaul				ifp->if_ierrors++;
276348597Swpaul				ti_newbuf_mini(sc, sc->ti_mini, m);
276448597Swpaul				continue;
276548597Swpaul			}
276698849Sken			m->m_len = cur_rx->ti_len;
276745386Swpaul		} else {
276845386Swpaul			TI_INC(sc->ti_std, TI_STD_RX_RING_CNT);
276945386Swpaul			m = sc->ti_cdata.ti_rx_std_chain[rxidx];
277045386Swpaul			sc->ti_cdata.ti_rx_std_chain[rxidx] = NULL;
2771153396Sscottl			map = sc->ti_cdata.ti_rx_std_maps[rxidx];
2772153396Sscottl			bus_dmamap_sync(sc->ti_mbufrx_dmat, map,
2773153396Sscottl			    BUS_DMASYNC_POSTREAD);
2774153396Sscottl			bus_dmamap_unload(sc->ti_mbufrx_dmat, map);
277545386Swpaul			if (cur_rx->ti_flags & TI_BDFLAG_ERROR) {
277645386Swpaul				ifp->if_ierrors++;
277745386Swpaul				ti_newbuf_std(sc, sc->ti_std, m);
277845386Swpaul				continue;
277945386Swpaul			}
278048597Swpaul			if (ti_newbuf_std(sc, sc->ti_std, NULL) == ENOBUFS) {
278148597Swpaul				ifp->if_ierrors++;
278248597Swpaul				ti_newbuf_std(sc, sc->ti_std, m);
278348597Swpaul				continue;
278448597Swpaul			}
278598849Sken			m->m_len = cur_rx->ti_len;
278645386Swpaul		}
278745386Swpaul
278898849Sken		m->m_pkthdr.len = cur_rx->ti_len;
278945386Swpaul		ifp->if_ipackets++;
279045386Swpaul		m->m_pkthdr.rcvif = ifp;
279145386Swpaul
279258698Sjlemon		if (ifp->if_hwassist) {
279358698Sjlemon			m->m_pkthdr.csum_flags |= CSUM_IP_CHECKED |
279458698Sjlemon			    CSUM_DATA_VALID;
279558698Sjlemon			if ((cur_rx->ti_ip_cksum ^ 0xffff) == 0)
279658698Sjlemon				m->m_pkthdr.csum_flags |= CSUM_IP_VALID;
279758698Sjlemon			m->m_pkthdr.csum_data = cur_rx->ti_tcp_udp_cksum;
279858698Sjlemon		}
279945386Swpaul
280045386Swpaul		/*
2801106936Ssam		 * If we received a packet with a vlan tag,
2802106936Ssam		 * tag it before passing the packet upward.
280345386Swpaul		 */
2804153512Sglebius		if (have_tag) {
2805162375Sandre			m->m_pkthdr.ether_vtag = vlan_tag;
2806162375Sandre			m->m_flags |= M_VLANTAG;
2807153512Sglebius		}
2808122689Ssam		TI_UNLOCK(sc);
2809106936Ssam		(*ifp->if_input)(ifp, m);
2810122689Ssam		TI_LOCK(sc);
281145386Swpaul	}
281245386Swpaul
281345386Swpaul	/* Only necessary on the Tigon 1. */
281445386Swpaul	if (sc->ti_hwrev == TI_HWREV_TIGON)
281545386Swpaul		CSR_WRITE_4(sc, TI_GCR_RXRETURNCONS_IDX,
281645386Swpaul		    sc->ti_rx_saved_considx);
281745386Swpaul
281848597Swpaul	TI_UPDATE_STDPROD(sc, sc->ti_std);
281948597Swpaul	TI_UPDATE_MINIPROD(sc, sc->ti_mini);
282048597Swpaul	TI_UPDATE_JUMBOPROD(sc, sc->ti_jumbo);
282145386Swpaul}
282245386Swpaul
2823102336Salfredstatic void
2824102336Salfredti_txeof(sc)
282545386Swpaul	struct ti_softc		*sc;
282645386Swpaul{
2827153982Syongari	struct ti_txdesc	*txd;
2828153982Syongari	struct ti_tx_desc	txdesc;
282945386Swpaul	struct ti_tx_desc	*cur_tx = NULL;
283045386Swpaul	struct ifnet		*ifp;
2831153982Syongari	int			idx;
283245386Swpaul
2833147256Sbrooks	ifp = sc->ti_ifp;
283445386Swpaul
2835153982Syongari	txd = STAILQ_FIRST(&sc->ti_cdata.ti_txbusyq);
2836153982Syongari	if (txd == NULL)
2837153982Syongari		return;
283845386Swpaul	/*
283945386Swpaul	 * Go through our tx ring and free mbufs for those
284045386Swpaul	 * frames that have been sent.
284145386Swpaul	 */
2842153982Syongari	for (idx = sc->ti_tx_saved_considx; idx != sc->ti_tx_considx.ti_idx;
2843153982Syongari	    TI_INC(idx, TI_TX_RING_CNT)) {
284445386Swpaul		if (sc->ti_hwrev == TI_HWREV_TIGON) {
2845153770Syongari			ti_mem_read(sc, TI_TX_RING_BASE + idx * sizeof(txdesc),
2846153770Syongari			    sizeof(txdesc), &txdesc);
2847153770Syongari			cur_tx = &txdesc;
284845386Swpaul		} else
284945386Swpaul			cur_tx = &sc->ti_rdata->ti_tx_ring[idx];
285048011Swpaul		sc->ti_txcnt--;
2851153982Syongari		ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
2852153982Syongari		if ((cur_tx->ti_flags & TI_BDFLAG_END) == 0)
2853153982Syongari			continue;
2854153982Syongari		bus_dmamap_sync(sc->ti_mbuftx_dmat, txd->tx_dmamap,
2855153982Syongari		    BUS_DMASYNC_POSTWRITE);
2856153982Syongari		bus_dmamap_unload(sc->ti_mbuftx_dmat, txd->tx_dmamap);
2857153982Syongari
2858153982Syongari		ifp->if_opackets++;
2859153982Syongari		m_freem(txd->tx_m);
2860153982Syongari		txd->tx_m = NULL;
2861153982Syongari		STAILQ_REMOVE_HEAD(&sc->ti_cdata.ti_txbusyq, tx_q);
2862153982Syongari		STAILQ_INSERT_TAIL(&sc->ti_cdata.ti_txfreeq, txd, tx_q);
2863153982Syongari		txd = STAILQ_FIRST(&sc->ti_cdata.ti_txbusyq);
286445386Swpaul	}
2865153982Syongari	sc->ti_tx_saved_considx = idx;
286645386Swpaul
2867199559Sjhb	sc->ti_timer = sc->ti_txcnt > 0 ? 5 : 0;
286845386Swpaul}
286945386Swpaul
2870102336Salfredstatic void
2871102336Salfredti_intr(xsc)
287245386Swpaul	void			*xsc;
287345386Swpaul{
287445386Swpaul	struct ti_softc		*sc;
287545386Swpaul	struct ifnet		*ifp;
287645386Swpaul
287745386Swpaul	sc = xsc;
287867087Swpaul	TI_LOCK(sc);
2879147256Sbrooks	ifp = sc->ti_ifp;
288045386Swpaul
288198849Sken/*#ifdef notdef*/
288245386Swpaul	/* Avoid this for now -- checking this register is expensive. */
288345386Swpaul	/* Make sure this is really our interrupt. */
288467087Swpaul	if (!(CSR_READ_4(sc, TI_MISC_HOST_CTL) & TI_MHC_INTSTATE)) {
288567087Swpaul		TI_UNLOCK(sc);
288645386Swpaul		return;
288767087Swpaul	}
288898849Sken/*#endif*/
288945386Swpaul
289045386Swpaul	/* Ack interrupt and stop others from occuring. */
289145386Swpaul	CSR_WRITE_4(sc, TI_MB_HOSTINTR, 1);
289245386Swpaul
2893148887Srwatson	if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
289445386Swpaul		/* Check RX return ring producer/consumer */
289545386Swpaul		ti_rxeof(sc);
289645386Swpaul
289745386Swpaul		/* Check TX ring producer/consumer */
289845386Swpaul		ti_txeof(sc);
289945386Swpaul	}
290045386Swpaul
290145386Swpaul	ti_handle_events(sc);
290245386Swpaul
290345386Swpaul	/* Re-enable interrupts. */
290445386Swpaul	CSR_WRITE_4(sc, TI_MB_HOSTINTR, 0);
290545386Swpaul
2906148887Srwatson	if (ifp->if_drv_flags & IFF_DRV_RUNNING &&
2907148887Srwatson	    ifp->if_snd.ifq_head != NULL)
2908153770Syongari		ti_start_locked(ifp);
290945386Swpaul
291067087Swpaul	TI_UNLOCK(sc);
291145386Swpaul}
291245386Swpaul
2913102336Salfredstatic void
2914102336Salfredti_stats_update(sc)
291545386Swpaul	struct ti_softc		*sc;
291645386Swpaul{
291745386Swpaul	struct ifnet		*ifp;
291845386Swpaul
2919147256Sbrooks	ifp = sc->ti_ifp;
292045386Swpaul
2921153770Syongari	bus_dmamap_sync(sc->ti_rdata_dmat, sc->ti_rdata_dmamap,
2922153770Syongari	    BUS_DMASYNC_POSTREAD);
2923153770Syongari
292445386Swpaul	ifp->if_collisions +=
292545386Swpaul	   (sc->ti_rdata->ti_info.ti_stats.dot3StatsSingleCollisionFrames +
292645386Swpaul	   sc->ti_rdata->ti_info.ti_stats.dot3StatsMultipleCollisionFrames +
292745386Swpaul	   sc->ti_rdata->ti_info.ti_stats.dot3StatsExcessiveCollisions +
292845386Swpaul	   sc->ti_rdata->ti_info.ti_stats.dot3StatsLateCollisions) -
292945386Swpaul	   ifp->if_collisions;
2930153770Syongari
2931153770Syongari	bus_dmamap_sync(sc->ti_rdata_dmat, sc->ti_rdata_dmamap,
2932153770Syongari	    BUS_DMASYNC_PREREAD);
293345386Swpaul}
293445386Swpaul
2935153982Syongari/*
2936153982Syongari * Encapsulate an mbuf chain in the tx ring  by coupling the mbuf data
2937153982Syongari * pointers to descriptors.
2938153982Syongari */
2939153982Syongaristatic int
2940153982Syongariti_encap(sc, m_head)
2941153982Syongari	struct ti_softc		*sc;
2942153982Syongari	struct mbuf		**m_head;
2943153396Sscottl{
2944153982Syongari	struct ti_txdesc	*txd;
2945153982Syongari	struct ti_tx_desc	*f;
2946153770Syongari	struct ti_tx_desc	txdesc;
2947161236Syongari	struct mbuf		*m;
2948153982Syongari	bus_dma_segment_t	txsegs[TI_MAXTXSEGS];
2949153396Sscottl	u_int16_t		csum_flags;
2950153982Syongari	int			error, frag, i, nseg;
2951153396Sscottl
2952153982Syongari	if ((txd = STAILQ_FIRST(&sc->ti_cdata.ti_txfreeq)) == NULL)
2953153982Syongari		return (ENOBUFS);
2954153396Sscottl
2955153982Syongari	error = bus_dmamap_load_mbuf_sg(sc->ti_mbuftx_dmat, txd->tx_dmamap,
2956161236Syongari	    *m_head, txsegs, &nseg, 0);
2957153982Syongari	if (error == EFBIG) {
2958161236Syongari		m = m_defrag(*m_head, M_DONTWAIT);
2959161236Syongari		if (m == NULL) {
2960161236Syongari			m_freem(*m_head);
2961161236Syongari			*m_head = NULL;
2962153982Syongari			return (ENOMEM);
2963153982Syongari		}
2964161236Syongari		*m_head = m;
2965153982Syongari		error = bus_dmamap_load_mbuf_sg(sc->ti_mbuftx_dmat,
2966161236Syongari		    txd->tx_dmamap, *m_head, txsegs, &nseg, 0);
2967153982Syongari		if (error) {
2968161236Syongari			m_freem(*m_head);
2969161236Syongari			*m_head = NULL;
2970153982Syongari			return (error);
2971153982Syongari		}
2972153982Syongari	} else if (error != 0)
2973153982Syongari		return (error);
2974153982Syongari	if (nseg == 0) {
2975161236Syongari		m_freem(*m_head);
2976161236Syongari		*m_head = NULL;
2977153982Syongari		return (EIO);
2978153776Sscottl	}
2979153776Sscottl
2980153982Syongari	if (sc->ti_txcnt + nseg >= TI_TX_RING_CNT) {
2981153982Syongari		bus_dmamap_unload(sc->ti_mbuftx_dmat, txd->tx_dmamap);
2982153982Syongari		return (ENOBUFS);
2983153982Syongari	}
2984153982Syongari
2985161236Syongari	m = *m_head;
2986161236Syongari	csum_flags = 0;
2987161236Syongari	if (m->m_pkthdr.csum_flags) {
2988161236Syongari		if (m->m_pkthdr.csum_flags & CSUM_IP)
2989161236Syongari			csum_flags |= TI_BDFLAG_IP_CKSUM;
2990161236Syongari		if (m->m_pkthdr.csum_flags & (CSUM_TCP | CSUM_UDP))
2991161236Syongari			csum_flags |= TI_BDFLAG_TCP_UDP_CKSUM;
2992161236Syongari		if (m->m_flags & M_LASTFRAG)
2993161236Syongari			csum_flags |= TI_BDFLAG_IP_FRAG_END;
2994161236Syongari		else if (m->m_flags & M_FRAG)
2995161236Syongari			csum_flags |= TI_BDFLAG_IP_FRAG;
2996161236Syongari	}
2997161236Syongari
2998153982Syongari	bus_dmamap_sync(sc->ti_mbuftx_dmat, txd->tx_dmamap,
2999153982Syongari	    BUS_DMASYNC_PREWRITE);
3000153982Syongari	bus_dmamap_sync(sc->ti_rdata_dmat, sc->ti_rdata_dmamap,
3001153982Syongari	    BUS_DMASYNC_PREWRITE);
3002153982Syongari
3003153982Syongari	frag = sc->ti_tx_saved_prodidx;
3004153982Syongari	for (i = 0; i < nseg; i++) {
3005153396Sscottl		if (sc->ti_hwrev == TI_HWREV_TIGON) {
3006153770Syongari			bzero(&txdesc, sizeof(txdesc));
3007153770Syongari			f = &txdesc;
3008153396Sscottl		} else
3009153396Sscottl			f = &sc->ti_rdata->ti_tx_ring[frag];
3010153982Syongari		ti_hostaddr64(&f->ti_addr, txsegs[i].ds_addr);
3011153982Syongari		f->ti_len = txsegs[i].ds_len;
3012153396Sscottl		f->ti_flags = csum_flags;
3013162375Sandre		if (m->m_flags & M_VLANTAG) {
3014153396Sscottl			f->ti_flags |= TI_BDFLAG_VLAN_TAG;
3015162375Sandre			f->ti_vlan_tag = m->m_pkthdr.ether_vtag & 0xfff;
3016153396Sscottl		} else {
3017153396Sscottl			f->ti_vlan_tag = 0;
3018153396Sscottl		}
3019153396Sscottl
3020153770Syongari		if (sc->ti_hwrev == TI_HWREV_TIGON)
3021153770Syongari			ti_mem_write(sc, TI_TX_RING_BASE + frag *
3022153770Syongari			    sizeof(txdesc), sizeof(txdesc), &txdesc);
3023153396Sscottl		TI_INC(frag, TI_TX_RING_CNT);
3024153396Sscottl	}
3025153396Sscottl
3026153982Syongari	sc->ti_tx_saved_prodidx = frag;
3027153982Syongari	/* set TI_BDFLAG_END on the last descriptor */
3028153982Syongari	frag = (frag + TI_TX_RING_CNT - 1) % TI_TX_RING_CNT;
3029153770Syongari	if (sc->ti_hwrev == TI_HWREV_TIGON) {
3030153770Syongari		txdesc.ti_flags |= TI_BDFLAG_END;
3031153982Syongari		ti_mem_write(sc, TI_TX_RING_BASE + frag * sizeof(txdesc),
3032153770Syongari		    sizeof(txdesc), &txdesc);
3033153770Syongari	} else
3034153982Syongari		sc->ti_rdata->ti_tx_ring[frag].ti_flags |= TI_BDFLAG_END;
3035153396Sscottl
3036153982Syongari	STAILQ_REMOVE_HEAD(&sc->ti_cdata.ti_txfreeq, tx_q);
3037153982Syongari	STAILQ_INSERT_TAIL(&sc->ti_cdata.ti_txbusyq, txd, tx_q);
3038153982Syongari	txd->tx_m = m;
3039153982Syongari	sc->ti_txcnt += nseg;
304045386Swpaul
3041131654Sbms	return (0);
304245386Swpaul}
304345386Swpaul
3044153770Syongaristatic void
3045153770Syongariti_start(ifp)
3046153770Syongari	struct ifnet		*ifp;
3047153770Syongari{
3048153770Syongari	struct ti_softc		*sc;
3049153770Syongari
3050153770Syongari	sc = ifp->if_softc;
3051153770Syongari	TI_LOCK(sc);
3052153770Syongari	ti_start_locked(ifp);
3053153770Syongari	TI_UNLOCK(sc);
3054153770Syongari}
3055153770Syongari
305645386Swpaul/*
305745386Swpaul * Main transmit routine. To avoid having to do mbuf copies, we put pointers
305845386Swpaul * to the mbuf data regions directly in the transmit descriptors.
305945386Swpaul */
3060102336Salfredstatic void
3061153770Syongariti_start_locked(ifp)
306245386Swpaul	struct ifnet		*ifp;
306345386Swpaul{
306445386Swpaul	struct ti_softc		*sc;
306545386Swpaul	struct mbuf		*m_head = NULL;
3066153982Syongari	int			enq = 0;
306745386Swpaul
306845386Swpaul	sc = ifp->if_softc;
306945386Swpaul
3070153982Syongari	for (; ifp->if_snd.ifq_head != NULL &&
3071153982Syongari	    sc->ti_txcnt < (TI_TX_RING_CNT - 16);) {
307245386Swpaul		IF_DEQUEUE(&ifp->if_snd, m_head);
307345386Swpaul		if (m_head == NULL)
307445386Swpaul			break;
307545386Swpaul
307645386Swpaul		/*
307758698Sjlemon		 * XXX
307858698Sjlemon		 * safety overkill.  If this is a fragmented packet chain
307958698Sjlemon		 * with delayed TCP/UDP checksums, then only encapsulate
308058698Sjlemon		 * it if we have enough descriptors to handle the entire
308158698Sjlemon		 * chain at once.
308258698Sjlemon		 * (paranoia -- may not actually be needed)
308358698Sjlemon		 */
308458698Sjlemon		if (m_head->m_flags & M_FIRSTFRAG &&
308558698Sjlemon		    m_head->m_pkthdr.csum_flags & (CSUM_DELAY_DATA)) {
308658698Sjlemon			if ((TI_TX_RING_CNT - sc->ti_txcnt) <
308758698Sjlemon			    m_head->m_pkthdr.csum_data + 16) {
308858698Sjlemon				IF_PREPEND(&ifp->if_snd, m_head);
3089148887Srwatson				ifp->if_drv_flags |= IFF_DRV_OACTIVE;
309058698Sjlemon				break;
309158698Sjlemon			}
309258698Sjlemon		}
309358698Sjlemon
309458698Sjlemon		/*
309545386Swpaul		 * Pack the data into the transmit ring. If we
309645386Swpaul		 * don't have room, set the OACTIVE flag and wait
309745386Swpaul		 * for the NIC to drain the ring.
309845386Swpaul		 */
3099153982Syongari		if (ti_encap(sc, &m_head)) {
3100153982Syongari			if (m_head == NULL)
3101153982Syongari				break;
310245386Swpaul			IF_PREPEND(&ifp->if_snd, m_head);
3103148887Srwatson			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
310445386Swpaul			break;
310545386Swpaul		}
310645386Swpaul
3107153982Syongari		enq++;
310845386Swpaul		/*
310945386Swpaul		 * If there's a BPF listener, bounce a copy of this frame
311045386Swpaul		 * to him.
311145386Swpaul		 */
3112167190Scsjp		ETHER_BPF_MTAP(ifp, m_head);
311345386Swpaul	}
311445386Swpaul
3115153982Syongari	if (enq > 0) {
3116153982Syongari		/* Transmit */
3117153982Syongari		CSR_WRITE_4(sc, TI_MB_SENDPROD_IDX, sc->ti_tx_saved_prodidx);
311845386Swpaul
3119153982Syongari		/*
3120153982Syongari		 * Set a timeout in case the chip goes out to lunch.
3121153982Syongari		 */
3122199559Sjhb		sc->ti_timer = 5;
3123153982Syongari	}
312445386Swpaul}
312545386Swpaul
3126102336Salfredstatic void
3127102336Salfredti_init(xsc)
312845386Swpaul	void			*xsc;
312945386Swpaul{
3130153770Syongari	struct ti_softc		*sc;
3131153770Syongari
3132153770Syongari	sc = xsc;
3133153770Syongari	TI_LOCK(sc);
3134153770Syongari	ti_init_locked(sc);
3135153770Syongari	TI_UNLOCK(sc);
3136153770Syongari}
3137153770Syongari
3138153770Syongaristatic void
3139153770Syongariti_init_locked(xsc)
3140153770Syongari	void			*xsc;
3141153770Syongari{
314245386Swpaul	struct ti_softc		*sc = xsc;
314345386Swpaul
314445386Swpaul	/* Cancel pending I/O and flush buffers. */
314545386Swpaul	ti_stop(sc);
314645386Swpaul
314745386Swpaul	/* Init the gen info block, ring control blocks and firmware. */
314845386Swpaul	if (ti_gibinit(sc)) {
3149162321Sglebius		device_printf(sc->ti_dev, "initialization failure\n");
315045386Swpaul		return;
315145386Swpaul	}
315245386Swpaul}
315345386Swpaul
315445386Swpaulstatic void ti_init2(sc)
315545386Swpaul	struct ti_softc		*sc;
315645386Swpaul{
315745386Swpaul	struct ti_cmd_desc	cmd;
315845386Swpaul	struct ifnet		*ifp;
3159153770Syongari	u_int8_t		*ea;
316045386Swpaul	struct ifmedia		*ifm;
316145386Swpaul	int			tmp;
316245386Swpaul
3163153770Syongari	TI_LOCK_ASSERT(sc);
3164153770Syongari
3165147256Sbrooks	ifp = sc->ti_ifp;
316645386Swpaul
316745386Swpaul	/* Specify MTU and interface index. */
3168121816Sbrooks	CSR_WRITE_4(sc, TI_GCR_IFINDEX, sc->ti_unit);
316945386Swpaul	CSR_WRITE_4(sc, TI_GCR_IFMTU, ifp->if_mtu +
3170118454Ssimokawa	    ETHER_HDR_LEN + ETHER_CRC_LEN + ETHER_VLAN_ENCAP_LEN);
317145386Swpaul	TI_DO_CMD(TI_CMD_UPDATE_GENCOM, 0, 0);
317245386Swpaul
317345386Swpaul	/* Load our MAC address. */
3174153770Syongari	ea = IF_LLADDR(sc->ti_ifp);
3175153770Syongari	CSR_WRITE_4(sc, TI_GCR_PAR0, (ea[0] << 8) | ea[1]);
3176153770Syongari	CSR_WRITE_4(sc, TI_GCR_PAR1,
3177153770Syongari	    (ea[2] << 24) | (ea[3] << 16) | (ea[4] << 8) | ea[5]);
317845386Swpaul	TI_DO_CMD(TI_CMD_SET_MAC_ADDR, 0, 0);
317945386Swpaul
318045386Swpaul	/* Enable or disable promiscuous mode as needed. */
318145386Swpaul	if (ifp->if_flags & IFF_PROMISC) {
318245386Swpaul		TI_DO_CMD(TI_CMD_SET_PROMISC_MODE, TI_CMD_CODE_PROMISC_ENB, 0);
318345386Swpaul	} else {
318445386Swpaul		TI_DO_CMD(TI_CMD_SET_PROMISC_MODE, TI_CMD_CODE_PROMISC_DIS, 0);
318545386Swpaul	}
318645386Swpaul
318745386Swpaul	/* Program multicast filter. */
318845386Swpaul	ti_setmulti(sc);
318945386Swpaul
319045386Swpaul	/*
319145386Swpaul	 * If this is a Tigon 1, we should tell the
319245386Swpaul	 * firmware to use software packet filtering.
319345386Swpaul	 */
319445386Swpaul	if (sc->ti_hwrev == TI_HWREV_TIGON) {
319545386Swpaul		TI_DO_CMD(TI_CMD_FDR_FILTERING, TI_CMD_CODE_FILT_ENB, 0);
319645386Swpaul	}
319745386Swpaul
319845386Swpaul	/* Init RX ring. */
319945386Swpaul	ti_init_rx_ring_std(sc);
320045386Swpaul
320145386Swpaul	/* Init jumbo RX ring. */
320245386Swpaul	if (ifp->if_mtu > (ETHERMTU + ETHER_HDR_LEN + ETHER_CRC_LEN))
320345386Swpaul		ti_init_rx_ring_jumbo(sc);
320445386Swpaul
320545386Swpaul	/*
320645386Swpaul	 * If this is a Tigon 2, we can also configure the
320745386Swpaul	 * mini ring.
320845386Swpaul	 */
320945386Swpaul	if (sc->ti_hwrev == TI_HWREV_TIGON_II)
321045386Swpaul		ti_init_rx_ring_mini(sc);
321145386Swpaul
321245386Swpaul	CSR_WRITE_4(sc, TI_GCR_RXRETURNCONS_IDX, 0);
321345386Swpaul	sc->ti_rx_saved_considx = 0;
321445386Swpaul
321545386Swpaul	/* Init TX ring. */
321645386Swpaul	ti_init_tx_ring(sc);
321745386Swpaul
321845386Swpaul	/* Tell firmware we're alive. */
321945386Swpaul	TI_DO_CMD(TI_CMD_HOST_STATE, TI_CMD_CODE_STACK_UP, 0);
322045386Swpaul
322145386Swpaul	/* Enable host interrupts. */
322245386Swpaul	CSR_WRITE_4(sc, TI_MB_HOSTINTR, 0);
322345386Swpaul
3224148887Srwatson	ifp->if_drv_flags |= IFF_DRV_RUNNING;
3225148887Srwatson	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
3226199559Sjhb	callout_reset(&sc->ti_watchdog, hz, ti_watchdog, sc);
322745386Swpaul
322845386Swpaul	/*
322945386Swpaul	 * Make sure to set media properly. We have to do this
323045386Swpaul	 * here since we have to issue commands in order to set
323145386Swpaul	 * the link negotiation and we can't issue commands until
323245386Swpaul	 * the firmware is running.
323345386Swpaul	 */
323445386Swpaul	ifm = &sc->ifmedia;
323545386Swpaul	tmp = ifm->ifm_media;
323645386Swpaul	ifm->ifm_media = ifm->ifm_cur->ifm_media;
323745386Swpaul	ti_ifmedia_upd(ifp);
323845386Swpaul	ifm->ifm_media = tmp;
323945386Swpaul}
324045386Swpaul
324145386Swpaul/*
324245386Swpaul * Set media options.
324345386Swpaul */
3244102336Salfredstatic int
3245102336Salfredti_ifmedia_upd(ifp)
324645386Swpaul	struct ifnet		*ifp;
324745386Swpaul{
324845386Swpaul	struct ti_softc		*sc;
324945386Swpaul	struct ifmedia		*ifm;
325045386Swpaul	struct ti_cmd_desc	cmd;
325198849Sken	u_int32_t		flowctl;
325245386Swpaul
325345386Swpaul	sc = ifp->if_softc;
325445386Swpaul	ifm = &sc->ifmedia;
325545386Swpaul
325645386Swpaul	if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
3257131654Sbms		return (EINVAL);
325845386Swpaul
325998849Sken	flowctl = 0;
326098849Sken
3261131654Sbms	switch (IFM_SUBTYPE(ifm->ifm_media)) {
326245386Swpaul	case IFM_AUTO:
326398849Sken		/*
326498849Sken		 * Transmit flow control doesn't work on the Tigon 1.
326598849Sken		 */
326698849Sken		flowctl = TI_GLNK_RX_FLOWCTL_Y;
326798849Sken
326898849Sken		/*
326998849Sken		 * Transmit flow control can also cause problems on the
327098849Sken		 * Tigon 2, apparantly with both the copper and fiber
327198849Sken		 * boards.  The symptom is that the interface will just
327298849Sken		 * hang.  This was reproduced with Alteon 180 switches.
327398849Sken		 */
327498849Sken#if 0
327598849Sken		if (sc->ti_hwrev != TI_HWREV_TIGON)
3276131652Sbms			flowctl |= TI_GLNK_TX_FLOWCTL_Y;
327798849Sken#endif
327898849Sken
327945386Swpaul		CSR_WRITE_4(sc, TI_GCR_GLINK, TI_GLNK_PREF|TI_GLNK_1000MB|
328098849Sken		    TI_GLNK_FULL_DUPLEX| flowctl |
328145386Swpaul		    TI_GLNK_AUTONEGENB|TI_GLNK_ENB);
328298849Sken
328398849Sken		flowctl = TI_LNK_RX_FLOWCTL_Y;
328498849Sken#if 0
328598849Sken		if (sc->ti_hwrev != TI_HWREV_TIGON)
328698849Sken			flowctl |= TI_LNK_TX_FLOWCTL_Y;
328798849Sken#endif
328898849Sken
328945386Swpaul		CSR_WRITE_4(sc, TI_GCR_LINK, TI_LNK_100MB|TI_LNK_10MB|
329098849Sken		    TI_LNK_FULL_DUPLEX|TI_LNK_HALF_DUPLEX| flowctl |
329145386Swpaul		    TI_LNK_AUTONEGENB|TI_LNK_ENB);
329245386Swpaul		TI_DO_CMD(TI_CMD_LINK_NEGOTIATION,
329345386Swpaul		    TI_CMD_CODE_NEGOTIATE_BOTH, 0);
329445386Swpaul		break;
329545386Swpaul	case IFM_1000_SX:
329695673Sphk	case IFM_1000_T:
329798849Sken		flowctl = TI_GLNK_RX_FLOWCTL_Y;
329898849Sken#if 0
329998849Sken		if (sc->ti_hwrev != TI_HWREV_TIGON)
3300131652Sbms			flowctl |= TI_GLNK_TX_FLOWCTL_Y;
330198849Sken#endif
330298849Sken
330345386Swpaul		CSR_WRITE_4(sc, TI_GCR_GLINK, TI_GLNK_PREF|TI_GLNK_1000MB|
330498849Sken		    flowctl |TI_GLNK_ENB);
330545386Swpaul		CSR_WRITE_4(sc, TI_GCR_LINK, 0);
330663699Swpaul		if ((ifm->ifm_media & IFM_GMASK) == IFM_FDX) {
330763699Swpaul			TI_SETBIT(sc, TI_GCR_GLINK, TI_GLNK_FULL_DUPLEX);
330863699Swpaul		}
330945386Swpaul		TI_DO_CMD(TI_CMD_LINK_NEGOTIATION,
331045386Swpaul		    TI_CMD_CODE_NEGOTIATE_GIGABIT, 0);
331145386Swpaul		break;
331245386Swpaul	case IFM_100_FX:
331345386Swpaul	case IFM_10_FL:
331463699Swpaul	case IFM_100_TX:
331563699Swpaul	case IFM_10_T:
331698849Sken		flowctl = TI_LNK_RX_FLOWCTL_Y;
331798849Sken#if 0
331898849Sken		if (sc->ti_hwrev != TI_HWREV_TIGON)
331998849Sken			flowctl |= TI_LNK_TX_FLOWCTL_Y;
332098849Sken#endif
332198849Sken
332245386Swpaul		CSR_WRITE_4(sc, TI_GCR_GLINK, 0);
332398849Sken		CSR_WRITE_4(sc, TI_GCR_LINK, TI_LNK_ENB|TI_LNK_PREF|flowctl);
332463699Swpaul		if (IFM_SUBTYPE(ifm->ifm_media) == IFM_100_FX ||
332563699Swpaul		    IFM_SUBTYPE(ifm->ifm_media) == IFM_100_TX) {
332645386Swpaul			TI_SETBIT(sc, TI_GCR_LINK, TI_LNK_100MB);
332745386Swpaul		} else {
332845386Swpaul			TI_SETBIT(sc, TI_GCR_LINK, TI_LNK_10MB);
332945386Swpaul		}
333045386Swpaul		if ((ifm->ifm_media & IFM_GMASK) == IFM_FDX) {
333145386Swpaul			TI_SETBIT(sc, TI_GCR_LINK, TI_LNK_FULL_DUPLEX);
333245386Swpaul		} else {
333345386Swpaul			TI_SETBIT(sc, TI_GCR_LINK, TI_LNK_HALF_DUPLEX);
333445386Swpaul		}
333545386Swpaul		TI_DO_CMD(TI_CMD_LINK_NEGOTIATION,
333645386Swpaul		    TI_CMD_CODE_NEGOTIATE_10_100, 0);
333745386Swpaul		break;
333845386Swpaul	}
333945386Swpaul
3340131654Sbms	return (0);
334145386Swpaul}
334245386Swpaul
334345386Swpaul/*
334445386Swpaul * Report current media status.
334545386Swpaul */
3346102336Salfredstatic void
3347102336Salfredti_ifmedia_sts(ifp, ifmr)
334845386Swpaul	struct ifnet		*ifp;
334945386Swpaul	struct ifmediareq	*ifmr;
335045386Swpaul{
335145386Swpaul	struct ti_softc		*sc;
335263699Swpaul	u_int32_t		media = 0;
335345386Swpaul
335445386Swpaul	sc = ifp->if_softc;
335545386Swpaul
335645386Swpaul	ifmr->ifm_status = IFM_AVALID;
335745386Swpaul	ifmr->ifm_active = IFM_ETHER;
335845386Swpaul
335945386Swpaul	if (sc->ti_linkstat == TI_EV_CODE_LINK_DOWN)
336045386Swpaul		return;
336145386Swpaul
336245386Swpaul	ifmr->ifm_status |= IFM_ACTIVE;
336345386Swpaul
336463699Swpaul	if (sc->ti_linkstat == TI_EV_CODE_GIG_LINK_UP) {
336563699Swpaul		media = CSR_READ_4(sc, TI_GCR_GLINK_STAT);
336663699Swpaul		if (sc->ti_copper)
336795673Sphk			ifmr->ifm_active |= IFM_1000_T;
336863699Swpaul		else
336963699Swpaul			ifmr->ifm_active |= IFM_1000_SX;
337063699Swpaul		if (media & TI_GLNK_FULL_DUPLEX)
337163699Swpaul			ifmr->ifm_active |= IFM_FDX;
337263699Swpaul		else
337363699Swpaul			ifmr->ifm_active |= IFM_HDX;
337463699Swpaul	} else if (sc->ti_linkstat == TI_EV_CODE_LINK_UP) {
337545386Swpaul		media = CSR_READ_4(sc, TI_GCR_LINK_STAT);
337663699Swpaul		if (sc->ti_copper) {
337763699Swpaul			if (media & TI_LNK_100MB)
337863699Swpaul				ifmr->ifm_active |= IFM_100_TX;
337963699Swpaul			if (media & TI_LNK_10MB)
338063699Swpaul				ifmr->ifm_active |= IFM_10_T;
338163699Swpaul		} else {
338263699Swpaul			if (media & TI_LNK_100MB)
338363699Swpaul				ifmr->ifm_active |= IFM_100_FX;
338463699Swpaul			if (media & TI_LNK_10MB)
338563699Swpaul				ifmr->ifm_active |= IFM_10_FL;
338663699Swpaul		}
338745386Swpaul		if (media & TI_LNK_FULL_DUPLEX)
338845386Swpaul			ifmr->ifm_active |= IFM_FDX;
338945386Swpaul		if (media & TI_LNK_HALF_DUPLEX)
339045386Swpaul			ifmr->ifm_active |= IFM_HDX;
339145386Swpaul	}
339245386Swpaul}
339345386Swpaul
3394102336Salfredstatic int
3395102336Salfredti_ioctl(ifp, command, data)
339645386Swpaul	struct ifnet		*ifp;
339745386Swpaul	u_long			command;
339845386Swpaul	caddr_t			data;
339945386Swpaul{
340045386Swpaul	struct ti_softc		*sc = ifp->if_softc;
340145386Swpaul	struct ifreq		*ifr = (struct ifreq *) data;
340283630Sjlemon	int			mask, error = 0;
340345386Swpaul	struct ti_cmd_desc	cmd;
340445386Swpaul
3405131654Sbms	switch (command) {
340645386Swpaul	case SIOCSIFMTU:
3407153770Syongari		TI_LOCK(sc);
340845386Swpaul		if (ifr->ifr_mtu > TI_JUMBO_MTU)
340945386Swpaul			error = EINVAL;
341045386Swpaul		else {
341145386Swpaul			ifp->if_mtu = ifr->ifr_mtu;
3412153770Syongari			ti_init_locked(sc);
341345386Swpaul		}
3414153770Syongari		TI_UNLOCK(sc);
341545386Swpaul		break;
341645386Swpaul	case SIOCSIFFLAGS:
3417153770Syongari		TI_LOCK(sc);
341845386Swpaul		if (ifp->if_flags & IFF_UP) {
341945386Swpaul			/*
342045386Swpaul			 * If only the state of the PROMISC flag changed,
342145386Swpaul			 * then just use the 'set promisc mode' command
342245386Swpaul			 * instead of reinitializing the entire NIC. Doing
342345386Swpaul			 * a full re-init means reloading the firmware and
342445386Swpaul			 * waiting for it to start up, which may take a
342545386Swpaul			 * second or two.
342645386Swpaul			 */
3427148887Srwatson			if (ifp->if_drv_flags & IFF_DRV_RUNNING &&
342845386Swpaul			    ifp->if_flags & IFF_PROMISC &&
342945386Swpaul			    !(sc->ti_if_flags & IFF_PROMISC)) {
343045386Swpaul				TI_DO_CMD(TI_CMD_SET_PROMISC_MODE,
343145386Swpaul				    TI_CMD_CODE_PROMISC_ENB, 0);
3432148887Srwatson			} else if (ifp->if_drv_flags & IFF_DRV_RUNNING &&
343345386Swpaul			    !(ifp->if_flags & IFF_PROMISC) &&
343445386Swpaul			    sc->ti_if_flags & IFF_PROMISC) {
343545386Swpaul				TI_DO_CMD(TI_CMD_SET_PROMISC_MODE,
343645386Swpaul				    TI_CMD_CODE_PROMISC_DIS, 0);
343745386Swpaul			} else
3438153770Syongari				ti_init_locked(sc);
343945386Swpaul		} else {
3440148887Srwatson			if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
344145386Swpaul				ti_stop(sc);
344245386Swpaul			}
344345386Swpaul		}
344445386Swpaul		sc->ti_if_flags = ifp->if_flags;
3445153770Syongari		TI_UNLOCK(sc);
344645386Swpaul		break;
344745386Swpaul	case SIOCADDMULTI:
344845386Swpaul	case SIOCDELMULTI:
3449153770Syongari		TI_LOCK(sc);
3450153770Syongari		if (ifp->if_drv_flags & IFF_DRV_RUNNING)
345145386Swpaul			ti_setmulti(sc);
3452153770Syongari		TI_UNLOCK(sc);
345345386Swpaul		break;
345445386Swpaul	case SIOCSIFMEDIA:
345545386Swpaul	case SIOCGIFMEDIA:
345645386Swpaul		error = ifmedia_ioctl(ifp, ifr, &sc->ifmedia, command);
345745386Swpaul		break;
345883630Sjlemon	case SIOCSIFCAP:
3459153770Syongari		TI_LOCK(sc);
346083630Sjlemon		mask = ifr->ifr_reqcap ^ ifp->if_capenable;
346183630Sjlemon		if (mask & IFCAP_HWCSUM) {
346283630Sjlemon			if (IFCAP_HWCSUM & ifp->if_capenable)
346383630Sjlemon				ifp->if_capenable &= ~IFCAP_HWCSUM;
3464131655Sbms			else
3465131655Sbms				ifp->if_capenable |= IFCAP_HWCSUM;
3466148887Srwatson			if (ifp->if_drv_flags & IFF_DRV_RUNNING)
3467153770Syongari				ti_init_locked(sc);
3468131655Sbms		}
3469153770Syongari		TI_UNLOCK(sc);
347083630Sjlemon		break;
347145386Swpaul	default:
3472106936Ssam		error = ether_ioctl(ifp, command, data);
347345386Swpaul		break;
347445386Swpaul	}
347545386Swpaul
3476131654Sbms	return (error);
347745386Swpaul}
347845386Swpaul
347998849Skenstatic int
3480130585Sphkti_open(struct cdev *dev, int flags, int fmt, struct thread *td)
348198849Sken{
348298849Sken	struct ti_softc *sc;
348398849Sken
3484120980Sphk	sc = dev->si_drv1;
348598849Sken	if (sc == NULL)
3486131654Sbms		return (ENODEV);
348798849Sken
348898849Sken	TI_LOCK(sc);
348998849Sken	sc->ti_flags |= TI_FLAG_DEBUGING;
349098849Sken	TI_UNLOCK(sc);
349198849Sken
3492131654Sbms	return (0);
349398849Sken}
349498849Sken
349598849Skenstatic int
3496130585Sphkti_close(struct cdev *dev, int flag, int fmt, struct thread *td)
349798849Sken{
349898849Sken	struct ti_softc *sc;
349998849Sken
3500120980Sphk	sc = dev->si_drv1;
350198849Sken	if (sc == NULL)
3502131654Sbms		return (ENODEV);
350398849Sken
350498849Sken	TI_LOCK(sc);
350598849Sken	sc->ti_flags &= ~TI_FLAG_DEBUGING;
350698849Sken	TI_UNLOCK(sc);
350798849Sken
3508131654Sbms	return (0);
350998849Sken}
351098849Sken
351198849Sken/*
351298849Sken * This ioctl routine goes along with the Tigon character device.
351398849Sken */
3514131652Sbmsstatic int
3515150719Sjhbti_ioctl2(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
3516150719Sjhb    struct thread *td)
351798849Sken{
3518120980Sphk	int error;
351998849Sken	struct ti_softc *sc;
352098849Sken
3521120980Sphk	sc = dev->si_drv1;
352298849Sken	if (sc == NULL)
3523131654Sbms		return (ENODEV);
352498849Sken
352598849Sken	error = 0;
352698849Sken
3527131654Sbms	switch (cmd) {
352898849Sken	case TIIOCGETSTATS:
352998849Sken	{
353098849Sken		struct ti_stats *outstats;
353198849Sken
353298849Sken		outstats = (struct ti_stats *)addr;
353398849Sken
3534153281Sscottl		TI_LOCK(sc);
353598849Sken		bcopy(&sc->ti_rdata->ti_info.ti_stats, outstats,
353698849Sken		      sizeof(struct ti_stats));
3537153281Sscottl		TI_UNLOCK(sc);
353898849Sken		break;
353998849Sken	}
354098849Sken	case TIIOCGETPARAMS:
354198849Sken	{
354298849Sken		struct ti_params	*params;
354398849Sken
354498849Sken		params = (struct ti_params *)addr;
354598849Sken
3546153281Sscottl		TI_LOCK(sc);
354798849Sken		params->ti_stat_ticks = sc->ti_stat_ticks;
354898849Sken		params->ti_rx_coal_ticks = sc->ti_rx_coal_ticks;
354998849Sken		params->ti_tx_coal_ticks = sc->ti_tx_coal_ticks;
355098849Sken		params->ti_rx_max_coal_bds = sc->ti_rx_max_coal_bds;
355198849Sken		params->ti_tx_max_coal_bds = sc->ti_tx_max_coal_bds;
355298849Sken		params->ti_tx_buf_ratio = sc->ti_tx_buf_ratio;
355398849Sken		params->param_mask = TI_PARAM_ALL;
3554153281Sscottl		TI_UNLOCK(sc);
355598849Sken
355698849Sken		error = 0;
355798849Sken
355898849Sken		break;
355998849Sken	}
356098849Sken	case TIIOCSETPARAMS:
356198849Sken	{
356298849Sken		struct ti_params *params;
356398849Sken
356498849Sken		params = (struct ti_params *)addr;
356598849Sken
3566153281Sscottl		TI_LOCK(sc);
356798849Sken		if (params->param_mask & TI_PARAM_STAT_TICKS) {
356898849Sken			sc->ti_stat_ticks = params->ti_stat_ticks;
356998849Sken			CSR_WRITE_4(sc, TI_GCR_STAT_TICKS, sc->ti_stat_ticks);
357098849Sken		}
357198849Sken
357298849Sken		if (params->param_mask & TI_PARAM_RX_COAL_TICKS) {
357398849Sken			sc->ti_rx_coal_ticks = params->ti_rx_coal_ticks;
357498849Sken			CSR_WRITE_4(sc, TI_GCR_RX_COAL_TICKS,
357598849Sken				    sc->ti_rx_coal_ticks);
357698849Sken		}
357798849Sken
357898849Sken		if (params->param_mask & TI_PARAM_TX_COAL_TICKS) {
357998849Sken			sc->ti_tx_coal_ticks = params->ti_tx_coal_ticks;
358098849Sken			CSR_WRITE_4(sc, TI_GCR_TX_COAL_TICKS,
358198849Sken				    sc->ti_tx_coal_ticks);
358298849Sken		}
358398849Sken
358498849Sken		if (params->param_mask & TI_PARAM_RX_COAL_BDS) {
358598849Sken			sc->ti_rx_max_coal_bds = params->ti_rx_max_coal_bds;
358698849Sken			CSR_WRITE_4(sc, TI_GCR_RX_MAX_COAL_BD,
358798849Sken				    sc->ti_rx_max_coal_bds);
358898849Sken		}
358998849Sken
359098849Sken		if (params->param_mask & TI_PARAM_TX_COAL_BDS) {
359198849Sken			sc->ti_tx_max_coal_bds = params->ti_tx_max_coal_bds;
359298849Sken			CSR_WRITE_4(sc, TI_GCR_TX_MAX_COAL_BD,
359398849Sken				    sc->ti_tx_max_coal_bds);
359498849Sken		}
359598849Sken
359698849Sken		if (params->param_mask & TI_PARAM_TX_BUF_RATIO) {
359798849Sken			sc->ti_tx_buf_ratio = params->ti_tx_buf_ratio;
359898849Sken			CSR_WRITE_4(sc, TI_GCR_TX_BUFFER_RATIO,
359998849Sken				    sc->ti_tx_buf_ratio);
360098849Sken		}
3601153281Sscottl		TI_UNLOCK(sc);
360298849Sken
360398849Sken		error = 0;
360498849Sken
360598849Sken		break;
360698849Sken	}
360798849Sken	case TIIOCSETTRACE: {
360898849Sken		ti_trace_type	trace_type;
360998849Sken
361098849Sken		trace_type = *(ti_trace_type *)addr;
361198849Sken
361298849Sken		/*
361398849Sken		 * Set tracing to whatever the user asked for.  Setting
361498849Sken		 * this register to 0 should have the effect of disabling
361598849Sken		 * tracing.
361698849Sken		 */
361798849Sken		CSR_WRITE_4(sc, TI_GCR_NIC_TRACING, trace_type);
361898849Sken
361998849Sken		error = 0;
362098849Sken
362198849Sken		break;
362298849Sken	}
362398849Sken	case TIIOCGETTRACE: {
362498849Sken		struct ti_trace_buf	*trace_buf;
362598849Sken		u_int32_t		trace_start, cur_trace_ptr, trace_len;
362698849Sken
362798849Sken		trace_buf = (struct ti_trace_buf *)addr;
362898849Sken
3629153281Sscottl		TI_LOCK(sc);
363098849Sken		trace_start = CSR_READ_4(sc, TI_GCR_NICTRACE_START);
363198849Sken		cur_trace_ptr = CSR_READ_4(sc, TI_GCR_NICTRACE_PTR);
363298849Sken		trace_len = CSR_READ_4(sc, TI_GCR_NICTRACE_LEN);
363398849Sken
363498849Sken#if 0
3635150719Sjhb		if_printf(sc->ti_ifp, "trace_start = %#x, cur_trace_ptr = %#x, "
3636150719Sjhb		       "trace_len = %d\n", trace_start,
363798849Sken		       cur_trace_ptr, trace_len);
3638150719Sjhb		if_printf(sc->ti_ifp, "trace_buf->buf_len = %d\n",
363998849Sken		       trace_buf->buf_len);
364098849Sken#endif
364198849Sken
364298849Sken		error = ti_copy_mem(sc, trace_start, min(trace_len,
364398849Sken				    trace_buf->buf_len),
364498849Sken				    (caddr_t)trace_buf->buf, 1, 1);
364598849Sken
364698849Sken		if (error == 0) {
364798849Sken			trace_buf->fill_len = min(trace_len,
364898849Sken						  trace_buf->buf_len);
364998849Sken			if (cur_trace_ptr < trace_start)
365098849Sken				trace_buf->cur_trace_ptr =
365198849Sken					trace_start - cur_trace_ptr;
365298849Sken			else
365398849Sken				trace_buf->cur_trace_ptr =
365498849Sken					cur_trace_ptr - trace_start;
365598849Sken		} else
365698849Sken			trace_buf->fill_len = 0;
3657153281Sscottl		TI_UNLOCK(sc);
365898849Sken
365998849Sken		break;
366098849Sken	}
366198849Sken
366298849Sken	/*
366398849Sken	 * For debugging, five ioctls are needed:
366498849Sken	 * ALT_ATTACH
366598849Sken	 * ALT_READ_TG_REG
366698849Sken	 * ALT_WRITE_TG_REG
366798849Sken	 * ALT_READ_TG_MEM
366898849Sken	 * ALT_WRITE_TG_MEM
366998849Sken	 */
367098849Sken	case ALT_ATTACH:
367198849Sken		/*
3672131652Sbms		 * From what I can tell, Alteon's Solaris Tigon driver
367398849Sken		 * only has one character device, so you have to attach
367498849Sken		 * to the Tigon board you're interested in.  This seems
367598849Sken		 * like a not-so-good way to do things, since unless you
367698849Sken		 * subsequently specify the unit number of the device
3677177626Sbrueffer		 * you're interested in every ioctl, you'll only be
367898849Sken		 * able to debug one board at a time.
367998849Sken		 */
368098849Sken		error = 0;
368198849Sken		break;
368298849Sken	case ALT_READ_TG_MEM:
368398849Sken	case ALT_WRITE_TG_MEM:
368498849Sken	{
368598849Sken		struct tg_mem *mem_param;
368698849Sken		u_int32_t sram_end, scratch_end;
368798849Sken
368898849Sken		mem_param = (struct tg_mem *)addr;
368998849Sken
369098849Sken		if (sc->ti_hwrev == TI_HWREV_TIGON) {
369198849Sken			sram_end = TI_END_SRAM_I;
369298849Sken			scratch_end = TI_END_SCRATCH_I;
369398849Sken		} else {
369498849Sken			sram_end = TI_END_SRAM_II;
369598849Sken			scratch_end = TI_END_SCRATCH_II;
369698849Sken		}
369798849Sken
369898849Sken		/*
369998849Sken		 * For now, we'll only handle accessing regular SRAM,
370098849Sken		 * nothing else.
370198849Sken		 */
3702153281Sscottl		TI_LOCK(sc);
370398849Sken		if ((mem_param->tgAddr >= TI_BEG_SRAM)
370498849Sken		 && ((mem_param->tgAddr + mem_param->len) <= sram_end)) {
370598849Sken			/*
370698849Sken			 * In this instance, we always copy to/from user
370798849Sken			 * space, so the user space argument is set to 1.
370898849Sken			 */
370998849Sken			error = ti_copy_mem(sc, mem_param->tgAddr,
371098849Sken					    mem_param->len,
371198849Sken					    mem_param->userAddr, 1,
371298849Sken					    (cmd == ALT_READ_TG_MEM) ? 1 : 0);
371398849Sken		} else if ((mem_param->tgAddr >= TI_BEG_SCRATCH)
371498849Sken			&& (mem_param->tgAddr <= scratch_end)) {
371598849Sken			error = ti_copy_scratch(sc, mem_param->tgAddr,
371698849Sken						mem_param->len,
371798849Sken						mem_param->userAddr, 1,
371898849Sken						(cmd == ALT_READ_TG_MEM) ?
371998849Sken						1 : 0, TI_PROCESSOR_A);
372098849Sken		} else if ((mem_param->tgAddr >= TI_BEG_SCRATCH_B_DEBUG)
372198849Sken			&& (mem_param->tgAddr <= TI_BEG_SCRATCH_B_DEBUG)) {
372298849Sken			if (sc->ti_hwrev == TI_HWREV_TIGON) {
3723150719Sjhb				if_printf(sc->ti_ifp,
3724150719Sjhb				    "invalid memory range for Tigon I\n");
372598849Sken				error = EINVAL;
372698849Sken				break;
372798849Sken			}
3728131652Sbms			error = ti_copy_scratch(sc, mem_param->tgAddr -
372998849Sken						TI_SCRATCH_DEBUG_OFF,
373098849Sken						mem_param->len,
373198849Sken						mem_param->userAddr, 1,
373298849Sken						(cmd == ALT_READ_TG_MEM) ?
373398849Sken						1 : 0, TI_PROCESSOR_B);
373498849Sken		} else {
3735150719Sjhb			if_printf(sc->ti_ifp, "memory address %#x len %d is "
3736150719Sjhb			        "out of supported range\n",
373798849Sken			        mem_param->tgAddr, mem_param->len);
373898849Sken			error = EINVAL;
373998849Sken		}
3740153281Sscottl		TI_UNLOCK(sc);
374198849Sken
374298849Sken		break;
374398849Sken	}
374498849Sken	case ALT_READ_TG_REG:
374598849Sken	case ALT_WRITE_TG_REG:
374698849Sken	{
374798849Sken		struct tg_reg	*regs;
374898849Sken		u_int32_t	tmpval;
374998849Sken
375098849Sken		regs = (struct tg_reg *)addr;
375198849Sken
375298849Sken		/*
375398849Sken		 * Make sure the address in question isn't out of range.
375498849Sken		 */
375598849Sken		if (regs->addr > TI_REG_MAX) {
375698849Sken			error = EINVAL;
375798849Sken			break;
375898849Sken		}
3759153281Sscottl		TI_LOCK(sc);
376098849Sken		if (cmd == ALT_READ_TG_REG) {
376198849Sken			bus_space_read_region_4(sc->ti_btag, sc->ti_bhandle,
376298849Sken						regs->addr, &tmpval, 1);
376398849Sken			regs->data = ntohl(tmpval);
376498849Sken#if 0
376598849Sken			if ((regs->addr == TI_CPU_STATE)
376698849Sken			 || (regs->addr == TI_CPU_CTL_B)) {
3767150719Sjhb				if_printf(sc->ti_ifp, "register %#x = %#x\n",
3768150719Sjhb				       regs->addr, tmpval);
376998849Sken			}
377098849Sken#endif
377198849Sken		} else {
377298849Sken			tmpval = htonl(regs->data);
377398849Sken			bus_space_write_region_4(sc->ti_btag, sc->ti_bhandle,
377498849Sken						 regs->addr, &tmpval, 1);
377598849Sken		}
3776153281Sscottl		TI_UNLOCK(sc);
377798849Sken
377898849Sken		break;
377998849Sken	}
378098849Sken	default:
378198849Sken		error = ENOTTY;
378298849Sken		break;
378398849Sken	}
3784131654Sbms	return (error);
378598849Sken}
378698849Sken
3787102336Salfredstatic void
3788199559Sjhbti_watchdog(void *arg)
378945386Swpaul{
379045386Swpaul	struct ti_softc		*sc;
3791199559Sjhb	struct ifnet		*ifp;
379245386Swpaul
3793199559Sjhb	sc = arg;
3794199559Sjhb	TI_LOCK_ASSERT(sc);
3795199559Sjhb	callout_reset(&sc->ti_watchdog, hz, ti_watchdog, sc);
3796199559Sjhb	if (sc->ti_timer == 0 || --sc->ti_timer > 0)
3797199559Sjhb		return;
379845386Swpaul
379998849Sken	/*
380098849Sken	 * When we're debugging, the chip is often stopped for long periods
380198849Sken	 * of time, and that would normally cause the watchdog timer to fire.
380298849Sken	 * Since that impedes debugging, we don't want to do that.
380398849Sken	 */
3804199559Sjhb	if (sc->ti_flags & TI_FLAG_DEBUGING)
380598849Sken		return;
380698849Sken
3807199559Sjhb	ifp = sc->ti_ifp;
3808150719Sjhb	if_printf(ifp, "watchdog timeout -- resetting\n");
380945386Swpaul	ti_stop(sc);
3810153770Syongari	ti_init_locked(sc);
381145386Swpaul
381245386Swpaul	ifp->if_oerrors++;
381345386Swpaul}
381445386Swpaul
381545386Swpaul/*
381645386Swpaul * Stop the adapter and free any mbufs allocated to the
381745386Swpaul * RX and TX lists.
381845386Swpaul */
3819102336Salfredstatic void
3820102336Salfredti_stop(sc)
382145386Swpaul	struct ti_softc		*sc;
382245386Swpaul{
382345386Swpaul	struct ifnet		*ifp;
382445386Swpaul	struct ti_cmd_desc	cmd;
382545386Swpaul
3826153770Syongari	TI_LOCK_ASSERT(sc);
382767087Swpaul
3828147256Sbrooks	ifp = sc->ti_ifp;
382945386Swpaul
383045386Swpaul	/* Disable host interrupts. */
383145386Swpaul	CSR_WRITE_4(sc, TI_MB_HOSTINTR, 1);
383245386Swpaul	/*
383345386Swpaul	 * Tell firmware we're shutting down.
383445386Swpaul	 */
383545386Swpaul	TI_DO_CMD(TI_CMD_HOST_STATE, TI_CMD_CODE_STACK_DOWN, 0);
383645386Swpaul
383745386Swpaul	/* Halt and reinitialize. */
3838153770Syongari	if (ti_chipinit(sc) != 0)
3839153770Syongari		return;
3840153770Syongari	ti_mem_zero(sc, 0x2000, 0x100000 - 0x2000);
3841153770Syongari	if (ti_chipinit(sc) != 0)
3842153770Syongari		return;
384345386Swpaul
384445386Swpaul	/* Free the RX lists. */
384545386Swpaul	ti_free_rx_ring_std(sc);
384645386Swpaul
384745386Swpaul	/* Free jumbo RX list. */
384845386Swpaul	ti_free_rx_ring_jumbo(sc);
384945386Swpaul
385045386Swpaul	/* Free mini RX list. */
385145386Swpaul	ti_free_rx_ring_mini(sc);
385245386Swpaul
385345386Swpaul	/* Free TX buffers. */
385445386Swpaul	ti_free_tx_ring(sc);
385545386Swpaul
385645386Swpaul	sc->ti_ev_prodidx.ti_idx = 0;
385745386Swpaul	sc->ti_return_prodidx.ti_idx = 0;
385845386Swpaul	sc->ti_tx_considx.ti_idx = 0;
385945386Swpaul	sc->ti_tx_saved_considx = TI_TXCONS_UNSET;
386045386Swpaul
3861148887Srwatson	ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
3862199559Sjhb	callout_stop(&sc->ti_watchdog);
386345386Swpaul}
386445386Swpaul
386545386Swpaul/*
386645386Swpaul * Stop all chip I/O so that the kernel's probe routines don't
386745386Swpaul * get confused by errant DMAs when rebooting.
386845386Swpaul */
3869173839Syongaristatic int
3870102336Salfredti_shutdown(dev)
387149011Swpaul	device_t		dev;
387245386Swpaul{
387345386Swpaul	struct ti_softc		*sc;
387445386Swpaul
387549011Swpaul	sc = device_get_softc(dev);
387667087Swpaul	TI_LOCK(sc);
387745386Swpaul	ti_chipinit(sc);
387867087Swpaul	TI_UNLOCK(sc);
3879173839Syongari
3880173839Syongari	return (0);
388145386Swpaul}
3882