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>
9 #include <rte_kvargs.h>
11 #include "hns3_ethdev.h"
12 #include "hns3_logs.h"
13 #include "hns3_rxtx.h"
14 #include "hns3_intr.h"
15 #include "hns3_regs.h"
19 #define HNS3_DEFAULT_PORT_CONF_BURST_SIZE 32
20 #define HNS3_DEFAULT_PORT_CONF_QUEUES_NUM 1
22 #define HNS3_SERVICE_INTERVAL 1000000 /* us */
23 #define HNS3_SERVICE_QUICK_INTERVAL 10
24 #define HNS3_INVALID_PVID 0xFFFF
26 #define HNS3_FILTER_TYPE_VF 0
27 #define HNS3_FILTER_TYPE_PORT 1
28 #define HNS3_FILTER_FE_EGRESS_V1_B BIT(0)
29 #define HNS3_FILTER_FE_NIC_INGRESS_B BIT(0)
30 #define HNS3_FILTER_FE_NIC_EGRESS_B BIT(1)
31 #define HNS3_FILTER_FE_ROCE_INGRESS_B BIT(2)
32 #define HNS3_FILTER_FE_ROCE_EGRESS_B BIT(3)
33 #define HNS3_FILTER_FE_EGRESS (HNS3_FILTER_FE_NIC_EGRESS_B \
34 | HNS3_FILTER_FE_ROCE_EGRESS_B)
35 #define HNS3_FILTER_FE_INGRESS (HNS3_FILTER_FE_NIC_INGRESS_B \
36 | HNS3_FILTER_FE_ROCE_INGRESS_B)
38 /* Reset related Registers */
39 #define HNS3_GLOBAL_RESET_BIT 0
40 #define HNS3_CORE_RESET_BIT 1
41 #define HNS3_IMP_RESET_BIT 2
42 #define HNS3_FUN_RST_ING_B 0
44 #define HNS3_VECTOR0_IMP_RESET_INT_B 1
45 #define HNS3_VECTOR0_IMP_CMDQ_ERR_B 4U
46 #define HNS3_VECTOR0_IMP_RD_POISON_B 5U
47 #define HNS3_VECTOR0_ALL_MSIX_ERR_B 6U
49 #define HNS3_RESET_WAIT_MS 100
50 #define HNS3_RESET_WAIT_CNT 200
52 /* FEC mode order defined in HNS3 hardware */
53 #define HNS3_HW_FEC_MODE_NOFEC 0
54 #define HNS3_HW_FEC_MODE_BASER 1
55 #define HNS3_HW_FEC_MODE_RS 2
58 HNS3_VECTOR0_EVENT_RST,
59 HNS3_VECTOR0_EVENT_MBX,
60 HNS3_VECTOR0_EVENT_ERR,
61 HNS3_VECTOR0_EVENT_PTP,
62 HNS3_VECTOR0_EVENT_OTHER,
65 static const struct rte_eth_fec_capa speed_fec_capa_tbl[] = {
66 { ETH_SPEED_NUM_10G, RTE_ETH_FEC_MODE_CAPA_MASK(NOFEC) |
67 RTE_ETH_FEC_MODE_CAPA_MASK(AUTO) |
68 RTE_ETH_FEC_MODE_CAPA_MASK(BASER) },
70 { ETH_SPEED_NUM_25G, RTE_ETH_FEC_MODE_CAPA_MASK(NOFEC) |
71 RTE_ETH_FEC_MODE_CAPA_MASK(AUTO) |
72 RTE_ETH_FEC_MODE_CAPA_MASK(BASER) |
73 RTE_ETH_FEC_MODE_CAPA_MASK(RS) },
75 { ETH_SPEED_NUM_40G, RTE_ETH_FEC_MODE_CAPA_MASK(NOFEC) |
76 RTE_ETH_FEC_MODE_CAPA_MASK(AUTO) |
77 RTE_ETH_FEC_MODE_CAPA_MASK(BASER) },
79 { ETH_SPEED_NUM_50G, RTE_ETH_FEC_MODE_CAPA_MASK(NOFEC) |
80 RTE_ETH_FEC_MODE_CAPA_MASK(AUTO) |
81 RTE_ETH_FEC_MODE_CAPA_MASK(BASER) |
82 RTE_ETH_FEC_MODE_CAPA_MASK(RS) },
84 { ETH_SPEED_NUM_100G, RTE_ETH_FEC_MODE_CAPA_MASK(NOFEC) |
85 RTE_ETH_FEC_MODE_CAPA_MASK(AUTO) |
86 RTE_ETH_FEC_MODE_CAPA_MASK(RS) },
88 { ETH_SPEED_NUM_200G, RTE_ETH_FEC_MODE_CAPA_MASK(NOFEC) |
89 RTE_ETH_FEC_MODE_CAPA_MASK(AUTO) |
90 RTE_ETH_FEC_MODE_CAPA_MASK(RS) }
93 static enum hns3_reset_level hns3_get_reset_level(struct hns3_adapter *hns,
95 static int hns3_dev_mtu_set(struct rte_eth_dev *dev, uint16_t mtu);
96 static int hns3_vlan_pvid_configure(struct hns3_adapter *hns, uint16_t pvid,
98 static int hns3_update_link_info(struct rte_eth_dev *eth_dev);
99 static bool hns3_update_link_status(struct hns3_hw *hw);
101 static int hns3_add_mc_addr(struct hns3_hw *hw,
102 struct rte_ether_addr *mac_addr);
103 static int hns3_remove_mc_addr(struct hns3_hw *hw,
104 struct rte_ether_addr *mac_addr);
105 static int hns3_restore_fec(struct hns3_hw *hw);
106 static int hns3_query_dev_fec_info(struct hns3_hw *hw);
107 static int hns3_do_stop(struct hns3_adapter *hns);
109 void hns3_ether_format_addr(char *buf, uint16_t size,
110 const struct rte_ether_addr *ether_addr)
112 snprintf(buf, size, "%02X:**:**:**:%02X:%02X",
113 ether_addr->addr_bytes[0],
114 ether_addr->addr_bytes[4],
115 ether_addr->addr_bytes[5]);
119 hns3_pf_disable_irq0(struct hns3_hw *hw)
121 hns3_write_dev(hw, HNS3_MISC_VECTOR_REG_BASE, 0);
125 hns3_pf_enable_irq0(struct hns3_hw *hw)
127 hns3_write_dev(hw, HNS3_MISC_VECTOR_REG_BASE, 1);
130 static enum hns3_evt_cause
131 hns3_proc_imp_reset_event(struct hns3_adapter *hns, bool is_delay,
134 struct hns3_hw *hw = &hns->hw;
136 __atomic_store_n(&hw->reset.disable_cmd, 1, __ATOMIC_RELAXED);
137 hns3_atomic_set_bit(HNS3_IMP_RESET, &hw->reset.pending);
138 *vec_val = BIT(HNS3_VECTOR0_IMPRESET_INT_B);
140 hw->reset.stats.imp_cnt++;
141 hns3_warn(hw, "IMP reset detected, clear reset status");
143 hns3_schedule_delayed_reset(hns);
144 hns3_warn(hw, "IMP reset detected, don't clear reset status");
147 return HNS3_VECTOR0_EVENT_RST;
150 static enum hns3_evt_cause
151 hns3_proc_global_reset_event(struct hns3_adapter *hns, bool is_delay,
154 struct hns3_hw *hw = &hns->hw;
156 __atomic_store_n(&hw->reset.disable_cmd, 1, __ATOMIC_RELAXED);
157 hns3_atomic_set_bit(HNS3_GLOBAL_RESET, &hw->reset.pending);
158 *vec_val = BIT(HNS3_VECTOR0_GLOBALRESET_INT_B);
160 hw->reset.stats.global_cnt++;
161 hns3_warn(hw, "Global reset detected, clear reset status");
163 hns3_schedule_delayed_reset(hns);
165 "Global reset detected, don't clear reset status");
168 return HNS3_VECTOR0_EVENT_RST;
171 static enum hns3_evt_cause
172 hns3_check_event_cause(struct hns3_adapter *hns, uint32_t *clearval)
174 struct hns3_hw *hw = &hns->hw;
175 uint32_t vector0_int_stats;
176 uint32_t cmdq_src_val;
177 uint32_t hw_err_src_reg;
179 enum hns3_evt_cause ret;
182 /* fetch the events from their corresponding regs */
183 vector0_int_stats = hns3_read_dev(hw, HNS3_VECTOR0_OTHER_INT_STS_REG);
184 cmdq_src_val = hns3_read_dev(hw, HNS3_VECTOR0_CMDQ_SRC_REG);
185 hw_err_src_reg = hns3_read_dev(hw, HNS3_RAS_PF_OTHER_INT_STS_REG);
187 is_delay = clearval == NULL ? true : false;
189 * Assumption: If by any chance reset and mailbox events are reported
190 * together then we will only process reset event and defer the
191 * processing of the mailbox events. Since, we would have not cleared
192 * RX CMDQ event this time we would receive again another interrupt
193 * from H/W just for the mailbox.
195 if (BIT(HNS3_VECTOR0_IMPRESET_INT_B) & vector0_int_stats) { /* IMP */
196 ret = hns3_proc_imp_reset_event(hns, is_delay, &val);
201 if (BIT(HNS3_VECTOR0_GLOBALRESET_INT_B) & vector0_int_stats) {
202 ret = hns3_proc_global_reset_event(hns, is_delay, &val);
206 /* Check for vector0 1588 event source */
207 if (BIT(HNS3_VECTOR0_1588_INT_B) & vector0_int_stats) {
208 val = BIT(HNS3_VECTOR0_1588_INT_B);
209 ret = HNS3_VECTOR0_EVENT_PTP;
213 /* check for vector0 msix event source */
214 if (vector0_int_stats & HNS3_VECTOR0_REG_MSIX_MASK ||
215 hw_err_src_reg & HNS3_RAS_REG_NFE_MASK) {
216 val = vector0_int_stats | hw_err_src_reg;
217 ret = HNS3_VECTOR0_EVENT_ERR;
221 /* check for vector0 mailbox(=CMDQ RX) event source */
222 if (BIT(HNS3_VECTOR0_RX_CMDQ_INT_B) & cmdq_src_val) {
223 cmdq_src_val &= ~BIT(HNS3_VECTOR0_RX_CMDQ_INT_B);
225 ret = HNS3_VECTOR0_EVENT_MBX;
229 val = vector0_int_stats;
230 ret = HNS3_VECTOR0_EVENT_OTHER;
239 hns3_is_1588_event_type(uint32_t event_type)
241 return (event_type == HNS3_VECTOR0_EVENT_PTP);
245 hns3_clear_event_cause(struct hns3_hw *hw, uint32_t event_type, uint32_t regclr)
247 if (event_type == HNS3_VECTOR0_EVENT_RST ||
248 hns3_is_1588_event_type(event_type))
249 hns3_write_dev(hw, HNS3_MISC_RESET_STS_REG, regclr);
250 else if (event_type == HNS3_VECTOR0_EVENT_MBX)
251 hns3_write_dev(hw, HNS3_VECTOR0_CMDQ_SRC_REG, regclr);
255 hns3_clear_all_event_cause(struct hns3_hw *hw)
257 uint32_t vector0_int_stats;
258 vector0_int_stats = hns3_read_dev(hw, HNS3_VECTOR0_OTHER_INT_STS_REG);
260 if (BIT(HNS3_VECTOR0_IMPRESET_INT_B) & vector0_int_stats)
261 hns3_warn(hw, "Probe during IMP reset interrupt");
263 if (BIT(HNS3_VECTOR0_GLOBALRESET_INT_B) & vector0_int_stats)
264 hns3_warn(hw, "Probe during Global reset interrupt");
266 hns3_clear_event_cause(hw, HNS3_VECTOR0_EVENT_RST,
267 BIT(HNS3_VECTOR0_IMPRESET_INT_B) |
268 BIT(HNS3_VECTOR0_GLOBALRESET_INT_B) |
269 BIT(HNS3_VECTOR0_CORERESET_INT_B));
270 hns3_clear_event_cause(hw, HNS3_VECTOR0_EVENT_MBX, 0);
271 hns3_clear_event_cause(hw, HNS3_VECTOR0_EVENT_PTP,
272 BIT(HNS3_VECTOR0_1588_INT_B));
276 hns3_handle_mac_tnl(struct hns3_hw *hw)
278 struct hns3_cmd_desc desc;
282 /* query and clear mac tnl interruptions */
283 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_QUERY_MAC_TNL_INT, true);
284 ret = hns3_cmd_send(hw, &desc, 1);
286 hns3_err(hw, "failed to query mac tnl int, ret = %d.", ret);
290 status = rte_le_to_cpu_32(desc.data[0]);
292 hns3_warn(hw, "mac tnl int occurs, status = 0x%x.", status);
293 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_CLEAR_MAC_TNL_INT,
295 desc.data[0] = rte_cpu_to_le_32(HNS3_MAC_TNL_INT_CLR);
296 ret = hns3_cmd_send(hw, &desc, 1);
298 hns3_err(hw, "failed to clear mac tnl int, ret = %d.",
304 hns3_interrupt_handler(void *param)
306 struct rte_eth_dev *dev = (struct rte_eth_dev *)param;
307 struct hns3_adapter *hns = dev->data->dev_private;
308 struct hns3_hw *hw = &hns->hw;
309 enum hns3_evt_cause event_cause;
310 uint32_t clearval = 0;
311 uint32_t vector0_int;
315 /* Disable interrupt */
316 hns3_pf_disable_irq0(hw);
318 event_cause = hns3_check_event_cause(hns, &clearval);
319 vector0_int = hns3_read_dev(hw, HNS3_VECTOR0_OTHER_INT_STS_REG);
320 ras_int = hns3_read_dev(hw, HNS3_RAS_PF_OTHER_INT_STS_REG);
321 cmdq_int = hns3_read_dev(hw, HNS3_VECTOR0_CMDQ_SRC_REG);
322 /* vector 0 interrupt is shared with reset and mailbox source events. */
323 if (event_cause == HNS3_VECTOR0_EVENT_ERR) {
324 hns3_warn(hw, "received interrupt: vector0_int_stat:0x%x "
325 "ras_int_stat:0x%x cmdq_int_stat:0x%x",
326 vector0_int, ras_int, cmdq_int);
327 hns3_handle_mac_tnl(hw);
328 hns3_handle_error(hns);
329 } else if (event_cause == HNS3_VECTOR0_EVENT_RST) {
330 hns3_warn(hw, "received reset interrupt");
331 hns3_schedule_reset(hns);
332 } else if (event_cause == HNS3_VECTOR0_EVENT_MBX) {
333 hns3_dev_handle_mbx_msg(hw);
335 hns3_warn(hw, "received unknown event: vector0_int_stat:0x%x "
336 "ras_int_stat:0x%x cmdq_int_stat:0x%x",
337 vector0_int, ras_int, cmdq_int);
340 hns3_clear_event_cause(hw, event_cause, clearval);
341 /* Enable interrupt if it is not cause by reset */
342 hns3_pf_enable_irq0(hw);
346 hns3_set_port_vlan_filter(struct hns3_adapter *hns, uint16_t vlan_id, int on)
348 #define HNS3_VLAN_ID_OFFSET_STEP 160
349 #define HNS3_VLAN_BYTE_SIZE 8
350 struct hns3_vlan_filter_pf_cfg_cmd *req;
351 struct hns3_hw *hw = &hns->hw;
352 uint8_t vlan_offset_byte_val;
353 struct hns3_cmd_desc desc;
354 uint8_t vlan_offset_byte;
355 uint8_t vlan_offset_base;
358 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_VLAN_FILTER_PF_CFG, false);
360 vlan_offset_base = vlan_id / HNS3_VLAN_ID_OFFSET_STEP;
361 vlan_offset_byte = (vlan_id % HNS3_VLAN_ID_OFFSET_STEP) /
363 vlan_offset_byte_val = 1 << (vlan_id % HNS3_VLAN_BYTE_SIZE);
365 req = (struct hns3_vlan_filter_pf_cfg_cmd *)desc.data;
366 req->vlan_offset = vlan_offset_base;
367 req->vlan_cfg = on ? 0 : 1;
368 req->vlan_offset_bitmap[vlan_offset_byte] = vlan_offset_byte_val;
370 ret = hns3_cmd_send(hw, &desc, 1);
372 hns3_err(hw, "set port vlan id failed, vlan_id =%u, ret =%d",
379 hns3_rm_dev_vlan_table(struct hns3_adapter *hns, uint16_t vlan_id)
381 struct hns3_user_vlan_table *vlan_entry;
382 struct hns3_pf *pf = &hns->pf;
384 LIST_FOREACH(vlan_entry, &pf->vlan_list, next) {
385 if (vlan_entry->vlan_id == vlan_id) {
386 if (vlan_entry->hd_tbl_status)
387 hns3_set_port_vlan_filter(hns, vlan_id, 0);
388 LIST_REMOVE(vlan_entry, next);
389 rte_free(vlan_entry);
396 hns3_add_dev_vlan_table(struct hns3_adapter *hns, uint16_t vlan_id,
399 struct hns3_user_vlan_table *vlan_entry;
400 struct hns3_hw *hw = &hns->hw;
401 struct hns3_pf *pf = &hns->pf;
403 LIST_FOREACH(vlan_entry, &pf->vlan_list, next) {
404 if (vlan_entry->vlan_id == vlan_id)
408 vlan_entry = rte_zmalloc("hns3_vlan_tbl", sizeof(*vlan_entry), 0);
409 if (vlan_entry == NULL) {
410 hns3_err(hw, "Failed to malloc hns3 vlan table");
414 vlan_entry->hd_tbl_status = writen_to_tbl;
415 vlan_entry->vlan_id = vlan_id;
417 LIST_INSERT_HEAD(&pf->vlan_list, vlan_entry, next);
421 hns3_restore_vlan_table(struct hns3_adapter *hns)
423 struct hns3_user_vlan_table *vlan_entry;
424 struct hns3_hw *hw = &hns->hw;
425 struct hns3_pf *pf = &hns->pf;
429 if (hw->port_base_vlan_cfg.state == HNS3_PORT_BASE_VLAN_ENABLE)
430 return hns3_vlan_pvid_configure(hns,
431 hw->port_base_vlan_cfg.pvid, 1);
433 LIST_FOREACH(vlan_entry, &pf->vlan_list, next) {
434 if (vlan_entry->hd_tbl_status) {
435 vlan_id = vlan_entry->vlan_id;
436 ret = hns3_set_port_vlan_filter(hns, vlan_id, 1);
446 hns3_vlan_filter_configure(struct hns3_adapter *hns, uint16_t vlan_id, int on)
448 struct hns3_hw *hw = &hns->hw;
449 bool writen_to_tbl = false;
453 * When vlan filter is enabled, hardware regards packets without vlan
454 * as packets with vlan 0. So, to receive packets without vlan, vlan id
455 * 0 is not allowed to be removed by rte_eth_dev_vlan_filter.
457 if (on == 0 && vlan_id == 0)
461 * When port base vlan enabled, we use port base vlan as the vlan
462 * filter condition. In this case, we don't update vlan filter table
463 * when user add new vlan or remove exist vlan, just update the
464 * vlan list. The vlan id in vlan list will be writen in vlan filter
465 * table until port base vlan disabled
467 if (hw->port_base_vlan_cfg.state == HNS3_PORT_BASE_VLAN_DISABLE) {
468 ret = hns3_set_port_vlan_filter(hns, vlan_id, on);
469 writen_to_tbl = true;
474 hns3_add_dev_vlan_table(hns, vlan_id, writen_to_tbl);
476 hns3_rm_dev_vlan_table(hns, vlan_id);
482 hns3_vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on)
484 struct hns3_adapter *hns = dev->data->dev_private;
485 struct hns3_hw *hw = &hns->hw;
488 rte_spinlock_lock(&hw->lock);
489 ret = hns3_vlan_filter_configure(hns, vlan_id, on);
490 rte_spinlock_unlock(&hw->lock);
495 hns3_vlan_tpid_configure(struct hns3_adapter *hns, enum rte_vlan_type vlan_type,
498 struct hns3_rx_vlan_type_cfg_cmd *rx_req;
499 struct hns3_tx_vlan_type_cfg_cmd *tx_req;
500 struct hns3_hw *hw = &hns->hw;
501 struct hns3_cmd_desc desc;
504 if ((vlan_type != ETH_VLAN_TYPE_INNER &&
505 vlan_type != ETH_VLAN_TYPE_OUTER)) {
506 hns3_err(hw, "Unsupported vlan type, vlan_type =%d", vlan_type);
510 if (tpid != RTE_ETHER_TYPE_VLAN) {
511 hns3_err(hw, "Unsupported vlan tpid, vlan_type =%d", vlan_type);
515 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_MAC_VLAN_TYPE_ID, false);
516 rx_req = (struct hns3_rx_vlan_type_cfg_cmd *)desc.data;
518 if (vlan_type == ETH_VLAN_TYPE_OUTER) {
519 rx_req->ot_fst_vlan_type = rte_cpu_to_le_16(tpid);
520 rx_req->ot_sec_vlan_type = rte_cpu_to_le_16(tpid);
521 } else if (vlan_type == ETH_VLAN_TYPE_INNER) {
522 rx_req->ot_fst_vlan_type = rte_cpu_to_le_16(tpid);
523 rx_req->ot_sec_vlan_type = rte_cpu_to_le_16(tpid);
524 rx_req->in_fst_vlan_type = rte_cpu_to_le_16(tpid);
525 rx_req->in_sec_vlan_type = rte_cpu_to_le_16(tpid);
528 ret = hns3_cmd_send(hw, &desc, 1);
530 hns3_err(hw, "Send rxvlan protocol type command fail, ret =%d",
535 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_MAC_VLAN_INSERT, false);
537 tx_req = (struct hns3_tx_vlan_type_cfg_cmd *)desc.data;
538 tx_req->ot_vlan_type = rte_cpu_to_le_16(tpid);
539 tx_req->in_vlan_type = rte_cpu_to_le_16(tpid);
541 ret = hns3_cmd_send(hw, &desc, 1);
543 hns3_err(hw, "Send txvlan protocol type command fail, ret =%d",
549 hns3_vlan_tpid_set(struct rte_eth_dev *dev, enum rte_vlan_type vlan_type,
552 struct hns3_adapter *hns = dev->data->dev_private;
553 struct hns3_hw *hw = &hns->hw;
556 rte_spinlock_lock(&hw->lock);
557 ret = hns3_vlan_tpid_configure(hns, vlan_type, tpid);
558 rte_spinlock_unlock(&hw->lock);
563 hns3_set_vlan_rx_offload_cfg(struct hns3_adapter *hns,
564 struct hns3_rx_vtag_cfg *vcfg)
566 struct hns3_vport_vtag_rx_cfg_cmd *req;
567 struct hns3_hw *hw = &hns->hw;
568 struct hns3_cmd_desc desc;
573 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_VLAN_PORT_RX_CFG, false);
575 req = (struct hns3_vport_vtag_rx_cfg_cmd *)desc.data;
576 hns3_set_bit(req->vport_vlan_cfg, HNS3_REM_TAG1_EN_B,
577 vcfg->strip_tag1_en ? 1 : 0);
578 hns3_set_bit(req->vport_vlan_cfg, HNS3_REM_TAG2_EN_B,
579 vcfg->strip_tag2_en ? 1 : 0);
580 hns3_set_bit(req->vport_vlan_cfg, HNS3_SHOW_TAG1_EN_B,
581 vcfg->vlan1_vlan_prionly ? 1 : 0);
582 hns3_set_bit(req->vport_vlan_cfg, HNS3_SHOW_TAG2_EN_B,
583 vcfg->vlan2_vlan_prionly ? 1 : 0);
585 /* firmwall will ignore this configuration for PCI_REVISION_ID_HIP08 */
586 hns3_set_bit(req->vport_vlan_cfg, HNS3_DISCARD_TAG1_EN_B,
587 vcfg->strip_tag1_discard_en ? 1 : 0);
588 hns3_set_bit(req->vport_vlan_cfg, HNS3_DISCARD_TAG2_EN_B,
589 vcfg->strip_tag2_discard_en ? 1 : 0);
591 * In current version VF is not supported when PF is driven by DPDK
592 * driver, just need to configure parameters for PF vport.
594 vport_id = HNS3_PF_FUNC_ID;
595 req->vf_offset = vport_id / HNS3_VF_NUM_PER_CMD;
596 bitmap = 1 << (vport_id % HNS3_VF_NUM_PER_BYTE);
597 req->vf_bitmap[req->vf_offset] = bitmap;
599 ret = hns3_cmd_send(hw, &desc, 1);
601 hns3_err(hw, "Send port rxvlan cfg command fail, ret =%d", ret);
606 hns3_update_rx_offload_cfg(struct hns3_adapter *hns,
607 struct hns3_rx_vtag_cfg *vcfg)
609 struct hns3_pf *pf = &hns->pf;
610 memcpy(&pf->vtag_config.rx_vcfg, vcfg, sizeof(pf->vtag_config.rx_vcfg));
614 hns3_update_tx_offload_cfg(struct hns3_adapter *hns,
615 struct hns3_tx_vtag_cfg *vcfg)
617 struct hns3_pf *pf = &hns->pf;
618 memcpy(&pf->vtag_config.tx_vcfg, vcfg, sizeof(pf->vtag_config.tx_vcfg));
622 hns3_en_hw_strip_rxvtag(struct hns3_adapter *hns, bool enable)
624 struct hns3_rx_vtag_cfg rxvlan_cfg;
625 struct hns3_hw *hw = &hns->hw;
628 if (hw->port_base_vlan_cfg.state == HNS3_PORT_BASE_VLAN_DISABLE) {
629 rxvlan_cfg.strip_tag1_en = false;
630 rxvlan_cfg.strip_tag2_en = enable;
631 rxvlan_cfg.strip_tag2_discard_en = false;
633 rxvlan_cfg.strip_tag1_en = enable;
634 rxvlan_cfg.strip_tag2_en = true;
635 rxvlan_cfg.strip_tag2_discard_en = true;
638 rxvlan_cfg.strip_tag1_discard_en = false;
639 rxvlan_cfg.vlan1_vlan_prionly = false;
640 rxvlan_cfg.vlan2_vlan_prionly = false;
641 rxvlan_cfg.rx_vlan_offload_en = enable;
643 ret = hns3_set_vlan_rx_offload_cfg(hns, &rxvlan_cfg);
645 hns3_err(hw, "enable strip rx vtag failed, ret =%d", ret);
649 hns3_update_rx_offload_cfg(hns, &rxvlan_cfg);
655 hns3_set_vlan_filter_ctrl(struct hns3_hw *hw, uint8_t vlan_type,
656 uint8_t fe_type, bool filter_en, uint8_t vf_id)
658 struct hns3_vlan_filter_ctrl_cmd *req;
659 struct hns3_cmd_desc desc;
662 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_VLAN_FILTER_CTRL, false);
664 req = (struct hns3_vlan_filter_ctrl_cmd *)desc.data;
665 req->vlan_type = vlan_type;
666 req->vlan_fe = filter_en ? fe_type : 0;
669 ret = hns3_cmd_send(hw, &desc, 1);
671 hns3_err(hw, "set vlan filter fail, ret =%d", ret);
677 hns3_vlan_filter_init(struct hns3_adapter *hns)
679 struct hns3_hw *hw = &hns->hw;
682 ret = hns3_set_vlan_filter_ctrl(hw, HNS3_FILTER_TYPE_VF,
683 HNS3_FILTER_FE_EGRESS, false,
686 hns3_err(hw, "failed to init vf vlan filter, ret = %d", ret);
690 ret = hns3_set_vlan_filter_ctrl(hw, HNS3_FILTER_TYPE_PORT,
691 HNS3_FILTER_FE_INGRESS, false,
694 hns3_err(hw, "failed to init port vlan filter, ret = %d", ret);
700 hns3_enable_vlan_filter(struct hns3_adapter *hns, bool enable)
702 struct hns3_hw *hw = &hns->hw;
705 ret = hns3_set_vlan_filter_ctrl(hw, HNS3_FILTER_TYPE_PORT,
706 HNS3_FILTER_FE_INGRESS, enable,
709 hns3_err(hw, "failed to %s port vlan filter, ret = %d",
710 enable ? "enable" : "disable", ret);
716 hns3_vlan_offload_set(struct rte_eth_dev *dev, int mask)
718 struct hns3_adapter *hns = dev->data->dev_private;
719 struct hns3_hw *hw = &hns->hw;
720 struct rte_eth_rxmode *rxmode;
721 unsigned int tmp_mask;
725 rte_spinlock_lock(&hw->lock);
726 rxmode = &dev->data->dev_conf.rxmode;
727 tmp_mask = (unsigned int)mask;
728 if (tmp_mask & ETH_VLAN_FILTER_MASK) {
729 /* ignore vlan filter configuration during promiscuous mode */
730 if (!dev->data->promiscuous) {
731 /* Enable or disable VLAN filter */
732 enable = rxmode->offloads & DEV_RX_OFFLOAD_VLAN_FILTER ?
735 ret = hns3_enable_vlan_filter(hns, enable);
737 rte_spinlock_unlock(&hw->lock);
738 hns3_err(hw, "failed to %s rx filter, ret = %d",
739 enable ? "enable" : "disable", ret);
745 if (tmp_mask & ETH_VLAN_STRIP_MASK) {
746 /* Enable or disable VLAN stripping */
747 enable = rxmode->offloads & DEV_RX_OFFLOAD_VLAN_STRIP ?
750 ret = hns3_en_hw_strip_rxvtag(hns, enable);
752 rte_spinlock_unlock(&hw->lock);
753 hns3_err(hw, "failed to %s rx strip, ret = %d",
754 enable ? "enable" : "disable", ret);
759 rte_spinlock_unlock(&hw->lock);
765 hns3_set_vlan_tx_offload_cfg(struct hns3_adapter *hns,
766 struct hns3_tx_vtag_cfg *vcfg)
768 struct hns3_vport_vtag_tx_cfg_cmd *req;
769 struct hns3_cmd_desc desc;
770 struct hns3_hw *hw = &hns->hw;
775 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_VLAN_PORT_TX_CFG, false);
777 req = (struct hns3_vport_vtag_tx_cfg_cmd *)desc.data;
778 req->def_vlan_tag1 = vcfg->default_tag1;
779 req->def_vlan_tag2 = vcfg->default_tag2;
780 hns3_set_bit(req->vport_vlan_cfg, HNS3_ACCEPT_TAG1_B,
781 vcfg->accept_tag1 ? 1 : 0);
782 hns3_set_bit(req->vport_vlan_cfg, HNS3_ACCEPT_UNTAG1_B,
783 vcfg->accept_untag1 ? 1 : 0);
784 hns3_set_bit(req->vport_vlan_cfg, HNS3_ACCEPT_TAG2_B,
785 vcfg->accept_tag2 ? 1 : 0);
786 hns3_set_bit(req->vport_vlan_cfg, HNS3_ACCEPT_UNTAG2_B,
787 vcfg->accept_untag2 ? 1 : 0);
788 hns3_set_bit(req->vport_vlan_cfg, HNS3_PORT_INS_TAG1_EN_B,
789 vcfg->insert_tag1_en ? 1 : 0);
790 hns3_set_bit(req->vport_vlan_cfg, HNS3_PORT_INS_TAG2_EN_B,
791 vcfg->insert_tag2_en ? 1 : 0);
792 hns3_set_bit(req->vport_vlan_cfg, HNS3_CFG_NIC_ROCE_SEL_B, 0);
794 /* firmwall will ignore this configuration for PCI_REVISION_ID_HIP08 */
795 hns3_set_bit(req->vport_vlan_cfg, HNS3_TAG_SHIFT_MODE_EN_B,
796 vcfg->tag_shift_mode_en ? 1 : 0);
799 * In current version VF is not supported when PF is driven by DPDK
800 * driver, just need to configure parameters for PF vport.
802 vport_id = HNS3_PF_FUNC_ID;
803 req->vf_offset = vport_id / HNS3_VF_NUM_PER_CMD;
804 bitmap = 1 << (vport_id % HNS3_VF_NUM_PER_BYTE);
805 req->vf_bitmap[req->vf_offset] = bitmap;
807 ret = hns3_cmd_send(hw, &desc, 1);
809 hns3_err(hw, "Send port txvlan cfg command fail, ret =%d", ret);
815 hns3_vlan_txvlan_cfg(struct hns3_adapter *hns, uint16_t port_base_vlan_state,
818 struct hns3_hw *hw = &hns->hw;
819 struct hns3_tx_vtag_cfg txvlan_cfg;
822 if (port_base_vlan_state == HNS3_PORT_BASE_VLAN_DISABLE) {
823 txvlan_cfg.accept_tag1 = true;
824 txvlan_cfg.insert_tag1_en = false;
825 txvlan_cfg.default_tag1 = 0;
827 txvlan_cfg.accept_tag1 =
828 hw->vlan_mode == HNS3_HW_SHIFT_AND_DISCARD_MODE;
829 txvlan_cfg.insert_tag1_en = true;
830 txvlan_cfg.default_tag1 = pvid;
833 txvlan_cfg.accept_untag1 = true;
834 txvlan_cfg.accept_tag2 = true;
835 txvlan_cfg.accept_untag2 = true;
836 txvlan_cfg.insert_tag2_en = false;
837 txvlan_cfg.default_tag2 = 0;
838 txvlan_cfg.tag_shift_mode_en = true;
840 ret = hns3_set_vlan_tx_offload_cfg(hns, &txvlan_cfg);
842 hns3_err(hw, "pf vlan set pvid failed, pvid =%u ,ret =%d", pvid,
847 hns3_update_tx_offload_cfg(hns, &txvlan_cfg);
853 hns3_rm_all_vlan_table(struct hns3_adapter *hns, bool is_del_list)
855 struct hns3_user_vlan_table *vlan_entry;
856 struct hns3_pf *pf = &hns->pf;
858 LIST_FOREACH(vlan_entry, &pf->vlan_list, next) {
859 if (vlan_entry->hd_tbl_status) {
860 hns3_set_port_vlan_filter(hns, vlan_entry->vlan_id, 0);
861 vlan_entry->hd_tbl_status = false;
866 vlan_entry = LIST_FIRST(&pf->vlan_list);
868 LIST_REMOVE(vlan_entry, next);
869 rte_free(vlan_entry);
870 vlan_entry = LIST_FIRST(&pf->vlan_list);
876 hns3_add_all_vlan_table(struct hns3_adapter *hns)
878 struct hns3_user_vlan_table *vlan_entry;
879 struct hns3_pf *pf = &hns->pf;
881 LIST_FOREACH(vlan_entry, &pf->vlan_list, next) {
882 if (!vlan_entry->hd_tbl_status) {
883 hns3_set_port_vlan_filter(hns, vlan_entry->vlan_id, 1);
884 vlan_entry->hd_tbl_status = true;
890 hns3_remove_all_vlan_table(struct hns3_adapter *hns)
892 struct hns3_hw *hw = &hns->hw;
895 hns3_rm_all_vlan_table(hns, true);
896 if (hw->port_base_vlan_cfg.pvid != HNS3_INVALID_PVID) {
897 ret = hns3_set_port_vlan_filter(hns,
898 hw->port_base_vlan_cfg.pvid, 0);
900 hns3_err(hw, "Failed to remove all vlan table, ret =%d",
908 hns3_update_vlan_filter_entries(struct hns3_adapter *hns,
909 uint16_t port_base_vlan_state, uint16_t new_pvid)
911 struct hns3_hw *hw = &hns->hw;
915 if (port_base_vlan_state == HNS3_PORT_BASE_VLAN_ENABLE) {
916 old_pvid = hw->port_base_vlan_cfg.pvid;
917 if (old_pvid != HNS3_INVALID_PVID) {
918 ret = hns3_set_port_vlan_filter(hns, old_pvid, 0);
920 hns3_err(hw, "failed to remove old pvid %u, "
921 "ret = %d", old_pvid, ret);
926 hns3_rm_all_vlan_table(hns, false);
927 ret = hns3_set_port_vlan_filter(hns, new_pvid, 1);
929 hns3_err(hw, "failed to add new pvid %u, ret = %d",
934 ret = hns3_set_port_vlan_filter(hns, new_pvid, 0);
936 hns3_err(hw, "failed to remove pvid %u, ret = %d",
941 hns3_add_all_vlan_table(hns);
947 hns3_en_pvid_strip(struct hns3_adapter *hns, int on)
949 struct hns3_rx_vtag_cfg *old_cfg = &hns->pf.vtag_config.rx_vcfg;
950 struct hns3_rx_vtag_cfg rx_vlan_cfg;
954 rx_strip_en = old_cfg->rx_vlan_offload_en;
956 rx_vlan_cfg.strip_tag1_en = rx_strip_en;
957 rx_vlan_cfg.strip_tag2_en = true;
958 rx_vlan_cfg.strip_tag2_discard_en = true;
960 rx_vlan_cfg.strip_tag1_en = false;
961 rx_vlan_cfg.strip_tag2_en = rx_strip_en;
962 rx_vlan_cfg.strip_tag2_discard_en = false;
964 rx_vlan_cfg.strip_tag1_discard_en = false;
965 rx_vlan_cfg.vlan1_vlan_prionly = false;
966 rx_vlan_cfg.vlan2_vlan_prionly = false;
967 rx_vlan_cfg.rx_vlan_offload_en = old_cfg->rx_vlan_offload_en;
969 ret = hns3_set_vlan_rx_offload_cfg(hns, &rx_vlan_cfg);
973 hns3_update_rx_offload_cfg(hns, &rx_vlan_cfg);
978 hns3_vlan_pvid_configure(struct hns3_adapter *hns, uint16_t pvid, int on)
980 struct hns3_hw *hw = &hns->hw;
981 uint16_t port_base_vlan_state;
984 if (on == 0 && pvid != hw->port_base_vlan_cfg.pvid) {
985 if (hw->port_base_vlan_cfg.pvid != HNS3_INVALID_PVID)
986 hns3_warn(hw, "Invalid operation! As current pvid set "
987 "is %u, disable pvid %u is invalid",
988 hw->port_base_vlan_cfg.pvid, pvid);
992 port_base_vlan_state = on ? HNS3_PORT_BASE_VLAN_ENABLE :
993 HNS3_PORT_BASE_VLAN_DISABLE;
994 ret = hns3_vlan_txvlan_cfg(hns, port_base_vlan_state, pvid);
996 hns3_err(hw, "failed to config tx vlan for pvid, ret = %d",
1001 ret = hns3_en_pvid_strip(hns, on);
1003 hns3_err(hw, "failed to config rx vlan strip for pvid, "
1005 goto pvid_vlan_strip_fail;
1008 if (pvid == HNS3_INVALID_PVID)
1010 ret = hns3_update_vlan_filter_entries(hns, port_base_vlan_state, pvid);
1012 hns3_err(hw, "failed to update vlan filter entries, ret = %d",
1014 goto vlan_filter_set_fail;
1018 hw->port_base_vlan_cfg.state = port_base_vlan_state;
1019 hw->port_base_vlan_cfg.pvid = on ? pvid : HNS3_INVALID_PVID;
1022 vlan_filter_set_fail:
1023 err = hns3_en_pvid_strip(hns, hw->port_base_vlan_cfg.state ==
1024 HNS3_PORT_BASE_VLAN_ENABLE);
1026 hns3_err(hw, "fail to rollback pvid strip, ret = %d", err);
1028 pvid_vlan_strip_fail:
1029 err = hns3_vlan_txvlan_cfg(hns, hw->port_base_vlan_cfg.state,
1030 hw->port_base_vlan_cfg.pvid);
1032 hns3_err(hw, "fail to rollback txvlan status, ret = %d", err);
1038 hns3_vlan_pvid_set(struct rte_eth_dev *dev, uint16_t pvid, int on)
1040 struct hns3_adapter *hns = dev->data->dev_private;
1041 struct hns3_hw *hw = &hns->hw;
1042 bool pvid_en_state_change;
1043 uint16_t pvid_state;
1046 if (pvid > RTE_ETHER_MAX_VLAN_ID) {
1047 hns3_err(hw, "Invalid vlan_id = %u > %d", pvid,
1048 RTE_ETHER_MAX_VLAN_ID);
1053 * If PVID configuration state change, should refresh the PVID
1054 * configuration state in struct hns3_tx_queue/hns3_rx_queue.
1056 pvid_state = hw->port_base_vlan_cfg.state;
1057 if ((on && pvid_state == HNS3_PORT_BASE_VLAN_ENABLE) ||
1058 (!on && pvid_state == HNS3_PORT_BASE_VLAN_DISABLE))
1059 pvid_en_state_change = false;
1061 pvid_en_state_change = true;
1063 rte_spinlock_lock(&hw->lock);
1064 ret = hns3_vlan_pvid_configure(hns, pvid, on);
1065 rte_spinlock_unlock(&hw->lock);
1069 * Only in HNS3_SW_SHIFT_AND_MODE the PVID related operation in Tx/Rx
1070 * need be processed by PMD driver.
1072 if (pvid_en_state_change &&
1073 hw->vlan_mode == HNS3_SW_SHIFT_AND_DISCARD_MODE)
1074 hns3_update_all_queues_pvid_proc_en(hw);
1080 hns3_default_vlan_config(struct hns3_adapter *hns)
1082 struct hns3_hw *hw = &hns->hw;
1086 * When vlan filter is enabled, hardware regards packets without vlan
1087 * as packets with vlan 0. Therefore, if vlan 0 is not in the vlan
1088 * table, packets without vlan won't be received. So, add vlan 0 as
1091 ret = hns3_vlan_filter_configure(hns, 0, 1);
1093 hns3_err(hw, "default vlan 0 config failed, ret =%d", ret);
1098 hns3_init_vlan_config(struct hns3_adapter *hns)
1100 struct hns3_hw *hw = &hns->hw;
1104 * This function can be called in the initialization and reset process,
1105 * when in reset process, it means that hardware had been reseted
1106 * successfully and we need to restore the hardware configuration to
1107 * ensure that the hardware configuration remains unchanged before and
1110 if (__atomic_load_n(&hw->reset.resetting, __ATOMIC_RELAXED) == 0) {
1111 hw->port_base_vlan_cfg.state = HNS3_PORT_BASE_VLAN_DISABLE;
1112 hw->port_base_vlan_cfg.pvid = HNS3_INVALID_PVID;
1115 ret = hns3_vlan_filter_init(hns);
1117 hns3_err(hw, "vlan init fail in pf, ret =%d", ret);
1121 ret = hns3_vlan_tpid_configure(hns, ETH_VLAN_TYPE_INNER,
1122 RTE_ETHER_TYPE_VLAN);
1124 hns3_err(hw, "tpid set fail in pf, ret =%d", ret);
1129 * When in the reinit dev stage of the reset process, the following
1130 * vlan-related configurations may differ from those at initialization,
1131 * we will restore configurations to hardware in hns3_restore_vlan_table
1132 * and hns3_restore_vlan_conf later.
1134 if (__atomic_load_n(&hw->reset.resetting, __ATOMIC_RELAXED) == 0) {
1135 ret = hns3_vlan_pvid_configure(hns, HNS3_INVALID_PVID, 0);
1137 hns3_err(hw, "pvid set fail in pf, ret =%d", ret);
1141 ret = hns3_en_hw_strip_rxvtag(hns, false);
1143 hns3_err(hw, "rx strip configure fail in pf, ret =%d",
1149 return hns3_default_vlan_config(hns);
1153 hns3_restore_vlan_conf(struct hns3_adapter *hns)
1155 struct hns3_pf *pf = &hns->pf;
1156 struct hns3_hw *hw = &hns->hw;
1161 if (!hw->data->promiscuous) {
1162 /* restore vlan filter states */
1163 offloads = hw->data->dev_conf.rxmode.offloads;
1164 enable = offloads & DEV_RX_OFFLOAD_VLAN_FILTER ? true : false;
1165 ret = hns3_enable_vlan_filter(hns, enable);
1167 hns3_err(hw, "failed to restore vlan rx filter conf, "
1173 ret = hns3_set_vlan_rx_offload_cfg(hns, &pf->vtag_config.rx_vcfg);
1175 hns3_err(hw, "failed to restore vlan rx conf, ret = %d", ret);
1179 ret = hns3_set_vlan_tx_offload_cfg(hns, &pf->vtag_config.tx_vcfg);
1181 hns3_err(hw, "failed to restore vlan tx conf, ret = %d", ret);
1187 hns3_dev_configure_vlan(struct rte_eth_dev *dev)
1189 struct hns3_adapter *hns = dev->data->dev_private;
1190 struct rte_eth_dev_data *data = dev->data;
1191 struct rte_eth_txmode *txmode;
1192 struct hns3_hw *hw = &hns->hw;
1196 txmode = &data->dev_conf.txmode;
1197 if (txmode->hw_vlan_reject_tagged || txmode->hw_vlan_reject_untagged)
1199 "hw_vlan_reject_tagged or hw_vlan_reject_untagged "
1200 "configuration is not supported! Ignore these two "
1201 "parameters: hw_vlan_reject_tagged(%u), "
1202 "hw_vlan_reject_untagged(%u)",
1203 txmode->hw_vlan_reject_tagged,
1204 txmode->hw_vlan_reject_untagged);
1206 /* Apply vlan offload setting */
1207 mask = ETH_VLAN_STRIP_MASK | ETH_VLAN_FILTER_MASK;
1208 ret = hns3_vlan_offload_set(dev, mask);
1210 hns3_err(hw, "dev config rx vlan offload failed, ret = %d",
1216 * If pvid config is not set in rte_eth_conf, driver needn't to set
1217 * VLAN pvid related configuration to hardware.
1219 if (txmode->pvid == 0 && txmode->hw_vlan_insert_pvid == 0)
1222 /* Apply pvid setting */
1223 ret = hns3_vlan_pvid_set(dev, txmode->pvid,
1224 txmode->hw_vlan_insert_pvid);
1226 hns3_err(hw, "dev config vlan pvid(%u) failed, ret = %d",
1233 hns3_config_tso(struct hns3_hw *hw, unsigned int tso_mss_min,
1234 unsigned int tso_mss_max)
1236 struct hns3_cfg_tso_status_cmd *req;
1237 struct hns3_cmd_desc desc;
1240 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_TSO_GENERIC_CONFIG, false);
1242 req = (struct hns3_cfg_tso_status_cmd *)desc.data;
1245 hns3_set_field(tso_mss, HNS3_TSO_MSS_MIN_M, HNS3_TSO_MSS_MIN_S,
1247 req->tso_mss_min = rte_cpu_to_le_16(tso_mss);
1250 hns3_set_field(tso_mss, HNS3_TSO_MSS_MIN_M, HNS3_TSO_MSS_MIN_S,
1252 req->tso_mss_max = rte_cpu_to_le_16(tso_mss);
1254 return hns3_cmd_send(hw, &desc, 1);
1258 hns3_set_umv_space(struct hns3_hw *hw, uint16_t space_size,
1259 uint16_t *allocated_size, bool is_alloc)
1261 struct hns3_umv_spc_alc_cmd *req;
1262 struct hns3_cmd_desc desc;
1265 req = (struct hns3_umv_spc_alc_cmd *)desc.data;
1266 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_MAC_VLAN_ALLOCATE, false);
1267 hns3_set_bit(req->allocate, HNS3_UMV_SPC_ALC_B, is_alloc ? 0 : 1);
1268 req->space_size = rte_cpu_to_le_32(space_size);
1270 ret = hns3_cmd_send(hw, &desc, 1);
1272 PMD_INIT_LOG(ERR, "%s umv space failed for cmd_send, ret =%d",
1273 is_alloc ? "allocate" : "free", ret);
1277 if (is_alloc && allocated_size)
1278 *allocated_size = rte_le_to_cpu_32(desc.data[1]);
1284 hns3_init_umv_space(struct hns3_hw *hw)
1286 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
1287 struct hns3_pf *pf = &hns->pf;
1288 uint16_t allocated_size = 0;
1291 ret = hns3_set_umv_space(hw, pf->wanted_umv_size, &allocated_size,
1296 if (allocated_size < pf->wanted_umv_size)
1297 PMD_INIT_LOG(WARNING, "Alloc umv space failed, want %u, get %u",
1298 pf->wanted_umv_size, allocated_size);
1300 pf->max_umv_size = (!!allocated_size) ? allocated_size :
1301 pf->wanted_umv_size;
1302 pf->used_umv_size = 0;
1307 hns3_uninit_umv_space(struct hns3_hw *hw)
1309 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
1310 struct hns3_pf *pf = &hns->pf;
1313 if (pf->max_umv_size == 0)
1316 ret = hns3_set_umv_space(hw, pf->max_umv_size, NULL, false);
1320 pf->max_umv_size = 0;
1326 hns3_is_umv_space_full(struct hns3_hw *hw)
1328 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
1329 struct hns3_pf *pf = &hns->pf;
1332 is_full = (pf->used_umv_size >= pf->max_umv_size);
1338 hns3_update_umv_space(struct hns3_hw *hw, bool is_free)
1340 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
1341 struct hns3_pf *pf = &hns->pf;
1344 if (pf->used_umv_size > 0)
1345 pf->used_umv_size--;
1347 pf->used_umv_size++;
1351 hns3_prepare_mac_addr(struct hns3_mac_vlan_tbl_entry_cmd *new_req,
1352 const uint8_t *addr, bool is_mc)
1354 const unsigned char *mac_addr = addr;
1355 uint32_t high_val = ((uint32_t)mac_addr[3] << 24) |
1356 ((uint32_t)mac_addr[2] << 16) |
1357 ((uint32_t)mac_addr[1] << 8) |
1358 (uint32_t)mac_addr[0];
1359 uint32_t low_val = ((uint32_t)mac_addr[5] << 8) | (uint32_t)mac_addr[4];
1361 hns3_set_bit(new_req->flags, HNS3_MAC_VLAN_BIT0_EN_B, 1);
1363 hns3_set_bit(new_req->entry_type, HNS3_MAC_VLAN_BIT0_EN_B, 0);
1364 hns3_set_bit(new_req->entry_type, HNS3_MAC_VLAN_BIT1_EN_B, 1);
1365 hns3_set_bit(new_req->mc_mac_en, HNS3_MAC_VLAN_BIT0_EN_B, 1);
1368 new_req->mac_addr_hi32 = rte_cpu_to_le_32(high_val);
1369 new_req->mac_addr_lo16 = rte_cpu_to_le_16(low_val & 0xffff);
1373 hns3_get_mac_vlan_cmd_status(struct hns3_hw *hw, uint16_t cmdq_resp,
1375 enum hns3_mac_vlan_tbl_opcode op)
1378 hns3_err(hw, "cmdq execute failed for get_mac_vlan_cmd_status,status=%u",
1383 if (op == HNS3_MAC_VLAN_ADD) {
1384 if (resp_code == 0 || resp_code == 1) {
1386 } else if (resp_code == HNS3_ADD_UC_OVERFLOW) {
1387 hns3_err(hw, "add mac addr failed for uc_overflow");
1389 } else if (resp_code == HNS3_ADD_MC_OVERFLOW) {
1390 hns3_err(hw, "add mac addr failed for mc_overflow");
1394 hns3_err(hw, "add mac addr failed for undefined, code=%u",
1397 } else if (op == HNS3_MAC_VLAN_REMOVE) {
1398 if (resp_code == 0) {
1400 } else if (resp_code == 1) {
1401 hns3_dbg(hw, "remove mac addr failed for miss");
1405 hns3_err(hw, "remove mac addr failed for undefined, code=%u",
1408 } else if (op == HNS3_MAC_VLAN_LKUP) {
1409 if (resp_code == 0) {
1411 } else if (resp_code == 1) {
1412 hns3_dbg(hw, "lookup mac addr failed for miss");
1416 hns3_err(hw, "lookup mac addr failed for undefined, code=%u",
1421 hns3_err(hw, "unknown opcode for get_mac_vlan_cmd_status, opcode=%u",
1428 hns3_lookup_mac_vlan_tbl(struct hns3_hw *hw,
1429 struct hns3_mac_vlan_tbl_entry_cmd *req,
1430 struct hns3_cmd_desc *desc, bool is_mc)
1436 hns3_cmd_setup_basic_desc(&desc[0], HNS3_OPC_MAC_VLAN_ADD, true);
1438 desc[0].flag |= rte_cpu_to_le_16(HNS3_CMD_FLAG_NEXT);
1439 memcpy(desc[0].data, req,
1440 sizeof(struct hns3_mac_vlan_tbl_entry_cmd));
1441 hns3_cmd_setup_basic_desc(&desc[1], HNS3_OPC_MAC_VLAN_ADD,
1443 desc[1].flag |= rte_cpu_to_le_16(HNS3_CMD_FLAG_NEXT);
1444 hns3_cmd_setup_basic_desc(&desc[2], HNS3_OPC_MAC_VLAN_ADD,
1446 ret = hns3_cmd_send(hw, desc, HNS3_MC_MAC_VLAN_ADD_DESC_NUM);
1448 memcpy(desc[0].data, req,
1449 sizeof(struct hns3_mac_vlan_tbl_entry_cmd));
1450 ret = hns3_cmd_send(hw, desc, 1);
1453 hns3_err(hw, "lookup mac addr failed for cmd_send, ret =%d.",
1457 resp_code = (rte_le_to_cpu_32(desc[0].data[0]) >> 8) & 0xff;
1458 retval = rte_le_to_cpu_16(desc[0].retval);
1460 return hns3_get_mac_vlan_cmd_status(hw, retval, resp_code,
1461 HNS3_MAC_VLAN_LKUP);
1465 hns3_add_mac_vlan_tbl(struct hns3_hw *hw,
1466 struct hns3_mac_vlan_tbl_entry_cmd *req,
1467 struct hns3_cmd_desc *mc_desc)
1474 if (mc_desc == NULL) {
1475 struct hns3_cmd_desc desc;
1477 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_MAC_VLAN_ADD, false);
1478 memcpy(desc.data, req,
1479 sizeof(struct hns3_mac_vlan_tbl_entry_cmd));
1480 ret = hns3_cmd_send(hw, &desc, 1);
1481 resp_code = (rte_le_to_cpu_32(desc.data[0]) >> 8) & 0xff;
1482 retval = rte_le_to_cpu_16(desc.retval);
1484 cfg_status = hns3_get_mac_vlan_cmd_status(hw, retval, resp_code,
1487 hns3_cmd_reuse_desc(&mc_desc[0], false);
1488 mc_desc[0].flag |= rte_cpu_to_le_16(HNS3_CMD_FLAG_NEXT);
1489 hns3_cmd_reuse_desc(&mc_desc[1], false);
1490 mc_desc[1].flag |= rte_cpu_to_le_16(HNS3_CMD_FLAG_NEXT);
1491 hns3_cmd_reuse_desc(&mc_desc[2], false);
1492 mc_desc[2].flag &= rte_cpu_to_le_16(~HNS3_CMD_FLAG_NEXT);
1493 memcpy(mc_desc[0].data, req,
1494 sizeof(struct hns3_mac_vlan_tbl_entry_cmd));
1495 mc_desc[0].retval = 0;
1496 ret = hns3_cmd_send(hw, mc_desc, HNS3_MC_MAC_VLAN_ADD_DESC_NUM);
1497 resp_code = (rte_le_to_cpu_32(mc_desc[0].data[0]) >> 8) & 0xff;
1498 retval = rte_le_to_cpu_16(mc_desc[0].retval);
1500 cfg_status = hns3_get_mac_vlan_cmd_status(hw, retval, resp_code,
1505 hns3_err(hw, "add mac addr failed for cmd_send, ret =%d", ret);
1513 hns3_remove_mac_vlan_tbl(struct hns3_hw *hw,
1514 struct hns3_mac_vlan_tbl_entry_cmd *req)
1516 struct hns3_cmd_desc desc;
1521 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_MAC_VLAN_REMOVE, false);
1523 memcpy(desc.data, req, sizeof(struct hns3_mac_vlan_tbl_entry_cmd));
1525 ret = hns3_cmd_send(hw, &desc, 1);
1527 hns3_err(hw, "del mac addr failed for cmd_send, ret =%d", ret);
1530 resp_code = (rte_le_to_cpu_32(desc.data[0]) >> 8) & 0xff;
1531 retval = rte_le_to_cpu_16(desc.retval);
1533 return hns3_get_mac_vlan_cmd_status(hw, retval, resp_code,
1534 HNS3_MAC_VLAN_REMOVE);
1538 hns3_add_uc_addr_common(struct hns3_hw *hw, struct rte_ether_addr *mac_addr)
1540 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
1541 struct hns3_mac_vlan_tbl_entry_cmd req;
1542 struct hns3_pf *pf = &hns->pf;
1543 struct hns3_cmd_desc desc[3];
1544 char mac_str[RTE_ETHER_ADDR_FMT_SIZE];
1545 uint16_t egress_port = 0;
1549 /* check if mac addr is valid */
1550 if (!rte_is_valid_assigned_ether_addr(mac_addr)) {
1551 hns3_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
1553 hns3_err(hw, "Add unicast mac addr err! addr(%s) invalid",
1558 memset(&req, 0, sizeof(req));
1561 * In current version VF is not supported when PF is driven by DPDK
1562 * driver, just need to configure parameters for PF vport.
1564 vf_id = HNS3_PF_FUNC_ID;
1565 hns3_set_field(egress_port, HNS3_MAC_EPORT_VFID_M,
1566 HNS3_MAC_EPORT_VFID_S, vf_id);
1568 req.egress_port = rte_cpu_to_le_16(egress_port);
1570 hns3_prepare_mac_addr(&req, mac_addr->addr_bytes, false);
1573 * Lookup the mac address in the mac_vlan table, and add
1574 * it if the entry is inexistent. Repeated unicast entry
1575 * is not allowed in the mac vlan table.
1577 ret = hns3_lookup_mac_vlan_tbl(hw, &req, desc, false);
1578 if (ret == -ENOENT) {
1579 if (!hns3_is_umv_space_full(hw)) {
1580 ret = hns3_add_mac_vlan_tbl(hw, &req, NULL);
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_add_mc_addr_common(struct hns3_hw *hw, struct rte_ether_addr *mac_addr)
1608 char mac_str[RTE_ETHER_ADDR_FMT_SIZE];
1609 struct rte_ether_addr *addr;
1613 for (i = 0; i < hw->mc_addrs_num; i++) {
1614 addr = &hw->mc_addrs[i];
1615 /* Check if there are duplicate addresses */
1616 if (rte_is_same_ether_addr(addr, mac_addr)) {
1617 hns3_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
1619 hns3_err(hw, "failed to add mc mac addr, same addrs"
1620 "(%s) is added by the set_mc_mac_addr_list "
1626 ret = hns3_add_mc_addr(hw, mac_addr);
1628 hns3_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
1630 hns3_err(hw, "failed to add mc mac addr(%s), ret = %d",
1637 hns3_remove_mc_addr_common(struct hns3_hw *hw, struct rte_ether_addr *mac_addr)
1639 char mac_str[RTE_ETHER_ADDR_FMT_SIZE];
1642 ret = hns3_remove_mc_addr(hw, mac_addr);
1644 hns3_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
1646 hns3_err(hw, "failed to remove mc mac addr(%s), ret = %d",
1653 hns3_add_mac_addr(struct rte_eth_dev *dev, struct rte_ether_addr *mac_addr,
1654 uint32_t idx, __rte_unused uint32_t pool)
1656 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1657 char mac_str[RTE_ETHER_ADDR_FMT_SIZE];
1660 rte_spinlock_lock(&hw->lock);
1663 * In hns3 network engine adding UC and MC mac address with different
1664 * commands with firmware. We need to determine whether the input
1665 * address is a UC or a MC address to call different commands.
1666 * By the way, it is recommended calling the API function named
1667 * rte_eth_dev_set_mc_addr_list to set the MC mac address, because
1668 * using the rte_eth_dev_mac_addr_add API function to set MC mac address
1669 * may affect the specifications of UC mac addresses.
1671 if (rte_is_multicast_ether_addr(mac_addr))
1672 ret = hns3_add_mc_addr_common(hw, mac_addr);
1674 ret = hns3_add_uc_addr_common(hw, mac_addr);
1677 rte_spinlock_unlock(&hw->lock);
1678 hns3_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
1680 hns3_err(hw, "failed to add mac addr(%s), ret = %d", mac_str,
1686 hw->mac.default_addr_setted = true;
1687 rte_spinlock_unlock(&hw->lock);
1693 hns3_remove_uc_addr_common(struct hns3_hw *hw, struct rte_ether_addr *mac_addr)
1695 struct hns3_mac_vlan_tbl_entry_cmd req;
1696 char mac_str[RTE_ETHER_ADDR_FMT_SIZE];
1699 /* check if mac addr is valid */
1700 if (!rte_is_valid_assigned_ether_addr(mac_addr)) {
1701 hns3_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
1703 hns3_err(hw, "remove unicast mac addr err! addr(%s) invalid",
1708 memset(&req, 0, sizeof(req));
1709 hns3_set_bit(req.entry_type, HNS3_MAC_VLAN_BIT0_EN_B, 0);
1710 hns3_prepare_mac_addr(&req, mac_addr->addr_bytes, false);
1711 ret = hns3_remove_mac_vlan_tbl(hw, &req);
1712 if (ret == -ENOENT) /* mac addr isn't existent in the mac vlan table. */
1715 hns3_update_umv_space(hw, true);
1721 hns3_remove_mac_addr(struct rte_eth_dev *dev, uint32_t idx)
1723 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1724 /* index will be checked by upper level rte interface */
1725 struct rte_ether_addr *mac_addr = &dev->data->mac_addrs[idx];
1726 char mac_str[RTE_ETHER_ADDR_FMT_SIZE];
1729 rte_spinlock_lock(&hw->lock);
1731 if (rte_is_multicast_ether_addr(mac_addr))
1732 ret = hns3_remove_mc_addr_common(hw, mac_addr);
1734 ret = hns3_remove_uc_addr_common(hw, mac_addr);
1735 rte_spinlock_unlock(&hw->lock);
1737 hns3_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
1739 hns3_err(hw, "failed to remove mac addr(%s), ret = %d", mac_str,
1745 hns3_set_default_mac_addr(struct rte_eth_dev *dev,
1746 struct rte_ether_addr *mac_addr)
1748 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1749 struct rte_ether_addr *oaddr;
1750 char mac_str[RTE_ETHER_ADDR_FMT_SIZE];
1751 bool default_addr_setted;
1752 bool rm_succes = false;
1756 * It has been guaranteed that input parameter named mac_addr is valid
1757 * address in the rte layer of DPDK framework.
1759 oaddr = (struct rte_ether_addr *)hw->mac.mac_addr;
1760 default_addr_setted = hw->mac.default_addr_setted;
1761 if (default_addr_setted && !!rte_is_same_ether_addr(mac_addr, oaddr))
1764 rte_spinlock_lock(&hw->lock);
1765 if (default_addr_setted) {
1766 ret = hns3_remove_uc_addr_common(hw, oaddr);
1768 hns3_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
1770 hns3_warn(hw, "Remove old uc mac address(%s) fail: %d",
1777 ret = hns3_add_uc_addr_common(hw, mac_addr);
1779 hns3_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
1781 hns3_err(hw, "Failed to set mac addr(%s): %d", mac_str, ret);
1782 goto err_add_uc_addr;
1785 ret = hns3_pause_addr_cfg(hw, mac_addr->addr_bytes);
1787 hns3_err(hw, "Failed to configure mac pause address: %d", ret);
1788 goto err_pause_addr_cfg;
1791 rte_ether_addr_copy(mac_addr,
1792 (struct rte_ether_addr *)hw->mac.mac_addr);
1793 hw->mac.default_addr_setted = true;
1794 rte_spinlock_unlock(&hw->lock);
1799 ret_val = hns3_remove_uc_addr_common(hw, mac_addr);
1801 hns3_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
1804 "Failed to roll back to del setted mac addr(%s): %d",
1810 ret_val = hns3_add_uc_addr_common(hw, oaddr);
1812 hns3_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
1815 "Failed to restore old uc mac addr(%s): %d",
1817 hw->mac.default_addr_setted = false;
1820 rte_spinlock_unlock(&hw->lock);
1826 hns3_configure_all_mac_addr(struct hns3_adapter *hns, bool del)
1828 char mac_str[RTE_ETHER_ADDR_FMT_SIZE];
1829 struct hns3_hw *hw = &hns->hw;
1830 struct rte_ether_addr *addr;
1835 for (i = 0; i < HNS3_UC_MACADDR_NUM; i++) {
1836 addr = &hw->data->mac_addrs[i];
1837 if (rte_is_zero_ether_addr(addr))
1839 if (rte_is_multicast_ether_addr(addr))
1840 ret = del ? hns3_remove_mc_addr(hw, addr) :
1841 hns3_add_mc_addr(hw, addr);
1843 ret = del ? hns3_remove_uc_addr_common(hw, addr) :
1844 hns3_add_uc_addr_common(hw, addr);
1848 hns3_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
1850 hns3_err(hw, "failed to %s mac addr(%s) index:%d "
1851 "ret = %d.", del ? "remove" : "restore",
1859 hns3_update_desc_vfid(struct hns3_cmd_desc *desc, uint8_t vfid, bool clr)
1861 #define HNS3_VF_NUM_IN_FIRST_DESC 192
1865 if (vfid < HNS3_VF_NUM_IN_FIRST_DESC) {
1866 word_num = vfid / 32;
1867 bit_num = vfid % 32;
1869 desc[1].data[word_num] &=
1870 rte_cpu_to_le_32(~(1UL << bit_num));
1872 desc[1].data[word_num] |=
1873 rte_cpu_to_le_32(1UL << bit_num);
1875 word_num = (vfid - HNS3_VF_NUM_IN_FIRST_DESC) / 32;
1876 bit_num = vfid % 32;
1878 desc[2].data[word_num] &=
1879 rte_cpu_to_le_32(~(1UL << bit_num));
1881 desc[2].data[word_num] |=
1882 rte_cpu_to_le_32(1UL << bit_num);
1887 hns3_add_mc_addr(struct hns3_hw *hw, struct rte_ether_addr *mac_addr)
1889 struct hns3_mac_vlan_tbl_entry_cmd req;
1890 struct hns3_cmd_desc desc[3];
1891 char mac_str[RTE_ETHER_ADDR_FMT_SIZE];
1895 /* Check if mac addr is valid */
1896 if (!rte_is_multicast_ether_addr(mac_addr)) {
1897 hns3_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
1899 hns3_err(hw, "failed to add mc mac addr, addr(%s) invalid",
1904 memset(&req, 0, sizeof(req));
1905 hns3_set_bit(req.entry_type, HNS3_MAC_VLAN_BIT0_EN_B, 0);
1906 hns3_prepare_mac_addr(&req, mac_addr->addr_bytes, true);
1907 ret = hns3_lookup_mac_vlan_tbl(hw, &req, desc, true);
1909 /* This mac addr do not exist, add new entry for it */
1910 memset(desc[0].data, 0, sizeof(desc[0].data));
1911 memset(desc[1].data, 0, sizeof(desc[0].data));
1912 memset(desc[2].data, 0, sizeof(desc[0].data));
1916 * In current version VF is not supported when PF is driven by DPDK
1917 * driver, just need to configure parameters for PF vport.
1919 vf_id = HNS3_PF_FUNC_ID;
1920 hns3_update_desc_vfid(desc, vf_id, false);
1921 ret = hns3_add_mac_vlan_tbl(hw, &req, desc);
1924 hns3_err(hw, "mc mac vlan table is full");
1925 hns3_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
1927 hns3_err(hw, "failed to add mc mac addr(%s): %d", mac_str, ret);
1934 hns3_remove_mc_addr(struct hns3_hw *hw, struct rte_ether_addr *mac_addr)
1936 struct hns3_mac_vlan_tbl_entry_cmd req;
1937 struct hns3_cmd_desc desc[3];
1938 char mac_str[RTE_ETHER_ADDR_FMT_SIZE];
1942 /* Check if mac addr is valid */
1943 if (!rte_is_multicast_ether_addr(mac_addr)) {
1944 hns3_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
1946 hns3_err(hw, "Failed to rm mc mac addr, addr(%s) invalid",
1951 memset(&req, 0, sizeof(req));
1952 hns3_set_bit(req.entry_type, HNS3_MAC_VLAN_BIT0_EN_B, 0);
1953 hns3_prepare_mac_addr(&req, mac_addr->addr_bytes, true);
1954 ret = hns3_lookup_mac_vlan_tbl(hw, &req, desc, true);
1957 * This mac addr exist, remove this handle's VFID for it.
1958 * In current version VF is not supported when PF is driven by
1959 * DPDK driver, just need to configure parameters for PF vport.
1961 vf_id = HNS3_PF_FUNC_ID;
1962 hns3_update_desc_vfid(desc, vf_id, true);
1964 /* All the vfid is zero, so need to delete this entry */
1965 ret = hns3_remove_mac_vlan_tbl(hw, &req);
1966 } else if (ret == -ENOENT) {
1967 /* This mac addr doesn't exist. */
1972 hns3_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
1974 hns3_err(hw, "Failed to rm mc mac addr(%s): %d", mac_str, ret);
1981 hns3_set_mc_addr_chk_param(struct hns3_hw *hw,
1982 struct rte_ether_addr *mc_addr_set,
1983 uint32_t nb_mc_addr)
1985 char mac_str[RTE_ETHER_ADDR_FMT_SIZE];
1986 struct rte_ether_addr *addr;
1990 if (nb_mc_addr > HNS3_MC_MACADDR_NUM) {
1991 hns3_err(hw, "failed to set mc mac addr, nb_mc_addr(%u) "
1992 "invalid. valid range: 0~%d",
1993 nb_mc_addr, HNS3_MC_MACADDR_NUM);
1997 /* Check if input mac addresses are valid */
1998 for (i = 0; i < nb_mc_addr; i++) {
1999 addr = &mc_addr_set[i];
2000 if (!rte_is_multicast_ether_addr(addr)) {
2001 hns3_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
2004 "failed to set mc mac addr, addr(%s) invalid.",
2009 /* Check if there are duplicate addresses */
2010 for (j = i + 1; j < nb_mc_addr; j++) {
2011 if (rte_is_same_ether_addr(addr, &mc_addr_set[j])) {
2012 hns3_ether_format_addr(mac_str,
2013 RTE_ETHER_ADDR_FMT_SIZE,
2015 hns3_err(hw, "failed to set mc mac addr, "
2016 "addrs invalid. two same addrs(%s).",
2023 * Check if there are duplicate addresses between mac_addrs
2026 for (j = 0; j < HNS3_UC_MACADDR_NUM; j++) {
2027 if (rte_is_same_ether_addr(addr,
2028 &hw->data->mac_addrs[j])) {
2029 hns3_ether_format_addr(mac_str,
2030 RTE_ETHER_ADDR_FMT_SIZE,
2032 hns3_err(hw, "failed to set mc mac addr, "
2033 "addrs invalid. addrs(%s) has already "
2034 "configured in mac_addr add API",
2045 hns3_set_mc_addr_calc_addr(struct hns3_hw *hw,
2046 struct rte_ether_addr *mc_addr_set,
2048 struct rte_ether_addr *reserved_addr_list,
2049 int *reserved_addr_num,
2050 struct rte_ether_addr *add_addr_list,
2052 struct rte_ether_addr *rm_addr_list,
2055 struct rte_ether_addr *addr;
2056 int current_addr_num;
2057 int reserved_num = 0;
2065 /* Calculate the mc mac address list that should be removed */
2066 current_addr_num = hw->mc_addrs_num;
2067 for (i = 0; i < current_addr_num; i++) {
2068 addr = &hw->mc_addrs[i];
2070 for (j = 0; j < mc_addr_num; j++) {
2071 if (rte_is_same_ether_addr(addr, &mc_addr_set[j])) {
2078 rte_ether_addr_copy(addr, &rm_addr_list[rm_num]);
2081 rte_ether_addr_copy(addr,
2082 &reserved_addr_list[reserved_num]);
2087 /* Calculate the mc mac address list that should be added */
2088 for (i = 0; i < mc_addr_num; i++) {
2089 addr = &mc_addr_set[i];
2091 for (j = 0; j < current_addr_num; j++) {
2092 if (rte_is_same_ether_addr(addr, &hw->mc_addrs[j])) {
2099 rte_ether_addr_copy(addr, &add_addr_list[add_num]);
2104 /* Reorder the mc mac address list maintained by driver */
2105 for (i = 0; i < reserved_num; i++)
2106 rte_ether_addr_copy(&reserved_addr_list[i], &hw->mc_addrs[i]);
2108 for (i = 0; i < rm_num; i++) {
2109 num = reserved_num + i;
2110 rte_ether_addr_copy(&rm_addr_list[i], &hw->mc_addrs[num]);
2113 *reserved_addr_num = reserved_num;
2114 *add_addr_num = add_num;
2115 *rm_addr_num = rm_num;
2119 hns3_set_mc_mac_addr_list(struct rte_eth_dev *dev,
2120 struct rte_ether_addr *mc_addr_set,
2121 uint32_t nb_mc_addr)
2123 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2124 struct rte_ether_addr reserved_addr_list[HNS3_MC_MACADDR_NUM];
2125 struct rte_ether_addr add_addr_list[HNS3_MC_MACADDR_NUM];
2126 struct rte_ether_addr rm_addr_list[HNS3_MC_MACADDR_NUM];
2127 struct rte_ether_addr *addr;
2128 int reserved_addr_num;
2136 /* Check if input parameters are valid */
2137 ret = hns3_set_mc_addr_chk_param(hw, mc_addr_set, nb_mc_addr);
2141 rte_spinlock_lock(&hw->lock);
2144 * Calculate the mc mac address lists those should be removed and be
2145 * added, Reorder the mc mac address list maintained by driver.
2147 mc_addr_num = (int)nb_mc_addr;
2148 hns3_set_mc_addr_calc_addr(hw, mc_addr_set, mc_addr_num,
2149 reserved_addr_list, &reserved_addr_num,
2150 add_addr_list, &add_addr_num,
2151 rm_addr_list, &rm_addr_num);
2153 /* Remove mc mac addresses */
2154 for (i = 0; i < rm_addr_num; i++) {
2155 num = rm_addr_num - i - 1;
2156 addr = &rm_addr_list[num];
2157 ret = hns3_remove_mc_addr(hw, addr);
2159 rte_spinlock_unlock(&hw->lock);
2165 /* Add mc mac addresses */
2166 for (i = 0; i < add_addr_num; i++) {
2167 addr = &add_addr_list[i];
2168 ret = hns3_add_mc_addr(hw, addr);
2170 rte_spinlock_unlock(&hw->lock);
2174 num = reserved_addr_num + i;
2175 rte_ether_addr_copy(addr, &hw->mc_addrs[num]);
2178 rte_spinlock_unlock(&hw->lock);
2184 hns3_configure_all_mc_mac_addr(struct hns3_adapter *hns, bool del)
2186 char mac_str[RTE_ETHER_ADDR_FMT_SIZE];
2187 struct hns3_hw *hw = &hns->hw;
2188 struct rte_ether_addr *addr;
2193 for (i = 0; i < hw->mc_addrs_num; i++) {
2194 addr = &hw->mc_addrs[i];
2195 if (!rte_is_multicast_ether_addr(addr))
2198 ret = hns3_remove_mc_addr(hw, addr);
2200 ret = hns3_add_mc_addr(hw, addr);
2203 hns3_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
2205 hns3_dbg(hw, "%s mc mac addr: %s failed for pf: ret = %d",
2206 del ? "Remove" : "Restore", mac_str, ret);
2213 hns3_check_mq_mode(struct rte_eth_dev *dev)
2215 enum rte_eth_rx_mq_mode rx_mq_mode = dev->data->dev_conf.rxmode.mq_mode;
2216 enum rte_eth_tx_mq_mode tx_mq_mode = dev->data->dev_conf.txmode.mq_mode;
2217 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2218 struct hns3_pf *pf = HNS3_DEV_PRIVATE_TO_PF(dev->data->dev_private);
2219 struct rte_eth_dcb_rx_conf *dcb_rx_conf;
2220 struct rte_eth_dcb_tx_conf *dcb_tx_conf;
2225 if ((rx_mq_mode & ETH_MQ_RX_VMDQ_FLAG) ||
2226 (tx_mq_mode == ETH_MQ_TX_VMDQ_DCB ||
2227 tx_mq_mode == ETH_MQ_TX_VMDQ_ONLY)) {
2228 hns3_err(hw, "VMDQ is not supported, rx_mq_mode = %d, tx_mq_mode = %d.",
2229 rx_mq_mode, tx_mq_mode);
2233 dcb_rx_conf = &dev->data->dev_conf.rx_adv_conf.dcb_rx_conf;
2234 dcb_tx_conf = &dev->data->dev_conf.tx_adv_conf.dcb_tx_conf;
2235 if (rx_mq_mode & ETH_MQ_RX_DCB_FLAG) {
2236 if (dcb_rx_conf->nb_tcs > pf->tc_max) {
2237 hns3_err(hw, "nb_tcs(%u) > max_tc(%u) driver supported.",
2238 dcb_rx_conf->nb_tcs, pf->tc_max);
2242 if (!(dcb_rx_conf->nb_tcs == HNS3_4_TCS ||
2243 dcb_rx_conf->nb_tcs == HNS3_8_TCS)) {
2244 hns3_err(hw, "on ETH_MQ_RX_DCB_RSS mode, "
2245 "nb_tcs(%d) != %d or %d in rx direction.",
2246 dcb_rx_conf->nb_tcs, HNS3_4_TCS, HNS3_8_TCS);
2250 if (dcb_rx_conf->nb_tcs != dcb_tx_conf->nb_tcs) {
2251 hns3_err(hw, "num_tcs(%d) of tx is not equal to rx(%d)",
2252 dcb_tx_conf->nb_tcs, dcb_rx_conf->nb_tcs);
2256 for (i = 0; i < HNS3_MAX_USER_PRIO; i++) {
2257 if (dcb_rx_conf->dcb_tc[i] != dcb_tx_conf->dcb_tc[i]) {
2258 hns3_err(hw, "dcb_tc[%d] = %u in rx direction, "
2259 "is not equal to one in tx direction.",
2260 i, dcb_rx_conf->dcb_tc[i]);
2263 if (dcb_rx_conf->dcb_tc[i] > max_tc)
2264 max_tc = dcb_rx_conf->dcb_tc[i];
2267 num_tc = max_tc + 1;
2268 if (num_tc > dcb_rx_conf->nb_tcs) {
2269 hns3_err(hw, "max num_tc(%u) mapped > nb_tcs(%u)",
2270 num_tc, dcb_rx_conf->nb_tcs);
2279 hns3_check_dcb_cfg(struct rte_eth_dev *dev)
2281 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2283 if (!hns3_dev_dcb_supported(hw)) {
2284 hns3_err(hw, "this port does not support dcb configurations.");
2288 if (hw->current_fc_status == HNS3_FC_STATUS_MAC_PAUSE) {
2289 hns3_err(hw, "MAC pause enabled, cannot config dcb info.");
2297 hns3_bind_ring_with_vector(struct hns3_hw *hw, uint16_t vector_id, bool en,
2298 enum hns3_ring_type queue_type, uint16_t queue_id)
2300 struct hns3_cmd_desc desc;
2301 struct hns3_ctrl_vector_chain_cmd *req =
2302 (struct hns3_ctrl_vector_chain_cmd *)desc.data;
2303 enum hns3_opcode_type op;
2304 uint16_t tqp_type_and_id = 0;
2309 op = en ? HNS3_OPC_ADD_RING_TO_VECTOR : HNS3_OPC_DEL_RING_TO_VECTOR;
2310 hns3_cmd_setup_basic_desc(&desc, op, false);
2311 req->int_vector_id = hns3_get_field(vector_id, HNS3_TQP_INT_ID_L_M,
2312 HNS3_TQP_INT_ID_L_S);
2313 req->int_vector_id_h = hns3_get_field(vector_id, HNS3_TQP_INT_ID_H_M,
2314 HNS3_TQP_INT_ID_H_S);
2316 if (queue_type == HNS3_RING_TYPE_RX)
2317 gl = HNS3_RING_GL_RX;
2319 gl = HNS3_RING_GL_TX;
2323 hns3_set_field(tqp_type_and_id, HNS3_INT_TYPE_M, HNS3_INT_TYPE_S,
2325 hns3_set_field(tqp_type_and_id, HNS3_TQP_ID_M, HNS3_TQP_ID_S, queue_id);
2326 hns3_set_field(tqp_type_and_id, HNS3_INT_GL_IDX_M, HNS3_INT_GL_IDX_S,
2328 req->tqp_type_and_id[0] = rte_cpu_to_le_16(tqp_type_and_id);
2329 req->int_cause_num = 1;
2330 ret = hns3_cmd_send(hw, &desc, 1);
2332 hns3_err(hw, "%s TQP %u fail, vector_id = %u, ret = %d.",
2333 en ? "Map" : "Unmap", queue_id, vector_id, ret);
2341 hns3_init_ring_with_vector(struct hns3_hw *hw)
2348 * In hns3 network engine, vector 0 is always the misc interrupt of this
2349 * function, vector 1~N can be used respectively for the queues of the
2350 * function. Tx and Rx queues with the same number share the interrupt
2351 * vector. In the initialization clearing the all hardware mapping
2352 * relationship configurations between queues and interrupt vectors is
2353 * needed, so some error caused by the residual configurations, such as
2354 * the unexpected Tx interrupt, can be avoid.
2356 vec = hw->num_msi - 1; /* vector 0 for misc interrupt, not for queue */
2357 if (hw->intr.mapping_mode == HNS3_INTR_MAPPING_VEC_RSV_ONE)
2358 vec = vec - 1; /* the last interrupt is reserved */
2359 hw->intr_tqps_num = RTE_MIN(vec, hw->tqps_num);
2360 for (i = 0; i < hw->intr_tqps_num; i++) {
2362 * Set gap limiter/rate limiter/quanity limiter algorithm
2363 * configuration for interrupt coalesce of queue's interrupt.
2365 hns3_set_queue_intr_gl(hw, i, HNS3_RING_GL_RX,
2366 HNS3_TQP_INTR_GL_DEFAULT);
2367 hns3_set_queue_intr_gl(hw, i, HNS3_RING_GL_TX,
2368 HNS3_TQP_INTR_GL_DEFAULT);
2369 hns3_set_queue_intr_rl(hw, i, HNS3_TQP_INTR_RL_DEFAULT);
2371 * QL(quantity limiter) is not used currently, just set 0 to
2374 hns3_set_queue_intr_ql(hw, i, HNS3_TQP_INTR_QL_DEFAULT);
2376 ret = hns3_bind_ring_with_vector(hw, vec, false,
2377 HNS3_RING_TYPE_TX, i);
2379 PMD_INIT_LOG(ERR, "PF fail to unbind TX ring(%d) with "
2380 "vector: %u, ret=%d", i, vec, ret);
2384 ret = hns3_bind_ring_with_vector(hw, vec, false,
2385 HNS3_RING_TYPE_RX, i);
2387 PMD_INIT_LOG(ERR, "PF fail to unbind RX ring(%d) with "
2388 "vector: %u, ret=%d", i, vec, ret);
2397 hns3_refresh_mtu(struct rte_eth_dev *dev, struct rte_eth_conf *conf)
2399 struct hns3_adapter *hns = dev->data->dev_private;
2400 struct hns3_hw *hw = &hns->hw;
2401 uint32_t max_rx_pkt_len;
2405 if (!(conf->rxmode.offloads & DEV_RX_OFFLOAD_JUMBO_FRAME))
2409 * If jumbo frames are enabled, MTU needs to be refreshed
2410 * according to the maximum RX packet length.
2412 max_rx_pkt_len = conf->rxmode.max_rx_pkt_len;
2413 if (max_rx_pkt_len > HNS3_MAX_FRAME_LEN ||
2414 max_rx_pkt_len <= HNS3_DEFAULT_FRAME_LEN) {
2415 hns3_err(hw, "maximum Rx packet length must be greater than %u "
2416 "and no more than %u when jumbo frame enabled.",
2417 (uint16_t)HNS3_DEFAULT_FRAME_LEN,
2418 (uint16_t)HNS3_MAX_FRAME_LEN);
2422 mtu = (uint16_t)HNS3_PKTLEN_TO_MTU(max_rx_pkt_len);
2423 ret = hns3_dev_mtu_set(dev, mtu);
2426 dev->data->mtu = mtu;
2432 hns3_dev_configure(struct rte_eth_dev *dev)
2434 struct hns3_adapter *hns = dev->data->dev_private;
2435 struct rte_eth_conf *conf = &dev->data->dev_conf;
2436 enum rte_eth_rx_mq_mode mq_mode = conf->rxmode.mq_mode;
2437 struct hns3_hw *hw = &hns->hw;
2438 uint16_t nb_rx_q = dev->data->nb_rx_queues;
2439 uint16_t nb_tx_q = dev->data->nb_tx_queues;
2440 struct rte_eth_rss_conf rss_conf;
2444 hw->cfg_max_queues = RTE_MAX(nb_rx_q, nb_tx_q);
2447 * Some versions of hardware network engine does not support
2448 * individually enable/disable/reset the Tx or Rx queue. These devices
2449 * must enable/disable/reset Tx and Rx queues at the same time. When the
2450 * numbers of Tx queues allocated by upper applications are not equal to
2451 * the numbers of Rx queues, driver needs to setup fake Tx or Rx queues
2452 * to adjust numbers of Tx/Rx queues. otherwise, network engine can not
2453 * work as usual. But these fake queues are imperceptible, and can not
2454 * be used by upper applications.
2456 if (!hns3_dev_indep_txrx_supported(hw)) {
2457 ret = hns3_set_fake_rx_or_tx_queues(dev, nb_rx_q, nb_tx_q);
2459 hns3_err(hw, "fail to set Rx/Tx fake queues, ret = %d.",
2465 hw->adapter_state = HNS3_NIC_CONFIGURING;
2466 ret = hns3_check_mq_mode(dev);
2470 if ((uint32_t)mq_mode & ETH_MQ_RX_DCB_FLAG) {
2471 ret = hns3_check_dcb_cfg(dev);
2476 /* When RSS is not configured, redirect the packet queue 0 */
2477 if ((uint32_t)mq_mode & ETH_MQ_RX_RSS_FLAG) {
2478 conf->rxmode.offloads |= DEV_RX_OFFLOAD_RSS_HASH;
2479 rss_conf = conf->rx_adv_conf.rss_conf;
2480 hw->rss_dis_flag = false;
2481 ret = hns3_dev_rss_hash_update(dev, &rss_conf);
2486 ret = hns3_refresh_mtu(dev, conf);
2490 ret = hns3_mbuf_dyn_rx_timestamp_register(dev, conf);
2494 ret = hns3_dev_configure_vlan(dev);
2498 /* config hardware GRO */
2499 gro_en = conf->rxmode.offloads & DEV_RX_OFFLOAD_TCP_LRO ? true : false;
2500 ret = hns3_config_gro(hw, gro_en);
2504 hns3_init_rx_ptype_tble(dev);
2505 hw->adapter_state = HNS3_NIC_CONFIGURED;
2510 (void)hns3_set_fake_rx_or_tx_queues(dev, 0, 0);
2511 hw->adapter_state = HNS3_NIC_INITIALIZED;
2517 hns3_set_mac_mtu(struct hns3_hw *hw, uint16_t new_mps)
2519 struct hns3_config_max_frm_size_cmd *req;
2520 struct hns3_cmd_desc desc;
2522 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_CONFIG_MAX_FRM_SIZE, false);
2524 req = (struct hns3_config_max_frm_size_cmd *)desc.data;
2525 req->max_frm_size = rte_cpu_to_le_16(new_mps);
2526 req->min_frm_size = RTE_ETHER_MIN_LEN;
2528 return hns3_cmd_send(hw, &desc, 1);
2532 hns3_config_mtu(struct hns3_hw *hw, uint16_t mps)
2534 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
2535 uint16_t original_mps = hns->pf.mps;
2539 ret = hns3_set_mac_mtu(hw, mps);
2541 hns3_err(hw, "failed to set mtu, ret = %d", ret);
2546 ret = hns3_buffer_alloc(hw);
2548 hns3_err(hw, "failed to allocate buffer, ret = %d", ret);
2555 err = hns3_set_mac_mtu(hw, original_mps);
2557 hns3_err(hw, "fail to rollback MTU, err = %d", err);
2560 hns->pf.mps = original_mps;
2566 hns3_dev_mtu_set(struct rte_eth_dev *dev, uint16_t mtu)
2568 struct hns3_adapter *hns = dev->data->dev_private;
2569 uint32_t frame_size = mtu + HNS3_ETH_OVERHEAD;
2570 struct hns3_hw *hw = &hns->hw;
2571 bool is_jumbo_frame;
2574 if (dev->data->dev_started) {
2575 hns3_err(hw, "Failed to set mtu, port %u must be stopped "
2576 "before configuration", dev->data->port_id);
2580 rte_spinlock_lock(&hw->lock);
2581 is_jumbo_frame = frame_size > HNS3_DEFAULT_FRAME_LEN ? true : false;
2582 frame_size = RTE_MAX(frame_size, HNS3_DEFAULT_FRAME_LEN);
2585 * Maximum value of frame_size is HNS3_MAX_FRAME_LEN, so it can safely
2586 * assign to "uint16_t" type variable.
2588 ret = hns3_config_mtu(hw, (uint16_t)frame_size);
2590 rte_spinlock_unlock(&hw->lock);
2591 hns3_err(hw, "Failed to set mtu, port %u mtu %u: %d",
2592 dev->data->port_id, mtu, ret);
2597 dev->data->dev_conf.rxmode.offloads |=
2598 DEV_RX_OFFLOAD_JUMBO_FRAME;
2600 dev->data->dev_conf.rxmode.offloads &=
2601 ~DEV_RX_OFFLOAD_JUMBO_FRAME;
2602 dev->data->dev_conf.rxmode.max_rx_pkt_len = frame_size;
2603 rte_spinlock_unlock(&hw->lock);
2609 hns3_get_copper_port_speed_capa(uint32_t supported_speed)
2611 uint32_t speed_capa = 0;
2613 if (supported_speed & HNS3_PHY_LINK_SPEED_10M_HD_BIT)
2614 speed_capa |= ETH_LINK_SPEED_10M_HD;
2615 if (supported_speed & HNS3_PHY_LINK_SPEED_10M_BIT)
2616 speed_capa |= ETH_LINK_SPEED_10M;
2617 if (supported_speed & HNS3_PHY_LINK_SPEED_100M_HD_BIT)
2618 speed_capa |= ETH_LINK_SPEED_100M_HD;
2619 if (supported_speed & HNS3_PHY_LINK_SPEED_100M_BIT)
2620 speed_capa |= ETH_LINK_SPEED_100M;
2621 if (supported_speed & HNS3_PHY_LINK_SPEED_1000M_BIT)
2622 speed_capa |= ETH_LINK_SPEED_1G;
2628 hns3_get_firber_port_speed_capa(uint32_t supported_speed)
2630 uint32_t speed_capa = 0;
2632 if (supported_speed & HNS3_FIBER_LINK_SPEED_1G_BIT)
2633 speed_capa |= ETH_LINK_SPEED_1G;
2634 if (supported_speed & HNS3_FIBER_LINK_SPEED_10G_BIT)
2635 speed_capa |= ETH_LINK_SPEED_10G;
2636 if (supported_speed & HNS3_FIBER_LINK_SPEED_25G_BIT)
2637 speed_capa |= ETH_LINK_SPEED_25G;
2638 if (supported_speed & HNS3_FIBER_LINK_SPEED_40G_BIT)
2639 speed_capa |= ETH_LINK_SPEED_40G;
2640 if (supported_speed & HNS3_FIBER_LINK_SPEED_50G_BIT)
2641 speed_capa |= ETH_LINK_SPEED_50G;
2642 if (supported_speed & HNS3_FIBER_LINK_SPEED_100G_BIT)
2643 speed_capa |= ETH_LINK_SPEED_100G;
2644 if (supported_speed & HNS3_FIBER_LINK_SPEED_200G_BIT)
2645 speed_capa |= ETH_LINK_SPEED_200G;
2651 hns3_get_speed_capa(struct hns3_hw *hw)
2653 struct hns3_mac *mac = &hw->mac;
2654 uint32_t speed_capa;
2656 if (mac->media_type == HNS3_MEDIA_TYPE_COPPER)
2658 hns3_get_copper_port_speed_capa(mac->supported_speed);
2661 hns3_get_firber_port_speed_capa(mac->supported_speed);
2663 if (mac->support_autoneg == 0)
2664 speed_capa |= ETH_LINK_SPEED_FIXED;
2670 hns3_dev_infos_get(struct rte_eth_dev *eth_dev, struct rte_eth_dev_info *info)
2672 struct hns3_adapter *hns = eth_dev->data->dev_private;
2673 struct hns3_hw *hw = &hns->hw;
2674 uint16_t queue_num = hw->tqps_num;
2677 * In interrupt mode, 'max_rx_queues' is set based on the number of
2678 * MSI-X interrupt resources of the hardware.
2680 if (hw->data->dev_conf.intr_conf.rxq == 1)
2681 queue_num = hw->intr_tqps_num;
2683 info->max_rx_queues = queue_num;
2684 info->max_tx_queues = hw->tqps_num;
2685 info->max_rx_pktlen = HNS3_MAX_FRAME_LEN; /* CRC included */
2686 info->min_rx_bufsize = HNS3_MIN_BD_BUF_SIZE;
2687 info->max_mac_addrs = HNS3_UC_MACADDR_NUM;
2688 info->max_mtu = info->max_rx_pktlen - HNS3_ETH_OVERHEAD;
2689 info->max_lro_pkt_size = HNS3_MAX_LRO_SIZE;
2690 info->rx_offload_capa = (DEV_RX_OFFLOAD_IPV4_CKSUM |
2691 DEV_RX_OFFLOAD_TCP_CKSUM |
2692 DEV_RX_OFFLOAD_UDP_CKSUM |
2693 DEV_RX_OFFLOAD_SCTP_CKSUM |
2694 DEV_RX_OFFLOAD_OUTER_IPV4_CKSUM |
2695 DEV_RX_OFFLOAD_OUTER_UDP_CKSUM |
2696 DEV_RX_OFFLOAD_KEEP_CRC |
2697 DEV_RX_OFFLOAD_SCATTER |
2698 DEV_RX_OFFLOAD_VLAN_STRIP |
2699 DEV_RX_OFFLOAD_VLAN_FILTER |
2700 DEV_RX_OFFLOAD_JUMBO_FRAME |
2701 DEV_RX_OFFLOAD_RSS_HASH |
2702 DEV_RX_OFFLOAD_TCP_LRO);
2703 info->tx_offload_capa = (DEV_TX_OFFLOAD_OUTER_IPV4_CKSUM |
2704 DEV_TX_OFFLOAD_IPV4_CKSUM |
2705 DEV_TX_OFFLOAD_TCP_CKSUM |
2706 DEV_TX_OFFLOAD_UDP_CKSUM |
2707 DEV_TX_OFFLOAD_SCTP_CKSUM |
2708 DEV_TX_OFFLOAD_MULTI_SEGS |
2709 DEV_TX_OFFLOAD_TCP_TSO |
2710 DEV_TX_OFFLOAD_VXLAN_TNL_TSO |
2711 DEV_TX_OFFLOAD_GRE_TNL_TSO |
2712 DEV_TX_OFFLOAD_GENEVE_TNL_TSO |
2713 DEV_TX_OFFLOAD_MBUF_FAST_FREE |
2714 hns3_txvlan_cap_get(hw));
2716 if (hns3_dev_outer_udp_cksum_supported(hw))
2717 info->tx_offload_capa |= DEV_TX_OFFLOAD_OUTER_UDP_CKSUM;
2719 if (hns3_dev_indep_txrx_supported(hw))
2720 info->dev_capa = RTE_ETH_DEV_CAPA_RUNTIME_RX_QUEUE_SETUP |
2721 RTE_ETH_DEV_CAPA_RUNTIME_TX_QUEUE_SETUP;
2723 if (hns3_dev_ptp_supported(hw))
2724 info->rx_offload_capa |= DEV_RX_OFFLOAD_TIMESTAMP;
2726 info->rx_desc_lim = (struct rte_eth_desc_lim) {
2727 .nb_max = HNS3_MAX_RING_DESC,
2728 .nb_min = HNS3_MIN_RING_DESC,
2729 .nb_align = HNS3_ALIGN_RING_DESC,
2732 info->tx_desc_lim = (struct rte_eth_desc_lim) {
2733 .nb_max = HNS3_MAX_RING_DESC,
2734 .nb_min = HNS3_MIN_RING_DESC,
2735 .nb_align = HNS3_ALIGN_RING_DESC,
2736 .nb_seg_max = HNS3_MAX_TSO_BD_PER_PKT,
2737 .nb_mtu_seg_max = hw->max_non_tso_bd_num,
2740 info->speed_capa = hns3_get_speed_capa(hw);
2741 info->default_rxconf = (struct rte_eth_rxconf) {
2742 .rx_free_thresh = HNS3_DEFAULT_RX_FREE_THRESH,
2744 * If there are no available Rx buffer descriptors, incoming
2745 * packets are always dropped by hardware based on hns3 network
2751 info->default_txconf = (struct rte_eth_txconf) {
2752 .tx_rs_thresh = HNS3_DEFAULT_TX_RS_THRESH,
2756 info->vmdq_queue_num = 0;
2758 info->reta_size = hw->rss_ind_tbl_size;
2759 info->hash_key_size = HNS3_RSS_KEY_SIZE;
2760 info->flow_type_rss_offloads = HNS3_ETH_RSS_SUPPORT;
2762 info->default_rxportconf.burst_size = HNS3_DEFAULT_PORT_CONF_BURST_SIZE;
2763 info->default_txportconf.burst_size = HNS3_DEFAULT_PORT_CONF_BURST_SIZE;
2764 info->default_rxportconf.nb_queues = HNS3_DEFAULT_PORT_CONF_QUEUES_NUM;
2765 info->default_txportconf.nb_queues = HNS3_DEFAULT_PORT_CONF_QUEUES_NUM;
2766 info->default_rxportconf.ring_size = HNS3_DEFAULT_RING_DESC;
2767 info->default_txportconf.ring_size = HNS3_DEFAULT_RING_DESC;
2773 hns3_fw_version_get(struct rte_eth_dev *eth_dev, char *fw_version,
2776 struct hns3_adapter *hns = eth_dev->data->dev_private;
2777 struct hns3_hw *hw = &hns->hw;
2778 uint32_t version = hw->fw_version;
2781 ret = snprintf(fw_version, fw_size, "%lu.%lu.%lu.%lu",
2782 hns3_get_field(version, HNS3_FW_VERSION_BYTE3_M,
2783 HNS3_FW_VERSION_BYTE3_S),
2784 hns3_get_field(version, HNS3_FW_VERSION_BYTE2_M,
2785 HNS3_FW_VERSION_BYTE2_S),
2786 hns3_get_field(version, HNS3_FW_VERSION_BYTE1_M,
2787 HNS3_FW_VERSION_BYTE1_S),
2788 hns3_get_field(version, HNS3_FW_VERSION_BYTE0_M,
2789 HNS3_FW_VERSION_BYTE0_S));
2790 ret += 1; /* add the size of '\0' */
2791 if (fw_size < (uint32_t)ret)
2798 hns3_update_port_link_info(struct rte_eth_dev *eth_dev)
2800 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(eth_dev->data->dev_private);
2803 (void)hns3_update_link_status(hw);
2805 ret = hns3_update_link_info(eth_dev);
2807 hw->mac.link_status = ETH_LINK_DOWN;
2813 hns3_setup_linkstatus(struct rte_eth_dev *eth_dev,
2814 struct rte_eth_link *new_link)
2816 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(eth_dev->data->dev_private);
2817 struct hns3_mac *mac = &hw->mac;
2819 switch (mac->link_speed) {
2820 case ETH_SPEED_NUM_10M:
2821 case ETH_SPEED_NUM_100M:
2822 case ETH_SPEED_NUM_1G:
2823 case ETH_SPEED_NUM_10G:
2824 case ETH_SPEED_NUM_25G:
2825 case ETH_SPEED_NUM_40G:
2826 case ETH_SPEED_NUM_50G:
2827 case ETH_SPEED_NUM_100G:
2828 case ETH_SPEED_NUM_200G:
2829 new_link->link_speed = mac->link_speed;
2832 if (mac->link_status)
2833 new_link->link_speed = ETH_SPEED_NUM_UNKNOWN;
2835 new_link->link_speed = ETH_SPEED_NUM_NONE;
2839 new_link->link_duplex = mac->link_duplex;
2840 new_link->link_status = mac->link_status ? ETH_LINK_UP : ETH_LINK_DOWN;
2841 new_link->link_autoneg = mac->link_autoneg;
2845 hns3_dev_link_update(struct rte_eth_dev *eth_dev, int wait_to_complete)
2847 #define HNS3_LINK_CHECK_INTERVAL 100 /* 100ms */
2848 #define HNS3_MAX_LINK_CHECK_TIMES 20 /* 2s (100 * 20ms) in total */
2850 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(eth_dev->data->dev_private);
2851 uint32_t retry_cnt = HNS3_MAX_LINK_CHECK_TIMES;
2852 struct hns3_mac *mac = &hw->mac;
2853 struct rte_eth_link new_link;
2857 ret = hns3_update_port_link_info(eth_dev);
2859 hns3_err(hw, "failed to get port link info, ret = %d.",
2864 if (!wait_to_complete || mac->link_status == ETH_LINK_UP)
2867 rte_delay_ms(HNS3_LINK_CHECK_INTERVAL);
2868 } while (retry_cnt--);
2870 memset(&new_link, 0, sizeof(new_link));
2871 hns3_setup_linkstatus(eth_dev, &new_link);
2873 return rte_eth_linkstatus_set(eth_dev, &new_link);
2877 hns3_parse_func_status(struct hns3_hw *hw, struct hns3_func_status_cmd *status)
2879 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
2880 struct hns3_pf *pf = &hns->pf;
2882 if (!(status->pf_state & HNS3_PF_STATE_DONE))
2885 pf->is_main_pf = (status->pf_state & HNS3_PF_STATE_MAIN) ? true : false;
2891 hns3_query_function_status(struct hns3_hw *hw)
2893 #define HNS3_QUERY_MAX_CNT 10
2894 #define HNS3_QUERY_SLEEP_MSCOEND 1
2895 struct hns3_func_status_cmd *req;
2896 struct hns3_cmd_desc desc;
2900 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_QUERY_FUNC_STATUS, true);
2901 req = (struct hns3_func_status_cmd *)desc.data;
2904 ret = hns3_cmd_send(hw, &desc, 1);
2906 PMD_INIT_LOG(ERR, "query function status failed %d",
2911 /* Check pf reset is done */
2915 rte_delay_ms(HNS3_QUERY_SLEEP_MSCOEND);
2916 } while (timeout++ < HNS3_QUERY_MAX_CNT);
2918 return hns3_parse_func_status(hw, req);
2922 hns3_get_pf_max_tqp_num(struct hns3_hw *hw)
2924 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
2925 struct hns3_pf *pf = &hns->pf;
2927 if (pf->tqp_config_mode == HNS3_FLEX_MAX_TQP_NUM_MODE) {
2929 * The total_tqps_num obtained from firmware is maximum tqp
2930 * numbers of this port, which should be used for PF and VFs.
2931 * There is no need for pf to have so many tqp numbers in
2932 * most cases. RTE_LIBRTE_HNS3_MAX_TQP_NUM_PER_PF,
2933 * coming from config file, is assigned to maximum queue number
2934 * for the PF of this port by user. So users can modify the
2935 * maximum queue number of PF according to their own application
2936 * scenarios, which is more flexible to use. In addition, many
2937 * memories can be saved due to allocating queue statistics
2938 * room according to the actual number of queues required. The
2939 * maximum queue number of PF for network engine with
2940 * revision_id greater than 0x30 is assigned by config file.
2942 if (RTE_LIBRTE_HNS3_MAX_TQP_NUM_PER_PF <= 0) {
2943 hns3_err(hw, "RTE_LIBRTE_HNS3_MAX_TQP_NUM_PER_PF(%d) "
2944 "must be greater than 0.",
2945 RTE_LIBRTE_HNS3_MAX_TQP_NUM_PER_PF);
2949 hw->tqps_num = RTE_MIN(RTE_LIBRTE_HNS3_MAX_TQP_NUM_PER_PF,
2950 hw->total_tqps_num);
2953 * Due to the limitation on the number of PF interrupts
2954 * available, the maximum queue number assigned to PF on
2955 * the network engine with revision_id 0x21 is 64.
2957 hw->tqps_num = RTE_MIN(hw->total_tqps_num,
2958 HNS3_MAX_TQP_NUM_HIP08_PF);
2965 hns3_query_pf_resource(struct hns3_hw *hw)
2967 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
2968 struct hns3_pf *pf = &hns->pf;
2969 struct hns3_pf_res_cmd *req;
2970 struct hns3_cmd_desc desc;
2973 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_QUERY_PF_RSRC, true);
2974 ret = hns3_cmd_send(hw, &desc, 1);
2976 PMD_INIT_LOG(ERR, "query pf resource failed %d", ret);
2980 req = (struct hns3_pf_res_cmd *)desc.data;
2981 hw->total_tqps_num = rte_le_to_cpu_16(req->tqp_num) +
2982 rte_le_to_cpu_16(req->ext_tqp_num);
2983 ret = hns3_get_pf_max_tqp_num(hw);
2987 pf->pkt_buf_size = rte_le_to_cpu_16(req->buf_size) << HNS3_BUF_UNIT_S;
2988 pf->func_num = rte_le_to_cpu_16(req->pf_own_fun_number);
2990 if (req->tx_buf_size)
2992 rte_le_to_cpu_16(req->tx_buf_size) << HNS3_BUF_UNIT_S;
2994 pf->tx_buf_size = HNS3_DEFAULT_TX_BUF;
2996 pf->tx_buf_size = roundup(pf->tx_buf_size, HNS3_BUF_SIZE_UNIT);
2998 if (req->dv_buf_size)
3000 rte_le_to_cpu_16(req->dv_buf_size) << HNS3_BUF_UNIT_S;
3002 pf->dv_buf_size = HNS3_DEFAULT_DV;
3004 pf->dv_buf_size = roundup(pf->dv_buf_size, HNS3_BUF_SIZE_UNIT);
3007 hns3_get_field(rte_le_to_cpu_16(req->nic_pf_intr_vector_number),
3008 HNS3_PF_VEC_NUM_M, HNS3_PF_VEC_NUM_S);
3014 hns3_parse_cfg(struct hns3_cfg *cfg, struct hns3_cmd_desc *desc)
3016 struct hns3_cfg_param_cmd *req;
3017 uint64_t mac_addr_tmp_high;
3018 uint8_t ext_rss_size_max;
3019 uint64_t mac_addr_tmp;
3022 req = (struct hns3_cfg_param_cmd *)desc[0].data;
3024 /* get the configuration */
3025 cfg->vmdq_vport_num = hns3_get_field(rte_le_to_cpu_32(req->param[0]),
3026 HNS3_CFG_VMDQ_M, HNS3_CFG_VMDQ_S);
3027 cfg->tc_num = hns3_get_field(rte_le_to_cpu_32(req->param[0]),
3028 HNS3_CFG_TC_NUM_M, HNS3_CFG_TC_NUM_S);
3029 cfg->tqp_desc_num = hns3_get_field(rte_le_to_cpu_32(req->param[0]),
3030 HNS3_CFG_TQP_DESC_N_M,
3031 HNS3_CFG_TQP_DESC_N_S);
3033 cfg->phy_addr = hns3_get_field(rte_le_to_cpu_32(req->param[1]),
3034 HNS3_CFG_PHY_ADDR_M,
3035 HNS3_CFG_PHY_ADDR_S);
3036 cfg->media_type = hns3_get_field(rte_le_to_cpu_32(req->param[1]),
3037 HNS3_CFG_MEDIA_TP_M,
3038 HNS3_CFG_MEDIA_TP_S);
3039 cfg->rx_buf_len = hns3_get_field(rte_le_to_cpu_32(req->param[1]),
3040 HNS3_CFG_RX_BUF_LEN_M,
3041 HNS3_CFG_RX_BUF_LEN_S);
3042 /* get mac address */
3043 mac_addr_tmp = rte_le_to_cpu_32(req->param[2]);
3044 mac_addr_tmp_high = hns3_get_field(rte_le_to_cpu_32(req->param[3]),
3045 HNS3_CFG_MAC_ADDR_H_M,
3046 HNS3_CFG_MAC_ADDR_H_S);
3048 mac_addr_tmp |= (mac_addr_tmp_high << 31) << 1;
3050 cfg->default_speed = hns3_get_field(rte_le_to_cpu_32(req->param[3]),
3051 HNS3_CFG_DEFAULT_SPEED_M,
3052 HNS3_CFG_DEFAULT_SPEED_S);
3053 cfg->rss_size_max = hns3_get_field(rte_le_to_cpu_32(req->param[3]),
3054 HNS3_CFG_RSS_SIZE_M,
3055 HNS3_CFG_RSS_SIZE_S);
3057 for (i = 0; i < RTE_ETHER_ADDR_LEN; i++)
3058 cfg->mac_addr[i] = (mac_addr_tmp >> (8 * i)) & 0xff;
3060 req = (struct hns3_cfg_param_cmd *)desc[1].data;
3061 cfg->numa_node_map = rte_le_to_cpu_32(req->param[0]);
3063 cfg->speed_ability = hns3_get_field(rte_le_to_cpu_32(req->param[1]),
3064 HNS3_CFG_SPEED_ABILITY_M,
3065 HNS3_CFG_SPEED_ABILITY_S);
3066 cfg->umv_space = hns3_get_field(rte_le_to_cpu_32(req->param[1]),
3067 HNS3_CFG_UMV_TBL_SPACE_M,
3068 HNS3_CFG_UMV_TBL_SPACE_S);
3069 if (!cfg->umv_space)
3070 cfg->umv_space = HNS3_DEFAULT_UMV_SPACE_PER_PF;
3072 ext_rss_size_max = hns3_get_field(rte_le_to_cpu_32(req->param[2]),
3073 HNS3_CFG_EXT_RSS_SIZE_M,
3074 HNS3_CFG_EXT_RSS_SIZE_S);
3077 * Field ext_rss_size_max obtained from firmware will be more flexible
3078 * for future changes and expansions, which is an exponent of 2, instead
3079 * of reading out directly. If this field is not zero, hns3 PF PMD
3080 * driver uses it as rss_size_max under one TC. Device, whose revision
3081 * id is greater than or equal to PCI_REVISION_ID_HIP09_A, obtains the
3082 * maximum number of queues supported under a TC through this field.
3084 if (ext_rss_size_max)
3085 cfg->rss_size_max = 1U << ext_rss_size_max;
3088 /* hns3_get_board_cfg: query the static parameter from NCL_config file in flash
3089 * @hw: pointer to struct hns3_hw
3090 * @hcfg: the config structure to be getted
3093 hns3_get_board_cfg(struct hns3_hw *hw, struct hns3_cfg *hcfg)
3095 struct hns3_cmd_desc desc[HNS3_PF_CFG_DESC_NUM];
3096 struct hns3_cfg_param_cmd *req;
3101 for (i = 0; i < HNS3_PF_CFG_DESC_NUM; i++) {
3103 req = (struct hns3_cfg_param_cmd *)desc[i].data;
3104 hns3_cmd_setup_basic_desc(&desc[i], HNS3_OPC_GET_CFG_PARAM,
3106 hns3_set_field(offset, HNS3_CFG_OFFSET_M, HNS3_CFG_OFFSET_S,
3107 i * HNS3_CFG_RD_LEN_BYTES);
3108 /* Len should be divided by 4 when send to hardware */
3109 hns3_set_field(offset, HNS3_CFG_RD_LEN_M, HNS3_CFG_RD_LEN_S,
3110 HNS3_CFG_RD_LEN_BYTES / HNS3_CFG_RD_LEN_UNIT);
3111 req->offset = rte_cpu_to_le_32(offset);
3114 ret = hns3_cmd_send(hw, desc, HNS3_PF_CFG_DESC_NUM);
3116 PMD_INIT_LOG(ERR, "get config failed %d.", ret);
3120 hns3_parse_cfg(hcfg, desc);
3126 hns3_parse_speed(int speed_cmd, uint32_t *speed)
3128 switch (speed_cmd) {
3129 case HNS3_CFG_SPEED_10M:
3130 *speed = ETH_SPEED_NUM_10M;
3132 case HNS3_CFG_SPEED_100M:
3133 *speed = ETH_SPEED_NUM_100M;
3135 case HNS3_CFG_SPEED_1G:
3136 *speed = ETH_SPEED_NUM_1G;
3138 case HNS3_CFG_SPEED_10G:
3139 *speed = ETH_SPEED_NUM_10G;
3141 case HNS3_CFG_SPEED_25G:
3142 *speed = ETH_SPEED_NUM_25G;
3144 case HNS3_CFG_SPEED_40G:
3145 *speed = ETH_SPEED_NUM_40G;
3147 case HNS3_CFG_SPEED_50G:
3148 *speed = ETH_SPEED_NUM_50G;
3150 case HNS3_CFG_SPEED_100G:
3151 *speed = ETH_SPEED_NUM_100G;
3153 case HNS3_CFG_SPEED_200G:
3154 *speed = ETH_SPEED_NUM_200G;
3164 hns3_set_default_dev_specifications(struct hns3_hw *hw)
3166 hw->max_non_tso_bd_num = HNS3_MAX_NON_TSO_BD_PER_PKT;
3167 hw->rss_ind_tbl_size = HNS3_RSS_IND_TBL_SIZE;
3168 hw->rss_key_size = HNS3_RSS_KEY_SIZE;
3169 hw->max_tm_rate = HNS3_ETHER_MAX_RATE;
3170 hw->intr.int_ql_max = HNS3_INTR_QL_NONE;
3174 hns3_parse_dev_specifications(struct hns3_hw *hw, struct hns3_cmd_desc *desc)
3176 struct hns3_dev_specs_0_cmd *req0;
3178 req0 = (struct hns3_dev_specs_0_cmd *)desc[0].data;
3180 hw->max_non_tso_bd_num = req0->max_non_tso_bd_num;
3181 hw->rss_ind_tbl_size = rte_le_to_cpu_16(req0->rss_ind_tbl_size);
3182 hw->rss_key_size = rte_le_to_cpu_16(req0->rss_key_size);
3183 hw->max_tm_rate = rte_le_to_cpu_32(req0->max_tm_rate);
3184 hw->intr.int_ql_max = rte_le_to_cpu_16(req0->intr_ql_max);
3188 hns3_check_dev_specifications(struct hns3_hw *hw)
3190 if (hw->rss_ind_tbl_size == 0 ||
3191 hw->rss_ind_tbl_size > HNS3_RSS_IND_TBL_SIZE_MAX) {
3192 hns3_err(hw, "the size of hash lookup table configured (%u)"
3193 " exceeds the maximum(%u)", hw->rss_ind_tbl_size,
3194 HNS3_RSS_IND_TBL_SIZE_MAX);
3202 hns3_query_dev_specifications(struct hns3_hw *hw)
3204 struct hns3_cmd_desc desc[HNS3_QUERY_DEV_SPECS_BD_NUM];
3208 for (i = 0; i < HNS3_QUERY_DEV_SPECS_BD_NUM - 1; i++) {
3209 hns3_cmd_setup_basic_desc(&desc[i], HNS3_OPC_QUERY_DEV_SPECS,
3211 desc[i].flag |= rte_cpu_to_le_16(HNS3_CMD_FLAG_NEXT);
3213 hns3_cmd_setup_basic_desc(&desc[i], HNS3_OPC_QUERY_DEV_SPECS, true);
3215 ret = hns3_cmd_send(hw, desc, HNS3_QUERY_DEV_SPECS_BD_NUM);
3219 hns3_parse_dev_specifications(hw, desc);
3221 return hns3_check_dev_specifications(hw);
3225 hns3_get_capability(struct hns3_hw *hw)
3227 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
3228 struct rte_pci_device *pci_dev;
3229 struct hns3_pf *pf = &hns->pf;
3230 struct rte_eth_dev *eth_dev;
3235 eth_dev = &rte_eth_devices[hw->data->port_id];
3236 pci_dev = RTE_ETH_DEV_TO_PCI(eth_dev);
3237 device_id = pci_dev->id.device_id;
3239 if (device_id == HNS3_DEV_ID_25GE_RDMA ||
3240 device_id == HNS3_DEV_ID_50GE_RDMA ||
3241 device_id == HNS3_DEV_ID_100G_RDMA_MACSEC ||
3242 device_id == HNS3_DEV_ID_200G_RDMA)
3243 hns3_set_bit(hw->capability, HNS3_DEV_SUPPORT_DCB_B, 1);
3245 /* Get PCI revision id */
3246 ret = rte_pci_read_config(pci_dev, &revision, HNS3_PCI_REVISION_ID_LEN,
3247 HNS3_PCI_REVISION_ID);
3248 if (ret != HNS3_PCI_REVISION_ID_LEN) {
3249 PMD_INIT_LOG(ERR, "failed to read pci revision id, ret = %d",
3253 hw->revision = revision;
3255 if (revision < PCI_REVISION_ID_HIP09_A) {
3256 hns3_set_default_dev_specifications(hw);
3257 hw->intr.mapping_mode = HNS3_INTR_MAPPING_VEC_RSV_ONE;
3258 hw->intr.gl_unit = HNS3_INTR_COALESCE_GL_UINT_2US;
3259 hw->tso_mode = HNS3_TSO_SW_CAL_PSEUDO_H_CSUM;
3260 hw->vlan_mode = HNS3_SW_SHIFT_AND_DISCARD_MODE;
3261 hw->drop_stats_mode = HNS3_PKTS_DROP_STATS_MODE1;
3262 hw->min_tx_pkt_len = HNS3_HIP08_MIN_TX_PKT_LEN;
3263 pf->tqp_config_mode = HNS3_FIXED_MAX_TQP_NUM_MODE;
3264 hw->rss_info.ipv6_sctp_offload_supported = false;
3265 hw->udp_cksum_mode = HNS3_SPECIAL_PORT_SW_CKSUM_MODE;
3269 ret = hns3_query_dev_specifications(hw);
3272 "failed to query dev specifications, ret = %d",
3277 hw->intr.mapping_mode = HNS3_INTR_MAPPING_VEC_ALL;
3278 hw->intr.gl_unit = HNS3_INTR_COALESCE_GL_UINT_1US;
3279 hw->tso_mode = HNS3_TSO_HW_CAL_PSEUDO_H_CSUM;
3280 hw->vlan_mode = HNS3_HW_SHIFT_AND_DISCARD_MODE;
3281 hw->drop_stats_mode = HNS3_PKTS_DROP_STATS_MODE2;
3282 hw->min_tx_pkt_len = HNS3_HIP09_MIN_TX_PKT_LEN;
3283 pf->tqp_config_mode = HNS3_FLEX_MAX_TQP_NUM_MODE;
3284 hw->rss_info.ipv6_sctp_offload_supported = true;
3285 hw->udp_cksum_mode = HNS3_SPECIAL_PORT_HW_CKSUM_MODE;
3291 hns3_check_media_type(struct hns3_hw *hw, uint8_t media_type)
3295 switch (media_type) {
3296 case HNS3_MEDIA_TYPE_COPPER:
3297 if (!hns3_dev_copper_supported(hw)) {
3299 "Media type is copper, not supported.");
3305 case HNS3_MEDIA_TYPE_FIBER:
3308 case HNS3_MEDIA_TYPE_BACKPLANE:
3309 PMD_INIT_LOG(ERR, "Media type is Backplane, not supported.");
3313 PMD_INIT_LOG(ERR, "Unknown media type = %u!", media_type);
3322 hns3_get_board_configuration(struct hns3_hw *hw)
3324 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
3325 struct hns3_pf *pf = &hns->pf;
3326 struct hns3_cfg cfg;
3329 ret = hns3_get_board_cfg(hw, &cfg);
3331 PMD_INIT_LOG(ERR, "get board config failed %d", ret);
3335 ret = hns3_check_media_type(hw, cfg.media_type);
3339 hw->mac.media_type = cfg.media_type;
3340 hw->rss_size_max = cfg.rss_size_max;
3341 hw->rss_dis_flag = false;
3342 memcpy(hw->mac.mac_addr, cfg.mac_addr, RTE_ETHER_ADDR_LEN);
3343 hw->mac.phy_addr = cfg.phy_addr;
3344 hw->mac.default_addr_setted = false;
3345 hw->num_tx_desc = cfg.tqp_desc_num;
3346 hw->num_rx_desc = cfg.tqp_desc_num;
3347 hw->dcb_info.num_pg = 1;
3348 hw->dcb_info.hw_pfc_map = 0;
3350 ret = hns3_parse_speed(cfg.default_speed, &hw->mac.link_speed);
3352 PMD_INIT_LOG(ERR, "Get wrong speed %u, ret = %d",
3353 cfg.default_speed, ret);
3357 pf->tc_max = cfg.tc_num;
3358 if (pf->tc_max > HNS3_MAX_TC_NUM || pf->tc_max < 1) {
3359 PMD_INIT_LOG(WARNING,
3360 "Get TC num(%u) from flash, set TC num to 1",
3365 /* Dev does not support DCB */
3366 if (!hns3_dev_dcb_supported(hw)) {
3370 pf->pfc_max = pf->tc_max;
3372 hw->dcb_info.num_tc = 1;
3373 hw->alloc_rss_size = RTE_MIN(hw->rss_size_max,
3374 hw->tqps_num / hw->dcb_info.num_tc);
3375 hns3_set_bit(hw->hw_tc_map, 0, 1);
3376 pf->tx_sch_mode = HNS3_FLAG_TC_BASE_SCH_MODE;
3378 pf->wanted_umv_size = cfg.umv_space;
3384 hns3_get_configuration(struct hns3_hw *hw)
3388 ret = hns3_query_function_status(hw);
3390 PMD_INIT_LOG(ERR, "Failed to query function status: %d.", ret);
3394 /* Get device capability */
3395 ret = hns3_get_capability(hw);
3397 PMD_INIT_LOG(ERR, "failed to get device capability: %d.", ret);
3401 /* Get pf resource */
3402 ret = hns3_query_pf_resource(hw);
3404 PMD_INIT_LOG(ERR, "Failed to query pf resource: %d", ret);
3408 ret = hns3_get_board_configuration(hw);
3410 PMD_INIT_LOG(ERR, "failed to get board configuration: %d", ret);
3414 ret = hns3_query_dev_fec_info(hw);
3417 "failed to query FEC information, ret = %d", ret);
3423 hns3_map_tqps_to_func(struct hns3_hw *hw, uint16_t func_id, uint16_t tqp_pid,
3424 uint16_t tqp_vid, bool is_pf)
3426 struct hns3_tqp_map_cmd *req;
3427 struct hns3_cmd_desc desc;
3430 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_SET_TQP_MAP, false);
3432 req = (struct hns3_tqp_map_cmd *)desc.data;
3433 req->tqp_id = rte_cpu_to_le_16(tqp_pid);
3434 req->tqp_vf = func_id;
3435 req->tqp_flag = 1 << HNS3_TQP_MAP_EN_B;
3437 req->tqp_flag |= (1 << HNS3_TQP_MAP_TYPE_B);
3438 req->tqp_vid = rte_cpu_to_le_16(tqp_vid);
3440 ret = hns3_cmd_send(hw, &desc, 1);
3442 PMD_INIT_LOG(ERR, "TQP map failed %d", ret);
3448 hns3_map_tqp(struct hns3_hw *hw)
3454 * In current version, VF is not supported when PF is driven by DPDK
3455 * driver, so we assign total tqps_num tqps allocated to this port
3458 for (i = 0; i < hw->total_tqps_num; i++) {
3459 ret = hns3_map_tqps_to_func(hw, HNS3_PF_FUNC_ID, i, i, true);
3468 hns3_cfg_mac_speed_dup_hw(struct hns3_hw *hw, uint32_t speed, uint8_t duplex)
3470 struct hns3_config_mac_speed_dup_cmd *req;
3471 struct hns3_cmd_desc desc;
3474 req = (struct hns3_config_mac_speed_dup_cmd *)desc.data;
3476 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_CONFIG_SPEED_DUP, false);
3478 hns3_set_bit(req->speed_dup, HNS3_CFG_DUPLEX_B, !!duplex ? 1 : 0);
3481 case ETH_SPEED_NUM_10M:
3482 hns3_set_field(req->speed_dup, HNS3_CFG_SPEED_M,
3483 HNS3_CFG_SPEED_S, HNS3_CFG_SPEED_10M);
3485 case ETH_SPEED_NUM_100M:
3486 hns3_set_field(req->speed_dup, HNS3_CFG_SPEED_M,
3487 HNS3_CFG_SPEED_S, HNS3_CFG_SPEED_100M);
3489 case ETH_SPEED_NUM_1G:
3490 hns3_set_field(req->speed_dup, HNS3_CFG_SPEED_M,
3491 HNS3_CFG_SPEED_S, HNS3_CFG_SPEED_1G);
3493 case ETH_SPEED_NUM_10G:
3494 hns3_set_field(req->speed_dup, HNS3_CFG_SPEED_M,
3495 HNS3_CFG_SPEED_S, HNS3_CFG_SPEED_10G);
3497 case ETH_SPEED_NUM_25G:
3498 hns3_set_field(req->speed_dup, HNS3_CFG_SPEED_M,
3499 HNS3_CFG_SPEED_S, HNS3_CFG_SPEED_25G);
3501 case ETH_SPEED_NUM_40G:
3502 hns3_set_field(req->speed_dup, HNS3_CFG_SPEED_M,
3503 HNS3_CFG_SPEED_S, HNS3_CFG_SPEED_40G);
3505 case ETH_SPEED_NUM_50G:
3506 hns3_set_field(req->speed_dup, HNS3_CFG_SPEED_M,
3507 HNS3_CFG_SPEED_S, HNS3_CFG_SPEED_50G);
3509 case ETH_SPEED_NUM_100G:
3510 hns3_set_field(req->speed_dup, HNS3_CFG_SPEED_M,
3511 HNS3_CFG_SPEED_S, HNS3_CFG_SPEED_100G);
3513 case ETH_SPEED_NUM_200G:
3514 hns3_set_field(req->speed_dup, HNS3_CFG_SPEED_M,
3515 HNS3_CFG_SPEED_S, HNS3_CFG_SPEED_200G);
3518 PMD_INIT_LOG(ERR, "invalid speed (%u)", speed);
3522 hns3_set_bit(req->mac_change_fec_en, HNS3_CFG_MAC_SPEED_CHANGE_EN_B, 1);
3524 ret = hns3_cmd_send(hw, &desc, 1);
3526 PMD_INIT_LOG(ERR, "mac speed/duplex config cmd failed %d", ret);
3532 hns3_tx_buffer_calc(struct hns3_hw *hw, struct hns3_pkt_buf_alloc *buf_alloc)
3534 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
3535 struct hns3_pf *pf = &hns->pf;
3536 struct hns3_priv_buf *priv;
3537 uint32_t i, total_size;
3539 total_size = pf->pkt_buf_size;
3541 /* alloc tx buffer for all enabled tc */
3542 for (i = 0; i < HNS3_MAX_TC_NUM; i++) {
3543 priv = &buf_alloc->priv_buf[i];
3545 if (hw->hw_tc_map & BIT(i)) {
3546 if (total_size < pf->tx_buf_size)
3549 priv->tx_buf_size = pf->tx_buf_size;
3551 priv->tx_buf_size = 0;
3553 total_size -= priv->tx_buf_size;
3560 hns3_tx_buffer_alloc(struct hns3_hw *hw, struct hns3_pkt_buf_alloc *buf_alloc)
3562 /* TX buffer size is unit by 128 byte */
3563 #define HNS3_BUF_SIZE_UNIT_SHIFT 7
3564 #define HNS3_BUF_SIZE_UPDATE_EN_MSK BIT(15)
3565 struct hns3_tx_buff_alloc_cmd *req;
3566 struct hns3_cmd_desc desc;
3571 req = (struct hns3_tx_buff_alloc_cmd *)desc.data;
3573 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_TX_BUFF_ALLOC, 0);
3574 for (i = 0; i < HNS3_MAX_TC_NUM; i++) {
3575 buf_size = buf_alloc->priv_buf[i].tx_buf_size;
3577 buf_size = buf_size >> HNS3_BUF_SIZE_UNIT_SHIFT;
3578 req->tx_pkt_buff[i] = rte_cpu_to_le_16(buf_size |
3579 HNS3_BUF_SIZE_UPDATE_EN_MSK);
3582 ret = hns3_cmd_send(hw, &desc, 1);
3584 PMD_INIT_LOG(ERR, "tx buffer alloc cmd failed %d", ret);
3590 hns3_get_tc_num(struct hns3_hw *hw)
3595 for (i = 0; i < HNS3_MAX_TC_NUM; i++)
3596 if (hw->hw_tc_map & BIT(i))
3602 hns3_get_rx_priv_buff_alloced(struct hns3_pkt_buf_alloc *buf_alloc)
3604 struct hns3_priv_buf *priv;
3605 uint32_t rx_priv = 0;
3608 for (i = 0; i < HNS3_MAX_TC_NUM; i++) {
3609 priv = &buf_alloc->priv_buf[i];
3611 rx_priv += priv->buf_size;
3617 hns3_get_tx_buff_alloced(struct hns3_pkt_buf_alloc *buf_alloc)
3619 uint32_t total_tx_size = 0;
3622 for (i = 0; i < HNS3_MAX_TC_NUM; i++)
3623 total_tx_size += buf_alloc->priv_buf[i].tx_buf_size;
3625 return total_tx_size;
3628 /* Get the number of pfc enabled TCs, which have private buffer */
3630 hns3_get_pfc_priv_num(struct hns3_hw *hw, struct hns3_pkt_buf_alloc *buf_alloc)
3632 struct hns3_priv_buf *priv;
3636 for (i = 0; i < HNS3_MAX_TC_NUM; i++) {
3637 priv = &buf_alloc->priv_buf[i];
3638 if ((hw->dcb_info.hw_pfc_map & BIT(i)) && priv->enable)
3645 /* Get the number of pfc disabled TCs, which have private buffer */
3647 hns3_get_no_pfc_priv_num(struct hns3_hw *hw,
3648 struct hns3_pkt_buf_alloc *buf_alloc)
3650 struct hns3_priv_buf *priv;
3654 for (i = 0; i < HNS3_MAX_TC_NUM; i++) {
3655 priv = &buf_alloc->priv_buf[i];
3656 if (hw->hw_tc_map & BIT(i) &&
3657 !(hw->dcb_info.hw_pfc_map & BIT(i)) && priv->enable)
3665 hns3_is_rx_buf_ok(struct hns3_hw *hw, struct hns3_pkt_buf_alloc *buf_alloc,
3668 uint32_t shared_buf_min, shared_buf_tc, shared_std, hi_thrd, lo_thrd;
3669 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
3670 struct hns3_pf *pf = &hns->pf;
3671 uint32_t shared_buf, aligned_mps;
3676 tc_num = hns3_get_tc_num(hw);
3677 aligned_mps = roundup(pf->mps, HNS3_BUF_SIZE_UNIT);
3679 if (hns3_dev_dcb_supported(hw))
3680 shared_buf_min = HNS3_BUF_MUL_BY * aligned_mps +
3683 shared_buf_min = aligned_mps + HNS3_NON_DCB_ADDITIONAL_BUF
3686 shared_buf_tc = tc_num * aligned_mps + aligned_mps;
3687 shared_std = roundup(RTE_MAX(shared_buf_min, shared_buf_tc),
3688 HNS3_BUF_SIZE_UNIT);
3690 rx_priv = hns3_get_rx_priv_buff_alloced(buf_alloc);
3691 if (rx_all < rx_priv + shared_std)
3694 shared_buf = rounddown(rx_all - rx_priv, HNS3_BUF_SIZE_UNIT);
3695 buf_alloc->s_buf.buf_size = shared_buf;
3696 if (hns3_dev_dcb_supported(hw)) {
3697 buf_alloc->s_buf.self.high = shared_buf - pf->dv_buf_size;
3698 buf_alloc->s_buf.self.low = buf_alloc->s_buf.self.high
3699 - roundup(aligned_mps / HNS3_BUF_DIV_BY,
3700 HNS3_BUF_SIZE_UNIT);
3702 buf_alloc->s_buf.self.high =
3703 aligned_mps + HNS3_NON_DCB_ADDITIONAL_BUF;
3704 buf_alloc->s_buf.self.low = aligned_mps;
3707 if (hns3_dev_dcb_supported(hw)) {
3708 hi_thrd = shared_buf - pf->dv_buf_size;
3710 if (tc_num <= NEED_RESERVE_TC_NUM)
3711 hi_thrd = hi_thrd * BUF_RESERVE_PERCENT /
3715 hi_thrd = hi_thrd / tc_num;
3717 hi_thrd = RTE_MAX(hi_thrd, HNS3_BUF_MUL_BY * aligned_mps);
3718 hi_thrd = rounddown(hi_thrd, HNS3_BUF_SIZE_UNIT);
3719 lo_thrd = hi_thrd - aligned_mps / HNS3_BUF_DIV_BY;
3721 hi_thrd = aligned_mps + HNS3_NON_DCB_ADDITIONAL_BUF;
3722 lo_thrd = aligned_mps;
3725 for (i = 0; i < HNS3_MAX_TC_NUM; i++) {
3726 buf_alloc->s_buf.tc_thrd[i].low = lo_thrd;
3727 buf_alloc->s_buf.tc_thrd[i].high = hi_thrd;
3734 hns3_rx_buf_calc_all(struct hns3_hw *hw, bool max,
3735 struct hns3_pkt_buf_alloc *buf_alloc)
3737 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
3738 struct hns3_pf *pf = &hns->pf;
3739 struct hns3_priv_buf *priv;
3740 uint32_t aligned_mps;
3744 rx_all = pf->pkt_buf_size - hns3_get_tx_buff_alloced(buf_alloc);
3745 aligned_mps = roundup(pf->mps, HNS3_BUF_SIZE_UNIT);
3747 for (i = 0; i < HNS3_MAX_TC_NUM; i++) {
3748 priv = &buf_alloc->priv_buf[i];
3755 if (!(hw->hw_tc_map & BIT(i)))
3759 if (hw->dcb_info.hw_pfc_map & BIT(i)) {
3760 priv->wl.low = max ? aligned_mps : HNS3_BUF_SIZE_UNIT;
3761 priv->wl.high = roundup(priv->wl.low + aligned_mps,
3762 HNS3_BUF_SIZE_UNIT);
3765 priv->wl.high = max ? (aligned_mps * HNS3_BUF_MUL_BY) :
3769 priv->buf_size = priv->wl.high + pf->dv_buf_size;
3772 return hns3_is_rx_buf_ok(hw, buf_alloc, rx_all);
3776 hns3_drop_nopfc_buf_till_fit(struct hns3_hw *hw,
3777 struct hns3_pkt_buf_alloc *buf_alloc)
3779 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
3780 struct hns3_pf *pf = &hns->pf;
3781 struct hns3_priv_buf *priv;
3782 int no_pfc_priv_num;
3787 rx_all = pf->pkt_buf_size - hns3_get_tx_buff_alloced(buf_alloc);
3788 no_pfc_priv_num = hns3_get_no_pfc_priv_num(hw, buf_alloc);
3790 /* let the last to be cleared first */
3791 for (i = HNS3_MAX_TC_NUM - 1; i >= 0; i--) {
3792 priv = &buf_alloc->priv_buf[i];
3793 mask = BIT((uint8_t)i);
3795 if (hw->hw_tc_map & mask &&
3796 !(hw->dcb_info.hw_pfc_map & mask)) {
3797 /* Clear the no pfc TC private buffer */
3805 if (hns3_is_rx_buf_ok(hw, buf_alloc, rx_all) ||
3806 no_pfc_priv_num == 0)
3810 return hns3_is_rx_buf_ok(hw, buf_alloc, rx_all);
3814 hns3_drop_pfc_buf_till_fit(struct hns3_hw *hw,
3815 struct hns3_pkt_buf_alloc *buf_alloc)
3817 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
3818 struct hns3_pf *pf = &hns->pf;
3819 struct hns3_priv_buf *priv;
3825 rx_all = pf->pkt_buf_size - hns3_get_tx_buff_alloced(buf_alloc);
3826 pfc_priv_num = hns3_get_pfc_priv_num(hw, buf_alloc);
3828 /* let the last to be cleared first */
3829 for (i = HNS3_MAX_TC_NUM - 1; i >= 0; i--) {
3830 priv = &buf_alloc->priv_buf[i];
3831 mask = BIT((uint8_t)i);
3832 if (hw->hw_tc_map & mask && hw->dcb_info.hw_pfc_map & mask) {
3833 /* Reduce the number of pfc TC with private buffer */
3840 if (hns3_is_rx_buf_ok(hw, buf_alloc, rx_all) ||
3845 return hns3_is_rx_buf_ok(hw, buf_alloc, rx_all);
3849 hns3_only_alloc_priv_buff(struct hns3_hw *hw,
3850 struct hns3_pkt_buf_alloc *buf_alloc)
3852 #define COMPENSATE_BUFFER 0x3C00
3853 #define COMPENSATE_HALF_MPS_NUM 5
3854 #define PRIV_WL_GAP 0x1800
3855 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
3856 struct hns3_pf *pf = &hns->pf;
3857 uint32_t tc_num = hns3_get_tc_num(hw);
3858 uint32_t half_mps = pf->mps >> 1;
3859 struct hns3_priv_buf *priv;
3860 uint32_t min_rx_priv;
3864 rx_priv = pf->pkt_buf_size - hns3_get_tx_buff_alloced(buf_alloc);
3866 rx_priv = rx_priv / tc_num;
3868 if (tc_num <= NEED_RESERVE_TC_NUM)
3869 rx_priv = rx_priv * BUF_RESERVE_PERCENT / BUF_MAX_PERCENT;
3872 * Minimum value of private buffer in rx direction (min_rx_priv) is
3873 * equal to "DV + 2.5 * MPS + 15KB". Driver only allocates rx private
3874 * buffer if rx_priv is greater than min_rx_priv.
3876 min_rx_priv = pf->dv_buf_size + COMPENSATE_BUFFER +
3877 COMPENSATE_HALF_MPS_NUM * half_mps;
3878 min_rx_priv = roundup(min_rx_priv, HNS3_BUF_SIZE_UNIT);
3879 rx_priv = rounddown(rx_priv, HNS3_BUF_SIZE_UNIT);
3881 if (rx_priv < min_rx_priv)
3884 for (i = 0; i < HNS3_MAX_TC_NUM; i++) {
3885 priv = &buf_alloc->priv_buf[i];
3891 if (!(hw->hw_tc_map & BIT(i)))
3895 priv->buf_size = rx_priv;
3896 priv->wl.high = rx_priv - pf->dv_buf_size;
3897 priv->wl.low = priv->wl.high - PRIV_WL_GAP;
3900 buf_alloc->s_buf.buf_size = 0;
3906 * hns3_rx_buffer_calc: calculate the rx private buffer size for all TCs
3907 * @hw: pointer to struct hns3_hw
3908 * @buf_alloc: pointer to buffer calculation data
3909 * @return: 0: calculate sucessful, negative: fail
3912 hns3_rx_buffer_calc(struct hns3_hw *hw, struct hns3_pkt_buf_alloc *buf_alloc)
3914 /* When DCB is not supported, rx private buffer is not allocated. */
3915 if (!hns3_dev_dcb_supported(hw)) {
3916 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
3917 struct hns3_pf *pf = &hns->pf;
3918 uint32_t rx_all = pf->pkt_buf_size;
3920 rx_all -= hns3_get_tx_buff_alloced(buf_alloc);
3921 if (!hns3_is_rx_buf_ok(hw, buf_alloc, rx_all))
3928 * Try to allocate privated packet buffer for all TCs without share
3931 if (hns3_only_alloc_priv_buff(hw, buf_alloc))
3935 * Try to allocate privated packet buffer for all TCs with share
3938 if (hns3_rx_buf_calc_all(hw, true, buf_alloc))
3942 * For different application scenes, the enabled port number, TC number
3943 * and no_drop TC number are different. In order to obtain the better
3944 * performance, software could allocate the buffer size and configure
3945 * the waterline by tring to decrease the private buffer size according
3946 * to the order, namely, waterline of valided tc, pfc disabled tc, pfc
3949 if (hns3_rx_buf_calc_all(hw, false, buf_alloc))
3952 if (hns3_drop_nopfc_buf_till_fit(hw, buf_alloc))
3955 if (hns3_drop_pfc_buf_till_fit(hw, buf_alloc))
3962 hns3_rx_priv_buf_alloc(struct hns3_hw *hw, struct hns3_pkt_buf_alloc *buf_alloc)
3964 struct hns3_rx_priv_buff_cmd *req;
3965 struct hns3_cmd_desc desc;
3970 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_RX_PRIV_BUFF_ALLOC, false);
3971 req = (struct hns3_rx_priv_buff_cmd *)desc.data;
3973 /* Alloc private buffer TCs */
3974 for (i = 0; i < HNS3_MAX_TC_NUM; i++) {
3975 struct hns3_priv_buf *priv = &buf_alloc->priv_buf[i];
3978 rte_cpu_to_le_16(priv->buf_size >> HNS3_BUF_UNIT_S);
3979 req->buf_num[i] |= rte_cpu_to_le_16(1 << HNS3_TC0_PRI_BUF_EN_B);
3982 buf_size = buf_alloc->s_buf.buf_size;
3983 req->shared_buf = rte_cpu_to_le_16((buf_size >> HNS3_BUF_UNIT_S) |
3984 (1 << HNS3_TC0_PRI_BUF_EN_B));
3986 ret = hns3_cmd_send(hw, &desc, 1);
3988 PMD_INIT_LOG(ERR, "rx private buffer alloc cmd failed %d", ret);
3994 hns3_rx_priv_wl_config(struct hns3_hw *hw, struct hns3_pkt_buf_alloc *buf_alloc)
3996 #define HNS3_RX_PRIV_WL_ALLOC_DESC_NUM 2
3997 struct hns3_rx_priv_wl_buf *req;
3998 struct hns3_priv_buf *priv;
3999 struct hns3_cmd_desc desc[HNS3_RX_PRIV_WL_ALLOC_DESC_NUM];
4003 for (i = 0; i < HNS3_RX_PRIV_WL_ALLOC_DESC_NUM; i++) {
4004 hns3_cmd_setup_basic_desc(&desc[i], HNS3_OPC_RX_PRIV_WL_ALLOC,
4006 req = (struct hns3_rx_priv_wl_buf *)desc[i].data;
4008 /* The first descriptor set the NEXT bit to 1 */
4010 desc[i].flag |= rte_cpu_to_le_16(HNS3_CMD_FLAG_NEXT);
4012 desc[i].flag &= ~rte_cpu_to_le_16(HNS3_CMD_FLAG_NEXT);
4014 for (j = 0; j < HNS3_TC_NUM_ONE_DESC; j++) {
4015 uint32_t idx = i * HNS3_TC_NUM_ONE_DESC + j;
4017 priv = &buf_alloc->priv_buf[idx];
4018 req->tc_wl[j].high = rte_cpu_to_le_16(priv->wl.high >>
4020 req->tc_wl[j].high |=
4021 rte_cpu_to_le_16(BIT(HNS3_RX_PRIV_EN_B));
4022 req->tc_wl[j].low = rte_cpu_to_le_16(priv->wl.low >>
4024 req->tc_wl[j].low |=
4025 rte_cpu_to_le_16(BIT(HNS3_RX_PRIV_EN_B));
4029 /* Send 2 descriptor at one time */
4030 ret = hns3_cmd_send(hw, desc, HNS3_RX_PRIV_WL_ALLOC_DESC_NUM);
4032 PMD_INIT_LOG(ERR, "rx private waterline config cmd failed %d",
4038 hns3_common_thrd_config(struct hns3_hw *hw,
4039 struct hns3_pkt_buf_alloc *buf_alloc)
4041 #define HNS3_RX_COM_THRD_ALLOC_DESC_NUM 2
4042 struct hns3_shared_buf *s_buf = &buf_alloc->s_buf;
4043 struct hns3_rx_com_thrd *req;
4044 struct hns3_cmd_desc desc[HNS3_RX_COM_THRD_ALLOC_DESC_NUM];
4045 struct hns3_tc_thrd *tc;
4050 for (i = 0; i < HNS3_RX_COM_THRD_ALLOC_DESC_NUM; i++) {
4051 hns3_cmd_setup_basic_desc(&desc[i], HNS3_OPC_RX_COM_THRD_ALLOC,
4053 req = (struct hns3_rx_com_thrd *)&desc[i].data;
4055 /* The first descriptor set the NEXT bit to 1 */
4057 desc[i].flag |= rte_cpu_to_le_16(HNS3_CMD_FLAG_NEXT);
4059 desc[i].flag &= ~rte_cpu_to_le_16(HNS3_CMD_FLAG_NEXT);
4061 for (j = 0; j < HNS3_TC_NUM_ONE_DESC; j++) {
4062 tc_idx = i * HNS3_TC_NUM_ONE_DESC + j;
4063 tc = &s_buf->tc_thrd[tc_idx];
4065 req->com_thrd[j].high =
4066 rte_cpu_to_le_16(tc->high >> HNS3_BUF_UNIT_S);
4067 req->com_thrd[j].high |=
4068 rte_cpu_to_le_16(BIT(HNS3_RX_PRIV_EN_B));
4069 req->com_thrd[j].low =
4070 rte_cpu_to_le_16(tc->low >> HNS3_BUF_UNIT_S);
4071 req->com_thrd[j].low |=
4072 rte_cpu_to_le_16(BIT(HNS3_RX_PRIV_EN_B));
4076 /* Send 2 descriptors at one time */
4077 ret = hns3_cmd_send(hw, desc, HNS3_RX_COM_THRD_ALLOC_DESC_NUM);
4079 PMD_INIT_LOG(ERR, "common threshold config cmd failed %d", ret);
4085 hns3_common_wl_config(struct hns3_hw *hw, struct hns3_pkt_buf_alloc *buf_alloc)
4087 struct hns3_shared_buf *buf = &buf_alloc->s_buf;
4088 struct hns3_rx_com_wl *req;
4089 struct hns3_cmd_desc desc;
4092 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_RX_COM_WL_ALLOC, false);
4094 req = (struct hns3_rx_com_wl *)desc.data;
4095 req->com_wl.high = rte_cpu_to_le_16(buf->self.high >> HNS3_BUF_UNIT_S);
4096 req->com_wl.high |= rte_cpu_to_le_16(BIT(HNS3_RX_PRIV_EN_B));
4098 req->com_wl.low = rte_cpu_to_le_16(buf->self.low >> HNS3_BUF_UNIT_S);
4099 req->com_wl.low |= rte_cpu_to_le_16(BIT(HNS3_RX_PRIV_EN_B));
4101 ret = hns3_cmd_send(hw, &desc, 1);
4103 PMD_INIT_LOG(ERR, "common waterline config cmd failed %d", ret);
4109 hns3_buffer_alloc(struct hns3_hw *hw)
4111 struct hns3_pkt_buf_alloc pkt_buf;
4114 memset(&pkt_buf, 0, sizeof(pkt_buf));
4115 ret = hns3_tx_buffer_calc(hw, &pkt_buf);
4118 "could not calc tx buffer size for all TCs %d",
4123 ret = hns3_tx_buffer_alloc(hw, &pkt_buf);
4125 PMD_INIT_LOG(ERR, "could not alloc tx buffers %d", ret);
4129 ret = hns3_rx_buffer_calc(hw, &pkt_buf);
4132 "could not calc rx priv buffer size for all TCs %d",
4137 ret = hns3_rx_priv_buf_alloc(hw, &pkt_buf);
4139 PMD_INIT_LOG(ERR, "could not alloc rx priv buffer %d", ret);
4143 if (hns3_dev_dcb_supported(hw)) {
4144 ret = hns3_rx_priv_wl_config(hw, &pkt_buf);
4147 "could not configure rx private waterline %d",
4152 ret = hns3_common_thrd_config(hw, &pkt_buf);
4155 "could not configure common threshold %d",
4161 ret = hns3_common_wl_config(hw, &pkt_buf);
4163 PMD_INIT_LOG(ERR, "could not configure common waterline %d",
4170 hns3_mac_init(struct hns3_hw *hw)
4172 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
4173 struct hns3_mac *mac = &hw->mac;
4174 struct hns3_pf *pf = &hns->pf;
4177 pf->support_sfp_query = true;
4178 mac->link_duplex = ETH_LINK_FULL_DUPLEX;
4179 ret = hns3_cfg_mac_speed_dup_hw(hw, mac->link_speed, mac->link_duplex);
4181 PMD_INIT_LOG(ERR, "Config mac speed dup fail ret = %d", ret);
4185 mac->link_status = ETH_LINK_DOWN;
4187 return hns3_config_mtu(hw, pf->mps);
4191 hns3_get_mac_ethertype_cmd_status(uint16_t cmdq_resp, uint8_t resp_code)
4193 #define HNS3_ETHERTYPE_SUCCESS_ADD 0
4194 #define HNS3_ETHERTYPE_ALREADY_ADD 1
4195 #define HNS3_ETHERTYPE_MGR_TBL_OVERFLOW 2
4196 #define HNS3_ETHERTYPE_KEY_CONFLICT 3
4201 "cmdq execute failed for get_mac_ethertype_cmd_status, status=%u.\n",
4206 switch (resp_code) {
4207 case HNS3_ETHERTYPE_SUCCESS_ADD:
4208 case HNS3_ETHERTYPE_ALREADY_ADD:
4211 case HNS3_ETHERTYPE_MGR_TBL_OVERFLOW:
4213 "add mac ethertype failed for manager table overflow.");
4214 return_status = -EIO;
4216 case HNS3_ETHERTYPE_KEY_CONFLICT:
4217 PMD_INIT_LOG(ERR, "add mac ethertype failed for key conflict.");
4218 return_status = -EIO;
4222 "add mac ethertype failed for undefined, code=%u.",
4224 return_status = -EIO;
4228 return return_status;
4232 hns3_add_mgr_tbl(struct hns3_hw *hw,
4233 const struct hns3_mac_mgr_tbl_entry_cmd *req)
4235 struct hns3_cmd_desc desc;
4240 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_MAC_ETHTYPE_ADD, false);
4241 memcpy(desc.data, req, sizeof(struct hns3_mac_mgr_tbl_entry_cmd));
4243 ret = hns3_cmd_send(hw, &desc, 1);
4246 "add mac ethertype failed for cmd_send, ret =%d.",
4251 resp_code = (rte_le_to_cpu_32(desc.data[0]) >> 8) & 0xff;
4252 retval = rte_le_to_cpu_16(desc.retval);
4254 return hns3_get_mac_ethertype_cmd_status(retval, resp_code);
4258 hns3_prepare_mgr_tbl(struct hns3_mac_mgr_tbl_entry_cmd *mgr_table,
4259 int *table_item_num)
4261 struct hns3_mac_mgr_tbl_entry_cmd *tbl;
4264 * In current version, we add one item in management table as below:
4265 * 0x0180C200000E -- LLDP MC address
4268 tbl->flags = HNS3_MAC_MGR_MASK_VLAN_B;
4269 tbl->ethter_type = rte_cpu_to_le_16(HNS3_MAC_ETHERTYPE_LLDP);
4270 tbl->mac_addr_hi32 = rte_cpu_to_le_32(htonl(0x0180C200));
4271 tbl->mac_addr_lo16 = rte_cpu_to_le_16(htons(0x000E));
4272 tbl->i_port_bitmap = 0x1;
4273 *table_item_num = 1;
4277 hns3_init_mgr_tbl(struct hns3_hw *hw)
4279 #define HNS_MAC_MGR_TBL_MAX_SIZE 16
4280 struct hns3_mac_mgr_tbl_entry_cmd mgr_table[HNS_MAC_MGR_TBL_MAX_SIZE];
4285 memset(mgr_table, 0, sizeof(mgr_table));
4286 hns3_prepare_mgr_tbl(mgr_table, &table_item_num);
4287 for (i = 0; i < table_item_num; i++) {
4288 ret = hns3_add_mgr_tbl(hw, &mgr_table[i]);
4290 PMD_INIT_LOG(ERR, "add mac ethertype failed, ret =%d",
4300 hns3_promisc_param_init(struct hns3_promisc_param *param, bool en_uc,
4301 bool en_mc, bool en_bc, int vport_id)
4306 memset(param, 0, sizeof(struct hns3_promisc_param));
4308 param->enable = HNS3_PROMISC_EN_UC;
4310 param->enable |= HNS3_PROMISC_EN_MC;
4312 param->enable |= HNS3_PROMISC_EN_BC;
4313 param->vf_id = vport_id;
4317 hns3_cmd_set_promisc_mode(struct hns3_hw *hw, struct hns3_promisc_param *param)
4319 struct hns3_promisc_cfg_cmd *req;
4320 struct hns3_cmd_desc desc;
4323 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_CFG_PROMISC_MODE, false);
4325 req = (struct hns3_promisc_cfg_cmd *)desc.data;
4326 req->vf_id = param->vf_id;
4327 req->flag = (param->enable << HNS3_PROMISC_EN_B) |
4328 HNS3_PROMISC_TX_EN_B | HNS3_PROMISC_RX_EN_B;
4330 ret = hns3_cmd_send(hw, &desc, 1);
4332 PMD_INIT_LOG(ERR, "Set promisc mode fail, ret = %d", ret);
4338 hns3_set_promisc_mode(struct hns3_hw *hw, bool en_uc_pmc, bool en_mc_pmc)
4340 struct hns3_promisc_param param;
4341 bool en_bc_pmc = true;
4345 * In current version VF is not supported when PF is driven by DPDK
4346 * driver, just need to configure parameters for PF vport.
4348 vf_id = HNS3_PF_FUNC_ID;
4350 hns3_promisc_param_init(¶m, en_uc_pmc, en_mc_pmc, en_bc_pmc, vf_id);
4351 return hns3_cmd_set_promisc_mode(hw, ¶m);
4355 hns3_promisc_init(struct hns3_hw *hw)
4357 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
4358 struct hns3_pf *pf = &hns->pf;
4359 struct hns3_promisc_param param;
4363 ret = hns3_set_promisc_mode(hw, false, false);
4365 PMD_INIT_LOG(ERR, "failed to set promisc mode, ret = %d", ret);
4370 * In current version VFs are not supported when PF is driven by DPDK
4371 * driver. After PF has been taken over by DPDK, the original VF will
4372 * be invalid. So, there is a possibility of entry residues. It should
4373 * clear VFs's promisc mode to avoid unnecessary bandwidth usage
4376 for (func_id = HNS3_1ST_VF_FUNC_ID; func_id < pf->func_num; func_id++) {
4377 hns3_promisc_param_init(¶m, false, false, false, func_id);
4378 ret = hns3_cmd_set_promisc_mode(hw, ¶m);
4380 PMD_INIT_LOG(ERR, "failed to clear vf:%u promisc mode,"
4381 " ret = %d", func_id, ret);
4390 hns3_promisc_uninit(struct hns3_hw *hw)
4392 struct hns3_promisc_param param;
4396 func_id = HNS3_PF_FUNC_ID;
4399 * In current version VFs are not supported when PF is driven by
4400 * DPDK driver, and VFs' promisc mode status has been cleared during
4401 * init and their status will not change. So just clear PF's promisc
4402 * mode status during uninit.
4404 hns3_promisc_param_init(¶m, false, false, false, func_id);
4405 ret = hns3_cmd_set_promisc_mode(hw, ¶m);
4407 PMD_INIT_LOG(ERR, "failed to clear promisc status during"
4408 " uninit, ret = %d", ret);
4412 hns3_dev_promiscuous_enable(struct rte_eth_dev *dev)
4414 bool allmulti = dev->data->all_multicast ? true : false;
4415 struct hns3_adapter *hns = dev->data->dev_private;
4416 struct hns3_hw *hw = &hns->hw;
4421 rte_spinlock_lock(&hw->lock);
4422 ret = hns3_set_promisc_mode(hw, true, true);
4424 rte_spinlock_unlock(&hw->lock);
4425 hns3_err(hw, "failed to enable promiscuous mode, ret = %d",
4431 * When promiscuous mode was enabled, disable the vlan filter to let
4432 * all packets coming in in the receiving direction.
4434 offloads = dev->data->dev_conf.rxmode.offloads;
4435 if (offloads & DEV_RX_OFFLOAD_VLAN_FILTER) {
4436 ret = hns3_enable_vlan_filter(hns, false);
4438 hns3_err(hw, "failed to enable promiscuous mode due to "
4439 "failure to disable vlan filter, ret = %d",
4441 err = hns3_set_promisc_mode(hw, false, allmulti);
4443 hns3_err(hw, "failed to restore promiscuous "
4444 "status after disable vlan filter "
4445 "failed during enabling promiscuous "
4446 "mode, ret = %d", ret);
4450 rte_spinlock_unlock(&hw->lock);
4456 hns3_dev_promiscuous_disable(struct rte_eth_dev *dev)
4458 bool allmulti = dev->data->all_multicast ? true : false;
4459 struct hns3_adapter *hns = dev->data->dev_private;
4460 struct hns3_hw *hw = &hns->hw;
4465 /* If now in all_multicast mode, must remain in all_multicast mode. */
4466 rte_spinlock_lock(&hw->lock);
4467 ret = hns3_set_promisc_mode(hw, false, allmulti);
4469 rte_spinlock_unlock(&hw->lock);
4470 hns3_err(hw, "failed to disable promiscuous mode, ret = %d",
4474 /* when promiscuous mode was disabled, restore the vlan filter status */
4475 offloads = dev->data->dev_conf.rxmode.offloads;
4476 if (offloads & DEV_RX_OFFLOAD_VLAN_FILTER) {
4477 ret = hns3_enable_vlan_filter(hns, true);
4479 hns3_err(hw, "failed to disable promiscuous mode due to"
4480 " failure to restore vlan filter, ret = %d",
4482 err = hns3_set_promisc_mode(hw, true, true);
4484 hns3_err(hw, "failed to restore promiscuous "
4485 "status after enabling vlan filter "
4486 "failed during disabling promiscuous "
4487 "mode, ret = %d", ret);
4490 rte_spinlock_unlock(&hw->lock);
4496 hns3_dev_allmulticast_enable(struct rte_eth_dev *dev)
4498 struct hns3_adapter *hns = dev->data->dev_private;
4499 struct hns3_hw *hw = &hns->hw;
4502 if (dev->data->promiscuous)
4505 rte_spinlock_lock(&hw->lock);
4506 ret = hns3_set_promisc_mode(hw, false, true);
4507 rte_spinlock_unlock(&hw->lock);
4509 hns3_err(hw, "failed to enable allmulticast mode, ret = %d",
4516 hns3_dev_allmulticast_disable(struct rte_eth_dev *dev)
4518 struct hns3_adapter *hns = dev->data->dev_private;
4519 struct hns3_hw *hw = &hns->hw;
4522 /* If now in promiscuous mode, must remain in all_multicast mode. */
4523 if (dev->data->promiscuous)
4526 rte_spinlock_lock(&hw->lock);
4527 ret = hns3_set_promisc_mode(hw, false, false);
4528 rte_spinlock_unlock(&hw->lock);
4530 hns3_err(hw, "failed to disable allmulticast mode, ret = %d",
4537 hns3_dev_promisc_restore(struct hns3_adapter *hns)
4539 struct hns3_hw *hw = &hns->hw;
4540 bool allmulti = hw->data->all_multicast ? true : false;
4543 if (hw->data->promiscuous) {
4544 ret = hns3_set_promisc_mode(hw, true, true);
4546 hns3_err(hw, "failed to restore promiscuous mode, "
4551 ret = hns3_set_promisc_mode(hw, false, allmulti);
4553 hns3_err(hw, "failed to restore allmulticast mode, ret = %d",
4559 hns3_get_sfp_info(struct hns3_hw *hw, struct hns3_mac *mac_info)
4561 struct hns3_sfp_info_cmd *resp;
4562 struct hns3_cmd_desc desc;
4565 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_GET_SFP_INFO, true);
4566 resp = (struct hns3_sfp_info_cmd *)desc.data;
4567 resp->query_type = HNS3_ACTIVE_QUERY;
4569 ret = hns3_cmd_send(hw, &desc, 1);
4570 if (ret == -EOPNOTSUPP) {
4571 hns3_warn(hw, "firmware does not support get SFP info,"
4575 hns3_err(hw, "get sfp info failed, ret = %d.", ret);
4580 * In some case, the speed of MAC obtained from firmware may be 0, it
4581 * shouldn't be set to mac->speed.
4583 if (!rte_le_to_cpu_32(resp->sfp_speed))
4586 mac_info->link_speed = rte_le_to_cpu_32(resp->sfp_speed);
4588 * if resp->supported_speed is 0, it means it's an old version
4589 * firmware, do not update these params.
4591 if (resp->supported_speed) {
4592 mac_info->query_type = HNS3_ACTIVE_QUERY;
4593 mac_info->supported_speed =
4594 rte_le_to_cpu_32(resp->supported_speed);
4595 mac_info->support_autoneg = resp->autoneg_ability;
4596 mac_info->link_autoneg = (resp->autoneg == 0) ? ETH_LINK_FIXED
4599 mac_info->query_type = HNS3_DEFAULT_QUERY;
4606 hns3_check_speed_dup(uint8_t duplex, uint32_t speed)
4608 if (!(speed == ETH_SPEED_NUM_10M || speed == ETH_SPEED_NUM_100M))
4609 duplex = ETH_LINK_FULL_DUPLEX;
4615 hns3_cfg_mac_speed_dup(struct hns3_hw *hw, uint32_t speed, uint8_t duplex)
4617 struct hns3_mac *mac = &hw->mac;
4620 duplex = hns3_check_speed_dup(duplex, speed);
4621 if (mac->link_speed == speed && mac->link_duplex == duplex)
4624 ret = hns3_cfg_mac_speed_dup_hw(hw, speed, duplex);
4628 ret = hns3_port_shaper_update(hw, speed);
4632 mac->link_speed = speed;
4633 mac->link_duplex = duplex;
4639 hns3_update_fiber_link_info(struct hns3_hw *hw)
4641 struct hns3_pf *pf = HNS3_DEV_HW_TO_PF(hw);
4642 struct hns3_mac *mac = &hw->mac;
4643 struct hns3_mac mac_info;
4646 /* If firmware do not support get SFP/qSFP speed, return directly */
4647 if (!pf->support_sfp_query)
4650 memset(&mac_info, 0, sizeof(struct hns3_mac));
4651 ret = hns3_get_sfp_info(hw, &mac_info);
4652 if (ret == -EOPNOTSUPP) {
4653 pf->support_sfp_query = false;
4658 /* Do nothing if no SFP */
4659 if (mac_info.link_speed == ETH_SPEED_NUM_NONE)
4663 * If query_type is HNS3_ACTIVE_QUERY, it is no need
4664 * to reconfigure the speed of MAC. Otherwise, it indicates
4665 * that the current firmware only supports to obtain the
4666 * speed of the SFP, and the speed of MAC needs to reconfigure.
4668 mac->query_type = mac_info.query_type;
4669 if (mac->query_type == HNS3_ACTIVE_QUERY) {
4670 if (mac_info.link_speed != mac->link_speed) {
4671 ret = hns3_port_shaper_update(hw, mac_info.link_speed);
4676 mac->link_speed = mac_info.link_speed;
4677 mac->supported_speed = mac_info.supported_speed;
4678 mac->support_autoneg = mac_info.support_autoneg;
4679 mac->link_autoneg = mac_info.link_autoneg;
4684 /* Config full duplex for SFP */
4685 return hns3_cfg_mac_speed_dup(hw, mac_info.link_speed,
4686 ETH_LINK_FULL_DUPLEX);
4690 hns3_parse_copper_phy_params(struct hns3_cmd_desc *desc, struct hns3_mac *mac)
4692 #define HNS3_PHY_SUPPORTED_SPEED_MASK 0x2f
4694 struct hns3_phy_params_bd0_cmd *req;
4697 req = (struct hns3_phy_params_bd0_cmd *)desc[0].data;
4698 mac->link_speed = rte_le_to_cpu_32(req->speed);
4699 mac->link_duplex = hns3_get_bit(req->duplex,
4700 HNS3_PHY_DUPLEX_CFG_B);
4701 mac->link_autoneg = hns3_get_bit(req->autoneg,
4702 HNS3_PHY_AUTONEG_CFG_B);
4703 mac->advertising = rte_le_to_cpu_32(req->advertising);
4704 mac->lp_advertising = rte_le_to_cpu_32(req->lp_advertising);
4705 supported = rte_le_to_cpu_32(req->supported);
4706 mac->supported_speed = supported & HNS3_PHY_SUPPORTED_SPEED_MASK;
4707 mac->support_autoneg = !!(supported & HNS3_PHY_LINK_MODE_AUTONEG_BIT);
4711 hns3_get_copper_phy_params(struct hns3_hw *hw, struct hns3_mac *mac)
4713 struct hns3_cmd_desc desc[HNS3_PHY_PARAM_CFG_BD_NUM];
4717 for (i = 0; i < HNS3_PHY_PARAM_CFG_BD_NUM - 1; i++) {
4718 hns3_cmd_setup_basic_desc(&desc[i], HNS3_OPC_PHY_PARAM_CFG,
4720 desc[i].flag |= rte_cpu_to_le_16(HNS3_CMD_FLAG_NEXT);
4722 hns3_cmd_setup_basic_desc(&desc[i], HNS3_OPC_PHY_PARAM_CFG, true);
4724 ret = hns3_cmd_send(hw, desc, HNS3_PHY_PARAM_CFG_BD_NUM);
4726 hns3_err(hw, "get phy parameters failed, ret = %d.", ret);
4730 hns3_parse_copper_phy_params(desc, mac);
4736 hns3_update_copper_link_info(struct hns3_hw *hw)
4738 struct hns3_mac *mac = &hw->mac;
4739 struct hns3_mac mac_info;
4742 memset(&mac_info, 0, sizeof(struct hns3_mac));
4743 ret = hns3_get_copper_phy_params(hw, &mac_info);
4747 if (mac_info.link_speed != mac->link_speed) {
4748 ret = hns3_port_shaper_update(hw, mac_info.link_speed);
4753 mac->link_speed = mac_info.link_speed;
4754 mac->link_duplex = mac_info.link_duplex;
4755 mac->link_autoneg = mac_info.link_autoneg;
4756 mac->supported_speed = mac_info.supported_speed;
4757 mac->advertising = mac_info.advertising;
4758 mac->lp_advertising = mac_info.lp_advertising;
4759 mac->support_autoneg = mac_info.support_autoneg;
4765 hns3_update_link_info(struct rte_eth_dev *eth_dev)
4767 struct hns3_adapter *hns = eth_dev->data->dev_private;
4768 struct hns3_hw *hw = &hns->hw;
4771 if (hw->mac.media_type == HNS3_MEDIA_TYPE_COPPER)
4772 ret = hns3_update_copper_link_info(hw);
4773 else if (hw->mac.media_type == HNS3_MEDIA_TYPE_FIBER)
4774 ret = hns3_update_fiber_link_info(hw);
4780 hns3_cfg_mac_mode(struct hns3_hw *hw, bool enable)
4782 struct hns3_config_mac_mode_cmd *req;
4783 struct hns3_cmd_desc desc;
4784 uint32_t loop_en = 0;
4788 req = (struct hns3_config_mac_mode_cmd *)desc.data;
4790 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_CONFIG_MAC_MODE, false);
4793 hns3_set_bit(loop_en, HNS3_MAC_TX_EN_B, val);
4794 hns3_set_bit(loop_en, HNS3_MAC_RX_EN_B, val);
4795 hns3_set_bit(loop_en, HNS3_MAC_PAD_TX_B, val);
4796 hns3_set_bit(loop_en, HNS3_MAC_PAD_RX_B, val);
4797 hns3_set_bit(loop_en, HNS3_MAC_1588_TX_B, 0);
4798 hns3_set_bit(loop_en, HNS3_MAC_1588_RX_B, 0);
4799 hns3_set_bit(loop_en, HNS3_MAC_APP_LP_B, 0);
4800 hns3_set_bit(loop_en, HNS3_MAC_LINE_LP_B, 0);
4801 hns3_set_bit(loop_en, HNS3_MAC_FCS_TX_B, val);
4802 hns3_set_bit(loop_en, HNS3_MAC_RX_FCS_B, val);
4805 * If DEV_RX_OFFLOAD_KEEP_CRC offload is set, MAC will not strip CRC
4806 * when receiving frames. Otherwise, CRC will be stripped.
4808 if (hw->data->dev_conf.rxmode.offloads & DEV_RX_OFFLOAD_KEEP_CRC)
4809 hns3_set_bit(loop_en, HNS3_MAC_RX_FCS_STRIP_B, 0);
4811 hns3_set_bit(loop_en, HNS3_MAC_RX_FCS_STRIP_B, val);
4812 hns3_set_bit(loop_en, HNS3_MAC_TX_OVERSIZE_TRUNCATE_B, val);
4813 hns3_set_bit(loop_en, HNS3_MAC_RX_OVERSIZE_TRUNCATE_B, val);
4814 hns3_set_bit(loop_en, HNS3_MAC_TX_UNDER_MIN_ERR_B, val);
4815 req->txrx_pad_fcs_loop_en = rte_cpu_to_le_32(loop_en);
4817 ret = hns3_cmd_send(hw, &desc, 1);
4819 PMD_INIT_LOG(ERR, "mac enable fail, ret =%d.", ret);
4825 hns3_get_mac_link_status(struct hns3_hw *hw)
4827 struct hns3_link_status_cmd *req;
4828 struct hns3_cmd_desc desc;
4832 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_QUERY_LINK_STATUS, true);
4833 ret = hns3_cmd_send(hw, &desc, 1);
4835 hns3_err(hw, "get link status cmd failed %d", ret);
4836 return ETH_LINK_DOWN;
4839 req = (struct hns3_link_status_cmd *)desc.data;
4840 link_status = req->status & HNS3_LINK_STATUS_UP_M;
4842 return !!link_status;
4846 hns3_update_link_status(struct hns3_hw *hw)
4850 state = hns3_get_mac_link_status(hw);
4851 if (state != hw->mac.link_status) {
4852 hw->mac.link_status = state;
4853 hns3_warn(hw, "Link status change to %s!", state ? "up" : "down");
4854 hns3_config_mac_tnl_int(hw,
4855 state == ETH_LINK_UP ? true : false);
4863 hns3_update_linkstatus_and_event(struct hns3_hw *hw, bool query)
4865 struct rte_eth_dev *dev = &rte_eth_devices[hw->data->port_id];
4866 struct rte_eth_link new_link;
4870 hns3_update_port_link_info(dev);
4872 memset(&new_link, 0, sizeof(new_link));
4873 hns3_setup_linkstatus(dev, &new_link);
4875 ret = rte_eth_linkstatus_set(dev, &new_link);
4876 if (ret == 0 && dev->data->dev_conf.intr_conf.lsc != 0)
4877 hns3_start_report_lse(dev);
4881 hns3_service_handler(void *param)
4883 struct rte_eth_dev *eth_dev = (struct rte_eth_dev *)param;
4884 struct hns3_adapter *hns = eth_dev->data->dev_private;
4885 struct hns3_hw *hw = &hns->hw;
4887 if (!hns3_is_reset_pending(hns))
4888 hns3_update_linkstatus_and_event(hw, true);
4890 hns3_warn(hw, "Cancel the query when reset is pending");
4892 rte_eal_alarm_set(HNS3_SERVICE_INTERVAL, hns3_service_handler, eth_dev);
4896 hns3_init_hardware(struct hns3_adapter *hns)
4898 struct hns3_hw *hw = &hns->hw;
4901 ret = hns3_map_tqp(hw);
4903 PMD_INIT_LOG(ERR, "Failed to map tqp: %d", ret);
4907 ret = hns3_init_umv_space(hw);
4909 PMD_INIT_LOG(ERR, "Failed to init umv space: %d", ret);
4913 ret = hns3_mac_init(hw);
4915 PMD_INIT_LOG(ERR, "Failed to init MAC: %d", ret);
4919 ret = hns3_init_mgr_tbl(hw);
4921 PMD_INIT_LOG(ERR, "Failed to init manager table: %d", ret);
4925 ret = hns3_promisc_init(hw);
4927 PMD_INIT_LOG(ERR, "Failed to init promisc: %d",
4932 ret = hns3_init_vlan_config(hns);
4934 PMD_INIT_LOG(ERR, "Failed to init vlan: %d", ret);
4938 ret = hns3_dcb_init(hw);
4940 PMD_INIT_LOG(ERR, "Failed to init dcb: %d", ret);
4944 ret = hns3_init_fd_config(hns);
4946 PMD_INIT_LOG(ERR, "Failed to init flow director: %d", ret);
4950 ret = hns3_config_tso(hw, HNS3_TSO_MSS_MIN, HNS3_TSO_MSS_MAX);
4952 PMD_INIT_LOG(ERR, "Failed to config tso: %d", ret);
4956 ret = hns3_config_gro(hw, false);
4958 PMD_INIT_LOG(ERR, "Failed to config gro: %d", ret);
4963 * In the initialization clearing the all hardware mapping relationship
4964 * configurations between queues and interrupt vectors is needed, so
4965 * some error caused by the residual configurations, such as the
4966 * unexpected interrupt, can be avoid.
4968 ret = hns3_init_ring_with_vector(hw);
4970 PMD_INIT_LOG(ERR, "Failed to init ring intr vector: %d", ret);
4977 hns3_uninit_umv_space(hw);
4982 hns3_clear_hw(struct hns3_hw *hw)
4984 struct hns3_cmd_desc desc;
4987 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_CLEAR_HW_STATE, false);
4989 ret = hns3_cmd_send(hw, &desc, 1);
4990 if (ret && ret != -EOPNOTSUPP)
4997 hns3_config_all_msix_error(struct hns3_hw *hw, bool enable)
5002 * The new firmware support report more hardware error types by
5003 * msix mode. These errors are defined as RAS errors in hardware
5004 * and belong to a different type from the MSI-x errors processed
5005 * by the network driver.
5007 * Network driver should open the new error report on initialition
5009 val = hns3_read_dev(hw, HNS3_VECTOR0_OTER_EN_REG);
5010 hns3_set_bit(val, HNS3_VECTOR0_ALL_MSIX_ERR_B, enable ? 1 : 0);
5011 hns3_write_dev(hw, HNS3_VECTOR0_OTER_EN_REG, val);
5015 hns3_set_firber_default_support_speed(struct hns3_hw *hw)
5017 struct hns3_mac *mac = &hw->mac;
5019 switch (mac->link_speed) {
5020 case ETH_SPEED_NUM_1G:
5021 return HNS3_FIBER_LINK_SPEED_1G_BIT;
5022 case ETH_SPEED_NUM_10G:
5023 return HNS3_FIBER_LINK_SPEED_10G_BIT;
5024 case ETH_SPEED_NUM_25G:
5025 return HNS3_FIBER_LINK_SPEED_25G_BIT;
5026 case ETH_SPEED_NUM_40G:
5027 return HNS3_FIBER_LINK_SPEED_40G_BIT;
5028 case ETH_SPEED_NUM_50G:
5029 return HNS3_FIBER_LINK_SPEED_50G_BIT;
5030 case ETH_SPEED_NUM_100G:
5031 return HNS3_FIBER_LINK_SPEED_100G_BIT;
5032 case ETH_SPEED_NUM_200G:
5033 return HNS3_FIBER_LINK_SPEED_200G_BIT;
5035 hns3_warn(hw, "invalid speed %u Mbps.", mac->link_speed);
5041 * Validity of supported_speed for firber and copper media type can be
5042 * guaranteed by the following policy:
5044 * Although the initialization of the phy in the firmware may not be
5045 * completed, the firmware can guarantees that the supported_speed is
5048 * If the version of firmware supports the acitive query way of the
5049 * HNS3_OPC_GET_SFP_INFO opcode, the supported_speed can be obtained
5050 * through it. If unsupported, use the SFP's speed as the value of the
5054 hns3_get_port_supported_speed(struct rte_eth_dev *eth_dev)
5056 struct hns3_adapter *hns = eth_dev->data->dev_private;
5057 struct hns3_hw *hw = &hns->hw;
5058 struct hns3_mac *mac = &hw->mac;
5061 ret = hns3_update_link_info(eth_dev);
5065 if (mac->media_type == HNS3_MEDIA_TYPE_FIBER) {
5067 * Some firmware does not support the report of supported_speed,
5068 * and only report the effective speed of SFP. In this case, it
5069 * is necessary to use the SFP's speed as the supported_speed.
5071 if (mac->supported_speed == 0)
5072 mac->supported_speed =
5073 hns3_set_firber_default_support_speed(hw);
5080 hns3_get_fc_autoneg_capability(struct hns3_adapter *hns)
5082 struct hns3_mac *mac = &hns->hw.mac;
5084 if (mac->media_type == HNS3_MEDIA_TYPE_COPPER) {
5085 hns->pf.support_fc_autoneg = true;
5090 * Flow control auto-negotiation requires the cooperation of the driver
5091 * and firmware. Currently, the optical port does not support flow
5092 * control auto-negotiation.
5094 hns->pf.support_fc_autoneg = false;
5098 hns3_init_pf(struct rte_eth_dev *eth_dev)
5100 struct rte_device *dev = eth_dev->device;
5101 struct rte_pci_device *pci_dev = RTE_DEV_TO_PCI(dev);
5102 struct hns3_adapter *hns = eth_dev->data->dev_private;
5103 struct hns3_hw *hw = &hns->hw;
5106 PMD_INIT_FUNC_TRACE();
5108 /* Get hardware io base address from pcie BAR2 IO space */
5109 hw->io_base = pci_dev->mem_resource[2].addr;
5111 /* Firmware command queue initialize */
5112 ret = hns3_cmd_init_queue(hw);
5114 PMD_INIT_LOG(ERR, "Failed to init cmd queue: %d", ret);
5115 goto err_cmd_init_queue;
5118 hns3_clear_all_event_cause(hw);
5120 /* Firmware command initialize */
5121 ret = hns3_cmd_init(hw);
5123 PMD_INIT_LOG(ERR, "Failed to init cmd: %d", ret);
5128 * To ensure that the hardware environment is clean during
5129 * initialization, the driver actively clear the hardware environment
5130 * during initialization, including PF and corresponding VFs' vlan, mac,
5131 * flow table configurations, etc.
5133 ret = hns3_clear_hw(hw);
5135 PMD_INIT_LOG(ERR, "failed to clear hardware: %d", ret);
5139 /* Hardware statistics of imissed registers cleared. */
5140 ret = hns3_update_imissed_stats(hw, true);
5142 hns3_err(hw, "clear imissed stats failed, ret = %d", ret);
5146 hns3_config_all_msix_error(hw, true);
5148 ret = rte_intr_callback_register(&pci_dev->intr_handle,
5149 hns3_interrupt_handler,
5152 PMD_INIT_LOG(ERR, "Failed to register intr: %d", ret);
5153 goto err_intr_callback_register;
5156 ret = hns3_ptp_init(hw);
5158 goto err_get_config;
5160 /* Enable interrupt */
5161 rte_intr_enable(&pci_dev->intr_handle);
5162 hns3_pf_enable_irq0(hw);
5164 /* Get configuration */
5165 ret = hns3_get_configuration(hw);
5167 PMD_INIT_LOG(ERR, "Failed to fetch configuration: %d", ret);
5168 goto err_get_config;
5171 ret = hns3_tqp_stats_init(hw);
5173 goto err_get_config;
5175 ret = hns3_init_hardware(hns);
5177 PMD_INIT_LOG(ERR, "Failed to init hardware: %d", ret);
5181 /* Initialize flow director filter list & hash */
5182 ret = hns3_fdir_filter_init(hns);
5184 PMD_INIT_LOG(ERR, "Failed to alloc hashmap for fdir: %d", ret);
5188 hns3_rss_set_default_args(hw);
5190 ret = hns3_enable_hw_error_intr(hns, true);
5192 PMD_INIT_LOG(ERR, "fail to enable hw error interrupts: %d",
5194 goto err_enable_intr;
5197 ret = hns3_get_port_supported_speed(eth_dev);
5199 PMD_INIT_LOG(ERR, "failed to get speed capabilities supported "
5200 "by device, ret = %d.", ret);
5201 goto err_supported_speed;
5204 hns3_get_fc_autoneg_capability(hns);
5206 hns3_tm_conf_init(eth_dev);
5210 err_supported_speed:
5211 (void)hns3_enable_hw_error_intr(hns, false);
5213 hns3_fdir_filter_uninit(hns);
5215 hns3_uninit_umv_space(hw);
5217 hns3_tqp_stats_uninit(hw);
5219 hns3_pf_disable_irq0(hw);
5220 rte_intr_disable(&pci_dev->intr_handle);
5221 hns3_intr_unregister(&pci_dev->intr_handle, hns3_interrupt_handler,
5223 err_intr_callback_register:
5225 hns3_cmd_uninit(hw);
5226 hns3_cmd_destroy_queue(hw);
5234 hns3_uninit_pf(struct rte_eth_dev *eth_dev)
5236 struct hns3_adapter *hns = eth_dev->data->dev_private;
5237 struct rte_device *dev = eth_dev->device;
5238 struct rte_pci_device *pci_dev = RTE_DEV_TO_PCI(dev);
5239 struct hns3_hw *hw = &hns->hw;
5241 PMD_INIT_FUNC_TRACE();
5243 hns3_tm_conf_uninit(eth_dev);
5244 hns3_enable_hw_error_intr(hns, false);
5245 hns3_rss_uninit(hns);
5246 (void)hns3_config_gro(hw, false);
5247 hns3_promisc_uninit(hw);
5248 hns3_fdir_filter_uninit(hns);
5249 hns3_uninit_umv_space(hw);
5250 hns3_tqp_stats_uninit(hw);
5251 hns3_config_mac_tnl_int(hw, false);
5252 hns3_pf_disable_irq0(hw);
5253 rte_intr_disable(&pci_dev->intr_handle);
5254 hns3_intr_unregister(&pci_dev->intr_handle, hns3_interrupt_handler,
5256 hns3_config_all_msix_error(hw, false);
5257 hns3_cmd_uninit(hw);
5258 hns3_cmd_destroy_queue(hw);
5263 hns3_convert_link_speeds2bitmap_copper(uint32_t link_speeds)
5267 switch (link_speeds & ~ETH_LINK_SPEED_FIXED) {
5268 case ETH_LINK_SPEED_10M:
5269 speed_bit = HNS3_PHY_LINK_SPEED_10M_BIT;
5271 case ETH_LINK_SPEED_10M_HD:
5272 speed_bit = HNS3_PHY_LINK_SPEED_10M_HD_BIT;
5274 case ETH_LINK_SPEED_100M:
5275 speed_bit = HNS3_PHY_LINK_SPEED_100M_BIT;
5277 case ETH_LINK_SPEED_100M_HD:
5278 speed_bit = HNS3_PHY_LINK_SPEED_100M_HD_BIT;
5280 case ETH_LINK_SPEED_1G:
5281 speed_bit = HNS3_PHY_LINK_SPEED_1000M_BIT;
5292 hns3_convert_link_speeds2bitmap_fiber(uint32_t link_speeds)
5296 switch (link_speeds & ~ETH_LINK_SPEED_FIXED) {
5297 case ETH_LINK_SPEED_1G:
5298 speed_bit = HNS3_FIBER_LINK_SPEED_1G_BIT;
5300 case ETH_LINK_SPEED_10G:
5301 speed_bit = HNS3_FIBER_LINK_SPEED_10G_BIT;
5303 case ETH_LINK_SPEED_25G:
5304 speed_bit = HNS3_FIBER_LINK_SPEED_25G_BIT;
5306 case ETH_LINK_SPEED_40G:
5307 speed_bit = HNS3_FIBER_LINK_SPEED_40G_BIT;
5309 case ETH_LINK_SPEED_50G:
5310 speed_bit = HNS3_FIBER_LINK_SPEED_50G_BIT;
5312 case ETH_LINK_SPEED_100G:
5313 speed_bit = HNS3_FIBER_LINK_SPEED_100G_BIT;
5315 case ETH_LINK_SPEED_200G:
5316 speed_bit = HNS3_FIBER_LINK_SPEED_200G_BIT;
5327 hns3_check_port_speed(struct hns3_hw *hw, uint32_t link_speeds)
5329 struct hns3_mac *mac = &hw->mac;
5330 uint32_t supported_speed = mac->supported_speed;
5331 uint32_t speed_bit = 0;
5333 if (mac->media_type == HNS3_MEDIA_TYPE_COPPER)
5334 speed_bit = hns3_convert_link_speeds2bitmap_copper(link_speeds);
5335 else if (mac->media_type == HNS3_MEDIA_TYPE_FIBER)
5336 speed_bit = hns3_convert_link_speeds2bitmap_fiber(link_speeds);
5338 if (!(speed_bit & supported_speed)) {
5339 hns3_err(hw, "link_speeds(0x%x) exceeds the supported speed capability or is incorrect.",
5347 static inline uint32_t
5348 hns3_get_link_speed(uint32_t link_speeds)
5350 uint32_t speed = ETH_SPEED_NUM_NONE;
5352 if (link_speeds & ETH_LINK_SPEED_10M ||
5353 link_speeds & ETH_LINK_SPEED_10M_HD)
5354 speed = ETH_SPEED_NUM_10M;
5355 if (link_speeds & ETH_LINK_SPEED_100M ||
5356 link_speeds & ETH_LINK_SPEED_100M_HD)
5357 speed = ETH_SPEED_NUM_100M;
5358 if (link_speeds & ETH_LINK_SPEED_1G)
5359 speed = ETH_SPEED_NUM_1G;
5360 if (link_speeds & ETH_LINK_SPEED_10G)
5361 speed = ETH_SPEED_NUM_10G;
5362 if (link_speeds & ETH_LINK_SPEED_25G)
5363 speed = ETH_SPEED_NUM_25G;
5364 if (link_speeds & ETH_LINK_SPEED_40G)
5365 speed = ETH_SPEED_NUM_40G;
5366 if (link_speeds & ETH_LINK_SPEED_50G)
5367 speed = ETH_SPEED_NUM_50G;
5368 if (link_speeds & ETH_LINK_SPEED_100G)
5369 speed = ETH_SPEED_NUM_100G;
5370 if (link_speeds & ETH_LINK_SPEED_200G)
5371 speed = ETH_SPEED_NUM_200G;
5377 hns3_get_link_duplex(uint32_t link_speeds)
5379 if ((link_speeds & ETH_LINK_SPEED_10M_HD) ||
5380 (link_speeds & ETH_LINK_SPEED_100M_HD))
5381 return ETH_LINK_HALF_DUPLEX;
5383 return ETH_LINK_FULL_DUPLEX;
5387 hns3_set_copper_port_link_speed(struct hns3_hw *hw,
5388 struct hns3_set_link_speed_cfg *cfg)
5390 struct hns3_cmd_desc desc[HNS3_PHY_PARAM_CFG_BD_NUM];
5391 struct hns3_phy_params_bd0_cmd *req;
5394 for (i = 0; i < HNS3_PHY_PARAM_CFG_BD_NUM - 1; i++) {
5395 hns3_cmd_setup_basic_desc(&desc[i], HNS3_OPC_PHY_PARAM_CFG,
5397 desc[i].flag |= rte_cpu_to_le_16(HNS3_CMD_FLAG_NEXT);
5399 hns3_cmd_setup_basic_desc(&desc[i], HNS3_OPC_PHY_PARAM_CFG, false);
5400 req = (struct hns3_phy_params_bd0_cmd *)desc[0].data;
5401 req->autoneg = cfg->autoneg;
5404 * The full speed capability is used to negotiate when
5405 * auto-negotiation is enabled.
5408 req->advertising = HNS3_PHY_LINK_SPEED_10M_BIT |
5409 HNS3_PHY_LINK_SPEED_10M_HD_BIT |
5410 HNS3_PHY_LINK_SPEED_100M_BIT |
5411 HNS3_PHY_LINK_SPEED_100M_HD_BIT |
5412 HNS3_PHY_LINK_SPEED_1000M_BIT;
5414 req->speed = cfg->speed;
5415 req->duplex = cfg->duplex;
5418 return hns3_cmd_send(hw, desc, HNS3_PHY_PARAM_CFG_BD_NUM);
5422 hns3_set_autoneg(struct hns3_hw *hw, bool enable)
5424 struct hns3_config_auto_neg_cmd *req;
5425 struct hns3_cmd_desc desc;
5429 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_CONFIG_AN_MODE, false);
5431 req = (struct hns3_config_auto_neg_cmd *)desc.data;
5433 hns3_set_bit(flag, HNS3_MAC_CFG_AN_EN_B, 1);
5434 req->cfg_an_cmd_flag = rte_cpu_to_le_32(flag);
5436 ret = hns3_cmd_send(hw, &desc, 1);
5438 hns3_err(hw, "autoneg set cmd failed, ret = %d.", ret);
5444 hns3_set_fiber_port_link_speed(struct hns3_hw *hw,
5445 struct hns3_set_link_speed_cfg *cfg)
5449 if (hw->mac.support_autoneg) {
5450 ret = hns3_set_autoneg(hw, cfg->autoneg);
5452 hns3_err(hw, "failed to configure auto-negotiation.");
5457 * To enable auto-negotiation, we only need to open the switch
5458 * of auto-negotiation, then firmware sets all speed
5466 * Some hardware doesn't support auto-negotiation, but users may not
5467 * configure link_speeds (default 0), which means auto-negotiation
5468 * In this case, a warning message need to be printed, instead of
5472 hns3_warn(hw, "auto-negotiation is not supported.");
5476 return hns3_cfg_mac_speed_dup(hw, cfg->speed, cfg->duplex);
5480 hns3_set_port_link_speed(struct hns3_hw *hw,
5481 struct hns3_set_link_speed_cfg *cfg)
5485 if (hw->mac.media_type == HNS3_MEDIA_TYPE_COPPER) {
5486 #if defined(RTE_HNS3_ONLY_1630_FPGA)
5487 struct hns3_pf *pf = HNS3_DEV_HW_TO_PF(hw);
5492 ret = hns3_set_copper_port_link_speed(hw, cfg);
5494 hns3_err(hw, "failed to set copper port link speed,"
5498 } else if (hw->mac.media_type == HNS3_MEDIA_TYPE_FIBER) {
5499 ret = hns3_set_fiber_port_link_speed(hw, cfg);
5501 hns3_err(hw, "failed to set fiber port link speed,"
5511 hns3_apply_link_speed(struct hns3_hw *hw)
5513 struct rte_eth_conf *conf = &hw->data->dev_conf;
5514 struct hns3_set_link_speed_cfg cfg;
5517 memset(&cfg, 0, sizeof(struct hns3_set_link_speed_cfg));
5518 cfg.autoneg = (conf->link_speeds == ETH_LINK_SPEED_AUTONEG) ?
5519 ETH_LINK_AUTONEG : ETH_LINK_FIXED;
5520 if (cfg.autoneg != ETH_LINK_AUTONEG) {
5521 ret = hns3_check_port_speed(hw, conf->link_speeds);
5525 cfg.speed = hns3_get_link_speed(conf->link_speeds);
5526 cfg.duplex = hns3_get_link_duplex(conf->link_speeds);
5529 return hns3_set_port_link_speed(hw, &cfg);
5533 hns3_do_start(struct hns3_adapter *hns, bool reset_queue)
5535 struct hns3_hw *hw = &hns->hw;
5538 ret = hns3_dcb_cfg_update(hns);
5543 * The hns3_dcb_cfg_update may configure TM module, so
5544 * hns3_tm_conf_update must called later.
5546 ret = hns3_tm_conf_update(hw);
5548 PMD_INIT_LOG(ERR, "failed to update tm conf, ret = %d.", ret);
5552 hns3_enable_rxd_adv_layout(hw);
5554 ret = hns3_init_queues(hns, reset_queue);
5556 PMD_INIT_LOG(ERR, "failed to init queues, ret = %d.", ret);
5560 ret = hns3_cfg_mac_mode(hw, true);
5562 PMD_INIT_LOG(ERR, "failed to enable MAC, ret = %d", ret);
5563 goto err_config_mac_mode;
5566 ret = hns3_apply_link_speed(hw);
5568 goto err_config_mac_mode;
5572 err_config_mac_mode:
5573 (void)hns3_cfg_mac_mode(hw, false);
5574 hns3_dev_release_mbufs(hns);
5576 * Here is exception handling, hns3_reset_all_tqps will have the
5577 * corresponding error message if it is handled incorrectly, so it is
5578 * not necessary to check hns3_reset_all_tqps return value, here keep
5579 * ret as the error code causing the exception.
5581 (void)hns3_reset_all_tqps(hns);
5586 hns3_map_rx_interrupt(struct rte_eth_dev *dev)
5588 struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
5589 struct rte_intr_handle *intr_handle = &pci_dev->intr_handle;
5590 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
5591 uint16_t base = RTE_INTR_VEC_ZERO_OFFSET;
5592 uint16_t vec = RTE_INTR_VEC_ZERO_OFFSET;
5593 uint32_t intr_vector;
5598 * hns3 needs a separate interrupt to be used as event interrupt which
5599 * could not be shared with task queue pair, so KERNEL drivers need
5600 * support multiple interrupt vectors.
5602 if (dev->data->dev_conf.intr_conf.rxq == 0 ||
5603 !rte_intr_cap_multiple(intr_handle))
5606 rte_intr_disable(intr_handle);
5607 intr_vector = hw->used_rx_queues;
5608 /* creates event fd for each intr vector when MSIX is used */
5609 if (rte_intr_efd_enable(intr_handle, intr_vector))
5612 if (intr_handle->intr_vec == NULL) {
5613 intr_handle->intr_vec =
5614 rte_zmalloc("intr_vec",
5615 hw->used_rx_queues * sizeof(int), 0);
5616 if (intr_handle->intr_vec == NULL) {
5617 hns3_err(hw, "failed to allocate %u rx_queues intr_vec",
5618 hw->used_rx_queues);
5620 goto alloc_intr_vec_error;
5624 if (rte_intr_allow_others(intr_handle)) {
5625 vec = RTE_INTR_VEC_RXTX_OFFSET;
5626 base = RTE_INTR_VEC_RXTX_OFFSET;
5629 for (q_id = 0; q_id < hw->used_rx_queues; q_id++) {
5630 ret = hns3_bind_ring_with_vector(hw, vec, true,
5631 HNS3_RING_TYPE_RX, q_id);
5633 goto bind_vector_error;
5634 intr_handle->intr_vec[q_id] = vec;
5636 * If there are not enough efds (e.g. not enough interrupt),
5637 * remaining queues will be bond to the last interrupt.
5639 if (vec < base + intr_handle->nb_efd - 1)
5642 rte_intr_enable(intr_handle);
5646 rte_free(intr_handle->intr_vec);
5647 intr_handle->intr_vec = NULL;
5648 alloc_intr_vec_error:
5649 rte_intr_efd_disable(intr_handle);
5654 hns3_restore_rx_interrupt(struct hns3_hw *hw)
5656 struct rte_eth_dev *dev = &rte_eth_devices[hw->data->port_id];
5657 struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
5658 struct rte_intr_handle *intr_handle = &pci_dev->intr_handle;
5662 if (dev->data->dev_conf.intr_conf.rxq == 0)
5665 if (rte_intr_dp_is_en(intr_handle)) {
5666 for (q_id = 0; q_id < hw->used_rx_queues; q_id++) {
5667 ret = hns3_bind_ring_with_vector(hw,
5668 intr_handle->intr_vec[q_id], true,
5669 HNS3_RING_TYPE_RX, q_id);
5679 hns3_restore_filter(struct rte_eth_dev *dev)
5681 hns3_restore_rss_filter(dev);
5685 hns3_dev_start(struct rte_eth_dev *dev)
5687 struct hns3_adapter *hns = dev->data->dev_private;
5688 struct hns3_hw *hw = &hns->hw;
5691 PMD_INIT_FUNC_TRACE();
5692 if (__atomic_load_n(&hw->reset.resetting, __ATOMIC_RELAXED))
5695 rte_spinlock_lock(&hw->lock);
5696 hw->adapter_state = HNS3_NIC_STARTING;
5698 ret = hns3_do_start(hns, true);
5700 hw->adapter_state = HNS3_NIC_CONFIGURED;
5701 rte_spinlock_unlock(&hw->lock);
5704 ret = hns3_map_rx_interrupt(dev);
5706 goto map_rx_inter_err;
5709 * There are three register used to control the status of a TQP
5710 * (contains a pair of Tx queue and Rx queue) in the new version network
5711 * engine. One is used to control the enabling of Tx queue, the other is
5712 * used to control the enabling of Rx queue, and the last is the master
5713 * switch used to control the enabling of the tqp. The Tx register and
5714 * TQP register must be enabled at the same time to enable a Tx queue.
5715 * The same applies to the Rx queue. For the older network engine, this
5716 * function only refresh the enabled flag, and it is used to update the
5717 * status of queue in the dpdk framework.
5719 ret = hns3_start_all_txqs(dev);
5721 goto map_rx_inter_err;
5723 ret = hns3_start_all_rxqs(dev);
5725 goto start_all_rxqs_fail;
5727 hw->adapter_state = HNS3_NIC_STARTED;
5728 rte_spinlock_unlock(&hw->lock);
5730 hns3_rx_scattered_calc(dev);
5731 hns3_set_rxtx_function(dev);
5732 hns3_mp_req_start_rxtx(dev);
5734 hns3_restore_filter(dev);
5736 /* Enable interrupt of all rx queues before enabling queues */
5737 hns3_dev_all_rx_queue_intr_enable(hw, true);
5740 * After finished the initialization, enable tqps to receive/transmit
5741 * packets and refresh all queue status.
5743 hns3_start_tqps(hw);
5745 hns3_tm_dev_start_proc(hw);
5747 if (dev->data->dev_conf.intr_conf.lsc != 0)
5748 hns3_dev_link_update(dev, 0);
5749 rte_eal_alarm_set(HNS3_SERVICE_INTERVAL, hns3_service_handler, dev);
5751 hns3_info(hw, "hns3 dev start successful!");
5755 start_all_rxqs_fail:
5756 hns3_stop_all_txqs(dev);
5758 (void)hns3_do_stop(hns);
5759 hw->adapter_state = HNS3_NIC_CONFIGURED;
5760 rte_spinlock_unlock(&hw->lock);
5766 hns3_do_stop(struct hns3_adapter *hns)
5768 struct hns3_hw *hw = &hns->hw;
5772 * The "hns3_do_stop" function will also be called by .stop_service to
5773 * prepare reset. At the time of global or IMP reset, the command cannot
5774 * be sent to stop the tx/rx queues. The mbuf in Tx/Rx queues may be
5775 * accessed during the reset process. So the mbuf can not be released
5776 * during reset and is required to be released after the reset is
5779 if (__atomic_load_n(&hw->reset.resetting, __ATOMIC_RELAXED) == 0)
5780 hns3_dev_release_mbufs(hns);
5782 ret = hns3_cfg_mac_mode(hw, false);
5785 hw->mac.link_status = ETH_LINK_DOWN;
5787 if (__atomic_load_n(&hw->reset.disable_cmd, __ATOMIC_RELAXED) == 0) {
5788 hns3_configure_all_mac_addr(hns, true);
5789 ret = hns3_reset_all_tqps(hns);
5791 hns3_err(hw, "failed to reset all queues ret = %d.",
5796 hw->mac.default_addr_setted = false;
5801 hns3_unmap_rx_interrupt(struct rte_eth_dev *dev)
5803 struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
5804 struct rte_intr_handle *intr_handle = &pci_dev->intr_handle;
5805 struct hns3_adapter *hns = dev->data->dev_private;
5806 struct hns3_hw *hw = &hns->hw;
5807 uint8_t base = RTE_INTR_VEC_ZERO_OFFSET;
5808 uint8_t vec = RTE_INTR_VEC_ZERO_OFFSET;
5811 if (dev->data->dev_conf.intr_conf.rxq == 0)
5814 /* unmap the ring with vector */
5815 if (rte_intr_allow_others(intr_handle)) {
5816 vec = RTE_INTR_VEC_RXTX_OFFSET;
5817 base = RTE_INTR_VEC_RXTX_OFFSET;
5819 if (rte_intr_dp_is_en(intr_handle)) {
5820 for (q_id = 0; q_id < hw->used_rx_queues; q_id++) {
5821 (void)hns3_bind_ring_with_vector(hw, vec, false,
5824 if (vec < base + intr_handle->nb_efd - 1)
5828 /* Clean datapath event and queue/vec mapping */
5829 rte_intr_efd_disable(intr_handle);
5830 if (intr_handle->intr_vec) {
5831 rte_free(intr_handle->intr_vec);
5832 intr_handle->intr_vec = NULL;
5837 hns3_dev_stop(struct rte_eth_dev *dev)
5839 struct hns3_adapter *hns = dev->data->dev_private;
5840 struct hns3_hw *hw = &hns->hw;
5842 PMD_INIT_FUNC_TRACE();
5843 dev->data->dev_started = 0;
5845 hw->adapter_state = HNS3_NIC_STOPPING;
5846 hns3_set_rxtx_function(dev);
5848 /* Disable datapath on secondary process. */
5849 hns3_mp_req_stop_rxtx(dev);
5850 /* Prevent crashes when queues are still in use. */
5851 rte_delay_ms(hw->tqps_num);
5853 rte_spinlock_lock(&hw->lock);
5854 if (__atomic_load_n(&hw->reset.resetting, __ATOMIC_RELAXED) == 0) {
5855 hns3_tm_dev_stop_proc(hw);
5856 hns3_config_mac_tnl_int(hw, false);
5859 hns3_unmap_rx_interrupt(dev);
5860 hw->adapter_state = HNS3_NIC_CONFIGURED;
5862 hns3_rx_scattered_reset(dev);
5863 rte_eal_alarm_cancel(hns3_service_handler, dev);
5864 hns3_stop_report_lse(dev);
5865 rte_spinlock_unlock(&hw->lock);
5871 hns3_dev_close(struct rte_eth_dev *eth_dev)
5873 struct hns3_adapter *hns = eth_dev->data->dev_private;
5874 struct hns3_hw *hw = &hns->hw;
5877 if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
5878 rte_free(eth_dev->process_private);
5879 eth_dev->process_private = NULL;
5883 if (hw->adapter_state == HNS3_NIC_STARTED)
5884 ret = hns3_dev_stop(eth_dev);
5886 hw->adapter_state = HNS3_NIC_CLOSING;
5887 hns3_reset_abort(hns);
5888 hw->adapter_state = HNS3_NIC_CLOSED;
5890 hns3_configure_all_mc_mac_addr(hns, true);
5891 hns3_remove_all_vlan_table(hns);
5892 hns3_vlan_txvlan_cfg(hns, HNS3_PORT_BASE_VLAN_DISABLE, 0);
5893 hns3_uninit_pf(eth_dev);
5894 hns3_free_all_queues(eth_dev);
5895 rte_free(hw->reset.wait_data);
5896 rte_free(eth_dev->process_private);
5897 eth_dev->process_private = NULL;
5898 hns3_mp_uninit_primary();
5899 hns3_warn(hw, "Close port %u finished", hw->data->port_id);
5905 hns3_get_autoneg_rxtx_pause_copper(struct hns3_hw *hw, bool *rx_pause,
5908 struct hns3_mac *mac = &hw->mac;
5909 uint32_t advertising = mac->advertising;
5910 uint32_t lp_advertising = mac->lp_advertising;
5914 if (advertising & lp_advertising & HNS3_PHY_LINK_MODE_PAUSE_BIT) {
5917 } else if (advertising & lp_advertising &
5918 HNS3_PHY_LINK_MODE_ASYM_PAUSE_BIT) {
5919 if (advertising & HNS3_PHY_LINK_MODE_PAUSE_BIT)
5921 else if (lp_advertising & HNS3_PHY_LINK_MODE_PAUSE_BIT)
5926 static enum hns3_fc_mode
5927 hns3_get_autoneg_fc_mode(struct hns3_hw *hw)
5929 enum hns3_fc_mode current_mode;
5930 bool rx_pause = false;
5931 bool tx_pause = false;
5933 switch (hw->mac.media_type) {
5934 case HNS3_MEDIA_TYPE_COPPER:
5935 hns3_get_autoneg_rxtx_pause_copper(hw, &rx_pause, &tx_pause);
5939 * Flow control auto-negotiation is not supported for fiber and
5940 * backpalne media type.
5942 case HNS3_MEDIA_TYPE_FIBER:
5943 case HNS3_MEDIA_TYPE_BACKPLANE:
5944 hns3_err(hw, "autoneg FC mode can't be obtained, but flow control auto-negotiation is enabled.");
5945 current_mode = hw->requested_fc_mode;
5948 hns3_err(hw, "autoneg FC mode can't be obtained for unknown media type(%u).",
5949 hw->mac.media_type);
5950 current_mode = HNS3_FC_NONE;
5954 if (rx_pause && tx_pause)
5955 current_mode = HNS3_FC_FULL;
5957 current_mode = HNS3_FC_RX_PAUSE;
5959 current_mode = HNS3_FC_TX_PAUSE;
5961 current_mode = HNS3_FC_NONE;
5964 return current_mode;
5967 static enum hns3_fc_mode
5968 hns3_get_current_fc_mode(struct rte_eth_dev *dev)
5970 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
5971 struct hns3_pf *pf = HNS3_DEV_PRIVATE_TO_PF(dev->data->dev_private);
5972 struct hns3_mac *mac = &hw->mac;
5975 * When the flow control mode is obtained, the device may not complete
5976 * auto-negotiation. It is necessary to wait for link establishment.
5978 (void)hns3_dev_link_update(dev, 1);
5981 * If the link auto-negotiation of the nic is disabled, or the flow
5982 * control auto-negotiation is not supported, the forced flow control
5985 if (mac->link_autoneg == 0 || !pf->support_fc_autoneg)
5986 return hw->requested_fc_mode;
5988 return hns3_get_autoneg_fc_mode(hw);
5992 hns3_flow_ctrl_get(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
5994 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
5995 struct hns3_pf *pf = HNS3_DEV_PRIVATE_TO_PF(dev->data->dev_private);
5996 enum hns3_fc_mode current_mode;
5998 current_mode = hns3_get_current_fc_mode(dev);
5999 switch (current_mode) {
6001 fc_conf->mode = RTE_FC_FULL;
6003 case HNS3_FC_TX_PAUSE:
6004 fc_conf->mode = RTE_FC_TX_PAUSE;
6006 case HNS3_FC_RX_PAUSE:
6007 fc_conf->mode = RTE_FC_RX_PAUSE;
6011 fc_conf->mode = RTE_FC_NONE;
6015 fc_conf->pause_time = pf->pause_time;
6016 fc_conf->autoneg = pf->support_fc_autoneg ? hw->mac.link_autoneg : 0;
6022 hns3_get_fc_mode(struct hns3_hw *hw, enum rte_eth_fc_mode mode)
6026 hw->requested_fc_mode = HNS3_FC_NONE;
6028 case RTE_FC_RX_PAUSE:
6029 hw->requested_fc_mode = HNS3_FC_RX_PAUSE;
6031 case RTE_FC_TX_PAUSE:
6032 hw->requested_fc_mode = HNS3_FC_TX_PAUSE;
6035 hw->requested_fc_mode = HNS3_FC_FULL;
6038 hw->requested_fc_mode = HNS3_FC_NONE;
6039 hns3_warn(hw, "fc_mode(%u) exceeds member scope and is "
6040 "configured to RTE_FC_NONE", mode);
6046 hns3_check_fc_autoneg_valid(struct hns3_hw *hw, uint8_t autoneg)
6048 struct hns3_pf *pf = HNS3_DEV_HW_TO_PF(hw);
6050 if (!pf->support_fc_autoneg) {
6052 hns3_err(hw, "unsupported fc auto-negotiation setting.");
6057 * Flow control auto-negotiation of the NIC is not supported,
6058 * but other auto-negotiation features may be supported.
6060 if (autoneg != hw->mac.link_autoneg) {
6061 hns3_err(hw, "please use 'link_speeds' in struct rte_eth_conf to disable autoneg!");
6069 * If flow control auto-negotiation of the NIC is supported, all
6070 * auto-negotiation features are supported.
6072 if (autoneg != hw->mac.link_autoneg) {
6073 hns3_err(hw, "please use 'link_speeds' in struct rte_eth_conf to change autoneg!");
6081 hns3_flow_ctrl_set(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
6083 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
6086 if (fc_conf->high_water || fc_conf->low_water ||
6087 fc_conf->send_xon || fc_conf->mac_ctrl_frame_fwd) {
6088 hns3_err(hw, "Unsupported flow control settings specified, "
6089 "high_water(%u), low_water(%u), send_xon(%u) and "
6090 "mac_ctrl_frame_fwd(%u) must be set to '0'",
6091 fc_conf->high_water, fc_conf->low_water,
6092 fc_conf->send_xon, fc_conf->mac_ctrl_frame_fwd);
6096 ret = hns3_check_fc_autoneg_valid(hw, fc_conf->autoneg);
6100 if (!fc_conf->pause_time) {
6101 hns3_err(hw, "Invalid pause time %u setting.",
6102 fc_conf->pause_time);
6106 if (!(hw->current_fc_status == HNS3_FC_STATUS_NONE ||
6107 hw->current_fc_status == HNS3_FC_STATUS_MAC_PAUSE)) {
6108 hns3_err(hw, "PFC is enabled. Cannot set MAC pause. "
6109 "current_fc_status = %d", hw->current_fc_status);
6113 if (hw->num_tc > 1) {
6114 hns3_err(hw, "in multi-TC scenarios, MAC pause is not supported.");
6118 hns3_get_fc_mode(hw, fc_conf->mode);
6120 rte_spinlock_lock(&hw->lock);
6121 ret = hns3_fc_enable(dev, fc_conf);
6122 rte_spinlock_unlock(&hw->lock);
6128 hns3_priority_flow_ctrl_set(struct rte_eth_dev *dev,
6129 struct rte_eth_pfc_conf *pfc_conf)
6131 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
6134 if (!hns3_dev_dcb_supported(hw)) {
6135 hns3_err(hw, "This port does not support dcb configurations.");
6139 if (pfc_conf->fc.high_water || pfc_conf->fc.low_water ||
6140 pfc_conf->fc.send_xon || pfc_conf->fc.mac_ctrl_frame_fwd) {
6141 hns3_err(hw, "Unsupported flow control settings specified, "
6142 "high_water(%u), low_water(%u), send_xon(%u) and "
6143 "mac_ctrl_frame_fwd(%u) must be set to '0'",
6144 pfc_conf->fc.high_water, pfc_conf->fc.low_water,
6145 pfc_conf->fc.send_xon,
6146 pfc_conf->fc.mac_ctrl_frame_fwd);
6149 if (pfc_conf->fc.autoneg) {
6150 hns3_err(hw, "Unsupported fc auto-negotiation setting.");
6153 if (pfc_conf->fc.pause_time == 0) {
6154 hns3_err(hw, "Invalid pause time %u setting.",
6155 pfc_conf->fc.pause_time);
6159 if (!(hw->current_fc_status == HNS3_FC_STATUS_NONE ||
6160 hw->current_fc_status == HNS3_FC_STATUS_PFC)) {
6161 hns3_err(hw, "MAC pause is enabled. Cannot set PFC."
6162 "current_fc_status = %d", hw->current_fc_status);
6166 hns3_get_fc_mode(hw, pfc_conf->fc.mode);
6168 rte_spinlock_lock(&hw->lock);
6169 ret = hns3_dcb_pfc_enable(dev, pfc_conf);
6170 rte_spinlock_unlock(&hw->lock);
6176 hns3_get_dcb_info(struct rte_eth_dev *dev, struct rte_eth_dcb_info *dcb_info)
6178 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
6179 struct hns3_pf *pf = HNS3_DEV_PRIVATE_TO_PF(dev->data->dev_private);
6180 enum rte_eth_rx_mq_mode mq_mode = dev->data->dev_conf.rxmode.mq_mode;
6183 rte_spinlock_lock(&hw->lock);
6184 if ((uint32_t)mq_mode & ETH_MQ_RX_DCB_FLAG)
6185 dcb_info->nb_tcs = pf->local_max_tc;
6187 dcb_info->nb_tcs = 1;
6189 for (i = 0; i < HNS3_MAX_USER_PRIO; i++)
6190 dcb_info->prio_tc[i] = hw->dcb_info.prio_tc[i];
6191 for (i = 0; i < dcb_info->nb_tcs; i++)
6192 dcb_info->tc_bws[i] = hw->dcb_info.pg_info[0].tc_dwrr[i];
6194 for (i = 0; i < hw->num_tc; i++) {
6195 dcb_info->tc_queue.tc_rxq[0][i].base = hw->alloc_rss_size * i;
6196 dcb_info->tc_queue.tc_txq[0][i].base =
6197 hw->tc_queue[i].tqp_offset;
6198 dcb_info->tc_queue.tc_rxq[0][i].nb_queue = hw->alloc_rss_size;
6199 dcb_info->tc_queue.tc_txq[0][i].nb_queue =
6200 hw->tc_queue[i].tqp_count;
6202 rte_spinlock_unlock(&hw->lock);
6208 hns3_reinit_dev(struct hns3_adapter *hns)
6210 struct hns3_hw *hw = &hns->hw;
6213 ret = hns3_cmd_init(hw);
6215 hns3_err(hw, "Failed to init cmd: %d", ret);
6219 ret = hns3_reset_all_tqps(hns);
6221 hns3_err(hw, "Failed to reset all queues: %d", ret);
6225 ret = hns3_init_hardware(hns);
6227 hns3_err(hw, "Failed to init hardware: %d", ret);
6231 ret = hns3_enable_hw_error_intr(hns, true);
6233 hns3_err(hw, "fail to enable hw error interrupts: %d",
6237 hns3_info(hw, "Reset done, driver initialization finished.");
6243 is_pf_reset_done(struct hns3_hw *hw)
6245 uint32_t val, reg, reg_bit;
6247 switch (hw->reset.level) {
6248 case HNS3_IMP_RESET:
6249 reg = HNS3_GLOBAL_RESET_REG;
6250 reg_bit = HNS3_IMP_RESET_BIT;
6252 case HNS3_GLOBAL_RESET:
6253 reg = HNS3_GLOBAL_RESET_REG;
6254 reg_bit = HNS3_GLOBAL_RESET_BIT;
6256 case HNS3_FUNC_RESET:
6257 reg = HNS3_FUN_RST_ING;
6258 reg_bit = HNS3_FUN_RST_ING_B;
6260 case HNS3_FLR_RESET:
6262 hns3_err(hw, "Wait for unsupported reset level: %d",
6266 val = hns3_read_dev(hw, reg);
6267 if (hns3_get_bit(val, reg_bit))
6274 hns3_is_reset_pending(struct hns3_adapter *hns)
6276 struct hns3_hw *hw = &hns->hw;
6277 enum hns3_reset_level reset;
6279 hns3_check_event_cause(hns, NULL);
6280 reset = hns3_get_reset_level(hns, &hw->reset.pending);
6282 if (reset != HNS3_NONE_RESET && hw->reset.level != HNS3_NONE_RESET &&
6283 hw->reset.level < reset) {
6284 hns3_warn(hw, "High level reset %d is pending", reset);
6287 reset = hns3_get_reset_level(hns, &hw->reset.request);
6288 if (reset != HNS3_NONE_RESET && hw->reset.level != HNS3_NONE_RESET &&
6289 hw->reset.level < reset) {
6290 hns3_warn(hw, "High level reset %d is request", reset);
6297 hns3_wait_hardware_ready(struct hns3_adapter *hns)
6299 struct hns3_hw *hw = &hns->hw;
6300 struct hns3_wait_data *wait_data = hw->reset.wait_data;
6303 if (wait_data->result == HNS3_WAIT_SUCCESS)
6305 else if (wait_data->result == HNS3_WAIT_TIMEOUT) {
6306 gettimeofday(&tv, NULL);
6307 hns3_warn(hw, "Reset step4 hardware not ready after reset time=%ld.%.6ld",
6308 tv.tv_sec, tv.tv_usec);
6310 } else if (wait_data->result == HNS3_WAIT_REQUEST)
6313 wait_data->hns = hns;
6314 wait_data->check_completion = is_pf_reset_done;
6315 wait_data->end_ms = (uint64_t)HNS3_RESET_WAIT_CNT *
6316 HNS3_RESET_WAIT_MS + get_timeofday_ms();
6317 wait_data->interval = HNS3_RESET_WAIT_MS * USEC_PER_MSEC;
6318 wait_data->count = HNS3_RESET_WAIT_CNT;
6319 wait_data->result = HNS3_WAIT_REQUEST;
6320 rte_eal_alarm_set(wait_data->interval, hns3_wait_callback, wait_data);
6325 hns3_func_reset_cmd(struct hns3_hw *hw, int func_id)
6327 struct hns3_cmd_desc desc;
6328 struct hns3_reset_cmd *req = (struct hns3_reset_cmd *)desc.data;
6330 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_CFG_RST_TRIGGER, false);
6331 hns3_set_bit(req->mac_func_reset, HNS3_CFG_RESET_FUNC_B, 1);
6332 req->fun_reset_vfid = func_id;
6334 return hns3_cmd_send(hw, &desc, 1);
6338 hns3_imp_reset_cmd(struct hns3_hw *hw)
6340 struct hns3_cmd_desc desc;
6342 hns3_cmd_setup_basic_desc(&desc, 0xFFFE, false);
6343 desc.data[0] = 0xeedd;
6345 return hns3_cmd_send(hw, &desc, 1);
6349 hns3_msix_process(struct hns3_adapter *hns, enum hns3_reset_level reset_level)
6351 struct hns3_hw *hw = &hns->hw;
6355 gettimeofday(&tv, NULL);
6356 if (hns3_read_dev(hw, HNS3_GLOBAL_RESET_REG) ||
6357 hns3_read_dev(hw, HNS3_FUN_RST_ING)) {
6358 hns3_warn(hw, "Don't process msix during resetting time=%ld.%.6ld",
6359 tv.tv_sec, tv.tv_usec);
6363 switch (reset_level) {
6364 case HNS3_IMP_RESET:
6365 hns3_imp_reset_cmd(hw);
6366 hns3_warn(hw, "IMP Reset requested time=%ld.%.6ld",
6367 tv.tv_sec, tv.tv_usec);
6369 case HNS3_GLOBAL_RESET:
6370 val = hns3_read_dev(hw, HNS3_GLOBAL_RESET_REG);
6371 hns3_set_bit(val, HNS3_GLOBAL_RESET_BIT, 1);
6372 hns3_write_dev(hw, HNS3_GLOBAL_RESET_REG, val);
6373 hns3_warn(hw, "Global Reset requested time=%ld.%.6ld",
6374 tv.tv_sec, tv.tv_usec);
6376 case HNS3_FUNC_RESET:
6377 hns3_warn(hw, "PF Reset requested time=%ld.%.6ld",
6378 tv.tv_sec, tv.tv_usec);
6379 /* schedule again to check later */
6380 hns3_atomic_set_bit(HNS3_FUNC_RESET, &hw->reset.pending);
6381 hns3_schedule_reset(hns);
6384 hns3_warn(hw, "Unsupported reset level: %d", reset_level);
6387 hns3_atomic_clear_bit(reset_level, &hw->reset.request);
6390 static enum hns3_reset_level
6391 hns3_get_reset_level(struct hns3_adapter *hns, uint64_t *levels)
6393 struct hns3_hw *hw = &hns->hw;
6394 enum hns3_reset_level reset_level = HNS3_NONE_RESET;
6396 /* Return the highest priority reset level amongst all */
6397 if (hns3_atomic_test_bit(HNS3_IMP_RESET, levels))
6398 reset_level = HNS3_IMP_RESET;
6399 else if (hns3_atomic_test_bit(HNS3_GLOBAL_RESET, levels))
6400 reset_level = HNS3_GLOBAL_RESET;
6401 else if (hns3_atomic_test_bit(HNS3_FUNC_RESET, levels))
6402 reset_level = HNS3_FUNC_RESET;
6403 else if (hns3_atomic_test_bit(HNS3_FLR_RESET, levels))
6404 reset_level = HNS3_FLR_RESET;
6406 if (hw->reset.level != HNS3_NONE_RESET && reset_level < hw->reset.level)
6407 return HNS3_NONE_RESET;
6413 hns3_record_imp_error(struct hns3_adapter *hns)
6415 struct hns3_hw *hw = &hns->hw;
6418 reg_val = hns3_read_dev(hw, HNS3_VECTOR0_OTER_EN_REG);
6419 if (hns3_get_bit(reg_val, HNS3_VECTOR0_IMP_RD_POISON_B)) {
6420 hns3_warn(hw, "Detected IMP RD poison!");
6421 hns3_set_bit(reg_val, HNS3_VECTOR0_IMP_RD_POISON_B, 0);
6422 hns3_write_dev(hw, HNS3_VECTOR0_OTER_EN_REG, reg_val);
6425 if (hns3_get_bit(reg_val, HNS3_VECTOR0_IMP_CMDQ_ERR_B)) {
6426 hns3_warn(hw, "Detected IMP CMDQ error!");
6427 hns3_set_bit(reg_val, HNS3_VECTOR0_IMP_CMDQ_ERR_B, 0);
6428 hns3_write_dev(hw, HNS3_VECTOR0_OTER_EN_REG, reg_val);
6433 hns3_prepare_reset(struct hns3_adapter *hns)
6435 struct hns3_hw *hw = &hns->hw;
6439 switch (hw->reset.level) {
6440 case HNS3_FUNC_RESET:
6441 ret = hns3_func_reset_cmd(hw, HNS3_PF_FUNC_ID);
6446 * After performaning pf reset, it is not necessary to do the
6447 * mailbox handling or send any command to firmware, because
6448 * any mailbox handling or command to firmware is only valid
6449 * after hns3_cmd_init is called.
6451 __atomic_store_n(&hw->reset.disable_cmd, 1, __ATOMIC_RELAXED);
6452 hw->reset.stats.request_cnt++;
6454 case HNS3_IMP_RESET:
6455 hns3_record_imp_error(hns);
6456 reg_val = hns3_read_dev(hw, HNS3_VECTOR0_OTER_EN_REG);
6457 hns3_write_dev(hw, HNS3_VECTOR0_OTER_EN_REG, reg_val |
6458 BIT(HNS3_VECTOR0_IMP_RESET_INT_B));
6467 hns3_set_rst_done(struct hns3_hw *hw)
6469 struct hns3_pf_rst_done_cmd *req;
6470 struct hns3_cmd_desc desc;
6472 req = (struct hns3_pf_rst_done_cmd *)desc.data;
6473 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_PF_RST_DONE, false);
6474 req->pf_rst_done |= HNS3_PF_RESET_DONE_BIT;
6475 return hns3_cmd_send(hw, &desc, 1);
6479 hns3_stop_service(struct hns3_adapter *hns)
6481 struct hns3_hw *hw = &hns->hw;
6482 struct rte_eth_dev *eth_dev;
6484 eth_dev = &rte_eth_devices[hw->data->port_id];
6485 hw->mac.link_status = ETH_LINK_DOWN;
6486 if (hw->adapter_state == HNS3_NIC_STARTED) {
6487 rte_eal_alarm_cancel(hns3_service_handler, eth_dev);
6488 hns3_update_linkstatus_and_event(hw, false);
6491 hns3_set_rxtx_function(eth_dev);
6493 /* Disable datapath on secondary process. */
6494 hns3_mp_req_stop_rxtx(eth_dev);
6495 rte_delay_ms(hw->tqps_num);
6497 rte_spinlock_lock(&hw->lock);
6498 if (hns->hw.adapter_state == HNS3_NIC_STARTED ||
6499 hw->adapter_state == HNS3_NIC_STOPPING) {
6500 hns3_enable_all_queues(hw, false);
6502 hw->reset.mbuf_deferred_free = true;
6504 hw->reset.mbuf_deferred_free = false;
6507 * It is cumbersome for hardware to pick-and-choose entries for deletion
6508 * from table space. Hence, for function reset software intervention is
6509 * required to delete the entries
6511 if (__atomic_load_n(&hw->reset.disable_cmd, __ATOMIC_RELAXED) == 0)
6512 hns3_configure_all_mc_mac_addr(hns, true);
6513 rte_spinlock_unlock(&hw->lock);
6519 hns3_start_service(struct hns3_adapter *hns)
6521 struct hns3_hw *hw = &hns->hw;
6522 struct rte_eth_dev *eth_dev;
6524 if (hw->reset.level == HNS3_IMP_RESET ||
6525 hw->reset.level == HNS3_GLOBAL_RESET)
6526 hns3_set_rst_done(hw);
6527 eth_dev = &rte_eth_devices[hw->data->port_id];
6528 hns3_set_rxtx_function(eth_dev);
6529 hns3_mp_req_start_rxtx(eth_dev);
6530 if (hw->adapter_state == HNS3_NIC_STARTED) {
6532 * This API parent function already hold the hns3_hw.lock, the
6533 * hns3_service_handler may report lse, in bonding application
6534 * it will call driver's ops which may acquire the hns3_hw.lock
6535 * again, thus lead to deadlock.
6536 * We defer calls hns3_service_handler to avoid the deadlock.
6538 rte_eal_alarm_set(HNS3_SERVICE_QUICK_INTERVAL,
6539 hns3_service_handler, eth_dev);
6541 /* Enable interrupt of all rx queues before enabling queues */
6542 hns3_dev_all_rx_queue_intr_enable(hw, true);
6544 * Enable state of each rxq and txq will be recovered after
6545 * reset, so we need to restore them before enable all tqps;
6547 hns3_restore_tqp_enable_state(hw);
6549 * When finished the initialization, enable queues to receive
6550 * and transmit packets.
6552 hns3_enable_all_queues(hw, true);
6559 hns3_restore_conf(struct hns3_adapter *hns)
6561 struct hns3_hw *hw = &hns->hw;
6564 ret = hns3_configure_all_mac_addr(hns, false);
6568 ret = hns3_configure_all_mc_mac_addr(hns, false);
6572 ret = hns3_dev_promisc_restore(hns);
6576 ret = hns3_restore_vlan_table(hns);
6580 ret = hns3_restore_vlan_conf(hns);
6584 ret = hns3_restore_all_fdir_filter(hns);
6588 ret = hns3_restore_ptp(hns);
6592 ret = hns3_restore_rx_interrupt(hw);
6596 ret = hns3_restore_gro_conf(hw);
6600 ret = hns3_restore_fec(hw);
6604 if (hns->hw.adapter_state == HNS3_NIC_STARTED) {
6605 ret = hns3_do_start(hns, false);
6608 hns3_info(hw, "hns3 dev restart successful!");
6609 } else if (hw->adapter_state == HNS3_NIC_STOPPING)
6610 hw->adapter_state = HNS3_NIC_CONFIGURED;
6614 hns3_configure_all_mc_mac_addr(hns, true);
6616 hns3_configure_all_mac_addr(hns, true);
6621 hns3_reset_service(void *param)
6623 struct hns3_adapter *hns = (struct hns3_adapter *)param;
6624 struct hns3_hw *hw = &hns->hw;
6625 enum hns3_reset_level reset_level;
6626 struct timeval tv_delta;
6627 struct timeval tv_start;
6633 * The interrupt is not triggered within the delay time.
6634 * The interrupt may have been lost. It is necessary to handle
6635 * the interrupt to recover from the error.
6637 if (__atomic_load_n(&hw->reset.schedule, __ATOMIC_RELAXED) ==
6638 SCHEDULE_DEFERRED) {
6639 __atomic_store_n(&hw->reset.schedule, SCHEDULE_REQUESTED,
6641 hns3_err(hw, "Handling interrupts in delayed tasks");
6642 hns3_interrupt_handler(&rte_eth_devices[hw->data->port_id]);
6643 reset_level = hns3_get_reset_level(hns, &hw->reset.pending);
6644 if (reset_level == HNS3_NONE_RESET) {
6645 hns3_err(hw, "No reset level is set, try IMP reset");
6646 hns3_atomic_set_bit(HNS3_IMP_RESET, &hw->reset.pending);
6649 __atomic_store_n(&hw->reset.schedule, SCHEDULE_NONE, __ATOMIC_RELAXED);
6652 * Check if there is any ongoing reset in the hardware. This status can
6653 * be checked from reset_pending. If there is then, we need to wait for
6654 * hardware to complete reset.
6655 * a. If we are able to figure out in reasonable time that hardware
6656 * has fully resetted then, we can proceed with driver, client
6658 * b. else, we can come back later to check this status so re-sched
6661 reset_level = hns3_get_reset_level(hns, &hw->reset.pending);
6662 if (reset_level != HNS3_NONE_RESET) {
6663 gettimeofday(&tv_start, NULL);
6664 ret = hns3_reset_process(hns, reset_level);
6665 gettimeofday(&tv, NULL);
6666 timersub(&tv, &tv_start, &tv_delta);
6667 msec = tv_delta.tv_sec * MSEC_PER_SEC +
6668 tv_delta.tv_usec / USEC_PER_MSEC;
6669 if (msec > HNS3_RESET_PROCESS_MS)
6670 hns3_err(hw, "%d handle long time delta %" PRIx64
6671 " ms time=%ld.%.6ld",
6672 hw->reset.level, msec,
6673 tv.tv_sec, tv.tv_usec);
6678 /* Check if we got any *new* reset requests to be honored */
6679 reset_level = hns3_get_reset_level(hns, &hw->reset.request);
6680 if (reset_level != HNS3_NONE_RESET)
6681 hns3_msix_process(hns, reset_level);
6685 hns3_get_speed_capa_num(uint16_t device_id)
6689 switch (device_id) {
6690 case HNS3_DEV_ID_25GE:
6691 case HNS3_DEV_ID_25GE_RDMA:
6694 case HNS3_DEV_ID_100G_RDMA_MACSEC:
6695 case HNS3_DEV_ID_200G_RDMA:
6707 hns3_get_speed_fec_capa(struct rte_eth_fec_capa *speed_fec_capa,
6710 switch (device_id) {
6711 case HNS3_DEV_ID_25GE:
6713 case HNS3_DEV_ID_25GE_RDMA:
6714 speed_fec_capa[0].speed = speed_fec_capa_tbl[1].speed;
6715 speed_fec_capa[0].capa = speed_fec_capa_tbl[1].capa;
6717 /* In HNS3 device, the 25G NIC is compatible with 10G rate */
6718 speed_fec_capa[1].speed = speed_fec_capa_tbl[0].speed;
6719 speed_fec_capa[1].capa = speed_fec_capa_tbl[0].capa;
6721 case HNS3_DEV_ID_100G_RDMA_MACSEC:
6722 speed_fec_capa[0].speed = speed_fec_capa_tbl[4].speed;
6723 speed_fec_capa[0].capa = speed_fec_capa_tbl[4].capa;
6725 case HNS3_DEV_ID_200G_RDMA:
6726 speed_fec_capa[0].speed = speed_fec_capa_tbl[5].speed;
6727 speed_fec_capa[0].capa = speed_fec_capa_tbl[5].capa;
6737 hns3_fec_get_capability(struct rte_eth_dev *dev,
6738 struct rte_eth_fec_capa *speed_fec_capa,
6741 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
6742 struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
6743 uint16_t device_id = pci_dev->id.device_id;
6744 unsigned int capa_num;
6747 capa_num = hns3_get_speed_capa_num(device_id);
6748 if (capa_num == 0) {
6749 hns3_err(hw, "device(0x%x) is not supported by hns3 PMD",
6754 if (speed_fec_capa == NULL || num < capa_num)
6757 ret = hns3_get_speed_fec_capa(speed_fec_capa, device_id);
6765 get_current_fec_auto_state(struct hns3_hw *hw, uint8_t *state)
6767 struct hns3_config_fec_cmd *req;
6768 struct hns3_cmd_desc desc;
6772 * CMD(HNS3_OPC_CONFIG_FEC_MODE) read is not supported
6773 * in device of link speed
6776 if (hw->mac.link_speed < ETH_SPEED_NUM_10G) {
6781 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_CONFIG_FEC_MODE, true);
6782 req = (struct hns3_config_fec_cmd *)desc.data;
6783 ret = hns3_cmd_send(hw, &desc, 1);
6785 hns3_err(hw, "get current fec auto state failed, ret = %d",
6790 *state = req->fec_mode & (1U << HNS3_MAC_CFG_FEC_AUTO_EN_B);
6795 hns3_fec_get_internal(struct hns3_hw *hw, uint32_t *fec_capa)
6797 struct hns3_sfp_info_cmd *resp;
6798 uint32_t tmp_fec_capa;
6800 struct hns3_cmd_desc desc;
6804 * If link is down and AUTO is enabled, AUTO is returned, otherwise,
6805 * configured FEC mode is returned.
6806 * If link is up, current FEC mode is returned.
6808 if (hw->mac.link_status == ETH_LINK_DOWN) {
6809 ret = get_current_fec_auto_state(hw, &auto_state);
6813 if (auto_state == 0x1) {
6814 *fec_capa = RTE_ETH_FEC_MODE_CAPA_MASK(AUTO);
6819 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_GET_SFP_INFO, true);
6820 resp = (struct hns3_sfp_info_cmd *)desc.data;
6821 resp->query_type = HNS3_ACTIVE_QUERY;
6823 ret = hns3_cmd_send(hw, &desc, 1);
6824 if (ret == -EOPNOTSUPP) {
6825 hns3_err(hw, "IMP do not support get FEC, ret = %d", ret);
6828 hns3_err(hw, "get FEC failed, ret = %d", ret);
6833 * FEC mode order defined in hns3 hardware is inconsistend with
6834 * that defined in the ethdev library. So the sequence needs
6837 switch (resp->active_fec) {
6838 case HNS3_HW_FEC_MODE_NOFEC:
6839 tmp_fec_capa = RTE_ETH_FEC_MODE_CAPA_MASK(NOFEC);
6841 case HNS3_HW_FEC_MODE_BASER:
6842 tmp_fec_capa = RTE_ETH_FEC_MODE_CAPA_MASK(BASER);
6844 case HNS3_HW_FEC_MODE_RS:
6845 tmp_fec_capa = RTE_ETH_FEC_MODE_CAPA_MASK(RS);
6848 tmp_fec_capa = RTE_ETH_FEC_MODE_CAPA_MASK(NOFEC);
6852 *fec_capa = tmp_fec_capa;
6857 hns3_fec_get(struct rte_eth_dev *dev, uint32_t *fec_capa)
6859 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
6861 return hns3_fec_get_internal(hw, fec_capa);
6865 hns3_set_fec_hw(struct hns3_hw *hw, uint32_t mode)
6867 struct hns3_config_fec_cmd *req;
6868 struct hns3_cmd_desc desc;
6871 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_CONFIG_FEC_MODE, false);
6873 req = (struct hns3_config_fec_cmd *)desc.data;
6875 case RTE_ETH_FEC_MODE_CAPA_MASK(NOFEC):
6876 hns3_set_field(req->fec_mode, HNS3_MAC_CFG_FEC_MODE_M,
6877 HNS3_MAC_CFG_FEC_MODE_S, HNS3_MAC_FEC_OFF);
6879 case RTE_ETH_FEC_MODE_CAPA_MASK(BASER):
6880 hns3_set_field(req->fec_mode, HNS3_MAC_CFG_FEC_MODE_M,
6881 HNS3_MAC_CFG_FEC_MODE_S, HNS3_MAC_FEC_BASER);
6883 case RTE_ETH_FEC_MODE_CAPA_MASK(RS):
6884 hns3_set_field(req->fec_mode, HNS3_MAC_CFG_FEC_MODE_M,
6885 HNS3_MAC_CFG_FEC_MODE_S, HNS3_MAC_FEC_RS);
6887 case RTE_ETH_FEC_MODE_CAPA_MASK(AUTO):
6888 hns3_set_bit(req->fec_mode, HNS3_MAC_CFG_FEC_AUTO_EN_B, 1);
6893 ret = hns3_cmd_send(hw, &desc, 1);
6895 hns3_err(hw, "set fec mode failed, ret = %d", ret);
6901 get_current_speed_fec_cap(struct hns3_hw *hw, struct rte_eth_fec_capa *fec_capa)
6903 struct hns3_mac *mac = &hw->mac;
6906 switch (mac->link_speed) {
6907 case ETH_SPEED_NUM_10G:
6908 cur_capa = fec_capa[1].capa;
6910 case ETH_SPEED_NUM_25G:
6911 case ETH_SPEED_NUM_100G:
6912 case ETH_SPEED_NUM_200G:
6913 cur_capa = fec_capa[0].capa;
6924 is_fec_mode_one_bit_set(uint32_t mode)
6929 for (i = 0; i < sizeof(mode); i++)
6930 if (mode >> i & 0x1)
6933 return cnt == 1 ? true : false;
6937 hns3_fec_set(struct rte_eth_dev *dev, uint32_t mode)
6939 #define FEC_CAPA_NUM 2
6940 struct hns3_adapter *hns = dev->data->dev_private;
6941 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(hns);
6942 struct hns3_pf *pf = &hns->pf;
6944 struct rte_eth_fec_capa fec_capa[FEC_CAPA_NUM];
6946 uint32_t num = FEC_CAPA_NUM;
6949 ret = hns3_fec_get_capability(dev, fec_capa, num);
6953 /* HNS3 PMD driver only support one bit set mode, e.g. 0x1, 0x4 */
6954 if (!is_fec_mode_one_bit_set(mode))
6955 hns3_err(hw, "FEC mode(0x%x) not supported in HNS3 PMD,"
6956 "FEC mode should be only one bit set", mode);
6959 * Check whether the configured mode is within the FEC capability.
6960 * If not, the configured mode will not be supported.
6962 cur_capa = get_current_speed_fec_cap(hw, fec_capa);
6963 if (!(cur_capa & mode)) {
6964 hns3_err(hw, "unsupported FEC mode = 0x%x", mode);
6968 rte_spinlock_lock(&hw->lock);
6969 ret = hns3_set_fec_hw(hw, mode);
6971 rte_spinlock_unlock(&hw->lock);
6975 pf->fec_mode = mode;
6976 rte_spinlock_unlock(&hw->lock);
6982 hns3_restore_fec(struct hns3_hw *hw)
6984 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
6985 struct hns3_pf *pf = &hns->pf;
6986 uint32_t mode = pf->fec_mode;
6989 ret = hns3_set_fec_hw(hw, mode);
6991 hns3_err(hw, "restore fec mode(0x%x) failed, ret = %d",
6998 hns3_query_dev_fec_info(struct hns3_hw *hw)
7000 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
7001 struct hns3_pf *pf = HNS3_DEV_PRIVATE_TO_PF(hns);
7004 ret = hns3_fec_get_internal(hw, &pf->fec_mode);
7006 hns3_err(hw, "query device FEC info failed, ret = %d", ret);
7012 hns3_optical_module_existed(struct hns3_hw *hw)
7014 struct hns3_cmd_desc desc;
7018 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_GET_SFP_EXIST, true);
7019 ret = hns3_cmd_send(hw, &desc, 1);
7022 "fail to get optical module exist state, ret = %d.\n",
7026 existed = !!desc.data[0];
7032 hns3_get_module_eeprom_data(struct hns3_hw *hw, uint32_t offset,
7033 uint32_t len, uint8_t *data)
7035 #define HNS3_SFP_INFO_CMD_NUM 6
7036 #define HNS3_SFP_INFO_MAX_LEN \
7037 (HNS3_SFP_INFO_BD0_LEN + \
7038 (HNS3_SFP_INFO_CMD_NUM - 1) * HNS3_SFP_INFO_BDX_LEN)
7039 struct hns3_cmd_desc desc[HNS3_SFP_INFO_CMD_NUM];
7040 struct hns3_sfp_info_bd0_cmd *sfp_info_bd0;
7046 for (i = 0; i < HNS3_SFP_INFO_CMD_NUM; i++) {
7047 hns3_cmd_setup_basic_desc(&desc[i], HNS3_OPC_GET_SFP_EEPROM,
7049 if (i < HNS3_SFP_INFO_CMD_NUM - 1)
7050 desc[i].flag |= rte_cpu_to_le_16(HNS3_CMD_FLAG_NEXT);
7053 sfp_info_bd0 = (struct hns3_sfp_info_bd0_cmd *)desc[0].data;
7054 sfp_info_bd0->offset = rte_cpu_to_le_16((uint16_t)offset);
7055 read_len = RTE_MIN(len, HNS3_SFP_INFO_MAX_LEN);
7056 sfp_info_bd0->read_len = rte_cpu_to_le_16((uint16_t)read_len);
7058 ret = hns3_cmd_send(hw, desc, HNS3_SFP_INFO_CMD_NUM);
7060 hns3_err(hw, "fail to get module EEPROM info, ret = %d.\n",
7065 /* The data format in BD0 is different with the others. */
7066 copy_len = RTE_MIN(len, HNS3_SFP_INFO_BD0_LEN);
7067 memcpy(data, sfp_info_bd0->data, copy_len);
7068 read_len = copy_len;
7070 for (i = 1; i < HNS3_SFP_INFO_CMD_NUM; i++) {
7071 if (read_len >= len)
7074 copy_len = RTE_MIN(len - read_len, HNS3_SFP_INFO_BDX_LEN);
7075 memcpy(data + read_len, desc[i].data, copy_len);
7076 read_len += copy_len;
7079 return (int)read_len;
7083 hns3_get_module_eeprom(struct rte_eth_dev *dev,
7084 struct rte_dev_eeprom_info *info)
7086 struct hns3_adapter *hns = dev->data->dev_private;
7087 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(hns);
7088 uint32_t offset = info->offset;
7089 uint32_t len = info->length;
7090 uint8_t *data = info->data;
7091 uint32_t read_len = 0;
7093 if (hw->mac.media_type != HNS3_MEDIA_TYPE_FIBER)
7096 if (!hns3_optical_module_existed(hw)) {
7097 hns3_err(hw, "fail to read module EEPROM: no module is connected.\n");
7101 while (read_len < len) {
7103 ret = hns3_get_module_eeprom_data(hw, offset + read_len,
7115 hns3_get_module_info(struct rte_eth_dev *dev,
7116 struct rte_eth_dev_module_info *modinfo)
7118 #define HNS3_SFF8024_ID_SFP 0x03
7119 #define HNS3_SFF8024_ID_QSFP_8438 0x0c
7120 #define HNS3_SFF8024_ID_QSFP_8436_8636 0x0d
7121 #define HNS3_SFF8024_ID_QSFP28_8636 0x11
7122 #define HNS3_SFF_8636_V1_3 0x03
7123 struct hns3_adapter *hns = dev->data->dev_private;
7124 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(hns);
7125 struct rte_dev_eeprom_info info;
7126 struct hns3_sfp_type sfp_type;
7129 memset(&sfp_type, 0, sizeof(sfp_type));
7130 memset(&info, 0, sizeof(info));
7131 info.data = (uint8_t *)&sfp_type;
7132 info.length = sizeof(sfp_type);
7133 ret = hns3_get_module_eeprom(dev, &info);
7137 switch (sfp_type.type) {
7138 case HNS3_SFF8024_ID_SFP:
7139 modinfo->type = RTE_ETH_MODULE_SFF_8472;
7140 modinfo->eeprom_len = RTE_ETH_MODULE_SFF_8472_LEN;
7142 case HNS3_SFF8024_ID_QSFP_8438:
7143 modinfo->type = RTE_ETH_MODULE_SFF_8436;
7144 modinfo->eeprom_len = RTE_ETH_MODULE_SFF_8436_MAX_LEN;
7146 case HNS3_SFF8024_ID_QSFP_8436_8636:
7147 if (sfp_type.ext_type < HNS3_SFF_8636_V1_3) {
7148 modinfo->type = RTE_ETH_MODULE_SFF_8436;
7149 modinfo->eeprom_len = RTE_ETH_MODULE_SFF_8436_MAX_LEN;
7151 modinfo->type = RTE_ETH_MODULE_SFF_8636;
7152 modinfo->eeprom_len = RTE_ETH_MODULE_SFF_8636_MAX_LEN;
7155 case HNS3_SFF8024_ID_QSFP28_8636:
7156 modinfo->type = RTE_ETH_MODULE_SFF_8636;
7157 modinfo->eeprom_len = RTE_ETH_MODULE_SFF_8636_MAX_LEN;
7160 hns3_err(hw, "unknown module, type = %u, extra_type = %u.\n",
7161 sfp_type.type, sfp_type.ext_type);
7169 hns3_parse_io_hint_func(const char *key, const char *value, void *extra_args)
7171 uint32_t hint = HNS3_IO_FUNC_HINT_NONE;
7175 if (strcmp(value, "vec") == 0)
7176 hint = HNS3_IO_FUNC_HINT_VEC;
7177 else if (strcmp(value, "sve") == 0)
7178 hint = HNS3_IO_FUNC_HINT_SVE;
7179 else if (strcmp(value, "simple") == 0)
7180 hint = HNS3_IO_FUNC_HINT_SIMPLE;
7181 else if (strcmp(value, "common") == 0)
7182 hint = HNS3_IO_FUNC_HINT_COMMON;
7184 /* If the hint is valid then update output parameters */
7185 if (hint != HNS3_IO_FUNC_HINT_NONE)
7186 *(uint32_t *)extra_args = hint;
7192 hns3_get_io_hint_func_name(uint32_t hint)
7195 case HNS3_IO_FUNC_HINT_VEC:
7197 case HNS3_IO_FUNC_HINT_SVE:
7199 case HNS3_IO_FUNC_HINT_SIMPLE:
7201 case HNS3_IO_FUNC_HINT_COMMON:
7209 hns3_parse_dev_caps_mask(const char *key, const char *value, void *extra_args)
7215 val = strtoull(value, NULL, 16);
7216 *(uint64_t *)extra_args = val;
7222 hns3_parse_devargs(struct rte_eth_dev *dev)
7224 struct hns3_adapter *hns = dev->data->dev_private;
7225 uint32_t rx_func_hint = HNS3_IO_FUNC_HINT_NONE;
7226 uint32_t tx_func_hint = HNS3_IO_FUNC_HINT_NONE;
7227 struct hns3_hw *hw = &hns->hw;
7228 uint64_t dev_caps_mask = 0;
7229 struct rte_kvargs *kvlist;
7231 if (dev->device->devargs == NULL)
7234 kvlist = rte_kvargs_parse(dev->device->devargs->args, NULL);
7238 rte_kvargs_process(kvlist, HNS3_DEVARG_RX_FUNC_HINT,
7239 &hns3_parse_io_hint_func, &rx_func_hint);
7240 rte_kvargs_process(kvlist, HNS3_DEVARG_TX_FUNC_HINT,
7241 &hns3_parse_io_hint_func, &tx_func_hint);
7242 rte_kvargs_process(kvlist, HNS3_DEVARG_DEV_CAPS_MASK,
7243 &hns3_parse_dev_caps_mask, &dev_caps_mask);
7244 rte_kvargs_free(kvlist);
7246 if (rx_func_hint != HNS3_IO_FUNC_HINT_NONE)
7247 hns3_warn(hw, "parsed %s = %s.", HNS3_DEVARG_RX_FUNC_HINT,
7248 hns3_get_io_hint_func_name(rx_func_hint));
7249 hns->rx_func_hint = rx_func_hint;
7250 if (tx_func_hint != HNS3_IO_FUNC_HINT_NONE)
7251 hns3_warn(hw, "parsed %s = %s.", HNS3_DEVARG_TX_FUNC_HINT,
7252 hns3_get_io_hint_func_name(tx_func_hint));
7253 hns->tx_func_hint = tx_func_hint;
7255 if (dev_caps_mask != 0)
7256 hns3_warn(hw, "parsed %s = 0x%" PRIx64 ".",
7257 HNS3_DEVARG_DEV_CAPS_MASK, dev_caps_mask);
7258 hns->dev_caps_mask = dev_caps_mask;
7261 static const struct eth_dev_ops hns3_eth_dev_ops = {
7262 .dev_configure = hns3_dev_configure,
7263 .dev_start = hns3_dev_start,
7264 .dev_stop = hns3_dev_stop,
7265 .dev_close = hns3_dev_close,
7266 .promiscuous_enable = hns3_dev_promiscuous_enable,
7267 .promiscuous_disable = hns3_dev_promiscuous_disable,
7268 .allmulticast_enable = hns3_dev_allmulticast_enable,
7269 .allmulticast_disable = hns3_dev_allmulticast_disable,
7270 .mtu_set = hns3_dev_mtu_set,
7271 .stats_get = hns3_stats_get,
7272 .stats_reset = hns3_stats_reset,
7273 .xstats_get = hns3_dev_xstats_get,
7274 .xstats_get_names = hns3_dev_xstats_get_names,
7275 .xstats_reset = hns3_dev_xstats_reset,
7276 .xstats_get_by_id = hns3_dev_xstats_get_by_id,
7277 .xstats_get_names_by_id = hns3_dev_xstats_get_names_by_id,
7278 .dev_infos_get = hns3_dev_infos_get,
7279 .fw_version_get = hns3_fw_version_get,
7280 .rx_queue_setup = hns3_rx_queue_setup,
7281 .tx_queue_setup = hns3_tx_queue_setup,
7282 .rx_queue_release = hns3_dev_rx_queue_release,
7283 .tx_queue_release = hns3_dev_tx_queue_release,
7284 .rx_queue_start = hns3_dev_rx_queue_start,
7285 .rx_queue_stop = hns3_dev_rx_queue_stop,
7286 .tx_queue_start = hns3_dev_tx_queue_start,
7287 .tx_queue_stop = hns3_dev_tx_queue_stop,
7288 .rx_queue_intr_enable = hns3_dev_rx_queue_intr_enable,
7289 .rx_queue_intr_disable = hns3_dev_rx_queue_intr_disable,
7290 .rxq_info_get = hns3_rxq_info_get,
7291 .txq_info_get = hns3_txq_info_get,
7292 .rx_burst_mode_get = hns3_rx_burst_mode_get,
7293 .tx_burst_mode_get = hns3_tx_burst_mode_get,
7294 .flow_ctrl_get = hns3_flow_ctrl_get,
7295 .flow_ctrl_set = hns3_flow_ctrl_set,
7296 .priority_flow_ctrl_set = hns3_priority_flow_ctrl_set,
7297 .mac_addr_add = hns3_add_mac_addr,
7298 .mac_addr_remove = hns3_remove_mac_addr,
7299 .mac_addr_set = hns3_set_default_mac_addr,
7300 .set_mc_addr_list = hns3_set_mc_mac_addr_list,
7301 .link_update = hns3_dev_link_update,
7302 .rss_hash_update = hns3_dev_rss_hash_update,
7303 .rss_hash_conf_get = hns3_dev_rss_hash_conf_get,
7304 .reta_update = hns3_dev_rss_reta_update,
7305 .reta_query = hns3_dev_rss_reta_query,
7306 .flow_ops_get = hns3_dev_flow_ops_get,
7307 .vlan_filter_set = hns3_vlan_filter_set,
7308 .vlan_tpid_set = hns3_vlan_tpid_set,
7309 .vlan_offload_set = hns3_vlan_offload_set,
7310 .vlan_pvid_set = hns3_vlan_pvid_set,
7311 .get_reg = hns3_get_regs,
7312 .get_module_info = hns3_get_module_info,
7313 .get_module_eeprom = hns3_get_module_eeprom,
7314 .get_dcb_info = hns3_get_dcb_info,
7315 .dev_supported_ptypes_get = hns3_dev_supported_ptypes_get,
7316 .fec_get_capability = hns3_fec_get_capability,
7317 .fec_get = hns3_fec_get,
7318 .fec_set = hns3_fec_set,
7319 .tm_ops_get = hns3_tm_ops_get,
7320 .tx_done_cleanup = hns3_tx_done_cleanup,
7321 .timesync_enable = hns3_timesync_enable,
7322 .timesync_disable = hns3_timesync_disable,
7323 .timesync_read_rx_timestamp = hns3_timesync_read_rx_timestamp,
7324 .timesync_read_tx_timestamp = hns3_timesync_read_tx_timestamp,
7325 .timesync_adjust_time = hns3_timesync_adjust_time,
7326 .timesync_read_time = hns3_timesync_read_time,
7327 .timesync_write_time = hns3_timesync_write_time,
7330 static const struct hns3_reset_ops hns3_reset_ops = {
7331 .reset_service = hns3_reset_service,
7332 .stop_service = hns3_stop_service,
7333 .prepare_reset = hns3_prepare_reset,
7334 .wait_hardware_ready = hns3_wait_hardware_ready,
7335 .reinit_dev = hns3_reinit_dev,
7336 .restore_conf = hns3_restore_conf,
7337 .start_service = hns3_start_service,
7341 hns3_dev_init(struct rte_eth_dev *eth_dev)
7343 struct hns3_adapter *hns = eth_dev->data->dev_private;
7344 char mac_str[RTE_ETHER_ADDR_FMT_SIZE];
7345 struct rte_ether_addr *eth_addr;
7346 struct hns3_hw *hw = &hns->hw;
7349 PMD_INIT_FUNC_TRACE();
7351 eth_dev->process_private = (struct hns3_process_private *)
7352 rte_zmalloc_socket("hns3_filter_list",
7353 sizeof(struct hns3_process_private),
7354 RTE_CACHE_LINE_SIZE, eth_dev->device->numa_node);
7355 if (eth_dev->process_private == NULL) {
7356 PMD_INIT_LOG(ERR, "Failed to alloc memory for process private");
7360 hns3_flow_init(eth_dev);
7362 hns3_set_rxtx_function(eth_dev);
7363 eth_dev->dev_ops = &hns3_eth_dev_ops;
7364 eth_dev->rx_queue_count = hns3_rx_queue_count;
7365 if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
7366 ret = hns3_mp_init_secondary();
7368 PMD_INIT_LOG(ERR, "Failed to init for secondary "
7369 "process, ret = %d", ret);
7370 goto err_mp_init_secondary;
7373 hw->secondary_cnt++;
7377 ret = hns3_mp_init_primary();
7380 "Failed to init for primary process, ret = %d",
7382 goto err_mp_init_primary;
7385 hw->adapter_state = HNS3_NIC_UNINITIALIZED;
7387 hw->data = eth_dev->data;
7388 hns3_parse_devargs(eth_dev);
7391 * Set default max packet size according to the mtu
7392 * default vale in DPDK frame.
7394 hns->pf.mps = hw->data->mtu + HNS3_ETH_OVERHEAD;
7396 ret = hns3_reset_init(hw);
7398 goto err_init_reset;
7399 hw->reset.ops = &hns3_reset_ops;
7401 ret = hns3_init_pf(eth_dev);
7403 PMD_INIT_LOG(ERR, "Failed to init pf: %d", ret);
7407 /* Allocate memory for storing MAC addresses */
7408 eth_dev->data->mac_addrs = rte_zmalloc("hns3-mac",
7409 sizeof(struct rte_ether_addr) *
7410 HNS3_UC_MACADDR_NUM, 0);
7411 if (eth_dev->data->mac_addrs == NULL) {
7412 PMD_INIT_LOG(ERR, "Failed to allocate %zx bytes needed "
7413 "to store MAC addresses",
7414 sizeof(struct rte_ether_addr) *
7415 HNS3_UC_MACADDR_NUM);
7417 goto err_rte_zmalloc;
7420 eth_addr = (struct rte_ether_addr *)hw->mac.mac_addr;
7421 if (!rte_is_valid_assigned_ether_addr(eth_addr)) {
7422 rte_eth_random_addr(hw->mac.mac_addr);
7423 hns3_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
7424 (struct rte_ether_addr *)hw->mac.mac_addr);
7425 hns3_warn(hw, "default mac_addr from firmware is an invalid "
7426 "unicast address, using random MAC address %s",
7429 rte_ether_addr_copy((struct rte_ether_addr *)hw->mac.mac_addr,
7430 ð_dev->data->mac_addrs[0]);
7432 hw->adapter_state = HNS3_NIC_INITIALIZED;
7434 if (__atomic_load_n(&hw->reset.schedule, __ATOMIC_RELAXED) ==
7436 hns3_err(hw, "Reschedule reset service after dev_init");
7437 hns3_schedule_reset(hns);
7439 /* IMP will wait ready flag before reset */
7440 hns3_notify_reset_ready(hw, false);
7443 hns3_info(hw, "hns3 dev initialization successful!");
7447 hns3_uninit_pf(eth_dev);
7450 rte_free(hw->reset.wait_data);
7453 hns3_mp_uninit_primary();
7455 err_mp_init_primary:
7456 err_mp_init_secondary:
7457 eth_dev->dev_ops = NULL;
7458 eth_dev->rx_pkt_burst = NULL;
7459 eth_dev->rx_descriptor_status = NULL;
7460 eth_dev->tx_pkt_burst = NULL;
7461 eth_dev->tx_pkt_prepare = NULL;
7462 eth_dev->tx_descriptor_status = NULL;
7463 rte_free(eth_dev->process_private);
7464 eth_dev->process_private = NULL;
7469 hns3_dev_uninit(struct rte_eth_dev *eth_dev)
7471 struct hns3_adapter *hns = eth_dev->data->dev_private;
7472 struct hns3_hw *hw = &hns->hw;
7474 PMD_INIT_FUNC_TRACE();
7476 if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
7477 rte_free(eth_dev->process_private);
7478 eth_dev->process_private = NULL;
7482 if (hw->adapter_state < HNS3_NIC_CLOSING)
7483 hns3_dev_close(eth_dev);
7485 hw->adapter_state = HNS3_NIC_REMOVED;
7490 eth_hns3_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
7491 struct rte_pci_device *pci_dev)
7493 return rte_eth_dev_pci_generic_probe(pci_dev,
7494 sizeof(struct hns3_adapter),
7499 eth_hns3_pci_remove(struct rte_pci_device *pci_dev)
7501 return rte_eth_dev_pci_generic_remove(pci_dev, hns3_dev_uninit);
7504 static const struct rte_pci_id pci_id_hns3_map[] = {
7505 { RTE_PCI_DEVICE(PCI_VENDOR_ID_HUAWEI, HNS3_DEV_ID_GE) },
7506 { RTE_PCI_DEVICE(PCI_VENDOR_ID_HUAWEI, HNS3_DEV_ID_25GE) },
7507 { RTE_PCI_DEVICE(PCI_VENDOR_ID_HUAWEI, HNS3_DEV_ID_25GE_RDMA) },
7508 { RTE_PCI_DEVICE(PCI_VENDOR_ID_HUAWEI, HNS3_DEV_ID_50GE_RDMA) },
7509 { RTE_PCI_DEVICE(PCI_VENDOR_ID_HUAWEI, HNS3_DEV_ID_100G_RDMA_MACSEC) },
7510 { RTE_PCI_DEVICE(PCI_VENDOR_ID_HUAWEI, HNS3_DEV_ID_200G_RDMA) },
7511 { .vendor_id = 0, }, /* sentinel */
7514 static struct rte_pci_driver rte_hns3_pmd = {
7515 .id_table = pci_id_hns3_map,
7516 .drv_flags = RTE_PCI_DRV_NEED_MAPPING | RTE_PCI_DRV_INTR_LSC,
7517 .probe = eth_hns3_pci_probe,
7518 .remove = eth_hns3_pci_remove,
7521 RTE_PMD_REGISTER_PCI(net_hns3, rte_hns3_pmd);
7522 RTE_PMD_REGISTER_PCI_TABLE(net_hns3, pci_id_hns3_map);
7523 RTE_PMD_REGISTER_KMOD_DEP(net_hns3, "* igb_uio | vfio-pci");
7524 RTE_PMD_REGISTER_PARAM_STRING(net_hns3,
7525 HNS3_DEVARG_RX_FUNC_HINT "=vec|sve|simple|common "
7526 HNS3_DEVARG_TX_FUNC_HINT "=vec|sve|simple|common "
7527 HNS3_DEVARG_DEV_CAPS_MASK "=<1-65535> ");
7528 RTE_LOG_REGISTER(hns3_logtype_init, pmd.net.hns3.init, NOTICE);
7529 RTE_LOG_REGISTER(hns3_logtype_driver, pmd.net.hns3.driver, NOTICE);