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