bnxt_en: Refactor statistics code and structures.
authorMichael Chan <michael.chan@broadcom.com>
Mon, 27 Jul 2020 09:40:39 +0000 (05:40 -0400)
committerDavid S. Miller <davem@davemloft.net>
Mon, 27 Jul 2020 18:47:33 +0000 (11:47 -0700)
The driver manages multiple statistics structures of different sizes.
They are all allocated, freed, and handled practically the same.  Define
a new bnxt_stats_mem structure and common allocation and free functions
for all staistics memory blocks.

Reviewed-by: Vasundhara Volam <vasundhara-v.volam@broadcom.com>
Signed-off-by: Michael Chan <michael.chan@broadcom.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
drivers/net/ethernet/broadcom/bnxt/bnxt.c
drivers/net/ethernet/broadcom/bnxt/bnxt.h
drivers/net/ethernet/broadcom/bnxt/bnxt_dcb.c
drivers/net/ethernet/broadcom/bnxt/bnxt_ethtool.c

index 9d0800c56e7ba8f6a9b83980920ebe18d1594314..e08d636b7d9107154e58e505b71d7fdc961e255b 100644 (file)
@@ -3711,61 +3711,55 @@ static int bnxt_alloc_hwrm_short_cmd_req(struct bnxt *bp)
        return 0;
 }
 
-static void bnxt_free_port_stats(struct bnxt *bp)
+static void bnxt_free_stats_mem(struct bnxt *bp, struct bnxt_stats_mem *stats)
 {
-       struct pci_dev *pdev = bp->pdev;
+       if (stats->hw_stats) {
+               dma_free_coherent(&bp->pdev->dev, stats->len, stats->hw_stats,
+                                 stats->hw_stats_map);
+               stats->hw_stats = NULL;
+       }
+}
 
-       bp->flags &= ~BNXT_FLAG_PORT_STATS;
-       bp->flags &= ~BNXT_FLAG_PORT_STATS_EXT;
+static int bnxt_alloc_stats_mem(struct bnxt *bp, struct bnxt_stats_mem *stats)
+{
+       stats->hw_stats = dma_alloc_coherent(&bp->pdev->dev, stats->len,
+                                            &stats->hw_stats_map, GFP_KERNEL);
+       if (!stats->hw_stats)
+               return -ENOMEM;
 
-       if (bp->hw_rx_port_stats) {
-               dma_free_coherent(&pdev->dev, bp->hw_port_stats_size,
-                                 bp->hw_rx_port_stats,
-                                 bp->hw_rx_port_stats_map);
-               bp->hw_rx_port_stats = NULL;
-       }
+       memset(stats->hw_stats, 0, stats->len);
+       return 0;
+}
 
-       if (bp->hw_tx_port_stats_ext) {
-               dma_free_coherent(&pdev->dev, sizeof(struct tx_port_stats_ext),
-                                 bp->hw_tx_port_stats_ext,
-                                 bp->hw_tx_port_stats_ext_map);
-               bp->hw_tx_port_stats_ext = NULL;
-       }
+static void bnxt_free_port_stats(struct bnxt *bp)
+{
+       bp->flags &= ~BNXT_FLAG_PORT_STATS;
+       bp->flags &= ~BNXT_FLAG_PORT_STATS_EXT;
 
-       if (bp->hw_rx_port_stats_ext) {
-               dma_free_coherent(&pdev->dev, sizeof(struct rx_port_stats_ext),
-                                 bp->hw_rx_port_stats_ext,
-                                 bp->hw_rx_port_stats_ext_map);
-               bp->hw_rx_port_stats_ext = NULL;
-       }
+       bnxt_free_stats_mem(bp, &bp->port_stats);
+       bnxt_free_stats_mem(bp, &bp->rx_port_stats_ext);
+       bnxt_free_stats_mem(bp, &bp->tx_port_stats_ext);
 }
 
 static void bnxt_free_ring_stats(struct bnxt *bp)
 {
-       struct pci_dev *pdev = bp->pdev;
-       int size, i;
+       int i;
 
        if (!bp->bnapi)
                return;
 
-       size = bp->hw_ring_stats_size;
-
        for (i = 0; i < bp->cp_nr_rings; i++) {
                struct bnxt_napi *bnapi = bp->bnapi[i];
                struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
 
-               if (cpr->hw_stats) {
-                       dma_free_coherent(&pdev->dev, size, cpr->hw_stats,
-                                         cpr->hw_stats_map);
-                       cpr->hw_stats = NULL;
-               }
+               bnxt_free_stats_mem(bp, &cpr->stats);
        }
 }
 
 static int bnxt_alloc_stats(struct bnxt *bp)
 {
        u32 size, i;
-       struct pci_dev *pdev = bp->pdev;
+       int rc;
 
        size = bp->hw_ring_stats_size;
 
@@ -3773,11 +3767,10 @@ static int bnxt_alloc_stats(struct bnxt *bp)
                struct bnxt_napi *bnapi = bp->bnapi[i];
                struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
 
-               cpr->hw_stats = dma_alloc_coherent(&pdev->dev, size,
-                                                  &cpr->hw_stats_map,
-                                                  GFP_KERNEL);
-               if (!cpr->hw_stats)
-                       return -ENOMEM;
+               cpr->stats.len = size;
+               rc = bnxt_alloc_stats_mem(bp, &cpr->stats);
+               if (rc)
+                       return rc;
 
                cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
        }
@@ -3785,22 +3778,14 @@ static int bnxt_alloc_stats(struct bnxt *bp)
        if (BNXT_VF(bp) || bp->chip_num == CHIP_NUM_58700)
                return 0;
 
-       if (bp->hw_rx_port_stats)
+       if (bp->port_stats.hw_stats)
                goto alloc_ext_stats;
 
-       bp->hw_port_stats_size = BNXT_PORT_STATS_SIZE;
-
-       bp->hw_rx_port_stats =
-               dma_alloc_coherent(&pdev->dev, bp->hw_port_stats_size,
-                                  &bp->hw_rx_port_stats_map,
-                                  GFP_KERNEL);
-       if (!bp->hw_rx_port_stats)
-               return -ENOMEM;
+       bp->port_stats.len = BNXT_PORT_STATS_SIZE;
+       rc = bnxt_alloc_stats_mem(bp, &bp->port_stats);
+       if (rc)
+               return rc;
 
-       bp->hw_tx_port_stats = (void *)bp->hw_rx_port_stats +
-                              BNXT_TX_PORT_STATS_BYTE_OFFSET;
-       bp->hw_tx_port_stats_map = bp->hw_rx_port_stats_map +
-                                  BNXT_TX_PORT_STATS_BYTE_OFFSET;
        bp->flags |= BNXT_FLAG_PORT_STATS;
 
 alloc_ext_stats:
@@ -3809,26 +3794,26 @@ alloc_ext_stats:
                if (!(bp->fw_cap & BNXT_FW_CAP_EXT_STATS_SUPPORTED))
                        return 0;
 
-       if (bp->hw_rx_port_stats_ext)
+       if (bp->rx_port_stats_ext.hw_stats)
                goto alloc_tx_ext_stats;
 
-       bp->hw_rx_port_stats_ext =
-               dma_alloc_coherent(&pdev->dev, sizeof(struct rx_port_stats_ext),
-                                  &bp->hw_rx_port_stats_ext_map, GFP_KERNEL);
-       if (!bp->hw_rx_port_stats_ext)
+       bp->rx_port_stats_ext.len = sizeof(struct rx_port_stats_ext);
+       rc = bnxt_alloc_stats_mem(bp, &bp->rx_port_stats_ext);
+       /* Extended stats are optional */
+       if (rc)
                return 0;
 
 alloc_tx_ext_stats:
-       if (bp->hw_tx_port_stats_ext)
+       if (bp->tx_port_stats_ext.hw_stats)
                return 0;
 
        if (bp->hwrm_spec_code >= 0x10902 ||
            (bp->fw_cap & BNXT_FW_CAP_EXT_STATS_SUPPORTED)) {
-               bp->hw_tx_port_stats_ext =
-                       dma_alloc_coherent(&pdev->dev,
-                                          sizeof(struct tx_port_stats_ext),
-                                          &bp->hw_tx_port_stats_ext_map,
-                                          GFP_KERNEL);
+               bp->tx_port_stats_ext.len = sizeof(struct tx_port_stats_ext);
+               rc = bnxt_alloc_stats_mem(bp, &bp->tx_port_stats_ext);
+               /* Extended stats are optional */
+               if (rc)
+                       return 0;
        }
        bp->flags |= BNXT_FLAG_PORT_STATS_EXT;
        return 0;
@@ -6439,7 +6424,7 @@ static int bnxt_hwrm_stat_ctx_alloc(struct bnxt *bp)
                struct bnxt_napi *bnapi = bp->bnapi[i];
                struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
 
-               req.stats_dma_addr = cpu_to_le64(cpr->hw_stats_map);
+               req.stats_dma_addr = cpu_to_le64(cpr->stats.hw_stats_map);
 
                rc = _hwrm_send_message(bp, &req, sizeof(req),
                                        HWRM_CMD_TIMEOUT);
@@ -7480,8 +7465,9 @@ static int bnxt_hwrm_port_qstats(struct bnxt *bp)
 
        bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_QSTATS, -1, -1);
        req.port_id = cpu_to_le16(pf->port_id);
-       req.tx_stat_host_addr = cpu_to_le64(bp->hw_tx_port_stats_map);
-       req.rx_stat_host_addr = cpu_to_le64(bp->hw_rx_port_stats_map);
+       req.tx_stat_host_addr = cpu_to_le64(bp->port_stats.hw_stats_map +
+                                           BNXT_TX_PORT_STATS_BYTE_OFFSET);
+       req.rx_stat_host_addr = cpu_to_le64(bp->port_stats.hw_stats_map);
        return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
 }
 
@@ -7500,11 +7486,11 @@ static int bnxt_hwrm_port_qstats_ext(struct bnxt *bp)
        bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_QSTATS_EXT, -1, -1);
        req.port_id = cpu_to_le16(pf->port_id);
        req.rx_stat_size = cpu_to_le16(sizeof(struct rx_port_stats_ext));
-       req.rx_stat_host_addr = cpu_to_le64(bp->hw_rx_port_stats_ext_map);
-       tx_stat_size = bp->hw_tx_port_stats_ext ?
-                      sizeof(*bp->hw_tx_port_stats_ext) : 0;
+       req.rx_stat_host_addr = cpu_to_le64(bp->rx_port_stats_ext.hw_stats_map);
+       tx_stat_size = bp->tx_port_stats_ext.hw_stats ?
+                      sizeof(struct tx_port_stats_ext) : 0;
        req.tx_stat_size = cpu_to_le16(tx_stat_size);
-       req.tx_stat_host_addr = cpu_to_le64(bp->hw_tx_port_stats_ext_map);
+       req.tx_stat_host_addr = cpu_to_le64(bp->tx_port_stats_ext.hw_stats_map);
        mutex_lock(&bp->hwrm_cmd_lock);
        rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
        if (!rc) {
@@ -9582,7 +9568,7 @@ static void bnxt_get_ring_stats(struct bnxt *bp,
        for (i = 0; i < bp->cp_nr_rings; i++) {
                struct bnxt_napi *bnapi = bp->bnapi[i];
                struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
-               struct ctx_hw_stats *hw_stats = cpr->hw_stats;
+               struct ctx_hw_stats *hw_stats = cpr->stats.hw_stats;
 
                stats->rx_packets += le64_to_cpu(hw_stats->rx_ucast_pkts);
                stats->rx_packets += le64_to_cpu(hw_stats->rx_mcast_pkts);
@@ -9643,8 +9629,9 @@ bnxt_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
        bnxt_add_prev_stats(bp, stats);
 
        if (bp->flags & BNXT_FLAG_PORT_STATS) {
-               struct rx_port_stats *rx = bp->hw_rx_port_stats;
-               struct tx_port_stats *tx = bp->hw_tx_port_stats;
+               struct rx_port_stats *rx = bp->port_stats.hw_stats;
+               struct tx_port_stats *tx = bp->port_stats.hw_stats +
+                                          BNXT_TX_PORT_STATS_BYTE_OFFSET;
 
                stats->rx_crc_errors = le64_to_cpu(rx->rx_fcs_err_frames);
                stats->rx_frame_errors = le64_to_cpu(rx->rx_align_err_frames);
index df6289722721b37bdfc35c94a753f00c96ee7357..7e9fe1fe33a9b75cb65596ada67bacc29579307f 100644 (file)
@@ -919,6 +919,12 @@ struct bnxt_sw_stats {
        struct bnxt_cmn_sw_stats cmn;
 };
 
+struct bnxt_stats_mem {
+       void            *hw_stats;
+       dma_addr_t      hw_stats_map;
+       int             len;
+};
+
 struct bnxt_cp_ring_info {
        struct bnxt_napi        *bnapi;
        u32                     cp_raw_cons;
@@ -943,8 +949,7 @@ struct bnxt_cp_ring_info {
 
        dma_addr_t              cp_desc_mapping[MAX_CP_PAGES];
 
-       struct ctx_hw_stats     *hw_stats;
-       dma_addr_t              hw_stats_map;
+       struct bnxt_stats_mem   stats;
        u32                     hw_stats_ctx_id;
 
        struct bnxt_sw_stats    sw_stats;
@@ -1776,15 +1781,9 @@ struct bnxt {
        dma_addr_t              hwrm_cmd_kong_resp_dma_addr;
 
        struct rtnl_link_stats64        net_stats_prev;
-       struct rx_port_stats    *hw_rx_port_stats;
-       struct tx_port_stats    *hw_tx_port_stats;
-       struct rx_port_stats_ext        *hw_rx_port_stats_ext;
-       struct tx_port_stats_ext        *hw_tx_port_stats_ext;
-       dma_addr_t              hw_rx_port_stats_map;
-       dma_addr_t              hw_tx_port_stats_map;
-       dma_addr_t              hw_rx_port_stats_ext_map;
-       dma_addr_t              hw_tx_port_stats_ext_map;
-       int                     hw_port_stats_size;
+       struct bnxt_stats_mem   port_stats;
+       struct bnxt_stats_mem   rx_port_stats_ext;
+       struct bnxt_stats_mem   tx_port_stats_ext;
        u16                     fw_rx_stats_ext_size;
        u16                     fw_tx_stats_ext_size;
        u16                     hw_ring_stats_size;
index 02b27551d34d916b56cba280b18c11841e63f94e..8e90224c43a21434d4f832742d34c87cee4b9629 100644 (file)
@@ -544,7 +544,7 @@ static int bnxt_dcbnl_ieee_setets(struct net_device *dev, struct ieee_ets *ets)
 static int bnxt_dcbnl_ieee_getpfc(struct net_device *dev, struct ieee_pfc *pfc)
 {
        struct bnxt *bp = netdev_priv(dev);
-       __le64 *stats = (__le64 *)bp->hw_rx_port_stats;
+       __le64 *stats = bp->port_stats.hw_stats;
        struct ieee_pfc *my_pfc = bp->ieee_pfc;
        long rx_off, tx_off;
        int i, rc;
index be48727be6ca32f93971645fb8269346a3a05620..ae89f507677d08a244a414372419b806423cb1b4 100644 (file)
@@ -559,7 +559,8 @@ static void bnxt_get_ethtool_stats(struct net_device *dev,
        for (i = 0; i < bp->cp_nr_rings; i++) {
                struct bnxt_napi *bnapi = bp->bnapi[i];
                struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
-               __le64 *hw_stats = (__le64 *)cpr->hw_stats;
+               struct ctx_hw_stats *hw = cpr->stats.hw_stats;
+               __le64 *hw_stats = cpr->stats.hw_stats;
                u64 *sw;
                int k;
 
@@ -593,9 +594,9 @@ skip_tpa_ring_stats:
                        buf[j] = sw[k];
 
                bnxt_sw_func_stats[RX_TOTAL_DISCARDS].counter +=
-                       le64_to_cpu(cpr->hw_stats->rx_discard_pkts);
+                       le64_to_cpu(hw->rx_discard_pkts);
                bnxt_sw_func_stats[TX_TOTAL_DISCARDS].counter +=
-                       le64_to_cpu(cpr->hw_stats->tx_discard_pkts);
+                       le64_to_cpu(hw->tx_discard_pkts);
        }
 
        for (i = 0; i < BNXT_NUM_SW_FUNC_STATS; i++, j++)
@@ -603,7 +604,7 @@ skip_tpa_ring_stats:
 
 skip_ring_stats:
        if (bp->flags & BNXT_FLAG_PORT_STATS) {
-               __le64 *port_stats = (__le64 *)bp->hw_rx_port_stats;
+               __le64 *port_stats = bp->port_stats.hw_stats;
 
                for (i = 0; i < BNXT_NUM_PORT_STATS; i++, j++) {
                        buf[j] = le64_to_cpu(*(port_stats +
@@ -611,8 +612,8 @@ skip_ring_stats:
                }
        }
        if (bp->flags & BNXT_FLAG_PORT_STATS_EXT) {
-               __le64 *rx_port_stats_ext = (__le64 *)bp->hw_rx_port_stats_ext;
-               __le64 *tx_port_stats_ext = (__le64 *)bp->hw_tx_port_stats_ext;
+               __le64 *rx_port_stats_ext = bp->rx_port_stats_ext.hw_stats;
+               __le64 *tx_port_stats_ext = bp->tx_port_stats_ext.hw_stats;
 
                for (i = 0; i < bp->fw_rx_stats_ext_size; i++, j++) {
                        buf[j] = le64_to_cpu(*(rx_port_stats_ext +