2 * Copyright (c) 2006-2007 Broadcom Corporation
3 * David Christensen <davidch@broadcom.com>. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. Neither the name of Broadcom Corporation nor the name of its contributors
15 * may be used to endorse or promote products derived from this software
16 * without specific prior written consent.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS'
19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
22 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
28 * THE POSSIBILITY OF SUCH DAMAGE.
30 * $FreeBSD: src/sys/dev/bce/if_bce.c,v 1.31 2007/05/16 23:34:11 davidch Exp $
34 * The following controllers are supported by this driver:
42 * The following controllers are not supported by this driver:
48 * BCM5709S A0, A1, B0, B1, B2, C0
51 * Note about MSI-X on 5709/5716:
52 * - 9 MSI-X vectors are supported.
53 * - MSI-X vectors, RX/TX rings and status blocks' association
55 * o The first RX ring and the first TX ring use the first
57 * o The first MSI-X vector is associated with the first
59 * o The second RX ring and the second TX ring use the second
61 * o The second MSI-X vector is associated with the second
65 * - Status blocks must reside in physically contiguous memory
66 * and each status block consumes 128bytes. In addition to
67 * this, the memory for the status blocks is aligned on 128bytes
68 * in this driver. (see bce_dma_alloc() and HC_CONFIG)
69 * - Each status block has its own coalesce parameters, which also
70 * serve as the related MSI-X vector's interrupt moderation
71 * parameters. (see bce_coal_change())
75 #include "opt_ifpoll.h"
77 #include <sys/param.h>
79 #include <sys/endian.h>
80 #include <sys/kernel.h>
81 #include <sys/interrupt.h>
83 #include <sys/malloc.h>
84 #include <sys/queue.h>
86 #include <sys/serialize.h>
87 #include <sys/socket.h>
88 #include <sys/sockio.h>
89 #include <sys/sysctl.h>
91 #include <netinet/ip.h>
92 #include <netinet/tcp.h>
95 #include <net/ethernet.h>
97 #include <net/if_arp.h>
98 #include <net/if_dl.h>
99 #include <net/if_media.h>
100 #include <net/if_poll.h>
101 #include <net/if_types.h>
102 #include <net/ifq_var.h>
103 #include <net/toeplitz.h>
104 #include <net/toeplitz2.h>
105 #include <net/vlan/if_vlan_var.h>
106 #include <net/vlan/if_vlan_ether.h>
108 #include <dev/netif/mii_layer/mii.h>
109 #include <dev/netif/mii_layer/miivar.h>
110 #include <dev/netif/mii_layer/brgphyreg.h>
112 #include <bus/pci/pcireg.h>
113 #include <bus/pci/pcivar.h>
115 #include "miibus_if.h"
117 #include <dev/netif/bce/if_bcereg.h>
118 #include <dev/netif/bce/if_bcefw.h>
120 #define BCE_MSI_CKINTVL ((10 * hz) / 1000) /* 10ms */
123 #define BCE_RSS_DPRINTF(sc, lvl, fmt, ...) \
125 if (sc->rss_debug >= lvl) \
126 if_printf(&sc->arpcom.ac_if, fmt, __VA_ARGS__); \
128 #else /* !BCE_RSS_DEBUG */
129 #define BCE_RSS_DPRINTF(sc, lvl, fmt, ...) ((void)0)
130 #endif /* BCE_RSS_DEBUG */
132 /****************************************************************************/
133 /* PCI Device ID Table */
135 /* Used by bce_probe() to identify the devices supported by this driver. */
136 /****************************************************************************/
137 #define BCE_DEVDESC_MAX 64
139 static struct bce_type bce_devs[] = {
140 /* BCM5706C Controllers and OEM boards. */
141 { BRCM_VENDORID, BRCM_DEVICEID_BCM5706, HP_VENDORID, 0x3101,
142 "HP NC370T Multifunction Gigabit Server Adapter" },
143 { BRCM_VENDORID, BRCM_DEVICEID_BCM5706, HP_VENDORID, 0x3106,
144 "HP NC370i Multifunction Gigabit Server Adapter" },
145 { BRCM_VENDORID, BRCM_DEVICEID_BCM5706, HP_VENDORID, 0x3070,
146 "HP NC380T PCIe DP Multifunc Gig Server Adapter" },
147 { BRCM_VENDORID, BRCM_DEVICEID_BCM5706, HP_VENDORID, 0x1709,
148 "HP NC371i Multifunction Gigabit Server Adapter" },
149 { BRCM_VENDORID, BRCM_DEVICEID_BCM5706, PCI_ANY_ID, PCI_ANY_ID,
150 "Broadcom NetXtreme II BCM5706 1000Base-T" },
152 /* BCM5706S controllers and OEM boards. */
153 { BRCM_VENDORID, BRCM_DEVICEID_BCM5706S, HP_VENDORID, 0x3102,
154 "HP NC370F Multifunction Gigabit Server Adapter" },
155 { BRCM_VENDORID, BRCM_DEVICEID_BCM5706S, PCI_ANY_ID, PCI_ANY_ID,
156 "Broadcom NetXtreme II BCM5706 1000Base-SX" },
158 /* BCM5708C controllers and OEM boards. */
159 { BRCM_VENDORID, BRCM_DEVICEID_BCM5708, HP_VENDORID, 0x7037,
160 "HP NC373T PCIe Multifunction Gig Server Adapter" },
161 { BRCM_VENDORID, BRCM_DEVICEID_BCM5708, HP_VENDORID, 0x7038,
162 "HP NC373i Multifunction Gigabit Server Adapter" },
163 { BRCM_VENDORID, BRCM_DEVICEID_BCM5708, HP_VENDORID, 0x7045,
164 "HP NC374m PCIe Multifunction Adapter" },
165 { BRCM_VENDORID, BRCM_DEVICEID_BCM5708, PCI_ANY_ID, PCI_ANY_ID,
166 "Broadcom NetXtreme II BCM5708 1000Base-T" },
168 /* BCM5708S controllers and OEM boards. */
169 { BRCM_VENDORID, BRCM_DEVICEID_BCM5708S, HP_VENDORID, 0x1706,
170 "HP NC373m Multifunction Gigabit Server Adapter" },
171 { BRCM_VENDORID, BRCM_DEVICEID_BCM5708S, HP_VENDORID, 0x703b,
172 "HP NC373i Multifunction Gigabit Server Adapter" },
173 { BRCM_VENDORID, BRCM_DEVICEID_BCM5708S, HP_VENDORID, 0x703d,
174 "HP NC373F PCIe Multifunc Giga Server Adapter" },
175 { BRCM_VENDORID, BRCM_DEVICEID_BCM5708S, PCI_ANY_ID, PCI_ANY_ID,
176 "Broadcom NetXtreme II BCM5708S 1000Base-T" },
178 /* BCM5709C controllers and OEM boards. */
179 { BRCM_VENDORID, BRCM_DEVICEID_BCM5709, HP_VENDORID, 0x7055,
180 "HP NC382i DP Multifunction Gigabit Server Adapter" },
181 { BRCM_VENDORID, BRCM_DEVICEID_BCM5709, HP_VENDORID, 0x7059,
182 "HP NC382T PCIe DP Multifunction Gigabit Server Adapter" },
183 { BRCM_VENDORID, BRCM_DEVICEID_BCM5709, PCI_ANY_ID, PCI_ANY_ID,
184 "Broadcom NetXtreme II BCM5709 1000Base-T" },
186 /* BCM5709S controllers and OEM boards. */
187 { BRCM_VENDORID, BRCM_DEVICEID_BCM5709S, HP_VENDORID, 0x171d,
188 "HP NC382m DP 1GbE Multifunction BL-c Adapter" },
189 { BRCM_VENDORID, BRCM_DEVICEID_BCM5709S, HP_VENDORID, 0x7056,
190 "HP NC382i DP Multifunction Gigabit Server Adapter" },
191 { BRCM_VENDORID, BRCM_DEVICEID_BCM5709S, PCI_ANY_ID, PCI_ANY_ID,
192 "Broadcom NetXtreme II BCM5709 1000Base-SX" },
194 /* BCM5716 controllers and OEM boards. */
195 { BRCM_VENDORID, BRCM_DEVICEID_BCM5716, PCI_ANY_ID, PCI_ANY_ID,
196 "Broadcom NetXtreme II BCM5716 1000Base-T" },
202 /****************************************************************************/
203 /* Supported Flash NVRAM device data. */
204 /****************************************************************************/
205 static const struct flash_spec flash_table[] =
207 #define BUFFERED_FLAGS (BCE_NV_BUFFERED | BCE_NV_TRANSLATE)
208 #define NONBUFFERED_FLAGS (BCE_NV_WREN)
211 {0x00000000, 0x40830380, 0x009f0081, 0xa184a053, 0xaf000400,
212 BUFFERED_FLAGS, SEEPROM_PAGE_BITS, SEEPROM_PAGE_SIZE,
213 SEEPROM_BYTE_ADDR_MASK, SEEPROM_TOTAL_SIZE,
215 /* Expansion entry 0001 */
216 {0x08000002, 0x4b808201, 0x00050081, 0x03840253, 0xaf020406,
217 NONBUFFERED_FLAGS, SAIFUN_FLASH_PAGE_BITS, SAIFUN_FLASH_PAGE_SIZE,
218 SAIFUN_FLASH_BYTE_ADDR_MASK, 0,
220 /* Saifun SA25F010 (non-buffered flash) */
221 /* strap, cfg1, & write1 need updates */
222 {0x04000001, 0x47808201, 0x00050081, 0x03840253, 0xaf020406,
223 NONBUFFERED_FLAGS, SAIFUN_FLASH_PAGE_BITS, SAIFUN_FLASH_PAGE_SIZE,
224 SAIFUN_FLASH_BYTE_ADDR_MASK, SAIFUN_FLASH_BASE_TOTAL_SIZE*2,
225 "Non-buffered flash (128kB)"},
226 /* Saifun SA25F020 (non-buffered flash) */
227 /* strap, cfg1, & write1 need updates */
228 {0x0c000003, 0x4f808201, 0x00050081, 0x03840253, 0xaf020406,
229 NONBUFFERED_FLAGS, SAIFUN_FLASH_PAGE_BITS, SAIFUN_FLASH_PAGE_SIZE,
230 SAIFUN_FLASH_BYTE_ADDR_MASK, SAIFUN_FLASH_BASE_TOTAL_SIZE*4,
231 "Non-buffered flash (256kB)"},
232 /* Expansion entry 0100 */
233 {0x11000000, 0x53808201, 0x00050081, 0x03840253, 0xaf020406,
234 NONBUFFERED_FLAGS, SAIFUN_FLASH_PAGE_BITS, SAIFUN_FLASH_PAGE_SIZE,
235 SAIFUN_FLASH_BYTE_ADDR_MASK, 0,
237 /* Entry 0101: ST M45PE10 (non-buffered flash, TetonII B0) */
238 {0x19000002, 0x5b808201, 0x000500db, 0x03840253, 0xaf020406,
239 NONBUFFERED_FLAGS, ST_MICRO_FLASH_PAGE_BITS, ST_MICRO_FLASH_PAGE_SIZE,
240 ST_MICRO_FLASH_BYTE_ADDR_MASK, ST_MICRO_FLASH_BASE_TOTAL_SIZE*2,
241 "Entry 0101: ST M45PE10 (128kB non-bufferred)"},
242 /* Entry 0110: ST M45PE20 (non-buffered flash)*/
243 {0x15000001, 0x57808201, 0x000500db, 0x03840253, 0xaf020406,
244 NONBUFFERED_FLAGS, ST_MICRO_FLASH_PAGE_BITS, ST_MICRO_FLASH_PAGE_SIZE,
245 ST_MICRO_FLASH_BYTE_ADDR_MASK, ST_MICRO_FLASH_BASE_TOTAL_SIZE*4,
246 "Entry 0110: ST M45PE20 (256kB non-bufferred)"},
247 /* Saifun SA25F005 (non-buffered flash) */
248 /* strap, cfg1, & write1 need updates */
249 {0x1d000003, 0x5f808201, 0x00050081, 0x03840253, 0xaf020406,
250 NONBUFFERED_FLAGS, SAIFUN_FLASH_PAGE_BITS, SAIFUN_FLASH_PAGE_SIZE,
251 SAIFUN_FLASH_BYTE_ADDR_MASK, SAIFUN_FLASH_BASE_TOTAL_SIZE,
252 "Non-buffered flash (64kB)"},
254 {0x22000000, 0x62808380, 0x009f0081, 0xa184a053, 0xaf000400,
255 BUFFERED_FLAGS, SEEPROM_PAGE_BITS, SEEPROM_PAGE_SIZE,
256 SEEPROM_BYTE_ADDR_MASK, SEEPROM_TOTAL_SIZE,
258 /* Expansion entry 1001 */
259 {0x2a000002, 0x6b808201, 0x00050081, 0x03840253, 0xaf020406,
260 NONBUFFERED_FLAGS, SAIFUN_FLASH_PAGE_BITS, SAIFUN_FLASH_PAGE_SIZE,
261 SAIFUN_FLASH_BYTE_ADDR_MASK, 0,
263 /* Expansion entry 1010 */
264 {0x26000001, 0x67808201, 0x00050081, 0x03840253, 0xaf020406,
265 NONBUFFERED_FLAGS, SAIFUN_FLASH_PAGE_BITS, SAIFUN_FLASH_PAGE_SIZE,
266 SAIFUN_FLASH_BYTE_ADDR_MASK, 0,
268 /* ATMEL AT45DB011B (buffered flash) */
269 {0x2e000003, 0x6e808273, 0x00570081, 0x68848353, 0xaf000400,
270 BUFFERED_FLAGS, BUFFERED_FLASH_PAGE_BITS, BUFFERED_FLASH_PAGE_SIZE,
271 BUFFERED_FLASH_BYTE_ADDR_MASK, BUFFERED_FLASH_TOTAL_SIZE,
272 "Buffered flash (128kB)"},
273 /* Expansion entry 1100 */
274 {0x33000000, 0x73808201, 0x00050081, 0x03840253, 0xaf020406,
275 NONBUFFERED_FLAGS, SAIFUN_FLASH_PAGE_BITS, SAIFUN_FLASH_PAGE_SIZE,
276 SAIFUN_FLASH_BYTE_ADDR_MASK, 0,
278 /* Expansion entry 1101 */
279 {0x3b000002, 0x7b808201, 0x00050081, 0x03840253, 0xaf020406,
280 NONBUFFERED_FLAGS, SAIFUN_FLASH_PAGE_BITS, SAIFUN_FLASH_PAGE_SIZE,
281 SAIFUN_FLASH_BYTE_ADDR_MASK, 0,
283 /* Ateml Expansion entry 1110 */
284 {0x37000001, 0x76808273, 0x00570081, 0x68848353, 0xaf000400,
285 BUFFERED_FLAGS, BUFFERED_FLASH_PAGE_BITS, BUFFERED_FLASH_PAGE_SIZE,
286 BUFFERED_FLASH_BYTE_ADDR_MASK, 0,
287 "Entry 1110 (Atmel)"},
288 /* ATMEL AT45DB021B (buffered flash) */
289 {0x3f000003, 0x7e808273, 0x00570081, 0x68848353, 0xaf000400,
290 BUFFERED_FLAGS, BUFFERED_FLASH_PAGE_BITS, BUFFERED_FLASH_PAGE_SIZE,
291 BUFFERED_FLASH_BYTE_ADDR_MASK, BUFFERED_FLASH_TOTAL_SIZE*2,
292 "Buffered flash (256kB)"},
296 * The BCM5709 controllers transparently handle the
297 * differences between Atmel 264 byte pages and all
298 * flash devices which use 256 byte pages, so no
299 * logical-to-physical mapping is required in the
302 static struct flash_spec flash_5709 = {
303 .flags = BCE_NV_BUFFERED,
304 .page_bits = BCM5709_FLASH_PAGE_BITS,
305 .page_size = BCM5709_FLASH_PAGE_SIZE,
306 .addr_mask = BCM5709_FLASH_BYTE_ADDR_MASK,
307 .total_size = BUFFERED_FLASH_TOTAL_SIZE * 2,
308 .name = "5709/5716 buffered flash (256kB)",
312 /****************************************************************************/
313 /* DragonFly device entry points. */
314 /****************************************************************************/
315 static int bce_probe(device_t);
316 static int bce_attach(device_t);
317 static int bce_detach(device_t);
318 static void bce_shutdown(device_t);
319 static int bce_miibus_read_reg(device_t, int, int);
320 static int bce_miibus_write_reg(device_t, int, int, int);
321 static void bce_miibus_statchg(device_t);
323 /****************************************************************************/
324 /* BCE Register/Memory Access Routines */
325 /****************************************************************************/
326 static uint32_t bce_reg_rd_ind(struct bce_softc *, uint32_t);
327 static void bce_reg_wr_ind(struct bce_softc *, uint32_t, uint32_t);
328 static void bce_shmem_wr(struct bce_softc *, uint32_t, uint32_t);
329 static uint32_t bce_shmem_rd(struct bce_softc *, u32);
330 static void bce_ctx_wr(struct bce_softc *, uint32_t, uint32_t, uint32_t);
332 /****************************************************************************/
333 /* BCE NVRAM Access Routines */
334 /****************************************************************************/
335 static int bce_acquire_nvram_lock(struct bce_softc *);
336 static int bce_release_nvram_lock(struct bce_softc *);
337 static void bce_enable_nvram_access(struct bce_softc *);
338 static void bce_disable_nvram_access(struct bce_softc *);
339 static int bce_nvram_read_dword(struct bce_softc *, uint32_t, uint8_t *,
341 static int bce_init_nvram(struct bce_softc *);
342 static int bce_nvram_read(struct bce_softc *, uint32_t, uint8_t *, int);
343 static int bce_nvram_test(struct bce_softc *);
345 /****************************************************************************/
346 /* BCE DMA Allocate/Free Routines */
347 /****************************************************************************/
348 static int bce_dma_alloc(struct bce_softc *);
349 static void bce_dma_free(struct bce_softc *);
350 static void bce_dma_map_addr(void *, bus_dma_segment_t *, int, int);
352 /****************************************************************************/
353 /* BCE Firmware Synchronization and Load */
354 /****************************************************************************/
355 static int bce_fw_sync(struct bce_softc *, uint32_t);
356 static void bce_load_rv2p_fw(struct bce_softc *, uint32_t *,
358 static void bce_load_cpu_fw(struct bce_softc *, struct cpu_reg *,
360 static void bce_start_cpu(struct bce_softc *, struct cpu_reg *);
361 static void bce_halt_cpu(struct bce_softc *, struct cpu_reg *);
362 static void bce_start_rxp_cpu(struct bce_softc *);
363 static void bce_init_rxp_cpu(struct bce_softc *);
364 static void bce_init_txp_cpu(struct bce_softc *);
365 static void bce_init_tpat_cpu(struct bce_softc *);
366 static void bce_init_cp_cpu(struct bce_softc *);
367 static void bce_init_com_cpu(struct bce_softc *);
368 static void bce_init_cpus(struct bce_softc *);
369 static void bce_setup_msix_table(struct bce_softc *);
370 static void bce_init_rss(struct bce_softc *);
372 static void bce_stop(struct bce_softc *);
373 static int bce_reset(struct bce_softc *, uint32_t);
374 static int bce_chipinit(struct bce_softc *);
375 static int bce_blockinit(struct bce_softc *);
376 static void bce_probe_pci_caps(struct bce_softc *);
377 static void bce_print_adapter_info(struct bce_softc *);
378 static void bce_get_media(struct bce_softc *);
379 static void bce_mgmt_init(struct bce_softc *);
380 static int bce_init_ctx(struct bce_softc *);
381 static void bce_get_mac_addr(struct bce_softc *);
382 static void bce_set_mac_addr(struct bce_softc *);
383 static void bce_set_rx_mode(struct bce_softc *);
384 static void bce_coal_change(struct bce_softc *);
385 static void bce_npoll_coal_change(struct bce_softc *);
386 static void bce_setup_serialize(struct bce_softc *);
387 static void bce_serialize_skipmain(struct bce_softc *);
388 static void bce_deserialize_skipmain(struct bce_softc *);
389 static void bce_set_timer_cpuid(struct bce_softc *, boolean_t);
390 static int bce_alloc_intr(struct bce_softc *);
391 static void bce_free_intr(struct bce_softc *);
392 static void bce_try_alloc_msix(struct bce_softc *);
393 static void bce_free_msix(struct bce_softc *, boolean_t);
394 static void bce_setup_ring_cnt(struct bce_softc *);
395 static int bce_setup_intr(struct bce_softc *);
396 static void bce_teardown_intr(struct bce_softc *);
397 static int bce_setup_msix(struct bce_softc *);
398 static void bce_teardown_msix(struct bce_softc *, int);
400 static int bce_create_tx_ring(struct bce_tx_ring *);
401 static void bce_destroy_tx_ring(struct bce_tx_ring *);
402 static void bce_init_tx_context(struct bce_tx_ring *);
403 static int bce_init_tx_chain(struct bce_tx_ring *);
404 static void bce_free_tx_chain(struct bce_tx_ring *);
405 static void bce_xmit(struct bce_tx_ring *);
406 static int bce_encap(struct bce_tx_ring *, struct mbuf **, int *);
407 static int bce_tso_setup(struct bce_tx_ring *, struct mbuf **,
408 uint16_t *, uint16_t *);
410 static int bce_create_rx_ring(struct bce_rx_ring *);
411 static void bce_destroy_rx_ring(struct bce_rx_ring *);
412 static void bce_init_rx_context(struct bce_rx_ring *);
413 static int bce_init_rx_chain(struct bce_rx_ring *);
414 static void bce_free_rx_chain(struct bce_rx_ring *);
415 static int bce_newbuf_std(struct bce_rx_ring *, uint16_t *, uint16_t *,
417 static void bce_setup_rxdesc_std(struct bce_rx_ring *, uint16_t,
420 static void bce_start(struct ifnet *, struct ifaltq_subque *);
421 static int bce_ioctl(struct ifnet *, u_long, caddr_t, struct ucred *);
422 static void bce_watchdog(struct ifaltq_subque *);
423 static int bce_ifmedia_upd(struct ifnet *);
424 static void bce_ifmedia_sts(struct ifnet *, struct ifmediareq *);
425 static void bce_init(void *);
427 static void bce_npoll(struct ifnet *, struct ifpoll_info *);
428 static void bce_npoll_rx(struct ifnet *, void *, int);
429 static void bce_npoll_tx(struct ifnet *, void *, int);
430 static void bce_npoll_status(struct ifnet *);
431 static void bce_npoll_rx_pack(struct ifnet *, void *, int);
433 static void bce_serialize(struct ifnet *, enum ifnet_serialize);
434 static void bce_deserialize(struct ifnet *, enum ifnet_serialize);
435 static int bce_tryserialize(struct ifnet *, enum ifnet_serialize);
437 static void bce_serialize_assert(struct ifnet *, enum ifnet_serialize,
441 static void bce_intr(struct bce_softc *);
442 static void bce_intr_legacy(void *);
443 static void bce_intr_msi(void *);
444 static void bce_intr_msi_oneshot(void *);
445 static void bce_intr_msix_rxtx(void *);
446 static void bce_intr_msix_rx(void *);
447 static void bce_tx_intr(struct bce_tx_ring *, uint16_t);
448 static void bce_rx_intr(struct bce_rx_ring *, int, uint16_t);
449 static void bce_phy_intr(struct bce_softc *);
450 static void bce_disable_intr(struct bce_softc *);
451 static void bce_enable_intr(struct bce_softc *);
452 static void bce_reenable_intr(struct bce_rx_ring *);
453 static void bce_check_msi(void *);
455 static void bce_stats_update(struct bce_softc *);
456 static void bce_tick(void *);
457 static void bce_tick_serialized(struct bce_softc *);
458 static void bce_pulse(void *);
460 static void bce_add_sysctls(struct bce_softc *);
461 static int bce_sysctl_tx_bds_int(SYSCTL_HANDLER_ARGS);
462 static int bce_sysctl_tx_bds(SYSCTL_HANDLER_ARGS);
463 static int bce_sysctl_tx_ticks_int(SYSCTL_HANDLER_ARGS);
464 static int bce_sysctl_tx_ticks(SYSCTL_HANDLER_ARGS);
465 static int bce_sysctl_rx_bds_int(SYSCTL_HANDLER_ARGS);
466 static int bce_sysctl_rx_bds(SYSCTL_HANDLER_ARGS);
467 static int bce_sysctl_rx_ticks_int(SYSCTL_HANDLER_ARGS);
468 static int bce_sysctl_rx_ticks(SYSCTL_HANDLER_ARGS);
470 static int bce_sysctl_npoll_offset(SYSCTL_HANDLER_ARGS);
472 static int bce_sysctl_coal_change(SYSCTL_HANDLER_ARGS,
473 uint32_t *, uint32_t);
477 * Don't set bce_tx_ticks_int/bce_tx_ticks to 1023. Linux's bnx2
478 * takes 1023 as the TX ticks limit. However, using 1023 will
479 * cause 5708(B2) to generate extra interrupts (~2000/s) even when
480 * there is _no_ network activity on the NIC.
482 static uint32_t bce_tx_bds_int = 255; /* bcm: 20 */
483 static uint32_t bce_tx_bds = 255; /* bcm: 20 */
484 static uint32_t bce_tx_ticks_int = 1022; /* bcm: 80 */
485 static uint32_t bce_tx_ticks = 1022; /* bcm: 80 */
486 static uint32_t bce_rx_bds_int = 128; /* bcm: 6 */
487 static uint32_t bce_rx_bds = 0; /* bcm: 6 */
488 static uint32_t bce_rx_ticks_int = 150; /* bcm: 18 */
489 static uint32_t bce_rx_ticks = 150; /* bcm: 18 */
491 static int bce_tx_wreg = 8;
493 static int bce_msi_enable = 1;
494 static int bce_msix_enable = 1;
496 static int bce_rx_pages = RX_PAGES_DEFAULT;
497 static int bce_tx_pages = TX_PAGES_DEFAULT;
499 static int bce_rx_rings = 0; /* auto */
500 static int bce_tx_rings = 0; /* auto */
502 TUNABLE_INT("hw.bce.tx_bds_int", &bce_tx_bds_int);
503 TUNABLE_INT("hw.bce.tx_bds", &bce_tx_bds);
504 TUNABLE_INT("hw.bce.tx_ticks_int", &bce_tx_ticks_int);
505 TUNABLE_INT("hw.bce.tx_ticks", &bce_tx_ticks);
506 TUNABLE_INT("hw.bce.rx_bds_int", &bce_rx_bds_int);
507 TUNABLE_INT("hw.bce.rx_bds", &bce_rx_bds);
508 TUNABLE_INT("hw.bce.rx_ticks_int", &bce_rx_ticks_int);
509 TUNABLE_INT("hw.bce.rx_ticks", &bce_rx_ticks);
510 TUNABLE_INT("hw.bce.msi.enable", &bce_msi_enable);
511 TUNABLE_INT("hw.bce.msix.enable", &bce_msix_enable);
512 TUNABLE_INT("hw.bce.rx_pages", &bce_rx_pages);
513 TUNABLE_INT("hw.bce.tx_pages", &bce_tx_pages);
514 TUNABLE_INT("hw.bce.tx_wreg", &bce_tx_wreg);
515 TUNABLE_INT("hw.bce.tx_rings", &bce_tx_rings);
516 TUNABLE_INT("hw.bce.rx_rings", &bce_rx_rings);
518 /****************************************************************************/
519 /* DragonFly device dispatch table. */
520 /****************************************************************************/
521 static device_method_t bce_methods[] = {
522 /* Device interface */
523 DEVMETHOD(device_probe, bce_probe),
524 DEVMETHOD(device_attach, bce_attach),
525 DEVMETHOD(device_detach, bce_detach),
526 DEVMETHOD(device_shutdown, bce_shutdown),
529 DEVMETHOD(bus_print_child, bus_generic_print_child),
530 DEVMETHOD(bus_driver_added, bus_generic_driver_added),
533 DEVMETHOD(miibus_readreg, bce_miibus_read_reg),
534 DEVMETHOD(miibus_writereg, bce_miibus_write_reg),
535 DEVMETHOD(miibus_statchg, bce_miibus_statchg),
540 static driver_t bce_driver = {
543 sizeof(struct bce_softc)
546 static devclass_t bce_devclass;
549 DECLARE_DUMMY_MODULE(if_bce);
550 MODULE_DEPEND(bce, miibus, 1, 1, 1);
551 DRIVER_MODULE(if_bce, pci, bce_driver, bce_devclass, NULL, NULL);
552 DRIVER_MODULE(miibus, bce, miibus_driver, miibus_devclass, NULL, NULL);
555 /****************************************************************************/
556 /* Device probe function. */
558 /* Compares the device to the driver's list of supported devices and */
559 /* reports back to the OS whether this is the right driver for the device. */
562 /* BUS_PROBE_DEFAULT on success, positive value on failure. */
563 /****************************************************************************/
565 bce_probe(device_t dev)
568 uint16_t vid, did, svid, sdid;
570 /* Get the data for the device to be probed. */
571 vid = pci_get_vendor(dev);
572 did = pci_get_device(dev);
573 svid = pci_get_subvendor(dev);
574 sdid = pci_get_subdevice(dev);
576 /* Look through the list of known devices for a match. */
577 for (t = bce_devs; t->bce_name != NULL; ++t) {
578 if (vid == t->bce_vid && did == t->bce_did &&
579 (svid == t->bce_svid || t->bce_svid == PCI_ANY_ID) &&
580 (sdid == t->bce_sdid || t->bce_sdid == PCI_ANY_ID)) {
581 uint32_t revid = pci_read_config(dev, PCIR_REVID, 4);
584 descbuf = kmalloc(BCE_DEVDESC_MAX, M_TEMP, M_WAITOK);
586 /* Print out the device identity. */
587 ksnprintf(descbuf, BCE_DEVDESC_MAX, "%s (%c%d)",
589 ((revid & 0xf0) >> 4) + 'A', revid & 0xf);
591 device_set_desc_copy(dev, descbuf);
592 kfree(descbuf, M_TEMP);
600 /****************************************************************************/
601 /* PCI Capabilities Probe Function. */
603 /* Walks the PCI capabiites list for the device to find what features are */
608 /****************************************************************************/
610 bce_print_adapter_info(struct bce_softc *sc)
612 device_printf(sc->bce_dev, "ASIC (0x%08X); ", sc->bce_chipid);
614 kprintf("Rev (%c%d); ", ((BCE_CHIP_ID(sc) & 0xf000) >> 12) + 'A',
615 ((BCE_CHIP_ID(sc) & 0x0ff0) >> 4));
618 if (sc->bce_flags & BCE_PCIE_FLAG) {
619 kprintf("Bus (PCIe x%d, ", sc->link_width);
620 switch (sc->link_speed) {
622 kprintf("2.5Gbps); ");
628 kprintf("Unknown link speed); ");
632 kprintf("Bus (PCI%s, %s, %dMHz); ",
633 ((sc->bce_flags & BCE_PCIX_FLAG) ? "-X" : ""),
634 ((sc->bce_flags & BCE_PCI_32BIT_FLAG) ? "32-bit" : "64-bit"),
638 /* Firmware version and device features. */
639 kprintf("B/C (%s)", sc->bce_bc_ver);
641 if ((sc->bce_flags & BCE_MFW_ENABLE_FLAG) ||
642 (sc->bce_phy_flags & BCE_PHY_2_5G_CAPABLE_FLAG)) {
644 if (sc->bce_flags & BCE_MFW_ENABLE_FLAG)
645 kprintf("MFW[%s]", sc->bce_mfw_ver);
646 if (sc->bce_phy_flags & BCE_PHY_2_5G_CAPABLE_FLAG)
654 /****************************************************************************/
655 /* PCI Capabilities Probe Function. */
657 /* Walks the PCI capabiites list for the device to find what features are */
662 /****************************************************************************/
664 bce_probe_pci_caps(struct bce_softc *sc)
666 device_t dev = sc->bce_dev;
669 if (pci_is_pcix(dev))
670 sc->bce_cap_flags |= BCE_PCIX_CAPABLE_FLAG;
672 ptr = pci_get_pciecap_ptr(dev);
674 uint16_t link_status = pci_read_config(dev, ptr + 0x12, 2);
676 sc->link_speed = link_status & 0xf;
677 sc->link_width = (link_status >> 4) & 0x3f;
678 sc->bce_cap_flags |= BCE_PCIE_CAPABLE_FLAG;
679 sc->bce_flags |= BCE_PCIE_FLAG;
684 /****************************************************************************/
685 /* Device attach function. */
687 /* Allocates device resources, performs secondary chip identification, */
688 /* resets and initializes the hardware, and initializes driver instance */
692 /* 0 on success, positive value on failure. */
693 /****************************************************************************/
695 bce_attach(device_t dev)
697 struct bce_softc *sc = device_get_softc(dev);
698 struct ifnet *ifp = &sc->arpcom.ac_if;
702 struct mii_probe_args mii_args;
703 uintptr_t mii_priv = 0;
705 int offset, offset_def;
709 if_initname(ifp, device_get_name(dev), device_get_unit(dev));
711 lwkt_serialize_init(&sc->main_serialize);
712 for (i = 0; i < BCE_MSIX_MAX; ++i) {
713 struct bce_msix_data *msix = &sc->bce_msix[i];
715 msix->msix_cpuid = -1;
719 pci_enable_busmaster(dev);
721 bce_probe_pci_caps(sc);
723 /* Allocate PCI memory resources. */
725 sc->bce_res_mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
726 RF_ACTIVE | PCI_RF_DENSE);
727 if (sc->bce_res_mem == NULL) {
728 device_printf(dev, "PCI memory allocation failed\n");
731 sc->bce_btag = rman_get_bustag(sc->bce_res_mem);
732 sc->bce_bhandle = rman_get_bushandle(sc->bce_res_mem);
735 * Configure byte swap and enable indirect register access.
736 * Rely on CPU to do target byte swapping on big endian systems.
737 * Access to registers outside of PCI configurtion space are not
738 * valid until this is done.
740 pci_write_config(dev, BCE_PCICFG_MISC_CONFIG,
741 BCE_PCICFG_MISC_CONFIG_REG_WINDOW_ENA |
742 BCE_PCICFG_MISC_CONFIG_TARGET_MB_WORD_SWAP, 4);
744 /* Save ASIC revsion info. */
745 sc->bce_chipid = REG_RD(sc, BCE_MISC_ID);
747 /* Weed out any non-production controller revisions. */
748 switch (BCE_CHIP_ID(sc)) {
749 case BCE_CHIP_ID_5706_A0:
750 case BCE_CHIP_ID_5706_A1:
751 case BCE_CHIP_ID_5708_A0:
752 case BCE_CHIP_ID_5708_B0:
753 case BCE_CHIP_ID_5709_A0:
754 case BCE_CHIP_ID_5709_B0:
755 case BCE_CHIP_ID_5709_B1:
757 /* 5709C B2 seems to work fine */
758 case BCE_CHIP_ID_5709_B2:
760 device_printf(dev, "Unsupported chip id 0x%08x!\n",
766 mii_priv |= BRGPHY_FLAG_WIRESPEED;
767 if (BCE_CHIP_NUM(sc) == BCE_CHIP_NUM_5709) {
768 if (BCE_CHIP_REV(sc) == BCE_CHIP_REV_Ax ||
769 BCE_CHIP_REV(sc) == BCE_CHIP_REV_Bx)
770 mii_priv |= BRGPHY_FLAG_NO_EARLYDAC;
772 mii_priv |= BRGPHY_FLAG_BER_BUG;
776 * Find the base address for shared memory access.
777 * Newer versions of bootcode use a signature and offset
778 * while older versions use a fixed address.
780 val = REG_RD_IND(sc, BCE_SHM_HDR_SIGNATURE);
781 if ((val & BCE_SHM_HDR_SIGNATURE_SIG_MASK) ==
782 BCE_SHM_HDR_SIGNATURE_SIG) {
783 /* Multi-port devices use different offsets in shared memory. */
784 sc->bce_shmem_base = REG_RD_IND(sc,
785 BCE_SHM_HDR_ADDR_0 + (pci_get_function(sc->bce_dev) << 2));
787 sc->bce_shmem_base = HOST_VIEW_SHMEM_BASE;
790 /* Fetch the bootcode revision. */
791 val = bce_shmem_rd(sc, BCE_DEV_INFO_BC_REV);
792 for (i = 0, j = 0; i < 3; i++) {
796 num = (uint8_t)(val >> (24 - (i * 8)));
797 for (k = 100, skip0 = 1; k >= 1; num %= k, k /= 10) {
798 if (num >= k || !skip0 || k == 1) {
799 sc->bce_bc_ver[j++] = (num / k) + '0';
804 sc->bce_bc_ver[j++] = '.';
807 /* Check if any management firwmare is running. */
808 val = bce_shmem_rd(sc, BCE_PORT_FEATURE);
809 if (val & BCE_PORT_FEATURE_ASF_ENABLED) {
810 sc->bce_flags |= BCE_MFW_ENABLE_FLAG;
812 /* Allow time for firmware to enter the running state. */
813 for (i = 0; i < 30; i++) {
814 val = bce_shmem_rd(sc, BCE_BC_STATE_CONDITION);
815 if (val & BCE_CONDITION_MFW_RUN_MASK)
821 /* Check the current bootcode state. */
822 val = bce_shmem_rd(sc, BCE_BC_STATE_CONDITION) &
823 BCE_CONDITION_MFW_RUN_MASK;
824 if (val != BCE_CONDITION_MFW_RUN_UNKNOWN &&
825 val != BCE_CONDITION_MFW_RUN_NONE) {
826 uint32_t addr = bce_shmem_rd(sc, BCE_MFW_VER_PTR);
828 for (i = 0, j = 0; j < 3; j++) {
829 val = bce_reg_rd_ind(sc, addr + j * 4);
831 memcpy(&sc->bce_mfw_ver[i], &val, 4);
836 /* Get PCI bus information (speed and type). */
837 val = REG_RD(sc, BCE_PCICFG_MISC_STATUS);
838 if (val & BCE_PCICFG_MISC_STATUS_PCIX_DET) {
841 sc->bce_flags |= BCE_PCIX_FLAG;
843 clkreg = REG_RD(sc, BCE_PCICFG_PCI_CLOCK_CONTROL_BITS) &
844 BCE_PCICFG_PCI_CLOCK_CONTROL_BITS_PCI_CLK_SPD_DET;
846 case BCE_PCICFG_PCI_CLOCK_CONTROL_BITS_PCI_CLK_SPD_DET_133MHZ:
847 sc->bus_speed_mhz = 133;
850 case BCE_PCICFG_PCI_CLOCK_CONTROL_BITS_PCI_CLK_SPD_DET_95MHZ:
851 sc->bus_speed_mhz = 100;
854 case BCE_PCICFG_PCI_CLOCK_CONTROL_BITS_PCI_CLK_SPD_DET_66MHZ:
855 case BCE_PCICFG_PCI_CLOCK_CONTROL_BITS_PCI_CLK_SPD_DET_80MHZ:
856 sc->bus_speed_mhz = 66;
859 case BCE_PCICFG_PCI_CLOCK_CONTROL_BITS_PCI_CLK_SPD_DET_48MHZ:
860 case BCE_PCICFG_PCI_CLOCK_CONTROL_BITS_PCI_CLK_SPD_DET_55MHZ:
861 sc->bus_speed_mhz = 50;
864 case BCE_PCICFG_PCI_CLOCK_CONTROL_BITS_PCI_CLK_SPD_DET_LOW:
865 case BCE_PCICFG_PCI_CLOCK_CONTROL_BITS_PCI_CLK_SPD_DET_32MHZ:
866 case BCE_PCICFG_PCI_CLOCK_CONTROL_BITS_PCI_CLK_SPD_DET_38MHZ:
867 sc->bus_speed_mhz = 33;
871 if (val & BCE_PCICFG_MISC_STATUS_M66EN)
872 sc->bus_speed_mhz = 66;
874 sc->bus_speed_mhz = 33;
877 if (val & BCE_PCICFG_MISC_STATUS_32BIT_DET)
878 sc->bce_flags |= BCE_PCI_32BIT_FLAG;
880 /* Reset the controller. */
881 rc = bce_reset(sc, BCE_DRV_MSG_CODE_RESET);
885 /* Initialize the controller. */
886 rc = bce_chipinit(sc);
888 device_printf(dev, "Controller initialization failed!\n");
892 /* Perform NVRAM test. */
893 rc = bce_nvram_test(sc);
895 device_printf(dev, "NVRAM test failed!\n");
899 /* Fetch the permanent Ethernet MAC address. */
900 bce_get_mac_addr(sc);
903 * Trip points control how many BDs
904 * should be ready before generating an
905 * interrupt while ticks control how long
906 * a BD can sit in the chain before
907 * generating an interrupt. Set the default
908 * values for the RX and TX rings.
912 /* Force more frequent interrupts. */
913 sc->bce_tx_quick_cons_trip_int = 1;
914 sc->bce_tx_quick_cons_trip = 1;
915 sc->bce_tx_ticks_int = 0;
916 sc->bce_tx_ticks = 0;
918 sc->bce_rx_quick_cons_trip_int = 1;
919 sc->bce_rx_quick_cons_trip = 1;
920 sc->bce_rx_ticks_int = 0;
921 sc->bce_rx_ticks = 0;
923 sc->bce_tx_quick_cons_trip_int = bce_tx_bds_int;
924 sc->bce_tx_quick_cons_trip = bce_tx_bds;
925 sc->bce_tx_ticks_int = bce_tx_ticks_int;
926 sc->bce_tx_ticks = bce_tx_ticks;
928 sc->bce_rx_quick_cons_trip_int = bce_rx_bds_int;
929 sc->bce_rx_quick_cons_trip = bce_rx_bds;
930 sc->bce_rx_ticks_int = bce_rx_ticks_int;
931 sc->bce_rx_ticks = bce_rx_ticks;
934 /* Update statistics once every second. */
935 sc->bce_stats_ticks = 1000000 & 0xffff00;
937 /* Find the media type for the adapter. */
940 /* Find out RX/TX ring count */
941 bce_setup_ring_cnt(sc);
943 /* Allocate DMA memory resources. */
944 rc = bce_dma_alloc(sc);
946 device_printf(dev, "DMA resource allocation failed!\n");
952 * NPOLLING RX/TX CPU offset
954 if (sc->rx_ring_cnt2 == ncpus2) {
957 offset_def = (sc->rx_ring_cnt2 * device_get_unit(dev)) % ncpus2;
958 offset = device_getenv_int(dev, "npoll.offset", offset_def);
959 if (offset >= ncpus2 ||
960 offset % sc->rx_ring_cnt2 != 0) {
961 device_printf(dev, "invalid npoll.offset %d, use %d\n",
966 sc->npoll_ofs = offset;
969 /* Allocate PCI IRQ resources. */
970 rc = bce_alloc_intr(sc);
974 /* Setup serializer */
975 bce_setup_serialize(sc);
977 /* Initialize the ifnet interface. */
979 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
980 ifp->if_ioctl = bce_ioctl;
981 ifp->if_start = bce_start;
982 ifp->if_init = bce_init;
983 ifp->if_serialize = bce_serialize;
984 ifp->if_deserialize = bce_deserialize;
985 ifp->if_tryserialize = bce_tryserialize;
987 ifp->if_serialize_assert = bce_serialize_assert;
990 ifp->if_npoll = bce_npoll;
993 ifp->if_mtu = ETHERMTU;
994 ifp->if_hwassist = BCE_CSUM_FEATURES | CSUM_TSO;
995 ifp->if_capabilities = BCE_IF_CAPABILITIES;
996 ifp->if_capenable = ifp->if_capabilities;
998 if (sc->bce_phy_flags & BCE_PHY_2_5G_CAPABLE_FLAG)
999 ifp->if_baudrate = IF_Gbps(2.5);
1001 ifp->if_baudrate = IF_Gbps(1);
1003 ifq_set_maxlen(&ifp->if_snd, USABLE_TX_BD(&sc->tx_rings[0]));
1004 ifq_set_ready(&ifp->if_snd);
1005 ifq_set_subq_cnt(&ifp->if_snd, sc->tx_ring_cnt);
1007 if (sc->tx_ring_cnt > 1) {
1008 ifp->if_mapsubq = ifq_mapsubq_mask;
1009 ifq_set_subq_mask(&ifp->if_snd, sc->tx_ring_cnt - 1);
1015 mii_probe_args_init(&mii_args, bce_ifmedia_upd, bce_ifmedia_sts);
1016 mii_args.mii_probemask = 1 << sc->bce_phy_addr;
1017 mii_args.mii_privtag = MII_PRIVTAG_BRGPHY;
1018 mii_args.mii_priv = mii_priv;
1020 rc = mii_probe(dev, &sc->bce_miibus, &mii_args);
1022 device_printf(dev, "PHY probe failed!\n");
1026 /* Attach to the Ethernet interface list. */
1027 ether_ifattach(ifp, sc->eaddr, NULL);
1029 callout_init_mp(&sc->bce_tick_callout);
1030 callout_init_mp(&sc->bce_pulse_callout);
1031 callout_init_mp(&sc->bce_ckmsi_callout);
1033 rc = bce_setup_intr(sc);
1035 device_printf(dev, "Failed to setup IRQ!\n");
1036 ether_ifdetach(ifp);
1040 for (i = 0; i < sc->tx_ring_cnt; ++i) {
1041 struct ifaltq_subque *ifsq = ifq_get_subq(&ifp->if_snd, i);
1042 struct bce_tx_ring *txr = &sc->tx_rings[i];
1044 ifsq_set_cpuid(ifsq, sc->bce_msix[i].msix_cpuid);
1045 ifsq_set_priv(ifsq, txr);
1048 ifsq_watchdog_init(&txr->tx_watchdog, ifsq, bce_watchdog);
1051 /* Set timer CPUID */
1052 bce_set_timer_cpuid(sc, FALSE);
1054 /* Add the supported sysctls to the kernel. */
1055 bce_add_sysctls(sc);
1058 * The chip reset earlier notified the bootcode that
1059 * a driver is present. We now need to start our pulse
1060 * routine so that the bootcode is reminded that we're
1065 /* Get the firmware running so IPMI still works */
1069 bce_print_adapter_info(sc);
1078 /****************************************************************************/
1079 /* Device detach function. */
1081 /* Stops the controller, resets the controller, and releases resources. */
1084 /* 0 on success, positive value on failure. */
1085 /****************************************************************************/
1087 bce_detach(device_t dev)
1089 struct bce_softc *sc = device_get_softc(dev);
1091 if (device_is_attached(dev)) {
1092 struct ifnet *ifp = &sc->arpcom.ac_if;
1095 ifnet_serialize_all(ifp);
1097 /* Stop and reset the controller. */
1098 callout_stop(&sc->bce_pulse_callout);
1100 if (sc->bce_flags & BCE_NO_WOL_FLAG)
1101 msg = BCE_DRV_MSG_CODE_UNLOAD_LNK_DN;
1103 msg = BCE_DRV_MSG_CODE_UNLOAD;
1106 bce_teardown_intr(sc);
1108 ifnet_deserialize_all(ifp);
1110 ether_ifdetach(ifp);
1113 /* If we have a child device on the MII bus remove it too. */
1115 device_delete_child(dev, sc->bce_miibus);
1116 bus_generic_detach(dev);
1120 if (sc->bce_res_mem != NULL) {
1121 bus_release_resource(dev, SYS_RES_MEMORY, PCIR_BAR(0),
1127 if (sc->bce_sysctl_tree != NULL)
1128 sysctl_ctx_free(&sc->bce_sysctl_ctx);
1130 if (sc->serializes != NULL)
1131 kfree(sc->serializes, M_DEVBUF);
1137 /****************************************************************************/
1138 /* Device shutdown function. */
1140 /* Stops and resets the controller. */
1144 /****************************************************************************/
1146 bce_shutdown(device_t dev)
1148 struct bce_softc *sc = device_get_softc(dev);
1149 struct ifnet *ifp = &sc->arpcom.ac_if;
1152 ifnet_serialize_all(ifp);
1155 if (sc->bce_flags & BCE_NO_WOL_FLAG)
1156 msg = BCE_DRV_MSG_CODE_UNLOAD_LNK_DN;
1158 msg = BCE_DRV_MSG_CODE_UNLOAD;
1161 ifnet_deserialize_all(ifp);
1165 /****************************************************************************/
1166 /* Indirect register read. */
1168 /* Reads NetXtreme II registers using an index/data register pair in PCI */
1169 /* configuration space. Using this mechanism avoids issues with posted */
1170 /* reads but is much slower than memory-mapped I/O. */
1173 /* The value of the register. */
1174 /****************************************************************************/
1176 bce_reg_rd_ind(struct bce_softc *sc, uint32_t offset)
1178 device_t dev = sc->bce_dev;
1180 pci_write_config(dev, BCE_PCICFG_REG_WINDOW_ADDRESS, offset, 4);
1181 return pci_read_config(dev, BCE_PCICFG_REG_WINDOW, 4);
1185 /****************************************************************************/
1186 /* Indirect register write. */
1188 /* Writes NetXtreme II registers using an index/data register pair in PCI */
1189 /* configuration space. Using this mechanism avoids issues with posted */
1190 /* writes but is muchh slower than memory-mapped I/O. */
1194 /****************************************************************************/
1196 bce_reg_wr_ind(struct bce_softc *sc, uint32_t offset, uint32_t val)
1198 device_t dev = sc->bce_dev;
1200 pci_write_config(dev, BCE_PCICFG_REG_WINDOW_ADDRESS, offset, 4);
1201 pci_write_config(dev, BCE_PCICFG_REG_WINDOW, val, 4);
1205 /****************************************************************************/
1206 /* Shared memory write. */
1208 /* Writes NetXtreme II shared memory region. */
1212 /****************************************************************************/
1214 bce_shmem_wr(struct bce_softc *sc, uint32_t offset, uint32_t val)
1216 bce_reg_wr_ind(sc, sc->bce_shmem_base + offset, val);
1220 /****************************************************************************/
1221 /* Shared memory read. */
1223 /* Reads NetXtreme II shared memory region. */
1226 /* The 32 bit value read. */
1227 /****************************************************************************/
1229 bce_shmem_rd(struct bce_softc *sc, uint32_t offset)
1231 return bce_reg_rd_ind(sc, sc->bce_shmem_base + offset);
1235 /****************************************************************************/
1236 /* Context memory write. */
1238 /* The NetXtreme II controller uses context memory to track connection */
1239 /* information for L2 and higher network protocols. */
1243 /****************************************************************************/
1245 bce_ctx_wr(struct bce_softc *sc, uint32_t cid_addr, uint32_t ctx_offset,
1248 uint32_t idx, offset = ctx_offset + cid_addr;
1249 uint32_t val, retry_cnt = 5;
1251 if (BCE_CHIP_NUM(sc) == BCE_CHIP_NUM_5709 ||
1252 BCE_CHIP_NUM(sc) == BCE_CHIP_NUM_5716) {
1253 REG_WR(sc, BCE_CTX_CTX_DATA, ctx_val);
1254 REG_WR(sc, BCE_CTX_CTX_CTRL, (offset | BCE_CTX_CTX_CTRL_WRITE_REQ));
1256 for (idx = 0; idx < retry_cnt; idx++) {
1257 val = REG_RD(sc, BCE_CTX_CTX_CTRL);
1258 if ((val & BCE_CTX_CTX_CTRL_WRITE_REQ) == 0)
1263 if (val & BCE_CTX_CTX_CTRL_WRITE_REQ) {
1264 device_printf(sc->bce_dev,
1265 "Unable to write CTX memory: "
1266 "cid_addr = 0x%08X, offset = 0x%08X!\n",
1267 cid_addr, ctx_offset);
1270 REG_WR(sc, BCE_CTX_DATA_ADR, offset);
1271 REG_WR(sc, BCE_CTX_DATA, ctx_val);
1276 /****************************************************************************/
1277 /* PHY register read. */
1279 /* Implements register reads on the MII bus. */
1282 /* The value of the register. */
1283 /****************************************************************************/
1285 bce_miibus_read_reg(device_t dev, int phy, int reg)
1287 struct bce_softc *sc = device_get_softc(dev);
1291 /* Make sure we are accessing the correct PHY address. */
1292 KASSERT(phy == sc->bce_phy_addr,
1293 ("invalid phyno %d, should be %d\n", phy, sc->bce_phy_addr));
1295 if (sc->bce_phy_flags & BCE_PHY_INT_MODE_AUTO_POLLING_FLAG) {
1296 val = REG_RD(sc, BCE_EMAC_MDIO_MODE);
1297 val &= ~BCE_EMAC_MDIO_MODE_AUTO_POLL;
1299 REG_WR(sc, BCE_EMAC_MDIO_MODE, val);
1300 REG_RD(sc, BCE_EMAC_MDIO_MODE);
1305 val = BCE_MIPHY(phy) | BCE_MIREG(reg) |
1306 BCE_EMAC_MDIO_COMM_COMMAND_READ | BCE_EMAC_MDIO_COMM_DISEXT |
1307 BCE_EMAC_MDIO_COMM_START_BUSY;
1308 REG_WR(sc, BCE_EMAC_MDIO_COMM, val);
1310 for (i = 0; i < BCE_PHY_TIMEOUT; i++) {
1313 val = REG_RD(sc, BCE_EMAC_MDIO_COMM);
1314 if (!(val & BCE_EMAC_MDIO_COMM_START_BUSY)) {
1317 val = REG_RD(sc, BCE_EMAC_MDIO_COMM);
1318 val &= BCE_EMAC_MDIO_COMM_DATA;
1323 if (val & BCE_EMAC_MDIO_COMM_START_BUSY) {
1324 if_printf(&sc->arpcom.ac_if,
1325 "Error: PHY read timeout! phy = %d, reg = 0x%04X\n",
1329 val = REG_RD(sc, BCE_EMAC_MDIO_COMM);
1332 if (sc->bce_phy_flags & BCE_PHY_INT_MODE_AUTO_POLLING_FLAG) {
1333 val = REG_RD(sc, BCE_EMAC_MDIO_MODE);
1334 val |= BCE_EMAC_MDIO_MODE_AUTO_POLL;
1336 REG_WR(sc, BCE_EMAC_MDIO_MODE, val);
1337 REG_RD(sc, BCE_EMAC_MDIO_MODE);
1341 return (val & 0xffff);
1345 /****************************************************************************/
1346 /* PHY register write. */
1348 /* Implements register writes on the MII bus. */
1351 /* The value of the register. */
1352 /****************************************************************************/
1354 bce_miibus_write_reg(device_t dev, int phy, int reg, int val)
1356 struct bce_softc *sc = device_get_softc(dev);
1360 /* Make sure we are accessing the correct PHY address. */
1361 KASSERT(phy == sc->bce_phy_addr,
1362 ("invalid phyno %d, should be %d\n", phy, sc->bce_phy_addr));
1364 if (sc->bce_phy_flags & BCE_PHY_INT_MODE_AUTO_POLLING_FLAG) {
1365 val1 = REG_RD(sc, BCE_EMAC_MDIO_MODE);
1366 val1 &= ~BCE_EMAC_MDIO_MODE_AUTO_POLL;
1368 REG_WR(sc, BCE_EMAC_MDIO_MODE, val1);
1369 REG_RD(sc, BCE_EMAC_MDIO_MODE);
1374 val1 = BCE_MIPHY(phy) | BCE_MIREG(reg) | val |
1375 BCE_EMAC_MDIO_COMM_COMMAND_WRITE |
1376 BCE_EMAC_MDIO_COMM_START_BUSY | BCE_EMAC_MDIO_COMM_DISEXT;
1377 REG_WR(sc, BCE_EMAC_MDIO_COMM, val1);
1379 for (i = 0; i < BCE_PHY_TIMEOUT; i++) {
1382 val1 = REG_RD(sc, BCE_EMAC_MDIO_COMM);
1383 if (!(val1 & BCE_EMAC_MDIO_COMM_START_BUSY)) {
1389 if (val1 & BCE_EMAC_MDIO_COMM_START_BUSY)
1390 if_printf(&sc->arpcom.ac_if, "PHY write timeout!\n");
1392 if (sc->bce_phy_flags & BCE_PHY_INT_MODE_AUTO_POLLING_FLAG) {
1393 val1 = REG_RD(sc, BCE_EMAC_MDIO_MODE);
1394 val1 |= BCE_EMAC_MDIO_MODE_AUTO_POLL;
1396 REG_WR(sc, BCE_EMAC_MDIO_MODE, val1);
1397 REG_RD(sc, BCE_EMAC_MDIO_MODE);
1405 /****************************************************************************/
1406 /* MII bus status change. */
1408 /* Called by the MII bus driver when the PHY establishes link to set the */
1409 /* MAC interface registers. */
1413 /****************************************************************************/
1415 bce_miibus_statchg(device_t dev)
1417 struct bce_softc *sc = device_get_softc(dev);
1418 struct mii_data *mii = device_get_softc(sc->bce_miibus);
1420 BCE_CLRBIT(sc, BCE_EMAC_MODE, BCE_EMAC_MODE_PORT);
1423 * Set MII or GMII interface based on the speed negotiated
1426 if (IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_T ||
1427 IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_SX) {
1428 BCE_SETBIT(sc, BCE_EMAC_MODE, BCE_EMAC_MODE_PORT_GMII);
1430 BCE_SETBIT(sc, BCE_EMAC_MODE, BCE_EMAC_MODE_PORT_MII);
1434 * Set half or full duplex based on the duplicity negotiated
1437 if ((mii->mii_media_active & IFM_GMASK) == IFM_FDX) {
1438 BCE_CLRBIT(sc, BCE_EMAC_MODE, BCE_EMAC_MODE_HALF_DUPLEX);
1440 BCE_SETBIT(sc, BCE_EMAC_MODE, BCE_EMAC_MODE_HALF_DUPLEX);
1445 /****************************************************************************/
1446 /* Acquire NVRAM lock. */
1448 /* Before the NVRAM can be accessed the caller must acquire an NVRAM lock. */
1449 /* Locks 0 and 2 are reserved, lock 1 is used by firmware and lock 2 is */
1450 /* for use by the driver. */
1453 /* 0 on success, positive value on failure. */
1454 /****************************************************************************/
1456 bce_acquire_nvram_lock(struct bce_softc *sc)
1461 /* Request access to the flash interface. */
1462 REG_WR(sc, BCE_NVM_SW_ARB, BCE_NVM_SW_ARB_ARB_REQ_SET2);
1463 for (j = 0; j < NVRAM_TIMEOUT_COUNT; j++) {
1464 val = REG_RD(sc, BCE_NVM_SW_ARB);
1465 if (val & BCE_NVM_SW_ARB_ARB_ARB2)
1471 if (j >= NVRAM_TIMEOUT_COUNT) {
1478 /****************************************************************************/
1479 /* Release NVRAM lock. */
1481 /* When the caller is finished accessing NVRAM the lock must be released. */
1482 /* Locks 0 and 2 are reserved, lock 1 is used by firmware and lock 2 is */
1483 /* for use by the driver. */
1486 /* 0 on success, positive value on failure. */
1487 /****************************************************************************/
1489 bce_release_nvram_lock(struct bce_softc *sc)
1495 * Relinquish nvram interface.
1497 REG_WR(sc, BCE_NVM_SW_ARB, BCE_NVM_SW_ARB_ARB_REQ_CLR2);
1499 for (j = 0; j < NVRAM_TIMEOUT_COUNT; j++) {
1500 val = REG_RD(sc, BCE_NVM_SW_ARB);
1501 if (!(val & BCE_NVM_SW_ARB_ARB_ARB2))
1507 if (j >= NVRAM_TIMEOUT_COUNT) {
1514 /****************************************************************************/
1515 /* Enable NVRAM access. */
1517 /* Before accessing NVRAM for read or write operations the caller must */
1518 /* enabled NVRAM access. */
1522 /****************************************************************************/
1524 bce_enable_nvram_access(struct bce_softc *sc)
1528 val = REG_RD(sc, BCE_NVM_ACCESS_ENABLE);
1529 /* Enable both bits, even on read. */
1530 REG_WR(sc, BCE_NVM_ACCESS_ENABLE,
1531 val | BCE_NVM_ACCESS_ENABLE_EN | BCE_NVM_ACCESS_ENABLE_WR_EN);
1535 /****************************************************************************/
1536 /* Disable NVRAM access. */
1538 /* When the caller is finished accessing NVRAM access must be disabled. */
1542 /****************************************************************************/
1544 bce_disable_nvram_access(struct bce_softc *sc)
1548 val = REG_RD(sc, BCE_NVM_ACCESS_ENABLE);
1550 /* Disable both bits, even after read. */
1551 REG_WR(sc, BCE_NVM_ACCESS_ENABLE,
1552 val & ~(BCE_NVM_ACCESS_ENABLE_EN | BCE_NVM_ACCESS_ENABLE_WR_EN));
1556 /****************************************************************************/
1557 /* Read a dword (32 bits) from NVRAM. */
1559 /* Read a 32 bit word from NVRAM. The caller is assumed to have already */
1560 /* obtained the NVRAM lock and enabled the controller for NVRAM access. */
1563 /* 0 on success and the 32 bit value read, positive value on failure. */
1564 /****************************************************************************/
1566 bce_nvram_read_dword(struct bce_softc *sc, uint32_t offset, uint8_t *ret_val,
1572 /* Build the command word. */
1573 cmd = BCE_NVM_COMMAND_DOIT | cmd_flags;
1575 /* Calculate the offset for buffered flash. */
1576 if (sc->bce_flash_info->flags & BCE_NV_TRANSLATE) {
1577 offset = ((offset / sc->bce_flash_info->page_size) <<
1578 sc->bce_flash_info->page_bits) +
1579 (offset % sc->bce_flash_info->page_size);
1583 * Clear the DONE bit separately, set the address to read,
1584 * and issue the read.
1586 REG_WR(sc, BCE_NVM_COMMAND, BCE_NVM_COMMAND_DONE);
1587 REG_WR(sc, BCE_NVM_ADDR, offset & BCE_NVM_ADDR_NVM_ADDR_VALUE);
1588 REG_WR(sc, BCE_NVM_COMMAND, cmd);
1590 /* Wait for completion. */
1591 for (i = 0; i < NVRAM_TIMEOUT_COUNT; i++) {
1596 val = REG_RD(sc, BCE_NVM_COMMAND);
1597 if (val & BCE_NVM_COMMAND_DONE) {
1598 val = REG_RD(sc, BCE_NVM_READ);
1601 memcpy(ret_val, &val, 4);
1606 /* Check for errors. */
1607 if (i >= NVRAM_TIMEOUT_COUNT) {
1608 if_printf(&sc->arpcom.ac_if,
1609 "Timeout error reading NVRAM at offset 0x%08X!\n",
1617 /****************************************************************************/
1618 /* Initialize NVRAM access. */
1620 /* Identify the NVRAM device in use and prepare the NVRAM interface to */
1621 /* access that device. */
1624 /* 0 on success, positive value on failure. */
1625 /****************************************************************************/
1627 bce_init_nvram(struct bce_softc *sc)
1630 int j, entry_count, rc = 0;
1631 const struct flash_spec *flash;
1633 if (BCE_CHIP_NUM(sc) == BCE_CHIP_NUM_5709 ||
1634 BCE_CHIP_NUM(sc) == BCE_CHIP_NUM_5716) {
1635 sc->bce_flash_info = &flash_5709;
1636 goto bce_init_nvram_get_flash_size;
1639 /* Determine the selected interface. */
1640 val = REG_RD(sc, BCE_NVM_CFG1);
1642 entry_count = sizeof(flash_table) / sizeof(struct flash_spec);
1645 * Flash reconfiguration is required to support additional
1646 * NVRAM devices not directly supported in hardware.
1647 * Check if the flash interface was reconfigured
1651 if (val & 0x40000000) {
1652 /* Flash interface reconfigured by bootcode. */
1653 for (j = 0, flash = flash_table; j < entry_count;
1655 if ((val & FLASH_BACKUP_STRAP_MASK) ==
1656 (flash->config1 & FLASH_BACKUP_STRAP_MASK)) {
1657 sc->bce_flash_info = flash;
1662 /* Flash interface not yet reconfigured. */
1665 if (val & (1 << 23))
1666 mask = FLASH_BACKUP_STRAP_MASK;
1668 mask = FLASH_STRAP_MASK;
1670 /* Look for the matching NVRAM device configuration data. */
1671 for (j = 0, flash = flash_table; j < entry_count;
1673 /* Check if the device matches any of the known devices. */
1674 if ((val & mask) == (flash->strapping & mask)) {
1675 /* Found a device match. */
1676 sc->bce_flash_info = flash;
1678 /* Request access to the flash interface. */
1679 rc = bce_acquire_nvram_lock(sc);
1683 /* Reconfigure the flash interface. */
1684 bce_enable_nvram_access(sc);
1685 REG_WR(sc, BCE_NVM_CFG1, flash->config1);
1686 REG_WR(sc, BCE_NVM_CFG2, flash->config2);
1687 REG_WR(sc, BCE_NVM_CFG3, flash->config3);
1688 REG_WR(sc, BCE_NVM_WRITE1, flash->write1);
1689 bce_disable_nvram_access(sc);
1690 bce_release_nvram_lock(sc);
1696 /* Check if a matching device was found. */
1697 if (j == entry_count) {
1698 sc->bce_flash_info = NULL;
1699 if_printf(&sc->arpcom.ac_if, "Unknown Flash NVRAM found!\n");
1703 bce_init_nvram_get_flash_size:
1704 /* Write the flash config data to the shared memory interface. */
1705 val = bce_shmem_rd(sc, BCE_SHARED_HW_CFG_CONFIG2) &
1706 BCE_SHARED_HW_CFG2_NVM_SIZE_MASK;
1708 sc->bce_flash_size = val;
1710 sc->bce_flash_size = sc->bce_flash_info->total_size;
1716 /****************************************************************************/
1717 /* Read an arbitrary range of data from NVRAM. */
1719 /* Prepares the NVRAM interface for access and reads the requested data */
1720 /* into the supplied buffer. */
1723 /* 0 on success and the data read, positive value on failure. */
1724 /****************************************************************************/
1726 bce_nvram_read(struct bce_softc *sc, uint32_t offset, uint8_t *ret_buf,
1729 uint32_t cmd_flags, offset32, len32, extra;
1735 /* Request access to the flash interface. */
1736 rc = bce_acquire_nvram_lock(sc);
1740 /* Enable access to flash interface */
1741 bce_enable_nvram_access(sc);
1749 /* XXX should we release nvram lock if read_dword() fails? */
1755 pre_len = 4 - (offset & 3);
1757 if (pre_len >= len32) {
1759 cmd_flags = BCE_NVM_COMMAND_FIRST | BCE_NVM_COMMAND_LAST;
1761 cmd_flags = BCE_NVM_COMMAND_FIRST;
1764 rc = bce_nvram_read_dword(sc, offset32, buf, cmd_flags);
1768 memcpy(ret_buf, buf + (offset & 3), pre_len);
1776 extra = 4 - (len32 & 3);
1777 len32 = (len32 + 4) & ~3;
1784 cmd_flags = BCE_NVM_COMMAND_LAST;
1786 cmd_flags = BCE_NVM_COMMAND_FIRST |
1787 BCE_NVM_COMMAND_LAST;
1789 rc = bce_nvram_read_dword(sc, offset32, buf, cmd_flags);
1791 memcpy(ret_buf, buf, 4 - extra);
1792 } else if (len32 > 0) {
1795 /* Read the first word. */
1799 cmd_flags = BCE_NVM_COMMAND_FIRST;
1801 rc = bce_nvram_read_dword(sc, offset32, ret_buf, cmd_flags);
1803 /* Advance to the next dword. */
1808 while (len32 > 4 && rc == 0) {
1809 rc = bce_nvram_read_dword(sc, offset32, ret_buf, 0);
1811 /* Advance to the next dword. */
1818 goto bce_nvram_read_locked_exit;
1820 cmd_flags = BCE_NVM_COMMAND_LAST;
1821 rc = bce_nvram_read_dword(sc, offset32, buf, cmd_flags);
1823 memcpy(ret_buf, buf, 4 - extra);
1826 bce_nvram_read_locked_exit:
1827 /* Disable access to flash interface and release the lock. */
1828 bce_disable_nvram_access(sc);
1829 bce_release_nvram_lock(sc);
1835 /****************************************************************************/
1836 /* Verifies that NVRAM is accessible and contains valid data. */
1838 /* Reads the configuration data from NVRAM and verifies that the CRC is */
1842 /* 0 on success, positive value on failure. */
1843 /****************************************************************************/
1845 bce_nvram_test(struct bce_softc *sc)
1847 uint32_t buf[BCE_NVRAM_SIZE / 4];
1848 uint32_t magic, csum;
1849 uint8_t *data = (uint8_t *)buf;
1853 * Check that the device NVRAM is valid by reading
1854 * the magic value at offset 0.
1856 rc = bce_nvram_read(sc, 0, data, 4);
1860 magic = be32toh(buf[0]);
1861 if (magic != BCE_NVRAM_MAGIC) {
1862 if_printf(&sc->arpcom.ac_if,
1863 "Invalid NVRAM magic value! Expected: 0x%08X, "
1864 "Found: 0x%08X\n", BCE_NVRAM_MAGIC, magic);
1869 * Verify that the device NVRAM includes valid
1870 * configuration data.
1872 rc = bce_nvram_read(sc, 0x100, data, BCE_NVRAM_SIZE);
1876 csum = ether_crc32_le(data, 0x100);
1877 if (csum != BCE_CRC32_RESIDUAL) {
1878 if_printf(&sc->arpcom.ac_if,
1879 "Invalid Manufacturing Information NVRAM CRC! "
1880 "Expected: 0x%08X, Found: 0x%08X\n",
1881 BCE_CRC32_RESIDUAL, csum);
1885 csum = ether_crc32_le(data + 0x100, 0x100);
1886 if (csum != BCE_CRC32_RESIDUAL) {
1887 if_printf(&sc->arpcom.ac_if,
1888 "Invalid Feature Configuration Information "
1889 "NVRAM CRC! Expected: 0x%08X, Found: 08%08X\n",
1890 BCE_CRC32_RESIDUAL, csum);
1897 /****************************************************************************/
1898 /* Identifies the current media type of the controller and sets the PHY */
1903 /****************************************************************************/
1905 bce_get_media(struct bce_softc *sc)
1909 sc->bce_phy_addr = 1;
1911 if (BCE_CHIP_NUM(sc) == BCE_CHIP_NUM_5709 ||
1912 BCE_CHIP_NUM(sc) == BCE_CHIP_NUM_5716) {
1913 uint32_t val = REG_RD(sc, BCE_MISC_DUAL_MEDIA_CTRL);
1914 uint32_t bond_id = val & BCE_MISC_DUAL_MEDIA_CTRL_BOND_ID;
1918 * The BCM5709S is software configurable
1919 * for Copper or SerDes operation.
1921 if (bond_id == BCE_MISC_DUAL_MEDIA_CTRL_BOND_ID_C) {
1923 } else if (bond_id == BCE_MISC_DUAL_MEDIA_CTRL_BOND_ID_S) {
1924 sc->bce_phy_flags |= BCE_PHY_SERDES_FLAG;
1928 if (val & BCE_MISC_DUAL_MEDIA_CTRL_STRAP_OVERRIDE) {
1929 strap = (val & BCE_MISC_DUAL_MEDIA_CTRL_PHY_CTRL) >> 21;
1932 (val & BCE_MISC_DUAL_MEDIA_CTRL_PHY_CTRL_STRAP) >> 8;
1935 if (pci_get_function(sc->bce_dev) == 0) {
1940 sc->bce_phy_flags |= BCE_PHY_SERDES_FLAG;
1948 sc->bce_phy_flags |= BCE_PHY_SERDES_FLAG;
1952 } else if (BCE_CHIP_BOND_ID(sc) & BCE_CHIP_BOND_ID_SERDES_BIT) {
1953 sc->bce_phy_flags |= BCE_PHY_SERDES_FLAG;
1956 if (sc->bce_phy_flags & BCE_PHY_SERDES_FLAG) {
1957 sc->bce_flags |= BCE_NO_WOL_FLAG;
1958 if (BCE_CHIP_NUM(sc) != BCE_CHIP_NUM_5706) {
1959 sc->bce_phy_addr = 2;
1960 val = bce_shmem_rd(sc, BCE_SHARED_HW_CFG_CONFIG);
1961 if (val & BCE_SHARED_HW_CFG_PHY_2_5G)
1962 sc->bce_phy_flags |= BCE_PHY_2_5G_CAPABLE_FLAG;
1964 } else if ((BCE_CHIP_NUM(sc) == BCE_CHIP_NUM_5706) ||
1965 (BCE_CHIP_NUM(sc) == BCE_CHIP_NUM_5708)) {
1966 sc->bce_phy_flags |= BCE_PHY_CRC_FIX_FLAG;
1972 bce_destroy_tx_ring(struct bce_tx_ring *txr)
1976 /* Destroy the TX buffer descriptor DMA stuffs. */
1977 if (txr->tx_bd_chain_tag != NULL) {
1978 for (i = 0; i < txr->tx_pages; i++) {
1979 if (txr->tx_bd_chain[i] != NULL) {
1980 bus_dmamap_unload(txr->tx_bd_chain_tag,
1981 txr->tx_bd_chain_map[i]);
1982 bus_dmamem_free(txr->tx_bd_chain_tag,
1983 txr->tx_bd_chain[i],
1984 txr->tx_bd_chain_map[i]);
1987 bus_dma_tag_destroy(txr->tx_bd_chain_tag);
1990 /* Destroy the TX mbuf DMA stuffs. */
1991 if (txr->tx_mbuf_tag != NULL) {
1992 for (i = 0; i < TOTAL_TX_BD(txr); i++) {
1993 /* Must have been unloaded in bce_stop() */
1994 KKASSERT(txr->tx_mbuf_ptr[i] == NULL);
1995 bus_dmamap_destroy(txr->tx_mbuf_tag,
1996 txr->tx_mbuf_map[i]);
1998 bus_dma_tag_destroy(txr->tx_mbuf_tag);
2001 if (txr->tx_bd_chain_map != NULL)
2002 kfree(txr->tx_bd_chain_map, M_DEVBUF);
2003 if (txr->tx_bd_chain != NULL)
2004 kfree(txr->tx_bd_chain, M_DEVBUF);
2005 if (txr->tx_bd_chain_paddr != NULL)
2006 kfree(txr->tx_bd_chain_paddr, M_DEVBUF);
2008 if (txr->tx_mbuf_map != NULL)
2009 kfree(txr->tx_mbuf_map, M_DEVBUF);
2010 if (txr->tx_mbuf_ptr != NULL)
2011 kfree(txr->tx_mbuf_ptr, M_DEVBUF);
2016 bce_destroy_rx_ring(struct bce_rx_ring *rxr)
2020 /* Destroy the RX buffer descriptor DMA stuffs. */
2021 if (rxr->rx_bd_chain_tag != NULL) {
2022 for (i = 0; i < rxr->rx_pages; i++) {
2023 if (rxr->rx_bd_chain[i] != NULL) {
2024 bus_dmamap_unload(rxr->rx_bd_chain_tag,
2025 rxr->rx_bd_chain_map[i]);
2026 bus_dmamem_free(rxr->rx_bd_chain_tag,
2027 rxr->rx_bd_chain[i],
2028 rxr->rx_bd_chain_map[i]);
2031 bus_dma_tag_destroy(rxr->rx_bd_chain_tag);
2034 /* Destroy the RX mbuf DMA stuffs. */
2035 if (rxr->rx_mbuf_tag != NULL) {
2036 for (i = 0; i < TOTAL_RX_BD(rxr); i++) {
2037 /* Must have been unloaded in bce_stop() */
2038 KKASSERT(rxr->rx_mbuf_ptr[i] == NULL);
2039 bus_dmamap_destroy(rxr->rx_mbuf_tag,
2040 rxr->rx_mbuf_map[i]);
2042 bus_dmamap_destroy(rxr->rx_mbuf_tag, rxr->rx_mbuf_tmpmap);
2043 bus_dma_tag_destroy(rxr->rx_mbuf_tag);
2046 if (rxr->rx_bd_chain_map != NULL)
2047 kfree(rxr->rx_bd_chain_map, M_DEVBUF);
2048 if (rxr->rx_bd_chain != NULL)
2049 kfree(rxr->rx_bd_chain, M_DEVBUF);
2050 if (rxr->rx_bd_chain_paddr != NULL)
2051 kfree(rxr->rx_bd_chain_paddr, M_DEVBUF);
2053 if (rxr->rx_mbuf_map != NULL)
2054 kfree(rxr->rx_mbuf_map, M_DEVBUF);
2055 if (rxr->rx_mbuf_ptr != NULL)
2056 kfree(rxr->rx_mbuf_ptr, M_DEVBUF);
2057 if (rxr->rx_mbuf_paddr != NULL)
2058 kfree(rxr->rx_mbuf_paddr, M_DEVBUF);
2062 /****************************************************************************/
2063 /* Free any DMA memory owned by the driver. */
2065 /* Scans through each data structre that requires DMA memory and frees */
2066 /* the memory if allocated. */
2070 /****************************************************************************/
2072 bce_dma_free(struct bce_softc *sc)
2076 /* Destroy the status block. */
2077 if (sc->status_tag != NULL) {
2078 if (sc->status_block != NULL) {
2079 bus_dmamap_unload(sc->status_tag, sc->status_map);
2080 bus_dmamem_free(sc->status_tag, sc->status_block,
2083 bus_dma_tag_destroy(sc->status_tag);
2086 /* Destroy the statistics block. */
2087 if (sc->stats_tag != NULL) {
2088 if (sc->stats_block != NULL) {
2089 bus_dmamap_unload(sc->stats_tag, sc->stats_map);
2090 bus_dmamem_free(sc->stats_tag, sc->stats_block,
2093 bus_dma_tag_destroy(sc->stats_tag);
2096 /* Destroy the CTX DMA stuffs. */
2097 if (sc->ctx_tag != NULL) {
2098 for (i = 0; i < sc->ctx_pages; i++) {
2099 if (sc->ctx_block[i] != NULL) {
2100 bus_dmamap_unload(sc->ctx_tag, sc->ctx_map[i]);
2101 bus_dmamem_free(sc->ctx_tag, sc->ctx_block[i],
2105 bus_dma_tag_destroy(sc->ctx_tag);
2109 if (sc->tx_rings != NULL) {
2110 for (i = 0; i < sc->tx_ring_cnt; ++i)
2111 bce_destroy_tx_ring(&sc->tx_rings[i]);
2112 kfree(sc->tx_rings, M_DEVBUF);
2116 if (sc->rx_rings != NULL) {
2117 for (i = 0; i < sc->rx_ring_cnt; ++i)
2118 bce_destroy_rx_ring(&sc->rx_rings[i]);
2119 kfree(sc->rx_rings, M_DEVBUF);
2122 /* Destroy the parent tag */
2123 if (sc->parent_tag != NULL)
2124 bus_dma_tag_destroy(sc->parent_tag);
2128 /****************************************************************************/
2129 /* Get DMA memory from the OS. */
2131 /* Validates that the OS has provided DMA buffers in response to a */
2132 /* bus_dmamap_load() call and saves the physical address of those buffers. */
2133 /* When the callback is used the OS will return 0 for the mapping function */
2134 /* (bus_dmamap_load()) so we use the value of map_arg->maxsegs to pass any */
2135 /* failures back to the caller. */
2139 /****************************************************************************/
2141 bce_dma_map_addr(void *arg, bus_dma_segment_t *segs, int nseg, int error)
2143 bus_addr_t *busaddr = arg;
2145 /* Check for an error and signal the caller that an error occurred. */
2149 KASSERT(nseg == 1, ("only one segment is allowed"));
2150 *busaddr = segs->ds_addr;
2155 bce_create_tx_ring(struct bce_tx_ring *txr)
2159 lwkt_serialize_init(&txr->tx_serialize);
2160 txr->tx_wreg = bce_tx_wreg;
2162 pages = device_getenv_int(txr->sc->bce_dev, "tx_pages", bce_tx_pages);
2163 if (pages <= 0 || pages > TX_PAGES_MAX || !powerof2(pages)) {
2164 device_printf(txr->sc->bce_dev, "invalid # of TX pages\n");
2165 pages = TX_PAGES_DEFAULT;
2167 txr->tx_pages = pages;
2169 txr->tx_bd_chain_map = kmalloc(sizeof(bus_dmamap_t) * txr->tx_pages,
2170 M_DEVBUF, M_WAITOK | M_ZERO);
2171 txr->tx_bd_chain = kmalloc(sizeof(struct tx_bd *) * txr->tx_pages,
2172 M_DEVBUF, M_WAITOK | M_ZERO);
2173 txr->tx_bd_chain_paddr = kmalloc(sizeof(bus_addr_t) * txr->tx_pages,
2174 M_DEVBUF, M_WAITOK | M_ZERO);
2176 txr->tx_mbuf_map = kmalloc(sizeof(bus_dmamap_t) * TOTAL_TX_BD(txr),
2177 M_DEVBUF, M_WAITOK | M_ZERO);
2178 txr->tx_mbuf_ptr = kmalloc(sizeof(struct mbuf *) * TOTAL_TX_BD(txr),
2179 M_DEVBUF, M_WAITOK | M_ZERO);
2182 * Create a DMA tag for the TX buffer descriptor chain,
2183 * allocate and clear the memory, and fetch the
2184 * physical address of the block.
2186 rc = bus_dma_tag_create(txr->sc->parent_tag, BCM_PAGE_SIZE, 0,
2187 BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL,
2188 BCE_TX_CHAIN_PAGE_SZ, 1, BCE_TX_CHAIN_PAGE_SZ,
2189 0, &txr->tx_bd_chain_tag);
2191 device_printf(txr->sc->bce_dev, "Could not allocate "
2192 "TX descriptor chain DMA tag!\n");
2196 for (i = 0; i < txr->tx_pages; i++) {
2199 rc = bus_dmamem_alloc(txr->tx_bd_chain_tag,
2200 (void **)&txr->tx_bd_chain[i],
2201 BUS_DMA_WAITOK | BUS_DMA_ZERO | BUS_DMA_COHERENT,
2202 &txr->tx_bd_chain_map[i]);
2204 device_printf(txr->sc->bce_dev,
2205 "Could not allocate %dth TX descriptor "
2206 "chain DMA memory!\n", i);
2210 rc = bus_dmamap_load(txr->tx_bd_chain_tag,
2211 txr->tx_bd_chain_map[i],
2212 txr->tx_bd_chain[i],
2213 BCE_TX_CHAIN_PAGE_SZ,
2214 bce_dma_map_addr, &busaddr,
2217 if (rc == EINPROGRESS) {
2218 panic("%s coherent memory loading "
2219 "is still in progress!",
2220 txr->sc->arpcom.ac_if.if_xname);
2222 device_printf(txr->sc->bce_dev, "Could not map %dth "
2223 "TX descriptor chain DMA memory!\n", i);
2224 bus_dmamem_free(txr->tx_bd_chain_tag,
2225 txr->tx_bd_chain[i],
2226 txr->tx_bd_chain_map[i]);
2227 txr->tx_bd_chain[i] = NULL;
2231 txr->tx_bd_chain_paddr[i] = busaddr;
2234 /* Create a DMA tag for TX mbufs. */
2235 rc = bus_dma_tag_create(txr->sc->parent_tag, 1, 0,
2236 BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL,
2237 IP_MAXPACKET + sizeof(struct ether_vlan_header),
2238 BCE_MAX_SEGMENTS, PAGE_SIZE,
2239 BUS_DMA_ALLOCNOW | BUS_DMA_WAITOK | BUS_DMA_ONEBPAGE,
2242 device_printf(txr->sc->bce_dev,
2243 "Could not allocate TX mbuf DMA tag!\n");
2247 /* Create DMA maps for the TX mbufs clusters. */
2248 for (i = 0; i < TOTAL_TX_BD(txr); i++) {
2249 rc = bus_dmamap_create(txr->tx_mbuf_tag,
2250 BUS_DMA_WAITOK | BUS_DMA_ONEBPAGE,
2251 &txr->tx_mbuf_map[i]);
2255 for (j = 0; j < i; ++j) {
2256 bus_dmamap_destroy(txr->tx_mbuf_tag,
2257 txr->tx_mbuf_map[i]);
2259 bus_dma_tag_destroy(txr->tx_mbuf_tag);
2260 txr->tx_mbuf_tag = NULL;
2262 device_printf(txr->sc->bce_dev, "Unable to create "
2263 "%dth TX mbuf DMA map!\n", i);
2272 bce_create_rx_ring(struct bce_rx_ring *rxr)
2276 lwkt_serialize_init(&rxr->rx_serialize);
2278 pages = device_getenv_int(rxr->sc->bce_dev, "rx_pages", bce_rx_pages);
2279 if (pages <= 0 || pages > RX_PAGES_MAX || !powerof2(pages)) {
2280 device_printf(rxr->sc->bce_dev, "invalid # of RX pages\n");
2281 pages = RX_PAGES_DEFAULT;
2283 rxr->rx_pages = pages;
2285 rxr->rx_bd_chain_map = kmalloc(sizeof(bus_dmamap_t) * rxr->rx_pages,
2286 M_DEVBUF, M_WAITOK | M_ZERO);
2287 rxr->rx_bd_chain = kmalloc(sizeof(struct rx_bd *) * rxr->rx_pages,
2288 M_DEVBUF, M_WAITOK | M_ZERO);
2289 rxr->rx_bd_chain_paddr = kmalloc(sizeof(bus_addr_t) * rxr->rx_pages,
2290 M_DEVBUF, M_WAITOK | M_ZERO);
2292 rxr->rx_mbuf_map = kmalloc(sizeof(bus_dmamap_t) * TOTAL_RX_BD(rxr),
2293 M_DEVBUF, M_WAITOK | M_ZERO);
2294 rxr->rx_mbuf_ptr = kmalloc(sizeof(struct mbuf *) * TOTAL_RX_BD(rxr),
2295 M_DEVBUF, M_WAITOK | M_ZERO);
2296 rxr->rx_mbuf_paddr = kmalloc(sizeof(bus_addr_t) * TOTAL_RX_BD(rxr),
2297 M_DEVBUF, M_WAITOK | M_ZERO);
2300 * Create a DMA tag for the RX buffer descriptor chain,
2301 * allocate and clear the memory, and fetch the physical
2302 * address of the blocks.
2304 rc = bus_dma_tag_create(rxr->sc->parent_tag, BCM_PAGE_SIZE, 0,
2305 BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL,
2306 BCE_RX_CHAIN_PAGE_SZ, 1, BCE_RX_CHAIN_PAGE_SZ,
2307 0, &rxr->rx_bd_chain_tag);
2309 device_printf(rxr->sc->bce_dev, "Could not allocate "
2310 "RX descriptor chain DMA tag!\n");
2314 for (i = 0; i < rxr->rx_pages; i++) {
2317 rc = bus_dmamem_alloc(rxr->rx_bd_chain_tag,
2318 (void **)&rxr->rx_bd_chain[i],
2319 BUS_DMA_WAITOK | BUS_DMA_ZERO | BUS_DMA_COHERENT,
2320 &rxr->rx_bd_chain_map[i]);
2322 device_printf(rxr->sc->bce_dev,
2323 "Could not allocate %dth RX descriptor "
2324 "chain DMA memory!\n", i);
2328 rc = bus_dmamap_load(rxr->rx_bd_chain_tag,
2329 rxr->rx_bd_chain_map[i],
2330 rxr->rx_bd_chain[i],
2331 BCE_RX_CHAIN_PAGE_SZ,
2332 bce_dma_map_addr, &busaddr,
2335 if (rc == EINPROGRESS) {
2336 panic("%s coherent memory loading "
2337 "is still in progress!",
2338 rxr->sc->arpcom.ac_if.if_xname);
2340 device_printf(rxr->sc->bce_dev,
2341 "Could not map %dth RX descriptor "
2342 "chain DMA memory!\n", i);
2343 bus_dmamem_free(rxr->rx_bd_chain_tag,
2344 rxr->rx_bd_chain[i],
2345 rxr->rx_bd_chain_map[i]);
2346 rxr->rx_bd_chain[i] = NULL;
2350 rxr->rx_bd_chain_paddr[i] = busaddr;
2353 /* Create a DMA tag for RX mbufs. */
2354 rc = bus_dma_tag_create(rxr->sc->parent_tag, BCE_DMA_RX_ALIGN, 0,
2355 BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL,
2356 MCLBYTES, 1, MCLBYTES,
2357 BUS_DMA_ALLOCNOW | BUS_DMA_ALIGNED | BUS_DMA_WAITOK,
2360 device_printf(rxr->sc->bce_dev,
2361 "Could not allocate RX mbuf DMA tag!\n");
2365 /* Create tmp DMA map for RX mbuf clusters. */
2366 rc = bus_dmamap_create(rxr->rx_mbuf_tag, BUS_DMA_WAITOK,
2367 &rxr->rx_mbuf_tmpmap);
2369 bus_dma_tag_destroy(rxr->rx_mbuf_tag);
2370 rxr->rx_mbuf_tag = NULL;
2372 device_printf(rxr->sc->bce_dev,
2373 "Could not create RX mbuf tmp DMA map!\n");
2377 /* Create DMA maps for the RX mbuf clusters. */
2378 for (i = 0; i < TOTAL_RX_BD(rxr); i++) {
2379 rc = bus_dmamap_create(rxr->rx_mbuf_tag, BUS_DMA_WAITOK,
2380 &rxr->rx_mbuf_map[i]);
2384 for (j = 0; j < i; ++j) {
2385 bus_dmamap_destroy(rxr->rx_mbuf_tag,
2386 rxr->rx_mbuf_map[j]);
2388 bus_dma_tag_destroy(rxr->rx_mbuf_tag);
2389 rxr->rx_mbuf_tag = NULL;
2391 device_printf(rxr->sc->bce_dev, "Unable to create "
2392 "%dth RX mbuf DMA map!\n", i);
2400 /****************************************************************************/
2401 /* Allocate any DMA memory needed by the driver. */
2403 /* Allocates DMA memory needed for the various global structures needed by */
2406 /* Memory alignment requirements: */
2407 /* -----------------+----------+----------+----------+----------+ */
2408 /* Data Structure | 5706 | 5708 | 5709 | 5716 | */
2409 /* -----------------+----------+----------+----------+----------+ */
2410 /* Status Block | 8 bytes | 8 bytes | 16 bytes | 16 bytes | */
2411 /* Statistics Block | 8 bytes | 8 bytes | 16 bytes | 16 bytes | */
2412 /* RX Buffers | 16 bytes | 16 bytes | 16 bytes | 16 bytes | */
2413 /* PG Buffers | none | none | none | none | */
2414 /* TX Buffers | none | none | none | none | */
2415 /* Chain Pages(1) | 4KiB | 4KiB | 4KiB | 4KiB | */
2416 /* Context Pages(1) | N/A | N/A | 4KiB | 4KiB | */
2417 /* -----------------+----------+----------+----------+----------+ */
2419 /* (1) Must align with CPU page size (BCM_PAGE_SZIE). */
2422 /* 0 for success, positive value for failure. */
2423 /****************************************************************************/
2425 bce_dma_alloc(struct bce_softc *sc)
2427 struct ifnet *ifp = &sc->arpcom.ac_if;
2429 bus_addr_t busaddr, max_busaddr;
2430 bus_size_t status_align, stats_align, status_size;
2433 * The embedded PCIe to PCI-X bridge (EPB)
2434 * in the 5708 cannot address memory above
2435 * 40 bits (E7_5708CB1_23043 & E6_5708SB1_23043).
2437 if (BCE_CHIP_NUM(sc) == BCE_CHIP_NUM_5708)
2438 max_busaddr = BCE_BUS_SPACE_MAXADDR;
2440 max_busaddr = BUS_SPACE_MAXADDR;
2443 * BCM5709 and BCM5716 uses host memory as cache for context memory.
2445 if (BCE_CHIP_NUM(sc) == BCE_CHIP_NUM_5709 ||
2446 BCE_CHIP_NUM(sc) == BCE_CHIP_NUM_5716) {
2447 sc->ctx_pages = BCE_CTX_BLK_SZ / BCM_PAGE_SIZE;
2448 if (sc->ctx_pages == 0)
2450 if (sc->ctx_pages > BCE_CTX_PAGES) {
2451 device_printf(sc->bce_dev, "excessive ctx pages %d\n",
2463 * Each MSI-X vector needs a status block; each status block
2464 * consumes 128bytes and is 128bytes aligned.
2466 if (sc->rx_ring_cnt > 1) {
2467 status_size = BCE_MSIX_MAX * BCE_STATUS_BLK_MSIX_ALIGN;
2468 status_align = BCE_STATUS_BLK_MSIX_ALIGN;
2470 status_size = BCE_STATUS_BLK_SZ;
2474 * Allocate the parent bus DMA tag appropriate for PCI.
2476 rc = bus_dma_tag_create(NULL, 1, BCE_DMA_BOUNDARY,
2477 max_busaddr, BUS_SPACE_MAXADDR,
2479 BUS_SPACE_MAXSIZE_32BIT, 0,
2480 BUS_SPACE_MAXSIZE_32BIT,
2481 0, &sc->parent_tag);
2483 if_printf(ifp, "Could not allocate parent DMA tag!\n");
2488 * Allocate status block.
2490 sc->status_block = bus_dmamem_coherent_any(sc->parent_tag,
2491 status_align, status_size,
2492 BUS_DMA_WAITOK | BUS_DMA_ZERO,
2493 &sc->status_tag, &sc->status_map,
2494 &sc->status_block_paddr);
2495 if (sc->status_block == NULL) {
2496 if_printf(ifp, "Could not allocate status block!\n");
2501 * Allocate statistics block.
2503 sc->stats_block = bus_dmamem_coherent_any(sc->parent_tag,
2504 stats_align, BCE_STATS_BLK_SZ,
2505 BUS_DMA_WAITOK | BUS_DMA_ZERO,
2506 &sc->stats_tag, &sc->stats_map,
2507 &sc->stats_block_paddr);
2508 if (sc->stats_block == NULL) {
2509 if_printf(ifp, "Could not allocate statistics block!\n");
2514 * Allocate context block, if needed
2516 if (sc->ctx_pages != 0) {
2517 rc = bus_dma_tag_create(sc->parent_tag, BCM_PAGE_SIZE, 0,
2518 BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR,
2520 BCM_PAGE_SIZE, 1, BCM_PAGE_SIZE,
2523 if_printf(ifp, "Could not allocate "
2524 "context block DMA tag!\n");
2528 for (i = 0; i < sc->ctx_pages; i++) {
2529 rc = bus_dmamem_alloc(sc->ctx_tag,
2530 (void **)&sc->ctx_block[i],
2531 BUS_DMA_WAITOK | BUS_DMA_ZERO |
2535 if_printf(ifp, "Could not allocate %dth context "
2536 "DMA memory!\n", i);
2540 rc = bus_dmamap_load(sc->ctx_tag, sc->ctx_map[i],
2541 sc->ctx_block[i], BCM_PAGE_SIZE,
2542 bce_dma_map_addr, &busaddr,
2545 if (rc == EINPROGRESS) {
2546 panic("%s coherent memory loading "
2547 "is still in progress!", ifp->if_xname);
2549 if_printf(ifp, "Could not map %dth context "
2550 "DMA memory!\n", i);
2551 bus_dmamem_free(sc->ctx_tag, sc->ctx_block[i],
2553 sc->ctx_block[i] = NULL;
2556 sc->ctx_paddr[i] = busaddr;
2560 sc->tx_rings = kmalloc_cachealign(
2561 sizeof(struct bce_tx_ring) * sc->tx_ring_cnt, M_DEVBUF,
2563 for (i = 0; i < sc->tx_ring_cnt; ++i) {
2564 sc->tx_rings[i].sc = sc;
2566 sc->tx_rings[i].tx_cid = TX_CID;
2567 sc->tx_rings[i].tx_hw_cons =
2568 &sc->status_block->status_tx_quick_consumer_index0;
2570 struct status_block_msix *sblk =
2571 (struct status_block_msix *)
2572 (((uint8_t *)(sc->status_block)) +
2573 (i * BCE_STATUS_BLK_MSIX_ALIGN));
2575 sc->tx_rings[i].tx_cid = TX_TSS_CID + i - 1;
2576 sc->tx_rings[i].tx_hw_cons =
2577 &sblk->status_tx_quick_consumer_index;
2580 rc = bce_create_tx_ring(&sc->tx_rings[i]);
2582 device_printf(sc->bce_dev,
2583 "can't create %dth tx ring\n", i);
2588 sc->rx_rings = kmalloc_cachealign(
2589 sizeof(struct bce_rx_ring) * sc->rx_ring_cnt, M_DEVBUF,
2591 for (i = 0; i < sc->rx_ring_cnt; ++i) {
2592 sc->rx_rings[i].sc = sc;
2593 sc->rx_rings[i].idx = i;
2595 sc->rx_rings[i].rx_cid = RX_CID;
2596 sc->rx_rings[i].rx_hw_cons =
2597 &sc->status_block->status_rx_quick_consumer_index0;
2598 sc->rx_rings[i].hw_status_idx =
2599 &sc->status_block->status_idx;
2601 struct status_block_msix *sblk =
2602 (struct status_block_msix *)
2603 (((uint8_t *)(sc->status_block)) +
2604 (i * BCE_STATUS_BLK_MSIX_ALIGN));
2606 sc->rx_rings[i].rx_cid = RX_RSS_CID + i - 1;
2607 sc->rx_rings[i].rx_hw_cons =
2608 &sblk->status_rx_quick_consumer_index;
2609 sc->rx_rings[i].hw_status_idx = &sblk->status_idx;
2612 rc = bce_create_rx_ring(&sc->rx_rings[i]);
2614 device_printf(sc->bce_dev,
2615 "can't create %dth rx ring\n", i);
2624 /****************************************************************************/
2625 /* Firmware synchronization. */
2627 /* Before performing certain events such as a chip reset, synchronize with */
2628 /* the firmware first. */
2631 /* 0 for success, positive value for failure. */
2632 /****************************************************************************/
2634 bce_fw_sync(struct bce_softc *sc, uint32_t msg_data)
2639 /* Don't waste any time if we've timed out before. */
2640 if (sc->bce_fw_timed_out)
2643 /* Increment the message sequence number. */
2644 sc->bce_fw_wr_seq++;
2645 msg_data |= sc->bce_fw_wr_seq;
2647 /* Send the message to the bootcode driver mailbox. */
2648 bce_shmem_wr(sc, BCE_DRV_MB, msg_data);
2650 /* Wait for the bootcode to acknowledge the message. */
2651 for (i = 0; i < FW_ACK_TIME_OUT_MS; i++) {
2652 /* Check for a response in the bootcode firmware mailbox. */
2653 val = bce_shmem_rd(sc, BCE_FW_MB);
2654 if ((val & BCE_FW_MSG_ACK) == (msg_data & BCE_DRV_MSG_SEQ))
2659 /* If we've timed out, tell the bootcode that we've stopped waiting. */
2660 if ((val & BCE_FW_MSG_ACK) != (msg_data & BCE_DRV_MSG_SEQ) &&
2661 (msg_data & BCE_DRV_MSG_DATA) != BCE_DRV_MSG_DATA_WAIT0) {
2662 if_printf(&sc->arpcom.ac_if,
2663 "Firmware synchronization timeout! "
2664 "msg_data = 0x%08X\n", msg_data);
2666 msg_data &= ~BCE_DRV_MSG_CODE;
2667 msg_data |= BCE_DRV_MSG_CODE_FW_TIMEOUT;
2669 bce_shmem_wr(sc, BCE_DRV_MB, msg_data);
2671 sc->bce_fw_timed_out = 1;
2678 /****************************************************************************/
2679 /* Load Receive Virtual 2 Physical (RV2P) processor firmware. */
2683 /****************************************************************************/
2685 bce_load_rv2p_fw(struct bce_softc *sc, uint32_t *rv2p_code,
2686 uint32_t rv2p_code_len, uint32_t rv2p_proc)
2691 for (i = 0; i < rv2p_code_len; i += 8) {
2692 REG_WR(sc, BCE_RV2P_INSTR_HIGH, *rv2p_code);
2694 REG_WR(sc, BCE_RV2P_INSTR_LOW, *rv2p_code);
2697 if (rv2p_proc == RV2P_PROC1) {
2698 val = (i / 8) | BCE_RV2P_PROC1_ADDR_CMD_RDWR;
2699 REG_WR(sc, BCE_RV2P_PROC1_ADDR_CMD, val);
2701 val = (i / 8) | BCE_RV2P_PROC2_ADDR_CMD_RDWR;
2702 REG_WR(sc, BCE_RV2P_PROC2_ADDR_CMD, val);
2706 /* Reset the processor, un-stall is done later. */
2707 if (rv2p_proc == RV2P_PROC1)
2708 REG_WR(sc, BCE_RV2P_COMMAND, BCE_RV2P_COMMAND_PROC1_RESET);
2710 REG_WR(sc, BCE_RV2P_COMMAND, BCE_RV2P_COMMAND_PROC2_RESET);
2714 /****************************************************************************/
2715 /* Load RISC processor firmware. */
2717 /* Loads firmware from the file if_bcefw.h into the scratchpad memory */
2718 /* associated with a particular processor. */
2722 /****************************************************************************/
2724 bce_load_cpu_fw(struct bce_softc *sc, struct cpu_reg *cpu_reg,
2730 bce_halt_cpu(sc, cpu_reg);
2732 /* Load the Text area. */
2733 offset = cpu_reg->spad_base + (fw->text_addr - cpu_reg->mips_view_base);
2735 for (j = 0; j < (fw->text_len / 4); j++, offset += 4)
2736 REG_WR_IND(sc, offset, fw->text[j]);
2739 /* Load the Data area. */
2740 offset = cpu_reg->spad_base + (fw->data_addr - cpu_reg->mips_view_base);
2742 for (j = 0; j < (fw->data_len / 4); j++, offset += 4)
2743 REG_WR_IND(sc, offset, fw->data[j]);
2746 /* Load the SBSS area. */
2747 offset = cpu_reg->spad_base + (fw->sbss_addr - cpu_reg->mips_view_base);
2749 for (j = 0; j < (fw->sbss_len / 4); j++, offset += 4)
2750 REG_WR_IND(sc, offset, fw->sbss[j]);
2753 /* Load the BSS area. */
2754 offset = cpu_reg->spad_base + (fw->bss_addr - cpu_reg->mips_view_base);
2756 for (j = 0; j < (fw->bss_len/4); j++, offset += 4)
2757 REG_WR_IND(sc, offset, fw->bss[j]);
2760 /* Load the Read-Only area. */
2761 offset = cpu_reg->spad_base +
2762 (fw->rodata_addr - cpu_reg->mips_view_base);
2764 for (j = 0; j < (fw->rodata_len / 4); j++, offset += 4)
2765 REG_WR_IND(sc, offset, fw->rodata[j]);
2768 /* Clear the pre-fetch instruction and set the FW start address. */
2769 REG_WR_IND(sc, cpu_reg->inst, 0);
2770 REG_WR_IND(sc, cpu_reg->pc, fw->start_addr);
2774 /****************************************************************************/
2775 /* Starts the RISC processor. */
2777 /* Assumes the CPU starting address has already been set. */
2781 /****************************************************************************/
2783 bce_start_cpu(struct bce_softc *sc, struct cpu_reg *cpu_reg)
2787 /* Start the CPU. */
2788 val = REG_RD_IND(sc, cpu_reg->mode);
2789 val &= ~cpu_reg->mode_value_halt;
2790 REG_WR_IND(sc, cpu_reg->state, cpu_reg->state_value_clear);
2791 REG_WR_IND(sc, cpu_reg->mode, val);
2795 /****************************************************************************/
2796 /* Halts the RISC processor. */
2800 /****************************************************************************/
2802 bce_halt_cpu(struct bce_softc *sc, struct cpu_reg *cpu_reg)
2807 val = REG_RD_IND(sc, cpu_reg->mode);
2808 val |= cpu_reg->mode_value_halt;
2809 REG_WR_IND(sc, cpu_reg->mode, val);
2810 REG_WR_IND(sc, cpu_reg->state, cpu_reg->state_value_clear);
2814 /****************************************************************************/
2815 /* Start the RX CPU. */
2819 /****************************************************************************/
2821 bce_start_rxp_cpu(struct bce_softc *sc)
2823 struct cpu_reg cpu_reg;
2825 cpu_reg.mode = BCE_RXP_CPU_MODE;
2826 cpu_reg.mode_value_halt = BCE_RXP_CPU_MODE_SOFT_HALT;
2827 cpu_reg.mode_value_sstep = BCE_RXP_CPU_MODE_STEP_ENA;
2828 cpu_reg.state = BCE_RXP_CPU_STATE;
2829 cpu_reg.state_value_clear = 0xffffff;
2830 cpu_reg.gpr0 = BCE_RXP_CPU_REG_FILE;
2831 cpu_reg.evmask = BCE_RXP_CPU_EVENT_MASK;
2832 cpu_reg.pc = BCE_RXP_CPU_PROGRAM_COUNTER;
2833 cpu_reg.inst = BCE_RXP_CPU_INSTRUCTION;
2834 cpu_reg.bp = BCE_RXP_CPU_HW_BREAKPOINT;
2835 cpu_reg.spad_base = BCE_RXP_SCRATCH;
2836 cpu_reg.mips_view_base = 0x8000000;
2838 bce_start_cpu(sc, &cpu_reg);
2842 /****************************************************************************/
2843 /* Initialize the RX CPU. */
2847 /****************************************************************************/
2849 bce_init_rxp_cpu(struct bce_softc *sc)
2851 struct cpu_reg cpu_reg;
2854 cpu_reg.mode = BCE_RXP_CPU_MODE;
2855 cpu_reg.mode_value_halt = BCE_RXP_CPU_MODE_SOFT_HALT;
2856 cpu_reg.mode_value_sstep = BCE_RXP_CPU_MODE_STEP_ENA;
2857 cpu_reg.state = BCE_RXP_CPU_STATE;
2858 cpu_reg.state_value_clear = 0xffffff;
2859 cpu_reg.gpr0 = BCE_RXP_CPU_REG_FILE;
2860 cpu_reg.evmask = BCE_RXP_CPU_EVENT_MASK;
2861 cpu_reg.pc = BCE_RXP_CPU_PROGRAM_COUNTER;
2862 cpu_reg.inst = BCE_RXP_CPU_INSTRUCTION;
2863 cpu_reg.bp = BCE_RXP_CPU_HW_BREAKPOINT;
2864 cpu_reg.spad_base = BCE_RXP_SCRATCH;
2865 cpu_reg.mips_view_base = 0x8000000;
2867 if (BCE_CHIP_NUM(sc) == BCE_CHIP_NUM_5709 ||
2868 BCE_CHIP_NUM(sc) == BCE_CHIP_NUM_5716) {
2869 fw.ver_major = bce_RXP_b09FwReleaseMajor;
2870 fw.ver_minor = bce_RXP_b09FwReleaseMinor;
2871 fw.ver_fix = bce_RXP_b09FwReleaseFix;
2872 fw.start_addr = bce_RXP_b09FwStartAddr;
2874 fw.text_addr = bce_RXP_b09FwTextAddr;
2875 fw.text_len = bce_RXP_b09FwTextLen;
2877 fw.text = bce_RXP_b09FwText;
2879 fw.data_addr = bce_RXP_b09FwDataAddr;
2880 fw.data_len = bce_RXP_b09FwDataLen;
2882 fw.data = bce_RXP_b09FwData;
2884 fw.sbss_addr = bce_RXP_b09FwSbssAddr;
2885 fw.sbss_len = bce_RXP_b09FwSbssLen;
2887 fw.sbss = bce_RXP_b09FwSbss;
2889 fw.bss_addr = bce_RXP_b09FwBssAddr;
2890 fw.bss_len = bce_RXP_b09FwBssLen;
2892 fw.bss = bce_RXP_b09FwBss;
2894 fw.rodata_addr = bce_RXP_b09FwRodataAddr;
2895 fw.rodata_len = bce_RXP_b09FwRodataLen;
2896 fw.rodata_index = 0;
2897 fw.rodata = bce_RXP_b09FwRodata;
2899 fw.ver_major = bce_RXP_b06FwReleaseMajor;
2900 fw.ver_minor = bce_RXP_b06FwReleaseMinor;
2901 fw.ver_fix = bce_RXP_b06FwReleaseFix;
2902 fw.start_addr = bce_RXP_b06FwStartAddr;
2904 fw.text_addr = bce_RXP_b06FwTextAddr;
2905 fw.text_len = bce_RXP_b06FwTextLen;
2907 fw.text = bce_RXP_b06FwText;
2909 fw.data_addr = bce_RXP_b06FwDataAddr;
2910 fw.data_len = bce_RXP_b06FwDataLen;
2912 fw.data = bce_RXP_b06FwData;
2914 fw.sbss_addr = bce_RXP_b06FwSbssAddr;
2915 fw.sbss_len = bce_RXP_b06FwSbssLen;
2917 fw.sbss = bce_RXP_b06FwSbss;
2919 fw.bss_addr = bce_RXP_b06FwBssAddr;
2920 fw.bss_len = bce_RXP_b06FwBssLen;
2922 fw.bss = bce_RXP_b06FwBss;
2924 fw.rodata_addr = bce_RXP_b06FwRodataAddr;
2925 fw.rodata_len = bce_RXP_b06FwRodataLen;
2926 fw.rodata_index = 0;
2927 fw.rodata = bce_RXP_b06FwRodata;
2930 bce_load_cpu_fw(sc, &cpu_reg, &fw);
2931 /* Delay RXP start until initialization is complete. */
2935 /****************************************************************************/
2936 /* Initialize the TX CPU. */
2940 /****************************************************************************/
2942 bce_init_txp_cpu(struct bce_softc *sc)
2944 struct cpu_reg cpu_reg;
2947 cpu_reg.mode = BCE_TXP_CPU_MODE;
2948 cpu_reg.mode_value_halt = BCE_TXP_CPU_MODE_SOFT_HALT;
2949 cpu_reg.mode_value_sstep = BCE_TXP_CPU_MODE_STEP_ENA;
2950 cpu_reg.state = BCE_TXP_CPU_STATE;
2951 cpu_reg.state_value_clear = 0xffffff;
2952 cpu_reg.gpr0 = BCE_TXP_CPU_REG_FILE;
2953 cpu_reg.evmask = BCE_TXP_CPU_EVENT_MASK;
2954 cpu_reg.pc = BCE_TXP_CPU_PROGRAM_COUNTER;
2955 cpu_reg.inst = BCE_TXP_CPU_INSTRUCTION;
2956 cpu_reg.bp = BCE_TXP_CPU_HW_BREAKPOINT;
2957 cpu_reg.spad_base = BCE_TXP_SCRATCH;
2958 cpu_reg.mips_view_base = 0x8000000;
2960 if (BCE_CHIP_NUM(sc) == BCE_CHIP_NUM_5709 ||
2961 BCE_CHIP_NUM(sc) == BCE_CHIP_NUM_5716) {
2962 fw.ver_major = bce_TXP_b09FwReleaseMajor;
2963 fw.ver_minor = bce_TXP_b09FwReleaseMinor;
2964 fw.ver_fix = bce_TXP_b09FwReleaseFix;
2965 fw.start_addr = bce_TXP_b09FwStartAddr;
2967 fw.text_addr = bce_TXP_b09FwTextAddr;
2968 fw.text_len = bce_TXP_b09FwTextLen;
2970 fw.text = bce_TXP_b09FwText;
2972 fw.data_addr = bce_TXP_b09FwDataAddr;
2973 fw.data_len = bce_TXP_b09FwDataLen;
2975 fw.data = bce_TXP_b09FwData;
2977 fw.sbss_addr = bce_TXP_b09FwSbssAddr;
2978 fw.sbss_len = bce_TXP_b09FwSbssLen;
2980 fw.sbss = bce_TXP_b09FwSbss;
2982 fw.bss_addr = bce_TXP_b09FwBssAddr;
2983 fw.bss_len = bce_TXP_b09FwBssLen;
2985 fw.bss = bce_TXP_b09FwBss;
2987 fw.rodata_addr = bce_TXP_b09FwRodataAddr;
2988 fw.rodata_len = bce_TXP_b09FwRodataLen;
2989 fw.rodata_index = 0;
2990 fw.rodata = bce_TXP_b09FwRodata;
2992 fw.ver_major = bce_TXP_b06FwReleaseMajor;
2993 fw.ver_minor = bce_TXP_b06FwReleaseMinor;
2994 fw.ver_fix = bce_TXP_b06FwReleaseFix;
2995 fw.start_addr = bce_TXP_b06FwStartAddr;
2997 fw.text_addr = bce_TXP_b06FwTextAddr;
2998 fw.text_len = bce_TXP_b06FwTextLen;
3000 fw.text = bce_TXP_b06FwText;
3002 fw.data_addr = bce_TXP_b06FwDataAddr;
3003 fw.data_len = bce_TXP_b06FwDataLen;
3005 fw.data = bce_TXP_b06FwData;
3007 fw.sbss_addr = bce_TXP_b06FwSbssAddr;
3008 fw.sbss_len = bce_TXP_b06FwSbssLen;
3010 fw.sbss = bce_TXP_b06FwSbss;
3012 fw.bss_addr = bce_TXP_b06FwBssAddr;
3013 fw.bss_len = bce_TXP_b06FwBssLen;
3015 fw.bss = bce_TXP_b06FwBss;
3017 fw.rodata_addr = bce_TXP_b06FwRodataAddr;
3018 fw.rodata_len = bce_TXP_b06FwRodataLen;
3019 fw.rodata_index = 0;
3020 fw.rodata = bce_TXP_b06FwRodata;
3023 bce_load_cpu_fw(sc, &cpu_reg, &fw);
3024 bce_start_cpu(sc, &cpu_reg);
3028 /****************************************************************************/
3029 /* Initialize the TPAT CPU. */
3033 /****************************************************************************/
3035 bce_init_tpat_cpu(struct bce_softc *sc)
3037 struct cpu_reg cpu_reg;
3040 cpu_reg.mode = BCE_TPAT_CPU_MODE;
3041 cpu_reg.mode_value_halt = BCE_TPAT_CPU_MODE_SOFT_HALT;
3042 cpu_reg.mode_value_sstep = BCE_TPAT_CPU_MODE_STEP_ENA;
3043 cpu_reg.state = BCE_TPAT_CPU_STATE;
3044 cpu_reg.state_value_clear = 0xffffff;
3045 cpu_reg.gpr0 = BCE_TPAT_CPU_REG_FILE;
3046 cpu_reg.evmask = BCE_TPAT_CPU_EVENT_MASK;
3047 cpu_reg.pc = BCE_TPAT_CPU_PROGRAM_COUNTER;
3048 cpu_reg.inst = BCE_TPAT_CPU_INSTRUCTION;
3049 cpu_reg.bp = BCE_TPAT_CPU_HW_BREAKPOINT;
3050 cpu_reg.spad_base = BCE_TPAT_SCRATCH;
3051 cpu_reg.mips_view_base = 0x8000000;
3053 if (BCE_CHIP_NUM(sc) == BCE_CHIP_NUM_5709 ||
3054 BCE_CHIP_NUM(sc) == BCE_CHIP_NUM_5716) {
3055 fw.ver_major = bce_TPAT_b09FwReleaseMajor;
3056 fw.ver_minor = bce_TPAT_b09FwReleaseMinor;
3057 fw.ver_fix = bce_TPAT_b09FwReleaseFix;
3058 fw.start_addr = bce_TPAT_b09FwStartAddr;
3060 fw.text_addr = bce_TPAT_b09FwTextAddr;
3061 fw.text_len = bce_TPAT_b09FwTextLen;
3063 fw.text = bce_TPAT_b09FwText;
3065 fw.data_addr = bce_TPAT_b09FwDataAddr;
3066 fw.data_len = bce_TPAT_b09FwDataLen;
3068 fw.data = bce_TPAT_b09FwData;
3070 fw.sbss_addr = bce_TPAT_b09FwSbssAddr;
3071 fw.sbss_len = bce_TPAT_b09FwSbssLen;
3073 fw.sbss = bce_TPAT_b09FwSbss;
3075 fw.bss_addr = bce_TPAT_b09FwBssAddr;
3076 fw.bss_len = bce_TPAT_b09FwBssLen;
3078 fw.bss = bce_TPAT_b09FwBss;
3080 fw.rodata_addr = bce_TPAT_b09FwRodataAddr;
3081 fw.rodata_len = bce_TPAT_b09FwRodataLen;
3082 fw.rodata_index = 0;
3083 fw.rodata = bce_TPAT_b09FwRodata;
3085 fw.ver_major = bce_TPAT_b06FwReleaseMajor;
3086 fw.ver_minor = bce_TPAT_b06FwReleaseMinor;
3087 fw.ver_fix = bce_TPAT_b06FwReleaseFix;
3088 fw.start_addr = bce_TPAT_b06FwStartAddr;
3090 fw.text_addr = bce_TPAT_b06FwTextAddr;
3091 fw.text_len = bce_TPAT_b06FwTextLen;
3093 fw.text = bce_TPAT_b06FwText;
3095 fw.data_addr = bce_TPAT_b06FwDataAddr;
3096 fw.data_len = bce_TPAT_b06FwDataLen;
3098 fw.data = bce_TPAT_b06FwData;
3100 fw.sbss_addr = bce_TPAT_b06FwSbssAddr;
3101 fw.sbss_len = bce_TPAT_b06FwSbssLen;
3103 fw.sbss = bce_TPAT_b06FwSbss;
3105 fw.bss_addr = bce_TPAT_b06FwBssAddr;
3106 fw.bss_len = bce_TPAT_b06FwBssLen;
3108 fw.bss = bce_TPAT_b06FwBss;
3110 fw.rodata_addr = bce_TPAT_b06FwRodataAddr;
3111 fw.rodata_len = bce_TPAT_b06FwRodataLen;
3112 fw.rodata_index = 0;
3113 fw.rodata = bce_TPAT_b06FwRodata;
3116 bce_load_cpu_fw(sc, &cpu_reg, &fw);
3117 bce_start_cpu(sc, &cpu_reg);
3121 /****************************************************************************/
3122 /* Initialize the CP CPU. */
3126 /****************************************************************************/
3128 bce_init_cp_cpu(struct bce_softc *sc)
3130 struct cpu_reg cpu_reg;
3133 cpu_reg.mode = BCE_CP_CPU_MODE;
3134 cpu_reg.mode_value_halt = BCE_CP_CPU_MODE_SOFT_HALT;
3135 cpu_reg.mode_value_sstep = BCE_CP_CPU_MODE_STEP_ENA;
3136 cpu_reg.state = BCE_CP_CPU_STATE;
3137 cpu_reg.state_value_clear = 0xffffff;
3138 cpu_reg.gpr0 = BCE_CP_CPU_REG_FILE;
3139 cpu_reg.evmask = BCE_CP_CPU_EVENT_MASK;
3140 cpu_reg.pc = BCE_CP_CPU_PROGRAM_COUNTER;
3141 cpu_reg.inst = BCE_CP_CPU_INSTRUCTION;
3142 cpu_reg.bp = BCE_CP_CPU_HW_BREAKPOINT;
3143 cpu_reg.spad_base = BCE_CP_SCRATCH;
3144 cpu_reg.mips_view_base = 0x8000000;
3146 if (BCE_CHIP_NUM(sc) == BCE_CHIP_NUM_5709 ||
3147 BCE_CHIP_NUM(sc) == BCE_CHIP_NUM_5716) {
3148 fw.ver_major = bce_CP_b09FwReleaseMajor;
3149 fw.ver_minor = bce_CP_b09FwReleaseMinor;
3150 fw.ver_fix = bce_CP_b09FwReleaseFix;
3151 fw.start_addr = bce_CP_b09FwStartAddr;
3153 fw.text_addr = bce_CP_b09FwTextAddr;
3154 fw.text_len = bce_CP_b09FwTextLen;
3156 fw.text = bce_CP_b09FwText;
3158 fw.data_addr = bce_CP_b09FwDataAddr;
3159 fw.data_len = bce_CP_b09FwDataLen;
3161 fw.data = bce_CP_b09FwData;
3163 fw.sbss_addr = bce_CP_b09FwSbssAddr;
3164 fw.sbss_len = bce_CP_b09FwSbssLen;
3166 fw.sbss = bce_CP_b09FwSbss;
3168 fw.bss_addr = bce_CP_b09FwBssAddr;
3169 fw.bss_len = bce_CP_b09FwBssLen;
3171 fw.bss = bce_CP_b09FwBss;
3173 fw.rodata_addr = bce_CP_b09FwRodataAddr;
3174 fw.rodata_len = bce_CP_b09FwRodataLen;
3175 fw.rodata_index = 0;
3176 fw.rodata = bce_CP_b09FwRodata;
3178 fw.ver_major = bce_CP_b06FwReleaseMajor;
3179 fw.ver_minor = bce_CP_b06FwReleaseMinor;
3180 fw.ver_fix = bce_CP_b06FwReleaseFix;
3181 fw.start_addr = bce_CP_b06FwStartAddr;
3183 fw.text_addr = bce_CP_b06FwTextAddr;
3184 fw.text_len = bce_CP_b06FwTextLen;
3186 fw.text = bce_CP_b06FwText;
3188 fw.data_addr = bce_CP_b06FwDataAddr;
3189 fw.data_len = bce_CP_b06FwDataLen;
3191 fw.data = bce_CP_b06FwData;
3193 fw.sbss_addr = bce_CP_b06FwSbssAddr;
3194 fw.sbss_len = bce_CP_b06FwSbssLen;
3196 fw.sbss = bce_CP_b06FwSbss;
3198 fw.bss_addr = bce_CP_b06FwBssAddr;
3199 fw.bss_len = bce_CP_b06FwBssLen;
3201 fw.bss = bce_CP_b06FwBss;
3203 fw.rodata_addr = bce_CP_b06FwRodataAddr;
3204 fw.rodata_len = bce_CP_b06FwRodataLen;
3205 fw.rodata_index = 0;
3206 fw.rodata = bce_CP_b06FwRodata;
3209 bce_load_cpu_fw(sc, &cpu_reg, &fw);
3210 bce_start_cpu(sc, &cpu_reg);
3214 /****************************************************************************/
3215 /* Initialize the COM CPU. */
3219 /****************************************************************************/
3221 bce_init_com_cpu(struct bce_softc *sc)
3223 struct cpu_reg cpu_reg;
3226 cpu_reg.mode = BCE_COM_CPU_MODE;
3227 cpu_reg.mode_value_halt = BCE_COM_CPU_MODE_SOFT_HALT;
3228 cpu_reg.mode_value_sstep = BCE_COM_CPU_MODE_STEP_ENA;
3229 cpu_reg.state = BCE_COM_CPU_STATE;
3230 cpu_reg.state_value_clear = 0xffffff;
3231 cpu_reg.gpr0 = BCE_COM_CPU_REG_FILE;
3232 cpu_reg.evmask = BCE_COM_CPU_EVENT_MASK;
3233 cpu_reg.pc = BCE_COM_CPU_PROGRAM_COUNTER;
3234 cpu_reg.inst = BCE_COM_CPU_INSTRUCTION;
3235 cpu_reg.bp = BCE_COM_CPU_HW_BREAKPOINT;
3236 cpu_reg.spad_base = BCE_COM_SCRATCH;
3237 cpu_reg.mips_view_base = 0x8000000;
3239 if (BCE_CHIP_NUM(sc) == BCE_CHIP_NUM_5709 ||
3240 BCE_CHIP_NUM(sc) == BCE_CHIP_NUM_5716) {
3241 fw.ver_major = bce_COM_b09FwReleaseMajor;
3242 fw.ver_minor = bce_COM_b09FwReleaseMinor;
3243 fw.ver_fix = bce_COM_b09FwReleaseFix;
3244 fw.start_addr = bce_COM_b09FwStartAddr;
3246 fw.text_addr = bce_COM_b09FwTextAddr;
3247 fw.text_len = bce_COM_b09FwTextLen;
3249 fw.text = bce_COM_b09FwText;
3251 fw.data_addr = bce_COM_b09FwDataAddr;
3252 fw.data_len = bce_COM_b09FwDataLen;
3254 fw.data = bce_COM_b09FwData;
3256 fw.sbss_addr = bce_COM_b09FwSbssAddr;
3257 fw.sbss_len = bce_COM_b09FwSbssLen;
3259 fw.sbss = bce_COM_b09FwSbss;
3261 fw.bss_addr = bce_COM_b09FwBssAddr;
3262 fw.bss_len = bce_COM_b09FwBssLen;
3264 fw.bss = bce_COM_b09FwBss;
3266 fw.rodata_addr = bce_COM_b09FwRodataAddr;
3267 fw.rodata_len = bce_COM_b09FwRodataLen;
3268 fw.rodata_index = 0;
3269 fw.rodata = bce_COM_b09FwRodata;
3271 fw.ver_major = bce_COM_b06FwReleaseMajor;
3272 fw.ver_minor = bce_COM_b06FwReleaseMinor;
3273 fw.ver_fix = bce_COM_b06FwReleaseFix;
3274 fw.start_addr = bce_COM_b06FwStartAddr;
3276 fw.text_addr = bce_COM_b06FwTextAddr;
3277 fw.text_len = bce_COM_b06FwTextLen;
3279 fw.text = bce_COM_b06FwText;
3281 fw.data_addr = bce_COM_b06FwDataAddr;
3282 fw.data_len = bce_COM_b06FwDataLen;
3284 fw.data = bce_COM_b06FwData;
3286 fw.sbss_addr = bce_COM_b06FwSbssAddr;
3287 fw.sbss_len = bce_COM_b06FwSbssLen;
3289 fw.sbss = bce_COM_b06FwSbss;
3291 fw.bss_addr = bce_COM_b06FwBssAddr;
3292 fw.bss_len = bce_COM_b06FwBssLen;
3294 fw.bss = bce_COM_b06FwBss;
3296 fw.rodata_addr = bce_COM_b06FwRodataAddr;
3297 fw.rodata_len = bce_COM_b06FwRodataLen;
3298 fw.rodata_index = 0;
3299 fw.rodata = bce_COM_b06FwRodata;
3302 bce_load_cpu_fw(sc, &cpu_reg, &fw);
3303 bce_start_cpu(sc, &cpu_reg);
3307 /****************************************************************************/
3308 /* Initialize the RV2P, RX, TX, TPAT, COM, and CP CPUs. */
3310 /* Loads the firmware for each CPU and starts the CPU. */
3314 /****************************************************************************/
3316 bce_init_cpus(struct bce_softc *sc)
3318 if (BCE_CHIP_NUM(sc) == BCE_CHIP_NUM_5709 ||
3319 BCE_CHIP_NUM(sc) == BCE_CHIP_NUM_5716) {
3320 if (BCE_CHIP_REV(sc) == BCE_CHIP_REV_Ax) {
3321 bce_load_rv2p_fw(sc, bce_xi90_rv2p_proc1,
3322 sizeof(bce_xi90_rv2p_proc1), RV2P_PROC1);
3323 bce_load_rv2p_fw(sc, bce_xi90_rv2p_proc2,
3324 sizeof(bce_xi90_rv2p_proc2), RV2P_PROC2);
3326 bce_load_rv2p_fw(sc, bce_xi_rv2p_proc1,
3327 sizeof(bce_xi_rv2p_proc1), RV2P_PROC1);
3328 bce_load_rv2p_fw(sc, bce_xi_rv2p_proc2,
3329 sizeof(bce_xi_rv2p_proc2), RV2P_PROC2);
3332 bce_load_rv2p_fw(sc, bce_rv2p_proc1,
3333 sizeof(bce_rv2p_proc1), RV2P_PROC1);
3334 bce_load_rv2p_fw(sc, bce_rv2p_proc2,
3335 sizeof(bce_rv2p_proc2), RV2P_PROC2);
3338 bce_init_rxp_cpu(sc);
3339 bce_init_txp_cpu(sc);
3340 bce_init_tpat_cpu(sc);
3341 bce_init_com_cpu(sc);
3342 bce_init_cp_cpu(sc);
3346 /****************************************************************************/
3347 /* Initialize context memory. */
3349 /* Clears the memory associated with each Context ID (CID). */
3353 /****************************************************************************/
3355 bce_init_ctx(struct bce_softc *sc)
3357 if (BCE_CHIP_NUM(sc) == BCE_CHIP_NUM_5709 ||
3358 BCE_CHIP_NUM(sc) == BCE_CHIP_NUM_5716) {
3359 /* DRC: Replace this constant value with a #define. */
3360 int i, retry_cnt = 10;
3364 * BCM5709 context memory may be cached
3365 * in host memory so prepare the host memory
3368 val = BCE_CTX_COMMAND_ENABLED | BCE_CTX_COMMAND_MEM_INIT |
3370 val |= (BCM_PAGE_BITS - 8) << 16;
3371 REG_WR(sc, BCE_CTX_COMMAND, val);
3373 /* Wait for mem init command to complete. */
3374 for (i = 0; i < retry_cnt; i++) {
3375 val = REG_RD(sc, BCE_CTX_COMMAND);
3376 if (!(val & BCE_CTX_COMMAND_MEM_INIT))
3380 if (i == retry_cnt) {
3381 device_printf(sc->bce_dev,
3382 "Context memory initialization failed!\n");
3386 for (i = 0; i < sc->ctx_pages; i++) {
3390 * Set the physical address of the context
3393 REG_WR(sc, BCE_CTX_HOST_PAGE_TBL_DATA0,
3394 BCE_ADDR_LO(sc->ctx_paddr[i] & 0xfffffff0) |
3395 BCE_CTX_HOST_PAGE_TBL_DATA0_VALID);
3396 REG_WR(sc, BCE_CTX_HOST_PAGE_TBL_DATA1,
3397 BCE_ADDR_HI(sc->ctx_paddr[i]));
3398 REG_WR(sc, BCE_CTX_HOST_PAGE_TBL_CTRL,
3399 i | BCE_CTX_HOST_PAGE_TBL_CTRL_WRITE_REQ);
3402 * Verify that the context memory write was successful.
3404 for (j = 0; j < retry_cnt; j++) {
3405 val = REG_RD(sc, BCE_CTX_HOST_PAGE_TBL_CTRL);
3407 BCE_CTX_HOST_PAGE_TBL_CTRL_WRITE_REQ) == 0)
3411 if (j == retry_cnt) {
3412 device_printf(sc->bce_dev,
3413 "Failed to initialize context page!\n");
3418 uint32_t vcid_addr, offset;
3421 * For the 5706/5708, context memory is local to
3422 * the controller, so initialize the controller
3426 vcid_addr = GET_CID_ADDR(96);
3428 vcid_addr -= PHY_CTX_SIZE;
3430 REG_WR(sc, BCE_CTX_VIRT_ADDR, 0);
3431 REG_WR(sc, BCE_CTX_PAGE_TBL, vcid_addr);
3433 for (offset = 0; offset < PHY_CTX_SIZE; offset += 4)
3434 CTX_WR(sc, 0x00, offset, 0);
3436 REG_WR(sc, BCE_CTX_VIRT_ADDR, vcid_addr);
3437 REG_WR(sc, BCE_CTX_PAGE_TBL, vcid_addr);
3444 /****************************************************************************/
3445 /* Fetch the permanent MAC address of the controller. */
3449 /****************************************************************************/
3451 bce_get_mac_addr(struct bce_softc *sc)
3453 uint32_t mac_lo = 0, mac_hi = 0;
3456 * The NetXtreme II bootcode populates various NIC
3457 * power-on and runtime configuration items in a
3458 * shared memory area. The factory configured MAC
3459 * address is available from both NVRAM and the
3460 * shared memory area so we'll read the value from
3461 * shared memory for speed.
3464 mac_hi = bce_shmem_rd(sc, BCE_PORT_HW_CFG_MAC_UPPER);
3465 mac_lo = bce_shmem_rd(sc, BCE_PORT_HW_CFG_MAC_LOWER);
3467 if (mac_lo == 0 && mac_hi == 0) {
3468 if_printf(&sc->arpcom.ac_if, "Invalid Ethernet address!\n");
3470 sc->eaddr[0] = (u_char)(mac_hi >> 8);
3471 sc->eaddr[1] = (u_char)(mac_hi >> 0);
3472 sc->eaddr[2] = (u_char)(mac_lo >> 24);
3473 sc->eaddr[3] = (u_char)(mac_lo >> 16);
3474 sc->eaddr[4] = (u_char)(mac_lo >> 8);
3475 sc->eaddr[5] = (u_char)(mac_lo >> 0);
3480 /****************************************************************************/
3481 /* Program the MAC address. */
3485 /****************************************************************************/
3487 bce_set_mac_addr(struct bce_softc *sc)
3489 const uint8_t *mac_addr = sc->eaddr;
3492 val = (mac_addr[0] << 8) | mac_addr[1];
3493 REG_WR(sc, BCE_EMAC_MAC_MATCH0, val);
3495 val = (mac_addr[2] << 24) |
3496 (mac_addr[3] << 16) |
3497 (mac_addr[4] << 8) |
3499 REG_WR(sc, BCE_EMAC_MAC_MATCH1, val);
3503 /****************************************************************************/
3504 /* Stop the controller. */
3508 /****************************************************************************/
3510 bce_stop(struct bce_softc *sc)
3512 struct ifnet *ifp = &sc->arpcom.ac_if;
3515 ASSERT_IFNET_SERIALIZED_ALL(ifp);
3517 callout_stop(&sc->bce_tick_callout);
3519 /* Disable the transmit/receive blocks. */
3520 REG_WR(sc, BCE_MISC_ENABLE_CLR_BITS, BCE_MISC_ENABLE_CLR_DEFAULT);
3521 REG_RD(sc, BCE_MISC_ENABLE_CLR_BITS);
3524 bce_disable_intr(sc);
3526 ifp->if_flags &= ~IFF_RUNNING;
3527 for (i = 0; i < sc->tx_ring_cnt; ++i) {
3528 ifsq_clr_oactive(sc->tx_rings[i].ifsq);
3529 ifsq_watchdog_stop(&sc->tx_rings[i].tx_watchdog);
3532 /* Free the RX lists. */
3533 for (i = 0; i < sc->rx_ring_cnt; ++i)
3534 bce_free_rx_chain(&sc->rx_rings[i]);
3536 /* Free TX buffers. */
3537 for (i = 0; i < sc->tx_ring_cnt; ++i)
3538 bce_free_tx_chain(&sc->tx_rings[i]);
3541 sc->bce_coalchg_mask = 0;
3546 bce_reset(struct bce_softc *sc, uint32_t reset_code)
3551 /* Wait for pending PCI transactions to complete. */
3552 REG_WR(sc, BCE_MISC_ENABLE_CLR_BITS,
3553 BCE_MISC_ENABLE_CLR_BITS_TX_DMA_ENABLE |
3554 BCE_MISC_ENABLE_CLR_BITS_DMA_ENGINE_ENABLE |
3555 BCE_MISC_ENABLE_CLR_BITS_RX_DMA_ENABLE |
3556 BCE_MISC_ENABLE_CLR_BITS_HOST_COALESCE_ENABLE);
3557 val = REG_RD(sc, BCE_MISC_ENABLE_CLR_BITS);
3561 if (BCE_CHIP_NUM(sc) == BCE_CHIP_NUM_5709 ||
3562 BCE_CHIP_NUM(sc) == BCE_CHIP_NUM_5716) {
3563 val = REG_RD(sc, BCE_MISC_NEW_CORE_CTL);
3564 val &= ~BCE_MISC_NEW_CORE_CTL_DMA_ENABLE;
3565 REG_WR(sc, BCE_MISC_NEW_CORE_CTL, val);
3568 /* Assume bootcode is running. */
3569 sc->bce_fw_timed_out = 0;
3570 sc->bce_drv_cardiac_arrest = 0;
3572 /* Give the firmware a chance to prepare for the reset. */
3573 rc = bce_fw_sync(sc, BCE_DRV_MSG_DATA_WAIT0 | reset_code);
3575 if_printf(&sc->arpcom.ac_if,
3576 "Firmware is not ready for reset\n");
3580 /* Set a firmware reminder that this is a soft reset. */
3581 bce_shmem_wr(sc, BCE_DRV_RESET_SIGNATURE,
3582 BCE_DRV_RESET_SIGNATURE_MAGIC);
3584 /* Dummy read to force the chip to complete all current transactions. */
3585 val = REG_RD(sc, BCE_MISC_ID);
3588 if (BCE_CHIP_NUM(sc) == BCE_CHIP_NUM_5709 ||
3589 BCE_CHIP_NUM(sc) == BCE_CHIP_NUM_5716) {
3590 REG_WR(sc, BCE_MISC_COMMAND, BCE_MISC_COMMAND_SW_RESET);
3591 REG_RD(sc, BCE_MISC_COMMAND);
3594 val = BCE_PCICFG_MISC_CONFIG_REG_WINDOW_ENA |
3595 BCE_PCICFG_MISC_CONFIG_TARGET_MB_WORD_SWAP;
3597 pci_write_config(sc->bce_dev, BCE_PCICFG_MISC_CONFIG, val, 4);
3599 val = BCE_PCICFG_MISC_CONFIG_CORE_RST_REQ |
3600 BCE_PCICFG_MISC_CONFIG_REG_WINDOW_ENA |
3601 BCE_PCICFG_MISC_CONFIG_TARGET_MB_WORD_SWAP;
3602 REG_WR(sc, BCE_PCICFG_MISC_CONFIG, val);
3604 /* Allow up to 30us for reset to complete. */
3605 for (i = 0; i < 10; i++) {
3606 val = REG_RD(sc, BCE_PCICFG_MISC_CONFIG);
3607 if ((val & (BCE_PCICFG_MISC_CONFIG_CORE_RST_REQ |
3608 BCE_PCICFG_MISC_CONFIG_CORE_RST_BSY)) == 0)
3613 /* Check that reset completed successfully. */
3614 if (val & (BCE_PCICFG_MISC_CONFIG_CORE_RST_REQ |
3615 BCE_PCICFG_MISC_CONFIG_CORE_RST_BSY)) {
3616 if_printf(&sc->arpcom.ac_if, "Reset failed!\n");
3621 /* Make sure byte swapping is properly configured. */
3622 val = REG_RD(sc, BCE_PCI_SWAP_DIAG0);
3623 if (val != 0x01020304) {
3624 if_printf(&sc->arpcom.ac_if, "Byte swap is incorrect!\n");
3628 /* Just completed a reset, assume that firmware is running again. */
3629 sc->bce_fw_timed_out = 0;
3630 sc->bce_drv_cardiac_arrest = 0;
3632 /* Wait for the firmware to finish its initialization. */
3633 rc = bce_fw_sync(sc, BCE_DRV_MSG_DATA_WAIT1 | reset_code);
3635 if_printf(&sc->arpcom.ac_if,
3636 "Firmware did not complete initialization!\n");
3639 if (sc->bce_irq_type == PCI_INTR_TYPE_MSIX) {
3640 bce_setup_msix_table(sc);
3641 /* Prevent MSIX table reads and write from timing out */
3642 REG_WR(sc, BCE_MISC_ECO_HW_CTL,
3643 BCE_MISC_ECO_HW_CTL_LARGE_GRC_TMOUT_EN);
3651 bce_chipinit(struct bce_softc *sc)
3656 /* Make sure the interrupt is not active. */
3657 REG_WR(sc, BCE_PCICFG_INT_ACK_CMD, BCE_PCICFG_INT_ACK_CMD_MASK_INT);
3658 REG_RD(sc, BCE_PCICFG_INT_ACK_CMD);
3661 * Initialize DMA byte/word swapping, configure the number of DMA
3662 * channels and PCI clock compensation delay.
3664 val = BCE_DMA_CONFIG_DATA_BYTE_SWAP |
3665 BCE_DMA_CONFIG_DATA_WORD_SWAP |
3666 #if BYTE_ORDER == BIG_ENDIAN
3667 BCE_DMA_CONFIG_CNTL_BYTE_SWAP |
3669 BCE_DMA_CONFIG_CNTL_WORD_SWAP |
3670 DMA_READ_CHANS << 12 |
3671 DMA_WRITE_CHANS << 16;
3673 val |= (0x2 << 20) | BCE_DMA_CONFIG_CNTL_PCI_COMP_DLY;
3675 if ((sc->bce_flags & BCE_PCIX_FLAG) && sc->bus_speed_mhz == 133)
3676 val |= BCE_DMA_CONFIG_PCI_FAST_CLK_CMP;
3679 * This setting resolves a problem observed on certain Intel PCI
3680 * chipsets that cannot handle multiple outstanding DMA operations.
3681 * See errata E9_5706A1_65.
3683 if (BCE_CHIP_NUM(sc) == BCE_CHIP_NUM_5706 &&
3684 BCE_CHIP_ID(sc) != BCE_CHIP_ID_5706_A0 &&
3685 !(sc->bce_flags & BCE_PCIX_FLAG))
3686 val |= BCE_DMA_CONFIG_CNTL_PING_PONG_DMA;
3688 REG_WR(sc, BCE_DMA_CONFIG, val);
3690 /* Enable the RX_V2P and Context state machines before access. */
3691 REG_WR(sc, BCE_MISC_ENABLE_SET_BITS,
3692 BCE_MISC_ENABLE_SET_BITS_HOST_COALESCE_ENABLE |
3693 BCE_MISC_ENABLE_STATUS_BITS_RX_V2P_ENABLE |
3694 BCE_MISC_ENABLE_STATUS_BITS_CONTEXT_ENABLE);
3696 /* Initialize context mapping and zero out the quick contexts. */
3697 rc = bce_init_ctx(sc);
3701 /* Initialize the on-boards CPUs */
3704 /* Enable management frames (NC-SI) to flow to the MCP. */
3705 if (sc->bce_flags & BCE_MFW_ENABLE_FLAG) {
3706 val = REG_RD(sc, BCE_RPM_MGMT_PKT_CTRL) |
3707 BCE_RPM_MGMT_PKT_CTRL_MGMT_EN;
3708 REG_WR(sc, BCE_RPM_MGMT_PKT_CTRL, val);
3711 /* Prepare NVRAM for access. */
3712 rc = bce_init_nvram(sc);
3716 /* Set the kernel bypass block size */
3717 val = REG_RD(sc, BCE_MQ_CONFIG);
3718 val &= ~BCE_MQ_CONFIG_KNL_BYP_BLK_SIZE;
3719 val |= BCE_MQ_CONFIG_KNL_BYP_BLK_SIZE_256;
3721 /* Enable bins used on the 5709/5716. */
3722 if (BCE_CHIP_NUM(sc) == BCE_CHIP_NUM_5709 ||
3723 BCE_CHIP_NUM(sc) == BCE_CHIP_NUM_5716) {
3724 val |= BCE_MQ_CONFIG_BIN_MQ_MODE;
3725 if (BCE_CHIP_ID(sc) == BCE_CHIP_ID_5709_A1)
3726 val |= BCE_MQ_CONFIG_HALT_DIS;
3729 REG_WR(sc, BCE_MQ_CONFIG, val);
3731 val = 0x10000 + (MAX_CID_CNT * MB_KERNEL_CTX_SIZE);
3732 REG_WR(sc, BCE_MQ_KNL_BYP_WIND_START, val);
3733 REG_WR(sc, BCE_MQ_KNL_WIND_END, val);
3735 /* Set the page size and clear the RV2P processor stall bits. */
3736 val = (BCM_PAGE_BITS - 8) << 24;
3737 REG_WR(sc, BCE_RV2P_CONFIG, val);
3739 /* Configure page size. */
3740 val = REG_RD(sc, BCE_TBDR_CONFIG);
3741 val &= ~BCE_TBDR_CONFIG_PAGE_SIZE;
3742 val |= (BCM_PAGE_BITS - 8) << 24 | 0x40;
3743 REG_WR(sc, BCE_TBDR_CONFIG, val);
3745 /* Set the perfect match control register to default. */
3746 REG_WR_IND(sc, BCE_RXP_PM_CTRL, 0);
3752 /****************************************************************************/
3753 /* Initialize the controller in preparation to send/receive traffic. */
3756 /* 0 for success, positive value for failure. */
3757 /****************************************************************************/
3759 bce_blockinit(struct bce_softc *sc)
3764 /* Load the hardware default MAC address. */
3765 bce_set_mac_addr(sc);
3767 /* Set the Ethernet backoff seed value */
3768 val = sc->eaddr[0] + (sc->eaddr[1] << 8) + (sc->eaddr[2] << 16) +
3769 sc->eaddr[3] + (sc->eaddr[4] << 8) + (sc->eaddr[5] << 16);
3770 REG_WR(sc, BCE_EMAC_BACKOFF_SEED, val);
3772 sc->rx_mode = BCE_EMAC_RX_MODE_SORT_MODE;
3774 /* Set up link change interrupt generation. */
3775 REG_WR(sc, BCE_EMAC_ATTENTION_ENA, BCE_EMAC_ATTENTION_ENA_LINK);
3777 /* Program the physical address of the status block. */
3778 REG_WR(sc, BCE_HC_STATUS_ADDR_L, BCE_ADDR_LO(sc->status_block_paddr));
3779 REG_WR(sc, BCE_HC_STATUS_ADDR_H, BCE_ADDR_HI(sc->status_block_paddr));
3781 /* Program the physical address of the statistics block. */
3782 REG_WR(sc, BCE_HC_STATISTICS_ADDR_L,
3783 BCE_ADDR_LO(sc->stats_block_paddr));
3784 REG_WR(sc, BCE_HC_STATISTICS_ADDR_H,
3785 BCE_ADDR_HI(sc->stats_block_paddr));
3787 /* Program various host coalescing parameters. */
3788 REG_WR(sc, BCE_HC_TX_QUICK_CONS_TRIP,
3789 (sc->bce_tx_quick_cons_trip_int << 16) |
3790 sc->bce_tx_quick_cons_trip);
3791 REG_WR(sc, BCE_HC_RX_QUICK_CONS_TRIP,
3792 (sc->bce_rx_quick_cons_trip_int << 16) |
3793 sc->bce_rx_quick_cons_trip);
3794 REG_WR(sc, BCE_HC_COMP_PROD_TRIP,
3795 (sc->bce_comp_prod_trip_int << 16) | sc->bce_comp_prod_trip);
3796 REG_WR(sc, BCE_HC_TX_TICKS,
3797 (sc->bce_tx_ticks_int << 16) | sc->bce_tx_ticks);
3798 REG_WR(sc, BCE_HC_RX_TICKS,
3799 (sc->bce_rx_ticks_int << 16) | sc->bce_rx_ticks);
3800 REG_WR(sc, BCE_HC_COM_TICKS,
3801 (sc->bce_com_ticks_int << 16) | sc->bce_com_ticks);
3802 REG_WR(sc, BCE_HC_CMD_TICKS,
3803 (sc->bce_cmd_ticks_int << 16) | sc->bce_cmd_ticks);
3804 REG_WR(sc, BCE_HC_STATS_TICKS, (sc->bce_stats_ticks & 0xffff00));
3805 REG_WR(sc, BCE_HC_STAT_COLLECT_TICKS, 0xbb8); /* 3ms */
3807 if (sc->bce_irq_type == PCI_INTR_TYPE_MSIX)
3808 REG_WR(sc, BCE_HC_MSIX_BIT_VECTOR, BCE_HC_MSIX_BIT_VECTOR_VAL);
3810 val = BCE_HC_CONFIG_TX_TMR_MODE | BCE_HC_CONFIG_COLLECT_STATS;
3811 if ((sc->bce_flags & BCE_ONESHOT_MSI_FLAG) ||
3812 sc->bce_irq_type == PCI_INTR_TYPE_MSIX) {
3814 if (sc->bce_irq_type == PCI_INTR_TYPE_MSIX) {
3815 if_printf(&sc->arpcom.ac_if,
3818 if_printf(&sc->arpcom.ac_if,
3819 "using oneshot MSI\n");
3822 val |= BCE_HC_CONFIG_ONE_SHOT | BCE_HC_CONFIG_USE_INT_PARAM;
3823 if (sc->bce_irq_type == PCI_INTR_TYPE_MSIX)
3824 val |= BCE_HC_CONFIG_SB_ADDR_INC_128B;
3826 REG_WR(sc, BCE_HC_CONFIG, val);
3828 for (i = 1; i < sc->rx_ring_cnt; ++i) {
3831 base = ((i - 1) * BCE_HC_SB_CONFIG_SIZE) + BCE_HC_SB_CONFIG_1;
3832 KKASSERT(base <= BCE_HC_SB_CONFIG_8);
3835 BCE_HC_SB_CONFIG_1_TX_TMR_MODE |
3836 /* BCE_HC_SB_CONFIG_1_RX_TMR_MODE | */
3837 BCE_HC_SB_CONFIG_1_ONE_SHOT);
3839 REG_WR(sc, base + BCE_HC_TX_QUICK_CONS_TRIP_OFF,
3840 (sc->bce_tx_quick_cons_trip_int << 16) |
3841 sc->bce_tx_quick_cons_trip);
3842 REG_WR(sc, base + BCE_HC_RX_QUICK_CONS_TRIP_OFF,
3843 (sc->bce_rx_quick_cons_trip_int << 16) |
3844 sc->bce_rx_quick_cons_trip);
3845 REG_WR(sc, base + BCE_HC_TX_TICKS_OFF,
3846 (sc->bce_tx_ticks_int << 16) | sc->bce_tx_ticks);
3847 REG_WR(sc, base + BCE_HC_RX_TICKS_OFF,
3848 (sc->bce_rx_ticks_int << 16) | sc->bce_rx_ticks);
3851 /* Clear the internal statistics counters. */
3852 REG_WR(sc, BCE_HC_COMMAND, BCE_HC_COMMAND_CLR_STAT_NOW);
3854 /* Verify that bootcode is running. */
3855 reg = bce_shmem_rd(sc, BCE_DEV_INFO_SIGNATURE);
3857 if ((reg & BCE_DEV_INFO_SIGNATURE_MAGIC_MASK) !=
3858 BCE_DEV_INFO_SIGNATURE_MAGIC) {
3859 if_printf(&sc->arpcom.ac_if,
3860 "Bootcode not running! Found: 0x%08X, "
3861 "Expected: 08%08X\n",
3862 reg & BCE_DEV_INFO_SIGNATURE_MAGIC_MASK,
3863 BCE_DEV_INFO_SIGNATURE_MAGIC);
3868 if (BCE_CHIP_NUM(sc) == BCE_CHIP_NUM_5709 ||
3869 BCE_CHIP_NUM(sc) == BCE_CHIP_NUM_5716) {
3870 val = REG_RD(sc, BCE_MISC_NEW_CORE_CTL);
3871 val |= BCE_MISC_NEW_CORE_CTL_DMA_ENABLE;
3872 REG_WR(sc, BCE_MISC_NEW_CORE_CTL, val);
3875 /* Allow bootcode to apply any additional fixes before enabling MAC. */
3876 bce_fw_sync(sc, BCE_DRV_MSG_DATA_WAIT2 | BCE_DRV_MSG_CODE_RESET);
3878 /* Enable link state change interrupt generation. */
3879 REG_WR(sc, BCE_HC_ATTN_BITS_ENABLE, STATUS_ATTN_BITS_LINK_STATE);
3881 /* Enable the RXP. */
3882 bce_start_rxp_cpu(sc);
3884 /* Disable management frames (NC-SI) from flowing to the MCP. */
3885 if (sc->bce_flags & BCE_MFW_ENABLE_FLAG) {
3886 val = REG_RD(sc, BCE_RPM_MGMT_PKT_CTRL) &
3887 ~BCE_RPM_MGMT_PKT_CTRL_MGMT_EN;
3888 REG_WR(sc, BCE_RPM_MGMT_PKT_CTRL, val);
3891 /* Enable all remaining blocks in the MAC. */
3892 if (BCE_CHIP_NUM(sc) == BCE_CHIP_NUM_5709 ||
3893 BCE_CHIP_NUM(sc) == BCE_CHIP_NUM_5716) {
3894 REG_WR(sc, BCE_MISC_ENABLE_SET_BITS,
3895 BCE_MISC_ENABLE_DEFAULT_XI);
3897 REG_WR(sc, BCE_MISC_ENABLE_SET_BITS, BCE_MISC_ENABLE_DEFAULT);
3899 REG_RD(sc, BCE_MISC_ENABLE_SET_BITS);
3902 /* Save the current host coalescing block settings. */
3903 sc->hc_command = REG_RD(sc, BCE_HC_COMMAND);
3909 /****************************************************************************/
3910 /* Encapsulate an mbuf cluster into the rx_bd chain. */
3912 /* The NetXtreme II can support Jumbo frames by using multiple rx_bd's. */
3913 /* This routine will map an mbuf cluster into 1 or more rx_bd's as */
3917 /* 0 for success, positive value for failure. */
3918 /****************************************************************************/
3920 bce_newbuf_std(struct bce_rx_ring *rxr, uint16_t *prod, uint16_t *chain_prod,
3921 uint32_t *prod_bseq, int init)
3924 bus_dma_segment_t seg;
3928 /* This is a new mbuf allocation. */
3929 m_new = m_getcl(init ? MB_WAIT : MB_DONTWAIT, MT_DATA, M_PKTHDR);
3933 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
3935 /* Map the mbuf cluster into device memory. */
3936 error = bus_dmamap_load_mbuf_segment(rxr->rx_mbuf_tag,
3937 rxr->rx_mbuf_tmpmap, m_new, &seg, 1, &nseg, BUS_DMA_NOWAIT);
3941 if_printf(&rxr->sc->arpcom.ac_if,
3942 "Error mapping mbuf into RX chain!\n");
3947 if (rxr->rx_mbuf_ptr[*chain_prod] != NULL) {
3948 bus_dmamap_unload(rxr->rx_mbuf_tag,
3949 rxr->rx_mbuf_map[*chain_prod]);
3952 map = rxr->rx_mbuf_map[*chain_prod];
3953 rxr->rx_mbuf_map[*chain_prod] = rxr->rx_mbuf_tmpmap;
3954 rxr->rx_mbuf_tmpmap = map;
3956 /* Save the mbuf and update our counter. */
3957 rxr->rx_mbuf_ptr[*chain_prod] = m_new;
3958 rxr->rx_mbuf_paddr[*chain_prod] = seg.ds_addr;
3961 bce_setup_rxdesc_std(rxr, *chain_prod, prod_bseq);
3968 bce_setup_rxdesc_std(struct bce_rx_ring *rxr, uint16_t chain_prod,
3969 uint32_t *prod_bseq)
3975 paddr = rxr->rx_mbuf_paddr[chain_prod];
3976 len = rxr->rx_mbuf_ptr[chain_prod]->m_len;
3978 /* Setup the rx_bd for the first segment. */
3979 rxbd = &rxr->rx_bd_chain[RX_PAGE(chain_prod)][RX_IDX(chain_prod)];
3981 rxbd->rx_bd_haddr_lo = htole32(BCE_ADDR_LO(paddr));
3982 rxbd->rx_bd_haddr_hi = htole32(BCE_ADDR_HI(paddr));
3983 rxbd->rx_bd_len = htole32(len);
3984 rxbd->rx_bd_flags = htole32(RX_BD_FLAGS_START);
3987 rxbd->rx_bd_flags |= htole32(RX_BD_FLAGS_END);
3991 /****************************************************************************/
3992 /* Initialize the TX context memory. */
3996 /****************************************************************************/
3998 bce_init_tx_context(struct bce_tx_ring *txr)
4002 /* Initialize the context ID for an L2 TX chain. */
4003 if (BCE_CHIP_NUM(txr->sc) == BCE_CHIP_NUM_5709 ||
4004 BCE_CHIP_NUM(txr->sc) == BCE_CHIP_NUM_5716) {
4005 /* Set the CID type to support an L2 connection. */
4006 val = BCE_L2CTX_TX_TYPE_TYPE_L2 | BCE_L2CTX_TX_TYPE_SIZE_L2;
4007 CTX_WR(txr->sc, GET_CID_ADDR(txr->tx_cid),
4008 BCE_L2CTX_TX_TYPE_XI, val);
4009 val = BCE_L2CTX_TX_CMD_TYPE_TYPE_L2 | (8 << 16);
4010 CTX_WR(txr->sc, GET_CID_ADDR(txr->tx_cid),
4011 BCE_L2CTX_TX_CMD_TYPE_XI, val);
4013 /* Point the hardware to the first page in the chain. */
4014 val = BCE_ADDR_HI(txr->tx_bd_chain_paddr[0]);
4015 CTX_WR(txr->sc, GET_CID_ADDR(txr->tx_cid),
4016 BCE_L2CTX_TX_TBDR_BHADDR_HI_XI, val);
4017 val = BCE_ADDR_LO(txr->tx_bd_chain_paddr[0]);
4018 CTX_WR(txr->sc, GET_CID_ADDR(txr->tx_cid),
4019 BCE_L2CTX_TX_TBDR_BHADDR_LO_XI, val);
4021 /* Set the CID type to support an L2 connection. */
4022 val = BCE_L2CTX_TX_TYPE_TYPE_L2 | BCE_L2CTX_TX_TYPE_SIZE_L2;
4023 CTX_WR(txr->sc, GET_CID_ADDR(txr->tx_cid),
4024 BCE_L2CTX_TX_TYPE, val);
4025 val = BCE_L2CTX_TX_CMD_TYPE_TYPE_L2 | (8 << 16);
4026 CTX_WR(txr->sc, GET_CID_ADDR(txr->tx_cid),
4027 BCE_L2CTX_TX_CMD_TYPE, val);
4029 /* Point the hardware to the first page in the chain. */
4030 val = BCE_ADDR_HI(txr->tx_bd_chain_paddr[0]);
4031 CTX_WR(txr->sc, GET_CID_ADDR(txr->tx_cid),
4032 BCE_L2CTX_TX_TBDR_BHADDR_HI, val);
4033 val = BCE_ADDR_LO(txr->tx_bd_chain_paddr[0]);
4034 CTX_WR(txr->sc, GET_CID_ADDR(txr->tx_cid),
4035 BCE_L2CTX_TX_TBDR_BHADDR_LO, val);
4040 /****************************************************************************/
4041 /* Allocate memory and initialize the TX data structures. */
4044 /* 0 for success, positive value for failure. */
4045 /****************************************************************************/
4047 bce_init_tx_chain(struct bce_tx_ring *txr)
4052 /* Set the initial TX producer/consumer indices. */
4055 txr->tx_prod_bseq = 0;
4056 txr->used_tx_bd = 0;
4057 txr->max_tx_bd = USABLE_TX_BD(txr);
4060 * The NetXtreme II supports a linked-list structre called
4061 * a Buffer Descriptor Chain (or BD chain). A BD chain
4062 * consists of a series of 1 or more chain pages, each of which
4063 * consists of a fixed number of BD entries.
4064 * The last BD entry on each page is a pointer to the next page
4065 * in the chain, and the last pointer in the BD chain
4066 * points back to the beginning of the chain.
4069 /* Set the TX next pointer chain entries. */
4070 for (i = 0; i < txr->tx_pages; i++) {
4073 txbd = &txr->tx_bd_chain[i][USABLE_TX_BD_PER_PAGE];
4075 /* Check if we've reached the last page. */
4076 if (i == (txr->tx_pages - 1))
4081 txbd->tx_bd_haddr_hi =
4082 htole32(BCE_ADDR_HI(txr->tx_bd_chain_paddr[j]));
4083 txbd->tx_bd_haddr_lo =
4084 htole32(BCE_ADDR_LO(txr->tx_bd_chain_paddr[j]));
4086 bce_init_tx_context(txr);
4092 /****************************************************************************/
4093 /* Free memory and clear the TX data structures. */
4097 /****************************************************************************/
4099 bce_free_tx_chain(struct bce_tx_ring *txr)
4103 /* Unmap, unload, and free any mbufs still in the TX mbuf chain. */
4104 for (i = 0; i < TOTAL_TX_BD(txr); i++) {
4105 if (txr->tx_mbuf_ptr[i] != NULL) {
4106 bus_dmamap_unload(txr->tx_mbuf_tag,
4107 txr->tx_mbuf_map[i]);
4108 m_freem(txr->tx_mbuf_ptr[i]);
4109 txr->tx_mbuf_ptr[i] = NULL;
4113 /* Clear each TX chain page. */
4114 for (i = 0; i < txr->tx_pages; i++)
4115 bzero(txr->tx_bd_chain[i], BCE_TX_CHAIN_PAGE_SZ);
4116 txr->used_tx_bd = 0;
4120 /****************************************************************************/
4121 /* Initialize the RX context memory. */
4125 /****************************************************************************/
4127 bce_init_rx_context(struct bce_rx_ring *rxr)
4131 /* Initialize the context ID for an L2 RX chain. */
4132 val = BCE_L2CTX_RX_CTX_TYPE_CTX_BD_CHN_TYPE_VALUE |
4133 BCE_L2CTX_RX_CTX_TYPE_SIZE_L2 | (0x02 << 8);
4136 * Set the level for generating pause frames
4137 * when the number of available rx_bd's gets
4138 * too low (the low watermark) and the level
4139 * when pause frames can be stopped (the high
4142 if (BCE_CHIP_NUM(rxr->sc) == BCE_CHIP_NUM_5709 ||
4143 BCE_CHIP_NUM(rxr->sc) == BCE_CHIP_NUM_5716) {
4144 uint32_t lo_water, hi_water;
4146 lo_water = BCE_L2CTX_RX_LO_WATER_MARK_DEFAULT;
4147 hi_water = USABLE_RX_BD(rxr) / 4;
4149 lo_water /= BCE_L2CTX_RX_LO_WATER_MARK_SCALE;
4150 hi_water /= BCE_L2CTX_RX_HI_WATER_MARK_SCALE;
4154 else if (hi_water == 0)
4157 (hi_water << BCE_L2CTX_RX_HI_WATER_MARK_SHIFT);
4160 CTX_WR(rxr->sc, GET_CID_ADDR(rxr->rx_cid),
4161 BCE_L2CTX_RX_CTX_TYPE, val);
4163 /* Setup the MQ BIN mapping for l2_ctx_host_bseq. */
4164 if (BCE_CHIP_NUM(rxr->sc) == BCE_CHIP_NUM_5709 ||
4165 BCE_CHIP_NUM(rxr->sc) == BCE_CHIP_NUM_5716) {
4166 val = REG_RD(rxr->sc, BCE_MQ_MAP_L2_5);
4167 REG_WR(rxr->sc, BCE_MQ_MAP_L2_5, val | BCE_MQ_MAP_L2_5_ARM);
4170 /* Point the hardware to the first page in the chain. */
4171 val = BCE_ADDR_HI(rxr->rx_bd_chain_paddr[0]);
4172 CTX_WR(rxr->sc, GET_CID_ADDR(rxr->rx_cid),
4173 BCE_L2CTX_RX_NX_BDHADDR_HI, val);
4174 val = BCE_ADDR_LO(rxr->rx_bd_chain_paddr[0]);
4175 CTX_WR(rxr->sc, GET_CID_ADDR(rxr->rx_cid),
4176 BCE_L2CTX_RX_NX_BDHADDR_LO, val);
4180 /****************************************************************************/
4181 /* Allocate memory and initialize the RX data structures. */
4184 /* 0 for success, positive value for failure. */
4185 /****************************************************************************/
4187 bce_init_rx_chain(struct bce_rx_ring *rxr)
4191 uint16_t prod, chain_prod;
4194 /* Initialize the RX producer and consumer indices. */
4197 rxr->rx_prod_bseq = 0;
4198 rxr->free_rx_bd = USABLE_RX_BD(rxr);
4199 rxr->max_rx_bd = USABLE_RX_BD(rxr);
4201 /* Clear cache status index */
4202 rxr->last_status_idx = 0;
4204 /* Initialize the RX next pointer chain entries. */
4205 for (i = 0; i < rxr->rx_pages; i++) {
4208 rxbd = &rxr->rx_bd_chain[i][USABLE_RX_BD_PER_PAGE];
4210 /* Check if we've reached the last page. */
4211 if (i == (rxr->rx_pages - 1))
4216 /* Setup the chain page pointers. */
4217 rxbd->rx_bd_haddr_hi =
4218 htole32(BCE_ADDR_HI(rxr->rx_bd_chain_paddr[j]));
4219 rxbd->rx_bd_haddr_lo =
4220 htole32(BCE_ADDR_LO(rxr->rx_bd_chain_paddr[j]));
4223 /* Allocate mbuf clusters for the rx_bd chain. */
4224 prod = prod_bseq = 0;
4225 while (prod < TOTAL_RX_BD(rxr)) {
4226 chain_prod = RX_CHAIN_IDX(rxr, prod);
4227 if (bce_newbuf_std(rxr, &prod, &chain_prod, &prod_bseq, 1)) {
4228 if_printf(&rxr->sc->arpcom.ac_if,
4229 "Error filling RX chain: rx_bd[0x%04X]!\n",
4234 prod = NEXT_RX_BD(prod);
4237 /* Save the RX chain producer index. */
4238 rxr->rx_prod = prod;
4239 rxr->rx_prod_bseq = prod_bseq;
4241 /* Tell the chip about the waiting rx_bd's. */
4242 REG_WR16(rxr->sc, MB_GET_CID_ADDR(rxr->rx_cid) + BCE_L2MQ_RX_HOST_BDIDX,
4244 REG_WR(rxr->sc, MB_GET_CID_ADDR(rxr->rx_cid) + BCE_L2MQ_RX_HOST_BSEQ,
4247 bce_init_rx_context(rxr);
4253 /****************************************************************************/
4254 /* Free memory and clear the RX data structures. */
4258 /****************************************************************************/
4260 bce_free_rx_chain(struct bce_rx_ring *rxr)
4264 /* Free any mbufs still in the RX mbuf chain. */
4265 for (i = 0; i < TOTAL_RX_BD(rxr); i++) {
4266 if (rxr->rx_mbuf_ptr[i] != NULL) {
4267 bus_dmamap_unload(rxr->rx_mbuf_tag,
4268 rxr->rx_mbuf_map[i]);
4269 m_freem(rxr->rx_mbuf_ptr[i]);
4270 rxr->rx_mbuf_ptr[i] = NULL;
4274 /* Clear each RX chain page. */
4275 for (i = 0; i < rxr->rx_pages; i++)
4276 bzero(rxr->rx_bd_chain[i], BCE_RX_CHAIN_PAGE_SZ);
4280 /****************************************************************************/
4281 /* Set media options. */
4284 /* 0 for success, positive value for failure. */
4285 /****************************************************************************/
4287 bce_ifmedia_upd(struct ifnet *ifp)
4289 struct bce_softc *sc = ifp->if_softc;
4290 struct mii_data *mii = device_get_softc(sc->bce_miibus);
4294 * 'mii' will be NULL, when this function is called on following
4295 * code path: bce_attach() -> bce_mgmt_init()
4298 /* Make sure the MII bus has been enumerated. */
4300 if (mii->mii_instance) {
4301 struct mii_softc *miisc;
4303 LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
4304 mii_phy_reset(miisc);
4306 error = mii_mediachg(mii);
4312 /****************************************************************************/
4313 /* Reports current media status. */
4317 /****************************************************************************/
4319 bce_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
4321 struct bce_softc *sc = ifp->if_softc;
4322 struct mii_data *mii = device_get_softc(sc->bce_miibus);
4325 ifmr->ifm_active = mii->mii_media_active;
4326 ifmr->ifm_status = mii->mii_media_status;
4330 /****************************************************************************/
4331 /* Handles PHY generated interrupt events. */
4335 /****************************************************************************/
4337 bce_phy_intr(struct bce_softc *sc)
4339 uint32_t new_link_state, old_link_state;
4340 struct ifnet *ifp = &sc->arpcom.ac_if;
4342 ASSERT_SERIALIZED(&sc->main_serialize);
4344 new_link_state = sc->status_block->status_attn_bits &
4345 STATUS_ATTN_BITS_LINK_STATE;
4346 old_link_state = sc->status_block->status_attn_bits_ack &
4347 STATUS_ATTN_BITS_LINK_STATE;
4349 /* Handle any changes if the link state has changed. */
4350 if (new_link_state != old_link_state) { /* XXX redundant? */
4351 /* Update the status_attn_bits_ack field in the status block. */
4352 if (new_link_state) {
4353 REG_WR(sc, BCE_PCICFG_STATUS_BIT_SET_CMD,
4354 STATUS_ATTN_BITS_LINK_STATE);
4356 if_printf(ifp, "Link is now UP.\n");
4358 REG_WR(sc, BCE_PCICFG_STATUS_BIT_CLEAR_CMD,
4359 STATUS_ATTN_BITS_LINK_STATE);
4361 if_printf(ifp, "Link is now DOWN.\n");
4365 * Assume link is down and allow tick routine to
4366 * update the state based on the actual media state.
4369 callout_stop(&sc->bce_tick_callout);
4370 bce_tick_serialized(sc);
4373 /* Acknowledge the link change interrupt. */
4374 REG_WR(sc, BCE_EMAC_STATUS, BCE_EMAC_STATUS_LINK_CHANGE);
4378 /****************************************************************************/
4379 /* Reads the receive consumer value from the status block (skipping over */
4380 /* chain page pointer if necessary). */
4384 /****************************************************************************/
4385 static __inline uint16_t
4386 bce_get_hw_rx_cons(struct bce_rx_ring *rxr)
4388 uint16_t hw_cons = *rxr->rx_hw_cons;
4390 if ((hw_cons & USABLE_RX_BD_PER_PAGE) == USABLE_RX_BD_PER_PAGE)
4396 /****************************************************************************/
4397 /* Handles received frame interrupt events. */
4401 /****************************************************************************/
4403 bce_rx_intr(struct bce_rx_ring *rxr, int count, uint16_t hw_cons)
4405 struct ifnet *ifp = &rxr->sc->arpcom.ac_if;
4406 uint16_t sw_cons, sw_chain_cons, sw_prod, sw_chain_prod;
4407 uint32_t sw_prod_bseq;
4409 ASSERT_SERIALIZED(&rxr->rx_serialize);
4411 /* Get working copies of the driver's view of the RX indices. */
4412 sw_cons = rxr->rx_cons;
4413 sw_prod = rxr->rx_prod;
4414 sw_prod_bseq = rxr->rx_prod_bseq;
4416 /* Scan through the receive chain as long as there is work to do. */
4417 while (sw_cons != hw_cons) {
4418 struct mbuf *m = NULL;
4419 struct l2_fhdr *l2fhdr = NULL;
4421 uint32_t status = 0;
4423 #ifdef IFPOLL_ENABLE
4424 if (count >= 0 && count-- == 0)
4429 * Convert the producer/consumer indices
4430 * to an actual rx_bd index.
4432 sw_chain_cons = RX_CHAIN_IDX(rxr, sw_cons);
4433 sw_chain_prod = RX_CHAIN_IDX(rxr, sw_prod);
4437 /* The mbuf is stored with the last rx_bd entry of a packet. */
4438 if (rxr->rx_mbuf_ptr[sw_chain_cons] != NULL) {
4439 if (sw_chain_cons != sw_chain_prod) {
4440 if_printf(ifp, "RX cons(%d) != prod(%d), "
4441 "drop!\n", sw_chain_cons, sw_chain_prod);
4442 IFNET_STAT_INC(ifp, ierrors, 1);
4444 bce_setup_rxdesc_std(rxr, sw_chain_cons,
4447 goto bce_rx_int_next_rx;
4450 /* Unmap the mbuf from DMA space. */
4451 bus_dmamap_sync(rxr->rx_mbuf_tag,
4452 rxr->rx_mbuf_map[sw_chain_cons],
4453 BUS_DMASYNC_POSTREAD);
4455 /* Save the mbuf from the driver's chain. */
4456 m = rxr->rx_mbuf_ptr[sw_chain_cons];
4459 * Frames received on the NetXteme II are prepended
4460 * with an l2_fhdr structure which provides status
4461 * information about the received frame (including
4462 * VLAN tags and checksum info). The frames are also
4463 * automatically adjusted to align the IP header
4464 * (i.e. two null bytes are inserted before the
4465 * Ethernet header). As a result the data DMA'd by
4466 * the controller into the mbuf is as follows:
4468 * +---------+-----+---------------------+-----+
4469 * | l2_fhdr | pad | packet data | FCS |
4470 * +---------+-----+---------------------+-----+
4472 * The l2_fhdr needs to be checked and skipped and the
4473 * FCS needs to be stripped before sending the packet
4476 l2fhdr = mtod(m, struct l2_fhdr *);
4478 len = l2fhdr->l2_fhdr_pkt_len;
4479 status = l2fhdr->l2_fhdr_status;
4481 len -= ETHER_CRC_LEN;
4483 /* Check the received frame for errors. */
4484 if (status & (L2_FHDR_ERRORS_BAD_CRC |
4485 L2_FHDR_ERRORS_PHY_DECODE |
4486 L2_FHDR_ERRORS_ALIGNMENT |
4487 L2_FHDR_ERRORS_TOO_SHORT |
4488 L2_FHDR_ERRORS_GIANT_FRAME)) {
4489 IFNET_STAT_INC(ifp, ierrors, 1);
4491 /* Reuse the mbuf for a new frame. */
4492 bce_setup_rxdesc_std(rxr, sw_chain_prod,
4495 goto bce_rx_int_next_rx;
4499 * Get a new mbuf for the rx_bd. If no new
4500 * mbufs are available then reuse the current mbuf,
4501 * log an ierror on the interface, and generate
4502 * an error in the system log.
4504 if (bce_newbuf_std(rxr, &sw_prod, &sw_chain_prod,
4505 &sw_prod_bseq, 0)) {
4506 IFNET_STAT_INC(ifp, ierrors, 1);
4508 /* Try and reuse the exisitng mbuf. */
4509 bce_setup_rxdesc_std(rxr, sw_chain_prod,
4512 goto bce_rx_int_next_rx;
4516 * Skip over the l2_fhdr when passing
4517 * the data up the stack.
4519 m_adj(m, sizeof(struct l2_fhdr) + ETHER_ALIGN);
4521 m->m_pkthdr.len = m->m_len = len;
4522 m->m_pkthdr.rcvif = ifp;
4524 /* Validate the checksum if offload enabled. */
4525 if (ifp->if_capenable & IFCAP_RXCSUM) {
4526 /* Check for an IP datagram. */
4527 if (status & L2_FHDR_STATUS_IP_DATAGRAM) {
4528 m->m_pkthdr.csum_flags |=
4531 /* Check if the IP checksum is valid. */
4532 if ((l2fhdr->l2_fhdr_ip_xsum ^
4534 m->m_pkthdr.csum_flags |=
4539 /* Check for a valid TCP/UDP frame. */
4540 if (status & (L2_FHDR_STATUS_TCP_SEGMENT |
4541 L2_FHDR_STATUS_UDP_DATAGRAM)) {
4543 /* Check for a good TCP/UDP checksum. */
4545 (L2_FHDR_ERRORS_TCP_XSUM |
4546 L2_FHDR_ERRORS_UDP_XSUM)) == 0) {
4547 m->m_pkthdr.csum_data =
4548 l2fhdr->l2_fhdr_tcp_udp_xsum;
4549 m->m_pkthdr.csum_flags |=
4556 IFNET_STAT_INC(ifp, ipackets, 1);
4558 sw_prod = NEXT_RX_BD(sw_prod);
4561 sw_cons = NEXT_RX_BD(sw_cons);
4563 /* If we have a packet, pass it up the stack */
4565 if (status & L2_FHDR_STATUS_L2_VLAN_TAG) {
4566 m->m_flags |= M_VLANTAG;
4567 m->m_pkthdr.ether_vlantag =
4568 l2fhdr->l2_fhdr_vlan_tag;
4570 ifp->if_input(ifp, m);
4575 rxr->rx_cons = sw_cons;
4576 rxr->rx_prod = sw_prod;
4577 rxr->rx_prod_bseq = sw_prod_bseq;
4579 REG_WR16(rxr->sc, MB_GET_CID_ADDR(rxr->rx_cid) + BCE_L2MQ_RX_HOST_BDIDX,
4581 REG_WR(rxr->sc, MB_GET_CID_ADDR(rxr->rx_cid) + BCE_L2MQ_RX_HOST_BSEQ,
4586 /****************************************************************************/
4587 /* Reads the transmit consumer value from the status block (skipping over */
4588 /* chain page pointer if necessary). */
4592 /****************************************************************************/
4593 static __inline uint16_t
4594 bce_get_hw_tx_cons(struct bce_tx_ring *txr)
4596 uint16_t hw_cons = *txr->tx_hw_cons;
4598 if ((hw_cons & USABLE_TX_BD_PER_PAGE) == USABLE_TX_BD_PER_PAGE)
4604 /****************************************************************************/
4605 /* Handles transmit completion interrupt events. */
4609 /****************************************************************************/
4611 bce_tx_intr(struct bce_tx_ring *txr, uint16_t hw_tx_cons)
4613 struct ifnet *ifp = &txr->sc->arpcom.ac_if;
4614 uint16_t sw_tx_cons, sw_tx_chain_cons;
4616 ASSERT_SERIALIZED(&txr->tx_serialize);
4618 /* Get the hardware's view of the TX consumer index. */
4619 sw_tx_cons = txr->tx_cons;
4621 /* Cycle through any completed TX chain page entries. */
4622 while (sw_tx_cons != hw_tx_cons) {
4623 sw_tx_chain_cons = TX_CHAIN_IDX(txr, sw_tx_cons);
4626 * Free the associated mbuf. Remember
4627 * that only the last tx_bd of a packet
4628 * has an mbuf pointer and DMA map.
4630 if (txr->tx_mbuf_ptr[sw_tx_chain_cons] != NULL) {
4631 /* Unmap the mbuf. */
4632 bus_dmamap_unload(txr->tx_mbuf_tag,
4633 txr->tx_mbuf_map[sw_tx_chain_cons]);
4635 /* Free the mbuf. */
4636 m_freem(txr->tx_mbuf_ptr[sw_tx_chain_cons]);
4637 txr->tx_mbuf_ptr[sw_tx_chain_cons] = NULL;
4639 IFNET_STAT_INC(ifp, opackets, 1);
4640 #ifdef BCE_TSS_DEBUG
4646 sw_tx_cons = NEXT_TX_BD(sw_tx_cons);
4649 if (txr->used_tx_bd == 0) {
4650 /* Clear the TX timeout timer. */
4651 txr->tx_watchdog.wd_timer = 0;
4654 /* Clear the tx hardware queue full flag. */
4655 if (txr->max_tx_bd - txr->used_tx_bd >= BCE_TX_SPARE_SPACE)
4656 ifsq_clr_oactive(txr->ifsq);
4657 txr->tx_cons = sw_tx_cons;
4661 /****************************************************************************/
4662 /* Disables interrupt generation. */
4666 /****************************************************************************/
4668 bce_disable_intr(struct bce_softc *sc)
4672 for (i = 0; i < sc->rx_ring_cnt; ++i) {
4673 REG_WR(sc, BCE_PCICFG_INT_ACK_CMD,
4674 (sc->rx_rings[i].idx << 24) |
4675 BCE_PCICFG_INT_ACK_CMD_MASK_INT);
4677 REG_RD(sc, BCE_PCICFG_INT_ACK_CMD);
4679 callout_stop(&sc->bce_ckmsi_callout);
4680 sc->bce_msi_maylose = FALSE;
4681 sc->bce_check_rx_cons = 0;
4682 sc->bce_check_tx_cons = 0;
4683 sc->bce_check_status_idx = 0xffff;
4685 for (i = 0; i < sc->rx_ring_cnt; ++i)
4686 lwkt_serialize_handler_disable(sc->bce_msix[i].msix_serialize);
4690 /****************************************************************************/
4691 /* Enables interrupt generation. */
4695 /****************************************************************************/
4697 bce_enable_intr(struct bce_softc *sc)
4701 for (i = 0; i < sc->rx_ring_cnt; ++i)
4702 lwkt_serialize_handler_enable(sc->bce_msix[i].msix_serialize);
4704 for (i = 0; i < sc->rx_ring_cnt; ++i) {
4705 struct bce_rx_ring *rxr = &sc->rx_rings[i];
4707 REG_WR(sc, BCE_PCICFG_INT_ACK_CMD, (rxr->idx << 24) |
4708 BCE_PCICFG_INT_ACK_CMD_INDEX_VALID |
4709 BCE_PCICFG_INT_ACK_CMD_MASK_INT |
4710 rxr->last_status_idx);
4711 REG_WR(sc, BCE_PCICFG_INT_ACK_CMD, (rxr->idx << 24) |
4712 BCE_PCICFG_INT_ACK_CMD_INDEX_VALID |
4713 rxr->last_status_idx);
4715 REG_WR(sc, BCE_HC_COMMAND, sc->hc_command | BCE_HC_COMMAND_COAL_NOW);
4717 if (sc->bce_flags & BCE_CHECK_MSI_FLAG) {
4718 sc->bce_msi_maylose = FALSE;
4719 sc->bce_check_rx_cons = 0;
4720 sc->bce_check_tx_cons = 0;
4721 sc->bce_check_status_idx = 0xffff;
4724 if_printf(&sc->arpcom.ac_if, "check msi\n");
4726 callout_reset_bycpu(&sc->bce_ckmsi_callout, BCE_MSI_CKINTVL,
4727 bce_check_msi, sc, sc->bce_msix[0].msix_cpuid);
4732 /****************************************************************************/
4733 /* Reenables interrupt generation during interrupt handling. */
4737 /****************************************************************************/
4739 bce_reenable_intr(struct bce_rx_ring *rxr)
4741 REG_WR(rxr->sc, BCE_PCICFG_INT_ACK_CMD, (rxr->idx << 24) |
4742 BCE_PCICFG_INT_ACK_CMD_INDEX_VALID | rxr->last_status_idx);
4746 /****************************************************************************/
4747 /* Handles controller initialization. */
4751 /****************************************************************************/
4755 struct bce_softc *sc = xsc;
4756 struct ifnet *ifp = &sc->arpcom.ac_if;
4761 ASSERT_IFNET_SERIALIZED_ALL(ifp);
4763 /* Check if the driver is still running and bail out if it is. */
4764 if (ifp->if_flags & IFF_RUNNING)
4769 error = bce_reset(sc, BCE_DRV_MSG_CODE_RESET);
4771 if_printf(ifp, "Controller reset failed!\n");
4775 error = bce_chipinit(sc);
4777 if_printf(ifp, "Controller initialization failed!\n");
4781 error = bce_blockinit(sc);
4783 if_printf(ifp, "Block initialization failed!\n");
4787 /* Load our MAC address. */
4788 bcopy(IF_LLADDR(ifp), sc->eaddr, ETHER_ADDR_LEN);
4789 bce_set_mac_addr(sc);
4791 /* Calculate and program the Ethernet MTU size. */
4792 ether_mtu = ETHER_HDR_LEN + EVL_ENCAPLEN + ifp->if_mtu + ETHER_CRC_LEN;
4795 * Program the mtu, enabling jumbo frame
4796 * support if necessary. Also set the mbuf
4797 * allocation count for RX frames.
4799 if (ether_mtu > ETHER_MAX_LEN + EVL_ENCAPLEN) {
4801 REG_WR(sc, BCE_EMAC_RX_MTU_SIZE,
4802 min(ether_mtu, BCE_MAX_JUMBO_ETHER_MTU) |
4803 BCE_EMAC_RX_MTU_SIZE_JUMBO_ENA);
4805 panic("jumbo buffer is not supported yet");
4808 REG_WR(sc, BCE_EMAC_RX_MTU_SIZE, ether_mtu);
4811 /* Program appropriate promiscuous/multicast filtering. */
4812 bce_set_rx_mode(sc);
4815 * Init RX buffer descriptor chain.
4817 REG_WR(sc, BCE_RLUP_RSS_CONFIG, 0);
4818 bce_reg_wr_ind(sc, BCE_RXP_SCRATCH_RSS_TBL_SZ, 0);
4820 for (i = 0; i < sc->rx_ring_cnt; ++i)
4821 bce_init_rx_chain(&sc->rx_rings[i]); /* XXX return value */
4823 if (sc->rx_ring_cnt > 1)
4827 * Init TX buffer descriptor chain.
4829 REG_WR(sc, BCE_TSCH_TSS_CFG, 0);
4831 for (i = 0; i < sc->tx_ring_cnt; ++i)
4832 bce_init_tx_chain(&sc->tx_rings[i]);
4834 if (sc->tx_ring_cnt > 1) {
4835 REG_WR(sc, BCE_TSCH_TSS_CFG,
4836 ((sc->tx_ring_cnt - 1) << 24) | (TX_TSS_CID << 7));
4840 #ifdef IFPOLL_ENABLE
4841 if (ifp->if_flags & IFF_NPOLLING)
4846 /* Disable interrupts if we are polling. */
4847 bce_disable_intr(sc);
4849 /* Change coalesce parameters */
4850 bce_npoll_coal_change(sc);
4852 /* Enable host interrupts. */
4853 bce_enable_intr(sc);
4855 bce_set_timer_cpuid(sc, polling);
4857 bce_ifmedia_upd(ifp);
4859 ifp->if_flags |= IFF_RUNNING;
4860 for (i = 0; i < sc->tx_ring_cnt; ++i) {
4861 ifsq_clr_oactive(sc->tx_rings[i].ifsq);
4862 ifsq_watchdog_start(&sc->tx_rings[i].tx_watchdog);
4865 callout_reset_bycpu(&sc->bce_tick_callout, hz, bce_tick, sc,
4866 sc->bce_timer_cpuid);
4873 /****************************************************************************/
4874 /* Initialize the controller just enough so that any management firmware */
4875 /* running on the device will continue to operate corectly. */
4879 /****************************************************************************/
4881 bce_mgmt_init(struct bce_softc *sc)
4883 struct ifnet *ifp = &sc->arpcom.ac_if;
4885 /* Bail out if management firmware is not running. */
4886 if (!(sc->bce_flags & BCE_MFW_ENABLE_FLAG))
4889 /* Enable all critical blocks in the MAC. */
4890 if (BCE_CHIP_NUM(sc) == BCE_CHIP_NUM_5709 ||
4891 BCE_CHIP_NUM(sc) == BCE_CHIP_NUM_5716) {
4892 REG_WR(sc, BCE_MISC_ENABLE_SET_BITS,
4893 BCE_MISC_ENABLE_DEFAULT_XI);
4895 REG_WR(sc, BCE_MISC_ENABLE_SET_BITS, BCE_MISC_ENABLE_DEFAULT);
4897 REG_RD(sc, BCE_MISC_ENABLE_SET_BITS);
4900 bce_ifmedia_upd(ifp);
4904 /****************************************************************************/
4905 /* Encapsultes an mbuf cluster into the tx_bd chain structure and makes the */
4906 /* memory visible to the controller. */
4909 /* 0 for success, positive value for failure. */
4910 /****************************************************************************/
4912 bce_encap(struct bce_tx_ring *txr, struct mbuf **m_head, int *nsegs_used)
4914 bus_dma_segment_t segs[BCE_MAX_SEGMENTS];
4915 bus_dmamap_t map, tmp_map;
4916 struct mbuf *m0 = *m_head;
4917 struct tx_bd *txbd = NULL;
4918 uint16_t vlan_tag = 0, flags = 0, mss = 0;
4919 uint16_t chain_prod, chain_prod_start, prod;
4921 int i, error, maxsegs, nsegs;
4923 /* Transfer any checksum offload flags to the bd. */
4924 if (m0->m_pkthdr.csum_flags & CSUM_TSO) {
4925 error = bce_tso_setup(txr, m_head, &flags, &mss);
4929 } else if (m0->m_pkthdr.csum_flags & BCE_CSUM_FEATURES) {
4930 if (m0->m_pkthdr.csum_flags & CSUM_IP)
4931 flags |= TX_BD_FLAGS_IP_CKSUM;
4932 if (m0->m_pkthdr.csum_flags & (CSUM_TCP | CSUM_UDP))
4933 flags |= TX_BD_FLAGS_TCP_UDP_CKSUM;
4936 /* Transfer any VLAN tags to the bd. */
4937 if (m0->m_flags & M_VLANTAG) {
4938 flags |= TX_BD_FLAGS_VLAN_TAG;
4939 vlan_tag = m0->m_pkthdr.ether_vlantag;
4942 prod = txr->tx_prod;
4943 chain_prod_start = chain_prod = TX_CHAIN_IDX(txr, prod);
4945 /* Map the mbuf into DMAable memory. */
4946 map = txr->tx_mbuf_map[chain_prod_start];
4948 maxsegs = txr->max_tx_bd - txr->used_tx_bd;
4949 KASSERT(maxsegs >= BCE_TX_SPARE_SPACE,
4950 ("not enough segments %d", maxsegs));
4951 if (maxsegs > BCE_MAX_SEGMENTS)
4952 maxsegs = BCE_MAX_SEGMENTS;
4954 /* Map the mbuf into our DMA address space. */
4955 error = bus_dmamap_load_mbuf_defrag(txr->tx_mbuf_tag, map, m_head,
4956 segs, maxsegs, &nsegs, BUS_DMA_NOWAIT);
4959 bus_dmamap_sync(txr->tx_mbuf_tag, map, BUS_DMASYNC_PREWRITE);
4961 *nsegs_used += nsegs;
4966 /* prod points to an empty tx_bd at this point. */
4967 prod_bseq = txr->tx_prod_bseq;
4970 * Cycle through each mbuf segment that makes up
4971 * the outgoing frame, gathering the mapping info
4972 * for that segment and creating a tx_bd to for
4975 for (i = 0; i < nsegs; i++) {
4976 chain_prod = TX_CHAIN_IDX(txr, prod);
4978 &txr->tx_bd_chain[TX_PAGE(chain_prod)][TX_IDX(chain_prod)];
4980 txbd->tx_bd_haddr_lo = htole32(BCE_ADDR_LO(segs[i].ds_addr));
4981 txbd->tx_bd_haddr_hi = htole32(BCE_ADDR_HI(segs[i].ds_addr));
4982 txbd->tx_bd_mss_nbytes = htole32(mss << 16) |
4983 htole16(segs[i].ds_len);
4984 txbd->tx_bd_vlan_tag = htole16(vlan_tag);
4985 txbd->tx_bd_flags = htole16(flags);
4987 prod_bseq += segs[i].ds_len;
4989 txbd->tx_bd_flags |= htole16(TX_BD_FLAGS_START);
4990 prod = NEXT_TX_BD(prod);
4993 /* Set the END flag on the last TX buffer descriptor. */
4994 txbd->tx_bd_flags |= htole16(TX_BD_FLAGS_END);
4997 * Ensure that the mbuf pointer for this transmission
4998 * is placed at the array index of the last
4999 * descriptor in this chain. This is done
5000 * because a single map is used for all
5001 * segments of the mbuf and we don't want to
5002 * unload the map before all of the segments
5005 txr->tx_mbuf_ptr[chain_prod] = m0;
5007 tmp_map = txr->tx_mbuf_map[chain_prod];
5008 txr->tx_mbuf_map[chain_prod] = map;
5009 txr->tx_mbuf_map[chain_prod_start] = tmp_map;
5011 txr->used_tx_bd += nsegs;
5013 /* prod points to the next free tx_bd at this point. */
5014 txr->tx_prod = prod;
5015 txr->tx_prod_bseq = prod_bseq;
5026 bce_xmit(struct bce_tx_ring *txr)
5028 /* Start the transmit. */
5029 REG_WR16(txr->sc, MB_GET_CID_ADDR(txr->tx_cid) + BCE_L2CTX_TX_HOST_BIDX,
5031 REG_WR(txr->sc, MB_GET_CID_ADDR(txr->tx_cid) + BCE_L2CTX_TX_HOST_BSEQ,
5036 /****************************************************************************/
5037 /* Main transmit routine when called from another routine with a lock. */
5041 /****************************************************************************/
5043 bce_start(struct ifnet *ifp, struct ifaltq_subque *ifsq)
5045 struct bce_softc *sc = ifp->if_softc;
5046 struct bce_tx_ring *txr = ifsq_get_priv(ifsq);
5049 KKASSERT(txr->ifsq == ifsq);
5050 ASSERT_SERIALIZED(&txr->tx_serialize);
5052 /* If there's no link or the transmit queue is empty then just exit. */
5053 if (!sc->bce_link) {
5058 if ((ifp->if_flags & IFF_RUNNING) == 0 || ifsq_is_oactive(ifsq))
5062 struct mbuf *m_head;
5065 * We keep BCE_TX_SPARE_SPACE entries, so bce_encap() is
5068 if (txr->max_tx_bd - txr->used_tx_bd < BCE_TX_SPARE_SPACE) {
5069 ifsq_set_oactive(ifsq);
5073 /* Check for any frames to send. */
5074 m_head = ifsq_dequeue(ifsq, NULL);
5079 * Pack the data into the transmit ring. If we
5080 * don't have room, place the mbuf back at the
5081 * head of the queue and set the OACTIVE flag
5082 * to wait for the NIC to drain the chain.
5084 if (bce_encap(txr, &m_head, &count)) {
5085 IFNET_STAT_INC(ifp, oerrors, 1);
5086 if (txr->used_tx_bd == 0) {
5089 ifsq_set_oactive(ifsq);
5094 if (count >= txr->tx_wreg) {
5099 /* Send a copy of the frame to any BPF listeners. */
5100 ETHER_BPF_MTAP(ifp, m_head);
5102 /* Set the tx timeout. */
5103 txr->tx_watchdog.wd_timer = BCE_TX_TIMEOUT;
5110 /****************************************************************************/
5111 /* Handles any IOCTL calls from the operating system. */
5114 /* 0 for success, positive value for failure. */
5115 /****************************************************************************/
5117 bce_ioctl(struct ifnet *ifp, u_long command, caddr_t data, struct ucred *cr)
5119 struct bce_softc *sc = ifp->if_softc;
5120 struct ifreq *ifr = (struct ifreq *)data;
5121 struct mii_data *mii;
5122 int mask, error = 0;
5124 ASSERT_IFNET_SERIALIZED_ALL(ifp);
5128 /* Check that the MTU setting is supported. */
5129 if (ifr->ifr_mtu < BCE_MIN_MTU ||
5131 ifr->ifr_mtu > BCE_MAX_JUMBO_MTU
5133 ifr->ifr_mtu > ETHERMTU
5140 ifp->if_mtu = ifr->ifr_mtu;
5141 ifp->if_flags &= ~IFF_RUNNING; /* Force reinitialize */
5146 if (ifp->if_flags & IFF_UP) {
5147 if (ifp->if_flags & IFF_RUNNING) {
5148 mask = ifp->if_flags ^ sc->bce_if_flags;
5150 if (mask & (IFF_PROMISC | IFF_ALLMULTI))
5151 bce_set_rx_mode(sc);
5155 } else if (ifp->if_flags & IFF_RUNNING) {
5158 /* If MFW is running, restart the controller a bit. */
5159 if (sc->bce_flags & BCE_MFW_ENABLE_FLAG) {
5160 bce_reset(sc, BCE_DRV_MSG_CODE_RESET);
5165 sc->bce_if_flags = ifp->if_flags;
5170 if (ifp->if_flags & IFF_RUNNING)
5171 bce_set_rx_mode(sc);
5176 mii = device_get_softc(sc->bce_miibus);
5177 error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
5181 mask = ifr->ifr_reqcap ^ ifp->if_capenable;
5182 if (mask & IFCAP_HWCSUM) {
5183 ifp->if_capenable ^= (mask & IFCAP_HWCSUM);
5184 if (ifp->if_capenable & IFCAP_TXCSUM)
5185 ifp->if_hwassist |= BCE_CSUM_FEATURES;
5187 ifp->if_hwassist &= ~BCE_CSUM_FEATURES;
5189 if (mask & IFCAP_TSO) {
5190 ifp->if_capenable ^= IFCAP_TSO;
5191 if (ifp->if_capenable & IFCAP_TSO)
5192 ifp->if_hwassist |= CSUM_TSO;
5194 ifp->if_hwassist &= ~CSUM_TSO;
5199 error = ether_ioctl(ifp, command, data);
5206 /****************************************************************************/
5207 /* Transmit timeout handler. */
5211 /****************************************************************************/
5213 bce_watchdog(struct ifaltq_subque *ifsq)
5215 struct ifnet *ifp = ifsq_get_ifp(ifsq);
5216 struct bce_softc *sc = ifp->if_softc;
5219 ASSERT_IFNET_SERIALIZED_ALL(ifp);
5222 * If we are in this routine because of pause frames, then
5223 * don't reset the hardware.
5225 if (REG_RD(sc, BCE_EMAC_TX_STATUS) & BCE_EMAC_TX_STATUS_XOFFED)
5228 if_printf(ifp, "Watchdog timeout occurred, resetting!\n");
5230 ifp->if_flags &= ~IFF_RUNNING; /* Force reinitialize */
5233 IFNET_STAT_INC(ifp, oerrors, 1);
5235 for (i = 0; i < sc->tx_ring_cnt; ++i)
5236 ifsq_devstart_sched(sc->tx_rings[i].ifsq);
5240 #ifdef IFPOLL_ENABLE
5243 bce_npoll_status(struct ifnet *ifp)
5245 struct bce_softc *sc = ifp->if_softc;
5246 struct status_block *sblk = sc->status_block;
5247 uint32_t status_attn_bits;
5249 ASSERT_SERIALIZED(&sc->main_serialize);
5251 status_attn_bits = sblk->status_attn_bits;
5253 /* Was it a link change interrupt? */
5254 if ((status_attn_bits & STATUS_ATTN_BITS_LINK_STATE) !=
5255 (sblk->status_attn_bits_ack & STATUS_ATTN_BITS_LINK_STATE)) {
5259 * Clear any transient status updates during link state change.
5261 REG_WR(sc, BCE_HC_COMMAND,
5262 sc->hc_command | BCE_HC_COMMAND_COAL_NOW_WO_INT);
5263 REG_RD(sc, BCE_HC_COMMAND);
5267 * If any other attention is asserted then the chip is toast.
5269 if ((status_attn_bits & ~STATUS_ATTN_BITS_LINK_STATE) !=
5270 (sblk->status_attn_bits_ack & ~STATUS_ATTN_BITS_LINK_STATE)) {
5271 if_printf(ifp, "Fatal attention detected: 0x%08X\n",
5272 sblk->status_attn_bits);
5273 bce_serialize_skipmain(sc);
5275 bce_deserialize_skipmain(sc);
5280 bce_npoll_rx(struct ifnet *ifp, void *arg, int count)
5282 struct bce_rx_ring *rxr = arg;
5283 uint16_t hw_rx_cons;
5285 ASSERT_SERIALIZED(&rxr->rx_serialize);
5288 * Save the status block index value for use when enabling
5291 rxr->last_status_idx = *rxr->hw_status_idx;
5293 /* Make sure status index is extracted before RX/TX cons */
5296 hw_rx_cons = bce_get_hw_rx_cons(rxr);
5298 /* Check for any completed RX frames. */
5299 if (hw_rx_cons != rxr->rx_cons)
5300 bce_rx_intr(rxr, count, hw_rx_cons);
5304 bce_npoll_rx_pack(struct ifnet *ifp, void *arg, int count)
5306 struct bce_rx_ring *rxr = arg;
5308 KASSERT(rxr->idx == 0, ("not the first RX ring, but %d", rxr->idx));
5309 bce_npoll_rx(ifp, rxr, count);
5311 KASSERT(rxr->sc->rx_ring_cnt != rxr->sc->rx_ring_cnt2,
5312 ("RX ring count %d, count2 %d", rxr->sc->rx_ring_cnt,
5313 rxr->sc->rx_ring_cnt2));
5315 /* Last ring carries packets whose masked hash is 0 */
5316 rxr = &rxr->sc->rx_rings[rxr->sc->rx_ring_cnt - 1];
5318 lwkt_serialize_enter(&rxr->rx_serialize);
5319 bce_npoll_rx(ifp, rxr, count);
5320 lwkt_serialize_exit(&rxr->rx_serialize);
5324 bce_npoll_tx(struct ifnet *ifp, void *arg, int count __unused)
5326 struct bce_tx_ring *txr = arg;
5327 uint16_t hw_tx_cons;
5329 ASSERT_SERIALIZED(&txr->tx_serialize);
5331 hw_tx_cons = bce_get_hw_tx_cons(txr);
5333 /* Check for any completed TX frames. */
5334 if (hw_tx_cons != txr->tx_cons) {
5335 bce_tx_intr(txr, hw_tx_cons);
5336 if (!ifsq_is_empty(txr->ifsq))
5337 ifsq_devstart(txr->ifsq);
5342 bce_npoll(struct ifnet *ifp, struct ifpoll_info *info)
5344 struct bce_softc *sc = ifp->if_softc;
5347 ASSERT_IFNET_SERIALIZED_ALL(ifp);
5350 info->ifpi_status.status_func = bce_npoll_status;
5351 info->ifpi_status.serializer = &sc->main_serialize;
5353 for (i = 0; i < sc->tx_ring_cnt; ++i) {
5354 struct bce_tx_ring *txr = &sc->tx_rings[i];
5355 int idx = i + sc->npoll_ofs;
5357 KKASSERT(idx < ncpus2);
5358 info->ifpi_tx[idx].poll_func = bce_npoll_tx;
5359 info->ifpi_tx[idx].arg = txr;
5360 info->ifpi_tx[idx].serializer = &txr->tx_serialize;
5361 ifsq_set_cpuid(txr->ifsq, idx);
5364 for (i = 0; i < sc->rx_ring_cnt2; ++i) {
5365 struct bce_rx_ring *rxr = &sc->rx_rings[i];
5366 int idx = i + sc->npoll_ofs;
5368 KKASSERT(idx < ncpus2);
5369 if (i == 0 && sc->rx_ring_cnt2 != sc->rx_ring_cnt) {
5371 * If RSS is enabled, the packets whose
5372 * masked hash are 0 are queued to the
5373 * last RX ring; piggyback the last RX
5374 * ring's processing in the first RX
5375 * polling handler. (see also: comment
5376 * in bce_setup_ring_cnt())
5379 if_printf(ifp, "npoll pack last "
5380 "RX ring on cpu%d\n", idx);
5382 info->ifpi_rx[idx].poll_func =
5385 info->ifpi_rx[idx].poll_func = bce_npoll_rx;
5387 info->ifpi_rx[idx].arg = rxr;
5388 info->ifpi_rx[idx].serializer = &rxr->rx_serialize;
5391 if (ifp->if_flags & IFF_RUNNING) {
5392 bce_set_timer_cpuid(sc, TRUE);
5393 bce_disable_intr(sc);
5394 bce_npoll_coal_change(sc);
5397 for (i = 0; i < sc->tx_ring_cnt; ++i) {
5398 ifsq_set_cpuid(sc->tx_rings[i].ifsq,
5399 sc->bce_msix[i].msix_cpuid);
5402 if (ifp->if_flags & IFF_RUNNING) {
5403 bce_set_timer_cpuid(sc, FALSE);
5404 bce_enable_intr(sc);
5406 sc->bce_coalchg_mask |= BCE_COALMASK_TX_BDS_INT |
5407 BCE_COALMASK_RX_BDS_INT;
5408 bce_coal_change(sc);
5413 #endif /* IFPOLL_ENABLE */
5417 * Interrupt handler.
5419 /****************************************************************************/
5420 /* Main interrupt entry point. Verifies that the controller generated the */
5421 /* interrupt and then calls a separate routine for handle the various */
5422 /* interrupt causes (PHY, TX, RX). */
5425 /* 0 for success, positive value for failure. */
5426 /****************************************************************************/
5428 bce_intr(struct bce_softc *sc)
5430 struct ifnet *ifp = &sc->arpcom.ac_if;
5431 struct status_block *sblk;
5432 uint16_t hw_rx_cons, hw_tx_cons;
5433 uint32_t status_attn_bits;
5434 struct bce_tx_ring *txr = &sc->tx_rings[0];
5435 struct bce_rx_ring *rxr = &sc->rx_rings[0];
5437 ASSERT_SERIALIZED(&sc->main_serialize);
5439 sblk = sc->status_block;
5442 * Save the status block index value for use during
5443 * the next interrupt.
5445 rxr->last_status_idx = *rxr->hw_status_idx;
5447 /* Make sure status index is extracted before RX/TX cons */
5450 /* Check if the hardware has finished any work. */
5451 hw_rx_cons = bce_get_hw_rx_cons(rxr);
5452 hw_tx_cons = bce_get_hw_tx_cons(txr);
5454 status_attn_bits = sblk->status_attn_bits;
5456 /* Was it a link change interrupt? */
5457 if ((status_attn_bits & STATUS_ATTN_BITS_LINK_STATE) !=
5458 (sblk->status_attn_bits_ack & STATUS_ATTN_BITS_LINK_STATE)) {
5462 * Clear any transient status updates during link state
5465 REG_WR(sc, BCE_HC_COMMAND,
5466 sc->hc_command | BCE_HC_COMMAND_COAL_NOW_WO_INT);
5467 REG_RD(sc, BCE_HC_COMMAND);
5471 * If any other attention is asserted then
5472 * the chip is toast.
5474 if ((status_attn_bits & ~STATUS_ATTN_BITS_LINK_STATE) !=
5475 (sblk->status_attn_bits_ack & ~STATUS_ATTN_BITS_LINK_STATE)) {
5476 if_printf(ifp, "Fatal attention detected: 0x%08X\n",
5477 sblk->status_attn_bits);
5478 bce_serialize_skipmain(sc);
5480 bce_deserialize_skipmain(sc);
5484 /* Check for any completed RX frames. */
5485 lwkt_serialize_enter(&rxr->rx_serialize);
5486 if (hw_rx_cons != rxr->rx_cons)
5487 bce_rx_intr(rxr, -1, hw_rx_cons);
5488 lwkt_serialize_exit(&rxr->rx_serialize);
5490 /* Check for any completed TX frames. */
5491 lwkt_serialize_enter(&txr->tx_serialize);
5492 if (hw_tx_cons != txr->tx_cons) {
5493 bce_tx_intr(txr, hw_tx_cons);
5494 if (!ifsq_is_empty(txr->ifsq))
5495 ifsq_devstart(txr->ifsq);
5497 lwkt_serialize_exit(&txr->tx_serialize);
5501 bce_intr_legacy(void *xsc)
5503 struct bce_softc *sc = xsc;
5504 struct bce_rx_ring *rxr = &sc->rx_rings[0];
5505 struct status_block *sblk;
5507 sblk = sc->status_block;
5510 * If the hardware status block index matches the last value
5511 * read by the driver and we haven't asserted our interrupt
5512 * then there's nothing to do.
5514 if (sblk->status_idx == rxr->last_status_idx &&
5515 (REG_RD(sc, BCE_PCICFG_MISC_STATUS) &
5516 BCE_PCICFG_MISC_STATUS_INTA_VALUE))
5519 /* Ack the interrupt and stop others from occuring. */
5520 REG_WR(sc, BCE_PCICFG_INT_ACK_CMD,
5521 BCE_PCICFG_INT_ACK_CMD_USE_INT_HC_PARAM |
5522 BCE_PCICFG_INT_ACK_CMD_MASK_INT);
5525 * Read back to deassert IRQ immediately to avoid too
5526 * many spurious interrupts.
5528 REG_RD(sc, BCE_PCICFG_INT_ACK_CMD);
5532 /* Re-enable interrupts. */
5533 REG_WR(sc, BCE_PCICFG_INT_ACK_CMD,
5534 BCE_PCICFG_INT_ACK_CMD_INDEX_VALID |
5535 BCE_PCICFG_INT_ACK_CMD_MASK_INT | rxr->last_status_idx);
5536 bce_reenable_intr(rxr);
5540 bce_intr_msi(void *xsc)
5542 struct bce_softc *sc = xsc;
5544 /* Ack the interrupt and stop others from occuring. */
5545 REG_WR(sc, BCE_PCICFG_INT_ACK_CMD,
5546 BCE_PCICFG_INT_ACK_CMD_USE_INT_HC_PARAM |
5547 BCE_PCICFG_INT_ACK_CMD_MASK_INT);
5551 /* Re-enable interrupts */
5552 bce_reenable_intr(&sc->rx_rings[0]);
5556 bce_intr_msi_oneshot(void *xsc)
5558 struct bce_softc *sc = xsc;
5562 /* Re-enable interrupts */
5563 bce_reenable_intr(&sc->rx_rings[0]);
5567 bce_intr_msix_rxtx(void *xrxr)
5569 struct bce_rx_ring *rxr = xrxr;
5570 struct bce_tx_ring *txr;
5571 uint16_t hw_rx_cons, hw_tx_cons;
5573 ASSERT_SERIALIZED(&rxr->rx_serialize);
5575 KKASSERT(rxr->idx < rxr->sc->tx_ring_cnt);
5576 txr = &rxr->sc->tx_rings[rxr->idx];
5579 * Save the status block index value for use during
5580 * the next interrupt.
5582 rxr->last_status_idx = *rxr->hw_status_idx;
5584 /* Make sure status index is extracted before RX/TX cons */
5587 /* Check if the hardware has finished any work. */
5588 hw_rx_cons = bce_get_hw_rx_cons(rxr);
5589 if (hw_rx_cons != rxr->rx_cons)
5590 bce_rx_intr(rxr, -1, hw_rx_cons);
5592 /* Check for any completed TX frames. */
5593 hw_tx_cons = bce_get_hw_tx_cons(txr);
5594 lwkt_serialize_enter(&txr->tx_serialize);
5595 if (hw_tx_cons != txr->tx_cons) {
5596 bce_tx_intr(txr, hw_tx_cons);
5597 if (!ifsq_is_empty(txr->ifsq))
5598 ifsq_devstart(txr->ifsq);
5600 lwkt_serialize_exit(&txr->tx_serialize);
5602 /* Re-enable interrupts */
5603 bce_reenable_intr(rxr);
5607 bce_intr_msix_rx(void *xrxr)
5609 struct bce_rx_ring *rxr = xrxr;
5610 uint16_t hw_rx_cons;
5612 ASSERT_SERIALIZED(&rxr->rx_serialize);
5615 * Save the status block index value for use during
5616 * the next interrupt.
5618 rxr->last_status_idx = *rxr->hw_status_idx;
5620 /* Make sure status index is extracted before RX cons */
5623 /* Check if the hardware has finished any work. */
5624 hw_rx_cons = bce_get_hw_rx_cons(rxr);
5625 if (hw_rx_cons != rxr->rx_cons)
5626 bce_rx_intr(rxr, -1, hw_rx_cons);
5628 /* Re-enable interrupts */
5629 bce_reenable_intr(rxr);
5633 /****************************************************************************/
5634 /* Programs the various packet receive modes (broadcast and multicast). */
5638 /****************************************************************************/
5640 bce_set_rx_mode(struct bce_softc *sc)
5642 struct ifnet *ifp = &sc->arpcom.ac_if;
5643 struct ifmultiaddr *ifma;
5644 uint32_t hashes[NUM_MC_HASH_REGISTERS] = { 0, 0, 0, 0, 0, 0, 0, 0 };
5645 uint32_t rx_mode, sort_mode;
5648 ASSERT_IFNET_SERIALIZED_ALL(ifp);
5650 /* Initialize receive mode default settings. */
5651 rx_mode = sc->rx_mode &
5652 ~(BCE_EMAC_RX_MODE_PROMISCUOUS |
5653 BCE_EMAC_RX_MODE_KEEP_VLAN_TAG);
5654 sort_mode = 1 | BCE_RPM_SORT_USER0_BC_EN;
5657 * ASF/IPMI/UMP firmware requires that VLAN tag stripping
5660 if (!(BCE_IF_CAPABILITIES & IFCAP_VLAN_HWTAGGING) &&
5661 !(sc->bce_flags & BCE_MFW_ENABLE_FLAG))
5662 rx_mode |= BCE_EMAC_RX_MODE_KEEP_VLAN_TAG;
5665 * Check for promiscuous, all multicast, or selected
5666 * multicast address filtering.
5668 if (ifp->if_flags & IFF_PROMISC) {
5669 /* Enable promiscuous mode. */
5670 rx_mode |= BCE_EMAC_RX_MODE_PROMISCUOUS;
5671 sort_mode |= BCE_RPM_SORT_USER0_PROM_EN;
5672 } else if (ifp->if_flags & IFF_ALLMULTI) {
5673 /* Enable all multicast addresses. */
5674 for (i = 0; i < NUM_MC_HASH_REGISTERS; i++) {
5675 REG_WR(sc, BCE_EMAC_MULTICAST_HASH0 + (i * 4),
5678 sort_mode |= BCE_RPM_SORT_USER0_MC_EN;
5680 /* Accept one or more multicast(s). */
5681 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
5682 if (ifma->ifma_addr->sa_family != AF_LINK)
5685 LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
5686 ETHER_ADDR_LEN) & 0xFF;
5687 hashes[(h & 0xE0) >> 5] |= 1 << (h & 0x1F);
5690 for (i = 0; i < NUM_MC_HASH_REGISTERS; i++) {
5691 REG_WR(sc, BCE_EMAC_MULTICAST_HASH0 + (i * 4),
5694 sort_mode |= BCE_RPM_SORT_USER0_MC_HSH_EN;
5697 /* Only make changes if the recive mode has actually changed. */
5698 if (rx_mode != sc->rx_mode) {
5699 sc->rx_mode = rx_mode;
5700 REG_WR(sc, BCE_EMAC_RX_MODE, rx_mode);
5703 /* Disable and clear the exisitng sort before enabling a new sort. */
5704 REG_WR(sc, BCE_RPM_SORT_USER0, 0x0);
5705 REG_WR(sc, BCE_RPM_SORT_USER0, sort_mode);
5706 REG_WR(sc, BCE_RPM_SORT_USER0, sort_mode | BCE_RPM_SORT_USER0_ENA);
5710 /****************************************************************************/
5711 /* Called periodically to updates statistics from the controllers */
5712 /* statistics block. */
5716 /****************************************************************************/
5718 bce_stats_update(struct bce_softc *sc)
5720 struct ifnet *ifp = &sc->arpcom.ac_if;
5721 struct statistics_block *stats = sc->stats_block;
5723 ASSERT_SERIALIZED(&sc->main_serialize);
5726 * Certain controllers don't report carrier sense errors correctly.
5727 * See errata E11_5708CA0_1165.
5729 if (!(BCE_CHIP_NUM(sc) == BCE_CHIP_NUM_5706) &&
5730 !(BCE_CHIP_ID(sc) == BCE_CHIP_ID_5708_A0)) {
5731 IFNET_STAT_INC(ifp, oerrors,
5732 (u_long)stats->stat_Dot3StatsCarrierSenseErrors);
5736 * Update the sysctl statistics from the hardware statistics.
5738 sc->stat_IfHCInOctets =
5739 ((uint64_t)stats->stat_IfHCInOctets_hi << 32) +
5740 (uint64_t)stats->stat_IfHCInOctets_lo;
5742 sc->stat_IfHCInBadOctets =
5743 ((uint64_t)stats->stat_IfHCInBadOctets_hi << 32) +
5744 (uint64_t)stats->stat_IfHCInBadOctets_lo;
5746 sc->stat_IfHCOutOctets =
5747 ((uint64_t)stats->stat_IfHCOutOctets_hi << 32) +
5748 (uint64_t)stats->stat_IfHCOutOctets_lo;
5750 sc->stat_IfHCOutBadOctets =
5751 ((uint64_t)stats->stat_IfHCOutBadOctets_hi << 32) +
5752 (uint64_t)stats->stat_IfHCOutBadOctets_lo;
5754 sc->stat_IfHCInUcastPkts =
5755 ((uint64_t)stats->stat_IfHCInUcastPkts_hi << 32) +
5756 (uint64_t)stats->stat_IfHCInUcastPkts_lo;
5758 sc->stat_IfHCInMulticastPkts =
5759 ((uint64_t)stats->stat_IfHCInMulticastPkts_hi << 32) +
5760 (uint64_t)stats->stat_IfHCInMulticastPkts_lo;
5762 sc->stat_IfHCInBroadcastPkts =
5763 ((uint64_t)stats->stat_IfHCInBroadcastPkts_hi << 32) +
5764 (uint64_t)stats->stat_IfHCInBroadcastPkts_lo;
5766 sc->stat_IfHCOutUcastPkts =
5767 ((uint64_t)stats->stat_IfHCOutUcastPkts_hi << 32) +
5768 (uint64_t)stats->stat_IfHCOutUcastPkts_lo;
5770 sc->stat_IfHCOutMulticastPkts =
5771 ((uint64_t)stats->stat_IfHCOutMulticastPkts_hi << 32) +
5772 (uint64_t)stats->stat_IfHCOutMulticastPkts_lo;
5774 sc->stat_IfHCOutBroadcastPkts =
5775 ((uint64_t)stats->stat_IfHCOutBroadcastPkts_hi << 32) +
5776 (uint64_t)stats->stat_IfHCOutBroadcastPkts_lo;
5778 sc->stat_emac_tx_stat_dot3statsinternalmactransmiterrors =
5779 stats->stat_emac_tx_stat_dot3statsinternalmactransmiterrors;
5781 sc->stat_Dot3StatsCarrierSenseErrors =
5782 stats->stat_Dot3StatsCarrierSenseErrors;
5784 sc->stat_Dot3StatsFCSErrors =
5785 stats->stat_Dot3StatsFCSErrors;
5787 sc->stat_Dot3StatsAlignmentErrors =
5788 stats->stat_Dot3StatsAlignmentErrors;
5790 sc->stat_Dot3StatsSingleCollisionFrames =
5791 stats->stat_Dot3StatsSingleCollisionFrames;
5793 sc->stat_Dot3StatsMultipleCollisionFrames =
5794 stats->stat_Dot3StatsMultipleCollisionFrames;
5796 sc->stat_Dot3StatsDeferredTransmissions =
5797 stats->stat_Dot3StatsDeferredTransmissions;
5799 sc->stat_Dot3StatsExcessiveCollisions =
5800 stats->stat_Dot3StatsExcessiveCollisions;
5802 sc->stat_Dot3StatsLateCollisions =
5803 stats->stat_Dot3StatsLateCollisions;
5805 sc->stat_EtherStatsCollisions =
5806 stats->stat_EtherStatsCollisions;
5808 sc->stat_EtherStatsFragments =
5809 stats->stat_EtherStatsFragments;
5811 sc->stat_EtherStatsJabbers =
5812 stats->stat_EtherStatsJabbers;
5814 sc->stat_EtherStatsUndersizePkts =
5815 stats->stat_EtherStatsUndersizePkts;
5817 sc->stat_EtherStatsOverrsizePkts =
5818 stats->stat_EtherStatsOverrsizePkts;
5820 sc->stat_EtherStatsPktsRx64Octets =
5821 stats->stat_EtherStatsPktsRx64Octets;
5823 sc->stat_EtherStatsPktsRx65Octetsto127Octets =
5824 stats->stat_EtherStatsPktsRx65Octetsto127Octets;
5826 sc->stat_EtherStatsPktsRx128Octetsto255Octets =
5827 stats->stat_EtherStatsPktsRx128Octetsto255Octets;
5829 sc->stat_EtherStatsPktsRx256Octetsto511Octets =
5830 stats->stat_EtherStatsPktsRx256Octetsto511Octets;
5832 sc->stat_EtherStatsPktsRx512Octetsto1023Octets =
5833 stats->stat_EtherStatsPktsRx512Octetsto1023Octets;
5835 sc->stat_EtherStatsPktsRx1024Octetsto1522Octets =
5836 stats->stat_EtherStatsPktsRx1024Octetsto1522Octets;
5838 sc->stat_EtherStatsPktsRx1523Octetsto9022Octets =
5839 stats->stat_EtherStatsPktsRx1523Octetsto9022Octets;
5841 sc->stat_EtherStatsPktsTx64Octets =
5842 stats->stat_EtherStatsPktsTx64Octets;
5844 sc->stat_EtherStatsPktsTx65Octetsto127Octets =
5845 stats->stat_EtherStatsPktsTx65Octetsto127Octets;
5847 sc->stat_EtherStatsPktsTx128Octetsto255Octets =
5848 stats->stat_EtherStatsPktsTx128Octetsto255Octets;
5850 sc->stat_EtherStatsPktsTx256Octetsto511Octets =
5851 stats->stat_EtherStatsPktsTx256Octetsto511Octets;
5853 sc->stat_EtherStatsPktsTx512Octetsto1023Octets =
5854 stats->stat_EtherStatsPktsTx512Octetsto1023Octets;
5856 sc->stat_EtherStatsPktsTx1024Octetsto1522Octets =
5857 stats->stat_EtherStatsPktsTx1024Octetsto1522Octets;
5859 sc->stat_EtherStatsPktsTx1523Octetsto9022Octets =
5860 stats->stat_EtherStatsPktsTx1523Octetsto9022Octets;
5862 sc->stat_XonPauseFramesReceived =
5863 stats->stat_XonPauseFramesReceived;
5865 sc->stat_XoffPauseFramesReceived =
5866 stats->stat_XoffPauseFramesReceived;
5868 sc->stat_OutXonSent =
5869 stats->stat_OutXonSent;
5871 sc->stat_OutXoffSent =
5872 stats->stat_OutXoffSent;
5874 sc->stat_FlowControlDone =
5875 stats->stat_FlowControlDone;
5877 sc->stat_MacControlFramesReceived =
5878 stats->stat_MacControlFramesReceived;
5880 sc->stat_XoffStateEntered =
5881 stats->stat_XoffStateEntered;
5883 sc->stat_IfInFramesL2FilterDiscards =
5884 stats->stat_IfInFramesL2FilterDiscards;
5886 sc->stat_IfInRuleCheckerDiscards =
5887 stats->stat_IfInRuleCheckerDiscards;
5889 sc->stat_IfInFTQDiscards =
5890 stats->stat_IfInFTQDiscards;
5892 sc->stat_IfInMBUFDiscards =
5893 stats->stat_IfInMBUFDiscards;
5895 sc->stat_IfInRuleCheckerP4Hit =
5896 stats->stat_IfInRuleCheckerP4Hit;
5898 sc->stat_CatchupInRuleCheckerDiscards =
5899 stats->stat_CatchupInRuleCheckerDiscards;
5901 sc->stat_CatchupInFTQDiscards =
5902 stats->stat_CatchupInFTQDiscards;
5904 sc->stat_CatchupInMBUFDiscards =
5905 stats->stat_CatchupInMBUFDiscards;
5907 sc->stat_CatchupInRuleCheckerP4Hit =
5908 stats->stat_CatchupInRuleCheckerP4Hit;
5910 sc->com_no_buffers = REG_RD_IND(sc, 0x120084);
5913 * Update the interface statistics from the
5914 * hardware statistics.
5916 IFNET_STAT_SET(ifp, collisions, (u_long)sc->stat_EtherStatsCollisions);
5918 IFNET_STAT_SET(ifp, ierrors, (u_long)sc->stat_EtherStatsUndersizePkts +
5919 (u_long)sc->stat_EtherStatsOverrsizePkts +
5920 (u_long)sc->stat_IfInMBUFDiscards +
5921 (u_long)sc->stat_Dot3StatsAlignmentErrors +
5922 (u_long)sc->stat_Dot3StatsFCSErrors +
5923 (u_long)sc->stat_IfInRuleCheckerDiscards +
5924 (u_long)sc->stat_IfInFTQDiscards +
5925 (u_long)sc->com_no_buffers);
5927 IFNET_STAT_SET(ifp, oerrors,
5928 (u_long)sc->stat_emac_tx_stat_dot3statsinternalmactransmiterrors +
5929 (u_long)sc->stat_Dot3StatsExcessiveCollisions +
5930 (u_long)sc->stat_Dot3StatsLateCollisions);
5934 /****************************************************************************/
5935 /* Periodic function to notify the bootcode that the driver is still */
5940 /****************************************************************************/
5942 bce_pulse(void *xsc)
5944 struct bce_softc *sc = xsc;
5945 struct ifnet *ifp = &sc->arpcom.ac_if;
5948 lwkt_serialize_enter(&sc->main_serialize);
5950 /* Tell the firmware that the driver is still running. */
5951 msg = (uint32_t)++sc->bce_fw_drv_pulse_wr_seq;
5952 bce_shmem_wr(sc, BCE_DRV_PULSE_MB, msg);
5954 /* Update the bootcode condition. */
5955 sc->bc_state = bce_shmem_rd(sc, BCE_BC_STATE_CONDITION);
5957 /* Report whether the bootcode still knows the driver is running. */
5958 if (!sc->bce_drv_cardiac_arrest) {
5959 if (!(sc->bc_state & BCE_CONDITION_DRV_PRESENT)) {
5960 sc->bce_drv_cardiac_arrest = 1;
5961 if_printf(ifp, "Bootcode lost the driver pulse! "
5962 "(bc_state = 0x%08X)\n", sc->bc_state);
5966 * Not supported by all bootcode versions.
5967 * (v5.0.11+ and v5.2.1+) Older bootcode
5968 * will require the driver to reset the
5969 * controller to clear this condition.
5971 if (sc->bc_state & BCE_CONDITION_DRV_PRESENT) {
5972 sc->bce_drv_cardiac_arrest = 0;
5973 if_printf(ifp, "Bootcode found the driver pulse! "
5974 "(bc_state = 0x%08X)\n", sc->bc_state);
5978 /* Schedule the next pulse. */
5979 callout_reset_bycpu(&sc->bce_pulse_callout, hz, bce_pulse, sc,
5980 sc->bce_timer_cpuid);
5982 lwkt_serialize_exit(&sc->main_serialize);
5986 /****************************************************************************/
5987 /* Periodic function to check whether MSI is lost */
5991 /****************************************************************************/
5993 bce_check_msi(void *xsc)
5995 struct bce_softc *sc = xsc;
5996 struct ifnet *ifp = &sc->arpcom.ac_if;
5997 struct status_block *sblk = sc->status_block;
5998 struct bce_tx_ring *txr = &sc->tx_rings[0];
5999 struct bce_rx_ring *rxr = &sc->rx_rings[0];
6001 lwkt_serialize_enter(&sc->main_serialize);
6003 KKASSERT(mycpuid == sc->bce_msix[0].msix_cpuid);
6005 if ((ifp->if_flags & (IFF_RUNNING | IFF_NPOLLING)) != IFF_RUNNING) {
6006 lwkt_serialize_exit(&sc->main_serialize);
6010 if (bce_get_hw_rx_cons(rxr) != rxr->rx_cons ||
6011 bce_get_hw_tx_cons(txr) != txr->tx_cons ||
6012 (sblk->status_attn_bits & STATUS_ATTN_BITS_LINK_STATE) !=
6013 (sblk->status_attn_bits_ack & STATUS_ATTN_BITS_LINK_STATE)) {
6014 if (sc->bce_check_rx_cons == rxr->rx_cons &&
6015 sc->bce_check_tx_cons == txr->tx_cons &&
6016 sc->bce_check_status_idx == rxr->last_status_idx) {
6019 if (!sc->bce_msi_maylose) {
6020 sc->bce_msi_maylose = TRUE;
6024 msi_ctrl = REG_RD(sc, BCE_PCICFG_MSI_CONTROL);
6025 if (msi_ctrl & BCE_PCICFG_MSI_CONTROL_ENABLE) {
6027 if_printf(ifp, "lost MSI\n");
6029 REG_WR(sc, BCE_PCICFG_MSI_CONTROL,
6030 msi_ctrl & ~BCE_PCICFG_MSI_CONTROL_ENABLE);
6031 REG_WR(sc, BCE_PCICFG_MSI_CONTROL, msi_ctrl);
6034 } else if (bootverbose) {
6035 if_printf(ifp, "MSI may be lost\n");
6039 sc->bce_msi_maylose = FALSE;
6040 sc->bce_check_rx_cons = rxr->rx_cons;
6041 sc->bce_check_tx_cons = txr->tx_cons;
6042 sc->bce_check_status_idx = rxr->last_status_idx;
6045 callout_reset(&sc->bce_ckmsi_callout, BCE_MSI_CKINTVL,
6047 lwkt_serialize_exit(&sc->main_serialize);
6051 /****************************************************************************/
6052 /* Periodic function to perform maintenance tasks. */
6056 /****************************************************************************/
6058 bce_tick_serialized(struct bce_softc *sc)
6060 struct mii_data *mii;
6062 ASSERT_SERIALIZED(&sc->main_serialize);
6064 /* Update the statistics from the hardware statistics block. */
6065 bce_stats_update(sc);
6067 /* Schedule the next tick. */
6068 callout_reset_bycpu(&sc->bce_tick_callout, hz, bce_tick, sc,
6069 sc->bce_timer_cpuid);
6071 /* If link is up already up then we're done. */
6075 mii = device_get_softc(sc->bce_miibus);
6078 /* Check if the link has come up. */
6079 if ((mii->mii_media_status & IFM_ACTIVE) &&
6080 IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
6084 /* Now that link is up, handle any outstanding TX traffic. */
6085 for (i = 0; i < sc->tx_ring_cnt; ++i)
6086 ifsq_devstart_sched(sc->tx_rings[i].ifsq);
6094 struct bce_softc *sc = xsc;
6096 lwkt_serialize_enter(&sc->main_serialize);
6097 bce_tick_serialized(sc);
6098 lwkt_serialize_exit(&sc->main_serialize);
6102 /****************************************************************************/
6103 /* Adds any sysctl parameters for tuning or debugging purposes. */
6106 /* 0 for success, positive value for failure. */
6107 /****************************************************************************/
6109 bce_add_sysctls(struct bce_softc *sc)
6111 struct sysctl_ctx_list *ctx;
6112 struct sysctl_oid_list *children;
6113 #if defined(BCE_TSS_DEBUG) || defined(BCE_RSS_DEBUG)
6118 sysctl_ctx_init(&sc->bce_sysctl_ctx);
6119 sc->bce_sysctl_tree = SYSCTL_ADD_NODE(&sc->bce_sysctl_ctx,
6120 SYSCTL_STATIC_CHILDREN(_hw),
6122 device_get_nameunit(sc->bce_dev),
6124 if (sc->bce_sysctl_tree == NULL) {
6125 device_printf(sc->bce_dev, "can't add sysctl node\n");
6129 ctx = &sc->bce_sysctl_ctx;
6130 children = SYSCTL_CHILDREN(sc->bce_sysctl_tree);
6132 SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "tx_bds_int",
6133 CTLTYPE_INT | CTLFLAG_RW,
6134 sc, 0, bce_sysctl_tx_bds_int, "I",
6135 "Send max coalesced BD count during interrupt");
6136 SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "tx_bds",
6137 CTLTYPE_INT | CTLFLAG_RW,
6138 sc, 0, bce_sysctl_tx_bds, "I",
6139 "Send max coalesced BD count");
6140 SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "tx_ticks_int",
6141 CTLTYPE_INT | CTLFLAG_RW,
6142 sc, 0, bce_sysctl_tx_ticks_int, "I",
6143 "Send coalescing ticks during interrupt");
6144 SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "tx_ticks",
6145 CTLTYPE_INT | CTLFLAG_RW,
6146 sc, 0, bce_sysctl_tx_ticks, "I",
6147 "Send coalescing ticks");
6149 SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "rx_bds_int",
6150 CTLTYPE_INT | CTLFLAG_RW,
6151 sc, 0, bce_sysctl_rx_bds_int, "I",
6152 "Receive max coalesced BD count during interrupt");
6153 SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "rx_bds",
6154 CTLTYPE_INT | CTLFLAG_RW,
6155 sc, 0, bce_sysctl_rx_bds, "I",
6156 "Receive max coalesced BD count");
6157 SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "rx_ticks_int",
6158 CTLTYPE_INT | CTLFLAG_RW,
6159 sc, 0, bce_sysctl_rx_ticks_int, "I",
6160 "Receive coalescing ticks during interrupt");
6161 SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "rx_ticks",
6162 CTLTYPE_INT | CTLFLAG_RW,
6163 sc, 0, bce_sysctl_rx_ticks, "I",
6164 "Receive coalescing ticks");
6166 SYSCTL_ADD_INT(ctx, children, OID_AUTO, "rx_rings",
6167 CTLFLAG_RD, &sc->rx_ring_cnt, 0, "# of RX rings");
6168 SYSCTL_ADD_INT(ctx, children, OID_AUTO, "rx_pages",
6169 CTLFLAG_RD, &sc->rx_rings[0].rx_pages, 0, "# of RX pages");
6171 SYSCTL_ADD_INT(ctx, children, OID_AUTO, "tx_rings",
6172 CTLFLAG_RD, &sc->tx_ring_cnt, 0, "# of TX rings");
6173 SYSCTL_ADD_INT(ctx, children, OID_AUTO, "tx_pages",
6174 CTLFLAG_RD, &sc->tx_rings[0].tx_pages, 0, "# of TX pages");
6176 SYSCTL_ADD_INT(ctx, children, OID_AUTO, "tx_wreg",
6177 CTLFLAG_RW, &sc->tx_rings[0].tx_wreg, 0,
6178 "# segments before write to hardware registers");
6180 #ifdef IFPOLL_ENABLE
6181 SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "npoll_offset",
6182 CTLTYPE_INT|CTLFLAG_RW, sc, 0, bce_sysctl_npoll_offset,
6183 "I", "NPOLLING cpu offset");
6186 #ifdef BCE_RSS_DEBUG
6187 SYSCTL_ADD_INT(ctx, children, OID_AUTO, "rss_debug",
6188 CTLFLAG_RW, &sc->rss_debug, 0, "RSS debug level");
6189 for (i = 0; i < sc->rx_ring_cnt; ++i) {
6190 ksnprintf(node, sizeof(node), "rx%d_pkt", i);
6191 SYSCTL_ADD_ULONG(ctx, children, OID_AUTO, node,
6192 CTLFLAG_RW, &sc->rx_rings[i].rx_pkts,
6197 #ifdef BCE_TSS_DEBUG
6198 for (i = 0; i < sc->tx_ring_cnt; ++i) {
6199 ksnprintf(node, sizeof(node), "tx%d_pkt", i);
6200 SYSCTL_ADD_ULONG(ctx, children, OID_AUTO, node,
6201 CTLFLAG_RW, &sc->tx_rings[i].tx_pkts,
6206 SYSCTL_ADD_ULONG(ctx, children, OID_AUTO,
6207 "stat_IfHCInOctets",
6208 CTLFLAG_RD, &sc->stat_IfHCInOctets,
6211 SYSCTL_ADD_ULONG(ctx, children, OID_AUTO,
6212 "stat_IfHCInBadOctets",
6213 CTLFLAG_RD, &sc->stat_IfHCInBadOctets,
6214 "Bad bytes received");
6216 SYSCTL_ADD_ULONG(ctx, children, OID_AUTO,
6217 "stat_IfHCOutOctets",
6218 CTLFLAG_RD, &sc->stat_IfHCOutOctets,
6221 SYSCTL_ADD_ULONG(ctx, children, OID_AUTO,
6222 "stat_IfHCOutBadOctets",
6223 CTLFLAG_RD, &sc->stat_IfHCOutBadOctets,
6226 SYSCTL_ADD_ULONG(ctx, children, OID_AUTO,
6227 "stat_IfHCInUcastPkts",
6228 CTLFLAG_RD, &sc->stat_IfHCInUcastPkts,
6229 "Unicast packets received");
6231 SYSCTL_ADD_ULONG(ctx, children, OID_AUTO,
6232 "stat_IfHCInMulticastPkts",
6233 CTLFLAG_RD, &sc->stat_IfHCInMulticastPkts,
6234 "Multicast packets received");
6236 SYSCTL_ADD_ULONG(ctx, children, OID_AUTO,
6237 "stat_IfHCInBroadcastPkts",
6238 CTLFLAG_RD, &sc->stat_IfHCInBroadcastPkts,
6239 "Broadcast packets received");
6241 SYSCTL_ADD_ULONG(ctx, children, OID_AUTO,
6242 "stat_IfHCOutUcastPkts",
6243 CTLFLAG_RD, &sc->stat_IfHCOutUcastPkts,
6244 "Unicast packets sent");
6246 SYSCTL_ADD_ULONG(ctx, children, OID_AUTO,
6247 "stat_IfHCOutMulticastPkts",
6248 CTLFLAG_RD, &sc->stat_IfHCOutMulticastPkts,
6249 "Multicast packets sent");
6251 SYSCTL_ADD_ULONG(ctx, children, OID_AUTO,
6252 "stat_IfHCOutBroadcastPkts",
6253 CTLFLAG_RD, &sc->stat_IfHCOutBroadcastPkts,
6254 "Broadcast packets sent");
6256 SYSCTL_ADD_UINT(ctx, children, OID_AUTO,
6257 "stat_emac_tx_stat_dot3statsinternalmactransmiterrors",
6258 CTLFLAG_RD, &sc->stat_emac_tx_stat_dot3statsinternalmactransmiterrors,
6259 0, "Internal MAC transmit errors");
6261 SYSCTL_ADD_UINT(ctx, children, OID_AUTO,
6262 "stat_Dot3StatsCarrierSenseErrors",
6263 CTLFLAG_RD, &sc->stat_Dot3StatsCarrierSenseErrors,
6264 0, "Carrier sense errors");
6266 SYSCTL_ADD_UINT(ctx, children, OID_AUTO,
6267 "stat_Dot3StatsFCSErrors",
6268 CTLFLAG_RD, &sc->stat_Dot3StatsFCSErrors,
6269 0, "Frame check sequence errors");
6271 SYSCTL_ADD_UINT(ctx, children, OID_AUTO,
6272 "stat_Dot3StatsAlignmentErrors",
6273 CTLFLAG_RD, &sc->stat_Dot3StatsAlignmentErrors,
6274 0, "Alignment errors");
6276 SYSCTL_ADD_UINT(ctx, children, OID_AUTO,
6277 "stat_Dot3StatsSingleCollisionFrames",
6278 CTLFLAG_RD, &sc->stat_Dot3StatsSingleCollisionFrames,
6279 0, "Single Collision Frames");
6281 SYSCTL_ADD_UINT(ctx, children, OID_AUTO,
6282 "stat_Dot3StatsMultipleCollisionFrames",
6283 CTLFLAG_RD, &sc->stat_Dot3StatsMultipleCollisionFrames,
6284 0, "Multiple Collision Frames");
6286 SYSCTL_ADD_UINT(ctx, children, OID_AUTO,
6287 "stat_Dot3StatsDeferredTransmissions",
6288 CTLFLAG_RD, &sc->stat_Dot3StatsDeferredTransmissions,
6289 0, "Deferred Transmissions");
6291 SYSCTL_ADD_UINT(ctx, children, OID_AUTO,
6292 "stat_Dot3StatsExcessiveCollisions",
6293 CTLFLAG_RD, &sc->stat_Dot3StatsExcessiveCollisions,
6294 0, "Excessive Collisions");
6296 SYSCTL_ADD_UINT(ctx, children, OID_AUTO,
6297 "stat_Dot3StatsLateCollisions",
6298 CTLFLAG_RD, &sc->stat_Dot3StatsLateCollisions,
6299 0, "Late Collisions");
6301 SYSCTL_ADD_UINT(ctx, children, OID_AUTO,
6302 "stat_EtherStatsCollisions",
6303 CTLFLAG_RD, &sc->stat_EtherStatsCollisions,
6306 SYSCTL_ADD_UINT(ctx, children, OID_AUTO,
6307 "stat_EtherStatsFragments",
6308 CTLFLAG_RD, &sc->stat_EtherStatsFragments,
6311 SYSCTL_ADD_UINT(ctx, children, OID_AUTO,
6312 "stat_EtherStatsJabbers",
6313 CTLFLAG_RD, &sc->stat_EtherStatsJabbers,
6316 SYSCTL_ADD_UINT(ctx, children, OID_AUTO,
6317 "stat_EtherStatsUndersizePkts",
6318 CTLFLAG_RD, &sc->stat_EtherStatsUndersizePkts,
6319 0, "Undersize packets");
6321 SYSCTL_ADD_UINT(ctx, children, OID_AUTO,
6322 "stat_EtherStatsOverrsizePkts",
6323 CTLFLAG_RD, &sc->stat_EtherStatsOverrsizePkts,
6324 0, "stat_EtherStatsOverrsizePkts");
6326 SYSCTL_ADD_UINT(ctx, children, OID_AUTO,
6327 "stat_EtherStatsPktsRx64Octets",
6328 CTLFLAG_RD, &sc->stat_EtherStatsPktsRx64Octets,
6329 0, "Bytes received in 64 byte packets");
6331 SYSCTL_ADD_UINT(ctx, children, OID_AUTO,
6332 "stat_EtherStatsPktsRx65Octetsto127Octets",
6333 CTLFLAG_RD, &sc->stat_EtherStatsPktsRx65Octetsto127Octets,
6334 0, "Bytes received in 65 to 127 byte packets");
6336 SYSCTL_ADD_UINT(ctx, children, OID_AUTO,
6337 "stat_EtherStatsPktsRx128Octetsto255Octets",
6338 CTLFLAG_RD, &sc->stat_EtherStatsPktsRx128Octetsto255Octets,
6339 0, "Bytes received in 128 to 255 byte packets");
6341 SYSCTL_ADD_UINT(ctx, children, OID_AUTO,
6342 "stat_EtherStatsPktsRx256Octetsto511Octets",
6343 CTLFLAG_RD, &sc->stat_EtherStatsPktsRx256Octetsto511Octets,
6344 0, "Bytes received in 256 to 511 byte packets");
6346 SYSCTL_ADD_UINT(ctx, children, OID_AUTO,
6347 "stat_EtherStatsPktsRx512Octetsto1023Octets",
6348 CTLFLAG_RD, &sc->stat_EtherStatsPktsRx512Octetsto1023Octets,
6349 0, "Bytes received in 512 to 1023 byte packets");
6351 SYSCTL_ADD_UINT(ctx, children, OID_AUTO,
6352 "stat_EtherStatsPktsRx1024Octetsto1522Octets",
6353 CTLFLAG_RD, &sc->stat_EtherStatsPktsRx1024Octetsto1522Octets,
6354 0, "Bytes received in 1024 t0 1522 byte packets");
6356 SYSCTL_ADD_UINT(ctx, children, OID_AUTO,
6357 "stat_EtherStatsPktsRx1523Octetsto9022Octets",
6358 CTLFLAG_RD, &sc->stat_EtherStatsPktsRx1523Octetsto9022Octets,
6359 0, "Bytes received in 1523 to 9022 byte packets");
6361 SYSCTL_ADD_UINT(ctx, children, OID_AUTO,
6362 "stat_EtherStatsPktsTx64Octets",
6363 CTLFLAG_RD, &sc->stat_EtherStatsPktsTx64Octets,
6364 0, "Bytes sent in 64 byte packets");
6366 SYSCTL_ADD_UINT(ctx, children, OID_AUTO,
6367 "stat_EtherStatsPktsTx65Octetsto127Octets",
6368 CTLFLAG_RD, &sc->stat_EtherStatsPktsTx65Octetsto127Octets,
6369 0, "Bytes sent in 65 to 127 byte packets");
6371 SYSCTL_ADD_UINT(ctx, children, OID_AUTO,
6372 "stat_EtherStatsPktsTx128Octetsto255Octets",
6373 CTLFLAG_RD, &sc->stat_EtherStatsPktsTx128Octetsto255Octets,
6374 0, "Bytes sent in 128 to 255 byte packets");
6376 SYSCTL_ADD_UINT(ctx, children, OID_AUTO,
6377 "stat_EtherStatsPktsTx256Octetsto511Octets",
6378 CTLFLAG_RD, &sc->stat_EtherStatsPktsTx256Octetsto511Octets,
6379 0, "Bytes sent in 256 to 511 byte packets");
6381 SYSCTL_ADD_UINT(ctx, children, OID_AUTO,
6382 "stat_EtherStatsPktsTx512Octetsto1023Octets",
6383 CTLFLAG_RD, &sc->stat_EtherStatsPktsTx512Octetsto1023Octets,
6384 0, "Bytes sent in 512 to 1023 byte packets");
6386 SYSCTL_ADD_UINT(ctx, children, OID_AUTO,
6387 "stat_EtherStatsPktsTx1024Octetsto1522Octets",
6388 CTLFLAG_RD, &sc->stat_EtherStatsPktsTx1024Octetsto1522Octets,
6389 0, "Bytes sent in 1024 to 1522 byte packets");
6391 SYSCTL_ADD_UINT(ctx, children, OID_AUTO,
6392 "stat_EtherStatsPktsTx1523Octetsto9022Octets",
6393 CTLFLAG_RD, &sc->stat_EtherStatsPktsTx1523Octetsto9022Octets,
6394 0, "Bytes sent in 1523 to 9022 byte packets");
6396 SYSCTL_ADD_UINT(ctx, children, OID_AUTO,
6397 "stat_XonPauseFramesReceived",
6398 CTLFLAG_RD, &sc->stat_XonPauseFramesReceived,
6399 0, "XON pause frames receved");
6401 SYSCTL_ADD_UINT(ctx, children, OID_AUTO,
6402 "stat_XoffPauseFramesReceived",
6403 CTLFLAG_RD, &sc->stat_XoffPauseFramesReceived,
6404 0, "XOFF pause frames received");
6406 SYSCTL_ADD_UINT(ctx, children, OID_AUTO,
6408 CTLFLAG_RD, &sc->stat_OutXonSent,
6409 0, "XON pause frames sent");
6411 SYSCTL_ADD_UINT(ctx, children, OID_AUTO,
6413 CTLFLAG_RD, &sc->stat_OutXoffSent,
6414 0, "XOFF pause frames sent");
6416 SYSCTL_ADD_UINT(ctx, children, OID_AUTO,
6417 "stat_FlowControlDone",
6418 CTLFLAG_RD, &sc->stat_FlowControlDone,
6419 0, "Flow control done");
6421 SYSCTL_ADD_UINT(ctx, children, OID_AUTO,
6422 "stat_MacControlFramesReceived",
6423 CTLFLAG_RD, &sc->stat_MacControlFramesReceived,
6424 0, "MAC control frames received");
6426 SYSCTL_ADD_UINT(ctx, children, OID_AUTO,
6427 "stat_XoffStateEntered",
6428 CTLFLAG_RD, &sc->stat_XoffStateEntered,
6429 0, "XOFF state entered");
6431 SYSCTL_ADD_UINT(ctx, children, OID_AUTO,
6432 "stat_IfInFramesL2FilterDiscards",
6433 CTLFLAG_RD, &sc->stat_IfInFramesL2FilterDiscards,
6434 0, "Received L2 packets discarded");
6436 SYSCTL_ADD_UINT(ctx, children, OID_AUTO,
6437 "stat_IfInRuleCheckerDiscards",
6438 CTLFLAG_RD, &sc->stat_IfInRuleCheckerDiscards,
6439 0, "Received packets discarded by rule");
6441 SYSCTL_ADD_UINT(ctx, children, OID_AUTO,
6442 "stat_IfInFTQDiscards",
6443 CTLFLAG_RD, &sc->stat_IfInFTQDiscards,
6444 0, "Received packet FTQ discards");
6446 SYSCTL_ADD_UINT(ctx, children, OID_AUTO,
6447 "stat_IfInMBUFDiscards",
6448 CTLFLAG_RD, &sc->stat_IfInMBUFDiscards,
6449 0, "Received packets discarded due to lack of controller buffer memory");
6451 SYSCTL_ADD_UINT(ctx, children, OID_AUTO,
6452 "stat_IfInRuleCheckerP4Hit",
6453 CTLFLAG_RD, &sc->stat_IfInRuleCheckerP4Hit,
6454 0, "Received packets rule checker hits");
6456 SYSCTL_ADD_UINT(ctx, children, OID_AUTO,
6457 "stat_CatchupInRuleCheckerDiscards",
6458 CTLFLAG_RD, &sc->stat_CatchupInRuleCheckerDiscards,
6459 0, "Received packets discarded in Catchup path");
6461 SYSCTL_ADD_UINT(ctx, children, OID_AUTO,
6462 "stat_CatchupInFTQDiscards",
6463 CTLFLAG_RD, &sc->stat_CatchupInFTQDiscards,
6464 0, "Received packets discarded in FTQ in Catchup path");
6466 SYSCTL_ADD_UINT(ctx, children, OID_AUTO,
6467 "stat_CatchupInMBUFDiscards",
6468 CTLFLAG_RD, &sc->stat_CatchupInMBUFDiscards,
6469 0, "Received packets discarded in controller buffer memory in Catchup path");
6471 SYSCTL_ADD_UINT(ctx, children, OID_AUTO,
6472 "stat_CatchupInRuleCheckerP4Hit",
6473 CTLFLAG_RD, &sc->stat_CatchupInRuleCheckerP4Hit,
6474 0, "Received packets rule checker hits in Catchup path");
6476 SYSCTL_ADD_UINT(ctx, children, OID_AUTO,
6478 CTLFLAG_RD, &sc->com_no_buffers,
6479 0, "Valid packets received but no RX buffers available");
6483 bce_sysctl_tx_bds_int(SYSCTL_HANDLER_ARGS)
6485 struct bce_softc *sc = arg1;
6487 return bce_sysctl_coal_change(oidp, arg1, arg2, req,
6488 &sc->bce_tx_quick_cons_trip_int,
6489 BCE_COALMASK_TX_BDS_INT);
6493 bce_sysctl_tx_bds(SYSCTL_HANDLER_ARGS)
6495 struct bce_softc *sc = arg1;
6497 return bce_sysctl_coal_change(oidp, arg1, arg2, req,
6498 &sc->bce_tx_quick_cons_trip,
6499 BCE_COALMASK_TX_BDS);
6503 bce_sysctl_tx_ticks_int(SYSCTL_HANDLER_ARGS)
6505 struct bce_softc *sc = arg1;
6507 return bce_sysctl_coal_change(oidp, arg1, arg2, req,
6508 &sc->bce_tx_ticks_int,
6509 BCE_COALMASK_TX_TICKS_INT);
6513 bce_sysctl_tx_ticks(SYSCTL_HANDLER_ARGS)
6515 struct bce_softc *sc = arg1;
6517 return bce_sysctl_coal_change(oidp, arg1, arg2, req,
6519 BCE_COALMASK_TX_TICKS);
6523 bce_sysctl_rx_bds_int(SYSCTL_HANDLER_ARGS)
6525 struct bce_softc *sc = arg1;
6527 return bce_sysctl_coal_change(oidp, arg1, arg2, req,
6528 &sc->bce_rx_quick_cons_trip_int,
6529 BCE_COALMASK_RX_BDS_INT);
6533 bce_sysctl_rx_bds(SYSCTL_HANDLER_ARGS)
6535 struct bce_softc *sc = arg1;
6537 return bce_sysctl_coal_change(oidp, arg1, arg2, req,
6538 &sc->bce_rx_quick_cons_trip,
6539 BCE_COALMASK_RX_BDS);
6543 bce_sysctl_rx_ticks_int(SYSCTL_HANDLER_ARGS)
6545 struct bce_softc *sc = arg1;
6547 return bce_sysctl_coal_change(oidp, arg1, arg2, req,
6548 &sc->bce_rx_ticks_int,
6549 BCE_COALMASK_RX_TICKS_INT);
6553 bce_sysctl_rx_ticks(SYSCTL_HANDLER_ARGS)
6555 struct bce_softc *sc = arg1;
6557 return bce_sysctl_coal_change(oidp, arg1, arg2, req,
6559 BCE_COALMASK_RX_TICKS);
6563 bce_sysctl_coal_change(SYSCTL_HANDLER_ARGS, uint32_t *coal,
6564 uint32_t coalchg_mask)
6566 struct bce_softc *sc = arg1;
6567 struct ifnet *ifp = &sc->arpcom.ac_if;
6570 ifnet_serialize_all(ifp);
6573 error = sysctl_handle_int(oidp, &v, 0, req);
6574 if (!error && req->newptr != NULL) {
6579 sc->bce_coalchg_mask |= coalchg_mask;
6581 /* Commit changes */
6582 bce_coal_change(sc);
6586 ifnet_deserialize_all(ifp);
6591 bce_coal_change(struct bce_softc *sc)
6593 struct ifnet *ifp = &sc->arpcom.ac_if;
6596 ASSERT_SERIALIZED(&sc->main_serialize);
6598 if ((ifp->if_flags & IFF_RUNNING) == 0) {
6599 sc->bce_coalchg_mask = 0;
6603 if (sc->bce_coalchg_mask &
6604 (BCE_COALMASK_TX_BDS | BCE_COALMASK_TX_BDS_INT)) {
6605 REG_WR(sc, BCE_HC_TX_QUICK_CONS_TRIP,
6606 (sc->bce_tx_quick_cons_trip_int << 16) |
6607 sc->bce_tx_quick_cons_trip);
6608 for (i = 1; i < sc->rx_ring_cnt; ++i) {
6611 base = ((i - 1) * BCE_HC_SB_CONFIG_SIZE) +
6613 REG_WR(sc, base + BCE_HC_TX_QUICK_CONS_TRIP_OFF,
6614 (sc->bce_tx_quick_cons_trip_int << 16) |
6615 sc->bce_tx_quick_cons_trip);
6618 if_printf(ifp, "tx_bds %u, tx_bds_int %u\n",
6619 sc->bce_tx_quick_cons_trip,
6620 sc->bce_tx_quick_cons_trip_int);
6624 if (sc->bce_coalchg_mask &
6625 (BCE_COALMASK_TX_TICKS | BCE_COALMASK_TX_TICKS_INT)) {
6626 REG_WR(sc, BCE_HC_TX_TICKS,
6627 (sc->bce_tx_ticks_int << 16) | sc->bce_tx_ticks);
6628 for (i = 1; i < sc->rx_ring_cnt; ++i) {
6631 base = ((i - 1) * BCE_HC_SB_CONFIG_SIZE) +
6633 REG_WR(sc, base + BCE_HC_TX_TICKS_OFF,
6634 (sc->bce_tx_ticks_int << 16) | sc->bce_tx_ticks);
6637 if_printf(ifp, "tx_ticks %u, tx_ticks_int %u\n",
6638 sc->bce_tx_ticks, sc->bce_tx_ticks_int);
6642 if (sc->bce_coalchg_mask &
6643 (BCE_COALMASK_RX_BDS | BCE_COALMASK_RX_BDS_INT)) {
6644 REG_WR(sc, BCE_HC_RX_QUICK_CONS_TRIP,
6645 (sc->bce_rx_quick_cons_trip_int << 16) |
6646 sc->bce_rx_quick_cons_trip);
6647 for (i = 1; i < sc->rx_ring_cnt; ++i) {
6650 base = ((i - 1) * BCE_HC_SB_CONFIG_SIZE) +
6652 REG_WR(sc, base + BCE_HC_RX_QUICK_CONS_TRIP_OFF,
6653 (sc->bce_rx_quick_cons_trip_int << 16) |
6654 sc->bce_rx_quick_cons_trip);
6657 if_printf(ifp, "rx_bds %u, rx_bds_int %u\n",
6658 sc->bce_rx_quick_cons_trip,
6659 sc->bce_rx_quick_cons_trip_int);
6663 if (sc->bce_coalchg_mask &
6664 (BCE_COALMASK_RX_TICKS | BCE_COALMASK_RX_TICKS_INT)) {
6665 REG_WR(sc, BCE_HC_RX_TICKS,
6666 (sc->bce_rx_ticks_int << 16) | sc->bce_rx_ticks);
6667 for (i = 1; i < sc->rx_ring_cnt; ++i) {
6670 base = ((i - 1) * BCE_HC_SB_CONFIG_SIZE) +
6672 REG_WR(sc, base + BCE_HC_RX_TICKS_OFF,
6673 (sc->bce_rx_ticks_int << 16) | sc->bce_rx_ticks);
6676 if_printf(ifp, "rx_ticks %u, rx_ticks_int %u\n",
6677 sc->bce_rx_ticks, sc->bce_rx_ticks_int);
6681 sc->bce_coalchg_mask = 0;
6685 bce_tso_setup(struct bce_tx_ring *txr, struct mbuf **mp,
6686 uint16_t *flags0, uint16_t *mss0)
6690 int thoff, iphlen, hoff;
6693 KASSERT(M_WRITABLE(m), ("TSO mbuf not writable"));
6695 hoff = m->m_pkthdr.csum_lhlen;
6696 iphlen = m->m_pkthdr.csum_iphlen;
6697 thoff = m->m_pkthdr.csum_thlen;
6699 KASSERT(hoff >= sizeof(struct ether_header),
6700 ("invalid ether header len %d", hoff));
6701 KASSERT(iphlen >= sizeof(struct ip),
6702 ("invalid ip header len %d", iphlen));
6703 KASSERT(thoff >= sizeof(struct tcphdr),
6704 ("invalid tcp header len %d", thoff));
6706 if (__predict_false(m->m_len < hoff + iphlen + thoff)) {
6707 m = m_pullup(m, hoff + iphlen + thoff);
6715 /* Set the LSO flag in the TX BD */
6716 flags = TX_BD_FLAGS_SW_LSO;
6718 /* Set the length of IP + TCP options (in 32 bit words) */
6719 flags |= (((iphlen + thoff -
6720 sizeof(struct ip) - sizeof(struct tcphdr)) >> 2) << 8);
6722 *mss0 = htole16(m->m_pkthdr.tso_segsz);
6729 bce_setup_serialize(struct bce_softc *sc)
6734 * Allocate serializer array
6737 /* Main + TX + RX */
6738 sc->serialize_cnt = 1 + sc->tx_ring_cnt + sc->rx_ring_cnt;
6741 kmalloc(sc->serialize_cnt * sizeof(struct lwkt_serialize *),
6742 M_DEVBUF, M_WAITOK | M_ZERO);
6747 * NOTE: Order is critical
6751 KKASSERT(i < sc->serialize_cnt);
6752 sc->serializes[i++] = &sc->main_serialize;
6754 sc->rx_serialize = i;
6755 for (j = 0; j < sc->rx_ring_cnt; ++j) {
6756 KKASSERT(i < sc->serialize_cnt);
6757 sc->serializes[i++] = &sc->rx_rings[j].rx_serialize;
6760 sc->tx_serialize = i;
6761 for (j = 0; j < sc->tx_ring_cnt; ++j) {
6762 KKASSERT(i < sc->serialize_cnt);
6763 sc->serializes[i++] = &sc->tx_rings[j].tx_serialize;
6766 KKASSERT(i == sc->serialize_cnt);
6770 bce_serialize(struct ifnet *ifp, enum ifnet_serialize slz)
6772 struct bce_softc *sc = ifp->if_softc;
6774 ifnet_serialize_array_enter(sc->serializes, sc->serialize_cnt,
6775 sc->tx_serialize, sc->rx_serialize, slz);
6779 bce_deserialize(struct ifnet *ifp, enum ifnet_serialize slz)
6781 struct bce_softc *sc = ifp->if_softc;
6783 ifnet_serialize_array_exit(sc->serializes, sc->serialize_cnt,
6784 sc->tx_serialize, sc->rx_serialize, slz);
6788 bce_tryserialize(struct ifnet *ifp, enum ifnet_serialize slz)
6790 struct bce_softc *sc = ifp->if_softc;
6792 return ifnet_serialize_array_try(sc->serializes, sc->serialize_cnt,
6793 sc->tx_serialize, sc->rx_serialize, slz);
6799 bce_serialize_assert(struct ifnet *ifp, enum ifnet_serialize slz,
6800 boolean_t serialized)
6802 struct bce_softc *sc = ifp->if_softc;
6804 ifnet_serialize_array_assert(sc->serializes, sc->serialize_cnt,
6805 sc->tx_serialize, sc->rx_serialize, slz, serialized);
6808 #endif /* INVARIANTS */
6811 bce_serialize_skipmain(struct bce_softc *sc)
6813 lwkt_serialize_array_enter(sc->serializes, sc->serialize_cnt, 1);
6817 bce_deserialize_skipmain(struct bce_softc *sc)
6819 lwkt_serialize_array_exit(sc->serializes, sc->serialize_cnt, 1);
6822 #ifdef IFPOLL_ENABLE
6825 bce_sysctl_npoll_offset(SYSCTL_HANDLER_ARGS)
6827 struct bce_softc *sc = (void *)arg1;
6828 struct ifnet *ifp = &sc->arpcom.ac_if;
6831 off = sc->npoll_ofs;
6832 error = sysctl_handle_int(oidp, &off, 0, req);
6833 if (error || req->newptr == NULL)
6838 ifnet_serialize_all(ifp);
6839 if (off >= ncpus2 || off % sc->rx_ring_cnt2 != 0) {
6843 sc->npoll_ofs = off;
6845 ifnet_deserialize_all(ifp);
6850 #endif /* IFPOLL_ENABLE */
6853 bce_set_timer_cpuid(struct bce_softc *sc, boolean_t polling)
6856 sc->bce_timer_cpuid = 0; /* XXX */
6858 sc->bce_timer_cpuid = sc->bce_msix[0].msix_cpuid;
6862 bce_alloc_intr(struct bce_softc *sc)
6866 bce_try_alloc_msix(sc);
6867 if (sc->bce_irq_type == PCI_INTR_TYPE_MSIX)
6870 sc->bce_irq_type = pci_alloc_1intr(sc->bce_dev, bce_msi_enable,
6871 &sc->bce_irq_rid, &irq_flags);
6873 sc->bce_res_irq = bus_alloc_resource_any(sc->bce_dev, SYS_RES_IRQ,
6874 &sc->bce_irq_rid, irq_flags);
6875 if (sc->bce_res_irq == NULL) {
6876 device_printf(sc->bce_dev, "PCI map interrupt failed\n");
6883 bce_try_alloc_msix(struct bce_softc *sc)
6885 struct bce_msix_data *msix;
6886 int offset, i, error;
6887 boolean_t setup = FALSE;
6889 if (sc->rx_ring_cnt == 1)
6892 if (sc->rx_ring_cnt2 == ncpus2) {
6896 (sc->rx_ring_cnt2 * device_get_unit(sc->bce_dev)) % ncpus2;
6898 offset = device_getenv_int(sc->bce_dev,
6899 "msix.offset", offset_def);
6900 if (offset >= ncpus2 || offset % sc->rx_ring_cnt2 != 0) {
6901 device_printf(sc->bce_dev,
6902 "invalid msix.offset %d, use %d\n",
6903 offset, offset_def);
6904 offset = offset_def;
6908 msix = &sc->bce_msix[0];
6909 msix->msix_serialize = &sc->main_serialize;
6910 msix->msix_func = bce_intr_msi_oneshot;
6911 msix->msix_arg = sc;
6912 KKASSERT(offset < ncpus2);
6913 msix->msix_cpuid = offset;
6914 ksnprintf(msix->msix_desc, sizeof(msix->msix_desc), "%s combo",
6915 device_get_nameunit(sc->bce_dev));
6917 for (i = 1; i < sc->rx_ring_cnt; ++i) {
6918 struct bce_rx_ring *rxr = &sc->rx_rings[i];
6920 msix = &sc->bce_msix[i];
6922 msix->msix_serialize = &rxr->rx_serialize;
6923 msix->msix_arg = rxr;
6924 msix->msix_cpuid = offset + (i % sc->rx_ring_cnt2);
6925 KKASSERT(msix->msix_cpuid < ncpus2);
6927 if (i < sc->tx_ring_cnt) {
6928 msix->msix_func = bce_intr_msix_rxtx;
6929 ksnprintf(msix->msix_desc, sizeof(msix->msix_desc),
6930 "%s rxtx%d", device_get_nameunit(sc->bce_dev), i);
6932 msix->msix_func = bce_intr_msix_rx;
6933 ksnprintf(msix->msix_desc, sizeof(msix->msix_desc),
6934 "%s rx%d", device_get_nameunit(sc->bce_dev), i);
6941 bce_setup_msix_table(sc);
6942 REG_WR(sc, BCE_PCI_MSIX_CONTROL, BCE_MSIX_MAX - 1);
6943 REG_WR(sc, BCE_PCI_MSIX_TBL_OFF_BIR, BCE_PCI_GRC_WINDOW2_BASE);
6944 REG_WR(sc, BCE_PCI_MSIX_PBA_OFF_BIT, BCE_PCI_GRC_WINDOW3_BASE);
6946 REG_RD(sc, BCE_PCI_MSIX_CONTROL);
6948 error = pci_setup_msix(sc->bce_dev);
6950 device_printf(sc->bce_dev, "Setup MSI-X failed\n");
6955 for (i = 0; i < sc->rx_ring_cnt; ++i) {
6956 msix = &sc->bce_msix[i];
6958 error = pci_alloc_msix_vector(sc->bce_dev, i, &msix->msix_rid,
6961 device_printf(sc->bce_dev,
6962 "Unable to allocate MSI-X %d on cpu%d\n",
6963 i, msix->msix_cpuid);
6967 msix->msix_res = bus_alloc_resource_any(sc->bce_dev,
6968 SYS_RES_IRQ, &msix->msix_rid, RF_ACTIVE);
6969 if (msix->msix_res == NULL) {
6970 device_printf(sc->bce_dev,
6971 "Unable to allocate MSI-X %d resource\n", i);
6977 pci_enable_msix(sc->bce_dev);
6978 sc->bce_irq_type = PCI_INTR_TYPE_MSIX;
6981 bce_free_msix(sc, setup);
6985 bce_setup_ring_cnt(struct bce_softc *sc)
6987 int msix_enable, ring_max, msix_cnt2, msix_cnt, i;
6989 sc->rx_ring_cnt = 1;
6990 sc->rx_ring_cnt2 = 1;
6991 sc->tx_ring_cnt = 1;
6993 if (BCE_CHIP_NUM(sc) != BCE_CHIP_NUM_5709)
6996 msix_enable = device_getenv_int(sc->bce_dev, "msix.enable",
7004 msix_cnt = pci_msix_count(sc->bce_dev);
7009 while ((1 << (i + 1)) <= msix_cnt)
7014 * One extra RX ring will be needed (see below), so make sure
7015 * that there are enough MSI-X vectors.
7017 if (msix_cnt == msix_cnt2) {
7020 * This probably will not happen; 5709/5716
7021 * come with 9 MSI-X vectors.
7024 if (msix_cnt2 <= 1) {
7025 device_printf(sc->bce_dev,
7026 "MSI-X count %d could not be used\n", msix_cnt);
7029 device_printf(sc->bce_dev, "MSI-X count %d is power of 2\n",
7034 * Setup RX ring count
7036 ring_max = BCE_RX_RING_MAX;
7037 if (ring_max > msix_cnt2)
7038 ring_max = msix_cnt2;
7039 sc->rx_ring_cnt2 = device_getenv_int(sc->bce_dev, "rx_rings",
7041 sc->rx_ring_cnt2 = if_ring_count2(sc->rx_ring_cnt2, ring_max);
7044 * One extra RX ring is allocated, since the first RX ring
7045 * could not be used for RSS hashed packets whose masked
7046 * hash is 0. The first RX ring is only used for packets
7047 * whose RSS hash could not be calculated, e.g. ARP packets.
7048 * This extra RX ring will be used for packets whose masked
7049 * hash is 0. The effective RX ring count involved in RSS
7050 * is still sc->rx_ring_cnt2.
7052 KKASSERT(sc->rx_ring_cnt2 + 1 <= msix_cnt);
7053 sc->rx_ring_cnt = sc->rx_ring_cnt2 + 1;
7056 * Setup TX ring count
7059 * TX ring count must be less than the effective RSS RX ring
7060 * count, since we use RX ring software data struct to save
7061 * status index and various other MSI-X related stuffs.
7063 ring_max = BCE_TX_RING_MAX;
7064 if (ring_max > msix_cnt2)
7065 ring_max = msix_cnt2;
7066 if (ring_max > sc->rx_ring_cnt2)
7067 ring_max = sc->rx_ring_cnt2;
7068 sc->tx_ring_cnt = device_getenv_int(sc->bce_dev, "tx_rings",
7070 sc->tx_ring_cnt = if_ring_count2(sc->tx_ring_cnt, ring_max);
7074 bce_free_msix(struct bce_softc *sc, boolean_t setup)
7078 KKASSERT(sc->rx_ring_cnt > 1);
7080 for (i = 0; i < sc->rx_ring_cnt; ++i) {
7081 struct bce_msix_data *msix = &sc->bce_msix[i];
7083 if (msix->msix_res != NULL) {
7084 bus_release_resource(sc->bce_dev, SYS_RES_IRQ,
7085 msix->msix_rid, msix->msix_res);
7087 if (msix->msix_rid >= 0)
7088 pci_release_msix_vector(sc->bce_dev, msix->msix_rid);
7091 pci_teardown_msix(sc->bce_dev);
7095 bce_free_intr(struct bce_softc *sc)
7097 if (sc->bce_irq_type != PCI_INTR_TYPE_MSIX) {
7098 if (sc->bce_res_irq != NULL) {
7099 bus_release_resource(sc->bce_dev, SYS_RES_IRQ,
7100 sc->bce_irq_rid, sc->bce_res_irq);
7102 if (sc->bce_irq_type == PCI_INTR_TYPE_MSI)
7103 pci_release_msi(sc->bce_dev);
7105 bce_free_msix(sc, TRUE);
7110 bce_setup_msix_table(struct bce_softc *sc)
7112 REG_WR(sc, BCE_PCI_GRC_WINDOW_ADDR, BCE_PCI_GRC_WINDOW_ADDR_SEP_WIN);
7113 REG_WR(sc, BCE_PCI_GRC_WINDOW2_ADDR, BCE_MSIX_TABLE_ADDR);
7114 REG_WR(sc, BCE_PCI_GRC_WINDOW3_ADDR, BCE_MSIX_PBA_ADDR);
7118 bce_setup_intr(struct bce_softc *sc)
7120 void (*irq_handle)(void *);
7123 if (sc->bce_irq_type == PCI_INTR_TYPE_MSIX)
7124 return bce_setup_msix(sc);
7126 if (sc->bce_irq_type == PCI_INTR_TYPE_LEGACY) {
7127 irq_handle = bce_intr_legacy;
7128 } else if (sc->bce_irq_type == PCI_INTR_TYPE_MSI) {
7129 if (BCE_CHIP_NUM(sc) == BCE_CHIP_NUM_5709) {
7130 irq_handle = bce_intr_msi_oneshot;
7131 sc->bce_flags |= BCE_ONESHOT_MSI_FLAG;
7133 irq_handle = bce_intr_msi;
7134 sc->bce_flags |= BCE_CHECK_MSI_FLAG;
7137 panic("%s: unsupported intr type %d",
7138 device_get_nameunit(sc->bce_dev), sc->bce_irq_type);
7141 error = bus_setup_intr(sc->bce_dev, sc->bce_res_irq, INTR_MPSAFE,
7142 irq_handle, sc, &sc->bce_intrhand, &sc->main_serialize);
7144 device_printf(sc->bce_dev, "Failed to setup IRQ!\n");
7147 sc->bce_msix[0].msix_cpuid = rman_get_cpuid(sc->bce_res_irq);
7148 sc->bce_msix[0].msix_serialize = &sc->main_serialize;
7154 bce_teardown_intr(struct bce_softc *sc)
7156 if (sc->bce_irq_type != PCI_INTR_TYPE_MSIX)
7157 bus_teardown_intr(sc->bce_dev, sc->bce_res_irq, sc->bce_intrhand);
7159 bce_teardown_msix(sc, sc->rx_ring_cnt);
7163 bce_setup_msix(struct bce_softc *sc)
7167 for (i = 0; i < sc->rx_ring_cnt; ++i) {
7168 struct bce_msix_data *msix = &sc->bce_msix[i];
7171 error = bus_setup_intr_descr(sc->bce_dev, msix->msix_res,
7172 INTR_MPSAFE, msix->msix_func, msix->msix_arg,
7173 &msix->msix_handle, msix->msix_serialize, msix->msix_desc);
7175 device_printf(sc->bce_dev, "could not set up %s "
7176 "interrupt handler.\n", msix->msix_desc);
7177 bce_teardown_msix(sc, i);
7185 bce_teardown_msix(struct bce_softc *sc, int msix_cnt)
7189 for (i = 0; i < msix_cnt; ++i) {
7190 struct bce_msix_data *msix = &sc->bce_msix[i];
7192 bus_teardown_intr(sc->bce_dev, msix->msix_res,
7198 bce_init_rss(struct bce_softc *sc)
7200 uint8_t key[BCE_RLUP_RSS_KEY_CNT * BCE_RLUP_RSS_KEY_SIZE];
7204 KKASSERT(sc->rx_ring_cnt > 2);
7207 * Configure RSS keys
7209 toeplitz_get_key(key, sizeof(key));
7210 for (i = 0; i < BCE_RLUP_RSS_KEY_CNT; ++i) {
7213 rss_key = BCE_RLUP_RSS_KEYVAL(key, i);
7214 BCE_RSS_DPRINTF(sc, 1, "rss_key%d 0x%08x\n", i, rss_key);
7216 REG_WR(sc, BCE_RLUP_RSS_KEY(i), rss_key);
7220 * Configure the redirect table
7223 * - The "queue ID" in redirect table is the software RX ring's
7224 * index _minus_ one.
7225 * - The last RX ring, whose "queue ID" is (sc->rx_ring_cnt - 2)
7226 * will be used for packets whose masked hash is 0.
7227 * (see also: comment in bce_setup_ring_cnt())
7229 * The redirect table is configured in following fashion, except
7230 * for the masked hash 0, which is noted above:
7231 * (hash & ring_cnt_mask) == rdr_table[(hash & rdr_table_mask)]
7233 for (i = 0; i < BCE_RXP_SCRATCH_RSS_TBL_MAX_ENTRIES; i++) {
7234 int shift = (i % 8) << 2, qid;
7236 qid = i % sc->rx_ring_cnt2;
7240 qid = sc->rx_ring_cnt - 2;
7241 KKASSERT(qid < (sc->rx_ring_cnt - 1));
7243 tbl |= qid << shift;
7245 BCE_RSS_DPRINTF(sc, 1, "tbl 0x%08x\n", tbl);
7246 REG_WR(sc, BCE_RLUP_RSS_DATA, tbl);
7247 REG_WR(sc, BCE_RLUP_RSS_COMMAND, (i >> 3) |
7248 BCE_RLUP_RSS_COMMAND_RSS_WRITE_MASK |
7249 BCE_RLUP_RSS_COMMAND_WRITE |
7250 BCE_RLUP_RSS_COMMAND_HASH_MASK);
7254 REG_WR(sc, BCE_RLUP_RSS_CONFIG,
7255 BCE_RLUP_RSS_CONFIG_IPV4_RSS_TYPE_ALL_XI);
7259 bce_npoll_coal_change(struct bce_softc *sc)
7261 uint32_t old_rx_cons, old_tx_cons;
7263 old_rx_cons = sc->bce_rx_quick_cons_trip_int;
7264 old_tx_cons = sc->bce_tx_quick_cons_trip_int;
7265 sc->bce_rx_quick_cons_trip_int = 1;
7266 sc->bce_tx_quick_cons_trip_int = 1;
7268 sc->bce_coalchg_mask |= BCE_COALMASK_TX_BDS_INT |
7269 BCE_COALMASK_RX_BDS_INT;
7270 bce_coal_change(sc);
7272 sc->bce_rx_quick_cons_trip_int = old_rx_cons;
7273 sc->bce_tx_quick_cons_trip_int = old_tx_cons;