xref: /freebsd/sys/dev/ti/if_ti.c (revision 2546665afcaf0d53dc2c7058fee96354b3680f5a)
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 
33 /*
34  * Alteon Networks Tigon PCI gigabit ethernet driver for FreeBSD.
35  * Manuals, sample driver and firmware source kits are available
36  * from http://www.alteon.com/support/openkits.
37  *
38  * Written by Bill Paul <wpaul@ctr.columbia.edu>
39  * Electrical Engineering Department
40  * Columbia University, New York City
41  */
42 
43 /*
44  * The Alteon Networks Tigon chip contains an embedded R4000 CPU,
45  * gigabit MAC, dual DMA channels and a PCI interface unit. NICs
46  * using the Tigon may have anywhere from 512K to 2MB of SRAM. The
47  * Tigon supports hardware IP, TCP and UCP checksumming, multicast
48  * filtering and jumbo (9014 byte) frames. The hardware is largely
49  * controlled by firmware, which must be loaded into the NIC during
50  * initialization.
51  *
52  * The Tigon 2 contains 2 R4000 CPUs and requires a newer firmware
53  * revision, which supports new features such as extended commands,
54  * extended jumbo receive ring desciptors and a mini receive ring.
55  *
56  * Alteon Networks is to be commended for releasing such a vast amount
57  * of development material for the Tigon NIC without requiring an NDA
58  * (although they really should have done it a long time ago). With
59  * any luck, the other vendors will finally wise up and follow Alteon's
60  * stellar example.
61  *
62  * The firmware for the Tigon 1 and 2 NICs is compiled directly into
63  * this driver by #including it as a C header file. This bloats the
64  * driver somewhat, but it's the easiest method considering that the
65  * driver code and firmware code need to be kept in sync. The source
66  * for the firmware is not provided with the FreeBSD distribution since
67  * compiling it requires a GNU toolchain targeted for mips-sgi-irix5.3.
68  *
69  * The following people deserve special thanks:
70  * - Terry Murphy of 3Com, for providing a 3c985 Tigon 1 board
71  *   for testing
72  * - Raymond Lee of Netgear, for providing a pair of Netgear
73  *   GA620 Tigon 2 boards for testing
74  * - Ulf Zimmermann, for bringing the GA260 to my attention and
75  *   convincing me to write this driver.
76  * - Andrew Gallatin for providing FreeBSD/Alpha support.
77  */
78 
79 #include <sys/cdefs.h>
80 __FBSDID("$FreeBSD$");
81 
82 #include "opt_ti.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/module.h>
91 #include <sys/socket.h>
92 #include <sys/queue.h>
93 #include <sys/conf.h>
94 
95 #include <net/if.h>
96 #include <net/if_arp.h>
97 #include <net/ethernet.h>
98 #include <net/if_dl.h>
99 #include <net/if_media.h>
100 #include <net/if_types.h>
101 #include <net/if_vlan_var.h>
102 
103 #include <net/bpf.h>
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/bus_memio.h>
112 #include <machine/bus.h>
113 #include <machine/resource.h>
114 #include <sys/bus.h>
115 #include <sys/rman.h>
116 
117 /* #define TI_PRIVATE_JUMBOS */
118 
119 #if !defined(TI_PRIVATE_JUMBOS)
120 #include <sys/sockio.h>
121 #include <sys/uio.h>
122 #include <sys/lock.h>
123 #include <vm/vm_extern.h>
124 #include <vm/pmap.h>
125 #include <vm/vm_map.h>
126 #include <vm/vm_map.h>
127 #include <vm/vm_param.h>
128 #include <vm/vm_pageout.h>
129 #include <sys/vmmeter.h>
130 #include <vm/vm_page.h>
131 #include <vm/vm_object.h>
132 #include <vm/vm_kern.h>
133 #include <sys/proc.h>
134 #include <sys/jumbo.h>
135 #endif /* !TI_PRIVATE_JUMBOS */
136 
137 #include <dev/pci/pcireg.h>
138 #include <dev/pci/pcivar.h>
139 
140 #include <sys/tiio.h>
141 #include <pci/if_tireg.h>
142 #include <pci/ti_fw.h>
143 #include <pci/ti_fw2.h>
144 
145 #define TI_CSUM_FEATURES	(CSUM_IP | CSUM_TCP | CSUM_UDP | CSUM_IP_FRAGS)
146 /*
147  * We can only turn on header splitting if we're using extended receive
148  * BDs.
149  */
150 #if defined(TI_JUMBO_HDRSPLIT) && defined(TI_PRIVATE_JUMBOS)
151 #error "options TI_JUMBO_HDRSPLIT and TI_PRIVATE_JUMBOS are mutually exclusive"
152 #endif /* TI_JUMBO_HDRSPLIT && TI_JUMBO_HDRSPLIT */
153 
154 struct ti_softc *tis[8];
155 
156 typedef enum {
157 	TI_SWAP_HTON,
158 	TI_SWAP_NTOH
159 } ti_swap_type;
160 
161 
162 /*
163  * Various supported device vendors/types and their names.
164  */
165 
166 static struct ti_type ti_devs[] = {
167 	{ ALT_VENDORID,	ALT_DEVICEID_ACENIC,
168 		"Alteon AceNIC 1000baseSX Gigabit Ethernet" },
169 	{ ALT_VENDORID,	ALT_DEVICEID_ACENIC_COPPER,
170 		"Alteon AceNIC 1000baseT Gigabit Ethernet" },
171 	{ TC_VENDORID,	TC_DEVICEID_3C985,
172 		"3Com 3c985-SX Gigabit Ethernet" },
173 	{ NG_VENDORID, NG_DEVICEID_GA620,
174 		"Netgear GA620 1000baseSX Gigabit Ethernet" },
175 	{ NG_VENDORID, NG_DEVICEID_GA620T,
176 		"Netgear GA620 1000baseT Gigabit Ethernet" },
177 	{ SGI_VENDORID, SGI_DEVICEID_TIGON,
178 		"Silicon Graphics Gigabit Ethernet" },
179 	{ DEC_VENDORID, DEC_DEVICEID_FARALLON_PN9000SX,
180 		"Farallon PN9000SX Gigabit Ethernet" },
181 	{ 0, 0, NULL }
182 };
183 
184 
185 static	d_open_t	ti_open;
186 static	d_close_t	ti_close;
187 static	d_ioctl_t	ti_ioctl2;
188 
189 static struct cdevsw ti_cdevsw = {
190 	.d_version =	D_VERSION,
191 	.d_flags =	D_NEEDGIANT,
192 	.d_open =	ti_open,
193 	.d_close =	ti_close,
194 	.d_ioctl =	ti_ioctl2,
195 	.d_name =	"ti",
196 };
197 
198 static int ti_probe		(device_t);
199 static int ti_attach		(device_t);
200 static int ti_detach		(device_t);
201 static void ti_txeof		(struct ti_softc *);
202 static void ti_rxeof		(struct ti_softc *);
203 
204 static void ti_stats_update	(struct ti_softc *);
205 static int ti_encap		(struct ti_softc *, struct mbuf *, u_int32_t *);
206 
207 static void ti_intr		(void *);
208 static void ti_start		(struct ifnet *);
209 static int ti_ioctl		(struct ifnet *, u_long, caddr_t);
210 static void ti_init		(void *);
211 static void ti_init2		(struct ti_softc *);
212 static void ti_stop		(struct ti_softc *);
213 static void ti_watchdog		(struct ifnet *);
214 static void ti_shutdown		(device_t);
215 static int ti_ifmedia_upd	(struct ifnet *);
216 static void ti_ifmedia_sts	(struct ifnet *, struct ifmediareq *);
217 
218 static u_int32_t ti_eeprom_putbyte	(struct ti_softc *, int);
219 static u_int8_t	ti_eeprom_getbyte	(struct ti_softc *, int, u_int8_t *);
220 static int ti_read_eeprom	(struct ti_softc *, caddr_t, int, int);
221 
222 static void ti_add_mcast	(struct ti_softc *, struct ether_addr *);
223 static void ti_del_mcast	(struct ti_softc *, struct ether_addr *);
224 static void ti_setmulti		(struct ti_softc *);
225 
226 static void ti_mem		(struct ti_softc *, u_int32_t,
227 					u_int32_t, caddr_t);
228 static int ti_copy_mem		(struct ti_softc *, u_int32_t,
229 					u_int32_t, caddr_t, int, int);
230 static int ti_copy_scratch	(struct ti_softc *, u_int32_t,
231 					u_int32_t, caddr_t, int, int, int);
232 static int ti_bcopy_swap	(const void *, void *, size_t,
233 					ti_swap_type);
234 static void ti_loadfw		(struct ti_softc *);
235 static void ti_cmd		(struct ti_softc *, struct ti_cmd_desc *);
236 static void ti_cmd_ext		(struct ti_softc *, struct ti_cmd_desc *,
237 					caddr_t, int);
238 static void ti_handle_events	(struct ti_softc *);
239 #ifdef TI_PRIVATE_JUMBOS
240 static int ti_alloc_jumbo_mem	(struct ti_softc *);
241 static void *ti_jalloc		(struct ti_softc *);
242 static void ti_jfree		(void *, void *);
243 #endif /* TI_PRIVATE_JUMBOS */
244 static int ti_newbuf_std	(struct ti_softc *, int, struct mbuf *);
245 static int ti_newbuf_mini	(struct ti_softc *, int, struct mbuf *);
246 static int ti_newbuf_jumbo	(struct ti_softc *, int, struct mbuf *);
247 static int ti_init_rx_ring_std	(struct ti_softc *);
248 static void ti_free_rx_ring_std	(struct ti_softc *);
249 static int ti_init_rx_ring_jumbo	(struct ti_softc *);
250 static void ti_free_rx_ring_jumbo	(struct ti_softc *);
251 static int ti_init_rx_ring_mini	(struct ti_softc *);
252 static void ti_free_rx_ring_mini	(struct ti_softc *);
253 static void ti_free_tx_ring	(struct ti_softc *);
254 static int ti_init_tx_ring	(struct ti_softc *);
255 
256 static int ti_64bitslot_war	(struct ti_softc *);
257 static int ti_chipinit		(struct ti_softc *);
258 static int ti_gibinit		(struct ti_softc *);
259 
260 #ifdef TI_JUMBO_HDRSPLIT
261 static __inline void ti_hdr_split	(struct mbuf *top, int hdr_len,
262 					     int pkt_len, int idx);
263 #endif /* TI_JUMBO_HDRSPLIT */
264 
265 static device_method_t ti_methods[] = {
266 	/* Device interface */
267 	DEVMETHOD(device_probe,		ti_probe),
268 	DEVMETHOD(device_attach,	ti_attach),
269 	DEVMETHOD(device_detach,	ti_detach),
270 	DEVMETHOD(device_shutdown,	ti_shutdown),
271 	{ 0, 0 }
272 };
273 
274 static driver_t ti_driver = {
275 	"ti",
276 	ti_methods,
277 	sizeof(struct ti_softc)
278 };
279 
280 static devclass_t ti_devclass;
281 
282 DRIVER_MODULE(ti, pci, ti_driver, ti_devclass, 0, 0);
283 MODULE_DEPEND(ti, pci, 1, 1, 1);
284 MODULE_DEPEND(ti, ether, 1, 1, 1);
285 
286 /*
287  * Send an instruction or address to the EEPROM, check for ACK.
288  */
289 static u_int32_t ti_eeprom_putbyte(sc, byte)
290 	struct ti_softc		*sc;
291 	int			byte;
292 {
293 	register int		i, ack = 0;
294 
295 	/*
296 	 * Make sure we're in TX mode.
297 	 */
298 	TI_SETBIT(sc, TI_MISC_LOCAL_CTL, TI_MLC_EE_TXEN);
299 
300 	/*
301 	 * Feed in each bit and stobe the clock.
302 	 */
303 	for (i = 0x80; i; i >>= 1) {
304 		if (byte & i) {
305 			TI_SETBIT(sc, TI_MISC_LOCAL_CTL, TI_MLC_EE_DOUT);
306 		} else {
307 			TI_CLRBIT(sc, TI_MISC_LOCAL_CTL, TI_MLC_EE_DOUT);
308 		}
309 		DELAY(1);
310 		TI_SETBIT(sc, TI_MISC_LOCAL_CTL, TI_MLC_EE_CLK);
311 		DELAY(1);
312 		TI_CLRBIT(sc, TI_MISC_LOCAL_CTL, TI_MLC_EE_CLK);
313 	}
314 
315 	/*
316 	 * Turn off TX mode.
317 	 */
318 	TI_CLRBIT(sc, TI_MISC_LOCAL_CTL, TI_MLC_EE_TXEN);
319 
320 	/*
321 	 * Check for ack.
322 	 */
323 	TI_SETBIT(sc, TI_MISC_LOCAL_CTL, TI_MLC_EE_CLK);
324 	ack = CSR_READ_4(sc, TI_MISC_LOCAL_CTL) & TI_MLC_EE_DIN;
325 	TI_CLRBIT(sc, TI_MISC_LOCAL_CTL, TI_MLC_EE_CLK);
326 
327 	return (ack);
328 }
329 
330 /*
331  * Read a byte of data stored in the EEPROM at address 'addr.'
332  * We have to send two address bytes since the EEPROM can hold
333  * more than 256 bytes of data.
334  */
335 static u_int8_t ti_eeprom_getbyte(sc, addr, dest)
336 	struct ti_softc		*sc;
337 	int			addr;
338 	u_int8_t		*dest;
339 {
340 	register int		i;
341 	u_int8_t		byte = 0;
342 
343 	EEPROM_START;
344 
345 	/*
346 	 * Send write control code to EEPROM.
347 	 */
348 	if (ti_eeprom_putbyte(sc, EEPROM_CTL_WRITE)) {
349 		printf("ti%d: failed to send write command, status: %x\n",
350 		    sc->ti_unit, CSR_READ_4(sc, TI_MISC_LOCAL_CTL));
351 		return (1);
352 	}
353 
354 	/*
355 	 * Send first byte of address of byte we want to read.
356 	 */
357 	if (ti_eeprom_putbyte(sc, (addr >> 8) & 0xFF)) {
358 		printf("ti%d: failed to send address, status: %x\n",
359 		    sc->ti_unit, CSR_READ_4(sc, TI_MISC_LOCAL_CTL));
360 		return (1);
361 	}
362 	/*
363 	 * Send second byte address of byte we want to read.
364 	 */
365 	if (ti_eeprom_putbyte(sc, addr & 0xFF)) {
366 		printf("ti%d: failed to send address, status: %x\n",
367 		    sc->ti_unit, CSR_READ_4(sc, TI_MISC_LOCAL_CTL));
368 		return (1);
369 	}
370 
371 	EEPROM_STOP;
372 	EEPROM_START;
373 	/*
374 	 * Send read control code to EEPROM.
375 	 */
376 	if (ti_eeprom_putbyte(sc, EEPROM_CTL_READ)) {
377 		printf("ti%d: failed to send read command, status: %x\n",
378 		    sc->ti_unit, CSR_READ_4(sc, TI_MISC_LOCAL_CTL));
379 		return (1);
380 	}
381 
382 	/*
383 	 * Start reading bits from EEPROM.
384 	 */
385 	TI_CLRBIT(sc, TI_MISC_LOCAL_CTL, TI_MLC_EE_TXEN);
386 	for (i = 0x80; i; i >>= 1) {
387 		TI_SETBIT(sc, TI_MISC_LOCAL_CTL, TI_MLC_EE_CLK);
388 		DELAY(1);
389 		if (CSR_READ_4(sc, TI_MISC_LOCAL_CTL) & TI_MLC_EE_DIN)
390 			byte |= i;
391 		TI_CLRBIT(sc, TI_MISC_LOCAL_CTL, TI_MLC_EE_CLK);
392 		DELAY(1);
393 	}
394 
395 	EEPROM_STOP;
396 
397 	/*
398 	 * No ACK generated for read, so just return byte.
399 	 */
400 
401 	*dest = byte;
402 
403 	return (0);
404 }
405 
406 /*
407  * Read a sequence of bytes from the EEPROM.
408  */
409 static int
410 ti_read_eeprom(sc, dest, off, cnt)
411 	struct ti_softc		*sc;
412 	caddr_t			dest;
413 	int			off;
414 	int			cnt;
415 {
416 	int			err = 0, i;
417 	u_int8_t		byte = 0;
418 
419 	for (i = 0; i < cnt; i++) {
420 		err = ti_eeprom_getbyte(sc, off + i, &byte);
421 		if (err)
422 			break;
423 		*(dest + i) = byte;
424 	}
425 
426 	return (err ? 1 : 0);
427 }
428 
429 /*
430  * NIC memory access function. Can be used to either clear a section
431  * of NIC local memory or (if buf is non-NULL) copy data into it.
432  */
433 static void
434 ti_mem(sc, addr, len, buf)
435 	struct ti_softc		*sc;
436 	u_int32_t		addr, len;
437 	caddr_t			buf;
438 {
439 	int			segptr, segsize, cnt;
440 	caddr_t			ti_winbase, ptr;
441 
442 	segptr = addr;
443 	cnt = len;
444 	ti_winbase = (caddr_t)(sc->ti_vhandle + TI_WINDOW);
445 	ptr = buf;
446 
447 	while (cnt) {
448 		if (cnt < TI_WINLEN)
449 			segsize = cnt;
450 		else
451 			segsize = TI_WINLEN - (segptr % TI_WINLEN);
452 		CSR_WRITE_4(sc, TI_WINBASE, (segptr & ~(TI_WINLEN - 1)));
453 		if (buf == NULL)
454 			bzero((char *)ti_winbase + (segptr &
455 			    (TI_WINLEN - 1)), segsize);
456 		else {
457 			bcopy((char *)ptr, (char *)ti_winbase +
458 			    (segptr & (TI_WINLEN - 1)), segsize);
459 			ptr += segsize;
460 		}
461 		segptr += segsize;
462 		cnt -= segsize;
463 	}
464 }
465 
466 static int
467 ti_copy_mem(sc, tigon_addr, len, buf, useraddr, readdata)
468 	struct ti_softc		*sc;
469 	u_int32_t		tigon_addr, len;
470 	caddr_t			buf;
471 	int			useraddr, readdata;
472 {
473 	int		segptr, segsize, cnt;
474 	caddr_t		ptr;
475 	u_int32_t	origwin;
476 	u_int8_t	tmparray[TI_WINLEN], tmparray2[TI_WINLEN];
477 	int		resid, segresid;
478 	int		first_pass;
479 
480 	/*
481 	 * At the moment, we don't handle non-aligned cases, we just bail.
482 	 * If this proves to be a problem, it will be fixed.
483 	 */
484 	if ((readdata == 0)
485 	 && (tigon_addr & 0x3)) {
486 		printf("ti%d: ti_copy_mem: tigon address %#x isn't "
487 		       "word-aligned\n", sc->ti_unit, tigon_addr);
488 		printf("ti%d: ti_copy_mem: unaligned writes aren't yet "
489 		       "supported\n", sc->ti_unit);
490 		return (EINVAL);
491 	}
492 
493 	segptr = tigon_addr & ~0x3;
494 	segresid = tigon_addr - segptr;
495 
496 	/*
497 	 * This is the non-aligned amount left over that we'll need to
498 	 * copy.
499 	 */
500 	resid = len & 0x3;
501 
502 	/* Add in the left over amount at the front of the buffer */
503 	resid += segresid;
504 
505 	cnt = len & ~0x3;
506 	/*
507 	 * If resid + segresid is >= 4, add multiples of 4 to the count and
508 	 * decrease the residual by that much.
509 	 */
510 	cnt += resid & ~0x3;
511 	resid -= resid & ~0x3;
512 
513 	ptr = buf;
514 
515 	first_pass = 1;
516 
517 	/*
518 	 * Make sure we aren't interrupted while we're changing the window
519 	 * pointer.
520 	 */
521 	TI_LOCK(sc);
522 
523 	/*
524 	 * Save the old window base value.
525 	 */
526 	origwin = CSR_READ_4(sc, TI_WINBASE);
527 
528 	while (cnt) {
529 		bus_size_t ti_offset;
530 
531 		if (cnt < TI_WINLEN)
532 			segsize = cnt;
533 		else
534 			segsize = TI_WINLEN - (segptr % TI_WINLEN);
535 		CSR_WRITE_4(sc, TI_WINBASE, (segptr & ~(TI_WINLEN - 1)));
536 
537 		ti_offset = TI_WINDOW + (segptr & (TI_WINLEN -1));
538 
539 		if (readdata) {
540 
541 			bus_space_read_region_4(sc->ti_btag,
542 						sc->ti_bhandle, ti_offset,
543 						(u_int32_t *)tmparray,
544 						segsize >> 2);
545 			if (useraddr) {
546 				/*
547 				 * Yeah, this is a little on the kludgy
548 				 * side, but at least this code is only
549 				 * used for debugging.
550 				 */
551 				ti_bcopy_swap(tmparray, tmparray2, segsize,
552 					      TI_SWAP_NTOH);
553 
554 				if (first_pass) {
555 					copyout(&tmparray2[segresid], ptr,
556 						segsize - segresid);
557 					first_pass = 0;
558 				} else
559 					copyout(tmparray2, ptr, segsize);
560 			} else {
561 				if (first_pass) {
562 
563 					ti_bcopy_swap(tmparray, tmparray2,
564 						      segsize, TI_SWAP_NTOH);
565 					bcopy(&tmparray2[segresid], ptr,
566 					      segsize - segresid);
567 					first_pass = 0;
568 				} else
569 					ti_bcopy_swap(tmparray, ptr, segsize,
570 						      TI_SWAP_NTOH);
571 			}
572 
573 		} else {
574 			if (useraddr) {
575 				copyin(ptr, tmparray2, segsize);
576 				ti_bcopy_swap(tmparray2, tmparray, segsize,
577 					      TI_SWAP_HTON);
578 			} else
579 				ti_bcopy_swap(ptr, tmparray, segsize,
580 					      TI_SWAP_HTON);
581 
582 			bus_space_write_region_4(sc->ti_btag,
583 						 sc->ti_bhandle, ti_offset,
584 						 (u_int32_t *)tmparray,
585 						 segsize >> 2);
586 		}
587 		segptr += segsize;
588 		ptr += segsize;
589 		cnt -= segsize;
590 	}
591 
592 	/*
593 	 * Handle leftover, non-word-aligned bytes.
594 	 */
595 	if (resid != 0) {
596 		u_int32_t	tmpval, tmpval2;
597 		bus_size_t	ti_offset;
598 
599 		/*
600 		 * Set the segment pointer.
601 		 */
602 		CSR_WRITE_4(sc, TI_WINBASE, (segptr & ~(TI_WINLEN - 1)));
603 
604 		ti_offset = TI_WINDOW + (segptr & (TI_WINLEN - 1));
605 
606 		/*
607 		 * First, grab whatever is in our source/destination.
608 		 * We'll obviously need this for reads, but also for
609 		 * writes, since we'll be doing read/modify/write.
610 		 */
611 		bus_space_read_region_4(sc->ti_btag, sc->ti_bhandle,
612 					ti_offset, &tmpval, 1);
613 
614 		/*
615 		 * Next, translate this from little-endian to big-endian
616 		 * (at least on i386 boxes).
617 		 */
618 		tmpval2 = ntohl(tmpval);
619 
620 		if (readdata) {
621 			/*
622 			 * If we're reading, just copy the leftover number
623 			 * of bytes from the host byte order buffer to
624 			 * the user's buffer.
625 			 */
626 			if (useraddr)
627 				copyout(&tmpval2, ptr, resid);
628 			else
629 				bcopy(&tmpval2, ptr, resid);
630 		} else {
631 			/*
632 			 * If we're writing, first copy the bytes to be
633 			 * written into the network byte order buffer,
634 			 * leaving the rest of the buffer with whatever was
635 			 * originally in there.  Then, swap the bytes
636 			 * around into host order and write them out.
637 			 *
638 			 * XXX KDM the read side of this has been verified
639 			 * to work, but the write side of it has not been
640 			 * verified.  So user beware.
641 			 */
642 			if (useraddr)
643 				copyin(ptr, &tmpval2, resid);
644 			else
645 				bcopy(ptr, &tmpval2, resid);
646 
647 			tmpval = htonl(tmpval2);
648 
649 			bus_space_write_region_4(sc->ti_btag, sc->ti_bhandle,
650 						 ti_offset, &tmpval, 1);
651 		}
652 	}
653 
654 	CSR_WRITE_4(sc, TI_WINBASE, origwin);
655 
656 	TI_UNLOCK(sc);
657 
658 	return (0);
659 }
660 
661 static int
662 ti_copy_scratch(sc, tigon_addr, len, buf, useraddr, readdata, cpu)
663 	struct ti_softc		*sc;
664 	u_int32_t		tigon_addr, len;
665 	caddr_t			buf;
666 	int			useraddr, readdata;
667 	int			cpu;
668 {
669 	u_int32_t	segptr;
670 	int		cnt;
671 	u_int32_t	tmpval, tmpval2;
672 	caddr_t		ptr;
673 
674 	/*
675 	 * At the moment, we don't handle non-aligned cases, we just bail.
676 	 * If this proves to be a problem, it will be fixed.
677 	 */
678 	if (tigon_addr & 0x3) {
679 		printf("ti%d: ti_copy_scratch: tigon address %#x isn't "
680 		       "word-aligned\n", sc->ti_unit, tigon_addr);
681 		return (EINVAL);
682 	}
683 
684 	if (len & 0x3) {
685 		printf("ti%d: ti_copy_scratch: transfer length %d isn't "
686 		       "word-aligned\n", sc->ti_unit, len);
687 		return (EINVAL);
688 	}
689 
690 	segptr = tigon_addr;
691 	cnt = len;
692 	ptr = buf;
693 
694 	TI_LOCK(sc);
695 
696 	while (cnt) {
697 		CSR_WRITE_4(sc, CPU_REG(TI_SRAM_ADDR, cpu), segptr);
698 
699 		if (readdata) {
700 			tmpval2 = CSR_READ_4(sc, CPU_REG(TI_SRAM_DATA, cpu));
701 
702 			tmpval = ntohl(tmpval2);
703 
704 			/*
705 			 * Note:  I've used this debugging interface
706 			 * extensively with Alteon's 12.3.15 firmware,
707 			 * compiled with GCC 2.7.2.1 and binutils 2.9.1.
708 			 *
709 			 * When you compile the firmware without
710 			 * optimization, which is necessary sometimes in
711 			 * order to properly step through it, you sometimes
712 			 * read out a bogus value of 0xc0017c instead of
713 			 * whatever was supposed to be in that scratchpad
714 			 * location.  That value is on the stack somewhere,
715 			 * but I've never been able to figure out what was
716 			 * causing the problem.
717 			 *
718 			 * The address seems to pop up in random places,
719 			 * often not in the same place on two subsequent
720 			 * reads.
721 			 *
722 			 * In any case, the underlying data doesn't seem
723 			 * to be affected, just the value read out.
724 			 *
725 			 * KDM, 3/7/2000
726 			 */
727 
728 			if (tmpval2 == 0xc0017c)
729 				printf("ti%d: found 0xc0017c at %#x "
730 				       "(tmpval2)\n", sc->ti_unit, segptr);
731 
732 			if (tmpval == 0xc0017c)
733 				printf("ti%d: found 0xc0017c at %#x "
734 				       "(tmpval)\n", sc->ti_unit, segptr);
735 
736 			if (useraddr)
737 				copyout(&tmpval, ptr, 4);
738 			else
739 				bcopy(&tmpval, ptr, 4);
740 		} else {
741 			if (useraddr)
742 				copyin(ptr, &tmpval2, 4);
743 			else
744 				bcopy(ptr, &tmpval2, 4);
745 
746 			tmpval = htonl(tmpval2);
747 
748 			CSR_WRITE_4(sc, CPU_REG(TI_SRAM_DATA, cpu), tmpval);
749 		}
750 
751 		cnt -= 4;
752 		segptr += 4;
753 		ptr += 4;
754 	}
755 
756 	TI_UNLOCK(sc);
757 
758 	return (0);
759 }
760 
761 static int
762 ti_bcopy_swap(src, dst, len, swap_type)
763 	const void	*src;
764 	void		*dst;
765 	size_t		len;
766 	ti_swap_type	swap_type;
767 {
768 	const u_int8_t *tmpsrc;
769 	u_int8_t *tmpdst;
770 	size_t tmplen;
771 
772 	if (len & 0x3) {
773 		printf("ti_bcopy_swap: length %zd isn't 32-bit aligned\n",
774 		       len);
775 		return (-1);
776 	}
777 
778 	tmpsrc = src;
779 	tmpdst = dst;
780 	tmplen = len;
781 
782 	while (tmplen) {
783 		if (swap_type == TI_SWAP_NTOH)
784 			*(u_int32_t *)tmpdst =
785 				ntohl(*(const u_int32_t *)tmpsrc);
786 		else
787 			*(u_int32_t *)tmpdst =
788 				htonl(*(const u_int32_t *)tmpsrc);
789 
790 		tmpsrc += 4;
791 		tmpdst += 4;
792 		tmplen -= 4;
793 	}
794 
795 	return (0);
796 }
797 
798 /*
799  * Load firmware image into the NIC. Check that the firmware revision
800  * is acceptable and see if we want the firmware for the Tigon 1 or
801  * Tigon 2.
802  */
803 static void
804 ti_loadfw(sc)
805 	struct ti_softc		*sc;
806 {
807 	switch (sc->ti_hwrev) {
808 	case TI_HWREV_TIGON:
809 		if (tigonFwReleaseMajor != TI_FIRMWARE_MAJOR ||
810 		    tigonFwReleaseMinor != TI_FIRMWARE_MINOR ||
811 		    tigonFwReleaseFix != TI_FIRMWARE_FIX) {
812 			printf("ti%d: firmware revision mismatch; want "
813 			    "%d.%d.%d, got %d.%d.%d\n", sc->ti_unit,
814 			    TI_FIRMWARE_MAJOR, TI_FIRMWARE_MINOR,
815 			    TI_FIRMWARE_FIX, tigonFwReleaseMajor,
816 			    tigonFwReleaseMinor, tigonFwReleaseFix);
817 			return;
818 		}
819 		ti_mem(sc, tigonFwTextAddr, tigonFwTextLen,
820 		    (caddr_t)tigonFwText);
821 		ti_mem(sc, tigonFwDataAddr, tigonFwDataLen,
822 		    (caddr_t)tigonFwData);
823 		ti_mem(sc, tigonFwRodataAddr, tigonFwRodataLen,
824 		    (caddr_t)tigonFwRodata);
825 		ti_mem(sc, tigonFwBssAddr, tigonFwBssLen, NULL);
826 		ti_mem(sc, tigonFwSbssAddr, tigonFwSbssLen, NULL);
827 		CSR_WRITE_4(sc, TI_CPU_PROGRAM_COUNTER, tigonFwStartAddr);
828 		break;
829 	case TI_HWREV_TIGON_II:
830 		if (tigon2FwReleaseMajor != TI_FIRMWARE_MAJOR ||
831 		    tigon2FwReleaseMinor != TI_FIRMWARE_MINOR ||
832 		    tigon2FwReleaseFix != TI_FIRMWARE_FIX) {
833 			printf("ti%d: firmware revision mismatch; want "
834 			    "%d.%d.%d, got %d.%d.%d\n", sc->ti_unit,
835 			    TI_FIRMWARE_MAJOR, TI_FIRMWARE_MINOR,
836 			    TI_FIRMWARE_FIX, tigon2FwReleaseMajor,
837 			    tigon2FwReleaseMinor, tigon2FwReleaseFix);
838 			return;
839 		}
840 		ti_mem(sc, tigon2FwTextAddr, tigon2FwTextLen,
841 		    (caddr_t)tigon2FwText);
842 		ti_mem(sc, tigon2FwDataAddr, tigon2FwDataLen,
843 		    (caddr_t)tigon2FwData);
844 		ti_mem(sc, tigon2FwRodataAddr, tigon2FwRodataLen,
845 		    (caddr_t)tigon2FwRodata);
846 		ti_mem(sc, tigon2FwBssAddr, tigon2FwBssLen, NULL);
847 		ti_mem(sc, tigon2FwSbssAddr, tigon2FwSbssLen, NULL);
848 		CSR_WRITE_4(sc, TI_CPU_PROGRAM_COUNTER, tigon2FwStartAddr);
849 		break;
850 	default:
851 		printf("ti%d: can't load firmware: unknown hardware rev\n",
852 		    sc->ti_unit);
853 		break;
854 	}
855 }
856 
857 /*
858  * Send the NIC a command via the command ring.
859  */
860 static void
861 ti_cmd(sc, cmd)
862 	struct ti_softc		*sc;
863 	struct ti_cmd_desc	*cmd;
864 {
865 	u_int32_t		index;
866 
867 	if (sc->ti_rdata->ti_cmd_ring == NULL)
868 		return;
869 
870 	index = sc->ti_cmd_saved_prodidx;
871 	CSR_WRITE_4(sc, TI_GCR_CMDRING + (index * 4), *(u_int32_t *)(cmd));
872 	TI_INC(index, TI_CMD_RING_CNT);
873 	CSR_WRITE_4(sc, TI_MB_CMDPROD_IDX, index);
874 	sc->ti_cmd_saved_prodidx = index;
875 }
876 
877 /*
878  * Send the NIC an extended command. The 'len' parameter specifies the
879  * number of command slots to include after the initial command.
880  */
881 static void
882 ti_cmd_ext(sc, cmd, arg, len)
883 	struct ti_softc		*sc;
884 	struct ti_cmd_desc	*cmd;
885 	caddr_t			arg;
886 	int			len;
887 {
888 	u_int32_t		index;
889 	register int		i;
890 
891 	if (sc->ti_rdata->ti_cmd_ring == NULL)
892 		return;
893 
894 	index = sc->ti_cmd_saved_prodidx;
895 	CSR_WRITE_4(sc, TI_GCR_CMDRING + (index * 4), *(u_int32_t *)(cmd));
896 	TI_INC(index, TI_CMD_RING_CNT);
897 	for (i = 0; i < len; i++) {
898 		CSR_WRITE_4(sc, TI_GCR_CMDRING + (index * 4),
899 		    *(u_int32_t *)(&arg[i * 4]));
900 		TI_INC(index, TI_CMD_RING_CNT);
901 	}
902 	CSR_WRITE_4(sc, TI_MB_CMDPROD_IDX, index);
903 	sc->ti_cmd_saved_prodidx = index;
904 }
905 
906 /*
907  * Handle events that have triggered interrupts.
908  */
909 static void
910 ti_handle_events(sc)
911 	struct ti_softc		*sc;
912 {
913 	struct ti_event_desc	*e;
914 
915 	if (sc->ti_rdata->ti_event_ring == NULL)
916 		return;
917 
918 	while (sc->ti_ev_saved_considx != sc->ti_ev_prodidx.ti_idx) {
919 		e = &sc->ti_rdata->ti_event_ring[sc->ti_ev_saved_considx];
920 		switch (e->ti_event) {
921 		case TI_EV_LINKSTAT_CHANGED:
922 			sc->ti_linkstat = e->ti_code;
923 			if (e->ti_code == TI_EV_CODE_LINK_UP)
924 				printf("ti%d: 10/100 link up\n", sc->ti_unit);
925 			else if (e->ti_code == TI_EV_CODE_GIG_LINK_UP)
926 				printf("ti%d: gigabit link up\n", sc->ti_unit);
927 			else if (e->ti_code == TI_EV_CODE_LINK_DOWN)
928 				printf("ti%d: link down\n", sc->ti_unit);
929 			break;
930 		case TI_EV_ERROR:
931 			if (e->ti_code == TI_EV_CODE_ERR_INVAL_CMD)
932 				printf("ti%d: invalid command\n", sc->ti_unit);
933 			else if (e->ti_code == TI_EV_CODE_ERR_UNIMP_CMD)
934 				printf("ti%d: unknown command\n", sc->ti_unit);
935 			else if (e->ti_code == TI_EV_CODE_ERR_BADCFG)
936 				printf("ti%d: bad config data\n", sc->ti_unit);
937 			break;
938 		case TI_EV_FIRMWARE_UP:
939 			ti_init2(sc);
940 			break;
941 		case TI_EV_STATS_UPDATED:
942 			ti_stats_update(sc);
943 			break;
944 		case TI_EV_RESET_JUMBO_RING:
945 		case TI_EV_MCAST_UPDATED:
946 			/* Who cares. */
947 			break;
948 		default:
949 			printf("ti%d: unknown event: %d\n",
950 			    sc->ti_unit, e->ti_event);
951 			break;
952 		}
953 		/* Advance the consumer index. */
954 		TI_INC(sc->ti_ev_saved_considx, TI_EVENT_RING_CNT);
955 		CSR_WRITE_4(sc, TI_GCR_EVENTCONS_IDX, sc->ti_ev_saved_considx);
956 	}
957 }
958 
959 #ifdef TI_PRIVATE_JUMBOS
960 
961 /*
962  * Memory management for the jumbo receive ring is a pain in the
963  * butt. We need to allocate at least 9018 bytes of space per frame,
964  * _and_ it has to be contiguous (unless you use the extended
965  * jumbo descriptor format). Using malloc() all the time won't
966  * work: malloc() allocates memory in powers of two, which means we
967  * would end up wasting a considerable amount of space by allocating
968  * 9K chunks. We don't have a jumbo mbuf cluster pool. Thus, we have
969  * to do our own memory management.
970  *
971  * The driver needs to allocate a contiguous chunk of memory at boot
972  * time. We then chop this up ourselves into 9K pieces and use them
973  * as external mbuf storage.
974  *
975  * One issue here is how much memory to allocate. The jumbo ring has
976  * 256 slots in it, but at 9K per slot than can consume over 2MB of
977  * RAM. This is a bit much, especially considering we also need
978  * RAM for the standard ring and mini ring (on the Tigon 2). To
979  * save space, we only actually allocate enough memory for 64 slots
980  * by default, which works out to between 500 and 600K. This can
981  * be tuned by changing a #define in if_tireg.h.
982  */
983 
984 static int
985 ti_alloc_jumbo_mem(sc)
986 	struct ti_softc		*sc;
987 {
988 	caddr_t			ptr;
989 	register int		i;
990 	struct ti_jpool_entry   *entry;
991 
992 	/* Grab a big chunk o' storage. */
993 	sc->ti_cdata.ti_jumbo_buf = contigmalloc(TI_JMEM, M_DEVBUF,
994 		M_NOWAIT, 0, 0xffffffff, PAGE_SIZE, 0);
995 
996 	if (sc->ti_cdata.ti_jumbo_buf == NULL) {
997 		printf("ti%d: no memory for jumbo buffers!\n", sc->ti_unit);
998 		return (ENOBUFS);
999 	}
1000 
1001 	SLIST_INIT(&sc->ti_jfree_listhead);
1002 	SLIST_INIT(&sc->ti_jinuse_listhead);
1003 
1004 	/*
1005 	 * Now divide it up into 9K pieces and save the addresses
1006 	 * in an array.
1007 	 */
1008 	ptr = sc->ti_cdata.ti_jumbo_buf;
1009 	for (i = 0; i < TI_JSLOTS; i++) {
1010 		sc->ti_cdata.ti_jslots[i] = ptr;
1011 		ptr += TI_JLEN;
1012 		entry = malloc(sizeof(struct ti_jpool_entry),
1013 			       M_DEVBUF, M_NOWAIT);
1014 		if (entry == NULL) {
1015 			contigfree(sc->ti_cdata.ti_jumbo_buf, TI_JMEM,
1016 			           M_DEVBUF);
1017 			sc->ti_cdata.ti_jumbo_buf = NULL;
1018 			printf("ti%d: no memory for jumbo "
1019 			    "buffer queue!\n", sc->ti_unit);
1020 			return (ENOBUFS);
1021 		}
1022 		entry->slot = i;
1023 		SLIST_INSERT_HEAD(&sc->ti_jfree_listhead, entry, jpool_entries);
1024 	}
1025 
1026 	return (0);
1027 }
1028 
1029 /*
1030  * Allocate a jumbo buffer.
1031  */
1032 static void *ti_jalloc(sc)
1033 	struct ti_softc		*sc;
1034 {
1035 	struct ti_jpool_entry	*entry;
1036 
1037 	entry = SLIST_FIRST(&sc->ti_jfree_listhead);
1038 
1039 	if (entry == NULL) {
1040 		printf("ti%d: no free jumbo buffers\n", sc->ti_unit);
1041 		return (NULL);
1042 	}
1043 
1044 	SLIST_REMOVE_HEAD(&sc->ti_jfree_listhead, jpool_entries);
1045 	SLIST_INSERT_HEAD(&sc->ti_jinuse_listhead, entry, jpool_entries);
1046 	return (sc->ti_cdata.ti_jslots[entry->slot]);
1047 }
1048 
1049 /*
1050  * Release a jumbo buffer.
1051  */
1052 static void
1053 ti_jfree(buf, args)
1054 	void			*buf;
1055 	void			*args;
1056 {
1057 	struct ti_softc		*sc;
1058 	int			i;
1059 	struct ti_jpool_entry	*entry;
1060 
1061 	/* Extract the softc struct pointer. */
1062 	sc = (struct ti_softc *)args;
1063 
1064 	if (sc == NULL)
1065 		panic("ti_jfree: didn't get softc pointer!");
1066 
1067 	/* calculate the slot this buffer belongs to */
1068 	i = ((vm_offset_t)buf
1069 	     - (vm_offset_t)sc->ti_cdata.ti_jumbo_buf) / TI_JLEN;
1070 
1071 	if ((i < 0) || (i >= TI_JSLOTS))
1072 		panic("ti_jfree: asked to free buffer that we don't manage!");
1073 
1074 	entry = SLIST_FIRST(&sc->ti_jinuse_listhead);
1075 	if (entry == NULL)
1076 		panic("ti_jfree: buffer not in use!");
1077 	entry->slot = i;
1078 	SLIST_REMOVE_HEAD(&sc->ti_jinuse_listhead, jpool_entries);
1079 	SLIST_INSERT_HEAD(&sc->ti_jfree_listhead, entry, jpool_entries);
1080 }
1081 
1082 #endif /* TI_PRIVATE_JUMBOS */
1083 
1084 /*
1085  * Intialize a standard receive ring descriptor.
1086  */
1087 static int
1088 ti_newbuf_std(sc, i, m)
1089 	struct ti_softc		*sc;
1090 	int			i;
1091 	struct mbuf		*m;
1092 {
1093 	struct mbuf		*m_new = NULL;
1094 	struct ti_rx_desc	*r;
1095 
1096 	if (m == NULL) {
1097 		MGETHDR(m_new, M_DONTWAIT, MT_DATA);
1098 		if (m_new == NULL)
1099 			return (ENOBUFS);
1100 
1101 		MCLGET(m_new, M_DONTWAIT);
1102 		if (!(m_new->m_flags & M_EXT)) {
1103 			m_freem(m_new);
1104 			return (ENOBUFS);
1105 		}
1106 		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
1107 	} else {
1108 		m_new = m;
1109 		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
1110 		m_new->m_data = m_new->m_ext.ext_buf;
1111 	}
1112 
1113 	m_adj(m_new, ETHER_ALIGN);
1114 	sc->ti_cdata.ti_rx_std_chain[i] = m_new;
1115 	r = &sc->ti_rdata->ti_rx_std_ring[i];
1116 	TI_HOSTADDR(r->ti_addr) = vtophys(mtod(m_new, caddr_t));
1117 	r->ti_type = TI_BDTYPE_RECV_BD;
1118 	r->ti_flags = 0;
1119 	if (sc->arpcom.ac_if.if_hwassist)
1120 		r->ti_flags |= TI_BDFLAG_TCP_UDP_CKSUM | TI_BDFLAG_IP_CKSUM;
1121 	r->ti_len = m_new->m_len;
1122 	r->ti_idx = i;
1123 
1124 	return (0);
1125 }
1126 
1127 /*
1128  * Intialize a mini receive ring descriptor. This only applies to
1129  * the Tigon 2.
1130  */
1131 static int
1132 ti_newbuf_mini(sc, i, m)
1133 	struct ti_softc		*sc;
1134 	int			i;
1135 	struct mbuf		*m;
1136 {
1137 	struct mbuf		*m_new = NULL;
1138 	struct ti_rx_desc	*r;
1139 
1140 	if (m == NULL) {
1141 		MGETHDR(m_new, M_DONTWAIT, MT_DATA);
1142 		if (m_new == NULL) {
1143 			return (ENOBUFS);
1144 		}
1145 		m_new->m_len = m_new->m_pkthdr.len = MHLEN;
1146 	} else {
1147 		m_new = m;
1148 		m_new->m_data = m_new->m_pktdat;
1149 		m_new->m_len = m_new->m_pkthdr.len = MHLEN;
1150 	}
1151 
1152 	m_adj(m_new, ETHER_ALIGN);
1153 	r = &sc->ti_rdata->ti_rx_mini_ring[i];
1154 	sc->ti_cdata.ti_rx_mini_chain[i] = m_new;
1155 	TI_HOSTADDR(r->ti_addr) = vtophys(mtod(m_new, caddr_t));
1156 	r->ti_type = TI_BDTYPE_RECV_BD;
1157 	r->ti_flags = TI_BDFLAG_MINI_RING;
1158 	if (sc->arpcom.ac_if.if_hwassist)
1159 		r->ti_flags |= TI_BDFLAG_TCP_UDP_CKSUM | TI_BDFLAG_IP_CKSUM;
1160 	r->ti_len = m_new->m_len;
1161 	r->ti_idx = i;
1162 
1163 	return (0);
1164 }
1165 
1166 #ifdef TI_PRIVATE_JUMBOS
1167 
1168 /*
1169  * Initialize a jumbo receive ring descriptor. This allocates
1170  * a jumbo buffer from the pool managed internally by the driver.
1171  */
1172 static int
1173 ti_newbuf_jumbo(sc, i, m)
1174 	struct ti_softc		*sc;
1175 	int			i;
1176 	struct mbuf		*m;
1177 {
1178 	struct mbuf		*m_new = NULL;
1179 	struct ti_rx_desc	*r;
1180 
1181 	if (m == NULL) {
1182 		caddr_t			*buf = NULL;
1183 
1184 		/* Allocate the mbuf. */
1185 		MGETHDR(m_new, M_DONTWAIT, MT_DATA);
1186 		if (m_new == NULL) {
1187 			return (ENOBUFS);
1188 		}
1189 
1190 		/* Allocate the jumbo buffer */
1191 		buf = ti_jalloc(sc);
1192 		if (buf == NULL) {
1193 			m_freem(m_new);
1194 			printf("ti%d: jumbo allocation failed "
1195 			    "-- packet dropped!\n", sc->ti_unit);
1196 			return (ENOBUFS);
1197 		}
1198 
1199 		/* Attach the buffer to the mbuf. */
1200 		m_new->m_data = (void *) buf;
1201 		m_new->m_len = m_new->m_pkthdr.len = TI_JUMBO_FRAMELEN;
1202 		MEXTADD(m_new, buf, TI_JUMBO_FRAMELEN, ti_jfree,
1203 		    (struct ti_softc *)sc, 0, EXT_NET_DRV);
1204 	} else {
1205 		m_new = m;
1206 		m_new->m_data = m_new->m_ext.ext_buf;
1207 		m_new->m_ext.ext_size = TI_JUMBO_FRAMELEN;
1208 	}
1209 
1210 	m_adj(m_new, ETHER_ALIGN);
1211 	/* Set up the descriptor. */
1212 	r = &sc->ti_rdata->ti_rx_jumbo_ring[i];
1213 	sc->ti_cdata.ti_rx_jumbo_chain[i] = m_new;
1214 	TI_HOSTADDR(r->ti_addr) = vtophys(mtod(m_new, caddr_t));
1215 	r->ti_type = TI_BDTYPE_RECV_JUMBO_BD;
1216 	r->ti_flags = TI_BDFLAG_JUMBO_RING;
1217 	if (sc->arpcom.ac_if.if_hwassist)
1218 		r->ti_flags |= TI_BDFLAG_TCP_UDP_CKSUM | TI_BDFLAG_IP_CKSUM;
1219 	r->ti_len = m_new->m_len;
1220 	r->ti_idx = i;
1221 
1222 	return (0);
1223 }
1224 
1225 #else
1226 #include <vm/vm_page.h>
1227 
1228 #if (PAGE_SIZE == 4096)
1229 #define NPAYLOAD 2
1230 #else
1231 #define NPAYLOAD 1
1232 #endif
1233 
1234 #define TCP_HDR_LEN (52 + sizeof(struct ether_header))
1235 #define UDP_HDR_LEN (28 + sizeof(struct ether_header))
1236 #define NFS_HDR_LEN (UDP_HDR_LEN)
1237 static int HDR_LEN =  TCP_HDR_LEN;
1238 
1239 
1240 /*
1241  * Initialize a jumbo receive ring descriptor. This allocates
1242  * a jumbo buffer from the pool managed internally by the driver.
1243  */
1244 static int
1245 ti_newbuf_jumbo(sc, idx, m_old)
1246 	struct ti_softc		*sc;
1247 	int			idx;
1248 	struct mbuf		*m_old;
1249 {
1250 	struct mbuf		*cur, *m_new = NULL;
1251 	struct mbuf		*m[3] = {NULL, NULL, NULL};
1252 	struct ti_rx_desc_ext	*r;
1253 	vm_page_t		frame;
1254 				/* 1 extra buf to make nobufs easy*/
1255 	caddr_t			buf[3] = {NULL, NULL, NULL};
1256 	int			i;
1257 
1258 	if (m_old != NULL) {
1259 		m_new = m_old;
1260 		cur = m_old->m_next;
1261 		for (i = 0; i <= NPAYLOAD; i++){
1262 			m[i] = cur;
1263 			cur = cur->m_next;
1264 		}
1265 	} else {
1266 		/* Allocate the mbufs. */
1267 		MGETHDR(m_new, M_DONTWAIT, MT_DATA);
1268 		if (m_new == NULL) {
1269 			printf("ti%d: mbuf allocation failed "
1270 			       "-- packet dropped!\n", sc->ti_unit);
1271 			goto nobufs;
1272 		}
1273 		MGET(m[NPAYLOAD], M_DONTWAIT, MT_DATA);
1274 		if (m[NPAYLOAD] == NULL) {
1275 			printf("ti%d: cluster mbuf allocation failed "
1276 			       "-- packet dropped!\n", sc->ti_unit);
1277 			goto nobufs;
1278 		}
1279 		MCLGET(m[NPAYLOAD], M_DONTWAIT);
1280 		if ((m[NPAYLOAD]->m_flags & M_EXT) == 0) {
1281 			printf("ti%d: mbuf allocation failed "
1282 			       "-- packet dropped!\n", sc->ti_unit);
1283 			goto nobufs;
1284 		}
1285 		m[NPAYLOAD]->m_len = MCLBYTES;
1286 
1287 		for (i = 0; i < NPAYLOAD; i++){
1288 			MGET(m[i], M_DONTWAIT, MT_DATA);
1289 			if (m[i] == NULL) {
1290 				printf("ti%d: mbuf allocation failed "
1291 				       "-- packet dropped!\n", sc->ti_unit);
1292 				goto nobufs;
1293 			}
1294 			if (!(frame = jumbo_pg_alloc())){
1295 				printf("ti%d: buffer allocation failed "
1296 				       "-- packet dropped!\n", sc->ti_unit);
1297 				printf("      index %d page %d\n", idx, i);
1298 				goto nobufs;
1299 			}
1300 			buf[i] = jumbo_phys_to_kva(VM_PAGE_TO_PHYS(frame));
1301 		}
1302 		for (i = 0; i < NPAYLOAD; i++){
1303 		/* Attach the buffer to the mbuf. */
1304 			m[i]->m_data = (void *)buf[i];
1305 			m[i]->m_len = PAGE_SIZE;
1306 			MEXTADD(m[i], (void *)buf[i], PAGE_SIZE,
1307 				jumbo_freem, NULL, 0, EXT_DISPOSABLE);
1308 			m[i]->m_next = m[i+1];
1309 		}
1310 		/* link the buffers to the header */
1311 		m_new->m_next = m[0];
1312 		m_new->m_data += ETHER_ALIGN;
1313 		if (sc->ti_hdrsplit)
1314 			m_new->m_len = MHLEN - ETHER_ALIGN;
1315 		else
1316 			m_new->m_len = HDR_LEN;
1317 		m_new->m_pkthdr.len = NPAYLOAD * PAGE_SIZE + m_new->m_len;
1318 	}
1319 
1320 	/* Set up the descriptor. */
1321 	r = &sc->ti_rdata->ti_rx_jumbo_ring[idx];
1322 	sc->ti_cdata.ti_rx_jumbo_chain[idx] = m_new;
1323 	TI_HOSTADDR(r->ti_addr0) = vtophys(mtod(m_new, caddr_t));
1324 	r->ti_len0 = m_new->m_len;
1325 
1326 	TI_HOSTADDR(r->ti_addr1) = vtophys(mtod(m[0], caddr_t));
1327 	r->ti_len1 = PAGE_SIZE;
1328 
1329 	TI_HOSTADDR(r->ti_addr2) = vtophys(mtod(m[1], caddr_t));
1330 	r->ti_len2 = m[1]->m_ext.ext_size; /* could be PAGE_SIZE or MCLBYTES */
1331 
1332 	if (PAGE_SIZE == 4096) {
1333 		TI_HOSTADDR(r->ti_addr3) = vtophys(mtod(m[2], caddr_t));
1334 		r->ti_len3 = MCLBYTES;
1335 	} else {
1336 		r->ti_len3 = 0;
1337 	}
1338 	r->ti_type = TI_BDTYPE_RECV_JUMBO_BD;
1339 
1340 	r->ti_flags = TI_BDFLAG_JUMBO_RING|TI_RCB_FLAG_USE_EXT_RX_BD;
1341 
1342 	if (sc->arpcom.ac_if.if_hwassist)
1343 		r->ti_flags |= TI_BDFLAG_TCP_UDP_CKSUM|TI_BDFLAG_IP_CKSUM;
1344 
1345 	r->ti_idx = idx;
1346 
1347 	return (0);
1348 
1349 nobufs:
1350 
1351 	/*
1352 	 * Warning! :
1353 	 * This can only be called before the mbufs are strung together.
1354 	 * If the mbufs are strung together, m_freem() will free the chain,
1355 	 * so that the later mbufs will be freed multiple times.
1356 	 */
1357 	if (m_new)
1358 		m_freem(m_new);
1359 
1360 	for (i = 0; i < 3; i++) {
1361 		if (m[i])
1362 			m_freem(m[i]);
1363 		if (buf[i])
1364 			jumbo_pg_free((vm_offset_t)buf[i]);
1365 	}
1366 	return (ENOBUFS);
1367 }
1368 #endif
1369 
1370 
1371 
1372 /*
1373  * The standard receive ring has 512 entries in it. At 2K per mbuf cluster,
1374  * that's 1MB or memory, which is a lot. For now, we fill only the first
1375  * 256 ring entries and hope that our CPU is fast enough to keep up with
1376  * the NIC.
1377  */
1378 static int
1379 ti_init_rx_ring_std(sc)
1380 	struct ti_softc		*sc;
1381 {
1382 	register int		i;
1383 	struct ti_cmd_desc	cmd;
1384 
1385 	for (i = 0; i < TI_SSLOTS; i++) {
1386 		if (ti_newbuf_std(sc, i, NULL) == ENOBUFS)
1387 			return (ENOBUFS);
1388 	};
1389 
1390 	TI_UPDATE_STDPROD(sc, i - 1);
1391 	sc->ti_std = i - 1;
1392 
1393 	return (0);
1394 }
1395 
1396 static void
1397 ti_free_rx_ring_std(sc)
1398 	struct ti_softc		*sc;
1399 {
1400 	register int		i;
1401 
1402 	for (i = 0; i < TI_STD_RX_RING_CNT; i++) {
1403 		if (sc->ti_cdata.ti_rx_std_chain[i] != NULL) {
1404 			m_freem(sc->ti_cdata.ti_rx_std_chain[i]);
1405 			sc->ti_cdata.ti_rx_std_chain[i] = NULL;
1406 		}
1407 		bzero((char *)&sc->ti_rdata->ti_rx_std_ring[i],
1408 		    sizeof(struct ti_rx_desc));
1409 	}
1410 }
1411 
1412 static int
1413 ti_init_rx_ring_jumbo(sc)
1414 	struct ti_softc		*sc;
1415 {
1416 	register int		i;
1417 	struct ti_cmd_desc	cmd;
1418 
1419 	for (i = 0; i < TI_JUMBO_RX_RING_CNT; i++) {
1420 		if (ti_newbuf_jumbo(sc, i, NULL) == ENOBUFS)
1421 			return (ENOBUFS);
1422 	};
1423 
1424 	TI_UPDATE_JUMBOPROD(sc, i - 1);
1425 	sc->ti_jumbo = i - 1;
1426 
1427 	return (0);
1428 }
1429 
1430 static void
1431 ti_free_rx_ring_jumbo(sc)
1432 	struct ti_softc		*sc;
1433 {
1434 	register int		i;
1435 
1436 	for (i = 0; i < TI_JUMBO_RX_RING_CNT; i++) {
1437 		if (sc->ti_cdata.ti_rx_jumbo_chain[i] != NULL) {
1438 			m_freem(sc->ti_cdata.ti_rx_jumbo_chain[i]);
1439 			sc->ti_cdata.ti_rx_jumbo_chain[i] = NULL;
1440 		}
1441 		bzero((char *)&sc->ti_rdata->ti_rx_jumbo_ring[i],
1442 		    sizeof(struct ti_rx_desc));
1443 	}
1444 }
1445 
1446 static int
1447 ti_init_rx_ring_mini(sc)
1448 	struct ti_softc		*sc;
1449 {
1450 	register int		i;
1451 
1452 	for (i = 0; i < TI_MSLOTS; i++) {
1453 		if (ti_newbuf_mini(sc, i, NULL) == ENOBUFS)
1454 			return (ENOBUFS);
1455 	};
1456 
1457 	TI_UPDATE_MINIPROD(sc, i - 1);
1458 	sc->ti_mini = i - 1;
1459 
1460 	return (0);
1461 }
1462 
1463 static void
1464 ti_free_rx_ring_mini(sc)
1465 	struct ti_softc		*sc;
1466 {
1467 	register int		i;
1468 
1469 	for (i = 0; i < TI_MINI_RX_RING_CNT; i++) {
1470 		if (sc->ti_cdata.ti_rx_mini_chain[i] != NULL) {
1471 			m_freem(sc->ti_cdata.ti_rx_mini_chain[i]);
1472 			sc->ti_cdata.ti_rx_mini_chain[i] = NULL;
1473 		}
1474 		bzero((char *)&sc->ti_rdata->ti_rx_mini_ring[i],
1475 		    sizeof(struct ti_rx_desc));
1476 	}
1477 }
1478 
1479 static void
1480 ti_free_tx_ring(sc)
1481 	struct ti_softc		*sc;
1482 {
1483 	register int		i;
1484 
1485 	if (sc->ti_rdata->ti_tx_ring == NULL)
1486 		return;
1487 
1488 	for (i = 0; i < TI_TX_RING_CNT; i++) {
1489 		if (sc->ti_cdata.ti_tx_chain[i] != NULL) {
1490 			m_freem(sc->ti_cdata.ti_tx_chain[i]);
1491 			sc->ti_cdata.ti_tx_chain[i] = NULL;
1492 		}
1493 		bzero((char *)&sc->ti_rdata->ti_tx_ring[i],
1494 		    sizeof(struct ti_tx_desc));
1495 	}
1496 }
1497 
1498 static int
1499 ti_init_tx_ring(sc)
1500 	struct ti_softc		*sc;
1501 {
1502 	sc->ti_txcnt = 0;
1503 	sc->ti_tx_saved_considx = 0;
1504 	CSR_WRITE_4(sc, TI_MB_SENDPROD_IDX, 0);
1505 	return (0);
1506 }
1507 
1508 /*
1509  * The Tigon 2 firmware has a new way to add/delete multicast addresses,
1510  * but we have to support the old way too so that Tigon 1 cards will
1511  * work.
1512  */
1513 static void
1514 ti_add_mcast(sc, addr)
1515 	struct ti_softc		*sc;
1516 	struct ether_addr	*addr;
1517 {
1518 	struct ti_cmd_desc	cmd;
1519 	u_int16_t		*m;
1520 	u_int32_t		ext[2] = {0, 0};
1521 
1522 	m = (u_int16_t *)&addr->octet[0];
1523 
1524 	switch (sc->ti_hwrev) {
1525 	case TI_HWREV_TIGON:
1526 		CSR_WRITE_4(sc, TI_GCR_MAR0, htons(m[0]));
1527 		CSR_WRITE_4(sc, TI_GCR_MAR1, (htons(m[1]) << 16) | htons(m[2]));
1528 		TI_DO_CMD(TI_CMD_ADD_MCAST_ADDR, 0, 0);
1529 		break;
1530 	case TI_HWREV_TIGON_II:
1531 		ext[0] = htons(m[0]);
1532 		ext[1] = (htons(m[1]) << 16) | htons(m[2]);
1533 		TI_DO_CMD_EXT(TI_CMD_EXT_ADD_MCAST, 0, 0, (caddr_t)&ext, 2);
1534 		break;
1535 	default:
1536 		printf("ti%d: unknown hwrev\n", sc->ti_unit);
1537 		break;
1538 	}
1539 }
1540 
1541 static void
1542 ti_del_mcast(sc, addr)
1543 	struct ti_softc		*sc;
1544 	struct ether_addr	*addr;
1545 {
1546 	struct ti_cmd_desc	cmd;
1547 	u_int16_t		*m;
1548 	u_int32_t		ext[2] = {0, 0};
1549 
1550 	m = (u_int16_t *)&addr->octet[0];
1551 
1552 	switch (sc->ti_hwrev) {
1553 	case TI_HWREV_TIGON:
1554 		CSR_WRITE_4(sc, TI_GCR_MAR0, htons(m[0]));
1555 		CSR_WRITE_4(sc, TI_GCR_MAR1, (htons(m[1]) << 16) | htons(m[2]));
1556 		TI_DO_CMD(TI_CMD_DEL_MCAST_ADDR, 0, 0);
1557 		break;
1558 	case TI_HWREV_TIGON_II:
1559 		ext[0] = htons(m[0]);
1560 		ext[1] = (htons(m[1]) << 16) | htons(m[2]);
1561 		TI_DO_CMD_EXT(TI_CMD_EXT_DEL_MCAST, 0, 0, (caddr_t)&ext, 2);
1562 		break;
1563 	default:
1564 		printf("ti%d: unknown hwrev\n", sc->ti_unit);
1565 		break;
1566 	}
1567 }
1568 
1569 /*
1570  * Configure the Tigon's multicast address filter.
1571  *
1572  * The actual multicast table management is a bit of a pain, thanks to
1573  * slight brain damage on the part of both Alteon and us. With our
1574  * multicast code, we are only alerted when the multicast address table
1575  * changes and at that point we only have the current list of addresses:
1576  * we only know the current state, not the previous state, so we don't
1577  * actually know what addresses were removed or added. The firmware has
1578  * state, but we can't get our grubby mits on it, and there is no 'delete
1579  * all multicast addresses' command. Hence, we have to maintain our own
1580  * state so we know what addresses have been programmed into the NIC at
1581  * any given time.
1582  */
1583 static void
1584 ti_setmulti(sc)
1585 	struct ti_softc		*sc;
1586 {
1587 	struct ifnet		*ifp;
1588 	struct ifmultiaddr	*ifma;
1589 	struct ti_cmd_desc	cmd;
1590 	struct ti_mc_entry	*mc;
1591 	u_int32_t		intrs;
1592 
1593 	ifp = &sc->arpcom.ac_if;
1594 
1595 	if (ifp->if_flags & IFF_ALLMULTI) {
1596 		TI_DO_CMD(TI_CMD_SET_ALLMULTI, TI_CMD_CODE_ALLMULTI_ENB, 0);
1597 		return;
1598 	} else {
1599 		TI_DO_CMD(TI_CMD_SET_ALLMULTI, TI_CMD_CODE_ALLMULTI_DIS, 0);
1600 	}
1601 
1602 	/* Disable interrupts. */
1603 	intrs = CSR_READ_4(sc, TI_MB_HOSTINTR);
1604 	CSR_WRITE_4(sc, TI_MB_HOSTINTR, 1);
1605 
1606 	/* First, zot all the existing filters. */
1607 	while (SLIST_FIRST(&sc->ti_mc_listhead) != NULL) {
1608 		mc = SLIST_FIRST(&sc->ti_mc_listhead);
1609 		ti_del_mcast(sc, &mc->mc_addr);
1610 		SLIST_REMOVE_HEAD(&sc->ti_mc_listhead, mc_entries);
1611 		free(mc, M_DEVBUF);
1612 	}
1613 
1614 	/* Now program new ones. */
1615 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1616 		if (ifma->ifma_addr->sa_family != AF_LINK)
1617 			continue;
1618 		mc = malloc(sizeof(struct ti_mc_entry), M_DEVBUF, M_NOWAIT);
1619 		bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
1620 		    (char *)&mc->mc_addr, ETHER_ADDR_LEN);
1621 		SLIST_INSERT_HEAD(&sc->ti_mc_listhead, mc, mc_entries);
1622 		ti_add_mcast(sc, &mc->mc_addr);
1623 	}
1624 
1625 	/* Re-enable interrupts. */
1626 	CSR_WRITE_4(sc, TI_MB_HOSTINTR, intrs);
1627 }
1628 
1629 /*
1630  * Check to see if the BIOS has configured us for a 64 bit slot when
1631  * we aren't actually in one. If we detect this condition, we can work
1632  * around it on the Tigon 2 by setting a bit in the PCI state register,
1633  * but for the Tigon 1 we must give up and abort the interface attach.
1634  */
1635 static int ti_64bitslot_war(sc)
1636 	struct ti_softc		*sc;
1637 {
1638 	if (!(CSR_READ_4(sc, TI_PCI_STATE) & TI_PCISTATE_32BIT_BUS)) {
1639 		CSR_WRITE_4(sc, 0x600, 0);
1640 		CSR_WRITE_4(sc, 0x604, 0);
1641 		CSR_WRITE_4(sc, 0x600, 0x5555AAAA);
1642 		if (CSR_READ_4(sc, 0x604) == 0x5555AAAA) {
1643 			if (sc->ti_hwrev == TI_HWREV_TIGON)
1644 				return (EINVAL);
1645 			else {
1646 				TI_SETBIT(sc, TI_PCI_STATE,
1647 				    TI_PCISTATE_32BIT_BUS);
1648 				return (0);
1649 			}
1650 		}
1651 	}
1652 
1653 	return (0);
1654 }
1655 
1656 /*
1657  * Do endian, PCI and DMA initialization. Also check the on-board ROM
1658  * self-test results.
1659  */
1660 static int
1661 ti_chipinit(sc)
1662 	struct ti_softc		*sc;
1663 {
1664 	u_int32_t		cacheline;
1665 	u_int32_t		pci_writemax = 0;
1666 	u_int32_t		hdrsplit;
1667 
1668 	/* Initialize link to down state. */
1669 	sc->ti_linkstat = TI_EV_CODE_LINK_DOWN;
1670 
1671 	if (sc->arpcom.ac_if.if_capenable & IFCAP_HWCSUM)
1672 		sc->arpcom.ac_if.if_hwassist = TI_CSUM_FEATURES;
1673 	else
1674 		sc->arpcom.ac_if.if_hwassist = 0;
1675 
1676 	/* Set endianness before we access any non-PCI registers. */
1677 #if BYTE_ORDER == BIG_ENDIAN
1678 	CSR_WRITE_4(sc, TI_MISC_HOST_CTL,
1679 	    TI_MHC_BIGENDIAN_INIT | (TI_MHC_BIGENDIAN_INIT << 24));
1680 #else
1681 	CSR_WRITE_4(sc, TI_MISC_HOST_CTL,
1682 	    TI_MHC_LITTLEENDIAN_INIT | (TI_MHC_LITTLEENDIAN_INIT << 24));
1683 #endif
1684 
1685 	/* Check the ROM failed bit to see if self-tests passed. */
1686 	if (CSR_READ_4(sc, TI_CPU_STATE) & TI_CPUSTATE_ROMFAIL) {
1687 		printf("ti%d: board self-diagnostics failed!\n", sc->ti_unit);
1688 		return (ENODEV);
1689 	}
1690 
1691 	/* Halt the CPU. */
1692 	TI_SETBIT(sc, TI_CPU_STATE, TI_CPUSTATE_HALT);
1693 
1694 	/* Figure out the hardware revision. */
1695 	switch (CSR_READ_4(sc, TI_MISC_HOST_CTL) & TI_MHC_CHIP_REV_MASK) {
1696 	case TI_REV_TIGON_I:
1697 		sc->ti_hwrev = TI_HWREV_TIGON;
1698 		break;
1699 	case TI_REV_TIGON_II:
1700 		sc->ti_hwrev = TI_HWREV_TIGON_II;
1701 		break;
1702 	default:
1703 		printf("ti%d: unsupported chip revision\n", sc->ti_unit);
1704 		return (ENODEV);
1705 	}
1706 
1707 	/* Do special setup for Tigon 2. */
1708 	if (sc->ti_hwrev == TI_HWREV_TIGON_II) {
1709 		TI_SETBIT(sc, TI_CPU_CTL_B, TI_CPUSTATE_HALT);
1710 		TI_SETBIT(sc, TI_MISC_LOCAL_CTL, TI_MLC_SRAM_BANK_512K);
1711 		TI_SETBIT(sc, TI_MISC_CONF, TI_MCR_SRAM_SYNCHRONOUS);
1712 	}
1713 
1714 	/*
1715 	 * We don't have firmware source for the Tigon 1, so Tigon 1 boards
1716 	 * can't do header splitting.
1717 	 */
1718 #ifdef TI_JUMBO_HDRSPLIT
1719 	if (sc->ti_hwrev != TI_HWREV_TIGON)
1720 		sc->ti_hdrsplit = 1;
1721 	else
1722 		printf("ti%d: can't do header splitting on a Tigon I board\n",
1723 		       sc->ti_unit);
1724 #endif /* TI_JUMBO_HDRSPLIT */
1725 
1726 	/* Set up the PCI state register. */
1727 	CSR_WRITE_4(sc, TI_PCI_STATE, TI_PCI_READ_CMD|TI_PCI_WRITE_CMD);
1728 	if (sc->ti_hwrev == TI_HWREV_TIGON_II) {
1729 		TI_SETBIT(sc, TI_PCI_STATE, TI_PCISTATE_USE_MEM_RD_MULT);
1730 	}
1731 
1732 	/* Clear the read/write max DMA parameters. */
1733 	TI_CLRBIT(sc, TI_PCI_STATE, (TI_PCISTATE_WRITE_MAXDMA|
1734 	    TI_PCISTATE_READ_MAXDMA));
1735 
1736 	/* Get cache line size. */
1737 	cacheline = CSR_READ_4(sc, TI_PCI_BIST) & 0xFF;
1738 
1739 	/*
1740 	 * If the system has set enabled the PCI memory write
1741 	 * and invalidate command in the command register, set
1742 	 * the write max parameter accordingly. This is necessary
1743 	 * to use MWI with the Tigon 2.
1744 	 */
1745 	if (CSR_READ_4(sc, TI_PCI_CMDSTAT) & PCIM_CMD_MWIEN) {
1746 		switch (cacheline) {
1747 		case 1:
1748 		case 4:
1749 		case 8:
1750 		case 16:
1751 		case 32:
1752 		case 64:
1753 			break;
1754 		default:
1755 		/* Disable PCI memory write and invalidate. */
1756 			if (bootverbose)
1757 				printf("ti%d: cache line size %d not "
1758 				    "supported; disabling PCI MWI\n",
1759 				    sc->ti_unit, cacheline);
1760 			CSR_WRITE_4(sc, TI_PCI_CMDSTAT, CSR_READ_4(sc,
1761 			    TI_PCI_CMDSTAT) & ~PCIM_CMD_MWIEN);
1762 			break;
1763 		}
1764 	}
1765 
1766 #ifdef __brokenalpha__
1767 	/*
1768 	 * From the Alteon sample driver:
1769 	 * Must insure that we do not cross an 8K (bytes) boundary
1770 	 * for DMA reads.  Our highest limit is 1K bytes.  This is a
1771 	 * restriction on some ALPHA platforms with early revision
1772 	 * 21174 PCI chipsets, such as the AlphaPC 164lx
1773 	 */
1774 	TI_SETBIT(sc, TI_PCI_STATE, pci_writemax|TI_PCI_READMAX_1024);
1775 #else
1776 	TI_SETBIT(sc, TI_PCI_STATE, pci_writemax);
1777 #endif
1778 
1779 	/* This sets the min dma param all the way up (0xff). */
1780 	TI_SETBIT(sc, TI_PCI_STATE, TI_PCISTATE_MINDMA);
1781 
1782 	if (sc->ti_hdrsplit)
1783 		hdrsplit = TI_OPMODE_JUMBO_HDRSPLIT;
1784 	else
1785 		hdrsplit = 0;
1786 
1787 	/* Configure DMA variables. */
1788 #if BYTE_ORDER == BIG_ENDIAN
1789 	CSR_WRITE_4(sc, TI_GCR_OPMODE, TI_OPMODE_BYTESWAP_BD |
1790 	    TI_OPMODE_BYTESWAP_DATA | TI_OPMODE_WORDSWAP_BD |
1791 	    TI_OPMODE_WARN_ENB | TI_OPMODE_FATAL_ENB |
1792 	    TI_OPMODE_DONT_FRAG_JUMBO | hdrsplit);
1793 #else /* BYTE_ORDER */
1794 	CSR_WRITE_4(sc, TI_GCR_OPMODE, TI_OPMODE_BYTESWAP_DATA|
1795 	    TI_OPMODE_WORDSWAP_BD|TI_OPMODE_DONT_FRAG_JUMBO|
1796 	    TI_OPMODE_WARN_ENB|TI_OPMODE_FATAL_ENB | hdrsplit);
1797 #endif /* BYTE_ORDER */
1798 
1799 	/*
1800 	 * Only allow 1 DMA channel to be active at a time.
1801 	 * I don't think this is a good idea, but without it
1802 	 * the firmware racks up lots of nicDmaReadRingFull
1803 	 * errors.  This is not compatible with hardware checksums.
1804 	 */
1805 	if (sc->arpcom.ac_if.if_hwassist == 0)
1806 		TI_SETBIT(sc, TI_GCR_OPMODE, TI_OPMODE_1_DMA_ACTIVE);
1807 
1808 	/* Recommended settings from Tigon manual. */
1809 	CSR_WRITE_4(sc, TI_GCR_DMA_WRITECFG, TI_DMA_STATE_THRESH_8W);
1810 	CSR_WRITE_4(sc, TI_GCR_DMA_READCFG, TI_DMA_STATE_THRESH_8W);
1811 
1812 	if (ti_64bitslot_war(sc)) {
1813 		printf("ti%d: bios thinks we're in a 64 bit slot, "
1814 		    "but we aren't", sc->ti_unit);
1815 		return (EINVAL);
1816 	}
1817 
1818 	return (0);
1819 }
1820 
1821 /*
1822  * Initialize the general information block and firmware, and
1823  * start the CPU(s) running.
1824  */
1825 static int
1826 ti_gibinit(sc)
1827 	struct ti_softc		*sc;
1828 {
1829 	struct ti_rcb		*rcb;
1830 	int			i;
1831 	struct ifnet		*ifp;
1832 
1833 	ifp = &sc->arpcom.ac_if;
1834 
1835 	/* Disable interrupts for now. */
1836 	CSR_WRITE_4(sc, TI_MB_HOSTINTR, 1);
1837 
1838 	/* Tell the chip where to find the general information block. */
1839 	CSR_WRITE_4(sc, TI_GCR_GENINFO_HI, 0);
1840 	CSR_WRITE_4(sc, TI_GCR_GENINFO_LO, vtophys(&sc->ti_rdata->ti_info));
1841 
1842 	/* Load the firmware into SRAM. */
1843 	ti_loadfw(sc);
1844 
1845 	/* Set up the contents of the general info and ring control blocks. */
1846 
1847 	/* Set up the event ring and producer pointer. */
1848 	rcb = &sc->ti_rdata->ti_info.ti_ev_rcb;
1849 
1850 	TI_HOSTADDR(rcb->ti_hostaddr) = vtophys(&sc->ti_rdata->ti_event_ring);
1851 	rcb->ti_flags = 0;
1852 	TI_HOSTADDR(sc->ti_rdata->ti_info.ti_ev_prodidx_ptr) =
1853 	    vtophys(&sc->ti_ev_prodidx);
1854 	sc->ti_ev_prodidx.ti_idx = 0;
1855 	CSR_WRITE_4(sc, TI_GCR_EVENTCONS_IDX, 0);
1856 	sc->ti_ev_saved_considx = 0;
1857 
1858 	/* Set up the command ring and producer mailbox. */
1859 	rcb = &sc->ti_rdata->ti_info.ti_cmd_rcb;
1860 
1861 	sc->ti_rdata->ti_cmd_ring =
1862 	    (struct ti_cmd_desc *)(sc->ti_vhandle + TI_GCR_CMDRING);
1863 	TI_HOSTADDR(rcb->ti_hostaddr) = TI_GCR_NIC_ADDR(TI_GCR_CMDRING);
1864 	rcb->ti_flags = 0;
1865 	rcb->ti_max_len = 0;
1866 	for (i = 0; i < TI_CMD_RING_CNT; i++) {
1867 		CSR_WRITE_4(sc, TI_GCR_CMDRING + (i * 4), 0);
1868 	}
1869 	CSR_WRITE_4(sc, TI_GCR_CMDCONS_IDX, 0);
1870 	CSR_WRITE_4(sc, TI_MB_CMDPROD_IDX, 0);
1871 	sc->ti_cmd_saved_prodidx = 0;
1872 
1873 	/*
1874 	 * Assign the address of the stats refresh buffer.
1875 	 * We re-use the current stats buffer for this to
1876 	 * conserve memory.
1877 	 */
1878 	TI_HOSTADDR(sc->ti_rdata->ti_info.ti_refresh_stats_ptr) =
1879 	    vtophys(&sc->ti_rdata->ti_info.ti_stats);
1880 
1881 	/* Set up the standard receive ring. */
1882 	rcb = &sc->ti_rdata->ti_info.ti_std_rx_rcb;
1883 	TI_HOSTADDR(rcb->ti_hostaddr) = vtophys(&sc->ti_rdata->ti_rx_std_ring);
1884 	rcb->ti_max_len = TI_FRAMELEN;
1885 	rcb->ti_flags = 0;
1886 	if (sc->arpcom.ac_if.if_hwassist)
1887 		rcb->ti_flags |= TI_RCB_FLAG_TCP_UDP_CKSUM |
1888 		     TI_RCB_FLAG_IP_CKSUM | TI_RCB_FLAG_NO_PHDR_CKSUM;
1889 	rcb->ti_flags |= TI_RCB_FLAG_VLAN_ASSIST;
1890 
1891 	/* Set up the jumbo receive ring. */
1892 	rcb = &sc->ti_rdata->ti_info.ti_jumbo_rx_rcb;
1893 	TI_HOSTADDR(rcb->ti_hostaddr) =
1894 	    vtophys(&sc->ti_rdata->ti_rx_jumbo_ring);
1895 
1896 #ifdef TI_PRIVATE_JUMBOS
1897 	rcb->ti_max_len = TI_JUMBO_FRAMELEN;
1898 	rcb->ti_flags = 0;
1899 #else
1900 	rcb->ti_max_len = PAGE_SIZE;
1901 	rcb->ti_flags = TI_RCB_FLAG_USE_EXT_RX_BD;
1902 #endif
1903 	if (sc->arpcom.ac_if.if_hwassist)
1904 		rcb->ti_flags |= TI_RCB_FLAG_TCP_UDP_CKSUM |
1905 		     TI_RCB_FLAG_IP_CKSUM | TI_RCB_FLAG_NO_PHDR_CKSUM;
1906 	rcb->ti_flags |= TI_RCB_FLAG_VLAN_ASSIST;
1907 
1908 	/*
1909 	 * Set up the mini ring. Only activated on the
1910 	 * Tigon 2 but the slot in the config block is
1911 	 * still there on the Tigon 1.
1912 	 */
1913 	rcb = &sc->ti_rdata->ti_info.ti_mini_rx_rcb;
1914 	TI_HOSTADDR(rcb->ti_hostaddr) =
1915 	    vtophys(&sc->ti_rdata->ti_rx_mini_ring);
1916 	rcb->ti_max_len = MHLEN - ETHER_ALIGN;
1917 	if (sc->ti_hwrev == TI_HWREV_TIGON)
1918 		rcb->ti_flags = TI_RCB_FLAG_RING_DISABLED;
1919 	else
1920 		rcb->ti_flags = 0;
1921 	if (sc->arpcom.ac_if.if_hwassist)
1922 		rcb->ti_flags |= TI_RCB_FLAG_TCP_UDP_CKSUM |
1923 		     TI_RCB_FLAG_IP_CKSUM | TI_RCB_FLAG_NO_PHDR_CKSUM;
1924 	rcb->ti_flags |= TI_RCB_FLAG_VLAN_ASSIST;
1925 
1926 	/*
1927 	 * Set up the receive return ring.
1928 	 */
1929 	rcb = &sc->ti_rdata->ti_info.ti_return_rcb;
1930 	TI_HOSTADDR(rcb->ti_hostaddr) =
1931 	    vtophys(&sc->ti_rdata->ti_rx_return_ring);
1932 	rcb->ti_flags = 0;
1933 	rcb->ti_max_len = TI_RETURN_RING_CNT;
1934 	TI_HOSTADDR(sc->ti_rdata->ti_info.ti_return_prodidx_ptr) =
1935 	    vtophys(&sc->ti_return_prodidx);
1936 
1937 	/*
1938 	 * Set up the tx ring. Note: for the Tigon 2, we have the option
1939 	 * of putting the transmit ring in the host's address space and
1940 	 * letting the chip DMA it instead of leaving the ring in the NIC's
1941 	 * memory and accessing it through the shared memory region. We
1942 	 * do this for the Tigon 2, but it doesn't work on the Tigon 1,
1943 	 * so we have to revert to the shared memory scheme if we detect
1944 	 * a Tigon 1 chip.
1945 	 */
1946 	CSR_WRITE_4(sc, TI_WINBASE, TI_TX_RING_BASE);
1947 	if (sc->ti_hwrev == TI_HWREV_TIGON) {
1948 		sc->ti_rdata->ti_tx_ring_nic =
1949 		    (struct ti_tx_desc *)(sc->ti_vhandle + TI_WINDOW);
1950 	}
1951 	bzero((char *)sc->ti_rdata->ti_tx_ring,
1952 	    TI_TX_RING_CNT * sizeof(struct ti_tx_desc));
1953 	rcb = &sc->ti_rdata->ti_info.ti_tx_rcb;
1954 	if (sc->ti_hwrev == TI_HWREV_TIGON)
1955 		rcb->ti_flags = 0;
1956 	else
1957 		rcb->ti_flags = TI_RCB_FLAG_HOST_RING;
1958 	rcb->ti_flags |= TI_RCB_FLAG_VLAN_ASSIST;
1959 	if (sc->arpcom.ac_if.if_hwassist)
1960 		rcb->ti_flags |= TI_RCB_FLAG_TCP_UDP_CKSUM |
1961 		     TI_RCB_FLAG_IP_CKSUM | TI_RCB_FLAG_NO_PHDR_CKSUM;
1962 	rcb->ti_max_len = TI_TX_RING_CNT;
1963 	if (sc->ti_hwrev == TI_HWREV_TIGON)
1964 		TI_HOSTADDR(rcb->ti_hostaddr) = TI_TX_RING_BASE;
1965 	else
1966 		TI_HOSTADDR(rcb->ti_hostaddr) =
1967 		    vtophys(&sc->ti_rdata->ti_tx_ring);
1968 	TI_HOSTADDR(sc->ti_rdata->ti_info.ti_tx_considx_ptr) =
1969 	    vtophys(&sc->ti_tx_considx);
1970 
1971 	/* Set up tuneables */
1972 #if 0
1973 	if (ifp->if_mtu > (ETHERMTU + ETHER_HDR_LEN + ETHER_CRC_LEN))
1974 		CSR_WRITE_4(sc, TI_GCR_RX_COAL_TICKS,
1975 		    (sc->ti_rx_coal_ticks / 10));
1976 	else
1977 #endif
1978 		CSR_WRITE_4(sc, TI_GCR_RX_COAL_TICKS, sc->ti_rx_coal_ticks);
1979 	CSR_WRITE_4(sc, TI_GCR_TX_COAL_TICKS, sc->ti_tx_coal_ticks);
1980 	CSR_WRITE_4(sc, TI_GCR_STAT_TICKS, sc->ti_stat_ticks);
1981 	CSR_WRITE_4(sc, TI_GCR_RX_MAX_COAL_BD, sc->ti_rx_max_coal_bds);
1982 	CSR_WRITE_4(sc, TI_GCR_TX_MAX_COAL_BD, sc->ti_tx_max_coal_bds);
1983 	CSR_WRITE_4(sc, TI_GCR_TX_BUFFER_RATIO, sc->ti_tx_buf_ratio);
1984 
1985 	/* Turn interrupts on. */
1986 	CSR_WRITE_4(sc, TI_GCR_MASK_INTRS, 0);
1987 	CSR_WRITE_4(sc, TI_MB_HOSTINTR, 0);
1988 
1989 	/* Start CPU. */
1990 	TI_CLRBIT(sc, TI_CPU_STATE, (TI_CPUSTATE_HALT|TI_CPUSTATE_STEP));
1991 
1992 	return (0);
1993 }
1994 
1995 /*
1996  * Probe for a Tigon chip. Check the PCI vendor and device IDs
1997  * against our list and return its name if we find a match.
1998  */
1999 static int
2000 ti_probe(dev)
2001 	device_t		dev;
2002 {
2003 	struct ti_type		*t;
2004 
2005 	t = ti_devs;
2006 
2007 	while (t->ti_name != NULL) {
2008 		if ((pci_get_vendor(dev) == t->ti_vid) &&
2009 		    (pci_get_device(dev) == t->ti_did)) {
2010 			device_set_desc(dev, t->ti_name);
2011 			return (0);
2012 		}
2013 		t++;
2014 	}
2015 
2016 	return (ENXIO);
2017 }
2018 
2019 static int
2020 ti_attach(dev)
2021 	device_t		dev;
2022 {
2023 	struct ifnet		*ifp;
2024 	struct ti_softc		*sc;
2025 	int			unit, error = 0, rid;
2026 
2027 	sc = device_get_softc(dev);
2028 	unit = device_get_unit(dev);
2029 
2030 	mtx_init(&sc->ti_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK,
2031 	    MTX_DEF | MTX_RECURSE);
2032 	ifmedia_init(&sc->ifmedia, IFM_IMASK, ti_ifmedia_upd, ti_ifmedia_sts);
2033 	sc->arpcom.ac_if.if_capabilities = IFCAP_HWCSUM |
2034 	    IFCAP_VLAN_HWTAGGING | IFCAP_VLAN_MTU;
2035 	sc->arpcom.ac_if.if_capenable = sc->arpcom.ac_if.if_capabilities;
2036 
2037 	/*
2038 	 * Map control/status registers.
2039 	 */
2040 	pci_enable_busmaster(dev);
2041 
2042 	rid = TI_PCI_LOMEM;
2043 	sc->ti_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
2044 	    RF_ACTIVE|PCI_RF_DENSE);
2045 
2046 	if (sc->ti_res == NULL) {
2047 		printf ("ti%d: couldn't map memory\n", unit);
2048 		error = ENXIO;
2049 		goto fail;
2050 	}
2051 
2052 	sc->ti_btag = rman_get_bustag(sc->ti_res);
2053 	sc->ti_bhandle = rman_get_bushandle(sc->ti_res);
2054 	sc->ti_vhandle = (vm_offset_t)rman_get_virtual(sc->ti_res);
2055 
2056 	/* Allocate interrupt */
2057 	rid = 0;
2058 
2059 	sc->ti_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
2060 	    RF_SHAREABLE | RF_ACTIVE);
2061 
2062 	if (sc->ti_irq == NULL) {
2063 		printf("ti%d: couldn't map interrupt\n", unit);
2064 		error = ENXIO;
2065 		goto fail;
2066 	}
2067 
2068 	sc->ti_unit = unit;
2069 
2070 	if (ti_chipinit(sc)) {
2071 		printf("ti%d: chip initialization failed\n", sc->ti_unit);
2072 		error = ENXIO;
2073 		goto fail;
2074 	}
2075 
2076 	/* Zero out the NIC's on-board SRAM. */
2077 	ti_mem(sc, 0x2000, 0x100000 - 0x2000,  NULL);
2078 
2079 	/* Init again -- zeroing memory may have clobbered some registers. */
2080 	if (ti_chipinit(sc)) {
2081 		printf("ti%d: chip initialization failed\n", sc->ti_unit);
2082 		error = ENXIO;
2083 		goto fail;
2084 	}
2085 
2086 	/*
2087 	 * Get station address from the EEPROM. Note: the manual states
2088 	 * that the MAC address is at offset 0x8c, however the data is
2089 	 * stored as two longwords (since that's how it's loaded into
2090 	 * the NIC). This means the MAC address is actually preceded
2091 	 * by two zero bytes. We need to skip over those.
2092 	 */
2093 	if (ti_read_eeprom(sc, (caddr_t)&sc->arpcom.ac_enaddr,
2094 				TI_EE_MAC_OFFSET + 2, ETHER_ADDR_LEN)) {
2095 		printf("ti%d: failed to read station address\n", unit);
2096 		error = ENXIO;
2097 		goto fail;
2098 	}
2099 
2100 	/* Allocate the general information block and ring buffers. */
2101 	sc->ti_rdata = contigmalloc(sizeof(struct ti_ring_data), M_DEVBUF,
2102 	    M_NOWAIT, 0, 0xffffffff, PAGE_SIZE, 0);
2103 
2104 	if (sc->ti_rdata == NULL) {
2105 		printf("ti%d: no memory for list buffers!\n", sc->ti_unit);
2106 		error = ENXIO;
2107 		goto fail;
2108 	}
2109 
2110 	bzero(sc->ti_rdata, sizeof(struct ti_ring_data));
2111 
2112 	/* Try to allocate memory for jumbo buffers. */
2113 #ifdef TI_PRIVATE_JUMBOS
2114 	if (ti_alloc_jumbo_mem(sc)) {
2115 		printf("ti%d: jumbo buffer allocation failed\n", sc->ti_unit);
2116 		error = ENXIO;
2117 		goto fail;
2118 	}
2119 #else
2120 	if (!jumbo_vm_init()) {
2121 		printf("ti%d: VM initialization failed!\n", sc->ti_unit);
2122 		error = ENOMEM;
2123 		goto fail;
2124 	}
2125 #endif
2126 
2127 	/*
2128 	 * We really need a better way to tell a 1000baseTX card
2129 	 * from a 1000baseSX one, since in theory there could be
2130 	 * OEMed 1000baseTX cards from lame vendors who aren't
2131 	 * clever enough to change the PCI ID. For the moment
2132 	 * though, the AceNIC is the only copper card available.
2133 	 */
2134 	if (pci_get_vendor(dev) == ALT_VENDORID &&
2135 	    pci_get_device(dev) == ALT_DEVICEID_ACENIC_COPPER)
2136 		sc->ti_copper = 1;
2137 	/* Ok, it's not the only copper card available. */
2138 	if (pci_get_vendor(dev) == NG_VENDORID &&
2139 	    pci_get_device(dev) == NG_DEVICEID_GA620T)
2140 		sc->ti_copper = 1;
2141 
2142 	/* Set default tuneable values. */
2143 	sc->ti_stat_ticks = 2 * TI_TICKS_PER_SEC;
2144 #if 0
2145 	sc->ti_rx_coal_ticks = TI_TICKS_PER_SEC / 5000;
2146 #endif
2147 	sc->ti_rx_coal_ticks = 170;
2148 	sc->ti_tx_coal_ticks = TI_TICKS_PER_SEC / 500;
2149 	sc->ti_rx_max_coal_bds = 64;
2150 #if 0
2151 	sc->ti_tx_max_coal_bds = 128;
2152 #endif
2153 	sc->ti_tx_max_coal_bds = 32;
2154 	sc->ti_tx_buf_ratio = 21;
2155 
2156 	/* Set up ifnet structure */
2157 	ifp = &sc->arpcom.ac_if;
2158 	ifp->if_softc = sc;
2159 	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
2160 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
2161 	tis[unit] = sc;
2162 	ifp->if_ioctl = ti_ioctl;
2163 	ifp->if_start = ti_start;
2164 	ifp->if_watchdog = ti_watchdog;
2165 	ifp->if_init = ti_init;
2166 	ifp->if_mtu = ETHERMTU;
2167 	ifp->if_snd.ifq_maxlen = TI_TX_RING_CNT - 1;
2168 
2169 	/* Set up ifmedia support. */
2170 	if (sc->ti_copper) {
2171 		/*
2172 		 * Copper cards allow manual 10/100 mode selection,
2173 		 * but not manual 1000baseTX mode selection. Why?
2174 		 * Becuase currently there's no way to specify the
2175 		 * master/slave setting through the firmware interface,
2176 		 * so Alteon decided to just bag it and handle it
2177 		 * via autonegotiation.
2178 		 */
2179 		ifmedia_add(&sc->ifmedia, IFM_ETHER|IFM_10_T, 0, NULL);
2180 		ifmedia_add(&sc->ifmedia,
2181 		    IFM_ETHER|IFM_10_T|IFM_FDX, 0, NULL);
2182 		ifmedia_add(&sc->ifmedia, IFM_ETHER|IFM_100_TX, 0, NULL);
2183 		ifmedia_add(&sc->ifmedia,
2184 		    IFM_ETHER|IFM_100_TX|IFM_FDX, 0, NULL);
2185 		ifmedia_add(&sc->ifmedia, IFM_ETHER|IFM_1000_T, 0, NULL);
2186 		ifmedia_add(&sc->ifmedia,
2187 		    IFM_ETHER|IFM_1000_T|IFM_FDX, 0, NULL);
2188 	} else {
2189 		/* Fiber cards don't support 10/100 modes. */
2190 		ifmedia_add(&sc->ifmedia, IFM_ETHER|IFM_1000_SX, 0, NULL);
2191 		ifmedia_add(&sc->ifmedia,
2192 		    IFM_ETHER|IFM_1000_SX|IFM_FDX, 0, NULL);
2193 	}
2194 	ifmedia_add(&sc->ifmedia, IFM_ETHER|IFM_AUTO, 0, NULL);
2195 	ifmedia_set(&sc->ifmedia, IFM_ETHER|IFM_AUTO);
2196 
2197 	/*
2198 	 * We're assuming here that card initialization is a sequential
2199 	 * thing.  If it isn't, multiple cards probing at the same time
2200 	 * could stomp on the list of softcs here.
2201 	 */
2202 
2203 	/* Register the device */
2204 	sc->dev = make_dev(&ti_cdevsw, sc->ti_unit, UID_ROOT, GID_OPERATOR,
2205 			   0600, "ti%d", sc->ti_unit);
2206 	sc->dev->si_drv1 = sc;
2207 
2208 	/*
2209 	 * Call MI attach routine.
2210 	 */
2211 	ether_ifattach(ifp, sc->arpcom.ac_enaddr);
2212 
2213 	/* Hook interrupt last to avoid having to lock softc */
2214 	error = bus_setup_intr(dev, sc->ti_irq, INTR_TYPE_NET,
2215 	   ti_intr, sc, &sc->ti_intrhand);
2216 
2217 	if (error) {
2218 		printf("ti%d: couldn't set up irq\n", unit);
2219 		ether_ifdetach(ifp);
2220 		goto fail;
2221 	}
2222 
2223 fail:
2224 	if (sc && error)
2225 		ti_detach(dev);
2226 
2227 	return (error);
2228 }
2229 
2230 /*
2231  * Shutdown hardware and free up resources. This can be called any
2232  * time after the mutex has been initialized. It is called in both
2233  * the error case in attach and the normal detach case so it needs
2234  * to be careful about only freeing resources that have actually been
2235  * allocated.
2236  */
2237 static int
2238 ti_detach(dev)
2239 	device_t		dev;
2240 {
2241 	struct ti_softc		*sc;
2242 	struct ifnet		*ifp;
2243 
2244 	sc = device_get_softc(dev);
2245 	destroy_dev(sc->dev);
2246 	KASSERT(mtx_initialized(&sc->ti_mtx), ("ti mutex not initialized"));
2247 	TI_LOCK(sc);
2248 	ifp = &sc->arpcom.ac_if;
2249 
2250 	/* These should only be active if attach succeeded */
2251 	if (device_is_attached(dev)) {
2252 		ti_stop(sc);
2253 		ether_ifdetach(ifp);
2254 		bus_generic_detach(dev);
2255 	}
2256 	ifmedia_removeall(&sc->ifmedia);
2257 
2258 	if (sc->ti_intrhand)
2259 		bus_teardown_intr(dev, sc->ti_irq, sc->ti_intrhand);
2260 	if (sc->ti_irq)
2261 		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->ti_irq);
2262 	if (sc->ti_res) {
2263 		bus_release_resource(dev, SYS_RES_MEMORY, TI_PCI_LOMEM,
2264 		    sc->ti_res);
2265 	}
2266 
2267 #ifdef TI_PRIVATE_JUMBOS
2268 	if (sc->ti_cdata.ti_jumbo_buf)
2269 		contigfree(sc->ti_cdata.ti_jumbo_buf, TI_JMEM, M_DEVBUF);
2270 #endif
2271 	if (sc->ti_rdata)
2272 		contigfree(sc->ti_rdata, sizeof(struct ti_ring_data), M_DEVBUF);
2273 
2274 	TI_UNLOCK(sc);
2275 	mtx_destroy(&sc->ti_mtx);
2276 
2277 	return (0);
2278 }
2279 
2280 #ifdef TI_JUMBO_HDRSPLIT
2281 /*
2282  * If hdr_len is 0, that means that header splitting wasn't done on
2283  * this packet for some reason.  The two most likely reasons are that
2284  * the protocol isn't a supported protocol for splitting, or this
2285  * packet had a fragment offset that wasn't 0.
2286  *
2287  * The header length, if it is non-zero, will always be the length of
2288  * the headers on the packet, but that length could be longer than the
2289  * first mbuf.  So we take the minimum of the two as the actual
2290  * length.
2291  */
2292 static __inline void
2293 ti_hdr_split(struct mbuf *top, int hdr_len, int pkt_len, int idx)
2294 {
2295 	int i = 0;
2296 	int lengths[4] = {0, 0, 0, 0};
2297 	struct mbuf *m, *mp;
2298 
2299 	if (hdr_len != 0)
2300 		top->m_len = min(hdr_len, top->m_len);
2301 	pkt_len -= top->m_len;
2302 	lengths[i++] = top->m_len;
2303 
2304 	mp = top;
2305 	for (m = top->m_next; m && pkt_len; m = m->m_next) {
2306 		m->m_len = m->m_ext.ext_size = min(m->m_len, pkt_len);
2307 		pkt_len -= m->m_len;
2308 		lengths[i++] = m->m_len;
2309 		mp = m;
2310 	}
2311 
2312 #if 0
2313 	if (hdr_len != 0)
2314 		printf("got split packet: ");
2315 	else
2316 		printf("got non-split packet: ");
2317 
2318 	printf("%d,%d,%d,%d = %d\n", lengths[0],
2319 	    lengths[1], lengths[2], lengths[3],
2320 	    lengths[0] + lengths[1] + lengths[2] +
2321 	    lengths[3]);
2322 #endif
2323 
2324 	if (pkt_len)
2325 		panic("header splitting didn't");
2326 
2327 	if (m) {
2328 		m_freem(m);
2329 		mp->m_next = NULL;
2330 
2331 	}
2332 	if (mp->m_next != NULL)
2333 		panic("ti_hdr_split: last mbuf in chain should be null");
2334 }
2335 #endif /* TI_JUMBO_HDRSPLIT */
2336 
2337 /*
2338  * Frame reception handling. This is called if there's a frame
2339  * on the receive return list.
2340  *
2341  * Note: we have to be able to handle three possibilities here:
2342  * 1) the frame is from the mini receive ring (can only happen)
2343  *    on Tigon 2 boards)
2344  * 2) the frame is from the jumbo recieve ring
2345  * 3) the frame is from the standard receive ring
2346  */
2347 
2348 static void
2349 ti_rxeof(sc)
2350 	struct ti_softc		*sc;
2351 {
2352 	struct ifnet		*ifp;
2353 	struct ti_cmd_desc	cmd;
2354 
2355 	TI_LOCK_ASSERT(sc);
2356 
2357 	ifp = &sc->arpcom.ac_if;
2358 
2359 	while (sc->ti_rx_saved_considx != sc->ti_return_prodidx.ti_idx) {
2360 		struct ti_rx_desc	*cur_rx;
2361 		u_int32_t		rxidx;
2362 		struct mbuf		*m = NULL;
2363 		u_int16_t		vlan_tag = 0;
2364 		int			have_tag = 0;
2365 
2366 		cur_rx =
2367 		    &sc->ti_rdata->ti_rx_return_ring[sc->ti_rx_saved_considx];
2368 		rxidx = cur_rx->ti_idx;
2369 		TI_INC(sc->ti_rx_saved_considx, TI_RETURN_RING_CNT);
2370 
2371 		if (cur_rx->ti_flags & TI_BDFLAG_VLAN_TAG) {
2372 			have_tag = 1;
2373 			vlan_tag = cur_rx->ti_vlan_tag & 0xfff;
2374 		}
2375 
2376 		if (cur_rx->ti_flags & TI_BDFLAG_JUMBO_RING) {
2377 
2378 			TI_INC(sc->ti_jumbo, TI_JUMBO_RX_RING_CNT);
2379 			m = sc->ti_cdata.ti_rx_jumbo_chain[rxidx];
2380 			sc->ti_cdata.ti_rx_jumbo_chain[rxidx] = NULL;
2381 			if (cur_rx->ti_flags & TI_BDFLAG_ERROR) {
2382 				ifp->if_ierrors++;
2383 				ti_newbuf_jumbo(sc, sc->ti_jumbo, m);
2384 				continue;
2385 			}
2386 			if (ti_newbuf_jumbo(sc, sc->ti_jumbo, NULL) == ENOBUFS) {
2387 				ifp->if_ierrors++;
2388 				ti_newbuf_jumbo(sc, sc->ti_jumbo, m);
2389 				continue;
2390 			}
2391 #ifdef TI_PRIVATE_JUMBOS
2392 			m->m_len = cur_rx->ti_len;
2393 #else /* TI_PRIVATE_JUMBOS */
2394 #ifdef TI_JUMBO_HDRSPLIT
2395 			if (sc->ti_hdrsplit)
2396 				ti_hdr_split(m, TI_HOSTADDR(cur_rx->ti_addr),
2397 					     cur_rx->ti_len, rxidx);
2398 			else
2399 #endif /* TI_JUMBO_HDRSPLIT */
2400 			m_adj(m, cur_rx->ti_len - m->m_pkthdr.len);
2401 #endif /* TI_PRIVATE_JUMBOS */
2402 		} else if (cur_rx->ti_flags & TI_BDFLAG_MINI_RING) {
2403 			TI_INC(sc->ti_mini, TI_MINI_RX_RING_CNT);
2404 			m = sc->ti_cdata.ti_rx_mini_chain[rxidx];
2405 			sc->ti_cdata.ti_rx_mini_chain[rxidx] = NULL;
2406 			if (cur_rx->ti_flags & TI_BDFLAG_ERROR) {
2407 				ifp->if_ierrors++;
2408 				ti_newbuf_mini(sc, sc->ti_mini, m);
2409 				continue;
2410 			}
2411 			if (ti_newbuf_mini(sc, sc->ti_mini, NULL) == ENOBUFS) {
2412 				ifp->if_ierrors++;
2413 				ti_newbuf_mini(sc, sc->ti_mini, m);
2414 				continue;
2415 			}
2416 			m->m_len = cur_rx->ti_len;
2417 		} else {
2418 			TI_INC(sc->ti_std, TI_STD_RX_RING_CNT);
2419 			m = sc->ti_cdata.ti_rx_std_chain[rxidx];
2420 			sc->ti_cdata.ti_rx_std_chain[rxidx] = NULL;
2421 			if (cur_rx->ti_flags & TI_BDFLAG_ERROR) {
2422 				ifp->if_ierrors++;
2423 				ti_newbuf_std(sc, sc->ti_std, m);
2424 				continue;
2425 			}
2426 			if (ti_newbuf_std(sc, sc->ti_std, NULL) == ENOBUFS) {
2427 				ifp->if_ierrors++;
2428 				ti_newbuf_std(sc, sc->ti_std, m);
2429 				continue;
2430 			}
2431 			m->m_len = cur_rx->ti_len;
2432 		}
2433 
2434 		m->m_pkthdr.len = cur_rx->ti_len;
2435 		ifp->if_ipackets++;
2436 		m->m_pkthdr.rcvif = ifp;
2437 
2438 		if (ifp->if_hwassist) {
2439 			m->m_pkthdr.csum_flags |= CSUM_IP_CHECKED |
2440 			    CSUM_DATA_VALID;
2441 			if ((cur_rx->ti_ip_cksum ^ 0xffff) == 0)
2442 				m->m_pkthdr.csum_flags |= CSUM_IP_VALID;
2443 			m->m_pkthdr.csum_data = cur_rx->ti_tcp_udp_cksum;
2444 		}
2445 
2446 		/*
2447 		 * If we received a packet with a vlan tag,
2448 		 * tag it before passing the packet upward.
2449 		 */
2450 		if (have_tag)
2451 			VLAN_INPUT_TAG(ifp, m, vlan_tag, continue);
2452 		TI_UNLOCK(sc);
2453 		(*ifp->if_input)(ifp, m);
2454 		TI_LOCK(sc);
2455 	}
2456 
2457 	/* Only necessary on the Tigon 1. */
2458 	if (sc->ti_hwrev == TI_HWREV_TIGON)
2459 		CSR_WRITE_4(sc, TI_GCR_RXRETURNCONS_IDX,
2460 		    sc->ti_rx_saved_considx);
2461 
2462 	TI_UPDATE_STDPROD(sc, sc->ti_std);
2463 	TI_UPDATE_MINIPROD(sc, sc->ti_mini);
2464 	TI_UPDATE_JUMBOPROD(sc, sc->ti_jumbo);
2465 }
2466 
2467 static void
2468 ti_txeof(sc)
2469 	struct ti_softc		*sc;
2470 {
2471 	struct ti_tx_desc	*cur_tx = NULL;
2472 	struct ifnet		*ifp;
2473 
2474 	ifp = &sc->arpcom.ac_if;
2475 
2476 	/*
2477 	 * Go through our tx ring and free mbufs for those
2478 	 * frames that have been sent.
2479 	 */
2480 	while (sc->ti_tx_saved_considx != sc->ti_tx_considx.ti_idx) {
2481 		u_int32_t		idx = 0;
2482 
2483 		idx = sc->ti_tx_saved_considx;
2484 		if (sc->ti_hwrev == TI_HWREV_TIGON) {
2485 			if (idx > 383)
2486 				CSR_WRITE_4(sc, TI_WINBASE,
2487 				    TI_TX_RING_BASE + 6144);
2488 			else if (idx > 255)
2489 				CSR_WRITE_4(sc, TI_WINBASE,
2490 				    TI_TX_RING_BASE + 4096);
2491 			else if (idx > 127)
2492 				CSR_WRITE_4(sc, TI_WINBASE,
2493 				    TI_TX_RING_BASE + 2048);
2494 			else
2495 				CSR_WRITE_4(sc, TI_WINBASE,
2496 				    TI_TX_RING_BASE);
2497 			cur_tx = &sc->ti_rdata->ti_tx_ring_nic[idx % 128];
2498 		} else
2499 			cur_tx = &sc->ti_rdata->ti_tx_ring[idx];
2500 		if (cur_tx->ti_flags & TI_BDFLAG_END)
2501 			ifp->if_opackets++;
2502 		if (sc->ti_cdata.ti_tx_chain[idx] != NULL) {
2503 			m_freem(sc->ti_cdata.ti_tx_chain[idx]);
2504 			sc->ti_cdata.ti_tx_chain[idx] = NULL;
2505 		}
2506 		sc->ti_txcnt--;
2507 		TI_INC(sc->ti_tx_saved_considx, TI_TX_RING_CNT);
2508 		ifp->if_timer = 0;
2509 	}
2510 
2511 	if (cur_tx != NULL)
2512 		ifp->if_flags &= ~IFF_OACTIVE;
2513 }
2514 
2515 static void
2516 ti_intr(xsc)
2517 	void			*xsc;
2518 {
2519 	struct ti_softc		*sc;
2520 	struct ifnet		*ifp;
2521 
2522 	sc = xsc;
2523 	TI_LOCK(sc);
2524 	ifp = &sc->arpcom.ac_if;
2525 
2526 /*#ifdef notdef*/
2527 	/* Avoid this for now -- checking this register is expensive. */
2528 	/* Make sure this is really our interrupt. */
2529 	if (!(CSR_READ_4(sc, TI_MISC_HOST_CTL) & TI_MHC_INTSTATE)) {
2530 		TI_UNLOCK(sc);
2531 		return;
2532 	}
2533 /*#endif*/
2534 
2535 	/* Ack interrupt and stop others from occuring. */
2536 	CSR_WRITE_4(sc, TI_MB_HOSTINTR, 1);
2537 
2538 	if (ifp->if_flags & IFF_RUNNING) {
2539 		/* Check RX return ring producer/consumer */
2540 		ti_rxeof(sc);
2541 
2542 		/* Check TX ring producer/consumer */
2543 		ti_txeof(sc);
2544 	}
2545 
2546 	ti_handle_events(sc);
2547 
2548 	/* Re-enable interrupts. */
2549 	CSR_WRITE_4(sc, TI_MB_HOSTINTR, 0);
2550 
2551 	if (ifp->if_flags & IFF_RUNNING && ifp->if_snd.ifq_head != NULL)
2552 		ti_start(ifp);
2553 
2554 	TI_UNLOCK(sc);
2555 }
2556 
2557 static void
2558 ti_stats_update(sc)
2559 	struct ti_softc		*sc;
2560 {
2561 	struct ifnet		*ifp;
2562 
2563 	ifp = &sc->arpcom.ac_if;
2564 
2565 	ifp->if_collisions +=
2566 	   (sc->ti_rdata->ti_info.ti_stats.dot3StatsSingleCollisionFrames +
2567 	   sc->ti_rdata->ti_info.ti_stats.dot3StatsMultipleCollisionFrames +
2568 	   sc->ti_rdata->ti_info.ti_stats.dot3StatsExcessiveCollisions +
2569 	   sc->ti_rdata->ti_info.ti_stats.dot3StatsLateCollisions) -
2570 	   ifp->if_collisions;
2571 }
2572 
2573 /*
2574  * Encapsulate an mbuf chain in the tx ring  by coupling the mbuf data
2575  * pointers to descriptors.
2576  */
2577 static int
2578 ti_encap(sc, m_head, txidx)
2579 	struct ti_softc		*sc;
2580 	struct mbuf		*m_head;
2581 	u_int32_t		*txidx;
2582 {
2583 	struct ti_tx_desc	*f = NULL;
2584 	struct mbuf		*m;
2585 	u_int32_t		frag, cur, cnt = 0;
2586 	u_int16_t		csum_flags = 0;
2587 	struct m_tag		*mtag;
2588 
2589 	m = m_head;
2590 	cur = frag = *txidx;
2591 
2592 	if (m_head->m_pkthdr.csum_flags) {
2593 		if (m_head->m_pkthdr.csum_flags & CSUM_IP)
2594 			csum_flags |= TI_BDFLAG_IP_CKSUM;
2595 		if (m_head->m_pkthdr.csum_flags & (CSUM_TCP | CSUM_UDP))
2596 			csum_flags |= TI_BDFLAG_TCP_UDP_CKSUM;
2597 		if (m_head->m_flags & M_LASTFRAG)
2598 			csum_flags |= TI_BDFLAG_IP_FRAG_END;
2599 		else if (m_head->m_flags & M_FRAG)
2600 			csum_flags |= TI_BDFLAG_IP_FRAG;
2601 	}
2602 
2603 	mtag = VLAN_OUTPUT_TAG(&sc->arpcom.ac_if, m);
2604 
2605 	/*
2606 	 * Start packing the mbufs in this chain into
2607 	 * the fragment pointers. Stop when we run out
2608 	 * of fragments or hit the end of the mbuf chain.
2609 	 */
2610 	for (m = m_head; m != NULL; m = m->m_next) {
2611 		if (m->m_len != 0) {
2612 			if (sc->ti_hwrev == TI_HWREV_TIGON) {
2613 				if (frag > 383)
2614 					CSR_WRITE_4(sc, TI_WINBASE,
2615 					    TI_TX_RING_BASE + 6144);
2616 				else if (frag > 255)
2617 					CSR_WRITE_4(sc, TI_WINBASE,
2618 					    TI_TX_RING_BASE + 4096);
2619 				else if (frag > 127)
2620 					CSR_WRITE_4(sc, TI_WINBASE,
2621 					    TI_TX_RING_BASE + 2048);
2622 				else
2623 					CSR_WRITE_4(sc, TI_WINBASE,
2624 					    TI_TX_RING_BASE);
2625 				f = &sc->ti_rdata->ti_tx_ring_nic[frag % 128];
2626 			} else
2627 				f = &sc->ti_rdata->ti_tx_ring[frag];
2628 			if (sc->ti_cdata.ti_tx_chain[frag] != NULL)
2629 				break;
2630 			TI_HOSTADDR(f->ti_addr) = vtophys(mtod(m, vm_offset_t));
2631 			f->ti_len = m->m_len;
2632 			f->ti_flags = csum_flags;
2633 
2634 			if (mtag != NULL) {
2635 				f->ti_flags |= TI_BDFLAG_VLAN_TAG;
2636 				f->ti_vlan_tag = VLAN_TAG_VALUE(mtag) & 0xfff;
2637 			} else {
2638 				f->ti_vlan_tag = 0;
2639 			}
2640 
2641 			/*
2642 			 * Sanity check: avoid coming within 16 descriptors
2643 			 * of the end of the ring.
2644 			 */
2645 			if ((TI_TX_RING_CNT - (sc->ti_txcnt + cnt)) < 16)
2646 				return (ENOBUFS);
2647 			cur = frag;
2648 			TI_INC(frag, TI_TX_RING_CNT);
2649 			cnt++;
2650 		}
2651 	}
2652 
2653 	if (m != NULL)
2654 		return (ENOBUFS);
2655 
2656 	if (frag == sc->ti_tx_saved_considx)
2657 		return (ENOBUFS);
2658 
2659 	if (sc->ti_hwrev == TI_HWREV_TIGON)
2660 		sc->ti_rdata->ti_tx_ring_nic[cur % 128].ti_flags |=
2661 	            TI_BDFLAG_END;
2662 	else
2663 		sc->ti_rdata->ti_tx_ring[cur].ti_flags |= TI_BDFLAG_END;
2664 	sc->ti_cdata.ti_tx_chain[cur] = m_head;
2665 	sc->ti_txcnt += cnt;
2666 
2667 	*txidx = frag;
2668 
2669 	return (0);
2670 }
2671 
2672 /*
2673  * Main transmit routine. To avoid having to do mbuf copies, we put pointers
2674  * to the mbuf data regions directly in the transmit descriptors.
2675  */
2676 static void
2677 ti_start(ifp)
2678 	struct ifnet		*ifp;
2679 {
2680 	struct ti_softc		*sc;
2681 	struct mbuf		*m_head = NULL;
2682 	u_int32_t		prodidx = 0;
2683 
2684 	sc = ifp->if_softc;
2685 	TI_LOCK(sc);
2686 
2687 	prodidx = CSR_READ_4(sc, TI_MB_SENDPROD_IDX);
2688 
2689 	while (sc->ti_cdata.ti_tx_chain[prodidx] == NULL) {
2690 		IF_DEQUEUE(&ifp->if_snd, m_head);
2691 		if (m_head == NULL)
2692 			break;
2693 
2694 		/*
2695 		 * XXX
2696 		 * safety overkill.  If this is a fragmented packet chain
2697 		 * with delayed TCP/UDP checksums, then only encapsulate
2698 		 * it if we have enough descriptors to handle the entire
2699 		 * chain at once.
2700 		 * (paranoia -- may not actually be needed)
2701 		 */
2702 		if (m_head->m_flags & M_FIRSTFRAG &&
2703 		    m_head->m_pkthdr.csum_flags & (CSUM_DELAY_DATA)) {
2704 			if ((TI_TX_RING_CNT - sc->ti_txcnt) <
2705 			    m_head->m_pkthdr.csum_data + 16) {
2706 				IF_PREPEND(&ifp->if_snd, m_head);
2707 				ifp->if_flags |= IFF_OACTIVE;
2708 				break;
2709 			}
2710 		}
2711 
2712 		/*
2713 		 * Pack the data into the transmit ring. If we
2714 		 * don't have room, set the OACTIVE flag and wait
2715 		 * for the NIC to drain the ring.
2716 		 */
2717 		if (ti_encap(sc, m_head, &prodidx)) {
2718 			IF_PREPEND(&ifp->if_snd, m_head);
2719 			ifp->if_flags |= IFF_OACTIVE;
2720 			break;
2721 		}
2722 
2723 		/*
2724 		 * If there's a BPF listener, bounce a copy of this frame
2725 		 * to him.
2726 		 */
2727 		BPF_MTAP(ifp, m_head);
2728 	}
2729 
2730 	/* Transmit */
2731 	CSR_WRITE_4(sc, TI_MB_SENDPROD_IDX, prodidx);
2732 
2733 	/*
2734 	 * Set a timeout in case the chip goes out to lunch.
2735 	 */
2736 	ifp->if_timer = 5;
2737 	TI_UNLOCK(sc);
2738 }
2739 
2740 static void
2741 ti_init(xsc)
2742 	void			*xsc;
2743 {
2744 	struct ti_softc		*sc = xsc;
2745 
2746 	/* Cancel pending I/O and flush buffers. */
2747 	ti_stop(sc);
2748 
2749 	TI_LOCK(sc);
2750 	/* Init the gen info block, ring control blocks and firmware. */
2751 	if (ti_gibinit(sc)) {
2752 		printf("ti%d: initialization failure\n", sc->ti_unit);
2753 		TI_UNLOCK(sc);
2754 		return;
2755 	}
2756 
2757 	TI_UNLOCK(sc);
2758 }
2759 
2760 static void ti_init2(sc)
2761 	struct ti_softc		*sc;
2762 {
2763 	struct ti_cmd_desc	cmd;
2764 	struct ifnet		*ifp;
2765 	u_int16_t		*m;
2766 	struct ifmedia		*ifm;
2767 	int			tmp;
2768 
2769 	ifp = &sc->arpcom.ac_if;
2770 
2771 	/* Specify MTU and interface index. */
2772 	CSR_WRITE_4(sc, TI_GCR_IFINDEX, sc->ti_unit);
2773 	CSR_WRITE_4(sc, TI_GCR_IFMTU, ifp->if_mtu +
2774 	    ETHER_HDR_LEN + ETHER_CRC_LEN + ETHER_VLAN_ENCAP_LEN);
2775 	TI_DO_CMD(TI_CMD_UPDATE_GENCOM, 0, 0);
2776 
2777 	/* Load our MAC address. */
2778 	m = (u_int16_t *)&sc->arpcom.ac_enaddr[0];
2779 	CSR_WRITE_4(sc, TI_GCR_PAR0, htons(m[0]));
2780 	CSR_WRITE_4(sc, TI_GCR_PAR1, (htons(m[1]) << 16) | htons(m[2]));
2781 	TI_DO_CMD(TI_CMD_SET_MAC_ADDR, 0, 0);
2782 
2783 	/* Enable or disable promiscuous mode as needed. */
2784 	if (ifp->if_flags & IFF_PROMISC) {
2785 		TI_DO_CMD(TI_CMD_SET_PROMISC_MODE, TI_CMD_CODE_PROMISC_ENB, 0);
2786 	} else {
2787 		TI_DO_CMD(TI_CMD_SET_PROMISC_MODE, TI_CMD_CODE_PROMISC_DIS, 0);
2788 	}
2789 
2790 	/* Program multicast filter. */
2791 	ti_setmulti(sc);
2792 
2793 	/*
2794 	 * If this is a Tigon 1, we should tell the
2795 	 * firmware to use software packet filtering.
2796 	 */
2797 	if (sc->ti_hwrev == TI_HWREV_TIGON) {
2798 		TI_DO_CMD(TI_CMD_FDR_FILTERING, TI_CMD_CODE_FILT_ENB, 0);
2799 	}
2800 
2801 	/* Init RX ring. */
2802 	ti_init_rx_ring_std(sc);
2803 
2804 	/* Init jumbo RX ring. */
2805 	if (ifp->if_mtu > (ETHERMTU + ETHER_HDR_LEN + ETHER_CRC_LEN))
2806 		ti_init_rx_ring_jumbo(sc);
2807 
2808 	/*
2809 	 * If this is a Tigon 2, we can also configure the
2810 	 * mini ring.
2811 	 */
2812 	if (sc->ti_hwrev == TI_HWREV_TIGON_II)
2813 		ti_init_rx_ring_mini(sc);
2814 
2815 	CSR_WRITE_4(sc, TI_GCR_RXRETURNCONS_IDX, 0);
2816 	sc->ti_rx_saved_considx = 0;
2817 
2818 	/* Init TX ring. */
2819 	ti_init_tx_ring(sc);
2820 
2821 	/* Tell firmware we're alive. */
2822 	TI_DO_CMD(TI_CMD_HOST_STATE, TI_CMD_CODE_STACK_UP, 0);
2823 
2824 	/* Enable host interrupts. */
2825 	CSR_WRITE_4(sc, TI_MB_HOSTINTR, 0);
2826 
2827 	ifp->if_flags |= IFF_RUNNING;
2828 	ifp->if_flags &= ~IFF_OACTIVE;
2829 
2830 	/*
2831 	 * Make sure to set media properly. We have to do this
2832 	 * here since we have to issue commands in order to set
2833 	 * the link negotiation and we can't issue commands until
2834 	 * the firmware is running.
2835 	 */
2836 	ifm = &sc->ifmedia;
2837 	tmp = ifm->ifm_media;
2838 	ifm->ifm_media = ifm->ifm_cur->ifm_media;
2839 	ti_ifmedia_upd(ifp);
2840 	ifm->ifm_media = tmp;
2841 }
2842 
2843 /*
2844  * Set media options.
2845  */
2846 static int
2847 ti_ifmedia_upd(ifp)
2848 	struct ifnet		*ifp;
2849 {
2850 	struct ti_softc		*sc;
2851 	struct ifmedia		*ifm;
2852 	struct ti_cmd_desc	cmd;
2853 	u_int32_t		flowctl;
2854 
2855 	sc = ifp->if_softc;
2856 	ifm = &sc->ifmedia;
2857 
2858 	if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
2859 		return (EINVAL);
2860 
2861 	flowctl = 0;
2862 
2863 	switch (IFM_SUBTYPE(ifm->ifm_media)) {
2864 	case IFM_AUTO:
2865 		/*
2866 		 * Transmit flow control doesn't work on the Tigon 1.
2867 		 */
2868 		flowctl = TI_GLNK_RX_FLOWCTL_Y;
2869 
2870 		/*
2871 		 * Transmit flow control can also cause problems on the
2872 		 * Tigon 2, apparantly with both the copper and fiber
2873 		 * boards.  The symptom is that the interface will just
2874 		 * hang.  This was reproduced with Alteon 180 switches.
2875 		 */
2876 #if 0
2877 		if (sc->ti_hwrev != TI_HWREV_TIGON)
2878 			flowctl |= TI_GLNK_TX_FLOWCTL_Y;
2879 #endif
2880 
2881 		CSR_WRITE_4(sc, TI_GCR_GLINK, TI_GLNK_PREF|TI_GLNK_1000MB|
2882 		    TI_GLNK_FULL_DUPLEX| flowctl |
2883 		    TI_GLNK_AUTONEGENB|TI_GLNK_ENB);
2884 
2885 		flowctl = TI_LNK_RX_FLOWCTL_Y;
2886 #if 0
2887 		if (sc->ti_hwrev != TI_HWREV_TIGON)
2888 			flowctl |= TI_LNK_TX_FLOWCTL_Y;
2889 #endif
2890 
2891 		CSR_WRITE_4(sc, TI_GCR_LINK, TI_LNK_100MB|TI_LNK_10MB|
2892 		    TI_LNK_FULL_DUPLEX|TI_LNK_HALF_DUPLEX| flowctl |
2893 		    TI_LNK_AUTONEGENB|TI_LNK_ENB);
2894 		TI_DO_CMD(TI_CMD_LINK_NEGOTIATION,
2895 		    TI_CMD_CODE_NEGOTIATE_BOTH, 0);
2896 		break;
2897 	case IFM_1000_SX:
2898 	case IFM_1000_T:
2899 		flowctl = TI_GLNK_RX_FLOWCTL_Y;
2900 #if 0
2901 		if (sc->ti_hwrev != TI_HWREV_TIGON)
2902 			flowctl |= TI_GLNK_TX_FLOWCTL_Y;
2903 #endif
2904 
2905 		CSR_WRITE_4(sc, TI_GCR_GLINK, TI_GLNK_PREF|TI_GLNK_1000MB|
2906 		    flowctl |TI_GLNK_ENB);
2907 		CSR_WRITE_4(sc, TI_GCR_LINK, 0);
2908 		if ((ifm->ifm_media & IFM_GMASK) == IFM_FDX) {
2909 			TI_SETBIT(sc, TI_GCR_GLINK, TI_GLNK_FULL_DUPLEX);
2910 		}
2911 		TI_DO_CMD(TI_CMD_LINK_NEGOTIATION,
2912 		    TI_CMD_CODE_NEGOTIATE_GIGABIT, 0);
2913 		break;
2914 	case IFM_100_FX:
2915 	case IFM_10_FL:
2916 	case IFM_100_TX:
2917 	case IFM_10_T:
2918 		flowctl = TI_LNK_RX_FLOWCTL_Y;
2919 #if 0
2920 		if (sc->ti_hwrev != TI_HWREV_TIGON)
2921 			flowctl |= TI_LNK_TX_FLOWCTL_Y;
2922 #endif
2923 
2924 		CSR_WRITE_4(sc, TI_GCR_GLINK, 0);
2925 		CSR_WRITE_4(sc, TI_GCR_LINK, TI_LNK_ENB|TI_LNK_PREF|flowctl);
2926 		if (IFM_SUBTYPE(ifm->ifm_media) == IFM_100_FX ||
2927 		    IFM_SUBTYPE(ifm->ifm_media) == IFM_100_TX) {
2928 			TI_SETBIT(sc, TI_GCR_LINK, TI_LNK_100MB);
2929 		} else {
2930 			TI_SETBIT(sc, TI_GCR_LINK, TI_LNK_10MB);
2931 		}
2932 		if ((ifm->ifm_media & IFM_GMASK) == IFM_FDX) {
2933 			TI_SETBIT(sc, TI_GCR_LINK, TI_LNK_FULL_DUPLEX);
2934 		} else {
2935 			TI_SETBIT(sc, TI_GCR_LINK, TI_LNK_HALF_DUPLEX);
2936 		}
2937 		TI_DO_CMD(TI_CMD_LINK_NEGOTIATION,
2938 		    TI_CMD_CODE_NEGOTIATE_10_100, 0);
2939 		break;
2940 	}
2941 
2942 	return (0);
2943 }
2944 
2945 /*
2946  * Report current media status.
2947  */
2948 static void
2949 ti_ifmedia_sts(ifp, ifmr)
2950 	struct ifnet		*ifp;
2951 	struct ifmediareq	*ifmr;
2952 {
2953 	struct ti_softc		*sc;
2954 	u_int32_t		media = 0;
2955 
2956 	sc = ifp->if_softc;
2957 
2958 	ifmr->ifm_status = IFM_AVALID;
2959 	ifmr->ifm_active = IFM_ETHER;
2960 
2961 	if (sc->ti_linkstat == TI_EV_CODE_LINK_DOWN)
2962 		return;
2963 
2964 	ifmr->ifm_status |= IFM_ACTIVE;
2965 
2966 	if (sc->ti_linkstat == TI_EV_CODE_GIG_LINK_UP) {
2967 		media = CSR_READ_4(sc, TI_GCR_GLINK_STAT);
2968 		if (sc->ti_copper)
2969 			ifmr->ifm_active |= IFM_1000_T;
2970 		else
2971 			ifmr->ifm_active |= IFM_1000_SX;
2972 		if (media & TI_GLNK_FULL_DUPLEX)
2973 			ifmr->ifm_active |= IFM_FDX;
2974 		else
2975 			ifmr->ifm_active |= IFM_HDX;
2976 	} else if (sc->ti_linkstat == TI_EV_CODE_LINK_UP) {
2977 		media = CSR_READ_4(sc, TI_GCR_LINK_STAT);
2978 		if (sc->ti_copper) {
2979 			if (media & TI_LNK_100MB)
2980 				ifmr->ifm_active |= IFM_100_TX;
2981 			if (media & TI_LNK_10MB)
2982 				ifmr->ifm_active |= IFM_10_T;
2983 		} else {
2984 			if (media & TI_LNK_100MB)
2985 				ifmr->ifm_active |= IFM_100_FX;
2986 			if (media & TI_LNK_10MB)
2987 				ifmr->ifm_active |= IFM_10_FL;
2988 		}
2989 		if (media & TI_LNK_FULL_DUPLEX)
2990 			ifmr->ifm_active |= IFM_FDX;
2991 		if (media & TI_LNK_HALF_DUPLEX)
2992 			ifmr->ifm_active |= IFM_HDX;
2993 	}
2994 }
2995 
2996 static int
2997 ti_ioctl(ifp, command, data)
2998 	struct ifnet		*ifp;
2999 	u_long			command;
3000 	caddr_t			data;
3001 {
3002 	struct ti_softc		*sc = ifp->if_softc;
3003 	struct ifreq		*ifr = (struct ifreq *) data;
3004 	int			mask, error = 0;
3005 	struct ti_cmd_desc	cmd;
3006 
3007 	TI_LOCK(sc);
3008 
3009 	switch (command) {
3010 	case SIOCSIFMTU:
3011 		if (ifr->ifr_mtu > TI_JUMBO_MTU)
3012 			error = EINVAL;
3013 		else {
3014 			ifp->if_mtu = ifr->ifr_mtu;
3015 			ti_init(sc);
3016 		}
3017 		break;
3018 	case SIOCSIFFLAGS:
3019 		if (ifp->if_flags & IFF_UP) {
3020 			/*
3021 			 * If only the state of the PROMISC flag changed,
3022 			 * then just use the 'set promisc mode' command
3023 			 * instead of reinitializing the entire NIC. Doing
3024 			 * a full re-init means reloading the firmware and
3025 			 * waiting for it to start up, which may take a
3026 			 * second or two.
3027 			 */
3028 			if (ifp->if_flags & IFF_RUNNING &&
3029 			    ifp->if_flags & IFF_PROMISC &&
3030 			    !(sc->ti_if_flags & IFF_PROMISC)) {
3031 				TI_DO_CMD(TI_CMD_SET_PROMISC_MODE,
3032 				    TI_CMD_CODE_PROMISC_ENB, 0);
3033 			} else if (ifp->if_flags & IFF_RUNNING &&
3034 			    !(ifp->if_flags & IFF_PROMISC) &&
3035 			    sc->ti_if_flags & IFF_PROMISC) {
3036 				TI_DO_CMD(TI_CMD_SET_PROMISC_MODE,
3037 				    TI_CMD_CODE_PROMISC_DIS, 0);
3038 			} else
3039 				ti_init(sc);
3040 		} else {
3041 			if (ifp->if_flags & IFF_RUNNING) {
3042 				ti_stop(sc);
3043 			}
3044 		}
3045 		sc->ti_if_flags = ifp->if_flags;
3046 		error = 0;
3047 		break;
3048 	case SIOCADDMULTI:
3049 	case SIOCDELMULTI:
3050 		if (ifp->if_flags & IFF_RUNNING) {
3051 			ti_setmulti(sc);
3052 			error = 0;
3053 		}
3054 		break;
3055 	case SIOCSIFMEDIA:
3056 	case SIOCGIFMEDIA:
3057 		error = ifmedia_ioctl(ifp, ifr, &sc->ifmedia, command);
3058 		break;
3059 	case SIOCSIFCAP:
3060 		mask = ifr->ifr_reqcap ^ ifp->if_capenable;
3061 		if (mask & IFCAP_HWCSUM) {
3062 			if (IFCAP_HWCSUM & ifp->if_capenable)
3063 				ifp->if_capenable &= ~IFCAP_HWCSUM;
3064 			else
3065 				ifp->if_capenable |= IFCAP_HWCSUM;
3066 			if (ifp->if_flags & IFF_RUNNING)
3067 				ti_init(sc);
3068 		}
3069 		error = 0;
3070 		break;
3071 	default:
3072 		error = ether_ioctl(ifp, command, data);
3073 		break;
3074 	}
3075 
3076 	TI_UNLOCK(sc);
3077 
3078 	return (error);
3079 }
3080 
3081 static int
3082 ti_open(struct cdev *dev, int flags, int fmt, struct thread *td)
3083 {
3084 	struct ti_softc *sc;
3085 
3086 	sc = dev->si_drv1;
3087 	if (sc == NULL)
3088 		return (ENODEV);
3089 
3090 	TI_LOCK(sc);
3091 	sc->ti_flags |= TI_FLAG_DEBUGING;
3092 	TI_UNLOCK(sc);
3093 
3094 	return (0);
3095 }
3096 
3097 static int
3098 ti_close(struct cdev *dev, int flag, int fmt, struct thread *td)
3099 {
3100 	struct ti_softc *sc;
3101 
3102 	sc = dev->si_drv1;
3103 	if (sc == NULL)
3104 		return (ENODEV);
3105 
3106 	TI_LOCK(sc);
3107 	sc->ti_flags &= ~TI_FLAG_DEBUGING;
3108 	TI_UNLOCK(sc);
3109 
3110 	return (0);
3111 }
3112 
3113 /*
3114  * This ioctl routine goes along with the Tigon character device.
3115  */
3116 static int
3117 ti_ioctl2(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
3118 {
3119 	int error;
3120 	struct ti_softc *sc;
3121 
3122 	sc = dev->si_drv1;
3123 	if (sc == NULL)
3124 		return (ENODEV);
3125 
3126 	error = 0;
3127 
3128 	switch (cmd) {
3129 	case TIIOCGETSTATS:
3130 	{
3131 		struct ti_stats *outstats;
3132 
3133 		outstats = (struct ti_stats *)addr;
3134 
3135 		bcopy(&sc->ti_rdata->ti_info.ti_stats, outstats,
3136 		      sizeof(struct ti_stats));
3137 		break;
3138 	}
3139 	case TIIOCGETPARAMS:
3140 	{
3141 		struct ti_params	*params;
3142 
3143 		params = (struct ti_params *)addr;
3144 
3145 		params->ti_stat_ticks = sc->ti_stat_ticks;
3146 		params->ti_rx_coal_ticks = sc->ti_rx_coal_ticks;
3147 		params->ti_tx_coal_ticks = sc->ti_tx_coal_ticks;
3148 		params->ti_rx_max_coal_bds = sc->ti_rx_max_coal_bds;
3149 		params->ti_tx_max_coal_bds = sc->ti_tx_max_coal_bds;
3150 		params->ti_tx_buf_ratio = sc->ti_tx_buf_ratio;
3151 		params->param_mask = TI_PARAM_ALL;
3152 
3153 		error = 0;
3154 
3155 		break;
3156 	}
3157 	case TIIOCSETPARAMS:
3158 	{
3159 		struct ti_params *params;
3160 
3161 		params = (struct ti_params *)addr;
3162 
3163 		if (params->param_mask & TI_PARAM_STAT_TICKS) {
3164 			sc->ti_stat_ticks = params->ti_stat_ticks;
3165 			CSR_WRITE_4(sc, TI_GCR_STAT_TICKS, sc->ti_stat_ticks);
3166 		}
3167 
3168 		if (params->param_mask & TI_PARAM_RX_COAL_TICKS) {
3169 			sc->ti_rx_coal_ticks = params->ti_rx_coal_ticks;
3170 			CSR_WRITE_4(sc, TI_GCR_RX_COAL_TICKS,
3171 				    sc->ti_rx_coal_ticks);
3172 		}
3173 
3174 		if (params->param_mask & TI_PARAM_TX_COAL_TICKS) {
3175 			sc->ti_tx_coal_ticks = params->ti_tx_coal_ticks;
3176 			CSR_WRITE_4(sc, TI_GCR_TX_COAL_TICKS,
3177 				    sc->ti_tx_coal_ticks);
3178 		}
3179 
3180 		if (params->param_mask & TI_PARAM_RX_COAL_BDS) {
3181 			sc->ti_rx_max_coal_bds = params->ti_rx_max_coal_bds;
3182 			CSR_WRITE_4(sc, TI_GCR_RX_MAX_COAL_BD,
3183 				    sc->ti_rx_max_coal_bds);
3184 		}
3185 
3186 		if (params->param_mask & TI_PARAM_TX_COAL_BDS) {
3187 			sc->ti_tx_max_coal_bds = params->ti_tx_max_coal_bds;
3188 			CSR_WRITE_4(sc, TI_GCR_TX_MAX_COAL_BD,
3189 				    sc->ti_tx_max_coal_bds);
3190 		}
3191 
3192 		if (params->param_mask & TI_PARAM_TX_BUF_RATIO) {
3193 			sc->ti_tx_buf_ratio = params->ti_tx_buf_ratio;
3194 			CSR_WRITE_4(sc, TI_GCR_TX_BUFFER_RATIO,
3195 				    sc->ti_tx_buf_ratio);
3196 		}
3197 
3198 		error = 0;
3199 
3200 		break;
3201 	}
3202 	case TIIOCSETTRACE: {
3203 		ti_trace_type	trace_type;
3204 
3205 		trace_type = *(ti_trace_type *)addr;
3206 
3207 		/*
3208 		 * Set tracing to whatever the user asked for.  Setting
3209 		 * this register to 0 should have the effect of disabling
3210 		 * tracing.
3211 		 */
3212 		CSR_WRITE_4(sc, TI_GCR_NIC_TRACING, trace_type);
3213 
3214 		error = 0;
3215 
3216 		break;
3217 	}
3218 	case TIIOCGETTRACE: {
3219 		struct ti_trace_buf	*trace_buf;
3220 		u_int32_t		trace_start, cur_trace_ptr, trace_len;
3221 
3222 		trace_buf = (struct ti_trace_buf *)addr;
3223 
3224 		trace_start = CSR_READ_4(sc, TI_GCR_NICTRACE_START);
3225 		cur_trace_ptr = CSR_READ_4(sc, TI_GCR_NICTRACE_PTR);
3226 		trace_len = CSR_READ_4(sc, TI_GCR_NICTRACE_LEN);
3227 
3228 #if 0
3229 		printf("ti%d: trace_start = %#x, cur_trace_ptr = %#x, "
3230 		       "trace_len = %d\n", sc->ti_unit, trace_start,
3231 		       cur_trace_ptr, trace_len);
3232 		printf("ti%d: trace_buf->buf_len = %d\n", sc->ti_unit,
3233 		       trace_buf->buf_len);
3234 #endif
3235 
3236 		error = ti_copy_mem(sc, trace_start, min(trace_len,
3237 				    trace_buf->buf_len),
3238 				    (caddr_t)trace_buf->buf, 1, 1);
3239 
3240 		if (error == 0) {
3241 			trace_buf->fill_len = min(trace_len,
3242 						  trace_buf->buf_len);
3243 			if (cur_trace_ptr < trace_start)
3244 				trace_buf->cur_trace_ptr =
3245 					trace_start - cur_trace_ptr;
3246 			else
3247 				trace_buf->cur_trace_ptr =
3248 					cur_trace_ptr - trace_start;
3249 		} else
3250 			trace_buf->fill_len = 0;
3251 
3252 		break;
3253 	}
3254 
3255 	/*
3256 	 * For debugging, five ioctls are needed:
3257 	 * ALT_ATTACH
3258 	 * ALT_READ_TG_REG
3259 	 * ALT_WRITE_TG_REG
3260 	 * ALT_READ_TG_MEM
3261 	 * ALT_WRITE_TG_MEM
3262 	 */
3263 	case ALT_ATTACH:
3264 		/*
3265 		 * From what I can tell, Alteon's Solaris Tigon driver
3266 		 * only has one character device, so you have to attach
3267 		 * to the Tigon board you're interested in.  This seems
3268 		 * like a not-so-good way to do things, since unless you
3269 		 * subsequently specify the unit number of the device
3270 		 * you're interested in in every ioctl, you'll only be
3271 		 * able to debug one board at a time.
3272 		 */
3273 		error = 0;
3274 		break;
3275 	case ALT_READ_TG_MEM:
3276 	case ALT_WRITE_TG_MEM:
3277 	{
3278 		struct tg_mem *mem_param;
3279 		u_int32_t sram_end, scratch_end;
3280 
3281 		mem_param = (struct tg_mem *)addr;
3282 
3283 		if (sc->ti_hwrev == TI_HWREV_TIGON) {
3284 			sram_end = TI_END_SRAM_I;
3285 			scratch_end = TI_END_SCRATCH_I;
3286 		} else {
3287 			sram_end = TI_END_SRAM_II;
3288 			scratch_end = TI_END_SCRATCH_II;
3289 		}
3290 
3291 		/*
3292 		 * For now, we'll only handle accessing regular SRAM,
3293 		 * nothing else.
3294 		 */
3295 		if ((mem_param->tgAddr >= TI_BEG_SRAM)
3296 		 && ((mem_param->tgAddr + mem_param->len) <= sram_end)) {
3297 			/*
3298 			 * In this instance, we always copy to/from user
3299 			 * space, so the user space argument is set to 1.
3300 			 */
3301 			error = ti_copy_mem(sc, mem_param->tgAddr,
3302 					    mem_param->len,
3303 					    mem_param->userAddr, 1,
3304 					    (cmd == ALT_READ_TG_MEM) ? 1 : 0);
3305 		} else if ((mem_param->tgAddr >= TI_BEG_SCRATCH)
3306 			&& (mem_param->tgAddr <= scratch_end)) {
3307 			error = ti_copy_scratch(sc, mem_param->tgAddr,
3308 						mem_param->len,
3309 						mem_param->userAddr, 1,
3310 						(cmd == ALT_READ_TG_MEM) ?
3311 						1 : 0, TI_PROCESSOR_A);
3312 		} else if ((mem_param->tgAddr >= TI_BEG_SCRATCH_B_DEBUG)
3313 			&& (mem_param->tgAddr <= TI_BEG_SCRATCH_B_DEBUG)) {
3314 			if (sc->ti_hwrev == TI_HWREV_TIGON) {
3315 				printf("ti%d:  invalid memory range for "
3316 				       "Tigon I\n", sc->ti_unit);
3317 				error = EINVAL;
3318 				break;
3319 			}
3320 			error = ti_copy_scratch(sc, mem_param->tgAddr -
3321 						TI_SCRATCH_DEBUG_OFF,
3322 						mem_param->len,
3323 						mem_param->userAddr, 1,
3324 						(cmd == ALT_READ_TG_MEM) ?
3325 						1 : 0, TI_PROCESSOR_B);
3326 		} else {
3327 			printf("ti%d: memory address %#x len %d is out of "
3328 			       "supported range\n", sc->ti_unit,
3329 			        mem_param->tgAddr, mem_param->len);
3330 			error = EINVAL;
3331 		}
3332 
3333 		break;
3334 	}
3335 	case ALT_READ_TG_REG:
3336 	case ALT_WRITE_TG_REG:
3337 	{
3338 		struct tg_reg	*regs;
3339 		u_int32_t	tmpval;
3340 
3341 		regs = (struct tg_reg *)addr;
3342 
3343 		/*
3344 		 * Make sure the address in question isn't out of range.
3345 		 */
3346 		if (regs->addr > TI_REG_MAX) {
3347 			error = EINVAL;
3348 			break;
3349 		}
3350 		if (cmd == ALT_READ_TG_REG) {
3351 			bus_space_read_region_4(sc->ti_btag, sc->ti_bhandle,
3352 						regs->addr, &tmpval, 1);
3353 			regs->data = ntohl(tmpval);
3354 #if 0
3355 			if ((regs->addr == TI_CPU_STATE)
3356 			 || (regs->addr == TI_CPU_CTL_B)) {
3357 				printf("ti%d: register %#x = %#x\n",
3358 				       sc->ti_unit, regs->addr, tmpval);
3359 			}
3360 #endif
3361 		} else {
3362 			tmpval = htonl(regs->data);
3363 			bus_space_write_region_4(sc->ti_btag, sc->ti_bhandle,
3364 						 regs->addr, &tmpval, 1);
3365 		}
3366 
3367 		break;
3368 	}
3369 	default:
3370 		error = ENOTTY;
3371 		break;
3372 	}
3373 	return (error);
3374 }
3375 
3376 static void
3377 ti_watchdog(ifp)
3378 	struct ifnet		*ifp;
3379 {
3380 	struct ti_softc		*sc;
3381 
3382 	sc = ifp->if_softc;
3383 	TI_LOCK(sc);
3384 
3385 	/*
3386 	 * When we're debugging, the chip is often stopped for long periods
3387 	 * of time, and that would normally cause the watchdog timer to fire.
3388 	 * Since that impedes debugging, we don't want to do that.
3389 	 */
3390 	if (sc->ti_flags & TI_FLAG_DEBUGING) {
3391 		TI_UNLOCK(sc);
3392 		return;
3393 	}
3394 
3395 	printf("ti%d: watchdog timeout -- resetting\n", sc->ti_unit);
3396 	ti_stop(sc);
3397 	ti_init(sc);
3398 
3399 	ifp->if_oerrors++;
3400 	TI_UNLOCK(sc);
3401 }
3402 
3403 /*
3404  * Stop the adapter and free any mbufs allocated to the
3405  * RX and TX lists.
3406  */
3407 static void
3408 ti_stop(sc)
3409 	struct ti_softc		*sc;
3410 {
3411 	struct ifnet		*ifp;
3412 	struct ti_cmd_desc	cmd;
3413 
3414 	TI_LOCK(sc);
3415 
3416 	ifp = &sc->arpcom.ac_if;
3417 
3418 	/* Disable host interrupts. */
3419 	CSR_WRITE_4(sc, TI_MB_HOSTINTR, 1);
3420 	/*
3421 	 * Tell firmware we're shutting down.
3422 	 */
3423 	TI_DO_CMD(TI_CMD_HOST_STATE, TI_CMD_CODE_STACK_DOWN, 0);
3424 
3425 	/* Halt and reinitialize. */
3426 	ti_chipinit(sc);
3427 	ti_mem(sc, 0x2000, 0x100000 - 0x2000, NULL);
3428 	ti_chipinit(sc);
3429 
3430 	/* Free the RX lists. */
3431 	ti_free_rx_ring_std(sc);
3432 
3433 	/* Free jumbo RX list. */
3434 	ti_free_rx_ring_jumbo(sc);
3435 
3436 	/* Free mini RX list. */
3437 	ti_free_rx_ring_mini(sc);
3438 
3439 	/* Free TX buffers. */
3440 	ti_free_tx_ring(sc);
3441 
3442 	sc->ti_ev_prodidx.ti_idx = 0;
3443 	sc->ti_return_prodidx.ti_idx = 0;
3444 	sc->ti_tx_considx.ti_idx = 0;
3445 	sc->ti_tx_saved_considx = TI_TXCONS_UNSET;
3446 
3447 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
3448 	TI_UNLOCK(sc);
3449 }
3450 
3451 /*
3452  * Stop all chip I/O so that the kernel's probe routines don't
3453  * get confused by errant DMAs when rebooting.
3454  */
3455 static void
3456 ti_shutdown(dev)
3457 	device_t		dev;
3458 {
3459 	struct ti_softc		*sc;
3460 
3461 	sc = device_get_softc(dev);
3462 	TI_LOCK(sc);
3463 	ti_chipinit(sc);
3464 	TI_UNLOCK(sc);
3465 }
3466