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