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