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