Deleted Added
full compact
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 * $FreeBSD: head/sys/dev/ti/if_ti.c 64139 2000-08-02 18:49:17Z wpaul $
32 * $FreeBSD: head/sys/dev/ti/if_ti.c 64837 2000-08-19 08:32:59Z dwmalone $
33 */
34
35/*
36 * Alteon Networks Tigon PCI gigabit ethernet driver for FreeBSD.
37 * Manuals, sample driver and firmware source kits are available
38 * from http://www.alteon.com/support/openkits.
39 *
40 * Written by Bill Paul <wpaul@ctr.columbia.edu>
41 * Electrical Engineering Department
42 * Columbia University, New York City
43 */
44
45/*
46 * The Alteon Networks Tigon chip contains an embedded R4000 CPU,
47 * gigabit MAC, dual DMA channels and a PCI interface unit. NICs
48 * using the Tigon may have anywhere from 512K to 2MB of SRAM. The
49 * Tigon supports hardware IP, TCP and UCP checksumming, multicast
50 * filtering and jumbo (9014 byte) frames. The hardware is largely
51 * controlled by firmware, which must be loaded into the NIC during
52 * initialization.
53 *
54 * The Tigon 2 contains 2 R4000 CPUs and requires a newer firmware
55 * revision, which supports new features such as extended commands,
56 * extended jumbo receive ring desciptors and a mini receive ring.
57 *
58 * Alteon Networks is to be commended for releasing such a vast amount
59 * of development material for the Tigon NIC without requiring an NDA
60 * (although they really should have done it a long time ago). With
61 * any luck, the other vendors will finally wise up and follow Alteon's
62 * stellar example.
63 *
64 * The firmware for the Tigon 1 and 2 NICs is compiled directly into
65 * this driver by #including it as a C header file. This bloats the
66 * driver somewhat, but it's the easiest method considering that the
67 * driver code and firmware code need to be kept in sync. The source
68 * for the firmware is not provided with the FreeBSD distribution since
69 * compiling it requires a GNU toolchain targeted for mips-sgi-irix5.3.
70 *
71 * The following people deserve special thanks:
72 * - Terry Murphy of 3Com, for providing a 3c985 Tigon 1 board
73 * for testing
74 * - Raymond Lee of Netgear, for providing a pair of Netgear
75 * GA620 Tigon 2 boards for testing
76 * - Ulf Zimmermann, for bringing the GA260 to my attention and
77 * convincing me to write this driver.
78 * - Andrew Gallatin for providing FreeBSD/Alpha support.
79 */
80
81#include "vlan.h"
82
83#include <sys/param.h>
84#include <sys/systm.h>
85#include <sys/sockio.h>
86#include <sys/mbuf.h>
87#include <sys/malloc.h>
88#include <sys/kernel.h>
89#include <sys/socket.h>
90#include <sys/queue.h>
91
92#include <net/if.h>
93#include <net/if_arp.h>
94#include <net/ethernet.h>
95#include <net/if_dl.h>
96#include <net/if_media.h>
97
98#include <net/bpf.h>
99
100#if NVLAN > 0
101#include <net/if_types.h>
102#include <net/if_vlan_var.h>
103#endif
104
105#include <netinet/in_systm.h>
106#include <netinet/in.h>
107#include <netinet/ip.h>
108
109#include <vm/vm.h> /* for vtophys */
110#include <vm/pmap.h> /* for vtophys */
111#include <machine/clock.h> /* for DELAY */
112#include <machine/bus_memio.h>
113#include <machine/bus.h>
114#include <machine/resource.h>
115#include <sys/bus.h>
116#include <sys/rman.h>
117
118#include <pci/pcireg.h>
119#include <pci/pcivar.h>
120
121#include <pci/if_tireg.h>
122#include <pci/ti_fw.h>
123#include <pci/ti_fw2.h>
124
125#define TI_CSUM_FEATURES (CSUM_IP | CSUM_TCP | CSUM_UDP | CSUM_IP_FRAGS)
126
127#if !defined(lint)
128static const char rcsid[] =
129 "$FreeBSD: head/sys/dev/ti/if_ti.c 64139 2000-08-02 18:49:17Z wpaul $";
129 "$FreeBSD: head/sys/dev/ti/if_ti.c 64837 2000-08-19 08:32:59Z dwmalone $";
130#endif
131
132/*
133 * Various supported device vendors/types and their names.
134 */
135
136static struct ti_type ti_devs[] = {
137 { ALT_VENDORID, ALT_DEVICEID_ACENIC,
138 "Alteon AceNIC 1000baseSX Gigabit Ethernet" },
139 { ALT_VENDORID, ALT_DEVICEID_ACENIC_COPPER,
140 "Alteon AceNIC 1000baseT Gigabit Ethernet" },
141 { TC_VENDORID, TC_DEVICEID_3C985,
142 "3Com 3c985-SX Gigabit Ethernet" },
143 { NG_VENDORID, NG_DEVICEID_GA620,
144 "Netgear GA620 1000baseSX Gigabit Ethernet" },
145 { NG_VENDORID, NG_DEVICEID_GA620T,
146 "Netgear GA620 1000baseT Gigabit Ethernet" },
147 { SGI_VENDORID, SGI_DEVICEID_TIGON,
148 "Silicon Graphics Gigabit Ethernet" },
149 { DEC_VENDORID, DEC_DEVICEID_FARALLON_PN9000SX,
150 "Farallon PN9000SX Gigabit Ethernet" },
151 { 0, 0, NULL }
152};
153
154static int ti_probe __P((device_t));
155static int ti_attach __P((device_t));
156static int ti_detach __P((device_t));
157static void ti_txeof __P((struct ti_softc *));
158static void ti_rxeof __P((struct ti_softc *));
159
160static void ti_stats_update __P((struct ti_softc *));
161static int ti_encap __P((struct ti_softc *, struct mbuf *,
162 u_int32_t *));
163
164static void ti_intr __P((void *));
165static void ti_start __P((struct ifnet *));
166static int ti_ioctl __P((struct ifnet *, u_long, caddr_t));
167static void ti_init __P((void *));
168static void ti_init2 __P((struct ti_softc *));
169static void ti_stop __P((struct ti_softc *));
170static void ti_watchdog __P((struct ifnet *));
171static void ti_shutdown __P((device_t));
172static int ti_ifmedia_upd __P((struct ifnet *));
173static void ti_ifmedia_sts __P((struct ifnet *, struct ifmediareq *));
174
175static u_int32_t ti_eeprom_putbyte __P((struct ti_softc *, int));
176static u_int8_t ti_eeprom_getbyte __P((struct ti_softc *,
177 int, u_int8_t *));
178static int ti_read_eeprom __P((struct ti_softc *, caddr_t, int, int));
179
180static void ti_add_mcast __P((struct ti_softc *, struct ether_addr *));
181static void ti_del_mcast __P((struct ti_softc *, struct ether_addr *));
182static void ti_setmulti __P((struct ti_softc *));
183
184static void ti_mem __P((struct ti_softc *, u_int32_t,
185 u_int32_t, caddr_t));
186static void ti_loadfw __P((struct ti_softc *));
187static void ti_cmd __P((struct ti_softc *, struct ti_cmd_desc *));
188static void ti_cmd_ext __P((struct ti_softc *, struct ti_cmd_desc *,
189 caddr_t, int));
190static void ti_handle_events __P((struct ti_softc *));
191static int ti_alloc_jumbo_mem __P((struct ti_softc *));
192static void *ti_jalloc __P((struct ti_softc *));
193static void ti_jfree __P((caddr_t, u_int));
194static void ti_jref __P((caddr_t, u_int));
193static void ti_jfree __P((caddr_t, void *));
194static int ti_newbuf_std __P((struct ti_softc *, int, struct mbuf *));
195static int ti_newbuf_mini __P((struct ti_softc *, int, struct mbuf *));
196static int ti_newbuf_jumbo __P((struct ti_softc *, int, struct mbuf *));
197static int ti_init_rx_ring_std __P((struct ti_softc *));
198static void ti_free_rx_ring_std __P((struct ti_softc *));
199static int ti_init_rx_ring_jumbo __P((struct ti_softc *));
200static void ti_free_rx_ring_jumbo __P((struct ti_softc *));
201static int ti_init_rx_ring_mini __P((struct ti_softc *));
202static void ti_free_rx_ring_mini __P((struct ti_softc *));
203static void ti_free_tx_ring __P((struct ti_softc *));
204static int ti_init_tx_ring __P((struct ti_softc *));
205
206static int ti_64bitslot_war __P((struct ti_softc *));
207static int ti_chipinit __P((struct ti_softc *));
208static int ti_gibinit __P((struct ti_softc *));
209
210static device_method_t ti_methods[] = {
211 /* Device interface */
212 DEVMETHOD(device_probe, ti_probe),
213 DEVMETHOD(device_attach, ti_attach),
214 DEVMETHOD(device_detach, ti_detach),
215 DEVMETHOD(device_shutdown, ti_shutdown),
216 { 0, 0 }
217};
218
219static driver_t ti_driver = {
220 "ti",
221 ti_methods,
222 sizeof(struct ti_softc)
223};
224
225static devclass_t ti_devclass;
226
227DRIVER_MODULE(if_ti, pci, ti_driver, ti_devclass, 0, 0);
228
229/*
230 * Send an instruction or address to the EEPROM, check for ACK.
231 */
232static u_int32_t ti_eeprom_putbyte(sc, byte)
233 struct ti_softc *sc;
234 int byte;
235{
236 register int i, ack = 0;
237
238 /*
239 * Make sure we're in TX mode.
240 */
241 TI_SETBIT(sc, TI_MISC_LOCAL_CTL, TI_MLC_EE_TXEN);
242
243 /*
244 * Feed in each bit and stobe the clock.
245 */
246 for (i = 0x80; i; i >>= 1) {
247 if (byte & i) {
248 TI_SETBIT(sc, TI_MISC_LOCAL_CTL, TI_MLC_EE_DOUT);
249 } else {
250 TI_CLRBIT(sc, TI_MISC_LOCAL_CTL, TI_MLC_EE_DOUT);
251 }
252 DELAY(1);
253 TI_SETBIT(sc, TI_MISC_LOCAL_CTL, TI_MLC_EE_CLK);
254 DELAY(1);
255 TI_CLRBIT(sc, TI_MISC_LOCAL_CTL, TI_MLC_EE_CLK);
256 }
257
258 /*
259 * Turn off TX mode.
260 */
261 TI_CLRBIT(sc, TI_MISC_LOCAL_CTL, TI_MLC_EE_TXEN);
262
263 /*
264 * Check for ack.
265 */
266 TI_SETBIT(sc, TI_MISC_LOCAL_CTL, TI_MLC_EE_CLK);
267 ack = CSR_READ_4(sc, TI_MISC_LOCAL_CTL) & TI_MLC_EE_DIN;
268 TI_CLRBIT(sc, TI_MISC_LOCAL_CTL, TI_MLC_EE_CLK);
269
270 return(ack);
271}
272
273/*
274 * Read a byte of data stored in the EEPROM at address 'addr.'
275 * We have to send two address bytes since the EEPROM can hold
276 * more than 256 bytes of data.
277 */
278static u_int8_t ti_eeprom_getbyte(sc, addr, dest)
279 struct ti_softc *sc;
280 int addr;
281 u_int8_t *dest;
282{
283 register int i;
284 u_int8_t byte = 0;
285
286 EEPROM_START;
287
288 /*
289 * Send write control code to EEPROM.
290 */
291 if (ti_eeprom_putbyte(sc, EEPROM_CTL_WRITE)) {
292 printf("ti%d: failed to send write command, status: %x\n",
293 sc->ti_unit, CSR_READ_4(sc, TI_MISC_LOCAL_CTL));
294 return(1);
295 }
296
297 /*
298 * Send first byte of address of byte we want to read.
299 */
300 if (ti_eeprom_putbyte(sc, (addr >> 8) & 0xFF)) {
301 printf("ti%d: failed to send address, status: %x\n",
302 sc->ti_unit, CSR_READ_4(sc, TI_MISC_LOCAL_CTL));
303 return(1);
304 }
305 /*
306 * Send second byte address of byte we want to read.
307 */
308 if (ti_eeprom_putbyte(sc, addr & 0xFF)) {
309 printf("ti%d: failed to send address, status: %x\n",
310 sc->ti_unit, CSR_READ_4(sc, TI_MISC_LOCAL_CTL));
311 return(1);
312 }
313
314 EEPROM_STOP;
315 EEPROM_START;
316 /*
317 * Send read control code to EEPROM.
318 */
319 if (ti_eeprom_putbyte(sc, EEPROM_CTL_READ)) {
320 printf("ti%d: failed to send read command, status: %x\n",
321 sc->ti_unit, CSR_READ_4(sc, TI_MISC_LOCAL_CTL));
322 return(1);
323 }
324
325 /*
326 * Start reading bits from EEPROM.
327 */
328 TI_CLRBIT(sc, TI_MISC_LOCAL_CTL, TI_MLC_EE_TXEN);
329 for (i = 0x80; i; i >>= 1) {
330 TI_SETBIT(sc, TI_MISC_LOCAL_CTL, TI_MLC_EE_CLK);
331 DELAY(1);
332 if (CSR_READ_4(sc, TI_MISC_LOCAL_CTL) & TI_MLC_EE_DIN)
333 byte |= i;
334 TI_CLRBIT(sc, TI_MISC_LOCAL_CTL, TI_MLC_EE_CLK);
335 DELAY(1);
336 }
337
338 EEPROM_STOP;
339
340 /*
341 * No ACK generated for read, so just return byte.
342 */
343
344 *dest = byte;
345
346 return(0);
347}
348
349/*
350 * Read a sequence of bytes from the EEPROM.
351 */
352static int ti_read_eeprom(sc, dest, off, cnt)
353 struct ti_softc *sc;
354 caddr_t dest;
355 int off;
356 int cnt;
357{
358 int err = 0, i;
359 u_int8_t byte = 0;
360
361 for (i = 0; i < cnt; i++) {
362 err = ti_eeprom_getbyte(sc, off + i, &byte);
363 if (err)
364 break;
365 *(dest + i) = byte;
366 }
367
368 return(err ? 1 : 0);
369}
370
371/*
372 * NIC memory access function. Can be used to either clear a section
373 * of NIC local memory or (if buf is non-NULL) copy data into it.
374 */
375static void ti_mem(sc, addr, len, buf)
376 struct ti_softc *sc;
377 u_int32_t addr, len;
378 caddr_t buf;
379{
380 int segptr, segsize, cnt;
381 caddr_t ti_winbase, ptr;
382
383 segptr = addr;
384 cnt = len;
385 ti_winbase = (caddr_t)(sc->ti_vhandle + TI_WINDOW);
386 ptr = buf;
387
388 while(cnt) {
389 if (cnt < TI_WINLEN)
390 segsize = cnt;
391 else
392 segsize = TI_WINLEN - (segptr % TI_WINLEN);
393 CSR_WRITE_4(sc, TI_WINBASE, (segptr & ~(TI_WINLEN - 1)));
394 if (buf == NULL)
395 bzero((char *)ti_winbase + (segptr &
396 (TI_WINLEN - 1)), segsize);
397 else {
398 bcopy((char *)ptr, (char *)ti_winbase +
399 (segptr & (TI_WINLEN - 1)), segsize);
400 ptr += segsize;
401 }
402 segptr += segsize;
403 cnt -= segsize;
404 }
405
406 return;
407}
408
409/*
410 * Load firmware image into the NIC. Check that the firmware revision
411 * is acceptable and see if we want the firmware for the Tigon 1 or
412 * Tigon 2.
413 */
414static void ti_loadfw(sc)
415 struct ti_softc *sc;
416{
417 switch(sc->ti_hwrev) {
418 case TI_HWREV_TIGON:
419 if (tigonFwReleaseMajor != TI_FIRMWARE_MAJOR ||
420 tigonFwReleaseMinor != TI_FIRMWARE_MINOR ||
421 tigonFwReleaseFix != TI_FIRMWARE_FIX) {
422 printf("ti%d: firmware revision mismatch; want "
423 "%d.%d.%d, got %d.%d.%d\n", sc->ti_unit,
424 TI_FIRMWARE_MAJOR, TI_FIRMWARE_MINOR,
425 TI_FIRMWARE_FIX, tigonFwReleaseMajor,
426 tigonFwReleaseMinor, tigonFwReleaseFix);
427 return;
428 }
429 ti_mem(sc, tigonFwTextAddr, tigonFwTextLen,
430 (caddr_t)tigonFwText);
431 ti_mem(sc, tigonFwDataAddr, tigonFwDataLen,
432 (caddr_t)tigonFwData);
433 ti_mem(sc, tigonFwRodataAddr, tigonFwRodataLen,
434 (caddr_t)tigonFwRodata);
435 ti_mem(sc, tigonFwBssAddr, tigonFwBssLen, NULL);
436 ti_mem(sc, tigonFwSbssAddr, tigonFwSbssLen, NULL);
437 CSR_WRITE_4(sc, TI_CPU_PROGRAM_COUNTER, tigonFwStartAddr);
438 break;
439 case TI_HWREV_TIGON_II:
440 if (tigon2FwReleaseMajor != TI_FIRMWARE_MAJOR ||
441 tigon2FwReleaseMinor != TI_FIRMWARE_MINOR ||
442 tigon2FwReleaseFix != TI_FIRMWARE_FIX) {
443 printf("ti%d: firmware revision mismatch; want "
444 "%d.%d.%d, got %d.%d.%d\n", sc->ti_unit,
445 TI_FIRMWARE_MAJOR, TI_FIRMWARE_MINOR,
446 TI_FIRMWARE_FIX, tigon2FwReleaseMajor,
447 tigon2FwReleaseMinor, tigon2FwReleaseFix);
448 return;
449 }
450 ti_mem(sc, tigon2FwTextAddr, tigon2FwTextLen,
451 (caddr_t)tigon2FwText);
452 ti_mem(sc, tigon2FwDataAddr, tigon2FwDataLen,
453 (caddr_t)tigon2FwData);
454 ti_mem(sc, tigon2FwRodataAddr, tigon2FwRodataLen,
455 (caddr_t)tigon2FwRodata);
456 ti_mem(sc, tigon2FwBssAddr, tigon2FwBssLen, NULL);
457 ti_mem(sc, tigon2FwSbssAddr, tigon2FwSbssLen, NULL);
458 CSR_WRITE_4(sc, TI_CPU_PROGRAM_COUNTER, tigon2FwStartAddr);
459 break;
460 default:
461 printf("ti%d: can't load firmware: unknown hardware rev\n",
462 sc->ti_unit);
463 break;
464 }
465
466 return;
467}
468
469/*
470 * Send the NIC a command via the command ring.
471 */
472static void ti_cmd(sc, cmd)
473 struct ti_softc *sc;
474 struct ti_cmd_desc *cmd;
475{
476 u_int32_t index;
477
478 if (sc->ti_rdata->ti_cmd_ring == NULL)
479 return;
480
481 index = sc->ti_cmd_saved_prodidx;
482 CSR_WRITE_4(sc, TI_GCR_CMDRING + (index * 4), *(u_int32_t *)(cmd));
483 TI_INC(index, TI_CMD_RING_CNT);
484 CSR_WRITE_4(sc, TI_MB_CMDPROD_IDX, index);
485 sc->ti_cmd_saved_prodidx = index;
486
487 return;
488}
489
490/*
491 * Send the NIC an extended command. The 'len' parameter specifies the
492 * number of command slots to include after the initial command.
493 */
494static void ti_cmd_ext(sc, cmd, arg, len)
495 struct ti_softc *sc;
496 struct ti_cmd_desc *cmd;
497 caddr_t arg;
498 int len;
499{
500 u_int32_t index;
501 register int i;
502
503 if (sc->ti_rdata->ti_cmd_ring == NULL)
504 return;
505
506 index = sc->ti_cmd_saved_prodidx;
507 CSR_WRITE_4(sc, TI_GCR_CMDRING + (index * 4), *(u_int32_t *)(cmd));
508 TI_INC(index, TI_CMD_RING_CNT);
509 for (i = 0; i < len; i++) {
510 CSR_WRITE_4(sc, TI_GCR_CMDRING + (index * 4),
511 *(u_int32_t *)(&arg[i * 4]));
512 TI_INC(index, TI_CMD_RING_CNT);
513 }
514 CSR_WRITE_4(sc, TI_MB_CMDPROD_IDX, index);
515 sc->ti_cmd_saved_prodidx = index;
516
517 return;
518}
519
520/*
521 * Handle events that have triggered interrupts.
522 */
523static void ti_handle_events(sc)
524 struct ti_softc *sc;
525{
526 struct ti_event_desc *e;
527
528 if (sc->ti_rdata->ti_event_ring == NULL)
529 return;
530
531 while (sc->ti_ev_saved_considx != sc->ti_ev_prodidx.ti_idx) {
532 e = &sc->ti_rdata->ti_event_ring[sc->ti_ev_saved_considx];
533 switch(e->ti_event) {
534 case TI_EV_LINKSTAT_CHANGED:
535 sc->ti_linkstat = e->ti_code;
536 if (e->ti_code == TI_EV_CODE_LINK_UP)
537 printf("ti%d: 10/100 link up\n", sc->ti_unit);
538 else if (e->ti_code == TI_EV_CODE_GIG_LINK_UP)
539 printf("ti%d: gigabit link up\n", sc->ti_unit);
540 else if (e->ti_code == TI_EV_CODE_LINK_DOWN)
541 printf("ti%d: link down\n", sc->ti_unit);
542 break;
543 case TI_EV_ERROR:
544 if (e->ti_code == TI_EV_CODE_ERR_INVAL_CMD)
545 printf("ti%d: invalid command\n", sc->ti_unit);
546 else if (e->ti_code == TI_EV_CODE_ERR_UNIMP_CMD)
547 printf("ti%d: unknown command\n", sc->ti_unit);
548 else if (e->ti_code == TI_EV_CODE_ERR_BADCFG)
549 printf("ti%d: bad config data\n", sc->ti_unit);
550 break;
551 case TI_EV_FIRMWARE_UP:
552 ti_init2(sc);
553 break;
554 case TI_EV_STATS_UPDATED:
555 ti_stats_update(sc);
556 break;
557 case TI_EV_RESET_JUMBO_RING:
558 case TI_EV_MCAST_UPDATED:
559 /* Who cares. */
560 break;
561 default:
562 printf("ti%d: unknown event: %d\n",
563 sc->ti_unit, e->ti_event);
564 break;
565 }
566 /* Advance the consumer index. */
567 TI_INC(sc->ti_ev_saved_considx, TI_EVENT_RING_CNT);
568 CSR_WRITE_4(sc, TI_GCR_EVENTCONS_IDX, sc->ti_ev_saved_considx);
569 }
570
571 return;
572}
573
574/*
575 * Memory management for the jumbo receive ring is a pain in the
576 * butt. We need to allocate at least 9018 bytes of space per frame,
577 * _and_ it has to be contiguous (unless you use the extended
578 * jumbo descriptor format). Using malloc() all the time won't
579 * work: malloc() allocates memory in powers of two, which means we
580 * would end up wasting a considerable amount of space by allocating
581 * 9K chunks. We don't have a jumbo mbuf cluster pool. Thus, we have
582 * to do our own memory management.
583 *
584 * The driver needs to allocate a contiguous chunk of memory at boot
585 * time. We then chop this up ourselves into 9K pieces and use them
586 * as external mbuf storage.
587 *
588 * One issue here is how much memory to allocate. The jumbo ring has
589 * 256 slots in it, but at 9K per slot than can consume over 2MB of
590 * RAM. This is a bit much, especially considering we also need
591 * RAM for the standard ring and mini ring (on the Tigon 2). To
592 * save space, we only actually allocate enough memory for 64 slots
593 * by default, which works out to between 500 and 600K. This can
594 * be tuned by changing a #define in if_tireg.h.
595 */
596
597static int ti_alloc_jumbo_mem(sc)
598 struct ti_softc *sc;
599{
600 caddr_t ptr;
601 register int i;
602 struct ti_jpool_entry *entry;
603
604 /* Grab a big chunk o' storage. */
605 sc->ti_cdata.ti_jumbo_buf = contigmalloc(TI_JMEM, M_DEVBUF,
606 M_NOWAIT, 0, 0xffffffff, PAGE_SIZE, 0);
607
608 if (sc->ti_cdata.ti_jumbo_buf == NULL) {
609 printf("ti%d: no memory for jumbo buffers!\n", sc->ti_unit);
610 return(ENOBUFS);
611 }
612
613 SLIST_INIT(&sc->ti_jfree_listhead);
614 SLIST_INIT(&sc->ti_jinuse_listhead);
615
616 /*
617 * Now divide it up into 9K pieces and save the addresses
618 * in an array. Note that we play an evil trick here by using
619 * the first few bytes in the buffer to hold the the address
620 * of the softc structure for this interface. This is because
621 * ti_jfree() needs it, but it is called by the mbuf management
622 * code which will not pass it to us explicitly.
623 */
624 ptr = sc->ti_cdata.ti_jumbo_buf;
625 for (i = 0; i < TI_JSLOTS; i++) {
626 u_int64_t **aptr;
627 aptr = (u_int64_t **)ptr;
628 aptr[0] = (u_int64_t *)sc;
629 ptr += sizeof(u_int64_t);
630 sc->ti_cdata.ti_jslots[i].ti_buf = ptr;
632 sc->ti_cdata.ti_jslots[i].ti_inuse = 0;
631 ptr += (TI_JLEN - sizeof(u_int64_t));
632 entry = malloc(sizeof(struct ti_jpool_entry),
633 M_DEVBUF, M_NOWAIT);
634 if (entry == NULL) {
635 contigfree(sc->ti_cdata.ti_jumbo_buf, TI_JMEM,
636 M_DEVBUF);
637 sc->ti_cdata.ti_jumbo_buf = NULL;
638 printf("ti%d: no memory for jumbo "
639 "buffer queue!\n", sc->ti_unit);
640 return(ENOBUFS);
641 }
642 entry->slot = i;
643 SLIST_INSERT_HEAD(&sc->ti_jfree_listhead, entry, jpool_entries);
644 }
645
646 return(0);
647}
648
649/*
650 * Allocate a jumbo buffer.
651 */
652static void *ti_jalloc(sc)
653 struct ti_softc *sc;
654{
655 struct ti_jpool_entry *entry;
656
657 entry = SLIST_FIRST(&sc->ti_jfree_listhead);
658
659 if (entry == NULL) {
660 printf("ti%d: no free jumbo buffers\n", sc->ti_unit);
661 return(NULL);
662 }
663
664 SLIST_REMOVE_HEAD(&sc->ti_jfree_listhead, jpool_entries);
665 SLIST_INSERT_HEAD(&sc->ti_jinuse_listhead, entry, jpool_entries);
668 sc->ti_cdata.ti_jslots[entry->slot].ti_inuse = 1;
666 return(sc->ti_cdata.ti_jslots[entry->slot].ti_buf);
667}
668
669/*
673 * Adjust usage count on a jumbo buffer. In general this doesn't
674 * get used much because our jumbo buffers don't get passed around
675 * too much, but it's implemented for correctness.
676 */
677static void ti_jref(buf, size)
678 caddr_t buf;
679 u_int size;
680{
681 struct ti_softc *sc;
682 u_int64_t **aptr;
683 register int i;
684
685 /* Extract the softc struct pointer. */
686 aptr = (u_int64_t **)(buf - sizeof(u_int64_t));
687 sc = (struct ti_softc *)(aptr[0]);
688
689 if (sc == NULL)
690 panic("ti_jref: can't find softc pointer!");
691
692 if (size != TI_JUMBO_FRAMELEN)
693 panic("ti_jref: adjusting refcount of buf of wrong size!");
694
695 /* calculate the slot this buffer belongs to */
696
697 i = ((vm_offset_t)aptr
698 - (vm_offset_t)sc->ti_cdata.ti_jumbo_buf) / TI_JLEN;
699
700 if ((i < 0) || (i >= TI_JSLOTS))
701 panic("ti_jref: asked to reference buffer "
702 "that we don't manage!");
703 else if (sc->ti_cdata.ti_jslots[i].ti_inuse == 0)
704 panic("ti_jref: buffer already free!");
705 else
706 sc->ti_cdata.ti_jslots[i].ti_inuse++;
707
708 return;
709}
710
711/*
670 * Release a jumbo buffer.
671 */
714static void ti_jfree(buf, size)
672static void ti_jfree(buf, args)
673 caddr_t buf;
716 u_int size;
674 void *args;
675{
676 struct ti_softc *sc;
677 u_int64_t **aptr;
678 int i;
679 struct ti_jpool_entry *entry;
680
681 /* Extract the softc struct pointer. */
682 aptr = (u_int64_t **)(buf - sizeof(u_int64_t));
683 sc = (struct ti_softc *)(aptr[0]);
684
685 if (sc == NULL)
686 panic("ti_jfree: can't find softc pointer!");
687
730 if (size != TI_JUMBO_FRAMELEN)
731 panic("ti_jfree: freeing buffer of wrong size!");
732
688 /* calculate the slot this buffer belongs to */
734
689 i = ((vm_offset_t)aptr
690 - (vm_offset_t)sc->ti_cdata.ti_jumbo_buf) / TI_JLEN;
691
692 if ((i < 0) || (i >= TI_JSLOTS))
693 panic("ti_jfree: asked to free buffer that we don't manage!");
740 else if (sc->ti_cdata.ti_jslots[i].ti_inuse == 0)
741 panic("ti_jfree: buffer already free!");
742 else {
743 sc->ti_cdata.ti_jslots[i].ti_inuse--;
744 if(sc->ti_cdata.ti_jslots[i].ti_inuse == 0) {
745 entry = SLIST_FIRST(&sc->ti_jinuse_listhead);
746 if (entry == NULL)
747 panic("ti_jfree: buffer not in use!");
748 entry->slot = i;
749 SLIST_REMOVE_HEAD(&sc->ti_jinuse_listhead,
750 jpool_entries);
751 SLIST_INSERT_HEAD(&sc->ti_jfree_listhead,
752 entry, jpool_entries);
753 }
754 }
694
695 entry = SLIST_FIRST(&sc->ti_jinuse_listhead);
696 if (entry == NULL)
697 panic("ti_jfree: buffer not in use!");
698 entry->slot = i;
699 SLIST_REMOVE_HEAD(&sc->ti_jinuse_listhead, jpool_entries);
700 SLIST_INSERT_HEAD(&sc->ti_jfree_listhead, entry, jpool_entries);
701
702 return;
703}
704
705
706/*
707 * Intialize a standard receive ring descriptor.
708 */
709static int ti_newbuf_std(sc, i, m)
710 struct ti_softc *sc;
711 int i;
712 struct mbuf *m;
713{
714 struct mbuf *m_new = NULL;
715 struct ti_rx_desc *r;
716
717 if (m == NULL) {
718 MGETHDR(m_new, M_DONTWAIT, MT_DATA);
719 if (m_new == NULL) {
720 printf("ti%d: mbuf allocation failed "
721 "-- packet dropped!\n", sc->ti_unit);
722 return(ENOBUFS);
723 }
724
725 MCLGET(m_new, M_DONTWAIT);
726 if (!(m_new->m_flags & M_EXT)) {
727 printf("ti%d: cluster allocation failed "
728 "-- packet dropped!\n", sc->ti_unit);
729 m_freem(m_new);
730 return(ENOBUFS);
731 }
732 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
733 } else {
734 m_new = m;
735 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
736 m_new->m_data = m_new->m_ext.ext_buf;
737 }
738
739 m_adj(m_new, ETHER_ALIGN);
740 sc->ti_cdata.ti_rx_std_chain[i] = m_new;
741 r = &sc->ti_rdata->ti_rx_std_ring[i];
742 TI_HOSTADDR(r->ti_addr) = vtophys(mtod(m_new, caddr_t));
743 r->ti_type = TI_BDTYPE_RECV_BD;
744 r->ti_flags = 0;
745 if (sc->arpcom.ac_if.if_hwassist)
746 r->ti_flags |= TI_BDFLAG_TCP_UDP_CKSUM | TI_BDFLAG_IP_CKSUM;
747 r->ti_len = m_new->m_len;
748 r->ti_idx = i;
749
750 return(0);
751}
752
753/*
754 * Intialize a mini receive ring descriptor. This only applies to
755 * the Tigon 2.
756 */
757static int ti_newbuf_mini(sc, i, m)
758 struct ti_softc *sc;
759 int i;
760 struct mbuf *m;
761{
762 struct mbuf *m_new = NULL;
763 struct ti_rx_desc *r;
764
765 if (m == NULL) {
766 MGETHDR(m_new, M_DONTWAIT, MT_DATA);
767 if (m_new == NULL) {
768 printf("ti%d: mbuf allocation failed "
769 "-- packet dropped!\n", sc->ti_unit);
770 return(ENOBUFS);
771 }
772 m_new->m_len = m_new->m_pkthdr.len = MHLEN;
773 } else {
774 m_new = m;
775 m_new->m_data = m_new->m_pktdat;
776 m_new->m_len = m_new->m_pkthdr.len = MHLEN;
777 }
778
779 m_adj(m_new, ETHER_ALIGN);
780 r = &sc->ti_rdata->ti_rx_mini_ring[i];
781 sc->ti_cdata.ti_rx_mini_chain[i] = m_new;
782 TI_HOSTADDR(r->ti_addr) = vtophys(mtod(m_new, caddr_t));
783 r->ti_type = TI_BDTYPE_RECV_BD;
784 r->ti_flags = TI_BDFLAG_MINI_RING;
785 if (sc->arpcom.ac_if.if_hwassist)
786 r->ti_flags |= TI_BDFLAG_TCP_UDP_CKSUM | TI_BDFLAG_IP_CKSUM;
787 r->ti_len = m_new->m_len;
788 r->ti_idx = i;
789
790 return(0);
791}
792
793/*
794 * Initialize a jumbo receive ring descriptor. This allocates
795 * a jumbo buffer from the pool managed internally by the driver.
796 */
797static int ti_newbuf_jumbo(sc, i, m)
798 struct ti_softc *sc;
799 int i;
800 struct mbuf *m;
801{
802 struct mbuf *m_new = NULL;
803 struct ti_rx_desc *r;
804
805 if (m == NULL) {
806 caddr_t *buf = NULL;
807
808 /* Allocate the mbuf. */
809 MGETHDR(m_new, M_DONTWAIT, MT_DATA);
810 if (m_new == NULL) {
811 printf("ti%d: mbuf allocation failed "
812 "-- packet dropped!\n", sc->ti_unit);
813 return(ENOBUFS);
814 }
815
816 /* Allocate the jumbo buffer */
817 buf = ti_jalloc(sc);
818 if (buf == NULL) {
819 m_freem(m_new);
820 printf("ti%d: jumbo allocation failed "
821 "-- packet dropped!\n", sc->ti_unit);
822 return(ENOBUFS);
823 }
824
825 /* Attach the buffer to the mbuf. */
880 m_new->m_data = m_new->m_ext.ext_buf = (void *)buf;
881 m_new->m_flags |= M_EXT;
882 m_new->m_len = m_new->m_pkthdr.len =
883 m_new->m_ext.ext_size = TI_JUMBO_FRAMELEN;
884 m_new->m_ext.ext_free = ti_jfree;
885 m_new->m_ext.ext_ref = ti_jref;
826 m_new->m_data = (void *) buf;
827 m_new->m_len = m_new->m_pkthdr.len = TI_JUMBO_FRAMELEN;
828 MEXTADD(m_new, buf, TI_JUMBO_FRAMELEN, ti_jfree, NULL);
829 } else {
830 m_new = m;
831 m_new->m_data = m_new->m_ext.ext_buf;
832 m_new->m_ext.ext_size = TI_JUMBO_FRAMELEN;
833 }
834
835 m_adj(m_new, ETHER_ALIGN);
836 /* Set up the descriptor. */
837 r = &sc->ti_rdata->ti_rx_jumbo_ring[i];
838 sc->ti_cdata.ti_rx_jumbo_chain[i] = m_new;
839 TI_HOSTADDR(r->ti_addr) = vtophys(mtod(m_new, caddr_t));
840 r->ti_type = TI_BDTYPE_RECV_JUMBO_BD;
841 r->ti_flags = TI_BDFLAG_JUMBO_RING;
842 if (sc->arpcom.ac_if.if_hwassist)
843 r->ti_flags |= TI_BDFLAG_TCP_UDP_CKSUM | TI_BDFLAG_IP_CKSUM;
844 r->ti_len = m_new->m_len;
845 r->ti_idx = i;
846
847 return(0);
848}
849
850/*
851 * The standard receive ring has 512 entries in it. At 2K per mbuf cluster,
852 * that's 1MB or memory, which is a lot. For now, we fill only the first
853 * 256 ring entries and hope that our CPU is fast enough to keep up with
854 * the NIC.
855 */
856static int ti_init_rx_ring_std(sc)
857 struct ti_softc *sc;
858{
859 register int i;
860 struct ti_cmd_desc cmd;
861
862 for (i = 0; i < TI_SSLOTS; i++) {
863 if (ti_newbuf_std(sc, i, NULL) == ENOBUFS)
864 return(ENOBUFS);
865 };
866
867 TI_UPDATE_STDPROD(sc, i - 1);
868 sc->ti_std = i - 1;
869
870 return(0);
871}
872
873static void ti_free_rx_ring_std(sc)
874 struct ti_softc *sc;
875{
876 register int i;
877
878 for (i = 0; i < TI_STD_RX_RING_CNT; i++) {
879 if (sc->ti_cdata.ti_rx_std_chain[i] != NULL) {
880 m_freem(sc->ti_cdata.ti_rx_std_chain[i]);
881 sc->ti_cdata.ti_rx_std_chain[i] = NULL;
882 }
883 bzero((char *)&sc->ti_rdata->ti_rx_std_ring[i],
884 sizeof(struct ti_rx_desc));
885 }
886
887 return;
888}
889
890static int ti_init_rx_ring_jumbo(sc)
891 struct ti_softc *sc;
892{
893 register int i;
894 struct ti_cmd_desc cmd;
895
896 for (i = 0; i < TI_JUMBO_RX_RING_CNT; i++) {
897 if (ti_newbuf_jumbo(sc, i, NULL) == ENOBUFS)
898 return(ENOBUFS);
899 };
900
901 TI_UPDATE_JUMBOPROD(sc, i - 1);
902 sc->ti_jumbo = i - 1;
903
904 return(0);
905}
906
907static void ti_free_rx_ring_jumbo(sc)
908 struct ti_softc *sc;
909{
910 register int i;
911
912 for (i = 0; i < TI_JUMBO_RX_RING_CNT; i++) {
913 if (sc->ti_cdata.ti_rx_jumbo_chain[i] != NULL) {
914 m_freem(sc->ti_cdata.ti_rx_jumbo_chain[i]);
915 sc->ti_cdata.ti_rx_jumbo_chain[i] = NULL;
916 }
917 bzero((char *)&sc->ti_rdata->ti_rx_jumbo_ring[i],
918 sizeof(struct ti_rx_desc));
919 }
920
921 return;
922}
923
924static int ti_init_rx_ring_mini(sc)
925 struct ti_softc *sc;
926{
927 register int i;
928
929 for (i = 0; i < TI_MSLOTS; i++) {
930 if (ti_newbuf_mini(sc, i, NULL) == ENOBUFS)
931 return(ENOBUFS);
932 };
933
934 TI_UPDATE_MINIPROD(sc, i - 1);
935 sc->ti_mini = i - 1;
936
937 return(0);
938}
939
940static void ti_free_rx_ring_mini(sc)
941 struct ti_softc *sc;
942{
943 register int i;
944
945 for (i = 0; i < TI_MINI_RX_RING_CNT; i++) {
946 if (sc->ti_cdata.ti_rx_mini_chain[i] != NULL) {
947 m_freem(sc->ti_cdata.ti_rx_mini_chain[i]);
948 sc->ti_cdata.ti_rx_mini_chain[i] = NULL;
949 }
950 bzero((char *)&sc->ti_rdata->ti_rx_mini_ring[i],
951 sizeof(struct ti_rx_desc));
952 }
953
954 return;
955}
956
957static void ti_free_tx_ring(sc)
958 struct ti_softc *sc;
959{
960 register int i;
961
962 if (sc->ti_rdata->ti_tx_ring == NULL)
963 return;
964
965 for (i = 0; i < TI_TX_RING_CNT; i++) {
966 if (sc->ti_cdata.ti_tx_chain[i] != NULL) {
967 m_freem(sc->ti_cdata.ti_tx_chain[i]);
968 sc->ti_cdata.ti_tx_chain[i] = NULL;
969 }
970 bzero((char *)&sc->ti_rdata->ti_tx_ring[i],
971 sizeof(struct ti_tx_desc));
972 }
973
974 return;
975}
976
977static int ti_init_tx_ring(sc)
978 struct ti_softc *sc;
979{
980 sc->ti_txcnt = 0;
981 sc->ti_tx_saved_considx = 0;
982 CSR_WRITE_4(sc, TI_MB_SENDPROD_IDX, 0);
983 return(0);
984}
985
986/*
987 * The Tigon 2 firmware has a new way to add/delete multicast addresses,
988 * but we have to support the old way too so that Tigon 1 cards will
989 * work.
990 */
991void ti_add_mcast(sc, addr)
992 struct ti_softc *sc;
993 struct ether_addr *addr;
994{
995 struct ti_cmd_desc cmd;
996 u_int16_t *m;
997 u_int32_t ext[2] = {0, 0};
998
999 m = (u_int16_t *)&addr->octet[0];
1000
1001 switch(sc->ti_hwrev) {
1002 case TI_HWREV_TIGON:
1003 CSR_WRITE_4(sc, TI_GCR_MAR0, htons(m[0]));
1004 CSR_WRITE_4(sc, TI_GCR_MAR1, (htons(m[1]) << 16) | htons(m[2]));
1005 TI_DO_CMD(TI_CMD_ADD_MCAST_ADDR, 0, 0);
1006 break;
1007 case TI_HWREV_TIGON_II:
1008 ext[0] = htons(m[0]);
1009 ext[1] = (htons(m[1]) << 16) | htons(m[2]);
1010 TI_DO_CMD_EXT(TI_CMD_EXT_ADD_MCAST, 0, 0, (caddr_t)&ext, 2);
1011 break;
1012 default:
1013 printf("ti%d: unknown hwrev\n", sc->ti_unit);
1014 break;
1015 }
1016
1017 return;
1018}
1019
1020void ti_del_mcast(sc, addr)
1021 struct ti_softc *sc;
1022 struct ether_addr *addr;
1023{
1024 struct ti_cmd_desc cmd;
1025 u_int16_t *m;
1026 u_int32_t ext[2] = {0, 0};
1027
1028 m = (u_int16_t *)&addr->octet[0];
1029
1030 switch(sc->ti_hwrev) {
1031 case TI_HWREV_TIGON:
1032 CSR_WRITE_4(sc, TI_GCR_MAR0, htons(m[0]));
1033 CSR_WRITE_4(sc, TI_GCR_MAR1, (htons(m[1]) << 16) | htons(m[2]));
1034 TI_DO_CMD(TI_CMD_DEL_MCAST_ADDR, 0, 0);
1035 break;
1036 case TI_HWREV_TIGON_II:
1037 ext[0] = htons(m[0]);
1038 ext[1] = (htons(m[1]) << 16) | htons(m[2]);
1039 TI_DO_CMD_EXT(TI_CMD_EXT_DEL_MCAST, 0, 0, (caddr_t)&ext, 2);
1040 break;
1041 default:
1042 printf("ti%d: unknown hwrev\n", sc->ti_unit);
1043 break;
1044 }
1045
1046 return;
1047}
1048
1049/*
1050 * Configure the Tigon's multicast address filter.
1051 *
1052 * The actual multicast table management is a bit of a pain, thanks to
1053 * slight brain damage on the part of both Alteon and us. With our
1054 * multicast code, we are only alerted when the multicast address table
1055 * changes and at that point we only have the current list of addresses:
1056 * we only know the current state, not the previous state, so we don't
1057 * actually know what addresses were removed or added. The firmware has
1058 * state, but we can't get our grubby mits on it, and there is no 'delete
1059 * all multicast addresses' command. Hence, we have to maintain our own
1060 * state so we know what addresses have been programmed into the NIC at
1061 * any given time.
1062 */
1063static void ti_setmulti(sc)
1064 struct ti_softc *sc;
1065{
1066 struct ifnet *ifp;
1067 struct ifmultiaddr *ifma;
1068 struct ti_cmd_desc cmd;
1069 struct ti_mc_entry *mc;
1070 u_int32_t intrs;
1071
1072 ifp = &sc->arpcom.ac_if;
1073
1074 if (ifp->if_flags & IFF_ALLMULTI) {
1075 TI_DO_CMD(TI_CMD_SET_ALLMULTI, TI_CMD_CODE_ALLMULTI_ENB, 0);
1076 return;
1077 } else {
1078 TI_DO_CMD(TI_CMD_SET_ALLMULTI, TI_CMD_CODE_ALLMULTI_DIS, 0);
1079 }
1080
1081 /* Disable interrupts. */
1082 intrs = CSR_READ_4(sc, TI_MB_HOSTINTR);
1083 CSR_WRITE_4(sc, TI_MB_HOSTINTR, 1);
1084
1085 /* First, zot all the existing filters. */
1086 while (sc->ti_mc_listhead.slh_first != NULL) {
1087 mc = sc->ti_mc_listhead.slh_first;
1088 ti_del_mcast(sc, &mc->mc_addr);
1089 SLIST_REMOVE_HEAD(&sc->ti_mc_listhead, mc_entries);
1090 free(mc, M_DEVBUF);
1091 }
1092
1093 /* Now program new ones. */
1094 for (ifma = ifp->if_multiaddrs.lh_first;
1095 ifma != NULL; ifma = ifma->ifma_link.le_next) {
1096 if (ifma->ifma_addr->sa_family != AF_LINK)
1097 continue;
1098 mc = malloc(sizeof(struct ti_mc_entry), M_DEVBUF, M_NOWAIT);
1099 bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
1100 (char *)&mc->mc_addr, ETHER_ADDR_LEN);
1101 SLIST_INSERT_HEAD(&sc->ti_mc_listhead, mc, mc_entries);
1102 ti_add_mcast(sc, &mc->mc_addr);
1103 }
1104
1105 /* Re-enable interrupts. */
1106 CSR_WRITE_4(sc, TI_MB_HOSTINTR, intrs);
1107
1108 return;
1109}
1110
1111/*
1112 * Check to see if the BIOS has configured us for a 64 bit slot when
1113 * we aren't actually in one. If we detect this condition, we can work
1114 * around it on the Tigon 2 by setting a bit in the PCI state register,
1115 * but for the Tigon 1 we must give up and abort the interface attach.
1116 */
1117static int ti_64bitslot_war(sc)
1118 struct ti_softc *sc;
1119{
1120 if (!(CSR_READ_4(sc, TI_PCI_STATE) & TI_PCISTATE_32BIT_BUS)) {
1121 CSR_WRITE_4(sc, 0x600, 0);
1122 CSR_WRITE_4(sc, 0x604, 0);
1123 CSR_WRITE_4(sc, 0x600, 0x5555AAAA);
1124 if (CSR_READ_4(sc, 0x604) == 0x5555AAAA) {
1125 if (sc->ti_hwrev == TI_HWREV_TIGON)
1126 return(EINVAL);
1127 else {
1128 TI_SETBIT(sc, TI_PCI_STATE,
1129 TI_PCISTATE_32BIT_BUS);
1130 return(0);
1131 }
1132 }
1133 }
1134
1135 return(0);
1136}
1137
1138/*
1139 * Do endian, PCI and DMA initialization. Also check the on-board ROM
1140 * self-test results.
1141 */
1142static int ti_chipinit(sc)
1143 struct ti_softc *sc;
1144{
1145 u_int32_t cacheline;
1146 u_int32_t pci_writemax = 0;
1147
1148 /* Initialize link to down state. */
1149 sc->ti_linkstat = TI_EV_CODE_LINK_DOWN;
1150
1151 sc->arpcom.ac_if.if_hwassist = TI_CSUM_FEATURES;
1152
1153 /* Set endianness before we access any non-PCI registers. */
1154#if BYTE_ORDER == BIG_ENDIAN
1155 CSR_WRITE_4(sc, TI_MISC_HOST_CTL,
1156 TI_MHC_BIGENDIAN_INIT | (TI_MHC_BIGENDIAN_INIT << 24));
1157#else
1158 CSR_WRITE_4(sc, TI_MISC_HOST_CTL,
1159 TI_MHC_LITTLEENDIAN_INIT | (TI_MHC_LITTLEENDIAN_INIT << 24));
1160#endif
1161
1162 /* Check the ROM failed bit to see if self-tests passed. */
1163 if (CSR_READ_4(sc, TI_CPU_STATE) & TI_CPUSTATE_ROMFAIL) {
1164 printf("ti%d: board self-diagnostics failed!\n", sc->ti_unit);
1165 return(ENODEV);
1166 }
1167
1168 /* Halt the CPU. */
1169 TI_SETBIT(sc, TI_CPU_STATE, TI_CPUSTATE_HALT);
1170
1171 /* Figure out the hardware revision. */
1172 switch(CSR_READ_4(sc, TI_MISC_HOST_CTL) & TI_MHC_CHIP_REV_MASK) {
1173 case TI_REV_TIGON_I:
1174 sc->ti_hwrev = TI_HWREV_TIGON;
1175 break;
1176 case TI_REV_TIGON_II:
1177 sc->ti_hwrev = TI_HWREV_TIGON_II;
1178 break;
1179 default:
1180 printf("ti%d: unsupported chip revision\n", sc->ti_unit);
1181 return(ENODEV);
1182 }
1183
1184 /* Do special setup for Tigon 2. */
1185 if (sc->ti_hwrev == TI_HWREV_TIGON_II) {
1186 TI_SETBIT(sc, TI_CPU_CTL_B, TI_CPUSTATE_HALT);
1187 TI_SETBIT(sc, TI_MISC_LOCAL_CTL, TI_MLC_SRAM_BANK_256K);
1188 TI_SETBIT(sc, TI_MISC_CONF, TI_MCR_SRAM_SYNCHRONOUS);
1189 }
1190
1191 /* Set up the PCI state register. */
1192 CSR_WRITE_4(sc, TI_PCI_STATE, TI_PCI_READ_CMD|TI_PCI_WRITE_CMD);
1193 if (sc->ti_hwrev == TI_HWREV_TIGON_II) {
1194 TI_SETBIT(sc, TI_PCI_STATE, TI_PCISTATE_USE_MEM_RD_MULT);
1195 }
1196
1197 /* Clear the read/write max DMA parameters. */
1198 TI_CLRBIT(sc, TI_PCI_STATE, (TI_PCISTATE_WRITE_MAXDMA|
1199 TI_PCISTATE_READ_MAXDMA));
1200
1201 /* Get cache line size. */
1202 cacheline = CSR_READ_4(sc, TI_PCI_BIST) & 0xFF;
1203
1204 /*
1205 * If the system has set enabled the PCI memory write
1206 * and invalidate command in the command register, set
1207 * the write max parameter accordingly. This is necessary
1208 * to use MWI with the Tigon 2.
1209 */
1210 if (CSR_READ_4(sc, TI_PCI_CMDSTAT) & PCIM_CMD_MWIEN) {
1211 switch(cacheline) {
1212 case 1:
1213 case 4:
1214 case 8:
1215 case 16:
1216 case 32:
1217 case 64:
1218 break;
1219 default:
1220 /* Disable PCI memory write and invalidate. */
1221 if (bootverbose)
1222 printf("ti%d: cache line size %d not "
1223 "supported; disabling PCI MWI\n",
1224 sc->ti_unit, cacheline);
1225 CSR_WRITE_4(sc, TI_PCI_CMDSTAT, CSR_READ_4(sc,
1226 TI_PCI_CMDSTAT) & ~PCIM_CMD_MWIEN);
1227 break;
1228 }
1229 }
1230
1231#ifdef __brokenalpha__
1232 /*
1233 * From the Alteon sample driver:
1234 * Must insure that we do not cross an 8K (bytes) boundary
1235 * for DMA reads. Our highest limit is 1K bytes. This is a
1236 * restriction on some ALPHA platforms with early revision
1237 * 21174 PCI chipsets, such as the AlphaPC 164lx
1238 */
1239 TI_SETBIT(sc, TI_PCI_STATE, pci_writemax|TI_PCI_READMAX_1024);
1240#else
1241 TI_SETBIT(sc, TI_PCI_STATE, pci_writemax);
1242#endif
1243
1244 /* This sets the min dma param all the way up (0xff). */
1245 TI_SETBIT(sc, TI_PCI_STATE, TI_PCISTATE_MINDMA);
1246
1247 /* Configure DMA variables. */
1248#if BYTE_ORDER == BIG_ENDIAN
1249 CSR_WRITE_4(sc, TI_GCR_OPMODE, TI_OPMODE_BYTESWAP_BD |
1250 TI_OPMODE_BYTESWAP_DATA | TI_OPMODE_WORDSWAP_BD |
1251 TI_OPMODE_WARN_ENB | TI_OPMODE_FATAL_ENB |
1252 TI_OPMODE_DONT_FRAG_JUMBO);
1253#else
1254 CSR_WRITE_4(sc, TI_GCR_OPMODE, TI_OPMODE_BYTESWAP_DATA|
1255 TI_OPMODE_WORDSWAP_BD|TI_OPMODE_DONT_FRAG_JUMBO|
1256 TI_OPMODE_WARN_ENB|TI_OPMODE_FATAL_ENB);
1257#endif
1258
1259 /*
1260 * Only allow 1 DMA channel to be active at a time.
1261 * I don't think this is a good idea, but without it
1262 * the firmware racks up lots of nicDmaReadRingFull
1263 * errors. This is not compatible with hardware checksums.
1264 */
1265 if (sc->arpcom.ac_if.if_hwassist == 0)
1266 TI_SETBIT(sc, TI_GCR_OPMODE, TI_OPMODE_1_DMA_ACTIVE);
1267
1268 /* Recommended settings from Tigon manual. */
1269 CSR_WRITE_4(sc, TI_GCR_DMA_WRITECFG, TI_DMA_STATE_THRESH_8W);
1270 CSR_WRITE_4(sc, TI_GCR_DMA_READCFG, TI_DMA_STATE_THRESH_8W);
1271
1272 if (ti_64bitslot_war(sc)) {
1273 printf("ti%d: bios thinks we're in a 64 bit slot, "
1274 "but we aren't", sc->ti_unit);
1275 return(EINVAL);
1276 }
1277
1278 return(0);
1279}
1280
1281/*
1282 * Initialize the general information block and firmware, and
1283 * start the CPU(s) running.
1284 */
1285static int ti_gibinit(sc)
1286 struct ti_softc *sc;
1287{
1288 struct ti_rcb *rcb;
1289 int i;
1290 struct ifnet *ifp;
1291
1292 ifp = &sc->arpcom.ac_if;
1293
1294 /* Disable interrupts for now. */
1295 CSR_WRITE_4(sc, TI_MB_HOSTINTR, 1);
1296
1297 /* Tell the chip where to find the general information block. */
1298 CSR_WRITE_4(sc, TI_GCR_GENINFO_HI, 0);
1299 CSR_WRITE_4(sc, TI_GCR_GENINFO_LO, vtophys(&sc->ti_rdata->ti_info));
1300
1301 /* Load the firmware into SRAM. */
1302 ti_loadfw(sc);
1303
1304 /* Set up the contents of the general info and ring control blocks. */
1305
1306 /* Set up the event ring and producer pointer. */
1307 rcb = &sc->ti_rdata->ti_info.ti_ev_rcb;
1308
1309 TI_HOSTADDR(rcb->ti_hostaddr) = vtophys(&sc->ti_rdata->ti_event_ring);
1310 rcb->ti_flags = 0;
1311 TI_HOSTADDR(sc->ti_rdata->ti_info.ti_ev_prodidx_ptr) =
1312 vtophys(&sc->ti_ev_prodidx);
1313 sc->ti_ev_prodidx.ti_idx = 0;
1314 CSR_WRITE_4(sc, TI_GCR_EVENTCONS_IDX, 0);
1315 sc->ti_ev_saved_considx = 0;
1316
1317 /* Set up the command ring and producer mailbox. */
1318 rcb = &sc->ti_rdata->ti_info.ti_cmd_rcb;
1319
1320 sc->ti_rdata->ti_cmd_ring =
1321 (struct ti_cmd_desc *)(sc->ti_vhandle + TI_GCR_CMDRING);
1322 TI_HOSTADDR(rcb->ti_hostaddr) = TI_GCR_NIC_ADDR(TI_GCR_CMDRING);
1323 rcb->ti_flags = 0;
1324 rcb->ti_max_len = 0;
1325 for (i = 0; i < TI_CMD_RING_CNT; i++) {
1326 CSR_WRITE_4(sc, TI_GCR_CMDRING + (i * 4), 0);
1327 }
1328 CSR_WRITE_4(sc, TI_GCR_CMDCONS_IDX, 0);
1329 CSR_WRITE_4(sc, TI_MB_CMDPROD_IDX, 0);
1330 sc->ti_cmd_saved_prodidx = 0;
1331
1332 /*
1333 * Assign the address of the stats refresh buffer.
1334 * We re-use the current stats buffer for this to
1335 * conserve memory.
1336 */
1337 TI_HOSTADDR(sc->ti_rdata->ti_info.ti_refresh_stats_ptr) =
1338 vtophys(&sc->ti_rdata->ti_info.ti_stats);
1339
1340 /* Set up the standard receive ring. */
1341 rcb = &sc->ti_rdata->ti_info.ti_std_rx_rcb;
1342 TI_HOSTADDR(rcb->ti_hostaddr) = vtophys(&sc->ti_rdata->ti_rx_std_ring);
1343 rcb->ti_max_len = TI_FRAMELEN;
1344 rcb->ti_flags = 0;
1345 if (sc->arpcom.ac_if.if_hwassist)
1346 rcb->ti_flags |= TI_RCB_FLAG_TCP_UDP_CKSUM |
1347 TI_RCB_FLAG_IP_CKSUM | TI_RCB_FLAG_NO_PHDR_CKSUM;
1348#if NVLAN > 0
1349 rcb->ti_flags |= TI_RCB_FLAG_VLAN_ASSIST;
1350#endif
1351
1352 /* Set up the jumbo receive ring. */
1353 rcb = &sc->ti_rdata->ti_info.ti_jumbo_rx_rcb;
1354 TI_HOSTADDR(rcb->ti_hostaddr) =
1355 vtophys(&sc->ti_rdata->ti_rx_jumbo_ring);
1356 rcb->ti_max_len = TI_JUMBO_FRAMELEN;
1357 rcb->ti_flags = 0;
1358 if (sc->arpcom.ac_if.if_hwassist)
1359 rcb->ti_flags |= TI_RCB_FLAG_TCP_UDP_CKSUM |
1360 TI_RCB_FLAG_IP_CKSUM | TI_RCB_FLAG_NO_PHDR_CKSUM;
1361#if NVLAN > 0
1362 rcb->ti_flags |= TI_RCB_FLAG_VLAN_ASSIST;
1363#endif
1364
1365 /*
1366 * Set up the mini ring. Only activated on the
1367 * Tigon 2 but the slot in the config block is
1368 * still there on the Tigon 1.
1369 */
1370 rcb = &sc->ti_rdata->ti_info.ti_mini_rx_rcb;
1371 TI_HOSTADDR(rcb->ti_hostaddr) =
1372 vtophys(&sc->ti_rdata->ti_rx_mini_ring);
1373 rcb->ti_max_len = MHLEN - ETHER_ALIGN;
1374 if (sc->ti_hwrev == TI_HWREV_TIGON)
1375 rcb->ti_flags = TI_RCB_FLAG_RING_DISABLED;
1376 else
1377 rcb->ti_flags = 0;
1378 if (sc->arpcom.ac_if.if_hwassist)
1379 rcb->ti_flags |= TI_RCB_FLAG_TCP_UDP_CKSUM |
1380 TI_RCB_FLAG_IP_CKSUM | TI_RCB_FLAG_NO_PHDR_CKSUM;
1381#if NVLAN > 0
1382 rcb->ti_flags |= TI_RCB_FLAG_VLAN_ASSIST;
1383#endif
1384
1385 /*
1386 * Set up the receive return ring.
1387 */
1388 rcb = &sc->ti_rdata->ti_info.ti_return_rcb;
1389 TI_HOSTADDR(rcb->ti_hostaddr) =
1390 vtophys(&sc->ti_rdata->ti_rx_return_ring);
1391 rcb->ti_flags = 0;
1392 rcb->ti_max_len = TI_RETURN_RING_CNT;
1393 TI_HOSTADDR(sc->ti_rdata->ti_info.ti_return_prodidx_ptr) =
1394 vtophys(&sc->ti_return_prodidx);
1395
1396 /*
1397 * Set up the tx ring. Note: for the Tigon 2, we have the option
1398 * of putting the transmit ring in the host's address space and
1399 * letting the chip DMA it instead of leaving the ring in the NIC's
1400 * memory and accessing it through the shared memory region. We
1401 * do this for the Tigon 2, but it doesn't work on the Tigon 1,
1402 * so we have to revert to the shared memory scheme if we detect
1403 * a Tigon 1 chip.
1404 */
1405 CSR_WRITE_4(sc, TI_WINBASE, TI_TX_RING_BASE);
1406 if (sc->ti_hwrev == TI_HWREV_TIGON) {
1407 sc->ti_rdata->ti_tx_ring_nic =
1408 (struct ti_tx_desc *)(sc->ti_vhandle + TI_WINDOW);
1409 }
1410 bzero((char *)sc->ti_rdata->ti_tx_ring,
1411 TI_TX_RING_CNT * sizeof(struct ti_tx_desc));
1412 rcb = &sc->ti_rdata->ti_info.ti_tx_rcb;
1413 if (sc->ti_hwrev == TI_HWREV_TIGON)
1414 rcb->ti_flags = 0;
1415 else
1416 rcb->ti_flags = TI_RCB_FLAG_HOST_RING;
1417#if NVLAN > 0
1418 rcb->ti_flags |= TI_RCB_FLAG_VLAN_ASSIST;
1419#endif
1420 if (sc->arpcom.ac_if.if_hwassist)
1421 rcb->ti_flags |= TI_RCB_FLAG_TCP_UDP_CKSUM |
1422 TI_RCB_FLAG_IP_CKSUM | TI_RCB_FLAG_NO_PHDR_CKSUM;
1423 rcb->ti_max_len = TI_TX_RING_CNT;
1424 if (sc->ti_hwrev == TI_HWREV_TIGON)
1425 TI_HOSTADDR(rcb->ti_hostaddr) = TI_TX_RING_BASE;
1426 else
1427 TI_HOSTADDR(rcb->ti_hostaddr) =
1428 vtophys(&sc->ti_rdata->ti_tx_ring);
1429 TI_HOSTADDR(sc->ti_rdata->ti_info.ti_tx_considx_ptr) =
1430 vtophys(&sc->ti_tx_considx);
1431
1432 /* Set up tuneables */
1433 if (ifp->if_mtu > (ETHERMTU + ETHER_HDR_LEN + ETHER_CRC_LEN))
1434 CSR_WRITE_4(sc, TI_GCR_RX_COAL_TICKS,
1435 (sc->ti_rx_coal_ticks / 10));
1436 else
1437 CSR_WRITE_4(sc, TI_GCR_RX_COAL_TICKS, sc->ti_rx_coal_ticks);
1438 CSR_WRITE_4(sc, TI_GCR_TX_COAL_TICKS, sc->ti_tx_coal_ticks);
1439 CSR_WRITE_4(sc, TI_GCR_STAT_TICKS, sc->ti_stat_ticks);
1440 CSR_WRITE_4(sc, TI_GCR_RX_MAX_COAL_BD, sc->ti_rx_max_coal_bds);
1441 CSR_WRITE_4(sc, TI_GCR_TX_MAX_COAL_BD, sc->ti_tx_max_coal_bds);
1442 CSR_WRITE_4(sc, TI_GCR_TX_BUFFER_RATIO, sc->ti_tx_buf_ratio);
1443
1444 /* Turn interrupts on. */
1445 CSR_WRITE_4(sc, TI_GCR_MASK_INTRS, 0);
1446 CSR_WRITE_4(sc, TI_MB_HOSTINTR, 0);
1447
1448 /* Start CPU. */
1449 TI_CLRBIT(sc, TI_CPU_STATE, (TI_CPUSTATE_HALT|TI_CPUSTATE_STEP));
1450
1451 return(0);
1452}
1453
1454/*
1455 * Probe for a Tigon chip. Check the PCI vendor and device IDs
1456 * against our list and return its name if we find a match.
1457 */
1458static int ti_probe(dev)
1459 device_t dev;
1460{
1461 struct ti_type *t;
1462
1463 t = ti_devs;
1464
1465 while(t->ti_name != NULL) {
1466 if ((pci_get_vendor(dev) == t->ti_vid) &&
1467 (pci_get_device(dev) == t->ti_did)) {
1468 device_set_desc(dev, t->ti_name);
1469 return(0);
1470 }
1471 t++;
1472 }
1473
1474 return(ENXIO);
1475}
1476
1477static int ti_attach(dev)
1478 device_t dev;
1479{
1480 int s;
1481 u_int32_t command;
1482 struct ifnet *ifp;
1483 struct ti_softc *sc;
1484 int unit, error = 0, rid;
1485
1486 s = splimp();
1487
1488 sc = device_get_softc(dev);
1489 unit = device_get_unit(dev);
1490 bzero(sc, sizeof(struct ti_softc));
1491
1492 /*
1493 * Map control/status registers.
1494 */
1495 command = pci_read_config(dev, PCIR_COMMAND, 4);
1496 command |= (PCIM_CMD_MEMEN|PCIM_CMD_BUSMASTEREN);
1497 pci_write_config(dev, PCIR_COMMAND, command, 4);
1498 command = pci_read_config(dev, PCIR_COMMAND, 4);
1499
1500 if (!(command & PCIM_CMD_MEMEN)) {
1501 printf("ti%d: failed to enable memory mapping!\n", unit);
1502 error = ENXIO;
1503 goto fail;
1504 }
1505
1506 rid = TI_PCI_LOMEM;
1507 sc->ti_res = bus_alloc_resource(dev, SYS_RES_MEMORY, &rid,
1508 0, ~0, 1, RF_ACTIVE);
1509
1510 if (sc->ti_res == NULL) {
1511 printf ("ti%d: couldn't map memory\n", unit);
1512 error = ENXIO;
1513 goto fail;
1514 }
1515
1516 sc->ti_btag = rman_get_bustag(sc->ti_res);
1517 sc->ti_bhandle = rman_get_bushandle(sc->ti_res);
1518 sc->ti_vhandle = (vm_offset_t)rman_get_virtual(sc->ti_res);
1519
1520 /*
1521 * XXX FIXME: rman_get_virtual() on the alpha is currently
1522 * broken and returns a physical address instead of a kernel
1523 * virtual address. Consequently, we need to do a little
1524 * extra mangling of the vhandle on the alpha. This should
1525 * eventually be fixed! The whole idea here is to get rid
1526 * of platform dependencies.
1527 */
1528#ifdef __alpha__
1529 if (pci_cvt_to_bwx(sc->ti_vhandle))
1530 sc->ti_vhandle = pci_cvt_to_bwx(sc->ti_vhandle);
1531 else
1532 sc->ti_vhandle = pci_cvt_to_dense(sc->ti_vhandle);
1533 sc->ti_vhandle = ALPHA_PHYS_TO_K0SEG(sc->ti_vhandle);
1534#endif
1535
1536 /* Allocate interrupt */
1537 rid = 0;
1538
1539 sc->ti_irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
1540 RF_SHAREABLE | RF_ACTIVE);
1541
1542 if (sc->ti_irq == NULL) {
1543 printf("ti%d: couldn't map interrupt\n", unit);
1544 error = ENXIO;
1545 goto fail;
1546 }
1547
1548 error = bus_setup_intr(dev, sc->ti_irq, INTR_TYPE_NET,
1549 ti_intr, sc, &sc->ti_intrhand);
1550
1551 if (error) {
1552 bus_release_resource(dev, SYS_RES_IRQ, 0, sc->ti_irq);
1553 bus_release_resource(dev, SYS_RES_MEMORY,
1554 TI_PCI_LOMEM, sc->ti_res);
1555 printf("ti%d: couldn't set up irq\n", unit);
1556 goto fail;
1557 }
1558
1559 sc->ti_unit = unit;
1560
1561 if (ti_chipinit(sc)) {
1562 printf("ti%d: chip initialization failed\n", sc->ti_unit);
1563 bus_teardown_intr(dev, sc->ti_irq, sc->ti_intrhand);
1564 bus_release_resource(dev, SYS_RES_IRQ, 0, sc->ti_irq);
1565 bus_release_resource(dev, SYS_RES_MEMORY,
1566 TI_PCI_LOMEM, sc->ti_res);
1567 error = ENXIO;
1568 goto fail;
1569 }
1570
1571 /* Zero out the NIC's on-board SRAM. */
1572 ti_mem(sc, 0x2000, 0x100000 - 0x2000, NULL);
1573
1574 /* Init again -- zeroing memory may have clobbered some registers. */
1575 if (ti_chipinit(sc)) {
1576 printf("ti%d: chip initialization failed\n", sc->ti_unit);
1577 bus_teardown_intr(dev, sc->ti_irq, sc->ti_intrhand);
1578 bus_release_resource(dev, SYS_RES_IRQ, 0, sc->ti_irq);
1579 bus_release_resource(dev, SYS_RES_MEMORY,
1580 TI_PCI_LOMEM, sc->ti_res);
1581 error = ENXIO;
1582 goto fail;
1583 }
1584
1585 /*
1586 * Get station address from the EEPROM. Note: the manual states
1587 * that the MAC address is at offset 0x8c, however the data is
1588 * stored as two longwords (since that's how it's loaded into
1589 * the NIC). This means the MAC address is actually preceeded
1590 * by two zero bytes. We need to skip over those.
1591 */
1592 if (ti_read_eeprom(sc, (caddr_t)&sc->arpcom.ac_enaddr,
1593 TI_EE_MAC_OFFSET + 2, ETHER_ADDR_LEN)) {
1594 printf("ti%d: failed to read station address\n", unit);
1595 bus_teardown_intr(dev, sc->ti_irq, sc->ti_intrhand);
1596 bus_release_resource(dev, SYS_RES_IRQ, 0, sc->ti_irq);
1597 bus_release_resource(dev, SYS_RES_MEMORY,
1598 TI_PCI_LOMEM, sc->ti_res);
1599 error = ENXIO;
1600 goto fail;
1601 }
1602
1603 /*
1604 * A Tigon chip was detected. Inform the world.
1605 */
1606 printf("ti%d: Ethernet address: %6D\n", unit,
1607 sc->arpcom.ac_enaddr, ":");
1608
1609 /* Allocate the general information block and ring buffers. */
1610 sc->ti_rdata = contigmalloc(sizeof(struct ti_ring_data), M_DEVBUF,
1611 M_NOWAIT, 0, 0xffffffff, PAGE_SIZE, 0);
1612
1613 if (sc->ti_rdata == NULL) {
1614 bus_teardown_intr(dev, sc->ti_irq, sc->ti_intrhand);
1615 bus_release_resource(dev, SYS_RES_IRQ, 0, sc->ti_irq);
1616 bus_release_resource(dev, SYS_RES_MEMORY,
1617 TI_PCI_LOMEM, sc->ti_res);
1618 error = ENXIO;
1619 printf("ti%d: no memory for list buffers!\n", sc->ti_unit);
1620 goto fail;
1621 }
1622
1623 bzero(sc->ti_rdata, sizeof(struct ti_ring_data));
1624
1625 /* Try to allocate memory for jumbo buffers. */
1626 if (ti_alloc_jumbo_mem(sc)) {
1627 printf("ti%d: jumbo buffer allocation failed\n", sc->ti_unit);
1628 bus_teardown_intr(dev, sc->ti_irq, sc->ti_intrhand);
1629 bus_release_resource(dev, SYS_RES_IRQ, 0, sc->ti_irq);
1630 bus_release_resource(dev, SYS_RES_MEMORY,
1631 TI_PCI_LOMEM, sc->ti_res);
1632 contigfree(sc->ti_rdata, sizeof(struct ti_ring_data),
1633 M_DEVBUF);
1634 error = ENXIO;
1635 goto fail;
1636 }
1637
1638 /*
1639 * We really need a better way to tell a 1000baseTX card
1640 * from a 1000baseSX one, since in theory there could be
1641 * OEMed 1000baseTX cards from lame vendors who aren't
1642 * clever enough to change the PCI ID. For the moment
1643 * though, the AceNIC is the only copper card available.
1644 */
1645 if (pci_get_vendor(dev) == ALT_VENDORID &&
1646 pci_get_device(dev) == ALT_DEVICEID_ACENIC_COPPER)
1647 sc->ti_copper = 1;
1648 /* Ok, it's not the only copper card available. */
1649 if (pci_get_vendor(dev) == NG_VENDORID &&
1650 pci_get_device(dev) == NG_DEVICEID_GA620T)
1651 sc->ti_copper = 1;
1652
1653 /* Set default tuneable values. */
1654 sc->ti_stat_ticks = 2 * TI_TICKS_PER_SEC;
1655 sc->ti_rx_coal_ticks = TI_TICKS_PER_SEC / 5000;
1656 sc->ti_tx_coal_ticks = TI_TICKS_PER_SEC / 500;
1657 sc->ti_rx_max_coal_bds = 64;
1658 sc->ti_tx_max_coal_bds = 128;
1659 sc->ti_tx_buf_ratio = 21;
1660
1661 /* Set up ifnet structure */
1662 ifp = &sc->arpcom.ac_if;
1663 ifp->if_softc = sc;
1664 ifp->if_unit = sc->ti_unit;
1665 ifp->if_name = "ti";
1666 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
1667 ifp->if_ioctl = ti_ioctl;
1668 ifp->if_output = ether_output;
1669 ifp->if_start = ti_start;
1670 ifp->if_watchdog = ti_watchdog;
1671 ifp->if_init = ti_init;
1672 ifp->if_mtu = ETHERMTU;
1673 ifp->if_snd.ifq_maxlen = TI_TX_RING_CNT - 1;
1674
1675 /* Set up ifmedia support. */
1676 ifmedia_init(&sc->ifmedia, IFM_IMASK, ti_ifmedia_upd, ti_ifmedia_sts);
1677 if (sc->ti_copper) {
1678 /*
1679 * Copper cards allow manual 10/100 mode selection,
1680 * but not manual 1000baseTX mode selection. Why?
1681 * Becuase currently there's no way to specify the
1682 * master/slave setting through the firmware interface,
1683 * so Alteon decided to just bag it and handle it
1684 * via autonegotiation.
1685 */
1686 ifmedia_add(&sc->ifmedia, IFM_ETHER|IFM_10_T, 0, NULL);
1687 ifmedia_add(&sc->ifmedia,
1688 IFM_ETHER|IFM_10_T|IFM_FDX, 0, NULL);
1689 ifmedia_add(&sc->ifmedia, IFM_ETHER|IFM_100_TX, 0, NULL);
1690 ifmedia_add(&sc->ifmedia,
1691 IFM_ETHER|IFM_100_TX|IFM_FDX, 0, NULL);
1692 ifmedia_add(&sc->ifmedia, IFM_ETHER|IFM_1000_TX, 0, NULL);
1693 ifmedia_add(&sc->ifmedia,
1694 IFM_ETHER|IFM_1000_TX|IFM_FDX, 0, NULL);
1695 } else {
1696 /* Fiber cards don't support 10/100 modes. */
1697 ifmedia_add(&sc->ifmedia, IFM_ETHER|IFM_1000_SX, 0, NULL);
1698 ifmedia_add(&sc->ifmedia,
1699 IFM_ETHER|IFM_1000_SX|IFM_FDX, 0, NULL);
1700 }
1701 ifmedia_add(&sc->ifmedia, IFM_ETHER|IFM_AUTO, 0, NULL);
1702 ifmedia_set(&sc->ifmedia, IFM_ETHER|IFM_AUTO);
1703
1704 /*
1705 * Call MI attach routine.
1706 */
1707 ether_ifattach(ifp, ETHER_BPF_SUPPORTED);
1708
1709fail:
1710 splx(s);
1711
1712 return(error);
1713}
1714
1715static int ti_detach(dev)
1716 device_t dev;
1717{
1718 struct ti_softc *sc;
1719 struct ifnet *ifp;
1720 int s;
1721
1722 s = splimp();
1723
1724 sc = device_get_softc(dev);
1725 ifp = &sc->arpcom.ac_if;
1726
1727 ether_ifdetach(ifp, ETHER_BPF_SUPPORTED);
1728 ti_stop(sc);
1729
1730 bus_teardown_intr(dev, sc->ti_irq, sc->ti_intrhand);
1731 bus_release_resource(dev, SYS_RES_IRQ, 0, sc->ti_irq);
1732 bus_release_resource(dev, SYS_RES_MEMORY, TI_PCI_LOMEM, sc->ti_res);
1733
1734 contigfree(sc->ti_cdata.ti_jumbo_buf, TI_JMEM, M_DEVBUF);
1735 contigfree(sc->ti_rdata, sizeof(struct ti_ring_data), M_DEVBUF);
1736 ifmedia_removeall(&sc->ifmedia);
1737
1738 splx(s);
1739
1740 return(0);
1741}
1742
1743/*
1744 * Frame reception handling. This is called if there's a frame
1745 * on the receive return list.
1746 *
1747 * Note: we have to be able to handle three possibilities here:
1748 * 1) the frame is from the mini receive ring (can only happen)
1749 * on Tigon 2 boards)
1750 * 2) the frame is from the jumbo recieve ring
1751 * 3) the frame is from the standard receive ring
1752 */
1753
1754static void ti_rxeof(sc)
1755 struct ti_softc *sc;
1756{
1757 struct ifnet *ifp;
1758 struct ti_cmd_desc cmd;
1759
1760 ifp = &sc->arpcom.ac_if;
1761
1762 while(sc->ti_rx_saved_considx != sc->ti_return_prodidx.ti_idx) {
1763 struct ti_rx_desc *cur_rx;
1764 u_int32_t rxidx;
1765 struct ether_header *eh;
1766 struct mbuf *m = NULL;
1767#if NVLAN > 0
1768 u_int16_t vlan_tag = 0;
1769 int have_tag = 0;
1770#endif
1771
1772 cur_rx =
1773 &sc->ti_rdata->ti_rx_return_ring[sc->ti_rx_saved_considx];
1774 rxidx = cur_rx->ti_idx;
1775 TI_INC(sc->ti_rx_saved_considx, TI_RETURN_RING_CNT);
1776
1777#if NVLAN > 0
1778 if (cur_rx->ti_flags & TI_BDFLAG_VLAN_TAG) {
1779 have_tag = 1;
1780 vlan_tag = cur_rx->ti_vlan_tag;
1781 }
1782#endif
1783
1784 if (cur_rx->ti_flags & TI_BDFLAG_JUMBO_RING) {
1785 TI_INC(sc->ti_jumbo, TI_JUMBO_RX_RING_CNT);
1786 m = sc->ti_cdata.ti_rx_jumbo_chain[rxidx];
1787 sc->ti_cdata.ti_rx_jumbo_chain[rxidx] = NULL;
1788 if (cur_rx->ti_flags & TI_BDFLAG_ERROR) {
1789 ifp->if_ierrors++;
1790 ti_newbuf_jumbo(sc, sc->ti_jumbo, m);
1791 continue;
1792 }
1793 if (ti_newbuf_jumbo(sc, sc->ti_jumbo, NULL) == ENOBUFS) {
1794 ifp->if_ierrors++;
1795 ti_newbuf_jumbo(sc, sc->ti_jumbo, m);
1796 continue;
1797 }
1798 } else if (cur_rx->ti_flags & TI_BDFLAG_MINI_RING) {
1799 TI_INC(sc->ti_mini, TI_MINI_RX_RING_CNT);
1800 m = sc->ti_cdata.ti_rx_mini_chain[rxidx];
1801 sc->ti_cdata.ti_rx_mini_chain[rxidx] = NULL;
1802 if (cur_rx->ti_flags & TI_BDFLAG_ERROR) {
1803 ifp->if_ierrors++;
1804 ti_newbuf_mini(sc, sc->ti_mini, m);
1805 continue;
1806 }
1807 if (ti_newbuf_mini(sc, sc->ti_mini, NULL) == ENOBUFS) {
1808 ifp->if_ierrors++;
1809 ti_newbuf_mini(sc, sc->ti_mini, m);
1810 continue;
1811 }
1812 } else {
1813 TI_INC(sc->ti_std, TI_STD_RX_RING_CNT);
1814 m = sc->ti_cdata.ti_rx_std_chain[rxidx];
1815 sc->ti_cdata.ti_rx_std_chain[rxidx] = NULL;
1816 if (cur_rx->ti_flags & TI_BDFLAG_ERROR) {
1817 ifp->if_ierrors++;
1818 ti_newbuf_std(sc, sc->ti_std, m);
1819 continue;
1820 }
1821 if (ti_newbuf_std(sc, sc->ti_std, NULL) == ENOBUFS) {
1822 ifp->if_ierrors++;
1823 ti_newbuf_std(sc, sc->ti_std, m);
1824 continue;
1825 }
1826 }
1827
1828 m->m_pkthdr.len = m->m_len = cur_rx->ti_len;
1829 ifp->if_ipackets++;
1830 eh = mtod(m, struct ether_header *);
1831 m->m_pkthdr.rcvif = ifp;
1832
1833 /* Remove header from mbuf and pass it on. */
1834 m_adj(m, sizeof(struct ether_header));
1835
1836 if (ifp->if_hwassist) {
1837 m->m_pkthdr.csum_flags |= CSUM_IP_CHECKED |
1838 CSUM_DATA_VALID;
1839 if ((cur_rx->ti_ip_cksum ^ 0xffff) == 0)
1840 m->m_pkthdr.csum_flags |= CSUM_IP_VALID;
1841 m->m_pkthdr.csum_data = cur_rx->ti_tcp_udp_cksum;
1842 }
1843
1844#if NVLAN > 0
1845 /*
1846 * If we received a packet with a vlan tag, pass it
1847 * to vlan_input() instead of ether_input().
1848 */
1849 if (have_tag) {
1850 vlan_input_tag(eh, m, vlan_tag);
1851 have_tag = vlan_tag = 0;
1852 continue;
1853 }
1854#endif
1855 ether_input(ifp, eh, m);
1856 }
1857
1858 /* Only necessary on the Tigon 1. */
1859 if (sc->ti_hwrev == TI_HWREV_TIGON)
1860 CSR_WRITE_4(sc, TI_GCR_RXRETURNCONS_IDX,
1861 sc->ti_rx_saved_considx);
1862
1863 TI_UPDATE_STDPROD(sc, sc->ti_std);
1864 TI_UPDATE_MINIPROD(sc, sc->ti_mini);
1865 TI_UPDATE_JUMBOPROD(sc, sc->ti_jumbo);
1866
1867 return;
1868}
1869
1870static void ti_txeof(sc)
1871 struct ti_softc *sc;
1872{
1873 struct ti_tx_desc *cur_tx = NULL;
1874 struct ifnet *ifp;
1875
1876 ifp = &sc->arpcom.ac_if;
1877
1878 /*
1879 * Go through our tx ring and free mbufs for those
1880 * frames that have been sent.
1881 */
1882 while (sc->ti_tx_saved_considx != sc->ti_tx_considx.ti_idx) {
1883 u_int32_t idx = 0;
1884
1885 idx = sc->ti_tx_saved_considx;
1886 if (sc->ti_hwrev == TI_HWREV_TIGON) {
1887 if (idx > 383)
1888 CSR_WRITE_4(sc, TI_WINBASE,
1889 TI_TX_RING_BASE + 6144);
1890 else if (idx > 255)
1891 CSR_WRITE_4(sc, TI_WINBASE,
1892 TI_TX_RING_BASE + 4096);
1893 else if (idx > 127)
1894 CSR_WRITE_4(sc, TI_WINBASE,
1895 TI_TX_RING_BASE + 2048);
1896 else
1897 CSR_WRITE_4(sc, TI_WINBASE,
1898 TI_TX_RING_BASE);
1899 cur_tx = &sc->ti_rdata->ti_tx_ring_nic[idx % 128];
1900 } else
1901 cur_tx = &sc->ti_rdata->ti_tx_ring[idx];
1902 if (cur_tx->ti_flags & TI_BDFLAG_END)
1903 ifp->if_opackets++;
1904 if (sc->ti_cdata.ti_tx_chain[idx] != NULL) {
1905 m_freem(sc->ti_cdata.ti_tx_chain[idx]);
1906 sc->ti_cdata.ti_tx_chain[idx] = NULL;
1907 }
1908 sc->ti_txcnt--;
1909 TI_INC(sc->ti_tx_saved_considx, TI_TX_RING_CNT);
1910 ifp->if_timer = 0;
1911 }
1912
1913 if (cur_tx != NULL)
1914 ifp->if_flags &= ~IFF_OACTIVE;
1915
1916 return;
1917}
1918
1919static void ti_intr(xsc)
1920 void *xsc;
1921{
1922 struct ti_softc *sc;
1923 struct ifnet *ifp;
1924
1925 sc = xsc;
1926 ifp = &sc->arpcom.ac_if;
1927
1928#ifdef notdef
1929 /* Avoid this for now -- checking this register is expensive. */
1930 /* Make sure this is really our interrupt. */
1931 if (!(CSR_READ_4(sc, TI_MISC_HOST_CTL) & TI_MHC_INTSTATE))
1932 return;
1933#endif
1934
1935 /* Ack interrupt and stop others from occuring. */
1936 CSR_WRITE_4(sc, TI_MB_HOSTINTR, 1);
1937
1938 if (ifp->if_flags & IFF_RUNNING) {
1939 /* Check RX return ring producer/consumer */
1940 ti_rxeof(sc);
1941
1942 /* Check TX ring producer/consumer */
1943 ti_txeof(sc);
1944 }
1945
1946 ti_handle_events(sc);
1947
1948 /* Re-enable interrupts. */
1949 CSR_WRITE_4(sc, TI_MB_HOSTINTR, 0);
1950
1951 if (ifp->if_flags & IFF_RUNNING && ifp->if_snd.ifq_head != NULL)
1952 ti_start(ifp);
1953
1954 return;
1955}
1956
1957static void ti_stats_update(sc)
1958 struct ti_softc *sc;
1959{
1960 struct ifnet *ifp;
1961
1962 ifp = &sc->arpcom.ac_if;
1963
1964 ifp->if_collisions +=
1965 (sc->ti_rdata->ti_info.ti_stats.dot3StatsSingleCollisionFrames +
1966 sc->ti_rdata->ti_info.ti_stats.dot3StatsMultipleCollisionFrames +
1967 sc->ti_rdata->ti_info.ti_stats.dot3StatsExcessiveCollisions +
1968 sc->ti_rdata->ti_info.ti_stats.dot3StatsLateCollisions) -
1969 ifp->if_collisions;
1970
1971 return;
1972}
1973
1974/*
1975 * Encapsulate an mbuf chain in the tx ring by coupling the mbuf data
1976 * pointers to descriptors.
1977 */
1978static int ti_encap(sc, m_head, txidx)
1979 struct ti_softc *sc;
1980 struct mbuf *m_head;
1981 u_int32_t *txidx;
1982{
1983 struct ti_tx_desc *f = NULL;
1984 struct mbuf *m;
1985 u_int32_t frag, cur, cnt = 0;
1986 u_int16_t csum_flags = 0;
1987#if NVLAN > 0
1988 struct ifvlan *ifv = NULL;
1989
1990 if ((m_head->m_flags & (M_PROTO1|M_PKTHDR)) == (M_PROTO1|M_PKTHDR) &&
1991 m_head->m_pkthdr.rcvif != NULL &&
1992 m_head->m_pkthdr.rcvif->if_type == IFT_8021_VLAN)
1993 ifv = m_head->m_pkthdr.rcvif->if_softc;
1994#endif
1995
1996 m = m_head;
1997 cur = frag = *txidx;
1998
1999 if (m_head->m_pkthdr.csum_flags) {
2000 if (m_head->m_pkthdr.csum_flags & CSUM_IP)
2001 csum_flags |= TI_BDFLAG_IP_CKSUM;
2002 if (m_head->m_pkthdr.csum_flags & (CSUM_TCP | CSUM_UDP))
2003 csum_flags |= TI_BDFLAG_TCP_UDP_CKSUM;
2004 if (m_head->m_flags & M_LASTFRAG)
2005 csum_flags |= TI_BDFLAG_IP_FRAG_END;
2006 else if (m_head->m_flags & M_FRAG)
2007 csum_flags |= TI_BDFLAG_IP_FRAG;
2008 }
2009 /*
2010 * Start packing the mbufs in this chain into
2011 * the fragment pointers. Stop when we run out
2012 * of fragments or hit the end of the mbuf chain.
2013 */
2014 for (m = m_head; m != NULL; m = m->m_next) {
2015 if (m->m_len != 0) {
2016 if (sc->ti_hwrev == TI_HWREV_TIGON) {
2017 if (frag > 383)
2018 CSR_WRITE_4(sc, TI_WINBASE,
2019 TI_TX_RING_BASE + 6144);
2020 else if (frag > 255)
2021 CSR_WRITE_4(sc, TI_WINBASE,
2022 TI_TX_RING_BASE + 4096);
2023 else if (frag > 127)
2024 CSR_WRITE_4(sc, TI_WINBASE,
2025 TI_TX_RING_BASE + 2048);
2026 else
2027 CSR_WRITE_4(sc, TI_WINBASE,
2028 TI_TX_RING_BASE);
2029 f = &sc->ti_rdata->ti_tx_ring_nic[frag % 128];
2030 } else
2031 f = &sc->ti_rdata->ti_tx_ring[frag];
2032 if (sc->ti_cdata.ti_tx_chain[frag] != NULL)
2033 break;
2034 TI_HOSTADDR(f->ti_addr) = vtophys(mtod(m, vm_offset_t));
2035 f->ti_len = m->m_len;
2036 f->ti_flags = csum_flags;
2037#if NVLAN > 0
2038 if (ifv != NULL) {
2039 f->ti_flags |= TI_BDFLAG_VLAN_TAG;
2040 f->ti_vlan_tag = ifv->ifv_tag;
2041 } else {
2042 f->ti_vlan_tag = 0;
2043 }
2044#endif
2045 /*
2046 * Sanity check: avoid coming within 16 descriptors
2047 * of the end of the ring.
2048 */
2049 if ((TI_TX_RING_CNT - (sc->ti_txcnt + cnt)) < 16)
2050 return(ENOBUFS);
2051 cur = frag;
2052 TI_INC(frag, TI_TX_RING_CNT);
2053 cnt++;
2054 }
2055 }
2056
2057 if (m != NULL)
2058 return(ENOBUFS);
2059
2060 if (frag == sc->ti_tx_saved_considx)
2061 return(ENOBUFS);
2062
2063 if (sc->ti_hwrev == TI_HWREV_TIGON)
2064 sc->ti_rdata->ti_tx_ring_nic[cur % 128].ti_flags |=
2065 TI_BDFLAG_END;
2066 else
2067 sc->ti_rdata->ti_tx_ring[cur].ti_flags |= TI_BDFLAG_END;
2068 sc->ti_cdata.ti_tx_chain[cur] = m_head;
2069 sc->ti_txcnt += cnt;
2070
2071 *txidx = frag;
2072
2073 return(0);
2074}
2075
2076/*
2077 * Main transmit routine. To avoid having to do mbuf copies, we put pointers
2078 * to the mbuf data regions directly in the transmit descriptors.
2079 */
2080static void ti_start(ifp)
2081 struct ifnet *ifp;
2082{
2083 struct ti_softc *sc;
2084 struct mbuf *m_head = NULL;
2085 u_int32_t prodidx = 0;
2086
2087 sc = ifp->if_softc;
2088
2089 prodidx = CSR_READ_4(sc, TI_MB_SENDPROD_IDX);
2090
2091 while(sc->ti_cdata.ti_tx_chain[prodidx] == NULL) {
2092 IF_DEQUEUE(&ifp->if_snd, m_head);
2093 if (m_head == NULL)
2094 break;
2095
2096 /*
2097 * XXX
2098 * safety overkill. If this is a fragmented packet chain
2099 * with delayed TCP/UDP checksums, then only encapsulate
2100 * it if we have enough descriptors to handle the entire
2101 * chain at once.
2102 * (paranoia -- may not actually be needed)
2103 */
2104 if (m_head->m_flags & M_FIRSTFRAG &&
2105 m_head->m_pkthdr.csum_flags & (CSUM_DELAY_DATA)) {
2106 if ((TI_TX_RING_CNT - sc->ti_txcnt) <
2107 m_head->m_pkthdr.csum_data + 16) {
2108 IF_PREPEND(&ifp->if_snd, m_head);
2109 ifp->if_flags |= IFF_OACTIVE;
2110 break;
2111 }
2112 }
2113
2114 /*
2115 * Pack the data into the transmit ring. If we
2116 * don't have room, set the OACTIVE flag and wait
2117 * for the NIC to drain the ring.
2118 */
2119 if (ti_encap(sc, m_head, &prodidx)) {
2120 IF_PREPEND(&ifp->if_snd, m_head);
2121 ifp->if_flags |= IFF_OACTIVE;
2122 break;
2123 }
2124
2125 /*
2126 * If there's a BPF listener, bounce a copy of this frame
2127 * to him.
2128 */
2129 if (ifp->if_bpf)
2130 bpf_mtap(ifp, m_head);
2131 }
2132
2133 /* Transmit */
2134 CSR_WRITE_4(sc, TI_MB_SENDPROD_IDX, prodidx);
2135
2136 /*
2137 * Set a timeout in case the chip goes out to lunch.
2138 */
2139 ifp->if_timer = 5;
2140
2141 return;
2142}
2143
2144static void ti_init(xsc)
2145 void *xsc;
2146{
2147 struct ti_softc *sc = xsc;
2148 int s;
2149
2150 s = splimp();
2151
2152 /* Cancel pending I/O and flush buffers. */
2153 ti_stop(sc);
2154
2155 /* Init the gen info block, ring control blocks and firmware. */
2156 if (ti_gibinit(sc)) {
2157 printf("ti%d: initialization failure\n", sc->ti_unit);
2158 splx(s);
2159 return;
2160 }
2161
2162 splx(s);
2163
2164 return;
2165}
2166
2167static void ti_init2(sc)
2168 struct ti_softc *sc;
2169{
2170 struct ti_cmd_desc cmd;
2171 struct ifnet *ifp;
2172 u_int16_t *m;
2173 struct ifmedia *ifm;
2174 int tmp;
2175
2176 ifp = &sc->arpcom.ac_if;
2177
2178 /* Specify MTU and interface index. */
2179 CSR_WRITE_4(sc, TI_GCR_IFINDEX, ifp->if_unit);
2180 CSR_WRITE_4(sc, TI_GCR_IFMTU, ifp->if_mtu +
2181 ETHER_HDR_LEN + ETHER_CRC_LEN);
2182 TI_DO_CMD(TI_CMD_UPDATE_GENCOM, 0, 0);
2183
2184 /* Load our MAC address. */
2185 m = (u_int16_t *)&sc->arpcom.ac_enaddr[0];
2186 CSR_WRITE_4(sc, TI_GCR_PAR0, htons(m[0]));
2187 CSR_WRITE_4(sc, TI_GCR_PAR1, (htons(m[1]) << 16) | htons(m[2]));
2188 TI_DO_CMD(TI_CMD_SET_MAC_ADDR, 0, 0);
2189
2190 /* Enable or disable promiscuous mode as needed. */
2191 if (ifp->if_flags & IFF_PROMISC) {
2192 TI_DO_CMD(TI_CMD_SET_PROMISC_MODE, TI_CMD_CODE_PROMISC_ENB, 0);
2193 } else {
2194 TI_DO_CMD(TI_CMD_SET_PROMISC_MODE, TI_CMD_CODE_PROMISC_DIS, 0);
2195 }
2196
2197 /* Program multicast filter. */
2198 ti_setmulti(sc);
2199
2200 /*
2201 * If this is a Tigon 1, we should tell the
2202 * firmware to use software packet filtering.
2203 */
2204 if (sc->ti_hwrev == TI_HWREV_TIGON) {
2205 TI_DO_CMD(TI_CMD_FDR_FILTERING, TI_CMD_CODE_FILT_ENB, 0);
2206 }
2207
2208 /* Init RX ring. */
2209 ti_init_rx_ring_std(sc);
2210
2211 /* Init jumbo RX ring. */
2212 if (ifp->if_mtu > (ETHERMTU + ETHER_HDR_LEN + ETHER_CRC_LEN))
2213 ti_init_rx_ring_jumbo(sc);
2214
2215 /*
2216 * If this is a Tigon 2, we can also configure the
2217 * mini ring.
2218 */
2219 if (sc->ti_hwrev == TI_HWREV_TIGON_II)
2220 ti_init_rx_ring_mini(sc);
2221
2222 CSR_WRITE_4(sc, TI_GCR_RXRETURNCONS_IDX, 0);
2223 sc->ti_rx_saved_considx = 0;
2224
2225 /* Init TX ring. */
2226 ti_init_tx_ring(sc);
2227
2228 /* Tell firmware we're alive. */
2229 TI_DO_CMD(TI_CMD_HOST_STATE, TI_CMD_CODE_STACK_UP, 0);
2230
2231 /* Enable host interrupts. */
2232 CSR_WRITE_4(sc, TI_MB_HOSTINTR, 0);
2233
2234 ifp->if_flags |= IFF_RUNNING;
2235 ifp->if_flags &= ~IFF_OACTIVE;
2236
2237 /*
2238 * Make sure to set media properly. We have to do this
2239 * here since we have to issue commands in order to set
2240 * the link negotiation and we can't issue commands until
2241 * the firmware is running.
2242 */
2243 ifm = &sc->ifmedia;
2244 tmp = ifm->ifm_media;
2245 ifm->ifm_media = ifm->ifm_cur->ifm_media;
2246 ti_ifmedia_upd(ifp);
2247 ifm->ifm_media = tmp;
2248
2249 return;
2250}
2251
2252/*
2253 * Set media options.
2254 */
2255static int ti_ifmedia_upd(ifp)
2256 struct ifnet *ifp;
2257{
2258 struct ti_softc *sc;
2259 struct ifmedia *ifm;
2260 struct ti_cmd_desc cmd;
2261
2262 sc = ifp->if_softc;
2263 ifm = &sc->ifmedia;
2264
2265 if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
2266 return(EINVAL);
2267
2268 switch(IFM_SUBTYPE(ifm->ifm_media)) {
2269 case IFM_AUTO:
2270 CSR_WRITE_4(sc, TI_GCR_GLINK, TI_GLNK_PREF|TI_GLNK_1000MB|
2271 TI_GLNK_FULL_DUPLEX|TI_GLNK_RX_FLOWCTL_Y|
2272 TI_GLNK_AUTONEGENB|TI_GLNK_ENB);
2273 CSR_WRITE_4(sc, TI_GCR_LINK, TI_LNK_100MB|TI_LNK_10MB|
2274 TI_LNK_FULL_DUPLEX|TI_LNK_HALF_DUPLEX|
2275 TI_LNK_AUTONEGENB|TI_LNK_ENB);
2276 TI_DO_CMD(TI_CMD_LINK_NEGOTIATION,
2277 TI_CMD_CODE_NEGOTIATE_BOTH, 0);
2278 break;
2279 case IFM_1000_SX:
2280 case IFM_1000_TX:
2281 CSR_WRITE_4(sc, TI_GCR_GLINK, TI_GLNK_PREF|TI_GLNK_1000MB|
2282 TI_GLNK_RX_FLOWCTL_Y|TI_GLNK_ENB);
2283 CSR_WRITE_4(sc, TI_GCR_LINK, 0);
2284 if ((ifm->ifm_media & IFM_GMASK) == IFM_FDX) {
2285 TI_SETBIT(sc, TI_GCR_GLINK, TI_GLNK_FULL_DUPLEX);
2286 }
2287 TI_DO_CMD(TI_CMD_LINK_NEGOTIATION,
2288 TI_CMD_CODE_NEGOTIATE_GIGABIT, 0);
2289 break;
2290 case IFM_100_FX:
2291 case IFM_10_FL:
2292 case IFM_100_TX:
2293 case IFM_10_T:
2294 CSR_WRITE_4(sc, TI_GCR_GLINK, 0);
2295 CSR_WRITE_4(sc, TI_GCR_LINK, TI_LNK_ENB|TI_LNK_PREF);
2296 if (IFM_SUBTYPE(ifm->ifm_media) == IFM_100_FX ||
2297 IFM_SUBTYPE(ifm->ifm_media) == IFM_100_TX) {
2298 TI_SETBIT(sc, TI_GCR_LINK, TI_LNK_100MB);
2299 } else {
2300 TI_SETBIT(sc, TI_GCR_LINK, TI_LNK_10MB);
2301 }
2302 if ((ifm->ifm_media & IFM_GMASK) == IFM_FDX) {
2303 TI_SETBIT(sc, TI_GCR_LINK, TI_LNK_FULL_DUPLEX);
2304 } else {
2305 TI_SETBIT(sc, TI_GCR_LINK, TI_LNK_HALF_DUPLEX);
2306 }
2307 TI_DO_CMD(TI_CMD_LINK_NEGOTIATION,
2308 TI_CMD_CODE_NEGOTIATE_10_100, 0);
2309 break;
2310 }
2311
2312 return(0);
2313}
2314
2315/*
2316 * Report current media status.
2317 */
2318static void ti_ifmedia_sts(ifp, ifmr)
2319 struct ifnet *ifp;
2320 struct ifmediareq *ifmr;
2321{
2322 struct ti_softc *sc;
2323 u_int32_t media = 0;
2324
2325 sc = ifp->if_softc;
2326
2327 ifmr->ifm_status = IFM_AVALID;
2328 ifmr->ifm_active = IFM_ETHER;
2329
2330 if (sc->ti_linkstat == TI_EV_CODE_LINK_DOWN)
2331 return;
2332
2333 ifmr->ifm_status |= IFM_ACTIVE;
2334
2335 if (sc->ti_linkstat == TI_EV_CODE_GIG_LINK_UP) {
2336 media = CSR_READ_4(sc, TI_GCR_GLINK_STAT);
2337 if (sc->ti_copper)
2338 ifmr->ifm_active |= IFM_1000_TX;
2339 else
2340 ifmr->ifm_active |= IFM_1000_SX;
2341 if (media & TI_GLNK_FULL_DUPLEX)
2342 ifmr->ifm_active |= IFM_FDX;
2343 else
2344 ifmr->ifm_active |= IFM_HDX;
2345 } else if (sc->ti_linkstat == TI_EV_CODE_LINK_UP) {
2346 media = CSR_READ_4(sc, TI_GCR_LINK_STAT);
2347 if (sc->ti_copper) {
2348 if (media & TI_LNK_100MB)
2349 ifmr->ifm_active |= IFM_100_TX;
2350 if (media & TI_LNK_10MB)
2351 ifmr->ifm_active |= IFM_10_T;
2352 } else {
2353 if (media & TI_LNK_100MB)
2354 ifmr->ifm_active |= IFM_100_FX;
2355 if (media & TI_LNK_10MB)
2356 ifmr->ifm_active |= IFM_10_FL;
2357 }
2358 if (media & TI_LNK_FULL_DUPLEX)
2359 ifmr->ifm_active |= IFM_FDX;
2360 if (media & TI_LNK_HALF_DUPLEX)
2361 ifmr->ifm_active |= IFM_HDX;
2362 }
2363
2364 return;
2365}
2366
2367static int ti_ioctl(ifp, command, data)
2368 struct ifnet *ifp;
2369 u_long command;
2370 caddr_t data;
2371{
2372 struct ti_softc *sc = ifp->if_softc;
2373 struct ifreq *ifr = (struct ifreq *) data;
2374 int s, error = 0;
2375 struct ti_cmd_desc cmd;
2376
2377 s = splimp();
2378
2379 switch(command) {
2380 case SIOCSIFADDR:
2381 case SIOCGIFADDR:
2382 error = ether_ioctl(ifp, command, data);
2383 break;
2384 case SIOCSIFMTU:
2385 if (ifr->ifr_mtu > TI_JUMBO_MTU)
2386 error = EINVAL;
2387 else {
2388 ifp->if_mtu = ifr->ifr_mtu;
2389 ti_init(sc);
2390 }
2391 break;
2392 case SIOCSIFFLAGS:
2393 if (ifp->if_flags & IFF_UP) {
2394 /*
2395 * If only the state of the PROMISC flag changed,
2396 * then just use the 'set promisc mode' command
2397 * instead of reinitializing the entire NIC. Doing
2398 * a full re-init means reloading the firmware and
2399 * waiting for it to start up, which may take a
2400 * second or two.
2401 */
2402 if (ifp->if_flags & IFF_RUNNING &&
2403 ifp->if_flags & IFF_PROMISC &&
2404 !(sc->ti_if_flags & IFF_PROMISC)) {
2405 TI_DO_CMD(TI_CMD_SET_PROMISC_MODE,
2406 TI_CMD_CODE_PROMISC_ENB, 0);
2407 } else if (ifp->if_flags & IFF_RUNNING &&
2408 !(ifp->if_flags & IFF_PROMISC) &&
2409 sc->ti_if_flags & IFF_PROMISC) {
2410 TI_DO_CMD(TI_CMD_SET_PROMISC_MODE,
2411 TI_CMD_CODE_PROMISC_DIS, 0);
2412 } else
2413 ti_init(sc);
2414 } else {
2415 if (ifp->if_flags & IFF_RUNNING) {
2416 ti_stop(sc);
2417 }
2418 }
2419 sc->ti_if_flags = ifp->if_flags;
2420 error = 0;
2421 break;
2422 case SIOCADDMULTI:
2423 case SIOCDELMULTI:
2424 if (ifp->if_flags & IFF_RUNNING) {
2425 ti_setmulti(sc);
2426 error = 0;
2427 }
2428 break;
2429 case SIOCSIFMEDIA:
2430 case SIOCGIFMEDIA:
2431 error = ifmedia_ioctl(ifp, ifr, &sc->ifmedia, command);
2432 break;
2433 default:
2434 error = EINVAL;
2435 break;
2436 }
2437
2438 (void)splx(s);
2439
2440 return(error);
2441}
2442
2443static void ti_watchdog(ifp)
2444 struct ifnet *ifp;
2445{
2446 struct ti_softc *sc;
2447
2448 sc = ifp->if_softc;
2449
2450 printf("ti%d: watchdog timeout -- resetting\n", sc->ti_unit);
2451 ti_stop(sc);
2452 ti_init(sc);
2453
2454 ifp->if_oerrors++;
2455
2456 return;
2457}
2458
2459/*
2460 * Stop the adapter and free any mbufs allocated to the
2461 * RX and TX lists.
2462 */
2463static void ti_stop(sc)
2464 struct ti_softc *sc;
2465{
2466 struct ifnet *ifp;
2467 struct ti_cmd_desc cmd;
2468
2469 ifp = &sc->arpcom.ac_if;
2470
2471 /* Disable host interrupts. */
2472 CSR_WRITE_4(sc, TI_MB_HOSTINTR, 1);
2473 /*
2474 * Tell firmware we're shutting down.
2475 */
2476 TI_DO_CMD(TI_CMD_HOST_STATE, TI_CMD_CODE_STACK_DOWN, 0);
2477
2478 /* Halt and reinitialize. */
2479 ti_chipinit(sc);
2480 ti_mem(sc, 0x2000, 0x100000 - 0x2000, NULL);
2481 ti_chipinit(sc);
2482
2483 /* Free the RX lists. */
2484 ti_free_rx_ring_std(sc);
2485
2486 /* Free jumbo RX list. */
2487 ti_free_rx_ring_jumbo(sc);
2488
2489 /* Free mini RX list. */
2490 ti_free_rx_ring_mini(sc);
2491
2492 /* Free TX buffers. */
2493 ti_free_tx_ring(sc);
2494
2495 sc->ti_ev_prodidx.ti_idx = 0;
2496 sc->ti_return_prodidx.ti_idx = 0;
2497 sc->ti_tx_considx.ti_idx = 0;
2498 sc->ti_tx_saved_considx = TI_TXCONS_UNSET;
2499
2500 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
2501
2502 return;
2503}
2504
2505/*
2506 * Stop all chip I/O so that the kernel's probe routines don't
2507 * get confused by errant DMAs when rebooting.
2508 */
2509static void ti_shutdown(dev)
2510 device_t dev;
2511{
2512 struct ti_softc *sc;
2513
2514 sc = device_get_softc(dev);
2515
2516 ti_chipinit(sc);
2517
2518 return;
2519}