xref: /linux/drivers/net/ethernet/hisilicon/hibmcge/hbg_err.c (revision 0ad9617c78acbc71373fb341a6f75d4012b01d69)
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 	struct ethtool_pauseparam *pause_param = &priv->user_def.pause_param;
30 
31 	hbg_restore_mac_table(priv);
32 	hbg_hw_set_mtu(priv, priv->netdev->mtu);
33 	hbg_hw_set_pause_enable(priv, pause_param->tx_pause,
34 				pause_param->rx_pause);
35 }
36 
37 int hbg_rebuild(struct hbg_priv *priv)
38 {
39 	int ret;
40 
41 	ret = hbg_hw_init(priv);
42 	if (ret)
43 		return ret;
44 
45 	hbg_restore_user_def_settings(priv);
46 	return 0;
47 }
48 
49 static int hbg_reset_prepare(struct hbg_priv *priv, enum hbg_reset_type type)
50 {
51 	int ret;
52 
53 	ASSERT_RTNL();
54 
55 	if (netif_running(priv->netdev)) {
56 		dev_warn(&priv->pdev->dev,
57 			 "failed to reset because port is up\n");
58 		return -EBUSY;
59 	}
60 
61 	priv->reset_type = type;
62 	set_bit(HBG_NIC_STATE_RESETTING, &priv->state);
63 	clear_bit(HBG_NIC_STATE_RESET_FAIL, &priv->state);
64 	ret = hbg_hw_event_notify(priv, HBG_HW_EVENT_RESET);
65 	if (ret) {
66 		set_bit(HBG_NIC_STATE_RESET_FAIL, &priv->state);
67 		clear_bit(HBG_NIC_STATE_RESETTING, &priv->state);
68 	}
69 
70 	return ret;
71 }
72 
73 static int hbg_reset_done(struct hbg_priv *priv, enum hbg_reset_type type)
74 {
75 	int ret;
76 
77 	if (!test_bit(HBG_NIC_STATE_RESETTING, &priv->state) ||
78 	    type != priv->reset_type)
79 		return 0;
80 
81 	ASSERT_RTNL();
82 
83 	clear_bit(HBG_NIC_STATE_RESETTING, &priv->state);
84 	ret = hbg_rebuild(priv);
85 	if (ret) {
86 		set_bit(HBG_NIC_STATE_RESET_FAIL, &priv->state);
87 		dev_err(&priv->pdev->dev, "failed to rebuild after reset\n");
88 		return ret;
89 	}
90 
91 	dev_info(&priv->pdev->dev, "reset done\n");
92 	return ret;
93 }
94 
95 /* must be protected by rtnl lock */
96 int hbg_reset(struct hbg_priv *priv)
97 {
98 	int ret;
99 
100 	ASSERT_RTNL();
101 	ret = hbg_reset_prepare(priv, HBG_RESET_TYPE_FUNCTION);
102 	if (ret)
103 		return ret;
104 
105 	return hbg_reset_done(priv, HBG_RESET_TYPE_FUNCTION);
106 }
107 
108 static void hbg_pci_err_reset_prepare(struct pci_dev *pdev)
109 {
110 	struct net_device *netdev = pci_get_drvdata(pdev);
111 	struct hbg_priv *priv = netdev_priv(netdev);
112 
113 	rtnl_lock();
114 	hbg_reset_prepare(priv, HBG_RESET_TYPE_FLR);
115 }
116 
117 static void hbg_pci_err_reset_done(struct pci_dev *pdev)
118 {
119 	struct net_device *netdev = pci_get_drvdata(pdev);
120 	struct hbg_priv *priv = netdev_priv(netdev);
121 
122 	hbg_reset_done(priv, HBG_RESET_TYPE_FLR);
123 	rtnl_unlock();
124 }
125 
126 static const struct pci_error_handlers hbg_pci_err_handler = {
127 	.reset_prepare = hbg_pci_err_reset_prepare,
128 	.reset_done = hbg_pci_err_reset_done,
129 };
130 
131 void hbg_set_pci_err_handler(struct pci_driver *pdrv)
132 {
133 	pdrv->err_handler = &hbg_pci_err_handler;
134 }
135