1 // SPDX-License-Identifier: GPL-2.0+
2 // Copyright (c) 2016-2017 Hisilicon Limited.
3 
4 #include <linux/etherdevice.h>
5 #include <linux/iopoll.h>
6 #include <net/rtnetlink.h>
7 #include "hclgevf_cmd.h"
8 #include "hclgevf_main.h"
9 #include "hclge_mbx.h"
10 #include "hnae3.h"
11 
12 #define HCLGEVF_NAME	"hclgevf"
13 
14 #define HCLGEVF_RESET_MAX_FAIL_CNT	5
15 
16 static int hclgevf_reset_hdev(struct hclgevf_dev *hdev);
17 static struct hnae3_ae_algo ae_algovf;
18 
19 static struct workqueue_struct *hclgevf_wq;
20 
21 static const struct pci_device_id ae_algovf_pci_tbl[] = {
22 	{PCI_VDEVICE(HUAWEI, HNAE3_DEV_ID_100G_VF), 0},
23 	{PCI_VDEVICE(HUAWEI, HNAE3_DEV_ID_100G_RDMA_DCB_PFC_VF), 0},
24 	/* required last entry */
25 	{0, }
26 };
27 
28 static const u8 hclgevf_hash_key[] = {
29 	0x6D, 0x5A, 0x56, 0xDA, 0x25, 0x5B, 0x0E, 0xC2,
30 	0x41, 0x67, 0x25, 0x3D, 0x43, 0xA3, 0x8F, 0xB0,
31 	0xD0, 0xCA, 0x2B, 0xCB, 0xAE, 0x7B, 0x30, 0xB4,
32 	0x77, 0xCB, 0x2D, 0xA3, 0x80, 0x30, 0xF2, 0x0C,
33 	0x6A, 0x42, 0xB7, 0x3B, 0xBE, 0xAC, 0x01, 0xFA
34 };
35 
36 MODULE_DEVICE_TABLE(pci, ae_algovf_pci_tbl);
37 
38 static const u32 cmdq_reg_addr_list[] = {HCLGEVF_CMDQ_TX_ADDR_L_REG,
39 					 HCLGEVF_CMDQ_TX_ADDR_H_REG,
40 					 HCLGEVF_CMDQ_TX_DEPTH_REG,
41 					 HCLGEVF_CMDQ_TX_TAIL_REG,
42 					 HCLGEVF_CMDQ_TX_HEAD_REG,
43 					 HCLGEVF_CMDQ_RX_ADDR_L_REG,
44 					 HCLGEVF_CMDQ_RX_ADDR_H_REG,
45 					 HCLGEVF_CMDQ_RX_DEPTH_REG,
46 					 HCLGEVF_CMDQ_RX_TAIL_REG,
47 					 HCLGEVF_CMDQ_RX_HEAD_REG,
48 					 HCLGEVF_VECTOR0_CMDQ_SRC_REG,
49 					 HCLGEVF_VECTOR0_CMDQ_STATE_REG,
50 					 HCLGEVF_CMDQ_INTR_EN_REG,
51 					 HCLGEVF_CMDQ_INTR_GEN_REG};
52 
53 static const u32 common_reg_addr_list[] = {HCLGEVF_MISC_VECTOR_REG_BASE,
54 					   HCLGEVF_RST_ING,
55 					   HCLGEVF_GRO_EN_REG};
56 
57 static const u32 ring_reg_addr_list[] = {HCLGEVF_RING_RX_ADDR_L_REG,
58 					 HCLGEVF_RING_RX_ADDR_H_REG,
59 					 HCLGEVF_RING_RX_BD_NUM_REG,
60 					 HCLGEVF_RING_RX_BD_LENGTH_REG,
61 					 HCLGEVF_RING_RX_MERGE_EN_REG,
62 					 HCLGEVF_RING_RX_TAIL_REG,
63 					 HCLGEVF_RING_RX_HEAD_REG,
64 					 HCLGEVF_RING_RX_FBD_NUM_REG,
65 					 HCLGEVF_RING_RX_OFFSET_REG,
66 					 HCLGEVF_RING_RX_FBD_OFFSET_REG,
67 					 HCLGEVF_RING_RX_STASH_REG,
68 					 HCLGEVF_RING_RX_BD_ERR_REG,
69 					 HCLGEVF_RING_TX_ADDR_L_REG,
70 					 HCLGEVF_RING_TX_ADDR_H_REG,
71 					 HCLGEVF_RING_TX_BD_NUM_REG,
72 					 HCLGEVF_RING_TX_PRIORITY_REG,
73 					 HCLGEVF_RING_TX_TC_REG,
74 					 HCLGEVF_RING_TX_MERGE_EN_REG,
75 					 HCLGEVF_RING_TX_TAIL_REG,
76 					 HCLGEVF_RING_TX_HEAD_REG,
77 					 HCLGEVF_RING_TX_FBD_NUM_REG,
78 					 HCLGEVF_RING_TX_OFFSET_REG,
79 					 HCLGEVF_RING_TX_EBD_NUM_REG,
80 					 HCLGEVF_RING_TX_EBD_OFFSET_REG,
81 					 HCLGEVF_RING_TX_BD_ERR_REG,
82 					 HCLGEVF_RING_EN_REG};
83 
84 static const u32 tqp_intr_reg_addr_list[] = {HCLGEVF_TQP_INTR_CTRL_REG,
85 					     HCLGEVF_TQP_INTR_GL0_REG,
86 					     HCLGEVF_TQP_INTR_GL1_REG,
87 					     HCLGEVF_TQP_INTR_GL2_REG,
88 					     HCLGEVF_TQP_INTR_RL_REG};
89 
90 static struct hclgevf_dev *hclgevf_ae_get_hdev(struct hnae3_handle *handle)
91 {
92 	if (!handle->client)
93 		return container_of(handle, struct hclgevf_dev, nic);
94 	else if (handle->client->type == HNAE3_CLIENT_ROCE)
95 		return container_of(handle, struct hclgevf_dev, roce);
96 	else
97 		return container_of(handle, struct hclgevf_dev, nic);
98 }
99 
100 static int hclgevf_tqps_update_stats(struct hnae3_handle *handle)
101 {
102 	struct hnae3_knic_private_info *kinfo = &handle->kinfo;
103 	struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);
104 	struct hclgevf_desc desc;
105 	struct hclgevf_tqp *tqp;
106 	int status;
107 	int i;
108 
109 	for (i = 0; i < kinfo->num_tqps; i++) {
110 		tqp = container_of(kinfo->tqp[i], struct hclgevf_tqp, q);
111 		hclgevf_cmd_setup_basic_desc(&desc,
112 					     HCLGEVF_OPC_QUERY_RX_STATUS,
113 					     true);
114 
115 		desc.data[0] = cpu_to_le32(tqp->index & 0x1ff);
116 		status = hclgevf_cmd_send(&hdev->hw, &desc, 1);
117 		if (status) {
118 			dev_err(&hdev->pdev->dev,
119 				"Query tqp stat fail, status = %d,queue = %d\n",
120 				status,	i);
121 			return status;
122 		}
123 		tqp->tqp_stats.rcb_rx_ring_pktnum_rcd +=
124 			le32_to_cpu(desc.data[1]);
125 
126 		hclgevf_cmd_setup_basic_desc(&desc, HCLGEVF_OPC_QUERY_TX_STATUS,
127 					     true);
128 
129 		desc.data[0] = cpu_to_le32(tqp->index & 0x1ff);
130 		status = hclgevf_cmd_send(&hdev->hw, &desc, 1);
131 		if (status) {
132 			dev_err(&hdev->pdev->dev,
133 				"Query tqp stat fail, status = %d,queue = %d\n",
134 				status, i);
135 			return status;
136 		}
137 		tqp->tqp_stats.rcb_tx_ring_pktnum_rcd +=
138 			le32_to_cpu(desc.data[1]);
139 	}
140 
141 	return 0;
142 }
143 
144 static u64 *hclgevf_tqps_get_stats(struct hnae3_handle *handle, u64 *data)
145 {
146 	struct hnae3_knic_private_info *kinfo = &handle->kinfo;
147 	struct hclgevf_tqp *tqp;
148 	u64 *buff = data;
149 	int i;
150 
151 	for (i = 0; i < kinfo->num_tqps; i++) {
152 		tqp = container_of(kinfo->tqp[i], struct hclgevf_tqp, q);
153 		*buff++ = tqp->tqp_stats.rcb_tx_ring_pktnum_rcd;
154 	}
155 	for (i = 0; i < kinfo->num_tqps; i++) {
156 		tqp = container_of(kinfo->tqp[i], struct hclgevf_tqp, q);
157 		*buff++ = tqp->tqp_stats.rcb_rx_ring_pktnum_rcd;
158 	}
159 
160 	return buff;
161 }
162 
163 static int hclgevf_tqps_get_sset_count(struct hnae3_handle *handle, int strset)
164 {
165 	struct hnae3_knic_private_info *kinfo = &handle->kinfo;
166 
167 	return kinfo->num_tqps * 2;
168 }
169 
170 static u8 *hclgevf_tqps_get_strings(struct hnae3_handle *handle, u8 *data)
171 {
172 	struct hnae3_knic_private_info *kinfo = &handle->kinfo;
173 	u8 *buff = data;
174 	int i = 0;
175 
176 	for (i = 0; i < kinfo->num_tqps; i++) {
177 		struct hclgevf_tqp *tqp = container_of(kinfo->tqp[i],
178 						       struct hclgevf_tqp, q);
179 		snprintf(buff, ETH_GSTRING_LEN, "txq%d_pktnum_rcd",
180 			 tqp->index);
181 		buff += ETH_GSTRING_LEN;
182 	}
183 
184 	for (i = 0; i < kinfo->num_tqps; i++) {
185 		struct hclgevf_tqp *tqp = container_of(kinfo->tqp[i],
186 						       struct hclgevf_tqp, q);
187 		snprintf(buff, ETH_GSTRING_LEN, "rxq%d_pktnum_rcd",
188 			 tqp->index);
189 		buff += ETH_GSTRING_LEN;
190 	}
191 
192 	return buff;
193 }
194 
195 static void hclgevf_update_stats(struct hnae3_handle *handle,
196 				 struct net_device_stats *net_stats)
197 {
198 	struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);
199 	int status;
200 
201 	status = hclgevf_tqps_update_stats(handle);
202 	if (status)
203 		dev_err(&hdev->pdev->dev,
204 			"VF update of TQPS stats fail, status = %d.\n",
205 			status);
206 }
207 
208 static int hclgevf_get_sset_count(struct hnae3_handle *handle, int strset)
209 {
210 	if (strset == ETH_SS_TEST)
211 		return -EOPNOTSUPP;
212 	else if (strset == ETH_SS_STATS)
213 		return hclgevf_tqps_get_sset_count(handle, strset);
214 
215 	return 0;
216 }
217 
218 static void hclgevf_get_strings(struct hnae3_handle *handle, u32 strset,
219 				u8 *data)
220 {
221 	u8 *p = (char *)data;
222 
223 	if (strset == ETH_SS_STATS)
224 		p = hclgevf_tqps_get_strings(handle, p);
225 }
226 
227 static void hclgevf_get_stats(struct hnae3_handle *handle, u64 *data)
228 {
229 	hclgevf_tqps_get_stats(handle, data);
230 }
231 
232 static void hclgevf_build_send_msg(struct hclge_vf_to_pf_msg *msg, u8 code,
233 				   u8 subcode)
234 {
235 	if (msg) {
236 		memset(msg, 0, sizeof(struct hclge_vf_to_pf_msg));
237 		msg->code = code;
238 		msg->subcode = subcode;
239 	}
240 }
241 
242 static int hclgevf_get_tc_info(struct hclgevf_dev *hdev)
243 {
244 	struct hclge_vf_to_pf_msg send_msg;
245 	u8 resp_msg;
246 	int status;
247 
248 	hclgevf_build_send_msg(&send_msg, HCLGE_MBX_GET_TCINFO, 0);
249 	status = hclgevf_send_mbx_msg(hdev, &send_msg, true, &resp_msg,
250 				      sizeof(resp_msg));
251 	if (status) {
252 		dev_err(&hdev->pdev->dev,
253 			"VF request to get TC info from PF failed %d",
254 			status);
255 		return status;
256 	}
257 
258 	hdev->hw_tc_map = resp_msg;
259 
260 	return 0;
261 }
262 
263 static int hclgevf_get_port_base_vlan_filter_state(struct hclgevf_dev *hdev)
264 {
265 	struct hnae3_handle *nic = &hdev->nic;
266 	struct hclge_vf_to_pf_msg send_msg;
267 	u8 resp_msg;
268 	int ret;
269 
270 	hclgevf_build_send_msg(&send_msg, HCLGE_MBX_SET_VLAN,
271 			       HCLGE_MBX_GET_PORT_BASE_VLAN_STATE);
272 	ret = hclgevf_send_mbx_msg(hdev, &send_msg, true, &resp_msg,
273 				   sizeof(u8));
274 	if (ret) {
275 		dev_err(&hdev->pdev->dev,
276 			"VF request to get port based vlan state failed %d",
277 			ret);
278 		return ret;
279 	}
280 
281 	nic->port_base_vlan_state = resp_msg;
282 
283 	return 0;
284 }
285 
286 static int hclgevf_get_queue_info(struct hclgevf_dev *hdev)
287 {
288 #define HCLGEVF_TQPS_RSS_INFO_LEN	6
289 #define HCLGEVF_TQPS_ALLOC_OFFSET	0
290 #define HCLGEVF_TQPS_RSS_SIZE_OFFSET	2
291 #define HCLGEVF_TQPS_RX_BUFFER_LEN_OFFSET	4
292 
293 	u8 resp_msg[HCLGEVF_TQPS_RSS_INFO_LEN];
294 	struct hclge_vf_to_pf_msg send_msg;
295 	int status;
296 
297 	hclgevf_build_send_msg(&send_msg, HCLGE_MBX_GET_QINFO, 0);
298 	status = hclgevf_send_mbx_msg(hdev, &send_msg, true, resp_msg,
299 				      HCLGEVF_TQPS_RSS_INFO_LEN);
300 	if (status) {
301 		dev_err(&hdev->pdev->dev,
302 			"VF request to get tqp info from PF failed %d",
303 			status);
304 		return status;
305 	}
306 
307 	memcpy(&hdev->num_tqps, &resp_msg[HCLGEVF_TQPS_ALLOC_OFFSET],
308 	       sizeof(u16));
309 	memcpy(&hdev->rss_size_max, &resp_msg[HCLGEVF_TQPS_RSS_SIZE_OFFSET],
310 	       sizeof(u16));
311 	memcpy(&hdev->rx_buf_len, &resp_msg[HCLGEVF_TQPS_RX_BUFFER_LEN_OFFSET],
312 	       sizeof(u16));
313 
314 	return 0;
315 }
316 
317 static int hclgevf_get_queue_depth(struct hclgevf_dev *hdev)
318 {
319 #define HCLGEVF_TQPS_DEPTH_INFO_LEN	4
320 #define HCLGEVF_TQPS_NUM_TX_DESC_OFFSET	0
321 #define HCLGEVF_TQPS_NUM_RX_DESC_OFFSET	2
322 
323 	u8 resp_msg[HCLGEVF_TQPS_DEPTH_INFO_LEN];
324 	struct hclge_vf_to_pf_msg send_msg;
325 	int ret;
326 
327 	hclgevf_build_send_msg(&send_msg, HCLGE_MBX_GET_QDEPTH, 0);
328 	ret = hclgevf_send_mbx_msg(hdev, &send_msg, true, resp_msg,
329 				   HCLGEVF_TQPS_DEPTH_INFO_LEN);
330 	if (ret) {
331 		dev_err(&hdev->pdev->dev,
332 			"VF request to get tqp depth info from PF failed %d",
333 			ret);
334 		return ret;
335 	}
336 
337 	memcpy(&hdev->num_tx_desc, &resp_msg[HCLGEVF_TQPS_NUM_TX_DESC_OFFSET],
338 	       sizeof(u16));
339 	memcpy(&hdev->num_rx_desc, &resp_msg[HCLGEVF_TQPS_NUM_RX_DESC_OFFSET],
340 	       sizeof(u16));
341 
342 	return 0;
343 }
344 
345 static u16 hclgevf_get_qid_global(struct hnae3_handle *handle, u16 queue_id)
346 {
347 	struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);
348 	struct hclge_vf_to_pf_msg send_msg;
349 	u16 qid_in_pf = 0;
350 	u8 resp_data[2];
351 	int ret;
352 
353 	hclgevf_build_send_msg(&send_msg, HCLGE_MBX_GET_QID_IN_PF, 0);
354 	memcpy(send_msg.data, &queue_id, sizeof(queue_id));
355 	ret = hclgevf_send_mbx_msg(hdev, &send_msg, true, resp_data,
356 				   sizeof(resp_data));
357 	if (!ret)
358 		qid_in_pf = *(u16 *)resp_data;
359 
360 	return qid_in_pf;
361 }
362 
363 static int hclgevf_get_pf_media_type(struct hclgevf_dev *hdev)
364 {
365 	struct hclge_vf_to_pf_msg send_msg;
366 	u8 resp_msg[2];
367 	int ret;
368 
369 	hclgevf_build_send_msg(&send_msg, HCLGE_MBX_GET_MEDIA_TYPE, 0);
370 	ret = hclgevf_send_mbx_msg(hdev, &send_msg, true, resp_msg,
371 				   sizeof(resp_msg));
372 	if (ret) {
373 		dev_err(&hdev->pdev->dev,
374 			"VF request to get the pf port media type failed %d",
375 			ret);
376 		return ret;
377 	}
378 
379 	hdev->hw.mac.media_type = resp_msg[0];
380 	hdev->hw.mac.module_type = resp_msg[1];
381 
382 	return 0;
383 }
384 
385 static int hclgevf_alloc_tqps(struct hclgevf_dev *hdev)
386 {
387 	struct hclgevf_tqp *tqp;
388 	int i;
389 
390 	hdev->htqp = devm_kcalloc(&hdev->pdev->dev, hdev->num_tqps,
391 				  sizeof(struct hclgevf_tqp), GFP_KERNEL);
392 	if (!hdev->htqp)
393 		return -ENOMEM;
394 
395 	tqp = hdev->htqp;
396 
397 	for (i = 0; i < hdev->num_tqps; i++) {
398 		tqp->dev = &hdev->pdev->dev;
399 		tqp->index = i;
400 
401 		tqp->q.ae_algo = &ae_algovf;
402 		tqp->q.buf_size = hdev->rx_buf_len;
403 		tqp->q.tx_desc_num = hdev->num_tx_desc;
404 		tqp->q.rx_desc_num = hdev->num_rx_desc;
405 		tqp->q.io_base = hdev->hw.io_base + HCLGEVF_TQP_REG_OFFSET +
406 			i * HCLGEVF_TQP_REG_SIZE;
407 
408 		tqp++;
409 	}
410 
411 	return 0;
412 }
413 
414 static int hclgevf_knic_setup(struct hclgevf_dev *hdev)
415 {
416 	struct hnae3_handle *nic = &hdev->nic;
417 	struct hnae3_knic_private_info *kinfo;
418 	u16 new_tqps = hdev->num_tqps;
419 	unsigned int i;
420 
421 	kinfo = &nic->kinfo;
422 	kinfo->num_tc = 0;
423 	kinfo->num_tx_desc = hdev->num_tx_desc;
424 	kinfo->num_rx_desc = hdev->num_rx_desc;
425 	kinfo->rx_buf_len = hdev->rx_buf_len;
426 	for (i = 0; i < HCLGEVF_MAX_TC_NUM; i++)
427 		if (hdev->hw_tc_map & BIT(i))
428 			kinfo->num_tc++;
429 
430 	kinfo->rss_size
431 		= min_t(u16, hdev->rss_size_max, new_tqps / kinfo->num_tc);
432 	new_tqps = kinfo->rss_size * kinfo->num_tc;
433 	kinfo->num_tqps = min(new_tqps, hdev->num_tqps);
434 
435 	kinfo->tqp = devm_kcalloc(&hdev->pdev->dev, kinfo->num_tqps,
436 				  sizeof(struct hnae3_queue *), GFP_KERNEL);
437 	if (!kinfo->tqp)
438 		return -ENOMEM;
439 
440 	for (i = 0; i < kinfo->num_tqps; i++) {
441 		hdev->htqp[i].q.handle = &hdev->nic;
442 		hdev->htqp[i].q.tqp_index = i;
443 		kinfo->tqp[i] = &hdev->htqp[i].q;
444 	}
445 
446 	/* after init the max rss_size and tqps, adjust the default tqp numbers
447 	 * and rss size with the actual vector numbers
448 	 */
449 	kinfo->num_tqps = min_t(u16, hdev->num_nic_msix - 1, kinfo->num_tqps);
450 	kinfo->rss_size = min_t(u16, kinfo->num_tqps / kinfo->num_tc,
451 				kinfo->rss_size);
452 
453 	return 0;
454 }
455 
456 static void hclgevf_request_link_info(struct hclgevf_dev *hdev)
457 {
458 	struct hclge_vf_to_pf_msg send_msg;
459 	int status;
460 
461 	hclgevf_build_send_msg(&send_msg, HCLGE_MBX_GET_LINK_STATUS, 0);
462 	status = hclgevf_send_mbx_msg(hdev, &send_msg, false, NULL, 0);
463 	if (status)
464 		dev_err(&hdev->pdev->dev,
465 			"VF failed to fetch link status(%d) from PF", status);
466 }
467 
468 void hclgevf_update_link_status(struct hclgevf_dev *hdev, int link_state)
469 {
470 	struct hnae3_handle *rhandle = &hdev->roce;
471 	struct hnae3_handle *handle = &hdev->nic;
472 	struct hnae3_client *rclient;
473 	struct hnae3_client *client;
474 
475 	if (test_and_set_bit(HCLGEVF_STATE_LINK_UPDATING, &hdev->state))
476 		return;
477 
478 	client = handle->client;
479 	rclient = hdev->roce_client;
480 
481 	link_state =
482 		test_bit(HCLGEVF_STATE_DOWN, &hdev->state) ? 0 : link_state;
483 
484 	if (link_state != hdev->hw.mac.link) {
485 		client->ops->link_status_change(handle, !!link_state);
486 		if (rclient && rclient->ops->link_status_change)
487 			rclient->ops->link_status_change(rhandle, !!link_state);
488 		hdev->hw.mac.link = link_state;
489 	}
490 
491 	clear_bit(HCLGEVF_STATE_LINK_UPDATING, &hdev->state);
492 }
493 
494 static void hclgevf_update_link_mode(struct hclgevf_dev *hdev)
495 {
496 #define HCLGEVF_ADVERTISING	0
497 #define HCLGEVF_SUPPORTED	1
498 
499 	struct hclge_vf_to_pf_msg send_msg;
500 
501 	hclgevf_build_send_msg(&send_msg, HCLGE_MBX_GET_LINK_MODE, 0);
502 	send_msg.data[0] = HCLGEVF_ADVERTISING;
503 	hclgevf_send_mbx_msg(hdev, &send_msg, false, NULL, 0);
504 	send_msg.data[0] = HCLGEVF_SUPPORTED;
505 	hclgevf_send_mbx_msg(hdev, &send_msg, false, NULL, 0);
506 }
507 
508 static int hclgevf_set_handle_info(struct hclgevf_dev *hdev)
509 {
510 	struct hnae3_handle *nic = &hdev->nic;
511 	int ret;
512 
513 	nic->ae_algo = &ae_algovf;
514 	nic->pdev = hdev->pdev;
515 	nic->numa_node_mask = hdev->numa_node_mask;
516 	nic->flags |= HNAE3_SUPPORT_VF;
517 
518 	ret = hclgevf_knic_setup(hdev);
519 	if (ret)
520 		dev_err(&hdev->pdev->dev, "VF knic setup failed %d\n",
521 			ret);
522 	return ret;
523 }
524 
525 static void hclgevf_free_vector(struct hclgevf_dev *hdev, int vector_id)
526 {
527 	if (hdev->vector_status[vector_id] == HCLGEVF_INVALID_VPORT) {
528 		dev_warn(&hdev->pdev->dev,
529 			 "vector(vector_id %d) has been freed.\n", vector_id);
530 		return;
531 	}
532 
533 	hdev->vector_status[vector_id] = HCLGEVF_INVALID_VPORT;
534 	hdev->num_msi_left += 1;
535 	hdev->num_msi_used -= 1;
536 }
537 
538 static int hclgevf_get_vector(struct hnae3_handle *handle, u16 vector_num,
539 			      struct hnae3_vector_info *vector_info)
540 {
541 	struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);
542 	struct hnae3_vector_info *vector = vector_info;
543 	int alloc = 0;
544 	int i, j;
545 
546 	vector_num = min_t(u16, hdev->num_nic_msix - 1, vector_num);
547 	vector_num = min(hdev->num_msi_left, vector_num);
548 
549 	for (j = 0; j < vector_num; j++) {
550 		for (i = HCLGEVF_MISC_VECTOR_NUM + 1; i < hdev->num_msi; i++) {
551 			if (hdev->vector_status[i] == HCLGEVF_INVALID_VPORT) {
552 				vector->vector = pci_irq_vector(hdev->pdev, i);
553 				vector->io_addr = hdev->hw.io_base +
554 					HCLGEVF_VECTOR_REG_BASE +
555 					(i - 1) * HCLGEVF_VECTOR_REG_OFFSET;
556 				hdev->vector_status[i] = 0;
557 				hdev->vector_irq[i] = vector->vector;
558 
559 				vector++;
560 				alloc++;
561 
562 				break;
563 			}
564 		}
565 	}
566 	hdev->num_msi_left -= alloc;
567 	hdev->num_msi_used += alloc;
568 
569 	return alloc;
570 }
571 
572 static int hclgevf_get_vector_index(struct hclgevf_dev *hdev, int vector)
573 {
574 	int i;
575 
576 	for (i = 0; i < hdev->num_msi; i++)
577 		if (vector == hdev->vector_irq[i])
578 			return i;
579 
580 	return -EINVAL;
581 }
582 
583 static int hclgevf_set_rss_algo_key(struct hclgevf_dev *hdev,
584 				    const u8 hfunc, const u8 *key)
585 {
586 	struct hclgevf_rss_config_cmd *req;
587 	unsigned int key_offset = 0;
588 	struct hclgevf_desc desc;
589 	int key_counts;
590 	int key_size;
591 	int ret;
592 
593 	key_counts = HCLGEVF_RSS_KEY_SIZE;
594 	req = (struct hclgevf_rss_config_cmd *)desc.data;
595 
596 	while (key_counts) {
597 		hclgevf_cmd_setup_basic_desc(&desc,
598 					     HCLGEVF_OPC_RSS_GENERIC_CONFIG,
599 					     false);
600 
601 		req->hash_config |= (hfunc & HCLGEVF_RSS_HASH_ALGO_MASK);
602 		req->hash_config |=
603 			(key_offset << HCLGEVF_RSS_HASH_KEY_OFFSET_B);
604 
605 		key_size = min(HCLGEVF_RSS_HASH_KEY_NUM, key_counts);
606 		memcpy(req->hash_key,
607 		       key + key_offset * HCLGEVF_RSS_HASH_KEY_NUM, key_size);
608 
609 		key_counts -= key_size;
610 		key_offset++;
611 		ret = hclgevf_cmd_send(&hdev->hw, &desc, 1);
612 		if (ret) {
613 			dev_err(&hdev->pdev->dev,
614 				"Configure RSS config fail, status = %d\n",
615 				ret);
616 			return ret;
617 		}
618 	}
619 
620 	return 0;
621 }
622 
623 static u32 hclgevf_get_rss_key_size(struct hnae3_handle *handle)
624 {
625 	return HCLGEVF_RSS_KEY_SIZE;
626 }
627 
628 static u32 hclgevf_get_rss_indir_size(struct hnae3_handle *handle)
629 {
630 	return HCLGEVF_RSS_IND_TBL_SIZE;
631 }
632 
633 static int hclgevf_set_rss_indir_table(struct hclgevf_dev *hdev)
634 {
635 	const u8 *indir = hdev->rss_cfg.rss_indirection_tbl;
636 	struct hclgevf_rss_indirection_table_cmd *req;
637 	struct hclgevf_desc desc;
638 	int status;
639 	int i, j;
640 
641 	req = (struct hclgevf_rss_indirection_table_cmd *)desc.data;
642 
643 	for (i = 0; i < HCLGEVF_RSS_CFG_TBL_NUM; i++) {
644 		hclgevf_cmd_setup_basic_desc(&desc, HCLGEVF_OPC_RSS_INDIR_TABLE,
645 					     false);
646 		req->start_table_index = i * HCLGEVF_RSS_CFG_TBL_SIZE;
647 		req->rss_set_bitmap = HCLGEVF_RSS_SET_BITMAP_MSK;
648 		for (j = 0; j < HCLGEVF_RSS_CFG_TBL_SIZE; j++)
649 			req->rss_result[j] =
650 				indir[i * HCLGEVF_RSS_CFG_TBL_SIZE + j];
651 
652 		status = hclgevf_cmd_send(&hdev->hw, &desc, 1);
653 		if (status) {
654 			dev_err(&hdev->pdev->dev,
655 				"VF failed(=%d) to set RSS indirection table\n",
656 				status);
657 			return status;
658 		}
659 	}
660 
661 	return 0;
662 }
663 
664 static int hclgevf_set_rss_tc_mode(struct hclgevf_dev *hdev,  u16 rss_size)
665 {
666 	struct hclgevf_rss_tc_mode_cmd *req;
667 	u16 tc_offset[HCLGEVF_MAX_TC_NUM];
668 	u16 tc_valid[HCLGEVF_MAX_TC_NUM];
669 	u16 tc_size[HCLGEVF_MAX_TC_NUM];
670 	struct hclgevf_desc desc;
671 	u16 roundup_size;
672 	unsigned int i;
673 	int status;
674 
675 	req = (struct hclgevf_rss_tc_mode_cmd *)desc.data;
676 
677 	roundup_size = roundup_pow_of_two(rss_size);
678 	roundup_size = ilog2(roundup_size);
679 
680 	for (i = 0; i < HCLGEVF_MAX_TC_NUM; i++) {
681 		tc_valid[i] = !!(hdev->hw_tc_map & BIT(i));
682 		tc_size[i] = roundup_size;
683 		tc_offset[i] = rss_size * i;
684 	}
685 
686 	hclgevf_cmd_setup_basic_desc(&desc, HCLGEVF_OPC_RSS_TC_MODE, false);
687 	for (i = 0; i < HCLGEVF_MAX_TC_NUM; i++) {
688 		hnae3_set_bit(req->rss_tc_mode[i], HCLGEVF_RSS_TC_VALID_B,
689 			      (tc_valid[i] & 0x1));
690 		hnae3_set_field(req->rss_tc_mode[i], HCLGEVF_RSS_TC_SIZE_M,
691 				HCLGEVF_RSS_TC_SIZE_S, tc_size[i]);
692 		hnae3_set_field(req->rss_tc_mode[i], HCLGEVF_RSS_TC_OFFSET_M,
693 				HCLGEVF_RSS_TC_OFFSET_S, tc_offset[i]);
694 	}
695 	status = hclgevf_cmd_send(&hdev->hw, &desc, 1);
696 	if (status)
697 		dev_err(&hdev->pdev->dev,
698 			"VF failed(=%d) to set rss tc mode\n", status);
699 
700 	return status;
701 }
702 
703 /* for revision 0x20, vf shared the same rss config with pf */
704 static int hclgevf_get_rss_hash_key(struct hclgevf_dev *hdev)
705 {
706 #define HCLGEVF_RSS_MBX_RESP_LEN	8
707 	struct hclgevf_rss_cfg *rss_cfg = &hdev->rss_cfg;
708 	u8 resp_msg[HCLGEVF_RSS_MBX_RESP_LEN];
709 	struct hclge_vf_to_pf_msg send_msg;
710 	u16 msg_num, hash_key_index;
711 	u8 index;
712 	int ret;
713 
714 	hclgevf_build_send_msg(&send_msg, HCLGE_MBX_GET_RSS_KEY, 0);
715 	msg_num = (HCLGEVF_RSS_KEY_SIZE + HCLGEVF_RSS_MBX_RESP_LEN - 1) /
716 			HCLGEVF_RSS_MBX_RESP_LEN;
717 	for (index = 0; index < msg_num; index++) {
718 		send_msg.data[0] = index;
719 		ret = hclgevf_send_mbx_msg(hdev, &send_msg, true, resp_msg,
720 					   HCLGEVF_RSS_MBX_RESP_LEN);
721 		if (ret) {
722 			dev_err(&hdev->pdev->dev,
723 				"VF get rss hash key from PF failed, ret=%d",
724 				ret);
725 			return ret;
726 		}
727 
728 		hash_key_index = HCLGEVF_RSS_MBX_RESP_LEN * index;
729 		if (index == msg_num - 1)
730 			memcpy(&rss_cfg->rss_hash_key[hash_key_index],
731 			       &resp_msg[0],
732 			       HCLGEVF_RSS_KEY_SIZE - hash_key_index);
733 		else
734 			memcpy(&rss_cfg->rss_hash_key[hash_key_index],
735 			       &resp_msg[0], HCLGEVF_RSS_MBX_RESP_LEN);
736 	}
737 
738 	return 0;
739 }
740 
741 static int hclgevf_get_rss(struct hnae3_handle *handle, u32 *indir, u8 *key,
742 			   u8 *hfunc)
743 {
744 	struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);
745 	struct hclgevf_rss_cfg *rss_cfg = &hdev->rss_cfg;
746 	int i, ret;
747 
748 	if (handle->pdev->revision >= 0x21) {
749 		/* Get hash algorithm */
750 		if (hfunc) {
751 			switch (rss_cfg->hash_algo) {
752 			case HCLGEVF_RSS_HASH_ALGO_TOEPLITZ:
753 				*hfunc = ETH_RSS_HASH_TOP;
754 				break;
755 			case HCLGEVF_RSS_HASH_ALGO_SIMPLE:
756 				*hfunc = ETH_RSS_HASH_XOR;
757 				break;
758 			default:
759 				*hfunc = ETH_RSS_HASH_UNKNOWN;
760 				break;
761 			}
762 		}
763 
764 		/* Get the RSS Key required by the user */
765 		if (key)
766 			memcpy(key, rss_cfg->rss_hash_key,
767 			       HCLGEVF_RSS_KEY_SIZE);
768 	} else {
769 		if (hfunc)
770 			*hfunc = ETH_RSS_HASH_TOP;
771 		if (key) {
772 			ret = hclgevf_get_rss_hash_key(hdev);
773 			if (ret)
774 				return ret;
775 			memcpy(key, rss_cfg->rss_hash_key,
776 			       HCLGEVF_RSS_KEY_SIZE);
777 		}
778 	}
779 
780 	if (indir)
781 		for (i = 0; i < HCLGEVF_RSS_IND_TBL_SIZE; i++)
782 			indir[i] = rss_cfg->rss_indirection_tbl[i];
783 
784 	return 0;
785 }
786 
787 static int hclgevf_set_rss(struct hnae3_handle *handle, const u32 *indir,
788 			   const u8 *key, const u8 hfunc)
789 {
790 	struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);
791 	struct hclgevf_rss_cfg *rss_cfg = &hdev->rss_cfg;
792 	int ret, i;
793 
794 	if (handle->pdev->revision >= 0x21) {
795 		/* Set the RSS Hash Key if specififed by the user */
796 		if (key) {
797 			switch (hfunc) {
798 			case ETH_RSS_HASH_TOP:
799 				rss_cfg->hash_algo =
800 					HCLGEVF_RSS_HASH_ALGO_TOEPLITZ;
801 				break;
802 			case ETH_RSS_HASH_XOR:
803 				rss_cfg->hash_algo =
804 					HCLGEVF_RSS_HASH_ALGO_SIMPLE;
805 				break;
806 			case ETH_RSS_HASH_NO_CHANGE:
807 				break;
808 			default:
809 				return -EINVAL;
810 			}
811 
812 			ret = hclgevf_set_rss_algo_key(hdev, rss_cfg->hash_algo,
813 						       key);
814 			if (ret)
815 				return ret;
816 
817 			/* Update the shadow RSS key with user specified qids */
818 			memcpy(rss_cfg->rss_hash_key, key,
819 			       HCLGEVF_RSS_KEY_SIZE);
820 		}
821 	}
822 
823 	/* update the shadow RSS table with user specified qids */
824 	for (i = 0; i < HCLGEVF_RSS_IND_TBL_SIZE; i++)
825 		rss_cfg->rss_indirection_tbl[i] = indir[i];
826 
827 	/* update the hardware */
828 	return hclgevf_set_rss_indir_table(hdev);
829 }
830 
831 static u8 hclgevf_get_rss_hash_bits(struct ethtool_rxnfc *nfc)
832 {
833 	u8 hash_sets = nfc->data & RXH_L4_B_0_1 ? HCLGEVF_S_PORT_BIT : 0;
834 
835 	if (nfc->data & RXH_L4_B_2_3)
836 		hash_sets |= HCLGEVF_D_PORT_BIT;
837 	else
838 		hash_sets &= ~HCLGEVF_D_PORT_BIT;
839 
840 	if (nfc->data & RXH_IP_SRC)
841 		hash_sets |= HCLGEVF_S_IP_BIT;
842 	else
843 		hash_sets &= ~HCLGEVF_S_IP_BIT;
844 
845 	if (nfc->data & RXH_IP_DST)
846 		hash_sets |= HCLGEVF_D_IP_BIT;
847 	else
848 		hash_sets &= ~HCLGEVF_D_IP_BIT;
849 
850 	if (nfc->flow_type == SCTP_V4_FLOW || nfc->flow_type == SCTP_V6_FLOW)
851 		hash_sets |= HCLGEVF_V_TAG_BIT;
852 
853 	return hash_sets;
854 }
855 
856 static int hclgevf_set_rss_tuple(struct hnae3_handle *handle,
857 				 struct ethtool_rxnfc *nfc)
858 {
859 	struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);
860 	struct hclgevf_rss_cfg *rss_cfg = &hdev->rss_cfg;
861 	struct hclgevf_rss_input_tuple_cmd *req;
862 	struct hclgevf_desc desc;
863 	u8 tuple_sets;
864 	int ret;
865 
866 	if (handle->pdev->revision == 0x20)
867 		return -EOPNOTSUPP;
868 
869 	if (nfc->data &
870 	    ~(RXH_IP_SRC | RXH_IP_DST | RXH_L4_B_0_1 | RXH_L4_B_2_3))
871 		return -EINVAL;
872 
873 	req = (struct hclgevf_rss_input_tuple_cmd *)desc.data;
874 	hclgevf_cmd_setup_basic_desc(&desc, HCLGEVF_OPC_RSS_INPUT_TUPLE, false);
875 
876 	req->ipv4_tcp_en = rss_cfg->rss_tuple_sets.ipv4_tcp_en;
877 	req->ipv4_udp_en = rss_cfg->rss_tuple_sets.ipv4_udp_en;
878 	req->ipv4_sctp_en = rss_cfg->rss_tuple_sets.ipv4_sctp_en;
879 	req->ipv4_fragment_en = rss_cfg->rss_tuple_sets.ipv4_fragment_en;
880 	req->ipv6_tcp_en = rss_cfg->rss_tuple_sets.ipv6_tcp_en;
881 	req->ipv6_udp_en = rss_cfg->rss_tuple_sets.ipv6_udp_en;
882 	req->ipv6_sctp_en = rss_cfg->rss_tuple_sets.ipv6_sctp_en;
883 	req->ipv6_fragment_en = rss_cfg->rss_tuple_sets.ipv6_fragment_en;
884 
885 	tuple_sets = hclgevf_get_rss_hash_bits(nfc);
886 	switch (nfc->flow_type) {
887 	case TCP_V4_FLOW:
888 		req->ipv4_tcp_en = tuple_sets;
889 		break;
890 	case TCP_V6_FLOW:
891 		req->ipv6_tcp_en = tuple_sets;
892 		break;
893 	case UDP_V4_FLOW:
894 		req->ipv4_udp_en = tuple_sets;
895 		break;
896 	case UDP_V6_FLOW:
897 		req->ipv6_udp_en = tuple_sets;
898 		break;
899 	case SCTP_V4_FLOW:
900 		req->ipv4_sctp_en = tuple_sets;
901 		break;
902 	case SCTP_V6_FLOW:
903 		if ((nfc->data & RXH_L4_B_0_1) ||
904 		    (nfc->data & RXH_L4_B_2_3))
905 			return -EINVAL;
906 
907 		req->ipv6_sctp_en = tuple_sets;
908 		break;
909 	case IPV4_FLOW:
910 		req->ipv4_fragment_en = HCLGEVF_RSS_INPUT_TUPLE_OTHER;
911 		break;
912 	case IPV6_FLOW:
913 		req->ipv6_fragment_en = HCLGEVF_RSS_INPUT_TUPLE_OTHER;
914 		break;
915 	default:
916 		return -EINVAL;
917 	}
918 
919 	ret = hclgevf_cmd_send(&hdev->hw, &desc, 1);
920 	if (ret) {
921 		dev_err(&hdev->pdev->dev,
922 			"Set rss tuple fail, status = %d\n", ret);
923 		return ret;
924 	}
925 
926 	rss_cfg->rss_tuple_sets.ipv4_tcp_en = req->ipv4_tcp_en;
927 	rss_cfg->rss_tuple_sets.ipv4_udp_en = req->ipv4_udp_en;
928 	rss_cfg->rss_tuple_sets.ipv4_sctp_en = req->ipv4_sctp_en;
929 	rss_cfg->rss_tuple_sets.ipv4_fragment_en = req->ipv4_fragment_en;
930 	rss_cfg->rss_tuple_sets.ipv6_tcp_en = req->ipv6_tcp_en;
931 	rss_cfg->rss_tuple_sets.ipv6_udp_en = req->ipv6_udp_en;
932 	rss_cfg->rss_tuple_sets.ipv6_sctp_en = req->ipv6_sctp_en;
933 	rss_cfg->rss_tuple_sets.ipv6_fragment_en = req->ipv6_fragment_en;
934 	return 0;
935 }
936 
937 static int hclgevf_get_rss_tuple(struct hnae3_handle *handle,
938 				 struct ethtool_rxnfc *nfc)
939 {
940 	struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);
941 	struct hclgevf_rss_cfg *rss_cfg = &hdev->rss_cfg;
942 	u8 tuple_sets;
943 
944 	if (handle->pdev->revision == 0x20)
945 		return -EOPNOTSUPP;
946 
947 	nfc->data = 0;
948 
949 	switch (nfc->flow_type) {
950 	case TCP_V4_FLOW:
951 		tuple_sets = rss_cfg->rss_tuple_sets.ipv4_tcp_en;
952 		break;
953 	case UDP_V4_FLOW:
954 		tuple_sets = rss_cfg->rss_tuple_sets.ipv4_udp_en;
955 		break;
956 	case TCP_V6_FLOW:
957 		tuple_sets = rss_cfg->rss_tuple_sets.ipv6_tcp_en;
958 		break;
959 	case UDP_V6_FLOW:
960 		tuple_sets = rss_cfg->rss_tuple_sets.ipv6_udp_en;
961 		break;
962 	case SCTP_V4_FLOW:
963 		tuple_sets = rss_cfg->rss_tuple_sets.ipv4_sctp_en;
964 		break;
965 	case SCTP_V6_FLOW:
966 		tuple_sets = rss_cfg->rss_tuple_sets.ipv6_sctp_en;
967 		break;
968 	case IPV4_FLOW:
969 	case IPV6_FLOW:
970 		tuple_sets = HCLGEVF_S_IP_BIT | HCLGEVF_D_IP_BIT;
971 		break;
972 	default:
973 		return -EINVAL;
974 	}
975 
976 	if (!tuple_sets)
977 		return 0;
978 
979 	if (tuple_sets & HCLGEVF_D_PORT_BIT)
980 		nfc->data |= RXH_L4_B_2_3;
981 	if (tuple_sets & HCLGEVF_S_PORT_BIT)
982 		nfc->data |= RXH_L4_B_0_1;
983 	if (tuple_sets & HCLGEVF_D_IP_BIT)
984 		nfc->data |= RXH_IP_DST;
985 	if (tuple_sets & HCLGEVF_S_IP_BIT)
986 		nfc->data |= RXH_IP_SRC;
987 
988 	return 0;
989 }
990 
991 static int hclgevf_set_rss_input_tuple(struct hclgevf_dev *hdev,
992 				       struct hclgevf_rss_cfg *rss_cfg)
993 {
994 	struct hclgevf_rss_input_tuple_cmd *req;
995 	struct hclgevf_desc desc;
996 	int ret;
997 
998 	hclgevf_cmd_setup_basic_desc(&desc, HCLGEVF_OPC_RSS_INPUT_TUPLE, false);
999 
1000 	req = (struct hclgevf_rss_input_tuple_cmd *)desc.data;
1001 
1002 	req->ipv4_tcp_en = rss_cfg->rss_tuple_sets.ipv4_tcp_en;
1003 	req->ipv4_udp_en = rss_cfg->rss_tuple_sets.ipv4_udp_en;
1004 	req->ipv4_sctp_en = rss_cfg->rss_tuple_sets.ipv4_sctp_en;
1005 	req->ipv4_fragment_en = rss_cfg->rss_tuple_sets.ipv4_fragment_en;
1006 	req->ipv6_tcp_en = rss_cfg->rss_tuple_sets.ipv6_tcp_en;
1007 	req->ipv6_udp_en = rss_cfg->rss_tuple_sets.ipv6_udp_en;
1008 	req->ipv6_sctp_en = rss_cfg->rss_tuple_sets.ipv6_sctp_en;
1009 	req->ipv6_fragment_en = rss_cfg->rss_tuple_sets.ipv6_fragment_en;
1010 
1011 	ret = hclgevf_cmd_send(&hdev->hw, &desc, 1);
1012 	if (ret)
1013 		dev_err(&hdev->pdev->dev,
1014 			"Configure rss input fail, status = %d\n", ret);
1015 	return ret;
1016 }
1017 
1018 static int hclgevf_get_tc_size(struct hnae3_handle *handle)
1019 {
1020 	struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);
1021 	struct hclgevf_rss_cfg *rss_cfg = &hdev->rss_cfg;
1022 
1023 	return rss_cfg->rss_size;
1024 }
1025 
1026 static int hclgevf_bind_ring_to_vector(struct hnae3_handle *handle, bool en,
1027 				       int vector_id,
1028 				       struct hnae3_ring_chain_node *ring_chain)
1029 {
1030 	struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);
1031 	struct hclge_vf_to_pf_msg send_msg;
1032 	struct hnae3_ring_chain_node *node;
1033 	int status;
1034 	int i = 0;
1035 
1036 	memset(&send_msg, 0, sizeof(send_msg));
1037 	send_msg.code = en ? HCLGE_MBX_MAP_RING_TO_VECTOR :
1038 		HCLGE_MBX_UNMAP_RING_TO_VECTOR;
1039 	send_msg.vector_id = vector_id;
1040 
1041 	for (node = ring_chain; node; node = node->next) {
1042 		send_msg.param[i].ring_type =
1043 				hnae3_get_bit(node->flag, HNAE3_RING_TYPE_B);
1044 
1045 		send_msg.param[i].tqp_index = node->tqp_index;
1046 		send_msg.param[i].int_gl_index =
1047 					hnae3_get_field(node->int_gl_idx,
1048 							HNAE3_RING_GL_IDX_M,
1049 							HNAE3_RING_GL_IDX_S);
1050 
1051 		i++;
1052 		if (i == HCLGE_MBX_MAX_RING_CHAIN_PARAM_NUM || !node->next) {
1053 			send_msg.ring_num = i;
1054 
1055 			status = hclgevf_send_mbx_msg(hdev, &send_msg, false,
1056 						      NULL, 0);
1057 			if (status) {
1058 				dev_err(&hdev->pdev->dev,
1059 					"Map TQP fail, status is %d.\n",
1060 					status);
1061 				return status;
1062 			}
1063 			i = 0;
1064 		}
1065 	}
1066 
1067 	return 0;
1068 }
1069 
1070 static int hclgevf_map_ring_to_vector(struct hnae3_handle *handle, int vector,
1071 				      struct hnae3_ring_chain_node *ring_chain)
1072 {
1073 	struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);
1074 	int vector_id;
1075 
1076 	vector_id = hclgevf_get_vector_index(hdev, vector);
1077 	if (vector_id < 0) {
1078 		dev_err(&handle->pdev->dev,
1079 			"Get vector index fail. ret =%d\n", vector_id);
1080 		return vector_id;
1081 	}
1082 
1083 	return hclgevf_bind_ring_to_vector(handle, true, vector_id, ring_chain);
1084 }
1085 
1086 static int hclgevf_unmap_ring_from_vector(
1087 				struct hnae3_handle *handle,
1088 				int vector,
1089 				struct hnae3_ring_chain_node *ring_chain)
1090 {
1091 	struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);
1092 	int ret, vector_id;
1093 
1094 	if (test_bit(HCLGEVF_STATE_RST_HANDLING, &hdev->state))
1095 		return 0;
1096 
1097 	vector_id = hclgevf_get_vector_index(hdev, vector);
1098 	if (vector_id < 0) {
1099 		dev_err(&handle->pdev->dev,
1100 			"Get vector index fail. ret =%d\n", vector_id);
1101 		return vector_id;
1102 	}
1103 
1104 	ret = hclgevf_bind_ring_to_vector(handle, false, vector_id, ring_chain);
1105 	if (ret)
1106 		dev_err(&handle->pdev->dev,
1107 			"Unmap ring from vector fail. vector=%d, ret =%d\n",
1108 			vector_id,
1109 			ret);
1110 
1111 	return ret;
1112 }
1113 
1114 static int hclgevf_put_vector(struct hnae3_handle *handle, int vector)
1115 {
1116 	struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);
1117 	int vector_id;
1118 
1119 	vector_id = hclgevf_get_vector_index(hdev, vector);
1120 	if (vector_id < 0) {
1121 		dev_err(&handle->pdev->dev,
1122 			"hclgevf_put_vector get vector index fail. ret =%d\n",
1123 			vector_id);
1124 		return vector_id;
1125 	}
1126 
1127 	hclgevf_free_vector(hdev, vector_id);
1128 
1129 	return 0;
1130 }
1131 
1132 static int hclgevf_cmd_set_promisc_mode(struct hclgevf_dev *hdev,
1133 					bool en_uc_pmc, bool en_mc_pmc,
1134 					bool en_bc_pmc)
1135 {
1136 	struct hclge_vf_to_pf_msg send_msg;
1137 	int ret;
1138 
1139 	memset(&send_msg, 0, sizeof(send_msg));
1140 	send_msg.code = HCLGE_MBX_SET_PROMISC_MODE;
1141 	send_msg.en_bc = en_bc_pmc ? 1 : 0;
1142 	send_msg.en_uc = en_uc_pmc ? 1 : 0;
1143 	send_msg.en_mc = en_mc_pmc ? 1 : 0;
1144 
1145 	ret = hclgevf_send_mbx_msg(hdev, &send_msg, false, NULL, 0);
1146 	if (ret)
1147 		dev_err(&hdev->pdev->dev,
1148 			"Set promisc mode fail, status is %d.\n", ret);
1149 
1150 	return ret;
1151 }
1152 
1153 static int hclgevf_set_promisc_mode(struct hnae3_handle *handle, bool en_uc_pmc,
1154 				    bool en_mc_pmc)
1155 {
1156 	struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);
1157 	struct pci_dev *pdev = hdev->pdev;
1158 	bool en_bc_pmc;
1159 
1160 	en_bc_pmc = pdev->revision != 0x20;
1161 
1162 	return hclgevf_cmd_set_promisc_mode(hdev, en_uc_pmc, en_mc_pmc,
1163 					    en_bc_pmc);
1164 }
1165 
1166 static void hclgevf_request_update_promisc_mode(struct hnae3_handle *handle)
1167 {
1168 	struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);
1169 
1170 	set_bit(HCLGEVF_STATE_PROMISC_CHANGED, &hdev->state);
1171 }
1172 
1173 static void hclgevf_sync_promisc_mode(struct hclgevf_dev *hdev)
1174 {
1175 	struct hnae3_handle *handle = &hdev->nic;
1176 	bool en_uc_pmc = handle->netdev_flags & HNAE3_UPE;
1177 	bool en_mc_pmc = handle->netdev_flags & HNAE3_MPE;
1178 	int ret;
1179 
1180 	if (test_bit(HCLGEVF_STATE_PROMISC_CHANGED, &hdev->state)) {
1181 		ret = hclgevf_set_promisc_mode(handle, en_uc_pmc, en_mc_pmc);
1182 		if (!ret)
1183 			clear_bit(HCLGEVF_STATE_PROMISC_CHANGED, &hdev->state);
1184 	}
1185 }
1186 
1187 static int hclgevf_tqp_enable(struct hclgevf_dev *hdev, unsigned int tqp_id,
1188 			      int stream_id, bool enable)
1189 {
1190 	struct hclgevf_cfg_com_tqp_queue_cmd *req;
1191 	struct hclgevf_desc desc;
1192 	int status;
1193 
1194 	req = (struct hclgevf_cfg_com_tqp_queue_cmd *)desc.data;
1195 
1196 	hclgevf_cmd_setup_basic_desc(&desc, HCLGEVF_OPC_CFG_COM_TQP_QUEUE,
1197 				     false);
1198 	req->tqp_id = cpu_to_le16(tqp_id & HCLGEVF_RING_ID_MASK);
1199 	req->stream_id = cpu_to_le16(stream_id);
1200 	if (enable)
1201 		req->enable |= 1U << HCLGEVF_TQP_ENABLE_B;
1202 
1203 	status = hclgevf_cmd_send(&hdev->hw, &desc, 1);
1204 	if (status)
1205 		dev_err(&hdev->pdev->dev,
1206 			"TQP enable fail, status =%d.\n", status);
1207 
1208 	return status;
1209 }
1210 
1211 static void hclgevf_reset_tqp_stats(struct hnae3_handle *handle)
1212 {
1213 	struct hnae3_knic_private_info *kinfo = &handle->kinfo;
1214 	struct hclgevf_tqp *tqp;
1215 	int i;
1216 
1217 	for (i = 0; i < kinfo->num_tqps; i++) {
1218 		tqp = container_of(kinfo->tqp[i], struct hclgevf_tqp, q);
1219 		memset(&tqp->tqp_stats, 0, sizeof(tqp->tqp_stats));
1220 	}
1221 }
1222 
1223 static int hclgevf_get_host_mac_addr(struct hclgevf_dev *hdev, u8 *p)
1224 {
1225 	struct hclge_vf_to_pf_msg send_msg;
1226 	u8 host_mac[ETH_ALEN];
1227 	int status;
1228 
1229 	hclgevf_build_send_msg(&send_msg, HCLGE_MBX_GET_MAC_ADDR, 0);
1230 	status = hclgevf_send_mbx_msg(hdev, &send_msg, true, host_mac,
1231 				      ETH_ALEN);
1232 	if (status) {
1233 		dev_err(&hdev->pdev->dev,
1234 			"fail to get VF MAC from host %d", status);
1235 		return status;
1236 	}
1237 
1238 	ether_addr_copy(p, host_mac);
1239 
1240 	return 0;
1241 }
1242 
1243 static void hclgevf_get_mac_addr(struct hnae3_handle *handle, u8 *p)
1244 {
1245 	struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);
1246 	u8 host_mac_addr[ETH_ALEN];
1247 
1248 	if (hclgevf_get_host_mac_addr(hdev, host_mac_addr))
1249 		return;
1250 
1251 	hdev->has_pf_mac = !is_zero_ether_addr(host_mac_addr);
1252 	if (hdev->has_pf_mac)
1253 		ether_addr_copy(p, host_mac_addr);
1254 	else
1255 		ether_addr_copy(p, hdev->hw.mac.mac_addr);
1256 }
1257 
1258 static int hclgevf_set_mac_addr(struct hnae3_handle *handle, void *p,
1259 				bool is_first)
1260 {
1261 	struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);
1262 	u8 *old_mac_addr = (u8 *)hdev->hw.mac.mac_addr;
1263 	struct hclge_vf_to_pf_msg send_msg;
1264 	u8 *new_mac_addr = (u8 *)p;
1265 	int status;
1266 
1267 	hclgevf_build_send_msg(&send_msg, HCLGE_MBX_SET_UNICAST, 0);
1268 	send_msg.subcode = HCLGE_MBX_MAC_VLAN_UC_MODIFY;
1269 	ether_addr_copy(send_msg.data, new_mac_addr);
1270 	if (is_first && !hdev->has_pf_mac)
1271 		eth_zero_addr(&send_msg.data[ETH_ALEN]);
1272 	else
1273 		ether_addr_copy(&send_msg.data[ETH_ALEN], old_mac_addr);
1274 	status = hclgevf_send_mbx_msg(hdev, &send_msg, true, NULL, 0);
1275 	if (!status)
1276 		ether_addr_copy(hdev->hw.mac.mac_addr, new_mac_addr);
1277 
1278 	return status;
1279 }
1280 
1281 static struct hclgevf_mac_addr_node *
1282 hclgevf_find_mac_node(struct list_head *list, const u8 *mac_addr)
1283 {
1284 	struct hclgevf_mac_addr_node *mac_node, *tmp;
1285 
1286 	list_for_each_entry_safe(mac_node, tmp, list, node)
1287 		if (ether_addr_equal(mac_addr, mac_node->mac_addr))
1288 			return mac_node;
1289 
1290 	return NULL;
1291 }
1292 
1293 static void hclgevf_update_mac_node(struct hclgevf_mac_addr_node *mac_node,
1294 				    enum HCLGEVF_MAC_NODE_STATE state)
1295 {
1296 	switch (state) {
1297 	/* from set_rx_mode or tmp_add_list */
1298 	case HCLGEVF_MAC_TO_ADD:
1299 		if (mac_node->state == HCLGEVF_MAC_TO_DEL)
1300 			mac_node->state = HCLGEVF_MAC_ACTIVE;
1301 		break;
1302 	/* only from set_rx_mode */
1303 	case HCLGEVF_MAC_TO_DEL:
1304 		if (mac_node->state == HCLGEVF_MAC_TO_ADD) {
1305 			list_del(&mac_node->node);
1306 			kfree(mac_node);
1307 		} else {
1308 			mac_node->state = HCLGEVF_MAC_TO_DEL;
1309 		}
1310 		break;
1311 	/* only from tmp_add_list, the mac_node->state won't be
1312 	 * HCLGEVF_MAC_ACTIVE
1313 	 */
1314 	case HCLGEVF_MAC_ACTIVE:
1315 		if (mac_node->state == HCLGEVF_MAC_TO_ADD)
1316 			mac_node->state = HCLGEVF_MAC_ACTIVE;
1317 		break;
1318 	}
1319 }
1320 
1321 static int hclgevf_update_mac_list(struct hnae3_handle *handle,
1322 				   enum HCLGEVF_MAC_NODE_STATE state,
1323 				   enum HCLGEVF_MAC_ADDR_TYPE mac_type,
1324 				   const unsigned char *addr)
1325 {
1326 	struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);
1327 	struct hclgevf_mac_addr_node *mac_node;
1328 	struct list_head *list;
1329 
1330 	list = (mac_type == HCLGEVF_MAC_ADDR_UC) ?
1331 	       &hdev->mac_table.uc_mac_list : &hdev->mac_table.mc_mac_list;
1332 
1333 	spin_lock_bh(&hdev->mac_table.mac_list_lock);
1334 
1335 	/* if the mac addr is already in the mac list, no need to add a new
1336 	 * one into it, just check the mac addr state, convert it to a new
1337 	 * new state, or just remove it, or do nothing.
1338 	 */
1339 	mac_node = hclgevf_find_mac_node(list, addr);
1340 	if (mac_node) {
1341 		hclgevf_update_mac_node(mac_node, state);
1342 		spin_unlock_bh(&hdev->mac_table.mac_list_lock);
1343 		return 0;
1344 	}
1345 	/* if this address is never added, unnecessary to delete */
1346 	if (state == HCLGEVF_MAC_TO_DEL) {
1347 		spin_unlock_bh(&hdev->mac_table.mac_list_lock);
1348 		return -ENOENT;
1349 	}
1350 
1351 	mac_node = kzalloc(sizeof(*mac_node), GFP_ATOMIC);
1352 	if (!mac_node) {
1353 		spin_unlock_bh(&hdev->mac_table.mac_list_lock);
1354 		return -ENOMEM;
1355 	}
1356 
1357 	mac_node->state = state;
1358 	ether_addr_copy(mac_node->mac_addr, addr);
1359 	list_add_tail(&mac_node->node, list);
1360 
1361 	spin_unlock_bh(&hdev->mac_table.mac_list_lock);
1362 	return 0;
1363 }
1364 
1365 static int hclgevf_add_uc_addr(struct hnae3_handle *handle,
1366 			       const unsigned char *addr)
1367 {
1368 	return hclgevf_update_mac_list(handle, HCLGEVF_MAC_TO_ADD,
1369 				       HCLGEVF_MAC_ADDR_UC, addr);
1370 }
1371 
1372 static int hclgevf_rm_uc_addr(struct hnae3_handle *handle,
1373 			      const unsigned char *addr)
1374 {
1375 	return hclgevf_update_mac_list(handle, HCLGEVF_MAC_TO_DEL,
1376 				       HCLGEVF_MAC_ADDR_UC, addr);
1377 }
1378 
1379 static int hclgevf_add_mc_addr(struct hnae3_handle *handle,
1380 			       const unsigned char *addr)
1381 {
1382 	return hclgevf_update_mac_list(handle, HCLGEVF_MAC_TO_ADD,
1383 				       HCLGEVF_MAC_ADDR_MC, addr);
1384 }
1385 
1386 static int hclgevf_rm_mc_addr(struct hnae3_handle *handle,
1387 			      const unsigned char *addr)
1388 {
1389 	return hclgevf_update_mac_list(handle, HCLGEVF_MAC_TO_DEL,
1390 				       HCLGEVF_MAC_ADDR_MC, addr);
1391 }
1392 
1393 static int hclgevf_add_del_mac_addr(struct hclgevf_dev *hdev,
1394 				    struct hclgevf_mac_addr_node *mac_node,
1395 				    enum HCLGEVF_MAC_ADDR_TYPE mac_type)
1396 {
1397 	struct hclge_vf_to_pf_msg send_msg;
1398 	u8 code, subcode;
1399 
1400 	if (mac_type == HCLGEVF_MAC_ADDR_UC) {
1401 		code = HCLGE_MBX_SET_UNICAST;
1402 		if (mac_node->state == HCLGEVF_MAC_TO_ADD)
1403 			subcode = HCLGE_MBX_MAC_VLAN_UC_ADD;
1404 		else
1405 			subcode = HCLGE_MBX_MAC_VLAN_UC_REMOVE;
1406 	} else {
1407 		code = HCLGE_MBX_SET_MULTICAST;
1408 		if (mac_node->state == HCLGEVF_MAC_TO_ADD)
1409 			subcode = HCLGE_MBX_MAC_VLAN_MC_ADD;
1410 		else
1411 			subcode = HCLGE_MBX_MAC_VLAN_MC_REMOVE;
1412 	}
1413 
1414 	hclgevf_build_send_msg(&send_msg, code, subcode);
1415 	ether_addr_copy(send_msg.data, mac_node->mac_addr);
1416 	return hclgevf_send_mbx_msg(hdev, &send_msg, false, NULL, 0);
1417 }
1418 
1419 static void hclgevf_config_mac_list(struct hclgevf_dev *hdev,
1420 				    struct list_head *list,
1421 				    enum HCLGEVF_MAC_ADDR_TYPE mac_type)
1422 {
1423 	struct hclgevf_mac_addr_node *mac_node, *tmp;
1424 	int ret;
1425 
1426 	list_for_each_entry_safe(mac_node, tmp, list, node) {
1427 		ret = hclgevf_add_del_mac_addr(hdev, mac_node, mac_type);
1428 		if  (ret) {
1429 			dev_err(&hdev->pdev->dev,
1430 				"failed to configure mac %pM, state = %d, ret = %d\n",
1431 				mac_node->mac_addr, mac_node->state, ret);
1432 			return;
1433 		}
1434 		if (mac_node->state == HCLGEVF_MAC_TO_ADD) {
1435 			mac_node->state = HCLGEVF_MAC_ACTIVE;
1436 		} else {
1437 			list_del(&mac_node->node);
1438 			kfree(mac_node);
1439 		}
1440 	}
1441 }
1442 
1443 static void hclgevf_sync_from_add_list(struct list_head *add_list,
1444 				       struct list_head *mac_list)
1445 {
1446 	struct hclgevf_mac_addr_node *mac_node, *tmp, *new_node;
1447 
1448 	list_for_each_entry_safe(mac_node, tmp, add_list, node) {
1449 		/* if the mac address from tmp_add_list is not in the
1450 		 * uc/mc_mac_list, it means have received a TO_DEL request
1451 		 * during the time window of sending mac config request to PF
1452 		 * If mac_node state is ACTIVE, then change its state to TO_DEL,
1453 		 * then it will be removed at next time. If is TO_ADD, it means
1454 		 * send TO_ADD request failed, so just remove the mac node.
1455 		 */
1456 		new_node = hclgevf_find_mac_node(mac_list, mac_node->mac_addr);
1457 		if (new_node) {
1458 			hclgevf_update_mac_node(new_node, mac_node->state);
1459 			list_del(&mac_node->node);
1460 			kfree(mac_node);
1461 		} else if (mac_node->state == HCLGEVF_MAC_ACTIVE) {
1462 			mac_node->state = HCLGEVF_MAC_TO_DEL;
1463 			list_del(&mac_node->node);
1464 			list_add_tail(&mac_node->node, mac_list);
1465 		} else {
1466 			list_del(&mac_node->node);
1467 			kfree(mac_node);
1468 		}
1469 	}
1470 }
1471 
1472 static void hclgevf_sync_from_del_list(struct list_head *del_list,
1473 				       struct list_head *mac_list)
1474 {
1475 	struct hclgevf_mac_addr_node *mac_node, *tmp, *new_node;
1476 
1477 	list_for_each_entry_safe(mac_node, tmp, del_list, node) {
1478 		new_node = hclgevf_find_mac_node(mac_list, mac_node->mac_addr);
1479 		if (new_node) {
1480 			/* If the mac addr is exist in the mac list, it means
1481 			 * received a new request TO_ADD during the time window
1482 			 * of sending mac addr configurrequest to PF, so just
1483 			 * change the mac state to ACTIVE.
1484 			 */
1485 			new_node->state = HCLGEVF_MAC_ACTIVE;
1486 			list_del(&mac_node->node);
1487 			kfree(mac_node);
1488 		} else {
1489 			list_del(&mac_node->node);
1490 			list_add_tail(&mac_node->node, mac_list);
1491 		}
1492 	}
1493 }
1494 
1495 static void hclgevf_clear_list(struct list_head *list)
1496 {
1497 	struct hclgevf_mac_addr_node *mac_node, *tmp;
1498 
1499 	list_for_each_entry_safe(mac_node, tmp, list, node) {
1500 		list_del(&mac_node->node);
1501 		kfree(mac_node);
1502 	}
1503 }
1504 
1505 static void hclgevf_sync_mac_list(struct hclgevf_dev *hdev,
1506 				  enum HCLGEVF_MAC_ADDR_TYPE mac_type)
1507 {
1508 	struct hclgevf_mac_addr_node *mac_node, *tmp, *new_node;
1509 	struct list_head tmp_add_list, tmp_del_list;
1510 	struct list_head *list;
1511 
1512 	INIT_LIST_HEAD(&tmp_add_list);
1513 	INIT_LIST_HEAD(&tmp_del_list);
1514 
1515 	/* move the mac addr to the tmp_add_list and tmp_del_list, then
1516 	 * we can add/delete these mac addr outside the spin lock
1517 	 */
1518 	list = (mac_type == HCLGEVF_MAC_ADDR_UC) ?
1519 		&hdev->mac_table.uc_mac_list : &hdev->mac_table.mc_mac_list;
1520 
1521 	spin_lock_bh(&hdev->mac_table.mac_list_lock);
1522 
1523 	list_for_each_entry_safe(mac_node, tmp, list, node) {
1524 		switch (mac_node->state) {
1525 		case HCLGEVF_MAC_TO_DEL:
1526 			list_del(&mac_node->node);
1527 			list_add_tail(&mac_node->node, &tmp_del_list);
1528 			break;
1529 		case HCLGEVF_MAC_TO_ADD:
1530 			new_node = kzalloc(sizeof(*new_node), GFP_ATOMIC);
1531 			if (!new_node)
1532 				goto stop_traverse;
1533 
1534 			ether_addr_copy(new_node->mac_addr, mac_node->mac_addr);
1535 			new_node->state = mac_node->state;
1536 			list_add_tail(&new_node->node, &tmp_add_list);
1537 			break;
1538 		default:
1539 			break;
1540 		}
1541 	}
1542 
1543 stop_traverse:
1544 	spin_unlock_bh(&hdev->mac_table.mac_list_lock);
1545 
1546 	/* delete first, in order to get max mac table space for adding */
1547 	hclgevf_config_mac_list(hdev, &tmp_del_list, mac_type);
1548 	hclgevf_config_mac_list(hdev, &tmp_add_list, mac_type);
1549 
1550 	/* if some mac addresses were added/deleted fail, move back to the
1551 	 * mac_list, and retry at next time.
1552 	 */
1553 	spin_lock_bh(&hdev->mac_table.mac_list_lock);
1554 
1555 	hclgevf_sync_from_del_list(&tmp_del_list, list);
1556 	hclgevf_sync_from_add_list(&tmp_add_list, list);
1557 
1558 	spin_unlock_bh(&hdev->mac_table.mac_list_lock);
1559 }
1560 
1561 static void hclgevf_sync_mac_table(struct hclgevf_dev *hdev)
1562 {
1563 	hclgevf_sync_mac_list(hdev, HCLGEVF_MAC_ADDR_UC);
1564 	hclgevf_sync_mac_list(hdev, HCLGEVF_MAC_ADDR_MC);
1565 }
1566 
1567 static void hclgevf_uninit_mac_list(struct hclgevf_dev *hdev)
1568 {
1569 	spin_lock_bh(&hdev->mac_table.mac_list_lock);
1570 
1571 	hclgevf_clear_list(&hdev->mac_table.uc_mac_list);
1572 	hclgevf_clear_list(&hdev->mac_table.mc_mac_list);
1573 
1574 	spin_unlock_bh(&hdev->mac_table.mac_list_lock);
1575 }
1576 
1577 static int hclgevf_set_vlan_filter(struct hnae3_handle *handle,
1578 				   __be16 proto, u16 vlan_id,
1579 				   bool is_kill)
1580 {
1581 #define HCLGEVF_VLAN_MBX_IS_KILL_OFFSET	0
1582 #define HCLGEVF_VLAN_MBX_VLAN_ID_OFFSET	1
1583 #define HCLGEVF_VLAN_MBX_PROTO_OFFSET	3
1584 
1585 	struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);
1586 	struct hclge_vf_to_pf_msg send_msg;
1587 	int ret;
1588 
1589 	if (vlan_id > HCLGEVF_MAX_VLAN_ID)
1590 		return -EINVAL;
1591 
1592 	if (proto != htons(ETH_P_8021Q))
1593 		return -EPROTONOSUPPORT;
1594 
1595 	/* When device is resetting, firmware is unable to handle
1596 	 * mailbox. Just record the vlan id, and remove it after
1597 	 * reset finished.
1598 	 */
1599 	if (test_bit(HCLGEVF_STATE_RST_HANDLING, &hdev->state) && is_kill) {
1600 		set_bit(vlan_id, hdev->vlan_del_fail_bmap);
1601 		return -EBUSY;
1602 	}
1603 
1604 	hclgevf_build_send_msg(&send_msg, HCLGE_MBX_SET_VLAN,
1605 			       HCLGE_MBX_VLAN_FILTER);
1606 	send_msg.data[HCLGEVF_VLAN_MBX_IS_KILL_OFFSET] = is_kill;
1607 	memcpy(&send_msg.data[HCLGEVF_VLAN_MBX_VLAN_ID_OFFSET], &vlan_id,
1608 	       sizeof(vlan_id));
1609 	memcpy(&send_msg.data[HCLGEVF_VLAN_MBX_PROTO_OFFSET], &proto,
1610 	       sizeof(proto));
1611 	/* when remove hw vlan filter failed, record the vlan id,
1612 	 * and try to remove it from hw later, to be consistence
1613 	 * with stack.
1614 	 */
1615 	ret = hclgevf_send_mbx_msg(hdev, &send_msg, true, NULL, 0);
1616 	if (is_kill && ret)
1617 		set_bit(vlan_id, hdev->vlan_del_fail_bmap);
1618 
1619 	return ret;
1620 }
1621 
1622 static void hclgevf_sync_vlan_filter(struct hclgevf_dev *hdev)
1623 {
1624 #define HCLGEVF_MAX_SYNC_COUNT	60
1625 	struct hnae3_handle *handle = &hdev->nic;
1626 	int ret, sync_cnt = 0;
1627 	u16 vlan_id;
1628 
1629 	vlan_id = find_first_bit(hdev->vlan_del_fail_bmap, VLAN_N_VID);
1630 	while (vlan_id != VLAN_N_VID) {
1631 		ret = hclgevf_set_vlan_filter(handle, htons(ETH_P_8021Q),
1632 					      vlan_id, true);
1633 		if (ret)
1634 			return;
1635 
1636 		clear_bit(vlan_id, hdev->vlan_del_fail_bmap);
1637 		sync_cnt++;
1638 		if (sync_cnt >= HCLGEVF_MAX_SYNC_COUNT)
1639 			return;
1640 
1641 		vlan_id = find_first_bit(hdev->vlan_del_fail_bmap, VLAN_N_VID);
1642 	}
1643 }
1644 
1645 static int hclgevf_en_hw_strip_rxvtag(struct hnae3_handle *handle, bool enable)
1646 {
1647 	struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);
1648 	struct hclge_vf_to_pf_msg send_msg;
1649 
1650 	hclgevf_build_send_msg(&send_msg, HCLGE_MBX_SET_VLAN,
1651 			       HCLGE_MBX_VLAN_RX_OFF_CFG);
1652 	send_msg.data[0] = enable ? 1 : 0;
1653 	return hclgevf_send_mbx_msg(hdev, &send_msg, false, NULL, 0);
1654 }
1655 
1656 static int hclgevf_reset_tqp(struct hnae3_handle *handle, u16 queue_id)
1657 {
1658 	struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);
1659 	struct hclge_vf_to_pf_msg send_msg;
1660 	int ret;
1661 
1662 	/* disable vf queue before send queue reset msg to PF */
1663 	ret = hclgevf_tqp_enable(hdev, queue_id, 0, false);
1664 	if (ret)
1665 		return ret;
1666 
1667 	hclgevf_build_send_msg(&send_msg, HCLGE_MBX_QUEUE_RESET, 0);
1668 	memcpy(send_msg.data, &queue_id, sizeof(queue_id));
1669 	return hclgevf_send_mbx_msg(hdev, &send_msg, true, NULL, 0);
1670 }
1671 
1672 static int hclgevf_set_mtu(struct hnae3_handle *handle, int new_mtu)
1673 {
1674 	struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);
1675 	struct hclge_vf_to_pf_msg send_msg;
1676 
1677 	hclgevf_build_send_msg(&send_msg, HCLGE_MBX_SET_MTU, 0);
1678 	memcpy(send_msg.data, &new_mtu, sizeof(new_mtu));
1679 	return hclgevf_send_mbx_msg(hdev, &send_msg, true, NULL, 0);
1680 }
1681 
1682 static int hclgevf_notify_client(struct hclgevf_dev *hdev,
1683 				 enum hnae3_reset_notify_type type)
1684 {
1685 	struct hnae3_client *client = hdev->nic_client;
1686 	struct hnae3_handle *handle = &hdev->nic;
1687 	int ret;
1688 
1689 	if (!test_bit(HCLGEVF_STATE_NIC_REGISTERED, &hdev->state) ||
1690 	    !client)
1691 		return 0;
1692 
1693 	if (!client->ops->reset_notify)
1694 		return -EOPNOTSUPP;
1695 
1696 	ret = client->ops->reset_notify(handle, type);
1697 	if (ret)
1698 		dev_err(&hdev->pdev->dev, "notify nic client failed %d(%d)\n",
1699 			type, ret);
1700 
1701 	return ret;
1702 }
1703 
1704 static int hclgevf_reset_wait(struct hclgevf_dev *hdev)
1705 {
1706 #define HCLGEVF_RESET_WAIT_US	20000
1707 #define HCLGEVF_RESET_WAIT_CNT	2000
1708 #define HCLGEVF_RESET_WAIT_TIMEOUT_US	\
1709 	(HCLGEVF_RESET_WAIT_US * HCLGEVF_RESET_WAIT_CNT)
1710 
1711 	u32 val;
1712 	int ret;
1713 
1714 	if (hdev->reset_type == HNAE3_VF_RESET)
1715 		ret = readl_poll_timeout(hdev->hw.io_base +
1716 					 HCLGEVF_VF_RST_ING, val,
1717 					 !(val & HCLGEVF_VF_RST_ING_BIT),
1718 					 HCLGEVF_RESET_WAIT_US,
1719 					 HCLGEVF_RESET_WAIT_TIMEOUT_US);
1720 	else
1721 		ret = readl_poll_timeout(hdev->hw.io_base +
1722 					 HCLGEVF_RST_ING, val,
1723 					 !(val & HCLGEVF_RST_ING_BITS),
1724 					 HCLGEVF_RESET_WAIT_US,
1725 					 HCLGEVF_RESET_WAIT_TIMEOUT_US);
1726 
1727 	/* hardware completion status should be available by this time */
1728 	if (ret) {
1729 		dev_err(&hdev->pdev->dev,
1730 			"could'nt get reset done status from h/w, timeout!\n");
1731 		return ret;
1732 	}
1733 
1734 	/* we will wait a bit more to let reset of the stack to complete. This
1735 	 * might happen in case reset assertion was made by PF. Yes, this also
1736 	 * means we might end up waiting bit more even for VF reset.
1737 	 */
1738 	msleep(5000);
1739 
1740 	return 0;
1741 }
1742 
1743 static void hclgevf_reset_handshake(struct hclgevf_dev *hdev, bool enable)
1744 {
1745 	u32 reg_val;
1746 
1747 	reg_val = hclgevf_read_dev(&hdev->hw, HCLGEVF_NIC_CSQ_DEPTH_REG);
1748 	if (enable)
1749 		reg_val |= HCLGEVF_NIC_SW_RST_RDY;
1750 	else
1751 		reg_val &= ~HCLGEVF_NIC_SW_RST_RDY;
1752 
1753 	hclgevf_write_dev(&hdev->hw, HCLGEVF_NIC_CSQ_DEPTH_REG,
1754 			  reg_val);
1755 }
1756 
1757 static int hclgevf_reset_stack(struct hclgevf_dev *hdev)
1758 {
1759 	int ret;
1760 
1761 	/* uninitialize the nic client */
1762 	ret = hclgevf_notify_client(hdev, HNAE3_UNINIT_CLIENT);
1763 	if (ret)
1764 		return ret;
1765 
1766 	/* re-initialize the hclge device */
1767 	ret = hclgevf_reset_hdev(hdev);
1768 	if (ret) {
1769 		dev_err(&hdev->pdev->dev,
1770 			"hclge device re-init failed, VF is disabled!\n");
1771 		return ret;
1772 	}
1773 
1774 	/* bring up the nic client again */
1775 	ret = hclgevf_notify_client(hdev, HNAE3_INIT_CLIENT);
1776 	if (ret)
1777 		return ret;
1778 
1779 	/* clear handshake status with IMP */
1780 	hclgevf_reset_handshake(hdev, false);
1781 
1782 	/* bring up the nic to enable TX/RX again */
1783 	return hclgevf_notify_client(hdev, HNAE3_UP_CLIENT);
1784 }
1785 
1786 static int hclgevf_reset_prepare_wait(struct hclgevf_dev *hdev)
1787 {
1788 #define HCLGEVF_RESET_SYNC_TIME 100
1789 
1790 	struct hclge_vf_to_pf_msg send_msg;
1791 	int ret = 0;
1792 
1793 	if (hdev->reset_type == HNAE3_VF_FUNC_RESET) {
1794 		hclgevf_build_send_msg(&send_msg, HCLGE_MBX_RESET, 0);
1795 		ret = hclgevf_send_mbx_msg(hdev, &send_msg, true, NULL, 0);
1796 		hdev->rst_stats.vf_func_rst_cnt++;
1797 	}
1798 
1799 	set_bit(HCLGEVF_STATE_CMD_DISABLE, &hdev->state);
1800 	/* inform hardware that preparatory work is done */
1801 	msleep(HCLGEVF_RESET_SYNC_TIME);
1802 	hclgevf_reset_handshake(hdev, true);
1803 	dev_info(&hdev->pdev->dev, "prepare reset(%d) wait done, ret:%d\n",
1804 		 hdev->reset_type, ret);
1805 
1806 	return ret;
1807 }
1808 
1809 static void hclgevf_dump_rst_info(struct hclgevf_dev *hdev)
1810 {
1811 	dev_info(&hdev->pdev->dev, "VF function reset count: %u\n",
1812 		 hdev->rst_stats.vf_func_rst_cnt);
1813 	dev_info(&hdev->pdev->dev, "FLR reset count: %u\n",
1814 		 hdev->rst_stats.flr_rst_cnt);
1815 	dev_info(&hdev->pdev->dev, "VF reset count: %u\n",
1816 		 hdev->rst_stats.vf_rst_cnt);
1817 	dev_info(&hdev->pdev->dev, "reset done count: %u\n",
1818 		 hdev->rst_stats.rst_done_cnt);
1819 	dev_info(&hdev->pdev->dev, "HW reset done count: %u\n",
1820 		 hdev->rst_stats.hw_rst_done_cnt);
1821 	dev_info(&hdev->pdev->dev, "reset count: %u\n",
1822 		 hdev->rst_stats.rst_cnt);
1823 	dev_info(&hdev->pdev->dev, "reset fail count: %u\n",
1824 		 hdev->rst_stats.rst_fail_cnt);
1825 	dev_info(&hdev->pdev->dev, "vector0 interrupt enable status: 0x%x\n",
1826 		 hclgevf_read_dev(&hdev->hw, HCLGEVF_MISC_VECTOR_REG_BASE));
1827 	dev_info(&hdev->pdev->dev, "vector0 interrupt status: 0x%x\n",
1828 		 hclgevf_read_dev(&hdev->hw, HCLGEVF_VECTOR0_CMDQ_STATE_REG));
1829 	dev_info(&hdev->pdev->dev, "handshake status: 0x%x\n",
1830 		 hclgevf_read_dev(&hdev->hw, HCLGEVF_CMDQ_TX_DEPTH_REG));
1831 	dev_info(&hdev->pdev->dev, "function reset status: 0x%x\n",
1832 		 hclgevf_read_dev(&hdev->hw, HCLGEVF_RST_ING));
1833 	dev_info(&hdev->pdev->dev, "hdev state: 0x%lx\n", hdev->state);
1834 }
1835 
1836 static void hclgevf_reset_err_handle(struct hclgevf_dev *hdev)
1837 {
1838 	/* recover handshake status with IMP when reset fail */
1839 	hclgevf_reset_handshake(hdev, true);
1840 	hdev->rst_stats.rst_fail_cnt++;
1841 	dev_err(&hdev->pdev->dev, "failed to reset VF(%u)\n",
1842 		hdev->rst_stats.rst_fail_cnt);
1843 
1844 	if (hdev->rst_stats.rst_fail_cnt < HCLGEVF_RESET_MAX_FAIL_CNT)
1845 		set_bit(hdev->reset_type, &hdev->reset_pending);
1846 
1847 	if (hclgevf_is_reset_pending(hdev)) {
1848 		set_bit(HCLGEVF_RESET_PENDING, &hdev->reset_state);
1849 		hclgevf_reset_task_schedule(hdev);
1850 	} else {
1851 		set_bit(HCLGEVF_STATE_RST_FAIL, &hdev->state);
1852 		hclgevf_dump_rst_info(hdev);
1853 	}
1854 }
1855 
1856 static int hclgevf_reset_prepare(struct hclgevf_dev *hdev)
1857 {
1858 	int ret;
1859 
1860 	hdev->rst_stats.rst_cnt++;
1861 
1862 	rtnl_lock();
1863 	/* bring down the nic to stop any ongoing TX/RX */
1864 	ret = hclgevf_notify_client(hdev, HNAE3_DOWN_CLIENT);
1865 	rtnl_unlock();
1866 	if (ret)
1867 		return ret;
1868 
1869 	return hclgevf_reset_prepare_wait(hdev);
1870 }
1871 
1872 static int hclgevf_reset_rebuild(struct hclgevf_dev *hdev)
1873 {
1874 	int ret;
1875 
1876 	hdev->rst_stats.hw_rst_done_cnt++;
1877 
1878 	rtnl_lock();
1879 	/* now, re-initialize the nic client and ae device */
1880 	ret = hclgevf_reset_stack(hdev);
1881 	rtnl_unlock();
1882 	if (ret) {
1883 		dev_err(&hdev->pdev->dev, "failed to reset VF stack\n");
1884 		return ret;
1885 	}
1886 
1887 	hdev->last_reset_time = jiffies;
1888 	hdev->rst_stats.rst_done_cnt++;
1889 	hdev->rst_stats.rst_fail_cnt = 0;
1890 	clear_bit(HCLGEVF_STATE_RST_FAIL, &hdev->state);
1891 
1892 	return 0;
1893 }
1894 
1895 static void hclgevf_reset(struct hclgevf_dev *hdev)
1896 {
1897 	if (hclgevf_reset_prepare(hdev))
1898 		goto err_reset;
1899 
1900 	/* check if VF could successfully fetch the hardware reset completion
1901 	 * status from the hardware
1902 	 */
1903 	if (hclgevf_reset_wait(hdev)) {
1904 		/* can't do much in this situation, will disable VF */
1905 		dev_err(&hdev->pdev->dev,
1906 			"failed to fetch H/W reset completion status\n");
1907 		goto err_reset;
1908 	}
1909 
1910 	if (hclgevf_reset_rebuild(hdev))
1911 		goto err_reset;
1912 
1913 	return;
1914 
1915 err_reset:
1916 	hclgevf_reset_err_handle(hdev);
1917 }
1918 
1919 static enum hnae3_reset_type hclgevf_get_reset_level(struct hclgevf_dev *hdev,
1920 						     unsigned long *addr)
1921 {
1922 	enum hnae3_reset_type rst_level = HNAE3_NONE_RESET;
1923 
1924 	/* return the highest priority reset level amongst all */
1925 	if (test_bit(HNAE3_VF_RESET, addr)) {
1926 		rst_level = HNAE3_VF_RESET;
1927 		clear_bit(HNAE3_VF_RESET, addr);
1928 		clear_bit(HNAE3_VF_PF_FUNC_RESET, addr);
1929 		clear_bit(HNAE3_VF_FUNC_RESET, addr);
1930 	} else if (test_bit(HNAE3_VF_FULL_RESET, addr)) {
1931 		rst_level = HNAE3_VF_FULL_RESET;
1932 		clear_bit(HNAE3_VF_FULL_RESET, addr);
1933 		clear_bit(HNAE3_VF_FUNC_RESET, addr);
1934 	} else if (test_bit(HNAE3_VF_PF_FUNC_RESET, addr)) {
1935 		rst_level = HNAE3_VF_PF_FUNC_RESET;
1936 		clear_bit(HNAE3_VF_PF_FUNC_RESET, addr);
1937 		clear_bit(HNAE3_VF_FUNC_RESET, addr);
1938 	} else if (test_bit(HNAE3_VF_FUNC_RESET, addr)) {
1939 		rst_level = HNAE3_VF_FUNC_RESET;
1940 		clear_bit(HNAE3_VF_FUNC_RESET, addr);
1941 	} else if (test_bit(HNAE3_FLR_RESET, addr)) {
1942 		rst_level = HNAE3_FLR_RESET;
1943 		clear_bit(HNAE3_FLR_RESET, addr);
1944 	}
1945 
1946 	return rst_level;
1947 }
1948 
1949 static void hclgevf_reset_event(struct pci_dev *pdev,
1950 				struct hnae3_handle *handle)
1951 {
1952 	struct hnae3_ae_dev *ae_dev = pci_get_drvdata(pdev);
1953 	struct hclgevf_dev *hdev = ae_dev->priv;
1954 
1955 	dev_info(&hdev->pdev->dev, "received reset request from VF enet\n");
1956 
1957 	if (hdev->default_reset_request)
1958 		hdev->reset_level =
1959 			hclgevf_get_reset_level(hdev,
1960 						&hdev->default_reset_request);
1961 	else
1962 		hdev->reset_level = HNAE3_VF_FUNC_RESET;
1963 
1964 	/* reset of this VF requested */
1965 	set_bit(HCLGEVF_RESET_REQUESTED, &hdev->reset_state);
1966 	hclgevf_reset_task_schedule(hdev);
1967 
1968 	hdev->last_reset_time = jiffies;
1969 }
1970 
1971 static void hclgevf_set_def_reset_request(struct hnae3_ae_dev *ae_dev,
1972 					  enum hnae3_reset_type rst_type)
1973 {
1974 	struct hclgevf_dev *hdev = ae_dev->priv;
1975 
1976 	set_bit(rst_type, &hdev->default_reset_request);
1977 }
1978 
1979 static void hclgevf_enable_vector(struct hclgevf_misc_vector *vector, bool en)
1980 {
1981 	writel(en ? 1 : 0, vector->addr);
1982 }
1983 
1984 static void hclgevf_flr_prepare(struct hnae3_ae_dev *ae_dev)
1985 {
1986 #define HCLGEVF_FLR_RETRY_WAIT_MS	500
1987 #define HCLGEVF_FLR_RETRY_CNT		5
1988 
1989 	struct hclgevf_dev *hdev = ae_dev->priv;
1990 	int retry_cnt = 0;
1991 	int ret;
1992 
1993 retry:
1994 	down(&hdev->reset_sem);
1995 	set_bit(HCLGEVF_STATE_RST_HANDLING, &hdev->state);
1996 	hdev->reset_type = HNAE3_FLR_RESET;
1997 	ret = hclgevf_reset_prepare(hdev);
1998 	if (ret) {
1999 		dev_err(&hdev->pdev->dev, "fail to prepare FLR, ret=%d\n",
2000 			ret);
2001 		if (hdev->reset_pending ||
2002 		    retry_cnt++ < HCLGEVF_FLR_RETRY_CNT) {
2003 			dev_err(&hdev->pdev->dev,
2004 				"reset_pending:0x%lx, retry_cnt:%d\n",
2005 				hdev->reset_pending, retry_cnt);
2006 			clear_bit(HCLGEVF_STATE_RST_HANDLING, &hdev->state);
2007 			up(&hdev->reset_sem);
2008 			msleep(HCLGEVF_FLR_RETRY_WAIT_MS);
2009 			goto retry;
2010 		}
2011 	}
2012 
2013 	/* disable misc vector before FLR done */
2014 	hclgevf_enable_vector(&hdev->misc_vector, false);
2015 	hdev->rst_stats.flr_rst_cnt++;
2016 }
2017 
2018 static void hclgevf_flr_done(struct hnae3_ae_dev *ae_dev)
2019 {
2020 	struct hclgevf_dev *hdev = ae_dev->priv;
2021 	int ret;
2022 
2023 	hclgevf_enable_vector(&hdev->misc_vector, true);
2024 
2025 	ret = hclgevf_reset_rebuild(hdev);
2026 	if (ret)
2027 		dev_warn(&hdev->pdev->dev, "fail to rebuild, ret=%d\n",
2028 			 ret);
2029 
2030 	hdev->reset_type = HNAE3_NONE_RESET;
2031 	clear_bit(HCLGEVF_STATE_RST_HANDLING, &hdev->state);
2032 	up(&hdev->reset_sem);
2033 }
2034 
2035 static u32 hclgevf_get_fw_version(struct hnae3_handle *handle)
2036 {
2037 	struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);
2038 
2039 	return hdev->fw_version;
2040 }
2041 
2042 static void hclgevf_get_misc_vector(struct hclgevf_dev *hdev)
2043 {
2044 	struct hclgevf_misc_vector *vector = &hdev->misc_vector;
2045 
2046 	vector->vector_irq = pci_irq_vector(hdev->pdev,
2047 					    HCLGEVF_MISC_VECTOR_NUM);
2048 	vector->addr = hdev->hw.io_base + HCLGEVF_MISC_VECTOR_REG_BASE;
2049 	/* vector status always valid for Vector 0 */
2050 	hdev->vector_status[HCLGEVF_MISC_VECTOR_NUM] = 0;
2051 	hdev->vector_irq[HCLGEVF_MISC_VECTOR_NUM] = vector->vector_irq;
2052 
2053 	hdev->num_msi_left -= 1;
2054 	hdev->num_msi_used += 1;
2055 }
2056 
2057 void hclgevf_reset_task_schedule(struct hclgevf_dev *hdev)
2058 {
2059 	if (!test_bit(HCLGEVF_STATE_REMOVING, &hdev->state) &&
2060 	    !test_and_set_bit(HCLGEVF_STATE_RST_SERVICE_SCHED,
2061 			      &hdev->state))
2062 		mod_delayed_work(hclgevf_wq, &hdev->service_task, 0);
2063 }
2064 
2065 void hclgevf_mbx_task_schedule(struct hclgevf_dev *hdev)
2066 {
2067 	if (!test_bit(HCLGEVF_STATE_REMOVING, &hdev->state) &&
2068 	    !test_and_set_bit(HCLGEVF_STATE_MBX_SERVICE_SCHED,
2069 			      &hdev->state))
2070 		mod_delayed_work(hclgevf_wq, &hdev->service_task, 0);
2071 }
2072 
2073 static void hclgevf_task_schedule(struct hclgevf_dev *hdev,
2074 				  unsigned long delay)
2075 {
2076 	if (!test_bit(HCLGEVF_STATE_REMOVING, &hdev->state) &&
2077 	    !test_bit(HCLGEVF_STATE_RST_FAIL, &hdev->state))
2078 		mod_delayed_work(hclgevf_wq, &hdev->service_task, delay);
2079 }
2080 
2081 static void hclgevf_reset_service_task(struct hclgevf_dev *hdev)
2082 {
2083 #define	HCLGEVF_MAX_RESET_ATTEMPTS_CNT	3
2084 
2085 	if (!test_and_clear_bit(HCLGEVF_STATE_RST_SERVICE_SCHED, &hdev->state))
2086 		return;
2087 
2088 	down(&hdev->reset_sem);
2089 	set_bit(HCLGEVF_STATE_RST_HANDLING, &hdev->state);
2090 
2091 	if (test_and_clear_bit(HCLGEVF_RESET_PENDING,
2092 			       &hdev->reset_state)) {
2093 		/* PF has initmated that it is about to reset the hardware.
2094 		 * We now have to poll & check if hardware has actually
2095 		 * completed the reset sequence. On hardware reset completion,
2096 		 * VF needs to reset the client and ae device.
2097 		 */
2098 		hdev->reset_attempts = 0;
2099 
2100 		hdev->last_reset_time = jiffies;
2101 		while ((hdev->reset_type =
2102 			hclgevf_get_reset_level(hdev, &hdev->reset_pending))
2103 		       != HNAE3_NONE_RESET)
2104 			hclgevf_reset(hdev);
2105 	} else if (test_and_clear_bit(HCLGEVF_RESET_REQUESTED,
2106 				      &hdev->reset_state)) {
2107 		/* we could be here when either of below happens:
2108 		 * 1. reset was initiated due to watchdog timeout caused by
2109 		 *    a. IMP was earlier reset and our TX got choked down and
2110 		 *       which resulted in watchdog reacting and inducing VF
2111 		 *       reset. This also means our cmdq would be unreliable.
2112 		 *    b. problem in TX due to other lower layer(example link
2113 		 *       layer not functioning properly etc.)
2114 		 * 2. VF reset might have been initiated due to some config
2115 		 *    change.
2116 		 *
2117 		 * NOTE: Theres no clear way to detect above cases than to react
2118 		 * to the response of PF for this reset request. PF will ack the
2119 		 * 1b and 2. cases but we will not get any intimation about 1a
2120 		 * from PF as cmdq would be in unreliable state i.e. mailbox
2121 		 * communication between PF and VF would be broken.
2122 		 *
2123 		 * if we are never geting into pending state it means either:
2124 		 * 1. PF is not receiving our request which could be due to IMP
2125 		 *    reset
2126 		 * 2. PF is screwed
2127 		 * We cannot do much for 2. but to check first we can try reset
2128 		 * our PCIe + stack and see if it alleviates the problem.
2129 		 */
2130 		if (hdev->reset_attempts > HCLGEVF_MAX_RESET_ATTEMPTS_CNT) {
2131 			/* prepare for full reset of stack + pcie interface */
2132 			set_bit(HNAE3_VF_FULL_RESET, &hdev->reset_pending);
2133 
2134 			/* "defer" schedule the reset task again */
2135 			set_bit(HCLGEVF_RESET_PENDING, &hdev->reset_state);
2136 		} else {
2137 			hdev->reset_attempts++;
2138 
2139 			set_bit(hdev->reset_level, &hdev->reset_pending);
2140 			set_bit(HCLGEVF_RESET_PENDING, &hdev->reset_state);
2141 		}
2142 		hclgevf_reset_task_schedule(hdev);
2143 	}
2144 
2145 	hdev->reset_type = HNAE3_NONE_RESET;
2146 	clear_bit(HCLGEVF_STATE_RST_HANDLING, &hdev->state);
2147 	up(&hdev->reset_sem);
2148 }
2149 
2150 static void hclgevf_mailbox_service_task(struct hclgevf_dev *hdev)
2151 {
2152 	if (!test_and_clear_bit(HCLGEVF_STATE_MBX_SERVICE_SCHED, &hdev->state))
2153 		return;
2154 
2155 	if (test_and_set_bit(HCLGEVF_STATE_MBX_HANDLING, &hdev->state))
2156 		return;
2157 
2158 	hclgevf_mbx_async_handler(hdev);
2159 
2160 	clear_bit(HCLGEVF_STATE_MBX_HANDLING, &hdev->state);
2161 }
2162 
2163 static void hclgevf_keep_alive(struct hclgevf_dev *hdev)
2164 {
2165 	struct hclge_vf_to_pf_msg send_msg;
2166 	int ret;
2167 
2168 	if (test_bit(HCLGEVF_STATE_CMD_DISABLE, &hdev->state))
2169 		return;
2170 
2171 	hclgevf_build_send_msg(&send_msg, HCLGE_MBX_KEEP_ALIVE, 0);
2172 	ret = hclgevf_send_mbx_msg(hdev, &send_msg, false, NULL, 0);
2173 	if (ret)
2174 		dev_err(&hdev->pdev->dev,
2175 			"VF sends keep alive cmd failed(=%d)\n", ret);
2176 }
2177 
2178 static void hclgevf_periodic_service_task(struct hclgevf_dev *hdev)
2179 {
2180 	unsigned long delta = round_jiffies_relative(HZ);
2181 	struct hnae3_handle *handle = &hdev->nic;
2182 
2183 	if (time_is_after_jiffies(hdev->last_serv_processed + HZ)) {
2184 		delta = jiffies - hdev->last_serv_processed;
2185 
2186 		if (delta < round_jiffies_relative(HZ)) {
2187 			delta = round_jiffies_relative(HZ) - delta;
2188 			goto out;
2189 		}
2190 	}
2191 
2192 	hdev->serv_processed_cnt++;
2193 	if (!(hdev->serv_processed_cnt % HCLGEVF_KEEP_ALIVE_TASK_INTERVAL))
2194 		hclgevf_keep_alive(hdev);
2195 
2196 	if (test_bit(HCLGEVF_STATE_DOWN, &hdev->state)) {
2197 		hdev->last_serv_processed = jiffies;
2198 		goto out;
2199 	}
2200 
2201 	if (!(hdev->serv_processed_cnt % HCLGEVF_STATS_TIMER_INTERVAL))
2202 		hclgevf_tqps_update_stats(handle);
2203 
2204 	/* request the link status from the PF. PF would be able to tell VF
2205 	 * about such updates in future so we might remove this later
2206 	 */
2207 	hclgevf_request_link_info(hdev);
2208 
2209 	hclgevf_update_link_mode(hdev);
2210 
2211 	hclgevf_sync_vlan_filter(hdev);
2212 
2213 	hclgevf_sync_mac_table(hdev);
2214 
2215 	hclgevf_sync_promisc_mode(hdev);
2216 
2217 	hdev->last_serv_processed = jiffies;
2218 
2219 out:
2220 	hclgevf_task_schedule(hdev, delta);
2221 }
2222 
2223 static void hclgevf_service_task(struct work_struct *work)
2224 {
2225 	struct hclgevf_dev *hdev = container_of(work, struct hclgevf_dev,
2226 						service_task.work);
2227 
2228 	hclgevf_reset_service_task(hdev);
2229 	hclgevf_mailbox_service_task(hdev);
2230 	hclgevf_periodic_service_task(hdev);
2231 
2232 	/* Handle reset and mbx again in case periodical task delays the
2233 	 * handling by calling hclgevf_task_schedule() in
2234 	 * hclgevf_periodic_service_task()
2235 	 */
2236 	hclgevf_reset_service_task(hdev);
2237 	hclgevf_mailbox_service_task(hdev);
2238 }
2239 
2240 static void hclgevf_clear_event_cause(struct hclgevf_dev *hdev, u32 regclr)
2241 {
2242 	hclgevf_write_dev(&hdev->hw, HCLGEVF_VECTOR0_CMDQ_SRC_REG, regclr);
2243 }
2244 
2245 static enum hclgevf_evt_cause hclgevf_check_evt_cause(struct hclgevf_dev *hdev,
2246 						      u32 *clearval)
2247 {
2248 	u32 val, cmdq_stat_reg, rst_ing_reg;
2249 
2250 	/* fetch the events from their corresponding regs */
2251 	cmdq_stat_reg = hclgevf_read_dev(&hdev->hw,
2252 					 HCLGEVF_VECTOR0_CMDQ_STATE_REG);
2253 
2254 	if (BIT(HCLGEVF_VECTOR0_RST_INT_B) & cmdq_stat_reg) {
2255 		rst_ing_reg = hclgevf_read_dev(&hdev->hw, HCLGEVF_RST_ING);
2256 		dev_info(&hdev->pdev->dev,
2257 			 "receive reset interrupt 0x%x!\n", rst_ing_reg);
2258 		set_bit(HNAE3_VF_RESET, &hdev->reset_pending);
2259 		set_bit(HCLGEVF_RESET_PENDING, &hdev->reset_state);
2260 		set_bit(HCLGEVF_STATE_CMD_DISABLE, &hdev->state);
2261 		*clearval = ~(1U << HCLGEVF_VECTOR0_RST_INT_B);
2262 		hdev->rst_stats.vf_rst_cnt++;
2263 		/* set up VF hardware reset status, its PF will clear
2264 		 * this status when PF has initialized done.
2265 		 */
2266 		val = hclgevf_read_dev(&hdev->hw, HCLGEVF_VF_RST_ING);
2267 		hclgevf_write_dev(&hdev->hw, HCLGEVF_VF_RST_ING,
2268 				  val | HCLGEVF_VF_RST_ING_BIT);
2269 		return HCLGEVF_VECTOR0_EVENT_RST;
2270 	}
2271 
2272 	/* check for vector0 mailbox(=CMDQ RX) event source */
2273 	if (BIT(HCLGEVF_VECTOR0_RX_CMDQ_INT_B) & cmdq_stat_reg) {
2274 		/* for revision 0x21, clearing interrupt is writing bit 0
2275 		 * to the clear register, writing bit 1 means to keep the
2276 		 * old value.
2277 		 * for revision 0x20, the clear register is a read & write
2278 		 * register, so we should just write 0 to the bit we are
2279 		 * handling, and keep other bits as cmdq_stat_reg.
2280 		 */
2281 		if (hdev->pdev->revision >= 0x21)
2282 			*clearval = ~(1U << HCLGEVF_VECTOR0_RX_CMDQ_INT_B);
2283 		else
2284 			*clearval = cmdq_stat_reg &
2285 				    ~BIT(HCLGEVF_VECTOR0_RX_CMDQ_INT_B);
2286 
2287 		return HCLGEVF_VECTOR0_EVENT_MBX;
2288 	}
2289 
2290 	/* print other vector0 event source */
2291 	dev_info(&hdev->pdev->dev,
2292 		 "vector 0 interrupt from unknown source, cmdq_src = %#x\n",
2293 		 cmdq_stat_reg);
2294 
2295 	return HCLGEVF_VECTOR0_EVENT_OTHER;
2296 }
2297 
2298 static irqreturn_t hclgevf_misc_irq_handle(int irq, void *data)
2299 {
2300 	enum hclgevf_evt_cause event_cause;
2301 	struct hclgevf_dev *hdev = data;
2302 	u32 clearval;
2303 
2304 	hclgevf_enable_vector(&hdev->misc_vector, false);
2305 	event_cause = hclgevf_check_evt_cause(hdev, &clearval);
2306 
2307 	switch (event_cause) {
2308 	case HCLGEVF_VECTOR0_EVENT_RST:
2309 		hclgevf_reset_task_schedule(hdev);
2310 		break;
2311 	case HCLGEVF_VECTOR0_EVENT_MBX:
2312 		hclgevf_mbx_handler(hdev);
2313 		break;
2314 	default:
2315 		break;
2316 	}
2317 
2318 	if (event_cause != HCLGEVF_VECTOR0_EVENT_OTHER) {
2319 		hclgevf_clear_event_cause(hdev, clearval);
2320 		hclgevf_enable_vector(&hdev->misc_vector, true);
2321 	}
2322 
2323 	return IRQ_HANDLED;
2324 }
2325 
2326 static int hclgevf_configure(struct hclgevf_dev *hdev)
2327 {
2328 	int ret;
2329 
2330 	/* get current port based vlan state from PF */
2331 	ret = hclgevf_get_port_base_vlan_filter_state(hdev);
2332 	if (ret)
2333 		return ret;
2334 
2335 	/* get queue configuration from PF */
2336 	ret = hclgevf_get_queue_info(hdev);
2337 	if (ret)
2338 		return ret;
2339 
2340 	/* get queue depth info from PF */
2341 	ret = hclgevf_get_queue_depth(hdev);
2342 	if (ret)
2343 		return ret;
2344 
2345 	ret = hclgevf_get_pf_media_type(hdev);
2346 	if (ret)
2347 		return ret;
2348 
2349 	/* get tc configuration from PF */
2350 	return hclgevf_get_tc_info(hdev);
2351 }
2352 
2353 static int hclgevf_alloc_hdev(struct hnae3_ae_dev *ae_dev)
2354 {
2355 	struct pci_dev *pdev = ae_dev->pdev;
2356 	struct hclgevf_dev *hdev;
2357 
2358 	hdev = devm_kzalloc(&pdev->dev, sizeof(*hdev), GFP_KERNEL);
2359 	if (!hdev)
2360 		return -ENOMEM;
2361 
2362 	hdev->pdev = pdev;
2363 	hdev->ae_dev = ae_dev;
2364 	ae_dev->priv = hdev;
2365 
2366 	return 0;
2367 }
2368 
2369 static int hclgevf_init_roce_base_info(struct hclgevf_dev *hdev)
2370 {
2371 	struct hnae3_handle *roce = &hdev->roce;
2372 	struct hnae3_handle *nic = &hdev->nic;
2373 
2374 	roce->rinfo.num_vectors = hdev->num_roce_msix;
2375 
2376 	if (hdev->num_msi_left < roce->rinfo.num_vectors ||
2377 	    hdev->num_msi_left == 0)
2378 		return -EINVAL;
2379 
2380 	roce->rinfo.base_vector = hdev->roce_base_vector;
2381 
2382 	roce->rinfo.netdev = nic->kinfo.netdev;
2383 	roce->rinfo.roce_io_base = hdev->hw.io_base;
2384 
2385 	roce->pdev = nic->pdev;
2386 	roce->ae_algo = nic->ae_algo;
2387 	roce->numa_node_mask = nic->numa_node_mask;
2388 
2389 	return 0;
2390 }
2391 
2392 static int hclgevf_config_gro(struct hclgevf_dev *hdev, bool en)
2393 {
2394 	struct hclgevf_cfg_gro_status_cmd *req;
2395 	struct hclgevf_desc desc;
2396 	int ret;
2397 
2398 	if (!hnae3_dev_gro_supported(hdev))
2399 		return 0;
2400 
2401 	hclgevf_cmd_setup_basic_desc(&desc, HCLGEVF_OPC_GRO_GENERIC_CONFIG,
2402 				     false);
2403 	req = (struct hclgevf_cfg_gro_status_cmd *)desc.data;
2404 
2405 	req->gro_en = en ? 1 : 0;
2406 
2407 	ret = hclgevf_cmd_send(&hdev->hw, &desc, 1);
2408 	if (ret)
2409 		dev_err(&hdev->pdev->dev,
2410 			"VF GRO hardware config cmd failed, ret = %d.\n", ret);
2411 
2412 	return ret;
2413 }
2414 
2415 static void hclgevf_rss_init_cfg(struct hclgevf_dev *hdev)
2416 {
2417 	struct hclgevf_rss_cfg *rss_cfg = &hdev->rss_cfg;
2418 	struct hclgevf_rss_tuple_cfg *tuple_sets;
2419 	u32 i;
2420 
2421 	rss_cfg->hash_algo = HCLGEVF_RSS_HASH_ALGO_TOEPLITZ;
2422 	rss_cfg->rss_size = hdev->nic.kinfo.rss_size;
2423 	tuple_sets = &rss_cfg->rss_tuple_sets;
2424 	if (hdev->pdev->revision >= 0x21) {
2425 		rss_cfg->hash_algo = HCLGEVF_RSS_HASH_ALGO_SIMPLE;
2426 		memcpy(rss_cfg->rss_hash_key, hclgevf_hash_key,
2427 		       HCLGEVF_RSS_KEY_SIZE);
2428 
2429 		tuple_sets->ipv4_tcp_en = HCLGEVF_RSS_INPUT_TUPLE_OTHER;
2430 		tuple_sets->ipv4_udp_en = HCLGEVF_RSS_INPUT_TUPLE_OTHER;
2431 		tuple_sets->ipv4_sctp_en = HCLGEVF_RSS_INPUT_TUPLE_SCTP;
2432 		tuple_sets->ipv4_fragment_en = HCLGEVF_RSS_INPUT_TUPLE_OTHER;
2433 		tuple_sets->ipv6_tcp_en = HCLGEVF_RSS_INPUT_TUPLE_OTHER;
2434 		tuple_sets->ipv6_udp_en = HCLGEVF_RSS_INPUT_TUPLE_OTHER;
2435 		tuple_sets->ipv6_sctp_en = HCLGEVF_RSS_INPUT_TUPLE_SCTP;
2436 		tuple_sets->ipv6_fragment_en = HCLGEVF_RSS_INPUT_TUPLE_OTHER;
2437 	}
2438 
2439 	/* Initialize RSS indirect table */
2440 	for (i = 0; i < HCLGEVF_RSS_IND_TBL_SIZE; i++)
2441 		rss_cfg->rss_indirection_tbl[i] = i % rss_cfg->rss_size;
2442 }
2443 
2444 static int hclgevf_rss_init_hw(struct hclgevf_dev *hdev)
2445 {
2446 	struct hclgevf_rss_cfg *rss_cfg = &hdev->rss_cfg;
2447 	int ret;
2448 
2449 	if (hdev->pdev->revision >= 0x21) {
2450 		ret = hclgevf_set_rss_algo_key(hdev, rss_cfg->hash_algo,
2451 					       rss_cfg->rss_hash_key);
2452 		if (ret)
2453 			return ret;
2454 
2455 		ret = hclgevf_set_rss_input_tuple(hdev, rss_cfg);
2456 		if (ret)
2457 			return ret;
2458 	}
2459 
2460 	ret = hclgevf_set_rss_indir_table(hdev);
2461 	if (ret)
2462 		return ret;
2463 
2464 	return hclgevf_set_rss_tc_mode(hdev, rss_cfg->rss_size);
2465 }
2466 
2467 static int hclgevf_init_vlan_config(struct hclgevf_dev *hdev)
2468 {
2469 	return hclgevf_set_vlan_filter(&hdev->nic, htons(ETH_P_8021Q), 0,
2470 				       false);
2471 }
2472 
2473 static void hclgevf_flush_link_update(struct hclgevf_dev *hdev)
2474 {
2475 #define HCLGEVF_FLUSH_LINK_TIMEOUT	100000
2476 
2477 	unsigned long last = hdev->serv_processed_cnt;
2478 	int i = 0;
2479 
2480 	while (test_bit(HCLGEVF_STATE_LINK_UPDATING, &hdev->state) &&
2481 	       i++ < HCLGEVF_FLUSH_LINK_TIMEOUT &&
2482 	       last == hdev->serv_processed_cnt)
2483 		usleep_range(1, 1);
2484 }
2485 
2486 static void hclgevf_set_timer_task(struct hnae3_handle *handle, bool enable)
2487 {
2488 	struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);
2489 
2490 	if (enable) {
2491 		hclgevf_task_schedule(hdev, 0);
2492 	} else {
2493 		set_bit(HCLGEVF_STATE_DOWN, &hdev->state);
2494 
2495 		/* flush memory to make sure DOWN is seen by service task */
2496 		smp_mb__before_atomic();
2497 		hclgevf_flush_link_update(hdev);
2498 	}
2499 }
2500 
2501 static int hclgevf_ae_start(struct hnae3_handle *handle)
2502 {
2503 	struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);
2504 
2505 	hclgevf_reset_tqp_stats(handle);
2506 
2507 	hclgevf_request_link_info(hdev);
2508 
2509 	hclgevf_update_link_mode(hdev);
2510 
2511 	clear_bit(HCLGEVF_STATE_DOWN, &hdev->state);
2512 
2513 	return 0;
2514 }
2515 
2516 static void hclgevf_ae_stop(struct hnae3_handle *handle)
2517 {
2518 	struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);
2519 	int i;
2520 
2521 	set_bit(HCLGEVF_STATE_DOWN, &hdev->state);
2522 
2523 	if (hdev->reset_type != HNAE3_VF_RESET)
2524 		for (i = 0; i < handle->kinfo.num_tqps; i++)
2525 			if (hclgevf_reset_tqp(handle, i))
2526 				break;
2527 
2528 	hclgevf_reset_tqp_stats(handle);
2529 	hclgevf_update_link_status(hdev, 0);
2530 }
2531 
2532 static int hclgevf_set_alive(struct hnae3_handle *handle, bool alive)
2533 {
2534 #define HCLGEVF_STATE_ALIVE	1
2535 #define HCLGEVF_STATE_NOT_ALIVE	0
2536 
2537 	struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);
2538 	struct hclge_vf_to_pf_msg send_msg;
2539 
2540 	hclgevf_build_send_msg(&send_msg, HCLGE_MBX_SET_ALIVE, 0);
2541 	send_msg.data[0] = alive ? HCLGEVF_STATE_ALIVE :
2542 				HCLGEVF_STATE_NOT_ALIVE;
2543 	return hclgevf_send_mbx_msg(hdev, &send_msg, false, NULL, 0);
2544 }
2545 
2546 static int hclgevf_client_start(struct hnae3_handle *handle)
2547 {
2548 	int ret;
2549 
2550 	ret = hclgevf_set_alive(handle, true);
2551 	if (ret)
2552 		return ret;
2553 
2554 	return 0;
2555 }
2556 
2557 static void hclgevf_client_stop(struct hnae3_handle *handle)
2558 {
2559 	struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);
2560 	int ret;
2561 
2562 	ret = hclgevf_set_alive(handle, false);
2563 	if (ret)
2564 		dev_warn(&hdev->pdev->dev,
2565 			 "%s failed %d\n", __func__, ret);
2566 }
2567 
2568 static void hclgevf_state_init(struct hclgevf_dev *hdev)
2569 {
2570 	clear_bit(HCLGEVF_STATE_MBX_SERVICE_SCHED, &hdev->state);
2571 	clear_bit(HCLGEVF_STATE_MBX_HANDLING, &hdev->state);
2572 	clear_bit(HCLGEVF_STATE_RST_FAIL, &hdev->state);
2573 
2574 	INIT_DELAYED_WORK(&hdev->service_task, hclgevf_service_task);
2575 
2576 	mutex_init(&hdev->mbx_resp.mbx_mutex);
2577 	sema_init(&hdev->reset_sem, 1);
2578 
2579 	spin_lock_init(&hdev->mac_table.mac_list_lock);
2580 	INIT_LIST_HEAD(&hdev->mac_table.uc_mac_list);
2581 	INIT_LIST_HEAD(&hdev->mac_table.mc_mac_list);
2582 
2583 	/* bring the device down */
2584 	set_bit(HCLGEVF_STATE_DOWN, &hdev->state);
2585 }
2586 
2587 static void hclgevf_state_uninit(struct hclgevf_dev *hdev)
2588 {
2589 	set_bit(HCLGEVF_STATE_DOWN, &hdev->state);
2590 	set_bit(HCLGEVF_STATE_REMOVING, &hdev->state);
2591 
2592 	if (hdev->service_task.work.func)
2593 		cancel_delayed_work_sync(&hdev->service_task);
2594 
2595 	mutex_destroy(&hdev->mbx_resp.mbx_mutex);
2596 }
2597 
2598 static int hclgevf_init_msi(struct hclgevf_dev *hdev)
2599 {
2600 	struct pci_dev *pdev = hdev->pdev;
2601 	int vectors;
2602 	int i;
2603 
2604 	if (hnae3_dev_roce_supported(hdev))
2605 		vectors = pci_alloc_irq_vectors(pdev,
2606 						hdev->roce_base_msix_offset + 1,
2607 						hdev->num_msi,
2608 						PCI_IRQ_MSIX);
2609 	else
2610 		vectors = pci_alloc_irq_vectors(pdev, HNAE3_MIN_VECTOR_NUM,
2611 						hdev->num_msi,
2612 						PCI_IRQ_MSI | PCI_IRQ_MSIX);
2613 
2614 	if (vectors < 0) {
2615 		dev_err(&pdev->dev,
2616 			"failed(%d) to allocate MSI/MSI-X vectors\n",
2617 			vectors);
2618 		return vectors;
2619 	}
2620 	if (vectors < hdev->num_msi)
2621 		dev_warn(&hdev->pdev->dev,
2622 			 "requested %u MSI/MSI-X, but allocated %d MSI/MSI-X\n",
2623 			 hdev->num_msi, vectors);
2624 
2625 	hdev->num_msi = vectors;
2626 	hdev->num_msi_left = vectors;
2627 
2628 	hdev->base_msi_vector = pdev->irq;
2629 	hdev->roce_base_vector = pdev->irq + hdev->roce_base_msix_offset;
2630 
2631 	hdev->vector_status = devm_kcalloc(&pdev->dev, hdev->num_msi,
2632 					   sizeof(u16), GFP_KERNEL);
2633 	if (!hdev->vector_status) {
2634 		pci_free_irq_vectors(pdev);
2635 		return -ENOMEM;
2636 	}
2637 
2638 	for (i = 0; i < hdev->num_msi; i++)
2639 		hdev->vector_status[i] = HCLGEVF_INVALID_VPORT;
2640 
2641 	hdev->vector_irq = devm_kcalloc(&pdev->dev, hdev->num_msi,
2642 					sizeof(int), GFP_KERNEL);
2643 	if (!hdev->vector_irq) {
2644 		devm_kfree(&pdev->dev, hdev->vector_status);
2645 		pci_free_irq_vectors(pdev);
2646 		return -ENOMEM;
2647 	}
2648 
2649 	return 0;
2650 }
2651 
2652 static void hclgevf_uninit_msi(struct hclgevf_dev *hdev)
2653 {
2654 	struct pci_dev *pdev = hdev->pdev;
2655 
2656 	devm_kfree(&pdev->dev, hdev->vector_status);
2657 	devm_kfree(&pdev->dev, hdev->vector_irq);
2658 	pci_free_irq_vectors(pdev);
2659 }
2660 
2661 static int hclgevf_misc_irq_init(struct hclgevf_dev *hdev)
2662 {
2663 	int ret;
2664 
2665 	hclgevf_get_misc_vector(hdev);
2666 
2667 	snprintf(hdev->misc_vector.name, HNAE3_INT_NAME_LEN, "%s-misc-%s",
2668 		 HCLGEVF_NAME, pci_name(hdev->pdev));
2669 	ret = request_irq(hdev->misc_vector.vector_irq, hclgevf_misc_irq_handle,
2670 			  0, hdev->misc_vector.name, hdev);
2671 	if (ret) {
2672 		dev_err(&hdev->pdev->dev, "VF failed to request misc irq(%d)\n",
2673 			hdev->misc_vector.vector_irq);
2674 		return ret;
2675 	}
2676 
2677 	hclgevf_clear_event_cause(hdev, 0);
2678 
2679 	/* enable misc. vector(vector 0) */
2680 	hclgevf_enable_vector(&hdev->misc_vector, true);
2681 
2682 	return ret;
2683 }
2684 
2685 static void hclgevf_misc_irq_uninit(struct hclgevf_dev *hdev)
2686 {
2687 	/* disable misc vector(vector 0) */
2688 	hclgevf_enable_vector(&hdev->misc_vector, false);
2689 	synchronize_irq(hdev->misc_vector.vector_irq);
2690 	free_irq(hdev->misc_vector.vector_irq, hdev);
2691 	hclgevf_free_vector(hdev, 0);
2692 }
2693 
2694 static void hclgevf_info_show(struct hclgevf_dev *hdev)
2695 {
2696 	struct device *dev = &hdev->pdev->dev;
2697 
2698 	dev_info(dev, "VF info begin:\n");
2699 
2700 	dev_info(dev, "Task queue pairs numbers: %u\n", hdev->num_tqps);
2701 	dev_info(dev, "Desc num per TX queue: %u\n", hdev->num_tx_desc);
2702 	dev_info(dev, "Desc num per RX queue: %u\n", hdev->num_rx_desc);
2703 	dev_info(dev, "Numbers of vports: %u\n", hdev->num_alloc_vport);
2704 	dev_info(dev, "HW tc map: 0x%x\n", hdev->hw_tc_map);
2705 	dev_info(dev, "PF media type of this VF: %u\n",
2706 		 hdev->hw.mac.media_type);
2707 
2708 	dev_info(dev, "VF info end.\n");
2709 }
2710 
2711 static int hclgevf_init_nic_client_instance(struct hnae3_ae_dev *ae_dev,
2712 					    struct hnae3_client *client)
2713 {
2714 	struct hclgevf_dev *hdev = ae_dev->priv;
2715 	int rst_cnt = hdev->rst_stats.rst_cnt;
2716 	int ret;
2717 
2718 	ret = client->ops->init_instance(&hdev->nic);
2719 	if (ret)
2720 		return ret;
2721 
2722 	set_bit(HCLGEVF_STATE_NIC_REGISTERED, &hdev->state);
2723 	if (test_bit(HCLGEVF_STATE_RST_HANDLING, &hdev->state) ||
2724 	    rst_cnt != hdev->rst_stats.rst_cnt) {
2725 		clear_bit(HCLGEVF_STATE_NIC_REGISTERED, &hdev->state);
2726 
2727 		client->ops->uninit_instance(&hdev->nic, 0);
2728 		return -EBUSY;
2729 	}
2730 
2731 	hnae3_set_client_init_flag(client, ae_dev, 1);
2732 
2733 	if (netif_msg_drv(&hdev->nic))
2734 		hclgevf_info_show(hdev);
2735 
2736 	return 0;
2737 }
2738 
2739 static int hclgevf_init_roce_client_instance(struct hnae3_ae_dev *ae_dev,
2740 					     struct hnae3_client *client)
2741 {
2742 	struct hclgevf_dev *hdev = ae_dev->priv;
2743 	int ret;
2744 
2745 	if (!hnae3_dev_roce_supported(hdev) || !hdev->roce_client ||
2746 	    !hdev->nic_client)
2747 		return 0;
2748 
2749 	ret = hclgevf_init_roce_base_info(hdev);
2750 	if (ret)
2751 		return ret;
2752 
2753 	ret = client->ops->init_instance(&hdev->roce);
2754 	if (ret)
2755 		return ret;
2756 
2757 	hnae3_set_client_init_flag(client, ae_dev, 1);
2758 
2759 	return 0;
2760 }
2761 
2762 static int hclgevf_init_client_instance(struct hnae3_client *client,
2763 					struct hnae3_ae_dev *ae_dev)
2764 {
2765 	struct hclgevf_dev *hdev = ae_dev->priv;
2766 	int ret;
2767 
2768 	switch (client->type) {
2769 	case HNAE3_CLIENT_KNIC:
2770 		hdev->nic_client = client;
2771 		hdev->nic.client = client;
2772 
2773 		ret = hclgevf_init_nic_client_instance(ae_dev, client);
2774 		if (ret)
2775 			goto clear_nic;
2776 
2777 		ret = hclgevf_init_roce_client_instance(ae_dev,
2778 							hdev->roce_client);
2779 		if (ret)
2780 			goto clear_roce;
2781 
2782 		break;
2783 	case HNAE3_CLIENT_ROCE:
2784 		if (hnae3_dev_roce_supported(hdev)) {
2785 			hdev->roce_client = client;
2786 			hdev->roce.client = client;
2787 		}
2788 
2789 		ret = hclgevf_init_roce_client_instance(ae_dev, client);
2790 		if (ret)
2791 			goto clear_roce;
2792 
2793 		break;
2794 	default:
2795 		return -EINVAL;
2796 	}
2797 
2798 	return 0;
2799 
2800 clear_nic:
2801 	hdev->nic_client = NULL;
2802 	hdev->nic.client = NULL;
2803 	return ret;
2804 clear_roce:
2805 	hdev->roce_client = NULL;
2806 	hdev->roce.client = NULL;
2807 	return ret;
2808 }
2809 
2810 static void hclgevf_uninit_client_instance(struct hnae3_client *client,
2811 					   struct hnae3_ae_dev *ae_dev)
2812 {
2813 	struct hclgevf_dev *hdev = ae_dev->priv;
2814 
2815 	/* un-init roce, if it exists */
2816 	if (hdev->roce_client) {
2817 		hdev->roce_client->ops->uninit_instance(&hdev->roce, 0);
2818 		hdev->roce_client = NULL;
2819 		hdev->roce.client = NULL;
2820 	}
2821 
2822 	/* un-init nic/unic, if this was not called by roce client */
2823 	if (client->ops->uninit_instance && hdev->nic_client &&
2824 	    client->type != HNAE3_CLIENT_ROCE) {
2825 		clear_bit(HCLGEVF_STATE_NIC_REGISTERED, &hdev->state);
2826 
2827 		client->ops->uninit_instance(&hdev->nic, 0);
2828 		hdev->nic_client = NULL;
2829 		hdev->nic.client = NULL;
2830 	}
2831 }
2832 
2833 static int hclgevf_pci_init(struct hclgevf_dev *hdev)
2834 {
2835 	struct pci_dev *pdev = hdev->pdev;
2836 	struct hclgevf_hw *hw;
2837 	int ret;
2838 
2839 	ret = pci_enable_device(pdev);
2840 	if (ret) {
2841 		dev_err(&pdev->dev, "failed to enable PCI device\n");
2842 		return ret;
2843 	}
2844 
2845 	ret = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
2846 	if (ret) {
2847 		dev_err(&pdev->dev, "can't set consistent PCI DMA, exiting");
2848 		goto err_disable_device;
2849 	}
2850 
2851 	ret = pci_request_regions(pdev, HCLGEVF_DRIVER_NAME);
2852 	if (ret) {
2853 		dev_err(&pdev->dev, "PCI request regions failed %d\n", ret);
2854 		goto err_disable_device;
2855 	}
2856 
2857 	pci_set_master(pdev);
2858 	hw = &hdev->hw;
2859 	hw->hdev = hdev;
2860 	hw->io_base = pci_iomap(pdev, 2, 0);
2861 	if (!hw->io_base) {
2862 		dev_err(&pdev->dev, "can't map configuration register space\n");
2863 		ret = -ENOMEM;
2864 		goto err_clr_master;
2865 	}
2866 
2867 	return 0;
2868 
2869 err_clr_master:
2870 	pci_clear_master(pdev);
2871 	pci_release_regions(pdev);
2872 err_disable_device:
2873 	pci_disable_device(pdev);
2874 
2875 	return ret;
2876 }
2877 
2878 static void hclgevf_pci_uninit(struct hclgevf_dev *hdev)
2879 {
2880 	struct pci_dev *pdev = hdev->pdev;
2881 
2882 	pci_iounmap(pdev, hdev->hw.io_base);
2883 	pci_clear_master(pdev);
2884 	pci_release_regions(pdev);
2885 	pci_disable_device(pdev);
2886 }
2887 
2888 static int hclgevf_query_vf_resource(struct hclgevf_dev *hdev)
2889 {
2890 	struct hclgevf_query_res_cmd *req;
2891 	struct hclgevf_desc desc;
2892 	int ret;
2893 
2894 	hclgevf_cmd_setup_basic_desc(&desc, HCLGEVF_OPC_QUERY_VF_RSRC, true);
2895 	ret = hclgevf_cmd_send(&hdev->hw, &desc, 1);
2896 	if (ret) {
2897 		dev_err(&hdev->pdev->dev,
2898 			"query vf resource failed, ret = %d.\n", ret);
2899 		return ret;
2900 	}
2901 
2902 	req = (struct hclgevf_query_res_cmd *)desc.data;
2903 
2904 	if (hnae3_dev_roce_supported(hdev)) {
2905 		hdev->roce_base_msix_offset =
2906 		hnae3_get_field(le16_to_cpu(req->msixcap_localid_ba_rocee),
2907 				HCLGEVF_MSIX_OFT_ROCEE_M,
2908 				HCLGEVF_MSIX_OFT_ROCEE_S);
2909 		hdev->num_roce_msix =
2910 		hnae3_get_field(le16_to_cpu(req->vf_intr_vector_number),
2911 				HCLGEVF_VEC_NUM_M, HCLGEVF_VEC_NUM_S);
2912 
2913 		/* nic's msix numbers is always equals to the roce's. */
2914 		hdev->num_nic_msix = hdev->num_roce_msix;
2915 
2916 		/* VF should have NIC vectors and Roce vectors, NIC vectors
2917 		 * are queued before Roce vectors. The offset is fixed to 64.
2918 		 */
2919 		hdev->num_msi = hdev->num_roce_msix +
2920 				hdev->roce_base_msix_offset;
2921 	} else {
2922 		hdev->num_msi =
2923 		hnae3_get_field(le16_to_cpu(req->vf_intr_vector_number),
2924 				HCLGEVF_VEC_NUM_M, HCLGEVF_VEC_NUM_S);
2925 
2926 		hdev->num_nic_msix = hdev->num_msi;
2927 	}
2928 
2929 	if (hdev->num_nic_msix < HNAE3_MIN_VECTOR_NUM) {
2930 		dev_err(&hdev->pdev->dev,
2931 			"Just %u msi resources, not enough for vf(min:2).\n",
2932 			hdev->num_nic_msix);
2933 		return -EINVAL;
2934 	}
2935 
2936 	return 0;
2937 }
2938 
2939 static int hclgevf_pci_reset(struct hclgevf_dev *hdev)
2940 {
2941 	struct pci_dev *pdev = hdev->pdev;
2942 	int ret = 0;
2943 
2944 	if (hdev->reset_type == HNAE3_VF_FULL_RESET &&
2945 	    test_bit(HCLGEVF_STATE_IRQ_INITED, &hdev->state)) {
2946 		hclgevf_misc_irq_uninit(hdev);
2947 		hclgevf_uninit_msi(hdev);
2948 		clear_bit(HCLGEVF_STATE_IRQ_INITED, &hdev->state);
2949 	}
2950 
2951 	if (!test_bit(HCLGEVF_STATE_IRQ_INITED, &hdev->state)) {
2952 		pci_set_master(pdev);
2953 		ret = hclgevf_init_msi(hdev);
2954 		if (ret) {
2955 			dev_err(&pdev->dev,
2956 				"failed(%d) to init MSI/MSI-X\n", ret);
2957 			return ret;
2958 		}
2959 
2960 		ret = hclgevf_misc_irq_init(hdev);
2961 		if (ret) {
2962 			hclgevf_uninit_msi(hdev);
2963 			dev_err(&pdev->dev, "failed(%d) to init Misc IRQ(vector0)\n",
2964 				ret);
2965 			return ret;
2966 		}
2967 
2968 		set_bit(HCLGEVF_STATE_IRQ_INITED, &hdev->state);
2969 	}
2970 
2971 	return ret;
2972 }
2973 
2974 static int hclgevf_clear_vport_list(struct hclgevf_dev *hdev)
2975 {
2976 	struct hclge_vf_to_pf_msg send_msg;
2977 
2978 	hclgevf_build_send_msg(&send_msg, HCLGE_MBX_HANDLE_VF_TBL,
2979 			       HCLGE_MBX_VPORT_LIST_CLEAR);
2980 	return hclgevf_send_mbx_msg(hdev, &send_msg, false, NULL, 0);
2981 }
2982 
2983 static int hclgevf_reset_hdev(struct hclgevf_dev *hdev)
2984 {
2985 	struct pci_dev *pdev = hdev->pdev;
2986 	int ret;
2987 
2988 	ret = hclgevf_pci_reset(hdev);
2989 	if (ret) {
2990 		dev_err(&pdev->dev, "pci reset failed %d\n", ret);
2991 		return ret;
2992 	}
2993 
2994 	ret = hclgevf_cmd_init(hdev);
2995 	if (ret) {
2996 		dev_err(&pdev->dev, "cmd failed %d\n", ret);
2997 		return ret;
2998 	}
2999 
3000 	ret = hclgevf_rss_init_hw(hdev);
3001 	if (ret) {
3002 		dev_err(&hdev->pdev->dev,
3003 			"failed(%d) to initialize RSS\n", ret);
3004 		return ret;
3005 	}
3006 
3007 	ret = hclgevf_config_gro(hdev, true);
3008 	if (ret)
3009 		return ret;
3010 
3011 	ret = hclgevf_init_vlan_config(hdev);
3012 	if (ret) {
3013 		dev_err(&hdev->pdev->dev,
3014 			"failed(%d) to initialize VLAN config\n", ret);
3015 		return ret;
3016 	}
3017 
3018 	set_bit(HCLGEVF_STATE_PROMISC_CHANGED, &hdev->state);
3019 
3020 	dev_info(&hdev->pdev->dev, "Reset done\n");
3021 
3022 	return 0;
3023 }
3024 
3025 static int hclgevf_init_hdev(struct hclgevf_dev *hdev)
3026 {
3027 	struct pci_dev *pdev = hdev->pdev;
3028 	int ret;
3029 
3030 	ret = hclgevf_pci_init(hdev);
3031 	if (ret)
3032 		return ret;
3033 
3034 	ret = hclgevf_cmd_queue_init(hdev);
3035 	if (ret)
3036 		goto err_cmd_queue_init;
3037 
3038 	ret = hclgevf_cmd_init(hdev);
3039 	if (ret)
3040 		goto err_cmd_init;
3041 
3042 	/* Get vf resource */
3043 	ret = hclgevf_query_vf_resource(hdev);
3044 	if (ret)
3045 		goto err_cmd_init;
3046 
3047 	ret = hclgevf_init_msi(hdev);
3048 	if (ret) {
3049 		dev_err(&pdev->dev, "failed(%d) to init MSI/MSI-X\n", ret);
3050 		goto err_cmd_init;
3051 	}
3052 
3053 	hclgevf_state_init(hdev);
3054 	hdev->reset_level = HNAE3_VF_FUNC_RESET;
3055 	hdev->reset_type = HNAE3_NONE_RESET;
3056 
3057 	ret = hclgevf_misc_irq_init(hdev);
3058 	if (ret)
3059 		goto err_misc_irq_init;
3060 
3061 	set_bit(HCLGEVF_STATE_IRQ_INITED, &hdev->state);
3062 
3063 	ret = hclgevf_configure(hdev);
3064 	if (ret) {
3065 		dev_err(&pdev->dev, "failed(%d) to fetch configuration\n", ret);
3066 		goto err_config;
3067 	}
3068 
3069 	ret = hclgevf_alloc_tqps(hdev);
3070 	if (ret) {
3071 		dev_err(&pdev->dev, "failed(%d) to allocate TQPs\n", ret);
3072 		goto err_config;
3073 	}
3074 
3075 	ret = hclgevf_set_handle_info(hdev);
3076 	if (ret)
3077 		goto err_config;
3078 
3079 	ret = hclgevf_config_gro(hdev, true);
3080 	if (ret)
3081 		goto err_config;
3082 
3083 	/* Initialize RSS for this VF */
3084 	hclgevf_rss_init_cfg(hdev);
3085 	ret = hclgevf_rss_init_hw(hdev);
3086 	if (ret) {
3087 		dev_err(&hdev->pdev->dev,
3088 			"failed(%d) to initialize RSS\n", ret);
3089 		goto err_config;
3090 	}
3091 
3092 	/* ensure vf tbl list as empty before init*/
3093 	ret = hclgevf_clear_vport_list(hdev);
3094 	if (ret) {
3095 		dev_err(&pdev->dev,
3096 			"failed to clear tbl list configuration, ret = %d.\n",
3097 			ret);
3098 		goto err_config;
3099 	}
3100 
3101 	ret = hclgevf_init_vlan_config(hdev);
3102 	if (ret) {
3103 		dev_err(&hdev->pdev->dev,
3104 			"failed(%d) to initialize VLAN config\n", ret);
3105 		goto err_config;
3106 	}
3107 
3108 	hdev->last_reset_time = jiffies;
3109 	dev_info(&hdev->pdev->dev, "finished initializing %s driver\n",
3110 		 HCLGEVF_DRIVER_NAME);
3111 
3112 	hclgevf_task_schedule(hdev, round_jiffies_relative(HZ));
3113 
3114 	return 0;
3115 
3116 err_config:
3117 	hclgevf_misc_irq_uninit(hdev);
3118 err_misc_irq_init:
3119 	hclgevf_state_uninit(hdev);
3120 	hclgevf_uninit_msi(hdev);
3121 err_cmd_init:
3122 	hclgevf_cmd_uninit(hdev);
3123 err_cmd_queue_init:
3124 	hclgevf_pci_uninit(hdev);
3125 	clear_bit(HCLGEVF_STATE_IRQ_INITED, &hdev->state);
3126 	return ret;
3127 }
3128 
3129 static void hclgevf_uninit_hdev(struct hclgevf_dev *hdev)
3130 {
3131 	struct hclge_vf_to_pf_msg send_msg;
3132 
3133 	hclgevf_state_uninit(hdev);
3134 
3135 	hclgevf_build_send_msg(&send_msg, HCLGE_MBX_VF_UNINIT, 0);
3136 	hclgevf_send_mbx_msg(hdev, &send_msg, false, NULL, 0);
3137 
3138 	if (test_bit(HCLGEVF_STATE_IRQ_INITED, &hdev->state)) {
3139 		hclgevf_misc_irq_uninit(hdev);
3140 		hclgevf_uninit_msi(hdev);
3141 	}
3142 
3143 	hclgevf_pci_uninit(hdev);
3144 	hclgevf_cmd_uninit(hdev);
3145 	hclgevf_uninit_mac_list(hdev);
3146 }
3147 
3148 static int hclgevf_init_ae_dev(struct hnae3_ae_dev *ae_dev)
3149 {
3150 	struct pci_dev *pdev = ae_dev->pdev;
3151 	int ret;
3152 
3153 	ret = hclgevf_alloc_hdev(ae_dev);
3154 	if (ret) {
3155 		dev_err(&pdev->dev, "hclge device allocation failed\n");
3156 		return ret;
3157 	}
3158 
3159 	ret = hclgevf_init_hdev(ae_dev->priv);
3160 	if (ret) {
3161 		dev_err(&pdev->dev, "hclge device initialization failed\n");
3162 		return ret;
3163 	}
3164 
3165 	return 0;
3166 }
3167 
3168 static void hclgevf_uninit_ae_dev(struct hnae3_ae_dev *ae_dev)
3169 {
3170 	struct hclgevf_dev *hdev = ae_dev->priv;
3171 
3172 	hclgevf_uninit_hdev(hdev);
3173 	ae_dev->priv = NULL;
3174 }
3175 
3176 static u32 hclgevf_get_max_channels(struct hclgevf_dev *hdev)
3177 {
3178 	struct hnae3_handle *nic = &hdev->nic;
3179 	struct hnae3_knic_private_info *kinfo = &nic->kinfo;
3180 
3181 	return min_t(u32, hdev->rss_size_max,
3182 		     hdev->num_tqps / kinfo->num_tc);
3183 }
3184 
3185 /**
3186  * hclgevf_get_channels - Get the current channels enabled and max supported.
3187  * @handle: hardware information for network interface
3188  * @ch: ethtool channels structure
3189  *
3190  * We don't support separate tx and rx queues as channels. The other count
3191  * represents how many queues are being used for control. max_combined counts
3192  * how many queue pairs we can support. They may not be mapped 1 to 1 with
3193  * q_vectors since we support a lot more queue pairs than q_vectors.
3194  **/
3195 static void hclgevf_get_channels(struct hnae3_handle *handle,
3196 				 struct ethtool_channels *ch)
3197 {
3198 	struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);
3199 
3200 	ch->max_combined = hclgevf_get_max_channels(hdev);
3201 	ch->other_count = 0;
3202 	ch->max_other = 0;
3203 	ch->combined_count = handle->kinfo.rss_size;
3204 }
3205 
3206 static void hclgevf_get_tqps_and_rss_info(struct hnae3_handle *handle,
3207 					  u16 *alloc_tqps, u16 *max_rss_size)
3208 {
3209 	struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);
3210 
3211 	*alloc_tqps = hdev->num_tqps;
3212 	*max_rss_size = hdev->rss_size_max;
3213 }
3214 
3215 static void hclgevf_update_rss_size(struct hnae3_handle *handle,
3216 				    u32 new_tqps_num)
3217 {
3218 	struct hnae3_knic_private_info *kinfo = &handle->kinfo;
3219 	struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);
3220 	u16 max_rss_size;
3221 
3222 	kinfo->req_rss_size = new_tqps_num;
3223 
3224 	max_rss_size = min_t(u16, hdev->rss_size_max,
3225 			     hdev->num_tqps / kinfo->num_tc);
3226 
3227 	/* Use the user's configuration when it is not larger than
3228 	 * max_rss_size, otherwise, use the maximum specification value.
3229 	 */
3230 	if (kinfo->req_rss_size != kinfo->rss_size && kinfo->req_rss_size &&
3231 	    kinfo->req_rss_size <= max_rss_size)
3232 		kinfo->rss_size = kinfo->req_rss_size;
3233 	else if (kinfo->rss_size > max_rss_size ||
3234 		 (!kinfo->req_rss_size && kinfo->rss_size < max_rss_size))
3235 		kinfo->rss_size = max_rss_size;
3236 
3237 	kinfo->num_tqps = kinfo->num_tc * kinfo->rss_size;
3238 }
3239 
3240 static int hclgevf_set_channels(struct hnae3_handle *handle, u32 new_tqps_num,
3241 				bool rxfh_configured)
3242 {
3243 	struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);
3244 	struct hnae3_knic_private_info *kinfo = &handle->kinfo;
3245 	u16 cur_rss_size = kinfo->rss_size;
3246 	u16 cur_tqps = kinfo->num_tqps;
3247 	u32 *rss_indir;
3248 	unsigned int i;
3249 	int ret;
3250 
3251 	hclgevf_update_rss_size(handle, new_tqps_num);
3252 
3253 	ret = hclgevf_set_rss_tc_mode(hdev, kinfo->rss_size);
3254 	if (ret)
3255 		return ret;
3256 
3257 	/* RSS indirection table has been configuared by user */
3258 	if (rxfh_configured)
3259 		goto out;
3260 
3261 	/* Reinitializes the rss indirect table according to the new RSS size */
3262 	rss_indir = kcalloc(HCLGEVF_RSS_IND_TBL_SIZE, sizeof(u32), GFP_KERNEL);
3263 	if (!rss_indir)
3264 		return -ENOMEM;
3265 
3266 	for (i = 0; i < HCLGEVF_RSS_IND_TBL_SIZE; i++)
3267 		rss_indir[i] = i % kinfo->rss_size;
3268 
3269 	hdev->rss_cfg.rss_size = kinfo->rss_size;
3270 
3271 	ret = hclgevf_set_rss(handle, rss_indir, NULL, 0);
3272 	if (ret)
3273 		dev_err(&hdev->pdev->dev, "set rss indir table fail, ret=%d\n",
3274 			ret);
3275 
3276 	kfree(rss_indir);
3277 
3278 out:
3279 	if (!ret)
3280 		dev_info(&hdev->pdev->dev,
3281 			 "Channels changed, rss_size from %u to %u, tqps from %u to %u",
3282 			 cur_rss_size, kinfo->rss_size,
3283 			 cur_tqps, kinfo->rss_size * kinfo->num_tc);
3284 
3285 	return ret;
3286 }
3287 
3288 static int hclgevf_get_status(struct hnae3_handle *handle)
3289 {
3290 	struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);
3291 
3292 	return hdev->hw.mac.link;
3293 }
3294 
3295 static void hclgevf_get_ksettings_an_result(struct hnae3_handle *handle,
3296 					    u8 *auto_neg, u32 *speed,
3297 					    u8 *duplex)
3298 {
3299 	struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);
3300 
3301 	if (speed)
3302 		*speed = hdev->hw.mac.speed;
3303 	if (duplex)
3304 		*duplex = hdev->hw.mac.duplex;
3305 	if (auto_neg)
3306 		*auto_neg = AUTONEG_DISABLE;
3307 }
3308 
3309 void hclgevf_update_speed_duplex(struct hclgevf_dev *hdev, u32 speed,
3310 				 u8 duplex)
3311 {
3312 	hdev->hw.mac.speed = speed;
3313 	hdev->hw.mac.duplex = duplex;
3314 }
3315 
3316 static int hclgevf_gro_en(struct hnae3_handle *handle, bool enable)
3317 {
3318 	struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);
3319 
3320 	return hclgevf_config_gro(hdev, enable);
3321 }
3322 
3323 static void hclgevf_get_media_type(struct hnae3_handle *handle, u8 *media_type,
3324 				   u8 *module_type)
3325 {
3326 	struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);
3327 
3328 	if (media_type)
3329 		*media_type = hdev->hw.mac.media_type;
3330 
3331 	if (module_type)
3332 		*module_type = hdev->hw.mac.module_type;
3333 }
3334 
3335 static bool hclgevf_get_hw_reset_stat(struct hnae3_handle *handle)
3336 {
3337 	struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);
3338 
3339 	return !!hclgevf_read_dev(&hdev->hw, HCLGEVF_RST_ING);
3340 }
3341 
3342 static bool hclgevf_ae_dev_resetting(struct hnae3_handle *handle)
3343 {
3344 	struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);
3345 
3346 	return test_bit(HCLGEVF_STATE_RST_HANDLING, &hdev->state);
3347 }
3348 
3349 static unsigned long hclgevf_ae_dev_reset_cnt(struct hnae3_handle *handle)
3350 {
3351 	struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);
3352 
3353 	return hdev->rst_stats.hw_rst_done_cnt;
3354 }
3355 
3356 static void hclgevf_get_link_mode(struct hnae3_handle *handle,
3357 				  unsigned long *supported,
3358 				  unsigned long *advertising)
3359 {
3360 	struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);
3361 
3362 	*supported = hdev->hw.mac.supported;
3363 	*advertising = hdev->hw.mac.advertising;
3364 }
3365 
3366 #define MAX_SEPARATE_NUM	4
3367 #define SEPARATOR_VALUE		0xFFFFFFFF
3368 #define REG_NUM_PER_LINE	4
3369 #define REG_LEN_PER_LINE	(REG_NUM_PER_LINE * sizeof(u32))
3370 
3371 static int hclgevf_get_regs_len(struct hnae3_handle *handle)
3372 {
3373 	int cmdq_lines, common_lines, ring_lines, tqp_intr_lines;
3374 	struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);
3375 
3376 	cmdq_lines = sizeof(cmdq_reg_addr_list) / REG_LEN_PER_LINE + 1;
3377 	common_lines = sizeof(common_reg_addr_list) / REG_LEN_PER_LINE + 1;
3378 	ring_lines = sizeof(ring_reg_addr_list) / REG_LEN_PER_LINE + 1;
3379 	tqp_intr_lines = sizeof(tqp_intr_reg_addr_list) / REG_LEN_PER_LINE + 1;
3380 
3381 	return (cmdq_lines + common_lines + ring_lines * hdev->num_tqps +
3382 		tqp_intr_lines * (hdev->num_msi_used - 1)) * REG_LEN_PER_LINE;
3383 }
3384 
3385 static void hclgevf_get_regs(struct hnae3_handle *handle, u32 *version,
3386 			     void *data)
3387 {
3388 	struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);
3389 	int i, j, reg_um, separator_num;
3390 	u32 *reg = data;
3391 
3392 	*version = hdev->fw_version;
3393 
3394 	/* fetching per-VF registers values from VF PCIe register space */
3395 	reg_um = sizeof(cmdq_reg_addr_list) / sizeof(u32);
3396 	separator_num = MAX_SEPARATE_NUM - reg_um % REG_NUM_PER_LINE;
3397 	for (i = 0; i < reg_um; i++)
3398 		*reg++ = hclgevf_read_dev(&hdev->hw, cmdq_reg_addr_list[i]);
3399 	for (i = 0; i < separator_num; i++)
3400 		*reg++ = SEPARATOR_VALUE;
3401 
3402 	reg_um = sizeof(common_reg_addr_list) / sizeof(u32);
3403 	separator_num = MAX_SEPARATE_NUM - reg_um % REG_NUM_PER_LINE;
3404 	for (i = 0; i < reg_um; i++)
3405 		*reg++ = hclgevf_read_dev(&hdev->hw, common_reg_addr_list[i]);
3406 	for (i = 0; i < separator_num; i++)
3407 		*reg++ = SEPARATOR_VALUE;
3408 
3409 	reg_um = sizeof(ring_reg_addr_list) / sizeof(u32);
3410 	separator_num = MAX_SEPARATE_NUM - reg_um % REG_NUM_PER_LINE;
3411 	for (j = 0; j < hdev->num_tqps; j++) {
3412 		for (i = 0; i < reg_um; i++)
3413 			*reg++ = hclgevf_read_dev(&hdev->hw,
3414 						  ring_reg_addr_list[i] +
3415 						  0x200 * j);
3416 		for (i = 0; i < separator_num; i++)
3417 			*reg++ = SEPARATOR_VALUE;
3418 	}
3419 
3420 	reg_um = sizeof(tqp_intr_reg_addr_list) / sizeof(u32);
3421 	separator_num = MAX_SEPARATE_NUM - reg_um % REG_NUM_PER_LINE;
3422 	for (j = 0; j < hdev->num_msi_used - 1; j++) {
3423 		for (i = 0; i < reg_um; i++)
3424 			*reg++ = hclgevf_read_dev(&hdev->hw,
3425 						  tqp_intr_reg_addr_list[i] +
3426 						  4 * j);
3427 		for (i = 0; i < separator_num; i++)
3428 			*reg++ = SEPARATOR_VALUE;
3429 	}
3430 }
3431 
3432 void hclgevf_update_port_base_vlan_info(struct hclgevf_dev *hdev, u16 state,
3433 					u8 *port_base_vlan_info, u8 data_size)
3434 {
3435 	struct hnae3_handle *nic = &hdev->nic;
3436 	struct hclge_vf_to_pf_msg send_msg;
3437 
3438 	rtnl_lock();
3439 	hclgevf_notify_client(hdev, HNAE3_DOWN_CLIENT);
3440 	rtnl_unlock();
3441 
3442 	/* send msg to PF and wait update port based vlan info */
3443 	hclgevf_build_send_msg(&send_msg, HCLGE_MBX_SET_VLAN,
3444 			       HCLGE_MBX_PORT_BASE_VLAN_CFG);
3445 	memcpy(send_msg.data, port_base_vlan_info, data_size);
3446 	hclgevf_send_mbx_msg(hdev, &send_msg, false, NULL, 0);
3447 
3448 	if (state == HNAE3_PORT_BASE_VLAN_DISABLE)
3449 		nic->port_base_vlan_state = HNAE3_PORT_BASE_VLAN_DISABLE;
3450 	else
3451 		nic->port_base_vlan_state = HNAE3_PORT_BASE_VLAN_ENABLE;
3452 
3453 	rtnl_lock();
3454 	hclgevf_notify_client(hdev, HNAE3_UP_CLIENT);
3455 	rtnl_unlock();
3456 }
3457 
3458 static const struct hnae3_ae_ops hclgevf_ops = {
3459 	.init_ae_dev = hclgevf_init_ae_dev,
3460 	.uninit_ae_dev = hclgevf_uninit_ae_dev,
3461 	.flr_prepare = hclgevf_flr_prepare,
3462 	.flr_done = hclgevf_flr_done,
3463 	.init_client_instance = hclgevf_init_client_instance,
3464 	.uninit_client_instance = hclgevf_uninit_client_instance,
3465 	.start = hclgevf_ae_start,
3466 	.stop = hclgevf_ae_stop,
3467 	.client_start = hclgevf_client_start,
3468 	.client_stop = hclgevf_client_stop,
3469 	.map_ring_to_vector = hclgevf_map_ring_to_vector,
3470 	.unmap_ring_from_vector = hclgevf_unmap_ring_from_vector,
3471 	.get_vector = hclgevf_get_vector,
3472 	.put_vector = hclgevf_put_vector,
3473 	.reset_queue = hclgevf_reset_tqp,
3474 	.get_mac_addr = hclgevf_get_mac_addr,
3475 	.set_mac_addr = hclgevf_set_mac_addr,
3476 	.add_uc_addr = hclgevf_add_uc_addr,
3477 	.rm_uc_addr = hclgevf_rm_uc_addr,
3478 	.add_mc_addr = hclgevf_add_mc_addr,
3479 	.rm_mc_addr = hclgevf_rm_mc_addr,
3480 	.get_stats = hclgevf_get_stats,
3481 	.update_stats = hclgevf_update_stats,
3482 	.get_strings = hclgevf_get_strings,
3483 	.get_sset_count = hclgevf_get_sset_count,
3484 	.get_rss_key_size = hclgevf_get_rss_key_size,
3485 	.get_rss_indir_size = hclgevf_get_rss_indir_size,
3486 	.get_rss = hclgevf_get_rss,
3487 	.set_rss = hclgevf_set_rss,
3488 	.get_rss_tuple = hclgevf_get_rss_tuple,
3489 	.set_rss_tuple = hclgevf_set_rss_tuple,
3490 	.get_tc_size = hclgevf_get_tc_size,
3491 	.get_fw_version = hclgevf_get_fw_version,
3492 	.set_vlan_filter = hclgevf_set_vlan_filter,
3493 	.enable_hw_strip_rxvtag = hclgevf_en_hw_strip_rxvtag,
3494 	.reset_event = hclgevf_reset_event,
3495 	.set_default_reset_request = hclgevf_set_def_reset_request,
3496 	.set_channels = hclgevf_set_channels,
3497 	.get_channels = hclgevf_get_channels,
3498 	.get_tqps_and_rss_info = hclgevf_get_tqps_and_rss_info,
3499 	.get_regs_len = hclgevf_get_regs_len,
3500 	.get_regs = hclgevf_get_regs,
3501 	.get_status = hclgevf_get_status,
3502 	.get_ksettings_an_result = hclgevf_get_ksettings_an_result,
3503 	.get_media_type = hclgevf_get_media_type,
3504 	.get_hw_reset_stat = hclgevf_get_hw_reset_stat,
3505 	.ae_dev_resetting = hclgevf_ae_dev_resetting,
3506 	.ae_dev_reset_cnt = hclgevf_ae_dev_reset_cnt,
3507 	.set_gro_en = hclgevf_gro_en,
3508 	.set_mtu = hclgevf_set_mtu,
3509 	.get_global_queue_id = hclgevf_get_qid_global,
3510 	.set_timer_task = hclgevf_set_timer_task,
3511 	.get_link_mode = hclgevf_get_link_mode,
3512 	.set_promisc_mode = hclgevf_set_promisc_mode,
3513 	.request_update_promisc_mode = hclgevf_request_update_promisc_mode,
3514 };
3515 
3516 static struct hnae3_ae_algo ae_algovf = {
3517 	.ops = &hclgevf_ops,
3518 	.pdev_id_table = ae_algovf_pci_tbl,
3519 };
3520 
3521 static int hclgevf_init(void)
3522 {
3523 	pr_info("%s is initializing\n", HCLGEVF_NAME);
3524 
3525 	hclgevf_wq = alloc_workqueue("%s", 0, 0, HCLGEVF_NAME);
3526 	if (!hclgevf_wq) {
3527 		pr_err("%s: failed to create workqueue\n", HCLGEVF_NAME);
3528 		return -ENOMEM;
3529 	}
3530 
3531 	hnae3_register_ae_algo(&ae_algovf);
3532 
3533 	return 0;
3534 }
3535 
3536 static void hclgevf_exit(void)
3537 {
3538 	hnae3_unregister_ae_algo(&ae_algovf);
3539 	destroy_workqueue(hclgevf_wq);
3540 }
3541 module_init(hclgevf_init);
3542 module_exit(hclgevf_exit);
3543 
3544 MODULE_LICENSE("GPL");
3545 MODULE_AUTHOR("Huawei Tech. Co., Ltd.");
3546 MODULE_DESCRIPTION("HCLGEVF Driver");
3547 MODULE_VERSION(HCLGEVF_MOD_VERSION);
3548