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