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 %u 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 	struct hclge_dev *hdev = vport->back;
162 	int ring_num;
163 	int i;
164 
165 	ring_num = req->msg.ring_num;
166 
167 	if (ring_num > HCLGE_MBX_MAX_RING_CHAIN_PARAM_NUM)
168 		return -ENOMEM;
169 
170 	for (i = 0; i < ring_num; i++) {
171 		if (req->msg.param[i].tqp_index >= vport->nic.kinfo.rss_size) {
172 			dev_err(&hdev->pdev->dev, "tqp index(%u) is out of range(0-%u)\n",
173 				req->msg.param[i].tqp_index,
174 				vport->nic.kinfo.rss_size - 1);
175 			return -EINVAL;
176 		}
177 	}
178 
179 	hnae3_set_bit(ring_chain->flag, HNAE3_RING_TYPE_B,
180 		      req->msg.param[0].ring_type);
181 	ring_chain->tqp_index =
182 		hclge_get_queue_id(vport->nic.kinfo.tqp
183 				   [req->msg.param[0].tqp_index]);
184 	hnae3_set_field(ring_chain->int_gl_idx, HNAE3_RING_GL_IDX_M,
185 			HNAE3_RING_GL_IDX_S, req->msg.param[0].int_gl_index);
186 
187 	cur_chain = ring_chain;
188 
189 	for (i = 1; i < ring_num; i++) {
190 		new_chain = kzalloc(sizeof(*new_chain), GFP_KERNEL);
191 		if (!new_chain)
192 			goto err;
193 
194 		hnae3_set_bit(new_chain->flag, HNAE3_RING_TYPE_B,
195 			      req->msg.param[i].ring_type);
196 
197 		new_chain->tqp_index =
198 		hclge_get_queue_id(vport->nic.kinfo.tqp
199 			[req->msg.param[i].tqp_index]);
200 
201 		hnae3_set_field(new_chain->int_gl_idx, HNAE3_RING_GL_IDX_M,
202 				HNAE3_RING_GL_IDX_S,
203 				req->msg.param[i].int_gl_index);
204 
205 		cur_chain->next = new_chain;
206 		cur_chain = new_chain;
207 	}
208 
209 	return 0;
210 err:
211 	hclge_free_vector_ring_chain(ring_chain);
212 	return -ENOMEM;
213 }
214 
215 static int hclge_map_unmap_ring_to_vf_vector(struct hclge_vport *vport, bool en,
216 					     struct hclge_mbx_vf_to_pf_cmd *req)
217 {
218 	struct hnae3_ring_chain_node ring_chain;
219 	int vector_id = req->msg.vector_id;
220 	int ret;
221 
222 	memset(&ring_chain, 0, sizeof(ring_chain));
223 	ret = hclge_get_ring_chain_from_mbx(req, &ring_chain, vport);
224 	if (ret)
225 		return ret;
226 
227 	ret = hclge_bind_ring_with_vector(vport, vector_id, en, &ring_chain);
228 
229 	hclge_free_vector_ring_chain(&ring_chain);
230 
231 	return ret;
232 }
233 
234 static int hclge_set_vf_promisc_mode(struct hclge_vport *vport,
235 				     struct hclge_mbx_vf_to_pf_cmd *req)
236 {
237 	bool en_bc = req->msg.en_bc ? true : false;
238 	bool en_uc = req->msg.en_uc ? true : false;
239 	bool en_mc = req->msg.en_mc ? true : false;
240 	struct hnae3_handle *handle = &vport->nic;
241 	int ret;
242 
243 	if (!vport->vf_info.trusted) {
244 		en_uc = false;
245 		en_mc = false;
246 	}
247 
248 	if (req->msg.en_limit_promisc)
249 		set_bit(HNAE3_PFLAG_LIMIT_PROMISC, &handle->priv_flags);
250 	else
251 		clear_bit(HNAE3_PFLAG_LIMIT_PROMISC,
252 			  &handle->priv_flags);
253 
254 	ret = hclge_set_vport_promisc_mode(vport, en_uc, en_mc, en_bc);
255 
256 	vport->vf_info.promisc_enable = (en_uc || en_mc) ? 1 : 0;
257 
258 	return ret;
259 }
260 
261 void hclge_inform_vf_promisc_info(struct hclge_vport *vport)
262 {
263 	u8 dest_vfid = (u8)vport->vport_id;
264 	u8 msg_data[2];
265 
266 	memcpy(&msg_data[0], &vport->vf_info.promisc_enable, sizeof(u16));
267 
268 	hclge_send_mbx_msg(vport, msg_data, sizeof(msg_data),
269 			   HCLGE_MBX_PUSH_PROMISC_INFO, dest_vfid);
270 }
271 
272 static int hclge_set_vf_uc_mac_addr(struct hclge_vport *vport,
273 				    struct hclge_mbx_vf_to_pf_cmd *mbx_req)
274 {
275 #define HCLGE_MBX_VF_OLD_MAC_ADDR_OFFSET	6
276 
277 	const u8 *mac_addr = (const u8 *)(mbx_req->msg.data);
278 	struct hclge_dev *hdev = vport->back;
279 	int status;
280 
281 	if (mbx_req->msg.subcode == HCLGE_MBX_MAC_VLAN_UC_MODIFY) {
282 		const u8 *old_addr = (const u8 *)
283 		(&mbx_req->msg.data[HCLGE_MBX_VF_OLD_MAC_ADDR_OFFSET]);
284 
285 		/* If VF MAC has been configured by the host then it
286 		 * cannot be overridden by the MAC specified by the VM.
287 		 */
288 		if (!is_zero_ether_addr(vport->vf_info.mac) &&
289 		    !ether_addr_equal(mac_addr, vport->vf_info.mac))
290 			return -EPERM;
291 
292 		if (!is_valid_ether_addr(mac_addr))
293 			return -EINVAL;
294 
295 		spin_lock_bh(&vport->mac_list_lock);
296 		status = hclge_update_mac_node_for_dev_addr(vport, old_addr,
297 							    mac_addr);
298 		spin_unlock_bh(&vport->mac_list_lock);
299 		hclge_task_schedule(hdev, 0);
300 	} else if (mbx_req->msg.subcode == HCLGE_MBX_MAC_VLAN_UC_ADD) {
301 		status = hclge_update_mac_list(vport, HCLGE_MAC_TO_ADD,
302 					       HCLGE_MAC_ADDR_UC, mac_addr);
303 	} else if (mbx_req->msg.subcode == HCLGE_MBX_MAC_VLAN_UC_REMOVE) {
304 		status = hclge_update_mac_list(vport, HCLGE_MAC_TO_DEL,
305 					       HCLGE_MAC_ADDR_UC, mac_addr);
306 	} else {
307 		dev_err(&hdev->pdev->dev,
308 			"failed to set unicast mac addr, unknown subcode %u\n",
309 			mbx_req->msg.subcode);
310 		return -EIO;
311 	}
312 
313 	return status;
314 }
315 
316 static int hclge_set_vf_mc_mac_addr(struct hclge_vport *vport,
317 				    struct hclge_mbx_vf_to_pf_cmd *mbx_req)
318 {
319 	const u8 *mac_addr = (const u8 *)(mbx_req->msg.data);
320 	struct hclge_dev *hdev = vport->back;
321 
322 	if (mbx_req->msg.subcode == HCLGE_MBX_MAC_VLAN_MC_ADD) {
323 		hclge_update_mac_list(vport, HCLGE_MAC_TO_ADD,
324 				      HCLGE_MAC_ADDR_MC, mac_addr);
325 	} else if (mbx_req->msg.subcode == HCLGE_MBX_MAC_VLAN_MC_REMOVE) {
326 		hclge_update_mac_list(vport, HCLGE_MAC_TO_DEL,
327 				      HCLGE_MAC_ADDR_MC, mac_addr);
328 	} else {
329 		dev_err(&hdev->pdev->dev,
330 			"failed to set mcast mac addr, unknown subcode %u\n",
331 			mbx_req->msg.subcode);
332 		return -EIO;
333 	}
334 
335 	return 0;
336 }
337 
338 int hclge_push_vf_port_base_vlan_info(struct hclge_vport *vport, u8 vfid,
339 				      u16 state, u16 vlan_tag, u16 qos,
340 				      u16 vlan_proto)
341 {
342 #define MSG_DATA_SIZE	8
343 
344 	u8 msg_data[MSG_DATA_SIZE];
345 
346 	memcpy(&msg_data[0], &state, sizeof(u16));
347 	memcpy(&msg_data[2], &vlan_proto, sizeof(u16));
348 	memcpy(&msg_data[4], &qos, sizeof(u16));
349 	memcpy(&msg_data[6], &vlan_tag, sizeof(u16));
350 
351 	return hclge_send_mbx_msg(vport, msg_data, sizeof(msg_data),
352 				  HCLGE_MBX_PUSH_VLAN_INFO, vfid);
353 }
354 
355 static int hclge_set_vf_vlan_cfg(struct hclge_vport *vport,
356 				 struct hclge_mbx_vf_to_pf_cmd *mbx_req,
357 				 struct hclge_respond_to_vf_msg *resp_msg)
358 {
359 #define HCLGE_MBX_VLAN_STATE_OFFSET	0
360 #define HCLGE_MBX_VLAN_INFO_OFFSET	2
361 
362 	struct hclge_vf_vlan_cfg *msg_cmd;
363 	int status = 0;
364 
365 	msg_cmd = (struct hclge_vf_vlan_cfg *)&mbx_req->msg;
366 	if (msg_cmd->subcode == HCLGE_MBX_VLAN_FILTER) {
367 		struct hnae3_handle *handle = &vport->nic;
368 		u16 vlan, proto;
369 		bool is_kill;
370 
371 		is_kill = !!msg_cmd->is_kill;
372 		vlan =  msg_cmd->vlan;
373 		proto =  msg_cmd->proto;
374 		status = hclge_set_vlan_filter(handle, cpu_to_be16(proto),
375 					       vlan, is_kill);
376 	} else if (msg_cmd->subcode == HCLGE_MBX_VLAN_RX_OFF_CFG) {
377 		struct hnae3_handle *handle = &vport->nic;
378 		bool en = msg_cmd->is_kill ? true : false;
379 
380 		status = hclge_en_hw_strip_rxvtag(handle, en);
381 	} else if (msg_cmd->subcode == HCLGE_MBX_PORT_BASE_VLAN_CFG) {
382 		struct hclge_vlan_info *vlan_info;
383 		u16 *state;
384 
385 		state = (u16 *)&mbx_req->msg.data[HCLGE_MBX_VLAN_STATE_OFFSET];
386 		vlan_info = (struct hclge_vlan_info *)
387 			&mbx_req->msg.data[HCLGE_MBX_VLAN_INFO_OFFSET];
388 		status = hclge_update_port_base_vlan_cfg(vport, *state,
389 							 vlan_info);
390 	} else if (msg_cmd->subcode == HCLGE_MBX_GET_PORT_BASE_VLAN_STATE) {
391 		struct hnae3_ae_dev *ae_dev = pci_get_drvdata(vport->nic.pdev);
392 		/* vf does not need to know about the port based VLAN state
393 		 * on device HNAE3_DEVICE_VERSION_V3. So always return disable
394 		 * on device HNAE3_DEVICE_VERSION_V3 if vf queries the port
395 		 * based VLAN state.
396 		 */
397 		resp_msg->data[0] =
398 			ae_dev->dev_version >= HNAE3_DEVICE_VERSION_V3 ?
399 			HNAE3_PORT_BASE_VLAN_DISABLE :
400 			vport->port_base_vlan_cfg.state;
401 		resp_msg->len = sizeof(u8);
402 	}
403 
404 	return status;
405 }
406 
407 static int hclge_set_vf_alive(struct hclge_vport *vport,
408 			      struct hclge_mbx_vf_to_pf_cmd *mbx_req)
409 {
410 	bool alive = !!mbx_req->msg.data[0];
411 	int ret = 0;
412 
413 	if (alive)
414 		ret = hclge_vport_start(vport);
415 	else
416 		hclge_vport_stop(vport);
417 
418 	return ret;
419 }
420 
421 static void hclge_get_vf_tcinfo(struct hclge_vport *vport,
422 				struct hclge_respond_to_vf_msg *resp_msg)
423 {
424 	struct hnae3_knic_private_info *kinfo = &vport->nic.kinfo;
425 	unsigned int i;
426 
427 	for (i = 0; i < kinfo->tc_info.num_tc; i++)
428 		resp_msg->data[0] |= BIT(i);
429 
430 	resp_msg->len = sizeof(u8);
431 }
432 
433 static void hclge_get_vf_queue_info(struct hclge_vport *vport,
434 				    struct hclge_respond_to_vf_msg *resp_msg)
435 {
436 #define HCLGE_TQPS_RSS_INFO_LEN		6
437 #define HCLGE_TQPS_ALLOC_OFFSET		0
438 #define HCLGE_TQPS_RSS_SIZE_OFFSET	2
439 #define HCLGE_TQPS_RX_BUFFER_LEN_OFFSET	4
440 
441 	struct hclge_dev *hdev = vport->back;
442 
443 	/* get the queue related info */
444 	memcpy(&resp_msg->data[HCLGE_TQPS_ALLOC_OFFSET],
445 	       &vport->alloc_tqps, sizeof(u16));
446 	memcpy(&resp_msg->data[HCLGE_TQPS_RSS_SIZE_OFFSET],
447 	       &vport->nic.kinfo.rss_size, sizeof(u16));
448 	memcpy(&resp_msg->data[HCLGE_TQPS_RX_BUFFER_LEN_OFFSET],
449 	       &hdev->rx_buf_len, sizeof(u16));
450 	resp_msg->len = HCLGE_TQPS_RSS_INFO_LEN;
451 }
452 
453 static void hclge_get_vf_mac_addr(struct hclge_vport *vport,
454 				  struct hclge_respond_to_vf_msg *resp_msg)
455 {
456 	ether_addr_copy(resp_msg->data, vport->vf_info.mac);
457 	resp_msg->len = ETH_ALEN;
458 }
459 
460 static void hclge_get_vf_queue_depth(struct hclge_vport *vport,
461 				     struct hclge_respond_to_vf_msg *resp_msg)
462 {
463 #define HCLGE_TQPS_DEPTH_INFO_LEN	4
464 #define HCLGE_TQPS_NUM_TX_DESC_OFFSET	0
465 #define HCLGE_TQPS_NUM_RX_DESC_OFFSET	2
466 
467 	struct hclge_dev *hdev = vport->back;
468 
469 	/* get the queue depth info */
470 	memcpy(&resp_msg->data[HCLGE_TQPS_NUM_TX_DESC_OFFSET],
471 	       &hdev->num_tx_desc, sizeof(u16));
472 	memcpy(&resp_msg->data[HCLGE_TQPS_NUM_RX_DESC_OFFSET],
473 	       &hdev->num_rx_desc, sizeof(u16));
474 	resp_msg->len = HCLGE_TQPS_DEPTH_INFO_LEN;
475 }
476 
477 static void hclge_get_vf_media_type(struct hclge_vport *vport,
478 				    struct hclge_respond_to_vf_msg *resp_msg)
479 {
480 #define HCLGE_VF_MEDIA_TYPE_OFFSET	0
481 #define HCLGE_VF_MODULE_TYPE_OFFSET	1
482 #define HCLGE_VF_MEDIA_TYPE_LENGTH	2
483 
484 	struct hclge_dev *hdev = vport->back;
485 
486 	resp_msg->data[HCLGE_VF_MEDIA_TYPE_OFFSET] =
487 		hdev->hw.mac.media_type;
488 	resp_msg->data[HCLGE_VF_MODULE_TYPE_OFFSET] =
489 		hdev->hw.mac.module_type;
490 	resp_msg->len = HCLGE_VF_MEDIA_TYPE_LENGTH;
491 }
492 
493 static int hclge_get_link_info(struct hclge_vport *vport,
494 			       struct hclge_mbx_vf_to_pf_cmd *mbx_req)
495 {
496 #define HCLGE_VF_LINK_STATE_UP		1U
497 #define HCLGE_VF_LINK_STATE_DOWN	0U
498 
499 	struct hclge_dev *hdev = vport->back;
500 	u16 link_status;
501 	u8 msg_data[8];
502 	u8 dest_vfid;
503 	u16 duplex;
504 
505 	/* mac.link can only be 0 or 1 */
506 	switch (vport->vf_info.link_state) {
507 	case IFLA_VF_LINK_STATE_ENABLE:
508 		link_status = HCLGE_VF_LINK_STATE_UP;
509 		break;
510 	case IFLA_VF_LINK_STATE_DISABLE:
511 		link_status = HCLGE_VF_LINK_STATE_DOWN;
512 		break;
513 	case IFLA_VF_LINK_STATE_AUTO:
514 	default:
515 		link_status = (u16)hdev->hw.mac.link;
516 		break;
517 	}
518 
519 	duplex = hdev->hw.mac.duplex;
520 	memcpy(&msg_data[0], &link_status, sizeof(u16));
521 	memcpy(&msg_data[2], &hdev->hw.mac.speed, sizeof(u32));
522 	memcpy(&msg_data[6], &duplex, sizeof(u16));
523 	dest_vfid = mbx_req->mbx_src_vfid;
524 
525 	/* send this requested info to VF */
526 	return hclge_send_mbx_msg(vport, msg_data, sizeof(msg_data),
527 				  HCLGE_MBX_LINK_STAT_CHANGE, dest_vfid);
528 }
529 
530 static void hclge_get_link_mode(struct hclge_vport *vport,
531 				struct hclge_mbx_vf_to_pf_cmd *mbx_req)
532 {
533 #define HCLGE_SUPPORTED   1
534 	struct hclge_dev *hdev = vport->back;
535 	unsigned long advertising;
536 	unsigned long supported;
537 	unsigned long send_data;
538 	u8 msg_data[10];
539 	u8 dest_vfid;
540 
541 	advertising = hdev->hw.mac.advertising[0];
542 	supported = hdev->hw.mac.supported[0];
543 	dest_vfid = mbx_req->mbx_src_vfid;
544 	msg_data[0] = mbx_req->msg.data[0];
545 
546 	send_data = msg_data[0] == HCLGE_SUPPORTED ? supported : advertising;
547 
548 	memcpy(&msg_data[2], &send_data, sizeof(unsigned long));
549 	hclge_send_mbx_msg(vport, msg_data, sizeof(msg_data),
550 			   HCLGE_MBX_LINK_STAT_MODE, dest_vfid);
551 }
552 
553 static void hclge_mbx_reset_vf_queue(struct hclge_vport *vport,
554 				     struct hclge_mbx_vf_to_pf_cmd *mbx_req)
555 {
556 	u16 queue_id;
557 
558 	memcpy(&queue_id, mbx_req->msg.data, sizeof(queue_id));
559 
560 	hclge_reset_vf_queue(vport, queue_id);
561 }
562 
563 static int hclge_reset_vf(struct hclge_vport *vport)
564 {
565 	struct hclge_dev *hdev = vport->back;
566 
567 	dev_warn(&hdev->pdev->dev, "PF received VF reset request from VF %u!",
568 		 vport->vport_id);
569 
570 	return hclge_func_reset_cmd(hdev, vport->vport_id);
571 }
572 
573 static void hclge_vf_keep_alive(struct hclge_vport *vport)
574 {
575 	vport->last_active_jiffies = jiffies;
576 }
577 
578 static int hclge_set_vf_mtu(struct hclge_vport *vport,
579 			    struct hclge_mbx_vf_to_pf_cmd *mbx_req)
580 {
581 	u32 mtu;
582 
583 	memcpy(&mtu, mbx_req->msg.data, sizeof(mtu));
584 
585 	return hclge_set_vport_mtu(vport, mtu);
586 }
587 
588 static void hclge_get_queue_id_in_pf(struct hclge_vport *vport,
589 				     struct hclge_mbx_vf_to_pf_cmd *mbx_req,
590 				     struct hclge_respond_to_vf_msg *resp_msg)
591 {
592 	u16 queue_id, qid_in_pf;
593 
594 	memcpy(&queue_id, mbx_req->msg.data, sizeof(queue_id));
595 	qid_in_pf = hclge_covert_handle_qid_global(&vport->nic, queue_id);
596 	memcpy(resp_msg->data, &qid_in_pf, sizeof(qid_in_pf));
597 	resp_msg->len = sizeof(qid_in_pf);
598 }
599 
600 static void hclge_get_rss_key(struct hclge_vport *vport,
601 			      struct hclge_mbx_vf_to_pf_cmd *mbx_req,
602 			      struct hclge_respond_to_vf_msg *resp_msg)
603 {
604 #define HCLGE_RSS_MBX_RESP_LEN	8
605 	struct hclge_dev *hdev = vport->back;
606 	u8 index;
607 
608 	index = mbx_req->msg.data[0];
609 
610 	/* Check the query index of rss_hash_key from VF, make sure no
611 	 * more than the size of rss_hash_key.
612 	 */
613 	if (((index + 1) * HCLGE_RSS_MBX_RESP_LEN) >
614 	      sizeof(vport[0].rss_hash_key)) {
615 		dev_warn(&hdev->pdev->dev,
616 			 "failed to get the rss hash key, the index(%u) invalid !\n",
617 			 index);
618 		return;
619 	}
620 
621 	memcpy(resp_msg->data,
622 	       &hdev->vport[0].rss_hash_key[index * HCLGE_RSS_MBX_RESP_LEN],
623 	       HCLGE_RSS_MBX_RESP_LEN);
624 	resp_msg->len = HCLGE_RSS_MBX_RESP_LEN;
625 }
626 
627 static void hclge_link_fail_parse(struct hclge_dev *hdev, u8 link_fail_code)
628 {
629 	switch (link_fail_code) {
630 	case HCLGE_LF_REF_CLOCK_LOST:
631 		dev_warn(&hdev->pdev->dev, "Reference clock lost!\n");
632 		break;
633 	case HCLGE_LF_XSFP_TX_DISABLE:
634 		dev_warn(&hdev->pdev->dev, "SFP tx is disabled!\n");
635 		break;
636 	case HCLGE_LF_XSFP_ABSENT:
637 		dev_warn(&hdev->pdev->dev, "SFP is absent!\n");
638 		break;
639 	default:
640 		break;
641 	}
642 }
643 
644 static void hclge_handle_link_change_event(struct hclge_dev *hdev,
645 					   struct hclge_mbx_vf_to_pf_cmd *req)
646 {
647 	hclge_task_schedule(hdev, 0);
648 
649 	if (!req->msg.subcode)
650 		hclge_link_fail_parse(hdev, req->msg.data[0]);
651 }
652 
653 static bool hclge_cmd_crq_empty(struct hclge_hw *hw)
654 {
655 	u32 tail = hclge_read_dev(hw, HCLGE_NIC_CRQ_TAIL_REG);
656 
657 	return tail == hw->cmq.crq.next_to_use;
658 }
659 
660 static void hclge_handle_ncsi_error(struct hclge_dev *hdev)
661 {
662 	struct hnae3_ae_dev *ae_dev = hdev->ae_dev;
663 
664 	ae_dev->ops->set_default_reset_request(ae_dev, HNAE3_GLOBAL_RESET);
665 	dev_warn(&hdev->pdev->dev, "requesting reset due to NCSI error\n");
666 	ae_dev->ops->reset_event(hdev->pdev, NULL);
667 }
668 
669 static void hclge_handle_vf_tbl(struct hclge_vport *vport,
670 				struct hclge_mbx_vf_to_pf_cmd *mbx_req)
671 {
672 	struct hclge_dev *hdev = vport->back;
673 	struct hclge_vf_vlan_cfg *msg_cmd;
674 
675 	msg_cmd = (struct hclge_vf_vlan_cfg *)&mbx_req->msg;
676 	if (msg_cmd->subcode == HCLGE_MBX_VPORT_LIST_CLEAR) {
677 		hclge_rm_vport_all_mac_table(vport, true, HCLGE_MAC_ADDR_UC);
678 		hclge_rm_vport_all_mac_table(vport, true, HCLGE_MAC_ADDR_MC);
679 		hclge_rm_vport_all_vlan_table(vport, true);
680 	} else {
681 		dev_warn(&hdev->pdev->dev, "Invalid cmd(%u)\n",
682 			 msg_cmd->subcode);
683 	}
684 }
685 
686 void hclge_mbx_handler(struct hclge_dev *hdev)
687 {
688 	struct hclge_cmq_ring *crq = &hdev->hw.cmq.crq;
689 	struct hclge_respond_to_vf_msg resp_msg;
690 	struct hclge_mbx_vf_to_pf_cmd *req;
691 	struct hclge_vport *vport;
692 	struct hclge_desc *desc;
693 	bool is_del = false;
694 	unsigned int flag;
695 	int ret = 0;
696 
697 	memset(&resp_msg, 0, sizeof(resp_msg));
698 	/* handle all the mailbox requests in the queue */
699 	while (!hclge_cmd_crq_empty(&hdev->hw)) {
700 		if (test_bit(HCLGE_STATE_CMD_DISABLE, &hdev->state)) {
701 			dev_warn(&hdev->pdev->dev,
702 				 "command queue needs re-initializing\n");
703 			return;
704 		}
705 
706 		desc = &crq->desc[crq->next_to_use];
707 		req = (struct hclge_mbx_vf_to_pf_cmd *)desc->data;
708 
709 		flag = le16_to_cpu(crq->desc[crq->next_to_use].flag);
710 		if (unlikely(!hnae3_get_bit(flag, HCLGE_CMDQ_RX_OUTVLD_B))) {
711 			dev_warn(&hdev->pdev->dev,
712 				 "dropped invalid mailbox message, code = %u\n",
713 				 req->msg.code);
714 
715 			/* dropping/not processing this invalid message */
716 			crq->desc[crq->next_to_use].flag = 0;
717 			hclge_mbx_ring_ptr_move_crq(crq);
718 			continue;
719 		}
720 
721 		vport = &hdev->vport[req->mbx_src_vfid];
722 
723 		trace_hclge_pf_mbx_get(hdev, req);
724 
725 		switch (req->msg.code) {
726 		case HCLGE_MBX_MAP_RING_TO_VECTOR:
727 			ret = hclge_map_unmap_ring_to_vf_vector(vport, true,
728 								req);
729 			break;
730 		case HCLGE_MBX_UNMAP_RING_TO_VECTOR:
731 			ret = hclge_map_unmap_ring_to_vf_vector(vport, false,
732 								req);
733 			break;
734 		case HCLGE_MBX_SET_PROMISC_MODE:
735 			ret = hclge_set_vf_promisc_mode(vport, req);
736 			if (ret)
737 				dev_err(&hdev->pdev->dev,
738 					"PF fail(%d) to set VF promisc mode\n",
739 					ret);
740 			break;
741 		case HCLGE_MBX_SET_UNICAST:
742 			ret = hclge_set_vf_uc_mac_addr(vport, req);
743 			if (ret)
744 				dev_err(&hdev->pdev->dev,
745 					"PF fail(%d) to set VF UC MAC Addr\n",
746 					ret);
747 			break;
748 		case HCLGE_MBX_SET_MULTICAST:
749 			ret = hclge_set_vf_mc_mac_addr(vport, req);
750 			if (ret)
751 				dev_err(&hdev->pdev->dev,
752 					"PF fail(%d) to set VF MC MAC Addr\n",
753 					ret);
754 			break;
755 		case HCLGE_MBX_SET_VLAN:
756 			ret = hclge_set_vf_vlan_cfg(vport, req, &resp_msg);
757 			if (ret)
758 				dev_err(&hdev->pdev->dev,
759 					"PF failed(%d) to config VF's VLAN\n",
760 					ret);
761 			break;
762 		case HCLGE_MBX_SET_ALIVE:
763 			ret = hclge_set_vf_alive(vport, req);
764 			if (ret)
765 				dev_err(&hdev->pdev->dev,
766 					"PF failed(%d) to set VF's ALIVE\n",
767 					ret);
768 			break;
769 		case HCLGE_MBX_GET_QINFO:
770 			hclge_get_vf_queue_info(vport, &resp_msg);
771 			break;
772 		case HCLGE_MBX_GET_QDEPTH:
773 			hclge_get_vf_queue_depth(vport, &resp_msg);
774 			break;
775 		case HCLGE_MBX_GET_TCINFO:
776 			hclge_get_vf_tcinfo(vport, &resp_msg);
777 			break;
778 		case HCLGE_MBX_GET_LINK_STATUS:
779 			ret = hclge_get_link_info(vport, req);
780 			if (ret)
781 				dev_err(&hdev->pdev->dev,
782 					"failed to inform link stat to VF, ret = %d\n",
783 					ret);
784 			break;
785 		case HCLGE_MBX_QUEUE_RESET:
786 			hclge_mbx_reset_vf_queue(vport, req);
787 			break;
788 		case HCLGE_MBX_RESET:
789 			ret = hclge_reset_vf(vport);
790 			break;
791 		case HCLGE_MBX_KEEP_ALIVE:
792 			hclge_vf_keep_alive(vport);
793 			break;
794 		case HCLGE_MBX_SET_MTU:
795 			ret = hclge_set_vf_mtu(vport, req);
796 			if (ret)
797 				dev_err(&hdev->pdev->dev,
798 					"VF fail(%d) to set mtu\n", ret);
799 			break;
800 		case HCLGE_MBX_GET_QID_IN_PF:
801 			hclge_get_queue_id_in_pf(vport, req, &resp_msg);
802 			break;
803 		case HCLGE_MBX_GET_RSS_KEY:
804 			hclge_get_rss_key(vport, req, &resp_msg);
805 			break;
806 		case HCLGE_MBX_GET_LINK_MODE:
807 			hclge_get_link_mode(vport, req);
808 			break;
809 		case HCLGE_MBX_GET_VF_FLR_STATUS:
810 		case HCLGE_MBX_VF_UNINIT:
811 			is_del = req->msg.code == HCLGE_MBX_VF_UNINIT;
812 			hclge_rm_vport_all_mac_table(vport, is_del,
813 						     HCLGE_MAC_ADDR_UC);
814 			hclge_rm_vport_all_mac_table(vport, is_del,
815 						     HCLGE_MAC_ADDR_MC);
816 			hclge_rm_vport_all_vlan_table(vport, is_del);
817 			break;
818 		case HCLGE_MBX_GET_MEDIA_TYPE:
819 			hclge_get_vf_media_type(vport, &resp_msg);
820 			break;
821 		case HCLGE_MBX_PUSH_LINK_STATUS:
822 			hclge_handle_link_change_event(hdev, req);
823 			break;
824 		case HCLGE_MBX_GET_MAC_ADDR:
825 			hclge_get_vf_mac_addr(vport, &resp_msg);
826 			break;
827 		case HCLGE_MBX_NCSI_ERROR:
828 			hclge_handle_ncsi_error(hdev);
829 			break;
830 		case HCLGE_MBX_HANDLE_VF_TBL:
831 			hclge_handle_vf_tbl(vport, req);
832 			break;
833 		default:
834 			dev_err(&hdev->pdev->dev,
835 				"un-supported mailbox message, code = %u\n",
836 				req->msg.code);
837 			break;
838 		}
839 
840 		/* PF driver should not reply IMP */
841 		if (hnae3_get_bit(req->mbx_need_resp, HCLGE_MBX_NEED_RESP_B) &&
842 		    req->msg.code < HCLGE_MBX_GET_VF_FLR_STATUS) {
843 			resp_msg.status = ret;
844 			hclge_gen_resp_to_vf(vport, req, &resp_msg);
845 		}
846 
847 		crq->desc[crq->next_to_use].flag = 0;
848 		hclge_mbx_ring_ptr_move_crq(crq);
849 
850 		/* reinitialize ret after complete the mbx message processing */
851 		ret = 0;
852 	}
853 
854 	/* Write back CMDQ_RQ header pointer, M7 need this pointer */
855 	hclge_write_dev(&hdev->hw, HCLGE_NIC_CRQ_HEAD_REG, crq->next_to_use);
856 }
857