1 // SPDX-License-Identifier: GPL-2.0+
2 // Copyright (c) 2016-2017 Hisilicon Limited.
5 #include "hclgevf_main.h"
8 #define CREATE_TRACE_POINTS
9 #include "hclgevf_trace.h"
11 static int hclgevf_resp_to_errno(u16 resp_code)
13 return resp_code ? -resp_code : 0;
16 #define HCLGEVF_MBX_MATCH_ID_START 1
17 static void hclgevf_reset_mbx_resp_status(struct hclgevf_dev *hdev)
19 /* this function should be called with mbx_resp.mbx_mutex held
20 * to prtect the received_response from race condition
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);
32 /* hclgevf_get_mbx_resp: used to get a response from PF after VF sends a mailbox
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.
38 static int hclgevf_get_mbx_resp(struct hclgevf_dev *hdev, u16 code0, u16 code1,
39 u8 *resp_data, u16 resp_len)
41 #define HCLGEVF_MAX_TRY_TIMES 500
42 #define HCLGEVF_SLEEP_USECOND 1000
43 struct hclgevf_mbx_resp_status *mbx_resp;
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",
51 HCLGE_MBX_MAX_RESP_DATA_SIZE);
55 while ((!hdev->mbx_resp.received_resp) && (i < HCLGEVF_MAX_TRY_TIMES)) {
56 if (test_bit(HCLGEVF_STATE_CMD_DISABLE, &hdev->state))
59 usleep_range(HCLGEVF_SLEEP_USECOND, HCLGEVF_SLEEP_USECOND * 2);
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);
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);
74 if (mbx_resp->resp_status)
75 return mbx_resp->resp_status;
78 memcpy(resp_data, &mbx_resp->additional_info[0], resp_len);
80 hclgevf_reset_mbx_resp_status(hdev);
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",
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)
99 struct hclge_mbx_vf_to_pf_cmd *req;
100 struct hclgevf_desc desc;
103 req = (struct hclge_mbx_vf_to_pf_cmd *)desc.data;
106 dev_err(&hdev->pdev->dev,
107 "failed to send mbx, msg is NULL\n");
111 hclgevf_cmd_setup_basic_desc(&desc, HCLGEVF_OPC_MBX_VF_TO_PF, false);
113 hnae3_set_bit(req->mbx_need_resp, HCLGE_MBX_NEED_RESP_B, 1);
115 memcpy(&req->msg, send_msg, sizeof(struct hclge_vf_to_pf_msg));
117 trace_hclge_vf_mbx_send(hdev, req);
119 /* synchronous send */
121 mutex_lock(&hdev->mbx_resp.mbx_mutex);
122 hclgevf_reset_mbx_resp_status(hdev);
123 req->match_id = hdev->mbx_resp.match_id;
124 status = hclgevf_cmd_send(&hdev->hw, &desc, 1);
126 dev_err(&hdev->pdev->dev,
127 "VF failed(=%d) to send mbx message to PF\n",
129 mutex_unlock(&hdev->mbx_resp.mbx_mutex);
133 status = hclgevf_get_mbx_resp(hdev, send_msg->code,
134 send_msg->subcode, resp_data,
136 mutex_unlock(&hdev->mbx_resp.mbx_mutex);
138 /* asynchronous send */
139 status = hclgevf_cmd_send(&hdev->hw, &desc, 1);
141 dev_err(&hdev->pdev->dev,
142 "VF failed(=%d) to send mbx message to PF\n",
151 static bool hclgevf_cmd_crq_empty(struct hclgevf_hw *hw)
153 u32 tail = hclgevf_read_dev(hw, HCLGEVF_NIC_CRQ_TAIL_REG);
155 return tail == hw->cmq.crq.next_to_use;
158 void hclgevf_mbx_handler(struct hclgevf_dev *hdev)
160 struct hclgevf_mbx_resp_status *resp;
161 struct hclge_mbx_pf_to_vf_cmd *req;
162 struct hclgevf_cmq_ring *crq;
163 struct hclgevf_desc *desc;
169 resp = &hdev->mbx_resp;
170 crq = &hdev->hw.cmq.crq;
172 while (!hclgevf_cmd_crq_empty(&hdev->hw)) {
173 if (test_bit(HCLGEVF_STATE_CMD_DISABLE, &hdev->state)) {
174 dev_info(&hdev->pdev->dev, "vf crq need init\n");
178 desc = &crq->desc[crq->next_to_use];
179 req = (struct hclge_mbx_pf_to_vf_cmd *)desc->data;
181 flag = le16_to_cpu(crq->desc[crq->next_to_use].flag);
182 if (unlikely(!hnae3_get_bit(flag, HCLGEVF_CMDQ_RX_OUTVLD_B))) {
183 dev_warn(&hdev->pdev->dev,
184 "dropped invalid mailbox message, code = %u\n",
187 /* dropping/not processing this invalid message */
188 crq->desc[crq->next_to_use].flag = 0;
189 hclge_mbx_ring_ptr_move_crq(crq);
193 trace_hclge_vf_mbx_get(hdev, req);
195 /* synchronous messages are time critical and need preferential
196 * treatment. Therefore, we need to acknowledge all the sync
197 * responses as quickly as possible so that waiting tasks do not
198 * timeout and simultaneously queue the async messages for later
199 * prcessing in context of mailbox task i.e. the slow path.
201 switch (req->msg.code) {
202 case HCLGE_MBX_PF_VF_RESP:
203 if (resp->received_resp)
204 dev_warn(&hdev->pdev->dev,
205 "VF mbx resp flag not clear(%u)\n",
206 req->msg.vf_mbx_msg_code);
207 resp->received_resp = true;
209 resp->origin_mbx_msg =
210 (req->msg.vf_mbx_msg_code << 16);
211 resp->origin_mbx_msg |= req->msg.vf_mbx_msg_subcode;
213 hclgevf_resp_to_errno(req->msg.resp_status);
215 temp = (u8 *)req->msg.resp_data;
216 for (i = 0; i < HCLGE_MBX_MAX_RESP_DATA_SIZE; i++) {
217 resp->additional_info[i] = *temp;
221 /* If match_id is not zero, it means PF support
222 * match_id. If the match_id is right, VF get the
223 * right response, otherwise ignore the response.
224 * Driver will clear hdev->mbx_resp when send
225 * next message which need response.
228 if (req->match_id == resp->match_id)
229 resp->received_resp = true;
231 resp->received_resp = true;
234 case HCLGE_MBX_LINK_STAT_CHANGE:
235 case HCLGE_MBX_ASSERTING_RESET:
236 case HCLGE_MBX_LINK_STAT_MODE:
237 case HCLGE_MBX_PUSH_VLAN_INFO:
238 case HCLGE_MBX_PUSH_PROMISC_INFO:
239 /* set this mbx event as pending. This is required as we
240 * might loose interrupt event when mbx task is busy
241 * handling. This shall be cleared when mbx task just
242 * enters handling state.
244 hdev->mbx_event_pending = true;
246 /* we will drop the async msg if we find ARQ as full
247 * and continue with next message
249 if (atomic_read(&hdev->arq.count) >=
250 HCLGE_MBX_MAX_ARQ_MSG_NUM) {
251 dev_warn(&hdev->pdev->dev,
252 "Async Q full, dropping msg(%u)\n",
257 /* tail the async message in arq */
258 msg_q = hdev->arq.msg_q[hdev->arq.tail];
259 memcpy(&msg_q[0], &req->msg,
260 HCLGE_MBX_MAX_ARQ_MSG_SIZE * sizeof(u16));
261 hclge_mbx_tail_ptr_move_arq(hdev->arq);
262 atomic_inc(&hdev->arq.count);
264 hclgevf_mbx_task_schedule(hdev);
268 dev_err(&hdev->pdev->dev,
269 "VF received unsupported(%u) mbx msg from PF\n",
273 crq->desc[crq->next_to_use].flag = 0;
274 hclge_mbx_ring_ptr_move_crq(crq);
277 /* Write back CMDQ_RQ header pointer, M7 need this pointer */
278 hclgevf_write_dev(&hdev->hw, HCLGEVF_NIC_CRQ_HEAD_REG,
282 static void hclgevf_parse_promisc_info(struct hclgevf_dev *hdev,
286 dev_info(&hdev->pdev->dev,
287 "Promisc mode is closed by host for being untrusted.\n");
290 void hclgevf_mbx_async_handler(struct hclgevf_dev *hdev)
292 enum hnae3_reset_type reset_type;
293 u16 link_status, state;
294 u16 *msg_q, *vlan_info;
301 /* we can safely clear it now as we are at start of the async message
304 hdev->mbx_event_pending = false;
306 tail = hdev->arq.tail;
308 /* process all the async queue messages */
309 while (tail != hdev->arq.head) {
310 if (test_bit(HCLGEVF_STATE_CMD_DISABLE, &hdev->state)) {
311 dev_info(&hdev->pdev->dev,
312 "vf crq need init in async\n");
316 msg_q = hdev->arq.msg_q[hdev->arq.head];
319 case HCLGE_MBX_LINK_STAT_CHANGE:
320 link_status = msg_q[1];
321 memcpy(&speed, &msg_q[2], sizeof(speed));
322 duplex = (u8)msg_q[4];
325 /* update upper layer with new link link status */
326 hclgevf_update_link_status(hdev, link_status);
327 hclgevf_update_speed_duplex(hdev, speed, duplex);
329 if (flag & HCLGE_MBX_PUSH_LINK_STATUS_EN)
330 set_bit(HCLGEVF_STATE_PF_PUSH_LINK_STATUS,
334 case HCLGE_MBX_LINK_STAT_MODE:
337 memcpy(&hdev->hw.mac.supported, &msg_q[2],
338 sizeof(unsigned long));
340 memcpy(&hdev->hw.mac.advertising, &msg_q[2],
341 sizeof(unsigned long));
343 case HCLGE_MBX_ASSERTING_RESET:
344 /* PF has asserted reset hence VF should go in pending
345 * state and poll for the hardware reset status till it
346 * has been completely reset. After this stack should
347 * eventually be re-initialized.
349 reset_type = (enum hnae3_reset_type)msg_q[1];
350 set_bit(reset_type, &hdev->reset_pending);
351 set_bit(HCLGEVF_RESET_PENDING, &hdev->reset_state);
352 hclgevf_reset_task_schedule(hdev);
355 case HCLGE_MBX_PUSH_VLAN_INFO:
357 vlan_info = &msg_q[1];
358 hclgevf_update_port_base_vlan_info(hdev, state,
361 case HCLGE_MBX_PUSH_PROMISC_INFO:
362 hclgevf_parse_promisc_info(hdev, msg_q[1]);
365 dev_err(&hdev->pdev->dev,
366 "fetched unsupported(%u) message from arq\n",
371 hclge_mbx_head_ptr_move_arq(hdev->arq);
372 atomic_dec(&hdev->arq.count);
373 msg_q = hdev->arq.msg_q[hdev->arq.head];