1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2018-2019 Hisilicon Limited.
12 #include <rte_atomic.h>
13 #include <rte_bus_pci.h>
14 #include <rte_common.h>
15 #include <rte_cycles.h>
18 #include <rte_ether.h>
19 #include <rte_ethdev_driver.h>
20 #include <rte_ethdev_pci.h>
21 #include <rte_interrupts.h>
26 #include "hns3_ethdev.h"
27 #include "hns3_logs.h"
28 #include "hns3_rxtx.h"
29 #include "hns3_intr.h"
30 #include "hns3_regs.h"
34 #define HNS3_DEFAULT_PORT_CONF_BURST_SIZE 32
35 #define HNS3_DEFAULT_PORT_CONF_QUEUES_NUM 1
37 #define HNS3_SERVICE_INTERVAL 1000000 /* us */
38 #define HNS3_INVLID_PVID 0xFFFF
40 #define HNS3_FILTER_TYPE_VF 0
41 #define HNS3_FILTER_TYPE_PORT 1
42 #define HNS3_FILTER_FE_EGRESS_V1_B BIT(0)
43 #define HNS3_FILTER_FE_NIC_INGRESS_B BIT(0)
44 #define HNS3_FILTER_FE_NIC_EGRESS_B BIT(1)
45 #define HNS3_FILTER_FE_ROCE_INGRESS_B BIT(2)
46 #define HNS3_FILTER_FE_ROCE_EGRESS_B BIT(3)
47 #define HNS3_FILTER_FE_EGRESS (HNS3_FILTER_FE_NIC_EGRESS_B \
48 | HNS3_FILTER_FE_ROCE_EGRESS_B)
49 #define HNS3_FILTER_FE_INGRESS (HNS3_FILTER_FE_NIC_INGRESS_B \
50 | HNS3_FILTER_FE_ROCE_INGRESS_B)
52 /* Reset related Registers */
53 #define HNS3_GLOBAL_RESET_BIT 0
54 #define HNS3_CORE_RESET_BIT 1
55 #define HNS3_IMP_RESET_BIT 2
56 #define HNS3_FUN_RST_ING_B 0
58 #define HNS3_VECTOR0_IMP_RESET_INT_B 1
60 #define HNS3_RESET_WAIT_MS 100
61 #define HNS3_RESET_WAIT_CNT 200
64 HNS3_VECTOR0_EVENT_RST,
65 HNS3_VECTOR0_EVENT_MBX,
66 HNS3_VECTOR0_EVENT_ERR,
67 HNS3_VECTOR0_EVENT_OTHER,
70 static enum hns3_reset_level hns3_get_reset_level(struct hns3_adapter *hns,
72 static int hns3_dev_mtu_set(struct rte_eth_dev *dev, uint16_t mtu);
73 static int hns3_vlan_pvid_configure(struct hns3_adapter *hns, uint16_t pvid,
75 static int hns3_update_speed_duplex(struct rte_eth_dev *eth_dev);
77 static int hns3_add_mc_addr(struct hns3_hw *hw,
78 struct rte_ether_addr *mac_addr);
79 static int hns3_remove_mc_addr(struct hns3_hw *hw,
80 struct rte_ether_addr *mac_addr);
83 hns3_pf_disable_irq0(struct hns3_hw *hw)
85 hns3_write_dev(hw, HNS3_MISC_VECTOR_REG_BASE, 0);
89 hns3_pf_enable_irq0(struct hns3_hw *hw)
91 hns3_write_dev(hw, HNS3_MISC_VECTOR_REG_BASE, 1);
94 static enum hns3_evt_cause
95 hns3_check_event_cause(struct hns3_adapter *hns, uint32_t *clearval)
97 struct hns3_hw *hw = &hns->hw;
98 uint32_t vector0_int_stats;
99 uint32_t cmdq_src_val;
101 enum hns3_evt_cause ret;
103 /* fetch the events from their corresponding regs */
104 vector0_int_stats = hns3_read_dev(hw, HNS3_VECTOR0_OTHER_INT_STS_REG);
105 cmdq_src_val = hns3_read_dev(hw, HNS3_VECTOR0_CMDQ_SRC_REG);
108 * Assumption: If by any chance reset and mailbox events are reported
109 * together then we will only process reset event and defer the
110 * processing of the mailbox events. Since, we would have not cleared
111 * RX CMDQ event this time we would receive again another interrupt
112 * from H/W just for the mailbox.
114 if (BIT(HNS3_VECTOR0_IMPRESET_INT_B) & vector0_int_stats) { /* IMP */
115 rte_atomic16_set(&hw->reset.disable_cmd, 1);
116 hns3_atomic_set_bit(HNS3_IMP_RESET, &hw->reset.pending);
117 val = BIT(HNS3_VECTOR0_IMPRESET_INT_B);
119 hw->reset.stats.imp_cnt++;
120 hns3_warn(hw, "IMP reset detected, clear reset status");
122 hns3_schedule_delayed_reset(hns);
123 hns3_warn(hw, "IMP reset detected, don't clear reset status");
126 ret = HNS3_VECTOR0_EVENT_RST;
131 if (BIT(HNS3_VECTOR0_GLOBALRESET_INT_B) & vector0_int_stats) {
132 rte_atomic16_set(&hw->reset.disable_cmd, 1);
133 hns3_atomic_set_bit(HNS3_GLOBAL_RESET, &hw->reset.pending);
134 val = BIT(HNS3_VECTOR0_GLOBALRESET_INT_B);
136 hw->reset.stats.global_cnt++;
137 hns3_warn(hw, "Global reset detected, clear reset status");
139 hns3_schedule_delayed_reset(hns);
140 hns3_warn(hw, "Global reset detected, don't clear reset status");
143 ret = HNS3_VECTOR0_EVENT_RST;
147 /* check for vector0 msix event source */
148 if (vector0_int_stats & HNS3_VECTOR0_REG_MSIX_MASK) {
149 val = vector0_int_stats;
150 ret = HNS3_VECTOR0_EVENT_ERR;
154 /* check for vector0 mailbox(=CMDQ RX) event source */
155 if (BIT(HNS3_VECTOR0_RX_CMDQ_INT_B) & cmdq_src_val) {
156 cmdq_src_val &= ~BIT(HNS3_VECTOR0_RX_CMDQ_INT_B);
158 ret = HNS3_VECTOR0_EVENT_MBX;
162 if (clearval && (vector0_int_stats || cmdq_src_val))
163 hns3_warn(hw, "surprise irq ector0_int_stats:0x%x cmdq_src_val:0x%x",
164 vector0_int_stats, cmdq_src_val);
165 val = vector0_int_stats;
166 ret = HNS3_VECTOR0_EVENT_OTHER;
175 hns3_clear_event_cause(struct hns3_hw *hw, uint32_t event_type, uint32_t regclr)
177 if (event_type == HNS3_VECTOR0_EVENT_RST)
178 hns3_write_dev(hw, HNS3_MISC_RESET_STS_REG, regclr);
179 else if (event_type == HNS3_VECTOR0_EVENT_MBX)
180 hns3_write_dev(hw, HNS3_VECTOR0_CMDQ_SRC_REG, regclr);
184 hns3_clear_all_event_cause(struct hns3_hw *hw)
186 uint32_t vector0_int_stats;
187 vector0_int_stats = hns3_read_dev(hw, HNS3_VECTOR0_OTHER_INT_STS_REG);
189 if (BIT(HNS3_VECTOR0_IMPRESET_INT_B) & vector0_int_stats)
190 hns3_warn(hw, "Probe during IMP reset interrupt");
192 if (BIT(HNS3_VECTOR0_GLOBALRESET_INT_B) & vector0_int_stats)
193 hns3_warn(hw, "Probe during Global reset interrupt");
195 hns3_clear_event_cause(hw, HNS3_VECTOR0_EVENT_RST,
196 BIT(HNS3_VECTOR0_IMPRESET_INT_B) |
197 BIT(HNS3_VECTOR0_GLOBALRESET_INT_B) |
198 BIT(HNS3_VECTOR0_CORERESET_INT_B));
199 hns3_clear_event_cause(hw, HNS3_VECTOR0_EVENT_MBX, 0);
203 hns3_interrupt_handler(void *param)
205 struct rte_eth_dev *dev = (struct rte_eth_dev *)param;
206 struct hns3_adapter *hns = dev->data->dev_private;
207 struct hns3_hw *hw = &hns->hw;
208 enum hns3_evt_cause event_cause;
209 uint32_t clearval = 0;
211 /* Disable interrupt */
212 hns3_pf_disable_irq0(hw);
214 event_cause = hns3_check_event_cause(hns, &clearval);
216 /* vector 0 interrupt is shared with reset and mailbox source events. */
217 if (event_cause == HNS3_VECTOR0_EVENT_ERR) {
218 hns3_handle_msix_error(hns, &hw->reset.request);
219 hns3_schedule_reset(hns);
220 } else if (event_cause == HNS3_VECTOR0_EVENT_RST)
221 hns3_schedule_reset(hns);
222 else if (event_cause == HNS3_VECTOR0_EVENT_MBX)
223 hns3_dev_handle_mbx_msg(hw);
225 hns3_err(hw, "Received unknown event");
227 hns3_clear_event_cause(hw, event_cause, clearval);
228 /* Enable interrupt if it is not cause by reset */
229 hns3_pf_enable_irq0(hw);
233 hns3_set_port_vlan_filter(struct hns3_adapter *hns, uint16_t vlan_id, int on)
235 #define HNS3_VLAN_ID_OFFSET_STEP 160
236 #define HNS3_VLAN_BYTE_SIZE 8
237 struct hns3_vlan_filter_pf_cfg_cmd *req;
238 struct hns3_hw *hw = &hns->hw;
239 uint8_t vlan_offset_byte_val;
240 struct hns3_cmd_desc desc;
241 uint8_t vlan_offset_byte;
242 uint8_t vlan_offset_base;
245 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_VLAN_FILTER_PF_CFG, false);
247 vlan_offset_base = vlan_id / HNS3_VLAN_ID_OFFSET_STEP;
248 vlan_offset_byte = (vlan_id % HNS3_VLAN_ID_OFFSET_STEP) /
250 vlan_offset_byte_val = 1 << (vlan_id % HNS3_VLAN_BYTE_SIZE);
252 req = (struct hns3_vlan_filter_pf_cfg_cmd *)desc.data;
253 req->vlan_offset = vlan_offset_base;
254 req->vlan_cfg = on ? 0 : 1;
255 req->vlan_offset_bitmap[vlan_offset_byte] = vlan_offset_byte_val;
257 ret = hns3_cmd_send(hw, &desc, 1);
259 hns3_err(hw, "set port vlan id failed, vlan_id =%u, ret =%d",
266 hns3_rm_dev_vlan_table(struct hns3_adapter *hns, uint16_t vlan_id)
268 struct hns3_user_vlan_table *vlan_entry;
269 struct hns3_pf *pf = &hns->pf;
271 LIST_FOREACH(vlan_entry, &pf->vlan_list, next) {
272 if (vlan_entry->vlan_id == vlan_id) {
273 if (vlan_entry->hd_tbl_status)
274 hns3_set_port_vlan_filter(hns, vlan_id, 0);
275 LIST_REMOVE(vlan_entry, next);
276 rte_free(vlan_entry);
283 hns3_add_dev_vlan_table(struct hns3_adapter *hns, uint16_t vlan_id,
286 struct hns3_user_vlan_table *vlan_entry;
287 struct hns3_hw *hw = &hns->hw;
288 struct hns3_pf *pf = &hns->pf;
290 LIST_FOREACH(vlan_entry, &pf->vlan_list, next) {
291 if (vlan_entry->vlan_id == vlan_id)
295 vlan_entry = rte_zmalloc("hns3_vlan_tbl", sizeof(*vlan_entry), 0);
296 if (vlan_entry == NULL) {
297 hns3_err(hw, "Failed to malloc hns3 vlan table");
301 vlan_entry->hd_tbl_status = writen_to_tbl;
302 vlan_entry->vlan_id = vlan_id;
304 LIST_INSERT_HEAD(&pf->vlan_list, vlan_entry, next);
308 hns3_restore_vlan_table(struct hns3_adapter *hns)
310 struct hns3_user_vlan_table *vlan_entry;
311 struct hns3_hw *hw = &hns->hw;
312 struct hns3_pf *pf = &hns->pf;
316 if (hw->port_base_vlan_cfg.state == HNS3_PORT_BASE_VLAN_ENABLE)
317 return hns3_vlan_pvid_configure(hns,
318 hw->port_base_vlan_cfg.pvid, 1);
320 LIST_FOREACH(vlan_entry, &pf->vlan_list, next) {
321 if (vlan_entry->hd_tbl_status) {
322 vlan_id = vlan_entry->vlan_id;
323 ret = hns3_set_port_vlan_filter(hns, vlan_id, 1);
333 hns3_vlan_filter_configure(struct hns3_adapter *hns, uint16_t vlan_id, int on)
335 struct hns3_hw *hw = &hns->hw;
336 bool writen_to_tbl = false;
340 * When vlan filter is enabled, hardware regards vlan id 0 as the entry
341 * for normal packet, deleting vlan id 0 is not allowed.
343 if (on == 0 && vlan_id == 0)
347 * When port base vlan enabled, we use port base vlan as the vlan
348 * filter condition. In this case, we don't update vlan filter table
349 * when user add new vlan or remove exist vlan, just update the
350 * vlan list. The vlan id in vlan list will be writen in vlan filter
351 * table until port base vlan disabled
353 if (hw->port_base_vlan_cfg.state == HNS3_PORT_BASE_VLAN_DISABLE) {
354 ret = hns3_set_port_vlan_filter(hns, vlan_id, on);
355 writen_to_tbl = true;
358 if (ret == 0 && vlan_id) {
360 hns3_add_dev_vlan_table(hns, vlan_id, writen_to_tbl);
362 hns3_rm_dev_vlan_table(hns, vlan_id);
368 hns3_vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on)
370 struct hns3_adapter *hns = dev->data->dev_private;
371 struct hns3_hw *hw = &hns->hw;
374 rte_spinlock_lock(&hw->lock);
375 ret = hns3_vlan_filter_configure(hns, vlan_id, on);
376 rte_spinlock_unlock(&hw->lock);
381 hns3_vlan_tpid_configure(struct hns3_adapter *hns, enum rte_vlan_type vlan_type,
384 struct hns3_rx_vlan_type_cfg_cmd *rx_req;
385 struct hns3_tx_vlan_type_cfg_cmd *tx_req;
386 struct hns3_hw *hw = &hns->hw;
387 struct hns3_cmd_desc desc;
390 if ((vlan_type != ETH_VLAN_TYPE_INNER &&
391 vlan_type != ETH_VLAN_TYPE_OUTER)) {
392 hns3_err(hw, "Unsupported vlan type, vlan_type =%d", vlan_type);
396 if (tpid != RTE_ETHER_TYPE_VLAN) {
397 hns3_err(hw, "Unsupported vlan tpid, vlan_type =%d", vlan_type);
401 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_MAC_VLAN_TYPE_ID, false);
402 rx_req = (struct hns3_rx_vlan_type_cfg_cmd *)desc.data;
404 if (vlan_type == ETH_VLAN_TYPE_OUTER) {
405 rx_req->ot_fst_vlan_type = rte_cpu_to_le_16(tpid);
406 rx_req->ot_sec_vlan_type = rte_cpu_to_le_16(tpid);
407 } else if (vlan_type == ETH_VLAN_TYPE_INNER) {
408 rx_req->ot_fst_vlan_type = rte_cpu_to_le_16(tpid);
409 rx_req->ot_sec_vlan_type = rte_cpu_to_le_16(tpid);
410 rx_req->in_fst_vlan_type = rte_cpu_to_le_16(tpid);
411 rx_req->in_sec_vlan_type = rte_cpu_to_le_16(tpid);
414 ret = hns3_cmd_send(hw, &desc, 1);
416 hns3_err(hw, "Send rxvlan protocol type command fail, ret =%d",
421 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_MAC_VLAN_INSERT, false);
423 tx_req = (struct hns3_tx_vlan_type_cfg_cmd *)desc.data;
424 tx_req->ot_vlan_type = rte_cpu_to_le_16(tpid);
425 tx_req->in_vlan_type = rte_cpu_to_le_16(tpid);
427 ret = hns3_cmd_send(hw, &desc, 1);
429 hns3_err(hw, "Send txvlan protocol type command fail, ret =%d",
435 hns3_vlan_tpid_set(struct rte_eth_dev *dev, enum rte_vlan_type vlan_type,
438 struct hns3_adapter *hns = dev->data->dev_private;
439 struct hns3_hw *hw = &hns->hw;
442 rte_spinlock_lock(&hw->lock);
443 ret = hns3_vlan_tpid_configure(hns, vlan_type, tpid);
444 rte_spinlock_unlock(&hw->lock);
449 hns3_set_vlan_rx_offload_cfg(struct hns3_adapter *hns,
450 struct hns3_rx_vtag_cfg *vcfg)
452 struct hns3_vport_vtag_rx_cfg_cmd *req;
453 struct hns3_hw *hw = &hns->hw;
454 struct hns3_cmd_desc desc;
459 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_VLAN_PORT_RX_CFG, false);
461 req = (struct hns3_vport_vtag_rx_cfg_cmd *)desc.data;
462 hns3_set_bit(req->vport_vlan_cfg, HNS3_REM_TAG1_EN_B,
463 vcfg->strip_tag1_en ? 1 : 0);
464 hns3_set_bit(req->vport_vlan_cfg, HNS3_REM_TAG2_EN_B,
465 vcfg->strip_tag2_en ? 1 : 0);
466 hns3_set_bit(req->vport_vlan_cfg, HNS3_SHOW_TAG1_EN_B,
467 vcfg->vlan1_vlan_prionly ? 1 : 0);
468 hns3_set_bit(req->vport_vlan_cfg, HNS3_SHOW_TAG2_EN_B,
469 vcfg->vlan2_vlan_prionly ? 1 : 0);
472 * In current version VF is not supported when PF is driven by DPDK
473 * driver, just need to configure parameters for PF vport.
475 vport_id = HNS3_PF_FUNC_ID;
476 req->vf_offset = vport_id / HNS3_VF_NUM_PER_CMD;
477 bitmap = 1 << (vport_id % HNS3_VF_NUM_PER_BYTE);
478 req->vf_bitmap[req->vf_offset] = bitmap;
480 ret = hns3_cmd_send(hw, &desc, 1);
482 hns3_err(hw, "Send port rxvlan cfg command fail, ret =%d", ret);
487 hns3_update_rx_offload_cfg(struct hns3_adapter *hns,
488 struct hns3_rx_vtag_cfg *vcfg)
490 struct hns3_pf *pf = &hns->pf;
491 memcpy(&pf->vtag_config.rx_vcfg, vcfg, sizeof(pf->vtag_config.rx_vcfg));
495 hns3_update_tx_offload_cfg(struct hns3_adapter *hns,
496 struct hns3_tx_vtag_cfg *vcfg)
498 struct hns3_pf *pf = &hns->pf;
499 memcpy(&pf->vtag_config.tx_vcfg, vcfg, sizeof(pf->vtag_config.tx_vcfg));
503 hns3_en_hw_strip_rxvtag(struct hns3_adapter *hns, bool enable)
505 struct hns3_rx_vtag_cfg rxvlan_cfg;
506 struct hns3_hw *hw = &hns->hw;
509 if (hw->port_base_vlan_cfg.state == HNS3_PORT_BASE_VLAN_DISABLE) {
510 rxvlan_cfg.strip_tag1_en = false;
511 rxvlan_cfg.strip_tag2_en = enable;
513 rxvlan_cfg.strip_tag1_en = enable;
514 rxvlan_cfg.strip_tag2_en = true;
517 rxvlan_cfg.vlan1_vlan_prionly = false;
518 rxvlan_cfg.vlan2_vlan_prionly = false;
519 rxvlan_cfg.rx_vlan_offload_en = enable;
521 ret = hns3_set_vlan_rx_offload_cfg(hns, &rxvlan_cfg);
523 hns3_err(hw, "enable strip rx vtag failed, ret =%d", ret);
527 hns3_update_rx_offload_cfg(hns, &rxvlan_cfg);
533 hns3_set_vlan_filter_ctrl(struct hns3_hw *hw, uint8_t vlan_type,
534 uint8_t fe_type, bool filter_en, uint8_t vf_id)
536 struct hns3_vlan_filter_ctrl_cmd *req;
537 struct hns3_cmd_desc desc;
540 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_VLAN_FILTER_CTRL, false);
542 req = (struct hns3_vlan_filter_ctrl_cmd *)desc.data;
543 req->vlan_type = vlan_type;
544 req->vlan_fe = filter_en ? fe_type : 0;
547 ret = hns3_cmd_send(hw, &desc, 1);
549 hns3_err(hw, "set vlan filter fail, ret =%d", ret);
555 hns3_vlan_filter_init(struct hns3_adapter *hns)
557 struct hns3_hw *hw = &hns->hw;
560 ret = hns3_set_vlan_filter_ctrl(hw, HNS3_FILTER_TYPE_VF,
561 HNS3_FILTER_FE_EGRESS, false,
564 hns3_err(hw, "failed to init vf vlan filter, ret = %d", ret);
568 ret = hns3_set_vlan_filter_ctrl(hw, HNS3_FILTER_TYPE_PORT,
569 HNS3_FILTER_FE_INGRESS, false,
572 hns3_err(hw, "failed to init port vlan filter, ret = %d", ret);
578 hns3_enable_vlan_filter(struct hns3_adapter *hns, bool enable)
580 struct hns3_hw *hw = &hns->hw;
583 ret = hns3_set_vlan_filter_ctrl(hw, HNS3_FILTER_TYPE_PORT,
584 HNS3_FILTER_FE_INGRESS, enable,
587 hns3_err(hw, "failed to %s port vlan filter, ret = %d",
588 enable ? "enable" : "disable", ret);
594 hns3_vlan_offload_set(struct rte_eth_dev *dev, int mask)
596 struct hns3_adapter *hns = dev->data->dev_private;
597 struct hns3_hw *hw = &hns->hw;
598 struct rte_eth_rxmode *rxmode;
599 unsigned int tmp_mask;
603 rte_spinlock_lock(&hw->lock);
604 rxmode = &dev->data->dev_conf.rxmode;
605 tmp_mask = (unsigned int)mask;
606 if (tmp_mask & ETH_VLAN_FILTER_MASK) {
607 /* ignore vlan filter configuration during promiscuous mode */
608 if (!dev->data->promiscuous) {
609 /* Enable or disable VLAN filter */
610 enable = rxmode->offloads & DEV_RX_OFFLOAD_VLAN_FILTER ?
613 ret = hns3_enable_vlan_filter(hns, enable);
615 rte_spinlock_unlock(&hw->lock);
616 hns3_err(hw, "failed to %s rx filter, ret = %d",
617 enable ? "enable" : "disable", ret);
623 if (tmp_mask & ETH_VLAN_STRIP_MASK) {
624 /* Enable or disable VLAN stripping */
625 enable = rxmode->offloads & DEV_RX_OFFLOAD_VLAN_STRIP ?
628 ret = hns3_en_hw_strip_rxvtag(hns, enable);
630 rte_spinlock_unlock(&hw->lock);
631 hns3_err(hw, "failed to %s rx strip, ret = %d",
632 enable ? "enable" : "disable", ret);
637 rte_spinlock_unlock(&hw->lock);
643 hns3_set_vlan_tx_offload_cfg(struct hns3_adapter *hns,
644 struct hns3_tx_vtag_cfg *vcfg)
646 struct hns3_vport_vtag_tx_cfg_cmd *req;
647 struct hns3_cmd_desc desc;
648 struct hns3_hw *hw = &hns->hw;
653 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_VLAN_PORT_TX_CFG, false);
655 req = (struct hns3_vport_vtag_tx_cfg_cmd *)desc.data;
656 req->def_vlan_tag1 = vcfg->default_tag1;
657 req->def_vlan_tag2 = vcfg->default_tag2;
658 hns3_set_bit(req->vport_vlan_cfg, HNS3_ACCEPT_TAG1_B,
659 vcfg->accept_tag1 ? 1 : 0);
660 hns3_set_bit(req->vport_vlan_cfg, HNS3_ACCEPT_UNTAG1_B,
661 vcfg->accept_untag1 ? 1 : 0);
662 hns3_set_bit(req->vport_vlan_cfg, HNS3_ACCEPT_TAG2_B,
663 vcfg->accept_tag2 ? 1 : 0);
664 hns3_set_bit(req->vport_vlan_cfg, HNS3_ACCEPT_UNTAG2_B,
665 vcfg->accept_untag2 ? 1 : 0);
666 hns3_set_bit(req->vport_vlan_cfg, HNS3_PORT_INS_TAG1_EN_B,
667 vcfg->insert_tag1_en ? 1 : 0);
668 hns3_set_bit(req->vport_vlan_cfg, HNS3_PORT_INS_TAG2_EN_B,
669 vcfg->insert_tag2_en ? 1 : 0);
670 hns3_set_bit(req->vport_vlan_cfg, HNS3_CFG_NIC_ROCE_SEL_B, 0);
673 * In current version VF is not supported when PF is driven by DPDK
674 * driver, just need to configure parameters for PF vport.
676 vport_id = HNS3_PF_FUNC_ID;
677 req->vf_offset = vport_id / HNS3_VF_NUM_PER_CMD;
678 bitmap = 1 << (vport_id % HNS3_VF_NUM_PER_BYTE);
679 req->vf_bitmap[req->vf_offset] = bitmap;
681 ret = hns3_cmd_send(hw, &desc, 1);
683 hns3_err(hw, "Send port txvlan cfg command fail, ret =%d", ret);
689 hns3_vlan_txvlan_cfg(struct hns3_adapter *hns, uint16_t port_base_vlan_state,
692 struct hns3_hw *hw = &hns->hw;
693 struct hns3_tx_vtag_cfg txvlan_cfg;
696 if (port_base_vlan_state == HNS3_PORT_BASE_VLAN_DISABLE) {
697 txvlan_cfg.accept_tag1 = true;
698 txvlan_cfg.insert_tag1_en = false;
699 txvlan_cfg.default_tag1 = 0;
701 txvlan_cfg.accept_tag1 = false;
702 txvlan_cfg.insert_tag1_en = true;
703 txvlan_cfg.default_tag1 = pvid;
706 txvlan_cfg.accept_untag1 = true;
707 txvlan_cfg.accept_tag2 = true;
708 txvlan_cfg.accept_untag2 = true;
709 txvlan_cfg.insert_tag2_en = false;
710 txvlan_cfg.default_tag2 = 0;
712 ret = hns3_set_vlan_tx_offload_cfg(hns, &txvlan_cfg);
714 hns3_err(hw, "pf vlan set pvid failed, pvid =%u ,ret =%d", pvid,
719 hns3_update_tx_offload_cfg(hns, &txvlan_cfg);
724 hns3_store_port_base_vlan_info(struct hns3_adapter *hns, uint16_t pvid, int on)
726 struct hns3_hw *hw = &hns->hw;
728 hw->port_base_vlan_cfg.state = on ?
729 HNS3_PORT_BASE_VLAN_ENABLE : HNS3_PORT_BASE_VLAN_DISABLE;
731 hw->port_base_vlan_cfg.pvid = pvid;
735 hns3_rm_all_vlan_table(struct hns3_adapter *hns, bool is_del_list)
737 struct hns3_user_vlan_table *vlan_entry;
738 struct hns3_pf *pf = &hns->pf;
740 LIST_FOREACH(vlan_entry, &pf->vlan_list, next) {
741 if (vlan_entry->hd_tbl_status)
742 hns3_set_port_vlan_filter(hns, vlan_entry->vlan_id, 0);
744 vlan_entry->hd_tbl_status = false;
748 vlan_entry = LIST_FIRST(&pf->vlan_list);
750 LIST_REMOVE(vlan_entry, next);
751 rte_free(vlan_entry);
752 vlan_entry = LIST_FIRST(&pf->vlan_list);
758 hns3_add_all_vlan_table(struct hns3_adapter *hns)
760 struct hns3_user_vlan_table *vlan_entry;
761 struct hns3_pf *pf = &hns->pf;
763 LIST_FOREACH(vlan_entry, &pf->vlan_list, next) {
764 if (!vlan_entry->hd_tbl_status)
765 hns3_set_port_vlan_filter(hns, vlan_entry->vlan_id, 1);
767 vlan_entry->hd_tbl_status = true;
772 hns3_remove_all_vlan_table(struct hns3_adapter *hns)
774 struct hns3_hw *hw = &hns->hw;
777 hns3_rm_all_vlan_table(hns, true);
778 if (hw->port_base_vlan_cfg.pvid != HNS3_INVLID_PVID) {
779 ret = hns3_set_port_vlan_filter(hns,
780 hw->port_base_vlan_cfg.pvid, 0);
782 hns3_err(hw, "Failed to remove all vlan table, ret =%d",
790 hns3_update_vlan_filter_entries(struct hns3_adapter *hns,
791 uint16_t port_base_vlan_state,
792 uint16_t new_pvid, uint16_t old_pvid)
794 struct hns3_hw *hw = &hns->hw;
797 if (port_base_vlan_state == HNS3_PORT_BASE_VLAN_ENABLE) {
798 if (old_pvid != HNS3_INVLID_PVID && old_pvid != 0) {
799 ret = hns3_set_port_vlan_filter(hns, old_pvid, 0);
802 "Failed to clear clear old pvid filter, ret =%d",
808 hns3_rm_all_vlan_table(hns, false);
809 return hns3_set_port_vlan_filter(hns, new_pvid, 1);
813 ret = hns3_set_port_vlan_filter(hns, new_pvid, 0);
815 hns3_err(hw, "Failed to set port vlan filter, ret =%d",
821 if (new_pvid == hw->port_base_vlan_cfg.pvid)
822 hns3_add_all_vlan_table(hns);
828 hns3_en_pvid_strip(struct hns3_adapter *hns, int on)
830 struct hns3_rx_vtag_cfg *old_cfg = &hns->pf.vtag_config.rx_vcfg;
831 struct hns3_rx_vtag_cfg rx_vlan_cfg;
835 rx_strip_en = old_cfg->rx_vlan_offload_en ? true : false;
837 rx_vlan_cfg.strip_tag1_en = rx_strip_en;
838 rx_vlan_cfg.strip_tag2_en = true;
840 rx_vlan_cfg.strip_tag1_en = false;
841 rx_vlan_cfg.strip_tag2_en = rx_strip_en;
843 rx_vlan_cfg.vlan1_vlan_prionly = false;
844 rx_vlan_cfg.vlan2_vlan_prionly = false;
845 rx_vlan_cfg.rx_vlan_offload_en = old_cfg->rx_vlan_offload_en;
847 ret = hns3_set_vlan_rx_offload_cfg(hns, &rx_vlan_cfg);
851 hns3_update_rx_offload_cfg(hns, &rx_vlan_cfg);
856 hns3_vlan_pvid_configure(struct hns3_adapter *hns, uint16_t pvid, int on)
858 struct hns3_hw *hw = &hns->hw;
859 uint16_t port_base_vlan_state;
863 if (on == 0 && pvid != hw->port_base_vlan_cfg.pvid) {
864 if (hw->port_base_vlan_cfg.pvid != HNS3_INVLID_PVID)
865 hns3_warn(hw, "Invalid operation! As current pvid set "
866 "is %u, disable pvid %u is invalid",
867 hw->port_base_vlan_cfg.pvid, pvid);
871 port_base_vlan_state = on ? HNS3_PORT_BASE_VLAN_ENABLE :
872 HNS3_PORT_BASE_VLAN_DISABLE;
873 ret = hns3_vlan_txvlan_cfg(hns, port_base_vlan_state, pvid);
875 hns3_err(hw, "failed to config tx vlan for pvid, ret = %d",
880 ret = hns3_en_pvid_strip(hns, on);
882 hns3_err(hw, "failed to config rx vlan strip for pvid, "
887 if (pvid == HNS3_INVLID_PVID)
889 old_pvid = hw->port_base_vlan_cfg.pvid;
890 ret = hns3_update_vlan_filter_entries(hns, port_base_vlan_state, pvid,
893 hns3_err(hw, "Failed to update vlan filter entries, ret =%d",
899 hns3_store_port_base_vlan_info(hns, pvid, on);
904 hns3_vlan_pvid_set(struct rte_eth_dev *dev, uint16_t pvid, int on)
906 struct hns3_adapter *hns = dev->data->dev_private;
907 struct hns3_hw *hw = &hns->hw;
908 bool pvid_en_state_change;
912 if (pvid > RTE_ETHER_MAX_VLAN_ID) {
913 hns3_err(hw, "Invalid vlan_id = %u > %d", pvid,
914 RTE_ETHER_MAX_VLAN_ID);
919 * If PVID configuration state change, should refresh the PVID
920 * configuration state in struct hns3_tx_queue/hns3_rx_queue.
922 pvid_state = hw->port_base_vlan_cfg.state;
923 if ((on && pvid_state == HNS3_PORT_BASE_VLAN_ENABLE) ||
924 (!on && pvid_state == HNS3_PORT_BASE_VLAN_DISABLE))
925 pvid_en_state_change = false;
927 pvid_en_state_change = true;
929 rte_spinlock_lock(&hw->lock);
930 ret = hns3_vlan_pvid_configure(hns, pvid, on);
931 rte_spinlock_unlock(&hw->lock);
935 if (pvid_en_state_change)
936 hns3_update_all_queues_pvid_state(hw);
942 init_port_base_vlan_info(struct hns3_hw *hw)
944 hw->port_base_vlan_cfg.state = HNS3_PORT_BASE_VLAN_DISABLE;
945 hw->port_base_vlan_cfg.pvid = HNS3_INVLID_PVID;
949 hns3_default_vlan_config(struct hns3_adapter *hns)
951 struct hns3_hw *hw = &hns->hw;
954 ret = hns3_set_port_vlan_filter(hns, 0, 1);
956 hns3_err(hw, "default vlan 0 config failed, ret =%d", ret);
961 hns3_init_vlan_config(struct hns3_adapter *hns)
963 struct hns3_hw *hw = &hns->hw;
967 * This function can be called in the initialization and reset process,
968 * when in reset process, it means that hardware had been reseted
969 * successfully and we need to restore the hardware configuration to
970 * ensure that the hardware configuration remains unchanged before and
973 if (rte_atomic16_read(&hw->reset.resetting) == 0)
974 init_port_base_vlan_info(hw);
976 ret = hns3_vlan_filter_init(hns);
978 hns3_err(hw, "vlan init fail in pf, ret =%d", ret);
982 ret = hns3_vlan_tpid_configure(hns, ETH_VLAN_TYPE_INNER,
983 RTE_ETHER_TYPE_VLAN);
985 hns3_err(hw, "tpid set fail in pf, ret =%d", ret);
990 * When in the reinit dev stage of the reset process, the following
991 * vlan-related configurations may differ from those at initialization,
992 * we will restore configurations to hardware in hns3_restore_vlan_table
993 * and hns3_restore_vlan_conf later.
995 if (rte_atomic16_read(&hw->reset.resetting) == 0) {
996 ret = hns3_vlan_pvid_configure(hns, HNS3_INVLID_PVID, 0);
998 hns3_err(hw, "pvid set fail in pf, ret =%d", ret);
1002 ret = hns3_en_hw_strip_rxvtag(hns, false);
1004 hns3_err(hw, "rx strip configure fail in pf, ret =%d",
1010 return hns3_default_vlan_config(hns);
1014 hns3_restore_vlan_conf(struct hns3_adapter *hns)
1016 struct hns3_pf *pf = &hns->pf;
1017 struct hns3_hw *hw = &hns->hw;
1022 if (!hw->data->promiscuous) {
1023 /* restore vlan filter states */
1024 offloads = hw->data->dev_conf.rxmode.offloads;
1025 enable = offloads & DEV_RX_OFFLOAD_VLAN_FILTER ? true : false;
1026 ret = hns3_enable_vlan_filter(hns, enable);
1028 hns3_err(hw, "failed to restore vlan rx filter conf, "
1034 ret = hns3_set_vlan_rx_offload_cfg(hns, &pf->vtag_config.rx_vcfg);
1036 hns3_err(hw, "failed to restore vlan rx conf, ret = %d", ret);
1040 ret = hns3_set_vlan_tx_offload_cfg(hns, &pf->vtag_config.tx_vcfg);
1042 hns3_err(hw, "failed to restore vlan tx conf, ret = %d", ret);
1048 hns3_dev_configure_vlan(struct rte_eth_dev *dev)
1050 struct hns3_adapter *hns = dev->data->dev_private;
1051 struct rte_eth_dev_data *data = dev->data;
1052 struct rte_eth_txmode *txmode;
1053 struct hns3_hw *hw = &hns->hw;
1057 txmode = &data->dev_conf.txmode;
1058 if (txmode->hw_vlan_reject_tagged || txmode->hw_vlan_reject_untagged)
1060 "hw_vlan_reject_tagged or hw_vlan_reject_untagged "
1061 "configuration is not supported! Ignore these two "
1062 "parameters: hw_vlan_reject_tagged(%d), "
1063 "hw_vlan_reject_untagged(%d)",
1064 txmode->hw_vlan_reject_tagged,
1065 txmode->hw_vlan_reject_untagged);
1067 /* Apply vlan offload setting */
1068 mask = ETH_VLAN_STRIP_MASK | ETH_VLAN_FILTER_MASK;
1069 ret = hns3_vlan_offload_set(dev, mask);
1071 hns3_err(hw, "dev config rx vlan offload failed, ret = %d",
1077 * If pvid config is not set in rte_eth_conf, driver needn't to set
1078 * VLAN pvid related configuration to hardware.
1080 if (txmode->pvid == 0 && txmode->hw_vlan_insert_pvid == 0)
1083 /* Apply pvid setting */
1084 ret = hns3_vlan_pvid_set(dev, txmode->pvid,
1085 txmode->hw_vlan_insert_pvid);
1087 hns3_err(hw, "dev config vlan pvid(%d) failed, ret = %d",
1094 hns3_config_tso(struct hns3_hw *hw, unsigned int tso_mss_min,
1095 unsigned int tso_mss_max)
1097 struct hns3_cfg_tso_status_cmd *req;
1098 struct hns3_cmd_desc desc;
1101 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_TSO_GENERIC_CONFIG, false);
1103 req = (struct hns3_cfg_tso_status_cmd *)desc.data;
1106 hns3_set_field(tso_mss, HNS3_TSO_MSS_MIN_M, HNS3_TSO_MSS_MIN_S,
1108 req->tso_mss_min = rte_cpu_to_le_16(tso_mss);
1111 hns3_set_field(tso_mss, HNS3_TSO_MSS_MIN_M, HNS3_TSO_MSS_MIN_S,
1113 req->tso_mss_max = rte_cpu_to_le_16(tso_mss);
1115 return hns3_cmd_send(hw, &desc, 1);
1119 hns3_set_umv_space(struct hns3_hw *hw, uint16_t space_size,
1120 uint16_t *allocated_size, bool is_alloc)
1122 struct hns3_umv_spc_alc_cmd *req;
1123 struct hns3_cmd_desc desc;
1126 req = (struct hns3_umv_spc_alc_cmd *)desc.data;
1127 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_MAC_VLAN_ALLOCATE, false);
1128 hns3_set_bit(req->allocate, HNS3_UMV_SPC_ALC_B, is_alloc ? 0 : 1);
1129 req->space_size = rte_cpu_to_le_32(space_size);
1131 ret = hns3_cmd_send(hw, &desc, 1);
1133 PMD_INIT_LOG(ERR, "%s umv space failed for cmd_send, ret =%d",
1134 is_alloc ? "allocate" : "free", ret);
1138 if (is_alloc && allocated_size)
1139 *allocated_size = rte_le_to_cpu_32(desc.data[1]);
1145 hns3_init_umv_space(struct hns3_hw *hw)
1147 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
1148 struct hns3_pf *pf = &hns->pf;
1149 uint16_t allocated_size = 0;
1152 ret = hns3_set_umv_space(hw, pf->wanted_umv_size, &allocated_size,
1157 if (allocated_size < pf->wanted_umv_size)
1158 PMD_INIT_LOG(WARNING, "Alloc umv space failed, want %u, get %u",
1159 pf->wanted_umv_size, allocated_size);
1161 pf->max_umv_size = (!!allocated_size) ? allocated_size :
1162 pf->wanted_umv_size;
1163 pf->used_umv_size = 0;
1168 hns3_uninit_umv_space(struct hns3_hw *hw)
1170 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
1171 struct hns3_pf *pf = &hns->pf;
1174 if (pf->max_umv_size == 0)
1177 ret = hns3_set_umv_space(hw, pf->max_umv_size, NULL, false);
1181 pf->max_umv_size = 0;
1187 hns3_is_umv_space_full(struct hns3_hw *hw)
1189 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
1190 struct hns3_pf *pf = &hns->pf;
1193 is_full = (pf->used_umv_size >= pf->max_umv_size);
1199 hns3_update_umv_space(struct hns3_hw *hw, bool is_free)
1201 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
1202 struct hns3_pf *pf = &hns->pf;
1205 if (pf->used_umv_size > 0)
1206 pf->used_umv_size--;
1208 pf->used_umv_size++;
1212 hns3_prepare_mac_addr(struct hns3_mac_vlan_tbl_entry_cmd *new_req,
1213 const uint8_t *addr, bool is_mc)
1215 const unsigned char *mac_addr = addr;
1216 uint32_t high_val = ((uint32_t)mac_addr[3] << 24) |
1217 ((uint32_t)mac_addr[2] << 16) |
1218 ((uint32_t)mac_addr[1] << 8) |
1219 (uint32_t)mac_addr[0];
1220 uint32_t low_val = ((uint32_t)mac_addr[5] << 8) | (uint32_t)mac_addr[4];
1222 hns3_set_bit(new_req->flags, HNS3_MAC_VLAN_BIT0_EN_B, 1);
1224 hns3_set_bit(new_req->entry_type, HNS3_MAC_VLAN_BIT0_EN_B, 0);
1225 hns3_set_bit(new_req->entry_type, HNS3_MAC_VLAN_BIT1_EN_B, 1);
1226 hns3_set_bit(new_req->mc_mac_en, HNS3_MAC_VLAN_BIT0_EN_B, 1);
1229 new_req->mac_addr_hi32 = rte_cpu_to_le_32(high_val);
1230 new_req->mac_addr_lo16 = rte_cpu_to_le_16(low_val & 0xffff);
1234 hns3_get_mac_vlan_cmd_status(struct hns3_hw *hw, uint16_t cmdq_resp,
1236 enum hns3_mac_vlan_tbl_opcode op)
1239 hns3_err(hw, "cmdq execute failed for get_mac_vlan_cmd_status,status=%u",
1244 if (op == HNS3_MAC_VLAN_ADD) {
1245 if (resp_code == 0 || resp_code == 1) {
1247 } else if (resp_code == HNS3_ADD_UC_OVERFLOW) {
1248 hns3_err(hw, "add mac addr failed for uc_overflow");
1250 } else if (resp_code == HNS3_ADD_MC_OVERFLOW) {
1251 hns3_err(hw, "add mac addr failed for mc_overflow");
1255 hns3_err(hw, "add mac addr failed for undefined, code=%u",
1258 } else if (op == HNS3_MAC_VLAN_REMOVE) {
1259 if (resp_code == 0) {
1261 } else if (resp_code == 1) {
1262 hns3_dbg(hw, "remove mac addr failed for miss");
1266 hns3_err(hw, "remove mac addr failed for undefined, code=%u",
1269 } else if (op == HNS3_MAC_VLAN_LKUP) {
1270 if (resp_code == 0) {
1272 } else if (resp_code == 1) {
1273 hns3_dbg(hw, "lookup mac addr failed for miss");
1277 hns3_err(hw, "lookup mac addr failed for undefined, code=%u",
1282 hns3_err(hw, "unknown opcode for get_mac_vlan_cmd_status, opcode=%u",
1289 hns3_lookup_mac_vlan_tbl(struct hns3_hw *hw,
1290 struct hns3_mac_vlan_tbl_entry_cmd *req,
1291 struct hns3_cmd_desc *desc, bool is_mc)
1297 hns3_cmd_setup_basic_desc(&desc[0], HNS3_OPC_MAC_VLAN_ADD, true);
1299 desc[0].flag |= rte_cpu_to_le_16(HNS3_CMD_FLAG_NEXT);
1300 memcpy(desc[0].data, req,
1301 sizeof(struct hns3_mac_vlan_tbl_entry_cmd));
1302 hns3_cmd_setup_basic_desc(&desc[1], HNS3_OPC_MAC_VLAN_ADD,
1304 desc[1].flag |= rte_cpu_to_le_16(HNS3_CMD_FLAG_NEXT);
1305 hns3_cmd_setup_basic_desc(&desc[2], HNS3_OPC_MAC_VLAN_ADD,
1307 ret = hns3_cmd_send(hw, desc, HNS3_MC_MAC_VLAN_ADD_DESC_NUM);
1309 memcpy(desc[0].data, req,
1310 sizeof(struct hns3_mac_vlan_tbl_entry_cmd));
1311 ret = hns3_cmd_send(hw, desc, 1);
1314 hns3_err(hw, "lookup mac addr failed for cmd_send, ret =%d.",
1318 resp_code = (rte_le_to_cpu_32(desc[0].data[0]) >> 8) & 0xff;
1319 retval = rte_le_to_cpu_16(desc[0].retval);
1321 return hns3_get_mac_vlan_cmd_status(hw, retval, resp_code,
1322 HNS3_MAC_VLAN_LKUP);
1326 hns3_add_mac_vlan_tbl(struct hns3_hw *hw,
1327 struct hns3_mac_vlan_tbl_entry_cmd *req,
1328 struct hns3_cmd_desc *mc_desc)
1335 if (mc_desc == NULL) {
1336 struct hns3_cmd_desc desc;
1338 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_MAC_VLAN_ADD, false);
1339 memcpy(desc.data, req,
1340 sizeof(struct hns3_mac_vlan_tbl_entry_cmd));
1341 ret = hns3_cmd_send(hw, &desc, 1);
1342 resp_code = (rte_le_to_cpu_32(desc.data[0]) >> 8) & 0xff;
1343 retval = rte_le_to_cpu_16(desc.retval);
1345 cfg_status = hns3_get_mac_vlan_cmd_status(hw, retval, resp_code,
1348 hns3_cmd_reuse_desc(&mc_desc[0], false);
1349 mc_desc[0].flag |= rte_cpu_to_le_16(HNS3_CMD_FLAG_NEXT);
1350 hns3_cmd_reuse_desc(&mc_desc[1], false);
1351 mc_desc[1].flag |= rte_cpu_to_le_16(HNS3_CMD_FLAG_NEXT);
1352 hns3_cmd_reuse_desc(&mc_desc[2], false);
1353 mc_desc[2].flag &= rte_cpu_to_le_16(~HNS3_CMD_FLAG_NEXT);
1354 memcpy(mc_desc[0].data, req,
1355 sizeof(struct hns3_mac_vlan_tbl_entry_cmd));
1356 mc_desc[0].retval = 0;
1357 ret = hns3_cmd_send(hw, mc_desc, HNS3_MC_MAC_VLAN_ADD_DESC_NUM);
1358 resp_code = (rte_le_to_cpu_32(mc_desc[0].data[0]) >> 8) & 0xff;
1359 retval = rte_le_to_cpu_16(mc_desc[0].retval);
1361 cfg_status = hns3_get_mac_vlan_cmd_status(hw, retval, resp_code,
1366 hns3_err(hw, "add mac addr failed for cmd_send, ret =%d", ret);
1374 hns3_remove_mac_vlan_tbl(struct hns3_hw *hw,
1375 struct hns3_mac_vlan_tbl_entry_cmd *req)
1377 struct hns3_cmd_desc desc;
1382 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_MAC_VLAN_REMOVE, false);
1384 memcpy(desc.data, req, sizeof(struct hns3_mac_vlan_tbl_entry_cmd));
1386 ret = hns3_cmd_send(hw, &desc, 1);
1388 hns3_err(hw, "del mac addr failed for cmd_send, ret =%d", ret);
1391 resp_code = (rte_le_to_cpu_32(desc.data[0]) >> 8) & 0xff;
1392 retval = rte_le_to_cpu_16(desc.retval);
1394 return hns3_get_mac_vlan_cmd_status(hw, retval, resp_code,
1395 HNS3_MAC_VLAN_REMOVE);
1399 hns3_add_uc_addr_common(struct hns3_hw *hw, struct rte_ether_addr *mac_addr)
1401 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
1402 struct hns3_mac_vlan_tbl_entry_cmd req;
1403 struct hns3_pf *pf = &hns->pf;
1404 struct hns3_cmd_desc desc;
1405 char mac_str[RTE_ETHER_ADDR_FMT_SIZE];
1406 uint16_t egress_port = 0;
1410 /* check if mac addr is valid */
1411 if (!rte_is_valid_assigned_ether_addr(mac_addr)) {
1412 rte_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
1414 hns3_err(hw, "Add unicast mac addr err! addr(%s) invalid",
1419 memset(&req, 0, sizeof(req));
1422 * In current version VF is not supported when PF is driven by DPDK
1423 * driver, just need to configure parameters for PF vport.
1425 vf_id = HNS3_PF_FUNC_ID;
1426 hns3_set_field(egress_port, HNS3_MAC_EPORT_VFID_M,
1427 HNS3_MAC_EPORT_VFID_S, vf_id);
1429 req.egress_port = rte_cpu_to_le_16(egress_port);
1431 hns3_prepare_mac_addr(&req, mac_addr->addr_bytes, false);
1434 * Lookup the mac address in the mac_vlan table, and add
1435 * it if the entry is inexistent. Repeated unicast entry
1436 * is not allowed in the mac vlan table.
1438 ret = hns3_lookup_mac_vlan_tbl(hw, &req, &desc, false);
1439 if (ret == -ENOENT) {
1440 if (!hns3_is_umv_space_full(hw)) {
1441 ret = hns3_add_mac_vlan_tbl(hw, &req, NULL);
1443 hns3_update_umv_space(hw, false);
1447 hns3_err(hw, "UC MAC table full(%u)", pf->used_umv_size);
1452 rte_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE, mac_addr);
1454 /* check if we just hit the duplicate */
1456 hns3_dbg(hw, "mac addr(%s) has been in the MAC table", mac_str);
1460 hns3_err(hw, "PF failed to add unicast entry(%s) in the MAC table",
1467 hns3_add_mc_addr_common(struct hns3_hw *hw, struct rte_ether_addr *mac_addr)
1469 char mac_str[RTE_ETHER_ADDR_FMT_SIZE];
1470 struct rte_ether_addr *addr;
1474 for (i = 0; i < hw->mc_addrs_num; i++) {
1475 addr = &hw->mc_addrs[i];
1476 /* Check if there are duplicate addresses */
1477 if (rte_is_same_ether_addr(addr, mac_addr)) {
1478 rte_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
1480 hns3_err(hw, "failed to add mc mac addr, same addrs"
1481 "(%s) is added by the set_mc_mac_addr_list "
1487 ret = hns3_add_mc_addr(hw, mac_addr);
1489 rte_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
1491 hns3_err(hw, "failed to add mc mac addr(%s), ret = %d",
1498 hns3_remove_mc_addr_common(struct hns3_hw *hw, struct rte_ether_addr *mac_addr)
1500 char mac_str[RTE_ETHER_ADDR_FMT_SIZE];
1503 ret = hns3_remove_mc_addr(hw, mac_addr);
1505 rte_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
1507 hns3_err(hw, "failed to remove mc mac addr(%s), ret = %d",
1514 hns3_add_mac_addr(struct rte_eth_dev *dev, struct rte_ether_addr *mac_addr,
1515 uint32_t idx, __rte_unused uint32_t pool)
1517 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1518 char mac_str[RTE_ETHER_ADDR_FMT_SIZE];
1521 rte_spinlock_lock(&hw->lock);
1524 * In hns3 network engine adding UC and MC mac address with different
1525 * commands with firmware. We need to determine whether the input
1526 * address is a UC or a MC address to call different commands.
1527 * By the way, it is recommended calling the API function named
1528 * rte_eth_dev_set_mc_addr_list to set the MC mac address, because
1529 * using the rte_eth_dev_mac_addr_add API function to set MC mac address
1530 * may affect the specifications of UC mac addresses.
1532 if (rte_is_multicast_ether_addr(mac_addr))
1533 ret = hns3_add_mc_addr_common(hw, mac_addr);
1535 ret = hns3_add_uc_addr_common(hw, mac_addr);
1538 rte_spinlock_unlock(&hw->lock);
1539 rte_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
1541 hns3_err(hw, "failed to add mac addr(%s), ret = %d", mac_str,
1547 hw->mac.default_addr_setted = true;
1548 rte_spinlock_unlock(&hw->lock);
1554 hns3_remove_uc_addr_common(struct hns3_hw *hw, struct rte_ether_addr *mac_addr)
1556 struct hns3_mac_vlan_tbl_entry_cmd req;
1557 char mac_str[RTE_ETHER_ADDR_FMT_SIZE];
1560 /* check if mac addr is valid */
1561 if (!rte_is_valid_assigned_ether_addr(mac_addr)) {
1562 rte_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
1564 hns3_err(hw, "remove unicast mac addr err! addr(%s) invalid",
1569 memset(&req, 0, sizeof(req));
1570 hns3_set_bit(req.entry_type, HNS3_MAC_VLAN_BIT0_EN_B, 0);
1571 hns3_prepare_mac_addr(&req, mac_addr->addr_bytes, false);
1572 ret = hns3_remove_mac_vlan_tbl(hw, &req);
1573 if (ret == -ENOENT) /* mac addr isn't existent in the mac vlan table. */
1576 hns3_update_umv_space(hw, true);
1582 hns3_remove_mac_addr(struct rte_eth_dev *dev, uint32_t idx)
1584 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1585 /* index will be checked by upper level rte interface */
1586 struct rte_ether_addr *mac_addr = &dev->data->mac_addrs[idx];
1587 char mac_str[RTE_ETHER_ADDR_FMT_SIZE];
1590 rte_spinlock_lock(&hw->lock);
1592 if (rte_is_multicast_ether_addr(mac_addr))
1593 ret = hns3_remove_mc_addr_common(hw, mac_addr);
1595 ret = hns3_remove_uc_addr_common(hw, mac_addr);
1596 rte_spinlock_unlock(&hw->lock);
1598 rte_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
1600 hns3_err(hw, "failed to remove mac addr(%s), ret = %d", mac_str,
1606 hns3_set_default_mac_addr(struct rte_eth_dev *dev,
1607 struct rte_ether_addr *mac_addr)
1609 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1610 struct rte_ether_addr *oaddr;
1611 char mac_str[RTE_ETHER_ADDR_FMT_SIZE];
1612 bool default_addr_setted;
1613 bool rm_succes = false;
1617 * It has been guaranteed that input parameter named mac_addr is valid
1618 * address in the rte layer of DPDK framework.
1620 oaddr = (struct rte_ether_addr *)hw->mac.mac_addr;
1621 default_addr_setted = hw->mac.default_addr_setted;
1622 if (default_addr_setted && !!rte_is_same_ether_addr(mac_addr, oaddr))
1625 rte_spinlock_lock(&hw->lock);
1626 if (default_addr_setted) {
1627 ret = hns3_remove_uc_addr_common(hw, oaddr);
1629 rte_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
1631 hns3_warn(hw, "Remove old uc mac address(%s) fail: %d",
1638 ret = hns3_add_uc_addr_common(hw, mac_addr);
1640 rte_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
1642 hns3_err(hw, "Failed to set mac addr(%s): %d", mac_str, ret);
1643 goto err_add_uc_addr;
1646 ret = hns3_pause_addr_cfg(hw, mac_addr->addr_bytes);
1648 hns3_err(hw, "Failed to configure mac pause address: %d", ret);
1649 goto err_pause_addr_cfg;
1652 rte_ether_addr_copy(mac_addr,
1653 (struct rte_ether_addr *)hw->mac.mac_addr);
1654 hw->mac.default_addr_setted = true;
1655 rte_spinlock_unlock(&hw->lock);
1660 ret_val = hns3_remove_uc_addr_common(hw, mac_addr);
1662 rte_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
1665 "Failed to roll back to del setted mac addr(%s): %d",
1671 ret_val = hns3_add_uc_addr_common(hw, oaddr);
1673 rte_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
1676 "Failed to restore old uc mac addr(%s): %d",
1678 hw->mac.default_addr_setted = false;
1681 rte_spinlock_unlock(&hw->lock);
1687 hns3_configure_all_mac_addr(struct hns3_adapter *hns, bool del)
1689 char mac_str[RTE_ETHER_ADDR_FMT_SIZE];
1690 struct hns3_hw *hw = &hns->hw;
1691 struct rte_ether_addr *addr;
1696 for (i = 0; i < HNS3_UC_MACADDR_NUM; i++) {
1697 addr = &hw->data->mac_addrs[i];
1698 if (rte_is_zero_ether_addr(addr))
1700 if (rte_is_multicast_ether_addr(addr))
1701 ret = del ? hns3_remove_mc_addr(hw, addr) :
1702 hns3_add_mc_addr(hw, addr);
1704 ret = del ? hns3_remove_uc_addr_common(hw, addr) :
1705 hns3_add_uc_addr_common(hw, addr);
1709 rte_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
1711 hns3_err(hw, "failed to %s mac addr(%s) index:%d "
1712 "ret = %d.", del ? "remove" : "restore",
1720 hns3_update_desc_vfid(struct hns3_cmd_desc *desc, uint8_t vfid, bool clr)
1722 #define HNS3_VF_NUM_IN_FIRST_DESC 192
1726 if (vfid < HNS3_VF_NUM_IN_FIRST_DESC) {
1727 word_num = vfid / 32;
1728 bit_num = vfid % 32;
1730 desc[1].data[word_num] &=
1731 rte_cpu_to_le_32(~(1UL << bit_num));
1733 desc[1].data[word_num] |=
1734 rte_cpu_to_le_32(1UL << bit_num);
1736 word_num = (vfid - HNS3_VF_NUM_IN_FIRST_DESC) / 32;
1737 bit_num = vfid % 32;
1739 desc[2].data[word_num] &=
1740 rte_cpu_to_le_32(~(1UL << bit_num));
1742 desc[2].data[word_num] |=
1743 rte_cpu_to_le_32(1UL << bit_num);
1748 hns3_add_mc_addr(struct hns3_hw *hw, struct rte_ether_addr *mac_addr)
1750 struct hns3_mac_vlan_tbl_entry_cmd req;
1751 struct hns3_cmd_desc desc[3];
1752 char mac_str[RTE_ETHER_ADDR_FMT_SIZE];
1756 /* Check if mac addr is valid */
1757 if (!rte_is_multicast_ether_addr(mac_addr)) {
1758 rte_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
1760 hns3_err(hw, "failed to add mc mac addr, addr(%s) invalid",
1765 memset(&req, 0, sizeof(req));
1766 hns3_set_bit(req.entry_type, HNS3_MAC_VLAN_BIT0_EN_B, 0);
1767 hns3_prepare_mac_addr(&req, mac_addr->addr_bytes, true);
1768 ret = hns3_lookup_mac_vlan_tbl(hw, &req, desc, true);
1770 /* This mac addr do not exist, add new entry for it */
1771 memset(desc[0].data, 0, sizeof(desc[0].data));
1772 memset(desc[1].data, 0, sizeof(desc[0].data));
1773 memset(desc[2].data, 0, sizeof(desc[0].data));
1777 * In current version VF is not supported when PF is driven by DPDK
1778 * driver, just need to configure parameters for PF vport.
1780 vf_id = HNS3_PF_FUNC_ID;
1781 hns3_update_desc_vfid(desc, vf_id, false);
1782 ret = hns3_add_mac_vlan_tbl(hw, &req, desc);
1785 hns3_err(hw, "mc mac vlan table is full");
1786 rte_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
1788 hns3_err(hw, "failed to add mc mac addr(%s): %d", mac_str, ret);
1795 hns3_remove_mc_addr(struct hns3_hw *hw, struct rte_ether_addr *mac_addr)
1797 struct hns3_mac_vlan_tbl_entry_cmd req;
1798 struct hns3_cmd_desc desc[3];
1799 char mac_str[RTE_ETHER_ADDR_FMT_SIZE];
1803 /* Check if mac addr is valid */
1804 if (!rte_is_multicast_ether_addr(mac_addr)) {
1805 rte_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
1807 hns3_err(hw, "Failed to rm mc mac addr, addr(%s) invalid",
1812 memset(&req, 0, sizeof(req));
1813 hns3_set_bit(req.entry_type, HNS3_MAC_VLAN_BIT0_EN_B, 0);
1814 hns3_prepare_mac_addr(&req, mac_addr->addr_bytes, true);
1815 ret = hns3_lookup_mac_vlan_tbl(hw, &req, desc, true);
1818 * This mac addr exist, remove this handle's VFID for it.
1819 * In current version VF is not supported when PF is driven by
1820 * DPDK driver, just need to configure parameters for PF vport.
1822 vf_id = HNS3_PF_FUNC_ID;
1823 hns3_update_desc_vfid(desc, vf_id, true);
1825 /* All the vfid is zero, so need to delete this entry */
1826 ret = hns3_remove_mac_vlan_tbl(hw, &req);
1827 } else if (ret == -ENOENT) {
1828 /* This mac addr doesn't exist. */
1833 rte_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
1835 hns3_err(hw, "Failed to rm mc mac addr(%s): %d", mac_str, ret);
1842 hns3_set_mc_addr_chk_param(struct hns3_hw *hw,
1843 struct rte_ether_addr *mc_addr_set,
1844 uint32_t nb_mc_addr)
1846 char mac_str[RTE_ETHER_ADDR_FMT_SIZE];
1847 struct rte_ether_addr *addr;
1851 if (nb_mc_addr > HNS3_MC_MACADDR_NUM) {
1852 hns3_err(hw, "failed to set mc mac addr, nb_mc_addr(%d) "
1853 "invalid. valid range: 0~%d",
1854 nb_mc_addr, HNS3_MC_MACADDR_NUM);
1858 /* Check if input mac addresses are valid */
1859 for (i = 0; i < nb_mc_addr; i++) {
1860 addr = &mc_addr_set[i];
1861 if (!rte_is_multicast_ether_addr(addr)) {
1862 rte_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
1865 "failed to set mc mac addr, addr(%s) invalid.",
1870 /* Check if there are duplicate addresses */
1871 for (j = i + 1; j < nb_mc_addr; j++) {
1872 if (rte_is_same_ether_addr(addr, &mc_addr_set[j])) {
1873 rte_ether_format_addr(mac_str,
1874 RTE_ETHER_ADDR_FMT_SIZE,
1876 hns3_err(hw, "failed to set mc mac addr, "
1877 "addrs invalid. two same addrs(%s).",
1884 * Check if there are duplicate addresses between mac_addrs
1887 for (j = 0; j < HNS3_UC_MACADDR_NUM; j++) {
1888 if (rte_is_same_ether_addr(addr,
1889 &hw->data->mac_addrs[j])) {
1890 rte_ether_format_addr(mac_str,
1891 RTE_ETHER_ADDR_FMT_SIZE,
1893 hns3_err(hw, "failed to set mc mac addr, "
1894 "addrs invalid. addrs(%s) has already "
1895 "configured in mac_addr add API",
1906 hns3_set_mc_addr_calc_addr(struct hns3_hw *hw,
1907 struct rte_ether_addr *mc_addr_set,
1909 struct rte_ether_addr *reserved_addr_list,
1910 int *reserved_addr_num,
1911 struct rte_ether_addr *add_addr_list,
1913 struct rte_ether_addr *rm_addr_list,
1916 struct rte_ether_addr *addr;
1917 int current_addr_num;
1918 int reserved_num = 0;
1926 /* Calculate the mc mac address list that should be removed */
1927 current_addr_num = hw->mc_addrs_num;
1928 for (i = 0; i < current_addr_num; i++) {
1929 addr = &hw->mc_addrs[i];
1931 for (j = 0; j < mc_addr_num; j++) {
1932 if (rte_is_same_ether_addr(addr, &mc_addr_set[j])) {
1939 rte_ether_addr_copy(addr, &rm_addr_list[rm_num]);
1942 rte_ether_addr_copy(addr,
1943 &reserved_addr_list[reserved_num]);
1948 /* Calculate the mc mac address list that should be added */
1949 for (i = 0; i < mc_addr_num; i++) {
1950 addr = &mc_addr_set[i];
1952 for (j = 0; j < current_addr_num; j++) {
1953 if (rte_is_same_ether_addr(addr, &hw->mc_addrs[j])) {
1960 rte_ether_addr_copy(addr, &add_addr_list[add_num]);
1965 /* Reorder the mc mac address list maintained by driver */
1966 for (i = 0; i < reserved_num; i++)
1967 rte_ether_addr_copy(&reserved_addr_list[i], &hw->mc_addrs[i]);
1969 for (i = 0; i < rm_num; i++) {
1970 num = reserved_num + i;
1971 rte_ether_addr_copy(&rm_addr_list[i], &hw->mc_addrs[num]);
1974 *reserved_addr_num = reserved_num;
1975 *add_addr_num = add_num;
1976 *rm_addr_num = rm_num;
1980 hns3_set_mc_mac_addr_list(struct rte_eth_dev *dev,
1981 struct rte_ether_addr *mc_addr_set,
1982 uint32_t nb_mc_addr)
1984 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1985 struct rte_ether_addr reserved_addr_list[HNS3_MC_MACADDR_NUM];
1986 struct rte_ether_addr add_addr_list[HNS3_MC_MACADDR_NUM];
1987 struct rte_ether_addr rm_addr_list[HNS3_MC_MACADDR_NUM];
1988 struct rte_ether_addr *addr;
1989 int reserved_addr_num;
1997 /* Check if input parameters are valid */
1998 ret = hns3_set_mc_addr_chk_param(hw, mc_addr_set, nb_mc_addr);
2002 rte_spinlock_lock(&hw->lock);
2005 * Calculate the mc mac address lists those should be removed and be
2006 * added, Reorder the mc mac address list maintained by driver.
2008 mc_addr_num = (int)nb_mc_addr;
2009 hns3_set_mc_addr_calc_addr(hw, mc_addr_set, mc_addr_num,
2010 reserved_addr_list, &reserved_addr_num,
2011 add_addr_list, &add_addr_num,
2012 rm_addr_list, &rm_addr_num);
2014 /* Remove mc mac addresses */
2015 for (i = 0; i < rm_addr_num; i++) {
2016 num = rm_addr_num - i - 1;
2017 addr = &rm_addr_list[num];
2018 ret = hns3_remove_mc_addr(hw, addr);
2020 rte_spinlock_unlock(&hw->lock);
2026 /* Add mc mac addresses */
2027 for (i = 0; i < add_addr_num; i++) {
2028 addr = &add_addr_list[i];
2029 ret = hns3_add_mc_addr(hw, addr);
2031 rte_spinlock_unlock(&hw->lock);
2035 num = reserved_addr_num + i;
2036 rte_ether_addr_copy(addr, &hw->mc_addrs[num]);
2039 rte_spinlock_unlock(&hw->lock);
2045 hns3_configure_all_mc_mac_addr(struct hns3_adapter *hns, bool del)
2047 char mac_str[RTE_ETHER_ADDR_FMT_SIZE];
2048 struct hns3_hw *hw = &hns->hw;
2049 struct rte_ether_addr *addr;
2054 for (i = 0; i < hw->mc_addrs_num; i++) {
2055 addr = &hw->mc_addrs[i];
2056 if (!rte_is_multicast_ether_addr(addr))
2059 ret = hns3_remove_mc_addr(hw, addr);
2061 ret = hns3_add_mc_addr(hw, addr);
2064 rte_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
2066 hns3_dbg(hw, "%s mc mac addr: %s failed for pf: ret = %d",
2067 del ? "Remove" : "Restore", mac_str, ret);
2074 hns3_check_mq_mode(struct rte_eth_dev *dev)
2076 enum rte_eth_rx_mq_mode rx_mq_mode = dev->data->dev_conf.rxmode.mq_mode;
2077 enum rte_eth_tx_mq_mode tx_mq_mode = dev->data->dev_conf.txmode.mq_mode;
2078 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2079 struct hns3_pf *pf = HNS3_DEV_PRIVATE_TO_PF(dev->data->dev_private);
2080 struct rte_eth_dcb_rx_conf *dcb_rx_conf;
2081 struct rte_eth_dcb_tx_conf *dcb_tx_conf;
2086 dcb_rx_conf = &dev->data->dev_conf.rx_adv_conf.dcb_rx_conf;
2087 dcb_tx_conf = &dev->data->dev_conf.tx_adv_conf.dcb_tx_conf;
2089 if (rx_mq_mode == ETH_MQ_RX_VMDQ_DCB_RSS) {
2090 hns3_err(hw, "ETH_MQ_RX_VMDQ_DCB_RSS is not supported. "
2091 "rx_mq_mode = %d", rx_mq_mode);
2095 if (rx_mq_mode == ETH_MQ_RX_VMDQ_DCB ||
2096 tx_mq_mode == ETH_MQ_TX_VMDQ_DCB) {
2097 hns3_err(hw, "ETH_MQ_RX_VMDQ_DCB and ETH_MQ_TX_VMDQ_DCB "
2098 "is not supported. rx_mq_mode = %d, tx_mq_mode = %d",
2099 rx_mq_mode, tx_mq_mode);
2103 if (rx_mq_mode == ETH_MQ_RX_DCB_RSS) {
2104 if (dcb_rx_conf->nb_tcs > pf->tc_max) {
2105 hns3_err(hw, "nb_tcs(%u) > max_tc(%u) driver supported.",
2106 dcb_rx_conf->nb_tcs, pf->tc_max);
2110 if (!(dcb_rx_conf->nb_tcs == HNS3_4_TCS ||
2111 dcb_rx_conf->nb_tcs == HNS3_8_TCS)) {
2112 hns3_err(hw, "on ETH_MQ_RX_DCB_RSS mode, "
2113 "nb_tcs(%d) != %d or %d in rx direction.",
2114 dcb_rx_conf->nb_tcs, HNS3_4_TCS, HNS3_8_TCS);
2118 if (dcb_rx_conf->nb_tcs != dcb_tx_conf->nb_tcs) {
2119 hns3_err(hw, "num_tcs(%d) of tx is not equal to rx(%d)",
2120 dcb_tx_conf->nb_tcs, dcb_rx_conf->nb_tcs);
2124 for (i = 0; i < HNS3_MAX_USER_PRIO; i++) {
2125 if (dcb_rx_conf->dcb_tc[i] != dcb_tx_conf->dcb_tc[i]) {
2126 hns3_err(hw, "dcb_tc[%d] = %d in rx direction, "
2127 "is not equal to one in tx direction.",
2128 i, dcb_rx_conf->dcb_tc[i]);
2131 if (dcb_rx_conf->dcb_tc[i] > max_tc)
2132 max_tc = dcb_rx_conf->dcb_tc[i];
2135 num_tc = max_tc + 1;
2136 if (num_tc > dcb_rx_conf->nb_tcs) {
2137 hns3_err(hw, "max num_tc(%u) mapped > nb_tcs(%u)",
2138 num_tc, dcb_rx_conf->nb_tcs);
2147 hns3_check_dcb_cfg(struct rte_eth_dev *dev)
2149 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2151 if (!hns3_dev_dcb_supported(hw)) {
2152 hns3_err(hw, "this port does not support dcb configurations.");
2156 if (hw->current_fc_status == HNS3_FC_STATUS_MAC_PAUSE) {
2157 hns3_err(hw, "MAC pause enabled, cannot config dcb info.");
2161 /* Check multiple queue mode */
2162 return hns3_check_mq_mode(dev);
2166 hns3_bind_ring_with_vector(struct hns3_hw *hw, uint8_t vector_id, bool mmap,
2167 enum hns3_ring_type queue_type, uint16_t queue_id)
2169 struct hns3_cmd_desc desc;
2170 struct hns3_ctrl_vector_chain_cmd *req =
2171 (struct hns3_ctrl_vector_chain_cmd *)desc.data;
2172 enum hns3_cmd_status status;
2173 enum hns3_opcode_type op;
2174 uint16_t tqp_type_and_id = 0;
2179 op = mmap ? HNS3_OPC_ADD_RING_TO_VECTOR : HNS3_OPC_DEL_RING_TO_VECTOR;
2180 hns3_cmd_setup_basic_desc(&desc, op, false);
2181 req->int_vector_id = vector_id;
2183 if (queue_type == HNS3_RING_TYPE_RX)
2184 gl = HNS3_RING_GL_RX;
2186 gl = HNS3_RING_GL_TX;
2190 hns3_set_field(tqp_type_and_id, HNS3_INT_TYPE_M, HNS3_INT_TYPE_S,
2192 hns3_set_field(tqp_type_and_id, HNS3_TQP_ID_M, HNS3_TQP_ID_S, queue_id);
2193 hns3_set_field(tqp_type_and_id, HNS3_INT_GL_IDX_M, HNS3_INT_GL_IDX_S,
2195 req->tqp_type_and_id[0] = rte_cpu_to_le_16(tqp_type_and_id);
2196 req->int_cause_num = 1;
2197 op_str = mmap ? "Map" : "Unmap";
2198 status = hns3_cmd_send(hw, &desc, 1);
2200 hns3_err(hw, "%s TQP %d fail, vector_id is %d, status is %d.",
2201 op_str, queue_id, req->int_vector_id, status);
2209 hns3_init_ring_with_vector(struct hns3_hw *hw)
2216 * In hns3 network engine, vector 0 is always the misc interrupt of this
2217 * function, vector 1~N can be used respectively for the queues of the
2218 * function. Tx and Rx queues with the same number share the interrupt
2219 * vector. In the initialization clearing the all hardware mapping
2220 * relationship configurations between queues and interrupt vectors is
2221 * needed, so some error caused by the residual configurations, such as
2222 * the unexpected Tx interrupt, can be avoid. Because of the hardware
2223 * constraints in hns3 hardware engine, we have to implement clearing
2224 * the mapping relationship configurations by binding all queues to the
2225 * last interrupt vector and reserving the last interrupt vector. This
2226 * method results in a decrease of the maximum queues when upper
2227 * applications call the rte_eth_dev_configure API function to enable
2230 vec = hw->num_msi - 1; /* vector 0 for misc interrupt, not for queue */
2231 /* vec - 1: the last interrupt is reserved */
2232 hw->intr_tqps_num = vec > hw->tqps_num ? hw->tqps_num : vec - 1;
2233 for (i = 0; i < hw->intr_tqps_num; i++) {
2235 * Set gap limiter and rate limiter configuration of queue's
2238 hns3_set_queue_intr_gl(hw, i, HNS3_RING_GL_RX,
2239 HNS3_TQP_INTR_GL_DEFAULT);
2240 hns3_set_queue_intr_gl(hw, i, HNS3_RING_GL_TX,
2241 HNS3_TQP_INTR_GL_DEFAULT);
2242 hns3_set_queue_intr_rl(hw, i, HNS3_TQP_INTR_RL_DEFAULT);
2244 ret = hns3_bind_ring_with_vector(hw, vec, false,
2245 HNS3_RING_TYPE_TX, i);
2247 PMD_INIT_LOG(ERR, "PF fail to unbind TX ring(%d) with "
2248 "vector: %d, ret=%d", i, vec, ret);
2252 ret = hns3_bind_ring_with_vector(hw, vec, false,
2253 HNS3_RING_TYPE_RX, i);
2255 PMD_INIT_LOG(ERR, "PF fail to unbind RX ring(%d) with "
2256 "vector: %d, ret=%d", i, vec, ret);
2265 hns3_dev_configure(struct rte_eth_dev *dev)
2267 struct hns3_adapter *hns = dev->data->dev_private;
2268 struct rte_eth_conf *conf = &dev->data->dev_conf;
2269 enum rte_eth_rx_mq_mode mq_mode = conf->rxmode.mq_mode;
2270 struct hns3_hw *hw = &hns->hw;
2271 struct hns3_rss_conf *rss_cfg = &hw->rss_info;
2272 uint16_t nb_rx_q = dev->data->nb_rx_queues;
2273 uint16_t nb_tx_q = dev->data->nb_tx_queues;
2274 struct rte_eth_rss_conf rss_conf;
2280 * Hardware does not support individually enable/disable/reset the Tx or
2281 * Rx queue in hns3 network engine. Driver must enable/disable/reset Tx
2282 * and Rx queues at the same time. When the numbers of Tx queues
2283 * allocated by upper applications are not equal to the numbers of Rx
2284 * queues, driver needs to setup fake Tx or Rx queues to adjust numbers
2285 * of Tx/Rx queues. otherwise, network engine can not work as usual. But
2286 * these fake queues are imperceptible, and can not be used by upper
2289 ret = hns3_set_fake_rx_or_tx_queues(dev, nb_rx_q, nb_tx_q);
2291 hns3_err(hw, "Failed to set rx/tx fake queues: %d", ret);
2295 hw->adapter_state = HNS3_NIC_CONFIGURING;
2296 if (conf->link_speeds & ETH_LINK_SPEED_FIXED) {
2297 hns3_err(hw, "setting link speed/duplex not supported");
2302 if ((uint32_t)mq_mode & ETH_MQ_RX_DCB_FLAG) {
2303 ret = hns3_check_dcb_cfg(dev);
2308 /* When RSS is not configured, redirect the packet queue 0 */
2309 if ((uint32_t)mq_mode & ETH_MQ_RX_RSS_FLAG) {
2310 conf->rxmode.offloads |= DEV_RX_OFFLOAD_RSS_HASH;
2311 rss_conf = conf->rx_adv_conf.rss_conf;
2312 if (rss_conf.rss_key == NULL) {
2313 rss_conf.rss_key = rss_cfg->key;
2314 rss_conf.rss_key_len = HNS3_RSS_KEY_SIZE;
2317 ret = hns3_dev_rss_hash_update(dev, &rss_conf);
2323 * If jumbo frames are enabled, MTU needs to be refreshed
2324 * according to the maximum RX packet length.
2326 if (conf->rxmode.offloads & DEV_RX_OFFLOAD_JUMBO_FRAME) {
2328 * Security of max_rx_pkt_len is guaranteed in dpdk frame.
2329 * Maximum value of max_rx_pkt_len is HNS3_MAX_FRAME_LEN, so it
2330 * can safely assign to "uint16_t" type variable.
2332 mtu = (uint16_t)HNS3_PKTLEN_TO_MTU(conf->rxmode.max_rx_pkt_len);
2333 ret = hns3_dev_mtu_set(dev, mtu);
2336 dev->data->mtu = mtu;
2339 ret = hns3_dev_configure_vlan(dev);
2343 /* config hardware GRO */
2344 gro_en = conf->rxmode.offloads & DEV_RX_OFFLOAD_TCP_LRO ? true : false;
2345 ret = hns3_config_gro(hw, gro_en);
2349 hw->adapter_state = HNS3_NIC_CONFIGURED;
2354 (void)hns3_set_fake_rx_or_tx_queues(dev, 0, 0);
2355 hw->adapter_state = HNS3_NIC_INITIALIZED;
2361 hns3_set_mac_mtu(struct hns3_hw *hw, uint16_t new_mps)
2363 struct hns3_config_max_frm_size_cmd *req;
2364 struct hns3_cmd_desc desc;
2366 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_CONFIG_MAX_FRM_SIZE, false);
2368 req = (struct hns3_config_max_frm_size_cmd *)desc.data;
2369 req->max_frm_size = rte_cpu_to_le_16(new_mps);
2370 req->min_frm_size = RTE_ETHER_MIN_LEN;
2372 return hns3_cmd_send(hw, &desc, 1);
2376 hns3_config_mtu(struct hns3_hw *hw, uint16_t mps)
2380 ret = hns3_set_mac_mtu(hw, mps);
2382 hns3_err(hw, "Failed to set mtu, ret = %d", ret);
2386 ret = hns3_buffer_alloc(hw);
2388 hns3_err(hw, "Failed to allocate buffer, ret = %d", ret);
2394 hns3_dev_mtu_set(struct rte_eth_dev *dev, uint16_t mtu)
2396 struct hns3_adapter *hns = dev->data->dev_private;
2397 uint32_t frame_size = mtu + HNS3_ETH_OVERHEAD;
2398 struct hns3_hw *hw = &hns->hw;
2399 bool is_jumbo_frame;
2402 if (dev->data->dev_started) {
2403 hns3_err(hw, "Failed to set mtu, port %u must be stopped "
2404 "before configuration", dev->data->port_id);
2408 rte_spinlock_lock(&hw->lock);
2409 is_jumbo_frame = frame_size > RTE_ETHER_MAX_LEN ? true : false;
2410 frame_size = RTE_MAX(frame_size, HNS3_DEFAULT_FRAME_LEN);
2413 * Maximum value of frame_size is HNS3_MAX_FRAME_LEN, so it can safely
2414 * assign to "uint16_t" type variable.
2416 ret = hns3_config_mtu(hw, (uint16_t)frame_size);
2418 rte_spinlock_unlock(&hw->lock);
2419 hns3_err(hw, "Failed to set mtu, port %u mtu %u: %d",
2420 dev->data->port_id, mtu, ret);
2423 hns->pf.mps = (uint16_t)frame_size;
2425 dev->data->dev_conf.rxmode.offloads |=
2426 DEV_RX_OFFLOAD_JUMBO_FRAME;
2428 dev->data->dev_conf.rxmode.offloads &=
2429 ~DEV_RX_OFFLOAD_JUMBO_FRAME;
2430 dev->data->dev_conf.rxmode.max_rx_pkt_len = frame_size;
2431 rte_spinlock_unlock(&hw->lock);
2437 hns3_dev_infos_get(struct rte_eth_dev *eth_dev, struct rte_eth_dev_info *info)
2439 struct hns3_adapter *hns = eth_dev->data->dev_private;
2440 struct hns3_hw *hw = &hns->hw;
2441 uint16_t queue_num = hw->tqps_num;
2444 * In interrupt mode, 'max_rx_queues' is set based on the number of
2445 * MSI-X interrupt resources of the hardware.
2447 if (hw->data->dev_conf.intr_conf.rxq == 1)
2448 queue_num = hw->intr_tqps_num;
2450 info->max_rx_queues = queue_num;
2451 info->max_tx_queues = hw->tqps_num;
2452 info->max_rx_pktlen = HNS3_MAX_FRAME_LEN; /* CRC included */
2453 info->min_rx_bufsize = hw->rx_buf_len;
2454 info->max_mac_addrs = HNS3_UC_MACADDR_NUM;
2455 info->max_mtu = info->max_rx_pktlen - HNS3_ETH_OVERHEAD;
2456 info->max_lro_pkt_size = HNS3_MAX_LRO_SIZE;
2457 info->rx_offload_capa = (DEV_RX_OFFLOAD_IPV4_CKSUM |
2458 DEV_RX_OFFLOAD_TCP_CKSUM |
2459 DEV_RX_OFFLOAD_UDP_CKSUM |
2460 DEV_RX_OFFLOAD_SCTP_CKSUM |
2461 DEV_RX_OFFLOAD_OUTER_IPV4_CKSUM |
2462 DEV_RX_OFFLOAD_OUTER_UDP_CKSUM |
2463 DEV_RX_OFFLOAD_KEEP_CRC |
2464 DEV_RX_OFFLOAD_SCATTER |
2465 DEV_RX_OFFLOAD_VLAN_STRIP |
2466 DEV_RX_OFFLOAD_VLAN_FILTER |
2467 DEV_RX_OFFLOAD_JUMBO_FRAME |
2468 DEV_RX_OFFLOAD_RSS_HASH |
2469 DEV_RX_OFFLOAD_TCP_LRO);
2470 info->tx_queue_offload_capa = DEV_TX_OFFLOAD_MBUF_FAST_FREE;
2471 info->tx_offload_capa = (DEV_TX_OFFLOAD_OUTER_IPV4_CKSUM |
2472 DEV_TX_OFFLOAD_IPV4_CKSUM |
2473 DEV_TX_OFFLOAD_TCP_CKSUM |
2474 DEV_TX_OFFLOAD_UDP_CKSUM |
2475 DEV_TX_OFFLOAD_SCTP_CKSUM |
2476 DEV_TX_OFFLOAD_MULTI_SEGS |
2477 DEV_TX_OFFLOAD_TCP_TSO |
2478 DEV_TX_OFFLOAD_VXLAN_TNL_TSO |
2479 DEV_TX_OFFLOAD_GRE_TNL_TSO |
2480 DEV_TX_OFFLOAD_GENEVE_TNL_TSO |
2481 info->tx_queue_offload_capa |
2482 hns3_txvlan_cap_get(hw));
2484 info->rx_desc_lim = (struct rte_eth_desc_lim) {
2485 .nb_max = HNS3_MAX_RING_DESC,
2486 .nb_min = HNS3_MIN_RING_DESC,
2487 .nb_align = HNS3_ALIGN_RING_DESC,
2490 info->tx_desc_lim = (struct rte_eth_desc_lim) {
2491 .nb_max = HNS3_MAX_RING_DESC,
2492 .nb_min = HNS3_MIN_RING_DESC,
2493 .nb_align = HNS3_ALIGN_RING_DESC,
2494 .nb_seg_max = HNS3_MAX_TSO_BD_PER_PKT,
2495 .nb_mtu_seg_max = HNS3_MAX_NON_TSO_BD_PER_PKT,
2498 info->vmdq_queue_num = 0;
2500 info->reta_size = HNS3_RSS_IND_TBL_SIZE;
2501 info->hash_key_size = HNS3_RSS_KEY_SIZE;
2502 info->flow_type_rss_offloads = HNS3_ETH_RSS_SUPPORT;
2504 info->default_rxportconf.burst_size = HNS3_DEFAULT_PORT_CONF_BURST_SIZE;
2505 info->default_txportconf.burst_size = HNS3_DEFAULT_PORT_CONF_BURST_SIZE;
2506 info->default_rxportconf.nb_queues = HNS3_DEFAULT_PORT_CONF_QUEUES_NUM;
2507 info->default_txportconf.nb_queues = HNS3_DEFAULT_PORT_CONF_QUEUES_NUM;
2508 info->default_rxportconf.ring_size = HNS3_DEFAULT_RING_DESC;
2509 info->default_txportconf.ring_size = HNS3_DEFAULT_RING_DESC;
2515 hns3_fw_version_get(struct rte_eth_dev *eth_dev, char *fw_version,
2518 struct hns3_adapter *hns = eth_dev->data->dev_private;
2519 struct hns3_hw *hw = &hns->hw;
2520 uint32_t version = hw->fw_version;
2523 ret = snprintf(fw_version, fw_size, "%lu.%lu.%lu.%lu",
2524 hns3_get_field(version, HNS3_FW_VERSION_BYTE3_M,
2525 HNS3_FW_VERSION_BYTE3_S),
2526 hns3_get_field(version, HNS3_FW_VERSION_BYTE2_M,
2527 HNS3_FW_VERSION_BYTE2_S),
2528 hns3_get_field(version, HNS3_FW_VERSION_BYTE1_M,
2529 HNS3_FW_VERSION_BYTE1_S),
2530 hns3_get_field(version, HNS3_FW_VERSION_BYTE0_M,
2531 HNS3_FW_VERSION_BYTE0_S));
2532 ret += 1; /* add the size of '\0' */
2533 if (fw_size < (uint32_t)ret)
2540 hns3_dev_link_update(struct rte_eth_dev *eth_dev,
2541 __rte_unused int wait_to_complete)
2543 struct hns3_adapter *hns = eth_dev->data->dev_private;
2544 struct hns3_hw *hw = &hns->hw;
2545 struct hns3_mac *mac = &hw->mac;
2546 struct rte_eth_link new_link;
2548 if (!hns3_is_reset_pending(hns)) {
2549 hns3_update_speed_duplex(eth_dev);
2550 hns3_update_link_status(hw);
2553 memset(&new_link, 0, sizeof(new_link));
2554 switch (mac->link_speed) {
2555 case ETH_SPEED_NUM_10M:
2556 case ETH_SPEED_NUM_100M:
2557 case ETH_SPEED_NUM_1G:
2558 case ETH_SPEED_NUM_10G:
2559 case ETH_SPEED_NUM_25G:
2560 case ETH_SPEED_NUM_40G:
2561 case ETH_SPEED_NUM_50G:
2562 case ETH_SPEED_NUM_100G:
2563 new_link.link_speed = mac->link_speed;
2566 new_link.link_speed = ETH_SPEED_NUM_100M;
2570 new_link.link_duplex = mac->link_duplex;
2571 new_link.link_status = mac->link_status ? ETH_LINK_UP : ETH_LINK_DOWN;
2572 new_link.link_autoneg =
2573 !(eth_dev->data->dev_conf.link_speeds & ETH_LINK_SPEED_FIXED);
2575 return rte_eth_linkstatus_set(eth_dev, &new_link);
2579 hns3_parse_func_status(struct hns3_hw *hw, struct hns3_func_status_cmd *status)
2581 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
2582 struct hns3_pf *pf = &hns->pf;
2584 if (!(status->pf_state & HNS3_PF_STATE_DONE))
2587 pf->is_main_pf = (status->pf_state & HNS3_PF_STATE_MAIN) ? true : false;
2593 hns3_query_function_status(struct hns3_hw *hw)
2595 #define HNS3_QUERY_MAX_CNT 10
2596 #define HNS3_QUERY_SLEEP_MSCOEND 1
2597 struct hns3_func_status_cmd *req;
2598 struct hns3_cmd_desc desc;
2602 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_QUERY_FUNC_STATUS, true);
2603 req = (struct hns3_func_status_cmd *)desc.data;
2606 ret = hns3_cmd_send(hw, &desc, 1);
2608 PMD_INIT_LOG(ERR, "query function status failed %d",
2613 /* Check pf reset is done */
2617 rte_delay_ms(HNS3_QUERY_SLEEP_MSCOEND);
2618 } while (timeout++ < HNS3_QUERY_MAX_CNT);
2620 return hns3_parse_func_status(hw, req);
2624 hns3_query_pf_resource(struct hns3_hw *hw)
2626 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
2627 struct hns3_pf *pf = &hns->pf;
2628 struct hns3_pf_res_cmd *req;
2629 struct hns3_cmd_desc desc;
2632 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_QUERY_PF_RSRC, true);
2633 ret = hns3_cmd_send(hw, &desc, 1);
2635 PMD_INIT_LOG(ERR, "query pf resource failed %d", ret);
2639 req = (struct hns3_pf_res_cmd *)desc.data;
2640 hw->total_tqps_num = rte_le_to_cpu_16(req->tqp_num);
2641 pf->pkt_buf_size = rte_le_to_cpu_16(req->buf_size) << HNS3_BUF_UNIT_S;
2642 hw->tqps_num = RTE_MIN(hw->total_tqps_num, HNS3_MAX_TQP_NUM_PER_FUNC);
2643 pf->func_num = rte_le_to_cpu_16(req->pf_own_fun_number);
2645 if (req->tx_buf_size)
2647 rte_le_to_cpu_16(req->tx_buf_size) << HNS3_BUF_UNIT_S;
2649 pf->tx_buf_size = HNS3_DEFAULT_TX_BUF;
2651 pf->tx_buf_size = roundup(pf->tx_buf_size, HNS3_BUF_SIZE_UNIT);
2653 if (req->dv_buf_size)
2655 rte_le_to_cpu_16(req->dv_buf_size) << HNS3_BUF_UNIT_S;
2657 pf->dv_buf_size = HNS3_DEFAULT_DV;
2659 pf->dv_buf_size = roundup(pf->dv_buf_size, HNS3_BUF_SIZE_UNIT);
2662 hns3_get_field(rte_le_to_cpu_16(req->pf_intr_vector_number),
2663 HNS3_VEC_NUM_M, HNS3_VEC_NUM_S);
2669 hns3_parse_cfg(struct hns3_cfg *cfg, struct hns3_cmd_desc *desc)
2671 struct hns3_cfg_param_cmd *req;
2672 uint64_t mac_addr_tmp_high;
2673 uint64_t mac_addr_tmp;
2676 req = (struct hns3_cfg_param_cmd *)desc[0].data;
2678 /* get the configuration */
2679 cfg->vmdq_vport_num = hns3_get_field(rte_le_to_cpu_32(req->param[0]),
2680 HNS3_CFG_VMDQ_M, HNS3_CFG_VMDQ_S);
2681 cfg->tc_num = hns3_get_field(rte_le_to_cpu_32(req->param[0]),
2682 HNS3_CFG_TC_NUM_M, HNS3_CFG_TC_NUM_S);
2683 cfg->tqp_desc_num = hns3_get_field(rte_le_to_cpu_32(req->param[0]),
2684 HNS3_CFG_TQP_DESC_N_M,
2685 HNS3_CFG_TQP_DESC_N_S);
2687 cfg->phy_addr = hns3_get_field(rte_le_to_cpu_32(req->param[1]),
2688 HNS3_CFG_PHY_ADDR_M,
2689 HNS3_CFG_PHY_ADDR_S);
2690 cfg->media_type = hns3_get_field(rte_le_to_cpu_32(req->param[1]),
2691 HNS3_CFG_MEDIA_TP_M,
2692 HNS3_CFG_MEDIA_TP_S);
2693 cfg->rx_buf_len = hns3_get_field(rte_le_to_cpu_32(req->param[1]),
2694 HNS3_CFG_RX_BUF_LEN_M,
2695 HNS3_CFG_RX_BUF_LEN_S);
2696 /* get mac address */
2697 mac_addr_tmp = rte_le_to_cpu_32(req->param[2]);
2698 mac_addr_tmp_high = hns3_get_field(rte_le_to_cpu_32(req->param[3]),
2699 HNS3_CFG_MAC_ADDR_H_M,
2700 HNS3_CFG_MAC_ADDR_H_S);
2702 mac_addr_tmp |= (mac_addr_tmp_high << 31) << 1;
2704 cfg->default_speed = hns3_get_field(rte_le_to_cpu_32(req->param[3]),
2705 HNS3_CFG_DEFAULT_SPEED_M,
2706 HNS3_CFG_DEFAULT_SPEED_S);
2707 cfg->rss_size_max = hns3_get_field(rte_le_to_cpu_32(req->param[3]),
2708 HNS3_CFG_RSS_SIZE_M,
2709 HNS3_CFG_RSS_SIZE_S);
2711 for (i = 0; i < RTE_ETHER_ADDR_LEN; i++)
2712 cfg->mac_addr[i] = (mac_addr_tmp >> (8 * i)) & 0xff;
2714 req = (struct hns3_cfg_param_cmd *)desc[1].data;
2715 cfg->numa_node_map = rte_le_to_cpu_32(req->param[0]);
2717 cfg->speed_ability = hns3_get_field(rte_le_to_cpu_32(req->param[1]),
2718 HNS3_CFG_SPEED_ABILITY_M,
2719 HNS3_CFG_SPEED_ABILITY_S);
2720 cfg->umv_space = hns3_get_field(rte_le_to_cpu_32(req->param[1]),
2721 HNS3_CFG_UMV_TBL_SPACE_M,
2722 HNS3_CFG_UMV_TBL_SPACE_S);
2723 if (!cfg->umv_space)
2724 cfg->umv_space = HNS3_DEFAULT_UMV_SPACE_PER_PF;
2727 /* hns3_get_board_cfg: query the static parameter from NCL_config file in flash
2728 * @hw: pointer to struct hns3_hw
2729 * @hcfg: the config structure to be getted
2732 hns3_get_board_cfg(struct hns3_hw *hw, struct hns3_cfg *hcfg)
2734 struct hns3_cmd_desc desc[HNS3_PF_CFG_DESC_NUM];
2735 struct hns3_cfg_param_cmd *req;
2740 for (i = 0; i < HNS3_PF_CFG_DESC_NUM; i++) {
2742 req = (struct hns3_cfg_param_cmd *)desc[i].data;
2743 hns3_cmd_setup_basic_desc(&desc[i], HNS3_OPC_GET_CFG_PARAM,
2745 hns3_set_field(offset, HNS3_CFG_OFFSET_M, HNS3_CFG_OFFSET_S,
2746 i * HNS3_CFG_RD_LEN_BYTES);
2747 /* Len should be divided by 4 when send to hardware */
2748 hns3_set_field(offset, HNS3_CFG_RD_LEN_M, HNS3_CFG_RD_LEN_S,
2749 HNS3_CFG_RD_LEN_BYTES / HNS3_CFG_RD_LEN_UNIT);
2750 req->offset = rte_cpu_to_le_32(offset);
2753 ret = hns3_cmd_send(hw, desc, HNS3_PF_CFG_DESC_NUM);
2755 PMD_INIT_LOG(ERR, "get config failed %d.", ret);
2759 hns3_parse_cfg(hcfg, desc);
2765 hns3_parse_speed(int speed_cmd, uint32_t *speed)
2767 switch (speed_cmd) {
2768 case HNS3_CFG_SPEED_10M:
2769 *speed = ETH_SPEED_NUM_10M;
2771 case HNS3_CFG_SPEED_100M:
2772 *speed = ETH_SPEED_NUM_100M;
2774 case HNS3_CFG_SPEED_1G:
2775 *speed = ETH_SPEED_NUM_1G;
2777 case HNS3_CFG_SPEED_10G:
2778 *speed = ETH_SPEED_NUM_10G;
2780 case HNS3_CFG_SPEED_25G:
2781 *speed = ETH_SPEED_NUM_25G;
2783 case HNS3_CFG_SPEED_40G:
2784 *speed = ETH_SPEED_NUM_40G;
2786 case HNS3_CFG_SPEED_50G:
2787 *speed = ETH_SPEED_NUM_50G;
2789 case HNS3_CFG_SPEED_100G:
2790 *speed = ETH_SPEED_NUM_100G;
2800 hns3_get_capability(struct hns3_hw *hw)
2802 struct rte_pci_device *pci_dev;
2803 struct rte_eth_dev *eth_dev;
2808 eth_dev = &rte_eth_devices[hw->data->port_id];
2809 pci_dev = RTE_ETH_DEV_TO_PCI(eth_dev);
2810 device_id = pci_dev->id.device_id;
2812 if (device_id == HNS3_DEV_ID_25GE_RDMA ||
2813 device_id == HNS3_DEV_ID_50GE_RDMA ||
2814 device_id == HNS3_DEV_ID_100G_RDMA_MACSEC)
2815 hns3_set_bit(hw->capability, HNS3_DEV_SUPPORT_DCB_B, 1);
2817 /* Get PCI revision id */
2818 ret = rte_pci_read_config(pci_dev, &revision, HNS3_PCI_REVISION_ID_LEN,
2819 HNS3_PCI_REVISION_ID);
2820 if (ret != HNS3_PCI_REVISION_ID_LEN) {
2821 PMD_INIT_LOG(ERR, "failed to read pci revision id: %d", ret);
2824 hw->revision = revision;
2830 hns3_get_board_configuration(struct hns3_hw *hw)
2832 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
2833 struct hns3_pf *pf = &hns->pf;
2834 struct hns3_cfg cfg;
2837 ret = hns3_get_board_cfg(hw, &cfg);
2839 PMD_INIT_LOG(ERR, "get board config failed %d", ret);
2843 if (cfg.media_type == HNS3_MEDIA_TYPE_COPPER) {
2844 PMD_INIT_LOG(ERR, "media type is copper, not supported.");
2848 hw->mac.media_type = cfg.media_type;
2849 hw->rss_size_max = cfg.rss_size_max;
2850 hw->rss_dis_flag = false;
2851 hw->rx_buf_len = cfg.rx_buf_len;
2852 memcpy(hw->mac.mac_addr, cfg.mac_addr, RTE_ETHER_ADDR_LEN);
2853 hw->mac.phy_addr = cfg.phy_addr;
2854 hw->mac.default_addr_setted = false;
2855 hw->num_tx_desc = cfg.tqp_desc_num;
2856 hw->num_rx_desc = cfg.tqp_desc_num;
2857 hw->dcb_info.num_pg = 1;
2858 hw->dcb_info.hw_pfc_map = 0;
2860 ret = hns3_parse_speed(cfg.default_speed, &hw->mac.link_speed);
2862 PMD_INIT_LOG(ERR, "Get wrong speed %d, ret = %d",
2863 cfg.default_speed, ret);
2867 pf->tc_max = cfg.tc_num;
2868 if (pf->tc_max > HNS3_MAX_TC_NUM || pf->tc_max < 1) {
2869 PMD_INIT_LOG(WARNING,
2870 "Get TC num(%u) from flash, set TC num to 1",
2875 /* Dev does not support DCB */
2876 if (!hns3_dev_dcb_supported(hw)) {
2880 pf->pfc_max = pf->tc_max;
2882 hw->dcb_info.num_tc = 1;
2883 hw->alloc_rss_size = RTE_MIN(hw->rss_size_max,
2884 hw->tqps_num / hw->dcb_info.num_tc);
2885 hns3_set_bit(hw->hw_tc_map, 0, 1);
2886 pf->tx_sch_mode = HNS3_FLAG_TC_BASE_SCH_MODE;
2888 pf->wanted_umv_size = cfg.umv_space;
2894 hns3_get_configuration(struct hns3_hw *hw)
2898 ret = hns3_query_function_status(hw);
2900 PMD_INIT_LOG(ERR, "Failed to query function status: %d.", ret);
2904 /* Get device capability */
2905 ret = hns3_get_capability(hw);
2907 PMD_INIT_LOG(ERR, "failed to get device capability: %d.", ret);
2911 /* Get pf resource */
2912 ret = hns3_query_pf_resource(hw);
2914 PMD_INIT_LOG(ERR, "Failed to query pf resource: %d", ret);
2918 ret = hns3_get_board_configuration(hw);
2920 PMD_INIT_LOG(ERR, "Failed to get board configuration: %d", ret);
2926 hns3_map_tqps_to_func(struct hns3_hw *hw, uint16_t func_id, uint16_t tqp_pid,
2927 uint16_t tqp_vid, bool is_pf)
2929 struct hns3_tqp_map_cmd *req;
2930 struct hns3_cmd_desc desc;
2933 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_SET_TQP_MAP, false);
2935 req = (struct hns3_tqp_map_cmd *)desc.data;
2936 req->tqp_id = rte_cpu_to_le_16(tqp_pid);
2937 req->tqp_vf = func_id;
2938 req->tqp_flag = 1 << HNS3_TQP_MAP_EN_B;
2940 req->tqp_flag |= (1 << HNS3_TQP_MAP_TYPE_B);
2941 req->tqp_vid = rte_cpu_to_le_16(tqp_vid);
2943 ret = hns3_cmd_send(hw, &desc, 1);
2945 PMD_INIT_LOG(ERR, "TQP map failed %d", ret);
2951 hns3_map_tqp(struct hns3_hw *hw)
2953 uint16_t tqps_num = hw->total_tqps_num;
2962 * In current version VF is not supported when PF is driven by DPDK
2963 * driver, so we allocate tqps to PF as much as possible.
2966 num = DIV_ROUND_UP(hw->total_tqps_num, HNS3_MAX_TQP_NUM_PER_FUNC);
2967 for (func_id = HNS3_PF_FUNC_ID; func_id < num; func_id++) {
2968 is_pf = func_id == HNS3_PF_FUNC_ID ? true : false;
2970 i < HNS3_MAX_TQP_NUM_PER_FUNC && tqp_id < tqps_num; i++) {
2971 ret = hns3_map_tqps_to_func(hw, func_id, tqp_id++, i,
2982 hns3_cfg_mac_speed_dup_hw(struct hns3_hw *hw, uint32_t speed, uint8_t duplex)
2984 struct hns3_config_mac_speed_dup_cmd *req;
2985 struct hns3_cmd_desc desc;
2988 req = (struct hns3_config_mac_speed_dup_cmd *)desc.data;
2990 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_CONFIG_SPEED_DUP, false);
2992 hns3_set_bit(req->speed_dup, HNS3_CFG_DUPLEX_B, !!duplex ? 1 : 0);
2995 case ETH_SPEED_NUM_10M:
2996 hns3_set_field(req->speed_dup, HNS3_CFG_SPEED_M,
2997 HNS3_CFG_SPEED_S, HNS3_CFG_SPEED_10M);
2999 case ETH_SPEED_NUM_100M:
3000 hns3_set_field(req->speed_dup, HNS3_CFG_SPEED_M,
3001 HNS3_CFG_SPEED_S, HNS3_CFG_SPEED_100M);
3003 case ETH_SPEED_NUM_1G:
3004 hns3_set_field(req->speed_dup, HNS3_CFG_SPEED_M,
3005 HNS3_CFG_SPEED_S, HNS3_CFG_SPEED_1G);
3007 case ETH_SPEED_NUM_10G:
3008 hns3_set_field(req->speed_dup, HNS3_CFG_SPEED_M,
3009 HNS3_CFG_SPEED_S, HNS3_CFG_SPEED_10G);
3011 case ETH_SPEED_NUM_25G:
3012 hns3_set_field(req->speed_dup, HNS3_CFG_SPEED_M,
3013 HNS3_CFG_SPEED_S, HNS3_CFG_SPEED_25G);
3015 case ETH_SPEED_NUM_40G:
3016 hns3_set_field(req->speed_dup, HNS3_CFG_SPEED_M,
3017 HNS3_CFG_SPEED_S, HNS3_CFG_SPEED_40G);
3019 case ETH_SPEED_NUM_50G:
3020 hns3_set_field(req->speed_dup, HNS3_CFG_SPEED_M,
3021 HNS3_CFG_SPEED_S, HNS3_CFG_SPEED_50G);
3023 case ETH_SPEED_NUM_100G:
3024 hns3_set_field(req->speed_dup, HNS3_CFG_SPEED_M,
3025 HNS3_CFG_SPEED_S, HNS3_CFG_SPEED_100G);
3028 PMD_INIT_LOG(ERR, "invalid speed (%u)", speed);
3032 hns3_set_bit(req->mac_change_fec_en, HNS3_CFG_MAC_SPEED_CHANGE_EN_B, 1);
3034 ret = hns3_cmd_send(hw, &desc, 1);
3036 PMD_INIT_LOG(ERR, "mac speed/duplex config cmd failed %d", ret);
3042 hns3_tx_buffer_calc(struct hns3_hw *hw, struct hns3_pkt_buf_alloc *buf_alloc)
3044 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
3045 struct hns3_pf *pf = &hns->pf;
3046 struct hns3_priv_buf *priv;
3047 uint32_t i, total_size;
3049 total_size = pf->pkt_buf_size;
3051 /* alloc tx buffer for all enabled tc */
3052 for (i = 0; i < HNS3_MAX_TC_NUM; i++) {
3053 priv = &buf_alloc->priv_buf[i];
3055 if (hw->hw_tc_map & BIT(i)) {
3056 if (total_size < pf->tx_buf_size)
3059 priv->tx_buf_size = pf->tx_buf_size;
3061 priv->tx_buf_size = 0;
3063 total_size -= priv->tx_buf_size;
3070 hns3_tx_buffer_alloc(struct hns3_hw *hw, struct hns3_pkt_buf_alloc *buf_alloc)
3072 /* TX buffer size is unit by 128 byte */
3073 #define HNS3_BUF_SIZE_UNIT_SHIFT 7
3074 #define HNS3_BUF_SIZE_UPDATE_EN_MSK BIT(15)
3075 struct hns3_tx_buff_alloc_cmd *req;
3076 struct hns3_cmd_desc desc;
3081 req = (struct hns3_tx_buff_alloc_cmd *)desc.data;
3083 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_TX_BUFF_ALLOC, 0);
3084 for (i = 0; i < HNS3_MAX_TC_NUM; i++) {
3085 buf_size = buf_alloc->priv_buf[i].tx_buf_size;
3087 buf_size = buf_size >> HNS3_BUF_SIZE_UNIT_SHIFT;
3088 req->tx_pkt_buff[i] = rte_cpu_to_le_16(buf_size |
3089 HNS3_BUF_SIZE_UPDATE_EN_MSK);
3092 ret = hns3_cmd_send(hw, &desc, 1);
3094 PMD_INIT_LOG(ERR, "tx buffer alloc cmd failed %d", ret);
3100 hns3_get_tc_num(struct hns3_hw *hw)
3105 for (i = 0; i < HNS3_MAX_TC_NUM; i++)
3106 if (hw->hw_tc_map & BIT(i))
3112 hns3_get_rx_priv_buff_alloced(struct hns3_pkt_buf_alloc *buf_alloc)
3114 struct hns3_priv_buf *priv;
3115 uint32_t rx_priv = 0;
3118 for (i = 0; i < HNS3_MAX_TC_NUM; i++) {
3119 priv = &buf_alloc->priv_buf[i];
3121 rx_priv += priv->buf_size;
3127 hns3_get_tx_buff_alloced(struct hns3_pkt_buf_alloc *buf_alloc)
3129 uint32_t total_tx_size = 0;
3132 for (i = 0; i < HNS3_MAX_TC_NUM; i++)
3133 total_tx_size += buf_alloc->priv_buf[i].tx_buf_size;
3135 return total_tx_size;
3138 /* Get the number of pfc enabled TCs, which have private buffer */
3140 hns3_get_pfc_priv_num(struct hns3_hw *hw, struct hns3_pkt_buf_alloc *buf_alloc)
3142 struct hns3_priv_buf *priv;
3146 for (i = 0; i < HNS3_MAX_TC_NUM; i++) {
3147 priv = &buf_alloc->priv_buf[i];
3148 if ((hw->dcb_info.hw_pfc_map & BIT(i)) && priv->enable)
3155 /* Get the number of pfc disabled TCs, which have private buffer */
3157 hns3_get_no_pfc_priv_num(struct hns3_hw *hw,
3158 struct hns3_pkt_buf_alloc *buf_alloc)
3160 struct hns3_priv_buf *priv;
3164 for (i = 0; i < HNS3_MAX_TC_NUM; i++) {
3165 priv = &buf_alloc->priv_buf[i];
3166 if (hw->hw_tc_map & BIT(i) &&
3167 !(hw->dcb_info.hw_pfc_map & BIT(i)) && priv->enable)
3175 hns3_is_rx_buf_ok(struct hns3_hw *hw, struct hns3_pkt_buf_alloc *buf_alloc,
3178 uint32_t shared_buf_min, shared_buf_tc, shared_std, hi_thrd, lo_thrd;
3179 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
3180 struct hns3_pf *pf = &hns->pf;
3181 uint32_t shared_buf, aligned_mps;
3186 tc_num = hns3_get_tc_num(hw);
3187 aligned_mps = roundup(pf->mps, HNS3_BUF_SIZE_UNIT);
3189 if (hns3_dev_dcb_supported(hw))
3190 shared_buf_min = HNS3_BUF_MUL_BY * aligned_mps +
3193 shared_buf_min = aligned_mps + HNS3_NON_DCB_ADDITIONAL_BUF
3196 shared_buf_tc = tc_num * aligned_mps + aligned_mps;
3197 shared_std = roundup(max_t(uint32_t, shared_buf_min, shared_buf_tc),
3198 HNS3_BUF_SIZE_UNIT);
3200 rx_priv = hns3_get_rx_priv_buff_alloced(buf_alloc);
3201 if (rx_all < rx_priv + shared_std)
3204 shared_buf = rounddown(rx_all - rx_priv, HNS3_BUF_SIZE_UNIT);
3205 buf_alloc->s_buf.buf_size = shared_buf;
3206 if (hns3_dev_dcb_supported(hw)) {
3207 buf_alloc->s_buf.self.high = shared_buf - pf->dv_buf_size;
3208 buf_alloc->s_buf.self.low = buf_alloc->s_buf.self.high
3209 - roundup(aligned_mps / HNS3_BUF_DIV_BY,
3210 HNS3_BUF_SIZE_UNIT);
3212 buf_alloc->s_buf.self.high =
3213 aligned_mps + HNS3_NON_DCB_ADDITIONAL_BUF;
3214 buf_alloc->s_buf.self.low = aligned_mps;
3217 if (hns3_dev_dcb_supported(hw)) {
3218 hi_thrd = shared_buf - pf->dv_buf_size;
3220 if (tc_num <= NEED_RESERVE_TC_NUM)
3221 hi_thrd = hi_thrd * BUF_RESERVE_PERCENT
3225 hi_thrd = hi_thrd / tc_num;
3227 hi_thrd = max_t(uint32_t, hi_thrd,
3228 HNS3_BUF_MUL_BY * aligned_mps);
3229 hi_thrd = rounddown(hi_thrd, HNS3_BUF_SIZE_UNIT);
3230 lo_thrd = hi_thrd - aligned_mps / HNS3_BUF_DIV_BY;
3232 hi_thrd = aligned_mps + HNS3_NON_DCB_ADDITIONAL_BUF;
3233 lo_thrd = aligned_mps;
3236 for (i = 0; i < HNS3_MAX_TC_NUM; i++) {
3237 buf_alloc->s_buf.tc_thrd[i].low = lo_thrd;
3238 buf_alloc->s_buf.tc_thrd[i].high = hi_thrd;
3245 hns3_rx_buf_calc_all(struct hns3_hw *hw, bool max,
3246 struct hns3_pkt_buf_alloc *buf_alloc)
3248 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
3249 struct hns3_pf *pf = &hns->pf;
3250 struct hns3_priv_buf *priv;
3251 uint32_t aligned_mps;
3255 rx_all = pf->pkt_buf_size - hns3_get_tx_buff_alloced(buf_alloc);
3256 aligned_mps = roundup(pf->mps, HNS3_BUF_SIZE_UNIT);
3258 for (i = 0; i < HNS3_MAX_TC_NUM; i++) {
3259 priv = &buf_alloc->priv_buf[i];
3266 if (!(hw->hw_tc_map & BIT(i)))
3270 if (hw->dcb_info.hw_pfc_map & BIT(i)) {
3271 priv->wl.low = max ? aligned_mps : HNS3_BUF_SIZE_UNIT;
3272 priv->wl.high = roundup(priv->wl.low + aligned_mps,
3273 HNS3_BUF_SIZE_UNIT);
3276 priv->wl.high = max ? (aligned_mps * HNS3_BUF_MUL_BY) :
3280 priv->buf_size = priv->wl.high + pf->dv_buf_size;
3283 return hns3_is_rx_buf_ok(hw, buf_alloc, rx_all);
3287 hns3_drop_nopfc_buf_till_fit(struct hns3_hw *hw,
3288 struct hns3_pkt_buf_alloc *buf_alloc)
3290 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
3291 struct hns3_pf *pf = &hns->pf;
3292 struct hns3_priv_buf *priv;
3293 int no_pfc_priv_num;
3298 rx_all = pf->pkt_buf_size - hns3_get_tx_buff_alloced(buf_alloc);
3299 no_pfc_priv_num = hns3_get_no_pfc_priv_num(hw, buf_alloc);
3301 /* let the last to be cleared first */
3302 for (i = HNS3_MAX_TC_NUM - 1; i >= 0; i--) {
3303 priv = &buf_alloc->priv_buf[i];
3304 mask = BIT((uint8_t)i);
3306 if (hw->hw_tc_map & mask &&
3307 !(hw->dcb_info.hw_pfc_map & mask)) {
3308 /* Clear the no pfc TC private buffer */
3316 if (hns3_is_rx_buf_ok(hw, buf_alloc, rx_all) ||
3317 no_pfc_priv_num == 0)
3321 return hns3_is_rx_buf_ok(hw, buf_alloc, rx_all);
3325 hns3_drop_pfc_buf_till_fit(struct hns3_hw *hw,
3326 struct hns3_pkt_buf_alloc *buf_alloc)
3328 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
3329 struct hns3_pf *pf = &hns->pf;
3330 struct hns3_priv_buf *priv;
3336 rx_all = pf->pkt_buf_size - hns3_get_tx_buff_alloced(buf_alloc);
3337 pfc_priv_num = hns3_get_pfc_priv_num(hw, buf_alloc);
3339 /* let the last to be cleared first */
3340 for (i = HNS3_MAX_TC_NUM - 1; i >= 0; i--) {
3341 priv = &buf_alloc->priv_buf[i];
3342 mask = BIT((uint8_t)i);
3344 if (hw->hw_tc_map & mask &&
3345 hw->dcb_info.hw_pfc_map & mask) {
3346 /* Reduce the number of pfc TC with private buffer */
3353 if (hns3_is_rx_buf_ok(hw, buf_alloc, rx_all) ||
3358 return hns3_is_rx_buf_ok(hw, buf_alloc, rx_all);
3362 hns3_only_alloc_priv_buff(struct hns3_hw *hw,
3363 struct hns3_pkt_buf_alloc *buf_alloc)
3365 #define COMPENSATE_BUFFER 0x3C00
3366 #define COMPENSATE_HALF_MPS_NUM 5
3367 #define PRIV_WL_GAP 0x1800
3368 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
3369 struct hns3_pf *pf = &hns->pf;
3370 uint32_t tc_num = hns3_get_tc_num(hw);
3371 uint32_t half_mps = pf->mps >> 1;
3372 struct hns3_priv_buf *priv;
3373 uint32_t min_rx_priv;
3377 rx_priv = pf->pkt_buf_size - hns3_get_tx_buff_alloced(buf_alloc);
3379 rx_priv = rx_priv / tc_num;
3381 if (tc_num <= NEED_RESERVE_TC_NUM)
3382 rx_priv = rx_priv * BUF_RESERVE_PERCENT / BUF_MAX_PERCENT;
3385 * Minimum value of private buffer in rx direction (min_rx_priv) is
3386 * equal to "DV + 2.5 * MPS + 15KB". Driver only allocates rx private
3387 * buffer if rx_priv is greater than min_rx_priv.
3389 min_rx_priv = pf->dv_buf_size + COMPENSATE_BUFFER +
3390 COMPENSATE_HALF_MPS_NUM * half_mps;
3391 min_rx_priv = roundup(min_rx_priv, HNS3_BUF_SIZE_UNIT);
3392 rx_priv = rounddown(rx_priv, HNS3_BUF_SIZE_UNIT);
3394 if (rx_priv < min_rx_priv)
3397 for (i = 0; i < HNS3_MAX_TC_NUM; i++) {
3398 priv = &buf_alloc->priv_buf[i];
3405 if (!(hw->hw_tc_map & BIT(i)))
3409 priv->buf_size = rx_priv;
3410 priv->wl.high = rx_priv - pf->dv_buf_size;
3411 priv->wl.low = priv->wl.high - PRIV_WL_GAP;
3414 buf_alloc->s_buf.buf_size = 0;
3420 * hns3_rx_buffer_calc: calculate the rx private buffer size for all TCs
3421 * @hw: pointer to struct hns3_hw
3422 * @buf_alloc: pointer to buffer calculation data
3423 * @return: 0: calculate sucessful, negative: fail
3426 hns3_rx_buffer_calc(struct hns3_hw *hw, struct hns3_pkt_buf_alloc *buf_alloc)
3428 /* When DCB is not supported, rx private buffer is not allocated. */
3429 if (!hns3_dev_dcb_supported(hw)) {
3430 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
3431 struct hns3_pf *pf = &hns->pf;
3432 uint32_t rx_all = pf->pkt_buf_size;
3434 rx_all -= hns3_get_tx_buff_alloced(buf_alloc);
3435 if (!hns3_is_rx_buf_ok(hw, buf_alloc, rx_all))
3442 * Try to allocate privated packet buffer for all TCs without share
3445 if (hns3_only_alloc_priv_buff(hw, buf_alloc))
3449 * Try to allocate privated packet buffer for all TCs with share
3452 if (hns3_rx_buf_calc_all(hw, true, buf_alloc))
3456 * For different application scenes, the enabled port number, TC number
3457 * and no_drop TC number are different. In order to obtain the better
3458 * performance, software could allocate the buffer size and configure
3459 * the waterline by tring to decrease the private buffer size according
3460 * to the order, namely, waterline of valided tc, pfc disabled tc, pfc
3463 if (hns3_rx_buf_calc_all(hw, false, buf_alloc))
3466 if (hns3_drop_nopfc_buf_till_fit(hw, buf_alloc))
3469 if (hns3_drop_pfc_buf_till_fit(hw, buf_alloc))
3476 hns3_rx_priv_buf_alloc(struct hns3_hw *hw, struct hns3_pkt_buf_alloc *buf_alloc)
3478 struct hns3_rx_priv_buff_cmd *req;
3479 struct hns3_cmd_desc desc;
3484 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_RX_PRIV_BUFF_ALLOC, false);
3485 req = (struct hns3_rx_priv_buff_cmd *)desc.data;
3487 /* Alloc private buffer TCs */
3488 for (i = 0; i < HNS3_MAX_TC_NUM; i++) {
3489 struct hns3_priv_buf *priv = &buf_alloc->priv_buf[i];
3492 rte_cpu_to_le_16(priv->buf_size >> HNS3_BUF_UNIT_S);
3493 req->buf_num[i] |= rte_cpu_to_le_16(1 << HNS3_TC0_PRI_BUF_EN_B);
3496 buf_size = buf_alloc->s_buf.buf_size;
3497 req->shared_buf = rte_cpu_to_le_16((buf_size >> HNS3_BUF_UNIT_S) |
3498 (1 << HNS3_TC0_PRI_BUF_EN_B));
3500 ret = hns3_cmd_send(hw, &desc, 1);
3502 PMD_INIT_LOG(ERR, "rx private buffer alloc cmd failed %d", ret);
3508 hns3_rx_priv_wl_config(struct hns3_hw *hw, struct hns3_pkt_buf_alloc *buf_alloc)
3510 #define HNS3_RX_PRIV_WL_ALLOC_DESC_NUM 2
3511 struct hns3_rx_priv_wl_buf *req;
3512 struct hns3_priv_buf *priv;
3513 struct hns3_cmd_desc desc[HNS3_RX_PRIV_WL_ALLOC_DESC_NUM];
3517 for (i = 0; i < HNS3_RX_PRIV_WL_ALLOC_DESC_NUM; i++) {
3518 hns3_cmd_setup_basic_desc(&desc[i], HNS3_OPC_RX_PRIV_WL_ALLOC,
3520 req = (struct hns3_rx_priv_wl_buf *)desc[i].data;
3522 /* The first descriptor set the NEXT bit to 1 */
3524 desc[i].flag |= rte_cpu_to_le_16(HNS3_CMD_FLAG_NEXT);
3526 desc[i].flag &= ~rte_cpu_to_le_16(HNS3_CMD_FLAG_NEXT);
3528 for (j = 0; j < HNS3_TC_NUM_ONE_DESC; j++) {
3529 uint32_t idx = i * HNS3_TC_NUM_ONE_DESC + j;
3531 priv = &buf_alloc->priv_buf[idx];
3532 req->tc_wl[j].high = rte_cpu_to_le_16(priv->wl.high >>
3534 req->tc_wl[j].high |=
3535 rte_cpu_to_le_16(BIT(HNS3_RX_PRIV_EN_B));
3536 req->tc_wl[j].low = rte_cpu_to_le_16(priv->wl.low >>
3538 req->tc_wl[j].low |=
3539 rte_cpu_to_le_16(BIT(HNS3_RX_PRIV_EN_B));
3543 /* Send 2 descriptor at one time */
3544 ret = hns3_cmd_send(hw, desc, HNS3_RX_PRIV_WL_ALLOC_DESC_NUM);
3546 PMD_INIT_LOG(ERR, "rx private waterline config cmd failed %d",
3552 hns3_common_thrd_config(struct hns3_hw *hw,
3553 struct hns3_pkt_buf_alloc *buf_alloc)
3555 #define HNS3_RX_COM_THRD_ALLOC_DESC_NUM 2
3556 struct hns3_shared_buf *s_buf = &buf_alloc->s_buf;
3557 struct hns3_rx_com_thrd *req;
3558 struct hns3_cmd_desc desc[HNS3_RX_COM_THRD_ALLOC_DESC_NUM];
3559 struct hns3_tc_thrd *tc;
3564 for (i = 0; i < HNS3_RX_COM_THRD_ALLOC_DESC_NUM; i++) {
3565 hns3_cmd_setup_basic_desc(&desc[i], HNS3_OPC_RX_COM_THRD_ALLOC,
3567 req = (struct hns3_rx_com_thrd *)&desc[i].data;
3569 /* The first descriptor set the NEXT bit to 1 */
3571 desc[i].flag |= rte_cpu_to_le_16(HNS3_CMD_FLAG_NEXT);
3573 desc[i].flag &= ~rte_cpu_to_le_16(HNS3_CMD_FLAG_NEXT);
3575 for (j = 0; j < HNS3_TC_NUM_ONE_DESC; j++) {
3576 tc_idx = i * HNS3_TC_NUM_ONE_DESC + j;
3577 tc = &s_buf->tc_thrd[tc_idx];
3579 req->com_thrd[j].high =
3580 rte_cpu_to_le_16(tc->high >> HNS3_BUF_UNIT_S);
3581 req->com_thrd[j].high |=
3582 rte_cpu_to_le_16(BIT(HNS3_RX_PRIV_EN_B));
3583 req->com_thrd[j].low =
3584 rte_cpu_to_le_16(tc->low >> HNS3_BUF_UNIT_S);
3585 req->com_thrd[j].low |=
3586 rte_cpu_to_le_16(BIT(HNS3_RX_PRIV_EN_B));
3590 /* Send 2 descriptors at one time */
3591 ret = hns3_cmd_send(hw, desc, HNS3_RX_COM_THRD_ALLOC_DESC_NUM);
3593 PMD_INIT_LOG(ERR, "common threshold config cmd failed %d", ret);
3599 hns3_common_wl_config(struct hns3_hw *hw, struct hns3_pkt_buf_alloc *buf_alloc)
3601 struct hns3_shared_buf *buf = &buf_alloc->s_buf;
3602 struct hns3_rx_com_wl *req;
3603 struct hns3_cmd_desc desc;
3606 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_RX_COM_WL_ALLOC, false);
3608 req = (struct hns3_rx_com_wl *)desc.data;
3609 req->com_wl.high = rte_cpu_to_le_16(buf->self.high >> HNS3_BUF_UNIT_S);
3610 req->com_wl.high |= rte_cpu_to_le_16(BIT(HNS3_RX_PRIV_EN_B));
3612 req->com_wl.low = rte_cpu_to_le_16(buf->self.low >> HNS3_BUF_UNIT_S);
3613 req->com_wl.low |= rte_cpu_to_le_16(BIT(HNS3_RX_PRIV_EN_B));
3615 ret = hns3_cmd_send(hw, &desc, 1);
3617 PMD_INIT_LOG(ERR, "common waterline config cmd failed %d", ret);
3623 hns3_buffer_alloc(struct hns3_hw *hw)
3625 struct hns3_pkt_buf_alloc pkt_buf;
3628 memset(&pkt_buf, 0, sizeof(pkt_buf));
3629 ret = hns3_tx_buffer_calc(hw, &pkt_buf);
3632 "could not calc tx buffer size for all TCs %d",
3637 ret = hns3_tx_buffer_alloc(hw, &pkt_buf);
3639 PMD_INIT_LOG(ERR, "could not alloc tx buffers %d", ret);
3643 ret = hns3_rx_buffer_calc(hw, &pkt_buf);
3646 "could not calc rx priv buffer size for all TCs %d",
3651 ret = hns3_rx_priv_buf_alloc(hw, &pkt_buf);
3653 PMD_INIT_LOG(ERR, "could not alloc rx priv buffer %d", ret);
3657 if (hns3_dev_dcb_supported(hw)) {
3658 ret = hns3_rx_priv_wl_config(hw, &pkt_buf);
3661 "could not configure rx private waterline %d",
3666 ret = hns3_common_thrd_config(hw, &pkt_buf);
3669 "could not configure common threshold %d",
3675 ret = hns3_common_wl_config(hw, &pkt_buf);
3677 PMD_INIT_LOG(ERR, "could not configure common waterline %d",
3684 hns3_mac_init(struct hns3_hw *hw)
3686 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
3687 struct hns3_mac *mac = &hw->mac;
3688 struct hns3_pf *pf = &hns->pf;
3691 pf->support_sfp_query = true;
3692 mac->link_duplex = ETH_LINK_FULL_DUPLEX;
3693 ret = hns3_cfg_mac_speed_dup_hw(hw, mac->link_speed, mac->link_duplex);
3695 PMD_INIT_LOG(ERR, "Config mac speed dup fail ret = %d", ret);
3699 mac->link_status = ETH_LINK_DOWN;
3701 return hns3_config_mtu(hw, pf->mps);
3705 hns3_get_mac_ethertype_cmd_status(uint16_t cmdq_resp, uint8_t resp_code)
3707 #define HNS3_ETHERTYPE_SUCCESS_ADD 0
3708 #define HNS3_ETHERTYPE_ALREADY_ADD 1
3709 #define HNS3_ETHERTYPE_MGR_TBL_OVERFLOW 2
3710 #define HNS3_ETHERTYPE_KEY_CONFLICT 3
3715 "cmdq execute failed for get_mac_ethertype_cmd_status, status=%d.\n",
3720 switch (resp_code) {
3721 case HNS3_ETHERTYPE_SUCCESS_ADD:
3722 case HNS3_ETHERTYPE_ALREADY_ADD:
3725 case HNS3_ETHERTYPE_MGR_TBL_OVERFLOW:
3727 "add mac ethertype failed for manager table overflow.");
3728 return_status = -EIO;
3730 case HNS3_ETHERTYPE_KEY_CONFLICT:
3731 PMD_INIT_LOG(ERR, "add mac ethertype failed for key conflict.");
3732 return_status = -EIO;
3736 "add mac ethertype failed for undefined, code=%d.",
3738 return_status = -EIO;
3742 return return_status;
3746 hns3_add_mgr_tbl(struct hns3_hw *hw,
3747 const struct hns3_mac_mgr_tbl_entry_cmd *req)
3749 struct hns3_cmd_desc desc;
3754 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_MAC_ETHTYPE_ADD, false);
3755 memcpy(desc.data, req, sizeof(struct hns3_mac_mgr_tbl_entry_cmd));
3757 ret = hns3_cmd_send(hw, &desc, 1);
3760 "add mac ethertype failed for cmd_send, ret =%d.",
3765 resp_code = (rte_le_to_cpu_32(desc.data[0]) >> 8) & 0xff;
3766 retval = rte_le_to_cpu_16(desc.retval);
3768 return hns3_get_mac_ethertype_cmd_status(retval, resp_code);
3772 hns3_prepare_mgr_tbl(struct hns3_mac_mgr_tbl_entry_cmd *mgr_table,
3773 int *table_item_num)
3775 struct hns3_mac_mgr_tbl_entry_cmd *tbl;
3778 * In current version, we add one item in management table as below:
3779 * 0x0180C200000E -- LLDP MC address
3782 tbl->flags = HNS3_MAC_MGR_MASK_VLAN_B;
3783 tbl->ethter_type = rte_cpu_to_le_16(HNS3_MAC_ETHERTYPE_LLDP);
3784 tbl->mac_addr_hi32 = rte_cpu_to_le_32(htonl(0x0180C200));
3785 tbl->mac_addr_lo16 = rte_cpu_to_le_16(htons(0x000E));
3786 tbl->i_port_bitmap = 0x1;
3787 *table_item_num = 1;
3791 hns3_init_mgr_tbl(struct hns3_hw *hw)
3793 #define HNS_MAC_MGR_TBL_MAX_SIZE 16
3794 struct hns3_mac_mgr_tbl_entry_cmd mgr_table[HNS_MAC_MGR_TBL_MAX_SIZE];
3799 memset(mgr_table, 0, sizeof(mgr_table));
3800 hns3_prepare_mgr_tbl(mgr_table, &table_item_num);
3801 for (i = 0; i < table_item_num; i++) {
3802 ret = hns3_add_mgr_tbl(hw, &mgr_table[i]);
3804 PMD_INIT_LOG(ERR, "add mac ethertype failed, ret =%d",
3814 hns3_promisc_param_init(struct hns3_promisc_param *param, bool en_uc,
3815 bool en_mc, bool en_bc, int vport_id)
3820 memset(param, 0, sizeof(struct hns3_promisc_param));
3822 param->enable = HNS3_PROMISC_EN_UC;
3824 param->enable |= HNS3_PROMISC_EN_MC;
3826 param->enable |= HNS3_PROMISC_EN_BC;
3827 param->vf_id = vport_id;
3831 hns3_cmd_set_promisc_mode(struct hns3_hw *hw, struct hns3_promisc_param *param)
3833 struct hns3_promisc_cfg_cmd *req;
3834 struct hns3_cmd_desc desc;
3837 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_CFG_PROMISC_MODE, false);
3839 req = (struct hns3_promisc_cfg_cmd *)desc.data;
3840 req->vf_id = param->vf_id;
3841 req->flag = (param->enable << HNS3_PROMISC_EN_B) |
3842 HNS3_PROMISC_TX_EN_B | HNS3_PROMISC_RX_EN_B;
3844 ret = hns3_cmd_send(hw, &desc, 1);
3846 PMD_INIT_LOG(ERR, "Set promisc mode fail, ret = %d", ret);
3852 hns3_set_promisc_mode(struct hns3_hw *hw, bool en_uc_pmc, bool en_mc_pmc)
3854 struct hns3_promisc_param param;
3855 bool en_bc_pmc = true;
3859 * In current version VF is not supported when PF is driven by DPDK
3860 * driver, just need to configure parameters for PF vport.
3862 vf_id = HNS3_PF_FUNC_ID;
3864 hns3_promisc_param_init(¶m, en_uc_pmc, en_mc_pmc, en_bc_pmc, vf_id);
3865 return hns3_cmd_set_promisc_mode(hw, ¶m);
3869 hns3_promisc_init(struct hns3_hw *hw)
3871 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
3872 struct hns3_pf *pf = &hns->pf;
3873 struct hns3_promisc_param param;
3877 ret = hns3_set_promisc_mode(hw, false, false);
3879 PMD_INIT_LOG(ERR, "failed to set promisc mode, ret = %d", ret);
3884 * In current version VFs are not supported when PF is driven by DPDK
3885 * driver. After PF has been taken over by DPDK, the original VF will
3886 * be invalid. So, there is a possibility of entry residues. It should
3887 * clear VFs's promisc mode to avoid unnecessary bandwidth usage
3890 for (func_id = HNS3_1ST_VF_FUNC_ID; func_id < pf->func_num; func_id++) {
3891 hns3_promisc_param_init(¶m, false, false, false, func_id);
3892 ret = hns3_cmd_set_promisc_mode(hw, ¶m);
3894 PMD_INIT_LOG(ERR, "failed to clear vf:%d promisc mode,"
3895 " ret = %d", func_id, ret);
3904 hns3_promisc_uninit(struct hns3_hw *hw)
3906 struct hns3_promisc_param param;
3910 func_id = HNS3_PF_FUNC_ID;
3913 * In current version VFs are not supported when PF is driven by
3914 * DPDK driver, and VFs' promisc mode status has been cleared during
3915 * init and their status will not change. So just clear PF's promisc
3916 * mode status during uninit.
3918 hns3_promisc_param_init(¶m, false, false, false, func_id);
3919 ret = hns3_cmd_set_promisc_mode(hw, ¶m);
3921 PMD_INIT_LOG(ERR, "failed to clear promisc status during"
3922 " uninit, ret = %d", ret);
3926 hns3_dev_promiscuous_enable(struct rte_eth_dev *dev)
3928 bool allmulti = dev->data->all_multicast ? true : false;
3929 struct hns3_adapter *hns = dev->data->dev_private;
3930 struct hns3_hw *hw = &hns->hw;
3935 rte_spinlock_lock(&hw->lock);
3936 ret = hns3_set_promisc_mode(hw, true, true);
3938 rte_spinlock_unlock(&hw->lock);
3939 hns3_err(hw, "failed to enable promiscuous mode, ret = %d",
3945 * When promiscuous mode was enabled, disable the vlan filter to let
3946 * all packets coming in in the receiving direction.
3948 offloads = dev->data->dev_conf.rxmode.offloads;
3949 if (offloads & DEV_RX_OFFLOAD_VLAN_FILTER) {
3950 ret = hns3_enable_vlan_filter(hns, false);
3952 hns3_err(hw, "failed to enable promiscuous mode due to "
3953 "failure to disable vlan filter, ret = %d",
3955 err = hns3_set_promisc_mode(hw, false, allmulti);
3957 hns3_err(hw, "failed to restore promiscuous "
3958 "status after disable vlan filter "
3959 "failed during enabling promiscuous "
3960 "mode, ret = %d", ret);
3964 rte_spinlock_unlock(&hw->lock);
3970 hns3_dev_promiscuous_disable(struct rte_eth_dev *dev)
3972 bool allmulti = dev->data->all_multicast ? true : false;
3973 struct hns3_adapter *hns = dev->data->dev_private;
3974 struct hns3_hw *hw = &hns->hw;
3979 /* If now in all_multicast mode, must remain in all_multicast mode. */
3980 rte_spinlock_lock(&hw->lock);
3981 ret = hns3_set_promisc_mode(hw, false, allmulti);
3983 rte_spinlock_unlock(&hw->lock);
3984 hns3_err(hw, "failed to disable promiscuous mode, ret = %d",
3988 /* when promiscuous mode was disabled, restore the vlan filter status */
3989 offloads = dev->data->dev_conf.rxmode.offloads;
3990 if (offloads & DEV_RX_OFFLOAD_VLAN_FILTER) {
3991 ret = hns3_enable_vlan_filter(hns, true);
3993 hns3_err(hw, "failed to disable promiscuous mode due to"
3994 " failure to restore vlan filter, ret = %d",
3996 err = hns3_set_promisc_mode(hw, true, true);
3998 hns3_err(hw, "failed to restore promiscuous "
3999 "status after enabling vlan filter "
4000 "failed during disabling promiscuous "
4001 "mode, ret = %d", ret);
4004 rte_spinlock_unlock(&hw->lock);
4010 hns3_dev_allmulticast_enable(struct rte_eth_dev *dev)
4012 struct hns3_adapter *hns = dev->data->dev_private;
4013 struct hns3_hw *hw = &hns->hw;
4016 if (dev->data->promiscuous)
4019 rte_spinlock_lock(&hw->lock);
4020 ret = hns3_set_promisc_mode(hw, false, true);
4021 rte_spinlock_unlock(&hw->lock);
4023 hns3_err(hw, "failed to enable allmulticast mode, ret = %d",
4030 hns3_dev_allmulticast_disable(struct rte_eth_dev *dev)
4032 struct hns3_adapter *hns = dev->data->dev_private;
4033 struct hns3_hw *hw = &hns->hw;
4036 /* If now in promiscuous mode, must remain in all_multicast mode. */
4037 if (dev->data->promiscuous)
4040 rte_spinlock_lock(&hw->lock);
4041 ret = hns3_set_promisc_mode(hw, false, false);
4042 rte_spinlock_unlock(&hw->lock);
4044 hns3_err(hw, "failed to disable allmulticast mode, ret = %d",
4051 hns3_dev_promisc_restore(struct hns3_adapter *hns)
4053 struct hns3_hw *hw = &hns->hw;
4054 bool allmulti = hw->data->all_multicast ? true : false;
4057 if (hw->data->promiscuous) {
4058 ret = hns3_set_promisc_mode(hw, true, true);
4060 hns3_err(hw, "failed to restore promiscuous mode, "
4065 ret = hns3_set_promisc_mode(hw, false, allmulti);
4067 hns3_err(hw, "failed to restore allmulticast mode, ret = %d",
4073 hns3_get_sfp_speed(struct hns3_hw *hw, uint32_t *speed)
4075 struct hns3_sfp_speed_cmd *resp;
4076 struct hns3_cmd_desc desc;
4079 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_SFP_GET_SPEED, true);
4080 resp = (struct hns3_sfp_speed_cmd *)desc.data;
4081 ret = hns3_cmd_send(hw, &desc, 1);
4082 if (ret == -EOPNOTSUPP) {
4083 hns3_err(hw, "IMP do not support get SFP speed %d", ret);
4086 hns3_err(hw, "get sfp speed failed %d", ret);
4090 *speed = resp->sfp_speed;
4096 hns3_check_speed_dup(uint8_t duplex, uint32_t speed)
4098 if (!(speed == ETH_SPEED_NUM_10M || speed == ETH_SPEED_NUM_100M))
4099 duplex = ETH_LINK_FULL_DUPLEX;
4105 hns3_cfg_mac_speed_dup(struct hns3_hw *hw, uint32_t speed, uint8_t duplex)
4107 struct hns3_mac *mac = &hw->mac;
4110 duplex = hns3_check_speed_dup(duplex, speed);
4111 if (mac->link_speed == speed && mac->link_duplex == duplex)
4114 ret = hns3_cfg_mac_speed_dup_hw(hw, speed, duplex);
4118 mac->link_speed = speed;
4119 mac->link_duplex = duplex;
4125 hns3_update_speed_duplex(struct rte_eth_dev *eth_dev)
4127 struct hns3_adapter *hns = eth_dev->data->dev_private;
4128 struct hns3_hw *hw = &hns->hw;
4129 struct hns3_pf *pf = &hns->pf;
4133 /* If IMP do not support get SFP/qSFP speed, return directly */
4134 if (!pf->support_sfp_query)
4137 ret = hns3_get_sfp_speed(hw, &speed);
4138 if (ret == -EOPNOTSUPP) {
4139 pf->support_sfp_query = false;
4144 if (speed == ETH_SPEED_NUM_NONE)
4145 return 0; /* do nothing if no SFP */
4147 /* Config full duplex for SFP */
4148 return hns3_cfg_mac_speed_dup(hw, speed, ETH_LINK_FULL_DUPLEX);
4152 hns3_cfg_mac_mode(struct hns3_hw *hw, bool enable)
4154 struct hns3_config_mac_mode_cmd *req;
4155 struct hns3_cmd_desc desc;
4156 uint32_t loop_en = 0;
4160 req = (struct hns3_config_mac_mode_cmd *)desc.data;
4162 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_CONFIG_MAC_MODE, false);
4165 hns3_set_bit(loop_en, HNS3_MAC_TX_EN_B, val);
4166 hns3_set_bit(loop_en, HNS3_MAC_RX_EN_B, val);
4167 hns3_set_bit(loop_en, HNS3_MAC_PAD_TX_B, val);
4168 hns3_set_bit(loop_en, HNS3_MAC_PAD_RX_B, val);
4169 hns3_set_bit(loop_en, HNS3_MAC_1588_TX_B, 0);
4170 hns3_set_bit(loop_en, HNS3_MAC_1588_RX_B, 0);
4171 hns3_set_bit(loop_en, HNS3_MAC_APP_LP_B, 0);
4172 hns3_set_bit(loop_en, HNS3_MAC_LINE_LP_B, 0);
4173 hns3_set_bit(loop_en, HNS3_MAC_FCS_TX_B, val);
4174 hns3_set_bit(loop_en, HNS3_MAC_RX_FCS_B, val);
4175 hns3_set_bit(loop_en, HNS3_MAC_RX_FCS_STRIP_B, val);
4176 hns3_set_bit(loop_en, HNS3_MAC_TX_OVERSIZE_TRUNCATE_B, val);
4177 hns3_set_bit(loop_en, HNS3_MAC_RX_OVERSIZE_TRUNCATE_B, val);
4178 hns3_set_bit(loop_en, HNS3_MAC_TX_UNDER_MIN_ERR_B, val);
4179 req->txrx_pad_fcs_loop_en = rte_cpu_to_le_32(loop_en);
4181 ret = hns3_cmd_send(hw, &desc, 1);
4183 PMD_INIT_LOG(ERR, "mac enable fail, ret =%d.", ret);
4189 hns3_get_mac_link_status(struct hns3_hw *hw)
4191 struct hns3_link_status_cmd *req;
4192 struct hns3_cmd_desc desc;
4196 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_QUERY_LINK_STATUS, true);
4197 ret = hns3_cmd_send(hw, &desc, 1);
4199 hns3_err(hw, "get link status cmd failed %d", ret);
4200 return ETH_LINK_DOWN;
4203 req = (struct hns3_link_status_cmd *)desc.data;
4204 link_status = req->status & HNS3_LINK_STATUS_UP_M;
4206 return !!link_status;
4210 hns3_update_link_status(struct hns3_hw *hw)
4214 state = hns3_get_mac_link_status(hw);
4215 if (state != hw->mac.link_status) {
4216 hw->mac.link_status = state;
4217 hns3_warn(hw, "Link status change to %s!", state ? "up" : "down");
4222 hns3_service_handler(void *param)
4224 struct rte_eth_dev *eth_dev = (struct rte_eth_dev *)param;
4225 struct hns3_adapter *hns = eth_dev->data->dev_private;
4226 struct hns3_hw *hw = &hns->hw;
4228 if (!hns3_is_reset_pending(hns)) {
4229 hns3_update_speed_duplex(eth_dev);
4230 hns3_update_link_status(hw);
4232 hns3_warn(hw, "Cancel the query when reset is pending");
4234 rte_eal_alarm_set(HNS3_SERVICE_INTERVAL, hns3_service_handler, eth_dev);
4238 hns3_init_hardware(struct hns3_adapter *hns)
4240 struct hns3_hw *hw = &hns->hw;
4243 ret = hns3_map_tqp(hw);
4245 PMD_INIT_LOG(ERR, "Failed to map tqp: %d", ret);
4249 ret = hns3_init_umv_space(hw);
4251 PMD_INIT_LOG(ERR, "Failed to init umv space: %d", ret);
4255 ret = hns3_mac_init(hw);
4257 PMD_INIT_LOG(ERR, "Failed to init MAC: %d", ret);
4261 ret = hns3_init_mgr_tbl(hw);
4263 PMD_INIT_LOG(ERR, "Failed to init manager table: %d", ret);
4267 ret = hns3_promisc_init(hw);
4269 PMD_INIT_LOG(ERR, "Failed to init promisc: %d",
4274 ret = hns3_init_vlan_config(hns);
4276 PMD_INIT_LOG(ERR, "Failed to init vlan: %d", ret);
4280 ret = hns3_dcb_init(hw);
4282 PMD_INIT_LOG(ERR, "Failed to init dcb: %d", ret);
4286 ret = hns3_init_fd_config(hns);
4288 PMD_INIT_LOG(ERR, "Failed to init flow director: %d", ret);
4292 ret = hns3_config_tso(hw, HNS3_TSO_MSS_MIN, HNS3_TSO_MSS_MAX);
4294 PMD_INIT_LOG(ERR, "Failed to config tso: %d", ret);
4298 ret = hns3_config_gro(hw, false);
4300 PMD_INIT_LOG(ERR, "Failed to config gro: %d", ret);
4305 * In the initialization clearing the all hardware mapping relationship
4306 * configurations between queues and interrupt vectors is needed, so
4307 * some error caused by the residual configurations, such as the
4308 * unexpected interrupt, can be avoid.
4310 ret = hns3_init_ring_with_vector(hw);
4312 PMD_INIT_LOG(ERR, "Failed to init ring intr vector: %d", ret);
4319 hns3_uninit_umv_space(hw);
4324 hns3_clear_hw(struct hns3_hw *hw)
4326 struct hns3_cmd_desc desc;
4329 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_CLEAR_HW_STATE, false);
4331 ret = hns3_cmd_send(hw, &desc, 1);
4332 if (ret && ret != -EOPNOTSUPP)
4339 hns3_init_pf(struct rte_eth_dev *eth_dev)
4341 struct rte_device *dev = eth_dev->device;
4342 struct rte_pci_device *pci_dev = RTE_DEV_TO_PCI(dev);
4343 struct hns3_adapter *hns = eth_dev->data->dev_private;
4344 struct hns3_hw *hw = &hns->hw;
4347 PMD_INIT_FUNC_TRACE();
4349 /* Get hardware io base address from pcie BAR2 IO space */
4350 hw->io_base = pci_dev->mem_resource[2].addr;
4352 /* Firmware command queue initialize */
4353 ret = hns3_cmd_init_queue(hw);
4355 PMD_INIT_LOG(ERR, "Failed to init cmd queue: %d", ret);
4356 goto err_cmd_init_queue;
4359 hns3_clear_all_event_cause(hw);
4361 /* Firmware command initialize */
4362 ret = hns3_cmd_init(hw);
4364 PMD_INIT_LOG(ERR, "Failed to init cmd: %d", ret);
4369 * To ensure that the hardware environment is clean during
4370 * initialization, the driver actively clear the hardware environment
4371 * during initialization, including PF and corresponding VFs' vlan, mac,
4372 * flow table configurations, etc.
4374 ret = hns3_clear_hw(hw);
4376 PMD_INIT_LOG(ERR, "failed to clear hardware: %d", ret);
4380 ret = rte_intr_callback_register(&pci_dev->intr_handle,
4381 hns3_interrupt_handler,
4384 PMD_INIT_LOG(ERR, "Failed to register intr: %d", ret);
4385 goto err_intr_callback_register;
4388 /* Enable interrupt */
4389 rte_intr_enable(&pci_dev->intr_handle);
4390 hns3_pf_enable_irq0(hw);
4392 /* Get configuration */
4393 ret = hns3_get_configuration(hw);
4395 PMD_INIT_LOG(ERR, "Failed to fetch configuration: %d", ret);
4396 goto err_get_config;
4399 ret = hns3_init_hardware(hns);
4401 PMD_INIT_LOG(ERR, "Failed to init hardware: %d", ret);
4402 goto err_get_config;
4405 /* Initialize flow director filter list & hash */
4406 ret = hns3_fdir_filter_init(hns);
4408 PMD_INIT_LOG(ERR, "Failed to alloc hashmap for fdir: %d", ret);
4412 hns3_set_default_rss_args(hw);
4414 ret = hns3_enable_hw_error_intr(hns, true);
4416 PMD_INIT_LOG(ERR, "fail to enable hw error interrupts: %d",
4424 hns3_fdir_filter_uninit(hns);
4426 hns3_uninit_umv_space(hw);
4429 hns3_pf_disable_irq0(hw);
4430 rte_intr_disable(&pci_dev->intr_handle);
4431 hns3_intr_unregister(&pci_dev->intr_handle, hns3_interrupt_handler,
4433 err_intr_callback_register:
4435 hns3_cmd_uninit(hw);
4436 hns3_cmd_destroy_queue(hw);
4444 hns3_uninit_pf(struct rte_eth_dev *eth_dev)
4446 struct hns3_adapter *hns = eth_dev->data->dev_private;
4447 struct rte_device *dev = eth_dev->device;
4448 struct rte_pci_device *pci_dev = RTE_DEV_TO_PCI(dev);
4449 struct hns3_hw *hw = &hns->hw;
4451 PMD_INIT_FUNC_TRACE();
4453 hns3_enable_hw_error_intr(hns, false);
4454 hns3_rss_uninit(hns);
4455 (void)hns3_config_gro(hw, false);
4456 hns3_promisc_uninit(hw);
4457 hns3_fdir_filter_uninit(hns);
4458 hns3_uninit_umv_space(hw);
4459 hns3_pf_disable_irq0(hw);
4460 rte_intr_disable(&pci_dev->intr_handle);
4461 hns3_intr_unregister(&pci_dev->intr_handle, hns3_interrupt_handler,
4463 hns3_cmd_uninit(hw);
4464 hns3_cmd_destroy_queue(hw);
4469 hns3_do_start(struct hns3_adapter *hns, bool reset_queue)
4471 struct hns3_hw *hw = &hns->hw;
4474 ret = hns3_dcb_cfg_update(hns);
4479 ret = hns3_start_queues(hns, reset_queue);
4481 PMD_INIT_LOG(ERR, "Failed to start queues: %d", ret);
4486 ret = hns3_cfg_mac_mode(hw, true);
4488 PMD_INIT_LOG(ERR, "Failed to enable MAC: %d", ret);
4489 goto err_config_mac_mode;
4493 err_config_mac_mode:
4494 hns3_stop_queues(hns, true);
4499 hns3_map_rx_interrupt(struct rte_eth_dev *dev)
4501 struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
4502 struct rte_intr_handle *intr_handle = &pci_dev->intr_handle;
4503 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
4504 uint8_t base = RTE_INTR_VEC_ZERO_OFFSET;
4505 uint8_t vec = RTE_INTR_VEC_ZERO_OFFSET;
4506 uint32_t intr_vector;
4510 if (dev->data->dev_conf.intr_conf.rxq == 0)
4513 /* disable uio/vfio intr/eventfd mapping */
4514 rte_intr_disable(intr_handle);
4516 /* check and configure queue intr-vector mapping */
4517 if (rte_intr_cap_multiple(intr_handle) ||
4518 !RTE_ETH_DEV_SRIOV(dev).active) {
4519 intr_vector = hw->used_rx_queues;
4520 /* creates event fd for each intr vector when MSIX is used */
4521 if (rte_intr_efd_enable(intr_handle, intr_vector))
4524 if (rte_intr_dp_is_en(intr_handle) && !intr_handle->intr_vec) {
4525 intr_handle->intr_vec =
4526 rte_zmalloc("intr_vec",
4527 hw->used_rx_queues * sizeof(int), 0);
4528 if (intr_handle->intr_vec == NULL) {
4529 hns3_err(hw, "Failed to allocate %d rx_queues"
4530 " intr_vec", hw->used_rx_queues);
4532 goto alloc_intr_vec_error;
4536 if (rte_intr_allow_others(intr_handle)) {
4537 vec = RTE_INTR_VEC_RXTX_OFFSET;
4538 base = RTE_INTR_VEC_RXTX_OFFSET;
4540 if (rte_intr_dp_is_en(intr_handle)) {
4541 for (q_id = 0; q_id < hw->used_rx_queues; q_id++) {
4542 ret = hns3_bind_ring_with_vector(hw, vec, true,
4546 goto bind_vector_error;
4547 intr_handle->intr_vec[q_id] = vec;
4548 if (vec < base + intr_handle->nb_efd - 1)
4552 rte_intr_enable(intr_handle);
4556 rte_intr_efd_disable(intr_handle);
4557 if (intr_handle->intr_vec) {
4558 free(intr_handle->intr_vec);
4559 intr_handle->intr_vec = NULL;
4562 alloc_intr_vec_error:
4563 rte_intr_efd_disable(intr_handle);
4568 hns3_restore_rx_interrupt(struct hns3_hw *hw)
4570 struct rte_eth_dev *dev = &rte_eth_devices[hw->data->port_id];
4571 struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
4572 struct rte_intr_handle *intr_handle = &pci_dev->intr_handle;
4576 if (dev->data->dev_conf.intr_conf.rxq == 0)
4579 if (rte_intr_dp_is_en(intr_handle)) {
4580 for (q_id = 0; q_id < hw->used_rx_queues; q_id++) {
4581 ret = hns3_bind_ring_with_vector(hw,
4582 intr_handle->intr_vec[q_id], true,
4583 HNS3_RING_TYPE_RX, q_id);
4593 hns3_restore_filter(struct rte_eth_dev *dev)
4595 hns3_restore_rss_filter(dev);
4599 hns3_dev_start(struct rte_eth_dev *dev)
4601 struct hns3_adapter *hns = dev->data->dev_private;
4602 struct hns3_hw *hw = &hns->hw;
4605 PMD_INIT_FUNC_TRACE();
4606 if (rte_atomic16_read(&hw->reset.resetting))
4609 rte_spinlock_lock(&hw->lock);
4610 hw->adapter_state = HNS3_NIC_STARTING;
4612 ret = hns3_do_start(hns, true);
4614 hw->adapter_state = HNS3_NIC_CONFIGURED;
4615 rte_spinlock_unlock(&hw->lock);
4618 ret = hns3_map_rx_interrupt(dev);
4620 hw->adapter_state = HNS3_NIC_CONFIGURED;
4621 rte_spinlock_unlock(&hw->lock);
4625 hw->adapter_state = HNS3_NIC_STARTED;
4626 rte_spinlock_unlock(&hw->lock);
4628 hns3_set_rxtx_function(dev);
4629 hns3_mp_req_start_rxtx(dev);
4630 rte_eal_alarm_set(HNS3_SERVICE_INTERVAL, hns3_service_handler, dev);
4632 hns3_restore_filter(dev);
4634 /* Enable interrupt of all rx queues before enabling queues */
4635 hns3_dev_all_rx_queue_intr_enable(hw, true);
4637 * When finished the initialization, enable queues to receive/transmit
4640 hns3_enable_all_queues(hw, true);
4642 hns3_info(hw, "hns3 dev start successful!");
4647 hns3_do_stop(struct hns3_adapter *hns)
4649 struct hns3_hw *hw = &hns->hw;
4653 ret = hns3_cfg_mac_mode(hw, false);
4656 hw->mac.link_status = ETH_LINK_DOWN;
4658 if (rte_atomic16_read(&hw->reset.disable_cmd) == 0) {
4659 hns3_configure_all_mac_addr(hns, true);
4662 reset_queue = false;
4663 hw->mac.default_addr_setted = false;
4664 return hns3_stop_queues(hns, reset_queue);
4668 hns3_unmap_rx_interrupt(struct rte_eth_dev *dev)
4670 struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
4671 struct rte_intr_handle *intr_handle = &pci_dev->intr_handle;
4672 struct hns3_adapter *hns = dev->data->dev_private;
4673 struct hns3_hw *hw = &hns->hw;
4674 uint8_t base = RTE_INTR_VEC_ZERO_OFFSET;
4675 uint8_t vec = RTE_INTR_VEC_ZERO_OFFSET;
4678 if (dev->data->dev_conf.intr_conf.rxq == 0)
4681 /* unmap the ring with vector */
4682 if (rte_intr_allow_others(intr_handle)) {
4683 vec = RTE_INTR_VEC_RXTX_OFFSET;
4684 base = RTE_INTR_VEC_RXTX_OFFSET;
4686 if (rte_intr_dp_is_en(intr_handle)) {
4687 for (q_id = 0; q_id < hw->used_rx_queues; q_id++) {
4688 (void)hns3_bind_ring_with_vector(hw, vec, false,
4691 if (vec < base + intr_handle->nb_efd - 1)
4695 /* Clean datapath event and queue/vec mapping */
4696 rte_intr_efd_disable(intr_handle);
4697 if (intr_handle->intr_vec) {
4698 rte_free(intr_handle->intr_vec);
4699 intr_handle->intr_vec = NULL;
4704 hns3_dev_stop(struct rte_eth_dev *dev)
4706 struct hns3_adapter *hns = dev->data->dev_private;
4707 struct hns3_hw *hw = &hns->hw;
4709 PMD_INIT_FUNC_TRACE();
4711 hw->adapter_state = HNS3_NIC_STOPPING;
4712 hns3_set_rxtx_function(dev);
4714 /* Disable datapath on secondary process. */
4715 hns3_mp_req_stop_rxtx(dev);
4716 /* Prevent crashes when queues are still in use. */
4717 rte_delay_ms(hw->tqps_num);
4719 rte_spinlock_lock(&hw->lock);
4720 if (rte_atomic16_read(&hw->reset.resetting) == 0) {
4722 hns3_unmap_rx_interrupt(dev);
4723 hns3_dev_release_mbufs(hns);
4724 hw->adapter_state = HNS3_NIC_CONFIGURED;
4726 rte_eal_alarm_cancel(hns3_service_handler, dev);
4727 rte_spinlock_unlock(&hw->lock);
4731 hns3_dev_close(struct rte_eth_dev *eth_dev)
4733 struct hns3_adapter *hns = eth_dev->data->dev_private;
4734 struct hns3_hw *hw = &hns->hw;
4736 if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
4737 rte_free(eth_dev->process_private);
4738 eth_dev->process_private = NULL;
4742 if (hw->adapter_state == HNS3_NIC_STARTED)
4743 hns3_dev_stop(eth_dev);
4745 hw->adapter_state = HNS3_NIC_CLOSING;
4746 hns3_reset_abort(hns);
4747 hw->adapter_state = HNS3_NIC_CLOSED;
4749 hns3_configure_all_mc_mac_addr(hns, true);
4750 hns3_remove_all_vlan_table(hns);
4751 hns3_vlan_txvlan_cfg(hns, HNS3_PORT_BASE_VLAN_DISABLE, 0);
4752 hns3_uninit_pf(eth_dev);
4753 hns3_free_all_queues(eth_dev);
4754 rte_free(hw->reset.wait_data);
4755 rte_free(eth_dev->process_private);
4756 eth_dev->process_private = NULL;
4757 hns3_mp_uninit_primary();
4758 hns3_warn(hw, "Close port %d finished", hw->data->port_id);
4762 hns3_flow_ctrl_get(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
4764 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
4765 struct hns3_pf *pf = HNS3_DEV_PRIVATE_TO_PF(dev->data->dev_private);
4767 fc_conf->pause_time = pf->pause_time;
4769 /* return fc current mode */
4770 switch (hw->current_mode) {
4772 fc_conf->mode = RTE_FC_FULL;
4774 case HNS3_FC_TX_PAUSE:
4775 fc_conf->mode = RTE_FC_TX_PAUSE;
4777 case HNS3_FC_RX_PAUSE:
4778 fc_conf->mode = RTE_FC_RX_PAUSE;
4782 fc_conf->mode = RTE_FC_NONE;
4790 hns3_get_fc_mode(struct hns3_hw *hw, enum rte_eth_fc_mode mode)
4794 hw->requested_mode = HNS3_FC_NONE;
4796 case RTE_FC_RX_PAUSE:
4797 hw->requested_mode = HNS3_FC_RX_PAUSE;
4799 case RTE_FC_TX_PAUSE:
4800 hw->requested_mode = HNS3_FC_TX_PAUSE;
4803 hw->requested_mode = HNS3_FC_FULL;
4806 hw->requested_mode = HNS3_FC_NONE;
4807 hns3_warn(hw, "fc_mode(%u) exceeds member scope and is "
4808 "configured to RTE_FC_NONE", mode);
4814 hns3_flow_ctrl_set(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
4816 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
4817 struct hns3_pf *pf = HNS3_DEV_PRIVATE_TO_PF(dev->data->dev_private);
4820 if (fc_conf->high_water || fc_conf->low_water ||
4821 fc_conf->send_xon || fc_conf->mac_ctrl_frame_fwd) {
4822 hns3_err(hw, "Unsupported flow control settings specified, "
4823 "high_water(%u), low_water(%u), send_xon(%u) and "
4824 "mac_ctrl_frame_fwd(%u) must be set to '0'",
4825 fc_conf->high_water, fc_conf->low_water,
4826 fc_conf->send_xon, fc_conf->mac_ctrl_frame_fwd);
4829 if (fc_conf->autoneg) {
4830 hns3_err(hw, "Unsupported fc auto-negotiation setting.");
4833 if (!fc_conf->pause_time) {
4834 hns3_err(hw, "Invalid pause time %d setting.",
4835 fc_conf->pause_time);
4839 if (!(hw->current_fc_status == HNS3_FC_STATUS_NONE ||
4840 hw->current_fc_status == HNS3_FC_STATUS_MAC_PAUSE)) {
4841 hns3_err(hw, "PFC is enabled. Cannot set MAC pause. "
4842 "current_fc_status = %d", hw->current_fc_status);
4846 hns3_get_fc_mode(hw, fc_conf->mode);
4847 if (hw->requested_mode == hw->current_mode &&
4848 pf->pause_time == fc_conf->pause_time)
4851 rte_spinlock_lock(&hw->lock);
4852 ret = hns3_fc_enable(dev, fc_conf);
4853 rte_spinlock_unlock(&hw->lock);
4859 hns3_priority_flow_ctrl_set(struct rte_eth_dev *dev,
4860 struct rte_eth_pfc_conf *pfc_conf)
4862 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
4863 struct hns3_pf *pf = HNS3_DEV_PRIVATE_TO_PF(dev->data->dev_private);
4867 if (!hns3_dev_dcb_supported(hw)) {
4868 hns3_err(hw, "This port does not support dcb configurations.");
4872 if (pfc_conf->fc.high_water || pfc_conf->fc.low_water ||
4873 pfc_conf->fc.send_xon || pfc_conf->fc.mac_ctrl_frame_fwd) {
4874 hns3_err(hw, "Unsupported flow control settings specified, "
4875 "high_water(%u), low_water(%u), send_xon(%u) and "
4876 "mac_ctrl_frame_fwd(%u) must be set to '0'",
4877 pfc_conf->fc.high_water, pfc_conf->fc.low_water,
4878 pfc_conf->fc.send_xon,
4879 pfc_conf->fc.mac_ctrl_frame_fwd);
4882 if (pfc_conf->fc.autoneg) {
4883 hns3_err(hw, "Unsupported fc auto-negotiation setting.");
4886 if (pfc_conf->fc.pause_time == 0) {
4887 hns3_err(hw, "Invalid pause time %d setting.",
4888 pfc_conf->fc.pause_time);
4892 if (!(hw->current_fc_status == HNS3_FC_STATUS_NONE ||
4893 hw->current_fc_status == HNS3_FC_STATUS_PFC)) {
4894 hns3_err(hw, "MAC pause is enabled. Cannot set PFC."
4895 "current_fc_status = %d", hw->current_fc_status);
4899 priority = pfc_conf->priority;
4900 hns3_get_fc_mode(hw, pfc_conf->fc.mode);
4901 if (hw->dcb_info.pfc_en & BIT(priority) &&
4902 hw->requested_mode == hw->current_mode &&
4903 pfc_conf->fc.pause_time == pf->pause_time)
4906 rte_spinlock_lock(&hw->lock);
4907 ret = hns3_dcb_pfc_enable(dev, pfc_conf);
4908 rte_spinlock_unlock(&hw->lock);
4914 hns3_get_dcb_info(struct rte_eth_dev *dev, struct rte_eth_dcb_info *dcb_info)
4916 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
4917 struct hns3_pf *pf = HNS3_DEV_PRIVATE_TO_PF(dev->data->dev_private);
4918 enum rte_eth_rx_mq_mode mq_mode = dev->data->dev_conf.rxmode.mq_mode;
4921 rte_spinlock_lock(&hw->lock);
4922 if ((uint32_t)mq_mode & ETH_MQ_RX_DCB_FLAG)
4923 dcb_info->nb_tcs = pf->local_max_tc;
4925 dcb_info->nb_tcs = 1;
4927 for (i = 0; i < HNS3_MAX_USER_PRIO; i++)
4928 dcb_info->prio_tc[i] = hw->dcb_info.prio_tc[i];
4929 for (i = 0; i < dcb_info->nb_tcs; i++)
4930 dcb_info->tc_bws[i] = hw->dcb_info.pg_info[0].tc_dwrr[i];
4932 for (i = 0; i < hw->num_tc; i++) {
4933 dcb_info->tc_queue.tc_rxq[0][i].base = hw->alloc_rss_size * i;
4934 dcb_info->tc_queue.tc_txq[0][i].base =
4935 hw->tc_queue[i].tqp_offset;
4936 dcb_info->tc_queue.tc_rxq[0][i].nb_queue = hw->alloc_rss_size;
4937 dcb_info->tc_queue.tc_txq[0][i].nb_queue =
4938 hw->tc_queue[i].tqp_count;
4940 rte_spinlock_unlock(&hw->lock);
4946 hns3_reinit_dev(struct hns3_adapter *hns)
4948 struct hns3_hw *hw = &hns->hw;
4951 ret = hns3_cmd_init(hw);
4953 hns3_err(hw, "Failed to init cmd: %d", ret);
4957 ret = hns3_reset_all_queues(hns);
4959 hns3_err(hw, "Failed to reset all queues: %d", ret);
4963 ret = hns3_init_hardware(hns);
4965 hns3_err(hw, "Failed to init hardware: %d", ret);
4969 ret = hns3_enable_hw_error_intr(hns, true);
4971 hns3_err(hw, "fail to enable hw error interrupts: %d",
4975 hns3_info(hw, "Reset done, driver initialization finished.");
4981 is_pf_reset_done(struct hns3_hw *hw)
4983 uint32_t val, reg, reg_bit;
4985 switch (hw->reset.level) {
4986 case HNS3_IMP_RESET:
4987 reg = HNS3_GLOBAL_RESET_REG;
4988 reg_bit = HNS3_IMP_RESET_BIT;
4990 case HNS3_GLOBAL_RESET:
4991 reg = HNS3_GLOBAL_RESET_REG;
4992 reg_bit = HNS3_GLOBAL_RESET_BIT;
4994 case HNS3_FUNC_RESET:
4995 reg = HNS3_FUN_RST_ING;
4996 reg_bit = HNS3_FUN_RST_ING_B;
4998 case HNS3_FLR_RESET:
5000 hns3_err(hw, "Wait for unsupported reset level: %d",
5004 val = hns3_read_dev(hw, reg);
5005 if (hns3_get_bit(val, reg_bit))
5012 hns3_is_reset_pending(struct hns3_adapter *hns)
5014 struct hns3_hw *hw = &hns->hw;
5015 enum hns3_reset_level reset;
5017 hns3_check_event_cause(hns, NULL);
5018 reset = hns3_get_reset_level(hns, &hw->reset.pending);
5019 if (hw->reset.level != HNS3_NONE_RESET && hw->reset.level < reset) {
5020 hns3_warn(hw, "High level reset %d is pending", reset);
5023 reset = hns3_get_reset_level(hns, &hw->reset.request);
5024 if (hw->reset.level != HNS3_NONE_RESET && hw->reset.level < reset) {
5025 hns3_warn(hw, "High level reset %d is request", reset);
5032 hns3_wait_hardware_ready(struct hns3_adapter *hns)
5034 struct hns3_hw *hw = &hns->hw;
5035 struct hns3_wait_data *wait_data = hw->reset.wait_data;
5038 if (wait_data->result == HNS3_WAIT_SUCCESS)
5040 else if (wait_data->result == HNS3_WAIT_TIMEOUT) {
5041 gettimeofday(&tv, NULL);
5042 hns3_warn(hw, "Reset step4 hardware not ready after reset time=%ld.%.6ld",
5043 tv.tv_sec, tv.tv_usec);
5045 } else if (wait_data->result == HNS3_WAIT_REQUEST)
5048 wait_data->hns = hns;
5049 wait_data->check_completion = is_pf_reset_done;
5050 wait_data->end_ms = (uint64_t)HNS3_RESET_WAIT_CNT *
5051 HNS3_RESET_WAIT_MS + get_timeofday_ms();
5052 wait_data->interval = HNS3_RESET_WAIT_MS * USEC_PER_MSEC;
5053 wait_data->count = HNS3_RESET_WAIT_CNT;
5054 wait_data->result = HNS3_WAIT_REQUEST;
5055 rte_eal_alarm_set(wait_data->interval, hns3_wait_callback, wait_data);
5060 hns3_func_reset_cmd(struct hns3_hw *hw, int func_id)
5062 struct hns3_cmd_desc desc;
5063 struct hns3_reset_cmd *req = (struct hns3_reset_cmd *)desc.data;
5065 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_CFG_RST_TRIGGER, false);
5066 hns3_set_bit(req->mac_func_reset, HNS3_CFG_RESET_FUNC_B, 1);
5067 req->fun_reset_vfid = func_id;
5069 return hns3_cmd_send(hw, &desc, 1);
5073 hns3_imp_reset_cmd(struct hns3_hw *hw)
5075 struct hns3_cmd_desc desc;
5077 hns3_cmd_setup_basic_desc(&desc, 0xFFFE, false);
5078 desc.data[0] = 0xeedd;
5080 return hns3_cmd_send(hw, &desc, 1);
5084 hns3_msix_process(struct hns3_adapter *hns, enum hns3_reset_level reset_level)
5086 struct hns3_hw *hw = &hns->hw;
5090 gettimeofday(&tv, NULL);
5091 if (hns3_read_dev(hw, HNS3_GLOBAL_RESET_REG) ||
5092 hns3_read_dev(hw, HNS3_FUN_RST_ING)) {
5093 hns3_warn(hw, "Don't process msix during resetting time=%ld.%.6ld",
5094 tv.tv_sec, tv.tv_usec);
5098 switch (reset_level) {
5099 case HNS3_IMP_RESET:
5100 hns3_imp_reset_cmd(hw);
5101 hns3_warn(hw, "IMP Reset requested time=%ld.%.6ld",
5102 tv.tv_sec, tv.tv_usec);
5104 case HNS3_GLOBAL_RESET:
5105 val = hns3_read_dev(hw, HNS3_GLOBAL_RESET_REG);
5106 hns3_set_bit(val, HNS3_GLOBAL_RESET_BIT, 1);
5107 hns3_write_dev(hw, HNS3_GLOBAL_RESET_REG, val);
5108 hns3_warn(hw, "Global Reset requested time=%ld.%.6ld",
5109 tv.tv_sec, tv.tv_usec);
5111 case HNS3_FUNC_RESET:
5112 hns3_warn(hw, "PF Reset requested time=%ld.%.6ld",
5113 tv.tv_sec, tv.tv_usec);
5114 /* schedule again to check later */
5115 hns3_atomic_set_bit(HNS3_FUNC_RESET, &hw->reset.pending);
5116 hns3_schedule_reset(hns);
5119 hns3_warn(hw, "Unsupported reset level: %d", reset_level);
5122 hns3_atomic_clear_bit(reset_level, &hw->reset.request);
5125 static enum hns3_reset_level
5126 hns3_get_reset_level(struct hns3_adapter *hns, uint64_t *levels)
5128 struct hns3_hw *hw = &hns->hw;
5129 enum hns3_reset_level reset_level = HNS3_NONE_RESET;
5131 /* Return the highest priority reset level amongst all */
5132 if (hns3_atomic_test_bit(HNS3_IMP_RESET, levels))
5133 reset_level = HNS3_IMP_RESET;
5134 else if (hns3_atomic_test_bit(HNS3_GLOBAL_RESET, levels))
5135 reset_level = HNS3_GLOBAL_RESET;
5136 else if (hns3_atomic_test_bit(HNS3_FUNC_RESET, levels))
5137 reset_level = HNS3_FUNC_RESET;
5138 else if (hns3_atomic_test_bit(HNS3_FLR_RESET, levels))
5139 reset_level = HNS3_FLR_RESET;
5141 if (hw->reset.level != HNS3_NONE_RESET && reset_level < hw->reset.level)
5142 return HNS3_NONE_RESET;
5148 hns3_prepare_reset(struct hns3_adapter *hns)
5150 struct hns3_hw *hw = &hns->hw;
5154 switch (hw->reset.level) {
5155 case HNS3_FUNC_RESET:
5156 ret = hns3_func_reset_cmd(hw, HNS3_PF_FUNC_ID);
5161 * After performaning pf reset, it is not necessary to do the
5162 * mailbox handling or send any command to firmware, because
5163 * any mailbox handling or command to firmware is only valid
5164 * after hns3_cmd_init is called.
5166 rte_atomic16_set(&hw->reset.disable_cmd, 1);
5167 hw->reset.stats.request_cnt++;
5169 case HNS3_IMP_RESET:
5170 reg_val = hns3_read_dev(hw, HNS3_VECTOR0_OTER_EN_REG);
5171 hns3_write_dev(hw, HNS3_VECTOR0_OTER_EN_REG, reg_val |
5172 BIT(HNS3_VECTOR0_IMP_RESET_INT_B));
5181 hns3_set_rst_done(struct hns3_hw *hw)
5183 struct hns3_pf_rst_done_cmd *req;
5184 struct hns3_cmd_desc desc;
5186 req = (struct hns3_pf_rst_done_cmd *)desc.data;
5187 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_PF_RST_DONE, false);
5188 req->pf_rst_done |= HNS3_PF_RESET_DONE_BIT;
5189 return hns3_cmd_send(hw, &desc, 1);
5193 hns3_stop_service(struct hns3_adapter *hns)
5195 struct hns3_hw *hw = &hns->hw;
5196 struct rte_eth_dev *eth_dev;
5198 eth_dev = &rte_eth_devices[hw->data->port_id];
5199 if (hw->adapter_state == HNS3_NIC_STARTED)
5200 rte_eal_alarm_cancel(hns3_service_handler, eth_dev);
5201 hw->mac.link_status = ETH_LINK_DOWN;
5203 hns3_set_rxtx_function(eth_dev);
5205 /* Disable datapath on secondary process. */
5206 hns3_mp_req_stop_rxtx(eth_dev);
5207 rte_delay_ms(hw->tqps_num);
5209 rte_spinlock_lock(&hw->lock);
5210 if (hns->hw.adapter_state == HNS3_NIC_STARTED ||
5211 hw->adapter_state == HNS3_NIC_STOPPING) {
5213 hw->reset.mbuf_deferred_free = true;
5215 hw->reset.mbuf_deferred_free = false;
5218 * It is cumbersome for hardware to pick-and-choose entries for deletion
5219 * from table space. Hence, for function reset software intervention is
5220 * required to delete the entries
5222 if (rte_atomic16_read(&hw->reset.disable_cmd) == 0)
5223 hns3_configure_all_mc_mac_addr(hns, true);
5224 rte_spinlock_unlock(&hw->lock);
5230 hns3_start_service(struct hns3_adapter *hns)
5232 struct hns3_hw *hw = &hns->hw;
5233 struct rte_eth_dev *eth_dev;
5235 if (hw->reset.level == HNS3_IMP_RESET ||
5236 hw->reset.level == HNS3_GLOBAL_RESET)
5237 hns3_set_rst_done(hw);
5238 eth_dev = &rte_eth_devices[hw->data->port_id];
5239 hns3_set_rxtx_function(eth_dev);
5240 hns3_mp_req_start_rxtx(eth_dev);
5241 if (hw->adapter_state == HNS3_NIC_STARTED) {
5242 hns3_service_handler(eth_dev);
5244 /* Enable interrupt of all rx queues before enabling queues */
5245 hns3_dev_all_rx_queue_intr_enable(hw, true);
5247 * When finished the initialization, enable queues to receive
5248 * and transmit packets.
5250 hns3_enable_all_queues(hw, true);
5257 hns3_restore_conf(struct hns3_adapter *hns)
5259 struct hns3_hw *hw = &hns->hw;
5262 ret = hns3_configure_all_mac_addr(hns, false);
5266 ret = hns3_configure_all_mc_mac_addr(hns, false);
5270 ret = hns3_dev_promisc_restore(hns);
5274 ret = hns3_restore_vlan_table(hns);
5278 ret = hns3_restore_vlan_conf(hns);
5282 ret = hns3_restore_all_fdir_filter(hns);
5286 ret = hns3_restore_rx_interrupt(hw);
5290 ret = hns3_restore_gro_conf(hw);
5294 if (hns->hw.adapter_state == HNS3_NIC_STARTED) {
5295 ret = hns3_do_start(hns, false);
5298 hns3_info(hw, "hns3 dev restart successful!");
5299 } else if (hw->adapter_state == HNS3_NIC_STOPPING)
5300 hw->adapter_state = HNS3_NIC_CONFIGURED;
5304 hns3_configure_all_mc_mac_addr(hns, true);
5306 hns3_configure_all_mac_addr(hns, true);
5311 hns3_reset_service(void *param)
5313 struct hns3_adapter *hns = (struct hns3_adapter *)param;
5314 struct hns3_hw *hw = &hns->hw;
5315 enum hns3_reset_level reset_level;
5316 struct timeval tv_delta;
5317 struct timeval tv_start;
5323 * The interrupt is not triggered within the delay time.
5324 * The interrupt may have been lost. It is necessary to handle
5325 * the interrupt to recover from the error.
5327 if (rte_atomic16_read(&hns->hw.reset.schedule) == SCHEDULE_DEFERRED) {
5328 rte_atomic16_set(&hns->hw.reset.schedule, SCHEDULE_REQUESTED);
5329 hns3_err(hw, "Handling interrupts in delayed tasks");
5330 hns3_interrupt_handler(&rte_eth_devices[hw->data->port_id]);
5331 reset_level = hns3_get_reset_level(hns, &hw->reset.pending);
5332 if (reset_level == HNS3_NONE_RESET) {
5333 hns3_err(hw, "No reset level is set, try IMP reset");
5334 hns3_atomic_set_bit(HNS3_IMP_RESET, &hw->reset.pending);
5337 rte_atomic16_set(&hns->hw.reset.schedule, SCHEDULE_NONE);
5340 * Check if there is any ongoing reset in the hardware. This status can
5341 * be checked from reset_pending. If there is then, we need to wait for
5342 * hardware to complete reset.
5343 * a. If we are able to figure out in reasonable time that hardware
5344 * has fully resetted then, we can proceed with driver, client
5346 * b. else, we can come back later to check this status so re-sched
5349 reset_level = hns3_get_reset_level(hns, &hw->reset.pending);
5350 if (reset_level != HNS3_NONE_RESET) {
5351 gettimeofday(&tv_start, NULL);
5352 ret = hns3_reset_process(hns, reset_level);
5353 gettimeofday(&tv, NULL);
5354 timersub(&tv, &tv_start, &tv_delta);
5355 msec = tv_delta.tv_sec * MSEC_PER_SEC +
5356 tv_delta.tv_usec / USEC_PER_MSEC;
5357 if (msec > HNS3_RESET_PROCESS_MS)
5358 hns3_err(hw, "%d handle long time delta %" PRIx64
5359 " ms time=%ld.%.6ld",
5360 hw->reset.level, msec,
5361 tv.tv_sec, tv.tv_usec);
5366 /* Check if we got any *new* reset requests to be honored */
5367 reset_level = hns3_get_reset_level(hns, &hw->reset.request);
5368 if (reset_level != HNS3_NONE_RESET)
5369 hns3_msix_process(hns, reset_level);
5372 static const struct eth_dev_ops hns3_eth_dev_ops = {
5373 .dev_start = hns3_dev_start,
5374 .dev_stop = hns3_dev_stop,
5375 .dev_close = hns3_dev_close,
5376 .promiscuous_enable = hns3_dev_promiscuous_enable,
5377 .promiscuous_disable = hns3_dev_promiscuous_disable,
5378 .allmulticast_enable = hns3_dev_allmulticast_enable,
5379 .allmulticast_disable = hns3_dev_allmulticast_disable,
5380 .mtu_set = hns3_dev_mtu_set,
5381 .stats_get = hns3_stats_get,
5382 .stats_reset = hns3_stats_reset,
5383 .xstats_get = hns3_dev_xstats_get,
5384 .xstats_get_names = hns3_dev_xstats_get_names,
5385 .xstats_reset = hns3_dev_xstats_reset,
5386 .xstats_get_by_id = hns3_dev_xstats_get_by_id,
5387 .xstats_get_names_by_id = hns3_dev_xstats_get_names_by_id,
5388 .dev_infos_get = hns3_dev_infos_get,
5389 .fw_version_get = hns3_fw_version_get,
5390 .rx_queue_setup = hns3_rx_queue_setup,
5391 .tx_queue_setup = hns3_tx_queue_setup,
5392 .rx_queue_release = hns3_dev_rx_queue_release,
5393 .tx_queue_release = hns3_dev_tx_queue_release,
5394 .rx_queue_intr_enable = hns3_dev_rx_queue_intr_enable,
5395 .rx_queue_intr_disable = hns3_dev_rx_queue_intr_disable,
5396 .dev_configure = hns3_dev_configure,
5397 .flow_ctrl_get = hns3_flow_ctrl_get,
5398 .flow_ctrl_set = hns3_flow_ctrl_set,
5399 .priority_flow_ctrl_set = hns3_priority_flow_ctrl_set,
5400 .mac_addr_add = hns3_add_mac_addr,
5401 .mac_addr_remove = hns3_remove_mac_addr,
5402 .mac_addr_set = hns3_set_default_mac_addr,
5403 .set_mc_addr_list = hns3_set_mc_mac_addr_list,
5404 .link_update = hns3_dev_link_update,
5405 .rss_hash_update = hns3_dev_rss_hash_update,
5406 .rss_hash_conf_get = hns3_dev_rss_hash_conf_get,
5407 .reta_update = hns3_dev_rss_reta_update,
5408 .reta_query = hns3_dev_rss_reta_query,
5409 .filter_ctrl = hns3_dev_filter_ctrl,
5410 .vlan_filter_set = hns3_vlan_filter_set,
5411 .vlan_tpid_set = hns3_vlan_tpid_set,
5412 .vlan_offload_set = hns3_vlan_offload_set,
5413 .vlan_pvid_set = hns3_vlan_pvid_set,
5414 .get_reg = hns3_get_regs,
5415 .get_dcb_info = hns3_get_dcb_info,
5416 .dev_supported_ptypes_get = hns3_dev_supported_ptypes_get,
5419 static const struct hns3_reset_ops hns3_reset_ops = {
5420 .reset_service = hns3_reset_service,
5421 .stop_service = hns3_stop_service,
5422 .prepare_reset = hns3_prepare_reset,
5423 .wait_hardware_ready = hns3_wait_hardware_ready,
5424 .reinit_dev = hns3_reinit_dev,
5425 .restore_conf = hns3_restore_conf,
5426 .start_service = hns3_start_service,
5430 hns3_dev_init(struct rte_eth_dev *eth_dev)
5432 struct hns3_adapter *hns = eth_dev->data->dev_private;
5433 struct hns3_hw *hw = &hns->hw;
5436 PMD_INIT_FUNC_TRACE();
5438 eth_dev->process_private = (struct hns3_process_private *)
5439 rte_zmalloc_socket("hns3_filter_list",
5440 sizeof(struct hns3_process_private),
5441 RTE_CACHE_LINE_SIZE, eth_dev->device->numa_node);
5442 if (eth_dev->process_private == NULL) {
5443 PMD_INIT_LOG(ERR, "Failed to alloc memory for process private");
5446 /* initialize flow filter lists */
5447 hns3_filterlist_init(eth_dev);
5449 hns3_set_rxtx_function(eth_dev);
5450 eth_dev->dev_ops = &hns3_eth_dev_ops;
5451 if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
5452 hns3_mp_init_secondary();
5453 hw->secondary_cnt++;
5457 hns3_mp_init_primary();
5458 hw->adapter_state = HNS3_NIC_UNINITIALIZED;
5460 hw->data = eth_dev->data;
5463 * Set default max packet size according to the mtu
5464 * default vale in DPDK frame.
5466 hns->pf.mps = hw->data->mtu + HNS3_ETH_OVERHEAD;
5468 ret = hns3_reset_init(hw);
5470 goto err_init_reset;
5471 hw->reset.ops = &hns3_reset_ops;
5473 ret = hns3_init_pf(eth_dev);
5475 PMD_INIT_LOG(ERR, "Failed to init pf: %d", ret);
5479 /* Allocate memory for storing MAC addresses */
5480 eth_dev->data->mac_addrs = rte_zmalloc("hns3-mac",
5481 sizeof(struct rte_ether_addr) *
5482 HNS3_UC_MACADDR_NUM, 0);
5483 if (eth_dev->data->mac_addrs == NULL) {
5484 PMD_INIT_LOG(ERR, "Failed to allocate %zx bytes needed "
5485 "to store MAC addresses",
5486 sizeof(struct rte_ether_addr) *
5487 HNS3_UC_MACADDR_NUM);
5489 goto err_rte_zmalloc;
5492 rte_ether_addr_copy((struct rte_ether_addr *)hw->mac.mac_addr,
5493 ð_dev->data->mac_addrs[0]);
5495 hw->adapter_state = HNS3_NIC_INITIALIZED;
5497 * Pass the information to the rte_eth_dev_close() that it should also
5498 * release the private port resources.
5500 eth_dev->data->dev_flags |= RTE_ETH_DEV_CLOSE_REMOVE;
5502 if (rte_atomic16_read(&hns->hw.reset.schedule) == SCHEDULE_PENDING) {
5503 hns3_err(hw, "Reschedule reset service after dev_init");
5504 hns3_schedule_reset(hns);
5506 /* IMP will wait ready flag before reset */
5507 hns3_notify_reset_ready(hw, false);
5510 hns3_info(hw, "hns3 dev initialization successful!");
5514 hns3_uninit_pf(eth_dev);
5517 rte_free(hw->reset.wait_data);
5519 eth_dev->dev_ops = NULL;
5520 eth_dev->rx_pkt_burst = NULL;
5521 eth_dev->tx_pkt_burst = NULL;
5522 eth_dev->tx_pkt_prepare = NULL;
5523 rte_free(eth_dev->process_private);
5524 eth_dev->process_private = NULL;
5529 hns3_dev_uninit(struct rte_eth_dev *eth_dev)
5531 struct hns3_adapter *hns = eth_dev->data->dev_private;
5532 struct hns3_hw *hw = &hns->hw;
5534 PMD_INIT_FUNC_TRACE();
5536 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
5539 eth_dev->dev_ops = NULL;
5540 eth_dev->rx_pkt_burst = NULL;
5541 eth_dev->tx_pkt_burst = NULL;
5542 eth_dev->tx_pkt_prepare = NULL;
5543 if (hw->adapter_state < HNS3_NIC_CLOSING)
5544 hns3_dev_close(eth_dev);
5546 hw->adapter_state = HNS3_NIC_REMOVED;
5551 eth_hns3_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
5552 struct rte_pci_device *pci_dev)
5554 return rte_eth_dev_pci_generic_probe(pci_dev,
5555 sizeof(struct hns3_adapter),
5560 eth_hns3_pci_remove(struct rte_pci_device *pci_dev)
5562 return rte_eth_dev_pci_generic_remove(pci_dev, hns3_dev_uninit);
5565 static const struct rte_pci_id pci_id_hns3_map[] = {
5566 { RTE_PCI_DEVICE(PCI_VENDOR_ID_HUAWEI, HNS3_DEV_ID_GE) },
5567 { RTE_PCI_DEVICE(PCI_VENDOR_ID_HUAWEI, HNS3_DEV_ID_25GE) },
5568 { RTE_PCI_DEVICE(PCI_VENDOR_ID_HUAWEI, HNS3_DEV_ID_25GE_RDMA) },
5569 { RTE_PCI_DEVICE(PCI_VENDOR_ID_HUAWEI, HNS3_DEV_ID_50GE_RDMA) },
5570 { RTE_PCI_DEVICE(PCI_VENDOR_ID_HUAWEI, HNS3_DEV_ID_100G_RDMA_MACSEC) },
5571 { .vendor_id = 0, /* sentinel */ },
5574 static struct rte_pci_driver rte_hns3_pmd = {
5575 .id_table = pci_id_hns3_map,
5576 .drv_flags = RTE_PCI_DRV_NEED_MAPPING,
5577 .probe = eth_hns3_pci_probe,
5578 .remove = eth_hns3_pci_remove,
5581 RTE_PMD_REGISTER_PCI(net_hns3, rte_hns3_pmd);
5582 RTE_PMD_REGISTER_PCI_TABLE(net_hns3, pci_id_hns3_map);
5583 RTE_PMD_REGISTER_KMOD_DEP(net_hns3, "* igb_uio | vfio-pci");
5584 RTE_LOG_REGISTER(hns3_logtype_init, pmd.net.hns3.init, NOTICE);
5585 RTE_LOG_REGISTER(hns3_logtype_driver, pmd.net.hns3.driver, NOTICE);