net/sfc/base: add efsys macro to get memory region size
authorAndy Moreton <amoreton@solarflare.com>
Tue, 20 Feb 2018 07:33:52 +0000 (07:33 +0000)
committerFerruh Yigit <ferruh.yigit@intel.com>
Fri, 30 Mar 2018 12:08:42 +0000 (14:08 +0200)
EFSYS_MEM_SIZE() reports the DMA mapped size of an efsys_mem_t
allocated region (the allocation size may be different due to
memory allocator and DMA alignment restrictions).

This ensures that common code internals have explicit knowledge
of the usable size of DMA mapped memory regions.

Signed-off-by: Andy Moreton <amoreton@solarflare.com>
Signed-off-by: Andrew Rybchenko <arybchenko@solarflare.com>
drivers/net/sfc/base/ef10_rx.c
drivers/net/sfc/base/ef10_tx.c
drivers/net/sfc/base/efx_intr.c
drivers/net/sfc/base/efx_mcdi.c
drivers/net/sfc/base/siena_phy.c
drivers/net/sfc/efsys.h

index 79f2587..86a6ac7 100644 (file)
@@ -37,6 +37,11 @@ efx_mcdi_init_rxq(
 
        EFSYS_ASSERT3U(ndescs, <=, EFX_RXQ_MAXNDESCS);
 
+       if ((esmp == NULL) || (EFSYS_MEM_SIZE(esmp) < EFX_RXQ_SIZE(ndescs))) {
+               rc = EINVAL;
+               goto fail1;
+       }
+
        if (ps_bufsize > 0)
                dma_mode = MC_CMD_INIT_RXQ_EXT_IN_PACKED_STREAM;
        else
@@ -103,11 +108,13 @@ efx_mcdi_init_rxq(
 
        if (req.emr_rc != 0) {
                rc = req.emr_rc;
-               goto fail1;
+               goto fail2;
        }
 
        return (0);
 
+fail2:
+       EFSYS_PROBE(fail2);
 fail1:
        EFSYS_PROBE1(fail1, efx_rc_t, rc);
 
index 3087306..8ca0b55 100644 (file)
@@ -42,10 +42,15 @@ efx_mcdi_init_txq(
        EFSYS_ASSERT(EFX_TXQ_MAX_BUFS >=
            EFX_TXQ_NBUFS(enp->en_nic_cfg.enc_txq_max_ndescs));
 
+       if ((esmp == NULL) || (EFSYS_MEM_SIZE(esmp) < EFX_TXQ_SIZE(ndescs))) {
+               rc = EINVAL;
+               goto fail1;
+       }
+
        npages = EFX_TXQ_NBUFS(ndescs);
        if (MC_CMD_INIT_TXQ_IN_LEN(npages) > sizeof (payload)) {
                rc = EINVAL;
-               goto fail1;
+               goto fail2;
        }
 
        (void) memset(payload, 0, sizeof (payload));
@@ -94,11 +99,13 @@ efx_mcdi_init_txq(
 
        if (req.emr_rc != 0) {
                rc = req.emr_rc;
-               goto fail2;
+               goto fail3;
        }
 
        return (0);
 
+fail3:
+       EFSYS_PROBE(fail3);
 fail2:
        EFSYS_PROBE(fail2);
 fail1:
index 49cfaf7..b518916 100644 (file)
@@ -289,6 +289,12 @@ siena_intr_init(
 {
        efx_intr_t *eip = &(enp->en_intr);
        efx_oword_t oword;
+       efx_rc_t rc;
+
+       if ((esmp == NULL) || (EFSYS_MEM_SIZE(esmp) < EFX_INTR_SIZE)) {
+               rc = EINVAL;
+               goto fail1;
+       }
 
        /*
         * bug17213 workaround.
@@ -320,6 +326,11 @@ siena_intr_init(
        EFX_BAR_WRITEO(enp, FR_AZ_INT_ADR_REG_KER, &oword);
 
        return (0);
+
+fail1:
+       EFSYS_PROBE1(fail1, efx_rc_t, rc);
+
+       return (rc);
 }
 
 static                 void
index 9d7b741..3352b08 100644 (file)
@@ -1815,11 +1815,13 @@ efx_mcdi_mac_stats(
            MAC_STATS_IN_PERIOD_MS, (enable | events) ? period_ms : 0);
 
        if (esmp != NULL) {
-               int bytes = MC_CMD_MAC_NSTATS * sizeof (uint64_t);
+               uint32_t bytes = MC_CMD_MAC_NSTATS * sizeof (uint64_t);
 
                EFX_STATIC_ASSERT(MC_CMD_MAC_NSTATS * sizeof (uint64_t) <=
                    EFX_MAC_STATS_SIZE);
 
+               EFSYS_ASSERT3U(bytes, <=, (uint32_t)EFSYS_MEM_SIZE(esmp));
+
                MCDI_IN_SET_DWORD(req, MAC_STATS_IN_DMA_ADDR_LO,
                            EFSYS_MEM_ADDR(esmp) & 0xffffffff);
                MCDI_IN_SET_DWORD(req, MAC_STATS_IN_DMA_ADDR_HI,
index d638646..4b2190d 100644 (file)
@@ -534,6 +534,11 @@ siena_phy_stats_update(
                            MC_CMD_PHY_STATS_OUT_DMA_LEN)];
        efx_rc_t rc;
 
+       if ((esmp == NULL) || (EFSYS_MEM_SIZE(esmp) < EFX_PHY_STATS_SIZE)) {
+               rc = EINVAL;
+               goto fail1;
+       }
+
        (void) memset(payload, 0, sizeof (payload));
        req.emr_cmd = MC_CMD_PHY_STATS;
        req.emr_in_buf = payload;
@@ -550,7 +555,7 @@ siena_phy_stats_update(
 
        if (req.emr_rc != 0) {
                rc = req.emr_rc;
-               goto fail1;
+               goto fail2;
        }
        EFSYS_ASSERT3U(req.emr_out_length, ==, MC_CMD_PHY_STATS_OUT_DMA_LEN);
 
@@ -559,6 +564,8 @@ siena_phy_stats_update(
 
        return (0);
 
+fail2:
+       EFSYS_PROBE(fail2);
 fail1:
        EFSYS_PROBE1(fail1, efx_rc_t, rc);
 
index fcc4f5f..7958483 100644 (file)
@@ -374,6 +374,9 @@ typedef struct efsys_mem_s {
        } while (B_FALSE)
 
 
+#define        EFSYS_MEM_SIZE(_esmp)                                           \
+       ((_esmp)->esm_mz->len)
+
 #define EFSYS_MEM_ADDR(_esmp)                                          \
        ((_esmp)->esm_addr)