net/sfc/base: refactoring eliminating code analysis warnings
[dpdk.git] / drivers / net / sfc / base / efx_mcdi.c
index 8d91812..8156acc 100644 (file)
@@ -1,31 +1,7 @@
-/*
- * Copyright (c) 2008-2016 Solarflare Communications Inc.
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- * 1. Redistributions of source code must retain the above copyright notice,
- *    this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright notice,
- *    this list of conditions and the following disclaimer in the documentation
- *    and/or other materials provided with the distribution.
+/* SPDX-License-Identifier: BSD-3-Clause
  *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
- * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
- * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
- * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
- * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
- * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
- * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
- * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
- * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
- * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- *
- * The views and conclusions contained in the software and documentation are
- * those of the authors and should not be interpreted as representing official
- * policies, either expressed or implied, of the FreeBSD Project.
+ * Copyright (c) 2008-2018 Solarflare Communications Inc.
+ * All rights reserved.
  */
 
 #include "efx.h"
@@ -69,7 +45,7 @@ static const efx_mcdi_ops_t   __efx_mcdi_siena_ops = {
 
 #endif /* EFSYS_OPT_SIENA */
 
-#if EFSYS_OPT_HUNTINGTON || EFSYS_OPT_MEDFORD
+#if EFSYS_OPT_HUNTINGTON || EFSYS_OPT_MEDFORD || EFSYS_OPT_MEDFORD2
 
 static const efx_mcdi_ops_t    __efx_mcdi_ef10_ops = {
        ef10_mcdi_init,                 /* emco_init */
@@ -82,7 +58,7 @@ static const efx_mcdi_ops_t   __efx_mcdi_ef10_ops = {
        ef10_mcdi_get_timeout,          /* emco_get_timeout */
 };
 
-#endif /* EFSYS_OPT_HUNTINGTON || EFSYS_OPT_MEDFORD */
+#endif /* EFSYS_OPT_HUNTINGTON || EFSYS_OPT_MEDFORD || EFSYS_OPT_MEDFORD2 */
 
 
 
@@ -116,6 +92,12 @@ efx_mcdi_init(
                break;
 #endif /* EFSYS_OPT_MEDFORD */
 
+#if EFSYS_OPT_MEDFORD2
+       case EFX_FAMILY_MEDFORD2:
+               emcop = &__efx_mcdi_ef10_ops;
+               break;
+#endif /* EFSYS_OPT_MEDFORD2 */
+
        default:
                EFSYS_ASSERT(0);
                rc = ENOTSUP;
@@ -290,7 +272,8 @@ efx_mcdi_request_start(
         */
        if ((max_version >= 2) &&
            ((emrp->emr_cmd > MC_CMD_CMD_SPACE_ESCAPE_7) ||
-           (emrp->emr_in_length > MCDI_CTL_SDU_LEN_MAX_V1))) {
+           (emrp->emr_in_length > MCDI_CTL_SDU_LEN_MAX_V1) ||
+           (emrp->emr_out_length > MCDI_CTL_SDU_LEN_MAX_V1))) {
                /* Construct MCDI v2 header */
                hdr_len = sizeof (hdr);
                EFX_POPULATE_DWORD_8(hdr[0],
@@ -792,9 +775,8 @@ efx_mcdi_ev_cpl(
                        emrp->emr_rc = 0;
                }
        }
-       if (errcode == 0) {
+       if (emrp->emr_rc == 0)
                efx_mcdi_finish_response(enp, emrp);
-       }
 
        emtp->emt_ev_cpl(emtp->emt_context);
 }
@@ -809,6 +791,8 @@ efx_mcdi_get_proxy_handle(
 {
        efx_rc_t rc;
 
+       _NOTE(ARGUNUSED(enp))
+
        /*
         * Return proxy handle from MCDI request that returned with error
         * MC_MCD_ERR_PROXY_PENDING. This handle is used to wait for a matching
@@ -1024,6 +1008,79 @@ fail1:
        return (rc);
 }
 
+       __checkReturn   efx_rc_t
+efx_mcdi_get_capabilities(
+       __in            efx_nic_t *enp,
+       __out_opt       uint32_t *flagsp,
+       __out_opt       uint16_t *rx_dpcpu_fw_idp,
+       __out_opt       uint16_t *tx_dpcpu_fw_idp,
+       __out_opt       uint32_t *flags2p,
+       __out_opt       uint32_t *tso2ncp)
+{
+       efx_mcdi_req_t req;
+       uint8_t payload[MAX(MC_CMD_GET_CAPABILITIES_IN_LEN,
+                           MC_CMD_GET_CAPABILITIES_V2_OUT_LEN)];
+       boolean_t v2_capable;
+       efx_rc_t rc;
+
+       (void) memset(payload, 0, sizeof (payload));
+       req.emr_cmd = MC_CMD_GET_CAPABILITIES;
+       req.emr_in_buf = payload;
+       req.emr_in_length = MC_CMD_GET_CAPABILITIES_IN_LEN;
+       req.emr_out_buf = payload;
+       req.emr_out_length = MC_CMD_GET_CAPABILITIES_V2_OUT_LEN;
+
+       efx_mcdi_execute_quiet(enp, &req);
+
+       if (req.emr_rc != 0) {
+               rc = req.emr_rc;
+               goto fail1;
+       }
+
+       if (req.emr_out_length_used < MC_CMD_GET_CAPABILITIES_OUT_LEN) {
+               rc = EMSGSIZE;
+               goto fail2;
+       }
+
+       if (flagsp != NULL)
+               *flagsp = MCDI_OUT_DWORD(req, GET_CAPABILITIES_OUT_FLAGS1);
+
+       if (rx_dpcpu_fw_idp != NULL)
+               *rx_dpcpu_fw_idp = MCDI_OUT_WORD(req,
+                                       GET_CAPABILITIES_OUT_RX_DPCPU_FW_ID);
+
+       if (tx_dpcpu_fw_idp != NULL)
+               *tx_dpcpu_fw_idp = MCDI_OUT_WORD(req,
+                                       GET_CAPABILITIES_OUT_TX_DPCPU_FW_ID);
+
+       if (req.emr_out_length_used < MC_CMD_GET_CAPABILITIES_V2_OUT_LEN)
+               v2_capable = B_FALSE;
+       else
+               v2_capable = B_TRUE;
+
+       if (flags2p != NULL) {
+               *flags2p = (v2_capable) ?
+                       MCDI_OUT_DWORD(req, GET_CAPABILITIES_V2_OUT_FLAGS2) :
+                       0;
+       }
+
+       if (tso2ncp != NULL) {
+               *tso2ncp = (v2_capable) ?
+                       MCDI_OUT_WORD(req,
+                               GET_CAPABILITIES_V2_OUT_TX_TSO_V2_N_CONTEXTS) :
+                       0;
+       }
+
+       return (0);
+
+fail2:
+       EFSYS_PROBE(fail2);
+fail1:
+       EFSYS_PROBE1(fail1, efx_rc_t, rc);
+
+       return (rc);
+}
+
 static __checkReturn   efx_rc_t
 efx_mcdi_do_reboot(
        __in            efx_nic_t *enp,
@@ -1375,6 +1432,10 @@ efx_mcdi_get_phy_cfg(
        efx_mcdi_req_t req;
        uint8_t payload[MAX(MC_CMD_GET_PHY_CFG_IN_LEN,
                            MC_CMD_GET_PHY_CFG_OUT_LEN)];
+#if EFSYS_OPT_NAMES
+       const char *namep;
+       size_t namelen;
+#endif
        efx_rc_t rc;
 
        (void) memset(payload, 0, sizeof (payload));
@@ -1398,10 +1459,12 @@ efx_mcdi_get_phy_cfg(
 
        encp->enc_phy_type = MCDI_OUT_DWORD(req, GET_PHY_CFG_OUT_TYPE);
 #if EFSYS_OPT_NAMES
-       (void) strncpy(encp->enc_phy_name,
-               MCDI_OUT2(req, char, GET_PHY_CFG_OUT_NAME),
-               MIN(sizeof (encp->enc_phy_name) - 1,
-                   MC_CMD_GET_PHY_CFG_OUT_NAME_LEN));
+       namep = MCDI_OUT2(req, char, GET_PHY_CFG_OUT_NAME);
+       namelen = MIN(sizeof (encp->enc_phy_name) - 1,
+                   strnlen(namep, MC_CMD_GET_PHY_CFG_OUT_NAME_LEN));
+       (void) memset(encp->enc_phy_name, 0,
+           sizeof (encp->enc_phy_name));
+       memcpy(encp->enc_phy_name, namep, namelen);
 #endif /* EFSYS_OPT_NAMES */
        (void) memset(encp->enc_phy_revision, 0,
            sizeof (encp->enc_phy_revision));
@@ -1409,6 +1472,11 @@ efx_mcdi_get_phy_cfg(
                MCDI_OUT2(req, char, GET_PHY_CFG_OUT_REVISION),
                MIN(sizeof (encp->enc_phy_revision) - 1,
                    MC_CMD_GET_PHY_CFG_OUT_REVISION_LEN));
+#if EFSYS_OPT_PHY_LED_CONTROL
+       encp->enc_led_mask = ((1 << EFX_PHY_LED_DEFAULT) |
+                           (1 << EFX_PHY_LED_OFF) |
+                           (1 << EFX_PHY_LED_ON));
+#endif /* EFSYS_OPT_PHY_LED_CONTROL */
 
        /* Get the media type of the fixed port, if recognised. */
        EFX_STATIC_ASSERT(MC_CMD_MEDIA_XAUI == EFX_PHY_MEDIA_XAUI);
@@ -1425,6 +1493,9 @@ efx_mcdi_get_phy_cfg(
 
        epp->ep_phy_cap_mask =
                MCDI_OUT_DWORD(req, GET_PHY_CFG_OUT_SUPPORTED_CAP);
+#if EFSYS_OPT_PHY_FLAGS
+       encp->enc_phy_flags_mask = MCDI_OUT_DWORD(req, GET_PHY_CFG_OUT_FLAGS);
+#endif /* EFSYS_OPT_PHY_FLAGS */
 
        encp->enc_port = (uint8_t)MCDI_OUT_DWORD(req, GET_PHY_CFG_OUT_PRT);
 
@@ -1432,6 +1503,24 @@ efx_mcdi_get_phy_cfg(
        encp->enc_mcdi_mdio_channel =
                (uint8_t)MCDI_OUT_DWORD(req, GET_PHY_CFG_OUT_CHANNEL);
 
+#if EFSYS_OPT_PHY_STATS
+       encp->enc_mcdi_phy_stat_mask =
+               MCDI_OUT_DWORD(req, GET_PHY_CFG_OUT_STATS_MASK);
+#endif /* EFSYS_OPT_PHY_STATS */
+
+#if EFSYS_OPT_BIST
+       encp->enc_bist_mask = 0;
+       if (MCDI_OUT_DWORD_FIELD(req, GET_PHY_CFG_OUT_FLAGS,
+           GET_PHY_CFG_OUT_BIST_CABLE_SHORT))
+               encp->enc_bist_mask |= (1 << EFX_BIST_TYPE_PHY_CABLE_SHORT);
+       if (MCDI_OUT_DWORD_FIELD(req, GET_PHY_CFG_OUT_FLAGS,
+           GET_PHY_CFG_OUT_BIST_CABLE_LONG))
+               encp->enc_bist_mask |= (1 << EFX_BIST_TYPE_PHY_CABLE_LONG);
+       if (MCDI_OUT_DWORD_FIELD(req, GET_PHY_CFG_OUT_FLAGS,
+           GET_PHY_CFG_OUT_BIST))
+               encp->enc_bist_mask |= (1 << EFX_BIST_TYPE_PHY_NORMAL);
+#endif  /* EFSYS_OPT_BIST */
+
        return (0);
 
 fail2:
@@ -1542,6 +1631,108 @@ fail1:
        return (rc);
 }
 
+#if EFSYS_OPT_BIST
+
+#if EFSYS_OPT_HUNTINGTON || EFSYS_OPT_MEDFORD || EFSYS_OPT_MEDFORD2
+/*
+ * Enter bist offline mode. This is a fw mode which puts the NIC into a state
+ * where memory BIST tests can be run and not much else can interfere or happen.
+ * A reboot is required to exit this mode.
+ */
+       __checkReturn           efx_rc_t
+efx_mcdi_bist_enable_offline(
+       __in                    efx_nic_t *enp)
+{
+       efx_mcdi_req_t req;
+       efx_rc_t rc;
+
+       EFX_STATIC_ASSERT(MC_CMD_ENABLE_OFFLINE_BIST_IN_LEN == 0);
+       EFX_STATIC_ASSERT(MC_CMD_ENABLE_OFFLINE_BIST_OUT_LEN == 0);
+
+       req.emr_cmd = MC_CMD_ENABLE_OFFLINE_BIST;
+       req.emr_in_buf = NULL;
+       req.emr_in_length = 0;
+       req.emr_out_buf = NULL;
+       req.emr_out_length = 0;
+
+       efx_mcdi_execute(enp, &req);
+
+       if (req.emr_rc != 0) {
+               rc = req.emr_rc;
+               goto fail1;
+       }
+
+       return (0);
+
+fail1:
+       EFSYS_PROBE1(fail1, efx_rc_t, rc);
+
+       return (rc);
+}
+#endif /* EFSYS_OPT_HUNTINGTON || EFSYS_OPT_MEDFORD || EFSYS_OPT_MEDFORD2 */
+
+       __checkReturn           efx_rc_t
+efx_mcdi_bist_start(
+       __in                    efx_nic_t *enp,
+       __in                    efx_bist_type_t type)
+{
+       efx_mcdi_req_t req;
+       uint8_t payload[MAX(MC_CMD_START_BIST_IN_LEN,
+                           MC_CMD_START_BIST_OUT_LEN)];
+       efx_rc_t rc;
+
+       (void) memset(payload, 0, sizeof (payload));
+       req.emr_cmd = MC_CMD_START_BIST;
+       req.emr_in_buf = payload;
+       req.emr_in_length = MC_CMD_START_BIST_IN_LEN;
+       req.emr_out_buf = payload;
+       req.emr_out_length = MC_CMD_START_BIST_OUT_LEN;
+
+       switch (type) {
+       case EFX_BIST_TYPE_PHY_NORMAL:
+               MCDI_IN_SET_DWORD(req, START_BIST_IN_TYPE, MC_CMD_PHY_BIST);
+               break;
+       case EFX_BIST_TYPE_PHY_CABLE_SHORT:
+               MCDI_IN_SET_DWORD(req, START_BIST_IN_TYPE,
+                   MC_CMD_PHY_BIST_CABLE_SHORT);
+               break;
+       case EFX_BIST_TYPE_PHY_CABLE_LONG:
+               MCDI_IN_SET_DWORD(req, START_BIST_IN_TYPE,
+                   MC_CMD_PHY_BIST_CABLE_LONG);
+               break;
+       case EFX_BIST_TYPE_MC_MEM:
+               MCDI_IN_SET_DWORD(req, START_BIST_IN_TYPE,
+                   MC_CMD_MC_MEM_BIST);
+               break;
+       case EFX_BIST_TYPE_SAT_MEM:
+               MCDI_IN_SET_DWORD(req, START_BIST_IN_TYPE,
+                   MC_CMD_PORT_MEM_BIST);
+               break;
+       case EFX_BIST_TYPE_REG:
+               MCDI_IN_SET_DWORD(req, START_BIST_IN_TYPE,
+                   MC_CMD_REG_BIST);
+               break;
+       default:
+               EFSYS_ASSERT(0);
+       }
+
+       efx_mcdi_execute(enp, &req);
+
+       if (req.emr_rc != 0) {
+               rc = req.emr_rc;
+               goto fail1;
+       }
+
+       return (0);
+
+fail1:
+       EFSYS_PROBE1(fail1, efx_rc_t, rc);
+
+       return (rc);
+}
+
+#endif /* EFSYS_OPT_BIST */
+
 
 /* Enable logging of some events (e.g. link state changes) */
        __checkReturn   efx_rc_t
@@ -1580,7 +1771,169 @@ fail1:
 }
 
 
-#if EFSYS_OPT_HUNTINGTON || EFSYS_OPT_MEDFORD
+#if EFSYS_OPT_MAC_STATS
+
+typedef enum efx_stats_action_e {
+       EFX_STATS_CLEAR,
+       EFX_STATS_UPLOAD,
+       EFX_STATS_ENABLE_NOEVENTS,
+       EFX_STATS_ENABLE_EVENTS,
+       EFX_STATS_DISABLE,
+} efx_stats_action_t;
+
+static __checkReturn   efx_rc_t
+efx_mcdi_mac_stats(
+       __in            efx_nic_t *enp,
+       __in_opt        efsys_mem_t *esmp,
+       __in            efx_stats_action_t action,
+       __in            uint16_t period_ms)
+{
+       efx_mcdi_req_t req;
+       uint8_t payload[MAX(MC_CMD_MAC_STATS_IN_LEN,
+                           MC_CMD_MAC_STATS_OUT_DMA_LEN)];
+       int clear = (action == EFX_STATS_CLEAR);
+       int upload = (action == EFX_STATS_UPLOAD);
+       int enable = (action == EFX_STATS_ENABLE_NOEVENTS);
+       int events = (action == EFX_STATS_ENABLE_EVENTS);
+       int disable = (action == EFX_STATS_DISABLE);
+       efx_rc_t rc;
+
+       (void) memset(payload, 0, sizeof (payload));
+       req.emr_cmd = MC_CMD_MAC_STATS;
+       req.emr_in_buf = payload;
+       req.emr_in_length = MC_CMD_MAC_STATS_IN_LEN;
+       req.emr_out_buf = payload;
+       req.emr_out_length = MC_CMD_MAC_STATS_OUT_DMA_LEN;
+
+       MCDI_IN_POPULATE_DWORD_6(req, MAC_STATS_IN_CMD,
+           MAC_STATS_IN_DMA, upload,
+           MAC_STATS_IN_CLEAR, clear,
+           MAC_STATS_IN_PERIODIC_CHANGE, enable | events | disable,
+           MAC_STATS_IN_PERIODIC_ENABLE, enable | events,
+           MAC_STATS_IN_PERIODIC_NOEVENT, !events,
+           MAC_STATS_IN_PERIOD_MS, (enable | events) ? period_ms : 0);
+
+       if (esmp != NULL) {
+               int bytes = MC_CMD_MAC_NSTATS * sizeof (uint64_t);
+
+               EFX_STATIC_ASSERT(MC_CMD_MAC_NSTATS * sizeof (uint64_t) <=
+                   EFX_MAC_STATS_SIZE);
+
+               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,
+                           EFSYS_MEM_ADDR(esmp) >> 32);
+               MCDI_IN_SET_DWORD(req, MAC_STATS_IN_DMA_LEN, bytes);
+       } else {
+               EFSYS_ASSERT(!upload && !enable && !events);
+       }
+
+       /*
+        * NOTE: Do not use EVB_PORT_ID_ASSIGNED when disabling periodic stats,
+        *       as this may fail (and leave periodic DMA enabled) if the
+        *       vadapter has already been deleted.
+        */
+       MCDI_IN_SET_DWORD(req, MAC_STATS_IN_PORT_ID,
+           (disable ? EVB_PORT_ID_NULL : enp->en_vport_id));
+
+       efx_mcdi_execute(enp, &req);
+
+       if (req.emr_rc != 0) {
+               /* EF10: Expect ENOENT if no DMA queues are initialised */
+               if ((req.emr_rc != ENOENT) ||
+                   (enp->en_rx_qcount + enp->en_tx_qcount != 0)) {
+                       rc = req.emr_rc;
+                       goto fail1;
+               }
+       }
+
+       return (0);
+
+fail1:
+       EFSYS_PROBE1(fail1, efx_rc_t, rc);
+
+       return (rc);
+}
+
+       __checkReturn   efx_rc_t
+efx_mcdi_mac_stats_clear(
+       __in            efx_nic_t *enp)
+{
+       efx_rc_t rc;
+
+       if ((rc = efx_mcdi_mac_stats(enp, NULL, EFX_STATS_CLEAR, 0)) != 0)
+               goto fail1;
+
+       return (0);
+
+fail1:
+       EFSYS_PROBE1(fail1, efx_rc_t, rc);
+
+       return (rc);
+}
+
+       __checkReturn   efx_rc_t
+efx_mcdi_mac_stats_upload(
+       __in            efx_nic_t *enp,
+       __in            efsys_mem_t *esmp)
+{
+       efx_rc_t rc;
+
+       /*
+        * The MC DMAs aggregate statistics for our convenience, so we can
+        * avoid having to pull the statistics buffer into the cache to
+        * maintain cumulative statistics.
+        */
+       if ((rc = efx_mcdi_mac_stats(enp, esmp, EFX_STATS_UPLOAD, 0)) != 0)
+               goto fail1;
+
+       return (0);
+
+fail1:
+       EFSYS_PROBE1(fail1, efx_rc_t, rc);
+
+       return (rc);
+}
+
+       __checkReturn   efx_rc_t
+efx_mcdi_mac_stats_periodic(
+       __in            efx_nic_t *enp,
+       __in            efsys_mem_t *esmp,
+       __in            uint16_t period_ms,
+       __in            boolean_t events)
+{
+       efx_rc_t rc;
+
+       /*
+        * The MC DMAs aggregate statistics for our convenience, so we can
+        * avoid having to pull the statistics buffer into the cache to
+        * maintain cumulative statistics.
+        * Huntington uses a fixed 1sec period.
+        * Medford uses a fixed 1sec period before v6.2.1.1033 firmware.
+        */
+       if (period_ms == 0)
+               rc = efx_mcdi_mac_stats(enp, NULL, EFX_STATS_DISABLE, 0);
+       else if (events)
+               rc = efx_mcdi_mac_stats(enp, esmp, EFX_STATS_ENABLE_EVENTS,
+                   period_ms);
+       else
+               rc = efx_mcdi_mac_stats(enp, esmp, EFX_STATS_ENABLE_NOEVENTS,
+                   period_ms);
+
+       if (rc != 0)
+               goto fail1;
+
+       return (0);
+
+fail1:
+       EFSYS_PROBE1(fail1, efx_rc_t, rc);
+
+       return (rc);
+}
+
+#endif /* EFSYS_OPT_MAC_STATS */
+
+#if EFSYS_OPT_HUNTINGTON || EFSYS_OPT_MEDFORD || EFSYS_OPT_MEDFORD2
 
 /*
  * This function returns the pf and vf number of a function.  If it is a pf the
@@ -1679,7 +2032,7 @@ fail1:
        return (rc);
 }
 
-#endif /* EFSYS_OPT_HUNTINGTON || EFSYS_OPT_MEDFORD */
+#endif /* EFSYS_OPT_HUNTINGTON || EFSYS_OPT_MEDFORD || EFSYS_OPT_MEDFORD2 */
 
        __checkReturn           efx_rc_t
 efx_mcdi_set_workaround(