xref: /linux/drivers/net/ethernet/hisilicon/hns3/hns3pf/hclge_main.c (revision 4dd10fbbeb9c33709edc89916e19c03c780a0751)
1 // SPDX-License-Identifier: GPL-2.0+
2 // Copyright (c) 2016-2017 Hisilicon Limited.
3 
4 #include <linux/acpi.h>
5 #include <linux/device.h>
6 #include <linux/etherdevice.h>
7 #include <linux/init.h>
8 #include <linux/interrupt.h>
9 #include <linux/irq.h>
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/netdevice.h>
13 #include <linux/pci.h>
14 #include <linux/platform_device.h>
15 #include <linux/if_vlan.h>
16 #include <linux/crash_dump.h>
17 
18 #include <net/rtnetlink.h>
19 #include "hclge_cmd.h"
20 #include "hclge_dcb.h"
21 #include "hclge_fd.h"
22 #include "hclge_main.h"
23 #include "hclge_mbx.h"
24 #include "hclge_mdio.h"
25 #include "hclge_regs.h"
26 #include "hclge_tm.h"
27 #include "hclge_err.h"
28 #include "hnae3.h"
29 #include "hclge_devlink.h"
30 #include "hclge_comm_cmd.h"
31 
32 #include "hclge_trace.h"
33 
34 #define HCLGE_NAME			"hclge"
35 
36 #define HCLGE_BUF_SIZE_UNIT	256U
37 #define HCLGE_BUF_MUL_BY	2
38 #define HCLGE_BUF_DIV_BY	2
39 #define NEED_RESERVE_TC_NUM	2
40 #define BUF_MAX_PERCENT		100
41 #define BUF_RESERVE_PERCENT	90
42 
43 #define HCLGE_RESET_MAX_FAIL_CNT	5
44 #define HCLGE_RESET_SYNC_TIME		100
45 #define HCLGE_PF_RESET_SYNC_TIME	20
46 #define HCLGE_PF_RESET_SYNC_CNT		1500
47 
48 #define HCLGE_LINK_STATUS_MS	10
49 
50 static int hclge_set_mac_mtu(struct hclge_dev *hdev, int new_mps);
51 static int hclge_init_vlan_config(struct hclge_dev *hdev);
52 static void hclge_sync_vlan_filter(struct hclge_dev *hdev);
53 static int hclge_reset_ae_dev(struct hnae3_ae_dev *ae_dev);
54 static bool hclge_get_hw_reset_stat(struct hnae3_handle *handle);
55 static enum hnae3_reset_type hclge_get_reset_level(struct hnae3_ae_dev *ae_dev,
56 						   unsigned long *addr);
57 static int hclge_set_default_loopback(struct hclge_dev *hdev);
58 
59 static void hclge_sync_mac_table(struct hclge_dev *hdev);
60 static void hclge_restore_hw_table(struct hclge_dev *hdev);
61 static void hclge_sync_promisc_mode(struct hclge_dev *hdev);
62 static void hclge_update_fec_stats(struct hclge_dev *hdev);
63 static int hclge_mac_link_status_wait(struct hclge_dev *hdev, int link_ret,
64 				      int wait_cnt);
65 static int hclge_update_port_info(struct hclge_dev *hdev);
66 
67 static struct hnae3_ae_algo ae_algo;
68 
69 static struct workqueue_struct *hclge_wq;
70 
71 static const struct pci_device_id ae_algo_pci_tbl[] = {
72 	{ PCI_VDEVICE(HUAWEI, HNAE3_DEV_ID_GE) },
73 	{ PCI_VDEVICE(HUAWEI, HNAE3_DEV_ID_25GE) },
74 	{ PCI_VDEVICE(HUAWEI, HNAE3_DEV_ID_25GE_RDMA) },
75 	{ PCI_VDEVICE(HUAWEI, HNAE3_DEV_ID_25GE_RDMA_MACSEC) },
76 	{ PCI_VDEVICE(HUAWEI, HNAE3_DEV_ID_50GE_RDMA) },
77 	{ PCI_VDEVICE(HUAWEI, HNAE3_DEV_ID_50GE_RDMA_MACSEC) },
78 	{ PCI_VDEVICE(HUAWEI, HNAE3_DEV_ID_100G_RDMA_MACSEC) },
79 	{ PCI_VDEVICE(HUAWEI, HNAE3_DEV_ID_200G_RDMA) },
80 	/* required last entry */
81 	{ }
82 };
83 
84 MODULE_DEVICE_TABLE(pci, ae_algo_pci_tbl);
85 
86 static const char hns3_nic_test_strs[][ETH_GSTRING_LEN] = {
87 	"External Loopback test",
88 	"App      Loopback test",
89 	"Serdes   serial Loopback test",
90 	"Serdes   parallel Loopback test",
91 	"Phy      Loopback test"
92 };
93 
94 static const struct hclge_comm_stats_str g_mac_stats_string[] = {
95 	{"mac_tx_mac_pause_num", HCLGE_MAC_STATS_MAX_NUM_V1,
96 		HCLGE_MAC_STATS_FIELD_OFF(mac_tx_mac_pause_num)},
97 	{"mac_rx_mac_pause_num", HCLGE_MAC_STATS_MAX_NUM_V1,
98 		HCLGE_MAC_STATS_FIELD_OFF(mac_rx_mac_pause_num)},
99 	{"mac_tx_pause_xoff_time", HCLGE_MAC_STATS_MAX_NUM_V2,
100 		HCLGE_MAC_STATS_FIELD_OFF(mac_tx_pause_xoff_time)},
101 	{"mac_rx_pause_xoff_time", HCLGE_MAC_STATS_MAX_NUM_V2,
102 		HCLGE_MAC_STATS_FIELD_OFF(mac_rx_pause_xoff_time)},
103 	{"mac_tx_control_pkt_num", HCLGE_MAC_STATS_MAX_NUM_V1,
104 		HCLGE_MAC_STATS_FIELD_OFF(mac_tx_ctrl_pkt_num)},
105 	{"mac_rx_control_pkt_num", HCLGE_MAC_STATS_MAX_NUM_V1,
106 		HCLGE_MAC_STATS_FIELD_OFF(mac_rx_ctrl_pkt_num)},
107 	{"mac_tx_pfc_pkt_num", HCLGE_MAC_STATS_MAX_NUM_V1,
108 		HCLGE_MAC_STATS_FIELD_OFF(mac_tx_pfc_pause_pkt_num)},
109 	{"mac_tx_pfc_pri0_pkt_num", HCLGE_MAC_STATS_MAX_NUM_V1,
110 		HCLGE_MAC_STATS_FIELD_OFF(mac_tx_pfc_pri0_pkt_num)},
111 	{"mac_tx_pfc_pri1_pkt_num", HCLGE_MAC_STATS_MAX_NUM_V1,
112 		HCLGE_MAC_STATS_FIELD_OFF(mac_tx_pfc_pri1_pkt_num)},
113 	{"mac_tx_pfc_pri2_pkt_num", HCLGE_MAC_STATS_MAX_NUM_V1,
114 		HCLGE_MAC_STATS_FIELD_OFF(mac_tx_pfc_pri2_pkt_num)},
115 	{"mac_tx_pfc_pri3_pkt_num", HCLGE_MAC_STATS_MAX_NUM_V1,
116 		HCLGE_MAC_STATS_FIELD_OFF(mac_tx_pfc_pri3_pkt_num)},
117 	{"mac_tx_pfc_pri4_pkt_num", HCLGE_MAC_STATS_MAX_NUM_V1,
118 		HCLGE_MAC_STATS_FIELD_OFF(mac_tx_pfc_pri4_pkt_num)},
119 	{"mac_tx_pfc_pri5_pkt_num", HCLGE_MAC_STATS_MAX_NUM_V1,
120 		HCLGE_MAC_STATS_FIELD_OFF(mac_tx_pfc_pri5_pkt_num)},
121 	{"mac_tx_pfc_pri6_pkt_num", HCLGE_MAC_STATS_MAX_NUM_V1,
122 		HCLGE_MAC_STATS_FIELD_OFF(mac_tx_pfc_pri6_pkt_num)},
123 	{"mac_tx_pfc_pri7_pkt_num", HCLGE_MAC_STATS_MAX_NUM_V1,
124 		HCLGE_MAC_STATS_FIELD_OFF(mac_tx_pfc_pri7_pkt_num)},
125 	{"mac_tx_pfc_pri0_xoff_time", HCLGE_MAC_STATS_MAX_NUM_V2,
126 		HCLGE_MAC_STATS_FIELD_OFF(mac_tx_pfc_pri0_xoff_time)},
127 	{"mac_tx_pfc_pri1_xoff_time", HCLGE_MAC_STATS_MAX_NUM_V2,
128 		HCLGE_MAC_STATS_FIELD_OFF(mac_tx_pfc_pri1_xoff_time)},
129 	{"mac_tx_pfc_pri2_xoff_time", HCLGE_MAC_STATS_MAX_NUM_V2,
130 		HCLGE_MAC_STATS_FIELD_OFF(mac_tx_pfc_pri2_xoff_time)},
131 	{"mac_tx_pfc_pri3_xoff_time", HCLGE_MAC_STATS_MAX_NUM_V2,
132 		HCLGE_MAC_STATS_FIELD_OFF(mac_tx_pfc_pri3_xoff_time)},
133 	{"mac_tx_pfc_pri4_xoff_time", HCLGE_MAC_STATS_MAX_NUM_V2,
134 		HCLGE_MAC_STATS_FIELD_OFF(mac_tx_pfc_pri4_xoff_time)},
135 	{"mac_tx_pfc_pri5_xoff_time", HCLGE_MAC_STATS_MAX_NUM_V2,
136 		HCLGE_MAC_STATS_FIELD_OFF(mac_tx_pfc_pri5_xoff_time)},
137 	{"mac_tx_pfc_pri6_xoff_time", HCLGE_MAC_STATS_MAX_NUM_V2,
138 		HCLGE_MAC_STATS_FIELD_OFF(mac_tx_pfc_pri6_xoff_time)},
139 	{"mac_tx_pfc_pri7_xoff_time", HCLGE_MAC_STATS_MAX_NUM_V2,
140 		HCLGE_MAC_STATS_FIELD_OFF(mac_tx_pfc_pri7_xoff_time)},
141 	{"mac_rx_pfc_pkt_num", HCLGE_MAC_STATS_MAX_NUM_V1,
142 		HCLGE_MAC_STATS_FIELD_OFF(mac_rx_pfc_pause_pkt_num)},
143 	{"mac_rx_pfc_pri0_pkt_num", HCLGE_MAC_STATS_MAX_NUM_V1,
144 		HCLGE_MAC_STATS_FIELD_OFF(mac_rx_pfc_pri0_pkt_num)},
145 	{"mac_rx_pfc_pri1_pkt_num", HCLGE_MAC_STATS_MAX_NUM_V1,
146 		HCLGE_MAC_STATS_FIELD_OFF(mac_rx_pfc_pri1_pkt_num)},
147 	{"mac_rx_pfc_pri2_pkt_num", HCLGE_MAC_STATS_MAX_NUM_V1,
148 		HCLGE_MAC_STATS_FIELD_OFF(mac_rx_pfc_pri2_pkt_num)},
149 	{"mac_rx_pfc_pri3_pkt_num", HCLGE_MAC_STATS_MAX_NUM_V1,
150 		HCLGE_MAC_STATS_FIELD_OFF(mac_rx_pfc_pri3_pkt_num)},
151 	{"mac_rx_pfc_pri4_pkt_num", HCLGE_MAC_STATS_MAX_NUM_V1,
152 		HCLGE_MAC_STATS_FIELD_OFF(mac_rx_pfc_pri4_pkt_num)},
153 	{"mac_rx_pfc_pri5_pkt_num", HCLGE_MAC_STATS_MAX_NUM_V1,
154 		HCLGE_MAC_STATS_FIELD_OFF(mac_rx_pfc_pri5_pkt_num)},
155 	{"mac_rx_pfc_pri6_pkt_num", HCLGE_MAC_STATS_MAX_NUM_V1,
156 		HCLGE_MAC_STATS_FIELD_OFF(mac_rx_pfc_pri6_pkt_num)},
157 	{"mac_rx_pfc_pri7_pkt_num", HCLGE_MAC_STATS_MAX_NUM_V1,
158 		HCLGE_MAC_STATS_FIELD_OFF(mac_rx_pfc_pri7_pkt_num)},
159 	{"mac_rx_pfc_pri0_xoff_time", HCLGE_MAC_STATS_MAX_NUM_V2,
160 		HCLGE_MAC_STATS_FIELD_OFF(mac_rx_pfc_pri0_xoff_time)},
161 	{"mac_rx_pfc_pri1_xoff_time", HCLGE_MAC_STATS_MAX_NUM_V2,
162 		HCLGE_MAC_STATS_FIELD_OFF(mac_rx_pfc_pri1_xoff_time)},
163 	{"mac_rx_pfc_pri2_xoff_time", HCLGE_MAC_STATS_MAX_NUM_V2,
164 		HCLGE_MAC_STATS_FIELD_OFF(mac_rx_pfc_pri2_xoff_time)},
165 	{"mac_rx_pfc_pri3_xoff_time", HCLGE_MAC_STATS_MAX_NUM_V2,
166 		HCLGE_MAC_STATS_FIELD_OFF(mac_rx_pfc_pri3_xoff_time)},
167 	{"mac_rx_pfc_pri4_xoff_time", HCLGE_MAC_STATS_MAX_NUM_V2,
168 		HCLGE_MAC_STATS_FIELD_OFF(mac_rx_pfc_pri4_xoff_time)},
169 	{"mac_rx_pfc_pri5_xoff_time", HCLGE_MAC_STATS_MAX_NUM_V2,
170 		HCLGE_MAC_STATS_FIELD_OFF(mac_rx_pfc_pri5_xoff_time)},
171 	{"mac_rx_pfc_pri6_xoff_time", HCLGE_MAC_STATS_MAX_NUM_V2,
172 		HCLGE_MAC_STATS_FIELD_OFF(mac_rx_pfc_pri6_xoff_time)},
173 	{"mac_rx_pfc_pri7_xoff_time", HCLGE_MAC_STATS_MAX_NUM_V2,
174 		HCLGE_MAC_STATS_FIELD_OFF(mac_rx_pfc_pri7_xoff_time)},
175 	{"mac_tx_total_pkt_num", HCLGE_MAC_STATS_MAX_NUM_V1,
176 		HCLGE_MAC_STATS_FIELD_OFF(mac_tx_total_pkt_num)},
177 	{"mac_tx_total_oct_num", HCLGE_MAC_STATS_MAX_NUM_V1,
178 		HCLGE_MAC_STATS_FIELD_OFF(mac_tx_total_oct_num)},
179 	{"mac_tx_good_pkt_num", HCLGE_MAC_STATS_MAX_NUM_V1,
180 		HCLGE_MAC_STATS_FIELD_OFF(mac_tx_good_pkt_num)},
181 	{"mac_tx_bad_pkt_num", HCLGE_MAC_STATS_MAX_NUM_V1,
182 		HCLGE_MAC_STATS_FIELD_OFF(mac_tx_bad_pkt_num)},
183 	{"mac_tx_good_oct_num", HCLGE_MAC_STATS_MAX_NUM_V1,
184 		HCLGE_MAC_STATS_FIELD_OFF(mac_tx_good_oct_num)},
185 	{"mac_tx_bad_oct_num", HCLGE_MAC_STATS_MAX_NUM_V1,
186 		HCLGE_MAC_STATS_FIELD_OFF(mac_tx_bad_oct_num)},
187 	{"mac_tx_uni_pkt_num", HCLGE_MAC_STATS_MAX_NUM_V1,
188 		HCLGE_MAC_STATS_FIELD_OFF(mac_tx_uni_pkt_num)},
189 	{"mac_tx_multi_pkt_num", HCLGE_MAC_STATS_MAX_NUM_V1,
190 		HCLGE_MAC_STATS_FIELD_OFF(mac_tx_multi_pkt_num)},
191 	{"mac_tx_broad_pkt_num", HCLGE_MAC_STATS_MAX_NUM_V1,
192 		HCLGE_MAC_STATS_FIELD_OFF(mac_tx_broad_pkt_num)},
193 	{"mac_tx_undersize_pkt_num", HCLGE_MAC_STATS_MAX_NUM_V1,
194 		HCLGE_MAC_STATS_FIELD_OFF(mac_tx_undersize_pkt_num)},
195 	{"mac_tx_oversize_pkt_num", HCLGE_MAC_STATS_MAX_NUM_V1,
196 		HCLGE_MAC_STATS_FIELD_OFF(mac_tx_oversize_pkt_num)},
197 	{"mac_tx_64_oct_pkt_num", HCLGE_MAC_STATS_MAX_NUM_V1,
198 		HCLGE_MAC_STATS_FIELD_OFF(mac_tx_64_oct_pkt_num)},
199 	{"mac_tx_65_127_oct_pkt_num", HCLGE_MAC_STATS_MAX_NUM_V1,
200 		HCLGE_MAC_STATS_FIELD_OFF(mac_tx_65_127_oct_pkt_num)},
201 	{"mac_tx_128_255_oct_pkt_num", HCLGE_MAC_STATS_MAX_NUM_V1,
202 		HCLGE_MAC_STATS_FIELD_OFF(mac_tx_128_255_oct_pkt_num)},
203 	{"mac_tx_256_511_oct_pkt_num", HCLGE_MAC_STATS_MAX_NUM_V1,
204 		HCLGE_MAC_STATS_FIELD_OFF(mac_tx_256_511_oct_pkt_num)},
205 	{"mac_tx_512_1023_oct_pkt_num", HCLGE_MAC_STATS_MAX_NUM_V1,
206 		HCLGE_MAC_STATS_FIELD_OFF(mac_tx_512_1023_oct_pkt_num)},
207 	{"mac_tx_1024_1518_oct_pkt_num", HCLGE_MAC_STATS_MAX_NUM_V1,
208 		HCLGE_MAC_STATS_FIELD_OFF(mac_tx_1024_1518_oct_pkt_num)},
209 	{"mac_tx_1519_2047_oct_pkt_num", HCLGE_MAC_STATS_MAX_NUM_V1,
210 		HCLGE_MAC_STATS_FIELD_OFF(mac_tx_1519_2047_oct_pkt_num)},
211 	{"mac_tx_2048_4095_oct_pkt_num", HCLGE_MAC_STATS_MAX_NUM_V1,
212 		HCLGE_MAC_STATS_FIELD_OFF(mac_tx_2048_4095_oct_pkt_num)},
213 	{"mac_tx_4096_8191_oct_pkt_num", HCLGE_MAC_STATS_MAX_NUM_V1,
214 		HCLGE_MAC_STATS_FIELD_OFF(mac_tx_4096_8191_oct_pkt_num)},
215 	{"mac_tx_8192_9216_oct_pkt_num", HCLGE_MAC_STATS_MAX_NUM_V1,
216 		HCLGE_MAC_STATS_FIELD_OFF(mac_tx_8192_9216_oct_pkt_num)},
217 	{"mac_tx_9217_12287_oct_pkt_num", HCLGE_MAC_STATS_MAX_NUM_V1,
218 		HCLGE_MAC_STATS_FIELD_OFF(mac_tx_9217_12287_oct_pkt_num)},
219 	{"mac_tx_12288_16383_oct_pkt_num", HCLGE_MAC_STATS_MAX_NUM_V1,
220 		HCLGE_MAC_STATS_FIELD_OFF(mac_tx_12288_16383_oct_pkt_num)},
221 	{"mac_tx_1519_max_good_pkt_num", HCLGE_MAC_STATS_MAX_NUM_V1,
222 		HCLGE_MAC_STATS_FIELD_OFF(mac_tx_1519_max_good_oct_pkt_num)},
223 	{"mac_tx_1519_max_bad_pkt_num", HCLGE_MAC_STATS_MAX_NUM_V1,
224 		HCLGE_MAC_STATS_FIELD_OFF(mac_tx_1519_max_bad_oct_pkt_num)},
225 	{"mac_rx_total_pkt_num", HCLGE_MAC_STATS_MAX_NUM_V1,
226 		HCLGE_MAC_STATS_FIELD_OFF(mac_rx_total_pkt_num)},
227 	{"mac_rx_total_oct_num", HCLGE_MAC_STATS_MAX_NUM_V1,
228 		HCLGE_MAC_STATS_FIELD_OFF(mac_rx_total_oct_num)},
229 	{"mac_rx_good_pkt_num", HCLGE_MAC_STATS_MAX_NUM_V1,
230 		HCLGE_MAC_STATS_FIELD_OFF(mac_rx_good_pkt_num)},
231 	{"mac_rx_bad_pkt_num", HCLGE_MAC_STATS_MAX_NUM_V1,
232 		HCLGE_MAC_STATS_FIELD_OFF(mac_rx_bad_pkt_num)},
233 	{"mac_rx_good_oct_num", HCLGE_MAC_STATS_MAX_NUM_V1,
234 		HCLGE_MAC_STATS_FIELD_OFF(mac_rx_good_oct_num)},
235 	{"mac_rx_bad_oct_num", HCLGE_MAC_STATS_MAX_NUM_V1,
236 		HCLGE_MAC_STATS_FIELD_OFF(mac_rx_bad_oct_num)},
237 	{"mac_rx_uni_pkt_num", HCLGE_MAC_STATS_MAX_NUM_V1,
238 		HCLGE_MAC_STATS_FIELD_OFF(mac_rx_uni_pkt_num)},
239 	{"mac_rx_multi_pkt_num", HCLGE_MAC_STATS_MAX_NUM_V1,
240 		HCLGE_MAC_STATS_FIELD_OFF(mac_rx_multi_pkt_num)},
241 	{"mac_rx_broad_pkt_num", HCLGE_MAC_STATS_MAX_NUM_V1,
242 		HCLGE_MAC_STATS_FIELD_OFF(mac_rx_broad_pkt_num)},
243 	{"mac_rx_undersize_pkt_num", HCLGE_MAC_STATS_MAX_NUM_V1,
244 		HCLGE_MAC_STATS_FIELD_OFF(mac_rx_undersize_pkt_num)},
245 	{"mac_rx_oversize_pkt_num", HCLGE_MAC_STATS_MAX_NUM_V1,
246 		HCLGE_MAC_STATS_FIELD_OFF(mac_rx_oversize_pkt_num)},
247 	{"mac_rx_64_oct_pkt_num", HCLGE_MAC_STATS_MAX_NUM_V1,
248 		HCLGE_MAC_STATS_FIELD_OFF(mac_rx_64_oct_pkt_num)},
249 	{"mac_rx_65_127_oct_pkt_num", HCLGE_MAC_STATS_MAX_NUM_V1,
250 		HCLGE_MAC_STATS_FIELD_OFF(mac_rx_65_127_oct_pkt_num)},
251 	{"mac_rx_128_255_oct_pkt_num", HCLGE_MAC_STATS_MAX_NUM_V1,
252 		HCLGE_MAC_STATS_FIELD_OFF(mac_rx_128_255_oct_pkt_num)},
253 	{"mac_rx_256_511_oct_pkt_num", HCLGE_MAC_STATS_MAX_NUM_V1,
254 		HCLGE_MAC_STATS_FIELD_OFF(mac_rx_256_511_oct_pkt_num)},
255 	{"mac_rx_512_1023_oct_pkt_num", HCLGE_MAC_STATS_MAX_NUM_V1,
256 		HCLGE_MAC_STATS_FIELD_OFF(mac_rx_512_1023_oct_pkt_num)},
257 	{"mac_rx_1024_1518_oct_pkt_num", HCLGE_MAC_STATS_MAX_NUM_V1,
258 		HCLGE_MAC_STATS_FIELD_OFF(mac_rx_1024_1518_oct_pkt_num)},
259 	{"mac_rx_1519_2047_oct_pkt_num", HCLGE_MAC_STATS_MAX_NUM_V1,
260 		HCLGE_MAC_STATS_FIELD_OFF(mac_rx_1519_2047_oct_pkt_num)},
261 	{"mac_rx_2048_4095_oct_pkt_num", HCLGE_MAC_STATS_MAX_NUM_V1,
262 		HCLGE_MAC_STATS_FIELD_OFF(mac_rx_2048_4095_oct_pkt_num)},
263 	{"mac_rx_4096_8191_oct_pkt_num", HCLGE_MAC_STATS_MAX_NUM_V1,
264 		HCLGE_MAC_STATS_FIELD_OFF(mac_rx_4096_8191_oct_pkt_num)},
265 	{"mac_rx_8192_9216_oct_pkt_num", HCLGE_MAC_STATS_MAX_NUM_V1,
266 		HCLGE_MAC_STATS_FIELD_OFF(mac_rx_8192_9216_oct_pkt_num)},
267 	{"mac_rx_9217_12287_oct_pkt_num", HCLGE_MAC_STATS_MAX_NUM_V1,
268 		HCLGE_MAC_STATS_FIELD_OFF(mac_rx_9217_12287_oct_pkt_num)},
269 	{"mac_rx_12288_16383_oct_pkt_num", HCLGE_MAC_STATS_MAX_NUM_V1,
270 		HCLGE_MAC_STATS_FIELD_OFF(mac_rx_12288_16383_oct_pkt_num)},
271 	{"mac_rx_1519_max_good_pkt_num", HCLGE_MAC_STATS_MAX_NUM_V1,
272 		HCLGE_MAC_STATS_FIELD_OFF(mac_rx_1519_max_good_oct_pkt_num)},
273 	{"mac_rx_1519_max_bad_pkt_num", HCLGE_MAC_STATS_MAX_NUM_V1,
274 		HCLGE_MAC_STATS_FIELD_OFF(mac_rx_1519_max_bad_oct_pkt_num)},
275 
276 	{"mac_tx_fragment_pkt_num", HCLGE_MAC_STATS_MAX_NUM_V1,
277 		HCLGE_MAC_STATS_FIELD_OFF(mac_tx_fragment_pkt_num)},
278 	{"mac_tx_undermin_pkt_num", HCLGE_MAC_STATS_MAX_NUM_V1,
279 		HCLGE_MAC_STATS_FIELD_OFF(mac_tx_undermin_pkt_num)},
280 	{"mac_tx_jabber_pkt_num", HCLGE_MAC_STATS_MAX_NUM_V1,
281 		HCLGE_MAC_STATS_FIELD_OFF(mac_tx_jabber_pkt_num)},
282 	{"mac_tx_err_all_pkt_num", HCLGE_MAC_STATS_MAX_NUM_V1,
283 		HCLGE_MAC_STATS_FIELD_OFF(mac_tx_err_all_pkt_num)},
284 	{"mac_tx_from_app_good_pkt_num", HCLGE_MAC_STATS_MAX_NUM_V1,
285 		HCLGE_MAC_STATS_FIELD_OFF(mac_tx_from_app_good_pkt_num)},
286 	{"mac_tx_from_app_bad_pkt_num", HCLGE_MAC_STATS_MAX_NUM_V1,
287 		HCLGE_MAC_STATS_FIELD_OFF(mac_tx_from_app_bad_pkt_num)},
288 	{"mac_rx_fragment_pkt_num", HCLGE_MAC_STATS_MAX_NUM_V1,
289 		HCLGE_MAC_STATS_FIELD_OFF(mac_rx_fragment_pkt_num)},
290 	{"mac_rx_undermin_pkt_num", HCLGE_MAC_STATS_MAX_NUM_V1,
291 		HCLGE_MAC_STATS_FIELD_OFF(mac_rx_undermin_pkt_num)},
292 	{"mac_rx_jabber_pkt_num", HCLGE_MAC_STATS_MAX_NUM_V1,
293 		HCLGE_MAC_STATS_FIELD_OFF(mac_rx_jabber_pkt_num)},
294 	{"mac_rx_fcs_err_pkt_num", HCLGE_MAC_STATS_MAX_NUM_V1,
295 		HCLGE_MAC_STATS_FIELD_OFF(mac_rx_fcs_err_pkt_num)},
296 	{"mac_rx_send_app_good_pkt_num", HCLGE_MAC_STATS_MAX_NUM_V1,
297 		HCLGE_MAC_STATS_FIELD_OFF(mac_rx_send_app_good_pkt_num)},
298 	{"mac_rx_send_app_bad_pkt_num", HCLGE_MAC_STATS_MAX_NUM_V1,
299 		HCLGE_MAC_STATS_FIELD_OFF(mac_rx_send_app_bad_pkt_num)}
300 };
301 
302 static const struct hclge_mac_mgr_tbl_entry_cmd hclge_mgr_table[] = {
303 	{
304 		.flags = HCLGE_MAC_MGR_MASK_VLAN_B,
305 		.ethter_type = cpu_to_le16(ETH_P_LLDP),
306 		.mac_addr = {0x01, 0x80, 0xc2, 0x00, 0x00, 0x0e},
307 		.i_port_bitmap = 0x1,
308 	},
309 };
310 
311 /**
312  * hclge_cmd_send - send command to command queue
313  * @hw: pointer to the hw struct
314  * @desc: prefilled descriptor for describing the command
315  * @num : the number of descriptors to be sent
316  *
317  * This is the main send command for command queue, it
318  * sends the queue, cleans the queue, etc
319  **/
320 int hclge_cmd_send(struct hclge_hw *hw, struct hclge_desc *desc, int num)
321 {
322 	return hclge_comm_cmd_send(&hw->hw, desc, num);
323 }
324 
325 static void hclge_trace_cmd_send(struct hclge_comm_hw *hw, struct hclge_desc *desc,
326 				 int num, bool is_special)
327 {
328 	int i;
329 
330 	trace_hclge_pf_cmd_send(hw, desc, 0, num);
331 
332 	if (!is_special) {
333 		for (i = 1; i < num; i++)
334 			trace_hclge_pf_cmd_send(hw, &desc[i], i, num);
335 	} else {
336 		for (i = 1; i < num; i++)
337 			trace_hclge_pf_special_cmd_send(hw, (__le32 *)&desc[i],
338 							i, num);
339 	}
340 }
341 
342 static void hclge_trace_cmd_get(struct hclge_comm_hw *hw, struct hclge_desc *desc,
343 				int num, bool is_special)
344 {
345 	int i;
346 
347 	if (!HCLGE_COMM_SEND_SYNC(le16_to_cpu(desc->flag)))
348 		return;
349 
350 	trace_hclge_pf_cmd_get(hw, desc, 0, num);
351 
352 	if (!is_special) {
353 		for (i = 1; i < num; i++)
354 			trace_hclge_pf_cmd_get(hw, &desc[i], i, num);
355 	} else {
356 		for (i = 1; i < num; i++)
357 			trace_hclge_pf_special_cmd_get(hw, (__le32 *)&desc[i],
358 						       i, num);
359 	}
360 }
361 
362 static const struct hclge_comm_cmq_ops hclge_cmq_ops = {
363 	.trace_cmd_send = hclge_trace_cmd_send,
364 	.trace_cmd_get = hclge_trace_cmd_get,
365 };
366 
367 static int hclge_mac_update_stats_defective(struct hclge_dev *hdev)
368 {
369 #define HCLGE_MAC_CMD_NUM 21
370 
371 	u64 *data = (u64 *)(&hdev->mac_stats);
372 	struct hclge_desc desc[HCLGE_MAC_CMD_NUM];
373 	__le64 *desc_data;
374 	u32 data_size;
375 	int ret;
376 	u32 i;
377 
378 	hclge_cmd_setup_basic_desc(&desc[0], HCLGE_OPC_STATS_MAC, true);
379 	ret = hclge_cmd_send(&hdev->hw, desc, HCLGE_MAC_CMD_NUM);
380 	if (ret) {
381 		dev_err(&hdev->pdev->dev,
382 			"Get MAC pkt stats fail, status = %d.\n", ret);
383 
384 		return ret;
385 	}
386 
387 	/* The first desc has a 64-bit header, so data size need to minus 1 */
388 	data_size = sizeof(desc) / (sizeof(u64)) - 1;
389 
390 	desc_data = (__le64 *)(&desc[0].data[0]);
391 	for (i = 0; i < data_size; i++) {
392 		/* data memory is continuous becase only the first desc has a
393 		 * header in this command
394 		 */
395 		*data += le64_to_cpu(*desc_data);
396 		data++;
397 		desc_data++;
398 	}
399 
400 	return 0;
401 }
402 
403 static int hclge_mac_update_stats_complete(struct hclge_dev *hdev)
404 {
405 #define HCLGE_REG_NUM_PER_DESC		4
406 
407 	u32 reg_num = hdev->ae_dev->dev_specs.mac_stats_num;
408 	u64 *data = (u64 *)(&hdev->mac_stats);
409 	struct hclge_desc *desc;
410 	__le64 *desc_data;
411 	u32 data_size;
412 	u32 desc_num;
413 	int ret;
414 	u32 i;
415 
416 	/* The first desc has a 64-bit header, so need to consider it */
417 	desc_num = reg_num / HCLGE_REG_NUM_PER_DESC + 1;
418 
419 	/* This may be called inside atomic sections,
420 	 * so GFP_ATOMIC is more suitable here
421 	 */
422 	desc = kzalloc_objs(struct hclge_desc, desc_num, GFP_ATOMIC);
423 	if (!desc)
424 		return -ENOMEM;
425 
426 	hclge_cmd_setup_basic_desc(&desc[0], HCLGE_OPC_STATS_MAC_ALL, true);
427 	ret = hclge_cmd_send(&hdev->hw, desc, desc_num);
428 	if (ret) {
429 		kfree(desc);
430 		return ret;
431 	}
432 
433 	data_size = min_t(u32, sizeof(hdev->mac_stats) / sizeof(u64), reg_num);
434 
435 	desc_data = (__le64 *)(&desc[0].data[0]);
436 	for (i = 0; i < data_size; i++) {
437 		/* data memory is continuous becase only the first desc has a
438 		 * header in this command
439 		 */
440 		*data += le64_to_cpu(*desc_data);
441 		data++;
442 		desc_data++;
443 	}
444 
445 	kfree(desc);
446 
447 	return 0;
448 }
449 
450 static int hclge_mac_query_reg_num(struct hclge_dev *hdev, u32 *reg_num)
451 {
452 	struct hclge_desc desc;
453 	int ret;
454 
455 	/* Driver needs total register number of both valid registers and
456 	 * reserved registers, but the old firmware only returns number
457 	 * of valid registers in device V2. To be compatible with these
458 	 * devices, driver uses a fixed value.
459 	 */
460 	if (hdev->ae_dev->dev_version == HNAE3_DEVICE_VERSION_V2) {
461 		*reg_num = HCLGE_MAC_STATS_MAX_NUM_V1;
462 		return 0;
463 	}
464 
465 	hclge_cmd_setup_basic_desc(&desc, HCLGE_OPC_QUERY_MAC_REG_NUM, true);
466 	ret = hclge_cmd_send(&hdev->hw, &desc, 1);
467 	if (ret) {
468 		dev_err(&hdev->pdev->dev,
469 			"failed to query mac statistic reg number, ret = %d\n",
470 			ret);
471 		return ret;
472 	}
473 
474 	*reg_num = le32_to_cpu(desc.data[0]);
475 	if (*reg_num == 0) {
476 		dev_err(&hdev->pdev->dev,
477 			"mac statistic reg number is invalid!\n");
478 		return -ENODATA;
479 	}
480 
481 	return 0;
482 }
483 
484 int hclge_mac_update_stats(struct hclge_dev *hdev)
485 {
486 	/* The firmware supports the new statistics acquisition method */
487 	if (hdev->ae_dev->dev_specs.mac_stats_num)
488 		return hclge_mac_update_stats_complete(hdev);
489 	else
490 		return hclge_mac_update_stats_defective(hdev);
491 }
492 
493 static int hclge_comm_get_count(struct hclge_dev *hdev,
494 				const struct hclge_comm_stats_str strs[],
495 				u32 size)
496 {
497 	int count = 0;
498 	u32 i;
499 
500 	for (i = 0; i < size; i++)
501 		if (strs[i].stats_num <= hdev->ae_dev->dev_specs.mac_stats_num)
502 			count++;
503 
504 	return count;
505 }
506 
507 static u64 *hclge_comm_get_stats(struct hclge_dev *hdev,
508 				 const struct hclge_comm_stats_str strs[],
509 				 int size, u64 *data)
510 {
511 	u64 *buf = data;
512 	int i;
513 
514 	for (i = 0; i < size; i++) {
515 		if (strs[i].stats_num > hdev->ae_dev->dev_specs.mac_stats_num)
516 			continue;
517 
518 		*buf = HCLGE_STATS_READ(&hdev->mac_stats, strs[i].offset);
519 		buf++;
520 	}
521 
522 	return buf;
523 }
524 
525 static void hclge_comm_get_strings(struct hclge_dev *hdev, u32 stringset,
526 				   const struct hclge_comm_stats_str strs[],
527 				   int size, u8 **data)
528 {
529 	int i;
530 
531 	if (stringset != ETH_SS_STATS)
532 		return;
533 
534 	for (i = 0; i < size; i++) {
535 		if (strs[i].stats_num > hdev->ae_dev->dev_specs.mac_stats_num)
536 			continue;
537 
538 		ethtool_puts(data, strs[i].desc);
539 	}
540 }
541 
542 static void hclge_update_stats_for_all(struct hclge_dev *hdev)
543 {
544 	struct hnae3_handle *handle;
545 	int status;
546 
547 	handle = &hdev->vport[0].nic;
548 	if (handle->client) {
549 		status = hclge_comm_tqps_update_stats(handle, &hdev->hw.hw);
550 		if (status) {
551 			dev_err(&hdev->pdev->dev,
552 				"Update TQPS stats fail, status = %d.\n",
553 				status);
554 		}
555 	}
556 
557 	hclge_update_fec_stats(hdev);
558 
559 	status = hclge_mac_update_stats(hdev);
560 	if (status)
561 		dev_err(&hdev->pdev->dev,
562 			"Update MAC stats fail, status = %d.\n", status);
563 }
564 
565 static void hclge_update_stats(struct hnae3_handle *handle)
566 {
567 	struct hclge_vport *vport = hclge_get_vport(handle);
568 	struct hclge_dev *hdev = vport->back;
569 	int status;
570 
571 	if (test_and_set_bit(HCLGE_STATE_STATISTICS_UPDATING, &hdev->state))
572 		return;
573 
574 	status = hclge_mac_update_stats(hdev);
575 	if (status)
576 		dev_err(&hdev->pdev->dev,
577 			"Update MAC stats fail, status = %d.\n",
578 			status);
579 
580 	status = hclge_comm_tqps_update_stats(handle, &hdev->hw.hw);
581 	if (status)
582 		dev_err(&hdev->pdev->dev,
583 			"Update TQPS stats fail, status = %d.\n",
584 			status);
585 
586 	clear_bit(HCLGE_STATE_STATISTICS_UPDATING, &hdev->state);
587 }
588 
589 static int hclge_get_sset_count(struct hnae3_handle *handle, int stringset)
590 {
591 #define HCLGE_LOOPBACK_TEST_FLAGS (HNAE3_SUPPORT_APP_LOOPBACK | \
592 		HNAE3_SUPPORT_PHY_LOOPBACK | \
593 		HNAE3_SUPPORT_SERDES_SERIAL_LOOPBACK | \
594 		HNAE3_SUPPORT_SERDES_PARALLEL_LOOPBACK | \
595 		HNAE3_SUPPORT_EXTERNAL_LOOPBACK)
596 
597 	struct hclge_vport *vport = hclge_get_vport(handle);
598 	struct hclge_dev *hdev = vport->back;
599 	int count = 0;
600 
601 	/* Loopback test support rules:
602 	 * mac: only GE mode support
603 	 * serdes: all mac mode will support include GE/XGE/LGE/CGE
604 	 * phy: only support when phy device exist on board
605 	 */
606 	if (stringset == ETH_SS_TEST) {
607 		/* clear loopback bit flags at first */
608 		handle->flags = (handle->flags & (~HCLGE_LOOPBACK_TEST_FLAGS));
609 		if (hdev->ae_dev->dev_version >= HNAE3_DEVICE_VERSION_V2 ||
610 		    hdev->hw.mac.speed == HCLGE_MAC_SPEED_10M ||
611 		    hdev->hw.mac.speed == HCLGE_MAC_SPEED_100M ||
612 		    hdev->hw.mac.speed == HCLGE_MAC_SPEED_1G) {
613 			count += 1;
614 			handle->flags |= HNAE3_SUPPORT_APP_LOOPBACK;
615 		}
616 
617 		if (hdev->ae_dev->dev_specs.hilink_version !=
618 		    HCLGE_HILINK_H60) {
619 			count += 1;
620 			handle->flags |= HNAE3_SUPPORT_SERDES_SERIAL_LOOPBACK;
621 		}
622 
623 		count += 1;
624 		handle->flags |= HNAE3_SUPPORT_SERDES_PARALLEL_LOOPBACK;
625 		count += 1;
626 		handle->flags |= HNAE3_SUPPORT_EXTERNAL_LOOPBACK;
627 
628 		if ((hdev->hw.mac.phydev && hdev->hw.mac.phydev->drv &&
629 		     hdev->hw.mac.phydev->drv->set_loopback) ||
630 		    hnae3_dev_phy_imp_supported(hdev)) {
631 			count += 1;
632 			handle->flags |= HNAE3_SUPPORT_PHY_LOOPBACK;
633 		}
634 	} else if (stringset == ETH_SS_STATS) {
635 		count = hclge_comm_get_count(hdev, g_mac_stats_string,
636 					     ARRAY_SIZE(g_mac_stats_string)) +
637 			hclge_comm_tqps_get_sset_count(handle);
638 	}
639 
640 	return count;
641 }
642 
643 static void hclge_get_strings(struct hnae3_handle *handle, u32 stringset,
644 			      u8 **data)
645 {
646 	struct hclge_vport *vport = hclge_get_vport(handle);
647 	struct hclge_dev *hdev = vport->back;
648 	const char *str;
649 	int size;
650 
651 	if (stringset == ETH_SS_STATS) {
652 		size = ARRAY_SIZE(g_mac_stats_string);
653 		hclge_comm_get_strings(hdev, stringset, g_mac_stats_string,
654 				       size, data);
655 		hclge_comm_tqps_get_strings(handle, data);
656 	} else if (stringset == ETH_SS_TEST) {
657 		if (handle->flags & HNAE3_SUPPORT_EXTERNAL_LOOPBACK) {
658 			str = hns3_nic_test_strs[HNAE3_LOOP_EXTERNAL];
659 			ethtool_puts(data, str);
660 		}
661 		if (handle->flags & HNAE3_SUPPORT_APP_LOOPBACK) {
662 			str = hns3_nic_test_strs[HNAE3_LOOP_APP];
663 			ethtool_puts(data, str);
664 		}
665 		if (handle->flags & HNAE3_SUPPORT_SERDES_SERIAL_LOOPBACK) {
666 			str = hns3_nic_test_strs[HNAE3_LOOP_SERIAL_SERDES];
667 			ethtool_puts(data, str);
668 		}
669 		if (handle->flags & HNAE3_SUPPORT_SERDES_PARALLEL_LOOPBACK) {
670 			str = hns3_nic_test_strs[HNAE3_LOOP_PARALLEL_SERDES];
671 			ethtool_puts(data, str);
672 		}
673 		if (handle->flags & HNAE3_SUPPORT_PHY_LOOPBACK) {
674 			str = hns3_nic_test_strs[HNAE3_LOOP_PHY];
675 			ethtool_puts(data, str);
676 		}
677 	}
678 }
679 
680 static void hclge_get_stats(struct hnae3_handle *handle, u64 *data)
681 {
682 	struct hclge_vport *vport = hclge_get_vport(handle);
683 	struct hclge_dev *hdev = vport->back;
684 	u64 *p;
685 
686 	p = hclge_comm_get_stats(hdev, g_mac_stats_string,
687 				 ARRAY_SIZE(g_mac_stats_string), data);
688 	p = hclge_comm_tqps_get_stats(handle, p);
689 }
690 
691 static void hclge_get_mac_stat(struct hnae3_handle *handle,
692 			       struct hns3_mac_stats *mac_stats)
693 {
694 	struct hclge_vport *vport = hclge_get_vport(handle);
695 	struct hclge_dev *hdev = vport->back;
696 
697 	hclge_update_stats(handle);
698 
699 	mac_stats->tx_pause_cnt = hdev->mac_stats.mac_tx_mac_pause_num;
700 	mac_stats->rx_pause_cnt = hdev->mac_stats.mac_rx_mac_pause_num;
701 }
702 
703 static int hclge_parse_func_status(struct hclge_dev *hdev,
704 				   struct hclge_func_status_cmd *status)
705 {
706 #define HCLGE_MAC_ID_MASK	0xF
707 
708 	if (!(status->pf_state & HCLGE_PF_STATE_DONE))
709 		return -EINVAL;
710 
711 	/* Set the pf to main pf */
712 	if (status->pf_state & HCLGE_PF_STATE_MAIN)
713 		hdev->flag |= HCLGE_FLAG_MAIN;
714 	else
715 		hdev->flag &= ~HCLGE_FLAG_MAIN;
716 
717 	hdev->hw.mac.mac_id = status->mac_id & HCLGE_MAC_ID_MASK;
718 	return 0;
719 }
720 
721 static int hclge_query_function_status(struct hclge_dev *hdev)
722 {
723 #define HCLGE_QUERY_MAX_CNT	5
724 
725 	struct hclge_func_status_cmd *req;
726 	struct hclge_desc desc;
727 	int timeout = 0;
728 	int ret;
729 
730 	hclge_cmd_setup_basic_desc(&desc, HCLGE_OPC_QUERY_FUNC_STATUS, true);
731 	req = (struct hclge_func_status_cmd *)desc.data;
732 
733 	do {
734 		ret = hclge_cmd_send(&hdev->hw, &desc, 1);
735 		if (ret) {
736 			dev_err(&hdev->pdev->dev,
737 				"query function status failed %d.\n", ret);
738 			return ret;
739 		}
740 
741 		/* Check pf reset is done */
742 		if (req->pf_state)
743 			break;
744 		usleep_range(1000, 2000);
745 	} while (timeout++ < HCLGE_QUERY_MAX_CNT);
746 
747 	return hclge_parse_func_status(hdev, req);
748 }
749 
750 static int hclge_query_pf_resource(struct hclge_dev *hdev)
751 {
752 	struct hclge_pf_res_cmd *req;
753 	struct hclge_desc desc;
754 	int ret;
755 
756 	hclge_cmd_setup_basic_desc(&desc, HCLGE_OPC_QUERY_PF_RSRC, true);
757 	ret = hclge_cmd_send(&hdev->hw, &desc, 1);
758 	if (ret) {
759 		dev_err(&hdev->pdev->dev,
760 			"query pf resource failed %d.\n", ret);
761 		return ret;
762 	}
763 
764 	req = (struct hclge_pf_res_cmd *)desc.data;
765 	hdev->num_tqps = le16_to_cpu(req->tqp_num) +
766 			 le16_to_cpu(req->ext_tqp_num);
767 	hdev->pkt_buf_size = le16_to_cpu(req->buf_size) << HCLGE_BUF_UNIT_S;
768 
769 	if (req->tx_buf_size)
770 		hdev->tx_buf_size =
771 			le16_to_cpu(req->tx_buf_size) << HCLGE_BUF_UNIT_S;
772 	else
773 		hdev->tx_buf_size = HCLGE_DEFAULT_TX_BUF;
774 
775 	hdev->tx_buf_size = roundup(hdev->tx_buf_size, HCLGE_BUF_SIZE_UNIT);
776 
777 	if (req->dv_buf_size)
778 		hdev->dv_buf_size =
779 			le16_to_cpu(req->dv_buf_size) << HCLGE_BUF_UNIT_S;
780 	else
781 		hdev->dv_buf_size = HCLGE_DEFAULT_DV;
782 
783 	hdev->dv_buf_size = roundup(hdev->dv_buf_size, HCLGE_BUF_SIZE_UNIT);
784 
785 	hdev->num_nic_msi = le16_to_cpu(req->msixcap_localid_number_nic);
786 	if (hdev->num_nic_msi < HNAE3_MIN_VECTOR_NUM) {
787 		dev_err(&hdev->pdev->dev,
788 			"only %u msi resources available, not enough for pf(min:2).\n",
789 			hdev->num_nic_msi);
790 		return -EINVAL;
791 	}
792 
793 	if (hnae3_dev_roce_supported(hdev)) {
794 		hdev->num_roce_msi =
795 			le16_to_cpu(req->pf_intr_vector_number_roce);
796 
797 		/* PF should have NIC vectors and Roce vectors,
798 		 * NIC vectors are queued before Roce vectors.
799 		 */
800 		hdev->num_msi = hdev->num_nic_msi + hdev->num_roce_msi;
801 	} else {
802 		hdev->num_msi = hdev->num_nic_msi;
803 	}
804 
805 	return 0;
806 }
807 
808 static int hclge_parse_speed(u8 speed_cmd, u32 *speed)
809 {
810 	switch (speed_cmd) {
811 	case HCLGE_FW_MAC_SPEED_10M:
812 		*speed = HCLGE_MAC_SPEED_10M;
813 		break;
814 	case HCLGE_FW_MAC_SPEED_100M:
815 		*speed = HCLGE_MAC_SPEED_100M;
816 		break;
817 	case HCLGE_FW_MAC_SPEED_1G:
818 		*speed = HCLGE_MAC_SPEED_1G;
819 		break;
820 	case HCLGE_FW_MAC_SPEED_10G:
821 		*speed = HCLGE_MAC_SPEED_10G;
822 		break;
823 	case HCLGE_FW_MAC_SPEED_25G:
824 		*speed = HCLGE_MAC_SPEED_25G;
825 		break;
826 	case HCLGE_FW_MAC_SPEED_40G:
827 		*speed = HCLGE_MAC_SPEED_40G;
828 		break;
829 	case HCLGE_FW_MAC_SPEED_50G:
830 		*speed = HCLGE_MAC_SPEED_50G;
831 		break;
832 	case HCLGE_FW_MAC_SPEED_100G:
833 		*speed = HCLGE_MAC_SPEED_100G;
834 		break;
835 	case HCLGE_FW_MAC_SPEED_200G:
836 		*speed = HCLGE_MAC_SPEED_200G;
837 		break;
838 	default:
839 		return -EINVAL;
840 	}
841 
842 	return 0;
843 }
844 
845 static const struct hclge_speed_bit_map speed_bit_map[] = {
846 	{HCLGE_MAC_SPEED_10M, HCLGE_SUPPORT_10M_BIT},
847 	{HCLGE_MAC_SPEED_100M, HCLGE_SUPPORT_100M_BIT},
848 	{HCLGE_MAC_SPEED_1G, HCLGE_SUPPORT_1G_BIT},
849 	{HCLGE_MAC_SPEED_10G, HCLGE_SUPPORT_10G_BIT},
850 	{HCLGE_MAC_SPEED_25G, HCLGE_SUPPORT_25G_BIT},
851 	{HCLGE_MAC_SPEED_40G, HCLGE_SUPPORT_40G_BIT},
852 	{HCLGE_MAC_SPEED_50G, HCLGE_SUPPORT_50G_BITS},
853 	{HCLGE_MAC_SPEED_100G, HCLGE_SUPPORT_100G_BITS},
854 	{HCLGE_MAC_SPEED_200G, HCLGE_SUPPORT_200G_BITS},
855 };
856 
857 static int hclge_get_speed_bit(u32 speed, u32 *speed_bit)
858 {
859 	u16 i;
860 
861 	for (i = 0; i < ARRAY_SIZE(speed_bit_map); i++) {
862 		if (speed == speed_bit_map[i].speed) {
863 			*speed_bit = speed_bit_map[i].speed_bit;
864 			return 0;
865 		}
866 	}
867 
868 	return -EINVAL;
869 }
870 
871 static int hclge_check_port_speed(struct hnae3_handle *handle, u32 speed)
872 {
873 	struct hclge_vport *vport = hclge_get_vport(handle);
874 	struct hclge_dev *hdev = vport->back;
875 	u32 speed_ability = hdev->hw.mac.speed_ability;
876 	u32 speed_bit = 0;
877 	int ret;
878 
879 	ret = hclge_get_speed_bit(speed, &speed_bit);
880 	if (ret)
881 		return ret;
882 
883 	if (speed_bit & speed_ability)
884 		return 0;
885 
886 	return -EINVAL;
887 }
888 
889 static void hclge_update_fec_support(struct hclge_mac *mac)
890 {
891 	linkmode_clear_bit(ETHTOOL_LINK_MODE_FEC_BASER_BIT, mac->supported);
892 	linkmode_clear_bit(ETHTOOL_LINK_MODE_FEC_RS_BIT, mac->supported);
893 	linkmode_clear_bit(ETHTOOL_LINK_MODE_FEC_LLRS_BIT, mac->supported);
894 	linkmode_clear_bit(ETHTOOL_LINK_MODE_FEC_NONE_BIT, mac->supported);
895 
896 	if (mac->fec_ability & BIT(HNAE3_FEC_BASER))
897 		linkmode_set_bit(ETHTOOL_LINK_MODE_FEC_BASER_BIT,
898 				 mac->supported);
899 	if (mac->fec_ability & BIT(HNAE3_FEC_RS))
900 		linkmode_set_bit(ETHTOOL_LINK_MODE_FEC_RS_BIT,
901 				 mac->supported);
902 	if (mac->fec_ability & BIT(HNAE3_FEC_LLRS))
903 		linkmode_set_bit(ETHTOOL_LINK_MODE_FEC_LLRS_BIT,
904 				 mac->supported);
905 	if (mac->fec_ability & BIT(HNAE3_FEC_NONE))
906 		linkmode_set_bit(ETHTOOL_LINK_MODE_FEC_NONE_BIT,
907 				 mac->supported);
908 }
909 
910 static const struct hclge_link_mode_bmap hclge_sr_link_mode_bmap[] = {
911 	{HCLGE_SUPPORT_10G_BIT, ETHTOOL_LINK_MODE_10000baseSR_Full_BIT},
912 	{HCLGE_SUPPORT_25G_BIT, ETHTOOL_LINK_MODE_25000baseSR_Full_BIT},
913 	{HCLGE_SUPPORT_40G_BIT, ETHTOOL_LINK_MODE_40000baseSR4_Full_BIT},
914 	{HCLGE_SUPPORT_50G_R2_BIT, ETHTOOL_LINK_MODE_50000baseSR2_Full_BIT},
915 	{HCLGE_SUPPORT_50G_R1_BIT, ETHTOOL_LINK_MODE_50000baseSR_Full_BIT},
916 	{HCLGE_SUPPORT_100G_R4_BIT, ETHTOOL_LINK_MODE_100000baseSR4_Full_BIT},
917 	{HCLGE_SUPPORT_100G_R2_BIT, ETHTOOL_LINK_MODE_100000baseSR2_Full_BIT},
918 	{HCLGE_SUPPORT_200G_R4_EXT_BIT,
919 	 ETHTOOL_LINK_MODE_200000baseSR4_Full_BIT},
920 	{HCLGE_SUPPORT_200G_R4_BIT, ETHTOOL_LINK_MODE_200000baseSR4_Full_BIT},
921 };
922 
923 static const struct hclge_link_mode_bmap hclge_lr_link_mode_bmap[] = {
924 	{HCLGE_SUPPORT_10G_BIT, ETHTOOL_LINK_MODE_10000baseLR_Full_BIT},
925 	{HCLGE_SUPPORT_40G_BIT, ETHTOOL_LINK_MODE_40000baseLR4_Full_BIT},
926 	{HCLGE_SUPPORT_50G_R1_BIT, ETHTOOL_LINK_MODE_50000baseLR_ER_FR_Full_BIT},
927 	{HCLGE_SUPPORT_100G_R4_BIT,
928 	 ETHTOOL_LINK_MODE_100000baseLR4_ER4_Full_BIT},
929 	{HCLGE_SUPPORT_100G_R2_BIT,
930 	 ETHTOOL_LINK_MODE_100000baseLR2_ER2_FR2_Full_BIT},
931 	{HCLGE_SUPPORT_200G_R4_EXT_BIT,
932 	 ETHTOOL_LINK_MODE_200000baseLR4_ER4_FR4_Full_BIT},
933 	{HCLGE_SUPPORT_200G_R4_BIT,
934 	 ETHTOOL_LINK_MODE_200000baseLR4_ER4_FR4_Full_BIT},
935 };
936 
937 static const struct hclge_link_mode_bmap hclge_cr_link_mode_bmap[] = {
938 	{HCLGE_SUPPORT_10G_BIT, ETHTOOL_LINK_MODE_10000baseCR_Full_BIT},
939 	{HCLGE_SUPPORT_25G_BIT, ETHTOOL_LINK_MODE_25000baseCR_Full_BIT},
940 	{HCLGE_SUPPORT_40G_BIT, ETHTOOL_LINK_MODE_40000baseCR4_Full_BIT},
941 	{HCLGE_SUPPORT_50G_R2_BIT, ETHTOOL_LINK_MODE_50000baseCR2_Full_BIT},
942 	{HCLGE_SUPPORT_50G_R1_BIT, ETHTOOL_LINK_MODE_50000baseCR_Full_BIT},
943 	{HCLGE_SUPPORT_100G_R4_BIT, ETHTOOL_LINK_MODE_100000baseCR4_Full_BIT},
944 	{HCLGE_SUPPORT_100G_R2_BIT, ETHTOOL_LINK_MODE_100000baseCR2_Full_BIT},
945 	{HCLGE_SUPPORT_200G_R4_EXT_BIT,
946 	 ETHTOOL_LINK_MODE_200000baseCR4_Full_BIT},
947 	{HCLGE_SUPPORT_200G_R4_BIT, ETHTOOL_LINK_MODE_200000baseCR4_Full_BIT},
948 };
949 
950 static const struct hclge_link_mode_bmap hclge_kr_link_mode_bmap[] = {
951 	{HCLGE_SUPPORT_1G_BIT, ETHTOOL_LINK_MODE_1000baseKX_Full_BIT},
952 	{HCLGE_SUPPORT_10G_BIT, ETHTOOL_LINK_MODE_10000baseKR_Full_BIT},
953 	{HCLGE_SUPPORT_25G_BIT, ETHTOOL_LINK_MODE_25000baseKR_Full_BIT},
954 	{HCLGE_SUPPORT_40G_BIT, ETHTOOL_LINK_MODE_40000baseKR4_Full_BIT},
955 	{HCLGE_SUPPORT_50G_R2_BIT, ETHTOOL_LINK_MODE_50000baseKR2_Full_BIT},
956 	{HCLGE_SUPPORT_50G_R1_BIT, ETHTOOL_LINK_MODE_50000baseKR_Full_BIT},
957 	{HCLGE_SUPPORT_100G_R4_BIT, ETHTOOL_LINK_MODE_100000baseKR4_Full_BIT},
958 	{HCLGE_SUPPORT_100G_R2_BIT, ETHTOOL_LINK_MODE_100000baseKR2_Full_BIT},
959 	{HCLGE_SUPPORT_200G_R4_EXT_BIT,
960 	 ETHTOOL_LINK_MODE_200000baseKR4_Full_BIT},
961 	{HCLGE_SUPPORT_200G_R4_BIT, ETHTOOL_LINK_MODE_200000baseKR4_Full_BIT},
962 };
963 
964 static void hclge_convert_setting_sr(u16 speed_ability,
965 				     unsigned long *link_mode)
966 {
967 	int i;
968 
969 	for (i = 0; i < ARRAY_SIZE(hclge_sr_link_mode_bmap); i++) {
970 		if (speed_ability & hclge_sr_link_mode_bmap[i].support_bit)
971 			linkmode_set_bit(hclge_sr_link_mode_bmap[i].link_mode,
972 					 link_mode);
973 	}
974 }
975 
976 static void hclge_convert_setting_lr(u16 speed_ability,
977 				     unsigned long *link_mode)
978 {
979 	int i;
980 
981 	for (i = 0; i < ARRAY_SIZE(hclge_lr_link_mode_bmap); i++) {
982 		if (speed_ability & hclge_lr_link_mode_bmap[i].support_bit)
983 			linkmode_set_bit(hclge_lr_link_mode_bmap[i].link_mode,
984 					 link_mode);
985 	}
986 }
987 
988 static void hclge_convert_setting_cr(u16 speed_ability,
989 				     unsigned long *link_mode)
990 {
991 	int i;
992 
993 	for (i = 0; i < ARRAY_SIZE(hclge_cr_link_mode_bmap); i++) {
994 		if (speed_ability & hclge_cr_link_mode_bmap[i].support_bit)
995 			linkmode_set_bit(hclge_cr_link_mode_bmap[i].link_mode,
996 					 link_mode);
997 	}
998 }
999 
1000 static void hclge_convert_setting_kr(u16 speed_ability,
1001 				     unsigned long *link_mode)
1002 {
1003 	int i;
1004 
1005 	for (i = 0; i < ARRAY_SIZE(hclge_kr_link_mode_bmap); i++) {
1006 		if (speed_ability & hclge_kr_link_mode_bmap[i].support_bit)
1007 			linkmode_set_bit(hclge_kr_link_mode_bmap[i].link_mode,
1008 					 link_mode);
1009 	}
1010 }
1011 
1012 static void hclge_convert_setting_fec(struct hclge_mac *mac)
1013 {
1014 	/* If firmware has reported fec_ability, don't need to convert by speed */
1015 	if (mac->fec_ability)
1016 		goto out;
1017 
1018 	switch (mac->speed) {
1019 	case HCLGE_MAC_SPEED_10G:
1020 	case HCLGE_MAC_SPEED_40G:
1021 		mac->fec_ability = BIT(HNAE3_FEC_BASER) | BIT(HNAE3_FEC_AUTO) |
1022 				   BIT(HNAE3_FEC_NONE);
1023 		break;
1024 	case HCLGE_MAC_SPEED_25G:
1025 	case HCLGE_MAC_SPEED_50G:
1026 		mac->fec_ability = BIT(HNAE3_FEC_BASER) | BIT(HNAE3_FEC_RS) |
1027 				   BIT(HNAE3_FEC_AUTO) | BIT(HNAE3_FEC_NONE);
1028 		break;
1029 	case HCLGE_MAC_SPEED_100G:
1030 		mac->fec_ability = BIT(HNAE3_FEC_RS) | BIT(HNAE3_FEC_AUTO) |
1031 				   BIT(HNAE3_FEC_NONE);
1032 		break;
1033 	case HCLGE_MAC_SPEED_200G:
1034 		mac->fec_ability = BIT(HNAE3_FEC_RS) | BIT(HNAE3_FEC_AUTO) |
1035 				   BIT(HNAE3_FEC_LLRS);
1036 		break;
1037 	default:
1038 		mac->fec_ability = 0;
1039 		break;
1040 	}
1041 
1042 out:
1043 	hclge_update_fec_support(mac);
1044 }
1045 
1046 static void hclge_parse_fiber_link_mode(struct hclge_dev *hdev,
1047 					u16 speed_ability)
1048 {
1049 	struct hclge_mac *mac = &hdev->hw.mac;
1050 
1051 	if (speed_ability & HCLGE_SUPPORT_1G_BIT)
1052 		linkmode_set_bit(ETHTOOL_LINK_MODE_1000baseX_Full_BIT,
1053 				 mac->supported);
1054 
1055 	hclge_convert_setting_sr(speed_ability, mac->supported);
1056 	hclge_convert_setting_lr(speed_ability, mac->supported);
1057 	hclge_convert_setting_cr(speed_ability, mac->supported);
1058 	if (hnae3_dev_fec_supported(hdev))
1059 		hclge_convert_setting_fec(mac);
1060 
1061 	if (hnae3_dev_pause_supported(hdev))
1062 		linkmode_set_bit(ETHTOOL_LINK_MODE_Pause_BIT, mac->supported);
1063 
1064 	linkmode_set_bit(ETHTOOL_LINK_MODE_FIBRE_BIT, mac->supported);
1065 	linkmode_set_bit(ETHTOOL_LINK_MODE_FEC_NONE_BIT, mac->supported);
1066 }
1067 
1068 static void hclge_parse_backplane_link_mode(struct hclge_dev *hdev,
1069 					    u16 speed_ability)
1070 {
1071 	struct hclge_mac *mac = &hdev->hw.mac;
1072 
1073 	hclge_convert_setting_kr(speed_ability, mac->supported);
1074 	if (hnae3_dev_fec_supported(hdev))
1075 		hclge_convert_setting_fec(mac);
1076 
1077 	if (hnae3_dev_pause_supported(hdev))
1078 		linkmode_set_bit(ETHTOOL_LINK_MODE_Pause_BIT, mac->supported);
1079 
1080 	linkmode_set_bit(ETHTOOL_LINK_MODE_Backplane_BIT, mac->supported);
1081 	linkmode_set_bit(ETHTOOL_LINK_MODE_FEC_NONE_BIT, mac->supported);
1082 }
1083 
1084 static void hclge_parse_copper_link_mode(struct hclge_dev *hdev,
1085 					 u16 speed_ability)
1086 {
1087 	unsigned long *supported = hdev->hw.mac.supported;
1088 
1089 	/* default to support all speed for GE port */
1090 	if (!speed_ability)
1091 		speed_ability = HCLGE_SUPPORT_GE;
1092 
1093 	if (speed_ability & HCLGE_SUPPORT_1G_BIT)
1094 		linkmode_set_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT,
1095 				 supported);
1096 
1097 	if (speed_ability & HCLGE_SUPPORT_100M_BIT) {
1098 		linkmode_set_bit(ETHTOOL_LINK_MODE_100baseT_Full_BIT,
1099 				 supported);
1100 		linkmode_set_bit(ETHTOOL_LINK_MODE_100baseT_Half_BIT,
1101 				 supported);
1102 	}
1103 
1104 	if (speed_ability & HCLGE_SUPPORT_10M_BIT) {
1105 		linkmode_set_bit(ETHTOOL_LINK_MODE_10baseT_Full_BIT, supported);
1106 		linkmode_set_bit(ETHTOOL_LINK_MODE_10baseT_Half_BIT, supported);
1107 	}
1108 
1109 	if (hnae3_dev_pause_supported(hdev)) {
1110 		linkmode_set_bit(ETHTOOL_LINK_MODE_Pause_BIT, supported);
1111 		linkmode_set_bit(ETHTOOL_LINK_MODE_Asym_Pause_BIT, supported);
1112 	}
1113 
1114 	linkmode_set_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, supported);
1115 	linkmode_set_bit(ETHTOOL_LINK_MODE_TP_BIT, supported);
1116 }
1117 
1118 static void hclge_parse_link_mode(struct hclge_dev *hdev, u16 speed_ability)
1119 {
1120 	u8 media_type = hdev->hw.mac.media_type;
1121 
1122 	if (media_type == HNAE3_MEDIA_TYPE_FIBER)
1123 		hclge_parse_fiber_link_mode(hdev, speed_ability);
1124 	else if (media_type == HNAE3_MEDIA_TYPE_COPPER)
1125 		hclge_parse_copper_link_mode(hdev, speed_ability);
1126 	else if (media_type == HNAE3_MEDIA_TYPE_BACKPLANE)
1127 		hclge_parse_backplane_link_mode(hdev, speed_ability);
1128 }
1129 
1130 static u32 hclge_get_max_speed(u16 speed_ability)
1131 {
1132 	if (speed_ability & HCLGE_SUPPORT_200G_BITS)
1133 		return HCLGE_MAC_SPEED_200G;
1134 
1135 	if (speed_ability & HCLGE_SUPPORT_100G_BITS)
1136 		return HCLGE_MAC_SPEED_100G;
1137 
1138 	if (speed_ability & HCLGE_SUPPORT_50G_BITS)
1139 		return HCLGE_MAC_SPEED_50G;
1140 
1141 	if (speed_ability & HCLGE_SUPPORT_40G_BIT)
1142 		return HCLGE_MAC_SPEED_40G;
1143 
1144 	if (speed_ability & HCLGE_SUPPORT_25G_BIT)
1145 		return HCLGE_MAC_SPEED_25G;
1146 
1147 	if (speed_ability & HCLGE_SUPPORT_10G_BIT)
1148 		return HCLGE_MAC_SPEED_10G;
1149 
1150 	if (speed_ability & HCLGE_SUPPORT_1G_BIT)
1151 		return HCLGE_MAC_SPEED_1G;
1152 
1153 	if (speed_ability & HCLGE_SUPPORT_100M_BIT)
1154 		return HCLGE_MAC_SPEED_100M;
1155 
1156 	if (speed_ability & HCLGE_SUPPORT_10M_BIT)
1157 		return HCLGE_MAC_SPEED_10M;
1158 
1159 	return HCLGE_MAC_SPEED_1G;
1160 }
1161 
1162 static void hclge_parse_cfg(struct hclge_cfg *cfg, struct hclge_desc *desc)
1163 {
1164 #define HCLGE_TX_SPARE_SIZE_UNIT		4096
1165 #define SPEED_ABILITY_EXT_SHIFT			8
1166 
1167 	struct hclge_cfg_param_cmd *req;
1168 	u64 mac_addr_tmp_high;
1169 	u16 speed_ability_ext;
1170 	u64 mac_addr_tmp;
1171 	unsigned int i;
1172 
1173 	req = (struct hclge_cfg_param_cmd *)desc[0].data;
1174 
1175 	/* get the configuration */
1176 	cfg->tc_num = hnae3_get_field(__le32_to_cpu(req->param[0]),
1177 				      HCLGE_CFG_TC_NUM_M, HCLGE_CFG_TC_NUM_S);
1178 	cfg->tqp_desc_num = hnae3_get_field(__le32_to_cpu(req->param[0]),
1179 					    HCLGE_CFG_TQP_DESC_N_M,
1180 					    HCLGE_CFG_TQP_DESC_N_S);
1181 
1182 	cfg->phy_addr = hnae3_get_field(__le32_to_cpu(req->param[1]),
1183 					HCLGE_CFG_PHY_ADDR_M,
1184 					HCLGE_CFG_PHY_ADDR_S);
1185 	cfg->media_type = hnae3_get_field(__le32_to_cpu(req->param[1]),
1186 					  HCLGE_CFG_MEDIA_TP_M,
1187 					  HCLGE_CFG_MEDIA_TP_S);
1188 	cfg->rx_buf_len = hnae3_get_field(__le32_to_cpu(req->param[1]),
1189 					  HCLGE_CFG_RX_BUF_LEN_M,
1190 					  HCLGE_CFG_RX_BUF_LEN_S);
1191 	/* get mac_address */
1192 	mac_addr_tmp = __le32_to_cpu(req->param[2]);
1193 	mac_addr_tmp_high = hnae3_get_field(__le32_to_cpu(req->param[3]),
1194 					    HCLGE_CFG_MAC_ADDR_H_M,
1195 					    HCLGE_CFG_MAC_ADDR_H_S);
1196 
1197 	mac_addr_tmp |= (mac_addr_tmp_high << 31) << 1;
1198 
1199 	cfg->default_speed = hnae3_get_field(__le32_to_cpu(req->param[3]),
1200 					     HCLGE_CFG_DEFAULT_SPEED_M,
1201 					     HCLGE_CFG_DEFAULT_SPEED_S);
1202 	cfg->vf_rss_size_max = hnae3_get_field(__le32_to_cpu(req->param[3]),
1203 					       HCLGE_CFG_RSS_SIZE_M,
1204 					       HCLGE_CFG_RSS_SIZE_S);
1205 
1206 	for (i = 0; i < ETH_ALEN; i++)
1207 		cfg->mac_addr[i] = (mac_addr_tmp >> (8 * i)) & 0xff;
1208 
1209 	req = (struct hclge_cfg_param_cmd *)desc[1].data;
1210 	cfg->numa_node_map = __le32_to_cpu(req->param[0]);
1211 
1212 	cfg->speed_ability = hnae3_get_field(__le32_to_cpu(req->param[1]),
1213 					     HCLGE_CFG_SPEED_ABILITY_M,
1214 					     HCLGE_CFG_SPEED_ABILITY_S);
1215 	speed_ability_ext = hnae3_get_field(__le32_to_cpu(req->param[1]),
1216 					    HCLGE_CFG_SPEED_ABILITY_EXT_M,
1217 					    HCLGE_CFG_SPEED_ABILITY_EXT_S);
1218 	cfg->speed_ability |= speed_ability_ext << SPEED_ABILITY_EXT_SHIFT;
1219 
1220 	cfg->vlan_fliter_cap = hnae3_get_field(__le32_to_cpu(req->param[1]),
1221 					       HCLGE_CFG_VLAN_FLTR_CAP_M,
1222 					       HCLGE_CFG_VLAN_FLTR_CAP_S);
1223 
1224 	cfg->umv_space = hnae3_get_field(__le32_to_cpu(req->param[1]),
1225 					 HCLGE_CFG_UMV_TBL_SPACE_M,
1226 					 HCLGE_CFG_UMV_TBL_SPACE_S);
1227 
1228 	cfg->pf_rss_size_max = hnae3_get_field(__le32_to_cpu(req->param[2]),
1229 					       HCLGE_CFG_PF_RSS_SIZE_M,
1230 					       HCLGE_CFG_PF_RSS_SIZE_S);
1231 
1232 	/* HCLGE_CFG_PF_RSS_SIZE_M is the PF max rss size, which is a
1233 	 * power of 2, instead of reading out directly. This would
1234 	 * be more flexible for future changes and expansions.
1235 	 * When VF max  rss size field is HCLGE_CFG_RSS_SIZE_S,
1236 	 * it does not make sense if PF's field is 0. In this case, PF and VF
1237 	 * has the same max rss size filed: HCLGE_CFG_RSS_SIZE_S.
1238 	 */
1239 	cfg->pf_rss_size_max = cfg->pf_rss_size_max ?
1240 			       1U << cfg->pf_rss_size_max :
1241 			       cfg->vf_rss_size_max;
1242 
1243 	/* The unit of the tx spare buffer size queried from configuration
1244 	 * file is HCLGE_TX_SPARE_SIZE_UNIT(4096) bytes, so a conversion is
1245 	 * needed here.
1246 	 */
1247 	cfg->tx_spare_buf_size = hnae3_get_field(__le32_to_cpu(req->param[2]),
1248 						 HCLGE_CFG_TX_SPARE_BUF_SIZE_M,
1249 						 HCLGE_CFG_TX_SPARE_BUF_SIZE_S);
1250 	cfg->tx_spare_buf_size *= HCLGE_TX_SPARE_SIZE_UNIT;
1251 }
1252 
1253 /* hclge_get_cfg: query the static parameter from flash
1254  * @hdev: pointer to struct hclge_dev
1255  * @hcfg: the config structure to be getted
1256  */
1257 static int hclge_get_cfg(struct hclge_dev *hdev, struct hclge_cfg *hcfg)
1258 {
1259 	struct hclge_desc desc[HCLGE_PF_CFG_DESC_NUM];
1260 	struct hclge_cfg_param_cmd *req;
1261 	unsigned int i;
1262 	int ret;
1263 
1264 	for (i = 0; i < HCLGE_PF_CFG_DESC_NUM; i++) {
1265 		u32 offset = 0;
1266 
1267 		req = (struct hclge_cfg_param_cmd *)desc[i].data;
1268 		hclge_cmd_setup_basic_desc(&desc[i], HCLGE_OPC_GET_CFG_PARAM,
1269 					   true);
1270 		hnae3_set_field(offset, HCLGE_CFG_OFFSET_M,
1271 				HCLGE_CFG_OFFSET_S, i * HCLGE_CFG_RD_LEN_BYTES);
1272 		/* Len should be united by 4 bytes when send to hardware */
1273 		hnae3_set_field(offset, HCLGE_CFG_RD_LEN_M, HCLGE_CFG_RD_LEN_S,
1274 				HCLGE_CFG_RD_LEN_BYTES / HCLGE_CFG_RD_LEN_UNIT);
1275 		req->offset = cpu_to_le32(offset);
1276 	}
1277 
1278 	ret = hclge_cmd_send(&hdev->hw, desc, HCLGE_PF_CFG_DESC_NUM);
1279 	if (ret) {
1280 		dev_err(&hdev->pdev->dev, "get config failed %d.\n", ret);
1281 		return ret;
1282 	}
1283 
1284 	hclge_parse_cfg(hcfg, desc);
1285 
1286 	return 0;
1287 }
1288 
1289 static void hclge_set_default_dev_specs(struct hclge_dev *hdev)
1290 {
1291 #define HCLGE_MAX_NON_TSO_BD_NUM			8U
1292 
1293 	struct hnae3_ae_dev *ae_dev = pci_get_drvdata(hdev->pdev);
1294 
1295 	ae_dev->dev_specs.max_non_tso_bd_num = HCLGE_MAX_NON_TSO_BD_NUM;
1296 	ae_dev->dev_specs.rss_ind_tbl_size = HCLGE_RSS_IND_TBL_SIZE;
1297 	ae_dev->dev_specs.rss_key_size = HCLGE_COMM_RSS_KEY_SIZE;
1298 	ae_dev->dev_specs.max_tm_rate = HCLGE_ETHER_MAX_RATE;
1299 	ae_dev->dev_specs.max_int_gl = HCLGE_DEF_MAX_INT_GL;
1300 	ae_dev->dev_specs.max_frm_size = HCLGE_MAC_MAX_FRAME;
1301 	ae_dev->dev_specs.max_qset_num = HCLGE_MAX_QSET_NUM;
1302 	ae_dev->dev_specs.umv_size = HCLGE_DEFAULT_UMV_SPACE_PER_PF;
1303 	ae_dev->dev_specs.tnl_num = 0;
1304 }
1305 
1306 static void hclge_parse_dev_specs(struct hclge_dev *hdev,
1307 				  struct hclge_desc *desc)
1308 {
1309 	struct hnae3_ae_dev *ae_dev = pci_get_drvdata(hdev->pdev);
1310 	struct hclge_dev_specs_0_cmd *req0;
1311 	struct hclge_dev_specs_1_cmd *req1;
1312 
1313 	req0 = (struct hclge_dev_specs_0_cmd *)desc[0].data;
1314 	req1 = (struct hclge_dev_specs_1_cmd *)desc[1].data;
1315 
1316 	ae_dev->dev_specs.max_non_tso_bd_num = req0->max_non_tso_bd_num;
1317 	ae_dev->dev_specs.rss_ind_tbl_size =
1318 		le16_to_cpu(req0->rss_ind_tbl_size);
1319 	ae_dev->dev_specs.int_ql_max = le16_to_cpu(req0->int_ql_max);
1320 	ae_dev->dev_specs.rss_key_size = le16_to_cpu(req0->rss_key_size);
1321 	ae_dev->dev_specs.max_tm_rate = le32_to_cpu(req0->max_tm_rate);
1322 	ae_dev->dev_specs.max_qset_num = le16_to_cpu(req1->max_qset_num);
1323 	ae_dev->dev_specs.max_int_gl = le16_to_cpu(req1->max_int_gl);
1324 	ae_dev->dev_specs.max_frm_size = le16_to_cpu(req1->max_frm_size);
1325 	ae_dev->dev_specs.umv_size = le16_to_cpu(req1->umv_size);
1326 	ae_dev->dev_specs.mc_mac_size = le16_to_cpu(req1->mc_mac_size);
1327 	ae_dev->dev_specs.tnl_num = req1->tnl_num;
1328 	ae_dev->dev_specs.hilink_version = req1->hilink_version;
1329 }
1330 
1331 static void hclge_check_dev_specs(struct hclge_dev *hdev)
1332 {
1333 	struct hnae3_dev_specs *dev_specs = &hdev->ae_dev->dev_specs;
1334 
1335 	if (!dev_specs->max_non_tso_bd_num)
1336 		dev_specs->max_non_tso_bd_num = HCLGE_MAX_NON_TSO_BD_NUM;
1337 	if (!dev_specs->rss_ind_tbl_size)
1338 		dev_specs->rss_ind_tbl_size = HCLGE_RSS_IND_TBL_SIZE;
1339 	if (!dev_specs->rss_key_size)
1340 		dev_specs->rss_key_size = HCLGE_COMM_RSS_KEY_SIZE;
1341 	if (!dev_specs->max_tm_rate)
1342 		dev_specs->max_tm_rate = HCLGE_ETHER_MAX_RATE;
1343 	if (!dev_specs->max_qset_num)
1344 		dev_specs->max_qset_num = HCLGE_MAX_QSET_NUM;
1345 	if (!dev_specs->max_int_gl)
1346 		dev_specs->max_int_gl = HCLGE_DEF_MAX_INT_GL;
1347 	if (!dev_specs->max_frm_size)
1348 		dev_specs->max_frm_size = HCLGE_MAC_MAX_FRAME;
1349 	if (!dev_specs->umv_size)
1350 		dev_specs->umv_size = HCLGE_DEFAULT_UMV_SPACE_PER_PF;
1351 }
1352 
1353 static int hclge_query_mac_stats_num(struct hclge_dev *hdev)
1354 {
1355 	u32 reg_num = 0;
1356 	int ret;
1357 
1358 	ret = hclge_mac_query_reg_num(hdev, &reg_num);
1359 	if (ret && ret != -EOPNOTSUPP)
1360 		return ret;
1361 
1362 	hdev->ae_dev->dev_specs.mac_stats_num = reg_num;
1363 	return 0;
1364 }
1365 
1366 static int hclge_query_dev_specs(struct hclge_dev *hdev)
1367 {
1368 	struct hclge_desc desc[HCLGE_QUERY_DEV_SPECS_BD_NUM];
1369 	int ret;
1370 	int i;
1371 
1372 	ret = hclge_query_mac_stats_num(hdev);
1373 	if (ret)
1374 		return ret;
1375 
1376 	/* set default specifications as devices lower than version V3 do not
1377 	 * support querying specifications from firmware.
1378 	 */
1379 	if (hdev->ae_dev->dev_version < HNAE3_DEVICE_VERSION_V3) {
1380 		hclge_set_default_dev_specs(hdev);
1381 		return 0;
1382 	}
1383 
1384 	for (i = 0; i < HCLGE_QUERY_DEV_SPECS_BD_NUM - 1; i++) {
1385 		hclge_cmd_setup_basic_desc(&desc[i], HCLGE_OPC_QUERY_DEV_SPECS,
1386 					   true);
1387 		desc[i].flag |= cpu_to_le16(HCLGE_COMM_CMD_FLAG_NEXT);
1388 	}
1389 	hclge_cmd_setup_basic_desc(&desc[i], HCLGE_OPC_QUERY_DEV_SPECS, true);
1390 
1391 	ret = hclge_cmd_send(&hdev->hw, desc, HCLGE_QUERY_DEV_SPECS_BD_NUM);
1392 	if (ret)
1393 		return ret;
1394 
1395 	hclge_parse_dev_specs(hdev, desc);
1396 	hclge_check_dev_specs(hdev);
1397 
1398 	return 0;
1399 }
1400 
1401 static int hclge_get_cap(struct hclge_dev *hdev)
1402 {
1403 	int ret;
1404 
1405 	ret = hclge_query_function_status(hdev);
1406 	if (ret) {
1407 		dev_err(&hdev->pdev->dev,
1408 			"query function status error %d.\n", ret);
1409 		return ret;
1410 	}
1411 
1412 	/* get pf resource */
1413 	return hclge_query_pf_resource(hdev);
1414 }
1415 
1416 static void hclge_init_kdump_kernel_config(struct hclge_dev *hdev)
1417 {
1418 #define HCLGE_MIN_TX_DESC	64
1419 #define HCLGE_MIN_RX_DESC	64
1420 
1421 	if (!is_kdump_kernel())
1422 		return;
1423 
1424 	dev_info(&hdev->pdev->dev,
1425 		 "Running kdump kernel. Using minimal resources\n");
1426 
1427 	/* minimal queue pairs equals to the number of vports */
1428 	hdev->num_tqps = hdev->num_req_vfs + 1;
1429 	hdev->num_tx_desc = HCLGE_MIN_TX_DESC;
1430 	hdev->num_rx_desc = HCLGE_MIN_RX_DESC;
1431 }
1432 
1433 static void hclge_init_tc_config(struct hclge_dev *hdev)
1434 {
1435 	unsigned int i;
1436 
1437 	if (hdev->tc_max > HNAE3_MAX_TC ||
1438 	    hdev->tc_max < 1) {
1439 		dev_warn(&hdev->pdev->dev, "TC num = %u.\n",
1440 			 hdev->tc_max);
1441 		hdev->tc_max = 1;
1442 	}
1443 
1444 	/* Dev does not support DCB */
1445 	if (!hnae3_dev_dcb_supported(hdev)) {
1446 		hdev->tc_max = 1;
1447 		hdev->pfc_max = 0;
1448 	} else {
1449 		hdev->pfc_max = hdev->tc_max;
1450 	}
1451 
1452 	hdev->tm_info.num_tc = 1;
1453 
1454 	/* Currently not support uncontiuous tc */
1455 	for (i = 0; i < hdev->tm_info.num_tc; i++)
1456 		hnae3_set_bit(hdev->hw_tc_map, i, 1);
1457 
1458 	hdev->tx_sch_mode = HCLGE_FLAG_TC_BASE_SCH_MODE;
1459 }
1460 
1461 static int hclge_configure(struct hclge_dev *hdev)
1462 {
1463 	struct hnae3_ae_dev *ae_dev = pci_get_drvdata(hdev->pdev);
1464 	struct hclge_cfg cfg;
1465 	int ret;
1466 
1467 	ret = hclge_get_cfg(hdev, &cfg);
1468 	if (ret)
1469 		return ret;
1470 
1471 	hdev->base_tqp_pid = 0;
1472 	hdev->vf_rss_size_max = cfg.vf_rss_size_max;
1473 	hdev->pf_rss_size_max = cfg.pf_rss_size_max;
1474 	hdev->rx_buf_len = cfg.rx_buf_len;
1475 	ether_addr_copy(hdev->hw.mac.mac_addr, cfg.mac_addr);
1476 	hdev->hw.mac.media_type = cfg.media_type;
1477 	hdev->hw.mac.phy_addr = cfg.phy_addr;
1478 	hdev->num_tx_desc = cfg.tqp_desc_num;
1479 	hdev->num_rx_desc = cfg.tqp_desc_num;
1480 	hdev->tm_info.num_pg = 1;
1481 	hdev->tc_max = cfg.tc_num;
1482 	hdev->tm_info.hw_pfc_map = 0;
1483 	if (cfg.umv_space)
1484 		hdev->wanted_umv_size = cfg.umv_space;
1485 	else
1486 		hdev->wanted_umv_size = hdev->ae_dev->dev_specs.umv_size;
1487 	hdev->tx_spare_buf_size = cfg.tx_spare_buf_size;
1488 	hdev->gro_en = true;
1489 	if (cfg.vlan_fliter_cap == HCLGE_VLAN_FLTR_CAN_MDF)
1490 		set_bit(HNAE3_DEV_SUPPORT_VLAN_FLTR_MDF_B, ae_dev->caps);
1491 
1492 	if (hnae3_ae_dev_fd_supported(hdev->ae_dev)) {
1493 		hdev->fd_en = true;
1494 		hdev->fd_active_type = HCLGE_FD_RULE_NONE;
1495 	}
1496 
1497 	ret = hclge_parse_speed(cfg.default_speed, &hdev->hw.mac.speed);
1498 	if (ret) {
1499 		dev_err(&hdev->pdev->dev, "failed to parse speed %u, ret = %d\n",
1500 			cfg.default_speed, ret);
1501 		return ret;
1502 	}
1503 	hdev->hw.mac.req_speed = hdev->hw.mac.speed;
1504 	hdev->hw.mac.req_autoneg = AUTONEG_ENABLE;
1505 	hdev->hw.mac.req_duplex = DUPLEX_FULL;
1506 
1507 	/* When lane_num is 0, the firmware will automatically
1508 	 * select the appropriate lane_num based on the speed.
1509 	 */
1510 	hdev->hw.mac.req_lane_num = 0;
1511 
1512 	hclge_parse_link_mode(hdev, cfg.speed_ability);
1513 
1514 	hdev->hw.mac.max_speed = hclge_get_max_speed(cfg.speed_ability);
1515 
1516 	hclge_init_tc_config(hdev);
1517 	hclge_init_kdump_kernel_config(hdev);
1518 
1519 	return ret;
1520 }
1521 
1522 static int hclge_config_tso(struct hclge_dev *hdev, u16 tso_mss_min,
1523 			    u16 tso_mss_max)
1524 {
1525 	struct hclge_cfg_tso_status_cmd *req;
1526 	struct hclge_desc desc;
1527 
1528 	hclge_cmd_setup_basic_desc(&desc, HCLGE_OPC_TSO_GENERIC_CONFIG, false);
1529 
1530 	req = (struct hclge_cfg_tso_status_cmd *)desc.data;
1531 	req->tso_mss_min = cpu_to_le16(tso_mss_min);
1532 	req->tso_mss_max = cpu_to_le16(tso_mss_max);
1533 
1534 	return hclge_cmd_send(&hdev->hw, &desc, 1);
1535 }
1536 
1537 static int hclge_config_gro(struct hclge_dev *hdev)
1538 {
1539 	struct hclge_cfg_gro_status_cmd *req;
1540 	struct hclge_desc desc;
1541 	int ret;
1542 
1543 	if (!hnae3_ae_dev_gro_supported(hdev->ae_dev))
1544 		return 0;
1545 
1546 	hclge_cmd_setup_basic_desc(&desc, HCLGE_OPC_GRO_GENERIC_CONFIG, false);
1547 	req = (struct hclge_cfg_gro_status_cmd *)desc.data;
1548 
1549 	req->gro_en = hdev->gro_en ? 1 : 0;
1550 
1551 	ret = hclge_cmd_send(&hdev->hw, &desc, 1);
1552 	if (ret)
1553 		dev_err(&hdev->pdev->dev,
1554 			"GRO hardware config cmd failed, ret = %d\n", ret);
1555 
1556 	return ret;
1557 }
1558 
1559 static int hclge_alloc_tqps(struct hclge_dev *hdev)
1560 {
1561 	struct hnae3_ae_dev *ae_dev = pci_get_drvdata(hdev->pdev);
1562 	struct hclge_comm_tqp *tqp;
1563 	int i;
1564 
1565 	hdev->htqp = devm_kzalloc_objs(&hdev->pdev->dev, struct hclge_comm_tqp,
1566 				       hdev->num_tqps);
1567 	if (!hdev->htqp)
1568 		return -ENOMEM;
1569 
1570 	tqp = hdev->htqp;
1571 
1572 	for (i = 0; i < hdev->num_tqps; i++) {
1573 		tqp->dev = &hdev->pdev->dev;
1574 		tqp->index = i;
1575 
1576 		tqp->q.ae_algo = &ae_algo;
1577 		tqp->q.buf_size = hdev->rx_buf_len;
1578 		tqp->q.tx_desc_num = hdev->num_tx_desc;
1579 		tqp->q.rx_desc_num = hdev->num_rx_desc;
1580 
1581 		/* need an extended offset to configure queues >=
1582 		 * HCLGE_TQP_MAX_SIZE_DEV_V2
1583 		 */
1584 		if (i < HCLGE_TQP_MAX_SIZE_DEV_V2)
1585 			tqp->q.io_base = hdev->hw.hw.io_base +
1586 					 HCLGE_TQP_REG_OFFSET +
1587 					 i * HCLGE_TQP_REG_SIZE;
1588 		else
1589 			tqp->q.io_base = hdev->hw.hw.io_base +
1590 					 HCLGE_TQP_REG_OFFSET +
1591 					 HCLGE_TQP_EXT_REG_OFFSET +
1592 					 (i - HCLGE_TQP_MAX_SIZE_DEV_V2) *
1593 					 HCLGE_TQP_REG_SIZE;
1594 
1595 		/* when device supports tx push and has device memory,
1596 		 * the queue can execute push mode or doorbell mode on
1597 		 * device memory.
1598 		 */
1599 		if (test_bit(HNAE3_DEV_SUPPORT_TX_PUSH_B, ae_dev->caps))
1600 			tqp->q.mem_base = hdev->hw.hw.mem_base +
1601 					  HCLGE_TQP_MEM_OFFSET(hdev, i);
1602 
1603 		tqp++;
1604 	}
1605 
1606 	return 0;
1607 }
1608 
1609 static int hclge_map_tqps_to_func(struct hclge_dev *hdev, u16 func_id,
1610 				  u16 tqp_pid, u16 tqp_vid, bool is_pf)
1611 {
1612 	struct hclge_tqp_map_cmd *req;
1613 	struct hclge_desc desc;
1614 	int ret;
1615 
1616 	hclge_cmd_setup_basic_desc(&desc, HCLGE_OPC_SET_TQP_MAP, false);
1617 
1618 	req = (struct hclge_tqp_map_cmd *)desc.data;
1619 	req->tqp_id = cpu_to_le16(tqp_pid);
1620 	req->tqp_vf = func_id;
1621 	req->tqp_flag = 1U << HCLGE_TQP_MAP_EN_B;
1622 	if (!is_pf)
1623 		req->tqp_flag |= 1U << HCLGE_TQP_MAP_TYPE_B;
1624 	req->tqp_vid = cpu_to_le16(tqp_vid);
1625 
1626 	ret = hclge_cmd_send(&hdev->hw, &desc, 1);
1627 	if (ret)
1628 		dev_err(&hdev->pdev->dev, "TQP map failed %d.\n", ret);
1629 
1630 	return ret;
1631 }
1632 
1633 static int  hclge_assign_tqp(struct hclge_vport *vport, u16 num_tqps)
1634 {
1635 	struct hnae3_knic_private_info *kinfo = &vport->nic.kinfo;
1636 	struct hclge_dev *hdev = vport->back;
1637 	int i, alloced;
1638 
1639 	for (i = 0, alloced = 0; i < hdev->num_tqps &&
1640 	     alloced < num_tqps; i++) {
1641 		if (!hdev->htqp[i].alloced) {
1642 			hdev->htqp[i].q.handle = &vport->nic;
1643 			hdev->htqp[i].q.tqp_index = alloced;
1644 			hdev->htqp[i].q.tx_desc_num = kinfo->num_tx_desc;
1645 			hdev->htqp[i].q.rx_desc_num = kinfo->num_rx_desc;
1646 			kinfo->tqp[alloced] = &hdev->htqp[i].q;
1647 			hdev->htqp[i].alloced = true;
1648 			alloced++;
1649 		}
1650 	}
1651 	vport->alloc_tqps = alloced;
1652 	kinfo->rss_size = min_t(u16, hdev->pf_rss_size_max,
1653 				vport->alloc_tqps / hdev->tm_info.num_tc);
1654 
1655 	/* ensure one to one mapping between irq and queue at default */
1656 	kinfo->rss_size = min_t(u16, kinfo->rss_size,
1657 				(hdev->num_nic_msi - 1) / hdev->tm_info.num_tc);
1658 
1659 	return 0;
1660 }
1661 
1662 static int hclge_knic_setup(struct hclge_vport *vport, u16 num_tqps,
1663 			    u16 num_tx_desc, u16 num_rx_desc)
1664 
1665 {
1666 	struct hnae3_handle *nic = &vport->nic;
1667 	struct hnae3_knic_private_info *kinfo = &nic->kinfo;
1668 	struct hclge_dev *hdev = vport->back;
1669 	int ret;
1670 
1671 	kinfo->num_tx_desc = num_tx_desc;
1672 	kinfo->num_rx_desc = num_rx_desc;
1673 
1674 	kinfo->rx_buf_len = hdev->rx_buf_len;
1675 	kinfo->tx_spare_buf_size = hdev->tx_spare_buf_size;
1676 
1677 	kinfo->tqp = devm_kzalloc_objs(&hdev->pdev->dev, struct hnae3_queue *,
1678 				       num_tqps);
1679 	if (!kinfo->tqp)
1680 		return -ENOMEM;
1681 
1682 	ret = hclge_assign_tqp(vport, num_tqps);
1683 	if (ret)
1684 		dev_err(&hdev->pdev->dev, "fail to assign TQPs %d.\n", ret);
1685 
1686 	return ret;
1687 }
1688 
1689 static int hclge_map_tqp_to_vport(struct hclge_dev *hdev,
1690 				  struct hclge_vport *vport)
1691 {
1692 	struct hnae3_handle *nic = &vport->nic;
1693 	struct hnae3_knic_private_info *kinfo;
1694 	u16 i;
1695 
1696 	kinfo = &nic->kinfo;
1697 	for (i = 0; i < vport->alloc_tqps; i++) {
1698 		struct hclge_comm_tqp *q =
1699 			container_of(kinfo->tqp[i], struct hclge_comm_tqp, q);
1700 		bool is_pf;
1701 		int ret;
1702 
1703 		is_pf = !(vport->vport_id);
1704 		ret = hclge_map_tqps_to_func(hdev, vport->vport_id, q->index,
1705 					     i, is_pf);
1706 		if (ret)
1707 			return ret;
1708 	}
1709 
1710 	return 0;
1711 }
1712 
1713 static int hclge_map_tqp(struct hclge_dev *hdev)
1714 {
1715 	struct hclge_vport *vport = hdev->vport;
1716 	u16 i, num_vport;
1717 
1718 	num_vport = hdev->num_req_vfs + 1;
1719 	for (i = 0; i < num_vport; i++) {
1720 		int ret;
1721 
1722 		ret = hclge_map_tqp_to_vport(hdev, vport);
1723 		if (ret)
1724 			return ret;
1725 
1726 		vport++;
1727 	}
1728 
1729 	return 0;
1730 }
1731 
1732 static int hclge_vport_setup(struct hclge_vport *vport, u16 num_tqps)
1733 {
1734 	struct hnae3_handle *nic = &vport->nic;
1735 	struct hclge_dev *hdev = vport->back;
1736 	int ret;
1737 
1738 	nic->pdev = hdev->pdev;
1739 	nic->ae_algo = &ae_algo;
1740 	bitmap_copy(nic->numa_node_mask.bits, hdev->numa_node_mask.bits,
1741 		    MAX_NUMNODES);
1742 	nic->kinfo.io_base = hdev->hw.hw.io_base;
1743 
1744 	ret = hclge_knic_setup(vport, num_tqps,
1745 			       hdev->num_tx_desc, hdev->num_rx_desc);
1746 	if (ret)
1747 		dev_err(&hdev->pdev->dev, "knic setup failed %d\n", ret);
1748 
1749 	return ret;
1750 }
1751 
1752 static int hclge_alloc_vport(struct hclge_dev *hdev)
1753 {
1754 	struct pci_dev *pdev = hdev->pdev;
1755 	struct hclge_vport *vport;
1756 	u32 tqp_main_vport;
1757 	u32 tqp_per_vport;
1758 	int num_vport, i;
1759 	int ret;
1760 
1761 	/* We need to alloc a vport for main NIC of PF */
1762 	num_vport = hdev->num_req_vfs + 1;
1763 
1764 	if (hdev->num_tqps < num_vport) {
1765 		dev_err(&hdev->pdev->dev, "tqps(%u) is less than vports(%d)",
1766 			hdev->num_tqps, num_vport);
1767 		return -EINVAL;
1768 	}
1769 
1770 	/* Alloc the same number of TQPs for every vport */
1771 	tqp_per_vport = hdev->num_tqps / num_vport;
1772 	tqp_main_vport = tqp_per_vport + hdev->num_tqps % num_vport;
1773 
1774 	vport = devm_kzalloc_objs(&pdev->dev, struct hclge_vport, num_vport);
1775 	if (!vport)
1776 		return -ENOMEM;
1777 
1778 	hdev->vport = vport;
1779 	hdev->num_alloc_vport = num_vport;
1780 
1781 	if (IS_ENABLED(CONFIG_PCI_IOV))
1782 		hdev->num_alloc_vfs = hdev->num_req_vfs;
1783 
1784 	for (i = 0; i < num_vport; i++) {
1785 		vport->back = hdev;
1786 		vport->vport_id = i;
1787 		vport->vf_info.link_state = IFLA_VF_LINK_STATE_AUTO;
1788 		vport->mps = HCLGE_MAC_DEFAULT_FRAME;
1789 		vport->port_base_vlan_cfg.state = HNAE3_PORT_BASE_VLAN_DISABLE;
1790 		vport->port_base_vlan_cfg.tbl_sta = true;
1791 		vport->rxvlan_cfg.rx_vlan_offload_en = true;
1792 		vport->req_vlan_fltr_en = true;
1793 		INIT_LIST_HEAD(&vport->vlan_list);
1794 		INIT_LIST_HEAD(&vport->uc_mac_list);
1795 		INIT_LIST_HEAD(&vport->mc_mac_list);
1796 		spin_lock_init(&vport->mac_list_lock);
1797 
1798 		if (i == 0)
1799 			ret = hclge_vport_setup(vport, tqp_main_vport);
1800 		else
1801 			ret = hclge_vport_setup(vport, tqp_per_vport);
1802 		if (ret) {
1803 			dev_err(&pdev->dev,
1804 				"vport setup failed for vport %d, %d\n",
1805 				i, ret);
1806 			return ret;
1807 		}
1808 
1809 		vport++;
1810 	}
1811 
1812 	return 0;
1813 }
1814 
1815 static int  hclge_cmd_alloc_tx_buff(struct hclge_dev *hdev,
1816 				    struct hclge_pkt_buf_alloc *buf_alloc)
1817 {
1818 /* TX buffer size is unit by 128 byte */
1819 #define HCLGE_BUF_SIZE_UNIT_SHIFT	7
1820 #define HCLGE_BUF_SIZE_UPDATE_EN_MSK	BIT(15)
1821 	struct hclge_tx_buff_alloc_cmd *req;
1822 	struct hclge_desc desc;
1823 	int ret;
1824 	u8 i;
1825 
1826 	req = (struct hclge_tx_buff_alloc_cmd *)desc.data;
1827 
1828 	hclge_cmd_setup_basic_desc(&desc, HCLGE_OPC_TX_BUFF_ALLOC, 0);
1829 	for (i = 0; i < HCLGE_MAX_TC_NUM; i++) {
1830 		u32 buf_size = buf_alloc->priv_buf[i].tx_buf_size;
1831 
1832 		req->tx_pkt_buff[i] =
1833 			cpu_to_le16((buf_size >> HCLGE_BUF_SIZE_UNIT_SHIFT) |
1834 				     HCLGE_BUF_SIZE_UPDATE_EN_MSK);
1835 	}
1836 
1837 	ret = hclge_cmd_send(&hdev->hw, &desc, 1);
1838 	if (ret)
1839 		dev_err(&hdev->pdev->dev, "tx buffer alloc cmd failed %d.\n",
1840 			ret);
1841 
1842 	return ret;
1843 }
1844 
1845 static int hclge_tx_buffer_alloc(struct hclge_dev *hdev,
1846 				 struct hclge_pkt_buf_alloc *buf_alloc)
1847 {
1848 	int ret = hclge_cmd_alloc_tx_buff(hdev, buf_alloc);
1849 
1850 	if (ret)
1851 		dev_err(&hdev->pdev->dev, "tx buffer alloc failed %d\n", ret);
1852 
1853 	return ret;
1854 }
1855 
1856 static u32 hclge_get_tc_num(struct hclge_dev *hdev)
1857 {
1858 	unsigned int i;
1859 	u32 cnt = 0;
1860 
1861 	for (i = 0; i < HCLGE_MAX_TC_NUM; i++)
1862 		if (hdev->hw_tc_map & BIT(i))
1863 			cnt++;
1864 	return cnt;
1865 }
1866 
1867 /* Get the number of pfc enabled TCs, which have private buffer */
1868 static int hclge_get_pfc_priv_num(struct hclge_dev *hdev,
1869 				  struct hclge_pkt_buf_alloc *buf_alloc)
1870 {
1871 	struct hclge_priv_buf *priv;
1872 	unsigned int i;
1873 	int cnt = 0;
1874 
1875 	for (i = 0; i < HCLGE_MAX_TC_NUM; i++) {
1876 		priv = &buf_alloc->priv_buf[i];
1877 		if ((hdev->tm_info.hw_pfc_map & BIT(i)) &&
1878 		    priv->enable)
1879 			cnt++;
1880 	}
1881 
1882 	return cnt;
1883 }
1884 
1885 /* Get the number of pfc disabled TCs, which have private buffer */
1886 static int hclge_get_no_pfc_priv_num(struct hclge_dev *hdev,
1887 				     struct hclge_pkt_buf_alloc *buf_alloc)
1888 {
1889 	struct hclge_priv_buf *priv;
1890 	unsigned int i;
1891 	int cnt = 0;
1892 
1893 	for (i = 0; i < HCLGE_MAX_TC_NUM; i++) {
1894 		priv = &buf_alloc->priv_buf[i];
1895 		if (hdev->hw_tc_map & BIT(i) &&
1896 		    !(hdev->tm_info.hw_pfc_map & BIT(i)) &&
1897 		    priv->enable)
1898 			cnt++;
1899 	}
1900 
1901 	return cnt;
1902 }
1903 
1904 static u32 hclge_get_rx_priv_buff_alloced(struct hclge_pkt_buf_alloc *buf_alloc)
1905 {
1906 	struct hclge_priv_buf *priv;
1907 	u32 rx_priv = 0;
1908 	int i;
1909 
1910 	for (i = 0; i < HCLGE_MAX_TC_NUM; i++) {
1911 		priv = &buf_alloc->priv_buf[i];
1912 		if (priv->enable)
1913 			rx_priv += priv->buf_size;
1914 	}
1915 	return rx_priv;
1916 }
1917 
1918 static u32 hclge_get_tx_buff_alloced(struct hclge_pkt_buf_alloc *buf_alloc)
1919 {
1920 	u32 i, total_tx_size = 0;
1921 
1922 	for (i = 0; i < HCLGE_MAX_TC_NUM; i++)
1923 		total_tx_size += buf_alloc->priv_buf[i].tx_buf_size;
1924 
1925 	return total_tx_size;
1926 }
1927 
1928 static bool  hclge_is_rx_buf_ok(struct hclge_dev *hdev,
1929 				struct hclge_pkt_buf_alloc *buf_alloc,
1930 				u32 rx_all)
1931 {
1932 	u32 shared_buf_min, shared_buf_tc, shared_std, hi_thrd, lo_thrd;
1933 	u32 tc_num = hclge_get_tc_num(hdev);
1934 	u32 shared_buf, aligned_mps;
1935 	u32 rx_priv;
1936 	int i;
1937 
1938 	aligned_mps = roundup(hdev->mps, HCLGE_BUF_SIZE_UNIT);
1939 
1940 	if (hnae3_dev_dcb_supported(hdev))
1941 		shared_buf_min = HCLGE_BUF_MUL_BY * aligned_mps +
1942 					hdev->dv_buf_size;
1943 	else
1944 		shared_buf_min = aligned_mps + HCLGE_NON_DCB_ADDITIONAL_BUF
1945 					+ hdev->dv_buf_size;
1946 
1947 	shared_buf_tc = tc_num * aligned_mps + aligned_mps;
1948 	shared_std = roundup(max_t(u32, shared_buf_min, shared_buf_tc),
1949 			     HCLGE_BUF_SIZE_UNIT);
1950 
1951 	rx_priv = hclge_get_rx_priv_buff_alloced(buf_alloc);
1952 	if (rx_all < rx_priv + shared_std)
1953 		return false;
1954 
1955 	shared_buf = rounddown(rx_all - rx_priv, HCLGE_BUF_SIZE_UNIT);
1956 	buf_alloc->s_buf.buf_size = shared_buf;
1957 	if (hnae3_dev_dcb_supported(hdev)) {
1958 		buf_alloc->s_buf.self.high = shared_buf - hdev->dv_buf_size;
1959 		buf_alloc->s_buf.self.low = buf_alloc->s_buf.self.high
1960 			- roundup(aligned_mps / HCLGE_BUF_DIV_BY,
1961 				  HCLGE_BUF_SIZE_UNIT);
1962 	} else {
1963 		buf_alloc->s_buf.self.high = aligned_mps +
1964 						HCLGE_NON_DCB_ADDITIONAL_BUF;
1965 		buf_alloc->s_buf.self.low = aligned_mps;
1966 	}
1967 
1968 	if (hnae3_dev_dcb_supported(hdev)) {
1969 		hi_thrd = shared_buf - hdev->dv_buf_size;
1970 
1971 		if (tc_num <= NEED_RESERVE_TC_NUM)
1972 			hi_thrd = hi_thrd * BUF_RESERVE_PERCENT
1973 					/ BUF_MAX_PERCENT;
1974 
1975 		if (tc_num)
1976 			hi_thrd = hi_thrd / tc_num;
1977 
1978 		hi_thrd = max_t(u32, hi_thrd, HCLGE_BUF_MUL_BY * aligned_mps);
1979 		hi_thrd = rounddown(hi_thrd, HCLGE_BUF_SIZE_UNIT);
1980 		lo_thrd = hi_thrd - aligned_mps / HCLGE_BUF_DIV_BY;
1981 	} else {
1982 		hi_thrd = aligned_mps + HCLGE_NON_DCB_ADDITIONAL_BUF;
1983 		lo_thrd = aligned_mps;
1984 	}
1985 
1986 	for (i = 0; i < HCLGE_MAX_TC_NUM; i++) {
1987 		buf_alloc->s_buf.tc_thrd[i].low = lo_thrd;
1988 		buf_alloc->s_buf.tc_thrd[i].high = hi_thrd;
1989 	}
1990 
1991 	return true;
1992 }
1993 
1994 static int hclge_tx_buffer_calc(struct hclge_dev *hdev,
1995 				struct hclge_pkt_buf_alloc *buf_alloc)
1996 {
1997 	u32 i, total_size;
1998 
1999 	total_size = hdev->pkt_buf_size;
2000 
2001 	/* alloc tx buffer for all enabled tc */
2002 	for (i = 0; i < HCLGE_MAX_TC_NUM; i++) {
2003 		struct hclge_priv_buf *priv = &buf_alloc->priv_buf[i];
2004 
2005 		if (hdev->hw_tc_map & BIT(i)) {
2006 			if (total_size < hdev->tx_buf_size)
2007 				return -ENOMEM;
2008 
2009 			priv->tx_buf_size = hdev->tx_buf_size;
2010 		} else {
2011 			priv->tx_buf_size = 0;
2012 		}
2013 
2014 		total_size -= priv->tx_buf_size;
2015 	}
2016 
2017 	return 0;
2018 }
2019 
2020 static bool hclge_rx_buf_calc_all(struct hclge_dev *hdev, bool max,
2021 				  struct hclge_pkt_buf_alloc *buf_alloc)
2022 {
2023 	u32 rx_all = hdev->pkt_buf_size - hclge_get_tx_buff_alloced(buf_alloc);
2024 	u32 aligned_mps = round_up(hdev->mps, HCLGE_BUF_SIZE_UNIT);
2025 	unsigned int i;
2026 
2027 	for (i = 0; i < HCLGE_MAX_TC_NUM; i++) {
2028 		struct hclge_priv_buf *priv = &buf_alloc->priv_buf[i];
2029 
2030 		priv->enable = 0;
2031 		priv->wl.low = 0;
2032 		priv->wl.high = 0;
2033 		priv->buf_size = 0;
2034 
2035 		if (!(hdev->hw_tc_map & BIT(i)))
2036 			continue;
2037 
2038 		priv->enable = 1;
2039 
2040 		if (hdev->tm_info.hw_pfc_map & BIT(i)) {
2041 			priv->wl.low = max ? aligned_mps : HCLGE_BUF_SIZE_UNIT;
2042 			priv->wl.high = roundup(priv->wl.low + aligned_mps,
2043 						HCLGE_BUF_SIZE_UNIT);
2044 		} else {
2045 			priv->wl.low = 0;
2046 			priv->wl.high = max ? (aligned_mps * HCLGE_BUF_MUL_BY) :
2047 					aligned_mps;
2048 		}
2049 
2050 		priv->buf_size = priv->wl.high + hdev->dv_buf_size;
2051 	}
2052 
2053 	return hclge_is_rx_buf_ok(hdev, buf_alloc, rx_all);
2054 }
2055 
2056 static bool hclge_drop_nopfc_buf_till_fit(struct hclge_dev *hdev,
2057 					  struct hclge_pkt_buf_alloc *buf_alloc)
2058 {
2059 	u32 rx_all = hdev->pkt_buf_size - hclge_get_tx_buff_alloced(buf_alloc);
2060 	int no_pfc_priv_num = hclge_get_no_pfc_priv_num(hdev, buf_alloc);
2061 	int i;
2062 
2063 	/* let the last to be cleared first */
2064 	for (i = HCLGE_MAX_TC_NUM - 1; i >= 0; i--) {
2065 		struct hclge_priv_buf *priv = &buf_alloc->priv_buf[i];
2066 		unsigned int mask = BIT((unsigned int)i);
2067 
2068 		if (hdev->hw_tc_map & mask &&
2069 		    !(hdev->tm_info.hw_pfc_map & mask)) {
2070 			/* Clear the no pfc TC private buffer */
2071 			priv->wl.low = 0;
2072 			priv->wl.high = 0;
2073 			priv->buf_size = 0;
2074 			priv->enable = 0;
2075 			no_pfc_priv_num--;
2076 		}
2077 
2078 		if (hclge_is_rx_buf_ok(hdev, buf_alloc, rx_all) ||
2079 		    no_pfc_priv_num == 0)
2080 			break;
2081 	}
2082 
2083 	return hclge_is_rx_buf_ok(hdev, buf_alloc, rx_all);
2084 }
2085 
2086 static bool hclge_drop_pfc_buf_till_fit(struct hclge_dev *hdev,
2087 					struct hclge_pkt_buf_alloc *buf_alloc)
2088 {
2089 	u32 rx_all = hdev->pkt_buf_size - hclge_get_tx_buff_alloced(buf_alloc);
2090 	int pfc_priv_num = hclge_get_pfc_priv_num(hdev, buf_alloc);
2091 	int i;
2092 
2093 	/* let the last to be cleared first */
2094 	for (i = HCLGE_MAX_TC_NUM - 1; i >= 0; i--) {
2095 		struct hclge_priv_buf *priv = &buf_alloc->priv_buf[i];
2096 		unsigned int mask = BIT((unsigned int)i);
2097 
2098 		if (hdev->hw_tc_map & mask &&
2099 		    hdev->tm_info.hw_pfc_map & mask) {
2100 			/* Reduce the number of pfc TC with private buffer */
2101 			priv->wl.low = 0;
2102 			priv->enable = 0;
2103 			priv->wl.high = 0;
2104 			priv->buf_size = 0;
2105 			pfc_priv_num--;
2106 		}
2107 
2108 		if (hclge_is_rx_buf_ok(hdev, buf_alloc, rx_all) ||
2109 		    pfc_priv_num == 0)
2110 			break;
2111 	}
2112 
2113 	return hclge_is_rx_buf_ok(hdev, buf_alloc, rx_all);
2114 }
2115 
2116 static bool hclge_only_alloc_priv_buff(struct hclge_dev *hdev,
2117 				       struct hclge_pkt_buf_alloc *buf_alloc)
2118 {
2119 #define COMPENSATE_BUFFER	0x3C00
2120 #define COMPENSATE_HALF_MPS_NUM	5
2121 #define PRIV_WL_GAP		0x1800
2122 
2123 	u32 rx_priv = hdev->pkt_buf_size - hclge_get_tx_buff_alloced(buf_alloc);
2124 	u32 tc_num = hclge_get_tc_num(hdev);
2125 	u32 half_mps = hdev->mps >> 1;
2126 	u32 min_rx_priv;
2127 	unsigned int i;
2128 
2129 	if (tc_num)
2130 		rx_priv = rx_priv / tc_num;
2131 
2132 	if (tc_num <= NEED_RESERVE_TC_NUM)
2133 		rx_priv = rx_priv * BUF_RESERVE_PERCENT / BUF_MAX_PERCENT;
2134 
2135 	min_rx_priv = hdev->dv_buf_size + COMPENSATE_BUFFER +
2136 			COMPENSATE_HALF_MPS_NUM * half_mps;
2137 	min_rx_priv = round_up(min_rx_priv, HCLGE_BUF_SIZE_UNIT);
2138 	rx_priv = round_down(rx_priv, HCLGE_BUF_SIZE_UNIT);
2139 	if (rx_priv < min_rx_priv)
2140 		return false;
2141 
2142 	for (i = 0; i < HCLGE_MAX_TC_NUM; i++) {
2143 		struct hclge_priv_buf *priv = &buf_alloc->priv_buf[i];
2144 
2145 		priv->enable = 0;
2146 		priv->wl.low = 0;
2147 		priv->wl.high = 0;
2148 		priv->buf_size = 0;
2149 
2150 		if (!(hdev->hw_tc_map & BIT(i)))
2151 			continue;
2152 
2153 		priv->enable = 1;
2154 		priv->buf_size = rx_priv;
2155 		priv->wl.high = rx_priv - hdev->dv_buf_size;
2156 		priv->wl.low = priv->wl.high - PRIV_WL_GAP;
2157 	}
2158 
2159 	buf_alloc->s_buf.buf_size = 0;
2160 
2161 	return true;
2162 }
2163 
2164 /* hclge_rx_buffer_calc: calculate the rx private buffer size for all TCs
2165  * @hdev: pointer to struct hclge_dev
2166  * @buf_alloc: pointer to buffer calculation data
2167  * @return: 0: calculate successful, negative: fail
2168  */
2169 static int hclge_rx_buffer_calc(struct hclge_dev *hdev,
2170 				struct hclge_pkt_buf_alloc *buf_alloc)
2171 {
2172 	/* When DCB is not supported, rx private buffer is not allocated. */
2173 	if (!hnae3_dev_dcb_supported(hdev)) {
2174 		u32 rx_all = hdev->pkt_buf_size;
2175 
2176 		rx_all -= hclge_get_tx_buff_alloced(buf_alloc);
2177 		if (!hclge_is_rx_buf_ok(hdev, buf_alloc, rx_all))
2178 			return -ENOMEM;
2179 
2180 		return 0;
2181 	}
2182 
2183 	if (hclge_only_alloc_priv_buff(hdev, buf_alloc))
2184 		return 0;
2185 
2186 	if (hclge_rx_buf_calc_all(hdev, true, buf_alloc))
2187 		return 0;
2188 
2189 	/* try to decrease the buffer size */
2190 	if (hclge_rx_buf_calc_all(hdev, false, buf_alloc))
2191 		return 0;
2192 
2193 	if (hclge_drop_nopfc_buf_till_fit(hdev, buf_alloc))
2194 		return 0;
2195 
2196 	if (hclge_drop_pfc_buf_till_fit(hdev, buf_alloc))
2197 		return 0;
2198 
2199 	return -ENOMEM;
2200 }
2201 
2202 static int hclge_rx_priv_buf_alloc(struct hclge_dev *hdev,
2203 				   struct hclge_pkt_buf_alloc *buf_alloc)
2204 {
2205 	struct hclge_rx_priv_buff_cmd *req;
2206 	struct hclge_desc desc;
2207 	int ret;
2208 	int i;
2209 
2210 	hclge_cmd_setup_basic_desc(&desc, HCLGE_OPC_RX_PRIV_BUFF_ALLOC, false);
2211 	req = (struct hclge_rx_priv_buff_cmd *)desc.data;
2212 
2213 	/* Alloc private buffer TCs */
2214 	for (i = 0; i < HCLGE_MAX_TC_NUM; i++) {
2215 		struct hclge_priv_buf *priv = &buf_alloc->priv_buf[i];
2216 
2217 		req->buf_num[i] =
2218 			cpu_to_le16(priv->buf_size >> HCLGE_BUF_UNIT_S);
2219 		req->buf_num[i] |=
2220 			cpu_to_le16(1 << HCLGE_TC0_PRI_BUF_EN_B);
2221 	}
2222 
2223 	req->shared_buf =
2224 		cpu_to_le16((buf_alloc->s_buf.buf_size >> HCLGE_BUF_UNIT_S) |
2225 			    (1 << HCLGE_TC0_PRI_BUF_EN_B));
2226 
2227 	ret = hclge_cmd_send(&hdev->hw, &desc, 1);
2228 	if (ret)
2229 		dev_err(&hdev->pdev->dev,
2230 			"rx private buffer alloc cmd failed %d\n", ret);
2231 
2232 	return ret;
2233 }
2234 
2235 static int hclge_rx_priv_wl_config(struct hclge_dev *hdev,
2236 				   struct hclge_pkt_buf_alloc *buf_alloc)
2237 {
2238 	struct hclge_rx_priv_wl_buf *req;
2239 	struct hclge_priv_buf *priv;
2240 	struct hclge_desc desc[2];
2241 	int i, j;
2242 	int ret;
2243 
2244 	for (i = 0; i < 2; i++) {
2245 		hclge_cmd_setup_basic_desc(&desc[i], HCLGE_OPC_RX_PRIV_WL_ALLOC,
2246 					   false);
2247 		req = (struct hclge_rx_priv_wl_buf *)desc[i].data;
2248 
2249 		/* The first descriptor set the NEXT bit to 1 */
2250 		if (i == 0)
2251 			desc[i].flag |= cpu_to_le16(HCLGE_COMM_CMD_FLAG_NEXT);
2252 		else
2253 			desc[i].flag &= ~cpu_to_le16(HCLGE_COMM_CMD_FLAG_NEXT);
2254 
2255 		for (j = 0; j < HCLGE_TC_NUM_ONE_DESC; j++) {
2256 			u32 idx = i * HCLGE_TC_NUM_ONE_DESC + j;
2257 
2258 			priv = &buf_alloc->priv_buf[idx];
2259 			req->tc_wl[j].high =
2260 				cpu_to_le16(priv->wl.high >> HCLGE_BUF_UNIT_S);
2261 			req->tc_wl[j].high |=
2262 				cpu_to_le16(BIT(HCLGE_RX_PRIV_EN_B));
2263 			req->tc_wl[j].low =
2264 				cpu_to_le16(priv->wl.low >> HCLGE_BUF_UNIT_S);
2265 			req->tc_wl[j].low |=
2266 				 cpu_to_le16(BIT(HCLGE_RX_PRIV_EN_B));
2267 		}
2268 	}
2269 
2270 	/* Send 2 descriptor at one time */
2271 	ret = hclge_cmd_send(&hdev->hw, desc, 2);
2272 	if (ret)
2273 		dev_err(&hdev->pdev->dev,
2274 			"rx private waterline config cmd failed %d\n",
2275 			ret);
2276 	return ret;
2277 }
2278 
2279 static int hclge_common_thrd_config(struct hclge_dev *hdev,
2280 				    struct hclge_pkt_buf_alloc *buf_alloc)
2281 {
2282 	struct hclge_shared_buf *s_buf = &buf_alloc->s_buf;
2283 	struct hclge_rx_com_thrd *req;
2284 	struct hclge_desc desc[2];
2285 	struct hclge_tc_thrd *tc;
2286 	int i, j;
2287 	int ret;
2288 
2289 	for (i = 0; i < 2; i++) {
2290 		hclge_cmd_setup_basic_desc(&desc[i],
2291 					   HCLGE_OPC_RX_COM_THRD_ALLOC, false);
2292 		req = (struct hclge_rx_com_thrd *)desc[i].data;
2293 
2294 		/* The first descriptor set the NEXT bit to 1 */
2295 		if (i == 0)
2296 			desc[i].flag |= cpu_to_le16(HCLGE_COMM_CMD_FLAG_NEXT);
2297 		else
2298 			desc[i].flag &= ~cpu_to_le16(HCLGE_COMM_CMD_FLAG_NEXT);
2299 
2300 		for (j = 0; j < HCLGE_TC_NUM_ONE_DESC; j++) {
2301 			tc = &s_buf->tc_thrd[i * HCLGE_TC_NUM_ONE_DESC + j];
2302 
2303 			req->com_thrd[j].high =
2304 				cpu_to_le16(tc->high >> HCLGE_BUF_UNIT_S);
2305 			req->com_thrd[j].high |=
2306 				 cpu_to_le16(BIT(HCLGE_RX_PRIV_EN_B));
2307 			req->com_thrd[j].low =
2308 				cpu_to_le16(tc->low >> HCLGE_BUF_UNIT_S);
2309 			req->com_thrd[j].low |=
2310 				 cpu_to_le16(BIT(HCLGE_RX_PRIV_EN_B));
2311 		}
2312 	}
2313 
2314 	/* Send 2 descriptors at one time */
2315 	ret = hclge_cmd_send(&hdev->hw, desc, 2);
2316 	if (ret)
2317 		dev_err(&hdev->pdev->dev,
2318 			"common threshold config cmd failed %d\n", ret);
2319 	return ret;
2320 }
2321 
2322 static int hclge_common_wl_config(struct hclge_dev *hdev,
2323 				  struct hclge_pkt_buf_alloc *buf_alloc)
2324 {
2325 	struct hclge_shared_buf *buf = &buf_alloc->s_buf;
2326 	struct hclge_rx_com_wl *req;
2327 	struct hclge_desc desc;
2328 	int ret;
2329 
2330 	hclge_cmd_setup_basic_desc(&desc, HCLGE_OPC_RX_COM_WL_ALLOC, false);
2331 
2332 	req = (struct hclge_rx_com_wl *)desc.data;
2333 	req->com_wl.high = cpu_to_le16(buf->self.high >> HCLGE_BUF_UNIT_S);
2334 	req->com_wl.high |=  cpu_to_le16(BIT(HCLGE_RX_PRIV_EN_B));
2335 
2336 	req->com_wl.low = cpu_to_le16(buf->self.low >> HCLGE_BUF_UNIT_S);
2337 	req->com_wl.low |=  cpu_to_le16(BIT(HCLGE_RX_PRIV_EN_B));
2338 
2339 	ret = hclge_cmd_send(&hdev->hw, &desc, 1);
2340 	if (ret)
2341 		dev_err(&hdev->pdev->dev,
2342 			"common waterline config cmd failed %d\n", ret);
2343 
2344 	return ret;
2345 }
2346 
2347 int hclge_buffer_alloc(struct hclge_dev *hdev)
2348 {
2349 	struct hclge_pkt_buf_alloc *pkt_buf;
2350 	int ret;
2351 
2352 	pkt_buf = kzalloc_obj(*pkt_buf);
2353 	if (!pkt_buf)
2354 		return -ENOMEM;
2355 
2356 	ret = hclge_tx_buffer_calc(hdev, pkt_buf);
2357 	if (ret) {
2358 		dev_err(&hdev->pdev->dev,
2359 			"could not calc tx buffer size for all TCs %d\n", ret);
2360 		goto out;
2361 	}
2362 
2363 	ret = hclge_tx_buffer_alloc(hdev, pkt_buf);
2364 	if (ret) {
2365 		dev_err(&hdev->pdev->dev,
2366 			"could not alloc tx buffers %d\n", ret);
2367 		goto out;
2368 	}
2369 
2370 	ret = hclge_rx_buffer_calc(hdev, pkt_buf);
2371 	if (ret) {
2372 		dev_err(&hdev->pdev->dev,
2373 			"could not calc rx priv buffer size for all TCs %d\n",
2374 			ret);
2375 		goto out;
2376 	}
2377 
2378 	ret = hclge_rx_priv_buf_alloc(hdev, pkt_buf);
2379 	if (ret) {
2380 		dev_err(&hdev->pdev->dev, "could not alloc rx priv buffer %d\n",
2381 			ret);
2382 		goto out;
2383 	}
2384 
2385 	if (hnae3_dev_dcb_supported(hdev)) {
2386 		ret = hclge_rx_priv_wl_config(hdev, pkt_buf);
2387 		if (ret) {
2388 			dev_err(&hdev->pdev->dev,
2389 				"could not configure rx private waterline %d\n",
2390 				ret);
2391 			goto out;
2392 		}
2393 
2394 		ret = hclge_common_thrd_config(hdev, pkt_buf);
2395 		if (ret) {
2396 			dev_err(&hdev->pdev->dev,
2397 				"could not configure common threshold %d\n",
2398 				ret);
2399 			goto out;
2400 		}
2401 	}
2402 
2403 	ret = hclge_common_wl_config(hdev, pkt_buf);
2404 	if (ret)
2405 		dev_err(&hdev->pdev->dev,
2406 			"could not configure common waterline %d\n", ret);
2407 
2408 out:
2409 	kfree(pkt_buf);
2410 	return ret;
2411 }
2412 
2413 static int hclge_init_roce_base_info(struct hclge_vport *vport)
2414 {
2415 	struct hnae3_handle *roce = &vport->roce;
2416 	struct hnae3_handle *nic = &vport->nic;
2417 	struct hclge_dev *hdev = vport->back;
2418 
2419 	roce->rinfo.num_vectors = vport->back->num_roce_msi;
2420 
2421 	if (hdev->num_msi < hdev->num_nic_msi + hdev->num_roce_msi)
2422 		return -EINVAL;
2423 
2424 	roce->rinfo.base_vector = hdev->num_nic_msi;
2425 
2426 	roce->rinfo.netdev = nic->kinfo.netdev;
2427 	roce->rinfo.roce_io_base = hdev->hw.hw.io_base;
2428 	roce->rinfo.roce_mem_base = hdev->hw.hw.mem_base;
2429 
2430 	roce->pdev = nic->pdev;
2431 	roce->ae_algo = nic->ae_algo;
2432 	bitmap_copy(roce->numa_node_mask.bits, nic->numa_node_mask.bits,
2433 		    MAX_NUMNODES);
2434 
2435 	return 0;
2436 }
2437 
2438 static int hclge_init_msi(struct hclge_dev *hdev)
2439 {
2440 	struct pci_dev *pdev = hdev->pdev;
2441 	int vectors;
2442 	int i;
2443 
2444 	vectors = pci_alloc_irq_vectors(pdev, HNAE3_MIN_VECTOR_NUM,
2445 					hdev->num_msi,
2446 					PCI_IRQ_MSI | PCI_IRQ_MSIX);
2447 	if (vectors < 0) {
2448 		dev_err(&pdev->dev,
2449 			"failed(%d) to allocate MSI/MSI-X vectors\n",
2450 			vectors);
2451 		return vectors;
2452 	}
2453 	if (vectors < hdev->num_msi)
2454 		dev_warn(&hdev->pdev->dev,
2455 			 "requested %u MSI/MSI-X, but allocated %d MSI/MSI-X\n",
2456 			 hdev->num_msi, vectors);
2457 
2458 	hdev->num_msi = vectors;
2459 	hdev->num_msi_left = vectors;
2460 
2461 	hdev->vector_status = devm_kzalloc_objs(&pdev->dev, u16, hdev->num_msi);
2462 	if (!hdev->vector_status) {
2463 		pci_free_irq_vectors(pdev);
2464 		return -ENOMEM;
2465 	}
2466 
2467 	for (i = 0; i < hdev->num_msi; i++)
2468 		hdev->vector_status[i] = HCLGE_INVALID_VPORT;
2469 
2470 	hdev->vector_irq = devm_kzalloc_objs(&pdev->dev, int, hdev->num_msi);
2471 	if (!hdev->vector_irq) {
2472 		pci_free_irq_vectors(pdev);
2473 		return -ENOMEM;
2474 	}
2475 
2476 	return 0;
2477 }
2478 
2479 static u8 hclge_check_speed_dup(u8 duplex, int speed)
2480 {
2481 	if (!(speed == HCLGE_MAC_SPEED_10M || speed == HCLGE_MAC_SPEED_100M))
2482 		duplex = HCLGE_MAC_FULL;
2483 
2484 	return duplex;
2485 }
2486 
2487 static struct hclge_mac_speed_map hclge_mac_speed_map_to_fw[] = {
2488 	{HCLGE_MAC_SPEED_10M, HCLGE_FW_MAC_SPEED_10M},
2489 	{HCLGE_MAC_SPEED_100M, HCLGE_FW_MAC_SPEED_100M},
2490 	{HCLGE_MAC_SPEED_1G, HCLGE_FW_MAC_SPEED_1G},
2491 	{HCLGE_MAC_SPEED_10G, HCLGE_FW_MAC_SPEED_10G},
2492 	{HCLGE_MAC_SPEED_25G, HCLGE_FW_MAC_SPEED_25G},
2493 	{HCLGE_MAC_SPEED_40G, HCLGE_FW_MAC_SPEED_40G},
2494 	{HCLGE_MAC_SPEED_50G, HCLGE_FW_MAC_SPEED_50G},
2495 	{HCLGE_MAC_SPEED_100G, HCLGE_FW_MAC_SPEED_100G},
2496 	{HCLGE_MAC_SPEED_200G, HCLGE_FW_MAC_SPEED_200G},
2497 };
2498 
2499 static int hclge_convert_to_fw_speed(u32 speed_drv, u32 *speed_fw)
2500 {
2501 	u16 i;
2502 
2503 	for (i = 0; i < ARRAY_SIZE(hclge_mac_speed_map_to_fw); i++) {
2504 		if (hclge_mac_speed_map_to_fw[i].speed_drv == speed_drv) {
2505 			*speed_fw = hclge_mac_speed_map_to_fw[i].speed_fw;
2506 			return 0;
2507 		}
2508 	}
2509 
2510 	return -EINVAL;
2511 }
2512 
2513 static int hclge_cfg_mac_speed_dup_hw(struct hclge_dev *hdev, int speed,
2514 				      u8 duplex, u8 lane_num)
2515 {
2516 	struct hclge_config_mac_speed_dup_cmd *req;
2517 	struct hclge_desc desc;
2518 	u32 speed_fw;
2519 	int ret;
2520 
2521 	req = (struct hclge_config_mac_speed_dup_cmd *)desc.data;
2522 
2523 	hclge_cmd_setup_basic_desc(&desc, HCLGE_OPC_CONFIG_SPEED_DUP, false);
2524 
2525 	if (duplex)
2526 		hnae3_set_bit(req->speed_dup, HCLGE_CFG_DUPLEX_B, 1);
2527 
2528 	ret = hclge_convert_to_fw_speed(speed, &speed_fw);
2529 	if (ret) {
2530 		dev_err(&hdev->pdev->dev, "invalid speed (%d)\n", speed);
2531 		return ret;
2532 	}
2533 
2534 	hnae3_set_field(req->speed_dup, HCLGE_CFG_SPEED_M, HCLGE_CFG_SPEED_S,
2535 			speed_fw);
2536 	hnae3_set_bit(req->mac_change_fec_en, HCLGE_CFG_MAC_SPEED_CHANGE_EN_B,
2537 		      1);
2538 	req->lane_num = lane_num;
2539 
2540 	ret = hclge_cmd_send(&hdev->hw, &desc, 1);
2541 	if (ret) {
2542 		dev_err(&hdev->pdev->dev,
2543 			"mac speed/duplex config cmd failed %d.\n", ret);
2544 		return ret;
2545 	}
2546 
2547 	return 0;
2548 }
2549 
2550 int hclge_cfg_mac_speed_dup(struct hclge_dev *hdev, int speed, u8 duplex, u8 lane_num)
2551 {
2552 	struct hclge_mac *mac = &hdev->hw.mac;
2553 	int ret;
2554 
2555 	duplex = hclge_check_speed_dup(duplex, speed);
2556 	if (!mac->support_autoneg && mac->speed == (u32)speed &&
2557 	    mac->duplex == duplex && (mac->lane_num == lane_num || lane_num == 0))
2558 		return 0;
2559 
2560 	ret = hclge_cfg_mac_speed_dup_hw(hdev, speed, duplex, lane_num);
2561 	if (ret)
2562 		return ret;
2563 
2564 	hdev->hw.mac.speed = speed;
2565 	hdev->hw.mac.duplex = duplex;
2566 	if (!lane_num)
2567 		hdev->hw.mac.lane_num = lane_num;
2568 
2569 	return 0;
2570 }
2571 
2572 static int hclge_cfg_mac_speed_dup_h(struct hnae3_handle *handle, int speed,
2573 				     u8 duplex, u8 lane_num)
2574 {
2575 	struct hclge_vport *vport = hclge_get_vport(handle);
2576 	struct hclge_dev *hdev = vport->back;
2577 	int ret;
2578 
2579 	ret = hclge_cfg_mac_speed_dup(hdev, speed, duplex, lane_num);
2580 
2581 	if (ret)
2582 		return ret;
2583 
2584 	hdev->hw.mac.req_lane_num = lane_num;
2585 	if (speed != SPEED_UNKNOWN)
2586 		hdev->hw.mac.req_speed = (u32)speed;
2587 	if (duplex != DUPLEX_UNKNOWN)
2588 		hdev->hw.mac.req_duplex = duplex;
2589 
2590 	return 0;
2591 }
2592 
2593 static int hclge_set_autoneg_en(struct hclge_dev *hdev, bool enable)
2594 {
2595 	struct hclge_config_auto_neg_cmd *req;
2596 	struct hclge_desc desc;
2597 	u32 flag = 0;
2598 	int ret;
2599 
2600 	hclge_cmd_setup_basic_desc(&desc, HCLGE_OPC_CONFIG_AN_MODE, false);
2601 
2602 	req = (struct hclge_config_auto_neg_cmd *)desc.data;
2603 	if (enable)
2604 		hnae3_set_bit(flag, HCLGE_MAC_CFG_AN_EN_B, 1U);
2605 	req->cfg_an_cmd_flag = cpu_to_le32(flag);
2606 
2607 	ret = hclge_cmd_send(&hdev->hw, &desc, 1);
2608 	if (ret)
2609 		dev_err(&hdev->pdev->dev, "auto neg set cmd failed %d.\n",
2610 			ret);
2611 
2612 	return ret;
2613 }
2614 
2615 static int hclge_set_autoneg(struct hnae3_handle *handle, bool enable)
2616 {
2617 	struct hclge_vport *vport = hclge_get_vport(handle);
2618 	struct hclge_dev *hdev = vport->back;
2619 	int ret;
2620 
2621 	if (!hdev->hw.mac.support_autoneg) {
2622 		if (enable) {
2623 			dev_err(&hdev->pdev->dev,
2624 				"autoneg is not supported by current port\n");
2625 			return -EOPNOTSUPP;
2626 		} else {
2627 			return 0;
2628 		}
2629 	}
2630 
2631 	ret = hclge_set_autoneg_en(hdev, enable);
2632 	if (!ret)
2633 		hdev->hw.mac.req_autoneg = enable;
2634 	return ret;
2635 }
2636 
2637 static int hclge_get_autoneg(struct hnae3_handle *handle)
2638 {
2639 	struct hclge_vport *vport = hclge_get_vport(handle);
2640 	struct hclge_dev *hdev = vport->back;
2641 	struct phy_device *phydev = hdev->hw.mac.phydev;
2642 
2643 	if (phydev)
2644 		return phydev->autoneg;
2645 
2646 	return hdev->hw.mac.autoneg;
2647 }
2648 
2649 static int hclge_restart_autoneg(struct hnae3_handle *handle)
2650 {
2651 	struct hclge_vport *vport = hclge_get_vport(handle);
2652 	struct hclge_dev *hdev = vport->back;
2653 	int ret;
2654 
2655 	dev_dbg(&hdev->pdev->dev, "restart autoneg\n");
2656 
2657 	ret = hclge_notify_client(hdev, HNAE3_DOWN_CLIENT);
2658 	if (ret)
2659 		return ret;
2660 	return hclge_notify_client(hdev, HNAE3_UP_CLIENT);
2661 }
2662 
2663 static int hclge_halt_autoneg(struct hnae3_handle *handle, bool halt)
2664 {
2665 	struct hclge_vport *vport = hclge_get_vport(handle);
2666 	struct hclge_dev *hdev = vport->back;
2667 
2668 	if (hdev->hw.mac.support_autoneg && hdev->hw.mac.autoneg)
2669 		return hclge_set_autoneg_en(hdev, !halt);
2670 
2671 	return 0;
2672 }
2673 
2674 static void hclge_parse_fec_stats_lanes(struct hclge_dev *hdev,
2675 					struct hclge_desc *desc, u32 desc_len)
2676 {
2677 	u32 lane_size = HCLGE_FEC_STATS_MAX_LANES * 2;
2678 	u32 desc_index = 0;
2679 	u32 data_index = 0;
2680 	u32 i;
2681 
2682 	for (i = 0; i < lane_size; i++) {
2683 		if (data_index >= HCLGE_DESC_DATA_LEN) {
2684 			desc_index++;
2685 			data_index = 0;
2686 		}
2687 
2688 		if (desc_index >= desc_len)
2689 			return;
2690 
2691 		hdev->fec_stats.per_lanes[i] +=
2692 			le32_to_cpu(desc[desc_index].data[data_index]);
2693 		data_index++;
2694 	}
2695 }
2696 
2697 static void hclge_parse_fec_stats(struct hclge_dev *hdev,
2698 				  struct hclge_desc *desc, u32 desc_len)
2699 {
2700 	struct hclge_query_fec_stats_cmd *req;
2701 
2702 	req = (struct hclge_query_fec_stats_cmd *)desc[0].data;
2703 
2704 	hdev->fec_stats.base_r_lane_num = req->base_r_lane_num;
2705 	hdev->fec_stats.rs_corr_blocks +=
2706 		le32_to_cpu(req->rs_fec_corr_blocks);
2707 	hdev->fec_stats.rs_uncorr_blocks +=
2708 		le32_to_cpu(req->rs_fec_uncorr_blocks);
2709 	hdev->fec_stats.rs_error_blocks +=
2710 		le32_to_cpu(req->rs_fec_error_blocks);
2711 	hdev->fec_stats.base_r_corr_blocks +=
2712 		le32_to_cpu(req->base_r_fec_corr_blocks);
2713 	hdev->fec_stats.base_r_uncorr_blocks +=
2714 		le32_to_cpu(req->base_r_fec_uncorr_blocks);
2715 
2716 	hclge_parse_fec_stats_lanes(hdev, &desc[1], desc_len - 1);
2717 }
2718 
2719 static int hclge_update_fec_stats_hw(struct hclge_dev *hdev)
2720 {
2721 	struct hclge_desc desc[HCLGE_FEC_STATS_CMD_NUM];
2722 	int ret;
2723 	u32 i;
2724 
2725 	for (i = 0; i < HCLGE_FEC_STATS_CMD_NUM; i++) {
2726 		hclge_cmd_setup_basic_desc(&desc[i], HCLGE_OPC_QUERY_FEC_STATS,
2727 					   true);
2728 		if (i != (HCLGE_FEC_STATS_CMD_NUM - 1))
2729 			desc[i].flag |= cpu_to_le16(HCLGE_COMM_CMD_FLAG_NEXT);
2730 	}
2731 
2732 	ret = hclge_cmd_send(&hdev->hw, desc, HCLGE_FEC_STATS_CMD_NUM);
2733 	if (ret)
2734 		return ret;
2735 
2736 	hclge_parse_fec_stats(hdev, desc, HCLGE_FEC_STATS_CMD_NUM);
2737 
2738 	return 0;
2739 }
2740 
2741 static void hclge_update_fec_stats(struct hclge_dev *hdev)
2742 {
2743 	struct hnae3_ae_dev *ae_dev = pci_get_drvdata(hdev->pdev);
2744 	int ret;
2745 
2746 	if (!hnae3_ae_dev_fec_stats_supported(ae_dev) ||
2747 	    test_and_set_bit(HCLGE_STATE_FEC_STATS_UPDATING, &hdev->state))
2748 		return;
2749 
2750 	ret = hclge_update_fec_stats_hw(hdev);
2751 	if (ret)
2752 		dev_err(&hdev->pdev->dev,
2753 			"failed to update fec stats, ret = %d\n", ret);
2754 
2755 	clear_bit(HCLGE_STATE_FEC_STATS_UPDATING, &hdev->state);
2756 }
2757 
2758 static void hclge_get_fec_stats_total(struct hclge_dev *hdev,
2759 				      struct ethtool_fec_stats *fec_stats)
2760 {
2761 	fec_stats->corrected_blocks.total = hdev->fec_stats.rs_corr_blocks;
2762 	fec_stats->uncorrectable_blocks.total =
2763 		hdev->fec_stats.rs_uncorr_blocks;
2764 }
2765 
2766 static void hclge_get_fec_stats_lanes(struct hclge_dev *hdev,
2767 				      struct ethtool_fec_stats *fec_stats)
2768 {
2769 	u32 i;
2770 
2771 	if (hdev->fec_stats.base_r_lane_num == 0 ||
2772 	    hdev->fec_stats.base_r_lane_num > HCLGE_FEC_STATS_MAX_LANES) {
2773 		dev_err(&hdev->pdev->dev,
2774 			"fec stats lane number(%llu) is invalid\n",
2775 			hdev->fec_stats.base_r_lane_num);
2776 		return;
2777 	}
2778 
2779 	for (i = 0; i < hdev->fec_stats.base_r_lane_num; i++) {
2780 		fec_stats->corrected_blocks.lanes[i] =
2781 			hdev->fec_stats.base_r_corr_per_lanes[i];
2782 		fec_stats->uncorrectable_blocks.lanes[i] =
2783 			hdev->fec_stats.base_r_uncorr_per_lanes[i];
2784 	}
2785 }
2786 
2787 static void hclge_comm_get_fec_stats(struct hclge_dev *hdev,
2788 				     struct ethtool_fec_stats *fec_stats)
2789 {
2790 	u32 fec_mode = hdev->hw.mac.fec_mode;
2791 
2792 	switch (fec_mode) {
2793 	case BIT(HNAE3_FEC_RS):
2794 	case BIT(HNAE3_FEC_LLRS):
2795 		hclge_get_fec_stats_total(hdev, fec_stats);
2796 		break;
2797 	case BIT(HNAE3_FEC_BASER):
2798 		hclge_get_fec_stats_lanes(hdev, fec_stats);
2799 		break;
2800 	default:
2801 		dev_err(&hdev->pdev->dev,
2802 			"fec stats is not supported by current fec mode(0x%x)\n",
2803 			fec_mode);
2804 		break;
2805 	}
2806 }
2807 
2808 static void hclge_get_fec_stats(struct hnae3_handle *handle,
2809 				struct ethtool_fec_stats *fec_stats)
2810 {
2811 	struct hclge_vport *vport = hclge_get_vport(handle);
2812 	struct hclge_dev *hdev = vport->back;
2813 	u32 fec_mode = hdev->hw.mac.fec_mode;
2814 
2815 	if (fec_mode == BIT(HNAE3_FEC_NONE) ||
2816 	    fec_mode == BIT(HNAE3_FEC_AUTO) ||
2817 	    fec_mode == BIT(HNAE3_FEC_USER_DEF))
2818 		return;
2819 
2820 	hclge_update_fec_stats(hdev);
2821 
2822 	hclge_comm_get_fec_stats(hdev, fec_stats);
2823 }
2824 
2825 static int hclge_set_fec_hw(struct hclge_dev *hdev, u32 fec_mode)
2826 {
2827 	struct hclge_config_fec_cmd *req;
2828 	struct hclge_desc desc;
2829 	int ret;
2830 
2831 	hclge_cmd_setup_basic_desc(&desc, HCLGE_OPC_CONFIG_FEC_MODE, false);
2832 
2833 	req = (struct hclge_config_fec_cmd *)desc.data;
2834 	if (fec_mode & BIT(HNAE3_FEC_AUTO))
2835 		hnae3_set_bit(req->fec_mode, HCLGE_MAC_CFG_FEC_AUTO_EN_B, 1);
2836 	if (fec_mode & BIT(HNAE3_FEC_RS))
2837 		hnae3_set_field(req->fec_mode, HCLGE_MAC_CFG_FEC_MODE_M,
2838 				HCLGE_MAC_CFG_FEC_MODE_S, HCLGE_MAC_FEC_RS);
2839 	if (fec_mode & BIT(HNAE3_FEC_LLRS))
2840 		hnae3_set_field(req->fec_mode, HCLGE_MAC_CFG_FEC_MODE_M,
2841 				HCLGE_MAC_CFG_FEC_MODE_S, HCLGE_MAC_FEC_LLRS);
2842 	if (fec_mode & BIT(HNAE3_FEC_BASER))
2843 		hnae3_set_field(req->fec_mode, HCLGE_MAC_CFG_FEC_MODE_M,
2844 				HCLGE_MAC_CFG_FEC_MODE_S, HCLGE_MAC_FEC_BASER);
2845 
2846 	ret = hclge_cmd_send(&hdev->hw, &desc, 1);
2847 	if (ret)
2848 		dev_err(&hdev->pdev->dev, "set fec mode failed %d.\n", ret);
2849 
2850 	return ret;
2851 }
2852 
2853 static int hclge_set_fec(struct hnae3_handle *handle, u32 fec_mode)
2854 {
2855 	struct hclge_vport *vport = hclge_get_vport(handle);
2856 	struct hclge_dev *hdev = vport->back;
2857 	struct hclge_mac *mac = &hdev->hw.mac;
2858 	int ret;
2859 
2860 	if (fec_mode && !(mac->fec_ability & fec_mode)) {
2861 		dev_err(&hdev->pdev->dev, "unsupported fec mode\n");
2862 		return -EINVAL;
2863 	}
2864 
2865 	ret = hclge_set_fec_hw(hdev, fec_mode);
2866 	if (ret)
2867 		return ret;
2868 
2869 	mac->user_fec_mode = fec_mode | BIT(HNAE3_FEC_USER_DEF);
2870 	return 0;
2871 }
2872 
2873 static void hclge_get_fec(struct hnae3_handle *handle, u8 *fec_ability,
2874 			  u8 *fec_mode)
2875 {
2876 	struct hclge_vport *vport = hclge_get_vport(handle);
2877 	struct hclge_dev *hdev = vport->back;
2878 	struct hclge_mac *mac = &hdev->hw.mac;
2879 
2880 	if (fec_ability)
2881 		*fec_ability = mac->fec_ability;
2882 	if (fec_mode)
2883 		*fec_mode = mac->fec_mode;
2884 }
2885 
2886 static int hclge_mac_init(struct hclge_dev *hdev)
2887 {
2888 	struct hclge_mac *mac = &hdev->hw.mac;
2889 	int ret;
2890 
2891 	hdev->support_sfp_query = true;
2892 
2893 	if (!test_bit(HCLGE_STATE_RST_HANDLING, &hdev->state))
2894 		hdev->hw.mac.duplex = HCLGE_MAC_FULL;
2895 
2896 	mac->link = 0;
2897 
2898 	if (mac->user_fec_mode & BIT(HNAE3_FEC_USER_DEF)) {
2899 		ret = hclge_set_fec_hw(hdev, mac->user_fec_mode);
2900 		if (ret)
2901 			return ret;
2902 	}
2903 
2904 	ret = hclge_set_mac_mtu(hdev, hdev->mps);
2905 	if (ret) {
2906 		dev_err(&hdev->pdev->dev, "set mtu failed ret=%d\n", ret);
2907 		return ret;
2908 	}
2909 
2910 	ret = hclge_set_default_loopback(hdev);
2911 	if (ret)
2912 		return ret;
2913 
2914 	ret = hclge_buffer_alloc(hdev);
2915 	if (ret)
2916 		dev_err(&hdev->pdev->dev,
2917 			"allocate buffer fail, ret=%d\n", ret);
2918 
2919 	return ret;
2920 }
2921 
2922 static void hclge_mbx_task_schedule(struct hclge_dev *hdev)
2923 {
2924 	if (!test_bit(HCLGE_STATE_REMOVING, &hdev->state) &&
2925 	    !test_and_set_bit(HCLGE_STATE_MBX_SERVICE_SCHED, &hdev->state)) {
2926 		hdev->last_mbx_scheduled = jiffies;
2927 		mod_delayed_work(hclge_wq, &hdev->service_task, 0);
2928 	}
2929 }
2930 
2931 static void hclge_reset_task_schedule(struct hclge_dev *hdev)
2932 {
2933 	if (!test_bit(HCLGE_STATE_REMOVING, &hdev->state) &&
2934 	    test_bit(HCLGE_STATE_SERVICE_INITED, &hdev->state) &&
2935 	    !test_and_set_bit(HCLGE_STATE_RST_SERVICE_SCHED, &hdev->state)) {
2936 		hdev->last_rst_scheduled = jiffies;
2937 		mod_delayed_work(hclge_wq, &hdev->service_task, 0);
2938 	}
2939 }
2940 
2941 static void hclge_errhand_task_schedule(struct hclge_dev *hdev)
2942 {
2943 	if (!test_bit(HCLGE_STATE_REMOVING, &hdev->state) &&
2944 	    !test_and_set_bit(HCLGE_STATE_ERR_SERVICE_SCHED, &hdev->state))
2945 		mod_delayed_work(hclge_wq, &hdev->service_task, 0);
2946 }
2947 
2948 void hclge_task_schedule(struct hclge_dev *hdev, unsigned long delay_time)
2949 {
2950 	if (!test_bit(HCLGE_STATE_REMOVING, &hdev->state) &&
2951 	    !test_bit(HCLGE_STATE_RST_FAIL, &hdev->state))
2952 		mod_delayed_work(hclge_wq, &hdev->service_task, delay_time);
2953 }
2954 
2955 static int hclge_get_mac_link_status(struct hclge_dev *hdev, int *link_status)
2956 {
2957 	struct hclge_link_status_cmd *req;
2958 	struct hclge_desc desc;
2959 	int ret;
2960 
2961 	hclge_cmd_setup_basic_desc(&desc, HCLGE_OPC_QUERY_LINK_STATUS, true);
2962 	ret = hclge_cmd_send(&hdev->hw, &desc, 1);
2963 	if (ret) {
2964 		dev_err(&hdev->pdev->dev, "get link status cmd failed %d\n",
2965 			ret);
2966 		return ret;
2967 	}
2968 
2969 	req = (struct hclge_link_status_cmd *)desc.data;
2970 	*link_status = (req->status & HCLGE_LINK_STATUS_UP_M) > 0 ?
2971 		HCLGE_LINK_STATUS_UP : HCLGE_LINK_STATUS_DOWN;
2972 
2973 	return 0;
2974 }
2975 
2976 static int hclge_get_mac_phy_link(struct hclge_dev *hdev, int *link_status)
2977 {
2978 	struct phy_device *phydev = hdev->hw.mac.phydev;
2979 
2980 	*link_status = HCLGE_LINK_STATUS_DOWN;
2981 
2982 	if (test_bit(HCLGE_STATE_DOWN, &hdev->state))
2983 		return 0;
2984 
2985 	if (phydev && (phydev->state != PHY_RUNNING || !phydev->link))
2986 		return 0;
2987 
2988 	return hclge_get_mac_link_status(hdev, link_status);
2989 }
2990 
2991 static void hclge_push_link_status(struct hclge_dev *hdev)
2992 {
2993 	struct hclge_vport *vport;
2994 	int ret;
2995 	u16 i;
2996 
2997 	for (i = 0; i < pci_num_vf(hdev->pdev); i++) {
2998 		vport = &hdev->vport[i + HCLGE_VF_VPORT_START_NUM];
2999 
3000 		if (!test_bit(HCLGE_VPORT_STATE_ALIVE, &vport->state) ||
3001 		    vport->vf_info.link_state != IFLA_VF_LINK_STATE_AUTO)
3002 			continue;
3003 
3004 		ret = hclge_push_vf_link_status(vport);
3005 		if (ret) {
3006 			dev_err(&hdev->pdev->dev,
3007 				"failed to push link status to vf%u, ret = %d\n",
3008 				i, ret);
3009 		}
3010 	}
3011 }
3012 
3013 static void hclge_update_link_status(struct hclge_dev *hdev)
3014 {
3015 	struct hnae3_handle *handle = &hdev->vport[0].nic;
3016 	struct hnae3_client *client = hdev->nic_client;
3017 	int state;
3018 	int ret;
3019 
3020 	if (!client)
3021 		return;
3022 
3023 	if (test_and_set_bit(HCLGE_STATE_LINK_UPDATING, &hdev->state))
3024 		return;
3025 
3026 	ret = hclge_get_mac_phy_link(hdev, &state);
3027 	if (ret) {
3028 		clear_bit(HCLGE_STATE_LINK_UPDATING, &hdev->state);
3029 		return;
3030 	}
3031 
3032 	if (state != hdev->hw.mac.link) {
3033 		hdev->hw.mac.link = state;
3034 		if (state == HCLGE_LINK_STATUS_UP)
3035 			hclge_update_port_info(hdev);
3036 
3037 		client->ops->link_status_change(handle, state);
3038 		hclge_config_mac_tnl_int(hdev, state);
3039 
3040 		if (test_bit(HCLGE_STATE_ROCE_REGISTERED, &hdev->state)) {
3041 			struct hnae3_handle *rhandle = &hdev->vport[0].roce;
3042 			struct hnae3_client *rclient = hdev->roce_client;
3043 
3044 			if (rclient && rclient->ops->link_status_change)
3045 				rclient->ops->link_status_change(rhandle,
3046 								 state);
3047 		}
3048 
3049 		hclge_push_link_status(hdev);
3050 	}
3051 
3052 	clear_bit(HCLGE_STATE_LINK_UPDATING, &hdev->state);
3053 }
3054 
3055 static void hclge_update_speed_advertising(struct hclge_mac *mac)
3056 {
3057 	u32 speed_ability;
3058 
3059 	if (hclge_get_speed_bit(mac->speed, &speed_ability))
3060 		return;
3061 
3062 	switch (mac->module_type) {
3063 	case HNAE3_MODULE_TYPE_FIBRE_LR:
3064 		hclge_convert_setting_lr(speed_ability, mac->advertising);
3065 		break;
3066 	case HNAE3_MODULE_TYPE_FIBRE_SR:
3067 	case HNAE3_MODULE_TYPE_AOC:
3068 		hclge_convert_setting_sr(speed_ability, mac->advertising);
3069 		break;
3070 	case HNAE3_MODULE_TYPE_CR:
3071 		hclge_convert_setting_cr(speed_ability, mac->advertising);
3072 		break;
3073 	case HNAE3_MODULE_TYPE_KR:
3074 		hclge_convert_setting_kr(speed_ability, mac->advertising);
3075 		break;
3076 	default:
3077 		break;
3078 	}
3079 }
3080 
3081 static void hclge_update_fec_advertising(struct hclge_mac *mac)
3082 {
3083 	if (mac->fec_mode & BIT(HNAE3_FEC_RS))
3084 		linkmode_set_bit(ETHTOOL_LINK_MODE_FEC_RS_BIT,
3085 				 mac->advertising);
3086 	else if (mac->fec_mode & BIT(HNAE3_FEC_LLRS))
3087 		linkmode_set_bit(ETHTOOL_LINK_MODE_FEC_LLRS_BIT,
3088 				 mac->advertising);
3089 	else if (mac->fec_mode & BIT(HNAE3_FEC_BASER))
3090 		linkmode_set_bit(ETHTOOL_LINK_MODE_FEC_BASER_BIT,
3091 				 mac->advertising);
3092 	else
3093 		linkmode_set_bit(ETHTOOL_LINK_MODE_FEC_NONE_BIT,
3094 				 mac->advertising);
3095 }
3096 
3097 static void hclge_update_pause_advertising(struct hclge_dev *hdev)
3098 {
3099 	struct hclge_mac *mac = &hdev->hw.mac;
3100 	bool rx_en, tx_en;
3101 
3102 	switch (hdev->fc_mode_last_time) {
3103 	case HCLGE_FC_RX_PAUSE:
3104 		rx_en = true;
3105 		tx_en = false;
3106 		break;
3107 	case HCLGE_FC_TX_PAUSE:
3108 		rx_en = false;
3109 		tx_en = true;
3110 		break;
3111 	case HCLGE_FC_FULL:
3112 		rx_en = true;
3113 		tx_en = true;
3114 		break;
3115 	default:
3116 		rx_en = false;
3117 		tx_en = false;
3118 		break;
3119 	}
3120 
3121 	linkmode_set_pause(mac->advertising, tx_en, rx_en);
3122 }
3123 
3124 static void hclge_update_advertising(struct hclge_dev *hdev)
3125 {
3126 	struct hclge_mac *mac = &hdev->hw.mac;
3127 
3128 	linkmode_zero(mac->advertising);
3129 	hclge_update_speed_advertising(mac);
3130 	hclge_update_fec_advertising(mac);
3131 	hclge_update_pause_advertising(hdev);
3132 }
3133 
3134 static void hclge_update_port_capability(struct hclge_dev *hdev,
3135 					 struct hclge_mac *mac)
3136 {
3137 	if (hnae3_dev_fec_supported(hdev))
3138 		hclge_convert_setting_fec(mac);
3139 
3140 	/* firmware can not identify back plane type, the media type
3141 	 * read from configuration can help deal it
3142 	 */
3143 	if (mac->media_type == HNAE3_MEDIA_TYPE_BACKPLANE &&
3144 	    mac->module_type == HNAE3_MODULE_TYPE_UNKNOWN)
3145 		mac->module_type = HNAE3_MODULE_TYPE_KR;
3146 	else if (mac->media_type == HNAE3_MEDIA_TYPE_COPPER)
3147 		mac->module_type = HNAE3_MODULE_TYPE_TP;
3148 
3149 	if (mac->support_autoneg) {
3150 		linkmode_set_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, mac->supported);
3151 		linkmode_copy(mac->advertising, mac->supported);
3152 	} else {
3153 		linkmode_clear_bit(ETHTOOL_LINK_MODE_Autoneg_BIT,
3154 				   mac->supported);
3155 		hclge_update_advertising(hdev);
3156 	}
3157 }
3158 
3159 static int hclge_get_sfp_speed(struct hclge_dev *hdev, u32 *speed)
3160 {
3161 	struct hclge_sfp_info_cmd *resp;
3162 	struct hclge_desc desc;
3163 	int ret;
3164 
3165 	hclge_cmd_setup_basic_desc(&desc, HCLGE_OPC_GET_SFP_INFO, true);
3166 	resp = (struct hclge_sfp_info_cmd *)desc.data;
3167 	ret = hclge_cmd_send(&hdev->hw, &desc, 1);
3168 	if (ret == -EOPNOTSUPP) {
3169 		dev_warn(&hdev->pdev->dev,
3170 			 "IMP do not support get SFP speed %d\n", ret);
3171 		return ret;
3172 	} else if (ret) {
3173 		dev_err(&hdev->pdev->dev, "get sfp speed failed %d\n", ret);
3174 		return ret;
3175 	}
3176 
3177 	*speed = le32_to_cpu(resp->speed);
3178 
3179 	return 0;
3180 }
3181 
3182 static int hclge_get_sfp_info(struct hclge_dev *hdev, struct hclge_mac *mac)
3183 {
3184 	struct hclge_sfp_info_cmd *resp;
3185 	struct hclge_desc desc;
3186 	int ret;
3187 
3188 	hclge_cmd_setup_basic_desc(&desc, HCLGE_OPC_GET_SFP_INFO, true);
3189 	resp = (struct hclge_sfp_info_cmd *)desc.data;
3190 
3191 	resp->query_type = QUERY_ACTIVE_SPEED;
3192 
3193 	ret = hclge_cmd_send(&hdev->hw, &desc, 1);
3194 	if (ret == -EOPNOTSUPP) {
3195 		dev_warn(&hdev->pdev->dev,
3196 			 "IMP does not support get SFP info %d\n", ret);
3197 		return ret;
3198 	} else if (ret) {
3199 		dev_err(&hdev->pdev->dev, "get sfp info failed %d\n", ret);
3200 		return ret;
3201 	}
3202 
3203 	/* In some case, mac speed get from IMP may be 0, it shouldn't be
3204 	 * set to mac->speed.
3205 	 */
3206 	if (!le32_to_cpu(resp->speed))
3207 		return 0;
3208 
3209 	mac->speed = le32_to_cpu(resp->speed);
3210 	/* if resp->speed_ability is 0, it means it's an old version
3211 	 * firmware, do not update these params
3212 	 */
3213 	if (resp->speed_ability) {
3214 		mac->module_type = le32_to_cpu(resp->module_type);
3215 		mac->speed_ability = le32_to_cpu(resp->speed_ability);
3216 		mac->autoneg = resp->autoneg;
3217 		mac->support_autoneg = resp->autoneg_ability;
3218 		mac->speed_type = QUERY_ACTIVE_SPEED;
3219 		mac->lane_num = resp->lane_num;
3220 		if (!resp->active_fec)
3221 			mac->fec_mode = 0;
3222 		else
3223 			mac->fec_mode = BIT(resp->active_fec);
3224 		mac->fec_ability = resp->fec_ability;
3225 	} else {
3226 		mac->speed_type = QUERY_SFP_SPEED;
3227 	}
3228 
3229 	return 0;
3230 }
3231 
3232 static int hclge_get_phy_link_ksettings(struct hnae3_handle *handle,
3233 					struct ethtool_link_ksettings *cmd)
3234 {
3235 	struct hclge_desc desc[HCLGE_PHY_LINK_SETTING_BD_NUM];
3236 	struct hclge_vport *vport = hclge_get_vport(handle);
3237 	struct hclge_phy_link_ksetting_0_cmd *req0;
3238 	struct hclge_phy_link_ksetting_1_cmd *req1;
3239 	u32 supported, advertising, lp_advertising;
3240 	struct hclge_dev *hdev = vport->back;
3241 	int ret;
3242 
3243 	hclge_cmd_setup_basic_desc(&desc[0], HCLGE_OPC_PHY_LINK_KSETTING,
3244 				   true);
3245 	desc[0].flag |= cpu_to_le16(HCLGE_COMM_CMD_FLAG_NEXT);
3246 	hclge_cmd_setup_basic_desc(&desc[1], HCLGE_OPC_PHY_LINK_KSETTING,
3247 				   true);
3248 
3249 	ret = hclge_cmd_send(&hdev->hw, desc, HCLGE_PHY_LINK_SETTING_BD_NUM);
3250 	if (ret) {
3251 		dev_err(&hdev->pdev->dev,
3252 			"failed to get phy link ksetting, ret = %d.\n", ret);
3253 		return ret;
3254 	}
3255 
3256 	req0 = (struct hclge_phy_link_ksetting_0_cmd *)desc[0].data;
3257 	cmd->base.autoneg = req0->autoneg;
3258 	cmd->base.speed = le32_to_cpu(req0->speed);
3259 	cmd->base.duplex = req0->duplex;
3260 	cmd->base.port = req0->port;
3261 	cmd->base.transceiver = req0->transceiver;
3262 	cmd->base.phy_address = req0->phy_address;
3263 	cmd->base.eth_tp_mdix = req0->eth_tp_mdix;
3264 	cmd->base.eth_tp_mdix_ctrl = req0->eth_tp_mdix_ctrl;
3265 	supported = le32_to_cpu(req0->supported);
3266 	advertising = le32_to_cpu(req0->advertising);
3267 	lp_advertising = le32_to_cpu(req0->lp_advertising);
3268 	ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
3269 						supported);
3270 	ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising,
3271 						advertising);
3272 	ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.lp_advertising,
3273 						lp_advertising);
3274 
3275 	req1 = (struct hclge_phy_link_ksetting_1_cmd *)desc[1].data;
3276 	cmd->base.master_slave_cfg = req1->master_slave_cfg;
3277 	cmd->base.master_slave_state = req1->master_slave_state;
3278 
3279 	return 0;
3280 }
3281 
3282 static int
3283 hclge_ethtool_ksettings_set(struct hnae3_handle *handle,
3284 			    const struct ethtool_link_ksettings *cmd)
3285 {
3286 	struct hclge_desc desc[HCLGE_PHY_LINK_SETTING_BD_NUM];
3287 	struct hclge_vport *vport = hclge_get_vport(handle);
3288 	struct hclge_phy_link_ksetting_0_cmd *req0;
3289 	struct hclge_phy_link_ksetting_1_cmd *req1;
3290 	struct hclge_dev *hdev = vport->back;
3291 	u32 advertising;
3292 	int ret;
3293 
3294 	if (cmd->base.autoneg == AUTONEG_DISABLE &&
3295 	    ((cmd->base.speed != SPEED_100 && cmd->base.speed != SPEED_10) ||
3296 	     (cmd->base.duplex != DUPLEX_HALF &&
3297 	      cmd->base.duplex != DUPLEX_FULL)))
3298 		return -EINVAL;
3299 
3300 	hclge_cmd_setup_basic_desc(&desc[0], HCLGE_OPC_PHY_LINK_KSETTING,
3301 				   false);
3302 	desc[0].flag |= cpu_to_le16(HCLGE_COMM_CMD_FLAG_NEXT);
3303 	hclge_cmd_setup_basic_desc(&desc[1], HCLGE_OPC_PHY_LINK_KSETTING,
3304 				   false);
3305 
3306 	req0 = (struct hclge_phy_link_ksetting_0_cmd *)desc[0].data;
3307 	req0->autoneg = cmd->base.autoneg;
3308 	req0->speed = cpu_to_le32(cmd->base.speed);
3309 	req0->duplex = cmd->base.duplex;
3310 	ethtool_convert_link_mode_to_legacy_u32(&advertising,
3311 						cmd->link_modes.advertising);
3312 	req0->advertising = cpu_to_le32(advertising);
3313 	req0->eth_tp_mdix_ctrl = cmd->base.eth_tp_mdix_ctrl;
3314 
3315 	req1 = (struct hclge_phy_link_ksetting_1_cmd *)desc[1].data;
3316 	req1->master_slave_cfg = cmd->base.master_slave_cfg;
3317 
3318 	ret = hclge_cmd_send(&hdev->hw, desc, HCLGE_PHY_LINK_SETTING_BD_NUM);
3319 	if (ret) {
3320 		dev_err(&hdev->pdev->dev,
3321 			"failed to set phy link ksettings, ret = %d.\n", ret);
3322 		return ret;
3323 	}
3324 
3325 	linkmode_copy(hdev->hw.mac.advertising, cmd->link_modes.advertising);
3326 	return 0;
3327 }
3328 
3329 static int
3330 hclge_set_phy_link_ksettings(struct hnae3_handle *handle,
3331 			     const struct ethtool_link_ksettings *cmd)
3332 {
3333 	struct hclge_vport *vport = hclge_get_vport(handle);
3334 	struct hclge_dev *hdev = vport->back;
3335 	int ret = -ENODEV;
3336 
3337 	if (hnae3_dev_phy_imp_supported(hdev)) {
3338 		ret = hclge_ethtool_ksettings_set(handle, cmd);
3339 	} else if (handle->netdev->phydev) {
3340 		if (cmd->base.speed == SPEED_1000 &&
3341 		    cmd->base.autoneg == AUTONEG_DISABLE)
3342 			return -EINVAL;
3343 		ret = phy_ethtool_ksettings_set(handle->netdev->phydev, cmd);
3344 	}
3345 	if (ret)
3346 		return ret;
3347 
3348 	hdev->hw.mac.req_autoneg = cmd->base.autoneg;
3349 	if (cmd->base.speed != SPEED_UNKNOWN)
3350 		hdev->hw.mac.req_speed = cmd->base.speed;
3351 	if (cmd->base.duplex != DUPLEX_UNKNOWN)
3352 		hdev->hw.mac.req_duplex = cmd->base.duplex;
3353 
3354 	return 0;
3355 }
3356 
3357 static int hclge_update_tp_port_info(struct hclge_dev *hdev)
3358 {
3359 	struct ethtool_link_ksettings cmd;
3360 	int ret;
3361 
3362 	if (!hnae3_dev_phy_imp_supported(hdev))
3363 		return 0;
3364 
3365 	ret = hclge_get_phy_link_ksettings(&hdev->vport->nic, &cmd);
3366 	if (ret)
3367 		return ret;
3368 
3369 	hdev->hw.mac.autoneg = cmd.base.autoneg;
3370 	hdev->hw.mac.speed = cmd.base.speed;
3371 	hdev->hw.mac.duplex = cmd.base.duplex;
3372 	linkmode_copy(hdev->hw.mac.advertising, cmd.link_modes.advertising);
3373 
3374 	return 0;
3375 }
3376 
3377 static int hclge_tp_port_init(struct hclge_dev *hdev)
3378 {
3379 	struct ethtool_link_ksettings cmd;
3380 
3381 	if (!hnae3_dev_phy_imp_supported(hdev))
3382 		return 0;
3383 
3384 	cmd.base.autoneg = hdev->hw.mac.req_autoneg;
3385 	cmd.base.speed = hdev->hw.mac.req_speed;
3386 	cmd.base.duplex = hdev->hw.mac.req_duplex;
3387 	linkmode_copy(cmd.link_modes.advertising, hdev->hw.mac.advertising);
3388 
3389 	return hclge_set_phy_link_ksettings(&hdev->vport->nic, &cmd);
3390 }
3391 
3392 static int hclge_update_port_info(struct hclge_dev *hdev)
3393 {
3394 	struct hclge_mac *mac = &hdev->hw.mac;
3395 	u32 speed;
3396 	int ret;
3397 
3398 	/* get the port info from SFP cmd if not copper port */
3399 	if (mac->media_type == HNAE3_MEDIA_TYPE_COPPER)
3400 		return hclge_update_tp_port_info(hdev);
3401 
3402 	/* if IMP does not support get SFP/qSFP info, return directly */
3403 	if (!hdev->support_sfp_query)
3404 		return 0;
3405 
3406 	if (hdev->ae_dev->dev_version >= HNAE3_DEVICE_VERSION_V2) {
3407 		speed = mac->speed;
3408 		ret = hclge_get_sfp_info(hdev, mac);
3409 	} else {
3410 		speed = HCLGE_MAC_SPEED_UNKNOWN;
3411 		ret = hclge_get_sfp_speed(hdev, &speed);
3412 	}
3413 
3414 	if (ret == -EOPNOTSUPP) {
3415 		hdev->support_sfp_query = false;
3416 		return ret;
3417 	} else if (ret) {
3418 		return ret;
3419 	}
3420 
3421 	if (hdev->ae_dev->dev_version >= HNAE3_DEVICE_VERSION_V2) {
3422 		if (mac->speed_type == QUERY_ACTIVE_SPEED) {
3423 			hclge_update_port_capability(hdev, mac);
3424 			if (mac->speed != speed)
3425 				(void)hclge_tm_port_shaper_cfg(hdev);
3426 			return 0;
3427 		}
3428 		return hclge_cfg_mac_speed_dup(hdev, mac->speed,
3429 					       HCLGE_MAC_FULL, mac->lane_num);
3430 	} else {
3431 		if (speed == HCLGE_MAC_SPEED_UNKNOWN)
3432 			return 0; /* do nothing if no SFP */
3433 
3434 		/* must config full duplex for SFP */
3435 		return hclge_cfg_mac_speed_dup(hdev, speed, HCLGE_MAC_FULL, 0);
3436 	}
3437 }
3438 
3439 static int hclge_get_status(struct hnae3_handle *handle)
3440 {
3441 	struct hclge_vport *vport = hclge_get_vport(handle);
3442 	struct hclge_dev *hdev = vport->back;
3443 
3444 	hclge_update_link_status(hdev);
3445 
3446 	return hdev->hw.mac.link;
3447 }
3448 
3449 struct hclge_vport *hclge_get_vf_vport(struct hclge_dev *hdev, int vf)
3450 {
3451 	if (!pci_num_vf(hdev->pdev)) {
3452 		dev_err(&hdev->pdev->dev,
3453 			"SRIOV is disabled, can not get vport(%d) info.\n", vf);
3454 		return NULL;
3455 	}
3456 
3457 	if (vf < 0 || vf >= pci_num_vf(hdev->pdev)) {
3458 		dev_err(&hdev->pdev->dev,
3459 			"vf id(%d) is out of range(0 <= vfid < %d)\n",
3460 			vf, pci_num_vf(hdev->pdev));
3461 		return NULL;
3462 	}
3463 
3464 	/* VF start from 1 in vport */
3465 	vf += HCLGE_VF_VPORT_START_NUM;
3466 	return &hdev->vport[vf];
3467 }
3468 
3469 static int hclge_get_vf_config(struct hnae3_handle *handle, int vf,
3470 			       struct ifla_vf_info *ivf)
3471 {
3472 	struct hclge_vport *vport = hclge_get_vport(handle);
3473 	struct hclge_dev *hdev = vport->back;
3474 
3475 	vport = hclge_get_vf_vport(hdev, vf);
3476 	if (!vport)
3477 		return -EINVAL;
3478 
3479 	ivf->vf = vf;
3480 	ivf->linkstate = vport->vf_info.link_state;
3481 	ivf->spoofchk = vport->vf_info.spoofchk;
3482 	ivf->trusted = vport->vf_info.trusted;
3483 	ivf->min_tx_rate = 0;
3484 	ivf->max_tx_rate = vport->vf_info.max_tx_rate;
3485 	ivf->vlan = vport->port_base_vlan_cfg.vlan_info.vlan_tag;
3486 	ivf->vlan_proto = htons(vport->port_base_vlan_cfg.vlan_info.vlan_proto);
3487 	ivf->qos = vport->port_base_vlan_cfg.vlan_info.qos;
3488 	ether_addr_copy(ivf->mac, vport->vf_info.mac);
3489 
3490 	return 0;
3491 }
3492 
3493 static int hclge_set_vf_link_state(struct hnae3_handle *handle, int vf,
3494 				   int link_state)
3495 {
3496 	struct hclge_vport *vport = hclge_get_vport(handle);
3497 	struct hclge_dev *hdev = vport->back;
3498 	int link_state_old;
3499 	int ret;
3500 
3501 	vport = hclge_get_vf_vport(hdev, vf);
3502 	if (!vport)
3503 		return -EINVAL;
3504 
3505 	link_state_old = vport->vf_info.link_state;
3506 	vport->vf_info.link_state = link_state;
3507 
3508 	/* return success directly if the VF is unalive, VF will
3509 	 * query link state itself when it starts work.
3510 	 */
3511 	if (!test_bit(HCLGE_VPORT_STATE_ALIVE, &vport->state))
3512 		return 0;
3513 
3514 	ret = hclge_push_vf_link_status(vport);
3515 	if (ret) {
3516 		vport->vf_info.link_state = link_state_old;
3517 		dev_err(&hdev->pdev->dev,
3518 			"failed to push vf%d link status, ret = %d\n", vf, ret);
3519 	}
3520 
3521 	return ret;
3522 }
3523 
3524 static int hclge_set_pfc_storm_para(struct hclge_dev *hdev,
3525 				    struct hnae3_pfc_storm_para *para)
3526 {
3527 	struct hclge_pfc_storm_para_cmd *para_cmd;
3528 	struct hclge_desc desc;
3529 	int ret;
3530 
3531 	if (hdev->ae_dev->dev_version < HNAE3_DEVICE_VERSION_V3)
3532 		return -EOPNOTSUPP;
3533 
3534 	hclge_cmd_setup_basic_desc(&desc, HCLGE_OPC_CFG_PAUSE_STORM_PARA,
3535 				   false);
3536 	para_cmd = (struct hclge_pfc_storm_para_cmd *)desc.data;
3537 	para_cmd->dir = cpu_to_le32(para->dir);
3538 	para_cmd->enable = cpu_to_le32(para->enable);
3539 	para_cmd->period_ms = cpu_to_le32(para->period_ms);
3540 	para_cmd->times = cpu_to_le32(para->times);
3541 	para_cmd->recovery_period_ms = cpu_to_le32(para->recovery_period_ms);
3542 
3543 	ret = hclge_cmd_send(&hdev->hw, &desc, 1);
3544 	if (ret)
3545 		dev_err(&hdev->pdev->dev,
3546 			"failed to set pfc storm para, ret = %d\n", ret);
3547 	return ret;
3548 }
3549 
3550 static int hclge_get_pfc_storm_para(struct hclge_dev *hdev,
3551 				    struct hnae3_pfc_storm_para *para)
3552 {
3553 	struct hclge_pfc_storm_para_cmd *para_cmd;
3554 	struct hclge_desc desc;
3555 	int ret;
3556 
3557 	if (hdev->ae_dev->dev_version < HNAE3_DEVICE_VERSION_V3)
3558 		return -EOPNOTSUPP;
3559 
3560 	hclge_cmd_setup_basic_desc(&desc, HCLGE_OPC_CFG_PAUSE_STORM_PARA, true);
3561 	para_cmd = (struct hclge_pfc_storm_para_cmd *)desc.data;
3562 	para_cmd->dir = cpu_to_le32(para->dir);
3563 	ret = hclge_cmd_send(&hdev->hw, &desc, 1);
3564 	if (ret) {
3565 		dev_err(&hdev->pdev->dev,
3566 			"failed to get pfc storm para, ret = %d\n", ret);
3567 		return ret;
3568 	}
3569 
3570 	para->enable = le32_to_cpu(para_cmd->enable);
3571 	para->period_ms = le32_to_cpu(para_cmd->period_ms);
3572 	para->times = le32_to_cpu(para_cmd->times);
3573 	para->recovery_period_ms = le32_to_cpu(para_cmd->recovery_period_ms);
3574 
3575 	return 0;
3576 }
3577 
3578 static int hclge_enable_pfc_storm_prevent(struct hclge_dev *hdev,
3579 					  int dir, bool enable)
3580 {
3581 	struct hnae3_pfc_storm_para para = {0};
3582 	int ret;
3583 
3584 	para.dir = dir;
3585 	ret = hclge_get_pfc_storm_para(hdev, &para);
3586 	if (ret)
3587 		return ret;
3588 
3589 	para.enable = enable;
3590 	return hclge_set_pfc_storm_para(hdev, &para);
3591 }
3592 
3593 static int hclge_set_pfc_prevention_tout(struct hnae3_handle *h, u16 times)
3594 {
3595 	struct hclge_vport *vport = hclge_get_vport(h);
3596 	struct hclge_dev *hdev = vport->back;
3597 	struct hnae3_pfc_storm_para para;
3598 	int ret;
3599 
3600 	if (times > HCLGE_MAX_PFC_PREVENTION_TOUT) {
3601 		dev_err(&hdev->pdev->dev,
3602 			"times %u should be no more than %u!\n",
3603 			times, HCLGE_MAX_PFC_PREVENTION_TOUT);
3604 		return -EINVAL;
3605 	}
3606 
3607 	para.dir = HCLGE_DIR_TX;
3608 	ret = hclge_get_pfc_storm_para(hdev, &para);
3609 	if (ret)
3610 		return ret;
3611 
3612 	para.enable = times ? 1 : 0;
3613 	para.times = (u32)times;
3614 	ret = hclge_set_pfc_storm_para(hdev, &para);
3615 	if (ret)
3616 		return ret;
3617 
3618 	hdev->pfc_prevention_tout = times;
3619 
3620 	return 0;
3621 }
3622 
3623 static int hclge_get_pfc_prevention_tout(struct hnae3_handle *h, u16 *times)
3624 {
3625 	struct hclge_vport *vport = hclge_get_vport(h);
3626 	struct hclge_dev *hdev = vport->back;
3627 	struct hnae3_pfc_storm_para para;
3628 	int ret;
3629 
3630 	para.dir = HCLGE_DIR_TX;
3631 	ret = hclge_get_pfc_storm_para(hdev, &para);
3632 	if (ret)
3633 		return ret;
3634 
3635 	*times = para.enable ? (u16)para.times : 0;
3636 
3637 	return 0;
3638 }
3639 
3640 static void hclge_set_reset_pending(struct hclge_dev *hdev,
3641 				    enum hnae3_reset_type reset_type)
3642 {
3643 	/* When an incorrect reset type is executed, the get_reset_level
3644 	 * function generates the HNAE3_NONE_RESET flag. As a result, this
3645 	 * type do not need to pending.
3646 	 */
3647 	if (reset_type != HNAE3_NONE_RESET)
3648 		set_bit(reset_type, &hdev->reset_pending);
3649 }
3650 
3651 static u32 hclge_check_event_cause(struct hclge_dev *hdev, u32 *clearval)
3652 {
3653 	u32 cmdq_src_reg, msix_src_reg, hw_err_src_reg;
3654 
3655 	/* fetch the events from their corresponding regs */
3656 	cmdq_src_reg = hclge_read_dev(&hdev->hw, HCLGE_VECTOR0_CMDQ_SRC_REG);
3657 	msix_src_reg = hclge_read_dev(&hdev->hw, HCLGE_MISC_VECTOR_INT_STS);
3658 	hw_err_src_reg = hclge_read_dev(&hdev->hw,
3659 					HCLGE_RAS_PF_OTHER_INT_STS_REG);
3660 
3661 	/* Assumption: If by any chance reset and mailbox events are reported
3662 	 * together then we will only process reset event in this go and will
3663 	 * defer the processing of the mailbox events. Since, we would have not
3664 	 * cleared RX CMDQ event this time we would receive again another
3665 	 * interrupt from H/W just for the mailbox.
3666 	 *
3667 	 * check for vector0 reset event sources
3668 	 */
3669 	if (BIT(HCLGE_VECTOR0_IMPRESET_INT_B) & msix_src_reg) {
3670 		dev_info(&hdev->pdev->dev, "IMP reset interrupt\n");
3671 		hclge_set_reset_pending(hdev, HNAE3_IMP_RESET);
3672 		set_bit(HCLGE_COMM_STATE_CMD_DISABLE, &hdev->hw.hw.comm_state);
3673 		*clearval = BIT(HCLGE_VECTOR0_IMPRESET_INT_B);
3674 		hdev->rst_stats.imp_rst_cnt++;
3675 		return HCLGE_VECTOR0_EVENT_RST;
3676 	}
3677 
3678 	if (BIT(HCLGE_VECTOR0_GLOBALRESET_INT_B) & msix_src_reg) {
3679 		dev_info(&hdev->pdev->dev, "global reset interrupt\n");
3680 		set_bit(HCLGE_COMM_STATE_CMD_DISABLE, &hdev->hw.hw.comm_state);
3681 		hclge_set_reset_pending(hdev, HNAE3_GLOBAL_RESET);
3682 		*clearval = BIT(HCLGE_VECTOR0_GLOBALRESET_INT_B);
3683 		hdev->rst_stats.global_rst_cnt++;
3684 		return HCLGE_VECTOR0_EVENT_RST;
3685 	}
3686 
3687 	/* check for vector0 msix event and hardware error event source */
3688 	if (msix_src_reg & HCLGE_VECTOR0_REG_MSIX_MASK ||
3689 	    hw_err_src_reg & HCLGE_RAS_REG_ERR_MASK)
3690 		return HCLGE_VECTOR0_EVENT_ERR;
3691 
3692 	/* check for vector0 ptp event source */
3693 	if (BIT(HCLGE_VECTOR0_REG_PTP_INT_B) & msix_src_reg) {
3694 		*clearval = msix_src_reg;
3695 		return HCLGE_VECTOR0_EVENT_PTP;
3696 	}
3697 
3698 	/* check for vector0 mailbox(=CMDQ RX) event source */
3699 	if (BIT(HCLGE_VECTOR0_RX_CMDQ_INT_B) & cmdq_src_reg) {
3700 		cmdq_src_reg &= ~BIT(HCLGE_VECTOR0_RX_CMDQ_INT_B);
3701 		*clearval = cmdq_src_reg;
3702 		return HCLGE_VECTOR0_EVENT_MBX;
3703 	}
3704 
3705 	/* print other vector0 event source */
3706 	dev_info(&hdev->pdev->dev,
3707 		 "INT status: CMDQ(%#x) HW errors(%#x) other(%#x)\n",
3708 		 cmdq_src_reg, hw_err_src_reg, msix_src_reg);
3709 
3710 	return HCLGE_VECTOR0_EVENT_OTHER;
3711 }
3712 
3713 static void hclge_clear_event_cause(struct hclge_dev *hdev, u32 event_type,
3714 				    u32 regclr)
3715 {
3716 #define HCLGE_IMP_RESET_DELAY		5
3717 
3718 	switch (event_type) {
3719 	case HCLGE_VECTOR0_EVENT_PTP:
3720 	case HCLGE_VECTOR0_EVENT_RST:
3721 		if (regclr == BIT(HCLGE_VECTOR0_IMPRESET_INT_B))
3722 			mdelay(HCLGE_IMP_RESET_DELAY);
3723 
3724 		hclge_write_dev(&hdev->hw, HCLGE_MISC_RESET_STS_REG, regclr);
3725 		break;
3726 	case HCLGE_VECTOR0_EVENT_MBX:
3727 		hclge_write_dev(&hdev->hw, HCLGE_VECTOR0_CMDQ_SRC_REG, regclr);
3728 		break;
3729 	default:
3730 		break;
3731 	}
3732 }
3733 
3734 static void hclge_clear_all_event_cause(struct hclge_dev *hdev)
3735 {
3736 	hclge_clear_event_cause(hdev, HCLGE_VECTOR0_EVENT_RST,
3737 				BIT(HCLGE_VECTOR0_GLOBALRESET_INT_B) |
3738 				BIT(HCLGE_VECTOR0_CORERESET_INT_B) |
3739 				BIT(HCLGE_VECTOR0_IMPRESET_INT_B));
3740 	hclge_clear_event_cause(hdev, HCLGE_VECTOR0_EVENT_MBX, 0);
3741 }
3742 
3743 static void hclge_enable_vector(struct hclge_misc_vector *vector, bool enable)
3744 {
3745 	writel(enable ? 1 : 0, vector->addr);
3746 }
3747 
3748 static irqreturn_t hclge_misc_irq_handle(int irq, void *data)
3749 {
3750 	struct hclge_dev *hdev = data;
3751 	unsigned long flags;
3752 	u32 clearval = 0;
3753 	u32 event_cause;
3754 
3755 	hclge_enable_vector(&hdev->misc_vector, false);
3756 	event_cause = hclge_check_event_cause(hdev, &clearval);
3757 
3758 	/* vector 0 interrupt is shared with reset and mailbox source events. */
3759 	switch (event_cause) {
3760 	case HCLGE_VECTOR0_EVENT_ERR:
3761 		hclge_errhand_task_schedule(hdev);
3762 		break;
3763 	case HCLGE_VECTOR0_EVENT_RST:
3764 		hclge_reset_task_schedule(hdev);
3765 		break;
3766 	case HCLGE_VECTOR0_EVENT_PTP:
3767 		spin_lock_irqsave(&hdev->ptp->lock, flags);
3768 		hclge_ptp_clean_tx_hwts(hdev);
3769 		spin_unlock_irqrestore(&hdev->ptp->lock, flags);
3770 		break;
3771 	case HCLGE_VECTOR0_EVENT_MBX:
3772 		/* If we are here then,
3773 		 * 1. Either we are not handling any mbx task and we are not
3774 		 *    scheduled as well
3775 		 *                        OR
3776 		 * 2. We could be handling a mbx task but nothing more is
3777 		 *    scheduled.
3778 		 * In both cases, we should schedule mbx task as there are more
3779 		 * mbx messages reported by this interrupt.
3780 		 */
3781 		hclge_mbx_task_schedule(hdev);
3782 		break;
3783 	default:
3784 		dev_warn(&hdev->pdev->dev,
3785 			 "received unknown or unhandled event of vector0\n");
3786 		break;
3787 	}
3788 
3789 	hclge_clear_event_cause(hdev, event_cause, clearval);
3790 
3791 	/* Enable interrupt if it is not caused by reset event or error event */
3792 	if (event_cause == HCLGE_VECTOR0_EVENT_PTP ||
3793 	    event_cause == HCLGE_VECTOR0_EVENT_MBX ||
3794 	    event_cause == HCLGE_VECTOR0_EVENT_OTHER)
3795 		hclge_enable_vector(&hdev->misc_vector, true);
3796 
3797 	return IRQ_HANDLED;
3798 }
3799 
3800 static void hclge_free_vector(struct hclge_dev *hdev, int vector_id)
3801 {
3802 	if (hdev->vector_status[vector_id] == HCLGE_INVALID_VPORT) {
3803 		dev_warn(&hdev->pdev->dev,
3804 			 "vector(vector_id %d) has been freed.\n", vector_id);
3805 		return;
3806 	}
3807 
3808 	hdev->vector_status[vector_id] = HCLGE_INVALID_VPORT;
3809 	hdev->num_msi_left += 1;
3810 	hdev->num_msi_used -= 1;
3811 }
3812 
3813 static void hclge_get_misc_vector(struct hclge_dev *hdev)
3814 {
3815 	struct hclge_misc_vector *vector = &hdev->misc_vector;
3816 
3817 	vector->vector_irq = pci_irq_vector(hdev->pdev, 0);
3818 
3819 	vector->addr = hdev->hw.hw.io_base + HCLGE_MISC_VECTOR_REG_BASE;
3820 	hdev->vector_status[0] = 0;
3821 
3822 	hdev->num_msi_left -= 1;
3823 	hdev->num_msi_used += 1;
3824 }
3825 
3826 static int hclge_misc_irq_init(struct hclge_dev *hdev)
3827 {
3828 	int ret;
3829 
3830 	hclge_get_misc_vector(hdev);
3831 
3832 	/* this would be explicitly freed in the end */
3833 	snprintf(hdev->misc_vector.name, HNAE3_INT_NAME_LEN, "%s-misc-%s",
3834 		 HCLGE_NAME, pci_name(hdev->pdev));
3835 	ret = request_irq(hdev->misc_vector.vector_irq, hclge_misc_irq_handle,
3836 			  IRQF_NO_AUTOEN, hdev->misc_vector.name, hdev);
3837 	if (ret) {
3838 		hclge_free_vector(hdev, 0);
3839 		dev_err(&hdev->pdev->dev, "request misc irq(%d) fail\n",
3840 			hdev->misc_vector.vector_irq);
3841 	}
3842 
3843 	return ret;
3844 }
3845 
3846 static void hclge_misc_irq_uninit(struct hclge_dev *hdev)
3847 {
3848 	free_irq(hdev->misc_vector.vector_irq, hdev);
3849 	hclge_free_vector(hdev, 0);
3850 }
3851 
3852 int hclge_notify_client(struct hclge_dev *hdev,
3853 			enum hnae3_reset_notify_type type)
3854 {
3855 	struct hnae3_handle *handle = &hdev->vport[0].nic;
3856 	struct hnae3_client *client = hdev->nic_client;
3857 	int ret;
3858 
3859 	if (!test_bit(HCLGE_STATE_NIC_REGISTERED, &hdev->state) || !client)
3860 		return 0;
3861 
3862 	if (!client->ops->reset_notify)
3863 		return -EOPNOTSUPP;
3864 
3865 	ret = client->ops->reset_notify(handle, type);
3866 	if (ret)
3867 		dev_err(&hdev->pdev->dev, "notify nic client failed %d(%d)\n",
3868 			type, ret);
3869 
3870 	return ret;
3871 }
3872 
3873 static int hclge_notify_roce_client(struct hclge_dev *hdev,
3874 				    enum hnae3_reset_notify_type type)
3875 {
3876 	struct hnae3_handle *handle = &hdev->vport[0].roce;
3877 	struct hnae3_client *client = hdev->roce_client;
3878 	int ret;
3879 
3880 	if (!test_bit(HCLGE_STATE_ROCE_REGISTERED, &hdev->state) || !client)
3881 		return 0;
3882 
3883 	if (!client->ops->reset_notify)
3884 		return -EOPNOTSUPP;
3885 
3886 	ret = client->ops->reset_notify(handle, type);
3887 	if (ret)
3888 		dev_err(&hdev->pdev->dev, "notify roce client failed %d(%d)",
3889 			type, ret);
3890 
3891 	return ret;
3892 }
3893 
3894 static int hclge_reset_wait(struct hclge_dev *hdev)
3895 {
3896 #define HCLGE_RESET_WATI_MS	100
3897 #define HCLGE_RESET_WAIT_CNT	350
3898 
3899 	u32 val, reg, reg_bit;
3900 	u32 cnt = 0;
3901 
3902 	switch (hdev->reset_type) {
3903 	case HNAE3_IMP_RESET:
3904 		reg = HCLGE_GLOBAL_RESET_REG;
3905 		reg_bit = HCLGE_IMP_RESET_BIT;
3906 		break;
3907 	case HNAE3_GLOBAL_RESET:
3908 		reg = HCLGE_GLOBAL_RESET_REG;
3909 		reg_bit = HCLGE_GLOBAL_RESET_BIT;
3910 		break;
3911 	case HNAE3_FUNC_RESET:
3912 		reg = HCLGE_FUN_RST_ING;
3913 		reg_bit = HCLGE_FUN_RST_ING_B;
3914 		break;
3915 	default:
3916 		dev_err(&hdev->pdev->dev,
3917 			"Wait for unsupported reset type: %d\n",
3918 			hdev->reset_type);
3919 		return -EINVAL;
3920 	}
3921 
3922 	val = hclge_read_dev(&hdev->hw, reg);
3923 	while (hnae3_get_bit(val, reg_bit) && cnt < HCLGE_RESET_WAIT_CNT) {
3924 		msleep(HCLGE_RESET_WATI_MS);
3925 		val = hclge_read_dev(&hdev->hw, reg);
3926 		cnt++;
3927 	}
3928 
3929 	if (cnt >= HCLGE_RESET_WAIT_CNT) {
3930 		dev_warn(&hdev->pdev->dev,
3931 			 "Wait for reset timeout: %d\n", hdev->reset_type);
3932 		return -EBUSY;
3933 	}
3934 
3935 	return 0;
3936 }
3937 
3938 static int hclge_set_vf_rst(struct hclge_dev *hdev, int func_id, bool reset)
3939 {
3940 	struct hclge_vf_rst_cmd *req;
3941 	struct hclge_desc desc;
3942 
3943 	req = (struct hclge_vf_rst_cmd *)desc.data;
3944 	hclge_cmd_setup_basic_desc(&desc, HCLGE_OPC_GBL_RST_STATUS, false);
3945 	req->dest_vfid = func_id;
3946 
3947 	if (reset)
3948 		req->vf_rst = 0x1;
3949 
3950 	return hclge_cmd_send(&hdev->hw, &desc, 1);
3951 }
3952 
3953 static int hclge_set_all_vf_rst(struct hclge_dev *hdev, bool reset)
3954 {
3955 	int i;
3956 
3957 	for (i = HCLGE_VF_VPORT_START_NUM; i < hdev->num_alloc_vport; i++) {
3958 		struct hclge_vport *vport = &hdev->vport[i];
3959 		int ret;
3960 
3961 		/* Send cmd to set/clear VF's FUNC_RST_ING */
3962 		ret = hclge_set_vf_rst(hdev, vport->vport_id, reset);
3963 		if (ret) {
3964 			dev_err(&hdev->pdev->dev,
3965 				"set vf(%u) rst failed %d!\n",
3966 				vport->vport_id - HCLGE_VF_VPORT_START_NUM,
3967 				ret);
3968 			return ret;
3969 		}
3970 
3971 		if (!reset ||
3972 		    !test_bit(HCLGE_VPORT_STATE_INITED, &vport->state))
3973 			continue;
3974 
3975 		if (!test_bit(HCLGE_VPORT_STATE_ALIVE, &vport->state) &&
3976 		    hdev->reset_type == HNAE3_FUNC_RESET) {
3977 			set_bit(HCLGE_VPORT_NEED_NOTIFY_RESET,
3978 				&vport->need_notify);
3979 			continue;
3980 		}
3981 
3982 		/* Inform VF to process the reset.
3983 		 * hclge_inform_reset_assert_to_vf may fail if VF
3984 		 * driver is not loaded.
3985 		 */
3986 		ret = hclge_inform_reset_assert_to_vf(vport);
3987 		if (ret)
3988 			dev_warn(&hdev->pdev->dev,
3989 				 "inform reset to vf(%u) failed %d!\n",
3990 				 vport->vport_id - HCLGE_VF_VPORT_START_NUM,
3991 				 ret);
3992 	}
3993 
3994 	return 0;
3995 }
3996 
3997 static void hclge_mailbox_service_task(struct hclge_dev *hdev)
3998 {
3999 	if (!test_and_clear_bit(HCLGE_STATE_MBX_SERVICE_SCHED, &hdev->state) ||
4000 	    test_bit(HCLGE_COMM_STATE_CMD_DISABLE, &hdev->hw.hw.comm_state) ||
4001 	    test_and_set_bit(HCLGE_STATE_MBX_HANDLING, &hdev->state))
4002 		return;
4003 
4004 	if (time_is_before_jiffies(hdev->last_mbx_scheduled +
4005 				   HCLGE_MBX_SCHED_TIMEOUT))
4006 		dev_warn(&hdev->pdev->dev,
4007 			 "mbx service task is scheduled after %ums on cpu%u!\n",
4008 			 jiffies_to_msecs(jiffies - hdev->last_mbx_scheduled),
4009 			 smp_processor_id());
4010 
4011 	hclge_mbx_handler(hdev);
4012 
4013 	clear_bit(HCLGE_STATE_MBX_HANDLING, &hdev->state);
4014 }
4015 
4016 static void hclge_func_reset_sync_vf(struct hclge_dev *hdev)
4017 {
4018 	struct hclge_pf_rst_sync_cmd *req;
4019 	struct hclge_desc desc;
4020 	int cnt = 0;
4021 	int ret;
4022 
4023 	req = (struct hclge_pf_rst_sync_cmd *)desc.data;
4024 	hclge_cmd_setup_basic_desc(&desc, HCLGE_OPC_QUERY_VF_RST_RDY, true);
4025 
4026 	do {
4027 		/* vf need to down netdev by mbx during PF or FLR reset */
4028 		hclge_mailbox_service_task(hdev);
4029 
4030 		ret = hclge_cmd_send(&hdev->hw, &desc, 1);
4031 		/* for compatible with old firmware, wait
4032 		 * 100 ms for VF to stop IO
4033 		 */
4034 		if (ret == -EOPNOTSUPP) {
4035 			msleep(HCLGE_RESET_SYNC_TIME);
4036 			return;
4037 		} else if (ret) {
4038 			dev_warn(&hdev->pdev->dev, "sync with VF fail %d!\n",
4039 				 ret);
4040 			return;
4041 		} else if (req->all_vf_ready) {
4042 			return;
4043 		}
4044 		msleep(HCLGE_PF_RESET_SYNC_TIME);
4045 		hclge_comm_cmd_reuse_desc(&desc, true);
4046 	} while (cnt++ < HCLGE_PF_RESET_SYNC_CNT);
4047 
4048 	dev_warn(&hdev->pdev->dev, "sync with VF timeout!\n");
4049 }
4050 
4051 void hclge_report_hw_error(struct hclge_dev *hdev,
4052 			   enum hnae3_hw_error_type type)
4053 {
4054 	struct hnae3_client *client = hdev->nic_client;
4055 
4056 	if (!client || !client->ops->process_hw_error ||
4057 	    !test_bit(HCLGE_STATE_NIC_REGISTERED, &hdev->state))
4058 		return;
4059 
4060 	client->ops->process_hw_error(&hdev->vport[0].nic, type);
4061 }
4062 
4063 static void hclge_handle_imp_error(struct hclge_dev *hdev)
4064 {
4065 	u32 reg_val;
4066 
4067 	reg_val = hclge_read_dev(&hdev->hw, HCLGE_PF_OTHER_INT_REG);
4068 	if (reg_val & BIT(HCLGE_VECTOR0_IMP_RD_POISON_B)) {
4069 		hclge_report_hw_error(hdev, HNAE3_IMP_RD_POISON_ERROR);
4070 		reg_val &= ~BIT(HCLGE_VECTOR0_IMP_RD_POISON_B);
4071 		hclge_write_dev(&hdev->hw, HCLGE_PF_OTHER_INT_REG, reg_val);
4072 	}
4073 
4074 	if (reg_val & BIT(HCLGE_VECTOR0_IMP_CMDQ_ERR_B)) {
4075 		hclge_report_hw_error(hdev, HNAE3_CMDQ_ECC_ERROR);
4076 		reg_val &= ~BIT(HCLGE_VECTOR0_IMP_CMDQ_ERR_B);
4077 		hclge_write_dev(&hdev->hw, HCLGE_PF_OTHER_INT_REG, reg_val);
4078 	}
4079 }
4080 
4081 int hclge_func_reset_cmd(struct hclge_dev *hdev, int func_id)
4082 {
4083 	struct hclge_desc desc;
4084 	struct hclge_reset_cmd *req = (struct hclge_reset_cmd *)desc.data;
4085 	int ret;
4086 
4087 	hclge_cmd_setup_basic_desc(&desc, HCLGE_OPC_CFG_RST_TRIGGER, false);
4088 	hnae3_set_bit(req->mac_func_reset, HCLGE_CFG_RESET_FUNC_B, 1);
4089 	req->fun_reset_vfid = func_id;
4090 
4091 	ret = hclge_cmd_send(&hdev->hw, &desc, 1);
4092 	if (ret)
4093 		dev_err(&hdev->pdev->dev,
4094 			"send function reset cmd fail, status =%d\n", ret);
4095 
4096 	return ret;
4097 }
4098 
4099 static void hclge_do_reset(struct hclge_dev *hdev)
4100 {
4101 	struct hnae3_handle *handle = &hdev->vport[0].nic;
4102 	struct pci_dev *pdev = hdev->pdev;
4103 	u32 val;
4104 
4105 	if (hclge_get_hw_reset_stat(handle)) {
4106 		dev_info(&pdev->dev, "hardware reset not finish\n");
4107 		dev_info(&pdev->dev, "func_rst_reg:0x%x, global_rst_reg:0x%x\n",
4108 			 hclge_read_dev(&hdev->hw, HCLGE_FUN_RST_ING),
4109 			 hclge_read_dev(&hdev->hw, HCLGE_GLOBAL_RESET_REG));
4110 		return;
4111 	}
4112 
4113 	switch (hdev->reset_type) {
4114 	case HNAE3_IMP_RESET:
4115 		dev_info(&pdev->dev, "IMP reset requested\n");
4116 		val = hclge_read_dev(&hdev->hw, HCLGE_PF_OTHER_INT_REG);
4117 		hnae3_set_bit(val, HCLGE_TRIGGER_IMP_RESET_B, 1);
4118 		hclge_write_dev(&hdev->hw, HCLGE_PF_OTHER_INT_REG, val);
4119 		break;
4120 	case HNAE3_GLOBAL_RESET:
4121 		dev_info(&pdev->dev, "global reset requested\n");
4122 		val = hclge_read_dev(&hdev->hw, HCLGE_GLOBAL_RESET_REG);
4123 		hnae3_set_bit(val, HCLGE_GLOBAL_RESET_BIT, 1);
4124 		hclge_write_dev(&hdev->hw, HCLGE_GLOBAL_RESET_REG, val);
4125 		break;
4126 	case HNAE3_FUNC_RESET:
4127 		dev_info(&pdev->dev, "PF reset requested\n");
4128 		/* schedule again to check later */
4129 		hclge_set_reset_pending(hdev, HNAE3_FUNC_RESET);
4130 		hclge_reset_task_schedule(hdev);
4131 		break;
4132 	default:
4133 		dev_warn(&pdev->dev,
4134 			 "unsupported reset type: %d\n", hdev->reset_type);
4135 		break;
4136 	}
4137 }
4138 
4139 static enum hnae3_reset_type hclge_get_reset_level(struct hnae3_ae_dev *ae_dev,
4140 						   unsigned long *addr)
4141 {
4142 	enum hnae3_reset_type rst_level = HNAE3_NONE_RESET;
4143 	struct hclge_dev *hdev = ae_dev->priv;
4144 
4145 	/* return the highest priority reset level amongst all */
4146 	if (test_bit(HNAE3_IMP_RESET, addr)) {
4147 		rst_level = HNAE3_IMP_RESET;
4148 		clear_bit(HNAE3_IMP_RESET, addr);
4149 		clear_bit(HNAE3_GLOBAL_RESET, addr);
4150 		clear_bit(HNAE3_FUNC_RESET, addr);
4151 	} else if (test_bit(HNAE3_GLOBAL_RESET, addr)) {
4152 		rst_level = HNAE3_GLOBAL_RESET;
4153 		clear_bit(HNAE3_GLOBAL_RESET, addr);
4154 		clear_bit(HNAE3_FUNC_RESET, addr);
4155 	} else if (test_bit(HNAE3_FUNC_RESET, addr)) {
4156 		rst_level = HNAE3_FUNC_RESET;
4157 		clear_bit(HNAE3_FUNC_RESET, addr);
4158 	} else if (test_bit(HNAE3_FLR_RESET, addr)) {
4159 		rst_level = HNAE3_FLR_RESET;
4160 		clear_bit(HNAE3_FLR_RESET, addr);
4161 	}
4162 
4163 	clear_bit(HNAE3_NONE_RESET, addr);
4164 
4165 	if (hdev->reset_type != HNAE3_NONE_RESET &&
4166 	    rst_level < hdev->reset_type)
4167 		return HNAE3_NONE_RESET;
4168 
4169 	return rst_level;
4170 }
4171 
4172 static void hclge_clear_reset_cause(struct hclge_dev *hdev)
4173 {
4174 	u32 clearval = 0;
4175 
4176 	switch (hdev->reset_type) {
4177 	case HNAE3_IMP_RESET:
4178 		clearval = BIT(HCLGE_VECTOR0_IMPRESET_INT_B);
4179 		break;
4180 	case HNAE3_GLOBAL_RESET:
4181 		clearval = BIT(HCLGE_VECTOR0_GLOBALRESET_INT_B);
4182 		break;
4183 	default:
4184 		break;
4185 	}
4186 
4187 	if (!clearval)
4188 		return;
4189 
4190 	/* For revision 0x20, the reset interrupt source
4191 	 * can only be cleared after hardware reset done
4192 	 */
4193 	if (hdev->ae_dev->dev_version < HNAE3_DEVICE_VERSION_V2)
4194 		hclge_write_dev(&hdev->hw, HCLGE_MISC_RESET_STS_REG,
4195 				clearval);
4196 
4197 	hclge_enable_vector(&hdev->misc_vector, true);
4198 }
4199 
4200 static void hclge_reset_handshake(struct hclge_dev *hdev, bool enable)
4201 {
4202 	u32 reg_val;
4203 
4204 	reg_val = hclge_read_dev(&hdev->hw, HCLGE_COMM_NIC_CSQ_DEPTH_REG);
4205 	if (enable)
4206 		reg_val |= HCLGE_COMM_NIC_SW_RST_RDY;
4207 	else
4208 		reg_val &= ~HCLGE_COMM_NIC_SW_RST_RDY;
4209 
4210 	hclge_write_dev(&hdev->hw, HCLGE_COMM_NIC_CSQ_DEPTH_REG, reg_val);
4211 }
4212 
4213 static int hclge_func_reset_notify_vf(struct hclge_dev *hdev)
4214 {
4215 	int ret;
4216 
4217 	ret = hclge_set_all_vf_rst(hdev, true);
4218 	if (ret)
4219 		return ret;
4220 
4221 	hclge_func_reset_sync_vf(hdev);
4222 
4223 	return 0;
4224 }
4225 
4226 static int hclge_reset_prepare_wait(struct hclge_dev *hdev)
4227 {
4228 	u32 reg_val;
4229 	int ret = 0;
4230 
4231 	switch (hdev->reset_type) {
4232 	case HNAE3_FUNC_RESET:
4233 		ret = hclge_func_reset_notify_vf(hdev);
4234 		if (ret)
4235 			return ret;
4236 
4237 		ret = hclge_func_reset_cmd(hdev, 0);
4238 		if (ret) {
4239 			dev_err(&hdev->pdev->dev,
4240 				"asserting function reset fail %d!\n", ret);
4241 			return ret;
4242 		}
4243 
4244 		/* After performaning pf reset, it is not necessary to do the
4245 		 * mailbox handling or send any command to firmware, because
4246 		 * any mailbox handling or command to firmware is only valid
4247 		 * after hclge_comm_cmd_init is called.
4248 		 */
4249 		set_bit(HCLGE_COMM_STATE_CMD_DISABLE, &hdev->hw.hw.comm_state);
4250 		hdev->rst_stats.pf_rst_cnt++;
4251 		break;
4252 	case HNAE3_FLR_RESET:
4253 		ret = hclge_func_reset_notify_vf(hdev);
4254 		if (ret)
4255 			return ret;
4256 		break;
4257 	case HNAE3_IMP_RESET:
4258 		hclge_handle_imp_error(hdev);
4259 		reg_val = hclge_read_dev(&hdev->hw, HCLGE_PF_OTHER_INT_REG);
4260 		hclge_write_dev(&hdev->hw, HCLGE_PF_OTHER_INT_REG,
4261 				BIT(HCLGE_VECTOR0_IMP_RESET_INT_B) | reg_val);
4262 		break;
4263 	default:
4264 		break;
4265 	}
4266 
4267 	/* inform hardware that preparatory work is done */
4268 	msleep(HCLGE_RESET_SYNC_TIME);
4269 	hclge_reset_handshake(hdev, true);
4270 	dev_info(&hdev->pdev->dev, "prepare wait ok\n");
4271 
4272 	return ret;
4273 }
4274 
4275 static void hclge_show_rst_info(struct hclge_dev *hdev)
4276 {
4277 	char *buf;
4278 
4279 	buf = kzalloc(HCLGE_DBG_RESET_INFO_LEN, GFP_KERNEL);
4280 	if (!buf)
4281 		return;
4282 
4283 	hclge_dbg_dump_rst_info(hdev, buf, HCLGE_DBG_RESET_INFO_LEN);
4284 
4285 	dev_info(&hdev->pdev->dev, "dump reset info:\n%s", buf);
4286 
4287 	kfree(buf);
4288 }
4289 
4290 static bool hclge_reset_err_handle(struct hclge_dev *hdev)
4291 {
4292 #define MAX_RESET_FAIL_CNT 5
4293 
4294 	if (hdev->reset_pending) {
4295 		dev_info(&hdev->pdev->dev, "Reset pending %lu\n",
4296 			 hdev->reset_pending);
4297 		return true;
4298 	} else if (hclge_read_dev(&hdev->hw, HCLGE_MISC_VECTOR_INT_STS) &
4299 		   HCLGE_RESET_INT_M) {
4300 		dev_info(&hdev->pdev->dev,
4301 			 "reset failed because new reset interrupt\n");
4302 		hclge_clear_reset_cause(hdev);
4303 		return false;
4304 	} else if (hdev->rst_stats.reset_fail_cnt < MAX_RESET_FAIL_CNT) {
4305 		hdev->rst_stats.reset_fail_cnt++;
4306 		hclge_set_reset_pending(hdev, hdev->reset_type);
4307 		dev_info(&hdev->pdev->dev,
4308 			 "re-schedule reset task(%u)\n",
4309 			 hdev->rst_stats.reset_fail_cnt);
4310 		return true;
4311 	}
4312 
4313 	hclge_clear_reset_cause(hdev);
4314 
4315 	/* recover the handshake status when reset fail */
4316 	hclge_reset_handshake(hdev, true);
4317 
4318 	dev_err(&hdev->pdev->dev, "Reset fail!\n");
4319 
4320 	hclge_show_rst_info(hdev);
4321 
4322 	set_bit(HCLGE_STATE_RST_FAIL, &hdev->state);
4323 
4324 	return false;
4325 }
4326 
4327 static void hclge_update_reset_level(struct hclge_dev *hdev)
4328 {
4329 	struct hnae3_ae_dev *ae_dev = pci_get_drvdata(hdev->pdev);
4330 	enum hnae3_reset_type reset_level;
4331 
4332 	/* reset request will not be set during reset, so clear
4333 	 * pending reset request to avoid unnecessary reset
4334 	 * caused by the same reason.
4335 	 */
4336 	hclge_get_reset_level(ae_dev, &hdev->reset_request);
4337 
4338 	/* if default_reset_request has a higher level reset request,
4339 	 * it should be handled as soon as possible. since some errors
4340 	 * need this kind of reset to fix.
4341 	 */
4342 	reset_level = hclge_get_reset_level(ae_dev,
4343 					    &hdev->default_reset_request);
4344 	if (reset_level != HNAE3_NONE_RESET)
4345 		set_bit(reset_level, &hdev->reset_request);
4346 }
4347 
4348 static int hclge_set_rst_done(struct hclge_dev *hdev)
4349 {
4350 	struct hclge_pf_rst_done_cmd *req;
4351 	struct hclge_desc desc;
4352 	int ret;
4353 
4354 	req = (struct hclge_pf_rst_done_cmd *)desc.data;
4355 	hclge_cmd_setup_basic_desc(&desc, HCLGE_OPC_PF_RST_DONE, false);
4356 	req->pf_rst_done |= HCLGE_PF_RESET_DONE_BIT;
4357 
4358 	ret = hclge_cmd_send(&hdev->hw, &desc, 1);
4359 	/* To be compatible with the old firmware, which does not support
4360 	 * command HCLGE_OPC_PF_RST_DONE, just print a warning and
4361 	 * return success
4362 	 */
4363 	if (ret == -EOPNOTSUPP) {
4364 		dev_warn(&hdev->pdev->dev,
4365 			 "current firmware does not support command(0x%x)!\n",
4366 			 HCLGE_OPC_PF_RST_DONE);
4367 		return 0;
4368 	} else if (ret) {
4369 		dev_err(&hdev->pdev->dev, "assert PF reset done fail %d!\n",
4370 			ret);
4371 	}
4372 
4373 	return ret;
4374 }
4375 
4376 static int hclge_reset_prepare_up(struct hclge_dev *hdev)
4377 {
4378 	int ret = 0;
4379 
4380 	switch (hdev->reset_type) {
4381 	case HNAE3_FUNC_RESET:
4382 	case HNAE3_FLR_RESET:
4383 		ret = hclge_set_all_vf_rst(hdev, false);
4384 		break;
4385 	case HNAE3_GLOBAL_RESET:
4386 	case HNAE3_IMP_RESET:
4387 		ret = hclge_set_rst_done(hdev);
4388 		break;
4389 	default:
4390 		break;
4391 	}
4392 
4393 	/* clear up the handshake status after re-initialize done */
4394 	hclge_reset_handshake(hdev, false);
4395 
4396 	return ret;
4397 }
4398 
4399 static int hclge_reset_stack(struct hclge_dev *hdev)
4400 {
4401 	int ret;
4402 
4403 	ret = hclge_notify_client(hdev, HNAE3_UNINIT_CLIENT);
4404 	if (ret)
4405 		return ret;
4406 
4407 	ret = hclge_reset_ae_dev(hdev->ae_dev);
4408 	if (ret)
4409 		return ret;
4410 
4411 	return hclge_notify_client(hdev, HNAE3_INIT_CLIENT);
4412 }
4413 
4414 static int hclge_reset_prepare(struct hclge_dev *hdev)
4415 {
4416 	int ret;
4417 
4418 	hdev->rst_stats.reset_cnt++;
4419 	/* perform reset of the stack & ae device for a client */
4420 	ret = hclge_notify_roce_client(hdev, HNAE3_DOWN_CLIENT);
4421 	if (ret)
4422 		return ret;
4423 
4424 	rtnl_lock();
4425 	ret = hclge_notify_client(hdev, HNAE3_DOWN_CLIENT);
4426 	rtnl_unlock();
4427 	if (ret)
4428 		return ret;
4429 
4430 	return hclge_reset_prepare_wait(hdev);
4431 }
4432 
4433 static void hclge_restore_pfc_storm_prevention_tout(struct hclge_dev *hdev)
4434 {
4435 	struct hnae3_handle *handle = &hdev->vport[0].nic;
4436 	int ret;
4437 
4438 	ret = hclge_enable_pfc_storm_prevent(hdev, HCLGE_DIR_RX, false);
4439 	if (ret == -EOPNOTSUPP)
4440 		return;
4441 	else if (ret)
4442 		dev_warn(&hdev->pdev->dev,
4443 			 "failed to disable rx pfc storm prevent, ret = %d\n",
4444 			 ret);
4445 
4446 	ret = hclge_set_pfc_prevention_tout(handle, hdev->pfc_prevention_tout);
4447 	if (ret)
4448 		dev_warn(&hdev->pdev->dev,
4449 			 "failed to set tx pfc storm prevent, ret = %d\n",
4450 			 ret);
4451 }
4452 
4453 static int hclge_reset_rebuild(struct hclge_dev *hdev)
4454 {
4455 	int ret;
4456 
4457 	hdev->rst_stats.hw_reset_done_cnt++;
4458 
4459 	ret = hclge_notify_roce_client(hdev, HNAE3_UNINIT_CLIENT);
4460 	if (ret)
4461 		return ret;
4462 
4463 	rtnl_lock();
4464 	ret = hclge_reset_stack(hdev);
4465 	rtnl_unlock();
4466 	if (ret)
4467 		return ret;
4468 
4469 	hclge_clear_reset_cause(hdev);
4470 
4471 	ret = hclge_notify_roce_client(hdev, HNAE3_INIT_CLIENT);
4472 	/* ignore RoCE notify error if it fails HCLGE_RESET_MAX_FAIL_CNT - 1
4473 	 * times
4474 	 */
4475 	if (ret &&
4476 	    hdev->rst_stats.reset_fail_cnt < HCLGE_RESET_MAX_FAIL_CNT - 1)
4477 		return ret;
4478 
4479 	ret = hclge_reset_prepare_up(hdev);
4480 	if (ret)
4481 		return ret;
4482 
4483 	rtnl_lock();
4484 	ret = hclge_notify_client(hdev, HNAE3_UP_CLIENT);
4485 	rtnl_unlock();
4486 	if (ret)
4487 		return ret;
4488 
4489 	ret = hclge_notify_roce_client(hdev, HNAE3_UP_CLIENT);
4490 	if (ret)
4491 		return ret;
4492 
4493 	hdev->last_reset_time = jiffies;
4494 	hdev->rst_stats.reset_fail_cnt = 0;
4495 	hdev->rst_stats.reset_done_cnt++;
4496 	clear_bit(HCLGE_STATE_RST_FAIL, &hdev->state);
4497 
4498 	hclge_update_reset_level(hdev);
4499 
4500 	return 0;
4501 }
4502 
4503 static void hclge_reset(struct hclge_dev *hdev)
4504 {
4505 	if (hclge_reset_prepare(hdev))
4506 		goto err_reset;
4507 
4508 	if (hclge_reset_wait(hdev))
4509 		goto err_reset;
4510 
4511 	if (hclge_reset_rebuild(hdev))
4512 		goto err_reset;
4513 
4514 	return;
4515 
4516 err_reset:
4517 	if (hclge_reset_err_handle(hdev))
4518 		hclge_reset_task_schedule(hdev);
4519 }
4520 
4521 static void hclge_reset_event(struct pci_dev *pdev, struct hnae3_handle *handle)
4522 {
4523 	struct hnae3_ae_dev *ae_dev = pci_get_drvdata(pdev);
4524 	struct hclge_dev *hdev = ae_dev->priv;
4525 
4526 	/* We might end up getting called broadly because of 2 below cases:
4527 	 * 1. Recoverable error was conveyed through APEI and only way to bring
4528 	 *    normalcy is to reset.
4529 	 * 2. A new reset request from the stack due to timeout
4530 	 *
4531 	 * check if this is a new reset request and we are not here just because
4532 	 * last reset attempt did not succeed and watchdog hit us again. We will
4533 	 * know this if last reset request did not occur very recently (watchdog
4534 	 * timer = 5*HZ, let us check after sufficiently large time, say 4*5*Hz)
4535 	 * In case of new request we reset the "reset level" to PF reset.
4536 	 * And if it is a repeat reset request of the most recent one then we
4537 	 * want to make sure we throttle the reset request. Therefore, we will
4538 	 * not allow it again before 3*HZ times.
4539 	 */
4540 
4541 	if (time_before(jiffies, (hdev->last_reset_time +
4542 				  HCLGE_RESET_INTERVAL))) {
4543 		mod_timer(&hdev->reset_timer, jiffies + HCLGE_RESET_INTERVAL);
4544 		return;
4545 	}
4546 
4547 	if (hdev->default_reset_request) {
4548 		hdev->reset_level =
4549 			hclge_get_reset_level(ae_dev,
4550 					      &hdev->default_reset_request);
4551 	} else if (time_after(jiffies, (hdev->last_reset_time + 4 * 5 * HZ))) {
4552 		hdev->reset_level = HNAE3_FUNC_RESET;
4553 	}
4554 
4555 	dev_info(&hdev->pdev->dev, "received reset event, reset type is %d\n",
4556 		 hdev->reset_level);
4557 
4558 	/* request reset & schedule reset task */
4559 	set_bit(hdev->reset_level, &hdev->reset_request);
4560 	hclge_reset_task_schedule(hdev);
4561 
4562 	if (hdev->reset_level < HNAE3_GLOBAL_RESET)
4563 		hdev->reset_level++;
4564 }
4565 
4566 static void hclge_set_def_reset_request(struct hnae3_ae_dev *ae_dev,
4567 					enum hnae3_reset_type rst_type)
4568 {
4569 #define HCLGE_SUPPORT_RESET_TYPE \
4570 	(BIT(HNAE3_FLR_RESET) | BIT(HNAE3_FUNC_RESET) | \
4571 	BIT(HNAE3_GLOBAL_RESET) | BIT(HNAE3_IMP_RESET))
4572 
4573 	struct hclge_dev *hdev = ae_dev->priv;
4574 
4575 	if (!(BIT(rst_type) & HCLGE_SUPPORT_RESET_TYPE)) {
4576 		/* To prevent reset triggered by hclge_reset_event */
4577 		set_bit(HNAE3_NONE_RESET, &hdev->default_reset_request);
4578 		dev_warn(&hdev->pdev->dev, "unsupported reset type %d\n",
4579 			 rst_type);
4580 		return;
4581 	}
4582 
4583 	set_bit(rst_type, &hdev->default_reset_request);
4584 }
4585 
4586 static void hclge_reset_timer(struct timer_list *t)
4587 {
4588 	struct hclge_dev *hdev = timer_container_of(hdev, t, reset_timer);
4589 
4590 	/* if default_reset_request has no value, it means that this reset
4591 	 * request has already be handled, so just return here
4592 	 */
4593 	if (!hdev->default_reset_request)
4594 		return;
4595 
4596 	dev_info(&hdev->pdev->dev,
4597 		 "triggering reset in reset timer\n");
4598 	hclge_reset_event(hdev->pdev, NULL);
4599 }
4600 
4601 static void hclge_reset_subtask(struct hclge_dev *hdev)
4602 {
4603 	struct hnae3_ae_dev *ae_dev = pci_get_drvdata(hdev->pdev);
4604 
4605 	/* check if there is any ongoing reset in the hardware. This status can
4606 	 * be checked from reset_pending. If there is then, we need to wait for
4607 	 * hardware to complete reset.
4608 	 *    a. If we are able to figure out in reasonable time that hardware
4609 	 *       has fully resetted then, we can proceed with driver, client
4610 	 *       reset.
4611 	 *    b. else, we can come back later to check this status so re-sched
4612 	 *       now.
4613 	 */
4614 	hdev->last_reset_time = jiffies;
4615 	hdev->reset_type = hclge_get_reset_level(ae_dev, &hdev->reset_pending);
4616 	if (hdev->reset_type != HNAE3_NONE_RESET)
4617 		hclge_reset(hdev);
4618 
4619 	/* check if we got any *new* reset requests to be honored */
4620 	hdev->reset_type = hclge_get_reset_level(ae_dev, &hdev->reset_request);
4621 	if (hdev->reset_type != HNAE3_NONE_RESET)
4622 		hclge_do_reset(hdev);
4623 
4624 	hdev->reset_type = HNAE3_NONE_RESET;
4625 }
4626 
4627 static void hclge_handle_err_reset_request(struct hclge_dev *hdev)
4628 {
4629 	struct hnae3_ae_dev *ae_dev = pci_get_drvdata(hdev->pdev);
4630 	enum hnae3_reset_type reset_type;
4631 
4632 	if (ae_dev->hw_err_reset_req) {
4633 		reset_type = hclge_get_reset_level(ae_dev,
4634 						   &ae_dev->hw_err_reset_req);
4635 		hclge_set_def_reset_request(ae_dev, reset_type);
4636 	}
4637 
4638 	if (hdev->default_reset_request && ae_dev->ops->reset_event)
4639 		ae_dev->ops->reset_event(hdev->pdev, NULL);
4640 
4641 	/* enable interrupt after error handling complete */
4642 	hclge_enable_vector(&hdev->misc_vector, true);
4643 }
4644 
4645 static void hclge_handle_err_recovery(struct hclge_dev *hdev)
4646 {
4647 	struct hnae3_ae_dev *ae_dev = pci_get_drvdata(hdev->pdev);
4648 
4649 	ae_dev->hw_err_reset_req = 0;
4650 
4651 	if (hclge_find_error_source(hdev)) {
4652 		hclge_handle_error_info_log(ae_dev);
4653 		hclge_handle_mac_tnl(hdev);
4654 		hclge_handle_vf_queue_err_ras(hdev);
4655 	}
4656 
4657 	hclge_handle_err_reset_request(hdev);
4658 }
4659 
4660 static void hclge_misc_err_recovery(struct hclge_dev *hdev)
4661 {
4662 	struct hnae3_ae_dev *ae_dev = pci_get_drvdata(hdev->pdev);
4663 	struct device *dev = &hdev->pdev->dev;
4664 	u32 msix_sts_reg;
4665 
4666 	msix_sts_reg = hclge_read_dev(&hdev->hw, HCLGE_MISC_VECTOR_INT_STS);
4667 	if (msix_sts_reg & HCLGE_VECTOR0_REG_MSIX_MASK) {
4668 		if (hclge_handle_hw_msix_error
4669 				(hdev, &hdev->default_reset_request))
4670 			dev_info(dev, "received msix interrupt 0x%x\n",
4671 				 msix_sts_reg);
4672 	}
4673 
4674 	hclge_handle_hw_ras_error(ae_dev);
4675 
4676 	hclge_handle_err_reset_request(hdev);
4677 }
4678 
4679 static void hclge_errhand_service_task(struct hclge_dev *hdev)
4680 {
4681 	if (!test_and_clear_bit(HCLGE_STATE_ERR_SERVICE_SCHED, &hdev->state))
4682 		return;
4683 
4684 	if (hnae3_dev_ras_imp_supported(hdev))
4685 		hclge_handle_err_recovery(hdev);
4686 	else
4687 		hclge_misc_err_recovery(hdev);
4688 }
4689 
4690 static void hclge_reset_service_task(struct hclge_dev *hdev)
4691 {
4692 	if (!test_and_clear_bit(HCLGE_STATE_RST_SERVICE_SCHED, &hdev->state))
4693 		return;
4694 
4695 	if (time_is_before_jiffies(hdev->last_rst_scheduled +
4696 				   HCLGE_RESET_SCHED_TIMEOUT))
4697 		dev_warn(&hdev->pdev->dev,
4698 			 "reset service task is scheduled after %ums on cpu%u!\n",
4699 			 jiffies_to_msecs(jiffies - hdev->last_rst_scheduled),
4700 			 smp_processor_id());
4701 
4702 	down(&hdev->reset_sem);
4703 	set_bit(HCLGE_STATE_RST_HANDLING, &hdev->state);
4704 
4705 	hclge_reset_subtask(hdev);
4706 
4707 	clear_bit(HCLGE_STATE_RST_HANDLING, &hdev->state);
4708 	up(&hdev->reset_sem);
4709 }
4710 
4711 static void hclge_update_vport_alive(struct hclge_dev *hdev)
4712 {
4713 #define HCLGE_ALIVE_SECONDS_NORMAL		8
4714 
4715 	unsigned long alive_time = HCLGE_ALIVE_SECONDS_NORMAL * HZ;
4716 	int i;
4717 
4718 	/* start from vport 1 for PF is always alive */
4719 	for (i = 1; i < hdev->num_alloc_vport; i++) {
4720 		struct hclge_vport *vport = &hdev->vport[i];
4721 
4722 		if (!test_bit(HCLGE_VPORT_STATE_INITED, &vport->state) ||
4723 		    !test_bit(HCLGE_VPORT_STATE_ALIVE, &vport->state))
4724 			continue;
4725 		if (time_after(jiffies, vport->last_active_jiffies +
4726 			       alive_time)) {
4727 			clear_bit(HCLGE_VPORT_STATE_ALIVE, &vport->state);
4728 			dev_warn(&hdev->pdev->dev,
4729 				 "VF %u heartbeat timeout\n",
4730 				 i - HCLGE_VF_VPORT_START_NUM);
4731 		}
4732 	}
4733 }
4734 
4735 static void hclge_periodic_service_task(struct hclge_dev *hdev)
4736 {
4737 	unsigned long delta = round_jiffies_relative(HZ);
4738 
4739 	if (test_bit(HCLGE_STATE_RST_FAIL, &hdev->state))
4740 		return;
4741 
4742 	/* Always handle the link updating to make sure link state is
4743 	 * updated when it is triggered by mbx.
4744 	 */
4745 	hclge_update_link_status(hdev);
4746 	hclge_sync_mac_table(hdev);
4747 	hclge_sync_promisc_mode(hdev);
4748 	hclge_sync_fd_table(hdev);
4749 
4750 	if (time_is_after_jiffies(hdev->last_serv_processed + HZ)) {
4751 		delta = jiffies - hdev->last_serv_processed;
4752 
4753 		if (delta < round_jiffies_relative(HZ)) {
4754 			delta = round_jiffies_relative(HZ) - delta;
4755 			goto out;
4756 		}
4757 	}
4758 
4759 	hdev->serv_processed_cnt++;
4760 	hclge_update_vport_alive(hdev);
4761 
4762 	if (test_bit(HCLGE_STATE_DOWN, &hdev->state)) {
4763 		hdev->last_serv_processed = jiffies;
4764 		goto out;
4765 	}
4766 
4767 	if (!(hdev->serv_processed_cnt % HCLGE_STATS_TIMER_INTERVAL))
4768 		hclge_update_stats_for_all(hdev);
4769 
4770 	hclge_update_port_info(hdev);
4771 	hclge_sync_vlan_filter(hdev);
4772 
4773 	if (!(hdev->serv_processed_cnt % HCLGE_ARFS_EXPIRE_INTERVAL))
4774 		hclge_rfs_filter_expire(hdev);
4775 
4776 	hdev->last_serv_processed = jiffies;
4777 
4778 out:
4779 	hclge_task_schedule(hdev, delta);
4780 }
4781 
4782 static void hclge_ptp_service_task(struct hclge_dev *hdev)
4783 {
4784 	unsigned long flags;
4785 
4786 	if (!test_bit(HCLGE_STATE_PTP_EN, &hdev->state) ||
4787 	    !test_bit(HCLGE_STATE_PTP_TX_HANDLING, &hdev->state) ||
4788 	    !time_is_before_jiffies(hdev->ptp->tx_start + HZ))
4789 		return;
4790 
4791 	/* to prevent concurrence with the irq handler */
4792 	spin_lock_irqsave(&hdev->ptp->lock, flags);
4793 
4794 	/* check HCLGE_STATE_PTP_TX_HANDLING here again, since the irq
4795 	 * handler may handle it just before spin_lock_irqsave().
4796 	 */
4797 	if (test_bit(HCLGE_STATE_PTP_TX_HANDLING, &hdev->state))
4798 		hclge_ptp_clean_tx_hwts(hdev);
4799 
4800 	spin_unlock_irqrestore(&hdev->ptp->lock, flags);
4801 }
4802 
4803 static void hclge_service_task(struct work_struct *work)
4804 {
4805 	struct hclge_dev *hdev =
4806 		container_of(work, struct hclge_dev, service_task.work);
4807 
4808 	hclge_errhand_service_task(hdev);
4809 	hclge_reset_service_task(hdev);
4810 	hclge_ptp_service_task(hdev);
4811 	hclge_mailbox_service_task(hdev);
4812 	hclge_periodic_service_task(hdev);
4813 
4814 	/* Handle error recovery, reset and mbx again in case periodical task
4815 	 * delays the handling by calling hclge_task_schedule() in
4816 	 * hclge_periodic_service_task().
4817 	 */
4818 	hclge_errhand_service_task(hdev);
4819 	hclge_reset_service_task(hdev);
4820 	hclge_mailbox_service_task(hdev);
4821 }
4822 
4823 struct hclge_vport *hclge_get_vport(struct hnae3_handle *handle)
4824 {
4825 	/* VF handle has no client */
4826 	if (!handle->client)
4827 		return container_of(handle, struct hclge_vport, nic);
4828 	else if (handle->client->type == HNAE3_CLIENT_ROCE)
4829 		return container_of(handle, struct hclge_vport, roce);
4830 	else
4831 		return container_of(handle, struct hclge_vport, nic);
4832 }
4833 
4834 static void hclge_get_vector_info(struct hclge_dev *hdev, u16 idx,
4835 				  struct hnae3_vector_info *vector_info)
4836 {
4837 #define HCLGE_PF_MAX_VECTOR_NUM_DEV_V2	64
4838 
4839 	vector_info->vector = pci_irq_vector(hdev->pdev, idx);
4840 
4841 	/* need an extend offset to config vector >= 64 */
4842 	if (idx - 1 < HCLGE_PF_MAX_VECTOR_NUM_DEV_V2)
4843 		vector_info->io_addr = hdev->hw.hw.io_base +
4844 				HCLGE_VECTOR_REG_BASE +
4845 				(idx - 1) * HCLGE_VECTOR_REG_OFFSET;
4846 	else
4847 		vector_info->io_addr = hdev->hw.hw.io_base +
4848 				HCLGE_VECTOR_EXT_REG_BASE +
4849 				(idx - 1) / HCLGE_PF_MAX_VECTOR_NUM_DEV_V2 *
4850 				HCLGE_VECTOR_REG_OFFSET_H +
4851 				(idx - 1) % HCLGE_PF_MAX_VECTOR_NUM_DEV_V2 *
4852 				HCLGE_VECTOR_REG_OFFSET;
4853 
4854 	hdev->vector_status[idx] = hdev->vport[0].vport_id;
4855 	hdev->vector_irq[idx] = vector_info->vector;
4856 }
4857 
4858 static int hclge_get_vector(struct hnae3_handle *handle, u16 vector_num,
4859 			    struct hnae3_vector_info *vector_info)
4860 {
4861 	struct hclge_vport *vport = hclge_get_vport(handle);
4862 	struct hnae3_vector_info *vector = vector_info;
4863 	struct hclge_dev *hdev = vport->back;
4864 	int alloc = 0;
4865 	u16 i = 0;
4866 	u16 j;
4867 
4868 	vector_num = min_t(u16, hdev->num_nic_msi - 1, vector_num);
4869 	vector_num = min(hdev->num_msi_left, vector_num);
4870 
4871 	for (j = 0; j < vector_num; j++) {
4872 		while (++i < hdev->num_nic_msi) {
4873 			if (hdev->vector_status[i] == HCLGE_INVALID_VPORT) {
4874 				hclge_get_vector_info(hdev, i, vector);
4875 				vector++;
4876 				alloc++;
4877 
4878 				break;
4879 			}
4880 		}
4881 	}
4882 	hdev->num_msi_left -= alloc;
4883 	hdev->num_msi_used += alloc;
4884 
4885 	return alloc;
4886 }
4887 
4888 static int hclge_get_vector_index(struct hclge_dev *hdev, int vector)
4889 {
4890 	int i;
4891 
4892 	for (i = 0; i < hdev->num_msi; i++)
4893 		if (vector == hdev->vector_irq[i])
4894 			return i;
4895 
4896 	return -EINVAL;
4897 }
4898 
4899 static int hclge_put_vector(struct hnae3_handle *handle, int vector)
4900 {
4901 	struct hclge_vport *vport = hclge_get_vport(handle);
4902 	struct hclge_dev *hdev = vport->back;
4903 	int vector_id;
4904 
4905 	vector_id = hclge_get_vector_index(hdev, vector);
4906 	if (vector_id < 0) {
4907 		dev_err(&hdev->pdev->dev,
4908 			"Get vector index fail. vector = %d\n", vector);
4909 		return vector_id;
4910 	}
4911 
4912 	hclge_free_vector(hdev, vector_id);
4913 
4914 	return 0;
4915 }
4916 
4917 static int hclge_get_rss(struct hnae3_handle *handle, u32 *indir,
4918 			 u8 *key, u8 *hfunc)
4919 {
4920 	struct hnae3_ae_dev *ae_dev = pci_get_drvdata(handle->pdev);
4921 	struct hclge_vport *vport = hclge_get_vport(handle);
4922 	struct hclge_comm_rss_cfg *rss_cfg = &vport->back->rss_cfg;
4923 
4924 	hclge_comm_get_rss_hash_info(rss_cfg, key, hfunc);
4925 
4926 	hclge_comm_get_rss_indir_tbl(rss_cfg, indir,
4927 				     ae_dev->dev_specs.rss_ind_tbl_size);
4928 
4929 	return 0;
4930 }
4931 
4932 static int hclge_set_rss(struct hnae3_handle *handle, const u32 *indir,
4933 			 const  u8 *key, const  u8 hfunc)
4934 {
4935 	struct hnae3_ae_dev *ae_dev = pci_get_drvdata(handle->pdev);
4936 	struct hclge_vport *vport = hclge_get_vport(handle);
4937 	struct hclge_dev *hdev = vport->back;
4938 	struct hclge_comm_rss_cfg *rss_cfg = &hdev->rss_cfg;
4939 	int ret, i;
4940 
4941 	ret = hclge_comm_set_rss_hash_key(rss_cfg, &hdev->hw.hw, key, hfunc);
4942 	if (ret) {
4943 		dev_err(&hdev->pdev->dev, "invalid hfunc type %u\n", hfunc);
4944 		return ret;
4945 	}
4946 
4947 	/* Update the shadow RSS table with user specified qids */
4948 	for (i = 0; i < ae_dev->dev_specs.rss_ind_tbl_size; i++)
4949 		rss_cfg->rss_indirection_tbl[i] = indir[i];
4950 
4951 	/* Update the hardware */
4952 	return hclge_comm_set_rss_indir_table(ae_dev, &hdev->hw.hw,
4953 					      rss_cfg->rss_indirection_tbl);
4954 }
4955 
4956 static int hclge_set_rss_tuple(struct hnae3_handle *handle,
4957 			       const struct ethtool_rxfh_fields *nfc)
4958 {
4959 	struct hclge_vport *vport = hclge_get_vport(handle);
4960 	struct hclge_dev *hdev = vport->back;
4961 	int ret;
4962 
4963 	ret = hclge_comm_set_rss_tuple(hdev->ae_dev, &hdev->hw.hw,
4964 				       &hdev->rss_cfg, nfc);
4965 	if (ret) {
4966 		dev_err(&hdev->pdev->dev,
4967 			"failed to set rss tuple, ret = %d.\n", ret);
4968 		return ret;
4969 	}
4970 
4971 	return 0;
4972 }
4973 
4974 static int hclge_get_rss_tuple(struct hnae3_handle *handle,
4975 			       struct ethtool_rxfh_fields *nfc)
4976 {
4977 	struct hclge_vport *vport = hclge_get_vport(handle);
4978 	u8 tuple_sets;
4979 	int ret;
4980 
4981 	nfc->data = 0;
4982 
4983 	ret = hclge_comm_get_rss_tuple(&vport->back->rss_cfg, nfc->flow_type,
4984 				       &tuple_sets);
4985 	if (ret || !tuple_sets)
4986 		return ret;
4987 
4988 	nfc->data = hclge_comm_convert_rss_tuple(tuple_sets);
4989 
4990 	return 0;
4991 }
4992 
4993 static int hclge_get_tc_size(struct hnae3_handle *handle)
4994 {
4995 	struct hclge_vport *vport = hclge_get_vport(handle);
4996 	struct hclge_dev *hdev = vport->back;
4997 
4998 	return hdev->pf_rss_size_max;
4999 }
5000 
5001 static int hclge_init_rss_tc_mode(struct hclge_dev *hdev)
5002 {
5003 	struct hnae3_ae_dev *ae_dev = hdev->ae_dev;
5004 	struct hclge_vport *vport = hdev->vport;
5005 	u16 tc_offset[HCLGE_MAX_TC_NUM] = {0};
5006 	u16 tc_valid[HCLGE_MAX_TC_NUM] = {0};
5007 	u16 tc_size[HCLGE_MAX_TC_NUM] = {0};
5008 	struct hnae3_tc_info *tc_info;
5009 	u16 roundup_size;
5010 	u16 rss_size;
5011 	int i;
5012 
5013 	tc_info = &vport->nic.kinfo.tc_info;
5014 	for (i = 0; i < HCLGE_MAX_TC_NUM; i++) {
5015 		rss_size = tc_info->tqp_count[i];
5016 		tc_valid[i] = 0;
5017 
5018 		if (!(hdev->hw_tc_map & BIT(i)))
5019 			continue;
5020 
5021 		/* tc_size set to hardware is the log2 of roundup power of two
5022 		 * of rss_size, the acutal queue size is limited by indirection
5023 		 * table.
5024 		 */
5025 		if (rss_size > ae_dev->dev_specs.rss_ind_tbl_size ||
5026 		    rss_size == 0) {
5027 			dev_err(&hdev->pdev->dev,
5028 				"Configure rss tc size failed, invalid TC_SIZE = %u\n",
5029 				rss_size);
5030 			return -EINVAL;
5031 		}
5032 
5033 		roundup_size = roundup_pow_of_two(rss_size);
5034 		roundup_size = ilog2(roundup_size);
5035 
5036 		tc_valid[i] = 1;
5037 		tc_size[i] = roundup_size;
5038 		tc_offset[i] = tc_info->tqp_offset[i];
5039 	}
5040 
5041 	return hclge_comm_set_rss_tc_mode(&hdev->hw.hw, tc_offset, tc_valid,
5042 					  tc_size);
5043 }
5044 
5045 int hclge_rss_init_hw(struct hclge_dev *hdev)
5046 {
5047 	u16 *rss_indir = hdev->rss_cfg.rss_indirection_tbl;
5048 	u8 *key = hdev->rss_cfg.rss_hash_key;
5049 	u8 hfunc = hdev->rss_cfg.rss_algo;
5050 	int ret;
5051 
5052 	ret = hclge_comm_set_rss_indir_table(hdev->ae_dev, &hdev->hw.hw,
5053 					     rss_indir);
5054 	if (ret)
5055 		return ret;
5056 
5057 	ret = hclge_comm_set_rss_algo_key(&hdev->hw.hw, hfunc, key);
5058 	if (ret)
5059 		return ret;
5060 
5061 	ret = hclge_comm_set_rss_input_tuple(&hdev->hw.hw, &hdev->rss_cfg);
5062 	if (ret)
5063 		return ret;
5064 
5065 	return hclge_init_rss_tc_mode(hdev);
5066 }
5067 
5068 int hclge_bind_ring_with_vector(struct hclge_vport *vport,
5069 				int vector_id, bool en,
5070 				struct hnae3_ring_chain_node *ring_chain)
5071 {
5072 	struct hclge_dev *hdev = vport->back;
5073 	struct hnae3_ring_chain_node *node;
5074 	struct hclge_desc desc;
5075 	struct hclge_ctrl_vector_chain_cmd *req =
5076 		(struct hclge_ctrl_vector_chain_cmd *)desc.data;
5077 	enum hclge_comm_cmd_status status;
5078 	enum hclge_opcode_type op;
5079 	u16 tqp_type_and_id;
5080 	int i;
5081 
5082 	op = en ? HCLGE_OPC_ADD_RING_TO_VECTOR : HCLGE_OPC_DEL_RING_TO_VECTOR;
5083 	hclge_cmd_setup_basic_desc(&desc, op, false);
5084 	req->int_vector_id_l = hnae3_get_field(vector_id,
5085 					       HCLGE_VECTOR_ID_L_M,
5086 					       HCLGE_VECTOR_ID_L_S);
5087 	req->int_vector_id_h = hnae3_get_field(vector_id,
5088 					       HCLGE_VECTOR_ID_H_M,
5089 					       HCLGE_VECTOR_ID_H_S);
5090 
5091 	i = 0;
5092 	for (node = ring_chain; node; node = node->next) {
5093 		tqp_type_and_id = le16_to_cpu(req->tqp_type_and_id[i]);
5094 		hnae3_set_field(tqp_type_and_id,  HCLGE_INT_TYPE_M,
5095 				HCLGE_INT_TYPE_S,
5096 				hnae3_get_bit(node->flag, HNAE3_RING_TYPE_B));
5097 		hnae3_set_field(tqp_type_and_id, HCLGE_TQP_ID_M,
5098 				HCLGE_TQP_ID_S, node->tqp_index);
5099 		hnae3_set_field(tqp_type_and_id, HCLGE_INT_GL_IDX_M,
5100 				HCLGE_INT_GL_IDX_S,
5101 				hnae3_get_field(node->int_gl_idx,
5102 						HNAE3_RING_GL_IDX_M,
5103 						HNAE3_RING_GL_IDX_S));
5104 		req->tqp_type_and_id[i] = cpu_to_le16(tqp_type_and_id);
5105 		if (++i >= HCLGE_VECTOR_ELEMENTS_PER_CMD) {
5106 			req->int_cause_num = HCLGE_VECTOR_ELEMENTS_PER_CMD;
5107 			req->vfid = vport->vport_id;
5108 
5109 			status = hclge_cmd_send(&hdev->hw, &desc, 1);
5110 			if (status) {
5111 				dev_err(&hdev->pdev->dev,
5112 					"Map TQP fail, status is %d.\n",
5113 					status);
5114 				return -EIO;
5115 			}
5116 			i = 0;
5117 
5118 			hclge_cmd_setup_basic_desc(&desc,
5119 						   op,
5120 						   false);
5121 			req->int_vector_id_l =
5122 				hnae3_get_field(vector_id,
5123 						HCLGE_VECTOR_ID_L_M,
5124 						HCLGE_VECTOR_ID_L_S);
5125 			req->int_vector_id_h =
5126 				hnae3_get_field(vector_id,
5127 						HCLGE_VECTOR_ID_H_M,
5128 						HCLGE_VECTOR_ID_H_S);
5129 		}
5130 	}
5131 
5132 	if (i > 0) {
5133 		req->int_cause_num = i;
5134 		req->vfid = vport->vport_id;
5135 		status = hclge_cmd_send(&hdev->hw, &desc, 1);
5136 		if (status) {
5137 			dev_err(&hdev->pdev->dev,
5138 				"Map TQP fail, status is %d.\n", status);
5139 			return -EIO;
5140 		}
5141 	}
5142 
5143 	return 0;
5144 }
5145 
5146 static int hclge_map_ring_to_vector(struct hnae3_handle *handle, int vector,
5147 				    struct hnae3_ring_chain_node *ring_chain)
5148 {
5149 	struct hclge_vport *vport = hclge_get_vport(handle);
5150 	struct hclge_dev *hdev = vport->back;
5151 	int vector_id;
5152 
5153 	vector_id = hclge_get_vector_index(hdev, vector);
5154 	if (vector_id < 0) {
5155 		dev_err(&hdev->pdev->dev,
5156 			"failed to get vector index. vector=%d\n", vector);
5157 		return vector_id;
5158 	}
5159 
5160 	return hclge_bind_ring_with_vector(vport, vector_id, true, ring_chain);
5161 }
5162 
5163 static int hclge_unmap_ring_frm_vector(struct hnae3_handle *handle, int vector,
5164 				       struct hnae3_ring_chain_node *ring_chain)
5165 {
5166 	struct hclge_vport *vport = hclge_get_vport(handle);
5167 	struct hclge_dev *hdev = vport->back;
5168 	int vector_id, ret;
5169 
5170 	if (test_bit(HCLGE_STATE_RST_HANDLING, &hdev->state))
5171 		return 0;
5172 
5173 	vector_id = hclge_get_vector_index(hdev, vector);
5174 	if (vector_id < 0) {
5175 		dev_err(&handle->pdev->dev,
5176 			"Get vector index fail. ret =%d\n", vector_id);
5177 		return vector_id;
5178 	}
5179 
5180 	ret = hclge_bind_ring_with_vector(vport, vector_id, false, ring_chain);
5181 	if (ret)
5182 		dev_err(&handle->pdev->dev,
5183 			"Unmap ring from vector fail. vectorid=%d, ret =%d\n",
5184 			vector_id, ret);
5185 
5186 	return ret;
5187 }
5188 
5189 static int hclge_cmd_set_promisc_mode(struct hclge_dev *hdev, u8 vf_id,
5190 				      bool en_uc, bool en_mc, bool en_bc)
5191 {
5192 	struct hclge_vport *vport = &hdev->vport[vf_id];
5193 	struct hnae3_handle *handle = &vport->nic;
5194 	struct hclge_promisc_cfg_cmd *req;
5195 	struct hclge_desc desc;
5196 	bool uc_tx_en = en_uc;
5197 	u8 promisc_cfg = 0;
5198 	int ret;
5199 
5200 	hclge_cmd_setup_basic_desc(&desc, HCLGE_OPC_CFG_PROMISC_MODE, false);
5201 
5202 	req = (struct hclge_promisc_cfg_cmd *)desc.data;
5203 	req->vf_id = vf_id;
5204 
5205 	if (test_bit(HNAE3_PFLAG_LIMIT_PROMISC, &handle->priv_flags))
5206 		uc_tx_en = false;
5207 
5208 	hnae3_set_bit(promisc_cfg, HCLGE_PROMISC_UC_RX_EN, en_uc ? 1 : 0);
5209 	hnae3_set_bit(promisc_cfg, HCLGE_PROMISC_MC_RX_EN, en_mc ? 1 : 0);
5210 	hnae3_set_bit(promisc_cfg, HCLGE_PROMISC_BC_RX_EN, en_bc ? 1 : 0);
5211 	hnae3_set_bit(promisc_cfg, HCLGE_PROMISC_UC_TX_EN, uc_tx_en ? 1 : 0);
5212 	hnae3_set_bit(promisc_cfg, HCLGE_PROMISC_MC_TX_EN, en_mc ? 1 : 0);
5213 	hnae3_set_bit(promisc_cfg, HCLGE_PROMISC_BC_TX_EN, en_bc ? 1 : 0);
5214 	req->extend_promisc = promisc_cfg;
5215 
5216 	/* to be compatible with DEVICE_VERSION_V1/2 */
5217 	promisc_cfg = 0;
5218 	hnae3_set_bit(promisc_cfg, HCLGE_PROMISC_EN_UC, en_uc ? 1 : 0);
5219 	hnae3_set_bit(promisc_cfg, HCLGE_PROMISC_EN_MC, en_mc ? 1 : 0);
5220 	hnae3_set_bit(promisc_cfg, HCLGE_PROMISC_EN_BC, en_bc ? 1 : 0);
5221 	hnae3_set_bit(promisc_cfg, HCLGE_PROMISC_TX_EN, 1);
5222 	hnae3_set_bit(promisc_cfg, HCLGE_PROMISC_RX_EN, 1);
5223 	req->promisc = promisc_cfg;
5224 
5225 	ret = hclge_cmd_send(&hdev->hw, &desc, 1);
5226 	if (ret)
5227 		dev_err(&hdev->pdev->dev,
5228 			"failed to set vport %u promisc mode, ret = %d.\n",
5229 			vf_id, ret);
5230 
5231 	return ret;
5232 }
5233 
5234 int hclge_set_vport_promisc_mode(struct hclge_vport *vport, bool en_uc_pmc,
5235 				 bool en_mc_pmc, bool en_bc_pmc)
5236 {
5237 	return hclge_cmd_set_promisc_mode(vport->back, vport->vport_id,
5238 					  en_uc_pmc, en_mc_pmc, en_bc_pmc);
5239 }
5240 
5241 static int hclge_set_promisc_mode(struct hnae3_handle *handle, bool en_uc_pmc,
5242 				  bool en_mc_pmc)
5243 {
5244 	struct hclge_vport *vport = hclge_get_vport(handle);
5245 	struct hclge_dev *hdev = vport->back;
5246 	bool en_bc_pmc = true;
5247 
5248 	/* For device whose version below V2, if broadcast promisc enabled,
5249 	 * vlan filter is always bypassed. So broadcast promisc should be
5250 	 * disabled until user enable promisc mode
5251 	 */
5252 	if (hdev->ae_dev->dev_version < HNAE3_DEVICE_VERSION_V2)
5253 		en_bc_pmc = handle->netdev_flags & HNAE3_BPE ? true : false;
5254 
5255 	return hclge_set_vport_promisc_mode(vport, en_uc_pmc, en_mc_pmc,
5256 					    en_bc_pmc);
5257 }
5258 
5259 static void hclge_request_update_promisc_mode(struct hnae3_handle *handle)
5260 {
5261 	struct hclge_vport *vport = hclge_get_vport(handle);
5262 
5263 	set_bit(HCLGE_VPORT_STATE_PROMISC_CHANGE, &vport->state);
5264 }
5265 
5266 static bool hclge_get_hw_reset_stat(struct hnae3_handle *handle)
5267 {
5268 	struct hclge_vport *vport = hclge_get_vport(handle);
5269 	struct hclge_dev *hdev = vport->back;
5270 
5271 	return hclge_read_dev(&hdev->hw, HCLGE_GLOBAL_RESET_REG) ||
5272 	       hclge_read_dev(&hdev->hw, HCLGE_FUN_RST_ING);
5273 }
5274 
5275 static bool hclge_get_cmdq_stat(struct hnae3_handle *handle)
5276 {
5277 	struct hclge_vport *vport = hclge_get_vport(handle);
5278 	struct hclge_dev *hdev = vport->back;
5279 
5280 	return test_bit(HCLGE_COMM_STATE_CMD_DISABLE, &hdev->hw.hw.comm_state);
5281 }
5282 
5283 static bool hclge_ae_dev_resetting(struct hnae3_handle *handle)
5284 {
5285 	struct hclge_vport *vport = hclge_get_vport(handle);
5286 	struct hclge_dev *hdev = vport->back;
5287 
5288 	return test_bit(HCLGE_STATE_RST_HANDLING, &hdev->state);
5289 }
5290 
5291 static unsigned long hclge_ae_dev_reset_cnt(struct hnae3_handle *handle)
5292 {
5293 	struct hclge_vport *vport = hclge_get_vport(handle);
5294 	struct hclge_dev *hdev = vport->back;
5295 
5296 	return hdev->rst_stats.hw_reset_done_cnt;
5297 }
5298 
5299 static void hclge_cfg_mac_mode(struct hclge_dev *hdev, bool enable)
5300 {
5301 #define HCLGE_LINK_STATUS_WAIT_CNT  3
5302 
5303 	struct hclge_desc desc;
5304 	struct hclge_config_mac_mode_cmd *req =
5305 		(struct hclge_config_mac_mode_cmd *)desc.data;
5306 	u32 loop_en = 0;
5307 	int ret;
5308 
5309 	hclge_cmd_setup_basic_desc(&desc, HCLGE_OPC_CONFIG_MAC_MODE, false);
5310 
5311 	if (enable) {
5312 		hnae3_set_bit(loop_en, HCLGE_MAC_TX_EN_B, 1U);
5313 		hnae3_set_bit(loop_en, HCLGE_MAC_RX_EN_B, 1U);
5314 		hnae3_set_bit(loop_en, HCLGE_MAC_PAD_TX_B, 1U);
5315 		hnae3_set_bit(loop_en, HCLGE_MAC_PAD_RX_B, 1U);
5316 		hnae3_set_bit(loop_en, HCLGE_MAC_FCS_TX_B, 1U);
5317 		hnae3_set_bit(loop_en, HCLGE_MAC_RX_FCS_B, 1U);
5318 		hnae3_set_bit(loop_en, HCLGE_MAC_RX_FCS_STRIP_B, 1U);
5319 		hnae3_set_bit(loop_en, HCLGE_MAC_TX_OVERSIZE_TRUNCATE_B, 1U);
5320 		hnae3_set_bit(loop_en, HCLGE_MAC_RX_OVERSIZE_TRUNCATE_B, 1U);
5321 		hnae3_set_bit(loop_en, HCLGE_MAC_TX_UNDER_MIN_ERR_B, 1U);
5322 	}
5323 
5324 	req->txrx_pad_fcs_loop_en = cpu_to_le32(loop_en);
5325 
5326 	ret = hclge_cmd_send(&hdev->hw, &desc, 1);
5327 	if (ret) {
5328 		dev_err(&hdev->pdev->dev,
5329 			"mac enable fail, ret =%d.\n", ret);
5330 		return;
5331 	}
5332 
5333 	if (!enable)
5334 		hclge_mac_link_status_wait(hdev, HCLGE_LINK_STATUS_DOWN,
5335 					   HCLGE_LINK_STATUS_WAIT_CNT);
5336 }
5337 
5338 u32 hclge_get_port_number(enum HLCGE_PORT_TYPE port_type, u8 pf_id,
5339 			  u8 vf_id, u8 network_port_id)
5340 {
5341 	u32 port_number = 0;
5342 
5343 	if (port_type == HOST_PORT) {
5344 		hnae3_set_field(port_number, HCLGE_PF_ID_M, HCLGE_PF_ID_S,
5345 				pf_id);
5346 		hnae3_set_field(port_number, HCLGE_VF_ID_M, HCLGE_VF_ID_S,
5347 				vf_id);
5348 		hnae3_set_bit(port_number, HCLGE_PORT_TYPE_B, HOST_PORT);
5349 	} else {
5350 		hnae3_set_field(port_number, HCLGE_NETWORK_PORT_ID_M,
5351 				HCLGE_NETWORK_PORT_ID_S, network_port_id);
5352 		hnae3_set_bit(port_number, HCLGE_PORT_TYPE_B, NETWORK_PORT);
5353 	}
5354 
5355 	return port_number;
5356 }
5357 
5358 static int hclge_config_switch_param(struct hclge_dev *hdev, int vfid,
5359 				     u8 switch_param, u8 param_mask)
5360 {
5361 	struct hclge_mac_vlan_switch_cmd *req;
5362 	struct hclge_desc desc;
5363 	u32 func_id;
5364 	int ret;
5365 
5366 	func_id = hclge_get_port_number(HOST_PORT, 0, vfid, 0);
5367 	req = (struct hclge_mac_vlan_switch_cmd *)desc.data;
5368 
5369 	/* read current config parameter */
5370 	hclge_cmd_setup_basic_desc(&desc, HCLGE_OPC_MAC_VLAN_SWITCH_PARAM,
5371 				   true);
5372 	req->roce_sel = HCLGE_MAC_VLAN_NIC_SEL;
5373 	req->func_id = cpu_to_le32(func_id);
5374 
5375 	ret = hclge_cmd_send(&hdev->hw, &desc, 1);
5376 	if (ret) {
5377 		dev_err(&hdev->pdev->dev,
5378 			"read mac vlan switch parameter fail, ret = %d\n", ret);
5379 		return ret;
5380 	}
5381 
5382 	/* modify and write new config parameter */
5383 	hclge_comm_cmd_reuse_desc(&desc, false);
5384 	req->switch_param = (req->switch_param & param_mask) | switch_param;
5385 	req->param_mask = param_mask;
5386 
5387 	ret = hclge_cmd_send(&hdev->hw, &desc, 1);
5388 	if (ret)
5389 		dev_err(&hdev->pdev->dev,
5390 			"set mac vlan switch parameter fail, ret = %d\n", ret);
5391 	return ret;
5392 }
5393 
5394 static void hclge_phy_link_status_wait(struct hclge_dev *hdev,
5395 				       int link_ret)
5396 {
5397 #define HCLGE_PHY_LINK_STATUS_NUM  200
5398 
5399 	struct phy_device *phydev = hdev->hw.mac.phydev;
5400 	int i = 0;
5401 	int ret;
5402 
5403 	do {
5404 		ret = phy_read_status(phydev);
5405 		if (ret) {
5406 			dev_err(&hdev->pdev->dev,
5407 				"phy update link status fail, ret = %d\n", ret);
5408 			return;
5409 		}
5410 
5411 		if (phydev->link == link_ret)
5412 			break;
5413 
5414 		msleep(HCLGE_LINK_STATUS_MS);
5415 	} while (++i < HCLGE_PHY_LINK_STATUS_NUM);
5416 }
5417 
5418 static int hclge_mac_link_status_wait(struct hclge_dev *hdev, int link_ret,
5419 				      int wait_cnt)
5420 {
5421 	int link_status;
5422 	int i = 0;
5423 	int ret;
5424 
5425 	do {
5426 		ret = hclge_get_mac_link_status(hdev, &link_status);
5427 		if (ret)
5428 			return ret;
5429 		if (link_status == link_ret)
5430 			return 0;
5431 
5432 		msleep(HCLGE_LINK_STATUS_MS);
5433 	} while (++i < wait_cnt);
5434 	return -EBUSY;
5435 }
5436 
5437 static int hclge_mac_phy_link_status_wait(struct hclge_dev *hdev, bool en,
5438 					  bool is_phy)
5439 {
5440 #define HCLGE_MAC_LINK_STATUS_NUM  100
5441 
5442 	int link_ret;
5443 
5444 	link_ret = en ? HCLGE_LINK_STATUS_UP : HCLGE_LINK_STATUS_DOWN;
5445 
5446 	if (is_phy)
5447 		hclge_phy_link_status_wait(hdev, link_ret);
5448 
5449 	return hclge_mac_link_status_wait(hdev, link_ret,
5450 					  HCLGE_MAC_LINK_STATUS_NUM);
5451 }
5452 
5453 static int hclge_set_app_loopback(struct hclge_dev *hdev, bool en)
5454 {
5455 	struct hclge_config_mac_mode_cmd *req;
5456 	struct hclge_desc desc;
5457 	u32 loop_en;
5458 	int ret;
5459 
5460 	req = (struct hclge_config_mac_mode_cmd *)&desc.data[0];
5461 	/* 1 Read out the MAC mode config at first */
5462 	hclge_cmd_setup_basic_desc(&desc, HCLGE_OPC_CONFIG_MAC_MODE, true);
5463 	ret = hclge_cmd_send(&hdev->hw, &desc, 1);
5464 	if (ret) {
5465 		dev_err(&hdev->pdev->dev,
5466 			"mac loopback get fail, ret =%d.\n", ret);
5467 		return ret;
5468 	}
5469 
5470 	/* 2 Then setup the loopback flag */
5471 	loop_en = le32_to_cpu(req->txrx_pad_fcs_loop_en);
5472 	hnae3_set_bit(loop_en, HCLGE_MAC_APP_LP_B, en ? 1 : 0);
5473 
5474 	req->txrx_pad_fcs_loop_en = cpu_to_le32(loop_en);
5475 
5476 	/* 3 Config mac work mode with loopback flag
5477 	 * and its original configure parameters
5478 	 */
5479 	hclge_comm_cmd_reuse_desc(&desc, false);
5480 	ret = hclge_cmd_send(&hdev->hw, &desc, 1);
5481 	if (ret)
5482 		dev_err(&hdev->pdev->dev,
5483 			"mac loopback set fail, ret =%d.\n", ret);
5484 	return ret;
5485 }
5486 
5487 static int hclge_cfg_common_loopback_cmd_send(struct hclge_dev *hdev, bool en,
5488 					      enum hnae3_loop loop_mode)
5489 {
5490 	struct hclge_common_lb_cmd *req;
5491 	struct hclge_desc desc;
5492 	u8 loop_mode_b;
5493 	int ret;
5494 
5495 	req = (struct hclge_common_lb_cmd *)desc.data;
5496 	hclge_cmd_setup_basic_desc(&desc, HCLGE_OPC_COMMON_LOOPBACK, false);
5497 
5498 	switch (loop_mode) {
5499 	case HNAE3_LOOP_SERIAL_SERDES:
5500 		loop_mode_b = HCLGE_CMD_SERDES_SERIAL_INNER_LOOP_B;
5501 		break;
5502 	case HNAE3_LOOP_PARALLEL_SERDES:
5503 		loop_mode_b = HCLGE_CMD_SERDES_PARALLEL_INNER_LOOP_B;
5504 		break;
5505 	case HNAE3_LOOP_PHY:
5506 		loop_mode_b = HCLGE_CMD_GE_PHY_INNER_LOOP_B;
5507 		break;
5508 	default:
5509 		dev_err(&hdev->pdev->dev,
5510 			"unsupported loopback mode %d\n", loop_mode);
5511 		return -ENOTSUPP;
5512 	}
5513 
5514 	req->mask = loop_mode_b;
5515 	if (en)
5516 		req->enable = loop_mode_b;
5517 
5518 	ret = hclge_cmd_send(&hdev->hw, &desc, 1);
5519 	if (ret)
5520 		dev_err(&hdev->pdev->dev,
5521 			"failed to send loopback cmd, loop_mode = %d, ret = %d\n",
5522 			loop_mode, ret);
5523 
5524 	return ret;
5525 }
5526 
5527 static int hclge_cfg_common_loopback_wait(struct hclge_dev *hdev)
5528 {
5529 #define HCLGE_COMMON_LB_RETRY_MS	10
5530 #define HCLGE_COMMON_LB_RETRY_NUM	100
5531 
5532 	struct hclge_common_lb_cmd *req;
5533 	struct hclge_desc desc;
5534 	u32 i = 0;
5535 	int ret;
5536 
5537 	req = (struct hclge_common_lb_cmd *)desc.data;
5538 
5539 	do {
5540 		msleep(HCLGE_COMMON_LB_RETRY_MS);
5541 		hclge_cmd_setup_basic_desc(&desc, HCLGE_OPC_COMMON_LOOPBACK,
5542 					   true);
5543 		ret = hclge_cmd_send(&hdev->hw, &desc, 1);
5544 		if (ret) {
5545 			dev_err(&hdev->pdev->dev,
5546 				"failed to get loopback done status, ret = %d\n",
5547 				ret);
5548 			return ret;
5549 		}
5550 	} while (++i < HCLGE_COMMON_LB_RETRY_NUM &&
5551 		 !(req->result & HCLGE_CMD_COMMON_LB_DONE_B));
5552 
5553 	if (!(req->result & HCLGE_CMD_COMMON_LB_DONE_B)) {
5554 		dev_err(&hdev->pdev->dev, "wait loopback timeout\n");
5555 		return -EBUSY;
5556 	} else if (!(req->result & HCLGE_CMD_COMMON_LB_SUCCESS_B)) {
5557 		dev_err(&hdev->pdev->dev, "failed to do loopback test\n");
5558 		return -EIO;
5559 	}
5560 
5561 	return 0;
5562 }
5563 
5564 static int hclge_cfg_common_loopback(struct hclge_dev *hdev, bool en,
5565 				     enum hnae3_loop loop_mode)
5566 {
5567 	int ret;
5568 
5569 	ret = hclge_cfg_common_loopback_cmd_send(hdev, en, loop_mode);
5570 	if (ret)
5571 		return ret;
5572 
5573 	return hclge_cfg_common_loopback_wait(hdev);
5574 }
5575 
5576 static int hclge_set_common_loopback(struct hclge_dev *hdev, bool en,
5577 				     enum hnae3_loop loop_mode)
5578 {
5579 	int ret;
5580 
5581 	ret = hclge_cfg_common_loopback(hdev, en, loop_mode);
5582 	if (ret)
5583 		return ret;
5584 
5585 	hclge_cfg_mac_mode(hdev, en);
5586 
5587 	ret = hclge_mac_phy_link_status_wait(hdev, en, false);
5588 	if (ret)
5589 		dev_err(&hdev->pdev->dev,
5590 			"serdes loopback config mac mode timeout\n");
5591 
5592 	return ret;
5593 }
5594 
5595 static int hclge_enable_phy_loopback(struct hclge_dev *hdev,
5596 				     struct phy_device *phydev)
5597 {
5598 	int ret;
5599 
5600 	if (!phydev->suspended) {
5601 		ret = phy_suspend(phydev);
5602 		if (ret)
5603 			return ret;
5604 	}
5605 
5606 	ret = phy_resume(phydev);
5607 	if (ret)
5608 		return ret;
5609 
5610 	return phy_loopback(phydev, true, 0);
5611 }
5612 
5613 static int hclge_disable_phy_loopback(struct hclge_dev *hdev,
5614 				      struct phy_device *phydev)
5615 {
5616 	int ret;
5617 
5618 	ret = phy_loopback(phydev, false, 0);
5619 	if (ret)
5620 		return ret;
5621 
5622 	return phy_suspend(phydev);
5623 }
5624 
5625 static int hclge_set_phy_loopback(struct hclge_dev *hdev, bool en)
5626 {
5627 	struct phy_device *phydev = hdev->hw.mac.phydev;
5628 	int ret;
5629 
5630 	if (!phydev) {
5631 		if (hnae3_dev_phy_imp_supported(hdev))
5632 			return hclge_set_common_loopback(hdev, en,
5633 							 HNAE3_LOOP_PHY);
5634 		return -ENOTSUPP;
5635 	}
5636 
5637 	if (en)
5638 		ret = hclge_enable_phy_loopback(hdev, phydev);
5639 	else
5640 		ret = hclge_disable_phy_loopback(hdev, phydev);
5641 	if (ret) {
5642 		dev_err(&hdev->pdev->dev,
5643 			"set phy loopback fail, ret = %d\n", ret);
5644 		return ret;
5645 	}
5646 
5647 	hclge_cfg_mac_mode(hdev, en);
5648 
5649 	ret = hclge_mac_phy_link_status_wait(hdev, en, true);
5650 	if (ret)
5651 		dev_err(&hdev->pdev->dev,
5652 			"phy loopback config mac mode timeout\n");
5653 
5654 	return ret;
5655 }
5656 
5657 static int hclge_tqp_enable_cmd_send(struct hclge_dev *hdev, u16 tqp_id,
5658 				     u16 stream_id, bool enable)
5659 {
5660 	struct hclge_desc desc;
5661 	struct hclge_cfg_com_tqp_queue_cmd *req =
5662 		(struct hclge_cfg_com_tqp_queue_cmd *)desc.data;
5663 
5664 	hclge_cmd_setup_basic_desc(&desc, HCLGE_OPC_CFG_COM_TQP_QUEUE, false);
5665 	req->tqp_id = cpu_to_le16(tqp_id);
5666 	req->stream_id = cpu_to_le16(stream_id);
5667 	if (enable)
5668 		req->enable |= 1U << HCLGE_TQP_ENABLE_B;
5669 
5670 	return hclge_cmd_send(&hdev->hw, &desc, 1);
5671 }
5672 
5673 static int hclge_tqp_enable(struct hnae3_handle *handle, bool enable)
5674 {
5675 	struct hclge_vport *vport = hclge_get_vport(handle);
5676 	struct hclge_dev *hdev = vport->back;
5677 	int ret;
5678 	u16 i;
5679 
5680 	for (i = 0; i < handle->kinfo.num_tqps; i++) {
5681 		ret = hclge_tqp_enable_cmd_send(hdev, i, 0, enable);
5682 		if (ret)
5683 			return ret;
5684 	}
5685 	return 0;
5686 }
5687 
5688 static int hclge_set_loopback(struct hnae3_handle *handle,
5689 			      enum hnae3_loop loop_mode, bool en)
5690 {
5691 	struct hclge_vport *vport = hclge_get_vport(handle);
5692 	struct hclge_dev *hdev = vport->back;
5693 	int ret = 0;
5694 
5695 	/* Loopback can be enabled in three places: SSU, MAC, and serdes. By
5696 	 * default, SSU loopback is enabled, so if the SMAC and the DMAC are
5697 	 * the same, the packets are looped back in the SSU. If SSU loopback
5698 	 * is disabled, packets can reach MAC even if SMAC is the same as DMAC.
5699 	 */
5700 	if (hdev->ae_dev->dev_version >= HNAE3_DEVICE_VERSION_V2) {
5701 		u8 switch_param = en ? 0 : BIT(HCLGE_SWITCH_ALW_LPBK_B);
5702 
5703 		ret = hclge_config_switch_param(hdev, PF_VPORT_ID, switch_param,
5704 						HCLGE_SWITCH_ALW_LPBK_MASK);
5705 		if (ret)
5706 			return ret;
5707 	}
5708 
5709 	switch (loop_mode) {
5710 	case HNAE3_LOOP_APP:
5711 		ret = hclge_set_app_loopback(hdev, en);
5712 		break;
5713 	case HNAE3_LOOP_SERIAL_SERDES:
5714 	case HNAE3_LOOP_PARALLEL_SERDES:
5715 		ret = hclge_set_common_loopback(hdev, en, loop_mode);
5716 		break;
5717 	case HNAE3_LOOP_PHY:
5718 		ret = hclge_set_phy_loopback(hdev, en);
5719 		break;
5720 	case HNAE3_LOOP_EXTERNAL:
5721 		break;
5722 	default:
5723 		ret = -ENOTSUPP;
5724 		dev_err(&hdev->pdev->dev,
5725 			"loop_mode %d is not supported\n", loop_mode);
5726 		break;
5727 	}
5728 
5729 	if (ret)
5730 		return ret;
5731 
5732 	ret = hclge_tqp_enable(handle, en);
5733 	if (ret)
5734 		dev_err(&hdev->pdev->dev, "failed to %s tqp in loopback, ret = %d\n",
5735 			str_enable_disable(en), ret);
5736 
5737 	return ret;
5738 }
5739 
5740 static int hclge_set_default_loopback(struct hclge_dev *hdev)
5741 {
5742 	int ret;
5743 
5744 	ret = hclge_set_app_loopback(hdev, false);
5745 	if (ret)
5746 		return ret;
5747 
5748 	ret = hclge_cfg_common_loopback(hdev, false, HNAE3_LOOP_SERIAL_SERDES);
5749 	if (ret)
5750 		return ret;
5751 
5752 	return hclge_cfg_common_loopback(hdev, false,
5753 					 HNAE3_LOOP_PARALLEL_SERDES);
5754 }
5755 
5756 static void hclge_flush_link_update(struct hclge_dev *hdev)
5757 {
5758 #define HCLGE_FLUSH_LINK_TIMEOUT	100000
5759 
5760 	unsigned long last = hdev->serv_processed_cnt;
5761 	int i = 0;
5762 
5763 	while (test_bit(HCLGE_STATE_LINK_UPDATING, &hdev->state) &&
5764 	       i++ < HCLGE_FLUSH_LINK_TIMEOUT &&
5765 	       last == hdev->serv_processed_cnt)
5766 		usleep_range(1, 1);
5767 }
5768 
5769 static void hclge_set_timer_task(struct hnae3_handle *handle, bool enable)
5770 {
5771 	struct hclge_vport *vport = hclge_get_vport(handle);
5772 	struct hclge_dev *hdev = vport->back;
5773 
5774 	if (enable) {
5775 		hclge_task_schedule(hdev, 0);
5776 	} else {
5777 		/* Set the DOWN flag here to disable link updating */
5778 		set_bit(HCLGE_STATE_DOWN, &hdev->state);
5779 
5780 		smp_mb__after_atomic(); /* flush memory to make sure DOWN is seen by service task */
5781 		hclge_flush_link_update(hdev);
5782 	}
5783 }
5784 
5785 static int hclge_ae_start(struct hnae3_handle *handle)
5786 {
5787 	struct hclge_vport *vport = hclge_get_vport(handle);
5788 	struct hclge_dev *hdev = vport->back;
5789 
5790 	/* mac enable */
5791 	hclge_cfg_mac_mode(hdev, true);
5792 	clear_bit(HCLGE_STATE_DOWN, &hdev->state);
5793 	hdev->hw.mac.link = 0;
5794 
5795 	/* reset tqp stats */
5796 	hclge_comm_reset_tqp_stats(handle);
5797 
5798 	hclge_mac_start_phy(hdev);
5799 
5800 	return 0;
5801 }
5802 
5803 static void hclge_ae_stop(struct hnae3_handle *handle)
5804 {
5805 	struct hclge_vport *vport = hclge_get_vport(handle);
5806 	struct hclge_dev *hdev = vport->back;
5807 
5808 	set_bit(HCLGE_STATE_DOWN, &hdev->state);
5809 	spin_lock_bh(&hdev->fd_rule_lock);
5810 	hclge_clear_arfs_rules(hdev);
5811 	spin_unlock_bh(&hdev->fd_rule_lock);
5812 
5813 	/* If it is not PF reset or FLR, the firmware will disable the MAC,
5814 	 * so it only need to stop phy here.
5815 	 */
5816 	if (test_bit(HCLGE_STATE_RST_HANDLING, &hdev->state)) {
5817 		hclge_pfc_pause_en_cfg(hdev, HCLGE_PFC_TX_RX_DISABLE,
5818 				       HCLGE_PFC_DISABLE);
5819 		if (hdev->reset_type != HNAE3_FUNC_RESET &&
5820 		    hdev->reset_type != HNAE3_FLR_RESET) {
5821 			hclge_mac_stop_phy(hdev);
5822 			hclge_update_link_status(hdev);
5823 			return;
5824 		}
5825 	}
5826 
5827 	hclge_reset_tqp(handle);
5828 
5829 	hclge_config_mac_tnl_int(hdev, false);
5830 
5831 	/* Mac disable */
5832 	hclge_cfg_mac_mode(hdev, false);
5833 
5834 	hclge_mac_stop_phy(hdev);
5835 
5836 	/* reset tqp stats */
5837 	hclge_comm_reset_tqp_stats(handle);
5838 	hclge_update_link_status(hdev);
5839 }
5840 
5841 int hclge_vport_start(struct hclge_vport *vport)
5842 {
5843 	struct hclge_dev *hdev = vport->back;
5844 
5845 	set_bit(HCLGE_VPORT_STATE_INITED, &vport->state);
5846 	set_bit(HCLGE_VPORT_STATE_ALIVE, &vport->state);
5847 	set_bit(HCLGE_VPORT_STATE_PROMISC_CHANGE, &vport->state);
5848 	vport->last_active_jiffies = jiffies;
5849 	vport->need_notify = 0;
5850 
5851 	if (test_bit(vport->vport_id, hdev->vport_config_block)) {
5852 		if (vport->vport_id) {
5853 			hclge_restore_mac_table_common(vport);
5854 			hclge_restore_vport_vlan_table(vport);
5855 		} else {
5856 			hclge_restore_hw_table(hdev);
5857 		}
5858 	}
5859 
5860 	clear_bit(vport->vport_id, hdev->vport_config_block);
5861 
5862 	return 0;
5863 }
5864 
5865 void hclge_vport_stop(struct hclge_vport *vport)
5866 {
5867 	clear_bit(HCLGE_VPORT_STATE_INITED, &vport->state);
5868 	clear_bit(HCLGE_VPORT_STATE_ALIVE, &vport->state);
5869 	vport->need_notify = 0;
5870 }
5871 
5872 static int hclge_client_start(struct hnae3_handle *handle)
5873 {
5874 	struct hclge_vport *vport = hclge_get_vport(handle);
5875 
5876 	return hclge_vport_start(vport);
5877 }
5878 
5879 static void hclge_client_stop(struct hnae3_handle *handle)
5880 {
5881 	struct hclge_vport *vport = hclge_get_vport(handle);
5882 
5883 	hclge_vport_stop(vport);
5884 }
5885 
5886 static int hclge_get_mac_vlan_cmd_status(struct hclge_vport *vport,
5887 					 u16 cmdq_resp, u8  resp_code,
5888 					 enum hclge_mac_vlan_tbl_opcode op)
5889 {
5890 	struct hclge_dev *hdev = vport->back;
5891 
5892 	if (cmdq_resp) {
5893 		dev_err(&hdev->pdev->dev,
5894 			"cmdq execute failed for get_mac_vlan_cmd_status,status=%u.\n",
5895 			cmdq_resp);
5896 		return -EIO;
5897 	}
5898 
5899 	if (op == HCLGE_MAC_VLAN_ADD) {
5900 		if (!resp_code || resp_code == 1)
5901 			return 0;
5902 		else if (resp_code == HCLGE_ADD_UC_OVERFLOW ||
5903 			 resp_code == HCLGE_ADD_MC_OVERFLOW)
5904 			return -ENOSPC;
5905 
5906 		dev_err(&hdev->pdev->dev,
5907 			"add mac addr failed for undefined, code=%u.\n",
5908 			resp_code);
5909 		return -EIO;
5910 	} else if (op == HCLGE_MAC_VLAN_REMOVE) {
5911 		if (!resp_code) {
5912 			return 0;
5913 		} else if (resp_code == 1) {
5914 			dev_dbg(&hdev->pdev->dev,
5915 				"remove mac addr failed for miss.\n");
5916 			return -ENOENT;
5917 		}
5918 
5919 		dev_err(&hdev->pdev->dev,
5920 			"remove mac addr failed for undefined, code=%u.\n",
5921 			resp_code);
5922 		return -EIO;
5923 	} else if (op == HCLGE_MAC_VLAN_LKUP) {
5924 		if (!resp_code) {
5925 			return 0;
5926 		} else if (resp_code == 1) {
5927 			dev_dbg(&hdev->pdev->dev,
5928 				"lookup mac addr failed for miss.\n");
5929 			return -ENOENT;
5930 		}
5931 
5932 		dev_err(&hdev->pdev->dev,
5933 			"lookup mac addr failed for undefined, code=%u.\n",
5934 			resp_code);
5935 		return -EIO;
5936 	}
5937 
5938 	dev_err(&hdev->pdev->dev,
5939 		"unknown opcode for get_mac_vlan_cmd_status, opcode=%d.\n", op);
5940 
5941 	return -EINVAL;
5942 }
5943 
5944 static int hclge_update_desc_vfid(struct hclge_desc *desc, int vfid, bool clr)
5945 {
5946 #define HCLGE_VF_NUM_IN_FIRST_DESC 192
5947 
5948 	unsigned int word_num;
5949 	unsigned int bit_num;
5950 
5951 	if (vfid > 255 || vfid < 0)
5952 		return -EIO;
5953 
5954 	if (vfid >= 0 && vfid < HCLGE_VF_NUM_IN_FIRST_DESC) {
5955 		word_num = vfid / 32;
5956 		bit_num  = vfid % 32;
5957 		if (clr)
5958 			desc[1].data[word_num] &= cpu_to_le32(~(1U << bit_num));
5959 		else
5960 			desc[1].data[word_num] |= cpu_to_le32(1 << bit_num);
5961 	} else {
5962 		word_num = (vfid - HCLGE_VF_NUM_IN_FIRST_DESC) / 32;
5963 		bit_num  = vfid % 32;
5964 		if (clr)
5965 			desc[2].data[word_num] &= cpu_to_le32(~(1U << bit_num));
5966 		else
5967 			desc[2].data[word_num] |= cpu_to_le32(1 << bit_num);
5968 	}
5969 
5970 	return 0;
5971 }
5972 
5973 static bool hclge_is_all_function_id_zero(struct hclge_desc *desc)
5974 {
5975 #define HCLGE_DESC_NUMBER 3
5976 #define HCLGE_FUNC_NUMBER_PER_DESC 6
5977 	int i, j;
5978 
5979 	for (i = 1; i < HCLGE_DESC_NUMBER; i++)
5980 		for (j = 0; j < HCLGE_FUNC_NUMBER_PER_DESC; j++)
5981 			if (desc[i].data[j])
5982 				return false;
5983 
5984 	return true;
5985 }
5986 
5987 static void hclge_prepare_mac_addr(struct hclge_mac_vlan_tbl_entry_cmd *new_req,
5988 				   const u8 *addr, bool is_mc)
5989 {
5990 	const unsigned char *mac_addr = addr;
5991 	u32 high_val = mac_addr[2] << 16 | (mac_addr[3] << 24) |
5992 		       (mac_addr[0]) | (mac_addr[1] << 8);
5993 	u32 low_val  = mac_addr[4] | (mac_addr[5] << 8);
5994 
5995 	hnae3_set_bit(new_req->flags, HCLGE_MAC_VLAN_BIT0_EN_B, 1);
5996 	if (is_mc) {
5997 		hnae3_set_bit(new_req->entry_type, HCLGE_MAC_VLAN_BIT1_EN_B, 1);
5998 		hnae3_set_bit(new_req->mc_mac_en, HCLGE_MAC_VLAN_BIT0_EN_B, 1);
5999 	}
6000 
6001 	new_req->mac_addr_hi32 = cpu_to_le32(high_val);
6002 	new_req->mac_addr_lo16 = cpu_to_le16(low_val & 0xffff);
6003 }
6004 
6005 static int hclge_remove_mac_vlan_tbl(struct hclge_vport *vport,
6006 				     struct hclge_mac_vlan_tbl_entry_cmd *req)
6007 {
6008 	struct hclge_dev *hdev = vport->back;
6009 	struct hclge_desc desc;
6010 	u8 resp_code;
6011 	u16 retval;
6012 	int ret;
6013 
6014 	hclge_cmd_setup_basic_desc(&desc, HCLGE_OPC_MAC_VLAN_REMOVE, false);
6015 
6016 	memcpy(desc.data, req, sizeof(struct hclge_mac_vlan_tbl_entry_cmd));
6017 
6018 	ret = hclge_cmd_send(&hdev->hw, &desc, 1);
6019 	if (ret) {
6020 		dev_err(&hdev->pdev->dev,
6021 			"del mac addr failed for cmd_send, ret =%d.\n",
6022 			ret);
6023 		return ret;
6024 	}
6025 	resp_code = (le32_to_cpu(desc.data[0]) >> 8) & 0xff;
6026 	retval = le16_to_cpu(desc.retval);
6027 
6028 	return hclge_get_mac_vlan_cmd_status(vport, retval, resp_code,
6029 					     HCLGE_MAC_VLAN_REMOVE);
6030 }
6031 
6032 static int hclge_lookup_mac_vlan_tbl(struct hclge_vport *vport,
6033 				     struct hclge_mac_vlan_tbl_entry_cmd *req,
6034 				     struct hclge_desc *desc,
6035 				     bool is_mc)
6036 {
6037 	struct hclge_dev *hdev = vport->back;
6038 	u8 resp_code;
6039 	u16 retval;
6040 	int ret;
6041 
6042 	hclge_cmd_setup_basic_desc(&desc[0], HCLGE_OPC_MAC_VLAN_ADD, true);
6043 	if (is_mc) {
6044 		desc[0].flag |= cpu_to_le16(HCLGE_COMM_CMD_FLAG_NEXT);
6045 		memcpy(desc[0].data,
6046 		       req,
6047 		       sizeof(struct hclge_mac_vlan_tbl_entry_cmd));
6048 		hclge_cmd_setup_basic_desc(&desc[1],
6049 					   HCLGE_OPC_MAC_VLAN_ADD,
6050 					   true);
6051 		desc[1].flag |= cpu_to_le16(HCLGE_COMM_CMD_FLAG_NEXT);
6052 		hclge_cmd_setup_basic_desc(&desc[2],
6053 					   HCLGE_OPC_MAC_VLAN_ADD,
6054 					   true);
6055 		ret = hclge_cmd_send(&hdev->hw, desc, 3);
6056 	} else {
6057 		memcpy(desc[0].data,
6058 		       req,
6059 		       sizeof(struct hclge_mac_vlan_tbl_entry_cmd));
6060 		ret = hclge_cmd_send(&hdev->hw, desc, 1);
6061 	}
6062 	if (ret) {
6063 		dev_err(&hdev->pdev->dev,
6064 			"lookup mac addr failed for cmd_send, ret =%d.\n",
6065 			ret);
6066 		return ret;
6067 	}
6068 	resp_code = (le32_to_cpu(desc[0].data[0]) >> 8) & 0xff;
6069 	retval = le16_to_cpu(desc[0].retval);
6070 
6071 	return hclge_get_mac_vlan_cmd_status(vport, retval, resp_code,
6072 					     HCLGE_MAC_VLAN_LKUP);
6073 }
6074 
6075 static int hclge_add_mac_vlan_tbl(struct hclge_vport *vport,
6076 				  struct hclge_mac_vlan_tbl_entry_cmd *req,
6077 				  struct hclge_desc *mc_desc)
6078 {
6079 	struct hclge_dev *hdev = vport->back;
6080 	int cfg_status;
6081 	u8 resp_code;
6082 	u16 retval;
6083 	int ret;
6084 
6085 	if (!mc_desc) {
6086 		struct hclge_desc desc;
6087 
6088 		hclge_cmd_setup_basic_desc(&desc,
6089 					   HCLGE_OPC_MAC_VLAN_ADD,
6090 					   false);
6091 		memcpy(desc.data, req,
6092 		       sizeof(struct hclge_mac_vlan_tbl_entry_cmd));
6093 		ret = hclge_cmd_send(&hdev->hw, &desc, 1);
6094 		resp_code = (le32_to_cpu(desc.data[0]) >> 8) & 0xff;
6095 		retval = le16_to_cpu(desc.retval);
6096 
6097 		cfg_status = hclge_get_mac_vlan_cmd_status(vport, retval,
6098 							   resp_code,
6099 							   HCLGE_MAC_VLAN_ADD);
6100 	} else {
6101 		hclge_comm_cmd_reuse_desc(&mc_desc[0], false);
6102 		mc_desc[0].flag |= cpu_to_le16(HCLGE_COMM_CMD_FLAG_NEXT);
6103 		hclge_comm_cmd_reuse_desc(&mc_desc[1], false);
6104 		mc_desc[1].flag |= cpu_to_le16(HCLGE_COMM_CMD_FLAG_NEXT);
6105 		hclge_comm_cmd_reuse_desc(&mc_desc[2], false);
6106 		mc_desc[2].flag &= cpu_to_le16(~HCLGE_COMM_CMD_FLAG_NEXT);
6107 		memcpy(mc_desc[0].data, req,
6108 		       sizeof(struct hclge_mac_vlan_tbl_entry_cmd));
6109 		ret = hclge_cmd_send(&hdev->hw, mc_desc, 3);
6110 		resp_code = (le32_to_cpu(mc_desc[0].data[0]) >> 8) & 0xff;
6111 		retval = le16_to_cpu(mc_desc[0].retval);
6112 
6113 		cfg_status = hclge_get_mac_vlan_cmd_status(vport, retval,
6114 							   resp_code,
6115 							   HCLGE_MAC_VLAN_ADD);
6116 	}
6117 
6118 	if (ret) {
6119 		dev_err(&hdev->pdev->dev,
6120 			"add mac addr failed for cmd_send, ret =%d.\n",
6121 			ret);
6122 		return ret;
6123 	}
6124 
6125 	return cfg_status;
6126 }
6127 
6128 static int hclge_set_umv_space(struct hclge_dev *hdev, u16 space_size,
6129 			       u16 *allocated_size)
6130 {
6131 	struct hclge_umv_spc_alc_cmd *req;
6132 	struct hclge_desc desc;
6133 	int ret;
6134 
6135 	req = (struct hclge_umv_spc_alc_cmd *)desc.data;
6136 	hclge_cmd_setup_basic_desc(&desc, HCLGE_OPC_MAC_VLAN_ALLOCATE, false);
6137 
6138 	req->space_size = cpu_to_le32(space_size);
6139 
6140 	ret = hclge_cmd_send(&hdev->hw, &desc, 1);
6141 	if (ret) {
6142 		dev_err(&hdev->pdev->dev, "failed to set umv space, ret = %d\n",
6143 			ret);
6144 		return ret;
6145 	}
6146 
6147 	*allocated_size = le32_to_cpu(desc.data[1]);
6148 
6149 	return 0;
6150 }
6151 
6152 static int hclge_init_umv_space(struct hclge_dev *hdev)
6153 {
6154 	u16 allocated_size = 0;
6155 	int ret;
6156 
6157 	ret = hclge_set_umv_space(hdev, hdev->wanted_umv_size, &allocated_size);
6158 	if (ret)
6159 		return ret;
6160 
6161 	if (allocated_size < hdev->wanted_umv_size)
6162 		dev_warn(&hdev->pdev->dev,
6163 			 "failed to alloc umv space, want %u, get %u\n",
6164 			 hdev->wanted_umv_size, allocated_size);
6165 
6166 	hdev->max_umv_size = allocated_size;
6167 	hdev->priv_umv_size = hdev->max_umv_size / (hdev->num_alloc_vport + 1);
6168 	hdev->share_umv_size = hdev->priv_umv_size +
6169 			hdev->max_umv_size % (hdev->num_alloc_vport + 1);
6170 
6171 	if (hdev->ae_dev->dev_specs.mc_mac_size)
6172 		set_bit(HNAE3_DEV_SUPPORT_MC_MAC_MNG_B, hdev->ae_dev->caps);
6173 
6174 	return 0;
6175 }
6176 
6177 static void hclge_reset_umv_space(struct hclge_dev *hdev)
6178 {
6179 	struct hclge_vport *vport;
6180 	int i;
6181 
6182 	for (i = 0; i < hdev->num_alloc_vport; i++) {
6183 		vport = &hdev->vport[i];
6184 		vport->used_umv_num = 0;
6185 	}
6186 
6187 	mutex_lock(&hdev->vport_lock);
6188 	hdev->share_umv_size = hdev->priv_umv_size +
6189 			hdev->max_umv_size % (hdev->num_alloc_vport + 1);
6190 	mutex_unlock(&hdev->vport_lock);
6191 
6192 	hdev->used_mc_mac_num = 0;
6193 }
6194 
6195 static bool hclge_is_umv_space_full(struct hclge_vport *vport, bool need_lock)
6196 {
6197 	struct hclge_dev *hdev = vport->back;
6198 	bool is_full;
6199 
6200 	if (need_lock)
6201 		mutex_lock(&hdev->vport_lock);
6202 
6203 	is_full = (vport->used_umv_num >= hdev->priv_umv_size &&
6204 		   hdev->share_umv_size == 0);
6205 
6206 	if (need_lock)
6207 		mutex_unlock(&hdev->vport_lock);
6208 
6209 	return is_full;
6210 }
6211 
6212 static void hclge_update_umv_space(struct hclge_vport *vport, bool is_free)
6213 {
6214 	struct hclge_dev *hdev = vport->back;
6215 
6216 	if (is_free) {
6217 		if (vport->used_umv_num > hdev->priv_umv_size)
6218 			hdev->share_umv_size++;
6219 
6220 		if (vport->used_umv_num > 0)
6221 			vport->used_umv_num--;
6222 	} else {
6223 		if (vport->used_umv_num >= hdev->priv_umv_size &&
6224 		    hdev->share_umv_size > 0)
6225 			hdev->share_umv_size--;
6226 		vport->used_umv_num++;
6227 	}
6228 }
6229 
6230 static struct hclge_mac_node *hclge_find_mac_node(struct list_head *list,
6231 						  const u8 *mac_addr)
6232 {
6233 	struct hclge_mac_node *mac_node, *tmp;
6234 
6235 	list_for_each_entry_safe(mac_node, tmp, list, node)
6236 		if (ether_addr_equal(mac_addr, mac_node->mac_addr))
6237 			return mac_node;
6238 
6239 	return NULL;
6240 }
6241 
6242 static void hclge_update_mac_node(struct hclge_mac_node *mac_node,
6243 				  enum HCLGE_MAC_NODE_STATE state)
6244 {
6245 	switch (state) {
6246 	/* from set_rx_mode or tmp_add_list */
6247 	case HCLGE_MAC_TO_ADD:
6248 		if (mac_node->state == HCLGE_MAC_TO_DEL)
6249 			mac_node->state = HCLGE_MAC_ACTIVE;
6250 		break;
6251 	/* only from set_rx_mode */
6252 	case HCLGE_MAC_TO_DEL:
6253 		if (mac_node->state == HCLGE_MAC_TO_ADD) {
6254 			list_del(&mac_node->node);
6255 			kfree(mac_node);
6256 		} else {
6257 			mac_node->state = HCLGE_MAC_TO_DEL;
6258 		}
6259 		break;
6260 	/* only from tmp_add_list, the mac_node->state won't be
6261 	 * ACTIVE.
6262 	 */
6263 	case HCLGE_MAC_ACTIVE:
6264 		if (mac_node->state == HCLGE_MAC_TO_ADD)
6265 			mac_node->state = HCLGE_MAC_ACTIVE;
6266 
6267 		break;
6268 	}
6269 }
6270 
6271 int hclge_update_mac_list(struct hclge_vport *vport,
6272 			  enum HCLGE_MAC_NODE_STATE state,
6273 			  enum HCLGE_MAC_ADDR_TYPE mac_type,
6274 			  const unsigned char *addr)
6275 {
6276 	char format_mac_addr[HNAE3_FORMAT_MAC_ADDR_LEN];
6277 	struct hclge_dev *hdev = vport->back;
6278 	struct hclge_mac_node *mac_node;
6279 	struct list_head *list;
6280 
6281 	list = (mac_type == HCLGE_MAC_ADDR_UC) ?
6282 		&vport->uc_mac_list : &vport->mc_mac_list;
6283 
6284 	spin_lock_bh(&vport->mac_list_lock);
6285 
6286 	/* if the mac addr is already in the mac list, no need to add a new
6287 	 * one into it, just check the mac addr state, convert it to a new
6288 	 * state, or just remove it, or do nothing.
6289 	 */
6290 	mac_node = hclge_find_mac_node(list, addr);
6291 	if (mac_node) {
6292 		hclge_update_mac_node(mac_node, state);
6293 		spin_unlock_bh(&vport->mac_list_lock);
6294 		set_bit(HCLGE_VPORT_STATE_MAC_TBL_CHANGE, &vport->state);
6295 		return 0;
6296 	}
6297 
6298 	/* if this address is never added, unnecessary to delete */
6299 	if (state == HCLGE_MAC_TO_DEL) {
6300 		spin_unlock_bh(&vport->mac_list_lock);
6301 		hnae3_format_mac_addr(format_mac_addr, addr);
6302 		dev_err(&hdev->pdev->dev,
6303 			"failed to delete address %s from mac list\n",
6304 			format_mac_addr);
6305 		return -ENOENT;
6306 	}
6307 
6308 	mac_node = kzalloc_obj(*mac_node, GFP_ATOMIC);
6309 	if (!mac_node) {
6310 		spin_unlock_bh(&vport->mac_list_lock);
6311 		return -ENOMEM;
6312 	}
6313 
6314 	set_bit(HCLGE_VPORT_STATE_MAC_TBL_CHANGE, &vport->state);
6315 
6316 	mac_node->state = state;
6317 	ether_addr_copy(mac_node->mac_addr, addr);
6318 	list_add_tail(&mac_node->node, list);
6319 
6320 	spin_unlock_bh(&vport->mac_list_lock);
6321 
6322 	return 0;
6323 }
6324 
6325 static int hclge_add_uc_addr(struct hnae3_handle *handle,
6326 			     const unsigned char *addr)
6327 {
6328 	struct hclge_vport *vport = hclge_get_vport(handle);
6329 
6330 	return hclge_update_mac_list(vport, HCLGE_MAC_TO_ADD, HCLGE_MAC_ADDR_UC,
6331 				     addr);
6332 }
6333 
6334 int hclge_add_uc_addr_common(struct hclge_vport *vport,
6335 			     const unsigned char *addr)
6336 {
6337 	char format_mac_addr[HNAE3_FORMAT_MAC_ADDR_LEN];
6338 	struct hclge_dev *hdev = vport->back;
6339 	struct hclge_mac_vlan_tbl_entry_cmd req;
6340 	struct hclge_desc desc;
6341 	u16 egress_port = 0;
6342 	int ret;
6343 
6344 	/* mac addr check */
6345 	if (is_zero_ether_addr(addr) ||
6346 	    is_broadcast_ether_addr(addr) ||
6347 	    is_multicast_ether_addr(addr)) {
6348 		hnae3_format_mac_addr(format_mac_addr, addr);
6349 		dev_err(&hdev->pdev->dev,
6350 			"Set_uc mac err! invalid mac:%s. is_zero:%d,is_br=%d,is_mul=%d\n",
6351 			 format_mac_addr, is_zero_ether_addr(addr),
6352 			 is_broadcast_ether_addr(addr),
6353 			 is_multicast_ether_addr(addr));
6354 		return -EINVAL;
6355 	}
6356 
6357 	memset(&req, 0, sizeof(req));
6358 
6359 	hnae3_set_field(egress_port, HCLGE_MAC_EPORT_VFID_M,
6360 			HCLGE_MAC_EPORT_VFID_S, vport->vport_id);
6361 
6362 	req.egress_port = cpu_to_le16(egress_port);
6363 
6364 	hclge_prepare_mac_addr(&req, addr, false);
6365 
6366 	/* Lookup the mac address in the mac_vlan table, and add
6367 	 * it if the entry is inexistent. Repeated unicast entry
6368 	 * is not allowed in the mac vlan table.
6369 	 */
6370 	ret = hclge_lookup_mac_vlan_tbl(vport, &req, &desc, false);
6371 	if (ret == -ENOENT) {
6372 		mutex_lock(&hdev->vport_lock);
6373 		if (!hclge_is_umv_space_full(vport, false)) {
6374 			ret = hclge_add_mac_vlan_tbl(vport, &req, NULL);
6375 			if (!ret)
6376 				hclge_update_umv_space(vport, false);
6377 			mutex_unlock(&hdev->vport_lock);
6378 			return ret;
6379 		}
6380 		mutex_unlock(&hdev->vport_lock);
6381 
6382 		if (!(vport->overflow_promisc_flags & HNAE3_OVERFLOW_UPE))
6383 			dev_err(&hdev->pdev->dev, "UC MAC table full(%u)\n",
6384 				hdev->priv_umv_size);
6385 
6386 		return -ENOSPC;
6387 	}
6388 
6389 	/* check if we just hit the duplicate */
6390 	if (!ret)
6391 		return -EEXIST;
6392 
6393 	return ret;
6394 }
6395 
6396 static int hclge_rm_uc_addr(struct hnae3_handle *handle,
6397 			    const unsigned char *addr)
6398 {
6399 	struct hclge_vport *vport = hclge_get_vport(handle);
6400 
6401 	return hclge_update_mac_list(vport, HCLGE_MAC_TO_DEL, HCLGE_MAC_ADDR_UC,
6402 				     addr);
6403 }
6404 
6405 int hclge_rm_uc_addr_common(struct hclge_vport *vport,
6406 			    const unsigned char *addr)
6407 {
6408 	char format_mac_addr[HNAE3_FORMAT_MAC_ADDR_LEN];
6409 	struct hclge_dev *hdev = vport->back;
6410 	struct hclge_mac_vlan_tbl_entry_cmd req;
6411 	int ret;
6412 
6413 	/* mac addr check */
6414 	if (is_zero_ether_addr(addr) ||
6415 	    is_broadcast_ether_addr(addr) ||
6416 	    is_multicast_ether_addr(addr)) {
6417 		hnae3_format_mac_addr(format_mac_addr, addr);
6418 		dev_dbg(&hdev->pdev->dev, "Remove mac err! invalid mac:%s.\n",
6419 			format_mac_addr);
6420 		return -EINVAL;
6421 	}
6422 
6423 	memset(&req, 0, sizeof(req));
6424 	hnae3_set_bit(req.entry_type, HCLGE_MAC_VLAN_BIT0_EN_B, 0);
6425 	hclge_prepare_mac_addr(&req, addr, false);
6426 	ret = hclge_remove_mac_vlan_tbl(vport, &req);
6427 	if (!ret || ret == -ENOENT) {
6428 		mutex_lock(&hdev->vport_lock);
6429 		hclge_update_umv_space(vport, true);
6430 		mutex_unlock(&hdev->vport_lock);
6431 		return 0;
6432 	}
6433 
6434 	return ret;
6435 }
6436 
6437 static int hclge_add_mc_addr(struct hnae3_handle *handle,
6438 			     const unsigned char *addr)
6439 {
6440 	struct hclge_vport *vport = hclge_get_vport(handle);
6441 
6442 	return hclge_update_mac_list(vport, HCLGE_MAC_TO_ADD, HCLGE_MAC_ADDR_MC,
6443 				     addr);
6444 }
6445 
6446 int hclge_add_mc_addr_common(struct hclge_vport *vport,
6447 			     const unsigned char *addr)
6448 {
6449 	char format_mac_addr[HNAE3_FORMAT_MAC_ADDR_LEN];
6450 	struct hclge_dev *hdev = vport->back;
6451 	struct hclge_mac_vlan_tbl_entry_cmd req;
6452 	struct hclge_desc desc[3];
6453 	bool is_new_addr = false;
6454 	int status;
6455 
6456 	/* mac addr check */
6457 	if (!is_multicast_ether_addr(addr)) {
6458 		hnae3_format_mac_addr(format_mac_addr, addr);
6459 		dev_err(&hdev->pdev->dev,
6460 			"Add mc mac err! invalid mac:%s.\n",
6461 			 format_mac_addr);
6462 		return -EINVAL;
6463 	}
6464 	memset(&req, 0, sizeof(req));
6465 	hclge_prepare_mac_addr(&req, addr, true);
6466 	status = hclge_lookup_mac_vlan_tbl(vport, &req, desc, true);
6467 	if (status) {
6468 		if (hnae3_ae_dev_mc_mac_mng_supported(hdev->ae_dev) &&
6469 		    hdev->used_mc_mac_num >=
6470 		    hdev->ae_dev->dev_specs.mc_mac_size)
6471 			goto err_no_space;
6472 
6473 		is_new_addr = true;
6474 
6475 		/* This mac addr do not exist, add new entry for it */
6476 		memset(desc[0].data, 0, sizeof(desc[0].data));
6477 		memset(desc[1].data, 0, sizeof(desc[0].data));
6478 		memset(desc[2].data, 0, sizeof(desc[0].data));
6479 	}
6480 	status = hclge_update_desc_vfid(desc, vport->vport_id, false);
6481 	if (status)
6482 		return status;
6483 	status = hclge_add_mac_vlan_tbl(vport, &req, desc);
6484 	if (status == -ENOSPC)
6485 		goto err_no_space;
6486 	else if (!status && is_new_addr)
6487 		hdev->used_mc_mac_num++;
6488 
6489 	return status;
6490 
6491 err_no_space:
6492 	/* if already overflow, not to print each time */
6493 	if (!(vport->overflow_promisc_flags & HNAE3_OVERFLOW_MPE)) {
6494 		vport->overflow_promisc_flags |= HNAE3_OVERFLOW_MPE;
6495 		dev_err(&hdev->pdev->dev, "mc mac vlan table is full\n");
6496 	}
6497 
6498 	return -ENOSPC;
6499 }
6500 
6501 static int hclge_rm_mc_addr(struct hnae3_handle *handle,
6502 			    const unsigned char *addr)
6503 {
6504 	struct hclge_vport *vport = hclge_get_vport(handle);
6505 
6506 	return hclge_update_mac_list(vport, HCLGE_MAC_TO_DEL, HCLGE_MAC_ADDR_MC,
6507 				     addr);
6508 }
6509 
6510 int hclge_rm_mc_addr_common(struct hclge_vport *vport,
6511 			    const unsigned char *addr)
6512 {
6513 	char format_mac_addr[HNAE3_FORMAT_MAC_ADDR_LEN];
6514 	struct hclge_dev *hdev = vport->back;
6515 	struct hclge_mac_vlan_tbl_entry_cmd req;
6516 	enum hclge_comm_cmd_status status;
6517 	struct hclge_desc desc[3];
6518 
6519 	/* mac addr check */
6520 	if (!is_multicast_ether_addr(addr)) {
6521 		hnae3_format_mac_addr(format_mac_addr, addr);
6522 		dev_dbg(&hdev->pdev->dev,
6523 			"Remove mc mac err! invalid mac:%s.\n",
6524 			 format_mac_addr);
6525 		return -EINVAL;
6526 	}
6527 
6528 	memset(&req, 0, sizeof(req));
6529 	hclge_prepare_mac_addr(&req, addr, true);
6530 	status = hclge_lookup_mac_vlan_tbl(vport, &req, desc, true);
6531 	if (!status) {
6532 		/* This mac addr exist, remove this handle's VFID for it */
6533 		status = hclge_update_desc_vfid(desc, vport->vport_id, true);
6534 		if (status)
6535 			return status;
6536 
6537 		if (hclge_is_all_function_id_zero(desc)) {
6538 			/* All the vfid is zero, so need to delete this entry */
6539 			status = hclge_remove_mac_vlan_tbl(vport, &req);
6540 			if (!status)
6541 				hdev->used_mc_mac_num--;
6542 		} else {
6543 			/* Not all the vfid is zero, update the vfid */
6544 			status = hclge_add_mac_vlan_tbl(vport, &req, desc);
6545 		}
6546 	} else if (status == -ENOENT) {
6547 		status = 0;
6548 	}
6549 
6550 	return status;
6551 }
6552 
6553 static void hclge_sync_vport_mac_list(struct hclge_vport *vport,
6554 				      struct list_head *list,
6555 				      enum HCLGE_MAC_ADDR_TYPE mac_type)
6556 {
6557 	int (*sync)(struct hclge_vport *vport, const unsigned char *addr);
6558 	struct hclge_mac_node *mac_node, *tmp;
6559 	int ret;
6560 
6561 	if (mac_type == HCLGE_MAC_ADDR_UC)
6562 		sync = hclge_add_uc_addr_common;
6563 	else
6564 		sync = hclge_add_mc_addr_common;
6565 
6566 	list_for_each_entry_safe(mac_node, tmp, list, node) {
6567 		ret = sync(vport, mac_node->mac_addr);
6568 		if (!ret) {
6569 			mac_node->state = HCLGE_MAC_ACTIVE;
6570 		} else {
6571 			set_bit(HCLGE_VPORT_STATE_MAC_TBL_CHANGE,
6572 				&vport->state);
6573 
6574 			/* If one unicast mac address is existing in hardware,
6575 			 * we need to try whether other unicast mac addresses
6576 			 * are new addresses that can be added.
6577 			 * Multicast mac address can be reusable, even though
6578 			 * there is no space to add new multicast mac address,
6579 			 * we should check whether other mac addresses are
6580 			 * existing in hardware for reuse.
6581 			 */
6582 			if ((mac_type == HCLGE_MAC_ADDR_UC && ret != -EEXIST) ||
6583 			    (mac_type == HCLGE_MAC_ADDR_MC && ret != -ENOSPC))
6584 				break;
6585 		}
6586 	}
6587 }
6588 
6589 static void hclge_unsync_vport_mac_list(struct hclge_vport *vport,
6590 					struct list_head *list,
6591 					enum HCLGE_MAC_ADDR_TYPE mac_type)
6592 {
6593 	int (*unsync)(struct hclge_vport *vport, const unsigned char *addr);
6594 	struct hclge_mac_node *mac_node, *tmp;
6595 	int ret;
6596 
6597 	if (mac_type == HCLGE_MAC_ADDR_UC)
6598 		unsync = hclge_rm_uc_addr_common;
6599 	else
6600 		unsync = hclge_rm_mc_addr_common;
6601 
6602 	list_for_each_entry_safe(mac_node, tmp, list, node) {
6603 		ret = unsync(vport, mac_node->mac_addr);
6604 		if (!ret || ret == -ENOENT) {
6605 			list_del(&mac_node->node);
6606 			kfree(mac_node);
6607 		} else {
6608 			set_bit(HCLGE_VPORT_STATE_MAC_TBL_CHANGE,
6609 				&vport->state);
6610 			break;
6611 		}
6612 	}
6613 }
6614 
6615 static bool hclge_sync_from_add_list(struct list_head *add_list,
6616 				     struct list_head *mac_list)
6617 {
6618 	struct hclge_mac_node *mac_node, *tmp, *new_node;
6619 	bool all_added = true;
6620 
6621 	list_for_each_entry_safe(mac_node, tmp, add_list, node) {
6622 		if (mac_node->state == HCLGE_MAC_TO_ADD)
6623 			all_added = false;
6624 
6625 		/* if the mac address from tmp_add_list is not in the
6626 		 * uc/mc_mac_list, it means have received a TO_DEL request
6627 		 * during the time window of adding the mac address into mac
6628 		 * table. if mac_node state is ACTIVE, then change it to TO_DEL,
6629 		 * then it will be removed at next time. else it must be TO_ADD,
6630 		 * this address hasn't been added into mac table,
6631 		 * so just remove the mac node.
6632 		 */
6633 		new_node = hclge_find_mac_node(mac_list, mac_node->mac_addr);
6634 		if (new_node) {
6635 			hclge_update_mac_node(new_node, mac_node->state);
6636 			list_del(&mac_node->node);
6637 			kfree(mac_node);
6638 		} else if (mac_node->state == HCLGE_MAC_ACTIVE) {
6639 			mac_node->state = HCLGE_MAC_TO_DEL;
6640 			list_move_tail(&mac_node->node, mac_list);
6641 		} else {
6642 			list_del(&mac_node->node);
6643 			kfree(mac_node);
6644 		}
6645 	}
6646 
6647 	return all_added;
6648 }
6649 
6650 static void hclge_sync_from_del_list(struct list_head *del_list,
6651 				     struct list_head *mac_list)
6652 {
6653 	struct hclge_mac_node *mac_node, *tmp, *new_node;
6654 
6655 	list_for_each_entry_safe(mac_node, tmp, del_list, node) {
6656 		new_node = hclge_find_mac_node(mac_list, mac_node->mac_addr);
6657 		if (new_node) {
6658 			/* If the mac addr exists in the mac list, it means
6659 			 * received a new TO_ADD request during the time window
6660 			 * of configuring the mac address. For the mac node
6661 			 * state is TO_ADD, and the address is already in the
6662 			 * in the hardware(due to delete fail), so we just need
6663 			 * to change the mac node state to ACTIVE.
6664 			 */
6665 			new_node->state = HCLGE_MAC_ACTIVE;
6666 			list_del(&mac_node->node);
6667 			kfree(mac_node);
6668 		} else {
6669 			list_move_tail(&mac_node->node, mac_list);
6670 		}
6671 	}
6672 }
6673 
6674 static void hclge_update_overflow_flags(struct hclge_vport *vport,
6675 					enum HCLGE_MAC_ADDR_TYPE mac_type,
6676 					bool is_all_added)
6677 {
6678 	if (mac_type == HCLGE_MAC_ADDR_UC) {
6679 		if (is_all_added)
6680 			vport->overflow_promisc_flags &= ~HNAE3_OVERFLOW_UPE;
6681 		else if (hclge_is_umv_space_full(vport, true))
6682 			vport->overflow_promisc_flags |= HNAE3_OVERFLOW_UPE;
6683 	} else {
6684 		if (is_all_added)
6685 			vport->overflow_promisc_flags &= ~HNAE3_OVERFLOW_MPE;
6686 		else
6687 			vport->overflow_promisc_flags |= HNAE3_OVERFLOW_MPE;
6688 	}
6689 }
6690 
6691 static void hclge_sync_vport_mac_table(struct hclge_vport *vport,
6692 				       enum HCLGE_MAC_ADDR_TYPE mac_type)
6693 {
6694 	struct hclge_mac_node *mac_node, *tmp, *new_node;
6695 	struct list_head tmp_add_list, tmp_del_list;
6696 	struct list_head *list;
6697 	bool all_added;
6698 
6699 	INIT_LIST_HEAD(&tmp_add_list);
6700 	INIT_LIST_HEAD(&tmp_del_list);
6701 
6702 	/* move the mac addr to the tmp_add_list and tmp_del_list, then
6703 	 * we can add/delete these mac addr outside the spin lock
6704 	 */
6705 	list = (mac_type == HCLGE_MAC_ADDR_UC) ?
6706 		&vport->uc_mac_list : &vport->mc_mac_list;
6707 
6708 	spin_lock_bh(&vport->mac_list_lock);
6709 
6710 	list_for_each_entry_safe(mac_node, tmp, list, node) {
6711 		switch (mac_node->state) {
6712 		case HCLGE_MAC_TO_DEL:
6713 			list_move_tail(&mac_node->node, &tmp_del_list);
6714 			break;
6715 		case HCLGE_MAC_TO_ADD:
6716 			new_node = kzalloc_obj(*new_node, GFP_ATOMIC);
6717 			if (!new_node)
6718 				goto stop_traverse;
6719 			ether_addr_copy(new_node->mac_addr, mac_node->mac_addr);
6720 			new_node->state = mac_node->state;
6721 			list_add_tail(&new_node->node, &tmp_add_list);
6722 			break;
6723 		default:
6724 			break;
6725 		}
6726 	}
6727 
6728 stop_traverse:
6729 	spin_unlock_bh(&vport->mac_list_lock);
6730 
6731 	/* delete first, in order to get max mac table space for adding */
6732 	hclge_unsync_vport_mac_list(vport, &tmp_del_list, mac_type);
6733 	hclge_sync_vport_mac_list(vport, &tmp_add_list, mac_type);
6734 
6735 	/* if some mac addresses were added/deleted fail, move back to the
6736 	 * mac_list, and retry at next time.
6737 	 */
6738 	spin_lock_bh(&vport->mac_list_lock);
6739 
6740 	hclge_sync_from_del_list(&tmp_del_list, list);
6741 	all_added = hclge_sync_from_add_list(&tmp_add_list, list);
6742 
6743 	spin_unlock_bh(&vport->mac_list_lock);
6744 
6745 	hclge_update_overflow_flags(vport, mac_type, all_added);
6746 }
6747 
6748 static bool hclge_need_sync_mac_table(struct hclge_vport *vport)
6749 {
6750 	struct hclge_dev *hdev = vport->back;
6751 
6752 	if (test_bit(vport->vport_id, hdev->vport_config_block))
6753 		return false;
6754 
6755 	if (test_and_clear_bit(HCLGE_VPORT_STATE_MAC_TBL_CHANGE, &vport->state))
6756 		return true;
6757 
6758 	return false;
6759 }
6760 
6761 static void hclge_sync_mac_table(struct hclge_dev *hdev)
6762 {
6763 	int i;
6764 
6765 	for (i = 0; i < hdev->num_alloc_vport; i++) {
6766 		struct hclge_vport *vport = &hdev->vport[i];
6767 
6768 		if (!hclge_need_sync_mac_table(vport))
6769 			continue;
6770 
6771 		hclge_sync_vport_mac_table(vport, HCLGE_MAC_ADDR_UC);
6772 		hclge_sync_vport_mac_table(vport, HCLGE_MAC_ADDR_MC);
6773 	}
6774 }
6775 
6776 static void hclge_build_del_list(struct list_head *list,
6777 				 bool is_del_list,
6778 				 struct list_head *tmp_del_list)
6779 {
6780 	struct hclge_mac_node *mac_cfg, *tmp;
6781 
6782 	list_for_each_entry_safe(mac_cfg, tmp, list, node) {
6783 		switch (mac_cfg->state) {
6784 		case HCLGE_MAC_TO_DEL:
6785 		case HCLGE_MAC_ACTIVE:
6786 			list_move_tail(&mac_cfg->node, tmp_del_list);
6787 			break;
6788 		case HCLGE_MAC_TO_ADD:
6789 			if (is_del_list) {
6790 				list_del(&mac_cfg->node);
6791 				kfree(mac_cfg);
6792 			}
6793 			break;
6794 		}
6795 	}
6796 }
6797 
6798 static void hclge_unsync_del_list(struct hclge_vport *vport,
6799 				  int (*unsync)(struct hclge_vport *vport,
6800 						const unsigned char *addr),
6801 				  bool is_del_list,
6802 				  struct list_head *tmp_del_list)
6803 {
6804 	struct hclge_mac_node *mac_cfg, *tmp;
6805 	int ret;
6806 
6807 	list_for_each_entry_safe(mac_cfg, tmp, tmp_del_list, node) {
6808 		ret = unsync(vport, mac_cfg->mac_addr);
6809 		if (!ret || ret == -ENOENT) {
6810 			/* clear all mac addr from hardware, but remain these
6811 			 * mac addr in the mac list, and restore them after
6812 			 * vf reset finished.
6813 			 */
6814 			if (!is_del_list &&
6815 			    mac_cfg->state == HCLGE_MAC_ACTIVE) {
6816 				mac_cfg->state = HCLGE_MAC_TO_ADD;
6817 			} else {
6818 				list_del(&mac_cfg->node);
6819 				kfree(mac_cfg);
6820 			}
6821 		} else if (is_del_list) {
6822 			mac_cfg->state = HCLGE_MAC_TO_DEL;
6823 		}
6824 	}
6825 }
6826 
6827 void hclge_rm_vport_all_mac_table(struct hclge_vport *vport, bool is_del_list,
6828 				  enum HCLGE_MAC_ADDR_TYPE mac_type)
6829 {
6830 	int (*unsync)(struct hclge_vport *vport, const unsigned char *addr);
6831 	struct hclge_dev *hdev = vport->back;
6832 	struct list_head tmp_del_list, *list;
6833 
6834 	if (mac_type == HCLGE_MAC_ADDR_UC) {
6835 		list = &vport->uc_mac_list;
6836 		unsync = hclge_rm_uc_addr_common;
6837 	} else {
6838 		list = &vport->mc_mac_list;
6839 		unsync = hclge_rm_mc_addr_common;
6840 	}
6841 
6842 	INIT_LIST_HEAD(&tmp_del_list);
6843 
6844 	if (!is_del_list)
6845 		set_bit(vport->vport_id, hdev->vport_config_block);
6846 
6847 	spin_lock_bh(&vport->mac_list_lock);
6848 
6849 	hclge_build_del_list(list, is_del_list, &tmp_del_list);
6850 
6851 	spin_unlock_bh(&vport->mac_list_lock);
6852 
6853 	hclge_unsync_del_list(vport, unsync, is_del_list, &tmp_del_list);
6854 
6855 	spin_lock_bh(&vport->mac_list_lock);
6856 
6857 	hclge_sync_from_del_list(&tmp_del_list, list);
6858 
6859 	spin_unlock_bh(&vport->mac_list_lock);
6860 }
6861 
6862 /* remove all mac address when uninitailize */
6863 static void hclge_uninit_vport_mac_list(struct hclge_vport *vport,
6864 					enum HCLGE_MAC_ADDR_TYPE mac_type)
6865 {
6866 	struct hclge_mac_node *mac_node, *tmp;
6867 	struct hclge_dev *hdev = vport->back;
6868 	struct list_head tmp_del_list, *list;
6869 
6870 	INIT_LIST_HEAD(&tmp_del_list);
6871 
6872 	list = (mac_type == HCLGE_MAC_ADDR_UC) ?
6873 		&vport->uc_mac_list : &vport->mc_mac_list;
6874 
6875 	spin_lock_bh(&vport->mac_list_lock);
6876 
6877 	list_for_each_entry_safe(mac_node, tmp, list, node) {
6878 		switch (mac_node->state) {
6879 		case HCLGE_MAC_TO_DEL:
6880 		case HCLGE_MAC_ACTIVE:
6881 			list_move_tail(&mac_node->node, &tmp_del_list);
6882 			break;
6883 		case HCLGE_MAC_TO_ADD:
6884 			list_del(&mac_node->node);
6885 			kfree(mac_node);
6886 			break;
6887 		}
6888 	}
6889 
6890 	spin_unlock_bh(&vport->mac_list_lock);
6891 
6892 	hclge_unsync_vport_mac_list(vport, &tmp_del_list, mac_type);
6893 
6894 	if (!list_empty(&tmp_del_list))
6895 		dev_warn(&hdev->pdev->dev,
6896 			 "uninit %s mac list for vport %u not completely.\n",
6897 			 mac_type == HCLGE_MAC_ADDR_UC ? "uc" : "mc",
6898 			 vport->vport_id);
6899 
6900 	list_for_each_entry_safe(mac_node, tmp, &tmp_del_list, node) {
6901 		list_del(&mac_node->node);
6902 		kfree(mac_node);
6903 	}
6904 }
6905 
6906 static void hclge_uninit_mac_table(struct hclge_dev *hdev)
6907 {
6908 	struct hclge_vport *vport;
6909 	int i;
6910 
6911 	for (i = 0; i < hdev->num_alloc_vport; i++) {
6912 		vport = &hdev->vport[i];
6913 		hclge_uninit_vport_mac_list(vport, HCLGE_MAC_ADDR_UC);
6914 		hclge_uninit_vport_mac_list(vport, HCLGE_MAC_ADDR_MC);
6915 	}
6916 }
6917 
6918 static int hclge_get_mac_ethertype_cmd_status(struct hclge_dev *hdev,
6919 					      u16 cmdq_resp, u8 resp_code)
6920 {
6921 #define HCLGE_ETHERTYPE_SUCCESS_ADD		0
6922 #define HCLGE_ETHERTYPE_ALREADY_ADD		1
6923 #define HCLGE_ETHERTYPE_MGR_TBL_OVERFLOW	2
6924 #define HCLGE_ETHERTYPE_KEY_CONFLICT		3
6925 
6926 	int return_status;
6927 
6928 	if (cmdq_resp) {
6929 		dev_err(&hdev->pdev->dev,
6930 			"cmdq execute failed for get_mac_ethertype_cmd_status, status=%u.\n",
6931 			cmdq_resp);
6932 		return -EIO;
6933 	}
6934 
6935 	switch (resp_code) {
6936 	case HCLGE_ETHERTYPE_SUCCESS_ADD:
6937 	case HCLGE_ETHERTYPE_ALREADY_ADD:
6938 		return_status = 0;
6939 		break;
6940 	case HCLGE_ETHERTYPE_MGR_TBL_OVERFLOW:
6941 		dev_err(&hdev->pdev->dev,
6942 			"add mac ethertype failed for manager table overflow.\n");
6943 		return_status = -EIO;
6944 		break;
6945 	case HCLGE_ETHERTYPE_KEY_CONFLICT:
6946 		dev_err(&hdev->pdev->dev,
6947 			"add mac ethertype failed for key conflict.\n");
6948 		return_status = -EIO;
6949 		break;
6950 	default:
6951 		dev_err(&hdev->pdev->dev,
6952 			"add mac ethertype failed for undefined, code=%u.\n",
6953 			resp_code);
6954 		return_status = -EIO;
6955 	}
6956 
6957 	return return_status;
6958 }
6959 
6960 static int hclge_set_vf_mac(struct hnae3_handle *handle, int vf,
6961 			    u8 *mac_addr)
6962 {
6963 	struct hclge_vport *vport = hclge_get_vport(handle);
6964 	char format_mac_addr[HNAE3_FORMAT_MAC_ADDR_LEN];
6965 	struct hclge_dev *hdev = vport->back;
6966 
6967 	vport = hclge_get_vf_vport(hdev, vf);
6968 	if (!vport)
6969 		return -EINVAL;
6970 
6971 	hnae3_format_mac_addr(format_mac_addr, mac_addr);
6972 	if (ether_addr_equal(mac_addr, vport->vf_info.mac)) {
6973 		dev_info(&hdev->pdev->dev,
6974 			 "Specified MAC(=%s) is same as before, no change committed!\n",
6975 			 format_mac_addr);
6976 		return 0;
6977 	}
6978 
6979 	ether_addr_copy(vport->vf_info.mac, mac_addr);
6980 
6981 	/* there is a timewindow for PF to know VF unalive, it may
6982 	 * cause send mailbox fail, but it doesn't matter, VF will
6983 	 * query it when reinit.
6984 	 */
6985 	if (test_bit(HCLGE_VPORT_STATE_ALIVE, &vport->state)) {
6986 		dev_info(&hdev->pdev->dev,
6987 			 "MAC of VF %d has been set to %s, and it will be reinitialized!\n",
6988 			 vf, format_mac_addr);
6989 		(void)hclge_inform_reset_assert_to_vf(vport);
6990 		return 0;
6991 	}
6992 
6993 	dev_info(&hdev->pdev->dev,
6994 		 "MAC of VF %d has been set to %s, will be active after VF reset\n",
6995 		 vf, format_mac_addr);
6996 	return 0;
6997 }
6998 
6999 static int hclge_add_mgr_tbl(struct hclge_dev *hdev,
7000 			     const struct hclge_mac_mgr_tbl_entry_cmd *req)
7001 {
7002 	struct hclge_desc desc;
7003 	u8 resp_code;
7004 	u16 retval;
7005 	int ret;
7006 
7007 	hclge_cmd_setup_basic_desc(&desc, HCLGE_OPC_MAC_ETHTYPE_ADD, false);
7008 	memcpy(desc.data, req, sizeof(struct hclge_mac_mgr_tbl_entry_cmd));
7009 
7010 	ret = hclge_cmd_send(&hdev->hw, &desc, 1);
7011 	if (ret) {
7012 		dev_err(&hdev->pdev->dev,
7013 			"add mac ethertype failed for cmd_send, ret =%d.\n",
7014 			ret);
7015 		return ret;
7016 	}
7017 
7018 	resp_code = (le32_to_cpu(desc.data[0]) >> 8) & 0xff;
7019 	retval = le16_to_cpu(desc.retval);
7020 
7021 	return hclge_get_mac_ethertype_cmd_status(hdev, retval, resp_code);
7022 }
7023 
7024 static int init_mgr_tbl(struct hclge_dev *hdev)
7025 {
7026 	int ret;
7027 	u32 i;
7028 
7029 	for (i = 0; i < ARRAY_SIZE(hclge_mgr_table); i++) {
7030 		ret = hclge_add_mgr_tbl(hdev, &hclge_mgr_table[i]);
7031 		if (ret) {
7032 			dev_err(&hdev->pdev->dev,
7033 				"add mac ethertype failed, ret =%d.\n",
7034 				ret);
7035 			return ret;
7036 		}
7037 	}
7038 
7039 	return 0;
7040 }
7041 
7042 static void hclge_get_mac_addr(struct hnae3_handle *handle, u8 *p)
7043 {
7044 	struct hclge_vport *vport = hclge_get_vport(handle);
7045 	struct hclge_dev *hdev = vport->back;
7046 
7047 	ether_addr_copy(p, hdev->hw.mac.mac_addr);
7048 }
7049 
7050 int hclge_update_mac_node_for_dev_addr(struct hclge_vport *vport,
7051 				       const u8 *old_addr, const u8 *new_addr)
7052 {
7053 	struct list_head *list = &vport->uc_mac_list;
7054 	struct hclge_mac_node *old_node, *new_node;
7055 
7056 	new_node = hclge_find_mac_node(list, new_addr);
7057 	if (!new_node) {
7058 		new_node = kzalloc_obj(*new_node, GFP_ATOMIC);
7059 		if (!new_node)
7060 			return -ENOMEM;
7061 
7062 		new_node->state = HCLGE_MAC_TO_ADD;
7063 		ether_addr_copy(new_node->mac_addr, new_addr);
7064 		list_add(&new_node->node, list);
7065 	} else {
7066 		if (new_node->state == HCLGE_MAC_TO_DEL)
7067 			new_node->state = HCLGE_MAC_ACTIVE;
7068 
7069 		/* make sure the new addr is in the list head, avoid dev
7070 		 * addr may be not re-added into mac table for the umv space
7071 		 * limitation after global/imp reset which will clear mac
7072 		 * table by hardware.
7073 		 */
7074 		list_move(&new_node->node, list);
7075 	}
7076 
7077 	if (old_addr && !ether_addr_equal(old_addr, new_addr)) {
7078 		old_node = hclge_find_mac_node(list, old_addr);
7079 		if (old_node) {
7080 			if (old_node->state == HCLGE_MAC_TO_ADD) {
7081 				list_del(&old_node->node);
7082 				kfree(old_node);
7083 			} else {
7084 				old_node->state = HCLGE_MAC_TO_DEL;
7085 			}
7086 		}
7087 	}
7088 
7089 	set_bit(HCLGE_VPORT_STATE_MAC_TBL_CHANGE, &vport->state);
7090 
7091 	return 0;
7092 }
7093 
7094 static int hclge_set_mac_addr(struct hnae3_handle *handle, const void *p,
7095 			      bool is_first)
7096 {
7097 	const unsigned char *new_addr = (const unsigned char *)p;
7098 	struct hclge_vport *vport = hclge_get_vport(handle);
7099 	char format_mac_addr[HNAE3_FORMAT_MAC_ADDR_LEN];
7100 	struct hclge_dev *hdev = vport->back;
7101 	unsigned char *old_addr = NULL;
7102 	int ret;
7103 
7104 	/* mac addr check */
7105 	if (is_zero_ether_addr(new_addr) ||
7106 	    is_broadcast_ether_addr(new_addr) ||
7107 	    is_multicast_ether_addr(new_addr)) {
7108 		hnae3_format_mac_addr(format_mac_addr, new_addr);
7109 		dev_err(&hdev->pdev->dev,
7110 			"change uc mac err! invalid mac: %s.\n",
7111 			 format_mac_addr);
7112 		return -EINVAL;
7113 	}
7114 
7115 	ret = hclge_pause_addr_cfg(hdev, new_addr);
7116 	if (ret) {
7117 		dev_err(&hdev->pdev->dev,
7118 			"failed to configure mac pause address, ret = %d\n",
7119 			ret);
7120 		return ret;
7121 	}
7122 
7123 	if (!is_first)
7124 		old_addr = hdev->hw.mac.mac_addr;
7125 
7126 	spin_lock_bh(&vport->mac_list_lock);
7127 	ret = hclge_update_mac_node_for_dev_addr(vport, old_addr, new_addr);
7128 	if (ret) {
7129 		hnae3_format_mac_addr(format_mac_addr, new_addr);
7130 		dev_err(&hdev->pdev->dev,
7131 			"failed to change the mac addr:%s, ret = %d\n",
7132 			format_mac_addr, ret);
7133 		spin_unlock_bh(&vport->mac_list_lock);
7134 
7135 		if (!is_first)
7136 			hclge_pause_addr_cfg(hdev, old_addr);
7137 
7138 		return ret;
7139 	}
7140 	/* we must update dev addr with spin lock protect, preventing dev addr
7141 	 * being removed by set_rx_mode path.
7142 	 */
7143 	ether_addr_copy(hdev->hw.mac.mac_addr, new_addr);
7144 	spin_unlock_bh(&vport->mac_list_lock);
7145 
7146 	hclge_task_schedule(hdev, 0);
7147 
7148 	return 0;
7149 }
7150 
7151 static int hclge_mii_ioctl(struct hclge_dev *hdev, struct ifreq *ifr, int cmd)
7152 {
7153 	struct mii_ioctl_data *data = if_mii(ifr);
7154 
7155 	if (!hnae3_dev_phy_imp_supported(hdev))
7156 		return -EOPNOTSUPP;
7157 
7158 	switch (cmd) {
7159 	case SIOCGMIIPHY:
7160 		data->phy_id = hdev->hw.mac.phy_addr;
7161 		/* this command reads phy id and register at the same time */
7162 		fallthrough;
7163 	case SIOCGMIIREG:
7164 		return hclge_read_phy_reg(hdev, data->reg_num, &data->val_out);
7165 
7166 	case SIOCSMIIREG:
7167 		return hclge_write_phy_reg(hdev, data->reg_num, data->val_in);
7168 	default:
7169 		return -EOPNOTSUPP;
7170 	}
7171 }
7172 
7173 static int hclge_do_ioctl(struct hnae3_handle *handle, struct ifreq *ifr,
7174 			  int cmd)
7175 {
7176 	struct hclge_vport *vport = hclge_get_vport(handle);
7177 	struct hclge_dev *hdev = vport->back;
7178 
7179 	if (!hdev->hw.mac.phydev)
7180 		return hclge_mii_ioctl(hdev, ifr, cmd);
7181 
7182 	return phy_mii_ioctl(hdev->hw.mac.phydev, ifr, cmd);
7183 }
7184 
7185 static int hclge_set_port_vlan_filter_bypass(struct hclge_dev *hdev, u8 vf_id,
7186 					     bool bypass_en)
7187 {
7188 	struct hclge_port_vlan_filter_bypass_cmd *req;
7189 	struct hclge_desc desc;
7190 	int ret;
7191 
7192 	hclge_cmd_setup_basic_desc(&desc, HCLGE_OPC_PORT_VLAN_BYPASS, false);
7193 	req = (struct hclge_port_vlan_filter_bypass_cmd *)desc.data;
7194 	req->vf_id = vf_id;
7195 	hnae3_set_bit(req->bypass_state, HCLGE_INGRESS_BYPASS_B,
7196 		      bypass_en ? 1 : 0);
7197 
7198 	ret = hclge_cmd_send(&hdev->hw, &desc, 1);
7199 	if (ret)
7200 		dev_err(&hdev->pdev->dev,
7201 			"failed to set vport%u port vlan filter bypass state, ret = %d.\n",
7202 			vf_id, ret);
7203 
7204 	return ret;
7205 }
7206 
7207 static int hclge_set_vlan_filter_ctrl(struct hclge_dev *hdev, u8 vlan_type,
7208 				      u8 fe_type, bool filter_en, u8 vf_id)
7209 {
7210 	struct hclge_vlan_filter_ctrl_cmd *req;
7211 	struct hclge_desc desc;
7212 	int ret;
7213 
7214 	/* read current vlan filter parameter */
7215 	hclge_cmd_setup_basic_desc(&desc, HCLGE_OPC_VLAN_FILTER_CTRL, true);
7216 	req = (struct hclge_vlan_filter_ctrl_cmd *)desc.data;
7217 	req->vlan_type = vlan_type;
7218 	req->vf_id = vf_id;
7219 
7220 	ret = hclge_cmd_send(&hdev->hw, &desc, 1);
7221 	if (ret) {
7222 		dev_err(&hdev->pdev->dev, "failed to get vport%u vlan filter config, ret = %d.\n",
7223 			vf_id, ret);
7224 		return ret;
7225 	}
7226 
7227 	/* modify and write new config parameter */
7228 	hclge_comm_cmd_reuse_desc(&desc, false);
7229 	req->vlan_fe = filter_en ?
7230 			(req->vlan_fe | fe_type) : (req->vlan_fe & ~fe_type);
7231 
7232 	ret = hclge_cmd_send(&hdev->hw, &desc, 1);
7233 	if (ret)
7234 		dev_err(&hdev->pdev->dev, "failed to set vport%u vlan filter, ret = %d.\n",
7235 			vf_id, ret);
7236 
7237 	return ret;
7238 }
7239 
7240 static int hclge_set_vport_vlan_filter(struct hclge_vport *vport, bool enable)
7241 {
7242 	struct hclge_dev *hdev = vport->back;
7243 	struct hnae3_ae_dev *ae_dev = hdev->ae_dev;
7244 	int ret;
7245 
7246 	if (hdev->ae_dev->dev_version < HNAE3_DEVICE_VERSION_V2)
7247 		return hclge_set_vlan_filter_ctrl(hdev, HCLGE_FILTER_TYPE_VF,
7248 						  HCLGE_FILTER_FE_EGRESS_V1_B,
7249 						  enable, vport->vport_id);
7250 
7251 	ret = hclge_set_vlan_filter_ctrl(hdev, HCLGE_FILTER_TYPE_VF,
7252 					 HCLGE_FILTER_FE_EGRESS, enable,
7253 					 vport->vport_id);
7254 	if (ret)
7255 		return ret;
7256 
7257 	if (test_bit(HNAE3_DEV_SUPPORT_PORT_VLAN_BYPASS_B, ae_dev->caps)) {
7258 		ret = hclge_set_port_vlan_filter_bypass(hdev, vport->vport_id,
7259 							!enable);
7260 	} else if (!vport->vport_id) {
7261 		if (test_bit(HNAE3_DEV_SUPPORT_VLAN_FLTR_MDF_B, ae_dev->caps))
7262 			enable = false;
7263 
7264 		ret = hclge_set_vlan_filter_ctrl(hdev, HCLGE_FILTER_TYPE_PORT,
7265 						 HCLGE_FILTER_FE_INGRESS,
7266 						 enable, 0);
7267 	}
7268 
7269 	return ret;
7270 }
7271 
7272 static bool hclge_need_enable_vport_vlan_filter(struct hclge_vport *vport)
7273 {
7274 	struct hnae3_handle *handle = &vport->nic;
7275 	struct hclge_vport_vlan_cfg *vlan, *tmp;
7276 	struct hclge_dev *hdev = vport->back;
7277 
7278 	if (vport->vport_id) {
7279 		if (vport->port_base_vlan_cfg.state !=
7280 			HNAE3_PORT_BASE_VLAN_DISABLE)
7281 			return true;
7282 
7283 		if (vport->vf_info.trusted && vport->vf_info.request_uc_en)
7284 			return false;
7285 	} else if (handle->netdev_flags & HNAE3_USER_UPE) {
7286 		return false;
7287 	}
7288 
7289 	if (!vport->req_vlan_fltr_en)
7290 		return false;
7291 
7292 	/* compatible with former device, always enable vlan filter */
7293 	if (!test_bit(HNAE3_DEV_SUPPORT_VLAN_FLTR_MDF_B, hdev->ae_dev->caps))
7294 		return true;
7295 
7296 	list_for_each_entry_safe(vlan, tmp, &vport->vlan_list, node)
7297 		if (vlan->vlan_id != 0)
7298 			return true;
7299 
7300 	return false;
7301 }
7302 
7303 static int __hclge_enable_vport_vlan_filter(struct hclge_vport *vport,
7304 					    bool request_en)
7305 {
7306 	bool need_en;
7307 	int ret;
7308 
7309 	need_en = hclge_need_enable_vport_vlan_filter(vport);
7310 	if (need_en == vport->cur_vlan_fltr_en)
7311 		return 0;
7312 
7313 	ret = hclge_set_vport_vlan_filter(vport, need_en);
7314 	if (ret)
7315 		return ret;
7316 
7317 	vport->cur_vlan_fltr_en = need_en;
7318 
7319 	return 0;
7320 }
7321 
7322 int hclge_enable_vport_vlan_filter(struct hclge_vport *vport, bool request_en)
7323 {
7324 	struct hclge_dev *hdev = vport->back;
7325 	int ret;
7326 
7327 	mutex_lock(&hdev->vport_lock);
7328 	vport->req_vlan_fltr_en = request_en;
7329 	ret = __hclge_enable_vport_vlan_filter(vport, request_en);
7330 	mutex_unlock(&hdev->vport_lock);
7331 
7332 	return ret;
7333 }
7334 
7335 static int hclge_enable_vlan_filter(struct hnae3_handle *handle, bool enable)
7336 {
7337 	struct hclge_vport *vport = hclge_get_vport(handle);
7338 
7339 	return hclge_enable_vport_vlan_filter(vport, enable);
7340 }
7341 
7342 static int hclge_set_vf_vlan_filter_cmd(struct hclge_dev *hdev, u16 vfid,
7343 					bool is_kill, u16 vlan,
7344 					struct hclge_desc *desc)
7345 {
7346 	struct hclge_vlan_filter_vf_cfg_cmd *req0;
7347 	struct hclge_vlan_filter_vf_cfg_cmd *req1;
7348 	u8 vf_byte_val;
7349 	u8 vf_byte_off;
7350 	int ret;
7351 
7352 	hclge_cmd_setup_basic_desc(&desc[0],
7353 				   HCLGE_OPC_VLAN_FILTER_VF_CFG, false);
7354 	hclge_cmd_setup_basic_desc(&desc[1],
7355 				   HCLGE_OPC_VLAN_FILTER_VF_CFG, false);
7356 
7357 	desc[0].flag |= cpu_to_le16(HCLGE_COMM_CMD_FLAG_NEXT);
7358 
7359 	vf_byte_off = vfid / 8;
7360 	vf_byte_val = 1 << (vfid % 8);
7361 
7362 	req0 = (struct hclge_vlan_filter_vf_cfg_cmd *)desc[0].data;
7363 	req1 = (struct hclge_vlan_filter_vf_cfg_cmd *)desc[1].data;
7364 
7365 	req0->vlan_id  = cpu_to_le16(vlan);
7366 	req0->vlan_cfg = is_kill;
7367 
7368 	if (vf_byte_off < HCLGE_MAX_VF_BYTES)
7369 		req0->vf_bitmap[vf_byte_off] = vf_byte_val;
7370 	else
7371 		req1->vf_bitmap[vf_byte_off - HCLGE_MAX_VF_BYTES] = vf_byte_val;
7372 
7373 	ret = hclge_cmd_send(&hdev->hw, desc, 2);
7374 	if (ret) {
7375 		dev_err(&hdev->pdev->dev,
7376 			"Send vf vlan command fail, ret =%d.\n",
7377 			ret);
7378 		return ret;
7379 	}
7380 
7381 	return 0;
7382 }
7383 
7384 static int hclge_check_vf_vlan_cmd_status(struct hclge_dev *hdev, u16 vfid,
7385 					  bool is_kill, struct hclge_desc *desc)
7386 {
7387 	struct hclge_vlan_filter_vf_cfg_cmd *req;
7388 
7389 	req = (struct hclge_vlan_filter_vf_cfg_cmd *)desc[0].data;
7390 
7391 	if (!is_kill) {
7392 #define HCLGE_VF_VLAN_NO_ENTRY	2
7393 		if (!req->resp_code || req->resp_code == 1)
7394 			return 0;
7395 
7396 		if (req->resp_code == HCLGE_VF_VLAN_NO_ENTRY) {
7397 			set_bit(vfid, hdev->vf_vlan_full);
7398 			dev_warn(&hdev->pdev->dev,
7399 				 "vf vlan table is full, vf vlan filter is disabled\n");
7400 			return 0;
7401 		}
7402 
7403 		dev_err(&hdev->pdev->dev,
7404 			"Add vf vlan filter fail, ret =%u.\n",
7405 			req->resp_code);
7406 	} else {
7407 #define HCLGE_VF_VLAN_DEL_NO_FOUND	1
7408 		if (!req->resp_code)
7409 			return 0;
7410 
7411 		/* vf vlan filter is disabled when vf vlan table is full,
7412 		 * then new vlan id will not be added into vf vlan table.
7413 		 * Just return 0 without warning, avoid massive verbose
7414 		 * print logs when unload.
7415 		 */
7416 		if (req->resp_code == HCLGE_VF_VLAN_DEL_NO_FOUND)
7417 			return 0;
7418 
7419 		dev_err(&hdev->pdev->dev,
7420 			"Kill vf vlan filter fail, ret =%u.\n",
7421 			req->resp_code);
7422 	}
7423 
7424 	return -EIO;
7425 }
7426 
7427 static int hclge_set_vf_vlan_common(struct hclge_dev *hdev, u16 vfid,
7428 				    bool is_kill, u16 vlan)
7429 {
7430 	struct hclge_vport *vport = &hdev->vport[vfid];
7431 	struct hclge_desc desc[2];
7432 	int ret;
7433 
7434 	/* if vf vlan table is full, firmware will close vf vlan filter, it
7435 	 * is unable and unnecessary to add new vlan id to vf vlan filter.
7436 	 * If spoof check is enable, and vf vlan is full, it shouldn't add
7437 	 * new vlan, because tx packets with these vlan id will be dropped.
7438 	 */
7439 	if (test_bit(vfid, hdev->vf_vlan_full) && !is_kill) {
7440 		if (vport->vf_info.spoofchk && vlan) {
7441 			dev_err(&hdev->pdev->dev,
7442 				"Can't add vlan due to spoof check is on and vf vlan table is full\n");
7443 			return -EPERM;
7444 		}
7445 		return 0;
7446 	}
7447 
7448 	ret = hclge_set_vf_vlan_filter_cmd(hdev, vfid, is_kill, vlan, desc);
7449 	if (ret)
7450 		return ret;
7451 
7452 	return hclge_check_vf_vlan_cmd_status(hdev, vfid, is_kill, desc);
7453 }
7454 
7455 static int hclge_set_port_vlan_filter(struct hclge_dev *hdev, __be16 proto,
7456 				      u16 vlan_id, bool is_kill)
7457 {
7458 	struct hclge_vlan_filter_pf_cfg_cmd *req;
7459 	struct hclge_desc desc;
7460 	u8 vlan_offset_byte_val;
7461 	u8 vlan_offset_byte;
7462 	u8 vlan_offset_160;
7463 	int ret;
7464 
7465 	hclge_cmd_setup_basic_desc(&desc, HCLGE_OPC_VLAN_FILTER_PF_CFG, false);
7466 
7467 	vlan_offset_160 = vlan_id / HCLGE_VLAN_ID_OFFSET_STEP;
7468 	vlan_offset_byte = (vlan_id % HCLGE_VLAN_ID_OFFSET_STEP) /
7469 			   HCLGE_VLAN_BYTE_SIZE;
7470 	vlan_offset_byte_val = 1 << (vlan_id % HCLGE_VLAN_BYTE_SIZE);
7471 
7472 	req = (struct hclge_vlan_filter_pf_cfg_cmd *)desc.data;
7473 	req->vlan_offset = vlan_offset_160;
7474 	req->vlan_cfg = is_kill;
7475 	req->vlan_offset_bitmap[vlan_offset_byte] = vlan_offset_byte_val;
7476 
7477 	ret = hclge_cmd_send(&hdev->hw, &desc, 1);
7478 	if (ret)
7479 		dev_err(&hdev->pdev->dev,
7480 			"port vlan command, send fail, ret =%d.\n", ret);
7481 	return ret;
7482 }
7483 
7484 static bool hclge_need_update_port_vlan(struct hclge_dev *hdev, u16 vport_id,
7485 					u16 vlan_id, bool is_kill)
7486 {
7487 	/* vlan 0 may be added twice when 8021q module is enabled */
7488 	if (!is_kill && !vlan_id &&
7489 	    test_bit(vport_id, hdev->vlan_table[vlan_id]))
7490 		return false;
7491 
7492 	if (!is_kill && test_and_set_bit(vport_id, hdev->vlan_table[vlan_id])) {
7493 		dev_warn(&hdev->pdev->dev,
7494 			 "Add port vlan failed, vport %u is already in vlan %u\n",
7495 			 vport_id, vlan_id);
7496 		return false;
7497 	}
7498 
7499 	if (is_kill &&
7500 	    !test_and_clear_bit(vport_id, hdev->vlan_table[vlan_id])) {
7501 		dev_warn(&hdev->pdev->dev,
7502 			 "Delete port vlan failed, vport %u is not in vlan %u\n",
7503 			 vport_id, vlan_id);
7504 		return false;
7505 	}
7506 
7507 	return true;
7508 }
7509 
7510 static int hclge_set_vlan_filter_hw(struct hclge_dev *hdev, __be16 proto,
7511 				    u16 vport_id, u16 vlan_id,
7512 				    bool is_kill)
7513 {
7514 	u16 vport_idx, vport_num = 0;
7515 	int ret;
7516 
7517 	if (is_kill && !vlan_id)
7518 		return 0;
7519 
7520 	if (vlan_id >= VLAN_N_VID)
7521 		return -EINVAL;
7522 
7523 	ret = hclge_set_vf_vlan_common(hdev, vport_id, is_kill, vlan_id);
7524 	if (ret) {
7525 		dev_err(&hdev->pdev->dev,
7526 			"Set %u vport vlan filter config fail, ret =%d.\n",
7527 			vport_id, ret);
7528 		return ret;
7529 	}
7530 
7531 	if (!hclge_need_update_port_vlan(hdev, vport_id, vlan_id, is_kill))
7532 		return 0;
7533 
7534 	for_each_set_bit(vport_idx, hdev->vlan_table[vlan_id], HCLGE_VPORT_NUM)
7535 		vport_num++;
7536 
7537 	if ((is_kill && vport_num == 0) || (!is_kill && vport_num == 1))
7538 		ret = hclge_set_port_vlan_filter(hdev, proto, vlan_id,
7539 						 is_kill);
7540 
7541 	return ret;
7542 }
7543 
7544 static int hclge_set_vlan_tx_offload_cfg(struct hclge_vport *vport)
7545 {
7546 	struct hclge_tx_vtag_cfg *vcfg = &vport->txvlan_cfg;
7547 	struct hclge_vport_vtag_tx_cfg_cmd *req;
7548 	struct hclge_dev *hdev = vport->back;
7549 	struct hclge_desc desc;
7550 	u16 bmap_index;
7551 	int status;
7552 
7553 	hclge_cmd_setup_basic_desc(&desc, HCLGE_OPC_VLAN_PORT_TX_CFG, false);
7554 
7555 	req = (struct hclge_vport_vtag_tx_cfg_cmd *)desc.data;
7556 	req->def_vlan_tag1 = cpu_to_le16(vcfg->default_tag1);
7557 	req->def_vlan_tag2 = cpu_to_le16(vcfg->default_tag2);
7558 	hnae3_set_bit(req->vport_vlan_cfg, HCLGE_ACCEPT_TAG1_B,
7559 		      vcfg->accept_tag1 ? 1 : 0);
7560 	hnae3_set_bit(req->vport_vlan_cfg, HCLGE_ACCEPT_UNTAG1_B,
7561 		      vcfg->accept_untag1 ? 1 : 0);
7562 	hnae3_set_bit(req->vport_vlan_cfg, HCLGE_ACCEPT_TAG2_B,
7563 		      vcfg->accept_tag2 ? 1 : 0);
7564 	hnae3_set_bit(req->vport_vlan_cfg, HCLGE_ACCEPT_UNTAG2_B,
7565 		      vcfg->accept_untag2 ? 1 : 0);
7566 	hnae3_set_bit(req->vport_vlan_cfg, HCLGE_PORT_INS_TAG1_EN_B,
7567 		      vcfg->insert_tag1_en ? 1 : 0);
7568 	hnae3_set_bit(req->vport_vlan_cfg, HCLGE_PORT_INS_TAG2_EN_B,
7569 		      vcfg->insert_tag2_en ? 1 : 0);
7570 	hnae3_set_bit(req->vport_vlan_cfg, HCLGE_TAG_SHIFT_MODE_EN_B,
7571 		      vcfg->tag_shift_mode_en ? 1 : 0);
7572 	hnae3_set_bit(req->vport_vlan_cfg, HCLGE_CFG_NIC_ROCE_SEL_B, 0);
7573 
7574 	req->vf_offset = vport->vport_id / HCLGE_VF_NUM_PER_CMD;
7575 	bmap_index = vport->vport_id % HCLGE_VF_NUM_PER_CMD /
7576 			HCLGE_VF_NUM_PER_BYTE;
7577 	req->vf_bitmap[bmap_index] =
7578 		1U << (vport->vport_id % HCLGE_VF_NUM_PER_BYTE);
7579 
7580 	status = hclge_cmd_send(&hdev->hw, &desc, 1);
7581 	if (status)
7582 		dev_err(&hdev->pdev->dev,
7583 			"Send port txvlan cfg command fail, ret =%d\n",
7584 			status);
7585 
7586 	return status;
7587 }
7588 
7589 static int hclge_set_vlan_rx_offload_cfg(struct hclge_vport *vport)
7590 {
7591 	struct hclge_rx_vtag_cfg *vcfg = &vport->rxvlan_cfg;
7592 	struct hclge_vport_vtag_rx_cfg_cmd *req;
7593 	struct hclge_dev *hdev = vport->back;
7594 	struct hclge_desc desc;
7595 	u16 bmap_index;
7596 	int status;
7597 
7598 	hclge_cmd_setup_basic_desc(&desc, HCLGE_OPC_VLAN_PORT_RX_CFG, false);
7599 
7600 	req = (struct hclge_vport_vtag_rx_cfg_cmd *)desc.data;
7601 	hnae3_set_bit(req->vport_vlan_cfg, HCLGE_REM_TAG1_EN_B,
7602 		      vcfg->strip_tag1_en ? 1 : 0);
7603 	hnae3_set_bit(req->vport_vlan_cfg, HCLGE_REM_TAG2_EN_B,
7604 		      vcfg->strip_tag2_en ? 1 : 0);
7605 	hnae3_set_bit(req->vport_vlan_cfg, HCLGE_SHOW_TAG1_EN_B,
7606 		      vcfg->vlan1_vlan_prionly ? 1 : 0);
7607 	hnae3_set_bit(req->vport_vlan_cfg, HCLGE_SHOW_TAG2_EN_B,
7608 		      vcfg->vlan2_vlan_prionly ? 1 : 0);
7609 	hnae3_set_bit(req->vport_vlan_cfg, HCLGE_DISCARD_TAG1_EN_B,
7610 		      vcfg->strip_tag1_discard_en ? 1 : 0);
7611 	hnae3_set_bit(req->vport_vlan_cfg, HCLGE_DISCARD_TAG2_EN_B,
7612 		      vcfg->strip_tag2_discard_en ? 1 : 0);
7613 
7614 	req->vf_offset = vport->vport_id / HCLGE_VF_NUM_PER_CMD;
7615 	bmap_index = vport->vport_id % HCLGE_VF_NUM_PER_CMD /
7616 			HCLGE_VF_NUM_PER_BYTE;
7617 	req->vf_bitmap[bmap_index] =
7618 		1U << (vport->vport_id % HCLGE_VF_NUM_PER_BYTE);
7619 
7620 	status = hclge_cmd_send(&hdev->hw, &desc, 1);
7621 	if (status)
7622 		dev_err(&hdev->pdev->dev,
7623 			"Send port rxvlan cfg command fail, ret =%d\n",
7624 			status);
7625 
7626 	return status;
7627 }
7628 
7629 static int hclge_vlan_offload_cfg(struct hclge_vport *vport,
7630 				  u16 port_base_vlan_state,
7631 				  u16 vlan_tag, u8 qos)
7632 {
7633 	int ret;
7634 
7635 	if (port_base_vlan_state == HNAE3_PORT_BASE_VLAN_DISABLE) {
7636 		vport->txvlan_cfg.accept_tag1 = true;
7637 		vport->txvlan_cfg.insert_tag1_en = false;
7638 		vport->txvlan_cfg.default_tag1 = 0;
7639 	} else {
7640 		struct hnae3_ae_dev *ae_dev = pci_get_drvdata(vport->nic.pdev);
7641 
7642 		vport->txvlan_cfg.accept_tag1 =
7643 			ae_dev->dev_version >= HNAE3_DEVICE_VERSION_V3;
7644 		vport->txvlan_cfg.insert_tag1_en = true;
7645 		vport->txvlan_cfg.default_tag1 = (qos << VLAN_PRIO_SHIFT) |
7646 						 vlan_tag;
7647 	}
7648 
7649 	vport->txvlan_cfg.accept_untag1 = true;
7650 
7651 	/* accept_tag2 and accept_untag2 are not supported on
7652 	 * pdev revision(0x20), new revision support them,
7653 	 * this two fields can not be configured by user.
7654 	 */
7655 	vport->txvlan_cfg.accept_tag2 = true;
7656 	vport->txvlan_cfg.accept_untag2 = true;
7657 	vport->txvlan_cfg.insert_tag2_en = false;
7658 	vport->txvlan_cfg.default_tag2 = 0;
7659 	vport->txvlan_cfg.tag_shift_mode_en = true;
7660 
7661 	if (port_base_vlan_state == HNAE3_PORT_BASE_VLAN_DISABLE) {
7662 		vport->rxvlan_cfg.strip_tag1_en = false;
7663 		vport->rxvlan_cfg.strip_tag2_en =
7664 				vport->rxvlan_cfg.rx_vlan_offload_en;
7665 		vport->rxvlan_cfg.strip_tag2_discard_en = false;
7666 	} else {
7667 		vport->rxvlan_cfg.strip_tag1_en =
7668 				vport->rxvlan_cfg.rx_vlan_offload_en;
7669 		vport->rxvlan_cfg.strip_tag2_en = true;
7670 		vport->rxvlan_cfg.strip_tag2_discard_en = true;
7671 	}
7672 
7673 	vport->rxvlan_cfg.strip_tag1_discard_en = false;
7674 	vport->rxvlan_cfg.vlan1_vlan_prionly = false;
7675 	vport->rxvlan_cfg.vlan2_vlan_prionly = false;
7676 
7677 	ret = hclge_set_vlan_tx_offload_cfg(vport);
7678 	if (ret)
7679 		return ret;
7680 
7681 	return hclge_set_vlan_rx_offload_cfg(vport);
7682 }
7683 
7684 static int hclge_set_vlan_protocol_type(struct hclge_dev *hdev)
7685 {
7686 	struct hclge_rx_vlan_type_cfg_cmd *rx_req;
7687 	struct hclge_tx_vlan_type_cfg_cmd *tx_req;
7688 	struct hclge_desc desc;
7689 	int status;
7690 
7691 	hclge_cmd_setup_basic_desc(&desc, HCLGE_OPC_MAC_VLAN_TYPE_ID, false);
7692 	rx_req = (struct hclge_rx_vlan_type_cfg_cmd *)desc.data;
7693 	rx_req->ot_fst_vlan_type =
7694 		cpu_to_le16(hdev->vlan_type_cfg.rx_ot_fst_vlan_type);
7695 	rx_req->ot_sec_vlan_type =
7696 		cpu_to_le16(hdev->vlan_type_cfg.rx_ot_sec_vlan_type);
7697 	rx_req->in_fst_vlan_type =
7698 		cpu_to_le16(hdev->vlan_type_cfg.rx_in_fst_vlan_type);
7699 	rx_req->in_sec_vlan_type =
7700 		cpu_to_le16(hdev->vlan_type_cfg.rx_in_sec_vlan_type);
7701 
7702 	status = hclge_cmd_send(&hdev->hw, &desc, 1);
7703 	if (status) {
7704 		dev_err(&hdev->pdev->dev,
7705 			"Send rxvlan protocol type command fail, ret =%d\n",
7706 			status);
7707 		return status;
7708 	}
7709 
7710 	hclge_cmd_setup_basic_desc(&desc, HCLGE_OPC_MAC_VLAN_INSERT, false);
7711 
7712 	tx_req = (struct hclge_tx_vlan_type_cfg_cmd *)desc.data;
7713 	tx_req->ot_vlan_type = cpu_to_le16(hdev->vlan_type_cfg.tx_ot_vlan_type);
7714 	tx_req->in_vlan_type = cpu_to_le16(hdev->vlan_type_cfg.tx_in_vlan_type);
7715 
7716 	status = hclge_cmd_send(&hdev->hw, &desc, 1);
7717 	if (status)
7718 		dev_err(&hdev->pdev->dev,
7719 			"Send txvlan protocol type command fail, ret =%d\n",
7720 			status);
7721 
7722 	return status;
7723 }
7724 
7725 static int hclge_init_vlan_filter(struct hclge_dev *hdev)
7726 {
7727 	struct hclge_vport *vport;
7728 	bool enable = true;
7729 	int ret;
7730 	int i;
7731 
7732 	if (hdev->ae_dev->dev_version < HNAE3_DEVICE_VERSION_V2)
7733 		return hclge_set_vlan_filter_ctrl(hdev, HCLGE_FILTER_TYPE_VF,
7734 						  HCLGE_FILTER_FE_EGRESS_V1_B,
7735 						  true, 0);
7736 
7737 	/* for revision 0x21, vf vlan filter is per function */
7738 	for (i = 0; i < hdev->num_alloc_vport; i++) {
7739 		vport = &hdev->vport[i];
7740 		ret = hclge_set_vlan_filter_ctrl(hdev, HCLGE_FILTER_TYPE_VF,
7741 						 HCLGE_FILTER_FE_EGRESS, true,
7742 						 vport->vport_id);
7743 		if (ret)
7744 			return ret;
7745 		vport->cur_vlan_fltr_en = true;
7746 	}
7747 
7748 	if (test_bit(HNAE3_DEV_SUPPORT_VLAN_FLTR_MDF_B, hdev->ae_dev->caps) &&
7749 	    !test_bit(HNAE3_DEV_SUPPORT_PORT_VLAN_BYPASS_B, hdev->ae_dev->caps))
7750 		enable = false;
7751 
7752 	return hclge_set_vlan_filter_ctrl(hdev, HCLGE_FILTER_TYPE_PORT,
7753 					  HCLGE_FILTER_FE_INGRESS, enable, 0);
7754 }
7755 
7756 static int hclge_init_vlan_type(struct hclge_dev *hdev)
7757 {
7758 	hdev->vlan_type_cfg.rx_in_fst_vlan_type = ETH_P_8021Q;
7759 	hdev->vlan_type_cfg.rx_in_sec_vlan_type = ETH_P_8021Q;
7760 	hdev->vlan_type_cfg.rx_ot_fst_vlan_type = ETH_P_8021Q;
7761 	hdev->vlan_type_cfg.rx_ot_sec_vlan_type = ETH_P_8021Q;
7762 	hdev->vlan_type_cfg.tx_ot_vlan_type = ETH_P_8021Q;
7763 	hdev->vlan_type_cfg.tx_in_vlan_type = ETH_P_8021Q;
7764 
7765 	return hclge_set_vlan_protocol_type(hdev);
7766 }
7767 
7768 static int hclge_init_vport_vlan_offload(struct hclge_dev *hdev)
7769 {
7770 	struct hclge_port_base_vlan_config *cfg;
7771 	struct hclge_vport *vport;
7772 	int ret;
7773 	int i;
7774 
7775 	for (i = 0; i < hdev->num_alloc_vport; i++) {
7776 		vport = &hdev->vport[i];
7777 		cfg = &vport->port_base_vlan_cfg;
7778 
7779 		ret = hclge_vlan_offload_cfg(vport, cfg->state,
7780 					     cfg->vlan_info.vlan_tag,
7781 					     cfg->vlan_info.qos);
7782 		if (ret)
7783 			return ret;
7784 	}
7785 	return 0;
7786 }
7787 
7788 static int hclge_init_vlan_config(struct hclge_dev *hdev)
7789 {
7790 	struct hnae3_handle *handle = &hdev->vport[0].nic;
7791 	int ret;
7792 
7793 	ret = hclge_init_vlan_filter(hdev);
7794 	if (ret)
7795 		return ret;
7796 
7797 	ret = hclge_init_vlan_type(hdev);
7798 	if (ret)
7799 		return ret;
7800 
7801 	ret = hclge_init_vport_vlan_offload(hdev);
7802 	if (ret)
7803 		return ret;
7804 
7805 	return hclge_set_vlan_filter(handle, htons(ETH_P_8021Q), 0, false);
7806 }
7807 
7808 static void hclge_add_vport_vlan_table(struct hclge_vport *vport, u16 vlan_id,
7809 				       bool writen_to_tbl)
7810 {
7811 	struct hclge_vport_vlan_cfg *vlan, *tmp;
7812 	struct hclge_dev *hdev = vport->back;
7813 
7814 	mutex_lock(&hdev->vport_lock);
7815 
7816 	list_for_each_entry_safe(vlan, tmp, &vport->vlan_list, node) {
7817 		if (vlan->vlan_id == vlan_id) {
7818 			mutex_unlock(&hdev->vport_lock);
7819 			return;
7820 		}
7821 	}
7822 
7823 	vlan = kzalloc_obj(*vlan);
7824 	if (!vlan) {
7825 		mutex_unlock(&hdev->vport_lock);
7826 		return;
7827 	}
7828 
7829 	vlan->hd_tbl_status = writen_to_tbl;
7830 	vlan->vlan_id = vlan_id;
7831 
7832 	list_add_tail(&vlan->node, &vport->vlan_list);
7833 	mutex_unlock(&hdev->vport_lock);
7834 }
7835 
7836 static int hclge_add_vport_all_vlan_table(struct hclge_vport *vport)
7837 {
7838 	struct hclge_vport_vlan_cfg *vlan, *tmp;
7839 	struct hclge_dev *hdev = vport->back;
7840 	int ret;
7841 
7842 	mutex_lock(&hdev->vport_lock);
7843 
7844 	list_for_each_entry_safe(vlan, tmp, &vport->vlan_list, node) {
7845 		if (!vlan->hd_tbl_status) {
7846 			ret = hclge_set_vlan_filter_hw(hdev, htons(ETH_P_8021Q),
7847 						       vport->vport_id,
7848 						       vlan->vlan_id, false);
7849 			if (ret) {
7850 				dev_err(&hdev->pdev->dev,
7851 					"restore vport vlan list failed, ret=%d\n",
7852 					ret);
7853 
7854 				mutex_unlock(&hdev->vport_lock);
7855 				return ret;
7856 			}
7857 		}
7858 		vlan->hd_tbl_status = true;
7859 	}
7860 
7861 	mutex_unlock(&hdev->vport_lock);
7862 
7863 	return 0;
7864 }
7865 
7866 static void hclge_rm_vport_vlan_table(struct hclge_vport *vport, u16 vlan_id,
7867 				      bool is_write_tbl)
7868 {
7869 	struct hclge_vport_vlan_cfg *vlan, *tmp;
7870 	struct hclge_dev *hdev = vport->back;
7871 
7872 	list_for_each_entry_safe(vlan, tmp, &vport->vlan_list, node) {
7873 		if (vlan->vlan_id == vlan_id) {
7874 			if (is_write_tbl && vlan->hd_tbl_status)
7875 				hclge_set_vlan_filter_hw(hdev,
7876 							 htons(ETH_P_8021Q),
7877 							 vport->vport_id,
7878 							 vlan_id,
7879 							 true);
7880 
7881 			list_del(&vlan->node);
7882 			kfree(vlan);
7883 			break;
7884 		}
7885 	}
7886 }
7887 
7888 void hclge_rm_vport_all_vlan_table(struct hclge_vport *vport, bool is_del_list)
7889 {
7890 	struct hclge_vport_vlan_cfg *vlan, *tmp;
7891 	struct hclge_dev *hdev = vport->back;
7892 
7893 	mutex_lock(&hdev->vport_lock);
7894 
7895 	list_for_each_entry_safe(vlan, tmp, &vport->vlan_list, node) {
7896 		if (vlan->hd_tbl_status)
7897 			hclge_set_vlan_filter_hw(hdev,
7898 						 htons(ETH_P_8021Q),
7899 						 vport->vport_id,
7900 						 vlan->vlan_id,
7901 						 true);
7902 
7903 		vlan->hd_tbl_status = false;
7904 		if (is_del_list) {
7905 			list_del(&vlan->node);
7906 			kfree(vlan);
7907 		}
7908 	}
7909 	clear_bit(vport->vport_id, hdev->vf_vlan_full);
7910 	mutex_unlock(&hdev->vport_lock);
7911 }
7912 
7913 void hclge_uninit_vport_vlan_table(struct hclge_dev *hdev)
7914 {
7915 	struct hclge_vport_vlan_cfg *vlan, *tmp;
7916 	struct hclge_vport *vport;
7917 	int i;
7918 
7919 	mutex_lock(&hdev->vport_lock);
7920 
7921 	for (i = 0; i < hdev->num_alloc_vport; i++) {
7922 		vport = &hdev->vport[i];
7923 		list_for_each_entry_safe(vlan, tmp, &vport->vlan_list, node) {
7924 			list_del(&vlan->node);
7925 			kfree(vlan);
7926 		}
7927 	}
7928 
7929 	mutex_unlock(&hdev->vport_lock);
7930 }
7931 
7932 void hclge_restore_vport_port_base_vlan_config(struct hclge_dev *hdev)
7933 {
7934 	struct hclge_vlan_info *vlan_info;
7935 	struct hclge_vport *vport;
7936 	u16 vlan_proto;
7937 	u16 vlan_id;
7938 	u16 state;
7939 	int vf_id;
7940 	int ret;
7941 
7942 	/* PF should restore all vfs port base vlan */
7943 	for (vf_id = 0; vf_id < hdev->num_alloc_vfs; vf_id++) {
7944 		vport = &hdev->vport[vf_id + HCLGE_VF_VPORT_START_NUM];
7945 		vlan_info = vport->port_base_vlan_cfg.tbl_sta ?
7946 			    &vport->port_base_vlan_cfg.vlan_info :
7947 			    &vport->port_base_vlan_cfg.old_vlan_info;
7948 
7949 		vlan_id = vlan_info->vlan_tag;
7950 		vlan_proto = vlan_info->vlan_proto;
7951 		state = vport->port_base_vlan_cfg.state;
7952 
7953 		if (state != HNAE3_PORT_BASE_VLAN_DISABLE) {
7954 			clear_bit(vport->vport_id, hdev->vlan_table[vlan_id]);
7955 			ret = hclge_set_vlan_filter_hw(hdev, htons(vlan_proto),
7956 						       vport->vport_id,
7957 						       vlan_id, false);
7958 			vport->port_base_vlan_cfg.tbl_sta = ret == 0;
7959 		}
7960 	}
7961 }
7962 
7963 void hclge_restore_vport_vlan_table(struct hclge_vport *vport)
7964 {
7965 	struct hclge_vport_vlan_cfg *vlan, *tmp;
7966 	struct hclge_dev *hdev = vport->back;
7967 	int ret;
7968 
7969 	mutex_lock(&hdev->vport_lock);
7970 
7971 	if (vport->port_base_vlan_cfg.state == HNAE3_PORT_BASE_VLAN_DISABLE) {
7972 		list_for_each_entry_safe(vlan, tmp, &vport->vlan_list, node) {
7973 			ret = hclge_set_vlan_filter_hw(hdev, htons(ETH_P_8021Q),
7974 						       vport->vport_id,
7975 						       vlan->vlan_id, false);
7976 			if (ret)
7977 				break;
7978 			vlan->hd_tbl_status = true;
7979 		}
7980 	}
7981 
7982 	mutex_unlock(&hdev->vport_lock);
7983 }
7984 
7985 /* For global reset and imp reset, hardware will clear the mac table,
7986  * so we change the mac address state from ACTIVE to TO_ADD, then they
7987  * can be restored in the service task after reset complete. Furtherly,
7988  * the mac addresses with state TO_DEL or DEL_FAIL are unnecessary to
7989  * be restored after reset, so just remove these mac nodes from mac_list.
7990  */
7991 static void hclge_mac_node_convert_for_reset(struct list_head *list)
7992 {
7993 	struct hclge_mac_node *mac_node, *tmp;
7994 
7995 	list_for_each_entry_safe(mac_node, tmp, list, node) {
7996 		if (mac_node->state == HCLGE_MAC_ACTIVE) {
7997 			mac_node->state = HCLGE_MAC_TO_ADD;
7998 		} else if (mac_node->state == HCLGE_MAC_TO_DEL) {
7999 			list_del(&mac_node->node);
8000 			kfree(mac_node);
8001 		}
8002 	}
8003 }
8004 
8005 void hclge_restore_mac_table_common(struct hclge_vport *vport)
8006 {
8007 	spin_lock_bh(&vport->mac_list_lock);
8008 
8009 	hclge_mac_node_convert_for_reset(&vport->uc_mac_list);
8010 	hclge_mac_node_convert_for_reset(&vport->mc_mac_list);
8011 	set_bit(HCLGE_VPORT_STATE_MAC_TBL_CHANGE, &vport->state);
8012 
8013 	spin_unlock_bh(&vport->mac_list_lock);
8014 }
8015 
8016 static void hclge_restore_hw_table(struct hclge_dev *hdev)
8017 {
8018 	struct hclge_vport *vport = &hdev->vport[0];
8019 	struct hnae3_handle *handle = &vport->nic;
8020 
8021 	hclge_restore_mac_table_common(vport);
8022 	hclge_restore_vport_port_base_vlan_config(hdev);
8023 	hclge_restore_vport_vlan_table(vport);
8024 	set_bit(HCLGE_STATE_FD_USER_DEF_CHANGED, &hdev->state);
8025 	hclge_restore_fd_entries(handle);
8026 }
8027 
8028 int hclge_en_hw_strip_rxvtag(struct hnae3_handle *handle, bool enable)
8029 {
8030 	struct hclge_vport *vport = hclge_get_vport(handle);
8031 
8032 	if (vport->port_base_vlan_cfg.state == HNAE3_PORT_BASE_VLAN_DISABLE) {
8033 		vport->rxvlan_cfg.strip_tag1_en = false;
8034 		vport->rxvlan_cfg.strip_tag2_en = enable;
8035 		vport->rxvlan_cfg.strip_tag2_discard_en = false;
8036 	} else {
8037 		vport->rxvlan_cfg.strip_tag1_en = enable;
8038 		vport->rxvlan_cfg.strip_tag2_en = true;
8039 		vport->rxvlan_cfg.strip_tag2_discard_en = true;
8040 	}
8041 
8042 	vport->rxvlan_cfg.strip_tag1_discard_en = false;
8043 	vport->rxvlan_cfg.vlan1_vlan_prionly = false;
8044 	vport->rxvlan_cfg.vlan2_vlan_prionly = false;
8045 	vport->rxvlan_cfg.rx_vlan_offload_en = enable;
8046 
8047 	return hclge_set_vlan_rx_offload_cfg(vport);
8048 }
8049 
8050 static void hclge_set_vport_vlan_fltr_change(struct hclge_vport *vport)
8051 {
8052 	struct hclge_dev *hdev = vport->back;
8053 
8054 	if (test_bit(HNAE3_DEV_SUPPORT_VLAN_FLTR_MDF_B, hdev->ae_dev->caps))
8055 		set_bit(HCLGE_VPORT_STATE_VLAN_FLTR_CHANGE, &vport->state);
8056 }
8057 
8058 static int hclge_update_vlan_filter_entries(struct hclge_vport *vport,
8059 					    u16 port_base_vlan_state,
8060 					    struct hclge_vlan_info *new_info,
8061 					    struct hclge_vlan_info *old_info)
8062 {
8063 	struct hclge_dev *hdev = vport->back;
8064 	int ret;
8065 
8066 	if (port_base_vlan_state == HNAE3_PORT_BASE_VLAN_ENABLE) {
8067 		hclge_rm_vport_all_vlan_table(vport, false);
8068 		/* force clear VLAN 0 */
8069 		ret = hclge_set_vf_vlan_common(hdev, vport->vport_id, true, 0);
8070 		if (ret)
8071 			return ret;
8072 		return hclge_set_vlan_filter_hw(hdev,
8073 						 htons(new_info->vlan_proto),
8074 						 vport->vport_id,
8075 						 new_info->vlan_tag,
8076 						 false);
8077 	}
8078 
8079 	vport->port_base_vlan_cfg.tbl_sta = false;
8080 
8081 	/* force add VLAN 0 */
8082 	ret = hclge_set_vf_vlan_common(hdev, vport->vport_id, false, 0);
8083 	if (ret)
8084 		return ret;
8085 
8086 	ret = hclge_set_vlan_filter_hw(hdev, htons(old_info->vlan_proto),
8087 				       vport->vport_id, old_info->vlan_tag,
8088 				       true);
8089 	if (ret)
8090 		return ret;
8091 
8092 	return hclge_add_vport_all_vlan_table(vport);
8093 }
8094 
8095 static bool hclge_need_update_vlan_filter(const struct hclge_vlan_info *new_cfg,
8096 					  const struct hclge_vlan_info *old_cfg)
8097 {
8098 	if (new_cfg->vlan_tag != old_cfg->vlan_tag)
8099 		return true;
8100 
8101 	if (new_cfg->vlan_tag == 0 && (new_cfg->qos == 0 || old_cfg->qos == 0))
8102 		return true;
8103 
8104 	return false;
8105 }
8106 
8107 static int hclge_modify_port_base_vlan_tag(struct hclge_vport *vport,
8108 					   struct hclge_vlan_info *new_info,
8109 					   struct hclge_vlan_info *old_info)
8110 {
8111 	struct hclge_dev *hdev = vport->back;
8112 	int ret;
8113 
8114 	/* add new VLAN tag */
8115 	ret = hclge_set_vlan_filter_hw(hdev, htons(new_info->vlan_proto),
8116 				       vport->vport_id, new_info->vlan_tag,
8117 				       false);
8118 	if (ret)
8119 		return ret;
8120 
8121 	vport->port_base_vlan_cfg.tbl_sta = false;
8122 	/* remove old VLAN tag */
8123 	if (old_info->vlan_tag == 0)
8124 		ret = hclge_set_vf_vlan_common(hdev, vport->vport_id,
8125 					       true, 0);
8126 	else
8127 		ret = hclge_set_vlan_filter_hw(hdev, htons(ETH_P_8021Q),
8128 					       vport->vport_id,
8129 					       old_info->vlan_tag, true);
8130 	if (ret)
8131 		dev_err(&hdev->pdev->dev,
8132 			"failed to clear vport%u port base vlan %u, ret = %d.\n",
8133 			vport->vport_id, old_info->vlan_tag, ret);
8134 
8135 	return ret;
8136 }
8137 
8138 int hclge_update_port_base_vlan_cfg(struct hclge_vport *vport, u16 state,
8139 				    struct hclge_vlan_info *vlan_info)
8140 {
8141 	struct hnae3_handle *nic = &vport->nic;
8142 	struct hclge_vlan_info *old_vlan_info;
8143 	int ret;
8144 
8145 	old_vlan_info = &vport->port_base_vlan_cfg.vlan_info;
8146 
8147 	ret = hclge_vlan_offload_cfg(vport, state, vlan_info->vlan_tag,
8148 				     vlan_info->qos);
8149 	if (ret)
8150 		return ret;
8151 
8152 	if (!hclge_need_update_vlan_filter(vlan_info, old_vlan_info))
8153 		goto out;
8154 
8155 	if (state == HNAE3_PORT_BASE_VLAN_MODIFY)
8156 		ret = hclge_modify_port_base_vlan_tag(vport, vlan_info,
8157 						      old_vlan_info);
8158 	else
8159 		ret = hclge_update_vlan_filter_entries(vport, state, vlan_info,
8160 						       old_vlan_info);
8161 	if (ret)
8162 		return ret;
8163 
8164 out:
8165 	vport->port_base_vlan_cfg.state = state;
8166 	if (state == HNAE3_PORT_BASE_VLAN_DISABLE)
8167 		nic->port_base_vlan_state = HNAE3_PORT_BASE_VLAN_DISABLE;
8168 	else
8169 		nic->port_base_vlan_state = HNAE3_PORT_BASE_VLAN_ENABLE;
8170 
8171 	vport->port_base_vlan_cfg.old_vlan_info = *old_vlan_info;
8172 	vport->port_base_vlan_cfg.vlan_info = *vlan_info;
8173 	vport->port_base_vlan_cfg.tbl_sta = true;
8174 	hclge_set_vport_vlan_fltr_change(vport);
8175 
8176 	return 0;
8177 }
8178 
8179 static u16 hclge_get_port_base_vlan_state(struct hclge_vport *vport,
8180 					  enum hnae3_port_base_vlan_state state,
8181 					  u16 vlan, u8 qos)
8182 {
8183 	if (state == HNAE3_PORT_BASE_VLAN_DISABLE) {
8184 		if (!vlan && !qos)
8185 			return HNAE3_PORT_BASE_VLAN_NOCHANGE;
8186 
8187 		return HNAE3_PORT_BASE_VLAN_ENABLE;
8188 	}
8189 
8190 	if (!vlan && !qos)
8191 		return HNAE3_PORT_BASE_VLAN_DISABLE;
8192 
8193 	if (vport->port_base_vlan_cfg.vlan_info.vlan_tag == vlan &&
8194 	    vport->port_base_vlan_cfg.vlan_info.qos == qos)
8195 		return HNAE3_PORT_BASE_VLAN_NOCHANGE;
8196 
8197 	return HNAE3_PORT_BASE_VLAN_MODIFY;
8198 }
8199 
8200 static int hclge_set_vf_vlan_filter(struct hnae3_handle *handle, int vfid,
8201 				    u16 vlan, u8 qos, __be16 proto)
8202 {
8203 	struct hnae3_ae_dev *ae_dev = pci_get_drvdata(handle->pdev);
8204 	struct hclge_vport *vport = hclge_get_vport(handle);
8205 	struct hclge_dev *hdev = vport->back;
8206 	struct hclge_vlan_info vlan_info;
8207 	u16 state;
8208 	int ret;
8209 
8210 	if (hdev->ae_dev->dev_version < HNAE3_DEVICE_VERSION_V2)
8211 		return -EOPNOTSUPP;
8212 
8213 	vport = hclge_get_vf_vport(hdev, vfid);
8214 	if (!vport)
8215 		return -EINVAL;
8216 
8217 	/* qos is a 3 bits value, so can not be bigger than 7 */
8218 	if (vlan > VLAN_N_VID - 1 || qos > 7)
8219 		return -EINVAL;
8220 	if (proto != htons(ETH_P_8021Q))
8221 		return -EPROTONOSUPPORT;
8222 
8223 	state = hclge_get_port_base_vlan_state(vport,
8224 					       vport->port_base_vlan_cfg.state,
8225 					       vlan, qos);
8226 	if (state == HNAE3_PORT_BASE_VLAN_NOCHANGE)
8227 		return 0;
8228 
8229 	vlan_info.vlan_tag = vlan;
8230 	vlan_info.qos = qos;
8231 	vlan_info.vlan_proto = ntohs(proto);
8232 
8233 	ret = hclge_update_port_base_vlan_cfg(vport, state, &vlan_info);
8234 	if (ret) {
8235 		dev_err(&hdev->pdev->dev,
8236 			"failed to update port base vlan for vf %d, ret = %d\n",
8237 			vfid, ret);
8238 		return ret;
8239 	}
8240 
8241 	/* there is a timewindow for PF to know VF unalive, it may
8242 	 * cause send mailbox fail, but it doesn't matter, VF will
8243 	 * query it when reinit.
8244 	 * for DEVICE_VERSION_V3, vf doesn't need to know about the port based
8245 	 * VLAN state.
8246 	 */
8247 	if (ae_dev->dev_version < HNAE3_DEVICE_VERSION_V3) {
8248 		if (test_bit(HCLGE_VPORT_STATE_ALIVE, &vport->state))
8249 			(void)hclge_push_vf_port_base_vlan_info(&hdev->vport[0],
8250 								vport->vport_id,
8251 								state,
8252 								&vlan_info);
8253 		else
8254 			set_bit(HCLGE_VPORT_NEED_NOTIFY_VF_VLAN,
8255 				&vport->need_notify);
8256 	}
8257 	return 0;
8258 }
8259 
8260 static void hclge_clear_vf_vlan(struct hclge_dev *hdev)
8261 {
8262 	struct hclge_vlan_info *vlan_info;
8263 	struct hclge_vport *vport;
8264 	int ret;
8265 	int vf;
8266 
8267 	/* clear port base vlan for all vf */
8268 	for (vf = HCLGE_VF_VPORT_START_NUM; vf < hdev->num_alloc_vport; vf++) {
8269 		vport = &hdev->vport[vf];
8270 		vlan_info = &vport->port_base_vlan_cfg.vlan_info;
8271 
8272 		ret = hclge_set_vlan_filter_hw(hdev, htons(ETH_P_8021Q),
8273 					       vport->vport_id,
8274 					       vlan_info->vlan_tag, true);
8275 		if (ret)
8276 			dev_err(&hdev->pdev->dev,
8277 				"failed to clear vf vlan for vf%d, ret = %d\n",
8278 				vf - HCLGE_VF_VPORT_START_NUM, ret);
8279 	}
8280 }
8281 
8282 int hclge_set_vlan_filter(struct hnae3_handle *handle, __be16 proto,
8283 			  u16 vlan_id, bool is_kill)
8284 {
8285 	struct hclge_vport *vport = hclge_get_vport(handle);
8286 	struct hclge_dev *hdev = vport->back;
8287 	bool writen_to_tbl = false;
8288 	int ret = 0;
8289 
8290 	if (vlan_id >= VLAN_N_VID)
8291 		return -EINVAL;
8292 
8293 	/* When device is resetting or reset failed, firmware is unable to
8294 	 * handle mailbox. Just record the vlan id, and remove it after
8295 	 * reset finished.
8296 	 */
8297 	mutex_lock(&hdev->vport_lock);
8298 	if ((test_bit(HCLGE_STATE_RST_HANDLING, &hdev->state) ||
8299 	     test_bit(HCLGE_STATE_RST_FAIL, &hdev->state)) && is_kill) {
8300 		set_bit(vlan_id, vport->vlan_del_fail_bmap);
8301 		mutex_unlock(&hdev->vport_lock);
8302 		return -EBUSY;
8303 	} else if (!is_kill && test_bit(vlan_id, vport->vlan_del_fail_bmap)) {
8304 		clear_bit(vlan_id, vport->vlan_del_fail_bmap);
8305 	}
8306 	mutex_unlock(&hdev->vport_lock);
8307 
8308 	/* when port base vlan enabled, we use port base vlan as the vlan
8309 	 * filter entry. In this case, we don't update vlan filter table
8310 	 * when user add new vlan or remove exist vlan, just update the vport
8311 	 * vlan list. The vlan id in vlan list will be writen in vlan filter
8312 	 * table until port base vlan disabled
8313 	 */
8314 	if (handle->port_base_vlan_state == HNAE3_PORT_BASE_VLAN_DISABLE) {
8315 		ret = hclge_set_vlan_filter_hw(hdev, proto, vport->vport_id,
8316 					       vlan_id, is_kill);
8317 		writen_to_tbl = true;
8318 	}
8319 
8320 	if (!ret) {
8321 		if (!is_kill) {
8322 			hclge_add_vport_vlan_table(vport, vlan_id,
8323 						   writen_to_tbl);
8324 		} else if (is_kill && vlan_id != 0) {
8325 			mutex_lock(&hdev->vport_lock);
8326 			hclge_rm_vport_vlan_table(vport, vlan_id, false);
8327 			mutex_unlock(&hdev->vport_lock);
8328 		}
8329 	} else if (is_kill) {
8330 		/* when remove hw vlan filter failed, record the vlan id,
8331 		 * and try to remove it from hw later, to be consistence
8332 		 * with stack
8333 		 */
8334 		mutex_lock(&hdev->vport_lock);
8335 		set_bit(vlan_id, vport->vlan_del_fail_bmap);
8336 		mutex_unlock(&hdev->vport_lock);
8337 	}
8338 
8339 	hclge_set_vport_vlan_fltr_change(vport);
8340 
8341 	return ret;
8342 }
8343 
8344 static void hclge_sync_vlan_fltr_state(struct hclge_dev *hdev)
8345 {
8346 	struct hclge_vport *vport;
8347 	int ret;
8348 	u16 i;
8349 
8350 	for (i = 0; i < hdev->num_alloc_vport; i++) {
8351 		vport = &hdev->vport[i];
8352 		if (!test_and_clear_bit(HCLGE_VPORT_STATE_VLAN_FLTR_CHANGE,
8353 					&vport->state))
8354 			continue;
8355 
8356 		mutex_lock(&hdev->vport_lock);
8357 		ret = __hclge_enable_vport_vlan_filter(vport,
8358 						       vport->req_vlan_fltr_en);
8359 		if (ret) {
8360 			dev_err(&hdev->pdev->dev,
8361 				"failed to sync vlan filter state for vport%u, ret = %d\n",
8362 				vport->vport_id, ret);
8363 			set_bit(HCLGE_VPORT_STATE_VLAN_FLTR_CHANGE,
8364 				&vport->state);
8365 			mutex_unlock(&hdev->vport_lock);
8366 			return;
8367 		}
8368 		mutex_unlock(&hdev->vport_lock);
8369 	}
8370 }
8371 
8372 static void hclge_sync_vlan_filter(struct hclge_dev *hdev)
8373 {
8374 #define HCLGE_MAX_SYNC_COUNT	60
8375 
8376 	int i, ret, sync_cnt = 0;
8377 	u16 vlan_id;
8378 
8379 	mutex_lock(&hdev->vport_lock);
8380 	/* start from vport 1 for PF is always alive */
8381 	for (i = 0; i < hdev->num_alloc_vport; i++) {
8382 		struct hclge_vport *vport = &hdev->vport[i];
8383 
8384 		vlan_id = find_first_bit(vport->vlan_del_fail_bmap,
8385 					 VLAN_N_VID);
8386 		while (vlan_id != VLAN_N_VID) {
8387 			ret = hclge_set_vlan_filter_hw(hdev, htons(ETH_P_8021Q),
8388 						       vport->vport_id, vlan_id,
8389 						       true);
8390 			if (ret && ret != -EINVAL) {
8391 				mutex_unlock(&hdev->vport_lock);
8392 				return;
8393 			}
8394 
8395 			clear_bit(vlan_id, vport->vlan_del_fail_bmap);
8396 			hclge_rm_vport_vlan_table(vport, vlan_id, false);
8397 			hclge_set_vport_vlan_fltr_change(vport);
8398 
8399 			sync_cnt++;
8400 			if (sync_cnt >= HCLGE_MAX_SYNC_COUNT) {
8401 				mutex_unlock(&hdev->vport_lock);
8402 				return;
8403 			}
8404 
8405 			vlan_id = find_first_bit(vport->vlan_del_fail_bmap,
8406 						 VLAN_N_VID);
8407 		}
8408 	}
8409 	mutex_unlock(&hdev->vport_lock);
8410 
8411 	hclge_sync_vlan_fltr_state(hdev);
8412 }
8413 
8414 static int hclge_set_mac_mtu(struct hclge_dev *hdev, int new_mps)
8415 {
8416 	struct hclge_config_max_frm_size_cmd *req;
8417 	struct hclge_desc desc;
8418 
8419 	hclge_cmd_setup_basic_desc(&desc, HCLGE_OPC_CONFIG_MAX_FRM_SIZE, false);
8420 
8421 	req = (struct hclge_config_max_frm_size_cmd *)desc.data;
8422 	req->max_frm_size = cpu_to_le16(new_mps);
8423 	req->min_frm_size = HCLGE_MAC_MIN_FRAME;
8424 
8425 	return hclge_cmd_send(&hdev->hw, &desc, 1);
8426 }
8427 
8428 static int hclge_set_mtu(struct hnae3_handle *handle, int new_mtu)
8429 {
8430 	struct hclge_vport *vport = hclge_get_vport(handle);
8431 
8432 	return hclge_set_vport_mtu(vport, new_mtu);
8433 }
8434 
8435 int hclge_set_vport_mtu(struct hclge_vport *vport, int new_mtu)
8436 {
8437 	struct hclge_dev *hdev = vport->back;
8438 	int i, max_frm_size, ret;
8439 
8440 	/* HW supprt 2 layer vlan */
8441 	max_frm_size = new_mtu + ETH_HLEN + ETH_FCS_LEN + 2 * VLAN_HLEN;
8442 	if (max_frm_size < HCLGE_MAC_MIN_FRAME ||
8443 	    max_frm_size > hdev->ae_dev->dev_specs.max_frm_size)
8444 		return -EINVAL;
8445 
8446 	max_frm_size = max(max_frm_size, HCLGE_MAC_DEFAULT_FRAME);
8447 	mutex_lock(&hdev->vport_lock);
8448 	/* VF's mps must fit within hdev->mps */
8449 	if (vport->vport_id && (u32)max_frm_size > hdev->mps) {
8450 		mutex_unlock(&hdev->vport_lock);
8451 		return -EINVAL;
8452 	} else if (vport->vport_id) {
8453 		vport->mps = max_frm_size;
8454 		mutex_unlock(&hdev->vport_lock);
8455 		return 0;
8456 	}
8457 
8458 	/* PF's mps must be greater then VF's mps */
8459 	for (i = 1; i < hdev->num_alloc_vport; i++)
8460 		if ((u32)max_frm_size < hdev->vport[i].mps) {
8461 			dev_err(&hdev->pdev->dev,
8462 				"failed to set pf mtu for less than vport %d, mps = %u.\n",
8463 				i, hdev->vport[i].mps);
8464 			mutex_unlock(&hdev->vport_lock);
8465 			return -EINVAL;
8466 		}
8467 
8468 	hclge_notify_client(hdev, HNAE3_DOWN_CLIENT);
8469 
8470 	ret = hclge_set_mac_mtu(hdev, max_frm_size);
8471 	if (ret) {
8472 		dev_err(&hdev->pdev->dev,
8473 			"Change mtu fail, ret =%d\n", ret);
8474 		goto out;
8475 	}
8476 
8477 	hdev->mps = max_frm_size;
8478 	vport->mps = max_frm_size;
8479 
8480 	ret = hclge_buffer_alloc(hdev);
8481 	if (ret)
8482 		dev_err(&hdev->pdev->dev,
8483 			"Allocate buffer fail, ret =%d\n", ret);
8484 
8485 out:
8486 	hclge_notify_client(hdev, HNAE3_UP_CLIENT);
8487 	mutex_unlock(&hdev->vport_lock);
8488 	return ret;
8489 }
8490 
8491 static int hclge_reset_tqp_cmd_send(struct hclge_dev *hdev, u16 queue_id,
8492 				    bool enable)
8493 {
8494 	struct hclge_reset_tqp_queue_cmd *req;
8495 	struct hclge_desc desc;
8496 	int ret;
8497 
8498 	hclge_cmd_setup_basic_desc(&desc, HCLGE_OPC_RESET_TQP_QUEUE, false);
8499 
8500 	req = (struct hclge_reset_tqp_queue_cmd *)desc.data;
8501 	req->tqp_id = cpu_to_le16(queue_id);
8502 	if (enable)
8503 		hnae3_set_bit(req->reset_req, HCLGE_TQP_RESET_B, 1U);
8504 
8505 	ret = hclge_cmd_send(&hdev->hw, &desc, 1);
8506 	if (ret) {
8507 		dev_err(&hdev->pdev->dev,
8508 			"Send tqp reset cmd error, status =%d\n", ret);
8509 		return ret;
8510 	}
8511 
8512 	return 0;
8513 }
8514 
8515 static int hclge_get_reset_status(struct hclge_dev *hdev, u16 queue_id,
8516 				  u8 *reset_status)
8517 {
8518 	struct hclge_reset_tqp_queue_cmd *req;
8519 	struct hclge_desc desc;
8520 	int ret;
8521 
8522 	hclge_cmd_setup_basic_desc(&desc, HCLGE_OPC_RESET_TQP_QUEUE, true);
8523 
8524 	req = (struct hclge_reset_tqp_queue_cmd *)desc.data;
8525 	req->tqp_id = cpu_to_le16(queue_id);
8526 
8527 	ret = hclge_cmd_send(&hdev->hw, &desc, 1);
8528 	if (ret) {
8529 		dev_err(&hdev->pdev->dev,
8530 			"Get reset status error, status =%d\n", ret);
8531 		return ret;
8532 	}
8533 
8534 	*reset_status = hnae3_get_bit(req->ready_to_reset, HCLGE_TQP_RESET_B);
8535 
8536 	return 0;
8537 }
8538 
8539 u16 hclge_covert_handle_qid_global(struct hnae3_handle *handle, u16 queue_id)
8540 {
8541 	struct hclge_comm_tqp *tqp;
8542 	struct hnae3_queue *queue;
8543 
8544 	queue = handle->kinfo.tqp[queue_id];
8545 	tqp = container_of(queue, struct hclge_comm_tqp, q);
8546 
8547 	return tqp->index;
8548 }
8549 
8550 static int hclge_reset_tqp_cmd(struct hnae3_handle *handle)
8551 {
8552 	struct hclge_vport *vport = hclge_get_vport(handle);
8553 	struct hclge_dev *hdev = vport->back;
8554 	u16 reset_try_times = 0;
8555 	u8 reset_status;
8556 	u16 queue_gid;
8557 	int ret;
8558 	u16 i;
8559 
8560 	for (i = 0; i < handle->kinfo.num_tqps; i++) {
8561 		queue_gid = hclge_covert_handle_qid_global(handle, i);
8562 		ret = hclge_reset_tqp_cmd_send(hdev, queue_gid, true);
8563 		if (ret) {
8564 			dev_err(&hdev->pdev->dev,
8565 				"failed to send reset tqp cmd, ret = %d\n",
8566 				ret);
8567 			return ret;
8568 		}
8569 
8570 		while (reset_try_times++ < HCLGE_TQP_RESET_TRY_TIMES) {
8571 			ret = hclge_get_reset_status(hdev, queue_gid,
8572 						     &reset_status);
8573 			if (ret)
8574 				return ret;
8575 
8576 			if (reset_status)
8577 				break;
8578 
8579 			/* Wait for tqp hw reset */
8580 			usleep_range(1000, 1200);
8581 		}
8582 
8583 		if (reset_try_times >= HCLGE_TQP_RESET_TRY_TIMES) {
8584 			dev_err(&hdev->pdev->dev,
8585 				"wait for tqp hw reset timeout\n");
8586 			return -ETIME;
8587 		}
8588 
8589 		ret = hclge_reset_tqp_cmd_send(hdev, queue_gid, false);
8590 		if (ret) {
8591 			dev_err(&hdev->pdev->dev,
8592 				"failed to deassert soft reset, ret = %d\n",
8593 				ret);
8594 			return ret;
8595 		}
8596 		reset_try_times = 0;
8597 	}
8598 	return 0;
8599 }
8600 
8601 static int hclge_reset_rcb(struct hnae3_handle *handle)
8602 {
8603 #define HCLGE_RESET_RCB_NOT_SUPPORT	0U
8604 #define HCLGE_RESET_RCB_SUCCESS		1U
8605 
8606 	struct hclge_vport *vport = hclge_get_vport(handle);
8607 	struct hclge_dev *hdev = vport->back;
8608 	struct hclge_reset_cmd *req;
8609 	struct hclge_desc desc;
8610 	u8 return_status;
8611 	u16 queue_gid;
8612 	int ret;
8613 
8614 	queue_gid = hclge_covert_handle_qid_global(handle, 0);
8615 
8616 	req = (struct hclge_reset_cmd *)desc.data;
8617 	hclge_cmd_setup_basic_desc(&desc, HCLGE_OPC_CFG_RST_TRIGGER, false);
8618 	hnae3_set_bit(req->fun_reset_rcb, HCLGE_CFG_RESET_RCB_B, 1);
8619 	req->fun_reset_rcb_vqid_start = cpu_to_le16(queue_gid);
8620 	req->fun_reset_rcb_vqid_num = cpu_to_le16(handle->kinfo.num_tqps);
8621 
8622 	ret = hclge_cmd_send(&hdev->hw, &desc, 1);
8623 	if (ret) {
8624 		dev_err(&hdev->pdev->dev,
8625 			"failed to send rcb reset cmd, ret = %d\n", ret);
8626 		return ret;
8627 	}
8628 
8629 	return_status = req->fun_reset_rcb_return_status;
8630 	if (return_status == HCLGE_RESET_RCB_SUCCESS)
8631 		return 0;
8632 
8633 	if (return_status != HCLGE_RESET_RCB_NOT_SUPPORT) {
8634 		dev_err(&hdev->pdev->dev, "failed to reset rcb, ret = %u\n",
8635 			return_status);
8636 		return -EIO;
8637 	}
8638 
8639 	/* if reset rcb cmd is unsupported, we need to send reset tqp cmd
8640 	 * again to reset all tqps
8641 	 */
8642 	return hclge_reset_tqp_cmd(handle);
8643 }
8644 
8645 int hclge_reset_tqp(struct hnae3_handle *handle)
8646 {
8647 	struct hclge_vport *vport = hclge_get_vport(handle);
8648 	struct hclge_dev *hdev = vport->back;
8649 	int ret;
8650 
8651 	/* only need to disable PF's tqp */
8652 	if (!vport->vport_id) {
8653 		ret = hclge_tqp_enable(handle, false);
8654 		if (ret) {
8655 			dev_err(&hdev->pdev->dev,
8656 				"failed to disable tqp, ret = %d\n", ret);
8657 			return ret;
8658 		}
8659 	}
8660 
8661 	return hclge_reset_rcb(handle);
8662 }
8663 
8664 static u32 hclge_get_fw_version(struct hnae3_handle *handle)
8665 {
8666 	struct hclge_vport *vport = hclge_get_vport(handle);
8667 	struct hclge_dev *hdev = vport->back;
8668 
8669 	return hdev->fw_version;
8670 }
8671 
8672 int hclge_query_scc_version(struct hclge_dev *hdev, u32 *scc_version)
8673 {
8674 	struct hclge_comm_query_scc_cmd *resp;
8675 	struct hclge_desc desc;
8676 	int ret;
8677 
8678 	hclge_cmd_setup_basic_desc(&desc, HCLGE_OPC_QUERY_SCC_VER, 1);
8679 	resp = (struct hclge_comm_query_scc_cmd *)desc.data;
8680 
8681 	ret = hclge_cmd_send(&hdev->hw, &desc, 1);
8682 	if (ret)
8683 		return ret;
8684 
8685 	*scc_version = le32_to_cpu(resp->scc_version);
8686 
8687 	return 0;
8688 }
8689 
8690 static void hclge_set_flowctrl_adv(struct hclge_dev *hdev, u32 rx_en, u32 tx_en)
8691 {
8692 	struct phy_device *phydev = hdev->hw.mac.phydev;
8693 
8694 	if (!phydev)
8695 		return;
8696 
8697 	phy_set_asym_pause(phydev, rx_en, tx_en);
8698 }
8699 
8700 static int hclge_cfg_pauseparam(struct hclge_dev *hdev, u32 rx_en, u32 tx_en)
8701 {
8702 	int ret;
8703 
8704 	if (hdev->tm_info.fc_mode == HCLGE_FC_PFC)
8705 		return 0;
8706 
8707 	ret = hclge_mac_pause_en_cfg(hdev, tx_en, rx_en);
8708 	if (ret)
8709 		dev_err(&hdev->pdev->dev,
8710 			"configure pauseparam error, ret = %d.\n", ret);
8711 
8712 	return ret;
8713 }
8714 
8715 int hclge_cfg_flowctrl(struct hclge_dev *hdev)
8716 {
8717 	struct phy_device *phydev = hdev->hw.mac.phydev;
8718 	u16 remote_advertising = 0;
8719 	u16 local_advertising;
8720 	u32 rx_pause, tx_pause;
8721 	u8 flowctl;
8722 
8723 	if (!phydev->link)
8724 		return 0;
8725 
8726 	if (!phydev->autoneg)
8727 		return hclge_mac_pause_setup_hw(hdev);
8728 
8729 	local_advertising = linkmode_adv_to_lcl_adv_t(phydev->advertising);
8730 
8731 	if (phydev->pause)
8732 		remote_advertising = LPA_PAUSE_CAP;
8733 
8734 	if (phydev->asym_pause)
8735 		remote_advertising |= LPA_PAUSE_ASYM;
8736 
8737 	flowctl = mii_resolve_flowctrl_fdx(local_advertising,
8738 					   remote_advertising);
8739 	tx_pause = flowctl & FLOW_CTRL_TX;
8740 	rx_pause = flowctl & FLOW_CTRL_RX;
8741 
8742 	if (phydev->duplex == HCLGE_MAC_HALF) {
8743 		tx_pause = 0;
8744 		rx_pause = 0;
8745 	}
8746 
8747 	return hclge_cfg_pauseparam(hdev, rx_pause, tx_pause);
8748 }
8749 
8750 static void hclge_get_pauseparam(struct hnae3_handle *handle, u32 *auto_neg,
8751 				 u32 *rx_en, u32 *tx_en)
8752 {
8753 	struct hclge_vport *vport = hclge_get_vport(handle);
8754 	struct hclge_dev *hdev = vport->back;
8755 	u8 media_type = hdev->hw.mac.media_type;
8756 
8757 	*auto_neg = (media_type == HNAE3_MEDIA_TYPE_COPPER) ?
8758 		    hclge_get_autoneg(handle) : 0;
8759 
8760 	if (hdev->tm_info.fc_mode == HCLGE_FC_PFC) {
8761 		*rx_en = 0;
8762 		*tx_en = 0;
8763 		return;
8764 	}
8765 
8766 	if (hdev->tm_info.fc_mode == HCLGE_FC_RX_PAUSE) {
8767 		*rx_en = 1;
8768 		*tx_en = 0;
8769 	} else if (hdev->tm_info.fc_mode == HCLGE_FC_TX_PAUSE) {
8770 		*tx_en = 1;
8771 		*rx_en = 0;
8772 	} else if (hdev->tm_info.fc_mode == HCLGE_FC_FULL) {
8773 		*rx_en = 1;
8774 		*tx_en = 1;
8775 	} else {
8776 		*rx_en = 0;
8777 		*tx_en = 0;
8778 	}
8779 }
8780 
8781 static void hclge_record_user_pauseparam(struct hclge_dev *hdev,
8782 					 u32 rx_en, u32 tx_en)
8783 {
8784 	if (rx_en && tx_en)
8785 		hdev->fc_mode_last_time = HCLGE_FC_FULL;
8786 	else if (rx_en && !tx_en)
8787 		hdev->fc_mode_last_time = HCLGE_FC_RX_PAUSE;
8788 	else if (!rx_en && tx_en)
8789 		hdev->fc_mode_last_time = HCLGE_FC_TX_PAUSE;
8790 	else
8791 		hdev->fc_mode_last_time = HCLGE_FC_NONE;
8792 
8793 	hdev->tm_info.fc_mode = hdev->fc_mode_last_time;
8794 }
8795 
8796 static int hclge_set_pauseparam(struct hnae3_handle *handle, u32 auto_neg,
8797 				u32 rx_en, u32 tx_en)
8798 {
8799 	struct hclge_vport *vport = hclge_get_vport(handle);
8800 	struct hclge_dev *hdev = vport->back;
8801 	struct phy_device *phydev = hdev->hw.mac.phydev;
8802 	u32 fc_autoneg;
8803 
8804 	if (phydev || hnae3_dev_phy_imp_supported(hdev)) {
8805 		fc_autoneg = hclge_get_autoneg(handle);
8806 		if (auto_neg != fc_autoneg) {
8807 			dev_info(&hdev->pdev->dev,
8808 				 "To change autoneg please use: ethtool -s <dev> autoneg <on|off>\n");
8809 			return -EOPNOTSUPP;
8810 		}
8811 	}
8812 
8813 	if (hdev->tm_info.fc_mode == HCLGE_FC_PFC) {
8814 		dev_info(&hdev->pdev->dev,
8815 			 "Priority flow control enabled. Cannot set link flow control.\n");
8816 		return -EOPNOTSUPP;
8817 	}
8818 
8819 	hclge_set_flowctrl_adv(hdev, rx_en, tx_en);
8820 
8821 	hclge_record_user_pauseparam(hdev, rx_en, tx_en);
8822 
8823 	if (!auto_neg || hnae3_dev_phy_imp_supported(hdev))
8824 		return hclge_cfg_pauseparam(hdev, rx_en, tx_en);
8825 
8826 	if (phydev)
8827 		return phy_start_aneg(phydev);
8828 
8829 	return -EOPNOTSUPP;
8830 }
8831 
8832 static void hclge_get_ksettings_an_result(struct hnae3_handle *handle,
8833 					  u8 *auto_neg, u32 *speed, u8 *duplex, u32 *lane_num)
8834 {
8835 	struct hclge_vport *vport = hclge_get_vport(handle);
8836 	struct hclge_dev *hdev = vport->back;
8837 
8838 	if (speed)
8839 		*speed = hdev->hw.mac.speed;
8840 	if (duplex)
8841 		*duplex = hdev->hw.mac.duplex;
8842 	if (auto_neg)
8843 		*auto_neg = hdev->hw.mac.autoneg;
8844 	if (lane_num)
8845 		*lane_num = hdev->hw.mac.lane_num;
8846 }
8847 
8848 static void hclge_get_media_type(struct hnae3_handle *handle, u8 *media_type,
8849 				 u8 *module_type)
8850 {
8851 	struct hclge_vport *vport = hclge_get_vport(handle);
8852 	struct hclge_dev *hdev = vport->back;
8853 
8854 	/* When nic is down, the service task is not running, doesn't update
8855 	 * the port information per second. Query the port information before
8856 	 * return the media type, ensure getting the correct media information.
8857 	 */
8858 	hclge_update_port_info(hdev);
8859 
8860 	if (media_type)
8861 		*media_type = hdev->hw.mac.media_type;
8862 
8863 	if (module_type)
8864 		*module_type = hdev->hw.mac.module_type;
8865 }
8866 
8867 static void hclge_get_mdix_mode(struct hnae3_handle *handle,
8868 				u8 *tp_mdix_ctrl, u8 *tp_mdix)
8869 {
8870 	struct hclge_vport *vport = hclge_get_vport(handle);
8871 	struct hclge_dev *hdev = vport->back;
8872 	struct phy_device *phydev = hdev->hw.mac.phydev;
8873 	int mdix_ctrl, mdix, is_resolved;
8874 	unsigned int retval;
8875 
8876 	if (!phydev) {
8877 		*tp_mdix_ctrl = ETH_TP_MDI_INVALID;
8878 		*tp_mdix = ETH_TP_MDI_INVALID;
8879 		return;
8880 	}
8881 
8882 	phy_write(phydev, HCLGE_PHY_PAGE_REG, HCLGE_PHY_PAGE_MDIX);
8883 
8884 	retval = phy_read(phydev, HCLGE_PHY_CSC_REG);
8885 	mdix_ctrl = hnae3_get_field(retval, HCLGE_PHY_MDIX_CTRL_M,
8886 				    HCLGE_PHY_MDIX_CTRL_S);
8887 
8888 	retval = phy_read(phydev, HCLGE_PHY_CSS_REG);
8889 	mdix = hnae3_get_bit(retval, HCLGE_PHY_MDIX_STATUS_B);
8890 	is_resolved = hnae3_get_bit(retval, HCLGE_PHY_SPEED_DUP_RESOLVE_B);
8891 
8892 	phy_write(phydev, HCLGE_PHY_PAGE_REG, HCLGE_PHY_PAGE_COPPER);
8893 
8894 	switch (mdix_ctrl) {
8895 	case 0x0:
8896 		*tp_mdix_ctrl = ETH_TP_MDI;
8897 		break;
8898 	case 0x1:
8899 		*tp_mdix_ctrl = ETH_TP_MDI_X;
8900 		break;
8901 	case 0x3:
8902 		*tp_mdix_ctrl = ETH_TP_MDI_AUTO;
8903 		break;
8904 	default:
8905 		*tp_mdix_ctrl = ETH_TP_MDI_INVALID;
8906 		break;
8907 	}
8908 
8909 	if (!is_resolved)
8910 		*tp_mdix = ETH_TP_MDI_INVALID;
8911 	else if (mdix)
8912 		*tp_mdix = ETH_TP_MDI_X;
8913 	else
8914 		*tp_mdix = ETH_TP_MDI;
8915 }
8916 
8917 static void hclge_info_show(struct hclge_dev *hdev)
8918 {
8919 	struct hnae3_handle *handle = &hdev->vport->nic;
8920 	struct device *dev = &hdev->pdev->dev;
8921 
8922 	dev_info(dev, "PF info begin:\n");
8923 
8924 	dev_info(dev, "Task queue pairs numbers: %u\n", hdev->num_tqps);
8925 	dev_info(dev, "Desc num per TX queue: %u\n", hdev->num_tx_desc);
8926 	dev_info(dev, "Desc num per RX queue: %u\n", hdev->num_rx_desc);
8927 	dev_info(dev, "Numbers of vports: %u\n", hdev->num_alloc_vport);
8928 	dev_info(dev, "Numbers of VF for this PF: %u\n", hdev->num_req_vfs);
8929 	dev_info(dev, "HW tc map: 0x%x\n", hdev->hw_tc_map);
8930 	dev_info(dev, "Total buffer size for TX/RX: %u\n", hdev->pkt_buf_size);
8931 	dev_info(dev, "TX buffer size for each TC: %u\n", hdev->tx_buf_size);
8932 	dev_info(dev, "DV buffer size for each TC: %u\n", hdev->dv_buf_size);
8933 	dev_info(dev, "This is %s PF\n",
8934 		 hdev->flag & HCLGE_FLAG_MAIN ? "main" : "not main");
8935 	dev_info(dev, "DCB %s\n",
8936 		 str_enable_disable(handle->kinfo.tc_info.dcb_ets_active));
8937 	dev_info(dev, "MQPRIO %s\n",
8938 		 str_enable_disable(handle->kinfo.tc_info.mqprio_active));
8939 	dev_info(dev, "Default tx spare buffer size: %u\n",
8940 		 hdev->tx_spare_buf_size);
8941 
8942 	dev_info(dev, "PF info end.\n");
8943 }
8944 
8945 static int hclge_init_nic_client_instance(struct hnae3_ae_dev *ae_dev,
8946 					  struct hclge_vport *vport)
8947 {
8948 	struct hnae3_client *client = vport->nic.client;
8949 	struct hclge_dev *hdev = ae_dev->priv;
8950 	u32 rst_cnt = hdev->rst_stats.reset_cnt;
8951 	int ret;
8952 
8953 	ret = client->ops->init_instance(&vport->nic);
8954 	if (ret)
8955 		return ret;
8956 
8957 	set_bit(HCLGE_STATE_NIC_REGISTERED, &hdev->state);
8958 	if (test_bit(HCLGE_STATE_RST_HANDLING, &hdev->state) ||
8959 	    rst_cnt != hdev->rst_stats.reset_cnt) {
8960 		ret = -EBUSY;
8961 		goto init_nic_err;
8962 	}
8963 
8964 	/* Enable nic hw error interrupts */
8965 	ret = hclge_config_nic_hw_error(hdev, true);
8966 	if (ret) {
8967 		dev_err(&ae_dev->pdev->dev,
8968 			"fail(%d) to enable hw error interrupts\n", ret);
8969 		goto init_nic_err;
8970 	}
8971 
8972 	hnae3_set_client_init_flag(client, ae_dev, 1);
8973 
8974 	if (netif_msg_drv(&hdev->vport->nic))
8975 		hclge_info_show(hdev);
8976 
8977 	return ret;
8978 
8979 init_nic_err:
8980 	clear_bit(HCLGE_STATE_NIC_REGISTERED, &hdev->state);
8981 	while (test_bit(HCLGE_STATE_RST_HANDLING, &hdev->state))
8982 		msleep(HCLGE_WAIT_RESET_DONE);
8983 
8984 	client->ops->uninit_instance(&vport->nic, 0);
8985 
8986 	return ret;
8987 }
8988 
8989 static int hclge_init_roce_client_instance(struct hnae3_ae_dev *ae_dev,
8990 					   struct hclge_vport *vport)
8991 {
8992 	struct hclge_dev *hdev = ae_dev->priv;
8993 	struct hnae3_client *client;
8994 	u32 rst_cnt;
8995 	int ret;
8996 
8997 	if (!hnae3_dev_roce_supported(hdev) || !hdev->roce_client ||
8998 	    !hdev->nic_client)
8999 		return 0;
9000 
9001 	client = hdev->roce_client;
9002 	ret = hclge_init_roce_base_info(vport);
9003 	if (ret)
9004 		return ret;
9005 
9006 	rst_cnt = hdev->rst_stats.reset_cnt;
9007 	ret = client->ops->init_instance(&vport->roce);
9008 	if (ret)
9009 		return ret;
9010 
9011 	set_bit(HCLGE_STATE_ROCE_REGISTERED, &hdev->state);
9012 	if (test_bit(HCLGE_STATE_RST_HANDLING, &hdev->state) ||
9013 	    rst_cnt != hdev->rst_stats.reset_cnt) {
9014 		ret = -EBUSY;
9015 		goto init_roce_err;
9016 	}
9017 
9018 	/* Enable roce ras interrupts */
9019 	ret = hclge_config_rocee_ras_interrupt(hdev, true);
9020 	if (ret) {
9021 		dev_err(&ae_dev->pdev->dev,
9022 			"fail(%d) to enable roce ras interrupts\n", ret);
9023 		goto init_roce_err;
9024 	}
9025 
9026 	hnae3_set_client_init_flag(client, ae_dev, 1);
9027 
9028 	return 0;
9029 
9030 init_roce_err:
9031 	clear_bit(HCLGE_STATE_ROCE_REGISTERED, &hdev->state);
9032 	while (test_bit(HCLGE_STATE_RST_HANDLING, &hdev->state))
9033 		msleep(HCLGE_WAIT_RESET_DONE);
9034 
9035 	hdev->roce_client->ops->uninit_instance(&vport->roce, 0);
9036 
9037 	return ret;
9038 }
9039 
9040 static int hclge_init_client_instance(struct hnae3_client *client,
9041 				      struct hnae3_ae_dev *ae_dev)
9042 {
9043 	struct hclge_dev *hdev = ae_dev->priv;
9044 	struct hclge_vport *vport = &hdev->vport[0];
9045 	int ret;
9046 
9047 	switch (client->type) {
9048 	case HNAE3_CLIENT_KNIC:
9049 		hdev->nic_client = client;
9050 		vport->nic.client = client;
9051 		ret = hclge_init_nic_client_instance(ae_dev, vport);
9052 		if (ret)
9053 			goto clear_nic;
9054 
9055 		ret = hclge_init_roce_client_instance(ae_dev, vport);
9056 		if (ret)
9057 			goto clear_roce;
9058 
9059 		break;
9060 	case HNAE3_CLIENT_ROCE:
9061 		if (hnae3_dev_roce_supported(hdev)) {
9062 			hdev->roce_client = client;
9063 			vport->roce.client = client;
9064 		}
9065 
9066 		ret = hclge_init_roce_client_instance(ae_dev, vport);
9067 		if (ret)
9068 			goto clear_roce;
9069 
9070 		break;
9071 	default:
9072 		return -EINVAL;
9073 	}
9074 
9075 	return 0;
9076 
9077 clear_nic:
9078 	hdev->nic_client = NULL;
9079 	vport->nic.client = NULL;
9080 	return ret;
9081 clear_roce:
9082 	hdev->roce_client = NULL;
9083 	vport->roce.client = NULL;
9084 	return ret;
9085 }
9086 
9087 static bool hclge_uninit_need_wait(struct hclge_dev *hdev)
9088 {
9089 	return test_bit(HCLGE_STATE_RST_HANDLING, &hdev->state) ||
9090 	       test_bit(HCLGE_STATE_LINK_UPDATING, &hdev->state);
9091 }
9092 
9093 static void hclge_uninit_client_instance(struct hnae3_client *client,
9094 					 struct hnae3_ae_dev *ae_dev)
9095 {
9096 	struct hclge_dev *hdev = ae_dev->priv;
9097 	struct hclge_vport *vport = &hdev->vport[0];
9098 
9099 	if (hdev->roce_client) {
9100 		clear_bit(HCLGE_STATE_ROCE_REGISTERED, &hdev->state);
9101 		while (hclge_uninit_need_wait(hdev))
9102 			msleep(HCLGE_WAIT_RESET_DONE);
9103 
9104 		hdev->roce_client->ops->uninit_instance(&vport->roce, 0);
9105 		hdev->roce_client = NULL;
9106 		vport->roce.client = NULL;
9107 	}
9108 	if (client->type == HNAE3_CLIENT_ROCE)
9109 		return;
9110 	if (hdev->nic_client && client->ops->uninit_instance) {
9111 		clear_bit(HCLGE_STATE_NIC_REGISTERED, &hdev->state);
9112 		while (test_bit(HCLGE_STATE_RST_HANDLING, &hdev->state))
9113 			msleep(HCLGE_WAIT_RESET_DONE);
9114 
9115 		client->ops->uninit_instance(&vport->nic, 0);
9116 		hdev->nic_client = NULL;
9117 		vport->nic.client = NULL;
9118 	}
9119 }
9120 
9121 static int hclge_dev_mem_map(struct hclge_dev *hdev)
9122 {
9123 	struct pci_dev *pdev = hdev->pdev;
9124 	struct hclge_hw *hw = &hdev->hw;
9125 
9126 	/* for device does not have device memory, return directly */
9127 	if (!(pci_select_bars(pdev, IORESOURCE_MEM) & BIT(HCLGE_MEM_BAR)))
9128 		return 0;
9129 
9130 	hw->hw.mem_base =
9131 		devm_ioremap_wc(&pdev->dev,
9132 				pci_resource_start(pdev, HCLGE_MEM_BAR),
9133 				pci_resource_len(pdev, HCLGE_MEM_BAR));
9134 	if (!hw->hw.mem_base) {
9135 		dev_err(&pdev->dev, "failed to map device memory\n");
9136 		return -EFAULT;
9137 	}
9138 
9139 	return 0;
9140 }
9141 
9142 static int hclge_pci_init(struct hclge_dev *hdev)
9143 {
9144 	struct pci_dev *pdev = hdev->pdev;
9145 	struct hclge_hw *hw;
9146 	int ret;
9147 
9148 	ret = pci_enable_device(pdev);
9149 	if (ret) {
9150 		dev_err(&pdev->dev, "failed to enable PCI device\n");
9151 		return ret;
9152 	}
9153 
9154 	ret = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
9155 	if (ret) {
9156 		dev_err(&pdev->dev, "can't set consistent PCI DMA\n");
9157 		goto err_disable_device;
9158 	}
9159 
9160 	ret = pci_request_regions(pdev, HCLGE_DRIVER_NAME);
9161 	if (ret) {
9162 		dev_err(&pdev->dev, "PCI request regions failed %d\n", ret);
9163 		goto err_disable_device;
9164 	}
9165 
9166 	pci_set_master(pdev);
9167 	hw = &hdev->hw;
9168 	hw->hw.io_base = pcim_iomap(pdev, 2, 0);
9169 	if (!hw->hw.io_base) {
9170 		dev_err(&pdev->dev, "Can't map configuration register space\n");
9171 		ret = -ENOMEM;
9172 		goto err_release_regions;
9173 	}
9174 
9175 	ret = hclge_dev_mem_map(hdev);
9176 	if (ret)
9177 		goto err_unmap_io_base;
9178 
9179 	hdev->num_req_vfs = pci_sriov_get_totalvfs(pdev);
9180 
9181 	return 0;
9182 
9183 err_unmap_io_base:
9184 	pcim_iounmap(pdev, hdev->hw.hw.io_base);
9185 err_release_regions:
9186 	pci_release_regions(pdev);
9187 err_disable_device:
9188 	pci_disable_device(pdev);
9189 
9190 	return ret;
9191 }
9192 
9193 static void hclge_pci_uninit(struct hclge_dev *hdev)
9194 {
9195 	struct pci_dev *pdev = hdev->pdev;
9196 
9197 	if (hdev->hw.hw.mem_base)
9198 		devm_iounmap(&pdev->dev, hdev->hw.hw.mem_base);
9199 
9200 	pcim_iounmap(pdev, hdev->hw.hw.io_base);
9201 	pci_free_irq_vectors(pdev);
9202 	pci_release_regions(pdev);
9203 	pci_disable_device(pdev);
9204 }
9205 
9206 static void hclge_state_init(struct hclge_dev *hdev)
9207 {
9208 	set_bit(HCLGE_STATE_SERVICE_INITED, &hdev->state);
9209 	set_bit(HCLGE_STATE_DOWN, &hdev->state);
9210 	clear_bit(HCLGE_STATE_RST_SERVICE_SCHED, &hdev->state);
9211 	clear_bit(HCLGE_STATE_RST_HANDLING, &hdev->state);
9212 	clear_bit(HCLGE_STATE_RST_FAIL, &hdev->state);
9213 	clear_bit(HCLGE_STATE_MBX_SERVICE_SCHED, &hdev->state);
9214 	clear_bit(HCLGE_STATE_MBX_HANDLING, &hdev->state);
9215 }
9216 
9217 static void hclge_state_uninit(struct hclge_dev *hdev)
9218 {
9219 	set_bit(HCLGE_STATE_DOWN, &hdev->state);
9220 	set_bit(HCLGE_STATE_REMOVING, &hdev->state);
9221 
9222 	if (hdev->reset_timer.function)
9223 		timer_delete_sync(&hdev->reset_timer);
9224 	if (hdev->service_task.work.func)
9225 		cancel_delayed_work_sync(&hdev->service_task);
9226 }
9227 
9228 static void hclge_reset_prepare_general(struct hnae3_ae_dev *ae_dev,
9229 					enum hnae3_reset_type rst_type)
9230 {
9231 #define HCLGE_RESET_RETRY_WAIT_MS	500
9232 #define HCLGE_RESET_RETRY_CNT	5
9233 
9234 	struct hclge_dev *hdev = ae_dev->priv;
9235 	int retry_cnt = 0;
9236 	int ret;
9237 
9238 	while (retry_cnt++ < HCLGE_RESET_RETRY_CNT) {
9239 		down(&hdev->reset_sem);
9240 		set_bit(HCLGE_STATE_RST_HANDLING, &hdev->state);
9241 		hdev->reset_type = rst_type;
9242 		ret = hclge_reset_prepare(hdev);
9243 		if (!ret && !hdev->reset_pending)
9244 			break;
9245 
9246 		dev_err(&hdev->pdev->dev,
9247 			"failed to prepare to reset, ret=%d, reset_pending:0x%lx, retry_cnt:%d\n",
9248 			ret, hdev->reset_pending, retry_cnt);
9249 		clear_bit(HCLGE_STATE_RST_HANDLING, &hdev->state);
9250 		up(&hdev->reset_sem);
9251 		msleep(HCLGE_RESET_RETRY_WAIT_MS);
9252 	}
9253 
9254 	/* disable misc vector before reset done */
9255 	hclge_enable_vector(&hdev->misc_vector, false);
9256 	set_bit(HCLGE_COMM_STATE_CMD_DISABLE, &hdev->hw.hw.comm_state);
9257 
9258 	if (hdev->reset_type == HNAE3_FLR_RESET)
9259 		hdev->rst_stats.flr_rst_cnt++;
9260 }
9261 
9262 static void hclge_reset_done(struct hnae3_ae_dev *ae_dev)
9263 {
9264 	struct hclge_dev *hdev = ae_dev->priv;
9265 	int ret;
9266 
9267 	hclge_enable_vector(&hdev->misc_vector, true);
9268 
9269 	ret = hclge_reset_rebuild(hdev);
9270 	if (ret)
9271 		dev_err(&hdev->pdev->dev, "fail to rebuild, ret=%d\n", ret);
9272 
9273 	hdev->reset_type = HNAE3_NONE_RESET;
9274 	if (test_and_clear_bit(HCLGE_STATE_RST_HANDLING, &hdev->state))
9275 		up(&hdev->reset_sem);
9276 }
9277 
9278 static void hclge_clear_resetting_state(struct hclge_dev *hdev)
9279 {
9280 	u16 i;
9281 
9282 	for (i = 0; i < hdev->num_alloc_vport; i++) {
9283 		struct hclge_vport *vport = &hdev->vport[i];
9284 		int ret;
9285 
9286 		 /* Send cmd to clear vport's FUNC_RST_ING */
9287 		ret = hclge_set_vf_rst(hdev, vport->vport_id, false);
9288 		if (ret)
9289 			dev_warn(&hdev->pdev->dev,
9290 				 "clear vport(%u) rst failed %d!\n",
9291 				 vport->vport_id, ret);
9292 	}
9293 }
9294 
9295 static int hclge_clear_hw_resource(struct hclge_dev *hdev)
9296 {
9297 	struct hclge_desc desc;
9298 	int ret;
9299 
9300 	hclge_cmd_setup_basic_desc(&desc, HCLGE_OPC_CLEAR_HW_RESOURCE, false);
9301 
9302 	ret = hclge_cmd_send(&hdev->hw, &desc, 1);
9303 	/* This new command is only supported by new firmware, it will
9304 	 * fail with older firmware. Error value -EOPNOSUPP can only be
9305 	 * returned by older firmware running this command, to keep code
9306 	 * backward compatible we will override this value and return
9307 	 * success.
9308 	 */
9309 	if (ret && ret != -EOPNOTSUPP) {
9310 		dev_err(&hdev->pdev->dev,
9311 			"failed to clear hw resource, ret = %d\n", ret);
9312 		return ret;
9313 	}
9314 	return 0;
9315 }
9316 
9317 static void hclge_init_rxd_adv_layout(struct hclge_dev *hdev)
9318 {
9319 	if (hnae3_ae_dev_rxd_adv_layout_supported(hdev->ae_dev))
9320 		hclge_write_dev(&hdev->hw, HCLGE_RXD_ADV_LAYOUT_EN_REG, 1);
9321 }
9322 
9323 static void hclge_uninit_rxd_adv_layout(struct hclge_dev *hdev)
9324 {
9325 	if (hnae3_ae_dev_rxd_adv_layout_supported(hdev->ae_dev))
9326 		hclge_write_dev(&hdev->hw, HCLGE_RXD_ADV_LAYOUT_EN_REG, 0);
9327 }
9328 
9329 static struct hclge_wol_info *hclge_get_wol_info(struct hnae3_handle *handle)
9330 {
9331 	struct hclge_vport *vport = hclge_get_vport(handle);
9332 
9333 	return &vport->back->hw.mac.wol;
9334 }
9335 
9336 static int hclge_get_wol_supported_mode(struct hclge_dev *hdev,
9337 					u32 *wol_supported)
9338 {
9339 	struct hclge_query_wol_supported_cmd *wol_supported_cmd;
9340 	struct hclge_desc desc;
9341 	int ret;
9342 
9343 	hclge_cmd_setup_basic_desc(&desc, HCLGE_OPC_WOL_GET_SUPPORTED_MODE,
9344 				   true);
9345 	wol_supported_cmd = (struct hclge_query_wol_supported_cmd *)desc.data;
9346 
9347 	ret = hclge_cmd_send(&hdev->hw, &desc, 1);
9348 	if (ret) {
9349 		dev_err(&hdev->pdev->dev,
9350 			"failed to query wol supported, ret = %d\n", ret);
9351 		return ret;
9352 	}
9353 
9354 	*wol_supported = le32_to_cpu(wol_supported_cmd->supported_wake_mode);
9355 
9356 	return 0;
9357 }
9358 
9359 static int hclge_set_wol_cfg(struct hclge_dev *hdev,
9360 			     struct hclge_wol_info *wol_info)
9361 {
9362 	struct hclge_wol_cfg_cmd *wol_cfg_cmd;
9363 	struct hclge_desc desc;
9364 	int ret;
9365 
9366 	hclge_cmd_setup_basic_desc(&desc, HCLGE_OPC_WOL_CFG, false);
9367 	wol_cfg_cmd = (struct hclge_wol_cfg_cmd *)desc.data;
9368 	wol_cfg_cmd->wake_on_lan_mode = cpu_to_le32(wol_info->wol_current_mode);
9369 	wol_cfg_cmd->sopass_size = wol_info->wol_sopass_size;
9370 	memcpy(wol_cfg_cmd->sopass, wol_info->wol_sopass, SOPASS_MAX);
9371 
9372 	ret = hclge_cmd_send(&hdev->hw, &desc, 1);
9373 	if (ret)
9374 		dev_err(&hdev->pdev->dev,
9375 			"failed to set wol config, ret = %d\n", ret);
9376 
9377 	return ret;
9378 }
9379 
9380 static int hclge_update_wol(struct hclge_dev *hdev)
9381 {
9382 	struct hclge_wol_info *wol_info = &hdev->hw.mac.wol;
9383 
9384 	if (!hnae3_ae_dev_wol_supported(hdev->ae_dev))
9385 		return 0;
9386 
9387 	return hclge_set_wol_cfg(hdev, wol_info);
9388 }
9389 
9390 static int hclge_init_wol(struct hclge_dev *hdev)
9391 {
9392 	struct hclge_wol_info *wol_info = &hdev->hw.mac.wol;
9393 	int ret;
9394 
9395 	if (!hnae3_ae_dev_wol_supported(hdev->ae_dev))
9396 		return 0;
9397 
9398 	memset(wol_info, 0, sizeof(struct hclge_wol_info));
9399 	ret = hclge_get_wol_supported_mode(hdev,
9400 					   &wol_info->wol_support_mode);
9401 	if (ret) {
9402 		wol_info->wol_support_mode = 0;
9403 		return ret;
9404 	}
9405 
9406 	return hclge_update_wol(hdev);
9407 }
9408 
9409 static void hclge_init_pfc_prevention_tout(struct hclge_dev *hdev)
9410 {
9411 	struct hnae3_handle *handle = &hdev->vport[0].nic;
9412 	u16 times;
9413 	int ret;
9414 
9415 	ret = hclge_enable_pfc_storm_prevent(hdev, HCLGE_DIR_RX, false);
9416 	if (ret == -EOPNOTSUPP)
9417 		return;
9418 	else if (ret)
9419 		dev_warn(&hdev->pdev->dev,
9420 			 "failed to disable rx pfc storm prevent, ret = %d\n",
9421 			 ret);
9422 
9423 	ret = hclge_get_pfc_prevention_tout(handle, &times);
9424 	if (ret) {
9425 		dev_warn(&hdev->pdev->dev,
9426 			 "failed to get tx pfc prevention timeout, ret = %d\n",
9427 			 ret);
9428 		times = HCLGE_DEFAULT_PFC_PREVENTION_TOUT;
9429 	}
9430 
9431 	hdev->pfc_prevention_tout = times;
9432 	hdev->pfc_prevention_tout_default = times;
9433 }
9434 
9435 static void hclge_get_wol(struct hnae3_handle *handle,
9436 			  struct ethtool_wolinfo *wol)
9437 {
9438 	struct hclge_wol_info *wol_info = hclge_get_wol_info(handle);
9439 
9440 	wol->supported = wol_info->wol_support_mode;
9441 	wol->wolopts = wol_info->wol_current_mode;
9442 	if (wol_info->wol_current_mode & WAKE_MAGICSECURE)
9443 		memcpy(wol->sopass, wol_info->wol_sopass, SOPASS_MAX);
9444 }
9445 
9446 static int hclge_set_wol(struct hnae3_handle *handle,
9447 			 struct ethtool_wolinfo *wol)
9448 {
9449 	struct hclge_wol_info *wol_info = hclge_get_wol_info(handle);
9450 	struct hclge_vport *vport = hclge_get_vport(handle);
9451 	u32 wol_mode;
9452 	int ret;
9453 
9454 	wol_mode = wol->wolopts;
9455 	if (wol_mode & ~wol_info->wol_support_mode)
9456 		return -EINVAL;
9457 
9458 	wol_info->wol_current_mode = wol_mode;
9459 	if (wol_mode & WAKE_MAGICSECURE) {
9460 		memcpy(wol_info->wol_sopass, wol->sopass, SOPASS_MAX);
9461 		wol_info->wol_sopass_size = SOPASS_MAX;
9462 	} else {
9463 		wol_info->wol_sopass_size = 0;
9464 	}
9465 
9466 	ret = hclge_set_wol_cfg(vport->back, wol_info);
9467 	if (ret)
9468 		wol_info->wol_current_mode = 0;
9469 
9470 	return ret;
9471 }
9472 
9473 static int hclge_set_autoneg_speed_dup(struct hclge_dev *hdev)
9474 {
9475 	int ret;
9476 
9477 	if (hdev->hw.mac.support_autoneg) {
9478 		ret = hclge_set_autoneg_en(hdev, hdev->hw.mac.req_autoneg);
9479 		if (ret)
9480 			return ret;
9481 	}
9482 
9483 	if (!hdev->hw.mac.req_autoneg) {
9484 		ret = hclge_cfg_mac_speed_dup_hw(hdev, hdev->hw.mac.req_speed,
9485 						 hdev->hw.mac.req_duplex,
9486 						 hdev->hw.mac.req_lane_num);
9487 		if (ret)
9488 			return ret;
9489 	}
9490 
9491 	return 0;
9492 }
9493 
9494 static int hclge_init_ae_dev(struct hnae3_ae_dev *ae_dev)
9495 {
9496 	struct pci_dev *pdev = ae_dev->pdev;
9497 	struct hclge_dev *hdev;
9498 	int ret;
9499 
9500 	hdev = devm_kzalloc_obj(&pdev->dev, *hdev);
9501 	if (!hdev)
9502 		return -ENOMEM;
9503 
9504 	hdev->pdev = pdev;
9505 	hdev->ae_dev = ae_dev;
9506 	hdev->reset_type = HNAE3_NONE_RESET;
9507 	hdev->reset_level = HNAE3_FUNC_RESET;
9508 	ae_dev->priv = hdev;
9509 
9510 	/* HW supprt 2 layer vlan */
9511 	hdev->mps = ETH_FRAME_LEN + ETH_FCS_LEN + 2 * VLAN_HLEN;
9512 
9513 	mutex_init(&hdev->vport_lock);
9514 	spin_lock_init(&hdev->fd_rule_lock);
9515 	sema_init(&hdev->reset_sem, 1);
9516 
9517 	ret = hclge_pci_init(hdev);
9518 	if (ret)
9519 		goto out;
9520 
9521 	/* Firmware command queue initialize */
9522 	ret = hclge_comm_cmd_queue_init(hdev->pdev, &hdev->hw.hw);
9523 	if (ret)
9524 		goto err_pci_uninit;
9525 
9526 	/* Firmware command initialize */
9527 	hclge_comm_cmd_init_ops(&hdev->hw.hw, &hclge_cmq_ops);
9528 	ret = hclge_comm_cmd_init(hdev->ae_dev, &hdev->hw.hw, &hdev->fw_version,
9529 				  true, hdev->reset_pending);
9530 	if (ret)
9531 		goto err_cmd_uninit;
9532 
9533 	ret  = hclge_clear_hw_resource(hdev);
9534 	if (ret)
9535 		goto err_cmd_uninit;
9536 
9537 	ret = hclge_get_cap(hdev);
9538 	if (ret)
9539 		goto err_cmd_uninit;
9540 
9541 	ret = hclge_query_dev_specs(hdev);
9542 	if (ret) {
9543 		dev_err(&pdev->dev, "failed to query dev specifications, ret = %d.\n",
9544 			ret);
9545 		goto err_cmd_uninit;
9546 	}
9547 
9548 	ret = hclge_configure(hdev);
9549 	if (ret) {
9550 		dev_err(&pdev->dev, "Configure dev error, ret = %d.\n", ret);
9551 		goto err_cmd_uninit;
9552 	}
9553 
9554 	ret = hclge_init_msi(hdev);
9555 	if (ret) {
9556 		dev_err(&pdev->dev, "Init MSI/MSI-X error, ret = %d.\n", ret);
9557 		goto err_cmd_uninit;
9558 	}
9559 
9560 	ret = hclge_misc_irq_init(hdev);
9561 	if (ret)
9562 		goto err_msi_uninit;
9563 
9564 	ret = hclge_alloc_tqps(hdev);
9565 	if (ret) {
9566 		dev_err(&pdev->dev, "Allocate TQPs error, ret = %d.\n", ret);
9567 		goto err_msi_irq_uninit;
9568 	}
9569 
9570 	ret = hclge_alloc_vport(hdev);
9571 	if (ret)
9572 		goto err_msi_irq_uninit;
9573 
9574 	ret = hclge_map_tqp(hdev);
9575 	if (ret)
9576 		goto err_msi_irq_uninit;
9577 
9578 	if (hdev->hw.mac.media_type == HNAE3_MEDIA_TYPE_COPPER) {
9579 		clear_bit(HNAE3_DEV_SUPPORT_FEC_B, ae_dev->caps);
9580 		if (hnae3_dev_phy_imp_supported(hdev))
9581 			ret = hclge_update_tp_port_info(hdev);
9582 		else
9583 			ret = hclge_mac_mdio_config(hdev);
9584 
9585 		if (ret)
9586 			goto err_msi_irq_uninit;
9587 	}
9588 
9589 	ret = hclge_init_umv_space(hdev);
9590 	if (ret)
9591 		goto err_mdiobus_unreg;
9592 
9593 	ret = hclge_mac_init(hdev);
9594 	if (ret) {
9595 		dev_err(&pdev->dev, "Mac init error, ret = %d\n", ret);
9596 		goto err_mdiobus_unreg;
9597 	}
9598 
9599 	ret = hclge_config_tso(hdev, HCLGE_TSO_MSS_MIN, HCLGE_TSO_MSS_MAX);
9600 	if (ret) {
9601 		dev_err(&pdev->dev, "Enable tso fail, ret =%d\n", ret);
9602 		goto err_mdiobus_unreg;
9603 	}
9604 
9605 	ret = hclge_config_gro(hdev);
9606 	if (ret)
9607 		goto err_mdiobus_unreg;
9608 
9609 	ret = hclge_init_vlan_config(hdev);
9610 	if (ret) {
9611 		dev_err(&pdev->dev, "VLAN init fail, ret =%d\n", ret);
9612 		goto err_mdiobus_unreg;
9613 	}
9614 
9615 	ret = hclge_tm_schd_init(hdev);
9616 	if (ret) {
9617 		dev_err(&pdev->dev, "tm schd init fail, ret =%d\n", ret);
9618 		goto err_mdiobus_unreg;
9619 	}
9620 
9621 	ret = hclge_comm_rss_init_cfg(&hdev->vport->nic, hdev->ae_dev,
9622 				      &hdev->rss_cfg);
9623 	if (ret) {
9624 		dev_err(&pdev->dev, "failed to init rss cfg, ret = %d\n", ret);
9625 		goto err_mdiobus_unreg;
9626 	}
9627 
9628 	ret = hclge_rss_init_hw(hdev);
9629 	if (ret) {
9630 		dev_err(&pdev->dev, "Rss init fail, ret =%d\n", ret);
9631 		goto err_mdiobus_unreg;
9632 	}
9633 
9634 	ret = init_mgr_tbl(hdev);
9635 	if (ret) {
9636 		dev_err(&pdev->dev, "manager table init fail, ret =%d\n", ret);
9637 		goto err_mdiobus_unreg;
9638 	}
9639 
9640 	ret = hclge_init_fd_config(hdev);
9641 	if (ret) {
9642 		dev_err(&pdev->dev,
9643 			"fd table init fail, ret=%d\n", ret);
9644 		goto err_mdiobus_unreg;
9645 	}
9646 
9647 	ret = hclge_ptp_init(hdev);
9648 	if (ret)
9649 		goto err_mdiobus_unreg;
9650 
9651 	ret = hclge_update_port_info(hdev);
9652 	if (ret)
9653 		goto err_ptp_uninit;
9654 
9655 	if (hdev->hw.mac.media_type != HNAE3_MEDIA_TYPE_COPPER)
9656 		hdev->hw.mac.req_autoneg = hdev->hw.mac.autoneg;
9657 
9658 	ret = hclge_set_autoneg_speed_dup(hdev);
9659 	if (ret) {
9660 		dev_err(&pdev->dev,
9661 			"failed to set autoneg speed duplex, ret = %d\n", ret);
9662 		goto err_ptp_uninit;
9663 	}
9664 
9665 	INIT_KFIFO(hdev->mac_tnl_log);
9666 
9667 	hclge_dcb_ops_set(hdev);
9668 
9669 	timer_setup(&hdev->reset_timer, hclge_reset_timer, 0);
9670 	INIT_DELAYED_WORK(&hdev->service_task, hclge_service_task);
9671 
9672 	hclge_clear_all_event_cause(hdev);
9673 	hclge_clear_resetting_state(hdev);
9674 
9675 	/* Log and clear the hw errors those already occurred */
9676 	if (hnae3_dev_ras_imp_supported(hdev))
9677 		hclge_handle_occurred_error(hdev);
9678 	else
9679 		hclge_handle_all_hns_hw_errors(ae_dev);
9680 
9681 	/* request delayed reset for the error recovery because an immediate
9682 	 * global reset on a PF affecting pending initialization of other PFs
9683 	 */
9684 	if (ae_dev->hw_err_reset_req) {
9685 		enum hnae3_reset_type reset_level;
9686 
9687 		reset_level = hclge_get_reset_level(ae_dev,
9688 						    &ae_dev->hw_err_reset_req);
9689 		hclge_set_def_reset_request(ae_dev, reset_level);
9690 		mod_timer(&hdev->reset_timer, jiffies + HCLGE_RESET_INTERVAL);
9691 	}
9692 
9693 	hclge_init_rxd_adv_layout(hdev);
9694 
9695 	ret = hclge_init_wol(hdev);
9696 	if (ret)
9697 		dev_warn(&pdev->dev,
9698 			 "failed to wake on lan init, ret = %d\n", ret);
9699 
9700 	hclge_init_pfc_prevention_tout(hdev);
9701 
9702 	ret = hclge_devlink_init(hdev);
9703 	if (ret)
9704 		goto err_ptp_uninit;
9705 
9706 	hclge_state_init(hdev);
9707 	hdev->last_reset_time = jiffies;
9708 
9709 	/* Enable MISC vector(vector0) */
9710 	enable_irq(hdev->misc_vector.vector_irq);
9711 	hclge_enable_vector(&hdev->misc_vector, true);
9712 
9713 	dev_info(&hdev->pdev->dev, "%s driver initialization finished.\n",
9714 		 HCLGE_DRIVER_NAME);
9715 
9716 	hclge_task_schedule(hdev, round_jiffies_relative(HZ));
9717 	return 0;
9718 
9719 err_ptp_uninit:
9720 	hclge_ptp_uninit(hdev);
9721 err_mdiobus_unreg:
9722 	if (hdev->hw.mac.phydev)
9723 		mdiobus_unregister(hdev->hw.mac.mdio_bus);
9724 err_msi_irq_uninit:
9725 	hclge_misc_irq_uninit(hdev);
9726 err_msi_uninit:
9727 	pci_free_irq_vectors(pdev);
9728 err_cmd_uninit:
9729 	hclge_comm_cmd_uninit(hdev->ae_dev, &hdev->hw.hw);
9730 err_pci_uninit:
9731 	pcim_iounmap(pdev, hdev->hw.hw.io_base);
9732 	pci_release_regions(pdev);
9733 	pci_disable_device(pdev);
9734 out:
9735 	mutex_destroy(&hdev->vport_lock);
9736 	return ret;
9737 }
9738 
9739 static void hclge_stats_clear(struct hclge_dev *hdev)
9740 {
9741 	memset(&hdev->mac_stats, 0, sizeof(hdev->mac_stats));
9742 	memset(&hdev->fec_stats, 0, sizeof(hdev->fec_stats));
9743 }
9744 
9745 static int hclge_set_mac_spoofchk(struct hclge_dev *hdev, int vf, bool enable)
9746 {
9747 	return hclge_config_switch_param(hdev, vf, enable,
9748 					 HCLGE_SWITCH_ANTI_SPOOF_MASK);
9749 }
9750 
9751 static int hclge_set_vlan_spoofchk(struct hclge_dev *hdev, int vf, bool enable)
9752 {
9753 	return hclge_set_vlan_filter_ctrl(hdev, HCLGE_FILTER_TYPE_VF,
9754 					  HCLGE_FILTER_FE_NIC_INGRESS_B,
9755 					  enable, vf);
9756 }
9757 
9758 static int hclge_set_vf_spoofchk_hw(struct hclge_dev *hdev, int vf, bool enable)
9759 {
9760 	int ret;
9761 
9762 	ret = hclge_set_mac_spoofchk(hdev, vf, enable);
9763 	if (ret) {
9764 		dev_err(&hdev->pdev->dev,
9765 			"Set vf %d mac spoof check %s failed, ret=%d\n",
9766 			vf, str_on_off(enable), ret);
9767 		return ret;
9768 	}
9769 
9770 	ret = hclge_set_vlan_spoofchk(hdev, vf, enable);
9771 	if (ret)
9772 		dev_err(&hdev->pdev->dev,
9773 			"Set vf %d vlan spoof check %s failed, ret=%d\n",
9774 			vf, str_on_off(enable), ret);
9775 
9776 	return ret;
9777 }
9778 
9779 static int hclge_set_vf_spoofchk(struct hnae3_handle *handle, int vf,
9780 				 bool enable)
9781 {
9782 	struct hclge_vport *vport = hclge_get_vport(handle);
9783 	struct hclge_dev *hdev = vport->back;
9784 	u32 new_spoofchk = enable ? 1 : 0;
9785 	int ret;
9786 
9787 	if (hdev->ae_dev->dev_version < HNAE3_DEVICE_VERSION_V2)
9788 		return -EOPNOTSUPP;
9789 
9790 	vport = hclge_get_vf_vport(hdev, vf);
9791 	if (!vport)
9792 		return -EINVAL;
9793 
9794 	if (vport->vf_info.spoofchk == new_spoofchk)
9795 		return 0;
9796 
9797 	if (enable && test_bit(vport->vport_id, hdev->vf_vlan_full))
9798 		dev_warn(&hdev->pdev->dev,
9799 			 "vf %d vlan table is full, enable spoof check may cause its packet send fail\n",
9800 			 vf);
9801 	else if (enable && hclge_is_umv_space_full(vport, true))
9802 		dev_warn(&hdev->pdev->dev,
9803 			 "vf %d mac table is full, enable spoof check may cause its packet send fail\n",
9804 			 vf);
9805 
9806 	ret = hclge_set_vf_spoofchk_hw(hdev, vport->vport_id, enable);
9807 	if (ret)
9808 		return ret;
9809 
9810 	vport->vf_info.spoofchk = new_spoofchk;
9811 	return 0;
9812 }
9813 
9814 static int hclge_reset_vport_spoofchk(struct hclge_dev *hdev)
9815 {
9816 	struct hclge_vport *vport = hdev->vport;
9817 	int ret;
9818 	int i;
9819 
9820 	if (hdev->ae_dev->dev_version < HNAE3_DEVICE_VERSION_V2)
9821 		return 0;
9822 
9823 	/* resume the vf spoof check state after reset */
9824 	for (i = 0; i < hdev->num_alloc_vport; i++) {
9825 		ret = hclge_set_vf_spoofchk_hw(hdev, vport->vport_id,
9826 					       vport->vf_info.spoofchk);
9827 		if (ret)
9828 			return ret;
9829 
9830 		vport++;
9831 	}
9832 
9833 	return 0;
9834 }
9835 
9836 static int hclge_set_vf_trust(struct hnae3_handle *handle, int vf, bool enable)
9837 {
9838 	struct hclge_vport *vport = hclge_get_vport(handle);
9839 	struct hclge_dev *hdev = vport->back;
9840 	u32 new_trusted = enable ? 1 : 0;
9841 
9842 	vport = hclge_get_vf_vport(hdev, vf);
9843 	if (!vport)
9844 		return -EINVAL;
9845 
9846 	if (vport->vf_info.trusted == new_trusted)
9847 		return 0;
9848 
9849 	vport->vf_info.trusted = new_trusted;
9850 	set_bit(HCLGE_VPORT_STATE_PROMISC_CHANGE, &vport->state);
9851 	hclge_task_schedule(hdev, 0);
9852 
9853 	return 0;
9854 }
9855 
9856 static void hclge_reset_vf_rate(struct hclge_dev *hdev)
9857 {
9858 	int ret;
9859 	int vf;
9860 
9861 	/* reset vf rate to default value */
9862 	for (vf = HCLGE_VF_VPORT_START_NUM; vf < hdev->num_alloc_vport; vf++) {
9863 		struct hclge_vport *vport = &hdev->vport[vf];
9864 
9865 		vport->vf_info.max_tx_rate = 0;
9866 		ret = hclge_tm_qs_shaper_cfg(vport, vport->vf_info.max_tx_rate);
9867 		if (ret)
9868 			dev_err(&hdev->pdev->dev,
9869 				"vf%d failed to reset to default, ret=%d\n",
9870 				vf - HCLGE_VF_VPORT_START_NUM, ret);
9871 	}
9872 }
9873 
9874 static int hclge_vf_rate_param_check(struct hclge_dev *hdev,
9875 				     int min_tx_rate, int max_tx_rate)
9876 {
9877 	if (min_tx_rate != 0 ||
9878 	    max_tx_rate < 0 || (u32)max_tx_rate > hdev->hw.mac.max_speed) {
9879 		dev_err(&hdev->pdev->dev,
9880 			"min_tx_rate:%d [0], max_tx_rate:%d [0, %u]\n",
9881 			min_tx_rate, max_tx_rate, hdev->hw.mac.max_speed);
9882 		return -EINVAL;
9883 	}
9884 
9885 	return 0;
9886 }
9887 
9888 static int hclge_set_vf_rate(struct hnae3_handle *handle, int vf,
9889 			     int min_tx_rate, int max_tx_rate, bool force)
9890 {
9891 	struct hclge_vport *vport = hclge_get_vport(handle);
9892 	struct hclge_dev *hdev = vport->back;
9893 	int ret;
9894 
9895 	ret = hclge_vf_rate_param_check(hdev, min_tx_rate, max_tx_rate);
9896 	if (ret)
9897 		return ret;
9898 
9899 	vport = hclge_get_vf_vport(hdev, vf);
9900 	if (!vport)
9901 		return -EINVAL;
9902 
9903 	if (!force && (u32)max_tx_rate == vport->vf_info.max_tx_rate)
9904 		return 0;
9905 
9906 	ret = hclge_tm_qs_shaper_cfg(vport, max_tx_rate);
9907 	if (ret)
9908 		return ret;
9909 
9910 	vport->vf_info.max_tx_rate = max_tx_rate;
9911 
9912 	return 0;
9913 }
9914 
9915 static int hclge_resume_vf_rate(struct hclge_dev *hdev)
9916 {
9917 	struct hnae3_handle *handle = &hdev->vport->nic;
9918 	struct hclge_vport *vport;
9919 	int ret;
9920 	int vf;
9921 
9922 	/* resume the vf max_tx_rate after reset */
9923 	for (vf = 0; vf < pci_num_vf(hdev->pdev); vf++) {
9924 		vport = hclge_get_vf_vport(hdev, vf);
9925 		if (!vport)
9926 			return -EINVAL;
9927 
9928 		/* zero means max rate, after reset, firmware already set it to
9929 		 * max rate, so just continue.
9930 		 */
9931 		if (!vport->vf_info.max_tx_rate)
9932 			continue;
9933 
9934 		ret = hclge_set_vf_rate(handle, vf, 0,
9935 					vport->vf_info.max_tx_rate, true);
9936 		if (ret) {
9937 			dev_err(&hdev->pdev->dev,
9938 				"vf%d failed to resume tx_rate:%u, ret=%d\n",
9939 				vf, vport->vf_info.max_tx_rate, ret);
9940 			return ret;
9941 		}
9942 	}
9943 
9944 	return 0;
9945 }
9946 
9947 static void hclge_reset_vport_state(struct hclge_dev *hdev)
9948 {
9949 	struct hclge_vport *vport = hdev->vport;
9950 	int i;
9951 
9952 	for (i = 0; i < hdev->num_alloc_vport; i++) {
9953 		clear_bit(HCLGE_VPORT_STATE_ALIVE, &vport->state);
9954 		vport++;
9955 	}
9956 }
9957 
9958 static int hclge_reset_ae_dev(struct hnae3_ae_dev *ae_dev)
9959 {
9960 	struct hclge_dev *hdev = ae_dev->priv;
9961 	struct pci_dev *pdev = ae_dev->pdev;
9962 	int ret;
9963 
9964 	set_bit(HCLGE_STATE_DOWN, &hdev->state);
9965 
9966 	hclge_stats_clear(hdev);
9967 	/* NOTE: pf reset needn't to clear or restore pf and vf table entry.
9968 	 * so here should not clean table in memory.
9969 	 */
9970 	if (hdev->reset_type == HNAE3_IMP_RESET ||
9971 	    hdev->reset_type == HNAE3_GLOBAL_RESET) {
9972 		memset(hdev->vlan_table, 0, sizeof(hdev->vlan_table));
9973 		memset(hdev->vf_vlan_full, 0, sizeof(hdev->vf_vlan_full));
9974 		bitmap_set(hdev->vport_config_block, 0, hdev->num_alloc_vport);
9975 		hclge_reset_umv_space(hdev);
9976 	}
9977 
9978 	ret = hclge_comm_cmd_init(hdev->ae_dev, &hdev->hw.hw, &hdev->fw_version,
9979 				  true, hdev->reset_pending);
9980 	if (ret) {
9981 		dev_err(&pdev->dev, "Cmd queue init failed\n");
9982 		return ret;
9983 	}
9984 
9985 	ret = hclge_map_tqp(hdev);
9986 	if (ret) {
9987 		dev_err(&pdev->dev, "Map tqp error, ret = %d.\n", ret);
9988 		return ret;
9989 	}
9990 
9991 	ret = hclge_mac_init(hdev);
9992 	if (ret) {
9993 		dev_err(&pdev->dev, "Mac init error, ret = %d\n", ret);
9994 		return ret;
9995 	}
9996 
9997 	ret = hclge_set_autoneg_speed_dup(hdev);
9998 	if (ret) {
9999 		dev_err(&pdev->dev,
10000 			"failed to set autoneg speed duplex, ret = %d\n", ret);
10001 		return ret;
10002 	}
10003 
10004 	ret = hclge_tp_port_init(hdev);
10005 	if (ret) {
10006 		dev_err(&pdev->dev, "failed to init tp port, ret = %d\n",
10007 			ret);
10008 		return ret;
10009 	}
10010 
10011 	ret = hclge_config_tso(hdev, HCLGE_TSO_MSS_MIN, HCLGE_TSO_MSS_MAX);
10012 	if (ret) {
10013 		dev_err(&pdev->dev, "Enable tso fail, ret =%d\n", ret);
10014 		return ret;
10015 	}
10016 
10017 	ret = hclge_config_gro(hdev);
10018 	if (ret)
10019 		return ret;
10020 
10021 	ret = hclge_init_vlan_config(hdev);
10022 	if (ret) {
10023 		dev_err(&pdev->dev, "VLAN init fail, ret =%d\n", ret);
10024 		return ret;
10025 	}
10026 
10027 	hclge_reset_tc_config(hdev);
10028 
10029 	ret = hclge_tm_init_hw(hdev, true);
10030 	if (ret) {
10031 		dev_err(&pdev->dev, "tm init hw fail, ret =%d\n", ret);
10032 		return ret;
10033 	}
10034 
10035 	ret = hclge_rss_init_hw(hdev);
10036 	if (ret) {
10037 		dev_err(&pdev->dev, "Rss init fail, ret =%d\n", ret);
10038 		return ret;
10039 	}
10040 
10041 	ret = init_mgr_tbl(hdev);
10042 	if (ret) {
10043 		dev_err(&pdev->dev,
10044 			"failed to reinit manager table, ret = %d\n", ret);
10045 		return ret;
10046 	}
10047 
10048 	ret = hclge_init_fd_config(hdev);
10049 	if (ret) {
10050 		dev_err(&pdev->dev, "fd table init fail, ret=%d\n", ret);
10051 		return ret;
10052 	}
10053 
10054 	ret = hclge_ptp_init(hdev);
10055 	if (ret)
10056 		return ret;
10057 
10058 	/* Log and clear the hw errors those already occurred */
10059 	if (hnae3_dev_ras_imp_supported(hdev))
10060 		hclge_handle_occurred_error(hdev);
10061 	else
10062 		hclge_handle_all_hns_hw_errors(ae_dev);
10063 
10064 	/* Re-enable the hw error interrupts because
10065 	 * the interrupts get disabled on global reset.
10066 	 */
10067 	ret = hclge_config_nic_hw_error(hdev, true);
10068 	if (ret) {
10069 		dev_err(&pdev->dev,
10070 			"fail(%d) to re-enable NIC hw error interrupts\n",
10071 			ret);
10072 		return ret;
10073 	}
10074 
10075 	if (hdev->roce_client) {
10076 		ret = hclge_config_rocee_ras_interrupt(hdev, true);
10077 		if (ret) {
10078 			dev_err(&pdev->dev,
10079 				"fail(%d) to re-enable roce ras interrupts\n",
10080 				ret);
10081 			return ret;
10082 		}
10083 	}
10084 
10085 	hclge_reset_vport_state(hdev);
10086 	ret = hclge_reset_vport_spoofchk(hdev);
10087 	if (ret)
10088 		return ret;
10089 
10090 	ret = hclge_resume_vf_rate(hdev);
10091 	if (ret)
10092 		return ret;
10093 
10094 	hclge_init_rxd_adv_layout(hdev);
10095 
10096 	ret = hclge_update_wol(hdev);
10097 	if (ret)
10098 		dev_warn(&pdev->dev,
10099 			 "failed to update wol config, ret = %d\n", ret);
10100 
10101 	hclge_restore_pfc_storm_prevention_tout(hdev);
10102 
10103 	dev_info(&pdev->dev, "Reset done, %s driver initialization finished.\n",
10104 		 HCLGE_DRIVER_NAME);
10105 
10106 	return 0;
10107 }
10108 
10109 static void hclge_uninit_ae_dev(struct hnae3_ae_dev *ae_dev)
10110 {
10111 	struct hclge_dev *hdev = ae_dev->priv;
10112 	struct hclge_mac *mac = &hdev->hw.mac;
10113 
10114 	hclge_reset_vf_rate(hdev);
10115 	hclge_clear_vf_vlan(hdev);
10116 	hclge_state_uninit(hdev);
10117 	hclge_ptp_uninit(hdev);
10118 	hclge_uninit_rxd_adv_layout(hdev);
10119 	hclge_uninit_mac_table(hdev);
10120 	hclge_del_all_fd_entries(hdev);
10121 
10122 	if (mac->phydev)
10123 		mdiobus_unregister(mac->mdio_bus);
10124 
10125 	/* Disable MISC vector(vector0) */
10126 	hclge_enable_vector(&hdev->misc_vector, false);
10127 	disable_irq(hdev->misc_vector.vector_irq);
10128 
10129 	/* Disable all hw interrupts */
10130 	hclge_config_mac_tnl_int(hdev, false);
10131 	hclge_config_nic_hw_error(hdev, false);
10132 	hclge_config_rocee_ras_interrupt(hdev, false);
10133 
10134 	/* Restore hw default values for the next initialization */
10135 	hclge_set_pfc_prevention_tout(&hdev->vport->nic,
10136 				      hdev->pfc_prevention_tout_default);
10137 
10138 	hclge_comm_cmd_uninit(hdev->ae_dev, &hdev->hw.hw);
10139 	hclge_misc_irq_uninit(hdev);
10140 	hclge_devlink_uninit(hdev);
10141 	hclge_pci_uninit(hdev);
10142 	hclge_uninit_vport_vlan_table(hdev);
10143 	mutex_destroy(&hdev->vport_lock);
10144 	ae_dev->priv = NULL;
10145 }
10146 
10147 static u32 hclge_get_max_channels(struct hnae3_handle *handle)
10148 {
10149 	struct hclge_vport *vport = hclge_get_vport(handle);
10150 	struct hclge_dev *hdev = vport->back;
10151 
10152 	return min_t(u32, hdev->pf_rss_size_max, vport->alloc_tqps);
10153 }
10154 
10155 static void hclge_get_channels(struct hnae3_handle *handle,
10156 			       struct ethtool_channels *ch)
10157 {
10158 	ch->max_combined = hclge_get_max_channels(handle);
10159 	ch->other_count = 1;
10160 	ch->max_other = 1;
10161 	ch->combined_count = handle->kinfo.rss_size;
10162 }
10163 
10164 static void hclge_get_tqps_and_rss_info(struct hnae3_handle *handle,
10165 					u16 *alloc_tqps, u16 *max_rss_size)
10166 {
10167 	struct hclge_vport *vport = hclge_get_vport(handle);
10168 	struct hclge_dev *hdev = vport->back;
10169 
10170 	*alloc_tqps = vport->alloc_tqps;
10171 	*max_rss_size = hdev->pf_rss_size_max;
10172 }
10173 
10174 static int hclge_set_rss_tc_mode_cfg(struct hnae3_handle *handle)
10175 {
10176 	struct hclge_vport *vport = hclge_get_vport(handle);
10177 	u16 tc_offset[HCLGE_MAX_TC_NUM] = {0};
10178 	struct hclge_dev *hdev = vport->back;
10179 	u16 tc_size[HCLGE_MAX_TC_NUM] = {0};
10180 	u16 tc_valid[HCLGE_MAX_TC_NUM];
10181 	u16 roundup_size;
10182 	unsigned int i;
10183 
10184 	roundup_size = roundup_pow_of_two(vport->nic.kinfo.rss_size);
10185 	roundup_size = ilog2(roundup_size);
10186 	/* Set the RSS TC mode according to the new RSS size */
10187 	for (i = 0; i < HCLGE_MAX_TC_NUM; i++) {
10188 		tc_valid[i] = 0;
10189 
10190 		if (!(hdev->hw_tc_map & BIT(i)))
10191 			continue;
10192 
10193 		tc_valid[i] = 1;
10194 		tc_size[i] = roundup_size;
10195 		tc_offset[i] = vport->nic.kinfo.rss_size * i;
10196 	}
10197 
10198 	return hclge_comm_set_rss_tc_mode(&hdev->hw.hw, tc_offset, tc_valid,
10199 					  tc_size);
10200 }
10201 
10202 static int hclge_set_channels(struct hnae3_handle *handle, u32 new_tqps_num,
10203 			      bool rxfh_configured)
10204 {
10205 	struct hnae3_ae_dev *ae_dev = pci_get_drvdata(handle->pdev);
10206 	struct hclge_vport *vport = hclge_get_vport(handle);
10207 	struct hnae3_knic_private_info *kinfo = &vport->nic.kinfo;
10208 	struct hclge_dev *hdev = vport->back;
10209 	u16 cur_rss_size = kinfo->rss_size;
10210 	u16 cur_tqps = kinfo->num_tqps;
10211 	u32 *rss_indir;
10212 	unsigned int i;
10213 	int ret;
10214 
10215 	kinfo->req_rss_size = new_tqps_num;
10216 
10217 	ret = hclge_tm_vport_map_update(hdev);
10218 	if (ret) {
10219 		dev_err(&hdev->pdev->dev, "tm vport map fail, ret =%d\n", ret);
10220 		return ret;
10221 	}
10222 
10223 	ret = hclge_set_rss_tc_mode_cfg(handle);
10224 	if (ret)
10225 		return ret;
10226 
10227 	/* RSS indirection table has been configured by user */
10228 	if (rxfh_configured)
10229 		goto out;
10230 
10231 	/* Reinitializes the rss indirect table according to the new RSS size */
10232 	rss_indir = kzalloc_objs(u32, ae_dev->dev_specs.rss_ind_tbl_size);
10233 	if (!rss_indir)
10234 		return -ENOMEM;
10235 
10236 	for (i = 0; i < ae_dev->dev_specs.rss_ind_tbl_size; i++)
10237 		rss_indir[i] = i % kinfo->rss_size;
10238 
10239 	ret = hclge_set_rss(handle, rss_indir, NULL, 0);
10240 	if (ret)
10241 		dev_err(&hdev->pdev->dev, "set rss indir table fail, ret=%d\n",
10242 			ret);
10243 
10244 	kfree(rss_indir);
10245 
10246 out:
10247 	if (!ret)
10248 		dev_info(&hdev->pdev->dev,
10249 			 "Channels changed, rss_size from %u to %u, tqps from %u to %u",
10250 			 cur_rss_size, kinfo->rss_size,
10251 			 cur_tqps, kinfo->rss_size * kinfo->tc_info.num_tc);
10252 
10253 	return ret;
10254 }
10255 
10256 static int hclge_set_led_status(struct hclge_dev *hdev, u8 locate_led_status)
10257 {
10258 	struct hclge_set_led_state_cmd *req;
10259 	struct hclge_desc desc;
10260 	int ret;
10261 
10262 	hclge_cmd_setup_basic_desc(&desc, HCLGE_OPC_LED_STATUS_CFG, false);
10263 
10264 	req = (struct hclge_set_led_state_cmd *)desc.data;
10265 	hnae3_set_field(req->locate_led_config, HCLGE_LED_LOCATE_STATE_M,
10266 			HCLGE_LED_LOCATE_STATE_S, locate_led_status);
10267 
10268 	ret = hclge_cmd_send(&hdev->hw, &desc, 1);
10269 	if (ret)
10270 		dev_err(&hdev->pdev->dev,
10271 			"Send set led state cmd error, ret =%d\n", ret);
10272 
10273 	return ret;
10274 }
10275 
10276 enum hclge_led_status {
10277 	HCLGE_LED_OFF,
10278 	HCLGE_LED_ON,
10279 	HCLGE_LED_NO_CHANGE = 0xFF,
10280 };
10281 
10282 static int hclge_set_led_id(struct hnae3_handle *handle,
10283 			    enum ethtool_phys_id_state status)
10284 {
10285 	struct hclge_vport *vport = hclge_get_vport(handle);
10286 	struct hclge_dev *hdev = vport->back;
10287 
10288 	switch (status) {
10289 	case ETHTOOL_ID_ACTIVE:
10290 		return hclge_set_led_status(hdev, HCLGE_LED_ON);
10291 	case ETHTOOL_ID_INACTIVE:
10292 		return hclge_set_led_status(hdev, HCLGE_LED_OFF);
10293 	default:
10294 		return -EINVAL;
10295 	}
10296 }
10297 
10298 static void hclge_get_link_mode(struct hnae3_handle *handle,
10299 				unsigned long *supported,
10300 				unsigned long *advertising)
10301 {
10302 	unsigned int size = BITS_TO_LONGS(__ETHTOOL_LINK_MODE_MASK_NBITS);
10303 	struct hclge_vport *vport = hclge_get_vport(handle);
10304 	struct hclge_dev *hdev = vport->back;
10305 	unsigned int idx = 0;
10306 
10307 	for (; idx < size; idx++) {
10308 		supported[idx] = hdev->hw.mac.supported[idx];
10309 		advertising[idx] = hdev->hw.mac.advertising[idx];
10310 	}
10311 }
10312 
10313 static int hclge_gro_en(struct hnae3_handle *handle, bool enable)
10314 {
10315 	struct hclge_vport *vport = hclge_get_vport(handle);
10316 	struct hclge_dev *hdev = vport->back;
10317 	bool gro_en_old = hdev->gro_en;
10318 	int ret;
10319 
10320 	hdev->gro_en = enable;
10321 	ret = hclge_config_gro(hdev);
10322 	if (ret)
10323 		hdev->gro_en = gro_en_old;
10324 
10325 	return ret;
10326 }
10327 
10328 static int hclge_sync_vport_promisc_mode(struct hclge_vport *vport)
10329 {
10330 	struct hnae3_handle *handle = &vport->nic;
10331 	struct hclge_dev *hdev = vport->back;
10332 	bool uc_en = false;
10333 	bool mc_en = false;
10334 	u8 tmp_flags;
10335 	bool bc_en;
10336 	int ret;
10337 
10338 	if (vport->last_promisc_flags != vport->overflow_promisc_flags) {
10339 		set_bit(HCLGE_VPORT_STATE_PROMISC_CHANGE, &vport->state);
10340 		vport->last_promisc_flags = vport->overflow_promisc_flags;
10341 	}
10342 
10343 	if (!test_and_clear_bit(HCLGE_VPORT_STATE_PROMISC_CHANGE,
10344 				&vport->state))
10345 		return 0;
10346 
10347 	/* for PF */
10348 	if (!vport->vport_id) {
10349 		tmp_flags = handle->netdev_flags | vport->last_promisc_flags;
10350 		ret = hclge_set_promisc_mode(handle, tmp_flags & HNAE3_UPE,
10351 					     tmp_flags & HNAE3_MPE);
10352 		if (!ret)
10353 			set_bit(HCLGE_VPORT_STATE_VLAN_FLTR_CHANGE,
10354 				&vport->state);
10355 		else
10356 			set_bit(HCLGE_VPORT_STATE_PROMISC_CHANGE,
10357 				&vport->state);
10358 		return ret;
10359 	}
10360 
10361 	/* for VF */
10362 	if (vport->vf_info.trusted) {
10363 		uc_en = vport->vf_info.request_uc_en > 0 ||
10364 			vport->overflow_promisc_flags & HNAE3_OVERFLOW_UPE;
10365 		mc_en = vport->vf_info.request_mc_en > 0 ||
10366 			vport->overflow_promisc_flags & HNAE3_OVERFLOW_MPE;
10367 	}
10368 	bc_en = vport->vf_info.request_bc_en > 0;
10369 
10370 	ret = hclge_cmd_set_promisc_mode(hdev, vport->vport_id, uc_en,
10371 					 mc_en, bc_en);
10372 	if (ret) {
10373 		set_bit(HCLGE_VPORT_STATE_PROMISC_CHANGE, &vport->state);
10374 		return ret;
10375 	}
10376 	hclge_set_vport_vlan_fltr_change(vport);
10377 
10378 	return 0;
10379 }
10380 
10381 static void hclge_sync_promisc_mode(struct hclge_dev *hdev)
10382 {
10383 	struct hclge_vport *vport;
10384 	int ret;
10385 	u16 i;
10386 
10387 	for (i = 0; i < hdev->num_alloc_vport; i++) {
10388 		vport = &hdev->vport[i];
10389 
10390 		ret = hclge_sync_vport_promisc_mode(vport);
10391 		if (ret)
10392 			return;
10393 	}
10394 }
10395 
10396 static bool hclge_module_existed(struct hclge_dev *hdev)
10397 {
10398 	struct hclge_desc desc;
10399 	u32 existed;
10400 	int ret;
10401 
10402 	hclge_cmd_setup_basic_desc(&desc, HCLGE_OPC_GET_SFP_EXIST, true);
10403 	ret = hclge_cmd_send(&hdev->hw, &desc, 1);
10404 	if (ret) {
10405 		dev_err(&hdev->pdev->dev,
10406 			"failed to get SFP exist state, ret = %d\n", ret);
10407 		return false;
10408 	}
10409 
10410 	existed = le32_to_cpu(desc.data[0]);
10411 
10412 	return existed != 0;
10413 }
10414 
10415 /* need 6 bds(total 140 bytes) in one reading
10416  * return the number of bytes actually read, 0 means read failed.
10417  */
10418 static u16 hclge_get_sfp_eeprom_info(struct hclge_dev *hdev, u32 offset,
10419 				     u32 len, u8 *data)
10420 {
10421 	struct hclge_desc desc[HCLGE_SFP_INFO_CMD_NUM];
10422 	struct hclge_sfp_info_bd0_cmd *sfp_info_bd0;
10423 	u16 read_len;
10424 	u16 copy_len;
10425 	int ret;
10426 	int i;
10427 
10428 	/* setup all 6 bds to read module eeprom info. */
10429 	for (i = 0; i < HCLGE_SFP_INFO_CMD_NUM; i++) {
10430 		hclge_cmd_setup_basic_desc(&desc[i], HCLGE_OPC_GET_SFP_EEPROM,
10431 					   true);
10432 
10433 		/* bd0~bd4 need next flag */
10434 		if (i < HCLGE_SFP_INFO_CMD_NUM - 1)
10435 			desc[i].flag |= cpu_to_le16(HCLGE_COMM_CMD_FLAG_NEXT);
10436 	}
10437 
10438 	/* setup bd0, this bd contains offset and read length. */
10439 	sfp_info_bd0 = (struct hclge_sfp_info_bd0_cmd *)desc[0].data;
10440 	sfp_info_bd0->offset = cpu_to_le16((u16)offset);
10441 	read_len = min_t(u16, len, HCLGE_SFP_INFO_MAX_LEN);
10442 	sfp_info_bd0->read_len = cpu_to_le16(read_len);
10443 
10444 	ret = hclge_cmd_send(&hdev->hw, desc, i);
10445 	if (ret) {
10446 		dev_err(&hdev->pdev->dev,
10447 			"failed to get SFP eeprom info, ret = %d\n", ret);
10448 		return 0;
10449 	}
10450 
10451 	/* copy sfp info from bd0 to out buffer. */
10452 	copy_len = min_t(u16, len, HCLGE_SFP_INFO_BD0_LEN);
10453 	memcpy(data, sfp_info_bd0->data, copy_len);
10454 	read_len = copy_len;
10455 
10456 	/* copy sfp info from bd1~bd5 to out buffer if needed. */
10457 	for (i = 1; i < HCLGE_SFP_INFO_CMD_NUM; i++) {
10458 		if (read_len >= len)
10459 			return read_len;
10460 
10461 		copy_len = min_t(u16, len - read_len, HCLGE_SFP_INFO_BDX_LEN);
10462 		memcpy(data + read_len, desc[i].data, copy_len);
10463 		read_len += copy_len;
10464 	}
10465 
10466 	return read_len;
10467 }
10468 
10469 static int hclge_get_module_eeprom(struct hnae3_handle *handle, u32 offset,
10470 				   u32 len, u8 *data)
10471 {
10472 	struct hclge_vport *vport = hclge_get_vport(handle);
10473 	struct hclge_dev *hdev = vport->back;
10474 	u32 read_len = 0;
10475 	u16 data_len;
10476 
10477 	if (hdev->hw.mac.media_type != HNAE3_MEDIA_TYPE_FIBER)
10478 		return -EOPNOTSUPP;
10479 
10480 	if (!hclge_module_existed(hdev))
10481 		return -ENXIO;
10482 
10483 	while (read_len < len) {
10484 		data_len = hclge_get_sfp_eeprom_info(hdev,
10485 						     offset + read_len,
10486 						     len - read_len,
10487 						     data + read_len);
10488 		if (!data_len)
10489 			return -EIO;
10490 
10491 		read_len += data_len;
10492 	}
10493 
10494 	return 0;
10495 }
10496 
10497 static int hclge_get_link_diagnosis_info(struct hnae3_handle *handle,
10498 					 u32 *status_code)
10499 {
10500 	struct hclge_vport *vport = hclge_get_vport(handle);
10501 	struct hclge_dev *hdev = vport->back;
10502 	struct hclge_desc desc;
10503 	int ret;
10504 
10505 	if (hdev->ae_dev->dev_version <= HNAE3_DEVICE_VERSION_V2)
10506 		return -EOPNOTSUPP;
10507 
10508 	hclge_cmd_setup_basic_desc(&desc, HCLGE_OPC_QUERY_LINK_DIAGNOSIS, true);
10509 	ret = hclge_cmd_send(&hdev->hw, &desc, 1);
10510 	if (ret) {
10511 		dev_err(&hdev->pdev->dev,
10512 			"failed to query link diagnosis info, ret = %d\n", ret);
10513 		return ret;
10514 	}
10515 
10516 	*status_code = le32_to_cpu(desc.data[0]);
10517 	return 0;
10518 }
10519 
10520 /* After disable sriov, VF still has some config and info need clean,
10521  * which configured by PF.
10522  */
10523 static void hclge_clear_vport_vf_info(struct hclge_vport *vport, int vfid)
10524 {
10525 	struct hclge_dev *hdev = vport->back;
10526 	struct hclge_vlan_info vlan_info;
10527 	int ret;
10528 
10529 	clear_bit(HCLGE_VPORT_STATE_INITED, &vport->state);
10530 	clear_bit(HCLGE_VPORT_STATE_ALIVE, &vport->state);
10531 	vport->need_notify = 0;
10532 	vport->mps = 0;
10533 
10534 	/* after disable sriov, clean VF rate configured by PF */
10535 	ret = hclge_tm_qs_shaper_cfg(vport, 0);
10536 	if (ret)
10537 		dev_err(&hdev->pdev->dev,
10538 			"failed to clean vf%d rate config, ret = %d\n",
10539 			vfid, ret);
10540 
10541 	vlan_info.vlan_tag = 0;
10542 	vlan_info.qos = 0;
10543 	vlan_info.vlan_proto = ETH_P_8021Q;
10544 	ret = hclge_update_port_base_vlan_cfg(vport,
10545 					      HNAE3_PORT_BASE_VLAN_DISABLE,
10546 					      &vlan_info);
10547 	if (ret)
10548 		dev_err(&hdev->pdev->dev,
10549 			"failed to clean vf%d port base vlan, ret = %d\n",
10550 			vfid, ret);
10551 
10552 	ret = hclge_set_vf_spoofchk_hw(hdev, vport->vport_id, false);
10553 	if (ret)
10554 		dev_err(&hdev->pdev->dev,
10555 			"failed to clean vf%d spoof config, ret = %d\n",
10556 			vfid, ret);
10557 
10558 	memset(&vport->vf_info, 0, sizeof(vport->vf_info));
10559 }
10560 
10561 static void hclge_clean_vport_config(struct hnae3_ae_dev *ae_dev, int num_vfs)
10562 {
10563 	struct hclge_dev *hdev = ae_dev->priv;
10564 	struct hclge_vport *vport;
10565 	int i;
10566 
10567 	for (i = 0; i < num_vfs; i++) {
10568 		vport = &hdev->vport[i + HCLGE_VF_VPORT_START_NUM];
10569 
10570 		hclge_clear_vport_vf_info(vport, i);
10571 	}
10572 }
10573 
10574 static int hclge_get_dscp_prio(struct hnae3_handle *h, u8 dscp, u8 *tc_mode,
10575 			       u8 *priority)
10576 {
10577 	struct hclge_vport *vport = hclge_get_vport(h);
10578 
10579 	if (dscp >= HNAE3_MAX_DSCP)
10580 		return -EINVAL;
10581 
10582 	if (tc_mode)
10583 		*tc_mode = vport->nic.kinfo.tc_map_mode;
10584 	if (priority)
10585 		*priority = vport->nic.kinfo.dscp_prio[dscp] == HNAE3_PRIO_ID_INVALID ? 0 :
10586 			    vport->nic.kinfo.dscp_prio[dscp];
10587 
10588 	return 0;
10589 }
10590 
10591 static const struct hnae3_ae_ops hclge_ops = {
10592 	.init_ae_dev = hclge_init_ae_dev,
10593 	.uninit_ae_dev = hclge_uninit_ae_dev,
10594 	.reset_prepare = hclge_reset_prepare_general,
10595 	.reset_done = hclge_reset_done,
10596 	.init_client_instance = hclge_init_client_instance,
10597 	.uninit_client_instance = hclge_uninit_client_instance,
10598 	.map_ring_to_vector = hclge_map_ring_to_vector,
10599 	.unmap_ring_from_vector = hclge_unmap_ring_frm_vector,
10600 	.get_vector = hclge_get_vector,
10601 	.put_vector = hclge_put_vector,
10602 	.set_promisc_mode = hclge_set_promisc_mode,
10603 	.request_update_promisc_mode = hclge_request_update_promisc_mode,
10604 	.set_loopback = hclge_set_loopback,
10605 	.start = hclge_ae_start,
10606 	.stop = hclge_ae_stop,
10607 	.client_start = hclge_client_start,
10608 	.client_stop = hclge_client_stop,
10609 	.get_status = hclge_get_status,
10610 	.get_ksettings_an_result = hclge_get_ksettings_an_result,
10611 	.cfg_mac_speed_dup_h = hclge_cfg_mac_speed_dup_h,
10612 	.get_media_type = hclge_get_media_type,
10613 	.check_port_speed = hclge_check_port_speed,
10614 	.get_fec_stats = hclge_get_fec_stats,
10615 	.get_fec = hclge_get_fec,
10616 	.set_fec = hclge_set_fec,
10617 	.get_rss_key_size = hclge_comm_get_rss_key_size,
10618 	.get_rss = hclge_get_rss,
10619 	.set_rss = hclge_set_rss,
10620 	.set_rss_tuple = hclge_set_rss_tuple,
10621 	.get_rss_tuple = hclge_get_rss_tuple,
10622 	.get_tc_size = hclge_get_tc_size,
10623 	.get_mac_addr = hclge_get_mac_addr,
10624 	.set_mac_addr = hclge_set_mac_addr,
10625 	.do_ioctl = hclge_do_ioctl,
10626 	.add_uc_addr = hclge_add_uc_addr,
10627 	.rm_uc_addr = hclge_rm_uc_addr,
10628 	.add_mc_addr = hclge_add_mc_addr,
10629 	.rm_mc_addr = hclge_rm_mc_addr,
10630 	.set_autoneg = hclge_set_autoneg,
10631 	.get_autoneg = hclge_get_autoneg,
10632 	.restart_autoneg = hclge_restart_autoneg,
10633 	.halt_autoneg = hclge_halt_autoneg,
10634 	.get_pauseparam = hclge_get_pauseparam,
10635 	.set_pauseparam = hclge_set_pauseparam,
10636 	.set_mtu = hclge_set_mtu,
10637 	.reset_queue = hclge_reset_tqp,
10638 	.get_stats = hclge_get_stats,
10639 	.get_mac_stats = hclge_get_mac_stat,
10640 	.update_stats = hclge_update_stats,
10641 	.get_strings = hclge_get_strings,
10642 	.get_sset_count = hclge_get_sset_count,
10643 	.get_fw_version = hclge_get_fw_version,
10644 	.get_mdix_mode = hclge_get_mdix_mode,
10645 	.enable_vlan_filter = hclge_enable_vlan_filter,
10646 	.set_vlan_filter = hclge_set_vlan_filter,
10647 	.set_vf_vlan_filter = hclge_set_vf_vlan_filter,
10648 	.enable_hw_strip_rxvtag = hclge_en_hw_strip_rxvtag,
10649 	.reset_event = hclge_reset_event,
10650 	.get_reset_level = hclge_get_reset_level,
10651 	.set_default_reset_request = hclge_set_def_reset_request,
10652 	.get_tqps_and_rss_info = hclge_get_tqps_and_rss_info,
10653 	.set_channels = hclge_set_channels,
10654 	.get_channels = hclge_get_channels,
10655 	.get_regs_len = hclge_get_regs_len,
10656 	.get_regs = hclge_get_regs,
10657 	.set_led_id = hclge_set_led_id,
10658 	.get_link_mode = hclge_get_link_mode,
10659 	.add_fd_entry = hclge_add_fd_entry,
10660 	.del_fd_entry = hclge_del_fd_entry,
10661 	.get_fd_rule_cnt = hclge_get_fd_rule_cnt,
10662 	.get_fd_rule_info = hclge_get_fd_rule_info,
10663 	.get_fd_all_rules = hclge_get_all_rules,
10664 	.enable_fd = hclge_enable_fd,
10665 	.add_arfs_entry = hclge_add_fd_entry_by_arfs,
10666 	.dbg_get_read_func = hclge_dbg_get_read_func,
10667 	.handle_hw_ras_error = hclge_handle_hw_ras_error,
10668 	.get_hw_reset_stat = hclge_get_hw_reset_stat,
10669 	.ae_dev_resetting = hclge_ae_dev_resetting,
10670 	.ae_dev_reset_cnt = hclge_ae_dev_reset_cnt,
10671 	.set_gro_en = hclge_gro_en,
10672 	.get_global_queue_id = hclge_covert_handle_qid_global,
10673 	.set_timer_task = hclge_set_timer_task,
10674 	.mac_connect_phy = hclge_mac_connect_phy,
10675 	.mac_disconnect_phy = hclge_mac_disconnect_phy,
10676 	.get_vf_config = hclge_get_vf_config,
10677 	.set_vf_link_state = hclge_set_vf_link_state,
10678 	.set_vf_spoofchk = hclge_set_vf_spoofchk,
10679 	.set_vf_trust = hclge_set_vf_trust,
10680 	.set_vf_rate = hclge_set_vf_rate,
10681 	.set_vf_mac = hclge_set_vf_mac,
10682 	.get_module_eeprom = hclge_get_module_eeprom,
10683 	.get_cmdq_stat = hclge_get_cmdq_stat,
10684 	.add_cls_flower = hclge_add_cls_flower,
10685 	.del_cls_flower = hclge_del_cls_flower,
10686 	.cls_flower_active = hclge_is_cls_flower_active,
10687 	.get_phy_link_ksettings = hclge_get_phy_link_ksettings,
10688 	.set_phy_link_ksettings = hclge_set_phy_link_ksettings,
10689 	.set_tx_hwts_info = hclge_ptp_set_tx_info,
10690 	.get_rx_hwts = hclge_ptp_get_rx_hwts,
10691 	.get_ts_info = hclge_ptp_get_ts_info,
10692 	.get_link_diagnosis_info = hclge_get_link_diagnosis_info,
10693 	.clean_vf_config = hclge_clean_vport_config,
10694 	.get_dscp_prio = hclge_get_dscp_prio,
10695 	.get_wol = hclge_get_wol,
10696 	.set_wol = hclge_set_wol,
10697 	.hwtstamp_get = hclge_ptp_get_cfg,
10698 	.hwtstamp_set = hclge_ptp_set_cfg,
10699 	.set_pfc_prevention_tout = hclge_set_pfc_prevention_tout,
10700 	.get_pfc_prevention_tout = hclge_get_pfc_prevention_tout,
10701 };
10702 
10703 static struct hnae3_ae_algo ae_algo = {
10704 	.ops = &hclge_ops,
10705 	.pdev_id_table = ae_algo_pci_tbl,
10706 };
10707 
10708 static int __init hclge_init(void)
10709 {
10710 	pr_debug("%s is initializing\n", HCLGE_NAME);
10711 
10712 	hclge_wq = alloc_workqueue("%s", WQ_UNBOUND, 0,
10713 				   HCLGE_NAME);
10714 	if (!hclge_wq) {
10715 		pr_err("%s: failed to create workqueue\n", HCLGE_NAME);
10716 		return -ENOMEM;
10717 	}
10718 
10719 	hnae3_register_ae_algo(&ae_algo);
10720 
10721 	return 0;
10722 }
10723 
10724 static void __exit hclge_exit(void)
10725 {
10726 	hnae3_acquire_unload_lock();
10727 	hnae3_unregister_ae_algo_prepare(&ae_algo);
10728 	hnae3_unregister_ae_algo(&ae_algo);
10729 	destroy_workqueue(hclge_wq);
10730 	hnae3_release_unload_lock();
10731 }
10732 module_init(hclge_init);
10733 module_exit(hclge_exit);
10734 
10735 MODULE_LICENSE("GPL");
10736 MODULE_AUTHOR("Huawei Tech. Co., Ltd.");
10737 MODULE_DESCRIPTION("HCLGE Driver");
10738 MODULE_VERSION(HCLGE_MOD_VERSION);
10739