Implement Total Port Shutdown (TPS) for E610. When TPS is provisioned
in the NVM (by the platform firmware), the driver disables the PHY link
when the interface is brought down and at the end of probe if the
interface is not running.

When the PORT_DIS bit is set in the Link Default Override TLV in
the NVM PFA area, enable Total Port Shutdown (TPS). This forces the
link-down-on-close private flag and prevents the user from changing
it via ethtool, ensuring the PHY is powered down when the interface
is brought down as provisioned in the NVM.

The Link Default Override TLV (0x134) is read from the NVM PFA during
driver init. Each port has its own 10-word configuration block within
the TLV. If the PORT_DIS bit is set in the port's link_options field,
the driver sets both TOTAL_PORT_SHUTDOWN_ENA and LINK_DOWN_ON_CLOSE
flags.

TPS takes precedence over Wake-on-LAN and manageability: the link is
disabled on close even if WoL is enabled, matching the existing
link-down-on-close behaviour and ice.

Changing link settings or EEE via ethtool while the interface is down
may still re-enable the link until the next close, as before this patch.

Signed-off-by: Tomasz Lichwala <[email protected]>
Reviewed-by: Przemek Kitszel <[email protected]>
---
v2:
- index the Link Default Override TLV by hw->bus.func instead of
  PCI_FUNC() and call set_lan_id() before the lookup in ixgbe_sw_init()
- read only the link options word and drop struct
  ixgbe_link_default_override_tlv, keeping the 10-word per-port stride
- return -ERANGE when the per-port block does not fit in the 16-bit NVM
  word offset
- harden ixgbe_get_pfa_module_tlv() against u16 overflow, as ice does
- disable the PHY link at the end of probe on TPS ports that are not
  running; setup_eee() enables the link unconditionally, so a
  never-opened port would otherwise keep its link up

v1: 
https://patchwork.ozlabs.org/project/intel-wired-lan/patch/[email protected]/

 drivers/net/ethernet/intel/ixgbe/ixgbe.h      |  1 +
 drivers/net/ethernet/intel/ixgbe/ixgbe_e610.c | 52 +++++++++++++++++--
 drivers/net/ethernet/intel/ixgbe/ixgbe_e610.h |  1 +
 .../net/ethernet/intel/ixgbe/ixgbe_ethtool.c  | 13 +++++
 drivers/net/ethernet/intel/ixgbe/ixgbe_main.c | 24 ++++++++-
 .../ethernet/intel/ixgbe/ixgbe_type_e610.h    |  7 +++
 6 files changed, 93 insertions(+), 5 deletions(-)

diff --git a/drivers/net/ethernet/intel/ixgbe/ixgbe.h 
b/drivers/net/ethernet/intel/ixgbe/ixgbe.h
index 344c7de4187b..bac3b85a1d63 100644
--- a/drivers/net/ethernet/intel/ixgbe/ixgbe.h
+++ b/drivers/net/ethernet/intel/ixgbe/ixgbe.h
@@ -683,6 +683,7 @@ struct ixgbe_adapter {
 #define IXGBE_FLAG2_API_MISMATCH               BIT(23)
 #define IXGBE_FLAG2_FW_ROLLBACK                        BIT(24)
 #define IXGBE_FLAG2_LINK_DOWN_ON_CLOSE         BIT(25)
+#define IXGBE_FLAG2_TOTAL_PORT_SHUTDOWN_ENA    BIT(26)
 
        /* Tx fast path data */
        int num_tx_queues;
diff --git a/drivers/net/ethernet/intel/ixgbe/ixgbe_e610.c 
b/drivers/net/ethernet/intel/ixgbe/ixgbe_e610.c
index b4a3f255ec8f..d649b523f71b 100644
--- a/drivers/net/ethernet/intel/ixgbe/ixgbe_e610.c
+++ b/drivers/net/ethernet/intel/ixgbe/ixgbe_e610.c
@@ -3903,7 +3903,8 @@ int ixgbe_reset_hw_e610(struct ixgbe_hw *hw)
 static int ixgbe_get_pfa_module_tlv(struct ixgbe_hw *hw, u16 *module_tlv,
                                    u16 *module_tlv_len, u16 module_type)
 {
-       u16 pfa_len, pfa_ptr, pfa_end_ptr;
+       struct ixgbe_adapter *adapter = hw->back;
+       u16 pfa_len, pfa_ptr, max_tlv;
        u16 next_tlv;
        int err;
 
@@ -3915,12 +3916,18 @@ static int ixgbe_get_pfa_module_tlv(struct ixgbe_hw 
*hw, u16 *module_tlv,
        if (err)
                return err;
 
+       /* PFA length covers the length word, all TLVs and one trailing word */
+       if (check_add_overflow(pfa_ptr, pfa_len - 1, &max_tlv)) {
+               e_dev_warn("PFA at 0x%04x with length 0x%04x overflows 16-bit 
word space\n",
+                          pfa_ptr, pfa_len);
+               return -EINVAL;
+       }
+
        /* Starting with first TLV after PFA length, iterate through the list
         * of TLVs to find the requested one.
         */
        next_tlv = pfa_ptr + 1;
-       pfa_end_ptr = pfa_ptr + pfa_len;
-       while (next_tlv < pfa_end_ptr) {
+       while (next_tlv < max_tlv) {
                u16 tlv_sub_module_type, tlv_len;
 
                /* Read TLV type */
@@ -3945,12 +3952,49 @@ static int ixgbe_get_pfa_module_tlv(struct ixgbe_hw 
*hw, u16 *module_tlv,
                /* Check next TLV, i.e. current TLV pointer + length + 2 words
                 * (for current TLV's type and length).
                 */
-               next_tlv = next_tlv + tlv_len + 2;
+               if (check_add_overflow(next_tlv, 2, &next_tlv) ||
+                   check_add_overflow(next_tlv, tlv_len, &next_tlv)) {
+                       e_dev_warn("TLV type 0x%04x length 0x%04x overflows 
16-bit word space\n",
+                                  tlv_sub_module_type, tlv_len);
+                       return -EINVAL;
+               }
        }
        /* Module does not exist */
        return -ENODATA;
 }
 
+/**
+ * ixgbe_get_link_default_override - Get link default override from NVM
+ * @hw: pointer to hardware structure
+ * @link_options: pointer to the port's link options word to populate
+ *
+ * Read the link options word of this port's block in the Link Default
+ * Override TLV from the NVM PFA.
+ *
+ * Return: 0 on success, negative error code on failure.
+ */
+int ixgbe_get_link_default_override(struct ixgbe_hw *hw, u16 *link_options)
+{
+       u32 blk_start, blk_end;
+       u16 tlv, tlv_len;
+       int err;
+
+       err = ixgbe_get_pfa_module_tlv(hw, &tlv, &tlv_len,
+                                      IXGBE_E610_SR_LINK_DEFAULT_OVERRIDE_PTR);
+       if (err)
+               return err;
+
+       blk_start = tlv + IXGBE_E610_SR_PFA_TLV_HDR_SIZE +
+                   hw->bus.func * IXGBE_E610_SR_PFA_LINK_OVERRIDE_WORDS;
+       blk_end = blk_start + IXGBE_E610_SR_PFA_LINK_OVERRIDE_WORDS;
+
+       if (blk_end > tlv + IXGBE_E610_SR_PFA_TLV_HDR_SIZE + tlv_len ||
+           blk_end - 1 > U16_MAX)
+               return -ERANGE;
+
+       return ixgbe_read_ee_aci_e610(hw, blk_start, link_options);
+}
+
 /**
  * ixgbe_read_pba_string_e610 - Read PBA string from NVM
  * @hw: pointer to hardware structure
diff --git a/drivers/net/ethernet/intel/ixgbe/ixgbe_e610.h 
b/drivers/net/ethernet/intel/ixgbe/ixgbe_e610.h
index 1f4c8b022698..009b34a7434b 100644
--- a/drivers/net/ethernet/intel/ixgbe/ixgbe_e610.h
+++ b/drivers/net/ethernet/intel/ixgbe/ixgbe_e610.h
@@ -82,6 +82,7 @@ int ixgbe_read_ee_aci_e610(struct ixgbe_hw *hw, u16 offset, 
u16 *data);
 int ixgbe_read_ee_aci_buffer_e610(struct ixgbe_hw *hw, u16 offset,
                                  u16 words, u16 *data);
 int ixgbe_validate_eeprom_checksum_e610(struct ixgbe_hw *hw, u16 
*checksum_val);
+int ixgbe_get_link_default_override(struct ixgbe_hw *hw, u16 *link_options);
 int ixgbe_reset_hw_e610(struct ixgbe_hw *hw);
 int ixgbe_get_flash_data(struct ixgbe_hw *hw);
 int ixgbe_aci_nvm_update_empr(struct ixgbe_hw *hw);
diff --git a/drivers/net/ethernet/intel/ixgbe/ixgbe_ethtool.c 
b/drivers/net/ethernet/intel/ixgbe/ixgbe_ethtool.c
index dfe12f1ce77b..27c3e71842da 100644
--- a/drivers/net/ethernet/intel/ixgbe/ixgbe_ethtool.c
+++ b/drivers/net/ethernet/intel/ixgbe/ixgbe_ethtool.c
@@ -3860,6 +3860,19 @@ static int ixgbe_set_priv_flags(struct net_device 
*netdev, u32 priv_flags)
                }
        }
 
+       /* Do not allow any change to link-down-on-close when Total Port
+        * Shutdown is enabled - the flag is managed by TPS exclusively.
+        */
+       if (adapter->flags2 & IXGBE_FLAG2_TOTAL_PORT_SHUTDOWN_ENA) {
+               bool cur = !!(adapter->flags2 & IXGBE_FLAG2_LINK_DOWN_ON_CLOSE);
+               bool req = !!(priv_flags & IXGBE_PRIV_LINK_DOWN_ON_CLOSE);
+
+               if (req != cur) {
+                       e_warn(probe, "Setting link-down-on-close not supported 
on this port\n");
+                       return -EOPNOTSUPP;
+               }
+       }
+
        flags2 &= ~IXGBE_FLAG2_LINK_DOWN_ON_CLOSE;
        if (priv_flags & IXGBE_PRIV_LINK_DOWN_ON_CLOSE) {
                if (adapter->hw.mac.type == ixgbe_mac_e610) {
diff --git a/drivers/net/ethernet/intel/ixgbe/ixgbe_main.c 
b/drivers/net/ethernet/intel/ixgbe/ixgbe_main.c
index a6a6883a8021..ac08d6bd7e97 100644
--- a/drivers/net/ethernet/intel/ixgbe/ixgbe_main.c
+++ b/drivers/net/ethernet/intel/ixgbe/ixgbe_main.c
@@ -6989,9 +6989,23 @@ static int ixgbe_sw_init(struct ixgbe_adapter *adapter,
        if (hw->mac.ops.init_swfw_sync)
                hw->mac.ops.init_swfw_sync(hw);
 
-       if (hw->mac.type == ixgbe_mac_e610)
+       if (hw->mac.type == ixgbe_mac_e610) {
+               u16 link_options;
+               int err;
+
                mutex_init(&hw->aci.lock);
 
+               /* bus.func selects the TLV block; it is set only later */
+               hw->mac.ops.set_lan_id(hw);
+               err = ixgbe_get_link_default_override(hw, &link_options);
+               if (!err && (link_options & IXGBE_LINK_OVERRIDE_PORT_DIS)) {
+                       adapter->flags2 |=
+                               IXGBE_FLAG2_TOTAL_PORT_SHUTDOWN_ENA |
+                               IXGBE_FLAG2_LINK_DOWN_ON_CLOSE;
+                       e_dev_info("Total Port Shutdown enabled\n");
+               }
+       }
+
 #ifdef IXGBE_FCOE
        /* FCoE support exists, always init the FCoE lock */
        spin_lock_init(&adapter->fcoe.lock);
@@ -12070,6 +12084,14 @@ static int ixgbe_probe(struct pci_dev *pdev, const 
struct pci_device_id *ent)
                hw->mac.ops.setup_eee(hw, eee_enable);
        }
 
+       if (adapter->flags2 & IXGBE_FLAG2_TOTAL_PORT_SHUTDOWN_ENA) {
+               /* netdev is already registered, so ndo_open may have run */
+               rtnl_lock();
+               if (!netif_running(netdev) && ixgbe_disable_phy_link(hw))
+                       e_dev_err("Cannot set PHY link down\n");
+               rtnl_unlock();
+       }
+
        ixgbe_devlink_init_regions(adapter);
        devl_register(adapter->devlink);
        devl_unlock(adapter->devlink);
diff --git a/drivers/net/ethernet/intel/ixgbe/ixgbe_type_e610.h 
b/drivers/net/ethernet/intel/ixgbe/ixgbe_type_e610.h
index 959cacecae49..7e36fd3647aa 100644
--- a/drivers/net/ethernet/intel/ixgbe/ixgbe_type_e610.h
+++ b/drivers/net/ethernet/intel/ixgbe/ixgbe_type_e610.h
@@ -29,6 +29,13 @@
 #define IXGBE_E610_SR_NETLIST_BANK_PTR         0x46
 #define IXGBE_E610_SR_NETLIST_BANK_SIZE                0x47
 
+/* Link Default Override TLV in PFA */
+#define IXGBE_E610_SR_LINK_DEFAULT_OVERRIDE_PTR        0x134
+#define IXGBE_E610_SR_PFA_LINK_OVERRIDE_WORDS  10
+#define IXGBE_E610_SR_PFA_TLV_HDR_SIZE         2
+
+#define IXGBE_LINK_OVERRIDE_PORT_DIS           BIT(2)
+
 /* The OROM version topology */
 #define IXGBE_OROM_VER_PATCH_MASK              GENMASK_ULL(7, 0)
 #define IXGBE_OROM_VER_BUILD_MASK              GENMASK_ULL(23, 8)
-- 
2.54.0

Reply via email to