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