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
59 #define HNS3_VECTOR0_IMP_CMDQ_ERR_B 4U
60 #define HNS3_VECTOR0_IMP_RD_POISON_B 5U
61 #define HNS3_VECTOR0_ALL_MSIX_ERR_B 6U
63 #define HNS3_RESET_WAIT_MS 100
64 #define HNS3_RESET_WAIT_CNT 200
67 HNS3_VECTOR0_EVENT_RST,
68 HNS3_VECTOR0_EVENT_MBX,
69 HNS3_VECTOR0_EVENT_ERR,
70 HNS3_VECTOR0_EVENT_OTHER,
73 static enum hns3_reset_level hns3_get_reset_level(struct hns3_adapter *hns,
75 static int hns3_dev_mtu_set(struct rte_eth_dev *dev, uint16_t mtu);
76 static int hns3_vlan_pvid_configure(struct hns3_adapter *hns, uint16_t pvid,
78 static int hns3_update_speed_duplex(struct rte_eth_dev *eth_dev);
80 static int hns3_add_mc_addr(struct hns3_hw *hw,
81 struct rte_ether_addr *mac_addr);
82 static int hns3_remove_mc_addr(struct hns3_hw *hw,
83 struct rte_ether_addr *mac_addr);
86 hns3_pf_disable_irq0(struct hns3_hw *hw)
88 hns3_write_dev(hw, HNS3_MISC_VECTOR_REG_BASE, 0);
92 hns3_pf_enable_irq0(struct hns3_hw *hw)
94 hns3_write_dev(hw, HNS3_MISC_VECTOR_REG_BASE, 1);
97 static enum hns3_evt_cause
98 hns3_check_event_cause(struct hns3_adapter *hns, uint32_t *clearval)
100 struct hns3_hw *hw = &hns->hw;
101 uint32_t vector0_int_stats;
102 uint32_t cmdq_src_val;
103 uint32_t hw_err_src_reg;
105 enum hns3_evt_cause ret;
107 /* fetch the events from their corresponding regs */
108 vector0_int_stats = hns3_read_dev(hw, HNS3_VECTOR0_OTHER_INT_STS_REG);
109 cmdq_src_val = hns3_read_dev(hw, HNS3_VECTOR0_CMDQ_SRC_REG);
110 hw_err_src_reg = hns3_read_dev(hw, HNS3_RAS_PF_OTHER_INT_STS_REG);
113 * Assumption: If by any chance reset and mailbox events are reported
114 * together then we will only process reset event and defer the
115 * processing of the mailbox events. Since, we would have not cleared
116 * RX CMDQ event this time we would receive again another interrupt
117 * from H/W just for the mailbox.
119 if (BIT(HNS3_VECTOR0_IMPRESET_INT_B) & vector0_int_stats) { /* IMP */
120 rte_atomic16_set(&hw->reset.disable_cmd, 1);
121 hns3_atomic_set_bit(HNS3_IMP_RESET, &hw->reset.pending);
122 val = BIT(HNS3_VECTOR0_IMPRESET_INT_B);
124 hw->reset.stats.imp_cnt++;
125 hns3_warn(hw, "IMP reset detected, clear reset status");
127 hns3_schedule_delayed_reset(hns);
128 hns3_warn(hw, "IMP reset detected, don't clear reset status");
131 ret = HNS3_VECTOR0_EVENT_RST;
136 if (BIT(HNS3_VECTOR0_GLOBALRESET_INT_B) & vector0_int_stats) {
137 rte_atomic16_set(&hw->reset.disable_cmd, 1);
138 hns3_atomic_set_bit(HNS3_GLOBAL_RESET, &hw->reset.pending);
139 val = BIT(HNS3_VECTOR0_GLOBALRESET_INT_B);
141 hw->reset.stats.global_cnt++;
142 hns3_warn(hw, "Global reset detected, clear reset status");
144 hns3_schedule_delayed_reset(hns);
145 hns3_warn(hw, "Global reset detected, don't clear reset status");
148 ret = HNS3_VECTOR0_EVENT_RST;
152 /* check for vector0 msix event source */
153 if (vector0_int_stats & HNS3_VECTOR0_REG_MSIX_MASK ||
154 hw_err_src_reg & HNS3_RAS_REG_NFE_MASK) {
155 val = vector0_int_stats | hw_err_src_reg;
156 ret = HNS3_VECTOR0_EVENT_ERR;
160 /* check for vector0 mailbox(=CMDQ RX) event source */
161 if (BIT(HNS3_VECTOR0_RX_CMDQ_INT_B) & cmdq_src_val) {
162 cmdq_src_val &= ~BIT(HNS3_VECTOR0_RX_CMDQ_INT_B);
164 ret = HNS3_VECTOR0_EVENT_MBX;
168 if (clearval && (vector0_int_stats || cmdq_src_val || hw_err_src_reg))
169 hns3_warn(hw, "vector0_int_stats:0x%x cmdq_src_val:0x%x hw_err_src_reg:0x%x",
170 vector0_int_stats, cmdq_src_val, hw_err_src_reg);
171 val = vector0_int_stats;
172 ret = HNS3_VECTOR0_EVENT_OTHER;
181 hns3_clear_event_cause(struct hns3_hw *hw, uint32_t event_type, uint32_t regclr)
183 if (event_type == HNS3_VECTOR0_EVENT_RST)
184 hns3_write_dev(hw, HNS3_MISC_RESET_STS_REG, regclr);
185 else if (event_type == HNS3_VECTOR0_EVENT_MBX)
186 hns3_write_dev(hw, HNS3_VECTOR0_CMDQ_SRC_REG, regclr);
190 hns3_clear_all_event_cause(struct hns3_hw *hw)
192 uint32_t vector0_int_stats;
193 vector0_int_stats = hns3_read_dev(hw, HNS3_VECTOR0_OTHER_INT_STS_REG);
195 if (BIT(HNS3_VECTOR0_IMPRESET_INT_B) & vector0_int_stats)
196 hns3_warn(hw, "Probe during IMP reset interrupt");
198 if (BIT(HNS3_VECTOR0_GLOBALRESET_INT_B) & vector0_int_stats)
199 hns3_warn(hw, "Probe during Global reset interrupt");
201 hns3_clear_event_cause(hw, HNS3_VECTOR0_EVENT_RST,
202 BIT(HNS3_VECTOR0_IMPRESET_INT_B) |
203 BIT(HNS3_VECTOR0_GLOBALRESET_INT_B) |
204 BIT(HNS3_VECTOR0_CORERESET_INT_B));
205 hns3_clear_event_cause(hw, HNS3_VECTOR0_EVENT_MBX, 0);
209 hns3_interrupt_handler(void *param)
211 struct rte_eth_dev *dev = (struct rte_eth_dev *)param;
212 struct hns3_adapter *hns = dev->data->dev_private;
213 struct hns3_hw *hw = &hns->hw;
214 enum hns3_evt_cause event_cause;
215 uint32_t clearval = 0;
217 /* Disable interrupt */
218 hns3_pf_disable_irq0(hw);
220 event_cause = hns3_check_event_cause(hns, &clearval);
222 /* vector 0 interrupt is shared with reset and mailbox source events. */
223 if (event_cause == HNS3_VECTOR0_EVENT_ERR) {
224 hns3_warn(hw, "Received err interrupt");
225 hns3_handle_msix_error(hns, &hw->reset.request);
226 hns3_handle_ras_error(hns, &hw->reset.request);
227 hns3_schedule_reset(hns);
228 } else if (event_cause == HNS3_VECTOR0_EVENT_RST) {
229 hns3_warn(hw, "Received reset interrupt");
230 hns3_schedule_reset(hns);
231 } else if (event_cause == HNS3_VECTOR0_EVENT_MBX)
232 hns3_dev_handle_mbx_msg(hw);
234 hns3_err(hw, "Received unknown event");
236 hns3_clear_event_cause(hw, event_cause, clearval);
237 /* Enable interrupt if it is not cause by reset */
238 hns3_pf_enable_irq0(hw);
242 hns3_set_port_vlan_filter(struct hns3_adapter *hns, uint16_t vlan_id, int on)
244 #define HNS3_VLAN_ID_OFFSET_STEP 160
245 #define HNS3_VLAN_BYTE_SIZE 8
246 struct hns3_vlan_filter_pf_cfg_cmd *req;
247 struct hns3_hw *hw = &hns->hw;
248 uint8_t vlan_offset_byte_val;
249 struct hns3_cmd_desc desc;
250 uint8_t vlan_offset_byte;
251 uint8_t vlan_offset_base;
254 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_VLAN_FILTER_PF_CFG, false);
256 vlan_offset_base = vlan_id / HNS3_VLAN_ID_OFFSET_STEP;
257 vlan_offset_byte = (vlan_id % HNS3_VLAN_ID_OFFSET_STEP) /
259 vlan_offset_byte_val = 1 << (vlan_id % HNS3_VLAN_BYTE_SIZE);
261 req = (struct hns3_vlan_filter_pf_cfg_cmd *)desc.data;
262 req->vlan_offset = vlan_offset_base;
263 req->vlan_cfg = on ? 0 : 1;
264 req->vlan_offset_bitmap[vlan_offset_byte] = vlan_offset_byte_val;
266 ret = hns3_cmd_send(hw, &desc, 1);
268 hns3_err(hw, "set port vlan id failed, vlan_id =%u, ret =%d",
275 hns3_rm_dev_vlan_table(struct hns3_adapter *hns, uint16_t vlan_id)
277 struct hns3_user_vlan_table *vlan_entry;
278 struct hns3_pf *pf = &hns->pf;
280 LIST_FOREACH(vlan_entry, &pf->vlan_list, next) {
281 if (vlan_entry->vlan_id == vlan_id) {
282 if (vlan_entry->hd_tbl_status)
283 hns3_set_port_vlan_filter(hns, vlan_id, 0);
284 LIST_REMOVE(vlan_entry, next);
285 rte_free(vlan_entry);
292 hns3_add_dev_vlan_table(struct hns3_adapter *hns, uint16_t vlan_id,
295 struct hns3_user_vlan_table *vlan_entry;
296 struct hns3_hw *hw = &hns->hw;
297 struct hns3_pf *pf = &hns->pf;
299 LIST_FOREACH(vlan_entry, &pf->vlan_list, next) {
300 if (vlan_entry->vlan_id == vlan_id)
304 vlan_entry = rte_zmalloc("hns3_vlan_tbl", sizeof(*vlan_entry), 0);
305 if (vlan_entry == NULL) {
306 hns3_err(hw, "Failed to malloc hns3 vlan table");
310 vlan_entry->hd_tbl_status = writen_to_tbl;
311 vlan_entry->vlan_id = vlan_id;
313 LIST_INSERT_HEAD(&pf->vlan_list, vlan_entry, next);
317 hns3_restore_vlan_table(struct hns3_adapter *hns)
319 struct hns3_user_vlan_table *vlan_entry;
320 struct hns3_hw *hw = &hns->hw;
321 struct hns3_pf *pf = &hns->pf;
325 if (hw->port_base_vlan_cfg.state == HNS3_PORT_BASE_VLAN_ENABLE)
326 return hns3_vlan_pvid_configure(hns,
327 hw->port_base_vlan_cfg.pvid, 1);
329 LIST_FOREACH(vlan_entry, &pf->vlan_list, next) {
330 if (vlan_entry->hd_tbl_status) {
331 vlan_id = vlan_entry->vlan_id;
332 ret = hns3_set_port_vlan_filter(hns, vlan_id, 1);
342 hns3_vlan_filter_configure(struct hns3_adapter *hns, uint16_t vlan_id, int on)
344 struct hns3_hw *hw = &hns->hw;
345 bool writen_to_tbl = false;
349 * When vlan filter is enabled, hardware regards vlan id 0 as the entry
350 * for normal packet, deleting vlan id 0 is not allowed.
352 if (on == 0 && vlan_id == 0)
356 * When port base vlan enabled, we use port base vlan as the vlan
357 * filter condition. In this case, we don't update vlan filter table
358 * when user add new vlan or remove exist vlan, just update the
359 * vlan list. The vlan id in vlan list will be writen in vlan filter
360 * table until port base vlan disabled
362 if (hw->port_base_vlan_cfg.state == HNS3_PORT_BASE_VLAN_DISABLE) {
363 ret = hns3_set_port_vlan_filter(hns, vlan_id, on);
364 writen_to_tbl = true;
367 if (ret == 0 && vlan_id) {
369 hns3_add_dev_vlan_table(hns, vlan_id, writen_to_tbl);
371 hns3_rm_dev_vlan_table(hns, vlan_id);
377 hns3_vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on)
379 struct hns3_adapter *hns = dev->data->dev_private;
380 struct hns3_hw *hw = &hns->hw;
383 rte_spinlock_lock(&hw->lock);
384 ret = hns3_vlan_filter_configure(hns, vlan_id, on);
385 rte_spinlock_unlock(&hw->lock);
390 hns3_vlan_tpid_configure(struct hns3_adapter *hns, enum rte_vlan_type vlan_type,
393 struct hns3_rx_vlan_type_cfg_cmd *rx_req;
394 struct hns3_tx_vlan_type_cfg_cmd *tx_req;
395 struct hns3_hw *hw = &hns->hw;
396 struct hns3_cmd_desc desc;
399 if ((vlan_type != ETH_VLAN_TYPE_INNER &&
400 vlan_type != ETH_VLAN_TYPE_OUTER)) {
401 hns3_err(hw, "Unsupported vlan type, vlan_type =%d", vlan_type);
405 if (tpid != RTE_ETHER_TYPE_VLAN) {
406 hns3_err(hw, "Unsupported vlan tpid, vlan_type =%d", vlan_type);
410 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_MAC_VLAN_TYPE_ID, false);
411 rx_req = (struct hns3_rx_vlan_type_cfg_cmd *)desc.data;
413 if (vlan_type == ETH_VLAN_TYPE_OUTER) {
414 rx_req->ot_fst_vlan_type = rte_cpu_to_le_16(tpid);
415 rx_req->ot_sec_vlan_type = rte_cpu_to_le_16(tpid);
416 } else if (vlan_type == ETH_VLAN_TYPE_INNER) {
417 rx_req->ot_fst_vlan_type = rte_cpu_to_le_16(tpid);
418 rx_req->ot_sec_vlan_type = rte_cpu_to_le_16(tpid);
419 rx_req->in_fst_vlan_type = rte_cpu_to_le_16(tpid);
420 rx_req->in_sec_vlan_type = rte_cpu_to_le_16(tpid);
423 ret = hns3_cmd_send(hw, &desc, 1);
425 hns3_err(hw, "Send rxvlan protocol type command fail, ret =%d",
430 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_MAC_VLAN_INSERT, false);
432 tx_req = (struct hns3_tx_vlan_type_cfg_cmd *)desc.data;
433 tx_req->ot_vlan_type = rte_cpu_to_le_16(tpid);
434 tx_req->in_vlan_type = rte_cpu_to_le_16(tpid);
436 ret = hns3_cmd_send(hw, &desc, 1);
438 hns3_err(hw, "Send txvlan protocol type command fail, ret =%d",
444 hns3_vlan_tpid_set(struct rte_eth_dev *dev, enum rte_vlan_type vlan_type,
447 struct hns3_adapter *hns = dev->data->dev_private;
448 struct hns3_hw *hw = &hns->hw;
451 rte_spinlock_lock(&hw->lock);
452 ret = hns3_vlan_tpid_configure(hns, vlan_type, tpid);
453 rte_spinlock_unlock(&hw->lock);
458 hns3_set_vlan_rx_offload_cfg(struct hns3_adapter *hns,
459 struct hns3_rx_vtag_cfg *vcfg)
461 struct hns3_vport_vtag_rx_cfg_cmd *req;
462 struct hns3_hw *hw = &hns->hw;
463 struct hns3_cmd_desc desc;
468 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_VLAN_PORT_RX_CFG, false);
470 req = (struct hns3_vport_vtag_rx_cfg_cmd *)desc.data;
471 hns3_set_bit(req->vport_vlan_cfg, HNS3_REM_TAG1_EN_B,
472 vcfg->strip_tag1_en ? 1 : 0);
473 hns3_set_bit(req->vport_vlan_cfg, HNS3_REM_TAG2_EN_B,
474 vcfg->strip_tag2_en ? 1 : 0);
475 hns3_set_bit(req->vport_vlan_cfg, HNS3_SHOW_TAG1_EN_B,
476 vcfg->vlan1_vlan_prionly ? 1 : 0);
477 hns3_set_bit(req->vport_vlan_cfg, HNS3_SHOW_TAG2_EN_B,
478 vcfg->vlan2_vlan_prionly ? 1 : 0);
481 * In current version VF is not supported when PF is driven by DPDK
482 * driver, just need to configure parameters for PF vport.
484 vport_id = HNS3_PF_FUNC_ID;
485 req->vf_offset = vport_id / HNS3_VF_NUM_PER_CMD;
486 bitmap = 1 << (vport_id % HNS3_VF_NUM_PER_BYTE);
487 req->vf_bitmap[req->vf_offset] = bitmap;
489 ret = hns3_cmd_send(hw, &desc, 1);
491 hns3_err(hw, "Send port rxvlan cfg command fail, ret =%d", ret);
496 hns3_update_rx_offload_cfg(struct hns3_adapter *hns,
497 struct hns3_rx_vtag_cfg *vcfg)
499 struct hns3_pf *pf = &hns->pf;
500 memcpy(&pf->vtag_config.rx_vcfg, vcfg, sizeof(pf->vtag_config.rx_vcfg));
504 hns3_update_tx_offload_cfg(struct hns3_adapter *hns,
505 struct hns3_tx_vtag_cfg *vcfg)
507 struct hns3_pf *pf = &hns->pf;
508 memcpy(&pf->vtag_config.tx_vcfg, vcfg, sizeof(pf->vtag_config.tx_vcfg));
512 hns3_en_hw_strip_rxvtag(struct hns3_adapter *hns, bool enable)
514 struct hns3_rx_vtag_cfg rxvlan_cfg;
515 struct hns3_hw *hw = &hns->hw;
518 if (hw->port_base_vlan_cfg.state == HNS3_PORT_BASE_VLAN_DISABLE) {
519 rxvlan_cfg.strip_tag1_en = false;
520 rxvlan_cfg.strip_tag2_en = enable;
522 rxvlan_cfg.strip_tag1_en = enable;
523 rxvlan_cfg.strip_tag2_en = true;
526 rxvlan_cfg.vlan1_vlan_prionly = false;
527 rxvlan_cfg.vlan2_vlan_prionly = false;
528 rxvlan_cfg.rx_vlan_offload_en = enable;
530 ret = hns3_set_vlan_rx_offload_cfg(hns, &rxvlan_cfg);
532 hns3_err(hw, "enable strip rx vtag failed, ret =%d", ret);
536 hns3_update_rx_offload_cfg(hns, &rxvlan_cfg);
542 hns3_set_vlan_filter_ctrl(struct hns3_hw *hw, uint8_t vlan_type,
543 uint8_t fe_type, bool filter_en, uint8_t vf_id)
545 struct hns3_vlan_filter_ctrl_cmd *req;
546 struct hns3_cmd_desc desc;
549 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_VLAN_FILTER_CTRL, false);
551 req = (struct hns3_vlan_filter_ctrl_cmd *)desc.data;
552 req->vlan_type = vlan_type;
553 req->vlan_fe = filter_en ? fe_type : 0;
556 ret = hns3_cmd_send(hw, &desc, 1);
558 hns3_err(hw, "set vlan filter fail, ret =%d", ret);
564 hns3_vlan_filter_init(struct hns3_adapter *hns)
566 struct hns3_hw *hw = &hns->hw;
569 ret = hns3_set_vlan_filter_ctrl(hw, HNS3_FILTER_TYPE_VF,
570 HNS3_FILTER_FE_EGRESS, false,
573 hns3_err(hw, "failed to init vf vlan filter, ret = %d", ret);
577 ret = hns3_set_vlan_filter_ctrl(hw, HNS3_FILTER_TYPE_PORT,
578 HNS3_FILTER_FE_INGRESS, false,
581 hns3_err(hw, "failed to init port vlan filter, ret = %d", ret);
587 hns3_enable_vlan_filter(struct hns3_adapter *hns, bool enable)
589 struct hns3_hw *hw = &hns->hw;
592 ret = hns3_set_vlan_filter_ctrl(hw, HNS3_FILTER_TYPE_PORT,
593 HNS3_FILTER_FE_INGRESS, enable,
596 hns3_err(hw, "failed to %s port vlan filter, ret = %d",
597 enable ? "enable" : "disable", ret);
603 hns3_vlan_offload_set(struct rte_eth_dev *dev, int mask)
605 struct hns3_adapter *hns = dev->data->dev_private;
606 struct hns3_hw *hw = &hns->hw;
607 struct rte_eth_rxmode *rxmode;
608 unsigned int tmp_mask;
612 rte_spinlock_lock(&hw->lock);
613 rxmode = &dev->data->dev_conf.rxmode;
614 tmp_mask = (unsigned int)mask;
615 if (tmp_mask & ETH_VLAN_FILTER_MASK) {
616 /* ignore vlan filter configuration during promiscuous mode */
617 if (!dev->data->promiscuous) {
618 /* Enable or disable VLAN filter */
619 enable = rxmode->offloads & DEV_RX_OFFLOAD_VLAN_FILTER ?
622 ret = hns3_enable_vlan_filter(hns, enable);
624 rte_spinlock_unlock(&hw->lock);
625 hns3_err(hw, "failed to %s rx filter, ret = %d",
626 enable ? "enable" : "disable", ret);
632 if (tmp_mask & ETH_VLAN_STRIP_MASK) {
633 /* Enable or disable VLAN stripping */
634 enable = rxmode->offloads & DEV_RX_OFFLOAD_VLAN_STRIP ?
637 ret = hns3_en_hw_strip_rxvtag(hns, enable);
639 rte_spinlock_unlock(&hw->lock);
640 hns3_err(hw, "failed to %s rx strip, ret = %d",
641 enable ? "enable" : "disable", ret);
646 rte_spinlock_unlock(&hw->lock);
652 hns3_set_vlan_tx_offload_cfg(struct hns3_adapter *hns,
653 struct hns3_tx_vtag_cfg *vcfg)
655 struct hns3_vport_vtag_tx_cfg_cmd *req;
656 struct hns3_cmd_desc desc;
657 struct hns3_hw *hw = &hns->hw;
662 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_VLAN_PORT_TX_CFG, false);
664 req = (struct hns3_vport_vtag_tx_cfg_cmd *)desc.data;
665 req->def_vlan_tag1 = vcfg->default_tag1;
666 req->def_vlan_tag2 = vcfg->default_tag2;
667 hns3_set_bit(req->vport_vlan_cfg, HNS3_ACCEPT_TAG1_B,
668 vcfg->accept_tag1 ? 1 : 0);
669 hns3_set_bit(req->vport_vlan_cfg, HNS3_ACCEPT_UNTAG1_B,
670 vcfg->accept_untag1 ? 1 : 0);
671 hns3_set_bit(req->vport_vlan_cfg, HNS3_ACCEPT_TAG2_B,
672 vcfg->accept_tag2 ? 1 : 0);
673 hns3_set_bit(req->vport_vlan_cfg, HNS3_ACCEPT_UNTAG2_B,
674 vcfg->accept_untag2 ? 1 : 0);
675 hns3_set_bit(req->vport_vlan_cfg, HNS3_PORT_INS_TAG1_EN_B,
676 vcfg->insert_tag1_en ? 1 : 0);
677 hns3_set_bit(req->vport_vlan_cfg, HNS3_PORT_INS_TAG2_EN_B,
678 vcfg->insert_tag2_en ? 1 : 0);
679 hns3_set_bit(req->vport_vlan_cfg, HNS3_CFG_NIC_ROCE_SEL_B, 0);
682 * In current version VF is not supported when PF is driven by DPDK
683 * driver, just need to configure parameters for PF vport.
685 vport_id = HNS3_PF_FUNC_ID;
686 req->vf_offset = vport_id / HNS3_VF_NUM_PER_CMD;
687 bitmap = 1 << (vport_id % HNS3_VF_NUM_PER_BYTE);
688 req->vf_bitmap[req->vf_offset] = bitmap;
690 ret = hns3_cmd_send(hw, &desc, 1);
692 hns3_err(hw, "Send port txvlan cfg command fail, ret =%d", ret);
698 hns3_vlan_txvlan_cfg(struct hns3_adapter *hns, uint16_t port_base_vlan_state,
701 struct hns3_hw *hw = &hns->hw;
702 struct hns3_tx_vtag_cfg txvlan_cfg;
705 if (port_base_vlan_state == HNS3_PORT_BASE_VLAN_DISABLE) {
706 txvlan_cfg.accept_tag1 = true;
707 txvlan_cfg.insert_tag1_en = false;
708 txvlan_cfg.default_tag1 = 0;
710 txvlan_cfg.accept_tag1 = false;
711 txvlan_cfg.insert_tag1_en = true;
712 txvlan_cfg.default_tag1 = pvid;
715 txvlan_cfg.accept_untag1 = true;
716 txvlan_cfg.accept_tag2 = true;
717 txvlan_cfg.accept_untag2 = true;
718 txvlan_cfg.insert_tag2_en = false;
719 txvlan_cfg.default_tag2 = 0;
721 ret = hns3_set_vlan_tx_offload_cfg(hns, &txvlan_cfg);
723 hns3_err(hw, "pf vlan set pvid failed, pvid =%u ,ret =%d", pvid,
728 hns3_update_tx_offload_cfg(hns, &txvlan_cfg);
733 hns3_store_port_base_vlan_info(struct hns3_adapter *hns, uint16_t pvid, int on)
735 struct hns3_hw *hw = &hns->hw;
737 hw->port_base_vlan_cfg.state = on ?
738 HNS3_PORT_BASE_VLAN_ENABLE : HNS3_PORT_BASE_VLAN_DISABLE;
740 hw->port_base_vlan_cfg.pvid = pvid;
744 hns3_rm_all_vlan_table(struct hns3_adapter *hns, bool is_del_list)
746 struct hns3_user_vlan_table *vlan_entry;
747 struct hns3_pf *pf = &hns->pf;
749 LIST_FOREACH(vlan_entry, &pf->vlan_list, next) {
750 if (vlan_entry->hd_tbl_status)
751 hns3_set_port_vlan_filter(hns, vlan_entry->vlan_id, 0);
753 vlan_entry->hd_tbl_status = false;
757 vlan_entry = LIST_FIRST(&pf->vlan_list);
759 LIST_REMOVE(vlan_entry, next);
760 rte_free(vlan_entry);
761 vlan_entry = LIST_FIRST(&pf->vlan_list);
767 hns3_add_all_vlan_table(struct hns3_adapter *hns)
769 struct hns3_user_vlan_table *vlan_entry;
770 struct hns3_pf *pf = &hns->pf;
772 LIST_FOREACH(vlan_entry, &pf->vlan_list, next) {
773 if (!vlan_entry->hd_tbl_status)
774 hns3_set_port_vlan_filter(hns, vlan_entry->vlan_id, 1);
776 vlan_entry->hd_tbl_status = true;
781 hns3_remove_all_vlan_table(struct hns3_adapter *hns)
783 struct hns3_hw *hw = &hns->hw;
786 hns3_rm_all_vlan_table(hns, true);
787 if (hw->port_base_vlan_cfg.pvid != HNS3_INVLID_PVID) {
788 ret = hns3_set_port_vlan_filter(hns,
789 hw->port_base_vlan_cfg.pvid, 0);
791 hns3_err(hw, "Failed to remove all vlan table, ret =%d",
799 hns3_update_vlan_filter_entries(struct hns3_adapter *hns,
800 uint16_t port_base_vlan_state,
801 uint16_t new_pvid, uint16_t old_pvid)
803 struct hns3_hw *hw = &hns->hw;
806 if (port_base_vlan_state == HNS3_PORT_BASE_VLAN_ENABLE) {
807 if (old_pvid != HNS3_INVLID_PVID && old_pvid != 0) {
808 ret = hns3_set_port_vlan_filter(hns, old_pvid, 0);
811 "Failed to clear clear old pvid filter, ret =%d",
817 hns3_rm_all_vlan_table(hns, false);
818 return hns3_set_port_vlan_filter(hns, new_pvid, 1);
822 ret = hns3_set_port_vlan_filter(hns, new_pvid, 0);
824 hns3_err(hw, "Failed to set port vlan filter, ret =%d",
830 if (new_pvid == hw->port_base_vlan_cfg.pvid)
831 hns3_add_all_vlan_table(hns);
837 hns3_en_pvid_strip(struct hns3_adapter *hns, int on)
839 struct hns3_rx_vtag_cfg *old_cfg = &hns->pf.vtag_config.rx_vcfg;
840 struct hns3_rx_vtag_cfg rx_vlan_cfg;
844 rx_strip_en = old_cfg->rx_vlan_offload_en ? true : false;
846 rx_vlan_cfg.strip_tag1_en = rx_strip_en;
847 rx_vlan_cfg.strip_tag2_en = true;
849 rx_vlan_cfg.strip_tag1_en = false;
850 rx_vlan_cfg.strip_tag2_en = rx_strip_en;
852 rx_vlan_cfg.vlan1_vlan_prionly = false;
853 rx_vlan_cfg.vlan2_vlan_prionly = false;
854 rx_vlan_cfg.rx_vlan_offload_en = old_cfg->rx_vlan_offload_en;
856 ret = hns3_set_vlan_rx_offload_cfg(hns, &rx_vlan_cfg);
860 hns3_update_rx_offload_cfg(hns, &rx_vlan_cfg);
865 hns3_vlan_pvid_configure(struct hns3_adapter *hns, uint16_t pvid, int on)
867 struct hns3_hw *hw = &hns->hw;
868 uint16_t port_base_vlan_state;
872 if (on == 0 && pvid != hw->port_base_vlan_cfg.pvid) {
873 if (hw->port_base_vlan_cfg.pvid != HNS3_INVLID_PVID)
874 hns3_warn(hw, "Invalid operation! As current pvid set "
875 "is %u, disable pvid %u is invalid",
876 hw->port_base_vlan_cfg.pvid, pvid);
880 port_base_vlan_state = on ? HNS3_PORT_BASE_VLAN_ENABLE :
881 HNS3_PORT_BASE_VLAN_DISABLE;
882 ret = hns3_vlan_txvlan_cfg(hns, port_base_vlan_state, pvid);
884 hns3_err(hw, "failed to config tx vlan for pvid, ret = %d",
889 ret = hns3_en_pvid_strip(hns, on);
891 hns3_err(hw, "failed to config rx vlan strip for pvid, "
896 if (pvid == HNS3_INVLID_PVID)
898 old_pvid = hw->port_base_vlan_cfg.pvid;
899 ret = hns3_update_vlan_filter_entries(hns, port_base_vlan_state, pvid,
902 hns3_err(hw, "Failed to update vlan filter entries, ret =%d",
908 hns3_store_port_base_vlan_info(hns, pvid, on);
913 hns3_vlan_pvid_set(struct rte_eth_dev *dev, uint16_t pvid, int on)
915 struct hns3_adapter *hns = dev->data->dev_private;
916 struct hns3_hw *hw = &hns->hw;
917 bool pvid_en_state_change;
921 if (pvid > RTE_ETHER_MAX_VLAN_ID) {
922 hns3_err(hw, "Invalid vlan_id = %u > %d", pvid,
923 RTE_ETHER_MAX_VLAN_ID);
928 * If PVID configuration state change, should refresh the PVID
929 * configuration state in struct hns3_tx_queue/hns3_rx_queue.
931 pvid_state = hw->port_base_vlan_cfg.state;
932 if ((on && pvid_state == HNS3_PORT_BASE_VLAN_ENABLE) ||
933 (!on && pvid_state == HNS3_PORT_BASE_VLAN_DISABLE))
934 pvid_en_state_change = false;
936 pvid_en_state_change = true;
938 rte_spinlock_lock(&hw->lock);
939 ret = hns3_vlan_pvid_configure(hns, pvid, on);
940 rte_spinlock_unlock(&hw->lock);
944 if (pvid_en_state_change)
945 hns3_update_all_queues_pvid_state(hw);
951 init_port_base_vlan_info(struct hns3_hw *hw)
953 hw->port_base_vlan_cfg.state = HNS3_PORT_BASE_VLAN_DISABLE;
954 hw->port_base_vlan_cfg.pvid = HNS3_INVLID_PVID;
958 hns3_default_vlan_config(struct hns3_adapter *hns)
960 struct hns3_hw *hw = &hns->hw;
963 ret = hns3_set_port_vlan_filter(hns, 0, 1);
965 hns3_err(hw, "default vlan 0 config failed, ret =%d", ret);
970 hns3_init_vlan_config(struct hns3_adapter *hns)
972 struct hns3_hw *hw = &hns->hw;
976 * This function can be called in the initialization and reset process,
977 * when in reset process, it means that hardware had been reseted
978 * successfully and we need to restore the hardware configuration to
979 * ensure that the hardware configuration remains unchanged before and
982 if (rte_atomic16_read(&hw->reset.resetting) == 0)
983 init_port_base_vlan_info(hw);
985 ret = hns3_vlan_filter_init(hns);
987 hns3_err(hw, "vlan init fail in pf, ret =%d", ret);
991 ret = hns3_vlan_tpid_configure(hns, ETH_VLAN_TYPE_INNER,
992 RTE_ETHER_TYPE_VLAN);
994 hns3_err(hw, "tpid set fail in pf, ret =%d", ret);
999 * When in the reinit dev stage of the reset process, the following
1000 * vlan-related configurations may differ from those at initialization,
1001 * we will restore configurations to hardware in hns3_restore_vlan_table
1002 * and hns3_restore_vlan_conf later.
1004 if (rte_atomic16_read(&hw->reset.resetting) == 0) {
1005 ret = hns3_vlan_pvid_configure(hns, HNS3_INVLID_PVID, 0);
1007 hns3_err(hw, "pvid set fail in pf, ret =%d", ret);
1011 ret = hns3_en_hw_strip_rxvtag(hns, false);
1013 hns3_err(hw, "rx strip configure fail in pf, ret =%d",
1019 return hns3_default_vlan_config(hns);
1023 hns3_restore_vlan_conf(struct hns3_adapter *hns)
1025 struct hns3_pf *pf = &hns->pf;
1026 struct hns3_hw *hw = &hns->hw;
1031 if (!hw->data->promiscuous) {
1032 /* restore vlan filter states */
1033 offloads = hw->data->dev_conf.rxmode.offloads;
1034 enable = offloads & DEV_RX_OFFLOAD_VLAN_FILTER ? true : false;
1035 ret = hns3_enable_vlan_filter(hns, enable);
1037 hns3_err(hw, "failed to restore vlan rx filter conf, "
1043 ret = hns3_set_vlan_rx_offload_cfg(hns, &pf->vtag_config.rx_vcfg);
1045 hns3_err(hw, "failed to restore vlan rx conf, ret = %d", ret);
1049 ret = hns3_set_vlan_tx_offload_cfg(hns, &pf->vtag_config.tx_vcfg);
1051 hns3_err(hw, "failed to restore vlan tx conf, ret = %d", ret);
1057 hns3_dev_configure_vlan(struct rte_eth_dev *dev)
1059 struct hns3_adapter *hns = dev->data->dev_private;
1060 struct rte_eth_dev_data *data = dev->data;
1061 struct rte_eth_txmode *txmode;
1062 struct hns3_hw *hw = &hns->hw;
1066 txmode = &data->dev_conf.txmode;
1067 if (txmode->hw_vlan_reject_tagged || txmode->hw_vlan_reject_untagged)
1069 "hw_vlan_reject_tagged or hw_vlan_reject_untagged "
1070 "configuration is not supported! Ignore these two "
1071 "parameters: hw_vlan_reject_tagged(%d), "
1072 "hw_vlan_reject_untagged(%d)",
1073 txmode->hw_vlan_reject_tagged,
1074 txmode->hw_vlan_reject_untagged);
1076 /* Apply vlan offload setting */
1077 mask = ETH_VLAN_STRIP_MASK | ETH_VLAN_FILTER_MASK;
1078 ret = hns3_vlan_offload_set(dev, mask);
1080 hns3_err(hw, "dev config rx vlan offload failed, ret = %d",
1086 * If pvid config is not set in rte_eth_conf, driver needn't to set
1087 * VLAN pvid related configuration to hardware.
1089 if (txmode->pvid == 0 && txmode->hw_vlan_insert_pvid == 0)
1092 /* Apply pvid setting */
1093 ret = hns3_vlan_pvid_set(dev, txmode->pvid,
1094 txmode->hw_vlan_insert_pvid);
1096 hns3_err(hw, "dev config vlan pvid(%d) failed, ret = %d",
1103 hns3_config_tso(struct hns3_hw *hw, unsigned int tso_mss_min,
1104 unsigned int tso_mss_max)
1106 struct hns3_cfg_tso_status_cmd *req;
1107 struct hns3_cmd_desc desc;
1110 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_TSO_GENERIC_CONFIG, false);
1112 req = (struct hns3_cfg_tso_status_cmd *)desc.data;
1115 hns3_set_field(tso_mss, HNS3_TSO_MSS_MIN_M, HNS3_TSO_MSS_MIN_S,
1117 req->tso_mss_min = rte_cpu_to_le_16(tso_mss);
1120 hns3_set_field(tso_mss, HNS3_TSO_MSS_MIN_M, HNS3_TSO_MSS_MIN_S,
1122 req->tso_mss_max = rte_cpu_to_le_16(tso_mss);
1124 return hns3_cmd_send(hw, &desc, 1);
1128 hns3_set_umv_space(struct hns3_hw *hw, uint16_t space_size,
1129 uint16_t *allocated_size, bool is_alloc)
1131 struct hns3_umv_spc_alc_cmd *req;
1132 struct hns3_cmd_desc desc;
1135 req = (struct hns3_umv_spc_alc_cmd *)desc.data;
1136 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_MAC_VLAN_ALLOCATE, false);
1137 hns3_set_bit(req->allocate, HNS3_UMV_SPC_ALC_B, is_alloc ? 0 : 1);
1138 req->space_size = rte_cpu_to_le_32(space_size);
1140 ret = hns3_cmd_send(hw, &desc, 1);
1142 PMD_INIT_LOG(ERR, "%s umv space failed for cmd_send, ret =%d",
1143 is_alloc ? "allocate" : "free", ret);
1147 if (is_alloc && allocated_size)
1148 *allocated_size = rte_le_to_cpu_32(desc.data[1]);
1154 hns3_init_umv_space(struct hns3_hw *hw)
1156 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
1157 struct hns3_pf *pf = &hns->pf;
1158 uint16_t allocated_size = 0;
1161 ret = hns3_set_umv_space(hw, pf->wanted_umv_size, &allocated_size,
1166 if (allocated_size < pf->wanted_umv_size)
1167 PMD_INIT_LOG(WARNING, "Alloc umv space failed, want %u, get %u",
1168 pf->wanted_umv_size, allocated_size);
1170 pf->max_umv_size = (!!allocated_size) ? allocated_size :
1171 pf->wanted_umv_size;
1172 pf->used_umv_size = 0;
1177 hns3_uninit_umv_space(struct hns3_hw *hw)
1179 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
1180 struct hns3_pf *pf = &hns->pf;
1183 if (pf->max_umv_size == 0)
1186 ret = hns3_set_umv_space(hw, pf->max_umv_size, NULL, false);
1190 pf->max_umv_size = 0;
1196 hns3_is_umv_space_full(struct hns3_hw *hw)
1198 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
1199 struct hns3_pf *pf = &hns->pf;
1202 is_full = (pf->used_umv_size >= pf->max_umv_size);
1208 hns3_update_umv_space(struct hns3_hw *hw, bool is_free)
1210 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
1211 struct hns3_pf *pf = &hns->pf;
1214 if (pf->used_umv_size > 0)
1215 pf->used_umv_size--;
1217 pf->used_umv_size++;
1221 hns3_prepare_mac_addr(struct hns3_mac_vlan_tbl_entry_cmd *new_req,
1222 const uint8_t *addr, bool is_mc)
1224 const unsigned char *mac_addr = addr;
1225 uint32_t high_val = ((uint32_t)mac_addr[3] << 24) |
1226 ((uint32_t)mac_addr[2] << 16) |
1227 ((uint32_t)mac_addr[1] << 8) |
1228 (uint32_t)mac_addr[0];
1229 uint32_t low_val = ((uint32_t)mac_addr[5] << 8) | (uint32_t)mac_addr[4];
1231 hns3_set_bit(new_req->flags, HNS3_MAC_VLAN_BIT0_EN_B, 1);
1233 hns3_set_bit(new_req->entry_type, HNS3_MAC_VLAN_BIT0_EN_B, 0);
1234 hns3_set_bit(new_req->entry_type, HNS3_MAC_VLAN_BIT1_EN_B, 1);
1235 hns3_set_bit(new_req->mc_mac_en, HNS3_MAC_VLAN_BIT0_EN_B, 1);
1238 new_req->mac_addr_hi32 = rte_cpu_to_le_32(high_val);
1239 new_req->mac_addr_lo16 = rte_cpu_to_le_16(low_val & 0xffff);
1243 hns3_get_mac_vlan_cmd_status(struct hns3_hw *hw, uint16_t cmdq_resp,
1245 enum hns3_mac_vlan_tbl_opcode op)
1248 hns3_err(hw, "cmdq execute failed for get_mac_vlan_cmd_status,status=%u",
1253 if (op == HNS3_MAC_VLAN_ADD) {
1254 if (resp_code == 0 || resp_code == 1) {
1256 } else if (resp_code == HNS3_ADD_UC_OVERFLOW) {
1257 hns3_err(hw, "add mac addr failed for uc_overflow");
1259 } else if (resp_code == HNS3_ADD_MC_OVERFLOW) {
1260 hns3_err(hw, "add mac addr failed for mc_overflow");
1264 hns3_err(hw, "add mac addr failed for undefined, code=%u",
1267 } else if (op == HNS3_MAC_VLAN_REMOVE) {
1268 if (resp_code == 0) {
1270 } else if (resp_code == 1) {
1271 hns3_dbg(hw, "remove mac addr failed for miss");
1275 hns3_err(hw, "remove mac addr failed for undefined, code=%u",
1278 } else if (op == HNS3_MAC_VLAN_LKUP) {
1279 if (resp_code == 0) {
1281 } else if (resp_code == 1) {
1282 hns3_dbg(hw, "lookup mac addr failed for miss");
1286 hns3_err(hw, "lookup mac addr failed for undefined, code=%u",
1291 hns3_err(hw, "unknown opcode for get_mac_vlan_cmd_status, opcode=%u",
1298 hns3_lookup_mac_vlan_tbl(struct hns3_hw *hw,
1299 struct hns3_mac_vlan_tbl_entry_cmd *req,
1300 struct hns3_cmd_desc *desc, bool is_mc)
1306 hns3_cmd_setup_basic_desc(&desc[0], HNS3_OPC_MAC_VLAN_ADD, true);
1308 desc[0].flag |= rte_cpu_to_le_16(HNS3_CMD_FLAG_NEXT);
1309 memcpy(desc[0].data, req,
1310 sizeof(struct hns3_mac_vlan_tbl_entry_cmd));
1311 hns3_cmd_setup_basic_desc(&desc[1], HNS3_OPC_MAC_VLAN_ADD,
1313 desc[1].flag |= rte_cpu_to_le_16(HNS3_CMD_FLAG_NEXT);
1314 hns3_cmd_setup_basic_desc(&desc[2], HNS3_OPC_MAC_VLAN_ADD,
1316 ret = hns3_cmd_send(hw, desc, HNS3_MC_MAC_VLAN_ADD_DESC_NUM);
1318 memcpy(desc[0].data, req,
1319 sizeof(struct hns3_mac_vlan_tbl_entry_cmd));
1320 ret = hns3_cmd_send(hw, desc, 1);
1323 hns3_err(hw, "lookup mac addr failed for cmd_send, ret =%d.",
1327 resp_code = (rte_le_to_cpu_32(desc[0].data[0]) >> 8) & 0xff;
1328 retval = rte_le_to_cpu_16(desc[0].retval);
1330 return hns3_get_mac_vlan_cmd_status(hw, retval, resp_code,
1331 HNS3_MAC_VLAN_LKUP);
1335 hns3_add_mac_vlan_tbl(struct hns3_hw *hw,
1336 struct hns3_mac_vlan_tbl_entry_cmd *req,
1337 struct hns3_cmd_desc *mc_desc)
1344 if (mc_desc == NULL) {
1345 struct hns3_cmd_desc desc;
1347 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_MAC_VLAN_ADD, false);
1348 memcpy(desc.data, req,
1349 sizeof(struct hns3_mac_vlan_tbl_entry_cmd));
1350 ret = hns3_cmd_send(hw, &desc, 1);
1351 resp_code = (rte_le_to_cpu_32(desc.data[0]) >> 8) & 0xff;
1352 retval = rte_le_to_cpu_16(desc.retval);
1354 cfg_status = hns3_get_mac_vlan_cmd_status(hw, retval, resp_code,
1357 hns3_cmd_reuse_desc(&mc_desc[0], false);
1358 mc_desc[0].flag |= rte_cpu_to_le_16(HNS3_CMD_FLAG_NEXT);
1359 hns3_cmd_reuse_desc(&mc_desc[1], false);
1360 mc_desc[1].flag |= rte_cpu_to_le_16(HNS3_CMD_FLAG_NEXT);
1361 hns3_cmd_reuse_desc(&mc_desc[2], false);
1362 mc_desc[2].flag &= rte_cpu_to_le_16(~HNS3_CMD_FLAG_NEXT);
1363 memcpy(mc_desc[0].data, req,
1364 sizeof(struct hns3_mac_vlan_tbl_entry_cmd));
1365 mc_desc[0].retval = 0;
1366 ret = hns3_cmd_send(hw, mc_desc, HNS3_MC_MAC_VLAN_ADD_DESC_NUM);
1367 resp_code = (rte_le_to_cpu_32(mc_desc[0].data[0]) >> 8) & 0xff;
1368 retval = rte_le_to_cpu_16(mc_desc[0].retval);
1370 cfg_status = hns3_get_mac_vlan_cmd_status(hw, retval, resp_code,
1375 hns3_err(hw, "add mac addr failed for cmd_send, ret =%d", ret);
1383 hns3_remove_mac_vlan_tbl(struct hns3_hw *hw,
1384 struct hns3_mac_vlan_tbl_entry_cmd *req)
1386 struct hns3_cmd_desc desc;
1391 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_MAC_VLAN_REMOVE, false);
1393 memcpy(desc.data, req, sizeof(struct hns3_mac_vlan_tbl_entry_cmd));
1395 ret = hns3_cmd_send(hw, &desc, 1);
1397 hns3_err(hw, "del mac addr failed for cmd_send, ret =%d", ret);
1400 resp_code = (rte_le_to_cpu_32(desc.data[0]) >> 8) & 0xff;
1401 retval = rte_le_to_cpu_16(desc.retval);
1403 return hns3_get_mac_vlan_cmd_status(hw, retval, resp_code,
1404 HNS3_MAC_VLAN_REMOVE);
1408 hns3_add_uc_addr_common(struct hns3_hw *hw, struct rte_ether_addr *mac_addr)
1410 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
1411 struct hns3_mac_vlan_tbl_entry_cmd req;
1412 struct hns3_pf *pf = &hns->pf;
1413 struct hns3_cmd_desc desc;
1414 char mac_str[RTE_ETHER_ADDR_FMT_SIZE];
1415 uint16_t egress_port = 0;
1419 /* check if mac addr is valid */
1420 if (!rte_is_valid_assigned_ether_addr(mac_addr)) {
1421 rte_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
1423 hns3_err(hw, "Add unicast mac addr err! addr(%s) invalid",
1428 memset(&req, 0, sizeof(req));
1431 * In current version VF is not supported when PF is driven by DPDK
1432 * driver, just need to configure parameters for PF vport.
1434 vf_id = HNS3_PF_FUNC_ID;
1435 hns3_set_field(egress_port, HNS3_MAC_EPORT_VFID_M,
1436 HNS3_MAC_EPORT_VFID_S, vf_id);
1438 req.egress_port = rte_cpu_to_le_16(egress_port);
1440 hns3_prepare_mac_addr(&req, mac_addr->addr_bytes, false);
1443 * Lookup the mac address in the mac_vlan table, and add
1444 * it if the entry is inexistent. Repeated unicast entry
1445 * is not allowed in the mac vlan table.
1447 ret = hns3_lookup_mac_vlan_tbl(hw, &req, &desc, false);
1448 if (ret == -ENOENT) {
1449 if (!hns3_is_umv_space_full(hw)) {
1450 ret = hns3_add_mac_vlan_tbl(hw, &req, NULL);
1452 hns3_update_umv_space(hw, false);
1456 hns3_err(hw, "UC MAC table full(%u)", pf->used_umv_size);
1461 rte_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE, mac_addr);
1463 /* check if we just hit the duplicate */
1465 hns3_dbg(hw, "mac addr(%s) has been in the MAC table", mac_str);
1469 hns3_err(hw, "PF failed to add unicast entry(%s) in the MAC table",
1476 hns3_add_mc_addr_common(struct hns3_hw *hw, struct rte_ether_addr *mac_addr)
1478 char mac_str[RTE_ETHER_ADDR_FMT_SIZE];
1479 struct rte_ether_addr *addr;
1483 for (i = 0; i < hw->mc_addrs_num; i++) {
1484 addr = &hw->mc_addrs[i];
1485 /* Check if there are duplicate addresses */
1486 if (rte_is_same_ether_addr(addr, mac_addr)) {
1487 rte_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
1489 hns3_err(hw, "failed to add mc mac addr, same addrs"
1490 "(%s) is added by the set_mc_mac_addr_list "
1496 ret = hns3_add_mc_addr(hw, mac_addr);
1498 rte_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
1500 hns3_err(hw, "failed to add mc mac addr(%s), ret = %d",
1507 hns3_remove_mc_addr_common(struct hns3_hw *hw, struct rte_ether_addr *mac_addr)
1509 char mac_str[RTE_ETHER_ADDR_FMT_SIZE];
1512 ret = hns3_remove_mc_addr(hw, mac_addr);
1514 rte_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
1516 hns3_err(hw, "failed to remove mc mac addr(%s), ret = %d",
1523 hns3_add_mac_addr(struct rte_eth_dev *dev, struct rte_ether_addr *mac_addr,
1524 uint32_t idx, __rte_unused uint32_t pool)
1526 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1527 char mac_str[RTE_ETHER_ADDR_FMT_SIZE];
1530 rte_spinlock_lock(&hw->lock);
1533 * In hns3 network engine adding UC and MC mac address with different
1534 * commands with firmware. We need to determine whether the input
1535 * address is a UC or a MC address to call different commands.
1536 * By the way, it is recommended calling the API function named
1537 * rte_eth_dev_set_mc_addr_list to set the MC mac address, because
1538 * using the rte_eth_dev_mac_addr_add API function to set MC mac address
1539 * may affect the specifications of UC mac addresses.
1541 if (rte_is_multicast_ether_addr(mac_addr))
1542 ret = hns3_add_mc_addr_common(hw, mac_addr);
1544 ret = hns3_add_uc_addr_common(hw, mac_addr);
1547 rte_spinlock_unlock(&hw->lock);
1548 rte_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
1550 hns3_err(hw, "failed to add mac addr(%s), ret = %d", mac_str,
1556 hw->mac.default_addr_setted = true;
1557 rte_spinlock_unlock(&hw->lock);
1563 hns3_remove_uc_addr_common(struct hns3_hw *hw, struct rte_ether_addr *mac_addr)
1565 struct hns3_mac_vlan_tbl_entry_cmd req;
1566 char mac_str[RTE_ETHER_ADDR_FMT_SIZE];
1569 /* check if mac addr is valid */
1570 if (!rte_is_valid_assigned_ether_addr(mac_addr)) {
1571 rte_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
1573 hns3_err(hw, "remove unicast mac addr err! addr(%s) invalid",
1578 memset(&req, 0, sizeof(req));
1579 hns3_set_bit(req.entry_type, HNS3_MAC_VLAN_BIT0_EN_B, 0);
1580 hns3_prepare_mac_addr(&req, mac_addr->addr_bytes, false);
1581 ret = hns3_remove_mac_vlan_tbl(hw, &req);
1582 if (ret == -ENOENT) /* mac addr isn't existent in the mac vlan table. */
1585 hns3_update_umv_space(hw, true);
1591 hns3_remove_mac_addr(struct rte_eth_dev *dev, uint32_t idx)
1593 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1594 /* index will be checked by upper level rte interface */
1595 struct rte_ether_addr *mac_addr = &dev->data->mac_addrs[idx];
1596 char mac_str[RTE_ETHER_ADDR_FMT_SIZE];
1599 rte_spinlock_lock(&hw->lock);
1601 if (rte_is_multicast_ether_addr(mac_addr))
1602 ret = hns3_remove_mc_addr_common(hw, mac_addr);
1604 ret = hns3_remove_uc_addr_common(hw, mac_addr);
1605 rte_spinlock_unlock(&hw->lock);
1607 rte_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
1609 hns3_err(hw, "failed to remove mac addr(%s), ret = %d", mac_str,
1615 hns3_set_default_mac_addr(struct rte_eth_dev *dev,
1616 struct rte_ether_addr *mac_addr)
1618 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1619 struct rte_ether_addr *oaddr;
1620 char mac_str[RTE_ETHER_ADDR_FMT_SIZE];
1621 bool default_addr_setted;
1622 bool rm_succes = false;
1626 * It has been guaranteed that input parameter named mac_addr is valid
1627 * address in the rte layer of DPDK framework.
1629 oaddr = (struct rte_ether_addr *)hw->mac.mac_addr;
1630 default_addr_setted = hw->mac.default_addr_setted;
1631 if (default_addr_setted && !!rte_is_same_ether_addr(mac_addr, oaddr))
1634 rte_spinlock_lock(&hw->lock);
1635 if (default_addr_setted) {
1636 ret = hns3_remove_uc_addr_common(hw, oaddr);
1638 rte_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
1640 hns3_warn(hw, "Remove old uc mac address(%s) fail: %d",
1647 ret = hns3_add_uc_addr_common(hw, mac_addr);
1649 rte_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
1651 hns3_err(hw, "Failed to set mac addr(%s): %d", mac_str, ret);
1652 goto err_add_uc_addr;
1655 ret = hns3_pause_addr_cfg(hw, mac_addr->addr_bytes);
1657 hns3_err(hw, "Failed to configure mac pause address: %d", ret);
1658 goto err_pause_addr_cfg;
1661 rte_ether_addr_copy(mac_addr,
1662 (struct rte_ether_addr *)hw->mac.mac_addr);
1663 hw->mac.default_addr_setted = true;
1664 rte_spinlock_unlock(&hw->lock);
1669 ret_val = hns3_remove_uc_addr_common(hw, mac_addr);
1671 rte_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
1674 "Failed to roll back to del setted mac addr(%s): %d",
1680 ret_val = hns3_add_uc_addr_common(hw, oaddr);
1682 rte_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
1685 "Failed to restore old uc mac addr(%s): %d",
1687 hw->mac.default_addr_setted = false;
1690 rte_spinlock_unlock(&hw->lock);
1696 hns3_configure_all_mac_addr(struct hns3_adapter *hns, bool del)
1698 char mac_str[RTE_ETHER_ADDR_FMT_SIZE];
1699 struct hns3_hw *hw = &hns->hw;
1700 struct rte_ether_addr *addr;
1705 for (i = 0; i < HNS3_UC_MACADDR_NUM; i++) {
1706 addr = &hw->data->mac_addrs[i];
1707 if (rte_is_zero_ether_addr(addr))
1709 if (rte_is_multicast_ether_addr(addr))
1710 ret = del ? hns3_remove_mc_addr(hw, addr) :
1711 hns3_add_mc_addr(hw, addr);
1713 ret = del ? hns3_remove_uc_addr_common(hw, addr) :
1714 hns3_add_uc_addr_common(hw, addr);
1718 rte_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
1720 hns3_err(hw, "failed to %s mac addr(%s) index:%d "
1721 "ret = %d.", del ? "remove" : "restore",
1729 hns3_update_desc_vfid(struct hns3_cmd_desc *desc, uint8_t vfid, bool clr)
1731 #define HNS3_VF_NUM_IN_FIRST_DESC 192
1735 if (vfid < HNS3_VF_NUM_IN_FIRST_DESC) {
1736 word_num = vfid / 32;
1737 bit_num = vfid % 32;
1739 desc[1].data[word_num] &=
1740 rte_cpu_to_le_32(~(1UL << bit_num));
1742 desc[1].data[word_num] |=
1743 rte_cpu_to_le_32(1UL << bit_num);
1745 word_num = (vfid - HNS3_VF_NUM_IN_FIRST_DESC) / 32;
1746 bit_num = vfid % 32;
1748 desc[2].data[word_num] &=
1749 rte_cpu_to_le_32(~(1UL << bit_num));
1751 desc[2].data[word_num] |=
1752 rte_cpu_to_le_32(1UL << bit_num);
1757 hns3_add_mc_addr(struct hns3_hw *hw, struct rte_ether_addr *mac_addr)
1759 struct hns3_mac_vlan_tbl_entry_cmd req;
1760 struct hns3_cmd_desc desc[3];
1761 char mac_str[RTE_ETHER_ADDR_FMT_SIZE];
1765 /* Check if mac addr is valid */
1766 if (!rte_is_multicast_ether_addr(mac_addr)) {
1767 rte_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
1769 hns3_err(hw, "failed to add mc mac addr, addr(%s) invalid",
1774 memset(&req, 0, sizeof(req));
1775 hns3_set_bit(req.entry_type, HNS3_MAC_VLAN_BIT0_EN_B, 0);
1776 hns3_prepare_mac_addr(&req, mac_addr->addr_bytes, true);
1777 ret = hns3_lookup_mac_vlan_tbl(hw, &req, desc, true);
1779 /* This mac addr do not exist, add new entry for it */
1780 memset(desc[0].data, 0, sizeof(desc[0].data));
1781 memset(desc[1].data, 0, sizeof(desc[0].data));
1782 memset(desc[2].data, 0, sizeof(desc[0].data));
1786 * In current version VF is not supported when PF is driven by DPDK
1787 * driver, just need to configure parameters for PF vport.
1789 vf_id = HNS3_PF_FUNC_ID;
1790 hns3_update_desc_vfid(desc, vf_id, false);
1791 ret = hns3_add_mac_vlan_tbl(hw, &req, desc);
1794 hns3_err(hw, "mc mac vlan table is full");
1795 rte_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
1797 hns3_err(hw, "failed to add mc mac addr(%s): %d", mac_str, ret);
1804 hns3_remove_mc_addr(struct hns3_hw *hw, struct rte_ether_addr *mac_addr)
1806 struct hns3_mac_vlan_tbl_entry_cmd req;
1807 struct hns3_cmd_desc desc[3];
1808 char mac_str[RTE_ETHER_ADDR_FMT_SIZE];
1812 /* Check if mac addr is valid */
1813 if (!rte_is_multicast_ether_addr(mac_addr)) {
1814 rte_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
1816 hns3_err(hw, "Failed to rm mc mac addr, addr(%s) invalid",
1821 memset(&req, 0, sizeof(req));
1822 hns3_set_bit(req.entry_type, HNS3_MAC_VLAN_BIT0_EN_B, 0);
1823 hns3_prepare_mac_addr(&req, mac_addr->addr_bytes, true);
1824 ret = hns3_lookup_mac_vlan_tbl(hw, &req, desc, true);
1827 * This mac addr exist, remove this handle's VFID for it.
1828 * In current version VF is not supported when PF is driven by
1829 * DPDK driver, just need to configure parameters for PF vport.
1831 vf_id = HNS3_PF_FUNC_ID;
1832 hns3_update_desc_vfid(desc, vf_id, true);
1834 /* All the vfid is zero, so need to delete this entry */
1835 ret = hns3_remove_mac_vlan_tbl(hw, &req);
1836 } else if (ret == -ENOENT) {
1837 /* This mac addr doesn't exist. */
1842 rte_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
1844 hns3_err(hw, "Failed to rm mc mac addr(%s): %d", mac_str, ret);
1851 hns3_set_mc_addr_chk_param(struct hns3_hw *hw,
1852 struct rte_ether_addr *mc_addr_set,
1853 uint32_t nb_mc_addr)
1855 char mac_str[RTE_ETHER_ADDR_FMT_SIZE];
1856 struct rte_ether_addr *addr;
1860 if (nb_mc_addr > HNS3_MC_MACADDR_NUM) {
1861 hns3_err(hw, "failed to set mc mac addr, nb_mc_addr(%d) "
1862 "invalid. valid range: 0~%d",
1863 nb_mc_addr, HNS3_MC_MACADDR_NUM);
1867 /* Check if input mac addresses are valid */
1868 for (i = 0; i < nb_mc_addr; i++) {
1869 addr = &mc_addr_set[i];
1870 if (!rte_is_multicast_ether_addr(addr)) {
1871 rte_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
1874 "failed to set mc mac addr, addr(%s) invalid.",
1879 /* Check if there are duplicate addresses */
1880 for (j = i + 1; j < nb_mc_addr; j++) {
1881 if (rte_is_same_ether_addr(addr, &mc_addr_set[j])) {
1882 rte_ether_format_addr(mac_str,
1883 RTE_ETHER_ADDR_FMT_SIZE,
1885 hns3_err(hw, "failed to set mc mac addr, "
1886 "addrs invalid. two same addrs(%s).",
1893 * Check if there are duplicate addresses between mac_addrs
1896 for (j = 0; j < HNS3_UC_MACADDR_NUM; j++) {
1897 if (rte_is_same_ether_addr(addr,
1898 &hw->data->mac_addrs[j])) {
1899 rte_ether_format_addr(mac_str,
1900 RTE_ETHER_ADDR_FMT_SIZE,
1902 hns3_err(hw, "failed to set mc mac addr, "
1903 "addrs invalid. addrs(%s) has already "
1904 "configured in mac_addr add API",
1915 hns3_set_mc_addr_calc_addr(struct hns3_hw *hw,
1916 struct rte_ether_addr *mc_addr_set,
1918 struct rte_ether_addr *reserved_addr_list,
1919 int *reserved_addr_num,
1920 struct rte_ether_addr *add_addr_list,
1922 struct rte_ether_addr *rm_addr_list,
1925 struct rte_ether_addr *addr;
1926 int current_addr_num;
1927 int reserved_num = 0;
1935 /* Calculate the mc mac address list that should be removed */
1936 current_addr_num = hw->mc_addrs_num;
1937 for (i = 0; i < current_addr_num; i++) {
1938 addr = &hw->mc_addrs[i];
1940 for (j = 0; j < mc_addr_num; j++) {
1941 if (rte_is_same_ether_addr(addr, &mc_addr_set[j])) {
1948 rte_ether_addr_copy(addr, &rm_addr_list[rm_num]);
1951 rte_ether_addr_copy(addr,
1952 &reserved_addr_list[reserved_num]);
1957 /* Calculate the mc mac address list that should be added */
1958 for (i = 0; i < mc_addr_num; i++) {
1959 addr = &mc_addr_set[i];
1961 for (j = 0; j < current_addr_num; j++) {
1962 if (rte_is_same_ether_addr(addr, &hw->mc_addrs[j])) {
1969 rte_ether_addr_copy(addr, &add_addr_list[add_num]);
1974 /* Reorder the mc mac address list maintained by driver */
1975 for (i = 0; i < reserved_num; i++)
1976 rte_ether_addr_copy(&reserved_addr_list[i], &hw->mc_addrs[i]);
1978 for (i = 0; i < rm_num; i++) {
1979 num = reserved_num + i;
1980 rte_ether_addr_copy(&rm_addr_list[i], &hw->mc_addrs[num]);
1983 *reserved_addr_num = reserved_num;
1984 *add_addr_num = add_num;
1985 *rm_addr_num = rm_num;
1989 hns3_set_mc_mac_addr_list(struct rte_eth_dev *dev,
1990 struct rte_ether_addr *mc_addr_set,
1991 uint32_t nb_mc_addr)
1993 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1994 struct rte_ether_addr reserved_addr_list[HNS3_MC_MACADDR_NUM];
1995 struct rte_ether_addr add_addr_list[HNS3_MC_MACADDR_NUM];
1996 struct rte_ether_addr rm_addr_list[HNS3_MC_MACADDR_NUM];
1997 struct rte_ether_addr *addr;
1998 int reserved_addr_num;
2006 /* Check if input parameters are valid */
2007 ret = hns3_set_mc_addr_chk_param(hw, mc_addr_set, nb_mc_addr);
2011 rte_spinlock_lock(&hw->lock);
2014 * Calculate the mc mac address lists those should be removed and be
2015 * added, Reorder the mc mac address list maintained by driver.
2017 mc_addr_num = (int)nb_mc_addr;
2018 hns3_set_mc_addr_calc_addr(hw, mc_addr_set, mc_addr_num,
2019 reserved_addr_list, &reserved_addr_num,
2020 add_addr_list, &add_addr_num,
2021 rm_addr_list, &rm_addr_num);
2023 /* Remove mc mac addresses */
2024 for (i = 0; i < rm_addr_num; i++) {
2025 num = rm_addr_num - i - 1;
2026 addr = &rm_addr_list[num];
2027 ret = hns3_remove_mc_addr(hw, addr);
2029 rte_spinlock_unlock(&hw->lock);
2035 /* Add mc mac addresses */
2036 for (i = 0; i < add_addr_num; i++) {
2037 addr = &add_addr_list[i];
2038 ret = hns3_add_mc_addr(hw, addr);
2040 rte_spinlock_unlock(&hw->lock);
2044 num = reserved_addr_num + i;
2045 rte_ether_addr_copy(addr, &hw->mc_addrs[num]);
2048 rte_spinlock_unlock(&hw->lock);
2054 hns3_configure_all_mc_mac_addr(struct hns3_adapter *hns, bool del)
2056 char mac_str[RTE_ETHER_ADDR_FMT_SIZE];
2057 struct hns3_hw *hw = &hns->hw;
2058 struct rte_ether_addr *addr;
2063 for (i = 0; i < hw->mc_addrs_num; i++) {
2064 addr = &hw->mc_addrs[i];
2065 if (!rte_is_multicast_ether_addr(addr))
2068 ret = hns3_remove_mc_addr(hw, addr);
2070 ret = hns3_add_mc_addr(hw, addr);
2073 rte_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
2075 hns3_dbg(hw, "%s mc mac addr: %s failed for pf: ret = %d",
2076 del ? "Remove" : "Restore", mac_str, ret);
2083 hns3_check_mq_mode(struct rte_eth_dev *dev)
2085 enum rte_eth_rx_mq_mode rx_mq_mode = dev->data->dev_conf.rxmode.mq_mode;
2086 enum rte_eth_tx_mq_mode tx_mq_mode = dev->data->dev_conf.txmode.mq_mode;
2087 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2088 struct hns3_pf *pf = HNS3_DEV_PRIVATE_TO_PF(dev->data->dev_private);
2089 struct rte_eth_dcb_rx_conf *dcb_rx_conf;
2090 struct rte_eth_dcb_tx_conf *dcb_tx_conf;
2095 dcb_rx_conf = &dev->data->dev_conf.rx_adv_conf.dcb_rx_conf;
2096 dcb_tx_conf = &dev->data->dev_conf.tx_adv_conf.dcb_tx_conf;
2098 if (rx_mq_mode == ETH_MQ_RX_VMDQ_DCB_RSS) {
2099 hns3_err(hw, "ETH_MQ_RX_VMDQ_DCB_RSS is not supported. "
2100 "rx_mq_mode = %d", rx_mq_mode);
2104 if (rx_mq_mode == ETH_MQ_RX_VMDQ_DCB ||
2105 tx_mq_mode == ETH_MQ_TX_VMDQ_DCB) {
2106 hns3_err(hw, "ETH_MQ_RX_VMDQ_DCB and ETH_MQ_TX_VMDQ_DCB "
2107 "is not supported. rx_mq_mode = %d, tx_mq_mode = %d",
2108 rx_mq_mode, tx_mq_mode);
2112 if (rx_mq_mode == ETH_MQ_RX_DCB_RSS) {
2113 if (dcb_rx_conf->nb_tcs > pf->tc_max) {
2114 hns3_err(hw, "nb_tcs(%u) > max_tc(%u) driver supported.",
2115 dcb_rx_conf->nb_tcs, pf->tc_max);
2119 if (!(dcb_rx_conf->nb_tcs == HNS3_4_TCS ||
2120 dcb_rx_conf->nb_tcs == HNS3_8_TCS)) {
2121 hns3_err(hw, "on ETH_MQ_RX_DCB_RSS mode, "
2122 "nb_tcs(%d) != %d or %d in rx direction.",
2123 dcb_rx_conf->nb_tcs, HNS3_4_TCS, HNS3_8_TCS);
2127 if (dcb_rx_conf->nb_tcs != dcb_tx_conf->nb_tcs) {
2128 hns3_err(hw, "num_tcs(%d) of tx is not equal to rx(%d)",
2129 dcb_tx_conf->nb_tcs, dcb_rx_conf->nb_tcs);
2133 for (i = 0; i < HNS3_MAX_USER_PRIO; i++) {
2134 if (dcb_rx_conf->dcb_tc[i] != dcb_tx_conf->dcb_tc[i]) {
2135 hns3_err(hw, "dcb_tc[%d] = %d in rx direction, "
2136 "is not equal to one in tx direction.",
2137 i, dcb_rx_conf->dcb_tc[i]);
2140 if (dcb_rx_conf->dcb_tc[i] > max_tc)
2141 max_tc = dcb_rx_conf->dcb_tc[i];
2144 num_tc = max_tc + 1;
2145 if (num_tc > dcb_rx_conf->nb_tcs) {
2146 hns3_err(hw, "max num_tc(%u) mapped > nb_tcs(%u)",
2147 num_tc, dcb_rx_conf->nb_tcs);
2156 hns3_check_dcb_cfg(struct rte_eth_dev *dev)
2158 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2160 if (!hns3_dev_dcb_supported(hw)) {
2161 hns3_err(hw, "this port does not support dcb configurations.");
2165 if (hw->current_fc_status == HNS3_FC_STATUS_MAC_PAUSE) {
2166 hns3_err(hw, "MAC pause enabled, cannot config dcb info.");
2170 /* Check multiple queue mode */
2171 return hns3_check_mq_mode(dev);
2175 hns3_bind_ring_with_vector(struct hns3_hw *hw, uint8_t vector_id, bool mmap,
2176 enum hns3_ring_type queue_type, uint16_t queue_id)
2178 struct hns3_cmd_desc desc;
2179 struct hns3_ctrl_vector_chain_cmd *req =
2180 (struct hns3_ctrl_vector_chain_cmd *)desc.data;
2181 enum hns3_cmd_status status;
2182 enum hns3_opcode_type op;
2183 uint16_t tqp_type_and_id = 0;
2188 op = mmap ? HNS3_OPC_ADD_RING_TO_VECTOR : HNS3_OPC_DEL_RING_TO_VECTOR;
2189 hns3_cmd_setup_basic_desc(&desc, op, false);
2190 req->int_vector_id = vector_id;
2192 if (queue_type == HNS3_RING_TYPE_RX)
2193 gl = HNS3_RING_GL_RX;
2195 gl = HNS3_RING_GL_TX;
2199 hns3_set_field(tqp_type_and_id, HNS3_INT_TYPE_M, HNS3_INT_TYPE_S,
2201 hns3_set_field(tqp_type_and_id, HNS3_TQP_ID_M, HNS3_TQP_ID_S, queue_id);
2202 hns3_set_field(tqp_type_and_id, HNS3_INT_GL_IDX_M, HNS3_INT_GL_IDX_S,
2204 req->tqp_type_and_id[0] = rte_cpu_to_le_16(tqp_type_and_id);
2205 req->int_cause_num = 1;
2206 op_str = mmap ? "Map" : "Unmap";
2207 status = hns3_cmd_send(hw, &desc, 1);
2209 hns3_err(hw, "%s TQP %d fail, vector_id is %d, status is %d.",
2210 op_str, queue_id, req->int_vector_id, status);
2218 hns3_init_ring_with_vector(struct hns3_hw *hw)
2225 * In hns3 network engine, vector 0 is always the misc interrupt of this
2226 * function, vector 1~N can be used respectively for the queues of the
2227 * function. Tx and Rx queues with the same number share the interrupt
2228 * vector. In the initialization clearing the all hardware mapping
2229 * relationship configurations between queues and interrupt vectors is
2230 * needed, so some error caused by the residual configurations, such as
2231 * the unexpected Tx interrupt, can be avoid.
2233 vec = hw->num_msi - 1; /* vector 0 for misc interrupt, not for queue */
2234 if (hw->intr.mapping_mode == HNS3_INTR_MAPPING_VEC_RSV_ONE)
2235 vec = vec - 1; /* the last interrupt is reserved */
2236 hw->intr_tqps_num = RTE_MIN(vec, hw->tqps_num);
2237 for (i = 0; i < hw->intr_tqps_num; i++) {
2239 * Set gap limiter/rate limiter/quanity limiter algorithm
2240 * configuration for interrupt coalesce of queue's interrupt.
2242 hns3_set_queue_intr_gl(hw, i, HNS3_RING_GL_RX,
2243 HNS3_TQP_INTR_GL_DEFAULT);
2244 hns3_set_queue_intr_gl(hw, i, HNS3_RING_GL_TX,
2245 HNS3_TQP_INTR_GL_DEFAULT);
2246 hns3_set_queue_intr_rl(hw, i, HNS3_TQP_INTR_RL_DEFAULT);
2247 hns3_set_queue_intr_ql(hw, i, HNS3_TQP_INTR_QL_DEFAULT);
2249 ret = hns3_bind_ring_with_vector(hw, vec, false,
2250 HNS3_RING_TYPE_TX, i);
2252 PMD_INIT_LOG(ERR, "PF fail to unbind TX ring(%d) with "
2253 "vector: %d, ret=%d", i, vec, ret);
2257 ret = hns3_bind_ring_with_vector(hw, vec, false,
2258 HNS3_RING_TYPE_RX, i);
2260 PMD_INIT_LOG(ERR, "PF fail to unbind RX ring(%d) with "
2261 "vector: %d, ret=%d", i, vec, ret);
2270 hns3_dev_configure(struct rte_eth_dev *dev)
2272 struct hns3_adapter *hns = dev->data->dev_private;
2273 struct rte_eth_conf *conf = &dev->data->dev_conf;
2274 enum rte_eth_rx_mq_mode mq_mode = conf->rxmode.mq_mode;
2275 struct hns3_hw *hw = &hns->hw;
2276 struct hns3_rss_conf *rss_cfg = &hw->rss_info;
2277 uint16_t nb_rx_q = dev->data->nb_rx_queues;
2278 uint16_t nb_tx_q = dev->data->nb_tx_queues;
2279 struct rte_eth_rss_conf rss_conf;
2285 * Hardware does not support individually enable/disable/reset the Tx or
2286 * Rx queue in hns3 network engine. Driver must enable/disable/reset Tx
2287 * and Rx queues at the same time. When the numbers of Tx queues
2288 * allocated by upper applications are not equal to the numbers of Rx
2289 * queues, driver needs to setup fake Tx or Rx queues to adjust numbers
2290 * of Tx/Rx queues. otherwise, network engine can not work as usual. But
2291 * these fake queues are imperceptible, and can not be used by upper
2294 ret = hns3_set_fake_rx_or_tx_queues(dev, nb_rx_q, nb_tx_q);
2296 hns3_err(hw, "Failed to set rx/tx fake queues: %d", ret);
2300 hw->adapter_state = HNS3_NIC_CONFIGURING;
2301 if (conf->link_speeds & ETH_LINK_SPEED_FIXED) {
2302 hns3_err(hw, "setting link speed/duplex not supported");
2307 if ((uint32_t)mq_mode & ETH_MQ_RX_DCB_FLAG) {
2308 ret = hns3_check_dcb_cfg(dev);
2313 /* When RSS is not configured, redirect the packet queue 0 */
2314 if ((uint32_t)mq_mode & ETH_MQ_RX_RSS_FLAG) {
2315 conf->rxmode.offloads |= DEV_RX_OFFLOAD_RSS_HASH;
2316 rss_conf = conf->rx_adv_conf.rss_conf;
2317 if (rss_conf.rss_key == NULL) {
2318 rss_conf.rss_key = rss_cfg->key;
2319 rss_conf.rss_key_len = HNS3_RSS_KEY_SIZE;
2322 ret = hns3_dev_rss_hash_update(dev, &rss_conf);
2328 * If jumbo frames are enabled, MTU needs to be refreshed
2329 * according to the maximum RX packet length.
2331 if (conf->rxmode.offloads & DEV_RX_OFFLOAD_JUMBO_FRAME) {
2333 * Security of max_rx_pkt_len is guaranteed in dpdk frame.
2334 * Maximum value of max_rx_pkt_len is HNS3_MAX_FRAME_LEN, so it
2335 * can safely assign to "uint16_t" type variable.
2337 mtu = (uint16_t)HNS3_PKTLEN_TO_MTU(conf->rxmode.max_rx_pkt_len);
2338 ret = hns3_dev_mtu_set(dev, mtu);
2341 dev->data->mtu = mtu;
2344 ret = hns3_dev_configure_vlan(dev);
2348 /* config hardware GRO */
2349 gro_en = conf->rxmode.offloads & DEV_RX_OFFLOAD_TCP_LRO ? true : false;
2350 ret = hns3_config_gro(hw, gro_en);
2354 hw->adapter_state = HNS3_NIC_CONFIGURED;
2359 (void)hns3_set_fake_rx_or_tx_queues(dev, 0, 0);
2360 hw->adapter_state = HNS3_NIC_INITIALIZED;
2366 hns3_set_mac_mtu(struct hns3_hw *hw, uint16_t new_mps)
2368 struct hns3_config_max_frm_size_cmd *req;
2369 struct hns3_cmd_desc desc;
2371 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_CONFIG_MAX_FRM_SIZE, false);
2373 req = (struct hns3_config_max_frm_size_cmd *)desc.data;
2374 req->max_frm_size = rte_cpu_to_le_16(new_mps);
2375 req->min_frm_size = RTE_ETHER_MIN_LEN;
2377 return hns3_cmd_send(hw, &desc, 1);
2381 hns3_config_mtu(struct hns3_hw *hw, uint16_t mps)
2385 ret = hns3_set_mac_mtu(hw, mps);
2387 hns3_err(hw, "Failed to set mtu, ret = %d", ret);
2391 ret = hns3_buffer_alloc(hw);
2393 hns3_err(hw, "Failed to allocate buffer, ret = %d", ret);
2399 hns3_dev_mtu_set(struct rte_eth_dev *dev, uint16_t mtu)
2401 struct hns3_adapter *hns = dev->data->dev_private;
2402 uint32_t frame_size = mtu + HNS3_ETH_OVERHEAD;
2403 struct hns3_hw *hw = &hns->hw;
2404 bool is_jumbo_frame;
2407 if (dev->data->dev_started) {
2408 hns3_err(hw, "Failed to set mtu, port %u must be stopped "
2409 "before configuration", dev->data->port_id);
2413 rte_spinlock_lock(&hw->lock);
2414 is_jumbo_frame = frame_size > RTE_ETHER_MAX_LEN ? true : false;
2415 frame_size = RTE_MAX(frame_size, HNS3_DEFAULT_FRAME_LEN);
2418 * Maximum value of frame_size is HNS3_MAX_FRAME_LEN, so it can safely
2419 * assign to "uint16_t" type variable.
2421 ret = hns3_config_mtu(hw, (uint16_t)frame_size);
2423 rte_spinlock_unlock(&hw->lock);
2424 hns3_err(hw, "Failed to set mtu, port %u mtu %u: %d",
2425 dev->data->port_id, mtu, ret);
2428 hns->pf.mps = (uint16_t)frame_size;
2430 dev->data->dev_conf.rxmode.offloads |=
2431 DEV_RX_OFFLOAD_JUMBO_FRAME;
2433 dev->data->dev_conf.rxmode.offloads &=
2434 ~DEV_RX_OFFLOAD_JUMBO_FRAME;
2435 dev->data->dev_conf.rxmode.max_rx_pkt_len = frame_size;
2436 rte_spinlock_unlock(&hw->lock);
2442 hns3_dev_infos_get(struct rte_eth_dev *eth_dev, struct rte_eth_dev_info *info)
2444 struct hns3_adapter *hns = eth_dev->data->dev_private;
2445 struct hns3_hw *hw = &hns->hw;
2446 uint16_t queue_num = hw->tqps_num;
2449 * In interrupt mode, 'max_rx_queues' is set based on the number of
2450 * MSI-X interrupt resources of the hardware.
2452 if (hw->data->dev_conf.intr_conf.rxq == 1)
2453 queue_num = hw->intr_tqps_num;
2455 info->max_rx_queues = queue_num;
2456 info->max_tx_queues = hw->tqps_num;
2457 info->max_rx_pktlen = HNS3_MAX_FRAME_LEN; /* CRC included */
2458 info->min_rx_bufsize = HNS3_MIN_BD_BUF_SIZE;
2459 info->max_mac_addrs = HNS3_UC_MACADDR_NUM;
2460 info->max_mtu = info->max_rx_pktlen - HNS3_ETH_OVERHEAD;
2461 info->max_lro_pkt_size = HNS3_MAX_LRO_SIZE;
2462 info->rx_offload_capa = (DEV_RX_OFFLOAD_IPV4_CKSUM |
2463 DEV_RX_OFFLOAD_TCP_CKSUM |
2464 DEV_RX_OFFLOAD_UDP_CKSUM |
2465 DEV_RX_OFFLOAD_SCTP_CKSUM |
2466 DEV_RX_OFFLOAD_OUTER_IPV4_CKSUM |
2467 DEV_RX_OFFLOAD_OUTER_UDP_CKSUM |
2468 DEV_RX_OFFLOAD_KEEP_CRC |
2469 DEV_RX_OFFLOAD_SCATTER |
2470 DEV_RX_OFFLOAD_VLAN_STRIP |
2471 DEV_RX_OFFLOAD_VLAN_FILTER |
2472 DEV_RX_OFFLOAD_JUMBO_FRAME |
2473 DEV_RX_OFFLOAD_RSS_HASH |
2474 DEV_RX_OFFLOAD_TCP_LRO);
2475 info->tx_queue_offload_capa = DEV_TX_OFFLOAD_MBUF_FAST_FREE;
2476 info->tx_offload_capa = (DEV_TX_OFFLOAD_OUTER_IPV4_CKSUM |
2477 DEV_TX_OFFLOAD_IPV4_CKSUM |
2478 DEV_TX_OFFLOAD_TCP_CKSUM |
2479 DEV_TX_OFFLOAD_UDP_CKSUM |
2480 DEV_TX_OFFLOAD_SCTP_CKSUM |
2481 DEV_TX_OFFLOAD_MULTI_SEGS |
2482 DEV_TX_OFFLOAD_TCP_TSO |
2483 DEV_TX_OFFLOAD_VXLAN_TNL_TSO |
2484 DEV_TX_OFFLOAD_GRE_TNL_TSO |
2485 DEV_TX_OFFLOAD_GENEVE_TNL_TSO |
2486 info->tx_queue_offload_capa |
2487 hns3_txvlan_cap_get(hw));
2489 info->rx_desc_lim = (struct rte_eth_desc_lim) {
2490 .nb_max = HNS3_MAX_RING_DESC,
2491 .nb_min = HNS3_MIN_RING_DESC,
2492 .nb_align = HNS3_ALIGN_RING_DESC,
2495 info->tx_desc_lim = (struct rte_eth_desc_lim) {
2496 .nb_max = HNS3_MAX_RING_DESC,
2497 .nb_min = HNS3_MIN_RING_DESC,
2498 .nb_align = HNS3_ALIGN_RING_DESC,
2499 .nb_seg_max = HNS3_MAX_TSO_BD_PER_PKT,
2500 .nb_mtu_seg_max = HNS3_MAX_NON_TSO_BD_PER_PKT,
2503 info->default_rxconf = (struct rte_eth_rxconf) {
2505 * If there are no available Rx buffer descriptors, incoming
2506 * packets are always dropped by hardware based on hns3 network
2512 info->vmdq_queue_num = 0;
2514 info->reta_size = HNS3_RSS_IND_TBL_SIZE;
2515 info->hash_key_size = HNS3_RSS_KEY_SIZE;
2516 info->flow_type_rss_offloads = HNS3_ETH_RSS_SUPPORT;
2518 info->default_rxportconf.burst_size = HNS3_DEFAULT_PORT_CONF_BURST_SIZE;
2519 info->default_txportconf.burst_size = HNS3_DEFAULT_PORT_CONF_BURST_SIZE;
2520 info->default_rxportconf.nb_queues = HNS3_DEFAULT_PORT_CONF_QUEUES_NUM;
2521 info->default_txportconf.nb_queues = HNS3_DEFAULT_PORT_CONF_QUEUES_NUM;
2522 info->default_rxportconf.ring_size = HNS3_DEFAULT_RING_DESC;
2523 info->default_txportconf.ring_size = HNS3_DEFAULT_RING_DESC;
2529 hns3_fw_version_get(struct rte_eth_dev *eth_dev, char *fw_version,
2532 struct hns3_adapter *hns = eth_dev->data->dev_private;
2533 struct hns3_hw *hw = &hns->hw;
2534 uint32_t version = hw->fw_version;
2537 ret = snprintf(fw_version, fw_size, "%lu.%lu.%lu.%lu",
2538 hns3_get_field(version, HNS3_FW_VERSION_BYTE3_M,
2539 HNS3_FW_VERSION_BYTE3_S),
2540 hns3_get_field(version, HNS3_FW_VERSION_BYTE2_M,
2541 HNS3_FW_VERSION_BYTE2_S),
2542 hns3_get_field(version, HNS3_FW_VERSION_BYTE1_M,
2543 HNS3_FW_VERSION_BYTE1_S),
2544 hns3_get_field(version, HNS3_FW_VERSION_BYTE0_M,
2545 HNS3_FW_VERSION_BYTE0_S));
2546 ret += 1; /* add the size of '\0' */
2547 if (fw_size < (uint32_t)ret)
2554 hns3_dev_link_update(struct rte_eth_dev *eth_dev,
2555 __rte_unused int wait_to_complete)
2557 struct hns3_adapter *hns = eth_dev->data->dev_private;
2558 struct hns3_hw *hw = &hns->hw;
2559 struct hns3_mac *mac = &hw->mac;
2560 struct rte_eth_link new_link;
2562 if (!hns3_is_reset_pending(hns)) {
2563 hns3_update_speed_duplex(eth_dev);
2564 hns3_update_link_status(hw);
2567 memset(&new_link, 0, sizeof(new_link));
2568 switch (mac->link_speed) {
2569 case ETH_SPEED_NUM_10M:
2570 case ETH_SPEED_NUM_100M:
2571 case ETH_SPEED_NUM_1G:
2572 case ETH_SPEED_NUM_10G:
2573 case ETH_SPEED_NUM_25G:
2574 case ETH_SPEED_NUM_40G:
2575 case ETH_SPEED_NUM_50G:
2576 case ETH_SPEED_NUM_100G:
2577 case ETH_SPEED_NUM_200G:
2578 new_link.link_speed = mac->link_speed;
2581 new_link.link_speed = ETH_SPEED_NUM_100M;
2585 new_link.link_duplex = mac->link_duplex;
2586 new_link.link_status = mac->link_status ? ETH_LINK_UP : ETH_LINK_DOWN;
2587 new_link.link_autoneg =
2588 !(eth_dev->data->dev_conf.link_speeds & ETH_LINK_SPEED_FIXED);
2590 return rte_eth_linkstatus_set(eth_dev, &new_link);
2594 hns3_parse_func_status(struct hns3_hw *hw, struct hns3_func_status_cmd *status)
2596 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
2597 struct hns3_pf *pf = &hns->pf;
2599 if (!(status->pf_state & HNS3_PF_STATE_DONE))
2602 pf->is_main_pf = (status->pf_state & HNS3_PF_STATE_MAIN) ? true : false;
2608 hns3_query_function_status(struct hns3_hw *hw)
2610 #define HNS3_QUERY_MAX_CNT 10
2611 #define HNS3_QUERY_SLEEP_MSCOEND 1
2612 struct hns3_func_status_cmd *req;
2613 struct hns3_cmd_desc desc;
2617 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_QUERY_FUNC_STATUS, true);
2618 req = (struct hns3_func_status_cmd *)desc.data;
2621 ret = hns3_cmd_send(hw, &desc, 1);
2623 PMD_INIT_LOG(ERR, "query function status failed %d",
2628 /* Check pf reset is done */
2632 rte_delay_ms(HNS3_QUERY_SLEEP_MSCOEND);
2633 } while (timeout++ < HNS3_QUERY_MAX_CNT);
2635 return hns3_parse_func_status(hw, req);
2639 hns3_query_pf_resource(struct hns3_hw *hw)
2641 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
2642 struct hns3_pf *pf = &hns->pf;
2643 struct hns3_pf_res_cmd *req;
2644 struct hns3_cmd_desc desc;
2647 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_QUERY_PF_RSRC, true);
2648 ret = hns3_cmd_send(hw, &desc, 1);
2650 PMD_INIT_LOG(ERR, "query pf resource failed %d", ret);
2654 req = (struct hns3_pf_res_cmd *)desc.data;
2655 hw->total_tqps_num = rte_le_to_cpu_16(req->tqp_num);
2656 pf->pkt_buf_size = rte_le_to_cpu_16(req->buf_size) << HNS3_BUF_UNIT_S;
2657 hw->tqps_num = RTE_MIN(hw->total_tqps_num, HNS3_MAX_TQP_NUM_PER_FUNC);
2658 pf->func_num = rte_le_to_cpu_16(req->pf_own_fun_number);
2660 if (req->tx_buf_size)
2662 rte_le_to_cpu_16(req->tx_buf_size) << HNS3_BUF_UNIT_S;
2664 pf->tx_buf_size = HNS3_DEFAULT_TX_BUF;
2666 pf->tx_buf_size = roundup(pf->tx_buf_size, HNS3_BUF_SIZE_UNIT);
2668 if (req->dv_buf_size)
2670 rte_le_to_cpu_16(req->dv_buf_size) << HNS3_BUF_UNIT_S;
2672 pf->dv_buf_size = HNS3_DEFAULT_DV;
2674 pf->dv_buf_size = roundup(pf->dv_buf_size, HNS3_BUF_SIZE_UNIT);
2677 hns3_get_field(rte_le_to_cpu_16(req->nic_pf_intr_vector_number),
2678 HNS3_PF_VEC_NUM_M, HNS3_PF_VEC_NUM_S);
2684 hns3_parse_cfg(struct hns3_cfg *cfg, struct hns3_cmd_desc *desc)
2686 struct hns3_cfg_param_cmd *req;
2687 uint64_t mac_addr_tmp_high;
2688 uint64_t mac_addr_tmp;
2691 req = (struct hns3_cfg_param_cmd *)desc[0].data;
2693 /* get the configuration */
2694 cfg->vmdq_vport_num = hns3_get_field(rte_le_to_cpu_32(req->param[0]),
2695 HNS3_CFG_VMDQ_M, HNS3_CFG_VMDQ_S);
2696 cfg->tc_num = hns3_get_field(rte_le_to_cpu_32(req->param[0]),
2697 HNS3_CFG_TC_NUM_M, HNS3_CFG_TC_NUM_S);
2698 cfg->tqp_desc_num = hns3_get_field(rte_le_to_cpu_32(req->param[0]),
2699 HNS3_CFG_TQP_DESC_N_M,
2700 HNS3_CFG_TQP_DESC_N_S);
2702 cfg->phy_addr = hns3_get_field(rte_le_to_cpu_32(req->param[1]),
2703 HNS3_CFG_PHY_ADDR_M,
2704 HNS3_CFG_PHY_ADDR_S);
2705 cfg->media_type = hns3_get_field(rte_le_to_cpu_32(req->param[1]),
2706 HNS3_CFG_MEDIA_TP_M,
2707 HNS3_CFG_MEDIA_TP_S);
2708 cfg->rx_buf_len = hns3_get_field(rte_le_to_cpu_32(req->param[1]),
2709 HNS3_CFG_RX_BUF_LEN_M,
2710 HNS3_CFG_RX_BUF_LEN_S);
2711 /* get mac address */
2712 mac_addr_tmp = rte_le_to_cpu_32(req->param[2]);
2713 mac_addr_tmp_high = hns3_get_field(rte_le_to_cpu_32(req->param[3]),
2714 HNS3_CFG_MAC_ADDR_H_M,
2715 HNS3_CFG_MAC_ADDR_H_S);
2717 mac_addr_tmp |= (mac_addr_tmp_high << 31) << 1;
2719 cfg->default_speed = hns3_get_field(rte_le_to_cpu_32(req->param[3]),
2720 HNS3_CFG_DEFAULT_SPEED_M,
2721 HNS3_CFG_DEFAULT_SPEED_S);
2722 cfg->rss_size_max = hns3_get_field(rte_le_to_cpu_32(req->param[3]),
2723 HNS3_CFG_RSS_SIZE_M,
2724 HNS3_CFG_RSS_SIZE_S);
2726 for (i = 0; i < RTE_ETHER_ADDR_LEN; i++)
2727 cfg->mac_addr[i] = (mac_addr_tmp >> (8 * i)) & 0xff;
2729 req = (struct hns3_cfg_param_cmd *)desc[1].data;
2730 cfg->numa_node_map = rte_le_to_cpu_32(req->param[0]);
2732 cfg->speed_ability = hns3_get_field(rte_le_to_cpu_32(req->param[1]),
2733 HNS3_CFG_SPEED_ABILITY_M,
2734 HNS3_CFG_SPEED_ABILITY_S);
2735 cfg->umv_space = hns3_get_field(rte_le_to_cpu_32(req->param[1]),
2736 HNS3_CFG_UMV_TBL_SPACE_M,
2737 HNS3_CFG_UMV_TBL_SPACE_S);
2738 if (!cfg->umv_space)
2739 cfg->umv_space = HNS3_DEFAULT_UMV_SPACE_PER_PF;
2742 /* hns3_get_board_cfg: query the static parameter from NCL_config file in flash
2743 * @hw: pointer to struct hns3_hw
2744 * @hcfg: the config structure to be getted
2747 hns3_get_board_cfg(struct hns3_hw *hw, struct hns3_cfg *hcfg)
2749 struct hns3_cmd_desc desc[HNS3_PF_CFG_DESC_NUM];
2750 struct hns3_cfg_param_cmd *req;
2755 for (i = 0; i < HNS3_PF_CFG_DESC_NUM; i++) {
2757 req = (struct hns3_cfg_param_cmd *)desc[i].data;
2758 hns3_cmd_setup_basic_desc(&desc[i], HNS3_OPC_GET_CFG_PARAM,
2760 hns3_set_field(offset, HNS3_CFG_OFFSET_M, HNS3_CFG_OFFSET_S,
2761 i * HNS3_CFG_RD_LEN_BYTES);
2762 /* Len should be divided by 4 when send to hardware */
2763 hns3_set_field(offset, HNS3_CFG_RD_LEN_M, HNS3_CFG_RD_LEN_S,
2764 HNS3_CFG_RD_LEN_BYTES / HNS3_CFG_RD_LEN_UNIT);
2765 req->offset = rte_cpu_to_le_32(offset);
2768 ret = hns3_cmd_send(hw, desc, HNS3_PF_CFG_DESC_NUM);
2770 PMD_INIT_LOG(ERR, "get config failed %d.", ret);
2774 hns3_parse_cfg(hcfg, desc);
2780 hns3_parse_speed(int speed_cmd, uint32_t *speed)
2782 switch (speed_cmd) {
2783 case HNS3_CFG_SPEED_10M:
2784 *speed = ETH_SPEED_NUM_10M;
2786 case HNS3_CFG_SPEED_100M:
2787 *speed = ETH_SPEED_NUM_100M;
2789 case HNS3_CFG_SPEED_1G:
2790 *speed = ETH_SPEED_NUM_1G;
2792 case HNS3_CFG_SPEED_10G:
2793 *speed = ETH_SPEED_NUM_10G;
2795 case HNS3_CFG_SPEED_25G:
2796 *speed = ETH_SPEED_NUM_25G;
2798 case HNS3_CFG_SPEED_40G:
2799 *speed = ETH_SPEED_NUM_40G;
2801 case HNS3_CFG_SPEED_50G:
2802 *speed = ETH_SPEED_NUM_50G;
2804 case HNS3_CFG_SPEED_100G:
2805 *speed = ETH_SPEED_NUM_100G;
2807 case HNS3_CFG_SPEED_200G:
2808 *speed = ETH_SPEED_NUM_200G;
2818 hns3_set_default_dev_specifications(struct hns3_hw *hw)
2820 hw->max_non_tso_bd_num = HNS3_MAX_NON_TSO_BD_PER_PKT;
2821 hw->rss_ind_tbl_size = HNS3_RSS_IND_TBL_SIZE;
2822 hw->rss_key_size = HNS3_RSS_KEY_SIZE;
2823 hw->max_tm_rate = HNS3_ETHER_MAX_RATE;
2827 hns3_parse_dev_specifications(struct hns3_hw *hw, struct hns3_cmd_desc *desc)
2829 struct hns3_dev_specs_0_cmd *req0;
2831 req0 = (struct hns3_dev_specs_0_cmd *)desc[0].data;
2833 hw->max_non_tso_bd_num = req0->max_non_tso_bd_num;
2834 hw->rss_ind_tbl_size = rte_le_to_cpu_16(req0->rss_ind_tbl_size);
2835 hw->rss_key_size = rte_le_to_cpu_16(req0->rss_key_size);
2836 hw->max_tm_rate = rte_le_to_cpu_32(req0->max_tm_rate);
2840 hns3_query_dev_specifications(struct hns3_hw *hw)
2842 struct hns3_cmd_desc desc[HNS3_QUERY_DEV_SPECS_BD_NUM];
2846 for (i = 0; i < HNS3_QUERY_DEV_SPECS_BD_NUM - 1; i++) {
2847 hns3_cmd_setup_basic_desc(&desc[i], HNS3_OPC_QUERY_DEV_SPECS,
2849 desc[i].flag |= rte_cpu_to_le_16(HNS3_CMD_FLAG_NEXT);
2851 hns3_cmd_setup_basic_desc(&desc[i], HNS3_OPC_QUERY_DEV_SPECS, true);
2853 ret = hns3_cmd_send(hw, desc, HNS3_QUERY_DEV_SPECS_BD_NUM);
2857 hns3_parse_dev_specifications(hw, desc);
2863 hns3_get_capability(struct hns3_hw *hw)
2865 struct rte_pci_device *pci_dev;
2866 struct rte_eth_dev *eth_dev;
2871 eth_dev = &rte_eth_devices[hw->data->port_id];
2872 pci_dev = RTE_ETH_DEV_TO_PCI(eth_dev);
2873 device_id = pci_dev->id.device_id;
2875 if (device_id == HNS3_DEV_ID_25GE_RDMA ||
2876 device_id == HNS3_DEV_ID_50GE_RDMA ||
2877 device_id == HNS3_DEV_ID_100G_RDMA_MACSEC ||
2878 device_id == HNS3_DEV_ID_200G_RDMA)
2879 hns3_set_bit(hw->capability, HNS3_DEV_SUPPORT_DCB_B, 1);
2881 /* Get PCI revision id */
2882 ret = rte_pci_read_config(pci_dev, &revision, HNS3_PCI_REVISION_ID_LEN,
2883 HNS3_PCI_REVISION_ID);
2884 if (ret != HNS3_PCI_REVISION_ID_LEN) {
2885 PMD_INIT_LOG(ERR, "failed to read pci revision id, ret = %d",
2889 hw->revision = revision;
2891 if (revision < PCI_REVISION_ID_HIP09_A) {
2892 hns3_set_default_dev_specifications(hw);
2893 hw->intr.mapping_mode = HNS3_INTR_MAPPING_VEC_RSV_ONE;
2894 hw->intr.coalesce_mode = HNS3_INTR_COALESCE_NON_QL;
2895 hw->intr.gl_unit = HNS3_INTR_COALESCE_GL_UINT_2US;
2896 hw->min_tx_pkt_len = HNS3_HIP08_MIN_TX_PKT_LEN;
2900 ret = hns3_query_dev_specifications(hw);
2903 "failed to query dev specifications, ret = %d",
2908 hw->intr.mapping_mode = HNS3_INTR_MAPPING_VEC_ALL;
2909 hw->intr.coalesce_mode = HNS3_INTR_COALESCE_QL;
2910 hw->intr.gl_unit = HNS3_INTR_COALESCE_GL_UINT_1US;
2911 hw->min_tx_pkt_len = HNS3_HIP09_MIN_TX_PKT_LEN;
2917 hns3_get_board_configuration(struct hns3_hw *hw)
2919 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
2920 struct hns3_pf *pf = &hns->pf;
2921 struct hns3_cfg cfg;
2924 ret = hns3_get_board_cfg(hw, &cfg);
2926 PMD_INIT_LOG(ERR, "get board config failed %d", ret);
2930 if (cfg.media_type == HNS3_MEDIA_TYPE_COPPER &&
2931 !hns3_dev_copper_supported(hw)) {
2932 PMD_INIT_LOG(ERR, "media type is copper, not supported.");
2936 hw->mac.media_type = cfg.media_type;
2937 hw->rss_size_max = cfg.rss_size_max;
2938 hw->rss_dis_flag = false;
2939 memcpy(hw->mac.mac_addr, cfg.mac_addr, RTE_ETHER_ADDR_LEN);
2940 hw->mac.phy_addr = cfg.phy_addr;
2941 hw->mac.default_addr_setted = false;
2942 hw->num_tx_desc = cfg.tqp_desc_num;
2943 hw->num_rx_desc = cfg.tqp_desc_num;
2944 hw->dcb_info.num_pg = 1;
2945 hw->dcb_info.hw_pfc_map = 0;
2947 ret = hns3_parse_speed(cfg.default_speed, &hw->mac.link_speed);
2949 PMD_INIT_LOG(ERR, "Get wrong speed %d, ret = %d",
2950 cfg.default_speed, ret);
2954 pf->tc_max = cfg.tc_num;
2955 if (pf->tc_max > HNS3_MAX_TC_NUM || pf->tc_max < 1) {
2956 PMD_INIT_LOG(WARNING,
2957 "Get TC num(%u) from flash, set TC num to 1",
2962 /* Dev does not support DCB */
2963 if (!hns3_dev_dcb_supported(hw)) {
2967 pf->pfc_max = pf->tc_max;
2969 hw->dcb_info.num_tc = 1;
2970 hw->alloc_rss_size = RTE_MIN(hw->rss_size_max,
2971 hw->tqps_num / hw->dcb_info.num_tc);
2972 hns3_set_bit(hw->hw_tc_map, 0, 1);
2973 pf->tx_sch_mode = HNS3_FLAG_TC_BASE_SCH_MODE;
2975 pf->wanted_umv_size = cfg.umv_space;
2981 hns3_get_configuration(struct hns3_hw *hw)
2985 ret = hns3_query_function_status(hw);
2987 PMD_INIT_LOG(ERR, "Failed to query function status: %d.", ret);
2991 /* Get device capability */
2992 ret = hns3_get_capability(hw);
2994 PMD_INIT_LOG(ERR, "failed to get device capability: %d.", ret);
2998 /* Get pf resource */
2999 ret = hns3_query_pf_resource(hw);
3001 PMD_INIT_LOG(ERR, "Failed to query pf resource: %d", ret);
3005 ret = hns3_get_board_configuration(hw);
3007 PMD_INIT_LOG(ERR, "Failed to get board configuration: %d", ret);
3013 hns3_map_tqps_to_func(struct hns3_hw *hw, uint16_t func_id, uint16_t tqp_pid,
3014 uint16_t tqp_vid, bool is_pf)
3016 struct hns3_tqp_map_cmd *req;
3017 struct hns3_cmd_desc desc;
3020 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_SET_TQP_MAP, false);
3022 req = (struct hns3_tqp_map_cmd *)desc.data;
3023 req->tqp_id = rte_cpu_to_le_16(tqp_pid);
3024 req->tqp_vf = func_id;
3025 req->tqp_flag = 1 << HNS3_TQP_MAP_EN_B;
3027 req->tqp_flag |= (1 << HNS3_TQP_MAP_TYPE_B);
3028 req->tqp_vid = rte_cpu_to_le_16(tqp_vid);
3030 ret = hns3_cmd_send(hw, &desc, 1);
3032 PMD_INIT_LOG(ERR, "TQP map failed %d", ret);
3038 hns3_map_tqp(struct hns3_hw *hw)
3040 uint16_t tqps_num = hw->total_tqps_num;
3049 * In current version VF is not supported when PF is driven by DPDK
3050 * driver, so we allocate tqps to PF as much as possible.
3053 num = DIV_ROUND_UP(hw->total_tqps_num, HNS3_MAX_TQP_NUM_PER_FUNC);
3054 for (func_id = HNS3_PF_FUNC_ID; func_id < num; func_id++) {
3055 is_pf = func_id == HNS3_PF_FUNC_ID ? true : false;
3057 i < HNS3_MAX_TQP_NUM_PER_FUNC && tqp_id < tqps_num; i++) {
3058 ret = hns3_map_tqps_to_func(hw, func_id, tqp_id++, i,
3069 hns3_cfg_mac_speed_dup_hw(struct hns3_hw *hw, uint32_t speed, uint8_t duplex)
3071 struct hns3_config_mac_speed_dup_cmd *req;
3072 struct hns3_cmd_desc desc;
3075 req = (struct hns3_config_mac_speed_dup_cmd *)desc.data;
3077 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_CONFIG_SPEED_DUP, false);
3079 hns3_set_bit(req->speed_dup, HNS3_CFG_DUPLEX_B, !!duplex ? 1 : 0);
3082 case ETH_SPEED_NUM_10M:
3083 hns3_set_field(req->speed_dup, HNS3_CFG_SPEED_M,
3084 HNS3_CFG_SPEED_S, HNS3_CFG_SPEED_10M);
3086 case ETH_SPEED_NUM_100M:
3087 hns3_set_field(req->speed_dup, HNS3_CFG_SPEED_M,
3088 HNS3_CFG_SPEED_S, HNS3_CFG_SPEED_100M);
3090 case ETH_SPEED_NUM_1G:
3091 hns3_set_field(req->speed_dup, HNS3_CFG_SPEED_M,
3092 HNS3_CFG_SPEED_S, HNS3_CFG_SPEED_1G);
3094 case ETH_SPEED_NUM_10G:
3095 hns3_set_field(req->speed_dup, HNS3_CFG_SPEED_M,
3096 HNS3_CFG_SPEED_S, HNS3_CFG_SPEED_10G);
3098 case ETH_SPEED_NUM_25G:
3099 hns3_set_field(req->speed_dup, HNS3_CFG_SPEED_M,
3100 HNS3_CFG_SPEED_S, HNS3_CFG_SPEED_25G);
3102 case ETH_SPEED_NUM_40G:
3103 hns3_set_field(req->speed_dup, HNS3_CFG_SPEED_M,
3104 HNS3_CFG_SPEED_S, HNS3_CFG_SPEED_40G);
3106 case ETH_SPEED_NUM_50G:
3107 hns3_set_field(req->speed_dup, HNS3_CFG_SPEED_M,
3108 HNS3_CFG_SPEED_S, HNS3_CFG_SPEED_50G);
3110 case ETH_SPEED_NUM_100G:
3111 hns3_set_field(req->speed_dup, HNS3_CFG_SPEED_M,
3112 HNS3_CFG_SPEED_S, HNS3_CFG_SPEED_100G);
3114 case ETH_SPEED_NUM_200G:
3115 hns3_set_field(req->speed_dup, HNS3_CFG_SPEED_M,
3116 HNS3_CFG_SPEED_S, HNS3_CFG_SPEED_200G);
3119 PMD_INIT_LOG(ERR, "invalid speed (%u)", speed);
3123 hns3_set_bit(req->mac_change_fec_en, HNS3_CFG_MAC_SPEED_CHANGE_EN_B, 1);
3125 ret = hns3_cmd_send(hw, &desc, 1);
3127 PMD_INIT_LOG(ERR, "mac speed/duplex config cmd failed %d", ret);
3133 hns3_tx_buffer_calc(struct hns3_hw *hw, struct hns3_pkt_buf_alloc *buf_alloc)
3135 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
3136 struct hns3_pf *pf = &hns->pf;
3137 struct hns3_priv_buf *priv;
3138 uint32_t i, total_size;
3140 total_size = pf->pkt_buf_size;
3142 /* alloc tx buffer for all enabled tc */
3143 for (i = 0; i < HNS3_MAX_TC_NUM; i++) {
3144 priv = &buf_alloc->priv_buf[i];
3146 if (hw->hw_tc_map & BIT(i)) {
3147 if (total_size < pf->tx_buf_size)
3150 priv->tx_buf_size = pf->tx_buf_size;
3152 priv->tx_buf_size = 0;
3154 total_size -= priv->tx_buf_size;
3161 hns3_tx_buffer_alloc(struct hns3_hw *hw, struct hns3_pkt_buf_alloc *buf_alloc)
3163 /* TX buffer size is unit by 128 byte */
3164 #define HNS3_BUF_SIZE_UNIT_SHIFT 7
3165 #define HNS3_BUF_SIZE_UPDATE_EN_MSK BIT(15)
3166 struct hns3_tx_buff_alloc_cmd *req;
3167 struct hns3_cmd_desc desc;
3172 req = (struct hns3_tx_buff_alloc_cmd *)desc.data;
3174 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_TX_BUFF_ALLOC, 0);
3175 for (i = 0; i < HNS3_MAX_TC_NUM; i++) {
3176 buf_size = buf_alloc->priv_buf[i].tx_buf_size;
3178 buf_size = buf_size >> HNS3_BUF_SIZE_UNIT_SHIFT;
3179 req->tx_pkt_buff[i] = rte_cpu_to_le_16(buf_size |
3180 HNS3_BUF_SIZE_UPDATE_EN_MSK);
3183 ret = hns3_cmd_send(hw, &desc, 1);
3185 PMD_INIT_LOG(ERR, "tx buffer alloc cmd failed %d", ret);
3191 hns3_get_tc_num(struct hns3_hw *hw)
3196 for (i = 0; i < HNS3_MAX_TC_NUM; i++)
3197 if (hw->hw_tc_map & BIT(i))
3203 hns3_get_rx_priv_buff_alloced(struct hns3_pkt_buf_alloc *buf_alloc)
3205 struct hns3_priv_buf *priv;
3206 uint32_t rx_priv = 0;
3209 for (i = 0; i < HNS3_MAX_TC_NUM; i++) {
3210 priv = &buf_alloc->priv_buf[i];
3212 rx_priv += priv->buf_size;
3218 hns3_get_tx_buff_alloced(struct hns3_pkt_buf_alloc *buf_alloc)
3220 uint32_t total_tx_size = 0;
3223 for (i = 0; i < HNS3_MAX_TC_NUM; i++)
3224 total_tx_size += buf_alloc->priv_buf[i].tx_buf_size;
3226 return total_tx_size;
3229 /* Get the number of pfc enabled TCs, which have private buffer */
3231 hns3_get_pfc_priv_num(struct hns3_hw *hw, struct hns3_pkt_buf_alloc *buf_alloc)
3233 struct hns3_priv_buf *priv;
3237 for (i = 0; i < HNS3_MAX_TC_NUM; i++) {
3238 priv = &buf_alloc->priv_buf[i];
3239 if ((hw->dcb_info.hw_pfc_map & BIT(i)) && priv->enable)
3246 /* Get the number of pfc disabled TCs, which have private buffer */
3248 hns3_get_no_pfc_priv_num(struct hns3_hw *hw,
3249 struct hns3_pkt_buf_alloc *buf_alloc)
3251 struct hns3_priv_buf *priv;
3255 for (i = 0; i < HNS3_MAX_TC_NUM; i++) {
3256 priv = &buf_alloc->priv_buf[i];
3257 if (hw->hw_tc_map & BIT(i) &&
3258 !(hw->dcb_info.hw_pfc_map & BIT(i)) && priv->enable)
3266 hns3_is_rx_buf_ok(struct hns3_hw *hw, struct hns3_pkt_buf_alloc *buf_alloc,
3269 uint32_t shared_buf_min, shared_buf_tc, shared_std, hi_thrd, lo_thrd;
3270 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
3271 struct hns3_pf *pf = &hns->pf;
3272 uint32_t shared_buf, aligned_mps;
3277 tc_num = hns3_get_tc_num(hw);
3278 aligned_mps = roundup(pf->mps, HNS3_BUF_SIZE_UNIT);
3280 if (hns3_dev_dcb_supported(hw))
3281 shared_buf_min = HNS3_BUF_MUL_BY * aligned_mps +
3284 shared_buf_min = aligned_mps + HNS3_NON_DCB_ADDITIONAL_BUF
3287 shared_buf_tc = tc_num * aligned_mps + aligned_mps;
3288 shared_std = roundup(RTE_MAX(shared_buf_min, shared_buf_tc),
3289 HNS3_BUF_SIZE_UNIT);
3291 rx_priv = hns3_get_rx_priv_buff_alloced(buf_alloc);
3292 if (rx_all < rx_priv + shared_std)
3295 shared_buf = rounddown(rx_all - rx_priv, HNS3_BUF_SIZE_UNIT);
3296 buf_alloc->s_buf.buf_size = shared_buf;
3297 if (hns3_dev_dcb_supported(hw)) {
3298 buf_alloc->s_buf.self.high = shared_buf - pf->dv_buf_size;
3299 buf_alloc->s_buf.self.low = buf_alloc->s_buf.self.high
3300 - roundup(aligned_mps / HNS3_BUF_DIV_BY,
3301 HNS3_BUF_SIZE_UNIT);
3303 buf_alloc->s_buf.self.high =
3304 aligned_mps + HNS3_NON_DCB_ADDITIONAL_BUF;
3305 buf_alloc->s_buf.self.low = aligned_mps;
3308 if (hns3_dev_dcb_supported(hw)) {
3309 hi_thrd = shared_buf - pf->dv_buf_size;
3311 if (tc_num <= NEED_RESERVE_TC_NUM)
3312 hi_thrd = hi_thrd * BUF_RESERVE_PERCENT
3316 hi_thrd = hi_thrd / tc_num;
3318 hi_thrd = RTE_MAX(hi_thrd, HNS3_BUF_MUL_BY * aligned_mps);
3319 hi_thrd = rounddown(hi_thrd, HNS3_BUF_SIZE_UNIT);
3320 lo_thrd = hi_thrd - aligned_mps / HNS3_BUF_DIV_BY;
3322 hi_thrd = aligned_mps + HNS3_NON_DCB_ADDITIONAL_BUF;
3323 lo_thrd = aligned_mps;
3326 for (i = 0; i < HNS3_MAX_TC_NUM; i++) {
3327 buf_alloc->s_buf.tc_thrd[i].low = lo_thrd;
3328 buf_alloc->s_buf.tc_thrd[i].high = hi_thrd;
3335 hns3_rx_buf_calc_all(struct hns3_hw *hw, bool max,
3336 struct hns3_pkt_buf_alloc *buf_alloc)
3338 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
3339 struct hns3_pf *pf = &hns->pf;
3340 struct hns3_priv_buf *priv;
3341 uint32_t aligned_mps;
3345 rx_all = pf->pkt_buf_size - hns3_get_tx_buff_alloced(buf_alloc);
3346 aligned_mps = roundup(pf->mps, HNS3_BUF_SIZE_UNIT);
3348 for (i = 0; i < HNS3_MAX_TC_NUM; i++) {
3349 priv = &buf_alloc->priv_buf[i];
3356 if (!(hw->hw_tc_map & BIT(i)))
3360 if (hw->dcb_info.hw_pfc_map & BIT(i)) {
3361 priv->wl.low = max ? aligned_mps : HNS3_BUF_SIZE_UNIT;
3362 priv->wl.high = roundup(priv->wl.low + aligned_mps,
3363 HNS3_BUF_SIZE_UNIT);
3366 priv->wl.high = max ? (aligned_mps * HNS3_BUF_MUL_BY) :
3370 priv->buf_size = priv->wl.high + pf->dv_buf_size;
3373 return hns3_is_rx_buf_ok(hw, buf_alloc, rx_all);
3377 hns3_drop_nopfc_buf_till_fit(struct hns3_hw *hw,
3378 struct hns3_pkt_buf_alloc *buf_alloc)
3380 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
3381 struct hns3_pf *pf = &hns->pf;
3382 struct hns3_priv_buf *priv;
3383 int no_pfc_priv_num;
3388 rx_all = pf->pkt_buf_size - hns3_get_tx_buff_alloced(buf_alloc);
3389 no_pfc_priv_num = hns3_get_no_pfc_priv_num(hw, buf_alloc);
3391 /* let the last to be cleared first */
3392 for (i = HNS3_MAX_TC_NUM - 1; i >= 0; i--) {
3393 priv = &buf_alloc->priv_buf[i];
3394 mask = BIT((uint8_t)i);
3396 if (hw->hw_tc_map & mask &&
3397 !(hw->dcb_info.hw_pfc_map & mask)) {
3398 /* Clear the no pfc TC private buffer */
3406 if (hns3_is_rx_buf_ok(hw, buf_alloc, rx_all) ||
3407 no_pfc_priv_num == 0)
3411 return hns3_is_rx_buf_ok(hw, buf_alloc, rx_all);
3415 hns3_drop_pfc_buf_till_fit(struct hns3_hw *hw,
3416 struct hns3_pkt_buf_alloc *buf_alloc)
3418 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
3419 struct hns3_pf *pf = &hns->pf;
3420 struct hns3_priv_buf *priv;
3426 rx_all = pf->pkt_buf_size - hns3_get_tx_buff_alloced(buf_alloc);
3427 pfc_priv_num = hns3_get_pfc_priv_num(hw, buf_alloc);
3429 /* let the last to be cleared first */
3430 for (i = HNS3_MAX_TC_NUM - 1; i >= 0; i--) {
3431 priv = &buf_alloc->priv_buf[i];
3432 mask = BIT((uint8_t)i);
3434 if (hw->hw_tc_map & mask &&
3435 hw->dcb_info.hw_pfc_map & mask) {
3436 /* Reduce the number of pfc TC with private buffer */
3443 if (hns3_is_rx_buf_ok(hw, buf_alloc, rx_all) ||
3448 return hns3_is_rx_buf_ok(hw, buf_alloc, rx_all);
3452 hns3_only_alloc_priv_buff(struct hns3_hw *hw,
3453 struct hns3_pkt_buf_alloc *buf_alloc)
3455 #define COMPENSATE_BUFFER 0x3C00
3456 #define COMPENSATE_HALF_MPS_NUM 5
3457 #define PRIV_WL_GAP 0x1800
3458 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
3459 struct hns3_pf *pf = &hns->pf;
3460 uint32_t tc_num = hns3_get_tc_num(hw);
3461 uint32_t half_mps = pf->mps >> 1;
3462 struct hns3_priv_buf *priv;
3463 uint32_t min_rx_priv;
3467 rx_priv = pf->pkt_buf_size - hns3_get_tx_buff_alloced(buf_alloc);
3469 rx_priv = rx_priv / tc_num;
3471 if (tc_num <= NEED_RESERVE_TC_NUM)
3472 rx_priv = rx_priv * BUF_RESERVE_PERCENT / BUF_MAX_PERCENT;
3475 * Minimum value of private buffer in rx direction (min_rx_priv) is
3476 * equal to "DV + 2.5 * MPS + 15KB". Driver only allocates rx private
3477 * buffer if rx_priv is greater than min_rx_priv.
3479 min_rx_priv = pf->dv_buf_size + COMPENSATE_BUFFER +
3480 COMPENSATE_HALF_MPS_NUM * half_mps;
3481 min_rx_priv = roundup(min_rx_priv, HNS3_BUF_SIZE_UNIT);
3482 rx_priv = rounddown(rx_priv, HNS3_BUF_SIZE_UNIT);
3484 if (rx_priv < min_rx_priv)
3487 for (i = 0; i < HNS3_MAX_TC_NUM; i++) {
3488 priv = &buf_alloc->priv_buf[i];
3495 if (!(hw->hw_tc_map & BIT(i)))
3499 priv->buf_size = rx_priv;
3500 priv->wl.high = rx_priv - pf->dv_buf_size;
3501 priv->wl.low = priv->wl.high - PRIV_WL_GAP;
3504 buf_alloc->s_buf.buf_size = 0;
3510 * hns3_rx_buffer_calc: calculate the rx private buffer size for all TCs
3511 * @hw: pointer to struct hns3_hw
3512 * @buf_alloc: pointer to buffer calculation data
3513 * @return: 0: calculate sucessful, negative: fail
3516 hns3_rx_buffer_calc(struct hns3_hw *hw, struct hns3_pkt_buf_alloc *buf_alloc)
3518 /* When DCB is not supported, rx private buffer is not allocated. */
3519 if (!hns3_dev_dcb_supported(hw)) {
3520 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
3521 struct hns3_pf *pf = &hns->pf;
3522 uint32_t rx_all = pf->pkt_buf_size;
3524 rx_all -= hns3_get_tx_buff_alloced(buf_alloc);
3525 if (!hns3_is_rx_buf_ok(hw, buf_alloc, rx_all))
3532 * Try to allocate privated packet buffer for all TCs without share
3535 if (hns3_only_alloc_priv_buff(hw, buf_alloc))
3539 * Try to allocate privated packet buffer for all TCs with share
3542 if (hns3_rx_buf_calc_all(hw, true, buf_alloc))
3546 * For different application scenes, the enabled port number, TC number
3547 * and no_drop TC number are different. In order to obtain the better
3548 * performance, software could allocate the buffer size and configure
3549 * the waterline by tring to decrease the private buffer size according
3550 * to the order, namely, waterline of valided tc, pfc disabled tc, pfc
3553 if (hns3_rx_buf_calc_all(hw, false, buf_alloc))
3556 if (hns3_drop_nopfc_buf_till_fit(hw, buf_alloc))
3559 if (hns3_drop_pfc_buf_till_fit(hw, buf_alloc))
3566 hns3_rx_priv_buf_alloc(struct hns3_hw *hw, struct hns3_pkt_buf_alloc *buf_alloc)
3568 struct hns3_rx_priv_buff_cmd *req;
3569 struct hns3_cmd_desc desc;
3574 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_RX_PRIV_BUFF_ALLOC, false);
3575 req = (struct hns3_rx_priv_buff_cmd *)desc.data;
3577 /* Alloc private buffer TCs */
3578 for (i = 0; i < HNS3_MAX_TC_NUM; i++) {
3579 struct hns3_priv_buf *priv = &buf_alloc->priv_buf[i];
3582 rte_cpu_to_le_16(priv->buf_size >> HNS3_BUF_UNIT_S);
3583 req->buf_num[i] |= rte_cpu_to_le_16(1 << HNS3_TC0_PRI_BUF_EN_B);
3586 buf_size = buf_alloc->s_buf.buf_size;
3587 req->shared_buf = rte_cpu_to_le_16((buf_size >> HNS3_BUF_UNIT_S) |
3588 (1 << HNS3_TC0_PRI_BUF_EN_B));
3590 ret = hns3_cmd_send(hw, &desc, 1);
3592 PMD_INIT_LOG(ERR, "rx private buffer alloc cmd failed %d", ret);
3598 hns3_rx_priv_wl_config(struct hns3_hw *hw, struct hns3_pkt_buf_alloc *buf_alloc)
3600 #define HNS3_RX_PRIV_WL_ALLOC_DESC_NUM 2
3601 struct hns3_rx_priv_wl_buf *req;
3602 struct hns3_priv_buf *priv;
3603 struct hns3_cmd_desc desc[HNS3_RX_PRIV_WL_ALLOC_DESC_NUM];
3607 for (i = 0; i < HNS3_RX_PRIV_WL_ALLOC_DESC_NUM; i++) {
3608 hns3_cmd_setup_basic_desc(&desc[i], HNS3_OPC_RX_PRIV_WL_ALLOC,
3610 req = (struct hns3_rx_priv_wl_buf *)desc[i].data;
3612 /* The first descriptor set the NEXT bit to 1 */
3614 desc[i].flag |= rte_cpu_to_le_16(HNS3_CMD_FLAG_NEXT);
3616 desc[i].flag &= ~rte_cpu_to_le_16(HNS3_CMD_FLAG_NEXT);
3618 for (j = 0; j < HNS3_TC_NUM_ONE_DESC; j++) {
3619 uint32_t idx = i * HNS3_TC_NUM_ONE_DESC + j;
3621 priv = &buf_alloc->priv_buf[idx];
3622 req->tc_wl[j].high = rte_cpu_to_le_16(priv->wl.high >>
3624 req->tc_wl[j].high |=
3625 rte_cpu_to_le_16(BIT(HNS3_RX_PRIV_EN_B));
3626 req->tc_wl[j].low = rte_cpu_to_le_16(priv->wl.low >>
3628 req->tc_wl[j].low |=
3629 rte_cpu_to_le_16(BIT(HNS3_RX_PRIV_EN_B));
3633 /* Send 2 descriptor at one time */
3634 ret = hns3_cmd_send(hw, desc, HNS3_RX_PRIV_WL_ALLOC_DESC_NUM);
3636 PMD_INIT_LOG(ERR, "rx private waterline config cmd failed %d",
3642 hns3_common_thrd_config(struct hns3_hw *hw,
3643 struct hns3_pkt_buf_alloc *buf_alloc)
3645 #define HNS3_RX_COM_THRD_ALLOC_DESC_NUM 2
3646 struct hns3_shared_buf *s_buf = &buf_alloc->s_buf;
3647 struct hns3_rx_com_thrd *req;
3648 struct hns3_cmd_desc desc[HNS3_RX_COM_THRD_ALLOC_DESC_NUM];
3649 struct hns3_tc_thrd *tc;
3654 for (i = 0; i < HNS3_RX_COM_THRD_ALLOC_DESC_NUM; i++) {
3655 hns3_cmd_setup_basic_desc(&desc[i], HNS3_OPC_RX_COM_THRD_ALLOC,
3657 req = (struct hns3_rx_com_thrd *)&desc[i].data;
3659 /* The first descriptor set the NEXT bit to 1 */
3661 desc[i].flag |= rte_cpu_to_le_16(HNS3_CMD_FLAG_NEXT);
3663 desc[i].flag &= ~rte_cpu_to_le_16(HNS3_CMD_FLAG_NEXT);
3665 for (j = 0; j < HNS3_TC_NUM_ONE_DESC; j++) {
3666 tc_idx = i * HNS3_TC_NUM_ONE_DESC + j;
3667 tc = &s_buf->tc_thrd[tc_idx];
3669 req->com_thrd[j].high =
3670 rte_cpu_to_le_16(tc->high >> HNS3_BUF_UNIT_S);
3671 req->com_thrd[j].high |=
3672 rte_cpu_to_le_16(BIT(HNS3_RX_PRIV_EN_B));
3673 req->com_thrd[j].low =
3674 rte_cpu_to_le_16(tc->low >> HNS3_BUF_UNIT_S);
3675 req->com_thrd[j].low |=
3676 rte_cpu_to_le_16(BIT(HNS3_RX_PRIV_EN_B));
3680 /* Send 2 descriptors at one time */
3681 ret = hns3_cmd_send(hw, desc, HNS3_RX_COM_THRD_ALLOC_DESC_NUM);
3683 PMD_INIT_LOG(ERR, "common threshold config cmd failed %d", ret);
3689 hns3_common_wl_config(struct hns3_hw *hw, struct hns3_pkt_buf_alloc *buf_alloc)
3691 struct hns3_shared_buf *buf = &buf_alloc->s_buf;
3692 struct hns3_rx_com_wl *req;
3693 struct hns3_cmd_desc desc;
3696 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_RX_COM_WL_ALLOC, false);
3698 req = (struct hns3_rx_com_wl *)desc.data;
3699 req->com_wl.high = rte_cpu_to_le_16(buf->self.high >> HNS3_BUF_UNIT_S);
3700 req->com_wl.high |= rte_cpu_to_le_16(BIT(HNS3_RX_PRIV_EN_B));
3702 req->com_wl.low = rte_cpu_to_le_16(buf->self.low >> HNS3_BUF_UNIT_S);
3703 req->com_wl.low |= rte_cpu_to_le_16(BIT(HNS3_RX_PRIV_EN_B));
3705 ret = hns3_cmd_send(hw, &desc, 1);
3707 PMD_INIT_LOG(ERR, "common waterline config cmd failed %d", ret);
3713 hns3_buffer_alloc(struct hns3_hw *hw)
3715 struct hns3_pkt_buf_alloc pkt_buf;
3718 memset(&pkt_buf, 0, sizeof(pkt_buf));
3719 ret = hns3_tx_buffer_calc(hw, &pkt_buf);
3722 "could not calc tx buffer size for all TCs %d",
3727 ret = hns3_tx_buffer_alloc(hw, &pkt_buf);
3729 PMD_INIT_LOG(ERR, "could not alloc tx buffers %d", ret);
3733 ret = hns3_rx_buffer_calc(hw, &pkt_buf);
3736 "could not calc rx priv buffer size for all TCs %d",
3741 ret = hns3_rx_priv_buf_alloc(hw, &pkt_buf);
3743 PMD_INIT_LOG(ERR, "could not alloc rx priv buffer %d", ret);
3747 if (hns3_dev_dcb_supported(hw)) {
3748 ret = hns3_rx_priv_wl_config(hw, &pkt_buf);
3751 "could not configure rx private waterline %d",
3756 ret = hns3_common_thrd_config(hw, &pkt_buf);
3759 "could not configure common threshold %d",
3765 ret = hns3_common_wl_config(hw, &pkt_buf);
3767 PMD_INIT_LOG(ERR, "could not configure common waterline %d",
3774 hns3_mac_init(struct hns3_hw *hw)
3776 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
3777 struct hns3_mac *mac = &hw->mac;
3778 struct hns3_pf *pf = &hns->pf;
3781 pf->support_sfp_query = true;
3782 mac->link_duplex = ETH_LINK_FULL_DUPLEX;
3783 ret = hns3_cfg_mac_speed_dup_hw(hw, mac->link_speed, mac->link_duplex);
3785 PMD_INIT_LOG(ERR, "Config mac speed dup fail ret = %d", ret);
3789 mac->link_status = ETH_LINK_DOWN;
3791 return hns3_config_mtu(hw, pf->mps);
3795 hns3_get_mac_ethertype_cmd_status(uint16_t cmdq_resp, uint8_t resp_code)
3797 #define HNS3_ETHERTYPE_SUCCESS_ADD 0
3798 #define HNS3_ETHERTYPE_ALREADY_ADD 1
3799 #define HNS3_ETHERTYPE_MGR_TBL_OVERFLOW 2
3800 #define HNS3_ETHERTYPE_KEY_CONFLICT 3
3805 "cmdq execute failed for get_mac_ethertype_cmd_status, status=%d.\n",
3810 switch (resp_code) {
3811 case HNS3_ETHERTYPE_SUCCESS_ADD:
3812 case HNS3_ETHERTYPE_ALREADY_ADD:
3815 case HNS3_ETHERTYPE_MGR_TBL_OVERFLOW:
3817 "add mac ethertype failed for manager table overflow.");
3818 return_status = -EIO;
3820 case HNS3_ETHERTYPE_KEY_CONFLICT:
3821 PMD_INIT_LOG(ERR, "add mac ethertype failed for key conflict.");
3822 return_status = -EIO;
3826 "add mac ethertype failed for undefined, code=%d.",
3828 return_status = -EIO;
3832 return return_status;
3836 hns3_add_mgr_tbl(struct hns3_hw *hw,
3837 const struct hns3_mac_mgr_tbl_entry_cmd *req)
3839 struct hns3_cmd_desc desc;
3844 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_MAC_ETHTYPE_ADD, false);
3845 memcpy(desc.data, req, sizeof(struct hns3_mac_mgr_tbl_entry_cmd));
3847 ret = hns3_cmd_send(hw, &desc, 1);
3850 "add mac ethertype failed for cmd_send, ret =%d.",
3855 resp_code = (rte_le_to_cpu_32(desc.data[0]) >> 8) & 0xff;
3856 retval = rte_le_to_cpu_16(desc.retval);
3858 return hns3_get_mac_ethertype_cmd_status(retval, resp_code);
3862 hns3_prepare_mgr_tbl(struct hns3_mac_mgr_tbl_entry_cmd *mgr_table,
3863 int *table_item_num)
3865 struct hns3_mac_mgr_tbl_entry_cmd *tbl;
3868 * In current version, we add one item in management table as below:
3869 * 0x0180C200000E -- LLDP MC address
3872 tbl->flags = HNS3_MAC_MGR_MASK_VLAN_B;
3873 tbl->ethter_type = rte_cpu_to_le_16(HNS3_MAC_ETHERTYPE_LLDP);
3874 tbl->mac_addr_hi32 = rte_cpu_to_le_32(htonl(0x0180C200));
3875 tbl->mac_addr_lo16 = rte_cpu_to_le_16(htons(0x000E));
3876 tbl->i_port_bitmap = 0x1;
3877 *table_item_num = 1;
3881 hns3_init_mgr_tbl(struct hns3_hw *hw)
3883 #define HNS_MAC_MGR_TBL_MAX_SIZE 16
3884 struct hns3_mac_mgr_tbl_entry_cmd mgr_table[HNS_MAC_MGR_TBL_MAX_SIZE];
3889 memset(mgr_table, 0, sizeof(mgr_table));
3890 hns3_prepare_mgr_tbl(mgr_table, &table_item_num);
3891 for (i = 0; i < table_item_num; i++) {
3892 ret = hns3_add_mgr_tbl(hw, &mgr_table[i]);
3894 PMD_INIT_LOG(ERR, "add mac ethertype failed, ret =%d",
3904 hns3_promisc_param_init(struct hns3_promisc_param *param, bool en_uc,
3905 bool en_mc, bool en_bc, int vport_id)
3910 memset(param, 0, sizeof(struct hns3_promisc_param));
3912 param->enable = HNS3_PROMISC_EN_UC;
3914 param->enable |= HNS3_PROMISC_EN_MC;
3916 param->enable |= HNS3_PROMISC_EN_BC;
3917 param->vf_id = vport_id;
3921 hns3_cmd_set_promisc_mode(struct hns3_hw *hw, struct hns3_promisc_param *param)
3923 struct hns3_promisc_cfg_cmd *req;
3924 struct hns3_cmd_desc desc;
3927 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_CFG_PROMISC_MODE, false);
3929 req = (struct hns3_promisc_cfg_cmd *)desc.data;
3930 req->vf_id = param->vf_id;
3931 req->flag = (param->enable << HNS3_PROMISC_EN_B) |
3932 HNS3_PROMISC_TX_EN_B | HNS3_PROMISC_RX_EN_B;
3934 ret = hns3_cmd_send(hw, &desc, 1);
3936 PMD_INIT_LOG(ERR, "Set promisc mode fail, ret = %d", ret);
3942 hns3_set_promisc_mode(struct hns3_hw *hw, bool en_uc_pmc, bool en_mc_pmc)
3944 struct hns3_promisc_param param;
3945 bool en_bc_pmc = true;
3949 * In current version VF is not supported when PF is driven by DPDK
3950 * driver, just need to configure parameters for PF vport.
3952 vf_id = HNS3_PF_FUNC_ID;
3954 hns3_promisc_param_init(¶m, en_uc_pmc, en_mc_pmc, en_bc_pmc, vf_id);
3955 return hns3_cmd_set_promisc_mode(hw, ¶m);
3959 hns3_promisc_init(struct hns3_hw *hw)
3961 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
3962 struct hns3_pf *pf = &hns->pf;
3963 struct hns3_promisc_param param;
3967 ret = hns3_set_promisc_mode(hw, false, false);
3969 PMD_INIT_LOG(ERR, "failed to set promisc mode, ret = %d", ret);
3974 * In current version VFs are not supported when PF is driven by DPDK
3975 * driver. After PF has been taken over by DPDK, the original VF will
3976 * be invalid. So, there is a possibility of entry residues. It should
3977 * clear VFs's promisc mode to avoid unnecessary bandwidth usage
3980 for (func_id = HNS3_1ST_VF_FUNC_ID; func_id < pf->func_num; func_id++) {
3981 hns3_promisc_param_init(¶m, false, false, false, func_id);
3982 ret = hns3_cmd_set_promisc_mode(hw, ¶m);
3984 PMD_INIT_LOG(ERR, "failed to clear vf:%d promisc mode,"
3985 " ret = %d", func_id, ret);
3994 hns3_promisc_uninit(struct hns3_hw *hw)
3996 struct hns3_promisc_param param;
4000 func_id = HNS3_PF_FUNC_ID;
4003 * In current version VFs are not supported when PF is driven by
4004 * DPDK driver, and VFs' promisc mode status has been cleared during
4005 * init and their status will not change. So just clear PF's promisc
4006 * mode status during uninit.
4008 hns3_promisc_param_init(¶m, false, false, false, func_id);
4009 ret = hns3_cmd_set_promisc_mode(hw, ¶m);
4011 PMD_INIT_LOG(ERR, "failed to clear promisc status during"
4012 " uninit, ret = %d", ret);
4016 hns3_dev_promiscuous_enable(struct rte_eth_dev *dev)
4018 bool allmulti = dev->data->all_multicast ? true : false;
4019 struct hns3_adapter *hns = dev->data->dev_private;
4020 struct hns3_hw *hw = &hns->hw;
4025 rte_spinlock_lock(&hw->lock);
4026 ret = hns3_set_promisc_mode(hw, true, true);
4028 rte_spinlock_unlock(&hw->lock);
4029 hns3_err(hw, "failed to enable promiscuous mode, ret = %d",
4035 * When promiscuous mode was enabled, disable the vlan filter to let
4036 * all packets coming in in the receiving direction.
4038 offloads = dev->data->dev_conf.rxmode.offloads;
4039 if (offloads & DEV_RX_OFFLOAD_VLAN_FILTER) {
4040 ret = hns3_enable_vlan_filter(hns, false);
4042 hns3_err(hw, "failed to enable promiscuous mode due to "
4043 "failure to disable vlan filter, ret = %d",
4045 err = hns3_set_promisc_mode(hw, false, allmulti);
4047 hns3_err(hw, "failed to restore promiscuous "
4048 "status after disable vlan filter "
4049 "failed during enabling promiscuous "
4050 "mode, ret = %d", ret);
4054 rte_spinlock_unlock(&hw->lock);
4060 hns3_dev_promiscuous_disable(struct rte_eth_dev *dev)
4062 bool allmulti = dev->data->all_multicast ? true : false;
4063 struct hns3_adapter *hns = dev->data->dev_private;
4064 struct hns3_hw *hw = &hns->hw;
4069 /* If now in all_multicast mode, must remain in all_multicast mode. */
4070 rte_spinlock_lock(&hw->lock);
4071 ret = hns3_set_promisc_mode(hw, false, allmulti);
4073 rte_spinlock_unlock(&hw->lock);
4074 hns3_err(hw, "failed to disable promiscuous mode, ret = %d",
4078 /* when promiscuous mode was disabled, restore the vlan filter status */
4079 offloads = dev->data->dev_conf.rxmode.offloads;
4080 if (offloads & DEV_RX_OFFLOAD_VLAN_FILTER) {
4081 ret = hns3_enable_vlan_filter(hns, true);
4083 hns3_err(hw, "failed to disable promiscuous mode due to"
4084 " failure to restore vlan filter, ret = %d",
4086 err = hns3_set_promisc_mode(hw, true, true);
4088 hns3_err(hw, "failed to restore promiscuous "
4089 "status after enabling vlan filter "
4090 "failed during disabling promiscuous "
4091 "mode, ret = %d", ret);
4094 rte_spinlock_unlock(&hw->lock);
4100 hns3_dev_allmulticast_enable(struct rte_eth_dev *dev)
4102 struct hns3_adapter *hns = dev->data->dev_private;
4103 struct hns3_hw *hw = &hns->hw;
4106 if (dev->data->promiscuous)
4109 rte_spinlock_lock(&hw->lock);
4110 ret = hns3_set_promisc_mode(hw, false, true);
4111 rte_spinlock_unlock(&hw->lock);
4113 hns3_err(hw, "failed to enable allmulticast mode, ret = %d",
4120 hns3_dev_allmulticast_disable(struct rte_eth_dev *dev)
4122 struct hns3_adapter *hns = dev->data->dev_private;
4123 struct hns3_hw *hw = &hns->hw;
4126 /* If now in promiscuous mode, must remain in all_multicast mode. */
4127 if (dev->data->promiscuous)
4130 rte_spinlock_lock(&hw->lock);
4131 ret = hns3_set_promisc_mode(hw, false, false);
4132 rte_spinlock_unlock(&hw->lock);
4134 hns3_err(hw, "failed to disable allmulticast mode, ret = %d",
4141 hns3_dev_promisc_restore(struct hns3_adapter *hns)
4143 struct hns3_hw *hw = &hns->hw;
4144 bool allmulti = hw->data->all_multicast ? true : false;
4147 if (hw->data->promiscuous) {
4148 ret = hns3_set_promisc_mode(hw, true, true);
4150 hns3_err(hw, "failed to restore promiscuous mode, "
4155 ret = hns3_set_promisc_mode(hw, false, allmulti);
4157 hns3_err(hw, "failed to restore allmulticast mode, ret = %d",
4163 hns3_get_sfp_speed(struct hns3_hw *hw, uint32_t *speed)
4165 struct hns3_sfp_speed_cmd *resp;
4166 struct hns3_cmd_desc desc;
4169 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_SFP_GET_SPEED, true);
4170 resp = (struct hns3_sfp_speed_cmd *)desc.data;
4171 ret = hns3_cmd_send(hw, &desc, 1);
4172 if (ret == -EOPNOTSUPP) {
4173 hns3_err(hw, "IMP do not support get SFP speed %d", ret);
4176 hns3_err(hw, "get sfp speed failed %d", ret);
4180 *speed = resp->sfp_speed;
4186 hns3_check_speed_dup(uint8_t duplex, uint32_t speed)
4188 if (!(speed == ETH_SPEED_NUM_10M || speed == ETH_SPEED_NUM_100M))
4189 duplex = ETH_LINK_FULL_DUPLEX;
4195 hns3_cfg_mac_speed_dup(struct hns3_hw *hw, uint32_t speed, uint8_t duplex)
4197 struct hns3_mac *mac = &hw->mac;
4200 duplex = hns3_check_speed_dup(duplex, speed);
4201 if (mac->link_speed == speed && mac->link_duplex == duplex)
4204 ret = hns3_cfg_mac_speed_dup_hw(hw, speed, duplex);
4208 mac->link_speed = speed;
4209 mac->link_duplex = duplex;
4215 hns3_update_speed_duplex(struct rte_eth_dev *eth_dev)
4217 struct hns3_adapter *hns = eth_dev->data->dev_private;
4218 struct hns3_hw *hw = &hns->hw;
4219 struct hns3_pf *pf = &hns->pf;
4223 /* If IMP do not support get SFP/qSFP speed, return directly */
4224 if (!pf->support_sfp_query)
4227 ret = hns3_get_sfp_speed(hw, &speed);
4228 if (ret == -EOPNOTSUPP) {
4229 pf->support_sfp_query = false;
4234 if (speed == ETH_SPEED_NUM_NONE)
4235 return 0; /* do nothing if no SFP */
4237 /* Config full duplex for SFP */
4238 return hns3_cfg_mac_speed_dup(hw, speed, ETH_LINK_FULL_DUPLEX);
4242 hns3_cfg_mac_mode(struct hns3_hw *hw, bool enable)
4244 struct hns3_config_mac_mode_cmd *req;
4245 struct hns3_cmd_desc desc;
4246 uint32_t loop_en = 0;
4250 req = (struct hns3_config_mac_mode_cmd *)desc.data;
4252 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_CONFIG_MAC_MODE, false);
4255 hns3_set_bit(loop_en, HNS3_MAC_TX_EN_B, val);
4256 hns3_set_bit(loop_en, HNS3_MAC_RX_EN_B, val);
4257 hns3_set_bit(loop_en, HNS3_MAC_PAD_TX_B, val);
4258 hns3_set_bit(loop_en, HNS3_MAC_PAD_RX_B, val);
4259 hns3_set_bit(loop_en, HNS3_MAC_1588_TX_B, 0);
4260 hns3_set_bit(loop_en, HNS3_MAC_1588_RX_B, 0);
4261 hns3_set_bit(loop_en, HNS3_MAC_APP_LP_B, 0);
4262 hns3_set_bit(loop_en, HNS3_MAC_LINE_LP_B, 0);
4263 hns3_set_bit(loop_en, HNS3_MAC_FCS_TX_B, val);
4264 hns3_set_bit(loop_en, HNS3_MAC_RX_FCS_B, val);
4267 * If DEV_RX_OFFLOAD_KEEP_CRC offload is set, MAC will not strip CRC
4268 * when receiving frames. Otherwise, CRC will be stripped.
4270 if (hw->data->dev_conf.rxmode.offloads & DEV_RX_OFFLOAD_KEEP_CRC)
4271 hns3_set_bit(loop_en, HNS3_MAC_RX_FCS_STRIP_B, 0);
4273 hns3_set_bit(loop_en, HNS3_MAC_RX_FCS_STRIP_B, val);
4274 hns3_set_bit(loop_en, HNS3_MAC_TX_OVERSIZE_TRUNCATE_B, val);
4275 hns3_set_bit(loop_en, HNS3_MAC_RX_OVERSIZE_TRUNCATE_B, val);
4276 hns3_set_bit(loop_en, HNS3_MAC_TX_UNDER_MIN_ERR_B, val);
4277 req->txrx_pad_fcs_loop_en = rte_cpu_to_le_32(loop_en);
4279 ret = hns3_cmd_send(hw, &desc, 1);
4281 PMD_INIT_LOG(ERR, "mac enable fail, ret =%d.", ret);
4287 hns3_get_mac_link_status(struct hns3_hw *hw)
4289 struct hns3_link_status_cmd *req;
4290 struct hns3_cmd_desc desc;
4294 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_QUERY_LINK_STATUS, true);
4295 ret = hns3_cmd_send(hw, &desc, 1);
4297 hns3_err(hw, "get link status cmd failed %d", ret);
4298 return ETH_LINK_DOWN;
4301 req = (struct hns3_link_status_cmd *)desc.data;
4302 link_status = req->status & HNS3_LINK_STATUS_UP_M;
4304 return !!link_status;
4308 hns3_update_link_status(struct hns3_hw *hw)
4312 state = hns3_get_mac_link_status(hw);
4313 if (state != hw->mac.link_status) {
4314 hw->mac.link_status = state;
4315 hns3_warn(hw, "Link status change to %s!", state ? "up" : "down");
4320 hns3_service_handler(void *param)
4322 struct rte_eth_dev *eth_dev = (struct rte_eth_dev *)param;
4323 struct hns3_adapter *hns = eth_dev->data->dev_private;
4324 struct hns3_hw *hw = &hns->hw;
4326 if (!hns3_is_reset_pending(hns)) {
4327 hns3_update_speed_duplex(eth_dev);
4328 hns3_update_link_status(hw);
4330 hns3_warn(hw, "Cancel the query when reset is pending");
4332 rte_eal_alarm_set(HNS3_SERVICE_INTERVAL, hns3_service_handler, eth_dev);
4336 hns3_init_hardware(struct hns3_adapter *hns)
4338 struct hns3_hw *hw = &hns->hw;
4341 ret = hns3_map_tqp(hw);
4343 PMD_INIT_LOG(ERR, "Failed to map tqp: %d", ret);
4347 ret = hns3_init_umv_space(hw);
4349 PMD_INIT_LOG(ERR, "Failed to init umv space: %d", ret);
4353 ret = hns3_mac_init(hw);
4355 PMD_INIT_LOG(ERR, "Failed to init MAC: %d", ret);
4359 ret = hns3_init_mgr_tbl(hw);
4361 PMD_INIT_LOG(ERR, "Failed to init manager table: %d", ret);
4365 ret = hns3_promisc_init(hw);
4367 PMD_INIT_LOG(ERR, "Failed to init promisc: %d",
4372 ret = hns3_init_vlan_config(hns);
4374 PMD_INIT_LOG(ERR, "Failed to init vlan: %d", ret);
4378 ret = hns3_dcb_init(hw);
4380 PMD_INIT_LOG(ERR, "Failed to init dcb: %d", ret);
4384 ret = hns3_init_fd_config(hns);
4386 PMD_INIT_LOG(ERR, "Failed to init flow director: %d", ret);
4390 ret = hns3_config_tso(hw, HNS3_TSO_MSS_MIN, HNS3_TSO_MSS_MAX);
4392 PMD_INIT_LOG(ERR, "Failed to config tso: %d", ret);
4396 ret = hns3_config_gro(hw, false);
4398 PMD_INIT_LOG(ERR, "Failed to config gro: %d", ret);
4403 * In the initialization clearing the all hardware mapping relationship
4404 * configurations between queues and interrupt vectors is needed, so
4405 * some error caused by the residual configurations, such as the
4406 * unexpected interrupt, can be avoid.
4408 ret = hns3_init_ring_with_vector(hw);
4410 PMD_INIT_LOG(ERR, "Failed to init ring intr vector: %d", ret);
4417 hns3_uninit_umv_space(hw);
4422 hns3_clear_hw(struct hns3_hw *hw)
4424 struct hns3_cmd_desc desc;
4427 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_CLEAR_HW_STATE, false);
4429 ret = hns3_cmd_send(hw, &desc, 1);
4430 if (ret && ret != -EOPNOTSUPP)
4437 hns3_config_all_msix_error(struct hns3_hw *hw, bool enable)
4442 * The new firmware support report more hardware error types by
4443 * msix mode. These errors are defined as RAS errors in hardware
4444 * and belong to a different type from the MSI-x errors processed
4445 * by the network driver.
4447 * Network driver should open the new error report on initialition
4449 val = hns3_read_dev(hw, HNS3_VECTOR0_OTER_EN_REG);
4450 hns3_set_bit(val, HNS3_VECTOR0_ALL_MSIX_ERR_B, enable ? 1 : 0);
4451 hns3_write_dev(hw, HNS3_VECTOR0_OTER_EN_REG, val);
4455 hns3_init_pf(struct rte_eth_dev *eth_dev)
4457 struct rte_device *dev = eth_dev->device;
4458 struct rte_pci_device *pci_dev = RTE_DEV_TO_PCI(dev);
4459 struct hns3_adapter *hns = eth_dev->data->dev_private;
4460 struct hns3_hw *hw = &hns->hw;
4463 PMD_INIT_FUNC_TRACE();
4465 /* Get hardware io base address from pcie BAR2 IO space */
4466 hw->io_base = pci_dev->mem_resource[2].addr;
4468 /* Firmware command queue initialize */
4469 ret = hns3_cmd_init_queue(hw);
4471 PMD_INIT_LOG(ERR, "Failed to init cmd queue: %d", ret);
4472 goto err_cmd_init_queue;
4475 hns3_clear_all_event_cause(hw);
4477 /* Firmware command initialize */
4478 ret = hns3_cmd_init(hw);
4480 PMD_INIT_LOG(ERR, "Failed to init cmd: %d", ret);
4485 * To ensure that the hardware environment is clean during
4486 * initialization, the driver actively clear the hardware environment
4487 * during initialization, including PF and corresponding VFs' vlan, mac,
4488 * flow table configurations, etc.
4490 ret = hns3_clear_hw(hw);
4492 PMD_INIT_LOG(ERR, "failed to clear hardware: %d", ret);
4496 hns3_config_all_msix_error(hw, true);
4498 ret = rte_intr_callback_register(&pci_dev->intr_handle,
4499 hns3_interrupt_handler,
4502 PMD_INIT_LOG(ERR, "Failed to register intr: %d", ret);
4503 goto err_intr_callback_register;
4506 /* Enable interrupt */
4507 rte_intr_enable(&pci_dev->intr_handle);
4508 hns3_pf_enable_irq0(hw);
4510 /* Get configuration */
4511 ret = hns3_get_configuration(hw);
4513 PMD_INIT_LOG(ERR, "Failed to fetch configuration: %d", ret);
4514 goto err_get_config;
4517 ret = hns3_init_hardware(hns);
4519 PMD_INIT_LOG(ERR, "Failed to init hardware: %d", ret);
4520 goto err_get_config;
4523 /* Initialize flow director filter list & hash */
4524 ret = hns3_fdir_filter_init(hns);
4526 PMD_INIT_LOG(ERR, "Failed to alloc hashmap for fdir: %d", ret);
4530 hns3_set_default_rss_args(hw);
4532 ret = hns3_enable_hw_error_intr(hns, true);
4534 PMD_INIT_LOG(ERR, "fail to enable hw error interrupts: %d",
4542 hns3_fdir_filter_uninit(hns);
4544 hns3_uninit_umv_space(hw);
4547 hns3_pf_disable_irq0(hw);
4548 rte_intr_disable(&pci_dev->intr_handle);
4549 hns3_intr_unregister(&pci_dev->intr_handle, hns3_interrupt_handler,
4551 err_intr_callback_register:
4553 hns3_cmd_uninit(hw);
4554 hns3_cmd_destroy_queue(hw);
4562 hns3_uninit_pf(struct rte_eth_dev *eth_dev)
4564 struct hns3_adapter *hns = eth_dev->data->dev_private;
4565 struct rte_device *dev = eth_dev->device;
4566 struct rte_pci_device *pci_dev = RTE_DEV_TO_PCI(dev);
4567 struct hns3_hw *hw = &hns->hw;
4569 PMD_INIT_FUNC_TRACE();
4571 hns3_enable_hw_error_intr(hns, false);
4572 hns3_rss_uninit(hns);
4573 (void)hns3_config_gro(hw, false);
4574 hns3_promisc_uninit(hw);
4575 hns3_fdir_filter_uninit(hns);
4576 hns3_uninit_umv_space(hw);
4577 hns3_pf_disable_irq0(hw);
4578 rte_intr_disable(&pci_dev->intr_handle);
4579 hns3_intr_unregister(&pci_dev->intr_handle, hns3_interrupt_handler,
4581 hns3_config_all_msix_error(hw, false);
4582 hns3_cmd_uninit(hw);
4583 hns3_cmd_destroy_queue(hw);
4588 hns3_do_start(struct hns3_adapter *hns, bool reset_queue)
4590 struct hns3_hw *hw = &hns->hw;
4593 ret = hns3_dcb_cfg_update(hns);
4598 ret = hns3_start_queues(hns, reset_queue);
4600 PMD_INIT_LOG(ERR, "Failed to start queues: %d", ret);
4605 ret = hns3_cfg_mac_mode(hw, true);
4607 PMD_INIT_LOG(ERR, "Failed to enable MAC: %d", ret);
4608 goto err_config_mac_mode;
4612 err_config_mac_mode:
4613 hns3_stop_queues(hns, true);
4618 hns3_map_rx_interrupt(struct rte_eth_dev *dev)
4620 struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
4621 struct rte_intr_handle *intr_handle = &pci_dev->intr_handle;
4622 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
4623 uint8_t base = RTE_INTR_VEC_ZERO_OFFSET;
4624 uint8_t vec = RTE_INTR_VEC_ZERO_OFFSET;
4625 uint32_t intr_vector;
4629 if (dev->data->dev_conf.intr_conf.rxq == 0)
4632 /* disable uio/vfio intr/eventfd mapping */
4633 rte_intr_disable(intr_handle);
4635 /* check and configure queue intr-vector mapping */
4636 if (rte_intr_cap_multiple(intr_handle) ||
4637 !RTE_ETH_DEV_SRIOV(dev).active) {
4638 intr_vector = hw->used_rx_queues;
4639 /* creates event fd for each intr vector when MSIX is used */
4640 if (rte_intr_efd_enable(intr_handle, intr_vector))
4643 if (rte_intr_dp_is_en(intr_handle) && !intr_handle->intr_vec) {
4644 intr_handle->intr_vec =
4645 rte_zmalloc("intr_vec",
4646 hw->used_rx_queues * sizeof(int), 0);
4647 if (intr_handle->intr_vec == NULL) {
4648 hns3_err(hw, "Failed to allocate %d rx_queues"
4649 " intr_vec", hw->used_rx_queues);
4651 goto alloc_intr_vec_error;
4655 if (rte_intr_allow_others(intr_handle)) {
4656 vec = RTE_INTR_VEC_RXTX_OFFSET;
4657 base = RTE_INTR_VEC_RXTX_OFFSET;
4659 if (rte_intr_dp_is_en(intr_handle)) {
4660 for (q_id = 0; q_id < hw->used_rx_queues; q_id++) {
4661 ret = hns3_bind_ring_with_vector(hw, vec, true,
4665 goto bind_vector_error;
4666 intr_handle->intr_vec[q_id] = vec;
4667 if (vec < base + intr_handle->nb_efd - 1)
4671 rte_intr_enable(intr_handle);
4675 rte_intr_efd_disable(intr_handle);
4676 if (intr_handle->intr_vec) {
4677 free(intr_handle->intr_vec);
4678 intr_handle->intr_vec = NULL;
4681 alloc_intr_vec_error:
4682 rte_intr_efd_disable(intr_handle);
4687 hns3_restore_rx_interrupt(struct hns3_hw *hw)
4689 struct rte_eth_dev *dev = &rte_eth_devices[hw->data->port_id];
4690 struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
4691 struct rte_intr_handle *intr_handle = &pci_dev->intr_handle;
4695 if (dev->data->dev_conf.intr_conf.rxq == 0)
4698 if (rte_intr_dp_is_en(intr_handle)) {
4699 for (q_id = 0; q_id < hw->used_rx_queues; q_id++) {
4700 ret = hns3_bind_ring_with_vector(hw,
4701 intr_handle->intr_vec[q_id], true,
4702 HNS3_RING_TYPE_RX, q_id);
4712 hns3_restore_filter(struct rte_eth_dev *dev)
4714 hns3_restore_rss_filter(dev);
4718 hns3_dev_start(struct rte_eth_dev *dev)
4720 struct hns3_adapter *hns = dev->data->dev_private;
4721 struct hns3_hw *hw = &hns->hw;
4724 PMD_INIT_FUNC_TRACE();
4725 if (rte_atomic16_read(&hw->reset.resetting))
4728 rte_spinlock_lock(&hw->lock);
4729 hw->adapter_state = HNS3_NIC_STARTING;
4731 ret = hns3_do_start(hns, true);
4733 hw->adapter_state = HNS3_NIC_CONFIGURED;
4734 rte_spinlock_unlock(&hw->lock);
4737 ret = hns3_map_rx_interrupt(dev);
4739 hw->adapter_state = HNS3_NIC_CONFIGURED;
4740 rte_spinlock_unlock(&hw->lock);
4744 hw->adapter_state = HNS3_NIC_STARTED;
4745 rte_spinlock_unlock(&hw->lock);
4747 hns3_set_rxtx_function(dev);
4748 hns3_mp_req_start_rxtx(dev);
4749 rte_eal_alarm_set(HNS3_SERVICE_INTERVAL, hns3_service_handler, dev);
4751 hns3_restore_filter(dev);
4753 /* Enable interrupt of all rx queues before enabling queues */
4754 hns3_dev_all_rx_queue_intr_enable(hw, true);
4756 * When finished the initialization, enable queues to receive/transmit
4759 hns3_enable_all_queues(hw, true);
4761 hns3_info(hw, "hns3 dev start successful!");
4766 hns3_do_stop(struct hns3_adapter *hns)
4768 struct hns3_hw *hw = &hns->hw;
4772 ret = hns3_cfg_mac_mode(hw, false);
4775 hw->mac.link_status = ETH_LINK_DOWN;
4777 if (rte_atomic16_read(&hw->reset.disable_cmd) == 0) {
4778 hns3_configure_all_mac_addr(hns, true);
4781 reset_queue = false;
4782 hw->mac.default_addr_setted = false;
4783 return hns3_stop_queues(hns, reset_queue);
4787 hns3_unmap_rx_interrupt(struct rte_eth_dev *dev)
4789 struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
4790 struct rte_intr_handle *intr_handle = &pci_dev->intr_handle;
4791 struct hns3_adapter *hns = dev->data->dev_private;
4792 struct hns3_hw *hw = &hns->hw;
4793 uint8_t base = RTE_INTR_VEC_ZERO_OFFSET;
4794 uint8_t vec = RTE_INTR_VEC_ZERO_OFFSET;
4797 if (dev->data->dev_conf.intr_conf.rxq == 0)
4800 /* unmap the ring with vector */
4801 if (rte_intr_allow_others(intr_handle)) {
4802 vec = RTE_INTR_VEC_RXTX_OFFSET;
4803 base = RTE_INTR_VEC_RXTX_OFFSET;
4805 if (rte_intr_dp_is_en(intr_handle)) {
4806 for (q_id = 0; q_id < hw->used_rx_queues; q_id++) {
4807 (void)hns3_bind_ring_with_vector(hw, vec, false,
4810 if (vec < base + intr_handle->nb_efd - 1)
4814 /* Clean datapath event and queue/vec mapping */
4815 rte_intr_efd_disable(intr_handle);
4816 if (intr_handle->intr_vec) {
4817 rte_free(intr_handle->intr_vec);
4818 intr_handle->intr_vec = NULL;
4823 hns3_dev_stop(struct rte_eth_dev *dev)
4825 struct hns3_adapter *hns = dev->data->dev_private;
4826 struct hns3_hw *hw = &hns->hw;
4828 PMD_INIT_FUNC_TRACE();
4830 hw->adapter_state = HNS3_NIC_STOPPING;
4831 hns3_set_rxtx_function(dev);
4833 /* Disable datapath on secondary process. */
4834 hns3_mp_req_stop_rxtx(dev);
4835 /* Prevent crashes when queues are still in use. */
4836 rte_delay_ms(hw->tqps_num);
4838 rte_spinlock_lock(&hw->lock);
4839 if (rte_atomic16_read(&hw->reset.resetting) == 0) {
4841 hns3_unmap_rx_interrupt(dev);
4842 hns3_dev_release_mbufs(hns);
4843 hw->adapter_state = HNS3_NIC_CONFIGURED;
4845 rte_eal_alarm_cancel(hns3_service_handler, dev);
4846 rte_spinlock_unlock(&hw->lock);
4850 hns3_dev_close(struct rte_eth_dev *eth_dev)
4852 struct hns3_adapter *hns = eth_dev->data->dev_private;
4853 struct hns3_hw *hw = &hns->hw;
4855 if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
4856 rte_free(eth_dev->process_private);
4857 eth_dev->process_private = NULL;
4861 if (hw->adapter_state == HNS3_NIC_STARTED)
4862 hns3_dev_stop(eth_dev);
4864 hw->adapter_state = HNS3_NIC_CLOSING;
4865 hns3_reset_abort(hns);
4866 hw->adapter_state = HNS3_NIC_CLOSED;
4868 hns3_configure_all_mc_mac_addr(hns, true);
4869 hns3_remove_all_vlan_table(hns);
4870 hns3_vlan_txvlan_cfg(hns, HNS3_PORT_BASE_VLAN_DISABLE, 0);
4871 hns3_uninit_pf(eth_dev);
4872 hns3_free_all_queues(eth_dev);
4873 rte_free(hw->reset.wait_data);
4874 rte_free(eth_dev->process_private);
4875 eth_dev->process_private = NULL;
4876 hns3_mp_uninit_primary();
4877 hns3_warn(hw, "Close port %d finished", hw->data->port_id);
4881 hns3_flow_ctrl_get(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
4883 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
4884 struct hns3_pf *pf = HNS3_DEV_PRIVATE_TO_PF(dev->data->dev_private);
4886 fc_conf->pause_time = pf->pause_time;
4888 /* return fc current mode */
4889 switch (hw->current_mode) {
4891 fc_conf->mode = RTE_FC_FULL;
4893 case HNS3_FC_TX_PAUSE:
4894 fc_conf->mode = RTE_FC_TX_PAUSE;
4896 case HNS3_FC_RX_PAUSE:
4897 fc_conf->mode = RTE_FC_RX_PAUSE;
4901 fc_conf->mode = RTE_FC_NONE;
4909 hns3_get_fc_mode(struct hns3_hw *hw, enum rte_eth_fc_mode mode)
4913 hw->requested_mode = HNS3_FC_NONE;
4915 case RTE_FC_RX_PAUSE:
4916 hw->requested_mode = HNS3_FC_RX_PAUSE;
4918 case RTE_FC_TX_PAUSE:
4919 hw->requested_mode = HNS3_FC_TX_PAUSE;
4922 hw->requested_mode = HNS3_FC_FULL;
4925 hw->requested_mode = HNS3_FC_NONE;
4926 hns3_warn(hw, "fc_mode(%u) exceeds member scope and is "
4927 "configured to RTE_FC_NONE", mode);
4933 hns3_flow_ctrl_set(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
4935 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
4936 struct hns3_pf *pf = HNS3_DEV_PRIVATE_TO_PF(dev->data->dev_private);
4939 if (fc_conf->high_water || fc_conf->low_water ||
4940 fc_conf->send_xon || fc_conf->mac_ctrl_frame_fwd) {
4941 hns3_err(hw, "Unsupported flow control settings specified, "
4942 "high_water(%u), low_water(%u), send_xon(%u) and "
4943 "mac_ctrl_frame_fwd(%u) must be set to '0'",
4944 fc_conf->high_water, fc_conf->low_water,
4945 fc_conf->send_xon, fc_conf->mac_ctrl_frame_fwd);
4948 if (fc_conf->autoneg) {
4949 hns3_err(hw, "Unsupported fc auto-negotiation setting.");
4952 if (!fc_conf->pause_time) {
4953 hns3_err(hw, "Invalid pause time %d setting.",
4954 fc_conf->pause_time);
4958 if (!(hw->current_fc_status == HNS3_FC_STATUS_NONE ||
4959 hw->current_fc_status == HNS3_FC_STATUS_MAC_PAUSE)) {
4960 hns3_err(hw, "PFC is enabled. Cannot set MAC pause. "
4961 "current_fc_status = %d", hw->current_fc_status);
4965 hns3_get_fc_mode(hw, fc_conf->mode);
4966 if (hw->requested_mode == hw->current_mode &&
4967 pf->pause_time == fc_conf->pause_time)
4970 rte_spinlock_lock(&hw->lock);
4971 ret = hns3_fc_enable(dev, fc_conf);
4972 rte_spinlock_unlock(&hw->lock);
4978 hns3_priority_flow_ctrl_set(struct rte_eth_dev *dev,
4979 struct rte_eth_pfc_conf *pfc_conf)
4981 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
4982 struct hns3_pf *pf = HNS3_DEV_PRIVATE_TO_PF(dev->data->dev_private);
4986 if (!hns3_dev_dcb_supported(hw)) {
4987 hns3_err(hw, "This port does not support dcb configurations.");
4991 if (pfc_conf->fc.high_water || pfc_conf->fc.low_water ||
4992 pfc_conf->fc.send_xon || pfc_conf->fc.mac_ctrl_frame_fwd) {
4993 hns3_err(hw, "Unsupported flow control settings specified, "
4994 "high_water(%u), low_water(%u), send_xon(%u) and "
4995 "mac_ctrl_frame_fwd(%u) must be set to '0'",
4996 pfc_conf->fc.high_water, pfc_conf->fc.low_water,
4997 pfc_conf->fc.send_xon,
4998 pfc_conf->fc.mac_ctrl_frame_fwd);
5001 if (pfc_conf->fc.autoneg) {
5002 hns3_err(hw, "Unsupported fc auto-negotiation setting.");
5005 if (pfc_conf->fc.pause_time == 0) {
5006 hns3_err(hw, "Invalid pause time %d setting.",
5007 pfc_conf->fc.pause_time);
5011 if (!(hw->current_fc_status == HNS3_FC_STATUS_NONE ||
5012 hw->current_fc_status == HNS3_FC_STATUS_PFC)) {
5013 hns3_err(hw, "MAC pause is enabled. Cannot set PFC."
5014 "current_fc_status = %d", hw->current_fc_status);
5018 priority = pfc_conf->priority;
5019 hns3_get_fc_mode(hw, pfc_conf->fc.mode);
5020 if (hw->dcb_info.pfc_en & BIT(priority) &&
5021 hw->requested_mode == hw->current_mode &&
5022 pfc_conf->fc.pause_time == pf->pause_time)
5025 rte_spinlock_lock(&hw->lock);
5026 ret = hns3_dcb_pfc_enable(dev, pfc_conf);
5027 rte_spinlock_unlock(&hw->lock);
5033 hns3_get_dcb_info(struct rte_eth_dev *dev, struct rte_eth_dcb_info *dcb_info)
5035 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
5036 struct hns3_pf *pf = HNS3_DEV_PRIVATE_TO_PF(dev->data->dev_private);
5037 enum rte_eth_rx_mq_mode mq_mode = dev->data->dev_conf.rxmode.mq_mode;
5040 rte_spinlock_lock(&hw->lock);
5041 if ((uint32_t)mq_mode & ETH_MQ_RX_DCB_FLAG)
5042 dcb_info->nb_tcs = pf->local_max_tc;
5044 dcb_info->nb_tcs = 1;
5046 for (i = 0; i < HNS3_MAX_USER_PRIO; i++)
5047 dcb_info->prio_tc[i] = hw->dcb_info.prio_tc[i];
5048 for (i = 0; i < dcb_info->nb_tcs; i++)
5049 dcb_info->tc_bws[i] = hw->dcb_info.pg_info[0].tc_dwrr[i];
5051 for (i = 0; i < hw->num_tc; i++) {
5052 dcb_info->tc_queue.tc_rxq[0][i].base = hw->alloc_rss_size * i;
5053 dcb_info->tc_queue.tc_txq[0][i].base =
5054 hw->tc_queue[i].tqp_offset;
5055 dcb_info->tc_queue.tc_rxq[0][i].nb_queue = hw->alloc_rss_size;
5056 dcb_info->tc_queue.tc_txq[0][i].nb_queue =
5057 hw->tc_queue[i].tqp_count;
5059 rte_spinlock_unlock(&hw->lock);
5065 hns3_reinit_dev(struct hns3_adapter *hns)
5067 struct hns3_hw *hw = &hns->hw;
5070 ret = hns3_cmd_init(hw);
5072 hns3_err(hw, "Failed to init cmd: %d", ret);
5076 ret = hns3_reset_all_queues(hns);
5078 hns3_err(hw, "Failed to reset all queues: %d", ret);
5082 ret = hns3_init_hardware(hns);
5084 hns3_err(hw, "Failed to init hardware: %d", ret);
5088 ret = hns3_enable_hw_error_intr(hns, true);
5090 hns3_err(hw, "fail to enable hw error interrupts: %d",
5094 hns3_info(hw, "Reset done, driver initialization finished.");
5100 is_pf_reset_done(struct hns3_hw *hw)
5102 uint32_t val, reg, reg_bit;
5104 switch (hw->reset.level) {
5105 case HNS3_IMP_RESET:
5106 reg = HNS3_GLOBAL_RESET_REG;
5107 reg_bit = HNS3_IMP_RESET_BIT;
5109 case HNS3_GLOBAL_RESET:
5110 reg = HNS3_GLOBAL_RESET_REG;
5111 reg_bit = HNS3_GLOBAL_RESET_BIT;
5113 case HNS3_FUNC_RESET:
5114 reg = HNS3_FUN_RST_ING;
5115 reg_bit = HNS3_FUN_RST_ING_B;
5117 case HNS3_FLR_RESET:
5119 hns3_err(hw, "Wait for unsupported reset level: %d",
5123 val = hns3_read_dev(hw, reg);
5124 if (hns3_get_bit(val, reg_bit))
5131 hns3_is_reset_pending(struct hns3_adapter *hns)
5133 struct hns3_hw *hw = &hns->hw;
5134 enum hns3_reset_level reset;
5136 hns3_check_event_cause(hns, NULL);
5137 reset = hns3_get_reset_level(hns, &hw->reset.pending);
5138 if (hw->reset.level != HNS3_NONE_RESET && hw->reset.level < reset) {
5139 hns3_warn(hw, "High level reset %d is pending", reset);
5142 reset = hns3_get_reset_level(hns, &hw->reset.request);
5143 if (hw->reset.level != HNS3_NONE_RESET && hw->reset.level < reset) {
5144 hns3_warn(hw, "High level reset %d is request", reset);
5151 hns3_wait_hardware_ready(struct hns3_adapter *hns)
5153 struct hns3_hw *hw = &hns->hw;
5154 struct hns3_wait_data *wait_data = hw->reset.wait_data;
5157 if (wait_data->result == HNS3_WAIT_SUCCESS)
5159 else if (wait_data->result == HNS3_WAIT_TIMEOUT) {
5160 gettimeofday(&tv, NULL);
5161 hns3_warn(hw, "Reset step4 hardware not ready after reset time=%ld.%.6ld",
5162 tv.tv_sec, tv.tv_usec);
5164 } else if (wait_data->result == HNS3_WAIT_REQUEST)
5167 wait_data->hns = hns;
5168 wait_data->check_completion = is_pf_reset_done;
5169 wait_data->end_ms = (uint64_t)HNS3_RESET_WAIT_CNT *
5170 HNS3_RESET_WAIT_MS + get_timeofday_ms();
5171 wait_data->interval = HNS3_RESET_WAIT_MS * USEC_PER_MSEC;
5172 wait_data->count = HNS3_RESET_WAIT_CNT;
5173 wait_data->result = HNS3_WAIT_REQUEST;
5174 rte_eal_alarm_set(wait_data->interval, hns3_wait_callback, wait_data);
5179 hns3_func_reset_cmd(struct hns3_hw *hw, int func_id)
5181 struct hns3_cmd_desc desc;
5182 struct hns3_reset_cmd *req = (struct hns3_reset_cmd *)desc.data;
5184 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_CFG_RST_TRIGGER, false);
5185 hns3_set_bit(req->mac_func_reset, HNS3_CFG_RESET_FUNC_B, 1);
5186 req->fun_reset_vfid = func_id;
5188 return hns3_cmd_send(hw, &desc, 1);
5192 hns3_imp_reset_cmd(struct hns3_hw *hw)
5194 struct hns3_cmd_desc desc;
5196 hns3_cmd_setup_basic_desc(&desc, 0xFFFE, false);
5197 desc.data[0] = 0xeedd;
5199 return hns3_cmd_send(hw, &desc, 1);
5203 hns3_msix_process(struct hns3_adapter *hns, enum hns3_reset_level reset_level)
5205 struct hns3_hw *hw = &hns->hw;
5209 gettimeofday(&tv, NULL);
5210 if (hns3_read_dev(hw, HNS3_GLOBAL_RESET_REG) ||
5211 hns3_read_dev(hw, HNS3_FUN_RST_ING)) {
5212 hns3_warn(hw, "Don't process msix during resetting time=%ld.%.6ld",
5213 tv.tv_sec, tv.tv_usec);
5217 switch (reset_level) {
5218 case HNS3_IMP_RESET:
5219 hns3_imp_reset_cmd(hw);
5220 hns3_warn(hw, "IMP Reset requested time=%ld.%.6ld",
5221 tv.tv_sec, tv.tv_usec);
5223 case HNS3_GLOBAL_RESET:
5224 val = hns3_read_dev(hw, HNS3_GLOBAL_RESET_REG);
5225 hns3_set_bit(val, HNS3_GLOBAL_RESET_BIT, 1);
5226 hns3_write_dev(hw, HNS3_GLOBAL_RESET_REG, val);
5227 hns3_warn(hw, "Global Reset requested time=%ld.%.6ld",
5228 tv.tv_sec, tv.tv_usec);
5230 case HNS3_FUNC_RESET:
5231 hns3_warn(hw, "PF Reset requested time=%ld.%.6ld",
5232 tv.tv_sec, tv.tv_usec);
5233 /* schedule again to check later */
5234 hns3_atomic_set_bit(HNS3_FUNC_RESET, &hw->reset.pending);
5235 hns3_schedule_reset(hns);
5238 hns3_warn(hw, "Unsupported reset level: %d", reset_level);
5241 hns3_atomic_clear_bit(reset_level, &hw->reset.request);
5244 static enum hns3_reset_level
5245 hns3_get_reset_level(struct hns3_adapter *hns, uint64_t *levels)
5247 struct hns3_hw *hw = &hns->hw;
5248 enum hns3_reset_level reset_level = HNS3_NONE_RESET;
5250 /* Return the highest priority reset level amongst all */
5251 if (hns3_atomic_test_bit(HNS3_IMP_RESET, levels))
5252 reset_level = HNS3_IMP_RESET;
5253 else if (hns3_atomic_test_bit(HNS3_GLOBAL_RESET, levels))
5254 reset_level = HNS3_GLOBAL_RESET;
5255 else if (hns3_atomic_test_bit(HNS3_FUNC_RESET, levels))
5256 reset_level = HNS3_FUNC_RESET;
5257 else if (hns3_atomic_test_bit(HNS3_FLR_RESET, levels))
5258 reset_level = HNS3_FLR_RESET;
5260 if (hw->reset.level != HNS3_NONE_RESET && reset_level < hw->reset.level)
5261 return HNS3_NONE_RESET;
5267 hns3_record_imp_error(struct hns3_adapter *hns)
5269 struct hns3_hw *hw = &hns->hw;
5272 reg_val = hns3_read_dev(hw, HNS3_VECTOR0_OTER_EN_REG);
5273 if (hns3_get_bit(reg_val, HNS3_VECTOR0_IMP_RD_POISON_B)) {
5274 hns3_warn(hw, "Detected IMP RD poison!");
5275 hns3_error_int_stats_add(hns, "IMP_RD_POISON_INT_STS");
5276 hns3_set_bit(reg_val, HNS3_VECTOR0_IMP_RD_POISON_B, 0);
5277 hns3_write_dev(hw, HNS3_VECTOR0_OTER_EN_REG, reg_val);
5280 if (hns3_get_bit(reg_val, HNS3_VECTOR0_IMP_CMDQ_ERR_B)) {
5281 hns3_warn(hw, "Detected IMP CMDQ error!");
5282 hns3_error_int_stats_add(hns, "CMDQ_MEM_ECC_INT_STS");
5283 hns3_set_bit(reg_val, HNS3_VECTOR0_IMP_CMDQ_ERR_B, 0);
5284 hns3_write_dev(hw, HNS3_VECTOR0_OTER_EN_REG, reg_val);
5289 hns3_prepare_reset(struct hns3_adapter *hns)
5291 struct hns3_hw *hw = &hns->hw;
5295 switch (hw->reset.level) {
5296 case HNS3_FUNC_RESET:
5297 ret = hns3_func_reset_cmd(hw, HNS3_PF_FUNC_ID);
5302 * After performaning pf reset, it is not necessary to do the
5303 * mailbox handling or send any command to firmware, because
5304 * any mailbox handling or command to firmware is only valid
5305 * after hns3_cmd_init is called.
5307 rte_atomic16_set(&hw->reset.disable_cmd, 1);
5308 hw->reset.stats.request_cnt++;
5310 case HNS3_IMP_RESET:
5311 hns3_record_imp_error(hns);
5312 reg_val = hns3_read_dev(hw, HNS3_VECTOR0_OTER_EN_REG);
5313 hns3_write_dev(hw, HNS3_VECTOR0_OTER_EN_REG, reg_val |
5314 BIT(HNS3_VECTOR0_IMP_RESET_INT_B));
5323 hns3_set_rst_done(struct hns3_hw *hw)
5325 struct hns3_pf_rst_done_cmd *req;
5326 struct hns3_cmd_desc desc;
5328 req = (struct hns3_pf_rst_done_cmd *)desc.data;
5329 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_PF_RST_DONE, false);
5330 req->pf_rst_done |= HNS3_PF_RESET_DONE_BIT;
5331 return hns3_cmd_send(hw, &desc, 1);
5335 hns3_stop_service(struct hns3_adapter *hns)
5337 struct hns3_hw *hw = &hns->hw;
5338 struct rte_eth_dev *eth_dev;
5340 eth_dev = &rte_eth_devices[hw->data->port_id];
5341 if (hw->adapter_state == HNS3_NIC_STARTED)
5342 rte_eal_alarm_cancel(hns3_service_handler, eth_dev);
5343 hw->mac.link_status = ETH_LINK_DOWN;
5345 hns3_set_rxtx_function(eth_dev);
5347 /* Disable datapath on secondary process. */
5348 hns3_mp_req_stop_rxtx(eth_dev);
5349 rte_delay_ms(hw->tqps_num);
5351 rte_spinlock_lock(&hw->lock);
5352 if (hns->hw.adapter_state == HNS3_NIC_STARTED ||
5353 hw->adapter_state == HNS3_NIC_STOPPING) {
5355 hw->reset.mbuf_deferred_free = true;
5357 hw->reset.mbuf_deferred_free = false;
5360 * It is cumbersome for hardware to pick-and-choose entries for deletion
5361 * from table space. Hence, for function reset software intervention is
5362 * required to delete the entries
5364 if (rte_atomic16_read(&hw->reset.disable_cmd) == 0)
5365 hns3_configure_all_mc_mac_addr(hns, true);
5366 rte_spinlock_unlock(&hw->lock);
5372 hns3_start_service(struct hns3_adapter *hns)
5374 struct hns3_hw *hw = &hns->hw;
5375 struct rte_eth_dev *eth_dev;
5377 if (hw->reset.level == HNS3_IMP_RESET ||
5378 hw->reset.level == HNS3_GLOBAL_RESET)
5379 hns3_set_rst_done(hw);
5380 eth_dev = &rte_eth_devices[hw->data->port_id];
5381 hns3_set_rxtx_function(eth_dev);
5382 hns3_mp_req_start_rxtx(eth_dev);
5383 if (hw->adapter_state == HNS3_NIC_STARTED) {
5384 hns3_service_handler(eth_dev);
5386 /* Enable interrupt of all rx queues before enabling queues */
5387 hns3_dev_all_rx_queue_intr_enable(hw, true);
5389 * When finished the initialization, enable queues to receive
5390 * and transmit packets.
5392 hns3_enable_all_queues(hw, true);
5399 hns3_restore_conf(struct hns3_adapter *hns)
5401 struct hns3_hw *hw = &hns->hw;
5404 ret = hns3_configure_all_mac_addr(hns, false);
5408 ret = hns3_configure_all_mc_mac_addr(hns, false);
5412 ret = hns3_dev_promisc_restore(hns);
5416 ret = hns3_restore_vlan_table(hns);
5420 ret = hns3_restore_vlan_conf(hns);
5424 ret = hns3_restore_all_fdir_filter(hns);
5428 ret = hns3_restore_rx_interrupt(hw);
5432 ret = hns3_restore_gro_conf(hw);
5436 if (hns->hw.adapter_state == HNS3_NIC_STARTED) {
5437 ret = hns3_do_start(hns, false);
5440 hns3_info(hw, "hns3 dev restart successful!");
5441 } else if (hw->adapter_state == HNS3_NIC_STOPPING)
5442 hw->adapter_state = HNS3_NIC_CONFIGURED;
5446 hns3_configure_all_mc_mac_addr(hns, true);
5448 hns3_configure_all_mac_addr(hns, true);
5453 hns3_reset_service(void *param)
5455 struct hns3_adapter *hns = (struct hns3_adapter *)param;
5456 struct hns3_hw *hw = &hns->hw;
5457 enum hns3_reset_level reset_level;
5458 struct timeval tv_delta;
5459 struct timeval tv_start;
5465 * The interrupt is not triggered within the delay time.
5466 * The interrupt may have been lost. It is necessary to handle
5467 * the interrupt to recover from the error.
5469 if (rte_atomic16_read(&hns->hw.reset.schedule) == SCHEDULE_DEFERRED) {
5470 rte_atomic16_set(&hns->hw.reset.schedule, SCHEDULE_REQUESTED);
5471 hns3_err(hw, "Handling interrupts in delayed tasks");
5472 hns3_interrupt_handler(&rte_eth_devices[hw->data->port_id]);
5473 reset_level = hns3_get_reset_level(hns, &hw->reset.pending);
5474 if (reset_level == HNS3_NONE_RESET) {
5475 hns3_err(hw, "No reset level is set, try IMP reset");
5476 hns3_atomic_set_bit(HNS3_IMP_RESET, &hw->reset.pending);
5479 rte_atomic16_set(&hns->hw.reset.schedule, SCHEDULE_NONE);
5482 * Check if there is any ongoing reset in the hardware. This status can
5483 * be checked from reset_pending. If there is then, we need to wait for
5484 * hardware to complete reset.
5485 * a. If we are able to figure out in reasonable time that hardware
5486 * has fully resetted then, we can proceed with driver, client
5488 * b. else, we can come back later to check this status so re-sched
5491 reset_level = hns3_get_reset_level(hns, &hw->reset.pending);
5492 if (reset_level != HNS3_NONE_RESET) {
5493 gettimeofday(&tv_start, NULL);
5494 ret = hns3_reset_process(hns, reset_level);
5495 gettimeofday(&tv, NULL);
5496 timersub(&tv, &tv_start, &tv_delta);
5497 msec = tv_delta.tv_sec * MSEC_PER_SEC +
5498 tv_delta.tv_usec / USEC_PER_MSEC;
5499 if (msec > HNS3_RESET_PROCESS_MS)
5500 hns3_err(hw, "%d handle long time delta %" PRIx64
5501 " ms time=%ld.%.6ld",
5502 hw->reset.level, msec,
5503 tv.tv_sec, tv.tv_usec);
5508 /* Check if we got any *new* reset requests to be honored */
5509 reset_level = hns3_get_reset_level(hns, &hw->reset.request);
5510 if (reset_level != HNS3_NONE_RESET)
5511 hns3_msix_process(hns, reset_level);
5514 static const struct eth_dev_ops hns3_eth_dev_ops = {
5515 .dev_start = hns3_dev_start,
5516 .dev_stop = hns3_dev_stop,
5517 .dev_close = hns3_dev_close,
5518 .promiscuous_enable = hns3_dev_promiscuous_enable,
5519 .promiscuous_disable = hns3_dev_promiscuous_disable,
5520 .allmulticast_enable = hns3_dev_allmulticast_enable,
5521 .allmulticast_disable = hns3_dev_allmulticast_disable,
5522 .mtu_set = hns3_dev_mtu_set,
5523 .stats_get = hns3_stats_get,
5524 .stats_reset = hns3_stats_reset,
5525 .xstats_get = hns3_dev_xstats_get,
5526 .xstats_get_names = hns3_dev_xstats_get_names,
5527 .xstats_reset = hns3_dev_xstats_reset,
5528 .xstats_get_by_id = hns3_dev_xstats_get_by_id,
5529 .xstats_get_names_by_id = hns3_dev_xstats_get_names_by_id,
5530 .dev_infos_get = hns3_dev_infos_get,
5531 .fw_version_get = hns3_fw_version_get,
5532 .rx_queue_setup = hns3_rx_queue_setup,
5533 .tx_queue_setup = hns3_tx_queue_setup,
5534 .rx_queue_release = hns3_dev_rx_queue_release,
5535 .tx_queue_release = hns3_dev_tx_queue_release,
5536 .rx_queue_intr_enable = hns3_dev_rx_queue_intr_enable,
5537 .rx_queue_intr_disable = hns3_dev_rx_queue_intr_disable,
5538 .rxq_info_get = hns3_rxq_info_get,
5539 .txq_info_get = hns3_txq_info_get,
5540 .dev_configure = hns3_dev_configure,
5541 .flow_ctrl_get = hns3_flow_ctrl_get,
5542 .flow_ctrl_set = hns3_flow_ctrl_set,
5543 .priority_flow_ctrl_set = hns3_priority_flow_ctrl_set,
5544 .mac_addr_add = hns3_add_mac_addr,
5545 .mac_addr_remove = hns3_remove_mac_addr,
5546 .mac_addr_set = hns3_set_default_mac_addr,
5547 .set_mc_addr_list = hns3_set_mc_mac_addr_list,
5548 .link_update = hns3_dev_link_update,
5549 .rss_hash_update = hns3_dev_rss_hash_update,
5550 .rss_hash_conf_get = hns3_dev_rss_hash_conf_get,
5551 .reta_update = hns3_dev_rss_reta_update,
5552 .reta_query = hns3_dev_rss_reta_query,
5553 .filter_ctrl = hns3_dev_filter_ctrl,
5554 .vlan_filter_set = hns3_vlan_filter_set,
5555 .vlan_tpid_set = hns3_vlan_tpid_set,
5556 .vlan_offload_set = hns3_vlan_offload_set,
5557 .vlan_pvid_set = hns3_vlan_pvid_set,
5558 .get_reg = hns3_get_regs,
5559 .get_dcb_info = hns3_get_dcb_info,
5560 .dev_supported_ptypes_get = hns3_dev_supported_ptypes_get,
5563 static const struct hns3_reset_ops hns3_reset_ops = {
5564 .reset_service = hns3_reset_service,
5565 .stop_service = hns3_stop_service,
5566 .prepare_reset = hns3_prepare_reset,
5567 .wait_hardware_ready = hns3_wait_hardware_ready,
5568 .reinit_dev = hns3_reinit_dev,
5569 .restore_conf = hns3_restore_conf,
5570 .start_service = hns3_start_service,
5574 hns3_dev_init(struct rte_eth_dev *eth_dev)
5576 struct hns3_adapter *hns = eth_dev->data->dev_private;
5577 struct hns3_hw *hw = &hns->hw;
5580 PMD_INIT_FUNC_TRACE();
5582 eth_dev->process_private = (struct hns3_process_private *)
5583 rte_zmalloc_socket("hns3_filter_list",
5584 sizeof(struct hns3_process_private),
5585 RTE_CACHE_LINE_SIZE, eth_dev->device->numa_node);
5586 if (eth_dev->process_private == NULL) {
5587 PMD_INIT_LOG(ERR, "Failed to alloc memory for process private");
5590 /* initialize flow filter lists */
5591 hns3_filterlist_init(eth_dev);
5593 hns3_set_rxtx_function(eth_dev);
5594 eth_dev->dev_ops = &hns3_eth_dev_ops;
5595 if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
5596 ret = hns3_mp_init_secondary();
5598 PMD_INIT_LOG(ERR, "Failed to init for secondary "
5599 "process, ret = %d", ret);
5600 goto err_mp_init_secondary;
5603 hw->secondary_cnt++;
5607 ret = hns3_mp_init_primary();
5610 "Failed to init for primary process, ret = %d",
5612 goto err_mp_init_primary;
5615 hw->adapter_state = HNS3_NIC_UNINITIALIZED;
5617 hw->data = eth_dev->data;
5620 * Set default max packet size according to the mtu
5621 * default vale in DPDK frame.
5623 hns->pf.mps = hw->data->mtu + HNS3_ETH_OVERHEAD;
5625 ret = hns3_reset_init(hw);
5627 goto err_init_reset;
5628 hw->reset.ops = &hns3_reset_ops;
5630 ret = hns3_init_pf(eth_dev);
5632 PMD_INIT_LOG(ERR, "Failed to init pf: %d", ret);
5636 /* Allocate memory for storing MAC addresses */
5637 eth_dev->data->mac_addrs = rte_zmalloc("hns3-mac",
5638 sizeof(struct rte_ether_addr) *
5639 HNS3_UC_MACADDR_NUM, 0);
5640 if (eth_dev->data->mac_addrs == NULL) {
5641 PMD_INIT_LOG(ERR, "Failed to allocate %zx bytes needed "
5642 "to store MAC addresses",
5643 sizeof(struct rte_ether_addr) *
5644 HNS3_UC_MACADDR_NUM);
5646 goto err_rte_zmalloc;
5649 rte_ether_addr_copy((struct rte_ether_addr *)hw->mac.mac_addr,
5650 ð_dev->data->mac_addrs[0]);
5652 hw->adapter_state = HNS3_NIC_INITIALIZED;
5654 * Pass the information to the rte_eth_dev_close() that it should also
5655 * release the private port resources.
5657 eth_dev->data->dev_flags |= RTE_ETH_DEV_CLOSE_REMOVE;
5659 if (rte_atomic16_read(&hns->hw.reset.schedule) == SCHEDULE_PENDING) {
5660 hns3_err(hw, "Reschedule reset service after dev_init");
5661 hns3_schedule_reset(hns);
5663 /* IMP will wait ready flag before reset */
5664 hns3_notify_reset_ready(hw, false);
5667 hns3_info(hw, "hns3 dev initialization successful!");
5671 hns3_uninit_pf(eth_dev);
5674 rte_free(hw->reset.wait_data);
5677 hns3_mp_uninit_primary();
5679 err_mp_init_primary:
5680 err_mp_init_secondary:
5681 eth_dev->dev_ops = NULL;
5682 eth_dev->rx_pkt_burst = NULL;
5683 eth_dev->tx_pkt_burst = NULL;
5684 eth_dev->tx_pkt_prepare = NULL;
5685 rte_free(eth_dev->process_private);
5686 eth_dev->process_private = NULL;
5691 hns3_dev_uninit(struct rte_eth_dev *eth_dev)
5693 struct hns3_adapter *hns = eth_dev->data->dev_private;
5694 struct hns3_hw *hw = &hns->hw;
5696 PMD_INIT_FUNC_TRACE();
5698 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
5701 eth_dev->dev_ops = NULL;
5702 eth_dev->rx_pkt_burst = NULL;
5703 eth_dev->tx_pkt_burst = NULL;
5704 eth_dev->tx_pkt_prepare = NULL;
5705 if (hw->adapter_state < HNS3_NIC_CLOSING)
5706 hns3_dev_close(eth_dev);
5708 hw->adapter_state = HNS3_NIC_REMOVED;
5713 eth_hns3_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
5714 struct rte_pci_device *pci_dev)
5716 return rte_eth_dev_pci_generic_probe(pci_dev,
5717 sizeof(struct hns3_adapter),
5722 eth_hns3_pci_remove(struct rte_pci_device *pci_dev)
5724 return rte_eth_dev_pci_generic_remove(pci_dev, hns3_dev_uninit);
5727 static const struct rte_pci_id pci_id_hns3_map[] = {
5728 { RTE_PCI_DEVICE(PCI_VENDOR_ID_HUAWEI, HNS3_DEV_ID_GE) },
5729 { RTE_PCI_DEVICE(PCI_VENDOR_ID_HUAWEI, HNS3_DEV_ID_25GE) },
5730 { RTE_PCI_DEVICE(PCI_VENDOR_ID_HUAWEI, HNS3_DEV_ID_25GE_RDMA) },
5731 { RTE_PCI_DEVICE(PCI_VENDOR_ID_HUAWEI, HNS3_DEV_ID_50GE_RDMA) },
5732 { RTE_PCI_DEVICE(PCI_VENDOR_ID_HUAWEI, HNS3_DEV_ID_100G_RDMA_MACSEC) },
5733 { RTE_PCI_DEVICE(PCI_VENDOR_ID_HUAWEI, HNS3_DEV_ID_200G_RDMA) },
5734 { .vendor_id = 0, /* sentinel */ },
5737 static struct rte_pci_driver rte_hns3_pmd = {
5738 .id_table = pci_id_hns3_map,
5739 .drv_flags = RTE_PCI_DRV_NEED_MAPPING,
5740 .probe = eth_hns3_pci_probe,
5741 .remove = eth_hns3_pci_remove,
5744 RTE_PMD_REGISTER_PCI(net_hns3, rte_hns3_pmd);
5745 RTE_PMD_REGISTER_PCI_TABLE(net_hns3, pci_id_hns3_map);
5746 RTE_PMD_REGISTER_KMOD_DEP(net_hns3, "* igb_uio | vfio-pci");
5747 RTE_LOG_REGISTER(hns3_logtype_init, pmd.net.hns3.init, NOTICE);
5748 RTE_LOG_REGISTER(hns3_logtype_driver, pmd.net.hns3.driver, NOTICE);