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 driver.
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_init_ring_with_vector(struct hns3_hw *hw)
1940 * In hns3 network engine, vector 0 is always the misc interrupt of this
1941 * function, vector 1~N can be used respectively for the queues of the
1942 * function. Tx and Rx queues with the same number share the interrupt
1943 * vector. In the initialization clearing the all hardware mapping
1944 * relationship configurations between queues and interrupt vectors is
1945 * needed, so some error caused by the residual configurations, such as
1946 * the unexpected Tx interrupt, can be avoid.
1948 vec = hw->num_msi - 1; /* vector 0 for misc interrupt, not for queue */
1949 if (hw->intr.mapping_mode == HNS3_INTR_MAPPING_VEC_RSV_ONE)
1950 vec = vec - 1; /* the last interrupt is reserved */
1951 hw->intr_tqps_num = RTE_MIN(vec, hw->tqps_num);
1952 for (i = 0; i < hw->intr_tqps_num; i++) {
1954 * Set gap limiter/rate limiter/quanity limiter algorithm
1955 * configuration for interrupt coalesce of queue's interrupt.
1957 hns3_set_queue_intr_gl(hw, i, HNS3_RING_GL_RX,
1958 HNS3_TQP_INTR_GL_DEFAULT);
1959 hns3_set_queue_intr_gl(hw, i, HNS3_RING_GL_TX,
1960 HNS3_TQP_INTR_GL_DEFAULT);
1961 hns3_set_queue_intr_rl(hw, i, HNS3_TQP_INTR_RL_DEFAULT);
1963 * QL(quantity limiter) is not used currently, just set 0 to
1966 hns3_set_queue_intr_ql(hw, i, HNS3_TQP_INTR_QL_DEFAULT);
1968 ret = hns3_bind_ring_with_vector(hw, vec, false,
1969 HNS3_RING_TYPE_TX, i);
1971 PMD_INIT_LOG(ERR, "PF fail to unbind TX ring(%d) with "
1972 "vector: %u, ret=%d", i, vec, ret);
1976 ret = hns3_bind_ring_with_vector(hw, vec, false,
1977 HNS3_RING_TYPE_RX, i);
1979 PMD_INIT_LOG(ERR, "PF fail to unbind RX ring(%d) with "
1980 "vector: %u, ret=%d", i, vec, ret);
1989 hns3_setup_dcb(struct rte_eth_dev *dev)
1991 struct hns3_adapter *hns = dev->data->dev_private;
1992 struct hns3_hw *hw = &hns->hw;
1995 if (!hns3_dev_get_support(hw, DCB)) {
1996 hns3_err(hw, "this port does not support dcb configurations.");
2000 if (hw->current_fc_status == HNS3_FC_STATUS_MAC_PAUSE) {
2001 hns3_err(hw, "MAC pause enabled, cannot config dcb info.");
2005 ret = hns3_dcb_configure(hns);
2007 hns3_err(hw, "failed to config dcb: %d", ret);
2013 hns3_check_link_speed(struct hns3_hw *hw, uint32_t link_speeds)
2018 * Some hardware doesn't support auto-negotiation, but users may not
2019 * configure link_speeds (default 0), which means auto-negotiation.
2020 * In this case, it should return success.
2022 if (link_speeds == RTE_ETH_LINK_SPEED_AUTONEG &&
2023 hw->mac.support_autoneg == 0)
2026 if (link_speeds != RTE_ETH_LINK_SPEED_AUTONEG) {
2027 ret = hns3_check_port_speed(hw, link_speeds);
2036 hns3_check_dev_conf(struct rte_eth_dev *dev)
2038 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2039 struct rte_eth_conf *conf = &dev->data->dev_conf;
2042 ret = hns3_check_mq_mode(dev);
2046 return hns3_check_link_speed(hw, conf->link_speeds);
2050 hns3_dev_configure(struct rte_eth_dev *dev)
2052 struct hns3_adapter *hns = dev->data->dev_private;
2053 struct rte_eth_conf *conf = &dev->data->dev_conf;
2054 enum rte_eth_rx_mq_mode mq_mode = conf->rxmode.mq_mode;
2055 struct hns3_hw *hw = &hns->hw;
2056 uint16_t nb_rx_q = dev->data->nb_rx_queues;
2057 uint16_t nb_tx_q = dev->data->nb_tx_queues;
2058 struct rte_eth_rss_conf rss_conf;
2062 hw->cfg_max_queues = RTE_MAX(nb_rx_q, nb_tx_q);
2065 * Some versions of hardware network engine does not support
2066 * individually enable/disable/reset the Tx or Rx queue. These devices
2067 * must enable/disable/reset Tx and Rx queues at the same time. When the
2068 * numbers of Tx queues allocated by upper applications are not equal to
2069 * the numbers of Rx queues, driver needs to setup fake Tx or Rx queues
2070 * to adjust numbers of Tx/Rx queues. otherwise, network engine can not
2071 * work as usual. But these fake queues are imperceptible, and can not
2072 * be used by upper applications.
2074 ret = hns3_set_fake_rx_or_tx_queues(dev, nb_rx_q, nb_tx_q);
2076 hns3_err(hw, "fail to set Rx/Tx fake queues, ret = %d.", ret);
2077 hw->cfg_max_queues = 0;
2081 hw->adapter_state = HNS3_NIC_CONFIGURING;
2082 ret = hns3_check_dev_conf(dev);
2086 if ((uint32_t)mq_mode & RTE_ETH_MQ_RX_DCB_FLAG) {
2087 ret = hns3_setup_dcb(dev);
2092 /* When RSS is not configured, redirect the packet queue 0 */
2093 if ((uint32_t)mq_mode & RTE_ETH_MQ_RX_RSS_FLAG) {
2094 conf->rxmode.offloads |= RTE_ETH_RX_OFFLOAD_RSS_HASH;
2095 rss_conf = conf->rx_adv_conf.rss_conf;
2096 hw->rss_dis_flag = false;
2097 ret = hns3_dev_rss_hash_update(dev, &rss_conf);
2102 ret = hns3_dev_mtu_set(dev, conf->rxmode.mtu);
2106 ret = hns3_mbuf_dyn_rx_timestamp_register(dev, conf);
2110 ret = hns3_dev_configure_vlan(dev);
2114 /* config hardware GRO */
2115 gro_en = conf->rxmode.offloads & RTE_ETH_RX_OFFLOAD_TCP_LRO ? true : false;
2116 ret = hns3_config_gro(hw, gro_en);
2120 hns3_init_rx_ptype_tble(dev);
2121 hw->adapter_state = HNS3_NIC_CONFIGURED;
2126 hw->cfg_max_queues = 0;
2127 (void)hns3_set_fake_rx_or_tx_queues(dev, 0, 0);
2128 hw->adapter_state = HNS3_NIC_INITIALIZED;
2134 hns3_set_mac_mtu(struct hns3_hw *hw, uint16_t new_mps)
2136 struct hns3_config_max_frm_size_cmd *req;
2137 struct hns3_cmd_desc desc;
2139 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_CONFIG_MAX_FRM_SIZE, false);
2141 req = (struct hns3_config_max_frm_size_cmd *)desc.data;
2142 req->max_frm_size = rte_cpu_to_le_16(new_mps);
2143 req->min_frm_size = RTE_ETHER_MIN_LEN;
2145 return hns3_cmd_send(hw, &desc, 1);
2149 hns3_config_mtu(struct hns3_hw *hw, uint16_t mps)
2151 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
2152 uint16_t original_mps = hns->pf.mps;
2156 ret = hns3_set_mac_mtu(hw, mps);
2158 hns3_err(hw, "failed to set mtu, ret = %d", ret);
2163 ret = hns3_buffer_alloc(hw);
2165 hns3_err(hw, "failed to allocate buffer, ret = %d", ret);
2172 err = hns3_set_mac_mtu(hw, original_mps);
2174 hns3_err(hw, "fail to rollback MTU, err = %d", err);
2177 hns->pf.mps = original_mps;
2183 hns3_dev_mtu_set(struct rte_eth_dev *dev, uint16_t mtu)
2185 struct hns3_adapter *hns = dev->data->dev_private;
2186 uint32_t frame_size = mtu + HNS3_ETH_OVERHEAD;
2187 struct hns3_hw *hw = &hns->hw;
2190 if (dev->data->dev_started) {
2191 hns3_err(hw, "Failed to set mtu, port %u must be stopped "
2192 "before configuration", dev->data->port_id);
2196 rte_spinlock_lock(&hw->lock);
2197 frame_size = RTE_MAX(frame_size, HNS3_DEFAULT_FRAME_LEN);
2200 * Maximum value of frame_size is HNS3_MAX_FRAME_LEN, so it can safely
2201 * assign to "uint16_t" type variable.
2203 ret = hns3_config_mtu(hw, (uint16_t)frame_size);
2205 rte_spinlock_unlock(&hw->lock);
2206 hns3_err(hw, "Failed to set mtu, port %u mtu %u: %d",
2207 dev->data->port_id, mtu, ret);
2211 rte_spinlock_unlock(&hw->lock);
2217 hns3_get_copper_port_speed_capa(uint32_t supported_speed)
2219 uint32_t speed_capa = 0;
2221 if (supported_speed & HNS3_PHY_LINK_SPEED_10M_HD_BIT)
2222 speed_capa |= RTE_ETH_LINK_SPEED_10M_HD;
2223 if (supported_speed & HNS3_PHY_LINK_SPEED_10M_BIT)
2224 speed_capa |= RTE_ETH_LINK_SPEED_10M;
2225 if (supported_speed & HNS3_PHY_LINK_SPEED_100M_HD_BIT)
2226 speed_capa |= RTE_ETH_LINK_SPEED_100M_HD;
2227 if (supported_speed & HNS3_PHY_LINK_SPEED_100M_BIT)
2228 speed_capa |= RTE_ETH_LINK_SPEED_100M;
2229 if (supported_speed & HNS3_PHY_LINK_SPEED_1000M_BIT)
2230 speed_capa |= RTE_ETH_LINK_SPEED_1G;
2236 hns3_get_firber_port_speed_capa(uint32_t supported_speed)
2238 uint32_t speed_capa = 0;
2240 if (supported_speed & HNS3_FIBER_LINK_SPEED_1G_BIT)
2241 speed_capa |= RTE_ETH_LINK_SPEED_1G;
2242 if (supported_speed & HNS3_FIBER_LINK_SPEED_10G_BIT)
2243 speed_capa |= RTE_ETH_LINK_SPEED_10G;
2244 if (supported_speed & HNS3_FIBER_LINK_SPEED_25G_BIT)
2245 speed_capa |= RTE_ETH_LINK_SPEED_25G;
2246 if (supported_speed & HNS3_FIBER_LINK_SPEED_40G_BIT)
2247 speed_capa |= RTE_ETH_LINK_SPEED_40G;
2248 if (supported_speed & HNS3_FIBER_LINK_SPEED_50G_BIT)
2249 speed_capa |= RTE_ETH_LINK_SPEED_50G;
2250 if (supported_speed & HNS3_FIBER_LINK_SPEED_100G_BIT)
2251 speed_capa |= RTE_ETH_LINK_SPEED_100G;
2252 if (supported_speed & HNS3_FIBER_LINK_SPEED_200G_BIT)
2253 speed_capa |= RTE_ETH_LINK_SPEED_200G;
2259 hns3_get_speed_capa(struct hns3_hw *hw)
2261 struct hns3_mac *mac = &hw->mac;
2262 uint32_t speed_capa;
2264 if (mac->media_type == HNS3_MEDIA_TYPE_COPPER)
2266 hns3_get_copper_port_speed_capa(mac->supported_speed);
2269 hns3_get_firber_port_speed_capa(mac->supported_speed);
2271 if (mac->support_autoneg == 0)
2272 speed_capa |= RTE_ETH_LINK_SPEED_FIXED;
2278 hns3_dev_infos_get(struct rte_eth_dev *eth_dev, struct rte_eth_dev_info *info)
2280 struct hns3_adapter *hns = eth_dev->data->dev_private;
2281 struct hns3_hw *hw = &hns->hw;
2282 uint16_t queue_num = hw->tqps_num;
2285 * In interrupt mode, 'max_rx_queues' is set based on the number of
2286 * MSI-X interrupt resources of the hardware.
2288 if (hw->data->dev_conf.intr_conf.rxq == 1)
2289 queue_num = hw->intr_tqps_num;
2291 info->max_rx_queues = queue_num;
2292 info->max_tx_queues = hw->tqps_num;
2293 info->max_rx_pktlen = HNS3_MAX_FRAME_LEN; /* CRC included */
2294 info->min_rx_bufsize = HNS3_MIN_BD_BUF_SIZE;
2295 info->max_mac_addrs = HNS3_UC_MACADDR_NUM;
2296 info->max_mtu = info->max_rx_pktlen - HNS3_ETH_OVERHEAD;
2297 info->max_lro_pkt_size = HNS3_MAX_LRO_SIZE;
2298 info->rx_offload_capa = (RTE_ETH_RX_OFFLOAD_IPV4_CKSUM |
2299 RTE_ETH_RX_OFFLOAD_TCP_CKSUM |
2300 RTE_ETH_RX_OFFLOAD_UDP_CKSUM |
2301 RTE_ETH_RX_OFFLOAD_SCTP_CKSUM |
2302 RTE_ETH_RX_OFFLOAD_OUTER_IPV4_CKSUM |
2303 RTE_ETH_RX_OFFLOAD_OUTER_UDP_CKSUM |
2304 RTE_ETH_RX_OFFLOAD_KEEP_CRC |
2305 RTE_ETH_RX_OFFLOAD_SCATTER |
2306 RTE_ETH_RX_OFFLOAD_VLAN_STRIP |
2307 RTE_ETH_RX_OFFLOAD_VLAN_FILTER |
2308 RTE_ETH_RX_OFFLOAD_RSS_HASH |
2309 RTE_ETH_RX_OFFLOAD_TCP_LRO);
2310 info->tx_offload_capa = (RTE_ETH_TX_OFFLOAD_OUTER_IPV4_CKSUM |
2311 RTE_ETH_TX_OFFLOAD_IPV4_CKSUM |
2312 RTE_ETH_TX_OFFLOAD_TCP_CKSUM |
2313 RTE_ETH_TX_OFFLOAD_UDP_CKSUM |
2314 RTE_ETH_TX_OFFLOAD_SCTP_CKSUM |
2315 RTE_ETH_TX_OFFLOAD_MULTI_SEGS |
2316 RTE_ETH_TX_OFFLOAD_TCP_TSO |
2317 RTE_ETH_TX_OFFLOAD_VXLAN_TNL_TSO |
2318 RTE_ETH_TX_OFFLOAD_GRE_TNL_TSO |
2319 RTE_ETH_TX_OFFLOAD_GENEVE_TNL_TSO |
2320 RTE_ETH_TX_OFFLOAD_MBUF_FAST_FREE |
2321 hns3_txvlan_cap_get(hw));
2323 if (hns3_dev_get_support(hw, OUTER_UDP_CKSUM))
2324 info->tx_offload_capa |= RTE_ETH_TX_OFFLOAD_OUTER_UDP_CKSUM;
2326 if (hns3_dev_get_support(hw, INDEP_TXRX))
2327 info->dev_capa = RTE_ETH_DEV_CAPA_RUNTIME_RX_QUEUE_SETUP |
2328 RTE_ETH_DEV_CAPA_RUNTIME_TX_QUEUE_SETUP;
2329 info->dev_capa &= ~RTE_ETH_DEV_CAPA_FLOW_RULE_KEEP;
2331 if (hns3_dev_get_support(hw, PTP))
2332 info->rx_offload_capa |= RTE_ETH_RX_OFFLOAD_TIMESTAMP;
2334 info->rx_desc_lim = (struct rte_eth_desc_lim) {
2335 .nb_max = HNS3_MAX_RING_DESC,
2336 .nb_min = HNS3_MIN_RING_DESC,
2337 .nb_align = HNS3_ALIGN_RING_DESC,
2340 info->tx_desc_lim = (struct rte_eth_desc_lim) {
2341 .nb_max = HNS3_MAX_RING_DESC,
2342 .nb_min = HNS3_MIN_RING_DESC,
2343 .nb_align = HNS3_ALIGN_RING_DESC,
2344 .nb_seg_max = HNS3_MAX_TSO_BD_PER_PKT,
2345 .nb_mtu_seg_max = hw->max_non_tso_bd_num,
2348 info->speed_capa = hns3_get_speed_capa(hw);
2349 info->default_rxconf = (struct rte_eth_rxconf) {
2350 .rx_free_thresh = HNS3_DEFAULT_RX_FREE_THRESH,
2352 * If there are no available Rx buffer descriptors, incoming
2353 * packets are always dropped by hardware based on hns3 network
2359 info->default_txconf = (struct rte_eth_txconf) {
2360 .tx_rs_thresh = HNS3_DEFAULT_TX_RS_THRESH,
2364 info->reta_size = hw->rss_ind_tbl_size;
2365 info->hash_key_size = HNS3_RSS_KEY_SIZE;
2366 info->flow_type_rss_offloads = HNS3_ETH_RSS_SUPPORT;
2368 info->default_rxportconf.burst_size = HNS3_DEFAULT_PORT_CONF_BURST_SIZE;
2369 info->default_txportconf.burst_size = HNS3_DEFAULT_PORT_CONF_BURST_SIZE;
2370 info->default_rxportconf.nb_queues = HNS3_DEFAULT_PORT_CONF_QUEUES_NUM;
2371 info->default_txportconf.nb_queues = HNS3_DEFAULT_PORT_CONF_QUEUES_NUM;
2372 info->default_rxportconf.ring_size = HNS3_DEFAULT_RING_DESC;
2373 info->default_txportconf.ring_size = HNS3_DEFAULT_RING_DESC;
2379 hns3_fw_version_get(struct rte_eth_dev *eth_dev, char *fw_version,
2382 struct hns3_adapter *hns = eth_dev->data->dev_private;
2383 struct hns3_hw *hw = &hns->hw;
2384 uint32_t version = hw->fw_version;
2387 ret = snprintf(fw_version, fw_size, "%lu.%lu.%lu.%lu",
2388 hns3_get_field(version, HNS3_FW_VERSION_BYTE3_M,
2389 HNS3_FW_VERSION_BYTE3_S),
2390 hns3_get_field(version, HNS3_FW_VERSION_BYTE2_M,
2391 HNS3_FW_VERSION_BYTE2_S),
2392 hns3_get_field(version, HNS3_FW_VERSION_BYTE1_M,
2393 HNS3_FW_VERSION_BYTE1_S),
2394 hns3_get_field(version, HNS3_FW_VERSION_BYTE0_M,
2395 HNS3_FW_VERSION_BYTE0_S));
2399 ret += 1; /* add the size of '\0' */
2400 if (fw_size < (size_t)ret)
2407 hns3_update_port_link_info(struct rte_eth_dev *eth_dev)
2409 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(eth_dev->data->dev_private);
2412 (void)hns3_update_link_status(hw);
2414 ret = hns3_update_link_info(eth_dev);
2416 hw->mac.link_status = RTE_ETH_LINK_DOWN;
2422 hns3_setup_linkstatus(struct rte_eth_dev *eth_dev,
2423 struct rte_eth_link *new_link)
2425 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(eth_dev->data->dev_private);
2426 struct hns3_mac *mac = &hw->mac;
2428 switch (mac->link_speed) {
2429 case RTE_ETH_SPEED_NUM_10M:
2430 case RTE_ETH_SPEED_NUM_100M:
2431 case RTE_ETH_SPEED_NUM_1G:
2432 case RTE_ETH_SPEED_NUM_10G:
2433 case RTE_ETH_SPEED_NUM_25G:
2434 case RTE_ETH_SPEED_NUM_40G:
2435 case RTE_ETH_SPEED_NUM_50G:
2436 case RTE_ETH_SPEED_NUM_100G:
2437 case RTE_ETH_SPEED_NUM_200G:
2438 if (mac->link_status)
2439 new_link->link_speed = mac->link_speed;
2442 if (mac->link_status)
2443 new_link->link_speed = RTE_ETH_SPEED_NUM_UNKNOWN;
2447 if (!mac->link_status)
2448 new_link->link_speed = RTE_ETH_SPEED_NUM_NONE;
2450 new_link->link_duplex = mac->link_duplex;
2451 new_link->link_status = mac->link_status ? RTE_ETH_LINK_UP : RTE_ETH_LINK_DOWN;
2452 new_link->link_autoneg = mac->link_autoneg;
2456 hns3_dev_link_update(struct rte_eth_dev *eth_dev, int wait_to_complete)
2458 #define HNS3_LINK_CHECK_INTERVAL 100 /* 100ms */
2459 #define HNS3_MAX_LINK_CHECK_TIMES 20 /* 2s (100 * 20ms) in total */
2461 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(eth_dev->data->dev_private);
2462 uint32_t retry_cnt = HNS3_MAX_LINK_CHECK_TIMES;
2463 struct hns3_mac *mac = &hw->mac;
2464 struct rte_eth_link new_link;
2467 /* When port is stopped, report link down. */
2468 if (eth_dev->data->dev_started == 0) {
2469 new_link.link_autoneg = mac->link_autoneg;
2470 new_link.link_duplex = mac->link_duplex;
2471 new_link.link_speed = RTE_ETH_SPEED_NUM_NONE;
2472 new_link.link_status = RTE_ETH_LINK_DOWN;
2477 ret = hns3_update_port_link_info(eth_dev);
2479 hns3_err(hw, "failed to get port link info, ret = %d.",
2484 if (!wait_to_complete || mac->link_status == RTE_ETH_LINK_UP)
2487 rte_delay_ms(HNS3_LINK_CHECK_INTERVAL);
2488 } while (retry_cnt--);
2490 memset(&new_link, 0, sizeof(new_link));
2491 hns3_setup_linkstatus(eth_dev, &new_link);
2494 return rte_eth_linkstatus_set(eth_dev, &new_link);
2498 hns3_dev_set_link_up(struct rte_eth_dev *dev)
2500 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2504 * The "tx_pkt_burst" will be restored. But the secondary process does
2505 * not support the mechanism for notifying the primary process.
2507 if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
2508 hns3_err(hw, "secondary process does not support to set link up.");
2513 * If device isn't started Rx/Tx function is still disabled, setting
2514 * link up is not allowed. But it is probably better to return success
2515 * to reduce the impact on the upper layer.
2517 if (hw->adapter_state != HNS3_NIC_STARTED) {
2518 hns3_info(hw, "device isn't started, can't set link up.");
2522 if (!hw->set_link_down)
2525 rte_spinlock_lock(&hw->lock);
2526 ret = hns3_cfg_mac_mode(hw, true);
2528 rte_spinlock_unlock(&hw->lock);
2529 hns3_err(hw, "failed to set link up, ret = %d", ret);
2533 hw->set_link_down = false;
2534 hns3_start_tx_datapath(dev);
2535 rte_spinlock_unlock(&hw->lock);
2541 hns3_dev_set_link_down(struct rte_eth_dev *dev)
2543 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2547 * The "tx_pkt_burst" will be set to dummy function. But the secondary
2548 * process does not support the mechanism for notifying the primary
2551 if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
2552 hns3_err(hw, "secondary process does not support to set link down.");
2557 * If device isn't started or the API has been called, link status is
2558 * down, return success.
2560 if (hw->adapter_state != HNS3_NIC_STARTED || hw->set_link_down)
2563 rte_spinlock_lock(&hw->lock);
2564 hns3_stop_tx_datapath(dev);
2565 ret = hns3_cfg_mac_mode(hw, false);
2567 hns3_start_tx_datapath(dev);
2568 rte_spinlock_unlock(&hw->lock);
2569 hns3_err(hw, "failed to set link down, ret = %d", ret);
2573 hw->set_link_down = true;
2574 rte_spinlock_unlock(&hw->lock);
2580 hns3_parse_func_status(struct hns3_hw *hw, struct hns3_func_status_cmd *status)
2582 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
2583 struct hns3_pf *pf = &hns->pf;
2585 if (!(status->pf_state & HNS3_PF_STATE_DONE))
2588 pf->is_main_pf = (status->pf_state & HNS3_PF_STATE_MAIN) ? true : false;
2594 hns3_query_function_status(struct hns3_hw *hw)
2596 #define HNS3_QUERY_MAX_CNT 10
2597 #define HNS3_QUERY_SLEEP_MSCOEND 1
2598 struct hns3_func_status_cmd *req;
2599 struct hns3_cmd_desc desc;
2603 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_QUERY_FUNC_STATUS, true);
2604 req = (struct hns3_func_status_cmd *)desc.data;
2607 ret = hns3_cmd_send(hw, &desc, 1);
2609 PMD_INIT_LOG(ERR, "query function status failed %d",
2614 /* Check pf reset is done */
2618 rte_delay_ms(HNS3_QUERY_SLEEP_MSCOEND);
2619 } while (timeout++ < HNS3_QUERY_MAX_CNT);
2621 return hns3_parse_func_status(hw, req);
2625 hns3_get_pf_max_tqp_num(struct hns3_hw *hw)
2627 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
2628 struct hns3_pf *pf = &hns->pf;
2630 if (pf->tqp_config_mode == HNS3_FLEX_MAX_TQP_NUM_MODE) {
2632 * The total_tqps_num obtained from firmware is maximum tqp
2633 * numbers of this port, which should be used for PF and VFs.
2634 * There is no need for pf to have so many tqp numbers in
2635 * most cases. RTE_LIBRTE_HNS3_MAX_TQP_NUM_PER_PF,
2636 * coming from config file, is assigned to maximum queue number
2637 * for the PF of this port by user. So users can modify the
2638 * maximum queue number of PF according to their own application
2639 * scenarios, which is more flexible to use. In addition, many
2640 * memories can be saved due to allocating queue statistics
2641 * room according to the actual number of queues required. The
2642 * maximum queue number of PF for network engine with
2643 * revision_id greater than 0x30 is assigned by config file.
2645 if (RTE_LIBRTE_HNS3_MAX_TQP_NUM_PER_PF <= 0) {
2646 hns3_err(hw, "RTE_LIBRTE_HNS3_MAX_TQP_NUM_PER_PF(%d) "
2647 "must be greater than 0.",
2648 RTE_LIBRTE_HNS3_MAX_TQP_NUM_PER_PF);
2652 hw->tqps_num = RTE_MIN(RTE_LIBRTE_HNS3_MAX_TQP_NUM_PER_PF,
2653 hw->total_tqps_num);
2656 * Due to the limitation on the number of PF interrupts
2657 * available, the maximum queue number assigned to PF on
2658 * the network engine with revision_id 0x21 is 64.
2660 hw->tqps_num = RTE_MIN(hw->total_tqps_num,
2661 HNS3_MAX_TQP_NUM_HIP08_PF);
2668 hns3_query_pf_resource(struct hns3_hw *hw)
2670 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
2671 struct hns3_pf *pf = &hns->pf;
2672 struct hns3_pf_res_cmd *req;
2673 struct hns3_cmd_desc desc;
2676 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_QUERY_PF_RSRC, true);
2677 ret = hns3_cmd_send(hw, &desc, 1);
2679 PMD_INIT_LOG(ERR, "query pf resource failed %d", ret);
2683 req = (struct hns3_pf_res_cmd *)desc.data;
2684 hw->total_tqps_num = rte_le_to_cpu_16(req->tqp_num) +
2685 rte_le_to_cpu_16(req->ext_tqp_num);
2686 ret = hns3_get_pf_max_tqp_num(hw);
2690 pf->pkt_buf_size = rte_le_to_cpu_16(req->buf_size) << HNS3_BUF_UNIT_S;
2691 pf->func_num = rte_le_to_cpu_16(req->pf_own_fun_number);
2693 if (req->tx_buf_size)
2695 rte_le_to_cpu_16(req->tx_buf_size) << HNS3_BUF_UNIT_S;
2697 pf->tx_buf_size = HNS3_DEFAULT_TX_BUF;
2699 pf->tx_buf_size = roundup(pf->tx_buf_size, HNS3_BUF_SIZE_UNIT);
2701 if (req->dv_buf_size)
2703 rte_le_to_cpu_16(req->dv_buf_size) << HNS3_BUF_UNIT_S;
2705 pf->dv_buf_size = HNS3_DEFAULT_DV;
2707 pf->dv_buf_size = roundup(pf->dv_buf_size, HNS3_BUF_SIZE_UNIT);
2710 hns3_get_field(rte_le_to_cpu_16(req->nic_pf_intr_vector_number),
2711 HNS3_PF_VEC_NUM_M, HNS3_PF_VEC_NUM_S);
2717 hns3_parse_cfg(struct hns3_cfg *cfg, struct hns3_cmd_desc *desc)
2719 struct hns3_cfg_param_cmd *req;
2720 uint64_t mac_addr_tmp_high;
2721 uint8_t ext_rss_size_max;
2722 uint64_t mac_addr_tmp;
2725 req = (struct hns3_cfg_param_cmd *)desc[0].data;
2727 /* get the configuration */
2728 cfg->tc_num = hns3_get_field(rte_le_to_cpu_32(req->param[0]),
2729 HNS3_CFG_TC_NUM_M, HNS3_CFG_TC_NUM_S);
2730 cfg->tqp_desc_num = hns3_get_field(rte_le_to_cpu_32(req->param[0]),
2731 HNS3_CFG_TQP_DESC_N_M,
2732 HNS3_CFG_TQP_DESC_N_S);
2734 cfg->phy_addr = hns3_get_field(rte_le_to_cpu_32(req->param[1]),
2735 HNS3_CFG_PHY_ADDR_M,
2736 HNS3_CFG_PHY_ADDR_S);
2737 cfg->media_type = hns3_get_field(rte_le_to_cpu_32(req->param[1]),
2738 HNS3_CFG_MEDIA_TP_M,
2739 HNS3_CFG_MEDIA_TP_S);
2740 cfg->rx_buf_len = hns3_get_field(rte_le_to_cpu_32(req->param[1]),
2741 HNS3_CFG_RX_BUF_LEN_M,
2742 HNS3_CFG_RX_BUF_LEN_S);
2743 /* get mac address */
2744 mac_addr_tmp = rte_le_to_cpu_32(req->param[2]);
2745 mac_addr_tmp_high = hns3_get_field(rte_le_to_cpu_32(req->param[3]),
2746 HNS3_CFG_MAC_ADDR_H_M,
2747 HNS3_CFG_MAC_ADDR_H_S);
2749 mac_addr_tmp |= (mac_addr_tmp_high << 31) << 1;
2751 cfg->default_speed = hns3_get_field(rte_le_to_cpu_32(req->param[3]),
2752 HNS3_CFG_DEFAULT_SPEED_M,
2753 HNS3_CFG_DEFAULT_SPEED_S);
2754 cfg->rss_size_max = hns3_get_field(rte_le_to_cpu_32(req->param[3]),
2755 HNS3_CFG_RSS_SIZE_M,
2756 HNS3_CFG_RSS_SIZE_S);
2758 for (i = 0; i < RTE_ETHER_ADDR_LEN; i++)
2759 cfg->mac_addr[i] = (mac_addr_tmp >> (8 * i)) & 0xff;
2761 req = (struct hns3_cfg_param_cmd *)desc[1].data;
2762 cfg->numa_node_map = rte_le_to_cpu_32(req->param[0]);
2764 cfg->speed_ability = hns3_get_field(rte_le_to_cpu_32(req->param[1]),
2765 HNS3_CFG_SPEED_ABILITY_M,
2766 HNS3_CFG_SPEED_ABILITY_S);
2767 cfg->umv_space = hns3_get_field(rte_le_to_cpu_32(req->param[1]),
2768 HNS3_CFG_UMV_TBL_SPACE_M,
2769 HNS3_CFG_UMV_TBL_SPACE_S);
2770 if (!cfg->umv_space)
2771 cfg->umv_space = HNS3_DEFAULT_UMV_SPACE_PER_PF;
2773 ext_rss_size_max = hns3_get_field(rte_le_to_cpu_32(req->param[2]),
2774 HNS3_CFG_EXT_RSS_SIZE_M,
2775 HNS3_CFG_EXT_RSS_SIZE_S);
2777 * Field ext_rss_size_max obtained from firmware will be more flexible
2778 * for future changes and expansions, which is an exponent of 2, instead
2779 * of reading out directly. If this field is not zero, hns3 PF PMD
2780 * driver uses it as rss_size_max under one TC. Device, whose revision
2781 * id is greater than or equal to PCI_REVISION_ID_HIP09_A, obtains the
2782 * maximum number of queues supported under a TC through this field.
2784 if (ext_rss_size_max)
2785 cfg->rss_size_max = 1U << ext_rss_size_max;
2788 /* hns3_get_board_cfg: query the static parameter from NCL_config file in flash
2789 * @hw: pointer to struct hns3_hw
2790 * @hcfg: the config structure to be getted
2793 hns3_get_board_cfg(struct hns3_hw *hw, struct hns3_cfg *hcfg)
2795 struct hns3_cmd_desc desc[HNS3_PF_CFG_DESC_NUM];
2796 struct hns3_cfg_param_cmd *req;
2801 for (i = 0; i < HNS3_PF_CFG_DESC_NUM; i++) {
2803 req = (struct hns3_cfg_param_cmd *)desc[i].data;
2804 hns3_cmd_setup_basic_desc(&desc[i], HNS3_OPC_GET_CFG_PARAM,
2806 hns3_set_field(offset, HNS3_CFG_OFFSET_M, HNS3_CFG_OFFSET_S,
2807 i * HNS3_CFG_RD_LEN_BYTES);
2808 /* Len should be divided by 4 when send to hardware */
2809 hns3_set_field(offset, HNS3_CFG_RD_LEN_M, HNS3_CFG_RD_LEN_S,
2810 HNS3_CFG_RD_LEN_BYTES / HNS3_CFG_RD_LEN_UNIT);
2811 req->offset = rte_cpu_to_le_32(offset);
2814 ret = hns3_cmd_send(hw, desc, HNS3_PF_CFG_DESC_NUM);
2816 PMD_INIT_LOG(ERR, "get config failed %d.", ret);
2820 hns3_parse_cfg(hcfg, desc);
2826 hns3_parse_speed(int speed_cmd, uint32_t *speed)
2828 switch (speed_cmd) {
2829 case HNS3_CFG_SPEED_10M:
2830 *speed = RTE_ETH_SPEED_NUM_10M;
2832 case HNS3_CFG_SPEED_100M:
2833 *speed = RTE_ETH_SPEED_NUM_100M;
2835 case HNS3_CFG_SPEED_1G:
2836 *speed = RTE_ETH_SPEED_NUM_1G;
2838 case HNS3_CFG_SPEED_10G:
2839 *speed = RTE_ETH_SPEED_NUM_10G;
2841 case HNS3_CFG_SPEED_25G:
2842 *speed = RTE_ETH_SPEED_NUM_25G;
2844 case HNS3_CFG_SPEED_40G:
2845 *speed = RTE_ETH_SPEED_NUM_40G;
2847 case HNS3_CFG_SPEED_50G:
2848 *speed = RTE_ETH_SPEED_NUM_50G;
2850 case HNS3_CFG_SPEED_100G:
2851 *speed = RTE_ETH_SPEED_NUM_100G;
2853 case HNS3_CFG_SPEED_200G:
2854 *speed = RTE_ETH_SPEED_NUM_200G;
2864 hns3_set_default_dev_specifications(struct hns3_hw *hw)
2866 hw->max_non_tso_bd_num = HNS3_MAX_NON_TSO_BD_PER_PKT;
2867 hw->rss_ind_tbl_size = HNS3_RSS_IND_TBL_SIZE;
2868 hw->rss_key_size = HNS3_RSS_KEY_SIZE;
2869 hw->max_tm_rate = HNS3_ETHER_MAX_RATE;
2870 hw->intr.int_ql_max = HNS3_INTR_QL_NONE;
2874 hns3_parse_dev_specifications(struct hns3_hw *hw, struct hns3_cmd_desc *desc)
2876 struct hns3_dev_specs_0_cmd *req0;
2878 req0 = (struct hns3_dev_specs_0_cmd *)desc[0].data;
2880 hw->max_non_tso_bd_num = req0->max_non_tso_bd_num;
2881 hw->rss_ind_tbl_size = rte_le_to_cpu_16(req0->rss_ind_tbl_size);
2882 hw->rss_key_size = rte_le_to_cpu_16(req0->rss_key_size);
2883 hw->max_tm_rate = rte_le_to_cpu_32(req0->max_tm_rate);
2884 hw->intr.int_ql_max = rte_le_to_cpu_16(req0->intr_ql_max);
2888 hns3_check_dev_specifications(struct hns3_hw *hw)
2890 if (hw->rss_ind_tbl_size == 0 ||
2891 hw->rss_ind_tbl_size > HNS3_RSS_IND_TBL_SIZE_MAX) {
2892 hns3_err(hw, "the size of hash lookup table configured (%u)"
2893 " exceeds the maximum(%u)", hw->rss_ind_tbl_size,
2894 HNS3_RSS_IND_TBL_SIZE_MAX);
2902 hns3_query_dev_specifications(struct hns3_hw *hw)
2904 struct hns3_cmd_desc desc[HNS3_QUERY_DEV_SPECS_BD_NUM];
2908 for (i = 0; i < HNS3_QUERY_DEV_SPECS_BD_NUM - 1; i++) {
2909 hns3_cmd_setup_basic_desc(&desc[i], HNS3_OPC_QUERY_DEV_SPECS,
2911 desc[i].flag |= rte_cpu_to_le_16(HNS3_CMD_FLAG_NEXT);
2913 hns3_cmd_setup_basic_desc(&desc[i], HNS3_OPC_QUERY_DEV_SPECS, true);
2915 ret = hns3_cmd_send(hw, desc, HNS3_QUERY_DEV_SPECS_BD_NUM);
2919 hns3_parse_dev_specifications(hw, desc);
2921 return hns3_check_dev_specifications(hw);
2925 hns3_get_capability(struct hns3_hw *hw)
2927 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
2928 struct rte_pci_device *pci_dev;
2929 struct hns3_pf *pf = &hns->pf;
2930 struct rte_eth_dev *eth_dev;
2935 eth_dev = &rte_eth_devices[hw->data->port_id];
2936 pci_dev = RTE_ETH_DEV_TO_PCI(eth_dev);
2937 device_id = pci_dev->id.device_id;
2939 if (device_id == HNS3_DEV_ID_25GE_RDMA ||
2940 device_id == HNS3_DEV_ID_50GE_RDMA ||
2941 device_id == HNS3_DEV_ID_100G_RDMA_MACSEC ||
2942 device_id == HNS3_DEV_ID_200G_RDMA)
2943 hns3_set_bit(hw->capability, HNS3_DEV_SUPPORT_DCB_B, 1);
2945 /* Get PCI revision id */
2946 ret = rte_pci_read_config(pci_dev, &revision, HNS3_PCI_REVISION_ID_LEN,
2947 HNS3_PCI_REVISION_ID);
2948 if (ret != HNS3_PCI_REVISION_ID_LEN) {
2949 PMD_INIT_LOG(ERR, "failed to read pci revision id, ret = %d",
2953 hw->revision = revision;
2955 if (revision < PCI_REVISION_ID_HIP09_A) {
2956 hns3_set_default_dev_specifications(hw);
2957 hw->intr.mapping_mode = HNS3_INTR_MAPPING_VEC_RSV_ONE;
2958 hw->intr.gl_unit = HNS3_INTR_COALESCE_GL_UINT_2US;
2959 hw->tso_mode = HNS3_TSO_SW_CAL_PSEUDO_H_CSUM;
2960 hw->vlan_mode = HNS3_SW_SHIFT_AND_DISCARD_MODE;
2961 hw->drop_stats_mode = HNS3_PKTS_DROP_STATS_MODE1;
2962 hw->min_tx_pkt_len = HNS3_HIP08_MIN_TX_PKT_LEN;
2963 pf->tqp_config_mode = HNS3_FIXED_MAX_TQP_NUM_MODE;
2964 hw->rss_info.ipv6_sctp_offload_supported = false;
2965 hw->udp_cksum_mode = HNS3_SPECIAL_PORT_SW_CKSUM_MODE;
2966 pf->support_multi_tc_pause = false;
2970 ret = hns3_query_dev_specifications(hw);
2973 "failed to query dev specifications, ret = %d",
2978 hw->intr.mapping_mode = HNS3_INTR_MAPPING_VEC_ALL;
2979 hw->intr.gl_unit = HNS3_INTR_COALESCE_GL_UINT_1US;
2980 hw->tso_mode = HNS3_TSO_HW_CAL_PSEUDO_H_CSUM;
2981 hw->vlan_mode = HNS3_HW_SHIFT_AND_DISCARD_MODE;
2982 hw->drop_stats_mode = HNS3_PKTS_DROP_STATS_MODE2;
2983 hw->min_tx_pkt_len = HNS3_HIP09_MIN_TX_PKT_LEN;
2984 pf->tqp_config_mode = HNS3_FLEX_MAX_TQP_NUM_MODE;
2985 hw->rss_info.ipv6_sctp_offload_supported = true;
2986 hw->udp_cksum_mode = HNS3_SPECIAL_PORT_HW_CKSUM_MODE;
2987 pf->support_multi_tc_pause = true;
2993 hns3_check_media_type(struct hns3_hw *hw, uint8_t media_type)
2997 switch (media_type) {
2998 case HNS3_MEDIA_TYPE_COPPER:
2999 if (!hns3_dev_get_support(hw, COPPER)) {
3001 "Media type is copper, not supported.");
3007 case HNS3_MEDIA_TYPE_FIBER:
3010 case HNS3_MEDIA_TYPE_BACKPLANE:
3011 PMD_INIT_LOG(ERR, "Media type is Backplane, not supported.");
3015 PMD_INIT_LOG(ERR, "Unknown media type = %u!", media_type);
3024 hns3_get_board_configuration(struct hns3_hw *hw)
3026 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
3027 struct hns3_pf *pf = &hns->pf;
3028 struct hns3_cfg cfg;
3031 ret = hns3_get_board_cfg(hw, &cfg);
3033 PMD_INIT_LOG(ERR, "get board config failed %d", ret);
3037 ret = hns3_check_media_type(hw, cfg.media_type);
3041 hw->mac.media_type = cfg.media_type;
3042 hw->rss_size_max = cfg.rss_size_max;
3043 hw->rss_dis_flag = false;
3044 memcpy(hw->mac.mac_addr, cfg.mac_addr, RTE_ETHER_ADDR_LEN);
3045 hw->mac.phy_addr = cfg.phy_addr;
3046 hw->num_tx_desc = cfg.tqp_desc_num;
3047 hw->num_rx_desc = cfg.tqp_desc_num;
3048 hw->dcb_info.num_pg = 1;
3049 hw->dcb_info.hw_pfc_map = 0;
3051 ret = hns3_parse_speed(cfg.default_speed, &hw->mac.link_speed);
3053 PMD_INIT_LOG(ERR, "Get wrong speed %u, ret = %d",
3054 cfg.default_speed, ret);
3058 pf->tc_max = cfg.tc_num;
3059 if (pf->tc_max > HNS3_MAX_TC_NUM || pf->tc_max < 1) {
3060 PMD_INIT_LOG(WARNING,
3061 "Get TC num(%u) from flash, set TC num to 1",
3066 /* Dev does not support DCB */
3067 if (!hns3_dev_get_support(hw, DCB)) {
3071 pf->pfc_max = pf->tc_max;
3073 hw->dcb_info.num_tc = 1;
3074 hw->alloc_rss_size = RTE_MIN(hw->rss_size_max,
3075 hw->tqps_num / hw->dcb_info.num_tc);
3076 hns3_set_bit(hw->hw_tc_map, 0, 1);
3077 pf->tx_sch_mode = HNS3_FLAG_TC_BASE_SCH_MODE;
3079 pf->wanted_umv_size = cfg.umv_space;
3085 hns3_get_configuration(struct hns3_hw *hw)
3089 ret = hns3_query_function_status(hw);
3091 PMD_INIT_LOG(ERR, "Failed to query function status: %d.", ret);
3095 /* Get device capability */
3096 ret = hns3_get_capability(hw);
3098 PMD_INIT_LOG(ERR, "failed to get device capability: %d.", ret);
3102 /* Get pf resource */
3103 ret = hns3_query_pf_resource(hw);
3105 PMD_INIT_LOG(ERR, "Failed to query pf resource: %d", ret);
3109 ret = hns3_get_board_configuration(hw);
3111 PMD_INIT_LOG(ERR, "failed to get board configuration: %d", ret);
3115 ret = hns3_query_dev_fec_info(hw);
3118 "failed to query FEC information, ret = %d", ret);
3124 hns3_map_tqps_to_func(struct hns3_hw *hw, uint16_t func_id, uint16_t tqp_pid,
3125 uint16_t tqp_vid, bool is_pf)
3127 struct hns3_tqp_map_cmd *req;
3128 struct hns3_cmd_desc desc;
3131 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_SET_TQP_MAP, false);
3133 req = (struct hns3_tqp_map_cmd *)desc.data;
3134 req->tqp_id = rte_cpu_to_le_16(tqp_pid);
3135 req->tqp_vf = func_id;
3136 req->tqp_flag = 1 << HNS3_TQP_MAP_EN_B;
3138 req->tqp_flag |= (1 << HNS3_TQP_MAP_TYPE_B);
3139 req->tqp_vid = rte_cpu_to_le_16(tqp_vid);
3141 ret = hns3_cmd_send(hw, &desc, 1);
3143 PMD_INIT_LOG(ERR, "TQP map failed %d", ret);
3149 hns3_map_tqp(struct hns3_hw *hw)
3155 * In current version, VF is not supported when PF is driven by DPDK
3156 * driver, so we assign total tqps_num tqps allocated to this port
3159 for (i = 0; i < hw->total_tqps_num; i++) {
3160 ret = hns3_map_tqps_to_func(hw, HNS3_PF_FUNC_ID, i, i, true);
3169 hns3_cfg_mac_speed_dup_hw(struct hns3_hw *hw, uint32_t speed, uint8_t duplex)
3171 struct hns3_config_mac_speed_dup_cmd *req;
3172 struct hns3_cmd_desc desc;
3175 req = (struct hns3_config_mac_speed_dup_cmd *)desc.data;
3177 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_CONFIG_SPEED_DUP, false);
3179 hns3_set_bit(req->speed_dup, HNS3_CFG_DUPLEX_B, !!duplex ? 1 : 0);
3182 case RTE_ETH_SPEED_NUM_10M:
3183 hns3_set_field(req->speed_dup, HNS3_CFG_SPEED_M,
3184 HNS3_CFG_SPEED_S, HNS3_CFG_SPEED_10M);
3186 case RTE_ETH_SPEED_NUM_100M:
3187 hns3_set_field(req->speed_dup, HNS3_CFG_SPEED_M,
3188 HNS3_CFG_SPEED_S, HNS3_CFG_SPEED_100M);
3190 case RTE_ETH_SPEED_NUM_1G:
3191 hns3_set_field(req->speed_dup, HNS3_CFG_SPEED_M,
3192 HNS3_CFG_SPEED_S, HNS3_CFG_SPEED_1G);
3194 case RTE_ETH_SPEED_NUM_10G:
3195 hns3_set_field(req->speed_dup, HNS3_CFG_SPEED_M,
3196 HNS3_CFG_SPEED_S, HNS3_CFG_SPEED_10G);
3198 case RTE_ETH_SPEED_NUM_25G:
3199 hns3_set_field(req->speed_dup, HNS3_CFG_SPEED_M,
3200 HNS3_CFG_SPEED_S, HNS3_CFG_SPEED_25G);
3202 case RTE_ETH_SPEED_NUM_40G:
3203 hns3_set_field(req->speed_dup, HNS3_CFG_SPEED_M,
3204 HNS3_CFG_SPEED_S, HNS3_CFG_SPEED_40G);
3206 case RTE_ETH_SPEED_NUM_50G:
3207 hns3_set_field(req->speed_dup, HNS3_CFG_SPEED_M,
3208 HNS3_CFG_SPEED_S, HNS3_CFG_SPEED_50G);
3210 case RTE_ETH_SPEED_NUM_100G:
3211 hns3_set_field(req->speed_dup, HNS3_CFG_SPEED_M,
3212 HNS3_CFG_SPEED_S, HNS3_CFG_SPEED_100G);
3214 case RTE_ETH_SPEED_NUM_200G:
3215 hns3_set_field(req->speed_dup, HNS3_CFG_SPEED_M,
3216 HNS3_CFG_SPEED_S, HNS3_CFG_SPEED_200G);
3219 PMD_INIT_LOG(ERR, "invalid speed (%u)", speed);
3223 hns3_set_bit(req->mac_change_fec_en, HNS3_CFG_MAC_SPEED_CHANGE_EN_B, 1);
3225 ret = hns3_cmd_send(hw, &desc, 1);
3227 PMD_INIT_LOG(ERR, "mac speed/duplex config cmd failed %d", ret);
3233 hns3_tx_buffer_calc(struct hns3_hw *hw, struct hns3_pkt_buf_alloc *buf_alloc)
3235 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
3236 struct hns3_pf *pf = &hns->pf;
3237 struct hns3_priv_buf *priv;
3238 uint32_t i, total_size;
3240 total_size = pf->pkt_buf_size;
3242 /* alloc tx buffer for all enabled tc */
3243 for (i = 0; i < HNS3_MAX_TC_NUM; i++) {
3244 priv = &buf_alloc->priv_buf[i];
3246 if (hw->hw_tc_map & BIT(i)) {
3247 if (total_size < pf->tx_buf_size)
3250 priv->tx_buf_size = pf->tx_buf_size;
3252 priv->tx_buf_size = 0;
3254 total_size -= priv->tx_buf_size;
3261 hns3_tx_buffer_alloc(struct hns3_hw *hw, struct hns3_pkt_buf_alloc *buf_alloc)
3263 /* TX buffer size is unit by 128 byte */
3264 #define HNS3_BUF_SIZE_UNIT_SHIFT 7
3265 #define HNS3_BUF_SIZE_UPDATE_EN_MSK BIT(15)
3266 struct hns3_tx_buff_alloc_cmd *req;
3267 struct hns3_cmd_desc desc;
3272 req = (struct hns3_tx_buff_alloc_cmd *)desc.data;
3274 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_TX_BUFF_ALLOC, 0);
3275 for (i = 0; i < HNS3_MAX_TC_NUM; i++) {
3276 buf_size = buf_alloc->priv_buf[i].tx_buf_size;
3278 buf_size = buf_size >> HNS3_BUF_SIZE_UNIT_SHIFT;
3279 req->tx_pkt_buff[i] = rte_cpu_to_le_16(buf_size |
3280 HNS3_BUF_SIZE_UPDATE_EN_MSK);
3283 ret = hns3_cmd_send(hw, &desc, 1);
3285 PMD_INIT_LOG(ERR, "tx buffer alloc cmd failed %d", ret);
3291 hns3_get_tc_num(struct hns3_hw *hw)
3296 for (i = 0; i < HNS3_MAX_TC_NUM; i++)
3297 if (hw->hw_tc_map & BIT(i))
3303 hns3_get_rx_priv_buff_alloced(struct hns3_pkt_buf_alloc *buf_alloc)
3305 struct hns3_priv_buf *priv;
3306 uint32_t rx_priv = 0;
3309 for (i = 0; i < HNS3_MAX_TC_NUM; i++) {
3310 priv = &buf_alloc->priv_buf[i];
3312 rx_priv += priv->buf_size;
3318 hns3_get_tx_buff_alloced(struct hns3_pkt_buf_alloc *buf_alloc)
3320 uint32_t total_tx_size = 0;
3323 for (i = 0; i < HNS3_MAX_TC_NUM; i++)
3324 total_tx_size += buf_alloc->priv_buf[i].tx_buf_size;
3326 return total_tx_size;
3329 /* Get the number of pfc enabled TCs, which have private buffer */
3331 hns3_get_pfc_priv_num(struct hns3_hw *hw, struct hns3_pkt_buf_alloc *buf_alloc)
3333 struct hns3_priv_buf *priv;
3337 for (i = 0; i < HNS3_MAX_TC_NUM; i++) {
3338 priv = &buf_alloc->priv_buf[i];
3339 if ((hw->dcb_info.hw_pfc_map & BIT(i)) && priv->enable)
3346 /* Get the number of pfc disabled TCs, which have private buffer */
3348 hns3_get_no_pfc_priv_num(struct hns3_hw *hw,
3349 struct hns3_pkt_buf_alloc *buf_alloc)
3351 struct hns3_priv_buf *priv;
3355 for (i = 0; i < HNS3_MAX_TC_NUM; i++) {
3356 priv = &buf_alloc->priv_buf[i];
3357 if (hw->hw_tc_map & BIT(i) &&
3358 !(hw->dcb_info.hw_pfc_map & BIT(i)) && priv->enable)
3366 hns3_is_rx_buf_ok(struct hns3_hw *hw, struct hns3_pkt_buf_alloc *buf_alloc,
3369 uint32_t shared_buf_min, shared_buf_tc, shared_std, hi_thrd, lo_thrd;
3370 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
3371 struct hns3_pf *pf = &hns->pf;
3372 uint32_t shared_buf, aligned_mps;
3377 tc_num = hns3_get_tc_num(hw);
3378 aligned_mps = roundup(pf->mps, HNS3_BUF_SIZE_UNIT);
3380 if (hns3_dev_get_support(hw, DCB))
3381 shared_buf_min = HNS3_BUF_MUL_BY * aligned_mps +
3384 shared_buf_min = aligned_mps + HNS3_NON_DCB_ADDITIONAL_BUF
3387 shared_buf_tc = tc_num * aligned_mps + aligned_mps;
3388 shared_std = roundup(RTE_MAX(shared_buf_min, shared_buf_tc),
3389 HNS3_BUF_SIZE_UNIT);
3391 rx_priv = hns3_get_rx_priv_buff_alloced(buf_alloc);
3392 if (rx_all < rx_priv + shared_std)
3395 shared_buf = rounddown(rx_all - rx_priv, HNS3_BUF_SIZE_UNIT);
3396 buf_alloc->s_buf.buf_size = shared_buf;
3397 if (hns3_dev_get_support(hw, DCB)) {
3398 buf_alloc->s_buf.self.high = shared_buf - pf->dv_buf_size;
3399 buf_alloc->s_buf.self.low = buf_alloc->s_buf.self.high
3400 - roundup(aligned_mps / HNS3_BUF_DIV_BY,
3401 HNS3_BUF_SIZE_UNIT);
3403 buf_alloc->s_buf.self.high =
3404 aligned_mps + HNS3_NON_DCB_ADDITIONAL_BUF;
3405 buf_alloc->s_buf.self.low = aligned_mps;
3408 if (hns3_dev_get_support(hw, DCB)) {
3409 hi_thrd = shared_buf - pf->dv_buf_size;
3411 if (tc_num <= NEED_RESERVE_TC_NUM)
3412 hi_thrd = hi_thrd * BUF_RESERVE_PERCENT /
3416 hi_thrd = hi_thrd / tc_num;
3418 hi_thrd = RTE_MAX(hi_thrd, HNS3_BUF_MUL_BY * aligned_mps);
3419 hi_thrd = rounddown(hi_thrd, HNS3_BUF_SIZE_UNIT);
3420 lo_thrd = hi_thrd - aligned_mps / HNS3_BUF_DIV_BY;
3422 hi_thrd = aligned_mps + HNS3_NON_DCB_ADDITIONAL_BUF;
3423 lo_thrd = aligned_mps;
3426 for (i = 0; i < HNS3_MAX_TC_NUM; i++) {
3427 buf_alloc->s_buf.tc_thrd[i].low = lo_thrd;
3428 buf_alloc->s_buf.tc_thrd[i].high = hi_thrd;
3435 hns3_rx_buf_calc_all(struct hns3_hw *hw, bool max,
3436 struct hns3_pkt_buf_alloc *buf_alloc)
3438 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
3439 struct hns3_pf *pf = &hns->pf;
3440 struct hns3_priv_buf *priv;
3441 uint32_t aligned_mps;
3445 rx_all = pf->pkt_buf_size - hns3_get_tx_buff_alloced(buf_alloc);
3446 aligned_mps = roundup(pf->mps, HNS3_BUF_SIZE_UNIT);
3448 for (i = 0; i < HNS3_MAX_TC_NUM; i++) {
3449 priv = &buf_alloc->priv_buf[i];
3456 if (!(hw->hw_tc_map & BIT(i)))
3460 if (hw->dcb_info.hw_pfc_map & BIT(i)) {
3461 priv->wl.low = max ? aligned_mps : HNS3_BUF_SIZE_UNIT;
3462 priv->wl.high = roundup(priv->wl.low + aligned_mps,
3463 HNS3_BUF_SIZE_UNIT);
3466 priv->wl.high = max ? (aligned_mps * HNS3_BUF_MUL_BY) :
3470 priv->buf_size = priv->wl.high + pf->dv_buf_size;
3473 return hns3_is_rx_buf_ok(hw, buf_alloc, rx_all);
3477 hns3_drop_nopfc_buf_till_fit(struct hns3_hw *hw,
3478 struct hns3_pkt_buf_alloc *buf_alloc)
3480 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
3481 struct hns3_pf *pf = &hns->pf;
3482 struct hns3_priv_buf *priv;
3483 int no_pfc_priv_num;
3488 rx_all = pf->pkt_buf_size - hns3_get_tx_buff_alloced(buf_alloc);
3489 no_pfc_priv_num = hns3_get_no_pfc_priv_num(hw, buf_alloc);
3491 /* let the last to be cleared first */
3492 for (i = HNS3_MAX_TC_NUM - 1; i >= 0; i--) {
3493 priv = &buf_alloc->priv_buf[i];
3494 mask = BIT((uint8_t)i);
3495 if (hw->hw_tc_map & mask &&
3496 !(hw->dcb_info.hw_pfc_map & mask)) {
3497 /* Clear the no pfc TC private buffer */
3505 if (hns3_is_rx_buf_ok(hw, buf_alloc, rx_all) ||
3506 no_pfc_priv_num == 0)
3510 return hns3_is_rx_buf_ok(hw, buf_alloc, rx_all);
3514 hns3_drop_pfc_buf_till_fit(struct hns3_hw *hw,
3515 struct hns3_pkt_buf_alloc *buf_alloc)
3517 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
3518 struct hns3_pf *pf = &hns->pf;
3519 struct hns3_priv_buf *priv;
3525 rx_all = pf->pkt_buf_size - hns3_get_tx_buff_alloced(buf_alloc);
3526 pfc_priv_num = hns3_get_pfc_priv_num(hw, buf_alloc);
3528 /* let the last to be cleared first */
3529 for (i = HNS3_MAX_TC_NUM - 1; i >= 0; i--) {
3530 priv = &buf_alloc->priv_buf[i];
3531 mask = BIT((uint8_t)i);
3532 if (hw->hw_tc_map & mask && hw->dcb_info.hw_pfc_map & mask) {
3533 /* Reduce the number of pfc TC with private buffer */
3540 if (hns3_is_rx_buf_ok(hw, buf_alloc, rx_all) ||
3545 return hns3_is_rx_buf_ok(hw, buf_alloc, rx_all);
3549 hns3_only_alloc_priv_buff(struct hns3_hw *hw,
3550 struct hns3_pkt_buf_alloc *buf_alloc)
3552 #define COMPENSATE_BUFFER 0x3C00
3553 #define COMPENSATE_HALF_MPS_NUM 5
3554 #define PRIV_WL_GAP 0x1800
3555 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
3556 struct hns3_pf *pf = &hns->pf;
3557 uint32_t tc_num = hns3_get_tc_num(hw);
3558 uint32_t half_mps = pf->mps >> 1;
3559 struct hns3_priv_buf *priv;
3560 uint32_t min_rx_priv;
3564 rx_priv = pf->pkt_buf_size - hns3_get_tx_buff_alloced(buf_alloc);
3566 rx_priv = rx_priv / tc_num;
3568 if (tc_num <= NEED_RESERVE_TC_NUM)
3569 rx_priv = rx_priv * BUF_RESERVE_PERCENT / BUF_MAX_PERCENT;
3572 * Minimum value of private buffer in rx direction (min_rx_priv) is
3573 * equal to "DV + 2.5 * MPS + 15KB". Driver only allocates rx private
3574 * buffer if rx_priv is greater than min_rx_priv.
3576 min_rx_priv = pf->dv_buf_size + COMPENSATE_BUFFER +
3577 COMPENSATE_HALF_MPS_NUM * half_mps;
3578 min_rx_priv = roundup(min_rx_priv, HNS3_BUF_SIZE_UNIT);
3579 rx_priv = rounddown(rx_priv, HNS3_BUF_SIZE_UNIT);
3580 if (rx_priv < min_rx_priv)
3583 for (i = 0; i < HNS3_MAX_TC_NUM; i++) {
3584 priv = &buf_alloc->priv_buf[i];
3590 if (!(hw->hw_tc_map & BIT(i)))
3594 priv->buf_size = rx_priv;
3595 priv->wl.high = rx_priv - pf->dv_buf_size;
3596 priv->wl.low = priv->wl.high - PRIV_WL_GAP;
3599 buf_alloc->s_buf.buf_size = 0;
3605 * hns3_rx_buffer_calc: calculate the rx private buffer size for all TCs
3606 * @hw: pointer to struct hns3_hw
3607 * @buf_alloc: pointer to buffer calculation data
3608 * @return: 0: calculate sucessful, negative: fail
3611 hns3_rx_buffer_calc(struct hns3_hw *hw, struct hns3_pkt_buf_alloc *buf_alloc)
3613 /* When DCB is not supported, rx private buffer is not allocated. */
3614 if (!hns3_dev_get_support(hw, DCB)) {
3615 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
3616 struct hns3_pf *pf = &hns->pf;
3617 uint32_t rx_all = pf->pkt_buf_size;
3619 rx_all -= hns3_get_tx_buff_alloced(buf_alloc);
3620 if (!hns3_is_rx_buf_ok(hw, buf_alloc, rx_all))
3627 * Try to allocate privated packet buffer for all TCs without share
3630 if (hns3_only_alloc_priv_buff(hw, buf_alloc))
3634 * Try to allocate privated packet buffer for all TCs with share
3637 if (hns3_rx_buf_calc_all(hw, true, buf_alloc))
3641 * For different application scenes, the enabled port number, TC number
3642 * and no_drop TC number are different. In order to obtain the better
3643 * performance, software could allocate the buffer size and configure
3644 * the waterline by trying to decrease the private buffer size according
3645 * to the order, namely, waterline of valid tc, pfc disabled tc, pfc
3648 if (hns3_rx_buf_calc_all(hw, false, buf_alloc))
3651 if (hns3_drop_nopfc_buf_till_fit(hw, buf_alloc))
3654 if (hns3_drop_pfc_buf_till_fit(hw, buf_alloc))
3661 hns3_rx_priv_buf_alloc(struct hns3_hw *hw, struct hns3_pkt_buf_alloc *buf_alloc)
3663 struct hns3_rx_priv_buff_cmd *req;
3664 struct hns3_cmd_desc desc;
3669 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_RX_PRIV_BUFF_ALLOC, false);
3670 req = (struct hns3_rx_priv_buff_cmd *)desc.data;
3672 /* Alloc private buffer TCs */
3673 for (i = 0; i < HNS3_MAX_TC_NUM; i++) {
3674 struct hns3_priv_buf *priv = &buf_alloc->priv_buf[i];
3677 rte_cpu_to_le_16(priv->buf_size >> HNS3_BUF_UNIT_S);
3678 req->buf_num[i] |= rte_cpu_to_le_16(1 << HNS3_TC0_PRI_BUF_EN_B);
3681 buf_size = buf_alloc->s_buf.buf_size;
3682 req->shared_buf = rte_cpu_to_le_16((buf_size >> HNS3_BUF_UNIT_S) |
3683 (1 << HNS3_TC0_PRI_BUF_EN_B));
3685 ret = hns3_cmd_send(hw, &desc, 1);
3687 PMD_INIT_LOG(ERR, "rx private buffer alloc cmd failed %d", ret);
3693 hns3_rx_priv_wl_config(struct hns3_hw *hw, struct hns3_pkt_buf_alloc *buf_alloc)
3695 #define HNS3_RX_PRIV_WL_ALLOC_DESC_NUM 2
3696 struct hns3_rx_priv_wl_buf *req;
3697 struct hns3_priv_buf *priv;
3698 struct hns3_cmd_desc desc[HNS3_RX_PRIV_WL_ALLOC_DESC_NUM];
3702 for (i = 0; i < HNS3_RX_PRIV_WL_ALLOC_DESC_NUM; i++) {
3703 hns3_cmd_setup_basic_desc(&desc[i], HNS3_OPC_RX_PRIV_WL_ALLOC,
3705 req = (struct hns3_rx_priv_wl_buf *)desc[i].data;
3707 /* The first descriptor set the NEXT bit to 1 */
3709 desc[i].flag |= rte_cpu_to_le_16(HNS3_CMD_FLAG_NEXT);
3711 desc[i].flag &= ~rte_cpu_to_le_16(HNS3_CMD_FLAG_NEXT);
3713 for (j = 0; j < HNS3_TC_NUM_ONE_DESC; j++) {
3714 uint32_t idx = i * HNS3_TC_NUM_ONE_DESC + j;
3716 priv = &buf_alloc->priv_buf[idx];
3717 req->tc_wl[j].high = rte_cpu_to_le_16(priv->wl.high >>
3719 req->tc_wl[j].high |=
3720 rte_cpu_to_le_16(BIT(HNS3_RX_PRIV_EN_B));
3721 req->tc_wl[j].low = rte_cpu_to_le_16(priv->wl.low >>
3723 req->tc_wl[j].low |=
3724 rte_cpu_to_le_16(BIT(HNS3_RX_PRIV_EN_B));
3728 /* Send 2 descriptor at one time */
3729 ret = hns3_cmd_send(hw, desc, HNS3_RX_PRIV_WL_ALLOC_DESC_NUM);
3731 PMD_INIT_LOG(ERR, "rx private waterline config cmd failed %d",
3737 hns3_common_thrd_config(struct hns3_hw *hw,
3738 struct hns3_pkt_buf_alloc *buf_alloc)
3740 #define HNS3_RX_COM_THRD_ALLOC_DESC_NUM 2
3741 struct hns3_shared_buf *s_buf = &buf_alloc->s_buf;
3742 struct hns3_rx_com_thrd *req;
3743 struct hns3_cmd_desc desc[HNS3_RX_COM_THRD_ALLOC_DESC_NUM];
3744 struct hns3_tc_thrd *tc;
3749 for (i = 0; i < HNS3_RX_COM_THRD_ALLOC_DESC_NUM; i++) {
3750 hns3_cmd_setup_basic_desc(&desc[i], HNS3_OPC_RX_COM_THRD_ALLOC,
3752 req = (struct hns3_rx_com_thrd *)&desc[i].data;
3754 /* The first descriptor set the NEXT bit to 1 */
3756 desc[i].flag |= rte_cpu_to_le_16(HNS3_CMD_FLAG_NEXT);
3758 desc[i].flag &= ~rte_cpu_to_le_16(HNS3_CMD_FLAG_NEXT);
3760 for (j = 0; j < HNS3_TC_NUM_ONE_DESC; j++) {
3761 tc_idx = i * HNS3_TC_NUM_ONE_DESC + j;
3762 tc = &s_buf->tc_thrd[tc_idx];
3764 req->com_thrd[j].high =
3765 rte_cpu_to_le_16(tc->high >> HNS3_BUF_UNIT_S);
3766 req->com_thrd[j].high |=
3767 rte_cpu_to_le_16(BIT(HNS3_RX_PRIV_EN_B));
3768 req->com_thrd[j].low =
3769 rte_cpu_to_le_16(tc->low >> HNS3_BUF_UNIT_S);
3770 req->com_thrd[j].low |=
3771 rte_cpu_to_le_16(BIT(HNS3_RX_PRIV_EN_B));
3775 /* Send 2 descriptors at one time */
3776 ret = hns3_cmd_send(hw, desc, HNS3_RX_COM_THRD_ALLOC_DESC_NUM);
3778 PMD_INIT_LOG(ERR, "common threshold config cmd failed %d", ret);
3784 hns3_common_wl_config(struct hns3_hw *hw, struct hns3_pkt_buf_alloc *buf_alloc)
3786 struct hns3_shared_buf *buf = &buf_alloc->s_buf;
3787 struct hns3_rx_com_wl *req;
3788 struct hns3_cmd_desc desc;
3791 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_RX_COM_WL_ALLOC, false);
3793 req = (struct hns3_rx_com_wl *)desc.data;
3794 req->com_wl.high = rte_cpu_to_le_16(buf->self.high >> HNS3_BUF_UNIT_S);
3795 req->com_wl.high |= rte_cpu_to_le_16(BIT(HNS3_RX_PRIV_EN_B));
3797 req->com_wl.low = rte_cpu_to_le_16(buf->self.low >> HNS3_BUF_UNIT_S);
3798 req->com_wl.low |= rte_cpu_to_le_16(BIT(HNS3_RX_PRIV_EN_B));
3800 ret = hns3_cmd_send(hw, &desc, 1);
3802 PMD_INIT_LOG(ERR, "common waterline config cmd failed %d", ret);
3808 hns3_buffer_alloc(struct hns3_hw *hw)
3810 struct hns3_pkt_buf_alloc pkt_buf;
3813 memset(&pkt_buf, 0, sizeof(pkt_buf));
3814 ret = hns3_tx_buffer_calc(hw, &pkt_buf);
3817 "could not calc tx buffer size for all TCs %d",
3822 ret = hns3_tx_buffer_alloc(hw, &pkt_buf);
3824 PMD_INIT_LOG(ERR, "could not alloc tx buffers %d", ret);
3828 ret = hns3_rx_buffer_calc(hw, &pkt_buf);
3831 "could not calc rx priv buffer size for all TCs %d",
3836 ret = hns3_rx_priv_buf_alloc(hw, &pkt_buf);
3838 PMD_INIT_LOG(ERR, "could not alloc rx priv buffer %d", ret);
3842 if (hns3_dev_get_support(hw, DCB)) {
3843 ret = hns3_rx_priv_wl_config(hw, &pkt_buf);
3846 "could not configure rx private waterline %d",
3851 ret = hns3_common_thrd_config(hw, &pkt_buf);
3854 "could not configure common threshold %d",
3860 ret = hns3_common_wl_config(hw, &pkt_buf);
3862 PMD_INIT_LOG(ERR, "could not configure common waterline %d",
3869 hns3_mac_init(struct hns3_hw *hw)
3871 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
3872 struct hns3_mac *mac = &hw->mac;
3873 struct hns3_pf *pf = &hns->pf;
3876 pf->support_sfp_query = true;
3877 mac->link_duplex = RTE_ETH_LINK_FULL_DUPLEX;
3878 ret = hns3_cfg_mac_speed_dup_hw(hw, mac->link_speed, mac->link_duplex);
3880 PMD_INIT_LOG(ERR, "Config mac speed dup fail ret = %d", ret);
3884 mac->link_status = RTE_ETH_LINK_DOWN;
3886 return hns3_config_mtu(hw, pf->mps);
3890 hns3_get_mac_ethertype_cmd_status(uint16_t cmdq_resp, uint8_t resp_code)
3892 #define HNS3_ETHERTYPE_SUCCESS_ADD 0
3893 #define HNS3_ETHERTYPE_ALREADY_ADD 1
3894 #define HNS3_ETHERTYPE_MGR_TBL_OVERFLOW 2
3895 #define HNS3_ETHERTYPE_KEY_CONFLICT 3
3900 "cmdq execute failed for get_mac_ethertype_cmd_status, status=%u.\n",
3905 switch (resp_code) {
3906 case HNS3_ETHERTYPE_SUCCESS_ADD:
3907 case HNS3_ETHERTYPE_ALREADY_ADD:
3910 case HNS3_ETHERTYPE_MGR_TBL_OVERFLOW:
3912 "add mac ethertype failed for manager table overflow.");
3913 return_status = -EIO;
3915 case HNS3_ETHERTYPE_KEY_CONFLICT:
3916 PMD_INIT_LOG(ERR, "add mac ethertype failed for key conflict.");
3917 return_status = -EIO;
3921 "add mac ethertype failed for undefined, code=%u.",
3923 return_status = -EIO;
3927 return return_status;
3931 hns3_add_mgr_tbl(struct hns3_hw *hw,
3932 const struct hns3_mac_mgr_tbl_entry_cmd *req)
3934 struct hns3_cmd_desc desc;
3939 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_MAC_ETHTYPE_ADD, false);
3940 memcpy(desc.data, req, sizeof(struct hns3_mac_mgr_tbl_entry_cmd));
3942 ret = hns3_cmd_send(hw, &desc, 1);
3945 "add mac ethertype failed for cmd_send, ret =%d.",
3950 resp_code = (rte_le_to_cpu_32(desc.data[0]) >> 8) & 0xff;
3951 retval = rte_le_to_cpu_16(desc.retval);
3953 return hns3_get_mac_ethertype_cmd_status(retval, resp_code);
3957 hns3_prepare_mgr_tbl(struct hns3_mac_mgr_tbl_entry_cmd *mgr_table,
3958 int *table_item_num)
3960 struct hns3_mac_mgr_tbl_entry_cmd *tbl;
3963 * In current version, we add one item in management table as below:
3964 * 0x0180C200000E -- LLDP MC address
3967 tbl->flags = HNS3_MAC_MGR_MASK_VLAN_B;
3968 tbl->ethter_type = rte_cpu_to_le_16(HNS3_MAC_ETHERTYPE_LLDP);
3969 tbl->mac_addr_hi32 = rte_cpu_to_le_32(htonl(0x0180C200));
3970 tbl->mac_addr_lo16 = rte_cpu_to_le_16(htons(0x000E));
3971 tbl->i_port_bitmap = 0x1;
3972 *table_item_num = 1;
3976 hns3_init_mgr_tbl(struct hns3_hw *hw)
3978 #define HNS_MAC_MGR_TBL_MAX_SIZE 16
3979 struct hns3_mac_mgr_tbl_entry_cmd mgr_table[HNS_MAC_MGR_TBL_MAX_SIZE];
3984 memset(mgr_table, 0, sizeof(mgr_table));
3985 hns3_prepare_mgr_tbl(mgr_table, &table_item_num);
3986 for (i = 0; i < table_item_num; i++) {
3987 ret = hns3_add_mgr_tbl(hw, &mgr_table[i]);
3989 PMD_INIT_LOG(ERR, "add mac ethertype failed, ret =%d",
3999 hns3_promisc_param_init(struct hns3_promisc_param *param, bool en_uc,
4000 bool en_mc, bool en_bc, int vport_id)
4005 memset(param, 0, sizeof(struct hns3_promisc_param));
4007 param->enable = HNS3_PROMISC_EN_UC;
4009 param->enable |= HNS3_PROMISC_EN_MC;
4011 param->enable |= HNS3_PROMISC_EN_BC;
4012 param->vf_id = vport_id;
4016 hns3_cmd_set_promisc_mode(struct hns3_hw *hw, struct hns3_promisc_param *param)
4018 struct hns3_promisc_cfg_cmd *req;
4019 struct hns3_cmd_desc desc;
4022 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_CFG_PROMISC_MODE, false);
4024 req = (struct hns3_promisc_cfg_cmd *)desc.data;
4025 req->vf_id = param->vf_id;
4026 req->flag = (param->enable << HNS3_PROMISC_EN_B) |
4027 HNS3_PROMISC_TX_EN_B | HNS3_PROMISC_RX_EN_B;
4029 ret = hns3_cmd_send(hw, &desc, 1);
4031 PMD_INIT_LOG(ERR, "Set promisc mode fail, ret = %d", ret);
4037 hns3_set_promisc_mode(struct hns3_hw *hw, bool en_uc_pmc, bool en_mc_pmc)
4039 struct hns3_promisc_param param;
4040 bool en_bc_pmc = true;
4044 * In current version VF is not supported when PF is driven by DPDK
4045 * driver, just need to configure parameters for PF vport.
4047 vf_id = HNS3_PF_FUNC_ID;
4049 hns3_promisc_param_init(¶m, en_uc_pmc, en_mc_pmc, en_bc_pmc, vf_id);
4050 return hns3_cmd_set_promisc_mode(hw, ¶m);
4054 hns3_promisc_init(struct hns3_hw *hw)
4056 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
4057 struct hns3_pf *pf = &hns->pf;
4058 struct hns3_promisc_param param;
4062 ret = hns3_set_promisc_mode(hw, false, false);
4064 PMD_INIT_LOG(ERR, "failed to set promisc mode, ret = %d", ret);
4069 * In current version VFs are not supported when PF is driven by DPDK
4070 * driver. After PF has been taken over by DPDK, the original VF will
4071 * be invalid. So, there is a possibility of entry residues. It should
4072 * clear VFs's promisc mode to avoid unnecessary bandwidth usage
4075 for (func_id = HNS3_1ST_VF_FUNC_ID; func_id < pf->func_num; func_id++) {
4076 hns3_promisc_param_init(¶m, false, false, false, func_id);
4077 ret = hns3_cmd_set_promisc_mode(hw, ¶m);
4079 PMD_INIT_LOG(ERR, "failed to clear vf:%u promisc mode,"
4080 " ret = %d", func_id, ret);
4089 hns3_promisc_uninit(struct hns3_hw *hw)
4091 struct hns3_promisc_param param;
4095 func_id = HNS3_PF_FUNC_ID;
4098 * In current version VFs are not supported when PF is driven by
4099 * DPDK driver, and VFs' promisc mode status has been cleared during
4100 * init and their status will not change. So just clear PF's promisc
4101 * mode status during uninit.
4103 hns3_promisc_param_init(¶m, false, false, false, func_id);
4104 ret = hns3_cmd_set_promisc_mode(hw, ¶m);
4106 PMD_INIT_LOG(ERR, "failed to clear promisc status during"
4107 " uninit, ret = %d", ret);
4111 hns3_dev_promiscuous_enable(struct rte_eth_dev *dev)
4113 bool allmulti = dev->data->all_multicast ? true : false;
4114 struct hns3_adapter *hns = dev->data->dev_private;
4115 struct hns3_hw *hw = &hns->hw;
4120 rte_spinlock_lock(&hw->lock);
4121 ret = hns3_set_promisc_mode(hw, true, true);
4123 rte_spinlock_unlock(&hw->lock);
4124 hns3_err(hw, "failed to enable promiscuous mode, ret = %d",
4130 * When promiscuous mode was enabled, disable the vlan filter to let
4131 * all packets coming in in the receiving direction.
4133 offloads = dev->data->dev_conf.rxmode.offloads;
4134 if (offloads & RTE_ETH_RX_OFFLOAD_VLAN_FILTER) {
4135 ret = hns3_enable_vlan_filter(hns, false);
4137 hns3_err(hw, "failed to enable promiscuous mode due to "
4138 "failure to disable vlan filter, ret = %d",
4140 err = hns3_set_promisc_mode(hw, false, allmulti);
4142 hns3_err(hw, "failed to restore promiscuous "
4143 "status after disable vlan filter "
4144 "failed during enabling promiscuous "
4145 "mode, ret = %d", ret);
4149 rte_spinlock_unlock(&hw->lock);
4155 hns3_dev_promiscuous_disable(struct rte_eth_dev *dev)
4157 bool allmulti = dev->data->all_multicast ? true : false;
4158 struct hns3_adapter *hns = dev->data->dev_private;
4159 struct hns3_hw *hw = &hns->hw;
4164 /* If now in all_multicast mode, must remain in all_multicast mode. */
4165 rte_spinlock_lock(&hw->lock);
4166 ret = hns3_set_promisc_mode(hw, false, allmulti);
4168 rte_spinlock_unlock(&hw->lock);
4169 hns3_err(hw, "failed to disable promiscuous mode, ret = %d",
4173 /* when promiscuous mode was disabled, restore the vlan filter status */
4174 offloads = dev->data->dev_conf.rxmode.offloads;
4175 if (offloads & RTE_ETH_RX_OFFLOAD_VLAN_FILTER) {
4176 ret = hns3_enable_vlan_filter(hns, true);
4178 hns3_err(hw, "failed to disable promiscuous mode due to"
4179 " failure to restore vlan filter, ret = %d",
4181 err = hns3_set_promisc_mode(hw, true, true);
4183 hns3_err(hw, "failed to restore promiscuous "
4184 "status after enabling vlan filter "
4185 "failed during disabling promiscuous "
4186 "mode, ret = %d", ret);
4189 rte_spinlock_unlock(&hw->lock);
4195 hns3_dev_allmulticast_enable(struct rte_eth_dev *dev)
4197 struct hns3_adapter *hns = dev->data->dev_private;
4198 struct hns3_hw *hw = &hns->hw;
4201 if (dev->data->promiscuous)
4204 rte_spinlock_lock(&hw->lock);
4205 ret = hns3_set_promisc_mode(hw, false, true);
4206 rte_spinlock_unlock(&hw->lock);
4208 hns3_err(hw, "failed to enable allmulticast mode, ret = %d",
4215 hns3_dev_allmulticast_disable(struct rte_eth_dev *dev)
4217 struct hns3_adapter *hns = dev->data->dev_private;
4218 struct hns3_hw *hw = &hns->hw;
4221 /* If now in promiscuous mode, must remain in all_multicast mode. */
4222 if (dev->data->promiscuous)
4225 rte_spinlock_lock(&hw->lock);
4226 ret = hns3_set_promisc_mode(hw, false, false);
4227 rte_spinlock_unlock(&hw->lock);
4229 hns3_err(hw, "failed to disable allmulticast mode, ret = %d",
4236 hns3_dev_promisc_restore(struct hns3_adapter *hns)
4238 struct hns3_hw *hw = &hns->hw;
4239 bool allmulti = hw->data->all_multicast ? true : false;
4242 if (hw->data->promiscuous) {
4243 ret = hns3_set_promisc_mode(hw, true, true);
4245 hns3_err(hw, "failed to restore promiscuous mode, "
4250 ret = hns3_set_promisc_mode(hw, false, allmulti);
4252 hns3_err(hw, "failed to restore allmulticast mode, ret = %d",
4258 hns3_get_sfp_info(struct hns3_hw *hw, struct hns3_mac *mac_info)
4260 struct hns3_sfp_info_cmd *resp;
4261 struct hns3_cmd_desc desc;
4264 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_GET_SFP_INFO, true);
4265 resp = (struct hns3_sfp_info_cmd *)desc.data;
4266 resp->query_type = HNS3_ACTIVE_QUERY;
4268 ret = hns3_cmd_send(hw, &desc, 1);
4269 if (ret == -EOPNOTSUPP) {
4270 hns3_warn(hw, "firmware does not support get SFP info,"
4274 hns3_err(hw, "get sfp info failed, ret = %d.", ret);
4279 * In some case, the speed of MAC obtained from firmware may be 0, it
4280 * shouldn't be set to mac->speed.
4282 if (!rte_le_to_cpu_32(resp->sfp_speed))
4285 mac_info->link_speed = rte_le_to_cpu_32(resp->sfp_speed);
4287 * if resp->supported_speed is 0, it means it's an old version
4288 * firmware, do not update these params.
4290 if (resp->supported_speed) {
4291 mac_info->query_type = HNS3_ACTIVE_QUERY;
4292 mac_info->supported_speed =
4293 rte_le_to_cpu_32(resp->supported_speed);
4294 mac_info->support_autoneg = resp->autoneg_ability;
4295 mac_info->link_autoneg = (resp->autoneg == 0) ? RTE_ETH_LINK_FIXED
4296 : RTE_ETH_LINK_AUTONEG;
4298 mac_info->query_type = HNS3_DEFAULT_QUERY;
4305 hns3_check_speed_dup(uint8_t duplex, uint32_t speed)
4307 if (!(speed == RTE_ETH_SPEED_NUM_10M || speed == RTE_ETH_SPEED_NUM_100M))
4308 duplex = RTE_ETH_LINK_FULL_DUPLEX;
4314 hns3_cfg_mac_speed_dup(struct hns3_hw *hw, uint32_t speed, uint8_t duplex)
4316 struct hns3_mac *mac = &hw->mac;
4319 duplex = hns3_check_speed_dup(duplex, speed);
4320 if (mac->link_speed == speed && mac->link_duplex == duplex)
4323 ret = hns3_cfg_mac_speed_dup_hw(hw, speed, duplex);
4327 ret = hns3_port_shaper_update(hw, speed);
4331 mac->link_speed = speed;
4332 mac->link_duplex = duplex;
4338 hns3_update_fiber_link_info(struct hns3_hw *hw)
4340 struct hns3_pf *pf = HNS3_DEV_HW_TO_PF(hw);
4341 struct hns3_mac *mac = &hw->mac;
4342 struct hns3_mac mac_info;
4345 /* If firmware do not support get SFP/qSFP speed, return directly */
4346 if (!pf->support_sfp_query)
4349 memset(&mac_info, 0, sizeof(struct hns3_mac));
4350 ret = hns3_get_sfp_info(hw, &mac_info);
4351 if (ret == -EOPNOTSUPP) {
4352 pf->support_sfp_query = false;
4357 /* Do nothing if no SFP */
4358 if (mac_info.link_speed == RTE_ETH_SPEED_NUM_NONE)
4362 * If query_type is HNS3_ACTIVE_QUERY, it is no need
4363 * to reconfigure the speed of MAC. Otherwise, it indicates
4364 * that the current firmware only supports to obtain the
4365 * speed of the SFP, and the speed of MAC needs to reconfigure.
4367 mac->query_type = mac_info.query_type;
4368 if (mac->query_type == HNS3_ACTIVE_QUERY) {
4369 if (mac_info.link_speed != mac->link_speed) {
4370 ret = hns3_port_shaper_update(hw, mac_info.link_speed);
4375 mac->link_speed = mac_info.link_speed;
4376 mac->supported_speed = mac_info.supported_speed;
4377 mac->support_autoneg = mac_info.support_autoneg;
4378 mac->link_autoneg = mac_info.link_autoneg;
4383 /* Config full duplex for SFP */
4384 return hns3_cfg_mac_speed_dup(hw, mac_info.link_speed,
4385 RTE_ETH_LINK_FULL_DUPLEX);
4389 hns3_parse_copper_phy_params(struct hns3_cmd_desc *desc, struct hns3_mac *mac)
4391 #define HNS3_PHY_SUPPORTED_SPEED_MASK 0x2f
4393 struct hns3_phy_params_bd0_cmd *req;
4396 req = (struct hns3_phy_params_bd0_cmd *)desc[0].data;
4397 mac->link_speed = rte_le_to_cpu_32(req->speed);
4398 mac->link_duplex = hns3_get_bit(req->duplex,
4399 HNS3_PHY_DUPLEX_CFG_B);
4400 mac->link_autoneg = hns3_get_bit(req->autoneg,
4401 HNS3_PHY_AUTONEG_CFG_B);
4402 mac->advertising = rte_le_to_cpu_32(req->advertising);
4403 mac->lp_advertising = rte_le_to_cpu_32(req->lp_advertising);
4404 supported = rte_le_to_cpu_32(req->supported);
4405 mac->supported_speed = supported & HNS3_PHY_SUPPORTED_SPEED_MASK;
4406 mac->support_autoneg = !!(supported & HNS3_PHY_LINK_MODE_AUTONEG_BIT);
4410 hns3_get_copper_phy_params(struct hns3_hw *hw, struct hns3_mac *mac)
4412 struct hns3_cmd_desc desc[HNS3_PHY_PARAM_CFG_BD_NUM];
4416 for (i = 0; i < HNS3_PHY_PARAM_CFG_BD_NUM - 1; i++) {
4417 hns3_cmd_setup_basic_desc(&desc[i], HNS3_OPC_PHY_PARAM_CFG,
4419 desc[i].flag |= rte_cpu_to_le_16(HNS3_CMD_FLAG_NEXT);
4421 hns3_cmd_setup_basic_desc(&desc[i], HNS3_OPC_PHY_PARAM_CFG, true);
4423 ret = hns3_cmd_send(hw, desc, HNS3_PHY_PARAM_CFG_BD_NUM);
4425 hns3_err(hw, "get phy parameters failed, ret = %d.", ret);
4429 hns3_parse_copper_phy_params(desc, mac);
4435 hns3_update_copper_link_info(struct hns3_hw *hw)
4437 struct hns3_mac *mac = &hw->mac;
4438 struct hns3_mac mac_info;
4441 memset(&mac_info, 0, sizeof(struct hns3_mac));
4442 ret = hns3_get_copper_phy_params(hw, &mac_info);
4446 if (mac_info.link_speed != mac->link_speed) {
4447 ret = hns3_port_shaper_update(hw, mac_info.link_speed);
4452 mac->link_speed = mac_info.link_speed;
4453 mac->link_duplex = mac_info.link_duplex;
4454 mac->link_autoneg = mac_info.link_autoneg;
4455 mac->supported_speed = mac_info.supported_speed;
4456 mac->advertising = mac_info.advertising;
4457 mac->lp_advertising = mac_info.lp_advertising;
4458 mac->support_autoneg = mac_info.support_autoneg;
4464 hns3_update_link_info(struct rte_eth_dev *eth_dev)
4466 struct hns3_adapter *hns = eth_dev->data->dev_private;
4467 struct hns3_hw *hw = &hns->hw;
4470 if (hw->mac.media_type == HNS3_MEDIA_TYPE_COPPER)
4471 ret = hns3_update_copper_link_info(hw);
4472 else if (hw->mac.media_type == HNS3_MEDIA_TYPE_FIBER)
4473 ret = hns3_update_fiber_link_info(hw);
4479 hns3_cfg_mac_mode(struct hns3_hw *hw, bool enable)
4481 struct hns3_config_mac_mode_cmd *req;
4482 struct hns3_cmd_desc desc;
4483 uint32_t loop_en = 0;
4487 req = (struct hns3_config_mac_mode_cmd *)desc.data;
4489 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_CONFIG_MAC_MODE, false);
4492 hns3_set_bit(loop_en, HNS3_MAC_TX_EN_B, val);
4493 hns3_set_bit(loop_en, HNS3_MAC_RX_EN_B, val);
4494 hns3_set_bit(loop_en, HNS3_MAC_PAD_TX_B, val);
4495 hns3_set_bit(loop_en, HNS3_MAC_PAD_RX_B, val);
4496 hns3_set_bit(loop_en, HNS3_MAC_1588_TX_B, 0);
4497 hns3_set_bit(loop_en, HNS3_MAC_1588_RX_B, 0);
4498 hns3_set_bit(loop_en, HNS3_MAC_APP_LP_B, 0);
4499 hns3_set_bit(loop_en, HNS3_MAC_LINE_LP_B, 0);
4500 hns3_set_bit(loop_en, HNS3_MAC_FCS_TX_B, val);
4501 hns3_set_bit(loop_en, HNS3_MAC_RX_FCS_B, val);
4504 * If RTE_ETH_RX_OFFLOAD_KEEP_CRC offload is set, MAC will not strip CRC
4505 * when receiving frames. Otherwise, CRC will be stripped.
4507 if (hw->data->dev_conf.rxmode.offloads & RTE_ETH_RX_OFFLOAD_KEEP_CRC)
4508 hns3_set_bit(loop_en, HNS3_MAC_RX_FCS_STRIP_B, 0);
4510 hns3_set_bit(loop_en, HNS3_MAC_RX_FCS_STRIP_B, val);
4511 hns3_set_bit(loop_en, HNS3_MAC_TX_OVERSIZE_TRUNCATE_B, val);
4512 hns3_set_bit(loop_en, HNS3_MAC_RX_OVERSIZE_TRUNCATE_B, val);
4513 hns3_set_bit(loop_en, HNS3_MAC_TX_UNDER_MIN_ERR_B, val);
4514 req->txrx_pad_fcs_loop_en = rte_cpu_to_le_32(loop_en);
4516 ret = hns3_cmd_send(hw, &desc, 1);
4518 PMD_INIT_LOG(ERR, "mac enable fail, ret =%d.", ret);
4524 hns3_get_mac_link_status(struct hns3_hw *hw)
4526 struct hns3_link_status_cmd *req;
4527 struct hns3_cmd_desc desc;
4531 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_QUERY_LINK_STATUS, true);
4532 ret = hns3_cmd_send(hw, &desc, 1);
4534 hns3_err(hw, "get link status cmd failed %d", ret);
4535 return RTE_ETH_LINK_DOWN;
4538 req = (struct hns3_link_status_cmd *)desc.data;
4539 link_status = req->status & HNS3_LINK_STATUS_UP_M;
4541 return !!link_status;
4545 hns3_update_link_status(struct hns3_hw *hw)
4549 state = hns3_get_mac_link_status(hw);
4550 if (state != hw->mac.link_status) {
4551 hw->mac.link_status = state;
4552 hns3_warn(hw, "Link status change to %s!", state ? "up" : "down");
4560 hns3_update_linkstatus_and_event(struct hns3_hw *hw, bool query)
4562 struct rte_eth_dev *dev = &rte_eth_devices[hw->data->port_id];
4563 struct rte_eth_link new_link;
4567 hns3_update_port_link_info(dev);
4569 memset(&new_link, 0, sizeof(new_link));
4570 hns3_setup_linkstatus(dev, &new_link);
4572 ret = rte_eth_linkstatus_set(dev, &new_link);
4573 if (ret == 0 && dev->data->dev_conf.intr_conf.lsc != 0)
4574 hns3_start_report_lse(dev);
4578 hns3_service_handler(void *param)
4580 struct rte_eth_dev *eth_dev = (struct rte_eth_dev *)param;
4581 struct hns3_adapter *hns = eth_dev->data->dev_private;
4582 struct hns3_hw *hw = &hns->hw;
4584 if (!hns3_is_reset_pending(hns))
4585 hns3_update_linkstatus_and_event(hw, true);
4587 hns3_warn(hw, "Cancel the query when reset is pending");
4589 rte_eal_alarm_set(HNS3_SERVICE_INTERVAL, hns3_service_handler, eth_dev);
4593 hns3_init_hardware(struct hns3_adapter *hns)
4595 struct hns3_hw *hw = &hns->hw;
4598 ret = hns3_map_tqp(hw);
4600 PMD_INIT_LOG(ERR, "Failed to map tqp: %d", ret);
4604 ret = hns3_init_umv_space(hw);
4606 PMD_INIT_LOG(ERR, "Failed to init umv space: %d", ret);
4610 ret = hns3_mac_init(hw);
4612 PMD_INIT_LOG(ERR, "Failed to init MAC: %d", ret);
4616 ret = hns3_init_mgr_tbl(hw);
4618 PMD_INIT_LOG(ERR, "Failed to init manager table: %d", ret);
4622 ret = hns3_promisc_init(hw);
4624 PMD_INIT_LOG(ERR, "Failed to init promisc: %d",
4629 ret = hns3_init_vlan_config(hns);
4631 PMD_INIT_LOG(ERR, "Failed to init vlan: %d", ret);
4635 ret = hns3_dcb_init(hw);
4637 PMD_INIT_LOG(ERR, "Failed to init dcb: %d", ret);
4641 ret = hns3_init_fd_config(hns);
4643 PMD_INIT_LOG(ERR, "Failed to init flow director: %d", ret);
4647 ret = hns3_config_tso(hw, HNS3_TSO_MSS_MIN, HNS3_TSO_MSS_MAX);
4649 PMD_INIT_LOG(ERR, "Failed to config tso: %d", ret);
4653 ret = hns3_config_gro(hw, false);
4655 PMD_INIT_LOG(ERR, "Failed to config gro: %d", ret);
4660 * In the initialization clearing the all hardware mapping relationship
4661 * configurations between queues and interrupt vectors is needed, so
4662 * some error caused by the residual configurations, such as the
4663 * unexpected interrupt, can be avoid.
4665 ret = hns3_init_ring_with_vector(hw);
4667 PMD_INIT_LOG(ERR, "Failed to init ring intr vector: %d", ret);
4674 hns3_uninit_umv_space(hw);
4679 hns3_clear_hw(struct hns3_hw *hw)
4681 struct hns3_cmd_desc desc;
4684 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_CLEAR_HW_STATE, false);
4686 ret = hns3_cmd_send(hw, &desc, 1);
4687 if (ret && ret != -EOPNOTSUPP)
4694 hns3_config_all_msix_error(struct hns3_hw *hw, bool enable)
4699 * The new firmware support report more hardware error types by
4700 * msix mode. These errors are defined as RAS errors in hardware
4701 * and belong to a different type from the MSI-x errors processed
4702 * by the network driver.
4704 * Network driver should open the new error report on initialization.
4706 val = hns3_read_dev(hw, HNS3_VECTOR0_OTER_EN_REG);
4707 hns3_set_bit(val, HNS3_VECTOR0_ALL_MSIX_ERR_B, enable ? 1 : 0);
4708 hns3_write_dev(hw, HNS3_VECTOR0_OTER_EN_REG, val);
4712 hns3_set_firber_default_support_speed(struct hns3_hw *hw)
4714 struct hns3_mac *mac = &hw->mac;
4716 switch (mac->link_speed) {
4717 case RTE_ETH_SPEED_NUM_1G:
4718 return HNS3_FIBER_LINK_SPEED_1G_BIT;
4719 case RTE_ETH_SPEED_NUM_10G:
4720 return HNS3_FIBER_LINK_SPEED_10G_BIT;
4721 case RTE_ETH_SPEED_NUM_25G:
4722 return HNS3_FIBER_LINK_SPEED_25G_BIT;
4723 case RTE_ETH_SPEED_NUM_40G:
4724 return HNS3_FIBER_LINK_SPEED_40G_BIT;
4725 case RTE_ETH_SPEED_NUM_50G:
4726 return HNS3_FIBER_LINK_SPEED_50G_BIT;
4727 case RTE_ETH_SPEED_NUM_100G:
4728 return HNS3_FIBER_LINK_SPEED_100G_BIT;
4729 case RTE_ETH_SPEED_NUM_200G:
4730 return HNS3_FIBER_LINK_SPEED_200G_BIT;
4732 hns3_warn(hw, "invalid speed %u Mbps.", mac->link_speed);
4738 * Validity of supported_speed for firber and copper media type can be
4739 * guaranteed by the following policy:
4741 * Although the initialization of the phy in the firmware may not be
4742 * completed, the firmware can guarantees that the supported_speed is
4745 * If the version of firmware supports the acitive query way of the
4746 * HNS3_OPC_GET_SFP_INFO opcode, the supported_speed can be obtained
4747 * through it. If unsupported, use the SFP's speed as the value of the
4751 hns3_get_port_supported_speed(struct rte_eth_dev *eth_dev)
4753 struct hns3_adapter *hns = eth_dev->data->dev_private;
4754 struct hns3_hw *hw = &hns->hw;
4755 struct hns3_mac *mac = &hw->mac;
4758 ret = hns3_update_link_info(eth_dev);
4762 if (mac->media_type == HNS3_MEDIA_TYPE_FIBER) {
4764 * Some firmware does not support the report of supported_speed,
4765 * and only report the effective speed of SFP. In this case, it
4766 * is necessary to use the SFP's speed as the supported_speed.
4768 if (mac->supported_speed == 0)
4769 mac->supported_speed =
4770 hns3_set_firber_default_support_speed(hw);
4777 hns3_get_fc_autoneg_capability(struct hns3_adapter *hns)
4779 struct hns3_mac *mac = &hns->hw.mac;
4781 if (mac->media_type == HNS3_MEDIA_TYPE_COPPER) {
4782 hns->pf.support_fc_autoneg = true;
4787 * Flow control auto-negotiation requires the cooperation of the driver
4788 * and firmware. Currently, the optical port does not support flow
4789 * control auto-negotiation.
4791 hns->pf.support_fc_autoneg = false;
4795 hns3_init_pf(struct rte_eth_dev *eth_dev)
4797 struct rte_device *dev = eth_dev->device;
4798 struct rte_pci_device *pci_dev = RTE_DEV_TO_PCI(dev);
4799 struct hns3_adapter *hns = eth_dev->data->dev_private;
4800 struct hns3_hw *hw = &hns->hw;
4803 PMD_INIT_FUNC_TRACE();
4805 /* Get hardware io base address from pcie BAR2 IO space */
4806 hw->io_base = pci_dev->mem_resource[2].addr;
4808 /* Firmware command queue initialize */
4809 ret = hns3_cmd_init_queue(hw);
4811 PMD_INIT_LOG(ERR, "Failed to init cmd queue: %d", ret);
4812 goto err_cmd_init_queue;
4815 hns3_clear_all_event_cause(hw);
4817 /* Firmware command initialize */
4818 ret = hns3_cmd_init(hw);
4820 PMD_INIT_LOG(ERR, "Failed to init cmd: %d", ret);
4824 hns3_tx_push_init(eth_dev);
4827 * To ensure that the hardware environment is clean during
4828 * initialization, the driver actively clear the hardware environment
4829 * during initialization, including PF and corresponding VFs' vlan, mac,
4830 * flow table configurations, etc.
4832 ret = hns3_clear_hw(hw);
4834 PMD_INIT_LOG(ERR, "failed to clear hardware: %d", ret);
4838 /* Hardware statistics of imissed registers cleared. */
4839 ret = hns3_update_imissed_stats(hw, true);
4841 hns3_err(hw, "clear imissed stats failed, ret = %d", ret);
4845 hns3_config_all_msix_error(hw, true);
4847 ret = rte_intr_callback_register(pci_dev->intr_handle,
4848 hns3_interrupt_handler,
4851 PMD_INIT_LOG(ERR, "Failed to register intr: %d", ret);
4852 goto err_intr_callback_register;
4855 ret = hns3_ptp_init(hw);
4857 goto err_get_config;
4859 /* Enable interrupt */
4860 rte_intr_enable(pci_dev->intr_handle);
4861 hns3_pf_enable_irq0(hw);
4863 /* Get configuration */
4864 ret = hns3_get_configuration(hw);
4866 PMD_INIT_LOG(ERR, "Failed to fetch configuration: %d", ret);
4867 goto err_get_config;
4870 ret = hns3_tqp_stats_init(hw);
4872 goto err_get_config;
4874 ret = hns3_init_hardware(hns);
4876 PMD_INIT_LOG(ERR, "Failed to init hardware: %d", ret);
4880 /* Initialize flow director filter list & hash */
4881 ret = hns3_fdir_filter_init(hns);
4883 PMD_INIT_LOG(ERR, "Failed to alloc hashmap for fdir: %d", ret);
4887 hns3_rss_set_default_args(hw);
4889 ret = hns3_enable_hw_error_intr(hns, true);
4891 PMD_INIT_LOG(ERR, "fail to enable hw error interrupts: %d",
4893 goto err_enable_intr;
4896 ret = hns3_get_port_supported_speed(eth_dev);
4898 PMD_INIT_LOG(ERR, "failed to get speed capabilities supported "
4899 "by device, ret = %d.", ret);
4900 goto err_supported_speed;
4903 hns3_get_fc_autoneg_capability(hns);
4905 hns3_tm_conf_init(eth_dev);
4909 err_supported_speed:
4910 (void)hns3_enable_hw_error_intr(hns, false);
4912 hns3_fdir_filter_uninit(hns);
4914 hns3_uninit_umv_space(hw);
4916 hns3_tqp_stats_uninit(hw);
4918 hns3_pf_disable_irq0(hw);
4919 rte_intr_disable(pci_dev->intr_handle);
4920 hns3_intr_unregister(pci_dev->intr_handle, hns3_interrupt_handler,
4922 err_intr_callback_register:
4924 hns3_cmd_uninit(hw);
4925 hns3_cmd_destroy_queue(hw);
4933 hns3_uninit_pf(struct rte_eth_dev *eth_dev)
4935 struct hns3_adapter *hns = eth_dev->data->dev_private;
4936 struct rte_device *dev = eth_dev->device;
4937 struct rte_pci_device *pci_dev = RTE_DEV_TO_PCI(dev);
4938 struct hns3_hw *hw = &hns->hw;
4940 PMD_INIT_FUNC_TRACE();
4942 hns3_tm_conf_uninit(eth_dev);
4943 hns3_enable_hw_error_intr(hns, false);
4944 hns3_rss_uninit(hns);
4945 (void)hns3_config_gro(hw, false);
4946 hns3_promisc_uninit(hw);
4947 hns3_flow_uninit(eth_dev);
4948 hns3_fdir_filter_uninit(hns);
4949 hns3_uninit_umv_space(hw);
4950 hns3_tqp_stats_uninit(hw);
4951 hns3_config_mac_tnl_int(hw, false);
4952 hns3_pf_disable_irq0(hw);
4953 rte_intr_disable(pci_dev->intr_handle);
4954 hns3_intr_unregister(pci_dev->intr_handle, hns3_interrupt_handler,
4956 hns3_config_all_msix_error(hw, false);
4957 hns3_cmd_uninit(hw);
4958 hns3_cmd_destroy_queue(hw);
4963 hns3_convert_link_speeds2bitmap_copper(uint32_t link_speeds)
4967 switch (link_speeds & ~RTE_ETH_LINK_SPEED_FIXED) {
4968 case RTE_ETH_LINK_SPEED_10M:
4969 speed_bit = HNS3_PHY_LINK_SPEED_10M_BIT;
4971 case RTE_ETH_LINK_SPEED_10M_HD:
4972 speed_bit = HNS3_PHY_LINK_SPEED_10M_HD_BIT;
4974 case RTE_ETH_LINK_SPEED_100M:
4975 speed_bit = HNS3_PHY_LINK_SPEED_100M_BIT;
4977 case RTE_ETH_LINK_SPEED_100M_HD:
4978 speed_bit = HNS3_PHY_LINK_SPEED_100M_HD_BIT;
4980 case RTE_ETH_LINK_SPEED_1G:
4981 speed_bit = HNS3_PHY_LINK_SPEED_1000M_BIT;
4992 hns3_convert_link_speeds2bitmap_fiber(uint32_t link_speeds)
4996 switch (link_speeds & ~RTE_ETH_LINK_SPEED_FIXED) {
4997 case RTE_ETH_LINK_SPEED_1G:
4998 speed_bit = HNS3_FIBER_LINK_SPEED_1G_BIT;
5000 case RTE_ETH_LINK_SPEED_10G:
5001 speed_bit = HNS3_FIBER_LINK_SPEED_10G_BIT;
5003 case RTE_ETH_LINK_SPEED_25G:
5004 speed_bit = HNS3_FIBER_LINK_SPEED_25G_BIT;
5006 case RTE_ETH_LINK_SPEED_40G:
5007 speed_bit = HNS3_FIBER_LINK_SPEED_40G_BIT;
5009 case RTE_ETH_LINK_SPEED_50G:
5010 speed_bit = HNS3_FIBER_LINK_SPEED_50G_BIT;
5012 case RTE_ETH_LINK_SPEED_100G:
5013 speed_bit = HNS3_FIBER_LINK_SPEED_100G_BIT;
5015 case RTE_ETH_LINK_SPEED_200G:
5016 speed_bit = HNS3_FIBER_LINK_SPEED_200G_BIT;
5027 hns3_check_port_speed(struct hns3_hw *hw, uint32_t link_speeds)
5029 struct hns3_mac *mac = &hw->mac;
5030 uint32_t supported_speed = mac->supported_speed;
5031 uint32_t speed_bit = 0;
5033 if (mac->media_type == HNS3_MEDIA_TYPE_COPPER)
5034 speed_bit = hns3_convert_link_speeds2bitmap_copper(link_speeds);
5035 else if (mac->media_type == HNS3_MEDIA_TYPE_FIBER)
5036 speed_bit = hns3_convert_link_speeds2bitmap_fiber(link_speeds);
5038 if (!(speed_bit & supported_speed)) {
5039 hns3_err(hw, "link_speeds(0x%x) exceeds the supported speed capability or is incorrect.",
5047 static inline uint32_t
5048 hns3_get_link_speed(uint32_t link_speeds)
5050 uint32_t speed = RTE_ETH_SPEED_NUM_NONE;
5052 if (link_speeds & RTE_ETH_LINK_SPEED_10M ||
5053 link_speeds & RTE_ETH_LINK_SPEED_10M_HD)
5054 speed = RTE_ETH_SPEED_NUM_10M;
5055 if (link_speeds & RTE_ETH_LINK_SPEED_100M ||
5056 link_speeds & RTE_ETH_LINK_SPEED_100M_HD)
5057 speed = RTE_ETH_SPEED_NUM_100M;
5058 if (link_speeds & RTE_ETH_LINK_SPEED_1G)
5059 speed = RTE_ETH_SPEED_NUM_1G;
5060 if (link_speeds & RTE_ETH_LINK_SPEED_10G)
5061 speed = RTE_ETH_SPEED_NUM_10G;
5062 if (link_speeds & RTE_ETH_LINK_SPEED_25G)
5063 speed = RTE_ETH_SPEED_NUM_25G;
5064 if (link_speeds & RTE_ETH_LINK_SPEED_40G)
5065 speed = RTE_ETH_SPEED_NUM_40G;
5066 if (link_speeds & RTE_ETH_LINK_SPEED_50G)
5067 speed = RTE_ETH_SPEED_NUM_50G;
5068 if (link_speeds & RTE_ETH_LINK_SPEED_100G)
5069 speed = RTE_ETH_SPEED_NUM_100G;
5070 if (link_speeds & RTE_ETH_LINK_SPEED_200G)
5071 speed = RTE_ETH_SPEED_NUM_200G;
5077 hns3_get_link_duplex(uint32_t link_speeds)
5079 if ((link_speeds & RTE_ETH_LINK_SPEED_10M_HD) ||
5080 (link_speeds & RTE_ETH_LINK_SPEED_100M_HD))
5081 return RTE_ETH_LINK_HALF_DUPLEX;
5083 return RTE_ETH_LINK_FULL_DUPLEX;
5087 hns3_set_copper_port_link_speed(struct hns3_hw *hw,
5088 struct hns3_set_link_speed_cfg *cfg)
5090 struct hns3_cmd_desc desc[HNS3_PHY_PARAM_CFG_BD_NUM];
5091 struct hns3_phy_params_bd0_cmd *req;
5094 for (i = 0; i < HNS3_PHY_PARAM_CFG_BD_NUM - 1; i++) {
5095 hns3_cmd_setup_basic_desc(&desc[i], HNS3_OPC_PHY_PARAM_CFG,
5097 desc[i].flag |= rte_cpu_to_le_16(HNS3_CMD_FLAG_NEXT);
5099 hns3_cmd_setup_basic_desc(&desc[i], HNS3_OPC_PHY_PARAM_CFG, false);
5100 req = (struct hns3_phy_params_bd0_cmd *)desc[0].data;
5101 req->autoneg = cfg->autoneg;
5104 * The full speed capability is used to negotiate when
5105 * auto-negotiation is enabled.
5108 req->advertising = HNS3_PHY_LINK_SPEED_10M_BIT |
5109 HNS3_PHY_LINK_SPEED_10M_HD_BIT |
5110 HNS3_PHY_LINK_SPEED_100M_BIT |
5111 HNS3_PHY_LINK_SPEED_100M_HD_BIT |
5112 HNS3_PHY_LINK_SPEED_1000M_BIT;
5114 req->speed = cfg->speed;
5115 req->duplex = cfg->duplex;
5118 return hns3_cmd_send(hw, desc, HNS3_PHY_PARAM_CFG_BD_NUM);
5122 hns3_set_autoneg(struct hns3_hw *hw, bool enable)
5124 struct hns3_config_auto_neg_cmd *req;
5125 struct hns3_cmd_desc desc;
5129 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_CONFIG_AN_MODE, false);
5131 req = (struct hns3_config_auto_neg_cmd *)desc.data;
5133 hns3_set_bit(flag, HNS3_MAC_CFG_AN_EN_B, 1);
5134 req->cfg_an_cmd_flag = rte_cpu_to_le_32(flag);
5136 ret = hns3_cmd_send(hw, &desc, 1);
5138 hns3_err(hw, "autoneg set cmd failed, ret = %d.", ret);
5144 hns3_set_fiber_port_link_speed(struct hns3_hw *hw,
5145 struct hns3_set_link_speed_cfg *cfg)
5149 if (hw->mac.support_autoneg) {
5150 ret = hns3_set_autoneg(hw, cfg->autoneg);
5152 hns3_err(hw, "failed to configure auto-negotiation.");
5157 * To enable auto-negotiation, we only need to open the switch
5158 * of auto-negotiation, then firmware sets all speed
5166 * Some hardware doesn't support auto-negotiation, but users may not
5167 * configure link_speeds (default 0), which means auto-negotiation.
5168 * In this case, a warning message need to be printed, instead of
5172 hns3_warn(hw, "auto-negotiation is not supported, use default fixed speed!");
5176 return hns3_cfg_mac_speed_dup(hw, cfg->speed, cfg->duplex);
5180 hns3_set_port_link_speed(struct hns3_hw *hw,
5181 struct hns3_set_link_speed_cfg *cfg)
5185 if (hw->mac.media_type == HNS3_MEDIA_TYPE_COPPER) {
5186 #if defined(RTE_HNS3_ONLY_1630_FPGA)
5187 struct hns3_pf *pf = HNS3_DEV_HW_TO_PF(hw);
5192 ret = hns3_set_copper_port_link_speed(hw, cfg);
5194 hns3_err(hw, "failed to set copper port link speed,"
5198 } else if (hw->mac.media_type == HNS3_MEDIA_TYPE_FIBER) {
5199 ret = hns3_set_fiber_port_link_speed(hw, cfg);
5201 hns3_err(hw, "failed to set fiber port link speed,"
5211 hns3_apply_link_speed(struct hns3_hw *hw)
5213 struct rte_eth_conf *conf = &hw->data->dev_conf;
5214 struct hns3_set_link_speed_cfg cfg;
5216 memset(&cfg, 0, sizeof(struct hns3_set_link_speed_cfg));
5217 cfg.autoneg = (conf->link_speeds == RTE_ETH_LINK_SPEED_AUTONEG) ?
5218 RTE_ETH_LINK_AUTONEG : RTE_ETH_LINK_FIXED;
5219 if (cfg.autoneg != RTE_ETH_LINK_AUTONEG) {
5220 cfg.speed = hns3_get_link_speed(conf->link_speeds);
5221 cfg.duplex = hns3_get_link_duplex(conf->link_speeds);
5224 return hns3_set_port_link_speed(hw, &cfg);
5228 hns3_do_start(struct hns3_adapter *hns, bool reset_queue)
5230 struct hns3_hw *hw = &hns->hw;
5234 ret = hns3_update_queue_map_configure(hns);
5236 hns3_err(hw, "failed to update queue mapping configuration, ret = %d",
5241 /* Note: hns3_tm_conf_update must be called after configuring DCB. */
5242 ret = hns3_tm_conf_update(hw);
5244 PMD_INIT_LOG(ERR, "failed to update tm conf, ret = %d.", ret);
5248 hns3_enable_rxd_adv_layout(hw);
5250 ret = hns3_init_queues(hns, reset_queue);
5252 PMD_INIT_LOG(ERR, "failed to init queues, ret = %d.", ret);
5256 link_en = hw->set_link_down ? false : true;
5257 ret = hns3_cfg_mac_mode(hw, link_en);
5259 PMD_INIT_LOG(ERR, "failed to enable MAC, ret = %d", ret);
5260 goto err_config_mac_mode;
5263 ret = hns3_apply_link_speed(hw);
5265 goto err_set_link_speed;
5270 (void)hns3_cfg_mac_mode(hw, false);
5272 err_config_mac_mode:
5273 hns3_dev_release_mbufs(hns);
5275 * Here is exception handling, hns3_reset_all_tqps will have the
5276 * corresponding error message if it is handled incorrectly, so it is
5277 * not necessary to check hns3_reset_all_tqps return value, here keep
5278 * ret as the error code causing the exception.
5280 (void)hns3_reset_all_tqps(hns);
5285 hns3_map_rx_interrupt(struct rte_eth_dev *dev)
5287 struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
5288 struct rte_intr_handle *intr_handle = pci_dev->intr_handle;
5289 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
5290 uint16_t base = RTE_INTR_VEC_ZERO_OFFSET;
5291 uint16_t vec = RTE_INTR_VEC_ZERO_OFFSET;
5292 uint32_t intr_vector;
5297 * hns3 needs a separate interrupt to be used as event interrupt which
5298 * could not be shared with task queue pair, so KERNEL drivers need
5299 * support multiple interrupt vectors.
5301 if (dev->data->dev_conf.intr_conf.rxq == 0 ||
5302 !rte_intr_cap_multiple(intr_handle))
5305 rte_intr_disable(intr_handle);
5306 intr_vector = hw->used_rx_queues;
5307 /* creates event fd for each intr vector when MSIX is used */
5308 if (rte_intr_efd_enable(intr_handle, intr_vector))
5311 /* Allocate vector list */
5312 if (rte_intr_vec_list_alloc(intr_handle, "intr_vec",
5313 hw->used_rx_queues)) {
5314 hns3_err(hw, "failed to allocate %u rx_queues intr_vec",
5315 hw->used_rx_queues);
5317 goto alloc_intr_vec_error;
5320 if (rte_intr_allow_others(intr_handle)) {
5321 vec = RTE_INTR_VEC_RXTX_OFFSET;
5322 base = RTE_INTR_VEC_RXTX_OFFSET;
5325 for (q_id = 0; q_id < hw->used_rx_queues; q_id++) {
5326 ret = hns3_bind_ring_with_vector(hw, vec, true,
5327 HNS3_RING_TYPE_RX, q_id);
5329 goto bind_vector_error;
5331 if (rte_intr_vec_list_index_set(intr_handle, q_id, vec))
5332 goto bind_vector_error;
5334 * If there are not enough efds (e.g. not enough interrupt),
5335 * remaining queues will be bond to the last interrupt.
5337 if (vec < base + rte_intr_nb_efd_get(intr_handle) - 1)
5340 rte_intr_enable(intr_handle);
5344 rte_intr_vec_list_free(intr_handle);
5345 alloc_intr_vec_error:
5346 rte_intr_efd_disable(intr_handle);
5351 hns3_restore_rx_interrupt(struct hns3_hw *hw)
5353 struct rte_eth_dev *dev = &rte_eth_devices[hw->data->port_id];
5354 struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
5355 struct rte_intr_handle *intr_handle = pci_dev->intr_handle;
5359 if (dev->data->dev_conf.intr_conf.rxq == 0)
5362 if (rte_intr_dp_is_en(intr_handle)) {
5363 for (q_id = 0; q_id < hw->used_rx_queues; q_id++) {
5364 ret = hns3_bind_ring_with_vector(hw,
5365 rte_intr_vec_list_index_get(intr_handle,
5367 true, HNS3_RING_TYPE_RX, q_id);
5377 hns3_restore_filter(struct rte_eth_dev *dev)
5379 hns3_restore_rss_filter(dev);
5383 hns3_dev_start(struct rte_eth_dev *dev)
5385 struct hns3_adapter *hns = dev->data->dev_private;
5386 struct hns3_hw *hw = &hns->hw;
5387 bool old_state = hw->set_link_down;
5390 PMD_INIT_FUNC_TRACE();
5391 if (__atomic_load_n(&hw->reset.resetting, __ATOMIC_RELAXED))
5394 rte_spinlock_lock(&hw->lock);
5395 hw->adapter_state = HNS3_NIC_STARTING;
5398 * If the dev_set_link_down() API has been called, the "set_link_down"
5399 * flag can be cleared by dev_start() API. In addition, the flag should
5400 * also be cleared before calling hns3_do_start() so that MAC can be
5401 * enabled in dev_start stage.
5403 hw->set_link_down = false;
5404 ret = hns3_do_start(hns, true);
5408 ret = hns3_map_rx_interrupt(dev);
5410 goto map_rx_inter_err;
5413 * There are three register used to control the status of a TQP
5414 * (contains a pair of Tx queue and Rx queue) in the new version network
5415 * engine. One is used to control the enabling of Tx queue, the other is
5416 * used to control the enabling of Rx queue, and the last is the master
5417 * switch used to control the enabling of the tqp. The Tx register and
5418 * TQP register must be enabled at the same time to enable a Tx queue.
5419 * The same applies to the Rx queue. For the older network engine, this
5420 * function only refresh the enabled flag, and it is used to update the
5421 * status of queue in the dpdk framework.
5423 ret = hns3_start_all_txqs(dev);
5425 goto map_rx_inter_err;
5427 ret = hns3_start_all_rxqs(dev);
5429 goto start_all_rxqs_fail;
5431 hw->adapter_state = HNS3_NIC_STARTED;
5432 rte_spinlock_unlock(&hw->lock);
5434 hns3_rx_scattered_calc(dev);
5435 hns3_set_rxtx_function(dev);
5436 hns3_mp_req_start_rxtx(dev);
5438 hns3_restore_filter(dev);
5440 /* Enable interrupt of all rx queues before enabling queues */
5441 hns3_dev_all_rx_queue_intr_enable(hw, true);
5444 * After finished the initialization, enable tqps to receive/transmit
5445 * packets and refresh all queue status.
5447 hns3_start_tqps(hw);
5449 hns3_tm_dev_start_proc(hw);
5451 if (dev->data->dev_conf.intr_conf.lsc != 0)
5452 hns3_dev_link_update(dev, 0);
5453 rte_eal_alarm_set(HNS3_SERVICE_INTERVAL, hns3_service_handler, dev);
5455 hns3_info(hw, "hns3 dev start successful!");
5459 start_all_rxqs_fail:
5460 hns3_stop_all_txqs(dev);
5462 (void)hns3_do_stop(hns);
5464 hw->set_link_down = old_state;
5465 hw->adapter_state = HNS3_NIC_CONFIGURED;
5466 rte_spinlock_unlock(&hw->lock);
5472 hns3_do_stop(struct hns3_adapter *hns)
5474 struct hns3_hw *hw = &hns->hw;
5478 * The "hns3_do_stop" function will also be called by .stop_service to
5479 * prepare reset. At the time of global or IMP reset, the command cannot
5480 * be sent to stop the tx/rx queues. The mbuf in Tx/Rx queues may be
5481 * accessed during the reset process. So the mbuf can not be released
5482 * during reset and is required to be released after the reset is
5485 if (__atomic_load_n(&hw->reset.resetting, __ATOMIC_RELAXED) == 0)
5486 hns3_dev_release_mbufs(hns);
5488 ret = hns3_cfg_mac_mode(hw, false);
5491 hw->mac.link_status = RTE_ETH_LINK_DOWN;
5493 if (__atomic_load_n(&hw->reset.disable_cmd, __ATOMIC_RELAXED) == 0) {
5494 hns3_configure_all_mac_addr(hns, true);
5495 ret = hns3_reset_all_tqps(hns);
5497 hns3_err(hw, "failed to reset all queues ret = %d.",
5507 hns3_unmap_rx_interrupt(struct rte_eth_dev *dev)
5509 struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
5510 struct rte_intr_handle *intr_handle = pci_dev->intr_handle;
5511 struct hns3_adapter *hns = dev->data->dev_private;
5512 struct hns3_hw *hw = &hns->hw;
5513 uint8_t base = RTE_INTR_VEC_ZERO_OFFSET;
5514 uint8_t vec = RTE_INTR_VEC_ZERO_OFFSET;
5517 if (dev->data->dev_conf.intr_conf.rxq == 0)
5520 /* unmap the ring with vector */
5521 if (rte_intr_allow_others(intr_handle)) {
5522 vec = RTE_INTR_VEC_RXTX_OFFSET;
5523 base = RTE_INTR_VEC_RXTX_OFFSET;
5525 if (rte_intr_dp_is_en(intr_handle)) {
5526 for (q_id = 0; q_id < hw->used_rx_queues; q_id++) {
5527 (void)hns3_bind_ring_with_vector(hw, vec, false,
5530 if (vec < base + rte_intr_nb_efd_get(intr_handle)
5535 /* Clean datapath event and queue/vec mapping */
5536 rte_intr_efd_disable(intr_handle);
5537 rte_intr_vec_list_free(intr_handle);
5541 hns3_dev_stop(struct rte_eth_dev *dev)
5543 struct hns3_adapter *hns = dev->data->dev_private;
5544 struct hns3_hw *hw = &hns->hw;
5546 PMD_INIT_FUNC_TRACE();
5547 dev->data->dev_started = 0;
5549 hw->adapter_state = HNS3_NIC_STOPPING;
5550 hns3_set_rxtx_function(dev);
5552 /* Disable datapath on secondary process. */
5553 hns3_mp_req_stop_rxtx(dev);
5554 /* Prevent crashes when queues are still in use. */
5555 rte_delay_ms(hw->cfg_max_queues);
5557 rte_spinlock_lock(&hw->lock);
5558 if (__atomic_load_n(&hw->reset.resetting, __ATOMIC_RELAXED) == 0) {
5559 hns3_tm_dev_stop_proc(hw);
5560 hns3_config_mac_tnl_int(hw, false);
5563 hns3_unmap_rx_interrupt(dev);
5564 hw->adapter_state = HNS3_NIC_CONFIGURED;
5566 hns3_rx_scattered_reset(dev);
5567 rte_eal_alarm_cancel(hns3_service_handler, dev);
5568 hns3_stop_report_lse(dev);
5569 rte_spinlock_unlock(&hw->lock);
5575 hns3_dev_close(struct rte_eth_dev *eth_dev)
5577 struct hns3_adapter *hns = eth_dev->data->dev_private;
5578 struct hns3_hw *hw = &hns->hw;
5581 if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
5582 hns3_mp_uninit(eth_dev);
5586 if (hw->adapter_state == HNS3_NIC_STARTED)
5587 ret = hns3_dev_stop(eth_dev);
5589 hw->adapter_state = HNS3_NIC_CLOSING;
5590 hns3_reset_abort(hns);
5591 hw->adapter_state = HNS3_NIC_CLOSED;
5593 hns3_configure_all_mc_mac_addr(hns, true);
5594 hns3_remove_all_vlan_table(hns);
5595 hns3_vlan_txvlan_cfg(hns, HNS3_PORT_BASE_VLAN_DISABLE, 0);
5596 hns3_uninit_pf(eth_dev);
5597 hns3_free_all_queues(eth_dev);
5598 rte_free(hw->reset.wait_data);
5599 hns3_mp_uninit(eth_dev);
5600 hns3_warn(hw, "Close port %u finished", hw->data->port_id);
5606 hns3_get_autoneg_rxtx_pause_copper(struct hns3_hw *hw, bool *rx_pause,
5609 struct hns3_mac *mac = &hw->mac;
5610 uint32_t advertising = mac->advertising;
5611 uint32_t lp_advertising = mac->lp_advertising;
5615 if (advertising & lp_advertising & HNS3_PHY_LINK_MODE_PAUSE_BIT) {
5618 } else if (advertising & lp_advertising &
5619 HNS3_PHY_LINK_MODE_ASYM_PAUSE_BIT) {
5620 if (advertising & HNS3_PHY_LINK_MODE_PAUSE_BIT)
5622 else if (lp_advertising & HNS3_PHY_LINK_MODE_PAUSE_BIT)
5627 static enum hns3_fc_mode
5628 hns3_get_autoneg_fc_mode(struct hns3_hw *hw)
5630 enum hns3_fc_mode current_mode;
5631 bool rx_pause = false;
5632 bool tx_pause = false;
5634 switch (hw->mac.media_type) {
5635 case HNS3_MEDIA_TYPE_COPPER:
5636 hns3_get_autoneg_rxtx_pause_copper(hw, &rx_pause, &tx_pause);
5640 * Flow control auto-negotiation is not supported for fiber and
5641 * backpalne media type.
5643 case HNS3_MEDIA_TYPE_FIBER:
5644 case HNS3_MEDIA_TYPE_BACKPLANE:
5645 hns3_err(hw, "autoneg FC mode can't be obtained, but flow control auto-negotiation is enabled.");
5646 current_mode = hw->requested_fc_mode;
5649 hns3_err(hw, "autoneg FC mode can't be obtained for unknown media type(%u).",
5650 hw->mac.media_type);
5651 current_mode = HNS3_FC_NONE;
5655 if (rx_pause && tx_pause)
5656 current_mode = HNS3_FC_FULL;
5658 current_mode = HNS3_FC_RX_PAUSE;
5660 current_mode = HNS3_FC_TX_PAUSE;
5662 current_mode = HNS3_FC_NONE;
5665 return current_mode;
5668 static enum hns3_fc_mode
5669 hns3_get_current_fc_mode(struct rte_eth_dev *dev)
5671 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
5672 struct hns3_pf *pf = HNS3_DEV_PRIVATE_TO_PF(dev->data->dev_private);
5673 struct hns3_mac *mac = &hw->mac;
5676 * When the flow control mode is obtained, the device may not complete
5677 * auto-negotiation. It is necessary to wait for link establishment.
5679 (void)hns3_dev_link_update(dev, 1);
5682 * If the link auto-negotiation of the nic is disabled, or the flow
5683 * control auto-negotiation is not supported, the forced flow control
5686 if (mac->link_autoneg == 0 || !pf->support_fc_autoneg)
5687 return hw->requested_fc_mode;
5689 return hns3_get_autoneg_fc_mode(hw);
5693 hns3_flow_ctrl_get(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
5695 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
5696 struct hns3_pf *pf = HNS3_DEV_PRIVATE_TO_PF(dev->data->dev_private);
5697 enum hns3_fc_mode current_mode;
5699 current_mode = hns3_get_current_fc_mode(dev);
5700 switch (current_mode) {
5702 fc_conf->mode = RTE_ETH_FC_FULL;
5704 case HNS3_FC_TX_PAUSE:
5705 fc_conf->mode = RTE_ETH_FC_TX_PAUSE;
5707 case HNS3_FC_RX_PAUSE:
5708 fc_conf->mode = RTE_ETH_FC_RX_PAUSE;
5712 fc_conf->mode = RTE_ETH_FC_NONE;
5716 fc_conf->pause_time = pf->pause_time;
5717 fc_conf->autoneg = pf->support_fc_autoneg ? hw->mac.link_autoneg : 0;
5723 hns3_check_fc_autoneg_valid(struct hns3_hw *hw, uint8_t autoneg)
5725 struct hns3_pf *pf = HNS3_DEV_HW_TO_PF(hw);
5727 if (!pf->support_fc_autoneg) {
5729 hns3_err(hw, "unsupported fc auto-negotiation setting.");
5734 * Flow control auto-negotiation of the NIC is not supported,
5735 * but other auto-negotiation features may be supported.
5737 if (autoneg != hw->mac.link_autoneg) {
5738 hns3_err(hw, "please use 'link_speeds' in struct rte_eth_conf to disable autoneg!");
5746 * If flow control auto-negotiation of the NIC is supported, all
5747 * auto-negotiation features are supported.
5749 if (autoneg != hw->mac.link_autoneg) {
5750 hns3_err(hw, "please use 'link_speeds' in struct rte_eth_conf to change autoneg!");
5758 hns3_flow_ctrl_set(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
5760 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
5761 struct hns3_pf *pf = HNS3_DEV_PRIVATE_TO_PF(dev->data->dev_private);
5764 if (fc_conf->high_water || fc_conf->low_water ||
5765 fc_conf->send_xon || fc_conf->mac_ctrl_frame_fwd) {
5766 hns3_err(hw, "Unsupported flow control settings specified, "
5767 "high_water(%u), low_water(%u), send_xon(%u) and "
5768 "mac_ctrl_frame_fwd(%u) must be set to '0'",
5769 fc_conf->high_water, fc_conf->low_water,
5770 fc_conf->send_xon, fc_conf->mac_ctrl_frame_fwd);
5774 ret = hns3_check_fc_autoneg_valid(hw, fc_conf->autoneg);
5778 if (!fc_conf->pause_time) {
5779 hns3_err(hw, "Invalid pause time %u setting.",
5780 fc_conf->pause_time);
5784 if (!(hw->current_fc_status == HNS3_FC_STATUS_NONE ||
5785 hw->current_fc_status == HNS3_FC_STATUS_MAC_PAUSE)) {
5786 hns3_err(hw, "PFC is enabled. Cannot set MAC pause. "
5787 "current_fc_status = %d", hw->current_fc_status);
5791 if (hw->num_tc > 1 && !pf->support_multi_tc_pause) {
5792 hns3_err(hw, "in multi-TC scenarios, MAC pause is not supported.");
5796 rte_spinlock_lock(&hw->lock);
5797 ret = hns3_fc_enable(dev, fc_conf);
5798 rte_spinlock_unlock(&hw->lock);
5804 hns3_priority_flow_ctrl_set(struct rte_eth_dev *dev,
5805 struct rte_eth_pfc_conf *pfc_conf)
5807 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
5810 if (!hns3_dev_get_support(hw, DCB)) {
5811 hns3_err(hw, "This port does not support dcb configurations.");
5815 if (pfc_conf->fc.high_water || pfc_conf->fc.low_water ||
5816 pfc_conf->fc.send_xon || pfc_conf->fc.mac_ctrl_frame_fwd) {
5817 hns3_err(hw, "Unsupported flow control settings specified, "
5818 "high_water(%u), low_water(%u), send_xon(%u) and "
5819 "mac_ctrl_frame_fwd(%u) must be set to '0'",
5820 pfc_conf->fc.high_water, pfc_conf->fc.low_water,
5821 pfc_conf->fc.send_xon,
5822 pfc_conf->fc.mac_ctrl_frame_fwd);
5825 if (pfc_conf->fc.autoneg) {
5826 hns3_err(hw, "Unsupported fc auto-negotiation setting.");
5829 if (pfc_conf->fc.pause_time == 0) {
5830 hns3_err(hw, "Invalid pause time %u setting.",
5831 pfc_conf->fc.pause_time);
5835 if (!(hw->current_fc_status == HNS3_FC_STATUS_NONE ||
5836 hw->current_fc_status == HNS3_FC_STATUS_PFC)) {
5837 hns3_err(hw, "MAC pause is enabled. Cannot set PFC."
5838 "current_fc_status = %d", hw->current_fc_status);
5842 rte_spinlock_lock(&hw->lock);
5843 ret = hns3_dcb_pfc_enable(dev, pfc_conf);
5844 rte_spinlock_unlock(&hw->lock);
5850 hns3_get_dcb_info(struct rte_eth_dev *dev, struct rte_eth_dcb_info *dcb_info)
5852 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
5853 struct hns3_pf *pf = HNS3_DEV_PRIVATE_TO_PF(dev->data->dev_private);
5854 enum rte_eth_rx_mq_mode mq_mode = dev->data->dev_conf.rxmode.mq_mode;
5857 rte_spinlock_lock(&hw->lock);
5858 if ((uint32_t)mq_mode & RTE_ETH_MQ_RX_DCB_FLAG)
5859 dcb_info->nb_tcs = pf->local_max_tc;
5861 dcb_info->nb_tcs = 1;
5863 for (i = 0; i < HNS3_MAX_USER_PRIO; i++)
5864 dcb_info->prio_tc[i] = hw->dcb_info.prio_tc[i];
5865 for (i = 0; i < dcb_info->nb_tcs; i++)
5866 dcb_info->tc_bws[i] = hw->dcb_info.pg_info[0].tc_dwrr[i];
5868 for (i = 0; i < hw->num_tc; i++) {
5869 dcb_info->tc_queue.tc_rxq[0][i].base = hw->alloc_rss_size * i;
5870 dcb_info->tc_queue.tc_txq[0][i].base =
5871 hw->tc_queue[i].tqp_offset;
5872 dcb_info->tc_queue.tc_rxq[0][i].nb_queue = hw->alloc_rss_size;
5873 dcb_info->tc_queue.tc_txq[0][i].nb_queue =
5874 hw->tc_queue[i].tqp_count;
5876 rte_spinlock_unlock(&hw->lock);
5882 hns3_reinit_dev(struct hns3_adapter *hns)
5884 struct hns3_hw *hw = &hns->hw;
5887 ret = hns3_cmd_init(hw);
5889 hns3_err(hw, "Failed to init cmd: %d", ret);
5893 ret = hns3_reset_all_tqps(hns);
5895 hns3_err(hw, "Failed to reset all queues: %d", ret);
5899 ret = hns3_init_hardware(hns);
5901 hns3_err(hw, "Failed to init hardware: %d", ret);
5905 ret = hns3_enable_hw_error_intr(hns, true);
5907 hns3_err(hw, "fail to enable hw error interrupts: %d",
5911 hns3_info(hw, "Reset done, driver initialization finished.");
5917 is_pf_reset_done(struct hns3_hw *hw)
5919 uint32_t val, reg, reg_bit;
5921 switch (hw->reset.level) {
5922 case HNS3_IMP_RESET:
5923 reg = HNS3_GLOBAL_RESET_REG;
5924 reg_bit = HNS3_IMP_RESET_BIT;
5926 case HNS3_GLOBAL_RESET:
5927 reg = HNS3_GLOBAL_RESET_REG;
5928 reg_bit = HNS3_GLOBAL_RESET_BIT;
5930 case HNS3_FUNC_RESET:
5931 reg = HNS3_FUN_RST_ING;
5932 reg_bit = HNS3_FUN_RST_ING_B;
5934 case HNS3_FLR_RESET:
5936 hns3_err(hw, "Wait for unsupported reset level: %d",
5940 val = hns3_read_dev(hw, reg);
5941 if (hns3_get_bit(val, reg_bit))
5948 hns3_is_reset_pending(struct hns3_adapter *hns)
5950 struct hns3_hw *hw = &hns->hw;
5951 enum hns3_reset_level reset;
5953 hns3_check_event_cause(hns, NULL);
5954 reset = hns3_get_reset_level(hns, &hw->reset.pending);
5955 if (reset != HNS3_NONE_RESET && hw->reset.level != HNS3_NONE_RESET &&
5956 hw->reset.level < reset) {
5957 hns3_warn(hw, "High level reset %d is pending", reset);
5960 reset = hns3_get_reset_level(hns, &hw->reset.request);
5961 if (reset != HNS3_NONE_RESET && hw->reset.level != HNS3_NONE_RESET &&
5962 hw->reset.level < reset) {
5963 hns3_warn(hw, "High level reset %d is request", reset);
5970 hns3_wait_hardware_ready(struct hns3_adapter *hns)
5972 struct hns3_hw *hw = &hns->hw;
5973 struct hns3_wait_data *wait_data = hw->reset.wait_data;
5976 if (wait_data->result == HNS3_WAIT_SUCCESS)
5978 else if (wait_data->result == HNS3_WAIT_TIMEOUT) {
5979 hns3_clock_gettime(&tv);
5980 hns3_warn(hw, "Reset step4 hardware not ready after reset time=%ld.%.6ld",
5981 tv.tv_sec, tv.tv_usec);
5983 } else if (wait_data->result == HNS3_WAIT_REQUEST)
5986 wait_data->hns = hns;
5987 wait_data->check_completion = is_pf_reset_done;
5988 wait_data->end_ms = (uint64_t)HNS3_RESET_WAIT_CNT *
5989 HNS3_RESET_WAIT_MS + hns3_clock_gettime_ms();
5990 wait_data->interval = HNS3_RESET_WAIT_MS * USEC_PER_MSEC;
5991 wait_data->count = HNS3_RESET_WAIT_CNT;
5992 wait_data->result = HNS3_WAIT_REQUEST;
5993 rte_eal_alarm_set(wait_data->interval, hns3_wait_callback, wait_data);
5998 hns3_func_reset_cmd(struct hns3_hw *hw, int func_id)
6000 struct hns3_cmd_desc desc;
6001 struct hns3_reset_cmd *req = (struct hns3_reset_cmd *)desc.data;
6003 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_CFG_RST_TRIGGER, false);
6004 hns3_set_bit(req->mac_func_reset, HNS3_CFG_RESET_FUNC_B, 1);
6005 req->fun_reset_vfid = func_id;
6007 return hns3_cmd_send(hw, &desc, 1);
6011 hns3_imp_reset_cmd(struct hns3_hw *hw)
6013 struct hns3_cmd_desc desc;
6015 hns3_cmd_setup_basic_desc(&desc, 0xFFFE, false);
6016 desc.data[0] = 0xeedd;
6018 return hns3_cmd_send(hw, &desc, 1);
6022 hns3_msix_process(struct hns3_adapter *hns, enum hns3_reset_level reset_level)
6024 struct hns3_hw *hw = &hns->hw;
6028 hns3_clock_gettime(&tv);
6029 if (hns3_read_dev(hw, HNS3_GLOBAL_RESET_REG) ||
6030 hns3_read_dev(hw, HNS3_FUN_RST_ING)) {
6031 hns3_warn(hw, "Don't process msix during resetting time=%ld.%.6ld",
6032 tv.tv_sec, tv.tv_usec);
6036 switch (reset_level) {
6037 case HNS3_IMP_RESET:
6038 hns3_imp_reset_cmd(hw);
6039 hns3_warn(hw, "IMP Reset requested time=%ld.%.6ld",
6040 tv.tv_sec, tv.tv_usec);
6042 case HNS3_GLOBAL_RESET:
6043 val = hns3_read_dev(hw, HNS3_GLOBAL_RESET_REG);
6044 hns3_set_bit(val, HNS3_GLOBAL_RESET_BIT, 1);
6045 hns3_write_dev(hw, HNS3_GLOBAL_RESET_REG, val);
6046 hns3_warn(hw, "Global Reset requested time=%ld.%.6ld",
6047 tv.tv_sec, tv.tv_usec);
6049 case HNS3_FUNC_RESET:
6050 hns3_warn(hw, "PF Reset requested time=%ld.%.6ld",
6051 tv.tv_sec, tv.tv_usec);
6052 /* schedule again to check later */
6053 hns3_atomic_set_bit(HNS3_FUNC_RESET, &hw->reset.pending);
6054 hns3_schedule_reset(hns);
6057 hns3_warn(hw, "Unsupported reset level: %d", reset_level);
6060 hns3_atomic_clear_bit(reset_level, &hw->reset.request);
6063 static enum hns3_reset_level
6064 hns3_get_reset_level(struct hns3_adapter *hns, uint64_t *levels)
6066 struct hns3_hw *hw = &hns->hw;
6067 enum hns3_reset_level reset_level = HNS3_NONE_RESET;
6069 /* Return the highest priority reset level amongst all */
6070 if (hns3_atomic_test_bit(HNS3_IMP_RESET, levels))
6071 reset_level = HNS3_IMP_RESET;
6072 else if (hns3_atomic_test_bit(HNS3_GLOBAL_RESET, levels))
6073 reset_level = HNS3_GLOBAL_RESET;
6074 else if (hns3_atomic_test_bit(HNS3_FUNC_RESET, levels))
6075 reset_level = HNS3_FUNC_RESET;
6076 else if (hns3_atomic_test_bit(HNS3_FLR_RESET, levels))
6077 reset_level = HNS3_FLR_RESET;
6079 if (hw->reset.level != HNS3_NONE_RESET && reset_level < hw->reset.level)
6080 return HNS3_NONE_RESET;
6086 hns3_record_imp_error(struct hns3_adapter *hns)
6088 struct hns3_hw *hw = &hns->hw;
6091 reg_val = hns3_read_dev(hw, HNS3_VECTOR0_OTER_EN_REG);
6092 if (hns3_get_bit(reg_val, HNS3_VECTOR0_IMP_RD_POISON_B)) {
6093 hns3_warn(hw, "Detected IMP RD poison!");
6094 hns3_set_bit(reg_val, HNS3_VECTOR0_IMP_RD_POISON_B, 0);
6095 hns3_write_dev(hw, HNS3_VECTOR0_OTER_EN_REG, reg_val);
6098 if (hns3_get_bit(reg_val, HNS3_VECTOR0_IMP_CMDQ_ERR_B)) {
6099 hns3_warn(hw, "Detected IMP CMDQ error!");
6100 hns3_set_bit(reg_val, HNS3_VECTOR0_IMP_CMDQ_ERR_B, 0);
6101 hns3_write_dev(hw, HNS3_VECTOR0_OTER_EN_REG, reg_val);
6106 hns3_prepare_reset(struct hns3_adapter *hns)
6108 struct hns3_hw *hw = &hns->hw;
6112 switch (hw->reset.level) {
6113 case HNS3_FUNC_RESET:
6114 ret = hns3_func_reset_cmd(hw, HNS3_PF_FUNC_ID);
6119 * After performaning pf reset, it is not necessary to do the
6120 * mailbox handling or send any command to firmware, because
6121 * any mailbox handling or command to firmware is only valid
6122 * after hns3_cmd_init is called.
6124 __atomic_store_n(&hw->reset.disable_cmd, 1, __ATOMIC_RELAXED);
6125 hw->reset.stats.request_cnt++;
6127 case HNS3_IMP_RESET:
6128 hns3_record_imp_error(hns);
6129 reg_val = hns3_read_dev(hw, HNS3_VECTOR0_OTER_EN_REG);
6130 hns3_write_dev(hw, HNS3_VECTOR0_OTER_EN_REG, reg_val |
6131 BIT(HNS3_VECTOR0_IMP_RESET_INT_B));
6140 hns3_set_rst_done(struct hns3_hw *hw)
6142 struct hns3_pf_rst_done_cmd *req;
6143 struct hns3_cmd_desc desc;
6145 req = (struct hns3_pf_rst_done_cmd *)desc.data;
6146 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_PF_RST_DONE, false);
6147 req->pf_rst_done |= HNS3_PF_RESET_DONE_BIT;
6148 return hns3_cmd_send(hw, &desc, 1);
6152 hns3_stop_service(struct hns3_adapter *hns)
6154 struct hns3_hw *hw = &hns->hw;
6155 struct rte_eth_dev *eth_dev;
6157 eth_dev = &rte_eth_devices[hw->data->port_id];
6158 hw->mac.link_status = RTE_ETH_LINK_DOWN;
6159 if (hw->adapter_state == HNS3_NIC_STARTED) {
6160 rte_eal_alarm_cancel(hns3_service_handler, eth_dev);
6161 hns3_update_linkstatus_and_event(hw, false);
6164 hns3_set_rxtx_function(eth_dev);
6166 /* Disable datapath on secondary process. */
6167 hns3_mp_req_stop_rxtx(eth_dev);
6168 rte_delay_ms(hw->cfg_max_queues);
6170 rte_spinlock_lock(&hw->lock);
6171 if (hns->hw.adapter_state == HNS3_NIC_STARTED ||
6172 hw->adapter_state == HNS3_NIC_STOPPING) {
6173 hns3_enable_all_queues(hw, false);
6175 hw->reset.mbuf_deferred_free = true;
6177 hw->reset.mbuf_deferred_free = false;
6180 * It is cumbersome for hardware to pick-and-choose entries for deletion
6181 * from table space. Hence, for function reset software intervention is
6182 * required to delete the entries
6184 if (__atomic_load_n(&hw->reset.disable_cmd, __ATOMIC_RELAXED) == 0)
6185 hns3_configure_all_mc_mac_addr(hns, true);
6186 rte_spinlock_unlock(&hw->lock);
6192 hns3_start_service(struct hns3_adapter *hns)
6194 struct hns3_hw *hw = &hns->hw;
6195 struct rte_eth_dev *eth_dev;
6197 if (hw->reset.level == HNS3_IMP_RESET ||
6198 hw->reset.level == HNS3_GLOBAL_RESET)
6199 hns3_set_rst_done(hw);
6200 eth_dev = &rte_eth_devices[hw->data->port_id];
6201 hns3_set_rxtx_function(eth_dev);
6202 hns3_mp_req_start_rxtx(eth_dev);
6203 if (hw->adapter_state == HNS3_NIC_STARTED) {
6205 * This API parent function already hold the hns3_hw.lock, the
6206 * hns3_service_handler may report lse, in bonding application
6207 * it will call driver's ops which may acquire the hns3_hw.lock
6208 * again, thus lead to deadlock.
6209 * We defer calls hns3_service_handler to avoid the deadlock.
6211 rte_eal_alarm_set(HNS3_SERVICE_QUICK_INTERVAL,
6212 hns3_service_handler, eth_dev);
6214 /* Enable interrupt of all rx queues before enabling queues */
6215 hns3_dev_all_rx_queue_intr_enable(hw, true);
6217 * Enable state of each rxq and txq will be recovered after
6218 * reset, so we need to restore them before enable all tqps;
6220 hns3_restore_tqp_enable_state(hw);
6222 * When finished the initialization, enable queues to receive
6223 * and transmit packets.
6225 hns3_enable_all_queues(hw, true);
6232 hns3_restore_conf(struct hns3_adapter *hns)
6234 struct hns3_hw *hw = &hns->hw;
6237 ret = hns3_configure_all_mac_addr(hns, false);
6241 ret = hns3_configure_all_mc_mac_addr(hns, false);
6245 ret = hns3_dev_promisc_restore(hns);
6249 ret = hns3_restore_vlan_table(hns);
6253 ret = hns3_restore_vlan_conf(hns);
6257 ret = hns3_restore_all_fdir_filter(hns);
6261 ret = hns3_restore_ptp(hns);
6265 ret = hns3_restore_rx_interrupt(hw);
6269 ret = hns3_restore_gro_conf(hw);
6273 ret = hns3_restore_fec(hw);
6277 if (hns->hw.adapter_state == HNS3_NIC_STARTED) {
6278 ret = hns3_do_start(hns, false);
6281 hns3_info(hw, "hns3 dev restart successful!");
6282 } else if (hw->adapter_state == HNS3_NIC_STOPPING)
6283 hw->adapter_state = HNS3_NIC_CONFIGURED;
6287 hns3_configure_all_mc_mac_addr(hns, true);
6289 hns3_configure_all_mac_addr(hns, true);
6294 hns3_reset_service(void *param)
6296 struct hns3_adapter *hns = (struct hns3_adapter *)param;
6297 struct hns3_hw *hw = &hns->hw;
6298 enum hns3_reset_level reset_level;
6299 struct timeval tv_delta;
6300 struct timeval tv_start;
6306 * The interrupt is not triggered within the delay time.
6307 * The interrupt may have been lost. It is necessary to handle
6308 * the interrupt to recover from the error.
6310 if (__atomic_load_n(&hw->reset.schedule, __ATOMIC_RELAXED) ==
6311 SCHEDULE_DEFERRED) {
6312 __atomic_store_n(&hw->reset.schedule, SCHEDULE_REQUESTED,
6314 hns3_err(hw, "Handling interrupts in delayed tasks");
6315 hns3_interrupt_handler(&rte_eth_devices[hw->data->port_id]);
6316 reset_level = hns3_get_reset_level(hns, &hw->reset.pending);
6317 if (reset_level == HNS3_NONE_RESET) {
6318 hns3_err(hw, "No reset level is set, try IMP reset");
6319 hns3_atomic_set_bit(HNS3_IMP_RESET, &hw->reset.pending);
6322 __atomic_store_n(&hw->reset.schedule, SCHEDULE_NONE, __ATOMIC_RELAXED);
6325 * Check if there is any ongoing reset in the hardware. This status can
6326 * be checked from reset_pending. If there is then, we need to wait for
6327 * hardware to complete reset.
6328 * a. If we are able to figure out in reasonable time that hardware
6329 * has fully resetted then, we can proceed with driver, client
6331 * b. else, we can come back later to check this status so re-sched
6334 reset_level = hns3_get_reset_level(hns, &hw->reset.pending);
6335 if (reset_level != HNS3_NONE_RESET) {
6336 hns3_clock_gettime(&tv_start);
6337 ret = hns3_reset_process(hns, reset_level);
6338 hns3_clock_gettime(&tv);
6339 timersub(&tv, &tv_start, &tv_delta);
6340 msec = hns3_clock_calctime_ms(&tv_delta);
6341 if (msec > HNS3_RESET_PROCESS_MS)
6342 hns3_err(hw, "%d handle long time delta %" PRIu64
6343 " ms time=%ld.%.6ld",
6344 hw->reset.level, msec,
6345 tv.tv_sec, tv.tv_usec);
6350 /* Check if we got any *new* reset requests to be honored */
6351 reset_level = hns3_get_reset_level(hns, &hw->reset.request);
6352 if (reset_level != HNS3_NONE_RESET)
6353 hns3_msix_process(hns, reset_level);
6357 hns3_get_speed_capa_num(uint16_t device_id)
6361 switch (device_id) {
6362 case HNS3_DEV_ID_25GE:
6363 case HNS3_DEV_ID_25GE_RDMA:
6366 case HNS3_DEV_ID_100G_RDMA_MACSEC:
6367 case HNS3_DEV_ID_200G_RDMA:
6379 hns3_get_speed_fec_capa(struct rte_eth_fec_capa *speed_fec_capa,
6382 switch (device_id) {
6383 case HNS3_DEV_ID_25GE:
6385 case HNS3_DEV_ID_25GE_RDMA:
6386 speed_fec_capa[0].speed = speed_fec_capa_tbl[1].speed;
6387 speed_fec_capa[0].capa = speed_fec_capa_tbl[1].capa;
6389 /* In HNS3 device, the 25G NIC is compatible with 10G rate */
6390 speed_fec_capa[1].speed = speed_fec_capa_tbl[0].speed;
6391 speed_fec_capa[1].capa = speed_fec_capa_tbl[0].capa;
6393 case HNS3_DEV_ID_100G_RDMA_MACSEC:
6394 speed_fec_capa[0].speed = speed_fec_capa_tbl[4].speed;
6395 speed_fec_capa[0].capa = speed_fec_capa_tbl[4].capa;
6397 case HNS3_DEV_ID_200G_RDMA:
6398 speed_fec_capa[0].speed = speed_fec_capa_tbl[5].speed;
6399 speed_fec_capa[0].capa = speed_fec_capa_tbl[5].capa;
6409 hns3_fec_get_capability(struct rte_eth_dev *dev,
6410 struct rte_eth_fec_capa *speed_fec_capa,
6413 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
6414 struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
6415 uint16_t device_id = pci_dev->id.device_id;
6416 unsigned int capa_num;
6419 capa_num = hns3_get_speed_capa_num(device_id);
6420 if (capa_num == 0) {
6421 hns3_err(hw, "device(0x%x) is not supported by hns3 PMD",
6426 if (speed_fec_capa == NULL || num < capa_num)
6429 ret = hns3_get_speed_fec_capa(speed_fec_capa, device_id);
6437 get_current_fec_auto_state(struct hns3_hw *hw, uint8_t *state)
6439 struct hns3_config_fec_cmd *req;
6440 struct hns3_cmd_desc desc;
6444 * CMD(HNS3_OPC_CONFIG_FEC_MODE) read is not supported
6445 * in device of link speed
6448 if (hw->mac.link_speed < RTE_ETH_SPEED_NUM_10G) {
6453 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_CONFIG_FEC_MODE, true);
6454 req = (struct hns3_config_fec_cmd *)desc.data;
6455 ret = hns3_cmd_send(hw, &desc, 1);
6457 hns3_err(hw, "get current fec auto state failed, ret = %d",
6462 *state = req->fec_mode & (1U << HNS3_MAC_CFG_FEC_AUTO_EN_B);
6467 hns3_fec_get_internal(struct hns3_hw *hw, uint32_t *fec_capa)
6469 struct hns3_sfp_info_cmd *resp;
6470 uint32_t tmp_fec_capa;
6472 struct hns3_cmd_desc desc;
6476 * If link is down and AUTO is enabled, AUTO is returned, otherwise,
6477 * configured FEC mode is returned.
6478 * If link is up, current FEC mode is returned.
6480 if (hw->mac.link_status == RTE_ETH_LINK_DOWN) {
6481 ret = get_current_fec_auto_state(hw, &auto_state);
6485 if (auto_state == 0x1) {
6486 *fec_capa = RTE_ETH_FEC_MODE_CAPA_MASK(AUTO);
6491 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_GET_SFP_INFO, true);
6492 resp = (struct hns3_sfp_info_cmd *)desc.data;
6493 resp->query_type = HNS3_ACTIVE_QUERY;
6495 ret = hns3_cmd_send(hw, &desc, 1);
6496 if (ret == -EOPNOTSUPP) {
6497 hns3_err(hw, "IMP do not support get FEC, ret = %d", ret);
6500 hns3_err(hw, "get FEC failed, ret = %d", ret);
6505 * FEC mode order defined in hns3 hardware is inconsistend with
6506 * that defined in the ethdev library. So the sequence needs
6509 switch (resp->active_fec) {
6510 case HNS3_HW_FEC_MODE_NOFEC:
6511 tmp_fec_capa = RTE_ETH_FEC_MODE_CAPA_MASK(NOFEC);
6513 case HNS3_HW_FEC_MODE_BASER:
6514 tmp_fec_capa = RTE_ETH_FEC_MODE_CAPA_MASK(BASER);
6516 case HNS3_HW_FEC_MODE_RS:
6517 tmp_fec_capa = RTE_ETH_FEC_MODE_CAPA_MASK(RS);
6520 tmp_fec_capa = RTE_ETH_FEC_MODE_CAPA_MASK(NOFEC);
6524 *fec_capa = tmp_fec_capa;
6529 hns3_fec_get(struct rte_eth_dev *dev, uint32_t *fec_capa)
6531 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
6533 return hns3_fec_get_internal(hw, fec_capa);
6537 hns3_set_fec_hw(struct hns3_hw *hw, uint32_t mode)
6539 struct hns3_config_fec_cmd *req;
6540 struct hns3_cmd_desc desc;
6543 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_CONFIG_FEC_MODE, false);
6545 req = (struct hns3_config_fec_cmd *)desc.data;
6547 case RTE_ETH_FEC_MODE_CAPA_MASK(NOFEC):
6548 hns3_set_field(req->fec_mode, HNS3_MAC_CFG_FEC_MODE_M,
6549 HNS3_MAC_CFG_FEC_MODE_S, HNS3_MAC_FEC_OFF);
6551 case RTE_ETH_FEC_MODE_CAPA_MASK(BASER):
6552 hns3_set_field(req->fec_mode, HNS3_MAC_CFG_FEC_MODE_M,
6553 HNS3_MAC_CFG_FEC_MODE_S, HNS3_MAC_FEC_BASER);
6555 case RTE_ETH_FEC_MODE_CAPA_MASK(RS):
6556 hns3_set_field(req->fec_mode, HNS3_MAC_CFG_FEC_MODE_M,
6557 HNS3_MAC_CFG_FEC_MODE_S, HNS3_MAC_FEC_RS);
6559 case RTE_ETH_FEC_MODE_CAPA_MASK(AUTO):
6560 hns3_set_bit(req->fec_mode, HNS3_MAC_CFG_FEC_AUTO_EN_B, 1);
6565 ret = hns3_cmd_send(hw, &desc, 1);
6567 hns3_err(hw, "set fec mode failed, ret = %d", ret);
6573 get_current_speed_fec_cap(struct hns3_hw *hw, struct rte_eth_fec_capa *fec_capa)
6575 struct hns3_mac *mac = &hw->mac;
6578 switch (mac->link_speed) {
6579 case RTE_ETH_SPEED_NUM_10G:
6580 cur_capa = fec_capa[1].capa;
6582 case RTE_ETH_SPEED_NUM_25G:
6583 case RTE_ETH_SPEED_NUM_100G:
6584 case RTE_ETH_SPEED_NUM_200G:
6585 cur_capa = fec_capa[0].capa;
6596 is_fec_mode_one_bit_set(uint32_t mode)
6601 for (i = 0; i < sizeof(mode); i++)
6602 if (mode >> i & 0x1)
6605 return cnt == 1 ? true : false;
6609 hns3_fec_set(struct rte_eth_dev *dev, uint32_t mode)
6611 #define FEC_CAPA_NUM 2
6612 struct hns3_adapter *hns = dev->data->dev_private;
6613 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(hns);
6614 struct hns3_pf *pf = &hns->pf;
6616 struct rte_eth_fec_capa fec_capa[FEC_CAPA_NUM];
6618 uint32_t num = FEC_CAPA_NUM;
6621 ret = hns3_fec_get_capability(dev, fec_capa, num);
6625 /* HNS3 PMD driver only support one bit set mode, e.g. 0x1, 0x4 */
6626 if (!is_fec_mode_one_bit_set(mode)) {
6627 hns3_err(hw, "FEC mode(0x%x) not supported in HNS3 PMD, "
6628 "FEC mode should be only one bit set", mode);
6633 * Check whether the configured mode is within the FEC capability.
6634 * If not, the configured mode will not be supported.
6636 cur_capa = get_current_speed_fec_cap(hw, fec_capa);
6637 if (!(cur_capa & mode)) {
6638 hns3_err(hw, "unsupported FEC mode = 0x%x", mode);
6642 rte_spinlock_lock(&hw->lock);
6643 ret = hns3_set_fec_hw(hw, mode);
6645 rte_spinlock_unlock(&hw->lock);
6649 pf->fec_mode = mode;
6650 rte_spinlock_unlock(&hw->lock);
6656 hns3_restore_fec(struct hns3_hw *hw)
6658 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
6659 struct hns3_pf *pf = &hns->pf;
6660 uint32_t mode = pf->fec_mode;
6663 ret = hns3_set_fec_hw(hw, mode);
6665 hns3_err(hw, "restore fec mode(0x%x) failed, ret = %d",
6672 hns3_query_dev_fec_info(struct hns3_hw *hw)
6674 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
6675 struct hns3_pf *pf = HNS3_DEV_PRIVATE_TO_PF(hns);
6678 ret = hns3_fec_get_internal(hw, &pf->fec_mode);
6680 hns3_err(hw, "query device FEC info failed, ret = %d", ret);
6686 hns3_optical_module_existed(struct hns3_hw *hw)
6688 struct hns3_cmd_desc desc;
6692 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_GET_SFP_EXIST, true);
6693 ret = hns3_cmd_send(hw, &desc, 1);
6696 "fail to get optical module exist state, ret = %d.\n",
6700 existed = !!desc.data[0];
6706 hns3_get_module_eeprom_data(struct hns3_hw *hw, uint32_t offset,
6707 uint32_t len, uint8_t *data)
6709 #define HNS3_SFP_INFO_CMD_NUM 6
6710 #define HNS3_SFP_INFO_MAX_LEN \
6711 (HNS3_SFP_INFO_BD0_LEN + \
6712 (HNS3_SFP_INFO_CMD_NUM - 1) * HNS3_SFP_INFO_BDX_LEN)
6713 struct hns3_cmd_desc desc[HNS3_SFP_INFO_CMD_NUM];
6714 struct hns3_sfp_info_bd0_cmd *sfp_info_bd0;
6720 for (i = 0; i < HNS3_SFP_INFO_CMD_NUM; i++) {
6721 hns3_cmd_setup_basic_desc(&desc[i], HNS3_OPC_GET_SFP_EEPROM,
6723 if (i < HNS3_SFP_INFO_CMD_NUM - 1)
6724 desc[i].flag |= rte_cpu_to_le_16(HNS3_CMD_FLAG_NEXT);
6727 sfp_info_bd0 = (struct hns3_sfp_info_bd0_cmd *)desc[0].data;
6728 sfp_info_bd0->offset = rte_cpu_to_le_16((uint16_t)offset);
6729 read_len = RTE_MIN(len, HNS3_SFP_INFO_MAX_LEN);
6730 sfp_info_bd0->read_len = rte_cpu_to_le_16((uint16_t)read_len);
6732 ret = hns3_cmd_send(hw, desc, HNS3_SFP_INFO_CMD_NUM);
6734 hns3_err(hw, "fail to get module EEPROM info, ret = %d.\n",
6739 /* The data format in BD0 is different with the others. */
6740 copy_len = RTE_MIN(len, HNS3_SFP_INFO_BD0_LEN);
6741 memcpy(data, sfp_info_bd0->data, copy_len);
6742 read_len = copy_len;
6744 for (i = 1; i < HNS3_SFP_INFO_CMD_NUM; i++) {
6745 if (read_len >= len)
6748 copy_len = RTE_MIN(len - read_len, HNS3_SFP_INFO_BDX_LEN);
6749 memcpy(data + read_len, desc[i].data, copy_len);
6750 read_len += copy_len;
6753 return (int)read_len;
6757 hns3_get_module_eeprom(struct rte_eth_dev *dev,
6758 struct rte_dev_eeprom_info *info)
6760 struct hns3_adapter *hns = dev->data->dev_private;
6761 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(hns);
6762 uint32_t offset = info->offset;
6763 uint32_t len = info->length;
6764 uint8_t *data = info->data;
6765 uint32_t read_len = 0;
6767 if (hw->mac.media_type != HNS3_MEDIA_TYPE_FIBER)
6770 if (!hns3_optical_module_existed(hw)) {
6771 hns3_err(hw, "fail to read module EEPROM: no module is connected.\n");
6775 while (read_len < len) {
6777 ret = hns3_get_module_eeprom_data(hw, offset + read_len,
6789 hns3_get_module_info(struct rte_eth_dev *dev,
6790 struct rte_eth_dev_module_info *modinfo)
6792 #define HNS3_SFF8024_ID_SFP 0x03
6793 #define HNS3_SFF8024_ID_QSFP_8438 0x0c
6794 #define HNS3_SFF8024_ID_QSFP_8436_8636 0x0d
6795 #define HNS3_SFF8024_ID_QSFP28_8636 0x11
6796 #define HNS3_SFF_8636_V1_3 0x03
6797 struct hns3_adapter *hns = dev->data->dev_private;
6798 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(hns);
6799 struct rte_dev_eeprom_info info;
6800 struct hns3_sfp_type sfp_type;
6803 memset(&sfp_type, 0, sizeof(sfp_type));
6804 memset(&info, 0, sizeof(info));
6805 info.data = (uint8_t *)&sfp_type;
6806 info.length = sizeof(sfp_type);
6807 ret = hns3_get_module_eeprom(dev, &info);
6811 switch (sfp_type.type) {
6812 case HNS3_SFF8024_ID_SFP:
6813 modinfo->type = RTE_ETH_MODULE_SFF_8472;
6814 modinfo->eeprom_len = RTE_ETH_MODULE_SFF_8472_LEN;
6816 case HNS3_SFF8024_ID_QSFP_8438:
6817 modinfo->type = RTE_ETH_MODULE_SFF_8436;
6818 modinfo->eeprom_len = RTE_ETH_MODULE_SFF_8436_MAX_LEN;
6820 case HNS3_SFF8024_ID_QSFP_8436_8636:
6821 if (sfp_type.ext_type < HNS3_SFF_8636_V1_3) {
6822 modinfo->type = RTE_ETH_MODULE_SFF_8436;
6823 modinfo->eeprom_len = RTE_ETH_MODULE_SFF_8436_MAX_LEN;
6825 modinfo->type = RTE_ETH_MODULE_SFF_8636;
6826 modinfo->eeprom_len = RTE_ETH_MODULE_SFF_8636_MAX_LEN;
6829 case HNS3_SFF8024_ID_QSFP28_8636:
6830 modinfo->type = RTE_ETH_MODULE_SFF_8636;
6831 modinfo->eeprom_len = RTE_ETH_MODULE_SFF_8636_MAX_LEN;
6834 hns3_err(hw, "unknown module, type = %u, extra_type = %u.\n",
6835 sfp_type.type, sfp_type.ext_type);
6842 static const struct eth_dev_ops hns3_eth_dev_ops = {
6843 .dev_configure = hns3_dev_configure,
6844 .dev_start = hns3_dev_start,
6845 .dev_stop = hns3_dev_stop,
6846 .dev_close = hns3_dev_close,
6847 .promiscuous_enable = hns3_dev_promiscuous_enable,
6848 .promiscuous_disable = hns3_dev_promiscuous_disable,
6849 .allmulticast_enable = hns3_dev_allmulticast_enable,
6850 .allmulticast_disable = hns3_dev_allmulticast_disable,
6851 .mtu_set = hns3_dev_mtu_set,
6852 .stats_get = hns3_stats_get,
6853 .stats_reset = hns3_stats_reset,
6854 .xstats_get = hns3_dev_xstats_get,
6855 .xstats_get_names = hns3_dev_xstats_get_names,
6856 .xstats_reset = hns3_dev_xstats_reset,
6857 .xstats_get_by_id = hns3_dev_xstats_get_by_id,
6858 .xstats_get_names_by_id = hns3_dev_xstats_get_names_by_id,
6859 .dev_infos_get = hns3_dev_infos_get,
6860 .fw_version_get = hns3_fw_version_get,
6861 .rx_queue_setup = hns3_rx_queue_setup,
6862 .tx_queue_setup = hns3_tx_queue_setup,
6863 .rx_queue_release = hns3_dev_rx_queue_release,
6864 .tx_queue_release = hns3_dev_tx_queue_release,
6865 .rx_queue_start = hns3_dev_rx_queue_start,
6866 .rx_queue_stop = hns3_dev_rx_queue_stop,
6867 .tx_queue_start = hns3_dev_tx_queue_start,
6868 .tx_queue_stop = hns3_dev_tx_queue_stop,
6869 .rx_queue_intr_enable = hns3_dev_rx_queue_intr_enable,
6870 .rx_queue_intr_disable = hns3_dev_rx_queue_intr_disable,
6871 .rxq_info_get = hns3_rxq_info_get,
6872 .txq_info_get = hns3_txq_info_get,
6873 .rx_burst_mode_get = hns3_rx_burst_mode_get,
6874 .tx_burst_mode_get = hns3_tx_burst_mode_get,
6875 .flow_ctrl_get = hns3_flow_ctrl_get,
6876 .flow_ctrl_set = hns3_flow_ctrl_set,
6877 .priority_flow_ctrl_set = hns3_priority_flow_ctrl_set,
6878 .mac_addr_add = hns3_add_mac_addr,
6879 .mac_addr_remove = hns3_remove_mac_addr,
6880 .mac_addr_set = hns3_set_default_mac_addr,
6881 .set_mc_addr_list = hns3_set_mc_mac_addr_list,
6882 .link_update = hns3_dev_link_update,
6883 .dev_set_link_up = hns3_dev_set_link_up,
6884 .dev_set_link_down = hns3_dev_set_link_down,
6885 .rss_hash_update = hns3_dev_rss_hash_update,
6886 .rss_hash_conf_get = hns3_dev_rss_hash_conf_get,
6887 .reta_update = hns3_dev_rss_reta_update,
6888 .reta_query = hns3_dev_rss_reta_query,
6889 .flow_ops_get = hns3_dev_flow_ops_get,
6890 .vlan_filter_set = hns3_vlan_filter_set,
6891 .vlan_tpid_set = hns3_vlan_tpid_set,
6892 .vlan_offload_set = hns3_vlan_offload_set,
6893 .vlan_pvid_set = hns3_vlan_pvid_set,
6894 .get_reg = hns3_get_regs,
6895 .get_module_info = hns3_get_module_info,
6896 .get_module_eeprom = hns3_get_module_eeprom,
6897 .get_dcb_info = hns3_get_dcb_info,
6898 .dev_supported_ptypes_get = hns3_dev_supported_ptypes_get,
6899 .fec_get_capability = hns3_fec_get_capability,
6900 .fec_get = hns3_fec_get,
6901 .fec_set = hns3_fec_set,
6902 .tm_ops_get = hns3_tm_ops_get,
6903 .tx_done_cleanup = hns3_tx_done_cleanup,
6904 .timesync_enable = hns3_timesync_enable,
6905 .timesync_disable = hns3_timesync_disable,
6906 .timesync_read_rx_timestamp = hns3_timesync_read_rx_timestamp,
6907 .timesync_read_tx_timestamp = hns3_timesync_read_tx_timestamp,
6908 .timesync_adjust_time = hns3_timesync_adjust_time,
6909 .timesync_read_time = hns3_timesync_read_time,
6910 .timesync_write_time = hns3_timesync_write_time,
6913 static const struct hns3_reset_ops hns3_reset_ops = {
6914 .reset_service = hns3_reset_service,
6915 .stop_service = hns3_stop_service,
6916 .prepare_reset = hns3_prepare_reset,
6917 .wait_hardware_ready = hns3_wait_hardware_ready,
6918 .reinit_dev = hns3_reinit_dev,
6919 .restore_conf = hns3_restore_conf,
6920 .start_service = hns3_start_service,
6924 hns3_init_hw_ops(struct hns3_hw *hw)
6926 hw->ops.add_mc_mac_addr = hns3_add_mc_mac_addr;
6927 hw->ops.del_mc_mac_addr = hns3_remove_mc_mac_addr;
6928 hw->ops.add_uc_mac_addr = hns3_add_uc_mac_addr;
6929 hw->ops.del_uc_mac_addr = hns3_remove_uc_mac_addr;
6933 hns3_dev_init(struct rte_eth_dev *eth_dev)
6935 struct hns3_adapter *hns = eth_dev->data->dev_private;
6936 char mac_str[RTE_ETHER_ADDR_FMT_SIZE];
6937 struct rte_ether_addr *eth_addr;
6938 struct hns3_hw *hw = &hns->hw;
6941 PMD_INIT_FUNC_TRACE();
6943 hns3_flow_init(eth_dev);
6945 hns3_set_rxtx_function(eth_dev);
6946 eth_dev->dev_ops = &hns3_eth_dev_ops;
6947 eth_dev->rx_queue_count = hns3_rx_queue_count;
6948 ret = hns3_mp_init(eth_dev);
6952 if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
6953 hns3_tx_push_init(eth_dev);
6957 hw->adapter_state = HNS3_NIC_UNINITIALIZED;
6959 hw->data = eth_dev->data;
6960 hns3_parse_devargs(eth_dev);
6963 * Set default max packet size according to the mtu
6964 * default vale in DPDK frame.
6966 hns->pf.mps = hw->data->mtu + HNS3_ETH_OVERHEAD;
6968 ret = hns3_reset_init(hw);
6970 goto err_init_reset;
6971 hw->reset.ops = &hns3_reset_ops;
6973 hns3_init_hw_ops(hw);
6974 ret = hns3_init_pf(eth_dev);
6976 PMD_INIT_LOG(ERR, "Failed to init pf: %d", ret);
6980 /* Allocate memory for storing MAC addresses */
6981 eth_dev->data->mac_addrs = rte_zmalloc("hns3-mac",
6982 sizeof(struct rte_ether_addr) *
6983 HNS3_UC_MACADDR_NUM, 0);
6984 if (eth_dev->data->mac_addrs == NULL) {
6985 PMD_INIT_LOG(ERR, "Failed to allocate %zx bytes needed "
6986 "to store MAC addresses",
6987 sizeof(struct rte_ether_addr) *
6988 HNS3_UC_MACADDR_NUM);
6990 goto err_rte_zmalloc;
6993 eth_addr = (struct rte_ether_addr *)hw->mac.mac_addr;
6994 if (!rte_is_valid_assigned_ether_addr(eth_addr)) {
6995 rte_eth_random_addr(hw->mac.mac_addr);
6996 hns3_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
6997 (struct rte_ether_addr *)hw->mac.mac_addr);
6998 hns3_warn(hw, "default mac_addr from firmware is an invalid "
6999 "unicast address, using random MAC address %s",
7002 rte_ether_addr_copy((struct rte_ether_addr *)hw->mac.mac_addr,
7003 ð_dev->data->mac_addrs[0]);
7005 hw->adapter_state = HNS3_NIC_INITIALIZED;
7007 if (__atomic_load_n(&hw->reset.schedule, __ATOMIC_RELAXED) ==
7009 hns3_err(hw, "Reschedule reset service after dev_init");
7010 hns3_schedule_reset(hns);
7012 /* IMP will wait ready flag before reset */
7013 hns3_notify_reset_ready(hw, false);
7016 hns3_info(hw, "hns3 dev initialization successful!");
7020 hns3_uninit_pf(eth_dev);
7023 rte_free(hw->reset.wait_data);
7026 hns3_mp_uninit(eth_dev);
7029 eth_dev->dev_ops = NULL;
7030 eth_dev->rx_pkt_burst = NULL;
7031 eth_dev->rx_descriptor_status = NULL;
7032 eth_dev->tx_pkt_burst = NULL;
7033 eth_dev->tx_pkt_prepare = NULL;
7034 eth_dev->tx_descriptor_status = NULL;
7039 hns3_dev_uninit(struct rte_eth_dev *eth_dev)
7041 struct hns3_adapter *hns = eth_dev->data->dev_private;
7042 struct hns3_hw *hw = &hns->hw;
7044 PMD_INIT_FUNC_TRACE();
7046 if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
7047 hns3_mp_uninit(eth_dev);
7051 if (hw->adapter_state < HNS3_NIC_CLOSING)
7052 hns3_dev_close(eth_dev);
7054 hw->adapter_state = HNS3_NIC_REMOVED;
7059 eth_hns3_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
7060 struct rte_pci_device *pci_dev)
7062 return rte_eth_dev_pci_generic_probe(pci_dev,
7063 sizeof(struct hns3_adapter),
7068 eth_hns3_pci_remove(struct rte_pci_device *pci_dev)
7070 return rte_eth_dev_pci_generic_remove(pci_dev, hns3_dev_uninit);
7073 static const struct rte_pci_id pci_id_hns3_map[] = {
7074 { RTE_PCI_DEVICE(PCI_VENDOR_ID_HUAWEI, HNS3_DEV_ID_GE) },
7075 { RTE_PCI_DEVICE(PCI_VENDOR_ID_HUAWEI, HNS3_DEV_ID_25GE) },
7076 { RTE_PCI_DEVICE(PCI_VENDOR_ID_HUAWEI, HNS3_DEV_ID_25GE_RDMA) },
7077 { RTE_PCI_DEVICE(PCI_VENDOR_ID_HUAWEI, HNS3_DEV_ID_50GE_RDMA) },
7078 { RTE_PCI_DEVICE(PCI_VENDOR_ID_HUAWEI, HNS3_DEV_ID_100G_RDMA_MACSEC) },
7079 { RTE_PCI_DEVICE(PCI_VENDOR_ID_HUAWEI, HNS3_DEV_ID_200G_RDMA) },
7080 { .vendor_id = 0, }, /* sentinel */
7083 static struct rte_pci_driver rte_hns3_pmd = {
7084 .id_table = pci_id_hns3_map,
7085 .drv_flags = RTE_PCI_DRV_NEED_MAPPING | RTE_PCI_DRV_INTR_LSC,
7086 .probe = eth_hns3_pci_probe,
7087 .remove = eth_hns3_pci_remove,
7090 RTE_PMD_REGISTER_PCI(net_hns3, rte_hns3_pmd);
7091 RTE_PMD_REGISTER_PCI_TABLE(net_hns3, pci_id_hns3_map);
7092 RTE_PMD_REGISTER_KMOD_DEP(net_hns3, "* igb_uio | vfio-pci");
7093 RTE_PMD_REGISTER_PARAM_STRING(net_hns3,
7094 HNS3_DEVARG_RX_FUNC_HINT "=vec|sve|simple|common "
7095 HNS3_DEVARG_TX_FUNC_HINT "=vec|sve|simple|common "
7096 HNS3_DEVARG_DEV_CAPS_MASK "=<1-65535> "
7097 HNS3_DEVARG_MBX_TIME_LIMIT_MS "=<uint16> ");
7098 RTE_LOG_REGISTER_SUFFIX(hns3_logtype_init, init, NOTICE);
7099 RTE_LOG_REGISTER_SUFFIX(hns3_logtype_driver, driver, NOTICE);