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