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 	if (i >= HCLGEVF_MAX_TRY_TIMES) {
64 		dev_err(&hdev->pdev->dev,
65 			"VF could not get mbx(%u,%u) resp(=%d) from PF in %d tries\n",
66 			code0, code1, hdev->mbx_resp.received_resp, i);
67 		return -EIO;
68 	}
69 
70 	mbx_resp = &hdev->mbx_resp;
71 	r_code0 = (u16)(mbx_resp->origin_mbx_msg >> 16);
72 	r_code1 = (u16)(mbx_resp->origin_mbx_msg & 0xff);
73 
74 	if (mbx_resp->resp_status)
75 		return mbx_resp->resp_status;
76 
77 	if (resp_data)
78 		memcpy(resp_data, &mbx_resp->additional_info[0], resp_len);
79 
80 	hclgevf_reset_mbx_resp_status(hdev);
81 
82 	if (!(r_code0 == code0 && r_code1 == code1 && !mbx_resp->resp_status)) {
83 		dev_err(&hdev->pdev->dev,
84 			"VF could not match resp code(code0=%u,code1=%u), %d\n",
85 			code0, code1, mbx_resp->resp_status);
86 		dev_err(&hdev->pdev->dev,
87 			"VF could not match resp r_code(r_code0=%u,r_code1=%u)\n",
88 			r_code0, r_code1);
89 		return -EIO;
90 	}
91 
92 	return 0;
93 }
94 
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)95 int hclgevf_send_mbx_msg(struct hclgevf_dev *hdev,
96 			 struct hclge_vf_to_pf_msg *send_msg, bool need_resp,
97 			 u8 *resp_data, u16 resp_len)
98 {
99 	struct hclge_mbx_vf_to_pf_cmd *req;
100 	struct hclgevf_desc desc;
101 	int status;
102 
103 	req = (struct hclge_mbx_vf_to_pf_cmd *)desc.data;
104 
105 	if (!send_msg) {
106 		dev_err(&hdev->pdev->dev,
107 			"failed to send mbx, msg is NULL\n");
108 		return -EINVAL;
109 	}
110 
111 	hclgevf_cmd_setup_basic_desc(&desc, HCLGEVF_OPC_MBX_VF_TO_PF, false);
112 	if (need_resp)
113 		hnae3_set_bit(req->mbx_need_resp, HCLGE_MBX_NEED_RESP_B, 1);
114 
115 	memcpy(&req->msg, send_msg, sizeof(struct hclge_vf_to_pf_msg));
116 
117 	if (test_bit(HCLGEVF_STATE_NIC_REGISTERED, &hdev->state))
118 		trace_hclge_vf_mbx_send(hdev, req);
119 
120 	/* synchronous send */
121 	if (need_resp) {
122 		mutex_lock(&hdev->mbx_resp.mbx_mutex);
123 		hclgevf_reset_mbx_resp_status(hdev);
124 		req->match_id = hdev->mbx_resp.match_id;
125 		status = hclgevf_cmd_send(&hdev->hw, &desc, 1);
126 		if (status) {
127 			dev_err(&hdev->pdev->dev,
128 				"VF failed(=%d) to send mbx message to PF\n",
129 				status);
130 			mutex_unlock(&hdev->mbx_resp.mbx_mutex);
131 			return status;
132 		}
133 
134 		status = hclgevf_get_mbx_resp(hdev, send_msg->code,
135 					      send_msg->subcode, resp_data,
136 					      resp_len);
137 		mutex_unlock(&hdev->mbx_resp.mbx_mutex);
138 	} else {
139 		/* asynchronous send */
140 		status = hclgevf_cmd_send(&hdev->hw, &desc, 1);
141 		if (status) {
142 			dev_err(&hdev->pdev->dev,
143 				"VF failed(=%d) to send mbx message to PF\n",
144 				status);
145 			return status;
146 		}
147 	}
148 
149 	return status;
150 }
151 
hclgevf_cmd_crq_empty(struct hclgevf_hw * hw)152 static bool hclgevf_cmd_crq_empty(struct hclgevf_hw *hw)
153 {
154 	u32 tail = hclgevf_read_dev(hw, HCLGEVF_NIC_CRQ_TAIL_REG);
155 
156 	return tail == hw->cmq.crq.next_to_use;
157 }
158 
hclgevf_handle_mbx_response(struct hclgevf_dev * hdev,struct hclge_mbx_pf_to_vf_cmd * req)159 static void hclgevf_handle_mbx_response(struct hclgevf_dev *hdev,
160 					struct hclge_mbx_pf_to_vf_cmd *req)
161 {
162 	struct hclgevf_mbx_resp_status *resp = &hdev->mbx_resp;
163 
164 	if (resp->received_resp)
165 		dev_warn(&hdev->pdev->dev,
166 			 "VF mbx resp flag not clear(%u)\n",
167 			 req->msg.vf_mbx_msg_code);
168 
169 	resp->origin_mbx_msg =
170 			(req->msg.vf_mbx_msg_code << 16);
171 	resp->origin_mbx_msg |= req->msg.vf_mbx_msg_subcode;
172 	resp->resp_status =
173 		hclgevf_resp_to_errno(req->msg.resp_status);
174 	memcpy(resp->additional_info, req->msg.resp_data,
175 	       HCLGE_MBX_MAX_RESP_DATA_SIZE * sizeof(u8));
176 	if (req->match_id) {
177 		/* If match_id is not zero, it means PF support match_id.
178 		 * if the match_id is right, VF get the right response, or
179 		 * ignore the response. and driver will clear hdev->mbx_resp
180 		 * when send next message which need response.
181 		 */
182 		if (req->match_id == resp->match_id)
183 			resp->received_resp = true;
184 	} else {
185 		resp->received_resp = true;
186 	}
187 }
188 
hclgevf_handle_mbx_msg(struct hclgevf_dev * hdev,struct hclge_mbx_pf_to_vf_cmd * req)189 static void hclgevf_handle_mbx_msg(struct hclgevf_dev *hdev,
190 				   struct hclge_mbx_pf_to_vf_cmd *req)
191 {
192 	/* we will drop the async msg if we find ARQ as full
193 	 * and continue with next message
194 	 */
195 	if (atomic_read(&hdev->arq.count) >=
196 	    HCLGE_MBX_MAX_ARQ_MSG_NUM) {
197 		dev_warn(&hdev->pdev->dev,
198 			 "Async Q full, dropping msg(%u)\n",
199 			 req->msg.code);
200 		return;
201 	}
202 
203 	/* tail the async message in arq */
204 	memcpy(hdev->arq.msg_q[hdev->arq.tail], &req->msg,
205 	       HCLGE_MBX_MAX_ARQ_MSG_SIZE * sizeof(u16));
206 	hclge_mbx_tail_ptr_move_arq(hdev->arq);
207 	atomic_inc(&hdev->arq.count);
208 
209 	hclgevf_mbx_task_schedule(hdev);
210 }
211 
hclgevf_mbx_handler(struct hclgevf_dev * hdev)212 void hclgevf_mbx_handler(struct hclgevf_dev *hdev)
213 {
214 	struct hclge_mbx_pf_to_vf_cmd *req;
215 	struct hclgevf_cmq_ring *crq;
216 	struct hclgevf_desc *desc;
217 	u16 flag;
218 
219 	crq = &hdev->hw.cmq.crq;
220 
221 	while (!hclgevf_cmd_crq_empty(&hdev->hw)) {
222 		if (test_bit(HCLGEVF_STATE_CMD_DISABLE, &hdev->state)) {
223 			dev_info(&hdev->pdev->dev, "vf crq need init\n");
224 			return;
225 		}
226 
227 		desc = &crq->desc[crq->next_to_use];
228 		req = (struct hclge_mbx_pf_to_vf_cmd *)desc->data;
229 
230 		flag = le16_to_cpu(crq->desc[crq->next_to_use].flag);
231 		if (unlikely(!hnae3_get_bit(flag, HCLGEVF_CMDQ_RX_OUTVLD_B))) {
232 			dev_warn(&hdev->pdev->dev,
233 				 "dropped invalid mailbox message, code = %u\n",
234 				 req->msg.code);
235 
236 			/* dropping/not processing this invalid message */
237 			crq->desc[crq->next_to_use].flag = 0;
238 			hclge_mbx_ring_ptr_move_crq(crq);
239 			continue;
240 		}
241 
242 		trace_hclge_vf_mbx_get(hdev, req);
243 
244 		/* synchronous messages are time critical and need preferential
245 		 * treatment. Therefore, we need to acknowledge all the sync
246 		 * responses as quickly as possible so that waiting tasks do not
247 		 * timeout and simultaneously queue the async messages for later
248 		 * prcessing in context of mailbox task i.e. the slow path.
249 		 */
250 		switch (req->msg.code) {
251 		case HCLGE_MBX_PF_VF_RESP:
252 			hclgevf_handle_mbx_response(hdev, req);
253 			break;
254 		case HCLGE_MBX_LINK_STAT_CHANGE:
255 		case HCLGE_MBX_ASSERTING_RESET:
256 		case HCLGE_MBX_LINK_STAT_MODE:
257 		case HCLGE_MBX_PUSH_VLAN_INFO:
258 		case HCLGE_MBX_PUSH_PROMISC_INFO:
259 			hclgevf_handle_mbx_msg(hdev, req);
260 			break;
261 		default:
262 			dev_err(&hdev->pdev->dev,
263 				"VF received unsupported(%u) mbx msg from PF\n",
264 				req->msg.code);
265 			break;
266 		}
267 		crq->desc[crq->next_to_use].flag = 0;
268 		hclge_mbx_ring_ptr_move_crq(crq);
269 	}
270 
271 	/* Write back CMDQ_RQ header pointer, M7 need this pointer */
272 	hclgevf_write_dev(&hdev->hw, HCLGEVF_NIC_CRQ_HEAD_REG,
273 			  crq->next_to_use);
274 }
275 
hclgevf_parse_promisc_info(struct hclgevf_dev * hdev,u16 promisc_info)276 static void hclgevf_parse_promisc_info(struct hclgevf_dev *hdev,
277 				       u16 promisc_info)
278 {
279 	if (!promisc_info)
280 		dev_info(&hdev->pdev->dev,
281 			 "Promisc mode is closed by host for being untrusted.\n");
282 }
283 
hclgevf_mbx_async_handler(struct hclgevf_dev * hdev)284 void hclgevf_mbx_async_handler(struct hclgevf_dev *hdev)
285 {
286 	enum hnae3_reset_type reset_type;
287 	u16 link_status, state;
288 	u16 *msg_q, *vlan_info;
289 	u8 duplex;
290 	u32 speed;
291 	u32 tail;
292 	u8 flag;
293 	u8 idx;
294 
295 	tail = hdev->arq.tail;
296 
297 	/* process all the async queue messages */
298 	while (tail != hdev->arq.head) {
299 		if (test_bit(HCLGEVF_STATE_CMD_DISABLE, &hdev->state)) {
300 			dev_info(&hdev->pdev->dev,
301 				 "vf crq need init in async\n");
302 			return;
303 		}
304 
305 		msg_q = hdev->arq.msg_q[hdev->arq.head];
306 
307 		switch (msg_q[0]) {
308 		case HCLGE_MBX_LINK_STAT_CHANGE:
309 			link_status = msg_q[1];
310 			memcpy(&speed, &msg_q[2], sizeof(speed));
311 			duplex = (u8)msg_q[4];
312 			flag = (u8)msg_q[5];
313 
314 			/* update upper layer with new link link status */
315 			hclgevf_update_speed_duplex(hdev, speed, duplex);
316 			hclgevf_update_link_status(hdev, link_status);
317 
318 			if (flag & HCLGE_MBX_PUSH_LINK_STATUS_EN)
319 				set_bit(HCLGEVF_STATE_PF_PUSH_LINK_STATUS,
320 					&hdev->state);
321 
322 			break;
323 		case HCLGE_MBX_LINK_STAT_MODE:
324 			idx = (u8)msg_q[1];
325 			if (idx)
326 				memcpy(&hdev->hw.mac.supported, &msg_q[2],
327 				       sizeof(unsigned long));
328 			else
329 				memcpy(&hdev->hw.mac.advertising, &msg_q[2],
330 				       sizeof(unsigned long));
331 			break;
332 		case HCLGE_MBX_ASSERTING_RESET:
333 			/* PF has asserted reset hence VF should go in pending
334 			 * state and poll for the hardware reset status till it
335 			 * has been completely reset. After this stack should
336 			 * eventually be re-initialized.
337 			 */
338 			reset_type = (enum hnae3_reset_type)msg_q[1];
339 			set_bit(reset_type, &hdev->reset_pending);
340 			set_bit(HCLGEVF_RESET_PENDING, &hdev->reset_state);
341 			hclgevf_reset_task_schedule(hdev);
342 
343 			break;
344 		case HCLGE_MBX_PUSH_VLAN_INFO:
345 			state = msg_q[1];
346 			vlan_info = &msg_q[1];
347 			hclgevf_update_port_base_vlan_info(hdev, state,
348 							   (u8 *)vlan_info, 8);
349 			break;
350 		case HCLGE_MBX_PUSH_PROMISC_INFO:
351 			hclgevf_parse_promisc_info(hdev, msg_q[1]);
352 			break;
353 		default:
354 			dev_err(&hdev->pdev->dev,
355 				"fetched unsupported(%u) message from arq\n",
356 				msg_q[0]);
357 			break;
358 		}
359 
360 		hclge_mbx_head_ptr_move_arq(hdev->arq);
361 		atomic_dec(&hdev->arq.count);
362 		msg_q = hdev->arq.msg_q[hdev->arq.head];
363 	}
364 }
365