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"
18 #include "hns3_flow.h"
20 #define HNS3_SERVICE_INTERVAL 1000000 /* us */
21 #define HNS3_SERVICE_QUICK_INTERVAL 10
22 #define HNS3_INVALID_PVID 0xFFFF
24 #define HNS3_FILTER_TYPE_VF 0
25 #define HNS3_FILTER_TYPE_PORT 1
26 #define HNS3_FILTER_FE_EGRESS_V1_B BIT(0)
27 #define HNS3_FILTER_FE_NIC_INGRESS_B BIT(0)
28 #define HNS3_FILTER_FE_NIC_EGRESS_B BIT(1)
29 #define HNS3_FILTER_FE_ROCE_INGRESS_B BIT(2)
30 #define HNS3_FILTER_FE_ROCE_EGRESS_B BIT(3)
31 #define HNS3_FILTER_FE_EGRESS (HNS3_FILTER_FE_NIC_EGRESS_B \
32 | HNS3_FILTER_FE_ROCE_EGRESS_B)
33 #define HNS3_FILTER_FE_INGRESS (HNS3_FILTER_FE_NIC_INGRESS_B \
34 | HNS3_FILTER_FE_ROCE_INGRESS_B)
36 /* Reset related Registers */
37 #define HNS3_GLOBAL_RESET_BIT 0
38 #define HNS3_CORE_RESET_BIT 1
39 #define HNS3_IMP_RESET_BIT 2
40 #define HNS3_FUN_RST_ING_B 0
42 #define HNS3_VECTOR0_IMP_RESET_INT_B 1
43 #define HNS3_VECTOR0_IMP_CMDQ_ERR_B 4U
44 #define HNS3_VECTOR0_IMP_RD_POISON_B 5U
45 #define HNS3_VECTOR0_ALL_MSIX_ERR_B 6U
47 #define HNS3_RESET_WAIT_MS 100
48 #define HNS3_RESET_WAIT_CNT 200
50 /* FEC mode order defined in HNS3 hardware */
51 #define HNS3_HW_FEC_MODE_NOFEC 0
52 #define HNS3_HW_FEC_MODE_BASER 1
53 #define HNS3_HW_FEC_MODE_RS 2
56 HNS3_VECTOR0_EVENT_RST,
57 HNS3_VECTOR0_EVENT_MBX,
58 HNS3_VECTOR0_EVENT_ERR,
59 HNS3_VECTOR0_EVENT_PTP,
60 HNS3_VECTOR0_EVENT_OTHER,
63 static const struct rte_eth_fec_capa speed_fec_capa_tbl[] = {
64 { RTE_ETH_SPEED_NUM_10G, RTE_ETH_FEC_MODE_CAPA_MASK(NOFEC) |
65 RTE_ETH_FEC_MODE_CAPA_MASK(AUTO) |
66 RTE_ETH_FEC_MODE_CAPA_MASK(BASER) },
68 { RTE_ETH_SPEED_NUM_25G, RTE_ETH_FEC_MODE_CAPA_MASK(NOFEC) |
69 RTE_ETH_FEC_MODE_CAPA_MASK(AUTO) |
70 RTE_ETH_FEC_MODE_CAPA_MASK(BASER) |
71 RTE_ETH_FEC_MODE_CAPA_MASK(RS) },
73 { RTE_ETH_SPEED_NUM_40G, RTE_ETH_FEC_MODE_CAPA_MASK(NOFEC) |
74 RTE_ETH_FEC_MODE_CAPA_MASK(AUTO) |
75 RTE_ETH_FEC_MODE_CAPA_MASK(BASER) },
77 { RTE_ETH_SPEED_NUM_50G, RTE_ETH_FEC_MODE_CAPA_MASK(NOFEC) |
78 RTE_ETH_FEC_MODE_CAPA_MASK(AUTO) |
79 RTE_ETH_FEC_MODE_CAPA_MASK(BASER) |
80 RTE_ETH_FEC_MODE_CAPA_MASK(RS) },
82 { RTE_ETH_SPEED_NUM_100G, RTE_ETH_FEC_MODE_CAPA_MASK(NOFEC) |
83 RTE_ETH_FEC_MODE_CAPA_MASK(AUTO) |
84 RTE_ETH_FEC_MODE_CAPA_MASK(RS) },
86 { RTE_ETH_SPEED_NUM_200G, RTE_ETH_FEC_MODE_CAPA_MASK(NOFEC) |
87 RTE_ETH_FEC_MODE_CAPA_MASK(AUTO) |
88 RTE_ETH_FEC_MODE_CAPA_MASK(RS) }
91 static enum hns3_reset_level hns3_get_reset_level(struct hns3_adapter *hns,
93 static int hns3_dev_mtu_set(struct rte_eth_dev *dev, uint16_t mtu);
94 static int hns3_vlan_pvid_configure(struct hns3_adapter *hns, uint16_t pvid,
96 static int hns3_update_link_info(struct rte_eth_dev *eth_dev);
97 static bool hns3_update_link_status(struct hns3_hw *hw);
99 static int hns3_add_mc_mac_addr(struct hns3_hw *hw,
100 struct rte_ether_addr *mac_addr);
101 static int hns3_remove_mc_mac_addr(struct hns3_hw *hw,
102 struct rte_ether_addr *mac_addr);
103 static int hns3_restore_fec(struct hns3_hw *hw);
104 static int hns3_query_dev_fec_info(struct hns3_hw *hw);
105 static int hns3_do_stop(struct hns3_adapter *hns);
106 static int hns3_check_port_speed(struct hns3_hw *hw, uint32_t link_speeds);
107 static int hns3_cfg_mac_mode(struct hns3_hw *hw, bool enable);
111 hns3_pf_disable_irq0(struct hns3_hw *hw)
113 hns3_write_dev(hw, HNS3_MISC_VECTOR_REG_BASE, 0);
117 hns3_pf_enable_irq0(struct hns3_hw *hw)
119 hns3_write_dev(hw, HNS3_MISC_VECTOR_REG_BASE, 1);
122 static enum hns3_evt_cause
123 hns3_proc_imp_reset_event(struct hns3_adapter *hns, bool is_delay,
126 struct hns3_hw *hw = &hns->hw;
128 __atomic_store_n(&hw->reset.disable_cmd, 1, __ATOMIC_RELAXED);
129 hns3_atomic_set_bit(HNS3_IMP_RESET, &hw->reset.pending);
130 *vec_val = BIT(HNS3_VECTOR0_IMPRESET_INT_B);
132 hw->reset.stats.imp_cnt++;
133 hns3_warn(hw, "IMP reset detected, clear reset status");
135 hns3_schedule_delayed_reset(hns);
136 hns3_warn(hw, "IMP reset detected, don't clear reset status");
139 return HNS3_VECTOR0_EVENT_RST;
142 static enum hns3_evt_cause
143 hns3_proc_global_reset_event(struct hns3_adapter *hns, bool is_delay,
146 struct hns3_hw *hw = &hns->hw;
148 __atomic_store_n(&hw->reset.disable_cmd, 1, __ATOMIC_RELAXED);
149 hns3_atomic_set_bit(HNS3_GLOBAL_RESET, &hw->reset.pending);
150 *vec_val = BIT(HNS3_VECTOR0_GLOBALRESET_INT_B);
152 hw->reset.stats.global_cnt++;
153 hns3_warn(hw, "Global reset detected, clear reset status");
155 hns3_schedule_delayed_reset(hns);
157 "Global reset detected, don't clear reset status");
160 return HNS3_VECTOR0_EVENT_RST;
163 static enum hns3_evt_cause
164 hns3_check_event_cause(struct hns3_adapter *hns, uint32_t *clearval)
166 struct hns3_hw *hw = &hns->hw;
167 uint32_t vector0_int_stats;
168 uint32_t cmdq_src_val;
169 uint32_t hw_err_src_reg;
171 enum hns3_evt_cause ret;
174 /* fetch the events from their corresponding regs */
175 vector0_int_stats = hns3_read_dev(hw, HNS3_VECTOR0_OTHER_INT_STS_REG);
176 cmdq_src_val = hns3_read_dev(hw, HNS3_VECTOR0_CMDQ_SRC_REG);
177 hw_err_src_reg = hns3_read_dev(hw, HNS3_RAS_PF_OTHER_INT_STS_REG);
179 is_delay = clearval == NULL ? true : false;
181 * Assumption: If by any chance reset and mailbox events are reported
182 * together then we will only process reset event and defer the
183 * processing of the mailbox events. Since, we would have not cleared
184 * RX CMDQ event this time we would receive again another interrupt
185 * from H/W just for the mailbox.
187 if (BIT(HNS3_VECTOR0_IMPRESET_INT_B) & vector0_int_stats) { /* IMP */
188 ret = hns3_proc_imp_reset_event(hns, is_delay, &val);
193 if (BIT(HNS3_VECTOR0_GLOBALRESET_INT_B) & vector0_int_stats) {
194 ret = hns3_proc_global_reset_event(hns, is_delay, &val);
198 /* Check for vector0 1588 event source */
199 if (BIT(HNS3_VECTOR0_1588_INT_B) & vector0_int_stats) {
200 val = BIT(HNS3_VECTOR0_1588_INT_B);
201 ret = HNS3_VECTOR0_EVENT_PTP;
205 /* check for vector0 msix event source */
206 if (vector0_int_stats & HNS3_VECTOR0_REG_MSIX_MASK ||
207 hw_err_src_reg & HNS3_RAS_REG_NFE_MASK) {
208 val = vector0_int_stats | hw_err_src_reg;
209 ret = HNS3_VECTOR0_EVENT_ERR;
213 /* check for vector0 mailbox(=CMDQ RX) event source */
214 if (BIT(HNS3_VECTOR0_RX_CMDQ_INT_B) & cmdq_src_val) {
215 cmdq_src_val &= ~BIT(HNS3_VECTOR0_RX_CMDQ_INT_B);
217 ret = HNS3_VECTOR0_EVENT_MBX;
221 val = vector0_int_stats;
222 ret = HNS3_VECTOR0_EVENT_OTHER;
231 hns3_is_1588_event_type(uint32_t event_type)
233 return (event_type == HNS3_VECTOR0_EVENT_PTP);
237 hns3_clear_event_cause(struct hns3_hw *hw, uint32_t event_type, uint32_t regclr)
239 if (event_type == HNS3_VECTOR0_EVENT_RST ||
240 hns3_is_1588_event_type(event_type))
241 hns3_write_dev(hw, HNS3_MISC_RESET_STS_REG, regclr);
242 else if (event_type == HNS3_VECTOR0_EVENT_MBX)
243 hns3_write_dev(hw, HNS3_VECTOR0_CMDQ_SRC_REG, regclr);
247 hns3_clear_all_event_cause(struct hns3_hw *hw)
249 uint32_t vector0_int_stats;
251 vector0_int_stats = hns3_read_dev(hw, HNS3_VECTOR0_OTHER_INT_STS_REG);
252 if (BIT(HNS3_VECTOR0_IMPRESET_INT_B) & vector0_int_stats)
253 hns3_warn(hw, "Probe during IMP reset interrupt");
255 if (BIT(HNS3_VECTOR0_GLOBALRESET_INT_B) & vector0_int_stats)
256 hns3_warn(hw, "Probe during Global reset interrupt");
258 hns3_clear_event_cause(hw, HNS3_VECTOR0_EVENT_RST,
259 BIT(HNS3_VECTOR0_IMPRESET_INT_B) |
260 BIT(HNS3_VECTOR0_GLOBALRESET_INT_B) |
261 BIT(HNS3_VECTOR0_CORERESET_INT_B));
262 hns3_clear_event_cause(hw, HNS3_VECTOR0_EVENT_MBX, 0);
263 hns3_clear_event_cause(hw, HNS3_VECTOR0_EVENT_PTP,
264 BIT(HNS3_VECTOR0_1588_INT_B));
268 hns3_handle_mac_tnl(struct hns3_hw *hw)
270 struct hns3_cmd_desc desc;
274 /* query and clear mac tnl interrupt */
275 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_QUERY_MAC_TNL_INT, true);
276 ret = hns3_cmd_send(hw, &desc, 1);
278 hns3_err(hw, "failed to query mac tnl int, ret = %d.", ret);
282 status = rte_le_to_cpu_32(desc.data[0]);
284 hns3_warn(hw, "mac tnl int occurs, status = 0x%x.", status);
285 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_CLEAR_MAC_TNL_INT,
287 desc.data[0] = rte_cpu_to_le_32(HNS3_MAC_TNL_INT_CLR);
288 ret = hns3_cmd_send(hw, &desc, 1);
290 hns3_err(hw, "failed to clear mac tnl int, ret = %d.",
296 hns3_interrupt_handler(void *param)
298 struct rte_eth_dev *dev = (struct rte_eth_dev *)param;
299 struct hns3_adapter *hns = dev->data->dev_private;
300 struct hns3_hw *hw = &hns->hw;
301 enum hns3_evt_cause event_cause;
302 uint32_t clearval = 0;
303 uint32_t vector0_int;
307 /* Disable interrupt */
308 hns3_pf_disable_irq0(hw);
310 event_cause = hns3_check_event_cause(hns, &clearval);
311 vector0_int = hns3_read_dev(hw, HNS3_VECTOR0_OTHER_INT_STS_REG);
312 ras_int = hns3_read_dev(hw, HNS3_RAS_PF_OTHER_INT_STS_REG);
313 cmdq_int = hns3_read_dev(hw, HNS3_VECTOR0_CMDQ_SRC_REG);
314 hns3_clear_event_cause(hw, event_cause, clearval);
315 /* vector 0 interrupt is shared with reset and mailbox source events. */
316 if (event_cause == HNS3_VECTOR0_EVENT_ERR) {
317 hns3_warn(hw, "received interrupt: vector0_int_stat:0x%x "
318 "ras_int_stat:0x%x cmdq_int_stat:0x%x",
319 vector0_int, ras_int, cmdq_int);
320 hns3_handle_mac_tnl(hw);
321 hns3_handle_error(hns);
322 } else if (event_cause == HNS3_VECTOR0_EVENT_RST) {
323 hns3_warn(hw, "received reset interrupt");
324 hns3_schedule_reset(hns);
325 } else if (event_cause == HNS3_VECTOR0_EVENT_MBX) {
326 hns3_dev_handle_mbx_msg(hw);
328 hns3_warn(hw, "received unknown event: vector0_int_stat:0x%x "
329 "ras_int_stat:0x%x cmdq_int_stat:0x%x",
330 vector0_int, ras_int, cmdq_int);
333 /* Enable interrupt if it is not cause by reset */
334 hns3_pf_enable_irq0(hw);
338 hns3_set_port_vlan_filter(struct hns3_adapter *hns, uint16_t vlan_id, int on)
340 #define HNS3_VLAN_ID_OFFSET_STEP 160
341 #define HNS3_VLAN_BYTE_SIZE 8
342 struct hns3_vlan_filter_pf_cfg_cmd *req;
343 struct hns3_hw *hw = &hns->hw;
344 uint8_t vlan_offset_byte_val;
345 struct hns3_cmd_desc desc;
346 uint8_t vlan_offset_byte;
347 uint8_t vlan_offset_base;
350 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_VLAN_FILTER_PF_CFG, false);
352 vlan_offset_base = vlan_id / HNS3_VLAN_ID_OFFSET_STEP;
353 vlan_offset_byte = (vlan_id % HNS3_VLAN_ID_OFFSET_STEP) /
355 vlan_offset_byte_val = 1 << (vlan_id % HNS3_VLAN_BYTE_SIZE);
357 req = (struct hns3_vlan_filter_pf_cfg_cmd *)desc.data;
358 req->vlan_offset = vlan_offset_base;
359 req->vlan_cfg = on ? 0 : 1;
360 req->vlan_offset_bitmap[vlan_offset_byte] = vlan_offset_byte_val;
362 ret = hns3_cmd_send(hw, &desc, 1);
364 hns3_err(hw, "set port vlan id failed, vlan_id =%u, ret =%d",
371 hns3_rm_dev_vlan_table(struct hns3_adapter *hns, uint16_t vlan_id)
373 struct hns3_user_vlan_table *vlan_entry;
374 struct hns3_pf *pf = &hns->pf;
376 LIST_FOREACH(vlan_entry, &pf->vlan_list, next) {
377 if (vlan_entry->vlan_id == vlan_id) {
378 if (vlan_entry->hd_tbl_status)
379 hns3_set_port_vlan_filter(hns, vlan_id, 0);
380 LIST_REMOVE(vlan_entry, next);
381 rte_free(vlan_entry);
388 hns3_add_dev_vlan_table(struct hns3_adapter *hns, uint16_t vlan_id,
391 struct hns3_user_vlan_table *vlan_entry;
392 struct hns3_hw *hw = &hns->hw;
393 struct hns3_pf *pf = &hns->pf;
395 LIST_FOREACH(vlan_entry, &pf->vlan_list, next) {
396 if (vlan_entry->vlan_id == vlan_id)
400 vlan_entry = rte_zmalloc("hns3_vlan_tbl", sizeof(*vlan_entry), 0);
401 if (vlan_entry == NULL) {
402 hns3_err(hw, "Failed to malloc hns3 vlan table");
406 vlan_entry->hd_tbl_status = writen_to_tbl;
407 vlan_entry->vlan_id = vlan_id;
409 LIST_INSERT_HEAD(&pf->vlan_list, vlan_entry, next);
413 hns3_restore_vlan_table(struct hns3_adapter *hns)
415 struct hns3_user_vlan_table *vlan_entry;
416 struct hns3_hw *hw = &hns->hw;
417 struct hns3_pf *pf = &hns->pf;
421 if (hw->port_base_vlan_cfg.state == HNS3_PORT_BASE_VLAN_ENABLE)
422 return hns3_vlan_pvid_configure(hns,
423 hw->port_base_vlan_cfg.pvid, 1);
425 LIST_FOREACH(vlan_entry, &pf->vlan_list, next) {
426 if (vlan_entry->hd_tbl_status) {
427 vlan_id = vlan_entry->vlan_id;
428 ret = hns3_set_port_vlan_filter(hns, vlan_id, 1);
438 hns3_vlan_filter_configure(struct hns3_adapter *hns, uint16_t vlan_id, int on)
440 struct hns3_hw *hw = &hns->hw;
441 bool writen_to_tbl = false;
445 * When vlan filter is enabled, hardware regards packets without vlan
446 * as packets with vlan 0. So, to receive packets without vlan, vlan id
447 * 0 is not allowed to be removed by rte_eth_dev_vlan_filter.
449 if (on == 0 && vlan_id == 0)
453 * When port base vlan enabled, we use port base vlan as the vlan
454 * filter condition. In this case, we don't update vlan filter table
455 * when user add new vlan or remove exist vlan, just update the
456 * vlan list. The vlan id in vlan list will be written in vlan filter
457 * table until port base vlan disabled
459 if (hw->port_base_vlan_cfg.state == HNS3_PORT_BASE_VLAN_DISABLE) {
460 ret = hns3_set_port_vlan_filter(hns, vlan_id, on);
461 writen_to_tbl = true;
466 hns3_add_dev_vlan_table(hns, vlan_id, writen_to_tbl);
468 hns3_rm_dev_vlan_table(hns, vlan_id);
474 hns3_vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on)
476 struct hns3_adapter *hns = dev->data->dev_private;
477 struct hns3_hw *hw = &hns->hw;
480 rte_spinlock_lock(&hw->lock);
481 ret = hns3_vlan_filter_configure(hns, vlan_id, on);
482 rte_spinlock_unlock(&hw->lock);
487 hns3_vlan_tpid_configure(struct hns3_adapter *hns, enum rte_vlan_type vlan_type,
490 struct hns3_rx_vlan_type_cfg_cmd *rx_req;
491 struct hns3_tx_vlan_type_cfg_cmd *tx_req;
492 struct hns3_hw *hw = &hns->hw;
493 struct hns3_cmd_desc desc;
496 if ((vlan_type != RTE_ETH_VLAN_TYPE_INNER &&
497 vlan_type != RTE_ETH_VLAN_TYPE_OUTER)) {
498 hns3_err(hw, "Unsupported vlan type, vlan_type =%d", vlan_type);
502 if (tpid != RTE_ETHER_TYPE_VLAN) {
503 hns3_err(hw, "Unsupported vlan tpid, vlan_type =%d", vlan_type);
507 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_MAC_VLAN_TYPE_ID, false);
508 rx_req = (struct hns3_rx_vlan_type_cfg_cmd *)desc.data;
510 if (vlan_type == RTE_ETH_VLAN_TYPE_OUTER) {
511 rx_req->ot_fst_vlan_type = rte_cpu_to_le_16(tpid);
512 rx_req->ot_sec_vlan_type = rte_cpu_to_le_16(tpid);
513 } else if (vlan_type == RTE_ETH_VLAN_TYPE_INNER) {
514 rx_req->ot_fst_vlan_type = rte_cpu_to_le_16(tpid);
515 rx_req->ot_sec_vlan_type = rte_cpu_to_le_16(tpid);
516 rx_req->in_fst_vlan_type = rte_cpu_to_le_16(tpid);
517 rx_req->in_sec_vlan_type = rte_cpu_to_le_16(tpid);
520 ret = hns3_cmd_send(hw, &desc, 1);
522 hns3_err(hw, "Send rxvlan protocol type command fail, ret =%d",
527 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_MAC_VLAN_INSERT, false);
529 tx_req = (struct hns3_tx_vlan_type_cfg_cmd *)desc.data;
530 tx_req->ot_vlan_type = rte_cpu_to_le_16(tpid);
531 tx_req->in_vlan_type = rte_cpu_to_le_16(tpid);
533 ret = hns3_cmd_send(hw, &desc, 1);
535 hns3_err(hw, "Send txvlan protocol type command fail, ret =%d",
541 hns3_vlan_tpid_set(struct rte_eth_dev *dev, enum rte_vlan_type vlan_type,
544 struct hns3_adapter *hns = dev->data->dev_private;
545 struct hns3_hw *hw = &hns->hw;
548 rte_spinlock_lock(&hw->lock);
549 ret = hns3_vlan_tpid_configure(hns, vlan_type, tpid);
550 rte_spinlock_unlock(&hw->lock);
555 hns3_set_vlan_rx_offload_cfg(struct hns3_adapter *hns,
556 struct hns3_rx_vtag_cfg *vcfg)
558 struct hns3_vport_vtag_rx_cfg_cmd *req;
559 struct hns3_hw *hw = &hns->hw;
560 struct hns3_cmd_desc desc;
565 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_VLAN_PORT_RX_CFG, false);
567 req = (struct hns3_vport_vtag_rx_cfg_cmd *)desc.data;
568 hns3_set_bit(req->vport_vlan_cfg, HNS3_REM_TAG1_EN_B,
569 vcfg->strip_tag1_en ? 1 : 0);
570 hns3_set_bit(req->vport_vlan_cfg, HNS3_REM_TAG2_EN_B,
571 vcfg->strip_tag2_en ? 1 : 0);
572 hns3_set_bit(req->vport_vlan_cfg, HNS3_SHOW_TAG1_EN_B,
573 vcfg->vlan1_vlan_prionly ? 1 : 0);
574 hns3_set_bit(req->vport_vlan_cfg, HNS3_SHOW_TAG2_EN_B,
575 vcfg->vlan2_vlan_prionly ? 1 : 0);
577 /* firmwall will ignore this configuration for PCI_REVISION_ID_HIP08 */
578 hns3_set_bit(req->vport_vlan_cfg, HNS3_DISCARD_TAG1_EN_B,
579 vcfg->strip_tag1_discard_en ? 1 : 0);
580 hns3_set_bit(req->vport_vlan_cfg, HNS3_DISCARD_TAG2_EN_B,
581 vcfg->strip_tag2_discard_en ? 1 : 0);
583 * In current version VF is not supported when PF is driven by DPDK
584 * driver, just need to configure parameters for PF vport.
586 vport_id = HNS3_PF_FUNC_ID;
587 req->vf_offset = vport_id / HNS3_VF_NUM_PER_CMD;
588 bitmap = 1 << (vport_id % HNS3_VF_NUM_PER_BYTE);
589 req->vf_bitmap[req->vf_offset] = bitmap;
591 ret = hns3_cmd_send(hw, &desc, 1);
593 hns3_err(hw, "Send port rxvlan cfg command fail, ret =%d", ret);
598 hns3_update_rx_offload_cfg(struct hns3_adapter *hns,
599 struct hns3_rx_vtag_cfg *vcfg)
601 struct hns3_pf *pf = &hns->pf;
602 memcpy(&pf->vtag_config.rx_vcfg, vcfg, sizeof(pf->vtag_config.rx_vcfg));
606 hns3_update_tx_offload_cfg(struct hns3_adapter *hns,
607 struct hns3_tx_vtag_cfg *vcfg)
609 struct hns3_pf *pf = &hns->pf;
610 memcpy(&pf->vtag_config.tx_vcfg, vcfg, sizeof(pf->vtag_config.tx_vcfg));
614 hns3_en_hw_strip_rxvtag(struct hns3_adapter *hns, bool enable)
616 struct hns3_rx_vtag_cfg rxvlan_cfg;
617 struct hns3_hw *hw = &hns->hw;
620 if (hw->port_base_vlan_cfg.state == HNS3_PORT_BASE_VLAN_DISABLE) {
621 rxvlan_cfg.strip_tag1_en = false;
622 rxvlan_cfg.strip_tag2_en = enable;
623 rxvlan_cfg.strip_tag2_discard_en = false;
625 rxvlan_cfg.strip_tag1_en = enable;
626 rxvlan_cfg.strip_tag2_en = true;
627 rxvlan_cfg.strip_tag2_discard_en = true;
630 rxvlan_cfg.strip_tag1_discard_en = false;
631 rxvlan_cfg.vlan1_vlan_prionly = false;
632 rxvlan_cfg.vlan2_vlan_prionly = false;
633 rxvlan_cfg.rx_vlan_offload_en = enable;
635 ret = hns3_set_vlan_rx_offload_cfg(hns, &rxvlan_cfg);
637 hns3_err(hw, "%s strip rx vtag failed, ret = %d.",
638 enable ? "enable" : "disable", ret);
642 hns3_update_rx_offload_cfg(hns, &rxvlan_cfg);
648 hns3_set_vlan_filter_ctrl(struct hns3_hw *hw, uint8_t vlan_type,
649 uint8_t fe_type, bool filter_en, uint8_t vf_id)
651 struct hns3_vlan_filter_ctrl_cmd *req;
652 struct hns3_cmd_desc desc;
655 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_VLAN_FILTER_CTRL, false);
657 req = (struct hns3_vlan_filter_ctrl_cmd *)desc.data;
658 req->vlan_type = vlan_type;
659 req->vlan_fe = filter_en ? fe_type : 0;
662 ret = hns3_cmd_send(hw, &desc, 1);
664 hns3_err(hw, "set vlan filter fail, ret =%d", ret);
670 hns3_vlan_filter_init(struct hns3_adapter *hns)
672 struct hns3_hw *hw = &hns->hw;
675 ret = hns3_set_vlan_filter_ctrl(hw, HNS3_FILTER_TYPE_VF,
676 HNS3_FILTER_FE_EGRESS, false,
679 hns3_err(hw, "failed to init vf vlan filter, ret = %d", ret);
683 ret = hns3_set_vlan_filter_ctrl(hw, HNS3_FILTER_TYPE_PORT,
684 HNS3_FILTER_FE_INGRESS, false,
687 hns3_err(hw, "failed to init port vlan filter, ret = %d", ret);
693 hns3_enable_vlan_filter(struct hns3_adapter *hns, bool enable)
695 struct hns3_hw *hw = &hns->hw;
698 ret = hns3_set_vlan_filter_ctrl(hw, HNS3_FILTER_TYPE_PORT,
699 HNS3_FILTER_FE_INGRESS, enable,
702 hns3_err(hw, "failed to %s port vlan filter, ret = %d",
703 enable ? "enable" : "disable", ret);
709 hns3_vlan_offload_set(struct rte_eth_dev *dev, int mask)
711 struct hns3_adapter *hns = dev->data->dev_private;
712 struct hns3_hw *hw = &hns->hw;
713 struct rte_eth_rxmode *rxmode;
714 unsigned int tmp_mask;
718 rte_spinlock_lock(&hw->lock);
719 rxmode = &dev->data->dev_conf.rxmode;
720 tmp_mask = (unsigned int)mask;
721 if (tmp_mask & RTE_ETH_VLAN_FILTER_MASK) {
722 /* ignore vlan filter configuration during promiscuous mode */
723 if (!dev->data->promiscuous) {
724 /* Enable or disable VLAN filter */
725 enable = rxmode->offloads & RTE_ETH_RX_OFFLOAD_VLAN_FILTER ?
728 ret = hns3_enable_vlan_filter(hns, enable);
730 rte_spinlock_unlock(&hw->lock);
731 hns3_err(hw, "failed to %s rx filter, ret = %d",
732 enable ? "enable" : "disable", ret);
738 if (tmp_mask & RTE_ETH_VLAN_STRIP_MASK) {
739 /* Enable or disable VLAN stripping */
740 enable = rxmode->offloads & RTE_ETH_RX_OFFLOAD_VLAN_STRIP ?
743 ret = hns3_en_hw_strip_rxvtag(hns, enable);
745 rte_spinlock_unlock(&hw->lock);
746 hns3_err(hw, "failed to %s rx strip, ret = %d",
747 enable ? "enable" : "disable", ret);
752 rte_spinlock_unlock(&hw->lock);
758 hns3_set_vlan_tx_offload_cfg(struct hns3_adapter *hns,
759 struct hns3_tx_vtag_cfg *vcfg)
761 struct hns3_vport_vtag_tx_cfg_cmd *req;
762 struct hns3_cmd_desc desc;
763 struct hns3_hw *hw = &hns->hw;
768 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_VLAN_PORT_TX_CFG, false);
770 req = (struct hns3_vport_vtag_tx_cfg_cmd *)desc.data;
771 req->def_vlan_tag1 = vcfg->default_tag1;
772 req->def_vlan_tag2 = vcfg->default_tag2;
773 hns3_set_bit(req->vport_vlan_cfg, HNS3_ACCEPT_TAG1_B,
774 vcfg->accept_tag1 ? 1 : 0);
775 hns3_set_bit(req->vport_vlan_cfg, HNS3_ACCEPT_UNTAG1_B,
776 vcfg->accept_untag1 ? 1 : 0);
777 hns3_set_bit(req->vport_vlan_cfg, HNS3_ACCEPT_TAG2_B,
778 vcfg->accept_tag2 ? 1 : 0);
779 hns3_set_bit(req->vport_vlan_cfg, HNS3_ACCEPT_UNTAG2_B,
780 vcfg->accept_untag2 ? 1 : 0);
781 hns3_set_bit(req->vport_vlan_cfg, HNS3_PORT_INS_TAG1_EN_B,
782 vcfg->insert_tag1_en ? 1 : 0);
783 hns3_set_bit(req->vport_vlan_cfg, HNS3_PORT_INS_TAG2_EN_B,
784 vcfg->insert_tag2_en ? 1 : 0);
785 hns3_set_bit(req->vport_vlan_cfg, HNS3_CFG_NIC_ROCE_SEL_B, 0);
787 /* firmwall will ignore this configuration for PCI_REVISION_ID_HIP08 */
788 hns3_set_bit(req->vport_vlan_cfg, HNS3_TAG_SHIFT_MODE_EN_B,
789 vcfg->tag_shift_mode_en ? 1 : 0);
792 * In current version VF is not supported when PF is driven by DPDK
793 * driver, just need to configure parameters for PF vport.
795 vport_id = HNS3_PF_FUNC_ID;
796 req->vf_offset = vport_id / HNS3_VF_NUM_PER_CMD;
797 bitmap = 1 << (vport_id % HNS3_VF_NUM_PER_BYTE);
798 req->vf_bitmap[req->vf_offset] = bitmap;
800 ret = hns3_cmd_send(hw, &desc, 1);
802 hns3_err(hw, "Send port txvlan cfg command fail, ret =%d", ret);
808 hns3_vlan_txvlan_cfg(struct hns3_adapter *hns, uint16_t port_base_vlan_state,
811 struct hns3_hw *hw = &hns->hw;
812 struct hns3_tx_vtag_cfg txvlan_cfg;
815 if (port_base_vlan_state == HNS3_PORT_BASE_VLAN_DISABLE) {
816 txvlan_cfg.accept_tag1 = true;
817 txvlan_cfg.insert_tag1_en = false;
818 txvlan_cfg.default_tag1 = 0;
820 txvlan_cfg.accept_tag1 =
821 hw->vlan_mode == HNS3_HW_SHIFT_AND_DISCARD_MODE;
822 txvlan_cfg.insert_tag1_en = true;
823 txvlan_cfg.default_tag1 = pvid;
826 txvlan_cfg.accept_untag1 = true;
827 txvlan_cfg.accept_tag2 = true;
828 txvlan_cfg.accept_untag2 = true;
829 txvlan_cfg.insert_tag2_en = false;
830 txvlan_cfg.default_tag2 = 0;
831 txvlan_cfg.tag_shift_mode_en = true;
833 ret = hns3_set_vlan_tx_offload_cfg(hns, &txvlan_cfg);
835 hns3_err(hw, "pf vlan set pvid failed, pvid =%u ,ret =%d", pvid,
840 hns3_update_tx_offload_cfg(hns, &txvlan_cfg);
846 hns3_rm_all_vlan_table(struct hns3_adapter *hns, bool is_del_list)
848 struct hns3_user_vlan_table *vlan_entry;
849 struct hns3_pf *pf = &hns->pf;
851 LIST_FOREACH(vlan_entry, &pf->vlan_list, next) {
852 if (vlan_entry->hd_tbl_status) {
853 hns3_set_port_vlan_filter(hns, vlan_entry->vlan_id, 0);
854 vlan_entry->hd_tbl_status = false;
859 vlan_entry = LIST_FIRST(&pf->vlan_list);
861 LIST_REMOVE(vlan_entry, next);
862 rte_free(vlan_entry);
863 vlan_entry = LIST_FIRST(&pf->vlan_list);
869 hns3_add_all_vlan_table(struct hns3_adapter *hns)
871 struct hns3_user_vlan_table *vlan_entry;
872 struct hns3_pf *pf = &hns->pf;
874 LIST_FOREACH(vlan_entry, &pf->vlan_list, next) {
875 if (!vlan_entry->hd_tbl_status) {
876 hns3_set_port_vlan_filter(hns, vlan_entry->vlan_id, 1);
877 vlan_entry->hd_tbl_status = true;
883 hns3_remove_all_vlan_table(struct hns3_adapter *hns)
885 struct hns3_hw *hw = &hns->hw;
888 hns3_rm_all_vlan_table(hns, true);
889 if (hw->port_base_vlan_cfg.pvid != HNS3_INVALID_PVID) {
890 ret = hns3_set_port_vlan_filter(hns,
891 hw->port_base_vlan_cfg.pvid, 0);
893 hns3_err(hw, "Failed to remove all vlan table, ret =%d",
901 hns3_update_vlan_filter_entries(struct hns3_adapter *hns,
902 uint16_t port_base_vlan_state, uint16_t new_pvid)
904 struct hns3_hw *hw = &hns->hw;
908 if (port_base_vlan_state == HNS3_PORT_BASE_VLAN_ENABLE) {
909 old_pvid = hw->port_base_vlan_cfg.pvid;
910 if (old_pvid != HNS3_INVALID_PVID) {
911 ret = hns3_set_port_vlan_filter(hns, old_pvid, 0);
913 hns3_err(hw, "failed to remove old pvid %u, "
914 "ret = %d", old_pvid, ret);
919 hns3_rm_all_vlan_table(hns, false);
920 ret = hns3_set_port_vlan_filter(hns, new_pvid, 1);
922 hns3_err(hw, "failed to add new pvid %u, ret = %d",
927 ret = hns3_set_port_vlan_filter(hns, new_pvid, 0);
929 hns3_err(hw, "failed to remove pvid %u, ret = %d",
934 hns3_add_all_vlan_table(hns);
940 hns3_en_pvid_strip(struct hns3_adapter *hns, int on)
942 struct hns3_rx_vtag_cfg *old_cfg = &hns->pf.vtag_config.rx_vcfg;
943 struct hns3_rx_vtag_cfg rx_vlan_cfg;
947 rx_strip_en = old_cfg->rx_vlan_offload_en;
949 rx_vlan_cfg.strip_tag1_en = rx_strip_en;
950 rx_vlan_cfg.strip_tag2_en = true;
951 rx_vlan_cfg.strip_tag2_discard_en = true;
953 rx_vlan_cfg.strip_tag1_en = false;
954 rx_vlan_cfg.strip_tag2_en = rx_strip_en;
955 rx_vlan_cfg.strip_tag2_discard_en = false;
957 rx_vlan_cfg.strip_tag1_discard_en = false;
958 rx_vlan_cfg.vlan1_vlan_prionly = false;
959 rx_vlan_cfg.vlan2_vlan_prionly = false;
960 rx_vlan_cfg.rx_vlan_offload_en = old_cfg->rx_vlan_offload_en;
962 ret = hns3_set_vlan_rx_offload_cfg(hns, &rx_vlan_cfg);
966 hns3_update_rx_offload_cfg(hns, &rx_vlan_cfg);
971 hns3_vlan_pvid_configure(struct hns3_adapter *hns, uint16_t pvid, int on)
973 struct hns3_hw *hw = &hns->hw;
974 uint16_t port_base_vlan_state;
977 if (on == 0 && pvid != hw->port_base_vlan_cfg.pvid) {
978 if (hw->port_base_vlan_cfg.pvid != HNS3_INVALID_PVID)
979 hns3_warn(hw, "Invalid operation! As current pvid set "
980 "is %u, disable pvid %u is invalid",
981 hw->port_base_vlan_cfg.pvid, pvid);
985 port_base_vlan_state = on ? HNS3_PORT_BASE_VLAN_ENABLE :
986 HNS3_PORT_BASE_VLAN_DISABLE;
987 ret = hns3_vlan_txvlan_cfg(hns, port_base_vlan_state, pvid);
989 hns3_err(hw, "failed to config tx vlan for pvid, ret = %d",
994 ret = hns3_en_pvid_strip(hns, on);
996 hns3_err(hw, "failed to config rx vlan strip for pvid, "
998 goto pvid_vlan_strip_fail;
1001 if (pvid == HNS3_INVALID_PVID)
1003 ret = hns3_update_vlan_filter_entries(hns, port_base_vlan_state, pvid);
1005 hns3_err(hw, "failed to update vlan filter entries, ret = %d",
1007 goto vlan_filter_set_fail;
1011 hw->port_base_vlan_cfg.state = port_base_vlan_state;
1012 hw->port_base_vlan_cfg.pvid = on ? pvid : HNS3_INVALID_PVID;
1015 vlan_filter_set_fail:
1016 err = hns3_en_pvid_strip(hns, hw->port_base_vlan_cfg.state ==
1017 HNS3_PORT_BASE_VLAN_ENABLE);
1019 hns3_err(hw, "fail to rollback pvid strip, ret = %d", err);
1021 pvid_vlan_strip_fail:
1022 err = hns3_vlan_txvlan_cfg(hns, hw->port_base_vlan_cfg.state,
1023 hw->port_base_vlan_cfg.pvid);
1025 hns3_err(hw, "fail to rollback txvlan status, ret = %d", err);
1031 hns3_vlan_pvid_set(struct rte_eth_dev *dev, uint16_t pvid, int on)
1033 struct hns3_adapter *hns = dev->data->dev_private;
1034 struct hns3_hw *hw = &hns->hw;
1035 bool pvid_en_state_change;
1036 uint16_t pvid_state;
1039 if (pvid > RTE_ETHER_MAX_VLAN_ID) {
1040 hns3_err(hw, "Invalid vlan_id = %u > %d", pvid,
1041 RTE_ETHER_MAX_VLAN_ID);
1046 * If PVID configuration state change, should refresh the PVID
1047 * configuration state in struct hns3_tx_queue/hns3_rx_queue.
1049 pvid_state = hw->port_base_vlan_cfg.state;
1050 if ((on && pvid_state == HNS3_PORT_BASE_VLAN_ENABLE) ||
1051 (!on && pvid_state == HNS3_PORT_BASE_VLAN_DISABLE))
1052 pvid_en_state_change = false;
1054 pvid_en_state_change = true;
1056 rte_spinlock_lock(&hw->lock);
1057 ret = hns3_vlan_pvid_configure(hns, pvid, on);
1058 rte_spinlock_unlock(&hw->lock);
1062 * Only in HNS3_SW_SHIFT_AND_MODE the PVID related operation in Tx/Rx
1063 * need be processed by PMD.
1065 if (pvid_en_state_change &&
1066 hw->vlan_mode == HNS3_SW_SHIFT_AND_DISCARD_MODE)
1067 hns3_update_all_queues_pvid_proc_en(hw);
1073 hns3_default_vlan_config(struct hns3_adapter *hns)
1075 struct hns3_hw *hw = &hns->hw;
1079 * When vlan filter is enabled, hardware regards packets without vlan
1080 * as packets with vlan 0. Therefore, if vlan 0 is not in the vlan
1081 * table, packets without vlan won't be received. So, add vlan 0 as
1084 ret = hns3_vlan_filter_configure(hns, 0, 1);
1086 hns3_err(hw, "default vlan 0 config failed, ret =%d", ret);
1091 hns3_init_vlan_config(struct hns3_adapter *hns)
1093 struct hns3_hw *hw = &hns->hw;
1097 * This function can be called in the initialization and reset process,
1098 * when in reset process, it means that hardware had been reseted
1099 * successfully and we need to restore the hardware configuration to
1100 * ensure that the hardware configuration remains unchanged before and
1103 if (__atomic_load_n(&hw->reset.resetting, __ATOMIC_RELAXED) == 0) {
1104 hw->port_base_vlan_cfg.state = HNS3_PORT_BASE_VLAN_DISABLE;
1105 hw->port_base_vlan_cfg.pvid = HNS3_INVALID_PVID;
1108 ret = hns3_vlan_filter_init(hns);
1110 hns3_err(hw, "vlan init fail in pf, ret =%d", ret);
1114 ret = hns3_vlan_tpid_configure(hns, RTE_ETH_VLAN_TYPE_INNER,
1115 RTE_ETHER_TYPE_VLAN);
1117 hns3_err(hw, "tpid set fail in pf, ret =%d", ret);
1122 * When in the reinit dev stage of the reset process, the following
1123 * vlan-related configurations may differ from those at initialization,
1124 * we will restore configurations to hardware in hns3_restore_vlan_table
1125 * and hns3_restore_vlan_conf later.
1127 if (__atomic_load_n(&hw->reset.resetting, __ATOMIC_RELAXED) == 0) {
1128 ret = hns3_vlan_pvid_configure(hns, HNS3_INVALID_PVID, 0);
1130 hns3_err(hw, "pvid set fail in pf, ret =%d", ret);
1134 ret = hns3_en_hw_strip_rxvtag(hns, false);
1136 hns3_err(hw, "rx strip configure fail in pf, ret =%d",
1142 return hns3_default_vlan_config(hns);
1146 hns3_restore_vlan_conf(struct hns3_adapter *hns)
1148 struct hns3_pf *pf = &hns->pf;
1149 struct hns3_hw *hw = &hns->hw;
1154 if (!hw->data->promiscuous) {
1155 /* restore vlan filter states */
1156 offloads = hw->data->dev_conf.rxmode.offloads;
1157 enable = offloads & RTE_ETH_RX_OFFLOAD_VLAN_FILTER ? true : false;
1158 ret = hns3_enable_vlan_filter(hns, enable);
1160 hns3_err(hw, "failed to restore vlan rx filter conf, "
1166 ret = hns3_set_vlan_rx_offload_cfg(hns, &pf->vtag_config.rx_vcfg);
1168 hns3_err(hw, "failed to restore vlan rx conf, ret = %d", ret);
1172 ret = hns3_set_vlan_tx_offload_cfg(hns, &pf->vtag_config.tx_vcfg);
1174 hns3_err(hw, "failed to restore vlan tx conf, ret = %d", ret);
1180 hns3_dev_configure_vlan(struct rte_eth_dev *dev)
1182 struct hns3_adapter *hns = dev->data->dev_private;
1183 struct rte_eth_dev_data *data = dev->data;
1184 struct rte_eth_txmode *txmode;
1185 struct hns3_hw *hw = &hns->hw;
1189 txmode = &data->dev_conf.txmode;
1190 if (txmode->hw_vlan_reject_tagged || txmode->hw_vlan_reject_untagged)
1192 "hw_vlan_reject_tagged or hw_vlan_reject_untagged "
1193 "configuration is not supported! Ignore these two "
1194 "parameters: hw_vlan_reject_tagged(%u), "
1195 "hw_vlan_reject_untagged(%u)",
1196 txmode->hw_vlan_reject_tagged,
1197 txmode->hw_vlan_reject_untagged);
1199 /* Apply vlan offload setting */
1200 mask = RTE_ETH_VLAN_STRIP_MASK | RTE_ETH_VLAN_FILTER_MASK;
1201 ret = hns3_vlan_offload_set(dev, mask);
1203 hns3_err(hw, "dev config rx vlan offload failed, ret = %d",
1209 * If pvid config is not set in rte_eth_conf, driver needn't to set
1210 * VLAN pvid related configuration to hardware.
1212 if (txmode->pvid == 0 && txmode->hw_vlan_insert_pvid == 0)
1215 /* Apply pvid setting */
1216 ret = hns3_vlan_pvid_set(dev, txmode->pvid,
1217 txmode->hw_vlan_insert_pvid);
1219 hns3_err(hw, "dev config vlan pvid(%u) failed, ret = %d",
1226 hns3_config_tso(struct hns3_hw *hw, unsigned int tso_mss_min,
1227 unsigned int tso_mss_max)
1229 struct hns3_cfg_tso_status_cmd *req;
1230 struct hns3_cmd_desc desc;
1233 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_TSO_GENERIC_CONFIG, false);
1235 req = (struct hns3_cfg_tso_status_cmd *)desc.data;
1238 hns3_set_field(tso_mss, HNS3_TSO_MSS_MIN_M, HNS3_TSO_MSS_MIN_S,
1240 req->tso_mss_min = rte_cpu_to_le_16(tso_mss);
1243 hns3_set_field(tso_mss, HNS3_TSO_MSS_MIN_M, HNS3_TSO_MSS_MIN_S,
1245 req->tso_mss_max = rte_cpu_to_le_16(tso_mss);
1247 return hns3_cmd_send(hw, &desc, 1);
1251 hns3_set_umv_space(struct hns3_hw *hw, uint16_t space_size,
1252 uint16_t *allocated_size, bool is_alloc)
1254 struct hns3_umv_spc_alc_cmd *req;
1255 struct hns3_cmd_desc desc;
1258 req = (struct hns3_umv_spc_alc_cmd *)desc.data;
1259 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_MAC_VLAN_ALLOCATE, false);
1260 hns3_set_bit(req->allocate, HNS3_UMV_SPC_ALC_B, is_alloc ? 0 : 1);
1261 req->space_size = rte_cpu_to_le_32(space_size);
1263 ret = hns3_cmd_send(hw, &desc, 1);
1265 PMD_INIT_LOG(ERR, "%s umv space failed for cmd_send, ret =%d",
1266 is_alloc ? "allocate" : "free", ret);
1270 if (is_alloc && allocated_size)
1271 *allocated_size = rte_le_to_cpu_32(desc.data[1]);
1277 hns3_init_umv_space(struct hns3_hw *hw)
1279 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
1280 struct hns3_pf *pf = &hns->pf;
1281 uint16_t allocated_size = 0;
1284 ret = hns3_set_umv_space(hw, pf->wanted_umv_size, &allocated_size,
1289 if (allocated_size < pf->wanted_umv_size)
1290 PMD_INIT_LOG(WARNING, "Alloc umv space failed, want %u, get %u",
1291 pf->wanted_umv_size, allocated_size);
1293 pf->max_umv_size = (!!allocated_size) ? allocated_size :
1294 pf->wanted_umv_size;
1295 pf->used_umv_size = 0;
1300 hns3_uninit_umv_space(struct hns3_hw *hw)
1302 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
1303 struct hns3_pf *pf = &hns->pf;
1306 if (pf->max_umv_size == 0)
1309 ret = hns3_set_umv_space(hw, pf->max_umv_size, NULL, false);
1313 pf->max_umv_size = 0;
1319 hns3_is_umv_space_full(struct hns3_hw *hw)
1321 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
1322 struct hns3_pf *pf = &hns->pf;
1325 is_full = (pf->used_umv_size >= pf->max_umv_size);
1331 hns3_update_umv_space(struct hns3_hw *hw, bool is_free)
1333 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
1334 struct hns3_pf *pf = &hns->pf;
1337 if (pf->used_umv_size > 0)
1338 pf->used_umv_size--;
1340 pf->used_umv_size++;
1344 hns3_prepare_mac_addr(struct hns3_mac_vlan_tbl_entry_cmd *new_req,
1345 const uint8_t *addr, bool is_mc)
1347 const unsigned char *mac_addr = addr;
1348 uint32_t high_val = ((uint32_t)mac_addr[3] << 24) |
1349 ((uint32_t)mac_addr[2] << 16) |
1350 ((uint32_t)mac_addr[1] << 8) |
1351 (uint32_t)mac_addr[0];
1352 uint32_t low_val = ((uint32_t)mac_addr[5] << 8) | (uint32_t)mac_addr[4];
1354 hns3_set_bit(new_req->flags, HNS3_MAC_VLAN_BIT0_EN_B, 1);
1356 hns3_set_bit(new_req->entry_type, HNS3_MAC_VLAN_BIT0_EN_B, 0);
1357 hns3_set_bit(new_req->entry_type, HNS3_MAC_VLAN_BIT1_EN_B, 1);
1358 hns3_set_bit(new_req->mc_mac_en, HNS3_MAC_VLAN_BIT0_EN_B, 1);
1361 new_req->mac_addr_hi32 = rte_cpu_to_le_32(high_val);
1362 new_req->mac_addr_lo16 = rte_cpu_to_le_16(low_val & 0xffff);
1366 hns3_get_mac_vlan_cmd_status(struct hns3_hw *hw, uint16_t cmdq_resp,
1368 enum hns3_mac_vlan_tbl_opcode op)
1371 hns3_err(hw, "cmdq execute failed for get_mac_vlan_cmd_status,status=%u",
1376 if (op == HNS3_MAC_VLAN_ADD) {
1377 if (resp_code == 0 || resp_code == 1) {
1379 } else if (resp_code == HNS3_ADD_UC_OVERFLOW) {
1380 hns3_err(hw, "add mac addr failed for uc_overflow");
1382 } else if (resp_code == HNS3_ADD_MC_OVERFLOW) {
1383 hns3_err(hw, "add mac addr failed for mc_overflow");
1387 hns3_err(hw, "add mac addr failed for undefined, code=%u",
1390 } else if (op == HNS3_MAC_VLAN_REMOVE) {
1391 if (resp_code == 0) {
1393 } else if (resp_code == 1) {
1394 hns3_dbg(hw, "remove mac addr failed for miss");
1398 hns3_err(hw, "remove mac addr failed for undefined, code=%u",
1401 } else if (op == HNS3_MAC_VLAN_LKUP) {
1402 if (resp_code == 0) {
1404 } else if (resp_code == 1) {
1405 hns3_dbg(hw, "lookup mac addr failed for miss");
1409 hns3_err(hw, "lookup mac addr failed for undefined, code=%u",
1414 hns3_err(hw, "unknown opcode for get_mac_vlan_cmd_status, opcode=%u",
1421 hns3_lookup_mac_vlan_tbl(struct hns3_hw *hw,
1422 struct hns3_mac_vlan_tbl_entry_cmd *req,
1423 struct hns3_cmd_desc *desc, uint8_t desc_num)
1430 if (desc_num == HNS3_MC_MAC_VLAN_OPS_DESC_NUM) {
1431 for (i = 0; i < desc_num - 1; i++) {
1432 hns3_cmd_setup_basic_desc(&desc[i],
1433 HNS3_OPC_MAC_VLAN_ADD, true);
1434 desc[i].flag |= rte_cpu_to_le_16(HNS3_CMD_FLAG_NEXT);
1436 memcpy(desc[i].data, req,
1437 sizeof(struct hns3_mac_vlan_tbl_entry_cmd));
1439 hns3_cmd_setup_basic_desc(&desc[i], HNS3_OPC_MAC_VLAN_ADD,
1442 hns3_cmd_setup_basic_desc(&desc[0], HNS3_OPC_MAC_VLAN_ADD,
1444 memcpy(desc[0].data, req,
1445 sizeof(struct hns3_mac_vlan_tbl_entry_cmd));
1447 ret = hns3_cmd_send(hw, desc, desc_num);
1449 hns3_err(hw, "lookup mac addr failed for cmd_send, ret =%d.",
1453 resp_code = (rte_le_to_cpu_32(desc[0].data[0]) >> 8) & 0xff;
1454 retval = rte_le_to_cpu_16(desc[0].retval);
1456 return hns3_get_mac_vlan_cmd_status(hw, retval, resp_code,
1457 HNS3_MAC_VLAN_LKUP);
1461 hns3_add_mac_vlan_tbl(struct hns3_hw *hw,
1462 struct hns3_mac_vlan_tbl_entry_cmd *req,
1463 struct hns3_cmd_desc *desc, uint8_t desc_num)
1471 if (desc_num == HNS3_UC_MAC_VLAN_OPS_DESC_NUM) {
1472 hns3_cmd_setup_basic_desc(desc, HNS3_OPC_MAC_VLAN_ADD, false);
1473 memcpy(desc->data, req,
1474 sizeof(struct hns3_mac_vlan_tbl_entry_cmd));
1475 ret = hns3_cmd_send(hw, desc, desc_num);
1476 resp_code = (rte_le_to_cpu_32(desc->data[0]) >> 8) & 0xff;
1477 retval = rte_le_to_cpu_16(desc->retval);
1479 cfg_status = hns3_get_mac_vlan_cmd_status(hw, retval, resp_code,
1482 for (i = 0; i < desc_num; i++) {
1483 hns3_cmd_reuse_desc(&desc[i], false);
1484 if (i == desc_num - 1)
1486 rte_cpu_to_le_16(~HNS3_CMD_FLAG_NEXT);
1489 rte_cpu_to_le_16(HNS3_CMD_FLAG_NEXT);
1491 memcpy(desc[0].data, req,
1492 sizeof(struct hns3_mac_vlan_tbl_entry_cmd));
1494 ret = hns3_cmd_send(hw, desc, desc_num);
1495 resp_code = (rte_le_to_cpu_32(desc[0].data[0]) >> 8) & 0xff;
1496 retval = rte_le_to_cpu_16(desc[0].retval);
1498 cfg_status = hns3_get_mac_vlan_cmd_status(hw, retval, resp_code,
1503 hns3_err(hw, "add mac addr failed for cmd_send, ret =%d", ret);
1511 hns3_remove_mac_vlan_tbl(struct hns3_hw *hw,
1512 struct hns3_mac_vlan_tbl_entry_cmd *req)
1514 struct hns3_cmd_desc desc;
1519 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_MAC_VLAN_REMOVE, false);
1521 memcpy(desc.data, req, sizeof(struct hns3_mac_vlan_tbl_entry_cmd));
1523 ret = hns3_cmd_send(hw, &desc, 1);
1525 hns3_err(hw, "del mac addr failed for cmd_send, ret =%d", ret);
1528 resp_code = (rte_le_to_cpu_32(desc.data[0]) >> 8) & 0xff;
1529 retval = rte_le_to_cpu_16(desc.retval);
1531 return hns3_get_mac_vlan_cmd_status(hw, retval, resp_code,
1532 HNS3_MAC_VLAN_REMOVE);
1536 hns3_add_uc_mac_addr(struct hns3_hw *hw, struct rte_ether_addr *mac_addr)
1538 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
1539 struct hns3_mac_vlan_tbl_entry_cmd req;
1540 struct hns3_pf *pf = &hns->pf;
1541 struct hns3_cmd_desc desc;
1542 char mac_str[RTE_ETHER_ADDR_FMT_SIZE];
1543 uint16_t egress_port = 0;
1547 /* check if mac addr is valid */
1548 if (!rte_is_valid_assigned_ether_addr(mac_addr)) {
1549 hns3_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
1551 hns3_err(hw, "Add unicast mac addr err! addr(%s) invalid",
1556 memset(&req, 0, sizeof(req));
1559 * In current version VF is not supported when PF is driven by DPDK
1560 * driver, just need to configure parameters for PF vport.
1562 vf_id = HNS3_PF_FUNC_ID;
1563 hns3_set_field(egress_port, HNS3_MAC_EPORT_VFID_M,
1564 HNS3_MAC_EPORT_VFID_S, vf_id);
1566 req.egress_port = rte_cpu_to_le_16(egress_port);
1568 hns3_prepare_mac_addr(&req, mac_addr->addr_bytes, false);
1571 * Lookup the mac address in the mac_vlan table, and add
1572 * it if the entry is inexistent. Repeated unicast entry
1573 * is not allowed in the mac vlan table.
1575 ret = hns3_lookup_mac_vlan_tbl(hw, &req, &desc,
1576 HNS3_UC_MAC_VLAN_OPS_DESC_NUM);
1577 if (ret == -ENOENT) {
1578 if (!hns3_is_umv_space_full(hw)) {
1579 ret = hns3_add_mac_vlan_tbl(hw, &req, &desc,
1580 HNS3_UC_MAC_VLAN_OPS_DESC_NUM);
1582 hns3_update_umv_space(hw, false);
1586 hns3_err(hw, "UC MAC table full(%u)", pf->used_umv_size);
1591 hns3_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE, mac_addr);
1593 /* check if we just hit the duplicate */
1595 hns3_dbg(hw, "mac addr(%s) has been in the MAC table", mac_str);
1599 hns3_err(hw, "PF failed to add unicast entry(%s) in the MAC table",
1606 hns3_remove_uc_mac_addr(struct hns3_hw *hw, struct rte_ether_addr *mac_addr)
1608 struct hns3_mac_vlan_tbl_entry_cmd req;
1609 char mac_str[RTE_ETHER_ADDR_FMT_SIZE];
1612 /* check if mac addr is valid */
1613 if (!rte_is_valid_assigned_ether_addr(mac_addr)) {
1614 hns3_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
1616 hns3_err(hw, "remove unicast mac addr err! addr(%s) invalid",
1621 memset(&req, 0, sizeof(req));
1622 hns3_set_bit(req.entry_type, HNS3_MAC_VLAN_BIT0_EN_B, 0);
1623 hns3_prepare_mac_addr(&req, mac_addr->addr_bytes, false);
1624 ret = hns3_remove_mac_vlan_tbl(hw, &req);
1625 if (ret == -ENOENT) /* mac addr isn't existent in the mac vlan table. */
1628 hns3_update_umv_space(hw, true);
1634 hns3_set_default_mac_addr(struct rte_eth_dev *dev,
1635 struct rte_ether_addr *mac_addr)
1637 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1638 struct rte_ether_addr *oaddr;
1639 char mac_str[RTE_ETHER_ADDR_FMT_SIZE];
1642 rte_spinlock_lock(&hw->lock);
1643 oaddr = (struct rte_ether_addr *)hw->mac.mac_addr;
1644 ret = hw->ops.del_uc_mac_addr(hw, oaddr);
1646 hns3_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
1648 hns3_warn(hw, "Remove old uc mac address(%s) fail: %d",
1651 rte_spinlock_unlock(&hw->lock);
1655 ret = hw->ops.add_uc_mac_addr(hw, mac_addr);
1657 hns3_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
1659 hns3_err(hw, "Failed to set mac addr(%s): %d", mac_str, ret);
1660 goto err_add_uc_addr;
1663 ret = hns3_pause_addr_cfg(hw, mac_addr->addr_bytes);
1665 hns3_err(hw, "Failed to configure mac pause address: %d", ret);
1666 goto err_pause_addr_cfg;
1669 rte_ether_addr_copy(mac_addr,
1670 (struct rte_ether_addr *)hw->mac.mac_addr);
1671 rte_spinlock_unlock(&hw->lock);
1676 ret_val = hw->ops.del_uc_mac_addr(hw, mac_addr);
1678 hns3_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
1681 "Failed to roll back to del setted mac addr(%s): %d",
1686 ret_val = hw->ops.add_uc_mac_addr(hw, oaddr);
1688 hns3_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE, oaddr);
1689 hns3_warn(hw, "Failed to restore old uc mac addr(%s): %d",
1692 rte_spinlock_unlock(&hw->lock);
1698 hns3_update_desc_vfid(struct hns3_cmd_desc *desc, uint8_t vfid, bool clr)
1700 #define HNS3_VF_NUM_IN_FIRST_DESC 192
1704 if (vfid < HNS3_VF_NUM_IN_FIRST_DESC) {
1705 word_num = vfid / 32;
1706 bit_num = vfid % 32;
1708 desc[1].data[word_num] &=
1709 rte_cpu_to_le_32(~(1UL << bit_num));
1711 desc[1].data[word_num] |=
1712 rte_cpu_to_le_32(1UL << bit_num);
1714 word_num = (vfid - HNS3_VF_NUM_IN_FIRST_DESC) / 32;
1715 bit_num = vfid % 32;
1717 desc[2].data[word_num] &=
1718 rte_cpu_to_le_32(~(1UL << bit_num));
1720 desc[2].data[word_num] |=
1721 rte_cpu_to_le_32(1UL << bit_num);
1726 hns3_add_mc_mac_addr(struct hns3_hw *hw, struct rte_ether_addr *mac_addr)
1728 struct hns3_cmd_desc desc[HNS3_MC_MAC_VLAN_OPS_DESC_NUM];
1729 struct hns3_mac_vlan_tbl_entry_cmd req;
1730 char mac_str[RTE_ETHER_ADDR_FMT_SIZE];
1734 /* Check if mac addr is valid */
1735 if (!rte_is_multicast_ether_addr(mac_addr)) {
1736 hns3_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
1738 hns3_err(hw, "failed to add mc mac addr, addr(%s) invalid",
1743 memset(&req, 0, sizeof(req));
1744 hns3_set_bit(req.entry_type, HNS3_MAC_VLAN_BIT0_EN_B, 0);
1745 hns3_prepare_mac_addr(&req, mac_addr->addr_bytes, true);
1746 ret = hns3_lookup_mac_vlan_tbl(hw, &req, desc,
1747 HNS3_MC_MAC_VLAN_OPS_DESC_NUM);
1749 /* This mac addr do not exist, add new entry for it */
1750 memset(desc[0].data, 0, sizeof(desc[0].data));
1751 memset(desc[1].data, 0, sizeof(desc[0].data));
1752 memset(desc[2].data, 0, sizeof(desc[0].data));
1756 * In current version VF is not supported when PF is driven by DPDK
1757 * driver, just need to configure parameters for PF vport.
1759 vf_id = HNS3_PF_FUNC_ID;
1760 hns3_update_desc_vfid(desc, vf_id, false);
1761 ret = hns3_add_mac_vlan_tbl(hw, &req, desc,
1762 HNS3_MC_MAC_VLAN_OPS_DESC_NUM);
1765 hns3_err(hw, "mc mac vlan table is full");
1766 hns3_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
1768 hns3_err(hw, "failed to add mc mac addr(%s): %d", mac_str, ret);
1775 hns3_remove_mc_mac_addr(struct hns3_hw *hw, struct rte_ether_addr *mac_addr)
1777 struct hns3_mac_vlan_tbl_entry_cmd req;
1778 struct hns3_cmd_desc desc[3];
1779 char mac_str[RTE_ETHER_ADDR_FMT_SIZE];
1783 /* Check if mac addr is valid */
1784 if (!rte_is_multicast_ether_addr(mac_addr)) {
1785 hns3_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
1787 hns3_err(hw, "Failed to rm mc mac addr, addr(%s) invalid",
1792 memset(&req, 0, sizeof(req));
1793 hns3_set_bit(req.entry_type, HNS3_MAC_VLAN_BIT0_EN_B, 0);
1794 hns3_prepare_mac_addr(&req, mac_addr->addr_bytes, true);
1795 ret = hns3_lookup_mac_vlan_tbl(hw, &req, desc,
1796 HNS3_MC_MAC_VLAN_OPS_DESC_NUM);
1799 * This mac addr exist, remove this handle's VFID for it.
1800 * In current version VF is not supported when PF is driven by
1801 * DPDK driver, just need to configure parameters for PF vport.
1803 vf_id = HNS3_PF_FUNC_ID;
1804 hns3_update_desc_vfid(desc, vf_id, true);
1806 /* All the vfid is zero, so need to delete this entry */
1807 ret = hns3_remove_mac_vlan_tbl(hw, &req);
1808 } else if (ret == -ENOENT) {
1809 /* This mac addr doesn't exist. */
1814 hns3_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
1816 hns3_err(hw, "Failed to rm mc mac addr(%s): %d", mac_str, ret);
1823 hns3_check_mq_mode(struct rte_eth_dev *dev)
1825 enum rte_eth_rx_mq_mode rx_mq_mode = dev->data->dev_conf.rxmode.mq_mode;
1826 enum rte_eth_tx_mq_mode tx_mq_mode = dev->data->dev_conf.txmode.mq_mode;
1827 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1828 struct hns3_pf *pf = HNS3_DEV_PRIVATE_TO_PF(dev->data->dev_private);
1829 struct rte_eth_dcb_rx_conf *dcb_rx_conf;
1830 struct rte_eth_dcb_tx_conf *dcb_tx_conf;
1835 if (((uint32_t)rx_mq_mode & RTE_ETH_MQ_RX_VMDQ_FLAG) ||
1836 (tx_mq_mode == RTE_ETH_MQ_TX_VMDQ_DCB ||
1837 tx_mq_mode == RTE_ETH_MQ_TX_VMDQ_ONLY)) {
1838 hns3_err(hw, "VMDQ is not supported, rx_mq_mode = %d, tx_mq_mode = %d.",
1839 rx_mq_mode, tx_mq_mode);
1843 dcb_rx_conf = &dev->data->dev_conf.rx_adv_conf.dcb_rx_conf;
1844 dcb_tx_conf = &dev->data->dev_conf.tx_adv_conf.dcb_tx_conf;
1845 if ((uint32_t)rx_mq_mode & RTE_ETH_MQ_RX_DCB_FLAG) {
1846 if (dcb_rx_conf->nb_tcs > pf->tc_max) {
1847 hns3_err(hw, "nb_tcs(%u) > max_tc(%u) driver supported.",
1848 dcb_rx_conf->nb_tcs, pf->tc_max);
1852 if (!(dcb_rx_conf->nb_tcs == HNS3_4_TCS ||
1853 dcb_rx_conf->nb_tcs == HNS3_8_TCS)) {
1854 hns3_err(hw, "on RTE_ETH_MQ_RX_DCB_RSS mode, "
1855 "nb_tcs(%d) != %d or %d in rx direction.",
1856 dcb_rx_conf->nb_tcs, HNS3_4_TCS, HNS3_8_TCS);
1860 if (dcb_rx_conf->nb_tcs != dcb_tx_conf->nb_tcs) {
1861 hns3_err(hw, "num_tcs(%d) of tx is not equal to rx(%d)",
1862 dcb_tx_conf->nb_tcs, dcb_rx_conf->nb_tcs);
1866 for (i = 0; i < HNS3_MAX_USER_PRIO; i++) {
1867 if (dcb_rx_conf->dcb_tc[i] != dcb_tx_conf->dcb_tc[i]) {
1868 hns3_err(hw, "dcb_tc[%d] = %u in rx direction, "
1869 "is not equal to one in tx direction.",
1870 i, dcb_rx_conf->dcb_tc[i]);
1873 if (dcb_rx_conf->dcb_tc[i] > max_tc)
1874 max_tc = dcb_rx_conf->dcb_tc[i];
1877 num_tc = max_tc + 1;
1878 if (num_tc > dcb_rx_conf->nb_tcs) {
1879 hns3_err(hw, "max num_tc(%u) mapped > nb_tcs(%u)",
1880 num_tc, dcb_rx_conf->nb_tcs);
1889 hns3_bind_ring_with_vector(struct hns3_hw *hw, uint16_t vector_id, bool en,
1890 enum hns3_ring_type queue_type, uint16_t queue_id)
1892 struct hns3_cmd_desc desc;
1893 struct hns3_ctrl_vector_chain_cmd *req =
1894 (struct hns3_ctrl_vector_chain_cmd *)desc.data;
1895 enum hns3_opcode_type op;
1896 uint16_t tqp_type_and_id = 0;
1901 op = en ? HNS3_OPC_ADD_RING_TO_VECTOR : HNS3_OPC_DEL_RING_TO_VECTOR;
1902 hns3_cmd_setup_basic_desc(&desc, op, false);
1903 req->int_vector_id = hns3_get_field(vector_id, HNS3_TQP_INT_ID_L_M,
1904 HNS3_TQP_INT_ID_L_S);
1905 req->int_vector_id_h = hns3_get_field(vector_id, HNS3_TQP_INT_ID_H_M,
1906 HNS3_TQP_INT_ID_H_S);
1908 if (queue_type == HNS3_RING_TYPE_RX)
1909 gl = HNS3_RING_GL_RX;
1911 gl = HNS3_RING_GL_TX;
1915 hns3_set_field(tqp_type_and_id, HNS3_INT_TYPE_M, HNS3_INT_TYPE_S,
1917 hns3_set_field(tqp_type_and_id, HNS3_TQP_ID_M, HNS3_TQP_ID_S, queue_id);
1918 hns3_set_field(tqp_type_and_id, HNS3_INT_GL_IDX_M, HNS3_INT_GL_IDX_S,
1920 req->tqp_type_and_id[0] = rte_cpu_to_le_16(tqp_type_and_id);
1921 req->int_cause_num = 1;
1922 ret = hns3_cmd_send(hw, &desc, 1);
1924 hns3_err(hw, "%s TQP %u fail, vector_id = %u, ret = %d.",
1925 en ? "Map" : "Unmap", queue_id, vector_id, ret);
1933 hns3_setup_dcb(struct rte_eth_dev *dev)
1935 struct hns3_adapter *hns = dev->data->dev_private;
1936 struct hns3_hw *hw = &hns->hw;
1939 if (!hns3_dev_get_support(hw, DCB)) {
1940 hns3_err(hw, "this port does not support dcb configurations.");
1944 if (hw->current_fc_status == HNS3_FC_STATUS_MAC_PAUSE) {
1945 hns3_err(hw, "MAC pause enabled, cannot config dcb info.");
1949 ret = hns3_dcb_configure(hns);
1951 hns3_err(hw, "failed to config dcb: %d", ret);
1957 hns3_check_link_speed(struct hns3_hw *hw, uint32_t link_speeds)
1962 * Some hardware doesn't support auto-negotiation, but users may not
1963 * configure link_speeds (default 0), which means auto-negotiation.
1964 * In this case, it should return success.
1966 if (link_speeds == RTE_ETH_LINK_SPEED_AUTONEG &&
1967 hw->mac.support_autoneg == 0)
1970 if (link_speeds != RTE_ETH_LINK_SPEED_AUTONEG) {
1971 ret = hns3_check_port_speed(hw, link_speeds);
1980 hns3_check_dev_conf(struct rte_eth_dev *dev)
1982 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1983 struct rte_eth_conf *conf = &dev->data->dev_conf;
1986 ret = hns3_check_mq_mode(dev);
1990 return hns3_check_link_speed(hw, conf->link_speeds);
1994 hns3_dev_configure(struct rte_eth_dev *dev)
1996 struct hns3_adapter *hns = dev->data->dev_private;
1997 struct rte_eth_conf *conf = &dev->data->dev_conf;
1998 enum rte_eth_rx_mq_mode mq_mode = conf->rxmode.mq_mode;
1999 struct hns3_hw *hw = &hns->hw;
2000 uint16_t nb_rx_q = dev->data->nb_rx_queues;
2001 uint16_t nb_tx_q = dev->data->nb_tx_queues;
2002 struct rte_eth_rss_conf rss_conf;
2006 hw->cfg_max_queues = RTE_MAX(nb_rx_q, nb_tx_q);
2009 * Some versions of hardware network engine does not support
2010 * individually enable/disable/reset the Tx or Rx queue. These devices
2011 * must enable/disable/reset Tx and Rx queues at the same time. When the
2012 * numbers of Tx queues allocated by upper applications are not equal to
2013 * the numbers of Rx queues, driver needs to setup fake Tx or Rx queues
2014 * to adjust numbers of Tx/Rx queues. otherwise, network engine can not
2015 * work as usual. But these fake queues are imperceptible, and can not
2016 * be used by upper applications.
2018 ret = hns3_set_fake_rx_or_tx_queues(dev, nb_rx_q, nb_tx_q);
2020 hns3_err(hw, "fail to set Rx/Tx fake queues, ret = %d.", ret);
2021 hw->cfg_max_queues = 0;
2025 hw->adapter_state = HNS3_NIC_CONFIGURING;
2026 ret = hns3_check_dev_conf(dev);
2030 if ((uint32_t)mq_mode & RTE_ETH_MQ_RX_DCB_FLAG) {
2031 ret = hns3_setup_dcb(dev);
2036 /* When RSS is not configured, redirect the packet queue 0 */
2037 if ((uint32_t)mq_mode & RTE_ETH_MQ_RX_RSS_FLAG) {
2038 conf->rxmode.offloads |= RTE_ETH_RX_OFFLOAD_RSS_HASH;
2039 rss_conf = conf->rx_adv_conf.rss_conf;
2040 hw->rss_dis_flag = false;
2041 ret = hns3_dev_rss_hash_update(dev, &rss_conf);
2046 ret = hns3_dev_mtu_set(dev, conf->rxmode.mtu);
2050 ret = hns3_mbuf_dyn_rx_timestamp_register(dev, conf);
2054 ret = hns3_dev_configure_vlan(dev);
2058 /* config hardware GRO */
2059 gro_en = conf->rxmode.offloads & RTE_ETH_RX_OFFLOAD_TCP_LRO ? true : false;
2060 ret = hns3_config_gro(hw, gro_en);
2064 hns3_init_rx_ptype_tble(dev);
2065 hw->adapter_state = HNS3_NIC_CONFIGURED;
2070 hw->cfg_max_queues = 0;
2071 (void)hns3_set_fake_rx_or_tx_queues(dev, 0, 0);
2072 hw->adapter_state = HNS3_NIC_INITIALIZED;
2078 hns3_set_mac_mtu(struct hns3_hw *hw, uint16_t new_mps)
2080 struct hns3_config_max_frm_size_cmd *req;
2081 struct hns3_cmd_desc desc;
2083 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_CONFIG_MAX_FRM_SIZE, false);
2085 req = (struct hns3_config_max_frm_size_cmd *)desc.data;
2086 req->max_frm_size = rte_cpu_to_le_16(new_mps);
2087 req->min_frm_size = RTE_ETHER_MIN_LEN;
2089 return hns3_cmd_send(hw, &desc, 1);
2093 hns3_config_mtu(struct hns3_hw *hw, uint16_t mps)
2095 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
2096 uint16_t original_mps = hns->pf.mps;
2100 ret = hns3_set_mac_mtu(hw, mps);
2102 hns3_err(hw, "failed to set mtu, ret = %d", ret);
2107 ret = hns3_buffer_alloc(hw);
2109 hns3_err(hw, "failed to allocate buffer, ret = %d", ret);
2116 err = hns3_set_mac_mtu(hw, original_mps);
2118 hns3_err(hw, "fail to rollback MTU, err = %d", err);
2121 hns->pf.mps = original_mps;
2127 hns3_dev_mtu_set(struct rte_eth_dev *dev, uint16_t mtu)
2129 struct hns3_adapter *hns = dev->data->dev_private;
2130 uint32_t frame_size = mtu + HNS3_ETH_OVERHEAD;
2131 struct hns3_hw *hw = &hns->hw;
2134 if (dev->data->dev_started) {
2135 hns3_err(hw, "Failed to set mtu, port %u must be stopped "
2136 "before configuration", dev->data->port_id);
2140 rte_spinlock_lock(&hw->lock);
2141 frame_size = RTE_MAX(frame_size, HNS3_DEFAULT_FRAME_LEN);
2144 * Maximum value of frame_size is HNS3_MAX_FRAME_LEN, so it can safely
2145 * assign to "uint16_t" type variable.
2147 ret = hns3_config_mtu(hw, (uint16_t)frame_size);
2149 rte_spinlock_unlock(&hw->lock);
2150 hns3_err(hw, "Failed to set mtu, port %u mtu %u: %d",
2151 dev->data->port_id, mtu, ret);
2155 rte_spinlock_unlock(&hw->lock);
2161 hns3_get_copper_port_speed_capa(uint32_t supported_speed)
2163 uint32_t speed_capa = 0;
2165 if (supported_speed & HNS3_PHY_LINK_SPEED_10M_HD_BIT)
2166 speed_capa |= RTE_ETH_LINK_SPEED_10M_HD;
2167 if (supported_speed & HNS3_PHY_LINK_SPEED_10M_BIT)
2168 speed_capa |= RTE_ETH_LINK_SPEED_10M;
2169 if (supported_speed & HNS3_PHY_LINK_SPEED_100M_HD_BIT)
2170 speed_capa |= RTE_ETH_LINK_SPEED_100M_HD;
2171 if (supported_speed & HNS3_PHY_LINK_SPEED_100M_BIT)
2172 speed_capa |= RTE_ETH_LINK_SPEED_100M;
2173 if (supported_speed & HNS3_PHY_LINK_SPEED_1000M_BIT)
2174 speed_capa |= RTE_ETH_LINK_SPEED_1G;
2180 hns3_get_firber_port_speed_capa(uint32_t supported_speed)
2182 uint32_t speed_capa = 0;
2184 if (supported_speed & HNS3_FIBER_LINK_SPEED_1G_BIT)
2185 speed_capa |= RTE_ETH_LINK_SPEED_1G;
2186 if (supported_speed & HNS3_FIBER_LINK_SPEED_10G_BIT)
2187 speed_capa |= RTE_ETH_LINK_SPEED_10G;
2188 if (supported_speed & HNS3_FIBER_LINK_SPEED_25G_BIT)
2189 speed_capa |= RTE_ETH_LINK_SPEED_25G;
2190 if (supported_speed & HNS3_FIBER_LINK_SPEED_40G_BIT)
2191 speed_capa |= RTE_ETH_LINK_SPEED_40G;
2192 if (supported_speed & HNS3_FIBER_LINK_SPEED_50G_BIT)
2193 speed_capa |= RTE_ETH_LINK_SPEED_50G;
2194 if (supported_speed & HNS3_FIBER_LINK_SPEED_100G_BIT)
2195 speed_capa |= RTE_ETH_LINK_SPEED_100G;
2196 if (supported_speed & HNS3_FIBER_LINK_SPEED_200G_BIT)
2197 speed_capa |= RTE_ETH_LINK_SPEED_200G;
2203 hns3_get_speed_capa(struct hns3_hw *hw)
2205 struct hns3_mac *mac = &hw->mac;
2206 uint32_t speed_capa;
2208 if (mac->media_type == HNS3_MEDIA_TYPE_COPPER)
2210 hns3_get_copper_port_speed_capa(mac->supported_speed);
2213 hns3_get_firber_port_speed_capa(mac->supported_speed);
2215 if (mac->support_autoneg == 0)
2216 speed_capa |= RTE_ETH_LINK_SPEED_FIXED;
2222 hns3_update_port_link_info(struct rte_eth_dev *eth_dev)
2224 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(eth_dev->data->dev_private);
2227 (void)hns3_update_link_status(hw);
2229 ret = hns3_update_link_info(eth_dev);
2231 hw->mac.link_status = RTE_ETH_LINK_DOWN;
2237 hns3_setup_linkstatus(struct rte_eth_dev *eth_dev,
2238 struct rte_eth_link *new_link)
2240 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(eth_dev->data->dev_private);
2241 struct hns3_mac *mac = &hw->mac;
2243 switch (mac->link_speed) {
2244 case RTE_ETH_SPEED_NUM_10M:
2245 case RTE_ETH_SPEED_NUM_100M:
2246 case RTE_ETH_SPEED_NUM_1G:
2247 case RTE_ETH_SPEED_NUM_10G:
2248 case RTE_ETH_SPEED_NUM_25G:
2249 case RTE_ETH_SPEED_NUM_40G:
2250 case RTE_ETH_SPEED_NUM_50G:
2251 case RTE_ETH_SPEED_NUM_100G:
2252 case RTE_ETH_SPEED_NUM_200G:
2253 if (mac->link_status)
2254 new_link->link_speed = mac->link_speed;
2257 if (mac->link_status)
2258 new_link->link_speed = RTE_ETH_SPEED_NUM_UNKNOWN;
2262 if (!mac->link_status)
2263 new_link->link_speed = RTE_ETH_SPEED_NUM_NONE;
2265 new_link->link_duplex = mac->link_duplex;
2266 new_link->link_status = mac->link_status ? RTE_ETH_LINK_UP : RTE_ETH_LINK_DOWN;
2267 new_link->link_autoneg = mac->link_autoneg;
2271 hns3_dev_link_update(struct rte_eth_dev *eth_dev, int wait_to_complete)
2273 #define HNS3_LINK_CHECK_INTERVAL 100 /* 100ms */
2274 #define HNS3_MAX_LINK_CHECK_TIMES 20 /* 2s (100 * 20ms) in total */
2276 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(eth_dev->data->dev_private);
2277 uint32_t retry_cnt = HNS3_MAX_LINK_CHECK_TIMES;
2278 struct hns3_mac *mac = &hw->mac;
2279 struct rte_eth_link new_link;
2282 /* When port is stopped, report link down. */
2283 if (eth_dev->data->dev_started == 0) {
2284 new_link.link_autoneg = mac->link_autoneg;
2285 new_link.link_duplex = mac->link_duplex;
2286 new_link.link_speed = RTE_ETH_SPEED_NUM_NONE;
2287 new_link.link_status = RTE_ETH_LINK_DOWN;
2292 ret = hns3_update_port_link_info(eth_dev);
2294 hns3_err(hw, "failed to get port link info, ret = %d.",
2299 if (!wait_to_complete || mac->link_status == RTE_ETH_LINK_UP)
2302 rte_delay_ms(HNS3_LINK_CHECK_INTERVAL);
2303 } while (retry_cnt--);
2305 memset(&new_link, 0, sizeof(new_link));
2306 hns3_setup_linkstatus(eth_dev, &new_link);
2309 return rte_eth_linkstatus_set(eth_dev, &new_link);
2313 hns3_dev_set_link_up(struct rte_eth_dev *dev)
2315 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2319 * The "tx_pkt_burst" will be restored. But the secondary process does
2320 * not support the mechanism for notifying the primary process.
2322 if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
2323 hns3_err(hw, "secondary process does not support to set link up.");
2328 * If device isn't started Rx/Tx function is still disabled, setting
2329 * link up is not allowed. But it is probably better to return success
2330 * to reduce the impact on the upper layer.
2332 if (hw->adapter_state != HNS3_NIC_STARTED) {
2333 hns3_info(hw, "device isn't started, can't set link up.");
2337 if (!hw->set_link_down)
2340 rte_spinlock_lock(&hw->lock);
2341 ret = hns3_cfg_mac_mode(hw, true);
2343 rte_spinlock_unlock(&hw->lock);
2344 hns3_err(hw, "failed to set link up, ret = %d", ret);
2348 hw->set_link_down = false;
2349 hns3_start_tx_datapath(dev);
2350 rte_spinlock_unlock(&hw->lock);
2356 hns3_dev_set_link_down(struct rte_eth_dev *dev)
2358 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2362 * The "tx_pkt_burst" will be set to dummy function. But the secondary
2363 * process does not support the mechanism for notifying the primary
2366 if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
2367 hns3_err(hw, "secondary process does not support to set link down.");
2372 * If device isn't started or the API has been called, link status is
2373 * down, return success.
2375 if (hw->adapter_state != HNS3_NIC_STARTED || hw->set_link_down)
2378 rte_spinlock_lock(&hw->lock);
2379 hns3_stop_tx_datapath(dev);
2380 ret = hns3_cfg_mac_mode(hw, false);
2382 hns3_start_tx_datapath(dev);
2383 rte_spinlock_unlock(&hw->lock);
2384 hns3_err(hw, "failed to set link down, ret = %d", ret);
2388 hw->set_link_down = true;
2389 rte_spinlock_unlock(&hw->lock);
2395 hns3_parse_func_status(struct hns3_hw *hw, struct hns3_func_status_cmd *status)
2397 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
2398 struct hns3_pf *pf = &hns->pf;
2400 if (!(status->pf_state & HNS3_PF_STATE_DONE))
2403 pf->is_main_pf = (status->pf_state & HNS3_PF_STATE_MAIN) ? true : false;
2409 hns3_query_function_status(struct hns3_hw *hw)
2411 #define HNS3_QUERY_MAX_CNT 10
2412 #define HNS3_QUERY_SLEEP_MSCOEND 1
2413 struct hns3_func_status_cmd *req;
2414 struct hns3_cmd_desc desc;
2418 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_QUERY_FUNC_STATUS, true);
2419 req = (struct hns3_func_status_cmd *)desc.data;
2422 ret = hns3_cmd_send(hw, &desc, 1);
2424 PMD_INIT_LOG(ERR, "query function status failed %d",
2429 /* Check pf reset is done */
2433 rte_delay_ms(HNS3_QUERY_SLEEP_MSCOEND);
2434 } while (timeout++ < HNS3_QUERY_MAX_CNT);
2436 return hns3_parse_func_status(hw, req);
2440 hns3_get_pf_max_tqp_num(struct hns3_hw *hw)
2442 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
2443 struct hns3_pf *pf = &hns->pf;
2445 if (pf->tqp_config_mode == HNS3_FLEX_MAX_TQP_NUM_MODE) {
2447 * The total_tqps_num obtained from firmware is maximum tqp
2448 * numbers of this port, which should be used for PF and VFs.
2449 * There is no need for pf to have so many tqp numbers in
2450 * most cases. RTE_LIBRTE_HNS3_MAX_TQP_NUM_PER_PF,
2451 * coming from config file, is assigned to maximum queue number
2452 * for the PF of this port by user. So users can modify the
2453 * maximum queue number of PF according to their own application
2454 * scenarios, which is more flexible to use. In addition, many
2455 * memories can be saved due to allocating queue statistics
2456 * room according to the actual number of queues required. The
2457 * maximum queue number of PF for network engine with
2458 * revision_id greater than 0x30 is assigned by config file.
2460 if (RTE_LIBRTE_HNS3_MAX_TQP_NUM_PER_PF <= 0) {
2461 hns3_err(hw, "RTE_LIBRTE_HNS3_MAX_TQP_NUM_PER_PF(%d) "
2462 "must be greater than 0.",
2463 RTE_LIBRTE_HNS3_MAX_TQP_NUM_PER_PF);
2467 hw->tqps_num = RTE_MIN(RTE_LIBRTE_HNS3_MAX_TQP_NUM_PER_PF,
2468 hw->total_tqps_num);
2471 * Due to the limitation on the number of PF interrupts
2472 * available, the maximum queue number assigned to PF on
2473 * the network engine with revision_id 0x21 is 64.
2475 hw->tqps_num = RTE_MIN(hw->total_tqps_num,
2476 HNS3_MAX_TQP_NUM_HIP08_PF);
2483 hns3_query_pf_resource(struct hns3_hw *hw)
2485 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
2486 struct hns3_pf *pf = &hns->pf;
2487 struct hns3_pf_res_cmd *req;
2488 struct hns3_cmd_desc desc;
2491 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_QUERY_PF_RSRC, true);
2492 ret = hns3_cmd_send(hw, &desc, 1);
2494 PMD_INIT_LOG(ERR, "query pf resource failed %d", ret);
2498 req = (struct hns3_pf_res_cmd *)desc.data;
2499 hw->total_tqps_num = rte_le_to_cpu_16(req->tqp_num) +
2500 rte_le_to_cpu_16(req->ext_tqp_num);
2501 ret = hns3_get_pf_max_tqp_num(hw);
2505 pf->pkt_buf_size = rte_le_to_cpu_16(req->buf_size) << HNS3_BUF_UNIT_S;
2506 pf->func_num = rte_le_to_cpu_16(req->pf_own_fun_number);
2508 if (req->tx_buf_size)
2510 rte_le_to_cpu_16(req->tx_buf_size) << HNS3_BUF_UNIT_S;
2512 pf->tx_buf_size = HNS3_DEFAULT_TX_BUF;
2514 pf->tx_buf_size = roundup(pf->tx_buf_size, HNS3_BUF_SIZE_UNIT);
2516 if (req->dv_buf_size)
2518 rte_le_to_cpu_16(req->dv_buf_size) << HNS3_BUF_UNIT_S;
2520 pf->dv_buf_size = HNS3_DEFAULT_DV;
2522 pf->dv_buf_size = roundup(pf->dv_buf_size, HNS3_BUF_SIZE_UNIT);
2525 hns3_get_field(rte_le_to_cpu_16(req->nic_pf_intr_vector_number),
2526 HNS3_PF_VEC_NUM_M, HNS3_PF_VEC_NUM_S);
2532 hns3_parse_cfg(struct hns3_cfg *cfg, struct hns3_cmd_desc *desc)
2534 struct hns3_cfg_param_cmd *req;
2535 uint64_t mac_addr_tmp_high;
2536 uint8_t ext_rss_size_max;
2537 uint64_t mac_addr_tmp;
2540 req = (struct hns3_cfg_param_cmd *)desc[0].data;
2542 /* get the configuration */
2543 cfg->tc_num = hns3_get_field(rte_le_to_cpu_32(req->param[0]),
2544 HNS3_CFG_TC_NUM_M, HNS3_CFG_TC_NUM_S);
2545 cfg->tqp_desc_num = hns3_get_field(rte_le_to_cpu_32(req->param[0]),
2546 HNS3_CFG_TQP_DESC_N_M,
2547 HNS3_CFG_TQP_DESC_N_S);
2549 cfg->phy_addr = hns3_get_field(rte_le_to_cpu_32(req->param[1]),
2550 HNS3_CFG_PHY_ADDR_M,
2551 HNS3_CFG_PHY_ADDR_S);
2552 cfg->media_type = hns3_get_field(rte_le_to_cpu_32(req->param[1]),
2553 HNS3_CFG_MEDIA_TP_M,
2554 HNS3_CFG_MEDIA_TP_S);
2555 cfg->rx_buf_len = hns3_get_field(rte_le_to_cpu_32(req->param[1]),
2556 HNS3_CFG_RX_BUF_LEN_M,
2557 HNS3_CFG_RX_BUF_LEN_S);
2558 /* get mac address */
2559 mac_addr_tmp = rte_le_to_cpu_32(req->param[2]);
2560 mac_addr_tmp_high = hns3_get_field(rte_le_to_cpu_32(req->param[3]),
2561 HNS3_CFG_MAC_ADDR_H_M,
2562 HNS3_CFG_MAC_ADDR_H_S);
2564 mac_addr_tmp |= (mac_addr_tmp_high << 31) << 1;
2566 cfg->default_speed = hns3_get_field(rte_le_to_cpu_32(req->param[3]),
2567 HNS3_CFG_DEFAULT_SPEED_M,
2568 HNS3_CFG_DEFAULT_SPEED_S);
2569 cfg->rss_size_max = hns3_get_field(rte_le_to_cpu_32(req->param[3]),
2570 HNS3_CFG_RSS_SIZE_M,
2571 HNS3_CFG_RSS_SIZE_S);
2573 for (i = 0; i < RTE_ETHER_ADDR_LEN; i++)
2574 cfg->mac_addr[i] = (mac_addr_tmp >> (8 * i)) & 0xff;
2576 req = (struct hns3_cfg_param_cmd *)desc[1].data;
2577 cfg->numa_node_map = rte_le_to_cpu_32(req->param[0]);
2579 cfg->speed_ability = hns3_get_field(rte_le_to_cpu_32(req->param[1]),
2580 HNS3_CFG_SPEED_ABILITY_M,
2581 HNS3_CFG_SPEED_ABILITY_S);
2582 cfg->umv_space = hns3_get_field(rte_le_to_cpu_32(req->param[1]),
2583 HNS3_CFG_UMV_TBL_SPACE_M,
2584 HNS3_CFG_UMV_TBL_SPACE_S);
2585 if (!cfg->umv_space)
2586 cfg->umv_space = HNS3_DEFAULT_UMV_SPACE_PER_PF;
2588 ext_rss_size_max = hns3_get_field(rte_le_to_cpu_32(req->param[2]),
2589 HNS3_CFG_EXT_RSS_SIZE_M,
2590 HNS3_CFG_EXT_RSS_SIZE_S);
2592 * Field ext_rss_size_max obtained from firmware will be more flexible
2593 * for future changes and expansions, which is an exponent of 2, instead
2594 * of reading out directly. If this field is not zero, hns3 PF PMD
2595 * uses it as rss_size_max under one TC. Device, whose revision
2596 * id is greater than or equal to PCI_REVISION_ID_HIP09_A, obtains the
2597 * maximum number of queues supported under a TC through this field.
2599 if (ext_rss_size_max)
2600 cfg->rss_size_max = 1U << ext_rss_size_max;
2603 /* hns3_get_board_cfg: query the static parameter from NCL_config file in flash
2604 * @hw: pointer to struct hns3_hw
2605 * @hcfg: the config structure to be getted
2608 hns3_get_board_cfg(struct hns3_hw *hw, struct hns3_cfg *hcfg)
2610 struct hns3_cmd_desc desc[HNS3_PF_CFG_DESC_NUM];
2611 struct hns3_cfg_param_cmd *req;
2616 for (i = 0; i < HNS3_PF_CFG_DESC_NUM; i++) {
2618 req = (struct hns3_cfg_param_cmd *)desc[i].data;
2619 hns3_cmd_setup_basic_desc(&desc[i], HNS3_OPC_GET_CFG_PARAM,
2621 hns3_set_field(offset, HNS3_CFG_OFFSET_M, HNS3_CFG_OFFSET_S,
2622 i * HNS3_CFG_RD_LEN_BYTES);
2623 /* Len should be divided by 4 when send to hardware */
2624 hns3_set_field(offset, HNS3_CFG_RD_LEN_M, HNS3_CFG_RD_LEN_S,
2625 HNS3_CFG_RD_LEN_BYTES / HNS3_CFG_RD_LEN_UNIT);
2626 req->offset = rte_cpu_to_le_32(offset);
2629 ret = hns3_cmd_send(hw, desc, HNS3_PF_CFG_DESC_NUM);
2631 PMD_INIT_LOG(ERR, "get config failed %d.", ret);
2635 hns3_parse_cfg(hcfg, desc);
2641 hns3_parse_speed(int speed_cmd, uint32_t *speed)
2643 switch (speed_cmd) {
2644 case HNS3_CFG_SPEED_10M:
2645 *speed = RTE_ETH_SPEED_NUM_10M;
2647 case HNS3_CFG_SPEED_100M:
2648 *speed = RTE_ETH_SPEED_NUM_100M;
2650 case HNS3_CFG_SPEED_1G:
2651 *speed = RTE_ETH_SPEED_NUM_1G;
2653 case HNS3_CFG_SPEED_10G:
2654 *speed = RTE_ETH_SPEED_NUM_10G;
2656 case HNS3_CFG_SPEED_25G:
2657 *speed = RTE_ETH_SPEED_NUM_25G;
2659 case HNS3_CFG_SPEED_40G:
2660 *speed = RTE_ETH_SPEED_NUM_40G;
2662 case HNS3_CFG_SPEED_50G:
2663 *speed = RTE_ETH_SPEED_NUM_50G;
2665 case HNS3_CFG_SPEED_100G:
2666 *speed = RTE_ETH_SPEED_NUM_100G;
2668 case HNS3_CFG_SPEED_200G:
2669 *speed = RTE_ETH_SPEED_NUM_200G;
2679 hns3_set_default_dev_specifications(struct hns3_hw *hw)
2681 hw->max_non_tso_bd_num = HNS3_MAX_NON_TSO_BD_PER_PKT;
2682 hw->rss_ind_tbl_size = HNS3_RSS_IND_TBL_SIZE;
2683 hw->rss_key_size = HNS3_RSS_KEY_SIZE;
2684 hw->max_tm_rate = HNS3_ETHER_MAX_RATE;
2685 hw->intr.int_ql_max = HNS3_INTR_QL_NONE;
2689 hns3_parse_dev_specifications(struct hns3_hw *hw, struct hns3_cmd_desc *desc)
2691 struct hns3_dev_specs_0_cmd *req0;
2693 req0 = (struct hns3_dev_specs_0_cmd *)desc[0].data;
2695 hw->max_non_tso_bd_num = req0->max_non_tso_bd_num;
2696 hw->rss_ind_tbl_size = rte_le_to_cpu_16(req0->rss_ind_tbl_size);
2697 hw->rss_key_size = rte_le_to_cpu_16(req0->rss_key_size);
2698 hw->max_tm_rate = rte_le_to_cpu_32(req0->max_tm_rate);
2699 hw->intr.int_ql_max = rte_le_to_cpu_16(req0->intr_ql_max);
2703 hns3_check_dev_specifications(struct hns3_hw *hw)
2705 if (hw->rss_ind_tbl_size == 0 ||
2706 hw->rss_ind_tbl_size > HNS3_RSS_IND_TBL_SIZE_MAX) {
2707 hns3_err(hw, "the size of hash lookup table configured (%u)"
2708 " exceeds the maximum(%u)", hw->rss_ind_tbl_size,
2709 HNS3_RSS_IND_TBL_SIZE_MAX);
2717 hns3_query_dev_specifications(struct hns3_hw *hw)
2719 struct hns3_cmd_desc desc[HNS3_QUERY_DEV_SPECS_BD_NUM];
2723 for (i = 0; i < HNS3_QUERY_DEV_SPECS_BD_NUM - 1; i++) {
2724 hns3_cmd_setup_basic_desc(&desc[i], HNS3_OPC_QUERY_DEV_SPECS,
2726 desc[i].flag |= rte_cpu_to_le_16(HNS3_CMD_FLAG_NEXT);
2728 hns3_cmd_setup_basic_desc(&desc[i], HNS3_OPC_QUERY_DEV_SPECS, true);
2730 ret = hns3_cmd_send(hw, desc, HNS3_QUERY_DEV_SPECS_BD_NUM);
2734 hns3_parse_dev_specifications(hw, desc);
2736 return hns3_check_dev_specifications(hw);
2740 hns3_get_capability(struct hns3_hw *hw)
2742 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
2743 struct rte_pci_device *pci_dev;
2744 struct hns3_pf *pf = &hns->pf;
2745 struct rte_eth_dev *eth_dev;
2750 eth_dev = &rte_eth_devices[hw->data->port_id];
2751 pci_dev = RTE_ETH_DEV_TO_PCI(eth_dev);
2752 device_id = pci_dev->id.device_id;
2754 if (device_id == HNS3_DEV_ID_25GE_RDMA ||
2755 device_id == HNS3_DEV_ID_50GE_RDMA ||
2756 device_id == HNS3_DEV_ID_100G_RDMA_MACSEC ||
2757 device_id == HNS3_DEV_ID_200G_RDMA)
2758 hns3_set_bit(hw->capability, HNS3_DEV_SUPPORT_DCB_B, 1);
2760 /* Get PCI revision id */
2761 ret = rte_pci_read_config(pci_dev, &revision, HNS3_PCI_REVISION_ID_LEN,
2762 HNS3_PCI_REVISION_ID);
2763 if (ret != HNS3_PCI_REVISION_ID_LEN) {
2764 PMD_INIT_LOG(ERR, "failed to read pci revision id, ret = %d",
2768 hw->revision = revision;
2770 if (revision < PCI_REVISION_ID_HIP09_A) {
2771 hns3_set_default_dev_specifications(hw);
2772 hw->intr.mapping_mode = HNS3_INTR_MAPPING_VEC_RSV_ONE;
2773 hw->intr.gl_unit = HNS3_INTR_COALESCE_GL_UINT_2US;
2774 hw->tso_mode = HNS3_TSO_SW_CAL_PSEUDO_H_CSUM;
2775 hw->vlan_mode = HNS3_SW_SHIFT_AND_DISCARD_MODE;
2776 hw->drop_stats_mode = HNS3_PKTS_DROP_STATS_MODE1;
2777 hw->min_tx_pkt_len = HNS3_HIP08_MIN_TX_PKT_LEN;
2778 pf->tqp_config_mode = HNS3_FIXED_MAX_TQP_NUM_MODE;
2779 hw->rss_info.ipv6_sctp_offload_supported = false;
2780 hw->udp_cksum_mode = HNS3_SPECIAL_PORT_SW_CKSUM_MODE;
2781 pf->support_multi_tc_pause = false;
2785 ret = hns3_query_dev_specifications(hw);
2788 "failed to query dev specifications, ret = %d",
2793 hw->intr.mapping_mode = HNS3_INTR_MAPPING_VEC_ALL;
2794 hw->intr.gl_unit = HNS3_INTR_COALESCE_GL_UINT_1US;
2795 hw->tso_mode = HNS3_TSO_HW_CAL_PSEUDO_H_CSUM;
2796 hw->vlan_mode = HNS3_HW_SHIFT_AND_DISCARD_MODE;
2797 hw->drop_stats_mode = HNS3_PKTS_DROP_STATS_MODE2;
2798 hw->min_tx_pkt_len = HNS3_HIP09_MIN_TX_PKT_LEN;
2799 pf->tqp_config_mode = HNS3_FLEX_MAX_TQP_NUM_MODE;
2800 hw->rss_info.ipv6_sctp_offload_supported = true;
2801 hw->udp_cksum_mode = HNS3_SPECIAL_PORT_HW_CKSUM_MODE;
2802 pf->support_multi_tc_pause = true;
2808 hns3_check_media_type(struct hns3_hw *hw, uint8_t media_type)
2812 switch (media_type) {
2813 case HNS3_MEDIA_TYPE_COPPER:
2814 if (!hns3_dev_get_support(hw, COPPER)) {
2816 "Media type is copper, not supported.");
2822 case HNS3_MEDIA_TYPE_FIBER:
2825 case HNS3_MEDIA_TYPE_BACKPLANE:
2826 PMD_INIT_LOG(ERR, "Media type is Backplane, not supported.");
2830 PMD_INIT_LOG(ERR, "Unknown media type = %u!", media_type);
2839 hns3_get_board_configuration(struct hns3_hw *hw)
2841 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
2842 struct hns3_pf *pf = &hns->pf;
2843 struct hns3_cfg cfg;
2846 ret = hns3_get_board_cfg(hw, &cfg);
2848 PMD_INIT_LOG(ERR, "get board config failed %d", ret);
2852 ret = hns3_check_media_type(hw, cfg.media_type);
2856 hw->mac.media_type = cfg.media_type;
2857 hw->rss_size_max = cfg.rss_size_max;
2858 hw->rss_dis_flag = false;
2859 memcpy(hw->mac.mac_addr, cfg.mac_addr, RTE_ETHER_ADDR_LEN);
2860 hw->mac.phy_addr = cfg.phy_addr;
2861 hw->num_tx_desc = cfg.tqp_desc_num;
2862 hw->num_rx_desc = cfg.tqp_desc_num;
2863 hw->dcb_info.num_pg = 1;
2864 hw->dcb_info.hw_pfc_map = 0;
2866 ret = hns3_parse_speed(cfg.default_speed, &hw->mac.link_speed);
2868 PMD_INIT_LOG(ERR, "Get wrong speed %u, ret = %d",
2869 cfg.default_speed, ret);
2873 pf->tc_max = cfg.tc_num;
2874 if (pf->tc_max > HNS3_MAX_TC_NUM || pf->tc_max < 1) {
2875 PMD_INIT_LOG(WARNING,
2876 "Get TC num(%u) from flash, set TC num to 1",
2881 /* Dev does not support DCB */
2882 if (!hns3_dev_get_support(hw, DCB)) {
2886 pf->pfc_max = pf->tc_max;
2888 hw->dcb_info.num_tc = 1;
2889 hw->alloc_rss_size = RTE_MIN(hw->rss_size_max,
2890 hw->tqps_num / hw->dcb_info.num_tc);
2891 hns3_set_bit(hw->hw_tc_map, 0, 1);
2892 pf->tx_sch_mode = HNS3_FLAG_TC_BASE_SCH_MODE;
2894 pf->wanted_umv_size = cfg.umv_space;
2900 hns3_get_configuration(struct hns3_hw *hw)
2904 ret = hns3_query_function_status(hw);
2906 PMD_INIT_LOG(ERR, "Failed to query function status: %d.", ret);
2910 /* Get device capability */
2911 ret = hns3_get_capability(hw);
2913 PMD_INIT_LOG(ERR, "failed to get device capability: %d.", ret);
2917 /* Get pf resource */
2918 ret = hns3_query_pf_resource(hw);
2920 PMD_INIT_LOG(ERR, "Failed to query pf resource: %d", ret);
2924 ret = hns3_get_board_configuration(hw);
2926 PMD_INIT_LOG(ERR, "failed to get board configuration: %d", ret);
2930 ret = hns3_query_dev_fec_info(hw);
2933 "failed to query FEC information, ret = %d", ret);
2939 hns3_map_tqps_to_func(struct hns3_hw *hw, uint16_t func_id, uint16_t tqp_pid,
2940 uint16_t tqp_vid, bool is_pf)
2942 struct hns3_tqp_map_cmd *req;
2943 struct hns3_cmd_desc desc;
2946 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_SET_TQP_MAP, false);
2948 req = (struct hns3_tqp_map_cmd *)desc.data;
2949 req->tqp_id = rte_cpu_to_le_16(tqp_pid);
2950 req->tqp_vf = func_id;
2951 req->tqp_flag = 1 << HNS3_TQP_MAP_EN_B;
2953 req->tqp_flag |= (1 << HNS3_TQP_MAP_TYPE_B);
2954 req->tqp_vid = rte_cpu_to_le_16(tqp_vid);
2956 ret = hns3_cmd_send(hw, &desc, 1);
2958 PMD_INIT_LOG(ERR, "TQP map failed %d", ret);
2964 hns3_map_tqp(struct hns3_hw *hw)
2970 * In current version, VF is not supported when PF is driven by DPDK
2971 * driver, so we assign total tqps_num tqps allocated to this port
2974 for (i = 0; i < hw->total_tqps_num; i++) {
2975 ret = hns3_map_tqps_to_func(hw, HNS3_PF_FUNC_ID, i, i, true);
2984 hns3_cfg_mac_speed_dup_hw(struct hns3_hw *hw, uint32_t speed, uint8_t duplex)
2986 struct hns3_config_mac_speed_dup_cmd *req;
2987 struct hns3_cmd_desc desc;
2990 req = (struct hns3_config_mac_speed_dup_cmd *)desc.data;
2992 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_CONFIG_SPEED_DUP, false);
2994 hns3_set_bit(req->speed_dup, HNS3_CFG_DUPLEX_B, !!duplex ? 1 : 0);
2997 case RTE_ETH_SPEED_NUM_10M:
2998 hns3_set_field(req->speed_dup, HNS3_CFG_SPEED_M,
2999 HNS3_CFG_SPEED_S, HNS3_CFG_SPEED_10M);
3001 case RTE_ETH_SPEED_NUM_100M:
3002 hns3_set_field(req->speed_dup, HNS3_CFG_SPEED_M,
3003 HNS3_CFG_SPEED_S, HNS3_CFG_SPEED_100M);
3005 case RTE_ETH_SPEED_NUM_1G:
3006 hns3_set_field(req->speed_dup, HNS3_CFG_SPEED_M,
3007 HNS3_CFG_SPEED_S, HNS3_CFG_SPEED_1G);
3009 case RTE_ETH_SPEED_NUM_10G:
3010 hns3_set_field(req->speed_dup, HNS3_CFG_SPEED_M,
3011 HNS3_CFG_SPEED_S, HNS3_CFG_SPEED_10G);
3013 case RTE_ETH_SPEED_NUM_25G:
3014 hns3_set_field(req->speed_dup, HNS3_CFG_SPEED_M,
3015 HNS3_CFG_SPEED_S, HNS3_CFG_SPEED_25G);
3017 case RTE_ETH_SPEED_NUM_40G:
3018 hns3_set_field(req->speed_dup, HNS3_CFG_SPEED_M,
3019 HNS3_CFG_SPEED_S, HNS3_CFG_SPEED_40G);
3021 case RTE_ETH_SPEED_NUM_50G:
3022 hns3_set_field(req->speed_dup, HNS3_CFG_SPEED_M,
3023 HNS3_CFG_SPEED_S, HNS3_CFG_SPEED_50G);
3025 case RTE_ETH_SPEED_NUM_100G:
3026 hns3_set_field(req->speed_dup, HNS3_CFG_SPEED_M,
3027 HNS3_CFG_SPEED_S, HNS3_CFG_SPEED_100G);
3029 case RTE_ETH_SPEED_NUM_200G:
3030 hns3_set_field(req->speed_dup, HNS3_CFG_SPEED_M,
3031 HNS3_CFG_SPEED_S, HNS3_CFG_SPEED_200G);
3034 PMD_INIT_LOG(ERR, "invalid speed (%u)", speed);
3038 hns3_set_bit(req->mac_change_fec_en, HNS3_CFG_MAC_SPEED_CHANGE_EN_B, 1);
3040 ret = hns3_cmd_send(hw, &desc, 1);
3042 PMD_INIT_LOG(ERR, "mac speed/duplex config cmd failed %d", ret);
3048 hns3_tx_buffer_calc(struct hns3_hw *hw, struct hns3_pkt_buf_alloc *buf_alloc)
3050 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
3051 struct hns3_pf *pf = &hns->pf;
3052 struct hns3_priv_buf *priv;
3053 uint32_t i, total_size;
3055 total_size = pf->pkt_buf_size;
3057 /* alloc tx buffer for all enabled tc */
3058 for (i = 0; i < HNS3_MAX_TC_NUM; i++) {
3059 priv = &buf_alloc->priv_buf[i];
3061 if (hw->hw_tc_map & BIT(i)) {
3062 if (total_size < pf->tx_buf_size)
3065 priv->tx_buf_size = pf->tx_buf_size;
3067 priv->tx_buf_size = 0;
3069 total_size -= priv->tx_buf_size;
3076 hns3_tx_buffer_alloc(struct hns3_hw *hw, struct hns3_pkt_buf_alloc *buf_alloc)
3078 /* TX buffer size is unit by 128 byte */
3079 #define HNS3_BUF_SIZE_UNIT_SHIFT 7
3080 #define HNS3_BUF_SIZE_UPDATE_EN_MSK BIT(15)
3081 struct hns3_tx_buff_alloc_cmd *req;
3082 struct hns3_cmd_desc desc;
3087 req = (struct hns3_tx_buff_alloc_cmd *)desc.data;
3089 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_TX_BUFF_ALLOC, 0);
3090 for (i = 0; i < HNS3_MAX_TC_NUM; i++) {
3091 buf_size = buf_alloc->priv_buf[i].tx_buf_size;
3093 buf_size = buf_size >> HNS3_BUF_SIZE_UNIT_SHIFT;
3094 req->tx_pkt_buff[i] = rte_cpu_to_le_16(buf_size |
3095 HNS3_BUF_SIZE_UPDATE_EN_MSK);
3098 ret = hns3_cmd_send(hw, &desc, 1);
3100 PMD_INIT_LOG(ERR, "tx buffer alloc cmd failed %d", ret);
3106 hns3_get_tc_num(struct hns3_hw *hw)
3111 for (i = 0; i < HNS3_MAX_TC_NUM; i++)
3112 if (hw->hw_tc_map & BIT(i))
3118 hns3_get_rx_priv_buff_alloced(struct hns3_pkt_buf_alloc *buf_alloc)
3120 struct hns3_priv_buf *priv;
3121 uint32_t rx_priv = 0;
3124 for (i = 0; i < HNS3_MAX_TC_NUM; i++) {
3125 priv = &buf_alloc->priv_buf[i];
3127 rx_priv += priv->buf_size;
3133 hns3_get_tx_buff_alloced(struct hns3_pkt_buf_alloc *buf_alloc)
3135 uint32_t total_tx_size = 0;
3138 for (i = 0; i < HNS3_MAX_TC_NUM; i++)
3139 total_tx_size += buf_alloc->priv_buf[i].tx_buf_size;
3141 return total_tx_size;
3144 /* Get the number of pfc enabled TCs, which have private buffer */
3146 hns3_get_pfc_priv_num(struct hns3_hw *hw, struct hns3_pkt_buf_alloc *buf_alloc)
3148 struct hns3_priv_buf *priv;
3152 for (i = 0; i < HNS3_MAX_TC_NUM; i++) {
3153 priv = &buf_alloc->priv_buf[i];
3154 if ((hw->dcb_info.hw_pfc_map & BIT(i)) && priv->enable)
3161 /* Get the number of pfc disabled TCs, which have private buffer */
3163 hns3_get_no_pfc_priv_num(struct hns3_hw *hw,
3164 struct hns3_pkt_buf_alloc *buf_alloc)
3166 struct hns3_priv_buf *priv;
3170 for (i = 0; i < HNS3_MAX_TC_NUM; i++) {
3171 priv = &buf_alloc->priv_buf[i];
3172 if (hw->hw_tc_map & BIT(i) &&
3173 !(hw->dcb_info.hw_pfc_map & BIT(i)) && priv->enable)
3181 hns3_is_rx_buf_ok(struct hns3_hw *hw, struct hns3_pkt_buf_alloc *buf_alloc,
3184 uint32_t shared_buf_min, shared_buf_tc, shared_std, hi_thrd, lo_thrd;
3185 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
3186 struct hns3_pf *pf = &hns->pf;
3187 uint32_t shared_buf, aligned_mps;
3192 tc_num = hns3_get_tc_num(hw);
3193 aligned_mps = roundup(pf->mps, HNS3_BUF_SIZE_UNIT);
3195 if (hns3_dev_get_support(hw, DCB))
3196 shared_buf_min = HNS3_BUF_MUL_BY * aligned_mps +
3199 shared_buf_min = aligned_mps + HNS3_NON_DCB_ADDITIONAL_BUF
3202 shared_buf_tc = tc_num * aligned_mps + aligned_mps;
3203 shared_std = roundup(RTE_MAX(shared_buf_min, shared_buf_tc),
3204 HNS3_BUF_SIZE_UNIT);
3206 rx_priv = hns3_get_rx_priv_buff_alloced(buf_alloc);
3207 if (rx_all < rx_priv + shared_std)
3210 shared_buf = rounddown(rx_all - rx_priv, HNS3_BUF_SIZE_UNIT);
3211 buf_alloc->s_buf.buf_size = shared_buf;
3212 if (hns3_dev_get_support(hw, DCB)) {
3213 buf_alloc->s_buf.self.high = shared_buf - pf->dv_buf_size;
3214 buf_alloc->s_buf.self.low = buf_alloc->s_buf.self.high
3215 - roundup(aligned_mps / HNS3_BUF_DIV_BY,
3216 HNS3_BUF_SIZE_UNIT);
3218 buf_alloc->s_buf.self.high =
3219 aligned_mps + HNS3_NON_DCB_ADDITIONAL_BUF;
3220 buf_alloc->s_buf.self.low = aligned_mps;
3223 if (hns3_dev_get_support(hw, DCB)) {
3224 hi_thrd = shared_buf - pf->dv_buf_size;
3226 if (tc_num <= NEED_RESERVE_TC_NUM)
3227 hi_thrd = hi_thrd * BUF_RESERVE_PERCENT /
3231 hi_thrd = hi_thrd / tc_num;
3233 hi_thrd = RTE_MAX(hi_thrd, HNS3_BUF_MUL_BY * aligned_mps);
3234 hi_thrd = rounddown(hi_thrd, HNS3_BUF_SIZE_UNIT);
3235 lo_thrd = hi_thrd - aligned_mps / HNS3_BUF_DIV_BY;
3237 hi_thrd = aligned_mps + HNS3_NON_DCB_ADDITIONAL_BUF;
3238 lo_thrd = aligned_mps;
3241 for (i = 0; i < HNS3_MAX_TC_NUM; i++) {
3242 buf_alloc->s_buf.tc_thrd[i].low = lo_thrd;
3243 buf_alloc->s_buf.tc_thrd[i].high = hi_thrd;
3250 hns3_rx_buf_calc_all(struct hns3_hw *hw, bool max,
3251 struct hns3_pkt_buf_alloc *buf_alloc)
3253 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
3254 struct hns3_pf *pf = &hns->pf;
3255 struct hns3_priv_buf *priv;
3256 uint32_t aligned_mps;
3260 rx_all = pf->pkt_buf_size - hns3_get_tx_buff_alloced(buf_alloc);
3261 aligned_mps = roundup(pf->mps, HNS3_BUF_SIZE_UNIT);
3263 for (i = 0; i < HNS3_MAX_TC_NUM; i++) {
3264 priv = &buf_alloc->priv_buf[i];
3271 if (!(hw->hw_tc_map & BIT(i)))
3275 if (hw->dcb_info.hw_pfc_map & BIT(i)) {
3276 priv->wl.low = max ? aligned_mps : HNS3_BUF_SIZE_UNIT;
3277 priv->wl.high = roundup(priv->wl.low + aligned_mps,
3278 HNS3_BUF_SIZE_UNIT);
3281 priv->wl.high = max ? (aligned_mps * HNS3_BUF_MUL_BY) :
3285 priv->buf_size = priv->wl.high + pf->dv_buf_size;
3288 return hns3_is_rx_buf_ok(hw, buf_alloc, rx_all);
3292 hns3_drop_nopfc_buf_till_fit(struct hns3_hw *hw,
3293 struct hns3_pkt_buf_alloc *buf_alloc)
3295 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
3296 struct hns3_pf *pf = &hns->pf;
3297 struct hns3_priv_buf *priv;
3298 int no_pfc_priv_num;
3303 rx_all = pf->pkt_buf_size - hns3_get_tx_buff_alloced(buf_alloc);
3304 no_pfc_priv_num = hns3_get_no_pfc_priv_num(hw, buf_alloc);
3306 /* let the last to be cleared first */
3307 for (i = HNS3_MAX_TC_NUM - 1; i >= 0; i--) {
3308 priv = &buf_alloc->priv_buf[i];
3309 mask = BIT((uint8_t)i);
3310 if (hw->hw_tc_map & mask &&
3311 !(hw->dcb_info.hw_pfc_map & mask)) {
3312 /* Clear the no pfc TC private buffer */
3320 if (hns3_is_rx_buf_ok(hw, buf_alloc, rx_all) ||
3321 no_pfc_priv_num == 0)
3325 return hns3_is_rx_buf_ok(hw, buf_alloc, rx_all);
3329 hns3_drop_pfc_buf_till_fit(struct hns3_hw *hw,
3330 struct hns3_pkt_buf_alloc *buf_alloc)
3332 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
3333 struct hns3_pf *pf = &hns->pf;
3334 struct hns3_priv_buf *priv;
3340 rx_all = pf->pkt_buf_size - hns3_get_tx_buff_alloced(buf_alloc);
3341 pfc_priv_num = hns3_get_pfc_priv_num(hw, buf_alloc);
3343 /* let the last to be cleared first */
3344 for (i = HNS3_MAX_TC_NUM - 1; i >= 0; i--) {
3345 priv = &buf_alloc->priv_buf[i];
3346 mask = BIT((uint8_t)i);
3347 if (hw->hw_tc_map & mask && hw->dcb_info.hw_pfc_map & mask) {
3348 /* Reduce the number of pfc TC with private buffer */
3355 if (hns3_is_rx_buf_ok(hw, buf_alloc, rx_all) ||
3360 return hns3_is_rx_buf_ok(hw, buf_alloc, rx_all);
3364 hns3_only_alloc_priv_buff(struct hns3_hw *hw,
3365 struct hns3_pkt_buf_alloc *buf_alloc)
3367 #define COMPENSATE_BUFFER 0x3C00
3368 #define COMPENSATE_HALF_MPS_NUM 5
3369 #define PRIV_WL_GAP 0x1800
3370 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
3371 struct hns3_pf *pf = &hns->pf;
3372 uint32_t tc_num = hns3_get_tc_num(hw);
3373 uint32_t half_mps = pf->mps >> 1;
3374 struct hns3_priv_buf *priv;
3375 uint32_t min_rx_priv;
3379 rx_priv = pf->pkt_buf_size - hns3_get_tx_buff_alloced(buf_alloc);
3381 rx_priv = rx_priv / tc_num;
3383 if (tc_num <= NEED_RESERVE_TC_NUM)
3384 rx_priv = rx_priv * BUF_RESERVE_PERCENT / BUF_MAX_PERCENT;
3387 * Minimum value of private buffer in rx direction (min_rx_priv) is
3388 * equal to "DV + 2.5 * MPS + 15KB". Driver only allocates rx private
3389 * buffer if rx_priv is greater than min_rx_priv.
3391 min_rx_priv = pf->dv_buf_size + COMPENSATE_BUFFER +
3392 COMPENSATE_HALF_MPS_NUM * half_mps;
3393 min_rx_priv = roundup(min_rx_priv, HNS3_BUF_SIZE_UNIT);
3394 rx_priv = rounddown(rx_priv, HNS3_BUF_SIZE_UNIT);
3395 if (rx_priv < min_rx_priv)
3398 for (i = 0; i < HNS3_MAX_TC_NUM; i++) {
3399 priv = &buf_alloc->priv_buf[i];
3405 if (!(hw->hw_tc_map & BIT(i)))
3409 priv->buf_size = rx_priv;
3410 priv->wl.high = rx_priv - pf->dv_buf_size;
3411 priv->wl.low = priv->wl.high - PRIV_WL_GAP;
3414 buf_alloc->s_buf.buf_size = 0;
3420 * hns3_rx_buffer_calc: calculate the rx private buffer size for all TCs
3421 * @hw: pointer to struct hns3_hw
3422 * @buf_alloc: pointer to buffer calculation data
3423 * @return: 0: calculate sucessful, negative: fail
3426 hns3_rx_buffer_calc(struct hns3_hw *hw, struct hns3_pkt_buf_alloc *buf_alloc)
3428 /* When DCB is not supported, rx private buffer is not allocated. */
3429 if (!hns3_dev_get_support(hw, DCB)) {
3430 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
3431 struct hns3_pf *pf = &hns->pf;
3432 uint32_t rx_all = pf->pkt_buf_size;
3434 rx_all -= hns3_get_tx_buff_alloced(buf_alloc);
3435 if (!hns3_is_rx_buf_ok(hw, buf_alloc, rx_all))
3442 * Try to allocate privated packet buffer for all TCs without share
3445 if (hns3_only_alloc_priv_buff(hw, buf_alloc))
3449 * Try to allocate privated packet buffer for all TCs with share
3452 if (hns3_rx_buf_calc_all(hw, true, buf_alloc))
3456 * For different application scenes, the enabled port number, TC number
3457 * and no_drop TC number are different. In order to obtain the better
3458 * performance, software could allocate the buffer size and configure
3459 * the waterline by trying to decrease the private buffer size according
3460 * to the order, namely, waterline of valid tc, pfc disabled tc, pfc
3463 if (hns3_rx_buf_calc_all(hw, false, buf_alloc))
3466 if (hns3_drop_nopfc_buf_till_fit(hw, buf_alloc))
3469 if (hns3_drop_pfc_buf_till_fit(hw, buf_alloc))
3476 hns3_rx_priv_buf_alloc(struct hns3_hw *hw, struct hns3_pkt_buf_alloc *buf_alloc)
3478 struct hns3_rx_priv_buff_cmd *req;
3479 struct hns3_cmd_desc desc;
3484 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_RX_PRIV_BUFF_ALLOC, false);
3485 req = (struct hns3_rx_priv_buff_cmd *)desc.data;
3487 /* Alloc private buffer TCs */
3488 for (i = 0; i < HNS3_MAX_TC_NUM; i++) {
3489 struct hns3_priv_buf *priv = &buf_alloc->priv_buf[i];
3492 rte_cpu_to_le_16(priv->buf_size >> HNS3_BUF_UNIT_S);
3493 req->buf_num[i] |= rte_cpu_to_le_16(1 << HNS3_TC0_PRI_BUF_EN_B);
3496 buf_size = buf_alloc->s_buf.buf_size;
3497 req->shared_buf = rte_cpu_to_le_16((buf_size >> HNS3_BUF_UNIT_S) |
3498 (1 << HNS3_TC0_PRI_BUF_EN_B));
3500 ret = hns3_cmd_send(hw, &desc, 1);
3502 PMD_INIT_LOG(ERR, "rx private buffer alloc cmd failed %d", ret);
3508 hns3_rx_priv_wl_config(struct hns3_hw *hw, struct hns3_pkt_buf_alloc *buf_alloc)
3510 #define HNS3_RX_PRIV_WL_ALLOC_DESC_NUM 2
3511 struct hns3_rx_priv_wl_buf *req;
3512 struct hns3_priv_buf *priv;
3513 struct hns3_cmd_desc desc[HNS3_RX_PRIV_WL_ALLOC_DESC_NUM];
3517 for (i = 0; i < HNS3_RX_PRIV_WL_ALLOC_DESC_NUM; i++) {
3518 hns3_cmd_setup_basic_desc(&desc[i], HNS3_OPC_RX_PRIV_WL_ALLOC,
3520 req = (struct hns3_rx_priv_wl_buf *)desc[i].data;
3522 /* The first descriptor set the NEXT bit to 1 */
3524 desc[i].flag |= rte_cpu_to_le_16(HNS3_CMD_FLAG_NEXT);
3526 desc[i].flag &= ~rte_cpu_to_le_16(HNS3_CMD_FLAG_NEXT);
3528 for (j = 0; j < HNS3_TC_NUM_ONE_DESC; j++) {
3529 uint32_t idx = i * HNS3_TC_NUM_ONE_DESC + j;
3531 priv = &buf_alloc->priv_buf[idx];
3532 req->tc_wl[j].high = rte_cpu_to_le_16(priv->wl.high >>
3534 req->tc_wl[j].high |=
3535 rte_cpu_to_le_16(BIT(HNS3_RX_PRIV_EN_B));
3536 req->tc_wl[j].low = rte_cpu_to_le_16(priv->wl.low >>
3538 req->tc_wl[j].low |=
3539 rte_cpu_to_le_16(BIT(HNS3_RX_PRIV_EN_B));
3543 /* Send 2 descriptor at one time */
3544 ret = hns3_cmd_send(hw, desc, HNS3_RX_PRIV_WL_ALLOC_DESC_NUM);
3546 PMD_INIT_LOG(ERR, "rx private waterline config cmd failed %d",
3552 hns3_common_thrd_config(struct hns3_hw *hw,
3553 struct hns3_pkt_buf_alloc *buf_alloc)
3555 #define HNS3_RX_COM_THRD_ALLOC_DESC_NUM 2
3556 struct hns3_shared_buf *s_buf = &buf_alloc->s_buf;
3557 struct hns3_rx_com_thrd *req;
3558 struct hns3_cmd_desc desc[HNS3_RX_COM_THRD_ALLOC_DESC_NUM];
3559 struct hns3_tc_thrd *tc;
3564 for (i = 0; i < HNS3_RX_COM_THRD_ALLOC_DESC_NUM; i++) {
3565 hns3_cmd_setup_basic_desc(&desc[i], HNS3_OPC_RX_COM_THRD_ALLOC,
3567 req = (struct hns3_rx_com_thrd *)&desc[i].data;
3569 /* The first descriptor set the NEXT bit to 1 */
3571 desc[i].flag |= rte_cpu_to_le_16(HNS3_CMD_FLAG_NEXT);
3573 desc[i].flag &= ~rte_cpu_to_le_16(HNS3_CMD_FLAG_NEXT);
3575 for (j = 0; j < HNS3_TC_NUM_ONE_DESC; j++) {
3576 tc_idx = i * HNS3_TC_NUM_ONE_DESC + j;
3577 tc = &s_buf->tc_thrd[tc_idx];
3579 req->com_thrd[j].high =
3580 rte_cpu_to_le_16(tc->high >> HNS3_BUF_UNIT_S);
3581 req->com_thrd[j].high |=
3582 rte_cpu_to_le_16(BIT(HNS3_RX_PRIV_EN_B));
3583 req->com_thrd[j].low =
3584 rte_cpu_to_le_16(tc->low >> HNS3_BUF_UNIT_S);
3585 req->com_thrd[j].low |=
3586 rte_cpu_to_le_16(BIT(HNS3_RX_PRIV_EN_B));
3590 /* Send 2 descriptors at one time */
3591 ret = hns3_cmd_send(hw, desc, HNS3_RX_COM_THRD_ALLOC_DESC_NUM);
3593 PMD_INIT_LOG(ERR, "common threshold config cmd failed %d", ret);
3599 hns3_common_wl_config(struct hns3_hw *hw, struct hns3_pkt_buf_alloc *buf_alloc)
3601 struct hns3_shared_buf *buf = &buf_alloc->s_buf;
3602 struct hns3_rx_com_wl *req;
3603 struct hns3_cmd_desc desc;
3606 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_RX_COM_WL_ALLOC, false);
3608 req = (struct hns3_rx_com_wl *)desc.data;
3609 req->com_wl.high = rte_cpu_to_le_16(buf->self.high >> HNS3_BUF_UNIT_S);
3610 req->com_wl.high |= rte_cpu_to_le_16(BIT(HNS3_RX_PRIV_EN_B));
3612 req->com_wl.low = rte_cpu_to_le_16(buf->self.low >> HNS3_BUF_UNIT_S);
3613 req->com_wl.low |= rte_cpu_to_le_16(BIT(HNS3_RX_PRIV_EN_B));
3615 ret = hns3_cmd_send(hw, &desc, 1);
3617 PMD_INIT_LOG(ERR, "common waterline config cmd failed %d", ret);
3623 hns3_buffer_alloc(struct hns3_hw *hw)
3625 struct hns3_pkt_buf_alloc pkt_buf;
3628 memset(&pkt_buf, 0, sizeof(pkt_buf));
3629 ret = hns3_tx_buffer_calc(hw, &pkt_buf);
3632 "could not calc tx buffer size for all TCs %d",
3637 ret = hns3_tx_buffer_alloc(hw, &pkt_buf);
3639 PMD_INIT_LOG(ERR, "could not alloc tx buffers %d", ret);
3643 ret = hns3_rx_buffer_calc(hw, &pkt_buf);
3646 "could not calc rx priv buffer size for all TCs %d",
3651 ret = hns3_rx_priv_buf_alloc(hw, &pkt_buf);
3653 PMD_INIT_LOG(ERR, "could not alloc rx priv buffer %d", ret);
3657 if (hns3_dev_get_support(hw, DCB)) {
3658 ret = hns3_rx_priv_wl_config(hw, &pkt_buf);
3661 "could not configure rx private waterline %d",
3666 ret = hns3_common_thrd_config(hw, &pkt_buf);
3669 "could not configure common threshold %d",
3675 ret = hns3_common_wl_config(hw, &pkt_buf);
3677 PMD_INIT_LOG(ERR, "could not configure common waterline %d",
3684 hns3_mac_init(struct hns3_hw *hw)
3686 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
3687 struct hns3_mac *mac = &hw->mac;
3688 struct hns3_pf *pf = &hns->pf;
3691 pf->support_sfp_query = true;
3692 mac->link_duplex = RTE_ETH_LINK_FULL_DUPLEX;
3693 ret = hns3_cfg_mac_speed_dup_hw(hw, mac->link_speed, mac->link_duplex);
3695 PMD_INIT_LOG(ERR, "Config mac speed dup fail ret = %d", ret);
3699 mac->link_status = RTE_ETH_LINK_DOWN;
3701 return hns3_config_mtu(hw, pf->mps);
3705 hns3_get_mac_ethertype_cmd_status(uint16_t cmdq_resp, uint8_t resp_code)
3707 #define HNS3_ETHERTYPE_SUCCESS_ADD 0
3708 #define HNS3_ETHERTYPE_ALREADY_ADD 1
3709 #define HNS3_ETHERTYPE_MGR_TBL_OVERFLOW 2
3710 #define HNS3_ETHERTYPE_KEY_CONFLICT 3
3715 "cmdq execute failed for get_mac_ethertype_cmd_status, status=%u.\n",
3720 switch (resp_code) {
3721 case HNS3_ETHERTYPE_SUCCESS_ADD:
3722 case HNS3_ETHERTYPE_ALREADY_ADD:
3725 case HNS3_ETHERTYPE_MGR_TBL_OVERFLOW:
3727 "add mac ethertype failed for manager table overflow.");
3728 return_status = -EIO;
3730 case HNS3_ETHERTYPE_KEY_CONFLICT:
3731 PMD_INIT_LOG(ERR, "add mac ethertype failed for key conflict.");
3732 return_status = -EIO;
3736 "add mac ethertype failed for undefined, code=%u.",
3738 return_status = -EIO;
3742 return return_status;
3746 hns3_add_mgr_tbl(struct hns3_hw *hw,
3747 const struct hns3_mac_mgr_tbl_entry_cmd *req)
3749 struct hns3_cmd_desc desc;
3754 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_MAC_ETHTYPE_ADD, false);
3755 memcpy(desc.data, req, sizeof(struct hns3_mac_mgr_tbl_entry_cmd));
3757 ret = hns3_cmd_send(hw, &desc, 1);
3760 "add mac ethertype failed for cmd_send, ret =%d.",
3765 resp_code = (rte_le_to_cpu_32(desc.data[0]) >> 8) & 0xff;
3766 retval = rte_le_to_cpu_16(desc.retval);
3768 return hns3_get_mac_ethertype_cmd_status(retval, resp_code);
3772 hns3_prepare_mgr_tbl(struct hns3_mac_mgr_tbl_entry_cmd *mgr_table,
3773 int *table_item_num)
3775 struct hns3_mac_mgr_tbl_entry_cmd *tbl;
3778 * In current version, we add one item in management table as below:
3779 * 0x0180C200000E -- LLDP MC address
3782 tbl->flags = HNS3_MAC_MGR_MASK_VLAN_B;
3783 tbl->ethter_type = rte_cpu_to_le_16(HNS3_MAC_ETHERTYPE_LLDP);
3784 tbl->mac_addr_hi32 = rte_cpu_to_le_32(htonl(0x0180C200));
3785 tbl->mac_addr_lo16 = rte_cpu_to_le_16(htons(0x000E));
3786 tbl->i_port_bitmap = 0x1;
3787 *table_item_num = 1;
3791 hns3_init_mgr_tbl(struct hns3_hw *hw)
3793 #define HNS_MAC_MGR_TBL_MAX_SIZE 16
3794 struct hns3_mac_mgr_tbl_entry_cmd mgr_table[HNS_MAC_MGR_TBL_MAX_SIZE];
3799 memset(mgr_table, 0, sizeof(mgr_table));
3800 hns3_prepare_mgr_tbl(mgr_table, &table_item_num);
3801 for (i = 0; i < table_item_num; i++) {
3802 ret = hns3_add_mgr_tbl(hw, &mgr_table[i]);
3804 PMD_INIT_LOG(ERR, "add mac ethertype failed, ret =%d",
3814 hns3_promisc_param_init(struct hns3_promisc_param *param, bool en_uc,
3815 bool en_mc, bool en_bc, int vport_id)
3820 memset(param, 0, sizeof(struct hns3_promisc_param));
3822 param->enable = HNS3_PROMISC_EN_UC;
3824 param->enable |= HNS3_PROMISC_EN_MC;
3826 param->enable |= HNS3_PROMISC_EN_BC;
3827 param->vf_id = vport_id;
3831 hns3_cmd_set_promisc_mode(struct hns3_hw *hw, struct hns3_promisc_param *param)
3833 struct hns3_promisc_cfg_cmd *req;
3834 struct hns3_cmd_desc desc;
3837 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_CFG_PROMISC_MODE, false);
3839 req = (struct hns3_promisc_cfg_cmd *)desc.data;
3840 req->vf_id = param->vf_id;
3841 req->flag = (param->enable << HNS3_PROMISC_EN_B) |
3842 HNS3_PROMISC_TX_EN_B | HNS3_PROMISC_RX_EN_B;
3844 ret = hns3_cmd_send(hw, &desc, 1);
3846 PMD_INIT_LOG(ERR, "Set promisc mode fail, ret = %d", ret);
3852 hns3_set_promisc_mode(struct hns3_hw *hw, bool en_uc_pmc, bool en_mc_pmc)
3854 struct hns3_promisc_param param;
3855 bool en_bc_pmc = true;
3859 * In current version VF is not supported when PF is driven by DPDK
3860 * driver, just need to configure parameters for PF vport.
3862 vf_id = HNS3_PF_FUNC_ID;
3864 hns3_promisc_param_init(¶m, en_uc_pmc, en_mc_pmc, en_bc_pmc, vf_id);
3865 return hns3_cmd_set_promisc_mode(hw, ¶m);
3869 hns3_promisc_init(struct hns3_hw *hw)
3871 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
3872 struct hns3_pf *pf = &hns->pf;
3873 struct hns3_promisc_param param;
3877 ret = hns3_set_promisc_mode(hw, false, false);
3879 PMD_INIT_LOG(ERR, "failed to set promisc mode, ret = %d", ret);
3884 * In current version VFs are not supported when PF is driven by DPDK
3885 * driver. After PF has been taken over by DPDK, the original VF will
3886 * be invalid. So, there is a possibility of entry residues. It should
3887 * clear VFs's promisc mode to avoid unnecessary bandwidth usage
3890 for (func_id = HNS3_1ST_VF_FUNC_ID; func_id < pf->func_num; func_id++) {
3891 hns3_promisc_param_init(¶m, false, false, false, func_id);
3892 ret = hns3_cmd_set_promisc_mode(hw, ¶m);
3894 PMD_INIT_LOG(ERR, "failed to clear vf:%u promisc mode,"
3895 " ret = %d", func_id, ret);
3904 hns3_promisc_uninit(struct hns3_hw *hw)
3906 struct hns3_promisc_param param;
3910 func_id = HNS3_PF_FUNC_ID;
3913 * In current version VFs are not supported when PF is driven by
3914 * DPDK driver, and VFs' promisc mode status has been cleared during
3915 * init and their status will not change. So just clear PF's promisc
3916 * mode status during uninit.
3918 hns3_promisc_param_init(¶m, false, false, false, func_id);
3919 ret = hns3_cmd_set_promisc_mode(hw, ¶m);
3921 PMD_INIT_LOG(ERR, "failed to clear promisc status during"
3922 " uninit, ret = %d", ret);
3926 hns3_dev_promiscuous_enable(struct rte_eth_dev *dev)
3928 bool allmulti = dev->data->all_multicast ? true : false;
3929 struct hns3_adapter *hns = dev->data->dev_private;
3930 struct hns3_hw *hw = &hns->hw;
3935 rte_spinlock_lock(&hw->lock);
3936 ret = hns3_set_promisc_mode(hw, true, true);
3938 rte_spinlock_unlock(&hw->lock);
3939 hns3_err(hw, "failed to enable promiscuous mode, ret = %d",
3945 * When promiscuous mode was enabled, disable the vlan filter to let
3946 * all packets coming in in the receiving direction.
3948 offloads = dev->data->dev_conf.rxmode.offloads;
3949 if (offloads & RTE_ETH_RX_OFFLOAD_VLAN_FILTER) {
3950 ret = hns3_enable_vlan_filter(hns, false);
3952 hns3_err(hw, "failed to enable promiscuous mode due to "
3953 "failure to disable vlan filter, ret = %d",
3955 err = hns3_set_promisc_mode(hw, false, allmulti);
3957 hns3_err(hw, "failed to restore promiscuous "
3958 "status after disable vlan filter "
3959 "failed during enabling promiscuous "
3960 "mode, ret = %d", ret);
3964 rte_spinlock_unlock(&hw->lock);
3970 hns3_dev_promiscuous_disable(struct rte_eth_dev *dev)
3972 bool allmulti = dev->data->all_multicast ? true : false;
3973 struct hns3_adapter *hns = dev->data->dev_private;
3974 struct hns3_hw *hw = &hns->hw;
3979 /* If now in all_multicast mode, must remain in all_multicast mode. */
3980 rte_spinlock_lock(&hw->lock);
3981 ret = hns3_set_promisc_mode(hw, false, allmulti);
3983 rte_spinlock_unlock(&hw->lock);
3984 hns3_err(hw, "failed to disable promiscuous mode, ret = %d",
3988 /* when promiscuous mode was disabled, restore the vlan filter status */
3989 offloads = dev->data->dev_conf.rxmode.offloads;
3990 if (offloads & RTE_ETH_RX_OFFLOAD_VLAN_FILTER) {
3991 ret = hns3_enable_vlan_filter(hns, true);
3993 hns3_err(hw, "failed to disable promiscuous mode due to"
3994 " failure to restore vlan filter, ret = %d",
3996 err = hns3_set_promisc_mode(hw, true, true);
3998 hns3_err(hw, "failed to restore promiscuous "
3999 "status after enabling vlan filter "
4000 "failed during disabling promiscuous "
4001 "mode, ret = %d", ret);
4004 rte_spinlock_unlock(&hw->lock);
4010 hns3_dev_allmulticast_enable(struct rte_eth_dev *dev)
4012 struct hns3_adapter *hns = dev->data->dev_private;
4013 struct hns3_hw *hw = &hns->hw;
4016 if (dev->data->promiscuous)
4019 rte_spinlock_lock(&hw->lock);
4020 ret = hns3_set_promisc_mode(hw, false, true);
4021 rte_spinlock_unlock(&hw->lock);
4023 hns3_err(hw, "failed to enable allmulticast mode, ret = %d",
4030 hns3_dev_allmulticast_disable(struct rte_eth_dev *dev)
4032 struct hns3_adapter *hns = dev->data->dev_private;
4033 struct hns3_hw *hw = &hns->hw;
4036 /* If now in promiscuous mode, must remain in all_multicast mode. */
4037 if (dev->data->promiscuous)
4040 rte_spinlock_lock(&hw->lock);
4041 ret = hns3_set_promisc_mode(hw, false, false);
4042 rte_spinlock_unlock(&hw->lock);
4044 hns3_err(hw, "failed to disable allmulticast mode, ret = %d",
4051 hns3_dev_promisc_restore(struct hns3_adapter *hns)
4053 struct hns3_hw *hw = &hns->hw;
4054 bool allmulti = hw->data->all_multicast ? true : false;
4057 if (hw->data->promiscuous) {
4058 ret = hns3_set_promisc_mode(hw, true, true);
4060 hns3_err(hw, "failed to restore promiscuous mode, "
4065 ret = hns3_set_promisc_mode(hw, false, allmulti);
4067 hns3_err(hw, "failed to restore allmulticast mode, ret = %d",
4073 hns3_get_sfp_info(struct hns3_hw *hw, struct hns3_mac *mac_info)
4075 struct hns3_sfp_info_cmd *resp;
4076 struct hns3_cmd_desc desc;
4079 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_GET_SFP_INFO, true);
4080 resp = (struct hns3_sfp_info_cmd *)desc.data;
4081 resp->query_type = HNS3_ACTIVE_QUERY;
4083 ret = hns3_cmd_send(hw, &desc, 1);
4084 if (ret == -EOPNOTSUPP) {
4085 hns3_warn(hw, "firmware does not support get SFP info,"
4089 hns3_err(hw, "get sfp info failed, ret = %d.", ret);
4094 * In some case, the speed of MAC obtained from firmware may be 0, it
4095 * shouldn't be set to mac->speed.
4097 if (!rte_le_to_cpu_32(resp->sfp_speed))
4100 mac_info->link_speed = rte_le_to_cpu_32(resp->sfp_speed);
4102 * if resp->supported_speed is 0, it means it's an old version
4103 * firmware, do not update these params.
4105 if (resp->supported_speed) {
4106 mac_info->query_type = HNS3_ACTIVE_QUERY;
4107 mac_info->supported_speed =
4108 rte_le_to_cpu_32(resp->supported_speed);
4109 mac_info->support_autoneg = resp->autoneg_ability;
4110 mac_info->link_autoneg = (resp->autoneg == 0) ? RTE_ETH_LINK_FIXED
4111 : RTE_ETH_LINK_AUTONEG;
4113 mac_info->query_type = HNS3_DEFAULT_QUERY;
4120 hns3_check_speed_dup(uint8_t duplex, uint32_t speed)
4122 if (!(speed == RTE_ETH_SPEED_NUM_10M || speed == RTE_ETH_SPEED_NUM_100M))
4123 duplex = RTE_ETH_LINK_FULL_DUPLEX;
4129 hns3_cfg_mac_speed_dup(struct hns3_hw *hw, uint32_t speed, uint8_t duplex)
4131 struct hns3_mac *mac = &hw->mac;
4134 duplex = hns3_check_speed_dup(duplex, speed);
4135 if (mac->link_speed == speed && mac->link_duplex == duplex)
4138 ret = hns3_cfg_mac_speed_dup_hw(hw, speed, duplex);
4142 ret = hns3_port_shaper_update(hw, speed);
4146 mac->link_speed = speed;
4147 mac->link_duplex = duplex;
4153 hns3_update_fiber_link_info(struct hns3_hw *hw)
4155 struct hns3_pf *pf = HNS3_DEV_HW_TO_PF(hw);
4156 struct hns3_mac *mac = &hw->mac;
4157 struct hns3_mac mac_info;
4160 /* If firmware do not support get SFP/qSFP speed, return directly */
4161 if (!pf->support_sfp_query)
4164 memset(&mac_info, 0, sizeof(struct hns3_mac));
4165 ret = hns3_get_sfp_info(hw, &mac_info);
4166 if (ret == -EOPNOTSUPP) {
4167 pf->support_sfp_query = false;
4172 /* Do nothing if no SFP */
4173 if (mac_info.link_speed == RTE_ETH_SPEED_NUM_NONE)
4177 * If query_type is HNS3_ACTIVE_QUERY, it is no need
4178 * to reconfigure the speed of MAC. Otherwise, it indicates
4179 * that the current firmware only supports to obtain the
4180 * speed of the SFP, and the speed of MAC needs to reconfigure.
4182 mac->query_type = mac_info.query_type;
4183 if (mac->query_type == HNS3_ACTIVE_QUERY) {
4184 if (mac_info.link_speed != mac->link_speed) {
4185 ret = hns3_port_shaper_update(hw, mac_info.link_speed);
4190 mac->link_speed = mac_info.link_speed;
4191 mac->supported_speed = mac_info.supported_speed;
4192 mac->support_autoneg = mac_info.support_autoneg;
4193 mac->link_autoneg = mac_info.link_autoneg;
4198 /* Config full duplex for SFP */
4199 return hns3_cfg_mac_speed_dup(hw, mac_info.link_speed,
4200 RTE_ETH_LINK_FULL_DUPLEX);
4204 hns3_parse_copper_phy_params(struct hns3_cmd_desc *desc, struct hns3_mac *mac)
4206 #define HNS3_PHY_SUPPORTED_SPEED_MASK 0x2f
4208 struct hns3_phy_params_bd0_cmd *req;
4211 req = (struct hns3_phy_params_bd0_cmd *)desc[0].data;
4212 mac->link_speed = rte_le_to_cpu_32(req->speed);
4213 mac->link_duplex = hns3_get_bit(req->duplex,
4214 HNS3_PHY_DUPLEX_CFG_B);
4215 mac->link_autoneg = hns3_get_bit(req->autoneg,
4216 HNS3_PHY_AUTONEG_CFG_B);
4217 mac->advertising = rte_le_to_cpu_32(req->advertising);
4218 mac->lp_advertising = rte_le_to_cpu_32(req->lp_advertising);
4219 supported = rte_le_to_cpu_32(req->supported);
4220 mac->supported_speed = supported & HNS3_PHY_SUPPORTED_SPEED_MASK;
4221 mac->support_autoneg = !!(supported & HNS3_PHY_LINK_MODE_AUTONEG_BIT);
4225 hns3_get_copper_phy_params(struct hns3_hw *hw, struct hns3_mac *mac)
4227 struct hns3_cmd_desc desc[HNS3_PHY_PARAM_CFG_BD_NUM];
4231 for (i = 0; i < HNS3_PHY_PARAM_CFG_BD_NUM - 1; i++) {
4232 hns3_cmd_setup_basic_desc(&desc[i], HNS3_OPC_PHY_PARAM_CFG,
4234 desc[i].flag |= rte_cpu_to_le_16(HNS3_CMD_FLAG_NEXT);
4236 hns3_cmd_setup_basic_desc(&desc[i], HNS3_OPC_PHY_PARAM_CFG, true);
4238 ret = hns3_cmd_send(hw, desc, HNS3_PHY_PARAM_CFG_BD_NUM);
4240 hns3_err(hw, "get phy parameters failed, ret = %d.", ret);
4244 hns3_parse_copper_phy_params(desc, mac);
4250 hns3_update_copper_link_info(struct hns3_hw *hw)
4252 struct hns3_mac *mac = &hw->mac;
4253 struct hns3_mac mac_info;
4256 memset(&mac_info, 0, sizeof(struct hns3_mac));
4257 ret = hns3_get_copper_phy_params(hw, &mac_info);
4261 if (mac_info.link_speed != mac->link_speed) {
4262 ret = hns3_port_shaper_update(hw, mac_info.link_speed);
4267 mac->link_speed = mac_info.link_speed;
4268 mac->link_duplex = mac_info.link_duplex;
4269 mac->link_autoneg = mac_info.link_autoneg;
4270 mac->supported_speed = mac_info.supported_speed;
4271 mac->advertising = mac_info.advertising;
4272 mac->lp_advertising = mac_info.lp_advertising;
4273 mac->support_autoneg = mac_info.support_autoneg;
4279 hns3_update_link_info(struct rte_eth_dev *eth_dev)
4281 struct hns3_adapter *hns = eth_dev->data->dev_private;
4282 struct hns3_hw *hw = &hns->hw;
4285 if (hw->mac.media_type == HNS3_MEDIA_TYPE_COPPER)
4286 ret = hns3_update_copper_link_info(hw);
4287 else if (hw->mac.media_type == HNS3_MEDIA_TYPE_FIBER)
4288 ret = hns3_update_fiber_link_info(hw);
4294 hns3_cfg_mac_mode(struct hns3_hw *hw, bool enable)
4296 struct hns3_config_mac_mode_cmd *req;
4297 struct hns3_cmd_desc desc;
4298 uint32_t loop_en = 0;
4302 req = (struct hns3_config_mac_mode_cmd *)desc.data;
4304 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_CONFIG_MAC_MODE, false);
4307 hns3_set_bit(loop_en, HNS3_MAC_TX_EN_B, val);
4308 hns3_set_bit(loop_en, HNS3_MAC_RX_EN_B, val);
4309 hns3_set_bit(loop_en, HNS3_MAC_PAD_TX_B, val);
4310 hns3_set_bit(loop_en, HNS3_MAC_PAD_RX_B, val);
4311 hns3_set_bit(loop_en, HNS3_MAC_1588_TX_B, 0);
4312 hns3_set_bit(loop_en, HNS3_MAC_1588_RX_B, 0);
4313 hns3_set_bit(loop_en, HNS3_MAC_APP_LP_B, 0);
4314 hns3_set_bit(loop_en, HNS3_MAC_LINE_LP_B, 0);
4315 hns3_set_bit(loop_en, HNS3_MAC_FCS_TX_B, val);
4316 hns3_set_bit(loop_en, HNS3_MAC_RX_FCS_B, val);
4319 * If RTE_ETH_RX_OFFLOAD_KEEP_CRC offload is set, MAC will not strip CRC
4320 * when receiving frames. Otherwise, CRC will be stripped.
4322 if (hw->data->dev_conf.rxmode.offloads & RTE_ETH_RX_OFFLOAD_KEEP_CRC)
4323 hns3_set_bit(loop_en, HNS3_MAC_RX_FCS_STRIP_B, 0);
4325 hns3_set_bit(loop_en, HNS3_MAC_RX_FCS_STRIP_B, val);
4326 hns3_set_bit(loop_en, HNS3_MAC_TX_OVERSIZE_TRUNCATE_B, val);
4327 hns3_set_bit(loop_en, HNS3_MAC_RX_OVERSIZE_TRUNCATE_B, val);
4328 hns3_set_bit(loop_en, HNS3_MAC_TX_UNDER_MIN_ERR_B, val);
4329 req->txrx_pad_fcs_loop_en = rte_cpu_to_le_32(loop_en);
4331 ret = hns3_cmd_send(hw, &desc, 1);
4333 PMD_INIT_LOG(ERR, "mac enable fail, ret =%d.", ret);
4339 hns3_get_mac_link_status(struct hns3_hw *hw)
4341 struct hns3_link_status_cmd *req;
4342 struct hns3_cmd_desc desc;
4346 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_QUERY_LINK_STATUS, true);
4347 ret = hns3_cmd_send(hw, &desc, 1);
4349 hns3_err(hw, "get link status cmd failed %d", ret);
4350 return RTE_ETH_LINK_DOWN;
4353 req = (struct hns3_link_status_cmd *)desc.data;
4354 link_status = req->status & HNS3_LINK_STATUS_UP_M;
4356 return !!link_status;
4360 hns3_update_link_status(struct hns3_hw *hw)
4364 state = hns3_get_mac_link_status(hw);
4365 if (state != hw->mac.link_status) {
4366 hw->mac.link_status = state;
4367 hns3_warn(hw, "Link status change to %s!", state ? "up" : "down");
4375 hns3_update_linkstatus_and_event(struct hns3_hw *hw, bool query)
4377 struct rte_eth_dev *dev = &rte_eth_devices[hw->data->port_id];
4378 struct rte_eth_link new_link;
4382 hns3_update_port_link_info(dev);
4384 memset(&new_link, 0, sizeof(new_link));
4385 hns3_setup_linkstatus(dev, &new_link);
4387 ret = rte_eth_linkstatus_set(dev, &new_link);
4388 if (ret == 0 && dev->data->dev_conf.intr_conf.lsc != 0)
4389 hns3_start_report_lse(dev);
4393 hns3_service_handler(void *param)
4395 struct rte_eth_dev *eth_dev = (struct rte_eth_dev *)param;
4396 struct hns3_adapter *hns = eth_dev->data->dev_private;
4397 struct hns3_hw *hw = &hns->hw;
4399 if (!hns3_is_reset_pending(hns))
4400 hns3_update_linkstatus_and_event(hw, true);
4402 hns3_warn(hw, "Cancel the query when reset is pending");
4404 rte_eal_alarm_set(HNS3_SERVICE_INTERVAL, hns3_service_handler, eth_dev);
4408 hns3_init_hardware(struct hns3_adapter *hns)
4410 struct hns3_hw *hw = &hns->hw;
4413 ret = hns3_map_tqp(hw);
4415 PMD_INIT_LOG(ERR, "Failed to map tqp: %d", ret);
4419 ret = hns3_init_umv_space(hw);
4421 PMD_INIT_LOG(ERR, "Failed to init umv space: %d", ret);
4425 ret = hns3_mac_init(hw);
4427 PMD_INIT_LOG(ERR, "Failed to init MAC: %d", ret);
4431 ret = hns3_init_mgr_tbl(hw);
4433 PMD_INIT_LOG(ERR, "Failed to init manager table: %d", ret);
4437 ret = hns3_promisc_init(hw);
4439 PMD_INIT_LOG(ERR, "Failed to init promisc: %d",
4444 ret = hns3_init_vlan_config(hns);
4446 PMD_INIT_LOG(ERR, "Failed to init vlan: %d", ret);
4450 ret = hns3_dcb_init(hw);
4452 PMD_INIT_LOG(ERR, "Failed to init dcb: %d", ret);
4456 ret = hns3_init_fd_config(hns);
4458 PMD_INIT_LOG(ERR, "Failed to init flow director: %d", ret);
4462 ret = hns3_config_tso(hw, HNS3_TSO_MSS_MIN, HNS3_TSO_MSS_MAX);
4464 PMD_INIT_LOG(ERR, "Failed to config tso: %d", ret);
4468 ret = hns3_config_gro(hw, false);
4470 PMD_INIT_LOG(ERR, "Failed to config gro: %d", ret);
4475 * In the initialization clearing the all hardware mapping relationship
4476 * configurations between queues and interrupt vectors is needed, so
4477 * some error caused by the residual configurations, such as the
4478 * unexpected interrupt, can be avoid.
4480 ret = hns3_init_ring_with_vector(hw);
4482 PMD_INIT_LOG(ERR, "Failed to init ring intr vector: %d", ret);
4489 hns3_uninit_umv_space(hw);
4494 hns3_clear_hw(struct hns3_hw *hw)
4496 struct hns3_cmd_desc desc;
4499 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_CLEAR_HW_STATE, false);
4501 ret = hns3_cmd_send(hw, &desc, 1);
4502 if (ret && ret != -EOPNOTSUPP)
4509 hns3_config_all_msix_error(struct hns3_hw *hw, bool enable)
4514 * The new firmware support report more hardware error types by
4515 * msix mode. These errors are defined as RAS errors in hardware
4516 * and belong to a different type from the MSI-x errors processed
4517 * by the network driver.
4519 * Network driver should open the new error report on initialization.
4521 val = hns3_read_dev(hw, HNS3_VECTOR0_OTER_EN_REG);
4522 hns3_set_bit(val, HNS3_VECTOR0_ALL_MSIX_ERR_B, enable ? 1 : 0);
4523 hns3_write_dev(hw, HNS3_VECTOR0_OTER_EN_REG, val);
4527 hns3_set_firber_default_support_speed(struct hns3_hw *hw)
4529 struct hns3_mac *mac = &hw->mac;
4531 switch (mac->link_speed) {
4532 case RTE_ETH_SPEED_NUM_1G:
4533 return HNS3_FIBER_LINK_SPEED_1G_BIT;
4534 case RTE_ETH_SPEED_NUM_10G:
4535 return HNS3_FIBER_LINK_SPEED_10G_BIT;
4536 case RTE_ETH_SPEED_NUM_25G:
4537 return HNS3_FIBER_LINK_SPEED_25G_BIT;
4538 case RTE_ETH_SPEED_NUM_40G:
4539 return HNS3_FIBER_LINK_SPEED_40G_BIT;
4540 case RTE_ETH_SPEED_NUM_50G:
4541 return HNS3_FIBER_LINK_SPEED_50G_BIT;
4542 case RTE_ETH_SPEED_NUM_100G:
4543 return HNS3_FIBER_LINK_SPEED_100G_BIT;
4544 case RTE_ETH_SPEED_NUM_200G:
4545 return HNS3_FIBER_LINK_SPEED_200G_BIT;
4547 hns3_warn(hw, "invalid speed %u Mbps.", mac->link_speed);
4553 * Validity of supported_speed for firber and copper media type can be
4554 * guaranteed by the following policy:
4556 * Although the initialization of the phy in the firmware may not be
4557 * completed, the firmware can guarantees that the supported_speed is
4560 * If the version of firmware supports the acitive query way of the
4561 * HNS3_OPC_GET_SFP_INFO opcode, the supported_speed can be obtained
4562 * through it. If unsupported, use the SFP's speed as the value of the
4566 hns3_get_port_supported_speed(struct rte_eth_dev *eth_dev)
4568 struct hns3_adapter *hns = eth_dev->data->dev_private;
4569 struct hns3_hw *hw = &hns->hw;
4570 struct hns3_mac *mac = &hw->mac;
4573 ret = hns3_update_link_info(eth_dev);
4577 if (mac->media_type == HNS3_MEDIA_TYPE_FIBER) {
4579 * Some firmware does not support the report of supported_speed,
4580 * and only report the effective speed of SFP. In this case, it
4581 * is necessary to use the SFP's speed as the supported_speed.
4583 if (mac->supported_speed == 0)
4584 mac->supported_speed =
4585 hns3_set_firber_default_support_speed(hw);
4592 hns3_get_fc_autoneg_capability(struct hns3_adapter *hns)
4594 struct hns3_mac *mac = &hns->hw.mac;
4596 if (mac->media_type == HNS3_MEDIA_TYPE_COPPER) {
4597 hns->pf.support_fc_autoneg = true;
4602 * Flow control auto-negotiation requires the cooperation of the driver
4603 * and firmware. Currently, the optical port does not support flow
4604 * control auto-negotiation.
4606 hns->pf.support_fc_autoneg = false;
4610 hns3_init_pf(struct rte_eth_dev *eth_dev)
4612 struct rte_device *dev = eth_dev->device;
4613 struct rte_pci_device *pci_dev = RTE_DEV_TO_PCI(dev);
4614 struct hns3_adapter *hns = eth_dev->data->dev_private;
4615 struct hns3_hw *hw = &hns->hw;
4618 PMD_INIT_FUNC_TRACE();
4620 /* Get hardware io base address from pcie BAR2 IO space */
4621 hw->io_base = pci_dev->mem_resource[2].addr;
4623 /* Firmware command queue initialize */
4624 ret = hns3_cmd_init_queue(hw);
4626 PMD_INIT_LOG(ERR, "Failed to init cmd queue: %d", ret);
4627 goto err_cmd_init_queue;
4630 hns3_clear_all_event_cause(hw);
4632 /* Firmware command initialize */
4633 ret = hns3_cmd_init(hw);
4635 PMD_INIT_LOG(ERR, "Failed to init cmd: %d", ret);
4639 hns3_tx_push_init(eth_dev);
4642 * To ensure that the hardware environment is clean during
4643 * initialization, the driver actively clear the hardware environment
4644 * during initialization, including PF and corresponding VFs' vlan, mac,
4645 * flow table configurations, etc.
4647 ret = hns3_clear_hw(hw);
4649 PMD_INIT_LOG(ERR, "failed to clear hardware: %d", ret);
4653 /* Hardware statistics of imissed registers cleared. */
4654 ret = hns3_update_imissed_stats(hw, true);
4656 hns3_err(hw, "clear imissed stats failed, ret = %d", ret);
4660 hns3_config_all_msix_error(hw, true);
4662 ret = rte_intr_callback_register(pci_dev->intr_handle,
4663 hns3_interrupt_handler,
4666 PMD_INIT_LOG(ERR, "Failed to register intr: %d", ret);
4667 goto err_intr_callback_register;
4670 ret = hns3_ptp_init(hw);
4672 goto err_get_config;
4674 /* Enable interrupt */
4675 rte_intr_enable(pci_dev->intr_handle);
4676 hns3_pf_enable_irq0(hw);
4678 /* Get configuration */
4679 ret = hns3_get_configuration(hw);
4681 PMD_INIT_LOG(ERR, "Failed to fetch configuration: %d", ret);
4682 goto err_get_config;
4685 ret = hns3_tqp_stats_init(hw);
4687 goto err_get_config;
4689 ret = hns3_init_hardware(hns);
4691 PMD_INIT_LOG(ERR, "Failed to init hardware: %d", ret);
4695 /* Initialize flow director filter list & hash */
4696 ret = hns3_fdir_filter_init(hns);
4698 PMD_INIT_LOG(ERR, "Failed to alloc hashmap for fdir: %d", ret);
4702 hns3_rss_set_default_args(hw);
4704 ret = hns3_enable_hw_error_intr(hns, true);
4706 PMD_INIT_LOG(ERR, "fail to enable hw error interrupts: %d",
4708 goto err_enable_intr;
4711 ret = hns3_get_port_supported_speed(eth_dev);
4713 PMD_INIT_LOG(ERR, "failed to get speed capabilities supported "
4714 "by device, ret = %d.", ret);
4715 goto err_supported_speed;
4718 hns3_get_fc_autoneg_capability(hns);
4720 hns3_tm_conf_init(eth_dev);
4724 err_supported_speed:
4725 (void)hns3_enable_hw_error_intr(hns, false);
4727 hns3_fdir_filter_uninit(hns);
4729 hns3_uninit_umv_space(hw);
4731 hns3_tqp_stats_uninit(hw);
4733 hns3_pf_disable_irq0(hw);
4734 rte_intr_disable(pci_dev->intr_handle);
4735 hns3_intr_unregister(pci_dev->intr_handle, hns3_interrupt_handler,
4737 err_intr_callback_register:
4739 hns3_cmd_uninit(hw);
4740 hns3_cmd_destroy_queue(hw);
4748 hns3_uninit_pf(struct rte_eth_dev *eth_dev)
4750 struct hns3_adapter *hns = eth_dev->data->dev_private;
4751 struct rte_device *dev = eth_dev->device;
4752 struct rte_pci_device *pci_dev = RTE_DEV_TO_PCI(dev);
4753 struct hns3_hw *hw = &hns->hw;
4755 PMD_INIT_FUNC_TRACE();
4757 hns3_tm_conf_uninit(eth_dev);
4758 hns3_enable_hw_error_intr(hns, false);
4759 hns3_rss_uninit(hns);
4760 (void)hns3_config_gro(hw, false);
4761 hns3_promisc_uninit(hw);
4762 hns3_flow_uninit(eth_dev);
4763 hns3_fdir_filter_uninit(hns);
4764 hns3_uninit_umv_space(hw);
4765 hns3_tqp_stats_uninit(hw);
4766 hns3_config_mac_tnl_int(hw, false);
4767 hns3_pf_disable_irq0(hw);
4768 rte_intr_disable(pci_dev->intr_handle);
4769 hns3_intr_unregister(pci_dev->intr_handle, hns3_interrupt_handler,
4771 hns3_config_all_msix_error(hw, false);
4772 hns3_cmd_uninit(hw);
4773 hns3_cmd_destroy_queue(hw);
4778 hns3_convert_link_speeds2bitmap_copper(uint32_t link_speeds)
4782 switch (link_speeds & ~RTE_ETH_LINK_SPEED_FIXED) {
4783 case RTE_ETH_LINK_SPEED_10M:
4784 speed_bit = HNS3_PHY_LINK_SPEED_10M_BIT;
4786 case RTE_ETH_LINK_SPEED_10M_HD:
4787 speed_bit = HNS3_PHY_LINK_SPEED_10M_HD_BIT;
4789 case RTE_ETH_LINK_SPEED_100M:
4790 speed_bit = HNS3_PHY_LINK_SPEED_100M_BIT;
4792 case RTE_ETH_LINK_SPEED_100M_HD:
4793 speed_bit = HNS3_PHY_LINK_SPEED_100M_HD_BIT;
4795 case RTE_ETH_LINK_SPEED_1G:
4796 speed_bit = HNS3_PHY_LINK_SPEED_1000M_BIT;
4807 hns3_convert_link_speeds2bitmap_fiber(uint32_t link_speeds)
4811 switch (link_speeds & ~RTE_ETH_LINK_SPEED_FIXED) {
4812 case RTE_ETH_LINK_SPEED_1G:
4813 speed_bit = HNS3_FIBER_LINK_SPEED_1G_BIT;
4815 case RTE_ETH_LINK_SPEED_10G:
4816 speed_bit = HNS3_FIBER_LINK_SPEED_10G_BIT;
4818 case RTE_ETH_LINK_SPEED_25G:
4819 speed_bit = HNS3_FIBER_LINK_SPEED_25G_BIT;
4821 case RTE_ETH_LINK_SPEED_40G:
4822 speed_bit = HNS3_FIBER_LINK_SPEED_40G_BIT;
4824 case RTE_ETH_LINK_SPEED_50G:
4825 speed_bit = HNS3_FIBER_LINK_SPEED_50G_BIT;
4827 case RTE_ETH_LINK_SPEED_100G:
4828 speed_bit = HNS3_FIBER_LINK_SPEED_100G_BIT;
4830 case RTE_ETH_LINK_SPEED_200G:
4831 speed_bit = HNS3_FIBER_LINK_SPEED_200G_BIT;
4842 hns3_check_port_speed(struct hns3_hw *hw, uint32_t link_speeds)
4844 struct hns3_mac *mac = &hw->mac;
4845 uint32_t supported_speed = mac->supported_speed;
4846 uint32_t speed_bit = 0;
4848 if (mac->media_type == HNS3_MEDIA_TYPE_COPPER)
4849 speed_bit = hns3_convert_link_speeds2bitmap_copper(link_speeds);
4850 else if (mac->media_type == HNS3_MEDIA_TYPE_FIBER)
4851 speed_bit = hns3_convert_link_speeds2bitmap_fiber(link_speeds);
4853 if (!(speed_bit & supported_speed)) {
4854 hns3_err(hw, "link_speeds(0x%x) exceeds the supported speed capability or is incorrect.",
4862 static inline uint32_t
4863 hns3_get_link_speed(uint32_t link_speeds)
4865 uint32_t speed = RTE_ETH_SPEED_NUM_NONE;
4867 if (link_speeds & RTE_ETH_LINK_SPEED_10M ||
4868 link_speeds & RTE_ETH_LINK_SPEED_10M_HD)
4869 speed = RTE_ETH_SPEED_NUM_10M;
4870 if (link_speeds & RTE_ETH_LINK_SPEED_100M ||
4871 link_speeds & RTE_ETH_LINK_SPEED_100M_HD)
4872 speed = RTE_ETH_SPEED_NUM_100M;
4873 if (link_speeds & RTE_ETH_LINK_SPEED_1G)
4874 speed = RTE_ETH_SPEED_NUM_1G;
4875 if (link_speeds & RTE_ETH_LINK_SPEED_10G)
4876 speed = RTE_ETH_SPEED_NUM_10G;
4877 if (link_speeds & RTE_ETH_LINK_SPEED_25G)
4878 speed = RTE_ETH_SPEED_NUM_25G;
4879 if (link_speeds & RTE_ETH_LINK_SPEED_40G)
4880 speed = RTE_ETH_SPEED_NUM_40G;
4881 if (link_speeds & RTE_ETH_LINK_SPEED_50G)
4882 speed = RTE_ETH_SPEED_NUM_50G;
4883 if (link_speeds & RTE_ETH_LINK_SPEED_100G)
4884 speed = RTE_ETH_SPEED_NUM_100G;
4885 if (link_speeds & RTE_ETH_LINK_SPEED_200G)
4886 speed = RTE_ETH_SPEED_NUM_200G;
4892 hns3_get_link_duplex(uint32_t link_speeds)
4894 if ((link_speeds & RTE_ETH_LINK_SPEED_10M_HD) ||
4895 (link_speeds & RTE_ETH_LINK_SPEED_100M_HD))
4896 return RTE_ETH_LINK_HALF_DUPLEX;
4898 return RTE_ETH_LINK_FULL_DUPLEX;
4902 hns3_set_copper_port_link_speed(struct hns3_hw *hw,
4903 struct hns3_set_link_speed_cfg *cfg)
4905 struct hns3_cmd_desc desc[HNS3_PHY_PARAM_CFG_BD_NUM];
4906 struct hns3_phy_params_bd0_cmd *req;
4909 for (i = 0; i < HNS3_PHY_PARAM_CFG_BD_NUM - 1; i++) {
4910 hns3_cmd_setup_basic_desc(&desc[i], HNS3_OPC_PHY_PARAM_CFG,
4912 desc[i].flag |= rte_cpu_to_le_16(HNS3_CMD_FLAG_NEXT);
4914 hns3_cmd_setup_basic_desc(&desc[i], HNS3_OPC_PHY_PARAM_CFG, false);
4915 req = (struct hns3_phy_params_bd0_cmd *)desc[0].data;
4916 req->autoneg = cfg->autoneg;
4919 * The full speed capability is used to negotiate when
4920 * auto-negotiation is enabled.
4923 req->advertising = HNS3_PHY_LINK_SPEED_10M_BIT |
4924 HNS3_PHY_LINK_SPEED_10M_HD_BIT |
4925 HNS3_PHY_LINK_SPEED_100M_BIT |
4926 HNS3_PHY_LINK_SPEED_100M_HD_BIT |
4927 HNS3_PHY_LINK_SPEED_1000M_BIT;
4929 req->speed = cfg->speed;
4930 req->duplex = cfg->duplex;
4933 return hns3_cmd_send(hw, desc, HNS3_PHY_PARAM_CFG_BD_NUM);
4937 hns3_set_autoneg(struct hns3_hw *hw, bool enable)
4939 struct hns3_config_auto_neg_cmd *req;
4940 struct hns3_cmd_desc desc;
4944 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_CONFIG_AN_MODE, false);
4946 req = (struct hns3_config_auto_neg_cmd *)desc.data;
4948 hns3_set_bit(flag, HNS3_MAC_CFG_AN_EN_B, 1);
4949 req->cfg_an_cmd_flag = rte_cpu_to_le_32(flag);
4951 ret = hns3_cmd_send(hw, &desc, 1);
4953 hns3_err(hw, "autoneg set cmd failed, ret = %d.", ret);
4959 hns3_set_fiber_port_link_speed(struct hns3_hw *hw,
4960 struct hns3_set_link_speed_cfg *cfg)
4964 if (hw->mac.support_autoneg) {
4965 ret = hns3_set_autoneg(hw, cfg->autoneg);
4967 hns3_err(hw, "failed to configure auto-negotiation.");
4972 * To enable auto-negotiation, we only need to open the switch
4973 * of auto-negotiation, then firmware sets all speed
4981 * Some hardware doesn't support auto-negotiation, but users may not
4982 * configure link_speeds (default 0), which means auto-negotiation.
4983 * In this case, a warning message need to be printed, instead of
4987 hns3_warn(hw, "auto-negotiation is not supported, use default fixed speed!");
4991 return hns3_cfg_mac_speed_dup(hw, cfg->speed, cfg->duplex);
4995 hns3_set_port_link_speed(struct hns3_hw *hw,
4996 struct hns3_set_link_speed_cfg *cfg)
5000 if (hw->mac.media_type == HNS3_MEDIA_TYPE_COPPER) {
5001 #if defined(RTE_HNS3_ONLY_1630_FPGA)
5002 struct hns3_pf *pf = HNS3_DEV_HW_TO_PF(hw);
5007 ret = hns3_set_copper_port_link_speed(hw, cfg);
5009 hns3_err(hw, "failed to set copper port link speed,"
5013 } else if (hw->mac.media_type == HNS3_MEDIA_TYPE_FIBER) {
5014 ret = hns3_set_fiber_port_link_speed(hw, cfg);
5016 hns3_err(hw, "failed to set fiber port link speed,"
5026 hns3_apply_link_speed(struct hns3_hw *hw)
5028 struct rte_eth_conf *conf = &hw->data->dev_conf;
5029 struct hns3_set_link_speed_cfg cfg;
5031 memset(&cfg, 0, sizeof(struct hns3_set_link_speed_cfg));
5032 cfg.autoneg = (conf->link_speeds == RTE_ETH_LINK_SPEED_AUTONEG) ?
5033 RTE_ETH_LINK_AUTONEG : RTE_ETH_LINK_FIXED;
5034 if (cfg.autoneg != RTE_ETH_LINK_AUTONEG) {
5035 cfg.speed = hns3_get_link_speed(conf->link_speeds);
5036 cfg.duplex = hns3_get_link_duplex(conf->link_speeds);
5039 return hns3_set_port_link_speed(hw, &cfg);
5043 hns3_do_start(struct hns3_adapter *hns, bool reset_queue)
5045 struct hns3_hw *hw = &hns->hw;
5049 ret = hns3_update_queue_map_configure(hns);
5051 hns3_err(hw, "failed to update queue mapping configuration, ret = %d",
5056 /* Note: hns3_tm_conf_update must be called after configuring DCB. */
5057 ret = hns3_tm_conf_update(hw);
5059 PMD_INIT_LOG(ERR, "failed to update tm conf, ret = %d.", ret);
5063 hns3_enable_rxd_adv_layout(hw);
5065 ret = hns3_init_queues(hns, reset_queue);
5067 PMD_INIT_LOG(ERR, "failed to init queues, ret = %d.", ret);
5071 link_en = hw->set_link_down ? false : true;
5072 ret = hns3_cfg_mac_mode(hw, link_en);
5074 PMD_INIT_LOG(ERR, "failed to enable MAC, ret = %d", ret);
5075 goto err_config_mac_mode;
5078 ret = hns3_apply_link_speed(hw);
5080 goto err_set_link_speed;
5085 (void)hns3_cfg_mac_mode(hw, false);
5087 err_config_mac_mode:
5088 hns3_dev_release_mbufs(hns);
5090 * Here is exception handling, hns3_reset_all_tqps will have the
5091 * corresponding error message if it is handled incorrectly, so it is
5092 * not necessary to check hns3_reset_all_tqps return value, here keep
5093 * ret as the error code causing the exception.
5095 (void)hns3_reset_all_tqps(hns);
5100 hns3_restore_filter(struct rte_eth_dev *dev)
5102 hns3_restore_rss_filter(dev);
5106 hns3_dev_start(struct rte_eth_dev *dev)
5108 struct hns3_adapter *hns = dev->data->dev_private;
5109 struct hns3_hw *hw = &hns->hw;
5110 bool old_state = hw->set_link_down;
5113 PMD_INIT_FUNC_TRACE();
5114 if (__atomic_load_n(&hw->reset.resetting, __ATOMIC_RELAXED))
5117 rte_spinlock_lock(&hw->lock);
5118 hw->adapter_state = HNS3_NIC_STARTING;
5121 * If the dev_set_link_down() API has been called, the "set_link_down"
5122 * flag can be cleared by dev_start() API. In addition, the flag should
5123 * also be cleared before calling hns3_do_start() so that MAC can be
5124 * enabled in dev_start stage.
5126 hw->set_link_down = false;
5127 ret = hns3_do_start(hns, true);
5131 ret = hns3_map_rx_interrupt(dev);
5133 goto map_rx_inter_err;
5136 * There are three register used to control the status of a TQP
5137 * (contains a pair of Tx queue and Rx queue) in the new version network
5138 * engine. One is used to control the enabling of Tx queue, the other is
5139 * used to control the enabling of Rx queue, and the last is the master
5140 * switch used to control the enabling of the tqp. The Tx register and
5141 * TQP register must be enabled at the same time to enable a Tx queue.
5142 * The same applies to the Rx queue. For the older network engine, this
5143 * function only refresh the enabled flag, and it is used to update the
5144 * status of queue in the dpdk framework.
5146 ret = hns3_start_all_txqs(dev);
5148 goto map_rx_inter_err;
5150 ret = hns3_start_all_rxqs(dev);
5152 goto start_all_rxqs_fail;
5154 hw->adapter_state = HNS3_NIC_STARTED;
5155 rte_spinlock_unlock(&hw->lock);
5157 hns3_rx_scattered_calc(dev);
5158 hns3_set_rxtx_function(dev);
5159 hns3_mp_req_start_rxtx(dev);
5161 hns3_restore_filter(dev);
5163 /* Enable interrupt of all rx queues before enabling queues */
5164 hns3_dev_all_rx_queue_intr_enable(hw, true);
5167 * After finished the initialization, enable tqps to receive/transmit
5168 * packets and refresh all queue status.
5170 hns3_start_tqps(hw);
5172 hns3_tm_dev_start_proc(hw);
5174 if (dev->data->dev_conf.intr_conf.lsc != 0)
5175 hns3_dev_link_update(dev, 0);
5176 rte_eal_alarm_set(HNS3_SERVICE_INTERVAL, hns3_service_handler, dev);
5178 hns3_info(hw, "hns3 dev start successful!");
5182 start_all_rxqs_fail:
5183 hns3_stop_all_txqs(dev);
5185 (void)hns3_do_stop(hns);
5187 hw->set_link_down = old_state;
5188 hw->adapter_state = HNS3_NIC_CONFIGURED;
5189 rte_spinlock_unlock(&hw->lock);
5195 hns3_do_stop(struct hns3_adapter *hns)
5197 struct hns3_hw *hw = &hns->hw;
5201 * The "hns3_do_stop" function will also be called by .stop_service to
5202 * prepare reset. At the time of global or IMP reset, the command cannot
5203 * be sent to stop the tx/rx queues. The mbuf in Tx/Rx queues may be
5204 * accessed during the reset process. So the mbuf can not be released
5205 * during reset and is required to be released after the reset is
5208 if (__atomic_load_n(&hw->reset.resetting, __ATOMIC_RELAXED) == 0)
5209 hns3_dev_release_mbufs(hns);
5211 ret = hns3_cfg_mac_mode(hw, false);
5214 hw->mac.link_status = RTE_ETH_LINK_DOWN;
5216 if (__atomic_load_n(&hw->reset.disable_cmd, __ATOMIC_RELAXED) == 0) {
5217 hns3_configure_all_mac_addr(hns, true);
5218 ret = hns3_reset_all_tqps(hns);
5220 hns3_err(hw, "failed to reset all queues ret = %d.",
5230 hns3_dev_stop(struct rte_eth_dev *dev)
5232 struct hns3_adapter *hns = dev->data->dev_private;
5233 struct hns3_hw *hw = &hns->hw;
5235 PMD_INIT_FUNC_TRACE();
5236 dev->data->dev_started = 0;
5238 hw->adapter_state = HNS3_NIC_STOPPING;
5239 hns3_set_rxtx_function(dev);
5241 /* Disable datapath on secondary process. */
5242 hns3_mp_req_stop_rxtx(dev);
5243 /* Prevent crashes when queues are still in use. */
5244 rte_delay_ms(hw->cfg_max_queues);
5246 rte_spinlock_lock(&hw->lock);
5247 if (__atomic_load_n(&hw->reset.resetting, __ATOMIC_RELAXED) == 0) {
5248 hns3_tm_dev_stop_proc(hw);
5249 hns3_config_mac_tnl_int(hw, false);
5252 hns3_unmap_rx_interrupt(dev);
5253 hw->adapter_state = HNS3_NIC_CONFIGURED;
5255 hns3_rx_scattered_reset(dev);
5256 rte_eal_alarm_cancel(hns3_service_handler, dev);
5257 hns3_stop_report_lse(dev);
5258 rte_spinlock_unlock(&hw->lock);
5264 hns3_dev_close(struct rte_eth_dev *eth_dev)
5266 struct hns3_adapter *hns = eth_dev->data->dev_private;
5267 struct hns3_hw *hw = &hns->hw;
5270 if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
5271 hns3_mp_uninit(eth_dev);
5275 if (hw->adapter_state == HNS3_NIC_STARTED)
5276 ret = hns3_dev_stop(eth_dev);
5278 hw->adapter_state = HNS3_NIC_CLOSING;
5279 hns3_reset_abort(hns);
5280 hw->adapter_state = HNS3_NIC_CLOSED;
5282 hns3_configure_all_mc_mac_addr(hns, true);
5283 hns3_remove_all_vlan_table(hns);
5284 hns3_vlan_txvlan_cfg(hns, HNS3_PORT_BASE_VLAN_DISABLE, 0);
5285 hns3_uninit_pf(eth_dev);
5286 hns3_free_all_queues(eth_dev);
5287 rte_free(hw->reset.wait_data);
5288 hns3_mp_uninit(eth_dev);
5289 hns3_warn(hw, "Close port %u finished", hw->data->port_id);
5295 hns3_get_autoneg_rxtx_pause_copper(struct hns3_hw *hw, bool *rx_pause,
5298 struct hns3_mac *mac = &hw->mac;
5299 uint32_t advertising = mac->advertising;
5300 uint32_t lp_advertising = mac->lp_advertising;
5304 if (advertising & lp_advertising & HNS3_PHY_LINK_MODE_PAUSE_BIT) {
5307 } else if (advertising & lp_advertising &
5308 HNS3_PHY_LINK_MODE_ASYM_PAUSE_BIT) {
5309 if (advertising & HNS3_PHY_LINK_MODE_PAUSE_BIT)
5311 else if (lp_advertising & HNS3_PHY_LINK_MODE_PAUSE_BIT)
5316 static enum hns3_fc_mode
5317 hns3_get_autoneg_fc_mode(struct hns3_hw *hw)
5319 enum hns3_fc_mode current_mode;
5320 bool rx_pause = false;
5321 bool tx_pause = false;
5323 switch (hw->mac.media_type) {
5324 case HNS3_MEDIA_TYPE_COPPER:
5325 hns3_get_autoneg_rxtx_pause_copper(hw, &rx_pause, &tx_pause);
5329 * Flow control auto-negotiation is not supported for fiber and
5330 * backpalne media type.
5332 case HNS3_MEDIA_TYPE_FIBER:
5333 case HNS3_MEDIA_TYPE_BACKPLANE:
5334 hns3_err(hw, "autoneg FC mode can't be obtained, but flow control auto-negotiation is enabled.");
5335 current_mode = hw->requested_fc_mode;
5338 hns3_err(hw, "autoneg FC mode can't be obtained for unknown media type(%u).",
5339 hw->mac.media_type);
5340 current_mode = HNS3_FC_NONE;
5344 if (rx_pause && tx_pause)
5345 current_mode = HNS3_FC_FULL;
5347 current_mode = HNS3_FC_RX_PAUSE;
5349 current_mode = HNS3_FC_TX_PAUSE;
5351 current_mode = HNS3_FC_NONE;
5354 return current_mode;
5357 static enum hns3_fc_mode
5358 hns3_get_current_fc_mode(struct rte_eth_dev *dev)
5360 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
5361 struct hns3_pf *pf = HNS3_DEV_PRIVATE_TO_PF(dev->data->dev_private);
5362 struct hns3_mac *mac = &hw->mac;
5365 * When the flow control mode is obtained, the device may not complete
5366 * auto-negotiation. It is necessary to wait for link establishment.
5368 (void)hns3_dev_link_update(dev, 1);
5371 * If the link auto-negotiation of the nic is disabled, or the flow
5372 * control auto-negotiation is not supported, the forced flow control
5375 if (mac->link_autoneg == 0 || !pf->support_fc_autoneg)
5376 return hw->requested_fc_mode;
5378 return hns3_get_autoneg_fc_mode(hw);
5382 hns3_flow_ctrl_get(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
5384 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
5385 struct hns3_pf *pf = HNS3_DEV_PRIVATE_TO_PF(dev->data->dev_private);
5386 enum hns3_fc_mode current_mode;
5388 current_mode = hns3_get_current_fc_mode(dev);
5389 switch (current_mode) {
5391 fc_conf->mode = RTE_ETH_FC_FULL;
5393 case HNS3_FC_TX_PAUSE:
5394 fc_conf->mode = RTE_ETH_FC_TX_PAUSE;
5396 case HNS3_FC_RX_PAUSE:
5397 fc_conf->mode = RTE_ETH_FC_RX_PAUSE;
5401 fc_conf->mode = RTE_ETH_FC_NONE;
5405 fc_conf->pause_time = pf->pause_time;
5406 fc_conf->autoneg = pf->support_fc_autoneg ? hw->mac.link_autoneg : 0;
5412 hns3_check_fc_autoneg_valid(struct hns3_hw *hw, uint8_t autoneg)
5414 struct hns3_pf *pf = HNS3_DEV_HW_TO_PF(hw);
5416 if (!pf->support_fc_autoneg) {
5418 hns3_err(hw, "unsupported fc auto-negotiation setting.");
5423 * Flow control auto-negotiation of the NIC is not supported,
5424 * but other auto-negotiation features may be supported.
5426 if (autoneg != hw->mac.link_autoneg) {
5427 hns3_err(hw, "please use 'link_speeds' in struct rte_eth_conf to disable autoneg!");
5435 * If flow control auto-negotiation of the NIC is supported, all
5436 * auto-negotiation features are supported.
5438 if (autoneg != hw->mac.link_autoneg) {
5439 hns3_err(hw, "please use 'link_speeds' in struct rte_eth_conf to change autoneg!");
5447 hns3_flow_ctrl_set(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
5449 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
5450 struct hns3_pf *pf = HNS3_DEV_PRIVATE_TO_PF(dev->data->dev_private);
5453 if (fc_conf->high_water || fc_conf->low_water ||
5454 fc_conf->send_xon || fc_conf->mac_ctrl_frame_fwd) {
5455 hns3_err(hw, "Unsupported flow control settings specified, "
5456 "high_water(%u), low_water(%u), send_xon(%u) and "
5457 "mac_ctrl_frame_fwd(%u) must be set to '0'",
5458 fc_conf->high_water, fc_conf->low_water,
5459 fc_conf->send_xon, fc_conf->mac_ctrl_frame_fwd);
5463 ret = hns3_check_fc_autoneg_valid(hw, fc_conf->autoneg);
5467 if (!fc_conf->pause_time) {
5468 hns3_err(hw, "Invalid pause time %u setting.",
5469 fc_conf->pause_time);
5473 if (!(hw->current_fc_status == HNS3_FC_STATUS_NONE ||
5474 hw->current_fc_status == HNS3_FC_STATUS_MAC_PAUSE)) {
5475 hns3_err(hw, "PFC is enabled. Cannot set MAC pause. "
5476 "current_fc_status = %d", hw->current_fc_status);
5480 if (hw->num_tc > 1 && !pf->support_multi_tc_pause) {
5481 hns3_err(hw, "in multi-TC scenarios, MAC pause is not supported.");
5485 rte_spinlock_lock(&hw->lock);
5486 ret = hns3_fc_enable(dev, fc_conf);
5487 rte_spinlock_unlock(&hw->lock);
5493 hns3_priority_flow_ctrl_set(struct rte_eth_dev *dev,
5494 struct rte_eth_pfc_conf *pfc_conf)
5496 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
5499 if (!hns3_dev_get_support(hw, DCB)) {
5500 hns3_err(hw, "This port does not support dcb configurations.");
5504 if (pfc_conf->fc.high_water || pfc_conf->fc.low_water ||
5505 pfc_conf->fc.send_xon || pfc_conf->fc.mac_ctrl_frame_fwd) {
5506 hns3_err(hw, "Unsupported flow control settings specified, "
5507 "high_water(%u), low_water(%u), send_xon(%u) and "
5508 "mac_ctrl_frame_fwd(%u) must be set to '0'",
5509 pfc_conf->fc.high_water, pfc_conf->fc.low_water,
5510 pfc_conf->fc.send_xon,
5511 pfc_conf->fc.mac_ctrl_frame_fwd);
5514 if (pfc_conf->fc.autoneg) {
5515 hns3_err(hw, "Unsupported fc auto-negotiation setting.");
5518 if (pfc_conf->fc.pause_time == 0) {
5519 hns3_err(hw, "Invalid pause time %u setting.",
5520 pfc_conf->fc.pause_time);
5524 if (!(hw->current_fc_status == HNS3_FC_STATUS_NONE ||
5525 hw->current_fc_status == HNS3_FC_STATUS_PFC)) {
5526 hns3_err(hw, "MAC pause is enabled. Cannot set PFC."
5527 "current_fc_status = %d", hw->current_fc_status);
5531 rte_spinlock_lock(&hw->lock);
5532 ret = hns3_dcb_pfc_enable(dev, pfc_conf);
5533 rte_spinlock_unlock(&hw->lock);
5539 hns3_get_dcb_info(struct rte_eth_dev *dev, struct rte_eth_dcb_info *dcb_info)
5541 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
5542 struct hns3_pf *pf = HNS3_DEV_PRIVATE_TO_PF(dev->data->dev_private);
5543 enum rte_eth_rx_mq_mode mq_mode = dev->data->dev_conf.rxmode.mq_mode;
5546 rte_spinlock_lock(&hw->lock);
5547 if ((uint32_t)mq_mode & RTE_ETH_MQ_RX_DCB_FLAG)
5548 dcb_info->nb_tcs = pf->local_max_tc;
5550 dcb_info->nb_tcs = 1;
5552 for (i = 0; i < HNS3_MAX_USER_PRIO; i++)
5553 dcb_info->prio_tc[i] = hw->dcb_info.prio_tc[i];
5554 for (i = 0; i < dcb_info->nb_tcs; i++)
5555 dcb_info->tc_bws[i] = hw->dcb_info.pg_info[0].tc_dwrr[i];
5557 for (i = 0; i < hw->num_tc; i++) {
5558 dcb_info->tc_queue.tc_rxq[0][i].base = hw->alloc_rss_size * i;
5559 dcb_info->tc_queue.tc_txq[0][i].base =
5560 hw->tc_queue[i].tqp_offset;
5561 dcb_info->tc_queue.tc_rxq[0][i].nb_queue = hw->alloc_rss_size;
5562 dcb_info->tc_queue.tc_txq[0][i].nb_queue =
5563 hw->tc_queue[i].tqp_count;
5565 rte_spinlock_unlock(&hw->lock);
5571 hns3_reinit_dev(struct hns3_adapter *hns)
5573 struct hns3_hw *hw = &hns->hw;
5576 ret = hns3_cmd_init(hw);
5578 hns3_err(hw, "Failed to init cmd: %d", ret);
5582 ret = hns3_reset_all_tqps(hns);
5584 hns3_err(hw, "Failed to reset all queues: %d", ret);
5588 ret = hns3_init_hardware(hns);
5590 hns3_err(hw, "Failed to init hardware: %d", ret);
5594 ret = hns3_enable_hw_error_intr(hns, true);
5596 hns3_err(hw, "fail to enable hw error interrupts: %d",
5600 hns3_info(hw, "Reset done, driver initialization finished.");
5606 is_pf_reset_done(struct hns3_hw *hw)
5608 uint32_t val, reg, reg_bit;
5610 switch (hw->reset.level) {
5611 case HNS3_IMP_RESET:
5612 reg = HNS3_GLOBAL_RESET_REG;
5613 reg_bit = HNS3_IMP_RESET_BIT;
5615 case HNS3_GLOBAL_RESET:
5616 reg = HNS3_GLOBAL_RESET_REG;
5617 reg_bit = HNS3_GLOBAL_RESET_BIT;
5619 case HNS3_FUNC_RESET:
5620 reg = HNS3_FUN_RST_ING;
5621 reg_bit = HNS3_FUN_RST_ING_B;
5623 case HNS3_FLR_RESET:
5625 hns3_err(hw, "Wait for unsupported reset level: %d",
5629 val = hns3_read_dev(hw, reg);
5630 if (hns3_get_bit(val, reg_bit))
5637 hns3_is_reset_pending(struct hns3_adapter *hns)
5639 struct hns3_hw *hw = &hns->hw;
5640 enum hns3_reset_level reset;
5642 hns3_check_event_cause(hns, NULL);
5643 reset = hns3_get_reset_level(hns, &hw->reset.pending);
5644 if (reset != HNS3_NONE_RESET && hw->reset.level != HNS3_NONE_RESET &&
5645 hw->reset.level < reset) {
5646 hns3_warn(hw, "High level reset %d is pending", reset);
5649 reset = hns3_get_reset_level(hns, &hw->reset.request);
5650 if (reset != HNS3_NONE_RESET && hw->reset.level != HNS3_NONE_RESET &&
5651 hw->reset.level < reset) {
5652 hns3_warn(hw, "High level reset %d is request", reset);
5659 hns3_wait_hardware_ready(struct hns3_adapter *hns)
5661 struct hns3_hw *hw = &hns->hw;
5662 struct hns3_wait_data *wait_data = hw->reset.wait_data;
5665 if (wait_data->result == HNS3_WAIT_SUCCESS)
5667 else if (wait_data->result == HNS3_WAIT_TIMEOUT) {
5668 hns3_clock_gettime(&tv);
5669 hns3_warn(hw, "Reset step4 hardware not ready after reset time=%ld.%.6ld",
5670 tv.tv_sec, tv.tv_usec);
5672 } else if (wait_data->result == HNS3_WAIT_REQUEST)
5675 wait_data->hns = hns;
5676 wait_data->check_completion = is_pf_reset_done;
5677 wait_data->end_ms = (uint64_t)HNS3_RESET_WAIT_CNT *
5678 HNS3_RESET_WAIT_MS + hns3_clock_gettime_ms();
5679 wait_data->interval = HNS3_RESET_WAIT_MS * USEC_PER_MSEC;
5680 wait_data->count = HNS3_RESET_WAIT_CNT;
5681 wait_data->result = HNS3_WAIT_REQUEST;
5682 rte_eal_alarm_set(wait_data->interval, hns3_wait_callback, wait_data);
5687 hns3_func_reset_cmd(struct hns3_hw *hw, int func_id)
5689 struct hns3_cmd_desc desc;
5690 struct hns3_reset_cmd *req = (struct hns3_reset_cmd *)desc.data;
5692 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_CFG_RST_TRIGGER, false);
5693 hns3_set_bit(req->mac_func_reset, HNS3_CFG_RESET_FUNC_B, 1);
5694 req->fun_reset_vfid = func_id;
5696 return hns3_cmd_send(hw, &desc, 1);
5700 hns3_imp_reset_cmd(struct hns3_hw *hw)
5702 struct hns3_cmd_desc desc;
5704 hns3_cmd_setup_basic_desc(&desc, 0xFFFE, false);
5705 desc.data[0] = 0xeedd;
5707 return hns3_cmd_send(hw, &desc, 1);
5711 hns3_msix_process(struct hns3_adapter *hns, enum hns3_reset_level reset_level)
5713 struct hns3_hw *hw = &hns->hw;
5717 hns3_clock_gettime(&tv);
5718 if (hns3_read_dev(hw, HNS3_GLOBAL_RESET_REG) ||
5719 hns3_read_dev(hw, HNS3_FUN_RST_ING)) {
5720 hns3_warn(hw, "Don't process msix during resetting time=%ld.%.6ld",
5721 tv.tv_sec, tv.tv_usec);
5725 switch (reset_level) {
5726 case HNS3_IMP_RESET:
5727 hns3_imp_reset_cmd(hw);
5728 hns3_warn(hw, "IMP Reset requested time=%ld.%.6ld",
5729 tv.tv_sec, tv.tv_usec);
5731 case HNS3_GLOBAL_RESET:
5732 val = hns3_read_dev(hw, HNS3_GLOBAL_RESET_REG);
5733 hns3_set_bit(val, HNS3_GLOBAL_RESET_BIT, 1);
5734 hns3_write_dev(hw, HNS3_GLOBAL_RESET_REG, val);
5735 hns3_warn(hw, "Global Reset requested time=%ld.%.6ld",
5736 tv.tv_sec, tv.tv_usec);
5738 case HNS3_FUNC_RESET:
5739 hns3_warn(hw, "PF Reset requested time=%ld.%.6ld",
5740 tv.tv_sec, tv.tv_usec);
5741 /* schedule again to check later */
5742 hns3_atomic_set_bit(HNS3_FUNC_RESET, &hw->reset.pending);
5743 hns3_schedule_reset(hns);
5746 hns3_warn(hw, "Unsupported reset level: %d", reset_level);
5749 hns3_atomic_clear_bit(reset_level, &hw->reset.request);
5752 static enum hns3_reset_level
5753 hns3_get_reset_level(struct hns3_adapter *hns, uint64_t *levels)
5755 struct hns3_hw *hw = &hns->hw;
5756 enum hns3_reset_level reset_level = HNS3_NONE_RESET;
5758 /* Return the highest priority reset level amongst all */
5759 if (hns3_atomic_test_bit(HNS3_IMP_RESET, levels))
5760 reset_level = HNS3_IMP_RESET;
5761 else if (hns3_atomic_test_bit(HNS3_GLOBAL_RESET, levels))
5762 reset_level = HNS3_GLOBAL_RESET;
5763 else if (hns3_atomic_test_bit(HNS3_FUNC_RESET, levels))
5764 reset_level = HNS3_FUNC_RESET;
5765 else if (hns3_atomic_test_bit(HNS3_FLR_RESET, levels))
5766 reset_level = HNS3_FLR_RESET;
5768 if (hw->reset.level != HNS3_NONE_RESET && reset_level < hw->reset.level)
5769 return HNS3_NONE_RESET;
5775 hns3_record_imp_error(struct hns3_adapter *hns)
5777 struct hns3_hw *hw = &hns->hw;
5780 reg_val = hns3_read_dev(hw, HNS3_VECTOR0_OTER_EN_REG);
5781 if (hns3_get_bit(reg_val, HNS3_VECTOR0_IMP_RD_POISON_B)) {
5782 hns3_warn(hw, "Detected IMP RD poison!");
5783 hns3_set_bit(reg_val, HNS3_VECTOR0_IMP_RD_POISON_B, 0);
5784 hns3_write_dev(hw, HNS3_VECTOR0_OTER_EN_REG, reg_val);
5787 if (hns3_get_bit(reg_val, HNS3_VECTOR0_IMP_CMDQ_ERR_B)) {
5788 hns3_warn(hw, "Detected IMP CMDQ error!");
5789 hns3_set_bit(reg_val, HNS3_VECTOR0_IMP_CMDQ_ERR_B, 0);
5790 hns3_write_dev(hw, HNS3_VECTOR0_OTER_EN_REG, reg_val);
5795 hns3_prepare_reset(struct hns3_adapter *hns)
5797 struct hns3_hw *hw = &hns->hw;
5801 switch (hw->reset.level) {
5802 case HNS3_FUNC_RESET:
5803 ret = hns3_func_reset_cmd(hw, HNS3_PF_FUNC_ID);
5808 * After performaning pf reset, it is not necessary to do the
5809 * mailbox handling or send any command to firmware, because
5810 * any mailbox handling or command to firmware is only valid
5811 * after hns3_cmd_init is called.
5813 __atomic_store_n(&hw->reset.disable_cmd, 1, __ATOMIC_RELAXED);
5814 hw->reset.stats.request_cnt++;
5816 case HNS3_IMP_RESET:
5817 hns3_record_imp_error(hns);
5818 reg_val = hns3_read_dev(hw, HNS3_VECTOR0_OTER_EN_REG);
5819 hns3_write_dev(hw, HNS3_VECTOR0_OTER_EN_REG, reg_val |
5820 BIT(HNS3_VECTOR0_IMP_RESET_INT_B));
5829 hns3_set_rst_done(struct hns3_hw *hw)
5831 struct hns3_pf_rst_done_cmd *req;
5832 struct hns3_cmd_desc desc;
5834 req = (struct hns3_pf_rst_done_cmd *)desc.data;
5835 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_PF_RST_DONE, false);
5836 req->pf_rst_done |= HNS3_PF_RESET_DONE_BIT;
5837 return hns3_cmd_send(hw, &desc, 1);
5841 hns3_stop_service(struct hns3_adapter *hns)
5843 struct hns3_hw *hw = &hns->hw;
5844 struct rte_eth_dev *eth_dev;
5846 eth_dev = &rte_eth_devices[hw->data->port_id];
5847 hw->mac.link_status = RTE_ETH_LINK_DOWN;
5848 if (hw->adapter_state == HNS3_NIC_STARTED) {
5849 rte_eal_alarm_cancel(hns3_service_handler, eth_dev);
5850 hns3_update_linkstatus_and_event(hw, false);
5853 hns3_set_rxtx_function(eth_dev);
5855 /* Disable datapath on secondary process. */
5856 hns3_mp_req_stop_rxtx(eth_dev);
5857 rte_delay_ms(hw->cfg_max_queues);
5859 rte_spinlock_lock(&hw->lock);
5860 if (hns->hw.adapter_state == HNS3_NIC_STARTED ||
5861 hw->adapter_state == HNS3_NIC_STOPPING) {
5862 hns3_enable_all_queues(hw, false);
5864 hw->reset.mbuf_deferred_free = true;
5866 hw->reset.mbuf_deferred_free = false;
5869 * It is cumbersome for hardware to pick-and-choose entries for deletion
5870 * from table space. Hence, for function reset software intervention is
5871 * required to delete the entries
5873 if (__atomic_load_n(&hw->reset.disable_cmd, __ATOMIC_RELAXED) == 0)
5874 hns3_configure_all_mc_mac_addr(hns, true);
5875 rte_spinlock_unlock(&hw->lock);
5881 hns3_start_service(struct hns3_adapter *hns)
5883 struct hns3_hw *hw = &hns->hw;
5884 struct rte_eth_dev *eth_dev;
5886 if (hw->reset.level == HNS3_IMP_RESET ||
5887 hw->reset.level == HNS3_GLOBAL_RESET)
5888 hns3_set_rst_done(hw);
5889 eth_dev = &rte_eth_devices[hw->data->port_id];
5890 hns3_set_rxtx_function(eth_dev);
5891 hns3_mp_req_start_rxtx(eth_dev);
5892 if (hw->adapter_state == HNS3_NIC_STARTED) {
5894 * This API parent function already hold the hns3_hw.lock, the
5895 * hns3_service_handler may report lse, in bonding application
5896 * it will call driver's ops which may acquire the hns3_hw.lock
5897 * again, thus lead to deadlock.
5898 * We defer calls hns3_service_handler to avoid the deadlock.
5900 rte_eal_alarm_set(HNS3_SERVICE_QUICK_INTERVAL,
5901 hns3_service_handler, eth_dev);
5903 /* Enable interrupt of all rx queues before enabling queues */
5904 hns3_dev_all_rx_queue_intr_enable(hw, true);
5906 * Enable state of each rxq and txq will be recovered after
5907 * reset, so we need to restore them before enable all tqps;
5909 hns3_restore_tqp_enable_state(hw);
5911 * When finished the initialization, enable queues to receive
5912 * and transmit packets.
5914 hns3_enable_all_queues(hw, true);
5921 hns3_restore_conf(struct hns3_adapter *hns)
5923 struct hns3_hw *hw = &hns->hw;
5926 ret = hns3_configure_all_mac_addr(hns, false);
5930 ret = hns3_configure_all_mc_mac_addr(hns, false);
5934 ret = hns3_dev_promisc_restore(hns);
5938 ret = hns3_restore_vlan_table(hns);
5942 ret = hns3_restore_vlan_conf(hns);
5946 ret = hns3_restore_all_fdir_filter(hns);
5950 ret = hns3_restore_ptp(hns);
5954 ret = hns3_restore_rx_interrupt(hw);
5958 ret = hns3_restore_gro_conf(hw);
5962 ret = hns3_restore_fec(hw);
5966 if (hns->hw.adapter_state == HNS3_NIC_STARTED) {
5967 ret = hns3_do_start(hns, false);
5970 hns3_info(hw, "hns3 dev restart successful!");
5971 } else if (hw->adapter_state == HNS3_NIC_STOPPING)
5972 hw->adapter_state = HNS3_NIC_CONFIGURED;
5976 hns3_configure_all_mc_mac_addr(hns, true);
5978 hns3_configure_all_mac_addr(hns, true);
5983 hns3_reset_service(void *param)
5985 struct hns3_adapter *hns = (struct hns3_adapter *)param;
5986 struct hns3_hw *hw = &hns->hw;
5987 enum hns3_reset_level reset_level;
5988 struct timeval tv_delta;
5989 struct timeval tv_start;
5995 * The interrupt is not triggered within the delay time.
5996 * The interrupt may have been lost. It is necessary to handle
5997 * the interrupt to recover from the error.
5999 if (__atomic_load_n(&hw->reset.schedule, __ATOMIC_RELAXED) ==
6000 SCHEDULE_DEFERRED) {
6001 __atomic_store_n(&hw->reset.schedule, SCHEDULE_REQUESTED,
6003 hns3_err(hw, "Handling interrupts in delayed tasks");
6004 hns3_interrupt_handler(&rte_eth_devices[hw->data->port_id]);
6005 reset_level = hns3_get_reset_level(hns, &hw->reset.pending);
6006 if (reset_level == HNS3_NONE_RESET) {
6007 hns3_err(hw, "No reset level is set, try IMP reset");
6008 hns3_atomic_set_bit(HNS3_IMP_RESET, &hw->reset.pending);
6011 __atomic_store_n(&hw->reset.schedule, SCHEDULE_NONE, __ATOMIC_RELAXED);
6014 * Check if there is any ongoing reset in the hardware. This status can
6015 * be checked from reset_pending. If there is then, we need to wait for
6016 * hardware to complete reset.
6017 * a. If we are able to figure out in reasonable time that hardware
6018 * has fully resetted then, we can proceed with driver, client
6020 * b. else, we can come back later to check this status so re-sched
6023 reset_level = hns3_get_reset_level(hns, &hw->reset.pending);
6024 if (reset_level != HNS3_NONE_RESET) {
6025 hns3_clock_gettime(&tv_start);
6026 ret = hns3_reset_process(hns, reset_level);
6027 hns3_clock_gettime(&tv);
6028 timersub(&tv, &tv_start, &tv_delta);
6029 msec = hns3_clock_calctime_ms(&tv_delta);
6030 if (msec > HNS3_RESET_PROCESS_MS)
6031 hns3_err(hw, "%d handle long time delta %" PRIu64
6032 " ms time=%ld.%.6ld",
6033 hw->reset.level, msec,
6034 tv.tv_sec, tv.tv_usec);
6039 /* Check if we got any *new* reset requests to be honored */
6040 reset_level = hns3_get_reset_level(hns, &hw->reset.request);
6041 if (reset_level != HNS3_NONE_RESET)
6042 hns3_msix_process(hns, reset_level);
6046 hns3_get_speed_capa_num(uint16_t device_id)
6050 switch (device_id) {
6051 case HNS3_DEV_ID_25GE:
6052 case HNS3_DEV_ID_25GE_RDMA:
6055 case HNS3_DEV_ID_100G_RDMA_MACSEC:
6056 case HNS3_DEV_ID_200G_RDMA:
6068 hns3_get_speed_fec_capa(struct rte_eth_fec_capa *speed_fec_capa,
6071 switch (device_id) {
6072 case HNS3_DEV_ID_25GE:
6074 case HNS3_DEV_ID_25GE_RDMA:
6075 speed_fec_capa[0].speed = speed_fec_capa_tbl[1].speed;
6076 speed_fec_capa[0].capa = speed_fec_capa_tbl[1].capa;
6078 /* In HNS3 device, the 25G NIC is compatible with 10G rate */
6079 speed_fec_capa[1].speed = speed_fec_capa_tbl[0].speed;
6080 speed_fec_capa[1].capa = speed_fec_capa_tbl[0].capa;
6082 case HNS3_DEV_ID_100G_RDMA_MACSEC:
6083 speed_fec_capa[0].speed = speed_fec_capa_tbl[4].speed;
6084 speed_fec_capa[0].capa = speed_fec_capa_tbl[4].capa;
6086 case HNS3_DEV_ID_200G_RDMA:
6087 speed_fec_capa[0].speed = speed_fec_capa_tbl[5].speed;
6088 speed_fec_capa[0].capa = speed_fec_capa_tbl[5].capa;
6098 hns3_fec_get_capability(struct rte_eth_dev *dev,
6099 struct rte_eth_fec_capa *speed_fec_capa,
6102 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
6103 struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
6104 uint16_t device_id = pci_dev->id.device_id;
6105 unsigned int capa_num;
6108 capa_num = hns3_get_speed_capa_num(device_id);
6109 if (capa_num == 0) {
6110 hns3_err(hw, "device(0x%x) is not supported by hns3 PMD",
6115 if (speed_fec_capa == NULL || num < capa_num)
6118 ret = hns3_get_speed_fec_capa(speed_fec_capa, device_id);
6126 get_current_fec_auto_state(struct hns3_hw *hw, uint8_t *state)
6128 struct hns3_config_fec_cmd *req;
6129 struct hns3_cmd_desc desc;
6133 * CMD(HNS3_OPC_CONFIG_FEC_MODE) read is not supported
6134 * in device of link speed
6137 if (hw->mac.link_speed < RTE_ETH_SPEED_NUM_10G) {
6142 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_CONFIG_FEC_MODE, true);
6143 req = (struct hns3_config_fec_cmd *)desc.data;
6144 ret = hns3_cmd_send(hw, &desc, 1);
6146 hns3_err(hw, "get current fec auto state failed, ret = %d",
6151 *state = req->fec_mode & (1U << HNS3_MAC_CFG_FEC_AUTO_EN_B);
6156 hns3_fec_get_internal(struct hns3_hw *hw, uint32_t *fec_capa)
6158 struct hns3_sfp_info_cmd *resp;
6159 uint32_t tmp_fec_capa;
6161 struct hns3_cmd_desc desc;
6165 * If link is down and AUTO is enabled, AUTO is returned, otherwise,
6166 * configured FEC mode is returned.
6167 * If link is up, current FEC mode is returned.
6169 if (hw->mac.link_status == RTE_ETH_LINK_DOWN) {
6170 ret = get_current_fec_auto_state(hw, &auto_state);
6174 if (auto_state == 0x1) {
6175 *fec_capa = RTE_ETH_FEC_MODE_CAPA_MASK(AUTO);
6180 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_GET_SFP_INFO, true);
6181 resp = (struct hns3_sfp_info_cmd *)desc.data;
6182 resp->query_type = HNS3_ACTIVE_QUERY;
6184 ret = hns3_cmd_send(hw, &desc, 1);
6185 if (ret == -EOPNOTSUPP) {
6186 hns3_err(hw, "IMP do not support get FEC, ret = %d", ret);
6189 hns3_err(hw, "get FEC failed, ret = %d", ret);
6194 * FEC mode order defined in hns3 hardware is inconsistend with
6195 * that defined in the ethdev library. So the sequence needs
6198 switch (resp->active_fec) {
6199 case HNS3_HW_FEC_MODE_NOFEC:
6200 tmp_fec_capa = RTE_ETH_FEC_MODE_CAPA_MASK(NOFEC);
6202 case HNS3_HW_FEC_MODE_BASER:
6203 tmp_fec_capa = RTE_ETH_FEC_MODE_CAPA_MASK(BASER);
6205 case HNS3_HW_FEC_MODE_RS:
6206 tmp_fec_capa = RTE_ETH_FEC_MODE_CAPA_MASK(RS);
6209 tmp_fec_capa = RTE_ETH_FEC_MODE_CAPA_MASK(NOFEC);
6213 *fec_capa = tmp_fec_capa;
6218 hns3_fec_get(struct rte_eth_dev *dev, uint32_t *fec_capa)
6220 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
6222 return hns3_fec_get_internal(hw, fec_capa);
6226 hns3_set_fec_hw(struct hns3_hw *hw, uint32_t mode)
6228 struct hns3_config_fec_cmd *req;
6229 struct hns3_cmd_desc desc;
6232 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_CONFIG_FEC_MODE, false);
6234 req = (struct hns3_config_fec_cmd *)desc.data;
6236 case RTE_ETH_FEC_MODE_CAPA_MASK(NOFEC):
6237 hns3_set_field(req->fec_mode, HNS3_MAC_CFG_FEC_MODE_M,
6238 HNS3_MAC_CFG_FEC_MODE_S, HNS3_MAC_FEC_OFF);
6240 case RTE_ETH_FEC_MODE_CAPA_MASK(BASER):
6241 hns3_set_field(req->fec_mode, HNS3_MAC_CFG_FEC_MODE_M,
6242 HNS3_MAC_CFG_FEC_MODE_S, HNS3_MAC_FEC_BASER);
6244 case RTE_ETH_FEC_MODE_CAPA_MASK(RS):
6245 hns3_set_field(req->fec_mode, HNS3_MAC_CFG_FEC_MODE_M,
6246 HNS3_MAC_CFG_FEC_MODE_S, HNS3_MAC_FEC_RS);
6248 case RTE_ETH_FEC_MODE_CAPA_MASK(AUTO):
6249 hns3_set_bit(req->fec_mode, HNS3_MAC_CFG_FEC_AUTO_EN_B, 1);
6254 ret = hns3_cmd_send(hw, &desc, 1);
6256 hns3_err(hw, "set fec mode failed, ret = %d", ret);
6262 get_current_speed_fec_cap(struct hns3_hw *hw, struct rte_eth_fec_capa *fec_capa)
6264 struct hns3_mac *mac = &hw->mac;
6267 switch (mac->link_speed) {
6268 case RTE_ETH_SPEED_NUM_10G:
6269 cur_capa = fec_capa[1].capa;
6271 case RTE_ETH_SPEED_NUM_25G:
6272 case RTE_ETH_SPEED_NUM_100G:
6273 case RTE_ETH_SPEED_NUM_200G:
6274 cur_capa = fec_capa[0].capa;
6285 is_fec_mode_one_bit_set(uint32_t mode)
6290 for (i = 0; i < sizeof(mode); i++)
6291 if (mode >> i & 0x1)
6294 return cnt == 1 ? true : false;
6298 hns3_fec_set(struct rte_eth_dev *dev, uint32_t mode)
6300 #define FEC_CAPA_NUM 2
6301 struct hns3_adapter *hns = dev->data->dev_private;
6302 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(hns);
6303 struct hns3_pf *pf = &hns->pf;
6305 struct rte_eth_fec_capa fec_capa[FEC_CAPA_NUM];
6307 uint32_t num = FEC_CAPA_NUM;
6310 ret = hns3_fec_get_capability(dev, fec_capa, num);
6314 /* HNS3 PMD only support one bit set mode, e.g. 0x1, 0x4 */
6315 if (!is_fec_mode_one_bit_set(mode)) {
6316 hns3_err(hw, "FEC mode(0x%x) not supported in HNS3 PMD, "
6317 "FEC mode should be only one bit set", mode);
6322 * Check whether the configured mode is within the FEC capability.
6323 * If not, the configured mode will not be supported.
6325 cur_capa = get_current_speed_fec_cap(hw, fec_capa);
6326 if (!(cur_capa & mode)) {
6327 hns3_err(hw, "unsupported FEC mode = 0x%x", mode);
6331 rte_spinlock_lock(&hw->lock);
6332 ret = hns3_set_fec_hw(hw, mode);
6334 rte_spinlock_unlock(&hw->lock);
6338 pf->fec_mode = mode;
6339 rte_spinlock_unlock(&hw->lock);
6345 hns3_restore_fec(struct hns3_hw *hw)
6347 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
6348 struct hns3_pf *pf = &hns->pf;
6349 uint32_t mode = pf->fec_mode;
6352 ret = hns3_set_fec_hw(hw, mode);
6354 hns3_err(hw, "restore fec mode(0x%x) failed, ret = %d",
6361 hns3_query_dev_fec_info(struct hns3_hw *hw)
6363 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
6364 struct hns3_pf *pf = HNS3_DEV_PRIVATE_TO_PF(hns);
6367 ret = hns3_fec_get_internal(hw, &pf->fec_mode);
6369 hns3_err(hw, "query device FEC info failed, ret = %d", ret);
6375 hns3_optical_module_existed(struct hns3_hw *hw)
6377 struct hns3_cmd_desc desc;
6381 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_GET_SFP_EXIST, true);
6382 ret = hns3_cmd_send(hw, &desc, 1);
6385 "fail to get optical module exist state, ret = %d.\n",
6389 existed = !!desc.data[0];
6395 hns3_get_module_eeprom_data(struct hns3_hw *hw, uint32_t offset,
6396 uint32_t len, uint8_t *data)
6398 #define HNS3_SFP_INFO_CMD_NUM 6
6399 #define HNS3_SFP_INFO_MAX_LEN \
6400 (HNS3_SFP_INFO_BD0_LEN + \
6401 (HNS3_SFP_INFO_CMD_NUM - 1) * HNS3_SFP_INFO_BDX_LEN)
6402 struct hns3_cmd_desc desc[HNS3_SFP_INFO_CMD_NUM];
6403 struct hns3_sfp_info_bd0_cmd *sfp_info_bd0;
6409 for (i = 0; i < HNS3_SFP_INFO_CMD_NUM; i++) {
6410 hns3_cmd_setup_basic_desc(&desc[i], HNS3_OPC_GET_SFP_EEPROM,
6412 if (i < HNS3_SFP_INFO_CMD_NUM - 1)
6413 desc[i].flag |= rte_cpu_to_le_16(HNS3_CMD_FLAG_NEXT);
6416 sfp_info_bd0 = (struct hns3_sfp_info_bd0_cmd *)desc[0].data;
6417 sfp_info_bd0->offset = rte_cpu_to_le_16((uint16_t)offset);
6418 read_len = RTE_MIN(len, HNS3_SFP_INFO_MAX_LEN);
6419 sfp_info_bd0->read_len = rte_cpu_to_le_16((uint16_t)read_len);
6421 ret = hns3_cmd_send(hw, desc, HNS3_SFP_INFO_CMD_NUM);
6423 hns3_err(hw, "fail to get module EEPROM info, ret = %d.\n",
6428 /* The data format in BD0 is different with the others. */
6429 copy_len = RTE_MIN(len, HNS3_SFP_INFO_BD0_LEN);
6430 memcpy(data, sfp_info_bd0->data, copy_len);
6431 read_len = copy_len;
6433 for (i = 1; i < HNS3_SFP_INFO_CMD_NUM; i++) {
6434 if (read_len >= len)
6437 copy_len = RTE_MIN(len - read_len, HNS3_SFP_INFO_BDX_LEN);
6438 memcpy(data + read_len, desc[i].data, copy_len);
6439 read_len += copy_len;
6442 return (int)read_len;
6446 hns3_get_module_eeprom(struct rte_eth_dev *dev,
6447 struct rte_dev_eeprom_info *info)
6449 struct hns3_adapter *hns = dev->data->dev_private;
6450 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(hns);
6451 uint32_t offset = info->offset;
6452 uint32_t len = info->length;
6453 uint8_t *data = info->data;
6454 uint32_t read_len = 0;
6456 if (hw->mac.media_type != HNS3_MEDIA_TYPE_FIBER)
6459 if (!hns3_optical_module_existed(hw)) {
6460 hns3_err(hw, "fail to read module EEPROM: no module is connected.\n");
6464 while (read_len < len) {
6466 ret = hns3_get_module_eeprom_data(hw, offset + read_len,
6478 hns3_get_module_info(struct rte_eth_dev *dev,
6479 struct rte_eth_dev_module_info *modinfo)
6481 #define HNS3_SFF8024_ID_SFP 0x03
6482 #define HNS3_SFF8024_ID_QSFP_8438 0x0c
6483 #define HNS3_SFF8024_ID_QSFP_8436_8636 0x0d
6484 #define HNS3_SFF8024_ID_QSFP28_8636 0x11
6485 #define HNS3_SFF_8636_V1_3 0x03
6486 struct hns3_adapter *hns = dev->data->dev_private;
6487 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(hns);
6488 struct rte_dev_eeprom_info info;
6489 struct hns3_sfp_type sfp_type;
6492 memset(&sfp_type, 0, sizeof(sfp_type));
6493 memset(&info, 0, sizeof(info));
6494 info.data = (uint8_t *)&sfp_type;
6495 info.length = sizeof(sfp_type);
6496 ret = hns3_get_module_eeprom(dev, &info);
6500 switch (sfp_type.type) {
6501 case HNS3_SFF8024_ID_SFP:
6502 modinfo->type = RTE_ETH_MODULE_SFF_8472;
6503 modinfo->eeprom_len = RTE_ETH_MODULE_SFF_8472_LEN;
6505 case HNS3_SFF8024_ID_QSFP_8438:
6506 modinfo->type = RTE_ETH_MODULE_SFF_8436;
6507 modinfo->eeprom_len = RTE_ETH_MODULE_SFF_8436_MAX_LEN;
6509 case HNS3_SFF8024_ID_QSFP_8436_8636:
6510 if (sfp_type.ext_type < HNS3_SFF_8636_V1_3) {
6511 modinfo->type = RTE_ETH_MODULE_SFF_8436;
6512 modinfo->eeprom_len = RTE_ETH_MODULE_SFF_8436_MAX_LEN;
6514 modinfo->type = RTE_ETH_MODULE_SFF_8636;
6515 modinfo->eeprom_len = RTE_ETH_MODULE_SFF_8636_MAX_LEN;
6518 case HNS3_SFF8024_ID_QSFP28_8636:
6519 modinfo->type = RTE_ETH_MODULE_SFF_8636;
6520 modinfo->eeprom_len = RTE_ETH_MODULE_SFF_8636_MAX_LEN;
6523 hns3_err(hw, "unknown module, type = %u, extra_type = %u.\n",
6524 sfp_type.type, sfp_type.ext_type);
6531 static const struct eth_dev_ops hns3_eth_dev_ops = {
6532 .dev_configure = hns3_dev_configure,
6533 .dev_start = hns3_dev_start,
6534 .dev_stop = hns3_dev_stop,
6535 .dev_close = hns3_dev_close,
6536 .promiscuous_enable = hns3_dev_promiscuous_enable,
6537 .promiscuous_disable = hns3_dev_promiscuous_disable,
6538 .allmulticast_enable = hns3_dev_allmulticast_enable,
6539 .allmulticast_disable = hns3_dev_allmulticast_disable,
6540 .mtu_set = hns3_dev_mtu_set,
6541 .stats_get = hns3_stats_get,
6542 .stats_reset = hns3_stats_reset,
6543 .xstats_get = hns3_dev_xstats_get,
6544 .xstats_get_names = hns3_dev_xstats_get_names,
6545 .xstats_reset = hns3_dev_xstats_reset,
6546 .xstats_get_by_id = hns3_dev_xstats_get_by_id,
6547 .xstats_get_names_by_id = hns3_dev_xstats_get_names_by_id,
6548 .dev_infos_get = hns3_dev_infos_get,
6549 .fw_version_get = hns3_fw_version_get,
6550 .rx_queue_setup = hns3_rx_queue_setup,
6551 .tx_queue_setup = hns3_tx_queue_setup,
6552 .rx_queue_release = hns3_dev_rx_queue_release,
6553 .tx_queue_release = hns3_dev_tx_queue_release,
6554 .rx_queue_start = hns3_dev_rx_queue_start,
6555 .rx_queue_stop = hns3_dev_rx_queue_stop,
6556 .tx_queue_start = hns3_dev_tx_queue_start,
6557 .tx_queue_stop = hns3_dev_tx_queue_stop,
6558 .rx_queue_intr_enable = hns3_dev_rx_queue_intr_enable,
6559 .rx_queue_intr_disable = hns3_dev_rx_queue_intr_disable,
6560 .rxq_info_get = hns3_rxq_info_get,
6561 .txq_info_get = hns3_txq_info_get,
6562 .rx_burst_mode_get = hns3_rx_burst_mode_get,
6563 .tx_burst_mode_get = hns3_tx_burst_mode_get,
6564 .flow_ctrl_get = hns3_flow_ctrl_get,
6565 .flow_ctrl_set = hns3_flow_ctrl_set,
6566 .priority_flow_ctrl_set = hns3_priority_flow_ctrl_set,
6567 .mac_addr_add = hns3_add_mac_addr,
6568 .mac_addr_remove = hns3_remove_mac_addr,
6569 .mac_addr_set = hns3_set_default_mac_addr,
6570 .set_mc_addr_list = hns3_set_mc_mac_addr_list,
6571 .link_update = hns3_dev_link_update,
6572 .dev_set_link_up = hns3_dev_set_link_up,
6573 .dev_set_link_down = hns3_dev_set_link_down,
6574 .rss_hash_update = hns3_dev_rss_hash_update,
6575 .rss_hash_conf_get = hns3_dev_rss_hash_conf_get,
6576 .reta_update = hns3_dev_rss_reta_update,
6577 .reta_query = hns3_dev_rss_reta_query,
6578 .flow_ops_get = hns3_dev_flow_ops_get,
6579 .vlan_filter_set = hns3_vlan_filter_set,
6580 .vlan_tpid_set = hns3_vlan_tpid_set,
6581 .vlan_offload_set = hns3_vlan_offload_set,
6582 .vlan_pvid_set = hns3_vlan_pvid_set,
6583 .get_reg = hns3_get_regs,
6584 .get_module_info = hns3_get_module_info,
6585 .get_module_eeprom = hns3_get_module_eeprom,
6586 .get_dcb_info = hns3_get_dcb_info,
6587 .dev_supported_ptypes_get = hns3_dev_supported_ptypes_get,
6588 .fec_get_capability = hns3_fec_get_capability,
6589 .fec_get = hns3_fec_get,
6590 .fec_set = hns3_fec_set,
6591 .tm_ops_get = hns3_tm_ops_get,
6592 .tx_done_cleanup = hns3_tx_done_cleanup,
6593 .timesync_enable = hns3_timesync_enable,
6594 .timesync_disable = hns3_timesync_disable,
6595 .timesync_read_rx_timestamp = hns3_timesync_read_rx_timestamp,
6596 .timesync_read_tx_timestamp = hns3_timesync_read_tx_timestamp,
6597 .timesync_adjust_time = hns3_timesync_adjust_time,
6598 .timesync_read_time = hns3_timesync_read_time,
6599 .timesync_write_time = hns3_timesync_write_time,
6602 static const struct hns3_reset_ops hns3_reset_ops = {
6603 .reset_service = hns3_reset_service,
6604 .stop_service = hns3_stop_service,
6605 .prepare_reset = hns3_prepare_reset,
6606 .wait_hardware_ready = hns3_wait_hardware_ready,
6607 .reinit_dev = hns3_reinit_dev,
6608 .restore_conf = hns3_restore_conf,
6609 .start_service = hns3_start_service,
6613 hns3_init_hw_ops(struct hns3_hw *hw)
6615 hw->ops.add_mc_mac_addr = hns3_add_mc_mac_addr;
6616 hw->ops.del_mc_mac_addr = hns3_remove_mc_mac_addr;
6617 hw->ops.add_uc_mac_addr = hns3_add_uc_mac_addr;
6618 hw->ops.del_uc_mac_addr = hns3_remove_uc_mac_addr;
6619 hw->ops.bind_ring_with_vector = hns3_bind_ring_with_vector;
6623 hns3_dev_init(struct rte_eth_dev *eth_dev)
6625 struct hns3_adapter *hns = eth_dev->data->dev_private;
6626 char mac_str[RTE_ETHER_ADDR_FMT_SIZE];
6627 struct rte_ether_addr *eth_addr;
6628 struct hns3_hw *hw = &hns->hw;
6631 PMD_INIT_FUNC_TRACE();
6633 hns3_flow_init(eth_dev);
6635 hns3_set_rxtx_function(eth_dev);
6636 eth_dev->dev_ops = &hns3_eth_dev_ops;
6637 eth_dev->rx_queue_count = hns3_rx_queue_count;
6638 ret = hns3_mp_init(eth_dev);
6642 if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
6643 hns3_tx_push_init(eth_dev);
6647 hw->adapter_state = HNS3_NIC_UNINITIALIZED;
6649 hw->data = eth_dev->data;
6650 hns3_parse_devargs(eth_dev);
6653 * Set default max packet size according to the mtu
6654 * default vale in DPDK frame.
6656 hns->pf.mps = hw->data->mtu + HNS3_ETH_OVERHEAD;
6658 ret = hns3_reset_init(hw);
6660 goto err_init_reset;
6661 hw->reset.ops = &hns3_reset_ops;
6663 hns3_init_hw_ops(hw);
6664 ret = hns3_init_pf(eth_dev);
6666 PMD_INIT_LOG(ERR, "Failed to init pf: %d", ret);
6670 /* Allocate memory for storing MAC addresses */
6671 eth_dev->data->mac_addrs = rte_zmalloc("hns3-mac",
6672 sizeof(struct rte_ether_addr) *
6673 HNS3_UC_MACADDR_NUM, 0);
6674 if (eth_dev->data->mac_addrs == NULL) {
6675 PMD_INIT_LOG(ERR, "Failed to allocate %zx bytes needed "
6676 "to store MAC addresses",
6677 sizeof(struct rte_ether_addr) *
6678 HNS3_UC_MACADDR_NUM);
6680 goto err_rte_zmalloc;
6683 eth_addr = (struct rte_ether_addr *)hw->mac.mac_addr;
6684 if (!rte_is_valid_assigned_ether_addr(eth_addr)) {
6685 rte_eth_random_addr(hw->mac.mac_addr);
6686 hns3_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
6687 (struct rte_ether_addr *)hw->mac.mac_addr);
6688 hns3_warn(hw, "default mac_addr from firmware is an invalid "
6689 "unicast address, using random MAC address %s",
6692 rte_ether_addr_copy((struct rte_ether_addr *)hw->mac.mac_addr,
6693 ð_dev->data->mac_addrs[0]);
6695 hw->adapter_state = HNS3_NIC_INITIALIZED;
6697 if (__atomic_load_n(&hw->reset.schedule, __ATOMIC_RELAXED) ==
6699 hns3_err(hw, "Reschedule reset service after dev_init");
6700 hns3_schedule_reset(hns);
6702 /* IMP will wait ready flag before reset */
6703 hns3_notify_reset_ready(hw, false);
6706 hns3_info(hw, "hns3 dev initialization successful!");
6710 hns3_uninit_pf(eth_dev);
6713 rte_free(hw->reset.wait_data);
6716 hns3_mp_uninit(eth_dev);
6719 eth_dev->dev_ops = NULL;
6720 eth_dev->rx_pkt_burst = NULL;
6721 eth_dev->rx_descriptor_status = NULL;
6722 eth_dev->tx_pkt_burst = NULL;
6723 eth_dev->tx_pkt_prepare = NULL;
6724 eth_dev->tx_descriptor_status = NULL;
6729 hns3_dev_uninit(struct rte_eth_dev *eth_dev)
6731 struct hns3_adapter *hns = eth_dev->data->dev_private;
6732 struct hns3_hw *hw = &hns->hw;
6734 PMD_INIT_FUNC_TRACE();
6736 if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
6737 hns3_mp_uninit(eth_dev);
6741 if (hw->adapter_state < HNS3_NIC_CLOSING)
6742 hns3_dev_close(eth_dev);
6744 hw->adapter_state = HNS3_NIC_REMOVED;
6749 eth_hns3_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
6750 struct rte_pci_device *pci_dev)
6752 return rte_eth_dev_pci_generic_probe(pci_dev,
6753 sizeof(struct hns3_adapter),
6758 eth_hns3_pci_remove(struct rte_pci_device *pci_dev)
6760 return rte_eth_dev_pci_generic_remove(pci_dev, hns3_dev_uninit);
6763 static const struct rte_pci_id pci_id_hns3_map[] = {
6764 { RTE_PCI_DEVICE(PCI_VENDOR_ID_HUAWEI, HNS3_DEV_ID_GE) },
6765 { RTE_PCI_DEVICE(PCI_VENDOR_ID_HUAWEI, HNS3_DEV_ID_25GE) },
6766 { RTE_PCI_DEVICE(PCI_VENDOR_ID_HUAWEI, HNS3_DEV_ID_25GE_RDMA) },
6767 { RTE_PCI_DEVICE(PCI_VENDOR_ID_HUAWEI, HNS3_DEV_ID_50GE_RDMA) },
6768 { RTE_PCI_DEVICE(PCI_VENDOR_ID_HUAWEI, HNS3_DEV_ID_100G_RDMA_MACSEC) },
6769 { RTE_PCI_DEVICE(PCI_VENDOR_ID_HUAWEI, HNS3_DEV_ID_200G_RDMA) },
6770 { .vendor_id = 0, }, /* sentinel */
6773 static struct rte_pci_driver rte_hns3_pmd = {
6774 .id_table = pci_id_hns3_map,
6775 .drv_flags = RTE_PCI_DRV_NEED_MAPPING | RTE_PCI_DRV_INTR_LSC,
6776 .probe = eth_hns3_pci_probe,
6777 .remove = eth_hns3_pci_remove,
6780 RTE_PMD_REGISTER_PCI(net_hns3, rte_hns3_pmd);
6781 RTE_PMD_REGISTER_PCI_TABLE(net_hns3, pci_id_hns3_map);
6782 RTE_PMD_REGISTER_KMOD_DEP(net_hns3, "* igb_uio | vfio-pci");
6783 RTE_PMD_REGISTER_PARAM_STRING(net_hns3,
6784 HNS3_DEVARG_RX_FUNC_HINT "=vec|sve|simple|common "
6785 HNS3_DEVARG_TX_FUNC_HINT "=vec|sve|simple|common "
6786 HNS3_DEVARG_DEV_CAPS_MASK "=<1-65535> "
6787 HNS3_DEVARG_MBX_TIME_LIMIT_MS "=<uint16> ");
6788 RTE_LOG_REGISTER_SUFFIX(hns3_logtype_init, init, NOTICE);
6789 RTE_LOG_REGISTER_SUFFIX(hns3_logtype_driver, driver, NOTICE);