Index: sys/dev/pci/if_bnx.c =================================================================== RCS file: /cvsroot/src/sys/dev/pci/if_bnx.c,v retrieving revision 1.18 diff -u -p -r1.18 if_bnx.c --- sys/dev/pci/if_bnx.c 7 Feb 2008 01:21:55 -0000 1.18 +++ sys/dev/pci/if_bnx.c 1 Jul 2008 20:26:27 -0000 @@ -320,6 +320,7 @@ int bnx_get_buf(struct bnx_softc *, stru u_int16_t *, u_int32_t *); int bnx_init_tx_chain(struct bnx_softc *); +void bnx_fill_rx_chain(struct bnx_softc *); int bnx_init_rx_chain(struct bnx_softc *); void bnx_free_rx_chain(struct bnx_softc *); void bnx_free_tx_chain(struct bnx_softc *); @@ -2724,7 +2725,7 @@ bnx_stop(struct ifnet *ifp, int disable) else bnx_reset(sc, BNX_DRV_MSG_CODE_SUSPEND_NO_WOL); - /* Free the RX lists. */ + /* Free RX buffers. */ bnx_free_rx_chain(sc); /* Free TX buffers. */ @@ -3062,11 +3063,12 @@ bnx_get_buf(struct bnx_softc *sc, struct else min_free_bd = (BNX_MAX_MRU + PAGE_SIZE - 1) / PAGE_SIZE; while (sc->free_rx_bd >= min_free_bd) { + /* Check whether this is a new mbuf allocation. */ if (m == NULL) { + /* Simulate an mbuf allocation failure. */ DBRUNIF(DB_RANDOMTRUE(bnx_debug_mbuf_allocation_failure), - BNX_PRINTF(sc, "Simulating mbuf allocation failure.\n"); - sc->mbuf_alloc_failed++; + sc->mbuf_sim_alloc_failed++; rc = ENOBUFS; goto bnx_get_buf_exit); @@ -3077,13 +3079,24 @@ bnx_get_buf(struct bnx_softc *sc, struct "%s(%d): RX mbuf header allocation failed!\n", __FILE__, __LINE__); - DBRUNIF(1, sc->mbuf_alloc_failed++); + sc->mbuf_alloc_failed++; rc = ENOBUFS; goto bnx_get_buf_exit; } DBRUNIF(1, sc->rx_mbuf_alloc++); + + /* Simulate an mbuf cluster allocation failure. */ + DBRUNIF(DB_RANDOMTRUE(bnx_debug_mbuf_allocation_failure), + m_freem(m_new); + sc->rx_mbuf_alloc--; + sc->mbuf_alloc_failed++; + sc->mbuf_sim_alloc_failed++; + rc = ENOBUFS; + goto bnx_get_buf_exit); + + /* Attach a cluster to the mbuf. */ if (sc->mbuf_alloc_size == MCLBYTES) MCLGET(m_new, M_DONTWAIT); else @@ -3095,19 +3108,20 @@ bnx_get_buf(struct bnx_softc *sc, struct __FILE__, __LINE__); m_freem(m_new); - DBRUNIF(1, sc->rx_mbuf_alloc--); - DBRUNIF(1, sc->mbuf_alloc_failed++); + sc->mbuf_alloc_failed++; rc = ENOBUFS; goto bnx_get_buf_exit; } } else { + /* Reuse an existing mbuf. */ m_new = m; m = NULL; m_new->m_data = m_new->m_ext.ext_buf; } + /* Initialize the mbuf cluster. */ m_new->m_len = m_new->m_pkthdr.len = sc->mbuf_alloc_size; /* Map the mbuf cluster into device memory. */ @@ -3118,7 +3132,6 @@ bnx_get_buf(struct bnx_softc *sc, struct __FILE__, __LINE__); m_freem(m_new); - DBRUNIF(1, sc->rx_mbuf_alloc--); rc = ENOBUFS; @@ -3127,14 +3140,26 @@ bnx_get_buf(struct bnx_softc *sc, struct bus_dmamap_sync(sc->bnx_dmatag, map, 0, map->dm_mapsize, BUS_DMASYNC_PREREAD); - /* Watch for overflow. */ - DBRUNIF((sc->free_rx_bd > USABLE_RX_BD), - aprint_error_dev(sc->bnx_dev, - "Too many free rx_bd (0x%04X > 0x%04X)!\n", - sc->free_rx_bd, (u_int16_t)USABLE_RX_BD)); + /* Make sure there is room in the receive chain. */ + if (map->dm_nsegs > sc->free_rx_bd) { + bus_dmamap_unload(sc->bnx_dmatag, map); + + m_freem(m_new); + DBRUNIF(1, sc->rx_mbuf_alloc--); + + rc = EFBIG; + goto bnx_get_buf_exit; + } + +#ifdef BNX_DEBUG + /* Track the distribution of buffer segments. */ + sc->rx_mbuf_segs[map->dm_nsegs]++; +#endif + /* Update some debug statistics counters */ DBRUNIF((sc->free_rx_bd < sc->rx_low_watermark), sc->rx_low_watermark = sc->free_rx_bd); + DBRUNIF((sc->free_rx_bd == 0), sc->rx_empty_count++); /* * Setup the rx_bd for the first segment @@ -3315,6 +3340,8 @@ bnx_free_tx_chain(struct bnx_softc *sc) BNX_TX_CHAIN_PAGE_SZ, BUS_DMASYNC_PREWRITE); } + sc->used_tx_bd = 0; + /* Check if we lost any mbufs in the process. */ DBRUNIF((sc->tx_mbuf_alloc), aprint_error_dev(sc->bnx_dev, @@ -3325,6 +3352,60 @@ bnx_free_tx_chain(struct bnx_softc *sc) } /****************************************************************************/ +/* Add mbufs to the RX chain until its full or an mbuf allocation error */ +/* occurs. */ +/* */ +/* Returns: */ +/* Nothing */ +/****************************************************************************/ +void +bnx_fill_rx_chain(struct bnx_softc *sc) +{ + u_int16_t prod, chain_prod; + u_int32_t prod_bseq; +#ifdef BNX_DEBUG + int rx_mbuf_alloc_before, free_rx_bd_before; +#endif + + DBPRINT(sc, BNX_EXCESSIVE_RECV, "Entering %s()\n", __FUNCTION__); + + prod = sc->rx_prod; + prod_bseq = sc->rx_prod_bseq; + +#ifdef BNX_DEBUG + rx_mbuf_alloc_before = sc->rx_mbuf_alloc; + free_rx_bd_before = sc->free_rx_bd; +#endif + + /* Keep filling the RX chain until it's full. */ + while (sc->free_rx_bd > 0) { + chain_prod = RX_CHAIN_IDX(prod); + if (bnx_get_buf(sc, NULL, &prod, &chain_prod, &prod_bseq)) { + /* Bail out if we can't add an mbuf to the chain. */ + break; + } + prod = NEXT_RX_BD(prod); + } + +#if 0 + DBRUNIF((sc->rx_mbuf_alloc - rx_mbuf_alloc_before), + BNX_PRINTF(sc, "%s(): Installed %d mbufs in %d rx_bd entries.\n", + __FUNCTION__, (sc->rx_mbuf_alloc - rx_mbuf_alloc_before), + (free_rx_bd_before - sc->free_rx_bd))); +#endif + + /* Save the RX chain producer index. */ + sc->rx_prod = prod; + sc->rx_prod_bseq = prod_bseq; + + /* Tell the chip about the waiting rx_bd's. */ + REG_WR16(sc, MB_RX_CID_ADDR + BNX_L2CTX_HOST_BDIDX, sc->rx_prod); + REG_WR(sc, MB_RX_CID_ADDR + BNX_L2CTX_HOST_BSEQ, sc->rx_prod_bseq); + + DBPRINT(sc, BNX_EXCESSIVE_RECV, "Exiting %s()\n", __FUNCTION__); +} + +/****************************************************************************/ /* Allocate memory and initialize the RX data structures. */ /* */ /* Returns: */ @@ -3335,8 +3416,7 @@ bnx_init_rx_chain(struct bnx_softc *sc) { struct rx_bd *rxbd; int i, rc = 0; - u_int16_t prod, chain_prod; - u_int32_t prod_bseq, val, addr; + u_int32_t val, addr; DBPRINT(sc, BNX_VERBOSE_RESET, "Entering %s()\n", __func__); @@ -3381,27 +3461,14 @@ bnx_init_rx_chain(struct bnx_softc *sc) val = (u_int32_t)(sc->rx_bd_chain_paddr[0]); CTX_WR(sc, GET_CID_ADDR(RX_CID), BNX_L2CTX_NX_BDHADDR_LO, val); - /* Allocate mbuf clusters for the rx_bd chain. */ - prod = prod_bseq = 0; - chain_prod = RX_CHAIN_IDX(prod); - if (bnx_get_buf(sc, NULL, &prod, &chain_prod, &prod_bseq)) { - BNX_PRINTF(sc, - "Error filling RX chain: rx_bd[0x%04X]!\n", chain_prod); - } - - /* Save the RX chain producer index. */ - sc->rx_prod = prod; - sc->rx_prod_bseq = prod_bseq; + /* Fill up the RX chain. */ + bnx_fill_rx_chain(sc); for (i = 0; i < RX_PAGES; i++) bus_dmamap_sync(sc->bnx_dmatag, sc->rx_bd_chain_map[i], 0, sc->rx_bd_chain_map[i]->dm_mapsize, BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE); - /* Tell the chip about the waiting rx_bd's. */ - REG_WR16(sc, MB_RX_CID_ADDR + BNX_L2CTX_HOST_BDIDX, sc->rx_prod); - REG_WR(sc, MB_RX_CID_ADDR + BNX_L2CTX_HOST_BSEQ, sc->rx_prod_bseq); - DBRUN(BNX_VERBOSE_RECV, bnx_dump_rx_chain(sc, 0, TOTAL_RX_BD)); DBPRINT(sc, BNX_VERBOSE_RESET, "Exiting %s()\n", __func__); @@ -3419,9 +3486,16 @@ void bnx_free_rx_chain(struct bnx_softc *sc) { int i; +#ifdef BNX_DEBUG + int rx_mbuf_alloc_before; +#endif DBPRINT(sc, BNX_VERBOSE_RESET, "Entering %s()\n", __func__); +#ifdef BNX_DEBUG + rx_mbuf_alloc_before = sc->rx_mbuf_alloc; +#endif + /* Free any mbufs still in the RX mbuf chain. */ for (i = 0; i < TOTAL_RX_BD; i++) { if (sc->rx_mbuf_ptr[i] != NULL) { @@ -3436,10 +3510,16 @@ bnx_free_rx_chain(struct bnx_softc *sc) } } + DBRUNIF((rx_mbuf_alloc_before - sc->rx_mbuf_alloc), + BNX_PRINTF(sc, "%s(): Released %d mbufs.\n", + __FUNCTION__, (rx_mbuf_alloc_before - sc->rx_mbuf_alloc))); + /* Clear each RX chain page. */ for (i = 0; i < RX_PAGES; i++) bzero((char *)sc->rx_bd_chain[i], BNX_RX_CHAIN_PAGE_SZ); + sc->free_rx_bd = USABLE_RX_BD; + /* Check if we lost any mbufs in the process. */ DBRUNIF((sc->rx_mbuf_alloc), aprint_error_dev(sc->bnx_dev, @@ -3536,8 +3616,10 @@ bnx_rx_intr(struct bnx_softc *sc) bus_space_barrier(sc->bnx_btag, sc->bnx_bhandle, 0, 0, BUS_SPACE_BARRIER_READ); + /* Update some debug statistics counters */ DBRUNIF((sc->free_rx_bd < sc->rx_low_watermark), sc->rx_low_watermark = sc->free_rx_bd); + DBRUNIF((sc->free_rx_bd == USABLE_RX_BD), sc->rx_empty_count++); /* * Scan through the receive chain as long @@ -3549,6 +3631,9 @@ bnx_rx_intr(struct bnx_softc *sc) unsigned int len; u_int32_t status; + /* Clear the mbuf pointer. */ + m = NULL; + /* Convert the producer/consumer indices to an actual * rx_bd index. */ @@ -3601,7 +3686,7 @@ bnx_rx_intr(struct bnx_softc *sc) bus_dmamap_unload(sc->bnx_dmatag, sc->rx_mbuf_map[sw_chain_cons]); - /* Remove the mbuf from the driver's chain. */ + /* Remove the mbuf from RX chain. */ m = sc->rx_mbuf_ptr[sw_chain_cons]; sc->rx_mbuf_ptr[sw_chain_cons] = NULL; @@ -3646,41 +3731,13 @@ bnx_rx_intr(struct bnx_softc *sc) len < (BNX_MIN_MTU - ETHER_CRC_LEN) || len > (BNX_MAX_JUMBO_ETHER_MTU_VLAN - ETHER_CRC_LEN)) { + /* Log the error and release the mbuf. */ ifp->if_ierrors++; DBRUNIF(1, sc->l2fhdr_status_errors++); - /* Reuse the mbuf for a new frame. */ - if (bnx_get_buf(sc, m, &sw_prod, - &sw_chain_prod, &sw_prod_bseq)) { - DBRUNIF(1, bnx_breakpoint(sc)); - panic("%s: Can't reuse RX mbuf!\n", - device_xname(sc->bnx_dev)); - } - continue; - } - - /* - * Get a new mbuf for the rx_bd. If no new - * mbufs are available then reuse the current mbuf, - * log an ierror on the interface, and generate - * an error in the system log. - */ - if (bnx_get_buf(sc, NULL, &sw_prod, &sw_chain_prod, - &sw_prod_bseq)) { - DBRUN(BNX_WARN, BNX_PRINTF(sc, "Failed to allocate " - "new mbuf, incoming frame dropped!\n")); - - ifp->if_ierrors++; - - /* Try and reuse the exisitng mbuf. */ - if (bnx_get_buf(sc, m, &sw_prod, - &sw_chain_prod, &sw_prod_bseq)) { - DBRUNIF(1, bnx_breakpoint(sc)); - panic("%s: Double mbuf allocation " - "failure!", - device_xname(sc->bnx_dev)); - } - continue; + m_freem(m); + m = NULL; + goto bnx_rx_int_next_rx; } /* Skip over the l2_fhdr when passing the data up @@ -3773,10 +3830,23 @@ bnx_rx_intr(struct bnx_softc *sc) #else VLAN_INPUT_TAG(ifp, m, l2fhdr->l2_fhdr_vlan_tag, - continue); + goto bnx_rx_int_next_rx); #endif } + /* Pass the mbuf off to the upper layers. */ + ifp->if_ipackets++; + +bnx_rx_int_next_rx: + sw_prod = NEXT_RX_BD(sw_prod); + } + + sw_cons = NEXT_RX_BD(sw_cons); + + /* If we have a packet, pass it up the stack */ + if (m) { + sc->rx_cons = sw_cons; + #if NBPFILTER > 0 /* * Handle BPF listeners. Let the BPF @@ -3786,17 +3856,14 @@ bnx_rx_intr(struct bnx_softc *sc) bpf_mtap(ifp->if_bpf, m); #endif - /* Pass the mbuf off to the upper layers. */ - ifp->if_ipackets++; DBPRINT(sc, BNX_VERBOSE_RECV, "%s(): Passing received frame up.\n", __func__); (*ifp->if_input)(ifp, m); DBRUNIF(1, sc->rx_mbuf_alloc--); + sw_cons = sc->rx_cons; } - sw_cons = NEXT_RX_BD(sw_cons); - /* Refresh hw_cons to see if there's new work */ if (sw_cons == hw_cons) { hw_cons = sc->hw_rx_cons = @@ -3813,19 +3880,16 @@ bnx_rx_intr(struct bnx_softc *sc) BUS_SPACE_BARRIER_READ); } + /* No new packets to process. Refill the RX chain and exit. */ + sc->rx_cons = sw_cons; + bnx_fill_rx_chain(sc); + for (i = 0; i < RX_PAGES; i++) bus_dmamap_sync(sc->bnx_dmatag, sc->rx_bd_chain_map[i], 0, sc->rx_bd_chain_map[i]->dm_mapsize, BUS_DMASYNC_PREWRITE); - sc->rx_cons = sw_cons; - sc->rx_prod = sw_prod; - sc->rx_prod_bseq = sw_prod_bseq; - - REG_WR16(sc, MB_RX_CID_ADDR + BNX_L2CTX_HOST_BDIDX, sc->rx_prod); - REG_WR(sc, MB_RX_CID_ADDR + BNX_L2CTX_HOST_BSEQ, sc->rx_prod_bseq); - DBPRINT(sc, BNX_INFO_RECV, "%s(exit): rx_prod = 0x%04X, " "rx_cons = 0x%04X, rx_prod_bseq = 0x%08X\n", __func__, sc->rx_prod, sc->rx_cons, sc->rx_prod_bseq); @@ -4123,6 +4187,7 @@ bnx_tx_encap(struct bnx_softc *sc, struc "Error mapping mbuf into TX chain!\n"); m_freem(m0); *m_head = NULL; + sc->tx_dma_map_failures++; return (error); } bus_dmamap_sync(sc->bnx_dmatag, map, 0, map->dm_mapsize, @@ -4375,6 +4440,13 @@ bnx_watchdog(struct ifnet *ifp) DBRUN(BNX_WARN_SEND, bnx_dump_driver_state(sc); bnx_dump_status_block(sc)); + /* + * If we are in this routine because of pause frames, then + * don't reset the hardware. + */ + if (REG_RD(sc, BNX_EMAC_TX_STATUS) & BNX_EMAC_TX_STATUS_XOFFED) + return; + aprint_error_dev(sc->bnx_dev, "Watchdog timeout -- resetting!\n"); /* DBRUN(BNX_FATAL, bnx_breakpoint(sc)); */ @@ -5461,6 +5533,18 @@ bnx_dump_driver_state(struct bnx_softc * " 0x%08X - (sc->tx_prod_bseq) tx producer bseq index\n", sc->tx_prod_bseq); + BNX_PRINTF(sc, + " 0x%08X - (sc->tx_mbuf_alloc) tx mbufs allocated\n", + sc->tx_mbuf_alloc); + + BNX_PRINTF(sc, + " 0x%08X - (sc->used_tx_bd) used tx_bd's\n", + sc->used_tx_bd); + + BNX_PRINTF(sc, + " 0x%08X/%08X - (sc->tx_hi_watermark) tx hi watermark\n", + sc->tx_hi_watermark, sc->max_tx_bd); + BNX_PRINTF(sc, " 0x%08X - (sc->rx_prod) rx producer index\n", sc->rx_prod); @@ -5483,22 +5567,14 @@ bnx_dump_driver_state(struct bnx_softc * sc->rx_low_watermark, (u_int32_t) USABLE_RX_BD); BNX_PRINTF(sc, - " 0x%08X - (sc->txmbuf_alloc) tx mbufs allocated\n", - sc->tx_mbuf_alloc); - - BNX_PRINTF(sc, - " 0x%08X - (sc->rx_mbuf_alloc) rx mbufs allocated\n", - sc->rx_mbuf_alloc); - - BNX_PRINTF(sc, " 0x%08X - (sc->used_tx_bd) used tx_bd's\n", - sc->used_tx_bd); - - BNX_PRINTF(sc, "0x%08X/%08X - (sc->tx_hi_watermark) tx hi watermark\n", - sc->tx_hi_watermark, (u_int32_t) USABLE_TX_BD); + " 0x%08X - (sc->mbuf_alloc_failed) " + "mbuf alloc failures\n", + sc->mbuf_alloc_failed); BNX_PRINTF(sc, - " 0x%08X - (sc->mbuf_alloc_failed) failed mbuf alloc\n", - sc->mbuf_alloc_failed); + " 0x%0X - (sc->mbuf_sim_allocated_failed) " + "simulated mbuf alloc failures\n", + sc->mbuf_sim_alloc_failed); BNX_PRINTF(sc, "-------------------------------------------" "-----------------------------\n");