1 // SPDX-License-Identifier: GPL-2.0+ 2 // Copyright (c) 2016-2017 Hisilicon Limited. 3 4 #include <linux/etherdevice.h> 5 #include <linux/iopoll.h> 6 #include <net/rtnetlink.h> 7 #include "hclgevf_cmd.h" 8 #include "hclgevf_main.h" 9 #include "hclge_mbx.h" 10 #include "hnae3.h" 11 12 #define HCLGEVF_NAME "hclgevf" 13 14 #define HCLGEVF_RESET_MAX_FAIL_CNT 5 15 16 static int hclgevf_reset_hdev(struct hclgevf_dev *hdev); 17 static struct hnae3_ae_algo ae_algovf; 18 19 static const struct pci_device_id ae_algovf_pci_tbl[] = { 20 {PCI_VDEVICE(HUAWEI, HNAE3_DEV_ID_100G_VF), 0}, 21 {PCI_VDEVICE(HUAWEI, HNAE3_DEV_ID_100G_RDMA_DCB_PFC_VF), 0}, 22 /* required last entry */ 23 {0, } 24 }; 25 26 static const u8 hclgevf_hash_key[] = { 27 0x6D, 0x5A, 0x56, 0xDA, 0x25, 0x5B, 0x0E, 0xC2, 28 0x41, 0x67, 0x25, 0x3D, 0x43, 0xA3, 0x8F, 0xB0, 29 0xD0, 0xCA, 0x2B, 0xCB, 0xAE, 0x7B, 0x30, 0xB4, 30 0x77, 0xCB, 0x2D, 0xA3, 0x80, 0x30, 0xF2, 0x0C, 31 0x6A, 0x42, 0xB7, 0x3B, 0xBE, 0xAC, 0x01, 0xFA 32 }; 33 34 MODULE_DEVICE_TABLE(pci, ae_algovf_pci_tbl); 35 36 static const u32 cmdq_reg_addr_list[] = {HCLGEVF_CMDQ_TX_ADDR_L_REG, 37 HCLGEVF_CMDQ_TX_ADDR_H_REG, 38 HCLGEVF_CMDQ_TX_DEPTH_REG, 39 HCLGEVF_CMDQ_TX_TAIL_REG, 40 HCLGEVF_CMDQ_TX_HEAD_REG, 41 HCLGEVF_CMDQ_RX_ADDR_L_REG, 42 HCLGEVF_CMDQ_RX_ADDR_H_REG, 43 HCLGEVF_CMDQ_RX_DEPTH_REG, 44 HCLGEVF_CMDQ_RX_TAIL_REG, 45 HCLGEVF_CMDQ_RX_HEAD_REG, 46 HCLGEVF_VECTOR0_CMDQ_SRC_REG, 47 HCLGEVF_CMDQ_INTR_STS_REG, 48 HCLGEVF_CMDQ_INTR_EN_REG, 49 HCLGEVF_CMDQ_INTR_GEN_REG}; 50 51 static const u32 common_reg_addr_list[] = {HCLGEVF_MISC_VECTOR_REG_BASE, 52 HCLGEVF_RST_ING, 53 HCLGEVF_GRO_EN_REG}; 54 55 static const u32 ring_reg_addr_list[] = {HCLGEVF_RING_RX_ADDR_L_REG, 56 HCLGEVF_RING_RX_ADDR_H_REG, 57 HCLGEVF_RING_RX_BD_NUM_REG, 58 HCLGEVF_RING_RX_BD_LENGTH_REG, 59 HCLGEVF_RING_RX_MERGE_EN_REG, 60 HCLGEVF_RING_RX_TAIL_REG, 61 HCLGEVF_RING_RX_HEAD_REG, 62 HCLGEVF_RING_RX_FBD_NUM_REG, 63 HCLGEVF_RING_RX_OFFSET_REG, 64 HCLGEVF_RING_RX_FBD_OFFSET_REG, 65 HCLGEVF_RING_RX_STASH_REG, 66 HCLGEVF_RING_RX_BD_ERR_REG, 67 HCLGEVF_RING_TX_ADDR_L_REG, 68 HCLGEVF_RING_TX_ADDR_H_REG, 69 HCLGEVF_RING_TX_BD_NUM_REG, 70 HCLGEVF_RING_TX_PRIORITY_REG, 71 HCLGEVF_RING_TX_TC_REG, 72 HCLGEVF_RING_TX_MERGE_EN_REG, 73 HCLGEVF_RING_TX_TAIL_REG, 74 HCLGEVF_RING_TX_HEAD_REG, 75 HCLGEVF_RING_TX_FBD_NUM_REG, 76 HCLGEVF_RING_TX_OFFSET_REG, 77 HCLGEVF_RING_TX_EBD_NUM_REG, 78 HCLGEVF_RING_TX_EBD_OFFSET_REG, 79 HCLGEVF_RING_TX_BD_ERR_REG, 80 HCLGEVF_RING_EN_REG}; 81 82 static const u32 tqp_intr_reg_addr_list[] = {HCLGEVF_TQP_INTR_CTRL_REG, 83 HCLGEVF_TQP_INTR_GL0_REG, 84 HCLGEVF_TQP_INTR_GL1_REG, 85 HCLGEVF_TQP_INTR_GL2_REG, 86 HCLGEVF_TQP_INTR_RL_REG}; 87 88 static struct hclgevf_dev *hclgevf_ae_get_hdev(struct hnae3_handle *handle) 89 { 90 if (!handle->client) 91 return container_of(handle, struct hclgevf_dev, nic); 92 else if (handle->client->type == HNAE3_CLIENT_ROCE) 93 return container_of(handle, struct hclgevf_dev, roce); 94 else 95 return container_of(handle, struct hclgevf_dev, nic); 96 } 97 98 static int hclgevf_tqps_update_stats(struct hnae3_handle *handle) 99 { 100 struct hnae3_knic_private_info *kinfo = &handle->kinfo; 101 struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle); 102 struct hclgevf_desc desc; 103 struct hclgevf_tqp *tqp; 104 int status; 105 int i; 106 107 for (i = 0; i < kinfo->num_tqps; i++) { 108 tqp = container_of(kinfo->tqp[i], struct hclgevf_tqp, q); 109 hclgevf_cmd_setup_basic_desc(&desc, 110 HCLGEVF_OPC_QUERY_RX_STATUS, 111 true); 112 113 desc.data[0] = cpu_to_le32(tqp->index & 0x1ff); 114 status = hclgevf_cmd_send(&hdev->hw, &desc, 1); 115 if (status) { 116 dev_err(&hdev->pdev->dev, 117 "Query tqp stat fail, status = %d,queue = %d\n", 118 status, i); 119 return status; 120 } 121 tqp->tqp_stats.rcb_rx_ring_pktnum_rcd += 122 le32_to_cpu(desc.data[1]); 123 124 hclgevf_cmd_setup_basic_desc(&desc, HCLGEVF_OPC_QUERY_TX_STATUS, 125 true); 126 127 desc.data[0] = cpu_to_le32(tqp->index & 0x1ff); 128 status = hclgevf_cmd_send(&hdev->hw, &desc, 1); 129 if (status) { 130 dev_err(&hdev->pdev->dev, 131 "Query tqp stat fail, status = %d,queue = %d\n", 132 status, i); 133 return status; 134 } 135 tqp->tqp_stats.rcb_tx_ring_pktnum_rcd += 136 le32_to_cpu(desc.data[1]); 137 } 138 139 return 0; 140 } 141 142 static u64 *hclgevf_tqps_get_stats(struct hnae3_handle *handle, u64 *data) 143 { 144 struct hnae3_knic_private_info *kinfo = &handle->kinfo; 145 struct hclgevf_tqp *tqp; 146 u64 *buff = data; 147 int i; 148 149 for (i = 0; i < kinfo->num_tqps; i++) { 150 tqp = container_of(kinfo->tqp[i], struct hclgevf_tqp, q); 151 *buff++ = tqp->tqp_stats.rcb_tx_ring_pktnum_rcd; 152 } 153 for (i = 0; i < kinfo->num_tqps; i++) { 154 tqp = container_of(kinfo->tqp[i], struct hclgevf_tqp, q); 155 *buff++ = tqp->tqp_stats.rcb_rx_ring_pktnum_rcd; 156 } 157 158 return buff; 159 } 160 161 static int hclgevf_tqps_get_sset_count(struct hnae3_handle *handle, int strset) 162 { 163 struct hnae3_knic_private_info *kinfo = &handle->kinfo; 164 165 return kinfo->num_tqps * 2; 166 } 167 168 static u8 *hclgevf_tqps_get_strings(struct hnae3_handle *handle, u8 *data) 169 { 170 struct hnae3_knic_private_info *kinfo = &handle->kinfo; 171 u8 *buff = data; 172 int i = 0; 173 174 for (i = 0; i < kinfo->num_tqps; i++) { 175 struct hclgevf_tqp *tqp = container_of(kinfo->tqp[i], 176 struct hclgevf_tqp, q); 177 snprintf(buff, ETH_GSTRING_LEN, "txq%d_pktnum_rcd", 178 tqp->index); 179 buff += ETH_GSTRING_LEN; 180 } 181 182 for (i = 0; i < kinfo->num_tqps; i++) { 183 struct hclgevf_tqp *tqp = container_of(kinfo->tqp[i], 184 struct hclgevf_tqp, q); 185 snprintf(buff, ETH_GSTRING_LEN, "rxq%d_pktnum_rcd", 186 tqp->index); 187 buff += ETH_GSTRING_LEN; 188 } 189 190 return buff; 191 } 192 193 static void hclgevf_update_stats(struct hnae3_handle *handle, 194 struct net_device_stats *net_stats) 195 { 196 struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle); 197 int status; 198 199 status = hclgevf_tqps_update_stats(handle); 200 if (status) 201 dev_err(&hdev->pdev->dev, 202 "VF update of TQPS stats fail, status = %d.\n", 203 status); 204 } 205 206 static int hclgevf_get_sset_count(struct hnae3_handle *handle, int strset) 207 { 208 if (strset == ETH_SS_TEST) 209 return -EOPNOTSUPP; 210 else if (strset == ETH_SS_STATS) 211 return hclgevf_tqps_get_sset_count(handle, strset); 212 213 return 0; 214 } 215 216 static void hclgevf_get_strings(struct hnae3_handle *handle, u32 strset, 217 u8 *data) 218 { 219 u8 *p = (char *)data; 220 221 if (strset == ETH_SS_STATS) 222 p = hclgevf_tqps_get_strings(handle, p); 223 } 224 225 static void hclgevf_get_stats(struct hnae3_handle *handle, u64 *data) 226 { 227 hclgevf_tqps_get_stats(handle, data); 228 } 229 230 static int hclgevf_get_tc_info(struct hclgevf_dev *hdev) 231 { 232 u8 resp_msg; 233 int status; 234 235 status = hclgevf_send_mbx_msg(hdev, HCLGE_MBX_GET_TCINFO, 0, NULL, 0, 236 true, &resp_msg, sizeof(resp_msg)); 237 if (status) { 238 dev_err(&hdev->pdev->dev, 239 "VF request to get TC info from PF failed %d", 240 status); 241 return status; 242 } 243 244 hdev->hw_tc_map = resp_msg; 245 246 return 0; 247 } 248 249 static int hclgevf_get_port_base_vlan_filter_state(struct hclgevf_dev *hdev) 250 { 251 struct hnae3_handle *nic = &hdev->nic; 252 u8 resp_msg; 253 int ret; 254 255 ret = hclgevf_send_mbx_msg(hdev, HCLGE_MBX_SET_VLAN, 256 HCLGE_MBX_GET_PORT_BASE_VLAN_STATE, 257 NULL, 0, true, &resp_msg, sizeof(u8)); 258 if (ret) { 259 dev_err(&hdev->pdev->dev, 260 "VF request to get port based vlan state failed %d", 261 ret); 262 return ret; 263 } 264 265 nic->port_base_vlan_state = resp_msg; 266 267 return 0; 268 } 269 270 static int hclgevf_get_queue_info(struct hclgevf_dev *hdev) 271 { 272 #define HCLGEVF_TQPS_RSS_INFO_LEN 6 273 u8 resp_msg[HCLGEVF_TQPS_RSS_INFO_LEN]; 274 int status; 275 276 status = hclgevf_send_mbx_msg(hdev, HCLGE_MBX_GET_QINFO, 0, NULL, 0, 277 true, resp_msg, 278 HCLGEVF_TQPS_RSS_INFO_LEN); 279 if (status) { 280 dev_err(&hdev->pdev->dev, 281 "VF request to get tqp info from PF failed %d", 282 status); 283 return status; 284 } 285 286 memcpy(&hdev->num_tqps, &resp_msg[0], sizeof(u16)); 287 memcpy(&hdev->rss_size_max, &resp_msg[2], sizeof(u16)); 288 memcpy(&hdev->rx_buf_len, &resp_msg[4], sizeof(u16)); 289 290 return 0; 291 } 292 293 static int hclgevf_get_queue_depth(struct hclgevf_dev *hdev) 294 { 295 #define HCLGEVF_TQPS_DEPTH_INFO_LEN 4 296 u8 resp_msg[HCLGEVF_TQPS_DEPTH_INFO_LEN]; 297 int ret; 298 299 ret = hclgevf_send_mbx_msg(hdev, HCLGE_MBX_GET_QDEPTH, 0, NULL, 0, 300 true, resp_msg, 301 HCLGEVF_TQPS_DEPTH_INFO_LEN); 302 if (ret) { 303 dev_err(&hdev->pdev->dev, 304 "VF request to get tqp depth info from PF failed %d", 305 ret); 306 return ret; 307 } 308 309 memcpy(&hdev->num_tx_desc, &resp_msg[0], sizeof(u16)); 310 memcpy(&hdev->num_rx_desc, &resp_msg[2], sizeof(u16)); 311 312 return 0; 313 } 314 315 static u16 hclgevf_get_qid_global(struct hnae3_handle *handle, u16 queue_id) 316 { 317 struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle); 318 u8 msg_data[2], resp_data[2]; 319 u16 qid_in_pf = 0; 320 int ret; 321 322 memcpy(&msg_data[0], &queue_id, sizeof(queue_id)); 323 324 ret = hclgevf_send_mbx_msg(hdev, HCLGE_MBX_GET_QID_IN_PF, 0, msg_data, 325 sizeof(msg_data), true, resp_data, 326 sizeof(resp_data)); 327 if (!ret) 328 qid_in_pf = *(u16 *)resp_data; 329 330 return qid_in_pf; 331 } 332 333 static int hclgevf_get_pf_media_type(struct hclgevf_dev *hdev) 334 { 335 u8 resp_msg[2]; 336 int ret; 337 338 ret = hclgevf_send_mbx_msg(hdev, HCLGE_MBX_GET_MEDIA_TYPE, 0, NULL, 0, 339 true, resp_msg, sizeof(resp_msg)); 340 if (ret) { 341 dev_err(&hdev->pdev->dev, 342 "VF request to get the pf port media type failed %d", 343 ret); 344 return ret; 345 } 346 347 hdev->hw.mac.media_type = resp_msg[0]; 348 hdev->hw.mac.module_type = resp_msg[1]; 349 350 return 0; 351 } 352 353 static int hclgevf_alloc_tqps(struct hclgevf_dev *hdev) 354 { 355 struct hclgevf_tqp *tqp; 356 int i; 357 358 hdev->htqp = devm_kcalloc(&hdev->pdev->dev, hdev->num_tqps, 359 sizeof(struct hclgevf_tqp), GFP_KERNEL); 360 if (!hdev->htqp) 361 return -ENOMEM; 362 363 tqp = hdev->htqp; 364 365 for (i = 0; i < hdev->num_tqps; i++) { 366 tqp->dev = &hdev->pdev->dev; 367 tqp->index = i; 368 369 tqp->q.ae_algo = &ae_algovf; 370 tqp->q.buf_size = hdev->rx_buf_len; 371 tqp->q.tx_desc_num = hdev->num_tx_desc; 372 tqp->q.rx_desc_num = hdev->num_rx_desc; 373 tqp->q.io_base = hdev->hw.io_base + HCLGEVF_TQP_REG_OFFSET + 374 i * HCLGEVF_TQP_REG_SIZE; 375 376 tqp++; 377 } 378 379 return 0; 380 } 381 382 static int hclgevf_knic_setup(struct hclgevf_dev *hdev) 383 { 384 struct hnae3_handle *nic = &hdev->nic; 385 struct hnae3_knic_private_info *kinfo; 386 u16 new_tqps = hdev->num_tqps; 387 unsigned int i; 388 389 kinfo = &nic->kinfo; 390 kinfo->num_tc = 0; 391 kinfo->num_tx_desc = hdev->num_tx_desc; 392 kinfo->num_rx_desc = hdev->num_rx_desc; 393 kinfo->rx_buf_len = hdev->rx_buf_len; 394 for (i = 0; i < HCLGEVF_MAX_TC_NUM; i++) 395 if (hdev->hw_tc_map & BIT(i)) 396 kinfo->num_tc++; 397 398 kinfo->rss_size 399 = min_t(u16, hdev->rss_size_max, new_tqps / kinfo->num_tc); 400 new_tqps = kinfo->rss_size * kinfo->num_tc; 401 kinfo->num_tqps = min(new_tqps, hdev->num_tqps); 402 403 kinfo->tqp = devm_kcalloc(&hdev->pdev->dev, kinfo->num_tqps, 404 sizeof(struct hnae3_queue *), GFP_KERNEL); 405 if (!kinfo->tqp) 406 return -ENOMEM; 407 408 for (i = 0; i < kinfo->num_tqps; i++) { 409 hdev->htqp[i].q.handle = &hdev->nic; 410 hdev->htqp[i].q.tqp_index = i; 411 kinfo->tqp[i] = &hdev->htqp[i].q; 412 } 413 414 return 0; 415 } 416 417 static void hclgevf_request_link_info(struct hclgevf_dev *hdev) 418 { 419 int status; 420 u8 resp_msg; 421 422 status = hclgevf_send_mbx_msg(hdev, HCLGE_MBX_GET_LINK_STATUS, 0, NULL, 423 0, false, &resp_msg, sizeof(resp_msg)); 424 if (status) 425 dev_err(&hdev->pdev->dev, 426 "VF failed to fetch link status(%d) from PF", status); 427 } 428 429 void hclgevf_update_link_status(struct hclgevf_dev *hdev, int link_state) 430 { 431 struct hnae3_handle *rhandle = &hdev->roce; 432 struct hnae3_handle *handle = &hdev->nic; 433 struct hnae3_client *rclient; 434 struct hnae3_client *client; 435 436 client = handle->client; 437 rclient = hdev->roce_client; 438 439 link_state = 440 test_bit(HCLGEVF_STATE_DOWN, &hdev->state) ? 0 : link_state; 441 442 if (link_state != hdev->hw.mac.link) { 443 client->ops->link_status_change(handle, !!link_state); 444 if (rclient && rclient->ops->link_status_change) 445 rclient->ops->link_status_change(rhandle, !!link_state); 446 hdev->hw.mac.link = link_state; 447 } 448 } 449 450 static void hclgevf_update_link_mode(struct hclgevf_dev *hdev) 451 { 452 #define HCLGEVF_ADVERTISING 0 453 #define HCLGEVF_SUPPORTED 1 454 u8 send_msg; 455 u8 resp_msg; 456 457 send_msg = HCLGEVF_ADVERTISING; 458 hclgevf_send_mbx_msg(hdev, HCLGE_MBX_GET_LINK_MODE, 0, 459 &send_msg, sizeof(send_msg), false, 460 &resp_msg, sizeof(resp_msg)); 461 send_msg = HCLGEVF_SUPPORTED; 462 hclgevf_send_mbx_msg(hdev, HCLGE_MBX_GET_LINK_MODE, 0, 463 &send_msg, sizeof(send_msg), false, 464 &resp_msg, sizeof(resp_msg)); 465 } 466 467 static int hclgevf_set_handle_info(struct hclgevf_dev *hdev) 468 { 469 struct hnae3_handle *nic = &hdev->nic; 470 int ret; 471 472 nic->ae_algo = &ae_algovf; 473 nic->pdev = hdev->pdev; 474 nic->numa_node_mask = hdev->numa_node_mask; 475 nic->flags |= HNAE3_SUPPORT_VF; 476 477 ret = hclgevf_knic_setup(hdev); 478 if (ret) 479 dev_err(&hdev->pdev->dev, "VF knic setup failed %d\n", 480 ret); 481 return ret; 482 } 483 484 static void hclgevf_free_vector(struct hclgevf_dev *hdev, int vector_id) 485 { 486 if (hdev->vector_status[vector_id] == HCLGEVF_INVALID_VPORT) { 487 dev_warn(&hdev->pdev->dev, 488 "vector(vector_id %d) has been freed.\n", vector_id); 489 return; 490 } 491 492 hdev->vector_status[vector_id] = HCLGEVF_INVALID_VPORT; 493 hdev->num_msi_left += 1; 494 hdev->num_msi_used -= 1; 495 } 496 497 static int hclgevf_get_vector(struct hnae3_handle *handle, u16 vector_num, 498 struct hnae3_vector_info *vector_info) 499 { 500 struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle); 501 struct hnae3_vector_info *vector = vector_info; 502 int alloc = 0; 503 int i, j; 504 505 vector_num = min(hdev->num_msi_left, vector_num); 506 507 for (j = 0; j < vector_num; j++) { 508 for (i = HCLGEVF_MISC_VECTOR_NUM + 1; i < hdev->num_msi; i++) { 509 if (hdev->vector_status[i] == HCLGEVF_INVALID_VPORT) { 510 vector->vector = pci_irq_vector(hdev->pdev, i); 511 vector->io_addr = hdev->hw.io_base + 512 HCLGEVF_VECTOR_REG_BASE + 513 (i - 1) * HCLGEVF_VECTOR_REG_OFFSET; 514 hdev->vector_status[i] = 0; 515 hdev->vector_irq[i] = vector->vector; 516 517 vector++; 518 alloc++; 519 520 break; 521 } 522 } 523 } 524 hdev->num_msi_left -= alloc; 525 hdev->num_msi_used += alloc; 526 527 return alloc; 528 } 529 530 static int hclgevf_get_vector_index(struct hclgevf_dev *hdev, int vector) 531 { 532 int i; 533 534 for (i = 0; i < hdev->num_msi; i++) 535 if (vector == hdev->vector_irq[i]) 536 return i; 537 538 return -EINVAL; 539 } 540 541 static int hclgevf_set_rss_algo_key(struct hclgevf_dev *hdev, 542 const u8 hfunc, const u8 *key) 543 { 544 struct hclgevf_rss_config_cmd *req; 545 unsigned int key_offset = 0; 546 struct hclgevf_desc desc; 547 int key_counts; 548 int key_size; 549 int ret; 550 551 key_counts = HCLGEVF_RSS_KEY_SIZE; 552 req = (struct hclgevf_rss_config_cmd *)desc.data; 553 554 while (key_counts) { 555 hclgevf_cmd_setup_basic_desc(&desc, 556 HCLGEVF_OPC_RSS_GENERIC_CONFIG, 557 false); 558 559 req->hash_config |= (hfunc & HCLGEVF_RSS_HASH_ALGO_MASK); 560 req->hash_config |= 561 (key_offset << HCLGEVF_RSS_HASH_KEY_OFFSET_B); 562 563 key_size = min(HCLGEVF_RSS_HASH_KEY_NUM, key_counts); 564 memcpy(req->hash_key, 565 key + key_offset * HCLGEVF_RSS_HASH_KEY_NUM, key_size); 566 567 key_counts -= key_size; 568 key_offset++; 569 ret = hclgevf_cmd_send(&hdev->hw, &desc, 1); 570 if (ret) { 571 dev_err(&hdev->pdev->dev, 572 "Configure RSS config fail, status = %d\n", 573 ret); 574 return ret; 575 } 576 } 577 578 return 0; 579 } 580 581 static u32 hclgevf_get_rss_key_size(struct hnae3_handle *handle) 582 { 583 return HCLGEVF_RSS_KEY_SIZE; 584 } 585 586 static u32 hclgevf_get_rss_indir_size(struct hnae3_handle *handle) 587 { 588 return HCLGEVF_RSS_IND_TBL_SIZE; 589 } 590 591 static int hclgevf_set_rss_indir_table(struct hclgevf_dev *hdev) 592 { 593 const u8 *indir = hdev->rss_cfg.rss_indirection_tbl; 594 struct hclgevf_rss_indirection_table_cmd *req; 595 struct hclgevf_desc desc; 596 int status; 597 int i, j; 598 599 req = (struct hclgevf_rss_indirection_table_cmd *)desc.data; 600 601 for (i = 0; i < HCLGEVF_RSS_CFG_TBL_NUM; i++) { 602 hclgevf_cmd_setup_basic_desc(&desc, HCLGEVF_OPC_RSS_INDIR_TABLE, 603 false); 604 req->start_table_index = i * HCLGEVF_RSS_CFG_TBL_SIZE; 605 req->rss_set_bitmap = HCLGEVF_RSS_SET_BITMAP_MSK; 606 for (j = 0; j < HCLGEVF_RSS_CFG_TBL_SIZE; j++) 607 req->rss_result[j] = 608 indir[i * HCLGEVF_RSS_CFG_TBL_SIZE + j]; 609 610 status = hclgevf_cmd_send(&hdev->hw, &desc, 1); 611 if (status) { 612 dev_err(&hdev->pdev->dev, 613 "VF failed(=%d) to set RSS indirection table\n", 614 status); 615 return status; 616 } 617 } 618 619 return 0; 620 } 621 622 static int hclgevf_set_rss_tc_mode(struct hclgevf_dev *hdev, u16 rss_size) 623 { 624 struct hclgevf_rss_tc_mode_cmd *req; 625 u16 tc_offset[HCLGEVF_MAX_TC_NUM]; 626 u16 tc_valid[HCLGEVF_MAX_TC_NUM]; 627 u16 tc_size[HCLGEVF_MAX_TC_NUM]; 628 struct hclgevf_desc desc; 629 u16 roundup_size; 630 int status; 631 unsigned int i; 632 633 req = (struct hclgevf_rss_tc_mode_cmd *)desc.data; 634 635 roundup_size = roundup_pow_of_two(rss_size); 636 roundup_size = ilog2(roundup_size); 637 638 for (i = 0; i < HCLGEVF_MAX_TC_NUM; i++) { 639 tc_valid[i] = !!(hdev->hw_tc_map & BIT(i)); 640 tc_size[i] = roundup_size; 641 tc_offset[i] = rss_size * i; 642 } 643 644 hclgevf_cmd_setup_basic_desc(&desc, HCLGEVF_OPC_RSS_TC_MODE, false); 645 for (i = 0; i < HCLGEVF_MAX_TC_NUM; i++) { 646 hnae3_set_bit(req->rss_tc_mode[i], HCLGEVF_RSS_TC_VALID_B, 647 (tc_valid[i] & 0x1)); 648 hnae3_set_field(req->rss_tc_mode[i], HCLGEVF_RSS_TC_SIZE_M, 649 HCLGEVF_RSS_TC_SIZE_S, tc_size[i]); 650 hnae3_set_field(req->rss_tc_mode[i], HCLGEVF_RSS_TC_OFFSET_M, 651 HCLGEVF_RSS_TC_OFFSET_S, tc_offset[i]); 652 } 653 status = hclgevf_cmd_send(&hdev->hw, &desc, 1); 654 if (status) 655 dev_err(&hdev->pdev->dev, 656 "VF failed(=%d) to set rss tc mode\n", status); 657 658 return status; 659 } 660 661 /* for revision 0x20, vf shared the same rss config with pf */ 662 static int hclgevf_get_rss_hash_key(struct hclgevf_dev *hdev) 663 { 664 #define HCLGEVF_RSS_MBX_RESP_LEN 8 665 666 struct hclgevf_rss_cfg *rss_cfg = &hdev->rss_cfg; 667 u8 resp_msg[HCLGEVF_RSS_MBX_RESP_LEN]; 668 u16 msg_num, hash_key_index; 669 u8 index; 670 int ret; 671 672 msg_num = (HCLGEVF_RSS_KEY_SIZE + HCLGEVF_RSS_MBX_RESP_LEN - 1) / 673 HCLGEVF_RSS_MBX_RESP_LEN; 674 for (index = 0; index < msg_num; index++) { 675 ret = hclgevf_send_mbx_msg(hdev, HCLGE_MBX_GET_RSS_KEY, 0, 676 &index, sizeof(index), 677 true, resp_msg, 678 HCLGEVF_RSS_MBX_RESP_LEN); 679 if (ret) { 680 dev_err(&hdev->pdev->dev, 681 "VF get rss hash key from PF failed, ret=%d", 682 ret); 683 return ret; 684 } 685 686 hash_key_index = HCLGEVF_RSS_MBX_RESP_LEN * index; 687 if (index == msg_num - 1) 688 memcpy(&rss_cfg->rss_hash_key[hash_key_index], 689 &resp_msg[0], 690 HCLGEVF_RSS_KEY_SIZE - hash_key_index); 691 else 692 memcpy(&rss_cfg->rss_hash_key[hash_key_index], 693 &resp_msg[0], HCLGEVF_RSS_MBX_RESP_LEN); 694 } 695 696 return 0; 697 } 698 699 static int hclgevf_get_rss(struct hnae3_handle *handle, u32 *indir, u8 *key, 700 u8 *hfunc) 701 { 702 struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle); 703 struct hclgevf_rss_cfg *rss_cfg = &hdev->rss_cfg; 704 int i, ret; 705 706 if (handle->pdev->revision >= 0x21) { 707 /* Get hash algorithm */ 708 if (hfunc) { 709 switch (rss_cfg->hash_algo) { 710 case HCLGEVF_RSS_HASH_ALGO_TOEPLITZ: 711 *hfunc = ETH_RSS_HASH_TOP; 712 break; 713 case HCLGEVF_RSS_HASH_ALGO_SIMPLE: 714 *hfunc = ETH_RSS_HASH_XOR; 715 break; 716 default: 717 *hfunc = ETH_RSS_HASH_UNKNOWN; 718 break; 719 } 720 } 721 722 /* Get the RSS Key required by the user */ 723 if (key) 724 memcpy(key, rss_cfg->rss_hash_key, 725 HCLGEVF_RSS_KEY_SIZE); 726 } else { 727 if (hfunc) 728 *hfunc = ETH_RSS_HASH_TOP; 729 if (key) { 730 ret = hclgevf_get_rss_hash_key(hdev); 731 if (ret) 732 return ret; 733 memcpy(key, rss_cfg->rss_hash_key, 734 HCLGEVF_RSS_KEY_SIZE); 735 } 736 } 737 738 if (indir) 739 for (i = 0; i < HCLGEVF_RSS_IND_TBL_SIZE; i++) 740 indir[i] = rss_cfg->rss_indirection_tbl[i]; 741 742 return 0; 743 } 744 745 static int hclgevf_set_rss(struct hnae3_handle *handle, const u32 *indir, 746 const u8 *key, const u8 hfunc) 747 { 748 struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle); 749 struct hclgevf_rss_cfg *rss_cfg = &hdev->rss_cfg; 750 int ret, i; 751 752 if (handle->pdev->revision >= 0x21) { 753 /* Set the RSS Hash Key if specififed by the user */ 754 if (key) { 755 switch (hfunc) { 756 case ETH_RSS_HASH_TOP: 757 rss_cfg->hash_algo = 758 HCLGEVF_RSS_HASH_ALGO_TOEPLITZ; 759 break; 760 case ETH_RSS_HASH_XOR: 761 rss_cfg->hash_algo = 762 HCLGEVF_RSS_HASH_ALGO_SIMPLE; 763 break; 764 case ETH_RSS_HASH_NO_CHANGE: 765 break; 766 default: 767 return -EINVAL; 768 } 769 770 ret = hclgevf_set_rss_algo_key(hdev, rss_cfg->hash_algo, 771 key); 772 if (ret) 773 return ret; 774 775 /* Update the shadow RSS key with user specified qids */ 776 memcpy(rss_cfg->rss_hash_key, key, 777 HCLGEVF_RSS_KEY_SIZE); 778 } 779 } 780 781 /* update the shadow RSS table with user specified qids */ 782 for (i = 0; i < HCLGEVF_RSS_IND_TBL_SIZE; i++) 783 rss_cfg->rss_indirection_tbl[i] = indir[i]; 784 785 /* update the hardware */ 786 return hclgevf_set_rss_indir_table(hdev); 787 } 788 789 static u8 hclgevf_get_rss_hash_bits(struct ethtool_rxnfc *nfc) 790 { 791 u8 hash_sets = nfc->data & RXH_L4_B_0_1 ? HCLGEVF_S_PORT_BIT : 0; 792 793 if (nfc->data & RXH_L4_B_2_3) 794 hash_sets |= HCLGEVF_D_PORT_BIT; 795 else 796 hash_sets &= ~HCLGEVF_D_PORT_BIT; 797 798 if (nfc->data & RXH_IP_SRC) 799 hash_sets |= HCLGEVF_S_IP_BIT; 800 else 801 hash_sets &= ~HCLGEVF_S_IP_BIT; 802 803 if (nfc->data & RXH_IP_DST) 804 hash_sets |= HCLGEVF_D_IP_BIT; 805 else 806 hash_sets &= ~HCLGEVF_D_IP_BIT; 807 808 if (nfc->flow_type == SCTP_V4_FLOW || nfc->flow_type == SCTP_V6_FLOW) 809 hash_sets |= HCLGEVF_V_TAG_BIT; 810 811 return hash_sets; 812 } 813 814 static int hclgevf_set_rss_tuple(struct hnae3_handle *handle, 815 struct ethtool_rxnfc *nfc) 816 { 817 struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle); 818 struct hclgevf_rss_cfg *rss_cfg = &hdev->rss_cfg; 819 struct hclgevf_rss_input_tuple_cmd *req; 820 struct hclgevf_desc desc; 821 u8 tuple_sets; 822 int ret; 823 824 if (handle->pdev->revision == 0x20) 825 return -EOPNOTSUPP; 826 827 if (nfc->data & 828 ~(RXH_IP_SRC | RXH_IP_DST | RXH_L4_B_0_1 | RXH_L4_B_2_3)) 829 return -EINVAL; 830 831 req = (struct hclgevf_rss_input_tuple_cmd *)desc.data; 832 hclgevf_cmd_setup_basic_desc(&desc, HCLGEVF_OPC_RSS_INPUT_TUPLE, false); 833 834 req->ipv4_tcp_en = rss_cfg->rss_tuple_sets.ipv4_tcp_en; 835 req->ipv4_udp_en = rss_cfg->rss_tuple_sets.ipv4_udp_en; 836 req->ipv4_sctp_en = rss_cfg->rss_tuple_sets.ipv4_sctp_en; 837 req->ipv4_fragment_en = rss_cfg->rss_tuple_sets.ipv4_fragment_en; 838 req->ipv6_tcp_en = rss_cfg->rss_tuple_sets.ipv6_tcp_en; 839 req->ipv6_udp_en = rss_cfg->rss_tuple_sets.ipv6_udp_en; 840 req->ipv6_sctp_en = rss_cfg->rss_tuple_sets.ipv6_sctp_en; 841 req->ipv6_fragment_en = rss_cfg->rss_tuple_sets.ipv6_fragment_en; 842 843 tuple_sets = hclgevf_get_rss_hash_bits(nfc); 844 switch (nfc->flow_type) { 845 case TCP_V4_FLOW: 846 req->ipv4_tcp_en = tuple_sets; 847 break; 848 case TCP_V6_FLOW: 849 req->ipv6_tcp_en = tuple_sets; 850 break; 851 case UDP_V4_FLOW: 852 req->ipv4_udp_en = tuple_sets; 853 break; 854 case UDP_V6_FLOW: 855 req->ipv6_udp_en = tuple_sets; 856 break; 857 case SCTP_V4_FLOW: 858 req->ipv4_sctp_en = tuple_sets; 859 break; 860 case SCTP_V6_FLOW: 861 if ((nfc->data & RXH_L4_B_0_1) || 862 (nfc->data & RXH_L4_B_2_3)) 863 return -EINVAL; 864 865 req->ipv6_sctp_en = tuple_sets; 866 break; 867 case IPV4_FLOW: 868 req->ipv4_fragment_en = HCLGEVF_RSS_INPUT_TUPLE_OTHER; 869 break; 870 case IPV6_FLOW: 871 req->ipv6_fragment_en = HCLGEVF_RSS_INPUT_TUPLE_OTHER; 872 break; 873 default: 874 return -EINVAL; 875 } 876 877 ret = hclgevf_cmd_send(&hdev->hw, &desc, 1); 878 if (ret) { 879 dev_err(&hdev->pdev->dev, 880 "Set rss tuple fail, status = %d\n", ret); 881 return ret; 882 } 883 884 rss_cfg->rss_tuple_sets.ipv4_tcp_en = req->ipv4_tcp_en; 885 rss_cfg->rss_tuple_sets.ipv4_udp_en = req->ipv4_udp_en; 886 rss_cfg->rss_tuple_sets.ipv4_sctp_en = req->ipv4_sctp_en; 887 rss_cfg->rss_tuple_sets.ipv4_fragment_en = req->ipv4_fragment_en; 888 rss_cfg->rss_tuple_sets.ipv6_tcp_en = req->ipv6_tcp_en; 889 rss_cfg->rss_tuple_sets.ipv6_udp_en = req->ipv6_udp_en; 890 rss_cfg->rss_tuple_sets.ipv6_sctp_en = req->ipv6_sctp_en; 891 rss_cfg->rss_tuple_sets.ipv6_fragment_en = req->ipv6_fragment_en; 892 return 0; 893 } 894 895 static int hclgevf_get_rss_tuple(struct hnae3_handle *handle, 896 struct ethtool_rxnfc *nfc) 897 { 898 struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle); 899 struct hclgevf_rss_cfg *rss_cfg = &hdev->rss_cfg; 900 u8 tuple_sets; 901 902 if (handle->pdev->revision == 0x20) 903 return -EOPNOTSUPP; 904 905 nfc->data = 0; 906 907 switch (nfc->flow_type) { 908 case TCP_V4_FLOW: 909 tuple_sets = rss_cfg->rss_tuple_sets.ipv4_tcp_en; 910 break; 911 case UDP_V4_FLOW: 912 tuple_sets = rss_cfg->rss_tuple_sets.ipv4_udp_en; 913 break; 914 case TCP_V6_FLOW: 915 tuple_sets = rss_cfg->rss_tuple_sets.ipv6_tcp_en; 916 break; 917 case UDP_V6_FLOW: 918 tuple_sets = rss_cfg->rss_tuple_sets.ipv6_udp_en; 919 break; 920 case SCTP_V4_FLOW: 921 tuple_sets = rss_cfg->rss_tuple_sets.ipv4_sctp_en; 922 break; 923 case SCTP_V6_FLOW: 924 tuple_sets = rss_cfg->rss_tuple_sets.ipv6_sctp_en; 925 break; 926 case IPV4_FLOW: 927 case IPV6_FLOW: 928 tuple_sets = HCLGEVF_S_IP_BIT | HCLGEVF_D_IP_BIT; 929 break; 930 default: 931 return -EINVAL; 932 } 933 934 if (!tuple_sets) 935 return 0; 936 937 if (tuple_sets & HCLGEVF_D_PORT_BIT) 938 nfc->data |= RXH_L4_B_2_3; 939 if (tuple_sets & HCLGEVF_S_PORT_BIT) 940 nfc->data |= RXH_L4_B_0_1; 941 if (tuple_sets & HCLGEVF_D_IP_BIT) 942 nfc->data |= RXH_IP_DST; 943 if (tuple_sets & HCLGEVF_S_IP_BIT) 944 nfc->data |= RXH_IP_SRC; 945 946 return 0; 947 } 948 949 static int hclgevf_set_rss_input_tuple(struct hclgevf_dev *hdev, 950 struct hclgevf_rss_cfg *rss_cfg) 951 { 952 struct hclgevf_rss_input_tuple_cmd *req; 953 struct hclgevf_desc desc; 954 int ret; 955 956 hclgevf_cmd_setup_basic_desc(&desc, HCLGEVF_OPC_RSS_INPUT_TUPLE, false); 957 958 req = (struct hclgevf_rss_input_tuple_cmd *)desc.data; 959 960 req->ipv4_tcp_en = rss_cfg->rss_tuple_sets.ipv4_tcp_en; 961 req->ipv4_udp_en = rss_cfg->rss_tuple_sets.ipv4_udp_en; 962 req->ipv4_sctp_en = rss_cfg->rss_tuple_sets.ipv4_sctp_en; 963 req->ipv4_fragment_en = rss_cfg->rss_tuple_sets.ipv4_fragment_en; 964 req->ipv6_tcp_en = rss_cfg->rss_tuple_sets.ipv6_tcp_en; 965 req->ipv6_udp_en = rss_cfg->rss_tuple_sets.ipv6_udp_en; 966 req->ipv6_sctp_en = rss_cfg->rss_tuple_sets.ipv6_sctp_en; 967 req->ipv6_fragment_en = rss_cfg->rss_tuple_sets.ipv6_fragment_en; 968 969 ret = hclgevf_cmd_send(&hdev->hw, &desc, 1); 970 if (ret) 971 dev_err(&hdev->pdev->dev, 972 "Configure rss input fail, status = %d\n", ret); 973 return ret; 974 } 975 976 static int hclgevf_get_tc_size(struct hnae3_handle *handle) 977 { 978 struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle); 979 struct hclgevf_rss_cfg *rss_cfg = &hdev->rss_cfg; 980 981 return rss_cfg->rss_size; 982 } 983 984 static int hclgevf_bind_ring_to_vector(struct hnae3_handle *handle, bool en, 985 int vector_id, 986 struct hnae3_ring_chain_node *ring_chain) 987 { 988 struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle); 989 struct hnae3_ring_chain_node *node; 990 struct hclge_mbx_vf_to_pf_cmd *req; 991 struct hclgevf_desc desc; 992 int i = 0; 993 int status; 994 u8 type; 995 996 req = (struct hclge_mbx_vf_to_pf_cmd *)desc.data; 997 type = en ? HCLGE_MBX_MAP_RING_TO_VECTOR : 998 HCLGE_MBX_UNMAP_RING_TO_VECTOR; 999 1000 for (node = ring_chain; node; node = node->next) { 1001 int idx_offset = HCLGE_MBX_RING_MAP_BASIC_MSG_NUM + 1002 HCLGE_MBX_RING_NODE_VARIABLE_NUM * i; 1003 1004 if (i == 0) { 1005 hclgevf_cmd_setup_basic_desc(&desc, 1006 HCLGEVF_OPC_MBX_VF_TO_PF, 1007 false); 1008 req->msg[0] = type; 1009 req->msg[1] = vector_id; 1010 } 1011 1012 req->msg[idx_offset] = 1013 hnae3_get_bit(node->flag, HNAE3_RING_TYPE_B); 1014 req->msg[idx_offset + 1] = node->tqp_index; 1015 req->msg[idx_offset + 2] = hnae3_get_field(node->int_gl_idx, 1016 HNAE3_RING_GL_IDX_M, 1017 HNAE3_RING_GL_IDX_S); 1018 1019 i++; 1020 if ((i == (HCLGE_MBX_VF_MSG_DATA_NUM - 1021 HCLGE_MBX_RING_MAP_BASIC_MSG_NUM) / 1022 HCLGE_MBX_RING_NODE_VARIABLE_NUM) || 1023 !node->next) { 1024 req->msg[2] = i; 1025 1026 status = hclgevf_cmd_send(&hdev->hw, &desc, 1); 1027 if (status) { 1028 dev_err(&hdev->pdev->dev, 1029 "Map TQP fail, status is %d.\n", 1030 status); 1031 return status; 1032 } 1033 i = 0; 1034 hclgevf_cmd_setup_basic_desc(&desc, 1035 HCLGEVF_OPC_MBX_VF_TO_PF, 1036 false); 1037 req->msg[0] = type; 1038 req->msg[1] = vector_id; 1039 } 1040 } 1041 1042 return 0; 1043 } 1044 1045 static int hclgevf_map_ring_to_vector(struct hnae3_handle *handle, int vector, 1046 struct hnae3_ring_chain_node *ring_chain) 1047 { 1048 struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle); 1049 int vector_id; 1050 1051 vector_id = hclgevf_get_vector_index(hdev, vector); 1052 if (vector_id < 0) { 1053 dev_err(&handle->pdev->dev, 1054 "Get vector index fail. ret =%d\n", vector_id); 1055 return vector_id; 1056 } 1057 1058 return hclgevf_bind_ring_to_vector(handle, true, vector_id, ring_chain); 1059 } 1060 1061 static int hclgevf_unmap_ring_from_vector( 1062 struct hnae3_handle *handle, 1063 int vector, 1064 struct hnae3_ring_chain_node *ring_chain) 1065 { 1066 struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle); 1067 int ret, vector_id; 1068 1069 if (test_bit(HCLGEVF_STATE_RST_HANDLING, &hdev->state)) 1070 return 0; 1071 1072 vector_id = hclgevf_get_vector_index(hdev, vector); 1073 if (vector_id < 0) { 1074 dev_err(&handle->pdev->dev, 1075 "Get vector index fail. ret =%d\n", vector_id); 1076 return vector_id; 1077 } 1078 1079 ret = hclgevf_bind_ring_to_vector(handle, false, vector_id, ring_chain); 1080 if (ret) 1081 dev_err(&handle->pdev->dev, 1082 "Unmap ring from vector fail. vector=%d, ret =%d\n", 1083 vector_id, 1084 ret); 1085 1086 return ret; 1087 } 1088 1089 static int hclgevf_put_vector(struct hnae3_handle *handle, int vector) 1090 { 1091 struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle); 1092 int vector_id; 1093 1094 vector_id = hclgevf_get_vector_index(hdev, vector); 1095 if (vector_id < 0) { 1096 dev_err(&handle->pdev->dev, 1097 "hclgevf_put_vector get vector index fail. ret =%d\n", 1098 vector_id); 1099 return vector_id; 1100 } 1101 1102 hclgevf_free_vector(hdev, vector_id); 1103 1104 return 0; 1105 } 1106 1107 static int hclgevf_cmd_set_promisc_mode(struct hclgevf_dev *hdev, 1108 bool en_bc_pmc) 1109 { 1110 struct hclge_mbx_vf_to_pf_cmd *req; 1111 struct hclgevf_desc desc; 1112 int ret; 1113 1114 req = (struct hclge_mbx_vf_to_pf_cmd *)desc.data; 1115 1116 hclgevf_cmd_setup_basic_desc(&desc, HCLGEVF_OPC_MBX_VF_TO_PF, false); 1117 req->msg[0] = HCLGE_MBX_SET_PROMISC_MODE; 1118 req->msg[1] = en_bc_pmc ? 1 : 0; 1119 1120 ret = hclgevf_cmd_send(&hdev->hw, &desc, 1); 1121 if (ret) 1122 dev_err(&hdev->pdev->dev, 1123 "Set promisc mode fail, status is %d.\n", ret); 1124 1125 return ret; 1126 } 1127 1128 static int hclgevf_set_promisc_mode(struct hclgevf_dev *hdev, bool en_bc_pmc) 1129 { 1130 return hclgevf_cmd_set_promisc_mode(hdev, en_bc_pmc); 1131 } 1132 1133 static int hclgevf_tqp_enable(struct hclgevf_dev *hdev, unsigned int tqp_id, 1134 int stream_id, bool enable) 1135 { 1136 struct hclgevf_cfg_com_tqp_queue_cmd *req; 1137 struct hclgevf_desc desc; 1138 int status; 1139 1140 req = (struct hclgevf_cfg_com_tqp_queue_cmd *)desc.data; 1141 1142 hclgevf_cmd_setup_basic_desc(&desc, HCLGEVF_OPC_CFG_COM_TQP_QUEUE, 1143 false); 1144 req->tqp_id = cpu_to_le16(tqp_id & HCLGEVF_RING_ID_MASK); 1145 req->stream_id = cpu_to_le16(stream_id); 1146 if (enable) 1147 req->enable |= 1U << HCLGEVF_TQP_ENABLE_B; 1148 1149 status = hclgevf_cmd_send(&hdev->hw, &desc, 1); 1150 if (status) 1151 dev_err(&hdev->pdev->dev, 1152 "TQP enable fail, status =%d.\n", status); 1153 1154 return status; 1155 } 1156 1157 static void hclgevf_reset_tqp_stats(struct hnae3_handle *handle) 1158 { 1159 struct hnae3_knic_private_info *kinfo = &handle->kinfo; 1160 struct hclgevf_tqp *tqp; 1161 int i; 1162 1163 for (i = 0; i < kinfo->num_tqps; i++) { 1164 tqp = container_of(kinfo->tqp[i], struct hclgevf_tqp, q); 1165 memset(&tqp->tqp_stats, 0, sizeof(tqp->tqp_stats)); 1166 } 1167 } 1168 1169 static void hclgevf_get_mac_addr(struct hnae3_handle *handle, u8 *p) 1170 { 1171 struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle); 1172 1173 ether_addr_copy(p, hdev->hw.mac.mac_addr); 1174 } 1175 1176 static int hclgevf_set_mac_addr(struct hnae3_handle *handle, void *p, 1177 bool is_first) 1178 { 1179 struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle); 1180 u8 *old_mac_addr = (u8 *)hdev->hw.mac.mac_addr; 1181 u8 *new_mac_addr = (u8 *)p; 1182 u8 msg_data[ETH_ALEN * 2]; 1183 u16 subcode; 1184 int status; 1185 1186 ether_addr_copy(msg_data, new_mac_addr); 1187 ether_addr_copy(&msg_data[ETH_ALEN], old_mac_addr); 1188 1189 subcode = is_first ? HCLGE_MBX_MAC_VLAN_UC_ADD : 1190 HCLGE_MBX_MAC_VLAN_UC_MODIFY; 1191 1192 status = hclgevf_send_mbx_msg(hdev, HCLGE_MBX_SET_UNICAST, 1193 subcode, msg_data, sizeof(msg_data), 1194 true, NULL, 0); 1195 if (!status) 1196 ether_addr_copy(hdev->hw.mac.mac_addr, new_mac_addr); 1197 1198 return status; 1199 } 1200 1201 static int hclgevf_add_uc_addr(struct hnae3_handle *handle, 1202 const unsigned char *addr) 1203 { 1204 struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle); 1205 1206 return hclgevf_send_mbx_msg(hdev, HCLGE_MBX_SET_UNICAST, 1207 HCLGE_MBX_MAC_VLAN_UC_ADD, 1208 addr, ETH_ALEN, false, NULL, 0); 1209 } 1210 1211 static int hclgevf_rm_uc_addr(struct hnae3_handle *handle, 1212 const unsigned char *addr) 1213 { 1214 struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle); 1215 1216 return hclgevf_send_mbx_msg(hdev, HCLGE_MBX_SET_UNICAST, 1217 HCLGE_MBX_MAC_VLAN_UC_REMOVE, 1218 addr, ETH_ALEN, false, NULL, 0); 1219 } 1220 1221 static int hclgevf_add_mc_addr(struct hnae3_handle *handle, 1222 const unsigned char *addr) 1223 { 1224 struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle); 1225 1226 return hclgevf_send_mbx_msg(hdev, HCLGE_MBX_SET_MULTICAST, 1227 HCLGE_MBX_MAC_VLAN_MC_ADD, 1228 addr, ETH_ALEN, false, NULL, 0); 1229 } 1230 1231 static int hclgevf_rm_mc_addr(struct hnae3_handle *handle, 1232 const unsigned char *addr) 1233 { 1234 struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle); 1235 1236 return hclgevf_send_mbx_msg(hdev, HCLGE_MBX_SET_MULTICAST, 1237 HCLGE_MBX_MAC_VLAN_MC_REMOVE, 1238 addr, ETH_ALEN, false, NULL, 0); 1239 } 1240 1241 static int hclgevf_set_vlan_filter(struct hnae3_handle *handle, 1242 __be16 proto, u16 vlan_id, 1243 bool is_kill) 1244 { 1245 #define HCLGEVF_VLAN_MBX_MSG_LEN 5 1246 struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle); 1247 u8 msg_data[HCLGEVF_VLAN_MBX_MSG_LEN]; 1248 int ret; 1249 1250 if (vlan_id > HCLGEVF_MAX_VLAN_ID) 1251 return -EINVAL; 1252 1253 if (proto != htons(ETH_P_8021Q)) 1254 return -EPROTONOSUPPORT; 1255 1256 /* When device is resetting, firmware is unable to handle 1257 * mailbox. Just record the vlan id, and remove it after 1258 * reset finished. 1259 */ 1260 if (test_bit(HCLGEVF_STATE_RST_HANDLING, &hdev->state) && is_kill) { 1261 set_bit(vlan_id, hdev->vlan_del_fail_bmap); 1262 return -EBUSY; 1263 } 1264 1265 msg_data[0] = is_kill; 1266 memcpy(&msg_data[1], &vlan_id, sizeof(vlan_id)); 1267 memcpy(&msg_data[3], &proto, sizeof(proto)); 1268 ret = hclgevf_send_mbx_msg(hdev, HCLGE_MBX_SET_VLAN, 1269 HCLGE_MBX_VLAN_FILTER, msg_data, 1270 HCLGEVF_VLAN_MBX_MSG_LEN, false, NULL, 0); 1271 1272 /* when remove hw vlan filter failed, record the vlan id, 1273 * and try to remove it from hw later, to be consistence 1274 * with stack. 1275 */ 1276 if (is_kill && ret) 1277 set_bit(vlan_id, hdev->vlan_del_fail_bmap); 1278 1279 return ret; 1280 } 1281 1282 static void hclgevf_sync_vlan_filter(struct hclgevf_dev *hdev) 1283 { 1284 #define HCLGEVF_MAX_SYNC_COUNT 60 1285 struct hnae3_handle *handle = &hdev->nic; 1286 int ret, sync_cnt = 0; 1287 u16 vlan_id; 1288 1289 vlan_id = find_first_bit(hdev->vlan_del_fail_bmap, VLAN_N_VID); 1290 while (vlan_id != VLAN_N_VID) { 1291 ret = hclgevf_set_vlan_filter(handle, htons(ETH_P_8021Q), 1292 vlan_id, true); 1293 if (ret) 1294 return; 1295 1296 clear_bit(vlan_id, hdev->vlan_del_fail_bmap); 1297 sync_cnt++; 1298 if (sync_cnt >= HCLGEVF_MAX_SYNC_COUNT) 1299 return; 1300 1301 vlan_id = find_first_bit(hdev->vlan_del_fail_bmap, VLAN_N_VID); 1302 } 1303 } 1304 1305 static int hclgevf_en_hw_strip_rxvtag(struct hnae3_handle *handle, bool enable) 1306 { 1307 struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle); 1308 u8 msg_data; 1309 1310 msg_data = enable ? 1 : 0; 1311 return hclgevf_send_mbx_msg(hdev, HCLGE_MBX_SET_VLAN, 1312 HCLGE_MBX_VLAN_RX_OFF_CFG, &msg_data, 1313 1, false, NULL, 0); 1314 } 1315 1316 static int hclgevf_reset_tqp(struct hnae3_handle *handle, u16 queue_id) 1317 { 1318 struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle); 1319 u8 msg_data[2]; 1320 int ret; 1321 1322 memcpy(msg_data, &queue_id, sizeof(queue_id)); 1323 1324 /* disable vf queue before send queue reset msg to PF */ 1325 ret = hclgevf_tqp_enable(hdev, queue_id, 0, false); 1326 if (ret) 1327 return ret; 1328 1329 return hclgevf_send_mbx_msg(hdev, HCLGE_MBX_QUEUE_RESET, 0, msg_data, 1330 sizeof(msg_data), true, NULL, 0); 1331 } 1332 1333 static int hclgevf_set_mtu(struct hnae3_handle *handle, int new_mtu) 1334 { 1335 struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle); 1336 1337 return hclgevf_send_mbx_msg(hdev, HCLGE_MBX_SET_MTU, 0, (u8 *)&new_mtu, 1338 sizeof(new_mtu), true, NULL, 0); 1339 } 1340 1341 static int hclgevf_notify_client(struct hclgevf_dev *hdev, 1342 enum hnae3_reset_notify_type type) 1343 { 1344 struct hnae3_client *client = hdev->nic_client; 1345 struct hnae3_handle *handle = &hdev->nic; 1346 int ret; 1347 1348 if (!test_bit(HCLGEVF_STATE_NIC_REGISTERED, &hdev->state) || 1349 !client) 1350 return 0; 1351 1352 if (!client->ops->reset_notify) 1353 return -EOPNOTSUPP; 1354 1355 ret = client->ops->reset_notify(handle, type); 1356 if (ret) 1357 dev_err(&hdev->pdev->dev, "notify nic client failed %d(%d)\n", 1358 type, ret); 1359 1360 return ret; 1361 } 1362 1363 static void hclgevf_flr_done(struct hnae3_ae_dev *ae_dev) 1364 { 1365 struct hclgevf_dev *hdev = ae_dev->priv; 1366 1367 set_bit(HNAE3_FLR_DONE, &hdev->flr_state); 1368 } 1369 1370 static int hclgevf_flr_poll_timeout(struct hclgevf_dev *hdev, 1371 unsigned long delay_us, 1372 unsigned long wait_cnt) 1373 { 1374 unsigned long cnt = 0; 1375 1376 while (!test_bit(HNAE3_FLR_DONE, &hdev->flr_state) && 1377 cnt++ < wait_cnt) 1378 usleep_range(delay_us, delay_us * 2); 1379 1380 if (!test_bit(HNAE3_FLR_DONE, &hdev->flr_state)) { 1381 dev_err(&hdev->pdev->dev, 1382 "flr wait timeout\n"); 1383 return -ETIMEDOUT; 1384 } 1385 1386 return 0; 1387 } 1388 1389 static int hclgevf_reset_wait(struct hclgevf_dev *hdev) 1390 { 1391 #define HCLGEVF_RESET_WAIT_US 20000 1392 #define HCLGEVF_RESET_WAIT_CNT 2000 1393 #define HCLGEVF_RESET_WAIT_TIMEOUT_US \ 1394 (HCLGEVF_RESET_WAIT_US * HCLGEVF_RESET_WAIT_CNT) 1395 1396 u32 val; 1397 int ret; 1398 1399 if (hdev->reset_type == HNAE3_FLR_RESET) 1400 return hclgevf_flr_poll_timeout(hdev, 1401 HCLGEVF_RESET_WAIT_US, 1402 HCLGEVF_RESET_WAIT_CNT); 1403 else if (hdev->reset_type == HNAE3_VF_RESET) 1404 ret = readl_poll_timeout(hdev->hw.io_base + 1405 HCLGEVF_VF_RST_ING, val, 1406 !(val & HCLGEVF_VF_RST_ING_BIT), 1407 HCLGEVF_RESET_WAIT_US, 1408 HCLGEVF_RESET_WAIT_TIMEOUT_US); 1409 else 1410 ret = readl_poll_timeout(hdev->hw.io_base + 1411 HCLGEVF_RST_ING, val, 1412 !(val & HCLGEVF_RST_ING_BITS), 1413 HCLGEVF_RESET_WAIT_US, 1414 HCLGEVF_RESET_WAIT_TIMEOUT_US); 1415 1416 /* hardware completion status should be available by this time */ 1417 if (ret) { 1418 dev_err(&hdev->pdev->dev, 1419 "could'nt get reset done status from h/w, timeout!\n"); 1420 return ret; 1421 } 1422 1423 /* we will wait a bit more to let reset of the stack to complete. This 1424 * might happen in case reset assertion was made by PF. Yes, this also 1425 * means we might end up waiting bit more even for VF reset. 1426 */ 1427 msleep(5000); 1428 1429 return 0; 1430 } 1431 1432 static void hclgevf_reset_handshake(struct hclgevf_dev *hdev, bool enable) 1433 { 1434 u32 reg_val; 1435 1436 reg_val = hclgevf_read_dev(&hdev->hw, HCLGEVF_NIC_CSQ_DEPTH_REG); 1437 if (enable) 1438 reg_val |= HCLGEVF_NIC_SW_RST_RDY; 1439 else 1440 reg_val &= ~HCLGEVF_NIC_SW_RST_RDY; 1441 1442 hclgevf_write_dev(&hdev->hw, HCLGEVF_NIC_CSQ_DEPTH_REG, 1443 reg_val); 1444 } 1445 1446 static int hclgevf_reset_stack(struct hclgevf_dev *hdev) 1447 { 1448 int ret; 1449 1450 /* uninitialize the nic client */ 1451 ret = hclgevf_notify_client(hdev, HNAE3_UNINIT_CLIENT); 1452 if (ret) 1453 return ret; 1454 1455 /* re-initialize the hclge device */ 1456 ret = hclgevf_reset_hdev(hdev); 1457 if (ret) { 1458 dev_err(&hdev->pdev->dev, 1459 "hclge device re-init failed, VF is disabled!\n"); 1460 return ret; 1461 } 1462 1463 /* bring up the nic client again */ 1464 ret = hclgevf_notify_client(hdev, HNAE3_INIT_CLIENT); 1465 if (ret) 1466 return ret; 1467 1468 ret = hclgevf_notify_client(hdev, HNAE3_RESTORE_CLIENT); 1469 if (ret) 1470 return ret; 1471 1472 /* clear handshake status with IMP */ 1473 hclgevf_reset_handshake(hdev, false); 1474 1475 return 0; 1476 } 1477 1478 static int hclgevf_reset_prepare_wait(struct hclgevf_dev *hdev) 1479 { 1480 #define HCLGEVF_RESET_SYNC_TIME 100 1481 1482 int ret = 0; 1483 1484 switch (hdev->reset_type) { 1485 case HNAE3_VF_FUNC_RESET: 1486 ret = hclgevf_send_mbx_msg(hdev, HCLGE_MBX_RESET, 0, NULL, 1487 0, true, NULL, sizeof(u8)); 1488 hdev->rst_stats.vf_func_rst_cnt++; 1489 break; 1490 case HNAE3_FLR_RESET: 1491 set_bit(HNAE3_FLR_DOWN, &hdev->flr_state); 1492 hdev->rst_stats.flr_rst_cnt++; 1493 break; 1494 default: 1495 break; 1496 } 1497 1498 set_bit(HCLGEVF_STATE_CMD_DISABLE, &hdev->state); 1499 /* inform hardware that preparatory work is done */ 1500 msleep(HCLGEVF_RESET_SYNC_TIME); 1501 hclgevf_reset_handshake(hdev, true); 1502 dev_info(&hdev->pdev->dev, "prepare reset(%d) wait done, ret:%d\n", 1503 hdev->reset_type, ret); 1504 1505 return ret; 1506 } 1507 1508 static void hclgevf_reset_err_handle(struct hclgevf_dev *hdev) 1509 { 1510 /* recover handshake status with IMP when reset fail */ 1511 hclgevf_reset_handshake(hdev, true); 1512 hdev->rst_stats.rst_fail_cnt++; 1513 dev_err(&hdev->pdev->dev, "failed to reset VF(%d)\n", 1514 hdev->rst_stats.rst_fail_cnt); 1515 1516 if (hdev->rst_stats.rst_fail_cnt < HCLGEVF_RESET_MAX_FAIL_CNT) 1517 set_bit(hdev->reset_type, &hdev->reset_pending); 1518 1519 if (hclgevf_is_reset_pending(hdev)) { 1520 set_bit(HCLGEVF_RESET_PENDING, &hdev->reset_state); 1521 hclgevf_reset_task_schedule(hdev); 1522 } 1523 } 1524 1525 static int hclgevf_reset(struct hclgevf_dev *hdev) 1526 { 1527 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(hdev->pdev); 1528 int ret; 1529 1530 /* Initialize ae_dev reset status as well, in case enet layer wants to 1531 * know if device is undergoing reset 1532 */ 1533 ae_dev->reset_type = hdev->reset_type; 1534 hdev->rst_stats.rst_cnt++; 1535 rtnl_lock(); 1536 1537 /* bring down the nic to stop any ongoing TX/RX */ 1538 ret = hclgevf_notify_client(hdev, HNAE3_DOWN_CLIENT); 1539 if (ret) 1540 goto err_reset_lock; 1541 1542 rtnl_unlock(); 1543 1544 ret = hclgevf_reset_prepare_wait(hdev); 1545 if (ret) 1546 goto err_reset; 1547 1548 /* check if VF could successfully fetch the hardware reset completion 1549 * status from the hardware 1550 */ 1551 ret = hclgevf_reset_wait(hdev); 1552 if (ret) { 1553 /* can't do much in this situation, will disable VF */ 1554 dev_err(&hdev->pdev->dev, 1555 "VF failed(=%d) to fetch H/W reset completion status\n", 1556 ret); 1557 goto err_reset; 1558 } 1559 1560 hdev->rst_stats.hw_rst_done_cnt++; 1561 1562 rtnl_lock(); 1563 1564 /* now, re-initialize the nic client and ae device */ 1565 ret = hclgevf_reset_stack(hdev); 1566 if (ret) { 1567 dev_err(&hdev->pdev->dev, "failed to reset VF stack\n"); 1568 goto err_reset_lock; 1569 } 1570 1571 /* bring up the nic to enable TX/RX again */ 1572 ret = hclgevf_notify_client(hdev, HNAE3_UP_CLIENT); 1573 if (ret) 1574 goto err_reset_lock; 1575 1576 rtnl_unlock(); 1577 1578 hdev->last_reset_time = jiffies; 1579 ae_dev->reset_type = HNAE3_NONE_RESET; 1580 hdev->rst_stats.rst_done_cnt++; 1581 hdev->rst_stats.rst_fail_cnt = 0; 1582 1583 return ret; 1584 err_reset_lock: 1585 rtnl_unlock(); 1586 err_reset: 1587 hclgevf_reset_err_handle(hdev); 1588 1589 return ret; 1590 } 1591 1592 static enum hnae3_reset_type hclgevf_get_reset_level(struct hclgevf_dev *hdev, 1593 unsigned long *addr) 1594 { 1595 enum hnae3_reset_type rst_level = HNAE3_NONE_RESET; 1596 1597 /* return the highest priority reset level amongst all */ 1598 if (test_bit(HNAE3_VF_RESET, addr)) { 1599 rst_level = HNAE3_VF_RESET; 1600 clear_bit(HNAE3_VF_RESET, addr); 1601 clear_bit(HNAE3_VF_PF_FUNC_RESET, addr); 1602 clear_bit(HNAE3_VF_FUNC_RESET, addr); 1603 } else if (test_bit(HNAE3_VF_FULL_RESET, addr)) { 1604 rst_level = HNAE3_VF_FULL_RESET; 1605 clear_bit(HNAE3_VF_FULL_RESET, addr); 1606 clear_bit(HNAE3_VF_FUNC_RESET, addr); 1607 } else if (test_bit(HNAE3_VF_PF_FUNC_RESET, addr)) { 1608 rst_level = HNAE3_VF_PF_FUNC_RESET; 1609 clear_bit(HNAE3_VF_PF_FUNC_RESET, addr); 1610 clear_bit(HNAE3_VF_FUNC_RESET, addr); 1611 } else if (test_bit(HNAE3_VF_FUNC_RESET, addr)) { 1612 rst_level = HNAE3_VF_FUNC_RESET; 1613 clear_bit(HNAE3_VF_FUNC_RESET, addr); 1614 } else if (test_bit(HNAE3_FLR_RESET, addr)) { 1615 rst_level = HNAE3_FLR_RESET; 1616 clear_bit(HNAE3_FLR_RESET, addr); 1617 } 1618 1619 return rst_level; 1620 } 1621 1622 static void hclgevf_reset_event(struct pci_dev *pdev, 1623 struct hnae3_handle *handle) 1624 { 1625 struct hnae3_ae_dev *ae_dev = pci_get_drvdata(pdev); 1626 struct hclgevf_dev *hdev = ae_dev->priv; 1627 1628 dev_info(&hdev->pdev->dev, "received reset request from VF enet\n"); 1629 1630 if (hdev->default_reset_request) 1631 hdev->reset_level = 1632 hclgevf_get_reset_level(hdev, 1633 &hdev->default_reset_request); 1634 else 1635 hdev->reset_level = HNAE3_VF_FUNC_RESET; 1636 1637 /* reset of this VF requested */ 1638 set_bit(HCLGEVF_RESET_REQUESTED, &hdev->reset_state); 1639 hclgevf_reset_task_schedule(hdev); 1640 1641 hdev->last_reset_time = jiffies; 1642 } 1643 1644 static void hclgevf_set_def_reset_request(struct hnae3_ae_dev *ae_dev, 1645 enum hnae3_reset_type rst_type) 1646 { 1647 struct hclgevf_dev *hdev = ae_dev->priv; 1648 1649 set_bit(rst_type, &hdev->default_reset_request); 1650 } 1651 1652 static void hclgevf_flr_prepare(struct hnae3_ae_dev *ae_dev) 1653 { 1654 #define HCLGEVF_FLR_WAIT_MS 100 1655 #define HCLGEVF_FLR_WAIT_CNT 50 1656 struct hclgevf_dev *hdev = ae_dev->priv; 1657 int cnt = 0; 1658 1659 clear_bit(HNAE3_FLR_DOWN, &hdev->flr_state); 1660 clear_bit(HNAE3_FLR_DONE, &hdev->flr_state); 1661 set_bit(HNAE3_FLR_RESET, &hdev->default_reset_request); 1662 hclgevf_reset_event(hdev->pdev, NULL); 1663 1664 while (!test_bit(HNAE3_FLR_DOWN, &hdev->flr_state) && 1665 cnt++ < HCLGEVF_FLR_WAIT_CNT) 1666 msleep(HCLGEVF_FLR_WAIT_MS); 1667 1668 if (!test_bit(HNAE3_FLR_DOWN, &hdev->flr_state)) 1669 dev_err(&hdev->pdev->dev, 1670 "flr wait down timeout: %d\n", cnt); 1671 } 1672 1673 static u32 hclgevf_get_fw_version(struct hnae3_handle *handle) 1674 { 1675 struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle); 1676 1677 return hdev->fw_version; 1678 } 1679 1680 static void hclgevf_get_misc_vector(struct hclgevf_dev *hdev) 1681 { 1682 struct hclgevf_misc_vector *vector = &hdev->misc_vector; 1683 1684 vector->vector_irq = pci_irq_vector(hdev->pdev, 1685 HCLGEVF_MISC_VECTOR_NUM); 1686 vector->addr = hdev->hw.io_base + HCLGEVF_MISC_VECTOR_REG_BASE; 1687 /* vector status always valid for Vector 0 */ 1688 hdev->vector_status[HCLGEVF_MISC_VECTOR_NUM] = 0; 1689 hdev->vector_irq[HCLGEVF_MISC_VECTOR_NUM] = vector->vector_irq; 1690 1691 hdev->num_msi_left -= 1; 1692 hdev->num_msi_used += 1; 1693 } 1694 1695 void hclgevf_reset_task_schedule(struct hclgevf_dev *hdev) 1696 { 1697 if (!test_bit(HCLGEVF_STATE_RST_SERVICE_SCHED, &hdev->state) && 1698 !test_bit(HCLGEVF_STATE_REMOVING, &hdev->state)) { 1699 set_bit(HCLGEVF_STATE_RST_SERVICE_SCHED, &hdev->state); 1700 schedule_work(&hdev->rst_service_task); 1701 } 1702 } 1703 1704 void hclgevf_mbx_task_schedule(struct hclgevf_dev *hdev) 1705 { 1706 if (!test_bit(HCLGEVF_STATE_MBX_SERVICE_SCHED, &hdev->state) && 1707 !test_bit(HCLGEVF_STATE_MBX_HANDLING, &hdev->state)) { 1708 set_bit(HCLGEVF_STATE_MBX_SERVICE_SCHED, &hdev->state); 1709 schedule_work(&hdev->mbx_service_task); 1710 } 1711 } 1712 1713 static void hclgevf_task_schedule(struct hclgevf_dev *hdev) 1714 { 1715 if (!test_bit(HCLGEVF_STATE_DOWN, &hdev->state) && 1716 !test_and_set_bit(HCLGEVF_STATE_SERVICE_SCHED, &hdev->state)) 1717 schedule_work(&hdev->service_task); 1718 } 1719 1720 static void hclgevf_deferred_task_schedule(struct hclgevf_dev *hdev) 1721 { 1722 /* if we have any pending mailbox event then schedule the mbx task */ 1723 if (hdev->mbx_event_pending) 1724 hclgevf_mbx_task_schedule(hdev); 1725 1726 if (test_bit(HCLGEVF_RESET_PENDING, &hdev->reset_state)) 1727 hclgevf_reset_task_schedule(hdev); 1728 } 1729 1730 static void hclgevf_service_timer(struct timer_list *t) 1731 { 1732 struct hclgevf_dev *hdev = from_timer(hdev, t, service_timer); 1733 1734 mod_timer(&hdev->service_timer, jiffies + 1735 HCLGEVF_GENERAL_TASK_INTERVAL * HZ); 1736 1737 hdev->stats_timer++; 1738 hclgevf_task_schedule(hdev); 1739 } 1740 1741 static void hclgevf_reset_service_task(struct work_struct *work) 1742 { 1743 struct hclgevf_dev *hdev = 1744 container_of(work, struct hclgevf_dev, rst_service_task); 1745 int ret; 1746 1747 if (test_and_set_bit(HCLGEVF_STATE_RST_HANDLING, &hdev->state)) 1748 return; 1749 1750 clear_bit(HCLGEVF_STATE_RST_SERVICE_SCHED, &hdev->state); 1751 1752 if (test_and_clear_bit(HCLGEVF_RESET_PENDING, 1753 &hdev->reset_state)) { 1754 /* PF has initmated that it is about to reset the hardware. 1755 * We now have to poll & check if hardware has actually 1756 * completed the reset sequence. On hardware reset completion, 1757 * VF needs to reset the client and ae device. 1758 */ 1759 hdev->reset_attempts = 0; 1760 1761 hdev->last_reset_time = jiffies; 1762 while ((hdev->reset_type = 1763 hclgevf_get_reset_level(hdev, &hdev->reset_pending)) 1764 != HNAE3_NONE_RESET) { 1765 ret = hclgevf_reset(hdev); 1766 if (ret) 1767 dev_err(&hdev->pdev->dev, 1768 "VF stack reset failed %d.\n", ret); 1769 } 1770 } else if (test_and_clear_bit(HCLGEVF_RESET_REQUESTED, 1771 &hdev->reset_state)) { 1772 /* we could be here when either of below happens: 1773 * 1. reset was initiated due to watchdog timeout caused by 1774 * a. IMP was earlier reset and our TX got choked down and 1775 * which resulted in watchdog reacting and inducing VF 1776 * reset. This also means our cmdq would be unreliable. 1777 * b. problem in TX due to other lower layer(example link 1778 * layer not functioning properly etc.) 1779 * 2. VF reset might have been initiated due to some config 1780 * change. 1781 * 1782 * NOTE: Theres no clear way to detect above cases than to react 1783 * to the response of PF for this reset request. PF will ack the 1784 * 1b and 2. cases but we will not get any intimation about 1a 1785 * from PF as cmdq would be in unreliable state i.e. mailbox 1786 * communication between PF and VF would be broken. 1787 * 1788 * if we are never geting into pending state it means either: 1789 * 1. PF is not receiving our request which could be due to IMP 1790 * reset 1791 * 2. PF is screwed 1792 * We cannot do much for 2. but to check first we can try reset 1793 * our PCIe + stack and see if it alleviates the problem. 1794 */ 1795 if (hdev->reset_attempts > 3) { 1796 /* prepare for full reset of stack + pcie interface */ 1797 set_bit(HNAE3_VF_FULL_RESET, &hdev->reset_pending); 1798 1799 /* "defer" schedule the reset task again */ 1800 set_bit(HCLGEVF_RESET_PENDING, &hdev->reset_state); 1801 } else { 1802 hdev->reset_attempts++; 1803 1804 set_bit(hdev->reset_level, &hdev->reset_pending); 1805 set_bit(HCLGEVF_RESET_PENDING, &hdev->reset_state); 1806 } 1807 hclgevf_reset_task_schedule(hdev); 1808 } 1809 1810 clear_bit(HCLGEVF_STATE_RST_HANDLING, &hdev->state); 1811 } 1812 1813 static void hclgevf_mailbox_service_task(struct work_struct *work) 1814 { 1815 struct hclgevf_dev *hdev; 1816 1817 hdev = container_of(work, struct hclgevf_dev, mbx_service_task); 1818 1819 if (test_and_set_bit(HCLGEVF_STATE_MBX_HANDLING, &hdev->state)) 1820 return; 1821 1822 clear_bit(HCLGEVF_STATE_MBX_SERVICE_SCHED, &hdev->state); 1823 1824 hclgevf_mbx_async_handler(hdev); 1825 1826 clear_bit(HCLGEVF_STATE_MBX_HANDLING, &hdev->state); 1827 } 1828 1829 static void hclgevf_keep_alive_timer(struct timer_list *t) 1830 { 1831 struct hclgevf_dev *hdev = from_timer(hdev, t, keep_alive_timer); 1832 1833 schedule_work(&hdev->keep_alive_task); 1834 mod_timer(&hdev->keep_alive_timer, jiffies + 1835 HCLGEVF_KEEP_ALIVE_TASK_INTERVAL * HZ); 1836 } 1837 1838 static void hclgevf_keep_alive_task(struct work_struct *work) 1839 { 1840 struct hclgevf_dev *hdev; 1841 u8 respmsg; 1842 int ret; 1843 1844 hdev = container_of(work, struct hclgevf_dev, keep_alive_task); 1845 1846 if (test_bit(HCLGEVF_STATE_CMD_DISABLE, &hdev->state)) 1847 return; 1848 1849 ret = hclgevf_send_mbx_msg(hdev, HCLGE_MBX_KEEP_ALIVE, 0, NULL, 1850 0, false, &respmsg, sizeof(respmsg)); 1851 if (ret) 1852 dev_err(&hdev->pdev->dev, 1853 "VF sends keep alive cmd failed(=%d)\n", ret); 1854 } 1855 1856 static void hclgevf_service_task(struct work_struct *work) 1857 { 1858 struct hnae3_handle *handle; 1859 struct hclgevf_dev *hdev; 1860 1861 hdev = container_of(work, struct hclgevf_dev, service_task); 1862 handle = &hdev->nic; 1863 1864 if (hdev->stats_timer >= HCLGEVF_STATS_TIMER_INTERVAL) { 1865 hclgevf_tqps_update_stats(handle); 1866 hdev->stats_timer = 0; 1867 } 1868 1869 /* request the link status from the PF. PF would be able to tell VF 1870 * about such updates in future so we might remove this later 1871 */ 1872 hclgevf_request_link_info(hdev); 1873 1874 hclgevf_update_link_mode(hdev); 1875 1876 hclgevf_sync_vlan_filter(hdev); 1877 1878 hclgevf_deferred_task_schedule(hdev); 1879 1880 clear_bit(HCLGEVF_STATE_SERVICE_SCHED, &hdev->state); 1881 } 1882 1883 static void hclgevf_clear_event_cause(struct hclgevf_dev *hdev, u32 regclr) 1884 { 1885 hclgevf_write_dev(&hdev->hw, HCLGEVF_VECTOR0_CMDQ_SRC_REG, regclr); 1886 } 1887 1888 static enum hclgevf_evt_cause hclgevf_check_evt_cause(struct hclgevf_dev *hdev, 1889 u32 *clearval) 1890 { 1891 u32 val, cmdq_stat_reg, rst_ing_reg; 1892 1893 /* fetch the events from their corresponding regs */ 1894 cmdq_stat_reg = hclgevf_read_dev(&hdev->hw, 1895 HCLGEVF_VECTOR0_CMDQ_STAT_REG); 1896 1897 if (BIT(HCLGEVF_VECTOR0_RST_INT_B) & cmdq_stat_reg) { 1898 rst_ing_reg = hclgevf_read_dev(&hdev->hw, HCLGEVF_RST_ING); 1899 dev_info(&hdev->pdev->dev, 1900 "receive reset interrupt 0x%x!\n", rst_ing_reg); 1901 set_bit(HNAE3_VF_RESET, &hdev->reset_pending); 1902 set_bit(HCLGEVF_RESET_PENDING, &hdev->reset_state); 1903 set_bit(HCLGEVF_STATE_CMD_DISABLE, &hdev->state); 1904 *clearval = ~(1U << HCLGEVF_VECTOR0_RST_INT_B); 1905 hdev->rst_stats.vf_rst_cnt++; 1906 /* set up VF hardware reset status, its PF will clear 1907 * this status when PF has initialized done. 1908 */ 1909 val = hclgevf_read_dev(&hdev->hw, HCLGEVF_VF_RST_ING); 1910 hclgevf_write_dev(&hdev->hw, HCLGEVF_VF_RST_ING, 1911 val | HCLGEVF_VF_RST_ING_BIT); 1912 return HCLGEVF_VECTOR0_EVENT_RST; 1913 } 1914 1915 /* check for vector0 mailbox(=CMDQ RX) event source */ 1916 if (BIT(HCLGEVF_VECTOR0_RX_CMDQ_INT_B) & cmdq_stat_reg) { 1917 /* for revision 0x21, clearing interrupt is writing bit 0 1918 * to the clear register, writing bit 1 means to keep the 1919 * old value. 1920 * for revision 0x20, the clear register is a read & write 1921 * register, so we should just write 0 to the bit we are 1922 * handling, and keep other bits as cmdq_stat_reg. 1923 */ 1924 if (hdev->pdev->revision >= 0x21) 1925 *clearval = ~(1U << HCLGEVF_VECTOR0_RX_CMDQ_INT_B); 1926 else 1927 *clearval = cmdq_stat_reg & 1928 ~BIT(HCLGEVF_VECTOR0_RX_CMDQ_INT_B); 1929 1930 return HCLGEVF_VECTOR0_EVENT_MBX; 1931 } 1932 1933 dev_dbg(&hdev->pdev->dev, "vector 0 interrupt from unknown source\n"); 1934 1935 return HCLGEVF_VECTOR0_EVENT_OTHER; 1936 } 1937 1938 static void hclgevf_enable_vector(struct hclgevf_misc_vector *vector, bool en) 1939 { 1940 writel(en ? 1 : 0, vector->addr); 1941 } 1942 1943 static irqreturn_t hclgevf_misc_irq_handle(int irq, void *data) 1944 { 1945 enum hclgevf_evt_cause event_cause; 1946 struct hclgevf_dev *hdev = data; 1947 u32 clearval; 1948 1949 hclgevf_enable_vector(&hdev->misc_vector, false); 1950 event_cause = hclgevf_check_evt_cause(hdev, &clearval); 1951 1952 switch (event_cause) { 1953 case HCLGEVF_VECTOR0_EVENT_RST: 1954 hclgevf_reset_task_schedule(hdev); 1955 break; 1956 case HCLGEVF_VECTOR0_EVENT_MBX: 1957 hclgevf_mbx_handler(hdev); 1958 break; 1959 default: 1960 break; 1961 } 1962 1963 if (event_cause != HCLGEVF_VECTOR0_EVENT_OTHER) { 1964 hclgevf_clear_event_cause(hdev, clearval); 1965 hclgevf_enable_vector(&hdev->misc_vector, true); 1966 } 1967 1968 return IRQ_HANDLED; 1969 } 1970 1971 static int hclgevf_configure(struct hclgevf_dev *hdev) 1972 { 1973 int ret; 1974 1975 /* get current port based vlan state from PF */ 1976 ret = hclgevf_get_port_base_vlan_filter_state(hdev); 1977 if (ret) 1978 return ret; 1979 1980 /* get queue configuration from PF */ 1981 ret = hclgevf_get_queue_info(hdev); 1982 if (ret) 1983 return ret; 1984 1985 /* get queue depth info from PF */ 1986 ret = hclgevf_get_queue_depth(hdev); 1987 if (ret) 1988 return ret; 1989 1990 ret = hclgevf_get_pf_media_type(hdev); 1991 if (ret) 1992 return ret; 1993 1994 /* get tc configuration from PF */ 1995 return hclgevf_get_tc_info(hdev); 1996 } 1997 1998 static int hclgevf_alloc_hdev(struct hnae3_ae_dev *ae_dev) 1999 { 2000 struct pci_dev *pdev = ae_dev->pdev; 2001 struct hclgevf_dev *hdev; 2002 2003 hdev = devm_kzalloc(&pdev->dev, sizeof(*hdev), GFP_KERNEL); 2004 if (!hdev) 2005 return -ENOMEM; 2006 2007 hdev->pdev = pdev; 2008 hdev->ae_dev = ae_dev; 2009 ae_dev->priv = hdev; 2010 2011 return 0; 2012 } 2013 2014 static int hclgevf_init_roce_base_info(struct hclgevf_dev *hdev) 2015 { 2016 struct hnae3_handle *roce = &hdev->roce; 2017 struct hnae3_handle *nic = &hdev->nic; 2018 2019 roce->rinfo.num_vectors = hdev->num_roce_msix; 2020 2021 if (hdev->num_msi_left < roce->rinfo.num_vectors || 2022 hdev->num_msi_left == 0) 2023 return -EINVAL; 2024 2025 roce->rinfo.base_vector = hdev->roce_base_vector; 2026 2027 roce->rinfo.netdev = nic->kinfo.netdev; 2028 roce->rinfo.roce_io_base = hdev->hw.io_base; 2029 2030 roce->pdev = nic->pdev; 2031 roce->ae_algo = nic->ae_algo; 2032 roce->numa_node_mask = nic->numa_node_mask; 2033 2034 return 0; 2035 } 2036 2037 static int hclgevf_config_gro(struct hclgevf_dev *hdev, bool en) 2038 { 2039 struct hclgevf_cfg_gro_status_cmd *req; 2040 struct hclgevf_desc desc; 2041 int ret; 2042 2043 if (!hnae3_dev_gro_supported(hdev)) 2044 return 0; 2045 2046 hclgevf_cmd_setup_basic_desc(&desc, HCLGEVF_OPC_GRO_GENERIC_CONFIG, 2047 false); 2048 req = (struct hclgevf_cfg_gro_status_cmd *)desc.data; 2049 2050 req->gro_en = cpu_to_le16(en ? 1 : 0); 2051 2052 ret = hclgevf_cmd_send(&hdev->hw, &desc, 1); 2053 if (ret) 2054 dev_err(&hdev->pdev->dev, 2055 "VF GRO hardware config cmd failed, ret = %d.\n", ret); 2056 2057 return ret; 2058 } 2059 2060 static int hclgevf_rss_init_hw(struct hclgevf_dev *hdev) 2061 { 2062 struct hclgevf_rss_cfg *rss_cfg = &hdev->rss_cfg; 2063 int ret; 2064 u32 i; 2065 2066 rss_cfg->rss_size = hdev->nic.kinfo.rss_size; 2067 2068 if (hdev->pdev->revision >= 0x21) { 2069 rss_cfg->hash_algo = HCLGEVF_RSS_HASH_ALGO_SIMPLE; 2070 memcpy(rss_cfg->rss_hash_key, hclgevf_hash_key, 2071 HCLGEVF_RSS_KEY_SIZE); 2072 2073 ret = hclgevf_set_rss_algo_key(hdev, rss_cfg->hash_algo, 2074 rss_cfg->rss_hash_key); 2075 if (ret) 2076 return ret; 2077 2078 rss_cfg->rss_tuple_sets.ipv4_tcp_en = 2079 HCLGEVF_RSS_INPUT_TUPLE_OTHER; 2080 rss_cfg->rss_tuple_sets.ipv4_udp_en = 2081 HCLGEVF_RSS_INPUT_TUPLE_OTHER; 2082 rss_cfg->rss_tuple_sets.ipv4_sctp_en = 2083 HCLGEVF_RSS_INPUT_TUPLE_SCTP; 2084 rss_cfg->rss_tuple_sets.ipv4_fragment_en = 2085 HCLGEVF_RSS_INPUT_TUPLE_OTHER; 2086 rss_cfg->rss_tuple_sets.ipv6_tcp_en = 2087 HCLGEVF_RSS_INPUT_TUPLE_OTHER; 2088 rss_cfg->rss_tuple_sets.ipv6_udp_en = 2089 HCLGEVF_RSS_INPUT_TUPLE_OTHER; 2090 rss_cfg->rss_tuple_sets.ipv6_sctp_en = 2091 HCLGEVF_RSS_INPUT_TUPLE_SCTP; 2092 rss_cfg->rss_tuple_sets.ipv6_fragment_en = 2093 HCLGEVF_RSS_INPUT_TUPLE_OTHER; 2094 2095 ret = hclgevf_set_rss_input_tuple(hdev, rss_cfg); 2096 if (ret) 2097 return ret; 2098 2099 } 2100 2101 /* Initialize RSS indirect table */ 2102 for (i = 0; i < HCLGEVF_RSS_IND_TBL_SIZE; i++) 2103 rss_cfg->rss_indirection_tbl[i] = i % rss_cfg->rss_size; 2104 2105 ret = hclgevf_set_rss_indir_table(hdev); 2106 if (ret) 2107 return ret; 2108 2109 return hclgevf_set_rss_tc_mode(hdev, rss_cfg->rss_size); 2110 } 2111 2112 static int hclgevf_init_vlan_config(struct hclgevf_dev *hdev) 2113 { 2114 return hclgevf_set_vlan_filter(&hdev->nic, htons(ETH_P_8021Q), 0, 2115 false); 2116 } 2117 2118 static void hclgevf_set_timer_task(struct hnae3_handle *handle, bool enable) 2119 { 2120 struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle); 2121 2122 if (enable) { 2123 mod_timer(&hdev->service_timer, jiffies + HZ); 2124 } else { 2125 del_timer_sync(&hdev->service_timer); 2126 cancel_work_sync(&hdev->service_task); 2127 clear_bit(HCLGEVF_STATE_SERVICE_SCHED, &hdev->state); 2128 } 2129 } 2130 2131 static int hclgevf_ae_start(struct hnae3_handle *handle) 2132 { 2133 struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle); 2134 2135 hclgevf_reset_tqp_stats(handle); 2136 2137 hclgevf_request_link_info(hdev); 2138 2139 hclgevf_update_link_mode(hdev); 2140 2141 clear_bit(HCLGEVF_STATE_DOWN, &hdev->state); 2142 2143 return 0; 2144 } 2145 2146 static void hclgevf_ae_stop(struct hnae3_handle *handle) 2147 { 2148 struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle); 2149 int i; 2150 2151 set_bit(HCLGEVF_STATE_DOWN, &hdev->state); 2152 2153 if (hdev->reset_type != HNAE3_VF_RESET) 2154 for (i = 0; i < handle->kinfo.num_tqps; i++) 2155 if (hclgevf_reset_tqp(handle, i)) 2156 break; 2157 2158 hclgevf_reset_tqp_stats(handle); 2159 hclgevf_update_link_status(hdev, 0); 2160 } 2161 2162 static int hclgevf_set_alive(struct hnae3_handle *handle, bool alive) 2163 { 2164 struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle); 2165 u8 msg_data; 2166 2167 msg_data = alive ? 1 : 0; 2168 return hclgevf_send_mbx_msg(hdev, HCLGE_MBX_SET_ALIVE, 2169 0, &msg_data, 1, false, NULL, 0); 2170 } 2171 2172 static int hclgevf_client_start(struct hnae3_handle *handle) 2173 { 2174 struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle); 2175 int ret; 2176 2177 ret = hclgevf_set_alive(handle, true); 2178 if (ret) 2179 return ret; 2180 2181 mod_timer(&hdev->keep_alive_timer, jiffies + 2182 HCLGEVF_KEEP_ALIVE_TASK_INTERVAL * HZ); 2183 2184 return 0; 2185 } 2186 2187 static void hclgevf_client_stop(struct hnae3_handle *handle) 2188 { 2189 struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle); 2190 int ret; 2191 2192 ret = hclgevf_set_alive(handle, false); 2193 if (ret) 2194 dev_warn(&hdev->pdev->dev, 2195 "%s failed %d\n", __func__, ret); 2196 2197 del_timer_sync(&hdev->keep_alive_timer); 2198 cancel_work_sync(&hdev->keep_alive_task); 2199 } 2200 2201 static void hclgevf_state_init(struct hclgevf_dev *hdev) 2202 { 2203 /* setup tasks for the MBX */ 2204 INIT_WORK(&hdev->mbx_service_task, hclgevf_mailbox_service_task); 2205 clear_bit(HCLGEVF_STATE_MBX_SERVICE_SCHED, &hdev->state); 2206 clear_bit(HCLGEVF_STATE_MBX_HANDLING, &hdev->state); 2207 2208 /* setup tasks for service timer */ 2209 timer_setup(&hdev->service_timer, hclgevf_service_timer, 0); 2210 2211 INIT_WORK(&hdev->service_task, hclgevf_service_task); 2212 clear_bit(HCLGEVF_STATE_SERVICE_SCHED, &hdev->state); 2213 2214 INIT_WORK(&hdev->rst_service_task, hclgevf_reset_service_task); 2215 2216 mutex_init(&hdev->mbx_resp.mbx_mutex); 2217 2218 /* bring the device down */ 2219 set_bit(HCLGEVF_STATE_DOWN, &hdev->state); 2220 } 2221 2222 static void hclgevf_state_uninit(struct hclgevf_dev *hdev) 2223 { 2224 set_bit(HCLGEVF_STATE_DOWN, &hdev->state); 2225 set_bit(HCLGEVF_STATE_REMOVING, &hdev->state); 2226 2227 if (hdev->keep_alive_timer.function) 2228 del_timer_sync(&hdev->keep_alive_timer); 2229 if (hdev->keep_alive_task.func) 2230 cancel_work_sync(&hdev->keep_alive_task); 2231 if (hdev->service_timer.function) 2232 del_timer_sync(&hdev->service_timer); 2233 if (hdev->service_task.func) 2234 cancel_work_sync(&hdev->service_task); 2235 if (hdev->mbx_service_task.func) 2236 cancel_work_sync(&hdev->mbx_service_task); 2237 if (hdev->rst_service_task.func) 2238 cancel_work_sync(&hdev->rst_service_task); 2239 2240 mutex_destroy(&hdev->mbx_resp.mbx_mutex); 2241 } 2242 2243 static int hclgevf_init_msi(struct hclgevf_dev *hdev) 2244 { 2245 struct pci_dev *pdev = hdev->pdev; 2246 int vectors; 2247 int i; 2248 2249 if (hnae3_get_bit(hdev->ae_dev->flag, HNAE3_DEV_SUPPORT_ROCE_B)) 2250 vectors = pci_alloc_irq_vectors(pdev, 2251 hdev->roce_base_msix_offset + 1, 2252 hdev->num_msi, 2253 PCI_IRQ_MSIX); 2254 else 2255 vectors = pci_alloc_irq_vectors(pdev, 1, hdev->num_msi, 2256 PCI_IRQ_MSI | PCI_IRQ_MSIX); 2257 2258 if (vectors < 0) { 2259 dev_err(&pdev->dev, 2260 "failed(%d) to allocate MSI/MSI-X vectors\n", 2261 vectors); 2262 return vectors; 2263 } 2264 if (vectors < hdev->num_msi) 2265 dev_warn(&hdev->pdev->dev, 2266 "requested %d MSI/MSI-X, but allocated %d MSI/MSI-X\n", 2267 hdev->num_msi, vectors); 2268 2269 hdev->num_msi = vectors; 2270 hdev->num_msi_left = vectors; 2271 hdev->base_msi_vector = pdev->irq; 2272 hdev->roce_base_vector = pdev->irq + hdev->roce_base_msix_offset; 2273 2274 hdev->vector_status = devm_kcalloc(&pdev->dev, hdev->num_msi, 2275 sizeof(u16), GFP_KERNEL); 2276 if (!hdev->vector_status) { 2277 pci_free_irq_vectors(pdev); 2278 return -ENOMEM; 2279 } 2280 2281 for (i = 0; i < hdev->num_msi; i++) 2282 hdev->vector_status[i] = HCLGEVF_INVALID_VPORT; 2283 2284 hdev->vector_irq = devm_kcalloc(&pdev->dev, hdev->num_msi, 2285 sizeof(int), GFP_KERNEL); 2286 if (!hdev->vector_irq) { 2287 devm_kfree(&pdev->dev, hdev->vector_status); 2288 pci_free_irq_vectors(pdev); 2289 return -ENOMEM; 2290 } 2291 2292 return 0; 2293 } 2294 2295 static void hclgevf_uninit_msi(struct hclgevf_dev *hdev) 2296 { 2297 struct pci_dev *pdev = hdev->pdev; 2298 2299 devm_kfree(&pdev->dev, hdev->vector_status); 2300 devm_kfree(&pdev->dev, hdev->vector_irq); 2301 pci_free_irq_vectors(pdev); 2302 } 2303 2304 static int hclgevf_misc_irq_init(struct hclgevf_dev *hdev) 2305 { 2306 int ret; 2307 2308 hclgevf_get_misc_vector(hdev); 2309 2310 ret = request_irq(hdev->misc_vector.vector_irq, hclgevf_misc_irq_handle, 2311 0, "hclgevf_cmd", hdev); 2312 if (ret) { 2313 dev_err(&hdev->pdev->dev, "VF failed to request misc irq(%d)\n", 2314 hdev->misc_vector.vector_irq); 2315 return ret; 2316 } 2317 2318 hclgevf_clear_event_cause(hdev, 0); 2319 2320 /* enable misc. vector(vector 0) */ 2321 hclgevf_enable_vector(&hdev->misc_vector, true); 2322 2323 return ret; 2324 } 2325 2326 static void hclgevf_misc_irq_uninit(struct hclgevf_dev *hdev) 2327 { 2328 /* disable misc vector(vector 0) */ 2329 hclgevf_enable_vector(&hdev->misc_vector, false); 2330 synchronize_irq(hdev->misc_vector.vector_irq); 2331 free_irq(hdev->misc_vector.vector_irq, hdev); 2332 hclgevf_free_vector(hdev, 0); 2333 } 2334 2335 static void hclgevf_info_show(struct hclgevf_dev *hdev) 2336 { 2337 struct device *dev = &hdev->pdev->dev; 2338 2339 dev_info(dev, "VF info begin:\n"); 2340 2341 dev_info(dev, "Task queue pairs numbers: %d\n", hdev->num_tqps); 2342 dev_info(dev, "Desc num per TX queue: %d\n", hdev->num_tx_desc); 2343 dev_info(dev, "Desc num per RX queue: %d\n", hdev->num_rx_desc); 2344 dev_info(dev, "Numbers of vports: %d\n", hdev->num_alloc_vport); 2345 dev_info(dev, "HW tc map: %d\n", hdev->hw_tc_map); 2346 dev_info(dev, "PF media type of this VF: %d\n", 2347 hdev->hw.mac.media_type); 2348 2349 dev_info(dev, "VF info end.\n"); 2350 } 2351 2352 static int hclgevf_init_nic_client_instance(struct hnae3_ae_dev *ae_dev, 2353 struct hnae3_client *client) 2354 { 2355 struct hclgevf_dev *hdev = ae_dev->priv; 2356 int ret; 2357 2358 ret = client->ops->init_instance(&hdev->nic); 2359 if (ret) 2360 return ret; 2361 2362 set_bit(HCLGEVF_STATE_NIC_REGISTERED, &hdev->state); 2363 hnae3_set_client_init_flag(client, ae_dev, 1); 2364 2365 if (netif_msg_drv(&hdev->nic)) 2366 hclgevf_info_show(hdev); 2367 2368 return 0; 2369 } 2370 2371 static int hclgevf_init_roce_client_instance(struct hnae3_ae_dev *ae_dev, 2372 struct hnae3_client *client) 2373 { 2374 struct hclgevf_dev *hdev = ae_dev->priv; 2375 int ret; 2376 2377 if (!hnae3_dev_roce_supported(hdev) || !hdev->roce_client || 2378 !hdev->nic_client) 2379 return 0; 2380 2381 ret = hclgevf_init_roce_base_info(hdev); 2382 if (ret) 2383 return ret; 2384 2385 ret = client->ops->init_instance(&hdev->roce); 2386 if (ret) 2387 return ret; 2388 2389 hnae3_set_client_init_flag(client, ae_dev, 1); 2390 2391 return 0; 2392 } 2393 2394 static int hclgevf_init_client_instance(struct hnae3_client *client, 2395 struct hnae3_ae_dev *ae_dev) 2396 { 2397 struct hclgevf_dev *hdev = ae_dev->priv; 2398 int ret; 2399 2400 switch (client->type) { 2401 case HNAE3_CLIENT_KNIC: 2402 hdev->nic_client = client; 2403 hdev->nic.client = client; 2404 2405 ret = hclgevf_init_nic_client_instance(ae_dev, client); 2406 if (ret) 2407 goto clear_nic; 2408 2409 ret = hclgevf_init_roce_client_instance(ae_dev, 2410 hdev->roce_client); 2411 if (ret) 2412 goto clear_roce; 2413 2414 break; 2415 case HNAE3_CLIENT_ROCE: 2416 if (hnae3_dev_roce_supported(hdev)) { 2417 hdev->roce_client = client; 2418 hdev->roce.client = client; 2419 } 2420 2421 ret = hclgevf_init_roce_client_instance(ae_dev, client); 2422 if (ret) 2423 goto clear_roce; 2424 2425 break; 2426 default: 2427 return -EINVAL; 2428 } 2429 2430 return 0; 2431 2432 clear_nic: 2433 hdev->nic_client = NULL; 2434 hdev->nic.client = NULL; 2435 return ret; 2436 clear_roce: 2437 hdev->roce_client = NULL; 2438 hdev->roce.client = NULL; 2439 return ret; 2440 } 2441 2442 static void hclgevf_uninit_client_instance(struct hnae3_client *client, 2443 struct hnae3_ae_dev *ae_dev) 2444 { 2445 struct hclgevf_dev *hdev = ae_dev->priv; 2446 2447 /* un-init roce, if it exists */ 2448 if (hdev->roce_client) { 2449 hdev->roce_client->ops->uninit_instance(&hdev->roce, 0); 2450 hdev->roce_client = NULL; 2451 hdev->roce.client = NULL; 2452 } 2453 2454 /* un-init nic/unic, if this was not called by roce client */ 2455 if (client->ops->uninit_instance && hdev->nic_client && 2456 client->type != HNAE3_CLIENT_ROCE) { 2457 clear_bit(HCLGEVF_STATE_NIC_REGISTERED, &hdev->state); 2458 2459 client->ops->uninit_instance(&hdev->nic, 0); 2460 hdev->nic_client = NULL; 2461 hdev->nic.client = NULL; 2462 } 2463 } 2464 2465 static int hclgevf_pci_init(struct hclgevf_dev *hdev) 2466 { 2467 struct pci_dev *pdev = hdev->pdev; 2468 struct hclgevf_hw *hw; 2469 int ret; 2470 2471 ret = pci_enable_device(pdev); 2472 if (ret) { 2473 dev_err(&pdev->dev, "failed to enable PCI device\n"); 2474 return ret; 2475 } 2476 2477 ret = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)); 2478 if (ret) { 2479 dev_err(&pdev->dev, "can't set consistent PCI DMA, exiting"); 2480 goto err_disable_device; 2481 } 2482 2483 ret = pci_request_regions(pdev, HCLGEVF_DRIVER_NAME); 2484 if (ret) { 2485 dev_err(&pdev->dev, "PCI request regions failed %d\n", ret); 2486 goto err_disable_device; 2487 } 2488 2489 pci_set_master(pdev); 2490 hw = &hdev->hw; 2491 hw->hdev = hdev; 2492 hw->io_base = pci_iomap(pdev, 2, 0); 2493 if (!hw->io_base) { 2494 dev_err(&pdev->dev, "can't map configuration register space\n"); 2495 ret = -ENOMEM; 2496 goto err_clr_master; 2497 } 2498 2499 return 0; 2500 2501 err_clr_master: 2502 pci_clear_master(pdev); 2503 pci_release_regions(pdev); 2504 err_disable_device: 2505 pci_disable_device(pdev); 2506 2507 return ret; 2508 } 2509 2510 static void hclgevf_pci_uninit(struct hclgevf_dev *hdev) 2511 { 2512 struct pci_dev *pdev = hdev->pdev; 2513 2514 pci_iounmap(pdev, hdev->hw.io_base); 2515 pci_clear_master(pdev); 2516 pci_release_regions(pdev); 2517 pci_disable_device(pdev); 2518 } 2519 2520 static int hclgevf_query_vf_resource(struct hclgevf_dev *hdev) 2521 { 2522 struct hclgevf_query_res_cmd *req; 2523 struct hclgevf_desc desc; 2524 int ret; 2525 2526 hclgevf_cmd_setup_basic_desc(&desc, HCLGEVF_OPC_QUERY_VF_RSRC, true); 2527 ret = hclgevf_cmd_send(&hdev->hw, &desc, 1); 2528 if (ret) { 2529 dev_err(&hdev->pdev->dev, 2530 "query vf resource failed, ret = %d.\n", ret); 2531 return ret; 2532 } 2533 2534 req = (struct hclgevf_query_res_cmd *)desc.data; 2535 2536 if (hnae3_get_bit(hdev->ae_dev->flag, HNAE3_DEV_SUPPORT_ROCE_B)) { 2537 hdev->roce_base_msix_offset = 2538 hnae3_get_field(__le16_to_cpu(req->msixcap_localid_ba_rocee), 2539 HCLGEVF_MSIX_OFT_ROCEE_M, 2540 HCLGEVF_MSIX_OFT_ROCEE_S); 2541 hdev->num_roce_msix = 2542 hnae3_get_field(__le16_to_cpu(req->vf_intr_vector_number), 2543 HCLGEVF_VEC_NUM_M, HCLGEVF_VEC_NUM_S); 2544 2545 /* VF should have NIC vectors and Roce vectors, NIC vectors 2546 * are queued before Roce vectors. The offset is fixed to 64. 2547 */ 2548 hdev->num_msi = hdev->num_roce_msix + 2549 hdev->roce_base_msix_offset; 2550 } else { 2551 hdev->num_msi = 2552 hnae3_get_field(__le16_to_cpu(req->vf_intr_vector_number), 2553 HCLGEVF_VEC_NUM_M, HCLGEVF_VEC_NUM_S); 2554 } 2555 2556 return 0; 2557 } 2558 2559 static int hclgevf_pci_reset(struct hclgevf_dev *hdev) 2560 { 2561 struct pci_dev *pdev = hdev->pdev; 2562 int ret = 0; 2563 2564 if (hdev->reset_type == HNAE3_VF_FULL_RESET && 2565 test_bit(HCLGEVF_STATE_IRQ_INITED, &hdev->state)) { 2566 hclgevf_misc_irq_uninit(hdev); 2567 hclgevf_uninit_msi(hdev); 2568 clear_bit(HCLGEVF_STATE_IRQ_INITED, &hdev->state); 2569 } 2570 2571 if (!test_bit(HCLGEVF_STATE_IRQ_INITED, &hdev->state)) { 2572 pci_set_master(pdev); 2573 ret = hclgevf_init_msi(hdev); 2574 if (ret) { 2575 dev_err(&pdev->dev, 2576 "failed(%d) to init MSI/MSI-X\n", ret); 2577 return ret; 2578 } 2579 2580 ret = hclgevf_misc_irq_init(hdev); 2581 if (ret) { 2582 hclgevf_uninit_msi(hdev); 2583 dev_err(&pdev->dev, "failed(%d) to init Misc IRQ(vector0)\n", 2584 ret); 2585 return ret; 2586 } 2587 2588 set_bit(HCLGEVF_STATE_IRQ_INITED, &hdev->state); 2589 } 2590 2591 return ret; 2592 } 2593 2594 static int hclgevf_reset_hdev(struct hclgevf_dev *hdev) 2595 { 2596 struct pci_dev *pdev = hdev->pdev; 2597 int ret; 2598 2599 ret = hclgevf_pci_reset(hdev); 2600 if (ret) { 2601 dev_err(&pdev->dev, "pci reset failed %d\n", ret); 2602 return ret; 2603 } 2604 2605 ret = hclgevf_cmd_init(hdev); 2606 if (ret) { 2607 dev_err(&pdev->dev, "cmd failed %d\n", ret); 2608 return ret; 2609 } 2610 2611 ret = hclgevf_rss_init_hw(hdev); 2612 if (ret) { 2613 dev_err(&hdev->pdev->dev, 2614 "failed(%d) to initialize RSS\n", ret); 2615 return ret; 2616 } 2617 2618 ret = hclgevf_config_gro(hdev, true); 2619 if (ret) 2620 return ret; 2621 2622 ret = hclgevf_init_vlan_config(hdev); 2623 if (ret) { 2624 dev_err(&hdev->pdev->dev, 2625 "failed(%d) to initialize VLAN config\n", ret); 2626 return ret; 2627 } 2628 2629 if (pdev->revision >= 0x21) { 2630 ret = hclgevf_set_promisc_mode(hdev, true); 2631 if (ret) 2632 return ret; 2633 } 2634 2635 dev_info(&hdev->pdev->dev, "Reset done\n"); 2636 2637 return 0; 2638 } 2639 2640 static int hclgevf_init_hdev(struct hclgevf_dev *hdev) 2641 { 2642 struct pci_dev *pdev = hdev->pdev; 2643 int ret; 2644 2645 ret = hclgevf_pci_init(hdev); 2646 if (ret) { 2647 dev_err(&pdev->dev, "PCI initialization failed\n"); 2648 return ret; 2649 } 2650 2651 ret = hclgevf_cmd_queue_init(hdev); 2652 if (ret) { 2653 dev_err(&pdev->dev, "Cmd queue init failed: %d\n", ret); 2654 goto err_cmd_queue_init; 2655 } 2656 2657 ret = hclgevf_cmd_init(hdev); 2658 if (ret) 2659 goto err_cmd_init; 2660 2661 /* Get vf resource */ 2662 ret = hclgevf_query_vf_resource(hdev); 2663 if (ret) { 2664 dev_err(&hdev->pdev->dev, 2665 "Query vf status error, ret = %d.\n", ret); 2666 goto err_cmd_init; 2667 } 2668 2669 ret = hclgevf_init_msi(hdev); 2670 if (ret) { 2671 dev_err(&pdev->dev, "failed(%d) to init MSI/MSI-X\n", ret); 2672 goto err_cmd_init; 2673 } 2674 2675 hclgevf_state_init(hdev); 2676 hdev->reset_level = HNAE3_VF_FUNC_RESET; 2677 2678 ret = hclgevf_misc_irq_init(hdev); 2679 if (ret) { 2680 dev_err(&pdev->dev, "failed(%d) to init Misc IRQ(vector0)\n", 2681 ret); 2682 goto err_misc_irq_init; 2683 } 2684 2685 set_bit(HCLGEVF_STATE_IRQ_INITED, &hdev->state); 2686 2687 ret = hclgevf_configure(hdev); 2688 if (ret) { 2689 dev_err(&pdev->dev, "failed(%d) to fetch configuration\n", ret); 2690 goto err_config; 2691 } 2692 2693 ret = hclgevf_alloc_tqps(hdev); 2694 if (ret) { 2695 dev_err(&pdev->dev, "failed(%d) to allocate TQPs\n", ret); 2696 goto err_config; 2697 } 2698 2699 ret = hclgevf_set_handle_info(hdev); 2700 if (ret) { 2701 dev_err(&pdev->dev, "failed(%d) to set handle info\n", ret); 2702 goto err_config; 2703 } 2704 2705 ret = hclgevf_config_gro(hdev, true); 2706 if (ret) 2707 goto err_config; 2708 2709 /* vf is not allowed to enable unicast/multicast promisc mode. 2710 * For revision 0x20, default to disable broadcast promisc mode, 2711 * firmware makes sure broadcast packets can be accepted. 2712 * For revision 0x21, default to enable broadcast promisc mode. 2713 */ 2714 if (pdev->revision >= 0x21) { 2715 ret = hclgevf_set_promisc_mode(hdev, true); 2716 if (ret) 2717 goto err_config; 2718 } 2719 2720 /* Initialize RSS for this VF */ 2721 ret = hclgevf_rss_init_hw(hdev); 2722 if (ret) { 2723 dev_err(&hdev->pdev->dev, 2724 "failed(%d) to initialize RSS\n", ret); 2725 goto err_config; 2726 } 2727 2728 ret = hclgevf_init_vlan_config(hdev); 2729 if (ret) { 2730 dev_err(&hdev->pdev->dev, 2731 "failed(%d) to initialize VLAN config\n", ret); 2732 goto err_config; 2733 } 2734 2735 hdev->last_reset_time = jiffies; 2736 dev_info(&hdev->pdev->dev, "finished initializing %s driver\n", 2737 HCLGEVF_DRIVER_NAME); 2738 2739 return 0; 2740 2741 err_config: 2742 hclgevf_misc_irq_uninit(hdev); 2743 err_misc_irq_init: 2744 hclgevf_state_uninit(hdev); 2745 hclgevf_uninit_msi(hdev); 2746 err_cmd_init: 2747 hclgevf_cmd_uninit(hdev); 2748 err_cmd_queue_init: 2749 hclgevf_pci_uninit(hdev); 2750 clear_bit(HCLGEVF_STATE_IRQ_INITED, &hdev->state); 2751 return ret; 2752 } 2753 2754 static void hclgevf_uninit_hdev(struct hclgevf_dev *hdev) 2755 { 2756 hclgevf_state_uninit(hdev); 2757 2758 if (test_bit(HCLGEVF_STATE_IRQ_INITED, &hdev->state)) { 2759 hclgevf_misc_irq_uninit(hdev); 2760 hclgevf_uninit_msi(hdev); 2761 } 2762 2763 hclgevf_pci_uninit(hdev); 2764 hclgevf_cmd_uninit(hdev); 2765 } 2766 2767 static int hclgevf_init_ae_dev(struct hnae3_ae_dev *ae_dev) 2768 { 2769 struct pci_dev *pdev = ae_dev->pdev; 2770 struct hclgevf_dev *hdev; 2771 int ret; 2772 2773 ret = hclgevf_alloc_hdev(ae_dev); 2774 if (ret) { 2775 dev_err(&pdev->dev, "hclge device allocation failed\n"); 2776 return ret; 2777 } 2778 2779 ret = hclgevf_init_hdev(ae_dev->priv); 2780 if (ret) { 2781 dev_err(&pdev->dev, "hclge device initialization failed\n"); 2782 return ret; 2783 } 2784 2785 hdev = ae_dev->priv; 2786 timer_setup(&hdev->keep_alive_timer, hclgevf_keep_alive_timer, 0); 2787 INIT_WORK(&hdev->keep_alive_task, hclgevf_keep_alive_task); 2788 2789 return 0; 2790 } 2791 2792 static void hclgevf_uninit_ae_dev(struct hnae3_ae_dev *ae_dev) 2793 { 2794 struct hclgevf_dev *hdev = ae_dev->priv; 2795 2796 hclgevf_uninit_hdev(hdev); 2797 ae_dev->priv = NULL; 2798 } 2799 2800 static u32 hclgevf_get_max_channels(struct hclgevf_dev *hdev) 2801 { 2802 struct hnae3_handle *nic = &hdev->nic; 2803 struct hnae3_knic_private_info *kinfo = &nic->kinfo; 2804 2805 return min_t(u32, hdev->rss_size_max, 2806 hdev->num_tqps / kinfo->num_tc); 2807 } 2808 2809 /** 2810 * hclgevf_get_channels - Get the current channels enabled and max supported. 2811 * @handle: hardware information for network interface 2812 * @ch: ethtool channels structure 2813 * 2814 * We don't support separate tx and rx queues as channels. The other count 2815 * represents how many queues are being used for control. max_combined counts 2816 * how many queue pairs we can support. They may not be mapped 1 to 1 with 2817 * q_vectors since we support a lot more queue pairs than q_vectors. 2818 **/ 2819 static void hclgevf_get_channels(struct hnae3_handle *handle, 2820 struct ethtool_channels *ch) 2821 { 2822 struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle); 2823 2824 ch->max_combined = hclgevf_get_max_channels(hdev); 2825 ch->other_count = 0; 2826 ch->max_other = 0; 2827 ch->combined_count = handle->kinfo.rss_size; 2828 } 2829 2830 static void hclgevf_get_tqps_and_rss_info(struct hnae3_handle *handle, 2831 u16 *alloc_tqps, u16 *max_rss_size) 2832 { 2833 struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle); 2834 2835 *alloc_tqps = hdev->num_tqps; 2836 *max_rss_size = hdev->rss_size_max; 2837 } 2838 2839 static void hclgevf_update_rss_size(struct hnae3_handle *handle, 2840 u32 new_tqps_num) 2841 { 2842 struct hnae3_knic_private_info *kinfo = &handle->kinfo; 2843 struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle); 2844 u16 max_rss_size; 2845 2846 kinfo->req_rss_size = new_tqps_num; 2847 2848 max_rss_size = min_t(u16, hdev->rss_size_max, 2849 hdev->num_tqps / kinfo->num_tc); 2850 2851 /* Use the user's configuration when it is not larger than 2852 * max_rss_size, otherwise, use the maximum specification value. 2853 */ 2854 if (kinfo->req_rss_size != kinfo->rss_size && kinfo->req_rss_size && 2855 kinfo->req_rss_size <= max_rss_size) 2856 kinfo->rss_size = kinfo->req_rss_size; 2857 else if (kinfo->rss_size > max_rss_size || 2858 (!kinfo->req_rss_size && kinfo->rss_size < max_rss_size)) 2859 kinfo->rss_size = max_rss_size; 2860 2861 kinfo->num_tqps = kinfo->num_tc * kinfo->rss_size; 2862 } 2863 2864 static int hclgevf_set_channels(struct hnae3_handle *handle, u32 new_tqps_num, 2865 bool rxfh_configured) 2866 { 2867 struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle); 2868 struct hnae3_knic_private_info *kinfo = &handle->kinfo; 2869 u16 cur_rss_size = kinfo->rss_size; 2870 u16 cur_tqps = kinfo->num_tqps; 2871 u32 *rss_indir; 2872 unsigned int i; 2873 int ret; 2874 2875 hclgevf_update_rss_size(handle, new_tqps_num); 2876 2877 ret = hclgevf_set_rss_tc_mode(hdev, kinfo->rss_size); 2878 if (ret) 2879 return ret; 2880 2881 /* RSS indirection table has been configuared by user */ 2882 if (rxfh_configured) 2883 goto out; 2884 2885 /* Reinitializes the rss indirect table according to the new RSS size */ 2886 rss_indir = kcalloc(HCLGEVF_RSS_IND_TBL_SIZE, sizeof(u32), GFP_KERNEL); 2887 if (!rss_indir) 2888 return -ENOMEM; 2889 2890 for (i = 0; i < HCLGEVF_RSS_IND_TBL_SIZE; i++) 2891 rss_indir[i] = i % kinfo->rss_size; 2892 2893 ret = hclgevf_set_rss(handle, rss_indir, NULL, 0); 2894 if (ret) 2895 dev_err(&hdev->pdev->dev, "set rss indir table fail, ret=%d\n", 2896 ret); 2897 2898 kfree(rss_indir); 2899 2900 out: 2901 if (!ret) 2902 dev_info(&hdev->pdev->dev, 2903 "Channels changed, rss_size from %u to %u, tqps from %u to %u", 2904 cur_rss_size, kinfo->rss_size, 2905 cur_tqps, kinfo->rss_size * kinfo->num_tc); 2906 2907 return ret; 2908 } 2909 2910 static int hclgevf_get_status(struct hnae3_handle *handle) 2911 { 2912 struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle); 2913 2914 return hdev->hw.mac.link; 2915 } 2916 2917 static void hclgevf_get_ksettings_an_result(struct hnae3_handle *handle, 2918 u8 *auto_neg, u32 *speed, 2919 u8 *duplex) 2920 { 2921 struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle); 2922 2923 if (speed) 2924 *speed = hdev->hw.mac.speed; 2925 if (duplex) 2926 *duplex = hdev->hw.mac.duplex; 2927 if (auto_neg) 2928 *auto_neg = AUTONEG_DISABLE; 2929 } 2930 2931 void hclgevf_update_speed_duplex(struct hclgevf_dev *hdev, u32 speed, 2932 u8 duplex) 2933 { 2934 hdev->hw.mac.speed = speed; 2935 hdev->hw.mac.duplex = duplex; 2936 } 2937 2938 static int hclgevf_gro_en(struct hnae3_handle *handle, bool enable) 2939 { 2940 struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle); 2941 2942 return hclgevf_config_gro(hdev, enable); 2943 } 2944 2945 static void hclgevf_get_media_type(struct hnae3_handle *handle, u8 *media_type, 2946 u8 *module_type) 2947 { 2948 struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle); 2949 2950 if (media_type) 2951 *media_type = hdev->hw.mac.media_type; 2952 2953 if (module_type) 2954 *module_type = hdev->hw.mac.module_type; 2955 } 2956 2957 static bool hclgevf_get_hw_reset_stat(struct hnae3_handle *handle) 2958 { 2959 struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle); 2960 2961 return !!hclgevf_read_dev(&hdev->hw, HCLGEVF_RST_ING); 2962 } 2963 2964 static bool hclgevf_ae_dev_resetting(struct hnae3_handle *handle) 2965 { 2966 struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle); 2967 2968 return test_bit(HCLGEVF_STATE_RST_HANDLING, &hdev->state); 2969 } 2970 2971 static unsigned long hclgevf_ae_dev_reset_cnt(struct hnae3_handle *handle) 2972 { 2973 struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle); 2974 2975 return hdev->rst_stats.hw_rst_done_cnt; 2976 } 2977 2978 static void hclgevf_get_link_mode(struct hnae3_handle *handle, 2979 unsigned long *supported, 2980 unsigned long *advertising) 2981 { 2982 struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle); 2983 2984 *supported = hdev->hw.mac.supported; 2985 *advertising = hdev->hw.mac.advertising; 2986 } 2987 2988 #define MAX_SEPARATE_NUM 4 2989 #define SEPARATOR_VALUE 0xFFFFFFFF 2990 #define REG_NUM_PER_LINE 4 2991 #define REG_LEN_PER_LINE (REG_NUM_PER_LINE * sizeof(u32)) 2992 2993 static int hclgevf_get_regs_len(struct hnae3_handle *handle) 2994 { 2995 int cmdq_lines, common_lines, ring_lines, tqp_intr_lines; 2996 struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle); 2997 2998 cmdq_lines = sizeof(cmdq_reg_addr_list) / REG_LEN_PER_LINE + 1; 2999 common_lines = sizeof(common_reg_addr_list) / REG_LEN_PER_LINE + 1; 3000 ring_lines = sizeof(ring_reg_addr_list) / REG_LEN_PER_LINE + 1; 3001 tqp_intr_lines = sizeof(tqp_intr_reg_addr_list) / REG_LEN_PER_LINE + 1; 3002 3003 return (cmdq_lines + common_lines + ring_lines * hdev->num_tqps + 3004 tqp_intr_lines * (hdev->num_msi_used - 1)) * REG_LEN_PER_LINE; 3005 } 3006 3007 static void hclgevf_get_regs(struct hnae3_handle *handle, u32 *version, 3008 void *data) 3009 { 3010 struct hclgevf_dev *hdev = hclgevf_ae_get_hdev(handle); 3011 int i, j, reg_um, separator_num; 3012 u32 *reg = data; 3013 3014 *version = hdev->fw_version; 3015 3016 /* fetching per-VF registers values from VF PCIe register space */ 3017 reg_um = sizeof(cmdq_reg_addr_list) / sizeof(u32); 3018 separator_num = MAX_SEPARATE_NUM - reg_um % REG_NUM_PER_LINE; 3019 for (i = 0; i < reg_um; i++) 3020 *reg++ = hclgevf_read_dev(&hdev->hw, cmdq_reg_addr_list[i]); 3021 for (i = 0; i < separator_num; i++) 3022 *reg++ = SEPARATOR_VALUE; 3023 3024 reg_um = sizeof(common_reg_addr_list) / sizeof(u32); 3025 separator_num = MAX_SEPARATE_NUM - reg_um % REG_NUM_PER_LINE; 3026 for (i = 0; i < reg_um; i++) 3027 *reg++ = hclgevf_read_dev(&hdev->hw, common_reg_addr_list[i]); 3028 for (i = 0; i < separator_num; i++) 3029 *reg++ = SEPARATOR_VALUE; 3030 3031 reg_um = sizeof(ring_reg_addr_list) / sizeof(u32); 3032 separator_num = MAX_SEPARATE_NUM - reg_um % REG_NUM_PER_LINE; 3033 for (j = 0; j < hdev->num_tqps; j++) { 3034 for (i = 0; i < reg_um; i++) 3035 *reg++ = hclgevf_read_dev(&hdev->hw, 3036 ring_reg_addr_list[i] + 3037 0x200 * j); 3038 for (i = 0; i < separator_num; i++) 3039 *reg++ = SEPARATOR_VALUE; 3040 } 3041 3042 reg_um = sizeof(tqp_intr_reg_addr_list) / sizeof(u32); 3043 separator_num = MAX_SEPARATE_NUM - reg_um % REG_NUM_PER_LINE; 3044 for (j = 0; j < hdev->num_msi_used - 1; j++) { 3045 for (i = 0; i < reg_um; i++) 3046 *reg++ = hclgevf_read_dev(&hdev->hw, 3047 tqp_intr_reg_addr_list[i] + 3048 4 * j); 3049 for (i = 0; i < separator_num; i++) 3050 *reg++ = SEPARATOR_VALUE; 3051 } 3052 } 3053 3054 void hclgevf_update_port_base_vlan_info(struct hclgevf_dev *hdev, u16 state, 3055 u8 *port_base_vlan_info, u8 data_size) 3056 { 3057 struct hnae3_handle *nic = &hdev->nic; 3058 3059 rtnl_lock(); 3060 hclgevf_notify_client(hdev, HNAE3_DOWN_CLIENT); 3061 rtnl_unlock(); 3062 3063 /* send msg to PF and wait update port based vlan info */ 3064 hclgevf_send_mbx_msg(hdev, HCLGE_MBX_SET_VLAN, 3065 HCLGE_MBX_PORT_BASE_VLAN_CFG, 3066 port_base_vlan_info, data_size, 3067 false, NULL, 0); 3068 3069 if (state == HNAE3_PORT_BASE_VLAN_DISABLE) 3070 nic->port_base_vlan_state = HNAE3_PORT_BASE_VLAN_DISABLE; 3071 else 3072 nic->port_base_vlan_state = HNAE3_PORT_BASE_VLAN_ENABLE; 3073 3074 rtnl_lock(); 3075 hclgevf_notify_client(hdev, HNAE3_UP_CLIENT); 3076 rtnl_unlock(); 3077 } 3078 3079 static const struct hnae3_ae_ops hclgevf_ops = { 3080 .init_ae_dev = hclgevf_init_ae_dev, 3081 .uninit_ae_dev = hclgevf_uninit_ae_dev, 3082 .flr_prepare = hclgevf_flr_prepare, 3083 .flr_done = hclgevf_flr_done, 3084 .init_client_instance = hclgevf_init_client_instance, 3085 .uninit_client_instance = hclgevf_uninit_client_instance, 3086 .start = hclgevf_ae_start, 3087 .stop = hclgevf_ae_stop, 3088 .client_start = hclgevf_client_start, 3089 .client_stop = hclgevf_client_stop, 3090 .map_ring_to_vector = hclgevf_map_ring_to_vector, 3091 .unmap_ring_from_vector = hclgevf_unmap_ring_from_vector, 3092 .get_vector = hclgevf_get_vector, 3093 .put_vector = hclgevf_put_vector, 3094 .reset_queue = hclgevf_reset_tqp, 3095 .get_mac_addr = hclgevf_get_mac_addr, 3096 .set_mac_addr = hclgevf_set_mac_addr, 3097 .add_uc_addr = hclgevf_add_uc_addr, 3098 .rm_uc_addr = hclgevf_rm_uc_addr, 3099 .add_mc_addr = hclgevf_add_mc_addr, 3100 .rm_mc_addr = hclgevf_rm_mc_addr, 3101 .get_stats = hclgevf_get_stats, 3102 .update_stats = hclgevf_update_stats, 3103 .get_strings = hclgevf_get_strings, 3104 .get_sset_count = hclgevf_get_sset_count, 3105 .get_rss_key_size = hclgevf_get_rss_key_size, 3106 .get_rss_indir_size = hclgevf_get_rss_indir_size, 3107 .get_rss = hclgevf_get_rss, 3108 .set_rss = hclgevf_set_rss, 3109 .get_rss_tuple = hclgevf_get_rss_tuple, 3110 .set_rss_tuple = hclgevf_set_rss_tuple, 3111 .get_tc_size = hclgevf_get_tc_size, 3112 .get_fw_version = hclgevf_get_fw_version, 3113 .set_vlan_filter = hclgevf_set_vlan_filter, 3114 .enable_hw_strip_rxvtag = hclgevf_en_hw_strip_rxvtag, 3115 .reset_event = hclgevf_reset_event, 3116 .set_default_reset_request = hclgevf_set_def_reset_request, 3117 .set_channels = hclgevf_set_channels, 3118 .get_channels = hclgevf_get_channels, 3119 .get_tqps_and_rss_info = hclgevf_get_tqps_and_rss_info, 3120 .get_regs_len = hclgevf_get_regs_len, 3121 .get_regs = hclgevf_get_regs, 3122 .get_status = hclgevf_get_status, 3123 .get_ksettings_an_result = hclgevf_get_ksettings_an_result, 3124 .get_media_type = hclgevf_get_media_type, 3125 .get_hw_reset_stat = hclgevf_get_hw_reset_stat, 3126 .ae_dev_resetting = hclgevf_ae_dev_resetting, 3127 .ae_dev_reset_cnt = hclgevf_ae_dev_reset_cnt, 3128 .set_gro_en = hclgevf_gro_en, 3129 .set_mtu = hclgevf_set_mtu, 3130 .get_global_queue_id = hclgevf_get_qid_global, 3131 .set_timer_task = hclgevf_set_timer_task, 3132 .get_link_mode = hclgevf_get_link_mode, 3133 }; 3134 3135 static struct hnae3_ae_algo ae_algovf = { 3136 .ops = &hclgevf_ops, 3137 .pdev_id_table = ae_algovf_pci_tbl, 3138 }; 3139 3140 static int hclgevf_init(void) 3141 { 3142 pr_info("%s is initializing\n", HCLGEVF_NAME); 3143 3144 hnae3_register_ae_algo(&ae_algovf); 3145 3146 return 0; 3147 } 3148 3149 static void hclgevf_exit(void) 3150 { 3151 hnae3_unregister_ae_algo(&ae_algovf); 3152 } 3153 module_init(hclgevf_init); 3154 module_exit(hclgevf_exit); 3155 3156 MODULE_LICENSE("GPL"); 3157 MODULE_AUTHOR("Huawei Tech. Co., Ltd."); 3158 MODULE_DESCRIPTION("HCLGEVF Driver"); 3159 MODULE_VERSION(HCLGEVF_MOD_VERSION); 3160