1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2018-2019 Hisilicon Limited.
6 #include <rte_bus_pci.h>
7 #include <ethdev_pci.h>
10 #include "hns3_ethdev.h"
11 #include "hns3_logs.h"
12 #include "hns3_rxtx.h"
13 #include "hns3_intr.h"
14 #include "hns3_regs.h"
18 #define HNS3_DEFAULT_PORT_CONF_BURST_SIZE 32
19 #define HNS3_DEFAULT_PORT_CONF_QUEUES_NUM 1
21 #define HNS3_SERVICE_INTERVAL 1000000 /* us */
22 #define HNS3_SERVICE_QUICK_INTERVAL 10
23 #define HNS3_INVALID_PVID 0xFFFF
25 #define HNS3_FILTER_TYPE_VF 0
26 #define HNS3_FILTER_TYPE_PORT 1
27 #define HNS3_FILTER_FE_EGRESS_V1_B BIT(0)
28 #define HNS3_FILTER_FE_NIC_INGRESS_B BIT(0)
29 #define HNS3_FILTER_FE_NIC_EGRESS_B BIT(1)
30 #define HNS3_FILTER_FE_ROCE_INGRESS_B BIT(2)
31 #define HNS3_FILTER_FE_ROCE_EGRESS_B BIT(3)
32 #define HNS3_FILTER_FE_EGRESS (HNS3_FILTER_FE_NIC_EGRESS_B \
33 | HNS3_FILTER_FE_ROCE_EGRESS_B)
34 #define HNS3_FILTER_FE_INGRESS (HNS3_FILTER_FE_NIC_INGRESS_B \
35 | HNS3_FILTER_FE_ROCE_INGRESS_B)
37 /* Reset related Registers */
38 #define HNS3_GLOBAL_RESET_BIT 0
39 #define HNS3_CORE_RESET_BIT 1
40 #define HNS3_IMP_RESET_BIT 2
41 #define HNS3_FUN_RST_ING_B 0
43 #define HNS3_VECTOR0_IMP_RESET_INT_B 1
44 #define HNS3_VECTOR0_IMP_CMDQ_ERR_B 4U
45 #define HNS3_VECTOR0_IMP_RD_POISON_B 5U
46 #define HNS3_VECTOR0_ALL_MSIX_ERR_B 6U
48 #define HNS3_RESET_WAIT_MS 100
49 #define HNS3_RESET_WAIT_CNT 200
51 /* FEC mode order defined in HNS3 hardware */
52 #define HNS3_HW_FEC_MODE_NOFEC 0
53 #define HNS3_HW_FEC_MODE_BASER 1
54 #define HNS3_HW_FEC_MODE_RS 2
57 HNS3_VECTOR0_EVENT_RST,
58 HNS3_VECTOR0_EVENT_MBX,
59 HNS3_VECTOR0_EVENT_ERR,
60 HNS3_VECTOR0_EVENT_OTHER,
63 static const struct rte_eth_fec_capa speed_fec_capa_tbl[] = {
64 { ETH_SPEED_NUM_10G, RTE_ETH_FEC_MODE_CAPA_MASK(NOFEC) |
65 RTE_ETH_FEC_MODE_CAPA_MASK(AUTO) |
66 RTE_ETH_FEC_MODE_CAPA_MASK(BASER) },
68 { ETH_SPEED_NUM_25G, RTE_ETH_FEC_MODE_CAPA_MASK(NOFEC) |
69 RTE_ETH_FEC_MODE_CAPA_MASK(AUTO) |
70 RTE_ETH_FEC_MODE_CAPA_MASK(BASER) |
71 RTE_ETH_FEC_MODE_CAPA_MASK(RS) },
73 { ETH_SPEED_NUM_40G, RTE_ETH_FEC_MODE_CAPA_MASK(NOFEC) |
74 RTE_ETH_FEC_MODE_CAPA_MASK(AUTO) |
75 RTE_ETH_FEC_MODE_CAPA_MASK(BASER) },
77 { ETH_SPEED_NUM_50G, RTE_ETH_FEC_MODE_CAPA_MASK(NOFEC) |
78 RTE_ETH_FEC_MODE_CAPA_MASK(AUTO) |
79 RTE_ETH_FEC_MODE_CAPA_MASK(BASER) |
80 RTE_ETH_FEC_MODE_CAPA_MASK(RS) },
82 { ETH_SPEED_NUM_100G, RTE_ETH_FEC_MODE_CAPA_MASK(NOFEC) |
83 RTE_ETH_FEC_MODE_CAPA_MASK(AUTO) |
84 RTE_ETH_FEC_MODE_CAPA_MASK(RS) },
86 { ETH_SPEED_NUM_200G, RTE_ETH_FEC_MODE_CAPA_MASK(NOFEC) |
87 RTE_ETH_FEC_MODE_CAPA_MASK(AUTO) |
88 RTE_ETH_FEC_MODE_CAPA_MASK(RS) }
91 static enum hns3_reset_level hns3_get_reset_level(struct hns3_adapter *hns,
93 static int hns3_dev_mtu_set(struct rte_eth_dev *dev, uint16_t mtu);
94 static int hns3_vlan_pvid_configure(struct hns3_adapter *hns, uint16_t pvid,
96 static int hns3_update_link_info(struct rte_eth_dev *eth_dev);
97 static bool hns3_update_link_status(struct hns3_hw *hw);
99 static int hns3_add_mc_addr(struct hns3_hw *hw,
100 struct rte_ether_addr *mac_addr);
101 static int hns3_remove_mc_addr(struct hns3_hw *hw,
102 struct rte_ether_addr *mac_addr);
103 static int hns3_restore_fec(struct hns3_hw *hw);
104 static int hns3_query_dev_fec_info(struct hns3_hw *hw);
105 static int hns3_do_stop(struct hns3_adapter *hns);
107 void hns3_ether_format_addr(char *buf, uint16_t size,
108 const struct rte_ether_addr *ether_addr)
110 snprintf(buf, size, "%02X:**:**:**:%02X:%02X",
111 ether_addr->addr_bytes[0],
112 ether_addr->addr_bytes[4],
113 ether_addr->addr_bytes[5]);
117 hns3_pf_disable_irq0(struct hns3_hw *hw)
119 hns3_write_dev(hw, HNS3_MISC_VECTOR_REG_BASE, 0);
123 hns3_pf_enable_irq0(struct hns3_hw *hw)
125 hns3_write_dev(hw, HNS3_MISC_VECTOR_REG_BASE, 1);
128 static enum hns3_evt_cause
129 hns3_proc_imp_reset_event(struct hns3_adapter *hns, bool is_delay,
132 struct hns3_hw *hw = &hns->hw;
134 __atomic_store_n(&hw->reset.disable_cmd, 1, __ATOMIC_RELAXED);
135 hns3_atomic_set_bit(HNS3_IMP_RESET, &hw->reset.pending);
136 *vec_val = BIT(HNS3_VECTOR0_IMPRESET_INT_B);
138 hw->reset.stats.imp_cnt++;
139 hns3_warn(hw, "IMP reset detected, clear reset status");
141 hns3_schedule_delayed_reset(hns);
142 hns3_warn(hw, "IMP reset detected, don't clear reset status");
145 return HNS3_VECTOR0_EVENT_RST;
148 static enum hns3_evt_cause
149 hns3_proc_global_reset_event(struct hns3_adapter *hns, bool is_delay,
152 struct hns3_hw *hw = &hns->hw;
154 __atomic_store_n(&hw->reset.disable_cmd, 1, __ATOMIC_RELAXED);
155 hns3_atomic_set_bit(HNS3_GLOBAL_RESET, &hw->reset.pending);
156 *vec_val = BIT(HNS3_VECTOR0_GLOBALRESET_INT_B);
158 hw->reset.stats.global_cnt++;
159 hns3_warn(hw, "Global reset detected, clear reset status");
161 hns3_schedule_delayed_reset(hns);
163 "Global reset detected, don't clear reset status");
166 return HNS3_VECTOR0_EVENT_RST;
169 static enum hns3_evt_cause
170 hns3_check_event_cause(struct hns3_adapter *hns, uint32_t *clearval)
172 struct hns3_hw *hw = &hns->hw;
173 uint32_t vector0_int_stats;
174 uint32_t cmdq_src_val;
175 uint32_t hw_err_src_reg;
177 enum hns3_evt_cause ret;
180 /* fetch the events from their corresponding regs */
181 vector0_int_stats = hns3_read_dev(hw, HNS3_VECTOR0_OTHER_INT_STS_REG);
182 cmdq_src_val = hns3_read_dev(hw, HNS3_VECTOR0_CMDQ_SRC_REG);
183 hw_err_src_reg = hns3_read_dev(hw, HNS3_RAS_PF_OTHER_INT_STS_REG);
185 is_delay = clearval == NULL ? true : false;
187 * Assumption: If by any chance reset and mailbox events are reported
188 * together then we will only process reset event and defer the
189 * processing of the mailbox events. Since, we would have not cleared
190 * RX CMDQ event this time we would receive again another interrupt
191 * from H/W just for the mailbox.
193 if (BIT(HNS3_VECTOR0_IMPRESET_INT_B) & vector0_int_stats) { /* IMP */
194 ret = hns3_proc_imp_reset_event(hns, is_delay, &val);
199 if (BIT(HNS3_VECTOR0_GLOBALRESET_INT_B) & vector0_int_stats) {
200 ret = hns3_proc_global_reset_event(hns, is_delay, &val);
204 /* check for vector0 msix event source */
205 if (vector0_int_stats & HNS3_VECTOR0_REG_MSIX_MASK ||
206 hw_err_src_reg & HNS3_RAS_REG_NFE_MASK) {
207 val = vector0_int_stats | hw_err_src_reg;
208 ret = HNS3_VECTOR0_EVENT_ERR;
212 /* check for vector0 mailbox(=CMDQ RX) event source */
213 if (BIT(HNS3_VECTOR0_RX_CMDQ_INT_B) & cmdq_src_val) {
214 cmdq_src_val &= ~BIT(HNS3_VECTOR0_RX_CMDQ_INT_B);
216 ret = HNS3_VECTOR0_EVENT_MBX;
220 if (clearval && (vector0_int_stats || cmdq_src_val || hw_err_src_reg))
221 hns3_warn(hw, "vector0_int_stats:0x%x cmdq_src_val:0x%x hw_err_src_reg:0x%x",
222 vector0_int_stats, cmdq_src_val, hw_err_src_reg);
223 val = vector0_int_stats;
224 ret = HNS3_VECTOR0_EVENT_OTHER;
233 hns3_clear_event_cause(struct hns3_hw *hw, uint32_t event_type, uint32_t regclr)
235 if (event_type == HNS3_VECTOR0_EVENT_RST)
236 hns3_write_dev(hw, HNS3_MISC_RESET_STS_REG, regclr);
237 else if (event_type == HNS3_VECTOR0_EVENT_MBX)
238 hns3_write_dev(hw, HNS3_VECTOR0_CMDQ_SRC_REG, regclr);
242 hns3_clear_all_event_cause(struct hns3_hw *hw)
244 uint32_t vector0_int_stats;
245 vector0_int_stats = hns3_read_dev(hw, HNS3_VECTOR0_OTHER_INT_STS_REG);
247 if (BIT(HNS3_VECTOR0_IMPRESET_INT_B) & vector0_int_stats)
248 hns3_warn(hw, "Probe during IMP reset interrupt");
250 if (BIT(HNS3_VECTOR0_GLOBALRESET_INT_B) & vector0_int_stats)
251 hns3_warn(hw, "Probe during Global reset interrupt");
253 hns3_clear_event_cause(hw, HNS3_VECTOR0_EVENT_RST,
254 BIT(HNS3_VECTOR0_IMPRESET_INT_B) |
255 BIT(HNS3_VECTOR0_GLOBALRESET_INT_B) |
256 BIT(HNS3_VECTOR0_CORERESET_INT_B));
257 hns3_clear_event_cause(hw, HNS3_VECTOR0_EVENT_MBX, 0);
261 hns3_interrupt_handler(void *param)
263 struct rte_eth_dev *dev = (struct rte_eth_dev *)param;
264 struct hns3_adapter *hns = dev->data->dev_private;
265 struct hns3_hw *hw = &hns->hw;
266 enum hns3_evt_cause event_cause;
267 uint32_t clearval = 0;
269 /* Disable interrupt */
270 hns3_pf_disable_irq0(hw);
272 event_cause = hns3_check_event_cause(hns, &clearval);
273 /* vector 0 interrupt is shared with reset and mailbox source events. */
274 if (event_cause == HNS3_VECTOR0_EVENT_ERR) {
275 hns3_warn(hw, "Received err interrupt");
276 hns3_handle_msix_error(hns, &hw->reset.request);
277 hns3_handle_ras_error(hns, &hw->reset.request);
278 hns3_schedule_reset(hns);
279 } else if (event_cause == HNS3_VECTOR0_EVENT_RST) {
280 hns3_warn(hw, "Received reset interrupt");
281 hns3_schedule_reset(hns);
282 } else if (event_cause == HNS3_VECTOR0_EVENT_MBX)
283 hns3_dev_handle_mbx_msg(hw);
285 hns3_err(hw, "Received unknown event");
287 hns3_clear_event_cause(hw, event_cause, clearval);
288 /* Enable interrupt if it is not cause by reset */
289 hns3_pf_enable_irq0(hw);
293 hns3_set_port_vlan_filter(struct hns3_adapter *hns, uint16_t vlan_id, int on)
295 #define HNS3_VLAN_ID_OFFSET_STEP 160
296 #define HNS3_VLAN_BYTE_SIZE 8
297 struct hns3_vlan_filter_pf_cfg_cmd *req;
298 struct hns3_hw *hw = &hns->hw;
299 uint8_t vlan_offset_byte_val;
300 struct hns3_cmd_desc desc;
301 uint8_t vlan_offset_byte;
302 uint8_t vlan_offset_base;
305 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_VLAN_FILTER_PF_CFG, false);
307 vlan_offset_base = vlan_id / HNS3_VLAN_ID_OFFSET_STEP;
308 vlan_offset_byte = (vlan_id % HNS3_VLAN_ID_OFFSET_STEP) /
310 vlan_offset_byte_val = 1 << (vlan_id % HNS3_VLAN_BYTE_SIZE);
312 req = (struct hns3_vlan_filter_pf_cfg_cmd *)desc.data;
313 req->vlan_offset = vlan_offset_base;
314 req->vlan_cfg = on ? 0 : 1;
315 req->vlan_offset_bitmap[vlan_offset_byte] = vlan_offset_byte_val;
317 ret = hns3_cmd_send(hw, &desc, 1);
319 hns3_err(hw, "set port vlan id failed, vlan_id =%u, ret =%d",
326 hns3_rm_dev_vlan_table(struct hns3_adapter *hns, uint16_t vlan_id)
328 struct hns3_user_vlan_table *vlan_entry;
329 struct hns3_pf *pf = &hns->pf;
331 LIST_FOREACH(vlan_entry, &pf->vlan_list, next) {
332 if (vlan_entry->vlan_id == vlan_id) {
333 if (vlan_entry->hd_tbl_status)
334 hns3_set_port_vlan_filter(hns, vlan_id, 0);
335 LIST_REMOVE(vlan_entry, next);
336 rte_free(vlan_entry);
343 hns3_add_dev_vlan_table(struct hns3_adapter *hns, uint16_t vlan_id,
346 struct hns3_user_vlan_table *vlan_entry;
347 struct hns3_hw *hw = &hns->hw;
348 struct hns3_pf *pf = &hns->pf;
350 LIST_FOREACH(vlan_entry, &pf->vlan_list, next) {
351 if (vlan_entry->vlan_id == vlan_id)
355 vlan_entry = rte_zmalloc("hns3_vlan_tbl", sizeof(*vlan_entry), 0);
356 if (vlan_entry == NULL) {
357 hns3_err(hw, "Failed to malloc hns3 vlan table");
361 vlan_entry->hd_tbl_status = writen_to_tbl;
362 vlan_entry->vlan_id = vlan_id;
364 LIST_INSERT_HEAD(&pf->vlan_list, vlan_entry, next);
368 hns3_restore_vlan_table(struct hns3_adapter *hns)
370 struct hns3_user_vlan_table *vlan_entry;
371 struct hns3_hw *hw = &hns->hw;
372 struct hns3_pf *pf = &hns->pf;
376 if (hw->port_base_vlan_cfg.state == HNS3_PORT_BASE_VLAN_ENABLE)
377 return hns3_vlan_pvid_configure(hns,
378 hw->port_base_vlan_cfg.pvid, 1);
380 LIST_FOREACH(vlan_entry, &pf->vlan_list, next) {
381 if (vlan_entry->hd_tbl_status) {
382 vlan_id = vlan_entry->vlan_id;
383 ret = hns3_set_port_vlan_filter(hns, vlan_id, 1);
393 hns3_vlan_filter_configure(struct hns3_adapter *hns, uint16_t vlan_id, int on)
395 struct hns3_hw *hw = &hns->hw;
396 bool writen_to_tbl = false;
400 * When vlan filter is enabled, hardware regards packets without vlan
401 * as packets with vlan 0. So, to receive packets without vlan, vlan id
402 * 0 is not allowed to be removed by rte_eth_dev_vlan_filter.
404 if (on == 0 && vlan_id == 0)
408 * When port base vlan enabled, we use port base vlan as the vlan
409 * filter condition. In this case, we don't update vlan filter table
410 * when user add new vlan or remove exist vlan, just update the
411 * vlan list. The vlan id in vlan list will be writen in vlan filter
412 * table until port base vlan disabled
414 if (hw->port_base_vlan_cfg.state == HNS3_PORT_BASE_VLAN_DISABLE) {
415 ret = hns3_set_port_vlan_filter(hns, vlan_id, on);
416 writen_to_tbl = true;
421 hns3_add_dev_vlan_table(hns, vlan_id, writen_to_tbl);
423 hns3_rm_dev_vlan_table(hns, vlan_id);
429 hns3_vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on)
431 struct hns3_adapter *hns = dev->data->dev_private;
432 struct hns3_hw *hw = &hns->hw;
435 rte_spinlock_lock(&hw->lock);
436 ret = hns3_vlan_filter_configure(hns, vlan_id, on);
437 rte_spinlock_unlock(&hw->lock);
442 hns3_vlan_tpid_configure(struct hns3_adapter *hns, enum rte_vlan_type vlan_type,
445 struct hns3_rx_vlan_type_cfg_cmd *rx_req;
446 struct hns3_tx_vlan_type_cfg_cmd *tx_req;
447 struct hns3_hw *hw = &hns->hw;
448 struct hns3_cmd_desc desc;
451 if ((vlan_type != ETH_VLAN_TYPE_INNER &&
452 vlan_type != ETH_VLAN_TYPE_OUTER)) {
453 hns3_err(hw, "Unsupported vlan type, vlan_type =%d", vlan_type);
457 if (tpid != RTE_ETHER_TYPE_VLAN) {
458 hns3_err(hw, "Unsupported vlan tpid, vlan_type =%d", vlan_type);
462 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_MAC_VLAN_TYPE_ID, false);
463 rx_req = (struct hns3_rx_vlan_type_cfg_cmd *)desc.data;
465 if (vlan_type == ETH_VLAN_TYPE_OUTER) {
466 rx_req->ot_fst_vlan_type = rte_cpu_to_le_16(tpid);
467 rx_req->ot_sec_vlan_type = rte_cpu_to_le_16(tpid);
468 } else if (vlan_type == ETH_VLAN_TYPE_INNER) {
469 rx_req->ot_fst_vlan_type = rte_cpu_to_le_16(tpid);
470 rx_req->ot_sec_vlan_type = rte_cpu_to_le_16(tpid);
471 rx_req->in_fst_vlan_type = rte_cpu_to_le_16(tpid);
472 rx_req->in_sec_vlan_type = rte_cpu_to_le_16(tpid);
475 ret = hns3_cmd_send(hw, &desc, 1);
477 hns3_err(hw, "Send rxvlan protocol type command fail, ret =%d",
482 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_MAC_VLAN_INSERT, false);
484 tx_req = (struct hns3_tx_vlan_type_cfg_cmd *)desc.data;
485 tx_req->ot_vlan_type = rte_cpu_to_le_16(tpid);
486 tx_req->in_vlan_type = rte_cpu_to_le_16(tpid);
488 ret = hns3_cmd_send(hw, &desc, 1);
490 hns3_err(hw, "Send txvlan protocol type command fail, ret =%d",
496 hns3_vlan_tpid_set(struct rte_eth_dev *dev, enum rte_vlan_type vlan_type,
499 struct hns3_adapter *hns = dev->data->dev_private;
500 struct hns3_hw *hw = &hns->hw;
503 rte_spinlock_lock(&hw->lock);
504 ret = hns3_vlan_tpid_configure(hns, vlan_type, tpid);
505 rte_spinlock_unlock(&hw->lock);
510 hns3_set_vlan_rx_offload_cfg(struct hns3_adapter *hns,
511 struct hns3_rx_vtag_cfg *vcfg)
513 struct hns3_vport_vtag_rx_cfg_cmd *req;
514 struct hns3_hw *hw = &hns->hw;
515 struct hns3_cmd_desc desc;
520 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_VLAN_PORT_RX_CFG, false);
522 req = (struct hns3_vport_vtag_rx_cfg_cmd *)desc.data;
523 hns3_set_bit(req->vport_vlan_cfg, HNS3_REM_TAG1_EN_B,
524 vcfg->strip_tag1_en ? 1 : 0);
525 hns3_set_bit(req->vport_vlan_cfg, HNS3_REM_TAG2_EN_B,
526 vcfg->strip_tag2_en ? 1 : 0);
527 hns3_set_bit(req->vport_vlan_cfg, HNS3_SHOW_TAG1_EN_B,
528 vcfg->vlan1_vlan_prionly ? 1 : 0);
529 hns3_set_bit(req->vport_vlan_cfg, HNS3_SHOW_TAG2_EN_B,
530 vcfg->vlan2_vlan_prionly ? 1 : 0);
532 /* firmwall will ignore this configuration for PCI_REVISION_ID_HIP08 */
533 hns3_set_bit(req->vport_vlan_cfg, HNS3_DISCARD_TAG1_EN_B,
534 vcfg->strip_tag1_discard_en ? 1 : 0);
535 hns3_set_bit(req->vport_vlan_cfg, HNS3_DISCARD_TAG2_EN_B,
536 vcfg->strip_tag2_discard_en ? 1 : 0);
538 * In current version VF is not supported when PF is driven by DPDK
539 * driver, just need to configure parameters for PF vport.
541 vport_id = HNS3_PF_FUNC_ID;
542 req->vf_offset = vport_id / HNS3_VF_NUM_PER_CMD;
543 bitmap = 1 << (vport_id % HNS3_VF_NUM_PER_BYTE);
544 req->vf_bitmap[req->vf_offset] = bitmap;
546 ret = hns3_cmd_send(hw, &desc, 1);
548 hns3_err(hw, "Send port rxvlan cfg command fail, ret =%d", ret);
553 hns3_update_rx_offload_cfg(struct hns3_adapter *hns,
554 struct hns3_rx_vtag_cfg *vcfg)
556 struct hns3_pf *pf = &hns->pf;
557 memcpy(&pf->vtag_config.rx_vcfg, vcfg, sizeof(pf->vtag_config.rx_vcfg));
561 hns3_update_tx_offload_cfg(struct hns3_adapter *hns,
562 struct hns3_tx_vtag_cfg *vcfg)
564 struct hns3_pf *pf = &hns->pf;
565 memcpy(&pf->vtag_config.tx_vcfg, vcfg, sizeof(pf->vtag_config.tx_vcfg));
569 hns3_en_hw_strip_rxvtag(struct hns3_adapter *hns, bool enable)
571 struct hns3_rx_vtag_cfg rxvlan_cfg;
572 struct hns3_hw *hw = &hns->hw;
575 if (hw->port_base_vlan_cfg.state == HNS3_PORT_BASE_VLAN_DISABLE) {
576 rxvlan_cfg.strip_tag1_en = false;
577 rxvlan_cfg.strip_tag2_en = enable;
578 rxvlan_cfg.strip_tag2_discard_en = false;
580 rxvlan_cfg.strip_tag1_en = enable;
581 rxvlan_cfg.strip_tag2_en = true;
582 rxvlan_cfg.strip_tag2_discard_en = true;
585 rxvlan_cfg.strip_tag1_discard_en = false;
586 rxvlan_cfg.vlan1_vlan_prionly = false;
587 rxvlan_cfg.vlan2_vlan_prionly = false;
588 rxvlan_cfg.rx_vlan_offload_en = enable;
590 ret = hns3_set_vlan_rx_offload_cfg(hns, &rxvlan_cfg);
592 hns3_err(hw, "enable strip rx vtag failed, ret =%d", ret);
596 hns3_update_rx_offload_cfg(hns, &rxvlan_cfg);
602 hns3_set_vlan_filter_ctrl(struct hns3_hw *hw, uint8_t vlan_type,
603 uint8_t fe_type, bool filter_en, uint8_t vf_id)
605 struct hns3_vlan_filter_ctrl_cmd *req;
606 struct hns3_cmd_desc desc;
609 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_VLAN_FILTER_CTRL, false);
611 req = (struct hns3_vlan_filter_ctrl_cmd *)desc.data;
612 req->vlan_type = vlan_type;
613 req->vlan_fe = filter_en ? fe_type : 0;
616 ret = hns3_cmd_send(hw, &desc, 1);
618 hns3_err(hw, "set vlan filter fail, ret =%d", ret);
624 hns3_vlan_filter_init(struct hns3_adapter *hns)
626 struct hns3_hw *hw = &hns->hw;
629 ret = hns3_set_vlan_filter_ctrl(hw, HNS3_FILTER_TYPE_VF,
630 HNS3_FILTER_FE_EGRESS, false,
633 hns3_err(hw, "failed to init vf vlan filter, ret = %d", ret);
637 ret = hns3_set_vlan_filter_ctrl(hw, HNS3_FILTER_TYPE_PORT,
638 HNS3_FILTER_FE_INGRESS, false,
641 hns3_err(hw, "failed to init port vlan filter, ret = %d", ret);
647 hns3_enable_vlan_filter(struct hns3_adapter *hns, bool enable)
649 struct hns3_hw *hw = &hns->hw;
652 ret = hns3_set_vlan_filter_ctrl(hw, HNS3_FILTER_TYPE_PORT,
653 HNS3_FILTER_FE_INGRESS, enable,
656 hns3_err(hw, "failed to %s port vlan filter, ret = %d",
657 enable ? "enable" : "disable", ret);
663 hns3_vlan_offload_set(struct rte_eth_dev *dev, int mask)
665 struct hns3_adapter *hns = dev->data->dev_private;
666 struct hns3_hw *hw = &hns->hw;
667 struct rte_eth_rxmode *rxmode;
668 unsigned int tmp_mask;
672 rte_spinlock_lock(&hw->lock);
673 rxmode = &dev->data->dev_conf.rxmode;
674 tmp_mask = (unsigned int)mask;
675 if (tmp_mask & ETH_VLAN_FILTER_MASK) {
676 /* ignore vlan filter configuration during promiscuous mode */
677 if (!dev->data->promiscuous) {
678 /* Enable or disable VLAN filter */
679 enable = rxmode->offloads & DEV_RX_OFFLOAD_VLAN_FILTER ?
682 ret = hns3_enable_vlan_filter(hns, enable);
684 rte_spinlock_unlock(&hw->lock);
685 hns3_err(hw, "failed to %s rx filter, ret = %d",
686 enable ? "enable" : "disable", ret);
692 if (tmp_mask & ETH_VLAN_STRIP_MASK) {
693 /* Enable or disable VLAN stripping */
694 enable = rxmode->offloads & DEV_RX_OFFLOAD_VLAN_STRIP ?
697 ret = hns3_en_hw_strip_rxvtag(hns, enable);
699 rte_spinlock_unlock(&hw->lock);
700 hns3_err(hw, "failed to %s rx strip, ret = %d",
701 enable ? "enable" : "disable", ret);
706 rte_spinlock_unlock(&hw->lock);
712 hns3_set_vlan_tx_offload_cfg(struct hns3_adapter *hns,
713 struct hns3_tx_vtag_cfg *vcfg)
715 struct hns3_vport_vtag_tx_cfg_cmd *req;
716 struct hns3_cmd_desc desc;
717 struct hns3_hw *hw = &hns->hw;
722 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_VLAN_PORT_TX_CFG, false);
724 req = (struct hns3_vport_vtag_tx_cfg_cmd *)desc.data;
725 req->def_vlan_tag1 = vcfg->default_tag1;
726 req->def_vlan_tag2 = vcfg->default_tag2;
727 hns3_set_bit(req->vport_vlan_cfg, HNS3_ACCEPT_TAG1_B,
728 vcfg->accept_tag1 ? 1 : 0);
729 hns3_set_bit(req->vport_vlan_cfg, HNS3_ACCEPT_UNTAG1_B,
730 vcfg->accept_untag1 ? 1 : 0);
731 hns3_set_bit(req->vport_vlan_cfg, HNS3_ACCEPT_TAG2_B,
732 vcfg->accept_tag2 ? 1 : 0);
733 hns3_set_bit(req->vport_vlan_cfg, HNS3_ACCEPT_UNTAG2_B,
734 vcfg->accept_untag2 ? 1 : 0);
735 hns3_set_bit(req->vport_vlan_cfg, HNS3_PORT_INS_TAG1_EN_B,
736 vcfg->insert_tag1_en ? 1 : 0);
737 hns3_set_bit(req->vport_vlan_cfg, HNS3_PORT_INS_TAG2_EN_B,
738 vcfg->insert_tag2_en ? 1 : 0);
739 hns3_set_bit(req->vport_vlan_cfg, HNS3_CFG_NIC_ROCE_SEL_B, 0);
741 /* firmwall will ignore this configuration for PCI_REVISION_ID_HIP08 */
742 hns3_set_bit(req->vport_vlan_cfg, HNS3_TAG_SHIFT_MODE_EN_B,
743 vcfg->tag_shift_mode_en ? 1 : 0);
746 * In current version VF is not supported when PF is driven by DPDK
747 * driver, just need to configure parameters for PF vport.
749 vport_id = HNS3_PF_FUNC_ID;
750 req->vf_offset = vport_id / HNS3_VF_NUM_PER_CMD;
751 bitmap = 1 << (vport_id % HNS3_VF_NUM_PER_BYTE);
752 req->vf_bitmap[req->vf_offset] = bitmap;
754 ret = hns3_cmd_send(hw, &desc, 1);
756 hns3_err(hw, "Send port txvlan cfg command fail, ret =%d", ret);
762 hns3_vlan_txvlan_cfg(struct hns3_adapter *hns, uint16_t port_base_vlan_state,
765 struct hns3_hw *hw = &hns->hw;
766 struct hns3_tx_vtag_cfg txvlan_cfg;
769 if (port_base_vlan_state == HNS3_PORT_BASE_VLAN_DISABLE) {
770 txvlan_cfg.accept_tag1 = true;
771 txvlan_cfg.insert_tag1_en = false;
772 txvlan_cfg.default_tag1 = 0;
774 txvlan_cfg.accept_tag1 =
775 hw->vlan_mode == HNS3_HW_SHIFT_AND_DISCARD_MODE;
776 txvlan_cfg.insert_tag1_en = true;
777 txvlan_cfg.default_tag1 = pvid;
780 txvlan_cfg.accept_untag1 = true;
781 txvlan_cfg.accept_tag2 = true;
782 txvlan_cfg.accept_untag2 = true;
783 txvlan_cfg.insert_tag2_en = false;
784 txvlan_cfg.default_tag2 = 0;
785 txvlan_cfg.tag_shift_mode_en = true;
787 ret = hns3_set_vlan_tx_offload_cfg(hns, &txvlan_cfg);
789 hns3_err(hw, "pf vlan set pvid failed, pvid =%u ,ret =%d", pvid,
794 hns3_update_tx_offload_cfg(hns, &txvlan_cfg);
800 hns3_rm_all_vlan_table(struct hns3_adapter *hns, bool is_del_list)
802 struct hns3_user_vlan_table *vlan_entry;
803 struct hns3_pf *pf = &hns->pf;
805 LIST_FOREACH(vlan_entry, &pf->vlan_list, next) {
806 if (vlan_entry->hd_tbl_status) {
807 hns3_set_port_vlan_filter(hns, vlan_entry->vlan_id, 0);
808 vlan_entry->hd_tbl_status = false;
813 vlan_entry = LIST_FIRST(&pf->vlan_list);
815 LIST_REMOVE(vlan_entry, next);
816 rte_free(vlan_entry);
817 vlan_entry = LIST_FIRST(&pf->vlan_list);
823 hns3_add_all_vlan_table(struct hns3_adapter *hns)
825 struct hns3_user_vlan_table *vlan_entry;
826 struct hns3_pf *pf = &hns->pf;
828 LIST_FOREACH(vlan_entry, &pf->vlan_list, next) {
829 if (!vlan_entry->hd_tbl_status) {
830 hns3_set_port_vlan_filter(hns, vlan_entry->vlan_id, 1);
831 vlan_entry->hd_tbl_status = true;
837 hns3_remove_all_vlan_table(struct hns3_adapter *hns)
839 struct hns3_hw *hw = &hns->hw;
842 hns3_rm_all_vlan_table(hns, true);
843 if (hw->port_base_vlan_cfg.pvid != HNS3_INVALID_PVID) {
844 ret = hns3_set_port_vlan_filter(hns,
845 hw->port_base_vlan_cfg.pvid, 0);
847 hns3_err(hw, "Failed to remove all vlan table, ret =%d",
855 hns3_update_vlan_filter_entries(struct hns3_adapter *hns,
856 uint16_t port_base_vlan_state, uint16_t new_pvid)
858 struct hns3_hw *hw = &hns->hw;
862 if (port_base_vlan_state == HNS3_PORT_BASE_VLAN_ENABLE) {
863 old_pvid = hw->port_base_vlan_cfg.pvid;
864 if (old_pvid != HNS3_INVALID_PVID) {
865 ret = hns3_set_port_vlan_filter(hns, old_pvid, 0);
867 hns3_err(hw, "failed to remove old pvid %u, "
868 "ret = %d", old_pvid, ret);
873 hns3_rm_all_vlan_table(hns, false);
874 ret = hns3_set_port_vlan_filter(hns, new_pvid, 1);
876 hns3_err(hw, "failed to add new pvid %u, ret = %d",
881 ret = hns3_set_port_vlan_filter(hns, new_pvid, 0);
883 hns3_err(hw, "failed to remove pvid %u, ret = %d",
888 hns3_add_all_vlan_table(hns);
894 hns3_en_pvid_strip(struct hns3_adapter *hns, int on)
896 struct hns3_rx_vtag_cfg *old_cfg = &hns->pf.vtag_config.rx_vcfg;
897 struct hns3_rx_vtag_cfg rx_vlan_cfg;
901 rx_strip_en = old_cfg->rx_vlan_offload_en;
903 rx_vlan_cfg.strip_tag1_en = rx_strip_en;
904 rx_vlan_cfg.strip_tag2_en = true;
905 rx_vlan_cfg.strip_tag2_discard_en = true;
907 rx_vlan_cfg.strip_tag1_en = false;
908 rx_vlan_cfg.strip_tag2_en = rx_strip_en;
909 rx_vlan_cfg.strip_tag2_discard_en = false;
911 rx_vlan_cfg.strip_tag1_discard_en = false;
912 rx_vlan_cfg.vlan1_vlan_prionly = false;
913 rx_vlan_cfg.vlan2_vlan_prionly = false;
914 rx_vlan_cfg.rx_vlan_offload_en = old_cfg->rx_vlan_offload_en;
916 ret = hns3_set_vlan_rx_offload_cfg(hns, &rx_vlan_cfg);
920 hns3_update_rx_offload_cfg(hns, &rx_vlan_cfg);
925 hns3_vlan_pvid_configure(struct hns3_adapter *hns, uint16_t pvid, int on)
927 struct hns3_hw *hw = &hns->hw;
928 uint16_t port_base_vlan_state;
931 if (on == 0 && pvid != hw->port_base_vlan_cfg.pvid) {
932 if (hw->port_base_vlan_cfg.pvid != HNS3_INVALID_PVID)
933 hns3_warn(hw, "Invalid operation! As current pvid set "
934 "is %u, disable pvid %u is invalid",
935 hw->port_base_vlan_cfg.pvid, pvid);
939 port_base_vlan_state = on ? HNS3_PORT_BASE_VLAN_ENABLE :
940 HNS3_PORT_BASE_VLAN_DISABLE;
941 ret = hns3_vlan_txvlan_cfg(hns, port_base_vlan_state, pvid);
943 hns3_err(hw, "failed to config tx vlan for pvid, ret = %d",
948 ret = hns3_en_pvid_strip(hns, on);
950 hns3_err(hw, "failed to config rx vlan strip for pvid, "
955 if (pvid == HNS3_INVALID_PVID)
957 ret = hns3_update_vlan_filter_entries(hns, port_base_vlan_state, pvid);
959 hns3_err(hw, "failed to update vlan filter entries, ret = %d",
965 hw->port_base_vlan_cfg.state = port_base_vlan_state;
966 hw->port_base_vlan_cfg.pvid = on ? pvid : HNS3_INVALID_PVID;
971 hns3_vlan_pvid_set(struct rte_eth_dev *dev, uint16_t pvid, int on)
973 struct hns3_adapter *hns = dev->data->dev_private;
974 struct hns3_hw *hw = &hns->hw;
975 bool pvid_en_state_change;
979 if (pvid > RTE_ETHER_MAX_VLAN_ID) {
980 hns3_err(hw, "Invalid vlan_id = %u > %d", pvid,
981 RTE_ETHER_MAX_VLAN_ID);
986 * If PVID configuration state change, should refresh the PVID
987 * configuration state in struct hns3_tx_queue/hns3_rx_queue.
989 pvid_state = hw->port_base_vlan_cfg.state;
990 if ((on && pvid_state == HNS3_PORT_BASE_VLAN_ENABLE) ||
991 (!on && pvid_state == HNS3_PORT_BASE_VLAN_DISABLE))
992 pvid_en_state_change = false;
994 pvid_en_state_change = true;
996 rte_spinlock_lock(&hw->lock);
997 ret = hns3_vlan_pvid_configure(hns, pvid, on);
998 rte_spinlock_unlock(&hw->lock);
1002 * Only in HNS3_SW_SHIFT_AND_MODE the PVID related operation in Tx/Rx
1003 * need be processed by PMD driver.
1005 if (pvid_en_state_change &&
1006 hw->vlan_mode == HNS3_SW_SHIFT_AND_DISCARD_MODE)
1007 hns3_update_all_queues_pvid_proc_en(hw);
1013 hns3_default_vlan_config(struct hns3_adapter *hns)
1015 struct hns3_hw *hw = &hns->hw;
1019 * When vlan filter is enabled, hardware regards packets without vlan
1020 * as packets with vlan 0. Therefore, if vlan 0 is not in the vlan
1021 * table, packets without vlan won't be received. So, add vlan 0 as
1024 ret = hns3_vlan_filter_configure(hns, 0, 1);
1026 hns3_err(hw, "default vlan 0 config failed, ret =%d", ret);
1031 hns3_init_vlan_config(struct hns3_adapter *hns)
1033 struct hns3_hw *hw = &hns->hw;
1037 * This function can be called in the initialization and reset process,
1038 * when in reset process, it means that hardware had been reseted
1039 * successfully and we need to restore the hardware configuration to
1040 * ensure that the hardware configuration remains unchanged before and
1043 if (__atomic_load_n(&hw->reset.resetting, __ATOMIC_RELAXED) == 0) {
1044 hw->port_base_vlan_cfg.state = HNS3_PORT_BASE_VLAN_DISABLE;
1045 hw->port_base_vlan_cfg.pvid = HNS3_INVALID_PVID;
1048 ret = hns3_vlan_filter_init(hns);
1050 hns3_err(hw, "vlan init fail in pf, ret =%d", ret);
1054 ret = hns3_vlan_tpid_configure(hns, ETH_VLAN_TYPE_INNER,
1055 RTE_ETHER_TYPE_VLAN);
1057 hns3_err(hw, "tpid set fail in pf, ret =%d", ret);
1062 * When in the reinit dev stage of the reset process, the following
1063 * vlan-related configurations may differ from those at initialization,
1064 * we will restore configurations to hardware in hns3_restore_vlan_table
1065 * and hns3_restore_vlan_conf later.
1067 if (__atomic_load_n(&hw->reset.resetting, __ATOMIC_RELAXED) == 0) {
1068 ret = hns3_vlan_pvid_configure(hns, HNS3_INVALID_PVID, 0);
1070 hns3_err(hw, "pvid set fail in pf, ret =%d", ret);
1074 ret = hns3_en_hw_strip_rxvtag(hns, false);
1076 hns3_err(hw, "rx strip configure fail in pf, ret =%d",
1082 return hns3_default_vlan_config(hns);
1086 hns3_restore_vlan_conf(struct hns3_adapter *hns)
1088 struct hns3_pf *pf = &hns->pf;
1089 struct hns3_hw *hw = &hns->hw;
1094 if (!hw->data->promiscuous) {
1095 /* restore vlan filter states */
1096 offloads = hw->data->dev_conf.rxmode.offloads;
1097 enable = offloads & DEV_RX_OFFLOAD_VLAN_FILTER ? true : false;
1098 ret = hns3_enable_vlan_filter(hns, enable);
1100 hns3_err(hw, "failed to restore vlan rx filter conf, "
1106 ret = hns3_set_vlan_rx_offload_cfg(hns, &pf->vtag_config.rx_vcfg);
1108 hns3_err(hw, "failed to restore vlan rx conf, ret = %d", ret);
1112 ret = hns3_set_vlan_tx_offload_cfg(hns, &pf->vtag_config.tx_vcfg);
1114 hns3_err(hw, "failed to restore vlan tx conf, ret = %d", ret);
1120 hns3_dev_configure_vlan(struct rte_eth_dev *dev)
1122 struct hns3_adapter *hns = dev->data->dev_private;
1123 struct rte_eth_dev_data *data = dev->data;
1124 struct rte_eth_txmode *txmode;
1125 struct hns3_hw *hw = &hns->hw;
1129 txmode = &data->dev_conf.txmode;
1130 if (txmode->hw_vlan_reject_tagged || txmode->hw_vlan_reject_untagged)
1132 "hw_vlan_reject_tagged or hw_vlan_reject_untagged "
1133 "configuration is not supported! Ignore these two "
1134 "parameters: hw_vlan_reject_tagged(%u), "
1135 "hw_vlan_reject_untagged(%u)",
1136 txmode->hw_vlan_reject_tagged,
1137 txmode->hw_vlan_reject_untagged);
1139 /* Apply vlan offload setting */
1140 mask = ETH_VLAN_STRIP_MASK | ETH_VLAN_FILTER_MASK;
1141 ret = hns3_vlan_offload_set(dev, mask);
1143 hns3_err(hw, "dev config rx vlan offload failed, ret = %d",
1149 * If pvid config is not set in rte_eth_conf, driver needn't to set
1150 * VLAN pvid related configuration to hardware.
1152 if (txmode->pvid == 0 && txmode->hw_vlan_insert_pvid == 0)
1155 /* Apply pvid setting */
1156 ret = hns3_vlan_pvid_set(dev, txmode->pvid,
1157 txmode->hw_vlan_insert_pvid);
1159 hns3_err(hw, "dev config vlan pvid(%u) failed, ret = %d",
1166 hns3_config_tso(struct hns3_hw *hw, unsigned int tso_mss_min,
1167 unsigned int tso_mss_max)
1169 struct hns3_cfg_tso_status_cmd *req;
1170 struct hns3_cmd_desc desc;
1173 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_TSO_GENERIC_CONFIG, false);
1175 req = (struct hns3_cfg_tso_status_cmd *)desc.data;
1178 hns3_set_field(tso_mss, HNS3_TSO_MSS_MIN_M, HNS3_TSO_MSS_MIN_S,
1180 req->tso_mss_min = rte_cpu_to_le_16(tso_mss);
1183 hns3_set_field(tso_mss, HNS3_TSO_MSS_MIN_M, HNS3_TSO_MSS_MIN_S,
1185 req->tso_mss_max = rte_cpu_to_le_16(tso_mss);
1187 return hns3_cmd_send(hw, &desc, 1);
1191 hns3_set_umv_space(struct hns3_hw *hw, uint16_t space_size,
1192 uint16_t *allocated_size, bool is_alloc)
1194 struct hns3_umv_spc_alc_cmd *req;
1195 struct hns3_cmd_desc desc;
1198 req = (struct hns3_umv_spc_alc_cmd *)desc.data;
1199 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_MAC_VLAN_ALLOCATE, false);
1200 hns3_set_bit(req->allocate, HNS3_UMV_SPC_ALC_B, is_alloc ? 0 : 1);
1201 req->space_size = rte_cpu_to_le_32(space_size);
1203 ret = hns3_cmd_send(hw, &desc, 1);
1205 PMD_INIT_LOG(ERR, "%s umv space failed for cmd_send, ret =%d",
1206 is_alloc ? "allocate" : "free", ret);
1210 if (is_alloc && allocated_size)
1211 *allocated_size = rte_le_to_cpu_32(desc.data[1]);
1217 hns3_init_umv_space(struct hns3_hw *hw)
1219 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
1220 struct hns3_pf *pf = &hns->pf;
1221 uint16_t allocated_size = 0;
1224 ret = hns3_set_umv_space(hw, pf->wanted_umv_size, &allocated_size,
1229 if (allocated_size < pf->wanted_umv_size)
1230 PMD_INIT_LOG(WARNING, "Alloc umv space failed, want %u, get %u",
1231 pf->wanted_umv_size, allocated_size);
1233 pf->max_umv_size = (!!allocated_size) ? allocated_size :
1234 pf->wanted_umv_size;
1235 pf->used_umv_size = 0;
1240 hns3_uninit_umv_space(struct hns3_hw *hw)
1242 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
1243 struct hns3_pf *pf = &hns->pf;
1246 if (pf->max_umv_size == 0)
1249 ret = hns3_set_umv_space(hw, pf->max_umv_size, NULL, false);
1253 pf->max_umv_size = 0;
1259 hns3_is_umv_space_full(struct hns3_hw *hw)
1261 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
1262 struct hns3_pf *pf = &hns->pf;
1265 is_full = (pf->used_umv_size >= pf->max_umv_size);
1271 hns3_update_umv_space(struct hns3_hw *hw, bool is_free)
1273 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
1274 struct hns3_pf *pf = &hns->pf;
1277 if (pf->used_umv_size > 0)
1278 pf->used_umv_size--;
1280 pf->used_umv_size++;
1284 hns3_prepare_mac_addr(struct hns3_mac_vlan_tbl_entry_cmd *new_req,
1285 const uint8_t *addr, bool is_mc)
1287 const unsigned char *mac_addr = addr;
1288 uint32_t high_val = ((uint32_t)mac_addr[3] << 24) |
1289 ((uint32_t)mac_addr[2] << 16) |
1290 ((uint32_t)mac_addr[1] << 8) |
1291 (uint32_t)mac_addr[0];
1292 uint32_t low_val = ((uint32_t)mac_addr[5] << 8) | (uint32_t)mac_addr[4];
1294 hns3_set_bit(new_req->flags, HNS3_MAC_VLAN_BIT0_EN_B, 1);
1296 hns3_set_bit(new_req->entry_type, HNS3_MAC_VLAN_BIT0_EN_B, 0);
1297 hns3_set_bit(new_req->entry_type, HNS3_MAC_VLAN_BIT1_EN_B, 1);
1298 hns3_set_bit(new_req->mc_mac_en, HNS3_MAC_VLAN_BIT0_EN_B, 1);
1301 new_req->mac_addr_hi32 = rte_cpu_to_le_32(high_val);
1302 new_req->mac_addr_lo16 = rte_cpu_to_le_16(low_val & 0xffff);
1306 hns3_get_mac_vlan_cmd_status(struct hns3_hw *hw, uint16_t cmdq_resp,
1308 enum hns3_mac_vlan_tbl_opcode op)
1311 hns3_err(hw, "cmdq execute failed for get_mac_vlan_cmd_status,status=%u",
1316 if (op == HNS3_MAC_VLAN_ADD) {
1317 if (resp_code == 0 || resp_code == 1) {
1319 } else if (resp_code == HNS3_ADD_UC_OVERFLOW) {
1320 hns3_err(hw, "add mac addr failed for uc_overflow");
1322 } else if (resp_code == HNS3_ADD_MC_OVERFLOW) {
1323 hns3_err(hw, "add mac addr failed for mc_overflow");
1327 hns3_err(hw, "add mac addr failed for undefined, code=%u",
1330 } else if (op == HNS3_MAC_VLAN_REMOVE) {
1331 if (resp_code == 0) {
1333 } else if (resp_code == 1) {
1334 hns3_dbg(hw, "remove mac addr failed for miss");
1338 hns3_err(hw, "remove mac addr failed for undefined, code=%u",
1341 } else if (op == HNS3_MAC_VLAN_LKUP) {
1342 if (resp_code == 0) {
1344 } else if (resp_code == 1) {
1345 hns3_dbg(hw, "lookup mac addr failed for miss");
1349 hns3_err(hw, "lookup mac addr failed for undefined, code=%u",
1354 hns3_err(hw, "unknown opcode for get_mac_vlan_cmd_status, opcode=%u",
1361 hns3_lookup_mac_vlan_tbl(struct hns3_hw *hw,
1362 struct hns3_mac_vlan_tbl_entry_cmd *req,
1363 struct hns3_cmd_desc *desc, bool is_mc)
1369 hns3_cmd_setup_basic_desc(&desc[0], HNS3_OPC_MAC_VLAN_ADD, true);
1371 desc[0].flag |= rte_cpu_to_le_16(HNS3_CMD_FLAG_NEXT);
1372 memcpy(desc[0].data, req,
1373 sizeof(struct hns3_mac_vlan_tbl_entry_cmd));
1374 hns3_cmd_setup_basic_desc(&desc[1], HNS3_OPC_MAC_VLAN_ADD,
1376 desc[1].flag |= rte_cpu_to_le_16(HNS3_CMD_FLAG_NEXT);
1377 hns3_cmd_setup_basic_desc(&desc[2], HNS3_OPC_MAC_VLAN_ADD,
1379 ret = hns3_cmd_send(hw, desc, HNS3_MC_MAC_VLAN_ADD_DESC_NUM);
1381 memcpy(desc[0].data, req,
1382 sizeof(struct hns3_mac_vlan_tbl_entry_cmd));
1383 ret = hns3_cmd_send(hw, desc, 1);
1386 hns3_err(hw, "lookup mac addr failed for cmd_send, ret =%d.",
1390 resp_code = (rte_le_to_cpu_32(desc[0].data[0]) >> 8) & 0xff;
1391 retval = rte_le_to_cpu_16(desc[0].retval);
1393 return hns3_get_mac_vlan_cmd_status(hw, retval, resp_code,
1394 HNS3_MAC_VLAN_LKUP);
1398 hns3_add_mac_vlan_tbl(struct hns3_hw *hw,
1399 struct hns3_mac_vlan_tbl_entry_cmd *req,
1400 struct hns3_cmd_desc *mc_desc)
1407 if (mc_desc == NULL) {
1408 struct hns3_cmd_desc desc;
1410 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_MAC_VLAN_ADD, false);
1411 memcpy(desc.data, req,
1412 sizeof(struct hns3_mac_vlan_tbl_entry_cmd));
1413 ret = hns3_cmd_send(hw, &desc, 1);
1414 resp_code = (rte_le_to_cpu_32(desc.data[0]) >> 8) & 0xff;
1415 retval = rte_le_to_cpu_16(desc.retval);
1417 cfg_status = hns3_get_mac_vlan_cmd_status(hw, retval, resp_code,
1420 hns3_cmd_reuse_desc(&mc_desc[0], false);
1421 mc_desc[0].flag |= rte_cpu_to_le_16(HNS3_CMD_FLAG_NEXT);
1422 hns3_cmd_reuse_desc(&mc_desc[1], false);
1423 mc_desc[1].flag |= rte_cpu_to_le_16(HNS3_CMD_FLAG_NEXT);
1424 hns3_cmd_reuse_desc(&mc_desc[2], false);
1425 mc_desc[2].flag &= rte_cpu_to_le_16(~HNS3_CMD_FLAG_NEXT);
1426 memcpy(mc_desc[0].data, req,
1427 sizeof(struct hns3_mac_vlan_tbl_entry_cmd));
1428 mc_desc[0].retval = 0;
1429 ret = hns3_cmd_send(hw, mc_desc, HNS3_MC_MAC_VLAN_ADD_DESC_NUM);
1430 resp_code = (rte_le_to_cpu_32(mc_desc[0].data[0]) >> 8) & 0xff;
1431 retval = rte_le_to_cpu_16(mc_desc[0].retval);
1433 cfg_status = hns3_get_mac_vlan_cmd_status(hw, retval, resp_code,
1438 hns3_err(hw, "add mac addr failed for cmd_send, ret =%d", ret);
1446 hns3_remove_mac_vlan_tbl(struct hns3_hw *hw,
1447 struct hns3_mac_vlan_tbl_entry_cmd *req)
1449 struct hns3_cmd_desc desc;
1454 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_MAC_VLAN_REMOVE, false);
1456 memcpy(desc.data, req, sizeof(struct hns3_mac_vlan_tbl_entry_cmd));
1458 ret = hns3_cmd_send(hw, &desc, 1);
1460 hns3_err(hw, "del mac addr failed for cmd_send, ret =%d", ret);
1463 resp_code = (rte_le_to_cpu_32(desc.data[0]) >> 8) & 0xff;
1464 retval = rte_le_to_cpu_16(desc.retval);
1466 return hns3_get_mac_vlan_cmd_status(hw, retval, resp_code,
1467 HNS3_MAC_VLAN_REMOVE);
1471 hns3_add_uc_addr_common(struct hns3_hw *hw, struct rte_ether_addr *mac_addr)
1473 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
1474 struct hns3_mac_vlan_tbl_entry_cmd req;
1475 struct hns3_pf *pf = &hns->pf;
1476 struct hns3_cmd_desc desc[3];
1477 char mac_str[RTE_ETHER_ADDR_FMT_SIZE];
1478 uint16_t egress_port = 0;
1482 /* check if mac addr is valid */
1483 if (!rte_is_valid_assigned_ether_addr(mac_addr)) {
1484 hns3_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
1486 hns3_err(hw, "Add unicast mac addr err! addr(%s) invalid",
1491 memset(&req, 0, sizeof(req));
1494 * In current version VF is not supported when PF is driven by DPDK
1495 * driver, just need to configure parameters for PF vport.
1497 vf_id = HNS3_PF_FUNC_ID;
1498 hns3_set_field(egress_port, HNS3_MAC_EPORT_VFID_M,
1499 HNS3_MAC_EPORT_VFID_S, vf_id);
1501 req.egress_port = rte_cpu_to_le_16(egress_port);
1503 hns3_prepare_mac_addr(&req, mac_addr->addr_bytes, false);
1506 * Lookup the mac address in the mac_vlan table, and add
1507 * it if the entry is inexistent. Repeated unicast entry
1508 * is not allowed in the mac vlan table.
1510 ret = hns3_lookup_mac_vlan_tbl(hw, &req, desc, false);
1511 if (ret == -ENOENT) {
1512 if (!hns3_is_umv_space_full(hw)) {
1513 ret = hns3_add_mac_vlan_tbl(hw, &req, NULL);
1515 hns3_update_umv_space(hw, false);
1519 hns3_err(hw, "UC MAC table full(%u)", pf->used_umv_size);
1524 hns3_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE, mac_addr);
1526 /* check if we just hit the duplicate */
1528 hns3_dbg(hw, "mac addr(%s) has been in the MAC table", mac_str);
1532 hns3_err(hw, "PF failed to add unicast entry(%s) in the MAC table",
1539 hns3_add_mc_addr_common(struct hns3_hw *hw, struct rte_ether_addr *mac_addr)
1541 char mac_str[RTE_ETHER_ADDR_FMT_SIZE];
1542 struct rte_ether_addr *addr;
1546 for (i = 0; i < hw->mc_addrs_num; i++) {
1547 addr = &hw->mc_addrs[i];
1548 /* Check if there are duplicate addresses */
1549 if (rte_is_same_ether_addr(addr, mac_addr)) {
1550 hns3_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
1552 hns3_err(hw, "failed to add mc mac addr, same addrs"
1553 "(%s) is added by the set_mc_mac_addr_list "
1559 ret = hns3_add_mc_addr(hw, mac_addr);
1561 hns3_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
1563 hns3_err(hw, "failed to add mc mac addr(%s), ret = %d",
1570 hns3_remove_mc_addr_common(struct hns3_hw *hw, struct rte_ether_addr *mac_addr)
1572 char mac_str[RTE_ETHER_ADDR_FMT_SIZE];
1575 ret = hns3_remove_mc_addr(hw, mac_addr);
1577 hns3_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
1579 hns3_err(hw, "failed to remove mc mac addr(%s), ret = %d",
1586 hns3_add_mac_addr(struct rte_eth_dev *dev, struct rte_ether_addr *mac_addr,
1587 uint32_t idx, __rte_unused uint32_t pool)
1589 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1590 char mac_str[RTE_ETHER_ADDR_FMT_SIZE];
1593 rte_spinlock_lock(&hw->lock);
1596 * In hns3 network engine adding UC and MC mac address with different
1597 * commands with firmware. We need to determine whether the input
1598 * address is a UC or a MC address to call different commands.
1599 * By the way, it is recommended calling the API function named
1600 * rte_eth_dev_set_mc_addr_list to set the MC mac address, because
1601 * using the rte_eth_dev_mac_addr_add API function to set MC mac address
1602 * may affect the specifications of UC mac addresses.
1604 if (rte_is_multicast_ether_addr(mac_addr))
1605 ret = hns3_add_mc_addr_common(hw, mac_addr);
1607 ret = hns3_add_uc_addr_common(hw, mac_addr);
1610 rte_spinlock_unlock(&hw->lock);
1611 hns3_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
1613 hns3_err(hw, "failed to add mac addr(%s), ret = %d", mac_str,
1619 hw->mac.default_addr_setted = true;
1620 rte_spinlock_unlock(&hw->lock);
1626 hns3_remove_uc_addr_common(struct hns3_hw *hw, struct rte_ether_addr *mac_addr)
1628 struct hns3_mac_vlan_tbl_entry_cmd req;
1629 char mac_str[RTE_ETHER_ADDR_FMT_SIZE];
1632 /* check if mac addr is valid */
1633 if (!rte_is_valid_assigned_ether_addr(mac_addr)) {
1634 hns3_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
1636 hns3_err(hw, "remove unicast mac addr err! addr(%s) invalid",
1641 memset(&req, 0, sizeof(req));
1642 hns3_set_bit(req.entry_type, HNS3_MAC_VLAN_BIT0_EN_B, 0);
1643 hns3_prepare_mac_addr(&req, mac_addr->addr_bytes, false);
1644 ret = hns3_remove_mac_vlan_tbl(hw, &req);
1645 if (ret == -ENOENT) /* mac addr isn't existent in the mac vlan table. */
1648 hns3_update_umv_space(hw, true);
1654 hns3_remove_mac_addr(struct rte_eth_dev *dev, uint32_t idx)
1656 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1657 /* index will be checked by upper level rte interface */
1658 struct rte_ether_addr *mac_addr = &dev->data->mac_addrs[idx];
1659 char mac_str[RTE_ETHER_ADDR_FMT_SIZE];
1662 rte_spinlock_lock(&hw->lock);
1664 if (rte_is_multicast_ether_addr(mac_addr))
1665 ret = hns3_remove_mc_addr_common(hw, mac_addr);
1667 ret = hns3_remove_uc_addr_common(hw, mac_addr);
1668 rte_spinlock_unlock(&hw->lock);
1670 hns3_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
1672 hns3_err(hw, "failed to remove mac addr(%s), ret = %d", mac_str,
1678 hns3_set_default_mac_addr(struct rte_eth_dev *dev,
1679 struct rte_ether_addr *mac_addr)
1681 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1682 struct rte_ether_addr *oaddr;
1683 char mac_str[RTE_ETHER_ADDR_FMT_SIZE];
1684 bool default_addr_setted;
1685 bool rm_succes = false;
1689 * It has been guaranteed that input parameter named mac_addr is valid
1690 * address in the rte layer of DPDK framework.
1692 oaddr = (struct rte_ether_addr *)hw->mac.mac_addr;
1693 default_addr_setted = hw->mac.default_addr_setted;
1694 if (default_addr_setted && !!rte_is_same_ether_addr(mac_addr, oaddr))
1697 rte_spinlock_lock(&hw->lock);
1698 if (default_addr_setted) {
1699 ret = hns3_remove_uc_addr_common(hw, oaddr);
1701 hns3_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
1703 hns3_warn(hw, "Remove old uc mac address(%s) fail: %d",
1710 ret = hns3_add_uc_addr_common(hw, mac_addr);
1712 hns3_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
1714 hns3_err(hw, "Failed to set mac addr(%s): %d", mac_str, ret);
1715 goto err_add_uc_addr;
1718 ret = hns3_pause_addr_cfg(hw, mac_addr->addr_bytes);
1720 hns3_err(hw, "Failed to configure mac pause address: %d", ret);
1721 goto err_pause_addr_cfg;
1724 rte_ether_addr_copy(mac_addr,
1725 (struct rte_ether_addr *)hw->mac.mac_addr);
1726 hw->mac.default_addr_setted = true;
1727 rte_spinlock_unlock(&hw->lock);
1732 ret_val = hns3_remove_uc_addr_common(hw, mac_addr);
1734 hns3_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
1737 "Failed to roll back to del setted mac addr(%s): %d",
1743 ret_val = hns3_add_uc_addr_common(hw, oaddr);
1745 hns3_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
1748 "Failed to restore old uc mac addr(%s): %d",
1750 hw->mac.default_addr_setted = false;
1753 rte_spinlock_unlock(&hw->lock);
1759 hns3_configure_all_mac_addr(struct hns3_adapter *hns, bool del)
1761 char mac_str[RTE_ETHER_ADDR_FMT_SIZE];
1762 struct hns3_hw *hw = &hns->hw;
1763 struct rte_ether_addr *addr;
1768 for (i = 0; i < HNS3_UC_MACADDR_NUM; i++) {
1769 addr = &hw->data->mac_addrs[i];
1770 if (rte_is_zero_ether_addr(addr))
1772 if (rte_is_multicast_ether_addr(addr))
1773 ret = del ? hns3_remove_mc_addr(hw, addr) :
1774 hns3_add_mc_addr(hw, addr);
1776 ret = del ? hns3_remove_uc_addr_common(hw, addr) :
1777 hns3_add_uc_addr_common(hw, addr);
1781 hns3_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
1783 hns3_err(hw, "failed to %s mac addr(%s) index:%d "
1784 "ret = %d.", del ? "remove" : "restore",
1792 hns3_update_desc_vfid(struct hns3_cmd_desc *desc, uint8_t vfid, bool clr)
1794 #define HNS3_VF_NUM_IN_FIRST_DESC 192
1798 if (vfid < HNS3_VF_NUM_IN_FIRST_DESC) {
1799 word_num = vfid / 32;
1800 bit_num = vfid % 32;
1802 desc[1].data[word_num] &=
1803 rte_cpu_to_le_32(~(1UL << bit_num));
1805 desc[1].data[word_num] |=
1806 rte_cpu_to_le_32(1UL << bit_num);
1808 word_num = (vfid - HNS3_VF_NUM_IN_FIRST_DESC) / 32;
1809 bit_num = vfid % 32;
1811 desc[2].data[word_num] &=
1812 rte_cpu_to_le_32(~(1UL << bit_num));
1814 desc[2].data[word_num] |=
1815 rte_cpu_to_le_32(1UL << bit_num);
1820 hns3_add_mc_addr(struct hns3_hw *hw, struct rte_ether_addr *mac_addr)
1822 struct hns3_mac_vlan_tbl_entry_cmd req;
1823 struct hns3_cmd_desc desc[3];
1824 char mac_str[RTE_ETHER_ADDR_FMT_SIZE];
1828 /* Check if mac addr is valid */
1829 if (!rte_is_multicast_ether_addr(mac_addr)) {
1830 hns3_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
1832 hns3_err(hw, "failed to add mc mac addr, addr(%s) invalid",
1837 memset(&req, 0, sizeof(req));
1838 hns3_set_bit(req.entry_type, HNS3_MAC_VLAN_BIT0_EN_B, 0);
1839 hns3_prepare_mac_addr(&req, mac_addr->addr_bytes, true);
1840 ret = hns3_lookup_mac_vlan_tbl(hw, &req, desc, true);
1842 /* This mac addr do not exist, add new entry for it */
1843 memset(desc[0].data, 0, sizeof(desc[0].data));
1844 memset(desc[1].data, 0, sizeof(desc[0].data));
1845 memset(desc[2].data, 0, sizeof(desc[0].data));
1849 * In current version VF is not supported when PF is driven by DPDK
1850 * driver, just need to configure parameters for PF vport.
1852 vf_id = HNS3_PF_FUNC_ID;
1853 hns3_update_desc_vfid(desc, vf_id, false);
1854 ret = hns3_add_mac_vlan_tbl(hw, &req, desc);
1857 hns3_err(hw, "mc mac vlan table is full");
1858 hns3_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
1860 hns3_err(hw, "failed to add mc mac addr(%s): %d", mac_str, ret);
1867 hns3_remove_mc_addr(struct hns3_hw *hw, struct rte_ether_addr *mac_addr)
1869 struct hns3_mac_vlan_tbl_entry_cmd req;
1870 struct hns3_cmd_desc desc[3];
1871 char mac_str[RTE_ETHER_ADDR_FMT_SIZE];
1875 /* Check if mac addr is valid */
1876 if (!rte_is_multicast_ether_addr(mac_addr)) {
1877 hns3_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
1879 hns3_err(hw, "Failed to rm mc mac addr, addr(%s) invalid",
1884 memset(&req, 0, sizeof(req));
1885 hns3_set_bit(req.entry_type, HNS3_MAC_VLAN_BIT0_EN_B, 0);
1886 hns3_prepare_mac_addr(&req, mac_addr->addr_bytes, true);
1887 ret = hns3_lookup_mac_vlan_tbl(hw, &req, desc, true);
1890 * This mac addr exist, remove this handle's VFID for it.
1891 * In current version VF is not supported when PF is driven by
1892 * DPDK driver, just need to configure parameters for PF vport.
1894 vf_id = HNS3_PF_FUNC_ID;
1895 hns3_update_desc_vfid(desc, vf_id, true);
1897 /* All the vfid is zero, so need to delete this entry */
1898 ret = hns3_remove_mac_vlan_tbl(hw, &req);
1899 } else if (ret == -ENOENT) {
1900 /* This mac addr doesn't exist. */
1905 hns3_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
1907 hns3_err(hw, "Failed to rm mc mac addr(%s): %d", mac_str, ret);
1914 hns3_set_mc_addr_chk_param(struct hns3_hw *hw,
1915 struct rte_ether_addr *mc_addr_set,
1916 uint32_t nb_mc_addr)
1918 char mac_str[RTE_ETHER_ADDR_FMT_SIZE];
1919 struct rte_ether_addr *addr;
1923 if (nb_mc_addr > HNS3_MC_MACADDR_NUM) {
1924 hns3_err(hw, "failed to set mc mac addr, nb_mc_addr(%u) "
1925 "invalid. valid range: 0~%d",
1926 nb_mc_addr, HNS3_MC_MACADDR_NUM);
1930 /* Check if input mac addresses are valid */
1931 for (i = 0; i < nb_mc_addr; i++) {
1932 addr = &mc_addr_set[i];
1933 if (!rte_is_multicast_ether_addr(addr)) {
1934 hns3_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
1937 "failed to set mc mac addr, addr(%s) invalid.",
1942 /* Check if there are duplicate addresses */
1943 for (j = i + 1; j < nb_mc_addr; j++) {
1944 if (rte_is_same_ether_addr(addr, &mc_addr_set[j])) {
1945 hns3_ether_format_addr(mac_str,
1946 RTE_ETHER_ADDR_FMT_SIZE,
1948 hns3_err(hw, "failed to set mc mac addr, "
1949 "addrs invalid. two same addrs(%s).",
1956 * Check if there are duplicate addresses between mac_addrs
1959 for (j = 0; j < HNS3_UC_MACADDR_NUM; j++) {
1960 if (rte_is_same_ether_addr(addr,
1961 &hw->data->mac_addrs[j])) {
1962 hns3_ether_format_addr(mac_str,
1963 RTE_ETHER_ADDR_FMT_SIZE,
1965 hns3_err(hw, "failed to set mc mac addr, "
1966 "addrs invalid. addrs(%s) has already "
1967 "configured in mac_addr add API",
1978 hns3_set_mc_addr_calc_addr(struct hns3_hw *hw,
1979 struct rte_ether_addr *mc_addr_set,
1981 struct rte_ether_addr *reserved_addr_list,
1982 int *reserved_addr_num,
1983 struct rte_ether_addr *add_addr_list,
1985 struct rte_ether_addr *rm_addr_list,
1988 struct rte_ether_addr *addr;
1989 int current_addr_num;
1990 int reserved_num = 0;
1998 /* Calculate the mc mac address list that should be removed */
1999 current_addr_num = hw->mc_addrs_num;
2000 for (i = 0; i < current_addr_num; i++) {
2001 addr = &hw->mc_addrs[i];
2003 for (j = 0; j < mc_addr_num; j++) {
2004 if (rte_is_same_ether_addr(addr, &mc_addr_set[j])) {
2011 rte_ether_addr_copy(addr, &rm_addr_list[rm_num]);
2014 rte_ether_addr_copy(addr,
2015 &reserved_addr_list[reserved_num]);
2020 /* Calculate the mc mac address list that should be added */
2021 for (i = 0; i < mc_addr_num; i++) {
2022 addr = &mc_addr_set[i];
2024 for (j = 0; j < current_addr_num; j++) {
2025 if (rte_is_same_ether_addr(addr, &hw->mc_addrs[j])) {
2032 rte_ether_addr_copy(addr, &add_addr_list[add_num]);
2037 /* Reorder the mc mac address list maintained by driver */
2038 for (i = 0; i < reserved_num; i++)
2039 rte_ether_addr_copy(&reserved_addr_list[i], &hw->mc_addrs[i]);
2041 for (i = 0; i < rm_num; i++) {
2042 num = reserved_num + i;
2043 rte_ether_addr_copy(&rm_addr_list[i], &hw->mc_addrs[num]);
2046 *reserved_addr_num = reserved_num;
2047 *add_addr_num = add_num;
2048 *rm_addr_num = rm_num;
2052 hns3_set_mc_mac_addr_list(struct rte_eth_dev *dev,
2053 struct rte_ether_addr *mc_addr_set,
2054 uint32_t nb_mc_addr)
2056 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2057 struct rte_ether_addr reserved_addr_list[HNS3_MC_MACADDR_NUM];
2058 struct rte_ether_addr add_addr_list[HNS3_MC_MACADDR_NUM];
2059 struct rte_ether_addr rm_addr_list[HNS3_MC_MACADDR_NUM];
2060 struct rte_ether_addr *addr;
2061 int reserved_addr_num;
2069 /* Check if input parameters are valid */
2070 ret = hns3_set_mc_addr_chk_param(hw, mc_addr_set, nb_mc_addr);
2074 rte_spinlock_lock(&hw->lock);
2077 * Calculate the mc mac address lists those should be removed and be
2078 * added, Reorder the mc mac address list maintained by driver.
2080 mc_addr_num = (int)nb_mc_addr;
2081 hns3_set_mc_addr_calc_addr(hw, mc_addr_set, mc_addr_num,
2082 reserved_addr_list, &reserved_addr_num,
2083 add_addr_list, &add_addr_num,
2084 rm_addr_list, &rm_addr_num);
2086 /* Remove mc mac addresses */
2087 for (i = 0; i < rm_addr_num; i++) {
2088 num = rm_addr_num - i - 1;
2089 addr = &rm_addr_list[num];
2090 ret = hns3_remove_mc_addr(hw, addr);
2092 rte_spinlock_unlock(&hw->lock);
2098 /* Add mc mac addresses */
2099 for (i = 0; i < add_addr_num; i++) {
2100 addr = &add_addr_list[i];
2101 ret = hns3_add_mc_addr(hw, addr);
2103 rte_spinlock_unlock(&hw->lock);
2107 num = reserved_addr_num + i;
2108 rte_ether_addr_copy(addr, &hw->mc_addrs[num]);
2111 rte_spinlock_unlock(&hw->lock);
2117 hns3_configure_all_mc_mac_addr(struct hns3_adapter *hns, bool del)
2119 char mac_str[RTE_ETHER_ADDR_FMT_SIZE];
2120 struct hns3_hw *hw = &hns->hw;
2121 struct rte_ether_addr *addr;
2126 for (i = 0; i < hw->mc_addrs_num; i++) {
2127 addr = &hw->mc_addrs[i];
2128 if (!rte_is_multicast_ether_addr(addr))
2131 ret = hns3_remove_mc_addr(hw, addr);
2133 ret = hns3_add_mc_addr(hw, addr);
2136 hns3_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
2138 hns3_dbg(hw, "%s mc mac addr: %s failed for pf: ret = %d",
2139 del ? "Remove" : "Restore", mac_str, ret);
2146 hns3_check_mq_mode(struct rte_eth_dev *dev)
2148 enum rte_eth_rx_mq_mode rx_mq_mode = dev->data->dev_conf.rxmode.mq_mode;
2149 enum rte_eth_tx_mq_mode tx_mq_mode = dev->data->dev_conf.txmode.mq_mode;
2150 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2151 struct hns3_pf *pf = HNS3_DEV_PRIVATE_TO_PF(dev->data->dev_private);
2152 struct rte_eth_dcb_rx_conf *dcb_rx_conf;
2153 struct rte_eth_dcb_tx_conf *dcb_tx_conf;
2158 dcb_rx_conf = &dev->data->dev_conf.rx_adv_conf.dcb_rx_conf;
2159 dcb_tx_conf = &dev->data->dev_conf.tx_adv_conf.dcb_tx_conf;
2161 if (rx_mq_mode == ETH_MQ_RX_VMDQ_DCB_RSS) {
2162 hns3_err(hw, "ETH_MQ_RX_VMDQ_DCB_RSS is not supported. "
2163 "rx_mq_mode = %d", rx_mq_mode);
2167 if (rx_mq_mode == ETH_MQ_RX_VMDQ_DCB ||
2168 tx_mq_mode == ETH_MQ_TX_VMDQ_DCB) {
2169 hns3_err(hw, "ETH_MQ_RX_VMDQ_DCB and ETH_MQ_TX_VMDQ_DCB "
2170 "is not supported. rx_mq_mode = %d, tx_mq_mode = %d",
2171 rx_mq_mode, tx_mq_mode);
2175 if (rx_mq_mode == ETH_MQ_RX_DCB_RSS) {
2176 if (dcb_rx_conf->nb_tcs > pf->tc_max) {
2177 hns3_err(hw, "nb_tcs(%u) > max_tc(%u) driver supported.",
2178 dcb_rx_conf->nb_tcs, pf->tc_max);
2182 if (!(dcb_rx_conf->nb_tcs == HNS3_4_TCS ||
2183 dcb_rx_conf->nb_tcs == HNS3_8_TCS)) {
2184 hns3_err(hw, "on ETH_MQ_RX_DCB_RSS mode, "
2185 "nb_tcs(%d) != %d or %d in rx direction.",
2186 dcb_rx_conf->nb_tcs, HNS3_4_TCS, HNS3_8_TCS);
2190 if (dcb_rx_conf->nb_tcs != dcb_tx_conf->nb_tcs) {
2191 hns3_err(hw, "num_tcs(%d) of tx is not equal to rx(%d)",
2192 dcb_tx_conf->nb_tcs, dcb_rx_conf->nb_tcs);
2196 for (i = 0; i < HNS3_MAX_USER_PRIO; i++) {
2197 if (dcb_rx_conf->dcb_tc[i] != dcb_tx_conf->dcb_tc[i]) {
2198 hns3_err(hw, "dcb_tc[%d] = %u in rx direction, "
2199 "is not equal to one in tx direction.",
2200 i, dcb_rx_conf->dcb_tc[i]);
2203 if (dcb_rx_conf->dcb_tc[i] > max_tc)
2204 max_tc = dcb_rx_conf->dcb_tc[i];
2207 num_tc = max_tc + 1;
2208 if (num_tc > dcb_rx_conf->nb_tcs) {
2209 hns3_err(hw, "max num_tc(%u) mapped > nb_tcs(%u)",
2210 num_tc, dcb_rx_conf->nb_tcs);
2219 hns3_check_dcb_cfg(struct rte_eth_dev *dev)
2221 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2223 if (!hns3_dev_dcb_supported(hw)) {
2224 hns3_err(hw, "this port does not support dcb configurations.");
2228 if (hw->current_fc_status == HNS3_FC_STATUS_MAC_PAUSE) {
2229 hns3_err(hw, "MAC pause enabled, cannot config dcb info.");
2233 /* Check multiple queue mode */
2234 return hns3_check_mq_mode(dev);
2238 hns3_bind_ring_with_vector(struct hns3_hw *hw, uint16_t vector_id, bool en,
2239 enum hns3_ring_type queue_type, uint16_t queue_id)
2241 struct hns3_cmd_desc desc;
2242 struct hns3_ctrl_vector_chain_cmd *req =
2243 (struct hns3_ctrl_vector_chain_cmd *)desc.data;
2244 enum hns3_cmd_status status;
2245 enum hns3_opcode_type op;
2246 uint16_t tqp_type_and_id = 0;
2250 op = en ? HNS3_OPC_ADD_RING_TO_VECTOR : HNS3_OPC_DEL_RING_TO_VECTOR;
2251 hns3_cmd_setup_basic_desc(&desc, op, false);
2252 req->int_vector_id = hns3_get_field(vector_id, HNS3_TQP_INT_ID_L_M,
2253 HNS3_TQP_INT_ID_L_S);
2254 req->int_vector_id_h = hns3_get_field(vector_id, HNS3_TQP_INT_ID_H_M,
2255 HNS3_TQP_INT_ID_H_S);
2257 if (queue_type == HNS3_RING_TYPE_RX)
2258 gl = HNS3_RING_GL_RX;
2260 gl = HNS3_RING_GL_TX;
2264 hns3_set_field(tqp_type_and_id, HNS3_INT_TYPE_M, HNS3_INT_TYPE_S,
2266 hns3_set_field(tqp_type_and_id, HNS3_TQP_ID_M, HNS3_TQP_ID_S, queue_id);
2267 hns3_set_field(tqp_type_and_id, HNS3_INT_GL_IDX_M, HNS3_INT_GL_IDX_S,
2269 req->tqp_type_and_id[0] = rte_cpu_to_le_16(tqp_type_and_id);
2270 req->int_cause_num = 1;
2271 status = hns3_cmd_send(hw, &desc, 1);
2273 hns3_err(hw, "%s TQP %u fail, vector_id is %u, status is %d.",
2274 en ? "Map" : "Unmap", queue_id, vector_id, status);
2282 hns3_init_ring_with_vector(struct hns3_hw *hw)
2289 * In hns3 network engine, vector 0 is always the misc interrupt of this
2290 * function, vector 1~N can be used respectively for the queues of the
2291 * function. Tx and Rx queues with the same number share the interrupt
2292 * vector. In the initialization clearing the all hardware mapping
2293 * relationship configurations between queues and interrupt vectors is
2294 * needed, so some error caused by the residual configurations, such as
2295 * the unexpected Tx interrupt, can be avoid.
2297 vec = hw->num_msi - 1; /* vector 0 for misc interrupt, not for queue */
2298 if (hw->intr.mapping_mode == HNS3_INTR_MAPPING_VEC_RSV_ONE)
2299 vec = vec - 1; /* the last interrupt is reserved */
2300 hw->intr_tqps_num = RTE_MIN(vec, hw->tqps_num);
2301 for (i = 0; i < hw->intr_tqps_num; i++) {
2303 * Set gap limiter/rate limiter/quanity limiter algorithm
2304 * configuration for interrupt coalesce of queue's interrupt.
2306 hns3_set_queue_intr_gl(hw, i, HNS3_RING_GL_RX,
2307 HNS3_TQP_INTR_GL_DEFAULT);
2308 hns3_set_queue_intr_gl(hw, i, HNS3_RING_GL_TX,
2309 HNS3_TQP_INTR_GL_DEFAULT);
2310 hns3_set_queue_intr_rl(hw, i, HNS3_TQP_INTR_RL_DEFAULT);
2312 * QL(quantity limiter) is not used currently, just set 0 to
2315 hns3_set_queue_intr_ql(hw, i, HNS3_TQP_INTR_QL_DEFAULT);
2317 ret = hns3_bind_ring_with_vector(hw, vec, false,
2318 HNS3_RING_TYPE_TX, i);
2320 PMD_INIT_LOG(ERR, "PF fail to unbind TX ring(%d) with "
2321 "vector: %u, ret=%d", i, vec, ret);
2325 ret = hns3_bind_ring_with_vector(hw, vec, false,
2326 HNS3_RING_TYPE_RX, i);
2328 PMD_INIT_LOG(ERR, "PF fail to unbind RX ring(%d) with "
2329 "vector: %u, ret=%d", i, vec, ret);
2338 hns3_dev_configure(struct rte_eth_dev *dev)
2340 struct hns3_adapter *hns = dev->data->dev_private;
2341 struct rte_eth_conf *conf = &dev->data->dev_conf;
2342 enum rte_eth_rx_mq_mode mq_mode = conf->rxmode.mq_mode;
2343 struct hns3_hw *hw = &hns->hw;
2344 uint16_t nb_rx_q = dev->data->nb_rx_queues;
2345 uint16_t nb_tx_q = dev->data->nb_tx_queues;
2346 struct rte_eth_rss_conf rss_conf;
2347 uint32_t max_rx_pkt_len;
2352 hw->cfg_max_queues = RTE_MAX(nb_rx_q, nb_tx_q);
2355 * Some versions of hardware network engine does not support
2356 * individually enable/disable/reset the Tx or Rx queue. These devices
2357 * must enable/disable/reset Tx and Rx queues at the same time. When the
2358 * numbers of Tx queues allocated by upper applications are not equal to
2359 * the numbers of Rx queues, driver needs to setup fake Tx or Rx queues
2360 * to adjust numbers of Tx/Rx queues. otherwise, network engine can not
2361 * work as usual. But these fake queues are imperceptible, and can not
2362 * be used by upper applications.
2364 if (!hns3_dev_indep_txrx_supported(hw)) {
2365 ret = hns3_set_fake_rx_or_tx_queues(dev, nb_rx_q, nb_tx_q);
2367 hns3_err(hw, "fail to set Rx/Tx fake queues, ret = %d.",
2373 hw->adapter_state = HNS3_NIC_CONFIGURING;
2374 if (conf->link_speeds & ETH_LINK_SPEED_FIXED) {
2375 hns3_err(hw, "setting link speed/duplex not supported");
2380 if ((uint32_t)mq_mode & ETH_MQ_RX_DCB_FLAG) {
2381 ret = hns3_check_dcb_cfg(dev);
2386 /* When RSS is not configured, redirect the packet queue 0 */
2387 if ((uint32_t)mq_mode & ETH_MQ_RX_RSS_FLAG) {
2388 conf->rxmode.offloads |= DEV_RX_OFFLOAD_RSS_HASH;
2389 rss_conf = conf->rx_adv_conf.rss_conf;
2390 hw->rss_dis_flag = false;
2391 ret = hns3_dev_rss_hash_update(dev, &rss_conf);
2397 * If jumbo frames are enabled, MTU needs to be refreshed
2398 * according to the maximum RX packet length.
2400 if (conf->rxmode.offloads & DEV_RX_OFFLOAD_JUMBO_FRAME) {
2401 max_rx_pkt_len = conf->rxmode.max_rx_pkt_len;
2402 if (max_rx_pkt_len > HNS3_MAX_FRAME_LEN ||
2403 max_rx_pkt_len <= HNS3_DEFAULT_FRAME_LEN) {
2404 hns3_err(hw, "maximum Rx packet length must be greater "
2405 "than %u and less than %u when jumbo frame enabled.",
2406 (uint16_t)HNS3_DEFAULT_FRAME_LEN,
2407 (uint16_t)HNS3_MAX_FRAME_LEN);
2412 mtu = (uint16_t)HNS3_PKTLEN_TO_MTU(max_rx_pkt_len);
2413 ret = hns3_dev_mtu_set(dev, mtu);
2416 dev->data->mtu = mtu;
2419 ret = hns3_dev_configure_vlan(dev);
2423 /* config hardware GRO */
2424 gro_en = conf->rxmode.offloads & DEV_RX_OFFLOAD_TCP_LRO ? true : false;
2425 ret = hns3_config_gro(hw, gro_en);
2429 hns->rx_simple_allowed = true;
2430 hns->rx_vec_allowed = true;
2431 hns->tx_simple_allowed = true;
2432 hns->tx_vec_allowed = true;
2434 hns3_init_rx_ptype_tble(dev);
2435 hw->adapter_state = HNS3_NIC_CONFIGURED;
2440 (void)hns3_set_fake_rx_or_tx_queues(dev, 0, 0);
2441 hw->adapter_state = HNS3_NIC_INITIALIZED;
2447 hns3_set_mac_mtu(struct hns3_hw *hw, uint16_t new_mps)
2449 struct hns3_config_max_frm_size_cmd *req;
2450 struct hns3_cmd_desc desc;
2452 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_CONFIG_MAX_FRM_SIZE, false);
2454 req = (struct hns3_config_max_frm_size_cmd *)desc.data;
2455 req->max_frm_size = rte_cpu_to_le_16(new_mps);
2456 req->min_frm_size = RTE_ETHER_MIN_LEN;
2458 return hns3_cmd_send(hw, &desc, 1);
2462 hns3_config_mtu(struct hns3_hw *hw, uint16_t mps)
2464 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
2465 uint16_t original_mps = hns->pf.mps;
2469 ret = hns3_set_mac_mtu(hw, mps);
2471 hns3_err(hw, "failed to set mtu, ret = %d", ret);
2476 ret = hns3_buffer_alloc(hw);
2478 hns3_err(hw, "failed to allocate buffer, ret = %d", ret);
2485 err = hns3_set_mac_mtu(hw, original_mps);
2487 hns3_err(hw, "fail to rollback MTU, err = %d", err);
2490 hns->pf.mps = original_mps;
2496 hns3_dev_mtu_set(struct rte_eth_dev *dev, uint16_t mtu)
2498 struct hns3_adapter *hns = dev->data->dev_private;
2499 uint32_t frame_size = mtu + HNS3_ETH_OVERHEAD;
2500 struct hns3_hw *hw = &hns->hw;
2501 bool is_jumbo_frame;
2504 if (dev->data->dev_started) {
2505 hns3_err(hw, "Failed to set mtu, port %u must be stopped "
2506 "before configuration", dev->data->port_id);
2510 rte_spinlock_lock(&hw->lock);
2511 is_jumbo_frame = frame_size > HNS3_DEFAULT_FRAME_LEN ? true : false;
2512 frame_size = RTE_MAX(frame_size, HNS3_DEFAULT_FRAME_LEN);
2515 * Maximum value of frame_size is HNS3_MAX_FRAME_LEN, so it can safely
2516 * assign to "uint16_t" type variable.
2518 ret = hns3_config_mtu(hw, (uint16_t)frame_size);
2520 rte_spinlock_unlock(&hw->lock);
2521 hns3_err(hw, "Failed to set mtu, port %u mtu %u: %d",
2522 dev->data->port_id, mtu, ret);
2527 dev->data->dev_conf.rxmode.offloads |=
2528 DEV_RX_OFFLOAD_JUMBO_FRAME;
2530 dev->data->dev_conf.rxmode.offloads &=
2531 ~DEV_RX_OFFLOAD_JUMBO_FRAME;
2532 dev->data->dev_conf.rxmode.max_rx_pkt_len = frame_size;
2533 rte_spinlock_unlock(&hw->lock);
2539 hns3_dev_infos_get(struct rte_eth_dev *eth_dev, struct rte_eth_dev_info *info)
2541 struct hns3_adapter *hns = eth_dev->data->dev_private;
2542 struct hns3_hw *hw = &hns->hw;
2543 uint16_t queue_num = hw->tqps_num;
2546 * In interrupt mode, 'max_rx_queues' is set based on the number of
2547 * MSI-X interrupt resources of the hardware.
2549 if (hw->data->dev_conf.intr_conf.rxq == 1)
2550 queue_num = hw->intr_tqps_num;
2552 info->max_rx_queues = queue_num;
2553 info->max_tx_queues = hw->tqps_num;
2554 info->max_rx_pktlen = HNS3_MAX_FRAME_LEN; /* CRC included */
2555 info->min_rx_bufsize = HNS3_MIN_BD_BUF_SIZE;
2556 info->max_mac_addrs = HNS3_UC_MACADDR_NUM;
2557 info->max_mtu = info->max_rx_pktlen - HNS3_ETH_OVERHEAD;
2558 info->max_lro_pkt_size = HNS3_MAX_LRO_SIZE;
2559 info->rx_offload_capa = (DEV_RX_OFFLOAD_IPV4_CKSUM |
2560 DEV_RX_OFFLOAD_TCP_CKSUM |
2561 DEV_RX_OFFLOAD_UDP_CKSUM |
2562 DEV_RX_OFFLOAD_SCTP_CKSUM |
2563 DEV_RX_OFFLOAD_OUTER_IPV4_CKSUM |
2564 DEV_RX_OFFLOAD_OUTER_UDP_CKSUM |
2565 DEV_RX_OFFLOAD_KEEP_CRC |
2566 DEV_RX_OFFLOAD_SCATTER |
2567 DEV_RX_OFFLOAD_VLAN_STRIP |
2568 DEV_RX_OFFLOAD_VLAN_FILTER |
2569 DEV_RX_OFFLOAD_JUMBO_FRAME |
2570 DEV_RX_OFFLOAD_RSS_HASH |
2571 DEV_RX_OFFLOAD_TCP_LRO);
2572 info->tx_offload_capa = (DEV_TX_OFFLOAD_OUTER_IPV4_CKSUM |
2573 DEV_TX_OFFLOAD_IPV4_CKSUM |
2574 DEV_TX_OFFLOAD_TCP_CKSUM |
2575 DEV_TX_OFFLOAD_UDP_CKSUM |
2576 DEV_TX_OFFLOAD_SCTP_CKSUM |
2577 DEV_TX_OFFLOAD_MULTI_SEGS |
2578 DEV_TX_OFFLOAD_TCP_TSO |
2579 DEV_TX_OFFLOAD_VXLAN_TNL_TSO |
2580 DEV_TX_OFFLOAD_GRE_TNL_TSO |
2581 DEV_TX_OFFLOAD_GENEVE_TNL_TSO |
2582 DEV_TX_OFFLOAD_MBUF_FAST_FREE |
2583 hns3_txvlan_cap_get(hw));
2585 if (hns3_dev_indep_txrx_supported(hw))
2586 info->dev_capa = RTE_ETH_DEV_CAPA_RUNTIME_RX_QUEUE_SETUP |
2587 RTE_ETH_DEV_CAPA_RUNTIME_TX_QUEUE_SETUP;
2589 info->rx_desc_lim = (struct rte_eth_desc_lim) {
2590 .nb_max = HNS3_MAX_RING_DESC,
2591 .nb_min = HNS3_MIN_RING_DESC,
2592 .nb_align = HNS3_ALIGN_RING_DESC,
2595 info->tx_desc_lim = (struct rte_eth_desc_lim) {
2596 .nb_max = HNS3_MAX_RING_DESC,
2597 .nb_min = HNS3_MIN_RING_DESC,
2598 .nb_align = HNS3_ALIGN_RING_DESC,
2599 .nb_seg_max = HNS3_MAX_TSO_BD_PER_PKT,
2600 .nb_mtu_seg_max = hw->max_non_tso_bd_num,
2603 info->default_rxconf = (struct rte_eth_rxconf) {
2604 .rx_free_thresh = HNS3_DEFAULT_RX_FREE_THRESH,
2606 * If there are no available Rx buffer descriptors, incoming
2607 * packets are always dropped by hardware based on hns3 network
2613 info->default_txconf = (struct rte_eth_txconf) {
2614 .tx_rs_thresh = HNS3_DEFAULT_TX_RS_THRESH,
2618 info->vmdq_queue_num = 0;
2620 info->reta_size = hw->rss_ind_tbl_size;
2621 info->hash_key_size = HNS3_RSS_KEY_SIZE;
2622 info->flow_type_rss_offloads = HNS3_ETH_RSS_SUPPORT;
2624 info->default_rxportconf.burst_size = HNS3_DEFAULT_PORT_CONF_BURST_SIZE;
2625 info->default_txportconf.burst_size = HNS3_DEFAULT_PORT_CONF_BURST_SIZE;
2626 info->default_rxportconf.nb_queues = HNS3_DEFAULT_PORT_CONF_QUEUES_NUM;
2627 info->default_txportconf.nb_queues = HNS3_DEFAULT_PORT_CONF_QUEUES_NUM;
2628 info->default_rxportconf.ring_size = HNS3_DEFAULT_RING_DESC;
2629 info->default_txportconf.ring_size = HNS3_DEFAULT_RING_DESC;
2635 hns3_fw_version_get(struct rte_eth_dev *eth_dev, char *fw_version,
2638 struct hns3_adapter *hns = eth_dev->data->dev_private;
2639 struct hns3_hw *hw = &hns->hw;
2640 uint32_t version = hw->fw_version;
2643 ret = snprintf(fw_version, fw_size, "%lu.%lu.%lu.%lu",
2644 hns3_get_field(version, HNS3_FW_VERSION_BYTE3_M,
2645 HNS3_FW_VERSION_BYTE3_S),
2646 hns3_get_field(version, HNS3_FW_VERSION_BYTE2_M,
2647 HNS3_FW_VERSION_BYTE2_S),
2648 hns3_get_field(version, HNS3_FW_VERSION_BYTE1_M,
2649 HNS3_FW_VERSION_BYTE1_S),
2650 hns3_get_field(version, HNS3_FW_VERSION_BYTE0_M,
2651 HNS3_FW_VERSION_BYTE0_S));
2652 ret += 1; /* add the size of '\0' */
2653 if (fw_size < (uint32_t)ret)
2660 hns3_dev_link_update(struct rte_eth_dev *eth_dev,
2661 __rte_unused int wait_to_complete)
2663 struct hns3_adapter *hns = eth_dev->data->dev_private;
2664 struct hns3_hw *hw = &hns->hw;
2665 struct hns3_mac *mac = &hw->mac;
2666 struct rte_eth_link new_link;
2668 if (!hns3_is_reset_pending(hns)) {
2669 hns3_update_link_status(hw);
2670 hns3_update_link_info(eth_dev);
2673 memset(&new_link, 0, sizeof(new_link));
2674 switch (mac->link_speed) {
2675 case ETH_SPEED_NUM_10M:
2676 case ETH_SPEED_NUM_100M:
2677 case ETH_SPEED_NUM_1G:
2678 case ETH_SPEED_NUM_10G:
2679 case ETH_SPEED_NUM_25G:
2680 case ETH_SPEED_NUM_40G:
2681 case ETH_SPEED_NUM_50G:
2682 case ETH_SPEED_NUM_100G:
2683 case ETH_SPEED_NUM_200G:
2684 new_link.link_speed = mac->link_speed;
2687 new_link.link_speed = ETH_SPEED_NUM_100M;
2691 new_link.link_duplex = mac->link_duplex;
2692 new_link.link_status = mac->link_status ? ETH_LINK_UP : ETH_LINK_DOWN;
2693 new_link.link_autoneg =
2694 !(eth_dev->data->dev_conf.link_speeds & ETH_LINK_SPEED_FIXED);
2696 return rte_eth_linkstatus_set(eth_dev, &new_link);
2700 hns3_parse_func_status(struct hns3_hw *hw, struct hns3_func_status_cmd *status)
2702 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
2703 struct hns3_pf *pf = &hns->pf;
2705 if (!(status->pf_state & HNS3_PF_STATE_DONE))
2708 pf->is_main_pf = (status->pf_state & HNS3_PF_STATE_MAIN) ? true : false;
2714 hns3_query_function_status(struct hns3_hw *hw)
2716 #define HNS3_QUERY_MAX_CNT 10
2717 #define HNS3_QUERY_SLEEP_MSCOEND 1
2718 struct hns3_func_status_cmd *req;
2719 struct hns3_cmd_desc desc;
2723 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_QUERY_FUNC_STATUS, true);
2724 req = (struct hns3_func_status_cmd *)desc.data;
2727 ret = hns3_cmd_send(hw, &desc, 1);
2729 PMD_INIT_LOG(ERR, "query function status failed %d",
2734 /* Check pf reset is done */
2738 rte_delay_ms(HNS3_QUERY_SLEEP_MSCOEND);
2739 } while (timeout++ < HNS3_QUERY_MAX_CNT);
2741 return hns3_parse_func_status(hw, req);
2745 hns3_get_pf_max_tqp_num(struct hns3_hw *hw)
2747 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
2748 struct hns3_pf *pf = &hns->pf;
2750 if (pf->tqp_config_mode == HNS3_FLEX_MAX_TQP_NUM_MODE) {
2752 * The total_tqps_num obtained from firmware is maximum tqp
2753 * numbers of this port, which should be used for PF and VFs.
2754 * There is no need for pf to have so many tqp numbers in
2755 * most cases. RTE_LIBRTE_HNS3_MAX_TQP_NUM_PER_PF,
2756 * coming from config file, is assigned to maximum queue number
2757 * for the PF of this port by user. So users can modify the
2758 * maximum queue number of PF according to their own application
2759 * scenarios, which is more flexible to use. In addition, many
2760 * memories can be saved due to allocating queue statistics
2761 * room according to the actual number of queues required. The
2762 * maximum queue number of PF for network engine with
2763 * revision_id greater than 0x30 is assigned by config file.
2765 if (RTE_LIBRTE_HNS3_MAX_TQP_NUM_PER_PF <= 0) {
2766 hns3_err(hw, "RTE_LIBRTE_HNS3_MAX_TQP_NUM_PER_PF(%d) "
2767 "must be greater than 0.",
2768 RTE_LIBRTE_HNS3_MAX_TQP_NUM_PER_PF);
2772 hw->tqps_num = RTE_MIN(RTE_LIBRTE_HNS3_MAX_TQP_NUM_PER_PF,
2773 hw->total_tqps_num);
2776 * Due to the limitation on the number of PF interrupts
2777 * available, the maximum queue number assigned to PF on
2778 * the network engine with revision_id 0x21 is 64.
2780 hw->tqps_num = RTE_MIN(hw->total_tqps_num,
2781 HNS3_MAX_TQP_NUM_HIP08_PF);
2788 hns3_query_pf_resource(struct hns3_hw *hw)
2790 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
2791 struct hns3_pf *pf = &hns->pf;
2792 struct hns3_pf_res_cmd *req;
2793 struct hns3_cmd_desc desc;
2796 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_QUERY_PF_RSRC, true);
2797 ret = hns3_cmd_send(hw, &desc, 1);
2799 PMD_INIT_LOG(ERR, "query pf resource failed %d", ret);
2803 req = (struct hns3_pf_res_cmd *)desc.data;
2804 hw->total_tqps_num = rte_le_to_cpu_16(req->tqp_num) +
2805 rte_le_to_cpu_16(req->ext_tqp_num);
2806 ret = hns3_get_pf_max_tqp_num(hw);
2810 pf->pkt_buf_size = rte_le_to_cpu_16(req->buf_size) << HNS3_BUF_UNIT_S;
2811 pf->func_num = rte_le_to_cpu_16(req->pf_own_fun_number);
2813 if (req->tx_buf_size)
2815 rte_le_to_cpu_16(req->tx_buf_size) << HNS3_BUF_UNIT_S;
2817 pf->tx_buf_size = HNS3_DEFAULT_TX_BUF;
2819 pf->tx_buf_size = roundup(pf->tx_buf_size, HNS3_BUF_SIZE_UNIT);
2821 if (req->dv_buf_size)
2823 rte_le_to_cpu_16(req->dv_buf_size) << HNS3_BUF_UNIT_S;
2825 pf->dv_buf_size = HNS3_DEFAULT_DV;
2827 pf->dv_buf_size = roundup(pf->dv_buf_size, HNS3_BUF_SIZE_UNIT);
2830 hns3_get_field(rte_le_to_cpu_16(req->nic_pf_intr_vector_number),
2831 HNS3_PF_VEC_NUM_M, HNS3_PF_VEC_NUM_S);
2837 hns3_parse_cfg(struct hns3_cfg *cfg, struct hns3_cmd_desc *desc)
2839 struct hns3_cfg_param_cmd *req;
2840 uint64_t mac_addr_tmp_high;
2841 uint8_t ext_rss_size_max;
2842 uint64_t mac_addr_tmp;
2845 req = (struct hns3_cfg_param_cmd *)desc[0].data;
2847 /* get the configuration */
2848 cfg->vmdq_vport_num = hns3_get_field(rte_le_to_cpu_32(req->param[0]),
2849 HNS3_CFG_VMDQ_M, HNS3_CFG_VMDQ_S);
2850 cfg->tc_num = hns3_get_field(rte_le_to_cpu_32(req->param[0]),
2851 HNS3_CFG_TC_NUM_M, HNS3_CFG_TC_NUM_S);
2852 cfg->tqp_desc_num = hns3_get_field(rte_le_to_cpu_32(req->param[0]),
2853 HNS3_CFG_TQP_DESC_N_M,
2854 HNS3_CFG_TQP_DESC_N_S);
2856 cfg->phy_addr = hns3_get_field(rte_le_to_cpu_32(req->param[1]),
2857 HNS3_CFG_PHY_ADDR_M,
2858 HNS3_CFG_PHY_ADDR_S);
2859 cfg->media_type = hns3_get_field(rte_le_to_cpu_32(req->param[1]),
2860 HNS3_CFG_MEDIA_TP_M,
2861 HNS3_CFG_MEDIA_TP_S);
2862 cfg->rx_buf_len = hns3_get_field(rte_le_to_cpu_32(req->param[1]),
2863 HNS3_CFG_RX_BUF_LEN_M,
2864 HNS3_CFG_RX_BUF_LEN_S);
2865 /* get mac address */
2866 mac_addr_tmp = rte_le_to_cpu_32(req->param[2]);
2867 mac_addr_tmp_high = hns3_get_field(rte_le_to_cpu_32(req->param[3]),
2868 HNS3_CFG_MAC_ADDR_H_M,
2869 HNS3_CFG_MAC_ADDR_H_S);
2871 mac_addr_tmp |= (mac_addr_tmp_high << 31) << 1;
2873 cfg->default_speed = hns3_get_field(rte_le_to_cpu_32(req->param[3]),
2874 HNS3_CFG_DEFAULT_SPEED_M,
2875 HNS3_CFG_DEFAULT_SPEED_S);
2876 cfg->rss_size_max = hns3_get_field(rte_le_to_cpu_32(req->param[3]),
2877 HNS3_CFG_RSS_SIZE_M,
2878 HNS3_CFG_RSS_SIZE_S);
2880 for (i = 0; i < RTE_ETHER_ADDR_LEN; i++)
2881 cfg->mac_addr[i] = (mac_addr_tmp >> (8 * i)) & 0xff;
2883 req = (struct hns3_cfg_param_cmd *)desc[1].data;
2884 cfg->numa_node_map = rte_le_to_cpu_32(req->param[0]);
2886 cfg->speed_ability = hns3_get_field(rte_le_to_cpu_32(req->param[1]),
2887 HNS3_CFG_SPEED_ABILITY_M,
2888 HNS3_CFG_SPEED_ABILITY_S);
2889 cfg->umv_space = hns3_get_field(rte_le_to_cpu_32(req->param[1]),
2890 HNS3_CFG_UMV_TBL_SPACE_M,
2891 HNS3_CFG_UMV_TBL_SPACE_S);
2892 if (!cfg->umv_space)
2893 cfg->umv_space = HNS3_DEFAULT_UMV_SPACE_PER_PF;
2895 ext_rss_size_max = hns3_get_field(rte_le_to_cpu_32(req->param[2]),
2896 HNS3_CFG_EXT_RSS_SIZE_M,
2897 HNS3_CFG_EXT_RSS_SIZE_S);
2900 * Field ext_rss_size_max obtained from firmware will be more flexible
2901 * for future changes and expansions, which is an exponent of 2, instead
2902 * of reading out directly. If this field is not zero, hns3 PF PMD
2903 * driver uses it as rss_size_max under one TC. Device, whose revision
2904 * id is greater than or equal to PCI_REVISION_ID_HIP09_A, obtains the
2905 * maximum number of queues supported under a TC through this field.
2907 if (ext_rss_size_max)
2908 cfg->rss_size_max = 1U << ext_rss_size_max;
2911 /* hns3_get_board_cfg: query the static parameter from NCL_config file in flash
2912 * @hw: pointer to struct hns3_hw
2913 * @hcfg: the config structure to be getted
2916 hns3_get_board_cfg(struct hns3_hw *hw, struct hns3_cfg *hcfg)
2918 struct hns3_cmd_desc desc[HNS3_PF_CFG_DESC_NUM];
2919 struct hns3_cfg_param_cmd *req;
2924 for (i = 0; i < HNS3_PF_CFG_DESC_NUM; i++) {
2926 req = (struct hns3_cfg_param_cmd *)desc[i].data;
2927 hns3_cmd_setup_basic_desc(&desc[i], HNS3_OPC_GET_CFG_PARAM,
2929 hns3_set_field(offset, HNS3_CFG_OFFSET_M, HNS3_CFG_OFFSET_S,
2930 i * HNS3_CFG_RD_LEN_BYTES);
2931 /* Len should be divided by 4 when send to hardware */
2932 hns3_set_field(offset, HNS3_CFG_RD_LEN_M, HNS3_CFG_RD_LEN_S,
2933 HNS3_CFG_RD_LEN_BYTES / HNS3_CFG_RD_LEN_UNIT);
2934 req->offset = rte_cpu_to_le_32(offset);
2937 ret = hns3_cmd_send(hw, desc, HNS3_PF_CFG_DESC_NUM);
2939 PMD_INIT_LOG(ERR, "get config failed %d.", ret);
2943 hns3_parse_cfg(hcfg, desc);
2949 hns3_parse_speed(int speed_cmd, uint32_t *speed)
2951 switch (speed_cmd) {
2952 case HNS3_CFG_SPEED_10M:
2953 *speed = ETH_SPEED_NUM_10M;
2955 case HNS3_CFG_SPEED_100M:
2956 *speed = ETH_SPEED_NUM_100M;
2958 case HNS3_CFG_SPEED_1G:
2959 *speed = ETH_SPEED_NUM_1G;
2961 case HNS3_CFG_SPEED_10G:
2962 *speed = ETH_SPEED_NUM_10G;
2964 case HNS3_CFG_SPEED_25G:
2965 *speed = ETH_SPEED_NUM_25G;
2967 case HNS3_CFG_SPEED_40G:
2968 *speed = ETH_SPEED_NUM_40G;
2970 case HNS3_CFG_SPEED_50G:
2971 *speed = ETH_SPEED_NUM_50G;
2973 case HNS3_CFG_SPEED_100G:
2974 *speed = ETH_SPEED_NUM_100G;
2976 case HNS3_CFG_SPEED_200G:
2977 *speed = ETH_SPEED_NUM_200G;
2987 hns3_set_default_dev_specifications(struct hns3_hw *hw)
2989 hw->max_non_tso_bd_num = HNS3_MAX_NON_TSO_BD_PER_PKT;
2990 hw->rss_ind_tbl_size = HNS3_RSS_IND_TBL_SIZE;
2991 hw->rss_key_size = HNS3_RSS_KEY_SIZE;
2992 hw->max_tm_rate = HNS3_ETHER_MAX_RATE;
2993 hw->intr.int_ql_max = HNS3_INTR_QL_NONE;
2997 hns3_parse_dev_specifications(struct hns3_hw *hw, struct hns3_cmd_desc *desc)
2999 struct hns3_dev_specs_0_cmd *req0;
3001 req0 = (struct hns3_dev_specs_0_cmd *)desc[0].data;
3003 hw->max_non_tso_bd_num = req0->max_non_tso_bd_num;
3004 hw->rss_ind_tbl_size = rte_le_to_cpu_16(req0->rss_ind_tbl_size);
3005 hw->rss_key_size = rte_le_to_cpu_16(req0->rss_key_size);
3006 hw->max_tm_rate = rte_le_to_cpu_32(req0->max_tm_rate);
3007 hw->intr.int_ql_max = rte_le_to_cpu_16(req0->intr_ql_max);
3011 hns3_check_dev_specifications(struct hns3_hw *hw)
3013 if (hw->rss_ind_tbl_size == 0 ||
3014 hw->rss_ind_tbl_size > HNS3_RSS_IND_TBL_SIZE_MAX) {
3015 hns3_err(hw, "the size of hash lookup table configured (%u)"
3016 " exceeds the maximum(%u)", hw->rss_ind_tbl_size,
3017 HNS3_RSS_IND_TBL_SIZE_MAX);
3025 hns3_query_dev_specifications(struct hns3_hw *hw)
3027 struct hns3_cmd_desc desc[HNS3_QUERY_DEV_SPECS_BD_NUM];
3031 for (i = 0; i < HNS3_QUERY_DEV_SPECS_BD_NUM - 1; i++) {
3032 hns3_cmd_setup_basic_desc(&desc[i], HNS3_OPC_QUERY_DEV_SPECS,
3034 desc[i].flag |= rte_cpu_to_le_16(HNS3_CMD_FLAG_NEXT);
3036 hns3_cmd_setup_basic_desc(&desc[i], HNS3_OPC_QUERY_DEV_SPECS, true);
3038 ret = hns3_cmd_send(hw, desc, HNS3_QUERY_DEV_SPECS_BD_NUM);
3042 hns3_parse_dev_specifications(hw, desc);
3044 return hns3_check_dev_specifications(hw);
3048 hns3_get_capability(struct hns3_hw *hw)
3050 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
3051 struct rte_pci_device *pci_dev;
3052 struct hns3_pf *pf = &hns->pf;
3053 struct rte_eth_dev *eth_dev;
3058 eth_dev = &rte_eth_devices[hw->data->port_id];
3059 pci_dev = RTE_ETH_DEV_TO_PCI(eth_dev);
3060 device_id = pci_dev->id.device_id;
3062 if (device_id == HNS3_DEV_ID_25GE_RDMA ||
3063 device_id == HNS3_DEV_ID_50GE_RDMA ||
3064 device_id == HNS3_DEV_ID_100G_RDMA_MACSEC ||
3065 device_id == HNS3_DEV_ID_200G_RDMA)
3066 hns3_set_bit(hw->capability, HNS3_DEV_SUPPORT_DCB_B, 1);
3068 /* Get PCI revision id */
3069 ret = rte_pci_read_config(pci_dev, &revision, HNS3_PCI_REVISION_ID_LEN,
3070 HNS3_PCI_REVISION_ID);
3071 if (ret != HNS3_PCI_REVISION_ID_LEN) {
3072 PMD_INIT_LOG(ERR, "failed to read pci revision id, ret = %d",
3076 hw->revision = revision;
3078 if (revision < PCI_REVISION_ID_HIP09_A) {
3079 hns3_set_default_dev_specifications(hw);
3080 hw->intr.mapping_mode = HNS3_INTR_MAPPING_VEC_RSV_ONE;
3081 hw->intr.gl_unit = HNS3_INTR_COALESCE_GL_UINT_2US;
3082 hw->tso_mode = HNS3_TSO_SW_CAL_PSEUDO_H_CSUM;
3083 hw->vlan_mode = HNS3_SW_SHIFT_AND_DISCARD_MODE;
3084 hw->min_tx_pkt_len = HNS3_HIP08_MIN_TX_PKT_LEN;
3085 pf->tqp_config_mode = HNS3_FIXED_MAX_TQP_NUM_MODE;
3086 hw->rss_info.ipv6_sctp_offload_supported = false;
3090 ret = hns3_query_dev_specifications(hw);
3093 "failed to query dev specifications, ret = %d",
3098 hw->intr.mapping_mode = HNS3_INTR_MAPPING_VEC_ALL;
3099 hw->intr.gl_unit = HNS3_INTR_COALESCE_GL_UINT_1US;
3100 hw->tso_mode = HNS3_TSO_HW_CAL_PSEUDO_H_CSUM;
3101 hw->vlan_mode = HNS3_HW_SHIFT_AND_DISCARD_MODE;
3102 hw->min_tx_pkt_len = HNS3_HIP09_MIN_TX_PKT_LEN;
3103 pf->tqp_config_mode = HNS3_FLEX_MAX_TQP_NUM_MODE;
3104 hw->rss_info.ipv6_sctp_offload_supported = true;
3110 hns3_check_media_type(struct hns3_hw *hw, uint8_t media_type)
3114 switch (media_type) {
3115 case HNS3_MEDIA_TYPE_COPPER:
3116 if (!hns3_dev_copper_supported(hw)) {
3118 "Media type is copper, not supported.");
3124 case HNS3_MEDIA_TYPE_FIBER:
3127 case HNS3_MEDIA_TYPE_BACKPLANE:
3128 PMD_INIT_LOG(ERR, "Media type is Backplane, not supported.");
3132 PMD_INIT_LOG(ERR, "Unknown media type = %u!", media_type);
3141 hns3_get_board_configuration(struct hns3_hw *hw)
3143 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
3144 struct hns3_pf *pf = &hns->pf;
3145 struct hns3_cfg cfg;
3148 ret = hns3_get_board_cfg(hw, &cfg);
3150 PMD_INIT_LOG(ERR, "get board config failed %d", ret);
3154 ret = hns3_check_media_type(hw, cfg.media_type);
3158 hw->mac.media_type = cfg.media_type;
3159 hw->rss_size_max = cfg.rss_size_max;
3160 hw->rss_dis_flag = false;
3161 memcpy(hw->mac.mac_addr, cfg.mac_addr, RTE_ETHER_ADDR_LEN);
3162 hw->mac.phy_addr = cfg.phy_addr;
3163 hw->mac.default_addr_setted = false;
3164 hw->num_tx_desc = cfg.tqp_desc_num;
3165 hw->num_rx_desc = cfg.tqp_desc_num;
3166 hw->dcb_info.num_pg = 1;
3167 hw->dcb_info.hw_pfc_map = 0;
3169 ret = hns3_parse_speed(cfg.default_speed, &hw->mac.link_speed);
3171 PMD_INIT_LOG(ERR, "Get wrong speed %u, ret = %d",
3172 cfg.default_speed, ret);
3176 pf->tc_max = cfg.tc_num;
3177 if (pf->tc_max > HNS3_MAX_TC_NUM || pf->tc_max < 1) {
3178 PMD_INIT_LOG(WARNING,
3179 "Get TC num(%u) from flash, set TC num to 1",
3184 /* Dev does not support DCB */
3185 if (!hns3_dev_dcb_supported(hw)) {
3189 pf->pfc_max = pf->tc_max;
3191 hw->dcb_info.num_tc = 1;
3192 hw->alloc_rss_size = RTE_MIN(hw->rss_size_max,
3193 hw->tqps_num / hw->dcb_info.num_tc);
3194 hns3_set_bit(hw->hw_tc_map, 0, 1);
3195 pf->tx_sch_mode = HNS3_FLAG_TC_BASE_SCH_MODE;
3197 pf->wanted_umv_size = cfg.umv_space;
3203 hns3_get_configuration(struct hns3_hw *hw)
3207 ret = hns3_query_function_status(hw);
3209 PMD_INIT_LOG(ERR, "Failed to query function status: %d.", ret);
3213 /* Get device capability */
3214 ret = hns3_get_capability(hw);
3216 PMD_INIT_LOG(ERR, "failed to get device capability: %d.", ret);
3220 /* Get pf resource */
3221 ret = hns3_query_pf_resource(hw);
3223 PMD_INIT_LOG(ERR, "Failed to query pf resource: %d", ret);
3227 ret = hns3_get_board_configuration(hw);
3229 PMD_INIT_LOG(ERR, "failed to get board configuration: %d", ret);
3233 ret = hns3_query_dev_fec_info(hw);
3236 "failed to query FEC information, ret = %d", ret);
3242 hns3_map_tqps_to_func(struct hns3_hw *hw, uint16_t func_id, uint16_t tqp_pid,
3243 uint16_t tqp_vid, bool is_pf)
3245 struct hns3_tqp_map_cmd *req;
3246 struct hns3_cmd_desc desc;
3249 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_SET_TQP_MAP, false);
3251 req = (struct hns3_tqp_map_cmd *)desc.data;
3252 req->tqp_id = rte_cpu_to_le_16(tqp_pid);
3253 req->tqp_vf = func_id;
3254 req->tqp_flag = 1 << HNS3_TQP_MAP_EN_B;
3256 req->tqp_flag |= (1 << HNS3_TQP_MAP_TYPE_B);
3257 req->tqp_vid = rte_cpu_to_le_16(tqp_vid);
3259 ret = hns3_cmd_send(hw, &desc, 1);
3261 PMD_INIT_LOG(ERR, "TQP map failed %d", ret);
3267 hns3_map_tqp(struct hns3_hw *hw)
3273 * In current version, VF is not supported when PF is driven by DPDK
3274 * driver, so we assign total tqps_num tqps allocated to this port
3277 for (i = 0; i < hw->total_tqps_num; i++) {
3278 ret = hns3_map_tqps_to_func(hw, HNS3_PF_FUNC_ID, i, i, true);
3287 hns3_cfg_mac_speed_dup_hw(struct hns3_hw *hw, uint32_t speed, uint8_t duplex)
3289 struct hns3_config_mac_speed_dup_cmd *req;
3290 struct hns3_cmd_desc desc;
3293 req = (struct hns3_config_mac_speed_dup_cmd *)desc.data;
3295 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_CONFIG_SPEED_DUP, false);
3297 hns3_set_bit(req->speed_dup, HNS3_CFG_DUPLEX_B, !!duplex ? 1 : 0);
3300 case ETH_SPEED_NUM_10M:
3301 hns3_set_field(req->speed_dup, HNS3_CFG_SPEED_M,
3302 HNS3_CFG_SPEED_S, HNS3_CFG_SPEED_10M);
3304 case ETH_SPEED_NUM_100M:
3305 hns3_set_field(req->speed_dup, HNS3_CFG_SPEED_M,
3306 HNS3_CFG_SPEED_S, HNS3_CFG_SPEED_100M);
3308 case ETH_SPEED_NUM_1G:
3309 hns3_set_field(req->speed_dup, HNS3_CFG_SPEED_M,
3310 HNS3_CFG_SPEED_S, HNS3_CFG_SPEED_1G);
3312 case ETH_SPEED_NUM_10G:
3313 hns3_set_field(req->speed_dup, HNS3_CFG_SPEED_M,
3314 HNS3_CFG_SPEED_S, HNS3_CFG_SPEED_10G);
3316 case ETH_SPEED_NUM_25G:
3317 hns3_set_field(req->speed_dup, HNS3_CFG_SPEED_M,
3318 HNS3_CFG_SPEED_S, HNS3_CFG_SPEED_25G);
3320 case ETH_SPEED_NUM_40G:
3321 hns3_set_field(req->speed_dup, HNS3_CFG_SPEED_M,
3322 HNS3_CFG_SPEED_S, HNS3_CFG_SPEED_40G);
3324 case ETH_SPEED_NUM_50G:
3325 hns3_set_field(req->speed_dup, HNS3_CFG_SPEED_M,
3326 HNS3_CFG_SPEED_S, HNS3_CFG_SPEED_50G);
3328 case ETH_SPEED_NUM_100G:
3329 hns3_set_field(req->speed_dup, HNS3_CFG_SPEED_M,
3330 HNS3_CFG_SPEED_S, HNS3_CFG_SPEED_100G);
3332 case ETH_SPEED_NUM_200G:
3333 hns3_set_field(req->speed_dup, HNS3_CFG_SPEED_M,
3334 HNS3_CFG_SPEED_S, HNS3_CFG_SPEED_200G);
3337 PMD_INIT_LOG(ERR, "invalid speed (%u)", speed);
3341 hns3_set_bit(req->mac_change_fec_en, HNS3_CFG_MAC_SPEED_CHANGE_EN_B, 1);
3343 ret = hns3_cmd_send(hw, &desc, 1);
3345 PMD_INIT_LOG(ERR, "mac speed/duplex config cmd failed %d", ret);
3351 hns3_tx_buffer_calc(struct hns3_hw *hw, struct hns3_pkt_buf_alloc *buf_alloc)
3353 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
3354 struct hns3_pf *pf = &hns->pf;
3355 struct hns3_priv_buf *priv;
3356 uint32_t i, total_size;
3358 total_size = pf->pkt_buf_size;
3360 /* alloc tx buffer for all enabled tc */
3361 for (i = 0; i < HNS3_MAX_TC_NUM; i++) {
3362 priv = &buf_alloc->priv_buf[i];
3364 if (hw->hw_tc_map & BIT(i)) {
3365 if (total_size < pf->tx_buf_size)
3368 priv->tx_buf_size = pf->tx_buf_size;
3370 priv->tx_buf_size = 0;
3372 total_size -= priv->tx_buf_size;
3379 hns3_tx_buffer_alloc(struct hns3_hw *hw, struct hns3_pkt_buf_alloc *buf_alloc)
3381 /* TX buffer size is unit by 128 byte */
3382 #define HNS3_BUF_SIZE_UNIT_SHIFT 7
3383 #define HNS3_BUF_SIZE_UPDATE_EN_MSK BIT(15)
3384 struct hns3_tx_buff_alloc_cmd *req;
3385 struct hns3_cmd_desc desc;
3390 req = (struct hns3_tx_buff_alloc_cmd *)desc.data;
3392 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_TX_BUFF_ALLOC, 0);
3393 for (i = 0; i < HNS3_MAX_TC_NUM; i++) {
3394 buf_size = buf_alloc->priv_buf[i].tx_buf_size;
3396 buf_size = buf_size >> HNS3_BUF_SIZE_UNIT_SHIFT;
3397 req->tx_pkt_buff[i] = rte_cpu_to_le_16(buf_size |
3398 HNS3_BUF_SIZE_UPDATE_EN_MSK);
3401 ret = hns3_cmd_send(hw, &desc, 1);
3403 PMD_INIT_LOG(ERR, "tx buffer alloc cmd failed %d", ret);
3409 hns3_get_tc_num(struct hns3_hw *hw)
3414 for (i = 0; i < HNS3_MAX_TC_NUM; i++)
3415 if (hw->hw_tc_map & BIT(i))
3421 hns3_get_rx_priv_buff_alloced(struct hns3_pkt_buf_alloc *buf_alloc)
3423 struct hns3_priv_buf *priv;
3424 uint32_t rx_priv = 0;
3427 for (i = 0; i < HNS3_MAX_TC_NUM; i++) {
3428 priv = &buf_alloc->priv_buf[i];
3430 rx_priv += priv->buf_size;
3436 hns3_get_tx_buff_alloced(struct hns3_pkt_buf_alloc *buf_alloc)
3438 uint32_t total_tx_size = 0;
3441 for (i = 0; i < HNS3_MAX_TC_NUM; i++)
3442 total_tx_size += buf_alloc->priv_buf[i].tx_buf_size;
3444 return total_tx_size;
3447 /* Get the number of pfc enabled TCs, which have private buffer */
3449 hns3_get_pfc_priv_num(struct hns3_hw *hw, struct hns3_pkt_buf_alloc *buf_alloc)
3451 struct hns3_priv_buf *priv;
3455 for (i = 0; i < HNS3_MAX_TC_NUM; i++) {
3456 priv = &buf_alloc->priv_buf[i];
3457 if ((hw->dcb_info.hw_pfc_map & BIT(i)) && priv->enable)
3464 /* Get the number of pfc disabled TCs, which have private buffer */
3466 hns3_get_no_pfc_priv_num(struct hns3_hw *hw,
3467 struct hns3_pkt_buf_alloc *buf_alloc)
3469 struct hns3_priv_buf *priv;
3473 for (i = 0; i < HNS3_MAX_TC_NUM; i++) {
3474 priv = &buf_alloc->priv_buf[i];
3475 if (hw->hw_tc_map & BIT(i) &&
3476 !(hw->dcb_info.hw_pfc_map & BIT(i)) && priv->enable)
3484 hns3_is_rx_buf_ok(struct hns3_hw *hw, struct hns3_pkt_buf_alloc *buf_alloc,
3487 uint32_t shared_buf_min, shared_buf_tc, shared_std, hi_thrd, lo_thrd;
3488 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
3489 struct hns3_pf *pf = &hns->pf;
3490 uint32_t shared_buf, aligned_mps;
3495 tc_num = hns3_get_tc_num(hw);
3496 aligned_mps = roundup(pf->mps, HNS3_BUF_SIZE_UNIT);
3498 if (hns3_dev_dcb_supported(hw))
3499 shared_buf_min = HNS3_BUF_MUL_BY * aligned_mps +
3502 shared_buf_min = aligned_mps + HNS3_NON_DCB_ADDITIONAL_BUF
3505 shared_buf_tc = tc_num * aligned_mps + aligned_mps;
3506 shared_std = roundup(RTE_MAX(shared_buf_min, shared_buf_tc),
3507 HNS3_BUF_SIZE_UNIT);
3509 rx_priv = hns3_get_rx_priv_buff_alloced(buf_alloc);
3510 if (rx_all < rx_priv + shared_std)
3513 shared_buf = rounddown(rx_all - rx_priv, HNS3_BUF_SIZE_UNIT);
3514 buf_alloc->s_buf.buf_size = shared_buf;
3515 if (hns3_dev_dcb_supported(hw)) {
3516 buf_alloc->s_buf.self.high = shared_buf - pf->dv_buf_size;
3517 buf_alloc->s_buf.self.low = buf_alloc->s_buf.self.high
3518 - roundup(aligned_mps / HNS3_BUF_DIV_BY,
3519 HNS3_BUF_SIZE_UNIT);
3521 buf_alloc->s_buf.self.high =
3522 aligned_mps + HNS3_NON_DCB_ADDITIONAL_BUF;
3523 buf_alloc->s_buf.self.low = aligned_mps;
3526 if (hns3_dev_dcb_supported(hw)) {
3527 hi_thrd = shared_buf - pf->dv_buf_size;
3529 if (tc_num <= NEED_RESERVE_TC_NUM)
3530 hi_thrd = hi_thrd * BUF_RESERVE_PERCENT /
3534 hi_thrd = hi_thrd / tc_num;
3536 hi_thrd = RTE_MAX(hi_thrd, HNS3_BUF_MUL_BY * aligned_mps);
3537 hi_thrd = rounddown(hi_thrd, HNS3_BUF_SIZE_UNIT);
3538 lo_thrd = hi_thrd - aligned_mps / HNS3_BUF_DIV_BY;
3540 hi_thrd = aligned_mps + HNS3_NON_DCB_ADDITIONAL_BUF;
3541 lo_thrd = aligned_mps;
3544 for (i = 0; i < HNS3_MAX_TC_NUM; i++) {
3545 buf_alloc->s_buf.tc_thrd[i].low = lo_thrd;
3546 buf_alloc->s_buf.tc_thrd[i].high = hi_thrd;
3553 hns3_rx_buf_calc_all(struct hns3_hw *hw, bool max,
3554 struct hns3_pkt_buf_alloc *buf_alloc)
3556 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
3557 struct hns3_pf *pf = &hns->pf;
3558 struct hns3_priv_buf *priv;
3559 uint32_t aligned_mps;
3563 rx_all = pf->pkt_buf_size - hns3_get_tx_buff_alloced(buf_alloc);
3564 aligned_mps = roundup(pf->mps, HNS3_BUF_SIZE_UNIT);
3566 for (i = 0; i < HNS3_MAX_TC_NUM; i++) {
3567 priv = &buf_alloc->priv_buf[i];
3574 if (!(hw->hw_tc_map & BIT(i)))
3578 if (hw->dcb_info.hw_pfc_map & BIT(i)) {
3579 priv->wl.low = max ? aligned_mps : HNS3_BUF_SIZE_UNIT;
3580 priv->wl.high = roundup(priv->wl.low + aligned_mps,
3581 HNS3_BUF_SIZE_UNIT);
3584 priv->wl.high = max ? (aligned_mps * HNS3_BUF_MUL_BY) :
3588 priv->buf_size = priv->wl.high + pf->dv_buf_size;
3591 return hns3_is_rx_buf_ok(hw, buf_alloc, rx_all);
3595 hns3_drop_nopfc_buf_till_fit(struct hns3_hw *hw,
3596 struct hns3_pkt_buf_alloc *buf_alloc)
3598 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
3599 struct hns3_pf *pf = &hns->pf;
3600 struct hns3_priv_buf *priv;
3601 int no_pfc_priv_num;
3606 rx_all = pf->pkt_buf_size - hns3_get_tx_buff_alloced(buf_alloc);
3607 no_pfc_priv_num = hns3_get_no_pfc_priv_num(hw, buf_alloc);
3609 /* let the last to be cleared first */
3610 for (i = HNS3_MAX_TC_NUM - 1; i >= 0; i--) {
3611 priv = &buf_alloc->priv_buf[i];
3612 mask = BIT((uint8_t)i);
3614 if (hw->hw_tc_map & mask &&
3615 !(hw->dcb_info.hw_pfc_map & mask)) {
3616 /* Clear the no pfc TC private buffer */
3624 if (hns3_is_rx_buf_ok(hw, buf_alloc, rx_all) ||
3625 no_pfc_priv_num == 0)
3629 return hns3_is_rx_buf_ok(hw, buf_alloc, rx_all);
3633 hns3_drop_pfc_buf_till_fit(struct hns3_hw *hw,
3634 struct hns3_pkt_buf_alloc *buf_alloc)
3636 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
3637 struct hns3_pf *pf = &hns->pf;
3638 struct hns3_priv_buf *priv;
3644 rx_all = pf->pkt_buf_size - hns3_get_tx_buff_alloced(buf_alloc);
3645 pfc_priv_num = hns3_get_pfc_priv_num(hw, buf_alloc);
3647 /* let the last to be cleared first */
3648 for (i = HNS3_MAX_TC_NUM - 1; i >= 0; i--) {
3649 priv = &buf_alloc->priv_buf[i];
3650 mask = BIT((uint8_t)i);
3651 if (hw->hw_tc_map & mask && hw->dcb_info.hw_pfc_map & mask) {
3652 /* Reduce the number of pfc TC with private buffer */
3659 if (hns3_is_rx_buf_ok(hw, buf_alloc, rx_all) ||
3664 return hns3_is_rx_buf_ok(hw, buf_alloc, rx_all);
3668 hns3_only_alloc_priv_buff(struct hns3_hw *hw,
3669 struct hns3_pkt_buf_alloc *buf_alloc)
3671 #define COMPENSATE_BUFFER 0x3C00
3672 #define COMPENSATE_HALF_MPS_NUM 5
3673 #define PRIV_WL_GAP 0x1800
3674 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
3675 struct hns3_pf *pf = &hns->pf;
3676 uint32_t tc_num = hns3_get_tc_num(hw);
3677 uint32_t half_mps = pf->mps >> 1;
3678 struct hns3_priv_buf *priv;
3679 uint32_t min_rx_priv;
3683 rx_priv = pf->pkt_buf_size - hns3_get_tx_buff_alloced(buf_alloc);
3685 rx_priv = rx_priv / tc_num;
3687 if (tc_num <= NEED_RESERVE_TC_NUM)
3688 rx_priv = rx_priv * BUF_RESERVE_PERCENT / BUF_MAX_PERCENT;
3691 * Minimum value of private buffer in rx direction (min_rx_priv) is
3692 * equal to "DV + 2.5 * MPS + 15KB". Driver only allocates rx private
3693 * buffer if rx_priv is greater than min_rx_priv.
3695 min_rx_priv = pf->dv_buf_size + COMPENSATE_BUFFER +
3696 COMPENSATE_HALF_MPS_NUM * half_mps;
3697 min_rx_priv = roundup(min_rx_priv, HNS3_BUF_SIZE_UNIT);
3698 rx_priv = rounddown(rx_priv, HNS3_BUF_SIZE_UNIT);
3700 if (rx_priv < min_rx_priv)
3703 for (i = 0; i < HNS3_MAX_TC_NUM; i++) {
3704 priv = &buf_alloc->priv_buf[i];
3710 if (!(hw->hw_tc_map & BIT(i)))
3714 priv->buf_size = rx_priv;
3715 priv->wl.high = rx_priv - pf->dv_buf_size;
3716 priv->wl.low = priv->wl.high - PRIV_WL_GAP;
3719 buf_alloc->s_buf.buf_size = 0;
3725 * hns3_rx_buffer_calc: calculate the rx private buffer size for all TCs
3726 * @hw: pointer to struct hns3_hw
3727 * @buf_alloc: pointer to buffer calculation data
3728 * @return: 0: calculate sucessful, negative: fail
3731 hns3_rx_buffer_calc(struct hns3_hw *hw, struct hns3_pkt_buf_alloc *buf_alloc)
3733 /* When DCB is not supported, rx private buffer is not allocated. */
3734 if (!hns3_dev_dcb_supported(hw)) {
3735 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
3736 struct hns3_pf *pf = &hns->pf;
3737 uint32_t rx_all = pf->pkt_buf_size;
3739 rx_all -= hns3_get_tx_buff_alloced(buf_alloc);
3740 if (!hns3_is_rx_buf_ok(hw, buf_alloc, rx_all))
3747 * Try to allocate privated packet buffer for all TCs without share
3750 if (hns3_only_alloc_priv_buff(hw, buf_alloc))
3754 * Try to allocate privated packet buffer for all TCs with share
3757 if (hns3_rx_buf_calc_all(hw, true, buf_alloc))
3761 * For different application scenes, the enabled port number, TC number
3762 * and no_drop TC number are different. In order to obtain the better
3763 * performance, software could allocate the buffer size and configure
3764 * the waterline by tring to decrease the private buffer size according
3765 * to the order, namely, waterline of valided tc, pfc disabled tc, pfc
3768 if (hns3_rx_buf_calc_all(hw, false, buf_alloc))
3771 if (hns3_drop_nopfc_buf_till_fit(hw, buf_alloc))
3774 if (hns3_drop_pfc_buf_till_fit(hw, buf_alloc))
3781 hns3_rx_priv_buf_alloc(struct hns3_hw *hw, struct hns3_pkt_buf_alloc *buf_alloc)
3783 struct hns3_rx_priv_buff_cmd *req;
3784 struct hns3_cmd_desc desc;
3789 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_RX_PRIV_BUFF_ALLOC, false);
3790 req = (struct hns3_rx_priv_buff_cmd *)desc.data;
3792 /* Alloc private buffer TCs */
3793 for (i = 0; i < HNS3_MAX_TC_NUM; i++) {
3794 struct hns3_priv_buf *priv = &buf_alloc->priv_buf[i];
3797 rte_cpu_to_le_16(priv->buf_size >> HNS3_BUF_UNIT_S);
3798 req->buf_num[i] |= rte_cpu_to_le_16(1 << HNS3_TC0_PRI_BUF_EN_B);
3801 buf_size = buf_alloc->s_buf.buf_size;
3802 req->shared_buf = rte_cpu_to_le_16((buf_size >> HNS3_BUF_UNIT_S) |
3803 (1 << HNS3_TC0_PRI_BUF_EN_B));
3805 ret = hns3_cmd_send(hw, &desc, 1);
3807 PMD_INIT_LOG(ERR, "rx private buffer alloc cmd failed %d", ret);
3813 hns3_rx_priv_wl_config(struct hns3_hw *hw, struct hns3_pkt_buf_alloc *buf_alloc)
3815 #define HNS3_RX_PRIV_WL_ALLOC_DESC_NUM 2
3816 struct hns3_rx_priv_wl_buf *req;
3817 struct hns3_priv_buf *priv;
3818 struct hns3_cmd_desc desc[HNS3_RX_PRIV_WL_ALLOC_DESC_NUM];
3822 for (i = 0; i < HNS3_RX_PRIV_WL_ALLOC_DESC_NUM; i++) {
3823 hns3_cmd_setup_basic_desc(&desc[i], HNS3_OPC_RX_PRIV_WL_ALLOC,
3825 req = (struct hns3_rx_priv_wl_buf *)desc[i].data;
3827 /* The first descriptor set the NEXT bit to 1 */
3829 desc[i].flag |= rte_cpu_to_le_16(HNS3_CMD_FLAG_NEXT);
3831 desc[i].flag &= ~rte_cpu_to_le_16(HNS3_CMD_FLAG_NEXT);
3833 for (j = 0; j < HNS3_TC_NUM_ONE_DESC; j++) {
3834 uint32_t idx = i * HNS3_TC_NUM_ONE_DESC + j;
3836 priv = &buf_alloc->priv_buf[idx];
3837 req->tc_wl[j].high = rte_cpu_to_le_16(priv->wl.high >>
3839 req->tc_wl[j].high |=
3840 rte_cpu_to_le_16(BIT(HNS3_RX_PRIV_EN_B));
3841 req->tc_wl[j].low = rte_cpu_to_le_16(priv->wl.low >>
3843 req->tc_wl[j].low |=
3844 rte_cpu_to_le_16(BIT(HNS3_RX_PRIV_EN_B));
3848 /* Send 2 descriptor at one time */
3849 ret = hns3_cmd_send(hw, desc, HNS3_RX_PRIV_WL_ALLOC_DESC_NUM);
3851 PMD_INIT_LOG(ERR, "rx private waterline config cmd failed %d",
3857 hns3_common_thrd_config(struct hns3_hw *hw,
3858 struct hns3_pkt_buf_alloc *buf_alloc)
3860 #define HNS3_RX_COM_THRD_ALLOC_DESC_NUM 2
3861 struct hns3_shared_buf *s_buf = &buf_alloc->s_buf;
3862 struct hns3_rx_com_thrd *req;
3863 struct hns3_cmd_desc desc[HNS3_RX_COM_THRD_ALLOC_DESC_NUM];
3864 struct hns3_tc_thrd *tc;
3869 for (i = 0; i < HNS3_RX_COM_THRD_ALLOC_DESC_NUM; i++) {
3870 hns3_cmd_setup_basic_desc(&desc[i], HNS3_OPC_RX_COM_THRD_ALLOC,
3872 req = (struct hns3_rx_com_thrd *)&desc[i].data;
3874 /* The first descriptor set the NEXT bit to 1 */
3876 desc[i].flag |= rte_cpu_to_le_16(HNS3_CMD_FLAG_NEXT);
3878 desc[i].flag &= ~rte_cpu_to_le_16(HNS3_CMD_FLAG_NEXT);
3880 for (j = 0; j < HNS3_TC_NUM_ONE_DESC; j++) {
3881 tc_idx = i * HNS3_TC_NUM_ONE_DESC + j;
3882 tc = &s_buf->tc_thrd[tc_idx];
3884 req->com_thrd[j].high =
3885 rte_cpu_to_le_16(tc->high >> HNS3_BUF_UNIT_S);
3886 req->com_thrd[j].high |=
3887 rte_cpu_to_le_16(BIT(HNS3_RX_PRIV_EN_B));
3888 req->com_thrd[j].low =
3889 rte_cpu_to_le_16(tc->low >> HNS3_BUF_UNIT_S);
3890 req->com_thrd[j].low |=
3891 rte_cpu_to_le_16(BIT(HNS3_RX_PRIV_EN_B));
3895 /* Send 2 descriptors at one time */
3896 ret = hns3_cmd_send(hw, desc, HNS3_RX_COM_THRD_ALLOC_DESC_NUM);
3898 PMD_INIT_LOG(ERR, "common threshold config cmd failed %d", ret);
3904 hns3_common_wl_config(struct hns3_hw *hw, struct hns3_pkt_buf_alloc *buf_alloc)
3906 struct hns3_shared_buf *buf = &buf_alloc->s_buf;
3907 struct hns3_rx_com_wl *req;
3908 struct hns3_cmd_desc desc;
3911 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_RX_COM_WL_ALLOC, false);
3913 req = (struct hns3_rx_com_wl *)desc.data;
3914 req->com_wl.high = rte_cpu_to_le_16(buf->self.high >> HNS3_BUF_UNIT_S);
3915 req->com_wl.high |= rte_cpu_to_le_16(BIT(HNS3_RX_PRIV_EN_B));
3917 req->com_wl.low = rte_cpu_to_le_16(buf->self.low >> HNS3_BUF_UNIT_S);
3918 req->com_wl.low |= rte_cpu_to_le_16(BIT(HNS3_RX_PRIV_EN_B));
3920 ret = hns3_cmd_send(hw, &desc, 1);
3922 PMD_INIT_LOG(ERR, "common waterline config cmd failed %d", ret);
3928 hns3_buffer_alloc(struct hns3_hw *hw)
3930 struct hns3_pkt_buf_alloc pkt_buf;
3933 memset(&pkt_buf, 0, sizeof(pkt_buf));
3934 ret = hns3_tx_buffer_calc(hw, &pkt_buf);
3937 "could not calc tx buffer size for all TCs %d",
3942 ret = hns3_tx_buffer_alloc(hw, &pkt_buf);
3944 PMD_INIT_LOG(ERR, "could not alloc tx buffers %d", ret);
3948 ret = hns3_rx_buffer_calc(hw, &pkt_buf);
3951 "could not calc rx priv buffer size for all TCs %d",
3956 ret = hns3_rx_priv_buf_alloc(hw, &pkt_buf);
3958 PMD_INIT_LOG(ERR, "could not alloc rx priv buffer %d", ret);
3962 if (hns3_dev_dcb_supported(hw)) {
3963 ret = hns3_rx_priv_wl_config(hw, &pkt_buf);
3966 "could not configure rx private waterline %d",
3971 ret = hns3_common_thrd_config(hw, &pkt_buf);
3974 "could not configure common threshold %d",
3980 ret = hns3_common_wl_config(hw, &pkt_buf);
3982 PMD_INIT_LOG(ERR, "could not configure common waterline %d",
3989 hns3_firmware_compat_config(struct hns3_hw *hw, bool is_init)
3991 struct hns3_firmware_compat_cmd *req;
3992 struct hns3_cmd_desc desc;
3993 uint32_t compat = 0;
3995 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_FIRMWARE_COMPAT_CFG, false);
3996 req = (struct hns3_firmware_compat_cmd *)desc.data;
3999 hns3_set_bit(compat, HNS3_LINK_EVENT_REPORT_EN_B, 1);
4000 hns3_set_bit(compat, HNS3_NCSI_ERROR_REPORT_EN_B, 0);
4001 if (hw->mac.media_type == HNS3_MEDIA_TYPE_COPPER)
4002 hns3_set_bit(compat, HNS3_FIRMWARE_PHY_DRIVER_EN_B, 1);
4005 req->compat = rte_cpu_to_le_32(compat);
4007 return hns3_cmd_send(hw, &desc, 1);
4011 hns3_mac_init(struct hns3_hw *hw)
4013 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
4014 struct hns3_mac *mac = &hw->mac;
4015 struct hns3_pf *pf = &hns->pf;
4018 pf->support_sfp_query = true;
4019 mac->link_duplex = ETH_LINK_FULL_DUPLEX;
4020 ret = hns3_cfg_mac_speed_dup_hw(hw, mac->link_speed, mac->link_duplex);
4022 PMD_INIT_LOG(ERR, "Config mac speed dup fail ret = %d", ret);
4026 mac->link_status = ETH_LINK_DOWN;
4028 return hns3_config_mtu(hw, pf->mps);
4032 hns3_get_mac_ethertype_cmd_status(uint16_t cmdq_resp, uint8_t resp_code)
4034 #define HNS3_ETHERTYPE_SUCCESS_ADD 0
4035 #define HNS3_ETHERTYPE_ALREADY_ADD 1
4036 #define HNS3_ETHERTYPE_MGR_TBL_OVERFLOW 2
4037 #define HNS3_ETHERTYPE_KEY_CONFLICT 3
4042 "cmdq execute failed for get_mac_ethertype_cmd_status, status=%u.\n",
4047 switch (resp_code) {
4048 case HNS3_ETHERTYPE_SUCCESS_ADD:
4049 case HNS3_ETHERTYPE_ALREADY_ADD:
4052 case HNS3_ETHERTYPE_MGR_TBL_OVERFLOW:
4054 "add mac ethertype failed for manager table overflow.");
4055 return_status = -EIO;
4057 case HNS3_ETHERTYPE_KEY_CONFLICT:
4058 PMD_INIT_LOG(ERR, "add mac ethertype failed for key conflict.");
4059 return_status = -EIO;
4063 "add mac ethertype failed for undefined, code=%u.",
4065 return_status = -EIO;
4069 return return_status;
4073 hns3_add_mgr_tbl(struct hns3_hw *hw,
4074 const struct hns3_mac_mgr_tbl_entry_cmd *req)
4076 struct hns3_cmd_desc desc;
4081 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_MAC_ETHTYPE_ADD, false);
4082 memcpy(desc.data, req, sizeof(struct hns3_mac_mgr_tbl_entry_cmd));
4084 ret = hns3_cmd_send(hw, &desc, 1);
4087 "add mac ethertype failed for cmd_send, ret =%d.",
4092 resp_code = (rte_le_to_cpu_32(desc.data[0]) >> 8) & 0xff;
4093 retval = rte_le_to_cpu_16(desc.retval);
4095 return hns3_get_mac_ethertype_cmd_status(retval, resp_code);
4099 hns3_prepare_mgr_tbl(struct hns3_mac_mgr_tbl_entry_cmd *mgr_table,
4100 int *table_item_num)
4102 struct hns3_mac_mgr_tbl_entry_cmd *tbl;
4105 * In current version, we add one item in management table as below:
4106 * 0x0180C200000E -- LLDP MC address
4109 tbl->flags = HNS3_MAC_MGR_MASK_VLAN_B;
4110 tbl->ethter_type = rte_cpu_to_le_16(HNS3_MAC_ETHERTYPE_LLDP);
4111 tbl->mac_addr_hi32 = rte_cpu_to_le_32(htonl(0x0180C200));
4112 tbl->mac_addr_lo16 = rte_cpu_to_le_16(htons(0x000E));
4113 tbl->i_port_bitmap = 0x1;
4114 *table_item_num = 1;
4118 hns3_init_mgr_tbl(struct hns3_hw *hw)
4120 #define HNS_MAC_MGR_TBL_MAX_SIZE 16
4121 struct hns3_mac_mgr_tbl_entry_cmd mgr_table[HNS_MAC_MGR_TBL_MAX_SIZE];
4126 memset(mgr_table, 0, sizeof(mgr_table));
4127 hns3_prepare_mgr_tbl(mgr_table, &table_item_num);
4128 for (i = 0; i < table_item_num; i++) {
4129 ret = hns3_add_mgr_tbl(hw, &mgr_table[i]);
4131 PMD_INIT_LOG(ERR, "add mac ethertype failed, ret =%d",
4141 hns3_promisc_param_init(struct hns3_promisc_param *param, bool en_uc,
4142 bool en_mc, bool en_bc, int vport_id)
4147 memset(param, 0, sizeof(struct hns3_promisc_param));
4149 param->enable = HNS3_PROMISC_EN_UC;
4151 param->enable |= HNS3_PROMISC_EN_MC;
4153 param->enable |= HNS3_PROMISC_EN_BC;
4154 param->vf_id = vport_id;
4158 hns3_cmd_set_promisc_mode(struct hns3_hw *hw, struct hns3_promisc_param *param)
4160 struct hns3_promisc_cfg_cmd *req;
4161 struct hns3_cmd_desc desc;
4164 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_CFG_PROMISC_MODE, false);
4166 req = (struct hns3_promisc_cfg_cmd *)desc.data;
4167 req->vf_id = param->vf_id;
4168 req->flag = (param->enable << HNS3_PROMISC_EN_B) |
4169 HNS3_PROMISC_TX_EN_B | HNS3_PROMISC_RX_EN_B;
4171 ret = hns3_cmd_send(hw, &desc, 1);
4173 PMD_INIT_LOG(ERR, "Set promisc mode fail, ret = %d", ret);
4179 hns3_set_promisc_mode(struct hns3_hw *hw, bool en_uc_pmc, bool en_mc_pmc)
4181 struct hns3_promisc_param param;
4182 bool en_bc_pmc = true;
4186 * In current version VF is not supported when PF is driven by DPDK
4187 * driver, just need to configure parameters for PF vport.
4189 vf_id = HNS3_PF_FUNC_ID;
4191 hns3_promisc_param_init(¶m, en_uc_pmc, en_mc_pmc, en_bc_pmc, vf_id);
4192 return hns3_cmd_set_promisc_mode(hw, ¶m);
4196 hns3_promisc_init(struct hns3_hw *hw)
4198 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
4199 struct hns3_pf *pf = &hns->pf;
4200 struct hns3_promisc_param param;
4204 ret = hns3_set_promisc_mode(hw, false, false);
4206 PMD_INIT_LOG(ERR, "failed to set promisc mode, ret = %d", ret);
4211 * In current version VFs are not supported when PF is driven by DPDK
4212 * driver. After PF has been taken over by DPDK, the original VF will
4213 * be invalid. So, there is a possibility of entry residues. It should
4214 * clear VFs's promisc mode to avoid unnecessary bandwidth usage
4217 for (func_id = HNS3_1ST_VF_FUNC_ID; func_id < pf->func_num; func_id++) {
4218 hns3_promisc_param_init(¶m, false, false, false, func_id);
4219 ret = hns3_cmd_set_promisc_mode(hw, ¶m);
4221 PMD_INIT_LOG(ERR, "failed to clear vf:%u promisc mode,"
4222 " ret = %d", func_id, ret);
4231 hns3_promisc_uninit(struct hns3_hw *hw)
4233 struct hns3_promisc_param param;
4237 func_id = HNS3_PF_FUNC_ID;
4240 * In current version VFs are not supported when PF is driven by
4241 * DPDK driver, and VFs' promisc mode status has been cleared during
4242 * init and their status will not change. So just clear PF's promisc
4243 * mode status during uninit.
4245 hns3_promisc_param_init(¶m, false, false, false, func_id);
4246 ret = hns3_cmd_set_promisc_mode(hw, ¶m);
4248 PMD_INIT_LOG(ERR, "failed to clear promisc status during"
4249 " uninit, ret = %d", ret);
4253 hns3_dev_promiscuous_enable(struct rte_eth_dev *dev)
4255 bool allmulti = dev->data->all_multicast ? true : false;
4256 struct hns3_adapter *hns = dev->data->dev_private;
4257 struct hns3_hw *hw = &hns->hw;
4262 rte_spinlock_lock(&hw->lock);
4263 ret = hns3_set_promisc_mode(hw, true, true);
4265 rte_spinlock_unlock(&hw->lock);
4266 hns3_err(hw, "failed to enable promiscuous mode, ret = %d",
4272 * When promiscuous mode was enabled, disable the vlan filter to let
4273 * all packets coming in in the receiving direction.
4275 offloads = dev->data->dev_conf.rxmode.offloads;
4276 if (offloads & DEV_RX_OFFLOAD_VLAN_FILTER) {
4277 ret = hns3_enable_vlan_filter(hns, false);
4279 hns3_err(hw, "failed to enable promiscuous mode due to "
4280 "failure to disable vlan filter, ret = %d",
4282 err = hns3_set_promisc_mode(hw, false, allmulti);
4284 hns3_err(hw, "failed to restore promiscuous "
4285 "status after disable vlan filter "
4286 "failed during enabling promiscuous "
4287 "mode, ret = %d", ret);
4291 rte_spinlock_unlock(&hw->lock);
4297 hns3_dev_promiscuous_disable(struct rte_eth_dev *dev)
4299 bool allmulti = dev->data->all_multicast ? true : false;
4300 struct hns3_adapter *hns = dev->data->dev_private;
4301 struct hns3_hw *hw = &hns->hw;
4306 /* If now in all_multicast mode, must remain in all_multicast mode. */
4307 rte_spinlock_lock(&hw->lock);
4308 ret = hns3_set_promisc_mode(hw, false, allmulti);
4310 rte_spinlock_unlock(&hw->lock);
4311 hns3_err(hw, "failed to disable promiscuous mode, ret = %d",
4315 /* when promiscuous mode was disabled, restore the vlan filter status */
4316 offloads = dev->data->dev_conf.rxmode.offloads;
4317 if (offloads & DEV_RX_OFFLOAD_VLAN_FILTER) {
4318 ret = hns3_enable_vlan_filter(hns, true);
4320 hns3_err(hw, "failed to disable promiscuous mode due to"
4321 " failure to restore vlan filter, ret = %d",
4323 err = hns3_set_promisc_mode(hw, true, true);
4325 hns3_err(hw, "failed to restore promiscuous "
4326 "status after enabling vlan filter "
4327 "failed during disabling promiscuous "
4328 "mode, ret = %d", ret);
4331 rte_spinlock_unlock(&hw->lock);
4337 hns3_dev_allmulticast_enable(struct rte_eth_dev *dev)
4339 struct hns3_adapter *hns = dev->data->dev_private;
4340 struct hns3_hw *hw = &hns->hw;
4343 if (dev->data->promiscuous)
4346 rte_spinlock_lock(&hw->lock);
4347 ret = hns3_set_promisc_mode(hw, false, true);
4348 rte_spinlock_unlock(&hw->lock);
4350 hns3_err(hw, "failed to enable allmulticast mode, ret = %d",
4357 hns3_dev_allmulticast_disable(struct rte_eth_dev *dev)
4359 struct hns3_adapter *hns = dev->data->dev_private;
4360 struct hns3_hw *hw = &hns->hw;
4363 /* If now in promiscuous mode, must remain in all_multicast mode. */
4364 if (dev->data->promiscuous)
4367 rte_spinlock_lock(&hw->lock);
4368 ret = hns3_set_promisc_mode(hw, false, false);
4369 rte_spinlock_unlock(&hw->lock);
4371 hns3_err(hw, "failed to disable allmulticast mode, ret = %d",
4378 hns3_dev_promisc_restore(struct hns3_adapter *hns)
4380 struct hns3_hw *hw = &hns->hw;
4381 bool allmulti = hw->data->all_multicast ? true : false;
4384 if (hw->data->promiscuous) {
4385 ret = hns3_set_promisc_mode(hw, true, true);
4387 hns3_err(hw, "failed to restore promiscuous mode, "
4392 ret = hns3_set_promisc_mode(hw, false, allmulti);
4394 hns3_err(hw, "failed to restore allmulticast mode, ret = %d",
4400 hns3_get_sfp_speed(struct hns3_hw *hw, uint32_t *speed)
4402 struct hns3_sfp_speed_cmd *resp;
4403 struct hns3_cmd_desc desc;
4406 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_SFP_GET_SPEED, true);
4407 resp = (struct hns3_sfp_speed_cmd *)desc.data;
4408 ret = hns3_cmd_send(hw, &desc, 1);
4409 if (ret == -EOPNOTSUPP) {
4410 hns3_err(hw, "IMP do not support get SFP speed %d", ret);
4413 hns3_err(hw, "get sfp speed failed %d", ret);
4417 *speed = resp->sfp_speed;
4423 hns3_check_speed_dup(uint8_t duplex, uint32_t speed)
4425 if (!(speed == ETH_SPEED_NUM_10M || speed == ETH_SPEED_NUM_100M))
4426 duplex = ETH_LINK_FULL_DUPLEX;
4432 hns3_cfg_mac_speed_dup(struct hns3_hw *hw, uint32_t speed, uint8_t duplex)
4434 struct hns3_mac *mac = &hw->mac;
4437 duplex = hns3_check_speed_dup(duplex, speed);
4438 if (mac->link_speed == speed && mac->link_duplex == duplex)
4441 ret = hns3_cfg_mac_speed_dup_hw(hw, speed, duplex);
4445 ret = hns3_port_shaper_update(hw, speed);
4449 mac->link_speed = speed;
4450 mac->link_duplex = duplex;
4456 hns3_update_fiber_link_info(struct hns3_hw *hw)
4458 struct hns3_pf *pf = HNS3_DEV_HW_TO_PF(hw);
4462 /* If IMP do not support get SFP/qSFP speed, return directly */
4463 if (!pf->support_sfp_query)
4466 ret = hns3_get_sfp_speed(hw, &speed);
4467 if (ret == -EOPNOTSUPP) {
4468 pf->support_sfp_query = false;
4473 if (speed == ETH_SPEED_NUM_NONE)
4474 return 0; /* do nothing if no SFP */
4476 /* Config full duplex for SFP */
4477 return hns3_cfg_mac_speed_dup(hw, speed, ETH_LINK_FULL_DUPLEX);
4481 hns3_parse_phy_params(struct hns3_cmd_desc *desc, struct hns3_mac *mac)
4483 struct hns3_phy_params_bd0_cmd *req;
4485 req = (struct hns3_phy_params_bd0_cmd *)desc[0].data;
4486 mac->link_speed = rte_le_to_cpu_32(req->speed);
4487 mac->link_duplex = hns3_get_bit(req->duplex,
4488 HNS3_PHY_DUPLEX_CFG_B);
4489 mac->link_autoneg = hns3_get_bit(req->autoneg,
4490 HNS3_PHY_AUTONEG_CFG_B);
4491 mac->supported_capa = rte_le_to_cpu_32(req->supported);
4492 mac->advertising = rte_le_to_cpu_32(req->advertising);
4493 mac->lp_advertising = rte_le_to_cpu_32(req->lp_advertising);
4494 mac->support_autoneg = !!(mac->supported_capa &
4495 HNS3_PHY_LINK_MODE_AUTONEG_BIT);
4499 hns3_get_phy_params(struct hns3_hw *hw, struct hns3_mac *mac)
4501 struct hns3_cmd_desc desc[HNS3_PHY_PARAM_CFG_BD_NUM];
4505 for (i = 0; i < HNS3_PHY_PARAM_CFG_BD_NUM - 1; i++) {
4506 hns3_cmd_setup_basic_desc(&desc[i], HNS3_OPC_PHY_PARAM_CFG,
4508 desc[i].flag |= rte_cpu_to_le_16(HNS3_CMD_FLAG_NEXT);
4510 hns3_cmd_setup_basic_desc(&desc[i], HNS3_OPC_PHY_PARAM_CFG, true);
4512 ret = hns3_cmd_send(hw, desc, HNS3_PHY_PARAM_CFG_BD_NUM);
4514 hns3_err(hw, "get phy parameters failed, ret = %d.", ret);
4518 hns3_parse_phy_params(desc, mac);
4524 hns3_update_phy_link_info(struct hns3_hw *hw)
4526 struct hns3_mac *mac = &hw->mac;
4527 struct hns3_mac mac_info;
4530 memset(&mac_info, 0, sizeof(struct hns3_mac));
4531 ret = hns3_get_phy_params(hw, &mac_info);
4535 if (mac_info.link_speed != mac->link_speed) {
4536 ret = hns3_port_shaper_update(hw, mac_info.link_speed);
4541 mac->link_speed = mac_info.link_speed;
4542 mac->link_duplex = mac_info.link_duplex;
4543 mac->link_autoneg = mac_info.link_autoneg;
4544 mac->supported_capa = mac_info.supported_capa;
4545 mac->advertising = mac_info.advertising;
4546 mac->lp_advertising = mac_info.lp_advertising;
4547 mac->support_autoneg = mac_info.support_autoneg;
4553 hns3_update_link_info(struct rte_eth_dev *eth_dev)
4555 struct hns3_adapter *hns = eth_dev->data->dev_private;
4556 struct hns3_hw *hw = &hns->hw;
4559 if (hw->mac.media_type == HNS3_MEDIA_TYPE_COPPER)
4560 ret = hns3_update_phy_link_info(hw);
4561 else if (hw->mac.media_type == HNS3_MEDIA_TYPE_FIBER)
4562 ret = hns3_update_fiber_link_info(hw);
4568 hns3_cfg_mac_mode(struct hns3_hw *hw, bool enable)
4570 struct hns3_config_mac_mode_cmd *req;
4571 struct hns3_cmd_desc desc;
4572 uint32_t loop_en = 0;
4576 req = (struct hns3_config_mac_mode_cmd *)desc.data;
4578 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_CONFIG_MAC_MODE, false);
4581 hns3_set_bit(loop_en, HNS3_MAC_TX_EN_B, val);
4582 hns3_set_bit(loop_en, HNS3_MAC_RX_EN_B, val);
4583 hns3_set_bit(loop_en, HNS3_MAC_PAD_TX_B, val);
4584 hns3_set_bit(loop_en, HNS3_MAC_PAD_RX_B, val);
4585 hns3_set_bit(loop_en, HNS3_MAC_1588_TX_B, 0);
4586 hns3_set_bit(loop_en, HNS3_MAC_1588_RX_B, 0);
4587 hns3_set_bit(loop_en, HNS3_MAC_APP_LP_B, 0);
4588 hns3_set_bit(loop_en, HNS3_MAC_LINE_LP_B, 0);
4589 hns3_set_bit(loop_en, HNS3_MAC_FCS_TX_B, val);
4590 hns3_set_bit(loop_en, HNS3_MAC_RX_FCS_B, val);
4593 * If DEV_RX_OFFLOAD_KEEP_CRC offload is set, MAC will not strip CRC
4594 * when receiving frames. Otherwise, CRC will be stripped.
4596 if (hw->data->dev_conf.rxmode.offloads & DEV_RX_OFFLOAD_KEEP_CRC)
4597 hns3_set_bit(loop_en, HNS3_MAC_RX_FCS_STRIP_B, 0);
4599 hns3_set_bit(loop_en, HNS3_MAC_RX_FCS_STRIP_B, val);
4600 hns3_set_bit(loop_en, HNS3_MAC_TX_OVERSIZE_TRUNCATE_B, val);
4601 hns3_set_bit(loop_en, HNS3_MAC_RX_OVERSIZE_TRUNCATE_B, val);
4602 hns3_set_bit(loop_en, HNS3_MAC_TX_UNDER_MIN_ERR_B, val);
4603 req->txrx_pad_fcs_loop_en = rte_cpu_to_le_32(loop_en);
4605 ret = hns3_cmd_send(hw, &desc, 1);
4607 PMD_INIT_LOG(ERR, "mac enable fail, ret =%d.", ret);
4613 hns3_get_mac_link_status(struct hns3_hw *hw)
4615 struct hns3_link_status_cmd *req;
4616 struct hns3_cmd_desc desc;
4620 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_QUERY_LINK_STATUS, true);
4621 ret = hns3_cmd_send(hw, &desc, 1);
4623 hns3_err(hw, "get link status cmd failed %d", ret);
4624 return ETH_LINK_DOWN;
4627 req = (struct hns3_link_status_cmd *)desc.data;
4628 link_status = req->status & HNS3_LINK_STATUS_UP_M;
4630 return !!link_status;
4634 hns3_update_link_status(struct hns3_hw *hw)
4638 state = hns3_get_mac_link_status(hw);
4639 if (state != hw->mac.link_status) {
4640 hw->mac.link_status = state;
4641 hns3_warn(hw, "Link status change to %s!", state ? "up" : "down");
4649 * Current, the PF driver get link status by two ways:
4650 * 1) Periodic polling in the intr thread context, driver call
4651 * hns3_update_link_status to update link status.
4652 * 2) Firmware report async interrupt, driver process the event in the intr
4653 * thread context, and call hns3_update_link_status to update link status.
4655 * If detect link status changed, driver need report LSE. One method is add the
4656 * report LSE logic in hns3_update_link_status.
4658 * But the PF driver ops(link_update) also call hns3_update_link_status to
4659 * update link status.
4660 * If we report LSE in hns3_update_link_status, it may lead to deadlock in the
4661 * bonding application.
4663 * So add the one new API which used only in intr thread context.
4666 hns3_update_link_status_and_event(struct hns3_hw *hw)
4668 struct rte_eth_dev *dev = &rte_eth_devices[hw->data->port_id];
4669 bool changed = hns3_update_link_status(hw);
4671 rte_eth_dev_callback_process(dev, RTE_ETH_EVENT_INTR_LSC, NULL);
4675 hns3_service_handler(void *param)
4677 struct rte_eth_dev *eth_dev = (struct rte_eth_dev *)param;
4678 struct hns3_adapter *hns = eth_dev->data->dev_private;
4679 struct hns3_hw *hw = &hns->hw;
4681 if (!hns3_is_reset_pending(hns)) {
4682 hns3_update_link_status_and_event(hw);
4683 hns3_update_link_info(eth_dev);
4685 hns3_warn(hw, "Cancel the query when reset is pending");
4688 rte_eal_alarm_set(HNS3_SERVICE_INTERVAL, hns3_service_handler, eth_dev);
4692 hns3_init_hardware(struct hns3_adapter *hns)
4694 struct hns3_hw *hw = &hns->hw;
4697 ret = hns3_map_tqp(hw);
4699 PMD_INIT_LOG(ERR, "Failed to map tqp: %d", ret);
4703 ret = hns3_init_umv_space(hw);
4705 PMD_INIT_LOG(ERR, "Failed to init umv space: %d", ret);
4709 ret = hns3_mac_init(hw);
4711 PMD_INIT_LOG(ERR, "Failed to init MAC: %d", ret);
4715 ret = hns3_init_mgr_tbl(hw);
4717 PMD_INIT_LOG(ERR, "Failed to init manager table: %d", ret);
4721 ret = hns3_promisc_init(hw);
4723 PMD_INIT_LOG(ERR, "Failed to init promisc: %d",
4728 ret = hns3_init_vlan_config(hns);
4730 PMD_INIT_LOG(ERR, "Failed to init vlan: %d", ret);
4734 ret = hns3_dcb_init(hw);
4736 PMD_INIT_LOG(ERR, "Failed to init dcb: %d", ret);
4740 ret = hns3_init_fd_config(hns);
4742 PMD_INIT_LOG(ERR, "Failed to init flow director: %d", ret);
4746 ret = hns3_config_tso(hw, HNS3_TSO_MSS_MIN, HNS3_TSO_MSS_MAX);
4748 PMD_INIT_LOG(ERR, "Failed to config tso: %d", ret);
4752 ret = hns3_config_gro(hw, false);
4754 PMD_INIT_LOG(ERR, "Failed to config gro: %d", ret);
4759 * In the initialization clearing the all hardware mapping relationship
4760 * configurations between queues and interrupt vectors is needed, so
4761 * some error caused by the residual configurations, such as the
4762 * unexpected interrupt, can be avoid.
4764 ret = hns3_init_ring_with_vector(hw);
4766 PMD_INIT_LOG(ERR, "Failed to init ring intr vector: %d", ret);
4771 * Requiring firmware to enable some features, driver can
4772 * still work without it.
4774 ret = hns3_firmware_compat_config(hw, true);
4776 PMD_INIT_LOG(WARNING, "firmware compatible features not "
4777 "supported, ret = %d.", ret);
4782 hns3_uninit_umv_space(hw);
4787 hns3_clear_hw(struct hns3_hw *hw)
4789 struct hns3_cmd_desc desc;
4792 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_CLEAR_HW_STATE, false);
4794 ret = hns3_cmd_send(hw, &desc, 1);
4795 if (ret && ret != -EOPNOTSUPP)
4802 hns3_config_all_msix_error(struct hns3_hw *hw, bool enable)
4807 * The new firmware support report more hardware error types by
4808 * msix mode. These errors are defined as RAS errors in hardware
4809 * and belong to a different type from the MSI-x errors processed
4810 * by the network driver.
4812 * Network driver should open the new error report on initialition
4814 val = hns3_read_dev(hw, HNS3_VECTOR0_OTER_EN_REG);
4815 hns3_set_bit(val, HNS3_VECTOR0_ALL_MSIX_ERR_B, enable ? 1 : 0);
4816 hns3_write_dev(hw, HNS3_VECTOR0_OTER_EN_REG, val);
4820 hns3_init_pf(struct rte_eth_dev *eth_dev)
4822 struct rte_device *dev = eth_dev->device;
4823 struct rte_pci_device *pci_dev = RTE_DEV_TO_PCI(dev);
4824 struct hns3_adapter *hns = eth_dev->data->dev_private;
4825 struct hns3_hw *hw = &hns->hw;
4828 PMD_INIT_FUNC_TRACE();
4830 /* Get hardware io base address from pcie BAR2 IO space */
4831 hw->io_base = pci_dev->mem_resource[2].addr;
4833 /* Firmware command queue initialize */
4834 ret = hns3_cmd_init_queue(hw);
4836 PMD_INIT_LOG(ERR, "Failed to init cmd queue: %d", ret);
4837 goto err_cmd_init_queue;
4840 hns3_clear_all_event_cause(hw);
4842 /* Firmware command initialize */
4843 ret = hns3_cmd_init(hw);
4845 PMD_INIT_LOG(ERR, "Failed to init cmd: %d", ret);
4850 * To ensure that the hardware environment is clean during
4851 * initialization, the driver actively clear the hardware environment
4852 * during initialization, including PF and corresponding VFs' vlan, mac,
4853 * flow table configurations, etc.
4855 ret = hns3_clear_hw(hw);
4857 PMD_INIT_LOG(ERR, "failed to clear hardware: %d", ret);
4861 /* Hardware statistics of imissed registers cleared. */
4862 ret = hns3_update_imissed_stats(hw, true);
4864 hns3_err(hw, "clear imissed stats failed, ret = %d", ret);
4868 hns3_config_all_msix_error(hw, true);
4870 ret = rte_intr_callback_register(&pci_dev->intr_handle,
4871 hns3_interrupt_handler,
4874 PMD_INIT_LOG(ERR, "Failed to register intr: %d", ret);
4875 goto err_intr_callback_register;
4878 /* Enable interrupt */
4879 rte_intr_enable(&pci_dev->intr_handle);
4880 hns3_pf_enable_irq0(hw);
4882 /* Get configuration */
4883 ret = hns3_get_configuration(hw);
4885 PMD_INIT_LOG(ERR, "Failed to fetch configuration: %d", ret);
4886 goto err_get_config;
4889 ret = hns3_tqp_stats_init(hw);
4891 goto err_get_config;
4893 ret = hns3_init_hardware(hns);
4895 PMD_INIT_LOG(ERR, "Failed to init hardware: %d", ret);
4899 /* Initialize flow director filter list & hash */
4900 ret = hns3_fdir_filter_init(hns);
4902 PMD_INIT_LOG(ERR, "Failed to alloc hashmap for fdir: %d", ret);
4906 hns3_rss_set_default_args(hw);
4908 ret = hns3_enable_hw_error_intr(hns, true);
4910 PMD_INIT_LOG(ERR, "fail to enable hw error interrupts: %d",
4912 goto err_enable_intr;
4915 hns3_tm_conf_init(eth_dev);
4920 hns3_fdir_filter_uninit(hns);
4922 (void)hns3_firmware_compat_config(hw, false);
4923 hns3_uninit_umv_space(hw);
4925 hns3_tqp_stats_uninit(hw);
4927 hns3_pf_disable_irq0(hw);
4928 rte_intr_disable(&pci_dev->intr_handle);
4929 hns3_intr_unregister(&pci_dev->intr_handle, hns3_interrupt_handler,
4931 err_intr_callback_register:
4933 hns3_cmd_uninit(hw);
4934 hns3_cmd_destroy_queue(hw);
4942 hns3_uninit_pf(struct rte_eth_dev *eth_dev)
4944 struct hns3_adapter *hns = eth_dev->data->dev_private;
4945 struct rte_device *dev = eth_dev->device;
4946 struct rte_pci_device *pci_dev = RTE_DEV_TO_PCI(dev);
4947 struct hns3_hw *hw = &hns->hw;
4949 PMD_INIT_FUNC_TRACE();
4951 hns3_tm_conf_uninit(eth_dev);
4952 hns3_enable_hw_error_intr(hns, false);
4953 hns3_rss_uninit(hns);
4954 (void)hns3_config_gro(hw, false);
4955 hns3_promisc_uninit(hw);
4956 hns3_fdir_filter_uninit(hns);
4957 (void)hns3_firmware_compat_config(hw, false);
4958 hns3_uninit_umv_space(hw);
4959 hns3_tqp_stats_uninit(hw);
4960 hns3_pf_disable_irq0(hw);
4961 rte_intr_disable(&pci_dev->intr_handle);
4962 hns3_intr_unregister(&pci_dev->intr_handle, hns3_interrupt_handler,
4964 hns3_config_all_msix_error(hw, false);
4965 hns3_cmd_uninit(hw);
4966 hns3_cmd_destroy_queue(hw);
4971 hns3_do_start(struct hns3_adapter *hns, bool reset_queue)
4973 struct hns3_hw *hw = &hns->hw;
4976 ret = hns3_dcb_cfg_update(hns);
4981 * The hns3_dcb_cfg_update may configure TM module, so
4982 * hns3_tm_conf_update must called later.
4984 ret = hns3_tm_conf_update(hw);
4986 PMD_INIT_LOG(ERR, "failed to update tm conf, ret = %d.", ret);
4990 hns3_enable_rxd_adv_layout(hw);
4992 ret = hns3_init_queues(hns, reset_queue);
4994 PMD_INIT_LOG(ERR, "failed to init queues, ret = %d.", ret);
4998 ret = hns3_cfg_mac_mode(hw, true);
5000 PMD_INIT_LOG(ERR, "failed to enable MAC, ret = %d", ret);
5001 goto err_config_mac_mode;
5005 err_config_mac_mode:
5006 hns3_dev_release_mbufs(hns);
5008 * Here is exception handling, hns3_reset_all_tqps will have the
5009 * corresponding error message if it is handled incorrectly, so it is
5010 * not necessary to check hns3_reset_all_tqps return value, here keep
5011 * ret as the error code causing the exception.
5013 (void)hns3_reset_all_tqps(hns);
5018 hns3_map_rx_interrupt(struct rte_eth_dev *dev)
5020 struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
5021 struct rte_intr_handle *intr_handle = &pci_dev->intr_handle;
5022 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
5023 uint16_t base = RTE_INTR_VEC_ZERO_OFFSET;
5024 uint16_t vec = RTE_INTR_VEC_ZERO_OFFSET;
5025 uint32_t intr_vector;
5030 * hns3 needs a separate interrupt to be used as event interrupt which
5031 * could not be shared with task queue pair, so KERNEL drivers need
5032 * support multiple interrupt vectors.
5034 if (dev->data->dev_conf.intr_conf.rxq == 0 ||
5035 !rte_intr_cap_multiple(intr_handle))
5038 rte_intr_disable(intr_handle);
5039 intr_vector = hw->used_rx_queues;
5040 /* creates event fd for each intr vector when MSIX is used */
5041 if (rte_intr_efd_enable(intr_handle, intr_vector))
5044 if (intr_handle->intr_vec == NULL) {
5045 intr_handle->intr_vec =
5046 rte_zmalloc("intr_vec",
5047 hw->used_rx_queues * sizeof(int), 0);
5048 if (intr_handle->intr_vec == NULL) {
5049 hns3_err(hw, "failed to allocate %u rx_queues intr_vec",
5050 hw->used_rx_queues);
5052 goto alloc_intr_vec_error;
5056 if (rte_intr_allow_others(intr_handle)) {
5057 vec = RTE_INTR_VEC_RXTX_OFFSET;
5058 base = RTE_INTR_VEC_RXTX_OFFSET;
5061 for (q_id = 0; q_id < hw->used_rx_queues; q_id++) {
5062 ret = hns3_bind_ring_with_vector(hw, vec, true,
5063 HNS3_RING_TYPE_RX, q_id);
5065 goto bind_vector_error;
5066 intr_handle->intr_vec[q_id] = vec;
5068 * If there are not enough efds (e.g. not enough interrupt),
5069 * remaining queues will be bond to the last interrupt.
5071 if (vec < base + intr_handle->nb_efd - 1)
5074 rte_intr_enable(intr_handle);
5078 rte_free(intr_handle->intr_vec);
5079 intr_handle->intr_vec = NULL;
5080 alloc_intr_vec_error:
5081 rte_intr_efd_disable(intr_handle);
5086 hns3_restore_rx_interrupt(struct hns3_hw *hw)
5088 struct rte_eth_dev *dev = &rte_eth_devices[hw->data->port_id];
5089 struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
5090 struct rte_intr_handle *intr_handle = &pci_dev->intr_handle;
5094 if (dev->data->dev_conf.intr_conf.rxq == 0)
5097 if (rte_intr_dp_is_en(intr_handle)) {
5098 for (q_id = 0; q_id < hw->used_rx_queues; q_id++) {
5099 ret = hns3_bind_ring_with_vector(hw,
5100 intr_handle->intr_vec[q_id], true,
5101 HNS3_RING_TYPE_RX, q_id);
5111 hns3_restore_filter(struct rte_eth_dev *dev)
5113 hns3_restore_rss_filter(dev);
5117 hns3_dev_start(struct rte_eth_dev *dev)
5119 struct hns3_adapter *hns = dev->data->dev_private;
5120 struct hns3_hw *hw = &hns->hw;
5123 PMD_INIT_FUNC_TRACE();
5124 if (__atomic_load_n(&hw->reset.resetting, __ATOMIC_RELAXED))
5127 rte_spinlock_lock(&hw->lock);
5128 hw->adapter_state = HNS3_NIC_STARTING;
5130 ret = hns3_do_start(hns, true);
5132 hw->adapter_state = HNS3_NIC_CONFIGURED;
5133 rte_spinlock_unlock(&hw->lock);
5136 ret = hns3_map_rx_interrupt(dev);
5138 goto map_rx_inter_err;
5141 * There are three register used to control the status of a TQP
5142 * (contains a pair of Tx queue and Rx queue) in the new version network
5143 * engine. One is used to control the enabling of Tx queue, the other is
5144 * used to control the enabling of Rx queue, and the last is the master
5145 * switch used to control the enabling of the tqp. The Tx register and
5146 * TQP register must be enabled at the same time to enable a Tx queue.
5147 * The same applies to the Rx queue. For the older network engine, this
5148 * function only refresh the enabled flag, and it is used to update the
5149 * status of queue in the dpdk framework.
5151 ret = hns3_start_all_txqs(dev);
5153 goto map_rx_inter_err;
5155 ret = hns3_start_all_rxqs(dev);
5157 goto start_all_rxqs_fail;
5159 hw->adapter_state = HNS3_NIC_STARTED;
5160 rte_spinlock_unlock(&hw->lock);
5162 hns3_rx_scattered_calc(dev);
5163 hns3_set_rxtx_function(dev);
5164 hns3_mp_req_start_rxtx(dev);
5165 rte_eal_alarm_set(HNS3_SERVICE_INTERVAL, hns3_service_handler, dev);
5167 hns3_restore_filter(dev);
5169 /* Enable interrupt of all rx queues before enabling queues */
5170 hns3_dev_all_rx_queue_intr_enable(hw, true);
5173 * After finished the initialization, enable tqps to receive/transmit
5174 * packets and refresh all queue status.
5176 hns3_start_tqps(hw);
5178 hns3_tm_dev_start_proc(hw);
5180 hns3_info(hw, "hns3 dev start successful!");
5184 start_all_rxqs_fail:
5185 hns3_stop_all_txqs(dev);
5187 (void)hns3_do_stop(hns);
5188 hw->adapter_state = HNS3_NIC_CONFIGURED;
5189 rte_spinlock_unlock(&hw->lock);
5195 hns3_do_stop(struct hns3_adapter *hns)
5197 struct hns3_hw *hw = &hns->hw;
5201 * The "hns3_do_stop" function will also be called by .stop_service to
5202 * prepare reset. At the time of global or IMP reset, the command cannot
5203 * be sent to stop the tx/rx queues. The mbuf in Tx/Rx queues may be
5204 * accessed during the reset process. So the mbuf can not be released
5205 * during reset and is required to be released after the reset is
5208 if (__atomic_load_n(&hw->reset.resetting, __ATOMIC_RELAXED) == 0)
5209 hns3_dev_release_mbufs(hns);
5211 ret = hns3_cfg_mac_mode(hw, false);
5214 hw->mac.link_status = ETH_LINK_DOWN;
5216 if (__atomic_load_n(&hw->reset.disable_cmd, __ATOMIC_RELAXED) == 0) {
5217 hns3_configure_all_mac_addr(hns, true);
5218 ret = hns3_reset_all_tqps(hns);
5220 hns3_err(hw, "failed to reset all queues ret = %d.",
5225 hw->mac.default_addr_setted = false;
5230 hns3_unmap_rx_interrupt(struct rte_eth_dev *dev)
5232 struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
5233 struct rte_intr_handle *intr_handle = &pci_dev->intr_handle;
5234 struct hns3_adapter *hns = dev->data->dev_private;
5235 struct hns3_hw *hw = &hns->hw;
5236 uint8_t base = RTE_INTR_VEC_ZERO_OFFSET;
5237 uint8_t vec = RTE_INTR_VEC_ZERO_OFFSET;
5240 if (dev->data->dev_conf.intr_conf.rxq == 0)
5243 /* unmap the ring with vector */
5244 if (rte_intr_allow_others(intr_handle)) {
5245 vec = RTE_INTR_VEC_RXTX_OFFSET;
5246 base = RTE_INTR_VEC_RXTX_OFFSET;
5248 if (rte_intr_dp_is_en(intr_handle)) {
5249 for (q_id = 0; q_id < hw->used_rx_queues; q_id++) {
5250 (void)hns3_bind_ring_with_vector(hw, vec, false,
5253 if (vec < base + intr_handle->nb_efd - 1)
5257 /* Clean datapath event and queue/vec mapping */
5258 rte_intr_efd_disable(intr_handle);
5259 if (intr_handle->intr_vec) {
5260 rte_free(intr_handle->intr_vec);
5261 intr_handle->intr_vec = NULL;
5266 hns3_dev_stop(struct rte_eth_dev *dev)
5268 struct hns3_adapter *hns = dev->data->dev_private;
5269 struct hns3_hw *hw = &hns->hw;
5271 PMD_INIT_FUNC_TRACE();
5272 dev->data->dev_started = 0;
5274 hw->adapter_state = HNS3_NIC_STOPPING;
5275 hns3_set_rxtx_function(dev);
5277 /* Disable datapath on secondary process. */
5278 hns3_mp_req_stop_rxtx(dev);
5279 /* Prevent crashes when queues are still in use. */
5280 rte_delay_ms(hw->tqps_num);
5282 rte_spinlock_lock(&hw->lock);
5283 if (__atomic_load_n(&hw->reset.resetting, __ATOMIC_RELAXED) == 0) {
5284 hns3_tm_dev_stop_proc(hw);
5287 hns3_unmap_rx_interrupt(dev);
5288 hw->adapter_state = HNS3_NIC_CONFIGURED;
5290 hns3_rx_scattered_reset(dev);
5291 rte_eal_alarm_cancel(hns3_service_handler, dev);
5292 rte_spinlock_unlock(&hw->lock);
5298 hns3_dev_close(struct rte_eth_dev *eth_dev)
5300 struct hns3_adapter *hns = eth_dev->data->dev_private;
5301 struct hns3_hw *hw = &hns->hw;
5304 if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
5305 rte_free(eth_dev->process_private);
5306 eth_dev->process_private = NULL;
5310 if (hw->adapter_state == HNS3_NIC_STARTED)
5311 ret = hns3_dev_stop(eth_dev);
5313 hw->adapter_state = HNS3_NIC_CLOSING;
5314 hns3_reset_abort(hns);
5315 hw->adapter_state = HNS3_NIC_CLOSED;
5317 hns3_configure_all_mc_mac_addr(hns, true);
5318 hns3_remove_all_vlan_table(hns);
5319 hns3_vlan_txvlan_cfg(hns, HNS3_PORT_BASE_VLAN_DISABLE, 0);
5320 hns3_uninit_pf(eth_dev);
5321 hns3_free_all_queues(eth_dev);
5322 rte_free(hw->reset.wait_data);
5323 rte_free(eth_dev->process_private);
5324 eth_dev->process_private = NULL;
5325 hns3_mp_uninit_primary();
5326 hns3_warn(hw, "Close port %u finished", hw->data->port_id);
5332 hns3_flow_ctrl_get(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
5334 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
5335 struct hns3_pf *pf = HNS3_DEV_PRIVATE_TO_PF(dev->data->dev_private);
5337 fc_conf->pause_time = pf->pause_time;
5339 /* return fc current mode */
5340 switch (hw->current_mode) {
5342 fc_conf->mode = RTE_FC_FULL;
5344 case HNS3_FC_TX_PAUSE:
5345 fc_conf->mode = RTE_FC_TX_PAUSE;
5347 case HNS3_FC_RX_PAUSE:
5348 fc_conf->mode = RTE_FC_RX_PAUSE;
5352 fc_conf->mode = RTE_FC_NONE;
5360 hns3_get_fc_mode(struct hns3_hw *hw, enum rte_eth_fc_mode mode)
5364 hw->requested_mode = HNS3_FC_NONE;
5366 case RTE_FC_RX_PAUSE:
5367 hw->requested_mode = HNS3_FC_RX_PAUSE;
5369 case RTE_FC_TX_PAUSE:
5370 hw->requested_mode = HNS3_FC_TX_PAUSE;
5373 hw->requested_mode = HNS3_FC_FULL;
5376 hw->requested_mode = HNS3_FC_NONE;
5377 hns3_warn(hw, "fc_mode(%u) exceeds member scope and is "
5378 "configured to RTE_FC_NONE", mode);
5384 hns3_flow_ctrl_set(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
5386 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
5387 struct hns3_pf *pf = HNS3_DEV_PRIVATE_TO_PF(dev->data->dev_private);
5390 if (fc_conf->high_water || fc_conf->low_water ||
5391 fc_conf->send_xon || fc_conf->mac_ctrl_frame_fwd) {
5392 hns3_err(hw, "Unsupported flow control settings specified, "
5393 "high_water(%u), low_water(%u), send_xon(%u) and "
5394 "mac_ctrl_frame_fwd(%u) must be set to '0'",
5395 fc_conf->high_water, fc_conf->low_water,
5396 fc_conf->send_xon, fc_conf->mac_ctrl_frame_fwd);
5399 if (fc_conf->autoneg) {
5400 hns3_err(hw, "Unsupported fc auto-negotiation setting.");
5403 if (!fc_conf->pause_time) {
5404 hns3_err(hw, "Invalid pause time %u setting.",
5405 fc_conf->pause_time);
5409 if (!(hw->current_fc_status == HNS3_FC_STATUS_NONE ||
5410 hw->current_fc_status == HNS3_FC_STATUS_MAC_PAUSE)) {
5411 hns3_err(hw, "PFC is enabled. Cannot set MAC pause. "
5412 "current_fc_status = %d", hw->current_fc_status);
5416 hns3_get_fc_mode(hw, fc_conf->mode);
5417 if (hw->requested_mode == hw->current_mode &&
5418 pf->pause_time == fc_conf->pause_time)
5421 rte_spinlock_lock(&hw->lock);
5422 ret = hns3_fc_enable(dev, fc_conf);
5423 rte_spinlock_unlock(&hw->lock);
5429 hns3_priority_flow_ctrl_set(struct rte_eth_dev *dev,
5430 struct rte_eth_pfc_conf *pfc_conf)
5432 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
5433 struct hns3_pf *pf = HNS3_DEV_PRIVATE_TO_PF(dev->data->dev_private);
5437 if (!hns3_dev_dcb_supported(hw)) {
5438 hns3_err(hw, "This port does not support dcb configurations.");
5442 if (pfc_conf->fc.high_water || pfc_conf->fc.low_water ||
5443 pfc_conf->fc.send_xon || pfc_conf->fc.mac_ctrl_frame_fwd) {
5444 hns3_err(hw, "Unsupported flow control settings specified, "
5445 "high_water(%u), low_water(%u), send_xon(%u) and "
5446 "mac_ctrl_frame_fwd(%u) must be set to '0'",
5447 pfc_conf->fc.high_water, pfc_conf->fc.low_water,
5448 pfc_conf->fc.send_xon,
5449 pfc_conf->fc.mac_ctrl_frame_fwd);
5452 if (pfc_conf->fc.autoneg) {
5453 hns3_err(hw, "Unsupported fc auto-negotiation setting.");
5456 if (pfc_conf->fc.pause_time == 0) {
5457 hns3_err(hw, "Invalid pause time %u setting.",
5458 pfc_conf->fc.pause_time);
5462 if (!(hw->current_fc_status == HNS3_FC_STATUS_NONE ||
5463 hw->current_fc_status == HNS3_FC_STATUS_PFC)) {
5464 hns3_err(hw, "MAC pause is enabled. Cannot set PFC."
5465 "current_fc_status = %d", hw->current_fc_status);
5469 priority = pfc_conf->priority;
5470 hns3_get_fc_mode(hw, pfc_conf->fc.mode);
5471 if (hw->dcb_info.pfc_en & BIT(priority) &&
5472 hw->requested_mode == hw->current_mode &&
5473 pfc_conf->fc.pause_time == pf->pause_time)
5476 rte_spinlock_lock(&hw->lock);
5477 ret = hns3_dcb_pfc_enable(dev, pfc_conf);
5478 rte_spinlock_unlock(&hw->lock);
5484 hns3_get_dcb_info(struct rte_eth_dev *dev, struct rte_eth_dcb_info *dcb_info)
5486 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
5487 struct hns3_pf *pf = HNS3_DEV_PRIVATE_TO_PF(dev->data->dev_private);
5488 enum rte_eth_rx_mq_mode mq_mode = dev->data->dev_conf.rxmode.mq_mode;
5491 rte_spinlock_lock(&hw->lock);
5492 if ((uint32_t)mq_mode & ETH_MQ_RX_DCB_FLAG)
5493 dcb_info->nb_tcs = pf->local_max_tc;
5495 dcb_info->nb_tcs = 1;
5497 for (i = 0; i < HNS3_MAX_USER_PRIO; i++)
5498 dcb_info->prio_tc[i] = hw->dcb_info.prio_tc[i];
5499 for (i = 0; i < dcb_info->nb_tcs; i++)
5500 dcb_info->tc_bws[i] = hw->dcb_info.pg_info[0].tc_dwrr[i];
5502 for (i = 0; i < hw->num_tc; i++) {
5503 dcb_info->tc_queue.tc_rxq[0][i].base = hw->alloc_rss_size * i;
5504 dcb_info->tc_queue.tc_txq[0][i].base =
5505 hw->tc_queue[i].tqp_offset;
5506 dcb_info->tc_queue.tc_rxq[0][i].nb_queue = hw->alloc_rss_size;
5507 dcb_info->tc_queue.tc_txq[0][i].nb_queue =
5508 hw->tc_queue[i].tqp_count;
5510 rte_spinlock_unlock(&hw->lock);
5516 hns3_reinit_dev(struct hns3_adapter *hns)
5518 struct hns3_hw *hw = &hns->hw;
5521 ret = hns3_cmd_init(hw);
5523 hns3_err(hw, "Failed to init cmd: %d", ret);
5527 ret = hns3_reset_all_tqps(hns);
5529 hns3_err(hw, "Failed to reset all queues: %d", ret);
5533 ret = hns3_init_hardware(hns);
5535 hns3_err(hw, "Failed to init hardware: %d", ret);
5539 ret = hns3_enable_hw_error_intr(hns, true);
5541 hns3_err(hw, "fail to enable hw error interrupts: %d",
5545 hns3_info(hw, "Reset done, driver initialization finished.");
5551 is_pf_reset_done(struct hns3_hw *hw)
5553 uint32_t val, reg, reg_bit;
5555 switch (hw->reset.level) {
5556 case HNS3_IMP_RESET:
5557 reg = HNS3_GLOBAL_RESET_REG;
5558 reg_bit = HNS3_IMP_RESET_BIT;
5560 case HNS3_GLOBAL_RESET:
5561 reg = HNS3_GLOBAL_RESET_REG;
5562 reg_bit = HNS3_GLOBAL_RESET_BIT;
5564 case HNS3_FUNC_RESET:
5565 reg = HNS3_FUN_RST_ING;
5566 reg_bit = HNS3_FUN_RST_ING_B;
5568 case HNS3_FLR_RESET:
5570 hns3_err(hw, "Wait for unsupported reset level: %d",
5574 val = hns3_read_dev(hw, reg);
5575 if (hns3_get_bit(val, reg_bit))
5582 hns3_is_reset_pending(struct hns3_adapter *hns)
5584 struct hns3_hw *hw = &hns->hw;
5585 enum hns3_reset_level reset;
5587 hns3_check_event_cause(hns, NULL);
5588 reset = hns3_get_reset_level(hns, &hw->reset.pending);
5589 if (hw->reset.level != HNS3_NONE_RESET && hw->reset.level < reset) {
5590 hns3_warn(hw, "High level reset %d is pending", reset);
5593 reset = hns3_get_reset_level(hns, &hw->reset.request);
5594 if (hw->reset.level != HNS3_NONE_RESET && hw->reset.level < reset) {
5595 hns3_warn(hw, "High level reset %d is request", reset);
5602 hns3_wait_hardware_ready(struct hns3_adapter *hns)
5604 struct hns3_hw *hw = &hns->hw;
5605 struct hns3_wait_data *wait_data = hw->reset.wait_data;
5608 if (wait_data->result == HNS3_WAIT_SUCCESS)
5610 else if (wait_data->result == HNS3_WAIT_TIMEOUT) {
5611 gettimeofday(&tv, NULL);
5612 hns3_warn(hw, "Reset step4 hardware not ready after reset time=%ld.%.6ld",
5613 tv.tv_sec, tv.tv_usec);
5615 } else if (wait_data->result == HNS3_WAIT_REQUEST)
5618 wait_data->hns = hns;
5619 wait_data->check_completion = is_pf_reset_done;
5620 wait_data->end_ms = (uint64_t)HNS3_RESET_WAIT_CNT *
5621 HNS3_RESET_WAIT_MS + get_timeofday_ms();
5622 wait_data->interval = HNS3_RESET_WAIT_MS * USEC_PER_MSEC;
5623 wait_data->count = HNS3_RESET_WAIT_CNT;
5624 wait_data->result = HNS3_WAIT_REQUEST;
5625 rte_eal_alarm_set(wait_data->interval, hns3_wait_callback, wait_data);
5630 hns3_func_reset_cmd(struct hns3_hw *hw, int func_id)
5632 struct hns3_cmd_desc desc;
5633 struct hns3_reset_cmd *req = (struct hns3_reset_cmd *)desc.data;
5635 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_CFG_RST_TRIGGER, false);
5636 hns3_set_bit(req->mac_func_reset, HNS3_CFG_RESET_FUNC_B, 1);
5637 req->fun_reset_vfid = func_id;
5639 return hns3_cmd_send(hw, &desc, 1);
5643 hns3_imp_reset_cmd(struct hns3_hw *hw)
5645 struct hns3_cmd_desc desc;
5647 hns3_cmd_setup_basic_desc(&desc, 0xFFFE, false);
5648 desc.data[0] = 0xeedd;
5650 return hns3_cmd_send(hw, &desc, 1);
5654 hns3_msix_process(struct hns3_adapter *hns, enum hns3_reset_level reset_level)
5656 struct hns3_hw *hw = &hns->hw;
5660 gettimeofday(&tv, NULL);
5661 if (hns3_read_dev(hw, HNS3_GLOBAL_RESET_REG) ||
5662 hns3_read_dev(hw, HNS3_FUN_RST_ING)) {
5663 hns3_warn(hw, "Don't process msix during resetting time=%ld.%.6ld",
5664 tv.tv_sec, tv.tv_usec);
5668 switch (reset_level) {
5669 case HNS3_IMP_RESET:
5670 hns3_imp_reset_cmd(hw);
5671 hns3_warn(hw, "IMP Reset requested time=%ld.%.6ld",
5672 tv.tv_sec, tv.tv_usec);
5674 case HNS3_GLOBAL_RESET:
5675 val = hns3_read_dev(hw, HNS3_GLOBAL_RESET_REG);
5676 hns3_set_bit(val, HNS3_GLOBAL_RESET_BIT, 1);
5677 hns3_write_dev(hw, HNS3_GLOBAL_RESET_REG, val);
5678 hns3_warn(hw, "Global Reset requested time=%ld.%.6ld",
5679 tv.tv_sec, tv.tv_usec);
5681 case HNS3_FUNC_RESET:
5682 hns3_warn(hw, "PF Reset requested time=%ld.%.6ld",
5683 tv.tv_sec, tv.tv_usec);
5684 /* schedule again to check later */
5685 hns3_atomic_set_bit(HNS3_FUNC_RESET, &hw->reset.pending);
5686 hns3_schedule_reset(hns);
5689 hns3_warn(hw, "Unsupported reset level: %d", reset_level);
5692 hns3_atomic_clear_bit(reset_level, &hw->reset.request);
5695 static enum hns3_reset_level
5696 hns3_get_reset_level(struct hns3_adapter *hns, uint64_t *levels)
5698 struct hns3_hw *hw = &hns->hw;
5699 enum hns3_reset_level reset_level = HNS3_NONE_RESET;
5701 /* Return the highest priority reset level amongst all */
5702 if (hns3_atomic_test_bit(HNS3_IMP_RESET, levels))
5703 reset_level = HNS3_IMP_RESET;
5704 else if (hns3_atomic_test_bit(HNS3_GLOBAL_RESET, levels))
5705 reset_level = HNS3_GLOBAL_RESET;
5706 else if (hns3_atomic_test_bit(HNS3_FUNC_RESET, levels))
5707 reset_level = HNS3_FUNC_RESET;
5708 else if (hns3_atomic_test_bit(HNS3_FLR_RESET, levels))
5709 reset_level = HNS3_FLR_RESET;
5711 if (hw->reset.level != HNS3_NONE_RESET && reset_level < hw->reset.level)
5712 return HNS3_NONE_RESET;
5718 hns3_record_imp_error(struct hns3_adapter *hns)
5720 struct hns3_hw *hw = &hns->hw;
5723 reg_val = hns3_read_dev(hw, HNS3_VECTOR0_OTER_EN_REG);
5724 if (hns3_get_bit(reg_val, HNS3_VECTOR0_IMP_RD_POISON_B)) {
5725 hns3_warn(hw, "Detected IMP RD poison!");
5726 hns3_error_int_stats_add(hns, "IMP_RD_POISON_INT_STS");
5727 hns3_set_bit(reg_val, HNS3_VECTOR0_IMP_RD_POISON_B, 0);
5728 hns3_write_dev(hw, HNS3_VECTOR0_OTER_EN_REG, reg_val);
5731 if (hns3_get_bit(reg_val, HNS3_VECTOR0_IMP_CMDQ_ERR_B)) {
5732 hns3_warn(hw, "Detected IMP CMDQ error!");
5733 hns3_error_int_stats_add(hns, "CMDQ_MEM_ECC_INT_STS");
5734 hns3_set_bit(reg_val, HNS3_VECTOR0_IMP_CMDQ_ERR_B, 0);
5735 hns3_write_dev(hw, HNS3_VECTOR0_OTER_EN_REG, reg_val);
5740 hns3_prepare_reset(struct hns3_adapter *hns)
5742 struct hns3_hw *hw = &hns->hw;
5746 switch (hw->reset.level) {
5747 case HNS3_FUNC_RESET:
5748 ret = hns3_func_reset_cmd(hw, HNS3_PF_FUNC_ID);
5753 * After performaning pf reset, it is not necessary to do the
5754 * mailbox handling or send any command to firmware, because
5755 * any mailbox handling or command to firmware is only valid
5756 * after hns3_cmd_init is called.
5758 __atomic_store_n(&hw->reset.disable_cmd, 1, __ATOMIC_RELAXED);
5759 hw->reset.stats.request_cnt++;
5761 case HNS3_IMP_RESET:
5762 hns3_record_imp_error(hns);
5763 reg_val = hns3_read_dev(hw, HNS3_VECTOR0_OTER_EN_REG);
5764 hns3_write_dev(hw, HNS3_VECTOR0_OTER_EN_REG, reg_val |
5765 BIT(HNS3_VECTOR0_IMP_RESET_INT_B));
5774 hns3_set_rst_done(struct hns3_hw *hw)
5776 struct hns3_pf_rst_done_cmd *req;
5777 struct hns3_cmd_desc desc;
5779 req = (struct hns3_pf_rst_done_cmd *)desc.data;
5780 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_PF_RST_DONE, false);
5781 req->pf_rst_done |= HNS3_PF_RESET_DONE_BIT;
5782 return hns3_cmd_send(hw, &desc, 1);
5786 hns3_stop_service(struct hns3_adapter *hns)
5788 struct hns3_hw *hw = &hns->hw;
5789 struct rte_eth_dev *eth_dev;
5791 eth_dev = &rte_eth_devices[hw->data->port_id];
5792 if (hw->adapter_state == HNS3_NIC_STARTED) {
5793 rte_eal_alarm_cancel(hns3_service_handler, eth_dev);
5794 hns3_update_link_status_and_event(hw);
5796 hw->mac.link_status = ETH_LINK_DOWN;
5798 hns3_set_rxtx_function(eth_dev);
5800 /* Disable datapath on secondary process. */
5801 hns3_mp_req_stop_rxtx(eth_dev);
5802 rte_delay_ms(hw->tqps_num);
5804 rte_spinlock_lock(&hw->lock);
5805 if (hns->hw.adapter_state == HNS3_NIC_STARTED ||
5806 hw->adapter_state == HNS3_NIC_STOPPING) {
5807 hns3_enable_all_queues(hw, false);
5809 hw->reset.mbuf_deferred_free = true;
5811 hw->reset.mbuf_deferred_free = false;
5814 * It is cumbersome for hardware to pick-and-choose entries for deletion
5815 * from table space. Hence, for function reset software intervention is
5816 * required to delete the entries
5818 if (__atomic_load_n(&hw->reset.disable_cmd, __ATOMIC_RELAXED) == 0)
5819 hns3_configure_all_mc_mac_addr(hns, true);
5820 rte_spinlock_unlock(&hw->lock);
5826 hns3_start_service(struct hns3_adapter *hns)
5828 struct hns3_hw *hw = &hns->hw;
5829 struct rte_eth_dev *eth_dev;
5831 if (hw->reset.level == HNS3_IMP_RESET ||
5832 hw->reset.level == HNS3_GLOBAL_RESET)
5833 hns3_set_rst_done(hw);
5834 eth_dev = &rte_eth_devices[hw->data->port_id];
5835 hns3_set_rxtx_function(eth_dev);
5836 hns3_mp_req_start_rxtx(eth_dev);
5837 if (hw->adapter_state == HNS3_NIC_STARTED) {
5839 * This API parent function already hold the hns3_hw.lock, the
5840 * hns3_service_handler may report lse, in bonding application
5841 * it will call driver's ops which may acquire the hns3_hw.lock
5842 * again, thus lead to deadlock.
5843 * We defer calls hns3_service_handler to avoid the deadlock.
5845 rte_eal_alarm_set(HNS3_SERVICE_QUICK_INTERVAL,
5846 hns3_service_handler, eth_dev);
5848 /* Enable interrupt of all rx queues before enabling queues */
5849 hns3_dev_all_rx_queue_intr_enable(hw, true);
5851 * Enable state of each rxq and txq will be recovered after
5852 * reset, so we need to restore them before enable all tqps;
5854 hns3_restore_tqp_enable_state(hw);
5856 * When finished the initialization, enable queues to receive
5857 * and transmit packets.
5859 hns3_enable_all_queues(hw, true);
5866 hns3_restore_conf(struct hns3_adapter *hns)
5868 struct hns3_hw *hw = &hns->hw;
5871 ret = hns3_configure_all_mac_addr(hns, false);
5875 ret = hns3_configure_all_mc_mac_addr(hns, false);
5879 ret = hns3_dev_promisc_restore(hns);
5883 ret = hns3_restore_vlan_table(hns);
5887 ret = hns3_restore_vlan_conf(hns);
5891 ret = hns3_restore_all_fdir_filter(hns);
5895 ret = hns3_restore_rx_interrupt(hw);
5899 ret = hns3_restore_gro_conf(hw);
5903 ret = hns3_restore_fec(hw);
5907 if (hns->hw.adapter_state == HNS3_NIC_STARTED) {
5908 ret = hns3_do_start(hns, false);
5911 hns3_info(hw, "hns3 dev restart successful!");
5912 } else if (hw->adapter_state == HNS3_NIC_STOPPING)
5913 hw->adapter_state = HNS3_NIC_CONFIGURED;
5917 hns3_configure_all_mc_mac_addr(hns, true);
5919 hns3_configure_all_mac_addr(hns, true);
5924 hns3_reset_service(void *param)
5926 struct hns3_adapter *hns = (struct hns3_adapter *)param;
5927 struct hns3_hw *hw = &hns->hw;
5928 enum hns3_reset_level reset_level;
5929 struct timeval tv_delta;
5930 struct timeval tv_start;
5936 * The interrupt is not triggered within the delay time.
5937 * The interrupt may have been lost. It is necessary to handle
5938 * the interrupt to recover from the error.
5940 if (__atomic_load_n(&hw->reset.schedule, __ATOMIC_RELAXED) ==
5941 SCHEDULE_DEFERRED) {
5942 __atomic_store_n(&hw->reset.schedule, SCHEDULE_REQUESTED,
5944 hns3_err(hw, "Handling interrupts in delayed tasks");
5945 hns3_interrupt_handler(&rte_eth_devices[hw->data->port_id]);
5946 reset_level = hns3_get_reset_level(hns, &hw->reset.pending);
5947 if (reset_level == HNS3_NONE_RESET) {
5948 hns3_err(hw, "No reset level is set, try IMP reset");
5949 hns3_atomic_set_bit(HNS3_IMP_RESET, &hw->reset.pending);
5952 __atomic_store_n(&hw->reset.schedule, SCHEDULE_NONE, __ATOMIC_RELAXED);
5955 * Check if there is any ongoing reset in the hardware. This status can
5956 * be checked from reset_pending. If there is then, we need to wait for
5957 * hardware to complete reset.
5958 * a. If we are able to figure out in reasonable time that hardware
5959 * has fully resetted then, we can proceed with driver, client
5961 * b. else, we can come back later to check this status so re-sched
5964 reset_level = hns3_get_reset_level(hns, &hw->reset.pending);
5965 if (reset_level != HNS3_NONE_RESET) {
5966 gettimeofday(&tv_start, NULL);
5967 ret = hns3_reset_process(hns, reset_level);
5968 gettimeofday(&tv, NULL);
5969 timersub(&tv, &tv_start, &tv_delta);
5970 msec = tv_delta.tv_sec * MSEC_PER_SEC +
5971 tv_delta.tv_usec / USEC_PER_MSEC;
5972 if (msec > HNS3_RESET_PROCESS_MS)
5973 hns3_err(hw, "%d handle long time delta %" PRIx64
5974 " ms time=%ld.%.6ld",
5975 hw->reset.level, msec,
5976 tv.tv_sec, tv.tv_usec);
5981 /* Check if we got any *new* reset requests to be honored */
5982 reset_level = hns3_get_reset_level(hns, &hw->reset.request);
5983 if (reset_level != HNS3_NONE_RESET)
5984 hns3_msix_process(hns, reset_level);
5988 hns3_get_speed_capa_num(uint16_t device_id)
5992 switch (device_id) {
5993 case HNS3_DEV_ID_25GE:
5994 case HNS3_DEV_ID_25GE_RDMA:
5997 case HNS3_DEV_ID_100G_RDMA_MACSEC:
5998 case HNS3_DEV_ID_200G_RDMA:
6010 hns3_get_speed_fec_capa(struct rte_eth_fec_capa *speed_fec_capa,
6013 switch (device_id) {
6014 case HNS3_DEV_ID_25GE:
6016 case HNS3_DEV_ID_25GE_RDMA:
6017 speed_fec_capa[0].speed = speed_fec_capa_tbl[1].speed;
6018 speed_fec_capa[0].capa = speed_fec_capa_tbl[1].capa;
6020 /* In HNS3 device, the 25G NIC is compatible with 10G rate */
6021 speed_fec_capa[1].speed = speed_fec_capa_tbl[0].speed;
6022 speed_fec_capa[1].capa = speed_fec_capa_tbl[0].capa;
6024 case HNS3_DEV_ID_100G_RDMA_MACSEC:
6025 speed_fec_capa[0].speed = speed_fec_capa_tbl[4].speed;
6026 speed_fec_capa[0].capa = speed_fec_capa_tbl[4].capa;
6028 case HNS3_DEV_ID_200G_RDMA:
6029 speed_fec_capa[0].speed = speed_fec_capa_tbl[5].speed;
6030 speed_fec_capa[0].capa = speed_fec_capa_tbl[5].capa;
6040 hns3_fec_get_capability(struct rte_eth_dev *dev,
6041 struct rte_eth_fec_capa *speed_fec_capa,
6044 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
6045 struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
6046 uint16_t device_id = pci_dev->id.device_id;
6047 unsigned int capa_num;
6050 capa_num = hns3_get_speed_capa_num(device_id);
6051 if (capa_num == 0) {
6052 hns3_err(hw, "device(0x%x) is not supported by hns3 PMD",
6057 if (speed_fec_capa == NULL || num < capa_num)
6060 ret = hns3_get_speed_fec_capa(speed_fec_capa, device_id);
6068 get_current_fec_auto_state(struct hns3_hw *hw, uint8_t *state)
6070 struct hns3_config_fec_cmd *req;
6071 struct hns3_cmd_desc desc;
6075 * CMD(HNS3_OPC_CONFIG_FEC_MODE) read is not supported
6076 * in device of link speed
6079 if (hw->mac.link_speed < ETH_SPEED_NUM_10G) {
6084 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_CONFIG_FEC_MODE, true);
6085 req = (struct hns3_config_fec_cmd *)desc.data;
6086 ret = hns3_cmd_send(hw, &desc, 1);
6088 hns3_err(hw, "get current fec auto state failed, ret = %d",
6093 *state = req->fec_mode & (1U << HNS3_MAC_CFG_FEC_AUTO_EN_B);
6098 hns3_fec_get_internal(struct hns3_hw *hw, uint32_t *fec_capa)
6100 #define QUERY_ACTIVE_SPEED 1
6101 struct hns3_sfp_speed_cmd *resp;
6102 uint32_t tmp_fec_capa;
6104 struct hns3_cmd_desc desc;
6108 * If link is down and AUTO is enabled, AUTO is returned, otherwise,
6109 * configured FEC mode is returned.
6110 * If link is up, current FEC mode is returned.
6112 if (hw->mac.link_status == ETH_LINK_DOWN) {
6113 ret = get_current_fec_auto_state(hw, &auto_state);
6117 if (auto_state == 0x1) {
6118 *fec_capa = RTE_ETH_FEC_MODE_CAPA_MASK(AUTO);
6123 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_SFP_GET_SPEED, true);
6124 resp = (struct hns3_sfp_speed_cmd *)desc.data;
6125 resp->query_type = QUERY_ACTIVE_SPEED;
6127 ret = hns3_cmd_send(hw, &desc, 1);
6128 if (ret == -EOPNOTSUPP) {
6129 hns3_err(hw, "IMP do not support get FEC, ret = %d", ret);
6132 hns3_err(hw, "get FEC failed, ret = %d", ret);
6137 * FEC mode order defined in hns3 hardware is inconsistend with
6138 * that defined in the ethdev library. So the sequence needs
6141 switch (resp->active_fec) {
6142 case HNS3_HW_FEC_MODE_NOFEC:
6143 tmp_fec_capa = RTE_ETH_FEC_MODE_CAPA_MASK(NOFEC);
6145 case HNS3_HW_FEC_MODE_BASER:
6146 tmp_fec_capa = RTE_ETH_FEC_MODE_CAPA_MASK(BASER);
6148 case HNS3_HW_FEC_MODE_RS:
6149 tmp_fec_capa = RTE_ETH_FEC_MODE_CAPA_MASK(RS);
6152 tmp_fec_capa = RTE_ETH_FEC_MODE_CAPA_MASK(NOFEC);
6156 *fec_capa = tmp_fec_capa;
6161 hns3_fec_get(struct rte_eth_dev *dev, uint32_t *fec_capa)
6163 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
6165 return hns3_fec_get_internal(hw, fec_capa);
6169 hns3_set_fec_hw(struct hns3_hw *hw, uint32_t mode)
6171 struct hns3_config_fec_cmd *req;
6172 struct hns3_cmd_desc desc;
6175 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_CONFIG_FEC_MODE, false);
6177 req = (struct hns3_config_fec_cmd *)desc.data;
6179 case RTE_ETH_FEC_MODE_CAPA_MASK(NOFEC):
6180 hns3_set_field(req->fec_mode, HNS3_MAC_CFG_FEC_MODE_M,
6181 HNS3_MAC_CFG_FEC_MODE_S, HNS3_MAC_FEC_OFF);
6183 case RTE_ETH_FEC_MODE_CAPA_MASK(BASER):
6184 hns3_set_field(req->fec_mode, HNS3_MAC_CFG_FEC_MODE_M,
6185 HNS3_MAC_CFG_FEC_MODE_S, HNS3_MAC_FEC_BASER);
6187 case RTE_ETH_FEC_MODE_CAPA_MASK(RS):
6188 hns3_set_field(req->fec_mode, HNS3_MAC_CFG_FEC_MODE_M,
6189 HNS3_MAC_CFG_FEC_MODE_S, HNS3_MAC_FEC_RS);
6191 case RTE_ETH_FEC_MODE_CAPA_MASK(AUTO):
6192 hns3_set_bit(req->fec_mode, HNS3_MAC_CFG_FEC_AUTO_EN_B, 1);
6197 ret = hns3_cmd_send(hw, &desc, 1);
6199 hns3_err(hw, "set fec mode failed, ret = %d", ret);
6205 get_current_speed_fec_cap(struct hns3_hw *hw, struct rte_eth_fec_capa *fec_capa)
6207 struct hns3_mac *mac = &hw->mac;
6210 switch (mac->link_speed) {
6211 case ETH_SPEED_NUM_10G:
6212 cur_capa = fec_capa[1].capa;
6214 case ETH_SPEED_NUM_25G:
6215 case ETH_SPEED_NUM_100G:
6216 case ETH_SPEED_NUM_200G:
6217 cur_capa = fec_capa[0].capa;
6228 is_fec_mode_one_bit_set(uint32_t mode)
6233 for (i = 0; i < sizeof(mode); i++)
6234 if (mode >> i & 0x1)
6237 return cnt == 1 ? true : false;
6241 hns3_fec_set(struct rte_eth_dev *dev, uint32_t mode)
6243 #define FEC_CAPA_NUM 2
6244 struct hns3_adapter *hns = dev->data->dev_private;
6245 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(hns);
6246 struct hns3_pf *pf = &hns->pf;
6248 struct rte_eth_fec_capa fec_capa[FEC_CAPA_NUM];
6250 uint32_t num = FEC_CAPA_NUM;
6253 ret = hns3_fec_get_capability(dev, fec_capa, num);
6257 /* HNS3 PMD driver only support one bit set mode, e.g. 0x1, 0x4 */
6258 if (!is_fec_mode_one_bit_set(mode))
6259 hns3_err(hw, "FEC mode(0x%x) not supported in HNS3 PMD,"
6260 "FEC mode should be only one bit set", mode);
6263 * Check whether the configured mode is within the FEC capability.
6264 * If not, the configured mode will not be supported.
6266 cur_capa = get_current_speed_fec_cap(hw, fec_capa);
6267 if (!(cur_capa & mode)) {
6268 hns3_err(hw, "unsupported FEC mode = 0x%x", mode);
6272 ret = hns3_set_fec_hw(hw, mode);
6276 pf->fec_mode = mode;
6281 hns3_restore_fec(struct hns3_hw *hw)
6283 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
6284 struct hns3_pf *pf = &hns->pf;
6285 uint32_t mode = pf->fec_mode;
6288 ret = hns3_set_fec_hw(hw, mode);
6290 hns3_err(hw, "restore fec mode(0x%x) failed, ret = %d",
6297 hns3_query_dev_fec_info(struct hns3_hw *hw)
6299 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
6300 struct hns3_pf *pf = HNS3_DEV_PRIVATE_TO_PF(hns);
6303 ret = hns3_fec_get_internal(hw, &pf->fec_mode);
6305 hns3_err(hw, "query device FEC info failed, ret = %d", ret);
6311 hns3_optical_module_existed(struct hns3_hw *hw)
6313 struct hns3_cmd_desc desc;
6317 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_GET_SFP_EXIST, true);
6318 ret = hns3_cmd_send(hw, &desc, 1);
6321 "fail to get optical module exist state, ret = %d.\n",
6325 existed = !!desc.data[0];
6331 hns3_get_module_eeprom_data(struct hns3_hw *hw, uint32_t offset,
6332 uint32_t len, uint8_t *data)
6334 #define HNS3_SFP_INFO_CMD_NUM 6
6335 #define HNS3_SFP_INFO_MAX_LEN \
6336 (HNS3_SFP_INFO_BD0_LEN + \
6337 (HNS3_SFP_INFO_CMD_NUM - 1) * HNS3_SFP_INFO_BDX_LEN)
6338 struct hns3_cmd_desc desc[HNS3_SFP_INFO_CMD_NUM];
6339 struct hns3_sfp_info_bd0_cmd *sfp_info_bd0;
6345 for (i = 0; i < HNS3_SFP_INFO_CMD_NUM; i++) {
6346 hns3_cmd_setup_basic_desc(&desc[i], HNS3_OPC_GET_SFP_EEPROM,
6348 if (i < HNS3_SFP_INFO_CMD_NUM - 1)
6349 desc[i].flag |= rte_cpu_to_le_16(HNS3_CMD_FLAG_NEXT);
6352 sfp_info_bd0 = (struct hns3_sfp_info_bd0_cmd *)desc[0].data;
6353 sfp_info_bd0->offset = rte_cpu_to_le_16((uint16_t)offset);
6354 read_len = RTE_MIN(len, HNS3_SFP_INFO_MAX_LEN);
6355 sfp_info_bd0->read_len = rte_cpu_to_le_16((uint16_t)read_len);
6357 ret = hns3_cmd_send(hw, desc, HNS3_SFP_INFO_CMD_NUM);
6359 hns3_err(hw, "fail to get module EEPROM info, ret = %d.\n",
6364 /* The data format in BD0 is different with the others. */
6365 copy_len = RTE_MIN(len, HNS3_SFP_INFO_BD0_LEN);
6366 memcpy(data, sfp_info_bd0->data, copy_len);
6367 read_len = copy_len;
6369 for (i = 1; i < HNS3_SFP_INFO_CMD_NUM; i++) {
6370 if (read_len >= len)
6373 copy_len = RTE_MIN(len - read_len, HNS3_SFP_INFO_BDX_LEN);
6374 memcpy(data + read_len, desc[i].data, copy_len);
6375 read_len += copy_len;
6378 return (int)read_len;
6382 hns3_get_module_eeprom(struct rte_eth_dev *dev,
6383 struct rte_dev_eeprom_info *info)
6385 struct hns3_adapter *hns = dev->data->dev_private;
6386 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(hns);
6387 uint32_t offset = info->offset;
6388 uint32_t len = info->length;
6389 uint8_t *data = info->data;
6390 uint32_t read_len = 0;
6392 if (hw->mac.media_type != HNS3_MEDIA_TYPE_FIBER)
6395 if (!hns3_optical_module_existed(hw)) {
6396 hns3_err(hw, "fail to read module EEPROM: no module is connected.\n");
6400 while (read_len < len) {
6402 ret = hns3_get_module_eeprom_data(hw, offset + read_len,
6414 hns3_get_module_info(struct rte_eth_dev *dev,
6415 struct rte_eth_dev_module_info *modinfo)
6417 #define HNS3_SFF8024_ID_SFP 0x03
6418 #define HNS3_SFF8024_ID_QSFP_8438 0x0c
6419 #define HNS3_SFF8024_ID_QSFP_8436_8636 0x0d
6420 #define HNS3_SFF8024_ID_QSFP28_8636 0x11
6421 #define HNS3_SFF_8636_V1_3 0x03
6422 struct hns3_adapter *hns = dev->data->dev_private;
6423 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(hns);
6424 struct rte_dev_eeprom_info info;
6425 struct hns3_sfp_type sfp_type;
6428 memset(&sfp_type, 0, sizeof(sfp_type));
6429 memset(&info, 0, sizeof(info));
6430 info.data = (uint8_t *)&sfp_type;
6431 info.length = sizeof(sfp_type);
6432 ret = hns3_get_module_eeprom(dev, &info);
6436 switch (sfp_type.type) {
6437 case HNS3_SFF8024_ID_SFP:
6438 modinfo->type = RTE_ETH_MODULE_SFF_8472;
6439 modinfo->eeprom_len = RTE_ETH_MODULE_SFF_8472_LEN;
6441 case HNS3_SFF8024_ID_QSFP_8438:
6442 modinfo->type = RTE_ETH_MODULE_SFF_8436;
6443 modinfo->eeprom_len = RTE_ETH_MODULE_SFF_8436_MAX_LEN;
6445 case HNS3_SFF8024_ID_QSFP_8436_8636:
6446 if (sfp_type.ext_type < HNS3_SFF_8636_V1_3) {
6447 modinfo->type = RTE_ETH_MODULE_SFF_8436;
6448 modinfo->eeprom_len = RTE_ETH_MODULE_SFF_8436_MAX_LEN;
6450 modinfo->type = RTE_ETH_MODULE_SFF_8636;
6451 modinfo->eeprom_len = RTE_ETH_MODULE_SFF_8636_MAX_LEN;
6454 case HNS3_SFF8024_ID_QSFP28_8636:
6455 modinfo->type = RTE_ETH_MODULE_SFF_8636;
6456 modinfo->eeprom_len = RTE_ETH_MODULE_SFF_8636_MAX_LEN;
6459 hns3_err(hw, "unknown module, type = %u, extra_type = %u.\n",
6460 sfp_type.type, sfp_type.ext_type);
6467 static const struct eth_dev_ops hns3_eth_dev_ops = {
6468 .dev_configure = hns3_dev_configure,
6469 .dev_start = hns3_dev_start,
6470 .dev_stop = hns3_dev_stop,
6471 .dev_close = hns3_dev_close,
6472 .promiscuous_enable = hns3_dev_promiscuous_enable,
6473 .promiscuous_disable = hns3_dev_promiscuous_disable,
6474 .allmulticast_enable = hns3_dev_allmulticast_enable,
6475 .allmulticast_disable = hns3_dev_allmulticast_disable,
6476 .mtu_set = hns3_dev_mtu_set,
6477 .stats_get = hns3_stats_get,
6478 .stats_reset = hns3_stats_reset,
6479 .xstats_get = hns3_dev_xstats_get,
6480 .xstats_get_names = hns3_dev_xstats_get_names,
6481 .xstats_reset = hns3_dev_xstats_reset,
6482 .xstats_get_by_id = hns3_dev_xstats_get_by_id,
6483 .xstats_get_names_by_id = hns3_dev_xstats_get_names_by_id,
6484 .dev_infos_get = hns3_dev_infos_get,
6485 .fw_version_get = hns3_fw_version_get,
6486 .rx_queue_setup = hns3_rx_queue_setup,
6487 .tx_queue_setup = hns3_tx_queue_setup,
6488 .rx_queue_release = hns3_dev_rx_queue_release,
6489 .tx_queue_release = hns3_dev_tx_queue_release,
6490 .rx_queue_start = hns3_dev_rx_queue_start,
6491 .rx_queue_stop = hns3_dev_rx_queue_stop,
6492 .tx_queue_start = hns3_dev_tx_queue_start,
6493 .tx_queue_stop = hns3_dev_tx_queue_stop,
6494 .rx_queue_intr_enable = hns3_dev_rx_queue_intr_enable,
6495 .rx_queue_intr_disable = hns3_dev_rx_queue_intr_disable,
6496 .rxq_info_get = hns3_rxq_info_get,
6497 .txq_info_get = hns3_txq_info_get,
6498 .rx_burst_mode_get = hns3_rx_burst_mode_get,
6499 .tx_burst_mode_get = hns3_tx_burst_mode_get,
6500 .flow_ctrl_get = hns3_flow_ctrl_get,
6501 .flow_ctrl_set = hns3_flow_ctrl_set,
6502 .priority_flow_ctrl_set = hns3_priority_flow_ctrl_set,
6503 .mac_addr_add = hns3_add_mac_addr,
6504 .mac_addr_remove = hns3_remove_mac_addr,
6505 .mac_addr_set = hns3_set_default_mac_addr,
6506 .set_mc_addr_list = hns3_set_mc_mac_addr_list,
6507 .link_update = hns3_dev_link_update,
6508 .rss_hash_update = hns3_dev_rss_hash_update,
6509 .rss_hash_conf_get = hns3_dev_rss_hash_conf_get,
6510 .reta_update = hns3_dev_rss_reta_update,
6511 .reta_query = hns3_dev_rss_reta_query,
6512 .filter_ctrl = hns3_dev_filter_ctrl,
6513 .vlan_filter_set = hns3_vlan_filter_set,
6514 .vlan_tpid_set = hns3_vlan_tpid_set,
6515 .vlan_offload_set = hns3_vlan_offload_set,
6516 .vlan_pvid_set = hns3_vlan_pvid_set,
6517 .get_reg = hns3_get_regs,
6518 .get_module_info = hns3_get_module_info,
6519 .get_module_eeprom = hns3_get_module_eeprom,
6520 .get_dcb_info = hns3_get_dcb_info,
6521 .dev_supported_ptypes_get = hns3_dev_supported_ptypes_get,
6522 .fec_get_capability = hns3_fec_get_capability,
6523 .fec_get = hns3_fec_get,
6524 .fec_set = hns3_fec_set,
6525 .tm_ops_get = hns3_tm_ops_get,
6526 .tx_done_cleanup = hns3_tx_done_cleanup,
6529 static const struct hns3_reset_ops hns3_reset_ops = {
6530 .reset_service = hns3_reset_service,
6531 .stop_service = hns3_stop_service,
6532 .prepare_reset = hns3_prepare_reset,
6533 .wait_hardware_ready = hns3_wait_hardware_ready,
6534 .reinit_dev = hns3_reinit_dev,
6535 .restore_conf = hns3_restore_conf,
6536 .start_service = hns3_start_service,
6540 hns3_dev_init(struct rte_eth_dev *eth_dev)
6542 struct hns3_adapter *hns = eth_dev->data->dev_private;
6543 char mac_str[RTE_ETHER_ADDR_FMT_SIZE];
6544 struct rte_ether_addr *eth_addr;
6545 struct hns3_hw *hw = &hns->hw;
6548 PMD_INIT_FUNC_TRACE();
6550 eth_dev->process_private = (struct hns3_process_private *)
6551 rte_zmalloc_socket("hns3_filter_list",
6552 sizeof(struct hns3_process_private),
6553 RTE_CACHE_LINE_SIZE, eth_dev->device->numa_node);
6554 if (eth_dev->process_private == NULL) {
6555 PMD_INIT_LOG(ERR, "Failed to alloc memory for process private");
6558 /* initialize flow filter lists */
6559 hns3_filterlist_init(eth_dev);
6561 hns3_set_rxtx_function(eth_dev);
6562 eth_dev->dev_ops = &hns3_eth_dev_ops;
6563 eth_dev->rx_queue_count = hns3_rx_queue_count;
6564 if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
6565 ret = hns3_mp_init_secondary();
6567 PMD_INIT_LOG(ERR, "Failed to init for secondary "
6568 "process, ret = %d", ret);
6569 goto err_mp_init_secondary;
6572 hw->secondary_cnt++;
6576 ret = hns3_mp_init_primary();
6579 "Failed to init for primary process, ret = %d",
6581 goto err_mp_init_primary;
6584 hw->adapter_state = HNS3_NIC_UNINITIALIZED;
6586 hw->data = eth_dev->data;
6589 * Set default max packet size according to the mtu
6590 * default vale in DPDK frame.
6592 hns->pf.mps = hw->data->mtu + HNS3_ETH_OVERHEAD;
6594 ret = hns3_reset_init(hw);
6596 goto err_init_reset;
6597 hw->reset.ops = &hns3_reset_ops;
6599 ret = hns3_init_pf(eth_dev);
6601 PMD_INIT_LOG(ERR, "Failed to init pf: %d", ret);
6605 /* Allocate memory for storing MAC addresses */
6606 eth_dev->data->mac_addrs = rte_zmalloc("hns3-mac",
6607 sizeof(struct rte_ether_addr) *
6608 HNS3_UC_MACADDR_NUM, 0);
6609 if (eth_dev->data->mac_addrs == NULL) {
6610 PMD_INIT_LOG(ERR, "Failed to allocate %zx bytes needed "
6611 "to store MAC addresses",
6612 sizeof(struct rte_ether_addr) *
6613 HNS3_UC_MACADDR_NUM);
6615 goto err_rte_zmalloc;
6618 eth_addr = (struct rte_ether_addr *)hw->mac.mac_addr;
6619 if (!rte_is_valid_assigned_ether_addr(eth_addr)) {
6620 rte_eth_random_addr(hw->mac.mac_addr);
6621 hns3_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
6622 (struct rte_ether_addr *)hw->mac.mac_addr);
6623 hns3_warn(hw, "default mac_addr from firmware is an invalid "
6624 "unicast address, using random MAC address %s",
6627 rte_ether_addr_copy((struct rte_ether_addr *)hw->mac.mac_addr,
6628 ð_dev->data->mac_addrs[0]);
6630 hw->adapter_state = HNS3_NIC_INITIALIZED;
6632 if (__atomic_load_n(&hw->reset.schedule, __ATOMIC_RELAXED) ==
6634 hns3_err(hw, "Reschedule reset service after dev_init");
6635 hns3_schedule_reset(hns);
6637 /* IMP will wait ready flag before reset */
6638 hns3_notify_reset_ready(hw, false);
6641 hns3_info(hw, "hns3 dev initialization successful!");
6645 hns3_uninit_pf(eth_dev);
6648 rte_free(hw->reset.wait_data);
6651 hns3_mp_uninit_primary();
6653 err_mp_init_primary:
6654 err_mp_init_secondary:
6655 eth_dev->dev_ops = NULL;
6656 eth_dev->rx_pkt_burst = NULL;
6657 eth_dev->tx_pkt_burst = NULL;
6658 eth_dev->tx_pkt_prepare = NULL;
6659 rte_free(eth_dev->process_private);
6660 eth_dev->process_private = NULL;
6665 hns3_dev_uninit(struct rte_eth_dev *eth_dev)
6667 struct hns3_adapter *hns = eth_dev->data->dev_private;
6668 struct hns3_hw *hw = &hns->hw;
6670 PMD_INIT_FUNC_TRACE();
6672 if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
6673 rte_free(eth_dev->process_private);
6674 eth_dev->process_private = NULL;
6678 if (hw->adapter_state < HNS3_NIC_CLOSING)
6679 hns3_dev_close(eth_dev);
6681 hw->adapter_state = HNS3_NIC_REMOVED;
6686 eth_hns3_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
6687 struct rte_pci_device *pci_dev)
6689 return rte_eth_dev_pci_generic_probe(pci_dev,
6690 sizeof(struct hns3_adapter),
6695 eth_hns3_pci_remove(struct rte_pci_device *pci_dev)
6697 return rte_eth_dev_pci_generic_remove(pci_dev, hns3_dev_uninit);
6700 static const struct rte_pci_id pci_id_hns3_map[] = {
6701 { RTE_PCI_DEVICE(PCI_VENDOR_ID_HUAWEI, HNS3_DEV_ID_GE) },
6702 { RTE_PCI_DEVICE(PCI_VENDOR_ID_HUAWEI, HNS3_DEV_ID_25GE) },
6703 { RTE_PCI_DEVICE(PCI_VENDOR_ID_HUAWEI, HNS3_DEV_ID_25GE_RDMA) },
6704 { RTE_PCI_DEVICE(PCI_VENDOR_ID_HUAWEI, HNS3_DEV_ID_50GE_RDMA) },
6705 { RTE_PCI_DEVICE(PCI_VENDOR_ID_HUAWEI, HNS3_DEV_ID_100G_RDMA_MACSEC) },
6706 { RTE_PCI_DEVICE(PCI_VENDOR_ID_HUAWEI, HNS3_DEV_ID_200G_RDMA) },
6707 { .vendor_id = 0, }, /* sentinel */
6710 static struct rte_pci_driver rte_hns3_pmd = {
6711 .id_table = pci_id_hns3_map,
6712 .drv_flags = RTE_PCI_DRV_NEED_MAPPING,
6713 .probe = eth_hns3_pci_probe,
6714 .remove = eth_hns3_pci_remove,
6717 RTE_PMD_REGISTER_PCI(net_hns3, rte_hns3_pmd);
6718 RTE_PMD_REGISTER_PCI_TABLE(net_hns3, pci_id_hns3_map);
6719 RTE_PMD_REGISTER_KMOD_DEP(net_hns3, "* igb_uio | vfio-pci");
6720 RTE_LOG_REGISTER(hns3_logtype_init, pmd.net.hns3.init, NOTICE);
6721 RTE_LOG_REGISTER(hns3_logtype_driver, pmd.net.hns3.driver, NOTICE);