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