1 // SPDX-License-Identifier: GPL-2.0+
2 // Copyright (c) 2016-2017 Hisilicon Limited.
3 
4 #include "hclge_main.h"
5 #include "hclge_mbx.h"
6 #include "hnae3.h"
7 
8 #define CREATE_TRACE_POINTS
9 #include "hclge_trace.h"
10 
11 static u16 hclge_errno_to_resp(int errno)
12 {
13 	return abs(errno);
14 }
15 
16 /* hclge_gen_resp_to_vf: used to generate a synchronous response to VF when PF
17  * receives a mailbox message from VF.
18  * @vport: pointer to struct hclge_vport
19  * @vf_to_pf_req: pointer to hclge_mbx_vf_to_pf_cmd of the original mailbox
20  *		  message
21  * @resp_status: indicate to VF whether its request success(0) or failed.
22  */
23 static int hclge_gen_resp_to_vf(struct hclge_vport *vport,
24 				struct hclge_mbx_vf_to_pf_cmd *vf_to_pf_req,
25 				struct hclge_respond_to_vf_msg *resp_msg)
26 {
27 	struct hclge_mbx_pf_to_vf_cmd *resp_pf_to_vf;
28 	struct hclge_dev *hdev = vport->back;
29 	enum hclge_cmd_status status;
30 	struct hclge_desc desc;
31 	u16 resp;
32 
33 	resp_pf_to_vf = (struct hclge_mbx_pf_to_vf_cmd *)desc.data;
34 
35 	if (resp_msg->len > HCLGE_MBX_MAX_RESP_DATA_SIZE) {
36 		dev_err(&hdev->pdev->dev,
37 			"PF fail to gen resp to VF len %u exceeds max len %u\n",
38 			resp_msg->len,
39 			HCLGE_MBX_MAX_RESP_DATA_SIZE);
40 		/* If resp_msg->len is too long, set the value to max length
41 		 * and return the msg to VF
42 		 */
43 		resp_msg->len = HCLGE_MBX_MAX_RESP_DATA_SIZE;
44 	}
45 
46 	hclge_cmd_setup_basic_desc(&desc, HCLGEVF_OPC_MBX_PF_TO_VF, false);
47 
48 	resp_pf_to_vf->dest_vfid = vf_to_pf_req->mbx_src_vfid;
49 	resp_pf_to_vf->msg_len = vf_to_pf_req->msg_len;
50 
51 	resp_pf_to_vf->msg.code = HCLGE_MBX_PF_VF_RESP;
52 	resp_pf_to_vf->msg.vf_mbx_msg_code = vf_to_pf_req->msg.code;
53 	resp_pf_to_vf->msg.vf_mbx_msg_subcode = vf_to_pf_req->msg.subcode;
54 	resp = hclge_errno_to_resp(resp_msg->status);
55 	if (resp < SHRT_MAX) {
56 		resp_pf_to_vf->msg.resp_status = resp;
57 	} else {
58 		dev_warn(&hdev->pdev->dev,
59 			 "failed to send response to VF, response status %d is out-of-bound\n",
60 			 resp);
61 		resp_pf_to_vf->msg.resp_status = EIO;
62 	}
63 
64 	if (resp_msg->len > 0)
65 		memcpy(resp_pf_to_vf->msg.resp_data, resp_msg->data,
66 		       resp_msg->len);
67 
68 	status = hclge_cmd_send(&hdev->hw, &desc, 1);
69 	if (status)
70 		dev_err(&hdev->pdev->dev,
71 			"failed to send response to VF, status: %d, vfid: %u, code: %u, subcode: %u.\n",
72 			status, vf_to_pf_req->mbx_src_vfid,
73 			vf_to_pf_req->msg.code, vf_to_pf_req->msg.subcode);
74 
75 	return status;
76 }
77 
78 static int hclge_send_mbx_msg(struct hclge_vport *vport, u8 *msg, u16 msg_len,
79 			      u16 mbx_opcode, u8 dest_vfid)
80 {
81 	struct hclge_mbx_pf_to_vf_cmd *resp_pf_to_vf;
82 	struct hclge_dev *hdev = vport->back;
83 	enum hclge_cmd_status status;
84 	struct hclge_desc desc;
85 
86 	resp_pf_to_vf = (struct hclge_mbx_pf_to_vf_cmd *)desc.data;
87 
88 	hclge_cmd_setup_basic_desc(&desc, HCLGEVF_OPC_MBX_PF_TO_VF, false);
89 
90 	resp_pf_to_vf->dest_vfid = dest_vfid;
91 	resp_pf_to_vf->msg_len = msg_len;
92 	resp_pf_to_vf->msg.code = mbx_opcode;
93 
94 	memcpy(&resp_pf_to_vf->msg.vf_mbx_msg_code, msg, msg_len);
95 
96 	trace_hclge_pf_mbx_send(hdev, resp_pf_to_vf);
97 
98 	status = hclge_cmd_send(&hdev->hw, &desc, 1);
99 	if (status)
100 		dev_err(&hdev->pdev->dev,
101 			"failed to send mailbox to VF, status: %d, vfid: %u, opcode: %u\n",
102 			status, dest_vfid, mbx_opcode);
103 
104 	return status;
105 }
106 
107 int hclge_inform_reset_assert_to_vf(struct hclge_vport *vport)
108 {
109 	struct hclge_dev *hdev = vport->back;
110 	u16 reset_type;
111 	u8 msg_data[2];
112 	u8 dest_vfid;
113 
114 	BUILD_BUG_ON(HNAE3_MAX_RESET > U16_MAX);
115 
116 	dest_vfid = (u8)vport->vport_id;
117 
118 	if (hdev->reset_type == HNAE3_FUNC_RESET)
119 		reset_type = HNAE3_VF_PF_FUNC_RESET;
120 	else if (hdev->reset_type == HNAE3_FLR_RESET)
121 		reset_type = HNAE3_VF_FULL_RESET;
122 	else
123 		reset_type = HNAE3_VF_FUNC_RESET;
124 
125 	memcpy(&msg_data[0], &reset_type, sizeof(u16));
126 
127 	/* send this requested info to VF */
128 	return hclge_send_mbx_msg(vport, msg_data, sizeof(msg_data),
129 				  HCLGE_MBX_ASSERTING_RESET, dest_vfid);
130 }
131 
132 static void hclge_free_vector_ring_chain(struct hnae3_ring_chain_node *head)
133 {
134 	struct hnae3_ring_chain_node *chain_tmp, *chain;
135 
136 	chain = head->next;
137 
138 	while (chain) {
139 		chain_tmp = chain->next;
140 		kfree_sensitive(chain);
141 		chain = chain_tmp;
142 	}
143 }
144 
145 /* hclge_get_ring_chain_from_mbx: get ring type & tqp id & int_gl idx
146  * from mailbox message
147  * msg[0]: opcode
148  * msg[1]: <not relevant to this function>
149  * msg[2]: ring_num
150  * msg[3]: first ring type (TX|RX)
151  * msg[4]: first tqp id
152  * msg[5]: first int_gl idx
153  * msg[6] ~ msg[14]: other ring type, tqp id and int_gl idx
154  */
155 static int hclge_get_ring_chain_from_mbx(
156 			struct hclge_mbx_vf_to_pf_cmd *req,
157 			struct hnae3_ring_chain_node *ring_chain,
158 			struct hclge_vport *vport)
159 {
160 	struct hnae3_ring_chain_node *cur_chain, *new_chain;
161 	int ring_num;
162 	int i = 0;
163 
164 	ring_num = req->msg.ring_num;
165 
166 	if (ring_num > HCLGE_MBX_MAX_RING_CHAIN_PARAM_NUM)
167 		return -ENOMEM;
168 
169 	hnae3_set_bit(ring_chain->flag, HNAE3_RING_TYPE_B,
170 		      req->msg.param[i].ring_type);
171 	ring_chain->tqp_index =
172 		hclge_get_queue_id(vport->nic.kinfo.tqp
173 				   [req->msg.param[i].tqp_index]);
174 	hnae3_set_field(ring_chain->int_gl_idx, HNAE3_RING_GL_IDX_M,
175 			HNAE3_RING_GL_IDX_S, req->msg.param[i].int_gl_index);
176 
177 	cur_chain = ring_chain;
178 
179 	for (i = 1; i < ring_num; i++) {
180 		new_chain = kzalloc(sizeof(*new_chain), GFP_KERNEL);
181 		if (!new_chain)
182 			goto err;
183 
184 		hnae3_set_bit(new_chain->flag, HNAE3_RING_TYPE_B,
185 			      req->msg.param[i].ring_type);
186 
187 		new_chain->tqp_index =
188 		hclge_get_queue_id(vport->nic.kinfo.tqp
189 			[req->msg.param[i].tqp_index]);
190 
191 		hnae3_set_field(new_chain->int_gl_idx, HNAE3_RING_GL_IDX_M,
192 				HNAE3_RING_GL_IDX_S,
193 				req->msg.param[i].int_gl_index);
194 
195 		cur_chain->next = new_chain;
196 		cur_chain = new_chain;
197 	}
198 
199 	return 0;
200 err:
201 	hclge_free_vector_ring_chain(ring_chain);
202 	return -ENOMEM;
203 }
204 
205 static int hclge_map_unmap_ring_to_vf_vector(struct hclge_vport *vport, bool en,
206 					     struct hclge_mbx_vf_to_pf_cmd *req)
207 {
208 	struct hnae3_ring_chain_node ring_chain;
209 	int vector_id = req->msg.vector_id;
210 	int ret;
211 
212 	memset(&ring_chain, 0, sizeof(ring_chain));
213 	ret = hclge_get_ring_chain_from_mbx(req, &ring_chain, vport);
214 	if (ret)
215 		return ret;
216 
217 	ret = hclge_bind_ring_with_vector(vport, vector_id, en, &ring_chain);
218 
219 	hclge_free_vector_ring_chain(&ring_chain);
220 
221 	return ret;
222 }
223 
224 static int hclge_set_vf_promisc_mode(struct hclge_vport *vport,
225 				     struct hclge_mbx_vf_to_pf_cmd *req)
226 {
227 	bool en_bc = req->msg.en_bc ? true : false;
228 	bool en_uc = req->msg.en_uc ? true : false;
229 	bool en_mc = req->msg.en_mc ? true : false;
230 	int ret;
231 
232 	if (!vport->vf_info.trusted) {
233 		en_uc = false;
234 		en_mc = false;
235 	}
236 
237 	ret = hclge_set_vport_promisc_mode(vport, en_uc, en_mc, en_bc);
238 
239 	vport->vf_info.promisc_enable = (en_uc || en_mc) ? 1 : 0;
240 
241 	return ret;
242 }
243 
244 void hclge_inform_vf_promisc_info(struct hclge_vport *vport)
245 {
246 	u8 dest_vfid = (u8)vport->vport_id;
247 	u8 msg_data[2];
248 
249 	memcpy(&msg_data[0], &vport->vf_info.promisc_enable, sizeof(u16));
250 
251 	hclge_send_mbx_msg(vport, msg_data, sizeof(msg_data),
252 			   HCLGE_MBX_PUSH_PROMISC_INFO, dest_vfid);
253 }
254 
255 static int hclge_set_vf_uc_mac_addr(struct hclge_vport *vport,
256 				    struct hclge_mbx_vf_to_pf_cmd *mbx_req)
257 {
258 #define HCLGE_MBX_VF_OLD_MAC_ADDR_OFFSET	6
259 
260 	const u8 *mac_addr = (const u8 *)(mbx_req->msg.data);
261 	struct hclge_dev *hdev = vport->back;
262 	int status;
263 
264 	if (mbx_req->msg.subcode == HCLGE_MBX_MAC_VLAN_UC_MODIFY) {
265 		const u8 *old_addr = (const u8 *)
266 		(&mbx_req->msg.data[HCLGE_MBX_VF_OLD_MAC_ADDR_OFFSET]);
267 
268 		/* If VF MAC has been configured by the host then it
269 		 * cannot be overridden by the MAC specified by the VM.
270 		 */
271 		if (!is_zero_ether_addr(vport->vf_info.mac) &&
272 		    !ether_addr_equal(mac_addr, vport->vf_info.mac))
273 			return -EPERM;
274 
275 		if (!is_valid_ether_addr(mac_addr))
276 			return -EINVAL;
277 
278 		spin_lock_bh(&vport->mac_list_lock);
279 		status = hclge_update_mac_node_for_dev_addr(vport, old_addr,
280 							    mac_addr);
281 		spin_unlock_bh(&vport->mac_list_lock);
282 		hclge_task_schedule(hdev, 0);
283 	} else if (mbx_req->msg.subcode == HCLGE_MBX_MAC_VLAN_UC_ADD) {
284 		status = hclge_update_mac_list(vport, HCLGE_MAC_TO_ADD,
285 					       HCLGE_MAC_ADDR_UC, mac_addr);
286 	} else if (mbx_req->msg.subcode == HCLGE_MBX_MAC_VLAN_UC_REMOVE) {
287 		status = hclge_update_mac_list(vport, HCLGE_MAC_TO_DEL,
288 					       HCLGE_MAC_ADDR_UC, mac_addr);
289 	} else {
290 		dev_err(&hdev->pdev->dev,
291 			"failed to set unicast mac addr, unknown subcode %u\n",
292 			mbx_req->msg.subcode);
293 		return -EIO;
294 	}
295 
296 	return status;
297 }
298 
299 static int hclge_set_vf_mc_mac_addr(struct hclge_vport *vport,
300 				    struct hclge_mbx_vf_to_pf_cmd *mbx_req)
301 {
302 	const u8 *mac_addr = (const u8 *)(mbx_req->msg.data);
303 	struct hclge_dev *hdev = vport->back;
304 
305 	if (mbx_req->msg.subcode == HCLGE_MBX_MAC_VLAN_MC_ADD) {
306 		hclge_update_mac_list(vport, HCLGE_MAC_TO_ADD,
307 				      HCLGE_MAC_ADDR_MC, mac_addr);
308 	} else if (mbx_req->msg.subcode == HCLGE_MBX_MAC_VLAN_MC_REMOVE) {
309 		hclge_update_mac_list(vport, HCLGE_MAC_TO_DEL,
310 				      HCLGE_MAC_ADDR_MC, mac_addr);
311 	} else {
312 		dev_err(&hdev->pdev->dev,
313 			"failed to set mcast mac addr, unknown subcode %u\n",
314 			mbx_req->msg.subcode);
315 		return -EIO;
316 	}
317 
318 	return 0;
319 }
320 
321 int hclge_push_vf_port_base_vlan_info(struct hclge_vport *vport, u8 vfid,
322 				      u16 state, u16 vlan_tag, u16 qos,
323 				      u16 vlan_proto)
324 {
325 #define MSG_DATA_SIZE	8
326 
327 	u8 msg_data[MSG_DATA_SIZE];
328 
329 	memcpy(&msg_data[0], &state, sizeof(u16));
330 	memcpy(&msg_data[2], &vlan_proto, sizeof(u16));
331 	memcpy(&msg_data[4], &qos, sizeof(u16));
332 	memcpy(&msg_data[6], &vlan_tag, sizeof(u16));
333 
334 	return hclge_send_mbx_msg(vport, msg_data, sizeof(msg_data),
335 				  HCLGE_MBX_PUSH_VLAN_INFO, vfid);
336 }
337 
338 static int hclge_set_vf_vlan_cfg(struct hclge_vport *vport,
339 				 struct hclge_mbx_vf_to_pf_cmd *mbx_req,
340 				 struct hclge_respond_to_vf_msg *resp_msg)
341 {
342 #define HCLGE_MBX_VLAN_STATE_OFFSET	0
343 #define HCLGE_MBX_VLAN_INFO_OFFSET	2
344 
345 	struct hclge_vf_vlan_cfg *msg_cmd;
346 	int status = 0;
347 
348 	msg_cmd = (struct hclge_vf_vlan_cfg *)&mbx_req->msg;
349 	if (msg_cmd->subcode == HCLGE_MBX_VLAN_FILTER) {
350 		struct hnae3_handle *handle = &vport->nic;
351 		u16 vlan, proto;
352 		bool is_kill;
353 
354 		is_kill = !!msg_cmd->is_kill;
355 		vlan =  msg_cmd->vlan;
356 		proto =  msg_cmd->proto;
357 		status = hclge_set_vlan_filter(handle, cpu_to_be16(proto),
358 					       vlan, is_kill);
359 	} else if (msg_cmd->subcode == HCLGE_MBX_VLAN_RX_OFF_CFG) {
360 		struct hnae3_handle *handle = &vport->nic;
361 		bool en = msg_cmd->is_kill ? true : false;
362 
363 		status = hclge_en_hw_strip_rxvtag(handle, en);
364 	} else if (msg_cmd->subcode == HCLGE_MBX_PORT_BASE_VLAN_CFG) {
365 		struct hclge_vlan_info *vlan_info;
366 		u16 *state;
367 
368 		state = (u16 *)&mbx_req->msg.data[HCLGE_MBX_VLAN_STATE_OFFSET];
369 		vlan_info = (struct hclge_vlan_info *)
370 			&mbx_req->msg.data[HCLGE_MBX_VLAN_INFO_OFFSET];
371 		status = hclge_update_port_base_vlan_cfg(vport, *state,
372 							 vlan_info);
373 	} else if (msg_cmd->subcode == HCLGE_MBX_GET_PORT_BASE_VLAN_STATE) {
374 		resp_msg->data[0] = vport->port_base_vlan_cfg.state;
375 		resp_msg->len = sizeof(u8);
376 	}
377 
378 	return status;
379 }
380 
381 static int hclge_set_vf_alive(struct hclge_vport *vport,
382 			      struct hclge_mbx_vf_to_pf_cmd *mbx_req)
383 {
384 	bool alive = !!mbx_req->msg.data[0];
385 	int ret = 0;
386 
387 	if (alive)
388 		ret = hclge_vport_start(vport);
389 	else
390 		hclge_vport_stop(vport);
391 
392 	return ret;
393 }
394 
395 static void hclge_get_vf_tcinfo(struct hclge_vport *vport,
396 				struct hclge_respond_to_vf_msg *resp_msg)
397 {
398 	struct hnae3_knic_private_info *kinfo = &vport->nic.kinfo;
399 	unsigned int i;
400 
401 	for (i = 0; i < kinfo->num_tc; i++)
402 		resp_msg->data[0] |= BIT(i);
403 
404 	resp_msg->len = sizeof(u8);
405 }
406 
407 static void hclge_get_vf_queue_info(struct hclge_vport *vport,
408 				    struct hclge_respond_to_vf_msg *resp_msg)
409 {
410 #define HCLGE_TQPS_RSS_INFO_LEN		6
411 #define HCLGE_TQPS_ALLOC_OFFSET		0
412 #define HCLGE_TQPS_RSS_SIZE_OFFSET	2
413 #define HCLGE_TQPS_RX_BUFFER_LEN_OFFSET	4
414 
415 	struct hclge_dev *hdev = vport->back;
416 
417 	/* get the queue related info */
418 	memcpy(&resp_msg->data[HCLGE_TQPS_ALLOC_OFFSET],
419 	       &vport->alloc_tqps, sizeof(u16));
420 	memcpy(&resp_msg->data[HCLGE_TQPS_RSS_SIZE_OFFSET],
421 	       &vport->nic.kinfo.rss_size, sizeof(u16));
422 	memcpy(&resp_msg->data[HCLGE_TQPS_RX_BUFFER_LEN_OFFSET],
423 	       &hdev->rx_buf_len, sizeof(u16));
424 	resp_msg->len = HCLGE_TQPS_RSS_INFO_LEN;
425 }
426 
427 static void hclge_get_vf_mac_addr(struct hclge_vport *vport,
428 				  struct hclge_respond_to_vf_msg *resp_msg)
429 {
430 	ether_addr_copy(resp_msg->data, vport->vf_info.mac);
431 	resp_msg->len = ETH_ALEN;
432 }
433 
434 static void hclge_get_vf_queue_depth(struct hclge_vport *vport,
435 				     struct hclge_respond_to_vf_msg *resp_msg)
436 {
437 #define HCLGE_TQPS_DEPTH_INFO_LEN	4
438 #define HCLGE_TQPS_NUM_TX_DESC_OFFSET	0
439 #define HCLGE_TQPS_NUM_RX_DESC_OFFSET	2
440 
441 	struct hclge_dev *hdev = vport->back;
442 
443 	/* get the queue depth info */
444 	memcpy(&resp_msg->data[HCLGE_TQPS_NUM_TX_DESC_OFFSET],
445 	       &hdev->num_tx_desc, sizeof(u16));
446 	memcpy(&resp_msg->data[HCLGE_TQPS_NUM_RX_DESC_OFFSET],
447 	       &hdev->num_rx_desc, sizeof(u16));
448 	resp_msg->len = HCLGE_TQPS_DEPTH_INFO_LEN;
449 }
450 
451 static void hclge_get_vf_media_type(struct hclge_vport *vport,
452 				    struct hclge_respond_to_vf_msg *resp_msg)
453 {
454 #define HCLGE_VF_MEDIA_TYPE_OFFSET	0
455 #define HCLGE_VF_MODULE_TYPE_OFFSET	1
456 #define HCLGE_VF_MEDIA_TYPE_LENGTH	2
457 
458 	struct hclge_dev *hdev = vport->back;
459 
460 	resp_msg->data[HCLGE_VF_MEDIA_TYPE_OFFSET] =
461 		hdev->hw.mac.media_type;
462 	resp_msg->data[HCLGE_VF_MODULE_TYPE_OFFSET] =
463 		hdev->hw.mac.module_type;
464 	resp_msg->len = HCLGE_VF_MEDIA_TYPE_LENGTH;
465 }
466 
467 static int hclge_get_link_info(struct hclge_vport *vport,
468 			       struct hclge_mbx_vf_to_pf_cmd *mbx_req)
469 {
470 #define HCLGE_VF_LINK_STATE_UP		1U
471 #define HCLGE_VF_LINK_STATE_DOWN	0U
472 
473 	struct hclge_dev *hdev = vport->back;
474 	u16 link_status;
475 	u8 msg_data[8];
476 	u8 dest_vfid;
477 	u16 duplex;
478 
479 	/* mac.link can only be 0 or 1 */
480 	switch (vport->vf_info.link_state) {
481 	case IFLA_VF_LINK_STATE_ENABLE:
482 		link_status = HCLGE_VF_LINK_STATE_UP;
483 		break;
484 	case IFLA_VF_LINK_STATE_DISABLE:
485 		link_status = HCLGE_VF_LINK_STATE_DOWN;
486 		break;
487 	case IFLA_VF_LINK_STATE_AUTO:
488 	default:
489 		link_status = (u16)hdev->hw.mac.link;
490 		break;
491 	}
492 
493 	duplex = hdev->hw.mac.duplex;
494 	memcpy(&msg_data[0], &link_status, sizeof(u16));
495 	memcpy(&msg_data[2], &hdev->hw.mac.speed, sizeof(u32));
496 	memcpy(&msg_data[6], &duplex, sizeof(u16));
497 	dest_vfid = mbx_req->mbx_src_vfid;
498 
499 	/* send this requested info to VF */
500 	return hclge_send_mbx_msg(vport, msg_data, sizeof(msg_data),
501 				  HCLGE_MBX_LINK_STAT_CHANGE, dest_vfid);
502 }
503 
504 static void hclge_get_link_mode(struct hclge_vport *vport,
505 				struct hclge_mbx_vf_to_pf_cmd *mbx_req)
506 {
507 #define HCLGE_SUPPORTED   1
508 	struct hclge_dev *hdev = vport->back;
509 	unsigned long advertising;
510 	unsigned long supported;
511 	unsigned long send_data;
512 	u8 msg_data[10];
513 	u8 dest_vfid;
514 
515 	advertising = hdev->hw.mac.advertising[0];
516 	supported = hdev->hw.mac.supported[0];
517 	dest_vfid = mbx_req->mbx_src_vfid;
518 	msg_data[0] = mbx_req->msg.data[0];
519 
520 	send_data = msg_data[0] == HCLGE_SUPPORTED ? supported : advertising;
521 
522 	memcpy(&msg_data[2], &send_data, sizeof(unsigned long));
523 	hclge_send_mbx_msg(vport, msg_data, sizeof(msg_data),
524 			   HCLGE_MBX_LINK_STAT_MODE, dest_vfid);
525 }
526 
527 static void hclge_mbx_reset_vf_queue(struct hclge_vport *vport,
528 				     struct hclge_mbx_vf_to_pf_cmd *mbx_req)
529 {
530 	u16 queue_id;
531 
532 	memcpy(&queue_id, mbx_req->msg.data, sizeof(queue_id));
533 
534 	hclge_reset_vf_queue(vport, queue_id);
535 }
536 
537 static int hclge_reset_vf(struct hclge_vport *vport)
538 {
539 	struct hclge_dev *hdev = vport->back;
540 
541 	dev_warn(&hdev->pdev->dev, "PF received VF reset request from VF %u!",
542 		 vport->vport_id);
543 
544 	return hclge_func_reset_cmd(hdev, vport->vport_id);
545 }
546 
547 static void hclge_vf_keep_alive(struct hclge_vport *vport)
548 {
549 	vport->last_active_jiffies = jiffies;
550 }
551 
552 static int hclge_set_vf_mtu(struct hclge_vport *vport,
553 			    struct hclge_mbx_vf_to_pf_cmd *mbx_req)
554 {
555 	u32 mtu;
556 
557 	memcpy(&mtu, mbx_req->msg.data, sizeof(mtu));
558 
559 	return hclge_set_vport_mtu(vport, mtu);
560 }
561 
562 static void hclge_get_queue_id_in_pf(struct hclge_vport *vport,
563 				     struct hclge_mbx_vf_to_pf_cmd *mbx_req,
564 				     struct hclge_respond_to_vf_msg *resp_msg)
565 {
566 	u16 queue_id, qid_in_pf;
567 
568 	memcpy(&queue_id, mbx_req->msg.data, sizeof(queue_id));
569 	qid_in_pf = hclge_covert_handle_qid_global(&vport->nic, queue_id);
570 	memcpy(resp_msg->data, &qid_in_pf, sizeof(qid_in_pf));
571 	resp_msg->len = sizeof(qid_in_pf);
572 }
573 
574 static void hclge_get_rss_key(struct hclge_vport *vport,
575 			      struct hclge_mbx_vf_to_pf_cmd *mbx_req,
576 			      struct hclge_respond_to_vf_msg *resp_msg)
577 {
578 #define HCLGE_RSS_MBX_RESP_LEN	8
579 	struct hclge_dev *hdev = vport->back;
580 	u8 index;
581 
582 	index = mbx_req->msg.data[0];
583 
584 	memcpy(resp_msg->data,
585 	       &hdev->vport[0].rss_hash_key[index * HCLGE_RSS_MBX_RESP_LEN],
586 	       HCLGE_RSS_MBX_RESP_LEN);
587 	resp_msg->len = HCLGE_RSS_MBX_RESP_LEN;
588 }
589 
590 static void hclge_link_fail_parse(struct hclge_dev *hdev, u8 link_fail_code)
591 {
592 	switch (link_fail_code) {
593 	case HCLGE_LF_REF_CLOCK_LOST:
594 		dev_warn(&hdev->pdev->dev, "Reference clock lost!\n");
595 		break;
596 	case HCLGE_LF_XSFP_TX_DISABLE:
597 		dev_warn(&hdev->pdev->dev, "SFP tx is disabled!\n");
598 		break;
599 	case HCLGE_LF_XSFP_ABSENT:
600 		dev_warn(&hdev->pdev->dev, "SFP is absent!\n");
601 		break;
602 	default:
603 		break;
604 	}
605 }
606 
607 static void hclge_handle_link_change_event(struct hclge_dev *hdev,
608 					   struct hclge_mbx_vf_to_pf_cmd *req)
609 {
610 	hclge_task_schedule(hdev, 0);
611 
612 	if (!req->msg.subcode)
613 		hclge_link_fail_parse(hdev, req->msg.data[0]);
614 }
615 
616 static bool hclge_cmd_crq_empty(struct hclge_hw *hw)
617 {
618 	u32 tail = hclge_read_dev(hw, HCLGE_NIC_CRQ_TAIL_REG);
619 
620 	return tail == hw->cmq.crq.next_to_use;
621 }
622 
623 static void hclge_handle_ncsi_error(struct hclge_dev *hdev)
624 {
625 	struct hnae3_ae_dev *ae_dev = hdev->ae_dev;
626 
627 	ae_dev->ops->set_default_reset_request(ae_dev, HNAE3_GLOBAL_RESET);
628 	dev_warn(&hdev->pdev->dev, "requesting reset due to NCSI error\n");
629 	ae_dev->ops->reset_event(hdev->pdev, NULL);
630 }
631 
632 static void hclge_handle_vf_tbl(struct hclge_vport *vport,
633 				struct hclge_mbx_vf_to_pf_cmd *mbx_req)
634 {
635 	struct hclge_dev *hdev = vport->back;
636 	struct hclge_vf_vlan_cfg *msg_cmd;
637 
638 	msg_cmd = (struct hclge_vf_vlan_cfg *)&mbx_req->msg;
639 	if (msg_cmd->subcode == HCLGE_MBX_VPORT_LIST_CLEAR) {
640 		hclge_rm_vport_all_mac_table(vport, true, HCLGE_MAC_ADDR_UC);
641 		hclge_rm_vport_all_mac_table(vport, true, HCLGE_MAC_ADDR_MC);
642 		hclge_rm_vport_all_vlan_table(vport, true);
643 	} else {
644 		dev_warn(&hdev->pdev->dev, "Invalid cmd(%u)\n",
645 			 msg_cmd->subcode);
646 	}
647 }
648 
649 void hclge_mbx_handler(struct hclge_dev *hdev)
650 {
651 	struct hclge_cmq_ring *crq = &hdev->hw.cmq.crq;
652 	struct hclge_respond_to_vf_msg resp_msg;
653 	struct hclge_mbx_vf_to_pf_cmd *req;
654 	struct hclge_vport *vport;
655 	struct hclge_desc *desc;
656 	bool is_del = false;
657 	unsigned int flag;
658 	int ret = 0;
659 
660 	memset(&resp_msg, 0, sizeof(resp_msg));
661 	/* handle all the mailbox requests in the queue */
662 	while (!hclge_cmd_crq_empty(&hdev->hw)) {
663 		if (test_bit(HCLGE_STATE_CMD_DISABLE, &hdev->state)) {
664 			dev_warn(&hdev->pdev->dev,
665 				 "command queue needs re-initializing\n");
666 			return;
667 		}
668 
669 		desc = &crq->desc[crq->next_to_use];
670 		req = (struct hclge_mbx_vf_to_pf_cmd *)desc->data;
671 
672 		flag = le16_to_cpu(crq->desc[crq->next_to_use].flag);
673 		if (unlikely(!hnae3_get_bit(flag, HCLGE_CMDQ_RX_OUTVLD_B))) {
674 			dev_warn(&hdev->pdev->dev,
675 				 "dropped invalid mailbox message, code = %u\n",
676 				 req->msg.code);
677 
678 			/* dropping/not processing this invalid message */
679 			crq->desc[crq->next_to_use].flag = 0;
680 			hclge_mbx_ring_ptr_move_crq(crq);
681 			continue;
682 		}
683 
684 		vport = &hdev->vport[req->mbx_src_vfid];
685 
686 		trace_hclge_pf_mbx_get(hdev, req);
687 
688 		switch (req->msg.code) {
689 		case HCLGE_MBX_MAP_RING_TO_VECTOR:
690 			ret = hclge_map_unmap_ring_to_vf_vector(vport, true,
691 								req);
692 			break;
693 		case HCLGE_MBX_UNMAP_RING_TO_VECTOR:
694 			ret = hclge_map_unmap_ring_to_vf_vector(vport, false,
695 								req);
696 			break;
697 		case HCLGE_MBX_SET_PROMISC_MODE:
698 			ret = hclge_set_vf_promisc_mode(vport, req);
699 			if (ret)
700 				dev_err(&hdev->pdev->dev,
701 					"PF fail(%d) to set VF promisc mode\n",
702 					ret);
703 			break;
704 		case HCLGE_MBX_SET_UNICAST:
705 			ret = hclge_set_vf_uc_mac_addr(vport, req);
706 			if (ret)
707 				dev_err(&hdev->pdev->dev,
708 					"PF fail(%d) to set VF UC MAC Addr\n",
709 					ret);
710 			break;
711 		case HCLGE_MBX_SET_MULTICAST:
712 			ret = hclge_set_vf_mc_mac_addr(vport, req);
713 			if (ret)
714 				dev_err(&hdev->pdev->dev,
715 					"PF fail(%d) to set VF MC MAC Addr\n",
716 					ret);
717 			break;
718 		case HCLGE_MBX_SET_VLAN:
719 			ret = hclge_set_vf_vlan_cfg(vport, req, &resp_msg);
720 			if (ret)
721 				dev_err(&hdev->pdev->dev,
722 					"PF failed(%d) to config VF's VLAN\n",
723 					ret);
724 			break;
725 		case HCLGE_MBX_SET_ALIVE:
726 			ret = hclge_set_vf_alive(vport, req);
727 			if (ret)
728 				dev_err(&hdev->pdev->dev,
729 					"PF failed(%d) to set VF's ALIVE\n",
730 					ret);
731 			break;
732 		case HCLGE_MBX_GET_QINFO:
733 			hclge_get_vf_queue_info(vport, &resp_msg);
734 			break;
735 		case HCLGE_MBX_GET_QDEPTH:
736 			hclge_get_vf_queue_depth(vport, &resp_msg);
737 			break;
738 		case HCLGE_MBX_GET_TCINFO:
739 			hclge_get_vf_tcinfo(vport, &resp_msg);
740 			break;
741 		case HCLGE_MBX_GET_LINK_STATUS:
742 			ret = hclge_get_link_info(vport, req);
743 			if (ret)
744 				dev_err(&hdev->pdev->dev,
745 					"failed to inform link stat to VF, ret = %d\n",
746 					ret);
747 			break;
748 		case HCLGE_MBX_QUEUE_RESET:
749 			hclge_mbx_reset_vf_queue(vport, req);
750 			break;
751 		case HCLGE_MBX_RESET:
752 			ret = hclge_reset_vf(vport);
753 			break;
754 		case HCLGE_MBX_KEEP_ALIVE:
755 			hclge_vf_keep_alive(vport);
756 			break;
757 		case HCLGE_MBX_SET_MTU:
758 			ret = hclge_set_vf_mtu(vport, req);
759 			if (ret)
760 				dev_err(&hdev->pdev->dev,
761 					"VF fail(%d) to set mtu\n", ret);
762 			break;
763 		case HCLGE_MBX_GET_QID_IN_PF:
764 			hclge_get_queue_id_in_pf(vport, req, &resp_msg);
765 			break;
766 		case HCLGE_MBX_GET_RSS_KEY:
767 			hclge_get_rss_key(vport, req, &resp_msg);
768 			break;
769 		case HCLGE_MBX_GET_LINK_MODE:
770 			hclge_get_link_mode(vport, req);
771 			break;
772 		case HCLGE_MBX_GET_VF_FLR_STATUS:
773 		case HCLGE_MBX_VF_UNINIT:
774 			is_del = req->msg.code == HCLGE_MBX_VF_UNINIT;
775 			hclge_rm_vport_all_mac_table(vport, is_del,
776 						     HCLGE_MAC_ADDR_UC);
777 			hclge_rm_vport_all_mac_table(vport, is_del,
778 						     HCLGE_MAC_ADDR_MC);
779 			hclge_rm_vport_all_vlan_table(vport, is_del);
780 			break;
781 		case HCLGE_MBX_GET_MEDIA_TYPE:
782 			hclge_get_vf_media_type(vport, &resp_msg);
783 			break;
784 		case HCLGE_MBX_PUSH_LINK_STATUS:
785 			hclge_handle_link_change_event(hdev, req);
786 			break;
787 		case HCLGE_MBX_GET_MAC_ADDR:
788 			hclge_get_vf_mac_addr(vport, &resp_msg);
789 			break;
790 		case HCLGE_MBX_NCSI_ERROR:
791 			hclge_handle_ncsi_error(hdev);
792 			break;
793 		case HCLGE_MBX_HANDLE_VF_TBL:
794 			hclge_handle_vf_tbl(vport, req);
795 			break;
796 		default:
797 			dev_err(&hdev->pdev->dev,
798 				"un-supported mailbox message, code = %u\n",
799 				req->msg.code);
800 			break;
801 		}
802 
803 		/* PF driver should not reply IMP */
804 		if (hnae3_get_bit(req->mbx_need_resp, HCLGE_MBX_NEED_RESP_B) &&
805 		    req->msg.code < HCLGE_MBX_GET_VF_FLR_STATUS) {
806 			resp_msg.status = ret;
807 			hclge_gen_resp_to_vf(vport, req, &resp_msg);
808 		}
809 
810 		crq->desc[crq->next_to_use].flag = 0;
811 		hclge_mbx_ring_ptr_move_crq(crq);
812 
813 		/* reinitialize ret after complete the mbx message processing */
814 		ret = 0;
815 	}
816 
817 	/* Write back CMDQ_RQ header pointer, M7 need this pointer */
818 	hclge_write_dev(&hdev->hw, HCLGE_NIC_CRQ_HEAD_REG, crq->next_to_use);
819 }
820