1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2018-2021 HiSilicon Limited.
6 #include <rte_bus_pci.h>
7 #include <ethdev_pci.h>
10 #include "hns3_ethdev.h"
11 #include "hns3_common.h"
12 #include "hns3_logs.h"
13 #include "hns3_rxtx.h"
14 #include "hns3_intr.h"
15 #include "hns3_regs.h"
19 #define HNS3_SERVICE_INTERVAL 1000000 /* us */
20 #define HNS3_SERVICE_QUICK_INTERVAL 10
21 #define HNS3_INVALID_PVID 0xFFFF
23 #define HNS3_FILTER_TYPE_VF 0
24 #define HNS3_FILTER_TYPE_PORT 1
25 #define HNS3_FILTER_FE_EGRESS_V1_B BIT(0)
26 #define HNS3_FILTER_FE_NIC_INGRESS_B BIT(0)
27 #define HNS3_FILTER_FE_NIC_EGRESS_B BIT(1)
28 #define HNS3_FILTER_FE_ROCE_INGRESS_B BIT(2)
29 #define HNS3_FILTER_FE_ROCE_EGRESS_B BIT(3)
30 #define HNS3_FILTER_FE_EGRESS (HNS3_FILTER_FE_NIC_EGRESS_B \
31 | HNS3_FILTER_FE_ROCE_EGRESS_B)
32 #define HNS3_FILTER_FE_INGRESS (HNS3_FILTER_FE_NIC_INGRESS_B \
33 | HNS3_FILTER_FE_ROCE_INGRESS_B)
35 /* Reset related Registers */
36 #define HNS3_GLOBAL_RESET_BIT 0
37 #define HNS3_CORE_RESET_BIT 1
38 #define HNS3_IMP_RESET_BIT 2
39 #define HNS3_FUN_RST_ING_B 0
41 #define HNS3_VECTOR0_IMP_RESET_INT_B 1
42 #define HNS3_VECTOR0_IMP_CMDQ_ERR_B 4U
43 #define HNS3_VECTOR0_IMP_RD_POISON_B 5U
44 #define HNS3_VECTOR0_ALL_MSIX_ERR_B 6U
46 #define HNS3_RESET_WAIT_MS 100
47 #define HNS3_RESET_WAIT_CNT 200
49 /* FEC mode order defined in HNS3 hardware */
50 #define HNS3_HW_FEC_MODE_NOFEC 0
51 #define HNS3_HW_FEC_MODE_BASER 1
52 #define HNS3_HW_FEC_MODE_RS 2
55 HNS3_VECTOR0_EVENT_RST,
56 HNS3_VECTOR0_EVENT_MBX,
57 HNS3_VECTOR0_EVENT_ERR,
58 HNS3_VECTOR0_EVENT_PTP,
59 HNS3_VECTOR0_EVENT_OTHER,
62 static const struct rte_eth_fec_capa speed_fec_capa_tbl[] = {
63 { RTE_ETH_SPEED_NUM_10G, RTE_ETH_FEC_MODE_CAPA_MASK(NOFEC) |
64 RTE_ETH_FEC_MODE_CAPA_MASK(AUTO) |
65 RTE_ETH_FEC_MODE_CAPA_MASK(BASER) },
67 { RTE_ETH_SPEED_NUM_25G, RTE_ETH_FEC_MODE_CAPA_MASK(NOFEC) |
68 RTE_ETH_FEC_MODE_CAPA_MASK(AUTO) |
69 RTE_ETH_FEC_MODE_CAPA_MASK(BASER) |
70 RTE_ETH_FEC_MODE_CAPA_MASK(RS) },
72 { RTE_ETH_SPEED_NUM_40G, RTE_ETH_FEC_MODE_CAPA_MASK(NOFEC) |
73 RTE_ETH_FEC_MODE_CAPA_MASK(AUTO) |
74 RTE_ETH_FEC_MODE_CAPA_MASK(BASER) },
76 { RTE_ETH_SPEED_NUM_50G, RTE_ETH_FEC_MODE_CAPA_MASK(NOFEC) |
77 RTE_ETH_FEC_MODE_CAPA_MASK(AUTO) |
78 RTE_ETH_FEC_MODE_CAPA_MASK(BASER) |
79 RTE_ETH_FEC_MODE_CAPA_MASK(RS) },
81 { RTE_ETH_SPEED_NUM_100G, RTE_ETH_FEC_MODE_CAPA_MASK(NOFEC) |
82 RTE_ETH_FEC_MODE_CAPA_MASK(AUTO) |
83 RTE_ETH_FEC_MODE_CAPA_MASK(RS) },
85 { RTE_ETH_SPEED_NUM_200G, RTE_ETH_FEC_MODE_CAPA_MASK(NOFEC) |
86 RTE_ETH_FEC_MODE_CAPA_MASK(AUTO) |
87 RTE_ETH_FEC_MODE_CAPA_MASK(RS) }
90 static enum hns3_reset_level hns3_get_reset_level(struct hns3_adapter *hns,
92 static int hns3_dev_mtu_set(struct rte_eth_dev *dev, uint16_t mtu);
93 static int hns3_vlan_pvid_configure(struct hns3_adapter *hns, uint16_t pvid,
95 static int hns3_update_link_info(struct rte_eth_dev *eth_dev);
96 static bool hns3_update_link_status(struct hns3_hw *hw);
98 static int hns3_add_mc_mac_addr(struct hns3_hw *hw,
99 struct rte_ether_addr *mac_addr);
100 static int hns3_remove_mc_mac_addr(struct hns3_hw *hw,
101 struct rte_ether_addr *mac_addr);
102 static int hns3_restore_fec(struct hns3_hw *hw);
103 static int hns3_query_dev_fec_info(struct hns3_hw *hw);
104 static int hns3_do_stop(struct hns3_adapter *hns);
105 static int hns3_check_port_speed(struct hns3_hw *hw, uint32_t link_speeds);
106 static int hns3_cfg_mac_mode(struct hns3_hw *hw, bool enable);
110 hns3_pf_disable_irq0(struct hns3_hw *hw)
112 hns3_write_dev(hw, HNS3_MISC_VECTOR_REG_BASE, 0);
116 hns3_pf_enable_irq0(struct hns3_hw *hw)
118 hns3_write_dev(hw, HNS3_MISC_VECTOR_REG_BASE, 1);
121 static enum hns3_evt_cause
122 hns3_proc_imp_reset_event(struct hns3_adapter *hns, bool is_delay,
125 struct hns3_hw *hw = &hns->hw;
127 __atomic_store_n(&hw->reset.disable_cmd, 1, __ATOMIC_RELAXED);
128 hns3_atomic_set_bit(HNS3_IMP_RESET, &hw->reset.pending);
129 *vec_val = BIT(HNS3_VECTOR0_IMPRESET_INT_B);
131 hw->reset.stats.imp_cnt++;
132 hns3_warn(hw, "IMP reset detected, clear reset status");
134 hns3_schedule_delayed_reset(hns);
135 hns3_warn(hw, "IMP reset detected, don't clear reset status");
138 return HNS3_VECTOR0_EVENT_RST;
141 static enum hns3_evt_cause
142 hns3_proc_global_reset_event(struct hns3_adapter *hns, bool is_delay,
145 struct hns3_hw *hw = &hns->hw;
147 __atomic_store_n(&hw->reset.disable_cmd, 1, __ATOMIC_RELAXED);
148 hns3_atomic_set_bit(HNS3_GLOBAL_RESET, &hw->reset.pending);
149 *vec_val = BIT(HNS3_VECTOR0_GLOBALRESET_INT_B);
151 hw->reset.stats.global_cnt++;
152 hns3_warn(hw, "Global reset detected, clear reset status");
154 hns3_schedule_delayed_reset(hns);
156 "Global reset detected, don't clear reset status");
159 return HNS3_VECTOR0_EVENT_RST;
162 static enum hns3_evt_cause
163 hns3_check_event_cause(struct hns3_adapter *hns, uint32_t *clearval)
165 struct hns3_hw *hw = &hns->hw;
166 uint32_t vector0_int_stats;
167 uint32_t cmdq_src_val;
168 uint32_t hw_err_src_reg;
170 enum hns3_evt_cause ret;
173 /* fetch the events from their corresponding regs */
174 vector0_int_stats = hns3_read_dev(hw, HNS3_VECTOR0_OTHER_INT_STS_REG);
175 cmdq_src_val = hns3_read_dev(hw, HNS3_VECTOR0_CMDQ_SRC_REG);
176 hw_err_src_reg = hns3_read_dev(hw, HNS3_RAS_PF_OTHER_INT_STS_REG);
178 is_delay = clearval == NULL ? true : false;
180 * Assumption: If by any chance reset and mailbox events are reported
181 * together then we will only process reset event and defer the
182 * processing of the mailbox events. Since, we would have not cleared
183 * RX CMDQ event this time we would receive again another interrupt
184 * from H/W just for the mailbox.
186 if (BIT(HNS3_VECTOR0_IMPRESET_INT_B) & vector0_int_stats) { /* IMP */
187 ret = hns3_proc_imp_reset_event(hns, is_delay, &val);
192 if (BIT(HNS3_VECTOR0_GLOBALRESET_INT_B) & vector0_int_stats) {
193 ret = hns3_proc_global_reset_event(hns, is_delay, &val);
197 /* Check for vector0 1588 event source */
198 if (BIT(HNS3_VECTOR0_1588_INT_B) & vector0_int_stats) {
199 val = BIT(HNS3_VECTOR0_1588_INT_B);
200 ret = HNS3_VECTOR0_EVENT_PTP;
204 /* check for vector0 msix event source */
205 if (vector0_int_stats & HNS3_VECTOR0_REG_MSIX_MASK ||
206 hw_err_src_reg & HNS3_RAS_REG_NFE_MASK) {
207 val = vector0_int_stats | hw_err_src_reg;
208 ret = HNS3_VECTOR0_EVENT_ERR;
212 /* check for vector0 mailbox(=CMDQ RX) event source */
213 if (BIT(HNS3_VECTOR0_RX_CMDQ_INT_B) & cmdq_src_val) {
214 cmdq_src_val &= ~BIT(HNS3_VECTOR0_RX_CMDQ_INT_B);
216 ret = HNS3_VECTOR0_EVENT_MBX;
220 val = vector0_int_stats;
221 ret = HNS3_VECTOR0_EVENT_OTHER;
230 hns3_is_1588_event_type(uint32_t event_type)
232 return (event_type == HNS3_VECTOR0_EVENT_PTP);
236 hns3_clear_event_cause(struct hns3_hw *hw, uint32_t event_type, uint32_t regclr)
238 if (event_type == HNS3_VECTOR0_EVENT_RST ||
239 hns3_is_1588_event_type(event_type))
240 hns3_write_dev(hw, HNS3_MISC_RESET_STS_REG, regclr);
241 else if (event_type == HNS3_VECTOR0_EVENT_MBX)
242 hns3_write_dev(hw, HNS3_VECTOR0_CMDQ_SRC_REG, regclr);
246 hns3_clear_all_event_cause(struct hns3_hw *hw)
248 uint32_t vector0_int_stats;
250 vector0_int_stats = hns3_read_dev(hw, HNS3_VECTOR0_OTHER_INT_STS_REG);
251 if (BIT(HNS3_VECTOR0_IMPRESET_INT_B) & vector0_int_stats)
252 hns3_warn(hw, "Probe during IMP reset interrupt");
254 if (BIT(HNS3_VECTOR0_GLOBALRESET_INT_B) & vector0_int_stats)
255 hns3_warn(hw, "Probe during Global reset interrupt");
257 hns3_clear_event_cause(hw, HNS3_VECTOR0_EVENT_RST,
258 BIT(HNS3_VECTOR0_IMPRESET_INT_B) |
259 BIT(HNS3_VECTOR0_GLOBALRESET_INT_B) |
260 BIT(HNS3_VECTOR0_CORERESET_INT_B));
261 hns3_clear_event_cause(hw, HNS3_VECTOR0_EVENT_MBX, 0);
262 hns3_clear_event_cause(hw, HNS3_VECTOR0_EVENT_PTP,
263 BIT(HNS3_VECTOR0_1588_INT_B));
267 hns3_handle_mac_tnl(struct hns3_hw *hw)
269 struct hns3_cmd_desc desc;
273 /* query and clear mac tnl interrupt */
274 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_QUERY_MAC_TNL_INT, true);
275 ret = hns3_cmd_send(hw, &desc, 1);
277 hns3_err(hw, "failed to query mac tnl int, ret = %d.", ret);
281 status = rte_le_to_cpu_32(desc.data[0]);
283 hns3_warn(hw, "mac tnl int occurs, status = 0x%x.", status);
284 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_CLEAR_MAC_TNL_INT,
286 desc.data[0] = rte_cpu_to_le_32(HNS3_MAC_TNL_INT_CLR);
287 ret = hns3_cmd_send(hw, &desc, 1);
289 hns3_err(hw, "failed to clear mac tnl int, ret = %d.",
295 hns3_interrupt_handler(void *param)
297 struct rte_eth_dev *dev = (struct rte_eth_dev *)param;
298 struct hns3_adapter *hns = dev->data->dev_private;
299 struct hns3_hw *hw = &hns->hw;
300 enum hns3_evt_cause event_cause;
301 uint32_t clearval = 0;
302 uint32_t vector0_int;
306 /* Disable interrupt */
307 hns3_pf_disable_irq0(hw);
309 event_cause = hns3_check_event_cause(hns, &clearval);
310 vector0_int = hns3_read_dev(hw, HNS3_VECTOR0_OTHER_INT_STS_REG);
311 ras_int = hns3_read_dev(hw, HNS3_RAS_PF_OTHER_INT_STS_REG);
312 cmdq_int = hns3_read_dev(hw, HNS3_VECTOR0_CMDQ_SRC_REG);
313 hns3_clear_event_cause(hw, event_cause, clearval);
314 /* vector 0 interrupt is shared with reset and mailbox source events. */
315 if (event_cause == HNS3_VECTOR0_EVENT_ERR) {
316 hns3_warn(hw, "received interrupt: vector0_int_stat:0x%x "
317 "ras_int_stat:0x%x cmdq_int_stat:0x%x",
318 vector0_int, ras_int, cmdq_int);
319 hns3_handle_mac_tnl(hw);
320 hns3_handle_error(hns);
321 } else if (event_cause == HNS3_VECTOR0_EVENT_RST) {
322 hns3_warn(hw, "received reset interrupt");
323 hns3_schedule_reset(hns);
324 } else if (event_cause == HNS3_VECTOR0_EVENT_MBX) {
325 hns3_dev_handle_mbx_msg(hw);
327 hns3_warn(hw, "received unknown event: vector0_int_stat:0x%x "
328 "ras_int_stat:0x%x cmdq_int_stat:0x%x",
329 vector0_int, ras_int, cmdq_int);
332 /* Enable interrupt if it is not cause by reset */
333 hns3_pf_enable_irq0(hw);
337 hns3_set_port_vlan_filter(struct hns3_adapter *hns, uint16_t vlan_id, int on)
339 #define HNS3_VLAN_ID_OFFSET_STEP 160
340 #define HNS3_VLAN_BYTE_SIZE 8
341 struct hns3_vlan_filter_pf_cfg_cmd *req;
342 struct hns3_hw *hw = &hns->hw;
343 uint8_t vlan_offset_byte_val;
344 struct hns3_cmd_desc desc;
345 uint8_t vlan_offset_byte;
346 uint8_t vlan_offset_base;
349 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_VLAN_FILTER_PF_CFG, false);
351 vlan_offset_base = vlan_id / HNS3_VLAN_ID_OFFSET_STEP;
352 vlan_offset_byte = (vlan_id % HNS3_VLAN_ID_OFFSET_STEP) /
354 vlan_offset_byte_val = 1 << (vlan_id % HNS3_VLAN_BYTE_SIZE);
356 req = (struct hns3_vlan_filter_pf_cfg_cmd *)desc.data;
357 req->vlan_offset = vlan_offset_base;
358 req->vlan_cfg = on ? 0 : 1;
359 req->vlan_offset_bitmap[vlan_offset_byte] = vlan_offset_byte_val;
361 ret = hns3_cmd_send(hw, &desc, 1);
363 hns3_err(hw, "set port vlan id failed, vlan_id =%u, ret =%d",
370 hns3_rm_dev_vlan_table(struct hns3_adapter *hns, uint16_t vlan_id)
372 struct hns3_user_vlan_table *vlan_entry;
373 struct hns3_pf *pf = &hns->pf;
375 LIST_FOREACH(vlan_entry, &pf->vlan_list, next) {
376 if (vlan_entry->vlan_id == vlan_id) {
377 if (vlan_entry->hd_tbl_status)
378 hns3_set_port_vlan_filter(hns, vlan_id, 0);
379 LIST_REMOVE(vlan_entry, next);
380 rte_free(vlan_entry);
387 hns3_add_dev_vlan_table(struct hns3_adapter *hns, uint16_t vlan_id,
390 struct hns3_user_vlan_table *vlan_entry;
391 struct hns3_hw *hw = &hns->hw;
392 struct hns3_pf *pf = &hns->pf;
394 LIST_FOREACH(vlan_entry, &pf->vlan_list, next) {
395 if (vlan_entry->vlan_id == vlan_id)
399 vlan_entry = rte_zmalloc("hns3_vlan_tbl", sizeof(*vlan_entry), 0);
400 if (vlan_entry == NULL) {
401 hns3_err(hw, "Failed to malloc hns3 vlan table");
405 vlan_entry->hd_tbl_status = writen_to_tbl;
406 vlan_entry->vlan_id = vlan_id;
408 LIST_INSERT_HEAD(&pf->vlan_list, vlan_entry, next);
412 hns3_restore_vlan_table(struct hns3_adapter *hns)
414 struct hns3_user_vlan_table *vlan_entry;
415 struct hns3_hw *hw = &hns->hw;
416 struct hns3_pf *pf = &hns->pf;
420 if (hw->port_base_vlan_cfg.state == HNS3_PORT_BASE_VLAN_ENABLE)
421 return hns3_vlan_pvid_configure(hns,
422 hw->port_base_vlan_cfg.pvid, 1);
424 LIST_FOREACH(vlan_entry, &pf->vlan_list, next) {
425 if (vlan_entry->hd_tbl_status) {
426 vlan_id = vlan_entry->vlan_id;
427 ret = hns3_set_port_vlan_filter(hns, vlan_id, 1);
437 hns3_vlan_filter_configure(struct hns3_adapter *hns, uint16_t vlan_id, int on)
439 struct hns3_hw *hw = &hns->hw;
440 bool writen_to_tbl = false;
444 * When vlan filter is enabled, hardware regards packets without vlan
445 * as packets with vlan 0. So, to receive packets without vlan, vlan id
446 * 0 is not allowed to be removed by rte_eth_dev_vlan_filter.
448 if (on == 0 && vlan_id == 0)
452 * When port base vlan enabled, we use port base vlan as the vlan
453 * filter condition. In this case, we don't update vlan filter table
454 * when user add new vlan or remove exist vlan, just update the
455 * vlan list. The vlan id in vlan list will be written in vlan filter
456 * table until port base vlan disabled
458 if (hw->port_base_vlan_cfg.state == HNS3_PORT_BASE_VLAN_DISABLE) {
459 ret = hns3_set_port_vlan_filter(hns, vlan_id, on);
460 writen_to_tbl = true;
465 hns3_add_dev_vlan_table(hns, vlan_id, writen_to_tbl);
467 hns3_rm_dev_vlan_table(hns, vlan_id);
473 hns3_vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on)
475 struct hns3_adapter *hns = dev->data->dev_private;
476 struct hns3_hw *hw = &hns->hw;
479 rte_spinlock_lock(&hw->lock);
480 ret = hns3_vlan_filter_configure(hns, vlan_id, on);
481 rte_spinlock_unlock(&hw->lock);
486 hns3_vlan_tpid_configure(struct hns3_adapter *hns, enum rte_vlan_type vlan_type,
489 struct hns3_rx_vlan_type_cfg_cmd *rx_req;
490 struct hns3_tx_vlan_type_cfg_cmd *tx_req;
491 struct hns3_hw *hw = &hns->hw;
492 struct hns3_cmd_desc desc;
495 if ((vlan_type != RTE_ETH_VLAN_TYPE_INNER &&
496 vlan_type != RTE_ETH_VLAN_TYPE_OUTER)) {
497 hns3_err(hw, "Unsupported vlan type, vlan_type =%d", vlan_type);
501 if (tpid != RTE_ETHER_TYPE_VLAN) {
502 hns3_err(hw, "Unsupported vlan tpid, vlan_type =%d", vlan_type);
506 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_MAC_VLAN_TYPE_ID, false);
507 rx_req = (struct hns3_rx_vlan_type_cfg_cmd *)desc.data;
509 if (vlan_type == RTE_ETH_VLAN_TYPE_OUTER) {
510 rx_req->ot_fst_vlan_type = rte_cpu_to_le_16(tpid);
511 rx_req->ot_sec_vlan_type = rte_cpu_to_le_16(tpid);
512 } else if (vlan_type == RTE_ETH_VLAN_TYPE_INNER) {
513 rx_req->ot_fst_vlan_type = rte_cpu_to_le_16(tpid);
514 rx_req->ot_sec_vlan_type = rte_cpu_to_le_16(tpid);
515 rx_req->in_fst_vlan_type = rte_cpu_to_le_16(tpid);
516 rx_req->in_sec_vlan_type = rte_cpu_to_le_16(tpid);
519 ret = hns3_cmd_send(hw, &desc, 1);
521 hns3_err(hw, "Send rxvlan protocol type command fail, ret =%d",
526 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_MAC_VLAN_INSERT, false);
528 tx_req = (struct hns3_tx_vlan_type_cfg_cmd *)desc.data;
529 tx_req->ot_vlan_type = rte_cpu_to_le_16(tpid);
530 tx_req->in_vlan_type = rte_cpu_to_le_16(tpid);
532 ret = hns3_cmd_send(hw, &desc, 1);
534 hns3_err(hw, "Send txvlan protocol type command fail, ret =%d",
540 hns3_vlan_tpid_set(struct rte_eth_dev *dev, enum rte_vlan_type vlan_type,
543 struct hns3_adapter *hns = dev->data->dev_private;
544 struct hns3_hw *hw = &hns->hw;
547 rte_spinlock_lock(&hw->lock);
548 ret = hns3_vlan_tpid_configure(hns, vlan_type, tpid);
549 rte_spinlock_unlock(&hw->lock);
554 hns3_set_vlan_rx_offload_cfg(struct hns3_adapter *hns,
555 struct hns3_rx_vtag_cfg *vcfg)
557 struct hns3_vport_vtag_rx_cfg_cmd *req;
558 struct hns3_hw *hw = &hns->hw;
559 struct hns3_cmd_desc desc;
564 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_VLAN_PORT_RX_CFG, false);
566 req = (struct hns3_vport_vtag_rx_cfg_cmd *)desc.data;
567 hns3_set_bit(req->vport_vlan_cfg, HNS3_REM_TAG1_EN_B,
568 vcfg->strip_tag1_en ? 1 : 0);
569 hns3_set_bit(req->vport_vlan_cfg, HNS3_REM_TAG2_EN_B,
570 vcfg->strip_tag2_en ? 1 : 0);
571 hns3_set_bit(req->vport_vlan_cfg, HNS3_SHOW_TAG1_EN_B,
572 vcfg->vlan1_vlan_prionly ? 1 : 0);
573 hns3_set_bit(req->vport_vlan_cfg, HNS3_SHOW_TAG2_EN_B,
574 vcfg->vlan2_vlan_prionly ? 1 : 0);
576 /* firmwall will ignore this configuration for PCI_REVISION_ID_HIP08 */
577 hns3_set_bit(req->vport_vlan_cfg, HNS3_DISCARD_TAG1_EN_B,
578 vcfg->strip_tag1_discard_en ? 1 : 0);
579 hns3_set_bit(req->vport_vlan_cfg, HNS3_DISCARD_TAG2_EN_B,
580 vcfg->strip_tag2_discard_en ? 1 : 0);
582 * In current version VF is not supported when PF is driven by DPDK
583 * driver, just need to configure parameters for PF vport.
585 vport_id = HNS3_PF_FUNC_ID;
586 req->vf_offset = vport_id / HNS3_VF_NUM_PER_CMD;
587 bitmap = 1 << (vport_id % HNS3_VF_NUM_PER_BYTE);
588 req->vf_bitmap[req->vf_offset] = bitmap;
590 ret = hns3_cmd_send(hw, &desc, 1);
592 hns3_err(hw, "Send port rxvlan cfg command fail, ret =%d", ret);
597 hns3_update_rx_offload_cfg(struct hns3_adapter *hns,
598 struct hns3_rx_vtag_cfg *vcfg)
600 struct hns3_pf *pf = &hns->pf;
601 memcpy(&pf->vtag_config.rx_vcfg, vcfg, sizeof(pf->vtag_config.rx_vcfg));
605 hns3_update_tx_offload_cfg(struct hns3_adapter *hns,
606 struct hns3_tx_vtag_cfg *vcfg)
608 struct hns3_pf *pf = &hns->pf;
609 memcpy(&pf->vtag_config.tx_vcfg, vcfg, sizeof(pf->vtag_config.tx_vcfg));
613 hns3_en_hw_strip_rxvtag(struct hns3_adapter *hns, bool enable)
615 struct hns3_rx_vtag_cfg rxvlan_cfg;
616 struct hns3_hw *hw = &hns->hw;
619 if (hw->port_base_vlan_cfg.state == HNS3_PORT_BASE_VLAN_DISABLE) {
620 rxvlan_cfg.strip_tag1_en = false;
621 rxvlan_cfg.strip_tag2_en = enable;
622 rxvlan_cfg.strip_tag2_discard_en = false;
624 rxvlan_cfg.strip_tag1_en = enable;
625 rxvlan_cfg.strip_tag2_en = true;
626 rxvlan_cfg.strip_tag2_discard_en = true;
629 rxvlan_cfg.strip_tag1_discard_en = false;
630 rxvlan_cfg.vlan1_vlan_prionly = false;
631 rxvlan_cfg.vlan2_vlan_prionly = false;
632 rxvlan_cfg.rx_vlan_offload_en = enable;
634 ret = hns3_set_vlan_rx_offload_cfg(hns, &rxvlan_cfg);
636 hns3_err(hw, "%s strip rx vtag failed, ret = %d.",
637 enable ? "enable" : "disable", ret);
641 hns3_update_rx_offload_cfg(hns, &rxvlan_cfg);
647 hns3_set_vlan_filter_ctrl(struct hns3_hw *hw, uint8_t vlan_type,
648 uint8_t fe_type, bool filter_en, uint8_t vf_id)
650 struct hns3_vlan_filter_ctrl_cmd *req;
651 struct hns3_cmd_desc desc;
654 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_VLAN_FILTER_CTRL, false);
656 req = (struct hns3_vlan_filter_ctrl_cmd *)desc.data;
657 req->vlan_type = vlan_type;
658 req->vlan_fe = filter_en ? fe_type : 0;
661 ret = hns3_cmd_send(hw, &desc, 1);
663 hns3_err(hw, "set vlan filter fail, ret =%d", ret);
669 hns3_vlan_filter_init(struct hns3_adapter *hns)
671 struct hns3_hw *hw = &hns->hw;
674 ret = hns3_set_vlan_filter_ctrl(hw, HNS3_FILTER_TYPE_VF,
675 HNS3_FILTER_FE_EGRESS, false,
678 hns3_err(hw, "failed to init vf vlan filter, ret = %d", ret);
682 ret = hns3_set_vlan_filter_ctrl(hw, HNS3_FILTER_TYPE_PORT,
683 HNS3_FILTER_FE_INGRESS, false,
686 hns3_err(hw, "failed to init port vlan filter, ret = %d", ret);
692 hns3_enable_vlan_filter(struct hns3_adapter *hns, bool enable)
694 struct hns3_hw *hw = &hns->hw;
697 ret = hns3_set_vlan_filter_ctrl(hw, HNS3_FILTER_TYPE_PORT,
698 HNS3_FILTER_FE_INGRESS, enable,
701 hns3_err(hw, "failed to %s port vlan filter, ret = %d",
702 enable ? "enable" : "disable", ret);
708 hns3_vlan_offload_set(struct rte_eth_dev *dev, int mask)
710 struct hns3_adapter *hns = dev->data->dev_private;
711 struct hns3_hw *hw = &hns->hw;
712 struct rte_eth_rxmode *rxmode;
713 unsigned int tmp_mask;
717 rte_spinlock_lock(&hw->lock);
718 rxmode = &dev->data->dev_conf.rxmode;
719 tmp_mask = (unsigned int)mask;
720 if (tmp_mask & RTE_ETH_VLAN_FILTER_MASK) {
721 /* ignore vlan filter configuration during promiscuous mode */
722 if (!dev->data->promiscuous) {
723 /* Enable or disable VLAN filter */
724 enable = rxmode->offloads & RTE_ETH_RX_OFFLOAD_VLAN_FILTER ?
727 ret = hns3_enable_vlan_filter(hns, enable);
729 rte_spinlock_unlock(&hw->lock);
730 hns3_err(hw, "failed to %s rx filter, ret = %d",
731 enable ? "enable" : "disable", ret);
737 if (tmp_mask & RTE_ETH_VLAN_STRIP_MASK) {
738 /* Enable or disable VLAN stripping */
739 enable = rxmode->offloads & RTE_ETH_RX_OFFLOAD_VLAN_STRIP ?
742 ret = hns3_en_hw_strip_rxvtag(hns, enable);
744 rte_spinlock_unlock(&hw->lock);
745 hns3_err(hw, "failed to %s rx strip, ret = %d",
746 enable ? "enable" : "disable", ret);
751 rte_spinlock_unlock(&hw->lock);
757 hns3_set_vlan_tx_offload_cfg(struct hns3_adapter *hns,
758 struct hns3_tx_vtag_cfg *vcfg)
760 struct hns3_vport_vtag_tx_cfg_cmd *req;
761 struct hns3_cmd_desc desc;
762 struct hns3_hw *hw = &hns->hw;
767 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_VLAN_PORT_TX_CFG, false);
769 req = (struct hns3_vport_vtag_tx_cfg_cmd *)desc.data;
770 req->def_vlan_tag1 = vcfg->default_tag1;
771 req->def_vlan_tag2 = vcfg->default_tag2;
772 hns3_set_bit(req->vport_vlan_cfg, HNS3_ACCEPT_TAG1_B,
773 vcfg->accept_tag1 ? 1 : 0);
774 hns3_set_bit(req->vport_vlan_cfg, HNS3_ACCEPT_UNTAG1_B,
775 vcfg->accept_untag1 ? 1 : 0);
776 hns3_set_bit(req->vport_vlan_cfg, HNS3_ACCEPT_TAG2_B,
777 vcfg->accept_tag2 ? 1 : 0);
778 hns3_set_bit(req->vport_vlan_cfg, HNS3_ACCEPT_UNTAG2_B,
779 vcfg->accept_untag2 ? 1 : 0);
780 hns3_set_bit(req->vport_vlan_cfg, HNS3_PORT_INS_TAG1_EN_B,
781 vcfg->insert_tag1_en ? 1 : 0);
782 hns3_set_bit(req->vport_vlan_cfg, HNS3_PORT_INS_TAG2_EN_B,
783 vcfg->insert_tag2_en ? 1 : 0);
784 hns3_set_bit(req->vport_vlan_cfg, HNS3_CFG_NIC_ROCE_SEL_B, 0);
786 /* firmwall will ignore this configuration for PCI_REVISION_ID_HIP08 */
787 hns3_set_bit(req->vport_vlan_cfg, HNS3_TAG_SHIFT_MODE_EN_B,
788 vcfg->tag_shift_mode_en ? 1 : 0);
791 * In current version VF is not supported when PF is driven by DPDK
792 * driver, just need to configure parameters for PF vport.
794 vport_id = HNS3_PF_FUNC_ID;
795 req->vf_offset = vport_id / HNS3_VF_NUM_PER_CMD;
796 bitmap = 1 << (vport_id % HNS3_VF_NUM_PER_BYTE);
797 req->vf_bitmap[req->vf_offset] = bitmap;
799 ret = hns3_cmd_send(hw, &desc, 1);
801 hns3_err(hw, "Send port txvlan cfg command fail, ret =%d", ret);
807 hns3_vlan_txvlan_cfg(struct hns3_adapter *hns, uint16_t port_base_vlan_state,
810 struct hns3_hw *hw = &hns->hw;
811 struct hns3_tx_vtag_cfg txvlan_cfg;
814 if (port_base_vlan_state == HNS3_PORT_BASE_VLAN_DISABLE) {
815 txvlan_cfg.accept_tag1 = true;
816 txvlan_cfg.insert_tag1_en = false;
817 txvlan_cfg.default_tag1 = 0;
819 txvlan_cfg.accept_tag1 =
820 hw->vlan_mode == HNS3_HW_SHIFT_AND_DISCARD_MODE;
821 txvlan_cfg.insert_tag1_en = true;
822 txvlan_cfg.default_tag1 = pvid;
825 txvlan_cfg.accept_untag1 = true;
826 txvlan_cfg.accept_tag2 = true;
827 txvlan_cfg.accept_untag2 = true;
828 txvlan_cfg.insert_tag2_en = false;
829 txvlan_cfg.default_tag2 = 0;
830 txvlan_cfg.tag_shift_mode_en = true;
832 ret = hns3_set_vlan_tx_offload_cfg(hns, &txvlan_cfg);
834 hns3_err(hw, "pf vlan set pvid failed, pvid =%u ,ret =%d", pvid,
839 hns3_update_tx_offload_cfg(hns, &txvlan_cfg);
845 hns3_rm_all_vlan_table(struct hns3_adapter *hns, bool is_del_list)
847 struct hns3_user_vlan_table *vlan_entry;
848 struct hns3_pf *pf = &hns->pf;
850 LIST_FOREACH(vlan_entry, &pf->vlan_list, next) {
851 if (vlan_entry->hd_tbl_status) {
852 hns3_set_port_vlan_filter(hns, vlan_entry->vlan_id, 0);
853 vlan_entry->hd_tbl_status = false;
858 vlan_entry = LIST_FIRST(&pf->vlan_list);
860 LIST_REMOVE(vlan_entry, next);
861 rte_free(vlan_entry);
862 vlan_entry = LIST_FIRST(&pf->vlan_list);
868 hns3_add_all_vlan_table(struct hns3_adapter *hns)
870 struct hns3_user_vlan_table *vlan_entry;
871 struct hns3_pf *pf = &hns->pf;
873 LIST_FOREACH(vlan_entry, &pf->vlan_list, next) {
874 if (!vlan_entry->hd_tbl_status) {
875 hns3_set_port_vlan_filter(hns, vlan_entry->vlan_id, 1);
876 vlan_entry->hd_tbl_status = true;
882 hns3_remove_all_vlan_table(struct hns3_adapter *hns)
884 struct hns3_hw *hw = &hns->hw;
887 hns3_rm_all_vlan_table(hns, true);
888 if (hw->port_base_vlan_cfg.pvid != HNS3_INVALID_PVID) {
889 ret = hns3_set_port_vlan_filter(hns,
890 hw->port_base_vlan_cfg.pvid, 0);
892 hns3_err(hw, "Failed to remove all vlan table, ret =%d",
900 hns3_update_vlan_filter_entries(struct hns3_adapter *hns,
901 uint16_t port_base_vlan_state, uint16_t new_pvid)
903 struct hns3_hw *hw = &hns->hw;
907 if (port_base_vlan_state == HNS3_PORT_BASE_VLAN_ENABLE) {
908 old_pvid = hw->port_base_vlan_cfg.pvid;
909 if (old_pvid != HNS3_INVALID_PVID) {
910 ret = hns3_set_port_vlan_filter(hns, old_pvid, 0);
912 hns3_err(hw, "failed to remove old pvid %u, "
913 "ret = %d", old_pvid, ret);
918 hns3_rm_all_vlan_table(hns, false);
919 ret = hns3_set_port_vlan_filter(hns, new_pvid, 1);
921 hns3_err(hw, "failed to add new pvid %u, ret = %d",
926 ret = hns3_set_port_vlan_filter(hns, new_pvid, 0);
928 hns3_err(hw, "failed to remove pvid %u, ret = %d",
933 hns3_add_all_vlan_table(hns);
939 hns3_en_pvid_strip(struct hns3_adapter *hns, int on)
941 struct hns3_rx_vtag_cfg *old_cfg = &hns->pf.vtag_config.rx_vcfg;
942 struct hns3_rx_vtag_cfg rx_vlan_cfg;
946 rx_strip_en = old_cfg->rx_vlan_offload_en;
948 rx_vlan_cfg.strip_tag1_en = rx_strip_en;
949 rx_vlan_cfg.strip_tag2_en = true;
950 rx_vlan_cfg.strip_tag2_discard_en = true;
952 rx_vlan_cfg.strip_tag1_en = false;
953 rx_vlan_cfg.strip_tag2_en = rx_strip_en;
954 rx_vlan_cfg.strip_tag2_discard_en = false;
956 rx_vlan_cfg.strip_tag1_discard_en = false;
957 rx_vlan_cfg.vlan1_vlan_prionly = false;
958 rx_vlan_cfg.vlan2_vlan_prionly = false;
959 rx_vlan_cfg.rx_vlan_offload_en = old_cfg->rx_vlan_offload_en;
961 ret = hns3_set_vlan_rx_offload_cfg(hns, &rx_vlan_cfg);
965 hns3_update_rx_offload_cfg(hns, &rx_vlan_cfg);
970 hns3_vlan_pvid_configure(struct hns3_adapter *hns, uint16_t pvid, int on)
972 struct hns3_hw *hw = &hns->hw;
973 uint16_t port_base_vlan_state;
976 if (on == 0 && pvid != hw->port_base_vlan_cfg.pvid) {
977 if (hw->port_base_vlan_cfg.pvid != HNS3_INVALID_PVID)
978 hns3_warn(hw, "Invalid operation! As current pvid set "
979 "is %u, disable pvid %u is invalid",
980 hw->port_base_vlan_cfg.pvid, pvid);
984 port_base_vlan_state = on ? HNS3_PORT_BASE_VLAN_ENABLE :
985 HNS3_PORT_BASE_VLAN_DISABLE;
986 ret = hns3_vlan_txvlan_cfg(hns, port_base_vlan_state, pvid);
988 hns3_err(hw, "failed to config tx vlan for pvid, ret = %d",
993 ret = hns3_en_pvid_strip(hns, on);
995 hns3_err(hw, "failed to config rx vlan strip for pvid, "
997 goto pvid_vlan_strip_fail;
1000 if (pvid == HNS3_INVALID_PVID)
1002 ret = hns3_update_vlan_filter_entries(hns, port_base_vlan_state, pvid);
1004 hns3_err(hw, "failed to update vlan filter entries, ret = %d",
1006 goto vlan_filter_set_fail;
1010 hw->port_base_vlan_cfg.state = port_base_vlan_state;
1011 hw->port_base_vlan_cfg.pvid = on ? pvid : HNS3_INVALID_PVID;
1014 vlan_filter_set_fail:
1015 err = hns3_en_pvid_strip(hns, hw->port_base_vlan_cfg.state ==
1016 HNS3_PORT_BASE_VLAN_ENABLE);
1018 hns3_err(hw, "fail to rollback pvid strip, ret = %d", err);
1020 pvid_vlan_strip_fail:
1021 err = hns3_vlan_txvlan_cfg(hns, hw->port_base_vlan_cfg.state,
1022 hw->port_base_vlan_cfg.pvid);
1024 hns3_err(hw, "fail to rollback txvlan status, ret = %d", err);
1030 hns3_vlan_pvid_set(struct rte_eth_dev *dev, uint16_t pvid, int on)
1032 struct hns3_adapter *hns = dev->data->dev_private;
1033 struct hns3_hw *hw = &hns->hw;
1034 bool pvid_en_state_change;
1035 uint16_t pvid_state;
1038 if (pvid > RTE_ETHER_MAX_VLAN_ID) {
1039 hns3_err(hw, "Invalid vlan_id = %u > %d", pvid,
1040 RTE_ETHER_MAX_VLAN_ID);
1045 * If PVID configuration state change, should refresh the PVID
1046 * configuration state in struct hns3_tx_queue/hns3_rx_queue.
1048 pvid_state = hw->port_base_vlan_cfg.state;
1049 if ((on && pvid_state == HNS3_PORT_BASE_VLAN_ENABLE) ||
1050 (!on && pvid_state == HNS3_PORT_BASE_VLAN_DISABLE))
1051 pvid_en_state_change = false;
1053 pvid_en_state_change = true;
1055 rte_spinlock_lock(&hw->lock);
1056 ret = hns3_vlan_pvid_configure(hns, pvid, on);
1057 rte_spinlock_unlock(&hw->lock);
1061 * Only in HNS3_SW_SHIFT_AND_MODE the PVID related operation in Tx/Rx
1062 * need be processed by PMD driver.
1064 if (pvid_en_state_change &&
1065 hw->vlan_mode == HNS3_SW_SHIFT_AND_DISCARD_MODE)
1066 hns3_update_all_queues_pvid_proc_en(hw);
1072 hns3_default_vlan_config(struct hns3_adapter *hns)
1074 struct hns3_hw *hw = &hns->hw;
1078 * When vlan filter is enabled, hardware regards packets without vlan
1079 * as packets with vlan 0. Therefore, if vlan 0 is not in the vlan
1080 * table, packets without vlan won't be received. So, add vlan 0 as
1083 ret = hns3_vlan_filter_configure(hns, 0, 1);
1085 hns3_err(hw, "default vlan 0 config failed, ret =%d", ret);
1090 hns3_init_vlan_config(struct hns3_adapter *hns)
1092 struct hns3_hw *hw = &hns->hw;
1096 * This function can be called in the initialization and reset process,
1097 * when in reset process, it means that hardware had been reseted
1098 * successfully and we need to restore the hardware configuration to
1099 * ensure that the hardware configuration remains unchanged before and
1102 if (__atomic_load_n(&hw->reset.resetting, __ATOMIC_RELAXED) == 0) {
1103 hw->port_base_vlan_cfg.state = HNS3_PORT_BASE_VLAN_DISABLE;
1104 hw->port_base_vlan_cfg.pvid = HNS3_INVALID_PVID;
1107 ret = hns3_vlan_filter_init(hns);
1109 hns3_err(hw, "vlan init fail in pf, ret =%d", ret);
1113 ret = hns3_vlan_tpid_configure(hns, RTE_ETH_VLAN_TYPE_INNER,
1114 RTE_ETHER_TYPE_VLAN);
1116 hns3_err(hw, "tpid set fail in pf, ret =%d", ret);
1121 * When in the reinit dev stage of the reset process, the following
1122 * vlan-related configurations may differ from those at initialization,
1123 * we will restore configurations to hardware in hns3_restore_vlan_table
1124 * and hns3_restore_vlan_conf later.
1126 if (__atomic_load_n(&hw->reset.resetting, __ATOMIC_RELAXED) == 0) {
1127 ret = hns3_vlan_pvid_configure(hns, HNS3_INVALID_PVID, 0);
1129 hns3_err(hw, "pvid set fail in pf, ret =%d", ret);
1133 ret = hns3_en_hw_strip_rxvtag(hns, false);
1135 hns3_err(hw, "rx strip configure fail in pf, ret =%d",
1141 return hns3_default_vlan_config(hns);
1145 hns3_restore_vlan_conf(struct hns3_adapter *hns)
1147 struct hns3_pf *pf = &hns->pf;
1148 struct hns3_hw *hw = &hns->hw;
1153 if (!hw->data->promiscuous) {
1154 /* restore vlan filter states */
1155 offloads = hw->data->dev_conf.rxmode.offloads;
1156 enable = offloads & RTE_ETH_RX_OFFLOAD_VLAN_FILTER ? true : false;
1157 ret = hns3_enable_vlan_filter(hns, enable);
1159 hns3_err(hw, "failed to restore vlan rx filter conf, "
1165 ret = hns3_set_vlan_rx_offload_cfg(hns, &pf->vtag_config.rx_vcfg);
1167 hns3_err(hw, "failed to restore vlan rx conf, ret = %d", ret);
1171 ret = hns3_set_vlan_tx_offload_cfg(hns, &pf->vtag_config.tx_vcfg);
1173 hns3_err(hw, "failed to restore vlan tx conf, ret = %d", ret);
1179 hns3_dev_configure_vlan(struct rte_eth_dev *dev)
1181 struct hns3_adapter *hns = dev->data->dev_private;
1182 struct rte_eth_dev_data *data = dev->data;
1183 struct rte_eth_txmode *txmode;
1184 struct hns3_hw *hw = &hns->hw;
1188 txmode = &data->dev_conf.txmode;
1189 if (txmode->hw_vlan_reject_tagged || txmode->hw_vlan_reject_untagged)
1191 "hw_vlan_reject_tagged or hw_vlan_reject_untagged "
1192 "configuration is not supported! Ignore these two "
1193 "parameters: hw_vlan_reject_tagged(%u), "
1194 "hw_vlan_reject_untagged(%u)",
1195 txmode->hw_vlan_reject_tagged,
1196 txmode->hw_vlan_reject_untagged);
1198 /* Apply vlan offload setting */
1199 mask = RTE_ETH_VLAN_STRIP_MASK | RTE_ETH_VLAN_FILTER_MASK;
1200 ret = hns3_vlan_offload_set(dev, mask);
1202 hns3_err(hw, "dev config rx vlan offload failed, ret = %d",
1208 * If pvid config is not set in rte_eth_conf, driver needn't to set
1209 * VLAN pvid related configuration to hardware.
1211 if (txmode->pvid == 0 && txmode->hw_vlan_insert_pvid == 0)
1214 /* Apply pvid setting */
1215 ret = hns3_vlan_pvid_set(dev, txmode->pvid,
1216 txmode->hw_vlan_insert_pvid);
1218 hns3_err(hw, "dev config vlan pvid(%u) failed, ret = %d",
1225 hns3_config_tso(struct hns3_hw *hw, unsigned int tso_mss_min,
1226 unsigned int tso_mss_max)
1228 struct hns3_cfg_tso_status_cmd *req;
1229 struct hns3_cmd_desc desc;
1232 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_TSO_GENERIC_CONFIG, false);
1234 req = (struct hns3_cfg_tso_status_cmd *)desc.data;
1237 hns3_set_field(tso_mss, HNS3_TSO_MSS_MIN_M, HNS3_TSO_MSS_MIN_S,
1239 req->tso_mss_min = rte_cpu_to_le_16(tso_mss);
1242 hns3_set_field(tso_mss, HNS3_TSO_MSS_MIN_M, HNS3_TSO_MSS_MIN_S,
1244 req->tso_mss_max = rte_cpu_to_le_16(tso_mss);
1246 return hns3_cmd_send(hw, &desc, 1);
1250 hns3_set_umv_space(struct hns3_hw *hw, uint16_t space_size,
1251 uint16_t *allocated_size, bool is_alloc)
1253 struct hns3_umv_spc_alc_cmd *req;
1254 struct hns3_cmd_desc desc;
1257 req = (struct hns3_umv_spc_alc_cmd *)desc.data;
1258 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_MAC_VLAN_ALLOCATE, false);
1259 hns3_set_bit(req->allocate, HNS3_UMV_SPC_ALC_B, is_alloc ? 0 : 1);
1260 req->space_size = rte_cpu_to_le_32(space_size);
1262 ret = hns3_cmd_send(hw, &desc, 1);
1264 PMD_INIT_LOG(ERR, "%s umv space failed for cmd_send, ret =%d",
1265 is_alloc ? "allocate" : "free", ret);
1269 if (is_alloc && allocated_size)
1270 *allocated_size = rte_le_to_cpu_32(desc.data[1]);
1276 hns3_init_umv_space(struct hns3_hw *hw)
1278 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
1279 struct hns3_pf *pf = &hns->pf;
1280 uint16_t allocated_size = 0;
1283 ret = hns3_set_umv_space(hw, pf->wanted_umv_size, &allocated_size,
1288 if (allocated_size < pf->wanted_umv_size)
1289 PMD_INIT_LOG(WARNING, "Alloc umv space failed, want %u, get %u",
1290 pf->wanted_umv_size, allocated_size);
1292 pf->max_umv_size = (!!allocated_size) ? allocated_size :
1293 pf->wanted_umv_size;
1294 pf->used_umv_size = 0;
1299 hns3_uninit_umv_space(struct hns3_hw *hw)
1301 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
1302 struct hns3_pf *pf = &hns->pf;
1305 if (pf->max_umv_size == 0)
1308 ret = hns3_set_umv_space(hw, pf->max_umv_size, NULL, false);
1312 pf->max_umv_size = 0;
1318 hns3_is_umv_space_full(struct hns3_hw *hw)
1320 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
1321 struct hns3_pf *pf = &hns->pf;
1324 is_full = (pf->used_umv_size >= pf->max_umv_size);
1330 hns3_update_umv_space(struct hns3_hw *hw, bool is_free)
1332 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
1333 struct hns3_pf *pf = &hns->pf;
1336 if (pf->used_umv_size > 0)
1337 pf->used_umv_size--;
1339 pf->used_umv_size++;
1343 hns3_prepare_mac_addr(struct hns3_mac_vlan_tbl_entry_cmd *new_req,
1344 const uint8_t *addr, bool is_mc)
1346 const unsigned char *mac_addr = addr;
1347 uint32_t high_val = ((uint32_t)mac_addr[3] << 24) |
1348 ((uint32_t)mac_addr[2] << 16) |
1349 ((uint32_t)mac_addr[1] << 8) |
1350 (uint32_t)mac_addr[0];
1351 uint32_t low_val = ((uint32_t)mac_addr[5] << 8) | (uint32_t)mac_addr[4];
1353 hns3_set_bit(new_req->flags, HNS3_MAC_VLAN_BIT0_EN_B, 1);
1355 hns3_set_bit(new_req->entry_type, HNS3_MAC_VLAN_BIT0_EN_B, 0);
1356 hns3_set_bit(new_req->entry_type, HNS3_MAC_VLAN_BIT1_EN_B, 1);
1357 hns3_set_bit(new_req->mc_mac_en, HNS3_MAC_VLAN_BIT0_EN_B, 1);
1360 new_req->mac_addr_hi32 = rte_cpu_to_le_32(high_val);
1361 new_req->mac_addr_lo16 = rte_cpu_to_le_16(low_val & 0xffff);
1365 hns3_get_mac_vlan_cmd_status(struct hns3_hw *hw, uint16_t cmdq_resp,
1367 enum hns3_mac_vlan_tbl_opcode op)
1370 hns3_err(hw, "cmdq execute failed for get_mac_vlan_cmd_status,status=%u",
1375 if (op == HNS3_MAC_VLAN_ADD) {
1376 if (resp_code == 0 || resp_code == 1) {
1378 } else if (resp_code == HNS3_ADD_UC_OVERFLOW) {
1379 hns3_err(hw, "add mac addr failed for uc_overflow");
1381 } else if (resp_code == HNS3_ADD_MC_OVERFLOW) {
1382 hns3_err(hw, "add mac addr failed for mc_overflow");
1386 hns3_err(hw, "add mac addr failed for undefined, code=%u",
1389 } else if (op == HNS3_MAC_VLAN_REMOVE) {
1390 if (resp_code == 0) {
1392 } else if (resp_code == 1) {
1393 hns3_dbg(hw, "remove mac addr failed for miss");
1397 hns3_err(hw, "remove mac addr failed for undefined, code=%u",
1400 } else if (op == HNS3_MAC_VLAN_LKUP) {
1401 if (resp_code == 0) {
1403 } else if (resp_code == 1) {
1404 hns3_dbg(hw, "lookup mac addr failed for miss");
1408 hns3_err(hw, "lookup mac addr failed for undefined, code=%u",
1413 hns3_err(hw, "unknown opcode for get_mac_vlan_cmd_status, opcode=%u",
1420 hns3_lookup_mac_vlan_tbl(struct hns3_hw *hw,
1421 struct hns3_mac_vlan_tbl_entry_cmd *req,
1422 struct hns3_cmd_desc *desc, uint8_t desc_num)
1429 if (desc_num == HNS3_MC_MAC_VLAN_OPS_DESC_NUM) {
1430 for (i = 0; i < desc_num - 1; i++) {
1431 hns3_cmd_setup_basic_desc(&desc[i],
1432 HNS3_OPC_MAC_VLAN_ADD, true);
1433 desc[i].flag |= rte_cpu_to_le_16(HNS3_CMD_FLAG_NEXT);
1435 memcpy(desc[i].data, req,
1436 sizeof(struct hns3_mac_vlan_tbl_entry_cmd));
1438 hns3_cmd_setup_basic_desc(&desc[i], HNS3_OPC_MAC_VLAN_ADD,
1441 hns3_cmd_setup_basic_desc(&desc[0], HNS3_OPC_MAC_VLAN_ADD,
1443 memcpy(desc[0].data, req,
1444 sizeof(struct hns3_mac_vlan_tbl_entry_cmd));
1446 ret = hns3_cmd_send(hw, desc, desc_num);
1448 hns3_err(hw, "lookup mac addr failed for cmd_send, ret =%d.",
1452 resp_code = (rte_le_to_cpu_32(desc[0].data[0]) >> 8) & 0xff;
1453 retval = rte_le_to_cpu_16(desc[0].retval);
1455 return hns3_get_mac_vlan_cmd_status(hw, retval, resp_code,
1456 HNS3_MAC_VLAN_LKUP);
1460 hns3_add_mac_vlan_tbl(struct hns3_hw *hw,
1461 struct hns3_mac_vlan_tbl_entry_cmd *req,
1462 struct hns3_cmd_desc *desc, uint8_t desc_num)
1470 if (desc_num == HNS3_UC_MAC_VLAN_OPS_DESC_NUM) {
1471 hns3_cmd_setup_basic_desc(desc, HNS3_OPC_MAC_VLAN_ADD, false);
1472 memcpy(desc->data, req,
1473 sizeof(struct hns3_mac_vlan_tbl_entry_cmd));
1474 ret = hns3_cmd_send(hw, desc, desc_num);
1475 resp_code = (rte_le_to_cpu_32(desc->data[0]) >> 8) & 0xff;
1476 retval = rte_le_to_cpu_16(desc->retval);
1478 cfg_status = hns3_get_mac_vlan_cmd_status(hw, retval, resp_code,
1481 for (i = 0; i < desc_num; i++) {
1482 hns3_cmd_reuse_desc(&desc[i], false);
1483 if (i == desc_num - 1)
1485 rte_cpu_to_le_16(~HNS3_CMD_FLAG_NEXT);
1488 rte_cpu_to_le_16(HNS3_CMD_FLAG_NEXT);
1490 memcpy(desc[0].data, req,
1491 sizeof(struct hns3_mac_vlan_tbl_entry_cmd));
1493 ret = hns3_cmd_send(hw, desc, desc_num);
1494 resp_code = (rte_le_to_cpu_32(desc[0].data[0]) >> 8) & 0xff;
1495 retval = rte_le_to_cpu_16(desc[0].retval);
1497 cfg_status = hns3_get_mac_vlan_cmd_status(hw, retval, resp_code,
1502 hns3_err(hw, "add mac addr failed for cmd_send, ret =%d", ret);
1510 hns3_remove_mac_vlan_tbl(struct hns3_hw *hw,
1511 struct hns3_mac_vlan_tbl_entry_cmd *req)
1513 struct hns3_cmd_desc desc;
1518 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_MAC_VLAN_REMOVE, false);
1520 memcpy(desc.data, req, sizeof(struct hns3_mac_vlan_tbl_entry_cmd));
1522 ret = hns3_cmd_send(hw, &desc, 1);
1524 hns3_err(hw, "del mac addr failed for cmd_send, ret =%d", ret);
1527 resp_code = (rte_le_to_cpu_32(desc.data[0]) >> 8) & 0xff;
1528 retval = rte_le_to_cpu_16(desc.retval);
1530 return hns3_get_mac_vlan_cmd_status(hw, retval, resp_code,
1531 HNS3_MAC_VLAN_REMOVE);
1535 hns3_add_uc_mac_addr(struct hns3_hw *hw, struct rte_ether_addr *mac_addr)
1537 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
1538 struct hns3_mac_vlan_tbl_entry_cmd req;
1539 struct hns3_pf *pf = &hns->pf;
1540 struct hns3_cmd_desc desc;
1541 char mac_str[RTE_ETHER_ADDR_FMT_SIZE];
1542 uint16_t egress_port = 0;
1546 /* check if mac addr is valid */
1547 if (!rte_is_valid_assigned_ether_addr(mac_addr)) {
1548 hns3_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
1550 hns3_err(hw, "Add unicast mac addr err! addr(%s) invalid",
1555 memset(&req, 0, sizeof(req));
1558 * In current version VF is not supported when PF is driven by DPDK
1559 * driver, just need to configure parameters for PF vport.
1561 vf_id = HNS3_PF_FUNC_ID;
1562 hns3_set_field(egress_port, HNS3_MAC_EPORT_VFID_M,
1563 HNS3_MAC_EPORT_VFID_S, vf_id);
1565 req.egress_port = rte_cpu_to_le_16(egress_port);
1567 hns3_prepare_mac_addr(&req, mac_addr->addr_bytes, false);
1570 * Lookup the mac address in the mac_vlan table, and add
1571 * it if the entry is inexistent. Repeated unicast entry
1572 * is not allowed in the mac vlan table.
1574 ret = hns3_lookup_mac_vlan_tbl(hw, &req, &desc,
1575 HNS3_UC_MAC_VLAN_OPS_DESC_NUM);
1576 if (ret == -ENOENT) {
1577 if (!hns3_is_umv_space_full(hw)) {
1578 ret = hns3_add_mac_vlan_tbl(hw, &req, &desc,
1579 HNS3_UC_MAC_VLAN_OPS_DESC_NUM);
1581 hns3_update_umv_space(hw, false);
1585 hns3_err(hw, "UC MAC table full(%u)", pf->used_umv_size);
1590 hns3_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE, mac_addr);
1592 /* check if we just hit the duplicate */
1594 hns3_dbg(hw, "mac addr(%s) has been in the MAC table", mac_str);
1598 hns3_err(hw, "PF failed to add unicast entry(%s) in the MAC table",
1605 hns3_remove_uc_mac_addr(struct hns3_hw *hw, struct rte_ether_addr *mac_addr)
1607 struct hns3_mac_vlan_tbl_entry_cmd req;
1608 char mac_str[RTE_ETHER_ADDR_FMT_SIZE];
1611 /* check if mac addr is valid */
1612 if (!rte_is_valid_assigned_ether_addr(mac_addr)) {
1613 hns3_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
1615 hns3_err(hw, "remove unicast mac addr err! addr(%s) invalid",
1620 memset(&req, 0, sizeof(req));
1621 hns3_set_bit(req.entry_type, HNS3_MAC_VLAN_BIT0_EN_B, 0);
1622 hns3_prepare_mac_addr(&req, mac_addr->addr_bytes, false);
1623 ret = hns3_remove_mac_vlan_tbl(hw, &req);
1624 if (ret == -ENOENT) /* mac addr isn't existent in the mac vlan table. */
1627 hns3_update_umv_space(hw, true);
1633 hns3_set_default_mac_addr(struct rte_eth_dev *dev,
1634 struct rte_ether_addr *mac_addr)
1636 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1637 struct rte_ether_addr *oaddr;
1638 char mac_str[RTE_ETHER_ADDR_FMT_SIZE];
1641 rte_spinlock_lock(&hw->lock);
1642 oaddr = (struct rte_ether_addr *)hw->mac.mac_addr;
1643 ret = hw->ops.del_uc_mac_addr(hw, oaddr);
1645 hns3_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
1647 hns3_warn(hw, "Remove old uc mac address(%s) fail: %d",
1650 rte_spinlock_unlock(&hw->lock);
1654 ret = hw->ops.add_uc_mac_addr(hw, mac_addr);
1656 hns3_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
1658 hns3_err(hw, "Failed to set mac addr(%s): %d", mac_str, ret);
1659 goto err_add_uc_addr;
1662 ret = hns3_pause_addr_cfg(hw, mac_addr->addr_bytes);
1664 hns3_err(hw, "Failed to configure mac pause address: %d", ret);
1665 goto err_pause_addr_cfg;
1668 rte_ether_addr_copy(mac_addr,
1669 (struct rte_ether_addr *)hw->mac.mac_addr);
1670 rte_spinlock_unlock(&hw->lock);
1675 ret_val = hw->ops.del_uc_mac_addr(hw, mac_addr);
1677 hns3_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
1680 "Failed to roll back to del setted mac addr(%s): %d",
1685 ret_val = hw->ops.add_uc_mac_addr(hw, oaddr);
1687 hns3_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE, oaddr);
1688 hns3_warn(hw, "Failed to restore old uc mac addr(%s): %d",
1691 rte_spinlock_unlock(&hw->lock);
1697 hns3_update_desc_vfid(struct hns3_cmd_desc *desc, uint8_t vfid, bool clr)
1699 #define HNS3_VF_NUM_IN_FIRST_DESC 192
1703 if (vfid < HNS3_VF_NUM_IN_FIRST_DESC) {
1704 word_num = vfid / 32;
1705 bit_num = vfid % 32;
1707 desc[1].data[word_num] &=
1708 rte_cpu_to_le_32(~(1UL << bit_num));
1710 desc[1].data[word_num] |=
1711 rte_cpu_to_le_32(1UL << bit_num);
1713 word_num = (vfid - HNS3_VF_NUM_IN_FIRST_DESC) / 32;
1714 bit_num = vfid % 32;
1716 desc[2].data[word_num] &=
1717 rte_cpu_to_le_32(~(1UL << bit_num));
1719 desc[2].data[word_num] |=
1720 rte_cpu_to_le_32(1UL << bit_num);
1725 hns3_add_mc_mac_addr(struct hns3_hw *hw, struct rte_ether_addr *mac_addr)
1727 struct hns3_cmd_desc desc[HNS3_MC_MAC_VLAN_OPS_DESC_NUM];
1728 struct hns3_mac_vlan_tbl_entry_cmd req;
1729 char mac_str[RTE_ETHER_ADDR_FMT_SIZE];
1733 /* Check if mac addr is valid */
1734 if (!rte_is_multicast_ether_addr(mac_addr)) {
1735 hns3_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
1737 hns3_err(hw, "failed to add mc mac addr, addr(%s) invalid",
1742 memset(&req, 0, sizeof(req));
1743 hns3_set_bit(req.entry_type, HNS3_MAC_VLAN_BIT0_EN_B, 0);
1744 hns3_prepare_mac_addr(&req, mac_addr->addr_bytes, true);
1745 ret = hns3_lookup_mac_vlan_tbl(hw, &req, desc,
1746 HNS3_MC_MAC_VLAN_OPS_DESC_NUM);
1748 /* This mac addr do not exist, add new entry for it */
1749 memset(desc[0].data, 0, sizeof(desc[0].data));
1750 memset(desc[1].data, 0, sizeof(desc[0].data));
1751 memset(desc[2].data, 0, sizeof(desc[0].data));
1755 * In current version VF is not supported when PF is driven by DPDK
1756 * driver, just need to configure parameters for PF vport.
1758 vf_id = HNS3_PF_FUNC_ID;
1759 hns3_update_desc_vfid(desc, vf_id, false);
1760 ret = hns3_add_mac_vlan_tbl(hw, &req, desc,
1761 HNS3_MC_MAC_VLAN_OPS_DESC_NUM);
1764 hns3_err(hw, "mc mac vlan table is full");
1765 hns3_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
1767 hns3_err(hw, "failed to add mc mac addr(%s): %d", mac_str, ret);
1774 hns3_remove_mc_mac_addr(struct hns3_hw *hw, struct rte_ether_addr *mac_addr)
1776 struct hns3_mac_vlan_tbl_entry_cmd req;
1777 struct hns3_cmd_desc desc[3];
1778 char mac_str[RTE_ETHER_ADDR_FMT_SIZE];
1782 /* Check if mac addr is valid */
1783 if (!rte_is_multicast_ether_addr(mac_addr)) {
1784 hns3_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
1786 hns3_err(hw, "Failed to rm mc mac addr, addr(%s) invalid",
1791 memset(&req, 0, sizeof(req));
1792 hns3_set_bit(req.entry_type, HNS3_MAC_VLAN_BIT0_EN_B, 0);
1793 hns3_prepare_mac_addr(&req, mac_addr->addr_bytes, true);
1794 ret = hns3_lookup_mac_vlan_tbl(hw, &req, desc,
1795 HNS3_MC_MAC_VLAN_OPS_DESC_NUM);
1798 * This mac addr exist, remove this handle's VFID for it.
1799 * In current version VF is not supported when PF is driven by
1800 * DPDK driver, just need to configure parameters for PF vport.
1802 vf_id = HNS3_PF_FUNC_ID;
1803 hns3_update_desc_vfid(desc, vf_id, true);
1805 /* All the vfid is zero, so need to delete this entry */
1806 ret = hns3_remove_mac_vlan_tbl(hw, &req);
1807 } else if (ret == -ENOENT) {
1808 /* This mac addr doesn't exist. */
1813 hns3_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
1815 hns3_err(hw, "Failed to rm mc mac addr(%s): %d", mac_str, ret);
1822 hns3_check_mq_mode(struct rte_eth_dev *dev)
1824 enum rte_eth_rx_mq_mode rx_mq_mode = dev->data->dev_conf.rxmode.mq_mode;
1825 enum rte_eth_tx_mq_mode tx_mq_mode = dev->data->dev_conf.txmode.mq_mode;
1826 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1827 struct hns3_pf *pf = HNS3_DEV_PRIVATE_TO_PF(dev->data->dev_private);
1828 struct rte_eth_dcb_rx_conf *dcb_rx_conf;
1829 struct rte_eth_dcb_tx_conf *dcb_tx_conf;
1834 if (((uint32_t)rx_mq_mode & RTE_ETH_MQ_RX_VMDQ_FLAG) ||
1835 (tx_mq_mode == RTE_ETH_MQ_TX_VMDQ_DCB ||
1836 tx_mq_mode == RTE_ETH_MQ_TX_VMDQ_ONLY)) {
1837 hns3_err(hw, "VMDQ is not supported, rx_mq_mode = %d, tx_mq_mode = %d.",
1838 rx_mq_mode, tx_mq_mode);
1842 dcb_rx_conf = &dev->data->dev_conf.rx_adv_conf.dcb_rx_conf;
1843 dcb_tx_conf = &dev->data->dev_conf.tx_adv_conf.dcb_tx_conf;
1844 if ((uint32_t)rx_mq_mode & RTE_ETH_MQ_RX_DCB_FLAG) {
1845 if (dcb_rx_conf->nb_tcs > pf->tc_max) {
1846 hns3_err(hw, "nb_tcs(%u) > max_tc(%u) driver supported.",
1847 dcb_rx_conf->nb_tcs, pf->tc_max);
1851 if (!(dcb_rx_conf->nb_tcs == HNS3_4_TCS ||
1852 dcb_rx_conf->nb_tcs == HNS3_8_TCS)) {
1853 hns3_err(hw, "on RTE_ETH_MQ_RX_DCB_RSS mode, "
1854 "nb_tcs(%d) != %d or %d in rx direction.",
1855 dcb_rx_conf->nb_tcs, HNS3_4_TCS, HNS3_8_TCS);
1859 if (dcb_rx_conf->nb_tcs != dcb_tx_conf->nb_tcs) {
1860 hns3_err(hw, "num_tcs(%d) of tx is not equal to rx(%d)",
1861 dcb_tx_conf->nb_tcs, dcb_rx_conf->nb_tcs);
1865 for (i = 0; i < HNS3_MAX_USER_PRIO; i++) {
1866 if (dcb_rx_conf->dcb_tc[i] != dcb_tx_conf->dcb_tc[i]) {
1867 hns3_err(hw, "dcb_tc[%d] = %u in rx direction, "
1868 "is not equal to one in tx direction.",
1869 i, dcb_rx_conf->dcb_tc[i]);
1872 if (dcb_rx_conf->dcb_tc[i] > max_tc)
1873 max_tc = dcb_rx_conf->dcb_tc[i];
1876 num_tc = max_tc + 1;
1877 if (num_tc > dcb_rx_conf->nb_tcs) {
1878 hns3_err(hw, "max num_tc(%u) mapped > nb_tcs(%u)",
1879 num_tc, dcb_rx_conf->nb_tcs);
1888 hns3_bind_ring_with_vector(struct hns3_hw *hw, uint16_t vector_id, bool en,
1889 enum hns3_ring_type queue_type, uint16_t queue_id)
1891 struct hns3_cmd_desc desc;
1892 struct hns3_ctrl_vector_chain_cmd *req =
1893 (struct hns3_ctrl_vector_chain_cmd *)desc.data;
1894 enum hns3_opcode_type op;
1895 uint16_t tqp_type_and_id = 0;
1900 op = en ? HNS3_OPC_ADD_RING_TO_VECTOR : HNS3_OPC_DEL_RING_TO_VECTOR;
1901 hns3_cmd_setup_basic_desc(&desc, op, false);
1902 req->int_vector_id = hns3_get_field(vector_id, HNS3_TQP_INT_ID_L_M,
1903 HNS3_TQP_INT_ID_L_S);
1904 req->int_vector_id_h = hns3_get_field(vector_id, HNS3_TQP_INT_ID_H_M,
1905 HNS3_TQP_INT_ID_H_S);
1907 if (queue_type == HNS3_RING_TYPE_RX)
1908 gl = HNS3_RING_GL_RX;
1910 gl = HNS3_RING_GL_TX;
1914 hns3_set_field(tqp_type_and_id, HNS3_INT_TYPE_M, HNS3_INT_TYPE_S,
1916 hns3_set_field(tqp_type_and_id, HNS3_TQP_ID_M, HNS3_TQP_ID_S, queue_id);
1917 hns3_set_field(tqp_type_and_id, HNS3_INT_GL_IDX_M, HNS3_INT_GL_IDX_S,
1919 req->tqp_type_and_id[0] = rte_cpu_to_le_16(tqp_type_and_id);
1920 req->int_cause_num = 1;
1921 ret = hns3_cmd_send(hw, &desc, 1);
1923 hns3_err(hw, "%s TQP %u fail, vector_id = %u, ret = %d.",
1924 en ? "Map" : "Unmap", queue_id, vector_id, ret);
1932 hns3_init_ring_with_vector(struct hns3_hw *hw)
1939 * In hns3 network engine, vector 0 is always the misc interrupt of this
1940 * function, vector 1~N can be used respectively for the queues of the
1941 * function. Tx and Rx queues with the same number share the interrupt
1942 * vector. In the initialization clearing the all hardware mapping
1943 * relationship configurations between queues and interrupt vectors is
1944 * needed, so some error caused by the residual configurations, such as
1945 * the unexpected Tx interrupt, can be avoid.
1947 vec = hw->num_msi - 1; /* vector 0 for misc interrupt, not for queue */
1948 if (hw->intr.mapping_mode == HNS3_INTR_MAPPING_VEC_RSV_ONE)
1949 vec = vec - 1; /* the last interrupt is reserved */
1950 hw->intr_tqps_num = RTE_MIN(vec, hw->tqps_num);
1951 for (i = 0; i < hw->intr_tqps_num; i++) {
1953 * Set gap limiter/rate limiter/quanity limiter algorithm
1954 * configuration for interrupt coalesce of queue's interrupt.
1956 hns3_set_queue_intr_gl(hw, i, HNS3_RING_GL_RX,
1957 HNS3_TQP_INTR_GL_DEFAULT);
1958 hns3_set_queue_intr_gl(hw, i, HNS3_RING_GL_TX,
1959 HNS3_TQP_INTR_GL_DEFAULT);
1960 hns3_set_queue_intr_rl(hw, i, HNS3_TQP_INTR_RL_DEFAULT);
1962 * QL(quantity limiter) is not used currently, just set 0 to
1965 hns3_set_queue_intr_ql(hw, i, HNS3_TQP_INTR_QL_DEFAULT);
1967 ret = hns3_bind_ring_with_vector(hw, vec, false,
1968 HNS3_RING_TYPE_TX, i);
1970 PMD_INIT_LOG(ERR, "PF fail to unbind TX ring(%d) with "
1971 "vector: %u, ret=%d", i, vec, ret);
1975 ret = hns3_bind_ring_with_vector(hw, vec, false,
1976 HNS3_RING_TYPE_RX, i);
1978 PMD_INIT_LOG(ERR, "PF fail to unbind RX ring(%d) with "
1979 "vector: %u, ret=%d", i, vec, ret);
1988 hns3_setup_dcb(struct rte_eth_dev *dev)
1990 struct hns3_adapter *hns = dev->data->dev_private;
1991 struct hns3_hw *hw = &hns->hw;
1994 if (!hns3_dev_get_support(hw, DCB)) {
1995 hns3_err(hw, "this port does not support dcb configurations.");
1999 if (hw->current_fc_status == HNS3_FC_STATUS_MAC_PAUSE) {
2000 hns3_err(hw, "MAC pause enabled, cannot config dcb info.");
2004 ret = hns3_dcb_configure(hns);
2006 hns3_err(hw, "failed to config dcb: %d", ret);
2012 hns3_check_link_speed(struct hns3_hw *hw, uint32_t link_speeds)
2017 * Some hardware doesn't support auto-negotiation, but users may not
2018 * configure link_speeds (default 0), which means auto-negotiation.
2019 * In this case, it should return success.
2021 if (link_speeds == RTE_ETH_LINK_SPEED_AUTONEG &&
2022 hw->mac.support_autoneg == 0)
2025 if (link_speeds != RTE_ETH_LINK_SPEED_AUTONEG) {
2026 ret = hns3_check_port_speed(hw, link_speeds);
2035 hns3_check_dev_conf(struct rte_eth_dev *dev)
2037 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2038 struct rte_eth_conf *conf = &dev->data->dev_conf;
2041 ret = hns3_check_mq_mode(dev);
2045 return hns3_check_link_speed(hw, conf->link_speeds);
2049 hns3_dev_configure(struct rte_eth_dev *dev)
2051 struct hns3_adapter *hns = dev->data->dev_private;
2052 struct rte_eth_conf *conf = &dev->data->dev_conf;
2053 enum rte_eth_rx_mq_mode mq_mode = conf->rxmode.mq_mode;
2054 struct hns3_hw *hw = &hns->hw;
2055 uint16_t nb_rx_q = dev->data->nb_rx_queues;
2056 uint16_t nb_tx_q = dev->data->nb_tx_queues;
2057 struct rte_eth_rss_conf rss_conf;
2061 hw->cfg_max_queues = RTE_MAX(nb_rx_q, nb_tx_q);
2064 * Some versions of hardware network engine does not support
2065 * individually enable/disable/reset the Tx or Rx queue. These devices
2066 * must enable/disable/reset Tx and Rx queues at the same time. When the
2067 * numbers of Tx queues allocated by upper applications are not equal to
2068 * the numbers of Rx queues, driver needs to setup fake Tx or Rx queues
2069 * to adjust numbers of Tx/Rx queues. otherwise, network engine can not
2070 * work as usual. But these fake queues are imperceptible, and can not
2071 * be used by upper applications.
2073 ret = hns3_set_fake_rx_or_tx_queues(dev, nb_rx_q, nb_tx_q);
2075 hns3_err(hw, "fail to set Rx/Tx fake queues, ret = %d.", ret);
2076 hw->cfg_max_queues = 0;
2080 hw->adapter_state = HNS3_NIC_CONFIGURING;
2081 ret = hns3_check_dev_conf(dev);
2085 if ((uint32_t)mq_mode & RTE_ETH_MQ_RX_DCB_FLAG) {
2086 ret = hns3_setup_dcb(dev);
2091 /* When RSS is not configured, redirect the packet queue 0 */
2092 if ((uint32_t)mq_mode & RTE_ETH_MQ_RX_RSS_FLAG) {
2093 conf->rxmode.offloads |= RTE_ETH_RX_OFFLOAD_RSS_HASH;
2094 rss_conf = conf->rx_adv_conf.rss_conf;
2095 hw->rss_dis_flag = false;
2096 ret = hns3_dev_rss_hash_update(dev, &rss_conf);
2101 ret = hns3_dev_mtu_set(dev, conf->rxmode.mtu);
2105 ret = hns3_mbuf_dyn_rx_timestamp_register(dev, conf);
2109 ret = hns3_dev_configure_vlan(dev);
2113 /* config hardware GRO */
2114 gro_en = conf->rxmode.offloads & RTE_ETH_RX_OFFLOAD_TCP_LRO ? true : false;
2115 ret = hns3_config_gro(hw, gro_en);
2119 hns3_init_rx_ptype_tble(dev);
2120 hw->adapter_state = HNS3_NIC_CONFIGURED;
2125 hw->cfg_max_queues = 0;
2126 (void)hns3_set_fake_rx_or_tx_queues(dev, 0, 0);
2127 hw->adapter_state = HNS3_NIC_INITIALIZED;
2133 hns3_set_mac_mtu(struct hns3_hw *hw, uint16_t new_mps)
2135 struct hns3_config_max_frm_size_cmd *req;
2136 struct hns3_cmd_desc desc;
2138 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_CONFIG_MAX_FRM_SIZE, false);
2140 req = (struct hns3_config_max_frm_size_cmd *)desc.data;
2141 req->max_frm_size = rte_cpu_to_le_16(new_mps);
2142 req->min_frm_size = RTE_ETHER_MIN_LEN;
2144 return hns3_cmd_send(hw, &desc, 1);
2148 hns3_config_mtu(struct hns3_hw *hw, uint16_t mps)
2150 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
2151 uint16_t original_mps = hns->pf.mps;
2155 ret = hns3_set_mac_mtu(hw, mps);
2157 hns3_err(hw, "failed to set mtu, ret = %d", ret);
2162 ret = hns3_buffer_alloc(hw);
2164 hns3_err(hw, "failed to allocate buffer, ret = %d", ret);
2171 err = hns3_set_mac_mtu(hw, original_mps);
2173 hns3_err(hw, "fail to rollback MTU, err = %d", err);
2176 hns->pf.mps = original_mps;
2182 hns3_dev_mtu_set(struct rte_eth_dev *dev, uint16_t mtu)
2184 struct hns3_adapter *hns = dev->data->dev_private;
2185 uint32_t frame_size = mtu + HNS3_ETH_OVERHEAD;
2186 struct hns3_hw *hw = &hns->hw;
2189 if (dev->data->dev_started) {
2190 hns3_err(hw, "Failed to set mtu, port %u must be stopped "
2191 "before configuration", dev->data->port_id);
2195 rte_spinlock_lock(&hw->lock);
2196 frame_size = RTE_MAX(frame_size, HNS3_DEFAULT_FRAME_LEN);
2199 * Maximum value of frame_size is HNS3_MAX_FRAME_LEN, so it can safely
2200 * assign to "uint16_t" type variable.
2202 ret = hns3_config_mtu(hw, (uint16_t)frame_size);
2204 rte_spinlock_unlock(&hw->lock);
2205 hns3_err(hw, "Failed to set mtu, port %u mtu %u: %d",
2206 dev->data->port_id, mtu, ret);
2210 rte_spinlock_unlock(&hw->lock);
2216 hns3_get_copper_port_speed_capa(uint32_t supported_speed)
2218 uint32_t speed_capa = 0;
2220 if (supported_speed & HNS3_PHY_LINK_SPEED_10M_HD_BIT)
2221 speed_capa |= RTE_ETH_LINK_SPEED_10M_HD;
2222 if (supported_speed & HNS3_PHY_LINK_SPEED_10M_BIT)
2223 speed_capa |= RTE_ETH_LINK_SPEED_10M;
2224 if (supported_speed & HNS3_PHY_LINK_SPEED_100M_HD_BIT)
2225 speed_capa |= RTE_ETH_LINK_SPEED_100M_HD;
2226 if (supported_speed & HNS3_PHY_LINK_SPEED_100M_BIT)
2227 speed_capa |= RTE_ETH_LINK_SPEED_100M;
2228 if (supported_speed & HNS3_PHY_LINK_SPEED_1000M_BIT)
2229 speed_capa |= RTE_ETH_LINK_SPEED_1G;
2235 hns3_get_firber_port_speed_capa(uint32_t supported_speed)
2237 uint32_t speed_capa = 0;
2239 if (supported_speed & HNS3_FIBER_LINK_SPEED_1G_BIT)
2240 speed_capa |= RTE_ETH_LINK_SPEED_1G;
2241 if (supported_speed & HNS3_FIBER_LINK_SPEED_10G_BIT)
2242 speed_capa |= RTE_ETH_LINK_SPEED_10G;
2243 if (supported_speed & HNS3_FIBER_LINK_SPEED_25G_BIT)
2244 speed_capa |= RTE_ETH_LINK_SPEED_25G;
2245 if (supported_speed & HNS3_FIBER_LINK_SPEED_40G_BIT)
2246 speed_capa |= RTE_ETH_LINK_SPEED_40G;
2247 if (supported_speed & HNS3_FIBER_LINK_SPEED_50G_BIT)
2248 speed_capa |= RTE_ETH_LINK_SPEED_50G;
2249 if (supported_speed & HNS3_FIBER_LINK_SPEED_100G_BIT)
2250 speed_capa |= RTE_ETH_LINK_SPEED_100G;
2251 if (supported_speed & HNS3_FIBER_LINK_SPEED_200G_BIT)
2252 speed_capa |= RTE_ETH_LINK_SPEED_200G;
2258 hns3_get_speed_capa(struct hns3_hw *hw)
2260 struct hns3_mac *mac = &hw->mac;
2261 uint32_t speed_capa;
2263 if (mac->media_type == HNS3_MEDIA_TYPE_COPPER)
2265 hns3_get_copper_port_speed_capa(mac->supported_speed);
2268 hns3_get_firber_port_speed_capa(mac->supported_speed);
2270 if (mac->support_autoneg == 0)
2271 speed_capa |= RTE_ETH_LINK_SPEED_FIXED;
2277 hns3_dev_infos_get(struct rte_eth_dev *eth_dev, struct rte_eth_dev_info *info)
2279 struct hns3_adapter *hns = eth_dev->data->dev_private;
2280 struct hns3_hw *hw = &hns->hw;
2281 uint16_t queue_num = hw->tqps_num;
2284 * In interrupt mode, 'max_rx_queues' is set based on the number of
2285 * MSI-X interrupt resources of the hardware.
2287 if (hw->data->dev_conf.intr_conf.rxq == 1)
2288 queue_num = hw->intr_tqps_num;
2290 info->max_rx_queues = queue_num;
2291 info->max_tx_queues = hw->tqps_num;
2292 info->max_rx_pktlen = HNS3_MAX_FRAME_LEN; /* CRC included */
2293 info->min_rx_bufsize = HNS3_MIN_BD_BUF_SIZE;
2294 info->max_mac_addrs = HNS3_UC_MACADDR_NUM;
2295 info->max_mtu = info->max_rx_pktlen - HNS3_ETH_OVERHEAD;
2296 info->max_lro_pkt_size = HNS3_MAX_LRO_SIZE;
2297 info->rx_offload_capa = (RTE_ETH_RX_OFFLOAD_IPV4_CKSUM |
2298 RTE_ETH_RX_OFFLOAD_TCP_CKSUM |
2299 RTE_ETH_RX_OFFLOAD_UDP_CKSUM |
2300 RTE_ETH_RX_OFFLOAD_SCTP_CKSUM |
2301 RTE_ETH_RX_OFFLOAD_OUTER_IPV4_CKSUM |
2302 RTE_ETH_RX_OFFLOAD_OUTER_UDP_CKSUM |
2303 RTE_ETH_RX_OFFLOAD_KEEP_CRC |
2304 RTE_ETH_RX_OFFLOAD_SCATTER |
2305 RTE_ETH_RX_OFFLOAD_VLAN_STRIP |
2306 RTE_ETH_RX_OFFLOAD_VLAN_FILTER |
2307 RTE_ETH_RX_OFFLOAD_RSS_HASH |
2308 RTE_ETH_RX_OFFLOAD_TCP_LRO);
2309 info->tx_offload_capa = (RTE_ETH_TX_OFFLOAD_OUTER_IPV4_CKSUM |
2310 RTE_ETH_TX_OFFLOAD_IPV4_CKSUM |
2311 RTE_ETH_TX_OFFLOAD_TCP_CKSUM |
2312 RTE_ETH_TX_OFFLOAD_UDP_CKSUM |
2313 RTE_ETH_TX_OFFLOAD_SCTP_CKSUM |
2314 RTE_ETH_TX_OFFLOAD_MULTI_SEGS |
2315 RTE_ETH_TX_OFFLOAD_TCP_TSO |
2316 RTE_ETH_TX_OFFLOAD_VXLAN_TNL_TSO |
2317 RTE_ETH_TX_OFFLOAD_GRE_TNL_TSO |
2318 RTE_ETH_TX_OFFLOAD_GENEVE_TNL_TSO |
2319 RTE_ETH_TX_OFFLOAD_MBUF_FAST_FREE |
2320 hns3_txvlan_cap_get(hw));
2322 if (hns3_dev_get_support(hw, OUTER_UDP_CKSUM))
2323 info->tx_offload_capa |= RTE_ETH_TX_OFFLOAD_OUTER_UDP_CKSUM;
2325 if (hns3_dev_get_support(hw, INDEP_TXRX))
2326 info->dev_capa = RTE_ETH_DEV_CAPA_RUNTIME_RX_QUEUE_SETUP |
2327 RTE_ETH_DEV_CAPA_RUNTIME_TX_QUEUE_SETUP;
2328 info->dev_capa &= ~RTE_ETH_DEV_CAPA_FLOW_RULE_KEEP;
2330 if (hns3_dev_get_support(hw, PTP))
2331 info->rx_offload_capa |= RTE_ETH_RX_OFFLOAD_TIMESTAMP;
2333 info->rx_desc_lim = (struct rte_eth_desc_lim) {
2334 .nb_max = HNS3_MAX_RING_DESC,
2335 .nb_min = HNS3_MIN_RING_DESC,
2336 .nb_align = HNS3_ALIGN_RING_DESC,
2339 info->tx_desc_lim = (struct rte_eth_desc_lim) {
2340 .nb_max = HNS3_MAX_RING_DESC,
2341 .nb_min = HNS3_MIN_RING_DESC,
2342 .nb_align = HNS3_ALIGN_RING_DESC,
2343 .nb_seg_max = HNS3_MAX_TSO_BD_PER_PKT,
2344 .nb_mtu_seg_max = hw->max_non_tso_bd_num,
2347 info->speed_capa = hns3_get_speed_capa(hw);
2348 info->default_rxconf = (struct rte_eth_rxconf) {
2349 .rx_free_thresh = HNS3_DEFAULT_RX_FREE_THRESH,
2351 * If there are no available Rx buffer descriptors, incoming
2352 * packets are always dropped by hardware based on hns3 network
2358 info->default_txconf = (struct rte_eth_txconf) {
2359 .tx_rs_thresh = HNS3_DEFAULT_TX_RS_THRESH,
2363 info->reta_size = hw->rss_ind_tbl_size;
2364 info->hash_key_size = HNS3_RSS_KEY_SIZE;
2365 info->flow_type_rss_offloads = HNS3_ETH_RSS_SUPPORT;
2367 info->default_rxportconf.burst_size = HNS3_DEFAULT_PORT_CONF_BURST_SIZE;
2368 info->default_txportconf.burst_size = HNS3_DEFAULT_PORT_CONF_BURST_SIZE;
2369 info->default_rxportconf.nb_queues = HNS3_DEFAULT_PORT_CONF_QUEUES_NUM;
2370 info->default_txportconf.nb_queues = HNS3_DEFAULT_PORT_CONF_QUEUES_NUM;
2371 info->default_rxportconf.ring_size = HNS3_DEFAULT_RING_DESC;
2372 info->default_txportconf.ring_size = HNS3_DEFAULT_RING_DESC;
2378 hns3_fw_version_get(struct rte_eth_dev *eth_dev, char *fw_version,
2381 struct hns3_adapter *hns = eth_dev->data->dev_private;
2382 struct hns3_hw *hw = &hns->hw;
2383 uint32_t version = hw->fw_version;
2386 ret = snprintf(fw_version, fw_size, "%lu.%lu.%lu.%lu",
2387 hns3_get_field(version, HNS3_FW_VERSION_BYTE3_M,
2388 HNS3_FW_VERSION_BYTE3_S),
2389 hns3_get_field(version, HNS3_FW_VERSION_BYTE2_M,
2390 HNS3_FW_VERSION_BYTE2_S),
2391 hns3_get_field(version, HNS3_FW_VERSION_BYTE1_M,
2392 HNS3_FW_VERSION_BYTE1_S),
2393 hns3_get_field(version, HNS3_FW_VERSION_BYTE0_M,
2394 HNS3_FW_VERSION_BYTE0_S));
2398 ret += 1; /* add the size of '\0' */
2399 if (fw_size < (size_t)ret)
2406 hns3_update_port_link_info(struct rte_eth_dev *eth_dev)
2408 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(eth_dev->data->dev_private);
2411 (void)hns3_update_link_status(hw);
2413 ret = hns3_update_link_info(eth_dev);
2415 hw->mac.link_status = RTE_ETH_LINK_DOWN;
2421 hns3_setup_linkstatus(struct rte_eth_dev *eth_dev,
2422 struct rte_eth_link *new_link)
2424 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(eth_dev->data->dev_private);
2425 struct hns3_mac *mac = &hw->mac;
2427 switch (mac->link_speed) {
2428 case RTE_ETH_SPEED_NUM_10M:
2429 case RTE_ETH_SPEED_NUM_100M:
2430 case RTE_ETH_SPEED_NUM_1G:
2431 case RTE_ETH_SPEED_NUM_10G:
2432 case RTE_ETH_SPEED_NUM_25G:
2433 case RTE_ETH_SPEED_NUM_40G:
2434 case RTE_ETH_SPEED_NUM_50G:
2435 case RTE_ETH_SPEED_NUM_100G:
2436 case RTE_ETH_SPEED_NUM_200G:
2437 if (mac->link_status)
2438 new_link->link_speed = mac->link_speed;
2441 if (mac->link_status)
2442 new_link->link_speed = RTE_ETH_SPEED_NUM_UNKNOWN;
2446 if (!mac->link_status)
2447 new_link->link_speed = RTE_ETH_SPEED_NUM_NONE;
2449 new_link->link_duplex = mac->link_duplex;
2450 new_link->link_status = mac->link_status ? RTE_ETH_LINK_UP : RTE_ETH_LINK_DOWN;
2451 new_link->link_autoneg = mac->link_autoneg;
2455 hns3_dev_link_update(struct rte_eth_dev *eth_dev, int wait_to_complete)
2457 #define HNS3_LINK_CHECK_INTERVAL 100 /* 100ms */
2458 #define HNS3_MAX_LINK_CHECK_TIMES 20 /* 2s (100 * 20ms) in total */
2460 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(eth_dev->data->dev_private);
2461 uint32_t retry_cnt = HNS3_MAX_LINK_CHECK_TIMES;
2462 struct hns3_mac *mac = &hw->mac;
2463 struct rte_eth_link new_link;
2466 /* When port is stopped, report link down. */
2467 if (eth_dev->data->dev_started == 0) {
2468 new_link.link_autoneg = mac->link_autoneg;
2469 new_link.link_duplex = mac->link_duplex;
2470 new_link.link_speed = RTE_ETH_SPEED_NUM_NONE;
2471 new_link.link_status = RTE_ETH_LINK_DOWN;
2476 ret = hns3_update_port_link_info(eth_dev);
2478 hns3_err(hw, "failed to get port link info, ret = %d.",
2483 if (!wait_to_complete || mac->link_status == RTE_ETH_LINK_UP)
2486 rte_delay_ms(HNS3_LINK_CHECK_INTERVAL);
2487 } while (retry_cnt--);
2489 memset(&new_link, 0, sizeof(new_link));
2490 hns3_setup_linkstatus(eth_dev, &new_link);
2493 return rte_eth_linkstatus_set(eth_dev, &new_link);
2497 hns3_dev_set_link_up(struct rte_eth_dev *dev)
2499 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2503 * The "tx_pkt_burst" will be restored. But the secondary process does
2504 * not support the mechanism for notifying the primary process.
2506 if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
2507 hns3_err(hw, "secondary process does not support to set link up.");
2512 * If device isn't started Rx/Tx function is still disabled, setting
2513 * link up is not allowed. But it is probably better to return success
2514 * to reduce the impact on the upper layer.
2516 if (hw->adapter_state != HNS3_NIC_STARTED) {
2517 hns3_info(hw, "device isn't started, can't set link up.");
2521 if (!hw->set_link_down)
2524 rte_spinlock_lock(&hw->lock);
2525 ret = hns3_cfg_mac_mode(hw, true);
2527 rte_spinlock_unlock(&hw->lock);
2528 hns3_err(hw, "failed to set link up, ret = %d", ret);
2532 hw->set_link_down = false;
2533 hns3_start_tx_datapath(dev);
2534 rte_spinlock_unlock(&hw->lock);
2540 hns3_dev_set_link_down(struct rte_eth_dev *dev)
2542 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2546 * The "tx_pkt_burst" will be set to dummy function. But the secondary
2547 * process does not support the mechanism for notifying the primary
2550 if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
2551 hns3_err(hw, "secondary process does not support to set link down.");
2556 * If device isn't started or the API has been called, link status is
2557 * down, return success.
2559 if (hw->adapter_state != HNS3_NIC_STARTED || hw->set_link_down)
2562 rte_spinlock_lock(&hw->lock);
2563 hns3_stop_tx_datapath(dev);
2564 ret = hns3_cfg_mac_mode(hw, false);
2566 hns3_start_tx_datapath(dev);
2567 rte_spinlock_unlock(&hw->lock);
2568 hns3_err(hw, "failed to set link down, ret = %d", ret);
2572 hw->set_link_down = true;
2573 rte_spinlock_unlock(&hw->lock);
2579 hns3_parse_func_status(struct hns3_hw *hw, struct hns3_func_status_cmd *status)
2581 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
2582 struct hns3_pf *pf = &hns->pf;
2584 if (!(status->pf_state & HNS3_PF_STATE_DONE))
2587 pf->is_main_pf = (status->pf_state & HNS3_PF_STATE_MAIN) ? true : false;
2593 hns3_query_function_status(struct hns3_hw *hw)
2595 #define HNS3_QUERY_MAX_CNT 10
2596 #define HNS3_QUERY_SLEEP_MSCOEND 1
2597 struct hns3_func_status_cmd *req;
2598 struct hns3_cmd_desc desc;
2602 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_QUERY_FUNC_STATUS, true);
2603 req = (struct hns3_func_status_cmd *)desc.data;
2606 ret = hns3_cmd_send(hw, &desc, 1);
2608 PMD_INIT_LOG(ERR, "query function status failed %d",
2613 /* Check pf reset is done */
2617 rte_delay_ms(HNS3_QUERY_SLEEP_MSCOEND);
2618 } while (timeout++ < HNS3_QUERY_MAX_CNT);
2620 return hns3_parse_func_status(hw, req);
2624 hns3_get_pf_max_tqp_num(struct hns3_hw *hw)
2626 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
2627 struct hns3_pf *pf = &hns->pf;
2629 if (pf->tqp_config_mode == HNS3_FLEX_MAX_TQP_NUM_MODE) {
2631 * The total_tqps_num obtained from firmware is maximum tqp
2632 * numbers of this port, which should be used for PF and VFs.
2633 * There is no need for pf to have so many tqp numbers in
2634 * most cases. RTE_LIBRTE_HNS3_MAX_TQP_NUM_PER_PF,
2635 * coming from config file, is assigned to maximum queue number
2636 * for the PF of this port by user. So users can modify the
2637 * maximum queue number of PF according to their own application
2638 * scenarios, which is more flexible to use. In addition, many
2639 * memories can be saved due to allocating queue statistics
2640 * room according to the actual number of queues required. The
2641 * maximum queue number of PF for network engine with
2642 * revision_id greater than 0x30 is assigned by config file.
2644 if (RTE_LIBRTE_HNS3_MAX_TQP_NUM_PER_PF <= 0) {
2645 hns3_err(hw, "RTE_LIBRTE_HNS3_MAX_TQP_NUM_PER_PF(%d) "
2646 "must be greater than 0.",
2647 RTE_LIBRTE_HNS3_MAX_TQP_NUM_PER_PF);
2651 hw->tqps_num = RTE_MIN(RTE_LIBRTE_HNS3_MAX_TQP_NUM_PER_PF,
2652 hw->total_tqps_num);
2655 * Due to the limitation on the number of PF interrupts
2656 * available, the maximum queue number assigned to PF on
2657 * the network engine with revision_id 0x21 is 64.
2659 hw->tqps_num = RTE_MIN(hw->total_tqps_num,
2660 HNS3_MAX_TQP_NUM_HIP08_PF);
2667 hns3_query_pf_resource(struct hns3_hw *hw)
2669 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
2670 struct hns3_pf *pf = &hns->pf;
2671 struct hns3_pf_res_cmd *req;
2672 struct hns3_cmd_desc desc;
2675 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_QUERY_PF_RSRC, true);
2676 ret = hns3_cmd_send(hw, &desc, 1);
2678 PMD_INIT_LOG(ERR, "query pf resource failed %d", ret);
2682 req = (struct hns3_pf_res_cmd *)desc.data;
2683 hw->total_tqps_num = rte_le_to_cpu_16(req->tqp_num) +
2684 rte_le_to_cpu_16(req->ext_tqp_num);
2685 ret = hns3_get_pf_max_tqp_num(hw);
2689 pf->pkt_buf_size = rte_le_to_cpu_16(req->buf_size) << HNS3_BUF_UNIT_S;
2690 pf->func_num = rte_le_to_cpu_16(req->pf_own_fun_number);
2692 if (req->tx_buf_size)
2694 rte_le_to_cpu_16(req->tx_buf_size) << HNS3_BUF_UNIT_S;
2696 pf->tx_buf_size = HNS3_DEFAULT_TX_BUF;
2698 pf->tx_buf_size = roundup(pf->tx_buf_size, HNS3_BUF_SIZE_UNIT);
2700 if (req->dv_buf_size)
2702 rte_le_to_cpu_16(req->dv_buf_size) << HNS3_BUF_UNIT_S;
2704 pf->dv_buf_size = HNS3_DEFAULT_DV;
2706 pf->dv_buf_size = roundup(pf->dv_buf_size, HNS3_BUF_SIZE_UNIT);
2709 hns3_get_field(rte_le_to_cpu_16(req->nic_pf_intr_vector_number),
2710 HNS3_PF_VEC_NUM_M, HNS3_PF_VEC_NUM_S);
2716 hns3_parse_cfg(struct hns3_cfg *cfg, struct hns3_cmd_desc *desc)
2718 struct hns3_cfg_param_cmd *req;
2719 uint64_t mac_addr_tmp_high;
2720 uint8_t ext_rss_size_max;
2721 uint64_t mac_addr_tmp;
2724 req = (struct hns3_cfg_param_cmd *)desc[0].data;
2726 /* get the configuration */
2727 cfg->tc_num = hns3_get_field(rte_le_to_cpu_32(req->param[0]),
2728 HNS3_CFG_TC_NUM_M, HNS3_CFG_TC_NUM_S);
2729 cfg->tqp_desc_num = hns3_get_field(rte_le_to_cpu_32(req->param[0]),
2730 HNS3_CFG_TQP_DESC_N_M,
2731 HNS3_CFG_TQP_DESC_N_S);
2733 cfg->phy_addr = hns3_get_field(rte_le_to_cpu_32(req->param[1]),
2734 HNS3_CFG_PHY_ADDR_M,
2735 HNS3_CFG_PHY_ADDR_S);
2736 cfg->media_type = hns3_get_field(rte_le_to_cpu_32(req->param[1]),
2737 HNS3_CFG_MEDIA_TP_M,
2738 HNS3_CFG_MEDIA_TP_S);
2739 cfg->rx_buf_len = hns3_get_field(rte_le_to_cpu_32(req->param[1]),
2740 HNS3_CFG_RX_BUF_LEN_M,
2741 HNS3_CFG_RX_BUF_LEN_S);
2742 /* get mac address */
2743 mac_addr_tmp = rte_le_to_cpu_32(req->param[2]);
2744 mac_addr_tmp_high = hns3_get_field(rte_le_to_cpu_32(req->param[3]),
2745 HNS3_CFG_MAC_ADDR_H_M,
2746 HNS3_CFG_MAC_ADDR_H_S);
2748 mac_addr_tmp |= (mac_addr_tmp_high << 31) << 1;
2750 cfg->default_speed = hns3_get_field(rte_le_to_cpu_32(req->param[3]),
2751 HNS3_CFG_DEFAULT_SPEED_M,
2752 HNS3_CFG_DEFAULT_SPEED_S);
2753 cfg->rss_size_max = hns3_get_field(rte_le_to_cpu_32(req->param[3]),
2754 HNS3_CFG_RSS_SIZE_M,
2755 HNS3_CFG_RSS_SIZE_S);
2757 for (i = 0; i < RTE_ETHER_ADDR_LEN; i++)
2758 cfg->mac_addr[i] = (mac_addr_tmp >> (8 * i)) & 0xff;
2760 req = (struct hns3_cfg_param_cmd *)desc[1].data;
2761 cfg->numa_node_map = rte_le_to_cpu_32(req->param[0]);
2763 cfg->speed_ability = hns3_get_field(rte_le_to_cpu_32(req->param[1]),
2764 HNS3_CFG_SPEED_ABILITY_M,
2765 HNS3_CFG_SPEED_ABILITY_S);
2766 cfg->umv_space = hns3_get_field(rte_le_to_cpu_32(req->param[1]),
2767 HNS3_CFG_UMV_TBL_SPACE_M,
2768 HNS3_CFG_UMV_TBL_SPACE_S);
2769 if (!cfg->umv_space)
2770 cfg->umv_space = HNS3_DEFAULT_UMV_SPACE_PER_PF;
2772 ext_rss_size_max = hns3_get_field(rte_le_to_cpu_32(req->param[2]),
2773 HNS3_CFG_EXT_RSS_SIZE_M,
2774 HNS3_CFG_EXT_RSS_SIZE_S);
2776 * Field ext_rss_size_max obtained from firmware will be more flexible
2777 * for future changes and expansions, which is an exponent of 2, instead
2778 * of reading out directly. If this field is not zero, hns3 PF PMD
2779 * driver uses it as rss_size_max under one TC. Device, whose revision
2780 * id is greater than or equal to PCI_REVISION_ID_HIP09_A, obtains the
2781 * maximum number of queues supported under a TC through this field.
2783 if (ext_rss_size_max)
2784 cfg->rss_size_max = 1U << ext_rss_size_max;
2787 /* hns3_get_board_cfg: query the static parameter from NCL_config file in flash
2788 * @hw: pointer to struct hns3_hw
2789 * @hcfg: the config structure to be getted
2792 hns3_get_board_cfg(struct hns3_hw *hw, struct hns3_cfg *hcfg)
2794 struct hns3_cmd_desc desc[HNS3_PF_CFG_DESC_NUM];
2795 struct hns3_cfg_param_cmd *req;
2800 for (i = 0; i < HNS3_PF_CFG_DESC_NUM; i++) {
2802 req = (struct hns3_cfg_param_cmd *)desc[i].data;
2803 hns3_cmd_setup_basic_desc(&desc[i], HNS3_OPC_GET_CFG_PARAM,
2805 hns3_set_field(offset, HNS3_CFG_OFFSET_M, HNS3_CFG_OFFSET_S,
2806 i * HNS3_CFG_RD_LEN_BYTES);
2807 /* Len should be divided by 4 when send to hardware */
2808 hns3_set_field(offset, HNS3_CFG_RD_LEN_M, HNS3_CFG_RD_LEN_S,
2809 HNS3_CFG_RD_LEN_BYTES / HNS3_CFG_RD_LEN_UNIT);
2810 req->offset = rte_cpu_to_le_32(offset);
2813 ret = hns3_cmd_send(hw, desc, HNS3_PF_CFG_DESC_NUM);
2815 PMD_INIT_LOG(ERR, "get config failed %d.", ret);
2819 hns3_parse_cfg(hcfg, desc);
2825 hns3_parse_speed(int speed_cmd, uint32_t *speed)
2827 switch (speed_cmd) {
2828 case HNS3_CFG_SPEED_10M:
2829 *speed = RTE_ETH_SPEED_NUM_10M;
2831 case HNS3_CFG_SPEED_100M:
2832 *speed = RTE_ETH_SPEED_NUM_100M;
2834 case HNS3_CFG_SPEED_1G:
2835 *speed = RTE_ETH_SPEED_NUM_1G;
2837 case HNS3_CFG_SPEED_10G:
2838 *speed = RTE_ETH_SPEED_NUM_10G;
2840 case HNS3_CFG_SPEED_25G:
2841 *speed = RTE_ETH_SPEED_NUM_25G;
2843 case HNS3_CFG_SPEED_40G:
2844 *speed = RTE_ETH_SPEED_NUM_40G;
2846 case HNS3_CFG_SPEED_50G:
2847 *speed = RTE_ETH_SPEED_NUM_50G;
2849 case HNS3_CFG_SPEED_100G:
2850 *speed = RTE_ETH_SPEED_NUM_100G;
2852 case HNS3_CFG_SPEED_200G:
2853 *speed = RTE_ETH_SPEED_NUM_200G;
2863 hns3_set_default_dev_specifications(struct hns3_hw *hw)
2865 hw->max_non_tso_bd_num = HNS3_MAX_NON_TSO_BD_PER_PKT;
2866 hw->rss_ind_tbl_size = HNS3_RSS_IND_TBL_SIZE;
2867 hw->rss_key_size = HNS3_RSS_KEY_SIZE;
2868 hw->max_tm_rate = HNS3_ETHER_MAX_RATE;
2869 hw->intr.int_ql_max = HNS3_INTR_QL_NONE;
2873 hns3_parse_dev_specifications(struct hns3_hw *hw, struct hns3_cmd_desc *desc)
2875 struct hns3_dev_specs_0_cmd *req0;
2877 req0 = (struct hns3_dev_specs_0_cmd *)desc[0].data;
2879 hw->max_non_tso_bd_num = req0->max_non_tso_bd_num;
2880 hw->rss_ind_tbl_size = rte_le_to_cpu_16(req0->rss_ind_tbl_size);
2881 hw->rss_key_size = rte_le_to_cpu_16(req0->rss_key_size);
2882 hw->max_tm_rate = rte_le_to_cpu_32(req0->max_tm_rate);
2883 hw->intr.int_ql_max = rte_le_to_cpu_16(req0->intr_ql_max);
2887 hns3_check_dev_specifications(struct hns3_hw *hw)
2889 if (hw->rss_ind_tbl_size == 0 ||
2890 hw->rss_ind_tbl_size > HNS3_RSS_IND_TBL_SIZE_MAX) {
2891 hns3_err(hw, "the size of hash lookup table configured (%u)"
2892 " exceeds the maximum(%u)", hw->rss_ind_tbl_size,
2893 HNS3_RSS_IND_TBL_SIZE_MAX);
2901 hns3_query_dev_specifications(struct hns3_hw *hw)
2903 struct hns3_cmd_desc desc[HNS3_QUERY_DEV_SPECS_BD_NUM];
2907 for (i = 0; i < HNS3_QUERY_DEV_SPECS_BD_NUM - 1; i++) {
2908 hns3_cmd_setup_basic_desc(&desc[i], HNS3_OPC_QUERY_DEV_SPECS,
2910 desc[i].flag |= rte_cpu_to_le_16(HNS3_CMD_FLAG_NEXT);
2912 hns3_cmd_setup_basic_desc(&desc[i], HNS3_OPC_QUERY_DEV_SPECS, true);
2914 ret = hns3_cmd_send(hw, desc, HNS3_QUERY_DEV_SPECS_BD_NUM);
2918 hns3_parse_dev_specifications(hw, desc);
2920 return hns3_check_dev_specifications(hw);
2924 hns3_get_capability(struct hns3_hw *hw)
2926 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
2927 struct rte_pci_device *pci_dev;
2928 struct hns3_pf *pf = &hns->pf;
2929 struct rte_eth_dev *eth_dev;
2934 eth_dev = &rte_eth_devices[hw->data->port_id];
2935 pci_dev = RTE_ETH_DEV_TO_PCI(eth_dev);
2936 device_id = pci_dev->id.device_id;
2938 if (device_id == HNS3_DEV_ID_25GE_RDMA ||
2939 device_id == HNS3_DEV_ID_50GE_RDMA ||
2940 device_id == HNS3_DEV_ID_100G_RDMA_MACSEC ||
2941 device_id == HNS3_DEV_ID_200G_RDMA)
2942 hns3_set_bit(hw->capability, HNS3_DEV_SUPPORT_DCB_B, 1);
2944 /* Get PCI revision id */
2945 ret = rte_pci_read_config(pci_dev, &revision, HNS3_PCI_REVISION_ID_LEN,
2946 HNS3_PCI_REVISION_ID);
2947 if (ret != HNS3_PCI_REVISION_ID_LEN) {
2948 PMD_INIT_LOG(ERR, "failed to read pci revision id, ret = %d",
2952 hw->revision = revision;
2954 if (revision < PCI_REVISION_ID_HIP09_A) {
2955 hns3_set_default_dev_specifications(hw);
2956 hw->intr.mapping_mode = HNS3_INTR_MAPPING_VEC_RSV_ONE;
2957 hw->intr.gl_unit = HNS3_INTR_COALESCE_GL_UINT_2US;
2958 hw->tso_mode = HNS3_TSO_SW_CAL_PSEUDO_H_CSUM;
2959 hw->vlan_mode = HNS3_SW_SHIFT_AND_DISCARD_MODE;
2960 hw->drop_stats_mode = HNS3_PKTS_DROP_STATS_MODE1;
2961 hw->min_tx_pkt_len = HNS3_HIP08_MIN_TX_PKT_LEN;
2962 pf->tqp_config_mode = HNS3_FIXED_MAX_TQP_NUM_MODE;
2963 hw->rss_info.ipv6_sctp_offload_supported = false;
2964 hw->udp_cksum_mode = HNS3_SPECIAL_PORT_SW_CKSUM_MODE;
2965 pf->support_multi_tc_pause = false;
2969 ret = hns3_query_dev_specifications(hw);
2972 "failed to query dev specifications, ret = %d",
2977 hw->intr.mapping_mode = HNS3_INTR_MAPPING_VEC_ALL;
2978 hw->intr.gl_unit = HNS3_INTR_COALESCE_GL_UINT_1US;
2979 hw->tso_mode = HNS3_TSO_HW_CAL_PSEUDO_H_CSUM;
2980 hw->vlan_mode = HNS3_HW_SHIFT_AND_DISCARD_MODE;
2981 hw->drop_stats_mode = HNS3_PKTS_DROP_STATS_MODE2;
2982 hw->min_tx_pkt_len = HNS3_HIP09_MIN_TX_PKT_LEN;
2983 pf->tqp_config_mode = HNS3_FLEX_MAX_TQP_NUM_MODE;
2984 hw->rss_info.ipv6_sctp_offload_supported = true;
2985 hw->udp_cksum_mode = HNS3_SPECIAL_PORT_HW_CKSUM_MODE;
2986 pf->support_multi_tc_pause = true;
2992 hns3_check_media_type(struct hns3_hw *hw, uint8_t media_type)
2996 switch (media_type) {
2997 case HNS3_MEDIA_TYPE_COPPER:
2998 if (!hns3_dev_get_support(hw, COPPER)) {
3000 "Media type is copper, not supported.");
3006 case HNS3_MEDIA_TYPE_FIBER:
3009 case HNS3_MEDIA_TYPE_BACKPLANE:
3010 PMD_INIT_LOG(ERR, "Media type is Backplane, not supported.");
3014 PMD_INIT_LOG(ERR, "Unknown media type = %u!", media_type);
3023 hns3_get_board_configuration(struct hns3_hw *hw)
3025 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
3026 struct hns3_pf *pf = &hns->pf;
3027 struct hns3_cfg cfg;
3030 ret = hns3_get_board_cfg(hw, &cfg);
3032 PMD_INIT_LOG(ERR, "get board config failed %d", ret);
3036 ret = hns3_check_media_type(hw, cfg.media_type);
3040 hw->mac.media_type = cfg.media_type;
3041 hw->rss_size_max = cfg.rss_size_max;
3042 hw->rss_dis_flag = false;
3043 memcpy(hw->mac.mac_addr, cfg.mac_addr, RTE_ETHER_ADDR_LEN);
3044 hw->mac.phy_addr = cfg.phy_addr;
3045 hw->num_tx_desc = cfg.tqp_desc_num;
3046 hw->num_rx_desc = cfg.tqp_desc_num;
3047 hw->dcb_info.num_pg = 1;
3048 hw->dcb_info.hw_pfc_map = 0;
3050 ret = hns3_parse_speed(cfg.default_speed, &hw->mac.link_speed);
3052 PMD_INIT_LOG(ERR, "Get wrong speed %u, ret = %d",
3053 cfg.default_speed, ret);
3057 pf->tc_max = cfg.tc_num;
3058 if (pf->tc_max > HNS3_MAX_TC_NUM || pf->tc_max < 1) {
3059 PMD_INIT_LOG(WARNING,
3060 "Get TC num(%u) from flash, set TC num to 1",
3065 /* Dev does not support DCB */
3066 if (!hns3_dev_get_support(hw, DCB)) {
3070 pf->pfc_max = pf->tc_max;
3072 hw->dcb_info.num_tc = 1;
3073 hw->alloc_rss_size = RTE_MIN(hw->rss_size_max,
3074 hw->tqps_num / hw->dcb_info.num_tc);
3075 hns3_set_bit(hw->hw_tc_map, 0, 1);
3076 pf->tx_sch_mode = HNS3_FLAG_TC_BASE_SCH_MODE;
3078 pf->wanted_umv_size = cfg.umv_space;
3084 hns3_get_configuration(struct hns3_hw *hw)
3088 ret = hns3_query_function_status(hw);
3090 PMD_INIT_LOG(ERR, "Failed to query function status: %d.", ret);
3094 /* Get device capability */
3095 ret = hns3_get_capability(hw);
3097 PMD_INIT_LOG(ERR, "failed to get device capability: %d.", ret);
3101 /* Get pf resource */
3102 ret = hns3_query_pf_resource(hw);
3104 PMD_INIT_LOG(ERR, "Failed to query pf resource: %d", ret);
3108 ret = hns3_get_board_configuration(hw);
3110 PMD_INIT_LOG(ERR, "failed to get board configuration: %d", ret);
3114 ret = hns3_query_dev_fec_info(hw);
3117 "failed to query FEC information, ret = %d", ret);
3123 hns3_map_tqps_to_func(struct hns3_hw *hw, uint16_t func_id, uint16_t tqp_pid,
3124 uint16_t tqp_vid, bool is_pf)
3126 struct hns3_tqp_map_cmd *req;
3127 struct hns3_cmd_desc desc;
3130 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_SET_TQP_MAP, false);
3132 req = (struct hns3_tqp_map_cmd *)desc.data;
3133 req->tqp_id = rte_cpu_to_le_16(tqp_pid);
3134 req->tqp_vf = func_id;
3135 req->tqp_flag = 1 << HNS3_TQP_MAP_EN_B;
3137 req->tqp_flag |= (1 << HNS3_TQP_MAP_TYPE_B);
3138 req->tqp_vid = rte_cpu_to_le_16(tqp_vid);
3140 ret = hns3_cmd_send(hw, &desc, 1);
3142 PMD_INIT_LOG(ERR, "TQP map failed %d", ret);
3148 hns3_map_tqp(struct hns3_hw *hw)
3154 * In current version, VF is not supported when PF is driven by DPDK
3155 * driver, so we assign total tqps_num tqps allocated to this port
3158 for (i = 0; i < hw->total_tqps_num; i++) {
3159 ret = hns3_map_tqps_to_func(hw, HNS3_PF_FUNC_ID, i, i, true);
3168 hns3_cfg_mac_speed_dup_hw(struct hns3_hw *hw, uint32_t speed, uint8_t duplex)
3170 struct hns3_config_mac_speed_dup_cmd *req;
3171 struct hns3_cmd_desc desc;
3174 req = (struct hns3_config_mac_speed_dup_cmd *)desc.data;
3176 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_CONFIG_SPEED_DUP, false);
3178 hns3_set_bit(req->speed_dup, HNS3_CFG_DUPLEX_B, !!duplex ? 1 : 0);
3181 case RTE_ETH_SPEED_NUM_10M:
3182 hns3_set_field(req->speed_dup, HNS3_CFG_SPEED_M,
3183 HNS3_CFG_SPEED_S, HNS3_CFG_SPEED_10M);
3185 case RTE_ETH_SPEED_NUM_100M:
3186 hns3_set_field(req->speed_dup, HNS3_CFG_SPEED_M,
3187 HNS3_CFG_SPEED_S, HNS3_CFG_SPEED_100M);
3189 case RTE_ETH_SPEED_NUM_1G:
3190 hns3_set_field(req->speed_dup, HNS3_CFG_SPEED_M,
3191 HNS3_CFG_SPEED_S, HNS3_CFG_SPEED_1G);
3193 case RTE_ETH_SPEED_NUM_10G:
3194 hns3_set_field(req->speed_dup, HNS3_CFG_SPEED_M,
3195 HNS3_CFG_SPEED_S, HNS3_CFG_SPEED_10G);
3197 case RTE_ETH_SPEED_NUM_25G:
3198 hns3_set_field(req->speed_dup, HNS3_CFG_SPEED_M,
3199 HNS3_CFG_SPEED_S, HNS3_CFG_SPEED_25G);
3201 case RTE_ETH_SPEED_NUM_40G:
3202 hns3_set_field(req->speed_dup, HNS3_CFG_SPEED_M,
3203 HNS3_CFG_SPEED_S, HNS3_CFG_SPEED_40G);
3205 case RTE_ETH_SPEED_NUM_50G:
3206 hns3_set_field(req->speed_dup, HNS3_CFG_SPEED_M,
3207 HNS3_CFG_SPEED_S, HNS3_CFG_SPEED_50G);
3209 case RTE_ETH_SPEED_NUM_100G:
3210 hns3_set_field(req->speed_dup, HNS3_CFG_SPEED_M,
3211 HNS3_CFG_SPEED_S, HNS3_CFG_SPEED_100G);
3213 case RTE_ETH_SPEED_NUM_200G:
3214 hns3_set_field(req->speed_dup, HNS3_CFG_SPEED_M,
3215 HNS3_CFG_SPEED_S, HNS3_CFG_SPEED_200G);
3218 PMD_INIT_LOG(ERR, "invalid speed (%u)", speed);
3222 hns3_set_bit(req->mac_change_fec_en, HNS3_CFG_MAC_SPEED_CHANGE_EN_B, 1);
3224 ret = hns3_cmd_send(hw, &desc, 1);
3226 PMD_INIT_LOG(ERR, "mac speed/duplex config cmd failed %d", ret);
3232 hns3_tx_buffer_calc(struct hns3_hw *hw, struct hns3_pkt_buf_alloc *buf_alloc)
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 i, total_size;
3239 total_size = pf->pkt_buf_size;
3241 /* alloc tx buffer for all enabled tc */
3242 for (i = 0; i < HNS3_MAX_TC_NUM; i++) {
3243 priv = &buf_alloc->priv_buf[i];
3245 if (hw->hw_tc_map & BIT(i)) {
3246 if (total_size < pf->tx_buf_size)
3249 priv->tx_buf_size = pf->tx_buf_size;
3251 priv->tx_buf_size = 0;
3253 total_size -= priv->tx_buf_size;
3260 hns3_tx_buffer_alloc(struct hns3_hw *hw, struct hns3_pkt_buf_alloc *buf_alloc)
3262 /* TX buffer size is unit by 128 byte */
3263 #define HNS3_BUF_SIZE_UNIT_SHIFT 7
3264 #define HNS3_BUF_SIZE_UPDATE_EN_MSK BIT(15)
3265 struct hns3_tx_buff_alloc_cmd *req;
3266 struct hns3_cmd_desc desc;
3271 req = (struct hns3_tx_buff_alloc_cmd *)desc.data;
3273 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_TX_BUFF_ALLOC, 0);
3274 for (i = 0; i < HNS3_MAX_TC_NUM; i++) {
3275 buf_size = buf_alloc->priv_buf[i].tx_buf_size;
3277 buf_size = buf_size >> HNS3_BUF_SIZE_UNIT_SHIFT;
3278 req->tx_pkt_buff[i] = rte_cpu_to_le_16(buf_size |
3279 HNS3_BUF_SIZE_UPDATE_EN_MSK);
3282 ret = hns3_cmd_send(hw, &desc, 1);
3284 PMD_INIT_LOG(ERR, "tx buffer alloc cmd failed %d", ret);
3290 hns3_get_tc_num(struct hns3_hw *hw)
3295 for (i = 0; i < HNS3_MAX_TC_NUM; i++)
3296 if (hw->hw_tc_map & BIT(i))
3302 hns3_get_rx_priv_buff_alloced(struct hns3_pkt_buf_alloc *buf_alloc)
3304 struct hns3_priv_buf *priv;
3305 uint32_t rx_priv = 0;
3308 for (i = 0; i < HNS3_MAX_TC_NUM; i++) {
3309 priv = &buf_alloc->priv_buf[i];
3311 rx_priv += priv->buf_size;
3317 hns3_get_tx_buff_alloced(struct hns3_pkt_buf_alloc *buf_alloc)
3319 uint32_t total_tx_size = 0;
3322 for (i = 0; i < HNS3_MAX_TC_NUM; i++)
3323 total_tx_size += buf_alloc->priv_buf[i].tx_buf_size;
3325 return total_tx_size;
3328 /* Get the number of pfc enabled TCs, which have private buffer */
3330 hns3_get_pfc_priv_num(struct hns3_hw *hw, struct hns3_pkt_buf_alloc *buf_alloc)
3332 struct hns3_priv_buf *priv;
3336 for (i = 0; i < HNS3_MAX_TC_NUM; i++) {
3337 priv = &buf_alloc->priv_buf[i];
3338 if ((hw->dcb_info.hw_pfc_map & BIT(i)) && priv->enable)
3345 /* Get the number of pfc disabled TCs, which have private buffer */
3347 hns3_get_no_pfc_priv_num(struct hns3_hw *hw,
3348 struct hns3_pkt_buf_alloc *buf_alloc)
3350 struct hns3_priv_buf *priv;
3354 for (i = 0; i < HNS3_MAX_TC_NUM; i++) {
3355 priv = &buf_alloc->priv_buf[i];
3356 if (hw->hw_tc_map & BIT(i) &&
3357 !(hw->dcb_info.hw_pfc_map & BIT(i)) && priv->enable)
3365 hns3_is_rx_buf_ok(struct hns3_hw *hw, struct hns3_pkt_buf_alloc *buf_alloc,
3368 uint32_t shared_buf_min, shared_buf_tc, shared_std, hi_thrd, lo_thrd;
3369 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
3370 struct hns3_pf *pf = &hns->pf;
3371 uint32_t shared_buf, aligned_mps;
3376 tc_num = hns3_get_tc_num(hw);
3377 aligned_mps = roundup(pf->mps, HNS3_BUF_SIZE_UNIT);
3379 if (hns3_dev_get_support(hw, DCB))
3380 shared_buf_min = HNS3_BUF_MUL_BY * aligned_mps +
3383 shared_buf_min = aligned_mps + HNS3_NON_DCB_ADDITIONAL_BUF
3386 shared_buf_tc = tc_num * aligned_mps + aligned_mps;
3387 shared_std = roundup(RTE_MAX(shared_buf_min, shared_buf_tc),
3388 HNS3_BUF_SIZE_UNIT);
3390 rx_priv = hns3_get_rx_priv_buff_alloced(buf_alloc);
3391 if (rx_all < rx_priv + shared_std)
3394 shared_buf = rounddown(rx_all - rx_priv, HNS3_BUF_SIZE_UNIT);
3395 buf_alloc->s_buf.buf_size = shared_buf;
3396 if (hns3_dev_get_support(hw, DCB)) {
3397 buf_alloc->s_buf.self.high = shared_buf - pf->dv_buf_size;
3398 buf_alloc->s_buf.self.low = buf_alloc->s_buf.self.high
3399 - roundup(aligned_mps / HNS3_BUF_DIV_BY,
3400 HNS3_BUF_SIZE_UNIT);
3402 buf_alloc->s_buf.self.high =
3403 aligned_mps + HNS3_NON_DCB_ADDITIONAL_BUF;
3404 buf_alloc->s_buf.self.low = aligned_mps;
3407 if (hns3_dev_get_support(hw, DCB)) {
3408 hi_thrd = shared_buf - pf->dv_buf_size;
3410 if (tc_num <= NEED_RESERVE_TC_NUM)
3411 hi_thrd = hi_thrd * BUF_RESERVE_PERCENT /
3415 hi_thrd = hi_thrd / tc_num;
3417 hi_thrd = RTE_MAX(hi_thrd, HNS3_BUF_MUL_BY * aligned_mps);
3418 hi_thrd = rounddown(hi_thrd, HNS3_BUF_SIZE_UNIT);
3419 lo_thrd = hi_thrd - aligned_mps / HNS3_BUF_DIV_BY;
3421 hi_thrd = aligned_mps + HNS3_NON_DCB_ADDITIONAL_BUF;
3422 lo_thrd = aligned_mps;
3425 for (i = 0; i < HNS3_MAX_TC_NUM; i++) {
3426 buf_alloc->s_buf.tc_thrd[i].low = lo_thrd;
3427 buf_alloc->s_buf.tc_thrd[i].high = hi_thrd;
3434 hns3_rx_buf_calc_all(struct hns3_hw *hw, bool max,
3435 struct hns3_pkt_buf_alloc *buf_alloc)
3437 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
3438 struct hns3_pf *pf = &hns->pf;
3439 struct hns3_priv_buf *priv;
3440 uint32_t aligned_mps;
3444 rx_all = pf->pkt_buf_size - hns3_get_tx_buff_alloced(buf_alloc);
3445 aligned_mps = roundup(pf->mps, HNS3_BUF_SIZE_UNIT);
3447 for (i = 0; i < HNS3_MAX_TC_NUM; i++) {
3448 priv = &buf_alloc->priv_buf[i];
3455 if (!(hw->hw_tc_map & BIT(i)))
3459 if (hw->dcb_info.hw_pfc_map & BIT(i)) {
3460 priv->wl.low = max ? aligned_mps : HNS3_BUF_SIZE_UNIT;
3461 priv->wl.high = roundup(priv->wl.low + aligned_mps,
3462 HNS3_BUF_SIZE_UNIT);
3465 priv->wl.high = max ? (aligned_mps * HNS3_BUF_MUL_BY) :
3469 priv->buf_size = priv->wl.high + pf->dv_buf_size;
3472 return hns3_is_rx_buf_ok(hw, buf_alloc, rx_all);
3476 hns3_drop_nopfc_buf_till_fit(struct hns3_hw *hw,
3477 struct hns3_pkt_buf_alloc *buf_alloc)
3479 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
3480 struct hns3_pf *pf = &hns->pf;
3481 struct hns3_priv_buf *priv;
3482 int no_pfc_priv_num;
3487 rx_all = pf->pkt_buf_size - hns3_get_tx_buff_alloced(buf_alloc);
3488 no_pfc_priv_num = hns3_get_no_pfc_priv_num(hw, buf_alloc);
3490 /* let the last to be cleared first */
3491 for (i = HNS3_MAX_TC_NUM - 1; i >= 0; i--) {
3492 priv = &buf_alloc->priv_buf[i];
3493 mask = BIT((uint8_t)i);
3494 if (hw->hw_tc_map & mask &&
3495 !(hw->dcb_info.hw_pfc_map & mask)) {
3496 /* Clear the no pfc TC private buffer */
3504 if (hns3_is_rx_buf_ok(hw, buf_alloc, rx_all) ||
3505 no_pfc_priv_num == 0)
3509 return hns3_is_rx_buf_ok(hw, buf_alloc, rx_all);
3513 hns3_drop_pfc_buf_till_fit(struct hns3_hw *hw,
3514 struct hns3_pkt_buf_alloc *buf_alloc)
3516 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
3517 struct hns3_pf *pf = &hns->pf;
3518 struct hns3_priv_buf *priv;
3524 rx_all = pf->pkt_buf_size - hns3_get_tx_buff_alloced(buf_alloc);
3525 pfc_priv_num = hns3_get_pfc_priv_num(hw, buf_alloc);
3527 /* let the last to be cleared first */
3528 for (i = HNS3_MAX_TC_NUM - 1; i >= 0; i--) {
3529 priv = &buf_alloc->priv_buf[i];
3530 mask = BIT((uint8_t)i);
3531 if (hw->hw_tc_map & mask && hw->dcb_info.hw_pfc_map & mask) {
3532 /* Reduce the number of pfc TC with private buffer */
3539 if (hns3_is_rx_buf_ok(hw, buf_alloc, rx_all) ||
3544 return hns3_is_rx_buf_ok(hw, buf_alloc, rx_all);
3548 hns3_only_alloc_priv_buff(struct hns3_hw *hw,
3549 struct hns3_pkt_buf_alloc *buf_alloc)
3551 #define COMPENSATE_BUFFER 0x3C00
3552 #define COMPENSATE_HALF_MPS_NUM 5
3553 #define PRIV_WL_GAP 0x1800
3554 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
3555 struct hns3_pf *pf = &hns->pf;
3556 uint32_t tc_num = hns3_get_tc_num(hw);
3557 uint32_t half_mps = pf->mps >> 1;
3558 struct hns3_priv_buf *priv;
3559 uint32_t min_rx_priv;
3563 rx_priv = pf->pkt_buf_size - hns3_get_tx_buff_alloced(buf_alloc);
3565 rx_priv = rx_priv / tc_num;
3567 if (tc_num <= NEED_RESERVE_TC_NUM)
3568 rx_priv = rx_priv * BUF_RESERVE_PERCENT / BUF_MAX_PERCENT;
3571 * Minimum value of private buffer in rx direction (min_rx_priv) is
3572 * equal to "DV + 2.5 * MPS + 15KB". Driver only allocates rx private
3573 * buffer if rx_priv is greater than min_rx_priv.
3575 min_rx_priv = pf->dv_buf_size + COMPENSATE_BUFFER +
3576 COMPENSATE_HALF_MPS_NUM * half_mps;
3577 min_rx_priv = roundup(min_rx_priv, HNS3_BUF_SIZE_UNIT);
3578 rx_priv = rounddown(rx_priv, HNS3_BUF_SIZE_UNIT);
3579 if (rx_priv < min_rx_priv)
3582 for (i = 0; i < HNS3_MAX_TC_NUM; i++) {
3583 priv = &buf_alloc->priv_buf[i];
3589 if (!(hw->hw_tc_map & BIT(i)))
3593 priv->buf_size = rx_priv;
3594 priv->wl.high = rx_priv - pf->dv_buf_size;
3595 priv->wl.low = priv->wl.high - PRIV_WL_GAP;
3598 buf_alloc->s_buf.buf_size = 0;
3604 * hns3_rx_buffer_calc: calculate the rx private buffer size for all TCs
3605 * @hw: pointer to struct hns3_hw
3606 * @buf_alloc: pointer to buffer calculation data
3607 * @return: 0: calculate sucessful, negative: fail
3610 hns3_rx_buffer_calc(struct hns3_hw *hw, struct hns3_pkt_buf_alloc *buf_alloc)
3612 /* When DCB is not supported, rx private buffer is not allocated. */
3613 if (!hns3_dev_get_support(hw, DCB)) {
3614 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
3615 struct hns3_pf *pf = &hns->pf;
3616 uint32_t rx_all = pf->pkt_buf_size;
3618 rx_all -= hns3_get_tx_buff_alloced(buf_alloc);
3619 if (!hns3_is_rx_buf_ok(hw, buf_alloc, rx_all))
3626 * Try to allocate privated packet buffer for all TCs without share
3629 if (hns3_only_alloc_priv_buff(hw, buf_alloc))
3633 * Try to allocate privated packet buffer for all TCs with share
3636 if (hns3_rx_buf_calc_all(hw, true, buf_alloc))
3640 * For different application scenes, the enabled port number, TC number
3641 * and no_drop TC number are different. In order to obtain the better
3642 * performance, software could allocate the buffer size and configure
3643 * the waterline by trying to decrease the private buffer size according
3644 * to the order, namely, waterline of valid tc, pfc disabled tc, pfc
3647 if (hns3_rx_buf_calc_all(hw, false, buf_alloc))
3650 if (hns3_drop_nopfc_buf_till_fit(hw, buf_alloc))
3653 if (hns3_drop_pfc_buf_till_fit(hw, buf_alloc))
3660 hns3_rx_priv_buf_alloc(struct hns3_hw *hw, struct hns3_pkt_buf_alloc *buf_alloc)
3662 struct hns3_rx_priv_buff_cmd *req;
3663 struct hns3_cmd_desc desc;
3668 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_RX_PRIV_BUFF_ALLOC, false);
3669 req = (struct hns3_rx_priv_buff_cmd *)desc.data;
3671 /* Alloc private buffer TCs */
3672 for (i = 0; i < HNS3_MAX_TC_NUM; i++) {
3673 struct hns3_priv_buf *priv = &buf_alloc->priv_buf[i];
3676 rte_cpu_to_le_16(priv->buf_size >> HNS3_BUF_UNIT_S);
3677 req->buf_num[i] |= rte_cpu_to_le_16(1 << HNS3_TC0_PRI_BUF_EN_B);
3680 buf_size = buf_alloc->s_buf.buf_size;
3681 req->shared_buf = rte_cpu_to_le_16((buf_size >> HNS3_BUF_UNIT_S) |
3682 (1 << HNS3_TC0_PRI_BUF_EN_B));
3684 ret = hns3_cmd_send(hw, &desc, 1);
3686 PMD_INIT_LOG(ERR, "rx private buffer alloc cmd failed %d", ret);
3692 hns3_rx_priv_wl_config(struct hns3_hw *hw, struct hns3_pkt_buf_alloc *buf_alloc)
3694 #define HNS3_RX_PRIV_WL_ALLOC_DESC_NUM 2
3695 struct hns3_rx_priv_wl_buf *req;
3696 struct hns3_priv_buf *priv;
3697 struct hns3_cmd_desc desc[HNS3_RX_PRIV_WL_ALLOC_DESC_NUM];
3701 for (i = 0; i < HNS3_RX_PRIV_WL_ALLOC_DESC_NUM; i++) {
3702 hns3_cmd_setup_basic_desc(&desc[i], HNS3_OPC_RX_PRIV_WL_ALLOC,
3704 req = (struct hns3_rx_priv_wl_buf *)desc[i].data;
3706 /* The first descriptor set the NEXT bit to 1 */
3708 desc[i].flag |= rte_cpu_to_le_16(HNS3_CMD_FLAG_NEXT);
3710 desc[i].flag &= ~rte_cpu_to_le_16(HNS3_CMD_FLAG_NEXT);
3712 for (j = 0; j < HNS3_TC_NUM_ONE_DESC; j++) {
3713 uint32_t idx = i * HNS3_TC_NUM_ONE_DESC + j;
3715 priv = &buf_alloc->priv_buf[idx];
3716 req->tc_wl[j].high = rte_cpu_to_le_16(priv->wl.high >>
3718 req->tc_wl[j].high |=
3719 rte_cpu_to_le_16(BIT(HNS3_RX_PRIV_EN_B));
3720 req->tc_wl[j].low = rte_cpu_to_le_16(priv->wl.low >>
3722 req->tc_wl[j].low |=
3723 rte_cpu_to_le_16(BIT(HNS3_RX_PRIV_EN_B));
3727 /* Send 2 descriptor at one time */
3728 ret = hns3_cmd_send(hw, desc, HNS3_RX_PRIV_WL_ALLOC_DESC_NUM);
3730 PMD_INIT_LOG(ERR, "rx private waterline config cmd failed %d",
3736 hns3_common_thrd_config(struct hns3_hw *hw,
3737 struct hns3_pkt_buf_alloc *buf_alloc)
3739 #define HNS3_RX_COM_THRD_ALLOC_DESC_NUM 2
3740 struct hns3_shared_buf *s_buf = &buf_alloc->s_buf;
3741 struct hns3_rx_com_thrd *req;
3742 struct hns3_cmd_desc desc[HNS3_RX_COM_THRD_ALLOC_DESC_NUM];
3743 struct hns3_tc_thrd *tc;
3748 for (i = 0; i < HNS3_RX_COM_THRD_ALLOC_DESC_NUM; i++) {
3749 hns3_cmd_setup_basic_desc(&desc[i], HNS3_OPC_RX_COM_THRD_ALLOC,
3751 req = (struct hns3_rx_com_thrd *)&desc[i].data;
3753 /* The first descriptor set the NEXT bit to 1 */
3755 desc[i].flag |= rte_cpu_to_le_16(HNS3_CMD_FLAG_NEXT);
3757 desc[i].flag &= ~rte_cpu_to_le_16(HNS3_CMD_FLAG_NEXT);
3759 for (j = 0; j < HNS3_TC_NUM_ONE_DESC; j++) {
3760 tc_idx = i * HNS3_TC_NUM_ONE_DESC + j;
3761 tc = &s_buf->tc_thrd[tc_idx];
3763 req->com_thrd[j].high =
3764 rte_cpu_to_le_16(tc->high >> HNS3_BUF_UNIT_S);
3765 req->com_thrd[j].high |=
3766 rte_cpu_to_le_16(BIT(HNS3_RX_PRIV_EN_B));
3767 req->com_thrd[j].low =
3768 rte_cpu_to_le_16(tc->low >> HNS3_BUF_UNIT_S);
3769 req->com_thrd[j].low |=
3770 rte_cpu_to_le_16(BIT(HNS3_RX_PRIV_EN_B));
3774 /* Send 2 descriptors at one time */
3775 ret = hns3_cmd_send(hw, desc, HNS3_RX_COM_THRD_ALLOC_DESC_NUM);
3777 PMD_INIT_LOG(ERR, "common threshold config cmd failed %d", ret);
3783 hns3_common_wl_config(struct hns3_hw *hw, struct hns3_pkt_buf_alloc *buf_alloc)
3785 struct hns3_shared_buf *buf = &buf_alloc->s_buf;
3786 struct hns3_rx_com_wl *req;
3787 struct hns3_cmd_desc desc;
3790 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_RX_COM_WL_ALLOC, false);
3792 req = (struct hns3_rx_com_wl *)desc.data;
3793 req->com_wl.high = rte_cpu_to_le_16(buf->self.high >> HNS3_BUF_UNIT_S);
3794 req->com_wl.high |= rte_cpu_to_le_16(BIT(HNS3_RX_PRIV_EN_B));
3796 req->com_wl.low = rte_cpu_to_le_16(buf->self.low >> HNS3_BUF_UNIT_S);
3797 req->com_wl.low |= rte_cpu_to_le_16(BIT(HNS3_RX_PRIV_EN_B));
3799 ret = hns3_cmd_send(hw, &desc, 1);
3801 PMD_INIT_LOG(ERR, "common waterline config cmd failed %d", ret);
3807 hns3_buffer_alloc(struct hns3_hw *hw)
3809 struct hns3_pkt_buf_alloc pkt_buf;
3812 memset(&pkt_buf, 0, sizeof(pkt_buf));
3813 ret = hns3_tx_buffer_calc(hw, &pkt_buf);
3816 "could not calc tx buffer size for all TCs %d",
3821 ret = hns3_tx_buffer_alloc(hw, &pkt_buf);
3823 PMD_INIT_LOG(ERR, "could not alloc tx buffers %d", ret);
3827 ret = hns3_rx_buffer_calc(hw, &pkt_buf);
3830 "could not calc rx priv buffer size for all TCs %d",
3835 ret = hns3_rx_priv_buf_alloc(hw, &pkt_buf);
3837 PMD_INIT_LOG(ERR, "could not alloc rx priv buffer %d", ret);
3841 if (hns3_dev_get_support(hw, DCB)) {
3842 ret = hns3_rx_priv_wl_config(hw, &pkt_buf);
3845 "could not configure rx private waterline %d",
3850 ret = hns3_common_thrd_config(hw, &pkt_buf);
3853 "could not configure common threshold %d",
3859 ret = hns3_common_wl_config(hw, &pkt_buf);
3861 PMD_INIT_LOG(ERR, "could not configure common waterline %d",
3868 hns3_mac_init(struct hns3_hw *hw)
3870 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
3871 struct hns3_mac *mac = &hw->mac;
3872 struct hns3_pf *pf = &hns->pf;
3875 pf->support_sfp_query = true;
3876 mac->link_duplex = RTE_ETH_LINK_FULL_DUPLEX;
3877 ret = hns3_cfg_mac_speed_dup_hw(hw, mac->link_speed, mac->link_duplex);
3879 PMD_INIT_LOG(ERR, "Config mac speed dup fail ret = %d", ret);
3883 mac->link_status = RTE_ETH_LINK_DOWN;
3885 return hns3_config_mtu(hw, pf->mps);
3889 hns3_get_mac_ethertype_cmd_status(uint16_t cmdq_resp, uint8_t resp_code)
3891 #define HNS3_ETHERTYPE_SUCCESS_ADD 0
3892 #define HNS3_ETHERTYPE_ALREADY_ADD 1
3893 #define HNS3_ETHERTYPE_MGR_TBL_OVERFLOW 2
3894 #define HNS3_ETHERTYPE_KEY_CONFLICT 3
3899 "cmdq execute failed for get_mac_ethertype_cmd_status, status=%u.\n",
3904 switch (resp_code) {
3905 case HNS3_ETHERTYPE_SUCCESS_ADD:
3906 case HNS3_ETHERTYPE_ALREADY_ADD:
3909 case HNS3_ETHERTYPE_MGR_TBL_OVERFLOW:
3911 "add mac ethertype failed for manager table overflow.");
3912 return_status = -EIO;
3914 case HNS3_ETHERTYPE_KEY_CONFLICT:
3915 PMD_INIT_LOG(ERR, "add mac ethertype failed for key conflict.");
3916 return_status = -EIO;
3920 "add mac ethertype failed for undefined, code=%u.",
3922 return_status = -EIO;
3926 return return_status;
3930 hns3_add_mgr_tbl(struct hns3_hw *hw,
3931 const struct hns3_mac_mgr_tbl_entry_cmd *req)
3933 struct hns3_cmd_desc desc;
3938 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_MAC_ETHTYPE_ADD, false);
3939 memcpy(desc.data, req, sizeof(struct hns3_mac_mgr_tbl_entry_cmd));
3941 ret = hns3_cmd_send(hw, &desc, 1);
3944 "add mac ethertype failed for cmd_send, ret =%d.",
3949 resp_code = (rte_le_to_cpu_32(desc.data[0]) >> 8) & 0xff;
3950 retval = rte_le_to_cpu_16(desc.retval);
3952 return hns3_get_mac_ethertype_cmd_status(retval, resp_code);
3956 hns3_prepare_mgr_tbl(struct hns3_mac_mgr_tbl_entry_cmd *mgr_table,
3957 int *table_item_num)
3959 struct hns3_mac_mgr_tbl_entry_cmd *tbl;
3962 * In current version, we add one item in management table as below:
3963 * 0x0180C200000E -- LLDP MC address
3966 tbl->flags = HNS3_MAC_MGR_MASK_VLAN_B;
3967 tbl->ethter_type = rte_cpu_to_le_16(HNS3_MAC_ETHERTYPE_LLDP);
3968 tbl->mac_addr_hi32 = rte_cpu_to_le_32(htonl(0x0180C200));
3969 tbl->mac_addr_lo16 = rte_cpu_to_le_16(htons(0x000E));
3970 tbl->i_port_bitmap = 0x1;
3971 *table_item_num = 1;
3975 hns3_init_mgr_tbl(struct hns3_hw *hw)
3977 #define HNS_MAC_MGR_TBL_MAX_SIZE 16
3978 struct hns3_mac_mgr_tbl_entry_cmd mgr_table[HNS_MAC_MGR_TBL_MAX_SIZE];
3983 memset(mgr_table, 0, sizeof(mgr_table));
3984 hns3_prepare_mgr_tbl(mgr_table, &table_item_num);
3985 for (i = 0; i < table_item_num; i++) {
3986 ret = hns3_add_mgr_tbl(hw, &mgr_table[i]);
3988 PMD_INIT_LOG(ERR, "add mac ethertype failed, ret =%d",
3998 hns3_promisc_param_init(struct hns3_promisc_param *param, bool en_uc,
3999 bool en_mc, bool en_bc, int vport_id)
4004 memset(param, 0, sizeof(struct hns3_promisc_param));
4006 param->enable = HNS3_PROMISC_EN_UC;
4008 param->enable |= HNS3_PROMISC_EN_MC;
4010 param->enable |= HNS3_PROMISC_EN_BC;
4011 param->vf_id = vport_id;
4015 hns3_cmd_set_promisc_mode(struct hns3_hw *hw, struct hns3_promisc_param *param)
4017 struct hns3_promisc_cfg_cmd *req;
4018 struct hns3_cmd_desc desc;
4021 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_CFG_PROMISC_MODE, false);
4023 req = (struct hns3_promisc_cfg_cmd *)desc.data;
4024 req->vf_id = param->vf_id;
4025 req->flag = (param->enable << HNS3_PROMISC_EN_B) |
4026 HNS3_PROMISC_TX_EN_B | HNS3_PROMISC_RX_EN_B;
4028 ret = hns3_cmd_send(hw, &desc, 1);
4030 PMD_INIT_LOG(ERR, "Set promisc mode fail, ret = %d", ret);
4036 hns3_set_promisc_mode(struct hns3_hw *hw, bool en_uc_pmc, bool en_mc_pmc)
4038 struct hns3_promisc_param param;
4039 bool en_bc_pmc = true;
4043 * In current version VF is not supported when PF is driven by DPDK
4044 * driver, just need to configure parameters for PF vport.
4046 vf_id = HNS3_PF_FUNC_ID;
4048 hns3_promisc_param_init(¶m, en_uc_pmc, en_mc_pmc, en_bc_pmc, vf_id);
4049 return hns3_cmd_set_promisc_mode(hw, ¶m);
4053 hns3_promisc_init(struct hns3_hw *hw)
4055 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
4056 struct hns3_pf *pf = &hns->pf;
4057 struct hns3_promisc_param param;
4061 ret = hns3_set_promisc_mode(hw, false, false);
4063 PMD_INIT_LOG(ERR, "failed to set promisc mode, ret = %d", ret);
4068 * In current version VFs are not supported when PF is driven by DPDK
4069 * driver. After PF has been taken over by DPDK, the original VF will
4070 * be invalid. So, there is a possibility of entry residues. It should
4071 * clear VFs's promisc mode to avoid unnecessary bandwidth usage
4074 for (func_id = HNS3_1ST_VF_FUNC_ID; func_id < pf->func_num; func_id++) {
4075 hns3_promisc_param_init(¶m, false, false, false, func_id);
4076 ret = hns3_cmd_set_promisc_mode(hw, ¶m);
4078 PMD_INIT_LOG(ERR, "failed to clear vf:%u promisc mode,"
4079 " ret = %d", func_id, ret);
4088 hns3_promisc_uninit(struct hns3_hw *hw)
4090 struct hns3_promisc_param param;
4094 func_id = HNS3_PF_FUNC_ID;
4097 * In current version VFs are not supported when PF is driven by
4098 * DPDK driver, and VFs' promisc mode status has been cleared during
4099 * init and their status will not change. So just clear PF's promisc
4100 * mode status during uninit.
4102 hns3_promisc_param_init(¶m, false, false, false, func_id);
4103 ret = hns3_cmd_set_promisc_mode(hw, ¶m);
4105 PMD_INIT_LOG(ERR, "failed to clear promisc status during"
4106 " uninit, ret = %d", ret);
4110 hns3_dev_promiscuous_enable(struct rte_eth_dev *dev)
4112 bool allmulti = dev->data->all_multicast ? true : false;
4113 struct hns3_adapter *hns = dev->data->dev_private;
4114 struct hns3_hw *hw = &hns->hw;
4119 rte_spinlock_lock(&hw->lock);
4120 ret = hns3_set_promisc_mode(hw, true, true);
4122 rte_spinlock_unlock(&hw->lock);
4123 hns3_err(hw, "failed to enable promiscuous mode, ret = %d",
4129 * When promiscuous mode was enabled, disable the vlan filter to let
4130 * all packets coming in in the receiving direction.
4132 offloads = dev->data->dev_conf.rxmode.offloads;
4133 if (offloads & RTE_ETH_RX_OFFLOAD_VLAN_FILTER) {
4134 ret = hns3_enable_vlan_filter(hns, false);
4136 hns3_err(hw, "failed to enable promiscuous mode due to "
4137 "failure to disable vlan filter, ret = %d",
4139 err = hns3_set_promisc_mode(hw, false, allmulti);
4141 hns3_err(hw, "failed to restore promiscuous "
4142 "status after disable vlan filter "
4143 "failed during enabling promiscuous "
4144 "mode, ret = %d", ret);
4148 rte_spinlock_unlock(&hw->lock);
4154 hns3_dev_promiscuous_disable(struct rte_eth_dev *dev)
4156 bool allmulti = dev->data->all_multicast ? true : false;
4157 struct hns3_adapter *hns = dev->data->dev_private;
4158 struct hns3_hw *hw = &hns->hw;
4163 /* If now in all_multicast mode, must remain in all_multicast mode. */
4164 rte_spinlock_lock(&hw->lock);
4165 ret = hns3_set_promisc_mode(hw, false, allmulti);
4167 rte_spinlock_unlock(&hw->lock);
4168 hns3_err(hw, "failed to disable promiscuous mode, ret = %d",
4172 /* when promiscuous mode was disabled, restore the vlan filter status */
4173 offloads = dev->data->dev_conf.rxmode.offloads;
4174 if (offloads & RTE_ETH_RX_OFFLOAD_VLAN_FILTER) {
4175 ret = hns3_enable_vlan_filter(hns, true);
4177 hns3_err(hw, "failed to disable promiscuous mode due to"
4178 " failure to restore vlan filter, ret = %d",
4180 err = hns3_set_promisc_mode(hw, true, true);
4182 hns3_err(hw, "failed to restore promiscuous "
4183 "status after enabling vlan filter "
4184 "failed during disabling promiscuous "
4185 "mode, ret = %d", ret);
4188 rte_spinlock_unlock(&hw->lock);
4194 hns3_dev_allmulticast_enable(struct rte_eth_dev *dev)
4196 struct hns3_adapter *hns = dev->data->dev_private;
4197 struct hns3_hw *hw = &hns->hw;
4200 if (dev->data->promiscuous)
4203 rte_spinlock_lock(&hw->lock);
4204 ret = hns3_set_promisc_mode(hw, false, true);
4205 rte_spinlock_unlock(&hw->lock);
4207 hns3_err(hw, "failed to enable allmulticast mode, ret = %d",
4214 hns3_dev_allmulticast_disable(struct rte_eth_dev *dev)
4216 struct hns3_adapter *hns = dev->data->dev_private;
4217 struct hns3_hw *hw = &hns->hw;
4220 /* If now in promiscuous mode, must remain in all_multicast mode. */
4221 if (dev->data->promiscuous)
4224 rte_spinlock_lock(&hw->lock);
4225 ret = hns3_set_promisc_mode(hw, false, false);
4226 rte_spinlock_unlock(&hw->lock);
4228 hns3_err(hw, "failed to disable allmulticast mode, ret = %d",
4235 hns3_dev_promisc_restore(struct hns3_adapter *hns)
4237 struct hns3_hw *hw = &hns->hw;
4238 bool allmulti = hw->data->all_multicast ? true : false;
4241 if (hw->data->promiscuous) {
4242 ret = hns3_set_promisc_mode(hw, true, true);
4244 hns3_err(hw, "failed to restore promiscuous mode, "
4249 ret = hns3_set_promisc_mode(hw, false, allmulti);
4251 hns3_err(hw, "failed to restore allmulticast mode, ret = %d",
4257 hns3_get_sfp_info(struct hns3_hw *hw, struct hns3_mac *mac_info)
4259 struct hns3_sfp_info_cmd *resp;
4260 struct hns3_cmd_desc desc;
4263 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_GET_SFP_INFO, true);
4264 resp = (struct hns3_sfp_info_cmd *)desc.data;
4265 resp->query_type = HNS3_ACTIVE_QUERY;
4267 ret = hns3_cmd_send(hw, &desc, 1);
4268 if (ret == -EOPNOTSUPP) {
4269 hns3_warn(hw, "firmware does not support get SFP info,"
4273 hns3_err(hw, "get sfp info failed, ret = %d.", ret);
4278 * In some case, the speed of MAC obtained from firmware may be 0, it
4279 * shouldn't be set to mac->speed.
4281 if (!rte_le_to_cpu_32(resp->sfp_speed))
4284 mac_info->link_speed = rte_le_to_cpu_32(resp->sfp_speed);
4286 * if resp->supported_speed is 0, it means it's an old version
4287 * firmware, do not update these params.
4289 if (resp->supported_speed) {
4290 mac_info->query_type = HNS3_ACTIVE_QUERY;
4291 mac_info->supported_speed =
4292 rte_le_to_cpu_32(resp->supported_speed);
4293 mac_info->support_autoneg = resp->autoneg_ability;
4294 mac_info->link_autoneg = (resp->autoneg == 0) ? RTE_ETH_LINK_FIXED
4295 : RTE_ETH_LINK_AUTONEG;
4297 mac_info->query_type = HNS3_DEFAULT_QUERY;
4304 hns3_check_speed_dup(uint8_t duplex, uint32_t speed)
4306 if (!(speed == RTE_ETH_SPEED_NUM_10M || speed == RTE_ETH_SPEED_NUM_100M))
4307 duplex = RTE_ETH_LINK_FULL_DUPLEX;
4313 hns3_cfg_mac_speed_dup(struct hns3_hw *hw, uint32_t speed, uint8_t duplex)
4315 struct hns3_mac *mac = &hw->mac;
4318 duplex = hns3_check_speed_dup(duplex, speed);
4319 if (mac->link_speed == speed && mac->link_duplex == duplex)
4322 ret = hns3_cfg_mac_speed_dup_hw(hw, speed, duplex);
4326 ret = hns3_port_shaper_update(hw, speed);
4330 mac->link_speed = speed;
4331 mac->link_duplex = duplex;
4337 hns3_update_fiber_link_info(struct hns3_hw *hw)
4339 struct hns3_pf *pf = HNS3_DEV_HW_TO_PF(hw);
4340 struct hns3_mac *mac = &hw->mac;
4341 struct hns3_mac mac_info;
4344 /* If firmware do not support get SFP/qSFP speed, return directly */
4345 if (!pf->support_sfp_query)
4348 memset(&mac_info, 0, sizeof(struct hns3_mac));
4349 ret = hns3_get_sfp_info(hw, &mac_info);
4350 if (ret == -EOPNOTSUPP) {
4351 pf->support_sfp_query = false;
4356 /* Do nothing if no SFP */
4357 if (mac_info.link_speed == RTE_ETH_SPEED_NUM_NONE)
4361 * If query_type is HNS3_ACTIVE_QUERY, it is no need
4362 * to reconfigure the speed of MAC. Otherwise, it indicates
4363 * that the current firmware only supports to obtain the
4364 * speed of the SFP, and the speed of MAC needs to reconfigure.
4366 mac->query_type = mac_info.query_type;
4367 if (mac->query_type == HNS3_ACTIVE_QUERY) {
4368 if (mac_info.link_speed != mac->link_speed) {
4369 ret = hns3_port_shaper_update(hw, mac_info.link_speed);
4374 mac->link_speed = mac_info.link_speed;
4375 mac->supported_speed = mac_info.supported_speed;
4376 mac->support_autoneg = mac_info.support_autoneg;
4377 mac->link_autoneg = mac_info.link_autoneg;
4382 /* Config full duplex for SFP */
4383 return hns3_cfg_mac_speed_dup(hw, mac_info.link_speed,
4384 RTE_ETH_LINK_FULL_DUPLEX);
4388 hns3_parse_copper_phy_params(struct hns3_cmd_desc *desc, struct hns3_mac *mac)
4390 #define HNS3_PHY_SUPPORTED_SPEED_MASK 0x2f
4392 struct hns3_phy_params_bd0_cmd *req;
4395 req = (struct hns3_phy_params_bd0_cmd *)desc[0].data;
4396 mac->link_speed = rte_le_to_cpu_32(req->speed);
4397 mac->link_duplex = hns3_get_bit(req->duplex,
4398 HNS3_PHY_DUPLEX_CFG_B);
4399 mac->link_autoneg = hns3_get_bit(req->autoneg,
4400 HNS3_PHY_AUTONEG_CFG_B);
4401 mac->advertising = rte_le_to_cpu_32(req->advertising);
4402 mac->lp_advertising = rte_le_to_cpu_32(req->lp_advertising);
4403 supported = rte_le_to_cpu_32(req->supported);
4404 mac->supported_speed = supported & HNS3_PHY_SUPPORTED_SPEED_MASK;
4405 mac->support_autoneg = !!(supported & HNS3_PHY_LINK_MODE_AUTONEG_BIT);
4409 hns3_get_copper_phy_params(struct hns3_hw *hw, struct hns3_mac *mac)
4411 struct hns3_cmd_desc desc[HNS3_PHY_PARAM_CFG_BD_NUM];
4415 for (i = 0; i < HNS3_PHY_PARAM_CFG_BD_NUM - 1; i++) {
4416 hns3_cmd_setup_basic_desc(&desc[i], HNS3_OPC_PHY_PARAM_CFG,
4418 desc[i].flag |= rte_cpu_to_le_16(HNS3_CMD_FLAG_NEXT);
4420 hns3_cmd_setup_basic_desc(&desc[i], HNS3_OPC_PHY_PARAM_CFG, true);
4422 ret = hns3_cmd_send(hw, desc, HNS3_PHY_PARAM_CFG_BD_NUM);
4424 hns3_err(hw, "get phy parameters failed, ret = %d.", ret);
4428 hns3_parse_copper_phy_params(desc, mac);
4434 hns3_update_copper_link_info(struct hns3_hw *hw)
4436 struct hns3_mac *mac = &hw->mac;
4437 struct hns3_mac mac_info;
4440 memset(&mac_info, 0, sizeof(struct hns3_mac));
4441 ret = hns3_get_copper_phy_params(hw, &mac_info);
4445 if (mac_info.link_speed != mac->link_speed) {
4446 ret = hns3_port_shaper_update(hw, mac_info.link_speed);
4451 mac->link_speed = mac_info.link_speed;
4452 mac->link_duplex = mac_info.link_duplex;
4453 mac->link_autoneg = mac_info.link_autoneg;
4454 mac->supported_speed = mac_info.supported_speed;
4455 mac->advertising = mac_info.advertising;
4456 mac->lp_advertising = mac_info.lp_advertising;
4457 mac->support_autoneg = mac_info.support_autoneg;
4463 hns3_update_link_info(struct rte_eth_dev *eth_dev)
4465 struct hns3_adapter *hns = eth_dev->data->dev_private;
4466 struct hns3_hw *hw = &hns->hw;
4469 if (hw->mac.media_type == HNS3_MEDIA_TYPE_COPPER)
4470 ret = hns3_update_copper_link_info(hw);
4471 else if (hw->mac.media_type == HNS3_MEDIA_TYPE_FIBER)
4472 ret = hns3_update_fiber_link_info(hw);
4478 hns3_cfg_mac_mode(struct hns3_hw *hw, bool enable)
4480 struct hns3_config_mac_mode_cmd *req;
4481 struct hns3_cmd_desc desc;
4482 uint32_t loop_en = 0;
4486 req = (struct hns3_config_mac_mode_cmd *)desc.data;
4488 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_CONFIG_MAC_MODE, false);
4491 hns3_set_bit(loop_en, HNS3_MAC_TX_EN_B, val);
4492 hns3_set_bit(loop_en, HNS3_MAC_RX_EN_B, val);
4493 hns3_set_bit(loop_en, HNS3_MAC_PAD_TX_B, val);
4494 hns3_set_bit(loop_en, HNS3_MAC_PAD_RX_B, val);
4495 hns3_set_bit(loop_en, HNS3_MAC_1588_TX_B, 0);
4496 hns3_set_bit(loop_en, HNS3_MAC_1588_RX_B, 0);
4497 hns3_set_bit(loop_en, HNS3_MAC_APP_LP_B, 0);
4498 hns3_set_bit(loop_en, HNS3_MAC_LINE_LP_B, 0);
4499 hns3_set_bit(loop_en, HNS3_MAC_FCS_TX_B, val);
4500 hns3_set_bit(loop_en, HNS3_MAC_RX_FCS_B, val);
4503 * If RTE_ETH_RX_OFFLOAD_KEEP_CRC offload is set, MAC will not strip CRC
4504 * when receiving frames. Otherwise, CRC will be stripped.
4506 if (hw->data->dev_conf.rxmode.offloads & RTE_ETH_RX_OFFLOAD_KEEP_CRC)
4507 hns3_set_bit(loop_en, HNS3_MAC_RX_FCS_STRIP_B, 0);
4509 hns3_set_bit(loop_en, HNS3_MAC_RX_FCS_STRIP_B, val);
4510 hns3_set_bit(loop_en, HNS3_MAC_TX_OVERSIZE_TRUNCATE_B, val);
4511 hns3_set_bit(loop_en, HNS3_MAC_RX_OVERSIZE_TRUNCATE_B, val);
4512 hns3_set_bit(loop_en, HNS3_MAC_TX_UNDER_MIN_ERR_B, val);
4513 req->txrx_pad_fcs_loop_en = rte_cpu_to_le_32(loop_en);
4515 ret = hns3_cmd_send(hw, &desc, 1);
4517 PMD_INIT_LOG(ERR, "mac enable fail, ret =%d.", ret);
4523 hns3_get_mac_link_status(struct hns3_hw *hw)
4525 struct hns3_link_status_cmd *req;
4526 struct hns3_cmd_desc desc;
4530 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_QUERY_LINK_STATUS, true);
4531 ret = hns3_cmd_send(hw, &desc, 1);
4533 hns3_err(hw, "get link status cmd failed %d", ret);
4534 return RTE_ETH_LINK_DOWN;
4537 req = (struct hns3_link_status_cmd *)desc.data;
4538 link_status = req->status & HNS3_LINK_STATUS_UP_M;
4540 return !!link_status;
4544 hns3_update_link_status(struct hns3_hw *hw)
4548 state = hns3_get_mac_link_status(hw);
4549 if (state != hw->mac.link_status) {
4550 hw->mac.link_status = state;
4551 hns3_warn(hw, "Link status change to %s!", state ? "up" : "down");
4559 hns3_update_linkstatus_and_event(struct hns3_hw *hw, bool query)
4561 struct rte_eth_dev *dev = &rte_eth_devices[hw->data->port_id];
4562 struct rte_eth_link new_link;
4566 hns3_update_port_link_info(dev);
4568 memset(&new_link, 0, sizeof(new_link));
4569 hns3_setup_linkstatus(dev, &new_link);
4571 ret = rte_eth_linkstatus_set(dev, &new_link);
4572 if (ret == 0 && dev->data->dev_conf.intr_conf.lsc != 0)
4573 hns3_start_report_lse(dev);
4577 hns3_service_handler(void *param)
4579 struct rte_eth_dev *eth_dev = (struct rte_eth_dev *)param;
4580 struct hns3_adapter *hns = eth_dev->data->dev_private;
4581 struct hns3_hw *hw = &hns->hw;
4583 if (!hns3_is_reset_pending(hns))
4584 hns3_update_linkstatus_and_event(hw, true);
4586 hns3_warn(hw, "Cancel the query when reset is pending");
4588 rte_eal_alarm_set(HNS3_SERVICE_INTERVAL, hns3_service_handler, eth_dev);
4592 hns3_init_hardware(struct hns3_adapter *hns)
4594 struct hns3_hw *hw = &hns->hw;
4597 ret = hns3_map_tqp(hw);
4599 PMD_INIT_LOG(ERR, "Failed to map tqp: %d", ret);
4603 ret = hns3_init_umv_space(hw);
4605 PMD_INIT_LOG(ERR, "Failed to init umv space: %d", ret);
4609 ret = hns3_mac_init(hw);
4611 PMD_INIT_LOG(ERR, "Failed to init MAC: %d", ret);
4615 ret = hns3_init_mgr_tbl(hw);
4617 PMD_INIT_LOG(ERR, "Failed to init manager table: %d", ret);
4621 ret = hns3_promisc_init(hw);
4623 PMD_INIT_LOG(ERR, "Failed to init promisc: %d",
4628 ret = hns3_init_vlan_config(hns);
4630 PMD_INIT_LOG(ERR, "Failed to init vlan: %d", ret);
4634 ret = hns3_dcb_init(hw);
4636 PMD_INIT_LOG(ERR, "Failed to init dcb: %d", ret);
4640 ret = hns3_init_fd_config(hns);
4642 PMD_INIT_LOG(ERR, "Failed to init flow director: %d", ret);
4646 ret = hns3_config_tso(hw, HNS3_TSO_MSS_MIN, HNS3_TSO_MSS_MAX);
4648 PMD_INIT_LOG(ERR, "Failed to config tso: %d", ret);
4652 ret = hns3_config_gro(hw, false);
4654 PMD_INIT_LOG(ERR, "Failed to config gro: %d", ret);
4659 * In the initialization clearing the all hardware mapping relationship
4660 * configurations between queues and interrupt vectors is needed, so
4661 * some error caused by the residual configurations, such as the
4662 * unexpected interrupt, can be avoid.
4664 ret = hns3_init_ring_with_vector(hw);
4666 PMD_INIT_LOG(ERR, "Failed to init ring intr vector: %d", ret);
4673 hns3_uninit_umv_space(hw);
4678 hns3_clear_hw(struct hns3_hw *hw)
4680 struct hns3_cmd_desc desc;
4683 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_CLEAR_HW_STATE, false);
4685 ret = hns3_cmd_send(hw, &desc, 1);
4686 if (ret && ret != -EOPNOTSUPP)
4693 hns3_config_all_msix_error(struct hns3_hw *hw, bool enable)
4698 * The new firmware support report more hardware error types by
4699 * msix mode. These errors are defined as RAS errors in hardware
4700 * and belong to a different type from the MSI-x errors processed
4701 * by the network driver.
4703 * Network driver should open the new error report on initialization.
4705 val = hns3_read_dev(hw, HNS3_VECTOR0_OTER_EN_REG);
4706 hns3_set_bit(val, HNS3_VECTOR0_ALL_MSIX_ERR_B, enable ? 1 : 0);
4707 hns3_write_dev(hw, HNS3_VECTOR0_OTER_EN_REG, val);
4711 hns3_set_firber_default_support_speed(struct hns3_hw *hw)
4713 struct hns3_mac *mac = &hw->mac;
4715 switch (mac->link_speed) {
4716 case RTE_ETH_SPEED_NUM_1G:
4717 return HNS3_FIBER_LINK_SPEED_1G_BIT;
4718 case RTE_ETH_SPEED_NUM_10G:
4719 return HNS3_FIBER_LINK_SPEED_10G_BIT;
4720 case RTE_ETH_SPEED_NUM_25G:
4721 return HNS3_FIBER_LINK_SPEED_25G_BIT;
4722 case RTE_ETH_SPEED_NUM_40G:
4723 return HNS3_FIBER_LINK_SPEED_40G_BIT;
4724 case RTE_ETH_SPEED_NUM_50G:
4725 return HNS3_FIBER_LINK_SPEED_50G_BIT;
4726 case RTE_ETH_SPEED_NUM_100G:
4727 return HNS3_FIBER_LINK_SPEED_100G_BIT;
4728 case RTE_ETH_SPEED_NUM_200G:
4729 return HNS3_FIBER_LINK_SPEED_200G_BIT;
4731 hns3_warn(hw, "invalid speed %u Mbps.", mac->link_speed);
4737 * Validity of supported_speed for firber and copper media type can be
4738 * guaranteed by the following policy:
4740 * Although the initialization of the phy in the firmware may not be
4741 * completed, the firmware can guarantees that the supported_speed is
4744 * If the version of firmware supports the acitive query way of the
4745 * HNS3_OPC_GET_SFP_INFO opcode, the supported_speed can be obtained
4746 * through it. If unsupported, use the SFP's speed as the value of the
4750 hns3_get_port_supported_speed(struct rte_eth_dev *eth_dev)
4752 struct hns3_adapter *hns = eth_dev->data->dev_private;
4753 struct hns3_hw *hw = &hns->hw;
4754 struct hns3_mac *mac = &hw->mac;
4757 ret = hns3_update_link_info(eth_dev);
4761 if (mac->media_type == HNS3_MEDIA_TYPE_FIBER) {
4763 * Some firmware does not support the report of supported_speed,
4764 * and only report the effective speed of SFP. In this case, it
4765 * is necessary to use the SFP's speed as the supported_speed.
4767 if (mac->supported_speed == 0)
4768 mac->supported_speed =
4769 hns3_set_firber_default_support_speed(hw);
4776 hns3_get_fc_autoneg_capability(struct hns3_adapter *hns)
4778 struct hns3_mac *mac = &hns->hw.mac;
4780 if (mac->media_type == HNS3_MEDIA_TYPE_COPPER) {
4781 hns->pf.support_fc_autoneg = true;
4786 * Flow control auto-negotiation requires the cooperation of the driver
4787 * and firmware. Currently, the optical port does not support flow
4788 * control auto-negotiation.
4790 hns->pf.support_fc_autoneg = false;
4794 hns3_init_pf(struct rte_eth_dev *eth_dev)
4796 struct rte_device *dev = eth_dev->device;
4797 struct rte_pci_device *pci_dev = RTE_DEV_TO_PCI(dev);
4798 struct hns3_adapter *hns = eth_dev->data->dev_private;
4799 struct hns3_hw *hw = &hns->hw;
4802 PMD_INIT_FUNC_TRACE();
4804 /* Get hardware io base address from pcie BAR2 IO space */
4805 hw->io_base = pci_dev->mem_resource[2].addr;
4807 /* Firmware command queue initialize */
4808 ret = hns3_cmd_init_queue(hw);
4810 PMD_INIT_LOG(ERR, "Failed to init cmd queue: %d", ret);
4811 goto err_cmd_init_queue;
4814 hns3_clear_all_event_cause(hw);
4816 /* Firmware command initialize */
4817 ret = hns3_cmd_init(hw);
4819 PMD_INIT_LOG(ERR, "Failed to init cmd: %d", ret);
4823 hns3_tx_push_init(eth_dev);
4826 * To ensure that the hardware environment is clean during
4827 * initialization, the driver actively clear the hardware environment
4828 * during initialization, including PF and corresponding VFs' vlan, mac,
4829 * flow table configurations, etc.
4831 ret = hns3_clear_hw(hw);
4833 PMD_INIT_LOG(ERR, "failed to clear hardware: %d", ret);
4837 /* Hardware statistics of imissed registers cleared. */
4838 ret = hns3_update_imissed_stats(hw, true);
4840 hns3_err(hw, "clear imissed stats failed, ret = %d", ret);
4844 hns3_config_all_msix_error(hw, true);
4846 ret = rte_intr_callback_register(pci_dev->intr_handle,
4847 hns3_interrupt_handler,
4850 PMD_INIT_LOG(ERR, "Failed to register intr: %d", ret);
4851 goto err_intr_callback_register;
4854 ret = hns3_ptp_init(hw);
4856 goto err_get_config;
4858 /* Enable interrupt */
4859 rte_intr_enable(pci_dev->intr_handle);
4860 hns3_pf_enable_irq0(hw);
4862 /* Get configuration */
4863 ret = hns3_get_configuration(hw);
4865 PMD_INIT_LOG(ERR, "Failed to fetch configuration: %d", ret);
4866 goto err_get_config;
4869 ret = hns3_tqp_stats_init(hw);
4871 goto err_get_config;
4873 ret = hns3_init_hardware(hns);
4875 PMD_INIT_LOG(ERR, "Failed to init hardware: %d", ret);
4879 /* Initialize flow director filter list & hash */
4880 ret = hns3_fdir_filter_init(hns);
4882 PMD_INIT_LOG(ERR, "Failed to alloc hashmap for fdir: %d", ret);
4886 hns3_rss_set_default_args(hw);
4888 ret = hns3_enable_hw_error_intr(hns, true);
4890 PMD_INIT_LOG(ERR, "fail to enable hw error interrupts: %d",
4892 goto err_enable_intr;
4895 ret = hns3_get_port_supported_speed(eth_dev);
4897 PMD_INIT_LOG(ERR, "failed to get speed capabilities supported "
4898 "by device, ret = %d.", ret);
4899 goto err_supported_speed;
4902 hns3_get_fc_autoneg_capability(hns);
4904 hns3_tm_conf_init(eth_dev);
4908 err_supported_speed:
4909 (void)hns3_enable_hw_error_intr(hns, false);
4911 hns3_fdir_filter_uninit(hns);
4913 hns3_uninit_umv_space(hw);
4915 hns3_tqp_stats_uninit(hw);
4917 hns3_pf_disable_irq0(hw);
4918 rte_intr_disable(pci_dev->intr_handle);
4919 hns3_intr_unregister(pci_dev->intr_handle, hns3_interrupt_handler,
4921 err_intr_callback_register:
4923 hns3_cmd_uninit(hw);
4924 hns3_cmd_destroy_queue(hw);
4932 hns3_uninit_pf(struct rte_eth_dev *eth_dev)
4934 struct hns3_adapter *hns = eth_dev->data->dev_private;
4935 struct rte_device *dev = eth_dev->device;
4936 struct rte_pci_device *pci_dev = RTE_DEV_TO_PCI(dev);
4937 struct hns3_hw *hw = &hns->hw;
4939 PMD_INIT_FUNC_TRACE();
4941 hns3_tm_conf_uninit(eth_dev);
4942 hns3_enable_hw_error_intr(hns, false);
4943 hns3_rss_uninit(hns);
4944 (void)hns3_config_gro(hw, false);
4945 hns3_promisc_uninit(hw);
4946 hns3_flow_uninit(eth_dev);
4947 hns3_fdir_filter_uninit(hns);
4948 hns3_uninit_umv_space(hw);
4949 hns3_tqp_stats_uninit(hw);
4950 hns3_config_mac_tnl_int(hw, false);
4951 hns3_pf_disable_irq0(hw);
4952 rte_intr_disable(pci_dev->intr_handle);
4953 hns3_intr_unregister(pci_dev->intr_handle, hns3_interrupt_handler,
4955 hns3_config_all_msix_error(hw, false);
4956 hns3_cmd_uninit(hw);
4957 hns3_cmd_destroy_queue(hw);
4962 hns3_convert_link_speeds2bitmap_copper(uint32_t link_speeds)
4966 switch (link_speeds & ~RTE_ETH_LINK_SPEED_FIXED) {
4967 case RTE_ETH_LINK_SPEED_10M:
4968 speed_bit = HNS3_PHY_LINK_SPEED_10M_BIT;
4970 case RTE_ETH_LINK_SPEED_10M_HD:
4971 speed_bit = HNS3_PHY_LINK_SPEED_10M_HD_BIT;
4973 case RTE_ETH_LINK_SPEED_100M:
4974 speed_bit = HNS3_PHY_LINK_SPEED_100M_BIT;
4976 case RTE_ETH_LINK_SPEED_100M_HD:
4977 speed_bit = HNS3_PHY_LINK_SPEED_100M_HD_BIT;
4979 case RTE_ETH_LINK_SPEED_1G:
4980 speed_bit = HNS3_PHY_LINK_SPEED_1000M_BIT;
4991 hns3_convert_link_speeds2bitmap_fiber(uint32_t link_speeds)
4995 switch (link_speeds & ~RTE_ETH_LINK_SPEED_FIXED) {
4996 case RTE_ETH_LINK_SPEED_1G:
4997 speed_bit = HNS3_FIBER_LINK_SPEED_1G_BIT;
4999 case RTE_ETH_LINK_SPEED_10G:
5000 speed_bit = HNS3_FIBER_LINK_SPEED_10G_BIT;
5002 case RTE_ETH_LINK_SPEED_25G:
5003 speed_bit = HNS3_FIBER_LINK_SPEED_25G_BIT;
5005 case RTE_ETH_LINK_SPEED_40G:
5006 speed_bit = HNS3_FIBER_LINK_SPEED_40G_BIT;
5008 case RTE_ETH_LINK_SPEED_50G:
5009 speed_bit = HNS3_FIBER_LINK_SPEED_50G_BIT;
5011 case RTE_ETH_LINK_SPEED_100G:
5012 speed_bit = HNS3_FIBER_LINK_SPEED_100G_BIT;
5014 case RTE_ETH_LINK_SPEED_200G:
5015 speed_bit = HNS3_FIBER_LINK_SPEED_200G_BIT;
5026 hns3_check_port_speed(struct hns3_hw *hw, uint32_t link_speeds)
5028 struct hns3_mac *mac = &hw->mac;
5029 uint32_t supported_speed = mac->supported_speed;
5030 uint32_t speed_bit = 0;
5032 if (mac->media_type == HNS3_MEDIA_TYPE_COPPER)
5033 speed_bit = hns3_convert_link_speeds2bitmap_copper(link_speeds);
5034 else if (mac->media_type == HNS3_MEDIA_TYPE_FIBER)
5035 speed_bit = hns3_convert_link_speeds2bitmap_fiber(link_speeds);
5037 if (!(speed_bit & supported_speed)) {
5038 hns3_err(hw, "link_speeds(0x%x) exceeds the supported speed capability or is incorrect.",
5046 static inline uint32_t
5047 hns3_get_link_speed(uint32_t link_speeds)
5049 uint32_t speed = RTE_ETH_SPEED_NUM_NONE;
5051 if (link_speeds & RTE_ETH_LINK_SPEED_10M ||
5052 link_speeds & RTE_ETH_LINK_SPEED_10M_HD)
5053 speed = RTE_ETH_SPEED_NUM_10M;
5054 if (link_speeds & RTE_ETH_LINK_SPEED_100M ||
5055 link_speeds & RTE_ETH_LINK_SPEED_100M_HD)
5056 speed = RTE_ETH_SPEED_NUM_100M;
5057 if (link_speeds & RTE_ETH_LINK_SPEED_1G)
5058 speed = RTE_ETH_SPEED_NUM_1G;
5059 if (link_speeds & RTE_ETH_LINK_SPEED_10G)
5060 speed = RTE_ETH_SPEED_NUM_10G;
5061 if (link_speeds & RTE_ETH_LINK_SPEED_25G)
5062 speed = RTE_ETH_SPEED_NUM_25G;
5063 if (link_speeds & RTE_ETH_LINK_SPEED_40G)
5064 speed = RTE_ETH_SPEED_NUM_40G;
5065 if (link_speeds & RTE_ETH_LINK_SPEED_50G)
5066 speed = RTE_ETH_SPEED_NUM_50G;
5067 if (link_speeds & RTE_ETH_LINK_SPEED_100G)
5068 speed = RTE_ETH_SPEED_NUM_100G;
5069 if (link_speeds & RTE_ETH_LINK_SPEED_200G)
5070 speed = RTE_ETH_SPEED_NUM_200G;
5076 hns3_get_link_duplex(uint32_t link_speeds)
5078 if ((link_speeds & RTE_ETH_LINK_SPEED_10M_HD) ||
5079 (link_speeds & RTE_ETH_LINK_SPEED_100M_HD))
5080 return RTE_ETH_LINK_HALF_DUPLEX;
5082 return RTE_ETH_LINK_FULL_DUPLEX;
5086 hns3_set_copper_port_link_speed(struct hns3_hw *hw,
5087 struct hns3_set_link_speed_cfg *cfg)
5089 struct hns3_cmd_desc desc[HNS3_PHY_PARAM_CFG_BD_NUM];
5090 struct hns3_phy_params_bd0_cmd *req;
5093 for (i = 0; i < HNS3_PHY_PARAM_CFG_BD_NUM - 1; i++) {
5094 hns3_cmd_setup_basic_desc(&desc[i], HNS3_OPC_PHY_PARAM_CFG,
5096 desc[i].flag |= rte_cpu_to_le_16(HNS3_CMD_FLAG_NEXT);
5098 hns3_cmd_setup_basic_desc(&desc[i], HNS3_OPC_PHY_PARAM_CFG, false);
5099 req = (struct hns3_phy_params_bd0_cmd *)desc[0].data;
5100 req->autoneg = cfg->autoneg;
5103 * The full speed capability is used to negotiate when
5104 * auto-negotiation is enabled.
5107 req->advertising = HNS3_PHY_LINK_SPEED_10M_BIT |
5108 HNS3_PHY_LINK_SPEED_10M_HD_BIT |
5109 HNS3_PHY_LINK_SPEED_100M_BIT |
5110 HNS3_PHY_LINK_SPEED_100M_HD_BIT |
5111 HNS3_PHY_LINK_SPEED_1000M_BIT;
5113 req->speed = cfg->speed;
5114 req->duplex = cfg->duplex;
5117 return hns3_cmd_send(hw, desc, HNS3_PHY_PARAM_CFG_BD_NUM);
5121 hns3_set_autoneg(struct hns3_hw *hw, bool enable)
5123 struct hns3_config_auto_neg_cmd *req;
5124 struct hns3_cmd_desc desc;
5128 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_CONFIG_AN_MODE, false);
5130 req = (struct hns3_config_auto_neg_cmd *)desc.data;
5132 hns3_set_bit(flag, HNS3_MAC_CFG_AN_EN_B, 1);
5133 req->cfg_an_cmd_flag = rte_cpu_to_le_32(flag);
5135 ret = hns3_cmd_send(hw, &desc, 1);
5137 hns3_err(hw, "autoneg set cmd failed, ret = %d.", ret);
5143 hns3_set_fiber_port_link_speed(struct hns3_hw *hw,
5144 struct hns3_set_link_speed_cfg *cfg)
5148 if (hw->mac.support_autoneg) {
5149 ret = hns3_set_autoneg(hw, cfg->autoneg);
5151 hns3_err(hw, "failed to configure auto-negotiation.");
5156 * To enable auto-negotiation, we only need to open the switch
5157 * of auto-negotiation, then firmware sets all speed
5165 * Some hardware doesn't support auto-negotiation, but users may not
5166 * configure link_speeds (default 0), which means auto-negotiation.
5167 * In this case, a warning message need to be printed, instead of
5171 hns3_warn(hw, "auto-negotiation is not supported, use default fixed speed!");
5175 return hns3_cfg_mac_speed_dup(hw, cfg->speed, cfg->duplex);
5179 hns3_set_port_link_speed(struct hns3_hw *hw,
5180 struct hns3_set_link_speed_cfg *cfg)
5184 if (hw->mac.media_type == HNS3_MEDIA_TYPE_COPPER) {
5185 #if defined(RTE_HNS3_ONLY_1630_FPGA)
5186 struct hns3_pf *pf = HNS3_DEV_HW_TO_PF(hw);
5191 ret = hns3_set_copper_port_link_speed(hw, cfg);
5193 hns3_err(hw, "failed to set copper port link speed,"
5197 } else if (hw->mac.media_type == HNS3_MEDIA_TYPE_FIBER) {
5198 ret = hns3_set_fiber_port_link_speed(hw, cfg);
5200 hns3_err(hw, "failed to set fiber port link speed,"
5210 hns3_apply_link_speed(struct hns3_hw *hw)
5212 struct rte_eth_conf *conf = &hw->data->dev_conf;
5213 struct hns3_set_link_speed_cfg cfg;
5215 memset(&cfg, 0, sizeof(struct hns3_set_link_speed_cfg));
5216 cfg.autoneg = (conf->link_speeds == RTE_ETH_LINK_SPEED_AUTONEG) ?
5217 RTE_ETH_LINK_AUTONEG : RTE_ETH_LINK_FIXED;
5218 if (cfg.autoneg != RTE_ETH_LINK_AUTONEG) {
5219 cfg.speed = hns3_get_link_speed(conf->link_speeds);
5220 cfg.duplex = hns3_get_link_duplex(conf->link_speeds);
5223 return hns3_set_port_link_speed(hw, &cfg);
5227 hns3_do_start(struct hns3_adapter *hns, bool reset_queue)
5229 struct hns3_hw *hw = &hns->hw;
5233 ret = hns3_update_queue_map_configure(hns);
5235 hns3_err(hw, "failed to update queue mapping configuration, ret = %d",
5240 /* Note: hns3_tm_conf_update must be called after configuring DCB. */
5241 ret = hns3_tm_conf_update(hw);
5243 PMD_INIT_LOG(ERR, "failed to update tm conf, ret = %d.", ret);
5247 hns3_enable_rxd_adv_layout(hw);
5249 ret = hns3_init_queues(hns, reset_queue);
5251 PMD_INIT_LOG(ERR, "failed to init queues, ret = %d.", ret);
5255 link_en = hw->set_link_down ? false : true;
5256 ret = hns3_cfg_mac_mode(hw, link_en);
5258 PMD_INIT_LOG(ERR, "failed to enable MAC, ret = %d", ret);
5259 goto err_config_mac_mode;
5262 ret = hns3_apply_link_speed(hw);
5264 goto err_set_link_speed;
5269 (void)hns3_cfg_mac_mode(hw, false);
5271 err_config_mac_mode:
5272 hns3_dev_release_mbufs(hns);
5274 * Here is exception handling, hns3_reset_all_tqps will have the
5275 * corresponding error message if it is handled incorrectly, so it is
5276 * not necessary to check hns3_reset_all_tqps return value, here keep
5277 * ret as the error code causing the exception.
5279 (void)hns3_reset_all_tqps(hns);
5284 hns3_map_rx_interrupt(struct rte_eth_dev *dev)
5286 struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
5287 struct rte_intr_handle *intr_handle = pci_dev->intr_handle;
5288 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
5289 uint16_t base = RTE_INTR_VEC_ZERO_OFFSET;
5290 uint16_t vec = RTE_INTR_VEC_ZERO_OFFSET;
5291 uint32_t intr_vector;
5296 * hns3 needs a separate interrupt to be used as event interrupt which
5297 * could not be shared with task queue pair, so KERNEL drivers need
5298 * support multiple interrupt vectors.
5300 if (dev->data->dev_conf.intr_conf.rxq == 0 ||
5301 !rte_intr_cap_multiple(intr_handle))
5304 rte_intr_disable(intr_handle);
5305 intr_vector = hw->used_rx_queues;
5306 /* creates event fd for each intr vector when MSIX is used */
5307 if (rte_intr_efd_enable(intr_handle, intr_vector))
5310 /* Allocate vector list */
5311 if (rte_intr_vec_list_alloc(intr_handle, "intr_vec",
5312 hw->used_rx_queues)) {
5313 hns3_err(hw, "failed to allocate %u rx_queues intr_vec",
5314 hw->used_rx_queues);
5316 goto alloc_intr_vec_error;
5319 if (rte_intr_allow_others(intr_handle)) {
5320 vec = RTE_INTR_VEC_RXTX_OFFSET;
5321 base = RTE_INTR_VEC_RXTX_OFFSET;
5324 for (q_id = 0; q_id < hw->used_rx_queues; q_id++) {
5325 ret = hns3_bind_ring_with_vector(hw, vec, true,
5326 HNS3_RING_TYPE_RX, q_id);
5328 goto bind_vector_error;
5330 if (rte_intr_vec_list_index_set(intr_handle, q_id, vec))
5331 goto bind_vector_error;
5333 * If there are not enough efds (e.g. not enough interrupt),
5334 * remaining queues will be bond to the last interrupt.
5336 if (vec < base + rte_intr_nb_efd_get(intr_handle) - 1)
5339 rte_intr_enable(intr_handle);
5343 rte_intr_vec_list_free(intr_handle);
5344 alloc_intr_vec_error:
5345 rte_intr_efd_disable(intr_handle);
5350 hns3_restore_rx_interrupt(struct hns3_hw *hw)
5352 struct rte_eth_dev *dev = &rte_eth_devices[hw->data->port_id];
5353 struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
5354 struct rte_intr_handle *intr_handle = pci_dev->intr_handle;
5358 if (dev->data->dev_conf.intr_conf.rxq == 0)
5361 if (rte_intr_dp_is_en(intr_handle)) {
5362 for (q_id = 0; q_id < hw->used_rx_queues; q_id++) {
5363 ret = hns3_bind_ring_with_vector(hw,
5364 rte_intr_vec_list_index_get(intr_handle,
5366 true, HNS3_RING_TYPE_RX, q_id);
5376 hns3_restore_filter(struct rte_eth_dev *dev)
5378 hns3_restore_rss_filter(dev);
5382 hns3_dev_start(struct rte_eth_dev *dev)
5384 struct hns3_adapter *hns = dev->data->dev_private;
5385 struct hns3_hw *hw = &hns->hw;
5386 bool old_state = hw->set_link_down;
5389 PMD_INIT_FUNC_TRACE();
5390 if (__atomic_load_n(&hw->reset.resetting, __ATOMIC_RELAXED))
5393 rte_spinlock_lock(&hw->lock);
5394 hw->adapter_state = HNS3_NIC_STARTING;
5397 * If the dev_set_link_down() API has been called, the "set_link_down"
5398 * flag can be cleared by dev_start() API. In addition, the flag should
5399 * also be cleared before calling hns3_do_start() so that MAC can be
5400 * enabled in dev_start stage.
5402 hw->set_link_down = false;
5403 ret = hns3_do_start(hns, true);
5407 ret = hns3_map_rx_interrupt(dev);
5409 goto map_rx_inter_err;
5412 * There are three register used to control the status of a TQP
5413 * (contains a pair of Tx queue and Rx queue) in the new version network
5414 * engine. One is used to control the enabling of Tx queue, the other is
5415 * used to control the enabling of Rx queue, and the last is the master
5416 * switch used to control the enabling of the tqp. The Tx register and
5417 * TQP register must be enabled at the same time to enable a Tx queue.
5418 * The same applies to the Rx queue. For the older network engine, this
5419 * function only refresh the enabled flag, and it is used to update the
5420 * status of queue in the dpdk framework.
5422 ret = hns3_start_all_txqs(dev);
5424 goto map_rx_inter_err;
5426 ret = hns3_start_all_rxqs(dev);
5428 goto start_all_rxqs_fail;
5430 hw->adapter_state = HNS3_NIC_STARTED;
5431 rte_spinlock_unlock(&hw->lock);
5433 hns3_rx_scattered_calc(dev);
5434 hns3_set_rxtx_function(dev);
5435 hns3_mp_req_start_rxtx(dev);
5437 hns3_restore_filter(dev);
5439 /* Enable interrupt of all rx queues before enabling queues */
5440 hns3_dev_all_rx_queue_intr_enable(hw, true);
5443 * After finished the initialization, enable tqps to receive/transmit
5444 * packets and refresh all queue status.
5446 hns3_start_tqps(hw);
5448 hns3_tm_dev_start_proc(hw);
5450 if (dev->data->dev_conf.intr_conf.lsc != 0)
5451 hns3_dev_link_update(dev, 0);
5452 rte_eal_alarm_set(HNS3_SERVICE_INTERVAL, hns3_service_handler, dev);
5454 hns3_info(hw, "hns3 dev start successful!");
5458 start_all_rxqs_fail:
5459 hns3_stop_all_txqs(dev);
5461 (void)hns3_do_stop(hns);
5463 hw->set_link_down = old_state;
5464 hw->adapter_state = HNS3_NIC_CONFIGURED;
5465 rte_spinlock_unlock(&hw->lock);
5471 hns3_do_stop(struct hns3_adapter *hns)
5473 struct hns3_hw *hw = &hns->hw;
5477 * The "hns3_do_stop" function will also be called by .stop_service to
5478 * prepare reset. At the time of global or IMP reset, the command cannot
5479 * be sent to stop the tx/rx queues. The mbuf in Tx/Rx queues may be
5480 * accessed during the reset process. So the mbuf can not be released
5481 * during reset and is required to be released after the reset is
5484 if (__atomic_load_n(&hw->reset.resetting, __ATOMIC_RELAXED) == 0)
5485 hns3_dev_release_mbufs(hns);
5487 ret = hns3_cfg_mac_mode(hw, false);
5490 hw->mac.link_status = RTE_ETH_LINK_DOWN;
5492 if (__atomic_load_n(&hw->reset.disable_cmd, __ATOMIC_RELAXED) == 0) {
5493 hns3_configure_all_mac_addr(hns, true);
5494 ret = hns3_reset_all_tqps(hns);
5496 hns3_err(hw, "failed to reset all queues ret = %d.",
5506 hns3_unmap_rx_interrupt(struct rte_eth_dev *dev)
5508 struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
5509 struct rte_intr_handle *intr_handle = pci_dev->intr_handle;
5510 struct hns3_adapter *hns = dev->data->dev_private;
5511 struct hns3_hw *hw = &hns->hw;
5512 uint8_t base = RTE_INTR_VEC_ZERO_OFFSET;
5513 uint8_t vec = RTE_INTR_VEC_ZERO_OFFSET;
5516 if (dev->data->dev_conf.intr_conf.rxq == 0)
5519 /* unmap the ring with vector */
5520 if (rte_intr_allow_others(intr_handle)) {
5521 vec = RTE_INTR_VEC_RXTX_OFFSET;
5522 base = RTE_INTR_VEC_RXTX_OFFSET;
5524 if (rte_intr_dp_is_en(intr_handle)) {
5525 for (q_id = 0; q_id < hw->used_rx_queues; q_id++) {
5526 (void)hns3_bind_ring_with_vector(hw, vec, false,
5529 if (vec < base + rte_intr_nb_efd_get(intr_handle)
5534 /* Clean datapath event and queue/vec mapping */
5535 rte_intr_efd_disable(intr_handle);
5536 rte_intr_vec_list_free(intr_handle);
5540 hns3_dev_stop(struct rte_eth_dev *dev)
5542 struct hns3_adapter *hns = dev->data->dev_private;
5543 struct hns3_hw *hw = &hns->hw;
5545 PMD_INIT_FUNC_TRACE();
5546 dev->data->dev_started = 0;
5548 hw->adapter_state = HNS3_NIC_STOPPING;
5549 hns3_set_rxtx_function(dev);
5551 /* Disable datapath on secondary process. */
5552 hns3_mp_req_stop_rxtx(dev);
5553 /* Prevent crashes when queues are still in use. */
5554 rte_delay_ms(hw->cfg_max_queues);
5556 rte_spinlock_lock(&hw->lock);
5557 if (__atomic_load_n(&hw->reset.resetting, __ATOMIC_RELAXED) == 0) {
5558 hns3_tm_dev_stop_proc(hw);
5559 hns3_config_mac_tnl_int(hw, false);
5562 hns3_unmap_rx_interrupt(dev);
5563 hw->adapter_state = HNS3_NIC_CONFIGURED;
5565 hns3_rx_scattered_reset(dev);
5566 rte_eal_alarm_cancel(hns3_service_handler, dev);
5567 hns3_stop_report_lse(dev);
5568 rte_spinlock_unlock(&hw->lock);
5574 hns3_dev_close(struct rte_eth_dev *eth_dev)
5576 struct hns3_adapter *hns = eth_dev->data->dev_private;
5577 struct hns3_hw *hw = &hns->hw;
5580 if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
5581 hns3_mp_uninit(eth_dev);
5585 if (hw->adapter_state == HNS3_NIC_STARTED)
5586 ret = hns3_dev_stop(eth_dev);
5588 hw->adapter_state = HNS3_NIC_CLOSING;
5589 hns3_reset_abort(hns);
5590 hw->adapter_state = HNS3_NIC_CLOSED;
5592 hns3_configure_all_mc_mac_addr(hns, true);
5593 hns3_remove_all_vlan_table(hns);
5594 hns3_vlan_txvlan_cfg(hns, HNS3_PORT_BASE_VLAN_DISABLE, 0);
5595 hns3_uninit_pf(eth_dev);
5596 hns3_free_all_queues(eth_dev);
5597 rte_free(hw->reset.wait_data);
5598 hns3_mp_uninit(eth_dev);
5599 hns3_warn(hw, "Close port %u finished", hw->data->port_id);
5605 hns3_get_autoneg_rxtx_pause_copper(struct hns3_hw *hw, bool *rx_pause,
5608 struct hns3_mac *mac = &hw->mac;
5609 uint32_t advertising = mac->advertising;
5610 uint32_t lp_advertising = mac->lp_advertising;
5614 if (advertising & lp_advertising & HNS3_PHY_LINK_MODE_PAUSE_BIT) {
5617 } else if (advertising & lp_advertising &
5618 HNS3_PHY_LINK_MODE_ASYM_PAUSE_BIT) {
5619 if (advertising & HNS3_PHY_LINK_MODE_PAUSE_BIT)
5621 else if (lp_advertising & HNS3_PHY_LINK_MODE_PAUSE_BIT)
5626 static enum hns3_fc_mode
5627 hns3_get_autoneg_fc_mode(struct hns3_hw *hw)
5629 enum hns3_fc_mode current_mode;
5630 bool rx_pause = false;
5631 bool tx_pause = false;
5633 switch (hw->mac.media_type) {
5634 case HNS3_MEDIA_TYPE_COPPER:
5635 hns3_get_autoneg_rxtx_pause_copper(hw, &rx_pause, &tx_pause);
5639 * Flow control auto-negotiation is not supported for fiber and
5640 * backpalne media type.
5642 case HNS3_MEDIA_TYPE_FIBER:
5643 case HNS3_MEDIA_TYPE_BACKPLANE:
5644 hns3_err(hw, "autoneg FC mode can't be obtained, but flow control auto-negotiation is enabled.");
5645 current_mode = hw->requested_fc_mode;
5648 hns3_err(hw, "autoneg FC mode can't be obtained for unknown media type(%u).",
5649 hw->mac.media_type);
5650 current_mode = HNS3_FC_NONE;
5654 if (rx_pause && tx_pause)
5655 current_mode = HNS3_FC_FULL;
5657 current_mode = HNS3_FC_RX_PAUSE;
5659 current_mode = HNS3_FC_TX_PAUSE;
5661 current_mode = HNS3_FC_NONE;
5664 return current_mode;
5667 static enum hns3_fc_mode
5668 hns3_get_current_fc_mode(struct rte_eth_dev *dev)
5670 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
5671 struct hns3_pf *pf = HNS3_DEV_PRIVATE_TO_PF(dev->data->dev_private);
5672 struct hns3_mac *mac = &hw->mac;
5675 * When the flow control mode is obtained, the device may not complete
5676 * auto-negotiation. It is necessary to wait for link establishment.
5678 (void)hns3_dev_link_update(dev, 1);
5681 * If the link auto-negotiation of the nic is disabled, or the flow
5682 * control auto-negotiation is not supported, the forced flow control
5685 if (mac->link_autoneg == 0 || !pf->support_fc_autoneg)
5686 return hw->requested_fc_mode;
5688 return hns3_get_autoneg_fc_mode(hw);
5692 hns3_flow_ctrl_get(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
5694 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
5695 struct hns3_pf *pf = HNS3_DEV_PRIVATE_TO_PF(dev->data->dev_private);
5696 enum hns3_fc_mode current_mode;
5698 current_mode = hns3_get_current_fc_mode(dev);
5699 switch (current_mode) {
5701 fc_conf->mode = RTE_ETH_FC_FULL;
5703 case HNS3_FC_TX_PAUSE:
5704 fc_conf->mode = RTE_ETH_FC_TX_PAUSE;
5706 case HNS3_FC_RX_PAUSE:
5707 fc_conf->mode = RTE_ETH_FC_RX_PAUSE;
5711 fc_conf->mode = RTE_ETH_FC_NONE;
5715 fc_conf->pause_time = pf->pause_time;
5716 fc_conf->autoneg = pf->support_fc_autoneg ? hw->mac.link_autoneg : 0;
5722 hns3_check_fc_autoneg_valid(struct hns3_hw *hw, uint8_t autoneg)
5724 struct hns3_pf *pf = HNS3_DEV_HW_TO_PF(hw);
5726 if (!pf->support_fc_autoneg) {
5728 hns3_err(hw, "unsupported fc auto-negotiation setting.");
5733 * Flow control auto-negotiation of the NIC is not supported,
5734 * but other auto-negotiation features may be supported.
5736 if (autoneg != hw->mac.link_autoneg) {
5737 hns3_err(hw, "please use 'link_speeds' in struct rte_eth_conf to disable autoneg!");
5745 * If flow control auto-negotiation of the NIC is supported, all
5746 * auto-negotiation features are supported.
5748 if (autoneg != hw->mac.link_autoneg) {
5749 hns3_err(hw, "please use 'link_speeds' in struct rte_eth_conf to change autoneg!");
5757 hns3_flow_ctrl_set(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
5759 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
5760 struct hns3_pf *pf = HNS3_DEV_PRIVATE_TO_PF(dev->data->dev_private);
5763 if (fc_conf->high_water || fc_conf->low_water ||
5764 fc_conf->send_xon || fc_conf->mac_ctrl_frame_fwd) {
5765 hns3_err(hw, "Unsupported flow control settings specified, "
5766 "high_water(%u), low_water(%u), send_xon(%u) and "
5767 "mac_ctrl_frame_fwd(%u) must be set to '0'",
5768 fc_conf->high_water, fc_conf->low_water,
5769 fc_conf->send_xon, fc_conf->mac_ctrl_frame_fwd);
5773 ret = hns3_check_fc_autoneg_valid(hw, fc_conf->autoneg);
5777 if (!fc_conf->pause_time) {
5778 hns3_err(hw, "Invalid pause time %u setting.",
5779 fc_conf->pause_time);
5783 if (!(hw->current_fc_status == HNS3_FC_STATUS_NONE ||
5784 hw->current_fc_status == HNS3_FC_STATUS_MAC_PAUSE)) {
5785 hns3_err(hw, "PFC is enabled. Cannot set MAC pause. "
5786 "current_fc_status = %d", hw->current_fc_status);
5790 if (hw->num_tc > 1 && !pf->support_multi_tc_pause) {
5791 hns3_err(hw, "in multi-TC scenarios, MAC pause is not supported.");
5795 rte_spinlock_lock(&hw->lock);
5796 ret = hns3_fc_enable(dev, fc_conf);
5797 rte_spinlock_unlock(&hw->lock);
5803 hns3_priority_flow_ctrl_set(struct rte_eth_dev *dev,
5804 struct rte_eth_pfc_conf *pfc_conf)
5806 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
5809 if (!hns3_dev_get_support(hw, DCB)) {
5810 hns3_err(hw, "This port does not support dcb configurations.");
5814 if (pfc_conf->fc.high_water || pfc_conf->fc.low_water ||
5815 pfc_conf->fc.send_xon || pfc_conf->fc.mac_ctrl_frame_fwd) {
5816 hns3_err(hw, "Unsupported flow control settings specified, "
5817 "high_water(%u), low_water(%u), send_xon(%u) and "
5818 "mac_ctrl_frame_fwd(%u) must be set to '0'",
5819 pfc_conf->fc.high_water, pfc_conf->fc.low_water,
5820 pfc_conf->fc.send_xon,
5821 pfc_conf->fc.mac_ctrl_frame_fwd);
5824 if (pfc_conf->fc.autoneg) {
5825 hns3_err(hw, "Unsupported fc auto-negotiation setting.");
5828 if (pfc_conf->fc.pause_time == 0) {
5829 hns3_err(hw, "Invalid pause time %u setting.",
5830 pfc_conf->fc.pause_time);
5834 if (!(hw->current_fc_status == HNS3_FC_STATUS_NONE ||
5835 hw->current_fc_status == HNS3_FC_STATUS_PFC)) {
5836 hns3_err(hw, "MAC pause is enabled. Cannot set PFC."
5837 "current_fc_status = %d", hw->current_fc_status);
5841 rte_spinlock_lock(&hw->lock);
5842 ret = hns3_dcb_pfc_enable(dev, pfc_conf);
5843 rte_spinlock_unlock(&hw->lock);
5849 hns3_get_dcb_info(struct rte_eth_dev *dev, struct rte_eth_dcb_info *dcb_info)
5851 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
5852 struct hns3_pf *pf = HNS3_DEV_PRIVATE_TO_PF(dev->data->dev_private);
5853 enum rte_eth_rx_mq_mode mq_mode = dev->data->dev_conf.rxmode.mq_mode;
5856 rte_spinlock_lock(&hw->lock);
5857 if ((uint32_t)mq_mode & RTE_ETH_MQ_RX_DCB_FLAG)
5858 dcb_info->nb_tcs = pf->local_max_tc;
5860 dcb_info->nb_tcs = 1;
5862 for (i = 0; i < HNS3_MAX_USER_PRIO; i++)
5863 dcb_info->prio_tc[i] = hw->dcb_info.prio_tc[i];
5864 for (i = 0; i < dcb_info->nb_tcs; i++)
5865 dcb_info->tc_bws[i] = hw->dcb_info.pg_info[0].tc_dwrr[i];
5867 for (i = 0; i < hw->num_tc; i++) {
5868 dcb_info->tc_queue.tc_rxq[0][i].base = hw->alloc_rss_size * i;
5869 dcb_info->tc_queue.tc_txq[0][i].base =
5870 hw->tc_queue[i].tqp_offset;
5871 dcb_info->tc_queue.tc_rxq[0][i].nb_queue = hw->alloc_rss_size;
5872 dcb_info->tc_queue.tc_txq[0][i].nb_queue =
5873 hw->tc_queue[i].tqp_count;
5875 rte_spinlock_unlock(&hw->lock);
5881 hns3_reinit_dev(struct hns3_adapter *hns)
5883 struct hns3_hw *hw = &hns->hw;
5886 ret = hns3_cmd_init(hw);
5888 hns3_err(hw, "Failed to init cmd: %d", ret);
5892 ret = hns3_reset_all_tqps(hns);
5894 hns3_err(hw, "Failed to reset all queues: %d", ret);
5898 ret = hns3_init_hardware(hns);
5900 hns3_err(hw, "Failed to init hardware: %d", ret);
5904 ret = hns3_enable_hw_error_intr(hns, true);
5906 hns3_err(hw, "fail to enable hw error interrupts: %d",
5910 hns3_info(hw, "Reset done, driver initialization finished.");
5916 is_pf_reset_done(struct hns3_hw *hw)
5918 uint32_t val, reg, reg_bit;
5920 switch (hw->reset.level) {
5921 case HNS3_IMP_RESET:
5922 reg = HNS3_GLOBAL_RESET_REG;
5923 reg_bit = HNS3_IMP_RESET_BIT;
5925 case HNS3_GLOBAL_RESET:
5926 reg = HNS3_GLOBAL_RESET_REG;
5927 reg_bit = HNS3_GLOBAL_RESET_BIT;
5929 case HNS3_FUNC_RESET:
5930 reg = HNS3_FUN_RST_ING;
5931 reg_bit = HNS3_FUN_RST_ING_B;
5933 case HNS3_FLR_RESET:
5935 hns3_err(hw, "Wait for unsupported reset level: %d",
5939 val = hns3_read_dev(hw, reg);
5940 if (hns3_get_bit(val, reg_bit))
5947 hns3_is_reset_pending(struct hns3_adapter *hns)
5949 struct hns3_hw *hw = &hns->hw;
5950 enum hns3_reset_level reset;
5952 hns3_check_event_cause(hns, NULL);
5953 reset = hns3_get_reset_level(hns, &hw->reset.pending);
5954 if (reset != HNS3_NONE_RESET && hw->reset.level != HNS3_NONE_RESET &&
5955 hw->reset.level < reset) {
5956 hns3_warn(hw, "High level reset %d is pending", reset);
5959 reset = hns3_get_reset_level(hns, &hw->reset.request);
5960 if (reset != HNS3_NONE_RESET && hw->reset.level != HNS3_NONE_RESET &&
5961 hw->reset.level < reset) {
5962 hns3_warn(hw, "High level reset %d is request", reset);
5969 hns3_wait_hardware_ready(struct hns3_adapter *hns)
5971 struct hns3_hw *hw = &hns->hw;
5972 struct hns3_wait_data *wait_data = hw->reset.wait_data;
5975 if (wait_data->result == HNS3_WAIT_SUCCESS)
5977 else if (wait_data->result == HNS3_WAIT_TIMEOUT) {
5978 hns3_clock_gettime(&tv);
5979 hns3_warn(hw, "Reset step4 hardware not ready after reset time=%ld.%.6ld",
5980 tv.tv_sec, tv.tv_usec);
5982 } else if (wait_data->result == HNS3_WAIT_REQUEST)
5985 wait_data->hns = hns;
5986 wait_data->check_completion = is_pf_reset_done;
5987 wait_data->end_ms = (uint64_t)HNS3_RESET_WAIT_CNT *
5988 HNS3_RESET_WAIT_MS + hns3_clock_gettime_ms();
5989 wait_data->interval = HNS3_RESET_WAIT_MS * USEC_PER_MSEC;
5990 wait_data->count = HNS3_RESET_WAIT_CNT;
5991 wait_data->result = HNS3_WAIT_REQUEST;
5992 rte_eal_alarm_set(wait_data->interval, hns3_wait_callback, wait_data);
5997 hns3_func_reset_cmd(struct hns3_hw *hw, int func_id)
5999 struct hns3_cmd_desc desc;
6000 struct hns3_reset_cmd *req = (struct hns3_reset_cmd *)desc.data;
6002 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_CFG_RST_TRIGGER, false);
6003 hns3_set_bit(req->mac_func_reset, HNS3_CFG_RESET_FUNC_B, 1);
6004 req->fun_reset_vfid = func_id;
6006 return hns3_cmd_send(hw, &desc, 1);
6010 hns3_imp_reset_cmd(struct hns3_hw *hw)
6012 struct hns3_cmd_desc desc;
6014 hns3_cmd_setup_basic_desc(&desc, 0xFFFE, false);
6015 desc.data[0] = 0xeedd;
6017 return hns3_cmd_send(hw, &desc, 1);
6021 hns3_msix_process(struct hns3_adapter *hns, enum hns3_reset_level reset_level)
6023 struct hns3_hw *hw = &hns->hw;
6027 hns3_clock_gettime(&tv);
6028 if (hns3_read_dev(hw, HNS3_GLOBAL_RESET_REG) ||
6029 hns3_read_dev(hw, HNS3_FUN_RST_ING)) {
6030 hns3_warn(hw, "Don't process msix during resetting time=%ld.%.6ld",
6031 tv.tv_sec, tv.tv_usec);
6035 switch (reset_level) {
6036 case HNS3_IMP_RESET:
6037 hns3_imp_reset_cmd(hw);
6038 hns3_warn(hw, "IMP Reset requested time=%ld.%.6ld",
6039 tv.tv_sec, tv.tv_usec);
6041 case HNS3_GLOBAL_RESET:
6042 val = hns3_read_dev(hw, HNS3_GLOBAL_RESET_REG);
6043 hns3_set_bit(val, HNS3_GLOBAL_RESET_BIT, 1);
6044 hns3_write_dev(hw, HNS3_GLOBAL_RESET_REG, val);
6045 hns3_warn(hw, "Global Reset requested time=%ld.%.6ld",
6046 tv.tv_sec, tv.tv_usec);
6048 case HNS3_FUNC_RESET:
6049 hns3_warn(hw, "PF Reset requested time=%ld.%.6ld",
6050 tv.tv_sec, tv.tv_usec);
6051 /* schedule again to check later */
6052 hns3_atomic_set_bit(HNS3_FUNC_RESET, &hw->reset.pending);
6053 hns3_schedule_reset(hns);
6056 hns3_warn(hw, "Unsupported reset level: %d", reset_level);
6059 hns3_atomic_clear_bit(reset_level, &hw->reset.request);
6062 static enum hns3_reset_level
6063 hns3_get_reset_level(struct hns3_adapter *hns, uint64_t *levels)
6065 struct hns3_hw *hw = &hns->hw;
6066 enum hns3_reset_level reset_level = HNS3_NONE_RESET;
6068 /* Return the highest priority reset level amongst all */
6069 if (hns3_atomic_test_bit(HNS3_IMP_RESET, levels))
6070 reset_level = HNS3_IMP_RESET;
6071 else if (hns3_atomic_test_bit(HNS3_GLOBAL_RESET, levels))
6072 reset_level = HNS3_GLOBAL_RESET;
6073 else if (hns3_atomic_test_bit(HNS3_FUNC_RESET, levels))
6074 reset_level = HNS3_FUNC_RESET;
6075 else if (hns3_atomic_test_bit(HNS3_FLR_RESET, levels))
6076 reset_level = HNS3_FLR_RESET;
6078 if (hw->reset.level != HNS3_NONE_RESET && reset_level < hw->reset.level)
6079 return HNS3_NONE_RESET;
6085 hns3_record_imp_error(struct hns3_adapter *hns)
6087 struct hns3_hw *hw = &hns->hw;
6090 reg_val = hns3_read_dev(hw, HNS3_VECTOR0_OTER_EN_REG);
6091 if (hns3_get_bit(reg_val, HNS3_VECTOR0_IMP_RD_POISON_B)) {
6092 hns3_warn(hw, "Detected IMP RD poison!");
6093 hns3_set_bit(reg_val, HNS3_VECTOR0_IMP_RD_POISON_B, 0);
6094 hns3_write_dev(hw, HNS3_VECTOR0_OTER_EN_REG, reg_val);
6097 if (hns3_get_bit(reg_val, HNS3_VECTOR0_IMP_CMDQ_ERR_B)) {
6098 hns3_warn(hw, "Detected IMP CMDQ error!");
6099 hns3_set_bit(reg_val, HNS3_VECTOR0_IMP_CMDQ_ERR_B, 0);
6100 hns3_write_dev(hw, HNS3_VECTOR0_OTER_EN_REG, reg_val);
6105 hns3_prepare_reset(struct hns3_adapter *hns)
6107 struct hns3_hw *hw = &hns->hw;
6111 switch (hw->reset.level) {
6112 case HNS3_FUNC_RESET:
6113 ret = hns3_func_reset_cmd(hw, HNS3_PF_FUNC_ID);
6118 * After performaning pf reset, it is not necessary to do the
6119 * mailbox handling or send any command to firmware, because
6120 * any mailbox handling or command to firmware is only valid
6121 * after hns3_cmd_init is called.
6123 __atomic_store_n(&hw->reset.disable_cmd, 1, __ATOMIC_RELAXED);
6124 hw->reset.stats.request_cnt++;
6126 case HNS3_IMP_RESET:
6127 hns3_record_imp_error(hns);
6128 reg_val = hns3_read_dev(hw, HNS3_VECTOR0_OTER_EN_REG);
6129 hns3_write_dev(hw, HNS3_VECTOR0_OTER_EN_REG, reg_val |
6130 BIT(HNS3_VECTOR0_IMP_RESET_INT_B));
6139 hns3_set_rst_done(struct hns3_hw *hw)
6141 struct hns3_pf_rst_done_cmd *req;
6142 struct hns3_cmd_desc desc;
6144 req = (struct hns3_pf_rst_done_cmd *)desc.data;
6145 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_PF_RST_DONE, false);
6146 req->pf_rst_done |= HNS3_PF_RESET_DONE_BIT;
6147 return hns3_cmd_send(hw, &desc, 1);
6151 hns3_stop_service(struct hns3_adapter *hns)
6153 struct hns3_hw *hw = &hns->hw;
6154 struct rte_eth_dev *eth_dev;
6156 eth_dev = &rte_eth_devices[hw->data->port_id];
6157 hw->mac.link_status = RTE_ETH_LINK_DOWN;
6158 if (hw->adapter_state == HNS3_NIC_STARTED) {
6159 rte_eal_alarm_cancel(hns3_service_handler, eth_dev);
6160 hns3_update_linkstatus_and_event(hw, false);
6163 hns3_set_rxtx_function(eth_dev);
6165 /* Disable datapath on secondary process. */
6166 hns3_mp_req_stop_rxtx(eth_dev);
6167 rte_delay_ms(hw->cfg_max_queues);
6169 rte_spinlock_lock(&hw->lock);
6170 if (hns->hw.adapter_state == HNS3_NIC_STARTED ||
6171 hw->adapter_state == HNS3_NIC_STOPPING) {
6172 hns3_enable_all_queues(hw, false);
6174 hw->reset.mbuf_deferred_free = true;
6176 hw->reset.mbuf_deferred_free = false;
6179 * It is cumbersome for hardware to pick-and-choose entries for deletion
6180 * from table space. Hence, for function reset software intervention is
6181 * required to delete the entries
6183 if (__atomic_load_n(&hw->reset.disable_cmd, __ATOMIC_RELAXED) == 0)
6184 hns3_configure_all_mc_mac_addr(hns, true);
6185 rte_spinlock_unlock(&hw->lock);
6191 hns3_start_service(struct hns3_adapter *hns)
6193 struct hns3_hw *hw = &hns->hw;
6194 struct rte_eth_dev *eth_dev;
6196 if (hw->reset.level == HNS3_IMP_RESET ||
6197 hw->reset.level == HNS3_GLOBAL_RESET)
6198 hns3_set_rst_done(hw);
6199 eth_dev = &rte_eth_devices[hw->data->port_id];
6200 hns3_set_rxtx_function(eth_dev);
6201 hns3_mp_req_start_rxtx(eth_dev);
6202 if (hw->adapter_state == HNS3_NIC_STARTED) {
6204 * This API parent function already hold the hns3_hw.lock, the
6205 * hns3_service_handler may report lse, in bonding application
6206 * it will call driver's ops which may acquire the hns3_hw.lock
6207 * again, thus lead to deadlock.
6208 * We defer calls hns3_service_handler to avoid the deadlock.
6210 rte_eal_alarm_set(HNS3_SERVICE_QUICK_INTERVAL,
6211 hns3_service_handler, eth_dev);
6213 /* Enable interrupt of all rx queues before enabling queues */
6214 hns3_dev_all_rx_queue_intr_enable(hw, true);
6216 * Enable state of each rxq and txq will be recovered after
6217 * reset, so we need to restore them before enable all tqps;
6219 hns3_restore_tqp_enable_state(hw);
6221 * When finished the initialization, enable queues to receive
6222 * and transmit packets.
6224 hns3_enable_all_queues(hw, true);
6231 hns3_restore_conf(struct hns3_adapter *hns)
6233 struct hns3_hw *hw = &hns->hw;
6236 ret = hns3_configure_all_mac_addr(hns, false);
6240 ret = hns3_configure_all_mc_mac_addr(hns, false);
6244 ret = hns3_dev_promisc_restore(hns);
6248 ret = hns3_restore_vlan_table(hns);
6252 ret = hns3_restore_vlan_conf(hns);
6256 ret = hns3_restore_all_fdir_filter(hns);
6260 ret = hns3_restore_ptp(hns);
6264 ret = hns3_restore_rx_interrupt(hw);
6268 ret = hns3_restore_gro_conf(hw);
6272 ret = hns3_restore_fec(hw);
6276 if (hns->hw.adapter_state == HNS3_NIC_STARTED) {
6277 ret = hns3_do_start(hns, false);
6280 hns3_info(hw, "hns3 dev restart successful!");
6281 } else if (hw->adapter_state == HNS3_NIC_STOPPING)
6282 hw->adapter_state = HNS3_NIC_CONFIGURED;
6286 hns3_configure_all_mc_mac_addr(hns, true);
6288 hns3_configure_all_mac_addr(hns, true);
6293 hns3_reset_service(void *param)
6295 struct hns3_adapter *hns = (struct hns3_adapter *)param;
6296 struct hns3_hw *hw = &hns->hw;
6297 enum hns3_reset_level reset_level;
6298 struct timeval tv_delta;
6299 struct timeval tv_start;
6305 * The interrupt is not triggered within the delay time.
6306 * The interrupt may have been lost. It is necessary to handle
6307 * the interrupt to recover from the error.
6309 if (__atomic_load_n(&hw->reset.schedule, __ATOMIC_RELAXED) ==
6310 SCHEDULE_DEFERRED) {
6311 __atomic_store_n(&hw->reset.schedule, SCHEDULE_REQUESTED,
6313 hns3_err(hw, "Handling interrupts in delayed tasks");
6314 hns3_interrupt_handler(&rte_eth_devices[hw->data->port_id]);
6315 reset_level = hns3_get_reset_level(hns, &hw->reset.pending);
6316 if (reset_level == HNS3_NONE_RESET) {
6317 hns3_err(hw, "No reset level is set, try IMP reset");
6318 hns3_atomic_set_bit(HNS3_IMP_RESET, &hw->reset.pending);
6321 __atomic_store_n(&hw->reset.schedule, SCHEDULE_NONE, __ATOMIC_RELAXED);
6324 * Check if there is any ongoing reset in the hardware. This status can
6325 * be checked from reset_pending. If there is then, we need to wait for
6326 * hardware to complete reset.
6327 * a. If we are able to figure out in reasonable time that hardware
6328 * has fully resetted then, we can proceed with driver, client
6330 * b. else, we can come back later to check this status so re-sched
6333 reset_level = hns3_get_reset_level(hns, &hw->reset.pending);
6334 if (reset_level != HNS3_NONE_RESET) {
6335 hns3_clock_gettime(&tv_start);
6336 ret = hns3_reset_process(hns, reset_level);
6337 hns3_clock_gettime(&tv);
6338 timersub(&tv, &tv_start, &tv_delta);
6339 msec = hns3_clock_calctime_ms(&tv_delta);
6340 if (msec > HNS3_RESET_PROCESS_MS)
6341 hns3_err(hw, "%d handle long time delta %" PRIu64
6342 " ms time=%ld.%.6ld",
6343 hw->reset.level, msec,
6344 tv.tv_sec, tv.tv_usec);
6349 /* Check if we got any *new* reset requests to be honored */
6350 reset_level = hns3_get_reset_level(hns, &hw->reset.request);
6351 if (reset_level != HNS3_NONE_RESET)
6352 hns3_msix_process(hns, reset_level);
6356 hns3_get_speed_capa_num(uint16_t device_id)
6360 switch (device_id) {
6361 case HNS3_DEV_ID_25GE:
6362 case HNS3_DEV_ID_25GE_RDMA:
6365 case HNS3_DEV_ID_100G_RDMA_MACSEC:
6366 case HNS3_DEV_ID_200G_RDMA:
6378 hns3_get_speed_fec_capa(struct rte_eth_fec_capa *speed_fec_capa,
6381 switch (device_id) {
6382 case HNS3_DEV_ID_25GE:
6384 case HNS3_DEV_ID_25GE_RDMA:
6385 speed_fec_capa[0].speed = speed_fec_capa_tbl[1].speed;
6386 speed_fec_capa[0].capa = speed_fec_capa_tbl[1].capa;
6388 /* In HNS3 device, the 25G NIC is compatible with 10G rate */
6389 speed_fec_capa[1].speed = speed_fec_capa_tbl[0].speed;
6390 speed_fec_capa[1].capa = speed_fec_capa_tbl[0].capa;
6392 case HNS3_DEV_ID_100G_RDMA_MACSEC:
6393 speed_fec_capa[0].speed = speed_fec_capa_tbl[4].speed;
6394 speed_fec_capa[0].capa = speed_fec_capa_tbl[4].capa;
6396 case HNS3_DEV_ID_200G_RDMA:
6397 speed_fec_capa[0].speed = speed_fec_capa_tbl[5].speed;
6398 speed_fec_capa[0].capa = speed_fec_capa_tbl[5].capa;
6408 hns3_fec_get_capability(struct rte_eth_dev *dev,
6409 struct rte_eth_fec_capa *speed_fec_capa,
6412 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
6413 struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
6414 uint16_t device_id = pci_dev->id.device_id;
6415 unsigned int capa_num;
6418 capa_num = hns3_get_speed_capa_num(device_id);
6419 if (capa_num == 0) {
6420 hns3_err(hw, "device(0x%x) is not supported by hns3 PMD",
6425 if (speed_fec_capa == NULL || num < capa_num)
6428 ret = hns3_get_speed_fec_capa(speed_fec_capa, device_id);
6436 get_current_fec_auto_state(struct hns3_hw *hw, uint8_t *state)
6438 struct hns3_config_fec_cmd *req;
6439 struct hns3_cmd_desc desc;
6443 * CMD(HNS3_OPC_CONFIG_FEC_MODE) read is not supported
6444 * in device of link speed
6447 if (hw->mac.link_speed < RTE_ETH_SPEED_NUM_10G) {
6452 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_CONFIG_FEC_MODE, true);
6453 req = (struct hns3_config_fec_cmd *)desc.data;
6454 ret = hns3_cmd_send(hw, &desc, 1);
6456 hns3_err(hw, "get current fec auto state failed, ret = %d",
6461 *state = req->fec_mode & (1U << HNS3_MAC_CFG_FEC_AUTO_EN_B);
6466 hns3_fec_get_internal(struct hns3_hw *hw, uint32_t *fec_capa)
6468 struct hns3_sfp_info_cmd *resp;
6469 uint32_t tmp_fec_capa;
6471 struct hns3_cmd_desc desc;
6475 * If link is down and AUTO is enabled, AUTO is returned, otherwise,
6476 * configured FEC mode is returned.
6477 * If link is up, current FEC mode is returned.
6479 if (hw->mac.link_status == RTE_ETH_LINK_DOWN) {
6480 ret = get_current_fec_auto_state(hw, &auto_state);
6484 if (auto_state == 0x1) {
6485 *fec_capa = RTE_ETH_FEC_MODE_CAPA_MASK(AUTO);
6490 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_GET_SFP_INFO, true);
6491 resp = (struct hns3_sfp_info_cmd *)desc.data;
6492 resp->query_type = HNS3_ACTIVE_QUERY;
6494 ret = hns3_cmd_send(hw, &desc, 1);
6495 if (ret == -EOPNOTSUPP) {
6496 hns3_err(hw, "IMP do not support get FEC, ret = %d", ret);
6499 hns3_err(hw, "get FEC failed, ret = %d", ret);
6504 * FEC mode order defined in hns3 hardware is inconsistend with
6505 * that defined in the ethdev library. So the sequence needs
6508 switch (resp->active_fec) {
6509 case HNS3_HW_FEC_MODE_NOFEC:
6510 tmp_fec_capa = RTE_ETH_FEC_MODE_CAPA_MASK(NOFEC);
6512 case HNS3_HW_FEC_MODE_BASER:
6513 tmp_fec_capa = RTE_ETH_FEC_MODE_CAPA_MASK(BASER);
6515 case HNS3_HW_FEC_MODE_RS:
6516 tmp_fec_capa = RTE_ETH_FEC_MODE_CAPA_MASK(RS);
6519 tmp_fec_capa = RTE_ETH_FEC_MODE_CAPA_MASK(NOFEC);
6523 *fec_capa = tmp_fec_capa;
6528 hns3_fec_get(struct rte_eth_dev *dev, uint32_t *fec_capa)
6530 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
6532 return hns3_fec_get_internal(hw, fec_capa);
6536 hns3_set_fec_hw(struct hns3_hw *hw, uint32_t mode)
6538 struct hns3_config_fec_cmd *req;
6539 struct hns3_cmd_desc desc;
6542 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_CONFIG_FEC_MODE, false);
6544 req = (struct hns3_config_fec_cmd *)desc.data;
6546 case RTE_ETH_FEC_MODE_CAPA_MASK(NOFEC):
6547 hns3_set_field(req->fec_mode, HNS3_MAC_CFG_FEC_MODE_M,
6548 HNS3_MAC_CFG_FEC_MODE_S, HNS3_MAC_FEC_OFF);
6550 case RTE_ETH_FEC_MODE_CAPA_MASK(BASER):
6551 hns3_set_field(req->fec_mode, HNS3_MAC_CFG_FEC_MODE_M,
6552 HNS3_MAC_CFG_FEC_MODE_S, HNS3_MAC_FEC_BASER);
6554 case RTE_ETH_FEC_MODE_CAPA_MASK(RS):
6555 hns3_set_field(req->fec_mode, HNS3_MAC_CFG_FEC_MODE_M,
6556 HNS3_MAC_CFG_FEC_MODE_S, HNS3_MAC_FEC_RS);
6558 case RTE_ETH_FEC_MODE_CAPA_MASK(AUTO):
6559 hns3_set_bit(req->fec_mode, HNS3_MAC_CFG_FEC_AUTO_EN_B, 1);
6564 ret = hns3_cmd_send(hw, &desc, 1);
6566 hns3_err(hw, "set fec mode failed, ret = %d", ret);
6572 get_current_speed_fec_cap(struct hns3_hw *hw, struct rte_eth_fec_capa *fec_capa)
6574 struct hns3_mac *mac = &hw->mac;
6577 switch (mac->link_speed) {
6578 case RTE_ETH_SPEED_NUM_10G:
6579 cur_capa = fec_capa[1].capa;
6581 case RTE_ETH_SPEED_NUM_25G:
6582 case RTE_ETH_SPEED_NUM_100G:
6583 case RTE_ETH_SPEED_NUM_200G:
6584 cur_capa = fec_capa[0].capa;
6595 is_fec_mode_one_bit_set(uint32_t mode)
6600 for (i = 0; i < sizeof(mode); i++)
6601 if (mode >> i & 0x1)
6604 return cnt == 1 ? true : false;
6608 hns3_fec_set(struct rte_eth_dev *dev, uint32_t mode)
6610 #define FEC_CAPA_NUM 2
6611 struct hns3_adapter *hns = dev->data->dev_private;
6612 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(hns);
6613 struct hns3_pf *pf = &hns->pf;
6615 struct rte_eth_fec_capa fec_capa[FEC_CAPA_NUM];
6617 uint32_t num = FEC_CAPA_NUM;
6620 ret = hns3_fec_get_capability(dev, fec_capa, num);
6624 /* HNS3 PMD driver only support one bit set mode, e.g. 0x1, 0x4 */
6625 if (!is_fec_mode_one_bit_set(mode)) {
6626 hns3_err(hw, "FEC mode(0x%x) not supported in HNS3 PMD, "
6627 "FEC mode should be only one bit set", mode);
6632 * Check whether the configured mode is within the FEC capability.
6633 * If not, the configured mode will not be supported.
6635 cur_capa = get_current_speed_fec_cap(hw, fec_capa);
6636 if (!(cur_capa & mode)) {
6637 hns3_err(hw, "unsupported FEC mode = 0x%x", mode);
6641 rte_spinlock_lock(&hw->lock);
6642 ret = hns3_set_fec_hw(hw, mode);
6644 rte_spinlock_unlock(&hw->lock);
6648 pf->fec_mode = mode;
6649 rte_spinlock_unlock(&hw->lock);
6655 hns3_restore_fec(struct hns3_hw *hw)
6657 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
6658 struct hns3_pf *pf = &hns->pf;
6659 uint32_t mode = pf->fec_mode;
6662 ret = hns3_set_fec_hw(hw, mode);
6664 hns3_err(hw, "restore fec mode(0x%x) failed, ret = %d",
6671 hns3_query_dev_fec_info(struct hns3_hw *hw)
6673 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
6674 struct hns3_pf *pf = HNS3_DEV_PRIVATE_TO_PF(hns);
6677 ret = hns3_fec_get_internal(hw, &pf->fec_mode);
6679 hns3_err(hw, "query device FEC info failed, ret = %d", ret);
6685 hns3_optical_module_existed(struct hns3_hw *hw)
6687 struct hns3_cmd_desc desc;
6691 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_GET_SFP_EXIST, true);
6692 ret = hns3_cmd_send(hw, &desc, 1);
6695 "fail to get optical module exist state, ret = %d.\n",
6699 existed = !!desc.data[0];
6705 hns3_get_module_eeprom_data(struct hns3_hw *hw, uint32_t offset,
6706 uint32_t len, uint8_t *data)
6708 #define HNS3_SFP_INFO_CMD_NUM 6
6709 #define HNS3_SFP_INFO_MAX_LEN \
6710 (HNS3_SFP_INFO_BD0_LEN + \
6711 (HNS3_SFP_INFO_CMD_NUM - 1) * HNS3_SFP_INFO_BDX_LEN)
6712 struct hns3_cmd_desc desc[HNS3_SFP_INFO_CMD_NUM];
6713 struct hns3_sfp_info_bd0_cmd *sfp_info_bd0;
6719 for (i = 0; i < HNS3_SFP_INFO_CMD_NUM; i++) {
6720 hns3_cmd_setup_basic_desc(&desc[i], HNS3_OPC_GET_SFP_EEPROM,
6722 if (i < HNS3_SFP_INFO_CMD_NUM - 1)
6723 desc[i].flag |= rte_cpu_to_le_16(HNS3_CMD_FLAG_NEXT);
6726 sfp_info_bd0 = (struct hns3_sfp_info_bd0_cmd *)desc[0].data;
6727 sfp_info_bd0->offset = rte_cpu_to_le_16((uint16_t)offset);
6728 read_len = RTE_MIN(len, HNS3_SFP_INFO_MAX_LEN);
6729 sfp_info_bd0->read_len = rte_cpu_to_le_16((uint16_t)read_len);
6731 ret = hns3_cmd_send(hw, desc, HNS3_SFP_INFO_CMD_NUM);
6733 hns3_err(hw, "fail to get module EEPROM info, ret = %d.\n",
6738 /* The data format in BD0 is different with the others. */
6739 copy_len = RTE_MIN(len, HNS3_SFP_INFO_BD0_LEN);
6740 memcpy(data, sfp_info_bd0->data, copy_len);
6741 read_len = copy_len;
6743 for (i = 1; i < HNS3_SFP_INFO_CMD_NUM; i++) {
6744 if (read_len >= len)
6747 copy_len = RTE_MIN(len - read_len, HNS3_SFP_INFO_BDX_LEN);
6748 memcpy(data + read_len, desc[i].data, copy_len);
6749 read_len += copy_len;
6752 return (int)read_len;
6756 hns3_get_module_eeprom(struct rte_eth_dev *dev,
6757 struct rte_dev_eeprom_info *info)
6759 struct hns3_adapter *hns = dev->data->dev_private;
6760 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(hns);
6761 uint32_t offset = info->offset;
6762 uint32_t len = info->length;
6763 uint8_t *data = info->data;
6764 uint32_t read_len = 0;
6766 if (hw->mac.media_type != HNS3_MEDIA_TYPE_FIBER)
6769 if (!hns3_optical_module_existed(hw)) {
6770 hns3_err(hw, "fail to read module EEPROM: no module is connected.\n");
6774 while (read_len < len) {
6776 ret = hns3_get_module_eeprom_data(hw, offset + read_len,
6788 hns3_get_module_info(struct rte_eth_dev *dev,
6789 struct rte_eth_dev_module_info *modinfo)
6791 #define HNS3_SFF8024_ID_SFP 0x03
6792 #define HNS3_SFF8024_ID_QSFP_8438 0x0c
6793 #define HNS3_SFF8024_ID_QSFP_8436_8636 0x0d
6794 #define HNS3_SFF8024_ID_QSFP28_8636 0x11
6795 #define HNS3_SFF_8636_V1_3 0x03
6796 struct hns3_adapter *hns = dev->data->dev_private;
6797 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(hns);
6798 struct rte_dev_eeprom_info info;
6799 struct hns3_sfp_type sfp_type;
6802 memset(&sfp_type, 0, sizeof(sfp_type));
6803 memset(&info, 0, sizeof(info));
6804 info.data = (uint8_t *)&sfp_type;
6805 info.length = sizeof(sfp_type);
6806 ret = hns3_get_module_eeprom(dev, &info);
6810 switch (sfp_type.type) {
6811 case HNS3_SFF8024_ID_SFP:
6812 modinfo->type = RTE_ETH_MODULE_SFF_8472;
6813 modinfo->eeprom_len = RTE_ETH_MODULE_SFF_8472_LEN;
6815 case HNS3_SFF8024_ID_QSFP_8438:
6816 modinfo->type = RTE_ETH_MODULE_SFF_8436;
6817 modinfo->eeprom_len = RTE_ETH_MODULE_SFF_8436_MAX_LEN;
6819 case HNS3_SFF8024_ID_QSFP_8436_8636:
6820 if (sfp_type.ext_type < HNS3_SFF_8636_V1_3) {
6821 modinfo->type = RTE_ETH_MODULE_SFF_8436;
6822 modinfo->eeprom_len = RTE_ETH_MODULE_SFF_8436_MAX_LEN;
6824 modinfo->type = RTE_ETH_MODULE_SFF_8636;
6825 modinfo->eeprom_len = RTE_ETH_MODULE_SFF_8636_MAX_LEN;
6828 case HNS3_SFF8024_ID_QSFP28_8636:
6829 modinfo->type = RTE_ETH_MODULE_SFF_8636;
6830 modinfo->eeprom_len = RTE_ETH_MODULE_SFF_8636_MAX_LEN;
6833 hns3_err(hw, "unknown module, type = %u, extra_type = %u.\n",
6834 sfp_type.type, sfp_type.ext_type);
6841 static const struct eth_dev_ops hns3_eth_dev_ops = {
6842 .dev_configure = hns3_dev_configure,
6843 .dev_start = hns3_dev_start,
6844 .dev_stop = hns3_dev_stop,
6845 .dev_close = hns3_dev_close,
6846 .promiscuous_enable = hns3_dev_promiscuous_enable,
6847 .promiscuous_disable = hns3_dev_promiscuous_disable,
6848 .allmulticast_enable = hns3_dev_allmulticast_enable,
6849 .allmulticast_disable = hns3_dev_allmulticast_disable,
6850 .mtu_set = hns3_dev_mtu_set,
6851 .stats_get = hns3_stats_get,
6852 .stats_reset = hns3_stats_reset,
6853 .xstats_get = hns3_dev_xstats_get,
6854 .xstats_get_names = hns3_dev_xstats_get_names,
6855 .xstats_reset = hns3_dev_xstats_reset,
6856 .xstats_get_by_id = hns3_dev_xstats_get_by_id,
6857 .xstats_get_names_by_id = hns3_dev_xstats_get_names_by_id,
6858 .dev_infos_get = hns3_dev_infos_get,
6859 .fw_version_get = hns3_fw_version_get,
6860 .rx_queue_setup = hns3_rx_queue_setup,
6861 .tx_queue_setup = hns3_tx_queue_setup,
6862 .rx_queue_release = hns3_dev_rx_queue_release,
6863 .tx_queue_release = hns3_dev_tx_queue_release,
6864 .rx_queue_start = hns3_dev_rx_queue_start,
6865 .rx_queue_stop = hns3_dev_rx_queue_stop,
6866 .tx_queue_start = hns3_dev_tx_queue_start,
6867 .tx_queue_stop = hns3_dev_tx_queue_stop,
6868 .rx_queue_intr_enable = hns3_dev_rx_queue_intr_enable,
6869 .rx_queue_intr_disable = hns3_dev_rx_queue_intr_disable,
6870 .rxq_info_get = hns3_rxq_info_get,
6871 .txq_info_get = hns3_txq_info_get,
6872 .rx_burst_mode_get = hns3_rx_burst_mode_get,
6873 .tx_burst_mode_get = hns3_tx_burst_mode_get,
6874 .flow_ctrl_get = hns3_flow_ctrl_get,
6875 .flow_ctrl_set = hns3_flow_ctrl_set,
6876 .priority_flow_ctrl_set = hns3_priority_flow_ctrl_set,
6877 .mac_addr_add = hns3_add_mac_addr,
6878 .mac_addr_remove = hns3_remove_mac_addr,
6879 .mac_addr_set = hns3_set_default_mac_addr,
6880 .set_mc_addr_list = hns3_set_mc_mac_addr_list,
6881 .link_update = hns3_dev_link_update,
6882 .dev_set_link_up = hns3_dev_set_link_up,
6883 .dev_set_link_down = hns3_dev_set_link_down,
6884 .rss_hash_update = hns3_dev_rss_hash_update,
6885 .rss_hash_conf_get = hns3_dev_rss_hash_conf_get,
6886 .reta_update = hns3_dev_rss_reta_update,
6887 .reta_query = hns3_dev_rss_reta_query,
6888 .flow_ops_get = hns3_dev_flow_ops_get,
6889 .vlan_filter_set = hns3_vlan_filter_set,
6890 .vlan_tpid_set = hns3_vlan_tpid_set,
6891 .vlan_offload_set = hns3_vlan_offload_set,
6892 .vlan_pvid_set = hns3_vlan_pvid_set,
6893 .get_reg = hns3_get_regs,
6894 .get_module_info = hns3_get_module_info,
6895 .get_module_eeprom = hns3_get_module_eeprom,
6896 .get_dcb_info = hns3_get_dcb_info,
6897 .dev_supported_ptypes_get = hns3_dev_supported_ptypes_get,
6898 .fec_get_capability = hns3_fec_get_capability,
6899 .fec_get = hns3_fec_get,
6900 .fec_set = hns3_fec_set,
6901 .tm_ops_get = hns3_tm_ops_get,
6902 .tx_done_cleanup = hns3_tx_done_cleanup,
6903 .timesync_enable = hns3_timesync_enable,
6904 .timesync_disable = hns3_timesync_disable,
6905 .timesync_read_rx_timestamp = hns3_timesync_read_rx_timestamp,
6906 .timesync_read_tx_timestamp = hns3_timesync_read_tx_timestamp,
6907 .timesync_adjust_time = hns3_timesync_adjust_time,
6908 .timesync_read_time = hns3_timesync_read_time,
6909 .timesync_write_time = hns3_timesync_write_time,
6912 static const struct hns3_reset_ops hns3_reset_ops = {
6913 .reset_service = hns3_reset_service,
6914 .stop_service = hns3_stop_service,
6915 .prepare_reset = hns3_prepare_reset,
6916 .wait_hardware_ready = hns3_wait_hardware_ready,
6917 .reinit_dev = hns3_reinit_dev,
6918 .restore_conf = hns3_restore_conf,
6919 .start_service = hns3_start_service,
6923 hns3_init_hw_ops(struct hns3_hw *hw)
6925 hw->ops.add_mc_mac_addr = hns3_add_mc_mac_addr;
6926 hw->ops.del_mc_mac_addr = hns3_remove_mc_mac_addr;
6927 hw->ops.add_uc_mac_addr = hns3_add_uc_mac_addr;
6928 hw->ops.del_uc_mac_addr = hns3_remove_uc_mac_addr;
6932 hns3_dev_init(struct rte_eth_dev *eth_dev)
6934 struct hns3_adapter *hns = eth_dev->data->dev_private;
6935 char mac_str[RTE_ETHER_ADDR_FMT_SIZE];
6936 struct rte_ether_addr *eth_addr;
6937 struct hns3_hw *hw = &hns->hw;
6940 PMD_INIT_FUNC_TRACE();
6942 hns3_flow_init(eth_dev);
6944 hns3_set_rxtx_function(eth_dev);
6945 eth_dev->dev_ops = &hns3_eth_dev_ops;
6946 eth_dev->rx_queue_count = hns3_rx_queue_count;
6947 ret = hns3_mp_init(eth_dev);
6951 if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
6952 hns3_tx_push_init(eth_dev);
6956 hw->adapter_state = HNS3_NIC_UNINITIALIZED;
6958 hw->data = eth_dev->data;
6959 hns3_parse_devargs(eth_dev);
6962 * Set default max packet size according to the mtu
6963 * default vale in DPDK frame.
6965 hns->pf.mps = hw->data->mtu + HNS3_ETH_OVERHEAD;
6967 ret = hns3_reset_init(hw);
6969 goto err_init_reset;
6970 hw->reset.ops = &hns3_reset_ops;
6972 hns3_init_hw_ops(hw);
6973 ret = hns3_init_pf(eth_dev);
6975 PMD_INIT_LOG(ERR, "Failed to init pf: %d", ret);
6979 /* Allocate memory for storing MAC addresses */
6980 eth_dev->data->mac_addrs = rte_zmalloc("hns3-mac",
6981 sizeof(struct rte_ether_addr) *
6982 HNS3_UC_MACADDR_NUM, 0);
6983 if (eth_dev->data->mac_addrs == NULL) {
6984 PMD_INIT_LOG(ERR, "Failed to allocate %zx bytes needed "
6985 "to store MAC addresses",
6986 sizeof(struct rte_ether_addr) *
6987 HNS3_UC_MACADDR_NUM);
6989 goto err_rte_zmalloc;
6992 eth_addr = (struct rte_ether_addr *)hw->mac.mac_addr;
6993 if (!rte_is_valid_assigned_ether_addr(eth_addr)) {
6994 rte_eth_random_addr(hw->mac.mac_addr);
6995 hns3_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
6996 (struct rte_ether_addr *)hw->mac.mac_addr);
6997 hns3_warn(hw, "default mac_addr from firmware is an invalid "
6998 "unicast address, using random MAC address %s",
7001 rte_ether_addr_copy((struct rte_ether_addr *)hw->mac.mac_addr,
7002 ð_dev->data->mac_addrs[0]);
7004 hw->adapter_state = HNS3_NIC_INITIALIZED;
7006 if (__atomic_load_n(&hw->reset.schedule, __ATOMIC_RELAXED) ==
7008 hns3_err(hw, "Reschedule reset service after dev_init");
7009 hns3_schedule_reset(hns);
7011 /* IMP will wait ready flag before reset */
7012 hns3_notify_reset_ready(hw, false);
7015 hns3_info(hw, "hns3 dev initialization successful!");
7019 hns3_uninit_pf(eth_dev);
7022 rte_free(hw->reset.wait_data);
7025 hns3_mp_uninit(eth_dev);
7028 eth_dev->dev_ops = NULL;
7029 eth_dev->rx_pkt_burst = NULL;
7030 eth_dev->rx_descriptor_status = NULL;
7031 eth_dev->tx_pkt_burst = NULL;
7032 eth_dev->tx_pkt_prepare = NULL;
7033 eth_dev->tx_descriptor_status = NULL;
7038 hns3_dev_uninit(struct rte_eth_dev *eth_dev)
7040 struct hns3_adapter *hns = eth_dev->data->dev_private;
7041 struct hns3_hw *hw = &hns->hw;
7043 PMD_INIT_FUNC_TRACE();
7045 if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
7046 hns3_mp_uninit(eth_dev);
7050 if (hw->adapter_state < HNS3_NIC_CLOSING)
7051 hns3_dev_close(eth_dev);
7053 hw->adapter_state = HNS3_NIC_REMOVED;
7058 eth_hns3_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
7059 struct rte_pci_device *pci_dev)
7061 return rte_eth_dev_pci_generic_probe(pci_dev,
7062 sizeof(struct hns3_adapter),
7067 eth_hns3_pci_remove(struct rte_pci_device *pci_dev)
7069 return rte_eth_dev_pci_generic_remove(pci_dev, hns3_dev_uninit);
7072 static const struct rte_pci_id pci_id_hns3_map[] = {
7073 { RTE_PCI_DEVICE(PCI_VENDOR_ID_HUAWEI, HNS3_DEV_ID_GE) },
7074 { RTE_PCI_DEVICE(PCI_VENDOR_ID_HUAWEI, HNS3_DEV_ID_25GE) },
7075 { RTE_PCI_DEVICE(PCI_VENDOR_ID_HUAWEI, HNS3_DEV_ID_25GE_RDMA) },
7076 { RTE_PCI_DEVICE(PCI_VENDOR_ID_HUAWEI, HNS3_DEV_ID_50GE_RDMA) },
7077 { RTE_PCI_DEVICE(PCI_VENDOR_ID_HUAWEI, HNS3_DEV_ID_100G_RDMA_MACSEC) },
7078 { RTE_PCI_DEVICE(PCI_VENDOR_ID_HUAWEI, HNS3_DEV_ID_200G_RDMA) },
7079 { .vendor_id = 0, }, /* sentinel */
7082 static struct rte_pci_driver rte_hns3_pmd = {
7083 .id_table = pci_id_hns3_map,
7084 .drv_flags = RTE_PCI_DRV_NEED_MAPPING | RTE_PCI_DRV_INTR_LSC,
7085 .probe = eth_hns3_pci_probe,
7086 .remove = eth_hns3_pci_remove,
7089 RTE_PMD_REGISTER_PCI(net_hns3, rte_hns3_pmd);
7090 RTE_PMD_REGISTER_PCI_TABLE(net_hns3, pci_id_hns3_map);
7091 RTE_PMD_REGISTER_KMOD_DEP(net_hns3, "* igb_uio | vfio-pci");
7092 RTE_PMD_REGISTER_PARAM_STRING(net_hns3,
7093 HNS3_DEVARG_RX_FUNC_HINT "=vec|sve|simple|common "
7094 HNS3_DEVARG_TX_FUNC_HINT "=vec|sve|simple|common "
7095 HNS3_DEVARG_DEV_CAPS_MASK "=<1-65535> "
7096 HNS3_DEVARG_MBX_TIME_LIMIT_MS "=<uint16> ");
7097 RTE_LOG_REGISTER_SUFFIX(hns3_logtype_init, init, NOTICE);
7098 RTE_LOG_REGISTER_SUFFIX(hns3_logtype_driver, driver, NOTICE);