xref: /linux/drivers/net/ethernet/hisilicon/hibmcge/hbg_err.c (revision 746680ec6696585e30db3e18c93a63df9cbec39c)
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 	netif_device_detach(priv->netdev);
65 
66 	priv->reset_type = type;
67 	set_bit(HBG_NIC_STATE_RESETTING, &priv->state);
68 	clear_bit(HBG_NIC_STATE_RESET_FAIL, &priv->state);
69 	ret = hbg_hw_event_notify(priv, HBG_HW_EVENT_RESET);
70 	if (ret) {
71 		priv->stats.reset_fail_cnt++;
72 		set_bit(HBG_NIC_STATE_RESET_FAIL, &priv->state);
73 		clear_bit(HBG_NIC_STATE_RESETTING, &priv->state);
74 	}
75 
76 	return ret;
77 }
78 
79 static int hbg_reset_done(struct hbg_priv *priv, enum hbg_reset_type type)
80 {
81 	int ret;
82 
83 	if (!test_bit(HBG_NIC_STATE_RESETTING, &priv->state) ||
84 	    type != priv->reset_type)
85 		return 0;
86 
87 	ASSERT_RTNL();
88 
89 	clear_bit(HBG_NIC_STATE_RESETTING, &priv->state);
90 	ret = hbg_rebuild(priv);
91 	if (ret) {
92 		priv->stats.reset_fail_cnt++;
93 		set_bit(HBG_NIC_STATE_RESET_FAIL, &priv->state);
94 		dev_err(&priv->pdev->dev, "failed to rebuild after reset\n");
95 		return ret;
96 	}
97 
98 	netif_device_attach(priv->netdev);
99 
100 	dev_info(&priv->pdev->dev, "reset done\n");
101 	return ret;
102 }
103 
104 /* must be protected by rtnl lock */
105 int hbg_reset(struct hbg_priv *priv)
106 {
107 	int ret;
108 
109 	ASSERT_RTNL();
110 	ret = hbg_reset_prepare(priv, HBG_RESET_TYPE_FUNCTION);
111 	if (ret)
112 		return ret;
113 
114 	return hbg_reset_done(priv, HBG_RESET_TYPE_FUNCTION);
115 }
116 
117 void hbg_err_reset(struct hbg_priv *priv)
118 {
119 	bool running;
120 
121 	rtnl_lock();
122 	running = netif_running(priv->netdev);
123 	if (running)
124 		dev_close(priv->netdev);
125 
126 	if (hbg_reset(priv))
127 		goto err_unlock;
128 
129 	if (running)
130 		dev_open(priv->netdev, NULL);
131 
132 err_unlock:
133 	rtnl_unlock();
134 }
135 
136 static pci_ers_result_t hbg_pci_err_detected(struct pci_dev *pdev,
137 					     pci_channel_state_t state)
138 {
139 	struct net_device *netdev = pci_get_drvdata(pdev);
140 
141 	netif_device_detach(netdev);
142 
143 	if (state == pci_channel_io_perm_failure)
144 		return PCI_ERS_RESULT_DISCONNECT;
145 
146 	pci_disable_device(pdev);
147 	return PCI_ERS_RESULT_NEED_RESET;
148 }
149 
150 static pci_ers_result_t hbg_pci_err_slot_reset(struct pci_dev *pdev)
151 {
152 	struct net_device *netdev = pci_get_drvdata(pdev);
153 	struct hbg_priv *priv = netdev_priv(netdev);
154 
155 	if (pci_enable_device(pdev)) {
156 		dev_err(&pdev->dev,
157 			"failed to re-enable PCI device after reset\n");
158 		return PCI_ERS_RESULT_DISCONNECT;
159 	}
160 
161 	pci_set_master(pdev);
162 	pci_restore_state(pdev);
163 	pci_save_state(pdev);
164 
165 	hbg_err_reset(priv);
166 	return PCI_ERS_RESULT_RECOVERED;
167 }
168 
169 static void hbg_pci_err_reset_prepare(struct pci_dev *pdev)
170 {
171 	struct net_device *netdev = pci_get_drvdata(pdev);
172 	struct hbg_priv *priv = netdev_priv(netdev);
173 
174 	rtnl_lock();
175 	hbg_reset_prepare(priv, HBG_RESET_TYPE_FLR);
176 }
177 
178 static void hbg_pci_err_reset_done(struct pci_dev *pdev)
179 {
180 	struct net_device *netdev = pci_get_drvdata(pdev);
181 	struct hbg_priv *priv = netdev_priv(netdev);
182 
183 	hbg_reset_done(priv, HBG_RESET_TYPE_FLR);
184 	rtnl_unlock();
185 }
186 
187 static const struct pci_error_handlers hbg_pci_err_handler = {
188 	.error_detected = hbg_pci_err_detected,
189 	.slot_reset = hbg_pci_err_slot_reset,
190 	.reset_prepare = hbg_pci_err_reset_prepare,
191 	.reset_done = hbg_pci_err_reset_done,
192 };
193 
194 void hbg_set_pci_err_handler(struct pci_driver *pdrv)
195 {
196 	pdrv->err_handler = &hbg_pci_err_handler;
197 }
198