if_bge.c revision 1.173 1 /* $NetBSD: if_bge.c,v 1.173 2010/01/24 17:56:54 msaitoh Exp $ */
2
3 /*
4 * Copyright (c) 2001 Wind River Systems
5 * Copyright (c) 1997, 1998, 1999, 2001
6 * Bill Paul <wpaul (at) windriver.com>. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by Bill Paul.
19 * 4. Neither the name of the author nor the names of any co-contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
27 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
33 * THE POSSIBILITY OF SUCH DAMAGE.
34 *
35 * $FreeBSD: if_bge.c,v 1.13 2002/04/04 06:01:31 wpaul Exp $
36 */
37
38 /*
39 * Broadcom BCM570x family gigabit ethernet driver for NetBSD.
40 *
41 * NetBSD version by:
42 *
43 * Frank van der Linden <fvdl (at) wasabisystems.com>
44 * Jason Thorpe <thorpej (at) wasabisystems.com>
45 * Jonathan Stone <jonathan (at) dsg.stanford.edu>
46 *
47 * Originally written for FreeBSD by Bill Paul <wpaul (at) windriver.com>
48 * Senior Engineer, Wind River Systems
49 */
50
51 /*
52 * The Broadcom BCM5700 is based on technology originally developed by
53 * Alteon Networks as part of the Tigon I and Tigon II gigabit ethernet
54 * MAC chips. The BCM5700, sometimes refered to as the Tigon III, has
55 * two on-board MIPS R4000 CPUs and can have as much as 16MB of external
56 * SSRAM. The BCM5700 supports TCP, UDP and IP checksum offload, jumbo
57 * frames, highly configurable RX filtering, and 16 RX and TX queues
58 * (which, along with RX filter rules, can be used for QOS applications).
59 * Other features, such as TCP segmentation, may be available as part
60 * of value-added firmware updates. Unlike the Tigon I and Tigon II,
61 * firmware images can be stored in hardware and need not be compiled
62 * into the driver.
63 *
64 * The BCM5700 supports the PCI v2.2 and PCI-X v1.0 standards, and will
65 * function in a 32-bit/64-bit 33/66MHz bus, or a 64-bit/133MHz bus.
66 *
67 * The BCM5701 is a single-chip solution incorporating both the BCM5700
68 * MAC and a BCM5401 10/100/1000 PHY. Unlike the BCM5700, the BCM5701
69 * does not support external SSRAM.
70 *
71 * Broadcom also produces a variation of the BCM5700 under the "Altima"
72 * brand name, which is functionally similar but lacks PCI-X support.
73 *
74 * Without external SSRAM, you can only have at most 4 TX rings,
75 * and the use of the mini RX ring is disabled. This seems to imply
76 * that these features are simply not available on the BCM5701. As a
77 * result, this driver does not implement any support for the mini RX
78 * ring.
79 */
80
81 #include <sys/cdefs.h>
82 __KERNEL_RCSID(0, "$NetBSD: if_bge.c,v 1.173 2010/01/24 17:56:54 msaitoh Exp $");
83
84 #include "vlan.h"
85 #include "rnd.h"
86
87 #include <sys/param.h>
88 #include <sys/systm.h>
89 #include <sys/callout.h>
90 #include <sys/sockio.h>
91 #include <sys/mbuf.h>
92 #include <sys/malloc.h>
93 #include <sys/kernel.h>
94 #include <sys/device.h>
95 #include <sys/socket.h>
96 #include <sys/sysctl.h>
97
98 #include <net/if.h>
99 #include <net/if_dl.h>
100 #include <net/if_media.h>
101 #include <net/if_ether.h>
102
103 #if NRND > 0
104 #include <sys/rnd.h>
105 #endif
106
107 #ifdef INET
108 #include <netinet/in.h>
109 #include <netinet/in_systm.h>
110 #include <netinet/in_var.h>
111 #include <netinet/ip.h>
112 #endif
113
114 /* Headers for TCP Segmentation Offload (TSO) */
115 #include <netinet/in_systm.h> /* n_time for <netinet/ip.h>... */
116 #include <netinet/in.h> /* ip_{src,dst}, for <netinet/ip.h> */
117 #include <netinet/ip.h> /* for struct ip */
118 #include <netinet/tcp.h> /* for struct tcphdr */
119
120
121 #include <net/bpf.h>
122
123 #include <dev/pci/pcireg.h>
124 #include <dev/pci/pcivar.h>
125 #include <dev/pci/pcidevs.h>
126
127 #include <dev/mii/mii.h>
128 #include <dev/mii/miivar.h>
129 #include <dev/mii/miidevs.h>
130 #include <dev/mii/brgphyreg.h>
131
132 #include <dev/pci/if_bgereg.h>
133 #include <dev/pci/if_bgevar.h>
134
135 #include <uvm/uvm_extern.h>
136 #include <prop/proplib.h>
137
138 #define ETHER_MIN_NOPAD (ETHER_MIN_LEN - ETHER_CRC_LEN) /* i.e., 60 */
139
140
141 /*
142 * Tunable thresholds for rx-side bge interrupt mitigation.
143 */
144
145 /*
146 * The pairs of values below were obtained from empirical measurement
147 * on bcm5700 rev B2; they ar designed to give roughly 1 receive
148 * interrupt for every N packets received, where N is, approximately,
149 * the second value (rx_max_bds) in each pair. The values are chosen
150 * such that moving from one pair to the succeeding pair was observed
151 * to roughly halve interrupt rate under sustained input packet load.
152 * The values were empirically chosen to avoid overflowing internal
153 * limits on the bcm5700: inreasing rx_ticks much beyond 600
154 * results in internal wrapping and higher interrupt rates.
155 * The limit of 46 frames was chosen to match NFS workloads.
156 *
157 * These values also work well on bcm5701, bcm5704C, and (less
158 * tested) bcm5703. On other chipsets, (including the Altima chip
159 * family), the larger values may overflow internal chip limits,
160 * leading to increasing interrupt rates rather than lower interrupt
161 * rates.
162 *
163 * Applications using heavy interrupt mitigation (interrupting every
164 * 32 or 46 frames) in both directions may need to increase the TCP
165 * windowsize to above 131072 bytes (e.g., to 199608 bytes) to sustain
166 * full link bandwidth, due to ACKs and window updates lingering
167 * in the RX queue during the 30-to-40-frame interrupt-mitigation window.
168 */
169 static const struct bge_load_rx_thresh {
170 int rx_ticks;
171 int rx_max_bds; }
172 bge_rx_threshes[] = {
173 { 32, 2 },
174 { 50, 4 },
175 { 100, 8 },
176 { 192, 16 },
177 { 416, 32 },
178 { 598, 46 }
179 };
180 #define NBGE_RX_THRESH (sizeof(bge_rx_threshes) / sizeof(bge_rx_threshes[0]))
181
182 /* XXX patchable; should be sysctl'able */
183 static int bge_auto_thresh = 1;
184 static int bge_rx_thresh_lvl;
185
186 static int bge_rxthresh_nodenum;
187
188 typedef int (*bge_eaddr_fcn_t)(struct bge_softc *, uint8_t[]);
189
190 static int bge_probe(device_t, cfdata_t, void *);
191 static void bge_attach(device_t, device_t, void *);
192 static void bge_release_resources(struct bge_softc *);
193 static void bge_txeof(struct bge_softc *);
194 static void bge_rxeof(struct bge_softc *);
195
196 static int bge_get_eaddr_fw(struct bge_softc *, uint8_t[]);
197 static int bge_get_eaddr_mem(struct bge_softc *, uint8_t[]);
198 static int bge_get_eaddr_nvram(struct bge_softc *, uint8_t[]);
199 static int bge_get_eaddr_eeprom(struct bge_softc *, uint8_t[]);
200 static int bge_get_eaddr(struct bge_softc *, uint8_t[]);
201
202 static void bge_tick(void *);
203 static void bge_stats_update(struct bge_softc *);
204 static void bge_stats_update_regs(struct bge_softc *);
205 static int bge_encap(struct bge_softc *, struct mbuf *, uint32_t *);
206
207 static int bge_intr(void *);
208 static void bge_start(struct ifnet *);
209 static int bge_ioctl(struct ifnet *, u_long, void *);
210 static int bge_init(struct ifnet *);
211 static void bge_stop(struct ifnet *, int);
212 static void bge_watchdog(struct ifnet *);
213 static int bge_ifmedia_upd(struct ifnet *);
214 static void bge_ifmedia_sts(struct ifnet *, struct ifmediareq *);
215
216 static void bge_setmulti(struct bge_softc *);
217
218 static void bge_handle_events(struct bge_softc *);
219 static int bge_alloc_jumbo_mem(struct bge_softc *);
220 #if 0 /* XXX */
221 static void bge_free_jumbo_mem(struct bge_softc *);
222 #endif
223 static void *bge_jalloc(struct bge_softc *);
224 static void bge_jfree(struct mbuf *, void *, size_t, void *);
225 static int bge_newbuf_std(struct bge_softc *, int, struct mbuf *,
226 bus_dmamap_t);
227 static int bge_newbuf_jumbo(struct bge_softc *, int, struct mbuf *);
228 static int bge_init_rx_ring_std(struct bge_softc *);
229 static void bge_free_rx_ring_std(struct bge_softc *);
230 static int bge_init_rx_ring_jumbo(struct bge_softc *);
231 static void bge_free_rx_ring_jumbo(struct bge_softc *);
232 static void bge_free_tx_ring(struct bge_softc *);
233 static int bge_init_tx_ring(struct bge_softc *);
234
235 static int bge_chipinit(struct bge_softc *);
236 static int bge_blockinit(struct bge_softc *);
237 static int bge_setpowerstate(struct bge_softc *, int);
238
239 static void bge_reset(struct bge_softc *);
240 static void bge_link_upd(struct bge_softc *);
241
242 #ifdef BGE_DEBUG
243 #define DPRINTF(x) if (bgedebug) printf x
244 #define DPRINTFN(n,x) if (bgedebug >= (n)) printf x
245 #define BGE_TSO_PRINTF(x) do { if (bge_tso_debug) printf x ;} while (0)
246 int bgedebug = 0;
247 int bge_tso_debug = 0;
248 void bge_debug_info(struct bge_softc *);
249 #else
250 #define DPRINTF(x)
251 #define DPRINTFN(n,x)
252 #define BGE_TSO_PRINTF(x)
253 #endif
254
255 #ifdef BGE_EVENT_COUNTERS
256 #define BGE_EVCNT_INCR(ev) (ev).ev_count++
257 #define BGE_EVCNT_ADD(ev, val) (ev).ev_count += (val)
258 #define BGE_EVCNT_UPD(ev, val) (ev).ev_count = (val)
259 #else
260 #define BGE_EVCNT_INCR(ev) /* nothing */
261 #define BGE_EVCNT_ADD(ev, val) /* nothing */
262 #define BGE_EVCNT_UPD(ev, val) /* nothing */
263 #endif
264
265 static const struct bge_product {
266 pci_vendor_id_t bp_vendor;
267 pci_product_id_t bp_product;
268 const char *bp_name;
269 } bge_products[] = {
270 /*
271 * The BCM5700 documentation seems to indicate that the hardware
272 * still has the Alteon vendor ID burned into it, though it
273 * should always be overridden by the value in the EEPROM. We'll
274 * check for it anyway.
275 */
276 { PCI_VENDOR_ALTEON,
277 PCI_PRODUCT_ALTEON_BCM5700,
278 "Broadcom BCM5700 Gigabit Ethernet",
279 },
280 { PCI_VENDOR_ALTEON,
281 PCI_PRODUCT_ALTEON_BCM5701,
282 "Broadcom BCM5701 Gigabit Ethernet",
283 },
284 { PCI_VENDOR_ALTIMA,
285 PCI_PRODUCT_ALTIMA_AC1000,
286 "Altima AC1000 Gigabit Ethernet",
287 },
288 { PCI_VENDOR_ALTIMA,
289 PCI_PRODUCT_ALTIMA_AC1001,
290 "Altima AC1001 Gigabit Ethernet",
291 },
292 { PCI_VENDOR_ALTIMA,
293 PCI_PRODUCT_ALTIMA_AC9100,
294 "Altima AC9100 Gigabit Ethernet",
295 },
296 { PCI_VENDOR_BROADCOM,
297 PCI_PRODUCT_BROADCOM_BCM5700,
298 "Broadcom BCM5700 Gigabit Ethernet",
299 },
300 { PCI_VENDOR_BROADCOM,
301 PCI_PRODUCT_BROADCOM_BCM5701,
302 "Broadcom BCM5701 Gigabit Ethernet",
303 },
304 { PCI_VENDOR_BROADCOM,
305 PCI_PRODUCT_BROADCOM_BCM5702,
306 "Broadcom BCM5702 Gigabit Ethernet",
307 },
308 { PCI_VENDOR_BROADCOM,
309 PCI_PRODUCT_BROADCOM_BCM5702X,
310 "Broadcom BCM5702X Gigabit Ethernet" },
311 { PCI_VENDOR_BROADCOM,
312 PCI_PRODUCT_BROADCOM_BCM5703,
313 "Broadcom BCM5703 Gigabit Ethernet",
314 },
315 { PCI_VENDOR_BROADCOM,
316 PCI_PRODUCT_BROADCOM_BCM5703X,
317 "Broadcom BCM5703X Gigabit Ethernet",
318 },
319 { PCI_VENDOR_BROADCOM,
320 PCI_PRODUCT_BROADCOM_BCM5703_ALT,
321 "Broadcom BCM5703 Gigabit Ethernet",
322 },
323 { PCI_VENDOR_BROADCOM,
324 PCI_PRODUCT_BROADCOM_BCM5704C,
325 "Broadcom BCM5704C Dual Gigabit Ethernet",
326 },
327 { PCI_VENDOR_BROADCOM,
328 PCI_PRODUCT_BROADCOM_BCM5704S,
329 "Broadcom BCM5704S Dual Gigabit Ethernet",
330 },
331 { PCI_VENDOR_BROADCOM,
332 PCI_PRODUCT_BROADCOM_BCM5705,
333 "Broadcom BCM5705 Gigabit Ethernet",
334 },
335 { PCI_VENDOR_BROADCOM,
336 PCI_PRODUCT_BROADCOM_BCM5705F,
337 "Broadcom BCM5705F Gigabit Ethernet",
338 },
339 { PCI_VENDOR_BROADCOM,
340 PCI_PRODUCT_BROADCOM_BCM5705K,
341 "Broadcom BCM5705K Gigabit Ethernet",
342 },
343 { PCI_VENDOR_BROADCOM,
344 PCI_PRODUCT_BROADCOM_BCM5705M,
345 "Broadcom BCM5705M Gigabit Ethernet",
346 },
347 { PCI_VENDOR_BROADCOM,
348 PCI_PRODUCT_BROADCOM_BCM5705M_ALT,
349 "Broadcom BCM5705M Gigabit Ethernet",
350 },
351 { PCI_VENDOR_BROADCOM,
352 PCI_PRODUCT_BROADCOM_BCM5714,
353 "Broadcom BCM5714 Gigabit Ethernet",
354 },
355 { PCI_VENDOR_BROADCOM,
356 PCI_PRODUCT_BROADCOM_BCM5714S,
357 "Broadcom BCM5714S Gigabit Ethernet",
358 },
359 { PCI_VENDOR_BROADCOM,
360 PCI_PRODUCT_BROADCOM_BCM5715,
361 "Broadcom BCM5715 Gigabit Ethernet",
362 },
363 { PCI_VENDOR_BROADCOM,
364 PCI_PRODUCT_BROADCOM_BCM5715S,
365 "Broadcom BCM5715S Gigabit Ethernet",
366 },
367 { PCI_VENDOR_BROADCOM,
368 PCI_PRODUCT_BROADCOM_BCM5717,
369 "Broadcom BCM5717 Gigabit Ethernet",
370 },
371 { PCI_VENDOR_BROADCOM,
372 PCI_PRODUCT_BROADCOM_BCM5718,
373 "Broadcom BCM5718 Gigabit Ethernet",
374 },
375 { PCI_VENDOR_BROADCOM,
376 PCI_PRODUCT_BROADCOM_BCM5720,
377 "Broadcom BCM5720 Gigabit Ethernet",
378 },
379 { PCI_VENDOR_BROADCOM,
380 PCI_PRODUCT_BROADCOM_BCM5721,
381 "Broadcom BCM5721 Gigabit Ethernet",
382 },
383 { PCI_VENDOR_BROADCOM,
384 PCI_PRODUCT_BROADCOM_BCM5722,
385 "Broadcom BCM5722 Gigabit Ethernet",
386 },
387 { PCI_VENDOR_BROADCOM,
388 PCI_PRODUCT_BROADCOM_BCM5723,
389 "Broadcom BCM5723 Gigabit Ethernet",
390 },
391 { PCI_VENDOR_BROADCOM,
392 PCI_PRODUCT_BROADCOM_BCM5724,
393 "Broadcom BCM5724 Gigabit Ethernet",
394 },
395 { PCI_VENDOR_BROADCOM,
396 PCI_PRODUCT_BROADCOM_BCM5750,
397 "Broadcom BCM5750 Gigabit Ethernet",
398 },
399 { PCI_VENDOR_BROADCOM,
400 PCI_PRODUCT_BROADCOM_BCM5750M,
401 "Broadcom BCM5750M Gigabit Ethernet",
402 },
403 { PCI_VENDOR_BROADCOM,
404 PCI_PRODUCT_BROADCOM_BCM5751,
405 "Broadcom BCM5751 Gigabit Ethernet",
406 },
407 { PCI_VENDOR_BROADCOM,
408 PCI_PRODUCT_BROADCOM_BCM5751F,
409 "Broadcom BCM5751F Gigabit Ethernet",
410 },
411 { PCI_VENDOR_BROADCOM,
412 PCI_PRODUCT_BROADCOM_BCM5751M,
413 "Broadcom BCM5751M Gigabit Ethernet",
414 },
415 { PCI_VENDOR_BROADCOM,
416 PCI_PRODUCT_BROADCOM_BCM5752,
417 "Broadcom BCM5752 Gigabit Ethernet",
418 },
419 { PCI_VENDOR_BROADCOM,
420 PCI_PRODUCT_BROADCOM_BCM5752M,
421 "Broadcom BCM5752M Gigabit Ethernet",
422 },
423 { PCI_VENDOR_BROADCOM,
424 PCI_PRODUCT_BROADCOM_BCM5753,
425 "Broadcom BCM5753 Gigabit Ethernet",
426 },
427 { PCI_VENDOR_BROADCOM,
428 PCI_PRODUCT_BROADCOM_BCM5753F,
429 "Broadcom BCM5753F Gigabit Ethernet",
430 },
431 { PCI_VENDOR_BROADCOM,
432 PCI_PRODUCT_BROADCOM_BCM5753M,
433 "Broadcom BCM5753M Gigabit Ethernet",
434 },
435 { PCI_VENDOR_BROADCOM,
436 PCI_PRODUCT_BROADCOM_BCM5754,
437 "Broadcom BCM5754 Gigabit Ethernet",
438 },
439 { PCI_VENDOR_BROADCOM,
440 PCI_PRODUCT_BROADCOM_BCM5754M,
441 "Broadcom BCM5754M Gigabit Ethernet",
442 },
443 { PCI_VENDOR_BROADCOM,
444 PCI_PRODUCT_BROADCOM_BCM5755,
445 "Broadcom BCM5755 Gigabit Ethernet",
446 },
447 { PCI_VENDOR_BROADCOM,
448 PCI_PRODUCT_BROADCOM_BCM5755M,
449 "Broadcom BCM5755M Gigabit Ethernet",
450 },
451 { PCI_VENDOR_BROADCOM,
452 PCI_PRODUCT_BROADCOM_BCM5756,
453 "Broadcom BCM5756 Gigabit Ethernet",
454 },
455 { PCI_VENDOR_BROADCOM,
456 PCI_PRODUCT_BROADCOM_BCM5761,
457 "Broadcom BCM5761 Gigabit Ethernet",
458 },
459 { PCI_VENDOR_BROADCOM,
460 PCI_PRODUCT_BROADCOM_BCM5761E,
461 "Broadcom BCM5761E Gigabit Ethernet",
462 },
463 { PCI_VENDOR_BROADCOM,
464 PCI_PRODUCT_BROADCOM_BCM5761S,
465 "Broadcom BCM5761S Gigabit Ethernet",
466 },
467 { PCI_VENDOR_BROADCOM,
468 PCI_PRODUCT_BROADCOM_BCM5761SE,
469 "Broadcom BCM5761SE Gigabit Ethernet",
470 },
471 { PCI_VENDOR_BROADCOM,
472 PCI_PRODUCT_BROADCOM_BCM5764,
473 "Broadcom BCM5764 Gigabit Ethernet",
474 },
475 { PCI_VENDOR_BROADCOM,
476 PCI_PRODUCT_BROADCOM_BCM5780,
477 "Broadcom BCM5780 Gigabit Ethernet",
478 },
479 { PCI_VENDOR_BROADCOM,
480 PCI_PRODUCT_BROADCOM_BCM5780S,
481 "Broadcom BCM5780S Gigabit Ethernet",
482 },
483 { PCI_VENDOR_BROADCOM,
484 PCI_PRODUCT_BROADCOM_BCM5781,
485 "Broadcom BCM5781 Gigabit Ethernet",
486 },
487 { PCI_VENDOR_BROADCOM,
488 PCI_PRODUCT_BROADCOM_BCM5782,
489 "Broadcom BCM5782 Gigabit Ethernet",
490 },
491 { PCI_VENDOR_BROADCOM,
492 PCI_PRODUCT_BROADCOM_BCM5784M,
493 "BCM5784M NetLink 1000baseT Ethernet",
494 },
495 { PCI_VENDOR_BROADCOM,
496 PCI_PRODUCT_BROADCOM_BCM5786,
497 "Broadcom BCM5786 Gigabit Ethernet",
498 },
499 { PCI_VENDOR_BROADCOM,
500 PCI_PRODUCT_BROADCOM_BCM5787,
501 "Broadcom BCM5787 Gigabit Ethernet",
502 },
503 { PCI_VENDOR_BROADCOM,
504 PCI_PRODUCT_BROADCOM_BCM5787M,
505 "Broadcom BCM5787M Gigabit Ethernet",
506 },
507 { PCI_VENDOR_BROADCOM,
508 PCI_PRODUCT_BROADCOM_BCM5788,
509 "Broadcom BCM5788 Gigabit Ethernet",
510 },
511 { PCI_VENDOR_BROADCOM,
512 PCI_PRODUCT_BROADCOM_BCM5789,
513 "Broadcom BCM5789 Gigabit Ethernet",
514 },
515 { PCI_VENDOR_BROADCOM,
516 PCI_PRODUCT_BROADCOM_BCM5901,
517 "Broadcom BCM5901 Fast Ethernet",
518 },
519 { PCI_VENDOR_BROADCOM,
520 PCI_PRODUCT_BROADCOM_BCM5901A2,
521 "Broadcom BCM5901A2 Fast Ethernet",
522 },
523 { PCI_VENDOR_BROADCOM,
524 PCI_PRODUCT_BROADCOM_BCM5903M,
525 "Broadcom BCM5903M Fast Ethernet",
526 },
527 { PCI_VENDOR_BROADCOM,
528 PCI_PRODUCT_BROADCOM_BCM5906,
529 "Broadcom BCM5906 Fast Ethernet",
530 },
531 { PCI_VENDOR_BROADCOM,
532 PCI_PRODUCT_BROADCOM_BCM5906M,
533 "Broadcom BCM5906M Fast Ethernet",
534 },
535 { PCI_VENDOR_BROADCOM,
536 PCI_PRODUCT_BROADCOM_BCM57760,
537 "Broadcom BCM57760 Fast Ethernet",
538 },
539 { PCI_VENDOR_BROADCOM,
540 PCI_PRODUCT_BROADCOM_BCM57761,
541 "Broadcom BCM57761 Fast Ethernet",
542 },
543 { PCI_VENDOR_BROADCOM,
544 PCI_PRODUCT_BROADCOM_BCM57765,
545 "Broadcom BCM57765 Fast Ethernet",
546 },
547 { PCI_VENDOR_BROADCOM,
548 PCI_PRODUCT_BROADCOM_BCM57780,
549 "Broadcom BCM57780 Fast Ethernet",
550 },
551 { PCI_VENDOR_BROADCOM,
552 PCI_PRODUCT_BROADCOM_BCM57781,
553 "Broadcom BCM57781 Fast Ethernet",
554 },
555 { PCI_VENDOR_BROADCOM,
556 PCI_PRODUCT_BROADCOM_BCM57785,
557 "Broadcom BCM57785 Fast Ethernet",
558 },
559 { PCI_VENDOR_BROADCOM,
560 PCI_PRODUCT_BROADCOM_BCM57788,
561 "Broadcom BCM57788 Fast Ethernet",
562 },
563 { PCI_VENDOR_BROADCOM,
564 PCI_PRODUCT_BROADCOM_BCM57790,
565 "Broadcom BCM57790 Fast Ethernet",
566 },
567 { PCI_VENDOR_BROADCOM,
568 PCI_PRODUCT_BROADCOM_BCM57791,
569 "Broadcom BCM57791 Fast Ethernet",
570 },
571 { PCI_VENDOR_BROADCOM,
572 PCI_PRODUCT_BROADCOM_BCM57795,
573 "Broadcom BCM57795 Fast Ethernet",
574 },
575 { PCI_VENDOR_SCHNEIDERKOCH,
576 PCI_PRODUCT_SCHNEIDERKOCH_SK_9DX1,
577 "SysKonnect SK-9Dx1 Gigabit Ethernet",
578 },
579 { PCI_VENDOR_3COM,
580 PCI_PRODUCT_3COM_3C996,
581 "3Com 3c996 Gigabit Ethernet",
582 },
583 { 0,
584 0,
585 NULL },
586 };
587
588 /*
589 * XXX: how to handle variants based on 5750 and derivatives:
590 * 5750 5751, 5721, possibly 5714, 5752, and 5708?, which
591 * in general behave like a 5705, except with additional quirks.
592 * This driver's current handling of the 5721 is wrong;
593 * how we map ASIC revision to "quirks" needs more thought.
594 * (defined here until the thought is done).
595 */
596 #define BGE_IS_5700_FAMILY(sc) ((sc)->bge_flags & BGE_5700_FAMILY)
597 #define BGE_IS_5714_FAMILY(sc) ((sc)->bge_flags & BGE_5714_FAMILY)
598 #define BGE_IS_5705_PLUS(sc) ((sc)->bge_flags & BGE_5705_PLUS)
599 #define BGE_IS_5750_OR_BEYOND(sc) ((sc)->bge_flags & BGE_5750_PLUS)
600 #define BGE_IS_5755_PLUS(sc) ((sc)->bge_flags & BGE_5755_PLUS)
601 #define BGE_IS_JUMBO_CAPABLE(sc) ((sc)->bge_flags & BGE_JUMBO_CAPABLE)
602
603 static const struct bge_revision {
604 uint32_t br_chipid;
605 const char *br_name;
606 } bge_revisions[] = {
607 { BGE_CHIPID_BCM5700_A0, "BCM5700 A0" },
608 { BGE_CHIPID_BCM5700_A1, "BCM5700 A1" },
609 { BGE_CHIPID_BCM5700_B0, "BCM5700 B0" },
610 { BGE_CHIPID_BCM5700_B1, "BCM5700 B1" },
611 { BGE_CHIPID_BCM5700_B2, "BCM5700 B2" },
612 { BGE_CHIPID_BCM5700_B3, "BCM5700 B3" },
613 /* This is treated like a BCM5700 Bx */
614 { BGE_CHIPID_BCM5700_ALTIMA, "BCM5700 Altima" },
615 { BGE_CHIPID_BCM5700_C0, "BCM5700 C0" },
616 { BGE_CHIPID_BCM5701_A0, "BCM5701 A0" },
617 { BGE_CHIPID_BCM5701_B0, "BCM5701 B0" },
618 { BGE_CHIPID_BCM5701_B2, "BCM5701 B2" },
619 { BGE_CHIPID_BCM5701_B5, "BCM5701 B5" },
620 { BGE_CHIPID_BCM5703_A0, "BCM5702/5703 A0" },
621 { BGE_CHIPID_BCM5703_A1, "BCM5702/5703 A1" },
622 { BGE_CHIPID_BCM5703_A2, "BCM5702/5703 A2" },
623 { BGE_CHIPID_BCM5703_A3, "BCM5702/5703 A3" },
624 { BGE_CHIPID_BCM5703_B0, "BCM5702/5703 B0" },
625 { BGE_CHIPID_BCM5704_A0, "BCM5704 A0" },
626 { BGE_CHIPID_BCM5704_A1, "BCM5704 A1" },
627 { BGE_CHIPID_BCM5704_A2, "BCM5704 A2" },
628 { BGE_CHIPID_BCM5704_A3, "BCM5704 A3" },
629 { BGE_CHIPID_BCM5704_B0, "BCM5704 B0" },
630 { BGE_CHIPID_BCM5705_A0, "BCM5705 A0" },
631 { BGE_CHIPID_BCM5705_A1, "BCM5705 A1" },
632 { BGE_CHIPID_BCM5705_A2, "BCM5705 A2" },
633 { BGE_CHIPID_BCM5705_A3, "BCM5705 A3" },
634 { BGE_CHIPID_BCM5750_A0, "BCM5750 A0" },
635 { BGE_CHIPID_BCM5750_A1, "BCM5750 A1" },
636 { BGE_CHIPID_BCM5750_A3, "BCM5750 A3" },
637 { BGE_CHIPID_BCM5750_B0, "BCM5750 B0" },
638 { BGE_CHIPID_BCM5750_B1, "BCM5750 B1" },
639 { BGE_CHIPID_BCM5750_C0, "BCM5750 C0" },
640 { BGE_CHIPID_BCM5750_C1, "BCM5750 C1" },
641 { BGE_CHIPID_BCM5750_C2, "BCM5750 C2" },
642 { BGE_CHIPID_BCM5752_A0, "BCM5752 A0" },
643 { BGE_CHIPID_BCM5752_A1, "BCM5752 A1" },
644 { BGE_CHIPID_BCM5752_A2, "BCM5752 A2" },
645 { BGE_CHIPID_BCM5714_A0, "BCM5714 A0" },
646 { BGE_CHIPID_BCM5714_B0, "BCM5714 B0" },
647 { BGE_CHIPID_BCM5714_B3, "BCM5714 B3" },
648 { BGE_CHIPID_BCM5715_A0, "BCM5715 A0" },
649 { BGE_CHIPID_BCM5715_A1, "BCM5715 A1" },
650 { BGE_CHIPID_BCM5715_A3, "BCM5715 A3" },
651 { BGE_CHIPID_BCM5755_A0, "BCM5755 A0" },
652 { BGE_CHIPID_BCM5755_A1, "BCM5755 A1" },
653 { BGE_CHIPID_BCM5755_A2, "BCM5755 A2" },
654 { BGE_CHIPID_BCM5755_C0, "BCM5755 C0" },
655 { BGE_CHIPID_BCM5761_A0, "BCM5761 A0" },
656 { BGE_CHIPID_BCM5761_A1, "BCM5761 A1" },
657 { BGE_CHIPID_BCM5784_A0, "BCM5784 A0" },
658 { BGE_CHIPID_BCM5784_A1, "BCM5784 A1" },
659 /* 5754 and 5787 share the same ASIC ID */
660 { BGE_CHIPID_BCM5787_A0, "BCM5754/5787 A0" },
661 { BGE_CHIPID_BCM5787_A1, "BCM5754/5787 A1" },
662 { BGE_CHIPID_BCM5787_A2, "BCM5754/5787 A2" },
663 { BGE_CHIPID_BCM5906_A1, "BCM5906 A1" },
664 { BGE_CHIPID_BCM5906_A2, "BCM5906 A2" },
665 { BGE_CHIPID_BCM57780_A0, "BCM57780 A0" },
666 { BGE_CHIPID_BCM57780_A1, "BCM57780 A1" },
667
668 { 0, NULL }
669 };
670
671 /*
672 * Some defaults for major revisions, so that newer steppings
673 * that we don't know about have a shot at working.
674 */
675 static const struct bge_revision bge_majorrevs[] = {
676 { BGE_ASICREV_BCM5700, "unknown BCM5700" },
677 { BGE_ASICREV_BCM5701, "unknown BCM5701" },
678 { BGE_ASICREV_BCM5703, "unknown BCM5703" },
679 { BGE_ASICREV_BCM5704, "unknown BCM5704" },
680 { BGE_ASICREV_BCM5705, "unknown BCM5705" },
681 { BGE_ASICREV_BCM5750, "unknown BCM5750" },
682 { BGE_ASICREV_BCM5714_A0, "unknown BCM5714" },
683 { BGE_ASICREV_BCM5752, "unknown BCM5752" },
684 { BGE_ASICREV_BCM5780, "unknown BCM5780" },
685 { BGE_ASICREV_BCM5714, "unknown BCM5714" },
686 { BGE_ASICREV_BCM5755, "unknown BCM5755" },
687 { BGE_ASICREV_BCM5761, "unknown BCM5761" },
688 { BGE_ASICREV_BCM5784, "unknown BCM5784" },
689 { BGE_ASICREV_BCM5785, "unknown BCM5785" },
690 /* 5754 and 5787 share the same ASIC ID */
691 { BGE_ASICREV_BCM5787, "unknown BCM5754/5787" },
692 { BGE_ASICREV_BCM5906, "unknown BCM5906" },
693 { BGE_ASICREV_BCM57780, "unknown BCM57780" },
694 { BGE_ASICREV_BCM5717, "unknown BCM5717" },
695 { BGE_ASICREV_BCM57765, "unknown BCM57765" },
696
697 { 0, NULL }
698 };
699
700 CFATTACH_DECL_NEW(bge, sizeof(struct bge_softc),
701 bge_probe, bge_attach, NULL, NULL);
702
703 static uint32_t
704 bge_readmem_ind(struct bge_softc *sc, int off)
705 {
706 pcireg_t val;
707
708 pci_conf_write(sc->sc_pc, sc->sc_pcitag, BGE_PCI_MEMWIN_BASEADDR, off);
709 val = pci_conf_read(sc->sc_pc, sc->sc_pcitag, BGE_PCI_MEMWIN_DATA);
710 return val;
711 }
712
713 static void
714 bge_writemem_ind(struct bge_softc *sc, int off, int val)
715 {
716 pci_conf_write(sc->sc_pc, sc->sc_pcitag, BGE_PCI_MEMWIN_BASEADDR, off);
717 pci_conf_write(sc->sc_pc, sc->sc_pcitag, BGE_PCI_MEMWIN_DATA, val);
718 }
719
720 #ifdef notdef
721 static uint32_t
722 bge_readreg_ind(struct bge_softc *sc, int off)
723 {
724 pci_conf_write(sc->sc_pc, sc->sc_pcitag, BGE_PCI_REG_BASEADDR, off);
725 return (pci_conf_read(sc->sc_pc, sc->sc_pcitag, BGE_PCI_REG_DATA));
726 }
727 #endif
728
729 static void
730 bge_writereg_ind(struct bge_softc *sc, int off, int val)
731 {
732 pci_conf_write(sc->sc_pc, sc->sc_pcitag, BGE_PCI_REG_BASEADDR, off);
733 pci_conf_write(sc->sc_pc, sc->sc_pcitag, BGE_PCI_REG_DATA, val);
734 }
735
736 static void
737 bge_writemem_direct(struct bge_softc *sc, int off, int val)
738 {
739 CSR_WRITE_4(sc, off, val);
740 }
741
742 static void
743 bge_writembx(struct bge_softc *sc, int off, int val)
744 {
745 if (BGE_ASICREV(sc->bge_chipid) == BGE_ASICREV_BCM5906)
746 off += BGE_LPMBX_IRQ0_HI - BGE_MBX_IRQ0_HI;
747
748 CSR_WRITE_4(sc, off, val);
749 }
750
751 static uint8_t
752 bge_nvram_getbyte(struct bge_softc *sc, int addr, uint8_t *dest)
753 {
754 uint32_t access, byte = 0;
755 int i;
756
757 /* Lock. */
758 CSR_WRITE_4(sc, BGE_NVRAM_SWARB, BGE_NVRAMSWARB_SET1);
759 for (i = 0; i < 8000; i++) {
760 if (CSR_READ_4(sc, BGE_NVRAM_SWARB) & BGE_NVRAMSWARB_GNT1)
761 break;
762 DELAY(20);
763 }
764 if (i == 8000)
765 return 1;
766
767 /* Enable access. */
768 access = CSR_READ_4(sc, BGE_NVRAM_ACCESS);
769 CSR_WRITE_4(sc, BGE_NVRAM_ACCESS, access | BGE_NVRAMACC_ENABLE);
770
771 CSR_WRITE_4(sc, BGE_NVRAM_ADDR, addr & 0xfffffffc);
772 CSR_WRITE_4(sc, BGE_NVRAM_CMD, BGE_NVRAM_READCMD);
773 for (i = 0; i < BGE_TIMEOUT * 10; i++) {
774 DELAY(10);
775 if (CSR_READ_4(sc, BGE_NVRAM_CMD) & BGE_NVRAMCMD_DONE) {
776 DELAY(10);
777 break;
778 }
779 }
780
781 if (i == BGE_TIMEOUT * 10) {
782 aprint_error_dev(sc->bge_dev, "nvram read timed out\n");
783 return 1;
784 }
785
786 /* Get result. */
787 byte = CSR_READ_4(sc, BGE_NVRAM_RDDATA);
788
789 *dest = (bswap32(byte) >> ((addr % 4) * 8)) & 0xFF;
790
791 /* Disable access. */
792 CSR_WRITE_4(sc, BGE_NVRAM_ACCESS, access);
793
794 /* Unlock. */
795 CSR_WRITE_4(sc, BGE_NVRAM_SWARB, BGE_NVRAMSWARB_CLR1);
796 CSR_READ_4(sc, BGE_NVRAM_SWARB);
797
798 return 0;
799 }
800
801 /*
802 * Read a sequence of bytes from NVRAM.
803 */
804 static int
805 bge_read_nvram(struct bge_softc *sc, uint8_t *dest, int off, int cnt)
806 {
807 int err = 0, i;
808 uint8_t byte = 0;
809
810 if (BGE_ASICREV(sc->bge_chipid) != BGE_ASICREV_BCM5906)
811 return 1;
812
813 for (i = 0; i < cnt; i++) {
814 err = bge_nvram_getbyte(sc, off + i, &byte);
815 if (err)
816 break;
817 *(dest + i) = byte;
818 }
819
820 return (err ? 1 : 0);
821 }
822
823 /*
824 * Read a byte of data stored in the EEPROM at address 'addr.' The
825 * BCM570x supports both the traditional bitbang interface and an
826 * auto access interface for reading the EEPROM. We use the auto
827 * access method.
828 */
829 static uint8_t
830 bge_eeprom_getbyte(struct bge_softc *sc, int addr, uint8_t *dest)
831 {
832 int i;
833 uint32_t byte = 0;
834
835 /*
836 * Enable use of auto EEPROM access so we can avoid
837 * having to use the bitbang method.
838 */
839 BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_AUTO_EEPROM);
840
841 /* Reset the EEPROM, load the clock period. */
842 CSR_WRITE_4(sc, BGE_EE_ADDR,
843 BGE_EEADDR_RESET | BGE_EEHALFCLK(BGE_HALFCLK_384SCL));
844 DELAY(20);
845
846 /* Issue the read EEPROM command. */
847 CSR_WRITE_4(sc, BGE_EE_ADDR, BGE_EE_READCMD | addr);
848
849 /* Wait for completion */
850 for (i = 0; i < BGE_TIMEOUT * 10; i++) {
851 DELAY(10);
852 if (CSR_READ_4(sc, BGE_EE_ADDR) & BGE_EEADDR_DONE)
853 break;
854 }
855
856 if (i == BGE_TIMEOUT * 10) {
857 aprint_error_dev(sc->bge_dev, "eeprom read timed out\n");
858 return 0;
859 }
860
861 /* Get result. */
862 byte = CSR_READ_4(sc, BGE_EE_DATA);
863
864 *dest = (byte >> ((addr % 4) * 8)) & 0xFF;
865
866 return 0;
867 }
868
869 /*
870 * Read a sequence of bytes from the EEPROM.
871 */
872 static int
873 bge_read_eeprom(struct bge_softc *sc, void *destv, int off, int cnt)
874 {
875 int err = 0, i;
876 uint8_t byte = 0;
877 char *dest = destv;
878
879 for (i = 0; i < cnt; i++) {
880 err = bge_eeprom_getbyte(sc, off + i, &byte);
881 if (err)
882 break;
883 *(dest + i) = byte;
884 }
885
886 return (err ? 1 : 0);
887 }
888
889 static int
890 bge_miibus_readreg(device_t dev, int phy, int reg)
891 {
892 struct bge_softc *sc = device_private(dev);
893 uint32_t val;
894 uint32_t autopoll;
895 int i;
896
897 /*
898 * Broadcom's own driver always assumes the internal
899 * PHY is at GMII address 1. On some chips, the PHY responds
900 * to accesses at all addresses, which could cause us to
901 * bogusly attach the PHY 32 times at probe type. Always
902 * restricting the lookup to address 1 is simpler than
903 * trying to figure out which chips revisions should be
904 * special-cased.
905 */
906 if (phy != 1)
907 return 0;
908
909 /* Reading with autopolling on may trigger PCI errors */
910 autopoll = CSR_READ_4(sc, BGE_MI_MODE);
911 if (autopoll & BGE_MIMODE_AUTOPOLL) {
912 BGE_STS_CLRBIT(sc, BGE_STS_AUTOPOLL);
913 BGE_CLRBIT(sc, BGE_MI_MODE, BGE_MIMODE_AUTOPOLL);
914 DELAY(40);
915 }
916
917 CSR_WRITE_4(sc, BGE_MI_COMM, BGE_MICMD_READ | BGE_MICOMM_BUSY |
918 BGE_MIPHY(phy) | BGE_MIREG(reg));
919
920 for (i = 0; i < BGE_TIMEOUT; i++) {
921 val = CSR_READ_4(sc, BGE_MI_COMM);
922 if (!(val & BGE_MICOMM_BUSY))
923 break;
924 delay(10);
925 }
926
927 if (i == BGE_TIMEOUT) {
928 aprint_error_dev(sc->bge_dev, "PHY read timed out\n");
929 val = 0;
930 goto done;
931 }
932
933 val = CSR_READ_4(sc, BGE_MI_COMM);
934
935 done:
936 if (autopoll & BGE_MIMODE_AUTOPOLL) {
937 BGE_STS_SETBIT(sc, BGE_STS_AUTOPOLL);
938 BGE_SETBIT(sc, BGE_MI_MODE, BGE_MIMODE_AUTOPOLL);
939 DELAY(40);
940 }
941
942 if (val & BGE_MICOMM_READFAIL)
943 return 0;
944
945 return (val & 0xFFFF);
946 }
947
948 static void
949 bge_miibus_writereg(device_t dev, int phy, int reg, int val)
950 {
951 struct bge_softc *sc = device_private(dev);
952 uint32_t autopoll;
953 int i;
954
955 if (phy!=1) {
956 return;
957 }
958
959 if (BGE_ASICREV(sc->bge_chipid) == BGE_ASICREV_BCM5906 &&
960 (reg == BRGPHY_MII_1000CTL || reg == BRGPHY_MII_AUXCTL)) {
961 return;
962 }
963
964 /* Reading with autopolling on may trigger PCI errors */
965 autopoll = CSR_READ_4(sc, BGE_MI_MODE);
966 if (autopoll & BGE_MIMODE_AUTOPOLL) {
967 delay(40);
968 BGE_STS_CLRBIT(sc, BGE_STS_AUTOPOLL);
969 BGE_CLRBIT(sc, BGE_MI_MODE, BGE_MIMODE_AUTOPOLL);
970 delay(10); /* 40 usec is supposed to be adequate */
971 }
972
973 CSR_WRITE_4(sc, BGE_MI_COMM, BGE_MICMD_WRITE | BGE_MICOMM_BUSY |
974 BGE_MIPHY(phy) | BGE_MIREG(reg)|val);
975
976 for (i = 0; i < BGE_TIMEOUT; i++) {
977 delay(10);
978 if (!(CSR_READ_4(sc, BGE_MI_COMM) & BGE_MICOMM_BUSY)) {
979 delay(5);
980 CSR_READ_4(sc, BGE_MI_COMM);
981 break;
982 }
983 }
984
985 if (autopoll & BGE_MIMODE_AUTOPOLL) {
986 BGE_STS_SETBIT(sc, BGE_STS_AUTOPOLL);
987 BGE_SETBIT(sc, BGE_MI_MODE, BGE_MIMODE_AUTOPOLL);
988 delay(40);
989 }
990
991 if (i == BGE_TIMEOUT)
992 aprint_error_dev(sc->bge_dev, "PHY read timed out\n");
993 }
994
995 static void
996 bge_miibus_statchg(device_t dev)
997 {
998 struct bge_softc *sc = device_private(dev);
999 struct mii_data *mii = &sc->bge_mii;
1000
1001 /*
1002 * Get flow control negotiation result.
1003 */
1004 if (IFM_SUBTYPE(mii->mii_media.ifm_cur->ifm_media) == IFM_AUTO &&
1005 (mii->mii_media_active & IFM_ETH_FMASK) != sc->bge_flowflags) {
1006 sc->bge_flowflags = mii->mii_media_active & IFM_ETH_FMASK;
1007 mii->mii_media_active &= ~IFM_ETH_FMASK;
1008 }
1009
1010 BGE_CLRBIT(sc, BGE_MAC_MODE, BGE_MACMODE_PORTMODE);
1011 if (IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_T ||
1012 IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_SX)
1013 BGE_SETBIT(sc, BGE_MAC_MODE, BGE_PORTMODE_GMII);
1014 else
1015 BGE_SETBIT(sc, BGE_MAC_MODE, BGE_PORTMODE_MII);
1016
1017 if ((mii->mii_media_active & IFM_GMASK) == IFM_FDX)
1018 BGE_CLRBIT(sc, BGE_MAC_MODE, BGE_MACMODE_HALF_DUPLEX);
1019 else
1020 BGE_SETBIT(sc, BGE_MAC_MODE, BGE_MACMODE_HALF_DUPLEX);
1021
1022 /*
1023 * 802.3x flow control
1024 */
1025 if (sc->bge_flowflags & IFM_ETH_RXPAUSE)
1026 BGE_SETBIT(sc, BGE_RX_MODE, BGE_RXMODE_FLOWCTL_ENABLE);
1027 else
1028 BGE_CLRBIT(sc, BGE_RX_MODE, BGE_RXMODE_FLOWCTL_ENABLE);
1029
1030 if (sc->bge_flowflags & IFM_ETH_TXPAUSE)
1031 BGE_SETBIT(sc, BGE_TX_MODE, BGE_TXMODE_FLOWCTL_ENABLE);
1032 else
1033 BGE_CLRBIT(sc, BGE_TX_MODE, BGE_TXMODE_FLOWCTL_ENABLE);
1034 }
1035
1036 /*
1037 * Update rx threshold levels to values in a particular slot
1038 * of the interrupt-mitigation table bge_rx_threshes.
1039 */
1040 static void
1041 bge_set_thresh(struct ifnet *ifp, int lvl)
1042 {
1043 struct bge_softc *sc = ifp->if_softc;
1044 int s;
1045
1046 /* For now, just save the new Rx-intr thresholds and record
1047 * that a threshold update is pending. Updating the hardware
1048 * registers here (even at splhigh()) is observed to
1049 * occasionaly cause glitches where Rx-interrupts are not
1050 * honoured for up to 10 seconds. jonathan (at) NetBSD.org, 2003-04-05
1051 */
1052 s = splnet();
1053 sc->bge_rx_coal_ticks = bge_rx_threshes[lvl].rx_ticks;
1054 sc->bge_rx_max_coal_bds = bge_rx_threshes[lvl].rx_max_bds;
1055 sc->bge_pending_rxintr_change = 1;
1056 splx(s);
1057
1058 return;
1059 }
1060
1061
1062 /*
1063 * Update Rx thresholds of all bge devices
1064 */
1065 static void
1066 bge_update_all_threshes(int lvl)
1067 {
1068 struct ifnet *ifp;
1069 const char * const namebuf = "bge";
1070 int namelen;
1071
1072 if (lvl < 0)
1073 lvl = 0;
1074 else if (lvl >= NBGE_RX_THRESH)
1075 lvl = NBGE_RX_THRESH - 1;
1076
1077 namelen = strlen(namebuf);
1078 /*
1079 * Now search all the interfaces for this name/number
1080 */
1081 IFNET_FOREACH(ifp) {
1082 if (strncmp(ifp->if_xname, namebuf, namelen) != 0)
1083 continue;
1084 /* We got a match: update if doing auto-threshold-tuning */
1085 if (bge_auto_thresh)
1086 bge_set_thresh(ifp, lvl);
1087 }
1088 }
1089
1090 /*
1091 * Handle events that have triggered interrupts.
1092 */
1093 static void
1094 bge_handle_events(struct bge_softc *sc)
1095 {
1096
1097 return;
1098 }
1099
1100 /*
1101 * Memory management for jumbo frames.
1102 */
1103
1104 static int
1105 bge_alloc_jumbo_mem(struct bge_softc *sc)
1106 {
1107 char *ptr, *kva;
1108 bus_dma_segment_t seg;
1109 int i, rseg, state, error;
1110 struct bge_jpool_entry *entry;
1111
1112 state = error = 0;
1113
1114 /* Grab a big chunk o' storage. */
1115 if (bus_dmamem_alloc(sc->bge_dmatag, BGE_JMEM, PAGE_SIZE, 0,
1116 &seg, 1, &rseg, BUS_DMA_NOWAIT)) {
1117 aprint_error_dev(sc->bge_dev, "can't alloc rx buffers\n");
1118 return ENOBUFS;
1119 }
1120
1121 state = 1;
1122 if (bus_dmamem_map(sc->bge_dmatag, &seg, rseg, BGE_JMEM, (void **)&kva,
1123 BUS_DMA_NOWAIT)) {
1124 aprint_error_dev(sc->bge_dev,
1125 "can't map DMA buffers (%d bytes)\n", (int)BGE_JMEM);
1126 error = ENOBUFS;
1127 goto out;
1128 }
1129
1130 state = 2;
1131 if (bus_dmamap_create(sc->bge_dmatag, BGE_JMEM, 1, BGE_JMEM, 0,
1132 BUS_DMA_NOWAIT, &sc->bge_cdata.bge_rx_jumbo_map)) {
1133 aprint_error_dev(sc->bge_dev, "can't create DMA map\n");
1134 error = ENOBUFS;
1135 goto out;
1136 }
1137
1138 state = 3;
1139 if (bus_dmamap_load(sc->bge_dmatag, sc->bge_cdata.bge_rx_jumbo_map,
1140 kva, BGE_JMEM, NULL, BUS_DMA_NOWAIT)) {
1141 aprint_error_dev(sc->bge_dev, "can't load DMA map\n");
1142 error = ENOBUFS;
1143 goto out;
1144 }
1145
1146 state = 4;
1147 sc->bge_cdata.bge_jumbo_buf = (void *)kva;
1148 DPRINTFN(1,("bge_jumbo_buf = %p\n", sc->bge_cdata.bge_jumbo_buf));
1149
1150 SLIST_INIT(&sc->bge_jfree_listhead);
1151 SLIST_INIT(&sc->bge_jinuse_listhead);
1152
1153 /*
1154 * Now divide it up into 9K pieces and save the addresses
1155 * in an array.
1156 */
1157 ptr = sc->bge_cdata.bge_jumbo_buf;
1158 for (i = 0; i < BGE_JSLOTS; i++) {
1159 sc->bge_cdata.bge_jslots[i] = ptr;
1160 ptr += BGE_JLEN;
1161 entry = malloc(sizeof(struct bge_jpool_entry),
1162 M_DEVBUF, M_NOWAIT);
1163 if (entry == NULL) {
1164 aprint_error_dev(sc->bge_dev,
1165 "no memory for jumbo buffer queue!\n");
1166 error = ENOBUFS;
1167 goto out;
1168 }
1169 entry->slot = i;
1170 SLIST_INSERT_HEAD(&sc->bge_jfree_listhead,
1171 entry, jpool_entries);
1172 }
1173 out:
1174 if (error != 0) {
1175 switch (state) {
1176 case 4:
1177 bus_dmamap_unload(sc->bge_dmatag,
1178 sc->bge_cdata.bge_rx_jumbo_map);
1179 case 3:
1180 bus_dmamap_destroy(sc->bge_dmatag,
1181 sc->bge_cdata.bge_rx_jumbo_map);
1182 case 2:
1183 bus_dmamem_unmap(sc->bge_dmatag, kva, BGE_JMEM);
1184 case 1:
1185 bus_dmamem_free(sc->bge_dmatag, &seg, rseg);
1186 break;
1187 default:
1188 break;
1189 }
1190 }
1191
1192 return error;
1193 }
1194
1195 /*
1196 * Allocate a jumbo buffer.
1197 */
1198 static void *
1199 bge_jalloc(struct bge_softc *sc)
1200 {
1201 struct bge_jpool_entry *entry;
1202
1203 entry = SLIST_FIRST(&sc->bge_jfree_listhead);
1204
1205 if (entry == NULL) {
1206 aprint_error_dev(sc->bge_dev, "no free jumbo buffers\n");
1207 return NULL;
1208 }
1209
1210 SLIST_REMOVE_HEAD(&sc->bge_jfree_listhead, jpool_entries);
1211 SLIST_INSERT_HEAD(&sc->bge_jinuse_listhead, entry, jpool_entries);
1212 return (sc->bge_cdata.bge_jslots[entry->slot]);
1213 }
1214
1215 /*
1216 * Release a jumbo buffer.
1217 */
1218 static void
1219 bge_jfree(struct mbuf *m, void *buf, size_t size, void *arg)
1220 {
1221 struct bge_jpool_entry *entry;
1222 struct bge_softc *sc;
1223 int i, s;
1224
1225 /* Extract the softc struct pointer. */
1226 sc = (struct bge_softc *)arg;
1227
1228 if (sc == NULL)
1229 panic("bge_jfree: can't find softc pointer!");
1230
1231 /* calculate the slot this buffer belongs to */
1232
1233 i = ((char *)buf
1234 - (char *)sc->bge_cdata.bge_jumbo_buf) / BGE_JLEN;
1235
1236 if ((i < 0) || (i >= BGE_JSLOTS))
1237 panic("bge_jfree: asked to free buffer that we don't manage!");
1238
1239 s = splvm();
1240 entry = SLIST_FIRST(&sc->bge_jinuse_listhead);
1241 if (entry == NULL)
1242 panic("bge_jfree: buffer not in use!");
1243 entry->slot = i;
1244 SLIST_REMOVE_HEAD(&sc->bge_jinuse_listhead, jpool_entries);
1245 SLIST_INSERT_HEAD(&sc->bge_jfree_listhead, entry, jpool_entries);
1246
1247 if (__predict_true(m != NULL))
1248 pool_cache_put(mb_cache, m);
1249 splx(s);
1250 }
1251
1252
1253 /*
1254 * Intialize a standard receive ring descriptor.
1255 */
1256 static int
1257 bge_newbuf_std(struct bge_softc *sc, int i, struct mbuf *m, bus_dmamap_t dmamap)
1258 {
1259 struct mbuf *m_new = NULL;
1260 struct bge_rx_bd *r;
1261 int error;
1262
1263 if (dmamap == NULL) {
1264 error = bus_dmamap_create(sc->bge_dmatag, MCLBYTES, 1,
1265 MCLBYTES, 0, BUS_DMA_NOWAIT, &dmamap);
1266 if (error != 0)
1267 return error;
1268 }
1269
1270 sc->bge_cdata.bge_rx_std_map[i] = dmamap;
1271
1272 if (m == NULL) {
1273 MGETHDR(m_new, M_DONTWAIT, MT_DATA);
1274 if (m_new == NULL)
1275 return ENOBUFS;
1276
1277 MCLGET(m_new, M_DONTWAIT);
1278 if (!(m_new->m_flags & M_EXT)) {
1279 m_freem(m_new);
1280 return ENOBUFS;
1281 }
1282 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
1283
1284 } else {
1285 m_new = m;
1286 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
1287 m_new->m_data = m_new->m_ext.ext_buf;
1288 }
1289 if (!(sc->bge_flags & BGE_RX_ALIGNBUG))
1290 m_adj(m_new, ETHER_ALIGN);
1291 if (bus_dmamap_load_mbuf(sc->bge_dmatag, dmamap, m_new,
1292 BUS_DMA_READ|BUS_DMA_NOWAIT))
1293 return ENOBUFS;
1294 bus_dmamap_sync(sc->bge_dmatag, dmamap, 0, dmamap->dm_mapsize,
1295 BUS_DMASYNC_PREREAD);
1296
1297 sc->bge_cdata.bge_rx_std_chain[i] = m_new;
1298 r = &sc->bge_rdata->bge_rx_std_ring[i];
1299 BGE_HOSTADDR(r->bge_addr, dmamap->dm_segs[0].ds_addr);
1300 r->bge_flags = BGE_RXBDFLAG_END;
1301 r->bge_len = m_new->m_len;
1302 r->bge_idx = i;
1303
1304 bus_dmamap_sync(sc->bge_dmatag, sc->bge_ring_map,
1305 offsetof(struct bge_ring_data, bge_rx_std_ring) +
1306 i * sizeof (struct bge_rx_bd),
1307 sizeof (struct bge_rx_bd),
1308 BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD);
1309
1310 return 0;
1311 }
1312
1313 /*
1314 * Initialize a jumbo receive ring descriptor. This allocates
1315 * a jumbo buffer from the pool managed internally by the driver.
1316 */
1317 static int
1318 bge_newbuf_jumbo(struct bge_softc *sc, int i, struct mbuf *m)
1319 {
1320 struct mbuf *m_new = NULL;
1321 struct bge_rx_bd *r;
1322 void *buf = NULL;
1323
1324 if (m == NULL) {
1325
1326 /* Allocate the mbuf. */
1327 MGETHDR(m_new, M_DONTWAIT, MT_DATA);
1328 if (m_new == NULL)
1329 return ENOBUFS;
1330
1331 /* Allocate the jumbo buffer */
1332 buf = bge_jalloc(sc);
1333 if (buf == NULL) {
1334 m_freem(m_new);
1335 aprint_error_dev(sc->bge_dev,
1336 "jumbo allocation failed -- packet dropped!\n");
1337 return ENOBUFS;
1338 }
1339
1340 /* Attach the buffer to the mbuf. */
1341 m_new->m_len = m_new->m_pkthdr.len = BGE_JUMBO_FRAMELEN;
1342 MEXTADD(m_new, buf, BGE_JUMBO_FRAMELEN, M_DEVBUF,
1343 bge_jfree, sc);
1344 m_new->m_flags |= M_EXT_RW;
1345 } else {
1346 m_new = m;
1347 buf = m_new->m_data = m_new->m_ext.ext_buf;
1348 m_new->m_ext.ext_size = BGE_JUMBO_FRAMELEN;
1349 }
1350 if (!(sc->bge_flags & BGE_RX_ALIGNBUG))
1351 m_adj(m_new, ETHER_ALIGN);
1352 bus_dmamap_sync(sc->bge_dmatag, sc->bge_cdata.bge_rx_jumbo_map,
1353 mtod(m_new, char *) - (char *)sc->bge_cdata.bge_jumbo_buf, BGE_JLEN,
1354 BUS_DMASYNC_PREREAD);
1355 /* Set up the descriptor. */
1356 r = &sc->bge_rdata->bge_rx_jumbo_ring[i];
1357 sc->bge_cdata.bge_rx_jumbo_chain[i] = m_new;
1358 BGE_HOSTADDR(r->bge_addr, BGE_JUMBO_DMA_ADDR(sc, m_new));
1359 r->bge_flags = BGE_RXBDFLAG_END|BGE_RXBDFLAG_JUMBO_RING;
1360 r->bge_len = m_new->m_len;
1361 r->bge_idx = i;
1362
1363 bus_dmamap_sync(sc->bge_dmatag, sc->bge_ring_map,
1364 offsetof(struct bge_ring_data, bge_rx_jumbo_ring) +
1365 i * sizeof (struct bge_rx_bd),
1366 sizeof (struct bge_rx_bd),
1367 BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD);
1368
1369 return 0;
1370 }
1371
1372 /*
1373 * The standard receive ring has 512 entries in it. At 2K per mbuf cluster,
1374 * that's 1MB or memory, which is a lot. For now, we fill only the first
1375 * 256 ring entries and hope that our CPU is fast enough to keep up with
1376 * the NIC.
1377 */
1378 static int
1379 bge_init_rx_ring_std(struct bge_softc *sc)
1380 {
1381 int i;
1382
1383 if (sc->bge_flags & BGE_RXRING_VALID)
1384 return 0;
1385
1386 for (i = 0; i < BGE_SSLOTS; i++) {
1387 if (bge_newbuf_std(sc, i, NULL, 0) == ENOBUFS)
1388 return ENOBUFS;
1389 }
1390
1391 sc->bge_std = i - 1;
1392 bge_writembx(sc, BGE_MBX_RX_STD_PROD_LO, sc->bge_std);
1393
1394 sc->bge_flags |= BGE_RXRING_VALID;
1395
1396 return 0;
1397 }
1398
1399 static void
1400 bge_free_rx_ring_std(struct bge_softc *sc)
1401 {
1402 int i;
1403
1404 if (!(sc->bge_flags & BGE_RXRING_VALID))
1405 return;
1406
1407 for (i = 0; i < BGE_STD_RX_RING_CNT; i++) {
1408 if (sc->bge_cdata.bge_rx_std_chain[i] != NULL) {
1409 m_freem(sc->bge_cdata.bge_rx_std_chain[i]);
1410 sc->bge_cdata.bge_rx_std_chain[i] = NULL;
1411 bus_dmamap_destroy(sc->bge_dmatag,
1412 sc->bge_cdata.bge_rx_std_map[i]);
1413 }
1414 memset((char *)&sc->bge_rdata->bge_rx_std_ring[i], 0,
1415 sizeof(struct bge_rx_bd));
1416 }
1417
1418 sc->bge_flags &= ~BGE_RXRING_VALID;
1419 }
1420
1421 static int
1422 bge_init_rx_ring_jumbo(struct bge_softc *sc)
1423 {
1424 int i;
1425 volatile struct bge_rcb *rcb;
1426
1427 if (sc->bge_flags & BGE_JUMBO_RXRING_VALID)
1428 return 0;
1429
1430 for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
1431 if (bge_newbuf_jumbo(sc, i, NULL) == ENOBUFS)
1432 return ENOBUFS;
1433 };
1434
1435 sc->bge_jumbo = i - 1;
1436 sc->bge_flags |= BGE_JUMBO_RXRING_VALID;
1437
1438 rcb = &sc->bge_rdata->bge_info.bge_jumbo_rx_rcb;
1439 rcb->bge_maxlen_flags = 0;
1440 CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_MAXLEN_FLAGS, rcb->bge_maxlen_flags);
1441
1442 bge_writembx(sc, BGE_MBX_RX_JUMBO_PROD_LO, sc->bge_jumbo);
1443
1444 return 0;
1445 }
1446
1447 static void
1448 bge_free_rx_ring_jumbo(struct bge_softc *sc)
1449 {
1450 int i;
1451
1452 if (!(sc->bge_flags & BGE_JUMBO_RXRING_VALID))
1453 return;
1454
1455 for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
1456 if (sc->bge_cdata.bge_rx_jumbo_chain[i] != NULL) {
1457 m_freem(sc->bge_cdata.bge_rx_jumbo_chain[i]);
1458 sc->bge_cdata.bge_rx_jumbo_chain[i] = NULL;
1459 }
1460 memset((char *)&sc->bge_rdata->bge_rx_jumbo_ring[i], 0,
1461 sizeof(struct bge_rx_bd));
1462 }
1463
1464 sc->bge_flags &= ~BGE_JUMBO_RXRING_VALID;
1465 }
1466
1467 static void
1468 bge_free_tx_ring(struct bge_softc *sc)
1469 {
1470 int i, freed;
1471 struct txdmamap_pool_entry *dma;
1472
1473 if (!(sc->bge_flags & BGE_TXRING_VALID))
1474 return;
1475
1476 freed = 0;
1477
1478 for (i = 0; i < BGE_TX_RING_CNT; i++) {
1479 if (sc->bge_cdata.bge_tx_chain[i] != NULL) {
1480 freed++;
1481 m_freem(sc->bge_cdata.bge_tx_chain[i]);
1482 sc->bge_cdata.bge_tx_chain[i] = NULL;
1483 SLIST_INSERT_HEAD(&sc->txdma_list, sc->txdma[i],
1484 link);
1485 sc->txdma[i] = 0;
1486 }
1487 memset((char *)&sc->bge_rdata->bge_tx_ring[i], 0,
1488 sizeof(struct bge_tx_bd));
1489 }
1490
1491 while ((dma = SLIST_FIRST(&sc->txdma_list))) {
1492 SLIST_REMOVE_HEAD(&sc->txdma_list, link);
1493 bus_dmamap_destroy(sc->bge_dmatag, dma->dmamap);
1494 free(dma, M_DEVBUF);
1495 }
1496
1497 sc->bge_flags &= ~BGE_TXRING_VALID;
1498 }
1499
1500 static int
1501 bge_init_tx_ring(struct bge_softc *sc)
1502 {
1503 int i;
1504 bus_dmamap_t dmamap;
1505 struct txdmamap_pool_entry *dma;
1506
1507 if (sc->bge_flags & BGE_TXRING_VALID)
1508 return 0;
1509
1510 sc->bge_txcnt = 0;
1511 sc->bge_tx_saved_considx = 0;
1512
1513 /* Initialize transmit producer index for host-memory send ring. */
1514 sc->bge_tx_prodidx = 0;
1515 bge_writembx(sc, BGE_MBX_TX_HOST_PROD0_LO, sc->bge_tx_prodidx);
1516 /* 5700 b2 errata */
1517 if (BGE_CHIPREV(sc->bge_chipid) == BGE_CHIPREV_5700_BX)
1518 bge_writembx(sc, BGE_MBX_TX_HOST_PROD0_LO, sc->bge_tx_prodidx);
1519
1520 /* NIC-memory send ring not used; initialize to zero. */
1521 bge_writembx(sc, BGE_MBX_TX_NIC_PROD0_LO, 0);
1522 /* 5700 b2 errata */
1523 if (BGE_CHIPREV(sc->bge_chipid) == BGE_CHIPREV_5700_BX)
1524 bge_writembx(sc, BGE_MBX_TX_NIC_PROD0_LO, 0);
1525
1526 SLIST_INIT(&sc->txdma_list);
1527 for (i = 0; i < BGE_RSLOTS; i++) {
1528 if (bus_dmamap_create(sc->bge_dmatag, BGE_TXDMA_MAX,
1529 BGE_NTXSEG, ETHER_MAX_LEN_JUMBO, 0, BUS_DMA_NOWAIT,
1530 &dmamap))
1531 return ENOBUFS;
1532 if (dmamap == NULL)
1533 panic("dmamap NULL in bge_init_tx_ring");
1534 dma = malloc(sizeof(*dma), M_DEVBUF, M_NOWAIT);
1535 if (dma == NULL) {
1536 aprint_error_dev(sc->bge_dev,
1537 "can't alloc txdmamap_pool_entry\n");
1538 bus_dmamap_destroy(sc->bge_dmatag, dmamap);
1539 return ENOMEM;
1540 }
1541 dma->dmamap = dmamap;
1542 SLIST_INSERT_HEAD(&sc->txdma_list, dma, link);
1543 }
1544
1545 sc->bge_flags |= BGE_TXRING_VALID;
1546
1547 return 0;
1548 }
1549
1550 static void
1551 bge_setmulti(struct bge_softc *sc)
1552 {
1553 struct ethercom *ac = &sc->ethercom;
1554 struct ifnet *ifp = &ac->ec_if;
1555 struct ether_multi *enm;
1556 struct ether_multistep step;
1557 uint32_t hashes[4] = { 0, 0, 0, 0 };
1558 uint32_t h;
1559 int i;
1560
1561 if (ifp->if_flags & IFF_PROMISC)
1562 goto allmulti;
1563
1564 /* Now program new ones. */
1565 ETHER_FIRST_MULTI(step, ac, enm);
1566 while (enm != NULL) {
1567 if (memcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
1568 /*
1569 * We must listen to a range of multicast addresses.
1570 * For now, just accept all multicasts, rather than
1571 * trying to set only those filter bits needed to match
1572 * the range. (At this time, the only use of address
1573 * ranges is for IP multicast routing, for which the
1574 * range is big enough to require all bits set.)
1575 */
1576 goto allmulti;
1577 }
1578
1579 h = ether_crc32_le(enm->enm_addrlo, ETHER_ADDR_LEN);
1580
1581 /* Just want the 7 least-significant bits. */
1582 h &= 0x7f;
1583
1584 hashes[(h & 0x60) >> 5] |= 1 << (h & 0x1F);
1585 ETHER_NEXT_MULTI(step, enm);
1586 }
1587
1588 ifp->if_flags &= ~IFF_ALLMULTI;
1589 goto setit;
1590
1591 allmulti:
1592 ifp->if_flags |= IFF_ALLMULTI;
1593 hashes[0] = hashes[1] = hashes[2] = hashes[3] = 0xffffffff;
1594
1595 setit:
1596 for (i = 0; i < 4; i++)
1597 CSR_WRITE_4(sc, BGE_MAR0 + (i * 4), hashes[i]);
1598 }
1599
1600 const int bge_swapbits[] = {
1601 0,
1602 BGE_MODECTL_BYTESWAP_DATA,
1603 BGE_MODECTL_WORDSWAP_DATA,
1604 BGE_MODECTL_BYTESWAP_NONFRAME,
1605 BGE_MODECTL_WORDSWAP_NONFRAME,
1606
1607 BGE_MODECTL_BYTESWAP_DATA|BGE_MODECTL_WORDSWAP_DATA,
1608 BGE_MODECTL_BYTESWAP_DATA|BGE_MODECTL_BYTESWAP_NONFRAME,
1609 BGE_MODECTL_BYTESWAP_DATA|BGE_MODECTL_WORDSWAP_NONFRAME,
1610
1611 BGE_MODECTL_WORDSWAP_DATA|BGE_MODECTL_BYTESWAP_NONFRAME,
1612 BGE_MODECTL_WORDSWAP_DATA|BGE_MODECTL_WORDSWAP_NONFRAME,
1613
1614 BGE_MODECTL_BYTESWAP_NONFRAME|BGE_MODECTL_WORDSWAP_NONFRAME,
1615
1616 BGE_MODECTL_BYTESWAP_DATA|BGE_MODECTL_WORDSWAP_DATA|
1617 BGE_MODECTL_BYTESWAP_NONFRAME,
1618 BGE_MODECTL_BYTESWAP_DATA|BGE_MODECTL_WORDSWAP_DATA|
1619 BGE_MODECTL_WORDSWAP_NONFRAME,
1620 BGE_MODECTL_BYTESWAP_DATA|BGE_MODECTL_BYTESWAP_NONFRAME|
1621 BGE_MODECTL_WORDSWAP_NONFRAME,
1622 BGE_MODECTL_WORDSWAP_DATA|BGE_MODECTL_BYTESWAP_NONFRAME|
1623 BGE_MODECTL_WORDSWAP_NONFRAME,
1624
1625 BGE_MODECTL_BYTESWAP_DATA|BGE_MODECTL_WORDSWAP_DATA|
1626 BGE_MODECTL_BYTESWAP_NONFRAME|BGE_MODECTL_WORDSWAP_NONFRAME,
1627 };
1628
1629 int bge_swapindex = 0;
1630
1631 /*
1632 * Do endian, PCI and DMA initialization. Also check the on-board ROM
1633 * self-test results.
1634 */
1635 static int
1636 bge_chipinit(struct bge_softc *sc)
1637 {
1638 int i;
1639 uint32_t dma_rw_ctl;
1640
1641
1642 /* Set endianness before we access any non-PCI registers. */
1643 pci_conf_write(sc->sc_pc, sc->sc_pcitag, BGE_PCI_MISC_CTL,
1644 BGE_INIT);
1645
1646 /* Set power state to D0. */
1647 bge_setpowerstate(sc, 0);
1648
1649 /* Clear the MAC control register */
1650 CSR_WRITE_4(sc, BGE_MAC_MODE, 0);
1651
1652 /*
1653 * Clear the MAC statistics block in the NIC's
1654 * internal memory.
1655 */
1656 for (i = BGE_STATS_BLOCK;
1657 i < BGE_STATS_BLOCK_END + 1; i += sizeof(uint32_t))
1658 BGE_MEMWIN_WRITE(sc->sc_pc, sc->sc_pcitag, i, 0);
1659
1660 for (i = BGE_STATUS_BLOCK;
1661 i < BGE_STATUS_BLOCK_END + 1; i += sizeof(uint32_t))
1662 BGE_MEMWIN_WRITE(sc->sc_pc, sc->sc_pcitag, i, 0);
1663
1664 /* Set up the PCI DMA control register. */
1665 dma_rw_ctl = BGE_PCI_READ_CMD | BGE_PCI_WRITE_CMD;
1666 if (sc->bge_flags & BGE_PCIE) {
1667 /* Read watermark not used, 128 bytes for write. */
1668 DPRINTFN(4, ("(%s: PCI-Express DMA setting)\n",
1669 device_xname(sc->bge_dev)));
1670 dma_rw_ctl |= (0x3 << BGE_PCIDMARWCTL_WR_WAT_SHIFT);
1671 } else if (sc->bge_flags & BGE_PCIX) {
1672 DPRINTFN(4, ("(:%s: PCI-X DMA setting)\n",
1673 device_xname(sc->bge_dev)));
1674 /* PCI-X bus */
1675 if (BGE_IS_5714_FAMILY(sc)) {
1676 /* 256 bytes for read and write. */
1677 dma_rw_ctl |= (0x02 << BGE_PCIDMARWCTL_RD_WAT_SHIFT) |
1678 (0x02 << BGE_PCIDMARWCTL_WR_WAT_SHIFT);
1679
1680 if (BGE_ASICREV(sc->bge_chipid) == BGE_ASICREV_BCM5780)
1681 dma_rw_ctl |= BGE_PCIDMARWCTL_ONEDMA_ATONCE_GLOBAL;
1682 else
1683 dma_rw_ctl |= BGE_PCIDMARWCTL_ONEDMA_ATONCE_LOCAL;
1684 } else if (BGE_ASICREV(sc->bge_chipid) == BGE_ASICREV_BCM5704) {
1685 /* 1536 bytes for read, 384 bytes for write. */
1686 dma_rw_ctl |=
1687 (0x7 << BGE_PCIDMARWCTL_RD_WAT_SHIFT) |
1688 (0x3 << BGE_PCIDMARWCTL_WR_WAT_SHIFT);
1689 } else {
1690 /* 384 bytes for read and write. */
1691 dma_rw_ctl |= (0x03 << BGE_PCIDMARWCTL_RD_WAT_SHIFT) |
1692 (0x03 << BGE_PCIDMARWCTL_WR_WAT_SHIFT) |
1693 (0x0F);
1694 }
1695
1696 if (BGE_ASICREV(sc->bge_chipid) == BGE_ASICREV_BCM5703 ||
1697 BGE_ASICREV(sc->bge_chipid) == BGE_ASICREV_BCM5704) {
1698 uint32_t tmp;
1699
1700 /* Set ONEDMA_ATONCE for hardware workaround. */
1701 tmp = CSR_READ_4(sc, BGE_PCI_CLKCTL) & 0x1f;
1702 if (tmp == 6 || tmp == 7)
1703 dma_rw_ctl |=
1704 BGE_PCIDMARWCTL_ONEDMA_ATONCE_GLOBAL;
1705
1706 /* Set PCI-X DMA write workaround. */
1707 dma_rw_ctl |= BGE_PCIDMARWCTL_ASRT_ALL_BE;
1708 }
1709 } else {
1710 /* Conventional PCI bus: 256 bytes for read and write. */
1711 DPRINTFN(4, ("(%s: PCI 2.2 DMA setting)\n",
1712 device_xname(sc->bge_dev)));
1713 dma_rw_ctl = (0x7 << BGE_PCIDMARWCTL_RD_WAT_SHIFT) |
1714 (0x7 << BGE_PCIDMARWCTL_WR_WAT_SHIFT);
1715 if (BGE_ASICREV(sc->bge_chipid) != BGE_ASICREV_BCM5705 &&
1716 BGE_ASICREV(sc->bge_chipid) != BGE_ASICREV_BCM5750)
1717 dma_rw_ctl |= 0x0F;
1718 }
1719
1720 if (BGE_ASICREV(sc->bge_chipid) == BGE_ASICREV_BCM5700 ||
1721 BGE_ASICREV(sc->bge_chipid) == BGE_ASICREV_BCM5701)
1722 dma_rw_ctl |= BGE_PCIDMARWCTL_USE_MRM |
1723 BGE_PCIDMARWCTL_ASRT_ALL_BE;
1724
1725 if (BGE_ASICREV(sc->bge_chipid) == BGE_ASICREV_BCM5703 ||
1726 BGE_ASICREV(sc->bge_chipid) == BGE_ASICREV_BCM5704)
1727 dma_rw_ctl &= ~BGE_PCIDMARWCTL_MINDMA;
1728
1729 pci_conf_write(sc->sc_pc, sc->sc_pcitag, BGE_PCI_DMA_RW_CTL, dma_rw_ctl);
1730
1731 /*
1732 * Set up general mode register.
1733 */
1734 CSR_WRITE_4(sc, BGE_MODE_CTL, BGE_DMA_SWAP_OPTIONS |
1735 BGE_MODECTL_MAC_ATTN_INTR | BGE_MODECTL_HOST_SEND_BDS |
1736 BGE_MODECTL_TX_NO_PHDR_CSUM | BGE_MODECTL_RX_NO_PHDR_CSUM);
1737
1738 /*
1739 * BCM5701 B5 have a bug causing data corruption when using
1740 * 64-bit DMA reads, which can be terminated early and then
1741 * completed later as 32-bit accesses, in combination with
1742 * certain bridges.
1743 */
1744 if (BGE_ASICREV(sc->bge_chipid) == BGE_ASICREV_BCM5701 &&
1745 sc->bge_chipid == BGE_CHIPID_BCM5701_B5)
1746 BGE_SETBIT(sc, BGE_MODE_CTL, BGE_MODECTL_FORCE_PCI32);
1747
1748 /*
1749 * Disable memory write invalidate. Apparently it is not supported
1750 * properly by these devices.
1751 */
1752 PCI_CLRBIT(sc->sc_pc, sc->sc_pcitag, PCI_COMMAND_STATUS_REG,
1753 PCI_COMMAND_INVALIDATE_ENABLE);
1754
1755 #ifdef __brokenalpha__
1756 /*
1757 * Must insure that we do not cross an 8K (bytes) boundary
1758 * for DMA reads. Our highest limit is 1K bytes. This is a
1759 * restriction on some ALPHA platforms with early revision
1760 * 21174 PCI chipsets, such as the AlphaPC 164lx
1761 */
1762 PCI_SETBIT(sc, BGE_PCI_DMA_RW_CTL, BGE_PCI_READ_BNDRY_1024, 4);
1763 #endif
1764
1765 /* Set the timer prescaler (always 66MHz) */
1766 CSR_WRITE_4(sc, BGE_MISC_CFG, 65 << 1/*BGE_32BITTIME_66MHZ*/);
1767
1768 if (BGE_ASICREV(sc->bge_chipid) == BGE_ASICREV_BCM5906) {
1769 DELAY(40); /* XXX */
1770
1771 /* Put PHY into ready state */
1772 BGE_CLRBIT(sc, BGE_MISC_CFG, BGE_MISCCFG_EPHY_IDDQ);
1773 CSR_READ_4(sc, BGE_MISC_CFG); /* Flush */
1774 DELAY(40);
1775 }
1776
1777 return 0;
1778 }
1779
1780 static int
1781 bge_blockinit(struct bge_softc *sc)
1782 {
1783 volatile struct bge_rcb *rcb;
1784 bus_size_t rcb_addr;
1785 int i;
1786 struct ifnet *ifp = &sc->ethercom.ec_if;
1787 bge_hostaddr taddr;
1788 uint32_t val;
1789
1790 /*
1791 * Initialize the memory window pointer register so that
1792 * we can access the first 32K of internal NIC RAM. This will
1793 * allow us to set up the TX send ring RCBs and the RX return
1794 * ring RCBs, plus other things which live in NIC memory.
1795 */
1796
1797 pci_conf_write(sc->sc_pc, sc->sc_pcitag, BGE_PCI_MEMWIN_BASEADDR, 0);
1798
1799 /* Configure mbuf memory pool */
1800 if (BGE_IS_5700_FAMILY(sc)) {
1801 CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_BASEADDR,
1802 BGE_BUFFPOOL_1);
1803
1804 if (BGE_ASICREV(sc->bge_chipid) == BGE_ASICREV_BCM5704)
1805 CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_LEN, 0x10000);
1806 else
1807 CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_LEN, 0x18000);
1808
1809 /* Configure DMA resource pool */
1810 CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_BASEADDR,
1811 BGE_DMA_DESCRIPTORS);
1812 CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_LEN, 0x2000);
1813 }
1814
1815 /* Configure mbuf pool watermarks */
1816 #ifdef ORIG_WPAUL_VALUES
1817 CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_READDMA_LOWAT, 24);
1818 CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 24);
1819 CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_HIWAT, 48);
1820 #else
1821
1822 /* new broadcom docs strongly recommend these: */
1823 if (!BGE_IS_5705_PLUS(sc)) {
1824 if (ifp->if_mtu > ETHER_MAX_LEN) {
1825 CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_READDMA_LOWAT, 0x50);
1826 CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 0x20);
1827 CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_HIWAT, 0x60);
1828 } else {
1829 CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_READDMA_LOWAT, 304);
1830 CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 152);
1831 CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_HIWAT, 380);
1832 }
1833 } else if (BGE_ASICREV(sc->bge_chipid) == BGE_ASICREV_BCM5906) {
1834 CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_READDMA_LOWAT, 0x0);
1835 CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 0x04);
1836 CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_HIWAT, 0x10);
1837 } else {
1838 CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_READDMA_LOWAT, 0x0);
1839 CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_MACRX_LOWAT, 0x10);
1840 CSR_WRITE_4(sc, BGE_BMAN_MBUFPOOL_HIWAT, 0x60);
1841 }
1842 #endif
1843
1844 /* Configure DMA resource watermarks */
1845 CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_LOWAT, 5);
1846 CSR_WRITE_4(sc, BGE_BMAN_DMA_DESCPOOL_HIWAT, 10);
1847
1848 /* Enable buffer manager */
1849 CSR_WRITE_4(sc, BGE_BMAN_MODE,
1850 BGE_BMANMODE_ENABLE | BGE_BMANMODE_LOMBUF_ATTN);
1851
1852 /* Poll for buffer manager start indication */
1853 for (i = 0; i < BGE_TIMEOUT * 2; i++) {
1854 if (CSR_READ_4(sc, BGE_BMAN_MODE) & BGE_BMANMODE_ENABLE)
1855 break;
1856 DELAY(10);
1857 }
1858
1859 if (i == BGE_TIMEOUT * 2) {
1860 aprint_error_dev(sc->bge_dev,
1861 "buffer manager failed to start\n");
1862 return ENXIO;
1863 }
1864
1865 /* Enable flow-through queues */
1866 CSR_WRITE_4(sc, BGE_FTQ_RESET, 0xFFFFFFFF);
1867 CSR_WRITE_4(sc, BGE_FTQ_RESET, 0);
1868
1869 /* Wait until queue initialization is complete */
1870 for (i = 0; i < BGE_TIMEOUT * 2; i++) {
1871 if (CSR_READ_4(sc, BGE_FTQ_RESET) == 0)
1872 break;
1873 DELAY(10);
1874 }
1875
1876 if (i == BGE_TIMEOUT * 2) {
1877 aprint_error_dev(sc->bge_dev,
1878 "flow-through queue init failed\n");
1879 return ENXIO;
1880 }
1881
1882 /* Initialize the standard RX ring control block */
1883 rcb = &sc->bge_rdata->bge_info.bge_std_rx_rcb;
1884 BGE_HOSTADDR(rcb->bge_hostaddr, BGE_RING_DMA_ADDR(sc, bge_rx_std_ring));
1885 if (BGE_IS_5705_PLUS(sc))
1886 rcb->bge_maxlen_flags = BGE_RCB_MAXLEN_FLAGS(512, 0);
1887 else
1888 rcb->bge_maxlen_flags =
1889 BGE_RCB_MAXLEN_FLAGS(BGE_MAX_FRAMELEN, 0);
1890 rcb->bge_nicaddr = BGE_STD_RX_RINGS;
1891 CSR_WRITE_4(sc, BGE_RX_STD_RCB_HADDR_HI, rcb->bge_hostaddr.bge_addr_hi);
1892 CSR_WRITE_4(sc, BGE_RX_STD_RCB_HADDR_LO, rcb->bge_hostaddr.bge_addr_lo);
1893 CSR_WRITE_4(sc, BGE_RX_STD_RCB_MAXLEN_FLAGS, rcb->bge_maxlen_flags);
1894 CSR_WRITE_4(sc, BGE_RX_STD_RCB_NICADDR, rcb->bge_nicaddr);
1895
1896 /*
1897 * Initialize the jumbo RX ring control block
1898 * We set the 'ring disabled' bit in the flags
1899 * field until we're actually ready to start
1900 * using this ring (i.e. once we set the MTU
1901 * high enough to require it).
1902 */
1903 if (BGE_IS_JUMBO_CAPABLE(sc)) {
1904 rcb = &sc->bge_rdata->bge_info.bge_jumbo_rx_rcb;
1905 BGE_HOSTADDR(rcb->bge_hostaddr,
1906 BGE_RING_DMA_ADDR(sc, bge_rx_jumbo_ring));
1907 rcb->bge_maxlen_flags =
1908 BGE_RCB_MAXLEN_FLAGS(BGE_MAX_FRAMELEN,
1909 BGE_RCB_FLAG_RING_DISABLED);
1910 rcb->bge_nicaddr = BGE_JUMBO_RX_RINGS;
1911 CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_HADDR_HI,
1912 rcb->bge_hostaddr.bge_addr_hi);
1913 CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_HADDR_LO,
1914 rcb->bge_hostaddr.bge_addr_lo);
1915 CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_MAXLEN_FLAGS,
1916 rcb->bge_maxlen_flags);
1917 CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_NICADDR, rcb->bge_nicaddr);
1918
1919 /* Set up dummy disabled mini ring RCB */
1920 rcb = &sc->bge_rdata->bge_info.bge_mini_rx_rcb;
1921 rcb->bge_maxlen_flags = BGE_RCB_MAXLEN_FLAGS(0,
1922 BGE_RCB_FLAG_RING_DISABLED);
1923 CSR_WRITE_4(sc, BGE_RX_MINI_RCB_MAXLEN_FLAGS,
1924 rcb->bge_maxlen_flags);
1925
1926 bus_dmamap_sync(sc->bge_dmatag, sc->bge_ring_map,
1927 offsetof(struct bge_ring_data, bge_info),
1928 sizeof (struct bge_gib),
1929 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
1930 }
1931
1932 /*
1933 * Set the BD ring replenish thresholds. The recommended
1934 * values are 1/8th the number of descriptors allocated to
1935 * each ring.
1936 */
1937 i = BGE_STD_RX_RING_CNT / 8;
1938
1939 /*
1940 * Use a value of 8 for the following chips to workaround HW errata.
1941 * Some of these chips have been added based on empirical
1942 * evidence (they don't work unless this is done).
1943 */
1944 if (BGE_IS_5705_PLUS(sc))
1945 i = 8;
1946
1947 CSR_WRITE_4(sc, BGE_RBDI_STD_REPL_THRESH, i);
1948 CSR_WRITE_4(sc, BGE_RBDI_JUMBO_REPL_THRESH, BGE_JUMBO_RX_RING_CNT / 8);
1949
1950 if (BGE_ASICREV(sc->bge_chipid) == BGE_ASICREV_BCM5717 ||
1951 BGE_ASICREV(sc->bge_chipid) == BGE_ASICREV_BCM57765) {
1952 CSR_WRITE_4(sc, BGE_STD_REPL_LWM, 4);
1953 CSR_WRITE_4(sc, BGE_JUMBO_REPL_LWM, 4);
1954 }
1955
1956 /*
1957 * Disable all unused send rings by setting the 'ring disabled'
1958 * bit in the flags field of all the TX send ring control blocks.
1959 * These are located in NIC memory.
1960 */
1961 rcb_addr = BGE_MEMWIN_START + BGE_SEND_RING_RCB;
1962 for (i = 0; i < BGE_TX_RINGS_EXTSSRAM_MAX; i++) {
1963 RCB_WRITE_4(sc, rcb_addr, bge_maxlen_flags,
1964 BGE_RCB_MAXLEN_FLAGS(0, BGE_RCB_FLAG_RING_DISABLED));
1965 RCB_WRITE_4(sc, rcb_addr, bge_nicaddr, 0);
1966 rcb_addr += sizeof(struct bge_rcb);
1967 }
1968
1969 /* Configure TX RCB 0 (we use only the first ring) */
1970 rcb_addr = BGE_MEMWIN_START + BGE_SEND_RING_RCB;
1971 BGE_HOSTADDR(taddr, BGE_RING_DMA_ADDR(sc, bge_tx_ring));
1972 RCB_WRITE_4(sc, rcb_addr, bge_hostaddr.bge_addr_hi, taddr.bge_addr_hi);
1973 RCB_WRITE_4(sc, rcb_addr, bge_hostaddr.bge_addr_lo, taddr.bge_addr_lo);
1974 RCB_WRITE_4(sc, rcb_addr, bge_nicaddr,
1975 BGE_NIC_TXRING_ADDR(0, BGE_TX_RING_CNT));
1976 if (BGE_IS_5700_FAMILY(sc))
1977 RCB_WRITE_4(sc, rcb_addr, bge_maxlen_flags,
1978 BGE_RCB_MAXLEN_FLAGS(BGE_TX_RING_CNT, 0));
1979
1980 /* Disable all unused RX return rings */
1981 rcb_addr = BGE_MEMWIN_START + BGE_RX_RETURN_RING_RCB;
1982 for (i = 0; i < BGE_RX_RINGS_MAX; i++) {
1983 RCB_WRITE_4(sc, rcb_addr, bge_hostaddr.bge_addr_hi, 0);
1984 RCB_WRITE_4(sc, rcb_addr, bge_hostaddr.bge_addr_lo, 0);
1985 RCB_WRITE_4(sc, rcb_addr, bge_maxlen_flags,
1986 BGE_RCB_MAXLEN_FLAGS(sc->bge_return_ring_cnt,
1987 BGE_RCB_FLAG_RING_DISABLED));
1988 RCB_WRITE_4(sc, rcb_addr, bge_nicaddr, 0);
1989 bge_writembx(sc, BGE_MBX_RX_CONS0_LO +
1990 (i * (sizeof(uint64_t))), 0);
1991 rcb_addr += sizeof(struct bge_rcb);
1992 }
1993
1994 /* Initialize RX ring indexes */
1995 bge_writembx(sc, BGE_MBX_RX_STD_PROD_LO, 0);
1996 bge_writembx(sc, BGE_MBX_RX_JUMBO_PROD_LO, 0);
1997 bge_writembx(sc, BGE_MBX_RX_MINI_PROD_LO, 0);
1998
1999 /*
2000 * Set up RX return ring 0
2001 * Note that the NIC address for RX return rings is 0x00000000.
2002 * The return rings live entirely within the host, so the
2003 * nicaddr field in the RCB isn't used.
2004 */
2005 rcb_addr = BGE_MEMWIN_START + BGE_RX_RETURN_RING_RCB;
2006 BGE_HOSTADDR(taddr, BGE_RING_DMA_ADDR(sc, bge_rx_return_ring));
2007 RCB_WRITE_4(sc, rcb_addr, bge_hostaddr.bge_addr_hi, taddr.bge_addr_hi);
2008 RCB_WRITE_4(sc, rcb_addr, bge_hostaddr.bge_addr_lo, taddr.bge_addr_lo);
2009 RCB_WRITE_4(sc, rcb_addr, bge_nicaddr, 0x00000000);
2010 RCB_WRITE_4(sc, rcb_addr, bge_maxlen_flags,
2011 BGE_RCB_MAXLEN_FLAGS(sc->bge_return_ring_cnt, 0));
2012
2013 /* Set random backoff seed for TX */
2014 CSR_WRITE_4(sc, BGE_TX_RANDOM_BACKOFF,
2015 CLLADDR(ifp->if_sadl)[0] + CLLADDR(ifp->if_sadl)[1] +
2016 CLLADDR(ifp->if_sadl)[2] + CLLADDR(ifp->if_sadl)[3] +
2017 CLLADDR(ifp->if_sadl)[4] + CLLADDR(ifp->if_sadl)[5] +
2018 BGE_TX_BACKOFF_SEED_MASK);
2019
2020 /* Set inter-packet gap */
2021 CSR_WRITE_4(sc, BGE_TX_LENGTHS, 0x2620);
2022
2023 /*
2024 * Specify which ring to use for packets that don't match
2025 * any RX rules.
2026 */
2027 CSR_WRITE_4(sc, BGE_RX_RULES_CFG, 0x08);
2028
2029 /*
2030 * Configure number of RX lists. One interrupt distribution
2031 * list, sixteen active lists, one bad frames class.
2032 */
2033 CSR_WRITE_4(sc, BGE_RXLP_CFG, 0x181);
2034
2035 /* Inialize RX list placement stats mask. */
2036 CSR_WRITE_4(sc, BGE_RXLP_STATS_ENABLE_MASK, 0x007FFFFF);
2037 CSR_WRITE_4(sc, BGE_RXLP_STATS_CTL, 0x1);
2038
2039 /* Disable host coalescing until we get it set up */
2040 CSR_WRITE_4(sc, BGE_HCC_MODE, 0x00000000);
2041
2042 /* Poll to make sure it's shut down. */
2043 for (i = 0; i < BGE_TIMEOUT * 2; i++) {
2044 if (!(CSR_READ_4(sc, BGE_HCC_MODE) & BGE_HCCMODE_ENABLE))
2045 break;
2046 DELAY(10);
2047 }
2048
2049 if (i == BGE_TIMEOUT * 2) {
2050 aprint_error_dev(sc->bge_dev,
2051 "host coalescing engine failed to idle\n");
2052 return ENXIO;
2053 }
2054
2055 /* Set up host coalescing defaults */
2056 CSR_WRITE_4(sc, BGE_HCC_RX_COAL_TICKS, sc->bge_rx_coal_ticks);
2057 CSR_WRITE_4(sc, BGE_HCC_TX_COAL_TICKS, sc->bge_tx_coal_ticks);
2058 CSR_WRITE_4(sc, BGE_HCC_RX_MAX_COAL_BDS, sc->bge_rx_max_coal_bds);
2059 CSR_WRITE_4(sc, BGE_HCC_TX_MAX_COAL_BDS, sc->bge_tx_max_coal_bds);
2060 if (BGE_IS_5700_FAMILY(sc)) {
2061 CSR_WRITE_4(sc, BGE_HCC_RX_COAL_TICKS_INT, 0);
2062 CSR_WRITE_4(sc, BGE_HCC_TX_COAL_TICKS_INT, 0);
2063 }
2064 CSR_WRITE_4(sc, BGE_HCC_RX_MAX_COAL_BDS_INT, 0);
2065 CSR_WRITE_4(sc, BGE_HCC_TX_MAX_COAL_BDS_INT, 0);
2066
2067 /* Set up address of statistics block */
2068 if (BGE_IS_5700_FAMILY(sc)) {
2069 BGE_HOSTADDR(taddr, BGE_RING_DMA_ADDR(sc, bge_info.bge_stats));
2070 CSR_WRITE_4(sc, BGE_HCC_STATS_TICKS, sc->bge_stat_ticks);
2071 CSR_WRITE_4(sc, BGE_HCC_STATS_BASEADDR, BGE_STATS_BLOCK);
2072 CSR_WRITE_4(sc, BGE_HCC_STATS_ADDR_HI, taddr.bge_addr_hi);
2073 CSR_WRITE_4(sc, BGE_HCC_STATS_ADDR_LO, taddr.bge_addr_lo);
2074 }
2075
2076 /* Set up address of status block */
2077 BGE_HOSTADDR(taddr, BGE_RING_DMA_ADDR(sc, bge_status_block));
2078 CSR_WRITE_4(sc, BGE_HCC_STATUSBLK_BASEADDR, BGE_STATUS_BLOCK);
2079 CSR_WRITE_4(sc, BGE_HCC_STATUSBLK_ADDR_HI, taddr.bge_addr_hi);
2080 CSR_WRITE_4(sc, BGE_HCC_STATUSBLK_ADDR_LO, taddr.bge_addr_lo);
2081 sc->bge_rdata->bge_status_block.bge_idx[0].bge_rx_prod_idx = 0;
2082 sc->bge_rdata->bge_status_block.bge_idx[0].bge_tx_cons_idx = 0;
2083
2084 /* Turn on host coalescing state machine */
2085 CSR_WRITE_4(sc, BGE_HCC_MODE, BGE_HCCMODE_ENABLE);
2086
2087 /* Turn on RX BD completion state machine and enable attentions */
2088 CSR_WRITE_4(sc, BGE_RBDC_MODE,
2089 BGE_RBDCMODE_ENABLE | BGE_RBDCMODE_ATTN);
2090
2091 /* Turn on RX list placement state machine */
2092 CSR_WRITE_4(sc, BGE_RXLP_MODE, BGE_RXLPMODE_ENABLE);
2093
2094 /* Turn on RX list selector state machine. */
2095 if (BGE_IS_5700_FAMILY(sc))
2096 CSR_WRITE_4(sc, BGE_RXLS_MODE, BGE_RXLSMODE_ENABLE);
2097
2098 val = BGE_MACMODE_TXDMA_ENB | BGE_MACMODE_RXDMA_ENB |
2099 BGE_MACMODE_RX_STATS_CLEAR | BGE_MACMODE_TX_STATS_CLEAR |
2100 BGE_MACMODE_RX_STATS_ENB | BGE_MACMODE_TX_STATS_ENB |
2101 BGE_MACMODE_FRMHDR_DMA_ENB;
2102
2103 if (sc->bge_flags & BGE_PHY_FIBER_TBI)
2104 val |= BGE_PORTMODE_TBI;
2105 else if (sc->bge_flags & BGE_PHY_FIBER_MII)
2106 val |= BGE_PORTMODE_GMII;
2107 else
2108 val |= BGE_PORTMODE_MII;
2109
2110 /* Turn on DMA, clear stats */
2111 CSR_WRITE_4(sc, BGE_MAC_MODE, val);
2112
2113
2114 /* Set misc. local control, enable interrupts on attentions */
2115 sc->bge_local_ctrl_reg = BGE_MLC_INTR_ONATTN | BGE_MLC_AUTO_EEPROM;
2116
2117 #ifdef notdef
2118 /* Assert GPIO pins for PHY reset */
2119 BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_MISCIO_OUT0|
2120 BGE_MLC_MISCIO_OUT1|BGE_MLC_MISCIO_OUT2);
2121 BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_MISCIO_OUTEN0|
2122 BGE_MLC_MISCIO_OUTEN1|BGE_MLC_MISCIO_OUTEN2);
2123 #endif
2124
2125 #if defined(not_quite_yet)
2126 /* Linux driver enables enable gpio pin #1 on 5700s */
2127 if (sc->bge_chipid == BGE_CHIPID_BCM5700) {
2128 sc->bge_local_ctrl_reg |=
2129 (BGE_MLC_MISCIO_OUT1|BGE_MLC_MISCIO_OUTEN1);
2130 }
2131 #endif
2132 CSR_WRITE_4(sc, BGE_MISC_LOCAL_CTL, sc->bge_local_ctrl_reg);
2133
2134 /* Turn on DMA completion state machine */
2135 if (BGE_IS_5700_FAMILY(sc))
2136 CSR_WRITE_4(sc, BGE_DMAC_MODE, BGE_DMACMODE_ENABLE);
2137
2138 /* Turn on write DMA state machine */
2139 {
2140 uint32_t bge_wdma_mode =
2141 BGE_WDMAMODE_ENABLE|BGE_WDMAMODE_ALL_ATTNS;
2142
2143 /* Enable host coalescing bug fix; see Linux tg3.c */
2144 if (BGE_IS_5755_PLUS(sc))
2145 bge_wdma_mode |= BGE_WDMAMODE_STATUS_TAG_FIX;
2146
2147 CSR_WRITE_4(sc, BGE_WDMA_MODE, bge_wdma_mode);
2148 }
2149
2150 /* Turn on read DMA state machine */
2151 {
2152 uint32_t dma_read_modebits;
2153
2154 dma_read_modebits =
2155 BGE_RDMAMODE_ENABLE | BGE_RDMAMODE_ALL_ATTNS;
2156
2157 if (BGE_ASICREV(sc->bge_chipid) == BGE_ASICREV_BCM5784 ||
2158 BGE_ASICREV(sc->bge_chipid) == BGE_ASICREV_BCM5785 ||
2159 BGE_ASICREV(sc->bge_chipid) == BGE_ASICREV_BCM57780)
2160 dma_read_modebits |= BGE_RDMAMODE_BD_SBD_CRPT_ATTN |
2161 BGE_RDMAMODE_MBUF_RBD_CRPT_ATTN |
2162 BGE_RDMAMODE_MBUF_SBD_CRPT_ATTN;
2163
2164 if (sc->bge_flags & BGE_PCIE)
2165 dma_read_modebits |= BGE_RDMA_MODE_FIFO_LONG_BURST;
2166 if (sc->bge_flags & BGE_TSO)
2167 dma_read_modebits |= BGE_RDMAMODE_TSO4_ENABLE;
2168 CSR_WRITE_4(sc, BGE_RDMA_MODE, dma_read_modebits);
2169 delay(40);
2170 }
2171
2172 /* Turn on RX data completion state machine */
2173 CSR_WRITE_4(sc, BGE_RDC_MODE, BGE_RDCMODE_ENABLE);
2174
2175 /* Turn on RX BD initiator state machine */
2176 CSR_WRITE_4(sc, BGE_RBDI_MODE, BGE_RBDIMODE_ENABLE);
2177
2178 /* Turn on RX data and RX BD initiator state machine */
2179 CSR_WRITE_4(sc, BGE_RDBDI_MODE, BGE_RDBDIMODE_ENABLE);
2180
2181 /* Turn on Mbuf cluster free state machine */
2182 if (BGE_IS_5700_FAMILY(sc))
2183 CSR_WRITE_4(sc, BGE_MBCF_MODE, BGE_MBCFMODE_ENABLE);
2184
2185 /* Turn on send BD completion state machine */
2186 CSR_WRITE_4(sc, BGE_SBDC_MODE, BGE_SBDCMODE_ENABLE);
2187
2188 /* Turn on send data completion state machine */
2189 val = BGE_SDCMODE_ENABLE;
2190 if (BGE_ASICREV(sc->bge_chipid) == BGE_ASICREV_BCM5761)
2191 val |= BGE_SDCMODE_CDELAY;
2192 CSR_WRITE_4(sc, BGE_SDC_MODE, val);
2193
2194 /* Turn on send data initiator state machine */
2195 if (sc->bge_flags & BGE_TSO) {
2196 /* XXX: magic value from Linux driver */
2197 CSR_WRITE_4(sc, BGE_SDI_MODE, BGE_SDIMODE_ENABLE | 0x08);
2198 } else {
2199 CSR_WRITE_4(sc, BGE_SDI_MODE, BGE_SDIMODE_ENABLE);
2200 }
2201
2202 /* Turn on send BD initiator state machine */
2203 CSR_WRITE_4(sc, BGE_SBDI_MODE, BGE_SBDIMODE_ENABLE);
2204
2205 /* Turn on send BD selector state machine */
2206 CSR_WRITE_4(sc, BGE_SRS_MODE, BGE_SRSMODE_ENABLE);
2207
2208 CSR_WRITE_4(sc, BGE_SDI_STATS_ENABLE_MASK, 0x007FFFFF);
2209 CSR_WRITE_4(sc, BGE_SDI_STATS_CTL,
2210 BGE_SDISTATSCTL_ENABLE | BGE_SDISTATSCTL_FASTER);
2211
2212 /* ack/clear link change events */
2213 CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED |
2214 BGE_MACSTAT_CFG_CHANGED | BGE_MACSTAT_MI_COMPLETE |
2215 BGE_MACSTAT_LINK_CHANGED);
2216 CSR_WRITE_4(sc, BGE_MI_STS, 0);
2217
2218 /* Enable PHY auto polling (for MII/GMII only) */
2219 if (sc->bge_flags & BGE_PHY_FIBER_TBI) {
2220 CSR_WRITE_4(sc, BGE_MI_STS, BGE_MISTS_LINK);
2221 } else {
2222 BGE_STS_SETBIT(sc, BGE_STS_AUTOPOLL);
2223 BGE_SETBIT(sc, BGE_MI_MODE, BGE_MIMODE_AUTOPOLL | (10 << 16));
2224 if (BGE_ASICREV(sc->bge_chipid) == BGE_ASICREV_BCM5700)
2225 CSR_WRITE_4(sc, BGE_MAC_EVT_ENB,
2226 BGE_EVTENB_MI_INTERRUPT);
2227 }
2228
2229 /*
2230 * Clear any pending link state attention.
2231 * Otherwise some link state change events may be lost until attention
2232 * is cleared by bge_intr() -> bge_link_upd() sequence.
2233 * It's not necessary on newer BCM chips - perhaps enabling link
2234 * state change attentions implies clearing pending attention.
2235 */
2236 CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED |
2237 BGE_MACSTAT_CFG_CHANGED | BGE_MACSTAT_MI_COMPLETE |
2238 BGE_MACSTAT_LINK_CHANGED);
2239
2240 /* Enable link state change attentions. */
2241 BGE_SETBIT(sc, BGE_MAC_EVT_ENB, BGE_EVTENB_LINK_CHANGED);
2242
2243 return 0;
2244 }
2245
2246 static const struct bge_revision *
2247 bge_lookup_rev(uint32_t chipid)
2248 {
2249 const struct bge_revision *br;
2250
2251 for (br = bge_revisions; br->br_name != NULL; br++) {
2252 if (br->br_chipid == chipid)
2253 return br;
2254 }
2255
2256 for (br = bge_majorrevs; br->br_name != NULL; br++) {
2257 if (br->br_chipid == BGE_ASICREV(chipid))
2258 return br;
2259 }
2260
2261 return NULL;
2262 }
2263
2264 static const struct bge_product *
2265 bge_lookup(const struct pci_attach_args *pa)
2266 {
2267 const struct bge_product *bp;
2268
2269 for (bp = bge_products; bp->bp_name != NULL; bp++) {
2270 if (PCI_VENDOR(pa->pa_id) == bp->bp_vendor &&
2271 PCI_PRODUCT(pa->pa_id) == bp->bp_product)
2272 return bp;
2273 }
2274
2275 return NULL;
2276 }
2277
2278 static int
2279 bge_setpowerstate(struct bge_softc *sc, int powerlevel)
2280 {
2281 #ifdef NOTYET
2282 uint32_t pm_ctl = 0;
2283
2284 /* XXX FIXME: make sure indirect accesses enabled? */
2285 pm_ctl = pci_conf_read(sc->bge_dev, BGE_PCI_MISC_CTL, 4);
2286 pm_ctl |= BGE_PCIMISCCTL_INDIRECT_ACCESS;
2287 pci_write_config(sc->bge_dev, BGE_PCI_MISC_CTL, pm_ctl, 4);
2288
2289 /* clear the PME_assert bit and power state bits, enable PME */
2290 pm_ctl = pci_conf_read(sc->bge_dev, BGE_PCI_PWRMGMT_CMD, 2);
2291 pm_ctl &= ~PCIM_PSTAT_DMASK;
2292 pm_ctl |= (1 << 8);
2293
2294 if (powerlevel == 0) {
2295 pm_ctl |= PCIM_PSTAT_D0;
2296 pci_write_config(sc->bge_dev, BGE_PCI_PWRMGMT_CMD,
2297 pm_ctl, 2);
2298 DELAY(10000);
2299 CSR_WRITE_4(sc, BGE_MISC_LOCAL_CTL, sc->bge_local_ctrl_reg);
2300 DELAY(10000);
2301
2302 #ifdef NOTYET
2303 /* XXX FIXME: write 0x02 to phy aux_Ctrl reg */
2304 bge_miibus_writereg(sc->bge_dev, 1, 0x18, 0x02);
2305 #endif
2306 DELAY(40); DELAY(40); DELAY(40);
2307 DELAY(10000); /* above not quite adequate on 5700 */
2308 return 0;
2309 }
2310
2311
2312 /*
2313 * Entering ACPI power states D1-D3 is achieved by wiggling
2314 * GMII gpio pins. Example code assumes all hardware vendors
2315 * followed Broadom's sample pcb layout. Until we verify that
2316 * for all supported OEM cards, states D1-D3 are unsupported.
2317 */
2318 aprint_error_dev(sc->bge_dev,
2319 "power state %d unimplemented; check GPIO pins\n",
2320 powerlevel);
2321 #endif
2322 return EOPNOTSUPP;
2323 }
2324
2325
2326 /*
2327 * Probe for a Broadcom chip. Check the PCI vendor and device IDs
2328 * against our list and return its name if we find a match. Note
2329 * that since the Broadcom controller contains VPD support, we
2330 * can get the device name string from the controller itself instead
2331 * of the compiled-in string. This is a little slow, but it guarantees
2332 * we'll always announce the right product name.
2333 */
2334 static int
2335 bge_probe(device_t parent, cfdata_t match, void *aux)
2336 {
2337 struct pci_attach_args *pa = (struct pci_attach_args *)aux;
2338
2339 if (bge_lookup(pa) != NULL)
2340 return 1;
2341
2342 return 0;
2343 }
2344
2345 static void
2346 bge_attach(device_t parent, device_t self, void *aux)
2347 {
2348 struct bge_softc *sc = device_private(self);
2349 struct pci_attach_args *pa = aux;
2350 prop_dictionary_t dict;
2351 const struct bge_product *bp;
2352 const struct bge_revision *br;
2353 pci_chipset_tag_t pc;
2354 pci_intr_handle_t ih;
2355 const char *intrstr = NULL;
2356 bus_dma_segment_t seg;
2357 int rseg;
2358 uint32_t hwcfg = 0;
2359 uint32_t command;
2360 struct ifnet *ifp;
2361 uint32_t misccfg;
2362 void * kva;
2363 u_char eaddr[ETHER_ADDR_LEN];
2364 pcireg_t memtype, subid;
2365 bus_addr_t memaddr;
2366 bus_size_t memsize;
2367 uint32_t pm_ctl;
2368
2369 bp = bge_lookup(pa);
2370 KASSERT(bp != NULL);
2371
2372 sc->sc_pc = pa->pa_pc;
2373 sc->sc_pcitag = pa->pa_tag;
2374 sc->bge_dev = self;
2375
2376 pc = sc->sc_pc;
2377 subid = pci_conf_read(pc, sc->sc_pcitag, PCI_SUBSYS_ID_REG);
2378
2379 aprint_naive(": Ethernet controller\n");
2380 aprint_normal(": %s\n", bp->bp_name);
2381
2382 /*
2383 * Map control/status registers.
2384 */
2385 DPRINTFN(5, ("Map control/status regs\n"));
2386 command = pci_conf_read(pc, sc->sc_pcitag, PCI_COMMAND_STATUS_REG);
2387 command |= PCI_COMMAND_MEM_ENABLE | PCI_COMMAND_MASTER_ENABLE;
2388 pci_conf_write(pc, sc->sc_pcitag, PCI_COMMAND_STATUS_REG, command);
2389 command = pci_conf_read(pc, sc->sc_pcitag, PCI_COMMAND_STATUS_REG);
2390
2391 if (!(command & PCI_COMMAND_MEM_ENABLE)) {
2392 aprint_error_dev(sc->bge_dev,
2393 "failed to enable memory mapping!\n");
2394 return;
2395 }
2396
2397 DPRINTFN(5, ("pci_mem_find\n"));
2398 memtype = pci_mapreg_type(sc->sc_pc, sc->sc_pcitag, BGE_PCI_BAR0);
2399 switch (memtype) {
2400 case PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT:
2401 case PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_64BIT:
2402 if (pci_mapreg_map(pa, BGE_PCI_BAR0,
2403 memtype, 0, &sc->bge_btag, &sc->bge_bhandle,
2404 &memaddr, &memsize) == 0)
2405 break;
2406 default:
2407 aprint_error_dev(sc->bge_dev, "can't find mem space\n");
2408 return;
2409 }
2410
2411 DPRINTFN(5, ("pci_intr_map\n"));
2412 if (pci_intr_map(pa, &ih)) {
2413 aprint_error_dev(sc->bge_dev, "couldn't map interrupt\n");
2414 return;
2415 }
2416
2417 DPRINTFN(5, ("pci_intr_string\n"));
2418 intrstr = pci_intr_string(pc, ih);
2419
2420 DPRINTFN(5, ("pci_intr_establish\n"));
2421 sc->bge_intrhand = pci_intr_establish(pc, ih, IPL_NET, bge_intr, sc);
2422
2423 if (sc->bge_intrhand == NULL) {
2424 aprint_error_dev(sc->bge_dev,
2425 "couldn't establish interrupt%s%s\n",
2426 intrstr ? " at " : "", intrstr ? intrstr : "");
2427 return;
2428 }
2429 aprint_normal_dev(sc->bge_dev, "interrupting at %s\n", intrstr);
2430
2431 /*
2432 * Kludge for 5700 Bx bug: a hardware bug (PCIX byte enable?)
2433 * can clobber the chip's PCI config-space power control registers,
2434 * leaving the card in D3 powersave state.
2435 * We do not have memory-mapped registers in this state,
2436 * so force device into D0 state before starting initialization.
2437 */
2438 pm_ctl = pci_conf_read(pc, sc->sc_pcitag, BGE_PCI_PWRMGMT_CMD);
2439 pm_ctl &= ~(PCI_PWR_D0|PCI_PWR_D1|PCI_PWR_D2|PCI_PWR_D3);
2440 pm_ctl |= (1 << 8) | PCI_PWR_D0 ; /* D0 state */
2441 pci_conf_write(pc, sc->sc_pcitag, BGE_PCI_PWRMGMT_CMD, pm_ctl);
2442 DELAY(1000); /* 27 usec is allegedly sufficent */
2443
2444 /*
2445 * Save ASIC rev.
2446 */
2447 sc->bge_chipid =
2448 pci_conf_read(pa->pa_pc, pa->pa_tag, BGE_PCI_MISC_CTL)
2449 >> BGE_PCIMISCCTL_ASICREV_SHIFT;
2450
2451 if (BGE_ASICREV(sc->bge_chipid) == BGE_ASICREV_USE_PRODID_REG) {
2452 if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_BROADCOM_BCM5717 ||
2453 PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_BROADCOM_BCM5718 ||
2454 PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_BROADCOM_BCM5724)
2455 sc->bge_chipid = pci_conf_read(pc, pa->pa_tag,
2456 BGE_PCI_GEN2_PRODID_ASICREV);
2457 else if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_BROADCOM_BCM57761 ||
2458 PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_BROADCOM_BCM57765 ||
2459 PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_BROADCOM_BCM57781 ||
2460 PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_BROADCOM_BCM57785 ||
2461 PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_BROADCOM_BCM57791 ||
2462 PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_BROADCOM_BCM57795)
2463 sc->bge_chipid = pci_conf_read(pc, pa->pa_tag,
2464 BGE_PCI_GEN15_PRODID_ASICREV);
2465 else
2466 sc->bge_chipid = pci_conf_read(pc, pa->pa_tag,
2467 BGE_PCI_PRODID_ASICREV);
2468 }
2469
2470 if (pci_get_capability(sc->sc_pc, sc->sc_pcitag, PCI_CAP_PCIEXPRESS,
2471 NULL, NULL) != 0) {
2472 /* PCIe */
2473 sc->bge_flags |= BGE_PCIE;
2474 } else if ((pci_conf_read(sc->sc_pc, sc->sc_pcitag, BGE_PCI_PCISTATE) &
2475 BGE_PCISTATE_PCI_BUSMODE) == 0) {
2476 /* PCI-X */
2477 sc->bge_flags |= BGE_PCIX;
2478 }
2479
2480 /* chipid */
2481 if (BGE_ASICREV(sc->bge_chipid) == BGE_ASICREV_BCM5700 ||
2482 BGE_ASICREV(sc->bge_chipid) == BGE_ASICREV_BCM5701 ||
2483 BGE_ASICREV(sc->bge_chipid) == BGE_ASICREV_BCM5703 ||
2484 BGE_ASICREV(sc->bge_chipid) == BGE_ASICREV_BCM5704)
2485 sc->bge_flags |= BGE_5700_FAMILY;
2486
2487 if (BGE_ASICREV(sc->bge_chipid) == BGE_ASICREV_BCM5714_A0 ||
2488 BGE_ASICREV(sc->bge_chipid) == BGE_ASICREV_BCM5780 ||
2489 BGE_ASICREV(sc->bge_chipid) == BGE_ASICREV_BCM5714)
2490 sc->bge_flags |= BGE_5714_FAMILY;
2491
2492 /* Intentionally exclude BGE_ASICREV_BCM5906 */
2493 if (BGE_ASICREV(sc->bge_chipid) == BGE_ASICREV_BCM5717 ||
2494 BGE_ASICREV(sc->bge_chipid) == BGE_ASICREV_BCM5755 ||
2495 BGE_ASICREV(sc->bge_chipid) == BGE_ASICREV_BCM5761 ||
2496 BGE_ASICREV(sc->bge_chipid) == BGE_ASICREV_BCM5784 ||
2497 BGE_ASICREV(sc->bge_chipid) == BGE_ASICREV_BCM5785 ||
2498 BGE_ASICREV(sc->bge_chipid) == BGE_ASICREV_BCM5787 ||
2499 BGE_ASICREV(sc->bge_chipid) == BGE_ASICREV_BCM57765 ||
2500 BGE_ASICREV(sc->bge_chipid) == BGE_ASICREV_BCM57780)
2501 sc->bge_flags |= BGE_5755_PLUS;
2502
2503 if (BGE_ASICREV(sc->bge_chipid) == BGE_ASICREV_BCM5750 ||
2504 BGE_ASICREV(sc->bge_chipid) == BGE_ASICREV_BCM5752 ||
2505 BGE_ASICREV(sc->bge_chipid) == BGE_ASICREV_BCM5906 ||
2506 BGE_IS_5755_PLUS(sc) ||
2507 BGE_IS_5714_FAMILY(sc))
2508 sc->bge_flags |= BGE_5750_PLUS;
2509
2510 if (BGE_ASICREV(sc->bge_chipid) == BGE_ASICREV_BCM5705 ||
2511 BGE_IS_5750_OR_BEYOND(sc))
2512 sc->bge_flags |= BGE_5705_PLUS;
2513
2514 /*
2515 * When using the BCM5701 in PCI-X mode, data corruption has
2516 * been observed in the first few bytes of some received packets.
2517 * Aligning the packet buffer in memory eliminates the corruption.
2518 * Unfortunately, this misaligns the packet payloads. On platforms
2519 * which do not support unaligned accesses, we will realign the
2520 * payloads by copying the received packets.
2521 */
2522 if (BGE_ASICREV(sc->bge_chipid) == BGE_ASICREV_BCM5701 &&
2523 sc->bge_flags & BGE_PCIX)
2524 sc->bge_flags |= BGE_RX_ALIGNBUG;
2525
2526 if (BGE_IS_5700_FAMILY(sc))
2527 sc->bge_flags |= BGE_JUMBO_CAPABLE;
2528
2529 if ((BGE_ASICREV(sc->bge_chipid) == BGE_ASICREV_BCM5700 ||
2530 BGE_ASICREV(sc->bge_chipid) == BGE_ASICREV_BCM5701) &&
2531 PCI_VENDOR(subid) == PCI_VENDOR_DELL)
2532 sc->bge_flags |= BGE_NO_3LED;
2533
2534 misccfg = CSR_READ_4(sc, BGE_MISC_CFG);
2535 misccfg &= BGE_MISCCFG_BOARD_ID_MASK;
2536
2537 if (BGE_ASICREV(sc->bge_chipid) == BGE_ASICREV_BCM5705 &&
2538 (misccfg == BGE_MISCCFG_BOARD_ID_5788 ||
2539 misccfg == BGE_MISCCFG_BOARD_ID_5788M))
2540 sc->bge_flags |= BGE_IS_5788;
2541
2542 /*
2543 * Some controllers seem to require a special firmware to use
2544 * TSO. But the firmware is not available to FreeBSD and Linux
2545 * claims that the TSO performed by the firmware is slower than
2546 * hardware based TSO. Moreover the firmware based TSO has one
2547 * known bug which can't handle TSO if ethernet header + IP/TCP
2548 * header is greater than 80 bytes. The workaround for the TSO
2549 * bug exist but it seems it's too expensive than not using
2550 * TSO at all. Some hardwares also have the TSO bug so limit
2551 * the TSO to the controllers that are not affected TSO issues
2552 * (e.g. 5755 or higher).
2553 */
2554 if (BGE_IS_5755_PLUS(sc)) {
2555 /*
2556 * BCM5754 and BCM5787 shares the same ASIC id so
2557 * explicit device id check is required.
2558 */
2559 if ((PCI_PRODUCT(pa->pa_id) != PCI_PRODUCT_BROADCOM_BCM5754) &&
2560 (PCI_PRODUCT(pa->pa_id) != PCI_PRODUCT_BROADCOM_BCM5754M))
2561 sc->bge_flags |= BGE_TSO;
2562 }
2563
2564 if ((BGE_ASICREV(sc->bge_chipid) == BGE_ASICREV_BCM5703 &&
2565 (misccfg == 0x4000 || misccfg == 0x8000)) ||
2566 (BGE_ASICREV(sc->bge_chipid) == BGE_ASICREV_BCM5705 &&
2567 PCI_VENDOR(pa->pa_id) == PCI_VENDOR_BROADCOM &&
2568 (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_BROADCOM_BCM5901 ||
2569 PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_BROADCOM_BCM5901A2 ||
2570 PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_BROADCOM_BCM5705F)) ||
2571 (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_BROADCOM &&
2572 (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_BROADCOM_BCM5751F ||
2573 PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_BROADCOM_BCM5753F ||
2574 PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_BROADCOM_BCM5787F)) ||
2575 PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_BROADCOM_BCM57790 ||
2576 BGE_ASICREV(sc->bge_chipid) == BGE_ASICREV_BCM5906)
2577 sc->bge_flags |= BGE_10_100_ONLY;
2578
2579 if (BGE_ASICREV(sc->bge_chipid) == BGE_ASICREV_BCM5700 ||
2580 (BGE_ASICREV(sc->bge_chipid) == BGE_ASICREV_BCM5705 &&
2581 (sc->bge_chipid != BGE_CHIPID_BCM5705_A0 &&
2582 sc->bge_chipid != BGE_CHIPID_BCM5705_A1)) ||
2583 BGE_ASICREV(sc->bge_chipid) == BGE_ASICREV_BCM5906)
2584 sc->bge_flags |= BGE_NO_ETH_WIRE_SPEED;
2585
2586 if (sc->bge_chipid == BGE_CHIPID_BCM5701_A0 ||
2587 sc->bge_chipid == BGE_CHIPID_BCM5701_B0)
2588 sc->bge_flags |= BGE_PHY_CRC_BUG;
2589 if (BGE_CHIPREV(sc->bge_chipid) == BGE_CHIPREV_5703_AX ||
2590 BGE_CHIPREV(sc->bge_chipid) == BGE_CHIPREV_5704_AX)
2591 sc->bge_flags |= BGE_PHY_ADC_BUG;
2592 if (sc->bge_chipid == BGE_CHIPID_BCM5704_A0)
2593 sc->bge_flags |= BGE_PHY_5704_A0_BUG;
2594
2595 if (BGE_IS_5705_PLUS(sc) &&
2596 BGE_ASICREV(sc->bge_chipid) != BGE_ASICREV_BCM5906 &&
2597 BGE_ASICREV(sc->bge_chipid) != BGE_ASICREV_BCM5717 &&
2598 BGE_ASICREV(sc->bge_chipid) != BGE_ASICREV_BCM5785 &&
2599 BGE_ASICREV(sc->bge_chipid) != BGE_ASICREV_BCM57765 &&
2600 BGE_ASICREV(sc->bge_chipid) != BGE_ASICREV_BCM57780) {
2601 if (BGE_ASICREV(sc->bge_chipid) == BGE_ASICREV_BCM5755 ||
2602 BGE_ASICREV(sc->bge_chipid) == BGE_ASICREV_BCM5761 ||
2603 BGE_ASICREV(sc->bge_chipid) == BGE_ASICREV_BCM5784 ||
2604 BGE_ASICREV(sc->bge_chipid) == BGE_ASICREV_BCM5787) {
2605 if (PCI_PRODUCT(pa->pa_id) != PCI_PRODUCT_BROADCOM_BCM5722 &&
2606 PCI_PRODUCT(pa->pa_id) != PCI_PRODUCT_BROADCOM_BCM5756)
2607 sc->bge_flags |= BGE_PHY_JITTER_BUG;
2608 if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_BROADCOM_BCM5755M)
2609 sc->bge_flags |= BGE_PHY_ADJUST_TRIM;
2610 } else if (BGE_ASICREV(sc->bge_chipid) != BGE_ASICREV_BCM5906)
2611 sc->bge_flags |= BGE_PHY_BER_BUG;
2612 }
2613
2614 /* SEEPROM check. Check the 'ROM failed' bit on the RX CPU */
2615 if (CSR_READ_4(sc, BGE_RXCPU_MODE) & BGE_RXCPUMODE_ROMFAIL)
2616 sc->bge_flags |= BGE_NO_EEPROM;
2617
2618 /* Try to reset the chip. */
2619 DPRINTFN(5, ("bge_reset\n"));
2620 bge_reset(sc);
2621
2622 if (bge_chipinit(sc)) {
2623 aprint_error_dev(sc->bge_dev, "chip initialization failed\n");
2624 bge_release_resources(sc);
2625 return;
2626 }
2627
2628 /*
2629 * Get station address from the EEPROM.
2630 */
2631 if (bge_get_eaddr(sc, eaddr)) {
2632 aprint_error_dev(sc->bge_dev,
2633 "failed to read station address\n");
2634 bge_release_resources(sc);
2635 return;
2636 }
2637
2638 br = bge_lookup_rev(sc->bge_chipid);
2639
2640 if (br == NULL) {
2641 aprint_normal_dev(sc->bge_dev, "unknown ASIC (0x%x)",
2642 sc->bge_chipid);
2643 } else {
2644 aprint_normal_dev(sc->bge_dev, "ASIC %s (0x%x)",
2645 br->br_name, sc->bge_chipid);
2646 }
2647 aprint_normal(", Ethernet address %s\n", ether_sprintf(eaddr));
2648
2649 /* Allocate the general information block and ring buffers. */
2650 if (pci_dma64_available(pa))
2651 sc->bge_dmatag = pa->pa_dmat64;
2652 else
2653 sc->bge_dmatag = pa->pa_dmat;
2654 DPRINTFN(5, ("bus_dmamem_alloc\n"));
2655 if (bus_dmamem_alloc(sc->bge_dmatag, sizeof(struct bge_ring_data),
2656 PAGE_SIZE, 0, &seg, 1, &rseg, BUS_DMA_NOWAIT)) {
2657 aprint_error_dev(sc->bge_dev, "can't alloc rx buffers\n");
2658 return;
2659 }
2660 DPRINTFN(5, ("bus_dmamem_map\n"));
2661 if (bus_dmamem_map(sc->bge_dmatag, &seg, rseg,
2662 sizeof(struct bge_ring_data), &kva,
2663 BUS_DMA_NOWAIT)) {
2664 aprint_error_dev(sc->bge_dev,
2665 "can't map DMA buffers (%zu bytes)\n",
2666 sizeof(struct bge_ring_data));
2667 bus_dmamem_free(sc->bge_dmatag, &seg, rseg);
2668 return;
2669 }
2670 DPRINTFN(5, ("bus_dmamem_create\n"));
2671 if (bus_dmamap_create(sc->bge_dmatag, sizeof(struct bge_ring_data), 1,
2672 sizeof(struct bge_ring_data), 0,
2673 BUS_DMA_NOWAIT, &sc->bge_ring_map)) {
2674 aprint_error_dev(sc->bge_dev, "can't create DMA map\n");
2675 bus_dmamem_unmap(sc->bge_dmatag, kva,
2676 sizeof(struct bge_ring_data));
2677 bus_dmamem_free(sc->bge_dmatag, &seg, rseg);
2678 return;
2679 }
2680 DPRINTFN(5, ("bus_dmamem_load\n"));
2681 if (bus_dmamap_load(sc->bge_dmatag, sc->bge_ring_map, kva,
2682 sizeof(struct bge_ring_data), NULL,
2683 BUS_DMA_NOWAIT)) {
2684 bus_dmamap_destroy(sc->bge_dmatag, sc->bge_ring_map);
2685 bus_dmamem_unmap(sc->bge_dmatag, kva,
2686 sizeof(struct bge_ring_data));
2687 bus_dmamem_free(sc->bge_dmatag, &seg, rseg);
2688 return;
2689 }
2690
2691 DPRINTFN(5, ("bzero\n"));
2692 sc->bge_rdata = (struct bge_ring_data *)kva;
2693
2694 memset(sc->bge_rdata, 0, sizeof(struct bge_ring_data));
2695
2696 /* Try to allocate memory for jumbo buffers. */
2697 if (BGE_IS_JUMBO_CAPABLE(sc)) {
2698 if (bge_alloc_jumbo_mem(sc)) {
2699 aprint_error_dev(sc->bge_dev,
2700 "jumbo buffer allocation failed\n");
2701 } else
2702 sc->ethercom.ec_capabilities |= ETHERCAP_JUMBO_MTU;
2703 }
2704
2705 /* Set default tuneable values. */
2706 sc->bge_stat_ticks = BGE_TICKS_PER_SEC;
2707 sc->bge_rx_coal_ticks = 150;
2708 sc->bge_rx_max_coal_bds = 64;
2709 #ifdef ORIG_WPAUL_VALUES
2710 sc->bge_tx_coal_ticks = 150;
2711 sc->bge_tx_max_coal_bds = 128;
2712 #else
2713 sc->bge_tx_coal_ticks = 300;
2714 sc->bge_tx_max_coal_bds = 400;
2715 #endif
2716 if (BGE_IS_5705_PLUS(sc)) {
2717 sc->bge_tx_coal_ticks = (12 * 5);
2718 sc->bge_tx_max_coal_bds = (12 * 5);
2719 aprint_verbose_dev(sc->bge_dev,
2720 "setting short Tx thresholds\n");
2721 }
2722
2723 if (BGE_IS_5705_PLUS(sc))
2724 sc->bge_return_ring_cnt = BGE_RETURN_RING_CNT_5705;
2725 else
2726 sc->bge_return_ring_cnt = BGE_RETURN_RING_CNT;
2727
2728 /* Set up ifnet structure */
2729 ifp = &sc->ethercom.ec_if;
2730 ifp->if_softc = sc;
2731 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
2732 ifp->if_ioctl = bge_ioctl;
2733 ifp->if_stop = bge_stop;
2734 ifp->if_start = bge_start;
2735 ifp->if_init = bge_init;
2736 ifp->if_watchdog = bge_watchdog;
2737 IFQ_SET_MAXLEN(&ifp->if_snd, max(BGE_TX_RING_CNT - 1, IFQ_MAXLEN));
2738 IFQ_SET_READY(&ifp->if_snd);
2739 DPRINTFN(5, ("strcpy if_xname\n"));
2740 strcpy(ifp->if_xname, device_xname(sc->bge_dev));
2741
2742 if (sc->bge_chipid != BGE_CHIPID_BCM5700_B0)
2743 sc->ethercom.ec_if.if_capabilities |=
2744 IFCAP_CSUM_IPv4_Tx | IFCAP_CSUM_IPv4_Rx;
2745 #if 1 /* XXX TCP/UDP checksum offload breaks with pf(4) */
2746 sc->ethercom.ec_if.if_capabilities |=
2747 IFCAP_CSUM_TCPv4_Tx | IFCAP_CSUM_TCPv4_Rx |
2748 IFCAP_CSUM_UDPv4_Tx | IFCAP_CSUM_UDPv4_Rx;
2749 #endif
2750 sc->ethercom.ec_capabilities |=
2751 ETHERCAP_VLAN_HWTAGGING | ETHERCAP_VLAN_MTU;
2752
2753 if (sc->bge_flags & BGE_TSO)
2754 sc->ethercom.ec_if.if_capabilities |= IFCAP_TSOv4;
2755
2756 /*
2757 * Do MII setup.
2758 */
2759 DPRINTFN(5, ("mii setup\n"));
2760 sc->bge_mii.mii_ifp = ifp;
2761 sc->bge_mii.mii_readreg = bge_miibus_readreg;
2762 sc->bge_mii.mii_writereg = bge_miibus_writereg;
2763 sc->bge_mii.mii_statchg = bge_miibus_statchg;
2764
2765 /*
2766 * Figure out what sort of media we have by checking the
2767 * hardware config word in the first 32k of NIC internal memory,
2768 * or fall back to the config word in the EEPROM. Note: on some BCM5700
2769 * cards, this value appears to be unset. If that's the
2770 * case, we have to rely on identifying the NIC by its PCI
2771 * subsystem ID, as we do below for the SysKonnect SK-9D41.
2772 */
2773 if (bge_readmem_ind(sc, BGE_SOFTWARE_GENCOMM_SIG) == BGE_MAGIC_NUMBER) {
2774 hwcfg = bge_readmem_ind(sc, BGE_SOFTWARE_GENCOMM_NICCFG);
2775 } else {
2776 bge_read_eeprom(sc, (void *)&hwcfg,
2777 BGE_EE_HWCFG_OFFSET, sizeof(hwcfg));
2778 hwcfg = be32toh(hwcfg);
2779 }
2780 /* The SysKonnect SK-9D41 is a 1000baseSX card. */
2781 if (PCI_PRODUCT(pa->pa_id) == SK_SUBSYSID_9D41 ||
2782 (hwcfg & BGE_HWCFG_MEDIA) == BGE_MEDIA_FIBER) {
2783 if (BGE_IS_5714_FAMILY(sc))
2784 sc->bge_flags |= BGE_PHY_FIBER_MII;
2785 else
2786 sc->bge_flags |= BGE_PHY_FIBER_TBI;
2787 }
2788
2789 /* set phyflags before mii_attach() */
2790 dict = device_properties(self);
2791 prop_dictionary_set_uint32(dict, "phyflags", sc->bge_flags);
2792
2793 if (sc->bge_flags & BGE_PHY_FIBER_TBI) {
2794 ifmedia_init(&sc->bge_ifmedia, IFM_IMASK, bge_ifmedia_upd,
2795 bge_ifmedia_sts);
2796 ifmedia_add(&sc->bge_ifmedia, IFM_ETHER|IFM_1000_SX, 0, NULL);
2797 ifmedia_add(&sc->bge_ifmedia, IFM_ETHER|IFM_1000_SX|IFM_FDX,
2798 0, NULL);
2799 ifmedia_add(&sc->bge_ifmedia, IFM_ETHER|IFM_AUTO, 0, NULL);
2800 ifmedia_set(&sc->bge_ifmedia, IFM_ETHER|IFM_AUTO);
2801 /* Pretend the user requested this setting */
2802 sc->bge_ifmedia.ifm_media = sc->bge_ifmedia.ifm_cur->ifm_media;
2803 } else {
2804 /*
2805 * Do transceiver setup.
2806 */
2807 ifmedia_init(&sc->bge_mii.mii_media, 0, bge_ifmedia_upd,
2808 bge_ifmedia_sts);
2809 mii_attach(sc->bge_dev, &sc->bge_mii, 0xffffffff,
2810 MII_PHY_ANY, MII_OFFSET_ANY,
2811 MIIF_FORCEANEG|MIIF_DOPAUSE);
2812
2813 if (LIST_EMPTY(&sc->bge_mii.mii_phys)) {
2814 aprint_error_dev(sc->bge_dev, "no PHY found!\n");
2815 ifmedia_add(&sc->bge_mii.mii_media,
2816 IFM_ETHER|IFM_MANUAL, 0, NULL);
2817 ifmedia_set(&sc->bge_mii.mii_media,
2818 IFM_ETHER|IFM_MANUAL);
2819 } else
2820 ifmedia_set(&sc->bge_mii.mii_media,
2821 IFM_ETHER|IFM_AUTO);
2822 }
2823
2824 /*
2825 * Call MI attach routine.
2826 */
2827 DPRINTFN(5, ("if_attach\n"));
2828 if_attach(ifp);
2829 DPRINTFN(5, ("ether_ifattach\n"));
2830 ether_ifattach(ifp, eaddr);
2831 #if NRND > 0
2832 rnd_attach_source(&sc->rnd_source, device_xname(sc->bge_dev),
2833 RND_TYPE_NET, 0);
2834 #endif
2835 #ifdef BGE_EVENT_COUNTERS
2836 /*
2837 * Attach event counters.
2838 */
2839 evcnt_attach_dynamic(&sc->bge_ev_intr, EVCNT_TYPE_INTR,
2840 NULL, device_xname(sc->bge_dev), "intr");
2841 evcnt_attach_dynamic(&sc->bge_ev_tx_xoff, EVCNT_TYPE_MISC,
2842 NULL, device_xname(sc->bge_dev), "tx_xoff");
2843 evcnt_attach_dynamic(&sc->bge_ev_tx_xon, EVCNT_TYPE_MISC,
2844 NULL, device_xname(sc->bge_dev), "tx_xon");
2845 evcnt_attach_dynamic(&sc->bge_ev_rx_xoff, EVCNT_TYPE_MISC,
2846 NULL, device_xname(sc->bge_dev), "rx_xoff");
2847 evcnt_attach_dynamic(&sc->bge_ev_rx_xon, EVCNT_TYPE_MISC,
2848 NULL, device_xname(sc->bge_dev), "rx_xon");
2849 evcnt_attach_dynamic(&sc->bge_ev_rx_macctl, EVCNT_TYPE_MISC,
2850 NULL, device_xname(sc->bge_dev), "rx_macctl");
2851 evcnt_attach_dynamic(&sc->bge_ev_xoffentered, EVCNT_TYPE_MISC,
2852 NULL, device_xname(sc->bge_dev), "xoffentered");
2853 #endif /* BGE_EVENT_COUNTERS */
2854 DPRINTFN(5, ("callout_init\n"));
2855 callout_init(&sc->bge_timeout, 0);
2856
2857 if (pmf_device_register(self, NULL, NULL))
2858 pmf_class_network_register(self, ifp);
2859 else
2860 aprint_error_dev(self, "couldn't establish power handler\n");
2861
2862 #ifdef BGE_DEBUG
2863 bge_debug_info(sc);
2864 #endif
2865 }
2866
2867 static void
2868 bge_release_resources(struct bge_softc *sc)
2869 {
2870 if (sc->bge_vpd_prodname != NULL)
2871 free(sc->bge_vpd_prodname, M_DEVBUF);
2872
2873 if (sc->bge_vpd_readonly != NULL)
2874 free(sc->bge_vpd_readonly, M_DEVBUF);
2875 }
2876
2877 static void
2878 bge_reset(struct bge_softc *sc)
2879 {
2880 uint32_t cachesize, command, pcistate, new_pcistate;
2881 int i, val;
2882 void (*write_op)(struct bge_softc *, int, int);
2883
2884 if (BGE_IS_5750_OR_BEYOND(sc) && !BGE_IS_5714_FAMILY(sc) &&
2885 (BGE_ASICREV(sc->bge_chipid) != BGE_ASICREV_BCM5906)) {
2886 if (sc->bge_flags & BGE_PCIE) {
2887 write_op = bge_writemem_direct;
2888 } else {
2889 write_op = bge_writemem_ind;
2890 }
2891 } else {
2892 write_op = bge_writereg_ind;
2893 }
2894
2895
2896 /* Save some important PCI state. */
2897 cachesize = pci_conf_read(sc->sc_pc, sc->sc_pcitag, BGE_PCI_CACHESZ);
2898 command = pci_conf_read(sc->sc_pc, sc->sc_pcitag, BGE_PCI_CMD);
2899 pcistate = pci_conf_read(sc->sc_pc, sc->sc_pcitag, BGE_PCI_PCISTATE);
2900
2901 pci_conf_write(sc->sc_pc, sc->sc_pcitag, BGE_PCI_MISC_CTL,
2902 BGE_PCIMISCCTL_INDIRECT_ACCESS | BGE_PCIMISCCTL_MASK_PCI_INTR |
2903 BGE_HIF_SWAP_OPTIONS | BGE_PCIMISCCTL_PCISTATE_RW);
2904
2905 /* Disable fastboot on controllers that support it. */
2906 if (BGE_ASICREV(sc->bge_chipid) == BGE_ASICREV_BCM5752 ||
2907 BGE_IS_5755_PLUS(sc))
2908 CSR_WRITE_4(sc, BGE_FASTBOOT_PC, 0);
2909
2910 val = BGE_MISCCFG_RESET_CORE_CLOCKS | (65<<1);
2911 /*
2912 * XXX: from FreeBSD/Linux; no documentation
2913 */
2914 if (sc->bge_flags & BGE_PCIE) {
2915 if (CSR_READ_4(sc, BGE_PCIE_CTL1) == 0x60)
2916 /* PCI Express 1.0 system */
2917 CSR_WRITE_4(sc, BGE_PCIE_CTL1, 0x20);
2918 if (sc->bge_chipid != BGE_CHIPID_BCM5750_A0) {
2919 /*
2920 * Prevent PCI Express link training
2921 * during global reset.
2922 */
2923 CSR_WRITE_4(sc, BGE_MISC_CFG, 1 << 29);
2924 val |= (1<<29);
2925 }
2926 }
2927
2928 /*
2929 * Set GPHY Power Down Override to leave GPHY
2930 * powered up in D0 uninitialized.
2931 */
2932 if (BGE_IS_5705_PLUS(sc))
2933 val |= BGE_MISCCFG_KEEP_GPHY_POWER;
2934
2935 /* Issue global reset */
2936 write_op(sc, BGE_MISC_CFG, val);
2937
2938 if (BGE_ASICREV(sc->bge_chipid) == BGE_ASICREV_BCM5906) {
2939 i = CSR_READ_4(sc, BGE_VCPU_STATUS);
2940 CSR_WRITE_4(sc, BGE_VCPU_STATUS,
2941 i | BGE_VCPU_STATUS_DRV_RESET);
2942 i = CSR_READ_4(sc, BGE_VCPU_EXT_CTRL);
2943 CSR_WRITE_4(sc, BGE_VCPU_EXT_CTRL,
2944 i & ~BGE_VCPU_EXT_CTRL_HALT_CPU);
2945 }
2946
2947 DELAY(1000);
2948
2949 /*
2950 * XXX: from FreeBSD/Linux; no documentation
2951 */
2952 if (sc->bge_flags & BGE_PCIE) {
2953 if (sc->bge_chipid == BGE_CHIPID_BCM5750_A0) {
2954 pcireg_t reg;
2955
2956 DELAY(500000);
2957 /* XXX: Magic Numbers */
2958 reg = pci_conf_read(sc->sc_pc, sc->sc_pcitag,
2959 BGE_PCI_UNKNOWN0);
2960 pci_conf_write(sc->sc_pc, sc->sc_pcitag,
2961 BGE_PCI_UNKNOWN0,
2962 reg | (1 << 15));
2963 }
2964 /*
2965 * XXX: Magic Numbers.
2966 * Sets maximal PCI-e payload and clears any PCI-e errors.
2967 * Should be replaced with references to PCI config-space
2968 * capability block for PCI-Express.
2969 */
2970 pci_conf_write(sc->sc_pc, sc->sc_pcitag,
2971 BGE_PCI_CONF_DEV_CTRL, 0xf5000);
2972
2973 }
2974
2975 /* Reset some of the PCI state that got zapped by reset */
2976 pci_conf_write(sc->sc_pc, sc->sc_pcitag, BGE_PCI_MISC_CTL,
2977 BGE_PCIMISCCTL_INDIRECT_ACCESS | BGE_PCIMISCCTL_MASK_PCI_INTR |
2978 BGE_HIF_SWAP_OPTIONS | BGE_PCIMISCCTL_PCISTATE_RW);
2979 pci_conf_write(sc->sc_pc, sc->sc_pcitag, BGE_PCI_CACHESZ, cachesize);
2980 pci_conf_write(sc->sc_pc, sc->sc_pcitag, BGE_PCI_CMD, command);
2981 write_op(sc, BGE_MISC_CFG, (65 << 1));
2982
2983 /* Enable memory arbiter. */
2984 {
2985 uint32_t marbmode = 0;
2986 if (BGE_IS_5714_FAMILY(sc)) {
2987 marbmode = CSR_READ_4(sc, BGE_MARB_MODE);
2988 }
2989 CSR_WRITE_4(sc, BGE_MARB_MODE, BGE_MARBMODE_ENABLE | marbmode);
2990 }
2991
2992 /*
2993 * Prevent PXE restart: write a magic number to the
2994 * general communications memory at 0xB50.
2995 */
2996 bge_writemem_ind(sc, BGE_SOFTWARE_GENCOMM, BGE_MAGIC_NUMBER);
2997
2998 if (BGE_ASICREV(sc->bge_chipid) == BGE_ASICREV_BCM5906) {
2999 for (i = 0; i < BGE_TIMEOUT; i++) {
3000 val = CSR_READ_4(sc, BGE_VCPU_STATUS);
3001 if (val & BGE_VCPU_STATUS_INIT_DONE)
3002 break;
3003 DELAY(100);
3004 }
3005 if (i == BGE_TIMEOUT) {
3006 aprint_error_dev(sc->bge_dev, "reset timed out\n");
3007 return;
3008 }
3009 } else {
3010 /*
3011 * Poll the value location we just wrote until
3012 * we see the 1's complement of the magic number.
3013 * This indicates that the firmware initialization
3014 * is complete.
3015 */
3016 for (i = 0; i < BGE_TIMEOUT; i++) {
3017 val = bge_readmem_ind(sc, BGE_SOFTWARE_GENCOMM);
3018 if (val == ~BGE_MAGIC_NUMBER)
3019 break;
3020 DELAY(10);
3021 }
3022
3023 if (i >= BGE_TIMEOUT && (!(sc->bge_flags & BGE_NO_EEPROM))) {
3024 aprint_error_dev(sc->bge_dev,
3025 "firmware handshake timed out, val = %x\n", val);
3026 /*
3027 * XXX: occasionally fired on bcm5721, but without
3028 * apparent harm. For now, keep going if we timeout
3029 * against PCI-E devices.
3030 */
3031 if ((sc->bge_flags & BGE_PCIE) == 0)
3032 return;
3033 }
3034 }
3035
3036 /*
3037 * XXX Wait for the value of the PCISTATE register to
3038 * return to its original pre-reset state. This is a
3039 * fairly good indicator of reset completion. If we don't
3040 * wait for the reset to fully complete, trying to read
3041 * from the device's non-PCI registers may yield garbage
3042 * results.
3043 */
3044 for (i = 0; i < BGE_TIMEOUT; i++) {
3045 new_pcistate = pci_conf_read(sc->sc_pc, sc->sc_pcitag,
3046 BGE_PCI_PCISTATE);
3047 if ((new_pcistate & ~BGE_PCISTATE_RESERVED) ==
3048 (pcistate & ~BGE_PCISTATE_RESERVED))
3049 break;
3050 DELAY(10);
3051 }
3052 if ((new_pcistate & ~BGE_PCISTATE_RESERVED) !=
3053 (pcistate & ~BGE_PCISTATE_RESERVED)) {
3054 aprint_error_dev(sc->bge_dev, "pcistate failed to revert\n");
3055 }
3056
3057 #if 0
3058 /* Enable memory arbiter. */
3059 /* XXX why do this twice? */
3060 {
3061 uint32_t marbmode = 0;
3062 if (BGE_IS_5714_FAMILY(sc)) {
3063 marbmode = CSR_READ_4(sc, BGE_MARB_MODE);
3064 }
3065 CSR_WRITE_4(sc, BGE_MARB_MODE, BGE_MARBMODE_ENABLE | marbmode);
3066 }
3067 #endif
3068
3069 /* Fix up byte swapping */
3070 CSR_WRITE_4(sc, BGE_MODE_CTL, BGE_DMA_SWAP_OPTIONS);
3071
3072 CSR_WRITE_4(sc, BGE_MAC_MODE, 0);
3073
3074 /*
3075 * The 5704 in TBI mode apparently needs some special
3076 * adjustment to insure the SERDES drive level is set
3077 * to 1.2V.
3078 */
3079 if (sc->bge_flags & BGE_PHY_FIBER_TBI &&
3080 BGE_ASICREV(sc->bge_chipid) == BGE_ASICREV_BCM5704) {
3081 uint32_t serdescfg;
3082
3083 serdescfg = CSR_READ_4(sc, BGE_SERDES_CFG);
3084 serdescfg = (serdescfg & ~0xFFF) | 0x880;
3085 CSR_WRITE_4(sc, BGE_SERDES_CFG, serdescfg);
3086 }
3087
3088 if (sc->bge_flags & BGE_PCIE &&
3089 sc->bge_chipid != BGE_CHIPID_BCM5750_A0 &&
3090 BGE_ASICREV(sc->bge_chipid) != BGE_ASICREV_BCM5717 &&
3091 BGE_ASICREV(sc->bge_chipid) != BGE_ASICREV_BCM5785 &&
3092 BGE_ASICREV(sc->bge_chipid) != BGE_ASICREV_BCM57765) {
3093 uint32_t v;
3094
3095 /* Enable PCI Express bug fix */
3096 v = CSR_READ_4(sc, 0x7c00);
3097 CSR_WRITE_4(sc, 0x7c00, v | (1<<25));
3098 }
3099 DELAY(10000);
3100 }
3101
3102 /*
3103 * Frame reception handling. This is called if there's a frame
3104 * on the receive return list.
3105 *
3106 * Note: we have to be able to handle two possibilities here:
3107 * 1) the frame is from the jumbo recieve ring
3108 * 2) the frame is from the standard receive ring
3109 */
3110
3111 static void
3112 bge_rxeof(struct bge_softc *sc)
3113 {
3114 struct ifnet *ifp;
3115 uint16_t rx_prod, rx_cons;
3116 int stdcnt = 0, jumbocnt = 0;
3117 bus_dmamap_t dmamap;
3118 bus_addr_t offset, toff;
3119 bus_size_t tlen;
3120 int tosync;
3121
3122 rx_cons = sc->bge_rx_saved_considx;
3123 rx_prod = sc->bge_rdata->bge_status_block.bge_idx[0].bge_rx_prod_idx;
3124
3125 /* Nothing to do */
3126 if (rx_cons == rx_prod)
3127 return;
3128
3129 ifp = &sc->ethercom.ec_if;
3130
3131 bus_dmamap_sync(sc->bge_dmatag, sc->bge_ring_map,
3132 offsetof(struct bge_ring_data, bge_status_block),
3133 sizeof (struct bge_status_block),
3134 BUS_DMASYNC_POSTREAD);
3135
3136 offset = offsetof(struct bge_ring_data, bge_rx_return_ring);
3137 tosync = rx_prod - rx_cons;
3138
3139 #if NRND > 0
3140 if (tosync != 0 && RND_ENABLED(&sc->rnd_source))
3141 rnd_add_uint32(&sc->rnd_source, tosync);
3142 #endif
3143
3144 toff = offset + (rx_cons * sizeof (struct bge_rx_bd));
3145
3146 if (tosync < 0) {
3147 tlen = (sc->bge_return_ring_cnt - rx_cons) *
3148 sizeof (struct bge_rx_bd);
3149 bus_dmamap_sync(sc->bge_dmatag, sc->bge_ring_map,
3150 toff, tlen, BUS_DMASYNC_POSTREAD);
3151 tosync = -tosync;
3152 }
3153
3154 bus_dmamap_sync(sc->bge_dmatag, sc->bge_ring_map,
3155 offset, tosync * sizeof (struct bge_rx_bd),
3156 BUS_DMASYNC_POSTREAD);
3157
3158 while (rx_cons != rx_prod) {
3159 struct bge_rx_bd *cur_rx;
3160 uint32_t rxidx;
3161 struct mbuf *m = NULL;
3162
3163 cur_rx = &sc->bge_rdata->bge_rx_return_ring[rx_cons];
3164
3165 rxidx = cur_rx->bge_idx;
3166 BGE_INC(rx_cons, sc->bge_return_ring_cnt);
3167
3168 if (cur_rx->bge_flags & BGE_RXBDFLAG_JUMBO_RING) {
3169 BGE_INC(sc->bge_jumbo, BGE_JUMBO_RX_RING_CNT);
3170 m = sc->bge_cdata.bge_rx_jumbo_chain[rxidx];
3171 sc->bge_cdata.bge_rx_jumbo_chain[rxidx] = NULL;
3172 jumbocnt++;
3173 bus_dmamap_sync(sc->bge_dmatag,
3174 sc->bge_cdata.bge_rx_jumbo_map,
3175 mtod(m, char *) - (char *)sc->bge_cdata.bge_jumbo_buf,
3176 BGE_JLEN, BUS_DMASYNC_POSTREAD);
3177 if (cur_rx->bge_flags & BGE_RXBDFLAG_ERROR) {
3178 ifp->if_ierrors++;
3179 bge_newbuf_jumbo(sc, sc->bge_jumbo, m);
3180 continue;
3181 }
3182 if (bge_newbuf_jumbo(sc, sc->bge_jumbo,
3183 NULL)== ENOBUFS) {
3184 ifp->if_ierrors++;
3185 bge_newbuf_jumbo(sc, sc->bge_jumbo, m);
3186 continue;
3187 }
3188 } else {
3189 BGE_INC(sc->bge_std, BGE_STD_RX_RING_CNT);
3190 m = sc->bge_cdata.bge_rx_std_chain[rxidx];
3191
3192 sc->bge_cdata.bge_rx_std_chain[rxidx] = NULL;
3193 stdcnt++;
3194 dmamap = sc->bge_cdata.bge_rx_std_map[rxidx];
3195 sc->bge_cdata.bge_rx_std_map[rxidx] = 0;
3196 bus_dmamap_sync(sc->bge_dmatag, dmamap, 0,
3197 dmamap->dm_mapsize, BUS_DMASYNC_POSTREAD);
3198 bus_dmamap_unload(sc->bge_dmatag, dmamap);
3199 if (cur_rx->bge_flags & BGE_RXBDFLAG_ERROR) {
3200 ifp->if_ierrors++;
3201 bge_newbuf_std(sc, sc->bge_std, m, dmamap);
3202 continue;
3203 }
3204 if (bge_newbuf_std(sc, sc->bge_std,
3205 NULL, dmamap) == ENOBUFS) {
3206 ifp->if_ierrors++;
3207 bge_newbuf_std(sc, sc->bge_std, m, dmamap);
3208 continue;
3209 }
3210 }
3211
3212 ifp->if_ipackets++;
3213 #ifndef __NO_STRICT_ALIGNMENT
3214 /*
3215 * XXX: if the 5701 PCIX-Rx-DMA workaround is in effect,
3216 * the Rx buffer has the layer-2 header unaligned.
3217 * If our CPU requires alignment, re-align by copying.
3218 */
3219 if (sc->bge_flags & BGE_RX_ALIGNBUG) {
3220 memmove(mtod(m, char *) + ETHER_ALIGN, m->m_data,
3221 cur_rx->bge_len);
3222 m->m_data += ETHER_ALIGN;
3223 }
3224 #endif
3225
3226 m->m_pkthdr.len = m->m_len = cur_rx->bge_len - ETHER_CRC_LEN;
3227 m->m_pkthdr.rcvif = ifp;
3228
3229 /*
3230 * Handle BPF listeners. Let the BPF user see the packet.
3231 */
3232 if (ifp->if_bpf)
3233 bpf_ops->bpf_mtap(ifp->if_bpf, m);
3234
3235 m->m_pkthdr.csum_flags = M_CSUM_IPv4;
3236
3237 if ((cur_rx->bge_ip_csum ^ 0xffff) != 0)
3238 m->m_pkthdr.csum_flags |= M_CSUM_IPv4_BAD;
3239 /*
3240 * Rx transport checksum-offload may also
3241 * have bugs with packets which, when transmitted,
3242 * were `runts' requiring padding.
3243 */
3244 if (cur_rx->bge_flags & BGE_RXBDFLAG_TCP_UDP_CSUM &&
3245 (/* (sc->_bge_quirks & BGE_QUIRK_SHORT_CKSUM_BUG) == 0 ||*/
3246 m->m_pkthdr.len >= ETHER_MIN_NOPAD)) {
3247 m->m_pkthdr.csum_data =
3248 cur_rx->bge_tcp_udp_csum;
3249 m->m_pkthdr.csum_flags |=
3250 (M_CSUM_TCPv4|M_CSUM_UDPv4|
3251 M_CSUM_DATA|M_CSUM_NO_PSEUDOHDR);
3252 }
3253
3254 /*
3255 * If we received a packet with a vlan tag, pass it
3256 * to vlan_input() instead of ether_input().
3257 */
3258 if (cur_rx->bge_flags & BGE_RXBDFLAG_VLAN_TAG) {
3259 VLAN_INPUT_TAG(ifp, m, cur_rx->bge_vlan_tag, continue);
3260 }
3261
3262 (*ifp->if_input)(ifp, m);
3263 }
3264
3265 sc->bge_rx_saved_considx = rx_cons;
3266 bge_writembx(sc, BGE_MBX_RX_CONS0_LO, sc->bge_rx_saved_considx);
3267 if (stdcnt)
3268 bge_writembx(sc, BGE_MBX_RX_STD_PROD_LO, sc->bge_std);
3269 if (jumbocnt)
3270 bge_writembx(sc, BGE_MBX_RX_JUMBO_PROD_LO, sc->bge_jumbo);
3271 }
3272
3273 static void
3274 bge_txeof(struct bge_softc *sc)
3275 {
3276 struct bge_tx_bd *cur_tx = NULL;
3277 struct ifnet *ifp;
3278 struct txdmamap_pool_entry *dma;
3279 bus_addr_t offset, toff;
3280 bus_size_t tlen;
3281 int tosync;
3282 struct mbuf *m;
3283
3284 ifp = &sc->ethercom.ec_if;
3285
3286 bus_dmamap_sync(sc->bge_dmatag, sc->bge_ring_map,
3287 offsetof(struct bge_ring_data, bge_status_block),
3288 sizeof (struct bge_status_block),
3289 BUS_DMASYNC_POSTREAD);
3290
3291 offset = offsetof(struct bge_ring_data, bge_tx_ring);
3292 tosync = sc->bge_rdata->bge_status_block.bge_idx[0].bge_tx_cons_idx -
3293 sc->bge_tx_saved_considx;
3294
3295 #if NRND > 0
3296 if (tosync != 0 && RND_ENABLED(&sc->rnd_source))
3297 rnd_add_uint32(&sc->rnd_source, tosync);
3298 #endif
3299
3300 toff = offset + (sc->bge_tx_saved_considx * sizeof (struct bge_tx_bd));
3301
3302 if (tosync < 0) {
3303 tlen = (BGE_TX_RING_CNT - sc->bge_tx_saved_considx) *
3304 sizeof (struct bge_tx_bd);
3305 bus_dmamap_sync(sc->bge_dmatag, sc->bge_ring_map,
3306 toff, tlen, BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
3307 tosync = -tosync;
3308 }
3309
3310 bus_dmamap_sync(sc->bge_dmatag, sc->bge_ring_map,
3311 offset, tosync * sizeof (struct bge_tx_bd),
3312 BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
3313
3314 /*
3315 * Go through our tx ring and free mbufs for those
3316 * frames that have been sent.
3317 */
3318 while (sc->bge_tx_saved_considx !=
3319 sc->bge_rdata->bge_status_block.bge_idx[0].bge_tx_cons_idx) {
3320 uint32_t idx = 0;
3321
3322 idx = sc->bge_tx_saved_considx;
3323 cur_tx = &sc->bge_rdata->bge_tx_ring[idx];
3324 if (cur_tx->bge_flags & BGE_TXBDFLAG_END)
3325 ifp->if_opackets++;
3326 m = sc->bge_cdata.bge_tx_chain[idx];
3327 if (m != NULL) {
3328 sc->bge_cdata.bge_tx_chain[idx] = NULL;
3329 dma = sc->txdma[idx];
3330 bus_dmamap_sync(sc->bge_dmatag, dma->dmamap, 0,
3331 dma->dmamap->dm_mapsize, BUS_DMASYNC_POSTWRITE);
3332 bus_dmamap_unload(sc->bge_dmatag, dma->dmamap);
3333 SLIST_INSERT_HEAD(&sc->txdma_list, dma, link);
3334 sc->txdma[idx] = NULL;
3335
3336 m_freem(m);
3337 }
3338 sc->bge_txcnt--;
3339 BGE_INC(sc->bge_tx_saved_considx, BGE_TX_RING_CNT);
3340 ifp->if_timer = 0;
3341 }
3342
3343 if (cur_tx != NULL)
3344 ifp->if_flags &= ~IFF_OACTIVE;
3345 }
3346
3347 static int
3348 bge_intr(void *xsc)
3349 {
3350 struct bge_softc *sc;
3351 struct ifnet *ifp;
3352 uint32_t statusword;
3353
3354 sc = xsc;
3355 ifp = &sc->ethercom.ec_if;
3356
3357 /* It is possible for the interrupt to arrive before
3358 * the status block is updated prior to the interrupt.
3359 * Reading the PCI State register will confirm whether the
3360 * interrupt is ours and will flush the status block.
3361 */
3362
3363 /* read status word from status block */
3364 statusword = sc->bge_rdata->bge_status_block.bge_status;
3365
3366 if ((statusword & BGE_STATFLAG_UPDATED) ||
3367 (!(CSR_READ_4(sc, BGE_PCI_PCISTATE) & BGE_PCISTATE_INTR_NOT_ACTIVE))) {
3368 /* Ack interrupt and stop others from occuring. */
3369 bge_writembx(sc, BGE_MBX_IRQ0_LO, 1);
3370
3371 BGE_EVCNT_INCR(sc->bge_ev_intr);
3372
3373 /* clear status word */
3374 sc->bge_rdata->bge_status_block.bge_status = 0;
3375
3376 if (BGE_ASICREV(sc->bge_chipid) == BGE_ASICREV_BCM5700 ||
3377 statusword & BGE_STATFLAG_LINKSTATE_CHANGED ||
3378 BGE_STS_BIT(sc, BGE_STS_LINK_EVT))
3379 bge_link_upd(sc);
3380
3381 if (ifp->if_flags & IFF_RUNNING) {
3382 /* Check RX return ring producer/consumer */
3383 bge_rxeof(sc);
3384
3385 /* Check TX ring producer/consumer */
3386 bge_txeof(sc);
3387 }
3388
3389 if (sc->bge_pending_rxintr_change) {
3390 uint32_t rx_ticks = sc->bge_rx_coal_ticks;
3391 uint32_t rx_bds = sc->bge_rx_max_coal_bds;
3392 uint32_t junk;
3393
3394 CSR_WRITE_4(sc, BGE_HCC_RX_COAL_TICKS, rx_ticks);
3395 DELAY(10);
3396 junk = CSR_READ_4(sc, BGE_HCC_RX_COAL_TICKS);
3397
3398 CSR_WRITE_4(sc, BGE_HCC_RX_MAX_COAL_BDS, rx_bds);
3399 DELAY(10);
3400 junk = CSR_READ_4(sc, BGE_HCC_RX_MAX_COAL_BDS);
3401
3402 sc->bge_pending_rxintr_change = 0;
3403 }
3404 bge_handle_events(sc);
3405
3406 /* Re-enable interrupts. */
3407 bge_writembx(sc, BGE_MBX_IRQ0_LO, 0);
3408
3409 if (ifp->if_flags & IFF_RUNNING && !IFQ_IS_EMPTY(&ifp->if_snd))
3410 bge_start(ifp);
3411
3412 return 1;
3413 } else
3414 return 0;
3415 }
3416
3417 static void
3418 bge_tick(void *xsc)
3419 {
3420 struct bge_softc *sc = xsc;
3421 struct mii_data *mii = &sc->bge_mii;
3422 int s;
3423
3424 s = splnet();
3425
3426 if (BGE_IS_5705_PLUS(sc))
3427 bge_stats_update_regs(sc);
3428 else
3429 bge_stats_update(sc);
3430
3431 if (sc->bge_flags & BGE_PHY_FIBER_TBI) {
3432 /*
3433 * Since in TBI mode auto-polling can't be used we should poll
3434 * link status manually. Here we register pending link event
3435 * and trigger interrupt.
3436 */
3437 BGE_STS_SETBIT(sc, BGE_STS_LINK_EVT);
3438 BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_INTR_SET);
3439 } else {
3440 /*
3441 * Do not touch PHY if we have link up. This could break
3442 * IPMI/ASF mode or produce extra input errors.
3443 * (extra input errors was reported for bcm5701 & bcm5704).
3444 */
3445 if (!BGE_STS_BIT(sc, BGE_STS_LINK))
3446 mii_tick(mii);
3447 }
3448
3449 callout_reset(&sc->bge_timeout, hz, bge_tick, sc);
3450
3451 splx(s);
3452 }
3453
3454 static void
3455 bge_stats_update_regs(struct bge_softc *sc)
3456 {
3457 struct ifnet *ifp = &sc->ethercom.ec_if;
3458
3459 ifp->if_collisions += CSR_READ_4(sc, BGE_MAC_STATS +
3460 offsetof(struct bge_mac_stats_regs, etherStatsCollisions));
3461
3462 ifp->if_ierrors += CSR_READ_4(sc, BGE_RXLP_LOCSTAT_IFIN_DROPS);
3463 ifp->if_ierrors += CSR_READ_4(sc, BGE_RXLP_LOCSTAT_IFIN_ERRORS);
3464 ifp->if_ierrors += CSR_READ_4(sc, BGE_RXLP_LOCSTAT_OUT_OF_BDS);
3465 }
3466
3467 static void
3468 bge_stats_update(struct bge_softc *sc)
3469 {
3470 struct ifnet *ifp = &sc->ethercom.ec_if;
3471 bus_size_t stats = BGE_MEMWIN_START + BGE_STATS_BLOCK;
3472
3473 #define READ_STAT(sc, stats, stat) \
3474 CSR_READ_4(sc, stats + offsetof(struct bge_stats, stat))
3475
3476 ifp->if_collisions +=
3477 (READ_STAT(sc, stats, dot3StatsSingleCollisionFrames.bge_addr_lo) +
3478 READ_STAT(sc, stats, dot3StatsMultipleCollisionFrames.bge_addr_lo) +
3479 READ_STAT(sc, stats, dot3StatsExcessiveCollisions.bge_addr_lo) +
3480 READ_STAT(sc, stats, dot3StatsLateCollisions.bge_addr_lo)) -
3481 ifp->if_collisions;
3482
3483 BGE_EVCNT_UPD(sc->bge_ev_tx_xoff,
3484 READ_STAT(sc, stats, outXoffSent.bge_addr_lo));
3485 BGE_EVCNT_UPD(sc->bge_ev_tx_xon,
3486 READ_STAT(sc, stats, outXonSent.bge_addr_lo));
3487 BGE_EVCNT_UPD(sc->bge_ev_rx_xoff,
3488 READ_STAT(sc, stats,
3489 xoffPauseFramesReceived.bge_addr_lo));
3490 BGE_EVCNT_UPD(sc->bge_ev_rx_xon,
3491 READ_STAT(sc, stats, xonPauseFramesReceived.bge_addr_lo));
3492 BGE_EVCNT_UPD(sc->bge_ev_rx_macctl,
3493 READ_STAT(sc, stats,
3494 macControlFramesReceived.bge_addr_lo));
3495 BGE_EVCNT_UPD(sc->bge_ev_xoffentered,
3496 READ_STAT(sc, stats, xoffStateEntered.bge_addr_lo));
3497
3498 #undef READ_STAT
3499
3500 #ifdef notdef
3501 ifp->if_collisions +=
3502 (sc->bge_rdata->bge_info.bge_stats.dot3StatsSingleCollisionFrames +
3503 sc->bge_rdata->bge_info.bge_stats.dot3StatsMultipleCollisionFrames +
3504 sc->bge_rdata->bge_info.bge_stats.dot3StatsExcessiveCollisions +
3505 sc->bge_rdata->bge_info.bge_stats.dot3StatsLateCollisions) -
3506 ifp->if_collisions;
3507 #endif
3508 }
3509
3510 /*
3511 * Pad outbound frame to ETHER_MIN_NOPAD for an unusual reason.
3512 * The bge hardware will pad out Tx runts to ETHER_MIN_NOPAD,
3513 * but when such padded frames employ the bge IP/TCP checksum offload,
3514 * the hardware checksum assist gives incorrect results (possibly
3515 * from incorporating its own padding into the UDP/TCP checksum; who knows).
3516 * If we pad such runts with zeros, the onboard checksum comes out correct.
3517 */
3518 static inline int
3519 bge_cksum_pad(struct mbuf *pkt)
3520 {
3521 struct mbuf *last = NULL;
3522 int padlen;
3523
3524 padlen = ETHER_MIN_NOPAD - pkt->m_pkthdr.len;
3525
3526 /* if there's only the packet-header and we can pad there, use it. */
3527 if (pkt->m_pkthdr.len == pkt->m_len &&
3528 M_TRAILINGSPACE(pkt) >= padlen) {
3529 last = pkt;
3530 } else {
3531 /*
3532 * Walk packet chain to find last mbuf. We will either
3533 * pad there, or append a new mbuf and pad it
3534 * (thus perhaps avoiding the bcm5700 dma-min bug).
3535 */
3536 for (last = pkt; last->m_next != NULL; last = last->m_next) {
3537 continue; /* do nothing */
3538 }
3539
3540 /* `last' now points to last in chain. */
3541 if (M_TRAILINGSPACE(last) < padlen) {
3542 /* Allocate new empty mbuf, pad it. Compact later. */
3543 struct mbuf *n;
3544 MGET(n, M_DONTWAIT, MT_DATA);
3545 if (n == NULL)
3546 return ENOBUFS;
3547 n->m_len = 0;
3548 last->m_next = n;
3549 last = n;
3550 }
3551 }
3552
3553 KDASSERT(!M_READONLY(last));
3554 KDASSERT(M_TRAILINGSPACE(last) >= padlen);
3555
3556 /* Now zero the pad area, to avoid the bge cksum-assist bug */
3557 memset(mtod(last, char *) + last->m_len, 0, padlen);
3558 last->m_len += padlen;
3559 pkt->m_pkthdr.len += padlen;
3560 return 0;
3561 }
3562
3563 /*
3564 * Compact outbound packets to avoid bug with DMA segments less than 8 bytes.
3565 */
3566 static inline int
3567 bge_compact_dma_runt(struct mbuf *pkt)
3568 {
3569 struct mbuf *m, *prev;
3570 int totlen, prevlen;
3571
3572 prev = NULL;
3573 totlen = 0;
3574 prevlen = -1;
3575
3576 for (m = pkt; m != NULL; prev = m,m = m->m_next) {
3577 int mlen = m->m_len;
3578 int shortfall = 8 - mlen ;
3579
3580 totlen += mlen;
3581 if (mlen == 0) {
3582 continue;
3583 }
3584 if (mlen >= 8)
3585 continue;
3586
3587 /* If we get here, mbuf data is too small for DMA engine.
3588 * Try to fix by shuffling data to prev or next in chain.
3589 * If that fails, do a compacting deep-copy of the whole chain.
3590 */
3591
3592 /* Internal frag. If fits in prev, copy it there. */
3593 if (prev && M_TRAILINGSPACE(prev) >= m->m_len) {
3594 memcpy(prev->m_data + prev->m_len, m->m_data, mlen);
3595 prev->m_len += mlen;
3596 m->m_len = 0;
3597 /* XXX stitch chain */
3598 prev->m_next = m_free(m);
3599 m = prev;
3600 continue;
3601 }
3602 else if (m->m_next != NULL &&
3603 M_TRAILINGSPACE(m) >= shortfall &&
3604 m->m_next->m_len >= (8 + shortfall)) {
3605 /* m is writable and have enough data in next, pull up. */
3606
3607 memcpy(m->m_data + m->m_len, m->m_next->m_data,
3608 shortfall);
3609 m->m_len += shortfall;
3610 m->m_next->m_len -= shortfall;
3611 m->m_next->m_data += shortfall;
3612 }
3613 else if (m->m_next == NULL || 1) {
3614 /* Got a runt at the very end of the packet.
3615 * borrow data from the tail of the preceding mbuf and
3616 * update its length in-place. (The original data is still
3617 * valid, so we can do this even if prev is not writable.)
3618 */
3619
3620 /* if we'd make prev a runt, just move all of its data. */
3621 KASSERT(prev != NULL /*, ("runt but null PREV")*/);
3622 KASSERT(prev->m_len >= 8 /*, ("runt prev")*/);
3623
3624 if ((prev->m_len - shortfall) < 8)
3625 shortfall = prev->m_len;
3626
3627 #ifdef notyet /* just do the safe slow thing for now */
3628 if (!M_READONLY(m)) {
3629 if (M_LEADINGSPACE(m) < shorfall) {
3630 void *m_dat;
3631 m_dat = (m->m_flags & M_PKTHDR) ?
3632 m->m_pktdat : m->dat;
3633 memmove(m_dat, mtod(m, void*), m->m_len);
3634 m->m_data = m_dat;
3635 }
3636 } else
3637 #endif /* just do the safe slow thing */
3638 {
3639 struct mbuf * n = NULL;
3640 int newprevlen = prev->m_len - shortfall;
3641
3642 MGET(n, M_NOWAIT, MT_DATA);
3643 if (n == NULL)
3644 return ENOBUFS;
3645 KASSERT(m->m_len + shortfall < MLEN
3646 /*,
3647 ("runt %d +prev %d too big\n", m->m_len, shortfall)*/);
3648
3649 /* first copy the data we're stealing from prev */
3650 memcpy(n->m_data, prev->m_data + newprevlen,
3651 shortfall);
3652
3653 /* update prev->m_len accordingly */
3654 prev->m_len -= shortfall;
3655
3656 /* copy data from runt m */
3657 memcpy(n->m_data + shortfall, m->m_data,
3658 m->m_len);
3659
3660 /* n holds what we stole from prev, plus m */
3661 n->m_len = shortfall + m->m_len;
3662
3663 /* stitch n into chain and free m */
3664 n->m_next = m->m_next;
3665 prev->m_next = n;
3666 /* KASSERT(m->m_next == NULL); */
3667 m->m_next = NULL;
3668 m_free(m);
3669 m = n; /* for continuing loop */
3670 }
3671 }
3672 prevlen = m->m_len;
3673 }
3674 return 0;
3675 }
3676
3677 /*
3678 * Encapsulate an mbuf chain in the tx ring by coupling the mbuf data
3679 * pointers to descriptors.
3680 */
3681 static int
3682 bge_encap(struct bge_softc *sc, struct mbuf *m_head, uint32_t *txidx)
3683 {
3684 struct bge_tx_bd *f = NULL;
3685 uint32_t frag, cur;
3686 uint16_t csum_flags = 0;
3687 uint16_t txbd_tso_flags = 0;
3688 struct txdmamap_pool_entry *dma;
3689 bus_dmamap_t dmamap;
3690 int i = 0;
3691 struct m_tag *mtag;
3692 int use_tso, maxsegsize, error;
3693
3694 cur = frag = *txidx;
3695
3696 if (m_head->m_pkthdr.csum_flags) {
3697 if (m_head->m_pkthdr.csum_flags & M_CSUM_IPv4)
3698 csum_flags |= BGE_TXBDFLAG_IP_CSUM;
3699 if (m_head->m_pkthdr.csum_flags & (M_CSUM_TCPv4|M_CSUM_UDPv4))
3700 csum_flags |= BGE_TXBDFLAG_TCP_UDP_CSUM;
3701 }
3702
3703 /*
3704 * If we were asked to do an outboard checksum, and the NIC
3705 * has the bug where it sometimes adds in the Ethernet padding,
3706 * explicitly pad with zeros so the cksum will be correct either way.
3707 * (For now, do this for all chip versions, until newer
3708 * are confirmed to not require the workaround.)
3709 */
3710 if ((csum_flags & BGE_TXBDFLAG_TCP_UDP_CSUM) == 0 ||
3711 #ifdef notyet
3712 (sc->bge_quirks & BGE_QUIRK_SHORT_CKSUM_BUG) == 0 ||
3713 #endif
3714 m_head->m_pkthdr.len >= ETHER_MIN_NOPAD)
3715 goto check_dma_bug;
3716
3717 if (bge_cksum_pad(m_head) != 0)
3718 return ENOBUFS;
3719
3720 check_dma_bug:
3721 if (!(BGE_CHIPREV(sc->bge_chipid) == BGE_CHIPREV_5700_BX))
3722 goto doit;
3723
3724 /*
3725 * bcm5700 Revision B silicon cannot handle DMA descriptors with
3726 * less than eight bytes. If we encounter a teeny mbuf
3727 * at the end of a chain, we can pad. Otherwise, copy.
3728 */
3729 if (bge_compact_dma_runt(m_head) != 0)
3730 return ENOBUFS;
3731
3732 doit:
3733 dma = SLIST_FIRST(&sc->txdma_list);
3734 if (dma == NULL)
3735 return ENOBUFS;
3736 dmamap = dma->dmamap;
3737
3738 /*
3739 * Set up any necessary TSO state before we start packing...
3740 */
3741 use_tso = (m_head->m_pkthdr.csum_flags & M_CSUM_TSOv4) != 0;
3742 if (!use_tso) {
3743 maxsegsize = 0;
3744 } else { /* TSO setup */
3745 unsigned mss;
3746 struct ether_header *eh;
3747 unsigned ip_tcp_hlen, iptcp_opt_words, tcp_seg_flags, offset;
3748 struct mbuf * m0 = m_head;
3749 struct ip *ip;
3750 struct tcphdr *th;
3751 int iphl, hlen;
3752
3753 /*
3754 * XXX It would be nice if the mbuf pkthdr had offset
3755 * fields for the protocol headers.
3756 */
3757
3758 eh = mtod(m0, struct ether_header *);
3759 switch (htons(eh->ether_type)) {
3760 case ETHERTYPE_IP:
3761 offset = ETHER_HDR_LEN;
3762 break;
3763
3764 case ETHERTYPE_VLAN:
3765 offset = ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN;
3766 break;
3767
3768 default:
3769 /*
3770 * Don't support this protocol or encapsulation.
3771 */
3772 return ENOBUFS;
3773 }
3774
3775 /*
3776 * TCP/IP headers are in the first mbuf; we can do
3777 * this the easy way.
3778 */
3779 iphl = M_CSUM_DATA_IPv4_IPHL(m0->m_pkthdr.csum_data);
3780 hlen = iphl + offset;
3781 if (__predict_false(m0->m_len <
3782 (hlen + sizeof(struct tcphdr)))) {
3783
3784 aprint_debug_dev(sc->bge_dev,
3785 "TSO: hard case m0->m_len == %d < ip/tcp hlen %zd,"
3786 "not handled yet\n",
3787 m0->m_len, hlen+ sizeof(struct tcphdr));
3788 #ifdef NOTYET
3789 /*
3790 * XXX jonathan (at) NetBSD.org: untested.
3791 * how to force this branch to be taken?
3792 */
3793 BGE_EVCNT_INCR(&sc->sc_ev_txtsopain);
3794
3795 m_copydata(m0, offset, sizeof(ip), &ip);
3796 m_copydata(m0, hlen, sizeof(th), &th);
3797
3798 ip.ip_len = 0;
3799
3800 m_copyback(m0, hlen + offsetof(struct ip, ip_len),
3801 sizeof(ip.ip_len), &ip.ip_len);
3802
3803 th.th_sum = in_cksum_phdr(ip.ip_src.s_addr,
3804 ip.ip_dst.s_addr, htons(IPPROTO_TCP));
3805
3806 m_copyback(m0, hlen + offsetof(struct tcphdr, th_sum),
3807 sizeof(th.th_sum), &th.th_sum);
3808
3809 hlen += th.th_off << 2;
3810 iptcp_opt_words = hlen;
3811 #else
3812 /*
3813 * if_wm "hard" case not yet supported, can we not
3814 * mandate it out of existence?
3815 */
3816 (void) ip; (void)th; (void) ip_tcp_hlen;
3817
3818 return ENOBUFS;
3819 #endif
3820 } else {
3821 ip = (struct ip *) (mtod(m0, char *) + offset);
3822 th = (struct tcphdr *) (mtod(m0, char *) + hlen);
3823 ip_tcp_hlen = iphl + (th->th_off << 2);
3824
3825 /* Total IP/TCP options, in 32-bit words */
3826 iptcp_opt_words = (ip_tcp_hlen
3827 - sizeof(struct tcphdr)
3828 - sizeof(struct ip)) >> 2;
3829 }
3830 if (BGE_IS_5750_OR_BEYOND(sc)) {
3831 th->th_sum = 0;
3832 csum_flags &= ~(BGE_TXBDFLAG_TCP_UDP_CSUM);
3833 } else {
3834 /*
3835 * XXX jonathan (at) NetBSD.org: 5705 untested.
3836 * Requires TSO firmware patch for 5701/5703/5704.
3837 */
3838 th->th_sum = in_cksum_phdr(ip->ip_src.s_addr,
3839 ip->ip_dst.s_addr, htons(IPPROTO_TCP));
3840 }
3841
3842 mss = m_head->m_pkthdr.segsz;
3843 txbd_tso_flags |=
3844 BGE_TXBDFLAG_CPU_PRE_DMA |
3845 BGE_TXBDFLAG_CPU_POST_DMA;
3846
3847 /*
3848 * Our NIC TSO-assist assumes TSO has standard, optionless
3849 * IPv4 and TCP headers, which total 40 bytes. By default,
3850 * the NIC copies 40 bytes of IP/TCP header from the
3851 * supplied header into the IP/TCP header portion of
3852 * each post-TSO-segment. If the supplied packet has IP or
3853 * TCP options, we need to tell the NIC to copy those extra
3854 * bytes into each post-TSO header, in addition to the normal
3855 * 40-byte IP/TCP header (and to leave space accordingly).
3856 * Unfortunately, the driver encoding of option length
3857 * varies across different ASIC families.
3858 */
3859 tcp_seg_flags = 0;
3860 if (iptcp_opt_words) {
3861 if (BGE_IS_5705_PLUS(sc)) {
3862 tcp_seg_flags =
3863 iptcp_opt_words << 11;
3864 } else {
3865 txbd_tso_flags |=
3866 iptcp_opt_words << 12;
3867 }
3868 }
3869 maxsegsize = mss | tcp_seg_flags;
3870 ip->ip_len = htons(mss + ip_tcp_hlen);
3871
3872 } /* TSO setup */
3873
3874 /*
3875 * Start packing the mbufs in this chain into
3876 * the fragment pointers. Stop when we run out
3877 * of fragments or hit the end of the mbuf chain.
3878 */
3879 error = bus_dmamap_load_mbuf(sc->bge_dmatag, dmamap, m_head,
3880 BUS_DMA_NOWAIT);
3881 if (error)
3882 return ENOBUFS;
3883 /*
3884 * Sanity check: avoid coming within 16 descriptors
3885 * of the end of the ring.
3886 */
3887 if (dmamap->dm_nsegs > (BGE_TX_RING_CNT - sc->bge_txcnt - 16)) {
3888 BGE_TSO_PRINTF(("%s: "
3889 " dmamap_load_mbuf too close to ring wrap\n",
3890 device_xname(sc->bge_dev)));
3891 goto fail_unload;
3892 }
3893
3894 mtag = sc->ethercom.ec_nvlans ?
3895 m_tag_find(m_head, PACKET_TAG_VLAN, NULL) : NULL;
3896
3897
3898 /* Iterate over dmap-map fragments. */
3899 for (i = 0; i < dmamap->dm_nsegs; i++) {
3900 f = &sc->bge_rdata->bge_tx_ring[frag];
3901 if (sc->bge_cdata.bge_tx_chain[frag] != NULL)
3902 break;
3903
3904 BGE_HOSTADDR(f->bge_addr, dmamap->dm_segs[i].ds_addr);
3905 f->bge_len = dmamap->dm_segs[i].ds_len;
3906
3907 /*
3908 * For 5751 and follow-ons, for TSO we must turn
3909 * off checksum-assist flag in the tx-descr, and
3910 * supply the ASIC-revision-specific encoding
3911 * of TSO flags and segsize.
3912 */
3913 if (use_tso) {
3914 if (BGE_IS_5750_OR_BEYOND(sc) || i == 0) {
3915 f->bge_rsvd = maxsegsize;
3916 f->bge_flags = csum_flags | txbd_tso_flags;
3917 } else {
3918 f->bge_rsvd = 0;
3919 f->bge_flags =
3920 (csum_flags | txbd_tso_flags) & 0x0fff;
3921 }
3922 } else {
3923 f->bge_rsvd = 0;
3924 f->bge_flags = csum_flags;
3925 }
3926
3927 if (mtag != NULL) {
3928 f->bge_flags |= BGE_TXBDFLAG_VLAN_TAG;
3929 f->bge_vlan_tag = VLAN_TAG_VALUE(mtag);
3930 } else {
3931 f->bge_vlan_tag = 0;
3932 }
3933 cur = frag;
3934 BGE_INC(frag, BGE_TX_RING_CNT);
3935 }
3936
3937 if (i < dmamap->dm_nsegs) {
3938 BGE_TSO_PRINTF(("%s: reached %d < dm_nsegs %d\n",
3939 device_xname(sc->bge_dev), i, dmamap->dm_nsegs));
3940 goto fail_unload;
3941 }
3942
3943 bus_dmamap_sync(sc->bge_dmatag, dmamap, 0, dmamap->dm_mapsize,
3944 BUS_DMASYNC_PREWRITE);
3945
3946 if (frag == sc->bge_tx_saved_considx) {
3947 BGE_TSO_PRINTF(("%s: frag %d = wrapped id %d?\n",
3948 device_xname(sc->bge_dev), frag, sc->bge_tx_saved_considx));
3949
3950 goto fail_unload;
3951 }
3952
3953 sc->bge_rdata->bge_tx_ring[cur].bge_flags |= BGE_TXBDFLAG_END;
3954 sc->bge_cdata.bge_tx_chain[cur] = m_head;
3955 SLIST_REMOVE_HEAD(&sc->txdma_list, link);
3956 sc->txdma[cur] = dma;
3957 sc->bge_txcnt += dmamap->dm_nsegs;
3958
3959 *txidx = frag;
3960
3961 return 0;
3962
3963 fail_unload:
3964 bus_dmamap_unload(sc->bge_dmatag, dmamap);
3965
3966 return ENOBUFS;
3967 }
3968
3969 /*
3970 * Main transmit routine. To avoid having to do mbuf copies, we put pointers
3971 * to the mbuf data regions directly in the transmit descriptors.
3972 */
3973 static void
3974 bge_start(struct ifnet *ifp)
3975 {
3976 struct bge_softc *sc;
3977 struct mbuf *m_head = NULL;
3978 uint32_t prodidx;
3979 int pkts = 0;
3980
3981 sc = ifp->if_softc;
3982
3983 if ((ifp->if_flags & (IFF_RUNNING|IFF_OACTIVE)) != IFF_RUNNING)
3984 return;
3985
3986 prodidx = sc->bge_tx_prodidx;
3987
3988 while (sc->bge_cdata.bge_tx_chain[prodidx] == NULL) {
3989 IFQ_POLL(&ifp->if_snd, m_head);
3990 if (m_head == NULL)
3991 break;
3992
3993 #if 0
3994 /*
3995 * XXX
3996 * safety overkill. If this is a fragmented packet chain
3997 * with delayed TCP/UDP checksums, then only encapsulate
3998 * it if we have enough descriptors to handle the entire
3999 * chain at once.
4000 * (paranoia -- may not actually be needed)
4001 */
4002 if (m_head->m_flags & M_FIRSTFRAG &&
4003 m_head->m_pkthdr.csum_flags & (CSUM_DELAY_DATA)) {
4004 if ((BGE_TX_RING_CNT - sc->bge_txcnt) <
4005 M_CSUM_DATA_IPv4_OFFSET(m_head->m_pkthdr.csum_data) + 16) {
4006 ifp->if_flags |= IFF_OACTIVE;
4007 break;
4008 }
4009 }
4010 #endif
4011
4012 /*
4013 * Pack the data into the transmit ring. If we
4014 * don't have room, set the OACTIVE flag and wait
4015 * for the NIC to drain the ring.
4016 */
4017 if (bge_encap(sc, m_head, &prodidx)) {
4018 ifp->if_flags |= IFF_OACTIVE;
4019 break;
4020 }
4021
4022 /* now we are committed to transmit the packet */
4023 IFQ_DEQUEUE(&ifp->if_snd, m_head);
4024 pkts++;
4025
4026 /*
4027 * If there's a BPF listener, bounce a copy of this frame
4028 * to him.
4029 */
4030 if (ifp->if_bpf)
4031 bpf_ops->bpf_mtap(ifp->if_bpf, m_head);
4032 }
4033 if (pkts == 0)
4034 return;
4035
4036 /* Transmit */
4037 bge_writembx(sc, BGE_MBX_TX_HOST_PROD0_LO, prodidx);
4038 /* 5700 b2 errata */
4039 if (BGE_CHIPREV(sc->bge_chipid) == BGE_CHIPREV_5700_BX)
4040 bge_writembx(sc, BGE_MBX_TX_HOST_PROD0_LO, prodidx);
4041
4042 sc->bge_tx_prodidx = prodidx;
4043
4044 /*
4045 * Set a timeout in case the chip goes out to lunch.
4046 */
4047 ifp->if_timer = 5;
4048 }
4049
4050 static int
4051 bge_init(struct ifnet *ifp)
4052 {
4053 struct bge_softc *sc = ifp->if_softc;
4054 const uint16_t *m;
4055 int s, error = 0;
4056
4057 s = splnet();
4058
4059 ifp = &sc->ethercom.ec_if;
4060
4061 /* Cancel pending I/O and flush buffers. */
4062 bge_stop(ifp, 0);
4063 bge_reset(sc);
4064 bge_chipinit(sc);
4065
4066 /*
4067 * Init the various state machines, ring
4068 * control blocks and firmware.
4069 */
4070 error = bge_blockinit(sc);
4071 if (error != 0) {
4072 aprint_error_dev(sc->bge_dev, "initialization error %d\n",
4073 error);
4074 splx(s);
4075 return error;
4076 }
4077
4078 ifp = &sc->ethercom.ec_if;
4079
4080 /* Specify MTU. */
4081 CSR_WRITE_4(sc, BGE_RX_MTU, ifp->if_mtu +
4082 ETHER_HDR_LEN + ETHER_CRC_LEN + ETHER_VLAN_ENCAP_LEN);
4083
4084 /* Load our MAC address. */
4085 m = (const uint16_t *)&(CLLADDR(ifp->if_sadl)[0]);
4086 CSR_WRITE_4(sc, BGE_MAC_ADDR1_LO, htons(m[0]));
4087 CSR_WRITE_4(sc, BGE_MAC_ADDR1_HI, (htons(m[1]) << 16) | htons(m[2]));
4088
4089 /* Enable or disable promiscuous mode as needed. */
4090 if (ifp->if_flags & IFF_PROMISC) {
4091 BGE_SETBIT(sc, BGE_RX_MODE, BGE_RXMODE_RX_PROMISC);
4092 } else {
4093 BGE_CLRBIT(sc, BGE_RX_MODE, BGE_RXMODE_RX_PROMISC);
4094 }
4095
4096 /* Program multicast filter. */
4097 bge_setmulti(sc);
4098
4099 /* Init RX ring. */
4100 bge_init_rx_ring_std(sc);
4101
4102 /*
4103 * Workaround for a bug in 5705 ASIC rev A0. Poll the NIC's
4104 * memory to insure that the chip has in fact read the first
4105 * entry of the ring.
4106 */
4107 if (sc->bge_chipid == BGE_CHIPID_BCM5705_A0) {
4108 uint32_t v, i;
4109 for (i = 0; i < 10; i++) {
4110 DELAY(20);
4111 v = bge_readmem_ind(sc, BGE_STD_RX_RINGS + 8);
4112 if (v == (MCLBYTES - ETHER_ALIGN))
4113 break;
4114 }
4115 if (i == 10)
4116 aprint_error_dev(sc->bge_dev,
4117 "5705 A0 chip failed to load RX ring\n");
4118 }
4119
4120 /* Init jumbo RX ring. */
4121 if (ifp->if_mtu > (ETHERMTU + ETHER_HDR_LEN + ETHER_CRC_LEN))
4122 bge_init_rx_ring_jumbo(sc);
4123
4124 /* Init our RX return ring index */
4125 sc->bge_rx_saved_considx = 0;
4126
4127 /* Init TX ring. */
4128 bge_init_tx_ring(sc);
4129
4130 /* Turn on transmitter */
4131 BGE_SETBIT(sc, BGE_TX_MODE, BGE_TXMODE_ENABLE);
4132
4133 /* Turn on receiver */
4134 BGE_SETBIT(sc, BGE_RX_MODE, BGE_RXMODE_ENABLE);
4135
4136 CSR_WRITE_4(sc, BGE_MAX_RX_FRAME_LOWAT, 2);
4137
4138 /* Tell firmware we're alive. */
4139 BGE_SETBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
4140
4141 /* Enable host interrupts. */
4142 BGE_SETBIT(sc, BGE_PCI_MISC_CTL, BGE_PCIMISCCTL_CLEAR_INTA);
4143 BGE_CLRBIT(sc, BGE_PCI_MISC_CTL, BGE_PCIMISCCTL_MASK_PCI_INTR);
4144 bge_writembx(sc, BGE_MBX_IRQ0_LO, 0);
4145
4146 if ((error = bge_ifmedia_upd(ifp)) != 0)
4147 goto out;
4148
4149 ifp->if_flags |= IFF_RUNNING;
4150 ifp->if_flags &= ~IFF_OACTIVE;
4151
4152 callout_reset(&sc->bge_timeout, hz, bge_tick, sc);
4153
4154 out:
4155 splx(s);
4156
4157 return error;
4158 }
4159
4160 /*
4161 * Set media options.
4162 */
4163 static int
4164 bge_ifmedia_upd(struct ifnet *ifp)
4165 {
4166 struct bge_softc *sc = ifp->if_softc;
4167 struct mii_data *mii = &sc->bge_mii;
4168 struct ifmedia *ifm = &sc->bge_ifmedia;
4169 int rc;
4170
4171 /* If this is a 1000baseX NIC, enable the TBI port. */
4172 if (sc->bge_flags & BGE_PHY_FIBER_TBI) {
4173 if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
4174 return EINVAL;
4175 switch (IFM_SUBTYPE(ifm->ifm_media)) {
4176 case IFM_AUTO:
4177 /*
4178 * The BCM5704 ASIC appears to have a special
4179 * mechanism for programming the autoneg
4180 * advertisement registers in TBI mode.
4181 */
4182 if (BGE_ASICREV(sc->bge_chipid) == BGE_ASICREV_BCM5704) {
4183 uint32_t sgdig;
4184 sgdig = CSR_READ_4(sc, BGE_SGDIG_STS);
4185 if (sgdig & BGE_SGDIGSTS_DONE) {
4186 CSR_WRITE_4(sc, BGE_TX_TBI_AUTONEG, 0);
4187 sgdig = CSR_READ_4(sc, BGE_SGDIG_CFG);
4188 sgdig |= BGE_SGDIGCFG_AUTO |
4189 BGE_SGDIGCFG_PAUSE_CAP |
4190 BGE_SGDIGCFG_ASYM_PAUSE;
4191 CSR_WRITE_4(sc, BGE_SGDIG_CFG,
4192 sgdig | BGE_SGDIGCFG_SEND);
4193 DELAY(5);
4194 CSR_WRITE_4(sc, BGE_SGDIG_CFG, sgdig);
4195 }
4196 }
4197 break;
4198 case IFM_1000_SX:
4199 if ((ifm->ifm_media & IFM_GMASK) == IFM_FDX) {
4200 BGE_CLRBIT(sc, BGE_MAC_MODE,
4201 BGE_MACMODE_HALF_DUPLEX);
4202 } else {
4203 BGE_SETBIT(sc, BGE_MAC_MODE,
4204 BGE_MACMODE_HALF_DUPLEX);
4205 }
4206 break;
4207 default:
4208 return EINVAL;
4209 }
4210 /* XXX 802.3x flow control for 1000BASE-SX */
4211 return 0;
4212 }
4213
4214 BGE_STS_SETBIT(sc, BGE_STS_LINK_EVT);
4215 if ((rc = mii_mediachg(mii)) == ENXIO)
4216 return 0;
4217
4218 /*
4219 * Force an interrupt so that we will call bge_link_upd
4220 * if needed and clear any pending link state attention.
4221 * Without this we are not getting any further interrupts
4222 * for link state changes and thus will not UP the link and
4223 * not be able to send in bge_start. The only way to get
4224 * things working was to receive a packet and get a RX intr.
4225 */
4226 if (BGE_ASICREV(sc->bge_chipid) == BGE_ASICREV_BCM5700 ||
4227 sc->bge_flags & BGE_IS_5788)
4228 BGE_SETBIT(sc, BGE_MISC_LOCAL_CTL, BGE_MLC_INTR_SET);
4229 else
4230 BGE_SETBIT(sc, BGE_HCC_MODE, BGE_HCCMODE_COAL_NOW);
4231
4232 return rc;
4233 }
4234
4235 /*
4236 * Report current media status.
4237 */
4238 static void
4239 bge_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
4240 {
4241 struct bge_softc *sc = ifp->if_softc;
4242 struct mii_data *mii = &sc->bge_mii;
4243
4244 if (sc->bge_flags & BGE_PHY_FIBER_TBI) {
4245 ifmr->ifm_status = IFM_AVALID;
4246 ifmr->ifm_active = IFM_ETHER;
4247 if (CSR_READ_4(sc, BGE_MAC_STS) &
4248 BGE_MACSTAT_TBI_PCS_SYNCHED)
4249 ifmr->ifm_status |= IFM_ACTIVE;
4250 ifmr->ifm_active |= IFM_1000_SX;
4251 if (CSR_READ_4(sc, BGE_MAC_MODE) & BGE_MACMODE_HALF_DUPLEX)
4252 ifmr->ifm_active |= IFM_HDX;
4253 else
4254 ifmr->ifm_active |= IFM_FDX;
4255 return;
4256 }
4257
4258 mii_pollstat(mii);
4259 ifmr->ifm_status = mii->mii_media_status;
4260 ifmr->ifm_active = (mii->mii_media_active & ~IFM_ETH_FMASK) |
4261 sc->bge_flowflags;
4262 }
4263
4264 static int
4265 bge_ioctl(struct ifnet *ifp, u_long command, void *data)
4266 {
4267 struct bge_softc *sc = ifp->if_softc;
4268 struct ifreq *ifr = (struct ifreq *) data;
4269 int s, error = 0;
4270 struct mii_data *mii;
4271
4272 s = splnet();
4273
4274 switch (command) {
4275 case SIOCSIFFLAGS:
4276 if ((error = ifioctl_common(ifp, command, data)) != 0)
4277 break;
4278 if (ifp->if_flags & IFF_UP) {
4279 /*
4280 * If only the state of the PROMISC flag changed,
4281 * then just use the 'set promisc mode' command
4282 * instead of reinitializing the entire NIC. Doing
4283 * a full re-init means reloading the firmware and
4284 * waiting for it to start up, which may take a
4285 * second or two.
4286 */
4287 if (ifp->if_flags & IFF_RUNNING &&
4288 ifp->if_flags & IFF_PROMISC &&
4289 !(sc->bge_if_flags & IFF_PROMISC)) {
4290 BGE_SETBIT(sc, BGE_RX_MODE,
4291 BGE_RXMODE_RX_PROMISC);
4292 } else if (ifp->if_flags & IFF_RUNNING &&
4293 !(ifp->if_flags & IFF_PROMISC) &&
4294 sc->bge_if_flags & IFF_PROMISC) {
4295 BGE_CLRBIT(sc, BGE_RX_MODE,
4296 BGE_RXMODE_RX_PROMISC);
4297 } else if (!(sc->bge_if_flags & IFF_UP))
4298 bge_init(ifp);
4299 } else {
4300 if (ifp->if_flags & IFF_RUNNING)
4301 bge_stop(ifp, 1);
4302 }
4303 sc->bge_if_flags = ifp->if_flags;
4304 error = 0;
4305 break;
4306 case SIOCSIFMEDIA:
4307 /* XXX Flow control is not supported for 1000BASE-SX */
4308 if (sc->bge_flags & BGE_PHY_FIBER_TBI) {
4309 ifr->ifr_media &= ~IFM_ETH_FMASK;
4310 sc->bge_flowflags = 0;
4311 }
4312
4313 /* Flow control requires full-duplex mode. */
4314 if (IFM_SUBTYPE(ifr->ifr_media) == IFM_AUTO ||
4315 (ifr->ifr_media & IFM_FDX) == 0) {
4316 ifr->ifr_media &= ~IFM_ETH_FMASK;
4317 }
4318 if (IFM_SUBTYPE(ifr->ifr_media) != IFM_AUTO) {
4319 if ((ifr->ifr_media & IFM_ETH_FMASK) == IFM_FLOW) {
4320 /* We can do both TXPAUSE and RXPAUSE. */
4321 ifr->ifr_media |=
4322 IFM_ETH_TXPAUSE | IFM_ETH_RXPAUSE;
4323 }
4324 sc->bge_flowflags = ifr->ifr_media & IFM_ETH_FMASK;
4325 }
4326 /* FALLTHROUGH */
4327 case SIOCGIFMEDIA:
4328 if (sc->bge_flags & BGE_PHY_FIBER_TBI) {
4329 error = ifmedia_ioctl(ifp, ifr, &sc->bge_ifmedia,
4330 command);
4331 } else {
4332 mii = &sc->bge_mii;
4333 error = ifmedia_ioctl(ifp, ifr, &mii->mii_media,
4334 command);
4335 }
4336 break;
4337 default:
4338 if ((error = ether_ioctl(ifp, command, data)) != ENETRESET)
4339 break;
4340
4341 error = 0;
4342
4343 if (command != SIOCADDMULTI && command != SIOCDELMULTI)
4344 ;
4345 else if (ifp->if_flags & IFF_RUNNING)
4346 bge_setmulti(sc);
4347 break;
4348 }
4349
4350 splx(s);
4351
4352 return error;
4353 }
4354
4355 static void
4356 bge_watchdog(struct ifnet *ifp)
4357 {
4358 struct bge_softc *sc;
4359
4360 sc = ifp->if_softc;
4361
4362 aprint_error_dev(sc->bge_dev, "watchdog timeout -- resetting\n");
4363
4364 ifp->if_flags &= ~IFF_RUNNING;
4365 bge_init(ifp);
4366
4367 ifp->if_oerrors++;
4368 }
4369
4370 static void
4371 bge_stop_block(struct bge_softc *sc, bus_addr_t reg, uint32_t bit)
4372 {
4373 int i;
4374
4375 BGE_CLRBIT(sc, reg, bit);
4376
4377 for (i = 0; i < BGE_TIMEOUT; i++) {
4378 if ((CSR_READ_4(sc, reg) & bit) == 0)
4379 return;
4380 delay(100);
4381 if (sc->bge_flags & BGE_PCIE)
4382 DELAY(1000);
4383 }
4384
4385 /*
4386 * Doesn't print only when the register is BGE_SRS_MODE. It occurs
4387 * on some environment (and once after boot?)
4388 */
4389 if (reg != BGE_SRS_MODE)
4390 aprint_error_dev(sc->bge_dev,
4391 "block failed to stop: reg 0x%lx, bit 0x%08x\n",
4392 (u_long)reg, bit);
4393 }
4394
4395 /*
4396 * Stop the adapter and free any mbufs allocated to the
4397 * RX and TX lists.
4398 */
4399 static void
4400 bge_stop(struct ifnet *ifp, int disable)
4401 {
4402 struct bge_softc *sc = ifp->if_softc;
4403
4404 callout_stop(&sc->bge_timeout);
4405
4406 /*
4407 * Disable all of the receiver blocks
4408 */
4409 bge_stop_block(sc, BGE_RX_MODE, BGE_RXMODE_ENABLE);
4410 bge_stop_block(sc, BGE_RBDI_MODE, BGE_RBDIMODE_ENABLE);
4411 bge_stop_block(sc, BGE_RXLP_MODE, BGE_RXLPMODE_ENABLE);
4412 if (BGE_IS_5700_FAMILY(sc))
4413 bge_stop_block(sc, BGE_RXLS_MODE, BGE_RXLSMODE_ENABLE);
4414 bge_stop_block(sc, BGE_RDBDI_MODE, BGE_RBDIMODE_ENABLE);
4415 bge_stop_block(sc, BGE_RDC_MODE, BGE_RDCMODE_ENABLE);
4416 bge_stop_block(sc, BGE_RBDC_MODE, BGE_RBDCMODE_ENABLE);
4417
4418 /*
4419 * Disable all of the transmit blocks
4420 */
4421 bge_stop_block(sc, BGE_SRS_MODE, BGE_SRSMODE_ENABLE);
4422 bge_stop_block(sc, BGE_SBDI_MODE, BGE_SBDIMODE_ENABLE);
4423 bge_stop_block(sc, BGE_SDI_MODE, BGE_SDIMODE_ENABLE);
4424 bge_stop_block(sc, BGE_RDMA_MODE, BGE_RDMAMODE_ENABLE);
4425 bge_stop_block(sc, BGE_SDC_MODE, BGE_SDCMODE_ENABLE);
4426 if (BGE_IS_5700_FAMILY(sc))
4427 bge_stop_block(sc, BGE_DMAC_MODE, BGE_DMACMODE_ENABLE);
4428 bge_stop_block(sc, BGE_SBDC_MODE, BGE_SBDCMODE_ENABLE);
4429
4430 /*
4431 * Shut down all of the memory managers and related
4432 * state machines.
4433 */
4434 bge_stop_block(sc, BGE_HCC_MODE, BGE_HCCMODE_ENABLE);
4435 bge_stop_block(sc, BGE_WDMA_MODE, BGE_WDMAMODE_ENABLE);
4436 if (BGE_IS_5700_FAMILY(sc))
4437 bge_stop_block(sc, BGE_MBCF_MODE, BGE_MBCFMODE_ENABLE);
4438
4439 CSR_WRITE_4(sc, BGE_FTQ_RESET, 0xFFFFFFFF);
4440 CSR_WRITE_4(sc, BGE_FTQ_RESET, 0);
4441
4442 if (BGE_IS_5700_FAMILY(sc)) {
4443 bge_stop_block(sc, BGE_BMAN_MODE, BGE_BMANMODE_ENABLE);
4444 bge_stop_block(sc, BGE_MARB_MODE, BGE_MARBMODE_ENABLE);
4445 }
4446
4447 /* Disable host interrupts. */
4448 BGE_SETBIT(sc, BGE_PCI_MISC_CTL, BGE_PCIMISCCTL_MASK_PCI_INTR);
4449 bge_writembx(sc, BGE_MBX_IRQ0_LO, 1);
4450
4451 /*
4452 * Tell firmware we're shutting down.
4453 */
4454 BGE_CLRBIT(sc, BGE_MODE_CTL, BGE_MODECTL_STACKUP);
4455
4456 /* Free the RX lists. */
4457 bge_free_rx_ring_std(sc);
4458
4459 /* Free jumbo RX list. */
4460 if (BGE_IS_JUMBO_CAPABLE(sc))
4461 bge_free_rx_ring_jumbo(sc);
4462
4463 /* Free TX buffers. */
4464 bge_free_tx_ring(sc);
4465
4466 /*
4467 * Isolate/power down the PHY.
4468 */
4469 if (!(sc->bge_flags & BGE_PHY_FIBER_TBI))
4470 mii_down(&sc->bge_mii);
4471
4472 sc->bge_tx_saved_considx = BGE_TXCONS_UNSET;
4473
4474 /* Clear MAC's link state (PHY may still have link UP). */
4475 BGE_STS_CLRBIT(sc, BGE_STS_LINK);
4476
4477 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
4478 }
4479
4480 static void
4481 bge_link_upd(struct bge_softc *sc)
4482 {
4483 struct ifnet *ifp = &sc->ethercom.ec_if;
4484 struct mii_data *mii = &sc->bge_mii;
4485 uint32_t status;
4486 int link;
4487
4488 /* Clear 'pending link event' flag */
4489 BGE_STS_CLRBIT(sc, BGE_STS_LINK_EVT);
4490
4491 /*
4492 * Process link state changes.
4493 * Grrr. The link status word in the status block does
4494 * not work correctly on the BCM5700 rev AX and BX chips,
4495 * according to all available information. Hence, we have
4496 * to enable MII interrupts in order to properly obtain
4497 * async link changes. Unfortunately, this also means that
4498 * we have to read the MAC status register to detect link
4499 * changes, thereby adding an additional register access to
4500 * the interrupt handler.
4501 */
4502
4503 if (BGE_ASICREV(sc->bge_chipid) == BGE_ASICREV_BCM5700) {
4504 status = CSR_READ_4(sc, BGE_MAC_STS);
4505 if (status & BGE_MACSTAT_MI_INTERRUPT) {
4506 mii_pollstat(mii);
4507
4508 if (!BGE_STS_BIT(sc, BGE_STS_LINK) &&
4509 mii->mii_media_status & IFM_ACTIVE &&
4510 IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE)
4511 BGE_STS_SETBIT(sc, BGE_STS_LINK);
4512 else if (BGE_STS_BIT(sc, BGE_STS_LINK) &&
4513 (!(mii->mii_media_status & IFM_ACTIVE) ||
4514 IFM_SUBTYPE(mii->mii_media_active) == IFM_NONE))
4515 BGE_STS_CLRBIT(sc, BGE_STS_LINK);
4516
4517 /* Clear the interrupt */
4518 CSR_WRITE_4(sc, BGE_MAC_EVT_ENB,
4519 BGE_EVTENB_MI_INTERRUPT);
4520 bge_miibus_readreg(sc->bge_dev, 1, BRGPHY_MII_ISR);
4521 bge_miibus_writereg(sc->bge_dev, 1, BRGPHY_MII_IMR,
4522 BRGPHY_INTRS);
4523 }
4524 return;
4525 }
4526
4527 if (sc->bge_flags & BGE_PHY_FIBER_TBI) {
4528 status = CSR_READ_4(sc, BGE_MAC_STS);
4529 if (status & BGE_MACSTAT_TBI_PCS_SYNCHED) {
4530 if (!BGE_STS_BIT(sc, BGE_STS_LINK)) {
4531 BGE_STS_SETBIT(sc, BGE_STS_LINK);
4532 if (BGE_ASICREV(sc->bge_chipid) == BGE_ASICREV_BCM5704)
4533 BGE_CLRBIT(sc, BGE_MAC_MODE,
4534 BGE_MACMODE_TBI_SEND_CFGS);
4535 CSR_WRITE_4(sc, BGE_MAC_STS, 0xFFFFFFFF);
4536 if_link_state_change(ifp, LINK_STATE_UP);
4537 }
4538 } else if (BGE_STS_BIT(sc, BGE_STS_LINK)) {
4539 BGE_STS_CLRBIT(sc, BGE_STS_LINK);
4540 if_link_state_change(ifp, LINK_STATE_DOWN);
4541 }
4542 /*
4543 * Discard link events for MII/GMII cards if MI auto-polling disabled.
4544 * This should not happen since mii callouts are locked now, but
4545 * we keep this check for debug.
4546 */
4547 } else if (BGE_STS_BIT(sc, BGE_STS_AUTOPOLL)) {
4548 /*
4549 * Some broken BCM chips have BGE_STATFLAG_LINKSTATE_CHANGED
4550 * bit in status word always set. Workaround this bug by
4551 * reading PHY link status directly.
4552 */
4553 link = (CSR_READ_4(sc, BGE_MI_STS) & BGE_MISTS_LINK)?
4554 BGE_STS_LINK : 0;
4555
4556 if (BGE_STS_BIT(sc, BGE_STS_LINK) != link) {
4557 mii_pollstat(mii);
4558
4559 if (!BGE_STS_BIT(sc, BGE_STS_LINK) &&
4560 mii->mii_media_status & IFM_ACTIVE &&
4561 IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE)
4562 BGE_STS_SETBIT(sc, BGE_STS_LINK);
4563 else if (BGE_STS_BIT(sc, BGE_STS_LINK) &&
4564 (!(mii->mii_media_status & IFM_ACTIVE) ||
4565 IFM_SUBTYPE(mii->mii_media_active) == IFM_NONE))
4566 BGE_STS_CLRBIT(sc, BGE_STS_LINK);
4567 }
4568 }
4569
4570 /* Clear the attention */
4571 CSR_WRITE_4(sc, BGE_MAC_STS, BGE_MACSTAT_SYNC_CHANGED|
4572 BGE_MACSTAT_CFG_CHANGED|BGE_MACSTAT_MI_COMPLETE|
4573 BGE_MACSTAT_LINK_CHANGED);
4574 }
4575
4576 static int
4577 sysctl_bge_verify(SYSCTLFN_ARGS)
4578 {
4579 int error, t;
4580 struct sysctlnode node;
4581
4582 node = *rnode;
4583 t = *(int*)rnode->sysctl_data;
4584 node.sysctl_data = &t;
4585 error = sysctl_lookup(SYSCTLFN_CALL(&node));
4586 if (error || newp == NULL)
4587 return error;
4588
4589 #if 0
4590 DPRINTF2(("%s: t = %d, nodenum = %d, rnodenum = %d\n", __func__, t,
4591 node.sysctl_num, rnode->sysctl_num));
4592 #endif
4593
4594 if (node.sysctl_num == bge_rxthresh_nodenum) {
4595 if (t < 0 || t >= NBGE_RX_THRESH)
4596 return EINVAL;
4597 bge_update_all_threshes(t);
4598 } else
4599 return EINVAL;
4600
4601 *(int*)rnode->sysctl_data = t;
4602
4603 return 0;
4604 }
4605
4606 /*
4607 * Set up sysctl(3) MIB, hw.bge.*.
4608 *
4609 * TBD condition SYSCTL_PERMANENT on being an LKM or not
4610 */
4611 SYSCTL_SETUP(sysctl_bge, "sysctl bge subtree setup")
4612 {
4613 int rc, bge_root_num;
4614 const struct sysctlnode *node;
4615
4616 if ((rc = sysctl_createv(clog, 0, NULL, NULL,
4617 CTLFLAG_PERMANENT, CTLTYPE_NODE, "hw", NULL,
4618 NULL, 0, NULL, 0, CTL_HW, CTL_EOL)) != 0) {
4619 goto err;
4620 }
4621
4622 if ((rc = sysctl_createv(clog, 0, NULL, &node,
4623 CTLFLAG_PERMANENT, CTLTYPE_NODE, "bge",
4624 SYSCTL_DESCR("BGE interface controls"),
4625 NULL, 0, NULL, 0, CTL_HW, CTL_CREATE, CTL_EOL)) != 0) {
4626 goto err;
4627 }
4628
4629 bge_root_num = node->sysctl_num;
4630
4631 /* BGE Rx interrupt mitigation level */
4632 if ((rc = sysctl_createv(clog, 0, NULL, &node,
4633 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
4634 CTLTYPE_INT, "rx_lvl",
4635 SYSCTL_DESCR("BGE receive interrupt mitigation level"),
4636 sysctl_bge_verify, 0,
4637 &bge_rx_thresh_lvl,
4638 0, CTL_HW, bge_root_num, CTL_CREATE,
4639 CTL_EOL)) != 0) {
4640 goto err;
4641 }
4642
4643 bge_rxthresh_nodenum = node->sysctl_num;
4644
4645 return;
4646
4647 err:
4648 aprint_error("%s: sysctl_createv failed (rc = %d)\n", __func__, rc);
4649 }
4650
4651 #ifdef BGE_DEBUG
4652 void
4653 bge_debug_info(struct bge_softc *sc)
4654 {
4655
4656 printf("Hardware Flags:\n");
4657 if (BGE_IS_5755_PLUS(sc))
4658 printf(" - 5755 Plus\n");
4659 if (BGE_IS_5750_OR_BEYOND(sc))
4660 printf(" - 5750 Plus\n");
4661 if (BGE_IS_5705_PLUS(sc))
4662 printf(" - 5705 Plus\n");
4663 if (BGE_IS_5714_FAMILY(sc))
4664 printf(" - 5714 Family\n");
4665 if (BGE_IS_5700_FAMILY(sc))
4666 printf(" - 5700 Family\n");
4667 if (sc->bge_flags & BGE_IS_5788)
4668 printf(" - 5788\n");
4669 if (sc->bge_flags & BGE_JUMBO_CAPABLE)
4670 printf(" - Supports Jumbo Frames\n");
4671 if (sc->bge_flags & BGE_NO_EEPROM)
4672 printf(" - No EEPROM\n");
4673 if (sc->bge_flags & BGE_PCIX)
4674 printf(" - PCI-X Bus\n");
4675 if (sc->bge_flags & BGE_PCIE)
4676 printf(" - PCI Express Bus\n");
4677 if (sc->bge_flags & BGE_NO_3LED)
4678 printf(" - No 3 LEDs\n");
4679 if (sc->bge_flags & BGE_RX_ALIGNBUG)
4680 printf(" - RX Alignment Bug\n");
4681 if (sc->bge_flags & BGE_TSO)
4682 printf(" - TSO\n");
4683 }
4684 #endif /* BGE_DEBUG */
4685
4686 static int
4687 bge_get_eaddr_fw(struct bge_softc *sc, uint8_t ether_addr[])
4688 {
4689 prop_dictionary_t dict;
4690 prop_data_t ea;
4691
4692 if ((sc->bge_flags & BGE_NO_EEPROM) == 0)
4693 return 1;
4694
4695 dict = device_properties(sc->bge_dev);
4696 ea = prop_dictionary_get(dict, "mac-address");
4697 if (ea != NULL) {
4698 KASSERT(prop_object_type(ea) == PROP_TYPE_DATA);
4699 KASSERT(prop_data_size(ea) == ETHER_ADDR_LEN);
4700 memcpy(ether_addr, prop_data_data_nocopy(ea), ETHER_ADDR_LEN);
4701 return 0;
4702 }
4703
4704 return 1;
4705 }
4706
4707 static int
4708 bge_get_eaddr_mem(struct bge_softc *sc, uint8_t ether_addr[])
4709 {
4710 uint32_t mac_addr;
4711
4712 mac_addr = bge_readmem_ind(sc, 0x0c14);
4713 if ((mac_addr >> 16) == 0x484b) {
4714 ether_addr[0] = (uint8_t)(mac_addr >> 8);
4715 ether_addr[1] = (uint8_t)mac_addr;
4716 mac_addr = bge_readmem_ind(sc, 0x0c18);
4717 ether_addr[2] = (uint8_t)(mac_addr >> 24);
4718 ether_addr[3] = (uint8_t)(mac_addr >> 16);
4719 ether_addr[4] = (uint8_t)(mac_addr >> 8);
4720 ether_addr[5] = (uint8_t)mac_addr;
4721 return 0;
4722 }
4723 return 1;
4724 }
4725
4726 static int
4727 bge_get_eaddr_nvram(struct bge_softc *sc, uint8_t ether_addr[])
4728 {
4729 int mac_offset = BGE_EE_MAC_OFFSET;
4730
4731 if (BGE_ASICREV(sc->bge_chipid) == BGE_ASICREV_BCM5906) {
4732 mac_offset = BGE_EE_MAC_OFFSET_5906;
4733 }
4734
4735 return (bge_read_nvram(sc, ether_addr, mac_offset + 2,
4736 ETHER_ADDR_LEN));
4737 }
4738
4739 static int
4740 bge_get_eaddr_eeprom(struct bge_softc *sc, uint8_t ether_addr[])
4741 {
4742
4743 if (BGE_ASICREV(sc->bge_chipid) == BGE_ASICREV_BCM5906)
4744 return 1;
4745
4746 return (bge_read_eeprom(sc, ether_addr, BGE_EE_MAC_OFFSET + 2,
4747 ETHER_ADDR_LEN));
4748 }
4749
4750 static int
4751 bge_get_eaddr(struct bge_softc *sc, uint8_t eaddr[])
4752 {
4753 static const bge_eaddr_fcn_t bge_eaddr_funcs[] = {
4754 /* NOTE: Order is critical */
4755 bge_get_eaddr_fw,
4756 bge_get_eaddr_mem,
4757 bge_get_eaddr_nvram,
4758 bge_get_eaddr_eeprom,
4759 NULL
4760 };
4761 const bge_eaddr_fcn_t *func;
4762
4763 for (func = bge_eaddr_funcs; *func != NULL; ++func) {
4764 if ((*func)(sc, eaddr) == 0)
4765 break;
4766 }
4767 return (*func == NULL ? ENXIO : 0);
4768 }
4769