1 // SPDX-License-Identifier: GPL-2.0+
2 // Copyright (c) 2016-2017 Hisilicon Limited.
3 
4 #include <linux/etherdevice.h>
5 #include <linux/iopoll.h>
6 #include <net/rtnetlink.h>
7 #include "hclgevf_cmd.h"
8 #include "hclgevf_main.h"
9 #include "hclge_mbx.h"
10 #include "hnae3.h"
11 
12 #define HCLGEVF_NAME	"hclgevf"
13 
14 static int hclgevf_reset_hdev(struct hclgevf_dev *hdev);
15 static struct hnae3_ae_algo ae_algovf;
16 
17 static const struct pci_device_id ae_algovf_pci_tbl[] = {
18 	{PCI_VDEVICE(HUAWEI, HNAE3_DEV_ID_100G_VF), 0},
19 	{PCI_VDEVICE(HUAWEI, HNAE3_DEV_ID_100G_RDMA_DCB_PFC_VF), 0},
20 	/* required last entry */
21 	{0, }
22 };
23 
24 MODULE_DEVICE_TABLE(pci, ae_algovf_pci_tbl);
25 
26 static inline struct hclgevf_dev *hclgevf_ae_get_hdev(
27 	struct hnae3_handle *handle)
28 {
29 	return container_of(handle, struct hclgevf_dev, nic);
30 }
31 
32 static int hclgevf_tqps_update_stats(struct hnae3_handle *handle)
33 {
34 	struct hnae3_knic_private_info *kinfo = &handle->kinfo;
35 	struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);
36 	struct hclgevf_desc desc;
37 	struct hclgevf_tqp *tqp;
38 	int status;
39 	int i;
40 
41 	for (i = 0; i < kinfo->num_tqps; i++) {
42 		tqp = container_of(kinfo->tqp[i], struct hclgevf_tqp, q);
43 		hclgevf_cmd_setup_basic_desc(&desc,
44 					     HCLGEVF_OPC_QUERY_RX_STATUS,
45 					     true);
46 
47 		desc.data[0] = cpu_to_le32(tqp->index & 0x1ff);
48 		status = hclgevf_cmd_send(&hdev->hw, &desc, 1);
49 		if (status) {
50 			dev_err(&hdev->pdev->dev,
51 				"Query tqp stat fail, status = %d,queue = %d\n",
52 				status,	i);
53 			return status;
54 		}
55 		tqp->tqp_stats.rcb_rx_ring_pktnum_rcd +=
56 			le32_to_cpu(desc.data[1]);
57 
58 		hclgevf_cmd_setup_basic_desc(&desc, HCLGEVF_OPC_QUERY_TX_STATUS,
59 					     true);
60 
61 		desc.data[0] = cpu_to_le32(tqp->index & 0x1ff);
62 		status = hclgevf_cmd_send(&hdev->hw, &desc, 1);
63 		if (status) {
64 			dev_err(&hdev->pdev->dev,
65 				"Query tqp stat fail, status = %d,queue = %d\n",
66 				status, i);
67 			return status;
68 		}
69 		tqp->tqp_stats.rcb_tx_ring_pktnum_rcd +=
70 			le32_to_cpu(desc.data[1]);
71 	}
72 
73 	return 0;
74 }
75 
76 static u64 *hclgevf_tqps_get_stats(struct hnae3_handle *handle, u64 *data)
77 {
78 	struct hnae3_knic_private_info *kinfo = &handle->kinfo;
79 	struct hclgevf_tqp *tqp;
80 	u64 *buff = data;
81 	int i;
82 
83 	for (i = 0; i < kinfo->num_tqps; i++) {
84 		tqp = container_of(kinfo->tqp[i], struct hclgevf_tqp, q);
85 		*buff++ = tqp->tqp_stats.rcb_tx_ring_pktnum_rcd;
86 	}
87 	for (i = 0; i < kinfo->num_tqps; i++) {
88 		tqp = container_of(kinfo->tqp[i], struct hclgevf_tqp, q);
89 		*buff++ = tqp->tqp_stats.rcb_rx_ring_pktnum_rcd;
90 	}
91 
92 	return buff;
93 }
94 
95 static int hclgevf_tqps_get_sset_count(struct hnae3_handle *handle, int strset)
96 {
97 	struct hnae3_knic_private_info *kinfo = &handle->kinfo;
98 
99 	return kinfo->num_tqps * 2;
100 }
101 
102 static u8 *hclgevf_tqps_get_strings(struct hnae3_handle *handle, u8 *data)
103 {
104 	struct hnae3_knic_private_info *kinfo = &handle->kinfo;
105 	u8 *buff = data;
106 	int i = 0;
107 
108 	for (i = 0; i < kinfo->num_tqps; i++) {
109 		struct hclgevf_tqp *tqp = container_of(kinfo->tqp[i],
110 						       struct hclgevf_tqp, q);
111 		snprintf(buff, ETH_GSTRING_LEN, "txq%d_pktnum_rcd",
112 			 tqp->index);
113 		buff += ETH_GSTRING_LEN;
114 	}
115 
116 	for (i = 0; i < kinfo->num_tqps; i++) {
117 		struct hclgevf_tqp *tqp = container_of(kinfo->tqp[i],
118 						       struct hclgevf_tqp, q);
119 		snprintf(buff, ETH_GSTRING_LEN, "rxq%d_pktnum_rcd",
120 			 tqp->index);
121 		buff += ETH_GSTRING_LEN;
122 	}
123 
124 	return buff;
125 }
126 
127 static void hclgevf_update_stats(struct hnae3_handle *handle,
128 				 struct net_device_stats *net_stats)
129 {
130 	struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);
131 	int status;
132 
133 	status = hclgevf_tqps_update_stats(handle);
134 	if (status)
135 		dev_err(&hdev->pdev->dev,
136 			"VF update of TQPS stats fail, status = %d.\n",
137 			status);
138 }
139 
140 static int hclgevf_get_sset_count(struct hnae3_handle *handle, int strset)
141 {
142 	if (strset == ETH_SS_TEST)
143 		return -EOPNOTSUPP;
144 	else if (strset == ETH_SS_STATS)
145 		return hclgevf_tqps_get_sset_count(handle, strset);
146 
147 	return 0;
148 }
149 
150 static void hclgevf_get_strings(struct hnae3_handle *handle, u32 strset,
151 				u8 *data)
152 {
153 	u8 *p = (char *)data;
154 
155 	if (strset == ETH_SS_STATS)
156 		p = hclgevf_tqps_get_strings(handle, p);
157 }
158 
159 static void hclgevf_get_stats(struct hnae3_handle *handle, u64 *data)
160 {
161 	hclgevf_tqps_get_stats(handle, data);
162 }
163 
164 static int hclgevf_get_tc_info(struct hclgevf_dev *hdev)
165 {
166 	u8 resp_msg;
167 	int status;
168 
169 	status = hclgevf_send_mbx_msg(hdev, HCLGE_MBX_GET_TCINFO, 0, NULL, 0,
170 				      true, &resp_msg, sizeof(u8));
171 	if (status) {
172 		dev_err(&hdev->pdev->dev,
173 			"VF request to get TC info from PF failed %d",
174 			status);
175 		return status;
176 	}
177 
178 	hdev->hw_tc_map = resp_msg;
179 
180 	return 0;
181 }
182 
183 static int hclgevf_get_queue_info(struct hclgevf_dev *hdev)
184 {
185 #define HCLGEVF_TQPS_RSS_INFO_LEN	8
186 	u8 resp_msg[HCLGEVF_TQPS_RSS_INFO_LEN];
187 	int status;
188 
189 	status = hclgevf_send_mbx_msg(hdev, HCLGE_MBX_GET_QINFO, 0, NULL, 0,
190 				      true, resp_msg,
191 				      HCLGEVF_TQPS_RSS_INFO_LEN);
192 	if (status) {
193 		dev_err(&hdev->pdev->dev,
194 			"VF request to get tqp info from PF failed %d",
195 			status);
196 		return status;
197 	}
198 
199 	memcpy(&hdev->num_tqps, &resp_msg[0], sizeof(u16));
200 	memcpy(&hdev->rss_size_max, &resp_msg[2], sizeof(u16));
201 	memcpy(&hdev->num_desc, &resp_msg[4], sizeof(u16));
202 	memcpy(&hdev->rx_buf_len, &resp_msg[6], sizeof(u16));
203 
204 	return 0;
205 }
206 
207 static int hclgevf_alloc_tqps(struct hclgevf_dev *hdev)
208 {
209 	struct hclgevf_tqp *tqp;
210 	int i;
211 
212 	hdev->htqp = devm_kcalloc(&hdev->pdev->dev, hdev->num_tqps,
213 				  sizeof(struct hclgevf_tqp), GFP_KERNEL);
214 	if (!hdev->htqp)
215 		return -ENOMEM;
216 
217 	tqp = hdev->htqp;
218 
219 	for (i = 0; i < hdev->num_tqps; i++) {
220 		tqp->dev = &hdev->pdev->dev;
221 		tqp->index = i;
222 
223 		tqp->q.ae_algo = &ae_algovf;
224 		tqp->q.buf_size = hdev->rx_buf_len;
225 		tqp->q.desc_num = hdev->num_desc;
226 		tqp->q.io_base = hdev->hw.io_base + HCLGEVF_TQP_REG_OFFSET +
227 			i * HCLGEVF_TQP_REG_SIZE;
228 
229 		tqp++;
230 	}
231 
232 	return 0;
233 }
234 
235 static int hclgevf_knic_setup(struct hclgevf_dev *hdev)
236 {
237 	struct hnae3_handle *nic = &hdev->nic;
238 	struct hnae3_knic_private_info *kinfo;
239 	u16 new_tqps = hdev->num_tqps;
240 	int i;
241 
242 	kinfo = &nic->kinfo;
243 	kinfo->num_tc = 0;
244 	kinfo->num_desc = hdev->num_desc;
245 	kinfo->rx_buf_len = hdev->rx_buf_len;
246 	for (i = 0; i < HCLGEVF_MAX_TC_NUM; i++)
247 		if (hdev->hw_tc_map & BIT(i))
248 			kinfo->num_tc++;
249 
250 	kinfo->rss_size
251 		= min_t(u16, hdev->rss_size_max, new_tqps / kinfo->num_tc);
252 	new_tqps = kinfo->rss_size * kinfo->num_tc;
253 	kinfo->num_tqps = min(new_tqps, hdev->num_tqps);
254 
255 	kinfo->tqp = devm_kcalloc(&hdev->pdev->dev, kinfo->num_tqps,
256 				  sizeof(struct hnae3_queue *), GFP_KERNEL);
257 	if (!kinfo->tqp)
258 		return -ENOMEM;
259 
260 	for (i = 0; i < kinfo->num_tqps; i++) {
261 		hdev->htqp[i].q.handle = &hdev->nic;
262 		hdev->htqp[i].q.tqp_index = i;
263 		kinfo->tqp[i] = &hdev->htqp[i].q;
264 	}
265 
266 	return 0;
267 }
268 
269 static void hclgevf_request_link_info(struct hclgevf_dev *hdev)
270 {
271 	int status;
272 	u8 resp_msg;
273 
274 	status = hclgevf_send_mbx_msg(hdev, HCLGE_MBX_GET_LINK_STATUS, 0, NULL,
275 				      0, false, &resp_msg, sizeof(u8));
276 	if (status)
277 		dev_err(&hdev->pdev->dev,
278 			"VF failed to fetch link status(%d) from PF", status);
279 }
280 
281 void hclgevf_update_link_status(struct hclgevf_dev *hdev, int link_state)
282 {
283 	struct hnae3_handle *handle = &hdev->nic;
284 	struct hnae3_client *client;
285 
286 	client = handle->client;
287 
288 	link_state =
289 		test_bit(HCLGEVF_STATE_DOWN, &hdev->state) ? 0 : link_state;
290 
291 	if (link_state != hdev->hw.mac.link) {
292 		client->ops->link_status_change(handle, !!link_state);
293 		hdev->hw.mac.link = link_state;
294 	}
295 }
296 
297 static int hclgevf_set_handle_info(struct hclgevf_dev *hdev)
298 {
299 	struct hnae3_handle *nic = &hdev->nic;
300 	int ret;
301 
302 	nic->ae_algo = &ae_algovf;
303 	nic->pdev = hdev->pdev;
304 	nic->numa_node_mask = hdev->numa_node_mask;
305 	nic->flags |= HNAE3_SUPPORT_VF;
306 
307 	if (hdev->ae_dev->dev_type != HNAE3_DEV_KNIC) {
308 		dev_err(&hdev->pdev->dev, "unsupported device type %d\n",
309 			hdev->ae_dev->dev_type);
310 		return -EINVAL;
311 	}
312 
313 	ret = hclgevf_knic_setup(hdev);
314 	if (ret)
315 		dev_err(&hdev->pdev->dev, "VF knic setup failed %d\n",
316 			ret);
317 	return ret;
318 }
319 
320 static void hclgevf_free_vector(struct hclgevf_dev *hdev, int vector_id)
321 {
322 	if (hdev->vector_status[vector_id] == HCLGEVF_INVALID_VPORT) {
323 		dev_warn(&hdev->pdev->dev,
324 			 "vector(vector_id %d) has been freed.\n", vector_id);
325 		return;
326 	}
327 
328 	hdev->vector_status[vector_id] = HCLGEVF_INVALID_VPORT;
329 	hdev->num_msi_left += 1;
330 	hdev->num_msi_used -= 1;
331 }
332 
333 static int hclgevf_get_vector(struct hnae3_handle *handle, u16 vector_num,
334 			      struct hnae3_vector_info *vector_info)
335 {
336 	struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);
337 	struct hnae3_vector_info *vector = vector_info;
338 	int alloc = 0;
339 	int i, j;
340 
341 	vector_num = min(hdev->num_msi_left, vector_num);
342 
343 	for (j = 0; j < vector_num; j++) {
344 		for (i = HCLGEVF_MISC_VECTOR_NUM + 1; i < hdev->num_msi; i++) {
345 			if (hdev->vector_status[i] == HCLGEVF_INVALID_VPORT) {
346 				vector->vector = pci_irq_vector(hdev->pdev, i);
347 				vector->io_addr = hdev->hw.io_base +
348 					HCLGEVF_VECTOR_REG_BASE +
349 					(i - 1) * HCLGEVF_VECTOR_REG_OFFSET;
350 				hdev->vector_status[i] = 0;
351 				hdev->vector_irq[i] = vector->vector;
352 
353 				vector++;
354 				alloc++;
355 
356 				break;
357 			}
358 		}
359 	}
360 	hdev->num_msi_left -= alloc;
361 	hdev->num_msi_used += alloc;
362 
363 	return alloc;
364 }
365 
366 static int hclgevf_get_vector_index(struct hclgevf_dev *hdev, int vector)
367 {
368 	int i;
369 
370 	for (i = 0; i < hdev->num_msi; i++)
371 		if (vector == hdev->vector_irq[i])
372 			return i;
373 
374 	return -EINVAL;
375 }
376 
377 static int hclgevf_set_rss_algo_key(struct hclgevf_dev *hdev,
378 				    const u8 hfunc, const u8 *key)
379 {
380 	struct hclgevf_rss_config_cmd *req;
381 	struct hclgevf_desc desc;
382 	int key_offset;
383 	int key_size;
384 	int ret;
385 
386 	req = (struct hclgevf_rss_config_cmd *)desc.data;
387 
388 	for (key_offset = 0; key_offset < 3; key_offset++) {
389 		hclgevf_cmd_setup_basic_desc(&desc,
390 					     HCLGEVF_OPC_RSS_GENERIC_CONFIG,
391 					     false);
392 
393 		req->hash_config |= (hfunc & HCLGEVF_RSS_HASH_ALGO_MASK);
394 		req->hash_config |=
395 			(key_offset << HCLGEVF_RSS_HASH_KEY_OFFSET_B);
396 
397 		if (key_offset == 2)
398 			key_size =
399 			HCLGEVF_RSS_KEY_SIZE - HCLGEVF_RSS_HASH_KEY_NUM * 2;
400 		else
401 			key_size = HCLGEVF_RSS_HASH_KEY_NUM;
402 
403 		memcpy(req->hash_key,
404 		       key + key_offset * HCLGEVF_RSS_HASH_KEY_NUM, key_size);
405 
406 		ret = hclgevf_cmd_send(&hdev->hw, &desc, 1);
407 		if (ret) {
408 			dev_err(&hdev->pdev->dev,
409 				"Configure RSS config fail, status = %d\n",
410 				ret);
411 			return ret;
412 		}
413 	}
414 
415 	return 0;
416 }
417 
418 static u32 hclgevf_get_rss_key_size(struct hnae3_handle *handle)
419 {
420 	return HCLGEVF_RSS_KEY_SIZE;
421 }
422 
423 static u32 hclgevf_get_rss_indir_size(struct hnae3_handle *handle)
424 {
425 	return HCLGEVF_RSS_IND_TBL_SIZE;
426 }
427 
428 static int hclgevf_set_rss_indir_table(struct hclgevf_dev *hdev)
429 {
430 	const u8 *indir = hdev->rss_cfg.rss_indirection_tbl;
431 	struct hclgevf_rss_indirection_table_cmd *req;
432 	struct hclgevf_desc desc;
433 	int status;
434 	int i, j;
435 
436 	req = (struct hclgevf_rss_indirection_table_cmd *)desc.data;
437 
438 	for (i = 0; i < HCLGEVF_RSS_CFG_TBL_NUM; i++) {
439 		hclgevf_cmd_setup_basic_desc(&desc, HCLGEVF_OPC_RSS_INDIR_TABLE,
440 					     false);
441 		req->start_table_index = i * HCLGEVF_RSS_CFG_TBL_SIZE;
442 		req->rss_set_bitmap = HCLGEVF_RSS_SET_BITMAP_MSK;
443 		for (j = 0; j < HCLGEVF_RSS_CFG_TBL_SIZE; j++)
444 			req->rss_result[j] =
445 				indir[i * HCLGEVF_RSS_CFG_TBL_SIZE + j];
446 
447 		status = hclgevf_cmd_send(&hdev->hw, &desc, 1);
448 		if (status) {
449 			dev_err(&hdev->pdev->dev,
450 				"VF failed(=%d) to set RSS indirection table\n",
451 				status);
452 			return status;
453 		}
454 	}
455 
456 	return 0;
457 }
458 
459 static int hclgevf_set_rss_tc_mode(struct hclgevf_dev *hdev,  u16 rss_size)
460 {
461 	struct hclgevf_rss_tc_mode_cmd *req;
462 	u16 tc_offset[HCLGEVF_MAX_TC_NUM];
463 	u16 tc_valid[HCLGEVF_MAX_TC_NUM];
464 	u16 tc_size[HCLGEVF_MAX_TC_NUM];
465 	struct hclgevf_desc desc;
466 	u16 roundup_size;
467 	int status;
468 	int i;
469 
470 	req = (struct hclgevf_rss_tc_mode_cmd *)desc.data;
471 
472 	roundup_size = roundup_pow_of_two(rss_size);
473 	roundup_size = ilog2(roundup_size);
474 
475 	for (i = 0; i < HCLGEVF_MAX_TC_NUM; i++) {
476 		tc_valid[i] = !!(hdev->hw_tc_map & BIT(i));
477 		tc_size[i] = roundup_size;
478 		tc_offset[i] = rss_size * i;
479 	}
480 
481 	hclgevf_cmd_setup_basic_desc(&desc, HCLGEVF_OPC_RSS_TC_MODE, false);
482 	for (i = 0; i < HCLGEVF_MAX_TC_NUM; i++) {
483 		hnae3_set_bit(req->rss_tc_mode[i], HCLGEVF_RSS_TC_VALID_B,
484 			      (tc_valid[i] & 0x1));
485 		hnae3_set_field(req->rss_tc_mode[i], HCLGEVF_RSS_TC_SIZE_M,
486 				HCLGEVF_RSS_TC_SIZE_S, tc_size[i]);
487 		hnae3_set_field(req->rss_tc_mode[i], HCLGEVF_RSS_TC_OFFSET_M,
488 				HCLGEVF_RSS_TC_OFFSET_S, tc_offset[i]);
489 	}
490 	status = hclgevf_cmd_send(&hdev->hw, &desc, 1);
491 	if (status)
492 		dev_err(&hdev->pdev->dev,
493 			"VF failed(=%d) to set rss tc mode\n", status);
494 
495 	return status;
496 }
497 
498 static int hclgevf_get_rss(struct hnae3_handle *handle, u32 *indir, u8 *key,
499 			   u8 *hfunc)
500 {
501 	struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);
502 	struct hclgevf_rss_cfg *rss_cfg = &hdev->rss_cfg;
503 	int i;
504 
505 	if (handle->pdev->revision >= 0x21) {
506 		/* Get hash algorithm */
507 		if (hfunc) {
508 			switch (rss_cfg->hash_algo) {
509 			case HCLGEVF_RSS_HASH_ALGO_TOEPLITZ:
510 				*hfunc = ETH_RSS_HASH_TOP;
511 				break;
512 			case HCLGEVF_RSS_HASH_ALGO_SIMPLE:
513 				*hfunc = ETH_RSS_HASH_XOR;
514 				break;
515 			default:
516 				*hfunc = ETH_RSS_HASH_UNKNOWN;
517 				break;
518 			}
519 		}
520 
521 		/* Get the RSS Key required by the user */
522 		if (key)
523 			memcpy(key, rss_cfg->rss_hash_key,
524 			       HCLGEVF_RSS_KEY_SIZE);
525 	}
526 
527 	if (indir)
528 		for (i = 0; i < HCLGEVF_RSS_IND_TBL_SIZE; i++)
529 			indir[i] = rss_cfg->rss_indirection_tbl[i];
530 
531 	return 0;
532 }
533 
534 static int hclgevf_set_rss(struct hnae3_handle *handle, const u32 *indir,
535 			   const  u8 *key, const  u8 hfunc)
536 {
537 	struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);
538 	struct hclgevf_rss_cfg *rss_cfg = &hdev->rss_cfg;
539 	int ret, i;
540 
541 	if (handle->pdev->revision >= 0x21) {
542 		/* Set the RSS Hash Key if specififed by the user */
543 		if (key) {
544 			switch (hfunc) {
545 			case ETH_RSS_HASH_TOP:
546 				rss_cfg->hash_algo =
547 					HCLGEVF_RSS_HASH_ALGO_TOEPLITZ;
548 				break;
549 			case ETH_RSS_HASH_XOR:
550 				rss_cfg->hash_algo =
551 					HCLGEVF_RSS_HASH_ALGO_SIMPLE;
552 				break;
553 			case ETH_RSS_HASH_NO_CHANGE:
554 				break;
555 			default:
556 				return -EINVAL;
557 			}
558 
559 			ret = hclgevf_set_rss_algo_key(hdev, rss_cfg->hash_algo,
560 						       key);
561 			if (ret)
562 				return ret;
563 
564 			/* Update the shadow RSS key with user specified qids */
565 			memcpy(rss_cfg->rss_hash_key, key,
566 			       HCLGEVF_RSS_KEY_SIZE);
567 		}
568 	}
569 
570 	/* update the shadow RSS table with user specified qids */
571 	for (i = 0; i < HCLGEVF_RSS_IND_TBL_SIZE; i++)
572 		rss_cfg->rss_indirection_tbl[i] = indir[i];
573 
574 	/* update the hardware */
575 	return hclgevf_set_rss_indir_table(hdev);
576 }
577 
578 static u8 hclgevf_get_rss_hash_bits(struct ethtool_rxnfc *nfc)
579 {
580 	u8 hash_sets = nfc->data & RXH_L4_B_0_1 ? HCLGEVF_S_PORT_BIT : 0;
581 
582 	if (nfc->data & RXH_L4_B_2_3)
583 		hash_sets |= HCLGEVF_D_PORT_BIT;
584 	else
585 		hash_sets &= ~HCLGEVF_D_PORT_BIT;
586 
587 	if (nfc->data & RXH_IP_SRC)
588 		hash_sets |= HCLGEVF_S_IP_BIT;
589 	else
590 		hash_sets &= ~HCLGEVF_S_IP_BIT;
591 
592 	if (nfc->data & RXH_IP_DST)
593 		hash_sets |= HCLGEVF_D_IP_BIT;
594 	else
595 		hash_sets &= ~HCLGEVF_D_IP_BIT;
596 
597 	if (nfc->flow_type == SCTP_V4_FLOW || nfc->flow_type == SCTP_V6_FLOW)
598 		hash_sets |= HCLGEVF_V_TAG_BIT;
599 
600 	return hash_sets;
601 }
602 
603 static int hclgevf_set_rss_tuple(struct hnae3_handle *handle,
604 				 struct ethtool_rxnfc *nfc)
605 {
606 	struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);
607 	struct hclgevf_rss_cfg *rss_cfg = &hdev->rss_cfg;
608 	struct hclgevf_rss_input_tuple_cmd *req;
609 	struct hclgevf_desc desc;
610 	u8 tuple_sets;
611 	int ret;
612 
613 	if (handle->pdev->revision == 0x20)
614 		return -EOPNOTSUPP;
615 
616 	if (nfc->data &
617 	    ~(RXH_IP_SRC | RXH_IP_DST | RXH_L4_B_0_1 | RXH_L4_B_2_3))
618 		return -EINVAL;
619 
620 	req = (struct hclgevf_rss_input_tuple_cmd *)desc.data;
621 	hclgevf_cmd_setup_basic_desc(&desc, HCLGEVF_OPC_RSS_INPUT_TUPLE, false);
622 
623 	req->ipv4_tcp_en = rss_cfg->rss_tuple_sets.ipv4_tcp_en;
624 	req->ipv4_udp_en = rss_cfg->rss_tuple_sets.ipv4_udp_en;
625 	req->ipv4_sctp_en = rss_cfg->rss_tuple_sets.ipv4_sctp_en;
626 	req->ipv4_fragment_en = rss_cfg->rss_tuple_sets.ipv4_fragment_en;
627 	req->ipv6_tcp_en = rss_cfg->rss_tuple_sets.ipv6_tcp_en;
628 	req->ipv6_udp_en = rss_cfg->rss_tuple_sets.ipv6_udp_en;
629 	req->ipv6_sctp_en = rss_cfg->rss_tuple_sets.ipv6_sctp_en;
630 	req->ipv6_fragment_en = rss_cfg->rss_tuple_sets.ipv6_fragment_en;
631 
632 	tuple_sets = hclgevf_get_rss_hash_bits(nfc);
633 	switch (nfc->flow_type) {
634 	case TCP_V4_FLOW:
635 		req->ipv4_tcp_en = tuple_sets;
636 		break;
637 	case TCP_V6_FLOW:
638 		req->ipv6_tcp_en = tuple_sets;
639 		break;
640 	case UDP_V4_FLOW:
641 		req->ipv4_udp_en = tuple_sets;
642 		break;
643 	case UDP_V6_FLOW:
644 		req->ipv6_udp_en = tuple_sets;
645 		break;
646 	case SCTP_V4_FLOW:
647 		req->ipv4_sctp_en = tuple_sets;
648 		break;
649 	case SCTP_V6_FLOW:
650 		if ((nfc->data & RXH_L4_B_0_1) ||
651 		    (nfc->data & RXH_L4_B_2_3))
652 			return -EINVAL;
653 
654 		req->ipv6_sctp_en = tuple_sets;
655 		break;
656 	case IPV4_FLOW:
657 		req->ipv4_fragment_en = HCLGEVF_RSS_INPUT_TUPLE_OTHER;
658 		break;
659 	case IPV6_FLOW:
660 		req->ipv6_fragment_en = HCLGEVF_RSS_INPUT_TUPLE_OTHER;
661 		break;
662 	default:
663 		return -EINVAL;
664 	}
665 
666 	ret = hclgevf_cmd_send(&hdev->hw, &desc, 1);
667 	if (ret) {
668 		dev_err(&hdev->pdev->dev,
669 			"Set rss tuple fail, status = %d\n", ret);
670 		return ret;
671 	}
672 
673 	rss_cfg->rss_tuple_sets.ipv4_tcp_en = req->ipv4_tcp_en;
674 	rss_cfg->rss_tuple_sets.ipv4_udp_en = req->ipv4_udp_en;
675 	rss_cfg->rss_tuple_sets.ipv4_sctp_en = req->ipv4_sctp_en;
676 	rss_cfg->rss_tuple_sets.ipv4_fragment_en = req->ipv4_fragment_en;
677 	rss_cfg->rss_tuple_sets.ipv6_tcp_en = req->ipv6_tcp_en;
678 	rss_cfg->rss_tuple_sets.ipv6_udp_en = req->ipv6_udp_en;
679 	rss_cfg->rss_tuple_sets.ipv6_sctp_en = req->ipv6_sctp_en;
680 	rss_cfg->rss_tuple_sets.ipv6_fragment_en = req->ipv6_fragment_en;
681 	return 0;
682 }
683 
684 static int hclgevf_get_rss_tuple(struct hnae3_handle *handle,
685 				 struct ethtool_rxnfc *nfc)
686 {
687 	struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);
688 	struct hclgevf_rss_cfg *rss_cfg = &hdev->rss_cfg;
689 	u8 tuple_sets;
690 
691 	if (handle->pdev->revision == 0x20)
692 		return -EOPNOTSUPP;
693 
694 	nfc->data = 0;
695 
696 	switch (nfc->flow_type) {
697 	case TCP_V4_FLOW:
698 		tuple_sets = rss_cfg->rss_tuple_sets.ipv4_tcp_en;
699 		break;
700 	case UDP_V4_FLOW:
701 		tuple_sets = rss_cfg->rss_tuple_sets.ipv4_udp_en;
702 		break;
703 	case TCP_V6_FLOW:
704 		tuple_sets = rss_cfg->rss_tuple_sets.ipv6_tcp_en;
705 		break;
706 	case UDP_V6_FLOW:
707 		tuple_sets = rss_cfg->rss_tuple_sets.ipv6_udp_en;
708 		break;
709 	case SCTP_V4_FLOW:
710 		tuple_sets = rss_cfg->rss_tuple_sets.ipv4_sctp_en;
711 		break;
712 	case SCTP_V6_FLOW:
713 		tuple_sets = rss_cfg->rss_tuple_sets.ipv6_sctp_en;
714 		break;
715 	case IPV4_FLOW:
716 	case IPV6_FLOW:
717 		tuple_sets = HCLGEVF_S_IP_BIT | HCLGEVF_D_IP_BIT;
718 		break;
719 	default:
720 		return -EINVAL;
721 	}
722 
723 	if (!tuple_sets)
724 		return 0;
725 
726 	if (tuple_sets & HCLGEVF_D_PORT_BIT)
727 		nfc->data |= RXH_L4_B_2_3;
728 	if (tuple_sets & HCLGEVF_S_PORT_BIT)
729 		nfc->data |= RXH_L4_B_0_1;
730 	if (tuple_sets & HCLGEVF_D_IP_BIT)
731 		nfc->data |= RXH_IP_DST;
732 	if (tuple_sets & HCLGEVF_S_IP_BIT)
733 		nfc->data |= RXH_IP_SRC;
734 
735 	return 0;
736 }
737 
738 static int hclgevf_set_rss_input_tuple(struct hclgevf_dev *hdev,
739 				       struct hclgevf_rss_cfg *rss_cfg)
740 {
741 	struct hclgevf_rss_input_tuple_cmd *req;
742 	struct hclgevf_desc desc;
743 	int ret;
744 
745 	hclgevf_cmd_setup_basic_desc(&desc, HCLGEVF_OPC_RSS_INPUT_TUPLE, false);
746 
747 	req = (struct hclgevf_rss_input_tuple_cmd *)desc.data;
748 
749 	req->ipv4_tcp_en = rss_cfg->rss_tuple_sets.ipv4_tcp_en;
750 	req->ipv4_udp_en = rss_cfg->rss_tuple_sets.ipv4_udp_en;
751 	req->ipv4_sctp_en = rss_cfg->rss_tuple_sets.ipv4_sctp_en;
752 	req->ipv4_fragment_en = rss_cfg->rss_tuple_sets.ipv4_fragment_en;
753 	req->ipv6_tcp_en = rss_cfg->rss_tuple_sets.ipv6_tcp_en;
754 	req->ipv6_udp_en = rss_cfg->rss_tuple_sets.ipv6_udp_en;
755 	req->ipv6_sctp_en = rss_cfg->rss_tuple_sets.ipv6_sctp_en;
756 	req->ipv6_fragment_en = rss_cfg->rss_tuple_sets.ipv6_fragment_en;
757 
758 	ret = hclgevf_cmd_send(&hdev->hw, &desc, 1);
759 	if (ret)
760 		dev_err(&hdev->pdev->dev,
761 			"Configure rss input fail, status = %d\n", ret);
762 	return ret;
763 }
764 
765 static int hclgevf_get_tc_size(struct hnae3_handle *handle)
766 {
767 	struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);
768 	struct hclgevf_rss_cfg *rss_cfg = &hdev->rss_cfg;
769 
770 	return rss_cfg->rss_size;
771 }
772 
773 static int hclgevf_bind_ring_to_vector(struct hnae3_handle *handle, bool en,
774 				       int vector_id,
775 				       struct hnae3_ring_chain_node *ring_chain)
776 {
777 	struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);
778 	struct hnae3_ring_chain_node *node;
779 	struct hclge_mbx_vf_to_pf_cmd *req;
780 	struct hclgevf_desc desc;
781 	int i = 0;
782 	int status;
783 	u8 type;
784 
785 	req = (struct hclge_mbx_vf_to_pf_cmd *)desc.data;
786 
787 	for (node = ring_chain; node; node = node->next) {
788 		int idx_offset = HCLGE_MBX_RING_MAP_BASIC_MSG_NUM +
789 					HCLGE_MBX_RING_NODE_VARIABLE_NUM * i;
790 
791 		if (i == 0) {
792 			hclgevf_cmd_setup_basic_desc(&desc,
793 						     HCLGEVF_OPC_MBX_VF_TO_PF,
794 						     false);
795 			type = en ?
796 				HCLGE_MBX_MAP_RING_TO_VECTOR :
797 				HCLGE_MBX_UNMAP_RING_TO_VECTOR;
798 			req->msg[0] = type;
799 			req->msg[1] = vector_id;
800 		}
801 
802 		req->msg[idx_offset] =
803 				hnae3_get_bit(node->flag, HNAE3_RING_TYPE_B);
804 		req->msg[idx_offset + 1] = node->tqp_index;
805 		req->msg[idx_offset + 2] = hnae3_get_field(node->int_gl_idx,
806 							   HNAE3_RING_GL_IDX_M,
807 							   HNAE3_RING_GL_IDX_S);
808 
809 		i++;
810 		if ((i == (HCLGE_MBX_VF_MSG_DATA_NUM -
811 		     HCLGE_MBX_RING_MAP_BASIC_MSG_NUM) /
812 		     HCLGE_MBX_RING_NODE_VARIABLE_NUM) ||
813 		    !node->next) {
814 			req->msg[2] = i;
815 
816 			status = hclgevf_cmd_send(&hdev->hw, &desc, 1);
817 			if (status) {
818 				dev_err(&hdev->pdev->dev,
819 					"Map TQP fail, status is %d.\n",
820 					status);
821 				return status;
822 			}
823 			i = 0;
824 			hclgevf_cmd_setup_basic_desc(&desc,
825 						     HCLGEVF_OPC_MBX_VF_TO_PF,
826 						     false);
827 			req->msg[0] = type;
828 			req->msg[1] = vector_id;
829 		}
830 	}
831 
832 	return 0;
833 }
834 
835 static int hclgevf_map_ring_to_vector(struct hnae3_handle *handle, int vector,
836 				      struct hnae3_ring_chain_node *ring_chain)
837 {
838 	struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);
839 	int vector_id;
840 
841 	vector_id = hclgevf_get_vector_index(hdev, vector);
842 	if (vector_id < 0) {
843 		dev_err(&handle->pdev->dev,
844 			"Get vector index fail. ret =%d\n", vector_id);
845 		return vector_id;
846 	}
847 
848 	return hclgevf_bind_ring_to_vector(handle, true, vector_id, ring_chain);
849 }
850 
851 static int hclgevf_unmap_ring_from_vector(
852 				struct hnae3_handle *handle,
853 				int vector,
854 				struct hnae3_ring_chain_node *ring_chain)
855 {
856 	struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);
857 	int ret, vector_id;
858 
859 	if (test_bit(HCLGEVF_STATE_RST_HANDLING, &hdev->state))
860 		return 0;
861 
862 	vector_id = hclgevf_get_vector_index(hdev, vector);
863 	if (vector_id < 0) {
864 		dev_err(&handle->pdev->dev,
865 			"Get vector index fail. ret =%d\n", vector_id);
866 		return vector_id;
867 	}
868 
869 	ret = hclgevf_bind_ring_to_vector(handle, false, vector_id, ring_chain);
870 	if (ret)
871 		dev_err(&handle->pdev->dev,
872 			"Unmap ring from vector fail. vector=%d, ret =%d\n",
873 			vector_id,
874 			ret);
875 
876 	return ret;
877 }
878 
879 static int hclgevf_put_vector(struct hnae3_handle *handle, int vector)
880 {
881 	struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);
882 	int vector_id;
883 
884 	vector_id = hclgevf_get_vector_index(hdev, vector);
885 	if (vector_id < 0) {
886 		dev_err(&handle->pdev->dev,
887 			"hclgevf_put_vector get vector index fail. ret =%d\n",
888 			vector_id);
889 		return vector_id;
890 	}
891 
892 	hclgevf_free_vector(hdev, vector_id);
893 
894 	return 0;
895 }
896 
897 static int hclgevf_cmd_set_promisc_mode(struct hclgevf_dev *hdev,
898 					bool en_uc_pmc, bool en_mc_pmc)
899 {
900 	struct hclge_mbx_vf_to_pf_cmd *req;
901 	struct hclgevf_desc desc;
902 	int status;
903 
904 	req = (struct hclge_mbx_vf_to_pf_cmd *)desc.data;
905 
906 	hclgevf_cmd_setup_basic_desc(&desc, HCLGEVF_OPC_MBX_VF_TO_PF, false);
907 	req->msg[0] = HCLGE_MBX_SET_PROMISC_MODE;
908 	req->msg[1] = en_uc_pmc ? 1 : 0;
909 	req->msg[2] = en_mc_pmc ? 1 : 0;
910 
911 	status = hclgevf_cmd_send(&hdev->hw, &desc, 1);
912 	if (status)
913 		dev_err(&hdev->pdev->dev,
914 			"Set promisc mode fail, status is %d.\n", status);
915 
916 	return status;
917 }
918 
919 static int hclgevf_set_promisc_mode(struct hnae3_handle *handle,
920 				    bool en_uc_pmc, bool en_mc_pmc)
921 {
922 	struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);
923 
924 	return hclgevf_cmd_set_promisc_mode(hdev, en_uc_pmc, en_mc_pmc);
925 }
926 
927 static int hclgevf_tqp_enable(struct hclgevf_dev *hdev, int tqp_id,
928 			      int stream_id, bool enable)
929 {
930 	struct hclgevf_cfg_com_tqp_queue_cmd *req;
931 	struct hclgevf_desc desc;
932 	int status;
933 
934 	req = (struct hclgevf_cfg_com_tqp_queue_cmd *)desc.data;
935 
936 	hclgevf_cmd_setup_basic_desc(&desc, HCLGEVF_OPC_CFG_COM_TQP_QUEUE,
937 				     false);
938 	req->tqp_id = cpu_to_le16(tqp_id & HCLGEVF_RING_ID_MASK);
939 	req->stream_id = cpu_to_le16(stream_id);
940 	req->enable |= enable << HCLGEVF_TQP_ENABLE_B;
941 
942 	status = hclgevf_cmd_send(&hdev->hw, &desc, 1);
943 	if (status)
944 		dev_err(&hdev->pdev->dev,
945 			"TQP enable fail, status =%d.\n", status);
946 
947 	return status;
948 }
949 
950 static void hclgevf_reset_tqp_stats(struct hnae3_handle *handle)
951 {
952 	struct hnae3_knic_private_info *kinfo = &handle->kinfo;
953 	struct hclgevf_tqp *tqp;
954 	int i;
955 
956 	for (i = 0; i < kinfo->num_tqps; i++) {
957 		tqp = container_of(kinfo->tqp[i], struct hclgevf_tqp, q);
958 		memset(&tqp->tqp_stats, 0, sizeof(tqp->tqp_stats));
959 	}
960 }
961 
962 static void hclgevf_get_mac_addr(struct hnae3_handle *handle, u8 *p)
963 {
964 	struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);
965 
966 	ether_addr_copy(p, hdev->hw.mac.mac_addr);
967 }
968 
969 static int hclgevf_set_mac_addr(struct hnae3_handle *handle, void *p,
970 				bool is_first)
971 {
972 	struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);
973 	u8 *old_mac_addr = (u8 *)hdev->hw.mac.mac_addr;
974 	u8 *new_mac_addr = (u8 *)p;
975 	u8 msg_data[ETH_ALEN * 2];
976 	u16 subcode;
977 	int status;
978 
979 	ether_addr_copy(msg_data, new_mac_addr);
980 	ether_addr_copy(&msg_data[ETH_ALEN], old_mac_addr);
981 
982 	subcode = is_first ? HCLGE_MBX_MAC_VLAN_UC_ADD :
983 			HCLGE_MBX_MAC_VLAN_UC_MODIFY;
984 
985 	status = hclgevf_send_mbx_msg(hdev, HCLGE_MBX_SET_UNICAST,
986 				      subcode, msg_data, ETH_ALEN * 2,
987 				      true, NULL, 0);
988 	if (!status)
989 		ether_addr_copy(hdev->hw.mac.mac_addr, new_mac_addr);
990 
991 	return status;
992 }
993 
994 static int hclgevf_add_uc_addr(struct hnae3_handle *handle,
995 			       const unsigned char *addr)
996 {
997 	struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);
998 
999 	return hclgevf_send_mbx_msg(hdev, HCLGE_MBX_SET_UNICAST,
1000 				    HCLGE_MBX_MAC_VLAN_UC_ADD,
1001 				    addr, ETH_ALEN, false, NULL, 0);
1002 }
1003 
1004 static int hclgevf_rm_uc_addr(struct hnae3_handle *handle,
1005 			      const unsigned char *addr)
1006 {
1007 	struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);
1008 
1009 	return hclgevf_send_mbx_msg(hdev, HCLGE_MBX_SET_UNICAST,
1010 				    HCLGE_MBX_MAC_VLAN_UC_REMOVE,
1011 				    addr, ETH_ALEN, false, NULL, 0);
1012 }
1013 
1014 static int hclgevf_add_mc_addr(struct hnae3_handle *handle,
1015 			       const unsigned char *addr)
1016 {
1017 	struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);
1018 
1019 	return hclgevf_send_mbx_msg(hdev, HCLGE_MBX_SET_MULTICAST,
1020 				    HCLGE_MBX_MAC_VLAN_MC_ADD,
1021 				    addr, ETH_ALEN, false, NULL, 0);
1022 }
1023 
1024 static int hclgevf_rm_mc_addr(struct hnae3_handle *handle,
1025 			      const unsigned char *addr)
1026 {
1027 	struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);
1028 
1029 	return hclgevf_send_mbx_msg(hdev, HCLGE_MBX_SET_MULTICAST,
1030 				    HCLGE_MBX_MAC_VLAN_MC_REMOVE,
1031 				    addr, ETH_ALEN, false, NULL, 0);
1032 }
1033 
1034 static int hclgevf_set_vlan_filter(struct hnae3_handle *handle,
1035 				   __be16 proto, u16 vlan_id,
1036 				   bool is_kill)
1037 {
1038 #define HCLGEVF_VLAN_MBX_MSG_LEN 5
1039 	struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);
1040 	u8 msg_data[HCLGEVF_VLAN_MBX_MSG_LEN];
1041 
1042 	if (vlan_id > 4095)
1043 		return -EINVAL;
1044 
1045 	if (proto != htons(ETH_P_8021Q))
1046 		return -EPROTONOSUPPORT;
1047 
1048 	msg_data[0] = is_kill;
1049 	memcpy(&msg_data[1], &vlan_id, sizeof(vlan_id));
1050 	memcpy(&msg_data[3], &proto, sizeof(proto));
1051 	return hclgevf_send_mbx_msg(hdev, HCLGE_MBX_SET_VLAN,
1052 				    HCLGE_MBX_VLAN_FILTER, msg_data,
1053 				    HCLGEVF_VLAN_MBX_MSG_LEN, false, NULL, 0);
1054 }
1055 
1056 static int hclgevf_en_hw_strip_rxvtag(struct hnae3_handle *handle, bool enable)
1057 {
1058 	struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);
1059 	u8 msg_data;
1060 
1061 	msg_data = enable ? 1 : 0;
1062 	return hclgevf_send_mbx_msg(hdev, HCLGE_MBX_SET_VLAN,
1063 				    HCLGE_MBX_VLAN_RX_OFF_CFG, &msg_data,
1064 				    1, false, NULL, 0);
1065 }
1066 
1067 static int hclgevf_reset_tqp(struct hnae3_handle *handle, u16 queue_id)
1068 {
1069 	struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);
1070 	u8 msg_data[2];
1071 	int ret;
1072 
1073 	memcpy(&msg_data[0], &queue_id, sizeof(queue_id));
1074 
1075 	/* disable vf queue before send queue reset msg to PF */
1076 	ret = hclgevf_tqp_enable(hdev, queue_id, 0, false);
1077 	if (ret)
1078 		return ret;
1079 
1080 	return hclgevf_send_mbx_msg(hdev, HCLGE_MBX_QUEUE_RESET, 0, msg_data,
1081 				    2, true, NULL, 0);
1082 }
1083 
1084 static int hclgevf_notify_client(struct hclgevf_dev *hdev,
1085 				 enum hnae3_reset_notify_type type)
1086 {
1087 	struct hnae3_client *client = hdev->nic_client;
1088 	struct hnae3_handle *handle = &hdev->nic;
1089 
1090 	if (!client->ops->reset_notify)
1091 		return -EOPNOTSUPP;
1092 
1093 	return client->ops->reset_notify(handle, type);
1094 }
1095 
1096 static int hclgevf_reset_wait(struct hclgevf_dev *hdev)
1097 {
1098 #define HCLGEVF_RESET_WAIT_US	20000
1099 #define HCLGEVF_RESET_WAIT_CNT	2000
1100 #define HCLGEVF_RESET_WAIT_TIMEOUT_US	\
1101 	(HCLGEVF_RESET_WAIT_US * HCLGEVF_RESET_WAIT_CNT)
1102 
1103 	u32 val;
1104 	int ret;
1105 
1106 	/* wait to check the hardware reset completion status */
1107 	val = hclgevf_read_dev(&hdev->hw, HCLGEVF_RST_ING);
1108 	dev_info(&hdev->pdev->dev, "checking vf resetting status: %x\n", val);
1109 
1110 	ret = readl_poll_timeout(hdev->hw.io_base + HCLGEVF_RST_ING, val,
1111 				 !(val & HCLGEVF_RST_ING_BITS),
1112 				 HCLGEVF_RESET_WAIT_US,
1113 				 HCLGEVF_RESET_WAIT_TIMEOUT_US);
1114 
1115 	/* hardware completion status should be available by this time */
1116 	if (ret) {
1117 		dev_err(&hdev->pdev->dev,
1118 			"could'nt get reset done status from h/w, timeout!\n");
1119 		return ret;
1120 	}
1121 
1122 	/* we will wait a bit more to let reset of the stack to complete. This
1123 	 * might happen in case reset assertion was made by PF. Yes, this also
1124 	 * means we might end up waiting bit more even for VF reset.
1125 	 */
1126 	msleep(5000);
1127 
1128 	return 0;
1129 }
1130 
1131 static int hclgevf_reset_stack(struct hclgevf_dev *hdev)
1132 {
1133 	int ret;
1134 
1135 	/* uninitialize the nic client */
1136 	hclgevf_notify_client(hdev, HNAE3_UNINIT_CLIENT);
1137 
1138 	/* re-initialize the hclge device */
1139 	ret = hclgevf_reset_hdev(hdev);
1140 	if (ret) {
1141 		dev_err(&hdev->pdev->dev,
1142 			"hclge device re-init failed, VF is disabled!\n");
1143 		return ret;
1144 	}
1145 
1146 	/* bring up the nic client again */
1147 	hclgevf_notify_client(hdev, HNAE3_INIT_CLIENT);
1148 
1149 	return 0;
1150 }
1151 
1152 static int hclgevf_reset_prepare_wait(struct hclgevf_dev *hdev)
1153 {
1154 	int ret = 0;
1155 
1156 	switch (hdev->reset_type) {
1157 	case HNAE3_VF_FUNC_RESET:
1158 		ret = hclgevf_send_mbx_msg(hdev, HCLGE_MBX_RESET, 0, NULL,
1159 					   0, true, NULL, sizeof(u8));
1160 		break;
1161 	default:
1162 		break;
1163 	}
1164 
1165 	dev_info(&hdev->pdev->dev, "prepare reset(%d) wait done, ret:%d\n",
1166 		 hdev->reset_type, ret);
1167 
1168 	return ret;
1169 }
1170 
1171 static int hclgevf_reset(struct hclgevf_dev *hdev)
1172 {
1173 	struct hnae3_ae_dev *ae_dev = pci_get_drvdata(hdev->pdev);
1174 	int ret;
1175 
1176 	/* Initialize ae_dev reset status as well, in case enet layer wants to
1177 	 * know if device is undergoing reset
1178 	 */
1179 	ae_dev->reset_type = hdev->reset_type;
1180 	hdev->reset_count++;
1181 	rtnl_lock();
1182 
1183 	/* bring down the nic to stop any ongoing TX/RX */
1184 	hclgevf_notify_client(hdev, HNAE3_DOWN_CLIENT);
1185 
1186 	rtnl_unlock();
1187 
1188 	hclgevf_reset_prepare_wait(hdev);
1189 
1190 	/* check if VF could successfully fetch the hardware reset completion
1191 	 * status from the hardware
1192 	 */
1193 	ret = hclgevf_reset_wait(hdev);
1194 	if (ret) {
1195 		/* can't do much in this situation, will disable VF */
1196 		dev_err(&hdev->pdev->dev,
1197 			"VF failed(=%d) to fetch H/W reset completion status\n",
1198 			ret);
1199 
1200 		dev_warn(&hdev->pdev->dev, "VF reset failed, disabling VF!\n");
1201 		rtnl_lock();
1202 		hclgevf_notify_client(hdev, HNAE3_UNINIT_CLIENT);
1203 
1204 		rtnl_unlock();
1205 		return ret;
1206 	}
1207 
1208 	rtnl_lock();
1209 
1210 	/* now, re-initialize the nic client and ae device*/
1211 	ret = hclgevf_reset_stack(hdev);
1212 	if (ret)
1213 		dev_err(&hdev->pdev->dev, "failed to reset VF stack\n");
1214 
1215 	/* bring up the nic to enable TX/RX again */
1216 	hclgevf_notify_client(hdev, HNAE3_UP_CLIENT);
1217 
1218 	rtnl_unlock();
1219 
1220 	return ret;
1221 }
1222 
1223 static enum hnae3_reset_type hclgevf_get_reset_level(struct hclgevf_dev *hdev,
1224 						     unsigned long *addr)
1225 {
1226 	enum hnae3_reset_type rst_level = HNAE3_NONE_RESET;
1227 
1228 	/* return the highest priority reset level amongst all */
1229 	if (test_bit(HNAE3_VF_FULL_RESET, addr)) {
1230 		rst_level = HNAE3_VF_FULL_RESET;
1231 		clear_bit(HNAE3_VF_FULL_RESET, addr);
1232 		clear_bit(HNAE3_VF_FUNC_RESET, addr);
1233 	} else if (test_bit(HNAE3_VF_PF_FUNC_RESET, addr)) {
1234 		rst_level = HNAE3_VF_PF_FUNC_RESET;
1235 		clear_bit(HNAE3_VF_PF_FUNC_RESET, addr);
1236 		clear_bit(HNAE3_VF_FUNC_RESET, addr);
1237 	} else if (test_bit(HNAE3_VF_FUNC_RESET, addr)) {
1238 		rst_level = HNAE3_VF_FUNC_RESET;
1239 		clear_bit(HNAE3_VF_FUNC_RESET, addr);
1240 	}
1241 
1242 	return rst_level;
1243 }
1244 
1245 static void hclgevf_reset_event(struct pci_dev *pdev,
1246 				struct hnae3_handle *handle)
1247 {
1248 	struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);
1249 
1250 	dev_info(&hdev->pdev->dev, "received reset request from VF enet\n");
1251 
1252 	if (!hdev->default_reset_request)
1253 		hdev->reset_level =
1254 			hclgevf_get_reset_level(hdev,
1255 						&hdev->default_reset_request);
1256 	else
1257 		hdev->reset_level = HNAE3_VF_FUNC_RESET;
1258 
1259 	/* reset of this VF requested */
1260 	set_bit(HCLGEVF_RESET_REQUESTED, &hdev->reset_state);
1261 	hclgevf_reset_task_schedule(hdev);
1262 
1263 	hdev->last_reset_time = jiffies;
1264 }
1265 
1266 static void hclgevf_set_def_reset_request(struct hnae3_ae_dev *ae_dev,
1267 					  enum hnae3_reset_type rst_type)
1268 {
1269 	struct hclgevf_dev *hdev = ae_dev->priv;
1270 
1271 	set_bit(rst_type, &hdev->default_reset_request);
1272 }
1273 
1274 static u32 hclgevf_get_fw_version(struct hnae3_handle *handle)
1275 {
1276 	struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);
1277 
1278 	return hdev->fw_version;
1279 }
1280 
1281 static void hclgevf_get_misc_vector(struct hclgevf_dev *hdev)
1282 {
1283 	struct hclgevf_misc_vector *vector = &hdev->misc_vector;
1284 
1285 	vector->vector_irq = pci_irq_vector(hdev->pdev,
1286 					    HCLGEVF_MISC_VECTOR_NUM);
1287 	vector->addr = hdev->hw.io_base + HCLGEVF_MISC_VECTOR_REG_BASE;
1288 	/* vector status always valid for Vector 0 */
1289 	hdev->vector_status[HCLGEVF_MISC_VECTOR_NUM] = 0;
1290 	hdev->vector_irq[HCLGEVF_MISC_VECTOR_NUM] = vector->vector_irq;
1291 
1292 	hdev->num_msi_left -= 1;
1293 	hdev->num_msi_used += 1;
1294 }
1295 
1296 void hclgevf_reset_task_schedule(struct hclgevf_dev *hdev)
1297 {
1298 	if (!test_bit(HCLGEVF_STATE_RST_SERVICE_SCHED, &hdev->state) &&
1299 	    !test_bit(HCLGEVF_STATE_RST_HANDLING, &hdev->state)) {
1300 		set_bit(HCLGEVF_STATE_RST_SERVICE_SCHED, &hdev->state);
1301 		schedule_work(&hdev->rst_service_task);
1302 	}
1303 }
1304 
1305 void hclgevf_mbx_task_schedule(struct hclgevf_dev *hdev)
1306 {
1307 	if (!test_bit(HCLGEVF_STATE_MBX_SERVICE_SCHED, &hdev->state) &&
1308 	    !test_bit(HCLGEVF_STATE_MBX_HANDLING, &hdev->state)) {
1309 		set_bit(HCLGEVF_STATE_MBX_SERVICE_SCHED, &hdev->state);
1310 		schedule_work(&hdev->mbx_service_task);
1311 	}
1312 }
1313 
1314 static void hclgevf_task_schedule(struct hclgevf_dev *hdev)
1315 {
1316 	if (!test_bit(HCLGEVF_STATE_DOWN, &hdev->state)  &&
1317 	    !test_and_set_bit(HCLGEVF_STATE_SERVICE_SCHED, &hdev->state))
1318 		schedule_work(&hdev->service_task);
1319 }
1320 
1321 static void hclgevf_deferred_task_schedule(struct hclgevf_dev *hdev)
1322 {
1323 	/* if we have any pending mailbox event then schedule the mbx task */
1324 	if (hdev->mbx_event_pending)
1325 		hclgevf_mbx_task_schedule(hdev);
1326 
1327 	if (test_bit(HCLGEVF_RESET_PENDING, &hdev->reset_state))
1328 		hclgevf_reset_task_schedule(hdev);
1329 }
1330 
1331 static void hclgevf_service_timer(struct timer_list *t)
1332 {
1333 	struct hclgevf_dev *hdev = from_timer(hdev, t, service_timer);
1334 
1335 	mod_timer(&hdev->service_timer, jiffies + 5 * HZ);
1336 
1337 	hclgevf_task_schedule(hdev);
1338 }
1339 
1340 static void hclgevf_reset_service_task(struct work_struct *work)
1341 {
1342 	struct hclgevf_dev *hdev =
1343 		container_of(work, struct hclgevf_dev, rst_service_task);
1344 	int ret;
1345 
1346 	if (test_and_set_bit(HCLGEVF_STATE_RST_HANDLING, &hdev->state))
1347 		return;
1348 
1349 	clear_bit(HCLGEVF_STATE_RST_SERVICE_SCHED, &hdev->state);
1350 
1351 	if (test_and_clear_bit(HCLGEVF_RESET_PENDING,
1352 			       &hdev->reset_state)) {
1353 		/* PF has initmated that it is about to reset the hardware.
1354 		 * We now have to poll & check if harware has actually completed
1355 		 * the reset sequence. On hardware reset completion, VF needs to
1356 		 * reset the client and ae device.
1357 		 */
1358 		hdev->reset_attempts = 0;
1359 
1360 		hdev->last_reset_time = jiffies;
1361 		while ((hdev->reset_type =
1362 			hclgevf_get_reset_level(hdev, &hdev->reset_pending))
1363 		       != HNAE3_NONE_RESET) {
1364 			ret = hclgevf_reset(hdev);
1365 			if (ret)
1366 				dev_err(&hdev->pdev->dev,
1367 					"VF stack reset failed %d.\n", ret);
1368 		}
1369 	} else if (test_and_clear_bit(HCLGEVF_RESET_REQUESTED,
1370 				      &hdev->reset_state)) {
1371 		/* we could be here when either of below happens:
1372 		 * 1. reset was initiated due to watchdog timeout due to
1373 		 *    a. IMP was earlier reset and our TX got choked down and
1374 		 *       which resulted in watchdog reacting and inducing VF
1375 		 *       reset. This also means our cmdq would be unreliable.
1376 		 *    b. problem in TX due to other lower layer(example link
1377 		 *       layer not functioning properly etc.)
1378 		 * 2. VF reset might have been initiated due to some config
1379 		 *    change.
1380 		 *
1381 		 * NOTE: Theres no clear way to detect above cases than to react
1382 		 * to the response of PF for this reset request. PF will ack the
1383 		 * 1b and 2. cases but we will not get any intimation about 1a
1384 		 * from PF as cmdq would be in unreliable state i.e. mailbox
1385 		 * communication between PF and VF would be broken.
1386 		 */
1387 
1388 		/* if we are never geting into pending state it means either:
1389 		 * 1. PF is not receiving our request which could be due to IMP
1390 		 *    reset
1391 		 * 2. PF is screwed
1392 		 * We cannot do much for 2. but to check first we can try reset
1393 		 * our PCIe + stack and see if it alleviates the problem.
1394 		 */
1395 		if (hdev->reset_attempts > 3) {
1396 			/* prepare for full reset of stack + pcie interface */
1397 			set_bit(HNAE3_VF_FULL_RESET, &hdev->reset_pending);
1398 
1399 			/* "defer" schedule the reset task again */
1400 			set_bit(HCLGEVF_RESET_PENDING, &hdev->reset_state);
1401 		} else {
1402 			hdev->reset_attempts++;
1403 
1404 			set_bit(hdev->reset_level, &hdev->reset_pending);
1405 			set_bit(HCLGEVF_RESET_PENDING, &hdev->reset_state);
1406 		}
1407 		hclgevf_reset_task_schedule(hdev);
1408 	}
1409 
1410 	clear_bit(HCLGEVF_STATE_RST_HANDLING, &hdev->state);
1411 }
1412 
1413 static void hclgevf_mailbox_service_task(struct work_struct *work)
1414 {
1415 	struct hclgevf_dev *hdev;
1416 
1417 	hdev = container_of(work, struct hclgevf_dev, mbx_service_task);
1418 
1419 	if (test_and_set_bit(HCLGEVF_STATE_MBX_HANDLING, &hdev->state))
1420 		return;
1421 
1422 	clear_bit(HCLGEVF_STATE_MBX_SERVICE_SCHED, &hdev->state);
1423 
1424 	hclgevf_mbx_async_handler(hdev);
1425 
1426 	clear_bit(HCLGEVF_STATE_MBX_HANDLING, &hdev->state);
1427 }
1428 
1429 static void hclgevf_service_task(struct work_struct *work)
1430 {
1431 	struct hclgevf_dev *hdev;
1432 
1433 	hdev = container_of(work, struct hclgevf_dev, service_task);
1434 
1435 	/* request the link status from the PF. PF would be able to tell VF
1436 	 * about such updates in future so we might remove this later
1437 	 */
1438 	hclgevf_request_link_info(hdev);
1439 
1440 	hclgevf_deferred_task_schedule(hdev);
1441 
1442 	clear_bit(HCLGEVF_STATE_SERVICE_SCHED, &hdev->state);
1443 }
1444 
1445 static void hclgevf_clear_event_cause(struct hclgevf_dev *hdev, u32 regclr)
1446 {
1447 	hclgevf_write_dev(&hdev->hw, HCLGEVF_VECTOR0_CMDQ_SRC_REG, regclr);
1448 }
1449 
1450 static bool hclgevf_check_event_cause(struct hclgevf_dev *hdev, u32 *clearval)
1451 {
1452 	u32 cmdq_src_reg;
1453 
1454 	/* fetch the events from their corresponding regs */
1455 	cmdq_src_reg = hclgevf_read_dev(&hdev->hw,
1456 					HCLGEVF_VECTOR0_CMDQ_SRC_REG);
1457 
1458 	/* check for vector0 mailbox(=CMDQ RX) event source */
1459 	if (BIT(HCLGEVF_VECTOR0_RX_CMDQ_INT_B) & cmdq_src_reg) {
1460 		cmdq_src_reg &= ~BIT(HCLGEVF_VECTOR0_RX_CMDQ_INT_B);
1461 		*clearval = cmdq_src_reg;
1462 		return true;
1463 	}
1464 
1465 	dev_dbg(&hdev->pdev->dev, "vector 0 interrupt from unknown source\n");
1466 
1467 	return false;
1468 }
1469 
1470 static void hclgevf_enable_vector(struct hclgevf_misc_vector *vector, bool en)
1471 {
1472 	writel(en ? 1 : 0, vector->addr);
1473 }
1474 
1475 static irqreturn_t hclgevf_misc_irq_handle(int irq, void *data)
1476 {
1477 	struct hclgevf_dev *hdev = data;
1478 	u32 clearval;
1479 
1480 	hclgevf_enable_vector(&hdev->misc_vector, false);
1481 	if (!hclgevf_check_event_cause(hdev, &clearval))
1482 		goto skip_sched;
1483 
1484 	hclgevf_mbx_handler(hdev);
1485 
1486 	hclgevf_clear_event_cause(hdev, clearval);
1487 
1488 skip_sched:
1489 	hclgevf_enable_vector(&hdev->misc_vector, true);
1490 
1491 	return IRQ_HANDLED;
1492 }
1493 
1494 static int hclgevf_configure(struct hclgevf_dev *hdev)
1495 {
1496 	int ret;
1497 
1498 	hdev->hw.mac.media_type = HNAE3_MEDIA_TYPE_NONE;
1499 
1500 	/* get queue configuration from PF */
1501 	ret = hclgevf_get_queue_info(hdev);
1502 	if (ret)
1503 		return ret;
1504 	/* get tc configuration from PF */
1505 	return hclgevf_get_tc_info(hdev);
1506 }
1507 
1508 static int hclgevf_alloc_hdev(struct hnae3_ae_dev *ae_dev)
1509 {
1510 	struct pci_dev *pdev = ae_dev->pdev;
1511 	struct hclgevf_dev *hdev = ae_dev->priv;
1512 
1513 	hdev = devm_kzalloc(&pdev->dev, sizeof(*hdev), GFP_KERNEL);
1514 	if (!hdev)
1515 		return -ENOMEM;
1516 
1517 	hdev->pdev = pdev;
1518 	hdev->ae_dev = ae_dev;
1519 	ae_dev->priv = hdev;
1520 
1521 	return 0;
1522 }
1523 
1524 static int hclgevf_init_roce_base_info(struct hclgevf_dev *hdev)
1525 {
1526 	struct hnae3_handle *roce = &hdev->roce;
1527 	struct hnae3_handle *nic = &hdev->nic;
1528 
1529 	roce->rinfo.num_vectors = hdev->num_roce_msix;
1530 
1531 	if (hdev->num_msi_left < roce->rinfo.num_vectors ||
1532 	    hdev->num_msi_left == 0)
1533 		return -EINVAL;
1534 
1535 	roce->rinfo.base_vector = hdev->roce_base_vector;
1536 
1537 	roce->rinfo.netdev = nic->kinfo.netdev;
1538 	roce->rinfo.roce_io_base = hdev->hw.io_base;
1539 
1540 	roce->pdev = nic->pdev;
1541 	roce->ae_algo = nic->ae_algo;
1542 	roce->numa_node_mask = nic->numa_node_mask;
1543 
1544 	return 0;
1545 }
1546 
1547 static int hclgevf_rss_init_hw(struct hclgevf_dev *hdev)
1548 {
1549 	struct hclgevf_rss_cfg *rss_cfg = &hdev->rss_cfg;
1550 	int i, ret;
1551 
1552 	rss_cfg->rss_size = hdev->rss_size_max;
1553 
1554 	if (hdev->pdev->revision >= 0x21) {
1555 		rss_cfg->hash_algo = HCLGEVF_RSS_HASH_ALGO_TOEPLITZ;
1556 		netdev_rss_key_fill(rss_cfg->rss_hash_key,
1557 				    HCLGEVF_RSS_KEY_SIZE);
1558 
1559 		ret = hclgevf_set_rss_algo_key(hdev, rss_cfg->hash_algo,
1560 					       rss_cfg->rss_hash_key);
1561 		if (ret)
1562 			return ret;
1563 
1564 		rss_cfg->rss_tuple_sets.ipv4_tcp_en =
1565 					HCLGEVF_RSS_INPUT_TUPLE_OTHER;
1566 		rss_cfg->rss_tuple_sets.ipv4_udp_en =
1567 					HCLGEVF_RSS_INPUT_TUPLE_OTHER;
1568 		rss_cfg->rss_tuple_sets.ipv4_sctp_en =
1569 					HCLGEVF_RSS_INPUT_TUPLE_SCTP;
1570 		rss_cfg->rss_tuple_sets.ipv4_fragment_en =
1571 					HCLGEVF_RSS_INPUT_TUPLE_OTHER;
1572 		rss_cfg->rss_tuple_sets.ipv6_tcp_en =
1573 					HCLGEVF_RSS_INPUT_TUPLE_OTHER;
1574 		rss_cfg->rss_tuple_sets.ipv6_udp_en =
1575 					HCLGEVF_RSS_INPUT_TUPLE_OTHER;
1576 		rss_cfg->rss_tuple_sets.ipv6_sctp_en =
1577 					HCLGEVF_RSS_INPUT_TUPLE_SCTP;
1578 		rss_cfg->rss_tuple_sets.ipv6_fragment_en =
1579 					HCLGEVF_RSS_INPUT_TUPLE_OTHER;
1580 
1581 		ret = hclgevf_set_rss_input_tuple(hdev, rss_cfg);
1582 		if (ret)
1583 			return ret;
1584 
1585 	}
1586 
1587 	/* Initialize RSS indirect table for each vport */
1588 	for (i = 0; i < HCLGEVF_RSS_IND_TBL_SIZE; i++)
1589 		rss_cfg->rss_indirection_tbl[i] = i % hdev->rss_size_max;
1590 
1591 	ret = hclgevf_set_rss_indir_table(hdev);
1592 	if (ret)
1593 		return ret;
1594 
1595 	return hclgevf_set_rss_tc_mode(hdev, hdev->rss_size_max);
1596 }
1597 
1598 static int hclgevf_init_vlan_config(struct hclgevf_dev *hdev)
1599 {
1600 	/* other vlan config(like, VLAN TX/RX offload) would also be added
1601 	 * here later
1602 	 */
1603 	return hclgevf_set_vlan_filter(&hdev->nic, htons(ETH_P_8021Q), 0,
1604 				       false);
1605 }
1606 
1607 static int hclgevf_ae_start(struct hnae3_handle *handle)
1608 {
1609 	struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);
1610 
1611 	/* reset tqp stats */
1612 	hclgevf_reset_tqp_stats(handle);
1613 
1614 	hclgevf_request_link_info(hdev);
1615 
1616 	clear_bit(HCLGEVF_STATE_DOWN, &hdev->state);
1617 	mod_timer(&hdev->service_timer, jiffies + HZ);
1618 
1619 	return 0;
1620 }
1621 
1622 static void hclgevf_ae_stop(struct hnae3_handle *handle)
1623 {
1624 	struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);
1625 
1626 	set_bit(HCLGEVF_STATE_DOWN, &hdev->state);
1627 
1628 	/* reset tqp stats */
1629 	hclgevf_reset_tqp_stats(handle);
1630 	del_timer_sync(&hdev->service_timer);
1631 	cancel_work_sync(&hdev->service_task);
1632 	clear_bit(HCLGEVF_STATE_SERVICE_SCHED, &hdev->state);
1633 	hclgevf_update_link_status(hdev, 0);
1634 }
1635 
1636 static void hclgevf_state_init(struct hclgevf_dev *hdev)
1637 {
1638 	/* setup tasks for the MBX */
1639 	INIT_WORK(&hdev->mbx_service_task, hclgevf_mailbox_service_task);
1640 	clear_bit(HCLGEVF_STATE_MBX_SERVICE_SCHED, &hdev->state);
1641 	clear_bit(HCLGEVF_STATE_MBX_HANDLING, &hdev->state);
1642 
1643 	/* setup tasks for service timer */
1644 	timer_setup(&hdev->service_timer, hclgevf_service_timer, 0);
1645 
1646 	INIT_WORK(&hdev->service_task, hclgevf_service_task);
1647 	clear_bit(HCLGEVF_STATE_SERVICE_SCHED, &hdev->state);
1648 
1649 	INIT_WORK(&hdev->rst_service_task, hclgevf_reset_service_task);
1650 
1651 	mutex_init(&hdev->mbx_resp.mbx_mutex);
1652 
1653 	/* bring the device down */
1654 	set_bit(HCLGEVF_STATE_DOWN, &hdev->state);
1655 }
1656 
1657 static void hclgevf_state_uninit(struct hclgevf_dev *hdev)
1658 {
1659 	set_bit(HCLGEVF_STATE_DOWN, &hdev->state);
1660 
1661 	if (hdev->service_timer.function)
1662 		del_timer_sync(&hdev->service_timer);
1663 	if (hdev->service_task.func)
1664 		cancel_work_sync(&hdev->service_task);
1665 	if (hdev->mbx_service_task.func)
1666 		cancel_work_sync(&hdev->mbx_service_task);
1667 	if (hdev->rst_service_task.func)
1668 		cancel_work_sync(&hdev->rst_service_task);
1669 
1670 	mutex_destroy(&hdev->mbx_resp.mbx_mutex);
1671 }
1672 
1673 static int hclgevf_init_msi(struct hclgevf_dev *hdev)
1674 {
1675 	struct pci_dev *pdev = hdev->pdev;
1676 	int vectors;
1677 	int i;
1678 
1679 	if (hnae3_get_bit(hdev->ae_dev->flag, HNAE3_DEV_SUPPORT_ROCE_B))
1680 		vectors = pci_alloc_irq_vectors(pdev,
1681 						hdev->roce_base_msix_offset + 1,
1682 						hdev->num_msi,
1683 						PCI_IRQ_MSIX);
1684 	else
1685 		vectors = pci_alloc_irq_vectors(pdev, 1, hdev->num_msi,
1686 						PCI_IRQ_MSI | PCI_IRQ_MSIX);
1687 
1688 	if (vectors < 0) {
1689 		dev_err(&pdev->dev,
1690 			"failed(%d) to allocate MSI/MSI-X vectors\n",
1691 			vectors);
1692 		return vectors;
1693 	}
1694 	if (vectors < hdev->num_msi)
1695 		dev_warn(&hdev->pdev->dev,
1696 			 "requested %d MSI/MSI-X, but allocated %d MSI/MSI-X\n",
1697 			 hdev->num_msi, vectors);
1698 
1699 	hdev->num_msi = vectors;
1700 	hdev->num_msi_left = vectors;
1701 	hdev->base_msi_vector = pdev->irq;
1702 	hdev->roce_base_vector = pdev->irq + hdev->roce_base_msix_offset;
1703 
1704 	hdev->vector_status = devm_kcalloc(&pdev->dev, hdev->num_msi,
1705 					   sizeof(u16), GFP_KERNEL);
1706 	if (!hdev->vector_status) {
1707 		pci_free_irq_vectors(pdev);
1708 		return -ENOMEM;
1709 	}
1710 
1711 	for (i = 0; i < hdev->num_msi; i++)
1712 		hdev->vector_status[i] = HCLGEVF_INVALID_VPORT;
1713 
1714 	hdev->vector_irq = devm_kcalloc(&pdev->dev, hdev->num_msi,
1715 					sizeof(int), GFP_KERNEL);
1716 	if (!hdev->vector_irq) {
1717 		pci_free_irq_vectors(pdev);
1718 		return -ENOMEM;
1719 	}
1720 
1721 	return 0;
1722 }
1723 
1724 static void hclgevf_uninit_msi(struct hclgevf_dev *hdev)
1725 {
1726 	struct pci_dev *pdev = hdev->pdev;
1727 
1728 	pci_free_irq_vectors(pdev);
1729 }
1730 
1731 static int hclgevf_misc_irq_init(struct hclgevf_dev *hdev)
1732 {
1733 	int ret = 0;
1734 
1735 	hclgevf_get_misc_vector(hdev);
1736 
1737 	ret = request_irq(hdev->misc_vector.vector_irq, hclgevf_misc_irq_handle,
1738 			  0, "hclgevf_cmd", hdev);
1739 	if (ret) {
1740 		dev_err(&hdev->pdev->dev, "VF failed to request misc irq(%d)\n",
1741 			hdev->misc_vector.vector_irq);
1742 		return ret;
1743 	}
1744 
1745 	hclgevf_clear_event_cause(hdev, 0);
1746 
1747 	/* enable misc. vector(vector 0) */
1748 	hclgevf_enable_vector(&hdev->misc_vector, true);
1749 
1750 	return ret;
1751 }
1752 
1753 static void hclgevf_misc_irq_uninit(struct hclgevf_dev *hdev)
1754 {
1755 	/* disable misc vector(vector 0) */
1756 	hclgevf_enable_vector(&hdev->misc_vector, false);
1757 	synchronize_irq(hdev->misc_vector.vector_irq);
1758 	free_irq(hdev->misc_vector.vector_irq, hdev);
1759 	hclgevf_free_vector(hdev, 0);
1760 }
1761 
1762 static int hclgevf_init_client_instance(struct hnae3_client *client,
1763 					struct hnae3_ae_dev *ae_dev)
1764 {
1765 	struct hclgevf_dev *hdev = ae_dev->priv;
1766 	int ret;
1767 
1768 	switch (client->type) {
1769 	case HNAE3_CLIENT_KNIC:
1770 		hdev->nic_client = client;
1771 		hdev->nic.client = client;
1772 
1773 		ret = client->ops->init_instance(&hdev->nic);
1774 		if (ret)
1775 			goto clear_nic;
1776 
1777 		hnae3_set_client_init_flag(client, ae_dev, 1);
1778 
1779 		if (hdev->roce_client && hnae3_dev_roce_supported(hdev)) {
1780 			struct hnae3_client *rc = hdev->roce_client;
1781 
1782 			ret = hclgevf_init_roce_base_info(hdev);
1783 			if (ret)
1784 				goto clear_roce;
1785 			ret = rc->ops->init_instance(&hdev->roce);
1786 			if (ret)
1787 				goto clear_roce;
1788 
1789 			hnae3_set_client_init_flag(hdev->roce_client, ae_dev,
1790 						   1);
1791 		}
1792 		break;
1793 	case HNAE3_CLIENT_UNIC:
1794 		hdev->nic_client = client;
1795 		hdev->nic.client = client;
1796 
1797 		ret = client->ops->init_instance(&hdev->nic);
1798 		if (ret)
1799 			goto clear_nic;
1800 
1801 		hnae3_set_client_init_flag(client, ae_dev, 1);
1802 		break;
1803 	case HNAE3_CLIENT_ROCE:
1804 		if (hnae3_dev_roce_supported(hdev)) {
1805 			hdev->roce_client = client;
1806 			hdev->roce.client = client;
1807 		}
1808 
1809 		if (hdev->roce_client && hdev->nic_client) {
1810 			ret = hclgevf_init_roce_base_info(hdev);
1811 			if (ret)
1812 				goto clear_roce;
1813 
1814 			ret = client->ops->init_instance(&hdev->roce);
1815 			if (ret)
1816 				goto clear_roce;
1817 		}
1818 
1819 		hnae3_set_client_init_flag(client, ae_dev, 1);
1820 		break;
1821 	default:
1822 		return -EINVAL;
1823 	}
1824 
1825 	return 0;
1826 
1827 clear_nic:
1828 	hdev->nic_client = NULL;
1829 	hdev->nic.client = NULL;
1830 	return ret;
1831 clear_roce:
1832 	hdev->roce_client = NULL;
1833 	hdev->roce.client = NULL;
1834 	return ret;
1835 }
1836 
1837 static void hclgevf_uninit_client_instance(struct hnae3_client *client,
1838 					   struct hnae3_ae_dev *ae_dev)
1839 {
1840 	struct hclgevf_dev *hdev = ae_dev->priv;
1841 
1842 	/* un-init roce, if it exists */
1843 	if (hdev->roce_client) {
1844 		hdev->roce_client->ops->uninit_instance(&hdev->roce, 0);
1845 		hdev->roce_client = NULL;
1846 		hdev->roce.client = NULL;
1847 	}
1848 
1849 	/* un-init nic/unic, if this was not called by roce client */
1850 	if (client->ops->uninit_instance && hdev->nic_client &&
1851 	    client->type != HNAE3_CLIENT_ROCE) {
1852 		client->ops->uninit_instance(&hdev->nic, 0);
1853 		hdev->nic_client = NULL;
1854 		hdev->nic.client = NULL;
1855 	}
1856 }
1857 
1858 static int hclgevf_pci_init(struct hclgevf_dev *hdev)
1859 {
1860 	struct pci_dev *pdev = hdev->pdev;
1861 	struct hclgevf_hw *hw;
1862 	int ret;
1863 
1864 	ret = pci_enable_device(pdev);
1865 	if (ret) {
1866 		dev_err(&pdev->dev, "failed to enable PCI device\n");
1867 		return ret;
1868 	}
1869 
1870 	ret = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
1871 	if (ret) {
1872 		dev_err(&pdev->dev, "can't set consistent PCI DMA, exiting");
1873 		goto err_disable_device;
1874 	}
1875 
1876 	ret = pci_request_regions(pdev, HCLGEVF_DRIVER_NAME);
1877 	if (ret) {
1878 		dev_err(&pdev->dev, "PCI request regions failed %d\n", ret);
1879 		goto err_disable_device;
1880 	}
1881 
1882 	pci_set_master(pdev);
1883 	hw = &hdev->hw;
1884 	hw->hdev = hdev;
1885 	hw->io_base = pci_iomap(pdev, 2, 0);
1886 	if (!hw->io_base) {
1887 		dev_err(&pdev->dev, "can't map configuration register space\n");
1888 		ret = -ENOMEM;
1889 		goto err_clr_master;
1890 	}
1891 
1892 	return 0;
1893 
1894 err_clr_master:
1895 	pci_clear_master(pdev);
1896 	pci_release_regions(pdev);
1897 err_disable_device:
1898 	pci_disable_device(pdev);
1899 
1900 	return ret;
1901 }
1902 
1903 static void hclgevf_pci_uninit(struct hclgevf_dev *hdev)
1904 {
1905 	struct pci_dev *pdev = hdev->pdev;
1906 
1907 	pci_iounmap(pdev, hdev->hw.io_base);
1908 	pci_clear_master(pdev);
1909 	pci_release_regions(pdev);
1910 	pci_disable_device(pdev);
1911 }
1912 
1913 static int hclgevf_query_vf_resource(struct hclgevf_dev *hdev)
1914 {
1915 	struct hclgevf_query_res_cmd *req;
1916 	struct hclgevf_desc desc;
1917 	int ret;
1918 
1919 	hclgevf_cmd_setup_basic_desc(&desc, HCLGEVF_OPC_QUERY_VF_RSRC, true);
1920 	ret = hclgevf_cmd_send(&hdev->hw, &desc, 1);
1921 	if (ret) {
1922 		dev_err(&hdev->pdev->dev,
1923 			"query vf resource failed, ret = %d.\n", ret);
1924 		return ret;
1925 	}
1926 
1927 	req = (struct hclgevf_query_res_cmd *)desc.data;
1928 
1929 	if (hnae3_get_bit(hdev->ae_dev->flag, HNAE3_DEV_SUPPORT_ROCE_B)) {
1930 		hdev->roce_base_msix_offset =
1931 		hnae3_get_field(__le16_to_cpu(req->msixcap_localid_ba_rocee),
1932 				HCLGEVF_MSIX_OFT_ROCEE_M,
1933 				HCLGEVF_MSIX_OFT_ROCEE_S);
1934 		hdev->num_roce_msix =
1935 		hnae3_get_field(__le16_to_cpu(req->vf_intr_vector_number),
1936 				HCLGEVF_VEC_NUM_M, HCLGEVF_VEC_NUM_S);
1937 
1938 		/* VF should have NIC vectors and Roce vectors, NIC vectors
1939 		 * are queued before Roce vectors. The offset is fixed to 64.
1940 		 */
1941 		hdev->num_msi = hdev->num_roce_msix +
1942 				hdev->roce_base_msix_offset;
1943 	} else {
1944 		hdev->num_msi =
1945 		hnae3_get_field(__le16_to_cpu(req->vf_intr_vector_number),
1946 				HCLGEVF_VEC_NUM_M, HCLGEVF_VEC_NUM_S);
1947 	}
1948 
1949 	return 0;
1950 }
1951 
1952 static int hclgevf_reset_hdev(struct hclgevf_dev *hdev)
1953 {
1954 	struct pci_dev *pdev = hdev->pdev;
1955 	int ret;
1956 
1957 	ret = hclgevf_cmd_init(hdev);
1958 	if (ret) {
1959 		dev_err(&pdev->dev, "cmd failed %d\n", ret);
1960 		return ret;
1961 	}
1962 
1963 	ret = hclgevf_rss_init_hw(hdev);
1964 	if (ret) {
1965 		dev_err(&hdev->pdev->dev,
1966 			"failed(%d) to initialize RSS\n", ret);
1967 		return ret;
1968 	}
1969 
1970 	ret = hclgevf_init_vlan_config(hdev);
1971 	if (ret) {
1972 		dev_err(&hdev->pdev->dev,
1973 			"failed(%d) to initialize VLAN config\n", ret);
1974 		return ret;
1975 	}
1976 
1977 	dev_info(&hdev->pdev->dev, "Reset done\n");
1978 
1979 	return 0;
1980 }
1981 
1982 static int hclgevf_init_hdev(struct hclgevf_dev *hdev)
1983 {
1984 	struct pci_dev *pdev = hdev->pdev;
1985 	int ret;
1986 
1987 	ret = hclgevf_pci_init(hdev);
1988 	if (ret) {
1989 		dev_err(&pdev->dev, "PCI initialization failed\n");
1990 		return ret;
1991 	}
1992 
1993 	ret = hclgevf_cmd_queue_init(hdev);
1994 	if (ret) {
1995 		dev_err(&pdev->dev, "Cmd queue init failed: %d\n", ret);
1996 		goto err_cmd_queue_init;
1997 	}
1998 
1999 	ret = hclgevf_cmd_init(hdev);
2000 	if (ret)
2001 		goto err_cmd_init;
2002 
2003 	/* Get vf resource */
2004 	ret = hclgevf_query_vf_resource(hdev);
2005 	if (ret) {
2006 		dev_err(&hdev->pdev->dev,
2007 			"Query vf status error, ret = %d.\n", ret);
2008 		goto err_cmd_init;
2009 	}
2010 
2011 	ret = hclgevf_init_msi(hdev);
2012 	if (ret) {
2013 		dev_err(&pdev->dev, "failed(%d) to init MSI/MSI-X\n", ret);
2014 		goto err_cmd_init;
2015 	}
2016 
2017 	hclgevf_state_init(hdev);
2018 	hdev->reset_level = HNAE3_VF_FUNC_RESET;
2019 
2020 	ret = hclgevf_misc_irq_init(hdev);
2021 	if (ret) {
2022 		dev_err(&pdev->dev, "failed(%d) to init Misc IRQ(vector0)\n",
2023 			ret);
2024 		goto err_misc_irq_init;
2025 	}
2026 
2027 	ret = hclgevf_configure(hdev);
2028 	if (ret) {
2029 		dev_err(&pdev->dev, "failed(%d) to fetch configuration\n", ret);
2030 		goto err_config;
2031 	}
2032 
2033 	ret = hclgevf_alloc_tqps(hdev);
2034 	if (ret) {
2035 		dev_err(&pdev->dev, "failed(%d) to allocate TQPs\n", ret);
2036 		goto err_config;
2037 	}
2038 
2039 	ret = hclgevf_set_handle_info(hdev);
2040 	if (ret) {
2041 		dev_err(&pdev->dev, "failed(%d) to set handle info\n", ret);
2042 		goto err_config;
2043 	}
2044 
2045 	/* Initialize RSS for this VF */
2046 	ret = hclgevf_rss_init_hw(hdev);
2047 	if (ret) {
2048 		dev_err(&hdev->pdev->dev,
2049 			"failed(%d) to initialize RSS\n", ret);
2050 		goto err_config;
2051 	}
2052 
2053 	ret = hclgevf_init_vlan_config(hdev);
2054 	if (ret) {
2055 		dev_err(&hdev->pdev->dev,
2056 			"failed(%d) to initialize VLAN config\n", ret);
2057 		goto err_config;
2058 	}
2059 
2060 	hdev->last_reset_time = jiffies;
2061 	pr_info("finished initializing %s driver\n", HCLGEVF_DRIVER_NAME);
2062 
2063 	return 0;
2064 
2065 err_config:
2066 	hclgevf_misc_irq_uninit(hdev);
2067 err_misc_irq_init:
2068 	hclgevf_state_uninit(hdev);
2069 	hclgevf_uninit_msi(hdev);
2070 err_cmd_init:
2071 	hclgevf_cmd_uninit(hdev);
2072 err_cmd_queue_init:
2073 	hclgevf_pci_uninit(hdev);
2074 	return ret;
2075 }
2076 
2077 static void hclgevf_uninit_hdev(struct hclgevf_dev *hdev)
2078 {
2079 	hclgevf_state_uninit(hdev);
2080 	hclgevf_misc_irq_uninit(hdev);
2081 	hclgevf_cmd_uninit(hdev);
2082 	hclgevf_uninit_msi(hdev);
2083 	hclgevf_pci_uninit(hdev);
2084 }
2085 
2086 static int hclgevf_init_ae_dev(struct hnae3_ae_dev *ae_dev)
2087 {
2088 	struct pci_dev *pdev = ae_dev->pdev;
2089 	int ret;
2090 
2091 	ret = hclgevf_alloc_hdev(ae_dev);
2092 	if (ret) {
2093 		dev_err(&pdev->dev, "hclge device allocation failed\n");
2094 		return ret;
2095 	}
2096 
2097 	ret = hclgevf_init_hdev(ae_dev->priv);
2098 	if (ret)
2099 		dev_err(&pdev->dev, "hclge device initialization failed\n");
2100 
2101 	return ret;
2102 }
2103 
2104 static void hclgevf_uninit_ae_dev(struct hnae3_ae_dev *ae_dev)
2105 {
2106 	struct hclgevf_dev *hdev = ae_dev->priv;
2107 
2108 	hclgevf_uninit_hdev(hdev);
2109 	ae_dev->priv = NULL;
2110 }
2111 
2112 static u32 hclgevf_get_max_channels(struct hclgevf_dev *hdev)
2113 {
2114 	struct hnae3_handle *nic = &hdev->nic;
2115 	struct hnae3_knic_private_info *kinfo = &nic->kinfo;
2116 
2117 	return min_t(u32, hdev->rss_size_max * kinfo->num_tc, hdev->num_tqps);
2118 }
2119 
2120 /**
2121  * hclgevf_get_channels - Get the current channels enabled and max supported.
2122  * @handle: hardware information for network interface
2123  * @ch: ethtool channels structure
2124  *
2125  * We don't support separate tx and rx queues as channels. The other count
2126  * represents how many queues are being used for control. max_combined counts
2127  * how many queue pairs we can support. They may not be mapped 1 to 1 with
2128  * q_vectors since we support a lot more queue pairs than q_vectors.
2129  **/
2130 static void hclgevf_get_channels(struct hnae3_handle *handle,
2131 				 struct ethtool_channels *ch)
2132 {
2133 	struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);
2134 
2135 	ch->max_combined = hclgevf_get_max_channels(hdev);
2136 	ch->other_count = 0;
2137 	ch->max_other = 0;
2138 	ch->combined_count = hdev->num_tqps;
2139 }
2140 
2141 static void hclgevf_get_tqps_and_rss_info(struct hnae3_handle *handle,
2142 					  u16 *alloc_tqps, u16 *max_rss_size)
2143 {
2144 	struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);
2145 
2146 	*alloc_tqps = hdev->num_tqps;
2147 	*max_rss_size = hdev->rss_size_max;
2148 }
2149 
2150 static int hclgevf_get_status(struct hnae3_handle *handle)
2151 {
2152 	struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);
2153 
2154 	return hdev->hw.mac.link;
2155 }
2156 
2157 static void hclgevf_get_ksettings_an_result(struct hnae3_handle *handle,
2158 					    u8 *auto_neg, u32 *speed,
2159 					    u8 *duplex)
2160 {
2161 	struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);
2162 
2163 	if (speed)
2164 		*speed = hdev->hw.mac.speed;
2165 	if (duplex)
2166 		*duplex = hdev->hw.mac.duplex;
2167 	if (auto_neg)
2168 		*auto_neg = AUTONEG_DISABLE;
2169 }
2170 
2171 void hclgevf_update_speed_duplex(struct hclgevf_dev *hdev, u32 speed,
2172 				 u8 duplex)
2173 {
2174 	hdev->hw.mac.speed = speed;
2175 	hdev->hw.mac.duplex = duplex;
2176 }
2177 
2178 static void hclgevf_get_media_type(struct hnae3_handle *handle,
2179 				  u8 *media_type)
2180 {
2181 	struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);
2182 	if (media_type)
2183 		*media_type = hdev->hw.mac.media_type;
2184 }
2185 
2186 static bool hclgevf_get_hw_reset_stat(struct hnae3_handle *handle)
2187 {
2188 	struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);
2189 
2190 	return !!hclgevf_read_dev(&hdev->hw, HCLGEVF_RST_ING);
2191 }
2192 
2193 static bool hclgevf_ae_dev_resetting(struct hnae3_handle *handle)
2194 {
2195 	struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);
2196 
2197 	return test_bit(HCLGEVF_STATE_RST_HANDLING, &hdev->state);
2198 }
2199 
2200 static unsigned long hclgevf_ae_dev_reset_cnt(struct hnae3_handle *handle)
2201 {
2202 	struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle);
2203 
2204 	return hdev->reset_count;
2205 }
2206 
2207 static const struct hnae3_ae_ops hclgevf_ops = {
2208 	.init_ae_dev = hclgevf_init_ae_dev,
2209 	.uninit_ae_dev = hclgevf_uninit_ae_dev,
2210 	.init_client_instance = hclgevf_init_client_instance,
2211 	.uninit_client_instance = hclgevf_uninit_client_instance,
2212 	.start = hclgevf_ae_start,
2213 	.stop = hclgevf_ae_stop,
2214 	.map_ring_to_vector = hclgevf_map_ring_to_vector,
2215 	.unmap_ring_from_vector = hclgevf_unmap_ring_from_vector,
2216 	.get_vector = hclgevf_get_vector,
2217 	.put_vector = hclgevf_put_vector,
2218 	.reset_queue = hclgevf_reset_tqp,
2219 	.set_promisc_mode = hclgevf_set_promisc_mode,
2220 	.get_mac_addr = hclgevf_get_mac_addr,
2221 	.set_mac_addr = hclgevf_set_mac_addr,
2222 	.add_uc_addr = hclgevf_add_uc_addr,
2223 	.rm_uc_addr = hclgevf_rm_uc_addr,
2224 	.add_mc_addr = hclgevf_add_mc_addr,
2225 	.rm_mc_addr = hclgevf_rm_mc_addr,
2226 	.get_stats = hclgevf_get_stats,
2227 	.update_stats = hclgevf_update_stats,
2228 	.get_strings = hclgevf_get_strings,
2229 	.get_sset_count = hclgevf_get_sset_count,
2230 	.get_rss_key_size = hclgevf_get_rss_key_size,
2231 	.get_rss_indir_size = hclgevf_get_rss_indir_size,
2232 	.get_rss = hclgevf_get_rss,
2233 	.set_rss = hclgevf_set_rss,
2234 	.get_rss_tuple = hclgevf_get_rss_tuple,
2235 	.set_rss_tuple = hclgevf_set_rss_tuple,
2236 	.get_tc_size = hclgevf_get_tc_size,
2237 	.get_fw_version = hclgevf_get_fw_version,
2238 	.set_vlan_filter = hclgevf_set_vlan_filter,
2239 	.enable_hw_strip_rxvtag = hclgevf_en_hw_strip_rxvtag,
2240 	.reset_event = hclgevf_reset_event,
2241 	.set_default_reset_request = hclgevf_set_def_reset_request,
2242 	.get_channels = hclgevf_get_channels,
2243 	.get_tqps_and_rss_info = hclgevf_get_tqps_and_rss_info,
2244 	.get_status = hclgevf_get_status,
2245 	.get_ksettings_an_result = hclgevf_get_ksettings_an_result,
2246 	.get_media_type = hclgevf_get_media_type,
2247 	.get_hw_reset_stat = hclgevf_get_hw_reset_stat,
2248 	.ae_dev_resetting = hclgevf_ae_dev_resetting,
2249 	.ae_dev_reset_cnt = hclgevf_ae_dev_reset_cnt,
2250 };
2251 
2252 static struct hnae3_ae_algo ae_algovf = {
2253 	.ops = &hclgevf_ops,
2254 	.pdev_id_table = ae_algovf_pci_tbl,
2255 };
2256 
2257 static int hclgevf_init(void)
2258 {
2259 	pr_info("%s is initializing\n", HCLGEVF_NAME);
2260 
2261 	hnae3_register_ae_algo(&ae_algovf);
2262 
2263 	return 0;
2264 }
2265 
2266 static void hclgevf_exit(void)
2267 {
2268 	hnae3_unregister_ae_algo(&ae_algovf);
2269 }
2270 module_init(hclgevf_init);
2271 module_exit(hclgevf_exit);
2272 
2273 MODULE_LICENSE("GPL");
2274 MODULE_AUTHOR("Huawei Tech. Co., Ltd.");
2275 MODULE_DESCRIPTION("HCLGEVF Driver");
2276 MODULE_VERSION(HCLGEVF_MOD_VERSION);
2277