2 * Copyright (c) 2001 Wind River Systems
3 * Copyright (c) 1997, 1998, 1999, 2001
4 * Bill Paul <wpaul@windriver.com>. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by Bill Paul.
17 * 4. Neither the name of the author nor the names of any co-contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
31 * THE POSSIBILITY OF SUCH DAMAGE.
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
38 * Broadcom BCM57xx(x)/BCM590x NetXtreme and NetLink family Ethernet driver
40 * The Broadcom BCM5700 is based on technology originally developed by
41 * Alteon Networks as part of the Tigon I and Tigon II Gigabit Ethernet
42 * MAC chips. The BCM5700, sometimes referred to as the Tigon III, has
43 * two on-board MIPS R4000 CPUs and can have as much as 16MB of external
44 * SSRAM. The BCM5700 supports TCP, UDP and IP checksum offload, jumbo
45 * frames, highly configurable RX filtering, and 16 RX and TX queues
46 * (which, along with RX filter rules, can be used for QOS applications).
47 * Other features, such as TCP segmentation, may be available as part
48 * of value-added firmware updates. Unlike the Tigon I and Tigon II,
49 * firmware images can be stored in hardware and need not be compiled
52 * The BCM5700 supports the PCI v2.2 and PCI-X v1.0 standards, and will
53 * function in a 32-bit/64-bit 33/66Mhz bus, or a 64-bit/133Mhz bus.
55 * The BCM5701 is a single-chip solution incorporating both the BCM5700
56 * MAC and a BCM5401 10/100/1000 PHY. Unlike the BCM5700, the BCM5701
57 * does not support external SSRAM.
59 * Broadcom also produces a variation of the BCM5700 under the "Altima"
60 * brand name, which is functionally similar but lacks PCI-X support.
62 * Without external SSRAM, you can only have at most 4 TX rings,
63 * and the use of the mini RX ring is disabled. This seems to imply
64 * that these features are simply not available on the BCM5701. As a
65 * result, this driver does not implement any support for the mini RX
69 #ifdef HAVE_KERNEL_OPTION_HEADERS
70 #include "opt_device_polling.h"
73 #include <sys/param.h>
74 #include <sys/endian.h>
75 #include <sys/systm.h>
76 #include <sys/sockio.h>
78 #include <sys/malloc.h>
79 #include <sys/kernel.h>
80 #include <sys/module.h>
81 #include <sys/socket.h>
82 #include <sys/sysctl.h>
83 #include <sys/taskqueue.h>
86 #include <net/if_arp.h>
87 #include <net/ethernet.h>
88 #include <net/if_dl.h>
89 #include <net/if_media.h>
93 #include <net/if_types.h>
94 #include <net/if_vlan_var.h>
96 #include <netinet/in_systm.h>
97 #include <netinet/in.h>
98 #include <netinet/ip.h>
99 #include <netinet/tcp.h>
101 #include <machine/bus.h>
102 #include <machine/resource.h>
104 #include <sys/rman.h>
106 #include <dev/mii/mii.h>
107 #include <dev/mii/miivar.h>
109 #include <dev/mii/brgphyreg.h>
112 #include <dev/ofw/ofw_bus.h>
113 #include <dev/ofw/openfirm.h>
114 #include <machine/ofw_machdep.h>
115 #include <machine/ver.h>
118 #include <dev/pci/pcireg.h>
119 #include <dev/pci/pcivar.h>
121 #include <dev/bge/if_bgereg.h>
123 #define BGE_CSUM_FEATURES (CSUM_IP | CSUM_TCP)
124 #define ETHER_MIN_NOPAD (ETHER_MIN_LEN - ETHER_CRC_LEN) /* i.e., 60 */
126 MODULE_DEPEND(bge, pci, 1, 1, 1);
127 MODULE_DEPEND(bge, ether, 1, 1, 1);
128 MODULE_DEPEND(bge, miibus, 1, 1, 1);
130 /* "device miibus" required. See GENERIC if you get errors here. */
131 #include "miibus_if.h"
134 * Various supported device vendors/types and their names. Note: the
135 * spec seems to indicate that the hardware still has Alteon's vendor
136 * ID burned into it, though it will always be overriden by the vendor
137 * ID in the EEPROM. Just to be safe, we cover all possibilities.
139 static const struct bge_type {
143 { ALTEON_VENDORID, ALTEON_DEVICEID_BCM5700 },
144 { ALTEON_VENDORID, ALTEON_DEVICEID_BCM5701 },
146 { ALTIMA_VENDORID, ALTIMA_DEVICE_AC1000 },
147 { ALTIMA_VENDORID, ALTIMA_DEVICE_AC1002 },
148 { ALTIMA_VENDORID, ALTIMA_DEVICE_AC9100 },
150 { APPLE_VENDORID, APPLE_DEVICE_BCM5701 },
152 { BCOM_VENDORID, BCOM_DEVICEID_BCM5700 },
153 { BCOM_VENDORID, BCOM_DEVICEID_BCM5701 },
154 { BCOM_VENDORID, BCOM_DEVICEID_BCM5702 },
155 { BCOM_VENDORID, BCOM_DEVICEID_BCM5702_ALT },
156 { BCOM_VENDORID, BCOM_DEVICEID_BCM5702X },
157 { BCOM_VENDORID, BCOM_DEVICEID_BCM5703 },
158 { BCOM_VENDORID, BCOM_DEVICEID_BCM5703_ALT },
159 { BCOM_VENDORID, BCOM_DEVICEID_BCM5703X },
160 { BCOM_VENDORID, BCOM_DEVICEID_BCM5704C },
161 { BCOM_VENDORID, BCOM_DEVICEID_BCM5704S },
162 { BCOM_VENDORID, BCOM_DEVICEID_BCM5704S_ALT },
163 { BCOM_VENDORID, BCOM_DEVICEID_BCM5705 },
164 { BCOM_VENDORID, BCOM_DEVICEID_BCM5705F },
165 { BCOM_VENDORID, BCOM_DEVICEID_BCM5705K },
166 { BCOM_VENDORID, BCOM_DEVICEID_BCM5705M },
167 { BCOM_VENDORID, BCOM_DEVICEID_BCM5705M_ALT },
168 { BCOM_VENDORID, BCOM_DEVICEID_BCM5714C },
169 { BCOM_VENDORID, BCOM_DEVICEID_BCM5714S },
170 { BCOM_VENDORID, BCOM_DEVICEID_BCM5715 },
171 { BCOM_VENDORID, BCOM_DEVICEID_BCM5715S },
172 { BCOM_VENDORID, BCOM_DEVICEID_BCM5717 },
173 { BCOM_VENDORID, BCOM_DEVICEID_BCM5717C },
174 { BCOM_VENDORID, BCOM_DEVICEID_BCM5718 },
175 { BCOM_VENDORID, BCOM_DEVICEID_BCM5719 },
176 { BCOM_VENDORID, BCOM_DEVICEID_BCM5720 },
177 { BCOM_VENDORID, BCOM_DEVICEID_BCM5721 },
178 { BCOM_VENDORID, BCOM_DEVICEID_BCM5722 },
179 { BCOM_VENDORID, BCOM_DEVICEID_BCM5723 },
180 { BCOM_VENDORID, BCOM_DEVICEID_BCM5725 },
181 { BCOM_VENDORID, BCOM_DEVICEID_BCM5727 },
182 { BCOM_VENDORID, BCOM_DEVICEID_BCM5750 },
183 { BCOM_VENDORID, BCOM_DEVICEID_BCM5750M },
184 { BCOM_VENDORID, BCOM_DEVICEID_BCM5751 },
185 { BCOM_VENDORID, BCOM_DEVICEID_BCM5751F },
186 { BCOM_VENDORID, BCOM_DEVICEID_BCM5751M },
187 { BCOM_VENDORID, BCOM_DEVICEID_BCM5752 },
188 { BCOM_VENDORID, BCOM_DEVICEID_BCM5752M },
189 { BCOM_VENDORID, BCOM_DEVICEID_BCM5753 },
190 { BCOM_VENDORID, BCOM_DEVICEID_BCM5753F },
191 { BCOM_VENDORID, BCOM_DEVICEID_BCM5753M },
192 { BCOM_VENDORID, BCOM_DEVICEID_BCM5754 },
193 { BCOM_VENDORID, BCOM_DEVICEID_BCM5754M },
194 { BCOM_VENDORID, BCOM_DEVICEID_BCM5755 },
195 { BCOM_VENDORID, BCOM_DEVICEID_BCM5755M },
196 { BCOM_VENDORID, BCOM_DEVICEID_BCM5756 },
197 { BCOM_VENDORID, BCOM_DEVICEID_BCM5761 },
198 { BCOM_VENDORID, BCOM_DEVICEID_BCM5761E },
199 { BCOM_VENDORID, BCOM_DEVICEID_BCM5761S },
200 { BCOM_VENDORID, BCOM_DEVICEID_BCM5761SE },
201 { BCOM_VENDORID, BCOM_DEVICEID_BCM5762 },
202 { BCOM_VENDORID, BCOM_DEVICEID_BCM5764 },
203 { BCOM_VENDORID, BCOM_DEVICEID_BCM5780 },
204 { BCOM_VENDORID, BCOM_DEVICEID_BCM5780S },
205 { BCOM_VENDORID, BCOM_DEVICEID_BCM5781 },
206 { BCOM_VENDORID, BCOM_DEVICEID_BCM5782 },
207 { BCOM_VENDORID, BCOM_DEVICEID_BCM5784 },
208 { BCOM_VENDORID, BCOM_DEVICEID_BCM5785F },
209 { BCOM_VENDORID, BCOM_DEVICEID_BCM5785G },
210 { BCOM_VENDORID, BCOM_DEVICEID_BCM5786 },
211 { BCOM_VENDORID, BCOM_DEVICEID_BCM5787 },
212 { BCOM_VENDORID, BCOM_DEVICEID_BCM5787F },
213 { BCOM_VENDORID, BCOM_DEVICEID_BCM5787M },
214 { BCOM_VENDORID, BCOM_DEVICEID_BCM5788 },
215 { BCOM_VENDORID, BCOM_DEVICEID_BCM5789 },
216 { BCOM_VENDORID, BCOM_DEVICEID_BCM5901 },
217 { BCOM_VENDORID, BCOM_DEVICEID_BCM5901A2 },
218 { BCOM_VENDORID, BCOM_DEVICEID_BCM5903M },
219 { BCOM_VENDORID, BCOM_DEVICEID_BCM5906 },
220 { BCOM_VENDORID, BCOM_DEVICEID_BCM5906M },
221 { BCOM_VENDORID, BCOM_DEVICEID_BCM57760 },
222 { BCOM_VENDORID, BCOM_DEVICEID_BCM57761 },
223 { BCOM_VENDORID, BCOM_DEVICEID_BCM57762 },
224 { BCOM_VENDORID, BCOM_DEVICEID_BCM57764 },
225 { BCOM_VENDORID, BCOM_DEVICEID_BCM57765 },
226 { BCOM_VENDORID, BCOM_DEVICEID_BCM57766 },
227 { BCOM_VENDORID, BCOM_DEVICEID_BCM57767 },
228 { BCOM_VENDORID, BCOM_DEVICEID_BCM57780 },
229 { BCOM_VENDORID, BCOM_DEVICEID_BCM57781 },
230 { BCOM_VENDORID, BCOM_DEVICEID_BCM57782 },
231 { BCOM_VENDORID, BCOM_DEVICEID_BCM57785 },
232 { BCOM_VENDORID, BCOM_DEVICEID_BCM57786 },
233 { BCOM_VENDORID, BCOM_DEVICEID_BCM57787 },
234 { BCOM_VENDORID, BCOM_DEVICEID_BCM57788 },
235 { BCOM_VENDORID, BCOM_DEVICEID_BCM57790 },
236 { BCOM_VENDORID, BCOM_DEVICEID_BCM57791 },
237 { BCOM_VENDORID, BCOM_DEVICEID_BCM57795 },
239 { SK_VENDORID, SK_DEVICEID_ALTIMA },
241 { TC_VENDORID, TC_DEVICEID_3C996 },
243 { FJTSU_VENDORID, FJTSU_DEVICEID_PW008GE4 },
244 { FJTSU_VENDORID, FJTSU_DEVICEID_PW008GE5 },
245 { FJTSU_VENDORID, FJTSU_DEVICEID_PP250450 },
250 static const struct bge_vendor {
254 { ALTEON_VENDORID, "Alteon" },
255 { ALTIMA_VENDORID, "Altima" },
256 { APPLE_VENDORID, "Apple" },
257 { BCOM_VENDORID, "Broadcom" },
258 { SK_VENDORID, "SysKonnect" },
259 { TC_VENDORID, "3Com" },
260 { FJTSU_VENDORID, "Fujitsu" },
265 static const struct bge_revision {
268 } bge_revisions[] = {
269 { BGE_CHIPID_BCM5700_A0, "BCM5700 A0" },
270 { BGE_CHIPID_BCM5700_A1, "BCM5700 A1" },
271 { BGE_CHIPID_BCM5700_B0, "BCM5700 B0" },
272 { BGE_CHIPID_BCM5700_B1, "BCM5700 B1" },
273 { BGE_CHIPID_BCM5700_B2, "BCM5700 B2" },
274 { BGE_CHIPID_BCM5700_B3, "BCM5700 B3" },
275 { BGE_CHIPID_BCM5700_ALTIMA, "BCM5700 Altima" },
276 { BGE_CHIPID_BCM5700_C0, "BCM5700 C0" },
277 { BGE_CHIPID_BCM5701_A0, "BCM5701 A0" },
278 { BGE_CHIPID_BCM5701_B0, "BCM5701 B0" },
279 { BGE_CHIPID_BCM5701_B2, "BCM5701 B2" },
280 { BGE_CHIPID_BCM5701_B5, "BCM5701 B5" },
281 { BGE_CHIPID_BCM5703_A0, "BCM5703 A0" },
282 { BGE_CHIPID_BCM5703_A1, "BCM5703 A1" },
283 { BGE_CHIPID_BCM5703_A2, "BCM5703 A2" },
284 { BGE_CHIPID_BCM5703_A3, "BCM5703 A3" },
285 { BGE_CHIPID_BCM5703_B0, "BCM5703 B0" },
286 { BGE_CHIPID_BCM5704_A0, "BCM5704 A0" },
287 { BGE_CHIPID_BCM5704_A1, "BCM5704 A1" },
288 { BGE_CHIPID_BCM5704_A2, "BCM5704 A2" },
289 { BGE_CHIPID_BCM5704_A3, "BCM5704 A3" },
290 { BGE_CHIPID_BCM5704_B0, "BCM5704 B0" },
291 { BGE_CHIPID_BCM5705_A0, "BCM5705 A0" },
292 { BGE_CHIPID_BCM5705_A1, "BCM5705 A1" },
293 { BGE_CHIPID_BCM5705_A2, "BCM5705 A2" },
294 { BGE_CHIPID_BCM5705_A3, "BCM5705 A3" },
295 { BGE_CHIPID_BCM5750_A0, "BCM5750 A0" },
296 { BGE_CHIPID_BCM5750_A1, "BCM5750 A1" },
297 { BGE_CHIPID_BCM5750_A3, "BCM5750 A3" },
298 { BGE_CHIPID_BCM5750_B0, "BCM5750 B0" },
299 { BGE_CHIPID_BCM5750_B1, "BCM5750 B1" },
300 { BGE_CHIPID_BCM5750_C0, "BCM5750 C0" },
301 { BGE_CHIPID_BCM5750_C1, "BCM5750 C1" },
302 { BGE_CHIPID_BCM5750_C2, "BCM5750 C2" },
303 { BGE_CHIPID_BCM5714_A0, "BCM5714 A0" },
304 { BGE_CHIPID_BCM5752_A0, "BCM5752 A0" },
305 { BGE_CHIPID_BCM5752_A1, "BCM5752 A1" },
306 { BGE_CHIPID_BCM5752_A2, "BCM5752 A2" },
307 { BGE_CHIPID_BCM5714_B0, "BCM5714 B0" },
308 { BGE_CHIPID_BCM5714_B3, "BCM5714 B3" },
309 { BGE_CHIPID_BCM5715_A0, "BCM5715 A0" },
310 { BGE_CHIPID_BCM5715_A1, "BCM5715 A1" },
311 { BGE_CHIPID_BCM5715_A3, "BCM5715 A3" },
312 { BGE_CHIPID_BCM5717_A0, "BCM5717 A0" },
313 { BGE_CHIPID_BCM5717_B0, "BCM5717 B0" },
314 { BGE_CHIPID_BCM5717_C0, "BCM5717 C0" },
315 { BGE_CHIPID_BCM5719_A0, "BCM5719 A0" },
316 { BGE_CHIPID_BCM5720_A0, "BCM5720 A0" },
317 { BGE_CHIPID_BCM5755_A0, "BCM5755 A0" },
318 { BGE_CHIPID_BCM5755_A1, "BCM5755 A1" },
319 { BGE_CHIPID_BCM5755_A2, "BCM5755 A2" },
320 { BGE_CHIPID_BCM5722_A0, "BCM5722 A0" },
321 { BGE_CHIPID_BCM5761_A0, "BCM5761 A0" },
322 { BGE_CHIPID_BCM5761_A1, "BCM5761 A1" },
323 { BGE_CHIPID_BCM5762_A0, "BCM5762 A0" },
324 { BGE_CHIPID_BCM5784_A0, "BCM5784 A0" },
325 { BGE_CHIPID_BCM5784_A1, "BCM5784 A1" },
326 /* 5754 and 5787 share the same ASIC ID */
327 { BGE_CHIPID_BCM5787_A0, "BCM5754/5787 A0" },
328 { BGE_CHIPID_BCM5787_A1, "BCM5754/5787 A1" },
329 { BGE_CHIPID_BCM5787_A2, "BCM5754/5787 A2" },
330 { BGE_CHIPID_BCM5906_A1, "BCM5906 A1" },
331 { BGE_CHIPID_BCM5906_A2, "BCM5906 A2" },
332 { BGE_CHIPID_BCM57765_A0, "BCM57765 A0" },
333 { BGE_CHIPID_BCM57765_B0, "BCM57765 B0" },
334 { BGE_CHIPID_BCM57780_A0, "BCM57780 A0" },
335 { BGE_CHIPID_BCM57780_A1, "BCM57780 A1" },
341 * Some defaults for major revisions, so that newer steppings
342 * that we don't know about have a shot at working.
344 static const struct bge_revision bge_majorrevs[] = {
345 { BGE_ASICREV_BCM5700, "unknown BCM5700" },
346 { BGE_ASICREV_BCM5701, "unknown BCM5701" },
347 { BGE_ASICREV_BCM5703, "unknown BCM5703" },
348 { BGE_ASICREV_BCM5704, "unknown BCM5704" },
349 { BGE_ASICREV_BCM5705, "unknown BCM5705" },
350 { BGE_ASICREV_BCM5750, "unknown BCM5750" },
351 { BGE_ASICREV_BCM5714_A0, "unknown BCM5714" },
352 { BGE_ASICREV_BCM5752, "unknown BCM5752" },
353 { BGE_ASICREV_BCM5780, "unknown BCM5780" },
354 { BGE_ASICREV_BCM5714, "unknown BCM5714" },
355 { BGE_ASICREV_BCM5755, "unknown BCM5755" },
356 { BGE_ASICREV_BCM5761, "unknown BCM5761" },
357 { BGE_ASICREV_BCM5784, "unknown BCM5784" },
358 { BGE_ASICREV_BCM5785, "unknown BCM5785" },
359 /* 5754 and 5787 share the same ASIC ID */
360 { BGE_ASICREV_BCM5787, "unknown BCM5754/5787" },
361 { BGE_ASICREV_BCM5906, "unknown BCM5906" },
362 { BGE_ASICREV_BCM57765, "unknown BCM57765" },
363 { BGE_ASICREV_BCM57766, "unknown BCM57766" },
364 { BGE_ASICREV_BCM57780, "unknown BCM57780" },
365 { BGE_ASICREV_BCM5717, "unknown BCM5717" },
366 { BGE_ASICREV_BCM5719, "unknown BCM5719" },
367 { BGE_ASICREV_BCM5720, "unknown BCM5720" },
368 { BGE_ASICREV_BCM5762, "unknown BCM5762" },
373 #define BGE_IS_JUMBO_CAPABLE(sc) ((sc)->bge_flags & BGE_FLAG_JUMBO)
374 #define BGE_IS_5700_FAMILY(sc) ((sc)->bge_flags & BGE_FLAG_5700_FAMILY)
375 #define BGE_IS_5705_PLUS(sc) ((sc)->bge_flags & BGE_FLAG_5705_PLUS)
376 #define BGE_IS_5714_FAMILY(sc) ((sc)->bge_flags & BGE_FLAG_5714_FAMILY)
377 #define BGE_IS_575X_PLUS(sc) ((sc)->bge_flags & BGE_FLAG_575X_PLUS)
378 #define BGE_IS_5755_PLUS(sc) ((sc)->bge_flags & BGE_FLAG_5755_PLUS)
379 #define BGE_IS_5717_PLUS(sc) ((sc)->bge_flags & BGE_FLAG_5717_PLUS)
380 #define BGE_IS_57765_PLUS(sc) ((sc)->bge_flags & BGE_FLAG_57765_PLUS)
382 static uint32_t bge_chipid(device_t);
383 static const struct bge_vendor * bge_lookup_vendor(uint16_t);
384 static const struct bge_revision * bge_lookup_rev(uint32_t);
386 typedef int (*bge_eaddr_fcn_t)(struct bge_softc *, uint8_t[]);
388 static int bge_probe(device_t);
389 static int bge_attach(device_t);
390 static int bge_detach(device_t);
391 static int bge_suspend(device_t);
392 static int bge_resume(device_t);
393 static void bge_release_resources(struct bge_softc *);
394 static void bge_dma_map_addr(void *, bus_dma_segment_t *, int, int);
395 static int bge_dma_alloc(struct bge_softc *);
396 static void bge_dma_free(struct bge_softc *);
397 static int bge_dma_ring_alloc(struct bge_softc *, bus_size_t, bus_size_t,
398 bus_dma_tag_t *, uint8_t **, bus_dmamap_t *, bus_addr_t *, const char *);
400 static void bge_devinfo(struct bge_softc *);
401 static int bge_mbox_reorder(struct bge_softc *);
403 static int bge_get_eaddr_fw(struct bge_softc *sc, uint8_t ether_addr[]);
404 static int bge_get_eaddr_mem(struct bge_softc *, uint8_t[]);
405 static int bge_get_eaddr_nvram(struct bge_softc *, uint8_t[]);
406 static int bge_get_eaddr_eeprom(struct bge_softc *, uint8_t[]);
407 static int bge_get_eaddr(struct bge_softc *, uint8_t[]);
409 static void bge_txeof(struct bge_softc *, uint16_t);
410 static void bge_rxcsum(struct bge_softc *, struct bge_rx_bd *, struct mbuf *);
411 static int bge_rxeof(struct bge_softc *, uint16_t, int);
413 static void bge_asf_driver_up (struct bge_softc *);
414 static void bge_tick(void *);
415 static void bge_stats_clear_regs(struct bge_softc *);
416 static void bge_stats_update(struct bge_softc *);
417 static void bge_stats_update_regs(struct bge_softc *);
418 static struct mbuf *bge_check_short_dma(struct mbuf *);
419 static struct mbuf *bge_setup_tso(struct bge_softc *, struct mbuf *,
420 uint16_t *, uint16_t *);
421 static int bge_encap(struct bge_softc *, struct mbuf **, uint32_t *);
423 static void bge_intr(void *);
424 static int bge_msi_intr(void *);
425 static void bge_intr_task(void *, int);
426 static void bge_start_locked(struct ifnet *);
427 static void bge_start(struct ifnet *);
428 static int bge_ioctl(struct ifnet *, u_long, caddr_t);
429 static void bge_init_locked(struct bge_softc *);
430 static void bge_init(void *);
431 static void bge_stop_block(struct bge_softc *, bus_size_t, uint32_t);
432 static void bge_stop(struct bge_softc *);
433 static void bge_watchdog(struct bge_softc *);
434 static int bge_shutdown(device_t);
435 static int bge_ifmedia_upd_locked(struct ifnet *);
436 static int bge_ifmedia_upd(struct ifnet *);
437 static void bge_ifmedia_sts(struct ifnet *, struct ifmediareq *);
439 static uint8_t bge_nvram_getbyte(struct bge_softc *, int, uint8_t *);
440 static int bge_read_nvram(struct bge_softc *, caddr_t, int, int);
442 static uint8_t bge_eeprom_getbyte(struct bge_softc *, int, uint8_t *);
443 static int bge_read_eeprom(struct bge_softc *, caddr_t, int, int);
445 static void bge_setpromisc(struct bge_softc *);
446 static void bge_setmulti(struct bge_softc *);
447 static void bge_setvlan(struct bge_softc *);
449 static __inline void bge_rxreuse_std(struct bge_softc *, int);
450 static __inline void bge_rxreuse_jumbo(struct bge_softc *, int);
451 static int bge_newbuf_std(struct bge_softc *, int);
452 static int bge_newbuf_jumbo(struct bge_softc *, int);
453 static int bge_init_rx_ring_std(struct bge_softc *);
454 static void bge_free_rx_ring_std(struct bge_softc *);
455 static int bge_init_rx_ring_jumbo(struct bge_softc *);
456 static void bge_free_rx_ring_jumbo(struct bge_softc *);
457 static void bge_free_tx_ring(struct bge_softc *);
458 static int bge_init_tx_ring(struct bge_softc *);
460 static int bge_chipinit(struct bge_softc *);
461 static int bge_blockinit(struct bge_softc *);
462 static uint32_t bge_dma_swap_options(struct bge_softc *);
464 static int bge_has_eaddr(struct bge_softc *);
465 static uint32_t bge_readmem_ind(struct bge_softc *, int);
466 static void bge_writemem_ind(struct bge_softc *, int, int);
467 static void bge_writembx(struct bge_softc *, int, int);
469 static uint32_t bge_readreg_ind(struct bge_softc *, int);
471 static void bge_writemem_direct(struct bge_softc *, int, int);
472 static void bge_writereg_ind(struct bge_softc *, int, int);
474 static int bge_miibus_readreg(device_t, int, int);
475 static int bge_miibus_writereg(device_t, int, int, int);
476 static void bge_miibus_statchg(device_t);
477 #ifdef DEVICE_POLLING
478 static int bge_poll(struct ifnet *ifp, enum poll_cmd cmd, int count);
481 #define BGE_RESET_SHUTDOWN 0
482 #define BGE_RESET_START 1
483 #define BGE_RESET_SUSPEND 2
484 static void bge_sig_post_reset(struct bge_softc *, int);
485 static void bge_sig_legacy(struct bge_softc *, int);
486 static void bge_sig_pre_reset(struct bge_softc *, int);
487 static void bge_stop_fw(struct bge_softc *);
488 static int bge_reset(struct bge_softc *);
489 static void bge_link_upd(struct bge_softc *);
491 static void bge_ape_lock_init(struct bge_softc *);
492 static void bge_ape_read_fw_ver(struct bge_softc *);
493 static int bge_ape_lock(struct bge_softc *, int);
494 static void bge_ape_unlock(struct bge_softc *, int);
495 static void bge_ape_send_event(struct bge_softc *, uint32_t);
496 static void bge_ape_driver_state_change(struct bge_softc *, int);
499 * The BGE_REGISTER_DEBUG option is only for low-level debugging. It may
500 * leak information to untrusted users. It is also known to cause alignment
501 * traps on certain architectures.
503 #ifdef BGE_REGISTER_DEBUG
504 static int bge_sysctl_debug_info(SYSCTL_HANDLER_ARGS);
505 static int bge_sysctl_reg_read(SYSCTL_HANDLER_ARGS);
506 static int bge_sysctl_ape_read(SYSCTL_HANDLER_ARGS);
507 static int bge_sysctl_mem_read(SYSCTL_HANDLER_ARGS);
509 static void bge_add_sysctls(struct bge_softc *);
510 static void bge_add_sysctl_stats_regs(struct bge_softc *,
511 struct sysctl_ctx_list *, struct sysctl_oid_list *);
512 static void bge_add_sysctl_stats(struct bge_softc *, struct sysctl_ctx_list *,
513 struct sysctl_oid_list *);
514 static int bge_sysctl_stats(SYSCTL_HANDLER_ARGS);
516 static device_method_t bge_methods[] = {
517 /* Device interface */
518 DEVMETHOD(device_probe, bge_probe),
519 DEVMETHOD(device_attach, bge_attach),
520 DEVMETHOD(device_detach, bge_detach),
521 DEVMETHOD(device_shutdown, bge_shutdown),
522 DEVMETHOD(device_suspend, bge_suspend),
523 DEVMETHOD(device_resume, bge_resume),
526 DEVMETHOD(miibus_readreg, bge_miibus_readreg),
527 DEVMETHOD(miibus_writereg, bge_miibus_writereg),
528 DEVMETHOD(miibus_statchg, bge_miibus_statchg),
533 static driver_t bge_driver = {
536 sizeof(struct bge_softc)
539 static devclass_t bge_devclass;
541 DRIVER_MODULE(bge, pci, bge_driver, bge_devclass, 0, 0);
542 DRIVER_MODULE(miibus, bge, miibus_driver, miibus_devclass, 0, 0);
544 static int bge_allow_asf = 1;
546 TUNABLE_INT("hw.bge.allow_asf", &bge_allow_asf);
548 static SYSCTL_NODE(_hw, OID_AUTO, bge, CTLFLAG_RD, 0, "BGE driver parameters");
549 SYSCTL_INT(_hw_bge, OID_AUTO, allow_asf, CTLFLAG_RD, &bge_allow_asf, 0,
550 "Allow ASF mode if available");
552 #define SPARC64_BLADE_1500_MODEL "SUNW,Sun-Blade-1500"
553 #define SPARC64_BLADE_1500_PATH_BGE "/pci@1f,700000/network@2"
554 #define SPARC64_BLADE_2500_MODEL "SUNW,Sun-Blade-2500"
555 #define SPARC64_BLADE_2500_PATH_BGE "/pci@1c,600000/network@3"
556 #define SPARC64_OFW_SUBVENDOR "subsystem-vendor-id"
559 bge_has_eaddr(struct bge_softc *sc)
562 char buf[sizeof(SPARC64_BLADE_1500_PATH_BGE)];
569 * The on-board BGEs found in sun4u machines aren't fitted with
570 * an EEPROM which means that we have to obtain the MAC address
571 * via OFW and that some tests will always fail. We distinguish
572 * such BGEs by the subvendor ID, which also has to be obtained
573 * from OFW instead of the PCI configuration space as the latter
574 * indicates Broadcom as the subvendor of the netboot interface.
575 * For early Blade 1500 and 2500 we even have to check the OFW
576 * device path as the subvendor ID always defaults to Broadcom
579 if (OF_getprop(ofw_bus_get_node(dev), SPARC64_OFW_SUBVENDOR,
580 &subvendor, sizeof(subvendor)) == sizeof(subvendor) &&
581 (subvendor == FJTSU_VENDORID || subvendor == SUN_VENDORID))
583 memset(buf, 0, sizeof(buf));
584 if (OF_package_to_path(ofw_bus_get_node(dev), buf, sizeof(buf)) > 0) {
585 if (strcmp(sparc64_model, SPARC64_BLADE_1500_MODEL) == 0 &&
586 strcmp(buf, SPARC64_BLADE_1500_PATH_BGE) == 0)
588 if (strcmp(sparc64_model, SPARC64_BLADE_2500_MODEL) == 0 &&
589 strcmp(buf, SPARC64_BLADE_2500_PATH_BGE) == 0)
597 bge_readmem_ind(struct bge_softc *sc, int off)
602 if (sc->bge_asicrev == BGE_ASICREV_BCM5906 &&
603 off >= BGE_STATS_BLOCK && off < BGE_SEND_RING_1_TO_4)
608 pci_write_config(dev, BGE_PCI_MEMWIN_BASEADDR, off, 4);
609 val = pci_read_config(dev, BGE_PCI_MEMWIN_DATA, 4);
610 pci_write_config(dev, BGE_PCI_MEMWIN_BASEADDR, 0, 4);
615 bge_writemem_ind(struct bge_softc *sc, int off, int val)
619 if (sc->bge_asicrev == BGE_ASICREV_BCM5906 &&
620 off >= BGE_STATS_BLOCK && off < BGE_SEND_RING_1_TO_4)
625 pci_write_config(dev, BGE_PCI_MEMWIN_BASEADDR, off, 4);
626 pci_write_config(dev, BGE_PCI_MEMWIN_DATA, val, 4);
627 pci_write_config(dev, BGE_PCI_MEMWIN_BASEADDR, 0, 4);
632 bge_readreg_ind(struct bge_softc *sc, int off)
638 pci_write_config(dev, BGE_PCI_REG_BASEADDR, off, 4);
639 return (pci_read_config(dev, BGE_PCI_REG_DATA, 4));
644 bge_writereg_ind(struct bge_softc *sc, int off, int val)
650 pci_write_config(dev, BGE_PCI_REG_BASEADDR, off, 4);
651 pci_write_config(dev, BGE_PCI_REG_DATA, val, 4);
655 bge_writemem_direct(struct bge_softc *sc, int off, int val)
657 CSR_WRITE_4(sc, off, val);
661 bge_writembx(struct bge_softc *sc, int off, int val)
663 if (sc->bge_asicrev == BGE_ASICREV_BCM5906)
664 off += BGE_LPMBX_IRQ0_HI - BGE_MBX_IRQ0_HI;
666 CSR_WRITE_4(sc, off, val);
667 if ((sc->bge_flags & BGE_FLAG_MBOX_REORDER) != 0)
672 * Clear all stale locks and select the lock for this driver instance.
675 bge_ape_lock_init(struct bge_softc *sc)
677 uint32_t bit, regbase;
680 if (sc->bge_asicrev == BGE_ASICREV_BCM5761)
681 regbase = BGE_APE_LOCK_GRANT;
683 regbase = BGE_APE_PER_LOCK_GRANT;
685 /* Clear any stale locks. */
686 for (i = BGE_APE_LOCK_PHY0; i <= BGE_APE_LOCK_GPIO; i++) {
688 case BGE_APE_LOCK_PHY0:
689 case BGE_APE_LOCK_PHY1:
690 case BGE_APE_LOCK_PHY2:
691 case BGE_APE_LOCK_PHY3:
692 bit = BGE_APE_LOCK_GRANT_DRIVER0;
695 if (sc->bge_func_addr == 0)
696 bit = BGE_APE_LOCK_GRANT_DRIVER0;
698 bit = (1 << sc->bge_func_addr);
700 APE_WRITE_4(sc, regbase + 4 * i, bit);
703 /* Select the PHY lock based on the device's function number. */
704 switch (sc->bge_func_addr) {
706 sc->bge_phy_ape_lock = BGE_APE_LOCK_PHY0;
709 sc->bge_phy_ape_lock = BGE_APE_LOCK_PHY1;
712 sc->bge_phy_ape_lock = BGE_APE_LOCK_PHY2;
715 sc->bge_phy_ape_lock = BGE_APE_LOCK_PHY3;
718 device_printf(sc->bge_dev,
719 "PHY lock not supported on this function\n");
724 * Check for APE firmware, set flags, and print version info.
727 bge_ape_read_fw_ver(struct bge_softc *sc)
730 uint32_t apedata, features;
732 /* Check for a valid APE signature in shared memory. */
733 apedata = APE_READ_4(sc, BGE_APE_SEG_SIG);
734 if (apedata != BGE_APE_SEG_SIG_MAGIC) {
735 sc->bge_mfw_flags &= ~ BGE_MFW_ON_APE;
739 /* Check if APE firmware is running. */
740 apedata = APE_READ_4(sc, BGE_APE_FW_STATUS);
741 if ((apedata & BGE_APE_FW_STATUS_READY) == 0) {
742 device_printf(sc->bge_dev, "APE signature found "
743 "but FW status not ready! 0x%08x\n", apedata);
747 sc->bge_mfw_flags |= BGE_MFW_ON_APE;
749 /* Fetch the APE firwmare type and version. */
750 apedata = APE_READ_4(sc, BGE_APE_FW_VERSION);
751 features = APE_READ_4(sc, BGE_APE_FW_FEATURES);
752 if ((features & BGE_APE_FW_FEATURE_NCSI) != 0) {
753 sc->bge_mfw_flags |= BGE_MFW_TYPE_NCSI;
755 } else if ((features & BGE_APE_FW_FEATURE_DASH) != 0) {
756 sc->bge_mfw_flags |= BGE_MFW_TYPE_DASH;
761 /* Print the APE firmware version. */
762 device_printf(sc->bge_dev, "APE FW version: %s v%d.%d.%d.%d\n",
764 (apedata & BGE_APE_FW_VERSION_MAJMSK) >> BGE_APE_FW_VERSION_MAJSFT,
765 (apedata & BGE_APE_FW_VERSION_MINMSK) >> BGE_APE_FW_VERSION_MINSFT,
766 (apedata & BGE_APE_FW_VERSION_REVMSK) >> BGE_APE_FW_VERSION_REVSFT,
767 (apedata & BGE_APE_FW_VERSION_BLDMSK));
771 bge_ape_lock(struct bge_softc *sc, int locknum)
773 uint32_t bit, gnt, req, status;
776 if ((sc->bge_mfw_flags & BGE_MFW_ON_APE) == 0)
779 /* Lock request/grant registers have different bases. */
780 if (sc->bge_asicrev == BGE_ASICREV_BCM5761) {
781 req = BGE_APE_LOCK_REQ;
782 gnt = BGE_APE_LOCK_GRANT;
784 req = BGE_APE_PER_LOCK_REQ;
785 gnt = BGE_APE_PER_LOCK_GRANT;
791 case BGE_APE_LOCK_GPIO:
792 /* Lock required when using GPIO. */
793 if (sc->bge_asicrev == BGE_ASICREV_BCM5761)
795 if (sc->bge_func_addr == 0)
796 bit = BGE_APE_LOCK_REQ_DRIVER0;
798 bit = (1 << sc->bge_func_addr);
800 case BGE_APE_LOCK_GRC:
801 /* Lock required to reset the device. */
802 if (sc->bge_func_addr == 0)
803 bit = BGE_APE_LOCK_REQ_DRIVER0;
805 bit = (1 << sc->bge_func_addr);
807 case BGE_APE_LOCK_MEM:
808 /* Lock required when accessing certain APE memory. */
809 if (sc->bge_func_addr == 0)
810 bit = BGE_APE_LOCK_REQ_DRIVER0;
812 bit = (1 << sc->bge_func_addr);
814 case BGE_APE_LOCK_PHY0:
815 case BGE_APE_LOCK_PHY1:
816 case BGE_APE_LOCK_PHY2:
817 case BGE_APE_LOCK_PHY3:
818 /* Lock required when accessing PHYs. */
819 bit = BGE_APE_LOCK_REQ_DRIVER0;
825 /* Request a lock. */
826 APE_WRITE_4(sc, req + off, bit);
828 /* Wait up to 1 second to acquire lock. */
829 for (i = 0; i < 20000; i++) {
830 status = APE_READ_4(sc, gnt + off);
836 /* Handle any errors. */
838 device_printf(sc->bge_dev, "APE lock %d request failed! "
839 "request = 0x%04x[0x%04x], status = 0x%04x[0x%04x]\n",
840 locknum, req + off, bit & 0xFFFF, gnt + off,
842 /* Revoke the lock request. */
843 APE_WRITE_4(sc, gnt + off, bit);
851 bge_ape_unlock(struct bge_softc *sc, int locknum)
856 if ((sc->bge_mfw_flags & BGE_MFW_ON_APE) == 0)
859 if (sc->bge_asicrev == BGE_ASICREV_BCM5761)
860 gnt = BGE_APE_LOCK_GRANT;
862 gnt = BGE_APE_PER_LOCK_GRANT;
867 case BGE_APE_LOCK_GPIO:
868 if (sc->bge_asicrev == BGE_ASICREV_BCM5761)
870 if (sc->bge_func_addr == 0)
871 bit = BGE_APE_LOCK_GRANT_DRIVER0;
873 bit = (1 << sc->bge_func_addr);
875 case BGE_APE_LOCK_GRC:
876 if (sc->bge_func_addr == 0)
877 bit = BGE_APE_LOCK_GRANT_DRIVER0;
879 bit = (1 << sc->bge_func_addr);
881 case BGE_APE_LOCK_MEM:
882 if (sc->bge_func_addr == 0)
883 bit = BGE_APE_LOCK_GRANT_DRIVER0;
885 bit = (1 << sc->bge_func_addr);
887 case BGE_APE_LOCK_PHY0:
888 case BGE_APE_LOCK_PHY1:
889 case BGE_APE_LOCK_PHY2:
890 case BGE_APE_LOCK_PHY3:
891 bit = BGE_APE_LOCK_GRANT_DRIVER0;
897 APE_WRITE_4(sc, gnt + off, bit);
901 * Send an event to the APE firmware.
904 bge_ape_send_event(struct bge_softc *sc, uint32_t event)
909 /* NCSI does not support APE events. */
910 if ((sc->bge_mfw_flags & BGE_MFW_ON_APE) == 0)
913 /* Wait up to 1ms for APE to service previous event. */
914 for (i = 10; i > 0; i--) {
915 if (bge_ape_lock(sc, BGE_APE_LOCK_MEM) != 0)
917 apedata = APE_READ_4(sc, BGE_APE_EVENT_STATUS);
918 if ((apedata & BGE_APE_EVENT_STATUS_EVENT_PENDING) == 0) {
919 APE_WRITE_4(sc, BGE_APE_EVENT_STATUS, event |
920 BGE_APE_EVENT_STATUS_EVENT_PENDING);
921 bge_ape_unlock(sc, BGE_APE_LOCK_MEM);
922 APE_WRITE_4(sc, BGE_APE_EVENT, BGE_APE_EVENT_1);
925 bge_ape_unlock(sc, BGE_APE_LOCK_MEM);
929 device_printf(sc->bge_dev, "APE event 0x%08x send timed out\n",
934 bge_ape_driver_state_change(struct bge_softc *sc, int kind)
936 uint32_t apedata, event;
938 if ((sc->bge_mfw_flags & BGE_MFW_ON_APE) == 0)
942 case BGE_RESET_START:
943 /* If this is the first load, clear the load counter. */
944 apedata = APE_READ_4(sc, BGE_APE_HOST_SEG_SIG);
945 if (apedata != BGE_APE_HOST_SEG_SIG_MAGIC)
946 APE_WRITE_4(sc, BGE_APE_HOST_INIT_COUNT, 0);
948 apedata = APE_READ_4(sc, BGE_APE_HOST_INIT_COUNT);
949 APE_WRITE_4(sc, BGE_APE_HOST_INIT_COUNT, ++apedata);
951 APE_WRITE_4(sc, BGE_APE_HOST_SEG_SIG,
952 BGE_APE_HOST_SEG_SIG_MAGIC);
953 APE_WRITE_4(sc, BGE_APE_HOST_SEG_LEN,
954 BGE_APE_HOST_SEG_LEN_MAGIC);
956 /* Add some version info if bge(4) supports it. */
957 APE_WRITE_4(sc, BGE_APE_HOST_DRIVER_ID,
958 BGE_APE_HOST_DRIVER_ID_MAGIC(1, 0));
959 APE_WRITE_4(sc, BGE_APE_HOST_BEHAVIOR,
960 BGE_APE_HOST_BEHAV_NO_PHYLOCK);
961 APE_WRITE_4(sc, BGE_APE_HOST_HEARTBEAT_INT_MS,
962 BGE_APE_HOST_HEARTBEAT_INT_DISABLE);
963 APE_WRITE_4(sc, BGE_APE_HOST_DRVR_STATE,
964 BGE_APE_HOST_DRVR_STATE_START);
965 event = BGE_APE_EVENT_STATUS_STATE_START;
967 case BGE_RESET_SHUTDOWN:
968 APE_WRITE_4(sc, BGE_APE_HOST_DRVR_STATE,
969 BGE_APE_HOST_DRVR_STATE_UNLOAD);
970 event = BGE_APE_EVENT_STATUS_STATE_UNLOAD;
972 case BGE_RESET_SUSPEND:
973 event = BGE_APE_EVENT_STATUS_STATE_SUSPEND;
979 bge_ape_send_event(sc, event | BGE_APE_EVENT_STATUS_DRIVER_EVNT |
980 BGE_APE_EVENT_STATUS_STATE_CHNGE);
984 * Map a single buffer address.
988 bge_dma_map_addr(void *arg, bus_dma_segment_t *segs, int nseg, int error)
990 struct bge_dmamap_arg *ctx;
995 KASSERT(nseg == 1, ("%s: %d segments returned!", __func__, nseg));
998 ctx->bge_busaddr = segs->ds_addr;
1002 bge_nvram_getbyte(struct bge_softc *sc, int addr, uint8_t *dest)
1004 uint32_t access, byte = 0;
1008 CSR_WRITE_4(sc, BGE_NVRAM_SWARB, BGE_NVRAMSWARB_SET1);
1009 for (i = 0; i < 8000; i++) {
1010 if (CSR_READ_4(sc, BGE_NVRAM_SWARB) & BGE_NVRAMSWARB_GNT1)
1017 /* Enable access. */
1018 access = CSR_READ_4(sc, BGE_NVRAM_ACCESS);
1019 CSR_WRITE_4(sc, BGE_NVRAM_ACCESS, access | BGE_NVRAMACC_ENABLE);
1021 CSR_WRITE_4(sc, BGE_NVRAM_ADDR, addr & 0xfffffffc);
1022 CSR_WRITE_4(sc, BGE_NVRAM_CMD, BGE_NVRAM_READCMD);
1023 for (i = 0; i < BGE_TIMEOUT * 10; i++) {
1025 if (CSR_READ_4(sc, BGE_NVRAM_CMD) & BGE_NVRAMCMD_DONE) {
1031 if (i == BGE_TIMEOUT * 10) {
1032 if_printf(sc->bge_ifp, "nvram read timed out\n");
1037 byte = CSR_READ_4(sc, BGE_NVRAM_RDDATA);
1039 *dest = (bswap32(byte) >> ((addr % 4) * 8)) & 0xFF;
1041 /* Disable access. */
1042 CSR_WRITE_4(sc, BGE_NVRAM_ACCESS, access);
1045 CSR_WRITE_4(sc, BGE_NVRAM_SWARB, BGE_NVRAMSWARB_CLR1);
1046 CSR_READ_4(sc, BGE_NVRAM_SWARB);
1052 * Read a sequence of bytes from NVRAM.
1055 bge_read_nvram(struct bge_softc *sc, caddr_t dest, int off, int cnt)
1060 if (sc->bge_asicrev != BGE_ASICREV_BCM5906)
1063 for (i = 0; i < cnt; i++) {
1064 err = bge_nvram_getbyte(sc, off + i, &byte);
1070 return (err ? 1 : 0);
1074 * Read a byte of data stored in the EEPROM at address 'addr.' The
1075 * BCM570x supports both the traditional bitbang interface and an
1076 * auto access interface for reading the EEPROM. We use the auto
1080 bge_eeprom_getbyte(struct bge_softc *sc, int addr, uint8_t *dest)
1086 * Enable use of auto EEPROM access so we can avoid
1087 * having to use the bitbang method.
1089 BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_AUTO_EEPROM);
1091 /* Reset the EEPROM, load the clock period. */
1092 CSR_WRITE_4(sc, BGE_EE_ADDR,
1093 BGE_EEADDR_RESET | BGE_EEHALFCLK(BGE_HALFCLK_384SCL));
1096 /* Issue the read EEPROM command. */
1097 CSR_WRITE_4(sc, BGE_EE_ADDR, BGE_EE_READCMD | addr);
1099 /* Wait for completion */
1100 for(i = 0; i < BGE_TIMEOUT * 10; i++) {
1102 if (CSR_READ_4(sc, BGE_EE_ADDR) & BGE_EEADDR_DONE)
1106 if (i == BGE_TIMEOUT * 10) {
1107 device_printf(sc->bge_dev, "EEPROM read timed out\n");
1112 byte = CSR_READ_4(sc, BGE_EE_DATA);
1114 *dest = (byte >> ((addr % 4) * 8)) & 0xFF;
1120 * Read a sequence of bytes from the EEPROM.
1123 bge_read_eeprom(struct bge_softc *sc, caddr_t dest, int off, int cnt)
1128 for (i = 0; i < cnt; i++) {
1129 error = bge_eeprom_getbyte(sc, off + i, &byte);
1135 return (error ? 1 : 0);
1139 bge_miibus_readreg(device_t dev, int phy, int reg)
1141 struct bge_softc *sc;
1145 sc = device_get_softc(dev);
1147 if (bge_ape_lock(sc, sc->bge_phy_ape_lock) != 0)
1150 /* Clear the autopoll bit if set, otherwise may trigger PCI errors. */
1151 if ((sc->bge_mi_mode & BGE_MIMODE_AUTOPOLL) != 0) {
1152 CSR_WRITE_4(sc, BGE_MI_MODE,
1153 sc->bge_mi_mode & ~BGE_MIMODE_AUTOPOLL);
1157 CSR_WRITE_4(sc, BGE_MI_COMM, BGE_MICMD_READ | BGE_MICOMM_BUSY |
1158 BGE_MIPHY(phy) | BGE_MIREG(reg));
1160 /* Poll for the PHY register access to complete. */
1161 for (i = 0; i < BGE_TIMEOUT; i++) {
1163 val = CSR_READ_4(sc, BGE_MI_COMM);
1164 if ((val & BGE_MICOMM_BUSY) == 0) {
1166 val = CSR_READ_4(sc, BGE_MI_COMM);
1171 if (i == BGE_TIMEOUT) {
1172 device_printf(sc->bge_dev,
1173 "PHY read timed out (phy %d, reg %d, val 0x%08x)\n",
1178 /* Restore the autopoll bit if necessary. */
1179 if ((sc->bge_mi_mode & BGE_MIMODE_AUTOPOLL) != 0) {
1180 CSR_WRITE_4(sc, BGE_MI_MODE, sc->bge_mi_mode);
1184 bge_ape_unlock(sc, sc->bge_phy_ape_lock);
1186 if (val & BGE_MICOMM_READFAIL)
1189 return (val & 0xFFFF);
1193 bge_miibus_writereg(device_t dev, int phy, int reg, int val)
1195 struct bge_softc *sc;
1198 sc = device_get_softc(dev);
1200 if (sc->bge_asicrev == BGE_ASICREV_BCM5906 &&
1201 (reg == BRGPHY_MII_1000CTL || reg == BRGPHY_MII_AUXCTL))
1204 if (bge_ape_lock(sc, sc->bge_phy_ape_lock) != 0)
1207 /* Clear the autopoll bit if set, otherwise may trigger PCI errors. */
1208 if ((sc->bge_mi_mode & BGE_MIMODE_AUTOPOLL) != 0) {
1209 CSR_WRITE_4(sc, BGE_MI_MODE,
1210 sc->bge_mi_mode & ~BGE_MIMODE_AUTOPOLL);
1214 CSR_WRITE_4(sc, BGE_MI_COMM, BGE_MICMD_WRITE | BGE_MICOMM_BUSY |
1215 BGE_MIPHY(phy) | BGE_MIREG(reg) | val);
1217 for (i = 0; i < BGE_TIMEOUT; i++) {
1219 if (!(CSR_READ_4(sc, BGE_MI_COMM) & BGE_MICOMM_BUSY)) {
1221 CSR_READ_4(sc, BGE_MI_COMM); /* dummy read */
1226 /* Restore the autopoll bit if necessary. */
1227 if ((sc->bge_mi_mode & BGE_MIMODE_AUTOPOLL) != 0) {
1228 CSR_WRITE_4(sc, BGE_MI_MODE, sc->bge_mi_mode);
1232 bge_ape_unlock(sc, sc->bge_phy_ape_lock);
1234 if (i == BGE_TIMEOUT)
1235 device_printf(sc->bge_dev,
1236 "PHY write timed out (phy %d, reg %d, val 0x%04x)\n",
1243 bge_miibus_statchg(device_t dev)
1245 struct bge_softc *sc;
1246 struct mii_data *mii;
1247 uint32_t mac_mode, rx_mode, tx_mode;
1249 sc = device_get_softc(dev);
1250 if ((sc->bge_ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
1252 mii = device_get_softc(sc->bge_miibus);
1254 if ((mii->mii_media_status & (IFM_ACTIVE | IFM_AVALID)) ==
1255 (IFM_ACTIVE | IFM_AVALID)) {
1256 switch (IFM_SUBTYPE(mii->mii_media_active)) {
1264 if (sc->bge_asicrev != BGE_ASICREV_BCM5906)
1275 if (sc->bge_link == 0)
1279 * APE firmware touches these registers to keep the MAC
1280 * connected to the outside world. Try to keep the
1284 /* Set the port mode (MII/GMII) to match the link speed. */
1285 mac_mode = CSR_READ_4(sc, BGE_MAC_MODE) &
1286 ~(BGE_MACMODE_PORTMODE | BGE_MACMODE_HALF_DUPLEX);
1287 tx_mode = CSR_READ_4(sc, BGE_TX_MODE);
1288 rx_mode = CSR_READ_4(sc, BGE_RX_MODE);
1290 if (IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_T ||
1291 IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_SX)
1292 mac_mode |= BGE_PORTMODE_GMII;
1294 mac_mode |= BGE_PORTMODE_MII;
1296 /* Set MAC flow control behavior to match link flow control settings. */
1297 tx_mode &= ~BGE_TXMODE_FLOWCTL_ENABLE;
1298 rx_mode &= ~BGE_RXMODE_FLOWCTL_ENABLE;
1299 if ((IFM_OPTIONS(mii->mii_media_active) & IFM_FDX) != 0) {
1300 if ((IFM_OPTIONS(mii->mii_media_active) & IFM_ETH_TXPAUSE) != 0)
1301 tx_mode |= BGE_TXMODE_FLOWCTL_ENABLE;
1302 if ((IFM_OPTIONS(mii->mii_media_active) & IFM_ETH_RXPAUSE) != 0)
1303 rx_mode |= BGE_RXMODE_FLOWCTL_ENABLE;
1305 mac_mode |= BGE_MACMODE_HALF_DUPLEX;
1307 CSR_WRITE_4(sc, BGE_MAC_MODE, mac_mode);
1309 CSR_WRITE_4(sc, BGE_TX_MODE, tx_mode);
1310 CSR_WRITE_4(sc, BGE_RX_MODE, rx_mode);
1314 * Intialize a standard receive ring descriptor.
1317 bge_newbuf_std(struct bge_softc *sc, int i)
1320 struct bge_rx_bd *r;
1321 bus_dma_segment_t segs[1];
1325 if (sc->bge_flags & BGE_FLAG_JUMBO_STD &&
1326 (sc->bge_ifp->if_mtu + ETHER_HDR_LEN + ETHER_CRC_LEN +
1327 ETHER_VLAN_ENCAP_LEN > (MCLBYTES - ETHER_ALIGN))) {
1328 m = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR, MJUM9BYTES);
1331 m->m_len = m->m_pkthdr.len = MJUM9BYTES;
1333 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
1336 m->m_len = m->m_pkthdr.len = MCLBYTES;
1338 if ((sc->bge_flags & BGE_FLAG_RX_ALIGNBUG) == 0)
1339 m_adj(m, ETHER_ALIGN);
1341 error = bus_dmamap_load_mbuf_sg(sc->bge_cdata.bge_rx_mtag,
1342 sc->bge_cdata.bge_rx_std_sparemap, m, segs, &nsegs, 0);
1347 if (sc->bge_cdata.bge_rx_std_chain[i] != NULL) {
1348 bus_dmamap_sync(sc->bge_cdata.bge_rx_mtag,
1349 sc->bge_cdata.bge_rx_std_dmamap[i], BUS_DMASYNC_POSTREAD);
1350 bus_dmamap_unload(sc->bge_cdata.bge_rx_mtag,
1351 sc->bge_cdata.bge_rx_std_dmamap[i]);
1353 map = sc->bge_cdata.bge_rx_std_dmamap[i];
1354 sc->bge_cdata.bge_rx_std_dmamap[i] = sc->bge_cdata.bge_rx_std_sparemap;
1355 sc->bge_cdata.bge_rx_std_sparemap = map;
1356 sc->bge_cdata.bge_rx_std_chain[i] = m;
1357 sc->bge_cdata.bge_rx_std_seglen[i] = segs[0].ds_len;
1358 r = &sc->bge_ldata.bge_rx_std_ring[sc->bge_std];
1359 r->bge_addr.bge_addr_lo = BGE_ADDR_LO(segs[0].ds_addr);
1360 r->bge_addr.bge_addr_hi = BGE_ADDR_HI(segs[0].ds_addr);
1361 r->bge_flags = BGE_RXBDFLAG_END;
1362 r->bge_len = segs[0].ds_len;
1365 bus_dmamap_sync(sc->bge_cdata.bge_rx_mtag,
1366 sc->bge_cdata.bge_rx_std_dmamap[i], BUS_DMASYNC_PREREAD);
1372 * Initialize a jumbo receive ring descriptor. This allocates
1373 * a jumbo buffer from the pool managed internally by the driver.
1376 bge_newbuf_jumbo(struct bge_softc *sc, int i)
1378 bus_dma_segment_t segs[BGE_NSEG_JUMBO];
1380 struct bge_extrx_bd *r;
1384 MGETHDR(m, M_NOWAIT, MT_DATA);
1388 m_cljget(m, M_NOWAIT, MJUM9BYTES);
1389 if (!(m->m_flags & M_EXT)) {
1393 m->m_len = m->m_pkthdr.len = MJUM9BYTES;
1394 if ((sc->bge_flags & BGE_FLAG_RX_ALIGNBUG) == 0)
1395 m_adj(m, ETHER_ALIGN);
1397 error = bus_dmamap_load_mbuf_sg(sc->bge_cdata.bge_mtag_jumbo,
1398 sc->bge_cdata.bge_rx_jumbo_sparemap, m, segs, &nsegs, 0);
1404 if (sc->bge_cdata.bge_rx_jumbo_chain[i] != NULL) {
1405 bus_dmamap_sync(sc->bge_cdata.bge_mtag_jumbo,
1406 sc->bge_cdata.bge_rx_jumbo_dmamap[i], BUS_DMASYNC_POSTREAD);
1407 bus_dmamap_unload(sc->bge_cdata.bge_mtag_jumbo,
1408 sc->bge_cdata.bge_rx_jumbo_dmamap[i]);
1410 map = sc->bge_cdata.bge_rx_jumbo_dmamap[i];
1411 sc->bge_cdata.bge_rx_jumbo_dmamap[i] =
1412 sc->bge_cdata.bge_rx_jumbo_sparemap;
1413 sc->bge_cdata.bge_rx_jumbo_sparemap = map;
1414 sc->bge_cdata.bge_rx_jumbo_chain[i] = m;
1415 sc->bge_cdata.bge_rx_jumbo_seglen[i][0] = 0;
1416 sc->bge_cdata.bge_rx_jumbo_seglen[i][1] = 0;
1417 sc->bge_cdata.bge_rx_jumbo_seglen[i][2] = 0;
1418 sc->bge_cdata.bge_rx_jumbo_seglen[i][3] = 0;
1421 * Fill in the extended RX buffer descriptor.
1423 r = &sc->bge_ldata.bge_rx_jumbo_ring[sc->bge_jumbo];
1424 r->bge_flags = BGE_RXBDFLAG_JUMBO_RING | BGE_RXBDFLAG_END;
1426 r->bge_len3 = r->bge_len2 = r->bge_len1 = 0;
1429 r->bge_addr3.bge_addr_lo = BGE_ADDR_LO(segs[3].ds_addr);
1430 r->bge_addr3.bge_addr_hi = BGE_ADDR_HI(segs[3].ds_addr);
1431 r->bge_len3 = segs[3].ds_len;
1432 sc->bge_cdata.bge_rx_jumbo_seglen[i][3] = segs[3].ds_len;
1434 r->bge_addr2.bge_addr_lo = BGE_ADDR_LO(segs[2].ds_addr);
1435 r->bge_addr2.bge_addr_hi = BGE_ADDR_HI(segs[2].ds_addr);
1436 r->bge_len2 = segs[2].ds_len;
1437 sc->bge_cdata.bge_rx_jumbo_seglen[i][2] = segs[2].ds_len;
1439 r->bge_addr1.bge_addr_lo = BGE_ADDR_LO(segs[1].ds_addr);
1440 r->bge_addr1.bge_addr_hi = BGE_ADDR_HI(segs[1].ds_addr);
1441 r->bge_len1 = segs[1].ds_len;
1442 sc->bge_cdata.bge_rx_jumbo_seglen[i][1] = segs[1].ds_len;
1444 r->bge_addr0.bge_addr_lo = BGE_ADDR_LO(segs[0].ds_addr);
1445 r->bge_addr0.bge_addr_hi = BGE_ADDR_HI(segs[0].ds_addr);
1446 r->bge_len0 = segs[0].ds_len;
1447 sc->bge_cdata.bge_rx_jumbo_seglen[i][0] = segs[0].ds_len;
1450 panic("%s: %d segments\n", __func__, nsegs);
1453 bus_dmamap_sync(sc->bge_cdata.bge_mtag_jumbo,
1454 sc->bge_cdata.bge_rx_jumbo_dmamap[i], BUS_DMASYNC_PREREAD);
1460 bge_init_rx_ring_std(struct bge_softc *sc)
1464 bzero(sc->bge_ldata.bge_rx_std_ring, BGE_STD_RX_RING_SZ);
1466 for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
1467 if ((error = bge_newbuf_std(sc, i)) != 0)
1469 BGE_INC(sc->bge_std, BGE_STD_RX_RING_CNT);
1472 bus_dmamap_sync(sc->bge_cdata.bge_rx_std_ring_tag,
1473 sc->bge_cdata.bge_rx_std_ring_map, BUS_DMASYNC_PREWRITE);
1476 bge_writembx(sc, BGE_MBX_RX_STD_PROD_LO, BGE_STD_RX_RING_CNT - 1);
1482 bge_free_rx_ring_std(struct bge_softc *sc)
1486 for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
1487 if (sc->bge_cdata.bge_rx_std_chain[i] != NULL) {
1488 bus_dmamap_sync(sc->bge_cdata.bge_rx_mtag,
1489 sc->bge_cdata.bge_rx_std_dmamap[i],
1490 BUS_DMASYNC_POSTREAD);
1491 bus_dmamap_unload(sc->bge_cdata.bge_rx_mtag,
1492 sc->bge_cdata.bge_rx_std_dmamap[i]);
1493 m_freem(sc->bge_cdata.bge_rx_std_chain[i]);
1494 sc->bge_cdata.bge_rx_std_chain[i] = NULL;
1496 bzero((char *)&sc->bge_ldata.bge_rx_std_ring[i],
1497 sizeof(struct bge_rx_bd));
1502 bge_init_rx_ring_jumbo(struct bge_softc *sc)
1504 struct bge_rcb *rcb;
1507 bzero(sc->bge_ldata.bge_rx_jumbo_ring, BGE_JUMBO_RX_RING_SZ);
1509 for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
1510 if ((error = bge_newbuf_jumbo(sc, i)) != 0)
1512 BGE_INC(sc->bge_jumbo, BGE_JUMBO_RX_RING_CNT);
1515 bus_dmamap_sync(sc->bge_cdata.bge_rx_jumbo_ring_tag,
1516 sc->bge_cdata.bge_rx_jumbo_ring_map, BUS_DMASYNC_PREWRITE);
1520 /* Enable the jumbo receive producer ring. */
1521 rcb = &sc->bge_ldata.bge_info.bge_jumbo_rx_rcb;
1522 rcb->bge_maxlen_flags =
1523 BGE_RCB_MAXLEN_FLAGS(0, BGE_RCB_FLAG_USE_EXT_RX_BD);
1524 CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_MAXLEN_FLAGS, rcb->bge_maxlen_flags);
1526 bge_writembx(sc, BGE_MBX_RX_JUMBO_PROD_LO, BGE_JUMBO_RX_RING_CNT - 1);
1532 bge_free_rx_ring_jumbo(struct bge_softc *sc)
1536 for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
1537 if (sc->bge_cdata.bge_rx_jumbo_chain[i] != NULL) {
1538 bus_dmamap_sync(sc->bge_cdata.bge_mtag_jumbo,
1539 sc->bge_cdata.bge_rx_jumbo_dmamap[i],
1540 BUS_DMASYNC_POSTREAD);
1541 bus_dmamap_unload(sc->bge_cdata.bge_mtag_jumbo,
1542 sc->bge_cdata.bge_rx_jumbo_dmamap[i]);
1543 m_freem(sc->bge_cdata.bge_rx_jumbo_chain[i]);
1544 sc->bge_cdata.bge_rx_jumbo_chain[i] = NULL;
1546 bzero((char *)&sc->bge_ldata.bge_rx_jumbo_ring[i],
1547 sizeof(struct bge_extrx_bd));
1552 bge_free_tx_ring(struct bge_softc *sc)
1556 if (sc->bge_ldata.bge_tx_ring == NULL)
1559 for (i = 0; i < BGE_TX_RING_CNT; i++) {
1560 if (sc->bge_cdata.bge_tx_chain[i] != NULL) {
1561 bus_dmamap_sync(sc->bge_cdata.bge_tx_mtag,
1562 sc->bge_cdata.bge_tx_dmamap[i],
1563 BUS_DMASYNC_POSTWRITE);
1564 bus_dmamap_unload(sc->bge_cdata.bge_tx_mtag,
1565 sc->bge_cdata.bge_tx_dmamap[i]);
1566 m_freem(sc->bge_cdata.bge_tx_chain[i]);
1567 sc->bge_cdata.bge_tx_chain[i] = NULL;
1569 bzero((char *)&sc->bge_ldata.bge_tx_ring[i],
1570 sizeof(struct bge_tx_bd));
1575 bge_init_tx_ring(struct bge_softc *sc)
1578 sc->bge_tx_saved_considx = 0;
1580 bzero(sc->bge_ldata.bge_tx_ring, BGE_TX_RING_SZ);
1581 bus_dmamap_sync(sc->bge_cdata.bge_tx_ring_tag,
1582 sc->bge_cdata.bge_tx_ring_map, BUS_DMASYNC_PREWRITE);
1584 /* Initialize transmit producer index for host-memory send ring. */
1585 sc->bge_tx_prodidx = 0;
1586 bge_writembx(sc, BGE_MBX_TX_HOST_PROD0_LO, sc->bge_tx_prodidx);
1588 /* 5700 b2 errata */
1589 if (sc->bge_chiprev == BGE_CHIPREV_5700_BX)
1590 bge_writembx(sc, BGE_MBX_TX_HOST_PROD0_LO, sc->bge_tx_prodidx);
1592 /* NIC-memory send ring not used; initialize to zero. */
1593 bge_writembx(sc, BGE_MBX_TX_NIC_PROD0_LO, 0);
1594 /* 5700 b2 errata */
1595 if (sc->bge_chiprev == BGE_CHIPREV_5700_BX)
1596 bge_writembx(sc, BGE_MBX_TX_NIC_PROD0_LO, 0);
1602 bge_setpromisc(struct bge_softc *sc)
1606 BGE_LOCK_ASSERT(sc);
1610 /* Enable or disable promiscuous mode as needed. */
1611 if (ifp->if_flags & IFF_PROMISC)
1612 BGE_SETBIT(sc, BGE_RX_MODE, BGE_RXMODE_RX_PROMISC);
1614 BGE_CLRBIT(sc, BGE_RX_MODE, BGE_RXMODE_RX_PROMISC);
1618 bge_setmulti(struct bge_softc *sc)
1621 struct ifmultiaddr *ifma;
1622 uint32_t hashes[4] = { 0, 0, 0, 0 };
1625 BGE_LOCK_ASSERT(sc);
1629 if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
1630 for (i = 0; i < 4; i++)
1631 CSR_WRITE_4(sc, BGE_MAR0 + (i * 4), 0xFFFFFFFF);
1635 /* First, zot all the existing filters. */
1636 for (i = 0; i < 4; i++)
1637 CSR_WRITE_4(sc, BGE_MAR0 + (i * 4), 0);
1639 /* Now program new ones. */
1640 if_maddr_rlock(ifp);
1641 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1642 if (ifma->ifma_addr->sa_family != AF_LINK)
1644 h = ether_crc32_le(LLADDR((struct sockaddr_dl *)
1645 ifma->ifma_addr), ETHER_ADDR_LEN) & 0x7F;
1646 hashes[(h & 0x60) >> 5] |= 1 << (h & 0x1F);
1648 if_maddr_runlock(ifp);
1650 for (i = 0; i < 4; i++)
1651 CSR_WRITE_4(sc, BGE_MAR0 + (i * 4), hashes[i]);
1655 bge_setvlan(struct bge_softc *sc)
1659 BGE_LOCK_ASSERT(sc);
1663 /* Enable or disable VLAN tag stripping as needed. */
1664 if (ifp->if_capenable & IFCAP_VLAN_HWTAGGING)
1665 BGE_CLRBIT(sc, BGE_RX_MODE, BGE_RXMODE_RX_KEEP_VLAN_DIAG);
1667 BGE_SETBIT(sc, BGE_RX_MODE, BGE_RXMODE_RX_KEEP_VLAN_DIAG);
1671 bge_sig_pre_reset(struct bge_softc *sc, int type)
1675 * Some chips don't like this so only do this if ASF is enabled
1677 if (sc->bge_asf_mode)
1678 bge_writemem_ind(sc, BGE_SRAM_FW_MB, BGE_SRAM_FW_MB_MAGIC);
1680 if (sc->bge_asf_mode & ASF_NEW_HANDSHAKE) {
1682 case BGE_RESET_START:
1683 bge_writemem_ind(sc, BGE_SRAM_FW_DRV_STATE_MB,
1684 BGE_FW_DRV_STATE_START);
1686 case BGE_RESET_SHUTDOWN:
1687 bge_writemem_ind(sc, BGE_SRAM_FW_DRV_STATE_MB,
1688 BGE_FW_DRV_STATE_UNLOAD);
1690 case BGE_RESET_SUSPEND:
1691 bge_writemem_ind(sc, BGE_SRAM_FW_DRV_STATE_MB,
1692 BGE_FW_DRV_STATE_SUSPEND);
1697 if (type == BGE_RESET_START || type == BGE_RESET_SUSPEND)
1698 bge_ape_driver_state_change(sc, type);
1702 bge_sig_post_reset(struct bge_softc *sc, int type)
1705 if (sc->bge_asf_mode & ASF_NEW_HANDSHAKE) {
1707 case BGE_RESET_START:
1708 bge_writemem_ind(sc, BGE_SRAM_FW_DRV_STATE_MB,
1709 BGE_FW_DRV_STATE_START_DONE);
1712 case BGE_RESET_SHUTDOWN:
1713 bge_writemem_ind(sc, BGE_SRAM_FW_DRV_STATE_MB,
1714 BGE_FW_DRV_STATE_UNLOAD_DONE);
1718 if (type == BGE_RESET_SHUTDOWN)
1719 bge_ape_driver_state_change(sc, type);
1723 bge_sig_legacy(struct bge_softc *sc, int type)
1726 if (sc->bge_asf_mode) {
1728 case BGE_RESET_START:
1729 bge_writemem_ind(sc, BGE_SRAM_FW_DRV_STATE_MB,
1730 BGE_FW_DRV_STATE_START);
1732 case BGE_RESET_SHUTDOWN:
1733 bge_writemem_ind(sc, BGE_SRAM_FW_DRV_STATE_MB,
1734 BGE_FW_DRV_STATE_UNLOAD);
1741 bge_stop_fw(struct bge_softc *sc)
1745 if (sc->bge_asf_mode) {
1746 bge_writemem_ind(sc, BGE_SRAM_FW_CMD_MB, BGE_FW_CMD_PAUSE);
1747 CSR_WRITE_4(sc, BGE_RX_CPU_EVENT,
1748 CSR_READ_4(sc, BGE_RX_CPU_EVENT) | BGE_RX_CPU_DRV_EVENT);
1750 for (i = 0; i < 100; i++ ) {
1751 if (!(CSR_READ_4(sc, BGE_RX_CPU_EVENT) &
1752 BGE_RX_CPU_DRV_EVENT))
1760 bge_dma_swap_options(struct bge_softc *sc)
1762 uint32_t dma_options;
1764 dma_options = BGE_MODECTL_WORDSWAP_NONFRAME |
1765 BGE_MODECTL_BYTESWAP_DATA | BGE_MODECTL_WORDSWAP_DATA;
1766 #if BYTE_ORDER == BIG_ENDIAN
1767 dma_options |= BGE_MODECTL_BYTESWAP_NONFRAME;
1769 return (dma_options);
1773 * Do endian, PCI and DMA initialization.
1776 bge_chipinit(struct bge_softc *sc)
1778 uint32_t dma_rw_ctl, misc_ctl, mode_ctl;
1782 /* Set endianness before we access any non-PCI registers. */
1783 misc_ctl = BGE_INIT;
1784 if (sc->bge_flags & BGE_FLAG_TAGGED_STATUS)
1785 misc_ctl |= BGE_PCIMISCCTL_TAGGED_STATUS;
1786 pci_write_config(sc->bge_dev, BGE_PCI_MISC_CTL, misc_ctl, 4);
1789 * Clear the MAC statistics block in the NIC's
1792 for (i = BGE_STATS_BLOCK;
1793 i < BGE_STATS_BLOCK_END + 1; i += sizeof(uint32_t))
1794 BGE_MEMWIN_WRITE(sc, i, 0);
1796 for (i = BGE_STATUS_BLOCK;
1797 i < BGE_STATUS_BLOCK_END + 1; i += sizeof(uint32_t))
1798 BGE_MEMWIN_WRITE(sc, i, 0);
1800 if (sc->bge_chiprev == BGE_CHIPREV_5704_BX) {
1802 * Fix data corruption caused by non-qword write with WB.
1803 * Fix master abort in PCI mode.
1804 * Fix PCI latency timer.
1806 val = pci_read_config(sc->bge_dev, BGE_PCI_MSI_DATA + 2, 2);
1807 val |= (1 << 10) | (1 << 12) | (1 << 13);
1808 pci_write_config(sc->bge_dev, BGE_PCI_MSI_DATA + 2, val, 2);
1811 if (sc->bge_asicrev == BGE_ASICREV_BCM57765 ||
1812 sc->bge_asicrev == BGE_ASICREV_BCM57766) {
1814 * For the 57766 and non Ax versions of 57765, bootcode
1815 * needs to setup the PCIE Fast Training Sequence (FTS)
1816 * value to prevent transmit hangs.
1818 if (sc->bge_chiprev != BGE_CHIPREV_57765_AX) {
1819 CSR_WRITE_4(sc, BGE_CPMU_PADRNG_CTL,
1820 CSR_READ_4(sc, BGE_CPMU_PADRNG_CTL) |
1821 BGE_CPMU_PADRNG_CTL_RDIV2);
1826 * Set up the PCI DMA control register.
1828 dma_rw_ctl = BGE_PCIDMARWCTL_RD_CMD_SHIFT(6) |
1829 BGE_PCIDMARWCTL_WR_CMD_SHIFT(7);
1830 if (sc->bge_flags & BGE_FLAG_PCIE) {
1831 if (sc->bge_mps >= 256)
1832 dma_rw_ctl |= BGE_PCIDMARWCTL_WR_WAT_SHIFT(7);
1834 dma_rw_ctl |= BGE_PCIDMARWCTL_WR_WAT_SHIFT(3);
1835 } else if (sc->bge_flags & BGE_FLAG_PCIX) {
1836 if (BGE_IS_5714_FAMILY(sc)) {
1837 /* 256 bytes for read and write. */
1838 dma_rw_ctl |= BGE_PCIDMARWCTL_RD_WAT_SHIFT(2) |
1839 BGE_PCIDMARWCTL_WR_WAT_SHIFT(2);
1840 dma_rw_ctl |= (sc->bge_asicrev == BGE_ASICREV_BCM5780) ?
1841 BGE_PCIDMARWCTL_ONEDMA_ATONCE_GLOBAL :
1842 BGE_PCIDMARWCTL_ONEDMA_ATONCE_LOCAL;
1843 } else if (sc->bge_asicrev == BGE_ASICREV_BCM5703) {
1845 * In the BCM5703, the DMA read watermark should
1846 * be set to less than or equal to the maximum
1847 * memory read byte count of the PCI-X command
1850 dma_rw_ctl |= BGE_PCIDMARWCTL_RD_WAT_SHIFT(4) |
1851 BGE_PCIDMARWCTL_WR_WAT_SHIFT(3);
1852 } else if (sc->bge_asicrev == BGE_ASICREV_BCM5704) {
1853 /* 1536 bytes for read, 384 bytes for write. */
1854 dma_rw_ctl |= BGE_PCIDMARWCTL_RD_WAT_SHIFT(7) |
1855 BGE_PCIDMARWCTL_WR_WAT_SHIFT(3);
1857 /* 384 bytes for read and write. */
1858 dma_rw_ctl |= BGE_PCIDMARWCTL_RD_WAT_SHIFT(3) |
1859 BGE_PCIDMARWCTL_WR_WAT_SHIFT(3) |
1862 if (sc->bge_asicrev == BGE_ASICREV_BCM5703 ||
1863 sc->bge_asicrev == BGE_ASICREV_BCM5704) {
1866 /* Set ONE_DMA_AT_ONCE for hardware workaround. */
1867 tmp = CSR_READ_4(sc, BGE_PCI_CLKCTL) & 0x1F;
1868 if (tmp == 6 || tmp == 7)
1870 BGE_PCIDMARWCTL_ONEDMA_ATONCE_GLOBAL;
1872 /* Set PCI-X DMA write workaround. */
1873 dma_rw_ctl |= BGE_PCIDMARWCTL_ASRT_ALL_BE;
1876 /* Conventional PCI bus: 256 bytes for read and write. */
1877 dma_rw_ctl |= BGE_PCIDMARWCTL_RD_WAT_SHIFT(7) |
1878 BGE_PCIDMARWCTL_WR_WAT_SHIFT(7);
1880 if (sc->bge_asicrev != BGE_ASICREV_BCM5705 &&
1881 sc->bge_asicrev != BGE_ASICREV_BCM5750)
1884 if (sc->bge_asicrev == BGE_ASICREV_BCM5700 ||
1885 sc->bge_asicrev == BGE_ASICREV_BCM5701)
1886 dma_rw_ctl |= BGE_PCIDMARWCTL_USE_MRM |
1887 BGE_PCIDMARWCTL_ASRT_ALL_BE;
1888 if (sc->bge_asicrev == BGE_ASICREV_BCM5703 ||
1889 sc->bge_asicrev == BGE_ASICREV_BCM5704)
1890 dma_rw_ctl &= ~BGE_PCIDMARWCTL_MINDMA;
1891 if (BGE_IS_5717_PLUS(sc)) {
1892 dma_rw_ctl &= ~BGE_PCIDMARWCTL_DIS_CACHE_ALIGNMENT;
1893 if (sc->bge_chipid == BGE_CHIPID_BCM57765_A0)
1894 dma_rw_ctl &= ~BGE_PCIDMARWCTL_CRDRDR_RDMA_MRRS_MSK;
1896 * Enable HW workaround for controllers that misinterpret
1897 * a status tag update and leave interrupts permanently
1900 if (!BGE_IS_57765_PLUS(sc) &&
1901 sc->bge_asicrev != BGE_ASICREV_BCM5717 &&
1902 sc->bge_asicrev != BGE_ASICREV_BCM5762)
1903 dma_rw_ctl |= BGE_PCIDMARWCTL_TAGGED_STATUS_WA;
1905 pci_write_config(sc->bge_dev, BGE_PCI_DMA_RW_CTL, dma_rw_ctl, 4);
1908 * Set up general mode register.
1910 mode_ctl = bge_dma_swap_options(sc);
1911 if (sc->bge_asicrev == BGE_ASICREV_BCM5720 ||
1912 sc->bge_asicrev == BGE_ASICREV_BCM5762) {
1913 /* Retain Host-2-BMC settings written by APE firmware. */
1914 mode_ctl |= CSR_READ_4(sc, BGE_MODE_CTL) &
1915 (BGE_MODECTL_BYTESWAP_B2HRX_DATA |
1916 BGE_MODECTL_WORDSWAP_B2HRX_DATA |
1917 BGE_MODECTL_B2HRX_ENABLE | BGE_MODECTL_HTX2B_ENABLE);
1919 mode_ctl |= BGE_MODECTL_MAC_ATTN_INTR | BGE_MODECTL_HOST_SEND_BDS |
1920 BGE_MODECTL_TX_NO_PHDR_CSUM;
1923 * BCM5701 B5 have a bug causing data corruption when using
1924 * 64-bit DMA reads, which can be terminated early and then
1925 * completed later as 32-bit accesses, in combination with
1928 if (sc->bge_asicrev == BGE_ASICREV_BCM5701 &&
1929 sc->bge_chipid == BGE_CHIPID_BCM5701_B5)
1930 mode_ctl |= BGE_MODECTL_FORCE_PCI32;
1933 * Tell the firmware the driver is running
1935 if (sc->bge_asf_mode & ASF_STACKUP)
1936 mode_ctl |= BGE_MODECTL_STACKUP;
1938 CSR_WRITE_4(sc, BGE_MODE_CTL, mode_ctl);
1941 * Disable memory write invalidate. Apparently it is not supported
1942 * properly by these devices.
1944 PCI_CLRBIT(sc->bge_dev, BGE_PCI_CMD, PCIM_CMD_MWIEN, 4);
1946 /* Set the timer prescaler (always 66 MHz). */
1947 CSR_WRITE_4(sc, BGE_MISC_CFG, BGE_32BITTIME_66MHZ);
1949 /* XXX: The Linux tg3 driver does this at the start of brgphy_reset. */
1950 if (sc->bge_asicrev == BGE_ASICREV_BCM5906) {
1951 DELAY(40); /* XXX */
1953 /* Put PHY into ready state */
1954 BGE_CLRBIT(sc, BGE_MISC_CFG, BGE_MISCCFG_EPHY_IDDQ);
1955 CSR_READ_4(sc, BGE_MISC_CFG); /* Flush */
1963 bge_blockinit(struct bge_softc *sc)
1965 struct bge_rcb *rcb;
1968 uint32_t dmactl, rdmareg, val;
1972 * Initialize the memory window pointer register so that
1973 * we can access the first 32K of internal NIC RAM. This will
1974 * allow us to set up the TX send ring RCBs and the RX return
1975 * ring RCBs, plus other things which live in NIC memory.
1977 CSR_WRITE_4(sc, BGE_PCI_MEMWIN_BASEADDR, 0);
1979 /* Note: the BCM5704 has a smaller mbuf space than other chips. */
1981 if (!(BGE_IS_5705_PLUS(sc))) {
1982 /* Configure mbuf memory pool */
1983 CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_BASEADDR, BGE_BUFFPOOL_1);
1984 if (sc->bge_asicrev == BGE_ASICREV_BCM5704)
1985 CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_LEN, 0x10000);
1987 CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_LEN, 0x18000);
1989 /* Configure DMA resource pool */
1990 CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_BASEADDR,
1991 BGE_DMA_DESCRIPTORS);
1992 CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_LEN, 0x2000);
1995 /* Configure mbuf pool watermarks */
1996 if (BGE_IS_5717_PLUS(sc)) {
1997 CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_READDMA_LOWAT, 0x0);
1998 if (sc->bge_ifp->if_mtu > ETHERMTU) {
1999 CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 0x7e);
2000 CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_HIWAT, 0xea);
2002 CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 0x2a);
2003 CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_HIWAT, 0xa0);
2005 } else if (!BGE_IS_5705_PLUS(sc)) {
2006 CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_READDMA_LOWAT, 0x50);
2007 CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 0x20);
2008 CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_HIWAT, 0x60);
2009 } else if (sc->bge_asicrev == BGE_ASICREV_BCM5906) {
2010 CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_READDMA_LOWAT, 0x0);
2011 CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 0x04);
2012 CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_HIWAT, 0x10);
2014 CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_READDMA_LOWAT, 0x0);
2015 CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 0x10);
2016 CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_HIWAT, 0x60);
2019 /* Configure DMA resource watermarks */
2020 CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_LOWAT, 5);
2021 CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_HIWAT, 10);
2023 /* Enable buffer manager */
2024 val = BGE_BMANMODE_ENABLE | BGE_BMANMODE_LOMBUF_ATTN;
2026 * Change the arbitration algorithm of TXMBUF read request to
2027 * round-robin instead of priority based for BCM5719. When
2028 * TXFIFO is almost empty, RDMA will hold its request until
2029 * TXFIFO is not almost empty.
2031 if (sc->bge_asicrev == BGE_ASICREV_BCM5719)
2032 val |= BGE_BMANMODE_NO_TX_UNDERRUN;
2033 CSR_WRITE_4(sc, BGE_BMAN_MODE, val);
2035 /* Poll for buffer manager start indication */
2036 for (i = 0; i < BGE_TIMEOUT; i++) {
2038 if (CSR_READ_4(sc, BGE_BMAN_MODE) & BGE_BMANMODE_ENABLE)
2042 if (i == BGE_TIMEOUT) {
2043 device_printf(sc->bge_dev, "buffer manager failed to start\n");
2047 /* Enable flow-through queues */
2048 CSR_WRITE_4(sc, BGE_FTQ_RESET, 0xFFFFFFFF);
2049 CSR_WRITE_4(sc, BGE_FTQ_RESET, 0);
2051 /* Wait until queue initialization is complete */
2052 for (i = 0; i < BGE_TIMEOUT; i++) {
2054 if (CSR_READ_4(sc, BGE_FTQ_RESET) == 0)
2058 if (i == BGE_TIMEOUT) {
2059 device_printf(sc->bge_dev, "flow-through queue init failed\n");
2064 * Summary of rings supported by the controller:
2066 * Standard Receive Producer Ring
2067 * - This ring is used to feed receive buffers for "standard"
2068 * sized frames (typically 1536 bytes) to the controller.
2070 * Jumbo Receive Producer Ring
2071 * - This ring is used to feed receive buffers for jumbo sized
2072 * frames (i.e. anything bigger than the "standard" frames)
2073 * to the controller.
2075 * Mini Receive Producer Ring
2076 * - This ring is used to feed receive buffers for "mini"
2077 * sized frames to the controller.
2078 * - This feature required external memory for the controller
2079 * but was never used in a production system. Should always
2082 * Receive Return Ring
2083 * - After the controller has placed an incoming frame into a
2084 * receive buffer that buffer is moved into a receive return
2085 * ring. The driver is then responsible to passing the
2086 * buffer up to the stack. Many versions of the controller
2087 * support multiple RR rings.
2090 * - This ring is used for outgoing frames. Many versions of
2091 * the controller support multiple send rings.
2094 /* Initialize the standard receive producer ring control block. */
2095 rcb = &sc->bge_ldata.bge_info.bge_std_rx_rcb;
2096 rcb->bge_hostaddr.bge_addr_lo =
2097 BGE_ADDR_LO(sc->bge_ldata.bge_rx_std_ring_paddr);
2098 rcb->bge_hostaddr.bge_addr_hi =
2099 BGE_ADDR_HI(sc->bge_ldata.bge_rx_std_ring_paddr);
2100 bus_dmamap_sync(sc->bge_cdata.bge_rx_std_ring_tag,
2101 sc->bge_cdata.bge_rx_std_ring_map, BUS_DMASYNC_PREREAD);
2102 if (BGE_IS_5717_PLUS(sc)) {
2104 * Bits 31-16: Programmable ring size (2048, 1024, 512, .., 32)
2105 * Bits 15-2 : Maximum RX frame size
2106 * Bit 1 : 1 = Ring Disabled, 0 = Ring ENabled
2109 rcb->bge_maxlen_flags =
2110 BGE_RCB_MAXLEN_FLAGS(512, BGE_MAX_FRAMELEN << 2);
2111 } else if (BGE_IS_5705_PLUS(sc)) {
2113 * Bits 31-16: Programmable ring size (512, 256, 128, 64, 32)
2114 * Bits 15-2 : Reserved (should be 0)
2115 * Bit 1 : 1 = Ring Disabled, 0 = Ring Enabled
2118 rcb->bge_maxlen_flags = BGE_RCB_MAXLEN_FLAGS(512, 0);
2121 * Ring size is always XXX entries
2122 * Bits 31-16: Maximum RX frame size
2123 * Bits 15-2 : Reserved (should be 0)
2124 * Bit 1 : 1 = Ring Disabled, 0 = Ring Enabled
2127 rcb->bge_maxlen_flags =
2128 BGE_RCB_MAXLEN_FLAGS(BGE_MAX_FRAMELEN, 0);
2130 if (sc->bge_asicrev == BGE_ASICREV_BCM5717 ||
2131 sc->bge_asicrev == BGE_ASICREV_BCM5719 ||
2132 sc->bge_asicrev == BGE_ASICREV_BCM5720)
2133 rcb->bge_nicaddr = BGE_STD_RX_RINGS_5717;
2135 rcb->bge_nicaddr = BGE_STD_RX_RINGS;
2136 /* Write the standard receive producer ring control block. */
2137 CSR_WRITE_4(sc, BGE_RX_STD_RCB_HADDR_HI, rcb->bge_hostaddr.bge_addr_hi);
2138 CSR_WRITE_4(sc, BGE_RX_STD_RCB_HADDR_LO, rcb->bge_hostaddr.bge_addr_lo);
2139 CSR_WRITE_4(sc, BGE_RX_STD_RCB_MAXLEN_FLAGS, rcb->bge_maxlen_flags);
2140 CSR_WRITE_4(sc, BGE_RX_STD_RCB_NICADDR, rcb->bge_nicaddr);
2142 /* Reset the standard receive producer ring producer index. */
2143 bge_writembx(sc, BGE_MBX_RX_STD_PROD_LO, 0);
2146 * Initialize the jumbo RX producer ring control
2147 * block. We set the 'ring disabled' bit in the
2148 * flags field until we're actually ready to start
2149 * using this ring (i.e. once we set the MTU
2150 * high enough to require it).
2152 if (BGE_IS_JUMBO_CAPABLE(sc)) {
2153 rcb = &sc->bge_ldata.bge_info.bge_jumbo_rx_rcb;
2154 /* Get the jumbo receive producer ring RCB parameters. */
2155 rcb->bge_hostaddr.bge_addr_lo =
2156 BGE_ADDR_LO(sc->bge_ldata.bge_rx_jumbo_ring_paddr);
2157 rcb->bge_hostaddr.bge_addr_hi =
2158 BGE_ADDR_HI(sc->bge_ldata.bge_rx_jumbo_ring_paddr);
2159 bus_dmamap_sync(sc->bge_cdata.bge_rx_jumbo_ring_tag,
2160 sc->bge_cdata.bge_rx_jumbo_ring_map,
2161 BUS_DMASYNC_PREREAD);
2162 rcb->bge_maxlen_flags = BGE_RCB_MAXLEN_FLAGS(0,
2163 BGE_RCB_FLAG_USE_EXT_RX_BD | BGE_RCB_FLAG_RING_DISABLED);
2164 if (sc->bge_asicrev == BGE_ASICREV_BCM5717 ||
2165 sc->bge_asicrev == BGE_ASICREV_BCM5719 ||
2166 sc->bge_asicrev == BGE_ASICREV_BCM5720)
2167 rcb->bge_nicaddr = BGE_JUMBO_RX_RINGS_5717;
2169 rcb->bge_nicaddr = BGE_JUMBO_RX_RINGS;
2170 CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_HADDR_HI,
2171 rcb->bge_hostaddr.bge_addr_hi);
2172 CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_HADDR_LO,
2173 rcb->bge_hostaddr.bge_addr_lo);
2174 /* Program the jumbo receive producer ring RCB parameters. */
2175 CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_MAXLEN_FLAGS,
2176 rcb->bge_maxlen_flags);
2177 CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_NICADDR, rcb->bge_nicaddr);
2178 /* Reset the jumbo receive producer ring producer index. */
2179 bge_writembx(sc, BGE_MBX_RX_JUMBO_PROD_LO, 0);
2182 /* Disable the mini receive producer ring RCB. */
2183 if (BGE_IS_5700_FAMILY(sc)) {
2184 rcb = &sc->bge_ldata.bge_info.bge_mini_rx_rcb;
2185 rcb->bge_maxlen_flags =
2186 BGE_RCB_MAXLEN_FLAGS(0, BGE_RCB_FLAG_RING_DISABLED);
2187 CSR_WRITE_4(sc, BGE_RX_MINI_RCB_MAXLEN_FLAGS,
2188 rcb->bge_maxlen_flags);
2189 /* Reset the mini receive producer ring producer index. */
2190 bge_writembx(sc, BGE_MBX_RX_MINI_PROD_LO, 0);
2193 /* Choose de-pipeline mode for BCM5906 A0, A1 and A2. */
2194 if (sc->bge_asicrev == BGE_ASICREV_BCM5906) {
2195 if (sc->bge_chipid == BGE_CHIPID_BCM5906_A0 ||
2196 sc->bge_chipid == BGE_CHIPID_BCM5906_A1 ||
2197 sc->bge_chipid == BGE_CHIPID_BCM5906_A2)
2198 CSR_WRITE_4(sc, BGE_ISO_PKT_TX,
2199 (CSR_READ_4(sc, BGE_ISO_PKT_TX) & ~3) | 2);
2202 * The BD ring replenish thresholds control how often the
2203 * hardware fetches new BD's from the producer rings in host
2204 * memory. Setting the value too low on a busy system can
2205 * starve the hardware and recue the throughpout.
2207 * Set the BD ring replentish thresholds. The recommended
2208 * values are 1/8th the number of descriptors allocated to
2210 * XXX The 5754 requires a lower threshold, so it might be a
2211 * requirement of all 575x family chips. The Linux driver sets
2212 * the lower threshold for all 5705 family chips as well, but there
2213 * are reports that it might not need to be so strict.
2215 * XXX Linux does some extra fiddling here for the 5906 parts as
2218 if (BGE_IS_5705_PLUS(sc))
2221 val = BGE_STD_RX_RING_CNT / 8;
2222 CSR_WRITE_4(sc, BGE_RBDI_STD_REPL_THRESH, val);
2223 if (BGE_IS_JUMBO_CAPABLE(sc))
2224 CSR_WRITE_4(sc, BGE_RBDI_JUMBO_REPL_THRESH,
2225 BGE_JUMBO_RX_RING_CNT/8);
2226 if (BGE_IS_5717_PLUS(sc)) {
2227 CSR_WRITE_4(sc, BGE_STD_REPLENISH_LWM, 32);
2228 CSR_WRITE_4(sc, BGE_JMB_REPLENISH_LWM, 16);
2232 * Disable all send rings by setting the 'ring disabled' bit
2233 * in the flags field of all the TX send ring control blocks,
2234 * located in NIC memory.
2236 if (!BGE_IS_5705_PLUS(sc))
2237 /* 5700 to 5704 had 16 send rings. */
2238 limit = BGE_TX_RINGS_EXTSSRAM_MAX;
2239 else if (BGE_IS_57765_PLUS(sc) ||
2240 sc->bge_asicrev == BGE_ASICREV_BCM5762)
2242 else if (BGE_IS_5717_PLUS(sc))
2246 vrcb = BGE_MEMWIN_START + BGE_SEND_RING_RCB;
2247 for (i = 0; i < limit; i++) {
2248 RCB_WRITE_4(sc, vrcb, bge_maxlen_flags,
2249 BGE_RCB_MAXLEN_FLAGS(0, BGE_RCB_FLAG_RING_DISABLED));
2250 RCB_WRITE_4(sc, vrcb, bge_nicaddr, 0);
2251 vrcb += sizeof(struct bge_rcb);
2254 /* Configure send ring RCB 0 (we use only the first ring) */
2255 vrcb = BGE_MEMWIN_START + BGE_SEND_RING_RCB;
2256 BGE_HOSTADDR(taddr, sc->bge_ldata.bge_tx_ring_paddr);
2257 RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_hi, taddr.bge_addr_hi);
2258 RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_lo, taddr.bge_addr_lo);
2259 if (sc->bge_asicrev == BGE_ASICREV_BCM5717 ||
2260 sc->bge_asicrev == BGE_ASICREV_BCM5719 ||
2261 sc->bge_asicrev == BGE_ASICREV_BCM5720)
2262 RCB_WRITE_4(sc, vrcb, bge_nicaddr, BGE_SEND_RING_5717);
2264 RCB_WRITE_4(sc, vrcb, bge_nicaddr,
2265 BGE_NIC_TXRING_ADDR(0, BGE_TX_RING_CNT));
2266 RCB_WRITE_4(sc, vrcb, bge_maxlen_flags,
2267 BGE_RCB_MAXLEN_FLAGS(BGE_TX_RING_CNT, 0));
2270 * Disable all receive return rings by setting the
2271 * 'ring diabled' bit in the flags field of all the receive
2272 * return ring control blocks, located in NIC memory.
2274 if (sc->bge_asicrev == BGE_ASICREV_BCM5717 ||
2275 sc->bge_asicrev == BGE_ASICREV_BCM5719 ||
2276 sc->bge_asicrev == BGE_ASICREV_BCM5720) {
2277 /* Should be 17, use 16 until we get an SRAM map. */
2279 } else if (!BGE_IS_5705_PLUS(sc))
2280 limit = BGE_RX_RINGS_MAX;
2281 else if (sc->bge_asicrev == BGE_ASICREV_BCM5755 ||
2282 sc->bge_asicrev == BGE_ASICREV_BCM5762 ||
2283 BGE_IS_57765_PLUS(sc))
2287 /* Disable all receive return rings. */
2288 vrcb = BGE_MEMWIN_START + BGE_RX_RETURN_RING_RCB;
2289 for (i = 0; i < limit; i++) {
2290 RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_hi, 0);
2291 RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_lo, 0);
2292 RCB_WRITE_4(sc, vrcb, bge_maxlen_flags,
2293 BGE_RCB_FLAG_RING_DISABLED);
2294 RCB_WRITE_4(sc, vrcb, bge_nicaddr, 0);
2295 bge_writembx(sc, BGE_MBX_RX_CONS0_LO +
2296 (i * (sizeof(uint64_t))), 0);
2297 vrcb += sizeof(struct bge_rcb);
2301 * Set up receive return ring 0. Note that the NIC address
2302 * for RX return rings is 0x0. The return rings live entirely
2303 * within the host, so the nicaddr field in the RCB isn't used.
2305 vrcb = BGE_MEMWIN_START + BGE_RX_RETURN_RING_RCB;
2306 BGE_HOSTADDR(taddr, sc->bge_ldata.bge_rx_return_ring_paddr);
2307 RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_hi, taddr.bge_addr_hi);
2308 RCB_WRITE_4(sc, vrcb, bge_hostaddr.bge_addr_lo, taddr.bge_addr_lo);
2309 RCB_WRITE_4(sc, vrcb, bge_nicaddr, 0);
2310 RCB_WRITE_4(sc, vrcb, bge_maxlen_flags,
2311 BGE_RCB_MAXLEN_FLAGS(sc->bge_return_ring_cnt, 0));
2313 /* Set random backoff seed for TX */
2314 CSR_WRITE_4(sc, BGE_TX_RANDOM_BACKOFF,
2315 (IF_LLADDR(sc->bge_ifp)[0] + IF_LLADDR(sc->bge_ifp)[1] +
2316 IF_LLADDR(sc->bge_ifp)[2] + IF_LLADDR(sc->bge_ifp)[3] +
2317 IF_LLADDR(sc->bge_ifp)[4] + IF_LLADDR(sc->bge_ifp)[5]) &
2318 BGE_TX_BACKOFF_SEED_MASK);
2320 /* Set inter-packet gap */
2322 if (sc->bge_asicrev == BGE_ASICREV_BCM5720 ||
2323 sc->bge_asicrev == BGE_ASICREV_BCM5762)
2324 val |= CSR_READ_4(sc, BGE_TX_LENGTHS) &
2325 (BGE_TXLEN_JMB_FRM_LEN_MSK | BGE_TXLEN_CNT_DN_VAL_MSK);
2326 CSR_WRITE_4(sc, BGE_TX_LENGTHS, val);
2329 * Specify which ring to use for packets that don't match
2332 CSR_WRITE_4(sc, BGE_RX_RULES_CFG, 0x08);
2335 * Configure number of RX lists. One interrupt distribution
2336 * list, sixteen active lists, one bad frames class.
2338 CSR_WRITE_4(sc, BGE_RXLP_CFG, 0x181);
2340 /* Inialize RX list placement stats mask. */
2341 CSR_WRITE_4(sc, BGE_RXLP_STATS_ENABLE_MASK, 0x007FFFFF);
2342 CSR_WRITE_4(sc, BGE_RXLP_STATS_CTL, 0x1);
2344 /* Disable host coalescing until we get it set up */
2345 CSR_WRITE_4(sc, BGE_HCC_MODE, 0x00000000);
2347 /* Poll to make sure it's shut down. */
2348 for (i = 0; i < BGE_TIMEOUT; i++) {
2350 if (!(CSR_READ_4(sc, BGE_HCC_MODE) & BGE_HCCMODE_ENABLE))
2354 if (i == BGE_TIMEOUT) {
2355 device_printf(sc->bge_dev,
2356 "host coalescing engine failed to idle\n");
2360 /* Set up host coalescing defaults */
2361 CSR_WRITE_4(sc, BGE_HCC_RX_COAL_TICKS, sc->bge_rx_coal_ticks);
2362 CSR_WRITE_4(sc, BGE_HCC_TX_COAL_TICKS, sc->bge_tx_coal_ticks);
2363 CSR_WRITE_4(sc, BGE_HCC_RX_MAX_COAL_BDS, sc->bge_rx_max_coal_bds);
2364 CSR_WRITE_4(sc, BGE_HCC_TX_MAX_COAL_BDS, sc->bge_tx_max_coal_bds);
2365 if (!(BGE_IS_5705_PLUS(sc))) {
2366 CSR_WRITE_4(sc, BGE_HCC_RX_COAL_TICKS_INT, 0);
2367 CSR_WRITE_4(sc, BGE_HCC_TX_COAL_TICKS_INT, 0);
2369 CSR_WRITE_4(sc, BGE_HCC_RX_MAX_COAL_BDS_INT, 1);
2370 CSR_WRITE_4(sc, BGE_HCC_TX_MAX_COAL_BDS_INT, 1);
2372 /* Set up address of statistics block */
2373 if (!(BGE_IS_5705_PLUS(sc))) {
2374 CSR_WRITE_4(sc, BGE_HCC_STATS_ADDR_HI,
2375 BGE_ADDR_HI(sc->bge_ldata.bge_stats_paddr));
2376 CSR_WRITE_4(sc, BGE_HCC_STATS_ADDR_LO,
2377 BGE_ADDR_LO(sc->bge_ldata.bge_stats_paddr));
2378 CSR_WRITE_4(sc, BGE_HCC_STATS_BASEADDR, BGE_STATS_BLOCK);
2379 CSR_WRITE_4(sc, BGE_HCC_STATUSBLK_BASEADDR, BGE_STATUS_BLOCK);
2380 CSR_WRITE_4(sc, BGE_HCC_STATS_TICKS, sc->bge_stat_ticks);
2383 /* Set up address of status block */
2384 CSR_WRITE_4(sc, BGE_HCC_STATUSBLK_ADDR_HI,
2385 BGE_ADDR_HI(sc->bge_ldata.bge_status_block_paddr));
2386 CSR_WRITE_4(sc, BGE_HCC_STATUSBLK_ADDR_LO,
2387 BGE_ADDR_LO(sc->bge_ldata.bge_status_block_paddr));
2389 /* Set up status block size. */
2390 if (sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
2391 sc->bge_chipid != BGE_CHIPID_BCM5700_C0) {
2392 val = BGE_STATBLKSZ_FULL;
2393 bzero(sc->bge_ldata.bge_status_block, BGE_STATUS_BLK_SZ);
2395 val = BGE_STATBLKSZ_32BYTE;
2396 bzero(sc->bge_ldata.bge_status_block, 32);
2398 bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
2399 sc->bge_cdata.bge_status_map,
2400 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
2402 /* Turn on host coalescing state machine */
2403 CSR_WRITE_4(sc, BGE_HCC_MODE, val | BGE_HCCMODE_ENABLE);
2405 /* Turn on RX BD completion state machine and enable attentions */
2406 CSR_WRITE_4(sc, BGE_RBDC_MODE,
2407 BGE_RBDCMODE_ENABLE | BGE_RBDCMODE_ATTN);
2409 /* Turn on RX list placement state machine */
2410 CSR_WRITE_4(sc, BGE_RXLP_MODE, BGE_RXLPMODE_ENABLE);
2412 /* Turn on RX list selector state machine. */
2413 if (!(BGE_IS_5705_PLUS(sc)))
2414 CSR_WRITE_4(sc, BGE_RXLS_MODE, BGE_RXLSMODE_ENABLE);
2416 /* Turn on DMA, clear stats. */
2417 val = BGE_MACMODE_TXDMA_ENB | BGE_MACMODE_RXDMA_ENB |
2418 BGE_MACMODE_RX_STATS_CLEAR | BGE_MACMODE_TX_STATS_CLEAR |
2419 BGE_MACMODE_RX_STATS_ENB | BGE_MACMODE_TX_STATS_ENB |
2420 BGE_MACMODE_FRMHDR_DMA_ENB;
2422 if (sc->bge_flags & BGE_FLAG_TBI)
2423 val |= BGE_PORTMODE_TBI;
2424 else if (sc->bge_flags & BGE_FLAG_MII_SERDES)
2425 val |= BGE_PORTMODE_GMII;
2427 val |= BGE_PORTMODE_MII;
2429 /* Allow APE to send/receive frames. */
2430 if ((sc->bge_mfw_flags & BGE_MFW_ON_APE) != 0)
2431 val |= BGE_MACMODE_APE_RX_EN | BGE_MACMODE_APE_TX_EN;
2433 CSR_WRITE_4(sc, BGE_MAC_MODE, val);
2436 /* Set misc. local control, enable interrupts on attentions */
2437 BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_INTR_ONATTN);
2440 /* Assert GPIO pins for PHY reset */
2441 BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_MISCIO_OUT0 |
2442 BGE_MLC_MISCIO_OUT1 | BGE_MLC_MISCIO_OUT2);
2443 BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_MISCIO_OUTEN0 |
2444 BGE_MLC_MISCIO_OUTEN1 | BGE_MLC_MISCIO_OUTEN2);
2447 /* Turn on DMA completion state machine */
2448 if (!(BGE_IS_5705_PLUS(sc)))
2449 CSR_WRITE_4(sc, BGE_DMAC_MODE, BGE_DMACMODE_ENABLE);
2451 val = BGE_WDMAMODE_ENABLE | BGE_WDMAMODE_ALL_ATTNS;
2453 /* Enable host coalescing bug fix. */
2454 if (BGE_IS_5755_PLUS(sc))
2455 val |= BGE_WDMAMODE_STATUS_TAG_FIX;
2457 /* Request larger DMA burst size to get better performance. */
2458 if (sc->bge_asicrev == BGE_ASICREV_BCM5785)
2459 val |= BGE_WDMAMODE_BURST_ALL_DATA;
2461 /* Turn on write DMA state machine */
2462 CSR_WRITE_4(sc, BGE_WDMA_MODE, val);
2465 /* Turn on read DMA state machine */
2466 val = BGE_RDMAMODE_ENABLE | BGE_RDMAMODE_ALL_ATTNS;
2468 if (sc->bge_asicrev == BGE_ASICREV_BCM5717)
2469 val |= BGE_RDMAMODE_MULT_DMA_RD_DIS;
2471 if (sc->bge_asicrev == BGE_ASICREV_BCM5784 ||
2472 sc->bge_asicrev == BGE_ASICREV_BCM5785 ||
2473 sc->bge_asicrev == BGE_ASICREV_BCM57780)
2474 val |= BGE_RDMAMODE_BD_SBD_CRPT_ATTN |
2475 BGE_RDMAMODE_MBUF_RBD_CRPT_ATTN |
2476 BGE_RDMAMODE_MBUF_SBD_CRPT_ATTN;
2477 if (sc->bge_flags & BGE_FLAG_PCIE)
2478 val |= BGE_RDMAMODE_FIFO_LONG_BURST;
2479 if (sc->bge_flags & (BGE_FLAG_TSO | BGE_FLAG_TSO3)) {
2480 val |= BGE_RDMAMODE_TSO4_ENABLE;
2481 if (sc->bge_flags & BGE_FLAG_TSO3 ||
2482 sc->bge_asicrev == BGE_ASICREV_BCM5785 ||
2483 sc->bge_asicrev == BGE_ASICREV_BCM57780)
2484 val |= BGE_RDMAMODE_TSO6_ENABLE;
2487 if (sc->bge_asicrev == BGE_ASICREV_BCM5720 ||
2488 sc->bge_asicrev == BGE_ASICREV_BCM5762) {
2489 val |= CSR_READ_4(sc, BGE_RDMA_MODE) &
2490 BGE_RDMAMODE_H2BNC_VLAN_DET;
2492 * Allow multiple outstanding read requests from
2493 * non-LSO read DMA engine.
2495 val &= ~BGE_RDMAMODE_MULT_DMA_RD_DIS;
2498 if (sc->bge_asicrev == BGE_ASICREV_BCM5761 ||
2499 sc->bge_asicrev == BGE_ASICREV_BCM5784 ||
2500 sc->bge_asicrev == BGE_ASICREV_BCM5785 ||
2501 sc->bge_asicrev == BGE_ASICREV_BCM57780 ||
2502 BGE_IS_5717_PLUS(sc) || BGE_IS_57765_PLUS(sc)) {
2503 if (sc->bge_asicrev == BGE_ASICREV_BCM5762)
2504 rdmareg = BGE_RDMA_RSRVCTRL_REG2;
2506 rdmareg = BGE_RDMA_RSRVCTRL;
2507 dmactl = CSR_READ_4(sc, rdmareg);
2509 * Adjust tx margin to prevent TX data corruption and
2510 * fix internal FIFO overflow.
2512 if (sc->bge_chipid == BGE_CHIPID_BCM5719_A0 ||
2513 sc->bge_asicrev == BGE_ASICREV_BCM5762) {
2514 dmactl &= ~(BGE_RDMA_RSRVCTRL_FIFO_LWM_MASK |
2515 BGE_RDMA_RSRVCTRL_FIFO_HWM_MASK |
2516 BGE_RDMA_RSRVCTRL_TXMRGN_MASK);
2517 dmactl |= BGE_RDMA_RSRVCTRL_FIFO_LWM_1_5K |
2518 BGE_RDMA_RSRVCTRL_FIFO_HWM_1_5K |
2519 BGE_RDMA_RSRVCTRL_TXMRGN_320B;
2522 * Enable fix for read DMA FIFO overruns.
2523 * The fix is to limit the number of RX BDs
2524 * the hardware would fetch at a fime.
2526 CSR_WRITE_4(sc, rdmareg, dmactl |
2527 BGE_RDMA_RSRVCTRL_FIFO_OFLW_FIX);
2530 if (sc->bge_asicrev == BGE_ASICREV_BCM5719) {
2531 CSR_WRITE_4(sc, BGE_RDMA_LSO_CRPTEN_CTRL,
2532 CSR_READ_4(sc, BGE_RDMA_LSO_CRPTEN_CTRL) |
2533 BGE_RDMA_LSO_CRPTEN_CTRL_BLEN_BD_4K |
2534 BGE_RDMA_LSO_CRPTEN_CTRL_BLEN_LSO_4K);
2535 } else if (sc->bge_asicrev == BGE_ASICREV_BCM5720) {
2537 * Allow 4KB burst length reads for non-LSO frames.
2538 * Enable 512B burst length reads for buffer descriptors.
2540 CSR_WRITE_4(sc, BGE_RDMA_LSO_CRPTEN_CTRL,
2541 CSR_READ_4(sc, BGE_RDMA_LSO_CRPTEN_CTRL) |
2542 BGE_RDMA_LSO_CRPTEN_CTRL_BLEN_BD_512 |
2543 BGE_RDMA_LSO_CRPTEN_CTRL_BLEN_LSO_4K);
2544 } else if (sc->bge_asicrev == BGE_ASICREV_BCM5762) {
2545 CSR_WRITE_4(sc, BGE_RDMA_LSO_CRPTEN_CTRL_REG2,
2546 CSR_READ_4(sc, BGE_RDMA_LSO_CRPTEN_CTRL_REG2) |
2547 BGE_RDMA_LSO_CRPTEN_CTRL_BLEN_BD_4K |
2548 BGE_RDMA_LSO_CRPTEN_CTRL_BLEN_LSO_4K);
2551 CSR_WRITE_4(sc, BGE_RDMA_MODE, val);
2554 if (sc->bge_flags & BGE_FLAG_RDMA_BUG) {
2555 for (i = 0; i < BGE_NUM_RDMA_CHANNELS / 2; i++) {
2556 val = CSR_READ_4(sc, BGE_RDMA_LENGTH + i * 4);
2557 if ((val & 0xFFFF) > BGE_FRAMELEN)
2559 if (((val >> 16) & 0xFFFF) > BGE_FRAMELEN)
2562 if (i != BGE_NUM_RDMA_CHANNELS / 2) {
2563 val = CSR_READ_4(sc, BGE_RDMA_LSO_CRPTEN_CTRL);
2564 if (sc->bge_asicrev == BGE_ASICREV_BCM5719)
2565 val |= BGE_RDMA_TX_LENGTH_WA_5719;
2567 val |= BGE_RDMA_TX_LENGTH_WA_5720;
2568 CSR_WRITE_4(sc, BGE_RDMA_LSO_CRPTEN_CTRL, val);
2572 /* Turn on RX data completion state machine */
2573 CSR_WRITE_4(sc, BGE_RDC_MODE, BGE_RDCMODE_ENABLE);
2575 /* Turn on RX BD initiator state machine */
2576 CSR_WRITE_4(sc, BGE_RBDI_MODE, BGE_RBDIMODE_ENABLE);
2578 /* Turn on RX data and RX BD initiator state machine */
2579 CSR_WRITE_4(sc, BGE_RDBDI_MODE, BGE_RDBDIMODE_ENABLE);
2581 /* Turn on Mbuf cluster free state machine */
2582 if (!(BGE_IS_5705_PLUS(sc)))
2583 CSR_WRITE_4(sc, BGE_MBCF_MODE, BGE_MBCFMODE_ENABLE);
2585 /* Turn on send BD completion state machine */
2586 CSR_WRITE_4(sc, BGE_SBDC_MODE, BGE_SBDCMODE_ENABLE);
2588 /* Turn on send data completion state machine */
2589 val = BGE_SDCMODE_ENABLE;
2590 if (sc->bge_asicrev == BGE_ASICREV_BCM5761)
2591 val |= BGE_SDCMODE_CDELAY;
2592 CSR_WRITE_4(sc, BGE_SDC_MODE, val);
2594 /* Turn on send data initiator state machine */
2595 if (sc->bge_flags & (BGE_FLAG_TSO | BGE_FLAG_TSO3))
2596 CSR_WRITE_4(sc, BGE_SDI_MODE, BGE_SDIMODE_ENABLE |
2597 BGE_SDIMODE_HW_LSO_PRE_DMA);
2599 CSR_WRITE_4(sc, BGE_SDI_MODE, BGE_SDIMODE_ENABLE);
2601 /* Turn on send BD initiator state machine */
2602 CSR_WRITE_4(sc, BGE_SBDI_MODE, BGE_SBDIMODE_ENABLE);
2604 /* Turn on send BD selector state machine */
2605 CSR_WRITE_4(sc, BGE_SRS_MODE, BGE_SRSMODE_ENABLE);
2607 CSR_WRITE_4(sc, BGE_SDI_STATS_ENABLE_MASK, 0x007FFFFF);
2608 CSR_WRITE_4(sc, BGE_SDI_STATS_CTL,
2609 BGE_SDISTATSCTL_ENABLE | BGE_SDISTATSCTL_FASTER);
2611 /* ack/clear link change events */
2612 CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED |
2613 BGE_MACSTAT_CFG_CHANGED | BGE_MACSTAT_MI_COMPLETE |
2614 BGE_MACSTAT_LINK_CHANGED);
2615 CSR_WRITE_4(sc, BGE_MI_STS, 0);
2618 * Enable attention when the link has changed state for
2619 * devices that use auto polling.
2621 if (sc->bge_flags & BGE_FLAG_TBI) {
2622 CSR_WRITE_4(sc, BGE_MI_STS, BGE_MISTS_LINK);
2624 if (sc->bge_mi_mode & BGE_MIMODE_AUTOPOLL) {
2625 CSR_WRITE_4(sc, BGE_MI_MODE, sc->bge_mi_mode);
2628 if (sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
2629 sc->bge_chipid != BGE_CHIPID_BCM5700_B2)
2630 CSR_WRITE_4(sc, BGE_MAC_EVT_ENB,
2631 BGE_EVTENB_MI_INTERRUPT);
2635 * Clear any pending link state attention.
2636 * Otherwise some link state change events may be lost until attention
2637 * is cleared by bge_intr() -> bge_link_upd() sequence.
2638 * It's not necessary on newer BCM chips - perhaps enabling link
2639 * state change attentions implies clearing pending attention.
2641 CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED |
2642 BGE_MACSTAT_CFG_CHANGED | BGE_MACSTAT_MI_COMPLETE |
2643 BGE_MACSTAT_LINK_CHANGED);
2645 /* Enable link state change attentions. */
2646 BGE_SETBIT(sc, BGE_MAC_EVT_ENB, BGE_EVTENB_LINK_CHANGED);
2651 static const struct bge_revision *
2652 bge_lookup_rev(uint32_t chipid)
2654 const struct bge_revision *br;
2656 for (br = bge_revisions; br->br_name != NULL; br++) {
2657 if (br->br_chipid == chipid)
2661 for (br = bge_majorrevs; br->br_name != NULL; br++) {
2662 if (br->br_chipid == BGE_ASICREV(chipid))
2669 static const struct bge_vendor *
2670 bge_lookup_vendor(uint16_t vid)
2672 const struct bge_vendor *v;
2674 for (v = bge_vendors; v->v_name != NULL; v++)
2682 bge_chipid(device_t dev)
2686 id = pci_read_config(dev, BGE_PCI_MISC_CTL, 4) >>
2687 BGE_PCIMISCCTL_ASICREV_SHIFT;
2688 if (BGE_ASICREV(id) == BGE_ASICREV_USE_PRODID_REG) {
2690 * Find the ASCI revision. Different chips use different
2693 switch (pci_get_device(dev)) {
2694 case BCOM_DEVICEID_BCM5717C:
2695 /* 5717 C0 seems to belong to 5720 line. */
2696 id = BGE_CHIPID_BCM5720_A0;
2698 case BCOM_DEVICEID_BCM5717:
2699 case BCOM_DEVICEID_BCM5718:
2700 case BCOM_DEVICEID_BCM5719:
2701 case BCOM_DEVICEID_BCM5720:
2702 case BCOM_DEVICEID_BCM5725:
2703 case BCOM_DEVICEID_BCM5727:
2704 case BCOM_DEVICEID_BCM5762:
2705 case BCOM_DEVICEID_BCM57764:
2706 case BCOM_DEVICEID_BCM57767:
2707 case BCOM_DEVICEID_BCM57787:
2708 id = pci_read_config(dev,
2709 BGE_PCI_GEN2_PRODID_ASICREV, 4);
2711 case BCOM_DEVICEID_BCM57761:
2712 case BCOM_DEVICEID_BCM57762:
2713 case BCOM_DEVICEID_BCM57765:
2714 case BCOM_DEVICEID_BCM57766:
2715 case BCOM_DEVICEID_BCM57781:
2716 case BCOM_DEVICEID_BCM57782:
2717 case BCOM_DEVICEID_BCM57785:
2718 case BCOM_DEVICEID_BCM57786:
2719 case BCOM_DEVICEID_BCM57791:
2720 case BCOM_DEVICEID_BCM57795:
2721 id = pci_read_config(dev,
2722 BGE_PCI_GEN15_PRODID_ASICREV, 4);
2725 id = pci_read_config(dev, BGE_PCI_PRODID_ASICREV, 4);
2732 * Probe for a Broadcom chip. Check the PCI vendor and device IDs
2733 * against our list and return its name if we find a match.
2735 * Note that since the Broadcom controller contains VPD support, we
2736 * try to get the device name string from the controller itself instead
2737 * of the compiled-in string. It guarantees we'll always announce the
2738 * right product name. We fall back to the compiled-in string when
2739 * VPD is unavailable or corrupt.
2742 bge_probe(device_t dev)
2746 const struct bge_revision *br;
2748 struct bge_softc *sc;
2749 const struct bge_type *t = bge_devs;
2750 const struct bge_vendor *v;
2754 sc = device_get_softc(dev);
2756 vid = pci_get_vendor(dev);
2757 did = pci_get_device(dev);
2758 while(t->bge_vid != 0) {
2759 if ((vid == t->bge_vid) && (did == t->bge_did)) {
2760 id = bge_chipid(dev);
2761 br = bge_lookup_rev(id);
2762 if (bge_has_eaddr(sc) &&
2763 pci_get_vpd_ident(dev, &pname) == 0)
2764 snprintf(model, sizeof(model), "%s", pname);
2766 v = bge_lookup_vendor(vid);
2767 snprintf(model, sizeof(model), "%s %s",
2768 v != NULL ? v->v_name : "Unknown",
2769 br != NULL ? br->br_name :
2770 "NetXtreme/NetLink Ethernet Controller");
2772 snprintf(buf, sizeof(buf), "%s, %sASIC rev. %#08x",
2773 model, br != NULL ? "" : "unknown ", id);
2774 device_set_desc_copy(dev, buf);
2775 return (BUS_PROBE_DEFAULT);
2784 bge_dma_free(struct bge_softc *sc)
2788 /* Destroy DMA maps for RX buffers. */
2789 for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
2790 if (sc->bge_cdata.bge_rx_std_dmamap[i])
2791 bus_dmamap_destroy(sc->bge_cdata.bge_rx_mtag,
2792 sc->bge_cdata.bge_rx_std_dmamap[i]);
2794 if (sc->bge_cdata.bge_rx_std_sparemap)
2795 bus_dmamap_destroy(sc->bge_cdata.bge_rx_mtag,
2796 sc->bge_cdata.bge_rx_std_sparemap);
2798 /* Destroy DMA maps for jumbo RX buffers. */
2799 for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
2800 if (sc->bge_cdata.bge_rx_jumbo_dmamap[i])
2801 bus_dmamap_destroy(sc->bge_cdata.bge_mtag_jumbo,
2802 sc->bge_cdata.bge_rx_jumbo_dmamap[i]);
2804 if (sc->bge_cdata.bge_rx_jumbo_sparemap)
2805 bus_dmamap_destroy(sc->bge_cdata.bge_mtag_jumbo,
2806 sc->bge_cdata.bge_rx_jumbo_sparemap);
2808 /* Destroy DMA maps for TX buffers. */
2809 for (i = 0; i < BGE_TX_RING_CNT; i++) {
2810 if (sc->bge_cdata.bge_tx_dmamap[i])
2811 bus_dmamap_destroy(sc->bge_cdata.bge_tx_mtag,
2812 sc->bge_cdata.bge_tx_dmamap[i]);
2815 if (sc->bge_cdata.bge_rx_mtag)
2816 bus_dma_tag_destroy(sc->bge_cdata.bge_rx_mtag);
2817 if (sc->bge_cdata.bge_mtag_jumbo)
2818 bus_dma_tag_destroy(sc->bge_cdata.bge_mtag_jumbo);
2819 if (sc->bge_cdata.bge_tx_mtag)
2820 bus_dma_tag_destroy(sc->bge_cdata.bge_tx_mtag);
2822 /* Destroy standard RX ring. */
2823 if (sc->bge_cdata.bge_rx_std_ring_map)
2824 bus_dmamap_unload(sc->bge_cdata.bge_rx_std_ring_tag,
2825 sc->bge_cdata.bge_rx_std_ring_map);
2826 if (sc->bge_cdata.bge_rx_std_ring_map && sc->bge_ldata.bge_rx_std_ring)
2827 bus_dmamem_free(sc->bge_cdata.bge_rx_std_ring_tag,
2828 sc->bge_ldata.bge_rx_std_ring,
2829 sc->bge_cdata.bge_rx_std_ring_map);
2831 if (sc->bge_cdata.bge_rx_std_ring_tag)
2832 bus_dma_tag_destroy(sc->bge_cdata.bge_rx_std_ring_tag);
2834 /* Destroy jumbo RX ring. */
2835 if (sc->bge_cdata.bge_rx_jumbo_ring_map)
2836 bus_dmamap_unload(sc->bge_cdata.bge_rx_jumbo_ring_tag,
2837 sc->bge_cdata.bge_rx_jumbo_ring_map);
2839 if (sc->bge_cdata.bge_rx_jumbo_ring_map &&
2840 sc->bge_ldata.bge_rx_jumbo_ring)
2841 bus_dmamem_free(sc->bge_cdata.bge_rx_jumbo_ring_tag,
2842 sc->bge_ldata.bge_rx_jumbo_ring,
2843 sc->bge_cdata.bge_rx_jumbo_ring_map);
2845 if (sc->bge_cdata.bge_rx_jumbo_ring_tag)
2846 bus_dma_tag_destroy(sc->bge_cdata.bge_rx_jumbo_ring_tag);
2848 /* Destroy RX return ring. */
2849 if (sc->bge_cdata.bge_rx_return_ring_map)
2850 bus_dmamap_unload(sc->bge_cdata.bge_rx_return_ring_tag,
2851 sc->bge_cdata.bge_rx_return_ring_map);
2853 if (sc->bge_cdata.bge_rx_return_ring_map &&
2854 sc->bge_ldata.bge_rx_return_ring)
2855 bus_dmamem_free(sc->bge_cdata.bge_rx_return_ring_tag,
2856 sc->bge_ldata.bge_rx_return_ring,
2857 sc->bge_cdata.bge_rx_return_ring_map);
2859 if (sc->bge_cdata.bge_rx_return_ring_tag)
2860 bus_dma_tag_destroy(sc->bge_cdata.bge_rx_return_ring_tag);
2862 /* Destroy TX ring. */
2863 if (sc->bge_cdata.bge_tx_ring_map)
2864 bus_dmamap_unload(sc->bge_cdata.bge_tx_ring_tag,
2865 sc->bge_cdata.bge_tx_ring_map);
2867 if (sc->bge_cdata.bge_tx_ring_map && sc->bge_ldata.bge_tx_ring)
2868 bus_dmamem_free(sc->bge_cdata.bge_tx_ring_tag,
2869 sc->bge_ldata.bge_tx_ring,
2870 sc->bge_cdata.bge_tx_ring_map);
2872 if (sc->bge_cdata.bge_tx_ring_tag)
2873 bus_dma_tag_destroy(sc->bge_cdata.bge_tx_ring_tag);
2875 /* Destroy status block. */
2876 if (sc->bge_cdata.bge_status_map)
2877 bus_dmamap_unload(sc->bge_cdata.bge_status_tag,
2878 sc->bge_cdata.bge_status_map);
2880 if (sc->bge_cdata.bge_status_map && sc->bge_ldata.bge_status_block)
2881 bus_dmamem_free(sc->bge_cdata.bge_status_tag,
2882 sc->bge_ldata.bge_status_block,
2883 sc->bge_cdata.bge_status_map);
2885 if (sc->bge_cdata.bge_status_tag)
2886 bus_dma_tag_destroy(sc->bge_cdata.bge_status_tag);
2888 /* Destroy statistics block. */
2889 if (sc->bge_cdata.bge_stats_map)
2890 bus_dmamap_unload(sc->bge_cdata.bge_stats_tag,
2891 sc->bge_cdata.bge_stats_map);
2893 if (sc->bge_cdata.bge_stats_map && sc->bge_ldata.bge_stats)
2894 bus_dmamem_free(sc->bge_cdata.bge_stats_tag,
2895 sc->bge_ldata.bge_stats,
2896 sc->bge_cdata.bge_stats_map);
2898 if (sc->bge_cdata.bge_stats_tag)
2899 bus_dma_tag_destroy(sc->bge_cdata.bge_stats_tag);
2901 if (sc->bge_cdata.bge_buffer_tag)
2902 bus_dma_tag_destroy(sc->bge_cdata.bge_buffer_tag);
2904 /* Destroy the parent tag. */
2905 if (sc->bge_cdata.bge_parent_tag)
2906 bus_dma_tag_destroy(sc->bge_cdata.bge_parent_tag);
2910 bge_dma_ring_alloc(struct bge_softc *sc, bus_size_t alignment,
2911 bus_size_t maxsize, bus_dma_tag_t *tag, uint8_t **ring, bus_dmamap_t *map,
2912 bus_addr_t *paddr, const char *msg)
2914 struct bge_dmamap_arg ctx;
2917 error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
2918 alignment, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
2919 NULL, maxsize, 1, maxsize, 0, NULL, NULL, tag);
2921 device_printf(sc->bge_dev,
2922 "could not create %s dma tag\n", msg);
2925 /* Allocate DMA'able memory for ring. */
2926 error = bus_dmamem_alloc(*tag, (void **)ring,
2927 BUS_DMA_NOWAIT | BUS_DMA_ZERO | BUS_DMA_COHERENT, map);
2929 device_printf(sc->bge_dev,
2930 "could not allocate DMA'able memory for %s\n", msg);
2933 /* Load the address of the ring. */
2934 ctx.bge_busaddr = 0;
2935 error = bus_dmamap_load(*tag, *map, *ring, maxsize, bge_dma_map_addr,
2936 &ctx, BUS_DMA_NOWAIT);
2938 device_printf(sc->bge_dev,
2939 "could not load DMA'able memory for %s\n", msg);
2942 *paddr = ctx.bge_busaddr;
2947 bge_dma_alloc(struct bge_softc *sc)
2950 bus_size_t rxmaxsegsz, sbsz, txsegsz, txmaxsegsz;
2953 lowaddr = BUS_SPACE_MAXADDR;
2954 if ((sc->bge_flags & BGE_FLAG_40BIT_BUG) != 0)
2955 lowaddr = BGE_DMA_MAXADDR;
2957 * Allocate the parent bus DMA tag appropriate for PCI.
2959 error = bus_dma_tag_create(bus_get_dma_tag(sc->bge_dev),
2960 1, 0, lowaddr, BUS_SPACE_MAXADDR, NULL,
2961 NULL, BUS_SPACE_MAXSIZE_32BIT, 0, BUS_SPACE_MAXSIZE_32BIT,
2962 0, NULL, NULL, &sc->bge_cdata.bge_parent_tag);
2964 device_printf(sc->bge_dev,
2965 "could not allocate parent dma tag\n");
2969 /* Create tag for standard RX ring. */
2970 error = bge_dma_ring_alloc(sc, PAGE_SIZE, BGE_STD_RX_RING_SZ,
2971 &sc->bge_cdata.bge_rx_std_ring_tag,
2972 (uint8_t **)&sc->bge_ldata.bge_rx_std_ring,
2973 &sc->bge_cdata.bge_rx_std_ring_map,
2974 &sc->bge_ldata.bge_rx_std_ring_paddr, "RX ring");
2978 /* Create tag for RX return ring. */
2979 error = bge_dma_ring_alloc(sc, PAGE_SIZE, BGE_RX_RTN_RING_SZ(sc),
2980 &sc->bge_cdata.bge_rx_return_ring_tag,
2981 (uint8_t **)&sc->bge_ldata.bge_rx_return_ring,
2982 &sc->bge_cdata.bge_rx_return_ring_map,
2983 &sc->bge_ldata.bge_rx_return_ring_paddr, "RX return ring");
2987 /* Create tag for TX ring. */
2988 error = bge_dma_ring_alloc(sc, PAGE_SIZE, BGE_TX_RING_SZ,
2989 &sc->bge_cdata.bge_tx_ring_tag,
2990 (uint8_t **)&sc->bge_ldata.bge_tx_ring,
2991 &sc->bge_cdata.bge_tx_ring_map,
2992 &sc->bge_ldata.bge_tx_ring_paddr, "TX ring");
2997 * Create tag for status block.
2998 * Because we only use single Tx/Rx/Rx return ring, use
2999 * minimum status block size except BCM5700 AX/BX which
3000 * seems to want to see full status block size regardless
3001 * of configured number of ring.
3003 if (sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
3004 sc->bge_chipid != BGE_CHIPID_BCM5700_C0)
3005 sbsz = BGE_STATUS_BLK_SZ;
3008 error = bge_dma_ring_alloc(sc, PAGE_SIZE, sbsz,
3009 &sc->bge_cdata.bge_status_tag,
3010 (uint8_t **)&sc->bge_ldata.bge_status_block,
3011 &sc->bge_cdata.bge_status_map,
3012 &sc->bge_ldata.bge_status_block_paddr, "status block");
3016 /* Create tag for statistics block. */
3017 error = bge_dma_ring_alloc(sc, PAGE_SIZE, BGE_STATS_SZ,
3018 &sc->bge_cdata.bge_stats_tag,
3019 (uint8_t **)&sc->bge_ldata.bge_stats,
3020 &sc->bge_cdata.bge_stats_map,
3021 &sc->bge_ldata.bge_stats_paddr, "statistics block");
3025 /* Create tag for jumbo RX ring. */
3026 if (BGE_IS_JUMBO_CAPABLE(sc)) {
3027 error = bge_dma_ring_alloc(sc, PAGE_SIZE, BGE_JUMBO_RX_RING_SZ,
3028 &sc->bge_cdata.bge_rx_jumbo_ring_tag,
3029 (uint8_t **)&sc->bge_ldata.bge_rx_jumbo_ring,
3030 &sc->bge_cdata.bge_rx_jumbo_ring_map,
3031 &sc->bge_ldata.bge_rx_jumbo_ring_paddr, "jumbo RX ring");
3036 /* Create parent tag for buffers. */
3037 if ((sc->bge_flags & BGE_FLAG_4G_BNDRY_BUG) != 0) {
3040 * watchdog timeout issue was observed on BCM5704 which
3041 * lives behind PCI-X bridge(e.g AMD 8131 PCI-X bridge).
3042 * Both limiting DMA address space to 32bits and flushing
3043 * mailbox write seem to address the issue.
3045 if (sc->bge_pcixcap != 0)
3046 lowaddr = BUS_SPACE_MAXADDR_32BIT;
3048 error = bus_dma_tag_create(bus_get_dma_tag(sc->bge_dev), 1, 0, lowaddr,
3049 BUS_SPACE_MAXADDR, NULL, NULL, BUS_SPACE_MAXSIZE_32BIT, 0,
3050 BUS_SPACE_MAXSIZE_32BIT, 0, NULL, NULL,
3051 &sc->bge_cdata.bge_buffer_tag);
3053 device_printf(sc->bge_dev,
3054 "could not allocate buffer dma tag\n");
3057 /* Create tag for Tx mbufs. */
3058 if (sc->bge_flags & (BGE_FLAG_TSO | BGE_FLAG_TSO3)) {
3059 txsegsz = BGE_TSOSEG_SZ;
3060 txmaxsegsz = 65535 + sizeof(struct ether_vlan_header);
3063 txmaxsegsz = MCLBYTES * BGE_NSEG_NEW;
3065 error = bus_dma_tag_create(sc->bge_cdata.bge_buffer_tag, 1,
3066 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL,
3067 txmaxsegsz, BGE_NSEG_NEW, txsegsz, 0, NULL, NULL,
3068 &sc->bge_cdata.bge_tx_mtag);
3071 device_printf(sc->bge_dev, "could not allocate TX dma tag\n");
3075 /* Create tag for Rx mbufs. */
3076 if (sc->bge_flags & BGE_FLAG_JUMBO_STD)
3077 rxmaxsegsz = MJUM9BYTES;
3079 rxmaxsegsz = MCLBYTES;
3080 error = bus_dma_tag_create(sc->bge_cdata.bge_buffer_tag, 1, 0,
3081 BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL, rxmaxsegsz, 1,
3082 rxmaxsegsz, 0, NULL, NULL, &sc->bge_cdata.bge_rx_mtag);
3085 device_printf(sc->bge_dev, "could not allocate RX dma tag\n");
3089 /* Create DMA maps for RX buffers. */
3090 error = bus_dmamap_create(sc->bge_cdata.bge_rx_mtag, 0,
3091 &sc->bge_cdata.bge_rx_std_sparemap);
3093 device_printf(sc->bge_dev,
3094 "can't create spare DMA map for RX\n");
3097 for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
3098 error = bus_dmamap_create(sc->bge_cdata.bge_rx_mtag, 0,
3099 &sc->bge_cdata.bge_rx_std_dmamap[i]);
3101 device_printf(sc->bge_dev,
3102 "can't create DMA map for RX\n");
3107 /* Create DMA maps for TX buffers. */
3108 for (i = 0; i < BGE_TX_RING_CNT; i++) {
3109 error = bus_dmamap_create(sc->bge_cdata.bge_tx_mtag, 0,
3110 &sc->bge_cdata.bge_tx_dmamap[i]);
3112 device_printf(sc->bge_dev,
3113 "can't create DMA map for TX\n");
3118 /* Create tags for jumbo RX buffers. */
3119 if (BGE_IS_JUMBO_CAPABLE(sc)) {
3120 error = bus_dma_tag_create(sc->bge_cdata.bge_buffer_tag,
3121 1, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
3122 NULL, MJUM9BYTES, BGE_NSEG_JUMBO, PAGE_SIZE,
3123 0, NULL, NULL, &sc->bge_cdata.bge_mtag_jumbo);
3125 device_printf(sc->bge_dev,
3126 "could not allocate jumbo dma tag\n");
3129 /* Create DMA maps for jumbo RX buffers. */
3130 error = bus_dmamap_create(sc->bge_cdata.bge_mtag_jumbo,
3131 0, &sc->bge_cdata.bge_rx_jumbo_sparemap);
3133 device_printf(sc->bge_dev,
3134 "can't create spare DMA map for jumbo RX\n");
3137 for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
3138 error = bus_dmamap_create(sc->bge_cdata.bge_mtag_jumbo,
3139 0, &sc->bge_cdata.bge_rx_jumbo_dmamap[i]);
3141 device_printf(sc->bge_dev,
3142 "can't create DMA map for jumbo RX\n");
3152 * Return true if this device has more than one port.
3155 bge_has_multiple_ports(struct bge_softc *sc)
3157 device_t dev = sc->bge_dev;
3158 u_int b, d, f, fscan, s;
3160 d = pci_get_domain(dev);
3161 b = pci_get_bus(dev);
3162 s = pci_get_slot(dev);
3163 f = pci_get_function(dev);
3164 for (fscan = 0; fscan <= PCI_FUNCMAX; fscan++)
3165 if (fscan != f && pci_find_dbsf(d, b, s, fscan) != NULL)
3171 * Return true if MSI can be used with this device.
3174 bge_can_use_msi(struct bge_softc *sc)
3176 int can_use_msi = 0;
3178 if (sc->bge_msi == 0)
3181 /* Disable MSI for polling(4). */
3182 #ifdef DEVICE_POLLING
3185 switch (sc->bge_asicrev) {
3186 case BGE_ASICREV_BCM5714_A0:
3187 case BGE_ASICREV_BCM5714:
3189 * Apparently, MSI doesn't work when these chips are
3190 * configured in single-port mode.
3192 if (bge_has_multiple_ports(sc))
3195 case BGE_ASICREV_BCM5750:
3196 if (sc->bge_chiprev != BGE_CHIPREV_5750_AX &&
3197 sc->bge_chiprev != BGE_CHIPREV_5750_BX)
3201 if (BGE_IS_575X_PLUS(sc))
3204 return (can_use_msi);
3208 bge_mbox_reorder(struct bge_softc *sc)
3210 /* Lists of PCI bridges that are known to reorder mailbox writes. */
3211 static const struct mbox_reorder {
3212 const uint16_t vendor;
3213 const uint16_t device;
3215 } mbox_reorder_lists[] = {
3216 { 0x1022, 0x7450, "AMD-8131 PCI-X Bridge" },
3218 devclass_t pci, pcib;
3222 pci = devclass_find("pci");
3223 pcib = devclass_find("pcib");
3225 bus = device_get_parent(dev);
3227 dev = device_get_parent(bus);
3228 bus = device_get_parent(dev);
3229 if (device_get_devclass(dev) != pcib)
3231 for (i = 0; i < nitems(mbox_reorder_lists); i++) {
3232 if (pci_get_vendor(dev) ==
3233 mbox_reorder_lists[i].vendor &&
3234 pci_get_device(dev) ==
3235 mbox_reorder_lists[i].device) {
3236 device_printf(sc->bge_dev,
3237 "enabling MBOX workaround for %s\n",
3238 mbox_reorder_lists[i].desc);
3242 if (device_get_devclass(bus) != pci)
3249 bge_devinfo(struct bge_softc *sc)
3253 device_printf(sc->bge_dev,
3254 "CHIP ID 0x%08x; ASIC REV 0x%02x; CHIP REV 0x%02x; ",
3255 sc->bge_chipid, sc->bge_asicrev, sc->bge_chiprev);
3256 if (sc->bge_flags & BGE_FLAG_PCIE)
3258 else if (sc->bge_flags & BGE_FLAG_PCIX) {
3260 cfg = CSR_READ_4(sc, BGE_MISC_CFG) & BGE_MISCCFG_BOARD_ID_MASK;
3261 if (cfg == BGE_MISCCFG_BOARD_ID_5704CIOBE)
3264 clk = CSR_READ_4(sc, BGE_PCI_CLKCTL) & 0x1F;
3283 printf("%u MHz\n", clk);
3285 if (sc->bge_pcixcap != 0)
3286 printf("PCI on PCI-X ");
3289 cfg = pci_read_config(sc->bge_dev, BGE_PCI_PCISTATE, 4);
3290 if (cfg & BGE_PCISTATE_PCI_BUSSPEED)
3294 if (cfg & BGE_PCISTATE_32BIT_BUS)
3295 printf("%u MHz; 32bit\n", clk);
3297 printf("%u MHz; 64bit\n", clk);
3302 bge_attach(device_t dev)
3305 struct bge_softc *sc;
3306 uint32_t hwcfg = 0, misccfg, pcistate;
3307 u_char eaddr[ETHER_ADDR_LEN];
3308 int capmask, error, reg, rid, trys;
3310 sc = device_get_softc(dev);
3313 BGE_LOCK_INIT(sc, device_get_nameunit(dev));
3314 TASK_INIT(&sc->bge_intr_task, 0, bge_intr_task, sc);
3315 callout_init_mtx(&sc->bge_stat_ch, &sc->bge_mtx, 0);
3317 pci_enable_busmaster(dev);
3320 * Allocate control/status registers.
3323 sc->bge_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
3326 if (sc->bge_res == NULL) {
3327 device_printf (sc->bge_dev, "couldn't map BAR0 memory\n");
3332 /* Save various chip information. */
3333 sc->bge_func_addr = pci_get_function(dev);
3334 sc->bge_chipid = bge_chipid(dev);
3335 sc->bge_asicrev = BGE_ASICREV(sc->bge_chipid);
3336 sc->bge_chiprev = BGE_CHIPREV(sc->bge_chipid);
3338 /* Set default PHY address. */
3339 sc->bge_phy_addr = 1;
3341 * PHY address mapping for various devices.
3343 * | F0 Cu | F0 Sr | F1 Cu | F1 Sr |
3344 * ---------+-------+-------+-------+-------+
3345 * BCM57XX | 1 | X | X | X |
3346 * BCM5704 | 1 | X | 1 | X |
3347 * BCM5717 | 1 | 8 | 2 | 9 |
3348 * BCM5719 | 1 | 8 | 2 | 9 |
3349 * BCM5720 | 1 | 8 | 2 | 9 |
3351 * | F2 Cu | F2 Sr | F3 Cu | F3 Sr |
3352 * ---------+-------+-------+-------+-------+
3353 * BCM57XX | X | X | X | X |
3354 * BCM5704 | X | X | X | X |
3355 * BCM5717 | X | X | X | X |
3356 * BCM5719 | 3 | 10 | 4 | 11 |
3357 * BCM5720 | X | X | X | X |
3359 * Other addresses may respond but they are not
3360 * IEEE compliant PHYs and should be ignored.
3362 if (sc->bge_asicrev == BGE_ASICREV_BCM5717 ||
3363 sc->bge_asicrev == BGE_ASICREV_BCM5719 ||
3364 sc->bge_asicrev == BGE_ASICREV_BCM5720) {
3365 if (sc->bge_chipid != BGE_CHIPID_BCM5717_A0) {
3366 if (CSR_READ_4(sc, BGE_SGDIG_STS) &
3367 BGE_SGDIGSTS_IS_SERDES)
3368 sc->bge_phy_addr = sc->bge_func_addr + 8;
3370 sc->bge_phy_addr = sc->bge_func_addr + 1;
3372 if (CSR_READ_4(sc, BGE_CPMU_PHY_STRAP) &
3373 BGE_CPMU_PHY_STRAP_IS_SERDES)
3374 sc->bge_phy_addr = sc->bge_func_addr + 8;
3376 sc->bge_phy_addr = sc->bge_func_addr + 1;
3380 if (bge_has_eaddr(sc))
3381 sc->bge_flags |= BGE_FLAG_EADDR;
3383 /* Save chipset family. */
3384 switch (sc->bge_asicrev) {
3385 case BGE_ASICREV_BCM5762:
3386 case BGE_ASICREV_BCM57765:
3387 case BGE_ASICREV_BCM57766:
3388 sc->bge_flags |= BGE_FLAG_57765_PLUS;
3390 case BGE_ASICREV_BCM5717:
3391 case BGE_ASICREV_BCM5719:
3392 case BGE_ASICREV_BCM5720:
3393 sc->bge_flags |= BGE_FLAG_5717_PLUS | BGE_FLAG_5755_PLUS |
3394 BGE_FLAG_575X_PLUS | BGE_FLAG_5705_PLUS | BGE_FLAG_JUMBO |
3395 BGE_FLAG_JUMBO_FRAME;
3396 if (sc->bge_asicrev == BGE_ASICREV_BCM5719 ||
3397 sc->bge_asicrev == BGE_ASICREV_BCM5720) {
3399 * Enable work around for DMA engine miscalculation
3400 * of TXMBUF available space.
3402 sc->bge_flags |= BGE_FLAG_RDMA_BUG;
3403 if (sc->bge_asicrev == BGE_ASICREV_BCM5719 &&
3404 sc->bge_chipid == BGE_CHIPID_BCM5719_A0) {
3405 /* Jumbo frame on BCM5719 A0 does not work. */
3406 sc->bge_flags &= ~BGE_FLAG_JUMBO;
3410 case BGE_ASICREV_BCM5755:
3411 case BGE_ASICREV_BCM5761:
3412 case BGE_ASICREV_BCM5784:
3413 case BGE_ASICREV_BCM5785:
3414 case BGE_ASICREV_BCM5787:
3415 case BGE_ASICREV_BCM57780:
3416 sc->bge_flags |= BGE_FLAG_5755_PLUS | BGE_FLAG_575X_PLUS |
3419 case BGE_ASICREV_BCM5700:
3420 case BGE_ASICREV_BCM5701:
3421 case BGE_ASICREV_BCM5703:
3422 case BGE_ASICREV_BCM5704:
3423 sc->bge_flags |= BGE_FLAG_5700_FAMILY | BGE_FLAG_JUMBO;
3425 case BGE_ASICREV_BCM5714_A0:
3426 case BGE_ASICREV_BCM5780:
3427 case BGE_ASICREV_BCM5714:
3428 sc->bge_flags |= BGE_FLAG_5714_FAMILY | BGE_FLAG_JUMBO_STD;
3430 case BGE_ASICREV_BCM5750:
3431 case BGE_ASICREV_BCM5752:
3432 case BGE_ASICREV_BCM5906:
3433 sc->bge_flags |= BGE_FLAG_575X_PLUS;
3435 case BGE_ASICREV_BCM5705:
3436 sc->bge_flags |= BGE_FLAG_5705_PLUS;
3440 /* Identify chips with APE processor. */
3441 switch (sc->bge_asicrev) {
3442 case BGE_ASICREV_BCM5717:
3443 case BGE_ASICREV_BCM5719:
3444 case BGE_ASICREV_BCM5720:
3445 case BGE_ASICREV_BCM5761:
3446 case BGE_ASICREV_BCM5762:
3447 sc->bge_flags |= BGE_FLAG_APE;
3451 /* Chips with APE need BAR2 access for APE registers/memory. */
3452 if ((sc->bge_flags & BGE_FLAG_APE) != 0) {
3454 sc->bge_res2 = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
3456 if (sc->bge_res2 == NULL) {
3457 device_printf (sc->bge_dev,
3458 "couldn't map BAR2 memory\n");
3463 /* Enable APE register/memory access by host driver. */
3464 pcistate = pci_read_config(dev, BGE_PCI_PCISTATE, 4);
3465 pcistate |= BGE_PCISTATE_ALLOW_APE_CTLSPC_WR |
3466 BGE_PCISTATE_ALLOW_APE_SHMEM_WR |
3467 BGE_PCISTATE_ALLOW_APE_PSPACE_WR;
3468 pci_write_config(dev, BGE_PCI_PCISTATE, pcistate, 4);
3470 bge_ape_lock_init(sc);
3471 bge_ape_read_fw_ver(sc);
3474 /* Add SYSCTLs, requires the chipset family to be set. */
3475 bge_add_sysctls(sc);
3477 /* Identify the chips that use an CPMU. */
3478 if (BGE_IS_5717_PLUS(sc) ||
3479 sc->bge_asicrev == BGE_ASICREV_BCM5784 ||
3480 sc->bge_asicrev == BGE_ASICREV_BCM5761 ||
3481 sc->bge_asicrev == BGE_ASICREV_BCM5785 ||
3482 sc->bge_asicrev == BGE_ASICREV_BCM57780)
3483 sc->bge_flags |= BGE_FLAG_CPMU_PRESENT;
3484 if ((sc->bge_flags & BGE_FLAG_CPMU_PRESENT) != 0)
3485 sc->bge_mi_mode = BGE_MIMODE_500KHZ_CONST;
3487 sc->bge_mi_mode = BGE_MIMODE_BASE;
3488 /* Enable auto polling for BCM570[0-5]. */
3489 if (BGE_IS_5700_FAMILY(sc) || sc->bge_asicrev == BGE_ASICREV_BCM5705)
3490 sc->bge_mi_mode |= BGE_MIMODE_AUTOPOLL;
3493 * All Broadcom controllers have 4GB boundary DMA bug.
3494 * Whenever an address crosses a multiple of the 4GB boundary
3495 * (including 4GB, 8Gb, 12Gb, etc.) and makes the transition
3496 * from 0xX_FFFF_FFFF to 0x(X+1)_0000_0000 an internal DMA
3497 * state machine will lockup and cause the device to hang.
3499 sc->bge_flags |= BGE_FLAG_4G_BNDRY_BUG;
3501 /* BCM5755 or higher and BCM5906 have short DMA bug. */
3502 if (BGE_IS_5755_PLUS(sc) || sc->bge_asicrev == BGE_ASICREV_BCM5906)
3503 sc->bge_flags |= BGE_FLAG_SHORT_DMA_BUG;
3506 * BCM5719 cannot handle DMA requests for DMA segments that
3507 * have larger than 4KB in size. However the maximum DMA
3508 * segment size created in DMA tag is 4KB for TSO, so we
3509 * wouldn't encounter the issue here.
3511 if (sc->bge_asicrev == BGE_ASICREV_BCM5719)
3512 sc->bge_flags |= BGE_FLAG_4K_RDMA_BUG;
3514 misccfg = CSR_READ_4(sc, BGE_MISC_CFG) & BGE_MISCCFG_BOARD_ID_MASK;
3515 if (sc->bge_asicrev == BGE_ASICREV_BCM5705) {
3516 if (misccfg == BGE_MISCCFG_BOARD_ID_5788 ||
3517 misccfg == BGE_MISCCFG_BOARD_ID_5788M)
3518 sc->bge_flags |= BGE_FLAG_5788;
3521 capmask = BMSR_DEFCAPMASK;
3522 if ((sc->bge_asicrev == BGE_ASICREV_BCM5703 &&
3523 (misccfg == 0x4000 || misccfg == 0x8000)) ||
3524 (sc->bge_asicrev == BGE_ASICREV_BCM5705 &&
3525 pci_get_vendor(dev) == BCOM_VENDORID &&
3526 (pci_get_device(dev) == BCOM_DEVICEID_BCM5901 ||
3527 pci_get_device(dev) == BCOM_DEVICEID_BCM5901A2 ||
3528 pci_get_device(dev) == BCOM_DEVICEID_BCM5705F)) ||
3529 (pci_get_vendor(dev) == BCOM_VENDORID &&
3530 (pci_get_device(dev) == BCOM_DEVICEID_BCM5751F ||
3531 pci_get_device(dev) == BCOM_DEVICEID_BCM5753F ||
3532 pci_get_device(dev) == BCOM_DEVICEID_BCM5787F)) ||
3533 pci_get_device(dev) == BCOM_DEVICEID_BCM57790 ||
3534 pci_get_device(dev) == BCOM_DEVICEID_BCM57791 ||
3535 pci_get_device(dev) == BCOM_DEVICEID_BCM57795 ||
3536 sc->bge_asicrev == BGE_ASICREV_BCM5906) {
3537 /* These chips are 10/100 only. */
3538 capmask &= ~BMSR_EXTSTAT;
3539 sc->bge_phy_flags |= BGE_PHY_NO_WIRESPEED;
3543 * Some controllers seem to require a special firmware to use
3544 * TSO. But the firmware is not available to FreeBSD and Linux
3545 * claims that the TSO performed by the firmware is slower than
3546 * hardware based TSO. Moreover the firmware based TSO has one
3547 * known bug which can't handle TSO if Ethernet header + IP/TCP
3548 * header is greater than 80 bytes. A workaround for the TSO
3549 * bug exist but it seems it's too expensive than not using
3550 * TSO at all. Some hardwares also have the TSO bug so limit
3551 * the TSO to the controllers that are not affected TSO issues
3552 * (e.g. 5755 or higher).
3554 if (BGE_IS_5717_PLUS(sc)) {
3555 /* BCM5717 requires different TSO configuration. */
3556 sc->bge_flags |= BGE_FLAG_TSO3;
3557 if (sc->bge_asicrev == BGE_ASICREV_BCM5719 &&
3558 sc->bge_chipid == BGE_CHIPID_BCM5719_A0) {
3559 /* TSO on BCM5719 A0 does not work. */
3560 sc->bge_flags &= ~BGE_FLAG_TSO3;
3562 } else if (BGE_IS_5755_PLUS(sc)) {
3564 * BCM5754 and BCM5787 shares the same ASIC id so
3565 * explicit device id check is required.
3566 * Due to unknown reason TSO does not work on BCM5755M.
3568 if (pci_get_device(dev) != BCOM_DEVICEID_BCM5754 &&
3569 pci_get_device(dev) != BCOM_DEVICEID_BCM5754M &&
3570 pci_get_device(dev) != BCOM_DEVICEID_BCM5755M)
3571 sc->bge_flags |= BGE_FLAG_TSO;
3575 * Check if this is a PCI-X or PCI Express device.
3577 if (pci_find_cap(dev, PCIY_EXPRESS, ®) == 0) {
3579 * Found a PCI Express capabilities register, this
3580 * must be a PCI Express device.
3582 sc->bge_flags |= BGE_FLAG_PCIE;
3583 sc->bge_expcap = reg;
3584 /* Extract supported maximum payload size. */
3585 sc->bge_mps = pci_read_config(dev, sc->bge_expcap +
3586 PCIER_DEVICE_CAP, 2);
3587 sc->bge_mps = 128 << (sc->bge_mps & PCIEM_CAP_MAX_PAYLOAD);
3588 if (sc->bge_asicrev == BGE_ASICREV_BCM5719 ||
3589 sc->bge_asicrev == BGE_ASICREV_BCM5720)
3590 sc->bge_expmrq = 2048;
3592 sc->bge_expmrq = 4096;
3593 pci_set_max_read_req(dev, sc->bge_expmrq);
3596 * Check if the device is in PCI-X Mode.
3597 * (This bit is not valid on PCI Express controllers.)
3599 if (pci_find_cap(dev, PCIY_PCIX, ®) == 0)
3600 sc->bge_pcixcap = reg;
3601 if ((pci_read_config(dev, BGE_PCI_PCISTATE, 4) &
3602 BGE_PCISTATE_PCI_BUSMODE) == 0)
3603 sc->bge_flags |= BGE_FLAG_PCIX;
3607 * The 40bit DMA bug applies to the 5714/5715 controllers and is
3608 * not actually a MAC controller bug but an issue with the embedded
3609 * PCIe to PCI-X bridge in the device. Use 40bit DMA workaround.
3611 if (BGE_IS_5714_FAMILY(sc) && (sc->bge_flags & BGE_FLAG_PCIX))
3612 sc->bge_flags |= BGE_FLAG_40BIT_BUG;
3614 * Some PCI-X bridges are known to trigger write reordering to
3615 * the mailbox registers. Typical phenomena is watchdog timeouts
3616 * caused by out-of-order TX completions. Enable workaround for
3617 * PCI-X devices that live behind these bridges.
3618 * Note, PCI-X controllers can run in PCI mode so we can't use
3619 * BGE_FLAG_PCIX flag to detect PCI-X controllers.
3621 if (sc->bge_pcixcap != 0 && bge_mbox_reorder(sc) != 0)
3622 sc->bge_flags |= BGE_FLAG_MBOX_REORDER;
3624 * Allocate the interrupt, using MSI if possible. These devices
3625 * support 8 MSI messages, but only the first one is used in
3629 if (pci_find_cap(sc->bge_dev, PCIY_MSI, ®) == 0) {
3630 sc->bge_msicap = reg;
3632 if (bge_can_use_msi(sc) && pci_alloc_msi(dev, ®) == 0) {
3634 sc->bge_flags |= BGE_FLAG_MSI;
3639 * All controllers except BCM5700 supports tagged status but
3640 * we use tagged status only for MSI case on BCM5717. Otherwise
3641 * MSI on BCM5717 does not work.
3643 #ifndef DEVICE_POLLING
3644 if (sc->bge_flags & BGE_FLAG_MSI && BGE_IS_5717_PLUS(sc))
3645 sc->bge_flags |= BGE_FLAG_TAGGED_STATUS;
3648 sc->bge_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
3649 RF_ACTIVE | (rid != 0 ? 0 : RF_SHAREABLE));
3651 if (sc->bge_irq == NULL) {
3652 device_printf(sc->bge_dev, "couldn't map interrupt\n");
3659 sc->bge_asf_mode = 0;
3660 /* No ASF if APE present. */
3661 if ((sc->bge_flags & BGE_FLAG_APE) == 0) {
3662 if (bge_allow_asf && (bge_readmem_ind(sc, BGE_SRAM_DATA_SIG) ==
3663 BGE_SRAM_DATA_SIG_MAGIC)) {
3664 if (bge_readmem_ind(sc, BGE_SRAM_DATA_CFG) &
3666 sc->bge_asf_mode |= ASF_ENABLE;
3667 sc->bge_asf_mode |= ASF_STACKUP;
3668 if (BGE_IS_575X_PLUS(sc))
3669 sc->bge_asf_mode |= ASF_NEW_HANDSHAKE;
3675 bge_sig_pre_reset(sc, BGE_RESET_SHUTDOWN);
3676 if (bge_reset(sc)) {
3677 device_printf(sc->bge_dev, "chip reset failed\n");
3682 bge_sig_legacy(sc, BGE_RESET_SHUTDOWN);
3683 bge_sig_post_reset(sc, BGE_RESET_SHUTDOWN);
3685 if (bge_chipinit(sc)) {
3686 device_printf(sc->bge_dev, "chip initialization failed\n");
3691 error = bge_get_eaddr(sc, eaddr);
3693 device_printf(sc->bge_dev,
3694 "failed to read station address\n");
3699 /* 5705 limits RX return ring to 512 entries. */
3700 if (BGE_IS_5717_PLUS(sc))
3701 sc->bge_return_ring_cnt = BGE_RETURN_RING_CNT;
3702 else if (BGE_IS_5705_PLUS(sc))
3703 sc->bge_return_ring_cnt = BGE_RETURN_RING_CNT_5705;
3705 sc->bge_return_ring_cnt = BGE_RETURN_RING_CNT;
3707 if (bge_dma_alloc(sc)) {
3708 device_printf(sc->bge_dev,
3709 "failed to allocate DMA resources\n");
3714 /* Set default tuneable values. */
3715 sc->bge_stat_ticks = BGE_TICKS_PER_SEC;
3716 sc->bge_rx_coal_ticks = 150;
3717 sc->bge_tx_coal_ticks = 150;
3718 sc->bge_rx_max_coal_bds = 10;
3719 sc->bge_tx_max_coal_bds = 10;
3721 /* Initialize checksum features to use. */
3722 sc->bge_csum_features = BGE_CSUM_FEATURES;
3723 if (sc->bge_forced_udpcsum != 0)
3724 sc->bge_csum_features |= CSUM_UDP;
3726 /* Set up ifnet structure */
3727 ifp = sc->bge_ifp = if_alloc(IFT_ETHER);
3729 device_printf(sc->bge_dev, "failed to if_alloc()\n");
3734 if_initname(ifp, device_get_name(dev), device_get_unit(dev));
3735 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
3736 ifp->if_ioctl = bge_ioctl;
3737 ifp->if_start = bge_start;
3738 ifp->if_init = bge_init;
3739 ifp->if_snd.ifq_drv_maxlen = BGE_TX_RING_CNT - 1;
3740 IFQ_SET_MAXLEN(&ifp->if_snd, ifp->if_snd.ifq_drv_maxlen);
3741 IFQ_SET_READY(&ifp->if_snd);
3742 ifp->if_hwassist = sc->bge_csum_features;
3743 ifp->if_capabilities = IFCAP_HWCSUM | IFCAP_VLAN_HWTAGGING |
3745 if ((sc->bge_flags & (BGE_FLAG_TSO | BGE_FLAG_TSO3)) != 0) {
3746 ifp->if_hwassist |= CSUM_TSO;
3747 ifp->if_capabilities |= IFCAP_TSO4 | IFCAP_VLAN_HWTSO;
3749 #ifdef IFCAP_VLAN_HWCSUM
3750 ifp->if_capabilities |= IFCAP_VLAN_HWCSUM;
3752 ifp->if_capenable = ifp->if_capabilities;
3753 #ifdef DEVICE_POLLING
3754 ifp->if_capabilities |= IFCAP_POLLING;
3758 * 5700 B0 chips do not support checksumming correctly due
3761 if (sc->bge_chipid == BGE_CHIPID_BCM5700_B0) {
3762 ifp->if_capabilities &= ~IFCAP_HWCSUM;
3763 ifp->if_capenable &= ~IFCAP_HWCSUM;
3764 ifp->if_hwassist = 0;
3768 * Figure out what sort of media we have by checking the
3769 * hardware config word in the first 32k of NIC internal memory,
3770 * or fall back to examining the EEPROM if necessary.
3771 * Note: on some BCM5700 cards, this value appears to be unset.
3772 * If that's the case, we have to rely on identifying the NIC
3773 * by its PCI subsystem ID, as we do below for the SysKonnect
3776 if (bge_readmem_ind(sc, BGE_SRAM_DATA_SIG) == BGE_SRAM_DATA_SIG_MAGIC)
3777 hwcfg = bge_readmem_ind(sc, BGE_SRAM_DATA_CFG);
3778 else if ((sc->bge_flags & BGE_FLAG_EADDR) &&
3779 (sc->bge_asicrev != BGE_ASICREV_BCM5906)) {
3780 if (bge_read_eeprom(sc, (caddr_t)&hwcfg, BGE_EE_HWCFG_OFFSET,
3782 device_printf(sc->bge_dev, "failed to read EEPROM\n");
3786 hwcfg = ntohl(hwcfg);
3789 /* The SysKonnect SK-9D41 is a 1000baseSX card. */
3790 if ((pci_read_config(dev, BGE_PCI_SUBSYS, 4) >> 16) ==
3791 SK_SUBSYSID_9D41 || (hwcfg & BGE_HWCFG_MEDIA) == BGE_MEDIA_FIBER) {
3792 if (BGE_IS_5705_PLUS(sc)) {
3793 sc->bge_flags |= BGE_FLAG_MII_SERDES;
3794 sc->bge_phy_flags |= BGE_PHY_NO_WIRESPEED;
3796 sc->bge_flags |= BGE_FLAG_TBI;
3799 /* Set various PHY bug flags. */
3800 if (sc->bge_chipid == BGE_CHIPID_BCM5701_A0 ||
3801 sc->bge_chipid == BGE_CHIPID_BCM5701_B0)
3802 sc->bge_phy_flags |= BGE_PHY_CRC_BUG;
3803 if (sc->bge_chiprev == BGE_CHIPREV_5703_AX ||
3804 sc->bge_chiprev == BGE_CHIPREV_5704_AX)
3805 sc->bge_phy_flags |= BGE_PHY_ADC_BUG;
3806 if (sc->bge_chipid == BGE_CHIPID_BCM5704_A0)
3807 sc->bge_phy_flags |= BGE_PHY_5704_A0_BUG;
3808 if (pci_get_subvendor(dev) == DELL_VENDORID)
3809 sc->bge_phy_flags |= BGE_PHY_NO_3LED;
3810 if ((BGE_IS_5705_PLUS(sc)) &&
3811 sc->bge_asicrev != BGE_ASICREV_BCM5906 &&
3812 sc->bge_asicrev != BGE_ASICREV_BCM5785 &&
3813 sc->bge_asicrev != BGE_ASICREV_BCM57780 &&
3814 !BGE_IS_5717_PLUS(sc)) {
3815 if (sc->bge_asicrev == BGE_ASICREV_BCM5755 ||
3816 sc->bge_asicrev == BGE_ASICREV_BCM5761 ||
3817 sc->bge_asicrev == BGE_ASICREV_BCM5784 ||
3818 sc->bge_asicrev == BGE_ASICREV_BCM5787) {
3819 if (pci_get_device(dev) != BCOM_DEVICEID_BCM5722 &&
3820 pci_get_device(dev) != BCOM_DEVICEID_BCM5756)
3821 sc->bge_phy_flags |= BGE_PHY_JITTER_BUG;
3822 if (pci_get_device(dev) == BCOM_DEVICEID_BCM5755M)
3823 sc->bge_phy_flags |= BGE_PHY_ADJUST_TRIM;
3825 sc->bge_phy_flags |= BGE_PHY_BER_BUG;
3829 * Don't enable Ethernet@WireSpeed for the 5700 or the
3830 * 5705 A0 and A1 chips.
3832 if (sc->bge_asicrev == BGE_ASICREV_BCM5700 ||
3833 (sc->bge_asicrev == BGE_ASICREV_BCM5705 &&
3834 (sc->bge_chipid != BGE_CHIPID_BCM5705_A0 &&
3835 sc->bge_chipid != BGE_CHIPID_BCM5705_A1)))
3836 sc->bge_phy_flags |= BGE_PHY_NO_WIRESPEED;
3838 if (sc->bge_flags & BGE_FLAG_TBI) {
3839 ifmedia_init(&sc->bge_ifmedia, IFM_IMASK, bge_ifmedia_upd,
3841 ifmedia_add(&sc->bge_ifmedia, IFM_ETHER | IFM_1000_SX, 0, NULL);
3842 ifmedia_add(&sc->bge_ifmedia, IFM_ETHER | IFM_1000_SX | IFM_FDX,
3844 ifmedia_add(&sc->bge_ifmedia, IFM_ETHER | IFM_AUTO, 0, NULL);
3845 ifmedia_set(&sc->bge_ifmedia, IFM_ETHER | IFM_AUTO);
3846 sc->bge_ifmedia.ifm_media = sc->bge_ifmedia.ifm_cur->ifm_media;
3849 * Do transceiver setup and tell the firmware the
3850 * driver is down so we can try to get access the
3851 * probe if ASF is running. Retry a couple of times
3852 * if we get a conflict with the ASF firmware accessing
3856 BGE_CLRBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
3858 bge_asf_driver_up(sc);
3860 error = mii_attach(dev, &sc->bge_miibus, ifp, bge_ifmedia_upd,
3861 bge_ifmedia_sts, capmask, sc->bge_phy_addr, MII_OFFSET_ANY,
3865 device_printf(sc->bge_dev, "Try again\n");
3866 bge_miibus_writereg(sc->bge_dev,
3867 sc->bge_phy_addr, MII_BMCR, BMCR_RESET);
3870 device_printf(sc->bge_dev, "attaching PHYs failed\n");
3875 * Now tell the firmware we are going up after probing the PHY
3877 if (sc->bge_asf_mode & ASF_STACKUP)
3878 BGE_SETBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
3882 * When using the BCM5701 in PCI-X mode, data corruption has
3883 * been observed in the first few bytes of some received packets.
3884 * Aligning the packet buffer in memory eliminates the corruption.
3885 * Unfortunately, this misaligns the packet payloads. On platforms
3886 * which do not support unaligned accesses, we will realign the
3887 * payloads by copying the received packets.
3889 if (sc->bge_asicrev == BGE_ASICREV_BCM5701 &&
3890 sc->bge_flags & BGE_FLAG_PCIX)
3891 sc->bge_flags |= BGE_FLAG_RX_ALIGNBUG;
3894 * Call MI attach routine.
3896 ether_ifattach(ifp, eaddr);
3898 /* Tell upper layer we support long frames. */
3899 ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header);
3904 if (BGE_IS_5755_PLUS(sc) && sc->bge_flags & BGE_FLAG_MSI) {
3905 /* Take advantage of single-shot MSI. */
3906 CSR_WRITE_4(sc, BGE_MSI_MODE, CSR_READ_4(sc, BGE_MSI_MODE) &
3907 ~BGE_MSIMODE_ONE_SHOT_DISABLE);
3908 sc->bge_tq = taskqueue_create_fast("bge_taskq", M_WAITOK,
3909 taskqueue_thread_enqueue, &sc->bge_tq);
3910 if (sc->bge_tq == NULL) {
3911 device_printf(dev, "could not create taskqueue.\n");
3912 ether_ifdetach(ifp);
3916 error = taskqueue_start_threads(&sc->bge_tq, 1, PI_NET,
3917 "%s taskq", device_get_nameunit(sc->bge_dev));
3919 device_printf(dev, "could not start threads.\n");
3920 ether_ifdetach(ifp);
3923 error = bus_setup_intr(dev, sc->bge_irq,
3924 INTR_TYPE_NET | INTR_MPSAFE, bge_msi_intr, NULL, sc,
3927 error = bus_setup_intr(dev, sc->bge_irq,
3928 INTR_TYPE_NET | INTR_MPSAFE, NULL, bge_intr, sc,
3932 ether_ifdetach(ifp);
3933 device_printf(sc->bge_dev, "couldn't set up irq\n");
3943 bge_detach(device_t dev)
3945 struct bge_softc *sc;
3948 sc = device_get_softc(dev);
3951 #ifdef DEVICE_POLLING
3952 if (ifp->if_capenable & IFCAP_POLLING)
3953 ether_poll_deregister(ifp);
3956 if (device_is_attached(dev)) {
3957 ether_ifdetach(ifp);
3961 callout_drain(&sc->bge_stat_ch);
3965 taskqueue_drain(sc->bge_tq, &sc->bge_intr_task);
3967 if (sc->bge_flags & BGE_FLAG_TBI)
3968 ifmedia_removeall(&sc->bge_ifmedia);
3969 else if (sc->bge_miibus != NULL) {
3970 bus_generic_detach(dev);
3971 device_delete_child(dev, sc->bge_miibus);
3974 bge_release_resources(sc);
3980 bge_release_resources(struct bge_softc *sc)
3986 if (sc->bge_tq != NULL)
3987 taskqueue_free(sc->bge_tq);
3989 if (sc->bge_intrhand != NULL)
3990 bus_teardown_intr(dev, sc->bge_irq, sc->bge_intrhand);
3992 if (sc->bge_irq != NULL) {
3993 bus_release_resource(dev, SYS_RES_IRQ,
3994 rman_get_rid(sc->bge_irq), sc->bge_irq);
3995 pci_release_msi(dev);
3998 if (sc->bge_res != NULL)
3999 bus_release_resource(dev, SYS_RES_MEMORY,
4000 rman_get_rid(sc->bge_res), sc->bge_res);
4002 if (sc->bge_res2 != NULL)
4003 bus_release_resource(dev, SYS_RES_MEMORY,
4004 rman_get_rid(sc->bge_res2), sc->bge_res2);
4006 if (sc->bge_ifp != NULL)
4007 if_free(sc->bge_ifp);
4011 if (mtx_initialized(&sc->bge_mtx)) /* XXX */
4012 BGE_LOCK_DESTROY(sc);
4016 bge_reset(struct bge_softc *sc)
4019 uint32_t cachesize, command, mac_mode, mac_mode_mask, reset, val;
4020 void (*write_op)(struct bge_softc *, int, int);
4026 mac_mode_mask = BGE_MACMODE_HALF_DUPLEX | BGE_MACMODE_PORTMODE;
4027 if ((sc->bge_mfw_flags & BGE_MFW_ON_APE) != 0)
4028 mac_mode_mask |= BGE_MACMODE_APE_RX_EN | BGE_MACMODE_APE_TX_EN;
4029 mac_mode = CSR_READ_4(sc, BGE_MAC_MODE) & mac_mode_mask;
4031 if (BGE_IS_575X_PLUS(sc) && !BGE_IS_5714_FAMILY(sc) &&
4032 (sc->bge_asicrev != BGE_ASICREV_BCM5906)) {
4033 if (sc->bge_flags & BGE_FLAG_PCIE)
4034 write_op = bge_writemem_direct;
4036 write_op = bge_writemem_ind;
4038 write_op = bge_writereg_ind;
4040 if (sc->bge_asicrev != BGE_ASICREV_BCM5700 &&
4041 sc->bge_asicrev != BGE_ASICREV_BCM5701) {
4042 CSR_WRITE_4(sc, BGE_NVRAM_SWARB, BGE_NVRAMSWARB_SET1);
4043 for (i = 0; i < 8000; i++) {
4044 if (CSR_READ_4(sc, BGE_NVRAM_SWARB) &
4045 BGE_NVRAMSWARB_GNT1)
4051 device_printf(dev, "NVRAM lock timedout!\n");
4054 /* Take APE lock when performing reset. */
4055 bge_ape_lock(sc, BGE_APE_LOCK_GRC);
4057 /* Save some important PCI state. */
4058 cachesize = pci_read_config(dev, BGE_PCI_CACHESZ, 4);
4059 command = pci_read_config(dev, BGE_PCI_CMD, 4);
4061 pci_write_config(dev, BGE_PCI_MISC_CTL,
4062 BGE_PCIMISCCTL_INDIRECT_ACCESS | BGE_PCIMISCCTL_MASK_PCI_INTR |
4063 BGE_HIF_SWAP_OPTIONS | BGE_PCIMISCCTL_PCISTATE_RW, 4);
4065 /* Disable fastboot on controllers that support it. */
4066 if (sc->bge_asicrev == BGE_ASICREV_BCM5752 ||
4067 BGE_IS_5755_PLUS(sc)) {
4069 device_printf(dev, "Disabling fastboot\n");
4070 CSR_WRITE_4(sc, BGE_FASTBOOT_PC, 0x0);
4074 * Write the magic number to SRAM at offset 0xB50.
4075 * When firmware finishes its initialization it will
4076 * write ~BGE_SRAM_FW_MB_MAGIC to the same location.
4078 bge_writemem_ind(sc, BGE_SRAM_FW_MB, BGE_SRAM_FW_MB_MAGIC);
4080 reset = BGE_MISCCFG_RESET_CORE_CLOCKS | BGE_32BITTIME_66MHZ;
4082 /* XXX: Broadcom Linux driver. */
4083 if (sc->bge_flags & BGE_FLAG_PCIE) {
4084 if (sc->bge_asicrev != BGE_ASICREV_BCM5785 &&
4085 (sc->bge_flags & BGE_FLAG_5717_PLUS) == 0) {
4086 if (CSR_READ_4(sc, 0x7E2C) == 0x60) /* PCIE 1.0 */
4087 CSR_WRITE_4(sc, 0x7E2C, 0x20);
4089 if (sc->bge_chipid != BGE_CHIPID_BCM5750_A0) {
4090 /* Prevent PCIE link training during global reset */
4091 CSR_WRITE_4(sc, BGE_MISC_CFG, 1 << 29);
4096 if (sc->bge_asicrev == BGE_ASICREV_BCM5906) {
4097 val = CSR_READ_4(sc, BGE_VCPU_STATUS);
4098 CSR_WRITE_4(sc, BGE_VCPU_STATUS,
4099 val | BGE_VCPU_STATUS_DRV_RESET);
4100 val = CSR_READ_4(sc, BGE_VCPU_EXT_CTRL);
4101 CSR_WRITE_4(sc, BGE_VCPU_EXT_CTRL,
4102 val & ~BGE_VCPU_EXT_CTRL_HALT_CPU);
4106 * Set GPHY Power Down Override to leave GPHY
4107 * powered up in D0 uninitialized.
4109 if (BGE_IS_5705_PLUS(sc) &&
4110 (sc->bge_flags & BGE_FLAG_CPMU_PRESENT) == 0)
4111 reset |= BGE_MISCCFG_GPHY_PD_OVERRIDE;
4113 /* Issue global reset */
4114 write_op(sc, BGE_MISC_CFG, reset);
4116 if (sc->bge_flags & BGE_FLAG_PCIE)
4121 /* XXX: Broadcom Linux driver. */
4122 if (sc->bge_flags & BGE_FLAG_PCIE) {
4123 if (sc->bge_chipid == BGE_CHIPID_BCM5750_A0) {
4124 DELAY(500000); /* wait for link training to complete */
4125 val = pci_read_config(dev, 0xC4, 4);
4126 pci_write_config(dev, 0xC4, val | (1 << 15), 4);
4128 devctl = pci_read_config(dev,
4129 sc->bge_expcap + PCIER_DEVICE_CTL, 2);
4130 /* Clear enable no snoop and disable relaxed ordering. */
4131 devctl &= ~(PCIEM_CTL_RELAXED_ORD_ENABLE |
4132 PCIEM_CTL_NOSNOOP_ENABLE);
4133 pci_write_config(dev, sc->bge_expcap + PCIER_DEVICE_CTL,
4135 pci_set_max_read_req(dev, sc->bge_expmrq);
4136 /* Clear error status. */
4137 pci_write_config(dev, sc->bge_expcap + PCIER_DEVICE_STA,
4138 PCIEM_STA_CORRECTABLE_ERROR |
4139 PCIEM_STA_NON_FATAL_ERROR | PCIEM_STA_FATAL_ERROR |
4140 PCIEM_STA_UNSUPPORTED_REQ, 2);
4143 /* Reset some of the PCI state that got zapped by reset. */
4144 pci_write_config(dev, BGE_PCI_MISC_CTL,
4145 BGE_PCIMISCCTL_INDIRECT_ACCESS | BGE_PCIMISCCTL_MASK_PCI_INTR |
4146 BGE_HIF_SWAP_OPTIONS | BGE_PCIMISCCTL_PCISTATE_RW, 4);
4147 val = BGE_PCISTATE_ROM_ENABLE | BGE_PCISTATE_ROM_RETRY_ENABLE;
4148 if (sc->bge_chipid == BGE_CHIPID_BCM5704_A0 &&
4149 (sc->bge_flags & BGE_FLAG_PCIX) != 0)
4150 val |= BGE_PCISTATE_RETRY_SAME_DMA;
4151 if ((sc->bge_mfw_flags & BGE_MFW_ON_APE) != 0)
4152 val |= BGE_PCISTATE_ALLOW_APE_CTLSPC_WR |
4153 BGE_PCISTATE_ALLOW_APE_SHMEM_WR |
4154 BGE_PCISTATE_ALLOW_APE_PSPACE_WR;
4155 pci_write_config(dev, BGE_PCI_PCISTATE, val, 4);
4156 pci_write_config(dev, BGE_PCI_CACHESZ, cachesize, 4);
4157 pci_write_config(dev, BGE_PCI_CMD, command, 4);
4159 * Disable PCI-X relaxed ordering to ensure status block update
4160 * comes first then packet buffer DMA. Otherwise driver may
4161 * read stale status block.
4163 if (sc->bge_flags & BGE_FLAG_PCIX) {
4164 devctl = pci_read_config(dev,
4165 sc->bge_pcixcap + PCIXR_COMMAND, 2);
4166 devctl &= ~PCIXM_COMMAND_ERO;
4167 if (sc->bge_asicrev == BGE_ASICREV_BCM5703) {
4168 devctl &= ~PCIXM_COMMAND_MAX_READ;
4169 devctl |= PCIXM_COMMAND_MAX_READ_2048;
4170 } else if (sc->bge_asicrev == BGE_ASICREV_BCM5704) {
4171 devctl &= ~(PCIXM_COMMAND_MAX_SPLITS |
4172 PCIXM_COMMAND_MAX_READ);
4173 devctl |= PCIXM_COMMAND_MAX_READ_2048;
4175 pci_write_config(dev, sc->bge_pcixcap + PCIXR_COMMAND,
4178 /* Re-enable MSI, if necessary, and enable the memory arbiter. */
4179 if (BGE_IS_5714_FAMILY(sc)) {
4180 /* This chip disables MSI on reset. */
4181 if (sc->bge_flags & BGE_FLAG_MSI) {
4182 val = pci_read_config(dev,
4183 sc->bge_msicap + PCIR_MSI_CTRL, 2);
4184 pci_write_config(dev,
4185 sc->bge_msicap + PCIR_MSI_CTRL,
4186 val | PCIM_MSICTRL_MSI_ENABLE, 2);
4187 val = CSR_READ_4(sc, BGE_MSI_MODE);
4188 CSR_WRITE_4(sc, BGE_MSI_MODE,
4189 val | BGE_MSIMODE_ENABLE);
4191 val = CSR_READ_4(sc, BGE_MARB_MODE);
4192 CSR_WRITE_4(sc, BGE_MARB_MODE, BGE_MARBMODE_ENABLE | val);
4194 CSR_WRITE_4(sc, BGE_MARB_MODE, BGE_MARBMODE_ENABLE);
4196 /* Fix up byte swapping. */
4197 CSR_WRITE_4(sc, BGE_MODE_CTL, bge_dma_swap_options(sc));
4199 val = CSR_READ_4(sc, BGE_MAC_MODE);
4200 val = (val & ~mac_mode_mask) | mac_mode;
4201 CSR_WRITE_4(sc, BGE_MAC_MODE, val);
4204 bge_ape_unlock(sc, BGE_APE_LOCK_GRC);
4206 if (sc->bge_asicrev == BGE_ASICREV_BCM5906) {
4207 for (i = 0; i < BGE_TIMEOUT; i++) {
4208 val = CSR_READ_4(sc, BGE_VCPU_STATUS);
4209 if (val & BGE_VCPU_STATUS_INIT_DONE)
4213 if (i == BGE_TIMEOUT) {
4214 device_printf(dev, "reset timed out\n");
4219 * Poll until we see the 1's complement of the magic number.
4220 * This indicates that the firmware initialization is complete.
4221 * We expect this to fail if no chip containing the Ethernet
4222 * address is fitted though.
4224 for (i = 0; i < BGE_TIMEOUT; i++) {
4226 val = bge_readmem_ind(sc, BGE_SRAM_FW_MB);
4227 if (val == ~BGE_SRAM_FW_MB_MAGIC)
4231 if ((sc->bge_flags & BGE_FLAG_EADDR) && i == BGE_TIMEOUT)
4233 "firmware handshake timed out, found 0x%08x\n",
4235 /* BCM57765 A0 needs additional time before accessing. */
4236 if (sc->bge_chipid == BGE_CHIPID_BCM57765_A0)
4237 DELAY(10 * 1000); /* XXX */
4241 * The 5704 in TBI mode apparently needs some special
4242 * adjustment to insure the SERDES drive level is set
4245 if (sc->bge_asicrev == BGE_ASICREV_BCM5704 &&
4246 sc->bge_flags & BGE_FLAG_TBI) {
4247 val = CSR_READ_4(sc, BGE_SERDES_CFG);
4248 val = (val & ~0xFFF) | 0x880;
4249 CSR_WRITE_4(sc, BGE_SERDES_CFG, val);
4252 /* XXX: Broadcom Linux driver. */
4253 if (sc->bge_flags & BGE_FLAG_PCIE &&
4254 !BGE_IS_5717_PLUS(sc) &&
4255 sc->bge_chipid != BGE_CHIPID_BCM5750_A0 &&
4256 sc->bge_asicrev != BGE_ASICREV_BCM5785) {
4257 /* Enable Data FIFO protection. */
4258 val = CSR_READ_4(sc, 0x7C00);
4259 CSR_WRITE_4(sc, 0x7C00, val | (1 << 25));
4262 if (sc->bge_asicrev == BGE_ASICREV_BCM5720)
4263 BGE_CLRBIT(sc, BGE_CPMU_CLCK_ORIDE,
4264 CPMU_CLCK_ORIDE_MAC_ORIDE_EN);
4269 static __inline void
4270 bge_rxreuse_std(struct bge_softc *sc, int i)
4272 struct bge_rx_bd *r;
4274 r = &sc->bge_ldata.bge_rx_std_ring[sc->bge_std];
4275 r->bge_flags = BGE_RXBDFLAG_END;
4276 r->bge_len = sc->bge_cdata.bge_rx_std_seglen[i];
4278 BGE_INC(sc->bge_std, BGE_STD_RX_RING_CNT);
4281 static __inline void
4282 bge_rxreuse_jumbo(struct bge_softc *sc, int i)
4284 struct bge_extrx_bd *r;
4286 r = &sc->bge_ldata.bge_rx_jumbo_ring[sc->bge_jumbo];
4287 r->bge_flags = BGE_RXBDFLAG_JUMBO_RING | BGE_RXBDFLAG_END;
4288 r->bge_len0 = sc->bge_cdata.bge_rx_jumbo_seglen[i][0];
4289 r->bge_len1 = sc->bge_cdata.bge_rx_jumbo_seglen[i][1];
4290 r->bge_len2 = sc->bge_cdata.bge_rx_jumbo_seglen[i][2];
4291 r->bge_len3 = sc->bge_cdata.bge_rx_jumbo_seglen[i][3];
4293 BGE_INC(sc->bge_jumbo, BGE_JUMBO_RX_RING_CNT);
4297 * Frame reception handling. This is called if there's a frame
4298 * on the receive return list.
4300 * Note: we have to be able to handle two possibilities here:
4301 * 1) the frame is from the jumbo receive ring
4302 * 2) the frame is from the standard receive ring
4306 bge_rxeof(struct bge_softc *sc, uint16_t rx_prod, int holdlck)
4309 int rx_npkts = 0, stdcnt = 0, jumbocnt = 0;
4312 rx_cons = sc->bge_rx_saved_considx;
4314 /* Nothing to do. */
4315 if (rx_cons == rx_prod)
4320 bus_dmamap_sync(sc->bge_cdata.bge_rx_return_ring_tag,
4321 sc->bge_cdata.bge_rx_return_ring_map, BUS_DMASYNC_POSTREAD);
4322 bus_dmamap_sync(sc->bge_cdata.bge_rx_std_ring_tag,
4323 sc->bge_cdata.bge_rx_std_ring_map, BUS_DMASYNC_POSTWRITE);
4324 if (BGE_IS_JUMBO_CAPABLE(sc) &&
4325 ifp->if_mtu + ETHER_HDR_LEN + ETHER_CRC_LEN + ETHER_VLAN_ENCAP_LEN >
4326 (MCLBYTES - ETHER_ALIGN))
4327 bus_dmamap_sync(sc->bge_cdata.bge_rx_jumbo_ring_tag,
4328 sc->bge_cdata.bge_rx_jumbo_ring_map, BUS_DMASYNC_POSTWRITE);
4330 while (rx_cons != rx_prod) {
4331 struct bge_rx_bd *cur_rx;
4333 struct mbuf *m = NULL;
4334 uint16_t vlan_tag = 0;
4337 #ifdef DEVICE_POLLING
4338 if (ifp->if_capenable & IFCAP_POLLING) {
4339 if (sc->rxcycles <= 0)
4345 cur_rx = &sc->bge_ldata.bge_rx_return_ring[rx_cons];
4347 rxidx = cur_rx->bge_idx;
4348 BGE_INC(rx_cons, sc->bge_return_ring_cnt);
4350 if (ifp->if_capenable & IFCAP_VLAN_HWTAGGING &&
4351 cur_rx->bge_flags & BGE_RXBDFLAG_VLAN_TAG) {
4353 vlan_tag = cur_rx->bge_vlan_tag;
4356 if (cur_rx->bge_flags & BGE_RXBDFLAG_JUMBO_RING) {
4358 m = sc->bge_cdata.bge_rx_jumbo_chain[rxidx];
4359 if (cur_rx->bge_flags & BGE_RXBDFLAG_ERROR) {
4360 bge_rxreuse_jumbo(sc, rxidx);
4363 if (bge_newbuf_jumbo(sc, rxidx) != 0) {
4364 bge_rxreuse_jumbo(sc, rxidx);
4368 BGE_INC(sc->bge_jumbo, BGE_JUMBO_RX_RING_CNT);
4371 m = sc->bge_cdata.bge_rx_std_chain[rxidx];
4372 if (cur_rx->bge_flags & BGE_RXBDFLAG_ERROR) {
4373 bge_rxreuse_std(sc, rxidx);
4376 if (bge_newbuf_std(sc, rxidx) != 0) {
4377 bge_rxreuse_std(sc, rxidx);
4381 BGE_INC(sc->bge_std, BGE_STD_RX_RING_CNT);
4385 #ifndef __NO_STRICT_ALIGNMENT
4387 * For architectures with strict alignment we must make sure
4388 * the payload is aligned.
4390 if (sc->bge_flags & BGE_FLAG_RX_ALIGNBUG) {
4391 bcopy(m->m_data, m->m_data + ETHER_ALIGN,
4393 m->m_data += ETHER_ALIGN;
4396 m->m_pkthdr.len = m->m_len = cur_rx->bge_len - ETHER_CRC_LEN;
4397 m->m_pkthdr.rcvif = ifp;
4399 if (ifp->if_capenable & IFCAP_RXCSUM)
4400 bge_rxcsum(sc, cur_rx, m);
4403 * If we received a packet with a vlan tag,
4404 * attach that information to the packet.
4407 m->m_pkthdr.ether_vtag = vlan_tag;
4408 m->m_flags |= M_VLANTAG;
4413 (*ifp->if_input)(ifp, m);
4416 (*ifp->if_input)(ifp, m);
4419 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
4423 bus_dmamap_sync(sc->bge_cdata.bge_rx_return_ring_tag,
4424 sc->bge_cdata.bge_rx_return_ring_map, BUS_DMASYNC_PREREAD);
4426 bus_dmamap_sync(sc->bge_cdata.bge_rx_std_ring_tag,
4427 sc->bge_cdata.bge_rx_std_ring_map, BUS_DMASYNC_PREWRITE);
4430 bus_dmamap_sync(sc->bge_cdata.bge_rx_jumbo_ring_tag,
4431 sc->bge_cdata.bge_rx_jumbo_ring_map, BUS_DMASYNC_PREWRITE);
4433 sc->bge_rx_saved_considx = rx_cons;
4434 bge_writembx(sc, BGE_MBX_RX_CONS0_LO, sc->bge_rx_saved_considx);
4436 bge_writembx(sc, BGE_MBX_RX_STD_PROD_LO, (sc->bge_std +
4437 BGE_STD_RX_RING_CNT - 1) % BGE_STD_RX_RING_CNT);
4439 bge_writembx(sc, BGE_MBX_RX_JUMBO_PROD_LO, (sc->bge_jumbo +
4440 BGE_JUMBO_RX_RING_CNT - 1) % BGE_JUMBO_RX_RING_CNT);
4443 * This register wraps very quickly under heavy packet drops.
4444 * If you need correct statistics, you can enable this check.
4446 if (BGE_IS_5705_PLUS(sc))
4447 ifp->if_ierrors += CSR_READ_4(sc, BGE_RXLP_LOCSTAT_IFIN_DROPS);
4453 bge_rxcsum(struct bge_softc *sc, struct bge_rx_bd *cur_rx, struct mbuf *m)
4456 if (BGE_IS_5717_PLUS(sc)) {
4457 if ((cur_rx->bge_flags & BGE_RXBDFLAG_IPV6) == 0) {
4458 if (cur_rx->bge_flags & BGE_RXBDFLAG_IP_CSUM) {
4459 m->m_pkthdr.csum_flags |= CSUM_IP_CHECKED;
4460 if ((cur_rx->bge_error_flag &
4461 BGE_RXERRFLAG_IP_CSUM_NOK) == 0)
4462 m->m_pkthdr.csum_flags |= CSUM_IP_VALID;
4464 if (cur_rx->bge_flags & BGE_RXBDFLAG_TCP_UDP_CSUM) {
4465 m->m_pkthdr.csum_data =
4466 cur_rx->bge_tcp_udp_csum;
4467 m->m_pkthdr.csum_flags |= CSUM_DATA_VALID |
4472 if (cur_rx->bge_flags & BGE_RXBDFLAG_IP_CSUM) {
4473 m->m_pkthdr.csum_flags |= CSUM_IP_CHECKED;
4474 if ((cur_rx->bge_ip_csum ^ 0xFFFF) == 0)
4475 m->m_pkthdr.csum_flags |= CSUM_IP_VALID;
4477 if (cur_rx->bge_flags & BGE_RXBDFLAG_TCP_UDP_CSUM &&
4478 m->m_pkthdr.len >= ETHER_MIN_NOPAD) {
4479 m->m_pkthdr.csum_data =
4480 cur_rx->bge_tcp_udp_csum;
4481 m->m_pkthdr.csum_flags |= CSUM_DATA_VALID |
4488 bge_txeof(struct bge_softc *sc, uint16_t tx_cons)
4490 struct bge_tx_bd *cur_tx;
4493 BGE_LOCK_ASSERT(sc);
4495 /* Nothing to do. */
4496 if (sc->bge_tx_saved_considx == tx_cons)
4501 bus_dmamap_sync(sc->bge_cdata.bge_tx_ring_tag,
4502 sc->bge_cdata.bge_tx_ring_map, BUS_DMASYNC_POSTWRITE);
4504 * Go through our tx ring and free mbufs for those
4505 * frames that have been sent.
4507 while (sc->bge_tx_saved_considx != tx_cons) {
4510 idx = sc->bge_tx_saved_considx;
4511 cur_tx = &sc->bge_ldata.bge_tx_ring[idx];
4512 if (cur_tx->bge_flags & BGE_TXBDFLAG_END)
4514 if (sc->bge_cdata.bge_tx_chain[idx] != NULL) {
4515 bus_dmamap_sync(sc->bge_cdata.bge_tx_mtag,
4516 sc->bge_cdata.bge_tx_dmamap[idx],
4517 BUS_DMASYNC_POSTWRITE);
4518 bus_dmamap_unload(sc->bge_cdata.bge_tx_mtag,
4519 sc->bge_cdata.bge_tx_dmamap[idx]);
4520 m_freem(sc->bge_cdata.bge_tx_chain[idx]);
4521 sc->bge_cdata.bge_tx_chain[idx] = NULL;
4524 BGE_INC(sc->bge_tx_saved_considx, BGE_TX_RING_CNT);
4527 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
4528 if (sc->bge_txcnt == 0)
4532 #ifdef DEVICE_POLLING
4534 bge_poll(struct ifnet *ifp, enum poll_cmd cmd, int count)
4536 struct bge_softc *sc = ifp->if_softc;
4537 uint16_t rx_prod, tx_cons;
4538 uint32_t statusword;
4542 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
4547 bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
4548 sc->bge_cdata.bge_status_map,
4549 BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
4550 /* Fetch updates from the status block. */
4551 rx_prod = sc->bge_ldata.bge_status_block->bge_idx[0].bge_rx_prod_idx;
4552 tx_cons = sc->bge_ldata.bge_status_block->bge_idx[0].bge_tx_cons_idx;
4554 statusword = sc->bge_ldata.bge_status_block->bge_status;
4555 /* Clear the status so the next pass only sees the changes. */
4556 sc->bge_ldata.bge_status_block->bge_status = 0;
4558 bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
4559 sc->bge_cdata.bge_status_map,
4560 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
4562 /* Note link event. It will be processed by POLL_AND_CHECK_STATUS. */
4563 if (statusword & BGE_STATFLAG_LINKSTATE_CHANGED)
4566 if (cmd == POLL_AND_CHECK_STATUS)
4567 if ((sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
4568 sc->bge_chipid != BGE_CHIPID_BCM5700_B2) ||
4569 sc->bge_link_evt || (sc->bge_flags & BGE_FLAG_TBI))
4572 sc->rxcycles = count;
4573 rx_npkts = bge_rxeof(sc, rx_prod, 1);
4574 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
4578 bge_txeof(sc, tx_cons);
4579 if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
4580 bge_start_locked(ifp);
4585 #endif /* DEVICE_POLLING */
4588 bge_msi_intr(void *arg)
4590 struct bge_softc *sc;
4592 sc = (struct bge_softc *)arg;
4594 * This interrupt is not shared and controller already
4595 * disabled further interrupt.
4597 taskqueue_enqueue(sc->bge_tq, &sc->bge_intr_task);
4598 return (FILTER_HANDLED);
4602 bge_intr_task(void *arg, int pending)
4604 struct bge_softc *sc;
4606 uint32_t status, status_tag;
4607 uint16_t rx_prod, tx_cons;
4609 sc = (struct bge_softc *)arg;
4613 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
4618 /* Get updated status block. */
4619 bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
4620 sc->bge_cdata.bge_status_map,
4621 BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
4623 /* Save producer/consumer indices. */
4624 rx_prod = sc->bge_ldata.bge_status_block->bge_idx[0].bge_rx_prod_idx;
4625 tx_cons = sc->bge_ldata.bge_status_block->bge_idx[0].bge_tx_cons_idx;
4626 status = sc->bge_ldata.bge_status_block->bge_status;
4627 status_tag = sc->bge_ldata.bge_status_block->bge_status_tag << 24;
4628 /* Dirty the status flag. */
4629 sc->bge_ldata.bge_status_block->bge_status = 0;
4630 bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
4631 sc->bge_cdata.bge_status_map,
4632 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
4633 if ((sc->bge_flags & BGE_FLAG_TAGGED_STATUS) == 0)
4636 if ((status & BGE_STATFLAG_LINKSTATE_CHANGED) != 0)
4639 /* Let controller work. */
4640 bge_writembx(sc, BGE_MBX_IRQ0_LO, status_tag);
4642 if (ifp->if_drv_flags & IFF_DRV_RUNNING &&
4643 sc->bge_rx_saved_considx != rx_prod) {
4644 /* Check RX return ring producer/consumer. */
4646 bge_rxeof(sc, rx_prod, 0);
4649 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
4650 /* Check TX ring producer/consumer. */
4651 bge_txeof(sc, tx_cons);
4652 if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
4653 bge_start_locked(ifp);
4661 struct bge_softc *sc;
4663 uint32_t statusword;
4664 uint16_t rx_prod, tx_cons;
4672 #ifdef DEVICE_POLLING
4673 if (ifp->if_capenable & IFCAP_POLLING) {
4680 * Ack the interrupt by writing something to BGE_MBX_IRQ0_LO. Don't
4681 * disable interrupts by writing nonzero like we used to, since with
4682 * our current organization this just gives complications and
4683 * pessimizations for re-enabling interrupts. We used to have races
4684 * instead of the necessary complications. Disabling interrupts
4685 * would just reduce the chance of a status update while we are
4686 * running (by switching to the interrupt-mode coalescence
4687 * parameters), but this chance is already very low so it is more
4688 * efficient to get another interrupt than prevent it.
4690 * We do the ack first to ensure another interrupt if there is a
4691 * status update after the ack. We don't check for the status
4692 * changing later because it is more efficient to get another
4693 * interrupt than prevent it, not quite as above (not checking is
4694 * a smaller optimization than not toggling the interrupt enable,
4695 * since checking doesn't involve PCI accesses and toggling require
4696 * the status check). So toggling would probably be a pessimization
4697 * even with MSI. It would only be needed for using a task queue.
4699 bge_writembx(sc, BGE_MBX_IRQ0_LO, 0);
4702 * Do the mandatory PCI flush as well as get the link status.
4704 statusword = CSR_READ_4(sc, BGE_MAC_STS) & BGE_MACSTAT_LINK_CHANGED;
4706 /* Make sure the descriptor ring indexes are coherent. */
4707 bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
4708 sc->bge_cdata.bge_status_map,
4709 BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
4710 rx_prod = sc->bge_ldata.bge_status_block->bge_idx[0].bge_rx_prod_idx;
4711 tx_cons = sc->bge_ldata.bge_status_block->bge_idx[0].bge_tx_cons_idx;
4712 sc->bge_ldata.bge_status_block->bge_status = 0;
4713 bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
4714 sc->bge_cdata.bge_status_map,
4715 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
4717 if ((sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
4718 sc->bge_chipid != BGE_CHIPID_BCM5700_B2) ||
4719 statusword || sc->bge_link_evt)
4722 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
4723 /* Check RX return ring producer/consumer. */
4724 bge_rxeof(sc, rx_prod, 1);
4727 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
4728 /* Check TX ring producer/consumer. */
4729 bge_txeof(sc, tx_cons);
4732 if (ifp->if_drv_flags & IFF_DRV_RUNNING &&
4733 !IFQ_DRV_IS_EMPTY(&ifp->if_snd))
4734 bge_start_locked(ifp);
4740 bge_asf_driver_up(struct bge_softc *sc)
4742 if (sc->bge_asf_mode & ASF_STACKUP) {
4743 /* Send ASF heartbeat aprox. every 2s */
4744 if (sc->bge_asf_count)
4745 sc->bge_asf_count --;
4747 sc->bge_asf_count = 2;
4748 bge_writemem_ind(sc, BGE_SRAM_FW_CMD_MB,
4749 BGE_FW_CMD_DRV_ALIVE);
4750 bge_writemem_ind(sc, BGE_SRAM_FW_CMD_LEN_MB, 4);
4751 bge_writemem_ind(sc, BGE_SRAM_FW_CMD_DATA_MB,
4752 BGE_FW_HB_TIMEOUT_SEC);
4753 CSR_WRITE_4(sc, BGE_RX_CPU_EVENT,
4754 CSR_READ_4(sc, BGE_RX_CPU_EVENT) |
4755 BGE_RX_CPU_DRV_EVENT);
4763 struct bge_softc *sc = xsc;
4764 struct mii_data *mii = NULL;
4766 BGE_LOCK_ASSERT(sc);
4768 /* Synchronize with possible callout reset/stop. */
4769 if (callout_pending(&sc->bge_stat_ch) ||
4770 !callout_active(&sc->bge_stat_ch))
4773 if (BGE_IS_5705_PLUS(sc))
4774 bge_stats_update_regs(sc);
4776 bge_stats_update(sc);
4778 /* XXX Add APE heartbeat check here? */
4780 if ((sc->bge_flags & BGE_FLAG_TBI) == 0) {
4781 mii = device_get_softc(sc->bge_miibus);
4783 * Do not touch PHY if we have link up. This could break
4784 * IPMI/ASF mode or produce extra input errors
4785 * (extra errors was reported for bcm5701 & bcm5704).
4791 * Since in TBI mode auto-polling can't be used we should poll
4792 * link status manually. Here we register pending link event
4793 * and trigger interrupt.
4795 #ifdef DEVICE_POLLING
4796 /* In polling mode we poll link state in bge_poll(). */
4797 if (!(sc->bge_ifp->if_capenable & IFCAP_POLLING))
4801 if (sc->bge_asicrev == BGE_ASICREV_BCM5700 ||
4802 sc->bge_flags & BGE_FLAG_5788)
4803 BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_INTR_SET);
4805 BGE_SETBIT(sc, BGE_HCC_MODE, BGE_HCCMODE_COAL_NOW);
4809 bge_asf_driver_up(sc);
4812 callout_reset(&sc->bge_stat_ch, hz, bge_tick, sc);
4816 bge_stats_update_regs(struct bge_softc *sc)
4819 struct bge_mac_stats *stats;
4823 stats = &sc->bge_mac_stats;
4825 stats->ifHCOutOctets +=
4826 CSR_READ_4(sc, BGE_TX_MAC_STATS_OCTETS);
4827 stats->etherStatsCollisions +=
4828 CSR_READ_4(sc, BGE_TX_MAC_STATS_COLLS);
4829 stats->outXonSent +=
4830 CSR_READ_4(sc, BGE_TX_MAC_STATS_XON_SENT);
4831 stats->outXoffSent +=
4832 CSR_READ_4(sc, BGE_TX_MAC_STATS_XOFF_SENT);
4833 stats->dot3StatsInternalMacTransmitErrors +=
4834 CSR_READ_4(sc, BGE_TX_MAC_STATS_ERRORS);
4835 stats->dot3StatsSingleCollisionFrames +=
4836 CSR_READ_4(sc, BGE_TX_MAC_STATS_SINGLE_COLL);
4837 stats->dot3StatsMultipleCollisionFrames +=
4838 CSR_READ_4(sc, BGE_TX_MAC_STATS_MULTI_COLL);
4839 stats->dot3StatsDeferredTransmissions +=
4840 CSR_READ_4(sc, BGE_TX_MAC_STATS_DEFERRED);
4841 stats->dot3StatsExcessiveCollisions +=
4842 CSR_READ_4(sc, BGE_TX_MAC_STATS_EXCESS_COLL);
4843 stats->dot3StatsLateCollisions +=
4844 CSR_READ_4(sc, BGE_TX_MAC_STATS_LATE_COLL);
4845 stats->ifHCOutUcastPkts +=
4846 CSR_READ_4(sc, BGE_TX_MAC_STATS_UCAST);
4847 stats->ifHCOutMulticastPkts +=
4848 CSR_READ_4(sc, BGE_TX_MAC_STATS_MCAST);
4849 stats->ifHCOutBroadcastPkts +=
4850 CSR_READ_4(sc, BGE_TX_MAC_STATS_BCAST);
4852 stats->ifHCInOctets +=
4853 CSR_READ_4(sc, BGE_RX_MAC_STATS_OCTESTS);
4854 stats->etherStatsFragments +=
4855 CSR_READ_4(sc, BGE_RX_MAC_STATS_FRAGMENTS);
4856 stats->ifHCInUcastPkts +=
4857 CSR_READ_4(sc, BGE_RX_MAC_STATS_UCAST);
4858 stats->ifHCInMulticastPkts +=
4859 CSR_READ_4(sc, BGE_RX_MAC_STATS_MCAST);
4860 stats->ifHCInBroadcastPkts +=
4861 CSR_READ_4(sc, BGE_RX_MAC_STATS_BCAST);
4862 stats->dot3StatsFCSErrors +=
4863 CSR_READ_4(sc, BGE_RX_MAC_STATS_FCS_ERRORS);
4864 stats->dot3StatsAlignmentErrors +=
4865 CSR_READ_4(sc, BGE_RX_MAC_STATS_ALGIN_ERRORS);
4866 stats->xonPauseFramesReceived +=
4867 CSR_READ_4(sc, BGE_RX_MAC_STATS_XON_RCVD);
4868 stats->xoffPauseFramesReceived +=
4869 CSR_READ_4(sc, BGE_RX_MAC_STATS_XOFF_RCVD);
4870 stats->macControlFramesReceived +=
4871 CSR_READ_4(sc, BGE_RX_MAC_STATS_CTRL_RCVD);
4872 stats->xoffStateEntered +=
4873 CSR_READ_4(sc, BGE_RX_MAC_STATS_XOFF_ENTERED);
4874 stats->dot3StatsFramesTooLong +=
4875 CSR_READ_4(sc, BGE_RX_MAC_STATS_FRAME_TOO_LONG);
4876 stats->etherStatsJabbers +=
4877 CSR_READ_4(sc, BGE_RX_MAC_STATS_JABBERS);
4878 stats->etherStatsUndersizePkts +=
4879 CSR_READ_4(sc, BGE_RX_MAC_STATS_UNDERSIZE);
4881 stats->FramesDroppedDueToFilters +=
4882 CSR_READ_4(sc, BGE_RXLP_LOCSTAT_FILTDROP);
4883 stats->DmaWriteQueueFull +=
4884 CSR_READ_4(sc, BGE_RXLP_LOCSTAT_DMA_WRQ_FULL);
4885 stats->DmaWriteHighPriQueueFull +=
4886 CSR_READ_4(sc, BGE_RXLP_LOCSTAT_DMA_HPWRQ_FULL);
4887 stats->NoMoreRxBDs +=
4888 CSR_READ_4(sc, BGE_RXLP_LOCSTAT_OUT_OF_BDS);
4891 * Unlike other controllers, BGE_RXLP_LOCSTAT_IFIN_DROPS
4892 * counter of BCM5717, BCM5718, BCM5719 A0 and BCM5720 A0
4893 * includes number of unwanted multicast frames. This comes
4894 * from silicon bug and known workaround to get rough(not
4895 * exact) counter is to enable interrupt on MBUF low water
4896 * attention. This can be accomplished by setting
4897 * BGE_HCCMODE_ATTN bit of BGE_HCC_MODE,
4898 * BGE_BMANMODE_LOMBUF_ATTN bit of BGE_BMAN_MODE and
4899 * BGE_MODECTL_FLOWCTL_ATTN_INTR bit of BGE_MODE_CTL.
4900 * However that change would generate more interrupts and
4901 * there are still possibilities of losing multiple frames
4902 * during BGE_MODECTL_FLOWCTL_ATTN_INTR interrupt handling.
4903 * Given that the workaround still would not get correct
4904 * counter I don't think it's worth to implement it. So
4905 * ignore reading the counter on controllers that have the
4908 if (sc->bge_asicrev != BGE_ASICREV_BCM5717 &&
4909 sc->bge_chipid != BGE_CHIPID_BCM5719_A0 &&
4910 sc->bge_chipid != BGE_CHIPID_BCM5720_A0)
4911 stats->InputDiscards +=
4912 CSR_READ_4(sc, BGE_RXLP_LOCSTAT_IFIN_DROPS);
4913 stats->InputErrors +=
4914 CSR_READ_4(sc, BGE_RXLP_LOCSTAT_IFIN_ERRORS);
4915 stats->RecvThresholdHit +=
4916 CSR_READ_4(sc, BGE_RXLP_LOCSTAT_RXTHRESH_HIT);
4918 ifp->if_collisions = (u_long)stats->etherStatsCollisions;
4919 ifp->if_ierrors = (u_long)(stats->NoMoreRxBDs + stats->InputDiscards +
4920 stats->InputErrors);
4922 if (sc->bge_flags & BGE_FLAG_RDMA_BUG) {
4924 * If controller transmitted more than BGE_NUM_RDMA_CHANNELS
4925 * frames, it's safe to disable workaround for DMA engine's
4926 * miscalculation of TXMBUF space.
4928 if (stats->ifHCOutUcastPkts + stats->ifHCOutMulticastPkts +
4929 stats->ifHCOutBroadcastPkts > BGE_NUM_RDMA_CHANNELS) {
4930 val = CSR_READ_4(sc, BGE_RDMA_LSO_CRPTEN_CTRL);
4931 if (sc->bge_asicrev == BGE_ASICREV_BCM5719)
4932 val &= ~BGE_RDMA_TX_LENGTH_WA_5719;
4934 val &= ~BGE_RDMA_TX_LENGTH_WA_5720;
4935 CSR_WRITE_4(sc, BGE_RDMA_LSO_CRPTEN_CTRL, val);
4936 sc->bge_flags &= ~BGE_FLAG_RDMA_BUG;
4942 bge_stats_clear_regs(struct bge_softc *sc)
4945 CSR_READ_4(sc, BGE_TX_MAC_STATS_OCTETS);
4946 CSR_READ_4(sc, BGE_TX_MAC_STATS_COLLS);
4947 CSR_READ_4(sc, BGE_TX_MAC_STATS_XON_SENT);
4948 CSR_READ_4(sc, BGE_TX_MAC_STATS_XOFF_SENT);
4949 CSR_READ_4(sc, BGE_TX_MAC_STATS_ERRORS);
4950 CSR_READ_4(sc, BGE_TX_MAC_STATS_SINGLE_COLL);
4951 CSR_READ_4(sc, BGE_TX_MAC_STATS_MULTI_COLL);
4952 CSR_READ_4(sc, BGE_TX_MAC_STATS_DEFERRED);
4953 CSR_READ_4(sc, BGE_TX_MAC_STATS_EXCESS_COLL);
4954 CSR_READ_4(sc, BGE_TX_MAC_STATS_LATE_COLL);
4955 CSR_READ_4(sc, BGE_TX_MAC_STATS_UCAST);
4956 CSR_READ_4(sc, BGE_TX_MAC_STATS_MCAST);
4957 CSR_READ_4(sc, BGE_TX_MAC_STATS_BCAST);
4959 CSR_READ_4(sc, BGE_RX_MAC_STATS_OCTESTS);
4960 CSR_READ_4(sc, BGE_RX_MAC_STATS_FRAGMENTS);
4961 CSR_READ_4(sc, BGE_RX_MAC_STATS_UCAST);
4962 CSR_READ_4(sc, BGE_RX_MAC_STATS_MCAST);
4963 CSR_READ_4(sc, BGE_RX_MAC_STATS_BCAST);
4964 CSR_READ_4(sc, BGE_RX_MAC_STATS_FCS_ERRORS);
4965 CSR_READ_4(sc, BGE_RX_MAC_STATS_ALGIN_ERRORS);
4966 CSR_READ_4(sc, BGE_RX_MAC_STATS_XON_RCVD);
4967 CSR_READ_4(sc, BGE_RX_MAC_STATS_XOFF_RCVD);
4968 CSR_READ_4(sc, BGE_RX_MAC_STATS_CTRL_RCVD);
4969 CSR_READ_4(sc, BGE_RX_MAC_STATS_XOFF_ENTERED);
4970 CSR_READ_4(sc, BGE_RX_MAC_STATS_FRAME_TOO_LONG);
4971 CSR_READ_4(sc, BGE_RX_MAC_STATS_JABBERS);
4972 CSR_READ_4(sc, BGE_RX_MAC_STATS_UNDERSIZE);
4974 CSR_READ_4(sc, BGE_RXLP_LOCSTAT_FILTDROP);
4975 CSR_READ_4(sc, BGE_RXLP_LOCSTAT_DMA_WRQ_FULL);
4976 CSR_READ_4(sc, BGE_RXLP_LOCSTAT_DMA_HPWRQ_FULL);
4977 CSR_READ_4(sc, BGE_RXLP_LOCSTAT_OUT_OF_BDS);
4978 CSR_READ_4(sc, BGE_RXLP_LOCSTAT_IFIN_DROPS);
4979 CSR_READ_4(sc, BGE_RXLP_LOCSTAT_IFIN_ERRORS);
4980 CSR_READ_4(sc, BGE_RXLP_LOCSTAT_RXTHRESH_HIT);
4984 bge_stats_update(struct bge_softc *sc)
4988 uint32_t cnt; /* current register value */
4992 stats = BGE_MEMWIN_START + BGE_STATS_BLOCK;
4994 #define READ_STAT(sc, stats, stat) \
4995 CSR_READ_4(sc, stats + offsetof(struct bge_stats, stat))
4997 cnt = READ_STAT(sc, stats, txstats.etherStatsCollisions.bge_addr_lo);
4998 ifp->if_collisions += (uint32_t)(cnt - sc->bge_tx_collisions);
4999 sc->bge_tx_collisions = cnt;
5001 cnt = READ_STAT(sc, stats, nicNoMoreRxBDs.bge_addr_lo);
5002 ifp->if_ierrors += (uint32_t)(cnt - sc->bge_rx_nobds);
5003 sc->bge_rx_nobds = cnt;
5004 cnt = READ_STAT(sc, stats, ifInErrors.bge_addr_lo);
5005 ifp->if_ierrors += (uint32_t)(cnt - sc->bge_rx_inerrs);
5006 sc->bge_rx_inerrs = cnt;
5007 cnt = READ_STAT(sc, stats, ifInDiscards.bge_addr_lo);
5008 ifp->if_ierrors += (uint32_t)(cnt - sc->bge_rx_discards);
5009 sc->bge_rx_discards = cnt;
5011 cnt = READ_STAT(sc, stats, txstats.ifOutDiscards.bge_addr_lo);
5012 ifp->if_oerrors += (uint32_t)(cnt - sc->bge_tx_discards);
5013 sc->bge_tx_discards = cnt;
5019 * Pad outbound frame to ETHER_MIN_NOPAD for an unusual reason.
5020 * The bge hardware will pad out Tx runts to ETHER_MIN_NOPAD,
5021 * but when such padded frames employ the bge IP/TCP checksum offload,
5022 * the hardware checksum assist gives incorrect results (possibly
5023 * from incorporating its own padding into the UDP/TCP checksum; who knows).
5024 * If we pad such runts with zeros, the onboard checksum comes out correct.
5027 bge_cksum_pad(struct mbuf *m)
5029 int padlen = ETHER_MIN_NOPAD - m->m_pkthdr.len;
5032 /* If there's only the packet-header and we can pad there, use it. */
5033 if (m->m_pkthdr.len == m->m_len && M_WRITABLE(m) &&
5034 M_TRAILINGSPACE(m) >= padlen) {
5038 * Walk packet chain to find last mbuf. We will either
5039 * pad there, or append a new mbuf and pad it.
5041 for (last = m; last->m_next != NULL; last = last->m_next);
5042 if (!(M_WRITABLE(last) && M_TRAILINGSPACE(last) >= padlen)) {
5043 /* Allocate new empty mbuf, pad it. Compact later. */
5046 MGET(n, M_NOWAIT, MT_DATA);
5055 /* Now zero the pad area, to avoid the bge cksum-assist bug. */
5056 memset(mtod(last, caddr_t) + last->m_len, 0, padlen);
5057 last->m_len += padlen;
5058 m->m_pkthdr.len += padlen;
5063 static struct mbuf *
5064 bge_check_short_dma(struct mbuf *m)
5070 * If device receive two back-to-back send BDs with less than
5071 * or equal to 8 total bytes then the device may hang. The two
5072 * back-to-back send BDs must in the same frame for this failure
5073 * to occur. Scan mbuf chains and see whether two back-to-back
5074 * send BDs are there. If this is the case, allocate new mbuf
5075 * and copy the frame to workaround the silicon bug.
5077 for (n = m, found = 0; n != NULL; n = n->m_next) {
5088 n = m_defrag(m, M_NOWAIT);
5096 static struct mbuf *
5097 bge_setup_tso(struct bge_softc *sc, struct mbuf *m, uint16_t *mss,
5106 if (M_WRITABLE(m) == 0) {
5107 /* Get a writable copy. */
5108 n = m_dup(m, M_NOWAIT);
5114 m = m_pullup(m, sizeof(struct ether_header) + sizeof(struct ip));
5117 ip = (struct ip *)(mtod(m, char *) + sizeof(struct ether_header));
5118 poff = sizeof(struct ether_header) + (ip->ip_hl << 2);
5119 m = m_pullup(m, poff + sizeof(struct tcphdr));
5122 tcp = (struct tcphdr *)(mtod(m, char *) + poff);
5123 m = m_pullup(m, poff + (tcp->th_off << 2));
5127 * It seems controller doesn't modify IP length and TCP pseudo
5128 * checksum. These checksum computed by upper stack should be 0.
5130 *mss = m->m_pkthdr.tso_segsz;
5131 ip = (struct ip *)(mtod(m, char *) + sizeof(struct ether_header));
5133 ip->ip_len = htons(*mss + (ip->ip_hl << 2) + (tcp->th_off << 2));
5134 /* Clear pseudo checksum computed by TCP stack. */
5135 tcp = (struct tcphdr *)(mtod(m, char *) + poff);
5138 * Broadcom controllers uses different descriptor format for
5139 * TSO depending on ASIC revision. Due to TSO-capable firmware
5140 * license issue and lower performance of firmware based TSO
5141 * we only support hardware based TSO.
5143 /* Calculate header length, incl. TCP/IP options, in 32 bit units. */
5144 hlen = ((ip->ip_hl << 2) + (tcp->th_off << 2)) >> 2;
5145 if (sc->bge_flags & BGE_FLAG_TSO3) {
5147 * For BCM5717 and newer controllers, hardware based TSO
5148 * uses the 14 lower bits of the bge_mss field to store the
5149 * MSS and the upper 2 bits to store the lowest 2 bits of
5150 * the IP/TCP header length. The upper 6 bits of the header
5151 * length are stored in the bge_flags[14:10,4] field. Jumbo
5152 * frames are supported.
5154 *mss |= ((hlen & 0x3) << 14);
5155 *flags |= ((hlen & 0xF8) << 7) | ((hlen & 0x4) << 2);
5158 * For BCM5755 and newer controllers, hardware based TSO uses
5159 * the lower 11 bits to store the MSS and the upper 5 bits to
5160 * store the IP/TCP header length. Jumbo frames are not
5163 *mss |= (hlen << 11);
5169 * Encapsulate an mbuf chain in the tx ring by coupling the mbuf data
5170 * pointers to descriptors.
5173 bge_encap(struct bge_softc *sc, struct mbuf **m_head, uint32_t *txidx)
5175 bus_dma_segment_t segs[BGE_NSEG_NEW];
5177 struct bge_tx_bd *d;
5178 struct mbuf *m = *m_head;
5179 uint32_t idx = *txidx;
5180 uint16_t csum_flags, mss, vlan_tag;
5181 int nsegs, i, error;
5186 if ((sc->bge_flags & BGE_FLAG_SHORT_DMA_BUG) != 0 &&
5187 m->m_next != NULL) {
5188 *m_head = bge_check_short_dma(m);
5189 if (*m_head == NULL)
5193 if ((m->m_pkthdr.csum_flags & CSUM_TSO) != 0) {
5194 *m_head = m = bge_setup_tso(sc, m, &mss, &csum_flags);
5195 if (*m_head == NULL)
5197 csum_flags |= BGE_TXBDFLAG_CPU_PRE_DMA |
5198 BGE_TXBDFLAG_CPU_POST_DMA;
5199 } else if ((m->m_pkthdr.csum_flags & sc->bge_csum_features) != 0) {
5200 if (m->m_pkthdr.csum_flags & CSUM_IP)
5201 csum_flags |= BGE_TXBDFLAG_IP_CSUM;
5202 if (m->m_pkthdr.csum_flags & (CSUM_TCP | CSUM_UDP)) {
5203 csum_flags |= BGE_TXBDFLAG_TCP_UDP_CSUM;
5204 if (m->m_pkthdr.len < ETHER_MIN_NOPAD &&
5205 (error = bge_cksum_pad(m)) != 0) {
5213 if ((m->m_pkthdr.csum_flags & CSUM_TSO) == 0) {
5214 if (sc->bge_flags & BGE_FLAG_JUMBO_FRAME &&
5215 m->m_pkthdr.len > ETHER_MAX_LEN)
5216 csum_flags |= BGE_TXBDFLAG_JUMBO_FRAME;
5217 if (sc->bge_forced_collapse > 0 &&
5218 (sc->bge_flags & BGE_FLAG_PCIE) != 0 && m->m_next != NULL) {
5220 * Forcedly collapse mbuf chains to overcome hardware
5221 * limitation which only support a single outstanding
5222 * DMA read operation.
5224 if (sc->bge_forced_collapse == 1)
5225 m = m_defrag(m, M_NOWAIT);
5227 m = m_collapse(m, M_NOWAIT,
5228 sc->bge_forced_collapse);
5235 map = sc->bge_cdata.bge_tx_dmamap[idx];
5236 error = bus_dmamap_load_mbuf_sg(sc->bge_cdata.bge_tx_mtag, map, m, segs,
5237 &nsegs, BUS_DMA_NOWAIT);
5238 if (error == EFBIG) {
5239 m = m_collapse(m, M_NOWAIT, BGE_NSEG_NEW);
5246 error = bus_dmamap_load_mbuf_sg(sc->bge_cdata.bge_tx_mtag, map,
5247 m, segs, &nsegs, BUS_DMA_NOWAIT);
5253 } else if (error != 0)
5256 /* Check if we have enough free send BDs. */
5257 if (sc->bge_txcnt + nsegs >= BGE_TX_RING_CNT) {
5258 bus_dmamap_unload(sc->bge_cdata.bge_tx_mtag, map);
5262 bus_dmamap_sync(sc->bge_cdata.bge_tx_mtag, map, BUS_DMASYNC_PREWRITE);
5264 if (m->m_flags & M_VLANTAG) {
5265 csum_flags |= BGE_TXBDFLAG_VLAN_TAG;
5266 vlan_tag = m->m_pkthdr.ether_vtag;
5269 if (sc->bge_asicrev == BGE_ASICREV_BCM5762 &&
5270 (m->m_pkthdr.csum_flags & CSUM_TSO) != 0) {
5272 * 5725 family of devices corrupts TSO packets when TSO DMA
5273 * buffers cross into regions which are within MSS bytes of
5274 * a 4GB boundary. If we encounter the condition, drop the
5277 for (i = 0; ; i++) {
5278 d = &sc->bge_ldata.bge_tx_ring[idx];
5279 d->bge_addr.bge_addr_lo = BGE_ADDR_LO(segs[i].ds_addr);
5280 d->bge_addr.bge_addr_hi = BGE_ADDR_HI(segs[i].ds_addr);
5281 d->bge_len = segs[i].ds_len;
5282 if (d->bge_addr.bge_addr_lo + segs[i].ds_len + mss <
5283 d->bge_addr.bge_addr_lo)
5285 d->bge_flags = csum_flags;
5286 d->bge_vlan_tag = vlan_tag;
5290 BGE_INC(idx, BGE_TX_RING_CNT);
5292 if (i != nsegs - 1) {
5293 bus_dmamap_sync(sc->bge_cdata.bge_tx_mtag, map,
5294 BUS_DMASYNC_POSTWRITE);
5295 bus_dmamap_unload(sc->bge_cdata.bge_tx_mtag, map);
5301 for (i = 0; ; i++) {
5302 d = &sc->bge_ldata.bge_tx_ring[idx];
5303 d->bge_addr.bge_addr_lo = BGE_ADDR_LO(segs[i].ds_addr);
5304 d->bge_addr.bge_addr_hi = BGE_ADDR_HI(segs[i].ds_addr);
5305 d->bge_len = segs[i].ds_len;
5306 d->bge_flags = csum_flags;
5307 d->bge_vlan_tag = vlan_tag;
5311 BGE_INC(idx, BGE_TX_RING_CNT);
5315 /* Mark the last segment as end of packet... */
5316 d->bge_flags |= BGE_TXBDFLAG_END;
5319 * Insure that the map for this transmission
5320 * is placed at the array index of the last descriptor
5323 sc->bge_cdata.bge_tx_dmamap[*txidx] = sc->bge_cdata.bge_tx_dmamap[idx];
5324 sc->bge_cdata.bge_tx_dmamap[idx] = map;
5325 sc->bge_cdata.bge_tx_chain[idx] = m;
5326 sc->bge_txcnt += nsegs;
5328 BGE_INC(idx, BGE_TX_RING_CNT);
5335 * Main transmit routine. To avoid having to do mbuf copies, we put pointers
5336 * to the mbuf data regions directly in the transmit descriptors.
5339 bge_start_locked(struct ifnet *ifp)
5341 struct bge_softc *sc;
5342 struct mbuf *m_head;
5347 BGE_LOCK_ASSERT(sc);
5349 if (!sc->bge_link ||
5350 (ifp->if_drv_flags & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) !=
5354 prodidx = sc->bge_tx_prodidx;
5356 for (count = 0; !IFQ_DRV_IS_EMPTY(&ifp->if_snd);) {
5357 if (sc->bge_txcnt > BGE_TX_RING_CNT - 16) {
5358 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
5361 IFQ_DRV_DEQUEUE(&ifp->if_snd, m_head);
5366 * Pack the data into the transmit ring. If we
5367 * don't have room, set the OACTIVE flag and wait
5368 * for the NIC to drain the ring.
5370 if (bge_encap(sc, &m_head, &prodidx)) {
5373 IFQ_DRV_PREPEND(&ifp->if_snd, m_head);
5374 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
5380 * If there's a BPF listener, bounce a copy of this frame
5383 #ifdef ETHER_BPF_MTAP
5384 ETHER_BPF_MTAP(ifp, m_head);
5386 BPF_MTAP(ifp, m_head);
5391 bus_dmamap_sync(sc->bge_cdata.bge_tx_ring_tag,
5392 sc->bge_cdata.bge_tx_ring_map, BUS_DMASYNC_PREWRITE);
5394 bge_writembx(sc, BGE_MBX_TX_HOST_PROD0_LO, prodidx);
5395 /* 5700 b2 errata */
5396 if (sc->bge_chiprev == BGE_CHIPREV_5700_BX)
5397 bge_writembx(sc, BGE_MBX_TX_HOST_PROD0_LO, prodidx);
5399 sc->bge_tx_prodidx = prodidx;
5402 * Set a timeout in case the chip goes out to lunch.
5404 sc->bge_timer = BGE_TX_TIMEOUT;
5409 * Main transmit routine. To avoid having to do mbuf copies, we put pointers
5410 * to the mbuf data regions directly in the transmit descriptors.
5413 bge_start(struct ifnet *ifp)
5415 struct bge_softc *sc;
5419 bge_start_locked(ifp);
5424 bge_init_locked(struct bge_softc *sc)
5430 BGE_LOCK_ASSERT(sc);
5434 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
5437 /* Cancel pending I/O and flush buffers. */
5441 bge_sig_pre_reset(sc, BGE_RESET_START);
5443 bge_sig_legacy(sc, BGE_RESET_START);
5444 bge_sig_post_reset(sc, BGE_RESET_START);
5449 * Init the various state machines, ring
5450 * control blocks and firmware.
5452 if (bge_blockinit(sc)) {
5453 device_printf(sc->bge_dev, "initialization failure\n");
5460 CSR_WRITE_4(sc, BGE_RX_MTU, ifp->if_mtu +
5461 ETHER_HDR_LEN + ETHER_CRC_LEN +
5462 (ifp->if_capenable & IFCAP_VLAN_MTU ? ETHER_VLAN_ENCAP_LEN : 0));
5464 /* Load our MAC address. */
5465 m = (uint16_t *)IF_LLADDR(sc->bge_ifp);
5466 CSR_WRITE_4(sc, BGE_MAC_ADDR1_LO, htons(m[0]));
5467 CSR_WRITE_4(sc, BGE_MAC_ADDR1_HI, (htons(m[1]) << 16) | htons(m[2]));
5469 /* Program promiscuous mode. */
5472 /* Program multicast filter. */
5475 /* Program VLAN tag stripping. */
5478 /* Override UDP checksum offloading. */
5479 if (sc->bge_forced_udpcsum == 0)
5480 sc->bge_csum_features &= ~CSUM_UDP;
5482 sc->bge_csum_features |= CSUM_UDP;
5483 if (ifp->if_capabilities & IFCAP_TXCSUM &&
5484 ifp->if_capenable & IFCAP_TXCSUM) {
5485 ifp->if_hwassist &= ~(BGE_CSUM_FEATURES | CSUM_UDP);
5486 ifp->if_hwassist |= sc->bge_csum_features;
5490 if (bge_init_rx_ring_std(sc) != 0) {
5491 device_printf(sc->bge_dev, "no memory for std Rx buffers.\n");
5497 * Workaround for a bug in 5705 ASIC rev A0. Poll the NIC's
5498 * memory to insure that the chip has in fact read the first
5499 * entry of the ring.
5501 if (sc->bge_chipid == BGE_CHIPID_BCM5705_A0) {
5503 for (i = 0; i < 10; i++) {
5505 v = bge_readmem_ind(sc, BGE_STD_RX_RINGS + 8);
5506 if (v == (MCLBYTES - ETHER_ALIGN))
5510 device_printf (sc->bge_dev,
5511 "5705 A0 chip failed to load RX ring\n");
5514 /* Init jumbo RX ring. */
5515 if (BGE_IS_JUMBO_CAPABLE(sc) &&
5516 ifp->if_mtu + ETHER_HDR_LEN + ETHER_CRC_LEN + ETHER_VLAN_ENCAP_LEN >
5517 (MCLBYTES - ETHER_ALIGN)) {
5518 if (bge_init_rx_ring_jumbo(sc) != 0) {
5519 device_printf(sc->bge_dev,
5520 "no memory for jumbo Rx buffers.\n");
5526 /* Init our RX return ring index. */
5527 sc->bge_rx_saved_considx = 0;
5529 /* Init our RX/TX stat counters. */
5530 sc->bge_rx_discards = sc->bge_tx_discards = sc->bge_tx_collisions = 0;
5533 bge_init_tx_ring(sc);
5535 /* Enable TX MAC state machine lockup fix. */
5536 mode = CSR_READ_4(sc, BGE_TX_MODE);
5537 if (BGE_IS_5755_PLUS(sc) || sc->bge_asicrev == BGE_ASICREV_BCM5906)
5538 mode |= BGE_TXMODE_MBUF_LOCKUP_FIX;
5539 if (sc->bge_asicrev == BGE_ASICREV_BCM5720 ||
5540 sc->bge_asicrev == BGE_ASICREV_BCM5762) {
5541 mode &= ~(BGE_TXMODE_JMB_FRM_LEN | BGE_TXMODE_CNT_DN_MODE);
5542 mode |= CSR_READ_4(sc, BGE_TX_MODE) &
5543 (BGE_TXMODE_JMB_FRM_LEN | BGE_TXMODE_CNT_DN_MODE);
5545 /* Turn on transmitter. */
5546 CSR_WRITE_4(sc, BGE_TX_MODE, mode | BGE_TXMODE_ENABLE);
5549 /* Turn on receiver. */
5550 mode = CSR_READ_4(sc, BGE_RX_MODE);
5551 if (BGE_IS_5755_PLUS(sc))
5552 mode |= BGE_RXMODE_IPV6_ENABLE;
5553 if (sc->bge_asicrev == BGE_ASICREV_BCM5762)
5554 mode |= BGE_RXMODE_IPV4_FRAG_FIX;
5555 CSR_WRITE_4(sc,BGE_RX_MODE, mode | BGE_RXMODE_ENABLE);
5559 * Set the number of good frames to receive after RX MBUF
5560 * Low Watermark has been reached. After the RX MAC receives
5561 * this number of frames, it will drop subsequent incoming
5562 * frames until the MBUF High Watermark is reached.
5564 if (BGE_IS_57765_PLUS(sc))
5565 CSR_WRITE_4(sc, BGE_MAX_RX_FRAME_LOWAT, 1);
5567 CSR_WRITE_4(sc, BGE_MAX_RX_FRAME_LOWAT, 2);
5569 /* Clear MAC statistics. */
5570 if (BGE_IS_5705_PLUS(sc))
5571 bge_stats_clear_regs(sc);
5573 /* Tell firmware we're alive. */
5574 BGE_SETBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
5576 #ifdef DEVICE_POLLING
5577 /* Disable interrupts if we are polling. */
5578 if (ifp->if_capenable & IFCAP_POLLING) {
5579 BGE_SETBIT(sc, BGE_PCI_MISC_CTL,
5580 BGE_PCIMISCCTL_MASK_PCI_INTR);
5581 bge_writembx(sc, BGE_MBX_IRQ0_LO, 1);
5585 /* Enable host interrupts. */
5587 BGE_SETBIT(sc, BGE_PCI_MISC_CTL, BGE_PCIMISCCTL_CLEAR_INTA);
5588 BGE_CLRBIT(sc, BGE_PCI_MISC_CTL, BGE_PCIMISCCTL_MASK_PCI_INTR);
5589 bge_writembx(sc, BGE_MBX_IRQ0_LO, 0);
5592 ifp->if_drv_flags |= IFF_DRV_RUNNING;
5593 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
5595 bge_ifmedia_upd_locked(ifp);
5597 callout_reset(&sc->bge_stat_ch, hz, bge_tick, sc);
5603 struct bge_softc *sc = xsc;
5606 bge_init_locked(sc);
5611 * Set media options.
5614 bge_ifmedia_upd(struct ifnet *ifp)
5616 struct bge_softc *sc = ifp->if_softc;
5620 res = bge_ifmedia_upd_locked(ifp);
5627 bge_ifmedia_upd_locked(struct ifnet *ifp)
5629 struct bge_softc *sc = ifp->if_softc;
5630 struct mii_data *mii;
5631 struct mii_softc *miisc;
5632 struct ifmedia *ifm;
5634 BGE_LOCK_ASSERT(sc);
5636 ifm = &sc->bge_ifmedia;
5638 /* If this is a 1000baseX NIC, enable the TBI port. */
5639 if (sc->bge_flags & BGE_FLAG_TBI) {
5640 if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
5642 switch(IFM_SUBTYPE(ifm->ifm_media)) {
5645 * The BCM5704 ASIC appears to have a special
5646 * mechanism for programming the autoneg
5647 * advertisement registers in TBI mode.
5649 if (sc->bge_asicrev == BGE_ASICREV_BCM5704) {
5651 sgdig = CSR_READ_4(sc, BGE_SGDIG_STS);
5652 if (sgdig & BGE_SGDIGSTS_DONE) {
5653 CSR_WRITE_4(sc, BGE_TX_TBI_AUTONEG, 0);
5654 sgdig = CSR_READ_4(sc, BGE_SGDIG_CFG);
5655 sgdig |= BGE_SGDIGCFG_AUTO |
5656 BGE_SGDIGCFG_PAUSE_CAP |
5657 BGE_SGDIGCFG_ASYM_PAUSE;
5658 CSR_WRITE_4(sc, BGE_SGDIG_CFG,
5659 sgdig | BGE_SGDIGCFG_SEND);
5661 CSR_WRITE_4(sc, BGE_SGDIG_CFG, sgdig);
5666 if ((ifm->ifm_media & IFM_GMASK) == IFM_FDX) {
5667 BGE_CLRBIT(sc, BGE_MAC_MODE,
5668 BGE_MACMODE_HALF_DUPLEX);
5670 BGE_SETBIT(sc, BGE_MAC_MODE,
5671 BGE_MACMODE_HALF_DUPLEX);
5682 mii = device_get_softc(sc->bge_miibus);
5683 LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
5688 * Force an interrupt so that we will call bge_link_upd
5689 * if needed and clear any pending link state attention.
5690 * Without this we are not getting any further interrupts
5691 * for link state changes and thus will not UP the link and
5692 * not be able to send in bge_start_locked. The only
5693 * way to get things working was to receive a packet and
5695 * bge_tick should help for fiber cards and we might not
5696 * need to do this here if BGE_FLAG_TBI is set but as
5697 * we poll for fiber anyway it should not harm.
5699 if (sc->bge_asicrev == BGE_ASICREV_BCM5700 ||
5700 sc->bge_flags & BGE_FLAG_5788)
5701 BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_INTR_SET);
5703 BGE_SETBIT(sc, BGE_HCC_MODE, BGE_HCCMODE_COAL_NOW);
5709 * Report current media status.
5712 bge_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
5714 struct bge_softc *sc = ifp->if_softc;
5715 struct mii_data *mii;
5719 if ((ifp->if_flags & IFF_UP) == 0) {
5723 if (sc->bge_flags & BGE_FLAG_TBI) {
5724 ifmr->ifm_status = IFM_AVALID;
5725 ifmr->ifm_active = IFM_ETHER;
5726 if (CSR_READ_4(sc, BGE_MAC_STS) &
5727 BGE_MACSTAT_TBI_PCS_SYNCHED)
5728 ifmr->ifm_status |= IFM_ACTIVE;
5730 ifmr->ifm_active |= IFM_NONE;
5734 ifmr->ifm_active |= IFM_1000_SX;
5735 if (CSR_READ_4(sc, BGE_MAC_MODE) & BGE_MACMODE_HALF_DUPLEX)
5736 ifmr->ifm_active |= IFM_HDX;
5738 ifmr->ifm_active |= IFM_FDX;
5743 mii = device_get_softc(sc->bge_miibus);
5745 ifmr->ifm_active = mii->mii_media_active;
5746 ifmr->ifm_status = mii->mii_media_status;
5752 bge_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
5754 struct bge_softc *sc = ifp->if_softc;
5755 struct ifreq *ifr = (struct ifreq *) data;
5756 struct mii_data *mii;
5757 int flags, mask, error = 0;
5761 if (BGE_IS_JUMBO_CAPABLE(sc) ||
5762 (sc->bge_flags & BGE_FLAG_JUMBO_STD)) {
5763 if (ifr->ifr_mtu < ETHERMIN ||
5764 ifr->ifr_mtu > BGE_JUMBO_MTU) {
5768 } else if (ifr->ifr_mtu < ETHERMIN || ifr->ifr_mtu > ETHERMTU) {
5773 if (ifp->if_mtu != ifr->ifr_mtu) {
5774 ifp->if_mtu = ifr->ifr_mtu;
5775 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
5776 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
5777 bge_init_locked(sc);
5784 if (ifp->if_flags & IFF_UP) {
5786 * If only the state of the PROMISC flag changed,
5787 * then just use the 'set promisc mode' command
5788 * instead of reinitializing the entire NIC. Doing
5789 * a full re-init means reloading the firmware and
5790 * waiting for it to start up, which may take a
5791 * second or two. Similarly for ALLMULTI.
5793 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
5794 flags = ifp->if_flags ^ sc->bge_if_flags;
5795 if (flags & IFF_PROMISC)
5797 if (flags & IFF_ALLMULTI)
5800 bge_init_locked(sc);
5802 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
5806 sc->bge_if_flags = ifp->if_flags;
5812 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
5821 if (sc->bge_flags & BGE_FLAG_TBI) {
5822 error = ifmedia_ioctl(ifp, ifr,
5823 &sc->bge_ifmedia, command);
5825 mii = device_get_softc(sc->bge_miibus);
5826 error = ifmedia_ioctl(ifp, ifr,
5827 &mii->mii_media, command);
5831 mask = ifr->ifr_reqcap ^ ifp->if_capenable;
5832 #ifdef DEVICE_POLLING
5833 if (mask & IFCAP_POLLING) {
5834 if (ifr->ifr_reqcap & IFCAP_POLLING) {
5835 error = ether_poll_register(bge_poll, ifp);
5839 BGE_SETBIT(sc, BGE_PCI_MISC_CTL,
5840 BGE_PCIMISCCTL_MASK_PCI_INTR);
5841 bge_writembx(sc, BGE_MBX_IRQ0_LO, 1);
5842 ifp->if_capenable |= IFCAP_POLLING;
5845 error = ether_poll_deregister(ifp);
5846 /* Enable interrupt even in error case */
5848 BGE_CLRBIT(sc, BGE_PCI_MISC_CTL,
5849 BGE_PCIMISCCTL_MASK_PCI_INTR);
5850 bge_writembx(sc, BGE_MBX_IRQ0_LO, 0);
5851 ifp->if_capenable &= ~IFCAP_POLLING;
5856 if ((mask & IFCAP_TXCSUM) != 0 &&
5857 (ifp->if_capabilities & IFCAP_TXCSUM) != 0) {
5858 ifp->if_capenable ^= IFCAP_TXCSUM;
5859 if ((ifp->if_capenable & IFCAP_TXCSUM) != 0)
5860 ifp->if_hwassist |= sc->bge_csum_features;
5862 ifp->if_hwassist &= ~sc->bge_csum_features;
5865 if ((mask & IFCAP_RXCSUM) != 0 &&
5866 (ifp->if_capabilities & IFCAP_RXCSUM) != 0)
5867 ifp->if_capenable ^= IFCAP_RXCSUM;
5869 if ((mask & IFCAP_TSO4) != 0 &&
5870 (ifp->if_capabilities & IFCAP_TSO4) != 0) {
5871 ifp->if_capenable ^= IFCAP_TSO4;
5872 if ((ifp->if_capenable & IFCAP_TSO4) != 0)
5873 ifp->if_hwassist |= CSUM_TSO;
5875 ifp->if_hwassist &= ~CSUM_TSO;
5878 if (mask & IFCAP_VLAN_MTU) {
5879 ifp->if_capenable ^= IFCAP_VLAN_MTU;
5880 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
5884 if ((mask & IFCAP_VLAN_HWTSO) != 0 &&
5885 (ifp->if_capabilities & IFCAP_VLAN_HWTSO) != 0)
5886 ifp->if_capenable ^= IFCAP_VLAN_HWTSO;
5887 if ((mask & IFCAP_VLAN_HWTAGGING) != 0 &&
5888 (ifp->if_capabilities & IFCAP_VLAN_HWTAGGING) != 0) {
5889 ifp->if_capenable ^= IFCAP_VLAN_HWTAGGING;
5890 if ((ifp->if_capenable & IFCAP_VLAN_HWTAGGING) == 0)
5891 ifp->if_capenable &= ~IFCAP_VLAN_HWTSO;
5896 #ifdef VLAN_CAPABILITIES
5897 VLAN_CAPABILITIES(ifp);
5901 error = ether_ioctl(ifp, command, data);
5909 bge_watchdog(struct bge_softc *sc)
5914 BGE_LOCK_ASSERT(sc);
5916 if (sc->bge_timer == 0 || --sc->bge_timer)
5919 /* If pause frames are active then don't reset the hardware. */
5920 if ((CSR_READ_4(sc, BGE_RX_MODE) & BGE_RXMODE_FLOWCTL_ENABLE) != 0) {
5921 status = CSR_READ_4(sc, BGE_RX_STS);
5922 if ((status & BGE_RXSTAT_REMOTE_XOFFED) != 0) {
5924 * If link partner has us in XOFF state then wait for
5925 * the condition to clear.
5927 CSR_WRITE_4(sc, BGE_RX_STS, status);
5928 sc->bge_timer = BGE_TX_TIMEOUT;
5930 } else if ((status & BGE_RXSTAT_RCVD_XOFF) != 0 &&
5931 (status & BGE_RXSTAT_RCVD_XON) != 0) {
5933 * If link partner has us in XOFF state then wait for
5934 * the condition to clear.
5936 CSR_WRITE_4(sc, BGE_RX_STS, status);
5937 sc->bge_timer = BGE_TX_TIMEOUT;
5941 * Any other condition is unexpected and the controller
5948 if_printf(ifp, "watchdog timeout -- resetting\n");
5950 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
5951 bge_init_locked(sc);
5957 bge_stop_block(struct bge_softc *sc, bus_size_t reg, uint32_t bit)
5961 BGE_CLRBIT(sc, reg, bit);
5963 for (i = 0; i < BGE_TIMEOUT; i++) {
5964 if ((CSR_READ_4(sc, reg) & bit) == 0)
5971 * Stop the adapter and free any mbufs allocated to the
5975 bge_stop(struct bge_softc *sc)
5979 BGE_LOCK_ASSERT(sc);
5983 callout_stop(&sc->bge_stat_ch);
5985 /* Disable host interrupts. */
5986 BGE_SETBIT(sc, BGE_PCI_MISC_CTL, BGE_PCIMISCCTL_MASK_PCI_INTR);
5987 bge_writembx(sc, BGE_MBX_IRQ0_LO, 1);
5990 * Tell firmware we're shutting down.
5993 bge_sig_pre_reset(sc, BGE_RESET_SHUTDOWN);
5996 * Disable all of the receiver blocks.
5998 bge_stop_block(sc, BGE_RX_MODE, BGE_RXMODE_ENABLE);
5999 bge_stop_block(sc, BGE_RBDI_MODE, BGE_RBDIMODE_ENABLE);
6000 bge_stop_block(sc, BGE_RXLP_MODE, BGE_RXLPMODE_ENABLE);
6001 if (BGE_IS_5700_FAMILY(sc))
6002 bge_stop_block(sc, BGE_RXLS_MODE, BGE_RXLSMODE_ENABLE);
6003 bge_stop_block(sc, BGE_RDBDI_MODE, BGE_RBDIMODE_ENABLE);
6004 bge_stop_block(sc, BGE_RDC_MODE, BGE_RDCMODE_ENABLE);
6005 bge_stop_block(sc, BGE_RBDC_MODE, BGE_RBDCMODE_ENABLE);
6008 * Disable all of the transmit blocks.
6010 bge_stop_block(sc, BGE_SRS_MODE, BGE_SRSMODE_ENABLE);
6011 bge_stop_block(sc, BGE_SBDI_MODE, BGE_SBDIMODE_ENABLE);
6012 bge_stop_block(sc, BGE_SDI_MODE, BGE_SDIMODE_ENABLE);
6013 bge_stop_block(sc, BGE_RDMA_MODE, BGE_RDMAMODE_ENABLE);
6014 bge_stop_block(sc, BGE_SDC_MODE, BGE_SDCMODE_ENABLE);
6015 if (BGE_IS_5700_FAMILY(sc))
6016 bge_stop_block(sc, BGE_DMAC_MODE, BGE_DMACMODE_ENABLE);
6017 bge_stop_block(sc, BGE_SBDC_MODE, BGE_SBDCMODE_ENABLE);
6020 * Shut down all of the memory managers and related
6023 bge_stop_block(sc, BGE_HCC_MODE, BGE_HCCMODE_ENABLE);
6024 bge_stop_block(sc, BGE_WDMA_MODE, BGE_WDMAMODE_ENABLE);
6025 if (BGE_IS_5700_FAMILY(sc))
6026 bge_stop_block(sc, BGE_MBCF_MODE, BGE_MBCFMODE_ENABLE);
6028 CSR_WRITE_4(sc, BGE_FTQ_RESET, 0xFFFFFFFF);
6029 CSR_WRITE_4(sc, BGE_FTQ_RESET, 0);
6030 if (!(BGE_IS_5705_PLUS(sc))) {
6031 BGE_CLRBIT(sc, BGE_BMAN_MODE, BGE_BMANMODE_ENABLE);
6032 BGE_CLRBIT(sc, BGE_MARB_MODE, BGE_MARBMODE_ENABLE);
6034 /* Update MAC statistics. */
6035 if (BGE_IS_5705_PLUS(sc))
6036 bge_stats_update_regs(sc);
6039 bge_sig_legacy(sc, BGE_RESET_SHUTDOWN);
6040 bge_sig_post_reset(sc, BGE_RESET_SHUTDOWN);
6043 * Keep the ASF firmware running if up.
6045 if (sc->bge_asf_mode & ASF_STACKUP)
6046 BGE_SETBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
6048 BGE_CLRBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
6050 /* Free the RX lists. */
6051 bge_free_rx_ring_std(sc);
6053 /* Free jumbo RX list. */
6054 if (BGE_IS_JUMBO_CAPABLE(sc))
6055 bge_free_rx_ring_jumbo(sc);
6057 /* Free TX buffers. */
6058 bge_free_tx_ring(sc);
6060 sc->bge_tx_saved_considx = BGE_TXCONS_UNSET;
6062 /* Clear MAC's link state (PHY may still have link UP). */
6063 if (bootverbose && sc->bge_link)
6064 if_printf(sc->bge_ifp, "link DOWN\n");
6067 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
6071 * Stop all chip I/O so that the kernel's probe routines don't
6072 * get confused by errant DMAs when rebooting.
6075 bge_shutdown(device_t dev)
6077 struct bge_softc *sc;
6079 sc = device_get_softc(dev);
6088 bge_suspend(device_t dev)
6090 struct bge_softc *sc;
6092 sc = device_get_softc(dev);
6101 bge_resume(device_t dev)
6103 struct bge_softc *sc;
6106 sc = device_get_softc(dev);
6109 if (ifp->if_flags & IFF_UP) {
6110 bge_init_locked(sc);
6111 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
6112 bge_start_locked(ifp);
6120 bge_link_upd(struct bge_softc *sc)
6122 struct mii_data *mii;
6123 uint32_t link, status;
6125 BGE_LOCK_ASSERT(sc);
6127 /* Clear 'pending link event' flag. */
6128 sc->bge_link_evt = 0;
6131 * Process link state changes.
6132 * Grrr. The link status word in the status block does
6133 * not work correctly on the BCM5700 rev AX and BX chips,
6134 * according to all available information. Hence, we have
6135 * to enable MII interrupts in order to properly obtain
6136 * async link changes. Unfortunately, this also means that
6137 * we have to read the MAC status register to detect link
6138 * changes, thereby adding an additional register access to
6139 * the interrupt handler.
6141 * XXX: perhaps link state detection procedure used for
6142 * BGE_CHIPID_BCM5700_B2 can be used for others BCM5700 revisions.
6145 if (sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
6146 sc->bge_chipid != BGE_CHIPID_BCM5700_B2) {
6147 status = CSR_READ_4(sc, BGE_MAC_STS);
6148 if (status & BGE_MACSTAT_MI_INTERRUPT) {
6149 mii = device_get_softc(sc->bge_miibus);
6151 if (!sc->bge_link &&
6152 mii->mii_media_status & IFM_ACTIVE &&
6153 IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
6156 if_printf(sc->bge_ifp, "link UP\n");
6157 } else if (sc->bge_link &&
6158 (!(mii->mii_media_status & IFM_ACTIVE) ||
6159 IFM_SUBTYPE(mii->mii_media_active) == IFM_NONE)) {
6162 if_printf(sc->bge_ifp, "link DOWN\n");
6165 /* Clear the interrupt. */
6166 CSR_WRITE_4(sc, BGE_MAC_EVT_ENB,
6167 BGE_EVTENB_MI_INTERRUPT);
6168 bge_miibus_readreg(sc->bge_dev, sc->bge_phy_addr,
6170 bge_miibus_writereg(sc->bge_dev, sc->bge_phy_addr,
6171 BRGPHY_MII_IMR, BRGPHY_INTRS);
6176 if (sc->bge_flags & BGE_FLAG_TBI) {
6177 status = CSR_READ_4(sc, BGE_MAC_STS);
6178 if (status & BGE_MACSTAT_TBI_PCS_SYNCHED) {
6179 if (!sc->bge_link) {
6181 if (sc->bge_asicrev == BGE_ASICREV_BCM5704) {
6182 BGE_CLRBIT(sc, BGE_MAC_MODE,
6183 BGE_MACMODE_TBI_SEND_CFGS);
6186 CSR_WRITE_4(sc, BGE_MAC_STS, 0xFFFFFFFF);
6188 if_printf(sc->bge_ifp, "link UP\n");
6189 if_link_state_change(sc->bge_ifp,
6192 } else if (sc->bge_link) {
6195 if_printf(sc->bge_ifp, "link DOWN\n");
6196 if_link_state_change(sc->bge_ifp, LINK_STATE_DOWN);
6198 } else if ((sc->bge_mi_mode & BGE_MIMODE_AUTOPOLL) != 0) {
6200 * Some broken BCM chips have BGE_STATFLAG_LINKSTATE_CHANGED bit
6201 * in status word always set. Workaround this bug by reading
6202 * PHY link status directly.
6204 link = (CSR_READ_4(sc, BGE_MI_STS) & BGE_MISTS_LINK) ? 1 : 0;
6206 if (link != sc->bge_link ||
6207 sc->bge_asicrev == BGE_ASICREV_BCM5700) {
6208 mii = device_get_softc(sc->bge_miibus);
6210 if (!sc->bge_link &&
6211 mii->mii_media_status & IFM_ACTIVE &&
6212 IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
6215 if_printf(sc->bge_ifp, "link UP\n");
6216 } else if (sc->bge_link &&
6217 (!(mii->mii_media_status & IFM_ACTIVE) ||
6218 IFM_SUBTYPE(mii->mii_media_active) == IFM_NONE)) {
6221 if_printf(sc->bge_ifp, "link DOWN\n");
6226 * For controllers that call mii_tick, we have to poll
6229 mii = device_get_softc(sc->bge_miibus);
6231 bge_miibus_statchg(sc->bge_dev);
6234 /* Disable MAC attention when link is up. */
6235 CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED |
6236 BGE_MACSTAT_CFG_CHANGED | BGE_MACSTAT_MI_COMPLETE |
6237 BGE_MACSTAT_LINK_CHANGED);
6241 bge_add_sysctls(struct bge_softc *sc)
6243 struct sysctl_ctx_list *ctx;
6244 struct sysctl_oid_list *children;
6248 ctx = device_get_sysctl_ctx(sc->bge_dev);
6249 children = SYSCTL_CHILDREN(device_get_sysctl_tree(sc->bge_dev));
6251 #ifdef BGE_REGISTER_DEBUG
6252 SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "debug_info",
6253 CTLTYPE_INT | CTLFLAG_RW, sc, 0, bge_sysctl_debug_info, "I",
6254 "Debug Information");
6256 SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "reg_read",
6257 CTLTYPE_INT | CTLFLAG_RW, sc, 0, bge_sysctl_reg_read, "I",
6258 "MAC Register Read");
6260 SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "ape_read",
6261 CTLTYPE_INT | CTLFLAG_RW, sc, 0, bge_sysctl_ape_read, "I",
6262 "APE Register Read");
6264 SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "mem_read",
6265 CTLTYPE_INT | CTLFLAG_RW, sc, 0, bge_sysctl_mem_read, "I",
6270 unit = device_get_unit(sc->bge_dev);
6272 * A common design characteristic for many Broadcom client controllers
6273 * is that they only support a single outstanding DMA read operation
6274 * on the PCIe bus. This means that it will take twice as long to fetch
6275 * a TX frame that is split into header and payload buffers as it does
6276 * to fetch a single, contiguous TX frame (2 reads vs. 1 read). For
6277 * these controllers, coalescing buffers to reduce the number of memory
6278 * reads is effective way to get maximum performance(about 940Mbps).
6279 * Without collapsing TX buffers the maximum TCP bulk transfer
6280 * performance is about 850Mbps. However forcing coalescing mbufs
6281 * consumes a lot of CPU cycles, so leave it off by default.
6283 sc->bge_forced_collapse = 0;
6284 snprintf(tn, sizeof(tn), "dev.bge.%d.forced_collapse", unit);
6285 TUNABLE_INT_FETCH(tn, &sc->bge_forced_collapse);
6286 SYSCTL_ADD_INT(ctx, children, OID_AUTO, "forced_collapse",
6287 CTLFLAG_RW, &sc->bge_forced_collapse, 0,
6288 "Number of fragmented TX buffers of a frame allowed before "
6289 "forced collapsing");
6292 snprintf(tn, sizeof(tn), "dev.bge.%d.msi", unit);
6293 TUNABLE_INT_FETCH(tn, &sc->bge_msi);
6294 SYSCTL_ADD_INT(ctx, children, OID_AUTO, "msi",
6295 CTLFLAG_RD, &sc->bge_msi, 0, "Enable MSI");
6298 * It seems all Broadcom controllers have a bug that can generate UDP
6299 * datagrams with checksum value 0 when TX UDP checksum offloading is
6300 * enabled. Generating UDP checksum value 0 is RFC 768 violation.
6301 * Even though the probability of generating such UDP datagrams is
6302 * low, I don't want to see FreeBSD boxes to inject such datagrams
6303 * into network so disable UDP checksum offloading by default. Users
6304 * still override this behavior by setting a sysctl variable,
6305 * dev.bge.0.forced_udpcsum.
6307 sc->bge_forced_udpcsum = 0;
6308 snprintf(tn, sizeof(tn), "dev.bge.%d.bge_forced_udpcsum", unit);
6309 TUNABLE_INT_FETCH(tn, &sc->bge_forced_udpcsum);
6310 SYSCTL_ADD_INT(ctx, children, OID_AUTO, "forced_udpcsum",
6311 CTLFLAG_RW, &sc->bge_forced_udpcsum, 0,
6312 "Enable UDP checksum offloading even if controller can "
6313 "generate UDP checksum value 0");
6315 if (BGE_IS_5705_PLUS(sc))
6316 bge_add_sysctl_stats_regs(sc, ctx, children);
6318 bge_add_sysctl_stats(sc, ctx, children);
6321 #define BGE_SYSCTL_STAT(sc, ctx, desc, parent, node, oid) \
6322 SYSCTL_ADD_PROC(ctx, parent, OID_AUTO, oid, CTLTYPE_UINT|CTLFLAG_RD, \
6323 sc, offsetof(struct bge_stats, node), bge_sysctl_stats, "IU", \
6327 bge_add_sysctl_stats(struct bge_softc *sc, struct sysctl_ctx_list *ctx,
6328 struct sysctl_oid_list *parent)
6330 struct sysctl_oid *tree;
6331 struct sysctl_oid_list *children, *schildren;
6333 tree = SYSCTL_ADD_NODE(ctx, parent, OID_AUTO, "stats", CTLFLAG_RD,
6334 NULL, "BGE Statistics");
6335 schildren = children = SYSCTL_CHILDREN(tree);
6336 BGE_SYSCTL_STAT(sc, ctx, "Frames Dropped Due To Filters",
6337 children, COSFramesDroppedDueToFilters,
6338 "FramesDroppedDueToFilters");
6339 BGE_SYSCTL_STAT(sc, ctx, "NIC DMA Write Queue Full",
6340 children, nicDmaWriteQueueFull, "DmaWriteQueueFull");
6341 BGE_SYSCTL_STAT(sc, ctx, "NIC DMA Write High Priority Queue Full",
6342 children, nicDmaWriteHighPriQueueFull, "DmaWriteHighPriQueueFull");
6343 BGE_SYSCTL_STAT(sc, ctx, "NIC No More RX Buffer Descriptors",
6344 children, nicNoMoreRxBDs, "NoMoreRxBDs");
6345 BGE_SYSCTL_STAT(sc, ctx, "Discarded Input Frames",
6346 children, ifInDiscards, "InputDiscards");
6347 BGE_SYSCTL_STAT(sc, ctx, "Input Errors",
6348 children, ifInErrors, "InputErrors");
6349 BGE_SYSCTL_STAT(sc, ctx, "NIC Recv Threshold Hit",
6350 children, nicRecvThresholdHit, "RecvThresholdHit");
6351 BGE_SYSCTL_STAT(sc, ctx, "NIC DMA Read Queue Full",
6352 children, nicDmaReadQueueFull, "DmaReadQueueFull");
6353 BGE_SYSCTL_STAT(sc, ctx, "NIC DMA Read High Priority Queue Full",
6354 children, nicDmaReadHighPriQueueFull, "DmaReadHighPriQueueFull");
6355 BGE_SYSCTL_STAT(sc, ctx, "NIC Send Data Complete Queue Full",
6356 children, nicSendDataCompQueueFull, "SendDataCompQueueFull");
6357 BGE_SYSCTL_STAT(sc, ctx, "NIC Ring Set Send Producer Index",
6358 children, nicRingSetSendProdIndex, "RingSetSendProdIndex");
6359 BGE_SYSCTL_STAT(sc, ctx, "NIC Ring Status Update",
6360 children, nicRingStatusUpdate, "RingStatusUpdate");
6361 BGE_SYSCTL_STAT(sc, ctx, "NIC Interrupts",
6362 children, nicInterrupts, "Interrupts");
6363 BGE_SYSCTL_STAT(sc, ctx, "NIC Avoided Interrupts",
6364 children, nicAvoidedInterrupts, "AvoidedInterrupts");
6365 BGE_SYSCTL_STAT(sc, ctx, "NIC Send Threshold Hit",
6366 children, nicSendThresholdHit, "SendThresholdHit");
6368 tree = SYSCTL_ADD_NODE(ctx, schildren, OID_AUTO, "rx", CTLFLAG_RD,
6369 NULL, "BGE RX Statistics");
6370 children = SYSCTL_CHILDREN(tree);
6371 BGE_SYSCTL_STAT(sc, ctx, "Inbound Octets",
6372 children, rxstats.ifHCInOctets, "ifHCInOctets");
6373 BGE_SYSCTL_STAT(sc, ctx, "Fragments",
6374 children, rxstats.etherStatsFragments, "Fragments");
6375 BGE_SYSCTL_STAT(sc, ctx, "Inbound Unicast Packets",
6376 children, rxstats.ifHCInUcastPkts, "UnicastPkts");
6377 BGE_SYSCTL_STAT(sc, ctx, "Inbound Multicast Packets",
6378 children, rxstats.ifHCInMulticastPkts, "MulticastPkts");
6379 BGE_SYSCTL_STAT(sc, ctx, "FCS Errors",
6380 children, rxstats.dot3StatsFCSErrors, "FCSErrors");
6381 BGE_SYSCTL_STAT(sc, ctx, "Alignment Errors",
6382 children, rxstats.dot3StatsAlignmentErrors, "AlignmentErrors");
6383 BGE_SYSCTL_STAT(sc, ctx, "XON Pause Frames Received",
6384 children, rxstats.xonPauseFramesReceived, "xonPauseFramesReceived");
6385 BGE_SYSCTL_STAT(sc, ctx, "XOFF Pause Frames Received",
6386 children, rxstats.xoffPauseFramesReceived,
6387 "xoffPauseFramesReceived");
6388 BGE_SYSCTL_STAT(sc, ctx, "MAC Control Frames Received",
6389 children, rxstats.macControlFramesReceived,
6390 "ControlFramesReceived");
6391 BGE_SYSCTL_STAT(sc, ctx, "XOFF State Entered",
6392 children, rxstats.xoffStateEntered, "xoffStateEntered");
6393 BGE_SYSCTL_STAT(sc, ctx, "Frames Too Long",
6394 children, rxstats.dot3StatsFramesTooLong, "FramesTooLong");
6395 BGE_SYSCTL_STAT(sc, ctx, "Jabbers",
6396 children, rxstats.etherStatsJabbers, "Jabbers");
6397 BGE_SYSCTL_STAT(sc, ctx, "Undersized Packets",
6398 children, rxstats.etherStatsUndersizePkts, "UndersizePkts");
6399 BGE_SYSCTL_STAT(sc, ctx, "Inbound Range Length Errors",
6400 children, rxstats.inRangeLengthError, "inRangeLengthError");
6401 BGE_SYSCTL_STAT(sc, ctx, "Outbound Range Length Errors",
6402 children, rxstats.outRangeLengthError, "outRangeLengthError");
6404 tree = SYSCTL_ADD_NODE(ctx, schildren, OID_AUTO, "tx", CTLFLAG_RD,
6405 NULL, "BGE TX Statistics");
6406 children = SYSCTL_CHILDREN(tree);
6407 BGE_SYSCTL_STAT(sc, ctx, "Outbound Octets",
6408 children, txstats.ifHCOutOctets, "ifHCOutOctets");
6409 BGE_SYSCTL_STAT(sc, ctx, "TX Collisions",
6410 children, txstats.etherStatsCollisions, "Collisions");
6411 BGE_SYSCTL_STAT(sc, ctx, "XON Sent",
6412 children, txstats.outXonSent, "XonSent");
6413 BGE_SYSCTL_STAT(sc, ctx, "XOFF Sent",
6414 children, txstats.outXoffSent, "XoffSent");
6415 BGE_SYSCTL_STAT(sc, ctx, "Flow Control Done",
6416 children, txstats.flowControlDone, "flowControlDone");
6417 BGE_SYSCTL_STAT(sc, ctx, "Internal MAC TX errors",
6418 children, txstats.dot3StatsInternalMacTransmitErrors,
6419 "InternalMacTransmitErrors");
6420 BGE_SYSCTL_STAT(sc, ctx, "Single Collision Frames",
6421 children, txstats.dot3StatsSingleCollisionFrames,
6422 "SingleCollisionFrames");
6423 BGE_SYSCTL_STAT(sc, ctx, "Multiple Collision Frames",
6424 children, txstats.dot3StatsMultipleCollisionFrames,
6425 "MultipleCollisionFrames");
6426 BGE_SYSCTL_STAT(sc, ctx, "Deferred Transmissions",
6427 children, txstats.dot3StatsDeferredTransmissions,
6428 "DeferredTransmissions");
6429 BGE_SYSCTL_STAT(sc, ctx, "Excessive Collisions",
6430 children, txstats.dot3StatsExcessiveCollisions,
6431 "ExcessiveCollisions");
6432 BGE_SYSCTL_STAT(sc, ctx, "Late Collisions",
6433 children, txstats.dot3StatsLateCollisions,
6435 BGE_SYSCTL_STAT(sc, ctx, "Outbound Unicast Packets",
6436 children, txstats.ifHCOutUcastPkts, "UnicastPkts");
6437 BGE_SYSCTL_STAT(sc, ctx, "Outbound Multicast Packets",
6438 children, txstats.ifHCOutMulticastPkts, "MulticastPkts");
6439 BGE_SYSCTL_STAT(sc, ctx, "Outbound Broadcast Packets",
6440 children, txstats.ifHCOutBroadcastPkts, "BroadcastPkts");
6441 BGE_SYSCTL_STAT(sc, ctx, "Carrier Sense Errors",
6442 children, txstats.dot3StatsCarrierSenseErrors,
6443 "CarrierSenseErrors");
6444 BGE_SYSCTL_STAT(sc, ctx, "Outbound Discards",
6445 children, txstats.ifOutDiscards, "Discards");
6446 BGE_SYSCTL_STAT(sc, ctx, "Outbound Errors",
6447 children, txstats.ifOutErrors, "Errors");
6450 #undef BGE_SYSCTL_STAT
6452 #define BGE_SYSCTL_STAT_ADD64(c, h, n, p, d) \
6453 SYSCTL_ADD_UQUAD(c, h, OID_AUTO, n, CTLFLAG_RD, p, d)
6456 bge_add_sysctl_stats_regs(struct bge_softc *sc, struct sysctl_ctx_list *ctx,
6457 struct sysctl_oid_list *parent)
6459 struct sysctl_oid *tree;
6460 struct sysctl_oid_list *child, *schild;
6461 struct bge_mac_stats *stats;
6463 stats = &sc->bge_mac_stats;
6464 tree = SYSCTL_ADD_NODE(ctx, parent, OID_AUTO, "stats", CTLFLAG_RD,
6465 NULL, "BGE Statistics");
6466 schild = child = SYSCTL_CHILDREN(tree);
6467 BGE_SYSCTL_STAT_ADD64(ctx, child, "FramesDroppedDueToFilters",
6468 &stats->FramesDroppedDueToFilters, "Frames Dropped Due to Filters");
6469 BGE_SYSCTL_STAT_ADD64(ctx, child, "DmaWriteQueueFull",
6470 &stats->DmaWriteQueueFull, "NIC DMA Write Queue Full");
6471 BGE_SYSCTL_STAT_ADD64(ctx, child, "DmaWriteHighPriQueueFull",
6472 &stats->DmaWriteHighPriQueueFull,
6473 "NIC DMA Write High Priority Queue Full");
6474 BGE_SYSCTL_STAT_ADD64(ctx, child, "NoMoreRxBDs",
6475 &stats->NoMoreRxBDs, "NIC No More RX Buffer Descriptors");
6476 BGE_SYSCTL_STAT_ADD64(ctx, child, "InputDiscards",
6477 &stats->InputDiscards, "Discarded Input Frames");
6478 BGE_SYSCTL_STAT_ADD64(ctx, child, "InputErrors",
6479 &stats->InputErrors, "Input Errors");
6480 BGE_SYSCTL_STAT_ADD64(ctx, child, "RecvThresholdHit",
6481 &stats->RecvThresholdHit, "NIC Recv Threshold Hit");
6483 tree = SYSCTL_ADD_NODE(ctx, schild, OID_AUTO, "rx", CTLFLAG_RD,
6484 NULL, "BGE RX Statistics");
6485 child = SYSCTL_CHILDREN(tree);
6486 BGE_SYSCTL_STAT_ADD64(ctx, child, "ifHCInOctets",
6487 &stats->ifHCInOctets, "Inbound Octets");
6488 BGE_SYSCTL_STAT_ADD64(ctx, child, "Fragments",
6489 &stats->etherStatsFragments, "Fragments");
6490 BGE_SYSCTL_STAT_ADD64(ctx, child, "UnicastPkts",
6491 &stats->ifHCInUcastPkts, "Inbound Unicast Packets");
6492 BGE_SYSCTL_STAT_ADD64(ctx, child, "MulticastPkts",
6493 &stats->ifHCInMulticastPkts, "Inbound Multicast Packets");
6494 BGE_SYSCTL_STAT_ADD64(ctx, child, "BroadcastPkts",
6495 &stats->ifHCInBroadcastPkts, "Inbound Broadcast Packets");
6496 BGE_SYSCTL_STAT_ADD64(ctx, child, "FCSErrors",
6497 &stats->dot3StatsFCSErrors, "FCS Errors");
6498 BGE_SYSCTL_STAT_ADD64(ctx, child, "AlignmentErrors",
6499 &stats->dot3StatsAlignmentErrors, "Alignment Errors");
6500 BGE_SYSCTL_STAT_ADD64(ctx, child, "xonPauseFramesReceived",
6501 &stats->xonPauseFramesReceived, "XON Pause Frames Received");
6502 BGE_SYSCTL_STAT_ADD64(ctx, child, "xoffPauseFramesReceived",
6503 &stats->xoffPauseFramesReceived, "XOFF Pause Frames Received");
6504 BGE_SYSCTL_STAT_ADD64(ctx, child, "ControlFramesReceived",
6505 &stats->macControlFramesReceived, "MAC Control Frames Received");
6506 BGE_SYSCTL_STAT_ADD64(ctx, child, "xoffStateEntered",
6507 &stats->xoffStateEntered, "XOFF State Entered");
6508 BGE_SYSCTL_STAT_ADD64(ctx, child, "FramesTooLong",
6509 &stats->dot3StatsFramesTooLong, "Frames Too Long");
6510 BGE_SYSCTL_STAT_ADD64(ctx, child, "Jabbers",
6511 &stats->etherStatsJabbers, "Jabbers");
6512 BGE_SYSCTL_STAT_ADD64(ctx, child, "UndersizePkts",
6513 &stats->etherStatsUndersizePkts, "Undersized Packets");
6515 tree = SYSCTL_ADD_NODE(ctx, schild, OID_AUTO, "tx", CTLFLAG_RD,
6516 NULL, "BGE TX Statistics");
6517 child = SYSCTL_CHILDREN(tree);
6518 BGE_SYSCTL_STAT_ADD64(ctx, child, "ifHCOutOctets",
6519 &stats->ifHCOutOctets, "Outbound Octets");
6520 BGE_SYSCTL_STAT_ADD64(ctx, child, "Collisions",
6521 &stats->etherStatsCollisions, "TX Collisions");
6522 BGE_SYSCTL_STAT_ADD64(ctx, child, "XonSent",
6523 &stats->outXonSent, "XON Sent");
6524 BGE_SYSCTL_STAT_ADD64(ctx, child, "XoffSent",
6525 &stats->outXoffSent, "XOFF Sent");
6526 BGE_SYSCTL_STAT_ADD64(ctx, child, "InternalMacTransmitErrors",
6527 &stats->dot3StatsInternalMacTransmitErrors,
6528 "Internal MAC TX Errors");
6529 BGE_SYSCTL_STAT_ADD64(ctx, child, "SingleCollisionFrames",
6530 &stats->dot3StatsSingleCollisionFrames, "Single Collision Frames");
6531 BGE_SYSCTL_STAT_ADD64(ctx, child, "MultipleCollisionFrames",
6532 &stats->dot3StatsMultipleCollisionFrames,
6533 "Multiple Collision Frames");
6534 BGE_SYSCTL_STAT_ADD64(ctx, child, "DeferredTransmissions",
6535 &stats->dot3StatsDeferredTransmissions, "Deferred Transmissions");
6536 BGE_SYSCTL_STAT_ADD64(ctx, child, "ExcessiveCollisions",
6537 &stats->dot3StatsExcessiveCollisions, "Excessive Collisions");
6538 BGE_SYSCTL_STAT_ADD64(ctx, child, "LateCollisions",
6539 &stats->dot3StatsLateCollisions, "Late Collisions");
6540 BGE_SYSCTL_STAT_ADD64(ctx, child, "UnicastPkts",
6541 &stats->ifHCOutUcastPkts, "Outbound Unicast Packets");
6542 BGE_SYSCTL_STAT_ADD64(ctx, child, "MulticastPkts",
6543 &stats->ifHCOutMulticastPkts, "Outbound Multicast Packets");
6544 BGE_SYSCTL_STAT_ADD64(ctx, child, "BroadcastPkts",
6545 &stats->ifHCOutBroadcastPkts, "Outbound Broadcast Packets");
6548 #undef BGE_SYSCTL_STAT_ADD64
6551 bge_sysctl_stats(SYSCTL_HANDLER_ARGS)
6553 struct bge_softc *sc;
6557 sc = (struct bge_softc *)arg1;
6559 result = CSR_READ_4(sc, BGE_MEMWIN_START + BGE_STATS_BLOCK + offset +
6560 offsetof(bge_hostaddr, bge_addr_lo));
6561 return (sysctl_handle_int(oidp, &result, 0, req));
6564 #ifdef BGE_REGISTER_DEBUG
6566 bge_sysctl_debug_info(SYSCTL_HANDLER_ARGS)
6568 struct bge_softc *sc;
6570 int error, result, sbsz;
6574 error = sysctl_handle_int(oidp, &result, 0, req);
6575 if (error || (req->newptr == NULL))
6579 sc = (struct bge_softc *)arg1;
6581 if (sc->bge_asicrev == BGE_ASICREV_BCM5700 &&
6582 sc->bge_chipid != BGE_CHIPID_BCM5700_C0)
6583 sbsz = BGE_STATUS_BLK_SZ;
6586 sbdata = (uint16_t *)sc->bge_ldata.bge_status_block;
6587 printf("Status Block:\n");
6589 bus_dmamap_sync(sc->bge_cdata.bge_status_tag,
6590 sc->bge_cdata.bge_status_map,
6591 BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
6592 for (i = 0x0; i < sbsz / sizeof(uint16_t); ) {
6594 for (j = 0; j < 8; j++)
6595 printf(" %04x", sbdata[i++]);
6599 printf("Registers:\n");
6600 for (i = 0x800; i < 0xA00; ) {
6602 for (j = 0; j < 8; j++) {
6603 printf(" %08x", CSR_READ_4(sc, i));
6610 printf("Hardware Flags:\n");
6611 if (BGE_IS_5717_PLUS(sc))
6612 printf(" - 5717 Plus\n");
6613 if (BGE_IS_5755_PLUS(sc))
6614 printf(" - 5755 Plus\n");
6615 if (BGE_IS_575X_PLUS(sc))
6616 printf(" - 575X Plus\n");
6617 if (BGE_IS_5705_PLUS(sc))
6618 printf(" - 5705 Plus\n");
6619 if (BGE_IS_5714_FAMILY(sc))
6620 printf(" - 5714 Family\n");
6621 if (BGE_IS_5700_FAMILY(sc))
6622 printf(" - 5700 Family\n");
6623 if (sc->bge_flags & BGE_FLAG_JUMBO)
6624 printf(" - Supports Jumbo Frames\n");
6625 if (sc->bge_flags & BGE_FLAG_PCIX)
6626 printf(" - PCI-X Bus\n");
6627 if (sc->bge_flags & BGE_FLAG_PCIE)
6628 printf(" - PCI Express Bus\n");
6629 if (sc->bge_phy_flags & BGE_PHY_NO_3LED)
6630 printf(" - No 3 LEDs\n");
6631 if (sc->bge_flags & BGE_FLAG_RX_ALIGNBUG)
6632 printf(" - RX Alignment Bug\n");
6639 bge_sysctl_reg_read(SYSCTL_HANDLER_ARGS)
6641 struct bge_softc *sc;
6647 error = sysctl_handle_int(oidp, &result, 0, req);
6648 if (error || (req->newptr == NULL))
6651 if (result < 0x8000) {
6652 sc = (struct bge_softc *)arg1;
6653 val = CSR_READ_4(sc, result);
6654 printf("reg 0x%06X = 0x%08X\n", result, val);
6661 bge_sysctl_ape_read(SYSCTL_HANDLER_ARGS)
6663 struct bge_softc *sc;
6669 error = sysctl_handle_int(oidp, &result, 0, req);
6670 if (error || (req->newptr == NULL))
6673 if (result < 0x8000) {
6674 sc = (struct bge_softc *)arg1;
6675 val = APE_READ_4(sc, result);
6676 printf("reg 0x%06X = 0x%08X\n", result, val);
6683 bge_sysctl_mem_read(SYSCTL_HANDLER_ARGS)
6685 struct bge_softc *sc;
6691 error = sysctl_handle_int(oidp, &result, 0, req);
6692 if (error || (req->newptr == NULL))
6695 if (result < 0x8000) {
6696 sc = (struct bge_softc *)arg1;
6697 val = bge_readmem_ind(sc, result);
6698 printf("mem 0x%06X = 0x%08X\n", result, val);
6706 bge_get_eaddr_fw(struct bge_softc *sc, uint8_t ether_addr[])
6709 if (sc->bge_flags & BGE_FLAG_EADDR)
6713 OF_getetheraddr(sc->bge_dev, ether_addr);
6720 bge_get_eaddr_mem(struct bge_softc *sc, uint8_t ether_addr[])
6724 mac_addr = bge_readmem_ind(sc, BGE_SRAM_MAC_ADDR_HIGH_MB);
6725 if ((mac_addr >> 16) == 0x484b) {
6726 ether_addr[0] = (uint8_t)(mac_addr >> 8);
6727 ether_addr[1] = (uint8_t)mac_addr;
6728 mac_addr = bge_readmem_ind(sc, BGE_SRAM_MAC_ADDR_LOW_MB);
6729 ether_addr[2] = (uint8_t)(mac_addr >> 24);
6730 ether_addr[3] = (uint8_t)(mac_addr >> 16);
6731 ether_addr[4] = (uint8_t)(mac_addr >> 8);
6732 ether_addr[5] = (uint8_t)mac_addr;
6739 bge_get_eaddr_nvram(struct bge_softc *sc, uint8_t ether_addr[])
6741 int mac_offset = BGE_EE_MAC_OFFSET;
6743 if (sc->bge_asicrev == BGE_ASICREV_BCM5906)
6744 mac_offset = BGE_EE_MAC_OFFSET_5906;
6746 return (bge_read_nvram(sc, ether_addr, mac_offset + 2,
6751 bge_get_eaddr_eeprom(struct bge_softc *sc, uint8_t ether_addr[])
6754 if (sc->bge_asicrev == BGE_ASICREV_BCM5906)
6757 return (bge_read_eeprom(sc, ether_addr, BGE_EE_MAC_OFFSET + 2,
6762 bge_get_eaddr(struct bge_softc *sc, uint8_t eaddr[])
6764 static const bge_eaddr_fcn_t bge_eaddr_funcs[] = {
6765 /* NOTE: Order is critical */
6768 bge_get_eaddr_nvram,
6769 bge_get_eaddr_eeprom,
6772 const bge_eaddr_fcn_t *func;
6774 for (func = bge_eaddr_funcs; *func != NULL; ++func) {
6775 if ((*func)(sc, eaddr) == 0)
6778 return (*func == NULL ? ENXIO : 0);