if_ti.c revision 112930
197403Sobrien/*
297403Sobrien * Copyright (c) 1997, 1998, 1999
3169691Skan *	Bill Paul <wpaul@ctr.columbia.edu>.  All rights reserved.
4169691Skan *
597403Sobrien * Redistribution and use in source and binary forms, with or without
697403Sobrien * modification, are permitted provided that the following conditions
797403Sobrien * are met:
897403Sobrien * 1. Redistributions of source code must retain the above copyright
997403Sobrien *    notice, this list of conditions and the following disclaimer.
1097403Sobrien * 2. Redistributions in binary form must reproduce the above copyright
1197403Sobrien *    notice, this list of conditions and the following disclaimer in the
1297403Sobrien *    documentation and/or other materials provided with the distribution.
1397403Sobrien * 3. All advertising materials mentioning features or use of this software
1497403Sobrien *    must display the following acknowledgement:
1597403Sobrien *	This product includes software developed by Bill Paul.
1697403Sobrien * 4. Neither the name of the author nor the names of any co-contributors
1797403Sobrien *    may be used to endorse or promote products derived from this software
1897403Sobrien *    without specific prior written permission.
19169691Skan *
2097403Sobrien * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
2197403Sobrien * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2297403Sobrien * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2397403Sobrien * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
2497403Sobrien * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2597403Sobrien * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2697403Sobrien * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2797403Sobrien * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2897403Sobrien * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2997403Sobrien * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
3097403Sobrien * THE POSSIBILITY OF SUCH DAMAGE.
3197403Sobrien *
3297403Sobrien * $FreeBSD: head/sys/dev/ti/if_ti.c 112930 2003-04-01 08:57:28Z phk $
3397403Sobrien */
3497403Sobrien
3597403Sobrien/*
3697403Sobrien * Alteon Networks Tigon PCI gigabit ethernet driver for FreeBSD.
3797403Sobrien * Manuals, sample driver and firmware source kits are available
3897403Sobrien * from http://www.alteon.com/support/openkits.
3997403Sobrien *
4097403Sobrien * Written by Bill Paul <wpaul@ctr.columbia.edu>
4197403Sobrien * Electrical Engineering Department
4297403Sobrien * Columbia University, New York City
4397403Sobrien */
4497403Sobrien
4597403Sobrien/*
4697403Sobrien * The Alteon Networks Tigon chip contains an embedded R4000 CPU,
4797403Sobrien * gigabit MAC, dual DMA channels and a PCI interface unit. NICs
4897403Sobrien * using the Tigon may have anywhere from 512K to 2MB of SRAM. The
4997403Sobrien * Tigon supports hardware IP, TCP and UCP checksumming, multicast
5097403Sobrien * filtering and jumbo (9014 byte) frames. The hardware is largely
5197403Sobrien * controlled by firmware, which must be loaded into the NIC during
5297403Sobrien * initialization.
5397403Sobrien *
5497403Sobrien * The Tigon 2 contains 2 R4000 CPUs and requires a newer firmware
5597403Sobrien * revision, which supports new features such as extended commands,
5697403Sobrien * extended jumbo receive ring desciptors and a mini receive ring.
5797403Sobrien *
5897403Sobrien * Alteon Networks is to be commended for releasing such a vast amount
5997403Sobrien * of development material for the Tigon NIC without requiring an NDA
6097403Sobrien * (although they really should have done it a long time ago). With
6197403Sobrien * any luck, the other vendors will finally wise up and follow Alteon's
6297403Sobrien * stellar example.
6397403Sobrien *
64132720Skan * The firmware for the Tigon 1 and 2 NICs is compiled directly into
65132720Skan * this driver by #including it as a C header file. This bloats the
6697403Sobrien * driver somewhat, but it's the easiest method considering that the
67259694Spfg * driver code and firmware code need to be kept in sync. The source
68259694Spfg * for the firmware is not provided with the FreeBSD distribution since
6997403Sobrien * compiling it requires a GNU toolchain targeted for mips-sgi-irix5.3.
70132720Skan *
7197403Sobrien * The following people deserve special thanks:
7297403Sobrien * - Terry Murphy of 3Com, for providing a 3c985 Tigon 1 board
73132720Skan *   for testing
7497403Sobrien * - Raymond Lee of Netgear, for providing a pair of Netgear
75169691Skan *   GA620 Tigon 2 boards for testing
76169691Skan * - Ulf Zimmermann, for bringing the GA260 to my attention and
77132720Skan *   convincing me to write this driver.
78132720Skan * - Andrew Gallatin for providing FreeBSD/Alpha support.
79132720Skan */
80132720Skan
81132720Skan#include "opt_ti.h"
82132720Skan
83132720Skan#include <sys/param.h>
84132720Skan#include <sys/systm.h>
85132720Skan#include <sys/sockio.h>
86132720Skan#include <sys/mbuf.h>
87132720Skan#include <sys/malloc.h>
88132720Skan#include <sys/kernel.h>
89132720Skan#include <sys/socket.h>
90132720Skan#include <sys/queue.h>
91132720Skan#include <sys/conf.h>
9297403Sobrien
93132720Skan#include <net/if.h>
94132720Skan#include <net/if_arp.h>
9597403Sobrien#include <net/ethernet.h>
9697403Sobrien#include <net/if_dl.h>
9797403Sobrien#include <net/if_media.h>
98132720Skan#include <net/if_types.h>
99132720Skan#include <net/if_vlan_var.h>
100132720Skan
101132720Skan#include <net/bpf.h>
102132720Skan
103132720Skan#include <netinet/in_systm.h>
104132720Skan#include <netinet/in.h>
105132720Skan#include <netinet/ip.h>
10697403Sobrien
10797403Sobrien#include <vm/vm.h>              /* for vtophys */
10897403Sobrien#include <vm/pmap.h>            /* for vtophys */
10997403Sobrien#include <machine/bus_memio.h>
11097403Sobrien#include <machine/bus.h>
11197403Sobrien#include <machine/resource.h>
112132720Skan#include <sys/bus.h>
113132720Skan#include <sys/rman.h>
114132720Skan
115132720Skan/* #define TI_PRIVATE_JUMBOS */
116132720Skan
117132720Skan#if !defined(TI_PRIVATE_JUMBOS)
118132720Skan#include <sys/sockio.h>
119132720Skan#include <sys/uio.h>
12097403Sobrien#include <sys/lock.h>
12197403Sobrien#include <vm/vm_extern.h>
12297403Sobrien#include <vm/pmap.h>
12397403Sobrien#include <vm/vm_map.h>
12497403Sobrien#include <vm/vm_map.h>
125132720Skan#include <vm/vm_param.h>
126132720Skan#include <vm/vm_pageout.h>
127132720Skan#include <sys/vmmeter.h>
128132720Skan#include <vm/vm_page.h>
129132720Skan#include <vm/vm_object.h>
130132720Skan#include <vm/vm_kern.h>
131132720Skan#include <sys/proc.h>
13297403Sobrien#include <sys/jumbo.h>
13397403Sobrien#endif /* !TI_PRIVATE_JUMBOS */
13497403Sobrien#include <sys/vnode.h> /* for vfindev, vgone */
13597403Sobrien
13697403Sobrien#include <pci/pcireg.h>
13797403Sobrien#include <pci/pcivar.h>
13897403Sobrien
13997403Sobrien#include <sys/tiio.h>
14097403Sobrien#include <pci/if_tireg.h>
141132720Skan#include <pci/ti_fw.h>
142132720Skan#include <pci/ti_fw2.h>
14397403Sobrien
144132720Skan#define TI_CSUM_FEATURES	(CSUM_IP | CSUM_TCP | CSUM_UDP | CSUM_IP_FRAGS)
145132720Skan/*
14697403Sobrien * We can only turn on header splitting if we're using extended receive
147132720Skan * BDs.
148132720Skan */
14997403Sobrien#if defined(TI_JUMBO_HDRSPLIT) && defined(TI_PRIVATE_JUMBOS)
150132720Skan#error "options TI_JUMBO_HDRSPLIT and TI_PRIVATE_JUMBOS are mutually exclusive"
151132720Skan#endif /* TI_JUMBO_HDRSPLIT && TI_JUMBO_HDRSPLIT */
152132720Skan
153132720Skan#if !defined(lint)
154132720Skanstatic const char rcsid[] =
15597403Sobrien  "$FreeBSD: head/sys/dev/ti/if_ti.c 112930 2003-04-01 08:57:28Z phk $";
156132720Skan#endif
157132720Skan
158132720Skanstruct ti_softc *tis[8];
15997403Sobrien
160132720Skantypedef enum {
161132720Skan	TI_SWAP_HTON,
16297403Sobrien	TI_SWAP_NTOH
163132720Skan} ti_swap_type;
164132720Skan
165132720Skan
16697403Sobrien/*
167146897Skan * Various supported device vendors/types and their names.
168146897Skan */
169132720Skan
170169691Skanstatic struct ti_type ti_devs[] = {
171132720Skan	{ ALT_VENDORID,	ALT_DEVICEID_ACENIC,
172132720Skan		"Alteon AceNIC 1000baseSX Gigabit Ethernet" },
173132720Skan	{ ALT_VENDORID,	ALT_DEVICEID_ACENIC_COPPER,
174132720Skan		"Alteon AceNIC 1000baseT Gigabit Ethernet" },
175132720Skan	{ TC_VENDORID,	TC_DEVICEID_3C985,
176132720Skan		"3Com 3c985-SX Gigabit Ethernet" },
177132720Skan	{ NG_VENDORID, NG_DEVICEID_GA620,
178132720Skan		"Netgear GA620 1000baseSX Gigabit Ethernet" },
179132720Skan	{ NG_VENDORID, NG_DEVICEID_GA620T,
180132720Skan		"Netgear GA620 1000baseT Gigabit Ethernet" },
181132720Skan	{ SGI_VENDORID, SGI_DEVICEID_TIGON,
182132720Skan		"Silicon Graphics Gigabit Ethernet" },
183132720Skan	{ DEC_VENDORID, DEC_DEVICEID_FARALLON_PN9000SX,
184132720Skan		"Farallon PN9000SX Gigabit Ethernet" },
185132720Skan	{ 0, 0, NULL }
186132720Skan};
18797403Sobrien
18897403Sobrien#define	TI_CDEV_MAJOR	153
189132720Skan
190132720Skanstatic	d_open_t	ti_open;
19197403Sobrienstatic	d_close_t	ti_close;
19297403Sobrienstatic	d_ioctl_t	ti_ioctl2;
193132720Skan
19497403Sobrienstatic struct cdevsw ti_cdevsw = {
19597403Sobrien	.d_open =	ti_open,
19697403Sobrien	.d_close =	ti_close,
197132720Skan	.d_ioctl =	ti_ioctl2,
198132720Skan	.d_name =	"ti",
19997403Sobrien	.d_maj =	TI_CDEV_MAJOR,
200132720Skan};
201132720Skan
202132720Skanstatic int ti_probe		(device_t);
203132720Skanstatic int ti_attach		(device_t);
204132720Skanstatic int ti_detach		(device_t);
205132720Skanstatic void ti_txeof		(struct ti_softc *);
206132720Skanstatic void ti_rxeof		(struct ti_softc *);
20797403Sobrien
208132720Skanstatic void ti_stats_update	(struct ti_softc *);
20997403Sobrienstatic int ti_encap		(struct ti_softc *, struct mbuf *, u_int32_t *);
21097403Sobrien
211132720Skanstatic void ti_intr		(void *);
212132720Skanstatic void ti_start		(struct ifnet *);
213132720Skanstatic int ti_ioctl		(struct ifnet *, u_long, caddr_t);
214132720Skanstatic void ti_init		(void *);
215132720Skanstatic void ti_init2		(struct ti_softc *);
216132720Skanstatic void ti_stop		(struct ti_softc *);
217132720Skanstatic void ti_watchdog		(struct ifnet *);
218132720Skanstatic void ti_shutdown		(device_t);
219132720Skanstatic int ti_ifmedia_upd	(struct ifnet *);
220132720Skanstatic void ti_ifmedia_sts	(struct ifnet *, struct ifmediareq *);
22197403Sobrien
22297403Sobrienstatic u_int32_t ti_eeprom_putbyte	(struct ti_softc *, int);
223132720Skanstatic u_int8_t	ti_eeprom_getbyte	(struct ti_softc *, int, u_int8_t *);
224132720Skanstatic int ti_read_eeprom	(struct ti_softc *, caddr_t, int, int);
225132720Skan
226132720Skanstatic void ti_add_mcast	(struct ti_softc *, struct ether_addr *);
227132720Skanstatic void ti_del_mcast	(struct ti_softc *, struct ether_addr *);
228132720Skanstatic void ti_setmulti		(struct ti_softc *);
22997403Sobrien
230132720Skanstatic void ti_mem		(struct ti_softc *, u_int32_t,
23197403Sobrien					u_int32_t, caddr_t);
232132720Skanstatic int ti_copy_mem		(struct ti_softc *, u_int32_t,
233132720Skan					u_int32_t, caddr_t, int, int);
23497403Sobrienstatic int ti_copy_scratch	(struct ti_softc *, u_int32_t,
235132720Skan					u_int32_t, caddr_t, int, int, int);
236132720Skanstatic int ti_bcopy_swap	(const void *, void *, size_t,
237132720Skan					ti_swap_type);
23897403Sobrienstatic void ti_loadfw		(struct ti_softc *);
239146897Skanstatic void ti_cmd		(struct ti_softc *, struct ti_cmd_desc *);
240146897Skanstatic void ti_cmd_ext		(struct ti_softc *, struct ti_cmd_desc *,
24197403Sobrien					caddr_t, int);
242169691Skanstatic void ti_handle_events	(struct ti_softc *);
243132720Skan#ifdef TI_PRIVATE_JUMBOS
244132720Skanstatic int ti_alloc_jumbo_mem	(struct ti_softc *);
24597403Sobrienstatic void *ti_jalloc		(struct ti_softc *);
246132720Skanstatic void ti_jfree		(void *, void *);
247132720Skan#endif /* TI_PRIVATE_JUMBOS */
24897403Sobrienstatic int ti_newbuf_std	(struct ti_softc *, int, struct mbuf *);
249132720Skanstatic int ti_newbuf_mini	(struct ti_softc *, int, struct mbuf *);
250132720Skanstatic int ti_newbuf_jumbo	(struct ti_softc *, int, struct mbuf *);
251132720Skanstatic int ti_init_rx_ring_std	(struct ti_softc *);
25297403Sobrienstatic void ti_free_rx_ring_std	(struct ti_softc *);
253132720Skanstatic int ti_init_rx_ring_jumbo	(struct ti_softc *);
254132720Skanstatic void ti_free_rx_ring_jumbo	(struct ti_softc *);
255132720Skanstatic int ti_init_rx_ring_mini	(struct ti_softc *);
25697403Sobrienstatic void ti_free_rx_ring_mini	(struct ti_softc *);
257132720Skanstatic void ti_free_tx_ring	(struct ti_softc *);
258132720Skanstatic int ti_init_tx_ring	(struct ti_softc *);
25997403Sobrien
260132720Skanstatic int ti_64bitslot_war	(struct ti_softc *);
261132720Skanstatic int ti_chipinit		(struct ti_softc *);
26297403Sobrienstatic int ti_gibinit		(struct ti_softc *);
26397403Sobrien
264132720Skan#ifdef TI_JUMBO_HDRSPLIT
265132720Skanstatic __inline void ti_hdr_split	(struct mbuf *top, int hdr_len,
26697403Sobrien					     int pkt_len, int idx);
267132720Skan#endif /* TI_JUMBO_HDRSPLIT */
268132720Skan
269132720Skanstatic device_method_t ti_methods[] = {
27097403Sobrien	/* Device interface */
271132720Skan	DEVMETHOD(device_probe,		ti_probe),
272132720Skan	DEVMETHOD(device_attach,	ti_attach),
273132720Skan	DEVMETHOD(device_detach,	ti_detach),
274132720Skan	DEVMETHOD(device_shutdown,	ti_shutdown),
275132720Skan	{ 0, 0 }
276132720Skan};
27797403Sobrien
278132720Skanstatic driver_t ti_driver = {
279132720Skan	"ti",
280132720Skan	ti_methods,
281132720Skan	sizeof(struct ti_softc)
282132720Skan};
283132720Skan
284132720Skanstatic devclass_t ti_devclass;
28597403Sobrien
28697403SobrienDRIVER_MODULE(if_ti, pci, ti_driver, ti_devclass, 0, 0);
287132720Skan
288132720Skan/* List of Tigon softcs */
289132720Skanstatic STAILQ_HEAD(ti_softc_list, ti_softc) ti_sc_list;
29097403Sobrien
291132720Skanstatic struct ti_softc *
292132720Skanti_lookup_softc(int unit)
293132720Skan{
29497403Sobrien	struct ti_softc *sc;
295132720Skan	for (sc = STAILQ_FIRST(&ti_sc_list); sc != NULL;
296132720Skan	     sc = STAILQ_NEXT(sc, ti_links))
29797403Sobrien		if (sc->ti_unit == unit)
298132720Skan			return(sc);
299132720Skan	return(NULL);
300132720Skan}
301132720Skan
302132720Skan/*
30397403Sobrien * Send an instruction or address to the EEPROM, check for ACK.
304132720Skan */
305132720Skanstatic u_int32_t ti_eeprom_putbyte(sc, byte)
306132720Skan	struct ti_softc		*sc;
307132720Skan	int			byte;
308132720Skan{
30997403Sobrien	register int		i, ack = 0;
310132720Skan
311132720Skan	/*
312132720Skan	 * Make sure we're in TX mode.
31397403Sobrien	 */
314132720Skan	TI_SETBIT(sc, TI_MISC_LOCAL_CTL, TI_MLC_EE_TXEN);
315132720Skan
316132720Skan	/*
31797403Sobrien	 * Feed in each bit and stobe the clock.
318132720Skan	 */
319132720Skan	for (i = 0x80; i; i >>= 1) {
320132720Skan		if (byte & i) {
321132720Skan			TI_SETBIT(sc, TI_MISC_LOCAL_CTL, TI_MLC_EE_DOUT);
322132720Skan		} else {
32397403Sobrien			TI_CLRBIT(sc, TI_MISC_LOCAL_CTL, TI_MLC_EE_DOUT);
324132720Skan		}
325132720Skan		DELAY(1);
326132720Skan		TI_SETBIT(sc, TI_MISC_LOCAL_CTL, TI_MLC_EE_CLK);
32797403Sobrien		DELAY(1);
32897403Sobrien		TI_CLRBIT(sc, TI_MISC_LOCAL_CTL, TI_MLC_EE_CLK);
329132720Skan	}
330132720Skan
331132720Skan	/*
33297403Sobrien	 * Turn off TX mode.
333132720Skan	 */
334132720Skan	TI_CLRBIT(sc, TI_MISC_LOCAL_CTL, TI_MLC_EE_TXEN);
33597403Sobrien
33697403Sobrien	/*
33797403Sobrien	 * Check for ack.
338132720Skan	 */
33997403Sobrien	TI_SETBIT(sc, TI_MISC_LOCAL_CTL, TI_MLC_EE_CLK);
340132720Skan	ack = CSR_READ_4(sc, TI_MISC_LOCAL_CTL) & TI_MLC_EE_DIN;
34197403Sobrien	TI_CLRBIT(sc, TI_MISC_LOCAL_CTL, TI_MLC_EE_CLK);
34297403Sobrien
34397403Sobrien	return(ack);
34497403Sobrien}
34597403Sobrien
34697403Sobrien/*
34797403Sobrien * Read a byte of data stored in the EEPROM at address 'addr.'
34897403Sobrien * We have to send two address bytes since the EEPROM can hold
349132720Skan * more than 256 bytes of data.
35097403Sobrien */
35197403Sobrienstatic u_int8_t ti_eeprom_getbyte(sc, addr, dest)
352132720Skan	struct ti_softc		*sc;
353132720Skan	int			addr;
354169691Skan	u_int8_t		*dest;
355169691Skan{
356169691Skan	register int		i;
357169691Skan	u_int8_t		byte = 0;
358169691Skan
359169691Skan	EEPROM_START;
360132720Skan
361132720Skan	/*
362169691Skan	 * Send write control code to EEPROM.
363169691Skan	 */
364169691Skan	if (ti_eeprom_putbyte(sc, EEPROM_CTL_WRITE)) {
365169691Skan		printf("ti%d: failed to send write command, status: %x\n",
36697403Sobrien		    sc->ti_unit, CSR_READ_4(sc, TI_MISC_LOCAL_CTL));
367132720Skan		return(1);
368132720Skan	}
369132720Skan
370132720Skan	/*
371132720Skan	 * Send first byte of address of byte we want to read.
372132720Skan	 */
373132720Skan	if (ti_eeprom_putbyte(sc, (addr >> 8) & 0xFF)) {
374132720Skan		printf("ti%d: failed to send address, status: %x\n",
37597403Sobrien		    sc->ti_unit, CSR_READ_4(sc, TI_MISC_LOCAL_CTL));
37697403Sobrien		return(1);
37797403Sobrien	}
37897403Sobrien	/*
379132720Skan	 * Send second byte address of byte we want to read.
380169691Skan	 */
38197403Sobrien	if (ti_eeprom_putbyte(sc, addr & 0xFF)) {
38297403Sobrien		printf("ti%d: failed to send address, status: %x\n",
383132720Skan		    sc->ti_unit, CSR_READ_4(sc, TI_MISC_LOCAL_CTL));
384132720Skan		return(1);
38597403Sobrien	}
38697403Sobrien
38797403Sobrien	EEPROM_STOP;
388132720Skan	EEPROM_START;
389132720Skan	/*
390132720Skan	 * Send read control code to EEPROM.
39197403Sobrien	 */
39297403Sobrien	if (ti_eeprom_putbyte(sc, EEPROM_CTL_READ)) {
39397403Sobrien		printf("ti%d: failed to send read command, status: %x\n",
39497403Sobrien		    sc->ti_unit, CSR_READ_4(sc, TI_MISC_LOCAL_CTL));
39597403Sobrien		return(1);
39697403Sobrien	}
39797403Sobrien
39897403Sobrien	/*
39997403Sobrien	 * Start reading bits from EEPROM.
400169691Skan	 */
40197403Sobrien	TI_CLRBIT(sc, TI_MISC_LOCAL_CTL, TI_MLC_EE_TXEN);
402169691Skan	for (i = 0x80; i; i >>= 1) {
40397403Sobrien		TI_SETBIT(sc, TI_MISC_LOCAL_CTL, TI_MLC_EE_CLK);
40497403Sobrien		DELAY(1);
40597403Sobrien		if (CSR_READ_4(sc, TI_MISC_LOCAL_CTL) & TI_MLC_EE_DIN)
406132720Skan			byte |= i;
407132720Skan		TI_CLRBIT(sc, TI_MISC_LOCAL_CTL, TI_MLC_EE_CLK);
408169691Skan		DELAY(1);
409132720Skan	}
410132720Skan
411132720Skan	EEPROM_STOP;
412132720Skan
413132720Skan	/*
41497403Sobrien	 * No ACK generated for read, so just return byte.
415236829Spfg	 */
416236829Spfg
417169691Skan	*dest = byte;
418236829Spfg
419236829Spfg	return(0);
420236829Spfg}
421236829Spfg
422236829Spfg/*
423236829Spfg * Read a sequence of bytes from the EEPROM.
424236829Spfg */
425236829Spfgstatic int
426236829Spfgti_read_eeprom(sc, dest, off, cnt)
427236829Spfg	struct ti_softc		*sc;
428132720Skan	caddr_t			dest;
429132720Skan	int			off;
430132720Skan	int			cnt;
431132720Skan{
432132720Skan	int			err = 0, i;
433132720Skan	u_int8_t		byte = 0;
434132720Skan
43597403Sobrien	for (i = 0; i < cnt; i++) {
436132720Skan		err = ti_eeprom_getbyte(sc, off + i, &byte);
437132720Skan		if (err)
438132720Skan			break;
439132720Skan		*(dest + i) = byte;
440132720Skan	}
441132720Skan
442132720Skan	return(err ? 1 : 0);
443132720Skan}
44497403Sobrien
445236829Spfg/*
446236829Spfg * NIC memory access function. Can be used to either clear a section
447236829Spfg * of NIC local memory or (if buf is non-NULL) copy data into it.
448236829Spfg */
449236829Spfgstatic void
450236829Spfgti_mem(sc, addr, len, buf)
451169691Skan	struct ti_softc		*sc;
452169691Skan	u_int32_t		addr, len;
453236829Spfg	caddr_t			buf;
454236829Spfg{
455236829Spfg	int			segptr, segsize, cnt;
456236829Spfg	caddr_t			ti_winbase, ptr;
457236829Spfg
458236829Spfg	segptr = addr;
459132720Skan	cnt = len;
460132720Skan	ti_winbase = (caddr_t)(sc->ti_vhandle + TI_WINDOW);
461132720Skan	ptr = buf;
462132720Skan
463132720Skan	while(cnt) {
464132720Skan		if (cnt < TI_WINLEN)
46597403Sobrien			segsize = cnt;
466132720Skan		else
46797403Sobrien			segsize = TI_WINLEN - (segptr % TI_WINLEN);
468132720Skan		CSR_WRITE_4(sc, TI_WINBASE, (segptr & ~(TI_WINLEN - 1)));
469132720Skan		if (buf == NULL)
470132720Skan			bzero((char *)ti_winbase + (segptr &
471132720Skan			    (TI_WINLEN - 1)), segsize);
47297403Sobrien		else {
473132720Skan			bcopy((char *)ptr, (char *)ti_winbase +
474132720Skan			    (segptr & (TI_WINLEN - 1)), segsize);
475132720Skan			ptr += segsize;
47697403Sobrien		}
477132720Skan		segptr += segsize;
478132720Skan		cnt -= segsize;
479132720Skan	}
48097403Sobrien
481132720Skan	return;
482132720Skan}
483132720Skan
48497403Sobrienstatic int
485132720Skanti_copy_mem(sc, tigon_addr, len, buf, useraddr, readdata)
486132720Skan	struct ti_softc		*sc;
487132720Skan	u_int32_t		tigon_addr, len;
48897403Sobrien	caddr_t			buf;
489132720Skan	int			useraddr, readdata;
490132720Skan{
491132720Skan	int		segptr, segsize, cnt;
49297403Sobrien	caddr_t		ptr;
493132720Skan	u_int32_t	origwin;
494132720Skan	u_int8_t	tmparray[TI_WINLEN], tmparray2[TI_WINLEN];
495132720Skan	int		resid, segresid;
49697403Sobrien	int		first_pass;
497132720Skan
498132720Skan	/*
499169691Skan	 * At the moment, we don't handle non-aligned cases, we just bail.
500169691Skan	 * If this proves to be a problem, it will be fixed.
501169691Skan	 */
502169691Skan	if ((readdata == 0)
50397403Sobrien	 && (tigon_addr & 0x3)) {
504132720Skan		printf("ti%d: ti_copy_mem: tigon address %#x isn't "
505132720Skan		       "word-aligned\n", sc->ti_unit, tigon_addr);
506132720Skan		printf("ti%d: ti_copy_mem: unaligned writes aren't yet "
50797403Sobrien		       "supported\n", sc->ti_unit);
508132720Skan		return(EINVAL);
509132720Skan	}
510132720Skan
51197403Sobrien	segptr = tigon_addr & ~0x3;
512132720Skan	segresid = tigon_addr - segptr;
513132720Skan
514132720Skan	/*
51597403Sobrien	 * This is the non-aligned amount left over that we'll need to
516132720Skan	 * copy.
517132720Skan	 */
518132720Skan	resid = len & 0x3;
51997403Sobrien
520132720Skan	/* Add in the left over amount at the front of the buffer */
521132720Skan	resid += segresid;
522132720Skan
52397403Sobrien	cnt = len & ~0x3;
524132720Skan	/*
525132720Skan	 * If resid + segresid is >= 4, add multiples of 4 to the count and
526132720Skan	 * decrease the residual by that much.
527132720Skan	 */
528132720Skan	cnt += resid & ~0x3;
529132720Skan	resid -= resid & ~0x3;
530132720Skan
531132720Skan	ptr = buf;
532132720Skan
533132720Skan	first_pass = 1;
534132720Skan
535132720Skan	/*
536132720Skan	 * Make sure we aren't interrupted while we're changing the window
537132720Skan	 * pointer.
538132720Skan	 */
539132720Skan	TI_LOCK(sc);
540132720Skan
541132720Skan	/*
542132720Skan	 * Save the old window base value.
543132720Skan	 */
544132720Skan	origwin = CSR_READ_4(sc, TI_WINBASE);
545132720Skan
546132720Skan	while(cnt) {
547132720Skan		bus_size_t ti_offset;
548132720Skan
549132720Skan		if (cnt < TI_WINLEN)
550132720Skan			segsize = cnt;
551132720Skan		else
552132720Skan			segsize = TI_WINLEN - (segptr % TI_WINLEN);
553132720Skan		CSR_WRITE_4(sc, TI_WINBASE, (segptr & ~(TI_WINLEN - 1)));
554132720Skan
555132720Skan		ti_offset = TI_WINDOW + (segptr & (TI_WINLEN -1));
556132720Skan
557132720Skan		if (readdata) {
558132720Skan
559132720Skan			bus_space_read_region_4(sc->ti_btag,
56097403Sobrien						sc->ti_bhandle, ti_offset,
561132720Skan						(u_int32_t *)tmparray,
562132720Skan						segsize >> 2);
56397403Sobrien			if (useraddr) {
564132720Skan				/*
565117397Skan				 * Yeah, this is a little on the kludgy
56697403Sobrien				 * side, but at least this code is only
56797403Sobrien				 * used for debugging.
568132720Skan				 */
56997403Sobrien				ti_bcopy_swap(tmparray, tmparray2, segsize,
57097403Sobrien					      TI_SWAP_NTOH);
571169691Skan
572169691Skan				if (first_pass) {
573169691Skan					copyout(&tmparray2[segresid], ptr,
574169691Skan						segsize - segresid);
575169691Skan					first_pass = 0;
576169691Skan				} else
577169691Skan					copyout(tmparray2, ptr, segsize);
578169691Skan			} else {
579169691Skan				if (first_pass) {
580132720Skan
581132720Skan					ti_bcopy_swap(tmparray, tmparray2,
58297403Sobrien						      segsize, TI_SWAP_NTOH);
583132720Skan					bcopy(&tmparray2[segresid], ptr,
58497403Sobrien					      segsize - segresid);
58597403Sobrien					first_pass = 0;
58697403Sobrien				} else
58797403Sobrien					ti_bcopy_swap(tmparray, ptr, segsize,
58897403Sobrien						      TI_SWAP_NTOH);
589132720Skan			}
59097403Sobrien
591236829Spfg		} else {
592236829Spfg			if (useraddr) {
593236829Spfg				copyin(ptr, tmparray2, segsize);
594132720Skan				ti_bcopy_swap(tmparray2, tmparray, segsize,
59597403Sobrien					      TI_SWAP_HTON);
596236829Spfg			} else
597236829Spfg				ti_bcopy_swap(ptr, tmparray, segsize,
598132720Skan					      TI_SWAP_HTON);
599132720Skan
60097403Sobrien			bus_space_write_region_4(sc->ti_btag,
601132720Skan						 sc->ti_bhandle, ti_offset,
60297403Sobrien						 (u_int32_t *)tmparray,
60397403Sobrien						 segsize >> 2);
604132720Skan		}
60597403Sobrien		segptr += segsize;
60697403Sobrien		ptr += segsize;
60797403Sobrien		cnt -= segsize;
608132720Skan	}
609132720Skan
61097403Sobrien	/*
611236829Spfg	 * Handle leftover, non-word-aligned bytes.
612236829Spfg	 */
61397403Sobrien	if (resid != 0) {
61497403Sobrien		u_int32_t	tmpval, tmpval2;
615132720Skan		bus_size_t	ti_offset;
616132720Skan
617132720Skan		/*
61897403Sobrien		 * Set the segment pointer.
619132720Skan		 */
620132720Skan		CSR_WRITE_4(sc, TI_WINBASE, (segptr & ~(TI_WINLEN - 1)));
621169691Skan
622169691Skan		ti_offset = TI_WINDOW + (segptr & (TI_WINLEN - 1));
623169691Skan
624169691Skan		/*
62597403Sobrien		 * First, grab whatever is in our source/destination.
626132720Skan		 * We'll obviously need this for reads, but also for
627132720Skan		 * writes, since we'll be doing read/modify/write.
628169691Skan		 */
629169691Skan		bus_space_read_region_4(sc->ti_btag, sc->ti_bhandle,
630169691Skan					ti_offset, &tmpval, 1);
631169691Skan
63297403Sobrien		/*
633132720Skan		 * Next, translate this from little-endian to big-endian
634132720Skan		 * (at least on i386 boxes).
635169691Skan		 */
63697403Sobrien		tmpval2 = ntohl(tmpval);
637132720Skan
638132720Skan		if (readdata) {
639169691Skan			/*
640169691Skan			 * If we're reading, just copy the leftover number
641169691Skan			 * of bytes from the host byte order buffer to
642169691Skan			 * the user's buffer.
64397403Sobrien			 */
644132720Skan			if (useraddr)
645132720Skan				copyout(&tmpval2, ptr, resid);
646132720Skan			else
64797403Sobrien				bcopy(&tmpval2, ptr, resid);
648132720Skan		} else {
649132720Skan			/*
650132720Skan			 * If we're writing, first copy the bytes to be
65197403Sobrien			 * written into the network byte order buffer,
652132720Skan			 * leaving the rest of the buffer with whatever was
653132720Skan			 * originally in there.  Then, swap the bytes
654132720Skan			 * around into host order and write them out.
65597403Sobrien			 *
656132720Skan			 * XXX KDM the read side of this has been verified
657132720Skan			 * to work, but the write side of it has not been
658132720Skan			 * verified.  So user beware.
65997403Sobrien			 */
660132720Skan			if (useraddr)
661132720Skan				copyin(ptr, &tmpval2, resid);
662132720Skan			else
66397403Sobrien				bcopy(ptr, &tmpval2, resid);
664132720Skan
665132720Skan			tmpval = htonl(tmpval2);
666132720Skan
66797403Sobrien			bus_space_write_region_4(sc->ti_btag, sc->ti_bhandle,
668132720Skan						 ti_offset, &tmpval, 1);
669132720Skan		}
670169691Skan	}
671132720Skan
672132720Skan	CSR_WRITE_4(sc, TI_WINBASE, origwin);
673236829Spfg
674132720Skan	TI_UNLOCK(sc);
67597403Sobrien
676169691Skan	return(0);
677169691Skan}
67897403Sobrien
679132720Skanstatic int
680169691Skanti_copy_scratch(sc, tigon_addr, len, buf, useraddr, readdata, cpu)
68197403Sobrien	struct ti_softc		*sc;
682169691Skan	u_int32_t		tigon_addr, len;
683169691Skan	caddr_t			buf;
684132720Skan	int			useraddr, readdata;
685169691Skan	int			cpu;
68697403Sobrien{
687132720Skan	u_int32_t	segptr;
688169691Skan	int		cnt;
68997403Sobrien	u_int32_t	tmpval, tmpval2;
690169691Skan	caddr_t		ptr;
691169691Skan
692169691Skan	/*
693169691Skan	 * At the moment, we don't handle non-aligned cases, we just bail.
694169691Skan	 * If this proves to be a problem, it will be fixed.
695169691Skan	 */
696169691Skan	if (tigon_addr & 0x3) {
697169691Skan		printf("ti%d: ti_copy_scratch: tigon address %#x isn't "
698169691Skan		       "word-aligned\n", sc->ti_unit, tigon_addr);
69997403Sobrien		return(EINVAL);
700169691Skan	}
701169691Skan
70297403Sobrien	if (len & 0x3) {
70397403Sobrien		printf("ti%d: ti_copy_scratch: transfer length %d isn't "
704169691Skan		       "word-aligned\n", sc->ti_unit, len);
705169691Skan		return(EINVAL);
70697403Sobrien	}
707132720Skan
70897403Sobrien	segptr = tigon_addr;
70997403Sobrien	cnt = len;
710169691Skan	ptr = buf;
711169691Skan
712169691Skan	TI_LOCK(sc);
713132720Skan
71497403Sobrien	while (cnt) {
71597403Sobrien		CSR_WRITE_4(sc, CPU_REG(TI_SRAM_ADDR, cpu), segptr);
716132720Skan
71797403Sobrien		if (readdata) {
71897403Sobrien			tmpval2 = CSR_READ_4(sc, CPU_REG(TI_SRAM_DATA, cpu));
719132720Skan
720169691Skan			tmpval = ntohl(tmpval2);
721169691Skan
722169691Skan			/*
72397403Sobrien			 * Note:  I've used this debugging interface
72497403Sobrien			 * extensively with Alteon's 12.3.15 firmware,
725132720Skan			 * compiled with GCC 2.7.2.1 and binutils 2.9.1.
726132720Skan			 *
72797403Sobrien			 * When you compile the firmware without
728132720Skan			 * optimization, which is necessary sometimes in
729132720Skan			 * order to properly step through it, you sometimes
730132720Skan			 * read out a bogus value of 0xc0017c instead of
731132720Skan			 * whatever was supposed to be in that scratchpad
732132720Skan			 * location.  That value is on the stack somewhere,
733132720Skan			 * but I've never been able to figure out what was
73497403Sobrien			 * causing the problem.
73597403Sobrien			 *
736132720Skan			 * The address seems to pop up in random places,
73797403Sobrien			 * often not in the same place on two subsequent
73897403Sobrien			 * reads.
739132720Skan			 *
74097403Sobrien			 * In any case, the underlying data doesn't seem
74197403Sobrien			 * to be affected, just the value read out.
742132720Skan			 *
74397403Sobrien			 * KDM, 3/7/2000
74497403Sobrien			 */
745132720Skan
74697403Sobrien			if (tmpval2 == 0xc0017c)
74797403Sobrien				printf("ti%d: found 0xc0017c at %#x "
748132720Skan				       "(tmpval2)\n", sc->ti_unit, segptr);
74997403Sobrien
75097403Sobrien			if (tmpval == 0xc0017c)
751132720Skan				printf("ti%d: found 0xc0017c at %#x "
75297403Sobrien				       "(tmpval)\n", sc->ti_unit, segptr);
75397403Sobrien
754132720Skan			if (useraddr)
75597403Sobrien				copyout(&tmpval, ptr, 4);
75697403Sobrien			else
757132720Skan				bcopy(&tmpval, ptr, 4);
75897403Sobrien		} else {
75997403Sobrien			if (useraddr)
760132720Skan				copyin(ptr, &tmpval2, 4);
76197403Sobrien			else
76297403Sobrien				bcopy(ptr, &tmpval2, 4);
76397403Sobrien
764132720Skan			tmpval = htonl(tmpval2);
76597403Sobrien
76697403Sobrien			CSR_WRITE_4(sc, CPU_REG(TI_SRAM_DATA, cpu), tmpval);
76797403Sobrien		}
768132720Skan
76997403Sobrien		cnt -= 4;
770132720Skan		segptr += 4;
771169691Skan		ptr += 4;
772169691Skan	}
77397403Sobrien
774132720Skan	TI_UNLOCK(sc);
775146897Skan
77697403Sobrien	return(0);
77797403Sobrien}
778132720Skan
77997403Sobrienstatic int
780132720Skanti_bcopy_swap(src, dst, len, swap_type)
781169691Skan	const void	*src;
782169691Skan	void		*dst;
78397403Sobrien	size_t		len;
784146897Skan	ti_swap_type	swap_type;
785146897Skan{
78697403Sobrien	const u_int8_t *tmpsrc;
78797403Sobrien	u_int8_t *tmpdst;
788132720Skan	size_t tmplen;
78997403Sobrien
790132720Skan	if (len & 0x3) {
791169691Skan		printf("ti_bcopy_swap: length %zd isn't 32-bit aligned\n",
792169691Skan		       len);
79397403Sobrien		return(-1);
79497403Sobrien	}
795132720Skan
79697403Sobrien	tmpsrc = src;
797132720Skan	tmpdst = dst;
798169691Skan	tmplen = len;
799169691Skan
80097403Sobrien	while (tmplen) {
80197403Sobrien		if (swap_type == TI_SWAP_NTOH)
802132720Skan			*(u_int32_t *)tmpdst =
80397403Sobrien				ntohl(*(const u_int32_t *)tmpsrc);
804132720Skan		else
805169691Skan			*(u_int32_t *)tmpdst =
806169691Skan				htonl(*(const u_int32_t *)tmpsrc);
807132720Skan
80897403Sobrien		tmpsrc += 4;
809132720Skan		tmpdst += 4;
81097403Sobrien		tmplen -= 4;
811132720Skan	}
812169691Skan
813169691Skan	return(0);
814132720Skan}
81597403Sobrien
816132720Skan/*
81797403Sobrien * Load firmware image into the NIC. Check that the firmware revision
818132720Skan * is acceptable and see if we want the firmware for the Tigon 1 or
819169691Skan * Tigon 2.
820169691Skan */
82197403Sobrienstatic void
82297403Sobrienti_loadfw(sc)
823132720Skan	struct ti_softc		*sc;
82497403Sobrien{
825169691Skan	switch(sc->ti_hwrev) {
826169691Skan	case TI_HWREV_TIGON:
827169691Skan		if (tigonFwReleaseMajor != TI_FIRMWARE_MAJOR ||
82897403Sobrien		    tigonFwReleaseMinor != TI_FIRMWARE_MINOR ||
829132720Skan		    tigonFwReleaseFix != TI_FIRMWARE_FIX) {
83097403Sobrien			printf("ti%d: firmware revision mismatch; want "
83197403Sobrien			    "%d.%d.%d, got %d.%d.%d\n", sc->ti_unit,
83297403Sobrien			    TI_FIRMWARE_MAJOR, TI_FIRMWARE_MINOR,
833132720Skan			    TI_FIRMWARE_FIX, tigonFwReleaseMajor,
834132720Skan			    tigonFwReleaseMinor, tigonFwReleaseFix);
83597403Sobrien			return;
836132720Skan		}
83797403Sobrien		ti_mem(sc, tigonFwTextAddr, tigonFwTextLen,
83897403Sobrien		    (caddr_t)tigonFwText);
839132720Skan		ti_mem(sc, tigonFwDataAddr, tigonFwDataLen,
84097403Sobrien		    (caddr_t)tigonFwData);
84197403Sobrien		ti_mem(sc, tigonFwRodataAddr, tigonFwRodataLen,
84297403Sobrien		    (caddr_t)tigonFwRodata);
84397403Sobrien		ti_mem(sc, tigonFwBssAddr, tigonFwBssLen, NULL);
84497403Sobrien		ti_mem(sc, tigonFwSbssAddr, tigonFwSbssLen, NULL);
845132720Skan		CSR_WRITE_4(sc, TI_CPU_PROGRAM_COUNTER, tigonFwStartAddr);
84697403Sobrien		break;
847169691Skan	case TI_HWREV_TIGON_II:
848169691Skan		if (tigon2FwReleaseMajor != TI_FIRMWARE_MAJOR ||
849132720Skan		    tigon2FwReleaseMinor != TI_FIRMWARE_MINOR ||
85097403Sobrien		    tigon2FwReleaseFix != TI_FIRMWARE_FIX) {
851169691Skan			printf("ti%d: firmware revision mismatch; want "
852169691Skan			    "%d.%d.%d, got %d.%d.%d\n", sc->ti_unit,
853169691Skan			    TI_FIRMWARE_MAJOR, TI_FIRMWARE_MINOR,
854169691Skan			    TI_FIRMWARE_FIX, tigon2FwReleaseMajor,
855132720Skan			    tigon2FwReleaseMinor, tigon2FwReleaseFix);
856132720Skan			return;
857169691Skan		}
858169691Skan		ti_mem(sc, tigon2FwTextAddr, tigon2FwTextLen,
859169691Skan		    (caddr_t)tigon2FwText);
860169691Skan		ti_mem(sc, tigon2FwDataAddr, tigon2FwDataLen,
861169691Skan		    (caddr_t)tigon2FwData);
862132720Skan		ti_mem(sc, tigon2FwRodataAddr, tigon2FwRodataLen,
863169691Skan		    (caddr_t)tigon2FwRodata);
864169691Skan		ti_mem(sc, tigon2FwBssAddr, tigon2FwBssLen, NULL);
865169691Skan		ti_mem(sc, tigon2FwSbssAddr, tigon2FwSbssLen, NULL);
866169691Skan		CSR_WRITE_4(sc, TI_CPU_PROGRAM_COUNTER, tigon2FwStartAddr);
867169691Skan		break;
868169691Skan	default:
869169691Skan		printf("ti%d: can't load firmware: unknown hardware rev\n",
870169691Skan		    sc->ti_unit);
871169691Skan		break;
872169691Skan	}
873169691Skan
874169691Skan	return;
875132720Skan}
876132720Skan
877132720Skan/*
87897403Sobrien * Send the NIC a command via the command ring.
87997403Sobrien */
88097403Sobrienstatic void
881132720Skanti_cmd(sc, cmd)
88297403Sobrien	struct ti_softc		*sc;
883169691Skan	struct ti_cmd_desc	*cmd;
884169691Skan{
885169691Skan	u_int32_t		index;
88697403Sobrien
887169691Skan	if (sc->ti_rdata->ti_cmd_ring == NULL)
888169691Skan		return;
889169691Skan
890169691Skan	index = sc->ti_cmd_saved_prodidx;
891169691Skan	CSR_WRITE_4(sc, TI_GCR_CMDRING + (index * 4), *(u_int32_t *)(cmd));
892169691Skan	TI_INC(index, TI_CMD_RING_CNT);
893169691Skan	CSR_WRITE_4(sc, TI_MB_CMDPROD_IDX, index);
894169691Skan	sc->ti_cmd_saved_prodidx = index;
895169691Skan
896169691Skan	return;
897169691Skan}
898169691Skan
899169691Skan/*
900169691Skan * Send the NIC an extended command. The 'len' parameter specifies the
901169691Skan * number of command slots to include after the initial command.
902169691Skan */
903169691Skanstatic void
904169691Skanti_cmd_ext(sc, cmd, arg, len)
905169691Skan	struct ti_softc		*sc;
906132720Skan	struct ti_cmd_desc	*cmd;
907132720Skan	caddr_t			arg;
908132720Skan	int			len;
90997403Sobrien{
91097403Sobrien	u_int32_t		index;
911132720Skan	register int		i;
912132720Skan
91397403Sobrien	if (sc->ti_rdata->ti_cmd_ring == NULL)
91497403Sobrien		return;
91597403Sobrien
91697403Sobrien	index = sc->ti_cmd_saved_prodidx;
917132720Skan	CSR_WRITE_4(sc, TI_GCR_CMDRING + (index * 4), *(u_int32_t *)(cmd));
91897403Sobrien	TI_INC(index, TI_CMD_RING_CNT);
919169691Skan	for (i = 0; i < len; i++) {
920169691Skan		CSR_WRITE_4(sc, TI_GCR_CMDRING + (index * 4),
921169691Skan		    *(u_int32_t *)(&arg[i * 4]));
922169691Skan		TI_INC(index, TI_CMD_RING_CNT);
923169691Skan	}
924169691Skan	CSR_WRITE_4(sc, TI_MB_CMDPROD_IDX, index);
925169691Skan	sc->ti_cmd_saved_prodidx = index;
926169691Skan
927169691Skan	return;
928169691Skan}
929169691Skan
930169691Skan/*
931169691Skan * Handle events that have triggered interrupts.
932169691Skan */
933169691Skanstatic void
934169691Skanti_handle_events(sc)
935169691Skan	struct ti_softc		*sc;
936132720Skan{
937169691Skan	struct ti_event_desc	*e;
938169691Skan
939132720Skan	if (sc->ti_rdata->ti_event_ring == NULL)
940132720Skan		return;
941132720Skan
942169691Skan	while (sc->ti_ev_saved_considx != sc->ti_ev_prodidx.ti_idx) {
943169691Skan		e = &sc->ti_rdata->ti_event_ring[sc->ti_ev_saved_considx];
944169691Skan		switch(e->ti_event) {
945169691Skan		case TI_EV_LINKSTAT_CHANGED:
946169691Skan			sc->ti_linkstat = e->ti_code;
947169691Skan			if (e->ti_code == TI_EV_CODE_LINK_UP)
948169691Skan				printf("ti%d: 10/100 link up\n", sc->ti_unit);
949169691Skan			else if (e->ti_code == TI_EV_CODE_GIG_LINK_UP)
950169691Skan				printf("ti%d: gigabit link up\n", sc->ti_unit);
951169691Skan			else if (e->ti_code == TI_EV_CODE_LINK_DOWN)
952169691Skan				printf("ti%d: link down\n", sc->ti_unit);
953132720Skan			break;
954132720Skan		case TI_EV_ERROR:
955169691Skan			if (e->ti_code == TI_EV_CODE_ERR_INVAL_CMD)
956169691Skan				printf("ti%d: invalid command\n", sc->ti_unit);
957169691Skan			else if (e->ti_code == TI_EV_CODE_ERR_UNIMP_CMD)
958169691Skan				printf("ti%d: unknown command\n", sc->ti_unit);
959169691Skan			else if (e->ti_code == TI_EV_CODE_ERR_BADCFG)
960169691Skan				printf("ti%d: bad config data\n", sc->ti_unit);
961169691Skan			break;
962169691Skan		case TI_EV_FIRMWARE_UP:
963169691Skan			ti_init2(sc);
964169691Skan			break;
965132720Skan		case TI_EV_STATS_UPDATED:
966169691Skan			ti_stats_update(sc);
967169691Skan			break;
968169691Skan		case TI_EV_RESET_JUMBO_RING:
969169691Skan		case TI_EV_MCAST_UPDATED:
970169691Skan			/* Who cares. */
971169691Skan			break;
972169691Skan		default:
973169691Skan			printf("ti%d: unknown event: %d\n",
974132720Skan			    sc->ti_unit, e->ti_event);
975132720Skan			break;
976132720Skan		}
977169691Skan		/* Advance the consumer index. */
978169691Skan		TI_INC(sc->ti_ev_saved_considx, TI_EVENT_RING_CNT);
979169691Skan		CSR_WRITE_4(sc, TI_GCR_EVENTCONS_IDX, sc->ti_ev_saved_considx);
980169691Skan	}
981169691Skan
982132720Skan	return;
983132720Skan}
984132720Skan
985132720Skan#ifdef TI_PRIVATE_JUMBOS
986169691Skan
987169691Skan/*
988169691Skan * Memory management for the jumbo receive ring is a pain in the
989169691Skan * butt. We need to allocate at least 9018 bytes of space per frame,
99097403Sobrien * _and_ it has to be contiguous (unless you use the extended
991132720Skan * jumbo descriptor format). Using malloc() all the time won't
992132720Skan * work: malloc() allocates memory in powers of two, which means we
99397403Sobrien * would end up wasting a considerable amount of space by allocating
994132720Skan * 9K chunks. We don't have a jumbo mbuf cluster pool. Thus, we have
99597403Sobrien * to do our own memory management.
99697403Sobrien *
997132720Skan * The driver needs to allocate a contiguous chunk of memory at boot
99897403Sobrien * time. We then chop this up ourselves into 9K pieces and use them
99997403Sobrien * as external mbuf storage.
1000132720Skan *
100197403Sobrien * One issue here is how much memory to allocate. The jumbo ring has
1002233193Sdim * 256 slots in it, but at 9K per slot than can consume over 2MB of
1003233193Sdim * RAM. This is a bit much, especially considering we also need
1004233193Sdim * RAM for the standard ring and mini ring (on the Tigon 2). To
1005233193Sdim * save space, we only actually allocate enough memory for 64 slots
1006233193Sdim * by default, which works out to between 500 and 600K. This can
1007233193Sdim * be tuned by changing a #define in if_tireg.h.
1008132720Skan */
1009169691Skan
1010169691Skanstatic int
101197403Sobrienti_alloc_jumbo_mem(sc)
101297403Sobrien	struct ti_softc		*sc;
1013132720Skan{
101497403Sobrien	caddr_t			ptr;
1015132720Skan	register int		i;
101697403Sobrien	struct ti_jpool_entry   *entry;
1017169691Skan
101897403Sobrien	/* Grab a big chunk o' storage. */
1019169691Skan	sc->ti_cdata.ti_jumbo_buf = contigmalloc(TI_JMEM, M_DEVBUF,
1020169691Skan		M_NOWAIT, 0, 0xffffffff, PAGE_SIZE, 0);
1021132720Skan
1022132720Skan	if (sc->ti_cdata.ti_jumbo_buf == NULL) {
1023169691Skan		printf("ti%d: no memory for jumbo buffers!\n", sc->ti_unit);
1024169691Skan		return(ENOBUFS);
1025169691Skan	}
102697403Sobrien
1027169691Skan	SLIST_INIT(&sc->ti_jfree_listhead);
1028132720Skan	SLIST_INIT(&sc->ti_jinuse_listhead);
1029169691Skan
1030169691Skan	/*
1031132720Skan	 * Now divide it up into 9K pieces and save the addresses
1032169691Skan	 * in an array.
1033169691Skan	 */
1034169691Skan	ptr = sc->ti_cdata.ti_jumbo_buf;
1035169691Skan	for (i = 0; i < TI_JSLOTS; i++) {
1036169691Skan		sc->ti_cdata.ti_jslots[i] = ptr;
1037169691Skan		ptr += TI_JLEN;
1038169691Skan		entry = malloc(sizeof(struct ti_jpool_entry),
1039169691Skan			       M_DEVBUF, M_NOWAIT);
1040169691Skan		if (entry == NULL) {
1041169691Skan			contigfree(sc->ti_cdata.ti_jumbo_buf, TI_JMEM,
1042169691Skan			           M_DEVBUF);
1043169691Skan			sc->ti_cdata.ti_jumbo_buf = NULL;
1044169691Skan			printf("ti%d: no memory for jumbo "
1045169691Skan			    "buffer queue!\n", sc->ti_unit);
1046169691Skan			return(ENOBUFS);
1047169691Skan		}
1048169691Skan		entry->slot = i;
1049169691Skan		SLIST_INSERT_HEAD(&sc->ti_jfree_listhead, entry, jpool_entries);
1050169691Skan	}
1051169691Skan
1052169691Skan	return(0);
1053169691Skan}
105497403Sobrien
1055169691Skan/*
1056169691Skan * Allocate a jumbo buffer.
1057169691Skan */
1058169691Skanstatic void *ti_jalloc(sc)
1059169691Skan	struct ti_softc		*sc;
1060169691Skan{
1061169691Skan	struct ti_jpool_entry   *entry;
1062169691Skan
106397403Sobrien	entry = SLIST_FIRST(&sc->ti_jfree_listhead);
1064169691Skan
1065132720Skan	if (entry == NULL) {
1066132720Skan		printf("ti%d: no free jumbo buffers\n", sc->ti_unit);
1067169691Skan		return(NULL);
1068169691Skan	}
1069169691Skan
1070169691Skan	SLIST_REMOVE_HEAD(&sc->ti_jfree_listhead, jpool_entries);
1071169691Skan	SLIST_INSERT_HEAD(&sc->ti_jinuse_listhead, entry, jpool_entries);
1072169691Skan	return(sc->ti_cdata.ti_jslots[entry->slot]);
1073169691Skan}
1074169691Skan
1075169691Skan/*
1076169691Skan * Release a jumbo buffer.
1077169691Skan */
107897403Sobrienstatic void
1079169691Skanti_jfree(buf, args)
1080169691Skan	void			*buf;
1081169691Skan	void			*args;
1082169691Skan{
1083169691Skan	struct ti_softc		*sc;
1084169691Skan	int		        i;
1085169691Skan	struct ti_jpool_entry   *entry;
1086169691Skan
1087169691Skan	/* Extract the softc struct pointer. */
1088169691Skan	sc = (struct ti_softc *)args;
1089169691Skan
1090169691Skan	if (sc == NULL)
1091169691Skan		panic("ti_jfree: didn't get softc pointer!");
1092169691Skan
1093169691Skan	/* calculate the slot this buffer belongs to */
1094169691Skan	i = ((vm_offset_t)buf
109597403Sobrien	     - (vm_offset_t)sc->ti_cdata.ti_jumbo_buf) / TI_JLEN;
109697403Sobrien
1097132720Skan	if ((i < 0) || (i >= TI_JSLOTS))
109897403Sobrien		panic("ti_jfree: asked to free buffer that we don't manage!");
1099169691Skan
1100169691Skan	entry = SLIST_FIRST(&sc->ti_jinuse_listhead);
1101132720Skan	if (entry == NULL)
110297403Sobrien		panic("ti_jfree: buffer not in use!");
1103169691Skan	entry->slot = i;
110497403Sobrien	SLIST_REMOVE_HEAD(&sc->ti_jinuse_listhead, jpool_entries);
1105169691Skan	SLIST_INSERT_HEAD(&sc->ti_jfree_listhead, entry, jpool_entries);
1106169691Skan
1107169691Skan	return;
1108169691Skan}
1109169691Skan
1110169691Skan#endif /* TI_PRIVATE_JUMBOS */
1111169691Skan
1112169691Skan/*
1113169691Skan * Intialize a standard receive ring descriptor.
1114169691Skan */
1115169691Skanstatic int
1116169691Skanti_newbuf_std(sc, i, m)
1117169691Skan	struct ti_softc		*sc;
1118169691Skan	int			i;
1119169691Skan	struct mbuf		*m;
1120169691Skan{
1121169691Skan	struct mbuf		*m_new = NULL;
1122169691Skan	struct ti_rx_desc	*r;
1123169691Skan
1124169691Skan	if (m == NULL) {
112597403Sobrien		MGETHDR(m_new, M_DONTWAIT, MT_DATA);
112697403Sobrien		if (m_new == NULL)
1127132720Skan			return(ENOBUFS);
112897403Sobrien
1129169691Skan		MCLGET(m_new, M_DONTWAIT);
1130169691Skan		if (!(m_new->m_flags & M_EXT)) {
1131169691Skan			m_freem(m_new);
113297403Sobrien			return(ENOBUFS);
1133169691Skan		}
1134169691Skan		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
1135132720Skan	} else {
1136132720Skan		m_new = m;
1137169691Skan		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
1138169691Skan		m_new->m_data = m_new->m_ext.ext_buf;
1139169691Skan	}
114097403Sobrien
1141169691Skan	m_adj(m_new, ETHER_ALIGN);
1142132720Skan	sc->ti_cdata.ti_rx_std_chain[i] = m_new;
1143169691Skan	r = &sc->ti_rdata->ti_rx_std_ring[i];
1144169691Skan	TI_HOSTADDR(r->ti_addr) = vtophys(mtod(m_new, caddr_t));
114597403Sobrien	r->ti_type = TI_BDTYPE_RECV_BD;
1146169691Skan	r->ti_flags = 0;
1147169691Skan	if (sc->arpcom.ac_if.if_hwassist)
1148169691Skan		r->ti_flags |= TI_BDFLAG_TCP_UDP_CKSUM | TI_BDFLAG_IP_CKSUM;
1149169691Skan	r->ti_len = m_new->m_len;
1150169691Skan	r->ti_idx = i;
1151169691Skan
1152169691Skan	return(0);
1153169691Skan}
1154169691Skan
1155169691Skan/*
1156169691Skan * Intialize a mini receive ring descriptor. This only applies to
1157169691Skan * the Tigon 2.
1158169691Skan */
115997403Sobrienstatic int
1160169691Skanti_newbuf_mini(sc, i, m)
1161132720Skan	struct ti_softc		*sc;
1162132720Skan	int			i;
116397403Sobrien	struct mbuf		*m;
1164169691Skan{
1165169691Skan	struct mbuf		*m_new = NULL;
1166169691Skan	struct ti_rx_desc	*r;
1167169691Skan
1168169691Skan	if (m == NULL) {
1169169691Skan		MGETHDR(m_new, M_DONTWAIT, MT_DATA);
117097403Sobrien		if (m_new == NULL) {
1171169691Skan			return(ENOBUFS);
1172169691Skan		}
1173169691Skan		m_new->m_len = m_new->m_pkthdr.len = MHLEN;
1174169691Skan	} else {
1175169691Skan		m_new = m;
1176169691Skan		m_new->m_data = m_new->m_pktdat;
1177169691Skan		m_new->m_len = m_new->m_pkthdr.len = MHLEN;
1178169691Skan	}
1179169691Skan
1180169691Skan	m_adj(m_new, ETHER_ALIGN);
1181169691Skan	r = &sc->ti_rdata->ti_rx_mini_ring[i];
1182169691Skan	sc->ti_cdata.ti_rx_mini_chain[i] = m_new;
1183169691Skan	TI_HOSTADDR(r->ti_addr) = vtophys(mtod(m_new, caddr_t));
1184169691Skan	r->ti_type = TI_BDTYPE_RECV_BD;
1185169691Skan	r->ti_flags = TI_BDFLAG_MINI_RING;
1186169691Skan	if (sc->arpcom.ac_if.if_hwassist)
1187169691Skan		r->ti_flags |= TI_BDFLAG_TCP_UDP_CKSUM | TI_BDFLAG_IP_CKSUM;
1188169691Skan	r->ti_len = m_new->m_len;
1189169691Skan	r->ti_idx = i;
1190169691Skan
1191169691Skan	return(0);
1192169691Skan}
1193169691Skan
1194169691Skan#ifdef TI_PRIVATE_JUMBOS
1195169691Skan
1196169691Skan/*
1197169691Skan * Initialize a jumbo receive ring descriptor. This allocates
1198169691Skan * a jumbo buffer from the pool managed internally by the driver.
1199169691Skan */
1200169691Skanstatic int
1201169691Skanti_newbuf_jumbo(sc, i, m)
1202169691Skan	struct ti_softc		*sc;
1203169691Skan	int			i;
1204169691Skan	struct mbuf		*m;
1205169691Skan{
1206169691Skan	struct mbuf		*m_new = NULL;
120797403Sobrien	struct ti_rx_desc	*r;
1208132720Skan
120997403Sobrien	if (m == NULL) {
1210169691Skan		caddr_t			*buf = NULL;
1211169691Skan
1212132720Skan		/* Allocate the mbuf. */
121397403Sobrien		MGETHDR(m_new, M_DONTWAIT, MT_DATA);
1214169691Skan		if (m_new == NULL) {
121597403Sobrien			return(ENOBUFS);
1216169691Skan		}
1217169691Skan
1218169691Skan		/* Allocate the jumbo buffer */
1219169691Skan		buf = ti_jalloc(sc);
1220169691Skan		if (buf == NULL) {
1221169691Skan			m_freem(m_new);
1222169691Skan			printf("ti%d: jumbo allocation failed "
1223169691Skan			    "-- packet dropped!\n", sc->ti_unit);
1224169691Skan			return(ENOBUFS);
1225169691Skan		}
1226169691Skan
1227169691Skan		/* Attach the buffer to the mbuf. */
1228169691Skan		m_new->m_data = (void *) buf;
1229169691Skan		m_new->m_len = m_new->m_pkthdr.len = TI_JUMBO_FRAMELEN;
1230169691Skan		MEXTADD(m_new, buf, TI_JUMBO_FRAMELEN, ti_jfree,
1231169691Skan		    (struct ti_softc *)sc, 0, EXT_NET_DRV);
1232169691Skan	} else {
123397403Sobrien		m_new = m;
123497403Sobrien		m_new->m_data = m_new->m_ext.ext_buf;
1235132720Skan		m_new->m_ext.ext_size = TI_JUMBO_FRAMELEN;
123697403Sobrien	}
123797403Sobrien
1238132720Skan	m_adj(m_new, ETHER_ALIGN);
1239169691Skan	/* Set up the descriptor. */
1240169691Skan	r = &sc->ti_rdata->ti_rx_jumbo_ring[i];
124197403Sobrien	sc->ti_cdata.ti_rx_jumbo_chain[i] = m_new;
1242169691Skan	TI_HOSTADDR(r->ti_addr) = vtophys(mtod(m_new, caddr_t));
1243169691Skan	r->ti_type = TI_BDTYPE_RECV_JUMBO_BD;
124497403Sobrien	r->ti_flags = TI_BDFLAG_JUMBO_RING;
124597403Sobrien	if (sc->arpcom.ac_if.if_hwassist)
1246132720Skan		r->ti_flags |= TI_BDFLAG_TCP_UDP_CKSUM | TI_BDFLAG_IP_CKSUM;
1247132720Skan	r->ti_len = m_new->m_len;
124897403Sobrien	r->ti_idx = i;
1249169691Skan
1250169691Skan	return(0);
1251169691Skan}
1252169691Skan
1253169691Skan#else
1254169691Skan#include <vm/vm_page.h>
1255169691Skan
1256169691Skan#if (PAGE_SIZE == 4096)
1257169691Skan#define NPAYLOAD 2
1258169691Skan#else
1259169691Skan#define NPAYLOAD 1
1260169691Skan#endif
1261169691Skan
126297403Sobrien#define TCP_HDR_LEN (52 + sizeof(struct ether_header))
1263169691Skan#define UDP_HDR_LEN (28 + sizeof(struct ether_header))
1264169691Skan#define NFS_HDR_LEN (UDP_HDR_LEN)
1265169691Skanstatic int HDR_LEN =  TCP_HDR_LEN;
1266169691Skan
1267169691Skan
1268169691Skan /*
126997403Sobrien  * Initialize a jumbo receive ring descriptor. This allocates
127097403Sobrien  * a jumbo buffer from the pool managed internally by the driver.
1271132720Skan  */
127297403Sobrienstatic int
1273132720Skanti_newbuf_jumbo(sc, idx, m_old)
1274169691Skan        struct ti_softc         *sc;
1275169691Skan        int                     idx;
127697403Sobrien        struct mbuf             *m_old;
1277132720Skan{
1278169691Skan	struct mbuf		*cur, *m_new = NULL;
1279169691Skan	struct mbuf		*m[3] = {NULL, NULL, NULL};
1280169691Skan	struct ti_rx_desc_ext	*r;
1281169691Skan	vm_page_t		frame;
1282132720Skan				/* 1 extra buf to make nobufs easy*/
128397403Sobrien	caddr_t			buf[3] = {NULL, NULL, NULL};
128497403Sobrien	int			i;
1285132720Skan
128697403Sobrien	if (m_old != NULL) {
1287169691Skan		m_new = m_old;
1288169691Skan		cur = m_old->m_next;
1289169691Skan		for (i = 0; i <= NPAYLOAD; i++){
129097403Sobrien			m[i] = cur;
1291169691Skan			cur = cur->m_next;
1292169691Skan		}
129397403Sobrien	} else {
1294169691Skan		/* Allocate the mbufs. */
129597403Sobrien		MGETHDR(m_new, M_DONTWAIT, MT_DATA);
129697403Sobrien		if (m_new == NULL) {
1297132720Skan			printf("ti%d: mbuf allocation failed "
129897403Sobrien   			       "-- packet dropped!\n", sc->ti_unit);
1299132720Skan			goto nobufs;
1300169691Skan		}
1301132720Skan		MGET(m[NPAYLOAD], M_DONTWAIT, MT_DATA);
130297403Sobrien		if (m[NPAYLOAD] == NULL) {
130397403Sobrien			printf("ti%d: cluster mbuf allocation failed "
130497403Sobrien			       "-- packet dropped!\n", sc->ti_unit);
130597403Sobrien			goto nobufs;
1306132720Skan		}
1307132720Skan		MCLGET(m[NPAYLOAD], M_DONTWAIT);
130897403Sobrien		if ((m[NPAYLOAD]->m_flags & M_EXT) == 0) {
130997403Sobrien			printf("ti%d: mbuf allocation failed "
131097403Sobrien			       "-- packet dropped!\n", sc->ti_unit);
131197403Sobrien			goto nobufs;
131297403Sobrien		}
1313132720Skan		m[NPAYLOAD]->m_len = MCLBYTES;
1314132720Skan
131597403Sobrien		for (i = 0; i < NPAYLOAD; i++){
131697403Sobrien			MGET(m[i], M_DONTWAIT, MT_DATA);
131797403Sobrien			if (m[i] == NULL) {
131897403Sobrien				printf("ti%d: mbuf allocation failed "
131997403Sobrien				       "-- packet dropped!\n", sc->ti_unit);
132097403Sobrien				goto nobufs;
132197403Sobrien			}
132297403Sobrien			if (!(frame = jumbo_pg_alloc())){
132397403Sobrien  				printf("ti%d: buffer allocation failed "
132497403Sobrien   				       "-- packet dropped!\n", sc->ti_unit);
132597403Sobrien				printf("      index %d page %d\n", idx, i);
132697403Sobrien   				goto nobufs;
132797403Sobrien			}
1328132720Skan			buf[i] = jumbo_phys_to_kva(VM_PAGE_TO_PHYS(frame));
132997403Sobrien		}
133097403Sobrien		for (i = 0; i < NPAYLOAD; i++){
133197403Sobrien  		/* Attach the buffer to the mbuf. */
133297403Sobrien   			m[i]->m_data = (void *)buf[i];
1333132720Skan			m[i]->m_len = PAGE_SIZE;
133497403Sobrien			MEXTADD(m[i], (void *)buf[i], PAGE_SIZE,
1335132720Skan				jumbo_freem, NULL, 0, EXT_DISPOSABLE);
1336169691Skan			m[i]->m_next = m[i+1];
1337169691Skan		}
133897403Sobrien		/* link the buffers to the header */
133997403Sobrien		m_new->m_next = m[0];
1340132720Skan		m_new->m_data += ETHER_ALIGN;
134197403Sobrien		if (sc->ti_hdrsplit)
134297403Sobrien			m_new->m_len = MHLEN - ETHER_ALIGN;
134397403Sobrien		else
1344169691Skan   			m_new->m_len = HDR_LEN;
134597403Sobrien		m_new->m_pkthdr.len = NPAYLOAD * PAGE_SIZE + m_new->m_len;
134697403Sobrien	}
134797403Sobrien
134897403Sobrien	/* Set up the descriptor. */
1349132720Skan	r = &sc->ti_rdata->ti_rx_jumbo_ring[idx];
135097403Sobrien	sc->ti_cdata.ti_rx_jumbo_chain[idx] = m_new;
1351132720Skan	TI_HOSTADDR(r->ti_addr0) = vtophys(mtod(m_new, caddr_t));
1352169691Skan	r->ti_len0 = m_new->m_len;
135397403Sobrien
135497403Sobrien	TI_HOSTADDR(r->ti_addr1) = vtophys(mtod(m[0], caddr_t));
135597403Sobrien	r->ti_len1 = PAGE_SIZE;
135697403Sobrien
135797403Sobrien	TI_HOSTADDR(r->ti_addr2) = vtophys(mtod(m[1], caddr_t));
1358169691Skan	r->ti_len2 = m[1]->m_ext.ext_size; /* could be PAGE_SIZE or MCLBYTES */
1359169691Skan
136097403Sobrien	if (PAGE_SIZE == 4096) {
136197403Sobrien		TI_HOSTADDR(r->ti_addr3) = vtophys(mtod(m[2], caddr_t));
1362132720Skan		r->ti_len3 = MCLBYTES;
136397403Sobrien	} else {
1364132720Skan		r->ti_len3 = 0;
1365169691Skan	}
1366169691Skan        r->ti_type = TI_BDTYPE_RECV_JUMBO_BD;
1367169691Skan
1368169691Skan        r->ti_flags = TI_BDFLAG_JUMBO_RING|TI_RCB_FLAG_USE_EXT_RX_BD;
1369169691Skan
1370169691Skan	if (sc->arpcom.ac_if.if_hwassist)
1371169691Skan		r->ti_flags |= TI_BDFLAG_TCP_UDP_CKSUM|TI_BDFLAG_IP_CKSUM;
1372169691Skan
1373169691Skan        r->ti_idx = idx;
1374169691Skan
1375169691Skan        return(0);
1376169691Skan
1377169691Skan nobufs:
1378169691Skan
1379132720Skan	/*
1380132720Skan	 * Warning! :
1381132720Skan	 * This can only be called before the mbufs are strung together.
1382132720Skan	 * If the mbufs are strung together, m_freem() will free the chain,
138397403Sobrien	 * so that the later mbufs will be freed multiple times.
138497403Sobrien	 */
1385132720Skan        if (m_new)
138697403Sobrien                m_freem(m_new);
1387169691Skan
1388169691Skan        for(i = 0; i < 3; i++){
1389169691Skan                if (m[i])
139097403Sobrien                        m_freem(m[i]);
1391132720Skan                if (buf[i])
1392132720Skan                        jumbo_pg_free((vm_offset_t)buf[i]);
1393132720Skan        }
1394132720Skan        return ENOBUFS;
1395132720Skan}
139697403Sobrien#endif
139797403Sobrien
139897403Sobrien
1399132720Skan
1400132720Skan/*
1401169691Skan * The standard receive ring has 512 entries in it. At 2K per mbuf cluster,
1402169691Skan * that's 1MB or memory, which is a lot. For now, we fill only the first
1403169691Skan * 256 ring entries and hope that our CPU is fast enough to keep up with
140497403Sobrien * the NIC.
1405132720Skan */
1406132720Skanstatic int
140797403Sobrienti_init_rx_ring_std(sc)
1408169691Skan	struct ti_softc		*sc;
1409169691Skan{
141097403Sobrien	register int		i;
141197403Sobrien	struct ti_cmd_desc	cmd;
1412132720Skan
1413132720Skan	for (i = 0; i < TI_SSLOTS; i++) {
1414132720Skan		if (ti_newbuf_std(sc, i, NULL) == ENOBUFS)
1415132720Skan			return(ENOBUFS);
141697403Sobrien	};
1417132720Skan
141897403Sobrien	TI_UPDATE_STDPROD(sc, i - 1);
141997403Sobrien	sc->ti_std = i - 1;
142097403Sobrien
1421132720Skan	return(0);
1422132720Skan}
1423169691Skan
1424169691Skanstatic void
1425169691Skanti_free_rx_ring_std(sc)
142697403Sobrien	struct ti_softc		*sc;
142797403Sobrien{
1428132720Skan	register int		i;
142997403Sobrien
1430169691Skan	for (i = 0; i < TI_STD_RX_RING_CNT; i++) {
1431169691Skan		if (sc->ti_cdata.ti_rx_std_chain[i] != NULL) {
143297403Sobrien			m_freem(sc->ti_cdata.ti_rx_std_chain[i]);
143397403Sobrien			sc->ti_cdata.ti_rx_std_chain[i] = NULL;
143497403Sobrien		}
1435132720Skan		bzero((char *)&sc->ti_rdata->ti_rx_std_ring[i],
143697403Sobrien		    sizeof(struct ti_rx_desc));
143797403Sobrien	}
143897403Sobrien
1439132720Skan	return;
144097403Sobrien}
1441169691Skan
1442169691Skanstatic int
144397403Sobrienti_init_rx_ring_jumbo(sc)
144497403Sobrien	struct ti_softc		*sc;
1445132720Skan{
1446132720Skan	register int		i;
1447132720Skan	struct ti_cmd_desc	cmd;
1448132720Skan
1449132720Skan	for (i = 0; i < TI_JUMBO_RX_RING_CNT; i++) {
145097403Sobrien		if (ti_newbuf_jumbo(sc, i, NULL) == ENOBUFS)
145197403Sobrien			return(ENOBUFS);
145297403Sobrien	};
1453132720Skan
145497403Sobrien	TI_UPDATE_JUMBOPROD(sc, i - 1);
145597403Sobrien	sc->ti_jumbo = i - 1;
145697403Sobrien
1457132720Skan	return(0);
145897403Sobrien}
1459169691Skan
1460169691Skanstatic void
146197403Sobrienti_free_rx_ring_jumbo(sc)
146297403Sobrien	struct ti_softc		*sc;
1463132720Skan{
1464132720Skan	register int		i;
1465132720Skan
1466132720Skan	for (i = 0; i < TI_JUMBO_RX_RING_CNT; i++) {
1467132720Skan		if (sc->ti_cdata.ti_rx_jumbo_chain[i] != NULL) {
146897403Sobrien			m_freem(sc->ti_cdata.ti_rx_jumbo_chain[i]);
146997403Sobrien			sc->ti_cdata.ti_rx_jumbo_chain[i] = NULL;
147097403Sobrien		}
1471132720Skan		bzero((char *)&sc->ti_rdata->ti_rx_jumbo_ring[i],
147297403Sobrien		    sizeof(struct ti_rx_desc));
147397403Sobrien	}
147497403Sobrien
1475132720Skan	return;
147697403Sobrien}
1477169691Skan
1478169691Skanstatic int
147997403Sobrienti_init_rx_ring_mini(sc)
148097403Sobrien	struct ti_softc		*sc;
1481132720Skan{
1482132720Skan	register int		i;
1483132720Skan
1484132720Skan	for (i = 0; i < TI_MSLOTS; i++) {
1485132720Skan		if (ti_newbuf_mini(sc, i, NULL) == ENOBUFS)
148697403Sobrien			return(ENOBUFS);
148797403Sobrien	};
148897403Sobrien
1489132720Skan	TI_UPDATE_MINIPROD(sc, i - 1);
149097403Sobrien	sc->ti_mini = i - 1;
149197403Sobrien
149297403Sobrien	return(0);
1493132720Skan}
149497403Sobrien
1495169691Skanstatic void
1496169691Skanti_free_rx_ring_mini(sc)
149797403Sobrien	struct ti_softc		*sc;
149897403Sobrien{
1499132720Skan	register int		i;
1500132720Skan
1501132720Skan	for (i = 0; i < TI_MINI_RX_RING_CNT; i++) {
1502132720Skan		if (sc->ti_cdata.ti_rx_mini_chain[i] != NULL) {
1503132720Skan			m_freem(sc->ti_cdata.ti_rx_mini_chain[i]);
150497403Sobrien			sc->ti_cdata.ti_rx_mini_chain[i] = NULL;
150597403Sobrien		}
150697403Sobrien		bzero((char *)&sc->ti_rdata->ti_rx_mini_ring[i],
1507132720Skan		    sizeof(struct ti_rx_desc));
150897403Sobrien	}
150997403Sobrien
151097403Sobrien	return;
1511132720Skan}
151297403Sobrien
1513132720Skanstatic void
1514169691Skanti_free_tx_ring(sc)
1515169691Skan	struct ti_softc		*sc;
1516169691Skan{
1517169691Skan	register int		i;
151897403Sobrien
151997403Sobrien	if (sc->ti_rdata->ti_tx_ring == NULL)
152097403Sobrien		return;
1521132720Skan
152297403Sobrien	for (i = 0; i < TI_TX_RING_CNT; i++) {
1523132720Skan		if (sc->ti_cdata.ti_tx_chain[i] != NULL) {
1524132720Skan			m_freem(sc->ti_cdata.ti_tx_chain[i]);
1525132720Skan			sc->ti_cdata.ti_tx_chain[i] = NULL;
1526132720Skan		}
1527132720Skan		bzero((char *)&sc->ti_rdata->ti_tx_ring[i],
1528132720Skan		    sizeof(struct ti_tx_desc));
1529132720Skan	}
1530132720Skan
153197403Sobrien	return;
1532132720Skan}
1533132720Skan
1534132720Skanstatic int
153597403Sobrienti_init_tx_ring(sc)
1536132720Skan	struct ti_softc		*sc;
153797403Sobrien{
1538132720Skan	sc->ti_txcnt = 0;
153997403Sobrien	sc->ti_tx_saved_considx = 0;
154097403Sobrien	CSR_WRITE_4(sc, TI_MB_SENDPROD_IDX, 0);
1541132720Skan	return(0);
1542132720Skan}
1543132720Skan
1544132720Skan/*
1545132720Skan * The Tigon 2 firmware has a new way to add/delete multicast addresses,
1546132720Skan * but we have to support the old way too so that Tigon 1 cards will
1547132720Skan * work.
1548132720Skan */
1549132720Skanstatic void
1550132720Skanti_add_mcast(sc, addr)
1551132720Skan	struct ti_softc		*sc;
1552132720Skan	struct ether_addr	*addr;
1553132720Skan{
1554132720Skan	struct ti_cmd_desc	cmd;
1555132720Skan	u_int16_t		*m;
1556132720Skan	u_int32_t		ext[2] = {0, 0};
1557132720Skan
1558132720Skan	m = (u_int16_t *)&addr->octet[0];
155997403Sobrien
1560132720Skan	switch(sc->ti_hwrev) {
1561132720Skan	case TI_HWREV_TIGON:
156297403Sobrien		CSR_WRITE_4(sc, TI_GCR_MAR0, htons(m[0]));
1563132720Skan		CSR_WRITE_4(sc, TI_GCR_MAR1, (htons(m[1]) << 16) | htons(m[2]));
1564132720Skan		TI_DO_CMD(TI_CMD_ADD_MCAST_ADDR, 0, 0);
1565132720Skan		break;
1566132720Skan	case TI_HWREV_TIGON_II:
1567132720Skan		ext[0] = htons(m[0]);
1568132720Skan		ext[1] = (htons(m[1]) << 16) | htons(m[2]);
1569132720Skan		TI_DO_CMD_EXT(TI_CMD_EXT_ADD_MCAST, 0, 0, (caddr_t)&ext, 2);
1570132720Skan		break;
1571132720Skan	default:
157297403Sobrien		printf("ti%d: unknown hwrev\n", sc->ti_unit);
157397403Sobrien		break;
1574169691Skan	}
1575169691Skan
1576132720Skan	return;
1577}
1578
1579static void
1580ti_del_mcast(sc, addr)
1581	struct ti_softc		*sc;
1582	struct ether_addr	*addr;
1583{
1584	struct ti_cmd_desc	cmd;
1585	u_int16_t		*m;
1586	u_int32_t		ext[2] = {0, 0};
1587
1588	m = (u_int16_t *)&addr->octet[0];
1589
1590	switch(sc->ti_hwrev) {
1591	case TI_HWREV_TIGON:
1592		CSR_WRITE_4(sc, TI_GCR_MAR0, htons(m[0]));
1593		CSR_WRITE_4(sc, TI_GCR_MAR1, (htons(m[1]) << 16) | htons(m[2]));
1594		TI_DO_CMD(TI_CMD_DEL_MCAST_ADDR, 0, 0);
1595		break;
1596	case TI_HWREV_TIGON_II:
1597		ext[0] = htons(m[0]);
1598		ext[1] = (htons(m[1]) << 16) | htons(m[2]);
1599		TI_DO_CMD_EXT(TI_CMD_EXT_DEL_MCAST, 0, 0, (caddr_t)&ext, 2);
1600		break;
1601	default:
1602		printf("ti%d: unknown hwrev\n", sc->ti_unit);
1603		break;
1604	}
1605
1606	return;
1607}
1608
1609/*
1610 * Configure the Tigon's multicast address filter.
1611 *
1612 * The actual multicast table management is a bit of a pain, thanks to
1613 * slight brain damage on the part of both Alteon and us. With our
1614 * multicast code, we are only alerted when the multicast address table
1615 * changes and at that point we only have the current list of addresses:
1616 * we only know the current state, not the previous state, so we don't
1617 * actually know what addresses were removed or added. The firmware has
1618 * state, but we can't get our grubby mits on it, and there is no 'delete
1619 * all multicast addresses' command. Hence, we have to maintain our own
1620 * state so we know what addresses have been programmed into the NIC at
1621 * any given time.
1622 */
1623static void
1624ti_setmulti(sc)
1625	struct ti_softc		*sc;
1626{
1627	struct ifnet		*ifp;
1628	struct ifmultiaddr	*ifma;
1629	struct ti_cmd_desc	cmd;
1630	struct ti_mc_entry	*mc;
1631	u_int32_t		intrs;
1632
1633	ifp = &sc->arpcom.ac_if;
1634
1635	if (ifp->if_flags & IFF_ALLMULTI) {
1636		TI_DO_CMD(TI_CMD_SET_ALLMULTI, TI_CMD_CODE_ALLMULTI_ENB, 0);
1637		return;
1638	} else {
1639		TI_DO_CMD(TI_CMD_SET_ALLMULTI, TI_CMD_CODE_ALLMULTI_DIS, 0);
1640	}
1641
1642	/* Disable interrupts. */
1643	intrs = CSR_READ_4(sc, TI_MB_HOSTINTR);
1644	CSR_WRITE_4(sc, TI_MB_HOSTINTR, 1);
1645
1646	/* First, zot all the existing filters. */
1647	while (SLIST_FIRST(&sc->ti_mc_listhead) != NULL) {
1648		mc = SLIST_FIRST(&sc->ti_mc_listhead);
1649		ti_del_mcast(sc, &mc->mc_addr);
1650		SLIST_REMOVE_HEAD(&sc->ti_mc_listhead, mc_entries);
1651		free(mc, M_DEVBUF);
1652	}
1653
1654	/* Now program new ones. */
1655	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1656		if (ifma->ifma_addr->sa_family != AF_LINK)
1657			continue;
1658		mc = malloc(sizeof(struct ti_mc_entry), M_DEVBUF, M_NOWAIT);
1659		bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
1660		    (char *)&mc->mc_addr, ETHER_ADDR_LEN);
1661		SLIST_INSERT_HEAD(&sc->ti_mc_listhead, mc, mc_entries);
1662		ti_add_mcast(sc, &mc->mc_addr);
1663	}
1664
1665	/* Re-enable interrupts. */
1666	CSR_WRITE_4(sc, TI_MB_HOSTINTR, intrs);
1667
1668	return;
1669}
1670
1671/*
1672 * Check to see if the BIOS has configured us for a 64 bit slot when
1673 * we aren't actually in one. If we detect this condition, we can work
1674 * around it on the Tigon 2 by setting a bit in the PCI state register,
1675 * but for the Tigon 1 we must give up and abort the interface attach.
1676 */
1677static int ti_64bitslot_war(sc)
1678	struct ti_softc		*sc;
1679{
1680	if (!(CSR_READ_4(sc, TI_PCI_STATE) & TI_PCISTATE_32BIT_BUS)) {
1681		CSR_WRITE_4(sc, 0x600, 0);
1682		CSR_WRITE_4(sc, 0x604, 0);
1683		CSR_WRITE_4(sc, 0x600, 0x5555AAAA);
1684		if (CSR_READ_4(sc, 0x604) == 0x5555AAAA) {
1685			if (sc->ti_hwrev == TI_HWREV_TIGON)
1686				return(EINVAL);
1687			else {
1688				TI_SETBIT(sc, TI_PCI_STATE,
1689				    TI_PCISTATE_32BIT_BUS);
1690				return(0);
1691			}
1692		}
1693	}
1694
1695	return(0);
1696}
1697
1698/*
1699 * Do endian, PCI and DMA initialization. Also check the on-board ROM
1700 * self-test results.
1701 */
1702static int
1703ti_chipinit(sc)
1704	struct ti_softc		*sc;
1705{
1706	u_int32_t		cacheline;
1707	u_int32_t		pci_writemax = 0;
1708	u_int32_t		hdrsplit;
1709
1710	/* Initialize link to down state. */
1711	sc->ti_linkstat = TI_EV_CODE_LINK_DOWN;
1712
1713	if (sc->arpcom.ac_if.if_capenable & IFCAP_HWCSUM)
1714		sc->arpcom.ac_if.if_hwassist = TI_CSUM_FEATURES;
1715	else
1716		sc->arpcom.ac_if.if_hwassist = 0;
1717
1718	/* Set endianness before we access any non-PCI registers. */
1719#if BYTE_ORDER == BIG_ENDIAN
1720	CSR_WRITE_4(sc, TI_MISC_HOST_CTL,
1721	    TI_MHC_BIGENDIAN_INIT | (TI_MHC_BIGENDIAN_INIT << 24));
1722#else
1723	CSR_WRITE_4(sc, TI_MISC_HOST_CTL,
1724	    TI_MHC_LITTLEENDIAN_INIT | (TI_MHC_LITTLEENDIAN_INIT << 24));
1725#endif
1726
1727	/* Check the ROM failed bit to see if self-tests passed. */
1728	if (CSR_READ_4(sc, TI_CPU_STATE) & TI_CPUSTATE_ROMFAIL) {
1729		printf("ti%d: board self-diagnostics failed!\n", sc->ti_unit);
1730		return(ENODEV);
1731	}
1732
1733	/* Halt the CPU. */
1734	TI_SETBIT(sc, TI_CPU_STATE, TI_CPUSTATE_HALT);
1735
1736	/* Figure out the hardware revision. */
1737	switch(CSR_READ_4(sc, TI_MISC_HOST_CTL) & TI_MHC_CHIP_REV_MASK) {
1738	case TI_REV_TIGON_I:
1739		sc->ti_hwrev = TI_HWREV_TIGON;
1740		break;
1741	case TI_REV_TIGON_II:
1742		sc->ti_hwrev = TI_HWREV_TIGON_II;
1743		break;
1744	default:
1745		printf("ti%d: unsupported chip revision\n", sc->ti_unit);
1746		return(ENODEV);
1747	}
1748
1749	/* Do special setup for Tigon 2. */
1750	if (sc->ti_hwrev == TI_HWREV_TIGON_II) {
1751		TI_SETBIT(sc, TI_CPU_CTL_B, TI_CPUSTATE_HALT);
1752		TI_SETBIT(sc, TI_MISC_LOCAL_CTL, TI_MLC_SRAM_BANK_512K);
1753		TI_SETBIT(sc, TI_MISC_CONF, TI_MCR_SRAM_SYNCHRONOUS);
1754	}
1755
1756	/*
1757	 * We don't have firmware source for the Tigon 1, so Tigon 1 boards
1758	 * can't do header splitting.
1759	 */
1760#ifdef TI_JUMBO_HDRSPLIT
1761	if (sc->ti_hwrev != TI_HWREV_TIGON)
1762		sc->ti_hdrsplit = 1;
1763	else
1764		printf("ti%d: can't do header splitting on a Tigon I board\n",
1765		       sc->ti_unit);
1766#endif /* TI_JUMBO_HDRSPLIT */
1767
1768	/* Set up the PCI state register. */
1769	CSR_WRITE_4(sc, TI_PCI_STATE, TI_PCI_READ_CMD|TI_PCI_WRITE_CMD);
1770	if (sc->ti_hwrev == TI_HWREV_TIGON_II) {
1771		TI_SETBIT(sc, TI_PCI_STATE, TI_PCISTATE_USE_MEM_RD_MULT);
1772	}
1773
1774	/* Clear the read/write max DMA parameters. */
1775	TI_CLRBIT(sc, TI_PCI_STATE, (TI_PCISTATE_WRITE_MAXDMA|
1776	    TI_PCISTATE_READ_MAXDMA));
1777
1778	/* Get cache line size. */
1779	cacheline = CSR_READ_4(sc, TI_PCI_BIST) & 0xFF;
1780
1781	/*
1782	 * If the system has set enabled the PCI memory write
1783	 * and invalidate command in the command register, set
1784	 * the write max parameter accordingly. This is necessary
1785	 * to use MWI with the Tigon 2.
1786	 */
1787	if (CSR_READ_4(sc, TI_PCI_CMDSTAT) & PCIM_CMD_MWIEN) {
1788		switch(cacheline) {
1789		case 1:
1790		case 4:
1791		case 8:
1792		case 16:
1793		case 32:
1794		case 64:
1795			break;
1796		default:
1797		/* Disable PCI memory write and invalidate. */
1798			if (bootverbose)
1799				printf("ti%d: cache line size %d not "
1800				    "supported; disabling PCI MWI\n",
1801				    sc->ti_unit, cacheline);
1802			CSR_WRITE_4(sc, TI_PCI_CMDSTAT, CSR_READ_4(sc,
1803			    TI_PCI_CMDSTAT) & ~PCIM_CMD_MWIEN);
1804			break;
1805		}
1806	}
1807
1808#ifdef __brokenalpha__
1809	/*
1810	 * From the Alteon sample driver:
1811	 * Must insure that we do not cross an 8K (bytes) boundary
1812	 * for DMA reads.  Our highest limit is 1K bytes.  This is a
1813	 * restriction on some ALPHA platforms with early revision
1814	 * 21174 PCI chipsets, such as the AlphaPC 164lx
1815	 */
1816	TI_SETBIT(sc, TI_PCI_STATE, pci_writemax|TI_PCI_READMAX_1024);
1817#else
1818	TI_SETBIT(sc, TI_PCI_STATE, pci_writemax);
1819#endif
1820
1821	/* This sets the min dma param all the way up (0xff). */
1822	TI_SETBIT(sc, TI_PCI_STATE, TI_PCISTATE_MINDMA);
1823
1824	if (sc->ti_hdrsplit)
1825		hdrsplit = TI_OPMODE_JUMBO_HDRSPLIT;
1826	else
1827		hdrsplit = 0;
1828
1829	/* Configure DMA variables. */
1830#if BYTE_ORDER == BIG_ENDIAN
1831	CSR_WRITE_4(sc, TI_GCR_OPMODE, TI_OPMODE_BYTESWAP_BD |
1832	    TI_OPMODE_BYTESWAP_DATA | TI_OPMODE_WORDSWAP_BD |
1833	    TI_OPMODE_WARN_ENB | TI_OPMODE_FATAL_ENB |
1834	    TI_OPMODE_DONT_FRAG_JUMBO | hdrsplit);
1835#else /* BYTE_ORDER */
1836	CSR_WRITE_4(sc, TI_GCR_OPMODE, TI_OPMODE_BYTESWAP_DATA|
1837	    TI_OPMODE_WORDSWAP_BD|TI_OPMODE_DONT_FRAG_JUMBO|
1838	    TI_OPMODE_WARN_ENB|TI_OPMODE_FATAL_ENB | hdrsplit);
1839#endif /* BYTE_ORDER */
1840
1841	/*
1842	 * Only allow 1 DMA channel to be active at a time.
1843	 * I don't think this is a good idea, but without it
1844	 * the firmware racks up lots of nicDmaReadRingFull
1845	 * errors.  This is not compatible with hardware checksums.
1846	 */
1847	if (sc->arpcom.ac_if.if_hwassist == 0)
1848		TI_SETBIT(sc, TI_GCR_OPMODE, TI_OPMODE_1_DMA_ACTIVE);
1849
1850	/* Recommended settings from Tigon manual. */
1851	CSR_WRITE_4(sc, TI_GCR_DMA_WRITECFG, TI_DMA_STATE_THRESH_8W);
1852	CSR_WRITE_4(sc, TI_GCR_DMA_READCFG, TI_DMA_STATE_THRESH_8W);
1853
1854	if (ti_64bitslot_war(sc)) {
1855		printf("ti%d: bios thinks we're in a 64 bit slot, "
1856		    "but we aren't", sc->ti_unit);
1857		return(EINVAL);
1858	}
1859
1860	return(0);
1861}
1862
1863/*
1864 * Initialize the general information block and firmware, and
1865 * start the CPU(s) running.
1866 */
1867static int
1868ti_gibinit(sc)
1869	struct ti_softc		*sc;
1870{
1871	struct ti_rcb		*rcb;
1872	int			i;
1873	struct ifnet		*ifp;
1874
1875	ifp = &sc->arpcom.ac_if;
1876
1877	/* Disable interrupts for now. */
1878	CSR_WRITE_4(sc, TI_MB_HOSTINTR, 1);
1879
1880	/* Tell the chip where to find the general information block. */
1881	CSR_WRITE_4(sc, TI_GCR_GENINFO_HI, 0);
1882	CSR_WRITE_4(sc, TI_GCR_GENINFO_LO, vtophys(&sc->ti_rdata->ti_info));
1883
1884	/* Load the firmware into SRAM. */
1885	ti_loadfw(sc);
1886
1887	/* Set up the contents of the general info and ring control blocks. */
1888
1889	/* Set up the event ring and producer pointer. */
1890	rcb = &sc->ti_rdata->ti_info.ti_ev_rcb;
1891
1892	TI_HOSTADDR(rcb->ti_hostaddr) = vtophys(&sc->ti_rdata->ti_event_ring);
1893	rcb->ti_flags = 0;
1894	TI_HOSTADDR(sc->ti_rdata->ti_info.ti_ev_prodidx_ptr) =
1895	    vtophys(&sc->ti_ev_prodidx);
1896	sc->ti_ev_prodidx.ti_idx = 0;
1897	CSR_WRITE_4(sc, TI_GCR_EVENTCONS_IDX, 0);
1898	sc->ti_ev_saved_considx = 0;
1899
1900	/* Set up the command ring and producer mailbox. */
1901	rcb = &sc->ti_rdata->ti_info.ti_cmd_rcb;
1902
1903	sc->ti_rdata->ti_cmd_ring =
1904	    (struct ti_cmd_desc *)(sc->ti_vhandle + TI_GCR_CMDRING);
1905	TI_HOSTADDR(rcb->ti_hostaddr) = TI_GCR_NIC_ADDR(TI_GCR_CMDRING);
1906	rcb->ti_flags = 0;
1907	rcb->ti_max_len = 0;
1908	for (i = 0; i < TI_CMD_RING_CNT; i++) {
1909		CSR_WRITE_4(sc, TI_GCR_CMDRING + (i * 4), 0);
1910	}
1911	CSR_WRITE_4(sc, TI_GCR_CMDCONS_IDX, 0);
1912	CSR_WRITE_4(sc, TI_MB_CMDPROD_IDX, 0);
1913	sc->ti_cmd_saved_prodidx = 0;
1914
1915	/*
1916	 * Assign the address of the stats refresh buffer.
1917	 * We re-use the current stats buffer for this to
1918	 * conserve memory.
1919	 */
1920	TI_HOSTADDR(sc->ti_rdata->ti_info.ti_refresh_stats_ptr) =
1921	    vtophys(&sc->ti_rdata->ti_info.ti_stats);
1922
1923	/* Set up the standard receive ring. */
1924	rcb = &sc->ti_rdata->ti_info.ti_std_rx_rcb;
1925	TI_HOSTADDR(rcb->ti_hostaddr) = vtophys(&sc->ti_rdata->ti_rx_std_ring);
1926	rcb->ti_max_len = TI_FRAMELEN;
1927	rcb->ti_flags = 0;
1928	if (sc->arpcom.ac_if.if_hwassist)
1929		rcb->ti_flags |= TI_RCB_FLAG_TCP_UDP_CKSUM |
1930		     TI_RCB_FLAG_IP_CKSUM | TI_RCB_FLAG_NO_PHDR_CKSUM;
1931	rcb->ti_flags |= TI_RCB_FLAG_VLAN_ASSIST;
1932
1933	/* Set up the jumbo receive ring. */
1934	rcb = &sc->ti_rdata->ti_info.ti_jumbo_rx_rcb;
1935	TI_HOSTADDR(rcb->ti_hostaddr) =
1936	    vtophys(&sc->ti_rdata->ti_rx_jumbo_ring);
1937
1938#ifdef TI_PRIVATE_JUMBOS
1939	rcb->ti_max_len = TI_JUMBO_FRAMELEN;
1940	rcb->ti_flags = 0;
1941#else
1942	rcb->ti_max_len = PAGE_SIZE;
1943	rcb->ti_flags = TI_RCB_FLAG_USE_EXT_RX_BD;
1944#endif
1945	if (sc->arpcom.ac_if.if_hwassist)
1946		rcb->ti_flags |= TI_RCB_FLAG_TCP_UDP_CKSUM |
1947		     TI_RCB_FLAG_IP_CKSUM | TI_RCB_FLAG_NO_PHDR_CKSUM;
1948	rcb->ti_flags |= TI_RCB_FLAG_VLAN_ASSIST;
1949
1950	/*
1951	 * Set up the mini ring. Only activated on the
1952	 * Tigon 2 but the slot in the config block is
1953	 * still there on the Tigon 1.
1954	 */
1955	rcb = &sc->ti_rdata->ti_info.ti_mini_rx_rcb;
1956	TI_HOSTADDR(rcb->ti_hostaddr) =
1957	    vtophys(&sc->ti_rdata->ti_rx_mini_ring);
1958	rcb->ti_max_len = MHLEN - ETHER_ALIGN;
1959	if (sc->ti_hwrev == TI_HWREV_TIGON)
1960		rcb->ti_flags = TI_RCB_FLAG_RING_DISABLED;
1961	else
1962		rcb->ti_flags = 0;
1963	if (sc->arpcom.ac_if.if_hwassist)
1964		rcb->ti_flags |= TI_RCB_FLAG_TCP_UDP_CKSUM |
1965		     TI_RCB_FLAG_IP_CKSUM | TI_RCB_FLAG_NO_PHDR_CKSUM;
1966	rcb->ti_flags |= TI_RCB_FLAG_VLAN_ASSIST;
1967
1968	/*
1969	 * Set up the receive return ring.
1970	 */
1971	rcb = &sc->ti_rdata->ti_info.ti_return_rcb;
1972	TI_HOSTADDR(rcb->ti_hostaddr) =
1973	    vtophys(&sc->ti_rdata->ti_rx_return_ring);
1974	rcb->ti_flags = 0;
1975	rcb->ti_max_len = TI_RETURN_RING_CNT;
1976	TI_HOSTADDR(sc->ti_rdata->ti_info.ti_return_prodidx_ptr) =
1977	    vtophys(&sc->ti_return_prodidx);
1978
1979	/*
1980	 * Set up the tx ring. Note: for the Tigon 2, we have the option
1981	 * of putting the transmit ring in the host's address space and
1982	 * letting the chip DMA it instead of leaving the ring in the NIC's
1983	 * memory and accessing it through the shared memory region. We
1984	 * do this for the Tigon 2, but it doesn't work on the Tigon 1,
1985	 * so we have to revert to the shared memory scheme if we detect
1986	 * a Tigon 1 chip.
1987	 */
1988	CSR_WRITE_4(sc, TI_WINBASE, TI_TX_RING_BASE);
1989	if (sc->ti_hwrev == TI_HWREV_TIGON) {
1990		sc->ti_rdata->ti_tx_ring_nic =
1991		    (struct ti_tx_desc *)(sc->ti_vhandle + TI_WINDOW);
1992	}
1993	bzero((char *)sc->ti_rdata->ti_tx_ring,
1994	    TI_TX_RING_CNT * sizeof(struct ti_tx_desc));
1995	rcb = &sc->ti_rdata->ti_info.ti_tx_rcb;
1996	if (sc->ti_hwrev == TI_HWREV_TIGON)
1997		rcb->ti_flags = 0;
1998	else
1999		rcb->ti_flags = TI_RCB_FLAG_HOST_RING;
2000	rcb->ti_flags |= TI_RCB_FLAG_VLAN_ASSIST;
2001	if (sc->arpcom.ac_if.if_hwassist)
2002		rcb->ti_flags |= TI_RCB_FLAG_TCP_UDP_CKSUM |
2003		     TI_RCB_FLAG_IP_CKSUM | TI_RCB_FLAG_NO_PHDR_CKSUM;
2004	rcb->ti_max_len = TI_TX_RING_CNT;
2005	if (sc->ti_hwrev == TI_HWREV_TIGON)
2006		TI_HOSTADDR(rcb->ti_hostaddr) = TI_TX_RING_BASE;
2007	else
2008		TI_HOSTADDR(rcb->ti_hostaddr) =
2009		    vtophys(&sc->ti_rdata->ti_tx_ring);
2010	TI_HOSTADDR(sc->ti_rdata->ti_info.ti_tx_considx_ptr) =
2011	    vtophys(&sc->ti_tx_considx);
2012
2013	/* Set up tuneables */
2014#if 0
2015	if (ifp->if_mtu > (ETHERMTU + ETHER_HDR_LEN + ETHER_CRC_LEN))
2016		CSR_WRITE_4(sc, TI_GCR_RX_COAL_TICKS,
2017		    (sc->ti_rx_coal_ticks / 10));
2018	else
2019#endif
2020		CSR_WRITE_4(sc, TI_GCR_RX_COAL_TICKS, sc->ti_rx_coal_ticks);
2021	CSR_WRITE_4(sc, TI_GCR_TX_COAL_TICKS, sc->ti_tx_coal_ticks);
2022	CSR_WRITE_4(sc, TI_GCR_STAT_TICKS, sc->ti_stat_ticks);
2023	CSR_WRITE_4(sc, TI_GCR_RX_MAX_COAL_BD, sc->ti_rx_max_coal_bds);
2024	CSR_WRITE_4(sc, TI_GCR_TX_MAX_COAL_BD, sc->ti_tx_max_coal_bds);
2025	CSR_WRITE_4(sc, TI_GCR_TX_BUFFER_RATIO, sc->ti_tx_buf_ratio);
2026
2027	/* Turn interrupts on. */
2028	CSR_WRITE_4(sc, TI_GCR_MASK_INTRS, 0);
2029	CSR_WRITE_4(sc, TI_MB_HOSTINTR, 0);
2030
2031	/* Start CPU. */
2032	TI_CLRBIT(sc, TI_CPU_STATE, (TI_CPUSTATE_HALT|TI_CPUSTATE_STEP));
2033
2034	return(0);
2035}
2036
2037/*
2038 * Probe for a Tigon chip. Check the PCI vendor and device IDs
2039 * against our list and return its name if we find a match.
2040 */
2041static int
2042ti_probe(dev)
2043	device_t		dev;
2044{
2045	struct ti_type		*t;
2046
2047	t = ti_devs;
2048
2049	while(t->ti_name != NULL) {
2050		if ((pci_get_vendor(dev) == t->ti_vid) &&
2051		    (pci_get_device(dev) == t->ti_did)) {
2052			device_set_desc(dev, t->ti_name);
2053			return(0);
2054		}
2055		t++;
2056	}
2057
2058	return(ENXIO);
2059}
2060
2061#ifdef KLD_MODULE
2062static int
2063log2rndup(int len)
2064{
2065	int log2size = 0, t = len;
2066	while (t > 1) {
2067		log2size++;
2068		t >>= 1;
2069	}
2070	if (len != (1 << log2size))
2071		log2size++;
2072	return log2size;
2073}
2074
2075static int
2076ti_mbuf_sanity(device_t dev)
2077{
2078	if ((mbstat.m_msize != MSIZE) || mbstat.m_mclbytes != MCLBYTES){
2079		device_printf(dev, "\n");
2080		device_printf(dev, "This module was compiled with "
2081				   "-DMCLSHIFT=%d -DMSIZE=%d\n", MCLSHIFT,
2082				   MSIZE);
2083		device_printf(dev, "The kernel was compiled with MCLSHIFT=%d,"
2084			      " MSIZE=%d\n", log2rndup(mbstat.m_mclbytes),
2085			      (int)mbstat.m_msize);
2086		return(EINVAL);
2087	}
2088	return(0);
2089}
2090#endif
2091
2092
2093static int
2094ti_attach(dev)
2095	device_t		dev;
2096{
2097	u_int32_t		command;
2098	struct ifnet		*ifp;
2099	struct ti_softc		*sc;
2100	int			unit, error = 0, rid;
2101
2102	sc = NULL;
2103
2104#ifdef KLD_MODULE
2105	if (ti_mbuf_sanity(dev)){
2106		device_printf(dev, "Module mbuf constants do not match "
2107			      "kernel constants!\n");
2108		device_printf(dev, "Rebuild the module or the kernel so "
2109			      "they match\n");
2110		device_printf(dev, "\n");
2111		error = EINVAL;
2112		goto fail;
2113	}
2114#endif
2115
2116	sc = device_get_softc(dev);
2117	unit = device_get_unit(dev);
2118
2119	mtx_init(&sc->ti_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK,
2120	    MTX_DEF | MTX_RECURSE);
2121	sc->arpcom.ac_if.if_capabilities = IFCAP_HWCSUM | IFCAP_VLAN_HWTAGGING;
2122	sc->arpcom.ac_if.if_capenable = sc->arpcom.ac_if.if_capabilities;
2123
2124	/*
2125	 * Map control/status registers.
2126	 */
2127	pci_enable_busmaster(dev);
2128	pci_enable_io(dev, SYS_RES_MEMORY);
2129	command = pci_read_config(dev, PCIR_COMMAND, 4);
2130
2131	if (!(command & PCIM_CMD_MEMEN)) {
2132		printf("ti%d: failed to enable memory mapping!\n", unit);
2133		error = ENXIO;
2134		goto fail;
2135	}
2136
2137	rid = TI_PCI_LOMEM;
2138	sc->ti_res = bus_alloc_resource(dev, SYS_RES_MEMORY, &rid,
2139	    0, ~0, 1, RF_ACTIVE|PCI_RF_DENSE);
2140
2141	if (sc->ti_res == NULL) {
2142		printf ("ti%d: couldn't map memory\n", unit);
2143		error = ENXIO;
2144		goto fail;
2145	}
2146
2147	sc->ti_btag = rman_get_bustag(sc->ti_res);
2148	sc->ti_bhandle = rman_get_bushandle(sc->ti_res);
2149	sc->ti_vhandle = (vm_offset_t)rman_get_virtual(sc->ti_res);
2150
2151	/* Allocate interrupt */
2152	rid = 0;
2153
2154	sc->ti_irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
2155	    RF_SHAREABLE | RF_ACTIVE);
2156
2157	if (sc->ti_irq == NULL) {
2158		printf("ti%d: couldn't map interrupt\n", unit);
2159		error = ENXIO;
2160		goto fail;
2161	}
2162
2163	sc->ti_unit = unit;
2164
2165	if (ti_chipinit(sc)) {
2166		printf("ti%d: chip initialization failed\n", sc->ti_unit);
2167		error = ENXIO;
2168		goto fail;
2169	}
2170
2171	/* Zero out the NIC's on-board SRAM. */
2172	ti_mem(sc, 0x2000, 0x100000 - 0x2000,  NULL);
2173
2174	/* Init again -- zeroing memory may have clobbered some registers. */
2175	if (ti_chipinit(sc)) {
2176		printf("ti%d: chip initialization failed\n", sc->ti_unit);
2177		error = ENXIO;
2178		goto fail;
2179	}
2180
2181	/*
2182	 * Get station address from the EEPROM. Note: the manual states
2183	 * that the MAC address is at offset 0x8c, however the data is
2184	 * stored as two longwords (since that's how it's loaded into
2185	 * the NIC). This means the MAC address is actually preceded
2186	 * by two zero bytes. We need to skip over those.
2187	 */
2188	if (ti_read_eeprom(sc, (caddr_t)&sc->arpcom.ac_enaddr,
2189				TI_EE_MAC_OFFSET + 2, ETHER_ADDR_LEN)) {
2190		printf("ti%d: failed to read station address\n", unit);
2191		error = ENXIO;
2192		goto fail;
2193	}
2194
2195	/*
2196	 * A Tigon chip was detected. Inform the world.
2197	 */
2198	printf("ti%d: Ethernet address: %6D\n", unit,
2199				sc->arpcom.ac_enaddr, ":");
2200
2201	/* Allocate the general information block and ring buffers. */
2202	sc->ti_rdata = contigmalloc(sizeof(struct ti_ring_data), M_DEVBUF,
2203	    M_NOWAIT, 0, 0xffffffff, PAGE_SIZE, 0);
2204
2205	if (sc->ti_rdata == NULL) {
2206		printf("ti%d: no memory for list buffers!\n", sc->ti_unit);
2207		error = ENXIO;
2208		goto fail;
2209	}
2210
2211	bzero(sc->ti_rdata, sizeof(struct ti_ring_data));
2212
2213	/* Try to allocate memory for jumbo buffers. */
2214#ifdef TI_PRIVATE_JUMBOS
2215	if (ti_alloc_jumbo_mem(sc)) {
2216		printf("ti%d: jumbo buffer allocation failed\n", sc->ti_unit);
2217		error = ENXIO;
2218		goto fail;
2219	}
2220#else
2221	if (!jumbo_vm_init()) {
2222		printf("ti%d: VM initialization failed!\n", sc->ti_unit);
2223		error = ENOMEM;
2224		goto fail;
2225	}
2226#endif
2227
2228	/*
2229	 * We really need a better way to tell a 1000baseTX card
2230	 * from a 1000baseSX one, since in theory there could be
2231	 * OEMed 1000baseTX cards from lame vendors who aren't
2232	 * clever enough to change the PCI ID. For the moment
2233	 * though, the AceNIC is the only copper card available.
2234	 */
2235	if (pci_get_vendor(dev) == ALT_VENDORID &&
2236	    pci_get_device(dev) == ALT_DEVICEID_ACENIC_COPPER)
2237		sc->ti_copper = 1;
2238	/* Ok, it's not the only copper card available. */
2239	if (pci_get_vendor(dev) == NG_VENDORID &&
2240	    pci_get_device(dev) == NG_DEVICEID_GA620T)
2241		sc->ti_copper = 1;
2242
2243	/* Set default tuneable values. */
2244	sc->ti_stat_ticks = 2 * TI_TICKS_PER_SEC;
2245#if 0
2246	sc->ti_rx_coal_ticks = TI_TICKS_PER_SEC / 5000;
2247#endif
2248	sc->ti_rx_coal_ticks = 170;
2249	sc->ti_tx_coal_ticks = TI_TICKS_PER_SEC / 500;
2250	sc->ti_rx_max_coal_bds = 64;
2251#if 0
2252	sc->ti_tx_max_coal_bds = 128;
2253#endif
2254	sc->ti_tx_max_coal_bds = 32;
2255	sc->ti_tx_buf_ratio = 21;
2256
2257	/* Set up ifnet structure */
2258	ifp = &sc->arpcom.ac_if;
2259	ifp->if_softc = sc;
2260	ifp->if_unit = sc->ti_unit;
2261	ifp->if_name = "ti";
2262	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
2263	tis[unit] = sc;
2264	ifp->if_ioctl = ti_ioctl;
2265	ifp->if_output = ether_output;
2266	ifp->if_start = ti_start;
2267	ifp->if_watchdog = ti_watchdog;
2268	ifp->if_init = ti_init;
2269	ifp->if_mtu = ETHERMTU;
2270	ifp->if_snd.ifq_maxlen = TI_TX_RING_CNT - 1;
2271
2272	/* Set up ifmedia support. */
2273	ifmedia_init(&sc->ifmedia, IFM_IMASK, ti_ifmedia_upd, ti_ifmedia_sts);
2274	if (sc->ti_copper) {
2275		/*
2276		 * Copper cards allow manual 10/100 mode selection,
2277		 * but not manual 1000baseTX mode selection. Why?
2278		 * Becuase currently there's no way to specify the
2279		 * master/slave setting through the firmware interface,
2280		 * so Alteon decided to just bag it and handle it
2281		 * via autonegotiation.
2282		 */
2283		ifmedia_add(&sc->ifmedia, IFM_ETHER|IFM_10_T, 0, NULL);
2284		ifmedia_add(&sc->ifmedia,
2285		    IFM_ETHER|IFM_10_T|IFM_FDX, 0, NULL);
2286		ifmedia_add(&sc->ifmedia, IFM_ETHER|IFM_100_TX, 0, NULL);
2287		ifmedia_add(&sc->ifmedia,
2288		    IFM_ETHER|IFM_100_TX|IFM_FDX, 0, NULL);
2289		ifmedia_add(&sc->ifmedia, IFM_ETHER|IFM_1000_T, 0, NULL);
2290		ifmedia_add(&sc->ifmedia,
2291		    IFM_ETHER|IFM_1000_T|IFM_FDX, 0, NULL);
2292	} else {
2293		/* Fiber cards don't support 10/100 modes. */
2294		ifmedia_add(&sc->ifmedia, IFM_ETHER|IFM_1000_SX, 0, NULL);
2295		ifmedia_add(&sc->ifmedia,
2296		    IFM_ETHER|IFM_1000_SX|IFM_FDX, 0, NULL);
2297	}
2298	ifmedia_add(&sc->ifmedia, IFM_ETHER|IFM_AUTO, 0, NULL);
2299	ifmedia_set(&sc->ifmedia, IFM_ETHER|IFM_AUTO);
2300
2301	/*
2302	 * We're assuming here that card initialization is a sequential
2303	 * thing.  If it isn't, multiple cards probing at the same time
2304	 * could stomp on the list of softcs here.
2305	 */
2306	/*
2307	 * If this is the first card to be initialized, initialize the
2308	 * softc queue.
2309	 */
2310	if (unit == 0)
2311		STAILQ_INIT(&ti_sc_list);
2312
2313	STAILQ_INSERT_TAIL(&ti_sc_list, sc, ti_links);
2314
2315	/* Register the device */
2316	sc->dev = make_dev(&ti_cdevsw, sc->ti_unit, UID_ROOT, GID_OPERATOR,
2317			   0600, "ti%d", sc->ti_unit);
2318
2319	/*
2320	 * Call MI attach routine.
2321	 */
2322	ether_ifattach(ifp, sc->arpcom.ac_enaddr);
2323
2324	error = bus_setup_intr(dev, sc->ti_irq, INTR_TYPE_NET,
2325	   ti_intr, sc, &sc->ti_intrhand);
2326
2327	if (error) {
2328		printf("ti%d: couldn't set up irq\n", unit);
2329		goto fail;
2330	}
2331
2332fail:
2333	if (sc && error)
2334		ti_detach(dev);
2335
2336	return(error);
2337}
2338
2339/*
2340 * Verify that our character special device is not currently
2341 * open.  Also track down any cached vnodes & kill them before
2342 * the module is unloaded
2343 */
2344static int
2345ti_unref_special(device_t dev)
2346{
2347	struct vnode *ti_vn;
2348	int count;
2349	struct ti_softc *sc = sc = device_get_softc(dev);
2350
2351	if (!vfinddev(sc->dev, VCHR, &ti_vn)) {
2352		return 0;
2353	}
2354
2355	if ((count = vcount(ti_vn))) {
2356		device_printf(dev, "%d refs to special device, "
2357			      "denying unload\n", count);
2358		return count;
2359	}
2360	/* now we know that there's a vnode in the cache. We hunt it
2361	   down and kill it now, before unloading */
2362	vgone(ti_vn);
2363	return(0);
2364}
2365
2366
2367static int
2368ti_detach(dev)
2369	device_t		dev;
2370{
2371	struct ti_softc		*sc;
2372	struct ifnet		*ifp;
2373
2374	if (ti_unref_special(dev))
2375		return EBUSY;
2376
2377	sc = device_get_softc(dev);
2378	KASSERT(mtx_initialized(&sc->ti_mtx), ("ti mutex not initialized"));
2379	TI_LOCK(sc);
2380	ifp = &sc->arpcom.ac_if;
2381
2382	if (device_is_alive(dev)) {
2383		if (bus_child_present(dev))
2384			ti_stop(sc);
2385		ether_ifdetach(ifp);
2386		bus_generic_detach(dev);
2387		ifmedia_removeall(&sc->ifmedia);
2388	}
2389
2390	if (sc->ti_intrhand)
2391		bus_teardown_intr(dev, sc->ti_irq, sc->ti_intrhand);
2392	if (sc->ti_irq)
2393		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->ti_irq);
2394	if (sc->ti_res) {
2395		bus_release_resource(dev, SYS_RES_MEMORY, TI_PCI_LOMEM,
2396		    sc->ti_res);
2397	}
2398
2399#ifdef TI_PRIVATE_JUMBOS
2400	if (sc->ti_cdata.ti_jumbo_buf)
2401		contigfree(sc->ti_cdata.ti_jumbo_buf, TI_JMEM, M_DEVBUF);
2402#endif
2403	if (sc->ti_rdata)
2404		contigfree(sc->ti_rdata, sizeof(struct ti_ring_data), M_DEVBUF);
2405
2406	TI_UNLOCK(sc);
2407	mtx_destroy(&sc->ti_mtx);
2408
2409	return(0);
2410}
2411
2412#ifdef TI_JUMBO_HDRSPLIT
2413/*
2414 * If hdr_len is 0, that means that header splitting wasn't done on
2415 * this packet for some reason.  The two most likely reasons are that
2416 * the protocol isn't a supported protocol for splitting, or this
2417 * packet had a fragment offset that wasn't 0.
2418 *
2419 * The header length, if it is non-zero, will always be the length of
2420 * the headers on the packet, but that length could be longer than the
2421 * first mbuf.  So we take the minimum of the two as the actual
2422 * length.
2423 */
2424static __inline void
2425ti_hdr_split(struct mbuf *top, int hdr_len, int pkt_len, int idx)
2426{
2427	int i = 0;
2428	int lengths[4] = {0, 0, 0, 0};
2429	struct mbuf *m, *mp;
2430
2431	if (hdr_len != 0)
2432		top->m_len = min(hdr_len, top->m_len);
2433	pkt_len -= top->m_len;
2434	lengths[i++] = top->m_len;
2435
2436	mp = top;
2437	for (m = top->m_next; m && pkt_len; m = m->m_next) {
2438		m->m_len = m->m_ext.ext_size = min(m->m_len, pkt_len);
2439		pkt_len -= m->m_len;
2440		lengths[i++] = m->m_len;
2441		mp = m;
2442	}
2443
2444#if 0
2445	if (hdr_len != 0)
2446		printf("got split packet: ");
2447	else
2448		printf("got non-split packet: ");
2449
2450	printf("%d,%d,%d,%d = %d\n", lengths[0],
2451	    lengths[1], lengths[2], lengths[3],
2452	    lengths[0] + lengths[1] + lengths[2] +
2453	    lengths[3]);
2454#endif
2455
2456	if (pkt_len)
2457		panic("header splitting didn't");
2458
2459	if (m) {
2460		m_freem(m);
2461		mp->m_next = NULL;
2462
2463	}
2464	if (mp->m_next != NULL)
2465		panic("ti_hdr_split: last mbuf in chain should be null");
2466}
2467#endif /* TI_JUMBO_HDRSPLIT */
2468
2469/*
2470 * Frame reception handling. This is called if there's a frame
2471 * on the receive return list.
2472 *
2473 * Note: we have to be able to handle three possibilities here:
2474 * 1) the frame is from the mini receive ring (can only happen)
2475 *    on Tigon 2 boards)
2476 * 2) the frame is from the jumbo recieve ring
2477 * 3) the frame is from the standard receive ring
2478 */
2479
2480static void
2481ti_rxeof(sc)
2482	struct ti_softc		*sc;
2483{
2484	struct ifnet		*ifp;
2485	struct ti_cmd_desc	cmd;
2486
2487	ifp = &sc->arpcom.ac_if;
2488
2489	while(sc->ti_rx_saved_considx != sc->ti_return_prodidx.ti_idx) {
2490		struct ti_rx_desc	*cur_rx;
2491		u_int32_t		rxidx;
2492		struct ether_header	*eh;
2493		struct mbuf		*m = NULL;
2494		u_int16_t		vlan_tag = 0;
2495		int			have_tag = 0;
2496
2497		cur_rx =
2498		    &sc->ti_rdata->ti_rx_return_ring[sc->ti_rx_saved_considx];
2499		rxidx = cur_rx->ti_idx;
2500		TI_INC(sc->ti_rx_saved_considx, TI_RETURN_RING_CNT);
2501
2502		if (cur_rx->ti_flags & TI_BDFLAG_VLAN_TAG) {
2503			have_tag = 1;
2504			vlan_tag = cur_rx->ti_vlan_tag & 0xfff;
2505		}
2506
2507		if (cur_rx->ti_flags & TI_BDFLAG_JUMBO_RING) {
2508
2509			TI_INC(sc->ti_jumbo, TI_JUMBO_RX_RING_CNT);
2510			m = sc->ti_cdata.ti_rx_jumbo_chain[rxidx];
2511			sc->ti_cdata.ti_rx_jumbo_chain[rxidx] = NULL;
2512			if (cur_rx->ti_flags & TI_BDFLAG_ERROR) {
2513				ifp->if_ierrors++;
2514				ti_newbuf_jumbo(sc, sc->ti_jumbo, m);
2515				continue;
2516			}
2517			if (ti_newbuf_jumbo(sc, sc->ti_jumbo, NULL) == ENOBUFS) {
2518				ifp->if_ierrors++;
2519				ti_newbuf_jumbo(sc, sc->ti_jumbo, m);
2520				continue;
2521			}
2522#ifdef TI_PRIVATE_JUMBOS
2523                        m->m_len = cur_rx->ti_len;
2524#else /* TI_PRIVATE_JUMBOS */
2525#ifdef TI_JUMBO_HDRSPLIT
2526			if (sc->ti_hdrsplit)
2527				ti_hdr_split(m, TI_HOSTADDR(cur_rx->ti_addr),
2528					     cur_rx->ti_len, rxidx);
2529			else
2530#endif /* TI_JUMBO_HDRSPLIT */
2531                        	m_adj(m, cur_rx->ti_len - m->m_pkthdr.len);
2532#endif /* TI_PRIVATE_JUMBOS */
2533		} else if (cur_rx->ti_flags & TI_BDFLAG_MINI_RING) {
2534			TI_INC(sc->ti_mini, TI_MINI_RX_RING_CNT);
2535			m = sc->ti_cdata.ti_rx_mini_chain[rxidx];
2536			sc->ti_cdata.ti_rx_mini_chain[rxidx] = NULL;
2537			if (cur_rx->ti_flags & TI_BDFLAG_ERROR) {
2538				ifp->if_ierrors++;
2539				ti_newbuf_mini(sc, sc->ti_mini, m);
2540				continue;
2541			}
2542			if (ti_newbuf_mini(sc, sc->ti_mini, NULL) == ENOBUFS) {
2543				ifp->if_ierrors++;
2544				ti_newbuf_mini(sc, sc->ti_mini, m);
2545				continue;
2546			}
2547			m->m_len = cur_rx->ti_len;
2548		} else {
2549			TI_INC(sc->ti_std, TI_STD_RX_RING_CNT);
2550			m = sc->ti_cdata.ti_rx_std_chain[rxidx];
2551			sc->ti_cdata.ti_rx_std_chain[rxidx] = NULL;
2552			if (cur_rx->ti_flags & TI_BDFLAG_ERROR) {
2553				ifp->if_ierrors++;
2554				ti_newbuf_std(sc, sc->ti_std, m);
2555				continue;
2556			}
2557			if (ti_newbuf_std(sc, sc->ti_std, NULL) == ENOBUFS) {
2558				ifp->if_ierrors++;
2559				ti_newbuf_std(sc, sc->ti_std, m);
2560				continue;
2561			}
2562			m->m_len = cur_rx->ti_len;
2563		}
2564
2565		m->m_pkthdr.len = cur_rx->ti_len;
2566		ifp->if_ipackets++;
2567		eh = mtod(m, struct ether_header *);
2568		m->m_pkthdr.rcvif = ifp;
2569
2570		if (ifp->if_hwassist) {
2571			m->m_pkthdr.csum_flags |= CSUM_IP_CHECKED |
2572			    CSUM_DATA_VALID;
2573			if ((cur_rx->ti_ip_cksum ^ 0xffff) == 0)
2574				m->m_pkthdr.csum_flags |= CSUM_IP_VALID;
2575			m->m_pkthdr.csum_data = cur_rx->ti_tcp_udp_cksum;
2576		}
2577
2578		/*
2579		 * If we received a packet with a vlan tag,
2580		 * tag it before passing the packet upward.
2581		 */
2582		if (have_tag)
2583			VLAN_INPUT_TAG(ifp, m, vlan_tag, continue);
2584		(*ifp->if_input)(ifp, m);
2585	}
2586
2587	/* Only necessary on the Tigon 1. */
2588	if (sc->ti_hwrev == TI_HWREV_TIGON)
2589		CSR_WRITE_4(sc, TI_GCR_RXRETURNCONS_IDX,
2590		    sc->ti_rx_saved_considx);
2591
2592	TI_UPDATE_STDPROD(sc, sc->ti_std);
2593	TI_UPDATE_MINIPROD(sc, sc->ti_mini);
2594	TI_UPDATE_JUMBOPROD(sc, sc->ti_jumbo);
2595
2596	return;
2597}
2598
2599static void
2600ti_txeof(sc)
2601	struct ti_softc		*sc;
2602{
2603	struct ti_tx_desc	*cur_tx = NULL;
2604	struct ifnet		*ifp;
2605
2606	ifp = &sc->arpcom.ac_if;
2607
2608	/*
2609	 * Go through our tx ring and free mbufs for those
2610	 * frames that have been sent.
2611	 */
2612	while (sc->ti_tx_saved_considx != sc->ti_tx_considx.ti_idx) {
2613		u_int32_t		idx = 0;
2614
2615		idx = sc->ti_tx_saved_considx;
2616		if (sc->ti_hwrev == TI_HWREV_TIGON) {
2617			if (idx > 383)
2618				CSR_WRITE_4(sc, TI_WINBASE,
2619				    TI_TX_RING_BASE + 6144);
2620			else if (idx > 255)
2621				CSR_WRITE_4(sc, TI_WINBASE,
2622				    TI_TX_RING_BASE + 4096);
2623			else if (idx > 127)
2624				CSR_WRITE_4(sc, TI_WINBASE,
2625				    TI_TX_RING_BASE + 2048);
2626			else
2627				CSR_WRITE_4(sc, TI_WINBASE,
2628				    TI_TX_RING_BASE);
2629			cur_tx = &sc->ti_rdata->ti_tx_ring_nic[idx % 128];
2630		} else
2631			cur_tx = &sc->ti_rdata->ti_tx_ring[idx];
2632		if (cur_tx->ti_flags & TI_BDFLAG_END)
2633			ifp->if_opackets++;
2634		if (sc->ti_cdata.ti_tx_chain[idx] != NULL) {
2635			m_freem(sc->ti_cdata.ti_tx_chain[idx]);
2636			sc->ti_cdata.ti_tx_chain[idx] = NULL;
2637		}
2638		sc->ti_txcnt--;
2639		TI_INC(sc->ti_tx_saved_considx, TI_TX_RING_CNT);
2640		ifp->if_timer = 0;
2641	}
2642
2643	if (cur_tx != NULL)
2644		ifp->if_flags &= ~IFF_OACTIVE;
2645
2646	return;
2647}
2648
2649static void
2650ti_intr(xsc)
2651	void			*xsc;
2652{
2653	struct ti_softc		*sc;
2654	struct ifnet		*ifp;
2655
2656	sc = xsc;
2657	TI_LOCK(sc);
2658	ifp = &sc->arpcom.ac_if;
2659
2660/*#ifdef notdef*/
2661	/* Avoid this for now -- checking this register is expensive. */
2662	/* Make sure this is really our interrupt. */
2663	if (!(CSR_READ_4(sc, TI_MISC_HOST_CTL) & TI_MHC_INTSTATE)) {
2664		TI_UNLOCK(sc);
2665		return;
2666	}
2667/*#endif*/
2668
2669	/* Ack interrupt and stop others from occuring. */
2670	CSR_WRITE_4(sc, TI_MB_HOSTINTR, 1);
2671
2672	if (ifp->if_flags & IFF_RUNNING) {
2673		/* Check RX return ring producer/consumer */
2674		ti_rxeof(sc);
2675
2676		/* Check TX ring producer/consumer */
2677		ti_txeof(sc);
2678	}
2679
2680	ti_handle_events(sc);
2681
2682	/* Re-enable interrupts. */
2683	CSR_WRITE_4(sc, TI_MB_HOSTINTR, 0);
2684
2685	if (ifp->if_flags & IFF_RUNNING && ifp->if_snd.ifq_head != NULL)
2686		ti_start(ifp);
2687
2688	TI_UNLOCK(sc);
2689
2690	return;
2691}
2692
2693static void
2694ti_stats_update(sc)
2695	struct ti_softc		*sc;
2696{
2697	struct ifnet		*ifp;
2698
2699	ifp = &sc->arpcom.ac_if;
2700
2701	ifp->if_collisions +=
2702	   (sc->ti_rdata->ti_info.ti_stats.dot3StatsSingleCollisionFrames +
2703	   sc->ti_rdata->ti_info.ti_stats.dot3StatsMultipleCollisionFrames +
2704	   sc->ti_rdata->ti_info.ti_stats.dot3StatsExcessiveCollisions +
2705	   sc->ti_rdata->ti_info.ti_stats.dot3StatsLateCollisions) -
2706	   ifp->if_collisions;
2707
2708	return;
2709}
2710
2711/*
2712 * Encapsulate an mbuf chain in the tx ring  by coupling the mbuf data
2713 * pointers to descriptors.
2714 */
2715static int
2716ti_encap(sc, m_head, txidx)
2717	struct ti_softc		*sc;
2718	struct mbuf		*m_head;
2719	u_int32_t		*txidx;
2720{
2721	struct ti_tx_desc	*f = NULL;
2722	struct mbuf		*m;
2723	u_int32_t		frag, cur, cnt = 0;
2724	u_int16_t		csum_flags = 0;
2725	struct m_tag		*mtag;
2726
2727	m = m_head;
2728	cur = frag = *txidx;
2729
2730	if (m_head->m_pkthdr.csum_flags) {
2731		if (m_head->m_pkthdr.csum_flags & CSUM_IP)
2732			csum_flags |= TI_BDFLAG_IP_CKSUM;
2733		if (m_head->m_pkthdr.csum_flags & (CSUM_TCP | CSUM_UDP))
2734			csum_flags |= TI_BDFLAG_TCP_UDP_CKSUM;
2735		if (m_head->m_flags & M_LASTFRAG)
2736			csum_flags |= TI_BDFLAG_IP_FRAG_END;
2737		else if (m_head->m_flags & M_FRAG)
2738			csum_flags |= TI_BDFLAG_IP_FRAG;
2739	}
2740
2741	mtag = VLAN_OUTPUT_TAG(&sc->arpcom.ac_if, m);
2742
2743	/*
2744 	 * Start packing the mbufs in this chain into
2745	 * the fragment pointers. Stop when we run out
2746 	 * of fragments or hit the end of the mbuf chain.
2747	 */
2748	for (m = m_head; m != NULL; m = m->m_next) {
2749		if (m->m_len != 0) {
2750			if (sc->ti_hwrev == TI_HWREV_TIGON) {
2751				if (frag > 383)
2752					CSR_WRITE_4(sc, TI_WINBASE,
2753					    TI_TX_RING_BASE + 6144);
2754				else if (frag > 255)
2755					CSR_WRITE_4(sc, TI_WINBASE,
2756					    TI_TX_RING_BASE + 4096);
2757				else if (frag > 127)
2758					CSR_WRITE_4(sc, TI_WINBASE,
2759					    TI_TX_RING_BASE + 2048);
2760				else
2761					CSR_WRITE_4(sc, TI_WINBASE,
2762					    TI_TX_RING_BASE);
2763				f = &sc->ti_rdata->ti_tx_ring_nic[frag % 128];
2764			} else
2765				f = &sc->ti_rdata->ti_tx_ring[frag];
2766			if (sc->ti_cdata.ti_tx_chain[frag] != NULL)
2767				break;
2768			TI_HOSTADDR(f->ti_addr) = vtophys(mtod(m, vm_offset_t));
2769			f->ti_len = m->m_len;
2770			f->ti_flags = csum_flags;
2771
2772			if (mtag != NULL) {
2773				f->ti_flags |= TI_BDFLAG_VLAN_TAG;
2774				f->ti_vlan_tag = VLAN_TAG_VALUE(mtag) & 0xfff;
2775			} else {
2776				f->ti_vlan_tag = 0;
2777			}
2778
2779			/*
2780			 * Sanity check: avoid coming within 16 descriptors
2781			 * of the end of the ring.
2782			 */
2783			if ((TI_TX_RING_CNT - (sc->ti_txcnt + cnt)) < 16)
2784				return(ENOBUFS);
2785			cur = frag;
2786			TI_INC(frag, TI_TX_RING_CNT);
2787			cnt++;
2788		}
2789	}
2790
2791	if (m != NULL)
2792		return(ENOBUFS);
2793
2794	if (frag == sc->ti_tx_saved_considx)
2795		return(ENOBUFS);
2796
2797	if (sc->ti_hwrev == TI_HWREV_TIGON)
2798		sc->ti_rdata->ti_tx_ring_nic[cur % 128].ti_flags |=
2799	            TI_BDFLAG_END;
2800	else
2801		sc->ti_rdata->ti_tx_ring[cur].ti_flags |= TI_BDFLAG_END;
2802	sc->ti_cdata.ti_tx_chain[cur] = m_head;
2803	sc->ti_txcnt += cnt;
2804
2805	*txidx = frag;
2806
2807	return(0);
2808}
2809
2810/*
2811 * Main transmit routine. To avoid having to do mbuf copies, we put pointers
2812 * to the mbuf data regions directly in the transmit descriptors.
2813 */
2814static void
2815ti_start(ifp)
2816	struct ifnet		*ifp;
2817{
2818	struct ti_softc		*sc;
2819	struct mbuf		*m_head = NULL;
2820	u_int32_t		prodidx = 0;
2821
2822	sc = ifp->if_softc;
2823	TI_LOCK(sc);
2824
2825	prodidx = CSR_READ_4(sc, TI_MB_SENDPROD_IDX);
2826
2827	while(sc->ti_cdata.ti_tx_chain[prodidx] == NULL) {
2828		IF_DEQUEUE(&ifp->if_snd, m_head);
2829		if (m_head == NULL)
2830			break;
2831
2832		/*
2833		 * XXX
2834		 * safety overkill.  If this is a fragmented packet chain
2835		 * with delayed TCP/UDP checksums, then only encapsulate
2836		 * it if we have enough descriptors to handle the entire
2837		 * chain at once.
2838		 * (paranoia -- may not actually be needed)
2839		 */
2840		if (m_head->m_flags & M_FIRSTFRAG &&
2841		    m_head->m_pkthdr.csum_flags & (CSUM_DELAY_DATA)) {
2842			if ((TI_TX_RING_CNT - sc->ti_txcnt) <
2843			    m_head->m_pkthdr.csum_data + 16) {
2844				IF_PREPEND(&ifp->if_snd, m_head);
2845				ifp->if_flags |= IFF_OACTIVE;
2846				break;
2847			}
2848		}
2849
2850		/*
2851		 * Pack the data into the transmit ring. If we
2852		 * don't have room, set the OACTIVE flag and wait
2853		 * for the NIC to drain the ring.
2854		 */
2855		if (ti_encap(sc, m_head, &prodidx)) {
2856			IF_PREPEND(&ifp->if_snd, m_head);
2857			ifp->if_flags |= IFF_OACTIVE;
2858			break;
2859		}
2860
2861		/*
2862		 * If there's a BPF listener, bounce a copy of this frame
2863		 * to him.
2864		 */
2865		BPF_MTAP(ifp, m_head);
2866	}
2867
2868	/* Transmit */
2869	CSR_WRITE_4(sc, TI_MB_SENDPROD_IDX, prodidx);
2870
2871	/*
2872	 * Set a timeout in case the chip goes out to lunch.
2873	 */
2874	ifp->if_timer = 5;
2875	TI_UNLOCK(sc);
2876
2877	return;
2878}
2879
2880static void
2881ti_init(xsc)
2882	void			*xsc;
2883{
2884	struct ti_softc		*sc = xsc;
2885
2886	/* Cancel pending I/O and flush buffers. */
2887	ti_stop(sc);
2888
2889	TI_LOCK(sc);
2890	/* Init the gen info block, ring control blocks and firmware. */
2891	if (ti_gibinit(sc)) {
2892		printf("ti%d: initialization failure\n", sc->ti_unit);
2893		TI_UNLOCK(sc);
2894		return;
2895	}
2896
2897	TI_UNLOCK(sc);
2898
2899	return;
2900}
2901
2902static void ti_init2(sc)
2903	struct ti_softc		*sc;
2904{
2905	struct ti_cmd_desc	cmd;
2906	struct ifnet		*ifp;
2907	u_int16_t		*m;
2908	struct ifmedia		*ifm;
2909	int			tmp;
2910
2911	ifp = &sc->arpcom.ac_if;
2912
2913	/* Specify MTU and interface index. */
2914	CSR_WRITE_4(sc, TI_GCR_IFINDEX, ifp->if_unit);
2915	CSR_WRITE_4(sc, TI_GCR_IFMTU, ifp->if_mtu +
2916	    ETHER_HDR_LEN + ETHER_CRC_LEN);
2917	TI_DO_CMD(TI_CMD_UPDATE_GENCOM, 0, 0);
2918
2919	/* Load our MAC address. */
2920	m = (u_int16_t *)&sc->arpcom.ac_enaddr[0];
2921	CSR_WRITE_4(sc, TI_GCR_PAR0, htons(m[0]));
2922	CSR_WRITE_4(sc, TI_GCR_PAR1, (htons(m[1]) << 16) | htons(m[2]));
2923	TI_DO_CMD(TI_CMD_SET_MAC_ADDR, 0, 0);
2924
2925	/* Enable or disable promiscuous mode as needed. */
2926	if (ifp->if_flags & IFF_PROMISC) {
2927		TI_DO_CMD(TI_CMD_SET_PROMISC_MODE, TI_CMD_CODE_PROMISC_ENB, 0);
2928	} else {
2929		TI_DO_CMD(TI_CMD_SET_PROMISC_MODE, TI_CMD_CODE_PROMISC_DIS, 0);
2930	}
2931
2932	/* Program multicast filter. */
2933	ti_setmulti(sc);
2934
2935	/*
2936	 * If this is a Tigon 1, we should tell the
2937	 * firmware to use software packet filtering.
2938	 */
2939	if (sc->ti_hwrev == TI_HWREV_TIGON) {
2940		TI_DO_CMD(TI_CMD_FDR_FILTERING, TI_CMD_CODE_FILT_ENB, 0);
2941	}
2942
2943	/* Init RX ring. */
2944	ti_init_rx_ring_std(sc);
2945
2946	/* Init jumbo RX ring. */
2947	if (ifp->if_mtu > (ETHERMTU + ETHER_HDR_LEN + ETHER_CRC_LEN))
2948		ti_init_rx_ring_jumbo(sc);
2949
2950	/*
2951	 * If this is a Tigon 2, we can also configure the
2952	 * mini ring.
2953	 */
2954	if (sc->ti_hwrev == TI_HWREV_TIGON_II)
2955		ti_init_rx_ring_mini(sc);
2956
2957	CSR_WRITE_4(sc, TI_GCR_RXRETURNCONS_IDX, 0);
2958	sc->ti_rx_saved_considx = 0;
2959
2960	/* Init TX ring. */
2961	ti_init_tx_ring(sc);
2962
2963	/* Tell firmware we're alive. */
2964	TI_DO_CMD(TI_CMD_HOST_STATE, TI_CMD_CODE_STACK_UP, 0);
2965
2966	/* Enable host interrupts. */
2967	CSR_WRITE_4(sc, TI_MB_HOSTINTR, 0);
2968
2969	ifp->if_flags |= IFF_RUNNING;
2970	ifp->if_flags &= ~IFF_OACTIVE;
2971
2972	/*
2973	 * Make sure to set media properly. We have to do this
2974	 * here since we have to issue commands in order to set
2975	 * the link negotiation and we can't issue commands until
2976	 * the firmware is running.
2977	 */
2978	ifm = &sc->ifmedia;
2979	tmp = ifm->ifm_media;
2980	ifm->ifm_media = ifm->ifm_cur->ifm_media;
2981	ti_ifmedia_upd(ifp);
2982	ifm->ifm_media = tmp;
2983
2984	return;
2985}
2986
2987/*
2988 * Set media options.
2989 */
2990static int
2991ti_ifmedia_upd(ifp)
2992	struct ifnet		*ifp;
2993{
2994	struct ti_softc		*sc;
2995	struct ifmedia		*ifm;
2996	struct ti_cmd_desc	cmd;
2997	u_int32_t		flowctl;
2998
2999	sc = ifp->if_softc;
3000	ifm = &sc->ifmedia;
3001
3002	if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
3003		return(EINVAL);
3004
3005	flowctl = 0;
3006
3007	switch(IFM_SUBTYPE(ifm->ifm_media)) {
3008	case IFM_AUTO:
3009		/*
3010		 * Transmit flow control doesn't work on the Tigon 1.
3011		 */
3012		flowctl = TI_GLNK_RX_FLOWCTL_Y;
3013
3014		/*
3015		 * Transmit flow control can also cause problems on the
3016		 * Tigon 2, apparantly with both the copper and fiber
3017		 * boards.  The symptom is that the interface will just
3018		 * hang.  This was reproduced with Alteon 180 switches.
3019		 */
3020#if 0
3021		if (sc->ti_hwrev != TI_HWREV_TIGON)
3022			flowctl |= TI_GLNK_TX_FLOWCTL_Y;
3023#endif
3024
3025		CSR_WRITE_4(sc, TI_GCR_GLINK, TI_GLNK_PREF|TI_GLNK_1000MB|
3026		    TI_GLNK_FULL_DUPLEX| flowctl |
3027		    TI_GLNK_AUTONEGENB|TI_GLNK_ENB);
3028
3029		flowctl = TI_LNK_RX_FLOWCTL_Y;
3030#if 0
3031		if (sc->ti_hwrev != TI_HWREV_TIGON)
3032			flowctl |= TI_LNK_TX_FLOWCTL_Y;
3033#endif
3034
3035		CSR_WRITE_4(sc, TI_GCR_LINK, TI_LNK_100MB|TI_LNK_10MB|
3036		    TI_LNK_FULL_DUPLEX|TI_LNK_HALF_DUPLEX| flowctl |
3037		    TI_LNK_AUTONEGENB|TI_LNK_ENB);
3038		TI_DO_CMD(TI_CMD_LINK_NEGOTIATION,
3039		    TI_CMD_CODE_NEGOTIATE_BOTH, 0);
3040		break;
3041	case IFM_1000_SX:
3042	case IFM_1000_T:
3043		flowctl = TI_GLNK_RX_FLOWCTL_Y;
3044#if 0
3045		if (sc->ti_hwrev != TI_HWREV_TIGON)
3046			flowctl |= TI_GLNK_TX_FLOWCTL_Y;
3047#endif
3048
3049		CSR_WRITE_4(sc, TI_GCR_GLINK, TI_GLNK_PREF|TI_GLNK_1000MB|
3050		    flowctl |TI_GLNK_ENB);
3051		CSR_WRITE_4(sc, TI_GCR_LINK, 0);
3052		if ((ifm->ifm_media & IFM_GMASK) == IFM_FDX) {
3053			TI_SETBIT(sc, TI_GCR_GLINK, TI_GLNK_FULL_DUPLEX);
3054		}
3055		TI_DO_CMD(TI_CMD_LINK_NEGOTIATION,
3056		    TI_CMD_CODE_NEGOTIATE_GIGABIT, 0);
3057		break;
3058	case IFM_100_FX:
3059	case IFM_10_FL:
3060	case IFM_100_TX:
3061	case IFM_10_T:
3062		flowctl = TI_LNK_RX_FLOWCTL_Y;
3063#if 0
3064		if (sc->ti_hwrev != TI_HWREV_TIGON)
3065			flowctl |= TI_LNK_TX_FLOWCTL_Y;
3066#endif
3067
3068		CSR_WRITE_4(sc, TI_GCR_GLINK, 0);
3069		CSR_WRITE_4(sc, TI_GCR_LINK, TI_LNK_ENB|TI_LNK_PREF|flowctl);
3070		if (IFM_SUBTYPE(ifm->ifm_media) == IFM_100_FX ||
3071		    IFM_SUBTYPE(ifm->ifm_media) == IFM_100_TX) {
3072			TI_SETBIT(sc, TI_GCR_LINK, TI_LNK_100MB);
3073		} else {
3074			TI_SETBIT(sc, TI_GCR_LINK, TI_LNK_10MB);
3075		}
3076		if ((ifm->ifm_media & IFM_GMASK) == IFM_FDX) {
3077			TI_SETBIT(sc, TI_GCR_LINK, TI_LNK_FULL_DUPLEX);
3078		} else {
3079			TI_SETBIT(sc, TI_GCR_LINK, TI_LNK_HALF_DUPLEX);
3080		}
3081		TI_DO_CMD(TI_CMD_LINK_NEGOTIATION,
3082		    TI_CMD_CODE_NEGOTIATE_10_100, 0);
3083		break;
3084	}
3085
3086	return(0);
3087}
3088
3089/*
3090 * Report current media status.
3091 */
3092static void
3093ti_ifmedia_sts(ifp, ifmr)
3094	struct ifnet		*ifp;
3095	struct ifmediareq	*ifmr;
3096{
3097	struct ti_softc		*sc;
3098	u_int32_t		media = 0;
3099
3100	sc = ifp->if_softc;
3101
3102	ifmr->ifm_status = IFM_AVALID;
3103	ifmr->ifm_active = IFM_ETHER;
3104
3105	if (sc->ti_linkstat == TI_EV_CODE_LINK_DOWN)
3106		return;
3107
3108	ifmr->ifm_status |= IFM_ACTIVE;
3109
3110	if (sc->ti_linkstat == TI_EV_CODE_GIG_LINK_UP) {
3111		media = CSR_READ_4(sc, TI_GCR_GLINK_STAT);
3112		if (sc->ti_copper)
3113			ifmr->ifm_active |= IFM_1000_T;
3114		else
3115			ifmr->ifm_active |= IFM_1000_SX;
3116		if (media & TI_GLNK_FULL_DUPLEX)
3117			ifmr->ifm_active |= IFM_FDX;
3118		else
3119			ifmr->ifm_active |= IFM_HDX;
3120	} else if (sc->ti_linkstat == TI_EV_CODE_LINK_UP) {
3121		media = CSR_READ_4(sc, TI_GCR_LINK_STAT);
3122		if (sc->ti_copper) {
3123			if (media & TI_LNK_100MB)
3124				ifmr->ifm_active |= IFM_100_TX;
3125			if (media & TI_LNK_10MB)
3126				ifmr->ifm_active |= IFM_10_T;
3127		} else {
3128			if (media & TI_LNK_100MB)
3129				ifmr->ifm_active |= IFM_100_FX;
3130			if (media & TI_LNK_10MB)
3131				ifmr->ifm_active |= IFM_10_FL;
3132		}
3133		if (media & TI_LNK_FULL_DUPLEX)
3134			ifmr->ifm_active |= IFM_FDX;
3135		if (media & TI_LNK_HALF_DUPLEX)
3136			ifmr->ifm_active |= IFM_HDX;
3137	}
3138
3139	return;
3140}
3141
3142static int
3143ti_ioctl(ifp, command, data)
3144	struct ifnet		*ifp;
3145	u_long			command;
3146	caddr_t			data;
3147{
3148	struct ti_softc		*sc = ifp->if_softc;
3149	struct ifreq		*ifr = (struct ifreq *) data;
3150	int			mask, error = 0;
3151	struct ti_cmd_desc	cmd;
3152
3153	TI_LOCK(sc);
3154
3155	switch(command) {
3156	case SIOCSIFMTU:
3157		if (ifr->ifr_mtu > TI_JUMBO_MTU)
3158			error = EINVAL;
3159		else {
3160			ifp->if_mtu = ifr->ifr_mtu;
3161			ti_init(sc);
3162		}
3163		break;
3164	case SIOCSIFFLAGS:
3165		if (ifp->if_flags & IFF_UP) {
3166			/*
3167			 * If only the state of the PROMISC flag changed,
3168			 * then just use the 'set promisc mode' command
3169			 * instead of reinitializing the entire NIC. Doing
3170			 * a full re-init means reloading the firmware and
3171			 * waiting for it to start up, which may take a
3172			 * second or two.
3173			 */
3174			if (ifp->if_flags & IFF_RUNNING &&
3175			    ifp->if_flags & IFF_PROMISC &&
3176			    !(sc->ti_if_flags & IFF_PROMISC)) {
3177				TI_DO_CMD(TI_CMD_SET_PROMISC_MODE,
3178				    TI_CMD_CODE_PROMISC_ENB, 0);
3179			} else if (ifp->if_flags & IFF_RUNNING &&
3180			    !(ifp->if_flags & IFF_PROMISC) &&
3181			    sc->ti_if_flags & IFF_PROMISC) {
3182				TI_DO_CMD(TI_CMD_SET_PROMISC_MODE,
3183				    TI_CMD_CODE_PROMISC_DIS, 0);
3184			} else
3185				ti_init(sc);
3186		} else {
3187			if (ifp->if_flags & IFF_RUNNING) {
3188				ti_stop(sc);
3189			}
3190		}
3191		sc->ti_if_flags = ifp->if_flags;
3192		error = 0;
3193		break;
3194	case SIOCADDMULTI:
3195	case SIOCDELMULTI:
3196		if (ifp->if_flags & IFF_RUNNING) {
3197			ti_setmulti(sc);
3198			error = 0;
3199		}
3200		break;
3201	case SIOCSIFMEDIA:
3202	case SIOCGIFMEDIA:
3203		error = ifmedia_ioctl(ifp, ifr, &sc->ifmedia, command);
3204		break;
3205	case SIOCSIFCAP:
3206		mask = ifr->ifr_reqcap ^ ifp->if_capenable;
3207		if (mask & IFCAP_HWCSUM) {
3208			if (IFCAP_HWCSUM & ifp->if_capenable)
3209				ifp->if_capenable &= ~IFCAP_HWCSUM;
3210                        else
3211                                ifp->if_capenable |= IFCAP_HWCSUM;
3212			if (ifp->if_flags & IFF_RUNNING)
3213				ti_init(sc);
3214                }
3215		error = 0;
3216		break;
3217	default:
3218		error = ether_ioctl(ifp, command, data);
3219		break;
3220	}
3221
3222	TI_UNLOCK(sc);
3223
3224	return(error);
3225}
3226
3227static int
3228ti_open(dev_t dev, int flags, int fmt, struct thread *td)
3229{
3230	int unit;
3231	struct ti_softc *sc;
3232
3233	unit = minor(dev) & 0xff;
3234
3235	sc = ti_lookup_softc(unit);
3236
3237	if (sc == NULL)
3238		return(ENODEV);
3239
3240	TI_LOCK(sc);
3241	sc->ti_flags |= TI_FLAG_DEBUGING;
3242	TI_UNLOCK(sc);
3243
3244	return(0);
3245}
3246
3247static int
3248ti_close(dev_t dev, int flag, int fmt, struct thread *td)
3249{
3250	int unit;
3251	struct ti_softc *sc;
3252
3253	unit = minor(dev) & 0xff;
3254
3255	sc = ti_lookup_softc(unit);
3256
3257	if (sc == NULL)
3258		return(ENODEV);
3259
3260	TI_LOCK(sc);
3261	sc->ti_flags &= ~TI_FLAG_DEBUGING;
3262	TI_UNLOCK(sc);
3263
3264	return(0);
3265}
3266
3267/*
3268 * This ioctl routine goes along with the Tigon character device.
3269 */
3270static int
3271ti_ioctl2(dev_t dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
3272{
3273	int unit, error;
3274	struct ti_softc *sc;
3275
3276	unit = minor(dev) & 0xff;
3277
3278	sc = ti_lookup_softc(unit);
3279
3280	if (sc == NULL)
3281		return(ENODEV);
3282
3283	error = 0;
3284
3285	switch(cmd) {
3286	case TIIOCGETSTATS:
3287	{
3288		struct ti_stats *outstats;
3289
3290		outstats = (struct ti_stats *)addr;
3291
3292		bcopy(&sc->ti_rdata->ti_info.ti_stats, outstats,
3293		      sizeof(struct ti_stats));
3294		break;
3295	}
3296	case TIIOCGETPARAMS:
3297	{
3298		struct ti_params	*params;
3299
3300		params = (struct ti_params *)addr;
3301
3302		params->ti_stat_ticks = sc->ti_stat_ticks;
3303		params->ti_rx_coal_ticks = sc->ti_rx_coal_ticks;
3304		params->ti_tx_coal_ticks = sc->ti_tx_coal_ticks;
3305		params->ti_rx_max_coal_bds = sc->ti_rx_max_coal_bds;
3306		params->ti_tx_max_coal_bds = sc->ti_tx_max_coal_bds;
3307		params->ti_tx_buf_ratio = sc->ti_tx_buf_ratio;
3308		params->param_mask = TI_PARAM_ALL;
3309
3310		error = 0;
3311
3312		break;
3313	}
3314	case TIIOCSETPARAMS:
3315	{
3316		struct ti_params *params;
3317
3318		params = (struct ti_params *)addr;
3319
3320		if (params->param_mask & TI_PARAM_STAT_TICKS) {
3321			sc->ti_stat_ticks = params->ti_stat_ticks;
3322			CSR_WRITE_4(sc, TI_GCR_STAT_TICKS, sc->ti_stat_ticks);
3323		}
3324
3325		if (params->param_mask & TI_PARAM_RX_COAL_TICKS) {
3326			sc->ti_rx_coal_ticks = params->ti_rx_coal_ticks;
3327			CSR_WRITE_4(sc, TI_GCR_RX_COAL_TICKS,
3328				    sc->ti_rx_coal_ticks);
3329		}
3330
3331		if (params->param_mask & TI_PARAM_TX_COAL_TICKS) {
3332			sc->ti_tx_coal_ticks = params->ti_tx_coal_ticks;
3333			CSR_WRITE_4(sc, TI_GCR_TX_COAL_TICKS,
3334				    sc->ti_tx_coal_ticks);
3335		}
3336
3337		if (params->param_mask & TI_PARAM_RX_COAL_BDS) {
3338			sc->ti_rx_max_coal_bds = params->ti_rx_max_coal_bds;
3339			CSR_WRITE_4(sc, TI_GCR_RX_MAX_COAL_BD,
3340				    sc->ti_rx_max_coal_bds);
3341		}
3342
3343		if (params->param_mask & TI_PARAM_TX_COAL_BDS) {
3344			sc->ti_tx_max_coal_bds = params->ti_tx_max_coal_bds;
3345			CSR_WRITE_4(sc, TI_GCR_TX_MAX_COAL_BD,
3346				    sc->ti_tx_max_coal_bds);
3347		}
3348
3349		if (params->param_mask & TI_PARAM_TX_BUF_RATIO) {
3350			sc->ti_tx_buf_ratio = params->ti_tx_buf_ratio;
3351			CSR_WRITE_4(sc, TI_GCR_TX_BUFFER_RATIO,
3352				    sc->ti_tx_buf_ratio);
3353		}
3354
3355		error = 0;
3356
3357		break;
3358	}
3359	case TIIOCSETTRACE: {
3360		ti_trace_type	trace_type;
3361
3362		trace_type = *(ti_trace_type *)addr;
3363
3364		/*
3365		 * Set tracing to whatever the user asked for.  Setting
3366		 * this register to 0 should have the effect of disabling
3367		 * tracing.
3368		 */
3369		CSR_WRITE_4(sc, TI_GCR_NIC_TRACING, trace_type);
3370
3371		error = 0;
3372
3373		break;
3374	}
3375	case TIIOCGETTRACE: {
3376		struct ti_trace_buf	*trace_buf;
3377		u_int32_t		trace_start, cur_trace_ptr, trace_len;
3378
3379		trace_buf = (struct ti_trace_buf *)addr;
3380
3381		trace_start = CSR_READ_4(sc, TI_GCR_NICTRACE_START);
3382		cur_trace_ptr = CSR_READ_4(sc, TI_GCR_NICTRACE_PTR);
3383		trace_len = CSR_READ_4(sc, TI_GCR_NICTRACE_LEN);
3384
3385#if 0
3386		printf("ti%d: trace_start = %#x, cur_trace_ptr = %#x, "
3387		       "trace_len = %d\n", sc->ti_unit, trace_start,
3388		       cur_trace_ptr, trace_len);
3389		printf("ti%d: trace_buf->buf_len = %d\n", sc->ti_unit,
3390		       trace_buf->buf_len);
3391#endif
3392
3393		error = ti_copy_mem(sc, trace_start, min(trace_len,
3394				    trace_buf->buf_len),
3395				    (caddr_t)trace_buf->buf, 1, 1);
3396
3397		if (error == 0) {
3398			trace_buf->fill_len = min(trace_len,
3399						  trace_buf->buf_len);
3400			if (cur_trace_ptr < trace_start)
3401				trace_buf->cur_trace_ptr =
3402					trace_start - cur_trace_ptr;
3403			else
3404				trace_buf->cur_trace_ptr =
3405					cur_trace_ptr - trace_start;
3406		} else
3407			trace_buf->fill_len = 0;
3408
3409
3410		break;
3411	}
3412
3413	/*
3414	 * For debugging, five ioctls are needed:
3415	 * ALT_ATTACH
3416	 * ALT_READ_TG_REG
3417	 * ALT_WRITE_TG_REG
3418	 * ALT_READ_TG_MEM
3419	 * ALT_WRITE_TG_MEM
3420	 */
3421	case ALT_ATTACH:
3422		/*
3423		 * From what I can tell, Alteon's Solaris Tigon driver
3424		 * only has one character device, so you have to attach
3425		 * to the Tigon board you're interested in.  This seems
3426		 * like a not-so-good way to do things, since unless you
3427		 * subsequently specify the unit number of the device
3428		 * you're interested in in every ioctl, you'll only be
3429		 * able to debug one board at a time.
3430		 */
3431		error = 0;
3432		break;
3433	case ALT_READ_TG_MEM:
3434	case ALT_WRITE_TG_MEM:
3435	{
3436		struct tg_mem *mem_param;
3437		u_int32_t sram_end, scratch_end;
3438
3439		mem_param = (struct tg_mem *)addr;
3440
3441		if (sc->ti_hwrev == TI_HWREV_TIGON) {
3442			sram_end = TI_END_SRAM_I;
3443			scratch_end = TI_END_SCRATCH_I;
3444		} else {
3445			sram_end = TI_END_SRAM_II;
3446			scratch_end = TI_END_SCRATCH_II;
3447		}
3448
3449		/*
3450		 * For now, we'll only handle accessing regular SRAM,
3451		 * nothing else.
3452		 */
3453		if ((mem_param->tgAddr >= TI_BEG_SRAM)
3454		 && ((mem_param->tgAddr + mem_param->len) <= sram_end)) {
3455			/*
3456			 * In this instance, we always copy to/from user
3457			 * space, so the user space argument is set to 1.
3458			 */
3459			error = ti_copy_mem(sc, mem_param->tgAddr,
3460					    mem_param->len,
3461					    mem_param->userAddr, 1,
3462					    (cmd == ALT_READ_TG_MEM) ? 1 : 0);
3463		} else if ((mem_param->tgAddr >= TI_BEG_SCRATCH)
3464			&& (mem_param->tgAddr <= scratch_end)) {
3465			error = ti_copy_scratch(sc, mem_param->tgAddr,
3466						mem_param->len,
3467						mem_param->userAddr, 1,
3468						(cmd == ALT_READ_TG_MEM) ?
3469						1 : 0, TI_PROCESSOR_A);
3470		} else if ((mem_param->tgAddr >= TI_BEG_SCRATCH_B_DEBUG)
3471			&& (mem_param->tgAddr <= TI_BEG_SCRATCH_B_DEBUG)) {
3472			if (sc->ti_hwrev == TI_HWREV_TIGON) {
3473				printf("ti%d:  invalid memory range for "
3474				       "Tigon I\n", sc->ti_unit);
3475				error = EINVAL;
3476				break;
3477			}
3478			error = ti_copy_scratch(sc, mem_param->tgAddr -
3479						TI_SCRATCH_DEBUG_OFF,
3480						mem_param->len,
3481						mem_param->userAddr, 1,
3482						(cmd == ALT_READ_TG_MEM) ?
3483						1 : 0, TI_PROCESSOR_B);
3484		} else {
3485			printf("ti%d: memory address %#x len %d is out of "
3486			       "supported range\n", sc->ti_unit,
3487			        mem_param->tgAddr, mem_param->len);
3488			error = EINVAL;
3489		}
3490
3491		break;
3492	}
3493	case ALT_READ_TG_REG:
3494	case ALT_WRITE_TG_REG:
3495	{
3496		struct tg_reg	*regs;
3497		u_int32_t	tmpval;
3498
3499		regs = (struct tg_reg *)addr;
3500
3501		/*
3502		 * Make sure the address in question isn't out of range.
3503		 */
3504		if (regs->addr > TI_REG_MAX) {
3505			error = EINVAL;
3506			break;
3507		}
3508		if (cmd == ALT_READ_TG_REG) {
3509			bus_space_read_region_4(sc->ti_btag, sc->ti_bhandle,
3510						regs->addr, &tmpval, 1);
3511			regs->data = ntohl(tmpval);
3512#if 0
3513			if ((regs->addr == TI_CPU_STATE)
3514			 || (regs->addr == TI_CPU_CTL_B)) {
3515				printf("ti%d: register %#x = %#x\n",
3516				       sc->ti_unit, regs->addr, tmpval);
3517			}
3518#endif
3519		} else {
3520			tmpval = htonl(regs->data);
3521			bus_space_write_region_4(sc->ti_btag, sc->ti_bhandle,
3522						 regs->addr, &tmpval, 1);
3523		}
3524
3525		break;
3526	}
3527	default:
3528		error = ENOTTY;
3529		break;
3530	}
3531	return(error);
3532}
3533
3534static void
3535ti_watchdog(ifp)
3536	struct ifnet		*ifp;
3537{
3538	struct ti_softc		*sc;
3539
3540	sc = ifp->if_softc;
3541	TI_LOCK(sc);
3542
3543	/*
3544	 * When we're debugging, the chip is often stopped for long periods
3545	 * of time, and that would normally cause the watchdog timer to fire.
3546	 * Since that impedes debugging, we don't want to do that.
3547	 */
3548	if (sc->ti_flags & TI_FLAG_DEBUGING) {
3549		TI_UNLOCK(sc);
3550		return;
3551	}
3552
3553	printf("ti%d: watchdog timeout -- resetting\n", sc->ti_unit);
3554	ti_stop(sc);
3555	ti_init(sc);
3556
3557	ifp->if_oerrors++;
3558	TI_UNLOCK(sc);
3559
3560	return;
3561}
3562
3563/*
3564 * Stop the adapter and free any mbufs allocated to the
3565 * RX and TX lists.
3566 */
3567static void
3568ti_stop(sc)
3569	struct ti_softc		*sc;
3570{
3571	struct ifnet		*ifp;
3572	struct ti_cmd_desc	cmd;
3573
3574	TI_LOCK(sc);
3575
3576	ifp = &sc->arpcom.ac_if;
3577
3578	/* Disable host interrupts. */
3579	CSR_WRITE_4(sc, TI_MB_HOSTINTR, 1);
3580	/*
3581	 * Tell firmware we're shutting down.
3582	 */
3583	TI_DO_CMD(TI_CMD_HOST_STATE, TI_CMD_CODE_STACK_DOWN, 0);
3584
3585	/* Halt and reinitialize. */
3586	ti_chipinit(sc);
3587	ti_mem(sc, 0x2000, 0x100000 - 0x2000, NULL);
3588	ti_chipinit(sc);
3589
3590	/* Free the RX lists. */
3591	ti_free_rx_ring_std(sc);
3592
3593	/* Free jumbo RX list. */
3594	ti_free_rx_ring_jumbo(sc);
3595
3596	/* Free mini RX list. */
3597	ti_free_rx_ring_mini(sc);
3598
3599	/* Free TX buffers. */
3600	ti_free_tx_ring(sc);
3601
3602	sc->ti_ev_prodidx.ti_idx = 0;
3603	sc->ti_return_prodidx.ti_idx = 0;
3604	sc->ti_tx_considx.ti_idx = 0;
3605	sc->ti_tx_saved_considx = TI_TXCONS_UNSET;
3606
3607	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
3608	TI_UNLOCK(sc);
3609
3610	return;
3611}
3612
3613/*
3614 * Stop all chip I/O so that the kernel's probe routines don't
3615 * get confused by errant DMAs when rebooting.
3616 */
3617static void
3618ti_shutdown(dev)
3619	device_t		dev;
3620{
3621	struct ti_softc		*sc;
3622
3623	sc = device_get_softc(dev);
3624	TI_LOCK(sc);
3625	ti_chipinit(sc);
3626	TI_UNLOCK(sc);
3627
3628	return;
3629}
3630