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