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