• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0+
2 // Copyright (c) 2016-2017 Hisilicon Limited.
3 
4 #include "hclge_mbx.h"
5 #include "hclgevf_main.h"
6 #include "hnae3.h"
7 
8 #define CREATE_TRACE_POINTS
9 #include "hclgevf_trace.h"
10 
hclgevf_resp_to_errno(u16 resp_code)11 static int hclgevf_resp_to_errno(u16 resp_code)
12 {
13 	return resp_code ? -resp_code : 0;
14 }
15 
16 #define HCLGEVF_MBX_MATCH_ID_START	1
hclgevf_reset_mbx_resp_status(struct hclgevf_dev * hdev)17 static void hclgevf_reset_mbx_resp_status(struct hclgevf_dev *hdev)
18 {
19 	/* this function should be called with mbx_resp.mbx_mutex held
20 	 * to prtect the received_response from race condition
21 	 */
22 	hdev->mbx_resp.received_resp  = false;
23 	hdev->mbx_resp.origin_mbx_msg = 0;
24 	hdev->mbx_resp.resp_status    = 0;
25 	hdev->mbx_resp.match_id++;
26 	/* Update match_id and ensure the value of match_id is not zero */
27 	if (hdev->mbx_resp.match_id == 0)
28 		hdev->mbx_resp.match_id = HCLGEVF_MBX_MATCH_ID_START;
29 	memset(hdev->mbx_resp.additional_info, 0, HCLGE_MBX_MAX_RESP_DATA_SIZE);
30 }
31 
32 /* hclgevf_get_mbx_resp: used to get a response from PF after VF sends a mailbox
33  * message to PF.
34  * @hdev: pointer to struct hclgevf_dev
35  * @resp_msg: pointer to store the original message type and response status
36  * @len: the resp_msg data array length.
37  */
hclgevf_get_mbx_resp(struct hclgevf_dev * hdev,u16 code0,u16 code1,u8 * resp_data,u16 resp_len)38 static int hclgevf_get_mbx_resp(struct hclgevf_dev *hdev, u16 code0, u16 code1,
39 				u8 *resp_data, u16 resp_len)
40 {
41 #define HCLGEVF_MAX_TRY_TIMES	500
42 #define HCLGEVF_SLEEP_USECOND	1000
43 	struct hclgevf_mbx_resp_status *mbx_resp;
44 	u16 r_code0, r_code1;
45 	int i = 0;
46 
47 	if (resp_len > HCLGE_MBX_MAX_RESP_DATA_SIZE) {
48 		dev_err(&hdev->pdev->dev,
49 			"VF mbx response len(=%u) exceeds maximum(=%u)\n",
50 			resp_len,
51 			HCLGE_MBX_MAX_RESP_DATA_SIZE);
52 		return -EINVAL;
53 	}
54 
55 	while ((!hdev->mbx_resp.received_resp) && (i < HCLGEVF_MAX_TRY_TIMES)) {
56 		if (test_bit(HCLGEVF_STATE_CMD_DISABLE, &hdev->state))
57 			return -EIO;
58 
59 		usleep_range(HCLGEVF_SLEEP_USECOND, HCLGEVF_SLEEP_USECOND * 2);
60 		i++;
61 	}
62 
63 	/* ensure additional_info will be seen after received_resp */
64 	smp_rmb();
65 
66 	if (i >= HCLGEVF_MAX_TRY_TIMES) {
67 		dev_err(&hdev->pdev->dev,
68 			"VF could not get mbx(%u,%u) resp(=%d) from PF in %d tries\n",
69 			code0, code1, hdev->mbx_resp.received_resp, i);
70 		return -EIO;
71 	}
72 
73 	mbx_resp = &hdev->mbx_resp;
74 	r_code0 = (u16)(mbx_resp->origin_mbx_msg >> 16);
75 	r_code1 = (u16)(mbx_resp->origin_mbx_msg & 0xff);
76 
77 	if (mbx_resp->resp_status)
78 		return mbx_resp->resp_status;
79 
80 	if (resp_data)
81 		memcpy(resp_data, &mbx_resp->additional_info[0], resp_len);
82 
83 	hclgevf_reset_mbx_resp_status(hdev);
84 
85 	if (!(r_code0 == code0 && r_code1 == code1 && !mbx_resp->resp_status)) {
86 		dev_err(&hdev->pdev->dev,
87 			"VF could not match resp code(code0=%u,code1=%u), %d\n",
88 			code0, code1, mbx_resp->resp_status);
89 		dev_err(&hdev->pdev->dev,
90 			"VF could not match resp r_code(r_code0=%u,r_code1=%u)\n",
91 			r_code0, r_code1);
92 		return -EIO;
93 	}
94 
95 	return 0;
96 }
97 
hclgevf_send_mbx_msg(struct hclgevf_dev * hdev,struct hclge_vf_to_pf_msg * send_msg,bool need_resp,u8 * resp_data,u16 resp_len)98 int hclgevf_send_mbx_msg(struct hclgevf_dev *hdev,
99 			 struct hclge_vf_to_pf_msg *send_msg, bool need_resp,
100 			 u8 *resp_data, u16 resp_len)
101 {
102 	struct hclge_mbx_vf_to_pf_cmd *req;
103 	struct hclgevf_desc desc;
104 	int status;
105 
106 	req = (struct hclge_mbx_vf_to_pf_cmd *)desc.data;
107 
108 	if (!send_msg) {
109 		dev_err(&hdev->pdev->dev,
110 			"failed to send mbx, msg is NULL\n");
111 		return -EINVAL;
112 	}
113 
114 	hclgevf_cmd_setup_basic_desc(&desc, HCLGEVF_OPC_MBX_VF_TO_PF, false);
115 	if (need_resp)
116 		hnae3_set_bit(req->mbx_need_resp, HCLGE_MBX_NEED_RESP_B, 1);
117 
118 	memcpy(&req->msg, send_msg, sizeof(struct hclge_vf_to_pf_msg));
119 
120 	if (test_bit(HCLGEVF_STATE_NIC_REGISTERED, &hdev->state))
121 		trace_hclge_vf_mbx_send(hdev, req);
122 
123 	/* synchronous send */
124 	if (need_resp) {
125 		mutex_lock(&hdev->mbx_resp.mbx_mutex);
126 		hclgevf_reset_mbx_resp_status(hdev);
127 		req->match_id = cpu_to_le16(hdev->mbx_resp.match_id);
128 		status = hclgevf_cmd_send(&hdev->hw, &desc, 1);
129 		if (status) {
130 			dev_err(&hdev->pdev->dev,
131 				"VF failed(=%d) to send mbx message to PF\n",
132 				status);
133 			mutex_unlock(&hdev->mbx_resp.mbx_mutex);
134 			return status;
135 		}
136 
137 		status = hclgevf_get_mbx_resp(hdev, send_msg->code,
138 					      send_msg->subcode, resp_data,
139 					      resp_len);
140 		mutex_unlock(&hdev->mbx_resp.mbx_mutex);
141 	} else {
142 		/* asynchronous send */
143 		status = hclgevf_cmd_send(&hdev->hw, &desc, 1);
144 		if (status) {
145 			dev_err(&hdev->pdev->dev,
146 				"VF failed(=%d) to send mbx message to PF\n",
147 				status);
148 			return status;
149 		}
150 	}
151 
152 	return status;
153 }
154 
hclgevf_cmd_crq_empty(struct hclgevf_hw * hw)155 static bool hclgevf_cmd_crq_empty(struct hclgevf_hw *hw)
156 {
157 	u32 tail = hclgevf_read_dev(hw, HCLGEVF_NIC_CRQ_TAIL_REG);
158 
159 	return tail == hw->cmq.crq.next_to_use;
160 }
161 
hclgevf_handle_mbx_response(struct hclgevf_dev * hdev,struct hclge_mbx_pf_to_vf_cmd * req)162 static void hclgevf_handle_mbx_response(struct hclgevf_dev *hdev,
163 					struct hclge_mbx_pf_to_vf_cmd *req)
164 {
165 	u16 vf_mbx_msg_subcode = le16_to_cpu(req->msg.vf_mbx_msg_subcode);
166 	u16 vf_mbx_msg_code = le16_to_cpu(req->msg.vf_mbx_msg_code);
167 	struct hclgevf_mbx_resp_status *resp = &hdev->mbx_resp;
168 	u16 resp_status = le16_to_cpu(req->msg.resp_status);
169 	u16 match_id = le16_to_cpu(req->match_id);
170 
171 	if (resp->received_resp)
172 		dev_warn(&hdev->pdev->dev,
173 			"VF mbx resp flag not clear(%u)\n",
174 			 vf_mbx_msg_code);
175 
176 	resp->origin_mbx_msg = (vf_mbx_msg_code << 16);
177 	resp->origin_mbx_msg |= vf_mbx_msg_subcode;
178 	resp->resp_status = hclgevf_resp_to_errno(resp_status);
179 	memcpy(resp->additional_info, req->msg.resp_data,
180 	       HCLGE_MBX_MAX_RESP_DATA_SIZE * sizeof(u8));
181 
182 	/* ensure additional_info will be seen before setting received_resp */
183 	smp_wmb();
184 
185 	if (match_id) {
186 		/* If match_id is not zero, it means PF support match_id.
187 		 * if the match_id is right, VF get the right response, or
188 		 * ignore the response. and driver will clear hdev->mbx_resp
189 		 * when send next message which need response.
190 		 */
191 		if (match_id == resp->match_id)
192 			resp->received_resp = true;
193 	} else {
194 		resp->received_resp = true;
195 	}
196 }
197 
hclgevf_handle_mbx_msg(struct hclgevf_dev * hdev,struct hclge_mbx_pf_to_vf_cmd * req)198 static void hclgevf_handle_mbx_msg(struct hclgevf_dev *hdev,
199 				   struct hclge_mbx_pf_to_vf_cmd *req)
200 {
201 	/* we will drop the async msg if we find ARQ as full
202 	 * and continue with next message
203 	 */
204 	if (atomic_read(&hdev->arq.count) >=
205 	    HCLGE_MBX_MAX_ARQ_MSG_NUM) {
206 		dev_warn(&hdev->pdev->dev,
207 			 "Async Q full, dropping msg(%u)\n",
208 			 le16_to_cpu(req->msg.code));
209 		return;
210 	}
211 
212 	/* tail the async message in arq */
213 	memcpy(hdev->arq.msg_q[hdev->arq.tail], &req->msg,
214 	       HCLGE_MBX_MAX_ARQ_MSG_SIZE * sizeof(u16));
215 	hclge_mbx_tail_ptr_move_arq(hdev->arq);
216 	atomic_inc(&hdev->arq.count);
217 
218 	hclgevf_mbx_task_schedule(hdev);
219 }
220 
hclgevf_mbx_handler(struct hclgevf_dev * hdev)221 void hclgevf_mbx_handler(struct hclgevf_dev *hdev)
222 {
223 	struct hclge_mbx_pf_to_vf_cmd *req;
224 	struct hclgevf_cmq_ring *crq;
225 	struct hclgevf_desc *desc;
226 	u16 flag;
227 	u16 code;
228 
229 	crq = &hdev->hw.cmq.crq;
230 
231 	while (!hclgevf_cmd_crq_empty(&hdev->hw)) {
232 		if (test_bit(HCLGEVF_STATE_CMD_DISABLE, &hdev->state)) {
233 			dev_info(&hdev->pdev->dev, "vf crq need init\n");
234 			return;
235 		}
236 
237 		desc = &crq->desc[crq->next_to_use];
238 		req = (struct hclge_mbx_pf_to_vf_cmd *)desc->data;
239 
240 		flag = le16_to_cpu(crq->desc[crq->next_to_use].flag);
241 		code = le16_to_cpu(req->msg.code);
242 		if (unlikely(!hnae3_get_bit(flag, HCLGEVF_CMDQ_RX_OUTVLD_B))) {
243 			dev_warn(&hdev->pdev->dev,
244 				 "dropped invalid mailbox message, code = %u\n",
245 				 code);
246 
247 			/* dropping/not processing this invalid message */
248 			crq->desc[crq->next_to_use].flag = 0;
249 			hclge_mbx_ring_ptr_move_crq(crq);
250 			continue;
251 		}
252 
253 		trace_hclge_vf_mbx_get(hdev, req);
254 
255 		/* synchronous messages are time critical and need preferential
256 		 * treatment. Therefore, we need to acknowledge all the sync
257 		 * responses as quickly as possible so that waiting tasks do not
258 		 * timeout and simultaneously queue the async messages for later
259 		 * prcessing in context of mailbox task i.e. the slow path.
260 		 */
261 		switch (code) {
262 		case HCLGE_MBX_PF_VF_RESP:
263 			hclgevf_handle_mbx_response(hdev, req);
264 			break;
265 		case HCLGE_MBX_LINK_STAT_CHANGE:
266 		case HCLGE_MBX_ASSERTING_RESET:
267 		case HCLGE_MBX_LINK_STAT_MODE:
268 		case HCLGE_MBX_PUSH_VLAN_INFO:
269 		case HCLGE_MBX_PUSH_PROMISC_INFO:
270 			hclgevf_handle_mbx_msg(hdev, req);
271 			break;
272 		default:
273 			dev_err(&hdev->pdev->dev,
274 				"VF received unsupported(%u) mbx msg from PF\n",
275 				code);
276 			break;
277 		}
278 		crq->desc[crq->next_to_use].flag = 0;
279 		hclge_mbx_ring_ptr_move_crq(crq);
280 	}
281 
282 	/* Write back CMDQ_RQ header pointer, M7 need this pointer */
283 	hclgevf_write_dev(&hdev->hw, HCLGEVF_NIC_CRQ_HEAD_REG,
284 			  crq->next_to_use);
285 }
286 
hclgevf_parse_promisc_info(struct hclgevf_dev * hdev,u16 promisc_info)287 static void hclgevf_parse_promisc_info(struct hclgevf_dev *hdev,
288 				       u16 promisc_info)
289 {
290 	if (!promisc_info)
291 		dev_info(&hdev->pdev->dev,
292 			 "Promisc mode is closed by host for being untrusted.\n");
293 }
294 
hclgevf_mbx_async_handler(struct hclgevf_dev * hdev)295 void hclgevf_mbx_async_handler(struct hclgevf_dev *hdev)
296 {
297 	struct hclge_mbx_port_base_vlan *vlan_info;
298 	struct hclge_mbx_link_status *link_info;
299 	struct hclge_mbx_link_mode *link_mode;
300 	enum hnae3_reset_type reset_type;
301 	u16 link_status, state;
302 	__le16 *msg_q;
303 	u16 opcode;
304 	u8 duplex;
305 	u32 speed;
306 	u32 tail;
307 	u8 flag;
308 	u16 idx;
309 
310 	tail = hdev->arq.tail;
311 
312 	/* process all the async queue messages */
313 	while (tail != hdev->arq.head) {
314 		if (test_bit(HCLGEVF_STATE_CMD_DISABLE, &hdev->state)) {
315 			dev_info(&hdev->pdev->dev,
316 				 "vf crq need init in async\n");
317 			return;
318 		}
319 
320 		msg_q = hdev->arq.msg_q[hdev->arq.head];
321 		opcode = le16_to_cpu(msg_q[0]);
322 		switch (opcode) {
323 		case HCLGE_MBX_LINK_STAT_CHANGE:
324 			link_info = (struct hclge_mbx_link_status *)(msg_q + 1);
325 			link_status = le16_to_cpu(link_info->link_status);
326 			speed = le32_to_cpu(link_info->speed);
327 			duplex = (u8)le16_to_cpu(link_info->duplex);
328 			flag = link_info->flag;
329 
330 			/* update upper layer with new link link status */
331 			hclgevf_update_speed_duplex(hdev, speed, duplex);
332 			hclgevf_update_link_status(hdev, link_status);
333 
334 			if (flag & HCLGE_MBX_PUSH_LINK_STATUS_EN)
335 				set_bit(HCLGEVF_STATE_PF_PUSH_LINK_STATUS,
336 					&hdev->state);
337 
338 			break;
339 		case HCLGE_MBX_LINK_STAT_MODE:
340 			link_mode = (struct hclge_mbx_link_mode *)(msg_q + 1);
341 			idx = le16_to_cpu(link_mode->idx);
342 			if (idx)
343 				hdev->hw.mac.supported =
344 					le64_to_cpu(link_mode->link_mode);
345 			else
346 				hdev->hw.mac.advertising =
347 					le64_to_cpu(link_mode->link_mode);
348 			break;
349 		case HCLGE_MBX_ASSERTING_RESET:
350 			/* PF has asserted reset hence VF should go in pending
351 			 * state and poll for the hardware reset status till it
352 			 * has been completely reset. After this stack should
353 			 * eventually be re-initialized.
354 			 */
355 			reset_type =
356 				(enum hnae3_reset_type)le16_to_cpu(msg_q[1]);
357 			set_bit(reset_type, &hdev->reset_pending);
358 			set_bit(HCLGEVF_RESET_PENDING, &hdev->reset_state);
359 			hclgevf_reset_task_schedule(hdev);
360 
361 			break;
362 		case HCLGE_MBX_PUSH_VLAN_INFO:
363 			vlan_info =
364 				(struct hclge_mbx_port_base_vlan *)(msg_q + 1);
365 			state = le16_to_cpu(vlan_info->state);
366 			hclgevf_update_port_base_vlan_info(hdev, state,
367 							   vlan_info);
368 			break;
369 		case HCLGE_MBX_PUSH_PROMISC_INFO:
370 			hclgevf_parse_promisc_info(hdev, le16_to_cpu(msg_q[1]));
371 			break;
372 		default:
373 			dev_err(&hdev->pdev->dev,
374 				"fetched unsupported(%u) message from arq\n",
375 				opcode);
376 			break;
377 		}
378 
379 		hclge_mbx_head_ptr_move_arq(hdev->arq);
380 		atomic_dec(&hdev->arq.count);
381 		msg_q = hdev->arq.msg_q[hdev->arq.head];
382 	}
383 }
384