if_bge.c revision 1.71 1 /* $NetBSD: if_bge.c,v 1.71 2004/05/15 21:58:40 thorpej Exp $ */
2
3 /*
4 * Copyright (c) 2001 Wind River Systems
5 * Copyright (c) 1997, 1998, 1999, 2001
6 * Bill Paul <wpaul (at) windriver.com>. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by Bill Paul.
19 * 4. Neither the name of the author nor the names of any co-contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
27 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
33 * THE POSSIBILITY OF SUCH DAMAGE.
34 *
35 * $FreeBSD: if_bge.c,v 1.13 2002/04/04 06:01:31 wpaul Exp $
36 */
37
38 /*
39 * Broadcom BCM570x family gigabit ethernet driver for NetBSD.
40 *
41 * NetBSD version by:
42 *
43 * Frank van der Linden <fvdl (at) wasabisystems.com>
44 * Jason Thorpe <thorpej (at) wasabisystems.com>
45 * Jonathan Stone <jonathan (at) dsg.stanford.edu>
46 *
47 * Originally written for FreeBSD by Bill Paul <wpaul (at) windriver.com>
48 * Senior Engineer, Wind River Systems
49 */
50
51 /*
52 * The Broadcom BCM5700 is based on technology originally developed by
53 * Alteon Networks as part of the Tigon I and Tigon II gigabit ethernet
54 * MAC chips. The BCM5700, sometimes refered to as the Tigon III, has
55 * two on-board MIPS R4000 CPUs and can have as much as 16MB of external
56 * SSRAM. The BCM5700 supports TCP, UDP and IP checksum offload, jumbo
57 * frames, highly configurable RX filtering, and 16 RX and TX queues
58 * (which, along with RX filter rules, can be used for QOS applications).
59 * Other features, such as TCP segmentation, may be available as part
60 * of value-added firmware updates. Unlike the Tigon I and Tigon II,
61 * firmware images can be stored in hardware and need not be compiled
62 * into the driver.
63 *
64 * The BCM5700 supports the PCI v2.2 and PCI-X v1.0 standards, and will
65 * function in a 32-bit/64-bit 33/66MHz bus, or a 64-bit/133MHz bus.
66 *
67 * The BCM5701 is a single-chip solution incorporating both the BCM5700
68 * MAC and a BCM5401 10/100/1000 PHY. Unlike the BCM5700, the BCM5701
69 * does not support external SSRAM.
70 *
71 * Broadcom also produces a variation of the BCM5700 under the "Altima"
72 * brand name, which is functionally similar but lacks PCI-X support.
73 *
74 * Without external SSRAM, you can only have at most 4 TX rings,
75 * and the use of the mini RX ring is disabled. This seems to imply
76 * that these features are simply not available on the BCM5701. As a
77 * result, this driver does not implement any support for the mini RX
78 * ring.
79 */
80
81 #include <sys/cdefs.h>
82 __KERNEL_RCSID(0, "$NetBSD: if_bge.c,v 1.71 2004/05/15 21:58:40 thorpej Exp $");
83
84 #include "bpfilter.h"
85 #include "vlan.h"
86
87 #include <sys/param.h>
88 #include <sys/systm.h>
89 #include <sys/callout.h>
90 #include <sys/sockio.h>
91 #include <sys/mbuf.h>
92 #include <sys/malloc.h>
93 #include <sys/kernel.h>
94 #include <sys/device.h>
95 #include <sys/socket.h>
96 #include <sys/sysctl.h>
97
98 #include <net/if.h>
99 #include <net/if_dl.h>
100 #include <net/if_media.h>
101 #include <net/if_ether.h>
102
103 #ifdef INET
104 #include <netinet/in.h>
105 #include <netinet/in_systm.h>
106 #include <netinet/in_var.h>
107 #include <netinet/ip.h>
108 #endif
109
110 #if NBPFILTER > 0
111 #include <net/bpf.h>
112 #endif
113
114 #include <dev/pci/pcireg.h>
115 #include <dev/pci/pcivar.h>
116 #include <dev/pci/pcidevs.h>
117
118 #include <dev/mii/mii.h>
119 #include <dev/mii/miivar.h>
120 #include <dev/mii/miidevs.h>
121 #include <dev/mii/brgphyreg.h>
122
123 #include <dev/pci/if_bgereg.h>
124
125 #include <uvm/uvm_extern.h>
126
127 #define ETHER_MIN_NOPAD (ETHER_MIN_LEN - ETHER_CRC_LEN) /* i.e., 60 */
128
129
130 /*
131 * Tunable thresholds for rx-side bge interrupt mitigation.
132 */
133
134 /*
135 * The pairs of values below were obtained from empirical measurement
136 * on bcm5700 rev B2; they ar designed to give roughly 1 receive
137 * interrupt for every N packets received, where N is, approximately,
138 * the second value (rx_max_bds) in each pair. The values are chosen
139 * such that moving from one pair to the succeeding pair was observed
140 * to roughly halve interrupt rate under sustained input packet load.
141 * The values were empirically chosen to avoid overflowing internal
142 * limits on the bcm5700: inreasing rx_ticks much beyond 600
143 * results in internal wrapping and higher interrupt rates.
144 * The limit of 46 frames was chosen to match NFS workloads.
145 *
146 * These values also work well on bcm5701, bcm5704C, and (less
147 * tested) bcm5703. On other chipsets, (including the Altima chip
148 * family), the larger values may overflow internal chip limits,
149 * leading to increasing interrupt rates rather than lower interrupt
150 * rates.
151 *
152 * Applications using heavy interrupt mitigation (interrupting every
153 * 32 or 46 frames) in both directions may need to increase the TCP
154 * windowsize to above 131072 bytes (e.g., to 199608 bytes) to sustain
155 * full link bandwidth, due to ACKs and window updates lingering
156 * in the RX queue during the 30-to-40-frame interrupt-mitigation window.
157 */
158 struct bge_load_rx_thresh {
159 int rx_ticks;
160 int rx_max_bds; }
161 bge_rx_threshes[] = {
162 { 32, 2 },
163 { 50, 4 },
164 { 100, 8 },
165 { 192, 16 },
166 { 416, 32 },
167 { 598, 46 }
168 };
169 #define NBGE_RX_THRESH (sizeof(bge_rx_threshes) / sizeof(bge_rx_threshes[0]))
170
171 /* XXX patchable; should be sysctl'able */
172 static int bge_auto_thresh = 1;
173 static int bge_rx_thresh_lvl;
174
175 #ifdef __NetBSD__
176 static int bge_rxthresh_nodenum;
177 #endif /* __NetBSD__ */
178
179 int bge_probe(struct device *, struct cfdata *, void *);
180 void bge_attach(struct device *, struct device *, void *);
181 void bge_release_resources(struct bge_softc *);
182 void bge_txeof(struct bge_softc *);
183 void bge_rxeof(struct bge_softc *);
184
185 void bge_tick(void *);
186 void bge_stats_update(struct bge_softc *);
187 int bge_encap(struct bge_softc *, struct mbuf *, u_int32_t *);
188 static __inline int bge_cksum_pad(struct mbuf *pkt);
189 static __inline int bge_compact_dma_runt(struct mbuf *pkt);
190
191 int bge_intr(void *);
192 void bge_start(struct ifnet *);
193 int bge_ioctl(struct ifnet *, u_long, caddr_t);
194 int bge_init(struct ifnet *);
195 void bge_stop(struct bge_softc *);
196 void bge_watchdog(struct ifnet *);
197 void bge_shutdown(void *);
198 int bge_ifmedia_upd(struct ifnet *);
199 void bge_ifmedia_sts(struct ifnet *, struct ifmediareq *);
200
201 u_int8_t bge_eeprom_getbyte(struct bge_softc *, int, u_int8_t *);
202 int bge_read_eeprom(struct bge_softc *, caddr_t, int, int);
203
204 void bge_setmulti(struct bge_softc *);
205
206 void bge_handle_events(struct bge_softc *);
207 int bge_alloc_jumbo_mem(struct bge_softc *);
208 void bge_free_jumbo_mem(struct bge_softc *);
209 void *bge_jalloc(struct bge_softc *);
210 void bge_jfree(struct mbuf *, caddr_t, size_t, void *);
211 int bge_newbuf_std(struct bge_softc *, int, struct mbuf *, bus_dmamap_t);
212 int bge_newbuf_jumbo(struct bge_softc *, int, struct mbuf *);
213 int bge_init_rx_ring_std(struct bge_softc *);
214 void bge_free_rx_ring_std(struct bge_softc *);
215 int bge_init_rx_ring_jumbo(struct bge_softc *);
216 void bge_free_rx_ring_jumbo(struct bge_softc *);
217 void bge_free_tx_ring(struct bge_softc *);
218 int bge_init_tx_ring(struct bge_softc *);
219
220 int bge_chipinit(struct bge_softc *);
221 int bge_blockinit(struct bge_softc *);
222 int bge_setpowerstate(struct bge_softc *, int);
223
224 #ifdef notdef
225 u_int8_t bge_vpd_readbyte(struct bge_softc *, int);
226 void bge_vpd_read_res(struct bge_softc *, struct vpd_res *, int);
227 void bge_vpd_read(struct bge_softc *);
228 #endif
229
230 u_int32_t bge_readmem_ind(struct bge_softc *, int);
231 void bge_writemem_ind(struct bge_softc *, int, int);
232 #ifdef notdef
233 u_int32_t bge_readreg_ind(struct bge_softc *, int);
234 #endif
235 void bge_writereg_ind(struct bge_softc *, int, int);
236
237 int bge_miibus_readreg(struct device *, int, int);
238 void bge_miibus_writereg(struct device *, int, int, int);
239 void bge_miibus_statchg(struct device *);
240
241 void bge_reset(struct bge_softc *);
242
243 void bge_set_thresh(struct ifnet * /*ifp*/, int /*lvl*/);
244 void bge_update_all_threshes(int /*lvl*/);
245
246 void bge_dump_status(struct bge_softc *);
247 void bge_dump_rxbd(struct bge_rx_bd *);
248
249 #define BGE_DEBUG
250 #ifdef BGE_DEBUG
251 #define DPRINTF(x) if (bgedebug) printf x
252 #define DPRINTFN(n,x) if (bgedebug >= (n)) printf x
253 int bgedebug = 0;
254 #else
255 #define DPRINTF(x)
256 #define DPRINTFN(n,x)
257 #endif
258
259 /* Various chip quirks. */
260 #define BGE_QUIRK_LINK_STATE_BROKEN 0x00000001
261 #define BGE_QUIRK_CSUM_BROKEN 0x00000002
262 #define BGE_QUIRK_ONLY_PHY_1 0x00000004
263 #define BGE_QUIRK_5700_SMALLDMA 0x00000008
264 #define BGE_QUIRK_5700_PCIX_REG_BUG 0x00000010
265 #define BGE_QUIRK_PRODUCER_BUG 0x00000020
266 #define BGE_QUIRK_PCIX_DMA_ALIGN_BUG 0x00000040
267 #define BGE_QUIRK_5705_CORE 0x00000080
268 #define BGE_QUIRK_FEWER_MBUFS 0x00000100
269
270 /* following bugs are common to bcm5700 rev B, all flavours */
271 #define BGE_QUIRK_5700_COMMON \
272 (BGE_QUIRK_5700_SMALLDMA|BGE_QUIRK_PRODUCER_BUG)
273
274 CFATTACH_DECL(bge, sizeof(struct bge_softc),
275 bge_probe, bge_attach, NULL, NULL);
276
277 u_int32_t
278 bge_readmem_ind(sc, off)
279 struct bge_softc *sc;
280 int off;
281 {
282 struct pci_attach_args *pa = &(sc->bge_pa);
283 pcireg_t val;
284
285 pci_conf_write(pa->pa_pc, pa->pa_tag, BGE_PCI_MEMWIN_BASEADDR, off);
286 val = pci_conf_read(pa->pa_pc, pa->pa_tag, BGE_PCI_MEMWIN_DATA);
287 return val;
288 }
289
290 void
291 bge_writemem_ind(sc, off, val)
292 struct bge_softc *sc;
293 int off, val;
294 {
295 struct pci_attach_args *pa = &(sc->bge_pa);
296
297 pci_conf_write(pa->pa_pc, pa->pa_tag, BGE_PCI_MEMWIN_BASEADDR, off);
298 pci_conf_write(pa->pa_pc, pa->pa_tag, BGE_PCI_MEMWIN_DATA, val);
299 }
300
301 #ifdef notdef
302 u_int32_t
303 bge_readreg_ind(sc, off)
304 struct bge_softc *sc;
305 int off;
306 {
307 struct pci_attach_args *pa = &(sc->bge_pa);
308
309 pci_conf_write(pa->pa_pc, pa->pa_tag, BGE_PCI_REG_BASEADDR, off);
310 return(pci_conf_read(pa->pa_pc, pa->pa_tag, BGE_PCI_REG_DATA));
311 }
312 #endif
313
314 void
315 bge_writereg_ind(sc, off, val)
316 struct bge_softc *sc;
317 int off, val;
318 {
319 struct pci_attach_args *pa = &(sc->bge_pa);
320
321 pci_conf_write(pa->pa_pc, pa->pa_tag, BGE_PCI_REG_BASEADDR, off);
322 pci_conf_write(pa->pa_pc, pa->pa_tag, BGE_PCI_REG_DATA, val);
323 }
324
325 #ifdef notdef
326 u_int8_t
327 bge_vpd_readbyte(sc, addr)
328 struct bge_softc *sc;
329 int addr;
330 {
331 int i;
332 u_int32_t val;
333 struct pci_attach_args *pa = &(sc->bge_pa);
334
335 pci_conf_write(pa->pa_pc, pa->pa_tag, BGE_PCI_VPD_ADDR, addr);
336 for (i = 0; i < BGE_TIMEOUT * 10; i++) {
337 DELAY(10);
338 if (pci_conf_read(pa->pa_pc, pa->pa_tag, BGE_PCI_VPD_ADDR) &
339 BGE_VPD_FLAG)
340 break;
341 }
342
343 if (i == BGE_TIMEOUT) {
344 printf("%s: VPD read timed out\n", sc->bge_dev.dv_xname);
345 return(0);
346 }
347
348 val = pci_conf_read(pa->pa_pc, pa->pa_tag, BGE_PCI_VPD_DATA);
349
350 return((val >> ((addr % 4) * 8)) & 0xFF);
351 }
352
353 void
354 bge_vpd_read_res(sc, res, addr)
355 struct bge_softc *sc;
356 struct vpd_res *res;
357 int addr;
358 {
359 int i;
360 u_int8_t *ptr;
361
362 ptr = (u_int8_t *)res;
363 for (i = 0; i < sizeof(struct vpd_res); i++)
364 ptr[i] = bge_vpd_readbyte(sc, i + addr);
365 }
366
367 void
368 bge_vpd_read(sc)
369 struct bge_softc *sc;
370 {
371 int pos = 0, i;
372 struct vpd_res res;
373
374 if (sc->bge_vpd_prodname != NULL)
375 free(sc->bge_vpd_prodname, M_DEVBUF);
376 if (sc->bge_vpd_readonly != NULL)
377 free(sc->bge_vpd_readonly, M_DEVBUF);
378 sc->bge_vpd_prodname = NULL;
379 sc->bge_vpd_readonly = NULL;
380
381 bge_vpd_read_res(sc, &res, pos);
382
383 if (res.vr_id != VPD_RES_ID) {
384 printf("%s: bad VPD resource id: expected %x got %x\n",
385 sc->bge_dev.dv_xname, VPD_RES_ID, res.vr_id);
386 return;
387 }
388
389 pos += sizeof(res);
390 sc->bge_vpd_prodname = malloc(res.vr_len + 1, M_DEVBUF, M_NOWAIT);
391 if (sc->bge_vpd_prodname == NULL)
392 panic("bge_vpd_read");
393 for (i = 0; i < res.vr_len; i++)
394 sc->bge_vpd_prodname[i] = bge_vpd_readbyte(sc, i + pos);
395 sc->bge_vpd_prodname[i] = '\0';
396 pos += i;
397
398 bge_vpd_read_res(sc, &res, pos);
399
400 if (res.vr_id != VPD_RES_READ) {
401 printf("%s: bad VPD resource id: expected %x got %x\n",
402 sc->bge_dev.dv_xname, VPD_RES_READ, res.vr_id);
403 return;
404 }
405
406 pos += sizeof(res);
407 sc->bge_vpd_readonly = malloc(res.vr_len, M_DEVBUF, M_NOWAIT);
408 if (sc->bge_vpd_readonly == NULL)
409 panic("bge_vpd_read");
410 for (i = 0; i < res.vr_len + 1; i++)
411 sc->bge_vpd_readonly[i] = bge_vpd_readbyte(sc, i + pos);
412 }
413 #endif
414
415 /*
416 * Read a byte of data stored in the EEPROM at address 'addr.' The
417 * BCM570x supports both the traditional bitbang interface and an
418 * auto access interface for reading the EEPROM. We use the auto
419 * access method.
420 */
421 u_int8_t
422 bge_eeprom_getbyte(sc, addr, dest)
423 struct bge_softc *sc;
424 int addr;
425 u_int8_t *dest;
426 {
427 int i;
428 u_int32_t byte = 0;
429
430 /*
431 * Enable use of auto EEPROM access so we can avoid
432 * having to use the bitbang method.
433 */
434 BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_AUTO_EEPROM);
435
436 /* Reset the EEPROM, load the clock period. */
437 CSR_WRITE_4(sc, BGE_EE_ADDR,
438 BGE_EEADDR_RESET|BGE_EEHALFCLK(BGE_HALFCLK_384SCL));
439 DELAY(20);
440
441 /* Issue the read EEPROM command. */
442 CSR_WRITE_4(sc, BGE_EE_ADDR, BGE_EE_READCMD | addr);
443
444 /* Wait for completion */
445 for(i = 0; i < BGE_TIMEOUT * 10; i++) {
446 DELAY(10);
447 if (CSR_READ_4(sc, BGE_EE_ADDR) & BGE_EEADDR_DONE)
448 break;
449 }
450
451 if (i == BGE_TIMEOUT) {
452 printf("%s: eeprom read timed out\n", sc->bge_dev.dv_xname);
453 return(0);
454 }
455
456 /* Get result. */
457 byte = CSR_READ_4(sc, BGE_EE_DATA);
458
459 *dest = (byte >> ((addr % 4) * 8)) & 0xFF;
460
461 return(0);
462 }
463
464 /*
465 * Read a sequence of bytes from the EEPROM.
466 */
467 int
468 bge_read_eeprom(sc, dest, off, cnt)
469 struct bge_softc *sc;
470 caddr_t dest;
471 int off;
472 int cnt;
473 {
474 int err = 0, i;
475 u_int8_t byte = 0;
476
477 for (i = 0; i < cnt; i++) {
478 err = bge_eeprom_getbyte(sc, off + i, &byte);
479 if (err)
480 break;
481 *(dest + i) = byte;
482 }
483
484 return(err ? 1 : 0);
485 }
486
487 int
488 bge_miibus_readreg(dev, phy, reg)
489 struct device *dev;
490 int phy, reg;
491 {
492 struct bge_softc *sc = (struct bge_softc *)dev;
493 u_int32_t val;
494 u_int32_t saved_autopoll;
495 int i;
496
497 /*
498 * Several chips with builtin PHYs will incorrectly answer to
499 * other PHY instances than the builtin PHY at id 1.
500 */
501 if (phy != 1 && (sc->bge_quirks & BGE_QUIRK_ONLY_PHY_1))
502 return(0);
503
504 /* Reading with autopolling on may trigger PCI errors */
505 saved_autopoll = CSR_READ_4(sc, BGE_MI_MODE);
506 if (saved_autopoll & BGE_MIMODE_AUTOPOLL) {
507 CSR_WRITE_4(sc, BGE_MI_MODE,
508 saved_autopoll &~ BGE_MIMODE_AUTOPOLL);
509 DELAY(40);
510 }
511
512 CSR_WRITE_4(sc, BGE_MI_COMM, BGE_MICMD_READ|BGE_MICOMM_BUSY|
513 BGE_MIPHY(phy)|BGE_MIREG(reg));
514
515 for (i = 0; i < BGE_TIMEOUT; i++) {
516 val = CSR_READ_4(sc, BGE_MI_COMM);
517 if (!(val & BGE_MICOMM_BUSY))
518 break;
519 delay(10);
520 }
521
522 if (i == BGE_TIMEOUT) {
523 printf("%s: PHY read timed out\n", sc->bge_dev.dv_xname);
524 val = 0;
525 goto done;
526 }
527
528 val = CSR_READ_4(sc, BGE_MI_COMM);
529
530 done:
531 if (saved_autopoll & BGE_MIMODE_AUTOPOLL) {
532 CSR_WRITE_4(sc, BGE_MI_MODE, saved_autopoll);
533 DELAY(40);
534 }
535
536 if (val & BGE_MICOMM_READFAIL)
537 return(0);
538
539 return(val & 0xFFFF);
540 }
541
542 void
543 bge_miibus_writereg(dev, phy, reg, val)
544 struct device *dev;
545 int phy, reg, val;
546 {
547 struct bge_softc *sc = (struct bge_softc *)dev;
548 u_int32_t saved_autopoll;
549 int i;
550
551 /* Touching the PHY while autopolling is on may trigger PCI errors */
552 saved_autopoll = CSR_READ_4(sc, BGE_MI_MODE);
553 if (saved_autopoll & BGE_MIMODE_AUTOPOLL) {
554 delay(40);
555 CSR_WRITE_4(sc, BGE_MI_MODE,
556 saved_autopoll & (~BGE_MIMODE_AUTOPOLL));
557 delay(10); /* 40 usec is supposed to be adequate */
558 }
559
560 CSR_WRITE_4(sc, BGE_MI_COMM, BGE_MICMD_WRITE|BGE_MICOMM_BUSY|
561 BGE_MIPHY(phy)|BGE_MIREG(reg)|val);
562
563 for (i = 0; i < BGE_TIMEOUT; i++) {
564 if (!(CSR_READ_4(sc, BGE_MI_COMM) & BGE_MICOMM_BUSY))
565 break;
566 delay(10);
567 }
568
569 if (saved_autopoll & BGE_MIMODE_AUTOPOLL) {
570 CSR_WRITE_4(sc, BGE_MI_MODE, saved_autopoll);
571 delay(40);
572 }
573
574 if (i == BGE_TIMEOUT) {
575 printf("%s: PHY read timed out\n", sc->bge_dev.dv_xname);
576 }
577 }
578
579 void
580 bge_miibus_statchg(dev)
581 struct device *dev;
582 {
583 struct bge_softc *sc = (struct bge_softc *)dev;
584 struct mii_data *mii = &sc->bge_mii;
585
586 /*
587 * Get flow control negotiation result.
588 */
589 if (IFM_SUBTYPE(mii->mii_media.ifm_cur->ifm_media) == IFM_AUTO &&
590 (mii->mii_media_active & IFM_ETH_FMASK) != sc->bge_flowflags) {
591 sc->bge_flowflags = mii->mii_media_active & IFM_ETH_FMASK;
592 mii->mii_media_active &= ~IFM_ETH_FMASK;
593 }
594
595 BGE_CLRBIT(sc, BGE_MAC_MODE, BGE_MACMODE_PORTMODE);
596 if (IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_T) {
597 BGE_SETBIT(sc, BGE_MAC_MODE, BGE_PORTMODE_GMII);
598 } else {
599 BGE_SETBIT(sc, BGE_MAC_MODE, BGE_PORTMODE_MII);
600 }
601
602 if ((mii->mii_media_active & IFM_GMASK) == IFM_FDX) {
603 BGE_CLRBIT(sc, BGE_MAC_MODE, BGE_MACMODE_HALF_DUPLEX);
604 } else {
605 BGE_SETBIT(sc, BGE_MAC_MODE, BGE_MACMODE_HALF_DUPLEX);
606 }
607
608 /*
609 * 802.3x flow control
610 */
611 if (sc->bge_flowflags & IFM_ETH_RXPAUSE) {
612 BGE_SETBIT(sc, BGE_RX_MODE, BGE_RXMODE_FLOWCTL_ENABLE);
613 } else {
614 BGE_CLRBIT(sc, BGE_RX_MODE, BGE_RXMODE_FLOWCTL_ENABLE);
615 }
616 if (sc->bge_flowflags & IFM_ETH_TXPAUSE) {
617 BGE_SETBIT(sc, BGE_TX_MODE, BGE_TXMODE_FLOWCTL_ENABLE);
618 } else {
619 BGE_CLRBIT(sc, BGE_TX_MODE, BGE_TXMODE_FLOWCTL_ENABLE);
620 }
621 }
622
623 /*
624 * Update rx threshold levels to values in a particular slot
625 * of the interrupt-mitigation table bge_rx_threshes.
626 */
627 void
628 bge_set_thresh(struct ifnet *ifp, int lvl)
629 {
630 struct bge_softc *sc = ifp->if_softc;
631 int s;
632
633 /* For now, just save the new Rx-intr thresholds and record
634 * that a threshold update is pending. Updating the hardware
635 * registers here (even at splhigh()) is observed to
636 * occasionaly cause glitches where Rx-interrupts are not
637 * honoured for up to 10 seconds. jonathan (at) NetBSD.org, 2003-04-05
638 */
639 s = splnet();
640 sc->bge_rx_coal_ticks = bge_rx_threshes[lvl].rx_ticks;
641 sc->bge_rx_max_coal_bds = bge_rx_threshes[lvl].rx_max_bds;
642 sc->bge_pending_rxintr_change = 1;
643 splx(s);
644
645 return;
646 }
647
648
649 /*
650 * Update Rx thresholds of all bge devices
651 */
652 void
653 bge_update_all_threshes(int lvl)
654 {
655 struct ifnet *ifp;
656 const char * const namebuf = "bge";
657 int namelen;
658
659 if (lvl < 0)
660 lvl = 0;
661 else if( lvl >= NBGE_RX_THRESH)
662 lvl = NBGE_RX_THRESH - 1;
663
664 namelen = strlen(namebuf);
665 /*
666 * Now search all the interfaces for this name/number
667 */
668 TAILQ_FOREACH(ifp, &ifnet, if_list) {
669 if (strncmp(ifp->if_xname, namebuf, namelen) != 0)
670 continue;
671 /* We got a match: update if doing auto-threshold-tuning */
672 if (bge_auto_thresh)
673 bge_set_thresh(ifp, lvl);
674 }
675 }
676
677 /*
678 * Handle events that have triggered interrupts.
679 */
680 void
681 bge_handle_events(sc)
682 struct bge_softc *sc;
683 {
684
685 return;
686 }
687
688 /*
689 * Memory management for jumbo frames.
690 */
691
692 int
693 bge_alloc_jumbo_mem(sc)
694 struct bge_softc *sc;
695 {
696 caddr_t ptr, kva;
697 bus_dma_segment_t seg;
698 int i, rseg, state, error;
699 struct bge_jpool_entry *entry;
700
701 state = error = 0;
702
703 /* Grab a big chunk o' storage. */
704 if (bus_dmamem_alloc(sc->bge_dmatag, BGE_JMEM, PAGE_SIZE, 0,
705 &seg, 1, &rseg, BUS_DMA_NOWAIT)) {
706 printf("%s: can't alloc rx buffers\n", sc->bge_dev.dv_xname);
707 return ENOBUFS;
708 }
709
710 state = 1;
711 if (bus_dmamem_map(sc->bge_dmatag, &seg, rseg, BGE_JMEM, &kva,
712 BUS_DMA_NOWAIT)) {
713 printf("%s: can't map DMA buffers (%d bytes)\n",
714 sc->bge_dev.dv_xname, (int)BGE_JMEM);
715 error = ENOBUFS;
716 goto out;
717 }
718
719 state = 2;
720 if (bus_dmamap_create(sc->bge_dmatag, BGE_JMEM, 1, BGE_JMEM, 0,
721 BUS_DMA_NOWAIT, &sc->bge_cdata.bge_rx_jumbo_map)) {
722 printf("%s: can't create DMA map\n", sc->bge_dev.dv_xname);
723 error = ENOBUFS;
724 goto out;
725 }
726
727 state = 3;
728 if (bus_dmamap_load(sc->bge_dmatag, sc->bge_cdata.bge_rx_jumbo_map,
729 kva, BGE_JMEM, NULL, BUS_DMA_NOWAIT)) {
730 printf("%s: can't load DMA map\n", sc->bge_dev.dv_xname);
731 error = ENOBUFS;
732 goto out;
733 }
734
735 state = 4;
736 sc->bge_cdata.bge_jumbo_buf = (caddr_t)kva;
737 DPRINTFN(1,("bge_jumbo_buf = 0x%p\n", sc->bge_cdata.bge_jumbo_buf));
738
739 SLIST_INIT(&sc->bge_jfree_listhead);
740 SLIST_INIT(&sc->bge_jinuse_listhead);
741
742 /*
743 * Now divide it up into 9K pieces and save the addresses
744 * in an array.
745 */
746 ptr = sc->bge_cdata.bge_jumbo_buf;
747 for (i = 0; i < BGE_JSLOTS; i++) {
748 sc->bge_cdata.bge_jslots[i] = ptr;
749 ptr += BGE_JLEN;
750 entry = malloc(sizeof(struct bge_jpool_entry),
751 M_DEVBUF, M_NOWAIT);
752 if (entry == NULL) {
753 printf("%s: no memory for jumbo buffer queue!\n",
754 sc->bge_dev.dv_xname);
755 error = ENOBUFS;
756 goto out;
757 }
758 entry->slot = i;
759 SLIST_INSERT_HEAD(&sc->bge_jfree_listhead,
760 entry, jpool_entries);
761 }
762 out:
763 if (error != 0) {
764 switch (state) {
765 case 4:
766 bus_dmamap_unload(sc->bge_dmatag,
767 sc->bge_cdata.bge_rx_jumbo_map);
768 case 3:
769 bus_dmamap_destroy(sc->bge_dmatag,
770 sc->bge_cdata.bge_rx_jumbo_map);
771 case 2:
772 bus_dmamem_unmap(sc->bge_dmatag, kva, BGE_JMEM);
773 case 1:
774 bus_dmamem_free(sc->bge_dmatag, &seg, rseg);
775 break;
776 default:
777 break;
778 }
779 }
780
781 return error;
782 }
783
784 /*
785 * Allocate a jumbo buffer.
786 */
787 void *
788 bge_jalloc(sc)
789 struct bge_softc *sc;
790 {
791 struct bge_jpool_entry *entry;
792
793 entry = SLIST_FIRST(&sc->bge_jfree_listhead);
794
795 if (entry == NULL) {
796 printf("%s: no free jumbo buffers\n", sc->bge_dev.dv_xname);
797 return(NULL);
798 }
799
800 SLIST_REMOVE_HEAD(&sc->bge_jfree_listhead, jpool_entries);
801 SLIST_INSERT_HEAD(&sc->bge_jinuse_listhead, entry, jpool_entries);
802 return(sc->bge_cdata.bge_jslots[entry->slot]);
803 }
804
805 /*
806 * Release a jumbo buffer.
807 */
808 void
809 bge_jfree(m, buf, size, arg)
810 struct mbuf *m;
811 caddr_t buf;
812 size_t size;
813 void *arg;
814 {
815 struct bge_jpool_entry *entry;
816 struct bge_softc *sc;
817 int i, s;
818
819 /* Extract the softc struct pointer. */
820 sc = (struct bge_softc *)arg;
821
822 if (sc == NULL)
823 panic("bge_jfree: can't find softc pointer!");
824
825 /* calculate the slot this buffer belongs to */
826
827 i = ((caddr_t)buf
828 - (caddr_t)sc->bge_cdata.bge_jumbo_buf) / BGE_JLEN;
829
830 if ((i < 0) || (i >= BGE_JSLOTS))
831 panic("bge_jfree: asked to free buffer that we don't manage!");
832
833 s = splvm();
834 entry = SLIST_FIRST(&sc->bge_jinuse_listhead);
835 if (entry == NULL)
836 panic("bge_jfree: buffer not in use!");
837 entry->slot = i;
838 SLIST_REMOVE_HEAD(&sc->bge_jinuse_listhead, jpool_entries);
839 SLIST_INSERT_HEAD(&sc->bge_jfree_listhead, entry, jpool_entries);
840
841 if (__predict_true(m != NULL))
842 pool_cache_put(&mbpool_cache, m);
843 splx(s);
844 }
845
846
847 /*
848 * Intialize a standard receive ring descriptor.
849 */
850 int
851 bge_newbuf_std(sc, i, m, dmamap)
852 struct bge_softc *sc;
853 int i;
854 struct mbuf *m;
855 bus_dmamap_t dmamap;
856 {
857 struct mbuf *m_new = NULL;
858 struct bge_rx_bd *r;
859 int error;
860
861 if (dmamap == NULL) {
862 error = bus_dmamap_create(sc->bge_dmatag, MCLBYTES, 1,
863 MCLBYTES, 0, BUS_DMA_NOWAIT, &dmamap);
864 if (error != 0)
865 return error;
866 }
867
868 sc->bge_cdata.bge_rx_std_map[i] = dmamap;
869
870 if (m == NULL) {
871 MGETHDR(m_new, M_DONTWAIT, MT_DATA);
872 if (m_new == NULL) {
873 return(ENOBUFS);
874 }
875
876 MCLGET(m_new, M_DONTWAIT);
877 if (!(m_new->m_flags & M_EXT)) {
878 m_freem(m_new);
879 return(ENOBUFS);
880 }
881 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
882 if (!sc->bge_rx_alignment_bug)
883 m_adj(m_new, ETHER_ALIGN);
884
885 if (bus_dmamap_load_mbuf(sc->bge_dmatag, dmamap, m_new,
886 BUS_DMA_READ|BUS_DMA_NOWAIT))
887 return(ENOBUFS);
888 } else {
889 m_new = m;
890 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
891 m_new->m_data = m_new->m_ext.ext_buf;
892 if (!sc->bge_rx_alignment_bug)
893 m_adj(m_new, ETHER_ALIGN);
894 }
895
896 sc->bge_cdata.bge_rx_std_chain[i] = m_new;
897 r = &sc->bge_rdata->bge_rx_std_ring[i];
898 bge_set_hostaddr(&r->bge_addr,
899 dmamap->dm_segs[0].ds_addr);
900 r->bge_flags = BGE_RXBDFLAG_END;
901 r->bge_len = m_new->m_len;
902 r->bge_idx = i;
903
904 bus_dmamap_sync(sc->bge_dmatag, sc->bge_ring_map,
905 offsetof(struct bge_ring_data, bge_rx_std_ring) +
906 i * sizeof (struct bge_rx_bd),
907 sizeof (struct bge_rx_bd),
908 BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD);
909
910 return(0);
911 }
912
913 /*
914 * Initialize a jumbo receive ring descriptor. This allocates
915 * a jumbo buffer from the pool managed internally by the driver.
916 */
917 int
918 bge_newbuf_jumbo(sc, i, m)
919 struct bge_softc *sc;
920 int i;
921 struct mbuf *m;
922 {
923 struct mbuf *m_new = NULL;
924 struct bge_rx_bd *r;
925
926 if (m == NULL) {
927 caddr_t *buf = NULL;
928
929 /* Allocate the mbuf. */
930 MGETHDR(m_new, M_DONTWAIT, MT_DATA);
931 if (m_new == NULL) {
932 return(ENOBUFS);
933 }
934
935 /* Allocate the jumbo buffer */
936 buf = bge_jalloc(sc);
937 if (buf == NULL) {
938 m_freem(m_new);
939 printf("%s: jumbo allocation failed "
940 "-- packet dropped!\n", sc->bge_dev.dv_xname);
941 return(ENOBUFS);
942 }
943
944 /* Attach the buffer to the mbuf. */
945 m_new->m_len = m_new->m_pkthdr.len = BGE_JUMBO_FRAMELEN;
946 MEXTADD(m_new, buf, BGE_JUMBO_FRAMELEN, M_DEVBUF,
947 bge_jfree, sc);
948 } else {
949 m_new = m;
950 m_new->m_data = m_new->m_ext.ext_buf;
951 m_new->m_ext.ext_size = BGE_JUMBO_FRAMELEN;
952 }
953
954 if (!sc->bge_rx_alignment_bug)
955 m_adj(m_new, ETHER_ALIGN);
956 /* Set up the descriptor. */
957 r = &sc->bge_rdata->bge_rx_jumbo_ring[i];
958 sc->bge_cdata.bge_rx_jumbo_chain[i] = m_new;
959 bge_set_hostaddr(&r->bge_addr, BGE_JUMBO_DMA_ADDR(sc, m_new));
960 r->bge_flags = BGE_RXBDFLAG_END|BGE_RXBDFLAG_JUMBO_RING;
961 r->bge_len = m_new->m_len;
962 r->bge_idx = i;
963
964 bus_dmamap_sync(sc->bge_dmatag, sc->bge_ring_map,
965 offsetof(struct bge_ring_data, bge_rx_jumbo_ring) +
966 i * sizeof (struct bge_rx_bd),
967 sizeof (struct bge_rx_bd),
968 BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD);
969
970 return(0);
971 }
972
973 /*
974 * The standard receive ring has 512 entries in it. At 2K per mbuf cluster,
975 * that's 1MB or memory, which is a lot. For now, we fill only the first
976 * 256 ring entries and hope that our CPU is fast enough to keep up with
977 * the NIC.
978 */
979 int
980 bge_init_rx_ring_std(sc)
981 struct bge_softc *sc;
982 {
983 int i;
984
985 if (sc->bge_flags & BGE_RXRING_VALID)
986 return 0;
987
988 for (i = 0; i < BGE_SSLOTS; i++) {
989 if (bge_newbuf_std(sc, i, NULL, 0) == ENOBUFS)
990 return(ENOBUFS);
991 }
992
993 sc->bge_std = i - 1;
994 CSR_WRITE_4(sc, BGE_MBX_RX_STD_PROD_LO, sc->bge_std);
995
996 sc->bge_flags |= BGE_RXRING_VALID;
997
998 return(0);
999 }
1000
1001 void
1002 bge_free_rx_ring_std(sc)
1003 struct bge_softc *sc;
1004 {
1005 int i;
1006
1007 if (!(sc->bge_flags & BGE_RXRING_VALID))
1008 return;
1009
1010 for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
1011 if (sc->bge_cdata.bge_rx_std_chain[i] != NULL) {
1012 m_freem(sc->bge_cdata.bge_rx_std_chain[i]);
1013 sc->bge_cdata.bge_rx_std_chain[i] = NULL;
1014 bus_dmamap_destroy(sc->bge_dmatag,
1015 sc->bge_cdata.bge_rx_std_map[i]);
1016 }
1017 memset((char *)&sc->bge_rdata->bge_rx_std_ring[i], 0,
1018 sizeof(struct bge_rx_bd));
1019 }
1020
1021 sc->bge_flags &= ~BGE_RXRING_VALID;
1022 }
1023
1024 int
1025 bge_init_rx_ring_jumbo(sc)
1026 struct bge_softc *sc;
1027 {
1028 int i;
1029 volatile struct bge_rcb *rcb;
1030
1031 if (sc->bge_flags & BGE_JUMBO_RXRING_VALID)
1032 return 0;
1033
1034 for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
1035 if (bge_newbuf_jumbo(sc, i, NULL) == ENOBUFS)
1036 return(ENOBUFS);
1037 };
1038
1039 sc->bge_jumbo = i - 1;
1040 sc->bge_flags |= BGE_JUMBO_RXRING_VALID;
1041
1042 rcb = &sc->bge_rdata->bge_info.bge_jumbo_rx_rcb;
1043 rcb->bge_maxlen_flags = 0;
1044 CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_MAXLEN_FLAGS, rcb->bge_maxlen_flags);
1045
1046 CSR_WRITE_4(sc, BGE_MBX_RX_JUMBO_PROD_LO, sc->bge_jumbo);
1047
1048 return(0);
1049 }
1050
1051 void
1052 bge_free_rx_ring_jumbo(sc)
1053 struct bge_softc *sc;
1054 {
1055 int i;
1056
1057 if (!(sc->bge_flags & BGE_JUMBO_RXRING_VALID))
1058 return;
1059
1060 for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
1061 if (sc->bge_cdata.bge_rx_jumbo_chain[i] != NULL) {
1062 m_freem(sc->bge_cdata.bge_rx_jumbo_chain[i]);
1063 sc->bge_cdata.bge_rx_jumbo_chain[i] = NULL;
1064 }
1065 memset((char *)&sc->bge_rdata->bge_rx_jumbo_ring[i], 0,
1066 sizeof(struct bge_rx_bd));
1067 }
1068
1069 sc->bge_flags &= ~BGE_JUMBO_RXRING_VALID;
1070 }
1071
1072 void
1073 bge_free_tx_ring(sc)
1074 struct bge_softc *sc;
1075 {
1076 int i, freed;
1077 struct txdmamap_pool_entry *dma;
1078
1079 if (!(sc->bge_flags & BGE_TXRING_VALID))
1080 return;
1081
1082 freed = 0;
1083
1084 for (i = 0; i < BGE_TX_RING_CNT; i++) {
1085 if (sc->bge_cdata.bge_tx_chain[i] != NULL) {
1086 freed++;
1087 m_freem(sc->bge_cdata.bge_tx_chain[i]);
1088 sc->bge_cdata.bge_tx_chain[i] = NULL;
1089 SLIST_INSERT_HEAD(&sc->txdma_list, sc->txdma[i],
1090 link);
1091 sc->txdma[i] = 0;
1092 }
1093 memset((char *)&sc->bge_rdata->bge_tx_ring[i], 0,
1094 sizeof(struct bge_tx_bd));
1095 }
1096
1097 while ((dma = SLIST_FIRST(&sc->txdma_list))) {
1098 SLIST_REMOVE_HEAD(&sc->txdma_list, link);
1099 bus_dmamap_destroy(sc->bge_dmatag, dma->dmamap);
1100 free(dma, M_DEVBUF);
1101 }
1102
1103 sc->bge_flags &= ~BGE_TXRING_VALID;
1104 }
1105
1106 int
1107 bge_init_tx_ring(sc)
1108 struct bge_softc *sc;
1109 {
1110 int i;
1111 bus_dmamap_t dmamap;
1112 struct txdmamap_pool_entry *dma;
1113
1114 if (sc->bge_flags & BGE_TXRING_VALID)
1115 return 0;
1116
1117 sc->bge_txcnt = 0;
1118 sc->bge_tx_saved_considx = 0;
1119 CSR_WRITE_4(sc, BGE_MBX_TX_HOST_PROD0_LO, 0);
1120 if (sc->bge_quirks & BGE_QUIRK_PRODUCER_BUG) /* 5700 b2 errata */
1121 CSR_WRITE_4(sc, BGE_MBX_TX_HOST_PROD0_LO, 0);
1122
1123 CSR_WRITE_4(sc, BGE_MBX_TX_NIC_PROD0_LO, 0);
1124 if (sc->bge_quirks & BGE_QUIRK_PRODUCER_BUG) /* 5700 b2 errata */
1125 CSR_WRITE_4(sc, BGE_MBX_TX_HOST_PROD0_LO, 0);
1126
1127 SLIST_INIT(&sc->txdma_list);
1128 for (i = 0; i < BGE_RSLOTS; i++) {
1129 if (bus_dmamap_create(sc->bge_dmatag, ETHER_MAX_LEN_JUMBO,
1130 BGE_NTXSEG, ETHER_MAX_LEN_JUMBO, 0, BUS_DMA_NOWAIT,
1131 &dmamap))
1132 return(ENOBUFS);
1133 if (dmamap == NULL)
1134 panic("dmamap NULL in bge_init_tx_ring");
1135 dma = malloc(sizeof(*dma), M_DEVBUF, M_NOWAIT);
1136 if (dma == NULL) {
1137 printf("%s: can't alloc txdmamap_pool_entry\n",
1138 sc->bge_dev.dv_xname);
1139 bus_dmamap_destroy(sc->bge_dmatag, dmamap);
1140 return (ENOMEM);
1141 }
1142 dma->dmamap = dmamap;
1143 SLIST_INSERT_HEAD(&sc->txdma_list, dma, link);
1144 }
1145
1146 sc->bge_flags |= BGE_TXRING_VALID;
1147
1148 return(0);
1149 }
1150
1151 void
1152 bge_setmulti(sc)
1153 struct bge_softc *sc;
1154 {
1155 struct ethercom *ac = &sc->ethercom;
1156 struct ifnet *ifp = &ac->ec_if;
1157 struct ether_multi *enm;
1158 struct ether_multistep step;
1159 u_int32_t hashes[4] = { 0, 0, 0, 0 };
1160 u_int32_t h;
1161 int i;
1162
1163 if (ifp->if_flags & IFF_PROMISC)
1164 goto allmulti;
1165
1166 /* Now program new ones. */
1167 ETHER_FIRST_MULTI(step, ac, enm);
1168 while (enm != NULL) {
1169 if (memcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
1170 /*
1171 * We must listen to a range of multicast addresses.
1172 * For now, just accept all multicasts, rather than
1173 * trying to set only those filter bits needed to match
1174 * the range. (At this time, the only use of address
1175 * ranges is for IP multicast routing, for which the
1176 * range is big enough to require all bits set.)
1177 */
1178 goto allmulti;
1179 }
1180
1181 h = ether_crc32_le(enm->enm_addrlo, ETHER_ADDR_LEN);
1182
1183 /* Just want the 7 least-significant bits. */
1184 h &= 0x7f;
1185
1186 hashes[(h & 0x60) >> 5] |= 1 << (h & 0x1F);
1187 ETHER_NEXT_MULTI(step, enm);
1188 }
1189
1190 ifp->if_flags &= ~IFF_ALLMULTI;
1191 goto setit;
1192
1193 allmulti:
1194 ifp->if_flags |= IFF_ALLMULTI;
1195 hashes[0] = hashes[1] = hashes[2] = hashes[3] = 0xffffffff;
1196
1197 setit:
1198 for (i = 0; i < 4; i++)
1199 CSR_WRITE_4(sc, BGE_MAR0 + (i * 4), hashes[i]);
1200 }
1201
1202 const int bge_swapbits[] = {
1203 0,
1204 BGE_MODECTL_BYTESWAP_DATA,
1205 BGE_MODECTL_WORDSWAP_DATA,
1206 BGE_MODECTL_BYTESWAP_NONFRAME,
1207 BGE_MODECTL_WORDSWAP_NONFRAME,
1208
1209 BGE_MODECTL_BYTESWAP_DATA|BGE_MODECTL_WORDSWAP_DATA,
1210 BGE_MODECTL_BYTESWAP_DATA|BGE_MODECTL_BYTESWAP_NONFRAME,
1211 BGE_MODECTL_BYTESWAP_DATA|BGE_MODECTL_WORDSWAP_NONFRAME,
1212
1213 BGE_MODECTL_WORDSWAP_DATA|BGE_MODECTL_BYTESWAP_NONFRAME,
1214 BGE_MODECTL_WORDSWAP_DATA|BGE_MODECTL_WORDSWAP_NONFRAME,
1215
1216 BGE_MODECTL_BYTESWAP_NONFRAME|BGE_MODECTL_WORDSWAP_NONFRAME,
1217
1218 BGE_MODECTL_BYTESWAP_DATA|BGE_MODECTL_WORDSWAP_DATA|
1219 BGE_MODECTL_BYTESWAP_NONFRAME,
1220 BGE_MODECTL_BYTESWAP_DATA|BGE_MODECTL_WORDSWAP_DATA|
1221 BGE_MODECTL_WORDSWAP_NONFRAME,
1222 BGE_MODECTL_BYTESWAP_DATA|BGE_MODECTL_BYTESWAP_NONFRAME|
1223 BGE_MODECTL_WORDSWAP_NONFRAME,
1224 BGE_MODECTL_WORDSWAP_DATA|BGE_MODECTL_BYTESWAP_NONFRAME|
1225 BGE_MODECTL_WORDSWAP_NONFRAME,
1226
1227 BGE_MODECTL_BYTESWAP_DATA|BGE_MODECTL_WORDSWAP_DATA|
1228 BGE_MODECTL_BYTESWAP_NONFRAME|BGE_MODECTL_WORDSWAP_NONFRAME,
1229 };
1230
1231 int bge_swapindex = 0;
1232
1233 /*
1234 * Do endian, PCI and DMA initialization. Also check the on-board ROM
1235 * self-test results.
1236 */
1237 int
1238 bge_chipinit(sc)
1239 struct bge_softc *sc;
1240 {
1241 u_int32_t cachesize;
1242 int i;
1243 u_int32_t dma_rw_ctl;
1244 struct pci_attach_args *pa = &(sc->bge_pa);
1245
1246
1247 /* Set endianness before we access any non-PCI registers. */
1248 pci_conf_write(pa->pa_pc, pa->pa_tag, BGE_PCI_MISC_CTL,
1249 BGE_INIT);
1250
1251 /* Set power state to D0. */
1252 bge_setpowerstate(sc, 0);
1253
1254 /*
1255 * Check the 'ROM failed' bit on the RX CPU to see if
1256 * self-tests passed.
1257 */
1258 if (CSR_READ_4(sc, BGE_RXCPU_MODE) & BGE_RXCPUMODE_ROMFAIL) {
1259 printf("%s: RX CPU self-diagnostics failed!\n",
1260 sc->bge_dev.dv_xname);
1261 return(ENODEV);
1262 }
1263
1264 /* Clear the MAC control register */
1265 CSR_WRITE_4(sc, BGE_MAC_MODE, 0);
1266
1267 /*
1268 * Clear the MAC statistics block in the NIC's
1269 * internal memory.
1270 */
1271 for (i = BGE_STATS_BLOCK;
1272 i < BGE_STATS_BLOCK_END + 1; i += sizeof(u_int32_t))
1273 BGE_MEMWIN_WRITE(pa->pa_pc, pa->pa_tag, i, 0);
1274
1275 for (i = BGE_STATUS_BLOCK;
1276 i < BGE_STATUS_BLOCK_END + 1; i += sizeof(u_int32_t))
1277 BGE_MEMWIN_WRITE(pa->pa_pc, pa->pa_tag, i, 0);
1278
1279 /* Set up the PCI DMA control register. */
1280 if (pci_conf_read(pa->pa_pc, pa->pa_tag,BGE_PCI_PCISTATE) &
1281 BGE_PCISTATE_PCI_BUSMODE) {
1282 /* Conventional PCI bus */
1283 DPRINTFN(4, ("(%s: PCI 2.2 DMA setting)\n", sc->bge_dev.dv_xname));
1284 dma_rw_ctl = (BGE_PCI_READ_CMD | BGE_PCI_WRITE_CMD |
1285 (0x7 << BGE_PCIDMARWCTL_RD_WAT_SHIFT) |
1286 (0x7 << BGE_PCIDMARWCTL_WR_WAT_SHIFT));
1287 if ((sc->bge_quirks & BGE_QUIRK_5705_CORE) == 0) {
1288 dma_rw_ctl |= 0x0F;
1289 }
1290 } else {
1291 DPRINTFN(4, ("(:%s: PCI-X DMA setting)\n", sc->bge_dev.dv_xname));
1292 /* PCI-X bus */
1293 dma_rw_ctl = BGE_PCI_READ_CMD|BGE_PCI_WRITE_CMD |
1294 (0x3 << BGE_PCIDMARWCTL_RD_WAT_SHIFT) |
1295 (0x3 << BGE_PCIDMARWCTL_WR_WAT_SHIFT) |
1296 (0x0F);
1297 /*
1298 * 5703 and 5704 need ONEDMA_AT_ONCE as a workaround
1299 * for hardware bugs, which means we should also clear
1300 * the low-order MINDMA bits. In addition, the 5704
1301 * uses a different encoding of read/write watermarks.
1302 */
1303 if (BGE_ASICREV(sc->bge_chipid) == BGE_ASICREV_BCM5704) {
1304 dma_rw_ctl = BGE_PCI_READ_CMD|BGE_PCI_WRITE_CMD |
1305 /* should be 0x1f0000 */
1306 (0x7 << BGE_PCIDMARWCTL_RD_WAT_SHIFT) |
1307 (0x3 << BGE_PCIDMARWCTL_WR_WAT_SHIFT);
1308 dma_rw_ctl |= BGE_PCIDMARWCTL_ONEDMA_ATONCE;
1309 }
1310 else if (BGE_ASICREV(sc->bge_chipid) == BGE_ASICREV_BCM5703) {
1311 dma_rw_ctl &= 0xfffffff0;
1312 dma_rw_ctl |= BGE_PCIDMARWCTL_ONEDMA_ATONCE;
1313 }
1314 }
1315
1316 pci_conf_write(pa->pa_pc, pa->pa_tag, BGE_PCI_DMA_RW_CTL, dma_rw_ctl);
1317
1318 /*
1319 * Set up general mode register.
1320 */
1321 CSR_WRITE_4(sc, BGE_MODE_CTL, BGE_DMA_SWAP_OPTIONS|
1322 BGE_MODECTL_MAC_ATTN_INTR|BGE_MODECTL_HOST_SEND_BDS|
1323 BGE_MODECTL_TX_NO_PHDR_CSUM|BGE_MODECTL_RX_NO_PHDR_CSUM);
1324
1325 /* Get cache line size. */
1326 cachesize = pci_conf_read(pa->pa_pc, pa->pa_tag, BGE_PCI_CACHESZ);
1327
1328 /*
1329 * Avoid violating PCI spec on certain chip revs.
1330 */
1331 if (pci_conf_read(pa->pa_pc, pa->pa_tag, BGE_PCI_CMD) &
1332 PCIM_CMD_MWIEN) {
1333 switch(cachesize) {
1334 case 1:
1335 PCI_SETBIT(pa->pa_pc, pa->pa_tag, BGE_PCI_DMA_RW_CTL,
1336 BGE_PCI_WRITE_BNDRY_16BYTES);
1337 break;
1338 case 2:
1339 PCI_SETBIT(pa->pa_pc, pa->pa_tag, BGE_PCI_DMA_RW_CTL,
1340 BGE_PCI_WRITE_BNDRY_32BYTES);
1341 break;
1342 case 4:
1343 PCI_SETBIT(pa->pa_pc, pa->pa_tag, BGE_PCI_DMA_RW_CTL,
1344 BGE_PCI_WRITE_BNDRY_64BYTES);
1345 break;
1346 case 8:
1347 PCI_SETBIT(pa->pa_pc, pa->pa_tag, BGE_PCI_DMA_RW_CTL,
1348 BGE_PCI_WRITE_BNDRY_128BYTES);
1349 break;
1350 case 16:
1351 PCI_SETBIT(pa->pa_pc, pa->pa_tag, BGE_PCI_DMA_RW_CTL,
1352 BGE_PCI_WRITE_BNDRY_256BYTES);
1353 break;
1354 case 32:
1355 PCI_SETBIT(pa->pa_pc, pa->pa_tag, BGE_PCI_DMA_RW_CTL,
1356 BGE_PCI_WRITE_BNDRY_512BYTES);
1357 break;
1358 case 64:
1359 PCI_SETBIT(pa->pa_pc, pa->pa_tag, BGE_PCI_DMA_RW_CTL,
1360 BGE_PCI_WRITE_BNDRY_1024BYTES);
1361 break;
1362 default:
1363 /* Disable PCI memory write and invalidate. */
1364 #if 0
1365 if (bootverbose)
1366 printf("%s: cache line size %d not "
1367 "supported; disabling PCI MWI\n",
1368 sc->bge_dev.dv_xname, cachesize);
1369 #endif
1370 PCI_CLRBIT(pa->pa_pc, pa->pa_tag, BGE_PCI_CMD,
1371 PCIM_CMD_MWIEN);
1372 break;
1373 }
1374 }
1375
1376 /*
1377 * Disable memory write invalidate. Apparently it is not supported
1378 * properly by these devices.
1379 */
1380 PCI_CLRBIT(pa->pa_pc, pa->pa_tag, BGE_PCI_CMD, PCIM_CMD_MWIEN);
1381
1382
1383 #ifdef __brokenalpha__
1384 /*
1385 * Must insure that we do not cross an 8K (bytes) boundary
1386 * for DMA reads. Our highest limit is 1K bytes. This is a
1387 * restriction on some ALPHA platforms with early revision
1388 * 21174 PCI chipsets, such as the AlphaPC 164lx
1389 */
1390 PCI_SETBIT(sc, BGE_PCI_DMA_RW_CTL, BGE_PCI_READ_BNDRY_1024, 4);
1391 #endif
1392
1393 /* Set the timer prescaler (always 66MHz) */
1394 CSR_WRITE_4(sc, BGE_MISC_CFG, 65 << 1/*BGE_32BITTIME_66MHZ*/);
1395
1396 return(0);
1397 }
1398
1399 int
1400 bge_blockinit(sc)
1401 struct bge_softc *sc;
1402 {
1403 volatile struct bge_rcb *rcb;
1404 bus_size_t rcb_addr;
1405 int i;
1406 struct ifnet *ifp = &sc->ethercom.ec_if;
1407 bge_hostaddr taddr;
1408
1409 /*
1410 * Initialize the memory window pointer register so that
1411 * we can access the first 32K of internal NIC RAM. This will
1412 * allow us to set up the TX send ring RCBs and the RX return
1413 * ring RCBs, plus other things which live in NIC memory.
1414 */
1415
1416 pci_conf_write(sc->bge_pa.pa_pc, sc->bge_pa.pa_tag,
1417 BGE_PCI_MEMWIN_BASEADDR, 0);
1418
1419 /* Configure mbuf memory pool */
1420 if ((sc->bge_quirks & BGE_QUIRK_5705_CORE) == 0) {
1421 if (sc->bge_extram) {
1422 CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_BASEADDR,
1423 BGE_EXT_SSRAM);
1424 if ((sc->bge_quirks & BGE_QUIRK_FEWER_MBUFS) != 0)
1425 CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_LEN, 0x10000);
1426 else
1427 CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_LEN, 0x18000);
1428 } else {
1429 CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_BASEADDR,
1430 BGE_BUFFPOOL_1);
1431 if ((sc->bge_quirks & BGE_QUIRK_FEWER_MBUFS) != 0)
1432 CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_LEN, 0x10000);
1433 else
1434 CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_LEN, 0x18000);
1435 }
1436
1437 /* Configure DMA resource pool */
1438 CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_BASEADDR,
1439 BGE_DMA_DESCRIPTORS);
1440 CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_LEN, 0x2000);
1441 }
1442
1443 /* Configure mbuf pool watermarks */
1444 #ifdef ORIG_WPAUL_VALUES
1445 CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_READDMA_LOWAT, 24);
1446 CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 24);
1447 CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_HIWAT, 48);
1448 #else
1449 /* new broadcom docs strongly recommend these: */
1450 if ((sc->bge_quirks & BGE_QUIRK_5705_CORE) == 0) {
1451 if (ifp->if_mtu > ETHER_MAX_LEN) {
1452 CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_READDMA_LOWAT, 0x50);
1453 CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 0x20);
1454 CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_HIWAT, 0x60);
1455 } else {
1456 /* Values from Linux driver... */
1457 CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_READDMA_LOWAT, 304);
1458 CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 152);
1459 CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_HIWAT, 380);
1460 }
1461 } else {
1462 CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_READDMA_LOWAT, 0x0);
1463 CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 0x10);
1464 CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_HIWAT, 0x60);
1465 }
1466 #endif
1467
1468 /* Configure DMA resource watermarks */
1469 CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_LOWAT, 5);
1470 CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_HIWAT, 10);
1471
1472 /* Enable buffer manager */
1473 if ((sc->bge_quirks & BGE_QUIRK_5705_CORE) == 0) {
1474 CSR_WRITE_4(sc, BGE_BMAN_MODE,
1475 BGE_BMANMODE_ENABLE|BGE_BMANMODE_LOMBUF_ATTN);
1476
1477 /* Poll for buffer manager start indication */
1478 for (i = 0; i < BGE_TIMEOUT; i++) {
1479 if (CSR_READ_4(sc, BGE_BMAN_MODE) & BGE_BMANMODE_ENABLE)
1480 break;
1481 DELAY(10);
1482 }
1483
1484 if (i == BGE_TIMEOUT) {
1485 printf("%s: buffer manager failed to start\n",
1486 sc->bge_dev.dv_xname);
1487 return(ENXIO);
1488 }
1489 }
1490
1491 /* Enable flow-through queues */
1492 CSR_WRITE_4(sc, BGE_FTQ_RESET, 0xFFFFFFFF);
1493 CSR_WRITE_4(sc, BGE_FTQ_RESET, 0);
1494
1495 /* Wait until queue initialization is complete */
1496 for (i = 0; i < BGE_TIMEOUT; i++) {
1497 if (CSR_READ_4(sc, BGE_FTQ_RESET) == 0)
1498 break;
1499 DELAY(10);
1500 }
1501
1502 if (i == BGE_TIMEOUT) {
1503 printf("%s: flow-through queue init failed\n",
1504 sc->bge_dev.dv_xname);
1505 return(ENXIO);
1506 }
1507
1508 /* Initialize the standard RX ring control block */
1509 rcb = &sc->bge_rdata->bge_info.bge_std_rx_rcb;
1510 bge_set_hostaddr(&rcb->bge_hostaddr,
1511 BGE_RING_DMA_ADDR(sc, bge_rx_std_ring));
1512 if ((sc->bge_quirks & BGE_QUIRK_5705_CORE) == 0) {
1513 rcb->bge_maxlen_flags =
1514 BGE_RCB_MAXLEN_FLAGS(BGE_MAX_FRAMELEN, 0);
1515 } else {
1516 rcb->bge_maxlen_flags = BGE_RCB_MAXLEN_FLAGS(512, 0);
1517 }
1518 if (sc->bge_extram)
1519 rcb->bge_nicaddr = BGE_EXT_STD_RX_RINGS;
1520 else
1521 rcb->bge_nicaddr = BGE_STD_RX_RINGS;
1522 CSR_WRITE_4(sc, BGE_RX_STD_RCB_HADDR_HI, rcb->bge_hostaddr.bge_addr_hi);
1523 CSR_WRITE_4(sc, BGE_RX_STD_RCB_HADDR_LO, rcb->bge_hostaddr.bge_addr_lo);
1524 CSR_WRITE_4(sc, BGE_RX_STD_RCB_MAXLEN_FLAGS, rcb->bge_maxlen_flags);
1525 CSR_WRITE_4(sc, BGE_RX_STD_RCB_NICADDR, rcb->bge_nicaddr);
1526
1527 if ((sc->bge_quirks & BGE_QUIRK_5705_CORE) == 0) {
1528 sc->bge_return_ring_cnt = BGE_RETURN_RING_CNT;
1529 } else {
1530 sc->bge_return_ring_cnt = BGE_RETURN_RING_CNT_5705;
1531 }
1532
1533 /*
1534 * Initialize the jumbo RX ring control block
1535 * We set the 'ring disabled' bit in the flags
1536 * field until we're actually ready to start
1537 * using this ring (i.e. once we set the MTU
1538 * high enough to require it).
1539 */
1540 if ((sc->bge_quirks & BGE_QUIRK_5705_CORE) == 0) {
1541 rcb = &sc->bge_rdata->bge_info.bge_jumbo_rx_rcb;
1542 bge_set_hostaddr(&rcb->bge_hostaddr,
1543 BGE_RING_DMA_ADDR(sc, bge_rx_jumbo_ring));
1544 rcb->bge_maxlen_flags =
1545 BGE_RCB_MAXLEN_FLAGS(BGE_MAX_FRAMELEN,
1546 BGE_RCB_FLAG_RING_DISABLED);
1547 if (sc->bge_extram)
1548 rcb->bge_nicaddr = BGE_EXT_JUMBO_RX_RINGS;
1549 else
1550 rcb->bge_nicaddr = BGE_JUMBO_RX_RINGS;
1551
1552 CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_HADDR_HI,
1553 rcb->bge_hostaddr.bge_addr_hi);
1554 CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_HADDR_LO,
1555 rcb->bge_hostaddr.bge_addr_lo);
1556 CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_MAXLEN_FLAGS,
1557 rcb->bge_maxlen_flags);
1558 CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_NICADDR, rcb->bge_nicaddr);
1559
1560 /* Set up dummy disabled mini ring RCB */
1561 rcb = &sc->bge_rdata->bge_info.bge_mini_rx_rcb;
1562 rcb->bge_maxlen_flags = BGE_RCB_MAXLEN_FLAGS(0,
1563 BGE_RCB_FLAG_RING_DISABLED);
1564 CSR_WRITE_4(sc, BGE_RX_MINI_RCB_MAXLEN_FLAGS,
1565 rcb->bge_maxlen_flags);
1566
1567 bus_dmamap_sync(sc->bge_dmatag, sc->bge_ring_map,
1568 offsetof(struct bge_ring_data, bge_info),
1569 sizeof (struct bge_gib),
1570 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
1571 }
1572
1573 /*
1574 * Set the BD ring replentish thresholds. The recommended
1575 * values are 1/8th the number of descriptors allocated to
1576 * each ring.
1577 */
1578 CSR_WRITE_4(sc, BGE_RBDI_STD_REPL_THRESH, BGE_STD_RX_RING_CNT/8);
1579 CSR_WRITE_4(sc, BGE_RBDI_JUMBO_REPL_THRESH, BGE_JUMBO_RX_RING_CNT/8);
1580
1581 /*
1582 * Disable all unused send rings by setting the 'ring disabled'
1583 * bit in the flags field of all the TX send ring control blocks.
1584 * These are located in NIC memory.
1585 */
1586 rcb_addr = BGE_MEMWIN_START + BGE_SEND_RING_RCB;
1587 for (i = 0; i < BGE_TX_RINGS_EXTSSRAM_MAX; i++) {
1588 RCB_WRITE_4(sc, rcb_addr, bge_maxlen_flags,
1589 BGE_RCB_MAXLEN_FLAGS(0,BGE_RCB_FLAG_RING_DISABLED));
1590 RCB_WRITE_4(sc, rcb_addr, bge_nicaddr, 0);
1591 rcb_addr += sizeof(struct bge_rcb);
1592 }
1593
1594 /* Configure TX RCB 0 (we use only the first ring) */
1595 rcb_addr = BGE_MEMWIN_START + BGE_SEND_RING_RCB;
1596 bge_set_hostaddr(&taddr, BGE_RING_DMA_ADDR(sc, bge_tx_ring));
1597 RCB_WRITE_4(sc, rcb_addr, bge_hostaddr.bge_addr_hi, taddr.bge_addr_hi);
1598 RCB_WRITE_4(sc, rcb_addr, bge_hostaddr.bge_addr_lo, taddr.bge_addr_lo);
1599 RCB_WRITE_4(sc, rcb_addr, bge_nicaddr,
1600 BGE_NIC_TXRING_ADDR(0, BGE_TX_RING_CNT));
1601 if ((sc->bge_quirks & BGE_QUIRK_5705_CORE) == 0) {
1602 RCB_WRITE_4(sc, rcb_addr, bge_maxlen_flags,
1603 BGE_RCB_MAXLEN_FLAGS(BGE_TX_RING_CNT, 0));
1604 }
1605
1606 /* Disable all unused RX return rings */
1607 rcb_addr = BGE_MEMWIN_START + BGE_RX_RETURN_RING_RCB;
1608 for (i = 0; i < BGE_RX_RINGS_MAX; i++) {
1609 RCB_WRITE_4(sc, rcb_addr, bge_hostaddr.bge_addr_hi, 0);
1610 RCB_WRITE_4(sc, rcb_addr, bge_hostaddr.bge_addr_lo, 0);
1611 RCB_WRITE_4(sc, rcb_addr, bge_maxlen_flags,
1612 BGE_RCB_MAXLEN_FLAGS(sc->bge_return_ring_cnt,
1613 BGE_RCB_FLAG_RING_DISABLED));
1614 RCB_WRITE_4(sc, rcb_addr, bge_nicaddr, 0);
1615 CSR_WRITE_4(sc, BGE_MBX_RX_CONS0_LO +
1616 (i * (sizeof(u_int64_t))), 0);
1617 rcb_addr += sizeof(struct bge_rcb);
1618 }
1619
1620 /* Initialize RX ring indexes */
1621 CSR_WRITE_4(sc, BGE_MBX_RX_STD_PROD_LO, 0);
1622 CSR_WRITE_4(sc, BGE_MBX_RX_JUMBO_PROD_LO, 0);
1623 CSR_WRITE_4(sc, BGE_MBX_RX_MINI_PROD_LO, 0);
1624
1625 /*
1626 * Set up RX return ring 0
1627 * Note that the NIC address for RX return rings is 0x00000000.
1628 * The return rings live entirely within the host, so the
1629 * nicaddr field in the RCB isn't used.
1630 */
1631 rcb_addr = BGE_MEMWIN_START + BGE_RX_RETURN_RING_RCB;
1632 bge_set_hostaddr(&taddr, BGE_RING_DMA_ADDR(sc, bge_rx_return_ring));
1633 RCB_WRITE_4(sc, rcb_addr, bge_hostaddr.bge_addr_hi, taddr.bge_addr_hi);
1634 RCB_WRITE_4(sc, rcb_addr, bge_hostaddr.bge_addr_lo, taddr.bge_addr_lo);
1635 RCB_WRITE_4(sc, rcb_addr, bge_nicaddr, 0x00000000);
1636 RCB_WRITE_4(sc, rcb_addr, bge_maxlen_flags,
1637 BGE_RCB_MAXLEN_FLAGS(sc->bge_return_ring_cnt, 0));
1638
1639 /* Set random backoff seed for TX */
1640 CSR_WRITE_4(sc, BGE_TX_RANDOM_BACKOFF,
1641 LLADDR(ifp->if_sadl)[0] + LLADDR(ifp->if_sadl)[1] +
1642 LLADDR(ifp->if_sadl)[2] + LLADDR(ifp->if_sadl)[3] +
1643 LLADDR(ifp->if_sadl)[4] + LLADDR(ifp->if_sadl)[5] +
1644 BGE_TX_BACKOFF_SEED_MASK);
1645
1646 /* Set inter-packet gap */
1647 CSR_WRITE_4(sc, BGE_TX_LENGTHS, 0x2620);
1648
1649 /*
1650 * Specify which ring to use for packets that don't match
1651 * any RX rules.
1652 */
1653 CSR_WRITE_4(sc, BGE_RX_RULES_CFG, 0x08);
1654
1655 /*
1656 * Configure number of RX lists. One interrupt distribution
1657 * list, sixteen active lists, one bad frames class.
1658 */
1659 CSR_WRITE_4(sc, BGE_RXLP_CFG, 0x181);
1660
1661 /* Inialize RX list placement stats mask. */
1662 CSR_WRITE_4(sc, BGE_RXLP_STATS_ENABLE_MASK, 0x007FFFFF);
1663 CSR_WRITE_4(sc, BGE_RXLP_STATS_CTL, 0x1);
1664
1665 /* Disable host coalescing until we get it set up */
1666 CSR_WRITE_4(sc, BGE_HCC_MODE, 0x00000000);
1667
1668 /* Poll to make sure it's shut down. */
1669 for (i = 0; i < BGE_TIMEOUT; i++) {
1670 if (!(CSR_READ_4(sc, BGE_HCC_MODE) & BGE_HCCMODE_ENABLE))
1671 break;
1672 DELAY(10);
1673 }
1674
1675 if (i == BGE_TIMEOUT) {
1676 printf("%s: host coalescing engine failed to idle\n",
1677 sc->bge_dev.dv_xname);
1678 return(ENXIO);
1679 }
1680
1681 /* Set up host coalescing defaults */
1682 CSR_WRITE_4(sc, BGE_HCC_RX_COAL_TICKS, sc->bge_rx_coal_ticks);
1683 CSR_WRITE_4(sc, BGE_HCC_TX_COAL_TICKS, sc->bge_tx_coal_ticks);
1684 CSR_WRITE_4(sc, BGE_HCC_RX_MAX_COAL_BDS, sc->bge_rx_max_coal_bds);
1685 CSR_WRITE_4(sc, BGE_HCC_TX_MAX_COAL_BDS, sc->bge_tx_max_coal_bds);
1686 if ((sc->bge_quirks & BGE_QUIRK_5705_CORE) == 0) {
1687 CSR_WRITE_4(sc, BGE_HCC_RX_COAL_TICKS_INT, 0);
1688 CSR_WRITE_4(sc, BGE_HCC_TX_COAL_TICKS_INT, 0);
1689 }
1690 CSR_WRITE_4(sc, BGE_HCC_RX_MAX_COAL_BDS_INT, 0);
1691 CSR_WRITE_4(sc, BGE_HCC_TX_MAX_COAL_BDS_INT, 0);
1692
1693 /* Set up address of statistics block */
1694 if ((sc->bge_quirks & BGE_QUIRK_5705_CORE) == 0) {
1695 bge_set_hostaddr(&taddr,
1696 BGE_RING_DMA_ADDR(sc, bge_info.bge_stats));
1697 CSR_WRITE_4(sc, BGE_HCC_STATS_TICKS, sc->bge_stat_ticks);
1698 CSR_WRITE_4(sc, BGE_HCC_STATS_BASEADDR, BGE_STATS_BLOCK);
1699 CSR_WRITE_4(sc, BGE_HCC_STATS_ADDR_HI, taddr.bge_addr_hi);
1700 CSR_WRITE_4(sc, BGE_HCC_STATS_ADDR_LO, taddr.bge_addr_lo);
1701 }
1702
1703 /* Set up address of status block */
1704 bge_set_hostaddr(&taddr, BGE_RING_DMA_ADDR(sc, bge_status_block));
1705 CSR_WRITE_4(sc, BGE_HCC_STATUSBLK_BASEADDR, BGE_STATUS_BLOCK);
1706 CSR_WRITE_4(sc, BGE_HCC_STATUSBLK_ADDR_HI, taddr.bge_addr_hi);
1707 CSR_WRITE_4(sc, BGE_HCC_STATUSBLK_ADDR_LO, taddr.bge_addr_lo);
1708 sc->bge_rdata->bge_status_block.bge_idx[0].bge_rx_prod_idx = 0;
1709 sc->bge_rdata->bge_status_block.bge_idx[0].bge_tx_cons_idx = 0;
1710
1711 /* Turn on host coalescing state machine */
1712 CSR_WRITE_4(sc, BGE_HCC_MODE, BGE_HCCMODE_ENABLE);
1713
1714 /* Turn on RX BD completion state machine and enable attentions */
1715 CSR_WRITE_4(sc, BGE_RBDC_MODE,
1716 BGE_RBDCMODE_ENABLE|BGE_RBDCMODE_ATTN);
1717
1718 /* Turn on RX list placement state machine */
1719 CSR_WRITE_4(sc, BGE_RXLP_MODE, BGE_RXLPMODE_ENABLE);
1720
1721 /* Turn on RX list selector state machine. */
1722 if ((sc->bge_quirks & BGE_QUIRK_5705_CORE) == 0) {
1723 CSR_WRITE_4(sc, BGE_RXLS_MODE, BGE_RXLSMODE_ENABLE);
1724 }
1725
1726 /* Turn on DMA, clear stats */
1727 CSR_WRITE_4(sc, BGE_MAC_MODE, BGE_MACMODE_TXDMA_ENB|
1728 BGE_MACMODE_RXDMA_ENB|BGE_MACMODE_RX_STATS_CLEAR|
1729 BGE_MACMODE_TX_STATS_CLEAR|BGE_MACMODE_RX_STATS_ENB|
1730 BGE_MACMODE_TX_STATS_ENB|BGE_MACMODE_FRMHDR_DMA_ENB|
1731 (sc->bge_tbi ? BGE_PORTMODE_TBI : BGE_PORTMODE_MII));
1732
1733 /* Set misc. local control, enable interrupts on attentions */
1734 sc->bge_local_ctrl_reg = BGE_MLC_INTR_ONATTN | BGE_MLC_AUTO_EEPROM;
1735
1736 #ifdef notdef
1737 /* Assert GPIO pins for PHY reset */
1738 BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_MISCIO_OUT0|
1739 BGE_MLC_MISCIO_OUT1|BGE_MLC_MISCIO_OUT2);
1740 BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_MISCIO_OUTEN0|
1741 BGE_MLC_MISCIO_OUTEN1|BGE_MLC_MISCIO_OUTEN2);
1742 #endif
1743
1744 #if defined(not_quite_yet)
1745 /* Linux driver enables enable gpio pin #1 on 5700s */
1746 if (sc->bge_chipid == BGE_CHIPID_BCM5700) {
1747 sc->bge_local_ctrl_reg |=
1748 (BGE_MLC_MISCIO_OUT1|BGE_MLC_MISCIO_OUTEN1);
1749 }
1750 #endif
1751 CSR_WRITE_4(sc, BGE_MISC_LOCAL_CTL, sc->bge_local_ctrl_reg);
1752
1753 /* Turn on DMA completion state machine */
1754 if ((sc->bge_quirks & BGE_QUIRK_5705_CORE) == 0) {
1755 CSR_WRITE_4(sc, BGE_DMAC_MODE, BGE_DMACMODE_ENABLE);
1756 }
1757
1758 /* Turn on write DMA state machine */
1759 CSR_WRITE_4(sc, BGE_WDMA_MODE,
1760 BGE_WDMAMODE_ENABLE|BGE_WDMAMODE_ALL_ATTNS);
1761
1762 /* Turn on read DMA state machine */
1763 CSR_WRITE_4(sc, BGE_RDMA_MODE,
1764 BGE_RDMAMODE_ENABLE|BGE_RDMAMODE_ALL_ATTNS);
1765
1766 /* Turn on RX data completion state machine */
1767 CSR_WRITE_4(sc, BGE_RDC_MODE, BGE_RDCMODE_ENABLE);
1768
1769 /* Turn on RX BD initiator state machine */
1770 CSR_WRITE_4(sc, BGE_RBDI_MODE, BGE_RBDIMODE_ENABLE);
1771
1772 /* Turn on RX data and RX BD initiator state machine */
1773 CSR_WRITE_4(sc, BGE_RDBDI_MODE, BGE_RDBDIMODE_ENABLE);
1774
1775 /* Turn on Mbuf cluster free state machine */
1776 if ((sc->bge_quirks & BGE_QUIRK_5705_CORE) == 0) {
1777 CSR_WRITE_4(sc, BGE_MBCF_MODE, BGE_MBCFMODE_ENABLE);
1778 }
1779
1780 /* Turn on send BD completion state machine */
1781 CSR_WRITE_4(sc, BGE_SBDC_MODE, BGE_SBDCMODE_ENABLE);
1782
1783 /* Turn on send data completion state machine */
1784 CSR_WRITE_4(sc, BGE_SDC_MODE, BGE_SDCMODE_ENABLE);
1785
1786 /* Turn on send data initiator state machine */
1787 CSR_WRITE_4(sc, BGE_SDI_MODE, BGE_SDIMODE_ENABLE);
1788
1789 /* Turn on send BD initiator state machine */
1790 CSR_WRITE_4(sc, BGE_SBDI_MODE, BGE_SBDIMODE_ENABLE);
1791
1792 /* Turn on send BD selector state machine */
1793 CSR_WRITE_4(sc, BGE_SRS_MODE, BGE_SRSMODE_ENABLE);
1794
1795 CSR_WRITE_4(sc, BGE_SDI_STATS_ENABLE_MASK, 0x007FFFFF);
1796 CSR_WRITE_4(sc, BGE_SDI_STATS_CTL,
1797 BGE_SDISTATSCTL_ENABLE|BGE_SDISTATSCTL_FASTER);
1798
1799 /* ack/clear link change events */
1800 CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED|
1801 BGE_MACSTAT_CFG_CHANGED);
1802 CSR_WRITE_4(sc, BGE_MI_STS, 0);
1803
1804 /* Enable PHY auto polling (for MII/GMII only) */
1805 if (sc->bge_tbi) {
1806 CSR_WRITE_4(sc, BGE_MI_STS, BGE_MISTS_LINK);
1807 } else {
1808 BGE_SETBIT(sc, BGE_MI_MODE, BGE_MIMODE_AUTOPOLL|10<<16);
1809 if (sc->bge_quirks & BGE_QUIRK_LINK_STATE_BROKEN)
1810 CSR_WRITE_4(sc, BGE_MAC_EVT_ENB,
1811 BGE_EVTENB_MI_INTERRUPT);
1812 }
1813
1814 /* Enable link state change attentions. */
1815 BGE_SETBIT(sc, BGE_MAC_EVT_ENB, BGE_EVTENB_LINK_CHANGED);
1816
1817 return(0);
1818 }
1819
1820 static const struct bge_revision {
1821 uint32_t br_chipid;
1822 uint32_t br_quirks;
1823 const char *br_name;
1824 } bge_revisions[] = {
1825 { BGE_CHIPID_BCM5700_A0,
1826 BGE_QUIRK_LINK_STATE_BROKEN,
1827 "BCM5700 A0" },
1828
1829 { BGE_CHIPID_BCM5700_A1,
1830 BGE_QUIRK_LINK_STATE_BROKEN,
1831 "BCM5700 A1" },
1832
1833 { BGE_CHIPID_BCM5700_B0,
1834 BGE_QUIRK_LINK_STATE_BROKEN|BGE_QUIRK_CSUM_BROKEN|BGE_QUIRK_5700_COMMON,
1835 "BCM5700 B0" },
1836
1837 { BGE_CHIPID_BCM5700_B1,
1838 BGE_QUIRK_LINK_STATE_BROKEN|BGE_QUIRK_5700_COMMON,
1839 "BCM5700 B1" },
1840
1841 { BGE_CHIPID_BCM5700_B2,
1842 BGE_QUIRK_LINK_STATE_BROKEN|BGE_QUIRK_5700_COMMON,
1843 "BCM5700 B2" },
1844
1845 /* This is treated like a BCM5700 Bx */
1846 { BGE_CHIPID_BCM5700_ALTIMA,
1847 BGE_QUIRK_LINK_STATE_BROKEN|BGE_QUIRK_5700_COMMON,
1848 "BCM5700 Altima" },
1849
1850 { BGE_CHIPID_BCM5700_C0,
1851 0,
1852 "BCM5700 C0" },
1853
1854 { BGE_CHIPID_BCM5701_A0,
1855 0, /*XXX really, just not known */
1856 "BCM5701 A0" },
1857
1858 { BGE_CHIPID_BCM5701_B0,
1859 BGE_QUIRK_PCIX_DMA_ALIGN_BUG,
1860 "BCM5701 B0" },
1861
1862 { BGE_CHIPID_BCM5701_B2,
1863 BGE_QUIRK_PCIX_DMA_ALIGN_BUG,
1864 "BCM5701 B2" },
1865
1866 { BGE_CHIPID_BCM5701_B5,
1867 BGE_QUIRK_ONLY_PHY_1|BGE_QUIRK_PCIX_DMA_ALIGN_BUG,
1868 "BCM5701 B5" },
1869
1870 { BGE_CHIPID_BCM5703_A0,
1871 0,
1872 "BCM5703 A0" },
1873
1874 { BGE_CHIPID_BCM5703_A1,
1875 0,
1876 "BCM5703 A1" },
1877
1878 { BGE_CHIPID_BCM5703_A2,
1879 BGE_QUIRK_ONLY_PHY_1,
1880 "BCM5703 A2" },
1881
1882 { BGE_CHIPID_BCM5703_A3,
1883 BGE_QUIRK_ONLY_PHY_1,
1884 "BCM5703 A3" },
1885
1886 { BGE_CHIPID_BCM5704_A0,
1887 BGE_QUIRK_ONLY_PHY_1|BGE_QUIRK_FEWER_MBUFS,
1888 "BCM5704 A0" },
1889
1890 { BGE_CHIPID_BCM5704_A1,
1891 BGE_QUIRK_ONLY_PHY_1|BGE_QUIRK_FEWER_MBUFS,
1892 "BCM5704 A1" },
1893
1894 { BGE_CHIPID_BCM5704_A2,
1895 BGE_QUIRK_ONLY_PHY_1|BGE_QUIRK_FEWER_MBUFS,
1896 "BCM5704 A2" },
1897
1898 { BGE_CHIPID_BCM5704_A3,
1899 BGE_QUIRK_ONLY_PHY_1|BGE_QUIRK_FEWER_MBUFS,
1900 "BCM5704 A3" },
1901
1902 { BGE_CHIPID_BCM5705_A0,
1903 BGE_QUIRK_ONLY_PHY_1|BGE_QUIRK_5705_CORE,
1904 "BCM5705 A0" },
1905
1906 { BGE_CHIPID_BCM5705_A1,
1907 BGE_QUIRK_ONLY_PHY_1|BGE_QUIRK_5705_CORE,
1908 "BCM5705 A1" },
1909
1910 { BGE_CHIPID_BCM5705_A2,
1911 BGE_QUIRK_ONLY_PHY_1|BGE_QUIRK_5705_CORE,
1912 "BCM5705 A2" },
1913
1914 { BGE_CHIPID_BCM5705_A3,
1915 BGE_QUIRK_ONLY_PHY_1|BGE_QUIRK_5705_CORE,
1916 "BCM5705 A3" },
1917
1918 { 0, 0, NULL }
1919 };
1920
1921 /*
1922 * Some defaults for major revisions, so that newer steppings
1923 * that we don't know about have a shot at working.
1924 */
1925 static const struct bge_revision bge_majorrevs[] = {
1926 { BGE_ASICREV_BCM5700,
1927 BGE_QUIRK_LINK_STATE_BROKEN,
1928 "unknown BCM5700" },
1929
1930 { BGE_ASICREV_BCM5701,
1931 BGE_QUIRK_PCIX_DMA_ALIGN_BUG,
1932 "unknown BCM5701" },
1933
1934 { BGE_ASICREV_BCM5703,
1935 0,
1936 "unknown BCM5703" },
1937
1938 { BGE_ASICREV_BCM5704,
1939 BGE_QUIRK_ONLY_PHY_1,
1940 "unknown BCM5704" },
1941
1942 { BGE_ASICREV_BCM5705,
1943 BGE_QUIRK_ONLY_PHY_1|BGE_QUIRK_5705_CORE,
1944 "unknown BCM5705" },
1945
1946 { 0,
1947 0,
1948 NULL }
1949 };
1950
1951
1952 static const struct bge_revision *
1953 bge_lookup_rev(uint32_t chipid)
1954 {
1955 const struct bge_revision *br;
1956
1957 for (br = bge_revisions; br->br_name != NULL; br++) {
1958 if (br->br_chipid == chipid)
1959 return (br);
1960 }
1961
1962 for (br = bge_majorrevs; br->br_name != NULL; br++) {
1963 if (br->br_chipid == BGE_ASICREV(chipid))
1964 return (br);
1965 }
1966
1967 return (NULL);
1968 }
1969
1970 static const struct bge_product {
1971 pci_vendor_id_t bp_vendor;
1972 pci_product_id_t bp_product;
1973 const char *bp_name;
1974 } bge_products[] = {
1975 /*
1976 * The BCM5700 documentation seems to indicate that the hardware
1977 * still has the Alteon vendor ID burned into it, though it
1978 * should always be overridden by the value in the EEPROM. We'll
1979 * check for it anyway.
1980 */
1981 { PCI_VENDOR_ALTEON,
1982 PCI_PRODUCT_ALTEON_BCM5700,
1983 "Broadcom BCM5700 Gigabit Ethernet",
1984 },
1985 { PCI_VENDOR_ALTEON,
1986 PCI_PRODUCT_ALTEON_BCM5701,
1987 "Broadcom BCM5701 Gigabit Ethernet",
1988 },
1989
1990 { PCI_VENDOR_ALTIMA,
1991 PCI_PRODUCT_ALTIMA_AC1000,
1992 "Altima AC1000 Gigabit Ethernet",
1993 },
1994 { PCI_VENDOR_ALTIMA,
1995 PCI_PRODUCT_ALTIMA_AC1001,
1996 "Altima AC1001 Gigabit Ethernet",
1997 },
1998 { PCI_VENDOR_ALTIMA,
1999 PCI_PRODUCT_ALTIMA_AC9100,
2000 "Altima AC9100 Gigabit Ethernet",
2001 },
2002
2003 { PCI_VENDOR_BROADCOM,
2004 PCI_PRODUCT_BROADCOM_BCM5700,
2005 "Broadcom BCM5700 Gigabit Ethernet",
2006 },
2007 { PCI_VENDOR_BROADCOM,
2008 PCI_PRODUCT_BROADCOM_BCM5701,
2009 "Broadcom BCM5701 Gigabit Ethernet",
2010 },
2011 { PCI_VENDOR_BROADCOM,
2012 PCI_PRODUCT_BROADCOM_BCM5702,
2013 "Broadcom BCM5702 Gigabit Ethernet",
2014 },
2015 { PCI_VENDOR_BROADCOM,
2016 PCI_PRODUCT_BROADCOM_BCM5702X,
2017 "Broadcom BCM5702X Gigabit Ethernet" },
2018
2019 { PCI_VENDOR_BROADCOM,
2020 PCI_PRODUCT_BROADCOM_BCM5703,
2021 "Broadcom BCM5703 Gigabit Ethernet",
2022 },
2023 { PCI_VENDOR_BROADCOM,
2024 PCI_PRODUCT_BROADCOM_BCM5703X,
2025 "Broadcom BCM5703X Gigabit Ethernet",
2026 },
2027 { PCI_VENDOR_BROADCOM,
2028 PCI_PRODUCT_BROADCOM_BCM5703A3,
2029 "Broadcom BCM5703A3 Gigabit Ethernet",
2030 },
2031
2032 { PCI_VENDOR_BROADCOM,
2033 PCI_PRODUCT_BROADCOM_BCM5704C,
2034 "Broadcom BCM5704C Dual Gigabit Ethernet",
2035 },
2036 { PCI_VENDOR_BROADCOM,
2037 PCI_PRODUCT_BROADCOM_BCM5704S,
2038 "Broadcom BCM5704S Dual Gigabit Ethernet",
2039 },
2040
2041 { PCI_VENDOR_BROADCOM,
2042 PCI_PRODUCT_BROADCOM_BCM5705,
2043 "Broadcom BCM5705 Gigabit Ethernet",
2044 },
2045 { PCI_VENDOR_BROADCOM,
2046 PCI_PRODUCT_BROADCOM_BCM5705_ALT,
2047 "Broadcom BCM5705 Gigabit Ethernet",
2048 },
2049 { PCI_VENDOR_BROADCOM,
2050 PCI_PRODUCT_BROADCOM_BCM5705M,
2051 "Broadcom BCM5705M Gigabit Ethernet",
2052 },
2053
2054 { PCI_VENDOR_BROADCOM,
2055 PCI_PRODUCT_BROADCOM_BCM5782,
2056 "Broadcom BCM5782 Gigabit Ethernet",
2057 },
2058 { PCI_VENDOR_BROADCOM,
2059 PCI_PRODUCT_BROADCOM_BCM5788,
2060 "Broadcom BCM5788 Gigabit Ethernet",
2061 },
2062
2063 { PCI_VENDOR_BROADCOM,
2064 PCI_PRODUCT_BROADCOM_BCM5901,
2065 "Broadcom BCM5901 Fast Ethernet",
2066 },
2067 { PCI_VENDOR_BROADCOM,
2068 PCI_PRODUCT_BROADCOM_BCM5901A2,
2069 "Broadcom BCM5901A2 Fast Ethernet",
2070 },
2071
2072 { PCI_VENDOR_SCHNEIDERKOCH,
2073 PCI_PRODUCT_SCHNEIDERKOCH_SK_9DX1,
2074 "SysKonnect SK-9Dx1 Gigabit Ethernet",
2075 },
2076
2077 { PCI_VENDOR_3COM,
2078 PCI_PRODUCT_3COM_3C996,
2079 "3Com 3c996 Gigabit Ethernet",
2080 },
2081
2082 { 0,
2083 0,
2084 NULL },
2085 };
2086
2087 static const struct bge_product *
2088 bge_lookup(const struct pci_attach_args *pa)
2089 {
2090 const struct bge_product *bp;
2091
2092 for (bp = bge_products; bp->bp_name != NULL; bp++) {
2093 if (PCI_VENDOR(pa->pa_id) == bp->bp_vendor &&
2094 PCI_PRODUCT(pa->pa_id) == bp->bp_product)
2095 return (bp);
2096 }
2097
2098 return (NULL);
2099 }
2100
2101 int
2102 bge_setpowerstate(sc, powerlevel)
2103 struct bge_softc *sc;
2104 int powerlevel;
2105 {
2106 #ifdef NOTYET
2107 u_int32_t pm_ctl = 0;
2108
2109 /* XXX FIXME: make sure indirect accesses enabled? */
2110 pm_ctl = pci_conf_read(sc->bge_dev, BGE_PCI_MISC_CTL, 4);
2111 pm_ctl |= BGE_PCIMISCCTL_INDIRECT_ACCESS;
2112 pci_write_config(sc->bge_dev, BGE_PCI_MISC_CTL, pm_ctl, 4);
2113
2114 /* clear the PME_assert bit and power state bits, enable PME */
2115 pm_ctl = pci_conf_read(sc->bge_dev, BGE_PCI_PWRMGMT_CMD, 2);
2116 pm_ctl &= ~PCIM_PSTAT_DMASK;
2117 pm_ctl |= (1 << 8);
2118
2119 if (powerlevel == 0) {
2120 pm_ctl |= PCIM_PSTAT_D0;
2121 pci_write_config(sc->bge_dev, BGE_PCI_PWRMGMT_CMD,
2122 pm_ctl, 2);
2123 DELAY(10000);
2124 CSR_WRITE_4(sc, BGE_MISC_LOCAL_CTL, sc->bge_local_ctrl_reg);
2125 DELAY(10000);
2126
2127 #ifdef NOTYET
2128 /* XXX FIXME: write 0x02 to phy aux_Ctrl reg */
2129 bge_miibus_writereg(sc->bge_dev, 1, 0x18, 0x02);
2130 #endif
2131 DELAY(40); DELAY(40); DELAY(40);
2132 DELAY(10000); /* above not quite adequate on 5700 */
2133 return 0;
2134 }
2135
2136
2137 /*
2138 * Entering ACPI power states D1-D3 is achieved by wiggling
2139 * GMII gpio pins. Example code assumes all hardware vendors
2140 * followed Broadom's sample pcb layout. Until we verify that
2141 * for all supported OEM cards, states D1-D3 are unsupported.
2142 */
2143 printf("%s: power state %d unimplemented; check GPIO pins\n",
2144 sc->bge_dev.dv_xname, powerlevel);
2145 #endif
2146 return EOPNOTSUPP;
2147 }
2148
2149
2150 /*
2151 * Probe for a Broadcom chip. Check the PCI vendor and device IDs
2152 * against our list and return its name if we find a match. Note
2153 * that since the Broadcom controller contains VPD support, we
2154 * can get the device name string from the controller itself instead
2155 * of the compiled-in string. This is a little slow, but it guarantees
2156 * we'll always announce the right product name.
2157 */
2158 int
2159 bge_probe(parent, match, aux)
2160 struct device *parent;
2161 struct cfdata *match;
2162 void *aux;
2163 {
2164 struct pci_attach_args *pa = (struct pci_attach_args *)aux;
2165
2166 if (bge_lookup(pa) != NULL)
2167 return (1);
2168
2169 return (0);
2170 }
2171
2172 void
2173 bge_attach(parent, self, aux)
2174 struct device *parent, *self;
2175 void *aux;
2176 {
2177 struct bge_softc *sc = (struct bge_softc *)self;
2178 struct pci_attach_args *pa = aux;
2179 const struct bge_product *bp;
2180 const struct bge_revision *br;
2181 pci_chipset_tag_t pc = pa->pa_pc;
2182 pci_intr_handle_t ih;
2183 const char *intrstr = NULL;
2184 bus_dma_segment_t seg;
2185 int rseg;
2186 u_int32_t hwcfg = 0;
2187 u_int32_t mac_addr = 0;
2188 u_int32_t command;
2189 struct ifnet *ifp;
2190 caddr_t kva;
2191 u_char eaddr[ETHER_ADDR_LEN];
2192 pcireg_t memtype;
2193 bus_addr_t memaddr;
2194 bus_size_t memsize;
2195 u_int32_t pm_ctl;
2196
2197 bp = bge_lookup(pa);
2198 KASSERT(bp != NULL);
2199
2200 sc->bge_pa = *pa;
2201
2202 aprint_naive(": Ethernet controller\n");
2203 aprint_normal(": %s\n", bp->bp_name);
2204
2205 /*
2206 * Map control/status registers.
2207 */
2208 DPRINTFN(5, ("Map control/status regs\n"));
2209 command = pci_conf_read(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
2210 command |= PCI_COMMAND_MEM_ENABLE | PCI_COMMAND_MASTER_ENABLE;
2211 pci_conf_write(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG, command);
2212 command = pci_conf_read(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
2213
2214 if (!(command & PCI_COMMAND_MEM_ENABLE)) {
2215 aprint_error("%s: failed to enable memory mapping!\n",
2216 sc->bge_dev.dv_xname);
2217 return;
2218 }
2219
2220 DPRINTFN(5, ("pci_mem_find\n"));
2221 memtype = pci_mapreg_type(pa->pa_pc, pa->pa_tag, BGE_PCI_BAR0);
2222 switch (memtype) {
2223 case PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT:
2224 case PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_64BIT:
2225 if (pci_mapreg_map(pa, BGE_PCI_BAR0,
2226 memtype, 0, &sc->bge_btag, &sc->bge_bhandle,
2227 &memaddr, &memsize) == 0)
2228 break;
2229 default:
2230 aprint_error("%s: can't find mem space\n",
2231 sc->bge_dev.dv_xname);
2232 return;
2233 }
2234
2235 DPRINTFN(5, ("pci_intr_map\n"));
2236 if (pci_intr_map(pa, &ih)) {
2237 aprint_error("%s: couldn't map interrupt\n",
2238 sc->bge_dev.dv_xname);
2239 return;
2240 }
2241
2242 DPRINTFN(5, ("pci_intr_string\n"));
2243 intrstr = pci_intr_string(pc, ih);
2244
2245 DPRINTFN(5, ("pci_intr_establish\n"));
2246 sc->bge_intrhand = pci_intr_establish(pc, ih, IPL_NET, bge_intr, sc);
2247
2248 if (sc->bge_intrhand == NULL) {
2249 aprint_error("%s: couldn't establish interrupt",
2250 sc->bge_dev.dv_xname);
2251 if (intrstr != NULL)
2252 aprint_normal(" at %s", intrstr);
2253 aprint_normal("\n");
2254 return;
2255 }
2256 aprint_normal("%s: interrupting at %s\n",
2257 sc->bge_dev.dv_xname, intrstr);
2258
2259 /*
2260 * Kludge for 5700 Bx bug: a hardware bug (PCIX byte enable?)
2261 * can clobber the chip's PCI config-space power control registers,
2262 * leaving the card in D3 powersave state.
2263 * We do not have memory-mapped registers in this state,
2264 * so force device into D0 state before starting initialization.
2265 */
2266 pm_ctl = pci_conf_read(pc, pa->pa_tag, BGE_PCI_PWRMGMT_CMD);
2267 pm_ctl &= ~(PCI_PWR_D0|PCI_PWR_D1|PCI_PWR_D2|PCI_PWR_D3);
2268 pm_ctl |= (1 << 8) | PCI_PWR_D0 ; /* D0 state */
2269 pci_conf_write(pc, pa->pa_tag, BGE_PCI_PWRMGMT_CMD, pm_ctl);
2270 DELAY(1000); /* 27 usec is allegedly sufficent */
2271
2272 /* Try to reset the chip. */
2273 DPRINTFN(5, ("bge_reset\n"));
2274 bge_reset(sc);
2275
2276 if (bge_chipinit(sc)) {
2277 aprint_error("%s: chip initialization failed\n",
2278 sc->bge_dev.dv_xname);
2279 bge_release_resources(sc);
2280 return;
2281 }
2282
2283 /*
2284 * Get station address from the EEPROM.
2285 */
2286 mac_addr = bge_readmem_ind(sc, 0x0c14);
2287 if ((mac_addr >> 16) == 0x484b) {
2288 eaddr[0] = (u_char)(mac_addr >> 8);
2289 eaddr[1] = (u_char)(mac_addr >> 0);
2290 mac_addr = bge_readmem_ind(sc, 0x0c18);
2291 eaddr[2] = (u_char)(mac_addr >> 24);
2292 eaddr[3] = (u_char)(mac_addr >> 16);
2293 eaddr[4] = (u_char)(mac_addr >> 8);
2294 eaddr[5] = (u_char)(mac_addr >> 0);
2295 } else if (bge_read_eeprom(sc, (caddr_t)eaddr,
2296 BGE_EE_MAC_OFFSET + 2, ETHER_ADDR_LEN)) {
2297 aprint_error("%s: failed to read station address\n",
2298 sc->bge_dev.dv_xname);
2299 bge_release_resources(sc);
2300 return;
2301 }
2302
2303 /*
2304 * Save ASIC rev. Look up any quirks associated with this
2305 * ASIC.
2306 */
2307 sc->bge_chipid =
2308 pci_conf_read(pa->pa_pc, pa->pa_tag, BGE_PCI_MISC_CTL) &
2309 BGE_PCIMISCCTL_ASICREV;
2310 br = bge_lookup_rev(sc->bge_chipid);
2311
2312 aprint_normal("%s: ", sc->bge_dev.dv_xname);
2313
2314 if (br == NULL) {
2315 aprint_normal("unknown ASIC (0x%04x)", sc->bge_chipid >> 16);
2316 sc->bge_quirks = 0;
2317 } else {
2318 aprint_normal("ASIC %s (0x%04x)",
2319 br->br_name, sc->bge_chipid >> 16);
2320 sc->bge_quirks |= br->br_quirks;
2321 }
2322 aprint_normal(", Ethernet address %s\n", ether_sprintf(eaddr));
2323
2324 /* Allocate the general information block and ring buffers. */
2325 if (pci_dma64_available(pa))
2326 sc->bge_dmatag = pa->pa_dmat64;
2327 else
2328 sc->bge_dmatag = pa->pa_dmat;
2329 DPRINTFN(5, ("bus_dmamem_alloc\n"));
2330 if (bus_dmamem_alloc(sc->bge_dmatag, sizeof(struct bge_ring_data),
2331 PAGE_SIZE, 0, &seg, 1, &rseg, BUS_DMA_NOWAIT)) {
2332 aprint_error("%s: can't alloc rx buffers\n",
2333 sc->bge_dev.dv_xname);
2334 return;
2335 }
2336 DPRINTFN(5, ("bus_dmamem_map\n"));
2337 if (bus_dmamem_map(sc->bge_dmatag, &seg, rseg,
2338 sizeof(struct bge_ring_data), &kva,
2339 BUS_DMA_NOWAIT)) {
2340 aprint_error("%s: can't map DMA buffers (%d bytes)\n",
2341 sc->bge_dev.dv_xname, (int)sizeof(struct bge_ring_data));
2342 bus_dmamem_free(sc->bge_dmatag, &seg, rseg);
2343 return;
2344 }
2345 DPRINTFN(5, ("bus_dmamem_create\n"));
2346 if (bus_dmamap_create(sc->bge_dmatag, sizeof(struct bge_ring_data), 1,
2347 sizeof(struct bge_ring_data), 0,
2348 BUS_DMA_NOWAIT, &sc->bge_ring_map)) {
2349 aprint_error("%s: can't create DMA map\n",
2350 sc->bge_dev.dv_xname);
2351 bus_dmamem_unmap(sc->bge_dmatag, kva,
2352 sizeof(struct bge_ring_data));
2353 bus_dmamem_free(sc->bge_dmatag, &seg, rseg);
2354 return;
2355 }
2356 DPRINTFN(5, ("bus_dmamem_load\n"));
2357 if (bus_dmamap_load(sc->bge_dmatag, sc->bge_ring_map, kva,
2358 sizeof(struct bge_ring_data), NULL,
2359 BUS_DMA_NOWAIT)) {
2360 bus_dmamap_destroy(sc->bge_dmatag, sc->bge_ring_map);
2361 bus_dmamem_unmap(sc->bge_dmatag, kva,
2362 sizeof(struct bge_ring_data));
2363 bus_dmamem_free(sc->bge_dmatag, &seg, rseg);
2364 return;
2365 }
2366
2367 DPRINTFN(5, ("bzero\n"));
2368 sc->bge_rdata = (struct bge_ring_data *)kva;
2369
2370 memset(sc->bge_rdata, 0, sizeof(struct bge_ring_data));
2371
2372 /* Try to allocate memory for jumbo buffers. */
2373 if ((sc->bge_quirks & BGE_QUIRK_5705_CORE) == 0) {
2374 if (bge_alloc_jumbo_mem(sc)) {
2375 aprint_error("%s: jumbo buffer allocation failed\n",
2376 sc->bge_dev.dv_xname);
2377 } else
2378 sc->ethercom.ec_capabilities |= ETHERCAP_JUMBO_MTU;
2379 }
2380
2381 /* Set default tuneable values. */
2382 sc->bge_stat_ticks = BGE_TICKS_PER_SEC;
2383 sc->bge_rx_coal_ticks = 150;
2384 sc->bge_rx_max_coal_bds = 64;
2385 #ifdef ORIG_WPAUL_VALUES
2386 sc->bge_tx_coal_ticks = 150;
2387 sc->bge_tx_max_coal_bds = 128;
2388 #else
2389 sc->bge_tx_coal_ticks = 300;
2390 sc->bge_tx_max_coal_bds = 400;
2391 #endif
2392
2393 /* Set up ifnet structure */
2394 ifp = &sc->ethercom.ec_if;
2395 ifp->if_softc = sc;
2396 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
2397 ifp->if_ioctl = bge_ioctl;
2398 ifp->if_start = bge_start;
2399 ifp->if_init = bge_init;
2400 ifp->if_watchdog = bge_watchdog;
2401 IFQ_SET_MAXLEN(&ifp->if_snd, max(BGE_TX_RING_CNT - 1, IFQ_MAXLEN));
2402 IFQ_SET_READY(&ifp->if_snd);
2403 DPRINTFN(5, ("bcopy\n"));
2404 strcpy(ifp->if_xname, sc->bge_dev.dv_xname);
2405
2406 if ((sc->bge_quirks & BGE_QUIRK_CSUM_BROKEN) == 0)
2407 sc->ethercom.ec_if.if_capabilities |=
2408 IFCAP_CSUM_IPv4 | IFCAP_CSUM_TCPv4 | IFCAP_CSUM_UDPv4;
2409 sc->ethercom.ec_capabilities |=
2410 ETHERCAP_VLAN_HWTAGGING | ETHERCAP_VLAN_MTU;
2411
2412 /*
2413 * Do MII setup.
2414 */
2415 DPRINTFN(5, ("mii setup\n"));
2416 sc->bge_mii.mii_ifp = ifp;
2417 sc->bge_mii.mii_readreg = bge_miibus_readreg;
2418 sc->bge_mii.mii_writereg = bge_miibus_writereg;
2419 sc->bge_mii.mii_statchg = bge_miibus_statchg;
2420
2421 /*
2422 * Figure out what sort of media we have by checking the
2423 * hardware config word in the first 32k of NIC internal memory,
2424 * or fall back to the config word in the EEPROM. Note: on some BCM5700
2425 * cards, this value appears to be unset. If that's the
2426 * case, we have to rely on identifying the NIC by its PCI
2427 * subsystem ID, as we do below for the SysKonnect SK-9D41.
2428 */
2429 if (bge_readmem_ind(sc, BGE_SOFTWARE_GENCOMM_SIG) == BGE_MAGIC_NUMBER) {
2430 hwcfg = bge_readmem_ind(sc, BGE_SOFTWARE_GENCOMM_NICCFG);
2431 } else {
2432 bge_read_eeprom(sc, (caddr_t)&hwcfg,
2433 BGE_EE_HWCFG_OFFSET, sizeof(hwcfg));
2434 hwcfg = be32toh(hwcfg);
2435 }
2436 if ((hwcfg & BGE_HWCFG_MEDIA) == BGE_MEDIA_FIBER)
2437 sc->bge_tbi = 1;
2438
2439 /* The SysKonnect SK-9D41 is a 1000baseSX card. */
2440 if ((pci_conf_read(pa->pa_pc, pa->pa_tag, BGE_PCI_SUBSYS) >> 16) ==
2441 SK_SUBSYSID_9D41)
2442 sc->bge_tbi = 1;
2443
2444 if (sc->bge_tbi) {
2445 ifmedia_init(&sc->bge_ifmedia, IFM_IMASK, bge_ifmedia_upd,
2446 bge_ifmedia_sts);
2447 ifmedia_add(&sc->bge_ifmedia, IFM_ETHER|IFM_1000_SX, 0, NULL);
2448 ifmedia_add(&sc->bge_ifmedia, IFM_ETHER|IFM_1000_SX|IFM_FDX,
2449 0, NULL);
2450 ifmedia_add(&sc->bge_ifmedia, IFM_ETHER|IFM_AUTO, 0, NULL);
2451 ifmedia_set(&sc->bge_ifmedia, IFM_ETHER|IFM_AUTO);
2452 } else {
2453 /*
2454 * Do transceiver setup.
2455 */
2456 ifmedia_init(&sc->bge_mii.mii_media, 0, bge_ifmedia_upd,
2457 bge_ifmedia_sts);
2458 mii_attach(&sc->bge_dev, &sc->bge_mii, 0xffffffff,
2459 MII_PHY_ANY, MII_OFFSET_ANY,
2460 MIIF_FORCEANEG|MIIF_DOPAUSE);
2461
2462 if (LIST_FIRST(&sc->bge_mii.mii_phys) == NULL) {
2463 printf("%s: no PHY found!\n", sc->bge_dev.dv_xname);
2464 ifmedia_add(&sc->bge_mii.mii_media,
2465 IFM_ETHER|IFM_MANUAL, 0, NULL);
2466 ifmedia_set(&sc->bge_mii.mii_media,
2467 IFM_ETHER|IFM_MANUAL);
2468 } else
2469 ifmedia_set(&sc->bge_mii.mii_media,
2470 IFM_ETHER|IFM_AUTO);
2471 }
2472
2473 /*
2474 * When using the BCM5701 in PCI-X mode, data corruption has
2475 * been observed in the first few bytes of some received packets.
2476 * Aligning the packet buffer in memory eliminates the corruption.
2477 * Unfortunately, this misaligns the packet payloads. On platforms
2478 * which do not support unaligned accesses, we will realign the
2479 * payloads by copying the received packets.
2480 */
2481 if (sc->bge_quirks & BGE_QUIRK_PCIX_DMA_ALIGN_BUG) {
2482 /* If in PCI-X mode, work around the alignment bug. */
2483 if ((pci_conf_read(pa->pa_pc, pa->pa_tag, BGE_PCI_PCISTATE) &
2484 (BGE_PCISTATE_PCI_BUSMODE | BGE_PCISTATE_PCI_BUSSPEED)) ==
2485 BGE_PCISTATE_PCI_BUSSPEED)
2486 sc->bge_rx_alignment_bug = 1;
2487 }
2488
2489 /*
2490 * Call MI attach routine.
2491 */
2492 DPRINTFN(5, ("if_attach\n"));
2493 if_attach(ifp);
2494 DPRINTFN(5, ("ether_ifattach\n"));
2495 ether_ifattach(ifp, eaddr);
2496 DPRINTFN(5, ("callout_init\n"));
2497 callout_init(&sc->bge_timeout);
2498 }
2499
2500 void
2501 bge_release_resources(sc)
2502 struct bge_softc *sc;
2503 {
2504 if (sc->bge_vpd_prodname != NULL)
2505 free(sc->bge_vpd_prodname, M_DEVBUF);
2506
2507 if (sc->bge_vpd_readonly != NULL)
2508 free(sc->bge_vpd_readonly, M_DEVBUF);
2509 }
2510
2511 void
2512 bge_reset(sc)
2513 struct bge_softc *sc;
2514 {
2515 struct pci_attach_args *pa = &sc->bge_pa;
2516 u_int32_t cachesize, command, pcistate, new_pcistate;
2517 int i, val = 0;
2518
2519 /* Save some important PCI state. */
2520 cachesize = pci_conf_read(pa->pa_pc, pa->pa_tag, BGE_PCI_CACHESZ);
2521 command = pci_conf_read(pa->pa_pc, pa->pa_tag, BGE_PCI_CMD);
2522 pcistate = pci_conf_read(pa->pa_pc, pa->pa_tag, BGE_PCI_PCISTATE);
2523
2524 pci_conf_write(pa->pa_pc, pa->pa_tag, BGE_PCI_MISC_CTL,
2525 BGE_PCIMISCCTL_INDIRECT_ACCESS|BGE_PCIMISCCTL_MASK_PCI_INTR|
2526 BGE_HIF_SWAP_OPTIONS|BGE_PCIMISCCTL_PCISTATE_RW);
2527
2528 /* Issue global reset */
2529 bge_writereg_ind(sc, BGE_MISC_CFG,
2530 BGE_MISCCFG_RESET_CORE_CLOCKS|(65<<1));
2531
2532 DELAY(1000);
2533
2534 /* Reset some of the PCI state that got zapped by reset */
2535 pci_conf_write(pa->pa_pc, pa->pa_tag, BGE_PCI_MISC_CTL,
2536 BGE_PCIMISCCTL_INDIRECT_ACCESS|BGE_PCIMISCCTL_MASK_PCI_INTR|
2537 BGE_HIF_SWAP_OPTIONS|BGE_PCIMISCCTL_PCISTATE_RW);
2538 pci_conf_write(pa->pa_pc, pa->pa_tag, BGE_PCI_CMD, command);
2539 pci_conf_write(pa->pa_pc, pa->pa_tag, BGE_PCI_CACHESZ, cachesize);
2540 bge_writereg_ind(sc, BGE_MISC_CFG, (65 << 1));
2541
2542 /* Enable memory arbiter. */
2543 if ((sc->bge_quirks & BGE_QUIRK_5705_CORE) == 0) {
2544 CSR_WRITE_4(sc, BGE_MARB_MODE, BGE_MARBMODE_ENABLE);
2545 }
2546
2547 /*
2548 * Prevent PXE restart: write a magic number to the
2549 * general communications memory at 0xB50.
2550 */
2551 bge_writemem_ind(sc, BGE_SOFTWARE_GENCOMM, BGE_MAGIC_NUMBER);
2552
2553 /*
2554 * Poll the value location we just wrote until
2555 * we see the 1's complement of the magic number.
2556 * This indicates that the firmware initialization
2557 * is complete.
2558 */
2559 for (i = 0; i < 750; i++) {
2560 val = bge_readmem_ind(sc, BGE_SOFTWARE_GENCOMM);
2561 if (val == ~BGE_MAGIC_NUMBER)
2562 break;
2563 DELAY(1000);
2564 }
2565
2566 if (i == 750) {
2567 printf("%s: firmware handshake timed out, val = %x\n",
2568 sc->bge_dev.dv_xname, val);
2569 return;
2570 }
2571
2572 /*
2573 * XXX Wait for the value of the PCISTATE register to
2574 * return to its original pre-reset state. This is a
2575 * fairly good indicator of reset completion. If we don't
2576 * wait for the reset to fully complete, trying to read
2577 * from the device's non-PCI registers may yield garbage
2578 * results.
2579 */
2580 for (i = 0; i < BGE_TIMEOUT; i++) {
2581 new_pcistate = pci_conf_read(pa->pa_pc, pa->pa_tag,
2582 BGE_PCI_PCISTATE);
2583 if ((new_pcistate & ~BGE_PCISTATE_RESERVED) ==
2584 (pcistate & ~BGE_PCISTATE_RESERVED))
2585 break;
2586 DELAY(10);
2587 }
2588 if ((new_pcistate & ~BGE_PCISTATE_RESERVED) !=
2589 (pcistate & ~BGE_PCISTATE_RESERVED)) {
2590 printf("%s: pcistate failed to revert\n",
2591 sc->bge_dev.dv_xname);
2592 }
2593
2594 /* Enable memory arbiter. */
2595 if ((sc->bge_quirks & BGE_QUIRK_5705_CORE) == 0) {
2596 CSR_WRITE_4(sc, BGE_MARB_MODE, BGE_MARBMODE_ENABLE);
2597 }
2598
2599 /* Fix up byte swapping */
2600 CSR_WRITE_4(sc, BGE_MODE_CTL, BGE_DMA_SWAP_OPTIONS);
2601
2602 CSR_WRITE_4(sc, BGE_MAC_MODE, 0);
2603
2604 DELAY(10000);
2605 }
2606
2607 /*
2608 * Frame reception handling. This is called if there's a frame
2609 * on the receive return list.
2610 *
2611 * Note: we have to be able to handle two possibilities here:
2612 * 1) the frame is from the jumbo recieve ring
2613 * 2) the frame is from the standard receive ring
2614 */
2615
2616 void
2617 bge_rxeof(sc)
2618 struct bge_softc *sc;
2619 {
2620 struct ifnet *ifp;
2621 int stdcnt = 0, jumbocnt = 0;
2622 int have_tag = 0;
2623 u_int16_t vlan_tag = 0;
2624 bus_dmamap_t dmamap;
2625 bus_addr_t offset, toff;
2626 bus_size_t tlen;
2627 int tosync;
2628
2629 ifp = &sc->ethercom.ec_if;
2630
2631 bus_dmamap_sync(sc->bge_dmatag, sc->bge_ring_map,
2632 offsetof(struct bge_ring_data, bge_status_block),
2633 sizeof (struct bge_status_block),
2634 BUS_DMASYNC_POSTREAD);
2635
2636 offset = offsetof(struct bge_ring_data, bge_rx_return_ring);
2637 tosync = sc->bge_rdata->bge_status_block.bge_idx[0].bge_rx_prod_idx -
2638 sc->bge_rx_saved_considx;
2639
2640 toff = offset + (sc->bge_rx_saved_considx * sizeof (struct bge_rx_bd));
2641
2642 if (tosync < 0) {
2643 tlen = (sc->bge_return_ring_cnt - sc->bge_rx_saved_considx) *
2644 sizeof (struct bge_rx_bd);
2645 bus_dmamap_sync(sc->bge_dmatag, sc->bge_ring_map,
2646 toff, tlen, BUS_DMASYNC_POSTREAD);
2647 tosync = -tosync;
2648 }
2649
2650 bus_dmamap_sync(sc->bge_dmatag, sc->bge_ring_map,
2651 offset, tosync * sizeof (struct bge_rx_bd),
2652 BUS_DMASYNC_POSTREAD);
2653
2654 while(sc->bge_rx_saved_considx !=
2655 sc->bge_rdata->bge_status_block.bge_idx[0].bge_rx_prod_idx) {
2656 struct bge_rx_bd *cur_rx;
2657 u_int32_t rxidx;
2658 struct mbuf *m = NULL;
2659
2660 cur_rx = &sc->bge_rdata->
2661 bge_rx_return_ring[sc->bge_rx_saved_considx];
2662
2663 rxidx = cur_rx->bge_idx;
2664 BGE_INC(sc->bge_rx_saved_considx, sc->bge_return_ring_cnt);
2665
2666 if (cur_rx->bge_flags & BGE_RXBDFLAG_VLAN_TAG) {
2667 have_tag = 1;
2668 vlan_tag = cur_rx->bge_vlan_tag;
2669 }
2670
2671 if (cur_rx->bge_flags & BGE_RXBDFLAG_JUMBO_RING) {
2672 BGE_INC(sc->bge_jumbo, BGE_JUMBO_RX_RING_CNT);
2673 m = sc->bge_cdata.bge_rx_jumbo_chain[rxidx];
2674 sc->bge_cdata.bge_rx_jumbo_chain[rxidx] = NULL;
2675 jumbocnt++;
2676 if (cur_rx->bge_flags & BGE_RXBDFLAG_ERROR) {
2677 ifp->if_ierrors++;
2678 bge_newbuf_jumbo(sc, sc->bge_jumbo, m);
2679 continue;
2680 }
2681 if (bge_newbuf_jumbo(sc, sc->bge_jumbo,
2682 NULL)== ENOBUFS) {
2683 ifp->if_ierrors++;
2684 bge_newbuf_jumbo(sc, sc->bge_jumbo, m);
2685 continue;
2686 }
2687 } else {
2688 BGE_INC(sc->bge_std, BGE_STD_RX_RING_CNT);
2689 m = sc->bge_cdata.bge_rx_std_chain[rxidx];
2690 sc->bge_cdata.bge_rx_std_chain[rxidx] = NULL;
2691 stdcnt++;
2692 dmamap = sc->bge_cdata.bge_rx_std_map[rxidx];
2693 sc->bge_cdata.bge_rx_std_map[rxidx] = 0;
2694 if (cur_rx->bge_flags & BGE_RXBDFLAG_ERROR) {
2695 ifp->if_ierrors++;
2696 bge_newbuf_std(sc, sc->bge_std, m, dmamap);
2697 continue;
2698 }
2699 if (bge_newbuf_std(sc, sc->bge_std,
2700 NULL, dmamap) == ENOBUFS) {
2701 ifp->if_ierrors++;
2702 bge_newbuf_std(sc, sc->bge_std, m, dmamap);
2703 continue;
2704 }
2705 }
2706
2707 ifp->if_ipackets++;
2708 #ifndef __NO_STRICT_ALIGNMENT
2709 /*
2710 * XXX: if the 5701 PCIX-Rx-DMA workaround is in effect,
2711 * the Rx buffer has the layer-2 header unaligned.
2712 * If our CPU requires alignment, re-align by copying.
2713 */
2714 if (sc->bge_rx_alignment_bug) {
2715 memmove(mtod(m, caddr_t) + ETHER_ALIGN, m->m_data,
2716 cur_rx->bge_len);
2717 m->m_data += ETHER_ALIGN;
2718 }
2719 #endif
2720
2721 m->m_pkthdr.len = m->m_len = cur_rx->bge_len - ETHER_CRC_LEN;
2722 m->m_pkthdr.rcvif = ifp;
2723
2724 #if NBPFILTER > 0
2725 /*
2726 * Handle BPF listeners. Let the BPF user see the packet.
2727 */
2728 if (ifp->if_bpf)
2729 bpf_mtap(ifp->if_bpf, m);
2730 #endif
2731
2732 m->m_pkthdr.csum_flags = M_CSUM_IPv4;
2733
2734 if ((cur_rx->bge_ip_csum ^ 0xffff) != 0)
2735 m->m_pkthdr.csum_flags |= M_CSUM_IPv4_BAD;
2736 /*
2737 * Rx transport checksum-offload may also
2738 * have bugs with packets which, when transmitted,
2739 * were `runts' requiring padding.
2740 */
2741 if (cur_rx->bge_flags & BGE_RXBDFLAG_TCP_UDP_CSUM &&
2742 (/* (sc->_bge_quirks & BGE_QUIRK_SHORT_CKSUM_BUG) == 0 ||*/
2743 m->m_pkthdr.len >= ETHER_MIN_NOPAD)) {
2744 m->m_pkthdr.csum_data =
2745 cur_rx->bge_tcp_udp_csum;
2746 m->m_pkthdr.csum_flags |=
2747 (M_CSUM_TCPv4|M_CSUM_UDPv4|
2748 M_CSUM_DATA|M_CSUM_NO_PSEUDOHDR);
2749 }
2750
2751 /*
2752 * If we received a packet with a vlan tag, pass it
2753 * to vlan_input() instead of ether_input().
2754 */
2755 if (have_tag) {
2756 struct m_tag *mtag;
2757
2758 mtag = m_tag_get(PACKET_TAG_VLAN, sizeof(u_int),
2759 M_NOWAIT);
2760 if (mtag != NULL) {
2761 *(u_int *)(mtag + 1) = vlan_tag;
2762 m_tag_prepend(m, mtag);
2763 have_tag = vlan_tag = 0;
2764 } else {
2765 printf("%s: no mbuf for tag\n", ifp->if_xname);
2766 m_freem(m);
2767 have_tag = vlan_tag = 0;
2768 continue;
2769 }
2770 }
2771 (*ifp->if_input)(ifp, m);
2772 }
2773
2774 CSR_WRITE_4(sc, BGE_MBX_RX_CONS0_LO, sc->bge_rx_saved_considx);
2775 if (stdcnt)
2776 CSR_WRITE_4(sc, BGE_MBX_RX_STD_PROD_LO, sc->bge_std);
2777 if (jumbocnt)
2778 CSR_WRITE_4(sc, BGE_MBX_RX_JUMBO_PROD_LO, sc->bge_jumbo);
2779 }
2780
2781 void
2782 bge_txeof(sc)
2783 struct bge_softc *sc;
2784 {
2785 struct bge_tx_bd *cur_tx = NULL;
2786 struct ifnet *ifp;
2787 struct txdmamap_pool_entry *dma;
2788 bus_addr_t offset, toff;
2789 bus_size_t tlen;
2790 int tosync;
2791 struct mbuf *m;
2792
2793 ifp = &sc->ethercom.ec_if;
2794
2795 bus_dmamap_sync(sc->bge_dmatag, sc->bge_ring_map,
2796 offsetof(struct bge_ring_data, bge_status_block),
2797 sizeof (struct bge_status_block),
2798 BUS_DMASYNC_POSTREAD);
2799
2800 offset = offsetof(struct bge_ring_data, bge_tx_ring);
2801 tosync = sc->bge_rdata->bge_status_block.bge_idx[0].bge_tx_cons_idx -
2802 sc->bge_tx_saved_considx;
2803
2804 toff = offset + (sc->bge_tx_saved_considx * sizeof (struct bge_tx_bd));
2805
2806 if (tosync < 0) {
2807 tlen = (BGE_TX_RING_CNT - sc->bge_tx_saved_considx) *
2808 sizeof (struct bge_tx_bd);
2809 bus_dmamap_sync(sc->bge_dmatag, sc->bge_ring_map,
2810 toff, tlen, BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
2811 tosync = -tosync;
2812 }
2813
2814 bus_dmamap_sync(sc->bge_dmatag, sc->bge_ring_map,
2815 offset, tosync * sizeof (struct bge_tx_bd),
2816 BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
2817
2818 /*
2819 * Go through our tx ring and free mbufs for those
2820 * frames that have been sent.
2821 */
2822 while (sc->bge_tx_saved_considx !=
2823 sc->bge_rdata->bge_status_block.bge_idx[0].bge_tx_cons_idx) {
2824 u_int32_t idx = 0;
2825
2826 idx = sc->bge_tx_saved_considx;
2827 cur_tx = &sc->bge_rdata->bge_tx_ring[idx];
2828 if (cur_tx->bge_flags & BGE_TXBDFLAG_END)
2829 ifp->if_opackets++;
2830 m = sc->bge_cdata.bge_tx_chain[idx];
2831 if (m != NULL) {
2832 sc->bge_cdata.bge_tx_chain[idx] = NULL;
2833 dma = sc->txdma[idx];
2834 bus_dmamap_sync(sc->bge_dmatag, dma->dmamap, 0,
2835 dma->dmamap->dm_mapsize, BUS_DMASYNC_POSTWRITE);
2836 bus_dmamap_unload(sc->bge_dmatag, dma->dmamap);
2837 SLIST_INSERT_HEAD(&sc->txdma_list, dma, link);
2838 sc->txdma[idx] = NULL;
2839
2840 m_freem(m);
2841 }
2842 sc->bge_txcnt--;
2843 BGE_INC(sc->bge_tx_saved_considx, BGE_TX_RING_CNT);
2844 ifp->if_timer = 0;
2845 }
2846
2847 if (cur_tx != NULL)
2848 ifp->if_flags &= ~IFF_OACTIVE;
2849 }
2850
2851 int
2852 bge_intr(xsc)
2853 void *xsc;
2854 {
2855 struct bge_softc *sc;
2856 struct ifnet *ifp;
2857
2858 sc = xsc;
2859 ifp = &sc->ethercom.ec_if;
2860
2861 #ifdef notdef
2862 /* Avoid this for now -- checking this register is expensive. */
2863 /* Make sure this is really our interrupt. */
2864 if (!(CSR_READ_4(sc, BGE_MISC_LOCAL_CTL) & BGE_MLC_INTR_STATE))
2865 return (0);
2866 #endif
2867 /* Ack interrupt and stop others from occuring. */
2868 CSR_WRITE_4(sc, BGE_MBX_IRQ0_LO, 1);
2869
2870 /*
2871 * Process link state changes.
2872 * Grrr. The link status word in the status block does
2873 * not work correctly on the BCM5700 rev AX and BX chips,
2874 * according to all avaibable information. Hence, we have
2875 * to enable MII interrupts in order to properly obtain
2876 * async link changes. Unfortunately, this also means that
2877 * we have to read the MAC status register to detect link
2878 * changes, thereby adding an additional register access to
2879 * the interrupt handler.
2880 */
2881
2882 if (sc->bge_quirks & BGE_QUIRK_LINK_STATE_BROKEN) {
2883 u_int32_t status;
2884
2885 status = CSR_READ_4(sc, BGE_MAC_STS);
2886 if (status & BGE_MACSTAT_MI_INTERRUPT) {
2887 sc->bge_link = 0;
2888 callout_stop(&sc->bge_timeout);
2889 bge_tick(sc);
2890 /* Clear the interrupt */
2891 CSR_WRITE_4(sc, BGE_MAC_EVT_ENB,
2892 BGE_EVTENB_MI_INTERRUPT);
2893 bge_miibus_readreg(&sc->bge_dev, 1, BRGPHY_MII_ISR);
2894 bge_miibus_writereg(&sc->bge_dev, 1, BRGPHY_MII_IMR,
2895 BRGPHY_INTRS);
2896 }
2897 } else {
2898 if (sc->bge_rdata->bge_status_block.bge_status &
2899 BGE_STATFLAG_LINKSTATE_CHANGED) {
2900 sc->bge_link = 0;
2901 callout_stop(&sc->bge_timeout);
2902 bge_tick(sc);
2903 /* Clear the interrupt */
2904 CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED|
2905 BGE_MACSTAT_CFG_CHANGED|BGE_MACSTAT_MI_COMPLETE|
2906 BGE_MACSTAT_LINK_CHANGED);
2907 }
2908 }
2909
2910 if (ifp->if_flags & IFF_RUNNING) {
2911 /* Check RX return ring producer/consumer */
2912 bge_rxeof(sc);
2913
2914 /* Check TX ring producer/consumer */
2915 bge_txeof(sc);
2916 }
2917
2918 if (sc->bge_pending_rxintr_change) {
2919 uint32_t rx_ticks = sc->bge_rx_coal_ticks;
2920 uint32_t rx_bds = sc->bge_rx_max_coal_bds;
2921 uint32_t junk;
2922
2923 CSR_WRITE_4(sc, BGE_HCC_RX_COAL_TICKS, rx_ticks);
2924 DELAY(10);
2925 junk = CSR_READ_4(sc, BGE_HCC_RX_COAL_TICKS);
2926
2927 CSR_WRITE_4(sc, BGE_HCC_RX_MAX_COAL_BDS, rx_bds);
2928 DELAY(10);
2929 junk = CSR_READ_4(sc, BGE_HCC_RX_MAX_COAL_BDS);
2930
2931 sc->bge_pending_rxintr_change = 0;
2932 }
2933 bge_handle_events(sc);
2934
2935 /* Re-enable interrupts. */
2936 CSR_WRITE_4(sc, BGE_MBX_IRQ0_LO, 0);
2937
2938 if (ifp->if_flags & IFF_RUNNING && !IFQ_IS_EMPTY(&ifp->if_snd))
2939 bge_start(ifp);
2940
2941 return (1);
2942 }
2943
2944 void
2945 bge_tick(xsc)
2946 void *xsc;
2947 {
2948 struct bge_softc *sc = xsc;
2949 struct mii_data *mii = &sc->bge_mii;
2950 struct ifmedia *ifm = NULL;
2951 struct ifnet *ifp = &sc->ethercom.ec_if;
2952 int s;
2953
2954 s = splnet();
2955
2956 bge_stats_update(sc);
2957 callout_reset(&sc->bge_timeout, hz, bge_tick, sc);
2958 if (sc->bge_link) {
2959 splx(s);
2960 return;
2961 }
2962
2963 if (sc->bge_tbi) {
2964 ifm = &sc->bge_ifmedia;
2965 if (CSR_READ_4(sc, BGE_MAC_STS) &
2966 BGE_MACSTAT_TBI_PCS_SYNCHED) {
2967 sc->bge_link++;
2968 CSR_WRITE_4(sc, BGE_MAC_STS, 0xFFFFFFFF);
2969 if (!IFQ_IS_EMPTY(&ifp->if_snd))
2970 bge_start(ifp);
2971 }
2972 splx(s);
2973 return;
2974 }
2975
2976 mii_tick(mii);
2977
2978 if (!sc->bge_link && mii->mii_media_status & IFM_ACTIVE &&
2979 IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
2980 sc->bge_link++;
2981 if (!IFQ_IS_EMPTY(&ifp->if_snd))
2982 bge_start(ifp);
2983 }
2984
2985 splx(s);
2986 }
2987
2988 void
2989 bge_stats_update(sc)
2990 struct bge_softc *sc;
2991 {
2992 struct ifnet *ifp = &sc->ethercom.ec_if;
2993 bus_size_t stats = BGE_MEMWIN_START + BGE_STATS_BLOCK;
2994 bus_size_t rstats = BGE_RX_STATS;
2995
2996 #define READ_RSTAT(sc, stats, stat) \
2997 CSR_READ_4(sc, stats + offsetof(struct bge_mac_stats_regs, stat))
2998
2999 if (sc->bge_quirks & BGE_QUIRK_5705_CORE) {
3000 ifp->if_collisions +=
3001 READ_RSTAT(sc, rstats, dot3StatsSingleCollisionFrames) +
3002 READ_RSTAT(sc, rstats, dot3StatsMultipleCollisionFrames) +
3003 READ_RSTAT(sc, rstats, dot3StatsExcessiveCollisions) +
3004 READ_RSTAT(sc, rstats, dot3StatsLateCollisions);
3005 return;
3006 }
3007
3008 #undef READ_RSTAT
3009 #define READ_STAT(sc, stats, stat) \
3010 CSR_READ_4(sc, stats + offsetof(struct bge_stats, stat))
3011
3012 ifp->if_collisions +=
3013 (READ_STAT(sc, stats, dot3StatsSingleCollisionFrames.bge_addr_lo) +
3014 READ_STAT(sc, stats, dot3StatsMultipleCollisionFrames.bge_addr_lo) +
3015 READ_STAT(sc, stats, dot3StatsExcessiveCollisions.bge_addr_lo) +
3016 READ_STAT(sc, stats, dot3StatsLateCollisions.bge_addr_lo)) -
3017 ifp->if_collisions;
3018
3019 #undef READ_STAT
3020
3021 #ifdef notdef
3022 ifp->if_collisions +=
3023 (sc->bge_rdata->bge_info.bge_stats.dot3StatsSingleCollisionFrames +
3024 sc->bge_rdata->bge_info.bge_stats.dot3StatsMultipleCollisionFrames +
3025 sc->bge_rdata->bge_info.bge_stats.dot3StatsExcessiveCollisions +
3026 sc->bge_rdata->bge_info.bge_stats.dot3StatsLateCollisions) -
3027 ifp->if_collisions;
3028 #endif
3029 }
3030
3031 /*
3032 * Pad outbound frame to ETHER_MIN_NOPAD for an unusual reason.
3033 * The bge hardware will pad out Tx runts to ETHER_MIN_NOPAD,
3034 * but when such padded frames employ the bge IP/TCP checksum offload,
3035 * the hardware checksum assist gives incorrect results (possibly
3036 * from incorporating its own padding into the UDP/TCP checksum; who knows).
3037 * If we pad such runts with zeros, the onboard checksum comes out correct.
3038 */
3039 static __inline int
3040 bge_cksum_pad(struct mbuf *pkt)
3041 {
3042 struct mbuf *last = NULL;
3043 int padlen;
3044
3045 padlen = ETHER_MIN_NOPAD - pkt->m_pkthdr.len;
3046
3047 /* if there's only the packet-header and we can pad there, use it. */
3048 if (pkt->m_pkthdr.len == pkt->m_len &&
3049 !M_READONLY(pkt) && M_TRAILINGSPACE(pkt) >= padlen) {
3050 last = pkt;
3051 } else {
3052 /*
3053 * Walk packet chain to find last mbuf. We will either
3054 * pad there, or append a new mbuf and pad it
3055 * (thus perhaps avoiding the bcm5700 dma-min bug).
3056 */
3057 for (last = pkt; last->m_next != NULL; last = last->m_next) {
3058 (void) 0; /* do nothing*/
3059 }
3060
3061 /* `last' now points to last in chain. */
3062 if (!M_READONLY(last) && M_TRAILINGSPACE(last) >= padlen) {
3063 (void) 0; /* we can pad here, in-place. */
3064 } else {
3065 /* Allocate new empty mbuf, pad it. Compact later. */
3066 struct mbuf *n;
3067 MGET(n, M_DONTWAIT, MT_DATA);
3068 n->m_len = 0;
3069 last->m_next = n;
3070 last = n;
3071 }
3072 }
3073
3074 #ifdef DEBUG
3075 /*KASSERT(M_WRITABLE(last), ("to-pad mbuf not writeable\n"));*/
3076 KASSERT(M_TRAILINGSPACE(last) >= padlen /*, ("insufficient space to pad\n")*/ );
3077 #endif
3078 /* Now zero the pad area, to avoid the bge cksum-assist bug */
3079 memset(mtod(last, caddr_t) + last->m_len, 0, padlen);
3080 last->m_len += padlen;
3081 pkt->m_pkthdr.len += padlen;
3082 return 0;
3083 }
3084
3085 /*
3086 * Compact outbound packets to avoid bug with DMA segments less than 8 bytes.
3087 */
3088 static __inline int
3089 bge_compact_dma_runt(struct mbuf *pkt)
3090 {
3091 struct mbuf *m, *prev;
3092 int totlen, prevlen;
3093
3094 prev = NULL;
3095 totlen = 0;
3096 prevlen = -1;
3097
3098 for (m = pkt; m != NULL; prev = m,m = m->m_next) {
3099 int mlen = m->m_len;
3100 int shortfall = 8 - mlen ;
3101
3102 totlen += mlen;
3103 if (mlen == 0) {
3104 continue;
3105 }
3106 if (mlen >= 8)
3107 continue;
3108
3109 /* If we get here, mbuf data is too small for DMA engine.
3110 * Try to fix by shuffling data to prev or next in chain.
3111 * If that fails, do a compacting deep-copy of the whole chain.
3112 */
3113
3114 /* Internal frag. If fits in prev, copy it there. */
3115 if (prev && !M_READONLY(prev) &&
3116 M_TRAILINGSPACE(prev) >= m->m_len) {
3117 bcopy(m->m_data,
3118 prev->m_data+prev->m_len,
3119 mlen);
3120 prev->m_len += mlen;
3121 m->m_len = 0;
3122 /* XXX stitch chain */
3123 prev->m_next = m_free(m);
3124 m = prev;
3125 continue;
3126 }
3127 else if (m->m_next != NULL && !M_READONLY(m) &&
3128 M_TRAILINGSPACE(m) >= shortfall &&
3129 m->m_next->m_len >= (8 + shortfall)) {
3130 /* m is writable and have enough data in next, pull up. */
3131
3132 bcopy(m->m_next->m_data,
3133 m->m_data+m->m_len,
3134 shortfall);
3135 m->m_len += shortfall;
3136 m->m_next->m_len -= shortfall;
3137 m->m_next->m_data += shortfall;
3138 }
3139 else if (m->m_next == NULL || 1) {
3140 /* Got a runt at the very end of the packet.
3141 * borrow data from the tail of the preceding mbuf and
3142 * update its length in-place. (The original data is still
3143 * valid, so we can do this even if prev is not writable.)
3144 */
3145
3146 /* if we'd make prev a runt, just move all of its data. */
3147 #ifdef DEBUG
3148 KASSERT(prev != NULL /*, ("runt but null PREV")*/);
3149 KASSERT(prev->m_len >= 8 /*, ("runt prev")*/);
3150 #endif
3151 if ((prev->m_len - shortfall) < 8)
3152 shortfall = prev->m_len;
3153
3154 #ifdef notyet /* just do the safe slow thing for now */
3155 if (!M_READONLY(m)) {
3156 if (M_LEADINGSPACE(m) < shorfall) {
3157 void *m_dat;
3158 m_dat = (m->m_flags & M_PKTHDR) ?
3159 m->m_pktdat : m->dat;
3160 memmove(m_dat, mtod(m, void*), m->m_len);
3161 m->m_data = m_dat;
3162 }
3163 } else
3164 #endif /* just do the safe slow thing */
3165 {
3166 struct mbuf * n = NULL;
3167 int newprevlen = prev->m_len - shortfall;
3168
3169 MGET(n, M_NOWAIT, MT_DATA);
3170 if (n == NULL)
3171 return ENOBUFS;
3172 KASSERT(m->m_len + shortfall < MLEN
3173 /*,
3174 ("runt %d +prev %d too big\n", m->m_len, shortfall)*/);
3175
3176 /* first copy the data we're stealing from prev */
3177 bcopy(prev->m_data + newprevlen, n->m_data, shortfall);
3178
3179 /* update prev->m_len accordingly */
3180 prev->m_len -= shortfall;
3181
3182 /* copy data from runt m */
3183 bcopy(m->m_data, n->m_data + shortfall, m->m_len);
3184
3185 /* n holds what we stole from prev, plus m */
3186 n->m_len = shortfall + m->m_len;
3187
3188 /* stitch n into chain and free m */
3189 n->m_next = m->m_next;
3190 prev->m_next = n;
3191 /* KASSERT(m->m_next == NULL); */
3192 m->m_next = NULL;
3193 m_free(m);
3194 m = n; /* for continuing loop */
3195 }
3196 }
3197 prevlen = m->m_len;
3198 }
3199 return 0;
3200 }
3201
3202 /*
3203 * Encapsulate an mbuf chain in the tx ring by coupling the mbuf data
3204 * pointers to descriptors.
3205 */
3206 int
3207 bge_encap(sc, m_head, txidx)
3208 struct bge_softc *sc;
3209 struct mbuf *m_head;
3210 u_int32_t *txidx;
3211 {
3212 struct bge_tx_bd *f = NULL;
3213 u_int32_t frag, cur, cnt = 0;
3214 u_int16_t csum_flags = 0;
3215 struct txdmamap_pool_entry *dma;
3216 bus_dmamap_t dmamap;
3217 int i = 0;
3218 struct m_tag *mtag;
3219
3220 cur = frag = *txidx;
3221
3222 if (m_head->m_pkthdr.csum_flags) {
3223 if (m_head->m_pkthdr.csum_flags & M_CSUM_IPv4)
3224 csum_flags |= BGE_TXBDFLAG_IP_CSUM;
3225 if (m_head->m_pkthdr.csum_flags & (M_CSUM_TCPv4|M_CSUM_UDPv4))
3226 csum_flags |= BGE_TXBDFLAG_TCP_UDP_CSUM;
3227 }
3228
3229 /*
3230 * If we were asked to do an outboard checksum, and the NIC
3231 * has the bug where it sometimes adds in the Ethernet padding,
3232 * explicitly pad with zeros so the cksum will be correct either way.
3233 * (For now, do this for all chip versions, until newer
3234 * are confirmed to not require the workaround.)
3235 */
3236 if ((csum_flags & BGE_TXBDFLAG_TCP_UDP_CSUM) == 0 ||
3237 #ifdef notyet
3238 (sc->bge_quirks & BGE_QUIRK_SHORT_CKSUM_BUG) == 0 ||
3239 #endif
3240 m_head->m_pkthdr.len >= ETHER_MIN_NOPAD)
3241 goto check_dma_bug;
3242
3243 if (bge_cksum_pad(m_head) != 0)
3244 return ENOBUFS;
3245
3246 check_dma_bug:
3247 if (!(sc->bge_quirks & BGE_QUIRK_5700_SMALLDMA))
3248 goto doit;
3249 /*
3250 * bcm5700 Revision B silicon cannot handle DMA descriptors with
3251 * less than eight bytes. If we encounter a teeny mbuf
3252 * at the end of a chain, we can pad. Otherwise, copy.
3253 */
3254 if (bge_compact_dma_runt(m_head) != 0)
3255 return ENOBUFS;
3256
3257 doit:
3258 dma = SLIST_FIRST(&sc->txdma_list);
3259 if (dma == NULL)
3260 return ENOBUFS;
3261 dmamap = dma->dmamap;
3262
3263 /*
3264 * Start packing the mbufs in this chain into
3265 * the fragment pointers. Stop when we run out
3266 * of fragments or hit the end of the mbuf chain.
3267 */
3268 if (bus_dmamap_load_mbuf(sc->bge_dmatag, dmamap, m_head,
3269 BUS_DMA_NOWAIT))
3270 return(ENOBUFS);
3271
3272 mtag = sc->ethercom.ec_nvlans ?
3273 m_tag_find(m_head, PACKET_TAG_VLAN, NULL) : NULL;
3274
3275 for (i = 0; i < dmamap->dm_nsegs; i++) {
3276 f = &sc->bge_rdata->bge_tx_ring[frag];
3277 if (sc->bge_cdata.bge_tx_chain[frag] != NULL)
3278 break;
3279 bge_set_hostaddr(&f->bge_addr, dmamap->dm_segs[i].ds_addr);
3280 f->bge_len = dmamap->dm_segs[i].ds_len;
3281 f->bge_flags = csum_flags;
3282
3283 if (mtag != NULL) {
3284 f->bge_flags |= BGE_TXBDFLAG_VLAN_TAG;
3285 f->bge_vlan_tag = *(u_int *)(mtag + 1);
3286 } else {
3287 f->bge_vlan_tag = 0;
3288 }
3289 /*
3290 * Sanity check: avoid coming within 16 descriptors
3291 * of the end of the ring.
3292 */
3293 if ((BGE_TX_RING_CNT - (sc->bge_txcnt + cnt)) < 16)
3294 return(ENOBUFS);
3295 cur = frag;
3296 BGE_INC(frag, BGE_TX_RING_CNT);
3297 cnt++;
3298 }
3299
3300 if (i < dmamap->dm_nsegs)
3301 return ENOBUFS;
3302
3303 bus_dmamap_sync(sc->bge_dmatag, dmamap, 0, dmamap->dm_mapsize,
3304 BUS_DMASYNC_PREWRITE);
3305
3306 if (frag == sc->bge_tx_saved_considx)
3307 return(ENOBUFS);
3308
3309 sc->bge_rdata->bge_tx_ring[cur].bge_flags |= BGE_TXBDFLAG_END;
3310 sc->bge_cdata.bge_tx_chain[cur] = m_head;
3311 SLIST_REMOVE_HEAD(&sc->txdma_list, link);
3312 sc->txdma[cur] = dma;
3313 sc->bge_txcnt += cnt;
3314
3315 *txidx = frag;
3316
3317 return(0);
3318 }
3319
3320 /*
3321 * Main transmit routine. To avoid having to do mbuf copies, we put pointers
3322 * to the mbuf data regions directly in the transmit descriptors.
3323 */
3324 void
3325 bge_start(ifp)
3326 struct ifnet *ifp;
3327 {
3328 struct bge_softc *sc;
3329 struct mbuf *m_head = NULL;
3330 u_int32_t prodidx = 0;
3331 int pkts = 0;
3332
3333 sc = ifp->if_softc;
3334
3335 if (!sc->bge_link && ifp->if_snd.ifq_len < 10)
3336 return;
3337
3338 prodidx = CSR_READ_4(sc, BGE_MBX_TX_HOST_PROD0_LO);
3339
3340 while(sc->bge_cdata.bge_tx_chain[prodidx] == NULL) {
3341 IFQ_POLL(&ifp->if_snd, m_head);
3342 if (m_head == NULL)
3343 break;
3344
3345 #if 0
3346 /*
3347 * XXX
3348 * safety overkill. If this is a fragmented packet chain
3349 * with delayed TCP/UDP checksums, then only encapsulate
3350 * it if we have enough descriptors to handle the entire
3351 * chain at once.
3352 * (paranoia -- may not actually be needed)
3353 */
3354 if (m_head->m_flags & M_FIRSTFRAG &&
3355 m_head->m_pkthdr.csum_flags & (CSUM_DELAY_DATA)) {
3356 if ((BGE_TX_RING_CNT - sc->bge_txcnt) <
3357 m_head->m_pkthdr.csum_data + 16) {
3358 ifp->if_flags |= IFF_OACTIVE;
3359 break;
3360 }
3361 }
3362 #endif
3363
3364 /*
3365 * Pack the data into the transmit ring. If we
3366 * don't have room, set the OACTIVE flag and wait
3367 * for the NIC to drain the ring.
3368 */
3369 if (bge_encap(sc, m_head, &prodidx)) {
3370 ifp->if_flags |= IFF_OACTIVE;
3371 break;
3372 }
3373
3374 /* now we are committed to transmit the packet */
3375 IFQ_DEQUEUE(&ifp->if_snd, m_head);
3376 pkts++;
3377
3378 #if NBPFILTER > 0
3379 /*
3380 * If there's a BPF listener, bounce a copy of this frame
3381 * to him.
3382 */
3383 if (ifp->if_bpf)
3384 bpf_mtap(ifp->if_bpf, m_head);
3385 #endif
3386 }
3387 if (pkts == 0)
3388 return;
3389
3390 /* Transmit */
3391 CSR_WRITE_4(sc, BGE_MBX_TX_HOST_PROD0_LO, prodidx);
3392 if (sc->bge_quirks & BGE_QUIRK_PRODUCER_BUG) /* 5700 b2 errata */
3393 CSR_WRITE_4(sc, BGE_MBX_TX_HOST_PROD0_LO, prodidx);
3394
3395 /*
3396 * Set a timeout in case the chip goes out to lunch.
3397 */
3398 ifp->if_timer = 5;
3399 }
3400
3401 int
3402 bge_init(ifp)
3403 struct ifnet *ifp;
3404 {
3405 struct bge_softc *sc = ifp->if_softc;
3406 u_int16_t *m;
3407 int s, error;
3408
3409 s = splnet();
3410
3411 ifp = &sc->ethercom.ec_if;
3412
3413 /* Cancel pending I/O and flush buffers. */
3414 bge_stop(sc);
3415 bge_reset(sc);
3416 bge_chipinit(sc);
3417
3418 /*
3419 * Init the various state machines, ring
3420 * control blocks and firmware.
3421 */
3422 error = bge_blockinit(sc);
3423 if (error != 0) {
3424 printf("%s: initialization error %d\n", sc->bge_dev.dv_xname,
3425 error);
3426 splx(s);
3427 return error;
3428 }
3429
3430 ifp = &sc->ethercom.ec_if;
3431
3432 /* Specify MTU. */
3433 CSR_WRITE_4(sc, BGE_RX_MTU, ifp->if_mtu +
3434 ETHER_HDR_LEN + ETHER_CRC_LEN);
3435
3436 /* Load our MAC address. */
3437 m = (u_int16_t *)&(LLADDR(ifp->if_sadl)[0]);
3438 CSR_WRITE_4(sc, BGE_MAC_ADDR1_LO, htons(m[0]));
3439 CSR_WRITE_4(sc, BGE_MAC_ADDR1_HI, (htons(m[1]) << 16) | htons(m[2]));
3440
3441 /* Enable or disable promiscuous mode as needed. */
3442 if (ifp->if_flags & IFF_PROMISC) {
3443 BGE_SETBIT(sc, BGE_RX_MODE, BGE_RXMODE_RX_PROMISC);
3444 } else {
3445 BGE_CLRBIT(sc, BGE_RX_MODE, BGE_RXMODE_RX_PROMISC);
3446 }
3447
3448 /* Program multicast filter. */
3449 bge_setmulti(sc);
3450
3451 /* Init RX ring. */
3452 bge_init_rx_ring_std(sc);
3453
3454 /* Init jumbo RX ring. */
3455 if (ifp->if_mtu > (ETHERMTU + ETHER_HDR_LEN + ETHER_CRC_LEN))
3456 bge_init_rx_ring_jumbo(sc);
3457
3458 /* Init our RX return ring index */
3459 sc->bge_rx_saved_considx = 0;
3460
3461 /* Init TX ring. */
3462 bge_init_tx_ring(sc);
3463
3464 /* Turn on transmitter */
3465 BGE_SETBIT(sc, BGE_TX_MODE, BGE_TXMODE_ENABLE);
3466
3467 /* Turn on receiver */
3468 BGE_SETBIT(sc, BGE_RX_MODE, BGE_RXMODE_ENABLE);
3469
3470 CSR_WRITE_4(sc, BGE_MAX_RX_FRAME_LOWAT, 2);
3471
3472 /* Tell firmware we're alive. */
3473 BGE_SETBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
3474
3475 /* Enable host interrupts. */
3476 BGE_SETBIT(sc, BGE_PCI_MISC_CTL, BGE_PCIMISCCTL_CLEAR_INTA);
3477 BGE_CLRBIT(sc, BGE_PCI_MISC_CTL, BGE_PCIMISCCTL_MASK_PCI_INTR);
3478 CSR_WRITE_4(sc, BGE_MBX_IRQ0_LO, 0);
3479
3480 bge_ifmedia_upd(ifp);
3481
3482 ifp->if_flags |= IFF_RUNNING;
3483 ifp->if_flags &= ~IFF_OACTIVE;
3484
3485 splx(s);
3486
3487 callout_reset(&sc->bge_timeout, hz, bge_tick, sc);
3488
3489 return 0;
3490 }
3491
3492 /*
3493 * Set media options.
3494 */
3495 int
3496 bge_ifmedia_upd(ifp)
3497 struct ifnet *ifp;
3498 {
3499 struct bge_softc *sc = ifp->if_softc;
3500 struct mii_data *mii = &sc->bge_mii;
3501 struct ifmedia *ifm = &sc->bge_ifmedia;
3502
3503 /* If this is a 1000baseX NIC, enable the TBI port. */
3504 if (sc->bge_tbi) {
3505 if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
3506 return(EINVAL);
3507 switch(IFM_SUBTYPE(ifm->ifm_media)) {
3508 case IFM_AUTO:
3509 break;
3510 case IFM_1000_SX:
3511 if ((ifm->ifm_media & IFM_GMASK) == IFM_FDX) {
3512 BGE_CLRBIT(sc, BGE_MAC_MODE,
3513 BGE_MACMODE_HALF_DUPLEX);
3514 } else {
3515 BGE_SETBIT(sc, BGE_MAC_MODE,
3516 BGE_MACMODE_HALF_DUPLEX);
3517 }
3518 break;
3519 default:
3520 return(EINVAL);
3521 }
3522 /* XXX 802.3x flow control for 1000BASE-SX */
3523 return(0);
3524 }
3525
3526 sc->bge_link = 0;
3527 mii_mediachg(mii);
3528
3529 return(0);
3530 }
3531
3532 /*
3533 * Report current media status.
3534 */
3535 void
3536 bge_ifmedia_sts(ifp, ifmr)
3537 struct ifnet *ifp;
3538 struct ifmediareq *ifmr;
3539 {
3540 struct bge_softc *sc = ifp->if_softc;
3541 struct mii_data *mii = &sc->bge_mii;
3542
3543 if (sc->bge_tbi) {
3544 ifmr->ifm_status = IFM_AVALID;
3545 ifmr->ifm_active = IFM_ETHER;
3546 if (CSR_READ_4(sc, BGE_MAC_STS) &
3547 BGE_MACSTAT_TBI_PCS_SYNCHED)
3548 ifmr->ifm_status |= IFM_ACTIVE;
3549 ifmr->ifm_active |= IFM_1000_SX;
3550 if (CSR_READ_4(sc, BGE_MAC_MODE) & BGE_MACMODE_HALF_DUPLEX)
3551 ifmr->ifm_active |= IFM_HDX;
3552 else
3553 ifmr->ifm_active |= IFM_FDX;
3554 return;
3555 }
3556
3557 mii_pollstat(mii);
3558 ifmr->ifm_status = mii->mii_media_status;
3559 ifmr->ifm_active = (mii->mii_media_active & ~IFM_ETH_FMASK) |
3560 sc->bge_flowflags;
3561 }
3562
3563 int
3564 bge_ioctl(ifp, command, data)
3565 struct ifnet *ifp;
3566 u_long command;
3567 caddr_t data;
3568 {
3569 struct bge_softc *sc = ifp->if_softc;
3570 struct ifreq *ifr = (struct ifreq *) data;
3571 int s, error = 0;
3572 struct mii_data *mii;
3573
3574 s = splnet();
3575
3576 switch(command) {
3577 case SIOCSIFFLAGS:
3578 if (ifp->if_flags & IFF_UP) {
3579 /*
3580 * If only the state of the PROMISC flag changed,
3581 * then just use the 'set promisc mode' command
3582 * instead of reinitializing the entire NIC. Doing
3583 * a full re-init means reloading the firmware and
3584 * waiting for it to start up, which may take a
3585 * second or two.
3586 */
3587 if (ifp->if_flags & IFF_RUNNING &&
3588 ifp->if_flags & IFF_PROMISC &&
3589 !(sc->bge_if_flags & IFF_PROMISC)) {
3590 BGE_SETBIT(sc, BGE_RX_MODE,
3591 BGE_RXMODE_RX_PROMISC);
3592 } else if (ifp->if_flags & IFF_RUNNING &&
3593 !(ifp->if_flags & IFF_PROMISC) &&
3594 sc->bge_if_flags & IFF_PROMISC) {
3595 BGE_CLRBIT(sc, BGE_RX_MODE,
3596 BGE_RXMODE_RX_PROMISC);
3597 } else
3598 bge_init(ifp);
3599 } else {
3600 if (ifp->if_flags & IFF_RUNNING) {
3601 bge_stop(sc);
3602 }
3603 }
3604 sc->bge_if_flags = ifp->if_flags;
3605 error = 0;
3606 break;
3607 case SIOCSIFMEDIA:
3608 /* XXX Flow control is not supported for 1000BASE-SX */
3609 if (sc->bge_tbi) {
3610 ifr->ifr_media &= ~IFM_ETH_FMASK;
3611 sc->bge_flowflags = 0;
3612 }
3613
3614 /* Flow control requires full-duplex mode. */
3615 if (IFM_SUBTYPE(ifr->ifr_media) == IFM_AUTO ||
3616 (ifr->ifr_media & IFM_FDX) == 0) {
3617 ifr->ifr_media &= ~IFM_ETH_FMASK;
3618 }
3619 if (IFM_SUBTYPE(ifr->ifr_media) != IFM_AUTO) {
3620 if ((ifr->ifr_media & IFM_ETH_FMASK) == IFM_FLOW) {
3621 /* We an do both TXPAUSE and RXPAUSE. */
3622 ifr->ifr_media |=
3623 IFM_ETH_TXPAUSE | IFM_ETH_RXPAUSE;
3624 }
3625 sc->bge_flowflags = ifr->ifr_media & IFM_ETH_FMASK;
3626 }
3627 /* FALLTHROUGH */
3628 case SIOCGIFMEDIA:
3629 if (sc->bge_tbi) {
3630 error = ifmedia_ioctl(ifp, ifr, &sc->bge_ifmedia,
3631 command);
3632 } else {
3633 mii = &sc->bge_mii;
3634 error = ifmedia_ioctl(ifp, ifr, &mii->mii_media,
3635 command);
3636 }
3637 break;
3638 default:
3639 error = ether_ioctl(ifp, command, data);
3640 if (error == ENETRESET) {
3641 bge_setmulti(sc);
3642 error = 0;
3643 }
3644 break;
3645 }
3646
3647 splx(s);
3648
3649 return(error);
3650 }
3651
3652 void
3653 bge_watchdog(ifp)
3654 struct ifnet *ifp;
3655 {
3656 struct bge_softc *sc;
3657
3658 sc = ifp->if_softc;
3659
3660 printf("%s: watchdog timeout -- resetting\n", sc->bge_dev.dv_xname);
3661
3662 ifp->if_flags &= ~IFF_RUNNING;
3663 bge_init(ifp);
3664
3665 ifp->if_oerrors++;
3666 }
3667
3668 static void
3669 bge_stop_block(struct bge_softc *sc, bus_addr_t reg, uint32_t bit)
3670 {
3671 int i;
3672
3673 BGE_CLRBIT(sc, reg, bit);
3674
3675 for (i = 0; i < BGE_TIMEOUT; i++) {
3676 if ((CSR_READ_4(sc, reg) & bit) == 0)
3677 return;
3678 delay(100);
3679 }
3680
3681 printf("%s: block failed to stop: reg 0x%lx, bit 0x%08x\n",
3682 sc->bge_dev.dv_xname, (u_long) reg, bit);
3683 }
3684
3685 /*
3686 * Stop the adapter and free any mbufs allocated to the
3687 * RX and TX lists.
3688 */
3689 void
3690 bge_stop(sc)
3691 struct bge_softc *sc;
3692 {
3693 struct ifnet *ifp = &sc->ethercom.ec_if;
3694
3695 callout_stop(&sc->bge_timeout);
3696
3697 /*
3698 * Disable all of the receiver blocks
3699 */
3700 bge_stop_block(sc, BGE_RX_MODE, BGE_RXMODE_ENABLE);
3701 bge_stop_block(sc, BGE_RBDI_MODE, BGE_RBDIMODE_ENABLE);
3702 bge_stop_block(sc, BGE_RXLP_MODE, BGE_RXLPMODE_ENABLE);
3703 if ((sc->bge_quirks & BGE_QUIRK_5705_CORE) == 0) {
3704 bge_stop_block(sc, BGE_RXLS_MODE, BGE_RXLSMODE_ENABLE);
3705 }
3706 bge_stop_block(sc, BGE_RDBDI_MODE, BGE_RBDIMODE_ENABLE);
3707 bge_stop_block(sc, BGE_RDC_MODE, BGE_RDCMODE_ENABLE);
3708 bge_stop_block(sc, BGE_RBDC_MODE, BGE_RBDCMODE_ENABLE);
3709
3710 /*
3711 * Disable all of the transmit blocks
3712 */
3713 bge_stop_block(sc, BGE_SRS_MODE, BGE_SRSMODE_ENABLE);
3714 bge_stop_block(sc, BGE_SBDI_MODE, BGE_SBDIMODE_ENABLE);
3715 bge_stop_block(sc, BGE_SDI_MODE, BGE_SDIMODE_ENABLE);
3716 bge_stop_block(sc, BGE_RDMA_MODE, BGE_RDMAMODE_ENABLE);
3717 bge_stop_block(sc, BGE_SDC_MODE, BGE_SDCMODE_ENABLE);
3718 if ((sc->bge_quirks & BGE_QUIRK_5705_CORE) == 0) {
3719 bge_stop_block(sc, BGE_DMAC_MODE, BGE_DMACMODE_ENABLE);
3720 }
3721 bge_stop_block(sc, BGE_SBDC_MODE, BGE_SBDCMODE_ENABLE);
3722
3723 /*
3724 * Shut down all of the memory managers and related
3725 * state machines.
3726 */
3727 bge_stop_block(sc, BGE_HCC_MODE, BGE_HCCMODE_ENABLE);
3728 bge_stop_block(sc, BGE_WDMA_MODE, BGE_WDMAMODE_ENABLE);
3729 if ((sc->bge_quirks & BGE_QUIRK_5705_CORE) == 0) {
3730 bge_stop_block(sc, BGE_MBCF_MODE, BGE_MBCFMODE_ENABLE);
3731 }
3732
3733 CSR_WRITE_4(sc, BGE_FTQ_RESET, 0xFFFFFFFF);
3734 CSR_WRITE_4(sc, BGE_FTQ_RESET, 0);
3735
3736 if ((sc->bge_quirks & BGE_QUIRK_5705_CORE) == 0) {
3737 bge_stop_block(sc, BGE_BMAN_MODE, BGE_BMANMODE_ENABLE);
3738 bge_stop_block(sc, BGE_MARB_MODE, BGE_MARBMODE_ENABLE);
3739 }
3740
3741 /* Disable host interrupts. */
3742 BGE_SETBIT(sc, BGE_PCI_MISC_CTL, BGE_PCIMISCCTL_MASK_PCI_INTR);
3743 CSR_WRITE_4(sc, BGE_MBX_IRQ0_LO, 1);
3744
3745 /*
3746 * Tell firmware we're shutting down.
3747 */
3748 BGE_CLRBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
3749
3750 /* Free the RX lists. */
3751 bge_free_rx_ring_std(sc);
3752
3753 /* Free jumbo RX list. */
3754 bge_free_rx_ring_jumbo(sc);
3755
3756 /* Free TX buffers. */
3757 bge_free_tx_ring(sc);
3758
3759 /*
3760 * Isolate/power down the PHY.
3761 */
3762 if (!sc->bge_tbi)
3763 mii_down(&sc->bge_mii);
3764
3765 sc->bge_link = 0;
3766
3767 sc->bge_tx_saved_considx = BGE_TXCONS_UNSET;
3768
3769 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
3770 }
3771
3772 /*
3773 * Stop all chip I/O so that the kernel's probe routines don't
3774 * get confused by errant DMAs when rebooting.
3775 */
3776 void
3777 bge_shutdown(xsc)
3778 void *xsc;
3779 {
3780 struct bge_softc *sc = (struct bge_softc *)xsc;
3781
3782 bge_stop(sc);
3783 bge_reset(sc);
3784 }
3785
3786
3787 static int
3788 sysctl_bge_verify(SYSCTLFN_ARGS)
3789 {
3790 int error, t;
3791 struct sysctlnode node;
3792
3793 node = *rnode;
3794 t = *(int*)rnode->sysctl_data;
3795 node.sysctl_data = &t;
3796 error = sysctl_lookup(SYSCTLFN_CALL(&node));
3797 if (error || newp == NULL)
3798 return (error);
3799
3800 #if 0
3801 DPRINTF2(("%s: t = %d, nodenum = %d, rnodenum = %d\n", __func__, t,
3802 node.sysctl_num, rnode->sysctl_num));
3803 #endif
3804
3805 if (node.sysctl_num == bge_rxthresh_nodenum) {
3806 if (t < 0 || t >= NBGE_RX_THRESH)
3807 return (EINVAL);
3808 bge_update_all_threshes(t);
3809 } else
3810 return (EINVAL);
3811
3812 *(int*)rnode->sysctl_data = t;
3813
3814 return (0);
3815 }
3816
3817 /*
3818 * Set up sysctl(3) MIB, hw.bge.*.
3819 *
3820 * TBD condition SYSCTL_PERMANENT on being an LKM or not
3821 */
3822 SYSCTL_SETUP(sysctl_bge, "sysctl bge subtree setup")
3823 {
3824 int rc, bge_root_num;
3825 struct sysctlnode *node;
3826
3827 if ((rc = sysctl_createv(clog, 0, NULL, NULL,
3828 CTLFLAG_PERMANENT, CTLTYPE_NODE, "hw", NULL,
3829 NULL, 0, NULL, 0, CTL_HW, CTL_EOL)) != 0) {
3830 goto err;
3831 }
3832
3833 if ((rc = sysctl_createv(clog, 0, NULL, &node,
3834 CTLFLAG_PERMANENT, CTLTYPE_NODE, "bge", NULL,
3835 NULL, 0, NULL, 0, CTL_HW, CTL_CREATE, CTL_EOL)) != 0) {
3836 goto err;
3837 }
3838
3839 bge_root_num = node->sysctl_num;
3840
3841 /* BGE Rx interrupt mitigation level */
3842 if ((rc = sysctl_createv(clog, 0, NULL, &node,
3843 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
3844 CTLTYPE_INT, "rx_lvl", NULL, sysctl_bge_verify, 0,
3845 &bge_rx_thresh_lvl,
3846 0, CTL_HW, bge_root_num, CTL_CREATE,
3847 CTL_EOL)) != 0) {
3848 goto err;
3849 }
3850
3851 bge_rxthresh_nodenum = node->sysctl_num;
3852
3853 return;
3854
3855 err:
3856 printf("%s: sysctl_createv failed (rc = %d)\n", __func__, rc);
3857 }
3858