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