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