1 /* SPDX-License-Identifier: BSD-3-Clause 2 * Copyright(c) 2018-2021 HiSilicon Limited. 3 */ 4 5 #include <rte_alarm.h> 6 #include <bus_pci_driver.h> 7 #include <ethdev_pci.h> 8 9 #include "hns3_common.h" 10 #include "hns3_dump.h" 11 #include "hns3_logs.h" 12 #include "hns3_rxtx.h" 13 #include "hns3_intr.h" 14 #include "hns3_regs.h" 15 #include "hns3_dcb.h" 16 #include "hns3_mp.h" 17 #include "hns3_flow.h" 18 #include "hns3_ethdev.h" 19 20 #define HNS3_SERVICE_INTERVAL 1000000 /* us */ 21 #define HNS3_SERVICE_QUICK_INTERVAL 10 22 #define HNS3_INVALID_PVID 0xFFFF 23 24 #define HNS3_FILTER_TYPE_VF 0 25 #define HNS3_FILTER_TYPE_PORT 1 26 #define HNS3_FILTER_FE_EGRESS_V1_B BIT(0) 27 #define HNS3_FILTER_FE_NIC_INGRESS_B BIT(0) 28 #define HNS3_FILTER_FE_NIC_EGRESS_B BIT(1) 29 #define HNS3_FILTER_FE_ROCE_INGRESS_B BIT(2) 30 #define HNS3_FILTER_FE_ROCE_EGRESS_B BIT(3) 31 #define HNS3_FILTER_FE_EGRESS (HNS3_FILTER_FE_NIC_EGRESS_B \ 32 | HNS3_FILTER_FE_ROCE_EGRESS_B) 33 #define HNS3_FILTER_FE_INGRESS (HNS3_FILTER_FE_NIC_INGRESS_B \ 34 | HNS3_FILTER_FE_ROCE_INGRESS_B) 35 36 /* Reset related Registers */ 37 #define HNS3_GLOBAL_RESET_BIT 0 38 #define HNS3_CORE_RESET_BIT 1 39 #define HNS3_IMP_RESET_BIT 2 40 #define HNS3_FUN_RST_ING_B 0 41 42 #define HNS3_VECTOR0_IMP_RESET_INT_B 1 43 #define HNS3_VECTOR0_IMP_CMDQ_ERR_B 4U 44 #define HNS3_VECTOR0_IMP_RD_POISON_B 5U 45 #define HNS3_VECTOR0_ALL_MSIX_ERR_B 6U 46 47 #define HNS3_RESET_WAIT_MS 100 48 #define HNS3_RESET_WAIT_CNT 200 49 50 /* FEC mode order defined in HNS3 hardware */ 51 #define HNS3_HW_FEC_MODE_NOFEC 0 52 #define HNS3_HW_FEC_MODE_BASER 1 53 #define HNS3_HW_FEC_MODE_RS 2 54 55 enum hns3_evt_cause { 56 HNS3_VECTOR0_EVENT_RST, 57 HNS3_VECTOR0_EVENT_MBX, 58 HNS3_VECTOR0_EVENT_ERR, 59 HNS3_VECTOR0_EVENT_PTP, 60 HNS3_VECTOR0_EVENT_OTHER, 61 }; 62 63 static const struct rte_eth_fec_capa speed_fec_capa_tbl[] = { 64 { RTE_ETH_SPEED_NUM_10G, RTE_ETH_FEC_MODE_CAPA_MASK(NOFEC) | 65 RTE_ETH_FEC_MODE_CAPA_MASK(AUTO) | 66 RTE_ETH_FEC_MODE_CAPA_MASK(BASER) }, 67 68 { RTE_ETH_SPEED_NUM_25G, RTE_ETH_FEC_MODE_CAPA_MASK(NOFEC) | 69 RTE_ETH_FEC_MODE_CAPA_MASK(AUTO) | 70 RTE_ETH_FEC_MODE_CAPA_MASK(BASER) | 71 RTE_ETH_FEC_MODE_CAPA_MASK(RS) }, 72 73 { RTE_ETH_SPEED_NUM_40G, RTE_ETH_FEC_MODE_CAPA_MASK(NOFEC) | 74 RTE_ETH_FEC_MODE_CAPA_MASK(AUTO) | 75 RTE_ETH_FEC_MODE_CAPA_MASK(BASER) }, 76 77 { RTE_ETH_SPEED_NUM_50G, RTE_ETH_FEC_MODE_CAPA_MASK(NOFEC) | 78 RTE_ETH_FEC_MODE_CAPA_MASK(AUTO) | 79 RTE_ETH_FEC_MODE_CAPA_MASK(BASER) | 80 RTE_ETH_FEC_MODE_CAPA_MASK(RS) }, 81 82 { RTE_ETH_SPEED_NUM_100G, RTE_ETH_FEC_MODE_CAPA_MASK(NOFEC) | 83 RTE_ETH_FEC_MODE_CAPA_MASK(AUTO) | 84 RTE_ETH_FEC_MODE_CAPA_MASK(RS) }, 85 86 { RTE_ETH_SPEED_NUM_200G, RTE_ETH_FEC_MODE_CAPA_MASK(NOFEC) | 87 RTE_ETH_FEC_MODE_CAPA_MASK(AUTO) | 88 RTE_ETH_FEC_MODE_CAPA_MASK(RS) } 89 }; 90 91 static enum hns3_reset_level hns3_get_reset_level(struct hns3_adapter *hns, 92 uint64_t *levels); 93 static int hns3_dev_mtu_set(struct rte_eth_dev *dev, uint16_t mtu); 94 static int hns3_vlan_pvid_configure(struct hns3_adapter *hns, uint16_t pvid, 95 int on); 96 static int hns3_update_link_info(struct rte_eth_dev *eth_dev); 97 static bool hns3_update_link_status(struct hns3_hw *hw); 98 99 static int hns3_add_mc_mac_addr(struct hns3_hw *hw, 100 struct rte_ether_addr *mac_addr); 101 static int hns3_remove_mc_mac_addr(struct hns3_hw *hw, 102 struct rte_ether_addr *mac_addr); 103 static int hns3_restore_fec(struct hns3_hw *hw); 104 static int hns3_query_dev_fec_info(struct hns3_hw *hw); 105 static int hns3_do_stop(struct hns3_adapter *hns); 106 static int hns3_check_port_speed(struct hns3_hw *hw, uint32_t link_speeds); 107 static int hns3_cfg_mac_mode(struct hns3_hw *hw, bool enable); 108 109 110 static void 111 hns3_pf_disable_irq0(struct hns3_hw *hw) 112 { 113 hns3_write_dev(hw, HNS3_MISC_VECTOR_REG_BASE, 0); 114 } 115 116 static void 117 hns3_pf_enable_irq0(struct hns3_hw *hw) 118 { 119 hns3_write_dev(hw, HNS3_MISC_VECTOR_REG_BASE, 1); 120 } 121 122 static enum hns3_evt_cause 123 hns3_proc_imp_reset_event(struct hns3_adapter *hns, bool is_delay, 124 uint32_t *vec_val) 125 { 126 struct hns3_hw *hw = &hns->hw; 127 128 __atomic_store_n(&hw->reset.disable_cmd, 1, __ATOMIC_RELAXED); 129 hns3_atomic_set_bit(HNS3_IMP_RESET, &hw->reset.pending); 130 *vec_val = BIT(HNS3_VECTOR0_IMPRESET_INT_B); 131 if (!is_delay) { 132 hw->reset.stats.imp_cnt++; 133 hns3_warn(hw, "IMP reset detected, clear reset status"); 134 } else { 135 hns3_schedule_delayed_reset(hns); 136 hns3_warn(hw, "IMP reset detected, don't clear reset status"); 137 } 138 139 return HNS3_VECTOR0_EVENT_RST; 140 } 141 142 static enum hns3_evt_cause 143 hns3_proc_global_reset_event(struct hns3_adapter *hns, bool is_delay, 144 uint32_t *vec_val) 145 { 146 struct hns3_hw *hw = &hns->hw; 147 148 __atomic_store_n(&hw->reset.disable_cmd, 1, __ATOMIC_RELAXED); 149 hns3_atomic_set_bit(HNS3_GLOBAL_RESET, &hw->reset.pending); 150 *vec_val = BIT(HNS3_VECTOR0_GLOBALRESET_INT_B); 151 if (!is_delay) { 152 hw->reset.stats.global_cnt++; 153 hns3_warn(hw, "Global reset detected, clear reset status"); 154 } else { 155 hns3_schedule_delayed_reset(hns); 156 hns3_warn(hw, 157 "Global reset detected, don't clear reset status"); 158 } 159 160 return HNS3_VECTOR0_EVENT_RST; 161 } 162 163 static enum hns3_evt_cause 164 hns3_check_event_cause(struct hns3_adapter *hns, uint32_t *clearval) 165 { 166 struct hns3_hw *hw = &hns->hw; 167 uint32_t vector0_int_stats; 168 uint32_t cmdq_src_val; 169 uint32_t hw_err_src_reg; 170 uint32_t val; 171 enum hns3_evt_cause ret; 172 bool is_delay; 173 174 /* fetch the events from their corresponding regs */ 175 vector0_int_stats = hns3_read_dev(hw, HNS3_VECTOR0_OTHER_INT_STS_REG); 176 cmdq_src_val = hns3_read_dev(hw, HNS3_VECTOR0_CMDQ_SRC_REG); 177 hw_err_src_reg = hns3_read_dev(hw, HNS3_RAS_PF_OTHER_INT_STS_REG); 178 179 is_delay = clearval == NULL ? true : false; 180 /* 181 * Assumption: If by any chance reset and mailbox events are reported 182 * together then we will only process reset event and defer the 183 * processing of the mailbox events. Since, we would have not cleared 184 * RX CMDQ event this time we would receive again another interrupt 185 * from H/W just for the mailbox. 186 */ 187 if (BIT(HNS3_VECTOR0_IMPRESET_INT_B) & vector0_int_stats) { /* IMP */ 188 ret = hns3_proc_imp_reset_event(hns, is_delay, &val); 189 goto out; 190 } 191 192 /* Global reset */ 193 if (BIT(HNS3_VECTOR0_GLOBALRESET_INT_B) & vector0_int_stats) { 194 ret = hns3_proc_global_reset_event(hns, is_delay, &val); 195 goto out; 196 } 197 198 /* Check for vector0 1588 event source */ 199 if (BIT(HNS3_VECTOR0_1588_INT_B) & vector0_int_stats) { 200 val = BIT(HNS3_VECTOR0_1588_INT_B); 201 ret = HNS3_VECTOR0_EVENT_PTP; 202 goto out; 203 } 204 205 /* check for vector0 msix event source */ 206 if (vector0_int_stats & HNS3_VECTOR0_REG_MSIX_MASK || 207 hw_err_src_reg & HNS3_RAS_REG_NFE_MASK) { 208 val = vector0_int_stats | hw_err_src_reg; 209 ret = HNS3_VECTOR0_EVENT_ERR; 210 goto out; 211 } 212 213 /* check for vector0 mailbox(=CMDQ RX) event source */ 214 if (BIT(HNS3_VECTOR0_RX_CMDQ_INT_B) & cmdq_src_val) { 215 cmdq_src_val &= ~BIT(HNS3_VECTOR0_RX_CMDQ_INT_B); 216 val = cmdq_src_val; 217 ret = HNS3_VECTOR0_EVENT_MBX; 218 goto out; 219 } 220 221 val = vector0_int_stats; 222 ret = HNS3_VECTOR0_EVENT_OTHER; 223 out: 224 225 if (clearval) 226 *clearval = val; 227 return ret; 228 } 229 230 static void 231 hns3_clear_event_cause(struct hns3_hw *hw, uint32_t event_type, uint32_t regclr) 232 { 233 if (event_type == HNS3_VECTOR0_EVENT_RST || 234 event_type == HNS3_VECTOR0_EVENT_PTP) 235 hns3_write_dev(hw, HNS3_MISC_RESET_STS_REG, regclr); 236 else if (event_type == HNS3_VECTOR0_EVENT_MBX) 237 hns3_write_dev(hw, HNS3_VECTOR0_CMDQ_SRC_REG, regclr); 238 } 239 240 static void 241 hns3_clear_all_event_cause(struct hns3_hw *hw) 242 { 243 uint32_t vector0_int_stats; 244 245 vector0_int_stats = hns3_read_dev(hw, HNS3_VECTOR0_OTHER_INT_STS_REG); 246 if (BIT(HNS3_VECTOR0_IMPRESET_INT_B) & vector0_int_stats) 247 hns3_warn(hw, "Probe during IMP reset interrupt"); 248 249 if (BIT(HNS3_VECTOR0_GLOBALRESET_INT_B) & vector0_int_stats) 250 hns3_warn(hw, "Probe during Global reset interrupt"); 251 252 hns3_clear_event_cause(hw, HNS3_VECTOR0_EVENT_RST, 253 BIT(HNS3_VECTOR0_IMPRESET_INT_B) | 254 BIT(HNS3_VECTOR0_GLOBALRESET_INT_B) | 255 BIT(HNS3_VECTOR0_CORERESET_INT_B)); 256 hns3_clear_event_cause(hw, HNS3_VECTOR0_EVENT_MBX, 0); 257 hns3_clear_event_cause(hw, HNS3_VECTOR0_EVENT_PTP, 258 BIT(HNS3_VECTOR0_1588_INT_B)); 259 } 260 261 static void 262 hns3_handle_mac_tnl(struct hns3_hw *hw) 263 { 264 struct hns3_cmd_desc desc; 265 uint32_t status; 266 int ret; 267 268 /* query and clear mac tnl interrupt */ 269 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_QUERY_MAC_TNL_INT, true); 270 ret = hns3_cmd_send(hw, &desc, 1); 271 if (ret) { 272 hns3_err(hw, "failed to query mac tnl int, ret = %d.", ret); 273 return; 274 } 275 276 status = rte_le_to_cpu_32(desc.data[0]); 277 if (status) { 278 hns3_warn(hw, "mac tnl int occurs, status = 0x%x.", status); 279 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_CLEAR_MAC_TNL_INT, 280 false); 281 desc.data[0] = rte_cpu_to_le_32(HNS3_MAC_TNL_INT_CLR); 282 ret = hns3_cmd_send(hw, &desc, 1); 283 if (ret) 284 hns3_err(hw, "failed to clear mac tnl int, ret = %d.", 285 ret); 286 } 287 } 288 289 static void 290 hns3_interrupt_handler(void *param) 291 { 292 struct rte_eth_dev *dev = (struct rte_eth_dev *)param; 293 struct hns3_adapter *hns = dev->data->dev_private; 294 struct hns3_hw *hw = &hns->hw; 295 enum hns3_evt_cause event_cause; 296 uint32_t clearval = 0; 297 uint32_t vector0_int; 298 uint32_t ras_int; 299 uint32_t cmdq_int; 300 301 /* Disable interrupt */ 302 hns3_pf_disable_irq0(hw); 303 304 event_cause = hns3_check_event_cause(hns, &clearval); 305 vector0_int = hns3_read_dev(hw, HNS3_VECTOR0_OTHER_INT_STS_REG); 306 ras_int = hns3_read_dev(hw, HNS3_RAS_PF_OTHER_INT_STS_REG); 307 cmdq_int = hns3_read_dev(hw, HNS3_VECTOR0_CMDQ_SRC_REG); 308 hns3_clear_event_cause(hw, event_cause, clearval); 309 /* vector 0 interrupt is shared with reset and mailbox source events. */ 310 if (event_cause == HNS3_VECTOR0_EVENT_ERR) { 311 hns3_warn(hw, "received interrupt: vector0_int_stat:0x%x " 312 "ras_int_stat:0x%x cmdq_int_stat:0x%x", 313 vector0_int, ras_int, cmdq_int); 314 hns3_handle_mac_tnl(hw); 315 hns3_handle_error(hns); 316 } else if (event_cause == HNS3_VECTOR0_EVENT_RST) { 317 hns3_warn(hw, "received reset interrupt"); 318 hns3_schedule_reset(hns); 319 } else if (event_cause == HNS3_VECTOR0_EVENT_MBX) { 320 hns3_dev_handle_mbx_msg(hw); 321 } else if (event_cause != HNS3_VECTOR0_EVENT_PTP) { 322 hns3_warn(hw, "received unknown event: vector0_int_stat:0x%x " 323 "ras_int_stat:0x%x cmdq_int_stat:0x%x", 324 vector0_int, ras_int, cmdq_int); 325 } 326 327 /* Enable interrupt if it is not cause by reset */ 328 hns3_pf_enable_irq0(hw); 329 } 330 331 static int 332 hns3_set_port_vlan_filter(struct hns3_adapter *hns, uint16_t vlan_id, int on) 333 { 334 #define HNS3_VLAN_ID_OFFSET_STEP 160 335 #define HNS3_VLAN_BYTE_SIZE 8 336 struct hns3_vlan_filter_pf_cfg_cmd *req; 337 struct hns3_hw *hw = &hns->hw; 338 uint8_t vlan_offset_byte_val; 339 struct hns3_cmd_desc desc; 340 uint8_t vlan_offset_byte; 341 uint8_t vlan_offset_base; 342 int ret; 343 344 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_VLAN_FILTER_PF_CFG, false); 345 346 vlan_offset_base = vlan_id / HNS3_VLAN_ID_OFFSET_STEP; 347 vlan_offset_byte = (vlan_id % HNS3_VLAN_ID_OFFSET_STEP) / 348 HNS3_VLAN_BYTE_SIZE; 349 vlan_offset_byte_val = 1 << (vlan_id % HNS3_VLAN_BYTE_SIZE); 350 351 req = (struct hns3_vlan_filter_pf_cfg_cmd *)desc.data; 352 req->vlan_offset = vlan_offset_base; 353 req->vlan_cfg = on ? 0 : 1; 354 req->vlan_offset_bitmap[vlan_offset_byte] = vlan_offset_byte_val; 355 356 ret = hns3_cmd_send(hw, &desc, 1); 357 if (ret) 358 hns3_err(hw, "set port vlan id failed, vlan_id =%u, ret =%d", 359 vlan_id, ret); 360 361 return ret; 362 } 363 364 static void 365 hns3_rm_dev_vlan_table(struct hns3_adapter *hns, uint16_t vlan_id) 366 { 367 struct hns3_user_vlan_table *vlan_entry; 368 struct hns3_pf *pf = &hns->pf; 369 370 LIST_FOREACH(vlan_entry, &pf->vlan_list, next) { 371 if (vlan_entry->vlan_id == vlan_id) { 372 if (vlan_entry->hd_tbl_status) 373 hns3_set_port_vlan_filter(hns, vlan_id, 0); 374 LIST_REMOVE(vlan_entry, next); 375 rte_free(vlan_entry); 376 break; 377 } 378 } 379 } 380 381 static void 382 hns3_add_dev_vlan_table(struct hns3_adapter *hns, uint16_t vlan_id, 383 bool writen_to_tbl) 384 { 385 struct hns3_user_vlan_table *vlan_entry; 386 struct hns3_hw *hw = &hns->hw; 387 struct hns3_pf *pf = &hns->pf; 388 389 LIST_FOREACH(vlan_entry, &pf->vlan_list, next) { 390 if (vlan_entry->vlan_id == vlan_id) 391 return; 392 } 393 394 vlan_entry = rte_zmalloc("hns3_vlan_tbl", sizeof(*vlan_entry), 0); 395 if (vlan_entry == NULL) { 396 hns3_err(hw, "Failed to malloc hns3 vlan table"); 397 return; 398 } 399 400 vlan_entry->hd_tbl_status = writen_to_tbl; 401 vlan_entry->vlan_id = vlan_id; 402 403 LIST_INSERT_HEAD(&pf->vlan_list, vlan_entry, next); 404 } 405 406 static int 407 hns3_restore_vlan_table(struct hns3_adapter *hns) 408 { 409 struct hns3_user_vlan_table *vlan_entry; 410 struct hns3_hw *hw = &hns->hw; 411 struct hns3_pf *pf = &hns->pf; 412 uint16_t vlan_id; 413 int ret = 0; 414 415 if (hw->port_base_vlan_cfg.state == HNS3_PORT_BASE_VLAN_ENABLE) 416 return hns3_vlan_pvid_configure(hns, 417 hw->port_base_vlan_cfg.pvid, 1); 418 419 LIST_FOREACH(vlan_entry, &pf->vlan_list, next) { 420 if (vlan_entry->hd_tbl_status) { 421 vlan_id = vlan_entry->vlan_id; 422 ret = hns3_set_port_vlan_filter(hns, vlan_id, 1); 423 if (ret) 424 break; 425 } 426 } 427 428 return ret; 429 } 430 431 static int 432 hns3_vlan_filter_configure(struct hns3_adapter *hns, uint16_t vlan_id, int on) 433 { 434 struct hns3_hw *hw = &hns->hw; 435 bool writen_to_tbl = false; 436 int ret = 0; 437 438 /* 439 * When vlan filter is enabled, hardware regards packets without vlan 440 * as packets with vlan 0. So, to receive packets without vlan, vlan id 441 * 0 is not allowed to be removed by rte_eth_dev_vlan_filter. 442 */ 443 if (on == 0 && vlan_id == 0) 444 return 0; 445 446 /* 447 * When port base vlan enabled, we use port base vlan as the vlan 448 * filter condition. In this case, we don't update vlan filter table 449 * when user add new vlan or remove exist vlan, just update the 450 * vlan list. The vlan id in vlan list will be written in vlan filter 451 * table until port base vlan disabled 452 */ 453 if (hw->port_base_vlan_cfg.state == HNS3_PORT_BASE_VLAN_DISABLE) { 454 ret = hns3_set_port_vlan_filter(hns, vlan_id, on); 455 writen_to_tbl = true; 456 } 457 458 if (ret == 0) { 459 if (on) 460 hns3_add_dev_vlan_table(hns, vlan_id, writen_to_tbl); 461 else 462 hns3_rm_dev_vlan_table(hns, vlan_id); 463 } 464 return ret; 465 } 466 467 static int 468 hns3_vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on) 469 { 470 struct hns3_adapter *hns = dev->data->dev_private; 471 struct hns3_hw *hw = &hns->hw; 472 int ret; 473 474 rte_spinlock_lock(&hw->lock); 475 ret = hns3_vlan_filter_configure(hns, vlan_id, on); 476 rte_spinlock_unlock(&hw->lock); 477 return ret; 478 } 479 480 static int 481 hns3_vlan_tpid_configure(struct hns3_adapter *hns, enum rte_vlan_type vlan_type, 482 uint16_t tpid) 483 { 484 struct hns3_rx_vlan_type_cfg_cmd *rx_req; 485 struct hns3_tx_vlan_type_cfg_cmd *tx_req; 486 struct hns3_hw *hw = &hns->hw; 487 struct hns3_cmd_desc desc; 488 int ret; 489 490 if ((vlan_type != RTE_ETH_VLAN_TYPE_INNER && 491 vlan_type != RTE_ETH_VLAN_TYPE_OUTER)) { 492 hns3_err(hw, "Unsupported vlan type, vlan_type =%d", vlan_type); 493 return -EINVAL; 494 } 495 496 if (tpid != RTE_ETHER_TYPE_VLAN) { 497 hns3_err(hw, "Unsupported vlan tpid, vlan_type =%d", vlan_type); 498 return -EINVAL; 499 } 500 501 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_MAC_VLAN_TYPE_ID, false); 502 rx_req = (struct hns3_rx_vlan_type_cfg_cmd *)desc.data; 503 504 if (vlan_type == RTE_ETH_VLAN_TYPE_OUTER) { 505 rx_req->ot_fst_vlan_type = rte_cpu_to_le_16(tpid); 506 rx_req->ot_sec_vlan_type = rte_cpu_to_le_16(tpid); 507 } else if (vlan_type == RTE_ETH_VLAN_TYPE_INNER) { 508 rx_req->ot_fst_vlan_type = rte_cpu_to_le_16(tpid); 509 rx_req->ot_sec_vlan_type = rte_cpu_to_le_16(tpid); 510 rx_req->in_fst_vlan_type = rte_cpu_to_le_16(tpid); 511 rx_req->in_sec_vlan_type = rte_cpu_to_le_16(tpid); 512 } 513 514 ret = hns3_cmd_send(hw, &desc, 1); 515 if (ret) { 516 hns3_err(hw, "Send rxvlan protocol type command fail, ret =%d", 517 ret); 518 return ret; 519 } 520 521 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_MAC_VLAN_INSERT, false); 522 523 tx_req = (struct hns3_tx_vlan_type_cfg_cmd *)desc.data; 524 tx_req->ot_vlan_type = rte_cpu_to_le_16(tpid); 525 tx_req->in_vlan_type = rte_cpu_to_le_16(tpid); 526 527 ret = hns3_cmd_send(hw, &desc, 1); 528 if (ret) 529 hns3_err(hw, "Send txvlan protocol type command fail, ret =%d", 530 ret); 531 return ret; 532 } 533 534 static int 535 hns3_vlan_tpid_set(struct rte_eth_dev *dev, enum rte_vlan_type vlan_type, 536 uint16_t tpid) 537 { 538 struct hns3_adapter *hns = dev->data->dev_private; 539 struct hns3_hw *hw = &hns->hw; 540 int ret; 541 542 rte_spinlock_lock(&hw->lock); 543 ret = hns3_vlan_tpid_configure(hns, vlan_type, tpid); 544 rte_spinlock_unlock(&hw->lock); 545 return ret; 546 } 547 548 static int 549 hns3_set_vlan_rx_offload_cfg(struct hns3_adapter *hns, 550 struct hns3_rx_vtag_cfg *vcfg) 551 { 552 struct hns3_vport_vtag_rx_cfg_cmd *req; 553 struct hns3_hw *hw = &hns->hw; 554 struct hns3_cmd_desc desc; 555 uint16_t vport_id; 556 uint8_t bitmap; 557 int ret; 558 559 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_VLAN_PORT_RX_CFG, false); 560 561 req = (struct hns3_vport_vtag_rx_cfg_cmd *)desc.data; 562 hns3_set_bit(req->vport_vlan_cfg, HNS3_REM_TAG1_EN_B, 563 vcfg->strip_tag1_en ? 1 : 0); 564 hns3_set_bit(req->vport_vlan_cfg, HNS3_REM_TAG2_EN_B, 565 vcfg->strip_tag2_en ? 1 : 0); 566 hns3_set_bit(req->vport_vlan_cfg, HNS3_SHOW_TAG1_EN_B, 567 vcfg->vlan1_vlan_prionly ? 1 : 0); 568 hns3_set_bit(req->vport_vlan_cfg, HNS3_SHOW_TAG2_EN_B, 569 vcfg->vlan2_vlan_prionly ? 1 : 0); 570 571 /* firmware will ignore this configuration for PCI_REVISION_ID_HIP08 */ 572 hns3_set_bit(req->vport_vlan_cfg, HNS3_DISCARD_TAG1_EN_B, 573 vcfg->strip_tag1_discard_en ? 1 : 0); 574 hns3_set_bit(req->vport_vlan_cfg, HNS3_DISCARD_TAG2_EN_B, 575 vcfg->strip_tag2_discard_en ? 1 : 0); 576 /* 577 * In current version VF is not supported when PF is driven by DPDK 578 * driver, just need to configure parameters for PF vport. 579 */ 580 vport_id = HNS3_PF_FUNC_ID; 581 req->vf_offset = vport_id / HNS3_VF_NUM_PER_CMD; 582 bitmap = 1 << (vport_id % HNS3_VF_NUM_PER_BYTE); 583 req->vf_bitmap[req->vf_offset] = bitmap; 584 585 ret = hns3_cmd_send(hw, &desc, 1); 586 if (ret) 587 hns3_err(hw, "Send port rxvlan cfg command fail, ret =%d", ret); 588 return ret; 589 } 590 591 static int 592 hns3_en_hw_strip_rxvtag(struct hns3_adapter *hns, bool enable) 593 { 594 struct hns3_rx_vtag_cfg rxvlan_cfg; 595 struct hns3_hw *hw = &hns->hw; 596 int ret; 597 598 if (hw->port_base_vlan_cfg.state == HNS3_PORT_BASE_VLAN_DISABLE) { 599 rxvlan_cfg.strip_tag1_en = false; 600 rxvlan_cfg.strip_tag2_en = enable; 601 rxvlan_cfg.strip_tag2_discard_en = false; 602 } else { 603 rxvlan_cfg.strip_tag1_en = enable; 604 rxvlan_cfg.strip_tag2_en = true; 605 rxvlan_cfg.strip_tag2_discard_en = true; 606 } 607 608 rxvlan_cfg.strip_tag1_discard_en = false; 609 rxvlan_cfg.vlan1_vlan_prionly = false; 610 rxvlan_cfg.vlan2_vlan_prionly = false; 611 rxvlan_cfg.rx_vlan_offload_en = enable; 612 613 ret = hns3_set_vlan_rx_offload_cfg(hns, &rxvlan_cfg); 614 if (ret) { 615 hns3_err(hw, "%s strip rx vtag failed, ret = %d.", 616 enable ? "enable" : "disable", ret); 617 return ret; 618 } 619 620 memcpy(&hns->pf.vtag_config.rx_vcfg, &rxvlan_cfg, 621 sizeof(struct hns3_rx_vtag_cfg)); 622 623 return ret; 624 } 625 626 static int 627 hns3_set_vlan_filter_ctrl(struct hns3_hw *hw, uint8_t vlan_type, 628 uint8_t fe_type, bool filter_en, uint8_t vf_id) 629 { 630 struct hns3_vlan_filter_ctrl_cmd *req; 631 struct hns3_cmd_desc desc; 632 int ret; 633 634 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_VLAN_FILTER_CTRL, false); 635 636 req = (struct hns3_vlan_filter_ctrl_cmd *)desc.data; 637 req->vlan_type = vlan_type; 638 req->vlan_fe = filter_en ? fe_type : 0; 639 req->vf_id = vf_id; 640 641 ret = hns3_cmd_send(hw, &desc, 1); 642 if (ret) 643 hns3_err(hw, "set vlan filter fail, ret =%d", ret); 644 645 return ret; 646 } 647 648 static int 649 hns3_vlan_filter_init(struct hns3_adapter *hns) 650 { 651 struct hns3_hw *hw = &hns->hw; 652 int ret; 653 654 ret = hns3_set_vlan_filter_ctrl(hw, HNS3_FILTER_TYPE_VF, 655 HNS3_FILTER_FE_EGRESS, false, 656 HNS3_PF_FUNC_ID); 657 if (ret) { 658 hns3_err(hw, "failed to init vf vlan filter, ret = %d", ret); 659 return ret; 660 } 661 662 ret = hns3_set_vlan_filter_ctrl(hw, HNS3_FILTER_TYPE_PORT, 663 HNS3_FILTER_FE_INGRESS, false, 664 HNS3_PF_FUNC_ID); 665 if (ret) 666 hns3_err(hw, "failed to init port vlan filter, ret = %d", ret); 667 668 return ret; 669 } 670 671 static int 672 hns3_enable_vlan_filter(struct hns3_adapter *hns, bool enable) 673 { 674 struct hns3_hw *hw = &hns->hw; 675 int ret; 676 677 ret = hns3_set_vlan_filter_ctrl(hw, HNS3_FILTER_TYPE_PORT, 678 HNS3_FILTER_FE_INGRESS, enable, 679 HNS3_PF_FUNC_ID); 680 if (ret) 681 hns3_err(hw, "failed to %s port vlan filter, ret = %d", 682 enable ? "enable" : "disable", ret); 683 684 return ret; 685 } 686 687 static int 688 hns3_vlan_offload_set(struct rte_eth_dev *dev, int mask) 689 { 690 struct hns3_adapter *hns = dev->data->dev_private; 691 struct hns3_hw *hw = &hns->hw; 692 struct rte_eth_rxmode *rxmode; 693 unsigned int tmp_mask; 694 bool enable; 695 int ret = 0; 696 697 rte_spinlock_lock(&hw->lock); 698 rxmode = &dev->data->dev_conf.rxmode; 699 tmp_mask = (unsigned int)mask; 700 if (tmp_mask & RTE_ETH_VLAN_FILTER_MASK) { 701 /* ignore vlan filter configuration during promiscuous mode */ 702 if (!dev->data->promiscuous) { 703 /* Enable or disable VLAN filter */ 704 enable = rxmode->offloads & RTE_ETH_RX_OFFLOAD_VLAN_FILTER ? 705 true : false; 706 707 ret = hns3_enable_vlan_filter(hns, enable); 708 if (ret) { 709 rte_spinlock_unlock(&hw->lock); 710 hns3_err(hw, "failed to %s rx filter, ret = %d", 711 enable ? "enable" : "disable", ret); 712 return ret; 713 } 714 } 715 } 716 717 if (tmp_mask & RTE_ETH_VLAN_STRIP_MASK) { 718 /* Enable or disable VLAN stripping */ 719 enable = rxmode->offloads & RTE_ETH_RX_OFFLOAD_VLAN_STRIP ? 720 true : false; 721 722 ret = hns3_en_hw_strip_rxvtag(hns, enable); 723 if (ret) { 724 rte_spinlock_unlock(&hw->lock); 725 hns3_err(hw, "failed to %s rx strip, ret = %d", 726 enable ? "enable" : "disable", ret); 727 return ret; 728 } 729 } 730 731 rte_spinlock_unlock(&hw->lock); 732 733 return ret; 734 } 735 736 static int 737 hns3_set_vlan_tx_offload_cfg(struct hns3_adapter *hns, 738 struct hns3_tx_vtag_cfg *vcfg) 739 { 740 struct hns3_vport_vtag_tx_cfg_cmd *req; 741 struct hns3_cmd_desc desc; 742 struct hns3_hw *hw = &hns->hw; 743 uint16_t vport_id; 744 uint8_t bitmap; 745 int ret; 746 747 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_VLAN_PORT_TX_CFG, false); 748 749 req = (struct hns3_vport_vtag_tx_cfg_cmd *)desc.data; 750 req->def_vlan_tag1 = vcfg->default_tag1; 751 req->def_vlan_tag2 = vcfg->default_tag2; 752 hns3_set_bit(req->vport_vlan_cfg, HNS3_ACCEPT_TAG1_B, 753 vcfg->accept_tag1 ? 1 : 0); 754 hns3_set_bit(req->vport_vlan_cfg, HNS3_ACCEPT_UNTAG1_B, 755 vcfg->accept_untag1 ? 1 : 0); 756 hns3_set_bit(req->vport_vlan_cfg, HNS3_ACCEPT_TAG2_B, 757 vcfg->accept_tag2 ? 1 : 0); 758 hns3_set_bit(req->vport_vlan_cfg, HNS3_ACCEPT_UNTAG2_B, 759 vcfg->accept_untag2 ? 1 : 0); 760 hns3_set_bit(req->vport_vlan_cfg, HNS3_PORT_INS_TAG1_EN_B, 761 vcfg->insert_tag1_en ? 1 : 0); 762 hns3_set_bit(req->vport_vlan_cfg, HNS3_PORT_INS_TAG2_EN_B, 763 vcfg->insert_tag2_en ? 1 : 0); 764 hns3_set_bit(req->vport_vlan_cfg, HNS3_CFG_NIC_ROCE_SEL_B, 0); 765 766 /* firmware will ignore this configuration for PCI_REVISION_ID_HIP08 */ 767 hns3_set_bit(req->vport_vlan_cfg, HNS3_TAG_SHIFT_MODE_EN_B, 768 vcfg->tag_shift_mode_en ? 1 : 0); 769 770 /* 771 * In current version VF is not supported when PF is driven by DPDK 772 * driver, just need to configure parameters for PF vport. 773 */ 774 vport_id = HNS3_PF_FUNC_ID; 775 req->vf_offset = vport_id / HNS3_VF_NUM_PER_CMD; 776 bitmap = 1 << (vport_id % HNS3_VF_NUM_PER_BYTE); 777 req->vf_bitmap[req->vf_offset] = bitmap; 778 779 ret = hns3_cmd_send(hw, &desc, 1); 780 if (ret) 781 hns3_err(hw, "Send port txvlan cfg command fail, ret =%d", ret); 782 783 return ret; 784 } 785 786 static int 787 hns3_vlan_txvlan_cfg(struct hns3_adapter *hns, uint16_t port_base_vlan_state, 788 uint16_t pvid) 789 { 790 struct hns3_hw *hw = &hns->hw; 791 struct hns3_tx_vtag_cfg txvlan_cfg; 792 int ret; 793 794 if (port_base_vlan_state == HNS3_PORT_BASE_VLAN_DISABLE) { 795 txvlan_cfg.accept_tag1 = true; 796 txvlan_cfg.insert_tag1_en = false; 797 txvlan_cfg.default_tag1 = 0; 798 } else { 799 txvlan_cfg.accept_tag1 = 800 hw->vlan_mode == HNS3_HW_SHIFT_AND_DISCARD_MODE; 801 txvlan_cfg.insert_tag1_en = true; 802 txvlan_cfg.default_tag1 = pvid; 803 } 804 805 txvlan_cfg.accept_untag1 = true; 806 txvlan_cfg.accept_tag2 = true; 807 txvlan_cfg.accept_untag2 = true; 808 txvlan_cfg.insert_tag2_en = false; 809 txvlan_cfg.default_tag2 = 0; 810 txvlan_cfg.tag_shift_mode_en = true; 811 812 ret = hns3_set_vlan_tx_offload_cfg(hns, &txvlan_cfg); 813 if (ret) { 814 hns3_err(hw, "pf vlan set pvid failed, pvid =%u ,ret =%d", pvid, 815 ret); 816 return ret; 817 } 818 819 memcpy(&hns->pf.vtag_config.tx_vcfg, &txvlan_cfg, 820 sizeof(struct hns3_tx_vtag_cfg)); 821 822 return ret; 823 } 824 825 826 static void 827 hns3_rm_all_vlan_table(struct hns3_adapter *hns, bool is_del_list) 828 { 829 struct hns3_user_vlan_table *vlan_entry; 830 struct hns3_pf *pf = &hns->pf; 831 832 LIST_FOREACH(vlan_entry, &pf->vlan_list, next) { 833 if (vlan_entry->hd_tbl_status) { 834 hns3_set_port_vlan_filter(hns, vlan_entry->vlan_id, 0); 835 vlan_entry->hd_tbl_status = false; 836 } 837 } 838 839 if (is_del_list) { 840 vlan_entry = LIST_FIRST(&pf->vlan_list); 841 while (vlan_entry) { 842 LIST_REMOVE(vlan_entry, next); 843 rte_free(vlan_entry); 844 vlan_entry = LIST_FIRST(&pf->vlan_list); 845 } 846 } 847 } 848 849 static void 850 hns3_add_all_vlan_table(struct hns3_adapter *hns) 851 { 852 struct hns3_user_vlan_table *vlan_entry; 853 struct hns3_pf *pf = &hns->pf; 854 855 LIST_FOREACH(vlan_entry, &pf->vlan_list, next) { 856 if (!vlan_entry->hd_tbl_status) { 857 hns3_set_port_vlan_filter(hns, vlan_entry->vlan_id, 1); 858 vlan_entry->hd_tbl_status = true; 859 } 860 } 861 } 862 863 static void 864 hns3_remove_all_vlan_table(struct hns3_adapter *hns) 865 { 866 struct hns3_hw *hw = &hns->hw; 867 int ret; 868 869 hns3_rm_all_vlan_table(hns, true); 870 if (hw->port_base_vlan_cfg.pvid != HNS3_INVALID_PVID) { 871 ret = hns3_set_port_vlan_filter(hns, 872 hw->port_base_vlan_cfg.pvid, 0); 873 if (ret) { 874 hns3_err(hw, "Failed to remove all vlan table, ret =%d", 875 ret); 876 return; 877 } 878 } 879 } 880 881 static int 882 hns3_update_vlan_filter_entries(struct hns3_adapter *hns, 883 uint16_t port_base_vlan_state, uint16_t new_pvid) 884 { 885 struct hns3_hw *hw = &hns->hw; 886 uint16_t old_pvid; 887 int ret; 888 889 if (port_base_vlan_state == HNS3_PORT_BASE_VLAN_ENABLE) { 890 old_pvid = hw->port_base_vlan_cfg.pvid; 891 if (old_pvid != HNS3_INVALID_PVID) { 892 ret = hns3_set_port_vlan_filter(hns, old_pvid, 0); 893 if (ret) { 894 hns3_err(hw, "failed to remove old pvid %u, " 895 "ret = %d", old_pvid, ret); 896 return ret; 897 } 898 } 899 900 hns3_rm_all_vlan_table(hns, false); 901 ret = hns3_set_port_vlan_filter(hns, new_pvid, 1); 902 if (ret) { 903 hns3_err(hw, "failed to add new pvid %u, ret = %d", 904 new_pvid, ret); 905 return ret; 906 } 907 } else { 908 ret = hns3_set_port_vlan_filter(hns, new_pvid, 0); 909 if (ret) { 910 hns3_err(hw, "failed to remove pvid %u, ret = %d", 911 new_pvid, ret); 912 return ret; 913 } 914 915 hns3_add_all_vlan_table(hns); 916 } 917 return 0; 918 } 919 920 static int 921 hns3_en_pvid_strip(struct hns3_adapter *hns, int on) 922 { 923 struct hns3_rx_vtag_cfg *old_cfg = &hns->pf.vtag_config.rx_vcfg; 924 struct hns3_rx_vtag_cfg rx_vlan_cfg; 925 bool rx_strip_en; 926 int ret; 927 928 rx_strip_en = old_cfg->rx_vlan_offload_en; 929 if (on) { 930 rx_vlan_cfg.strip_tag1_en = rx_strip_en; 931 rx_vlan_cfg.strip_tag2_en = true; 932 rx_vlan_cfg.strip_tag2_discard_en = true; 933 } else { 934 rx_vlan_cfg.strip_tag1_en = false; 935 rx_vlan_cfg.strip_tag2_en = rx_strip_en; 936 rx_vlan_cfg.strip_tag2_discard_en = false; 937 } 938 rx_vlan_cfg.strip_tag1_discard_en = false; 939 rx_vlan_cfg.vlan1_vlan_prionly = false; 940 rx_vlan_cfg.vlan2_vlan_prionly = false; 941 rx_vlan_cfg.rx_vlan_offload_en = old_cfg->rx_vlan_offload_en; 942 943 ret = hns3_set_vlan_rx_offload_cfg(hns, &rx_vlan_cfg); 944 if (ret) 945 return ret; 946 947 memcpy(&hns->pf.vtag_config.rx_vcfg, &rx_vlan_cfg, 948 sizeof(struct hns3_rx_vtag_cfg)); 949 950 return ret; 951 } 952 953 static int 954 hns3_vlan_pvid_configure(struct hns3_adapter *hns, uint16_t pvid, int on) 955 { 956 struct hns3_hw *hw = &hns->hw; 957 uint16_t port_base_vlan_state; 958 int ret, err; 959 960 if (on == 0 && pvid != hw->port_base_vlan_cfg.pvid) { 961 if (hw->port_base_vlan_cfg.pvid != HNS3_INVALID_PVID) 962 hns3_warn(hw, "Invalid operation! As current pvid set " 963 "is %u, disable pvid %u is invalid", 964 hw->port_base_vlan_cfg.pvid, pvid); 965 return 0; 966 } 967 968 port_base_vlan_state = on ? HNS3_PORT_BASE_VLAN_ENABLE : 969 HNS3_PORT_BASE_VLAN_DISABLE; 970 ret = hns3_vlan_txvlan_cfg(hns, port_base_vlan_state, pvid); 971 if (ret) { 972 hns3_err(hw, "failed to config tx vlan for pvid, ret = %d", 973 ret); 974 return ret; 975 } 976 977 ret = hns3_en_pvid_strip(hns, on); 978 if (ret) { 979 hns3_err(hw, "failed to config rx vlan strip for pvid, " 980 "ret = %d", ret); 981 goto pvid_vlan_strip_fail; 982 } 983 984 if (pvid == HNS3_INVALID_PVID) 985 goto out; 986 ret = hns3_update_vlan_filter_entries(hns, port_base_vlan_state, pvid); 987 if (ret) { 988 hns3_err(hw, "failed to update vlan filter entries, ret = %d", 989 ret); 990 goto vlan_filter_set_fail; 991 } 992 993 out: 994 hw->port_base_vlan_cfg.state = port_base_vlan_state; 995 hw->port_base_vlan_cfg.pvid = on ? pvid : HNS3_INVALID_PVID; 996 return ret; 997 998 vlan_filter_set_fail: 999 err = hns3_en_pvid_strip(hns, hw->port_base_vlan_cfg.state == 1000 HNS3_PORT_BASE_VLAN_ENABLE); 1001 if (err) 1002 hns3_err(hw, "fail to rollback pvid strip, ret = %d", err); 1003 1004 pvid_vlan_strip_fail: 1005 err = hns3_vlan_txvlan_cfg(hns, hw->port_base_vlan_cfg.state, 1006 hw->port_base_vlan_cfg.pvid); 1007 if (err) 1008 hns3_err(hw, "fail to rollback txvlan status, ret = %d", err); 1009 1010 return ret; 1011 } 1012 1013 static int 1014 hns3_vlan_pvid_set(struct rte_eth_dev *dev, uint16_t pvid, int on) 1015 { 1016 struct hns3_adapter *hns = dev->data->dev_private; 1017 struct hns3_hw *hw = &hns->hw; 1018 bool pvid_en_state_change; 1019 uint16_t pvid_state; 1020 int ret; 1021 1022 if (pvid > RTE_ETHER_MAX_VLAN_ID) { 1023 hns3_err(hw, "Invalid vlan_id = %u > %d", pvid, 1024 RTE_ETHER_MAX_VLAN_ID); 1025 return -EINVAL; 1026 } 1027 1028 /* 1029 * If PVID configuration state change, should refresh the PVID 1030 * configuration state in struct hns3_tx_queue/hns3_rx_queue. 1031 */ 1032 pvid_state = hw->port_base_vlan_cfg.state; 1033 if ((on && pvid_state == HNS3_PORT_BASE_VLAN_ENABLE) || 1034 (!on && pvid_state == HNS3_PORT_BASE_VLAN_DISABLE)) 1035 pvid_en_state_change = false; 1036 else 1037 pvid_en_state_change = true; 1038 1039 rte_spinlock_lock(&hw->lock); 1040 ret = hns3_vlan_pvid_configure(hns, pvid, on); 1041 rte_spinlock_unlock(&hw->lock); 1042 if (ret) 1043 return ret; 1044 /* 1045 * Only in HNS3_SW_SHIFT_AND_MODE the PVID related operation in Tx/Rx 1046 * need be processed by PMD. 1047 */ 1048 if (pvid_en_state_change && 1049 hw->vlan_mode == HNS3_SW_SHIFT_AND_DISCARD_MODE) 1050 hns3_update_all_queues_pvid_proc_en(hw); 1051 1052 return 0; 1053 } 1054 1055 static int 1056 hns3_default_vlan_config(struct hns3_adapter *hns) 1057 { 1058 struct hns3_hw *hw = &hns->hw; 1059 int ret; 1060 1061 /* 1062 * When vlan filter is enabled, hardware regards packets without vlan 1063 * as packets with vlan 0. Therefore, if vlan 0 is not in the vlan 1064 * table, packets without vlan won't be received. So, add vlan 0 as 1065 * the default vlan. 1066 */ 1067 ret = hns3_vlan_filter_configure(hns, 0, 1); 1068 if (ret) 1069 hns3_err(hw, "default vlan 0 config failed, ret =%d", ret); 1070 return ret; 1071 } 1072 1073 static int 1074 hns3_init_vlan_config(struct hns3_adapter *hns) 1075 { 1076 struct hns3_hw *hw = &hns->hw; 1077 int ret; 1078 1079 /* 1080 * This function can be called in the initialization and reset process, 1081 * when in reset process, it means that hardware had been reseted 1082 * successfully and we need to restore the hardware configuration to 1083 * ensure that the hardware configuration remains unchanged before and 1084 * after reset. 1085 */ 1086 if (__atomic_load_n(&hw->reset.resetting, __ATOMIC_RELAXED) == 0) { 1087 hw->port_base_vlan_cfg.state = HNS3_PORT_BASE_VLAN_DISABLE; 1088 hw->port_base_vlan_cfg.pvid = HNS3_INVALID_PVID; 1089 } 1090 1091 ret = hns3_vlan_filter_init(hns); 1092 if (ret) { 1093 hns3_err(hw, "vlan init fail in pf, ret =%d", ret); 1094 return ret; 1095 } 1096 1097 ret = hns3_vlan_tpid_configure(hns, RTE_ETH_VLAN_TYPE_INNER, 1098 RTE_ETHER_TYPE_VLAN); 1099 if (ret) { 1100 hns3_err(hw, "tpid set fail in pf, ret =%d", ret); 1101 return ret; 1102 } 1103 1104 /* 1105 * When in the reinit dev stage of the reset process, the following 1106 * vlan-related configurations may differ from those at initialization, 1107 * we will restore configurations to hardware in hns3_restore_vlan_table 1108 * and hns3_restore_vlan_conf later. 1109 */ 1110 if (__atomic_load_n(&hw->reset.resetting, __ATOMIC_RELAXED) == 0) { 1111 ret = hns3_vlan_pvid_configure(hns, HNS3_INVALID_PVID, 0); 1112 if (ret) { 1113 hns3_err(hw, "pvid set fail in pf, ret =%d", ret); 1114 return ret; 1115 } 1116 1117 ret = hns3_en_hw_strip_rxvtag(hns, false); 1118 if (ret) { 1119 hns3_err(hw, "rx strip configure fail in pf, ret =%d", 1120 ret); 1121 return ret; 1122 } 1123 } 1124 1125 return hns3_default_vlan_config(hns); 1126 } 1127 1128 static int 1129 hns3_restore_vlan_conf(struct hns3_adapter *hns) 1130 { 1131 struct hns3_pf *pf = &hns->pf; 1132 struct hns3_hw *hw = &hns->hw; 1133 uint64_t offloads; 1134 bool enable; 1135 int ret; 1136 1137 if (!hw->data->promiscuous) { 1138 /* restore vlan filter states */ 1139 offloads = hw->data->dev_conf.rxmode.offloads; 1140 enable = offloads & RTE_ETH_RX_OFFLOAD_VLAN_FILTER ? true : false; 1141 ret = hns3_enable_vlan_filter(hns, enable); 1142 if (ret) { 1143 hns3_err(hw, "failed to restore vlan rx filter conf, " 1144 "ret = %d", ret); 1145 return ret; 1146 } 1147 } 1148 1149 ret = hns3_set_vlan_rx_offload_cfg(hns, &pf->vtag_config.rx_vcfg); 1150 if (ret) { 1151 hns3_err(hw, "failed to restore vlan rx conf, ret = %d", ret); 1152 return ret; 1153 } 1154 1155 ret = hns3_set_vlan_tx_offload_cfg(hns, &pf->vtag_config.tx_vcfg); 1156 if (ret) 1157 hns3_err(hw, "failed to restore vlan tx conf, ret = %d", ret); 1158 1159 return ret; 1160 } 1161 1162 static int 1163 hns3_dev_configure_vlan(struct rte_eth_dev *dev) 1164 { 1165 struct hns3_adapter *hns = dev->data->dev_private; 1166 struct rte_eth_dev_data *data = dev->data; 1167 struct rte_eth_txmode *txmode; 1168 struct hns3_hw *hw = &hns->hw; 1169 int mask; 1170 int ret; 1171 1172 txmode = &data->dev_conf.txmode; 1173 if (txmode->hw_vlan_reject_tagged || txmode->hw_vlan_reject_untagged) 1174 hns3_warn(hw, 1175 "hw_vlan_reject_tagged or hw_vlan_reject_untagged " 1176 "configuration is not supported! Ignore these two " 1177 "parameters: hw_vlan_reject_tagged(%u), " 1178 "hw_vlan_reject_untagged(%u)", 1179 txmode->hw_vlan_reject_tagged, 1180 txmode->hw_vlan_reject_untagged); 1181 1182 /* Apply vlan offload setting */ 1183 mask = RTE_ETH_VLAN_STRIP_MASK | RTE_ETH_VLAN_FILTER_MASK; 1184 ret = hns3_vlan_offload_set(dev, mask); 1185 if (ret) { 1186 hns3_err(hw, "dev config rx vlan offload failed, ret = %d", 1187 ret); 1188 return ret; 1189 } 1190 1191 /* 1192 * If pvid config is not set in rte_eth_conf, driver needn't to set 1193 * VLAN pvid related configuration to hardware. 1194 */ 1195 if (txmode->pvid == 0 && txmode->hw_vlan_insert_pvid == 0) 1196 return 0; 1197 1198 /* Apply pvid setting */ 1199 ret = hns3_vlan_pvid_set(dev, txmode->pvid, 1200 txmode->hw_vlan_insert_pvid); 1201 if (ret) 1202 hns3_err(hw, "dev config vlan pvid(%u) failed, ret = %d", 1203 txmode->pvid, ret); 1204 1205 return ret; 1206 } 1207 1208 static int 1209 hns3_config_tso(struct hns3_hw *hw, unsigned int tso_mss_min, 1210 unsigned int tso_mss_max) 1211 { 1212 struct hns3_cfg_tso_status_cmd *req; 1213 struct hns3_cmd_desc desc; 1214 uint16_t tso_mss; 1215 1216 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_TSO_GENERIC_CONFIG, false); 1217 1218 req = (struct hns3_cfg_tso_status_cmd *)desc.data; 1219 1220 tso_mss = 0; 1221 hns3_set_field(tso_mss, HNS3_TSO_MSS_MIN_M, HNS3_TSO_MSS_MIN_S, 1222 tso_mss_min); 1223 req->tso_mss_min = rte_cpu_to_le_16(tso_mss); 1224 1225 tso_mss = 0; 1226 hns3_set_field(tso_mss, HNS3_TSO_MSS_MIN_M, HNS3_TSO_MSS_MIN_S, 1227 tso_mss_max); 1228 req->tso_mss_max = rte_cpu_to_le_16(tso_mss); 1229 1230 return hns3_cmd_send(hw, &desc, 1); 1231 } 1232 1233 static int 1234 hns3_set_umv_space(struct hns3_hw *hw, uint16_t space_size, 1235 uint16_t *allocated_size, bool is_alloc) 1236 { 1237 struct hns3_umv_spc_alc_cmd *req; 1238 struct hns3_cmd_desc desc; 1239 int ret; 1240 1241 req = (struct hns3_umv_spc_alc_cmd *)desc.data; 1242 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_MAC_VLAN_ALLOCATE, false); 1243 hns3_set_bit(req->allocate, HNS3_UMV_SPC_ALC_B, is_alloc ? 0 : 1); 1244 req->space_size = rte_cpu_to_le_32(space_size); 1245 1246 ret = hns3_cmd_send(hw, &desc, 1); 1247 if (ret) { 1248 PMD_INIT_LOG(ERR, "%s umv space failed for cmd_send, ret =%d", 1249 is_alloc ? "allocate" : "free", ret); 1250 return ret; 1251 } 1252 1253 if (is_alloc && allocated_size) 1254 *allocated_size = rte_le_to_cpu_32(desc.data[1]); 1255 1256 return 0; 1257 } 1258 1259 static int 1260 hns3_init_umv_space(struct hns3_hw *hw) 1261 { 1262 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw); 1263 struct hns3_pf *pf = &hns->pf; 1264 uint16_t allocated_size = 0; 1265 int ret; 1266 1267 ret = hns3_set_umv_space(hw, pf->wanted_umv_size, &allocated_size, 1268 true); 1269 if (ret) 1270 return ret; 1271 1272 if (allocated_size < pf->wanted_umv_size) 1273 PMD_INIT_LOG(WARNING, "Alloc umv space failed, want %u, get %u", 1274 pf->wanted_umv_size, allocated_size); 1275 1276 pf->max_umv_size = (!!allocated_size) ? allocated_size : 1277 pf->wanted_umv_size; 1278 pf->used_umv_size = 0; 1279 return 0; 1280 } 1281 1282 static int 1283 hns3_uninit_umv_space(struct hns3_hw *hw) 1284 { 1285 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw); 1286 struct hns3_pf *pf = &hns->pf; 1287 int ret; 1288 1289 if (pf->max_umv_size == 0) 1290 return 0; 1291 1292 ret = hns3_set_umv_space(hw, pf->max_umv_size, NULL, false); 1293 if (ret) 1294 return ret; 1295 1296 pf->max_umv_size = 0; 1297 1298 return 0; 1299 } 1300 1301 static bool 1302 hns3_is_umv_space_full(struct hns3_hw *hw) 1303 { 1304 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw); 1305 struct hns3_pf *pf = &hns->pf; 1306 bool is_full; 1307 1308 is_full = (pf->used_umv_size >= pf->max_umv_size); 1309 1310 return is_full; 1311 } 1312 1313 static void 1314 hns3_update_umv_space(struct hns3_hw *hw, bool is_free) 1315 { 1316 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw); 1317 struct hns3_pf *pf = &hns->pf; 1318 1319 if (is_free) { 1320 if (pf->used_umv_size > 0) 1321 pf->used_umv_size--; 1322 } else 1323 pf->used_umv_size++; 1324 } 1325 1326 static void 1327 hns3_prepare_mac_addr(struct hns3_mac_vlan_tbl_entry_cmd *new_req, 1328 const uint8_t *addr, bool is_mc) 1329 { 1330 const unsigned char *mac_addr = addr; 1331 uint32_t high_val = ((uint32_t)mac_addr[3] << 24) | 1332 ((uint32_t)mac_addr[2] << 16) | 1333 ((uint32_t)mac_addr[1] << 8) | 1334 (uint32_t)mac_addr[0]; 1335 uint32_t low_val = ((uint32_t)mac_addr[5] << 8) | (uint32_t)mac_addr[4]; 1336 1337 hns3_set_bit(new_req->flags, HNS3_MAC_VLAN_BIT0_EN_B, 1); 1338 if (is_mc) { 1339 hns3_set_bit(new_req->entry_type, HNS3_MAC_VLAN_BIT0_EN_B, 0); 1340 hns3_set_bit(new_req->entry_type, HNS3_MAC_VLAN_BIT1_EN_B, 1); 1341 hns3_set_bit(new_req->mc_mac_en, HNS3_MAC_VLAN_BIT0_EN_B, 1); 1342 } 1343 1344 new_req->mac_addr_hi32 = rte_cpu_to_le_32(high_val); 1345 new_req->mac_addr_lo16 = rte_cpu_to_le_16(low_val & 0xffff); 1346 } 1347 1348 static int 1349 hns3_get_mac_vlan_cmd_status(struct hns3_hw *hw, uint16_t cmdq_resp, 1350 uint8_t resp_code, 1351 enum hns3_mac_vlan_tbl_opcode op) 1352 { 1353 if (cmdq_resp) { 1354 hns3_err(hw, "cmdq execute failed for get_mac_vlan_cmd_status,status=%u", 1355 cmdq_resp); 1356 return -EIO; 1357 } 1358 1359 if (op == HNS3_MAC_VLAN_ADD) { 1360 if (resp_code == 0 || resp_code == 1) { 1361 return 0; 1362 } else if (resp_code == HNS3_ADD_UC_OVERFLOW) { 1363 hns3_err(hw, "add mac addr failed for uc_overflow"); 1364 return -ENOSPC; 1365 } else if (resp_code == HNS3_ADD_MC_OVERFLOW) { 1366 hns3_err(hw, "add mac addr failed for mc_overflow"); 1367 return -ENOSPC; 1368 } 1369 1370 hns3_err(hw, "add mac addr failed for undefined, code=%u", 1371 resp_code); 1372 return -EIO; 1373 } else if (op == HNS3_MAC_VLAN_REMOVE) { 1374 if (resp_code == 0) { 1375 return 0; 1376 } else if (resp_code == 1) { 1377 hns3_dbg(hw, "remove mac addr failed for miss"); 1378 return -ENOENT; 1379 } 1380 1381 hns3_err(hw, "remove mac addr failed for undefined, code=%u", 1382 resp_code); 1383 return -EIO; 1384 } else if (op == HNS3_MAC_VLAN_LKUP) { 1385 if (resp_code == 0) { 1386 return 0; 1387 } else if (resp_code == 1) { 1388 hns3_dbg(hw, "lookup mac addr failed for miss"); 1389 return -ENOENT; 1390 } 1391 1392 hns3_err(hw, "lookup mac addr failed for undefined, code=%u", 1393 resp_code); 1394 return -EIO; 1395 } 1396 1397 hns3_err(hw, "unknown opcode for get_mac_vlan_cmd_status, opcode=%u", 1398 op); 1399 1400 return -EINVAL; 1401 } 1402 1403 static int 1404 hns3_lookup_mac_vlan_tbl(struct hns3_hw *hw, 1405 struct hns3_mac_vlan_tbl_entry_cmd *req, 1406 struct hns3_cmd_desc *desc, uint8_t desc_num) 1407 { 1408 uint8_t resp_code; 1409 uint16_t retval; 1410 int ret; 1411 int i; 1412 1413 if (desc_num == HNS3_MC_MAC_VLAN_OPS_DESC_NUM) { 1414 for (i = 0; i < desc_num - 1; i++) { 1415 hns3_cmd_setup_basic_desc(&desc[i], 1416 HNS3_OPC_MAC_VLAN_ADD, true); 1417 desc[i].flag |= rte_cpu_to_le_16(HNS3_CMD_FLAG_NEXT); 1418 if (i == 0) 1419 memcpy(desc[i].data, req, 1420 sizeof(struct hns3_mac_vlan_tbl_entry_cmd)); 1421 } 1422 hns3_cmd_setup_basic_desc(&desc[i], HNS3_OPC_MAC_VLAN_ADD, 1423 true); 1424 } else { 1425 hns3_cmd_setup_basic_desc(&desc[0], HNS3_OPC_MAC_VLAN_ADD, 1426 true); 1427 memcpy(desc[0].data, req, 1428 sizeof(struct hns3_mac_vlan_tbl_entry_cmd)); 1429 } 1430 ret = hns3_cmd_send(hw, desc, desc_num); 1431 if (ret) { 1432 hns3_err(hw, "lookup mac addr failed for cmd_send, ret =%d.", 1433 ret); 1434 return ret; 1435 } 1436 resp_code = (rte_le_to_cpu_32(desc[0].data[0]) >> 8) & 0xff; 1437 retval = rte_le_to_cpu_16(desc[0].retval); 1438 1439 return hns3_get_mac_vlan_cmd_status(hw, retval, resp_code, 1440 HNS3_MAC_VLAN_LKUP); 1441 } 1442 1443 static int 1444 hns3_add_mac_vlan_tbl(struct hns3_hw *hw, 1445 struct hns3_mac_vlan_tbl_entry_cmd *req, 1446 struct hns3_cmd_desc *desc, uint8_t desc_num) 1447 { 1448 uint8_t resp_code; 1449 uint16_t retval; 1450 int cfg_status; 1451 int ret; 1452 int i; 1453 1454 if (desc_num == HNS3_UC_MAC_VLAN_OPS_DESC_NUM) { 1455 hns3_cmd_setup_basic_desc(desc, HNS3_OPC_MAC_VLAN_ADD, false); 1456 memcpy(desc->data, req, 1457 sizeof(struct hns3_mac_vlan_tbl_entry_cmd)); 1458 ret = hns3_cmd_send(hw, desc, desc_num); 1459 resp_code = (rte_le_to_cpu_32(desc->data[0]) >> 8) & 0xff; 1460 retval = rte_le_to_cpu_16(desc->retval); 1461 1462 cfg_status = hns3_get_mac_vlan_cmd_status(hw, retval, resp_code, 1463 HNS3_MAC_VLAN_ADD); 1464 } else { 1465 for (i = 0; i < desc_num; i++) { 1466 hns3_cmd_reuse_desc(&desc[i], false); 1467 if (i == desc_num - 1) 1468 desc[i].flag &= 1469 rte_cpu_to_le_16(~HNS3_CMD_FLAG_NEXT); 1470 else 1471 desc[i].flag |= 1472 rte_cpu_to_le_16(HNS3_CMD_FLAG_NEXT); 1473 } 1474 memcpy(desc[0].data, req, 1475 sizeof(struct hns3_mac_vlan_tbl_entry_cmd)); 1476 desc[0].retval = 0; 1477 ret = hns3_cmd_send(hw, desc, desc_num); 1478 resp_code = (rte_le_to_cpu_32(desc[0].data[0]) >> 8) & 0xff; 1479 retval = rte_le_to_cpu_16(desc[0].retval); 1480 1481 cfg_status = hns3_get_mac_vlan_cmd_status(hw, retval, resp_code, 1482 HNS3_MAC_VLAN_ADD); 1483 } 1484 1485 if (ret) { 1486 hns3_err(hw, "add mac addr failed for cmd_send, ret =%d", ret); 1487 return ret; 1488 } 1489 1490 return cfg_status; 1491 } 1492 1493 static int 1494 hns3_remove_mac_vlan_tbl(struct hns3_hw *hw, 1495 struct hns3_mac_vlan_tbl_entry_cmd *req) 1496 { 1497 struct hns3_cmd_desc desc; 1498 uint8_t resp_code; 1499 uint16_t retval; 1500 int ret; 1501 1502 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_MAC_VLAN_REMOVE, false); 1503 1504 memcpy(desc.data, req, sizeof(struct hns3_mac_vlan_tbl_entry_cmd)); 1505 1506 ret = hns3_cmd_send(hw, &desc, 1); 1507 if (ret) { 1508 hns3_err(hw, "del mac addr failed for cmd_send, ret =%d", ret); 1509 return ret; 1510 } 1511 resp_code = (rte_le_to_cpu_32(desc.data[0]) >> 8) & 0xff; 1512 retval = rte_le_to_cpu_16(desc.retval); 1513 1514 return hns3_get_mac_vlan_cmd_status(hw, retval, resp_code, 1515 HNS3_MAC_VLAN_REMOVE); 1516 } 1517 1518 static int 1519 hns3_add_uc_mac_addr(struct hns3_hw *hw, struct rte_ether_addr *mac_addr) 1520 { 1521 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw); 1522 struct hns3_mac_vlan_tbl_entry_cmd req; 1523 struct hns3_pf *pf = &hns->pf; 1524 struct hns3_cmd_desc desc; 1525 char mac_str[RTE_ETHER_ADDR_FMT_SIZE]; 1526 uint16_t egress_port = 0; 1527 uint8_t vf_id; 1528 int ret; 1529 1530 /* check if mac addr is valid */ 1531 if (!rte_is_valid_assigned_ether_addr(mac_addr)) { 1532 hns3_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE, 1533 mac_addr); 1534 hns3_err(hw, "Add unicast mac addr err! addr(%s) invalid", 1535 mac_str); 1536 return -EINVAL; 1537 } 1538 1539 memset(&req, 0, sizeof(req)); 1540 1541 /* 1542 * In current version VF is not supported when PF is driven by DPDK 1543 * driver, just need to configure parameters for PF vport. 1544 */ 1545 vf_id = HNS3_PF_FUNC_ID; 1546 hns3_set_field(egress_port, HNS3_MAC_EPORT_VFID_M, 1547 HNS3_MAC_EPORT_VFID_S, vf_id); 1548 1549 req.egress_port = rte_cpu_to_le_16(egress_port); 1550 1551 hns3_prepare_mac_addr(&req, mac_addr->addr_bytes, false); 1552 1553 /* 1554 * Lookup the mac address in the mac_vlan table, and add 1555 * it if the entry is inexistent. Repeated unicast entry 1556 * is not allowed in the mac vlan table. 1557 */ 1558 ret = hns3_lookup_mac_vlan_tbl(hw, &req, &desc, 1559 HNS3_UC_MAC_VLAN_OPS_DESC_NUM); 1560 if (ret == -ENOENT) { 1561 if (!hns3_is_umv_space_full(hw)) { 1562 ret = hns3_add_mac_vlan_tbl(hw, &req, &desc, 1563 HNS3_UC_MAC_VLAN_OPS_DESC_NUM); 1564 if (!ret) 1565 hns3_update_umv_space(hw, false); 1566 return ret; 1567 } 1568 1569 hns3_err(hw, "UC MAC table full(%u)", pf->used_umv_size); 1570 1571 return -ENOSPC; 1572 } 1573 1574 hns3_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE, mac_addr); 1575 1576 /* check if we just hit the duplicate */ 1577 if (ret == 0) { 1578 hns3_dbg(hw, "mac addr(%s) has been in the MAC table", mac_str); 1579 return 0; 1580 } 1581 1582 hns3_err(hw, "PF failed to add unicast entry(%s) in the MAC table", 1583 mac_str); 1584 1585 return ret; 1586 } 1587 1588 static int 1589 hns3_remove_uc_mac_addr(struct hns3_hw *hw, struct rte_ether_addr *mac_addr) 1590 { 1591 struct hns3_mac_vlan_tbl_entry_cmd req; 1592 char mac_str[RTE_ETHER_ADDR_FMT_SIZE]; 1593 int ret; 1594 1595 /* check if mac addr is valid */ 1596 if (!rte_is_valid_assigned_ether_addr(mac_addr)) { 1597 hns3_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE, 1598 mac_addr); 1599 hns3_err(hw, "remove unicast mac addr err! addr(%s) invalid", 1600 mac_str); 1601 return -EINVAL; 1602 } 1603 1604 memset(&req, 0, sizeof(req)); 1605 hns3_set_bit(req.entry_type, HNS3_MAC_VLAN_BIT0_EN_B, 0); 1606 hns3_prepare_mac_addr(&req, mac_addr->addr_bytes, false); 1607 ret = hns3_remove_mac_vlan_tbl(hw, &req); 1608 if (ret == -ENOENT) /* mac addr isn't existent in the mac vlan table. */ 1609 return 0; 1610 else if (ret == 0) 1611 hns3_update_umv_space(hw, true); 1612 1613 return ret; 1614 } 1615 1616 static int 1617 hns3_set_default_mac_addr(struct rte_eth_dev *dev, 1618 struct rte_ether_addr *mac_addr) 1619 { 1620 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private); 1621 struct rte_ether_addr *oaddr; 1622 char mac_str[RTE_ETHER_ADDR_FMT_SIZE]; 1623 int ret, ret_val; 1624 1625 rte_spinlock_lock(&hw->lock); 1626 oaddr = (struct rte_ether_addr *)hw->mac.mac_addr; 1627 ret = hw->ops.del_uc_mac_addr(hw, oaddr); 1628 if (ret) { 1629 hns3_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE, 1630 oaddr); 1631 hns3_warn(hw, "Remove old uc mac address(%s) fail: %d", 1632 mac_str, ret); 1633 1634 rte_spinlock_unlock(&hw->lock); 1635 return ret; 1636 } 1637 1638 ret = hw->ops.add_uc_mac_addr(hw, mac_addr); 1639 if (ret) { 1640 hns3_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE, 1641 mac_addr); 1642 hns3_err(hw, "Failed to set mac addr(%s): %d", mac_str, ret); 1643 goto err_add_uc_addr; 1644 } 1645 1646 ret = hns3_pause_addr_cfg(hw, mac_addr->addr_bytes); 1647 if (ret) { 1648 hns3_err(hw, "Failed to configure mac pause address: %d", ret); 1649 goto err_pause_addr_cfg; 1650 } 1651 1652 rte_ether_addr_copy(mac_addr, 1653 (struct rte_ether_addr *)hw->mac.mac_addr); 1654 rte_spinlock_unlock(&hw->lock); 1655 1656 return 0; 1657 1658 err_pause_addr_cfg: 1659 ret_val = hw->ops.del_uc_mac_addr(hw, mac_addr); 1660 if (ret_val) { 1661 hns3_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE, 1662 mac_addr); 1663 hns3_warn(hw, 1664 "Failed to roll back to del set mac addr(%s): %d", 1665 mac_str, ret_val); 1666 } 1667 1668 err_add_uc_addr: 1669 ret_val = hw->ops.add_uc_mac_addr(hw, oaddr); 1670 if (ret_val) { 1671 hns3_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE, oaddr); 1672 hns3_warn(hw, "Failed to restore old uc mac addr(%s): %d", 1673 mac_str, ret_val); 1674 } 1675 rte_spinlock_unlock(&hw->lock); 1676 1677 return ret; 1678 } 1679 1680 static void 1681 hns3_update_desc_vfid(struct hns3_cmd_desc *desc, uint8_t vfid, bool clr) 1682 { 1683 #define HNS3_VF_NUM_IN_FIRST_DESC 192 1684 uint8_t word_num; 1685 uint8_t bit_num; 1686 1687 if (vfid < HNS3_VF_NUM_IN_FIRST_DESC) { 1688 word_num = vfid / 32; 1689 bit_num = vfid % 32; 1690 if (clr) 1691 desc[1].data[word_num] &= 1692 rte_cpu_to_le_32(~(1UL << bit_num)); 1693 else 1694 desc[1].data[word_num] |= 1695 rte_cpu_to_le_32(1UL << bit_num); 1696 } else { 1697 word_num = (vfid - HNS3_VF_NUM_IN_FIRST_DESC) / 32; 1698 bit_num = vfid % 32; 1699 if (clr) 1700 desc[2].data[word_num] &= 1701 rte_cpu_to_le_32(~(1UL << bit_num)); 1702 else 1703 desc[2].data[word_num] |= 1704 rte_cpu_to_le_32(1UL << bit_num); 1705 } 1706 } 1707 1708 static int 1709 hns3_add_mc_mac_addr(struct hns3_hw *hw, struct rte_ether_addr *mac_addr) 1710 { 1711 struct hns3_cmd_desc desc[HNS3_MC_MAC_VLAN_OPS_DESC_NUM]; 1712 struct hns3_mac_vlan_tbl_entry_cmd req; 1713 char mac_str[RTE_ETHER_ADDR_FMT_SIZE]; 1714 uint8_t vf_id; 1715 int ret; 1716 int idx; 1717 1718 /* Check if mac addr is valid */ 1719 if (!rte_is_multicast_ether_addr(mac_addr)) { 1720 hns3_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE, 1721 mac_addr); 1722 hns3_err(hw, "failed to add mc mac addr, addr(%s) invalid", 1723 mac_str); 1724 return -EINVAL; 1725 } 1726 1727 memset(&req, 0, sizeof(req)); 1728 hns3_set_bit(req.entry_type, HNS3_MAC_VLAN_BIT0_EN_B, 0); 1729 hns3_prepare_mac_addr(&req, mac_addr->addr_bytes, true); 1730 ret = hns3_lookup_mac_vlan_tbl(hw, &req, desc, 1731 HNS3_MC_MAC_VLAN_OPS_DESC_NUM); 1732 if (ret) { 1733 /* This mac addr do not exist, add new entry for it */ 1734 for (idx = 0; idx < HNS3_MC_MAC_VLAN_OPS_DESC_NUM; idx++) 1735 memset(desc[idx].data, 0, sizeof(desc[idx].data)); 1736 } 1737 1738 /* 1739 * In current version VF is not supported when PF is driven by DPDK 1740 * driver, just need to configure parameters for PF vport. 1741 */ 1742 vf_id = HNS3_PF_FUNC_ID; 1743 hns3_update_desc_vfid(desc, vf_id, false); 1744 ret = hns3_add_mac_vlan_tbl(hw, &req, desc, 1745 HNS3_MC_MAC_VLAN_OPS_DESC_NUM); 1746 if (ret) { 1747 if (ret == -ENOSPC) 1748 hns3_err(hw, "mc mac vlan table is full"); 1749 hns3_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE, 1750 mac_addr); 1751 hns3_err(hw, "failed to add mc mac addr(%s): %d", mac_str, ret); 1752 } 1753 1754 return ret; 1755 } 1756 1757 static int 1758 hns3_remove_mc_mac_addr(struct hns3_hw *hw, struct rte_ether_addr *mac_addr) 1759 { 1760 struct hns3_mac_vlan_tbl_entry_cmd req; 1761 struct hns3_cmd_desc desc[3]; 1762 char mac_str[RTE_ETHER_ADDR_FMT_SIZE]; 1763 uint8_t vf_id; 1764 int ret; 1765 1766 /* Check if mac addr is valid */ 1767 if (!rte_is_multicast_ether_addr(mac_addr)) { 1768 hns3_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE, 1769 mac_addr); 1770 hns3_err(hw, "Failed to rm mc mac addr, addr(%s) invalid", 1771 mac_str); 1772 return -EINVAL; 1773 } 1774 1775 memset(&req, 0, sizeof(req)); 1776 hns3_set_bit(req.entry_type, HNS3_MAC_VLAN_BIT0_EN_B, 0); 1777 hns3_prepare_mac_addr(&req, mac_addr->addr_bytes, true); 1778 ret = hns3_lookup_mac_vlan_tbl(hw, &req, desc, 1779 HNS3_MC_MAC_VLAN_OPS_DESC_NUM); 1780 if (ret == 0) { 1781 /* 1782 * This mac addr exist, remove this handle's VFID for it. 1783 * In current version VF is not supported when PF is driven by 1784 * DPDK driver, just need to configure parameters for PF vport. 1785 */ 1786 vf_id = HNS3_PF_FUNC_ID; 1787 hns3_update_desc_vfid(desc, vf_id, true); 1788 1789 /* All the vfid is zero, so need to delete this entry */ 1790 ret = hns3_remove_mac_vlan_tbl(hw, &req); 1791 } else if (ret == -ENOENT) { 1792 /* This mac addr doesn't exist. */ 1793 return 0; 1794 } 1795 1796 if (ret) { 1797 hns3_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE, 1798 mac_addr); 1799 hns3_err(hw, "Failed to rm mc mac addr(%s): %d", mac_str, ret); 1800 } 1801 1802 return ret; 1803 } 1804 1805 static int 1806 hns3_check_mq_mode(struct rte_eth_dev *dev) 1807 { 1808 enum rte_eth_rx_mq_mode rx_mq_mode = dev->data->dev_conf.rxmode.mq_mode; 1809 enum rte_eth_tx_mq_mode tx_mq_mode = dev->data->dev_conf.txmode.mq_mode; 1810 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private); 1811 struct hns3_pf *pf = HNS3_DEV_PRIVATE_TO_PF(dev->data->dev_private); 1812 struct rte_eth_dcb_rx_conf *dcb_rx_conf; 1813 struct rte_eth_dcb_tx_conf *dcb_tx_conf; 1814 uint8_t num_tc; 1815 int max_tc = 0; 1816 int i; 1817 1818 if (((uint32_t)rx_mq_mode & RTE_ETH_MQ_RX_VMDQ_FLAG) || 1819 (tx_mq_mode == RTE_ETH_MQ_TX_VMDQ_DCB || 1820 tx_mq_mode == RTE_ETH_MQ_TX_VMDQ_ONLY)) { 1821 hns3_err(hw, "VMDQ is not supported, rx_mq_mode = %d, tx_mq_mode = %d.", 1822 rx_mq_mode, tx_mq_mode); 1823 return -EOPNOTSUPP; 1824 } 1825 1826 dcb_rx_conf = &dev->data->dev_conf.rx_adv_conf.dcb_rx_conf; 1827 dcb_tx_conf = &dev->data->dev_conf.tx_adv_conf.dcb_tx_conf; 1828 if ((uint32_t)rx_mq_mode & RTE_ETH_MQ_RX_DCB_FLAG) { 1829 if (dcb_rx_conf->nb_tcs > pf->tc_max) { 1830 hns3_err(hw, "nb_tcs(%u) > max_tc(%u) driver supported.", 1831 dcb_rx_conf->nb_tcs, pf->tc_max); 1832 return -EINVAL; 1833 } 1834 1835 if (!(dcb_rx_conf->nb_tcs == HNS3_4_TCS || 1836 dcb_rx_conf->nb_tcs == HNS3_8_TCS)) { 1837 hns3_err(hw, "on RTE_ETH_MQ_RX_DCB_RSS mode, " 1838 "nb_tcs(%d) != %d or %d in rx direction.", 1839 dcb_rx_conf->nb_tcs, HNS3_4_TCS, HNS3_8_TCS); 1840 return -EINVAL; 1841 } 1842 1843 if (dcb_rx_conf->nb_tcs != dcb_tx_conf->nb_tcs) { 1844 hns3_err(hw, "num_tcs(%d) of tx is not equal to rx(%d)", 1845 dcb_tx_conf->nb_tcs, dcb_rx_conf->nb_tcs); 1846 return -EINVAL; 1847 } 1848 1849 for (i = 0; i < HNS3_MAX_USER_PRIO; i++) { 1850 if (dcb_rx_conf->dcb_tc[i] != dcb_tx_conf->dcb_tc[i]) { 1851 hns3_err(hw, "dcb_tc[%d] = %u in rx direction, " 1852 "is not equal to one in tx direction.", 1853 i, dcb_rx_conf->dcb_tc[i]); 1854 return -EINVAL; 1855 } 1856 if (dcb_rx_conf->dcb_tc[i] > max_tc) 1857 max_tc = dcb_rx_conf->dcb_tc[i]; 1858 } 1859 1860 num_tc = max_tc + 1; 1861 if (num_tc > dcb_rx_conf->nb_tcs) { 1862 hns3_err(hw, "max num_tc(%u) mapped > nb_tcs(%u)", 1863 num_tc, dcb_rx_conf->nb_tcs); 1864 return -EINVAL; 1865 } 1866 } 1867 1868 return 0; 1869 } 1870 1871 static int 1872 hns3_bind_ring_with_vector(struct hns3_hw *hw, uint16_t vector_id, bool en, 1873 enum hns3_ring_type queue_type, uint16_t queue_id) 1874 { 1875 struct hns3_cmd_desc desc; 1876 struct hns3_ctrl_vector_chain_cmd *req = 1877 (struct hns3_ctrl_vector_chain_cmd *)desc.data; 1878 enum hns3_opcode_type op; 1879 uint16_t tqp_type_and_id = 0; 1880 uint16_t type; 1881 uint16_t gl; 1882 int ret; 1883 1884 op = en ? HNS3_OPC_ADD_RING_TO_VECTOR : HNS3_OPC_DEL_RING_TO_VECTOR; 1885 hns3_cmd_setup_basic_desc(&desc, op, false); 1886 req->int_vector_id = hns3_get_field(vector_id, HNS3_TQP_INT_ID_L_M, 1887 HNS3_TQP_INT_ID_L_S); 1888 req->int_vector_id_h = hns3_get_field(vector_id, HNS3_TQP_INT_ID_H_M, 1889 HNS3_TQP_INT_ID_H_S); 1890 1891 if (queue_type == HNS3_RING_TYPE_RX) 1892 gl = HNS3_RING_GL_RX; 1893 else 1894 gl = HNS3_RING_GL_TX; 1895 1896 type = queue_type; 1897 1898 hns3_set_field(tqp_type_and_id, HNS3_INT_TYPE_M, HNS3_INT_TYPE_S, 1899 type); 1900 hns3_set_field(tqp_type_and_id, HNS3_TQP_ID_M, HNS3_TQP_ID_S, queue_id); 1901 hns3_set_field(tqp_type_and_id, HNS3_INT_GL_IDX_M, HNS3_INT_GL_IDX_S, 1902 gl); 1903 req->tqp_type_and_id[0] = rte_cpu_to_le_16(tqp_type_and_id); 1904 req->int_cause_num = 1; 1905 ret = hns3_cmd_send(hw, &desc, 1); 1906 if (ret) { 1907 hns3_err(hw, "%s TQP %u fail, vector_id = %u, ret = %d.", 1908 en ? "Map" : "Unmap", queue_id, vector_id, ret); 1909 return ret; 1910 } 1911 1912 return 0; 1913 } 1914 1915 static int 1916 hns3_setup_dcb(struct rte_eth_dev *dev) 1917 { 1918 struct hns3_adapter *hns = dev->data->dev_private; 1919 struct hns3_hw *hw = &hns->hw; 1920 int ret; 1921 1922 if (!hns3_dev_get_support(hw, DCB)) { 1923 hns3_err(hw, "this port does not support dcb configurations."); 1924 return -EOPNOTSUPP; 1925 } 1926 1927 if (hw->current_fc_status == HNS3_FC_STATUS_MAC_PAUSE) { 1928 hns3_err(hw, "MAC pause enabled, cannot config dcb info."); 1929 return -EOPNOTSUPP; 1930 } 1931 1932 ret = hns3_dcb_configure(hns); 1933 if (ret) 1934 hns3_err(hw, "failed to config dcb: %d", ret); 1935 1936 return ret; 1937 } 1938 1939 static int 1940 hns3_check_link_speed(struct hns3_hw *hw, uint32_t link_speeds) 1941 { 1942 int ret; 1943 1944 /* 1945 * Some hardware doesn't support auto-negotiation, but users may not 1946 * configure link_speeds (default 0), which means auto-negotiation. 1947 * In this case, it should return success. 1948 */ 1949 if (link_speeds == RTE_ETH_LINK_SPEED_AUTONEG && 1950 hw->mac.support_autoneg == 0) 1951 return 0; 1952 1953 if (link_speeds != RTE_ETH_LINK_SPEED_AUTONEG) { 1954 ret = hns3_check_port_speed(hw, link_speeds); 1955 if (ret) 1956 return ret; 1957 } 1958 1959 return 0; 1960 } 1961 1962 static int 1963 hns3_check_dev_conf(struct rte_eth_dev *dev) 1964 { 1965 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private); 1966 struct rte_eth_conf *conf = &dev->data->dev_conf; 1967 int ret; 1968 1969 ret = hns3_check_mq_mode(dev); 1970 if (ret) 1971 return ret; 1972 1973 return hns3_check_link_speed(hw, conf->link_speeds); 1974 } 1975 1976 static int 1977 hns3_dev_configure(struct rte_eth_dev *dev) 1978 { 1979 struct hns3_adapter *hns = dev->data->dev_private; 1980 struct rte_eth_conf *conf = &dev->data->dev_conf; 1981 enum rte_eth_rx_mq_mode mq_mode = conf->rxmode.mq_mode; 1982 struct hns3_hw *hw = &hns->hw; 1983 uint16_t nb_rx_q = dev->data->nb_rx_queues; 1984 uint16_t nb_tx_q = dev->data->nb_tx_queues; 1985 struct rte_eth_rss_conf rss_conf; 1986 bool gro_en; 1987 int ret; 1988 1989 hw->cfg_max_queues = RTE_MAX(nb_rx_q, nb_tx_q); 1990 1991 /* 1992 * Some versions of hardware network engine does not support 1993 * individually enable/disable/reset the Tx or Rx queue. These devices 1994 * must enable/disable/reset Tx and Rx queues at the same time. When the 1995 * numbers of Tx queues allocated by upper applications are not equal to 1996 * the numbers of Rx queues, driver needs to setup fake Tx or Rx queues 1997 * to adjust numbers of Tx/Rx queues. otherwise, network engine can not 1998 * work as usual. But these fake queues are imperceptible, and can not 1999 * be used by upper applications. 2000 */ 2001 ret = hns3_set_fake_rx_or_tx_queues(dev, nb_rx_q, nb_tx_q); 2002 if (ret) { 2003 hns3_err(hw, "fail to set Rx/Tx fake queues, ret = %d.", ret); 2004 hw->cfg_max_queues = 0; 2005 return ret; 2006 } 2007 2008 hw->adapter_state = HNS3_NIC_CONFIGURING; 2009 ret = hns3_check_dev_conf(dev); 2010 if (ret) 2011 goto cfg_err; 2012 2013 if ((uint32_t)mq_mode & RTE_ETH_MQ_RX_DCB_FLAG) { 2014 ret = hns3_setup_dcb(dev); 2015 if (ret) 2016 goto cfg_err; 2017 } 2018 2019 if ((uint32_t)mq_mode & RTE_ETH_MQ_RX_RSS_FLAG) { 2020 conf->rxmode.offloads |= RTE_ETH_RX_OFFLOAD_RSS_HASH; 2021 rss_conf = conf->rx_adv_conf.rss_conf; 2022 ret = hns3_dev_rss_hash_update(dev, &rss_conf); 2023 if (ret) 2024 goto cfg_err; 2025 } 2026 2027 ret = hns3_dev_mtu_set(dev, conf->rxmode.mtu); 2028 if (ret != 0) 2029 goto cfg_err; 2030 2031 ret = hns3_mbuf_dyn_rx_timestamp_register(dev, conf); 2032 if (ret) 2033 goto cfg_err; 2034 2035 ret = hns3_dev_configure_vlan(dev); 2036 if (ret) 2037 goto cfg_err; 2038 2039 /* config hardware GRO */ 2040 gro_en = conf->rxmode.offloads & RTE_ETH_RX_OFFLOAD_TCP_LRO ? true : false; 2041 ret = hns3_config_gro(hw, gro_en); 2042 if (ret) 2043 goto cfg_err; 2044 2045 hns3_init_rx_ptype_tble(dev); 2046 hw->adapter_state = HNS3_NIC_CONFIGURED; 2047 2048 return 0; 2049 2050 cfg_err: 2051 hw->cfg_max_queues = 0; 2052 (void)hns3_set_fake_rx_or_tx_queues(dev, 0, 0); 2053 hw->adapter_state = HNS3_NIC_INITIALIZED; 2054 2055 return ret; 2056 } 2057 2058 static int 2059 hns3_set_mac_mtu(struct hns3_hw *hw, uint16_t new_mps) 2060 { 2061 struct hns3_config_max_frm_size_cmd *req; 2062 struct hns3_cmd_desc desc; 2063 2064 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_CONFIG_MAX_FRM_SIZE, false); 2065 2066 req = (struct hns3_config_max_frm_size_cmd *)desc.data; 2067 req->max_frm_size = rte_cpu_to_le_16(new_mps); 2068 req->min_frm_size = RTE_ETHER_MIN_LEN; 2069 2070 return hns3_cmd_send(hw, &desc, 1); 2071 } 2072 2073 static int 2074 hns3_config_mtu(struct hns3_hw *hw, uint16_t mps) 2075 { 2076 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw); 2077 int err; 2078 int ret; 2079 2080 ret = hns3_set_mac_mtu(hw, mps); 2081 if (ret) { 2082 hns3_err(hw, "failed to set mtu, ret = %d", ret); 2083 return ret; 2084 } 2085 2086 ret = hns3_buffer_alloc(hw); 2087 if (ret) { 2088 hns3_err(hw, "failed to allocate buffer, ret = %d", ret); 2089 goto rollback; 2090 } 2091 2092 hns->pf.mps = mps; 2093 2094 return 0; 2095 2096 rollback: 2097 err = hns3_set_mac_mtu(hw, hns->pf.mps); 2098 if (err) 2099 hns3_err(hw, "fail to rollback MTU, err = %d", err); 2100 2101 return ret; 2102 } 2103 2104 static int 2105 hns3_dev_mtu_set(struct rte_eth_dev *dev, uint16_t mtu) 2106 { 2107 struct hns3_adapter *hns = dev->data->dev_private; 2108 uint32_t frame_size = mtu + HNS3_ETH_OVERHEAD; 2109 struct hns3_hw *hw = &hns->hw; 2110 int ret; 2111 2112 if (dev->data->dev_started) { 2113 hns3_err(hw, "Failed to set mtu, port %u must be stopped " 2114 "before configuration", dev->data->port_id); 2115 return -EBUSY; 2116 } 2117 2118 rte_spinlock_lock(&hw->lock); 2119 frame_size = RTE_MAX(frame_size, HNS3_DEFAULT_FRAME_LEN); 2120 2121 /* 2122 * Maximum value of frame_size is HNS3_MAX_FRAME_LEN, so it can safely 2123 * assign to "uint16_t" type variable. 2124 */ 2125 ret = hns3_config_mtu(hw, (uint16_t)frame_size); 2126 if (ret) { 2127 rte_spinlock_unlock(&hw->lock); 2128 hns3_err(hw, "Failed to set mtu, port %u mtu %u: %d", 2129 dev->data->port_id, mtu, ret); 2130 return ret; 2131 } 2132 2133 rte_spinlock_unlock(&hw->lock); 2134 2135 return 0; 2136 } 2137 2138 static uint32_t 2139 hns3_get_copper_port_speed_capa(uint32_t supported_speed) 2140 { 2141 uint32_t speed_capa = 0; 2142 2143 if (supported_speed & HNS3_PHY_LINK_SPEED_10M_HD_BIT) 2144 speed_capa |= RTE_ETH_LINK_SPEED_10M_HD; 2145 if (supported_speed & HNS3_PHY_LINK_SPEED_10M_BIT) 2146 speed_capa |= RTE_ETH_LINK_SPEED_10M; 2147 if (supported_speed & HNS3_PHY_LINK_SPEED_100M_HD_BIT) 2148 speed_capa |= RTE_ETH_LINK_SPEED_100M_HD; 2149 if (supported_speed & HNS3_PHY_LINK_SPEED_100M_BIT) 2150 speed_capa |= RTE_ETH_LINK_SPEED_100M; 2151 if (supported_speed & HNS3_PHY_LINK_SPEED_1000M_BIT) 2152 speed_capa |= RTE_ETH_LINK_SPEED_1G; 2153 2154 return speed_capa; 2155 } 2156 2157 static uint32_t 2158 hns3_get_firber_port_speed_capa(uint32_t supported_speed) 2159 { 2160 uint32_t speed_capa = 0; 2161 2162 if (supported_speed & HNS3_FIBER_LINK_SPEED_1G_BIT) 2163 speed_capa |= RTE_ETH_LINK_SPEED_1G; 2164 if (supported_speed & HNS3_FIBER_LINK_SPEED_10G_BIT) 2165 speed_capa |= RTE_ETH_LINK_SPEED_10G; 2166 if (supported_speed & HNS3_FIBER_LINK_SPEED_25G_BIT) 2167 speed_capa |= RTE_ETH_LINK_SPEED_25G; 2168 if (supported_speed & HNS3_FIBER_LINK_SPEED_40G_BIT) 2169 speed_capa |= RTE_ETH_LINK_SPEED_40G; 2170 if (supported_speed & HNS3_FIBER_LINK_SPEED_50G_BIT) 2171 speed_capa |= RTE_ETH_LINK_SPEED_50G; 2172 if (supported_speed & HNS3_FIBER_LINK_SPEED_100G_BIT) 2173 speed_capa |= RTE_ETH_LINK_SPEED_100G; 2174 if (supported_speed & HNS3_FIBER_LINK_SPEED_200G_BIT) 2175 speed_capa |= RTE_ETH_LINK_SPEED_200G; 2176 2177 return speed_capa; 2178 } 2179 2180 uint32_t 2181 hns3_get_speed_capa(struct hns3_hw *hw) 2182 { 2183 struct hns3_mac *mac = &hw->mac; 2184 uint32_t speed_capa; 2185 2186 if (mac->media_type == HNS3_MEDIA_TYPE_COPPER) 2187 speed_capa = 2188 hns3_get_copper_port_speed_capa(mac->supported_speed); 2189 else 2190 speed_capa = 2191 hns3_get_firber_port_speed_capa(mac->supported_speed); 2192 2193 if (mac->support_autoneg == 0) 2194 speed_capa |= RTE_ETH_LINK_SPEED_FIXED; 2195 2196 return speed_capa; 2197 } 2198 2199 static int 2200 hns3_update_port_link_info(struct rte_eth_dev *eth_dev) 2201 { 2202 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(eth_dev->data->dev_private); 2203 int ret; 2204 2205 (void)hns3_update_link_status(hw); 2206 2207 ret = hns3_update_link_info(eth_dev); 2208 if (ret) 2209 hw->mac.link_status = RTE_ETH_LINK_DOWN; 2210 2211 return ret; 2212 } 2213 2214 static void 2215 hns3_setup_linkstatus(struct rte_eth_dev *eth_dev, 2216 struct rte_eth_link *new_link) 2217 { 2218 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(eth_dev->data->dev_private); 2219 struct hns3_mac *mac = &hw->mac; 2220 2221 switch (mac->link_speed) { 2222 case RTE_ETH_SPEED_NUM_10M: 2223 case RTE_ETH_SPEED_NUM_100M: 2224 case RTE_ETH_SPEED_NUM_1G: 2225 case RTE_ETH_SPEED_NUM_10G: 2226 case RTE_ETH_SPEED_NUM_25G: 2227 case RTE_ETH_SPEED_NUM_40G: 2228 case RTE_ETH_SPEED_NUM_50G: 2229 case RTE_ETH_SPEED_NUM_100G: 2230 case RTE_ETH_SPEED_NUM_200G: 2231 if (mac->link_status) 2232 new_link->link_speed = mac->link_speed; 2233 break; 2234 default: 2235 if (mac->link_status) 2236 new_link->link_speed = RTE_ETH_SPEED_NUM_UNKNOWN; 2237 break; 2238 } 2239 2240 if (!mac->link_status) 2241 new_link->link_speed = RTE_ETH_SPEED_NUM_NONE; 2242 2243 new_link->link_duplex = mac->link_duplex; 2244 new_link->link_status = mac->link_status ? RTE_ETH_LINK_UP : RTE_ETH_LINK_DOWN; 2245 new_link->link_autoneg = mac->link_autoneg; 2246 } 2247 2248 static int 2249 hns3_dev_link_update(struct rte_eth_dev *eth_dev, int wait_to_complete) 2250 { 2251 #define HNS3_LINK_CHECK_INTERVAL 100 /* 100ms */ 2252 #define HNS3_MAX_LINK_CHECK_TIMES 20 /* 2s (100 * 20ms) in total */ 2253 2254 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(eth_dev->data->dev_private); 2255 uint32_t retry_cnt = HNS3_MAX_LINK_CHECK_TIMES; 2256 struct hns3_mac *mac = &hw->mac; 2257 struct rte_eth_link new_link; 2258 int ret; 2259 2260 /* When port is stopped, report link down. */ 2261 if (eth_dev->data->dev_started == 0) { 2262 new_link.link_autoneg = mac->link_autoneg; 2263 new_link.link_duplex = mac->link_duplex; 2264 new_link.link_speed = RTE_ETH_SPEED_NUM_NONE; 2265 new_link.link_status = RTE_ETH_LINK_DOWN; 2266 goto out; 2267 } 2268 2269 do { 2270 ret = hns3_update_port_link_info(eth_dev); 2271 if (ret) { 2272 hns3_err(hw, "failed to get port link info, ret = %d.", 2273 ret); 2274 break; 2275 } 2276 2277 if (!wait_to_complete || mac->link_status == RTE_ETH_LINK_UP) 2278 break; 2279 2280 rte_delay_ms(HNS3_LINK_CHECK_INTERVAL); 2281 } while (retry_cnt--); 2282 2283 memset(&new_link, 0, sizeof(new_link)); 2284 hns3_setup_linkstatus(eth_dev, &new_link); 2285 2286 out: 2287 return rte_eth_linkstatus_set(eth_dev, &new_link); 2288 } 2289 2290 static int 2291 hns3_dev_set_link_up(struct rte_eth_dev *dev) 2292 { 2293 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private); 2294 int ret; 2295 2296 /* 2297 * The "tx_pkt_burst" will be restored. But the secondary process does 2298 * not support the mechanism for notifying the primary process. 2299 */ 2300 if (rte_eal_process_type() != RTE_PROC_PRIMARY) { 2301 hns3_err(hw, "secondary process does not support to set link up."); 2302 return -ENOTSUP; 2303 } 2304 2305 /* 2306 * If device isn't started Rx/Tx function is still disabled, setting 2307 * link up is not allowed. But it is probably better to return success 2308 * to reduce the impact on the upper layer. 2309 */ 2310 if (hw->adapter_state != HNS3_NIC_STARTED) { 2311 hns3_info(hw, "device isn't started, can't set link up."); 2312 return 0; 2313 } 2314 2315 if (!hw->set_link_down) 2316 return 0; 2317 2318 rte_spinlock_lock(&hw->lock); 2319 ret = hns3_cfg_mac_mode(hw, true); 2320 if (ret) { 2321 rte_spinlock_unlock(&hw->lock); 2322 hns3_err(hw, "failed to set link up, ret = %d", ret); 2323 return ret; 2324 } 2325 2326 hw->set_link_down = false; 2327 hns3_start_tx_datapath(dev); 2328 rte_spinlock_unlock(&hw->lock); 2329 2330 return 0; 2331 } 2332 2333 static int 2334 hns3_dev_set_link_down(struct rte_eth_dev *dev) 2335 { 2336 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private); 2337 int ret; 2338 2339 /* 2340 * The "tx_pkt_burst" will be set to dummy function. But the secondary 2341 * process does not support the mechanism for notifying the primary 2342 * process. 2343 */ 2344 if (rte_eal_process_type() != RTE_PROC_PRIMARY) { 2345 hns3_err(hw, "secondary process does not support to set link down."); 2346 return -ENOTSUP; 2347 } 2348 2349 /* 2350 * If device isn't started or the API has been called, link status is 2351 * down, return success. 2352 */ 2353 if (hw->adapter_state != HNS3_NIC_STARTED || hw->set_link_down) 2354 return 0; 2355 2356 rte_spinlock_lock(&hw->lock); 2357 hns3_stop_tx_datapath(dev); 2358 ret = hns3_cfg_mac_mode(hw, false); 2359 if (ret) { 2360 hns3_start_tx_datapath(dev); 2361 rte_spinlock_unlock(&hw->lock); 2362 hns3_err(hw, "failed to set link down, ret = %d", ret); 2363 return ret; 2364 } 2365 2366 hw->set_link_down = true; 2367 rte_spinlock_unlock(&hw->lock); 2368 2369 return 0; 2370 } 2371 2372 static int 2373 hns3_parse_func_status(struct hns3_hw *hw, struct hns3_func_status_cmd *status) 2374 { 2375 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw); 2376 struct hns3_pf *pf = &hns->pf; 2377 2378 if (!(status->pf_state & HNS3_PF_STATE_DONE)) 2379 return -EINVAL; 2380 2381 pf->is_main_pf = (status->pf_state & HNS3_PF_STATE_MAIN) ? true : false; 2382 2383 return 0; 2384 } 2385 2386 static int 2387 hns3_query_function_status(struct hns3_hw *hw) 2388 { 2389 #define HNS3_QUERY_MAX_CNT 10 2390 #define HNS3_QUERY_SLEEP_MSCOEND 1 2391 struct hns3_func_status_cmd *req; 2392 struct hns3_cmd_desc desc; 2393 int timeout = 0; 2394 int ret; 2395 2396 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_QUERY_FUNC_STATUS, true); 2397 req = (struct hns3_func_status_cmd *)desc.data; 2398 2399 do { 2400 ret = hns3_cmd_send(hw, &desc, 1); 2401 if (ret) { 2402 PMD_INIT_LOG(ERR, "query function status failed %d", 2403 ret); 2404 return ret; 2405 } 2406 2407 /* Check pf reset is done */ 2408 if (req->pf_state) 2409 break; 2410 2411 rte_delay_ms(HNS3_QUERY_SLEEP_MSCOEND); 2412 } while (timeout++ < HNS3_QUERY_MAX_CNT); 2413 2414 return hns3_parse_func_status(hw, req); 2415 } 2416 2417 static int 2418 hns3_get_pf_max_tqp_num(struct hns3_hw *hw) 2419 { 2420 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw); 2421 struct hns3_pf *pf = &hns->pf; 2422 2423 if (pf->tqp_config_mode == HNS3_FLEX_MAX_TQP_NUM_MODE) { 2424 /* 2425 * The total_tqps_num obtained from firmware is maximum tqp 2426 * numbers of this port, which should be used for PF and VFs. 2427 * There is no need for pf to have so many tqp numbers in 2428 * most cases. RTE_LIBRTE_HNS3_MAX_TQP_NUM_PER_PF, 2429 * coming from config file, is assigned to maximum queue number 2430 * for the PF of this port by user. So users can modify the 2431 * maximum queue number of PF according to their own application 2432 * scenarios, which is more flexible to use. In addition, many 2433 * memories can be saved due to allocating queue statistics 2434 * room according to the actual number of queues required. The 2435 * maximum queue number of PF for network engine with 2436 * revision_id greater than 0x30 is assigned by config file. 2437 */ 2438 if (RTE_LIBRTE_HNS3_MAX_TQP_NUM_PER_PF <= 0) { 2439 hns3_err(hw, "RTE_LIBRTE_HNS3_MAX_TQP_NUM_PER_PF(%d) " 2440 "must be greater than 0.", 2441 RTE_LIBRTE_HNS3_MAX_TQP_NUM_PER_PF); 2442 return -EINVAL; 2443 } 2444 2445 hw->tqps_num = RTE_MIN(RTE_LIBRTE_HNS3_MAX_TQP_NUM_PER_PF, 2446 hw->total_tqps_num); 2447 } else { 2448 /* 2449 * Due to the limitation on the number of PF interrupts 2450 * available, the maximum queue number assigned to PF on 2451 * the network engine with revision_id 0x21 is 64. 2452 */ 2453 hw->tqps_num = RTE_MIN(hw->total_tqps_num, 2454 HNS3_MAX_TQP_NUM_HIP08_PF); 2455 } 2456 2457 return 0; 2458 } 2459 2460 static int 2461 hns3_query_pf_resource(struct hns3_hw *hw) 2462 { 2463 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw); 2464 struct hns3_pf *pf = &hns->pf; 2465 struct hns3_pf_res_cmd *req; 2466 struct hns3_cmd_desc desc; 2467 int ret; 2468 2469 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_QUERY_PF_RSRC, true); 2470 ret = hns3_cmd_send(hw, &desc, 1); 2471 if (ret) { 2472 PMD_INIT_LOG(ERR, "query pf resource failed %d", ret); 2473 return ret; 2474 } 2475 2476 req = (struct hns3_pf_res_cmd *)desc.data; 2477 hw->total_tqps_num = rte_le_to_cpu_16(req->tqp_num) + 2478 rte_le_to_cpu_16(req->ext_tqp_num); 2479 ret = hns3_get_pf_max_tqp_num(hw); 2480 if (ret) 2481 return ret; 2482 2483 pf->pkt_buf_size = rte_le_to_cpu_16(req->buf_size) << HNS3_BUF_UNIT_S; 2484 pf->func_num = rte_le_to_cpu_16(req->pf_own_fun_number); 2485 2486 if (req->tx_buf_size) 2487 pf->tx_buf_size = 2488 rte_le_to_cpu_16(req->tx_buf_size) << HNS3_BUF_UNIT_S; 2489 else 2490 pf->tx_buf_size = HNS3_DEFAULT_TX_BUF; 2491 2492 pf->tx_buf_size = roundup(pf->tx_buf_size, HNS3_BUF_SIZE_UNIT); 2493 2494 if (req->dv_buf_size) 2495 pf->dv_buf_size = 2496 rte_le_to_cpu_16(req->dv_buf_size) << HNS3_BUF_UNIT_S; 2497 else 2498 pf->dv_buf_size = HNS3_DEFAULT_DV; 2499 2500 pf->dv_buf_size = roundup(pf->dv_buf_size, HNS3_BUF_SIZE_UNIT); 2501 2502 hw->num_msi = 2503 hns3_get_field(rte_le_to_cpu_16(req->nic_pf_intr_vector_number), 2504 HNS3_PF_VEC_NUM_M, HNS3_PF_VEC_NUM_S); 2505 2506 return 0; 2507 } 2508 2509 static void 2510 hns3_parse_cfg(struct hns3_cfg *cfg, struct hns3_cmd_desc *desc) 2511 { 2512 struct hns3_cfg_param_cmd *req; 2513 uint64_t mac_addr_tmp_high; 2514 uint8_t ext_rss_size_max; 2515 uint64_t mac_addr_tmp; 2516 uint32_t i; 2517 2518 req = (struct hns3_cfg_param_cmd *)desc[0].data; 2519 2520 /* get the configuration */ 2521 cfg->tc_num = hns3_get_field(rte_le_to_cpu_32(req->param[0]), 2522 HNS3_CFG_TC_NUM_M, HNS3_CFG_TC_NUM_S); 2523 2524 cfg->phy_addr = hns3_get_field(rte_le_to_cpu_32(req->param[1]), 2525 HNS3_CFG_PHY_ADDR_M, 2526 HNS3_CFG_PHY_ADDR_S); 2527 cfg->media_type = hns3_get_field(rte_le_to_cpu_32(req->param[1]), 2528 HNS3_CFG_MEDIA_TP_M, 2529 HNS3_CFG_MEDIA_TP_S); 2530 /* get mac address */ 2531 mac_addr_tmp = rte_le_to_cpu_32(req->param[2]); 2532 mac_addr_tmp_high = hns3_get_field(rte_le_to_cpu_32(req->param[3]), 2533 HNS3_CFG_MAC_ADDR_H_M, 2534 HNS3_CFG_MAC_ADDR_H_S); 2535 2536 mac_addr_tmp |= (mac_addr_tmp_high << 31) << 1; 2537 2538 cfg->default_speed = hns3_get_field(rte_le_to_cpu_32(req->param[3]), 2539 HNS3_CFG_DEFAULT_SPEED_M, 2540 HNS3_CFG_DEFAULT_SPEED_S); 2541 cfg->rss_size_max = hns3_get_field(rte_le_to_cpu_32(req->param[3]), 2542 HNS3_CFG_RSS_SIZE_M, 2543 HNS3_CFG_RSS_SIZE_S); 2544 2545 for (i = 0; i < RTE_ETHER_ADDR_LEN; i++) 2546 cfg->mac_addr[i] = (mac_addr_tmp >> (8 * i)) & 0xff; 2547 2548 req = (struct hns3_cfg_param_cmd *)desc[1].data; 2549 cfg->numa_node_map = rte_le_to_cpu_32(req->param[0]); 2550 2551 cfg->speed_ability = hns3_get_field(rte_le_to_cpu_32(req->param[1]), 2552 HNS3_CFG_SPEED_ABILITY_M, 2553 HNS3_CFG_SPEED_ABILITY_S); 2554 cfg->umv_space = hns3_get_field(rte_le_to_cpu_32(req->param[1]), 2555 HNS3_CFG_UMV_TBL_SPACE_M, 2556 HNS3_CFG_UMV_TBL_SPACE_S); 2557 if (!cfg->umv_space) 2558 cfg->umv_space = HNS3_DEFAULT_UMV_SPACE_PER_PF; 2559 2560 ext_rss_size_max = hns3_get_field(rte_le_to_cpu_32(req->param[2]), 2561 HNS3_CFG_EXT_RSS_SIZE_M, 2562 HNS3_CFG_EXT_RSS_SIZE_S); 2563 /* 2564 * Field ext_rss_size_max obtained from firmware will be more flexible 2565 * for future changes and expansions, which is an exponent of 2, instead 2566 * of reading out directly. If this field is not zero, hns3 PF PMD 2567 * uses it as rss_size_max under one TC. Device, whose revision 2568 * id is greater than or equal to PCI_REVISION_ID_HIP09_A, obtains the 2569 * maximum number of queues supported under a TC through this field. 2570 */ 2571 if (ext_rss_size_max) 2572 cfg->rss_size_max = 1U << ext_rss_size_max; 2573 } 2574 2575 /* hns3_get_board_cfg: query the static parameter from NCL_config file in flash 2576 * @hw: pointer to struct hns3_hw 2577 * @hcfg: the config structure to be getted 2578 */ 2579 static int 2580 hns3_get_board_cfg(struct hns3_hw *hw, struct hns3_cfg *hcfg) 2581 { 2582 struct hns3_cmd_desc desc[HNS3_PF_CFG_DESC_NUM]; 2583 struct hns3_cfg_param_cmd *req; 2584 uint32_t offset; 2585 uint32_t i; 2586 int ret; 2587 2588 for (i = 0; i < HNS3_PF_CFG_DESC_NUM; i++) { 2589 offset = 0; 2590 req = (struct hns3_cfg_param_cmd *)desc[i].data; 2591 hns3_cmd_setup_basic_desc(&desc[i], HNS3_OPC_GET_CFG_PARAM, 2592 true); 2593 hns3_set_field(offset, HNS3_CFG_OFFSET_M, HNS3_CFG_OFFSET_S, 2594 i * HNS3_CFG_RD_LEN_BYTES); 2595 /* Len should be divided by 4 when send to hardware */ 2596 hns3_set_field(offset, HNS3_CFG_RD_LEN_M, HNS3_CFG_RD_LEN_S, 2597 HNS3_CFG_RD_LEN_BYTES / HNS3_CFG_RD_LEN_UNIT); 2598 req->offset = rte_cpu_to_le_32(offset); 2599 } 2600 2601 ret = hns3_cmd_send(hw, desc, HNS3_PF_CFG_DESC_NUM); 2602 if (ret) { 2603 PMD_INIT_LOG(ERR, "get config failed %d.", ret); 2604 return ret; 2605 } 2606 2607 hns3_parse_cfg(hcfg, desc); 2608 2609 return 0; 2610 } 2611 2612 static int 2613 hns3_parse_speed(int speed_cmd, uint32_t *speed) 2614 { 2615 switch (speed_cmd) { 2616 case HNS3_CFG_SPEED_10M: 2617 *speed = RTE_ETH_SPEED_NUM_10M; 2618 break; 2619 case HNS3_CFG_SPEED_100M: 2620 *speed = RTE_ETH_SPEED_NUM_100M; 2621 break; 2622 case HNS3_CFG_SPEED_1G: 2623 *speed = RTE_ETH_SPEED_NUM_1G; 2624 break; 2625 case HNS3_CFG_SPEED_10G: 2626 *speed = RTE_ETH_SPEED_NUM_10G; 2627 break; 2628 case HNS3_CFG_SPEED_25G: 2629 *speed = RTE_ETH_SPEED_NUM_25G; 2630 break; 2631 case HNS3_CFG_SPEED_40G: 2632 *speed = RTE_ETH_SPEED_NUM_40G; 2633 break; 2634 case HNS3_CFG_SPEED_50G: 2635 *speed = RTE_ETH_SPEED_NUM_50G; 2636 break; 2637 case HNS3_CFG_SPEED_100G: 2638 *speed = RTE_ETH_SPEED_NUM_100G; 2639 break; 2640 case HNS3_CFG_SPEED_200G: 2641 *speed = RTE_ETH_SPEED_NUM_200G; 2642 break; 2643 default: 2644 return -EINVAL; 2645 } 2646 2647 return 0; 2648 } 2649 2650 static int 2651 hns3_get_capability(struct hns3_hw *hw) 2652 { 2653 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw); 2654 struct rte_pci_device *pci_dev; 2655 struct hns3_pf *pf = &hns->pf; 2656 struct rte_eth_dev *eth_dev; 2657 uint16_t device_id; 2658 int ret; 2659 2660 eth_dev = &rte_eth_devices[hw->data->port_id]; 2661 pci_dev = RTE_ETH_DEV_TO_PCI(eth_dev); 2662 device_id = pci_dev->id.device_id; 2663 2664 if (device_id == HNS3_DEV_ID_25GE_RDMA || 2665 device_id == HNS3_DEV_ID_50GE_RDMA || 2666 device_id == HNS3_DEV_ID_100G_RDMA_MACSEC || 2667 device_id == HNS3_DEV_ID_200G_RDMA) 2668 hns3_set_bit(hw->capability, HNS3_DEV_SUPPORT_DCB_B, 1); 2669 2670 ret = hns3_get_pci_revision_id(hw, &hw->revision); 2671 if (ret) 2672 return ret; 2673 2674 ret = hns3_query_mac_stats_reg_num(hw); 2675 if (ret) 2676 return ret; 2677 2678 if (hw->revision < PCI_REVISION_ID_HIP09_A) { 2679 hns3_set_default_dev_specifications(hw); 2680 hw->intr.mapping_mode = HNS3_INTR_MAPPING_VEC_RSV_ONE; 2681 hw->intr.gl_unit = HNS3_INTR_COALESCE_GL_UINT_2US; 2682 hw->tso_mode = HNS3_TSO_SW_CAL_PSEUDO_H_CSUM; 2683 hw->vlan_mode = HNS3_SW_SHIFT_AND_DISCARD_MODE; 2684 hw->drop_stats_mode = HNS3_PKTS_DROP_STATS_MODE1; 2685 hw->min_tx_pkt_len = HNS3_HIP08_MIN_TX_PKT_LEN; 2686 pf->tqp_config_mode = HNS3_FIXED_MAX_TQP_NUM_MODE; 2687 hw->rss_info.ipv6_sctp_offload_supported = false; 2688 hw->udp_cksum_mode = HNS3_SPECIAL_PORT_SW_CKSUM_MODE; 2689 pf->support_multi_tc_pause = false; 2690 return 0; 2691 } 2692 2693 ret = hns3_query_dev_specifications(hw); 2694 if (ret) { 2695 PMD_INIT_LOG(ERR, 2696 "failed to query dev specifications, ret = %d", 2697 ret); 2698 return ret; 2699 } 2700 2701 hw->intr.mapping_mode = HNS3_INTR_MAPPING_VEC_ALL; 2702 hw->intr.gl_unit = HNS3_INTR_COALESCE_GL_UINT_1US; 2703 hw->tso_mode = HNS3_TSO_HW_CAL_PSEUDO_H_CSUM; 2704 hw->vlan_mode = HNS3_HW_SHIFT_AND_DISCARD_MODE; 2705 hw->drop_stats_mode = HNS3_PKTS_DROP_STATS_MODE2; 2706 pf->tqp_config_mode = HNS3_FLEX_MAX_TQP_NUM_MODE; 2707 hw->rss_info.ipv6_sctp_offload_supported = true; 2708 hw->udp_cksum_mode = HNS3_SPECIAL_PORT_HW_CKSUM_MODE; 2709 pf->support_multi_tc_pause = true; 2710 2711 return 0; 2712 } 2713 2714 static int 2715 hns3_check_media_type(struct hns3_hw *hw, uint8_t media_type) 2716 { 2717 int ret; 2718 2719 switch (media_type) { 2720 case HNS3_MEDIA_TYPE_COPPER: 2721 if (!hns3_dev_get_support(hw, COPPER)) { 2722 PMD_INIT_LOG(ERR, 2723 "Media type is copper, not supported."); 2724 ret = -EOPNOTSUPP; 2725 } else { 2726 ret = 0; 2727 } 2728 break; 2729 case HNS3_MEDIA_TYPE_FIBER: 2730 case HNS3_MEDIA_TYPE_BACKPLANE: 2731 ret = 0; 2732 break; 2733 default: 2734 PMD_INIT_LOG(ERR, "Unknown media type = %u!", media_type); 2735 ret = -EINVAL; 2736 break; 2737 } 2738 2739 return ret; 2740 } 2741 2742 static int 2743 hns3_get_board_configuration(struct hns3_hw *hw) 2744 { 2745 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw); 2746 struct hns3_pf *pf = &hns->pf; 2747 struct hns3_cfg cfg; 2748 int ret; 2749 2750 memset(&cfg, 0, sizeof(cfg)); 2751 ret = hns3_get_board_cfg(hw, &cfg); 2752 if (ret) { 2753 PMD_INIT_LOG(ERR, "get board config failed %d", ret); 2754 return ret; 2755 } 2756 2757 ret = hns3_check_media_type(hw, cfg.media_type); 2758 if (ret) 2759 return ret; 2760 2761 hw->mac.media_type = cfg.media_type; 2762 hw->rss_size_max = cfg.rss_size_max; 2763 memcpy(hw->mac.mac_addr, cfg.mac_addr, RTE_ETHER_ADDR_LEN); 2764 hw->mac.phy_addr = cfg.phy_addr; 2765 hw->dcb_info.num_pg = 1; 2766 hw->dcb_info.hw_pfc_map = 0; 2767 2768 ret = hns3_parse_speed(cfg.default_speed, &hw->mac.link_speed); 2769 if (ret) { 2770 PMD_INIT_LOG(ERR, "Get wrong speed %u, ret = %d", 2771 cfg.default_speed, ret); 2772 return ret; 2773 } 2774 2775 pf->tc_max = cfg.tc_num; 2776 if (pf->tc_max > HNS3_MAX_TC_NUM || pf->tc_max < 1) { 2777 PMD_INIT_LOG(WARNING, 2778 "Get TC num(%u) from flash, set TC num to 1", 2779 pf->tc_max); 2780 pf->tc_max = 1; 2781 } 2782 2783 /* Dev does not support DCB */ 2784 if (!hns3_dev_get_support(hw, DCB)) { 2785 pf->tc_max = 1; 2786 pf->pfc_max = 0; 2787 } else 2788 pf->pfc_max = pf->tc_max; 2789 2790 hw->dcb_info.num_tc = 1; 2791 hw->alloc_rss_size = RTE_MIN(hw->rss_size_max, 2792 hw->tqps_num / hw->dcb_info.num_tc); 2793 hns3_set_bit(hw->hw_tc_map, 0, 1); 2794 pf->tx_sch_mode = HNS3_FLAG_TC_BASE_SCH_MODE; 2795 2796 pf->wanted_umv_size = cfg.umv_space; 2797 2798 return ret; 2799 } 2800 2801 static int 2802 hns3_get_configuration(struct hns3_hw *hw) 2803 { 2804 int ret; 2805 2806 ret = hns3_query_function_status(hw); 2807 if (ret) { 2808 PMD_INIT_LOG(ERR, "Failed to query function status: %d.", ret); 2809 return ret; 2810 } 2811 2812 /* Get device capability */ 2813 ret = hns3_get_capability(hw); 2814 if (ret) { 2815 PMD_INIT_LOG(ERR, "failed to get device capability: %d.", ret); 2816 return ret; 2817 } 2818 2819 /* Get pf resource */ 2820 ret = hns3_query_pf_resource(hw); 2821 if (ret) { 2822 PMD_INIT_LOG(ERR, "Failed to query pf resource: %d", ret); 2823 return ret; 2824 } 2825 2826 ret = hns3_get_board_configuration(hw); 2827 if (ret) { 2828 PMD_INIT_LOG(ERR, "failed to get board configuration: %d", ret); 2829 return ret; 2830 } 2831 2832 ret = hns3_query_dev_fec_info(hw); 2833 if (ret) 2834 PMD_INIT_LOG(ERR, 2835 "failed to query FEC information, ret = %d", ret); 2836 2837 return ret; 2838 } 2839 2840 static int 2841 hns3_map_tqps_to_func(struct hns3_hw *hw, uint16_t func_id, uint16_t tqp_pid, 2842 uint16_t tqp_vid, bool is_pf) 2843 { 2844 struct hns3_tqp_map_cmd *req; 2845 struct hns3_cmd_desc desc; 2846 int ret; 2847 2848 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_SET_TQP_MAP, false); 2849 2850 req = (struct hns3_tqp_map_cmd *)desc.data; 2851 req->tqp_id = rte_cpu_to_le_16(tqp_pid); 2852 req->tqp_vf = func_id; 2853 req->tqp_flag = 1 << HNS3_TQP_MAP_EN_B; 2854 if (!is_pf) 2855 req->tqp_flag |= (1 << HNS3_TQP_MAP_TYPE_B); 2856 req->tqp_vid = rte_cpu_to_le_16(tqp_vid); 2857 2858 ret = hns3_cmd_send(hw, &desc, 1); 2859 if (ret) 2860 PMD_INIT_LOG(ERR, "TQP map failed %d", ret); 2861 2862 return ret; 2863 } 2864 2865 static int 2866 hns3_map_tqp(struct hns3_hw *hw) 2867 { 2868 uint16_t i; 2869 int ret; 2870 2871 /* 2872 * In current version, VF is not supported when PF is driven by DPDK 2873 * driver, so we assign total tqps_num tqps allocated to this port 2874 * to PF. 2875 */ 2876 for (i = 0; i < hw->total_tqps_num; i++) { 2877 ret = hns3_map_tqps_to_func(hw, HNS3_PF_FUNC_ID, i, i, true); 2878 if (ret) 2879 return ret; 2880 } 2881 2882 return 0; 2883 } 2884 2885 static int 2886 hns3_cfg_mac_speed_dup_hw(struct hns3_hw *hw, uint32_t speed, uint8_t duplex) 2887 { 2888 struct hns3_config_mac_speed_dup_cmd *req; 2889 struct hns3_cmd_desc desc; 2890 int ret; 2891 2892 req = (struct hns3_config_mac_speed_dup_cmd *)desc.data; 2893 2894 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_CONFIG_SPEED_DUP, false); 2895 2896 hns3_set_bit(req->speed_dup, HNS3_CFG_DUPLEX_B, !!duplex ? 1 : 0); 2897 2898 switch (speed) { 2899 case RTE_ETH_SPEED_NUM_10M: 2900 hns3_set_field(req->speed_dup, HNS3_CFG_SPEED_M, 2901 HNS3_CFG_SPEED_S, HNS3_CFG_SPEED_10M); 2902 break; 2903 case RTE_ETH_SPEED_NUM_100M: 2904 hns3_set_field(req->speed_dup, HNS3_CFG_SPEED_M, 2905 HNS3_CFG_SPEED_S, HNS3_CFG_SPEED_100M); 2906 break; 2907 case RTE_ETH_SPEED_NUM_1G: 2908 hns3_set_field(req->speed_dup, HNS3_CFG_SPEED_M, 2909 HNS3_CFG_SPEED_S, HNS3_CFG_SPEED_1G); 2910 break; 2911 case RTE_ETH_SPEED_NUM_10G: 2912 hns3_set_field(req->speed_dup, HNS3_CFG_SPEED_M, 2913 HNS3_CFG_SPEED_S, HNS3_CFG_SPEED_10G); 2914 break; 2915 case RTE_ETH_SPEED_NUM_25G: 2916 hns3_set_field(req->speed_dup, HNS3_CFG_SPEED_M, 2917 HNS3_CFG_SPEED_S, HNS3_CFG_SPEED_25G); 2918 break; 2919 case RTE_ETH_SPEED_NUM_40G: 2920 hns3_set_field(req->speed_dup, HNS3_CFG_SPEED_M, 2921 HNS3_CFG_SPEED_S, HNS3_CFG_SPEED_40G); 2922 break; 2923 case RTE_ETH_SPEED_NUM_50G: 2924 hns3_set_field(req->speed_dup, HNS3_CFG_SPEED_M, 2925 HNS3_CFG_SPEED_S, HNS3_CFG_SPEED_50G); 2926 break; 2927 case RTE_ETH_SPEED_NUM_100G: 2928 hns3_set_field(req->speed_dup, HNS3_CFG_SPEED_M, 2929 HNS3_CFG_SPEED_S, HNS3_CFG_SPEED_100G); 2930 break; 2931 case RTE_ETH_SPEED_NUM_200G: 2932 hns3_set_field(req->speed_dup, HNS3_CFG_SPEED_M, 2933 HNS3_CFG_SPEED_S, HNS3_CFG_SPEED_200G); 2934 break; 2935 default: 2936 PMD_INIT_LOG(ERR, "invalid speed (%u)", speed); 2937 return -EINVAL; 2938 } 2939 2940 hns3_set_bit(req->mac_change_fec_en, HNS3_CFG_MAC_SPEED_CHANGE_EN_B, 1); 2941 2942 ret = hns3_cmd_send(hw, &desc, 1); 2943 if (ret) 2944 PMD_INIT_LOG(ERR, "mac speed/duplex config cmd failed %d", ret); 2945 2946 return ret; 2947 } 2948 2949 static int 2950 hns3_tx_buffer_calc(struct hns3_hw *hw, struct hns3_pkt_buf_alloc *buf_alloc) 2951 { 2952 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw); 2953 struct hns3_pf *pf = &hns->pf; 2954 struct hns3_priv_buf *priv; 2955 uint32_t i, total_size; 2956 2957 total_size = pf->pkt_buf_size; 2958 2959 /* alloc tx buffer for all enabled tc */ 2960 for (i = 0; i < HNS3_MAX_TC_NUM; i++) { 2961 priv = &buf_alloc->priv_buf[i]; 2962 2963 if (hw->hw_tc_map & BIT(i)) { 2964 if (total_size < pf->tx_buf_size) 2965 return -ENOMEM; 2966 2967 priv->tx_buf_size = pf->tx_buf_size; 2968 } else 2969 priv->tx_buf_size = 0; 2970 2971 total_size -= priv->tx_buf_size; 2972 } 2973 2974 return 0; 2975 } 2976 2977 static int 2978 hns3_tx_buffer_alloc(struct hns3_hw *hw, struct hns3_pkt_buf_alloc *buf_alloc) 2979 { 2980 /* TX buffer size is unit by 128 byte */ 2981 #define HNS3_BUF_SIZE_UNIT_SHIFT 7 2982 #define HNS3_BUF_SIZE_UPDATE_EN_MSK BIT(15) 2983 struct hns3_tx_buff_alloc_cmd *req; 2984 struct hns3_cmd_desc desc; 2985 uint32_t buf_size; 2986 uint32_t i; 2987 int ret; 2988 2989 req = (struct hns3_tx_buff_alloc_cmd *)desc.data; 2990 2991 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_TX_BUFF_ALLOC, 0); 2992 for (i = 0; i < HNS3_MAX_TC_NUM; i++) { 2993 buf_size = buf_alloc->priv_buf[i].tx_buf_size; 2994 2995 buf_size = buf_size >> HNS3_BUF_SIZE_UNIT_SHIFT; 2996 req->tx_pkt_buff[i] = rte_cpu_to_le_16(buf_size | 2997 HNS3_BUF_SIZE_UPDATE_EN_MSK); 2998 } 2999 3000 ret = hns3_cmd_send(hw, &desc, 1); 3001 if (ret) 3002 PMD_INIT_LOG(ERR, "tx buffer alloc cmd failed %d", ret); 3003 3004 return ret; 3005 } 3006 3007 static int 3008 hns3_get_tc_num(struct hns3_hw *hw) 3009 { 3010 int cnt = 0; 3011 uint8_t i; 3012 3013 for (i = 0; i < HNS3_MAX_TC_NUM; i++) 3014 if (hw->hw_tc_map & BIT(i)) 3015 cnt++; 3016 return cnt; 3017 } 3018 3019 static uint32_t 3020 hns3_get_rx_priv_buff_alloced(struct hns3_pkt_buf_alloc *buf_alloc) 3021 { 3022 struct hns3_priv_buf *priv; 3023 uint32_t rx_priv = 0; 3024 int i; 3025 3026 for (i = 0; i < HNS3_MAX_TC_NUM; i++) { 3027 priv = &buf_alloc->priv_buf[i]; 3028 if (priv->enable) 3029 rx_priv += priv->buf_size; 3030 } 3031 return rx_priv; 3032 } 3033 3034 static uint32_t 3035 hns3_get_tx_buff_alloced(struct hns3_pkt_buf_alloc *buf_alloc) 3036 { 3037 uint32_t total_tx_size = 0; 3038 uint32_t i; 3039 3040 for (i = 0; i < HNS3_MAX_TC_NUM; i++) 3041 total_tx_size += buf_alloc->priv_buf[i].tx_buf_size; 3042 3043 return total_tx_size; 3044 } 3045 3046 /* Get the number of pfc enabled TCs, which have private buffer */ 3047 static int 3048 hns3_get_pfc_priv_num(struct hns3_hw *hw, struct hns3_pkt_buf_alloc *buf_alloc) 3049 { 3050 struct hns3_priv_buf *priv; 3051 int cnt = 0; 3052 uint8_t i; 3053 3054 for (i = 0; i < HNS3_MAX_TC_NUM; i++) { 3055 priv = &buf_alloc->priv_buf[i]; 3056 if ((hw->dcb_info.hw_pfc_map & BIT(i)) && priv->enable) 3057 cnt++; 3058 } 3059 3060 return cnt; 3061 } 3062 3063 /* Get the number of pfc disabled TCs, which have private buffer */ 3064 static int 3065 hns3_get_no_pfc_priv_num(struct hns3_hw *hw, 3066 struct hns3_pkt_buf_alloc *buf_alloc) 3067 { 3068 struct hns3_priv_buf *priv; 3069 int cnt = 0; 3070 uint8_t i; 3071 3072 for (i = 0; i < HNS3_MAX_TC_NUM; i++) { 3073 priv = &buf_alloc->priv_buf[i]; 3074 if (hw->hw_tc_map & BIT(i) && 3075 !(hw->dcb_info.hw_pfc_map & BIT(i)) && priv->enable) 3076 cnt++; 3077 } 3078 3079 return cnt; 3080 } 3081 3082 static bool 3083 hns3_is_rx_buf_ok(struct hns3_hw *hw, struct hns3_pkt_buf_alloc *buf_alloc, 3084 uint32_t rx_all) 3085 { 3086 uint32_t shared_buf_min, shared_buf_tc, shared_std, hi_thrd, lo_thrd; 3087 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw); 3088 struct hns3_pf *pf = &hns->pf; 3089 uint32_t shared_buf, aligned_mps; 3090 uint32_t rx_priv; 3091 uint8_t tc_num; 3092 uint8_t i; 3093 3094 tc_num = hns3_get_tc_num(hw); 3095 aligned_mps = roundup(pf->mps, HNS3_BUF_SIZE_UNIT); 3096 3097 if (hns3_dev_get_support(hw, DCB)) 3098 shared_buf_min = HNS3_BUF_MUL_BY * aligned_mps + 3099 pf->dv_buf_size; 3100 else 3101 shared_buf_min = aligned_mps + HNS3_NON_DCB_ADDITIONAL_BUF 3102 + pf->dv_buf_size; 3103 3104 shared_buf_tc = tc_num * aligned_mps + aligned_mps; 3105 shared_std = roundup(RTE_MAX(shared_buf_min, shared_buf_tc), 3106 HNS3_BUF_SIZE_UNIT); 3107 3108 rx_priv = hns3_get_rx_priv_buff_alloced(buf_alloc); 3109 if (rx_all < rx_priv + shared_std) 3110 return false; 3111 3112 shared_buf = rounddown(rx_all - rx_priv, HNS3_BUF_SIZE_UNIT); 3113 buf_alloc->s_buf.buf_size = shared_buf; 3114 if (hns3_dev_get_support(hw, DCB)) { 3115 buf_alloc->s_buf.self.high = shared_buf - pf->dv_buf_size; 3116 buf_alloc->s_buf.self.low = buf_alloc->s_buf.self.high 3117 - roundup(aligned_mps / HNS3_BUF_DIV_BY, 3118 HNS3_BUF_SIZE_UNIT); 3119 } else { 3120 buf_alloc->s_buf.self.high = 3121 aligned_mps + HNS3_NON_DCB_ADDITIONAL_BUF; 3122 buf_alloc->s_buf.self.low = aligned_mps; 3123 } 3124 3125 if (hns3_dev_get_support(hw, DCB)) { 3126 hi_thrd = shared_buf - pf->dv_buf_size; 3127 3128 if (tc_num <= NEED_RESERVE_TC_NUM) 3129 hi_thrd = hi_thrd * BUF_RESERVE_PERCENT / 3130 BUF_MAX_PERCENT; 3131 3132 if (tc_num) 3133 hi_thrd = hi_thrd / tc_num; 3134 3135 hi_thrd = RTE_MAX(hi_thrd, HNS3_BUF_MUL_BY * aligned_mps); 3136 hi_thrd = rounddown(hi_thrd, HNS3_BUF_SIZE_UNIT); 3137 lo_thrd = hi_thrd - aligned_mps / HNS3_BUF_DIV_BY; 3138 } else { 3139 hi_thrd = aligned_mps + HNS3_NON_DCB_ADDITIONAL_BUF; 3140 lo_thrd = aligned_mps; 3141 } 3142 3143 for (i = 0; i < HNS3_MAX_TC_NUM; i++) { 3144 buf_alloc->s_buf.tc_thrd[i].low = lo_thrd; 3145 buf_alloc->s_buf.tc_thrd[i].high = hi_thrd; 3146 } 3147 3148 return true; 3149 } 3150 3151 static bool 3152 hns3_rx_buf_calc_all(struct hns3_hw *hw, bool max, 3153 struct hns3_pkt_buf_alloc *buf_alloc) 3154 { 3155 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw); 3156 struct hns3_pf *pf = &hns->pf; 3157 struct hns3_priv_buf *priv; 3158 uint32_t aligned_mps; 3159 uint32_t rx_all; 3160 uint8_t i; 3161 3162 rx_all = pf->pkt_buf_size - hns3_get_tx_buff_alloced(buf_alloc); 3163 aligned_mps = roundup(pf->mps, HNS3_BUF_SIZE_UNIT); 3164 3165 for (i = 0; i < HNS3_MAX_TC_NUM; i++) { 3166 priv = &buf_alloc->priv_buf[i]; 3167 3168 priv->enable = 0; 3169 priv->wl.low = 0; 3170 priv->wl.high = 0; 3171 priv->buf_size = 0; 3172 3173 if (!(hw->hw_tc_map & BIT(i))) 3174 continue; 3175 3176 priv->enable = 1; 3177 if (hw->dcb_info.hw_pfc_map & BIT(i)) { 3178 priv->wl.low = max ? aligned_mps : HNS3_BUF_SIZE_UNIT; 3179 priv->wl.high = roundup(priv->wl.low + aligned_mps, 3180 HNS3_BUF_SIZE_UNIT); 3181 } else { 3182 priv->wl.low = 0; 3183 priv->wl.high = max ? (aligned_mps * HNS3_BUF_MUL_BY) : 3184 aligned_mps; 3185 } 3186 3187 priv->buf_size = priv->wl.high + pf->dv_buf_size; 3188 } 3189 3190 return hns3_is_rx_buf_ok(hw, buf_alloc, rx_all); 3191 } 3192 3193 static bool 3194 hns3_drop_nopfc_buf_till_fit(struct hns3_hw *hw, 3195 struct hns3_pkt_buf_alloc *buf_alloc) 3196 { 3197 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw); 3198 struct hns3_pf *pf = &hns->pf; 3199 struct hns3_priv_buf *priv; 3200 int no_pfc_priv_num; 3201 uint32_t rx_all; 3202 uint8_t mask; 3203 int i; 3204 3205 rx_all = pf->pkt_buf_size - hns3_get_tx_buff_alloced(buf_alloc); 3206 no_pfc_priv_num = hns3_get_no_pfc_priv_num(hw, buf_alloc); 3207 3208 /* let the last to be cleared first */ 3209 for (i = HNS3_MAX_TC_NUM - 1; i >= 0; i--) { 3210 priv = &buf_alloc->priv_buf[i]; 3211 mask = BIT((uint8_t)i); 3212 if (hw->hw_tc_map & mask && 3213 !(hw->dcb_info.hw_pfc_map & mask)) { 3214 /* Clear the no pfc TC private buffer */ 3215 priv->wl.low = 0; 3216 priv->wl.high = 0; 3217 priv->buf_size = 0; 3218 priv->enable = 0; 3219 no_pfc_priv_num--; 3220 } 3221 3222 if (hns3_is_rx_buf_ok(hw, buf_alloc, rx_all) || 3223 no_pfc_priv_num == 0) 3224 break; 3225 } 3226 3227 return hns3_is_rx_buf_ok(hw, buf_alloc, rx_all); 3228 } 3229 3230 static bool 3231 hns3_drop_pfc_buf_till_fit(struct hns3_hw *hw, 3232 struct hns3_pkt_buf_alloc *buf_alloc) 3233 { 3234 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw); 3235 struct hns3_pf *pf = &hns->pf; 3236 struct hns3_priv_buf *priv; 3237 uint32_t rx_all; 3238 int pfc_priv_num; 3239 uint8_t mask; 3240 int i; 3241 3242 rx_all = pf->pkt_buf_size - hns3_get_tx_buff_alloced(buf_alloc); 3243 pfc_priv_num = hns3_get_pfc_priv_num(hw, buf_alloc); 3244 3245 /* let the last to be cleared first */ 3246 for (i = HNS3_MAX_TC_NUM - 1; i >= 0; i--) { 3247 priv = &buf_alloc->priv_buf[i]; 3248 mask = BIT((uint8_t)i); 3249 if (hw->hw_tc_map & mask && hw->dcb_info.hw_pfc_map & mask) { 3250 /* Reduce the number of pfc TC with private buffer */ 3251 priv->wl.low = 0; 3252 priv->enable = 0; 3253 priv->wl.high = 0; 3254 priv->buf_size = 0; 3255 pfc_priv_num--; 3256 } 3257 if (hns3_is_rx_buf_ok(hw, buf_alloc, rx_all) || 3258 pfc_priv_num == 0) 3259 break; 3260 } 3261 3262 return hns3_is_rx_buf_ok(hw, buf_alloc, rx_all); 3263 } 3264 3265 static bool 3266 hns3_only_alloc_priv_buff(struct hns3_hw *hw, 3267 struct hns3_pkt_buf_alloc *buf_alloc) 3268 { 3269 #define COMPENSATE_BUFFER 0x3C00 3270 #define COMPENSATE_HALF_MPS_NUM 5 3271 #define PRIV_WL_GAP 0x1800 3272 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw); 3273 struct hns3_pf *pf = &hns->pf; 3274 uint32_t tc_num = hns3_get_tc_num(hw); 3275 uint32_t half_mps = pf->mps >> 1; 3276 struct hns3_priv_buf *priv; 3277 uint32_t min_rx_priv; 3278 uint32_t rx_priv; 3279 uint8_t i; 3280 3281 rx_priv = pf->pkt_buf_size - hns3_get_tx_buff_alloced(buf_alloc); 3282 if (tc_num) 3283 rx_priv = rx_priv / tc_num; 3284 3285 if (tc_num <= NEED_RESERVE_TC_NUM) 3286 rx_priv = rx_priv * BUF_RESERVE_PERCENT / BUF_MAX_PERCENT; 3287 3288 /* 3289 * Minimum value of private buffer in rx direction (min_rx_priv) is 3290 * equal to "DV + 2.5 * MPS + 15KB". Driver only allocates rx private 3291 * buffer if rx_priv is greater than min_rx_priv. 3292 */ 3293 min_rx_priv = pf->dv_buf_size + COMPENSATE_BUFFER + 3294 COMPENSATE_HALF_MPS_NUM * half_mps; 3295 min_rx_priv = roundup(min_rx_priv, HNS3_BUF_SIZE_UNIT); 3296 rx_priv = rounddown(rx_priv, HNS3_BUF_SIZE_UNIT); 3297 if (rx_priv < min_rx_priv) 3298 return false; 3299 3300 for (i = 0; i < HNS3_MAX_TC_NUM; i++) { 3301 priv = &buf_alloc->priv_buf[i]; 3302 priv->enable = 0; 3303 priv->wl.low = 0; 3304 priv->wl.high = 0; 3305 priv->buf_size = 0; 3306 3307 if (!(hw->hw_tc_map & BIT(i))) 3308 continue; 3309 3310 priv->enable = 1; 3311 priv->buf_size = rx_priv; 3312 priv->wl.high = rx_priv - pf->dv_buf_size; 3313 priv->wl.low = priv->wl.high - PRIV_WL_GAP; 3314 } 3315 3316 buf_alloc->s_buf.buf_size = 0; 3317 3318 return true; 3319 } 3320 3321 /* 3322 * hns3_rx_buffer_calc: calculate the rx private buffer size for all TCs 3323 * @hw: pointer to struct hns3_hw 3324 * @buf_alloc: pointer to buffer calculation data 3325 * @return: 0: calculate successful, negative: fail 3326 */ 3327 static int 3328 hns3_rx_buffer_calc(struct hns3_hw *hw, struct hns3_pkt_buf_alloc *buf_alloc) 3329 { 3330 /* When DCB is not supported, rx private buffer is not allocated. */ 3331 if (!hns3_dev_get_support(hw, DCB)) { 3332 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw); 3333 struct hns3_pf *pf = &hns->pf; 3334 uint32_t rx_all = pf->pkt_buf_size; 3335 3336 rx_all -= hns3_get_tx_buff_alloced(buf_alloc); 3337 if (!hns3_is_rx_buf_ok(hw, buf_alloc, rx_all)) 3338 return -ENOMEM; 3339 3340 return 0; 3341 } 3342 3343 /* 3344 * Try to allocate privated packet buffer for all TCs without share 3345 * buffer. 3346 */ 3347 if (hns3_only_alloc_priv_buff(hw, buf_alloc)) 3348 return 0; 3349 3350 /* 3351 * Try to allocate privated packet buffer for all TCs with share 3352 * buffer. 3353 */ 3354 if (hns3_rx_buf_calc_all(hw, true, buf_alloc)) 3355 return 0; 3356 3357 /* 3358 * For different application scenes, the enabled port number, TC number 3359 * and no_drop TC number are different. In order to obtain the better 3360 * performance, software could allocate the buffer size and configure 3361 * the waterline by trying to decrease the private buffer size according 3362 * to the order, namely, waterline of valid tc, pfc disabled tc, pfc 3363 * enabled tc. 3364 */ 3365 if (hns3_rx_buf_calc_all(hw, false, buf_alloc)) 3366 return 0; 3367 3368 if (hns3_drop_nopfc_buf_till_fit(hw, buf_alloc)) 3369 return 0; 3370 3371 if (hns3_drop_pfc_buf_till_fit(hw, buf_alloc)) 3372 return 0; 3373 3374 return -ENOMEM; 3375 } 3376 3377 static int 3378 hns3_rx_priv_buf_alloc(struct hns3_hw *hw, struct hns3_pkt_buf_alloc *buf_alloc) 3379 { 3380 struct hns3_rx_priv_buff_cmd *req; 3381 struct hns3_cmd_desc desc; 3382 uint32_t buf_size; 3383 int ret; 3384 int i; 3385 3386 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_RX_PRIV_BUFF_ALLOC, false); 3387 req = (struct hns3_rx_priv_buff_cmd *)desc.data; 3388 3389 /* Alloc private buffer TCs */ 3390 for (i = 0; i < HNS3_MAX_TC_NUM; i++) { 3391 struct hns3_priv_buf *priv = &buf_alloc->priv_buf[i]; 3392 3393 req->buf_num[i] = 3394 rte_cpu_to_le_16(priv->buf_size >> HNS3_BUF_UNIT_S); 3395 req->buf_num[i] |= rte_cpu_to_le_16(1 << HNS3_TC0_PRI_BUF_EN_B); 3396 } 3397 3398 buf_size = buf_alloc->s_buf.buf_size; 3399 req->shared_buf = rte_cpu_to_le_16((buf_size >> HNS3_BUF_UNIT_S) | 3400 (1 << HNS3_TC0_PRI_BUF_EN_B)); 3401 3402 ret = hns3_cmd_send(hw, &desc, 1); 3403 if (ret) 3404 PMD_INIT_LOG(ERR, "rx private buffer alloc cmd failed %d", ret); 3405 3406 return ret; 3407 } 3408 3409 static int 3410 hns3_rx_priv_wl_config(struct hns3_hw *hw, struct hns3_pkt_buf_alloc *buf_alloc) 3411 { 3412 #define HNS3_RX_PRIV_WL_ALLOC_DESC_NUM 2 3413 struct hns3_rx_priv_wl_buf *req; 3414 struct hns3_priv_buf *priv; 3415 struct hns3_cmd_desc desc[HNS3_RX_PRIV_WL_ALLOC_DESC_NUM]; 3416 int i, j; 3417 int ret; 3418 3419 for (i = 0; i < HNS3_RX_PRIV_WL_ALLOC_DESC_NUM; i++) { 3420 hns3_cmd_setup_basic_desc(&desc[i], HNS3_OPC_RX_PRIV_WL_ALLOC, 3421 false); 3422 req = (struct hns3_rx_priv_wl_buf *)desc[i].data; 3423 3424 /* The first descriptor set the NEXT bit to 1 */ 3425 if (i == 0) 3426 desc[i].flag |= rte_cpu_to_le_16(HNS3_CMD_FLAG_NEXT); 3427 else 3428 desc[i].flag &= ~rte_cpu_to_le_16(HNS3_CMD_FLAG_NEXT); 3429 3430 for (j = 0; j < HNS3_TC_NUM_ONE_DESC; j++) { 3431 uint32_t idx = i * HNS3_TC_NUM_ONE_DESC + j; 3432 3433 priv = &buf_alloc->priv_buf[idx]; 3434 req->tc_wl[j].high = rte_cpu_to_le_16(priv->wl.high >> 3435 HNS3_BUF_UNIT_S); 3436 req->tc_wl[j].high |= 3437 rte_cpu_to_le_16(BIT(HNS3_RX_PRIV_EN_B)); 3438 req->tc_wl[j].low = rte_cpu_to_le_16(priv->wl.low >> 3439 HNS3_BUF_UNIT_S); 3440 req->tc_wl[j].low |= 3441 rte_cpu_to_le_16(BIT(HNS3_RX_PRIV_EN_B)); 3442 } 3443 } 3444 3445 /* Send 2 descriptor at one time */ 3446 ret = hns3_cmd_send(hw, desc, HNS3_RX_PRIV_WL_ALLOC_DESC_NUM); 3447 if (ret) 3448 PMD_INIT_LOG(ERR, "rx private waterline config cmd failed %d", 3449 ret); 3450 return ret; 3451 } 3452 3453 static int 3454 hns3_common_thrd_config(struct hns3_hw *hw, 3455 struct hns3_pkt_buf_alloc *buf_alloc) 3456 { 3457 #define HNS3_RX_COM_THRD_ALLOC_DESC_NUM 2 3458 struct hns3_shared_buf *s_buf = &buf_alloc->s_buf; 3459 struct hns3_rx_com_thrd *req; 3460 struct hns3_cmd_desc desc[HNS3_RX_COM_THRD_ALLOC_DESC_NUM]; 3461 struct hns3_tc_thrd *tc; 3462 int tc_idx; 3463 int i, j; 3464 int ret; 3465 3466 for (i = 0; i < HNS3_RX_COM_THRD_ALLOC_DESC_NUM; i++) { 3467 hns3_cmd_setup_basic_desc(&desc[i], HNS3_OPC_RX_COM_THRD_ALLOC, 3468 false); 3469 req = (struct hns3_rx_com_thrd *)&desc[i].data; 3470 3471 /* The first descriptor set the NEXT bit to 1 */ 3472 if (i == 0) 3473 desc[i].flag |= rte_cpu_to_le_16(HNS3_CMD_FLAG_NEXT); 3474 else 3475 desc[i].flag &= ~rte_cpu_to_le_16(HNS3_CMD_FLAG_NEXT); 3476 3477 for (j = 0; j < HNS3_TC_NUM_ONE_DESC; j++) { 3478 tc_idx = i * HNS3_TC_NUM_ONE_DESC + j; 3479 tc = &s_buf->tc_thrd[tc_idx]; 3480 3481 req->com_thrd[j].high = 3482 rte_cpu_to_le_16(tc->high >> HNS3_BUF_UNIT_S); 3483 req->com_thrd[j].high |= 3484 rte_cpu_to_le_16(BIT(HNS3_RX_PRIV_EN_B)); 3485 req->com_thrd[j].low = 3486 rte_cpu_to_le_16(tc->low >> HNS3_BUF_UNIT_S); 3487 req->com_thrd[j].low |= 3488 rte_cpu_to_le_16(BIT(HNS3_RX_PRIV_EN_B)); 3489 } 3490 } 3491 3492 /* Send 2 descriptors at one time */ 3493 ret = hns3_cmd_send(hw, desc, HNS3_RX_COM_THRD_ALLOC_DESC_NUM); 3494 if (ret) 3495 PMD_INIT_LOG(ERR, "common threshold config cmd failed %d", ret); 3496 3497 return ret; 3498 } 3499 3500 static int 3501 hns3_common_wl_config(struct hns3_hw *hw, struct hns3_pkt_buf_alloc *buf_alloc) 3502 { 3503 struct hns3_shared_buf *buf = &buf_alloc->s_buf; 3504 struct hns3_rx_com_wl *req; 3505 struct hns3_cmd_desc desc; 3506 int ret; 3507 3508 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_RX_COM_WL_ALLOC, false); 3509 3510 req = (struct hns3_rx_com_wl *)desc.data; 3511 req->com_wl.high = rte_cpu_to_le_16(buf->self.high >> HNS3_BUF_UNIT_S); 3512 req->com_wl.high |= rte_cpu_to_le_16(BIT(HNS3_RX_PRIV_EN_B)); 3513 3514 req->com_wl.low = rte_cpu_to_le_16(buf->self.low >> HNS3_BUF_UNIT_S); 3515 req->com_wl.low |= rte_cpu_to_le_16(BIT(HNS3_RX_PRIV_EN_B)); 3516 3517 ret = hns3_cmd_send(hw, &desc, 1); 3518 if (ret) 3519 PMD_INIT_LOG(ERR, "common waterline config cmd failed %d", ret); 3520 3521 return ret; 3522 } 3523 3524 int 3525 hns3_buffer_alloc(struct hns3_hw *hw) 3526 { 3527 struct hns3_pkt_buf_alloc pkt_buf; 3528 int ret; 3529 3530 memset(&pkt_buf, 0, sizeof(pkt_buf)); 3531 ret = hns3_tx_buffer_calc(hw, &pkt_buf); 3532 if (ret) { 3533 PMD_INIT_LOG(ERR, 3534 "could not calc tx buffer size for all TCs %d", 3535 ret); 3536 return ret; 3537 } 3538 3539 ret = hns3_tx_buffer_alloc(hw, &pkt_buf); 3540 if (ret) { 3541 PMD_INIT_LOG(ERR, "could not alloc tx buffers %d", ret); 3542 return ret; 3543 } 3544 3545 ret = hns3_rx_buffer_calc(hw, &pkt_buf); 3546 if (ret) { 3547 PMD_INIT_LOG(ERR, 3548 "could not calc rx priv buffer size for all TCs %d", 3549 ret); 3550 return ret; 3551 } 3552 3553 ret = hns3_rx_priv_buf_alloc(hw, &pkt_buf); 3554 if (ret) { 3555 PMD_INIT_LOG(ERR, "could not alloc rx priv buffer %d", ret); 3556 return ret; 3557 } 3558 3559 if (hns3_dev_get_support(hw, DCB)) { 3560 ret = hns3_rx_priv_wl_config(hw, &pkt_buf); 3561 if (ret) { 3562 PMD_INIT_LOG(ERR, 3563 "could not configure rx private waterline %d", 3564 ret); 3565 return ret; 3566 } 3567 3568 ret = hns3_common_thrd_config(hw, &pkt_buf); 3569 if (ret) { 3570 PMD_INIT_LOG(ERR, 3571 "could not configure common threshold %d", 3572 ret); 3573 return ret; 3574 } 3575 } 3576 3577 ret = hns3_common_wl_config(hw, &pkt_buf); 3578 if (ret) 3579 PMD_INIT_LOG(ERR, "could not configure common waterline %d", 3580 ret); 3581 3582 return ret; 3583 } 3584 3585 static int 3586 hns3_mac_init(struct hns3_hw *hw) 3587 { 3588 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw); 3589 struct hns3_mac *mac = &hw->mac; 3590 struct hns3_pf *pf = &hns->pf; 3591 int ret; 3592 3593 pf->support_sfp_query = true; 3594 mac->link_duplex = RTE_ETH_LINK_FULL_DUPLEX; 3595 ret = hns3_cfg_mac_speed_dup_hw(hw, mac->link_speed, mac->link_duplex); 3596 if (ret) { 3597 PMD_INIT_LOG(ERR, "Config mac speed dup fail ret = %d", ret); 3598 return ret; 3599 } 3600 3601 mac->link_status = RTE_ETH_LINK_DOWN; 3602 3603 return hns3_config_mtu(hw, pf->mps); 3604 } 3605 3606 static int 3607 hns3_get_mac_ethertype_cmd_status(uint16_t cmdq_resp, uint8_t resp_code) 3608 { 3609 #define HNS3_ETHERTYPE_SUCCESS_ADD 0 3610 #define HNS3_ETHERTYPE_ALREADY_ADD 1 3611 #define HNS3_ETHERTYPE_MGR_TBL_OVERFLOW 2 3612 #define HNS3_ETHERTYPE_KEY_CONFLICT 3 3613 int return_status; 3614 3615 if (cmdq_resp) { 3616 PMD_INIT_LOG(ERR, 3617 "cmdq execute failed for get_mac_ethertype_cmd_status, status=%u.\n", 3618 cmdq_resp); 3619 return -EIO; 3620 } 3621 3622 switch (resp_code) { 3623 case HNS3_ETHERTYPE_SUCCESS_ADD: 3624 case HNS3_ETHERTYPE_ALREADY_ADD: 3625 return_status = 0; 3626 break; 3627 case HNS3_ETHERTYPE_MGR_TBL_OVERFLOW: 3628 PMD_INIT_LOG(ERR, 3629 "add mac ethertype failed for manager table overflow."); 3630 return_status = -EIO; 3631 break; 3632 case HNS3_ETHERTYPE_KEY_CONFLICT: 3633 PMD_INIT_LOG(ERR, "add mac ethertype failed for key conflict."); 3634 return_status = -EIO; 3635 break; 3636 default: 3637 PMD_INIT_LOG(ERR, 3638 "add mac ethertype failed for undefined, code=%u.", 3639 resp_code); 3640 return_status = -EIO; 3641 break; 3642 } 3643 3644 return return_status; 3645 } 3646 3647 static int 3648 hns3_add_mgr_tbl(struct hns3_hw *hw, 3649 const struct hns3_mac_mgr_tbl_entry_cmd *req) 3650 { 3651 struct hns3_cmd_desc desc; 3652 uint8_t resp_code; 3653 uint16_t retval; 3654 int ret; 3655 3656 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_MAC_ETHTYPE_ADD, false); 3657 memcpy(desc.data, req, sizeof(struct hns3_mac_mgr_tbl_entry_cmd)); 3658 3659 ret = hns3_cmd_send(hw, &desc, 1); 3660 if (ret) { 3661 PMD_INIT_LOG(ERR, 3662 "add mac ethertype failed for cmd_send, ret =%d.", 3663 ret); 3664 return ret; 3665 } 3666 3667 resp_code = (rte_le_to_cpu_32(desc.data[0]) >> 8) & 0xff; 3668 retval = rte_le_to_cpu_16(desc.retval); 3669 3670 return hns3_get_mac_ethertype_cmd_status(retval, resp_code); 3671 } 3672 3673 static void 3674 hns3_prepare_mgr_tbl(struct hns3_mac_mgr_tbl_entry_cmd *mgr_table, 3675 int *table_item_num) 3676 { 3677 struct hns3_mac_mgr_tbl_entry_cmd *tbl; 3678 3679 /* 3680 * In current version, we add one item in management table as below: 3681 * 0x0180C200000E -- LLDP MC address 3682 */ 3683 tbl = mgr_table; 3684 tbl->flags = HNS3_MAC_MGR_MASK_VLAN_B; 3685 tbl->ethter_type = rte_cpu_to_le_16(HNS3_MAC_ETHERTYPE_LLDP); 3686 tbl->mac_addr_hi32 = rte_cpu_to_le_32(htonl(0x0180C200)); 3687 tbl->mac_addr_lo16 = rte_cpu_to_le_16(htons(0x000E)); 3688 tbl->i_port_bitmap = 0x1; 3689 *table_item_num = 1; 3690 } 3691 3692 static int 3693 hns3_init_mgr_tbl(struct hns3_hw *hw) 3694 { 3695 #define HNS_MAC_MGR_TBL_MAX_SIZE 16 3696 struct hns3_mac_mgr_tbl_entry_cmd mgr_table[HNS_MAC_MGR_TBL_MAX_SIZE]; 3697 int table_item_num; 3698 int ret; 3699 int i; 3700 3701 memset(mgr_table, 0, sizeof(mgr_table)); 3702 hns3_prepare_mgr_tbl(mgr_table, &table_item_num); 3703 for (i = 0; i < table_item_num; i++) { 3704 ret = hns3_add_mgr_tbl(hw, &mgr_table[i]); 3705 if (ret) { 3706 PMD_INIT_LOG(ERR, "add mac ethertype failed, ret =%d", 3707 ret); 3708 return ret; 3709 } 3710 } 3711 3712 return 0; 3713 } 3714 3715 static void 3716 hns3_promisc_param_init(struct hns3_promisc_param *param, bool en_uc, 3717 bool en_mc, bool en_bc, int vport_id) 3718 { 3719 if (!param) 3720 return; 3721 3722 memset(param, 0, sizeof(struct hns3_promisc_param)); 3723 if (en_uc) 3724 param->enable = HNS3_PROMISC_EN_UC; 3725 if (en_mc) 3726 param->enable |= HNS3_PROMISC_EN_MC; 3727 if (en_bc) 3728 param->enable |= HNS3_PROMISC_EN_BC; 3729 param->vf_id = vport_id; 3730 } 3731 3732 static int 3733 hns3_cmd_set_promisc_mode(struct hns3_hw *hw, struct hns3_promisc_param *param) 3734 { 3735 struct hns3_promisc_cfg_cmd *req; 3736 struct hns3_cmd_desc desc; 3737 int ret; 3738 3739 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_CFG_PROMISC_MODE, false); 3740 3741 req = (struct hns3_promisc_cfg_cmd *)desc.data; 3742 req->vf_id = param->vf_id; 3743 req->flag = (param->enable << HNS3_PROMISC_EN_B) | 3744 HNS3_PROMISC_TX_EN_B | HNS3_PROMISC_RX_EN_B; 3745 3746 ret = hns3_cmd_send(hw, &desc, 1); 3747 if (ret) 3748 PMD_INIT_LOG(ERR, "Set promisc mode fail, ret = %d", ret); 3749 3750 return ret; 3751 } 3752 3753 static int 3754 hns3_set_promisc_mode(struct hns3_hw *hw, bool en_uc_pmc, bool en_mc_pmc) 3755 { 3756 struct hns3_promisc_param param; 3757 bool en_bc_pmc = true; 3758 uint8_t vf_id; 3759 3760 /* 3761 * In current version VF is not supported when PF is driven by DPDK 3762 * driver, just need to configure parameters for PF vport. 3763 */ 3764 vf_id = HNS3_PF_FUNC_ID; 3765 3766 hns3_promisc_param_init(¶m, en_uc_pmc, en_mc_pmc, en_bc_pmc, vf_id); 3767 return hns3_cmd_set_promisc_mode(hw, ¶m); 3768 } 3769 3770 static int 3771 hns3_promisc_init(struct hns3_hw *hw) 3772 { 3773 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw); 3774 struct hns3_pf *pf = &hns->pf; 3775 struct hns3_promisc_param param; 3776 uint16_t func_id; 3777 int ret; 3778 3779 ret = hns3_set_promisc_mode(hw, false, false); 3780 if (ret) { 3781 PMD_INIT_LOG(ERR, "failed to set promisc mode, ret = %d", ret); 3782 return ret; 3783 } 3784 3785 /* 3786 * In current version VFs are not supported when PF is driven by DPDK 3787 * driver. After PF has been taken over by DPDK, the original VF will 3788 * be invalid. So, there is a possibility of entry residues. It should 3789 * clear VFs's promisc mode to avoid unnecessary bandwidth usage 3790 * during init. 3791 */ 3792 for (func_id = HNS3_1ST_VF_FUNC_ID; func_id < pf->func_num; func_id++) { 3793 hns3_promisc_param_init(¶m, false, false, false, func_id); 3794 ret = hns3_cmd_set_promisc_mode(hw, ¶m); 3795 if (ret) { 3796 PMD_INIT_LOG(ERR, "failed to clear vf:%u promisc mode," 3797 " ret = %d", func_id, ret); 3798 return ret; 3799 } 3800 } 3801 3802 return 0; 3803 } 3804 3805 static void 3806 hns3_promisc_uninit(struct hns3_hw *hw) 3807 { 3808 struct hns3_promisc_param param; 3809 uint16_t func_id; 3810 int ret; 3811 3812 func_id = HNS3_PF_FUNC_ID; 3813 3814 /* 3815 * In current version VFs are not supported when PF is driven by 3816 * DPDK driver, and VFs' promisc mode status has been cleared during 3817 * init and their status will not change. So just clear PF's promisc 3818 * mode status during uninit. 3819 */ 3820 hns3_promisc_param_init(¶m, false, false, false, func_id); 3821 ret = hns3_cmd_set_promisc_mode(hw, ¶m); 3822 if (ret) 3823 PMD_INIT_LOG(ERR, "failed to clear promisc status during" 3824 " uninit, ret = %d", ret); 3825 } 3826 3827 static int 3828 hns3_dev_promiscuous_enable(struct rte_eth_dev *dev) 3829 { 3830 bool allmulti = dev->data->all_multicast ? true : false; 3831 struct hns3_adapter *hns = dev->data->dev_private; 3832 struct hns3_hw *hw = &hns->hw; 3833 uint64_t offloads; 3834 int err; 3835 int ret; 3836 3837 rte_spinlock_lock(&hw->lock); 3838 ret = hns3_set_promisc_mode(hw, true, true); 3839 if (ret) { 3840 rte_spinlock_unlock(&hw->lock); 3841 hns3_err(hw, "failed to enable promiscuous mode, ret = %d", 3842 ret); 3843 return ret; 3844 } 3845 3846 /* 3847 * When promiscuous mode was enabled, disable the vlan filter to let 3848 * all packets coming in in the receiving direction. 3849 */ 3850 offloads = dev->data->dev_conf.rxmode.offloads; 3851 if (offloads & RTE_ETH_RX_OFFLOAD_VLAN_FILTER) { 3852 ret = hns3_enable_vlan_filter(hns, false); 3853 if (ret) { 3854 hns3_err(hw, "failed to enable promiscuous mode due to " 3855 "failure to disable vlan filter, ret = %d", 3856 ret); 3857 err = hns3_set_promisc_mode(hw, false, allmulti); 3858 if (err) 3859 hns3_err(hw, "failed to restore promiscuous " 3860 "status after disable vlan filter " 3861 "failed during enabling promiscuous " 3862 "mode, ret = %d", ret); 3863 } 3864 } 3865 3866 rte_spinlock_unlock(&hw->lock); 3867 3868 return ret; 3869 } 3870 3871 static int 3872 hns3_dev_promiscuous_disable(struct rte_eth_dev *dev) 3873 { 3874 bool allmulti = dev->data->all_multicast ? true : false; 3875 struct hns3_adapter *hns = dev->data->dev_private; 3876 struct hns3_hw *hw = &hns->hw; 3877 uint64_t offloads; 3878 int err; 3879 int ret; 3880 3881 /* If now in all_multicast mode, must remain in all_multicast mode. */ 3882 rte_spinlock_lock(&hw->lock); 3883 ret = hns3_set_promisc_mode(hw, false, allmulti); 3884 if (ret) { 3885 rte_spinlock_unlock(&hw->lock); 3886 hns3_err(hw, "failed to disable promiscuous mode, ret = %d", 3887 ret); 3888 return ret; 3889 } 3890 /* when promiscuous mode was disabled, restore the vlan filter status */ 3891 offloads = dev->data->dev_conf.rxmode.offloads; 3892 if (offloads & RTE_ETH_RX_OFFLOAD_VLAN_FILTER) { 3893 ret = hns3_enable_vlan_filter(hns, true); 3894 if (ret) { 3895 hns3_err(hw, "failed to disable promiscuous mode due to" 3896 " failure to restore vlan filter, ret = %d", 3897 ret); 3898 err = hns3_set_promisc_mode(hw, true, true); 3899 if (err) 3900 hns3_err(hw, "failed to restore promiscuous " 3901 "status after enabling vlan filter " 3902 "failed during disabling promiscuous " 3903 "mode, ret = %d", ret); 3904 } 3905 } 3906 rte_spinlock_unlock(&hw->lock); 3907 3908 return ret; 3909 } 3910 3911 static int 3912 hns3_dev_allmulticast_enable(struct rte_eth_dev *dev) 3913 { 3914 struct hns3_adapter *hns = dev->data->dev_private; 3915 struct hns3_hw *hw = &hns->hw; 3916 int ret; 3917 3918 if (dev->data->promiscuous) 3919 return 0; 3920 3921 rte_spinlock_lock(&hw->lock); 3922 ret = hns3_set_promisc_mode(hw, false, true); 3923 rte_spinlock_unlock(&hw->lock); 3924 if (ret) 3925 hns3_err(hw, "failed to enable allmulticast mode, ret = %d", 3926 ret); 3927 3928 return ret; 3929 } 3930 3931 static int 3932 hns3_dev_allmulticast_disable(struct rte_eth_dev *dev) 3933 { 3934 struct hns3_adapter *hns = dev->data->dev_private; 3935 struct hns3_hw *hw = &hns->hw; 3936 int ret; 3937 3938 /* If now in promiscuous mode, must remain in all_multicast mode. */ 3939 if (dev->data->promiscuous) 3940 return 0; 3941 3942 rte_spinlock_lock(&hw->lock); 3943 ret = hns3_set_promisc_mode(hw, false, false); 3944 rte_spinlock_unlock(&hw->lock); 3945 if (ret) 3946 hns3_err(hw, "failed to disable allmulticast mode, ret = %d", 3947 ret); 3948 3949 return ret; 3950 } 3951 3952 static int 3953 hns3_dev_promisc_restore(struct hns3_adapter *hns) 3954 { 3955 struct hns3_hw *hw = &hns->hw; 3956 bool allmulti = hw->data->all_multicast ? true : false; 3957 int ret; 3958 3959 if (hw->data->promiscuous) { 3960 ret = hns3_set_promisc_mode(hw, true, true); 3961 if (ret) 3962 hns3_err(hw, "failed to restore promiscuous mode, " 3963 "ret = %d", ret); 3964 return ret; 3965 } 3966 3967 ret = hns3_set_promisc_mode(hw, false, allmulti); 3968 if (ret) 3969 hns3_err(hw, "failed to restore allmulticast mode, ret = %d", 3970 ret); 3971 return ret; 3972 } 3973 3974 static int 3975 hns3_get_sfp_info(struct hns3_hw *hw, struct hns3_mac *mac_info) 3976 { 3977 struct hns3_sfp_info_cmd *resp; 3978 struct hns3_cmd_desc desc; 3979 int ret; 3980 3981 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_GET_SFP_INFO, true); 3982 resp = (struct hns3_sfp_info_cmd *)desc.data; 3983 resp->query_type = HNS3_ACTIVE_QUERY; 3984 3985 ret = hns3_cmd_send(hw, &desc, 1); 3986 if (ret == -EOPNOTSUPP) { 3987 hns3_warn(hw, "firmware does not support get SFP info," 3988 " ret = %d.", ret); 3989 return ret; 3990 } else if (ret) { 3991 hns3_err(hw, "get sfp info failed, ret = %d.", ret); 3992 return ret; 3993 } 3994 3995 /* 3996 * In some case, the speed of MAC obtained from firmware may be 0, it 3997 * shouldn't be set to mac->speed. 3998 */ 3999 if (!rte_le_to_cpu_32(resp->sfp_speed)) 4000 return 0; 4001 4002 mac_info->link_speed = rte_le_to_cpu_32(resp->sfp_speed); 4003 /* 4004 * if resp->supported_speed is 0, it means it's an old version 4005 * firmware, do not update these params. 4006 */ 4007 if (resp->supported_speed) { 4008 mac_info->query_type = HNS3_ACTIVE_QUERY; 4009 mac_info->supported_speed = 4010 rte_le_to_cpu_32(resp->supported_speed); 4011 mac_info->support_autoneg = resp->autoneg_ability; 4012 mac_info->link_autoneg = (resp->autoneg == 0) ? RTE_ETH_LINK_FIXED 4013 : RTE_ETH_LINK_AUTONEG; 4014 } else { 4015 mac_info->query_type = HNS3_DEFAULT_QUERY; 4016 } 4017 4018 return 0; 4019 } 4020 4021 static uint8_t 4022 hns3_check_speed_dup(uint8_t duplex, uint32_t speed) 4023 { 4024 if (!(speed == RTE_ETH_SPEED_NUM_10M || speed == RTE_ETH_SPEED_NUM_100M)) 4025 duplex = RTE_ETH_LINK_FULL_DUPLEX; 4026 4027 return duplex; 4028 } 4029 4030 static int 4031 hns3_cfg_mac_speed_dup(struct hns3_hw *hw, uint32_t speed, uint8_t duplex) 4032 { 4033 struct hns3_mac *mac = &hw->mac; 4034 int ret; 4035 4036 duplex = hns3_check_speed_dup(duplex, speed); 4037 if (mac->link_speed == speed && mac->link_duplex == duplex) 4038 return 0; 4039 4040 ret = hns3_cfg_mac_speed_dup_hw(hw, speed, duplex); 4041 if (ret) 4042 return ret; 4043 4044 ret = hns3_port_shaper_update(hw, speed); 4045 if (ret) 4046 return ret; 4047 4048 mac->link_speed = speed; 4049 mac->link_duplex = duplex; 4050 4051 return 0; 4052 } 4053 4054 static int 4055 hns3_update_fiber_link_info(struct hns3_hw *hw) 4056 { 4057 struct hns3_pf *pf = HNS3_DEV_HW_TO_PF(hw); 4058 struct hns3_mac *mac = &hw->mac; 4059 struct hns3_mac mac_info; 4060 int ret; 4061 4062 /* If firmware do not support get SFP/qSFP speed, return directly */ 4063 if (!pf->support_sfp_query) 4064 return 0; 4065 4066 memset(&mac_info, 0, sizeof(struct hns3_mac)); 4067 ret = hns3_get_sfp_info(hw, &mac_info); 4068 if (ret == -EOPNOTSUPP) { 4069 pf->support_sfp_query = false; 4070 return ret; 4071 } else if (ret) 4072 return ret; 4073 4074 /* Do nothing if no SFP */ 4075 if (mac_info.link_speed == RTE_ETH_SPEED_NUM_NONE) 4076 return 0; 4077 4078 /* 4079 * If query_type is HNS3_ACTIVE_QUERY, it is no need 4080 * to reconfigure the speed of MAC. Otherwise, it indicates 4081 * that the current firmware only supports to obtain the 4082 * speed of the SFP, and the speed of MAC needs to reconfigure. 4083 */ 4084 mac->query_type = mac_info.query_type; 4085 if (mac->query_type == HNS3_ACTIVE_QUERY) { 4086 if (mac_info.link_speed != mac->link_speed) { 4087 ret = hns3_port_shaper_update(hw, mac_info.link_speed); 4088 if (ret) 4089 return ret; 4090 } 4091 4092 mac->link_speed = mac_info.link_speed; 4093 mac->supported_speed = mac_info.supported_speed; 4094 mac->support_autoneg = mac_info.support_autoneg; 4095 mac->link_autoneg = mac_info.link_autoneg; 4096 4097 return 0; 4098 } 4099 4100 /* Config full duplex for SFP */ 4101 return hns3_cfg_mac_speed_dup(hw, mac_info.link_speed, 4102 RTE_ETH_LINK_FULL_DUPLEX); 4103 } 4104 4105 static void 4106 hns3_parse_copper_phy_params(struct hns3_cmd_desc *desc, struct hns3_mac *mac) 4107 { 4108 #define HNS3_PHY_SUPPORTED_SPEED_MASK 0x2f 4109 4110 struct hns3_phy_params_bd0_cmd *req; 4111 uint32_t supported; 4112 4113 req = (struct hns3_phy_params_bd0_cmd *)desc[0].data; 4114 mac->link_speed = rte_le_to_cpu_32(req->speed); 4115 mac->link_duplex = hns3_get_bit(req->duplex, 4116 HNS3_PHY_DUPLEX_CFG_B); 4117 mac->link_autoneg = hns3_get_bit(req->autoneg, 4118 HNS3_PHY_AUTONEG_CFG_B); 4119 mac->advertising = rte_le_to_cpu_32(req->advertising); 4120 mac->lp_advertising = rte_le_to_cpu_32(req->lp_advertising); 4121 supported = rte_le_to_cpu_32(req->supported); 4122 mac->supported_speed = supported & HNS3_PHY_SUPPORTED_SPEED_MASK; 4123 mac->support_autoneg = !!(supported & HNS3_PHY_LINK_MODE_AUTONEG_BIT); 4124 } 4125 4126 static int 4127 hns3_get_copper_phy_params(struct hns3_hw *hw, struct hns3_mac *mac) 4128 { 4129 struct hns3_cmd_desc desc[HNS3_PHY_PARAM_CFG_BD_NUM]; 4130 uint16_t i; 4131 int ret; 4132 4133 for (i = 0; i < HNS3_PHY_PARAM_CFG_BD_NUM - 1; i++) { 4134 hns3_cmd_setup_basic_desc(&desc[i], HNS3_OPC_PHY_PARAM_CFG, 4135 true); 4136 desc[i].flag |= rte_cpu_to_le_16(HNS3_CMD_FLAG_NEXT); 4137 } 4138 hns3_cmd_setup_basic_desc(&desc[i], HNS3_OPC_PHY_PARAM_CFG, true); 4139 4140 ret = hns3_cmd_send(hw, desc, HNS3_PHY_PARAM_CFG_BD_NUM); 4141 if (ret) { 4142 hns3_err(hw, "get phy parameters failed, ret = %d.", ret); 4143 return ret; 4144 } 4145 4146 hns3_parse_copper_phy_params(desc, mac); 4147 4148 return 0; 4149 } 4150 4151 static int 4152 hns3_update_copper_link_info(struct hns3_hw *hw) 4153 { 4154 struct hns3_mac *mac = &hw->mac; 4155 struct hns3_mac mac_info; 4156 int ret; 4157 4158 memset(&mac_info, 0, sizeof(struct hns3_mac)); 4159 ret = hns3_get_copper_phy_params(hw, &mac_info); 4160 if (ret) 4161 return ret; 4162 4163 if (mac_info.link_speed != mac->link_speed) { 4164 ret = hns3_port_shaper_update(hw, mac_info.link_speed); 4165 if (ret) 4166 return ret; 4167 } 4168 4169 mac->link_speed = mac_info.link_speed; 4170 mac->link_duplex = mac_info.link_duplex; 4171 mac->link_autoneg = mac_info.link_autoneg; 4172 mac->supported_speed = mac_info.supported_speed; 4173 mac->advertising = mac_info.advertising; 4174 mac->lp_advertising = mac_info.lp_advertising; 4175 mac->support_autoneg = mac_info.support_autoneg; 4176 4177 return 0; 4178 } 4179 4180 static int 4181 hns3_update_link_info(struct rte_eth_dev *eth_dev) 4182 { 4183 struct hns3_adapter *hns = eth_dev->data->dev_private; 4184 struct hns3_hw *hw = &hns->hw; 4185 4186 if (hw->mac.media_type == HNS3_MEDIA_TYPE_COPPER) 4187 return hns3_update_copper_link_info(hw); 4188 4189 return hns3_update_fiber_link_info(hw); 4190 } 4191 4192 static int 4193 hns3_cfg_mac_mode(struct hns3_hw *hw, bool enable) 4194 { 4195 struct hns3_config_mac_mode_cmd *req; 4196 struct hns3_cmd_desc desc; 4197 uint32_t loop_en = 0; 4198 uint8_t val = 0; 4199 int ret; 4200 4201 req = (struct hns3_config_mac_mode_cmd *)desc.data; 4202 4203 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_CONFIG_MAC_MODE, false); 4204 if (enable) 4205 val = 1; 4206 hns3_set_bit(loop_en, HNS3_MAC_TX_EN_B, val); 4207 hns3_set_bit(loop_en, HNS3_MAC_RX_EN_B, val); 4208 hns3_set_bit(loop_en, HNS3_MAC_PAD_TX_B, val); 4209 hns3_set_bit(loop_en, HNS3_MAC_PAD_RX_B, val); 4210 hns3_set_bit(loop_en, HNS3_MAC_1588_TX_B, 0); 4211 hns3_set_bit(loop_en, HNS3_MAC_1588_RX_B, 0); 4212 hns3_set_bit(loop_en, HNS3_MAC_APP_LP_B, 0); 4213 hns3_set_bit(loop_en, HNS3_MAC_LINE_LP_B, 0); 4214 hns3_set_bit(loop_en, HNS3_MAC_FCS_TX_B, val); 4215 hns3_set_bit(loop_en, HNS3_MAC_RX_FCS_B, val); 4216 4217 /* 4218 * If RTE_ETH_RX_OFFLOAD_KEEP_CRC offload is set, MAC will not strip CRC 4219 * when receiving frames. Otherwise, CRC will be stripped. 4220 */ 4221 if (hw->data->dev_conf.rxmode.offloads & RTE_ETH_RX_OFFLOAD_KEEP_CRC) 4222 hns3_set_bit(loop_en, HNS3_MAC_RX_FCS_STRIP_B, 0); 4223 else 4224 hns3_set_bit(loop_en, HNS3_MAC_RX_FCS_STRIP_B, val); 4225 hns3_set_bit(loop_en, HNS3_MAC_TX_OVERSIZE_TRUNCATE_B, val); 4226 hns3_set_bit(loop_en, HNS3_MAC_RX_OVERSIZE_TRUNCATE_B, val); 4227 hns3_set_bit(loop_en, HNS3_MAC_TX_UNDER_MIN_ERR_B, val); 4228 req->txrx_pad_fcs_loop_en = rte_cpu_to_le_32(loop_en); 4229 4230 ret = hns3_cmd_send(hw, &desc, 1); 4231 if (ret) 4232 PMD_INIT_LOG(ERR, "mac enable fail, ret =%d.", ret); 4233 4234 return ret; 4235 } 4236 4237 static int 4238 hns3_get_mac_link_status(struct hns3_hw *hw) 4239 { 4240 struct hns3_link_status_cmd *req; 4241 struct hns3_cmd_desc desc; 4242 int link_status; 4243 int ret; 4244 4245 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_QUERY_LINK_STATUS, true); 4246 ret = hns3_cmd_send(hw, &desc, 1); 4247 if (ret) { 4248 hns3_err(hw, "get link status cmd failed %d", ret); 4249 return RTE_ETH_LINK_DOWN; 4250 } 4251 4252 req = (struct hns3_link_status_cmd *)desc.data; 4253 link_status = req->status & HNS3_LINK_STATUS_UP_M; 4254 4255 return !!link_status; 4256 } 4257 4258 static bool 4259 hns3_update_link_status(struct hns3_hw *hw) 4260 { 4261 int state; 4262 4263 state = hns3_get_mac_link_status(hw); 4264 if (state != hw->mac.link_status) { 4265 hw->mac.link_status = state; 4266 hns3_warn(hw, "Link status change to %s!", state ? "up" : "down"); 4267 return true; 4268 } 4269 4270 return false; 4271 } 4272 4273 void 4274 hns3_update_linkstatus_and_event(struct hns3_hw *hw, bool query) 4275 { 4276 struct rte_eth_dev *dev = &rte_eth_devices[hw->data->port_id]; 4277 struct rte_eth_link new_link; 4278 int ret; 4279 4280 if (query) 4281 hns3_update_port_link_info(dev); 4282 4283 memset(&new_link, 0, sizeof(new_link)); 4284 hns3_setup_linkstatus(dev, &new_link); 4285 4286 ret = rte_eth_linkstatus_set(dev, &new_link); 4287 if (ret == 0 && dev->data->dev_conf.intr_conf.lsc != 0) 4288 hns3_start_report_lse(dev); 4289 } 4290 4291 static void 4292 hns3_service_handler(void *param) 4293 { 4294 struct rte_eth_dev *eth_dev = (struct rte_eth_dev *)param; 4295 struct hns3_adapter *hns = eth_dev->data->dev_private; 4296 struct hns3_hw *hw = &hns->hw; 4297 4298 if (!hns3_is_reset_pending(hns)) { 4299 hns3_update_linkstatus_and_event(hw, true); 4300 hns3_update_hw_stats(hw); 4301 } else { 4302 hns3_warn(hw, "Cancel the query when reset is pending"); 4303 } 4304 4305 rte_eal_alarm_set(HNS3_SERVICE_INTERVAL, hns3_service_handler, eth_dev); 4306 } 4307 4308 static int 4309 hns3_init_hardware(struct hns3_adapter *hns) 4310 { 4311 struct hns3_hw *hw = &hns->hw; 4312 int ret; 4313 4314 /* 4315 * All queue-related HW operations must be performed after the TCAM 4316 * table is configured. 4317 */ 4318 ret = hns3_map_tqp(hw); 4319 if (ret) { 4320 PMD_INIT_LOG(ERR, "Failed to map tqp: %d", ret); 4321 return ret; 4322 } 4323 4324 ret = hns3_init_umv_space(hw); 4325 if (ret) { 4326 PMD_INIT_LOG(ERR, "Failed to init umv space: %d", ret); 4327 return ret; 4328 } 4329 4330 ret = hns3_mac_init(hw); 4331 if (ret) { 4332 PMD_INIT_LOG(ERR, "Failed to init MAC: %d", ret); 4333 goto err_mac_init; 4334 } 4335 4336 ret = hns3_init_mgr_tbl(hw); 4337 if (ret) { 4338 PMD_INIT_LOG(ERR, "Failed to init manager table: %d", ret); 4339 goto err_mac_init; 4340 } 4341 4342 ret = hns3_promisc_init(hw); 4343 if (ret) { 4344 PMD_INIT_LOG(ERR, "Failed to init promisc: %d", 4345 ret); 4346 goto err_mac_init; 4347 } 4348 4349 ret = hns3_init_vlan_config(hns); 4350 if (ret) { 4351 PMD_INIT_LOG(ERR, "Failed to init vlan: %d", ret); 4352 goto err_mac_init; 4353 } 4354 4355 ret = hns3_dcb_init(hw); 4356 if (ret) { 4357 PMD_INIT_LOG(ERR, "Failed to init dcb: %d", ret); 4358 goto err_mac_init; 4359 } 4360 4361 ret = hns3_init_fd_config(hns); 4362 if (ret) { 4363 PMD_INIT_LOG(ERR, "Failed to init flow director: %d", ret); 4364 goto err_mac_init; 4365 } 4366 4367 ret = hns3_config_tso(hw, HNS3_TSO_MSS_MIN, HNS3_TSO_MSS_MAX); 4368 if (ret) { 4369 PMD_INIT_LOG(ERR, "Failed to config tso: %d", ret); 4370 goto err_mac_init; 4371 } 4372 4373 ret = hns3_config_gro(hw, false); 4374 if (ret) { 4375 PMD_INIT_LOG(ERR, "Failed to config gro: %d", ret); 4376 goto err_mac_init; 4377 } 4378 4379 /* 4380 * In the initialization clearing the all hardware mapping relationship 4381 * configurations between queues and interrupt vectors is needed, so 4382 * some error caused by the residual configurations, such as the 4383 * unexpected interrupt, can be avoid. 4384 */ 4385 ret = hns3_init_ring_with_vector(hw); 4386 if (ret) { 4387 PMD_INIT_LOG(ERR, "Failed to init ring intr vector: %d", ret); 4388 goto err_mac_init; 4389 } 4390 4391 return 0; 4392 4393 err_mac_init: 4394 hns3_uninit_umv_space(hw); 4395 return ret; 4396 } 4397 4398 static int 4399 hns3_clear_hw(struct hns3_hw *hw) 4400 { 4401 struct hns3_cmd_desc desc; 4402 int ret; 4403 4404 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_CLEAR_HW_STATE, false); 4405 4406 ret = hns3_cmd_send(hw, &desc, 1); 4407 if (ret && ret != -EOPNOTSUPP) 4408 return ret; 4409 4410 return 0; 4411 } 4412 4413 static void 4414 hns3_config_all_msix_error(struct hns3_hw *hw, bool enable) 4415 { 4416 uint32_t val; 4417 4418 /* 4419 * The new firmware support report more hardware error types by 4420 * msix mode. These errors are defined as RAS errors in hardware 4421 * and belong to a different type from the MSI-x errors processed 4422 * by the network driver. 4423 * 4424 * Network driver should open the new error report on initialization. 4425 */ 4426 val = hns3_read_dev(hw, HNS3_VECTOR0_OTER_EN_REG); 4427 hns3_set_bit(val, HNS3_VECTOR0_ALL_MSIX_ERR_B, enable ? 1 : 0); 4428 hns3_write_dev(hw, HNS3_VECTOR0_OTER_EN_REG, val); 4429 } 4430 4431 static uint32_t 4432 hns3_set_firber_default_support_speed(struct hns3_hw *hw) 4433 { 4434 struct hns3_mac *mac = &hw->mac; 4435 4436 switch (mac->link_speed) { 4437 case RTE_ETH_SPEED_NUM_1G: 4438 return HNS3_FIBER_LINK_SPEED_1G_BIT; 4439 case RTE_ETH_SPEED_NUM_10G: 4440 return HNS3_FIBER_LINK_SPEED_10G_BIT; 4441 case RTE_ETH_SPEED_NUM_25G: 4442 return HNS3_FIBER_LINK_SPEED_25G_BIT; 4443 case RTE_ETH_SPEED_NUM_40G: 4444 return HNS3_FIBER_LINK_SPEED_40G_BIT; 4445 case RTE_ETH_SPEED_NUM_50G: 4446 return HNS3_FIBER_LINK_SPEED_50G_BIT; 4447 case RTE_ETH_SPEED_NUM_100G: 4448 return HNS3_FIBER_LINK_SPEED_100G_BIT; 4449 case RTE_ETH_SPEED_NUM_200G: 4450 return HNS3_FIBER_LINK_SPEED_200G_BIT; 4451 default: 4452 hns3_warn(hw, "invalid speed %u Mbps.", mac->link_speed); 4453 return 0; 4454 } 4455 } 4456 4457 /* 4458 * Validity of supported_speed for fiber and copper media type can be 4459 * guaranteed by the following policy: 4460 * Copper: 4461 * Although the initialization of the phy in the firmware may not be 4462 * completed, the firmware can guarantees that the supported_speed is 4463 * an valid value. 4464 * Firber: 4465 * If the version of firmware supports the active query way of the 4466 * HNS3_OPC_GET_SFP_INFO opcode, the supported_speed can be obtained 4467 * through it. If unsupported, use the SFP's speed as the value of the 4468 * supported_speed. 4469 */ 4470 static int 4471 hns3_get_port_supported_speed(struct rte_eth_dev *eth_dev) 4472 { 4473 struct hns3_adapter *hns = eth_dev->data->dev_private; 4474 struct hns3_hw *hw = &hns->hw; 4475 struct hns3_mac *mac = &hw->mac; 4476 int ret; 4477 4478 ret = hns3_update_link_info(eth_dev); 4479 if (ret) 4480 return ret; 4481 4482 if (mac->media_type == HNS3_MEDIA_TYPE_FIBER || 4483 mac->media_type == HNS3_MEDIA_TYPE_BACKPLANE) { 4484 /* 4485 * Some firmware does not support the report of supported_speed, 4486 * and only report the effective speed of SFP/backplane. In this 4487 * case, it is necessary to use the SFP/backplane's speed as the 4488 * supported_speed. 4489 */ 4490 if (mac->supported_speed == 0) 4491 mac->supported_speed = 4492 hns3_set_firber_default_support_speed(hw); 4493 } 4494 4495 return 0; 4496 } 4497 4498 static void 4499 hns3_get_fc_autoneg_capability(struct hns3_adapter *hns) 4500 { 4501 struct hns3_mac *mac = &hns->hw.mac; 4502 4503 if (mac->media_type == HNS3_MEDIA_TYPE_COPPER) { 4504 hns->pf.support_fc_autoneg = true; 4505 return; 4506 } 4507 4508 /* 4509 * Flow control auto-negotiation requires the cooperation of the driver 4510 * and firmware. Currently, the optical port does not support flow 4511 * control auto-negotiation. 4512 */ 4513 hns->pf.support_fc_autoneg = false; 4514 } 4515 4516 static int 4517 hns3_init_pf(struct rte_eth_dev *eth_dev) 4518 { 4519 struct rte_device *dev = eth_dev->device; 4520 struct rte_pci_device *pci_dev = RTE_DEV_TO_PCI(dev); 4521 struct hns3_adapter *hns = eth_dev->data->dev_private; 4522 struct hns3_hw *hw = &hns->hw; 4523 int ret; 4524 4525 PMD_INIT_FUNC_TRACE(); 4526 4527 /* Get hardware io base address from pcie BAR2 IO space */ 4528 hw->io_base = pci_dev->mem_resource[2].addr; 4529 4530 /* Firmware command queue initialize */ 4531 ret = hns3_cmd_init_queue(hw); 4532 if (ret) { 4533 PMD_INIT_LOG(ERR, "Failed to init cmd queue: %d", ret); 4534 goto err_cmd_init_queue; 4535 } 4536 4537 hns3_clear_all_event_cause(hw); 4538 4539 /* Firmware command initialize */ 4540 ret = hns3_cmd_init(hw); 4541 if (ret) { 4542 PMD_INIT_LOG(ERR, "Failed to init cmd: %d", ret); 4543 goto err_cmd_init; 4544 } 4545 4546 hns3_tx_push_init(eth_dev); 4547 4548 /* 4549 * To ensure that the hardware environment is clean during 4550 * initialization, the driver actively clear the hardware environment 4551 * during initialization, including PF and corresponding VFs' vlan, mac, 4552 * flow table configurations, etc. 4553 */ 4554 ret = hns3_clear_hw(hw); 4555 if (ret) { 4556 PMD_INIT_LOG(ERR, "failed to clear hardware: %d", ret); 4557 goto err_cmd_init; 4558 } 4559 4560 hns3_config_all_msix_error(hw, true); 4561 4562 ret = rte_intr_callback_register(pci_dev->intr_handle, 4563 hns3_interrupt_handler, 4564 eth_dev); 4565 if (ret) { 4566 PMD_INIT_LOG(ERR, "Failed to register intr: %d", ret); 4567 goto err_intr_callback_register; 4568 } 4569 4570 ret = hns3_ptp_init(hw); 4571 if (ret) 4572 goto err_get_config; 4573 4574 /* Enable interrupt */ 4575 rte_intr_enable(pci_dev->intr_handle); 4576 hns3_pf_enable_irq0(hw); 4577 4578 /* Get configuration */ 4579 ret = hns3_get_configuration(hw); 4580 if (ret) { 4581 PMD_INIT_LOG(ERR, "Failed to fetch configuration: %d", ret); 4582 goto err_get_config; 4583 } 4584 4585 ret = hns3_stats_init(hw); 4586 if (ret) 4587 goto err_get_config; 4588 4589 ret = hns3_init_hardware(hns); 4590 if (ret) { 4591 PMD_INIT_LOG(ERR, "Failed to init hardware: %d", ret); 4592 goto err_init_hw; 4593 } 4594 4595 /* Initialize flow director filter list & hash */ 4596 ret = hns3_fdir_filter_init(hns); 4597 if (ret) { 4598 PMD_INIT_LOG(ERR, "Failed to alloc hashmap for fdir: %d", ret); 4599 goto err_fdir; 4600 } 4601 4602 hns3_rss_set_default_args(hw); 4603 4604 ret = hns3_enable_hw_error_intr(hns, true); 4605 if (ret) { 4606 PMD_INIT_LOG(ERR, "fail to enable hw error interrupts: %d", 4607 ret); 4608 goto err_enable_intr; 4609 } 4610 4611 ret = hns3_get_port_supported_speed(eth_dev); 4612 if (ret) { 4613 PMD_INIT_LOG(ERR, "failed to get speed capabilities supported " 4614 "by device, ret = %d.", ret); 4615 goto err_supported_speed; 4616 } 4617 4618 hns3_get_fc_autoneg_capability(hns); 4619 4620 hns3_tm_conf_init(eth_dev); 4621 4622 return 0; 4623 4624 err_supported_speed: 4625 (void)hns3_enable_hw_error_intr(hns, false); 4626 err_enable_intr: 4627 hns3_fdir_filter_uninit(hns); 4628 err_fdir: 4629 hns3_uninit_umv_space(hw); 4630 err_init_hw: 4631 hns3_stats_uninit(hw); 4632 err_get_config: 4633 hns3_pf_disable_irq0(hw); 4634 rte_intr_disable(pci_dev->intr_handle); 4635 hns3_intr_unregister(pci_dev->intr_handle, hns3_interrupt_handler, 4636 eth_dev); 4637 err_intr_callback_register: 4638 err_cmd_init: 4639 hns3_cmd_uninit(hw); 4640 hns3_cmd_destroy_queue(hw); 4641 err_cmd_init_queue: 4642 hw->io_base = NULL; 4643 4644 return ret; 4645 } 4646 4647 static void 4648 hns3_uninit_pf(struct rte_eth_dev *eth_dev) 4649 { 4650 struct hns3_adapter *hns = eth_dev->data->dev_private; 4651 struct rte_device *dev = eth_dev->device; 4652 struct rte_pci_device *pci_dev = RTE_DEV_TO_PCI(dev); 4653 struct hns3_hw *hw = &hns->hw; 4654 4655 PMD_INIT_FUNC_TRACE(); 4656 4657 hns3_tm_conf_uninit(eth_dev); 4658 hns3_enable_hw_error_intr(hns, false); 4659 hns3_rss_uninit(hns); 4660 (void)hns3_config_gro(hw, false); 4661 hns3_promisc_uninit(hw); 4662 hns3_flow_uninit(eth_dev); 4663 hns3_fdir_filter_uninit(hns); 4664 hns3_uninit_umv_space(hw); 4665 hns3_stats_uninit(hw); 4666 hns3_config_mac_tnl_int(hw, false); 4667 hns3_pf_disable_irq0(hw); 4668 rte_intr_disable(pci_dev->intr_handle); 4669 hns3_intr_unregister(pci_dev->intr_handle, hns3_interrupt_handler, 4670 eth_dev); 4671 hns3_config_all_msix_error(hw, false); 4672 hns3_cmd_uninit(hw); 4673 hns3_cmd_destroy_queue(hw); 4674 hw->io_base = NULL; 4675 } 4676 4677 static uint32_t 4678 hns3_convert_link_speeds2bitmap_copper(uint32_t link_speeds) 4679 { 4680 uint32_t speed_bit; 4681 4682 switch (link_speeds & ~RTE_ETH_LINK_SPEED_FIXED) { 4683 case RTE_ETH_LINK_SPEED_10M: 4684 speed_bit = HNS3_PHY_LINK_SPEED_10M_BIT; 4685 break; 4686 case RTE_ETH_LINK_SPEED_10M_HD: 4687 speed_bit = HNS3_PHY_LINK_SPEED_10M_HD_BIT; 4688 break; 4689 case RTE_ETH_LINK_SPEED_100M: 4690 speed_bit = HNS3_PHY_LINK_SPEED_100M_BIT; 4691 break; 4692 case RTE_ETH_LINK_SPEED_100M_HD: 4693 speed_bit = HNS3_PHY_LINK_SPEED_100M_HD_BIT; 4694 break; 4695 case RTE_ETH_LINK_SPEED_1G: 4696 speed_bit = HNS3_PHY_LINK_SPEED_1000M_BIT; 4697 break; 4698 default: 4699 speed_bit = 0; 4700 break; 4701 } 4702 4703 return speed_bit; 4704 } 4705 4706 static uint32_t 4707 hns3_convert_link_speeds2bitmap_fiber(uint32_t link_speeds) 4708 { 4709 uint32_t speed_bit; 4710 4711 switch (link_speeds & ~RTE_ETH_LINK_SPEED_FIXED) { 4712 case RTE_ETH_LINK_SPEED_1G: 4713 speed_bit = HNS3_FIBER_LINK_SPEED_1G_BIT; 4714 break; 4715 case RTE_ETH_LINK_SPEED_10G: 4716 speed_bit = HNS3_FIBER_LINK_SPEED_10G_BIT; 4717 break; 4718 case RTE_ETH_LINK_SPEED_25G: 4719 speed_bit = HNS3_FIBER_LINK_SPEED_25G_BIT; 4720 break; 4721 case RTE_ETH_LINK_SPEED_40G: 4722 speed_bit = HNS3_FIBER_LINK_SPEED_40G_BIT; 4723 break; 4724 case RTE_ETH_LINK_SPEED_50G: 4725 speed_bit = HNS3_FIBER_LINK_SPEED_50G_BIT; 4726 break; 4727 case RTE_ETH_LINK_SPEED_100G: 4728 speed_bit = HNS3_FIBER_LINK_SPEED_100G_BIT; 4729 break; 4730 case RTE_ETH_LINK_SPEED_200G: 4731 speed_bit = HNS3_FIBER_LINK_SPEED_200G_BIT; 4732 break; 4733 default: 4734 speed_bit = 0; 4735 break; 4736 } 4737 4738 return speed_bit; 4739 } 4740 4741 static int 4742 hns3_check_port_speed(struct hns3_hw *hw, uint32_t link_speeds) 4743 { 4744 struct hns3_mac *mac = &hw->mac; 4745 uint32_t supported_speed = mac->supported_speed; 4746 uint32_t speed_bit = 0; 4747 4748 if (mac->media_type == HNS3_MEDIA_TYPE_COPPER) 4749 speed_bit = hns3_convert_link_speeds2bitmap_copper(link_speeds); 4750 else 4751 speed_bit = hns3_convert_link_speeds2bitmap_fiber(link_speeds); 4752 4753 if (!(speed_bit & supported_speed)) { 4754 hns3_err(hw, "link_speeds(0x%x) exceeds the supported speed capability or is incorrect.", 4755 link_speeds); 4756 return -EINVAL; 4757 } 4758 4759 return 0; 4760 } 4761 4762 static uint32_t 4763 hns3_get_link_speed(uint32_t link_speeds) 4764 { 4765 uint32_t speed = RTE_ETH_SPEED_NUM_NONE; 4766 4767 if (link_speeds & RTE_ETH_LINK_SPEED_10M || 4768 link_speeds & RTE_ETH_LINK_SPEED_10M_HD) 4769 speed = RTE_ETH_SPEED_NUM_10M; 4770 if (link_speeds & RTE_ETH_LINK_SPEED_100M || 4771 link_speeds & RTE_ETH_LINK_SPEED_100M_HD) 4772 speed = RTE_ETH_SPEED_NUM_100M; 4773 if (link_speeds & RTE_ETH_LINK_SPEED_1G) 4774 speed = RTE_ETH_SPEED_NUM_1G; 4775 if (link_speeds & RTE_ETH_LINK_SPEED_10G) 4776 speed = RTE_ETH_SPEED_NUM_10G; 4777 if (link_speeds & RTE_ETH_LINK_SPEED_25G) 4778 speed = RTE_ETH_SPEED_NUM_25G; 4779 if (link_speeds & RTE_ETH_LINK_SPEED_40G) 4780 speed = RTE_ETH_SPEED_NUM_40G; 4781 if (link_speeds & RTE_ETH_LINK_SPEED_50G) 4782 speed = RTE_ETH_SPEED_NUM_50G; 4783 if (link_speeds & RTE_ETH_LINK_SPEED_100G) 4784 speed = RTE_ETH_SPEED_NUM_100G; 4785 if (link_speeds & RTE_ETH_LINK_SPEED_200G) 4786 speed = RTE_ETH_SPEED_NUM_200G; 4787 4788 return speed; 4789 } 4790 4791 static uint8_t 4792 hns3_get_link_duplex(uint32_t link_speeds) 4793 { 4794 if ((link_speeds & RTE_ETH_LINK_SPEED_10M_HD) || 4795 (link_speeds & RTE_ETH_LINK_SPEED_100M_HD)) 4796 return RTE_ETH_LINK_HALF_DUPLEX; 4797 else 4798 return RTE_ETH_LINK_FULL_DUPLEX; 4799 } 4800 4801 static int 4802 hns3_set_copper_port_link_speed(struct hns3_hw *hw, 4803 struct hns3_set_link_speed_cfg *cfg) 4804 { 4805 struct hns3_cmd_desc desc[HNS3_PHY_PARAM_CFG_BD_NUM]; 4806 struct hns3_phy_params_bd0_cmd *req; 4807 uint16_t i; 4808 4809 for (i = 0; i < HNS3_PHY_PARAM_CFG_BD_NUM - 1; i++) { 4810 hns3_cmd_setup_basic_desc(&desc[i], HNS3_OPC_PHY_PARAM_CFG, 4811 false); 4812 desc[i].flag |= rte_cpu_to_le_16(HNS3_CMD_FLAG_NEXT); 4813 } 4814 hns3_cmd_setup_basic_desc(&desc[i], HNS3_OPC_PHY_PARAM_CFG, false); 4815 req = (struct hns3_phy_params_bd0_cmd *)desc[0].data; 4816 req->autoneg = cfg->autoneg; 4817 4818 /* 4819 * The full speed capability is used to negotiate when 4820 * auto-negotiation is enabled. 4821 */ 4822 if (cfg->autoneg) { 4823 req->advertising = HNS3_PHY_LINK_SPEED_10M_BIT | 4824 HNS3_PHY_LINK_SPEED_10M_HD_BIT | 4825 HNS3_PHY_LINK_SPEED_100M_BIT | 4826 HNS3_PHY_LINK_SPEED_100M_HD_BIT | 4827 HNS3_PHY_LINK_SPEED_1000M_BIT; 4828 } else { 4829 req->speed = cfg->speed; 4830 req->duplex = cfg->duplex; 4831 } 4832 4833 return hns3_cmd_send(hw, desc, HNS3_PHY_PARAM_CFG_BD_NUM); 4834 } 4835 4836 static int 4837 hns3_set_autoneg(struct hns3_hw *hw, bool enable) 4838 { 4839 struct hns3_config_auto_neg_cmd *req; 4840 struct hns3_cmd_desc desc; 4841 uint32_t flag = 0; 4842 int ret; 4843 4844 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_CONFIG_AN_MODE, false); 4845 4846 req = (struct hns3_config_auto_neg_cmd *)desc.data; 4847 if (enable) 4848 hns3_set_bit(flag, HNS3_MAC_CFG_AN_EN_B, 1); 4849 req->cfg_an_cmd_flag = rte_cpu_to_le_32(flag); 4850 4851 ret = hns3_cmd_send(hw, &desc, 1); 4852 if (ret) 4853 hns3_err(hw, "autoneg set cmd failed, ret = %d.", ret); 4854 4855 return ret; 4856 } 4857 4858 static int 4859 hns3_set_fiber_port_link_speed(struct hns3_hw *hw, 4860 struct hns3_set_link_speed_cfg *cfg) 4861 { 4862 int ret; 4863 4864 if (hw->mac.support_autoneg) { 4865 ret = hns3_set_autoneg(hw, cfg->autoneg); 4866 if (ret) { 4867 hns3_err(hw, "failed to configure auto-negotiation."); 4868 return ret; 4869 } 4870 4871 /* 4872 * To enable auto-negotiation, we only need to open the switch 4873 * of auto-negotiation, then firmware sets all speed 4874 * capabilities. 4875 */ 4876 if (cfg->autoneg) 4877 return 0; 4878 } 4879 4880 /* 4881 * Some hardware doesn't support auto-negotiation, but users may not 4882 * configure link_speeds (default 0), which means auto-negotiation. 4883 * In this case, a warning message need to be printed, instead of 4884 * an error. 4885 */ 4886 if (cfg->autoneg) { 4887 hns3_warn(hw, "auto-negotiation is not supported, use default fixed speed!"); 4888 return 0; 4889 } 4890 4891 return hns3_cfg_mac_speed_dup(hw, cfg->speed, cfg->duplex); 4892 } 4893 4894 static const char * 4895 hns3_get_media_type_name(uint8_t media_type) 4896 { 4897 if (media_type == HNS3_MEDIA_TYPE_FIBER) 4898 return "fiber"; 4899 else if (media_type == HNS3_MEDIA_TYPE_COPPER) 4900 return "copper"; 4901 else if (media_type == HNS3_MEDIA_TYPE_BACKPLANE) 4902 return "backplane"; 4903 else 4904 return "unknown"; 4905 } 4906 4907 static int 4908 hns3_set_port_link_speed(struct hns3_hw *hw, 4909 struct hns3_set_link_speed_cfg *cfg) 4910 { 4911 int ret; 4912 4913 if (hw->mac.media_type == HNS3_MEDIA_TYPE_COPPER) 4914 ret = hns3_set_copper_port_link_speed(hw, cfg); 4915 else 4916 ret = hns3_set_fiber_port_link_speed(hw, cfg); 4917 4918 if (ret) { 4919 hns3_err(hw, "failed to set %s port link speed, ret = %d.", 4920 hns3_get_media_type_name(hw->mac.media_type), 4921 ret); 4922 return ret; 4923 } 4924 4925 return 0; 4926 } 4927 4928 static int 4929 hns3_apply_link_speed(struct hns3_hw *hw) 4930 { 4931 struct rte_eth_conf *conf = &hw->data->dev_conf; 4932 struct hns3_set_link_speed_cfg cfg; 4933 4934 memset(&cfg, 0, sizeof(struct hns3_set_link_speed_cfg)); 4935 cfg.autoneg = (conf->link_speeds == RTE_ETH_LINK_SPEED_AUTONEG) ? 4936 RTE_ETH_LINK_AUTONEG : RTE_ETH_LINK_FIXED; 4937 if (cfg.autoneg != RTE_ETH_LINK_AUTONEG) { 4938 cfg.speed = hns3_get_link_speed(conf->link_speeds); 4939 cfg.duplex = hns3_get_link_duplex(conf->link_speeds); 4940 } 4941 4942 return hns3_set_port_link_speed(hw, &cfg); 4943 } 4944 4945 static int 4946 hns3_do_start(struct hns3_adapter *hns, bool reset_queue) 4947 { 4948 struct hns3_hw *hw = &hns->hw; 4949 bool link_en; 4950 int ret; 4951 4952 ret = hns3_update_queue_map_configure(hns); 4953 if (ret) { 4954 hns3_err(hw, "failed to update queue mapping configuration, ret = %d", 4955 ret); 4956 return ret; 4957 } 4958 4959 /* Note: hns3_tm_conf_update must be called after configuring DCB. */ 4960 ret = hns3_tm_conf_update(hw); 4961 if (ret) { 4962 PMD_INIT_LOG(ERR, "failed to update tm conf, ret = %d.", ret); 4963 return ret; 4964 } 4965 4966 hns3_enable_rxd_adv_layout(hw); 4967 4968 ret = hns3_init_queues(hns, reset_queue); 4969 if (ret) { 4970 PMD_INIT_LOG(ERR, "failed to init queues, ret = %d.", ret); 4971 return ret; 4972 } 4973 4974 link_en = hw->set_link_down ? false : true; 4975 ret = hns3_cfg_mac_mode(hw, link_en); 4976 if (ret) { 4977 PMD_INIT_LOG(ERR, "failed to enable MAC, ret = %d", ret); 4978 goto err_config_mac_mode; 4979 } 4980 4981 ret = hns3_apply_link_speed(hw); 4982 if (ret) 4983 goto err_set_link_speed; 4984 4985 return hns3_restore_filter(hns); 4986 4987 err_set_link_speed: 4988 (void)hns3_cfg_mac_mode(hw, false); 4989 4990 err_config_mac_mode: 4991 hns3_dev_release_mbufs(hns); 4992 /* 4993 * Here is exception handling, hns3_reset_all_tqps will have the 4994 * corresponding error message if it is handled incorrectly, so it is 4995 * not necessary to check hns3_reset_all_tqps return value, here keep 4996 * ret as the error code causing the exception. 4997 */ 4998 (void)hns3_reset_all_tqps(hns); 4999 return ret; 5000 } 5001 5002 static int 5003 hns3_dev_start(struct rte_eth_dev *dev) 5004 { 5005 struct hns3_adapter *hns = dev->data->dev_private; 5006 struct hns3_hw *hw = &hns->hw; 5007 bool old_state = hw->set_link_down; 5008 int ret; 5009 5010 PMD_INIT_FUNC_TRACE(); 5011 if (__atomic_load_n(&hw->reset.resetting, __ATOMIC_RELAXED)) 5012 return -EBUSY; 5013 5014 rte_spinlock_lock(&hw->lock); 5015 hw->adapter_state = HNS3_NIC_STARTING; 5016 5017 /* 5018 * If the dev_set_link_down() API has been called, the "set_link_down" 5019 * flag can be cleared by dev_start() API. In addition, the flag should 5020 * also be cleared before calling hns3_do_start() so that MAC can be 5021 * enabled in dev_start stage. 5022 */ 5023 hw->set_link_down = false; 5024 ret = hns3_do_start(hns, true); 5025 if (ret) 5026 goto do_start_fail; 5027 5028 ret = hns3_map_rx_interrupt(dev); 5029 if (ret) 5030 goto map_rx_inter_err; 5031 5032 /* 5033 * There are three register used to control the status of a TQP 5034 * (contains a pair of Tx queue and Rx queue) in the new version network 5035 * engine. One is used to control the enabling of Tx queue, the other is 5036 * used to control the enabling of Rx queue, and the last is the master 5037 * switch used to control the enabling of the tqp. The Tx register and 5038 * TQP register must be enabled at the same time to enable a Tx queue. 5039 * The same applies to the Rx queue. For the older network engine, this 5040 * function only refresh the enabled flag, and it is used to update the 5041 * status of queue in the dpdk framework. 5042 */ 5043 ret = hns3_start_all_txqs(dev); 5044 if (ret) 5045 goto map_rx_inter_err; 5046 5047 ret = hns3_start_all_rxqs(dev); 5048 if (ret) 5049 goto start_all_rxqs_fail; 5050 5051 hw->adapter_state = HNS3_NIC_STARTED; 5052 rte_spinlock_unlock(&hw->lock); 5053 5054 hns3_rx_scattered_calc(dev); 5055 hns3_set_rxtx_function(dev); 5056 hns3_mp_req_start_rxtx(dev); 5057 5058 /* Enable interrupt of all rx queues before enabling queues */ 5059 hns3_dev_all_rx_queue_intr_enable(hw, true); 5060 5061 /* 5062 * After finished the initialization, enable tqps to receive/transmit 5063 * packets and refresh all queue status. 5064 */ 5065 hns3_start_tqps(hw); 5066 5067 hns3_tm_dev_start_proc(hw); 5068 5069 if (dev->data->dev_conf.intr_conf.lsc != 0) 5070 hns3_dev_link_update(dev, 0); 5071 rte_eal_alarm_set(HNS3_SERVICE_INTERVAL, hns3_service_handler, dev); 5072 5073 hns3_info(hw, "hns3 dev start successful!"); 5074 5075 return 0; 5076 5077 start_all_rxqs_fail: 5078 hns3_stop_all_txqs(dev); 5079 map_rx_inter_err: 5080 (void)hns3_do_stop(hns); 5081 do_start_fail: 5082 hw->set_link_down = old_state; 5083 hw->adapter_state = HNS3_NIC_CONFIGURED; 5084 rte_spinlock_unlock(&hw->lock); 5085 5086 return ret; 5087 } 5088 5089 static int 5090 hns3_do_stop(struct hns3_adapter *hns) 5091 { 5092 struct hns3_hw *hw = &hns->hw; 5093 int ret; 5094 5095 /* 5096 * The "hns3_do_stop" function will also be called by .stop_service to 5097 * prepare reset. At the time of global or IMP reset, the command cannot 5098 * be sent to stop the tx/rx queues. The mbuf in Tx/Rx queues may be 5099 * accessed during the reset process. So the mbuf can not be released 5100 * during reset and is required to be released after the reset is 5101 * completed. 5102 */ 5103 if (__atomic_load_n(&hw->reset.resetting, __ATOMIC_RELAXED) == 0) 5104 hns3_dev_release_mbufs(hns); 5105 5106 ret = hns3_cfg_mac_mode(hw, false); 5107 if (ret) 5108 return ret; 5109 hw->mac.link_status = RTE_ETH_LINK_DOWN; 5110 5111 if (__atomic_load_n(&hw->reset.disable_cmd, __ATOMIC_RELAXED) == 0) { 5112 hns3_configure_all_mac_addr(hns, true); 5113 ret = hns3_reset_all_tqps(hns); 5114 if (ret) { 5115 hns3_err(hw, "failed to reset all queues ret = %d.", 5116 ret); 5117 return ret; 5118 } 5119 } 5120 5121 return 0; 5122 } 5123 5124 static int 5125 hns3_dev_stop(struct rte_eth_dev *dev) 5126 { 5127 struct hns3_adapter *hns = dev->data->dev_private; 5128 struct hns3_hw *hw = &hns->hw; 5129 5130 PMD_INIT_FUNC_TRACE(); 5131 dev->data->dev_started = 0; 5132 5133 hw->adapter_state = HNS3_NIC_STOPPING; 5134 hns3_set_rxtx_function(dev); 5135 rte_wmb(); 5136 /* Disable datapath on secondary process. */ 5137 hns3_mp_req_stop_rxtx(dev); 5138 /* Prevent crashes when queues are still in use. */ 5139 rte_delay_ms(hw->cfg_max_queues); 5140 5141 rte_spinlock_lock(&hw->lock); 5142 if (__atomic_load_n(&hw->reset.resetting, __ATOMIC_RELAXED) == 0) { 5143 hns3_tm_dev_stop_proc(hw); 5144 hns3_config_mac_tnl_int(hw, false); 5145 hns3_stop_tqps(hw); 5146 hns3_do_stop(hns); 5147 hns3_unmap_rx_interrupt(dev); 5148 hw->adapter_state = HNS3_NIC_CONFIGURED; 5149 } 5150 hns3_rx_scattered_reset(dev); 5151 rte_eal_alarm_cancel(hns3_service_handler, dev); 5152 hns3_stop_report_lse(dev); 5153 rte_spinlock_unlock(&hw->lock); 5154 5155 return 0; 5156 } 5157 5158 static int 5159 hns3_dev_close(struct rte_eth_dev *eth_dev) 5160 { 5161 struct hns3_adapter *hns = eth_dev->data->dev_private; 5162 struct hns3_hw *hw = &hns->hw; 5163 int ret = 0; 5164 5165 if (rte_eal_process_type() != RTE_PROC_PRIMARY) { 5166 hns3_mp_uninit(eth_dev); 5167 return 0; 5168 } 5169 5170 if (hw->adapter_state == HNS3_NIC_STARTED) 5171 ret = hns3_dev_stop(eth_dev); 5172 5173 hw->adapter_state = HNS3_NIC_CLOSING; 5174 hns3_reset_abort(hns); 5175 hw->adapter_state = HNS3_NIC_CLOSED; 5176 5177 hns3_configure_all_mc_mac_addr(hns, true); 5178 hns3_remove_all_vlan_table(hns); 5179 hns3_vlan_txvlan_cfg(hns, HNS3_PORT_BASE_VLAN_DISABLE, 0); 5180 hns3_uninit_pf(eth_dev); 5181 hns3_free_all_queues(eth_dev); 5182 rte_free(hw->reset.wait_data); 5183 hns3_mp_uninit(eth_dev); 5184 hns3_warn(hw, "Close port %u finished", hw->data->port_id); 5185 5186 return ret; 5187 } 5188 5189 static void 5190 hns3_get_autoneg_rxtx_pause_copper(struct hns3_hw *hw, bool *rx_pause, 5191 bool *tx_pause) 5192 { 5193 struct hns3_mac *mac = &hw->mac; 5194 uint32_t advertising = mac->advertising; 5195 uint32_t lp_advertising = mac->lp_advertising; 5196 *rx_pause = false; 5197 *tx_pause = false; 5198 5199 if (advertising & lp_advertising & HNS3_PHY_LINK_MODE_PAUSE_BIT) { 5200 *rx_pause = true; 5201 *tx_pause = true; 5202 } else if (advertising & lp_advertising & 5203 HNS3_PHY_LINK_MODE_ASYM_PAUSE_BIT) { 5204 if (advertising & HNS3_PHY_LINK_MODE_PAUSE_BIT) 5205 *rx_pause = true; 5206 else if (lp_advertising & HNS3_PHY_LINK_MODE_PAUSE_BIT) 5207 *tx_pause = true; 5208 } 5209 } 5210 5211 static enum hns3_fc_mode 5212 hns3_get_autoneg_fc_mode(struct hns3_hw *hw) 5213 { 5214 enum hns3_fc_mode current_mode; 5215 bool rx_pause = false; 5216 bool tx_pause = false; 5217 5218 switch (hw->mac.media_type) { 5219 case HNS3_MEDIA_TYPE_COPPER: 5220 hns3_get_autoneg_rxtx_pause_copper(hw, &rx_pause, &tx_pause); 5221 break; 5222 5223 /* 5224 * Flow control auto-negotiation is not supported for fiber and 5225 * backplane media type. 5226 */ 5227 case HNS3_MEDIA_TYPE_FIBER: 5228 case HNS3_MEDIA_TYPE_BACKPLANE: 5229 hns3_err(hw, "autoneg FC mode can't be obtained, but flow control auto-negotiation is enabled."); 5230 current_mode = hw->requested_fc_mode; 5231 goto out; 5232 default: 5233 hns3_err(hw, "autoneg FC mode can't be obtained for unknown media type(%u).", 5234 hw->mac.media_type); 5235 current_mode = HNS3_FC_NONE; 5236 goto out; 5237 } 5238 5239 if (rx_pause && tx_pause) 5240 current_mode = HNS3_FC_FULL; 5241 else if (rx_pause) 5242 current_mode = HNS3_FC_RX_PAUSE; 5243 else if (tx_pause) 5244 current_mode = HNS3_FC_TX_PAUSE; 5245 else 5246 current_mode = HNS3_FC_NONE; 5247 5248 out: 5249 return current_mode; 5250 } 5251 5252 static enum hns3_fc_mode 5253 hns3_get_current_fc_mode(struct rte_eth_dev *dev) 5254 { 5255 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private); 5256 struct hns3_pf *pf = HNS3_DEV_PRIVATE_TO_PF(dev->data->dev_private); 5257 struct hns3_mac *mac = &hw->mac; 5258 5259 /* 5260 * When the flow control mode is obtained, the device may not complete 5261 * auto-negotiation. It is necessary to wait for link establishment. 5262 */ 5263 (void)hns3_dev_link_update(dev, 1); 5264 5265 /* 5266 * If the link auto-negotiation of the nic is disabled, or the flow 5267 * control auto-negotiation is not supported, the forced flow control 5268 * mode is used. 5269 */ 5270 if (mac->link_autoneg == 0 || !pf->support_fc_autoneg) 5271 return hw->requested_fc_mode; 5272 5273 return hns3_get_autoneg_fc_mode(hw); 5274 } 5275 5276 int 5277 hns3_flow_ctrl_get(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf) 5278 { 5279 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private); 5280 struct hns3_pf *pf = HNS3_DEV_PRIVATE_TO_PF(dev->data->dev_private); 5281 enum hns3_fc_mode current_mode; 5282 5283 current_mode = hns3_get_current_fc_mode(dev); 5284 switch (current_mode) { 5285 case HNS3_FC_FULL: 5286 fc_conf->mode = RTE_ETH_FC_FULL; 5287 break; 5288 case HNS3_FC_TX_PAUSE: 5289 fc_conf->mode = RTE_ETH_FC_TX_PAUSE; 5290 break; 5291 case HNS3_FC_RX_PAUSE: 5292 fc_conf->mode = RTE_ETH_FC_RX_PAUSE; 5293 break; 5294 case HNS3_FC_NONE: 5295 default: 5296 fc_conf->mode = RTE_ETH_FC_NONE; 5297 break; 5298 } 5299 5300 fc_conf->pause_time = pf->pause_time; 5301 fc_conf->autoneg = pf->support_fc_autoneg ? hw->mac.link_autoneg : 0; 5302 5303 return 0; 5304 } 5305 5306 static int 5307 hns3_check_fc_autoneg_valid(struct hns3_hw *hw, uint8_t autoneg) 5308 { 5309 struct hns3_pf *pf = HNS3_DEV_HW_TO_PF(hw); 5310 5311 if (!pf->support_fc_autoneg) { 5312 if (autoneg != 0) { 5313 hns3_err(hw, "unsupported fc auto-negotiation setting."); 5314 return -EOPNOTSUPP; 5315 } 5316 5317 /* 5318 * Flow control auto-negotiation of the NIC is not supported, 5319 * but other auto-negotiation features may be supported. 5320 */ 5321 if (autoneg != hw->mac.link_autoneg) { 5322 hns3_err(hw, "please use 'link_speeds' in struct rte_eth_conf to disable autoneg!"); 5323 return -EOPNOTSUPP; 5324 } 5325 5326 return 0; 5327 } 5328 5329 /* 5330 * If flow control auto-negotiation of the NIC is supported, all 5331 * auto-negotiation features are supported. 5332 */ 5333 if (autoneg != hw->mac.link_autoneg) { 5334 hns3_err(hw, "please use 'link_speeds' in struct rte_eth_conf to change autoneg!"); 5335 return -EOPNOTSUPP; 5336 } 5337 5338 return 0; 5339 } 5340 5341 static int 5342 hns3_flow_ctrl_set(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf) 5343 { 5344 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private); 5345 struct hns3_pf *pf = HNS3_DEV_PRIVATE_TO_PF(dev->data->dev_private); 5346 int ret; 5347 5348 if (fc_conf->high_water || fc_conf->low_water || 5349 fc_conf->send_xon || fc_conf->mac_ctrl_frame_fwd) { 5350 hns3_err(hw, "Unsupported flow control settings specified, " 5351 "high_water(%u), low_water(%u), send_xon(%u) and " 5352 "mac_ctrl_frame_fwd(%u) must be set to '0'", 5353 fc_conf->high_water, fc_conf->low_water, 5354 fc_conf->send_xon, fc_conf->mac_ctrl_frame_fwd); 5355 return -EINVAL; 5356 } 5357 5358 ret = hns3_check_fc_autoneg_valid(hw, fc_conf->autoneg); 5359 if (ret) 5360 return ret; 5361 5362 if (!fc_conf->pause_time) { 5363 hns3_err(hw, "Invalid pause time %u setting.", 5364 fc_conf->pause_time); 5365 return -EINVAL; 5366 } 5367 5368 if (!(hw->current_fc_status == HNS3_FC_STATUS_NONE || 5369 hw->current_fc_status == HNS3_FC_STATUS_MAC_PAUSE)) { 5370 hns3_err(hw, "PFC is enabled. Cannot set MAC pause. " 5371 "current_fc_status = %d", hw->current_fc_status); 5372 return -EOPNOTSUPP; 5373 } 5374 5375 if (hw->num_tc > 1 && !pf->support_multi_tc_pause) { 5376 hns3_err(hw, "in multi-TC scenarios, MAC pause is not supported."); 5377 return -EOPNOTSUPP; 5378 } 5379 5380 rte_spinlock_lock(&hw->lock); 5381 ret = hns3_fc_enable(dev, fc_conf); 5382 rte_spinlock_unlock(&hw->lock); 5383 5384 return ret; 5385 } 5386 5387 static int 5388 hns3_priority_flow_ctrl_set(struct rte_eth_dev *dev, 5389 struct rte_eth_pfc_conf *pfc_conf) 5390 { 5391 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private); 5392 int ret; 5393 5394 if (!hns3_dev_get_support(hw, DCB)) { 5395 hns3_err(hw, "This port does not support dcb configurations."); 5396 return -EOPNOTSUPP; 5397 } 5398 5399 if (pfc_conf->fc.high_water || pfc_conf->fc.low_water || 5400 pfc_conf->fc.send_xon || pfc_conf->fc.mac_ctrl_frame_fwd) { 5401 hns3_err(hw, "Unsupported flow control settings specified, " 5402 "high_water(%u), low_water(%u), send_xon(%u) and " 5403 "mac_ctrl_frame_fwd(%u) must be set to '0'", 5404 pfc_conf->fc.high_water, pfc_conf->fc.low_water, 5405 pfc_conf->fc.send_xon, 5406 pfc_conf->fc.mac_ctrl_frame_fwd); 5407 return -EINVAL; 5408 } 5409 if (pfc_conf->fc.autoneg) { 5410 hns3_err(hw, "Unsupported fc auto-negotiation setting."); 5411 return -EINVAL; 5412 } 5413 if (pfc_conf->fc.pause_time == 0) { 5414 hns3_err(hw, "Invalid pause time %u setting.", 5415 pfc_conf->fc.pause_time); 5416 return -EINVAL; 5417 } 5418 5419 if (!(hw->current_fc_status == HNS3_FC_STATUS_NONE || 5420 hw->current_fc_status == HNS3_FC_STATUS_PFC)) { 5421 hns3_err(hw, "MAC pause is enabled. Cannot set PFC." 5422 "current_fc_status = %d", hw->current_fc_status); 5423 return -EOPNOTSUPP; 5424 } 5425 5426 rte_spinlock_lock(&hw->lock); 5427 ret = hns3_dcb_pfc_enable(dev, pfc_conf); 5428 rte_spinlock_unlock(&hw->lock); 5429 5430 return ret; 5431 } 5432 5433 static int 5434 hns3_get_dcb_info(struct rte_eth_dev *dev, struct rte_eth_dcb_info *dcb_info) 5435 { 5436 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private); 5437 struct hns3_pf *pf = HNS3_DEV_PRIVATE_TO_PF(dev->data->dev_private); 5438 enum rte_eth_rx_mq_mode mq_mode = dev->data->dev_conf.rxmode.mq_mode; 5439 int i; 5440 5441 rte_spinlock_lock(&hw->lock); 5442 if ((uint32_t)mq_mode & RTE_ETH_MQ_RX_DCB_FLAG) 5443 dcb_info->nb_tcs = pf->local_max_tc; 5444 else 5445 dcb_info->nb_tcs = 1; 5446 5447 for (i = 0; i < HNS3_MAX_USER_PRIO; i++) 5448 dcb_info->prio_tc[i] = hw->dcb_info.prio_tc[i]; 5449 for (i = 0; i < dcb_info->nb_tcs; i++) 5450 dcb_info->tc_bws[i] = hw->dcb_info.pg_info[0].tc_dwrr[i]; 5451 5452 for (i = 0; i < hw->num_tc; i++) { 5453 dcb_info->tc_queue.tc_rxq[0][i].base = hw->alloc_rss_size * i; 5454 dcb_info->tc_queue.tc_txq[0][i].base = 5455 hw->tc_queue[i].tqp_offset; 5456 dcb_info->tc_queue.tc_rxq[0][i].nb_queue = hw->alloc_rss_size; 5457 dcb_info->tc_queue.tc_txq[0][i].nb_queue = 5458 hw->tc_queue[i].tqp_count; 5459 } 5460 rte_spinlock_unlock(&hw->lock); 5461 5462 return 0; 5463 } 5464 5465 static int 5466 hns3_reinit_dev(struct hns3_adapter *hns) 5467 { 5468 struct hns3_hw *hw = &hns->hw; 5469 int ret; 5470 5471 ret = hns3_cmd_init(hw); 5472 if (ret) { 5473 hns3_err(hw, "Failed to init cmd: %d", ret); 5474 return ret; 5475 } 5476 5477 ret = hns3_init_hardware(hns); 5478 if (ret) { 5479 hns3_err(hw, "Failed to init hardware: %d", ret); 5480 return ret; 5481 } 5482 5483 ret = hns3_reset_all_tqps(hns); 5484 if (ret) { 5485 hns3_err(hw, "Failed to reset all queues: %d", ret); 5486 return ret; 5487 } 5488 5489 ret = hns3_enable_hw_error_intr(hns, true); 5490 if (ret) { 5491 hns3_err(hw, "fail to enable hw error interrupts: %d", 5492 ret); 5493 return ret; 5494 } 5495 hns3_info(hw, "Reset done, driver initialization finished."); 5496 5497 return 0; 5498 } 5499 5500 static bool 5501 is_pf_reset_done(struct hns3_hw *hw) 5502 { 5503 uint32_t val, reg, reg_bit; 5504 5505 switch (hw->reset.level) { 5506 case HNS3_IMP_RESET: 5507 reg = HNS3_GLOBAL_RESET_REG; 5508 reg_bit = HNS3_IMP_RESET_BIT; 5509 break; 5510 case HNS3_GLOBAL_RESET: 5511 reg = HNS3_GLOBAL_RESET_REG; 5512 reg_bit = HNS3_GLOBAL_RESET_BIT; 5513 break; 5514 case HNS3_FUNC_RESET: 5515 reg = HNS3_FUN_RST_ING; 5516 reg_bit = HNS3_FUN_RST_ING_B; 5517 break; 5518 case HNS3_FLR_RESET: 5519 default: 5520 hns3_err(hw, "Wait for unsupported reset level: %d", 5521 hw->reset.level); 5522 return true; 5523 } 5524 val = hns3_read_dev(hw, reg); 5525 if (hns3_get_bit(val, reg_bit)) 5526 return false; 5527 else 5528 return true; 5529 } 5530 5531 bool 5532 hns3_is_reset_pending(struct hns3_adapter *hns) 5533 { 5534 struct hns3_hw *hw = &hns->hw; 5535 enum hns3_reset_level reset; 5536 5537 /* 5538 * Check the registers to confirm whether there is reset pending. 5539 * Note: This check may lead to schedule reset task, but only primary 5540 * process can process the reset event. Therefore, limit the 5541 * checking under only primary process. 5542 */ 5543 if (rte_eal_process_type() == RTE_PROC_PRIMARY) 5544 hns3_check_event_cause(hns, NULL); 5545 5546 reset = hns3_get_reset_level(hns, &hw->reset.pending); 5547 if (reset != HNS3_NONE_RESET && hw->reset.level != HNS3_NONE_RESET && 5548 hw->reset.level < reset) { 5549 hns3_warn(hw, "High level reset %d is pending", reset); 5550 return true; 5551 } 5552 reset = hns3_get_reset_level(hns, &hw->reset.request); 5553 if (reset != HNS3_NONE_RESET && hw->reset.level != HNS3_NONE_RESET && 5554 hw->reset.level < reset) { 5555 hns3_warn(hw, "High level reset %d is request", reset); 5556 return true; 5557 } 5558 return false; 5559 } 5560 5561 static int 5562 hns3_wait_hardware_ready(struct hns3_adapter *hns) 5563 { 5564 struct hns3_hw *hw = &hns->hw; 5565 struct hns3_wait_data *wait_data = hw->reset.wait_data; 5566 struct timeval tv; 5567 5568 if (wait_data->result == HNS3_WAIT_SUCCESS) 5569 return 0; 5570 else if (wait_data->result == HNS3_WAIT_TIMEOUT) { 5571 hns3_clock_gettime(&tv); 5572 hns3_warn(hw, "Reset step4 hardware not ready after reset time=%ld.%.6ld", 5573 tv.tv_sec, tv.tv_usec); 5574 return -ETIME; 5575 } else if (wait_data->result == HNS3_WAIT_REQUEST) 5576 return -EAGAIN; 5577 5578 wait_data->hns = hns; 5579 wait_data->check_completion = is_pf_reset_done; 5580 wait_data->end_ms = (uint64_t)HNS3_RESET_WAIT_CNT * 5581 HNS3_RESET_WAIT_MS + hns3_clock_gettime_ms(); 5582 wait_data->interval = HNS3_RESET_WAIT_MS * USEC_PER_MSEC; 5583 wait_data->count = HNS3_RESET_WAIT_CNT; 5584 wait_data->result = HNS3_WAIT_REQUEST; 5585 rte_eal_alarm_set(wait_data->interval, hns3_wait_callback, wait_data); 5586 return -EAGAIN; 5587 } 5588 5589 static int 5590 hns3_func_reset_cmd(struct hns3_hw *hw, int func_id) 5591 { 5592 struct hns3_cmd_desc desc; 5593 struct hns3_reset_cmd *req = (struct hns3_reset_cmd *)desc.data; 5594 5595 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_CFG_RST_TRIGGER, false); 5596 hns3_set_bit(req->mac_func_reset, HNS3_CFG_RESET_FUNC_B, 1); 5597 req->fun_reset_vfid = func_id; 5598 5599 return hns3_cmd_send(hw, &desc, 1); 5600 } 5601 5602 static int 5603 hns3_imp_reset_cmd(struct hns3_hw *hw) 5604 { 5605 struct hns3_cmd_desc desc; 5606 5607 hns3_cmd_setup_basic_desc(&desc, 0xFFFE, false); 5608 desc.data[0] = 0xeedd; 5609 5610 return hns3_cmd_send(hw, &desc, 1); 5611 } 5612 5613 static void 5614 hns3_msix_process(struct hns3_adapter *hns, enum hns3_reset_level reset_level) 5615 { 5616 struct hns3_hw *hw = &hns->hw; 5617 struct timeval tv; 5618 uint32_t val; 5619 5620 hns3_clock_gettime(&tv); 5621 if (hns3_read_dev(hw, HNS3_GLOBAL_RESET_REG) || 5622 hns3_read_dev(hw, HNS3_FUN_RST_ING)) { 5623 hns3_warn(hw, "Don't process msix during resetting time=%ld.%.6ld", 5624 tv.tv_sec, tv.tv_usec); 5625 return; 5626 } 5627 5628 switch (reset_level) { 5629 case HNS3_IMP_RESET: 5630 hns3_imp_reset_cmd(hw); 5631 hns3_warn(hw, "IMP Reset requested time=%ld.%.6ld", 5632 tv.tv_sec, tv.tv_usec); 5633 break; 5634 case HNS3_GLOBAL_RESET: 5635 val = hns3_read_dev(hw, HNS3_GLOBAL_RESET_REG); 5636 hns3_set_bit(val, HNS3_GLOBAL_RESET_BIT, 1); 5637 hns3_write_dev(hw, HNS3_GLOBAL_RESET_REG, val); 5638 hns3_warn(hw, "Global Reset requested time=%ld.%.6ld", 5639 tv.tv_sec, tv.tv_usec); 5640 break; 5641 case HNS3_FUNC_RESET: 5642 hns3_warn(hw, "PF Reset requested time=%ld.%.6ld", 5643 tv.tv_sec, tv.tv_usec); 5644 /* schedule again to check later */ 5645 hns3_atomic_set_bit(HNS3_FUNC_RESET, &hw->reset.pending); 5646 hns3_schedule_reset(hns); 5647 break; 5648 default: 5649 hns3_warn(hw, "Unsupported reset level: %d", reset_level); 5650 return; 5651 } 5652 hns3_atomic_clear_bit(reset_level, &hw->reset.request); 5653 } 5654 5655 static enum hns3_reset_level 5656 hns3_get_reset_level(struct hns3_adapter *hns, uint64_t *levels) 5657 { 5658 struct hns3_hw *hw = &hns->hw; 5659 enum hns3_reset_level reset_level = HNS3_NONE_RESET; 5660 5661 /* Return the highest priority reset level amongst all */ 5662 if (hns3_atomic_test_bit(HNS3_IMP_RESET, levels)) 5663 reset_level = HNS3_IMP_RESET; 5664 else if (hns3_atomic_test_bit(HNS3_GLOBAL_RESET, levels)) 5665 reset_level = HNS3_GLOBAL_RESET; 5666 else if (hns3_atomic_test_bit(HNS3_FUNC_RESET, levels)) 5667 reset_level = HNS3_FUNC_RESET; 5668 else if (hns3_atomic_test_bit(HNS3_FLR_RESET, levels)) 5669 reset_level = HNS3_FLR_RESET; 5670 5671 if (hw->reset.level != HNS3_NONE_RESET && reset_level < hw->reset.level) 5672 return HNS3_NONE_RESET; 5673 5674 return reset_level; 5675 } 5676 5677 static void 5678 hns3_record_imp_error(struct hns3_adapter *hns) 5679 { 5680 struct hns3_hw *hw = &hns->hw; 5681 uint32_t reg_val; 5682 5683 reg_val = hns3_read_dev(hw, HNS3_VECTOR0_OTER_EN_REG); 5684 if (hns3_get_bit(reg_val, HNS3_VECTOR0_IMP_RD_POISON_B)) { 5685 hns3_warn(hw, "Detected IMP RD poison!"); 5686 hns3_set_bit(reg_val, HNS3_VECTOR0_IMP_RD_POISON_B, 0); 5687 hns3_write_dev(hw, HNS3_VECTOR0_OTER_EN_REG, reg_val); 5688 } 5689 5690 if (hns3_get_bit(reg_val, HNS3_VECTOR0_IMP_CMDQ_ERR_B)) { 5691 hns3_warn(hw, "Detected IMP CMDQ error!"); 5692 hns3_set_bit(reg_val, HNS3_VECTOR0_IMP_CMDQ_ERR_B, 0); 5693 hns3_write_dev(hw, HNS3_VECTOR0_OTER_EN_REG, reg_val); 5694 } 5695 } 5696 5697 static int 5698 hns3_prepare_reset(struct hns3_adapter *hns) 5699 { 5700 struct hns3_hw *hw = &hns->hw; 5701 uint32_t reg_val; 5702 int ret; 5703 5704 switch (hw->reset.level) { 5705 case HNS3_FUNC_RESET: 5706 ret = hns3_func_reset_cmd(hw, HNS3_PF_FUNC_ID); 5707 if (ret) 5708 return ret; 5709 5710 /* 5711 * After performaning pf reset, it is not necessary to do the 5712 * mailbox handling or send any command to firmware, because 5713 * any mailbox handling or command to firmware is only valid 5714 * after hns3_cmd_init is called. 5715 */ 5716 __atomic_store_n(&hw->reset.disable_cmd, 1, __ATOMIC_RELAXED); 5717 hw->reset.stats.request_cnt++; 5718 break; 5719 case HNS3_IMP_RESET: 5720 hns3_record_imp_error(hns); 5721 reg_val = hns3_read_dev(hw, HNS3_VECTOR0_OTER_EN_REG); 5722 hns3_write_dev(hw, HNS3_VECTOR0_OTER_EN_REG, reg_val | 5723 BIT(HNS3_VECTOR0_IMP_RESET_INT_B)); 5724 break; 5725 default: 5726 break; 5727 } 5728 return 0; 5729 } 5730 5731 static int 5732 hns3_set_rst_done(struct hns3_hw *hw) 5733 { 5734 struct hns3_pf_rst_done_cmd *req; 5735 struct hns3_cmd_desc desc; 5736 5737 req = (struct hns3_pf_rst_done_cmd *)desc.data; 5738 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_PF_RST_DONE, false); 5739 req->pf_rst_done |= HNS3_PF_RESET_DONE_BIT; 5740 return hns3_cmd_send(hw, &desc, 1); 5741 } 5742 5743 static int 5744 hns3_stop_service(struct hns3_adapter *hns) 5745 { 5746 struct hns3_hw *hw = &hns->hw; 5747 struct rte_eth_dev *eth_dev; 5748 5749 eth_dev = &rte_eth_devices[hw->data->port_id]; 5750 hw->mac.link_status = RTE_ETH_LINK_DOWN; 5751 if (hw->adapter_state == HNS3_NIC_STARTED) { 5752 rte_eal_alarm_cancel(hns3_service_handler, eth_dev); 5753 hns3_update_linkstatus_and_event(hw, false); 5754 } 5755 5756 hns3_set_rxtx_function(eth_dev); 5757 rte_wmb(); 5758 /* Disable datapath on secondary process. */ 5759 hns3_mp_req_stop_rxtx(eth_dev); 5760 rte_delay_ms(hw->cfg_max_queues); 5761 5762 rte_spinlock_lock(&hw->lock); 5763 if (hns->hw.adapter_state == HNS3_NIC_STARTED || 5764 hw->adapter_state == HNS3_NIC_STOPPING) { 5765 hns3_enable_all_queues(hw, false); 5766 hns3_do_stop(hns); 5767 hw->reset.mbuf_deferred_free = true; 5768 } else 5769 hw->reset.mbuf_deferred_free = false; 5770 5771 /* 5772 * It is cumbersome for hardware to pick-and-choose entries for deletion 5773 * from table space. Hence, for function reset software intervention is 5774 * required to delete the entries 5775 */ 5776 if (__atomic_load_n(&hw->reset.disable_cmd, __ATOMIC_RELAXED) == 0) 5777 hns3_configure_all_mc_mac_addr(hns, true); 5778 rte_spinlock_unlock(&hw->lock); 5779 5780 return 0; 5781 } 5782 5783 static int 5784 hns3_start_service(struct hns3_adapter *hns) 5785 { 5786 struct hns3_hw *hw = &hns->hw; 5787 struct rte_eth_dev *eth_dev; 5788 5789 if (hw->reset.level == HNS3_IMP_RESET || 5790 hw->reset.level == HNS3_GLOBAL_RESET) 5791 hns3_set_rst_done(hw); 5792 eth_dev = &rte_eth_devices[hw->data->port_id]; 5793 hns3_set_rxtx_function(eth_dev); 5794 hns3_mp_req_start_rxtx(eth_dev); 5795 if (hw->adapter_state == HNS3_NIC_STARTED) { 5796 /* 5797 * This API parent function already hold the hns3_hw.lock, the 5798 * hns3_service_handler may report lse, in bonding application 5799 * it will call driver's ops which may acquire the hns3_hw.lock 5800 * again, thus lead to deadlock. 5801 * We defer calls hns3_service_handler to avoid the deadlock. 5802 */ 5803 rte_eal_alarm_set(HNS3_SERVICE_QUICK_INTERVAL, 5804 hns3_service_handler, eth_dev); 5805 5806 /* Enable interrupt of all rx queues before enabling queues */ 5807 hns3_dev_all_rx_queue_intr_enable(hw, true); 5808 /* 5809 * Enable state of each rxq and txq will be recovered after 5810 * reset, so we need to restore them before enable all tqps; 5811 */ 5812 hns3_restore_tqp_enable_state(hw); 5813 /* 5814 * When finished the initialization, enable queues to receive 5815 * and transmit packets. 5816 */ 5817 hns3_enable_all_queues(hw, true); 5818 } 5819 5820 return 0; 5821 } 5822 5823 static int 5824 hns3_restore_conf(struct hns3_adapter *hns) 5825 { 5826 struct hns3_hw *hw = &hns->hw; 5827 int ret; 5828 5829 ret = hns3_configure_all_mac_addr(hns, false); 5830 if (ret) 5831 return ret; 5832 5833 ret = hns3_configure_all_mc_mac_addr(hns, false); 5834 if (ret) 5835 goto err_mc_mac; 5836 5837 ret = hns3_dev_promisc_restore(hns); 5838 if (ret) 5839 goto err_promisc; 5840 5841 ret = hns3_restore_vlan_table(hns); 5842 if (ret) 5843 goto err_promisc; 5844 5845 ret = hns3_restore_vlan_conf(hns); 5846 if (ret) 5847 goto err_promisc; 5848 5849 ret = hns3_restore_ptp(hns); 5850 if (ret) 5851 goto err_promisc; 5852 5853 ret = hns3_restore_rx_interrupt(hw); 5854 if (ret) 5855 goto err_promisc; 5856 5857 ret = hns3_restore_gro_conf(hw); 5858 if (ret) 5859 goto err_promisc; 5860 5861 ret = hns3_restore_fec(hw); 5862 if (ret) 5863 goto err_promisc; 5864 5865 if (hns->hw.adapter_state == HNS3_NIC_STARTED) { 5866 ret = hns3_do_start(hns, false); 5867 if (ret) 5868 goto err_promisc; 5869 hns3_info(hw, "hns3 dev restart successful!"); 5870 } else if (hw->adapter_state == HNS3_NIC_STOPPING) 5871 hw->adapter_state = HNS3_NIC_CONFIGURED; 5872 return 0; 5873 5874 err_promisc: 5875 hns3_configure_all_mc_mac_addr(hns, true); 5876 err_mc_mac: 5877 hns3_configure_all_mac_addr(hns, true); 5878 return ret; 5879 } 5880 5881 static void 5882 hns3_reset_service(void *param) 5883 { 5884 struct hns3_adapter *hns = (struct hns3_adapter *)param; 5885 struct hns3_hw *hw = &hns->hw; 5886 enum hns3_reset_level reset_level; 5887 struct timeval tv_delta; 5888 struct timeval tv_start; 5889 struct timeval tv; 5890 uint64_t msec; 5891 int ret; 5892 5893 /* 5894 * The interrupt is not triggered within the delay time. 5895 * The interrupt may have been lost. It is necessary to handle 5896 * the interrupt to recover from the error. 5897 */ 5898 if (__atomic_load_n(&hw->reset.schedule, __ATOMIC_RELAXED) == 5899 SCHEDULE_DEFERRED) { 5900 __atomic_store_n(&hw->reset.schedule, SCHEDULE_REQUESTED, 5901 __ATOMIC_RELAXED); 5902 hns3_err(hw, "Handling interrupts in delayed tasks"); 5903 hns3_interrupt_handler(&rte_eth_devices[hw->data->port_id]); 5904 reset_level = hns3_get_reset_level(hns, &hw->reset.pending); 5905 if (reset_level == HNS3_NONE_RESET) { 5906 hns3_err(hw, "No reset level is set, try IMP reset"); 5907 hns3_atomic_set_bit(HNS3_IMP_RESET, &hw->reset.pending); 5908 } 5909 } 5910 __atomic_store_n(&hw->reset.schedule, SCHEDULE_NONE, __ATOMIC_RELAXED); 5911 5912 /* 5913 * Check if there is any ongoing reset in the hardware. This status can 5914 * be checked from reset_pending. If there is then, we need to wait for 5915 * hardware to complete reset. 5916 * a. If we are able to figure out in reasonable time that hardware 5917 * has fully resetted then, we can proceed with driver, client 5918 * reset. 5919 * b. else, we can come back later to check this status so re-sched 5920 * now. 5921 */ 5922 reset_level = hns3_get_reset_level(hns, &hw->reset.pending); 5923 if (reset_level != HNS3_NONE_RESET) { 5924 hns3_clock_gettime(&tv_start); 5925 ret = hns3_reset_process(hns, reset_level); 5926 hns3_clock_gettime(&tv); 5927 timersub(&tv, &tv_start, &tv_delta); 5928 msec = hns3_clock_calctime_ms(&tv_delta); 5929 if (msec > HNS3_RESET_PROCESS_MS) 5930 hns3_err(hw, "%d handle long time delta %" PRIu64 " ms time=%ld.%.6ld", 5931 hw->reset.level, msec, 5932 tv.tv_sec, tv.tv_usec); 5933 if (ret == -EAGAIN) 5934 return; 5935 } 5936 5937 /* Check if we got any *new* reset requests to be honored */ 5938 reset_level = hns3_get_reset_level(hns, &hw->reset.request); 5939 if (reset_level != HNS3_NONE_RESET) 5940 hns3_msix_process(hns, reset_level); 5941 } 5942 5943 static unsigned int 5944 hns3_get_speed_capa_num(uint16_t device_id) 5945 { 5946 unsigned int num; 5947 5948 switch (device_id) { 5949 case HNS3_DEV_ID_25GE: 5950 case HNS3_DEV_ID_25GE_RDMA: 5951 num = 2; 5952 break; 5953 case HNS3_DEV_ID_100G_RDMA_MACSEC: 5954 case HNS3_DEV_ID_200G_RDMA: 5955 num = 1; 5956 break; 5957 default: 5958 num = 0; 5959 break; 5960 } 5961 5962 return num; 5963 } 5964 5965 static int 5966 hns3_get_speed_fec_capa(struct rte_eth_fec_capa *speed_fec_capa, 5967 uint16_t device_id) 5968 { 5969 switch (device_id) { 5970 case HNS3_DEV_ID_25GE: 5971 /* fallthrough */ 5972 case HNS3_DEV_ID_25GE_RDMA: 5973 speed_fec_capa[0].speed = speed_fec_capa_tbl[1].speed; 5974 speed_fec_capa[0].capa = speed_fec_capa_tbl[1].capa; 5975 5976 /* In HNS3 device, the 25G NIC is compatible with 10G rate */ 5977 speed_fec_capa[1].speed = speed_fec_capa_tbl[0].speed; 5978 speed_fec_capa[1].capa = speed_fec_capa_tbl[0].capa; 5979 break; 5980 case HNS3_DEV_ID_100G_RDMA_MACSEC: 5981 speed_fec_capa[0].speed = speed_fec_capa_tbl[4].speed; 5982 speed_fec_capa[0].capa = speed_fec_capa_tbl[4].capa; 5983 break; 5984 case HNS3_DEV_ID_200G_RDMA: 5985 speed_fec_capa[0].speed = speed_fec_capa_tbl[5].speed; 5986 speed_fec_capa[0].capa = speed_fec_capa_tbl[5].capa; 5987 break; 5988 default: 5989 return -ENOTSUP; 5990 } 5991 5992 return 0; 5993 } 5994 5995 static int 5996 hns3_fec_get_capability(struct rte_eth_dev *dev, 5997 struct rte_eth_fec_capa *speed_fec_capa, 5998 unsigned int num) 5999 { 6000 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private); 6001 struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev); 6002 uint16_t device_id = pci_dev->id.device_id; 6003 unsigned int capa_num; 6004 int ret; 6005 6006 capa_num = hns3_get_speed_capa_num(device_id); 6007 if (capa_num == 0) { 6008 hns3_err(hw, "device(0x%x) is not supported by hns3 PMD", 6009 device_id); 6010 return -ENOTSUP; 6011 } 6012 6013 if (speed_fec_capa == NULL || num < capa_num) 6014 return capa_num; 6015 6016 ret = hns3_get_speed_fec_capa(speed_fec_capa, device_id); 6017 if (ret) 6018 return -ENOTSUP; 6019 6020 return capa_num; 6021 } 6022 6023 static int 6024 get_current_fec_auto_state(struct hns3_hw *hw, uint8_t *state) 6025 { 6026 struct hns3_config_fec_cmd *req; 6027 struct hns3_cmd_desc desc; 6028 int ret; 6029 6030 /* 6031 * CMD(HNS3_OPC_CONFIG_FEC_MODE) read is not supported 6032 * in device of link speed 6033 * below 10 Gbps. 6034 */ 6035 if (hw->mac.link_speed < RTE_ETH_SPEED_NUM_10G) { 6036 *state = 0; 6037 return 0; 6038 } 6039 6040 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_CONFIG_FEC_MODE, true); 6041 req = (struct hns3_config_fec_cmd *)desc.data; 6042 ret = hns3_cmd_send(hw, &desc, 1); 6043 if (ret) { 6044 hns3_err(hw, "get current fec auto state failed, ret = %d", 6045 ret); 6046 return ret; 6047 } 6048 6049 *state = req->fec_mode & (1U << HNS3_MAC_CFG_FEC_AUTO_EN_B); 6050 return 0; 6051 } 6052 6053 static int 6054 hns3_fec_get_internal(struct hns3_hw *hw, uint32_t *fec_capa) 6055 { 6056 struct hns3_sfp_info_cmd *resp; 6057 uint32_t tmp_fec_capa; 6058 uint8_t auto_state; 6059 struct hns3_cmd_desc desc; 6060 int ret; 6061 6062 /* 6063 * If link is down and AUTO is enabled, AUTO is returned, otherwise, 6064 * configured FEC mode is returned. 6065 * If link is up, current FEC mode is returned. 6066 */ 6067 if (hw->mac.link_status == RTE_ETH_LINK_DOWN) { 6068 ret = get_current_fec_auto_state(hw, &auto_state); 6069 if (ret) 6070 return ret; 6071 6072 if (auto_state == 0x1) { 6073 *fec_capa = RTE_ETH_FEC_MODE_CAPA_MASK(AUTO); 6074 return 0; 6075 } 6076 } 6077 6078 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_GET_SFP_INFO, true); 6079 resp = (struct hns3_sfp_info_cmd *)desc.data; 6080 resp->query_type = HNS3_ACTIVE_QUERY; 6081 6082 ret = hns3_cmd_send(hw, &desc, 1); 6083 if (ret == -EOPNOTSUPP) { 6084 hns3_err(hw, "IMP do not support get FEC, ret = %d", ret); 6085 return ret; 6086 } else if (ret) { 6087 hns3_err(hw, "get FEC failed, ret = %d", ret); 6088 return ret; 6089 } 6090 6091 /* 6092 * FEC mode order defined in hns3 hardware is inconsistent with 6093 * that defined in the ethdev library. So the sequence needs 6094 * to be converted. 6095 */ 6096 switch (resp->active_fec) { 6097 case HNS3_HW_FEC_MODE_NOFEC: 6098 tmp_fec_capa = RTE_ETH_FEC_MODE_CAPA_MASK(NOFEC); 6099 break; 6100 case HNS3_HW_FEC_MODE_BASER: 6101 tmp_fec_capa = RTE_ETH_FEC_MODE_CAPA_MASK(BASER); 6102 break; 6103 case HNS3_HW_FEC_MODE_RS: 6104 tmp_fec_capa = RTE_ETH_FEC_MODE_CAPA_MASK(RS); 6105 break; 6106 default: 6107 tmp_fec_capa = RTE_ETH_FEC_MODE_CAPA_MASK(NOFEC); 6108 break; 6109 } 6110 6111 *fec_capa = tmp_fec_capa; 6112 return 0; 6113 } 6114 6115 static int 6116 hns3_fec_get(struct rte_eth_dev *dev, uint32_t *fec_capa) 6117 { 6118 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private); 6119 6120 return hns3_fec_get_internal(hw, fec_capa); 6121 } 6122 6123 static int 6124 hns3_set_fec_hw(struct hns3_hw *hw, uint32_t mode) 6125 { 6126 struct hns3_config_fec_cmd *req; 6127 struct hns3_cmd_desc desc; 6128 int ret; 6129 6130 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_CONFIG_FEC_MODE, false); 6131 6132 req = (struct hns3_config_fec_cmd *)desc.data; 6133 switch (mode) { 6134 case RTE_ETH_FEC_MODE_CAPA_MASK(NOFEC): 6135 hns3_set_field(req->fec_mode, HNS3_MAC_CFG_FEC_MODE_M, 6136 HNS3_MAC_CFG_FEC_MODE_S, HNS3_MAC_FEC_OFF); 6137 break; 6138 case RTE_ETH_FEC_MODE_CAPA_MASK(BASER): 6139 hns3_set_field(req->fec_mode, HNS3_MAC_CFG_FEC_MODE_M, 6140 HNS3_MAC_CFG_FEC_MODE_S, HNS3_MAC_FEC_BASER); 6141 break; 6142 case RTE_ETH_FEC_MODE_CAPA_MASK(RS): 6143 hns3_set_field(req->fec_mode, HNS3_MAC_CFG_FEC_MODE_M, 6144 HNS3_MAC_CFG_FEC_MODE_S, HNS3_MAC_FEC_RS); 6145 break; 6146 case RTE_ETH_FEC_MODE_CAPA_MASK(AUTO): 6147 hns3_set_bit(req->fec_mode, HNS3_MAC_CFG_FEC_AUTO_EN_B, 1); 6148 break; 6149 default: 6150 return 0; 6151 } 6152 ret = hns3_cmd_send(hw, &desc, 1); 6153 if (ret) 6154 hns3_err(hw, "set fec mode failed, ret = %d", ret); 6155 6156 return ret; 6157 } 6158 6159 static uint32_t 6160 get_current_speed_fec_cap(struct hns3_hw *hw, struct rte_eth_fec_capa *fec_capa) 6161 { 6162 struct hns3_mac *mac = &hw->mac; 6163 uint32_t cur_capa; 6164 6165 switch (mac->link_speed) { 6166 case RTE_ETH_SPEED_NUM_10G: 6167 cur_capa = fec_capa[1].capa; 6168 break; 6169 case RTE_ETH_SPEED_NUM_25G: 6170 case RTE_ETH_SPEED_NUM_100G: 6171 case RTE_ETH_SPEED_NUM_200G: 6172 cur_capa = fec_capa[0].capa; 6173 break; 6174 default: 6175 cur_capa = 0; 6176 break; 6177 } 6178 6179 return cur_capa; 6180 } 6181 6182 static bool 6183 is_fec_mode_one_bit_set(uint32_t mode) 6184 { 6185 int cnt = 0; 6186 uint8_t i; 6187 6188 for (i = 0; i < sizeof(mode); i++) 6189 if (mode >> i & 0x1) 6190 cnt++; 6191 6192 return cnt == 1 ? true : false; 6193 } 6194 6195 static int 6196 hns3_fec_set(struct rte_eth_dev *dev, uint32_t mode) 6197 { 6198 #define FEC_CAPA_NUM 2 6199 struct hns3_adapter *hns = dev->data->dev_private; 6200 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(hns); 6201 struct hns3_pf *pf = &hns->pf; 6202 struct rte_eth_fec_capa fec_capa[FEC_CAPA_NUM]; 6203 uint32_t cur_capa; 6204 uint32_t num = FEC_CAPA_NUM; 6205 int ret; 6206 6207 ret = hns3_fec_get_capability(dev, fec_capa, num); 6208 if (ret < 0) 6209 return ret; 6210 6211 /* HNS3 PMD only support one bit set mode, e.g. 0x1, 0x4 */ 6212 if (!is_fec_mode_one_bit_set(mode)) { 6213 hns3_err(hw, "FEC mode(0x%x) not supported in HNS3 PMD, " 6214 "FEC mode should be only one bit set", mode); 6215 return -EINVAL; 6216 } 6217 6218 /* 6219 * Check whether the configured mode is within the FEC capability. 6220 * If not, the configured mode will not be supported. 6221 */ 6222 cur_capa = get_current_speed_fec_cap(hw, fec_capa); 6223 if (!(cur_capa & mode)) { 6224 hns3_err(hw, "unsupported FEC mode = 0x%x", mode); 6225 return -EINVAL; 6226 } 6227 6228 rte_spinlock_lock(&hw->lock); 6229 ret = hns3_set_fec_hw(hw, mode); 6230 if (ret) { 6231 rte_spinlock_unlock(&hw->lock); 6232 return ret; 6233 } 6234 6235 pf->fec_mode = mode; 6236 rte_spinlock_unlock(&hw->lock); 6237 6238 return 0; 6239 } 6240 6241 static int 6242 hns3_restore_fec(struct hns3_hw *hw) 6243 { 6244 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw); 6245 struct hns3_pf *pf = &hns->pf; 6246 uint32_t mode = pf->fec_mode; 6247 int ret; 6248 6249 ret = hns3_set_fec_hw(hw, mode); 6250 if (ret) 6251 hns3_err(hw, "restore fec mode(0x%x) failed, ret = %d", 6252 mode, ret); 6253 6254 return ret; 6255 } 6256 6257 static int 6258 hns3_query_dev_fec_info(struct hns3_hw *hw) 6259 { 6260 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw); 6261 struct hns3_pf *pf = HNS3_DEV_PRIVATE_TO_PF(hns); 6262 int ret; 6263 6264 ret = hns3_fec_get_internal(hw, &pf->fec_mode); 6265 if (ret) 6266 hns3_err(hw, "query device FEC info failed, ret = %d", ret); 6267 6268 return ret; 6269 } 6270 6271 static bool 6272 hns3_optical_module_existed(struct hns3_hw *hw) 6273 { 6274 struct hns3_cmd_desc desc; 6275 bool existed; 6276 int ret; 6277 6278 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_GET_SFP_EXIST, true); 6279 ret = hns3_cmd_send(hw, &desc, 1); 6280 if (ret) { 6281 hns3_err(hw, 6282 "fail to get optical module exist state, ret = %d.\n", 6283 ret); 6284 return false; 6285 } 6286 existed = !!desc.data[0]; 6287 6288 return existed; 6289 } 6290 6291 static int 6292 hns3_get_module_eeprom_data(struct hns3_hw *hw, uint32_t offset, 6293 uint32_t len, uint8_t *data) 6294 { 6295 #define HNS3_SFP_INFO_CMD_NUM 6 6296 #define HNS3_SFP_INFO_MAX_LEN \ 6297 (HNS3_SFP_INFO_BD0_LEN + \ 6298 (HNS3_SFP_INFO_CMD_NUM - 1) * HNS3_SFP_INFO_BDX_LEN) 6299 struct hns3_cmd_desc desc[HNS3_SFP_INFO_CMD_NUM]; 6300 struct hns3_sfp_info_bd0_cmd *sfp_info_bd0; 6301 uint16_t read_len; 6302 uint16_t copy_len; 6303 int ret; 6304 int i; 6305 6306 for (i = 0; i < HNS3_SFP_INFO_CMD_NUM; i++) { 6307 hns3_cmd_setup_basic_desc(&desc[i], HNS3_OPC_GET_SFP_EEPROM, 6308 true); 6309 if (i < HNS3_SFP_INFO_CMD_NUM - 1) 6310 desc[i].flag |= rte_cpu_to_le_16(HNS3_CMD_FLAG_NEXT); 6311 } 6312 6313 sfp_info_bd0 = (struct hns3_sfp_info_bd0_cmd *)desc[0].data; 6314 sfp_info_bd0->offset = rte_cpu_to_le_16((uint16_t)offset); 6315 read_len = RTE_MIN(len, HNS3_SFP_INFO_MAX_LEN); 6316 sfp_info_bd0->read_len = rte_cpu_to_le_16((uint16_t)read_len); 6317 6318 ret = hns3_cmd_send(hw, desc, HNS3_SFP_INFO_CMD_NUM); 6319 if (ret) { 6320 hns3_err(hw, "fail to get module EEPROM info, ret = %d.\n", 6321 ret); 6322 return ret; 6323 } 6324 6325 /* The data format in BD0 is different with the others. */ 6326 copy_len = RTE_MIN(len, HNS3_SFP_INFO_BD0_LEN); 6327 memcpy(data, sfp_info_bd0->data, copy_len); 6328 read_len = copy_len; 6329 6330 for (i = 1; i < HNS3_SFP_INFO_CMD_NUM; i++) { 6331 if (read_len >= len) 6332 break; 6333 6334 copy_len = RTE_MIN(len - read_len, HNS3_SFP_INFO_BDX_LEN); 6335 memcpy(data + read_len, desc[i].data, copy_len); 6336 read_len += copy_len; 6337 } 6338 6339 return (int)read_len; 6340 } 6341 6342 static int 6343 hns3_get_module_eeprom(struct rte_eth_dev *dev, 6344 struct rte_dev_eeprom_info *info) 6345 { 6346 struct hns3_adapter *hns = dev->data->dev_private; 6347 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(hns); 6348 uint32_t offset = info->offset; 6349 uint32_t len = info->length; 6350 uint8_t *data = info->data; 6351 uint32_t read_len = 0; 6352 6353 if (hw->mac.media_type != HNS3_MEDIA_TYPE_FIBER) 6354 return -ENOTSUP; 6355 6356 if (!hns3_optical_module_existed(hw)) { 6357 hns3_err(hw, "fail to read module EEPROM: no module is connected.\n"); 6358 return -EIO; 6359 } 6360 6361 while (read_len < len) { 6362 int ret; 6363 ret = hns3_get_module_eeprom_data(hw, offset + read_len, 6364 len - read_len, 6365 data + read_len); 6366 if (ret < 0) 6367 return -EIO; 6368 read_len += ret; 6369 } 6370 6371 return 0; 6372 } 6373 6374 static int 6375 hns3_get_module_info(struct rte_eth_dev *dev, 6376 struct rte_eth_dev_module_info *modinfo) 6377 { 6378 #define HNS3_SFF8024_ID_SFP 0x03 6379 #define HNS3_SFF8024_ID_QSFP_8438 0x0c 6380 #define HNS3_SFF8024_ID_QSFP_8436_8636 0x0d 6381 #define HNS3_SFF8024_ID_QSFP28_8636 0x11 6382 #define HNS3_SFF_8636_V1_3 0x03 6383 struct hns3_adapter *hns = dev->data->dev_private; 6384 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(hns); 6385 struct rte_dev_eeprom_info info; 6386 struct hns3_sfp_type sfp_type; 6387 int ret; 6388 6389 memset(&sfp_type, 0, sizeof(sfp_type)); 6390 memset(&info, 0, sizeof(info)); 6391 info.data = (uint8_t *)&sfp_type; 6392 info.length = sizeof(sfp_type); 6393 ret = hns3_get_module_eeprom(dev, &info); 6394 if (ret) 6395 return ret; 6396 6397 switch (sfp_type.type) { 6398 case HNS3_SFF8024_ID_SFP: 6399 modinfo->type = RTE_ETH_MODULE_SFF_8472; 6400 modinfo->eeprom_len = RTE_ETH_MODULE_SFF_8472_LEN; 6401 break; 6402 case HNS3_SFF8024_ID_QSFP_8438: 6403 modinfo->type = RTE_ETH_MODULE_SFF_8436; 6404 modinfo->eeprom_len = RTE_ETH_MODULE_SFF_8436_MAX_LEN; 6405 break; 6406 case HNS3_SFF8024_ID_QSFP_8436_8636: 6407 if (sfp_type.ext_type < HNS3_SFF_8636_V1_3) { 6408 modinfo->type = RTE_ETH_MODULE_SFF_8436; 6409 modinfo->eeprom_len = RTE_ETH_MODULE_SFF_8436_MAX_LEN; 6410 } else { 6411 modinfo->type = RTE_ETH_MODULE_SFF_8636; 6412 modinfo->eeprom_len = RTE_ETH_MODULE_SFF_8636_MAX_LEN; 6413 } 6414 break; 6415 case HNS3_SFF8024_ID_QSFP28_8636: 6416 modinfo->type = RTE_ETH_MODULE_SFF_8636; 6417 modinfo->eeprom_len = RTE_ETH_MODULE_SFF_8636_MAX_LEN; 6418 break; 6419 default: 6420 hns3_err(hw, "unknown module, type = %u, extra_type = %u.\n", 6421 sfp_type.type, sfp_type.ext_type); 6422 return -EINVAL; 6423 } 6424 6425 return 0; 6426 } 6427 6428 static const struct eth_dev_ops hns3_eth_dev_ops = { 6429 .dev_configure = hns3_dev_configure, 6430 .dev_start = hns3_dev_start, 6431 .dev_stop = hns3_dev_stop, 6432 .dev_close = hns3_dev_close, 6433 .promiscuous_enable = hns3_dev_promiscuous_enable, 6434 .promiscuous_disable = hns3_dev_promiscuous_disable, 6435 .allmulticast_enable = hns3_dev_allmulticast_enable, 6436 .allmulticast_disable = hns3_dev_allmulticast_disable, 6437 .mtu_set = hns3_dev_mtu_set, 6438 .stats_get = hns3_stats_get, 6439 .stats_reset = hns3_stats_reset, 6440 .xstats_get = hns3_dev_xstats_get, 6441 .xstats_get_names = hns3_dev_xstats_get_names, 6442 .xstats_reset = hns3_dev_xstats_reset, 6443 .xstats_get_by_id = hns3_dev_xstats_get_by_id, 6444 .xstats_get_names_by_id = hns3_dev_xstats_get_names_by_id, 6445 .dev_infos_get = hns3_dev_infos_get, 6446 .fw_version_get = hns3_fw_version_get, 6447 .rx_queue_setup = hns3_rx_queue_setup, 6448 .tx_queue_setup = hns3_tx_queue_setup, 6449 .rx_queue_release = hns3_dev_rx_queue_release, 6450 .tx_queue_release = hns3_dev_tx_queue_release, 6451 .rx_queue_start = hns3_dev_rx_queue_start, 6452 .rx_queue_stop = hns3_dev_rx_queue_stop, 6453 .tx_queue_start = hns3_dev_tx_queue_start, 6454 .tx_queue_stop = hns3_dev_tx_queue_stop, 6455 .rx_queue_intr_enable = hns3_dev_rx_queue_intr_enable, 6456 .rx_queue_intr_disable = hns3_dev_rx_queue_intr_disable, 6457 .rxq_info_get = hns3_rxq_info_get, 6458 .txq_info_get = hns3_txq_info_get, 6459 .rx_burst_mode_get = hns3_rx_burst_mode_get, 6460 .tx_burst_mode_get = hns3_tx_burst_mode_get, 6461 .flow_ctrl_get = hns3_flow_ctrl_get, 6462 .flow_ctrl_set = hns3_flow_ctrl_set, 6463 .priority_flow_ctrl_set = hns3_priority_flow_ctrl_set, 6464 .mac_addr_add = hns3_add_mac_addr, 6465 .mac_addr_remove = hns3_remove_mac_addr, 6466 .mac_addr_set = hns3_set_default_mac_addr, 6467 .set_mc_addr_list = hns3_set_mc_mac_addr_list, 6468 .link_update = hns3_dev_link_update, 6469 .dev_set_link_up = hns3_dev_set_link_up, 6470 .dev_set_link_down = hns3_dev_set_link_down, 6471 .rss_hash_update = hns3_dev_rss_hash_update, 6472 .rss_hash_conf_get = hns3_dev_rss_hash_conf_get, 6473 .reta_update = hns3_dev_rss_reta_update, 6474 .reta_query = hns3_dev_rss_reta_query, 6475 .flow_ops_get = hns3_dev_flow_ops_get, 6476 .vlan_filter_set = hns3_vlan_filter_set, 6477 .vlan_tpid_set = hns3_vlan_tpid_set, 6478 .vlan_offload_set = hns3_vlan_offload_set, 6479 .vlan_pvid_set = hns3_vlan_pvid_set, 6480 .get_reg = hns3_get_regs, 6481 .get_module_info = hns3_get_module_info, 6482 .get_module_eeprom = hns3_get_module_eeprom, 6483 .get_dcb_info = hns3_get_dcb_info, 6484 .dev_supported_ptypes_get = hns3_dev_supported_ptypes_get, 6485 .fec_get_capability = hns3_fec_get_capability, 6486 .fec_get = hns3_fec_get, 6487 .fec_set = hns3_fec_set, 6488 .tm_ops_get = hns3_tm_ops_get, 6489 .tx_done_cleanup = hns3_tx_done_cleanup, 6490 .timesync_enable = hns3_timesync_enable, 6491 .timesync_disable = hns3_timesync_disable, 6492 .timesync_read_rx_timestamp = hns3_timesync_read_rx_timestamp, 6493 .timesync_read_tx_timestamp = hns3_timesync_read_tx_timestamp, 6494 .timesync_adjust_time = hns3_timesync_adjust_time, 6495 .timesync_read_time = hns3_timesync_read_time, 6496 .timesync_write_time = hns3_timesync_write_time, 6497 .eth_dev_priv_dump = hns3_eth_dev_priv_dump, 6498 .eth_rx_descriptor_dump = hns3_rx_descriptor_dump, 6499 .eth_tx_descriptor_dump = hns3_tx_descriptor_dump, 6500 }; 6501 6502 static const struct hns3_reset_ops hns3_reset_ops = { 6503 .reset_service = hns3_reset_service, 6504 .stop_service = hns3_stop_service, 6505 .prepare_reset = hns3_prepare_reset, 6506 .wait_hardware_ready = hns3_wait_hardware_ready, 6507 .reinit_dev = hns3_reinit_dev, 6508 .restore_conf = hns3_restore_conf, 6509 .start_service = hns3_start_service, 6510 }; 6511 6512 static void 6513 hns3_init_hw_ops(struct hns3_hw *hw) 6514 { 6515 hw->ops.add_mc_mac_addr = hns3_add_mc_mac_addr; 6516 hw->ops.del_mc_mac_addr = hns3_remove_mc_mac_addr; 6517 hw->ops.add_uc_mac_addr = hns3_add_uc_mac_addr; 6518 hw->ops.del_uc_mac_addr = hns3_remove_uc_mac_addr; 6519 hw->ops.bind_ring_with_vector = hns3_bind_ring_with_vector; 6520 } 6521 6522 static int 6523 hns3_dev_init(struct rte_eth_dev *eth_dev) 6524 { 6525 struct hns3_adapter *hns = eth_dev->data->dev_private; 6526 struct hns3_hw *hw = &hns->hw; 6527 int ret; 6528 6529 PMD_INIT_FUNC_TRACE(); 6530 6531 hns3_flow_init(eth_dev); 6532 6533 hns3_set_rxtx_function(eth_dev); 6534 eth_dev->dev_ops = &hns3_eth_dev_ops; 6535 eth_dev->rx_queue_count = hns3_rx_queue_count; 6536 ret = hns3_mp_init(eth_dev); 6537 if (ret) 6538 goto err_mp_init; 6539 6540 if (rte_eal_process_type() != RTE_PROC_PRIMARY) { 6541 hns3_tx_push_init(eth_dev); 6542 return 0; 6543 } 6544 6545 hw->adapter_state = HNS3_NIC_UNINITIALIZED; 6546 hns->is_vf = false; 6547 hw->data = eth_dev->data; 6548 hns3_parse_devargs(eth_dev); 6549 6550 /* 6551 * Set default max packet size according to the mtu 6552 * default vale in DPDK frame. 6553 */ 6554 hns->pf.mps = hw->data->mtu + HNS3_ETH_OVERHEAD; 6555 6556 ret = hns3_reset_init(hw); 6557 if (ret) 6558 goto err_init_reset; 6559 hw->reset.ops = &hns3_reset_ops; 6560 6561 hns3_init_hw_ops(hw); 6562 ret = hns3_init_pf(eth_dev); 6563 if (ret) { 6564 PMD_INIT_LOG(ERR, "Failed to init pf: %d", ret); 6565 goto err_init_pf; 6566 } 6567 6568 ret = hns3_init_mac_addrs(eth_dev); 6569 if (ret != 0) 6570 goto err_init_mac_addrs; 6571 6572 hw->adapter_state = HNS3_NIC_INITIALIZED; 6573 6574 if (__atomic_load_n(&hw->reset.schedule, __ATOMIC_RELAXED) == 6575 SCHEDULE_PENDING) { 6576 hns3_err(hw, "Reschedule reset service after dev_init"); 6577 hns3_schedule_reset(hns); 6578 } else { 6579 /* IMP will wait ready flag before reset */ 6580 hns3_notify_reset_ready(hw, false); 6581 } 6582 6583 hns3_info(hw, "hns3 dev initialization successful!"); 6584 return 0; 6585 6586 err_init_mac_addrs: 6587 hns3_uninit_pf(eth_dev); 6588 6589 err_init_pf: 6590 rte_free(hw->reset.wait_data); 6591 6592 err_init_reset: 6593 hns3_mp_uninit(eth_dev); 6594 6595 err_mp_init: 6596 eth_dev->dev_ops = NULL; 6597 eth_dev->rx_pkt_burst = NULL; 6598 eth_dev->rx_descriptor_status = NULL; 6599 eth_dev->tx_pkt_burst = NULL; 6600 eth_dev->tx_pkt_prepare = NULL; 6601 eth_dev->tx_descriptor_status = NULL; 6602 return ret; 6603 } 6604 6605 static int 6606 hns3_dev_uninit(struct rte_eth_dev *eth_dev) 6607 { 6608 struct hns3_adapter *hns = eth_dev->data->dev_private; 6609 struct hns3_hw *hw = &hns->hw; 6610 6611 PMD_INIT_FUNC_TRACE(); 6612 6613 if (rte_eal_process_type() != RTE_PROC_PRIMARY) { 6614 hns3_mp_uninit(eth_dev); 6615 return 0; 6616 } 6617 6618 if (hw->adapter_state < HNS3_NIC_CLOSING) 6619 hns3_dev_close(eth_dev); 6620 6621 hw->adapter_state = HNS3_NIC_REMOVED; 6622 return 0; 6623 } 6624 6625 static int 6626 eth_hns3_pci_probe(struct rte_pci_driver *pci_drv __rte_unused, 6627 struct rte_pci_device *pci_dev) 6628 { 6629 return rte_eth_dev_pci_generic_probe(pci_dev, 6630 sizeof(struct hns3_adapter), 6631 hns3_dev_init); 6632 } 6633 6634 static int 6635 eth_hns3_pci_remove(struct rte_pci_device *pci_dev) 6636 { 6637 return rte_eth_dev_pci_generic_remove(pci_dev, hns3_dev_uninit); 6638 } 6639 6640 static const struct rte_pci_id pci_id_hns3_map[] = { 6641 { RTE_PCI_DEVICE(PCI_VENDOR_ID_HUAWEI, HNS3_DEV_ID_GE) }, 6642 { RTE_PCI_DEVICE(PCI_VENDOR_ID_HUAWEI, HNS3_DEV_ID_25GE) }, 6643 { RTE_PCI_DEVICE(PCI_VENDOR_ID_HUAWEI, HNS3_DEV_ID_25GE_RDMA) }, 6644 { RTE_PCI_DEVICE(PCI_VENDOR_ID_HUAWEI, HNS3_DEV_ID_50GE_RDMA) }, 6645 { RTE_PCI_DEVICE(PCI_VENDOR_ID_HUAWEI, HNS3_DEV_ID_100G_RDMA_MACSEC) }, 6646 { RTE_PCI_DEVICE(PCI_VENDOR_ID_HUAWEI, HNS3_DEV_ID_200G_RDMA) }, 6647 { .vendor_id = 0, }, /* sentinel */ 6648 }; 6649 6650 static struct rte_pci_driver rte_hns3_pmd = { 6651 .id_table = pci_id_hns3_map, 6652 .drv_flags = RTE_PCI_DRV_NEED_MAPPING | RTE_PCI_DRV_INTR_LSC, 6653 .probe = eth_hns3_pci_probe, 6654 .remove = eth_hns3_pci_remove, 6655 }; 6656 6657 RTE_PMD_REGISTER_PCI(net_hns3, rte_hns3_pmd); 6658 RTE_PMD_REGISTER_PCI_TABLE(net_hns3, pci_id_hns3_map); 6659 RTE_PMD_REGISTER_KMOD_DEP(net_hns3, "* igb_uio | vfio-pci"); 6660 RTE_PMD_REGISTER_PARAM_STRING(net_hns3, 6661 HNS3_DEVARG_RX_FUNC_HINT "=vec|sve|simple|common " 6662 HNS3_DEVARG_TX_FUNC_HINT "=vec|sve|simple|common " 6663 HNS3_DEVARG_DEV_CAPS_MASK "=<1-65535> " 6664 HNS3_DEVARG_MBX_TIME_LIMIT_MS "=<uint16> "); 6665 RTE_LOG_REGISTER_SUFFIX(hns3_logtype_init, init, NOTICE); 6666 RTE_LOG_REGISTER_SUFFIX(hns3_logtype_driver, driver, NOTICE); 6667