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 int hclge_inform_reset_assert_to_vf(struct hclge_vport *vport) 83 { 84 struct hclge_dev *hdev = vport->back; 85 enum hnae3_reset_type reset_type; 86 u8 msg_data[2]; 87 u8 dest_vfid; 88 89 dest_vfid = (u8)vport->vport_id; 90 91 if (hdev->reset_type == HNAE3_FUNC_RESET) 92 reset_type = HNAE3_VF_PF_FUNC_RESET; 93 else if (hdev->reset_type == HNAE3_FLR_RESET) 94 reset_type = HNAE3_VF_FULL_RESET; 95 else 96 return -EINVAL; 97 98 memcpy(&msg_data[0], &reset_type, sizeof(u16)); 99 100 /* send this requested info to VF */ 101 return hclge_send_mbx_msg(vport, msg_data, sizeof(msg_data), 102 HCLGE_MBX_ASSERTING_RESET, dest_vfid); 103 } 104 105 static void hclge_free_vector_ring_chain(struct hnae3_ring_chain_node *head) 106 { 107 struct hnae3_ring_chain_node *chain_tmp, *chain; 108 109 chain = head->next; 110 111 while (chain) { 112 chain_tmp = chain->next; 113 kzfree(chain); 114 chain = chain_tmp; 115 } 116 } 117 118 /* hclge_get_ring_chain_from_mbx: get ring type & tqp id & int_gl idx 119 * from mailbox message 120 * msg[0]: opcode 121 * msg[1]: <not relevant to this function> 122 * msg[2]: ring_num 123 * msg[3]: first ring type (TX|RX) 124 * msg[4]: first tqp id 125 * msg[5]: first int_gl idx 126 * msg[6] ~ msg[14]: other ring type, tqp id and int_gl idx 127 */ 128 static int hclge_get_ring_chain_from_mbx( 129 struct hclge_mbx_vf_to_pf_cmd *req, 130 struct hnae3_ring_chain_node *ring_chain, 131 struct hclge_vport *vport) 132 { 133 struct hnae3_ring_chain_node *cur_chain, *new_chain; 134 int ring_num; 135 int i; 136 137 ring_num = req->msg[2]; 138 139 if (ring_num > ((HCLGE_MBX_VF_MSG_DATA_NUM - 140 HCLGE_MBX_RING_MAP_BASIC_MSG_NUM) / 141 HCLGE_MBX_RING_NODE_VARIABLE_NUM)) 142 return -ENOMEM; 143 144 hnae3_set_bit(ring_chain->flag, HNAE3_RING_TYPE_B, req->msg[3]); 145 ring_chain->tqp_index = 146 hclge_get_queue_id(vport->nic.kinfo.tqp[req->msg[4]]); 147 hnae3_set_field(ring_chain->int_gl_idx, HNAE3_RING_GL_IDX_M, 148 HNAE3_RING_GL_IDX_S, 149 req->msg[5]); 150 151 cur_chain = ring_chain; 152 153 for (i = 1; i < ring_num; i++) { 154 new_chain = kzalloc(sizeof(*new_chain), GFP_KERNEL); 155 if (!new_chain) 156 goto err; 157 158 hnae3_set_bit(new_chain->flag, HNAE3_RING_TYPE_B, 159 req->msg[HCLGE_MBX_RING_NODE_VARIABLE_NUM * i + 160 HCLGE_MBX_RING_MAP_BASIC_MSG_NUM]); 161 162 new_chain->tqp_index = 163 hclge_get_queue_id(vport->nic.kinfo.tqp 164 [req->msg[HCLGE_MBX_RING_NODE_VARIABLE_NUM * i + 165 HCLGE_MBX_RING_MAP_BASIC_MSG_NUM + 1]]); 166 167 hnae3_set_field(new_chain->int_gl_idx, HNAE3_RING_GL_IDX_M, 168 HNAE3_RING_GL_IDX_S, 169 req->msg[HCLGE_MBX_RING_NODE_VARIABLE_NUM * i + 170 HCLGE_MBX_RING_MAP_BASIC_MSG_NUM + 2]); 171 172 cur_chain->next = new_chain; 173 cur_chain = new_chain; 174 } 175 176 return 0; 177 err: 178 hclge_free_vector_ring_chain(ring_chain); 179 return -ENOMEM; 180 } 181 182 static int hclge_map_unmap_ring_to_vf_vector(struct hclge_vport *vport, bool en, 183 struct hclge_mbx_vf_to_pf_cmd *req) 184 { 185 struct hnae3_ring_chain_node ring_chain; 186 int vector_id = req->msg[1]; 187 int ret; 188 189 memset(&ring_chain, 0, sizeof(ring_chain)); 190 ret = hclge_get_ring_chain_from_mbx(req, &ring_chain, vport); 191 if (ret) 192 return ret; 193 194 ret = hclge_bind_ring_with_vector(vport, vector_id, en, &ring_chain); 195 if (ret) 196 return ret; 197 198 hclge_free_vector_ring_chain(&ring_chain); 199 200 return 0; 201 } 202 203 static int hclge_set_vf_promisc_mode(struct hclge_vport *vport, 204 struct hclge_mbx_vf_to_pf_cmd *req) 205 { 206 bool en_uc = req->msg[1] ? true : false; 207 bool en_mc = req->msg[2] ? true : false; 208 struct hclge_promisc_param param; 209 210 /* always enable broadcast promisc bit */ 211 hclge_promisc_param_init(¶m, en_uc, en_mc, true, vport->vport_id); 212 return hclge_cmd_set_promisc_mode(vport->back, ¶m); 213 } 214 215 static int hclge_set_vf_uc_mac_addr(struct hclge_vport *vport, 216 struct hclge_mbx_vf_to_pf_cmd *mbx_req, 217 bool gen_resp) 218 { 219 const u8 *mac_addr = (const u8 *)(&mbx_req->msg[2]); 220 struct hclge_dev *hdev = vport->back; 221 int status; 222 223 if (mbx_req->msg[1] == HCLGE_MBX_MAC_VLAN_UC_MODIFY) { 224 const u8 *old_addr = (const u8 *)(&mbx_req->msg[8]); 225 226 hclge_rm_uc_addr_common(vport, old_addr); 227 status = hclge_add_uc_addr_common(vport, mac_addr); 228 if (status) 229 hclge_add_uc_addr_common(vport, old_addr); 230 } else if (mbx_req->msg[1] == HCLGE_MBX_MAC_VLAN_UC_ADD) { 231 status = hclge_add_uc_addr_common(vport, mac_addr); 232 } else if (mbx_req->msg[1] == HCLGE_MBX_MAC_VLAN_UC_REMOVE) { 233 status = hclge_rm_uc_addr_common(vport, mac_addr); 234 } else { 235 dev_err(&hdev->pdev->dev, 236 "failed to set unicast mac addr, unknown subcode %d\n", 237 mbx_req->msg[1]); 238 return -EIO; 239 } 240 241 if (gen_resp) 242 hclge_gen_resp_to_vf(vport, mbx_req, status, NULL, 0); 243 244 return 0; 245 } 246 247 static int hclge_set_vf_mc_mac_addr(struct hclge_vport *vport, 248 struct hclge_mbx_vf_to_pf_cmd *mbx_req, 249 bool gen_resp) 250 { 251 const u8 *mac_addr = (const u8 *)(&mbx_req->msg[2]); 252 struct hclge_dev *hdev = vport->back; 253 u8 resp_len = 0; 254 u8 resp_data; 255 int status; 256 257 if (mbx_req->msg[1] == HCLGE_MBX_MAC_VLAN_MC_ADD) { 258 status = hclge_add_mc_addr_common(vport, mac_addr); 259 } else if (mbx_req->msg[1] == HCLGE_MBX_MAC_VLAN_MC_REMOVE) { 260 status = hclge_rm_mc_addr_common(vport, mac_addr); 261 } else { 262 dev_err(&hdev->pdev->dev, 263 "failed to set mcast mac addr, unknown subcode %d\n", 264 mbx_req->msg[1]); 265 return -EIO; 266 } 267 268 if (gen_resp) 269 hclge_gen_resp_to_vf(vport, mbx_req, status, 270 &resp_data, resp_len); 271 272 return 0; 273 } 274 275 static int hclge_set_vf_vlan_cfg(struct hclge_vport *vport, 276 struct hclge_mbx_vf_to_pf_cmd *mbx_req, 277 bool gen_resp) 278 { 279 int status = 0; 280 281 if (mbx_req->msg[1] == HCLGE_MBX_VLAN_FILTER) { 282 struct hnae3_handle *handle = &vport->nic; 283 u16 vlan, proto; 284 bool is_kill; 285 286 is_kill = !!mbx_req->msg[2]; 287 memcpy(&vlan, &mbx_req->msg[3], sizeof(vlan)); 288 memcpy(&proto, &mbx_req->msg[5], sizeof(proto)); 289 status = hclge_set_vlan_filter(handle, cpu_to_be16(proto), 290 vlan, is_kill); 291 } else if (mbx_req->msg[1] == HCLGE_MBX_VLAN_RX_OFF_CFG) { 292 struct hnae3_handle *handle = &vport->nic; 293 bool en = mbx_req->msg[2] ? true : false; 294 295 status = hclge_en_hw_strip_rxvtag(handle, en); 296 } 297 298 if (gen_resp) 299 status = hclge_gen_resp_to_vf(vport, mbx_req, status, NULL, 0); 300 301 return status; 302 } 303 304 static int hclge_set_vf_alive(struct hclge_vport *vport, 305 struct hclge_mbx_vf_to_pf_cmd *mbx_req, 306 bool gen_resp) 307 { 308 bool alive = !!mbx_req->msg[2]; 309 int ret = 0; 310 311 if (alive) 312 ret = hclge_vport_start(vport); 313 else 314 hclge_vport_stop(vport); 315 316 return ret; 317 } 318 319 static int hclge_get_vf_tcinfo(struct hclge_vport *vport, 320 struct hclge_mbx_vf_to_pf_cmd *mbx_req, 321 bool gen_resp) 322 { 323 struct hclge_dev *hdev = vport->back; 324 int ret; 325 326 ret = hclge_gen_resp_to_vf(vport, mbx_req, 0, &hdev->hw_tc_map, 327 sizeof(u8)); 328 329 return ret; 330 } 331 332 static int hclge_get_vf_queue_info(struct hclge_vport *vport, 333 struct hclge_mbx_vf_to_pf_cmd *mbx_req, 334 bool gen_resp) 335 { 336 #define HCLGE_TQPS_RSS_INFO_LEN 8 337 u8 resp_data[HCLGE_TQPS_RSS_INFO_LEN]; 338 struct hclge_dev *hdev = vport->back; 339 340 /* get the queue related info */ 341 memcpy(&resp_data[0], &vport->alloc_tqps, sizeof(u16)); 342 memcpy(&resp_data[2], &vport->nic.kinfo.rss_size, sizeof(u16)); 343 memcpy(&resp_data[4], &hdev->num_desc, sizeof(u16)); 344 memcpy(&resp_data[6], &hdev->rx_buf_len, sizeof(u16)); 345 346 return hclge_gen_resp_to_vf(vport, mbx_req, 0, resp_data, 347 HCLGE_TQPS_RSS_INFO_LEN); 348 } 349 350 static int hclge_get_link_info(struct hclge_vport *vport, 351 struct hclge_mbx_vf_to_pf_cmd *mbx_req) 352 { 353 struct hclge_dev *hdev = vport->back; 354 u16 link_status; 355 u8 msg_data[8]; 356 u8 dest_vfid; 357 u16 duplex; 358 359 /* mac.link can only be 0 or 1 */ 360 link_status = (u16)hdev->hw.mac.link; 361 duplex = hdev->hw.mac.duplex; 362 memcpy(&msg_data[0], &link_status, sizeof(u16)); 363 memcpy(&msg_data[2], &hdev->hw.mac.speed, sizeof(u32)); 364 memcpy(&msg_data[6], &duplex, sizeof(u16)); 365 dest_vfid = mbx_req->mbx_src_vfid; 366 367 /* send this requested info to VF */ 368 return hclge_send_mbx_msg(vport, msg_data, sizeof(msg_data), 369 HCLGE_MBX_LINK_STAT_CHANGE, dest_vfid); 370 } 371 372 static void hclge_mbx_reset_vf_queue(struct hclge_vport *vport, 373 struct hclge_mbx_vf_to_pf_cmd *mbx_req) 374 { 375 u16 queue_id; 376 377 memcpy(&queue_id, &mbx_req->msg[2], sizeof(queue_id)); 378 379 hclge_reset_vf_queue(vport, queue_id); 380 381 /* send response msg to VF after queue reset complete*/ 382 hclge_gen_resp_to_vf(vport, mbx_req, 0, NULL, 0); 383 } 384 385 static void hclge_reset_vf(struct hclge_vport *vport, 386 struct hclge_mbx_vf_to_pf_cmd *mbx_req) 387 { 388 struct hclge_dev *hdev = vport->back; 389 int ret; 390 391 dev_warn(&hdev->pdev->dev, "PF received VF reset request from VF %d!", 392 vport->vport_id); 393 394 ret = hclge_func_reset_cmd(hdev, vport->vport_id); 395 hclge_gen_resp_to_vf(vport, mbx_req, ret, NULL, 0); 396 } 397 398 static void hclge_vf_keep_alive(struct hclge_vport *vport, 399 struct hclge_mbx_vf_to_pf_cmd *mbx_req) 400 { 401 vport->last_active_jiffies = jiffies; 402 } 403 404 static int hclge_set_vf_mtu(struct hclge_vport *vport, 405 struct hclge_mbx_vf_to_pf_cmd *mbx_req) 406 { 407 int ret; 408 u32 mtu; 409 410 memcpy(&mtu, &mbx_req->msg[2], sizeof(mtu)); 411 ret = hclge_set_vport_mtu(vport, mtu); 412 413 return hclge_gen_resp_to_vf(vport, mbx_req, ret, NULL, 0); 414 } 415 416 static int hclge_get_queue_id_in_pf(struct hclge_vport *vport, 417 struct hclge_mbx_vf_to_pf_cmd *mbx_req) 418 { 419 u16 queue_id, qid_in_pf; 420 u8 resp_data[2]; 421 422 memcpy(&queue_id, &mbx_req->msg[2], sizeof(queue_id)); 423 qid_in_pf = hclge_covert_handle_qid_global(&vport->nic, queue_id); 424 memcpy(resp_data, &qid_in_pf, sizeof(qid_in_pf)); 425 426 return hclge_gen_resp_to_vf(vport, mbx_req, 0, resp_data, 2); 427 } 428 429 static bool hclge_cmd_crq_empty(struct hclge_hw *hw) 430 { 431 u32 tail = hclge_read_dev(hw, HCLGE_NIC_CRQ_TAIL_REG); 432 433 return tail == hw->cmq.crq.next_to_use; 434 } 435 436 void hclge_mbx_handler(struct hclge_dev *hdev) 437 { 438 struct hclge_cmq_ring *crq = &hdev->hw.cmq.crq; 439 struct hclge_mbx_vf_to_pf_cmd *req; 440 struct hclge_vport *vport; 441 struct hclge_desc *desc; 442 int ret, flag; 443 444 /* handle all the mailbox requests in the queue */ 445 while (!hclge_cmd_crq_empty(&hdev->hw)) { 446 if (test_bit(HCLGE_STATE_CMD_DISABLE, &hdev->state)) { 447 dev_warn(&hdev->pdev->dev, 448 "command queue needs re-initializing\n"); 449 return; 450 } 451 452 desc = &crq->desc[crq->next_to_use]; 453 req = (struct hclge_mbx_vf_to_pf_cmd *)desc->data; 454 455 flag = le16_to_cpu(crq->desc[crq->next_to_use].flag); 456 if (unlikely(!hnae3_get_bit(flag, HCLGE_CMDQ_RX_OUTVLD_B))) { 457 dev_warn(&hdev->pdev->dev, 458 "dropped invalid mailbox message, code = %d\n", 459 req->msg[0]); 460 461 /* dropping/not processing this invalid message */ 462 crq->desc[crq->next_to_use].flag = 0; 463 hclge_mbx_ring_ptr_move_crq(crq); 464 continue; 465 } 466 467 vport = &hdev->vport[req->mbx_src_vfid]; 468 469 switch (req->msg[0]) { 470 case HCLGE_MBX_MAP_RING_TO_VECTOR: 471 ret = hclge_map_unmap_ring_to_vf_vector(vport, true, 472 req); 473 break; 474 case HCLGE_MBX_UNMAP_RING_TO_VECTOR: 475 ret = hclge_map_unmap_ring_to_vf_vector(vport, false, 476 req); 477 break; 478 case HCLGE_MBX_SET_PROMISC_MODE: 479 ret = hclge_set_vf_promisc_mode(vport, req); 480 if (ret) 481 dev_err(&hdev->pdev->dev, 482 "PF fail(%d) to set VF promisc mode\n", 483 ret); 484 break; 485 case HCLGE_MBX_SET_UNICAST: 486 ret = hclge_set_vf_uc_mac_addr(vport, req, true); 487 if (ret) 488 dev_err(&hdev->pdev->dev, 489 "PF fail(%d) to set VF UC MAC Addr\n", 490 ret); 491 break; 492 case HCLGE_MBX_SET_MULTICAST: 493 ret = hclge_set_vf_mc_mac_addr(vport, req, false); 494 if (ret) 495 dev_err(&hdev->pdev->dev, 496 "PF fail(%d) to set VF MC MAC Addr\n", 497 ret); 498 break; 499 case HCLGE_MBX_SET_VLAN: 500 ret = hclge_set_vf_vlan_cfg(vport, req, false); 501 if (ret) 502 dev_err(&hdev->pdev->dev, 503 "PF failed(%d) to config VF's VLAN\n", 504 ret); 505 break; 506 case HCLGE_MBX_SET_ALIVE: 507 ret = hclge_set_vf_alive(vport, req, false); 508 if (ret) 509 dev_err(&hdev->pdev->dev, 510 "PF failed(%d) to set VF's ALIVE\n", 511 ret); 512 break; 513 case HCLGE_MBX_GET_QINFO: 514 ret = hclge_get_vf_queue_info(vport, req, true); 515 if (ret) 516 dev_err(&hdev->pdev->dev, 517 "PF failed(%d) to get Q info for VF\n", 518 ret); 519 break; 520 case HCLGE_MBX_GET_TCINFO: 521 ret = hclge_get_vf_tcinfo(vport, req, true); 522 if (ret) 523 dev_err(&hdev->pdev->dev, 524 "PF failed(%d) to get TC info for VF\n", 525 ret); 526 break; 527 case HCLGE_MBX_GET_LINK_STATUS: 528 ret = hclge_get_link_info(vport, req); 529 if (ret) 530 dev_err(&hdev->pdev->dev, 531 "PF fail(%d) to get link stat for VF\n", 532 ret); 533 break; 534 case HCLGE_MBX_QUEUE_RESET: 535 hclge_mbx_reset_vf_queue(vport, req); 536 break; 537 case HCLGE_MBX_RESET: 538 hclge_reset_vf(vport, req); 539 break; 540 case HCLGE_MBX_KEEP_ALIVE: 541 hclge_vf_keep_alive(vport, req); 542 break; 543 case HCLGE_MBX_SET_MTU: 544 ret = hclge_set_vf_mtu(vport, req); 545 if (ret) 546 dev_err(&hdev->pdev->dev, 547 "VF fail(%d) to set mtu\n", ret); 548 break; 549 case HCLGE_MBX_GET_QID_IN_PF: 550 ret = hclge_get_queue_id_in_pf(vport, req); 551 if (ret) 552 dev_err(&hdev->pdev->dev, 553 "PF failed(%d) to get qid for VF\n", 554 ret); 555 break; 556 default: 557 dev_err(&hdev->pdev->dev, 558 "un-supported mailbox message, code = %d\n", 559 req->msg[0]); 560 break; 561 } 562 crq->desc[crq->next_to_use].flag = 0; 563 hclge_mbx_ring_ptr_move_crq(crq); 564 } 565 566 /* Write back CMDQ_RQ header pointer, M7 need this pointer */ 567 hclge_write_dev(&hdev->hw, HCLGE_NIC_CRQ_HEAD_REG, crq->next_to_use); 568 } 569