1 // SPDX-License-Identifier: GPL-2.0+ 2 // Copyright (c) 2024 Hisilicon Limited. 3 4 #include <linux/etherdevice.h> 5 #include <linux/netdevice.h> 6 #include <linux/phy.h> 7 #include <linux/rtnetlink.h> 8 #include "hbg_common.h" 9 #include "hbg_err.h" 10 #include "hbg_hw.h" 11 12 static void hbg_restore_mac_table(struct hbg_priv *priv) 13 { 14 struct hbg_mac_filter *filter = &priv->filter; 15 u64 addr; 16 u32 i; 17 18 for (i = 0; i < filter->table_max_len; i++) 19 if (!is_zero_ether_addr(filter->mac_table[i].addr)) { 20 addr = ether_addr_to_u64(filter->mac_table[i].addr); 21 hbg_hw_set_uc_addr(priv, addr, i); 22 } 23 24 hbg_hw_set_mac_filter_enable(priv, priv->filter.enabled); 25 } 26 27 static void hbg_restore_user_def_settings(struct hbg_priv *priv) 28 { 29 /* The index of host mac is always 0. */ 30 u64 rx_pause_addr = ether_addr_to_u64(priv->filter.mac_table[0].addr); 31 struct ethtool_pauseparam *pause_param = &priv->user_def.pause_param; 32 33 hbg_restore_mac_table(priv); 34 hbg_hw_set_mtu(priv, priv->netdev->mtu); 35 hbg_hw_set_pause_enable(priv, pause_param->tx_pause, 36 pause_param->rx_pause); 37 hbg_hw_set_rx_pause_mac_addr(priv, rx_pause_addr); 38 } 39 40 int hbg_rebuild(struct hbg_priv *priv) 41 { 42 int ret; 43 44 ret = hbg_hw_init(priv); 45 if (ret) 46 return ret; 47 48 hbg_restore_user_def_settings(priv); 49 return 0; 50 } 51 52 static int hbg_reset_prepare(struct hbg_priv *priv, enum hbg_reset_type type) 53 { 54 int ret; 55 56 ASSERT_RTNL(); 57 58 if (netif_running(priv->netdev)) { 59 dev_warn(&priv->pdev->dev, 60 "failed to reset because port is up\n"); 61 return -EBUSY; 62 } 63 64 priv->reset_type = type; 65 set_bit(HBG_NIC_STATE_RESETTING, &priv->state); 66 clear_bit(HBG_NIC_STATE_RESET_FAIL, &priv->state); 67 ret = hbg_hw_event_notify(priv, HBG_HW_EVENT_RESET); 68 if (ret) { 69 set_bit(HBG_NIC_STATE_RESET_FAIL, &priv->state); 70 clear_bit(HBG_NIC_STATE_RESETTING, &priv->state); 71 } 72 73 return ret; 74 } 75 76 static int hbg_reset_done(struct hbg_priv *priv, enum hbg_reset_type type) 77 { 78 int ret; 79 80 if (!test_bit(HBG_NIC_STATE_RESETTING, &priv->state) || 81 type != priv->reset_type) 82 return 0; 83 84 ASSERT_RTNL(); 85 86 clear_bit(HBG_NIC_STATE_RESETTING, &priv->state); 87 ret = hbg_rebuild(priv); 88 if (ret) { 89 set_bit(HBG_NIC_STATE_RESET_FAIL, &priv->state); 90 dev_err(&priv->pdev->dev, "failed to rebuild after reset\n"); 91 return ret; 92 } 93 94 dev_info(&priv->pdev->dev, "reset done\n"); 95 return ret; 96 } 97 98 /* must be protected by rtnl lock */ 99 int hbg_reset(struct hbg_priv *priv) 100 { 101 int ret; 102 103 ASSERT_RTNL(); 104 ret = hbg_reset_prepare(priv, HBG_RESET_TYPE_FUNCTION); 105 if (ret) 106 return ret; 107 108 return hbg_reset_done(priv, HBG_RESET_TYPE_FUNCTION); 109 } 110 111 void hbg_err_reset(struct hbg_priv *priv) 112 { 113 bool running; 114 115 rtnl_lock(); 116 running = netif_running(priv->netdev); 117 if (running) 118 dev_close(priv->netdev); 119 120 hbg_reset(priv); 121 122 /* in hbg_pci_err_detected(), we will detach first, 123 * so we need to attach before open 124 */ 125 if (!netif_device_present(priv->netdev)) 126 netif_device_attach(priv->netdev); 127 128 if (running) 129 dev_open(priv->netdev, NULL); 130 rtnl_unlock(); 131 } 132 133 static pci_ers_result_t hbg_pci_err_detected(struct pci_dev *pdev, 134 pci_channel_state_t state) 135 { 136 struct net_device *netdev = pci_get_drvdata(pdev); 137 138 netif_device_detach(netdev); 139 140 if (state == pci_channel_io_perm_failure) 141 return PCI_ERS_RESULT_DISCONNECT; 142 143 pci_disable_device(pdev); 144 return PCI_ERS_RESULT_NEED_RESET; 145 } 146 147 static pci_ers_result_t hbg_pci_err_slot_reset(struct pci_dev *pdev) 148 { 149 struct net_device *netdev = pci_get_drvdata(pdev); 150 struct hbg_priv *priv = netdev_priv(netdev); 151 152 if (pci_enable_device(pdev)) { 153 dev_err(&pdev->dev, 154 "failed to re-enable PCI device after reset\n"); 155 return PCI_ERS_RESULT_DISCONNECT; 156 } 157 158 pci_set_master(pdev); 159 pci_restore_state(pdev); 160 pci_save_state(pdev); 161 162 hbg_err_reset(priv); 163 netif_device_attach(netdev); 164 return PCI_ERS_RESULT_RECOVERED; 165 } 166 167 static void hbg_pci_err_reset_prepare(struct pci_dev *pdev) 168 { 169 struct net_device *netdev = pci_get_drvdata(pdev); 170 struct hbg_priv *priv = netdev_priv(netdev); 171 172 rtnl_lock(); 173 hbg_reset_prepare(priv, HBG_RESET_TYPE_FLR); 174 } 175 176 static void hbg_pci_err_reset_done(struct pci_dev *pdev) 177 { 178 struct net_device *netdev = pci_get_drvdata(pdev); 179 struct hbg_priv *priv = netdev_priv(netdev); 180 181 hbg_reset_done(priv, HBG_RESET_TYPE_FLR); 182 rtnl_unlock(); 183 } 184 185 static const struct pci_error_handlers hbg_pci_err_handler = { 186 .error_detected = hbg_pci_err_detected, 187 .slot_reset = hbg_pci_err_slot_reset, 188 .reset_prepare = hbg_pci_err_reset_prepare, 189 .reset_done = hbg_pci_err_reset_done, 190 }; 191 192 void hbg_set_pci_err_handler(struct pci_driver *pdrv) 193 { 194 pdrv->err_handler = &hbg_pci_err_handler; 195 } 196