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