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_USCOEND 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 udelay(HCLGEVF_SLEEP_USCOEND); 44 i++; 45 } 46 47 if (i >= HCLGEVF_MAX_TRY_TIMES) { 48 dev_err(&hdev->pdev->dev, 49 "VF could not get mbx resp(=%d) from PF in %d tries\n", 50 hdev->mbx_resp.received_resp, i); 51 return -EIO; 52 } 53 54 mbx_resp = &hdev->mbx_resp; 55 r_code0 = (u16)(mbx_resp->origin_mbx_msg >> 16); 56 r_code1 = (u16)(mbx_resp->origin_mbx_msg & 0xff); 57 58 if (mbx_resp->resp_status) 59 return mbx_resp->resp_status; 60 61 if (resp_data) 62 memcpy(resp_data, &mbx_resp->additional_info[0], resp_len); 63 64 hclgevf_reset_mbx_resp_status(hdev); 65 66 if (!(r_code0 == code0 && r_code1 == code1 && !mbx_resp->resp_status)) { 67 dev_err(&hdev->pdev->dev, 68 "VF could not match resp code(code0=%d,code1=%d), %d", 69 code0, code1, mbx_resp->resp_status); 70 return -EIO; 71 } 72 73 return 0; 74 } 75 76 int hclgevf_send_mbx_msg(struct hclgevf_dev *hdev, u16 code, u16 subcode, 77 const u8 *msg_data, u8 msg_len, bool need_resp, 78 u8 *resp_data, u16 resp_len) 79 { 80 struct hclge_mbx_vf_to_pf_cmd *req; 81 struct hclgevf_desc desc; 82 int status; 83 84 req = (struct hclge_mbx_vf_to_pf_cmd *)desc.data; 85 86 /* first two bytes are reserved for code & subcode */ 87 if (msg_len > (HCLGE_MBX_MAX_MSG_SIZE - 2)) { 88 dev_err(&hdev->pdev->dev, 89 "VF send mbx msg fail, msg len %d exceeds max len %d\n", 90 msg_len, HCLGE_MBX_MAX_MSG_SIZE); 91 return -EINVAL; 92 } 93 94 hclgevf_cmd_setup_basic_desc(&desc, HCLGEVF_OPC_MBX_VF_TO_PF, false); 95 req->msg[0] = code; 96 req->msg[1] = subcode; 97 memcpy(&req->msg[2], msg_data, msg_len); 98 99 /* synchronous send */ 100 if (need_resp) { 101 mutex_lock(&hdev->mbx_resp.mbx_mutex); 102 hclgevf_reset_mbx_resp_status(hdev); 103 status = hclgevf_cmd_send(&hdev->hw, &desc, 1); 104 if (status) { 105 dev_err(&hdev->pdev->dev, 106 "VF failed(=%d) to send mbx message to PF\n", 107 status); 108 mutex_unlock(&hdev->mbx_resp.mbx_mutex); 109 return status; 110 } 111 112 status = hclgevf_get_mbx_resp(hdev, code, subcode, resp_data, 113 resp_len); 114 mutex_unlock(&hdev->mbx_resp.mbx_mutex); 115 } else { 116 /* asynchronous send */ 117 status = hclgevf_cmd_send(&hdev->hw, &desc, 1); 118 if (status) { 119 dev_err(&hdev->pdev->dev, 120 "VF failed(=%d) to send mbx message to PF\n", 121 status); 122 return status; 123 } 124 } 125 126 return status; 127 } 128 129 void hclgevf_mbx_handler(struct hclgevf_dev *hdev) 130 { 131 struct hclgevf_mbx_resp_status *resp; 132 struct hclge_mbx_pf_to_vf_cmd *req; 133 struct hclgevf_cmq_ring *crq; 134 struct hclgevf_desc *desc; 135 u16 *msg_q; 136 u16 flag; 137 u8 *temp; 138 int i; 139 140 resp = &hdev->mbx_resp; 141 crq = &hdev->hw.cmq.crq; 142 143 flag = le16_to_cpu(crq->desc[crq->next_to_use].flag); 144 while (hnae_get_bit(flag, HCLGEVF_CMDQ_RX_OUTVLD_B)) { 145 desc = &crq->desc[crq->next_to_use]; 146 req = (struct hclge_mbx_pf_to_vf_cmd *)desc->data; 147 148 /* synchronous messages are time critical and need preferential 149 * treatment. Therefore, we need to acknowledge all the sync 150 * responses as quickly as possible so that waiting tasks do not 151 * timeout and simultaneously queue the async messages for later 152 * prcessing in context of mailbox task i.e. the slow path. 153 */ 154 switch (req->msg[0]) { 155 case HCLGE_MBX_PF_VF_RESP: 156 if (resp->received_resp) 157 dev_warn(&hdev->pdev->dev, 158 "VF mbx resp flag not clear(%d)\n", 159 req->msg[1]); 160 resp->received_resp = true; 161 162 resp->origin_mbx_msg = (req->msg[1] << 16); 163 resp->origin_mbx_msg |= req->msg[2]; 164 resp->resp_status = req->msg[3]; 165 166 temp = (u8 *)&req->msg[4]; 167 for (i = 0; i < HCLGE_MBX_MAX_RESP_DATA_SIZE; i++) { 168 resp->additional_info[i] = *temp; 169 temp++; 170 } 171 break; 172 case HCLGE_MBX_LINK_STAT_CHANGE: 173 case HCLGE_MBX_ASSERTING_RESET: 174 /* set this mbx event as pending. This is required as we 175 * might loose interrupt event when mbx task is busy 176 * handling. This shall be cleared when mbx task just 177 * enters handling state. 178 */ 179 hdev->mbx_event_pending = true; 180 181 /* we will drop the async msg if we find ARQ as full 182 * and continue with next message 183 */ 184 if (hdev->arq.count >= HCLGE_MBX_MAX_ARQ_MSG_NUM) { 185 dev_warn(&hdev->pdev->dev, 186 "Async Q full, dropping msg(%d)\n", 187 req->msg[1]); 188 break; 189 } 190 191 /* tail the async message in arq */ 192 msg_q = hdev->arq.msg_q[hdev->arq.tail]; 193 memcpy(&msg_q[0], req->msg, HCLGE_MBX_MAX_ARQ_MSG_SIZE); 194 hclge_mbx_tail_ptr_move_arq(hdev->arq); 195 hdev->arq.count++; 196 197 hclgevf_mbx_task_schedule(hdev); 198 199 break; 200 default: 201 dev_err(&hdev->pdev->dev, 202 "VF received unsupported(%d) mbx msg from PF\n", 203 req->msg[0]); 204 break; 205 } 206 crq->desc[crq->next_to_use].flag = 0; 207 hclge_mbx_ring_ptr_move_crq(crq); 208 flag = le16_to_cpu(crq->desc[crq->next_to_use].flag); 209 } 210 211 /* Write back CMDQ_RQ header pointer, M7 need this pointer */ 212 hclgevf_write_dev(&hdev->hw, HCLGEVF_NIC_CRQ_HEAD_REG, 213 crq->next_to_use); 214 } 215 216 void hclgevf_mbx_async_handler(struct hclgevf_dev *hdev) 217 { 218 u16 link_status; 219 u16 *msg_q; 220 u8 duplex; 221 u32 speed; 222 u32 tail; 223 224 /* we can safely clear it now as we are at start of the async message 225 * processing 226 */ 227 hdev->mbx_event_pending = false; 228 229 tail = hdev->arq.tail; 230 231 /* process all the async queue messages */ 232 while (tail != hdev->arq.head) { 233 msg_q = hdev->arq.msg_q[hdev->arq.head]; 234 235 switch (msg_q[0]) { 236 case HCLGE_MBX_LINK_STAT_CHANGE: 237 link_status = le16_to_cpu(msg_q[1]); 238 memcpy(&speed, &msg_q[2], sizeof(speed)); 239 duplex = (u8)le16_to_cpu(msg_q[4]); 240 241 /* update upper layer with new link link status */ 242 hclgevf_update_link_status(hdev, link_status); 243 hclgevf_update_speed_duplex(hdev, speed, duplex); 244 245 break; 246 case HCLGE_MBX_ASSERTING_RESET: 247 /* PF has asserted reset hence VF should go in pending 248 * state and poll for the hardware reset status till it 249 * has been completely reset. After this stack should 250 * eventually be re-initialized. 251 */ 252 hdev->nic.reset_level = HNAE3_VF_RESET; 253 set_bit(HCLGEVF_RESET_PENDING, &hdev->reset_state); 254 hclgevf_reset_task_schedule(hdev); 255 256 break; 257 default: 258 dev_err(&hdev->pdev->dev, 259 "fetched unsupported(%d) message from arq\n", 260 msg_q[0]); 261 break; 262 } 263 264 hclge_mbx_head_ptr_move_arq(hdev->arq); 265 hdev->arq.count--; 266 msg_q = hdev->arq.msg_q[hdev->arq.head]; 267 } 268 } 269