if_bge.c revision 1.4 1 /* $NetBSD: if_bge.c,v 1.4 2002/06/27 22:10:11 fvdl Exp $ */
2 /*
3 * Copyright (c) 2001 Wind River Systems
4 * Copyright (c) 1997, 1998, 1999, 2001
5 * Bill Paul <wpaul (at) windriver.com>. 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: if_bge.c,v 1.13 2002/04/04 06:01:31 wpaul Exp $
35 */
36
37 /*
38 * Broadcom BCM570x family gigabit ethernet driver for FreeBSD.
39 *
40 * Written by Bill Paul <wpaul (at) windriver.com>
41 * Senior Engineer, Wind River Systems
42 */
43
44 /*
45 * The Broadcom BCM5700 is based on technology originally developed by
46 * Alteon Networks as part of the Tigon I and Tigon II gigabit ethernet
47 * MAC chips. The BCM5700, sometimes refered to as the Tigon III, has
48 * two on-board MIPS R4000 CPUs and can have as much as 16MB of external
49 * SSRAM. The BCM5700 supports TCP, UDP and IP checksum offload, jumbo
50 * frames, highly configurable RX filtering, and 16 RX and TX queues
51 * (which, along with RX filter rules, can be used for QOS applications).
52 * Other features, such as TCP segmentation, may be available as part
53 * of value-added firmware updates. Unlike the Tigon I and Tigon II,
54 * firmware images can be stored in hardware and need not be compiled
55 * into the driver.
56 *
57 * The BCM5700 supports the PCI v2.2 and PCI-X v1.0 standards, and will
58 * function in a 32-bit/64-bit 33/66Mhz bus, or a 64-bit/133Mhz bus.
59 *
60 * The BCM5701 is a single-chip solution incorporating both the BCM5700
61 * MAC and a BCM5401 10/100/1000 PHY. Unlike the BCM5700, the BCM5700
62 * does not support external SSRAM.
63 *
64 * Broadcom also produces a variation of the BCM5700 under the "Altima"
65 * brand name, which is functionally similar but lacks PCI-X support.
66 *
67 * Without external SSRAM, you can only have at most 4 TX rings,
68 * and the use of the mini RX ring is disabled. This seems to imply
69 * that these features are simply not available on the BCM5701. As a
70 * result, this driver does not implement any support for the mini RX
71 * ring.
72 */
73
74 #include "bpfilter.h"
75 #include "vlan.h"
76
77 #include <sys/param.h>
78 #include <sys/systm.h>
79 #include <sys/callout.h>
80 #include <sys/sockio.h>
81 #include <sys/mbuf.h>
82 #include <sys/malloc.h>
83 #include <sys/kernel.h>
84 #include <sys/device.h>
85 #include <sys/socket.h>
86
87 #include <net/if.h>
88 #include <net/if_dl.h>
89 #include <net/if_media.h>
90 #include <net/if_ether.h>
91
92 #ifdef INET
93 #include <netinet/in.h>
94 #include <netinet/in_systm.h>
95 #include <netinet/in_var.h>
96 #include <netinet/ip.h>
97 #endif
98
99 #if NBPFILTER > 0
100 #include <net/bpf.h>
101 #endif
102
103 #include <dev/pci/pcireg.h>
104 #include <dev/pci/pcivar.h>
105 #include <dev/pci/pcidevs.h>
106
107 #include <dev/mii/mii.h>
108 #include <dev/mii/miivar.h>
109 #include <dev/mii/miidevs.h>
110 #include <dev/mii/brgphyreg.h>
111
112 #include <dev/pci/if_bgereg.h>
113
114 #include <uvm/uvm_extern.h>
115
116 /* #define BGE_CHECKSUM */
117
118 int bge_probe(struct device *, struct cfdata *, void *);
119 void bge_attach(struct device *, struct device *, void *);
120 void bge_release_resources(struct bge_softc *);
121 void bge_txeof(struct bge_softc *);
122 void bge_rxeof(struct bge_softc *);
123
124 void bge_tick(void *);
125 void bge_stats_update(struct bge_softc *);
126 int bge_encap(struct bge_softc *, struct mbuf *, u_int32_t *);
127
128 int bge_intr(void *);
129 void bge_start(struct ifnet *);
130 int bge_ioctl(struct ifnet *, u_long, caddr_t);
131 int bge_init(struct ifnet *);
132 void bge_stop(struct bge_softc *);
133 void bge_watchdog(struct ifnet *);
134 void bge_shutdown(void *);
135 int bge_ifmedia_upd(struct ifnet *);
136 void bge_ifmedia_sts(struct ifnet *, struct ifmediareq *);
137
138 u_int8_t bge_eeprom_getbyte(struct bge_softc *, int, u_int8_t *);
139 int bge_read_eeprom(struct bge_softc *, caddr_t, int, int);
140
141 u_int32_t bge_crc(struct bge_softc *, caddr_t);
142 void bge_setmulti(struct bge_softc *);
143
144 void bge_handle_events(struct bge_softc *);
145 int bge_alloc_jumbo_mem(struct bge_softc *);
146 void bge_free_jumbo_mem(struct bge_softc *);
147 void *bge_jalloc(struct bge_softc *);
148 void bge_jfree(struct mbuf *, caddr_t, u_int, void *);
149 int bge_newbuf_std(struct bge_softc *, int, struct mbuf *, bus_dmamap_t);
150 int bge_newbuf_jumbo(struct bge_softc *, int, struct mbuf *);
151 int bge_init_rx_ring_std(struct bge_softc *);
152 void bge_free_rx_ring_std(struct bge_softc *);
153 int bge_init_rx_ring_jumbo(struct bge_softc *);
154 void bge_free_rx_ring_jumbo(struct bge_softc *);
155 void bge_free_tx_ring(struct bge_softc *);
156 int bge_init_tx_ring(struct bge_softc *);
157
158 int bge_chipinit(struct bge_softc *);
159 int bge_blockinit(struct bge_softc *);
160
161 #ifdef notdef
162 u_int8_t bge_vpd_readbyte(struct bge_softc *, int);
163 void bge_vpd_read_res(struct bge_softc *, struct vpd_res *, int);
164 void bge_vpd_read(struct bge_softc *);
165 #endif
166
167 u_int32_t bge_readmem_ind(struct bge_softc *, int);
168 void bge_writemem_ind(struct bge_softc *, int, int);
169 #ifdef notdef
170 u_int32_t bge_readreg_ind(struct bge_softc *, int);
171 #endif
172 void bge_writereg_ind(struct bge_softc *, int, int);
173
174 int bge_miibus_readreg(struct device *, int, int);
175 void bge_miibus_writereg(struct device *, int, int, int);
176 void bge_miibus_statchg(struct device *);
177
178 void bge_reset(struct bge_softc *);
179 void bge_phy_hack(struct bge_softc *);
180
181 void bge_dump_status(struct bge_softc *);
182 void bge_dump_rxbd(struct bge_rx_bd *);
183
184 #define BGE_DEBUG
185 #ifdef BGE_DEBUG
186 #define DPRINTF(x) if (bgedebug) printf x
187 #define DPRINTFN(n,x) if (bgedebug >= (n)) printf x
188 int bgedebug = 0;
189 #else
190 #define DPRINTF(x)
191 #define DPRINTFN(n,x)
192 #endif
193
194 struct cfattach bge_ca = {
195 sizeof(struct bge_softc), bge_probe, bge_attach
196 };
197
198 u_int32_t
199 bge_readmem_ind(sc, off)
200 struct bge_softc *sc;
201 int off;
202 {
203 struct pci_attach_args *pa = &(sc->bge_pa);
204 pcireg_t val;
205
206 pci_conf_write(pa->pa_pc, pa->pa_tag, BGE_PCI_MEMWIN_BASEADDR, off);
207 val = pci_conf_read(pa->pa_pc, pa->pa_tag, BGE_PCI_MEMWIN_DATA);
208 return val;
209 }
210
211 void
212 bge_writemem_ind(sc, off, val)
213 struct bge_softc *sc;
214 int off, val;
215 {
216 struct pci_attach_args *pa = &(sc->bge_pa);
217
218 pci_conf_write(pa->pa_pc, pa->pa_tag, BGE_PCI_MEMWIN_BASEADDR, off);
219 pci_conf_write(pa->pa_pc, pa->pa_tag, BGE_PCI_MEMWIN_DATA, val);
220 }
221
222 #ifdef notdef
223 u_int32_t
224 bge_readreg_ind(sc, off)
225 struct bge_softc *sc;
226 int off;
227 {
228 struct pci_attach_args *pa = &(sc->bge_pa);
229
230 pci_conf_write(pa->pa_pc, pa->pa_tag, BGE_PCI_REG_BASEADDR, off);
231 return(pci_conf_read(pa->pa_pc, pa->pa_tag, BGE_PCI_REG_DATA));
232 }
233 #endif
234
235 void
236 bge_writereg_ind(sc, off, val)
237 struct bge_softc *sc;
238 int off, val;
239 {
240 struct pci_attach_args *pa = &(sc->bge_pa);
241
242 pci_conf_write(pa->pa_pc, pa->pa_tag, BGE_PCI_REG_BASEADDR, off);
243 pci_conf_write(pa->pa_pc, pa->pa_tag, BGE_PCI_REG_DATA, val);
244 }
245
246 #ifdef notdef
247 u_int8_t
248 bge_vpd_readbyte(sc, addr)
249 struct bge_softc *sc;
250 int addr;
251 {
252 int i;
253 u_int32_t val;
254 struct pci_attach_args *pa = &(sc->bge_pa);
255
256 pci_conf_write(pa->pa_pc, pa->pa_tag, BGE_PCI_VPD_ADDR, addr);
257 for (i = 0; i < BGE_TIMEOUT * 10; i++) {
258 DELAY(10);
259 if (pci_conf_read(pa->pa_pc, pa->pa_tag, BGE_PCI_VPD_ADDR) &
260 BGE_VPD_FLAG)
261 break;
262 }
263
264 if (i == BGE_TIMEOUT) {
265 printf("%s: VPD read timed out\n", sc->bge_dev.dv_xname);
266 return(0);
267 }
268
269 val = pci_conf_read(pa->pa_pc, pa->pa_tag, BGE_PCI_VPD_DATA);
270
271 return((val >> ((addr % 4) * 8)) & 0xFF);
272 }
273
274 void
275 bge_vpd_read_res(sc, res, addr)
276 struct bge_softc *sc;
277 struct vpd_res *res;
278 int addr;
279 {
280 int i;
281 u_int8_t *ptr;
282
283 ptr = (u_int8_t *)res;
284 for (i = 0; i < sizeof(struct vpd_res); i++)
285 ptr[i] = bge_vpd_readbyte(sc, i + addr);
286 }
287
288 void
289 bge_vpd_read(sc)
290 struct bge_softc *sc;
291 {
292 int pos = 0, i;
293 struct vpd_res res;
294
295 if (sc->bge_vpd_prodname != NULL)
296 free(sc->bge_vpd_prodname, M_DEVBUF);
297 if (sc->bge_vpd_readonly != NULL)
298 free(sc->bge_vpd_readonly, M_DEVBUF);
299 sc->bge_vpd_prodname = NULL;
300 sc->bge_vpd_readonly = NULL;
301
302 bge_vpd_read_res(sc, &res, pos);
303
304 if (res.vr_id != VPD_RES_ID) {
305 printf("%s: bad VPD resource id: expected %x got %x\n",
306 sc->bge_dev.dv_xname, VPD_RES_ID, res.vr_id);
307 return;
308 }
309
310 pos += sizeof(res);
311 sc->bge_vpd_prodname = malloc(res.vr_len + 1, M_DEVBUF, M_NOWAIT);
312 if (sc->bge_vpd_prodname == NULL)
313 panic("bge_vpd_read");
314 for (i = 0; i < res.vr_len; i++)
315 sc->bge_vpd_prodname[i] = bge_vpd_readbyte(sc, i + pos);
316 sc->bge_vpd_prodname[i] = '\0';
317 pos += i;
318
319 bge_vpd_read_res(sc, &res, pos);
320
321 if (res.vr_id != VPD_RES_READ) {
322 printf("%s: bad VPD resource id: expected %x got %x\n",
323 sc->bge_dev.dv_xname, VPD_RES_READ, res.vr_id);
324 return;
325 }
326
327 pos += sizeof(res);
328 sc->bge_vpd_readonly = malloc(res.vr_len, M_DEVBUF, M_NOWAIT);
329 if (sc->bge_vpd_readonly == NULL)
330 panic("bge_vpd_read");
331 for (i = 0; i < res.vr_len + 1; i++)
332 sc->bge_vpd_readonly[i] = bge_vpd_readbyte(sc, i + pos);
333 }
334 #endif
335
336 /*
337 * Read a byte of data stored in the EEPROM at address 'addr.' The
338 * BCM570x supports both the traditional bitbang interface and an
339 * auto access interface for reading the EEPROM. We use the auto
340 * access method.
341 */
342 u_int8_t
343 bge_eeprom_getbyte(sc, addr, dest)
344 struct bge_softc *sc;
345 int addr;
346 u_int8_t *dest;
347 {
348 int i;
349 u_int32_t byte = 0;
350
351 /*
352 * Enable use of auto EEPROM access so we can avoid
353 * having to use the bitbang method.
354 */
355 BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_AUTO_EEPROM);
356
357 /* Reset the EEPROM, load the clock period. */
358 CSR_WRITE_4(sc, BGE_EE_ADDR,
359 BGE_EEADDR_RESET|BGE_EEHALFCLK(BGE_HALFCLK_384SCL));
360 DELAY(20);
361
362 /* Issue the read EEPROM command. */
363 CSR_WRITE_4(sc, BGE_EE_ADDR, BGE_EE_READCMD | addr);
364
365 /* Wait for completion */
366 for(i = 0; i < BGE_TIMEOUT * 10; i++) {
367 DELAY(10);
368 if (CSR_READ_4(sc, BGE_EE_ADDR) & BGE_EEADDR_DONE)
369 break;
370 }
371
372 if (i == BGE_TIMEOUT) {
373 printf("%s: eeprom read timed out\n", sc->bge_dev.dv_xname);
374 return(0);
375 }
376
377 /* Get result. */
378 byte = CSR_READ_4(sc, BGE_EE_DATA);
379
380 *dest = (byte >> ((addr % 4) * 8)) & 0xFF;
381
382 return(0);
383 }
384
385 /*
386 * Read a sequence of bytes from the EEPROM.
387 */
388 int
389 bge_read_eeprom(sc, dest, off, cnt)
390 struct bge_softc *sc;
391 caddr_t dest;
392 int off;
393 int cnt;
394 {
395 int err = 0, i;
396 u_int8_t byte = 0;
397
398 for (i = 0; i < cnt; i++) {
399 err = bge_eeprom_getbyte(sc, off + i, &byte);
400 if (err)
401 break;
402 *(dest + i) = byte;
403 }
404
405 return(err ? 1 : 0);
406 }
407
408 int
409 bge_miibus_readreg(dev, phy, reg)
410 struct device *dev;
411 int phy, reg;
412 {
413 struct bge_softc *sc = (struct bge_softc *)dev;
414 struct ifnet *ifp;
415 u_int32_t val;
416 int i;
417
418 ifp = &sc->ethercom.ec_if;
419
420 if (sc->bge_asicrev == BGE_ASICREV_BCM5701_B5 && phy != 1)
421 return(0);
422
423 CSR_WRITE_4(sc, BGE_MI_COMM, BGE_MICMD_READ|BGE_MICOMM_BUSY|
424 BGE_MIPHY(phy)|BGE_MIREG(reg));
425
426 for (i = 0; i < BGE_TIMEOUT; i++) {
427 val = CSR_READ_4(sc, BGE_MI_COMM);
428 if (!(val & BGE_MICOMM_BUSY))
429 break;
430 }
431
432 if (i == BGE_TIMEOUT) {
433 printf("%s: PHY read timed out\n", sc->bge_dev.dv_xname);
434 return(0);
435 }
436
437 val = CSR_READ_4(sc, BGE_MI_COMM);
438
439 if (val & BGE_MICOMM_READFAIL)
440 return(0);
441
442 return(val & 0xFFFF);
443 }
444
445 void
446 bge_miibus_writereg(dev, phy, reg, val)
447 struct device *dev;
448 int phy, reg, val;
449 {
450 struct bge_softc *sc = (struct bge_softc *)dev;
451 int i;
452
453 CSR_WRITE_4(sc, BGE_MI_COMM, BGE_MICMD_WRITE|BGE_MICOMM_BUSY|
454 BGE_MIPHY(phy)|BGE_MIREG(reg)|val);
455
456 for (i = 0; i < BGE_TIMEOUT; i++) {
457 if (!(CSR_READ_4(sc, BGE_MI_COMM) & BGE_MICOMM_BUSY))
458 break;
459 }
460
461 if (i == BGE_TIMEOUT) {
462 printf("%s: PHY read timed out\n", sc->bge_dev.dv_xname);
463 }
464 }
465
466 void
467 bge_miibus_statchg(dev)
468 struct device *dev;
469 {
470 struct bge_softc *sc = (struct bge_softc *)dev;
471 struct mii_data *mii = &sc->bge_mii;
472
473 BGE_CLRBIT(sc, BGE_MAC_MODE, BGE_MACMODE_PORTMODE);
474 if (IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_T) {
475 BGE_SETBIT(sc, BGE_MAC_MODE, BGE_PORTMODE_GMII);
476 } else {
477 BGE_SETBIT(sc, BGE_MAC_MODE, BGE_PORTMODE_MII);
478 }
479
480 if ((mii->mii_media_active & IFM_GMASK) == IFM_FDX) {
481 BGE_CLRBIT(sc, BGE_MAC_MODE, BGE_MACMODE_HALF_DUPLEX);
482 } else {
483 BGE_SETBIT(sc, BGE_MAC_MODE, BGE_MACMODE_HALF_DUPLEX);
484 }
485
486 bge_phy_hack(sc);
487 }
488
489 /*
490 * Handle events that have triggered interrupts.
491 */
492 void
493 bge_handle_events(sc)
494 struct bge_softc *sc;
495 {
496
497 return;
498 }
499
500 /*
501 * Memory management for jumbo frames.
502 */
503
504 int
505 bge_alloc_jumbo_mem(sc)
506 struct bge_softc *sc;
507 {
508 caddr_t ptr, kva;
509 bus_dma_segment_t seg;
510 int i, rseg, state, error;
511 struct bge_jpool_entry *entry;
512
513 state = error = 0;
514
515 /* Grab a big chunk o' storage. */
516 if (bus_dmamem_alloc(sc->bge_dmatag, BGE_JMEM, PAGE_SIZE, 0,
517 &seg, 1, &rseg, BUS_DMA_NOWAIT)) {
518 printf("%s: can't alloc rx buffers\n", sc->bge_dev.dv_xname);
519 return ENOBUFS;
520 }
521
522 state = 1;
523 if (bus_dmamem_map(sc->bge_dmatag, &seg, rseg, BGE_JMEM, &kva,
524 BUS_DMA_NOWAIT)) {
525 printf("%s: can't map dma buffers (%d bytes)\n",
526 sc->bge_dev.dv_xname, (int)BGE_JMEM);
527 error = ENOBUFS;
528 goto out;
529 }
530
531 state = 2;
532 if (bus_dmamap_create(sc->bge_dmatag, BGE_JMEM, 1, BGE_JMEM, 0,
533 BUS_DMA_NOWAIT, &sc->bge_cdata.bge_rx_jumbo_map)) {
534 printf("%s: can't create dma map\n", sc->bge_dev.dv_xname);
535 error = ENOBUFS;
536 goto out;
537 }
538
539 state = 3;
540 if (bus_dmamap_load(sc->bge_dmatag, sc->bge_cdata.bge_rx_jumbo_map,
541 kva, BGE_JMEM, NULL, BUS_DMA_NOWAIT)) {
542 printf("%s: can't load dma map\n", sc->bge_dev.dv_xname);
543 error = ENOBUFS;
544 goto out;
545 }
546
547 state = 4;
548 sc->bge_cdata.bge_jumbo_buf = (caddr_t)kva;
549 DPRINTFN(1,("bge_jumbo_buf = 0x%p\n", sc->bge_cdata.bge_jumbo_buf));
550
551 SLIST_INIT(&sc->bge_jfree_listhead);
552 SLIST_INIT(&sc->bge_jinuse_listhead);
553
554 /*
555 * Now divide it up into 9K pieces and save the addresses
556 * in an array.
557 */
558 ptr = sc->bge_cdata.bge_jumbo_buf;
559 for (i = 0; i < BGE_JSLOTS; i++) {
560 sc->bge_cdata.bge_jslots[i] = ptr;
561 ptr += BGE_JLEN;
562 entry = malloc(sizeof(struct bge_jpool_entry),
563 M_DEVBUF, M_NOWAIT);
564 if (entry == NULL) {
565 printf("%s: no memory for jumbo buffer queue!\n",
566 sc->bge_dev.dv_xname);
567 error = ENOBUFS;
568 goto out;
569 }
570 entry->slot = i;
571 SLIST_INSERT_HEAD(&sc->bge_jfree_listhead,
572 entry, jpool_entries);
573 }
574 out:
575 if (error != 0) {
576 switch (state) {
577 case 4:
578 bus_dmamap_unload(sc->bge_dmatag,
579 sc->bge_cdata.bge_rx_jumbo_map);
580 case 3:
581 bus_dmamap_destroy(sc->bge_dmatag,
582 sc->bge_cdata.bge_rx_jumbo_map);
583 case 2:
584 bus_dmamem_unmap(sc->bge_dmatag, kva, BGE_JMEM);
585 case 1:
586 bus_dmamem_free(sc->bge_dmatag, &seg, rseg);
587 break;
588 default:
589 break;
590 }
591 }
592
593 return error;
594 }
595
596 /*
597 * Allocate a jumbo buffer.
598 */
599 void *
600 bge_jalloc(sc)
601 struct bge_softc *sc;
602 {
603 struct bge_jpool_entry *entry;
604
605 entry = SLIST_FIRST(&sc->bge_jfree_listhead);
606
607 if (entry == NULL) {
608 printf("%s: no free jumbo buffers\n", sc->bge_dev.dv_xname);
609 return(NULL);
610 }
611
612 SLIST_REMOVE_HEAD(&sc->bge_jfree_listhead, jpool_entries);
613 SLIST_INSERT_HEAD(&sc->bge_jinuse_listhead, entry, jpool_entries);
614 return(sc->bge_cdata.bge_jslots[entry->slot]);
615 }
616
617 /*
618 * Release a jumbo buffer.
619 */
620 void
621 bge_jfree(m, buf, size, arg)
622 struct mbuf *m;
623 caddr_t buf;
624 u_int size;
625 void *arg;
626 {
627 struct bge_jpool_entry *entry;
628 struct bge_softc *sc;
629 int i, s;
630
631 /* Extract the softc struct pointer. */
632 sc = (struct bge_softc *)arg;
633
634 if (sc == NULL)
635 panic("bge_jfree: can't find softc pointer!");
636
637 /* calculate the slot this buffer belongs to */
638
639 i = ((caddr_t)buf
640 - (caddr_t)sc->bge_cdata.bge_jumbo_buf) / BGE_JLEN;
641
642 if ((i < 0) || (i >= BGE_JSLOTS))
643 panic("bge_jfree: asked to free buffer that we don't manage!");
644
645 s = splvm();
646 entry = SLIST_FIRST(&sc->bge_jinuse_listhead);
647 if (entry == NULL)
648 panic("bge_jfree: buffer not in use!");
649 entry->slot = i;
650 SLIST_REMOVE_HEAD(&sc->bge_jinuse_listhead, jpool_entries);
651 SLIST_INSERT_HEAD(&sc->bge_jfree_listhead, entry, jpool_entries);
652
653 if (__predict_true(m != NULL))
654 pool_cache_put(&mbpool_cache, m);
655 splx(s);
656 }
657
658
659 /*
660 * Intialize a standard receive ring descriptor.
661 */
662 int
663 bge_newbuf_std(sc, i, m, dmamap)
664 struct bge_softc *sc;
665 int i;
666 struct mbuf *m;
667 bus_dmamap_t dmamap;
668 {
669 struct mbuf *m_new = NULL;
670 struct bge_rx_bd *r;
671 int error;
672
673 if (dmamap == NULL) {
674 error = bus_dmamap_create(sc->bge_dmatag, MCLBYTES, 1,
675 MCLBYTES, 0, BUS_DMA_NOWAIT, &dmamap);
676 if (error != 0)
677 return error;
678 }
679
680 sc->bge_cdata.bge_rx_std_map[i] = dmamap;
681
682 if (m == NULL) {
683 MGETHDR(m_new, M_DONTWAIT, MT_DATA);
684 if (m_new == NULL) {
685 return(ENOBUFS);
686 }
687
688 MCLGET(m_new, M_DONTWAIT);
689 if (!(m_new->m_flags & M_EXT)) {
690 m_freem(m_new);
691 return(ENOBUFS);
692 }
693 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
694
695 if (bus_dmamap_load_mbuf(sc->bge_dmatag, dmamap, m_new,
696 BUS_DMA_READ|BUS_DMA_NOWAIT))
697 return(ENOBUFS);
698 } else {
699 m_new = m;
700 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
701 m_new->m_data = m_new->m_ext.ext_buf;
702 }
703
704 m_adj(m_new, ETHER_ALIGN);
705 sc->bge_cdata.bge_rx_std_chain[i] = m_new;
706 r = &sc->bge_rdata->bge_rx_std_ring[i];
707 bge_set_hostaddr(&r->bge_addr,
708 dmamap->dm_segs[0].ds_addr + ETHER_ALIGN);
709 r->bge_flags = BGE_RXBDFLAG_END;
710 r->bge_len = m_new->m_len;
711 r->bge_idx = i;
712
713 bus_dmamap_sync(sc->bge_dmatag, sc->bge_ring_map,
714 offsetof(struct bge_ring_data, bge_rx_std_ring) +
715 i * sizeof (struct bge_rx_bd),
716 sizeof (struct bge_rx_bd),
717 BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD);
718
719 return(0);
720 }
721
722 /*
723 * Initialize a jumbo receive ring descriptor. This allocates
724 * a jumbo buffer from the pool managed internally by the driver.
725 */
726 int
727 bge_newbuf_jumbo(sc, i, m)
728 struct bge_softc *sc;
729 int i;
730 struct mbuf *m;
731 {
732 struct mbuf *m_new = NULL;
733 struct bge_rx_bd *r;
734
735 if (m == NULL) {
736 caddr_t *buf = NULL;
737
738 /* Allocate the mbuf. */
739 MGETHDR(m_new, M_DONTWAIT, MT_DATA);
740 if (m_new == NULL) {
741 return(ENOBUFS);
742 }
743
744 /* Allocate the jumbo buffer */
745 buf = bge_jalloc(sc);
746 if (buf == NULL) {
747 m_freem(m_new);
748 printf("%s: jumbo allocation failed "
749 "-- packet dropped!\n", sc->bge_dev.dv_xname);
750 return(ENOBUFS);
751 }
752
753 /* Attach the buffer to the mbuf. */
754 m_new->m_len = m_new->m_pkthdr.len = BGE_JUMBO_FRAMELEN;
755 MEXTADD(m_new, buf, BGE_JUMBO_FRAMELEN, M_DEVBUF,
756 bge_jfree, sc);
757 } else {
758 m_new = m;
759 m_new->m_data = m_new->m_ext.ext_buf;
760 m_new->m_ext.ext_size = BGE_JUMBO_FRAMELEN;
761 }
762
763 m_adj(m_new, ETHER_ALIGN);
764 /* Set up the descriptor. */
765 r = &sc->bge_rdata->bge_rx_jumbo_ring[i];
766 sc->bge_cdata.bge_rx_jumbo_chain[i] = m_new;
767 bge_set_hostaddr(&r->bge_addr, BGE_JUMBO_DMA_ADDR(sc, m_new));
768 r->bge_flags = BGE_RXBDFLAG_END|BGE_RXBDFLAG_JUMBO_RING;
769 r->bge_len = m_new->m_len;
770 r->bge_idx = i;
771
772 bus_dmamap_sync(sc->bge_dmatag, sc->bge_ring_map,
773 offsetof(struct bge_ring_data, bge_rx_jumbo_ring) +
774 i * sizeof (struct bge_rx_bd),
775 sizeof (struct bge_rx_bd),
776 BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD);
777
778 return(0);
779 }
780
781 /*
782 * The standard receive ring has 512 entries in it. At 2K per mbuf cluster,
783 * that's 1MB or memory, which is a lot. For now, we fill only the first
784 * 256 ring entries and hope that our CPU is fast enough to keep up with
785 * the NIC.
786 */
787 int
788 bge_init_rx_ring_std(sc)
789 struct bge_softc *sc;
790 {
791 int i;
792
793 if (sc->bge_flags & BGE_RXRING_VALID)
794 return 0;
795
796 for (i = 0; i < BGE_SSLOTS; i++) {
797 if (bge_newbuf_std(sc, i, NULL, 0) == ENOBUFS)
798 return(ENOBUFS);
799 }
800
801 sc->bge_std = i - 1;
802 CSR_WRITE_4(sc, BGE_MBX_RX_STD_PROD_LO, sc->bge_std);
803
804 sc->bge_flags |= BGE_RXRING_VALID;
805
806 return(0);
807 }
808
809 void
810 bge_free_rx_ring_std(sc)
811 struct bge_softc *sc;
812 {
813 int i;
814
815 if (!(sc->bge_flags & BGE_RXRING_VALID))
816 return;
817
818 for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
819 if (sc->bge_cdata.bge_rx_std_chain[i] != NULL) {
820 m_freem(sc->bge_cdata.bge_rx_std_chain[i]);
821 sc->bge_cdata.bge_rx_std_chain[i] = NULL;
822 bus_dmamap_destroy(sc->bge_dmatag,
823 sc->bge_cdata.bge_rx_std_map[i]);
824 }
825 memset((char *)&sc->bge_rdata->bge_rx_std_ring[i], 0,
826 sizeof(struct bge_rx_bd));
827 }
828
829 sc->bge_flags &= ~BGE_RXRING_VALID;
830 }
831
832 int
833 bge_init_rx_ring_jumbo(sc)
834 struct bge_softc *sc;
835 {
836 int i;
837 struct bge_rcb *rcb;
838 struct bge_rcb_opaque *rcbo;
839
840 for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
841 if (bge_newbuf_jumbo(sc, i, NULL) == ENOBUFS)
842 return(ENOBUFS);
843 };
844
845 sc->bge_jumbo = i - 1;
846
847 rcb = &sc->bge_rdata->bge_info.bge_jumbo_rx_rcb;
848 rcbo = (struct bge_rcb_opaque *)rcb;
849 rcb->bge_flags = 0;
850 CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_MAXLEN_FLAGS, rcbo->bge_reg2);
851
852 CSR_WRITE_4(sc, BGE_MBX_RX_JUMBO_PROD_LO, sc->bge_jumbo);
853
854 return(0);
855 }
856
857 void
858 bge_free_rx_ring_jumbo(sc)
859 struct bge_softc *sc;
860 {
861 int i;
862
863 if (!(sc->bge_flags & BGE_JUMBO_RXRING_VALID))
864 return;
865
866 for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
867 if (sc->bge_cdata.bge_rx_jumbo_chain[i] != NULL) {
868 m_freem(sc->bge_cdata.bge_rx_jumbo_chain[i]);
869 sc->bge_cdata.bge_rx_jumbo_chain[i] = NULL;
870 }
871 memset((char *)&sc->bge_rdata->bge_rx_jumbo_ring[i], 0,
872 sizeof(struct bge_rx_bd));
873 }
874
875 sc->bge_flags &= ~BGE_JUMBO_RXRING_VALID;
876 }
877
878 void
879 bge_free_tx_ring(sc)
880 struct bge_softc *sc;
881 {
882 int i, freed;
883 struct txdmamap_pool_entry *dma;
884
885 if (!(sc->bge_flags & BGE_TXRING_VALID))
886 return;
887
888 freed = 0;
889
890 for (i = 0; i < BGE_TX_RING_CNT; i++) {
891 if (sc->bge_cdata.bge_tx_chain[i] != NULL) {
892 freed++;
893 m_freem(sc->bge_cdata.bge_tx_chain[i]);
894 sc->bge_cdata.bge_tx_chain[i] = NULL;
895 SLIST_INSERT_HEAD(&sc->txdma_list, sc->txdma[i],
896 link);
897 sc->txdma[i] = 0;
898 }
899 memset((char *)&sc->bge_rdata->bge_tx_ring[i], 0,
900 sizeof(struct bge_tx_bd));
901 }
902
903 while ((dma = SLIST_FIRST(&sc->txdma_list))) {
904 SLIST_REMOVE_HEAD(&sc->txdma_list, link);
905 bus_dmamap_destroy(sc->bge_dmatag, dma->dmamap);
906 free(dma, M_DEVBUF);
907 }
908
909 sc->bge_flags &= ~BGE_TXRING_VALID;
910 }
911
912 int
913 bge_init_tx_ring(sc)
914 struct bge_softc *sc;
915 {
916 int i;
917 bus_dmamap_t dmamap;
918 struct txdmamap_pool_entry *dma;
919
920 if (sc->bge_flags & BGE_TXRING_VALID)
921 return 0;
922
923 sc->bge_txcnt = 0;
924 sc->bge_tx_saved_considx = 0;
925 CSR_WRITE_4(sc, BGE_MBX_TX_HOST_PROD0_LO, 0);
926 CSR_WRITE_4(sc, BGE_MBX_TX_NIC_PROD0_LO, 0);
927
928 SLIST_INIT(&sc->txdma_list);
929 for (i = 0; i < BGE_RSLOTS; i++) {
930 if (bus_dmamap_create(sc->bge_dmatag, ETHER_MAX_LEN_JUMBO,
931 BGE_NTXSEG, ETHER_MAX_LEN_JUMBO, 0, BUS_DMA_NOWAIT,
932 &dmamap))
933 return(ENOBUFS);
934 if (dmamap == NULL)
935 panic("dmamap NULL in bge_init_tx_ring");
936 dma = malloc(sizeof(*dma), M_DEVBUF, M_NOWAIT);
937 if (dma == NULL) {
938 printf("%s: can't alloc txdmamap_pool_entry\n",
939 sc->bge_dev.dv_xname);
940 bus_dmamap_destroy(sc->bge_dmatag, dmamap);
941 return (ENOMEM);
942 }
943 dma->dmamap = dmamap;
944 SLIST_INSERT_HEAD(&sc->txdma_list, dma, link);
945 }
946
947 sc->bge_flags |= BGE_TXRING_VALID;
948
949 return(0);
950 }
951
952 #define BGE_POLY 0xEDB88320
953
954 u_int32_t
955 bge_crc(sc, addr)
956 struct bge_softc *sc;
957 caddr_t addr;
958 {
959 u_int32_t idx, bit, data, crc;
960
961 /* Compute CRC for the address value. */
962 crc = 0xFFFFFFFF; /* initial value */
963
964 for (idx = 0; idx < 6; idx++) {
965 for (data = *addr++, bit = 0; bit < 8; bit++, data >>= 1)
966 crc = (crc >> 1) ^ (((crc ^ data) & 1) ? BGE_POLY : 0);
967 }
968
969 return(crc & 0x7F);
970 }
971
972 void
973 bge_setmulti(sc)
974 struct bge_softc *sc;
975 {
976 struct ethercom *ac = &sc->ethercom;
977 struct ifnet *ifp = &ac->ec_if;
978 struct ether_multi *enm;
979 struct ether_multistep step;
980 u_int32_t hashes[4] = { 0, 0, 0, 0 };
981 u_int32_t h;
982 int i;
983
984 if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
985 for (i = 0; i < 4; i++)
986 CSR_WRITE_4(sc, BGE_MAR0 + (i * 4), 0xFFFFFFFF);
987 return;
988 }
989
990 /* First, zot all the existing filters. */
991 for (i = 0; i < 4; i++)
992 CSR_WRITE_4(sc, BGE_MAR0 + (i * 4), 0);
993
994 /* Now program new ones. */
995 ETHER_FIRST_MULTI(step, ac, enm);
996 while (enm != NULL) {
997 h = bge_crc(sc, LLADDR((struct sockaddr_dl *)enm->enm_addrlo));
998 hashes[(h & 0x60) >> 5] |= 1 << (h & 0x1F);
999 ETHER_NEXT_MULTI(step, enm);
1000 }
1001
1002 for (i = 0; i < 4; i++)
1003 CSR_WRITE_4(sc, BGE_MAR0 + (i * 4), hashes[i]);
1004 }
1005
1006 int bge_swapbits[] = {
1007 0,
1008 BGE_MODECTL_BYTESWAP_DATA,
1009 BGE_MODECTL_WORDSWAP_DATA,
1010 BGE_MODECTL_BYTESWAP_NONFRAME,
1011 BGE_MODECTL_WORDSWAP_NONFRAME,
1012
1013 BGE_MODECTL_BYTESWAP_DATA|BGE_MODECTL_WORDSWAP_DATA,
1014 BGE_MODECTL_BYTESWAP_DATA|BGE_MODECTL_BYTESWAP_NONFRAME,
1015 BGE_MODECTL_BYTESWAP_DATA|BGE_MODECTL_WORDSWAP_NONFRAME,
1016
1017 BGE_MODECTL_WORDSWAP_DATA|BGE_MODECTL_BYTESWAP_NONFRAME,
1018 BGE_MODECTL_WORDSWAP_DATA|BGE_MODECTL_WORDSWAP_NONFRAME,
1019
1020 BGE_MODECTL_BYTESWAP_NONFRAME|BGE_MODECTL_WORDSWAP_NONFRAME,
1021
1022 BGE_MODECTL_BYTESWAP_DATA|BGE_MODECTL_WORDSWAP_DATA|
1023 BGE_MODECTL_BYTESWAP_NONFRAME,
1024 BGE_MODECTL_BYTESWAP_DATA|BGE_MODECTL_WORDSWAP_DATA|
1025 BGE_MODECTL_WORDSWAP_NONFRAME,
1026 BGE_MODECTL_BYTESWAP_DATA|BGE_MODECTL_BYTESWAP_NONFRAME|
1027 BGE_MODECTL_WORDSWAP_NONFRAME,
1028 BGE_MODECTL_WORDSWAP_DATA|BGE_MODECTL_BYTESWAP_NONFRAME|
1029 BGE_MODECTL_WORDSWAP_NONFRAME,
1030
1031 BGE_MODECTL_BYTESWAP_DATA|BGE_MODECTL_WORDSWAP_DATA|
1032 BGE_MODECTL_BYTESWAP_NONFRAME|BGE_MODECTL_WORDSWAP_NONFRAME,
1033 };
1034
1035 int bge_swapindex = 0;
1036
1037 /*
1038 * Do endian, PCI and DMA initialization. Also check the on-board ROM
1039 * self-test results.
1040 */
1041 int
1042 bge_chipinit(sc)
1043 struct bge_softc *sc;
1044 {
1045 u_int32_t cachesize;
1046 int i;
1047 struct pci_attach_args *pa = &(sc->bge_pa);
1048
1049
1050 /* Set endianness before we access any non-PCI registers. */
1051 pci_conf_write(pa->pa_pc, pa->pa_tag, BGE_PCI_MISC_CTL,
1052 BGE_INIT);
1053
1054 /*
1055 * Check the 'ROM failed' bit on the RX CPU to see if
1056 * self-tests passed.
1057 */
1058 if (CSR_READ_4(sc, BGE_RXCPU_MODE) & BGE_RXCPUMODE_ROMFAIL) {
1059 printf("%s: RX CPU self-diagnostics failed!\n",
1060 sc->bge_dev.dv_xname);
1061 return(ENODEV);
1062 }
1063
1064 /* Clear the MAC control register */
1065 CSR_WRITE_4(sc, BGE_MAC_MODE, 0);
1066
1067 /*
1068 * Clear the MAC statistics block in the NIC's
1069 * internal memory.
1070 */
1071 for (i = BGE_STATS_BLOCK;
1072 i < BGE_STATS_BLOCK_END + 1; i += sizeof(u_int32_t))
1073 BGE_MEMWIN_WRITE(pa->pa_pc, pa->pa_tag, i, 0);
1074
1075 for (i = BGE_STATUS_BLOCK;
1076 i < BGE_STATUS_BLOCK_END + 1; i += sizeof(u_int32_t))
1077 BGE_MEMWIN_WRITE(pa->pa_pc, pa->pa_tag, i, 0);
1078
1079 /* Set up the PCI DMA control register. */
1080 pci_conf_write(pa->pa_pc, pa->pa_tag, BGE_PCI_DMA_RW_CTL,
1081 BGE_PCI_READ_CMD|BGE_PCI_WRITE_CMD|0x0F);
1082
1083 /*
1084 * Set up general mode register.
1085 */
1086 CSR_WRITE_4(sc, BGE_MODE_CTL, BGE_DMA_SWAP_OPTIONS|
1087 BGE_MODECTL_MAC_ATTN_INTR|BGE_MODECTL_HOST_SEND_BDS|
1088 BGE_MODECTL_NO_RX_CRC|BGE_MODECTL_TX_NO_PHDR_CSUM|
1089 BGE_MODECTL_RX_NO_PHDR_CSUM);
1090
1091 /* Get cache line size. */
1092 cachesize = pci_conf_read(pa->pa_pc, pa->pa_tag, BGE_PCI_CACHESZ);
1093
1094 /*
1095 * Avoid violating PCI spec on certain chip revs.
1096 */
1097 if (pci_conf_read(pa->pa_pc, pa->pa_tag, BGE_PCI_CMD) &
1098 PCIM_CMD_MWIEN) {
1099 switch(cachesize) {
1100 case 1:
1101 PCI_SETBIT(pa->pa_pc, pa->pa_tag, BGE_PCI_DMA_RW_CTL,
1102 BGE_PCI_WRITE_BNDRY_16BYTES);
1103 break;
1104 case 2:
1105 PCI_SETBIT(pa->pa_pc, pa->pa_tag, BGE_PCI_DMA_RW_CTL,
1106 BGE_PCI_WRITE_BNDRY_32BYTES);
1107 break;
1108 case 4:
1109 PCI_SETBIT(pa->pa_pc, pa->pa_tag, BGE_PCI_DMA_RW_CTL,
1110 BGE_PCI_WRITE_BNDRY_64BYTES);
1111 break;
1112 case 8:
1113 PCI_SETBIT(pa->pa_pc, pa->pa_tag, BGE_PCI_DMA_RW_CTL,
1114 BGE_PCI_WRITE_BNDRY_128BYTES);
1115 break;
1116 case 16:
1117 PCI_SETBIT(pa->pa_pc, pa->pa_tag, BGE_PCI_DMA_RW_CTL,
1118 BGE_PCI_WRITE_BNDRY_256BYTES);
1119 break;
1120 case 32:
1121 PCI_SETBIT(pa->pa_pc, pa->pa_tag, BGE_PCI_DMA_RW_CTL,
1122 BGE_PCI_WRITE_BNDRY_512BYTES);
1123 break;
1124 case 64:
1125 PCI_SETBIT(pa->pa_pc, pa->pa_tag, BGE_PCI_DMA_RW_CTL,
1126 BGE_PCI_WRITE_BNDRY_1024BYTES);
1127 break;
1128 default:
1129 /* Disable PCI memory write and invalidate. */
1130 #if 0
1131 if (bootverbose)
1132 printf("%s: cache line size %d not "
1133 "supported; disabling PCI MWI\n",
1134 sc->bge_dev.dv_xname, cachesize);
1135 #endif
1136 PCI_CLRBIT(pa->pa_pc, pa->pa_tag, BGE_PCI_CMD,
1137 PCIM_CMD_MWIEN);
1138 break;
1139 }
1140 }
1141
1142 #ifdef __brokenalpha__
1143 /*
1144 * Must insure that we do not cross an 8K (bytes) boundary
1145 * for DMA reads. Our highest limit is 1K bytes. This is a
1146 * restriction on some ALPHA platforms with early revision
1147 * 21174 PCI chipsets, such as the AlphaPC 164lx
1148 */
1149 PCI_SETBIT(sc, BGE_PCI_DMA_RW_CTL, BGE_PCI_READ_BNDRY_1024, 4);
1150 #endif
1151
1152 /* Set the timer prescaler (always 66Mhz) */
1153 CSR_WRITE_4(sc, BGE_MISC_CFG, 65 << 1/*BGE_32BITTIME_66MHZ*/);
1154
1155 return(0);
1156 }
1157
1158 int
1159 bge_blockinit(sc)
1160 struct bge_softc *sc;
1161 {
1162 struct bge_rcb *rcb;
1163 struct bge_rcb_opaque *rcbo;
1164 bus_size_t rcb_addr;
1165 int i;
1166 struct ifnet *ifp = &sc->ethercom.ec_if;
1167 bge_hostaddr taddr;
1168
1169 /*
1170 * Initialize the memory window pointer register so that
1171 * we can access the first 32K of internal NIC RAM. This will
1172 * allow us to set up the TX send ring RCBs and the RX return
1173 * ring RCBs, plus other things which live in NIC memory.
1174 */
1175
1176 pci_conf_write(sc->bge_pa.pa_pc, sc->bge_pa.pa_tag,
1177 BGE_PCI_MEMWIN_BASEADDR, 0);
1178
1179 /* Configure mbuf memory pool */
1180 if (sc->bge_extram) {
1181 CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_BASEADDR, BGE_EXT_SSRAM);
1182 CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_LEN, 0x18000);
1183 } else {
1184 CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_BASEADDR, BGE_BUFFPOOL_1);
1185 CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_LEN, 0x18000);
1186 }
1187
1188 /* Configure DMA resource pool */
1189 CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_BASEADDR, BGE_DMA_DESCRIPTORS);
1190 CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_LEN, 0x2000);
1191
1192 /* Configure mbuf pool watermarks */
1193 CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_READDMA_LOWAT, 24);
1194 CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 24);
1195 CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_HIWAT, 48);
1196
1197 /* Configure DMA resource watermarks */
1198 CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_LOWAT, 5);
1199 CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_HIWAT, 10);
1200
1201 /* Enable buffer manager */
1202 CSR_WRITE_4(sc, BGE_BMAN_MODE,
1203 BGE_BMANMODE_ENABLE|BGE_BMANMODE_LOMBUF_ATTN);
1204
1205 /* Poll for buffer manager start indication */
1206 for (i = 0; i < BGE_TIMEOUT; i++) {
1207 if (CSR_READ_4(sc, BGE_BMAN_MODE) & BGE_BMANMODE_ENABLE)
1208 break;
1209 DELAY(10);
1210 }
1211
1212 if (i == BGE_TIMEOUT) {
1213 printf("%s: buffer manager failed to start\n",
1214 sc->bge_dev.dv_xname);
1215 return(ENXIO);
1216 }
1217
1218 /* Enable flow-through queues */
1219 CSR_WRITE_4(sc, BGE_FTQ_RESET, 0xFFFFFFFF);
1220 CSR_WRITE_4(sc, BGE_FTQ_RESET, 0);
1221
1222 /* Wait until queue initialization is complete */
1223 for (i = 0; i < BGE_TIMEOUT; i++) {
1224 if (CSR_READ_4(sc, BGE_FTQ_RESET) == 0)
1225 break;
1226 DELAY(10);
1227 }
1228
1229 if (i == BGE_TIMEOUT) {
1230 printf("%s: flow-through queue init failed\n",
1231 sc->bge_dev.dv_xname);
1232 return(ENXIO);
1233 }
1234
1235 /* Initialize the standard RX ring control block */
1236 rcb = &sc->bge_rdata->bge_info.bge_std_rx_rcb;
1237 bge_set_hostaddr(&rcb->bge_hostaddr,
1238 BGE_RING_DMA_ADDR(sc, bge_rx_std_ring));
1239 rcb->bge_max_len = BGE_MAX_FRAMELEN;
1240 if (sc->bge_extram)
1241 rcb->bge_nicaddr = BGE_EXT_STD_RX_RINGS;
1242 else
1243 rcb->bge_nicaddr = BGE_STD_RX_RINGS;
1244 rcb->bge_flags = 0;
1245 rcbo = (struct bge_rcb_opaque *)rcb;
1246 CSR_WRITE_4(sc, BGE_RX_STD_RCB_HADDR_HI, rcbo->bge_reg0);
1247 CSR_WRITE_4(sc, BGE_RX_STD_RCB_HADDR_LO, rcbo->bge_reg1);
1248 CSR_WRITE_4(sc, BGE_RX_STD_RCB_MAXLEN_FLAGS, rcbo->bge_reg2);
1249 CSR_WRITE_4(sc, BGE_RX_STD_RCB_NICADDR, rcbo->bge_reg3);
1250
1251 /*
1252 * Initialize the jumbo RX ring control block
1253 * We set the 'ring disabled' bit in the flags
1254 * field until we're actually ready to start
1255 * using this ring (i.e. once we set the MTU
1256 * high enough to require it).
1257 */
1258 rcb = &sc->bge_rdata->bge_info.bge_jumbo_rx_rcb;
1259 bge_set_hostaddr(&rcb->bge_hostaddr,
1260 BGE_RING_DMA_ADDR(sc, bge_rx_jumbo_ring));
1261 rcb->bge_max_len = BGE_MAX_FRAMELEN;
1262 if (sc->bge_extram)
1263 rcb->bge_nicaddr = BGE_EXT_JUMBO_RX_RINGS;
1264 else
1265 rcb->bge_nicaddr = BGE_JUMBO_RX_RINGS;
1266 rcb->bge_flags = BGE_RCB_FLAG_RING_DISABLED;
1267
1268 rcbo = (struct bge_rcb_opaque *)rcb;
1269 CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_HADDR_HI, rcbo->bge_reg0);
1270 CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_HADDR_LO, rcbo->bge_reg1);
1271 CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_MAXLEN_FLAGS, rcbo->bge_reg2);
1272 CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_NICADDR, rcbo->bge_reg3);
1273
1274 /* Set up dummy disabled mini ring RCB */
1275 rcb = &sc->bge_rdata->bge_info.bge_mini_rx_rcb;
1276 rcb->bge_flags = BGE_RCB_FLAG_RING_DISABLED;
1277 rcbo = (struct bge_rcb_opaque *)rcb;
1278 CSR_WRITE_4(sc, BGE_RX_MINI_RCB_MAXLEN_FLAGS, rcbo->bge_reg2);
1279
1280 bus_dmamap_sync(sc->bge_dmatag, sc->bge_ring_map,
1281 offsetof(struct bge_ring_data, bge_info), sizeof (struct bge_gib),
1282 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
1283
1284 /*
1285 * Set the BD ring replentish thresholds. The recommended
1286 * values are 1/8th the number of descriptors allocated to
1287 * each ring.
1288 */
1289 CSR_WRITE_4(sc, BGE_RBDI_STD_REPL_THRESH, BGE_STD_RX_RING_CNT/8);
1290 CSR_WRITE_4(sc, BGE_RBDI_JUMBO_REPL_THRESH, BGE_JUMBO_RX_RING_CNT/8);
1291
1292 /*
1293 * Disable all unused send rings by setting the 'ring disabled'
1294 * bit in the flags field of all the TX send ring control blocks.
1295 * These are located in NIC memory.
1296 */
1297 rcb_addr = BGE_MEMWIN_START + BGE_SEND_RING_RCB;
1298 for (i = 0; i < BGE_TX_RINGS_EXTSSRAM_MAX; i++) {
1299 RCB_WRITE_2(sc, rcb_addr, bge_flags,
1300 BGE_RCB_FLAG_RING_DISABLED);
1301 RCB_WRITE_2(sc, rcb_addr, bge_max_len, 0);
1302 RCB_WRITE_4(sc, rcb_addr, bge_nicaddr, 0);
1303 rcb_addr += sizeof(struct bge_rcb);
1304 }
1305
1306 /* Configure TX RCB 0 (we use only the first ring) */
1307 rcb_addr = BGE_MEMWIN_START + BGE_SEND_RING_RCB;
1308 bge_set_hostaddr(&taddr, BGE_RING_DMA_ADDR(sc, bge_tx_ring));
1309 RCB_WRITE_4(sc, rcb_addr, bge_hostaddr.bge_addr_hi, taddr.bge_addr_hi);
1310 RCB_WRITE_4(sc, rcb_addr, bge_hostaddr.bge_addr_lo, taddr.bge_addr_lo);
1311 RCB_WRITE_4(sc, rcb_addr, bge_nicaddr,
1312 BGE_NIC_TXRING_ADDR(0, BGE_TX_RING_CNT));
1313 RCB_WRITE_2(sc, rcb_addr, bge_max_len, BGE_TX_RING_CNT);
1314 RCB_WRITE_2(sc, rcb_addr, bge_flags, 0);
1315
1316 /* Disable all unused RX return rings */
1317 rcb_addr = BGE_MEMWIN_START + BGE_RX_RETURN_RING_RCB;
1318 for (i = 0; i < BGE_RX_RINGS_MAX; i++) {
1319 RCB_WRITE_4(sc, rcb_addr, bge_hostaddr.bge_addr_hi, 0);
1320 RCB_WRITE_4(sc, rcb_addr, bge_hostaddr.bge_addr_lo, 0);
1321 RCB_WRITE_2(sc, rcb_addr, bge_flags,
1322 BGE_RCB_FLAG_RING_DISABLED);
1323 RCB_WRITE_2(sc, rcb_addr, bge_max_len, BGE_RETURN_RING_CNT);
1324 RCB_WRITE_4(sc, rcb_addr, bge_nicaddr, 0);
1325 CSR_WRITE_4(sc, BGE_MBX_RX_CONS0_LO +
1326 (i * (sizeof(u_int64_t))), 0);
1327 rcb_addr += sizeof(struct bge_rcb);
1328 }
1329
1330 /* Initialize RX ring indexes */
1331 CSR_WRITE_4(sc, BGE_MBX_RX_STD_PROD_LO, 0);
1332 CSR_WRITE_4(sc, BGE_MBX_RX_JUMBO_PROD_LO, 0);
1333 CSR_WRITE_4(sc, BGE_MBX_RX_MINI_PROD_LO, 0);
1334
1335 /*
1336 * Set up RX return ring 0
1337 * Note that the NIC address for RX return rings is 0x00000000.
1338 * The return rings live entirely within the host, so the
1339 * nicaddr field in the RCB isn't used.
1340 */
1341 rcb_addr = BGE_MEMWIN_START + BGE_RX_RETURN_RING_RCB;
1342 bge_set_hostaddr(&taddr, BGE_RING_DMA_ADDR(sc, bge_rx_return_ring));
1343 RCB_WRITE_4(sc, rcb_addr, bge_hostaddr.bge_addr_hi, taddr.bge_addr_hi);
1344 RCB_WRITE_4(sc, rcb_addr, bge_hostaddr.bge_addr_lo, taddr.bge_addr_lo);
1345 RCB_WRITE_4(sc, rcb_addr, bge_nicaddr, 0x00000000);
1346 RCB_WRITE_2(sc, rcb_addr, bge_max_len, BGE_RETURN_RING_CNT);
1347 RCB_WRITE_2(sc, rcb_addr, bge_flags, 0);
1348
1349 /* Set random backoff seed for TX */
1350 CSR_WRITE_4(sc, BGE_TX_RANDOM_BACKOFF,
1351 LLADDR(ifp->if_sadl)[0] + LLADDR(ifp->if_sadl)[1] +
1352 LLADDR(ifp->if_sadl)[2] + LLADDR(ifp->if_sadl)[3] +
1353 LLADDR(ifp->if_sadl)[4] + LLADDR(ifp->if_sadl)[5] +
1354 BGE_TX_BACKOFF_SEED_MASK);
1355
1356 /* Set inter-packet gap */
1357 CSR_WRITE_4(sc, BGE_TX_LENGTHS, 0x2620);
1358
1359 /*
1360 * Specify which ring to use for packets that don't match
1361 * any RX rules.
1362 */
1363 CSR_WRITE_4(sc, BGE_RX_RULES_CFG, 0x08);
1364
1365 /*
1366 * Configure number of RX lists. One interrupt distribution
1367 * list, sixteen active lists, one bad frames class.
1368 */
1369 CSR_WRITE_4(sc, BGE_RXLP_CFG, 0x181);
1370
1371 /* Inialize RX list placement stats mask. */
1372 CSR_WRITE_4(sc, BGE_RXLP_STATS_ENABLE_MASK, 0x007FFFFF);
1373 CSR_WRITE_4(sc, BGE_RXLP_STATS_CTL, 0x1);
1374
1375 /* Disable host coalescing until we get it set up */
1376 CSR_WRITE_4(sc, BGE_HCC_MODE, 0x00000000);
1377
1378 /* Poll to make sure it's shut down. */
1379 for (i = 0; i < BGE_TIMEOUT; i++) {
1380 if (!(CSR_READ_4(sc, BGE_HCC_MODE) & BGE_HCCMODE_ENABLE))
1381 break;
1382 DELAY(10);
1383 }
1384
1385 if (i == BGE_TIMEOUT) {
1386 printf("%s: host coalescing engine failed to idle\n",
1387 sc->bge_dev.dv_xname);
1388 return(ENXIO);
1389 }
1390
1391 /* Set up host coalescing defaults */
1392 CSR_WRITE_4(sc, BGE_HCC_RX_COAL_TICKS, sc->bge_rx_coal_ticks);
1393 CSR_WRITE_4(sc, BGE_HCC_TX_COAL_TICKS, sc->bge_tx_coal_ticks);
1394 CSR_WRITE_4(sc, BGE_HCC_RX_MAX_COAL_BDS, sc->bge_rx_max_coal_bds);
1395 CSR_WRITE_4(sc, BGE_HCC_TX_MAX_COAL_BDS, sc->bge_tx_max_coal_bds);
1396 CSR_WRITE_4(sc, BGE_HCC_RX_COAL_TICKS_INT, 0);
1397 CSR_WRITE_4(sc, BGE_HCC_TX_COAL_TICKS_INT, 0);
1398 CSR_WRITE_4(sc, BGE_HCC_RX_MAX_COAL_BDS_INT, 0);
1399 CSR_WRITE_4(sc, BGE_HCC_TX_MAX_COAL_BDS_INT, 0);
1400 CSR_WRITE_4(sc, BGE_HCC_STATS_TICKS, sc->bge_stat_ticks);
1401
1402 /* Set up address of statistics block */
1403 bge_set_hostaddr(&taddr, BGE_RING_DMA_ADDR(sc, bge_info.bge_stats));
1404 CSR_WRITE_4(sc, BGE_HCC_STATS_BASEADDR, BGE_STATS_BLOCK);
1405 CSR_WRITE_4(sc, BGE_HCC_STATS_ADDR_HI, taddr.bge_addr_hi);
1406 CSR_WRITE_4(sc, BGE_HCC_STATS_ADDR_LO, taddr.bge_addr_lo);
1407
1408 /* Set up address of status block */
1409 bge_set_hostaddr(&taddr, BGE_RING_DMA_ADDR(sc, bge_status_block));
1410 CSR_WRITE_4(sc, BGE_HCC_STATUSBLK_BASEADDR, BGE_STATUS_BLOCK);
1411 CSR_WRITE_4(sc, BGE_HCC_STATUSBLK_ADDR_HI, taddr.bge_addr_hi);
1412 CSR_WRITE_4(sc, BGE_HCC_STATUSBLK_ADDR_LO, taddr.bge_addr_lo);
1413 sc->bge_rdata->bge_status_block.bge_idx[0].bge_rx_prod_idx = 0;
1414 sc->bge_rdata->bge_status_block.bge_idx[0].bge_tx_cons_idx = 0;
1415
1416 /* Turn on host coalescing state machine */
1417 CSR_WRITE_4(sc, BGE_HCC_MODE, BGE_HCCMODE_ENABLE);
1418
1419 /* Turn on RX BD completion state machine and enable attentions */
1420 CSR_WRITE_4(sc, BGE_RBDC_MODE,
1421 BGE_RBDCMODE_ENABLE|BGE_RBDCMODE_ATTN);
1422
1423 /* Turn on RX list placement state machine */
1424 CSR_WRITE_4(sc, BGE_RXLP_MODE, BGE_RXLPMODE_ENABLE);
1425
1426 /* Turn on RX list selector state machine. */
1427 CSR_WRITE_4(sc, BGE_RXLS_MODE, BGE_RXLSMODE_ENABLE);
1428
1429 /* Turn on DMA, clear stats */
1430 CSR_WRITE_4(sc, BGE_MAC_MODE, BGE_MACMODE_TXDMA_ENB|
1431 BGE_MACMODE_RXDMA_ENB|BGE_MACMODE_RX_STATS_CLEAR|
1432 BGE_MACMODE_TX_STATS_CLEAR|BGE_MACMODE_RX_STATS_ENB|
1433 BGE_MACMODE_TX_STATS_ENB|BGE_MACMODE_FRMHDR_DMA_ENB|
1434 (sc->bge_tbi ? BGE_PORTMODE_TBI : BGE_PORTMODE_MII));
1435
1436 /* Set misc. local control, enable interrupts on attentions */
1437 CSR_WRITE_4(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_INTR_ONATTN);
1438
1439 #ifdef notdef
1440 /* Assert GPIO pins for PHY reset */
1441 BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_MISCIO_OUT0|
1442 BGE_MLC_MISCIO_OUT1|BGE_MLC_MISCIO_OUT2);
1443 BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_MISCIO_OUTEN0|
1444 BGE_MLC_MISCIO_OUTEN1|BGE_MLC_MISCIO_OUTEN2);
1445 #endif
1446
1447 /* Turn on DMA completion state machine */
1448 CSR_WRITE_4(sc, BGE_DMAC_MODE, BGE_DMACMODE_ENABLE);
1449
1450 /* Turn on write DMA state machine */
1451 CSR_WRITE_4(sc, BGE_WDMA_MODE,
1452 BGE_WDMAMODE_ENABLE|BGE_WDMAMODE_ALL_ATTNS);
1453
1454 /* Turn on read DMA state machine */
1455 CSR_WRITE_4(sc, BGE_RDMA_MODE,
1456 BGE_RDMAMODE_ENABLE|BGE_RDMAMODE_ALL_ATTNS);
1457
1458 /* Turn on RX data completion state machine */
1459 CSR_WRITE_4(sc, BGE_RDC_MODE, BGE_RDCMODE_ENABLE);
1460
1461 /* Turn on RX BD initiator state machine */
1462 CSR_WRITE_4(sc, BGE_RBDI_MODE, BGE_RBDIMODE_ENABLE);
1463
1464 /* Turn on RX data and RX BD initiator state machine */
1465 CSR_WRITE_4(sc, BGE_RDBDI_MODE, BGE_RDBDIMODE_ENABLE);
1466
1467 /* Turn on Mbuf cluster free state machine */
1468 CSR_WRITE_4(sc, BGE_MBCF_MODE, BGE_MBCFMODE_ENABLE);
1469
1470 /* Turn on send BD completion state machine */
1471 CSR_WRITE_4(sc, BGE_SBDC_MODE, BGE_SBDCMODE_ENABLE);
1472
1473 /* Turn on send data completion state machine */
1474 CSR_WRITE_4(sc, BGE_SDC_MODE, BGE_SDCMODE_ENABLE);
1475
1476 /* Turn on send data initiator state machine */
1477 CSR_WRITE_4(sc, BGE_SDI_MODE, BGE_SDIMODE_ENABLE);
1478
1479 /* Turn on send BD initiator state machine */
1480 CSR_WRITE_4(sc, BGE_SBDI_MODE, BGE_SBDIMODE_ENABLE);
1481
1482 /* Turn on send BD selector state machine */
1483 CSR_WRITE_4(sc, BGE_SRS_MODE, BGE_SRSMODE_ENABLE);
1484
1485 CSR_WRITE_4(sc, BGE_SDI_STATS_ENABLE_MASK, 0x007FFFFF);
1486 CSR_WRITE_4(sc, BGE_SDI_STATS_CTL,
1487 BGE_SDISTATSCTL_ENABLE|BGE_SDISTATSCTL_FASTER);
1488
1489 /* init LED register */
1490 CSR_WRITE_4(sc, BGE_MAC_LED_CTL, 0x00000000);
1491
1492 /* ack/clear link change events */
1493 CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED|
1494 BGE_MACSTAT_CFG_CHANGED);
1495 CSR_WRITE_4(sc, BGE_MI_STS, 0);
1496
1497 /* Enable PHY auto polling (for MII/GMII only) */
1498 if (sc->bge_tbi) {
1499 CSR_WRITE_4(sc, BGE_MI_STS, BGE_MISTS_LINK);
1500 } else {
1501 BGE_SETBIT(sc, BGE_MI_MODE, BGE_MIMODE_AUTOPOLL|10<<16);
1502 if (BGE_IS_5700_Ax_Bx(sc->bge_asicrev))
1503 CSR_WRITE_4(sc, BGE_MAC_EVT_ENB,
1504 BGE_EVTENB_MI_INTERRUPT);
1505 }
1506
1507 /* Enable link state change attentions. */
1508 BGE_SETBIT(sc, BGE_MAC_EVT_ENB, BGE_EVTENB_LINK_CHANGED);
1509
1510 return(0);
1511 }
1512
1513 /*
1514 * Probe for a Broadcom chip. Check the PCI vendor and device IDs
1515 * against our list and return its name if we find a match. Note
1516 * that since the Broadcom controller contains VPD support, we
1517 * can get the device name string from the controller itself instead
1518 * of the compiled-in string. This is a little slow, but it guarantees
1519 * we'll always announce the right product name.
1520 */
1521 int
1522 bge_probe(parent, match, aux)
1523 struct device *parent;
1524 struct cfdata *match;
1525 void *aux;
1526 {
1527 struct pci_attach_args *pa = (struct pci_attach_args *)aux;
1528
1529 /*
1530 * Various supported device vendors/types and their
1531 * names. Note: the spec seems to indicate that the hardware
1532 * still has Alteon's vendor ID burned into it, though it will
1533 * always be overriden by the vendor ID in the EEPROM. Just to
1534 * be safe, we cover all possibilities.
1535 */
1536
1537 if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_ALTEON &&
1538 (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_ALTEON_BCM5700 ||
1539 PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_ALTEON_BCM5701))
1540 return (1);
1541
1542 if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_ALTIMA &&
1543 ((PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_ALTIMA_AC1000) ||
1544 PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_ALTIMA_AC9100))
1545 return (1);
1546
1547 if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_BROADCOM &&
1548 (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_BROADCOM_BCM5700 ||
1549 PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_BROADCOM_BCM5701))
1550 return (1);
1551
1552 if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_SCHNEIDERKOCH &&
1553 PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_SCHNEIDERKOCH_SK_9DX1)
1554 return (1);
1555
1556 if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_3COM &&
1557 PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_3COM_3C996)
1558 return (1);
1559
1560 return (0);
1561 }
1562
1563 void
1564 bge_attach(parent, self, aux)
1565 struct device *parent, *self;
1566 void *aux;
1567 {
1568 struct bge_softc *sc = (struct bge_softc *)self;
1569 struct pci_attach_args *pa = aux;
1570 pci_chipset_tag_t pc = pa->pa_pc;
1571 pci_intr_handle_t ih;
1572 const char *intrstr = NULL;
1573 bus_dma_segment_t seg;
1574 int rseg;
1575 u_int32_t hwcfg = 0;
1576 u_int32_t command;
1577 struct ifnet *ifp;
1578 int unit;
1579 caddr_t kva;
1580 u_char eaddr[ETHER_ADDR_LEN];
1581 pcireg_t memtype;
1582 char devinfo[256];
1583 bus_addr_t memaddr;
1584 bus_size_t memsize;
1585
1586 sc->bge_pa = *pa;
1587
1588 pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo);
1589 printf(": %s (rev. 0x%02x)\n", devinfo, PCI_REVISION(pa->pa_class));
1590
1591 /*
1592 * Map control/status registers.
1593 */
1594 DPRINTFN(5, ("Map control/status regs\n"));
1595 command = pci_conf_read(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
1596 command |= PCI_COMMAND_MEM_ENABLE | PCI_COMMAND_MASTER_ENABLE;
1597 pci_conf_write(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG, command);
1598 command = pci_conf_read(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
1599
1600 if (!(command & PCI_COMMAND_MEM_ENABLE)) {
1601 printf("%s: failed to enable memory mapping!\n",
1602 sc->bge_dev.dv_xname);
1603 return;
1604 }
1605
1606 DPRINTFN(5, ("pci_mem_find\n"));
1607 memtype = pci_mapreg_type(pa->pa_pc, pa->pa_tag, BGE_PCI_BAR0);
1608 switch (memtype) {
1609 case PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT:
1610 case PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_64BIT:
1611 if (pci_mapreg_map(pa, BGE_PCI_BAR0,
1612 memtype, 0, &sc->bge_btag, &sc->bge_bhandle,
1613 &memaddr, &memsize) == 0)
1614 break;
1615 default:
1616 printf("%s: can't find mem space\n",
1617 sc->bge_dev.dv_xname);
1618 return;
1619 }
1620
1621 DPRINTFN(5, ("pci_intr_map\n"));
1622 if (pci_intr_map(pa, &ih)) {
1623 printf("%s: couldn't map interrupt\n",
1624 sc->bge_dev.dv_xname);
1625 return;
1626 }
1627
1628 DPRINTFN(5, ("pci_intr_string\n"));
1629 intrstr = pci_intr_string(pc, ih);
1630
1631 DPRINTFN(5, ("pci_intr_establish\n"));
1632 sc->bge_intrhand = pci_intr_establish(pc, ih, IPL_NET, bge_intr, sc);
1633
1634 if (sc->bge_intrhand == NULL) {
1635 printf("%s: couldn't establish interrupt",
1636 sc->bge_dev.dv_xname);
1637 if (intrstr != NULL)
1638 printf(" at %s", intrstr);
1639 printf("\n");
1640 return;
1641 }
1642 printf("%s: interrupting at %s\n", sc->bge_dev.dv_xname, intrstr);
1643
1644 /* Try to reset the chip. */
1645 DPRINTFN(5, ("bge_reset\n"));
1646 bge_reset(sc);
1647
1648 if (bge_chipinit(sc)) {
1649 printf("%s: chip initializatino failed\n",
1650 sc->bge_dev.dv_xname);
1651 bge_release_resources(sc);
1652 return;
1653 }
1654
1655 /*
1656 * Get station address from the EEPROM.
1657 */
1658 if (bge_read_eeprom(sc, (caddr_t)eaddr,
1659 BGE_EE_MAC_OFFSET + 2, ETHER_ADDR_LEN)) {
1660 printf("bge%d: failed to read station address\n", unit);
1661 bge_release_resources(sc);
1662 return;
1663 }
1664
1665 /*
1666 * A Broadcom chip was detected. Inform the world.
1667 */
1668 printf("%s: Ethernet address %s\n", sc->bge_dev.dv_xname,
1669 ether_sprintf(eaddr));
1670
1671 /* Allocate the general information block and ring buffers. */
1672 sc->bge_dmatag = pa->pa_dmat;
1673 DPRINTFN(5, ("bus_dmamem_alloc\n"));
1674 if (bus_dmamem_alloc(sc->bge_dmatag, sizeof(struct bge_ring_data),
1675 PAGE_SIZE, 0, &seg, 1, &rseg, BUS_DMA_NOWAIT)) {
1676 printf("%s: can't alloc rx buffers\n", sc->bge_dev.dv_xname);
1677 return;
1678 }
1679 DPRINTFN(5, ("bus_dmamem_map\n"));
1680 if (bus_dmamem_map(sc->bge_dmatag, &seg, rseg,
1681 sizeof(struct bge_ring_data), &kva,
1682 BUS_DMA_NOWAIT)) {
1683 printf("%s: can't map dma buffers (%d bytes)\n",
1684 sc->bge_dev.dv_xname, (int)sizeof(struct bge_ring_data));
1685 bus_dmamem_free(sc->bge_dmatag, &seg, rseg);
1686 return;
1687 }
1688 DPRINTFN(5, ("bus_dmamem_create\n"));
1689 if (bus_dmamap_create(sc->bge_dmatag, sizeof(struct bge_ring_data), 1,
1690 sizeof(struct bge_ring_data), 0,
1691 BUS_DMA_NOWAIT, &sc->bge_ring_map)) {
1692 printf("%s: can't create dma map\n", sc->bge_dev.dv_xname);
1693 bus_dmamem_unmap(sc->bge_dmatag, kva,
1694 sizeof(struct bge_ring_data));
1695 bus_dmamem_free(sc->bge_dmatag, &seg, rseg);
1696 return;
1697 }
1698 DPRINTFN(5, ("bus_dmamem_load\n"));
1699 if (bus_dmamap_load(sc->bge_dmatag, sc->bge_ring_map, kva,
1700 sizeof(struct bge_ring_data), NULL,
1701 BUS_DMA_NOWAIT)) {
1702 bus_dmamap_destroy(sc->bge_dmatag, sc->bge_ring_map);
1703 bus_dmamem_unmap(sc->bge_dmatag, kva,
1704 sizeof(struct bge_ring_data));
1705 bus_dmamem_free(sc->bge_dmatag, &seg, rseg);
1706 return;
1707 }
1708
1709 DPRINTFN(5, ("bzero\n"));
1710 sc->bge_rdata = (struct bge_ring_data *)kva;
1711
1712 memset(sc->bge_rdata, sizeof(struct bge_ring_data), 0);
1713
1714 /* Try to allocate memory for jumbo buffers. */
1715 if (bge_alloc_jumbo_mem(sc)) {
1716 printf("%s: jumbo buffer allocation failed\n",
1717 sc->bge_dev.dv_xname);
1718 }
1719
1720 /* Set default tuneable values. */
1721 sc->bge_stat_ticks = BGE_TICKS_PER_SEC;
1722 sc->bge_rx_coal_ticks = 150;
1723 sc->bge_tx_coal_ticks = 150;
1724 sc->bge_rx_max_coal_bds = 64;
1725 sc->bge_tx_max_coal_bds = 128;
1726
1727 /* Set up ifnet structure */
1728 ifp = &sc->ethercom.ec_if;
1729 ifp->if_softc = sc;
1730 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
1731 ifp->if_ioctl = bge_ioctl;
1732 ifp->if_start = bge_start;
1733 ifp->if_init = bge_init;
1734 ifp->if_watchdog = bge_watchdog;
1735 IFQ_SET_MAXLEN(&ifp->if_snd, BGE_TX_RING_CNT - 1);
1736 IFQ_SET_READY(&ifp->if_snd);
1737 DPRINTFN(5, ("bcopy\n"));
1738 strcpy(ifp->if_xname, sc->bge_dev.dv_xname);
1739
1740 sc->ethercom.ec_capabilities |= ETHERCAP_JUMBO_MTU;
1741
1742 sc->ethercom.ec_if.if_capabilities |=
1743 IFCAP_CSUM_IPv4 | IFCAP_CSUM_TCPv4 | IFCAP_CSUM_UDPv4;
1744 sc->ethercom.ec_capabilities |=
1745 ETHERCAP_VLAN_HWTAGGING | ETHERCAP_VLAN_MTU;
1746
1747 /*
1748 * Do MII setup.
1749 */
1750 DPRINTFN(5, ("mii setup\n"));
1751 sc->bge_mii.mii_ifp = ifp;
1752 sc->bge_mii.mii_readreg = bge_miibus_readreg;
1753 sc->bge_mii.mii_writereg = bge_miibus_writereg;
1754 sc->bge_mii.mii_statchg = bge_miibus_statchg;
1755
1756 /* Save ASIC rev. */
1757
1758 sc->bge_asicrev =
1759 pci_conf_read(pa->pa_pc, pa->pa_tag, BGE_PCI_MISC_CTL) &
1760 BGE_PCIMISCCTL_ASICREV;
1761
1762 /*
1763 * Figure out what sort of media we have by checking the
1764 * hardware config word in the EEPROM. Note: on some BCM5700
1765 * cards, this value appears to be unset. If that's the
1766 * case, we have to rely on identifying the NIC by its PCI
1767 * subsystem ID, as we do below for the SysKonnect SK-9D41.
1768 */
1769 bge_read_eeprom(sc, (caddr_t)&hwcfg,
1770 BGE_EE_HWCFG_OFFSET, sizeof(hwcfg));
1771 if ((be32toh(hwcfg) & BGE_HWCFG_MEDIA) == BGE_MEDIA_FIBER)
1772 sc->bge_tbi = 1;
1773
1774 /* The SysKonnect SK-9D41 is a 1000baseSX card. */
1775 if ((pci_conf_read(pa->pa_pc, pa->pa_tag, BGE_PCI_SUBSYS) >> 16) ==
1776 SK_SUBSYSID_9D41)
1777 sc->bge_tbi = 1;
1778
1779 if (sc->bge_tbi) {
1780 ifmedia_init(&sc->bge_ifmedia, IFM_IMASK, bge_ifmedia_upd,
1781 bge_ifmedia_sts);
1782 ifmedia_add(&sc->bge_ifmedia, IFM_ETHER|IFM_1000_SX, 0, NULL);
1783 ifmedia_add(&sc->bge_ifmedia, IFM_ETHER|IFM_1000_SX|IFM_FDX,
1784 0, NULL);
1785 ifmedia_add(&sc->bge_ifmedia, IFM_ETHER|IFM_AUTO, 0, NULL);
1786 ifmedia_set(&sc->bge_ifmedia, IFM_ETHER|IFM_AUTO);
1787 } else {
1788 /*
1789 * Do transceiver setup.
1790 */
1791 ifmedia_init(&sc->bge_mii.mii_media, 0, bge_ifmedia_upd,
1792 bge_ifmedia_sts);
1793 mii_attach(&sc->bge_dev, &sc->bge_mii, 0xffffffff,
1794 MII_PHY_ANY, MII_OFFSET_ANY, 0);
1795
1796 if (LIST_FIRST(&sc->bge_mii.mii_phys) == NULL) {
1797 printf("%s: no PHY found!\n", sc->bge_dev.dv_xname);
1798 ifmedia_add(&sc->bge_mii.mii_media,
1799 IFM_ETHER|IFM_MANUAL, 0, NULL);
1800 ifmedia_set(&sc->bge_mii.mii_media,
1801 IFM_ETHER|IFM_MANUAL);
1802 } else
1803 ifmedia_set(&sc->bge_mii.mii_media,
1804 IFM_ETHER|IFM_AUTO);
1805 }
1806
1807 /*
1808 * Call MI attach routine.
1809 */
1810 DPRINTFN(5, ("if_attach\n"));
1811 if_attach(ifp);
1812 DPRINTFN(5, ("ether_ifattach\n"));
1813 ether_ifattach(ifp, eaddr);
1814 DPRINTFN(5, ("callout_init\n"));
1815 callout_init(&sc->bge_timeout);
1816 }
1817
1818 void
1819 bge_release_resources(sc)
1820 struct bge_softc *sc;
1821 {
1822 if (sc->bge_vpd_prodname != NULL)
1823 free(sc->bge_vpd_prodname, M_DEVBUF);
1824
1825 if (sc->bge_vpd_readonly != NULL)
1826 free(sc->bge_vpd_readonly, M_DEVBUF);
1827
1828 #ifdef fake
1829 if (sc->bge_intrhand != NULL)
1830 bus_teardown_intr(dev, sc->bge_irq, sc->bge_intrhand);
1831
1832 if (sc->bge_irq != NULL)
1833 bus_release_resource(dev, SYS_RES_IRQ, 0, sc->bge_irq);
1834
1835 if (sc->bge_res != NULL)
1836 bus_release_resource(dev, SYS_RES_MEMORY,
1837 BGE_PCI_BAR0, sc->bge_res);
1838
1839 if (sc->bge_rdata != NULL)
1840 contigfree(sc->bge_rdata,
1841 sizeof(struct bge_ring_data), M_DEVBUF);
1842 #endif
1843 }
1844
1845 void
1846 bge_reset(sc)
1847 struct bge_softc *sc;
1848 {
1849 struct pci_attach_args *pa = &sc->bge_pa;
1850 u_int32_t cachesize, command, pcistate;
1851 int i, val = 0;
1852
1853 /* Save some important PCI state. */
1854 cachesize = pci_conf_read(pa->pa_pc, pa->pa_tag, BGE_PCI_CACHESZ);
1855 command = pci_conf_read(pa->pa_pc, pa->pa_tag, BGE_PCI_CMD);
1856 pcistate = pci_conf_read(pa->pa_pc, pa->pa_tag, BGE_PCI_PCISTATE);
1857
1858 pci_conf_write(pa->pa_pc, pa->pa_tag, BGE_PCI_MISC_CTL,
1859 BGE_PCIMISCCTL_INDIRECT_ACCESS|BGE_PCIMISCCTL_MASK_PCI_INTR|
1860 BGE_HIF_SWAP_OPTIONS|BGE_PCIMISCCTL_PCISTATE_RW);
1861
1862 /* Issue global reset */
1863 bge_writereg_ind(sc, BGE_MISC_CFG,
1864 BGE_MISCCFG_RESET_CORE_CLOCKS|(65<<1));
1865
1866 DELAY(1000);
1867
1868 /* Reset some of the PCI state that got zapped by reset */
1869 pci_conf_write(pa->pa_pc, pa->pa_tag, BGE_PCI_MISC_CTL,
1870 BGE_PCIMISCCTL_INDIRECT_ACCESS|BGE_PCIMISCCTL_MASK_PCI_INTR|
1871 BGE_HIF_SWAP_OPTIONS|BGE_PCIMISCCTL_PCISTATE_RW);
1872 pci_conf_write(pa->pa_pc, pa->pa_tag, BGE_PCI_CMD, command);
1873 pci_conf_write(pa->pa_pc, pa->pa_tag, BGE_PCI_CACHESZ, cachesize);
1874 bge_writereg_ind(sc, BGE_MISC_CFG, (65 << 1));
1875
1876 /* Enable memory arbiter. */
1877 CSR_WRITE_4(sc, BGE_MARB_MODE, BGE_MARBMODE_ENABLE);
1878
1879 /*
1880 * Prevent PXE restart: write a magic number to the
1881 * general communications memory at 0xB50.
1882 */
1883 bge_writemem_ind(sc, BGE_SOFTWARE_GENCOMM, BGE_MAGIC_NUMBER);
1884
1885 /*
1886 * Poll the value location we just wrote until
1887 * we see the 1's complement of the magic number.
1888 * This indicates that the firmware initialization
1889 * is complete.
1890 */
1891 for (i = 0; i < 750; i++) {
1892 val = bge_readmem_ind(sc, BGE_SOFTWARE_GENCOMM);
1893 if (val == ~BGE_MAGIC_NUMBER)
1894 break;
1895 DELAY(1000);
1896 }
1897
1898 if (i == BGE_TIMEOUT) {
1899 printf("%s: firmware handshake timed out, val = %x\n",
1900 sc->bge_dev.dv_xname, val);
1901 return;
1902 }
1903
1904 /*
1905 * XXX Wait for the value of the PCISTATE register to
1906 * return to its original pre-reset state. This is a
1907 * fairly good indicator of reset completion. If we don't
1908 * wait for the reset to fully complete, trying to read
1909 * from the device's non-PCI registers may yield garbage
1910 * results.
1911 */
1912 for (i = 0; i < BGE_TIMEOUT; i++) {
1913 if (pci_conf_read(pa->pa_pc, pa->pa_tag, BGE_PCI_PCISTATE) ==
1914 pcistate)
1915 break;
1916 DELAY(10);
1917 }
1918
1919 /* Enable memory arbiter. */
1920 CSR_WRITE_4(sc, BGE_MARB_MODE, BGE_MARBMODE_ENABLE);
1921
1922 /* Fix up byte swapping */
1923 CSR_WRITE_4(sc, BGE_MODE_CTL, BGE_DMA_SWAP_OPTIONS);
1924
1925 CSR_WRITE_4(sc, BGE_MAC_MODE, 0);
1926
1927 DELAY(10000);
1928 }
1929
1930 /*
1931 * Frame reception handling. This is called if there's a frame
1932 * on the receive return list.
1933 *
1934 * Note: we have to be able to handle two possibilities here:
1935 * 1) the frame is from the jumbo recieve ring
1936 * 2) the frame is from the standard receive ring
1937 */
1938
1939 void
1940 bge_rxeof(sc)
1941 struct bge_softc *sc;
1942 {
1943 struct ifnet *ifp;
1944 int stdcnt = 0, jumbocnt = 0;
1945 int have_tag = 0;
1946 u_int16_t vlan_tag = 0;
1947 bus_dmamap_t dmamap;
1948 bus_addr_t offset, toff;
1949 bus_size_t tlen;
1950 int tosync;
1951
1952 ifp = &sc->ethercom.ec_if;
1953
1954 bus_dmamap_sync(sc->bge_dmatag, sc->bge_ring_map,
1955 offsetof(struct bge_ring_data, bge_status_block),
1956 sizeof (struct bge_status_block),
1957 BUS_DMASYNC_POSTREAD);
1958
1959 offset = offsetof(struct bge_ring_data, bge_rx_return_ring);
1960 tosync = sc->bge_rdata->bge_status_block.bge_idx[0].bge_rx_prod_idx -
1961 sc->bge_rx_saved_considx;
1962
1963 toff = offset + (sc->bge_rx_saved_considx * sizeof (struct bge_rx_bd));
1964
1965 if (tosync < 0) {
1966 tlen = (BGE_RETURN_RING_CNT - sc->bge_rx_saved_considx) *
1967 sizeof (struct bge_rx_bd);
1968 bus_dmamap_sync(sc->bge_dmatag, sc->bge_ring_map,
1969 toff, tlen, BUS_DMASYNC_POSTREAD);
1970 tosync = -tosync;
1971 }
1972
1973 bus_dmamap_sync(sc->bge_dmatag, sc->bge_ring_map,
1974 offset, tosync * sizeof (struct bge_rx_bd),
1975 BUS_DMASYNC_POSTREAD);
1976
1977 while(sc->bge_rx_saved_considx !=
1978 sc->bge_rdata->bge_status_block.bge_idx[0].bge_rx_prod_idx) {
1979 struct bge_rx_bd *cur_rx;
1980 u_int32_t rxidx;
1981 struct mbuf *m = NULL;
1982
1983 cur_rx = &sc->bge_rdata->
1984 bge_rx_return_ring[sc->bge_rx_saved_considx];
1985
1986 rxidx = cur_rx->bge_idx;
1987 BGE_INC(sc->bge_rx_saved_considx, BGE_RETURN_RING_CNT);
1988
1989 if (cur_rx->bge_flags & BGE_RXBDFLAG_VLAN_TAG) {
1990 have_tag = 1;
1991 vlan_tag = cur_rx->bge_vlan_tag;
1992 }
1993
1994 if (cur_rx->bge_flags & BGE_RXBDFLAG_JUMBO_RING) {
1995 BGE_INC(sc->bge_jumbo, BGE_JUMBO_RX_RING_CNT);
1996 m = sc->bge_cdata.bge_rx_jumbo_chain[rxidx];
1997 sc->bge_cdata.bge_rx_jumbo_chain[rxidx] = NULL;
1998 jumbocnt++;
1999 if (cur_rx->bge_flags & BGE_RXBDFLAG_ERROR) {
2000 ifp->if_ierrors++;
2001 bge_newbuf_jumbo(sc, sc->bge_jumbo, m);
2002 continue;
2003 }
2004 if (bge_newbuf_jumbo(sc, sc->bge_jumbo,
2005 NULL)== ENOBUFS) {
2006 ifp->if_ierrors++;
2007 bge_newbuf_jumbo(sc, sc->bge_jumbo, m);
2008 continue;
2009 }
2010 } else {
2011 BGE_INC(sc->bge_std, BGE_STD_RX_RING_CNT);
2012 m = sc->bge_cdata.bge_rx_std_chain[rxidx];
2013 sc->bge_cdata.bge_rx_std_chain[rxidx] = NULL;
2014 stdcnt++;
2015 dmamap = sc->bge_cdata.bge_rx_std_map[rxidx];
2016 sc->bge_cdata.bge_rx_std_map[rxidx] = 0;
2017 if (cur_rx->bge_flags & BGE_RXBDFLAG_ERROR) {
2018 ifp->if_ierrors++;
2019 bge_newbuf_std(sc, sc->bge_std, m, dmamap);
2020 continue;
2021 }
2022 if (bge_newbuf_std(sc, sc->bge_std,
2023 NULL, dmamap) == ENOBUFS) {
2024 ifp->if_ierrors++;
2025 bge_newbuf_std(sc, sc->bge_std, m, dmamap);
2026 continue;
2027 }
2028 }
2029
2030 ifp->if_ipackets++;
2031 m->m_pkthdr.len = m->m_len = cur_rx->bge_len;
2032 m->m_pkthdr.rcvif = ifp;
2033
2034 #if NBPFILTER > 0
2035 /*
2036 * Handle BPF listeners. Let the BPF user see the packet.
2037 */
2038 if (ifp->if_bpf)
2039 bpf_mtap(ifp->if_bpf, m);
2040 #endif
2041
2042 if (sc->bge_asicrev != BGE_ASICREV_BCM5700_B0) {
2043 m->m_pkthdr.csum_flags |= M_CSUM_IPv4;
2044 if ((cur_rx->bge_ip_csum ^ 0xffff) != 0)
2045 m->m_pkthdr.csum_flags |= M_CSUM_IPv4_BAD;
2046 #if 0 /* XXX appears to be broken */
2047 if (cur_rx->bge_flags & BGE_RXBDFLAG_TCP_UDP_CSUM) {
2048 m->m_pkthdr.csum_data =
2049 cur_rx->bge_tcp_udp_csum;
2050 m->m_pkthdr.csum_flags |=
2051 (M_CSUM_TCPv4|M_CSUM_UDPv4|M_CSUM_DATA);
2052 }
2053 #endif
2054 }
2055
2056 /*
2057 * If we received a packet with a vlan tag, pass it
2058 * to vlan_input() instead of ether_input().
2059 */
2060 if (have_tag) {
2061 struct mbuf *n;
2062
2063 n = m_aux_add(m, AF_LINK, ETHERTYPE_VLAN);
2064 if (n != NULL) {
2065 *mtod(n, int *) = vlan_tag;
2066 n->m_len = sizeof(int);
2067 have_tag = vlan_tag = 0;
2068 } else {
2069 printf("%s: no mbuf for tag\n", ifp->if_xname);
2070 m_freem(m);
2071 have_tag = vlan_tag = 0;
2072 continue;
2073 }
2074 }
2075 (*ifp->if_input)(ifp, m);
2076 }
2077
2078 CSR_WRITE_4(sc, BGE_MBX_RX_CONS0_LO, sc->bge_rx_saved_considx);
2079 if (stdcnt)
2080 CSR_WRITE_4(sc, BGE_MBX_RX_STD_PROD_LO, sc->bge_std);
2081 if (jumbocnt)
2082 CSR_WRITE_4(sc, BGE_MBX_RX_JUMBO_PROD_LO, sc->bge_jumbo);
2083 }
2084
2085 void
2086 bge_txeof(sc)
2087 struct bge_softc *sc;
2088 {
2089 struct bge_tx_bd *cur_tx = NULL;
2090 struct ifnet *ifp;
2091 struct txdmamap_pool_entry *dma;
2092 bus_addr_t offset, toff;
2093 bus_size_t tlen;
2094 int tosync;
2095 struct mbuf *m;
2096
2097 ifp = &sc->ethercom.ec_if;
2098
2099 bus_dmamap_sync(sc->bge_dmatag, sc->bge_ring_map,
2100 offsetof(struct bge_ring_data, bge_status_block),
2101 sizeof (struct bge_status_block),
2102 BUS_DMASYNC_POSTREAD);
2103
2104 offset = offsetof(struct bge_ring_data, bge_tx_ring);
2105 tosync = sc->bge_rdata->bge_status_block.bge_idx[0].bge_tx_cons_idx -
2106 sc->bge_tx_saved_considx;
2107
2108 toff = offset + (sc->bge_tx_saved_considx * sizeof (struct bge_tx_bd));
2109
2110 if (tosync < 0) {
2111 tlen = (BGE_TX_RING_CNT - sc->bge_tx_saved_considx) *
2112 sizeof (struct bge_tx_bd);
2113 bus_dmamap_sync(sc->bge_dmatag, sc->bge_ring_map,
2114 toff, tlen, BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
2115 tosync = -tosync;
2116 }
2117
2118 bus_dmamap_sync(sc->bge_dmatag, sc->bge_ring_map,
2119 offset, tosync * sizeof (struct bge_tx_bd),
2120 BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
2121
2122 /*
2123 * Go through our tx ring and free mbufs for those
2124 * frames that have been sent.
2125 */
2126 while (sc->bge_tx_saved_considx !=
2127 sc->bge_rdata->bge_status_block.bge_idx[0].bge_tx_cons_idx) {
2128 u_int32_t idx = 0;
2129
2130 idx = sc->bge_tx_saved_considx;
2131 cur_tx = &sc->bge_rdata->bge_tx_ring[idx];
2132 if (cur_tx->bge_flags & BGE_TXBDFLAG_END)
2133 ifp->if_opackets++;
2134 m = sc->bge_cdata.bge_tx_chain[idx];
2135 if (m != NULL) {
2136 sc->bge_cdata.bge_tx_chain[idx] = NULL;
2137 dma = sc->txdma[idx];
2138 bus_dmamap_sync(sc->bge_dmatag, dma->dmamap, 0,
2139 dma->dmamap->dm_mapsize, BUS_DMASYNC_POSTWRITE);
2140 bus_dmamap_unload(sc->bge_dmatag, dma->dmamap);
2141 SLIST_INSERT_HEAD(&sc->txdma_list, dma, link);
2142 sc->txdma[idx] = NULL;
2143
2144 m_freem(m);
2145 }
2146 sc->bge_txcnt--;
2147 BGE_INC(sc->bge_tx_saved_considx, BGE_TX_RING_CNT);
2148 ifp->if_timer = 0;
2149 }
2150
2151 if (cur_tx != NULL)
2152 ifp->if_flags &= ~IFF_OACTIVE;
2153 }
2154
2155 int
2156 bge_intr(xsc)
2157 void *xsc;
2158 {
2159 struct bge_softc *sc;
2160 struct ifnet *ifp;
2161
2162 sc = xsc;
2163 ifp = &sc->ethercom.ec_if;
2164
2165 #ifdef notdef
2166 /* Avoid this for now -- checking this register is expensive. */
2167 /* Make sure this is really our interrupt. */
2168 if (!(CSR_READ_4(sc, BGE_MISC_LOCAL_CTL) & BGE_MLC_INTR_STATE))
2169 return (0);
2170 #endif
2171 /* Ack interrupt and stop others from occuring. */
2172 CSR_WRITE_4(sc, BGE_MBX_IRQ0_LO, 1);
2173
2174 /*
2175 * Process link state changes.
2176 * Grrr. The link status word in the status block does
2177 * not work correctly on the BCM5700 rev AX and BX chips,
2178 * according to all avaibable information. Hence, we have
2179 * to enable MII interrupts in order to properly obtain
2180 * async link changes. Unfortunately, this also means that
2181 * we have to read the MAC status register to detect link
2182 * changes, thereby adding an additional register access to
2183 * the interrupt handler.
2184 */
2185
2186 if (BGE_IS_5700_Ax_Bx(sc->bge_asicrev)) {
2187 u_int32_t status;
2188
2189 status = CSR_READ_4(sc, BGE_MAC_STS);
2190 if (status & BGE_MACSTAT_MI_INTERRUPT) {
2191 sc->bge_link = 0;
2192 callout_stop(&sc->bge_timeout);
2193 bge_tick(sc);
2194 /* Clear the interrupt */
2195 CSR_WRITE_4(sc, BGE_MAC_EVT_ENB,
2196 BGE_EVTENB_MI_INTERRUPT);
2197 bge_miibus_readreg(&sc->bge_dev, 1, BRGPHY_MII_ISR);
2198 bge_miibus_writereg(&sc->bge_dev, 1, BRGPHY_MII_IMR,
2199 BRGPHY_INTRS);
2200 }
2201 } else {
2202 if (sc->bge_rdata->bge_status_block.bge_status &
2203 BGE_STATFLAG_LINKSTATE_CHANGED) {
2204 sc->bge_link = 0;
2205 callout_stop(&sc->bge_timeout);
2206 bge_tick(sc);
2207 /* Clear the interrupt */
2208 CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED|
2209 BGE_MACSTAT_CFG_CHANGED);
2210 }
2211 }
2212
2213 if (ifp->if_flags & IFF_RUNNING) {
2214 /* Check RX return ring producer/consumer */
2215 bge_rxeof(sc);
2216
2217 /* Check TX ring producer/consumer */
2218 bge_txeof(sc);
2219 }
2220
2221 bge_handle_events(sc);
2222
2223 /* Re-enable interrupts. */
2224 CSR_WRITE_4(sc, BGE_MBX_IRQ0_LO, 0);
2225
2226 if (ifp->if_flags & IFF_RUNNING && !IFQ_IS_EMPTY(&ifp->if_snd))
2227 bge_start(ifp);
2228
2229 return (1);
2230 }
2231
2232 void
2233 bge_tick(xsc)
2234 void *xsc;
2235 {
2236 struct bge_softc *sc = xsc;
2237 struct mii_data *mii = &sc->bge_mii;
2238 struct ifmedia *ifm = NULL;
2239 struct ifnet *ifp = &sc->ethercom.ec_if;
2240 int s;
2241
2242 s = splnet();
2243
2244 bge_stats_update(sc);
2245 callout_reset(&sc->bge_timeout, hz, bge_tick, sc);
2246 if (sc->bge_link) {
2247 splx(s);
2248 return;
2249 }
2250
2251 if (sc->bge_tbi) {
2252 ifm = &sc->bge_ifmedia;
2253 if (CSR_READ_4(sc, BGE_MAC_STS) &
2254 BGE_MACSTAT_TBI_PCS_SYNCHED) {
2255 sc->bge_link++;
2256 CSR_WRITE_4(sc, BGE_MAC_STS, 0xFFFFFFFF);
2257 printf("%s: gigabit link up\n", sc->bge_dev.dv_xname);
2258 if (!IFQ_IS_EMPTY(&ifp->if_snd))
2259 bge_start(ifp);
2260 }
2261 splx(s);
2262 return;
2263 }
2264
2265 mii_tick(mii);
2266
2267 if (!sc->bge_link && mii->mii_media_status & IFM_ACTIVE &&
2268 IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
2269 sc->bge_link++;
2270 if (IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_T ||
2271 IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_SX)
2272 printf("%s: gigabit link up\n", sc->bge_dev.dv_xname);
2273 if (!IFQ_IS_EMPTY(&ifp->if_snd))
2274 bge_start(ifp);
2275 }
2276
2277 splx(s);
2278 }
2279
2280 void
2281 bge_stats_update(sc)
2282 struct bge_softc *sc;
2283 {
2284 struct ifnet *ifp = &sc->ethercom.ec_if;
2285 bus_size_t stats = BGE_MEMWIN_START + BGE_STATS_BLOCK;
2286
2287 #define READ_STAT(sc, stats, stat) \
2288 CSR_READ_4(sc, stats + offsetof(struct bge_stats, stat))
2289
2290 ifp->if_collisions +=
2291 (READ_STAT(sc, stats, dot3StatsSingleCollisionFrames.bge_addr_lo) +
2292 READ_STAT(sc, stats, dot3StatsMultipleCollisionFrames.bge_addr_lo) +
2293 READ_STAT(sc, stats, dot3StatsExcessiveCollisions.bge_addr_lo) +
2294 READ_STAT(sc, stats, dot3StatsLateCollisions.bge_addr_lo)) -
2295 ifp->if_collisions;
2296
2297 #undef READ_STAT
2298
2299 #ifdef notdef
2300 ifp->if_collisions +=
2301 (sc->bge_rdata->bge_info.bge_stats.dot3StatsSingleCollisionFrames +
2302 sc->bge_rdata->bge_info.bge_stats.dot3StatsMultipleCollisionFrames +
2303 sc->bge_rdata->bge_info.bge_stats.dot3StatsExcessiveCollisions +
2304 sc->bge_rdata->bge_info.bge_stats.dot3StatsLateCollisions) -
2305 ifp->if_collisions;
2306 #endif
2307 }
2308
2309 /*
2310 * Encapsulate an mbuf chain in the tx ring by coupling the mbuf data
2311 * pointers to descriptors.
2312 */
2313 int
2314 bge_encap(sc, m_head, txidx)
2315 struct bge_softc *sc;
2316 struct mbuf *m_head;
2317 u_int32_t *txidx;
2318 {
2319 struct bge_tx_bd *f = NULL;
2320 u_int32_t frag, cur, cnt = 0;
2321 u_int16_t csum_flags = 0;
2322 struct txdmamap_pool_entry *dma;
2323 bus_dmamap_t dmamap;
2324 int i = 0;
2325 struct mbuf *n;
2326
2327 cur = frag = *txidx;
2328
2329 if (m_head->m_pkthdr.csum_flags) {
2330 if (m_head->m_pkthdr.csum_flags & M_CSUM_IPv4)
2331 csum_flags |= BGE_TXBDFLAG_IP_CSUM;
2332 if (m_head->m_pkthdr.csum_flags & (M_CSUM_TCPv4| M_CSUM_UDPv4))
2333 csum_flags |= BGE_TXBDFLAG_TCP_UDP_CSUM;
2334 }
2335
2336 dma = SLIST_FIRST(&sc->txdma_list);
2337 if (dma == NULL)
2338 return ENOBUFS;
2339 dmamap = dma->dmamap;
2340
2341 /*
2342 * Start packing the mbufs in this chain into
2343 * the fragment pointers. Stop when we run out
2344 * of fragments or hit the end of the mbuf chain.
2345 */
2346 if (bus_dmamap_load_mbuf(sc->bge_dmatag, dmamap, m_head,
2347 BUS_DMA_NOWAIT))
2348 return(ENOBUFS);
2349
2350 for (i = 0; i < dmamap->dm_nsegs; i++) {
2351 f = &sc->bge_rdata->bge_tx_ring[frag];
2352 if (sc->bge_cdata.bge_tx_chain[frag] != NULL)
2353 break;
2354 bge_set_hostaddr(&f->bge_addr, dmamap->dm_segs[i].ds_addr);
2355 f->bge_len = dmamap->dm_segs[i].ds_len;
2356 f->bge_flags = csum_flags;
2357
2358 n = m_aux_find(m_head, AF_LINK, ETHERTYPE_VLAN);
2359 if (n != NULL) {
2360 f->bge_flags |= BGE_TXBDFLAG_VLAN_TAG;
2361 f->bge_vlan_tag = *mtod(n, int *);
2362 } else {
2363 f->bge_vlan_tag = 0;
2364 }
2365 /*
2366 * Sanity check: avoid coming within 16 descriptors
2367 * of the end of the ring.
2368 */
2369 if ((BGE_TX_RING_CNT - (sc->bge_txcnt + cnt)) < 16)
2370 return(ENOBUFS);
2371 cur = frag;
2372 BGE_INC(frag, BGE_TX_RING_CNT);
2373 cnt++;
2374 }
2375
2376 if (i < dmamap->dm_nsegs)
2377 return ENOBUFS;
2378
2379 bus_dmamap_sync(sc->bge_dmatag, dmamap, 0, dmamap->dm_mapsize,
2380 BUS_DMASYNC_PREWRITE);
2381
2382 if (frag == sc->bge_tx_saved_considx)
2383 return(ENOBUFS);
2384
2385 sc->bge_rdata->bge_tx_ring[cur].bge_flags |= BGE_TXBDFLAG_END;
2386 sc->bge_cdata.bge_tx_chain[cur] = m_head;
2387 SLIST_REMOVE_HEAD(&sc->txdma_list, link);
2388 sc->txdma[cur] = dma;
2389 sc->bge_txcnt += cnt;
2390
2391 *txidx = frag;
2392
2393 return(0);
2394 }
2395
2396 /*
2397 * Main transmit routine. To avoid having to do mbuf copies, we put pointers
2398 * to the mbuf data regions directly in the transmit descriptors.
2399 */
2400 void
2401 bge_start(ifp)
2402 struct ifnet *ifp;
2403 {
2404 struct bge_softc *sc;
2405 struct mbuf *m_head = NULL;
2406 u_int32_t prodidx = 0;
2407 int pkts = 0;
2408
2409 sc = ifp->if_softc;
2410
2411 if (!sc->bge_link && ifp->if_snd.ifq_len < 10)
2412 return;
2413
2414 prodidx = CSR_READ_4(sc, BGE_MBX_TX_HOST_PROD0_LO);
2415
2416 while(sc->bge_cdata.bge_tx_chain[prodidx] == NULL) {
2417 IFQ_POLL(&ifp->if_snd, m_head);
2418 if (m_head == NULL)
2419 break;
2420
2421 #if 0
2422 /*
2423 * XXX
2424 * safety overkill. If this is a fragmented packet chain
2425 * with delayed TCP/UDP checksums, then only encapsulate
2426 * it if we have enough descriptors to handle the entire
2427 * chain at once.
2428 * (paranoia -- may not actually be needed)
2429 */
2430 if (m_head->m_flags & M_FIRSTFRAG &&
2431 m_head->m_pkthdr.csum_flags & (CSUM_DELAY_DATA)) {
2432 if ((BGE_TX_RING_CNT - sc->bge_txcnt) <
2433 m_head->m_pkthdr.csum_data + 16) {
2434 ifp->if_flags |= IFF_OACTIVE;
2435 break;
2436 }
2437 }
2438 #endif
2439
2440 /*
2441 * Pack the data into the transmit ring. If we
2442 * don't have room, set the OACTIVE flag and wait
2443 * for the NIC to drain the ring.
2444 */
2445 if (bge_encap(sc, m_head, &prodidx)) {
2446 ifp->if_flags |= IFF_OACTIVE;
2447 break;
2448 }
2449
2450 /* now we are committed to transmit the packet */
2451 IFQ_DEQUEUE(&ifp->if_snd, m_head);
2452 pkts++;
2453
2454 #if NBPFILTER > 0
2455 /*
2456 * If there's a BPF listener, bounce a copy of this frame
2457 * to him.
2458 */
2459 if (ifp->if_bpf)
2460 bpf_mtap(ifp->if_bpf, m_head);
2461 #endif
2462 }
2463 if (pkts == 0)
2464 return;
2465
2466 /* Transmit */
2467 CSR_WRITE_4(sc, BGE_MBX_TX_HOST_PROD0_LO, prodidx);
2468
2469 /*
2470 * Set a timeout in case the chip goes out to lunch.
2471 */
2472 ifp->if_timer = 5;
2473 }
2474
2475 /*
2476 * If we have a BCM5400 or BCM5401 PHY, we need to properly
2477 * program its internal DSP. Failing to do this can result in
2478 * massive packet loss at 1Gb speeds.
2479 */
2480 void
2481 bge_phy_hack(sc)
2482 struct bge_softc *sc;
2483 {
2484 struct bge_bcom_hack bhack[] = {
2485 { BRGPHY_MII_AUXCTL, 0x4C20 },
2486 { BRGPHY_MII_DSP_ADDR_REG, 0x0012 },
2487 { BRGPHY_MII_DSP_RW_PORT, 0x1804 },
2488 { BRGPHY_MII_DSP_ADDR_REG, 0x0013 },
2489 { BRGPHY_MII_DSP_RW_PORT, 0x1204 },
2490 { BRGPHY_MII_DSP_ADDR_REG, 0x8006 },
2491 { BRGPHY_MII_DSP_RW_PORT, 0x0132 },
2492 { BRGPHY_MII_DSP_ADDR_REG, 0x8006 },
2493 { BRGPHY_MII_DSP_RW_PORT, 0x0232 },
2494 { BRGPHY_MII_DSP_ADDR_REG, 0x201F },
2495 { BRGPHY_MII_DSP_RW_PORT, 0x0A20 },
2496 { 0, 0 } };
2497 u_int16_t vid, did;
2498 int i;
2499
2500 vid = bge_miibus_readreg(&sc->bge_dev, 1, MII_PHYIDR1);
2501 did = bge_miibus_readreg(&sc->bge_dev, 1, MII_PHYIDR2);
2502
2503 if (MII_OUI(vid, did) == MII_OUI_BROADCOM &&
2504 (MII_MODEL(did) == MII_MODEL_BROADCOM_BCM5400 ||
2505 MII_MODEL(did) == MII_MODEL_BROADCOM_BCM5401)) {
2506 i = 0;
2507 while (bhack[i].reg) {
2508 bge_miibus_writereg(&sc->bge_dev, 1, bhack[i].reg,
2509 bhack[i].val);
2510 i++;
2511 }
2512 }
2513 }
2514
2515 int
2516 bge_init(ifp)
2517 struct ifnet *ifp;
2518 {
2519 struct bge_softc *sc = ifp->if_softc;
2520 u_int16_t *m;
2521 int s, error;
2522
2523 s = splnet();
2524
2525 ifp = &sc->ethercom.ec_if;
2526
2527 /* Cancel pending I/O and flush buffers. */
2528 bge_stop(sc);
2529 bge_reset(sc);
2530 bge_chipinit(sc);
2531
2532 /*
2533 * Init the various state machines, ring
2534 * control blocks and firmware.
2535 */
2536 error = bge_blockinit(sc);
2537 if (error != 0) {
2538 printf("%s: initialization error %d\n", sc->bge_dev.dv_xname,
2539 error);
2540 splx(s);
2541 return error;
2542 }
2543
2544 ifp = &sc->ethercom.ec_if;
2545
2546 /* Specify MTU. */
2547 CSR_WRITE_4(sc, BGE_RX_MTU, ifp->if_mtu +
2548 ETHER_HDR_LEN + ETHER_CRC_LEN);
2549
2550 /* Load our MAC address. */
2551 m = (u_int16_t *)&(LLADDR(ifp->if_sadl)[0]);
2552 CSR_WRITE_4(sc, BGE_MAC_ADDR1_LO, htons(m[0]));
2553 CSR_WRITE_4(sc, BGE_MAC_ADDR1_HI, (htons(m[1]) << 16) | htons(m[2]));
2554
2555 /* Enable or disable promiscuous mode as needed. */
2556 if (ifp->if_flags & IFF_PROMISC) {
2557 BGE_SETBIT(sc, BGE_RX_MODE, BGE_RXMODE_RX_PROMISC);
2558 } else {
2559 BGE_CLRBIT(sc, BGE_RX_MODE, BGE_RXMODE_RX_PROMISC);
2560 }
2561
2562 /* Program multicast filter. */
2563 bge_setmulti(sc);
2564
2565 /* Init RX ring. */
2566 bge_init_rx_ring_std(sc);
2567
2568 /* Init jumbo RX ring. */
2569 if (ifp->if_mtu > (ETHERMTU + ETHER_HDR_LEN + ETHER_CRC_LEN))
2570 bge_init_rx_ring_jumbo(sc);
2571
2572 /* Init our RX return ring index */
2573 sc->bge_rx_saved_considx = 0;
2574
2575 /* Init TX ring. */
2576 bge_init_tx_ring(sc);
2577
2578 /* Turn on transmitter */
2579 BGE_SETBIT(sc, BGE_TX_MODE, BGE_TXMODE_ENABLE);
2580
2581 /* Turn on receiver */
2582 BGE_SETBIT(sc, BGE_RX_MODE, BGE_RXMODE_ENABLE);
2583
2584 /* Tell firmware we're alive. */
2585 BGE_SETBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
2586
2587 /* Enable host interrupts. */
2588 BGE_SETBIT(sc, BGE_PCI_MISC_CTL, BGE_PCIMISCCTL_CLEAR_INTA);
2589 BGE_CLRBIT(sc, BGE_PCI_MISC_CTL, BGE_PCIMISCCTL_MASK_PCI_INTR);
2590 CSR_WRITE_4(sc, BGE_MBX_IRQ0_LO, 0);
2591
2592 bge_ifmedia_upd(ifp);
2593
2594 ifp->if_flags |= IFF_RUNNING;
2595 ifp->if_flags &= ~IFF_OACTIVE;
2596
2597 splx(s);
2598
2599 callout_reset(&sc->bge_timeout, hz, bge_tick, sc);
2600
2601 return 0;
2602 }
2603
2604 /*
2605 * Set media options.
2606 */
2607 int
2608 bge_ifmedia_upd(ifp)
2609 struct ifnet *ifp;
2610 {
2611 struct bge_softc *sc = ifp->if_softc;
2612 struct mii_data *mii = &sc->bge_mii;
2613 struct ifmedia *ifm = &sc->bge_ifmedia;
2614
2615 /* If this is a 1000baseX NIC, enable the TBI port. */
2616 if (sc->bge_tbi) {
2617 if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
2618 return(EINVAL);
2619 switch(IFM_SUBTYPE(ifm->ifm_media)) {
2620 case IFM_AUTO:
2621 break;
2622 case IFM_1000_SX:
2623 if ((ifm->ifm_media & IFM_GMASK) == IFM_FDX) {
2624 BGE_CLRBIT(sc, BGE_MAC_MODE,
2625 BGE_MACMODE_HALF_DUPLEX);
2626 } else {
2627 BGE_SETBIT(sc, BGE_MAC_MODE,
2628 BGE_MACMODE_HALF_DUPLEX);
2629 }
2630 break;
2631 default:
2632 return(EINVAL);
2633 }
2634 return(0);
2635 }
2636
2637 sc->bge_link = 0;
2638 if (mii->mii_instance) {
2639 struct mii_softc *miisc;
2640 for (miisc = LIST_FIRST(&mii->mii_phys); miisc != NULL;
2641 miisc = LIST_NEXT(miisc, mii_list))
2642 mii_phy_reset(miisc);
2643 }
2644 bge_phy_hack(sc);
2645 mii_mediachg(mii);
2646
2647 return(0);
2648 }
2649
2650 /*
2651 * Report current media status.
2652 */
2653 void
2654 bge_ifmedia_sts(ifp, ifmr)
2655 struct ifnet *ifp;
2656 struct ifmediareq *ifmr;
2657 {
2658 struct bge_softc *sc = ifp->if_softc;
2659 struct mii_data *mii = &sc->bge_mii;
2660
2661 if (sc->bge_tbi) {
2662 ifmr->ifm_status = IFM_AVALID;
2663 ifmr->ifm_active = IFM_ETHER;
2664 if (CSR_READ_4(sc, BGE_MAC_STS) &
2665 BGE_MACSTAT_TBI_PCS_SYNCHED)
2666 ifmr->ifm_status |= IFM_ACTIVE;
2667 ifmr->ifm_active |= IFM_1000_SX;
2668 if (CSR_READ_4(sc, BGE_MAC_MODE) & BGE_MACMODE_HALF_DUPLEX)
2669 ifmr->ifm_active |= IFM_HDX;
2670 else
2671 ifmr->ifm_active |= IFM_FDX;
2672 return;
2673 }
2674
2675 mii_pollstat(mii);
2676 ifmr->ifm_active = mii->mii_media_active;
2677 ifmr->ifm_status = mii->mii_media_status;
2678 }
2679
2680 int
2681 bge_ioctl(ifp, command, data)
2682 struct ifnet *ifp;
2683 u_long command;
2684 caddr_t data;
2685 {
2686 struct bge_softc *sc = ifp->if_softc;
2687 struct ifreq *ifr = (struct ifreq *) data;
2688 int s, error = 0;
2689 struct mii_data *mii;
2690
2691 s = splnet();
2692
2693 switch(command) {
2694 case SIOCSIFFLAGS:
2695 if (ifp->if_flags & IFF_UP) {
2696 /*
2697 * If only the state of the PROMISC flag changed,
2698 * then just use the 'set promisc mode' command
2699 * instead of reinitializing the entire NIC. Doing
2700 * a full re-init means reloading the firmware and
2701 * waiting for it to start up, which may take a
2702 * second or two.
2703 */
2704 if (ifp->if_flags & IFF_RUNNING &&
2705 ifp->if_flags & IFF_PROMISC &&
2706 !(sc->bge_if_flags & IFF_PROMISC)) {
2707 BGE_SETBIT(sc, BGE_RX_MODE,
2708 BGE_RXMODE_RX_PROMISC);
2709 } else if (ifp->if_flags & IFF_RUNNING &&
2710 !(ifp->if_flags & IFF_PROMISC) &&
2711 sc->bge_if_flags & IFF_PROMISC) {
2712 BGE_CLRBIT(sc, BGE_RX_MODE,
2713 BGE_RXMODE_RX_PROMISC);
2714 } else
2715 bge_init(ifp);
2716 } else {
2717 if (ifp->if_flags & IFF_RUNNING) {
2718 bge_stop(sc);
2719 }
2720 }
2721 sc->bge_if_flags = ifp->if_flags;
2722 error = 0;
2723 break;
2724 case SIOCSIFMEDIA:
2725 case SIOCGIFMEDIA:
2726 if (sc->bge_tbi) {
2727 error = ifmedia_ioctl(ifp, ifr, &sc->bge_ifmedia,
2728 command);
2729 } else {
2730 mii = &sc->bge_mii;
2731 error = ifmedia_ioctl(ifp, ifr, &mii->mii_media,
2732 command);
2733 }
2734 error = 0;
2735 break;
2736 default:
2737 error = ether_ioctl(ifp, command, data);
2738 if (error == ENETRESET) {
2739 bge_setmulti(sc);
2740 error = 0;
2741 }
2742 break;
2743 }
2744
2745 splx(s);
2746
2747 return(error);
2748 }
2749
2750 void
2751 bge_watchdog(ifp)
2752 struct ifnet *ifp;
2753 {
2754 struct bge_softc *sc;
2755
2756 sc = ifp->if_softc;
2757
2758 printf("%s: watchdog timeout -- resetting\n", sc->bge_dev.dv_xname);
2759
2760 ifp->if_flags &= ~IFF_RUNNING;
2761 bge_init(ifp);
2762
2763 ifp->if_oerrors++;
2764 }
2765
2766 /*
2767 * Stop the adapter and free any mbufs allocated to the
2768 * RX and TX lists.
2769 */
2770 void
2771 bge_stop(sc)
2772 struct bge_softc *sc;
2773 {
2774 struct ifnet *ifp = &sc->ethercom.ec_if;
2775
2776 callout_stop(&sc->bge_timeout);
2777
2778 /*
2779 * Disable all of the receiver blocks
2780 */
2781 BGE_CLRBIT(sc, BGE_RX_MODE, BGE_RXMODE_ENABLE);
2782 BGE_CLRBIT(sc, BGE_RBDI_MODE, BGE_RBDIMODE_ENABLE);
2783 BGE_CLRBIT(sc, BGE_RXLP_MODE, BGE_RXLPMODE_ENABLE);
2784 BGE_CLRBIT(sc, BGE_RXLS_MODE, BGE_RXLSMODE_ENABLE);
2785 BGE_CLRBIT(sc, BGE_RDBDI_MODE, BGE_RBDIMODE_ENABLE);
2786 BGE_CLRBIT(sc, BGE_RDC_MODE, BGE_RDCMODE_ENABLE);
2787 BGE_CLRBIT(sc, BGE_RBDC_MODE, BGE_RBDCMODE_ENABLE);
2788
2789 /*
2790 * Disable all of the transmit blocks
2791 */
2792 BGE_CLRBIT(sc, BGE_SRS_MODE, BGE_SRSMODE_ENABLE);
2793 BGE_CLRBIT(sc, BGE_SBDI_MODE, BGE_SBDIMODE_ENABLE);
2794 BGE_CLRBIT(sc, BGE_SDI_MODE, BGE_SDIMODE_ENABLE);
2795 BGE_CLRBIT(sc, BGE_RDMA_MODE, BGE_RDMAMODE_ENABLE);
2796 BGE_CLRBIT(sc, BGE_SDC_MODE, BGE_SDCMODE_ENABLE);
2797 BGE_CLRBIT(sc, BGE_DMAC_MODE, BGE_DMACMODE_ENABLE);
2798 BGE_CLRBIT(sc, BGE_SBDC_MODE, BGE_SBDCMODE_ENABLE);
2799
2800 /*
2801 * Shut down all of the memory managers and related
2802 * state machines.
2803 */
2804 BGE_CLRBIT(sc, BGE_HCC_MODE, BGE_HCCMODE_ENABLE);
2805 BGE_CLRBIT(sc, BGE_WDMA_MODE, BGE_WDMAMODE_ENABLE);
2806 BGE_CLRBIT(sc, BGE_MBCF_MODE, BGE_MBCFMODE_ENABLE);
2807 CSR_WRITE_4(sc, BGE_FTQ_RESET, 0xFFFFFFFF);
2808 CSR_WRITE_4(sc, BGE_FTQ_RESET, 0);
2809 BGE_CLRBIT(sc, BGE_BMAN_MODE, BGE_BMANMODE_ENABLE);
2810 BGE_CLRBIT(sc, BGE_MARB_MODE, BGE_MARBMODE_ENABLE);
2811
2812 /* Disable host interrupts. */
2813 BGE_SETBIT(sc, BGE_PCI_MISC_CTL, BGE_PCIMISCCTL_MASK_PCI_INTR);
2814 CSR_WRITE_4(sc, BGE_MBX_IRQ0_LO, 1);
2815
2816 /*
2817 * Tell firmware we're shutting down.
2818 */
2819 BGE_CLRBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
2820
2821 /* Free the RX lists. */
2822 bge_free_rx_ring_std(sc);
2823
2824 /* Free jumbo RX list. */
2825 bge_free_rx_ring_jumbo(sc);
2826
2827 /* Free TX buffers. */
2828 bge_free_tx_ring(sc);
2829
2830 /*
2831 * Isolate/power down the PHY.
2832 */
2833 if (!sc->bge_tbi)
2834 mii_down(&sc->bge_mii);
2835
2836 sc->bge_link = 0;
2837
2838 sc->bge_tx_saved_considx = BGE_TXCONS_UNSET;
2839
2840 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
2841 }
2842
2843 /*
2844 * Stop all chip I/O so that the kernel's probe routines don't
2845 * get confused by errant DMAs when rebooting.
2846 */
2847 void
2848 bge_shutdown(xsc)
2849 void *xsc;
2850 {
2851 struct bge_softc *sc = (struct bge_softc *)xsc;
2852
2853 bge_stop(sc);
2854 bge_reset(sc);
2855 }
2856