1 // SPDX-License-Identifier: GPL-2.0+ 2 // Copyright (c) 2016-2017 Hisilicon Limited. 3 4 #include "hclge_main.h" 5 #include "hclge_mbx.h" 6 #include "hnae3.h" 7 8 /* hclge_gen_resp_to_vf: used to generate a synchronous response to VF when PF 9 * receives a mailbox message from VF. 10 * @vport: pointer to struct hclge_vport 11 * @vf_to_pf_req: pointer to hclge_mbx_vf_to_pf_cmd of the original mailbox 12 * message 13 * @resp_status: indicate to VF whether its request success(0) or failed. 14 */ 15 static int hclge_gen_resp_to_vf(struct hclge_vport *vport, 16 struct hclge_mbx_vf_to_pf_cmd *vf_to_pf_req, 17 int resp_status, 18 u8 *resp_data, u16 resp_data_len) 19 { 20 struct hclge_mbx_pf_to_vf_cmd *resp_pf_to_vf; 21 struct hclge_dev *hdev = vport->back; 22 enum hclge_cmd_status status; 23 struct hclge_desc desc; 24 25 resp_pf_to_vf = (struct hclge_mbx_pf_to_vf_cmd *)desc.data; 26 27 if (resp_data_len > HCLGE_MBX_MAX_RESP_DATA_SIZE) { 28 dev_err(&hdev->pdev->dev, 29 "PF fail to gen resp to VF len %d exceeds max len %d\n", 30 resp_data_len, 31 HCLGE_MBX_MAX_RESP_DATA_SIZE); 32 } 33 34 hclge_cmd_setup_basic_desc(&desc, HCLGEVF_OPC_MBX_PF_TO_VF, false); 35 36 resp_pf_to_vf->dest_vfid = vf_to_pf_req->mbx_src_vfid; 37 resp_pf_to_vf->msg_len = vf_to_pf_req->msg_len; 38 39 resp_pf_to_vf->msg[0] = HCLGE_MBX_PF_VF_RESP; 40 resp_pf_to_vf->msg[1] = vf_to_pf_req->msg[0]; 41 resp_pf_to_vf->msg[2] = vf_to_pf_req->msg[1]; 42 resp_pf_to_vf->msg[3] = (resp_status == 0) ? 0 : 1; 43 44 if (resp_data && resp_data_len > 0) 45 memcpy(&resp_pf_to_vf->msg[4], resp_data, resp_data_len); 46 47 status = hclge_cmd_send(&hdev->hw, &desc, 1); 48 if (status) 49 dev_err(&hdev->pdev->dev, 50 "PF failed(=%d) to send response to VF\n", status); 51 52 return status; 53 } 54 55 static int hclge_send_mbx_msg(struct hclge_vport *vport, u8 *msg, u16 msg_len, 56 u16 mbx_opcode, u8 dest_vfid) 57 { 58 struct hclge_mbx_pf_to_vf_cmd *resp_pf_to_vf; 59 struct hclge_dev *hdev = vport->back; 60 enum hclge_cmd_status status; 61 struct hclge_desc desc; 62 63 resp_pf_to_vf = (struct hclge_mbx_pf_to_vf_cmd *)desc.data; 64 65 hclge_cmd_setup_basic_desc(&desc, HCLGEVF_OPC_MBX_PF_TO_VF, false); 66 67 resp_pf_to_vf->dest_vfid = dest_vfid; 68 resp_pf_to_vf->msg_len = msg_len; 69 resp_pf_to_vf->msg[0] = mbx_opcode; 70 71 memcpy(&resp_pf_to_vf->msg[1], msg, msg_len); 72 73 status = hclge_cmd_send(&hdev->hw, &desc, 1); 74 if (status) 75 dev_err(&hdev->pdev->dev, 76 "PF failed(=%d) to send mailbox message to VF\n", 77 status); 78 79 return status; 80 } 81 82 static void hclge_free_vector_ring_chain(struct hnae3_ring_chain_node *head) 83 { 84 struct hnae3_ring_chain_node *chain_tmp, *chain; 85 86 chain = head->next; 87 88 while (chain) { 89 chain_tmp = chain->next; 90 kzfree(chain); 91 chain = chain_tmp; 92 } 93 } 94 95 /* hclge_get_ring_chain_from_mbx: get ring type & tqpid from mailbox message 96 * msg[0]: opcode 97 * msg[1]: <not relevant to this function> 98 * msg[2]: ring_num 99 * msg[3]: first ring type (TX|RX) 100 * msg[4]: first tqp id 101 * msg[5] ~ msg[14]: other ring type and tqp id 102 */ 103 static int hclge_get_ring_chain_from_mbx( 104 struct hclge_mbx_vf_to_pf_cmd *req, 105 struct hnae3_ring_chain_node *ring_chain, 106 struct hclge_vport *vport) 107 { 108 #define HCLGE_RING_NODE_VARIABLE_NUM 3 109 #define HCLGE_RING_MAP_MBX_BASIC_MSG_NUM 3 110 struct hnae3_ring_chain_node *cur_chain, *new_chain; 111 int ring_num; 112 int i; 113 114 ring_num = req->msg[2]; 115 116 hnae_set_bit(ring_chain->flag, HNAE3_RING_TYPE_B, req->msg[3]); 117 ring_chain->tqp_index = 118 hclge_get_queue_id(vport->nic.kinfo.tqp[req->msg[4]]); 119 hnae_set_field(ring_chain->int_gl_idx, HCLGE_INT_GL_IDX_M, 120 HCLGE_INT_GL_IDX_S, 121 req->msg[5]); 122 123 cur_chain = ring_chain; 124 125 for (i = 1; i < ring_num; i++) { 126 new_chain = kzalloc(sizeof(*new_chain), GFP_KERNEL); 127 if (!new_chain) 128 goto err; 129 130 hnae_set_bit(new_chain->flag, HNAE3_RING_TYPE_B, 131 req->msg[HCLGE_RING_NODE_VARIABLE_NUM * i + 132 HCLGE_RING_MAP_MBX_BASIC_MSG_NUM]); 133 134 new_chain->tqp_index = 135 hclge_get_queue_id(vport->nic.kinfo.tqp 136 [req->msg[HCLGE_RING_NODE_VARIABLE_NUM * i + 137 HCLGE_RING_MAP_MBX_BASIC_MSG_NUM + 1]]); 138 139 hnae_set_field(new_chain->int_gl_idx, HCLGE_INT_GL_IDX_M, 140 HCLGE_INT_GL_IDX_S, 141 req->msg[HCLGE_RING_NODE_VARIABLE_NUM * i + 142 HCLGE_RING_MAP_MBX_BASIC_MSG_NUM + 2]); 143 144 cur_chain->next = new_chain; 145 cur_chain = new_chain; 146 } 147 148 return 0; 149 err: 150 hclge_free_vector_ring_chain(ring_chain); 151 return -ENOMEM; 152 } 153 154 static int hclge_map_unmap_ring_to_vf_vector(struct hclge_vport *vport, bool en, 155 struct hclge_mbx_vf_to_pf_cmd *req) 156 { 157 struct hnae3_ring_chain_node ring_chain; 158 int vector_id = req->msg[1]; 159 int ret; 160 161 memset(&ring_chain, 0, sizeof(ring_chain)); 162 ret = hclge_get_ring_chain_from_mbx(req, &ring_chain, vport); 163 if (ret) 164 return ret; 165 166 ret = hclge_bind_ring_with_vector(vport, vector_id, en, &ring_chain); 167 if (ret) 168 return ret; 169 170 hclge_free_vector_ring_chain(&ring_chain); 171 172 return 0; 173 } 174 175 static int hclge_set_vf_promisc_mode(struct hclge_vport *vport, 176 struct hclge_mbx_vf_to_pf_cmd *req) 177 { 178 bool en = req->msg[1] ? true : false; 179 struct hclge_promisc_param param; 180 181 /* always enable broadcast promisc bit */ 182 hclge_promisc_param_init(¶m, en, en, true, vport->vport_id); 183 return hclge_cmd_set_promisc_mode(vport->back, ¶m); 184 } 185 186 static int hclge_set_vf_uc_mac_addr(struct hclge_vport *vport, 187 struct hclge_mbx_vf_to_pf_cmd *mbx_req, 188 bool gen_resp) 189 { 190 const u8 *mac_addr = (const u8 *)(&mbx_req->msg[2]); 191 struct hclge_dev *hdev = vport->back; 192 int status; 193 194 if (mbx_req->msg[1] == HCLGE_MBX_MAC_VLAN_UC_MODIFY) { 195 const u8 *old_addr = (const u8 *)(&mbx_req->msg[8]); 196 197 hclge_rm_uc_addr_common(vport, old_addr); 198 status = hclge_add_uc_addr_common(vport, mac_addr); 199 } else if (mbx_req->msg[1] == HCLGE_MBX_MAC_VLAN_UC_ADD) { 200 status = hclge_add_uc_addr_common(vport, mac_addr); 201 } else if (mbx_req->msg[1] == HCLGE_MBX_MAC_VLAN_UC_REMOVE) { 202 status = hclge_rm_uc_addr_common(vport, mac_addr); 203 } else { 204 dev_err(&hdev->pdev->dev, 205 "failed to set unicast mac addr, unknown subcode %d\n", 206 mbx_req->msg[1]); 207 return -EIO; 208 } 209 210 if (gen_resp) 211 hclge_gen_resp_to_vf(vport, mbx_req, status, NULL, 0); 212 213 return 0; 214 } 215 216 static int hclge_set_vf_mc_mac_addr(struct hclge_vport *vport, 217 struct hclge_mbx_vf_to_pf_cmd *mbx_req, 218 bool gen_resp) 219 { 220 const u8 *mac_addr = (const u8 *)(&mbx_req->msg[2]); 221 struct hclge_dev *hdev = vport->back; 222 int status; 223 224 if (mbx_req->msg[1] == HCLGE_MBX_MAC_VLAN_MC_ADD) { 225 status = hclge_add_mc_addr_common(vport, mac_addr); 226 } else if (mbx_req->msg[1] == HCLGE_MBX_MAC_VLAN_MC_REMOVE) { 227 status = hclge_rm_mc_addr_common(vport, mac_addr); 228 } else if (mbx_req->msg[1] == HCLGE_MBX_MAC_VLAN_MC_FUNC_MTA_ENABLE) { 229 u8 func_id = vport->vport_id; 230 bool enable = mbx_req->msg[2]; 231 232 status = hclge_cfg_func_mta_filter(hdev, func_id, enable); 233 } else { 234 dev_err(&hdev->pdev->dev, 235 "failed to set mcast mac addr, unknown subcode %d\n", 236 mbx_req->msg[1]); 237 return -EIO; 238 } 239 240 if (gen_resp) 241 hclge_gen_resp_to_vf(vport, mbx_req, status, NULL, 0); 242 243 return 0; 244 } 245 246 static int hclge_set_vf_vlan_cfg(struct hclge_vport *vport, 247 struct hclge_mbx_vf_to_pf_cmd *mbx_req, 248 bool gen_resp) 249 { 250 struct hclge_dev *hdev = vport->back; 251 int status = 0; 252 253 if (mbx_req->msg[1] == HCLGE_MBX_VLAN_FILTER) { 254 u16 vlan, proto; 255 bool is_kill; 256 257 is_kill = !!mbx_req->msg[2]; 258 memcpy(&vlan, &mbx_req->msg[3], sizeof(vlan)); 259 memcpy(&proto, &mbx_req->msg[5], sizeof(proto)); 260 status = hclge_set_vf_vlan_common(hdev, vport->vport_id, 261 is_kill, vlan, 0, 262 cpu_to_be16(proto)); 263 } 264 265 if (gen_resp) 266 status = hclge_gen_resp_to_vf(vport, mbx_req, status, NULL, 0); 267 268 return status; 269 } 270 271 static int hclge_get_vf_tcinfo(struct hclge_vport *vport, 272 struct hclge_mbx_vf_to_pf_cmd *mbx_req, 273 bool gen_resp) 274 { 275 struct hclge_dev *hdev = vport->back; 276 int ret; 277 278 ret = hclge_gen_resp_to_vf(vport, mbx_req, 0, &hdev->hw_tc_map, 279 sizeof(u8)); 280 281 return ret; 282 } 283 284 static int hclge_get_vf_queue_info(struct hclge_vport *vport, 285 struct hclge_mbx_vf_to_pf_cmd *mbx_req, 286 bool gen_resp) 287 { 288 #define HCLGE_TQPS_RSS_INFO_LEN 8 289 u8 resp_data[HCLGE_TQPS_RSS_INFO_LEN]; 290 struct hclge_dev *hdev = vport->back; 291 292 /* get the queue related info */ 293 memcpy(&resp_data[0], &vport->alloc_tqps, sizeof(u16)); 294 memcpy(&resp_data[2], &hdev->rss_size_max, sizeof(u16)); 295 memcpy(&resp_data[4], &hdev->num_desc, sizeof(u16)); 296 memcpy(&resp_data[6], &hdev->rx_buf_len, sizeof(u16)); 297 298 return hclge_gen_resp_to_vf(vport, mbx_req, 0, resp_data, 299 HCLGE_TQPS_RSS_INFO_LEN); 300 } 301 302 static int hclge_get_link_info(struct hclge_vport *vport, 303 struct hclge_mbx_vf_to_pf_cmd *mbx_req) 304 { 305 struct hclge_dev *hdev = vport->back; 306 u16 link_status; 307 u8 msg_data[2]; 308 u8 dest_vfid; 309 310 /* mac.link can only be 0 or 1 */ 311 link_status = (u16)hdev->hw.mac.link; 312 memcpy(&msg_data[0], &link_status, sizeof(u16)); 313 dest_vfid = mbx_req->mbx_src_vfid; 314 315 /* send this requested info to VF */ 316 return hclge_send_mbx_msg(vport, msg_data, sizeof(u8), 317 HCLGE_MBX_LINK_STAT_CHANGE, dest_vfid); 318 } 319 320 static void hclge_reset_vf_queue(struct hclge_vport *vport, 321 struct hclge_mbx_vf_to_pf_cmd *mbx_req) 322 { 323 u16 queue_id; 324 325 memcpy(&queue_id, &mbx_req->msg[2], sizeof(queue_id)); 326 327 hclge_reset_tqp(&vport->nic, queue_id); 328 } 329 330 void hclge_mbx_handler(struct hclge_dev *hdev) 331 { 332 struct hclge_cmq_ring *crq = &hdev->hw.cmq.crq; 333 struct hclge_mbx_vf_to_pf_cmd *req; 334 struct hclge_vport *vport; 335 struct hclge_desc *desc; 336 int ret; 337 338 /* handle all the mailbox requests in the queue */ 339 while (hnae_get_bit(crq->desc[crq->next_to_use].flag, 340 HCLGE_CMDQ_RX_OUTVLD_B)) { 341 desc = &crq->desc[crq->next_to_use]; 342 req = (struct hclge_mbx_vf_to_pf_cmd *)desc->data; 343 344 vport = &hdev->vport[req->mbx_src_vfid]; 345 346 switch (req->msg[0]) { 347 case HCLGE_MBX_MAP_RING_TO_VECTOR: 348 ret = hclge_map_unmap_ring_to_vf_vector(vport, true, 349 req); 350 break; 351 case HCLGE_MBX_UNMAP_RING_TO_VECTOR: 352 ret = hclge_map_unmap_ring_to_vf_vector(vport, false, 353 req); 354 break; 355 case HCLGE_MBX_SET_PROMISC_MODE: 356 ret = hclge_set_vf_promisc_mode(vport, req); 357 if (ret) 358 dev_err(&hdev->pdev->dev, 359 "PF fail(%d) to set VF promisc mode\n", 360 ret); 361 break; 362 case HCLGE_MBX_SET_UNICAST: 363 ret = hclge_set_vf_uc_mac_addr(vport, req, false); 364 if (ret) 365 dev_err(&hdev->pdev->dev, 366 "PF fail(%d) to set VF UC MAC Addr\n", 367 ret); 368 break; 369 case HCLGE_MBX_SET_MULTICAST: 370 ret = hclge_set_vf_mc_mac_addr(vport, req, false); 371 if (ret) 372 dev_err(&hdev->pdev->dev, 373 "PF fail(%d) to set VF MC MAC Addr\n", 374 ret); 375 break; 376 case HCLGE_MBX_SET_VLAN: 377 ret = hclge_set_vf_vlan_cfg(vport, req, false); 378 if (ret) 379 dev_err(&hdev->pdev->dev, 380 "PF failed(%d) to config VF's VLAN\n", 381 ret); 382 break; 383 case HCLGE_MBX_GET_QINFO: 384 ret = hclge_get_vf_queue_info(vport, req, true); 385 if (ret) 386 dev_err(&hdev->pdev->dev, 387 "PF failed(%d) to get Q info for VF\n", 388 ret); 389 break; 390 case HCLGE_MBX_GET_TCINFO: 391 ret = hclge_get_vf_tcinfo(vport, req, true); 392 if (ret) 393 dev_err(&hdev->pdev->dev, 394 "PF failed(%d) to get TC info for VF\n", 395 ret); 396 break; 397 case HCLGE_MBX_GET_LINK_STATUS: 398 ret = hclge_get_link_info(vport, req); 399 if (ret) 400 dev_err(&hdev->pdev->dev, 401 "PF fail(%d) to get link stat for VF\n", 402 ret); 403 break; 404 case HCLGE_MBX_QUEUE_RESET: 405 hclge_reset_vf_queue(vport, req); 406 break; 407 default: 408 dev_err(&hdev->pdev->dev, 409 "un-supported mailbox message, code = %d\n", 410 req->msg[0]); 411 break; 412 } 413 hclge_mbx_ring_ptr_move_crq(crq); 414 } 415 416 /* Write back CMDQ_RQ header pointer, M7 need this pointer */ 417 hclge_write_dev(&hdev->hw, HCLGE_NIC_CRQ_HEAD_REG, crq->next_to_use); 418 } 419