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