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 static void hclgevf_reset_mbx_resp_status(struct hclgevf_dev *hdev)
9 {
10 	/* this function should be called with mbx_resp.mbx_mutex held
11 	 * to prtect the received_response from race condition
12 	 */
13 	hdev->mbx_resp.received_resp  = false;
14 	hdev->mbx_resp.origin_mbx_msg = 0;
15 	hdev->mbx_resp.resp_status    = 0;
16 	memset(hdev->mbx_resp.additional_info, 0, HCLGE_MBX_MAX_RESP_DATA_SIZE);
17 }
18 
19 /* hclgevf_get_mbx_resp: used to get a response from PF after VF sends a mailbox
20  * message to PF.
21  * @hdev: pointer to struct hclgevf_dev
22  * @resp_msg: pointer to store the original message type and response status
23  * @len: the resp_msg data array length.
24  */
25 static int hclgevf_get_mbx_resp(struct hclgevf_dev *hdev, u16 code0, u16 code1,
26 				u8 *resp_data, u16 resp_len)
27 {
28 #define HCLGEVF_MAX_TRY_TIMES	500
29 #define HCLGEVF_SLEEP_USECOND	1000
30 	struct hclgevf_mbx_resp_status *mbx_resp;
31 	u16 r_code0, r_code1;
32 	int i = 0;
33 
34 	if (resp_len > HCLGE_MBX_MAX_RESP_DATA_SIZE) {
35 		dev_err(&hdev->pdev->dev,
36 			"VF mbx response len(=%d) exceeds maximum(=%d)\n",
37 			resp_len,
38 			HCLGE_MBX_MAX_RESP_DATA_SIZE);
39 		return -EINVAL;
40 	}
41 
42 	while ((!hdev->mbx_resp.received_resp) && (i < HCLGEVF_MAX_TRY_TIMES)) {
43 		if (test_bit(HCLGEVF_STATE_CMD_DISABLE, &hdev->state))
44 			return -EIO;
45 
46 		usleep_range(HCLGEVF_SLEEP_USECOND, HCLGEVF_SLEEP_USECOND * 2);
47 		i++;
48 	}
49 
50 	if (i >= HCLGEVF_MAX_TRY_TIMES) {
51 		dev_err(&hdev->pdev->dev,
52 			"VF could not get mbx(%d,%d) resp(=%d) from PF in %d tries\n",
53 			code0, code1, hdev->mbx_resp.received_resp, i);
54 		return -EIO;
55 	}
56 
57 	mbx_resp = &hdev->mbx_resp;
58 	r_code0 = (u16)(mbx_resp->origin_mbx_msg >> 16);
59 	r_code1 = (u16)(mbx_resp->origin_mbx_msg & 0xff);
60 
61 	if (mbx_resp->resp_status)
62 		return mbx_resp->resp_status;
63 
64 	if (resp_data)
65 		memcpy(resp_data, &mbx_resp->additional_info[0], resp_len);
66 
67 	hclgevf_reset_mbx_resp_status(hdev);
68 
69 	if (!(r_code0 == code0 && r_code1 == code1 && !mbx_resp->resp_status)) {
70 		dev_err(&hdev->pdev->dev,
71 			"VF could not match resp code(code0=%d,code1=%d), %d\n",
72 			code0, code1, mbx_resp->resp_status);
73 		dev_err(&hdev->pdev->dev,
74 			"VF could not match resp r_code(r_code0=%d,r_code1=%d)\n",
75 			r_code0, r_code1);
76 		return -EIO;
77 	}
78 
79 	return 0;
80 }
81 
82 int hclgevf_send_mbx_msg(struct hclgevf_dev *hdev, u16 code, u16 subcode,
83 			 const u8 *msg_data, u8 msg_len, bool need_resp,
84 			 u8 *resp_data, u16 resp_len)
85 {
86 	struct hclge_mbx_vf_to_pf_cmd *req;
87 	struct hclgevf_desc desc;
88 	int status;
89 
90 	req = (struct hclge_mbx_vf_to_pf_cmd *)desc.data;
91 
92 	/* first two bytes are reserved for code & subcode */
93 	if (msg_len > (HCLGE_MBX_MAX_MSG_SIZE - 2)) {
94 		dev_err(&hdev->pdev->dev,
95 			"VF send mbx msg fail, msg len %d exceeds max len %d\n",
96 			msg_len, HCLGE_MBX_MAX_MSG_SIZE);
97 		return -EINVAL;
98 	}
99 
100 	hclgevf_cmd_setup_basic_desc(&desc, HCLGEVF_OPC_MBX_VF_TO_PF, false);
101 	req->mbx_need_resp |= need_resp ? HCLGE_MBX_NEED_RESP_BIT :
102 					  ~HCLGE_MBX_NEED_RESP_BIT;
103 	req->msg[0] = code;
104 	req->msg[1] = subcode;
105 	if (msg_data)
106 		memcpy(&req->msg[2], msg_data, msg_len);
107 
108 	/* synchronous send */
109 	if (need_resp) {
110 		mutex_lock(&hdev->mbx_resp.mbx_mutex);
111 		hclgevf_reset_mbx_resp_status(hdev);
112 		status = hclgevf_cmd_send(&hdev->hw, &desc, 1);
113 		if (status) {
114 			dev_err(&hdev->pdev->dev,
115 				"VF failed(=%d) to send mbx message to PF\n",
116 				status);
117 			mutex_unlock(&hdev->mbx_resp.mbx_mutex);
118 			return status;
119 		}
120 
121 		status = hclgevf_get_mbx_resp(hdev, code, subcode, resp_data,
122 					      resp_len);
123 		mutex_unlock(&hdev->mbx_resp.mbx_mutex);
124 	} else {
125 		/* asynchronous send */
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 			return status;
132 		}
133 	}
134 
135 	return status;
136 }
137 
138 static bool hclgevf_cmd_crq_empty(struct hclgevf_hw *hw)
139 {
140 	u32 tail = hclgevf_read_dev(hw, HCLGEVF_NIC_CRQ_TAIL_REG);
141 
142 	return tail == hw->cmq.crq.next_to_use;
143 }
144 
145 void hclgevf_mbx_handler(struct hclgevf_dev *hdev)
146 {
147 	struct hclgevf_mbx_resp_status *resp;
148 	struct hclge_mbx_pf_to_vf_cmd *req;
149 	struct hclgevf_cmq_ring *crq;
150 	struct hclgevf_desc *desc;
151 	u16 *msg_q;
152 	u16 flag;
153 	u8 *temp;
154 	int i;
155 
156 	resp = &hdev->mbx_resp;
157 	crq = &hdev->hw.cmq.crq;
158 
159 	while (!hclgevf_cmd_crq_empty(&hdev->hw)) {
160 		if (test_bit(HCLGEVF_STATE_CMD_DISABLE, &hdev->state)) {
161 			dev_info(&hdev->pdev->dev, "vf crq need init\n");
162 			return;
163 		}
164 
165 		desc = &crq->desc[crq->next_to_use];
166 		req = (struct hclge_mbx_pf_to_vf_cmd *)desc->data;
167 
168 		flag = le16_to_cpu(crq->desc[crq->next_to_use].flag);
169 		if (unlikely(!hnae3_get_bit(flag, HCLGEVF_CMDQ_RX_OUTVLD_B))) {
170 			dev_warn(&hdev->pdev->dev,
171 				 "dropped invalid mailbox message, code = %d\n",
172 				 req->msg[0]);
173 
174 			/* dropping/not processing this invalid message */
175 			crq->desc[crq->next_to_use].flag = 0;
176 			hclge_mbx_ring_ptr_move_crq(crq);
177 			continue;
178 		}
179 
180 		/* synchronous messages are time critical and need preferential
181 		 * treatment. Therefore, we need to acknowledge all the sync
182 		 * responses as quickly as possible so that waiting tasks do not
183 		 * timeout and simultaneously queue the async messages for later
184 		 * prcessing in context of mailbox task i.e. the slow path.
185 		 */
186 		switch (req->msg[0]) {
187 		case HCLGE_MBX_PF_VF_RESP:
188 			if (resp->received_resp)
189 				dev_warn(&hdev->pdev->dev,
190 					 "VF mbx resp flag not clear(%d)\n",
191 					 req->msg[1]);
192 			resp->received_resp = true;
193 
194 			resp->origin_mbx_msg = (req->msg[1] << 16);
195 			resp->origin_mbx_msg |= req->msg[2];
196 			resp->resp_status = req->msg[3];
197 
198 			temp = (u8 *)&req->msg[4];
199 			for (i = 0; i < HCLGE_MBX_MAX_RESP_DATA_SIZE; i++) {
200 				resp->additional_info[i] = *temp;
201 				temp++;
202 			}
203 			break;
204 		case HCLGE_MBX_LINK_STAT_CHANGE:
205 		case HCLGE_MBX_ASSERTING_RESET:
206 		case HCLGE_MBX_LINK_STAT_MODE:
207 		case HCLGE_MBX_PUSH_VLAN_INFO:
208 			/* set this mbx event as pending. This is required as we
209 			 * might loose interrupt event when mbx task is busy
210 			 * handling. This shall be cleared when mbx task just
211 			 * enters handling state.
212 			 */
213 			hdev->mbx_event_pending = true;
214 
215 			/* we will drop the async msg if we find ARQ as full
216 			 * and continue with next message
217 			 */
218 			if (atomic_read(&hdev->arq.count) >=
219 			    HCLGE_MBX_MAX_ARQ_MSG_NUM) {
220 				dev_warn(&hdev->pdev->dev,
221 					 "Async Q full, dropping msg(%d)\n",
222 					 req->msg[1]);
223 				break;
224 			}
225 
226 			/* tail the async message in arq */
227 			msg_q = hdev->arq.msg_q[hdev->arq.tail];
228 			memcpy(&msg_q[0], req->msg,
229 			       HCLGE_MBX_MAX_ARQ_MSG_SIZE * sizeof(u16));
230 			hclge_mbx_tail_ptr_move_arq(hdev->arq);
231 			atomic_inc(&hdev->arq.count);
232 
233 			hclgevf_mbx_task_schedule(hdev);
234 
235 			break;
236 		default:
237 			dev_err(&hdev->pdev->dev,
238 				"VF received unsupported(%d) mbx msg from PF\n",
239 				req->msg[0]);
240 			break;
241 		}
242 		crq->desc[crq->next_to_use].flag = 0;
243 		hclge_mbx_ring_ptr_move_crq(crq);
244 	}
245 
246 	/* Write back CMDQ_RQ header pointer, M7 need this pointer */
247 	hclgevf_write_dev(&hdev->hw, HCLGEVF_NIC_CRQ_HEAD_REG,
248 			  crq->next_to_use);
249 }
250 
251 void hclgevf_mbx_async_handler(struct hclgevf_dev *hdev)
252 {
253 	enum hnae3_reset_type reset_type;
254 	u16 link_status, state;
255 	u16 *msg_q, *vlan_info;
256 	u8 duplex;
257 	u32 speed;
258 	u32 tail;
259 	u8 idx;
260 
261 	/* we can safely clear it now as we are at start of the async message
262 	 * processing
263 	 */
264 	hdev->mbx_event_pending = false;
265 
266 	tail = hdev->arq.tail;
267 
268 	/* process all the async queue messages */
269 	while (tail != hdev->arq.head) {
270 		if (test_bit(HCLGEVF_STATE_CMD_DISABLE, &hdev->state)) {
271 			dev_info(&hdev->pdev->dev,
272 				 "vf crq need init in async\n");
273 			return;
274 		}
275 
276 		msg_q = hdev->arq.msg_q[hdev->arq.head];
277 
278 		switch (msg_q[0]) {
279 		case HCLGE_MBX_LINK_STAT_CHANGE:
280 			link_status = le16_to_cpu(msg_q[1]);
281 			memcpy(&speed, &msg_q[2], sizeof(speed));
282 			duplex = (u8)le16_to_cpu(msg_q[4]);
283 
284 			/* update upper layer with new link link status */
285 			hclgevf_update_link_status(hdev, link_status);
286 			hclgevf_update_speed_duplex(hdev, speed, duplex);
287 
288 			break;
289 		case HCLGE_MBX_LINK_STAT_MODE:
290 			idx = (u8)le16_to_cpu(msg_q[1]);
291 			if (idx)
292 				memcpy(&hdev->hw.mac.supported, &msg_q[2],
293 				       sizeof(unsigned long));
294 			else
295 				memcpy(&hdev->hw.mac.advertising, &msg_q[2],
296 				       sizeof(unsigned long));
297 			break;
298 		case HCLGE_MBX_ASSERTING_RESET:
299 			/* PF has asserted reset hence VF should go in pending
300 			 * state and poll for the hardware reset status till it
301 			 * has been completely reset. After this stack should
302 			 * eventually be re-initialized.
303 			 */
304 			reset_type = le16_to_cpu(msg_q[1]);
305 			set_bit(reset_type, &hdev->reset_pending);
306 			set_bit(HCLGEVF_RESET_PENDING, &hdev->reset_state);
307 			hclgevf_reset_task_schedule(hdev);
308 
309 			break;
310 		case HCLGE_MBX_PUSH_VLAN_INFO:
311 			state = le16_to_cpu(msg_q[1]);
312 			vlan_info = &msg_q[1];
313 			hclgevf_update_port_base_vlan_info(hdev, state,
314 							   (u8 *)vlan_info, 8);
315 			break;
316 		default:
317 			dev_err(&hdev->pdev->dev,
318 				"fetched unsupported(%d) message from arq\n",
319 				msg_q[0]);
320 			break;
321 		}
322 
323 		hclge_mbx_head_ptr_move_arq(hdev->arq);
324 		atomic_dec(&hdev->arq.count);
325 		msg_q = hdev->arq.msg_q[hdev->arq.head];
326 	}
327 }
328