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