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);
106 void hns3_ether_format_addr(char *buf, uint16_t size,
107 const struct rte_ether_addr *ether_addr)
109 snprintf(buf, size, "%02X:**:**:**:%02X:%02X",
110 ether_addr->addr_bytes[0],
111 ether_addr->addr_bytes[4],
112 ether_addr->addr_bytes[5]);
116 hns3_pf_disable_irq0(struct hns3_hw *hw)
118 hns3_write_dev(hw, HNS3_MISC_VECTOR_REG_BASE, 0);
122 hns3_pf_enable_irq0(struct hns3_hw *hw)
124 hns3_write_dev(hw, HNS3_MISC_VECTOR_REG_BASE, 1);
127 static enum hns3_evt_cause
128 hns3_proc_imp_reset_event(struct hns3_adapter *hns, bool is_delay,
131 struct hns3_hw *hw = &hns->hw;
133 __atomic_store_n(&hw->reset.disable_cmd, 1, __ATOMIC_RELAXED);
134 hns3_atomic_set_bit(HNS3_IMP_RESET, &hw->reset.pending);
135 *vec_val = BIT(HNS3_VECTOR0_IMPRESET_INT_B);
137 hw->reset.stats.imp_cnt++;
138 hns3_warn(hw, "IMP reset detected, clear reset status");
140 hns3_schedule_delayed_reset(hns);
141 hns3_warn(hw, "IMP reset detected, don't clear reset status");
144 return HNS3_VECTOR0_EVENT_RST;
147 static enum hns3_evt_cause
148 hns3_proc_global_reset_event(struct hns3_adapter *hns, bool is_delay,
151 struct hns3_hw *hw = &hns->hw;
153 __atomic_store_n(&hw->reset.disable_cmd, 1, __ATOMIC_RELAXED);
154 hns3_atomic_set_bit(HNS3_GLOBAL_RESET, &hw->reset.pending);
155 *vec_val = BIT(HNS3_VECTOR0_GLOBALRESET_INT_B);
157 hw->reset.stats.global_cnt++;
158 hns3_warn(hw, "Global reset detected, clear reset status");
160 hns3_schedule_delayed_reset(hns);
162 "Global reset detected, don't clear reset status");
165 return HNS3_VECTOR0_EVENT_RST;
168 static enum hns3_evt_cause
169 hns3_check_event_cause(struct hns3_adapter *hns, uint32_t *clearval)
171 struct hns3_hw *hw = &hns->hw;
172 uint32_t vector0_int_stats;
173 uint32_t cmdq_src_val;
174 uint32_t hw_err_src_reg;
176 enum hns3_evt_cause ret;
179 /* fetch the events from their corresponding regs */
180 vector0_int_stats = hns3_read_dev(hw, HNS3_VECTOR0_OTHER_INT_STS_REG);
181 cmdq_src_val = hns3_read_dev(hw, HNS3_VECTOR0_CMDQ_SRC_REG);
182 hw_err_src_reg = hns3_read_dev(hw, HNS3_RAS_PF_OTHER_INT_STS_REG);
184 is_delay = clearval == NULL ? true : false;
186 * Assumption: If by any chance reset and mailbox events are reported
187 * together then we will only process reset event and defer the
188 * processing of the mailbox events. Since, we would have not cleared
189 * RX CMDQ event this time we would receive again another interrupt
190 * from H/W just for the mailbox.
192 if (BIT(HNS3_VECTOR0_IMPRESET_INT_B) & vector0_int_stats) { /* IMP */
193 ret = hns3_proc_imp_reset_event(hns, is_delay, &val);
198 if (BIT(HNS3_VECTOR0_GLOBALRESET_INT_B) & vector0_int_stats) {
199 ret = hns3_proc_global_reset_event(hns, is_delay, &val);
203 /* check for vector0 msix event source */
204 if (vector0_int_stats & HNS3_VECTOR0_REG_MSIX_MASK ||
205 hw_err_src_reg & HNS3_RAS_REG_NFE_MASK) {
206 val = vector0_int_stats | hw_err_src_reg;
207 ret = HNS3_VECTOR0_EVENT_ERR;
211 /* check for vector0 mailbox(=CMDQ RX) event source */
212 if (BIT(HNS3_VECTOR0_RX_CMDQ_INT_B) & cmdq_src_val) {
213 cmdq_src_val &= ~BIT(HNS3_VECTOR0_RX_CMDQ_INT_B);
215 ret = HNS3_VECTOR0_EVENT_MBX;
219 if (clearval && (vector0_int_stats || cmdq_src_val || hw_err_src_reg))
220 hns3_warn(hw, "vector0_int_stats:0x%x cmdq_src_val:0x%x hw_err_src_reg:0x%x",
221 vector0_int_stats, cmdq_src_val, hw_err_src_reg);
222 val = vector0_int_stats;
223 ret = HNS3_VECTOR0_EVENT_OTHER;
232 hns3_clear_event_cause(struct hns3_hw *hw, uint32_t event_type, uint32_t regclr)
234 if (event_type == HNS3_VECTOR0_EVENT_RST)
235 hns3_write_dev(hw, HNS3_MISC_RESET_STS_REG, regclr);
236 else if (event_type == HNS3_VECTOR0_EVENT_MBX)
237 hns3_write_dev(hw, HNS3_VECTOR0_CMDQ_SRC_REG, regclr);
241 hns3_clear_all_event_cause(struct hns3_hw *hw)
243 uint32_t vector0_int_stats;
244 vector0_int_stats = hns3_read_dev(hw, HNS3_VECTOR0_OTHER_INT_STS_REG);
246 if (BIT(HNS3_VECTOR0_IMPRESET_INT_B) & vector0_int_stats)
247 hns3_warn(hw, "Probe during IMP reset interrupt");
249 if (BIT(HNS3_VECTOR0_GLOBALRESET_INT_B) & vector0_int_stats)
250 hns3_warn(hw, "Probe during Global reset interrupt");
252 hns3_clear_event_cause(hw, HNS3_VECTOR0_EVENT_RST,
253 BIT(HNS3_VECTOR0_IMPRESET_INT_B) |
254 BIT(HNS3_VECTOR0_GLOBALRESET_INT_B) |
255 BIT(HNS3_VECTOR0_CORERESET_INT_B));
256 hns3_clear_event_cause(hw, HNS3_VECTOR0_EVENT_MBX, 0);
260 hns3_interrupt_handler(void *param)
262 struct rte_eth_dev *dev = (struct rte_eth_dev *)param;
263 struct hns3_adapter *hns = dev->data->dev_private;
264 struct hns3_hw *hw = &hns->hw;
265 enum hns3_evt_cause event_cause;
266 uint32_t clearval = 0;
268 /* Disable interrupt */
269 hns3_pf_disable_irq0(hw);
271 event_cause = hns3_check_event_cause(hns, &clearval);
272 /* vector 0 interrupt is shared with reset and mailbox source events. */
273 if (event_cause == HNS3_VECTOR0_EVENT_ERR) {
274 hns3_warn(hw, "Received err interrupt");
275 hns3_handle_msix_error(hns, &hw->reset.request);
276 hns3_handle_ras_error(hns, &hw->reset.request);
277 hns3_schedule_reset(hns);
278 } else if (event_cause == HNS3_VECTOR0_EVENT_RST) {
279 hns3_warn(hw, "Received reset interrupt");
280 hns3_schedule_reset(hns);
281 } else if (event_cause == HNS3_VECTOR0_EVENT_MBX)
282 hns3_dev_handle_mbx_msg(hw);
284 hns3_err(hw, "Received unknown event");
286 hns3_clear_event_cause(hw, event_cause, clearval);
287 /* Enable interrupt if it is not cause by reset */
288 hns3_pf_enable_irq0(hw);
292 hns3_set_port_vlan_filter(struct hns3_adapter *hns, uint16_t vlan_id, int on)
294 #define HNS3_VLAN_ID_OFFSET_STEP 160
295 #define HNS3_VLAN_BYTE_SIZE 8
296 struct hns3_vlan_filter_pf_cfg_cmd *req;
297 struct hns3_hw *hw = &hns->hw;
298 uint8_t vlan_offset_byte_val;
299 struct hns3_cmd_desc desc;
300 uint8_t vlan_offset_byte;
301 uint8_t vlan_offset_base;
304 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_VLAN_FILTER_PF_CFG, false);
306 vlan_offset_base = vlan_id / HNS3_VLAN_ID_OFFSET_STEP;
307 vlan_offset_byte = (vlan_id % HNS3_VLAN_ID_OFFSET_STEP) /
309 vlan_offset_byte_val = 1 << (vlan_id % HNS3_VLAN_BYTE_SIZE);
311 req = (struct hns3_vlan_filter_pf_cfg_cmd *)desc.data;
312 req->vlan_offset = vlan_offset_base;
313 req->vlan_cfg = on ? 0 : 1;
314 req->vlan_offset_bitmap[vlan_offset_byte] = vlan_offset_byte_val;
316 ret = hns3_cmd_send(hw, &desc, 1);
318 hns3_err(hw, "set port vlan id failed, vlan_id =%u, ret =%d",
325 hns3_rm_dev_vlan_table(struct hns3_adapter *hns, uint16_t vlan_id)
327 struct hns3_user_vlan_table *vlan_entry;
328 struct hns3_pf *pf = &hns->pf;
330 LIST_FOREACH(vlan_entry, &pf->vlan_list, next) {
331 if (vlan_entry->vlan_id == vlan_id) {
332 if (vlan_entry->hd_tbl_status)
333 hns3_set_port_vlan_filter(hns, vlan_id, 0);
334 LIST_REMOVE(vlan_entry, next);
335 rte_free(vlan_entry);
342 hns3_add_dev_vlan_table(struct hns3_adapter *hns, uint16_t vlan_id,
345 struct hns3_user_vlan_table *vlan_entry;
346 struct hns3_hw *hw = &hns->hw;
347 struct hns3_pf *pf = &hns->pf;
349 LIST_FOREACH(vlan_entry, &pf->vlan_list, next) {
350 if (vlan_entry->vlan_id == vlan_id)
354 vlan_entry = rte_zmalloc("hns3_vlan_tbl", sizeof(*vlan_entry), 0);
355 if (vlan_entry == NULL) {
356 hns3_err(hw, "Failed to malloc hns3 vlan table");
360 vlan_entry->hd_tbl_status = writen_to_tbl;
361 vlan_entry->vlan_id = vlan_id;
363 LIST_INSERT_HEAD(&pf->vlan_list, vlan_entry, next);
367 hns3_restore_vlan_table(struct hns3_adapter *hns)
369 struct hns3_user_vlan_table *vlan_entry;
370 struct hns3_hw *hw = &hns->hw;
371 struct hns3_pf *pf = &hns->pf;
375 if (hw->port_base_vlan_cfg.state == HNS3_PORT_BASE_VLAN_ENABLE)
376 return hns3_vlan_pvid_configure(hns,
377 hw->port_base_vlan_cfg.pvid, 1);
379 LIST_FOREACH(vlan_entry, &pf->vlan_list, next) {
380 if (vlan_entry->hd_tbl_status) {
381 vlan_id = vlan_entry->vlan_id;
382 ret = hns3_set_port_vlan_filter(hns, vlan_id, 1);
392 hns3_vlan_filter_configure(struct hns3_adapter *hns, uint16_t vlan_id, int on)
394 struct hns3_hw *hw = &hns->hw;
395 bool writen_to_tbl = false;
399 * When vlan filter is enabled, hardware regards packets without vlan
400 * as packets with vlan 0. So, to receive packets without vlan, vlan id
401 * 0 is not allowed to be removed by rte_eth_dev_vlan_filter.
403 if (on == 0 && vlan_id == 0)
407 * When port base vlan enabled, we use port base vlan as the vlan
408 * filter condition. In this case, we don't update vlan filter table
409 * when user add new vlan or remove exist vlan, just update the
410 * vlan list. The vlan id in vlan list will be writen in vlan filter
411 * table until port base vlan disabled
413 if (hw->port_base_vlan_cfg.state == HNS3_PORT_BASE_VLAN_DISABLE) {
414 ret = hns3_set_port_vlan_filter(hns, vlan_id, on);
415 writen_to_tbl = true;
420 hns3_add_dev_vlan_table(hns, vlan_id, writen_to_tbl);
422 hns3_rm_dev_vlan_table(hns, vlan_id);
428 hns3_vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on)
430 struct hns3_adapter *hns = dev->data->dev_private;
431 struct hns3_hw *hw = &hns->hw;
434 rte_spinlock_lock(&hw->lock);
435 ret = hns3_vlan_filter_configure(hns, vlan_id, on);
436 rte_spinlock_unlock(&hw->lock);
441 hns3_vlan_tpid_configure(struct hns3_adapter *hns, enum rte_vlan_type vlan_type,
444 struct hns3_rx_vlan_type_cfg_cmd *rx_req;
445 struct hns3_tx_vlan_type_cfg_cmd *tx_req;
446 struct hns3_hw *hw = &hns->hw;
447 struct hns3_cmd_desc desc;
450 if ((vlan_type != ETH_VLAN_TYPE_INNER &&
451 vlan_type != ETH_VLAN_TYPE_OUTER)) {
452 hns3_err(hw, "Unsupported vlan type, vlan_type =%d", vlan_type);
456 if (tpid != RTE_ETHER_TYPE_VLAN) {
457 hns3_err(hw, "Unsupported vlan tpid, vlan_type =%d", vlan_type);
461 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_MAC_VLAN_TYPE_ID, false);
462 rx_req = (struct hns3_rx_vlan_type_cfg_cmd *)desc.data;
464 if (vlan_type == ETH_VLAN_TYPE_OUTER) {
465 rx_req->ot_fst_vlan_type = rte_cpu_to_le_16(tpid);
466 rx_req->ot_sec_vlan_type = rte_cpu_to_le_16(tpid);
467 } else if (vlan_type == ETH_VLAN_TYPE_INNER) {
468 rx_req->ot_fst_vlan_type = rte_cpu_to_le_16(tpid);
469 rx_req->ot_sec_vlan_type = rte_cpu_to_le_16(tpid);
470 rx_req->in_fst_vlan_type = rte_cpu_to_le_16(tpid);
471 rx_req->in_sec_vlan_type = rte_cpu_to_le_16(tpid);
474 ret = hns3_cmd_send(hw, &desc, 1);
476 hns3_err(hw, "Send rxvlan protocol type command fail, ret =%d",
481 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_MAC_VLAN_INSERT, false);
483 tx_req = (struct hns3_tx_vlan_type_cfg_cmd *)desc.data;
484 tx_req->ot_vlan_type = rte_cpu_to_le_16(tpid);
485 tx_req->in_vlan_type = rte_cpu_to_le_16(tpid);
487 ret = hns3_cmd_send(hw, &desc, 1);
489 hns3_err(hw, "Send txvlan protocol type command fail, ret =%d",
495 hns3_vlan_tpid_set(struct rte_eth_dev *dev, enum rte_vlan_type vlan_type,
498 struct hns3_adapter *hns = dev->data->dev_private;
499 struct hns3_hw *hw = &hns->hw;
502 rte_spinlock_lock(&hw->lock);
503 ret = hns3_vlan_tpid_configure(hns, vlan_type, tpid);
504 rte_spinlock_unlock(&hw->lock);
509 hns3_set_vlan_rx_offload_cfg(struct hns3_adapter *hns,
510 struct hns3_rx_vtag_cfg *vcfg)
512 struct hns3_vport_vtag_rx_cfg_cmd *req;
513 struct hns3_hw *hw = &hns->hw;
514 struct hns3_cmd_desc desc;
519 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_VLAN_PORT_RX_CFG, false);
521 req = (struct hns3_vport_vtag_rx_cfg_cmd *)desc.data;
522 hns3_set_bit(req->vport_vlan_cfg, HNS3_REM_TAG1_EN_B,
523 vcfg->strip_tag1_en ? 1 : 0);
524 hns3_set_bit(req->vport_vlan_cfg, HNS3_REM_TAG2_EN_B,
525 vcfg->strip_tag2_en ? 1 : 0);
526 hns3_set_bit(req->vport_vlan_cfg, HNS3_SHOW_TAG1_EN_B,
527 vcfg->vlan1_vlan_prionly ? 1 : 0);
528 hns3_set_bit(req->vport_vlan_cfg, HNS3_SHOW_TAG2_EN_B,
529 vcfg->vlan2_vlan_prionly ? 1 : 0);
531 /* firmwall will ignore this configuration for PCI_REVISION_ID_HIP08 */
532 hns3_set_bit(req->vport_vlan_cfg, HNS3_DISCARD_TAG1_EN_B,
533 vcfg->strip_tag1_discard_en ? 1 : 0);
534 hns3_set_bit(req->vport_vlan_cfg, HNS3_DISCARD_TAG2_EN_B,
535 vcfg->strip_tag2_discard_en ? 1 : 0);
537 * In current version VF is not supported when PF is driven by DPDK
538 * driver, just need to configure parameters for PF vport.
540 vport_id = HNS3_PF_FUNC_ID;
541 req->vf_offset = vport_id / HNS3_VF_NUM_PER_CMD;
542 bitmap = 1 << (vport_id % HNS3_VF_NUM_PER_BYTE);
543 req->vf_bitmap[req->vf_offset] = bitmap;
545 ret = hns3_cmd_send(hw, &desc, 1);
547 hns3_err(hw, "Send port rxvlan cfg command fail, ret =%d", ret);
552 hns3_update_rx_offload_cfg(struct hns3_adapter *hns,
553 struct hns3_rx_vtag_cfg *vcfg)
555 struct hns3_pf *pf = &hns->pf;
556 memcpy(&pf->vtag_config.rx_vcfg, vcfg, sizeof(pf->vtag_config.rx_vcfg));
560 hns3_update_tx_offload_cfg(struct hns3_adapter *hns,
561 struct hns3_tx_vtag_cfg *vcfg)
563 struct hns3_pf *pf = &hns->pf;
564 memcpy(&pf->vtag_config.tx_vcfg, vcfg, sizeof(pf->vtag_config.tx_vcfg));
568 hns3_en_hw_strip_rxvtag(struct hns3_adapter *hns, bool enable)
570 struct hns3_rx_vtag_cfg rxvlan_cfg;
571 struct hns3_hw *hw = &hns->hw;
574 if (hw->port_base_vlan_cfg.state == HNS3_PORT_BASE_VLAN_DISABLE) {
575 rxvlan_cfg.strip_tag1_en = false;
576 rxvlan_cfg.strip_tag2_en = enable;
577 rxvlan_cfg.strip_tag2_discard_en = false;
579 rxvlan_cfg.strip_tag1_en = enable;
580 rxvlan_cfg.strip_tag2_en = true;
581 rxvlan_cfg.strip_tag2_discard_en = true;
584 rxvlan_cfg.strip_tag1_discard_en = false;
585 rxvlan_cfg.vlan1_vlan_prionly = false;
586 rxvlan_cfg.vlan2_vlan_prionly = false;
587 rxvlan_cfg.rx_vlan_offload_en = enable;
589 ret = hns3_set_vlan_rx_offload_cfg(hns, &rxvlan_cfg);
591 hns3_err(hw, "enable strip rx vtag failed, ret =%d", ret);
595 hns3_update_rx_offload_cfg(hns, &rxvlan_cfg);
601 hns3_set_vlan_filter_ctrl(struct hns3_hw *hw, uint8_t vlan_type,
602 uint8_t fe_type, bool filter_en, uint8_t vf_id)
604 struct hns3_vlan_filter_ctrl_cmd *req;
605 struct hns3_cmd_desc desc;
608 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_VLAN_FILTER_CTRL, false);
610 req = (struct hns3_vlan_filter_ctrl_cmd *)desc.data;
611 req->vlan_type = vlan_type;
612 req->vlan_fe = filter_en ? fe_type : 0;
615 ret = hns3_cmd_send(hw, &desc, 1);
617 hns3_err(hw, "set vlan filter fail, ret =%d", ret);
623 hns3_vlan_filter_init(struct hns3_adapter *hns)
625 struct hns3_hw *hw = &hns->hw;
628 ret = hns3_set_vlan_filter_ctrl(hw, HNS3_FILTER_TYPE_VF,
629 HNS3_FILTER_FE_EGRESS, false,
632 hns3_err(hw, "failed to init vf vlan filter, ret = %d", ret);
636 ret = hns3_set_vlan_filter_ctrl(hw, HNS3_FILTER_TYPE_PORT,
637 HNS3_FILTER_FE_INGRESS, false,
640 hns3_err(hw, "failed to init port vlan filter, ret = %d", ret);
646 hns3_enable_vlan_filter(struct hns3_adapter *hns, bool enable)
648 struct hns3_hw *hw = &hns->hw;
651 ret = hns3_set_vlan_filter_ctrl(hw, HNS3_FILTER_TYPE_PORT,
652 HNS3_FILTER_FE_INGRESS, enable,
655 hns3_err(hw, "failed to %s port vlan filter, ret = %d",
656 enable ? "enable" : "disable", ret);
662 hns3_vlan_offload_set(struct rte_eth_dev *dev, int mask)
664 struct hns3_adapter *hns = dev->data->dev_private;
665 struct hns3_hw *hw = &hns->hw;
666 struct rte_eth_rxmode *rxmode;
667 unsigned int tmp_mask;
671 rte_spinlock_lock(&hw->lock);
672 rxmode = &dev->data->dev_conf.rxmode;
673 tmp_mask = (unsigned int)mask;
674 if (tmp_mask & ETH_VLAN_FILTER_MASK) {
675 /* ignore vlan filter configuration during promiscuous mode */
676 if (!dev->data->promiscuous) {
677 /* Enable or disable VLAN filter */
678 enable = rxmode->offloads & DEV_RX_OFFLOAD_VLAN_FILTER ?
681 ret = hns3_enable_vlan_filter(hns, enable);
683 rte_spinlock_unlock(&hw->lock);
684 hns3_err(hw, "failed to %s rx filter, ret = %d",
685 enable ? "enable" : "disable", ret);
691 if (tmp_mask & ETH_VLAN_STRIP_MASK) {
692 /* Enable or disable VLAN stripping */
693 enable = rxmode->offloads & DEV_RX_OFFLOAD_VLAN_STRIP ?
696 ret = hns3_en_hw_strip_rxvtag(hns, enable);
698 rte_spinlock_unlock(&hw->lock);
699 hns3_err(hw, "failed to %s rx strip, ret = %d",
700 enable ? "enable" : "disable", ret);
705 rte_spinlock_unlock(&hw->lock);
711 hns3_set_vlan_tx_offload_cfg(struct hns3_adapter *hns,
712 struct hns3_tx_vtag_cfg *vcfg)
714 struct hns3_vport_vtag_tx_cfg_cmd *req;
715 struct hns3_cmd_desc desc;
716 struct hns3_hw *hw = &hns->hw;
721 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_VLAN_PORT_TX_CFG, false);
723 req = (struct hns3_vport_vtag_tx_cfg_cmd *)desc.data;
724 req->def_vlan_tag1 = vcfg->default_tag1;
725 req->def_vlan_tag2 = vcfg->default_tag2;
726 hns3_set_bit(req->vport_vlan_cfg, HNS3_ACCEPT_TAG1_B,
727 vcfg->accept_tag1 ? 1 : 0);
728 hns3_set_bit(req->vport_vlan_cfg, HNS3_ACCEPT_UNTAG1_B,
729 vcfg->accept_untag1 ? 1 : 0);
730 hns3_set_bit(req->vport_vlan_cfg, HNS3_ACCEPT_TAG2_B,
731 vcfg->accept_tag2 ? 1 : 0);
732 hns3_set_bit(req->vport_vlan_cfg, HNS3_ACCEPT_UNTAG2_B,
733 vcfg->accept_untag2 ? 1 : 0);
734 hns3_set_bit(req->vport_vlan_cfg, HNS3_PORT_INS_TAG1_EN_B,
735 vcfg->insert_tag1_en ? 1 : 0);
736 hns3_set_bit(req->vport_vlan_cfg, HNS3_PORT_INS_TAG2_EN_B,
737 vcfg->insert_tag2_en ? 1 : 0);
738 hns3_set_bit(req->vport_vlan_cfg, HNS3_CFG_NIC_ROCE_SEL_B, 0);
740 /* firmwall will ignore this configuration for PCI_REVISION_ID_HIP08 */
741 hns3_set_bit(req->vport_vlan_cfg, HNS3_TAG_SHIFT_MODE_EN_B,
742 vcfg->tag_shift_mode_en ? 1 : 0);
745 * In current version VF is not supported when PF is driven by DPDK
746 * driver, just need to configure parameters for PF vport.
748 vport_id = HNS3_PF_FUNC_ID;
749 req->vf_offset = vport_id / HNS3_VF_NUM_PER_CMD;
750 bitmap = 1 << (vport_id % HNS3_VF_NUM_PER_BYTE);
751 req->vf_bitmap[req->vf_offset] = bitmap;
753 ret = hns3_cmd_send(hw, &desc, 1);
755 hns3_err(hw, "Send port txvlan cfg command fail, ret =%d", ret);
761 hns3_vlan_txvlan_cfg(struct hns3_adapter *hns, uint16_t port_base_vlan_state,
764 struct hns3_hw *hw = &hns->hw;
765 struct hns3_tx_vtag_cfg txvlan_cfg;
768 if (port_base_vlan_state == HNS3_PORT_BASE_VLAN_DISABLE) {
769 txvlan_cfg.accept_tag1 = true;
770 txvlan_cfg.insert_tag1_en = false;
771 txvlan_cfg.default_tag1 = 0;
773 txvlan_cfg.accept_tag1 =
774 hw->vlan_mode == HNS3_HW_SHIFT_AND_DISCARD_MODE;
775 txvlan_cfg.insert_tag1_en = true;
776 txvlan_cfg.default_tag1 = pvid;
779 txvlan_cfg.accept_untag1 = true;
780 txvlan_cfg.accept_tag2 = true;
781 txvlan_cfg.accept_untag2 = true;
782 txvlan_cfg.insert_tag2_en = false;
783 txvlan_cfg.default_tag2 = 0;
784 txvlan_cfg.tag_shift_mode_en = true;
786 ret = hns3_set_vlan_tx_offload_cfg(hns, &txvlan_cfg);
788 hns3_err(hw, "pf vlan set pvid failed, pvid =%u ,ret =%d", pvid,
793 hns3_update_tx_offload_cfg(hns, &txvlan_cfg);
799 hns3_rm_all_vlan_table(struct hns3_adapter *hns, bool is_del_list)
801 struct hns3_user_vlan_table *vlan_entry;
802 struct hns3_pf *pf = &hns->pf;
804 LIST_FOREACH(vlan_entry, &pf->vlan_list, next) {
805 if (vlan_entry->hd_tbl_status) {
806 hns3_set_port_vlan_filter(hns, vlan_entry->vlan_id, 0);
807 vlan_entry->hd_tbl_status = false;
812 vlan_entry = LIST_FIRST(&pf->vlan_list);
814 LIST_REMOVE(vlan_entry, next);
815 rte_free(vlan_entry);
816 vlan_entry = LIST_FIRST(&pf->vlan_list);
822 hns3_add_all_vlan_table(struct hns3_adapter *hns)
824 struct hns3_user_vlan_table *vlan_entry;
825 struct hns3_pf *pf = &hns->pf;
827 LIST_FOREACH(vlan_entry, &pf->vlan_list, next) {
828 if (!vlan_entry->hd_tbl_status) {
829 hns3_set_port_vlan_filter(hns, vlan_entry->vlan_id, 1);
830 vlan_entry->hd_tbl_status = true;
836 hns3_remove_all_vlan_table(struct hns3_adapter *hns)
838 struct hns3_hw *hw = &hns->hw;
841 hns3_rm_all_vlan_table(hns, true);
842 if (hw->port_base_vlan_cfg.pvid != HNS3_INVALID_PVID) {
843 ret = hns3_set_port_vlan_filter(hns,
844 hw->port_base_vlan_cfg.pvid, 0);
846 hns3_err(hw, "Failed to remove all vlan table, ret =%d",
854 hns3_update_vlan_filter_entries(struct hns3_adapter *hns,
855 uint16_t port_base_vlan_state, uint16_t new_pvid)
857 struct hns3_hw *hw = &hns->hw;
861 if (port_base_vlan_state == HNS3_PORT_BASE_VLAN_ENABLE) {
862 old_pvid = hw->port_base_vlan_cfg.pvid;
863 if (old_pvid != HNS3_INVALID_PVID) {
864 ret = hns3_set_port_vlan_filter(hns, old_pvid, 0);
866 hns3_err(hw, "failed to remove old pvid %u, "
867 "ret = %d", old_pvid, ret);
872 hns3_rm_all_vlan_table(hns, false);
873 ret = hns3_set_port_vlan_filter(hns, new_pvid, 1);
875 hns3_err(hw, "failed to add new pvid %u, ret = %d",
880 ret = hns3_set_port_vlan_filter(hns, new_pvid, 0);
882 hns3_err(hw, "failed to remove pvid %u, ret = %d",
887 hns3_add_all_vlan_table(hns);
893 hns3_en_pvid_strip(struct hns3_adapter *hns, int on)
895 struct hns3_rx_vtag_cfg *old_cfg = &hns->pf.vtag_config.rx_vcfg;
896 struct hns3_rx_vtag_cfg rx_vlan_cfg;
900 rx_strip_en = old_cfg->rx_vlan_offload_en;
902 rx_vlan_cfg.strip_tag1_en = rx_strip_en;
903 rx_vlan_cfg.strip_tag2_en = true;
904 rx_vlan_cfg.strip_tag2_discard_en = true;
906 rx_vlan_cfg.strip_tag1_en = false;
907 rx_vlan_cfg.strip_tag2_en = rx_strip_en;
908 rx_vlan_cfg.strip_tag2_discard_en = false;
910 rx_vlan_cfg.strip_tag1_discard_en = false;
911 rx_vlan_cfg.vlan1_vlan_prionly = false;
912 rx_vlan_cfg.vlan2_vlan_prionly = false;
913 rx_vlan_cfg.rx_vlan_offload_en = old_cfg->rx_vlan_offload_en;
915 ret = hns3_set_vlan_rx_offload_cfg(hns, &rx_vlan_cfg);
919 hns3_update_rx_offload_cfg(hns, &rx_vlan_cfg);
924 hns3_vlan_pvid_configure(struct hns3_adapter *hns, uint16_t pvid, int on)
926 struct hns3_hw *hw = &hns->hw;
927 uint16_t port_base_vlan_state;
930 if (on == 0 && pvid != hw->port_base_vlan_cfg.pvid) {
931 if (hw->port_base_vlan_cfg.pvid != HNS3_INVALID_PVID)
932 hns3_warn(hw, "Invalid operation! As current pvid set "
933 "is %u, disable pvid %u is invalid",
934 hw->port_base_vlan_cfg.pvid, pvid);
938 port_base_vlan_state = on ? HNS3_PORT_BASE_VLAN_ENABLE :
939 HNS3_PORT_BASE_VLAN_DISABLE;
940 ret = hns3_vlan_txvlan_cfg(hns, port_base_vlan_state, pvid);
942 hns3_err(hw, "failed to config tx vlan for pvid, ret = %d",
947 ret = hns3_en_pvid_strip(hns, on);
949 hns3_err(hw, "failed to config rx vlan strip for pvid, "
954 if (pvid == HNS3_INVALID_PVID)
956 ret = hns3_update_vlan_filter_entries(hns, port_base_vlan_state, pvid);
958 hns3_err(hw, "failed to update vlan filter entries, ret = %d",
964 hw->port_base_vlan_cfg.state = port_base_vlan_state;
965 hw->port_base_vlan_cfg.pvid = on ? pvid : HNS3_INVALID_PVID;
970 hns3_vlan_pvid_set(struct rte_eth_dev *dev, uint16_t pvid, int on)
972 struct hns3_adapter *hns = dev->data->dev_private;
973 struct hns3_hw *hw = &hns->hw;
974 bool pvid_en_state_change;
978 if (pvid > RTE_ETHER_MAX_VLAN_ID) {
979 hns3_err(hw, "Invalid vlan_id = %u > %d", pvid,
980 RTE_ETHER_MAX_VLAN_ID);
985 * If PVID configuration state change, should refresh the PVID
986 * configuration state in struct hns3_tx_queue/hns3_rx_queue.
988 pvid_state = hw->port_base_vlan_cfg.state;
989 if ((on && pvid_state == HNS3_PORT_BASE_VLAN_ENABLE) ||
990 (!on && pvid_state == HNS3_PORT_BASE_VLAN_DISABLE))
991 pvid_en_state_change = false;
993 pvid_en_state_change = true;
995 rte_spinlock_lock(&hw->lock);
996 ret = hns3_vlan_pvid_configure(hns, pvid, on);
997 rte_spinlock_unlock(&hw->lock);
1001 * Only in HNS3_SW_SHIFT_AND_MODE the PVID related operation in Tx/Rx
1002 * need be processed by PMD driver.
1004 if (pvid_en_state_change &&
1005 hw->vlan_mode == HNS3_SW_SHIFT_AND_DISCARD_MODE)
1006 hns3_update_all_queues_pvid_proc_en(hw);
1012 hns3_default_vlan_config(struct hns3_adapter *hns)
1014 struct hns3_hw *hw = &hns->hw;
1018 * When vlan filter is enabled, hardware regards packets without vlan
1019 * as packets with vlan 0. Therefore, if vlan 0 is not in the vlan
1020 * table, packets without vlan won't be received. So, add vlan 0 as
1023 ret = hns3_vlan_filter_configure(hns, 0, 1);
1025 hns3_err(hw, "default vlan 0 config failed, ret =%d", ret);
1030 hns3_init_vlan_config(struct hns3_adapter *hns)
1032 struct hns3_hw *hw = &hns->hw;
1036 * This function can be called in the initialization and reset process,
1037 * when in reset process, it means that hardware had been reseted
1038 * successfully and we need to restore the hardware configuration to
1039 * ensure that the hardware configuration remains unchanged before and
1042 if (__atomic_load_n(&hw->reset.resetting, __ATOMIC_RELAXED) == 0) {
1043 hw->port_base_vlan_cfg.state = HNS3_PORT_BASE_VLAN_DISABLE;
1044 hw->port_base_vlan_cfg.pvid = HNS3_INVALID_PVID;
1047 ret = hns3_vlan_filter_init(hns);
1049 hns3_err(hw, "vlan init fail in pf, ret =%d", ret);
1053 ret = hns3_vlan_tpid_configure(hns, ETH_VLAN_TYPE_INNER,
1054 RTE_ETHER_TYPE_VLAN);
1056 hns3_err(hw, "tpid set fail in pf, ret =%d", ret);
1061 * When in the reinit dev stage of the reset process, the following
1062 * vlan-related configurations may differ from those at initialization,
1063 * we will restore configurations to hardware in hns3_restore_vlan_table
1064 * and hns3_restore_vlan_conf later.
1066 if (__atomic_load_n(&hw->reset.resetting, __ATOMIC_RELAXED) == 0) {
1067 ret = hns3_vlan_pvid_configure(hns, HNS3_INVALID_PVID, 0);
1069 hns3_err(hw, "pvid set fail in pf, ret =%d", ret);
1073 ret = hns3_en_hw_strip_rxvtag(hns, false);
1075 hns3_err(hw, "rx strip configure fail in pf, ret =%d",
1081 return hns3_default_vlan_config(hns);
1085 hns3_restore_vlan_conf(struct hns3_adapter *hns)
1087 struct hns3_pf *pf = &hns->pf;
1088 struct hns3_hw *hw = &hns->hw;
1093 if (!hw->data->promiscuous) {
1094 /* restore vlan filter states */
1095 offloads = hw->data->dev_conf.rxmode.offloads;
1096 enable = offloads & DEV_RX_OFFLOAD_VLAN_FILTER ? true : false;
1097 ret = hns3_enable_vlan_filter(hns, enable);
1099 hns3_err(hw, "failed to restore vlan rx filter conf, "
1105 ret = hns3_set_vlan_rx_offload_cfg(hns, &pf->vtag_config.rx_vcfg);
1107 hns3_err(hw, "failed to restore vlan rx conf, ret = %d", ret);
1111 ret = hns3_set_vlan_tx_offload_cfg(hns, &pf->vtag_config.tx_vcfg);
1113 hns3_err(hw, "failed to restore vlan tx conf, ret = %d", ret);
1119 hns3_dev_configure_vlan(struct rte_eth_dev *dev)
1121 struct hns3_adapter *hns = dev->data->dev_private;
1122 struct rte_eth_dev_data *data = dev->data;
1123 struct rte_eth_txmode *txmode;
1124 struct hns3_hw *hw = &hns->hw;
1128 txmode = &data->dev_conf.txmode;
1129 if (txmode->hw_vlan_reject_tagged || txmode->hw_vlan_reject_untagged)
1131 "hw_vlan_reject_tagged or hw_vlan_reject_untagged "
1132 "configuration is not supported! Ignore these two "
1133 "parameters: hw_vlan_reject_tagged(%u), "
1134 "hw_vlan_reject_untagged(%u)",
1135 txmode->hw_vlan_reject_tagged,
1136 txmode->hw_vlan_reject_untagged);
1138 /* Apply vlan offload setting */
1139 mask = ETH_VLAN_STRIP_MASK | ETH_VLAN_FILTER_MASK;
1140 ret = hns3_vlan_offload_set(dev, mask);
1142 hns3_err(hw, "dev config rx vlan offload failed, ret = %d",
1148 * If pvid config is not set in rte_eth_conf, driver needn't to set
1149 * VLAN pvid related configuration to hardware.
1151 if (txmode->pvid == 0 && txmode->hw_vlan_insert_pvid == 0)
1154 /* Apply pvid setting */
1155 ret = hns3_vlan_pvid_set(dev, txmode->pvid,
1156 txmode->hw_vlan_insert_pvid);
1158 hns3_err(hw, "dev config vlan pvid(%u) failed, ret = %d",
1165 hns3_config_tso(struct hns3_hw *hw, unsigned int tso_mss_min,
1166 unsigned int tso_mss_max)
1168 struct hns3_cfg_tso_status_cmd *req;
1169 struct hns3_cmd_desc desc;
1172 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_TSO_GENERIC_CONFIG, false);
1174 req = (struct hns3_cfg_tso_status_cmd *)desc.data;
1177 hns3_set_field(tso_mss, HNS3_TSO_MSS_MIN_M, HNS3_TSO_MSS_MIN_S,
1179 req->tso_mss_min = rte_cpu_to_le_16(tso_mss);
1182 hns3_set_field(tso_mss, HNS3_TSO_MSS_MIN_M, HNS3_TSO_MSS_MIN_S,
1184 req->tso_mss_max = rte_cpu_to_le_16(tso_mss);
1186 return hns3_cmd_send(hw, &desc, 1);
1190 hns3_set_umv_space(struct hns3_hw *hw, uint16_t space_size,
1191 uint16_t *allocated_size, bool is_alloc)
1193 struct hns3_umv_spc_alc_cmd *req;
1194 struct hns3_cmd_desc desc;
1197 req = (struct hns3_umv_spc_alc_cmd *)desc.data;
1198 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_MAC_VLAN_ALLOCATE, false);
1199 hns3_set_bit(req->allocate, HNS3_UMV_SPC_ALC_B, is_alloc ? 0 : 1);
1200 req->space_size = rte_cpu_to_le_32(space_size);
1202 ret = hns3_cmd_send(hw, &desc, 1);
1204 PMD_INIT_LOG(ERR, "%s umv space failed for cmd_send, ret =%d",
1205 is_alloc ? "allocate" : "free", ret);
1209 if (is_alloc && allocated_size)
1210 *allocated_size = rte_le_to_cpu_32(desc.data[1]);
1216 hns3_init_umv_space(struct hns3_hw *hw)
1218 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
1219 struct hns3_pf *pf = &hns->pf;
1220 uint16_t allocated_size = 0;
1223 ret = hns3_set_umv_space(hw, pf->wanted_umv_size, &allocated_size,
1228 if (allocated_size < pf->wanted_umv_size)
1229 PMD_INIT_LOG(WARNING, "Alloc umv space failed, want %u, get %u",
1230 pf->wanted_umv_size, allocated_size);
1232 pf->max_umv_size = (!!allocated_size) ? allocated_size :
1233 pf->wanted_umv_size;
1234 pf->used_umv_size = 0;
1239 hns3_uninit_umv_space(struct hns3_hw *hw)
1241 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
1242 struct hns3_pf *pf = &hns->pf;
1245 if (pf->max_umv_size == 0)
1248 ret = hns3_set_umv_space(hw, pf->max_umv_size, NULL, false);
1252 pf->max_umv_size = 0;
1258 hns3_is_umv_space_full(struct hns3_hw *hw)
1260 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
1261 struct hns3_pf *pf = &hns->pf;
1264 is_full = (pf->used_umv_size >= pf->max_umv_size);
1270 hns3_update_umv_space(struct hns3_hw *hw, bool is_free)
1272 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
1273 struct hns3_pf *pf = &hns->pf;
1276 if (pf->used_umv_size > 0)
1277 pf->used_umv_size--;
1279 pf->used_umv_size++;
1283 hns3_prepare_mac_addr(struct hns3_mac_vlan_tbl_entry_cmd *new_req,
1284 const uint8_t *addr, bool is_mc)
1286 const unsigned char *mac_addr = addr;
1287 uint32_t high_val = ((uint32_t)mac_addr[3] << 24) |
1288 ((uint32_t)mac_addr[2] << 16) |
1289 ((uint32_t)mac_addr[1] << 8) |
1290 (uint32_t)mac_addr[0];
1291 uint32_t low_val = ((uint32_t)mac_addr[5] << 8) | (uint32_t)mac_addr[4];
1293 hns3_set_bit(new_req->flags, HNS3_MAC_VLAN_BIT0_EN_B, 1);
1295 hns3_set_bit(new_req->entry_type, HNS3_MAC_VLAN_BIT0_EN_B, 0);
1296 hns3_set_bit(new_req->entry_type, HNS3_MAC_VLAN_BIT1_EN_B, 1);
1297 hns3_set_bit(new_req->mc_mac_en, HNS3_MAC_VLAN_BIT0_EN_B, 1);
1300 new_req->mac_addr_hi32 = rte_cpu_to_le_32(high_val);
1301 new_req->mac_addr_lo16 = rte_cpu_to_le_16(low_val & 0xffff);
1305 hns3_get_mac_vlan_cmd_status(struct hns3_hw *hw, uint16_t cmdq_resp,
1307 enum hns3_mac_vlan_tbl_opcode op)
1310 hns3_err(hw, "cmdq execute failed for get_mac_vlan_cmd_status,status=%u",
1315 if (op == HNS3_MAC_VLAN_ADD) {
1316 if (resp_code == 0 || resp_code == 1) {
1318 } else if (resp_code == HNS3_ADD_UC_OVERFLOW) {
1319 hns3_err(hw, "add mac addr failed for uc_overflow");
1321 } else if (resp_code == HNS3_ADD_MC_OVERFLOW) {
1322 hns3_err(hw, "add mac addr failed for mc_overflow");
1326 hns3_err(hw, "add mac addr failed for undefined, code=%u",
1329 } else if (op == HNS3_MAC_VLAN_REMOVE) {
1330 if (resp_code == 0) {
1332 } else if (resp_code == 1) {
1333 hns3_dbg(hw, "remove mac addr failed for miss");
1337 hns3_err(hw, "remove mac addr failed for undefined, code=%u",
1340 } else if (op == HNS3_MAC_VLAN_LKUP) {
1341 if (resp_code == 0) {
1343 } else if (resp_code == 1) {
1344 hns3_dbg(hw, "lookup mac addr failed for miss");
1348 hns3_err(hw, "lookup mac addr failed for undefined, code=%u",
1353 hns3_err(hw, "unknown opcode for get_mac_vlan_cmd_status, opcode=%u",
1360 hns3_lookup_mac_vlan_tbl(struct hns3_hw *hw,
1361 struct hns3_mac_vlan_tbl_entry_cmd *req,
1362 struct hns3_cmd_desc *desc, bool is_mc)
1368 hns3_cmd_setup_basic_desc(&desc[0], HNS3_OPC_MAC_VLAN_ADD, true);
1370 desc[0].flag |= rte_cpu_to_le_16(HNS3_CMD_FLAG_NEXT);
1371 memcpy(desc[0].data, req,
1372 sizeof(struct hns3_mac_vlan_tbl_entry_cmd));
1373 hns3_cmd_setup_basic_desc(&desc[1], HNS3_OPC_MAC_VLAN_ADD,
1375 desc[1].flag |= rte_cpu_to_le_16(HNS3_CMD_FLAG_NEXT);
1376 hns3_cmd_setup_basic_desc(&desc[2], HNS3_OPC_MAC_VLAN_ADD,
1378 ret = hns3_cmd_send(hw, desc, HNS3_MC_MAC_VLAN_ADD_DESC_NUM);
1380 memcpy(desc[0].data, req,
1381 sizeof(struct hns3_mac_vlan_tbl_entry_cmd));
1382 ret = hns3_cmd_send(hw, desc, 1);
1385 hns3_err(hw, "lookup mac addr failed for cmd_send, ret =%d.",
1389 resp_code = (rte_le_to_cpu_32(desc[0].data[0]) >> 8) & 0xff;
1390 retval = rte_le_to_cpu_16(desc[0].retval);
1392 return hns3_get_mac_vlan_cmd_status(hw, retval, resp_code,
1393 HNS3_MAC_VLAN_LKUP);
1397 hns3_add_mac_vlan_tbl(struct hns3_hw *hw,
1398 struct hns3_mac_vlan_tbl_entry_cmd *req,
1399 struct hns3_cmd_desc *mc_desc)
1406 if (mc_desc == NULL) {
1407 struct hns3_cmd_desc desc;
1409 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_MAC_VLAN_ADD, false);
1410 memcpy(desc.data, req,
1411 sizeof(struct hns3_mac_vlan_tbl_entry_cmd));
1412 ret = hns3_cmd_send(hw, &desc, 1);
1413 resp_code = (rte_le_to_cpu_32(desc.data[0]) >> 8) & 0xff;
1414 retval = rte_le_to_cpu_16(desc.retval);
1416 cfg_status = hns3_get_mac_vlan_cmd_status(hw, retval, resp_code,
1419 hns3_cmd_reuse_desc(&mc_desc[0], false);
1420 mc_desc[0].flag |= rte_cpu_to_le_16(HNS3_CMD_FLAG_NEXT);
1421 hns3_cmd_reuse_desc(&mc_desc[1], false);
1422 mc_desc[1].flag |= rte_cpu_to_le_16(HNS3_CMD_FLAG_NEXT);
1423 hns3_cmd_reuse_desc(&mc_desc[2], false);
1424 mc_desc[2].flag &= rte_cpu_to_le_16(~HNS3_CMD_FLAG_NEXT);
1425 memcpy(mc_desc[0].data, req,
1426 sizeof(struct hns3_mac_vlan_tbl_entry_cmd));
1427 mc_desc[0].retval = 0;
1428 ret = hns3_cmd_send(hw, mc_desc, HNS3_MC_MAC_VLAN_ADD_DESC_NUM);
1429 resp_code = (rte_le_to_cpu_32(mc_desc[0].data[0]) >> 8) & 0xff;
1430 retval = rte_le_to_cpu_16(mc_desc[0].retval);
1432 cfg_status = hns3_get_mac_vlan_cmd_status(hw, retval, resp_code,
1437 hns3_err(hw, "add mac addr failed for cmd_send, ret =%d", ret);
1445 hns3_remove_mac_vlan_tbl(struct hns3_hw *hw,
1446 struct hns3_mac_vlan_tbl_entry_cmd *req)
1448 struct hns3_cmd_desc desc;
1453 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_MAC_VLAN_REMOVE, false);
1455 memcpy(desc.data, req, sizeof(struct hns3_mac_vlan_tbl_entry_cmd));
1457 ret = hns3_cmd_send(hw, &desc, 1);
1459 hns3_err(hw, "del mac addr failed for cmd_send, ret =%d", ret);
1462 resp_code = (rte_le_to_cpu_32(desc.data[0]) >> 8) & 0xff;
1463 retval = rte_le_to_cpu_16(desc.retval);
1465 return hns3_get_mac_vlan_cmd_status(hw, retval, resp_code,
1466 HNS3_MAC_VLAN_REMOVE);
1470 hns3_add_uc_addr_common(struct hns3_hw *hw, struct rte_ether_addr *mac_addr)
1472 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
1473 struct hns3_mac_vlan_tbl_entry_cmd req;
1474 struct hns3_pf *pf = &hns->pf;
1475 struct hns3_cmd_desc desc[3];
1476 char mac_str[RTE_ETHER_ADDR_FMT_SIZE];
1477 uint16_t egress_port = 0;
1481 /* check if mac addr is valid */
1482 if (!rte_is_valid_assigned_ether_addr(mac_addr)) {
1483 hns3_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
1485 hns3_err(hw, "Add unicast mac addr err! addr(%s) invalid",
1490 memset(&req, 0, sizeof(req));
1493 * In current version VF is not supported when PF is driven by DPDK
1494 * driver, just need to configure parameters for PF vport.
1496 vf_id = HNS3_PF_FUNC_ID;
1497 hns3_set_field(egress_port, HNS3_MAC_EPORT_VFID_M,
1498 HNS3_MAC_EPORT_VFID_S, vf_id);
1500 req.egress_port = rte_cpu_to_le_16(egress_port);
1502 hns3_prepare_mac_addr(&req, mac_addr->addr_bytes, false);
1505 * Lookup the mac address in the mac_vlan table, and add
1506 * it if the entry is inexistent. Repeated unicast entry
1507 * is not allowed in the mac vlan table.
1509 ret = hns3_lookup_mac_vlan_tbl(hw, &req, desc, false);
1510 if (ret == -ENOENT) {
1511 if (!hns3_is_umv_space_full(hw)) {
1512 ret = hns3_add_mac_vlan_tbl(hw, &req, NULL);
1514 hns3_update_umv_space(hw, false);
1518 hns3_err(hw, "UC MAC table full(%u)", pf->used_umv_size);
1523 hns3_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE, mac_addr);
1525 /* check if we just hit the duplicate */
1527 hns3_dbg(hw, "mac addr(%s) has been in the MAC table", mac_str);
1531 hns3_err(hw, "PF failed to add unicast entry(%s) in the MAC table",
1538 hns3_add_mc_addr_common(struct hns3_hw *hw, struct rte_ether_addr *mac_addr)
1540 char mac_str[RTE_ETHER_ADDR_FMT_SIZE];
1541 struct rte_ether_addr *addr;
1545 for (i = 0; i < hw->mc_addrs_num; i++) {
1546 addr = &hw->mc_addrs[i];
1547 /* Check if there are duplicate addresses */
1548 if (rte_is_same_ether_addr(addr, mac_addr)) {
1549 hns3_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
1551 hns3_err(hw, "failed to add mc mac addr, same addrs"
1552 "(%s) is added by the set_mc_mac_addr_list "
1558 ret = hns3_add_mc_addr(hw, mac_addr);
1560 hns3_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
1562 hns3_err(hw, "failed to add mc mac addr(%s), ret = %d",
1569 hns3_remove_mc_addr_common(struct hns3_hw *hw, struct rte_ether_addr *mac_addr)
1571 char mac_str[RTE_ETHER_ADDR_FMT_SIZE];
1574 ret = hns3_remove_mc_addr(hw, mac_addr);
1576 hns3_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
1578 hns3_err(hw, "failed to remove mc mac addr(%s), ret = %d",
1585 hns3_add_mac_addr(struct rte_eth_dev *dev, struct rte_ether_addr *mac_addr,
1586 uint32_t idx, __rte_unused uint32_t pool)
1588 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1589 char mac_str[RTE_ETHER_ADDR_FMT_SIZE];
1592 rte_spinlock_lock(&hw->lock);
1595 * In hns3 network engine adding UC and MC mac address with different
1596 * commands with firmware. We need to determine whether the input
1597 * address is a UC or a MC address to call different commands.
1598 * By the way, it is recommended calling the API function named
1599 * rte_eth_dev_set_mc_addr_list to set the MC mac address, because
1600 * using the rte_eth_dev_mac_addr_add API function to set MC mac address
1601 * may affect the specifications of UC mac addresses.
1603 if (rte_is_multicast_ether_addr(mac_addr))
1604 ret = hns3_add_mc_addr_common(hw, mac_addr);
1606 ret = hns3_add_uc_addr_common(hw, mac_addr);
1609 rte_spinlock_unlock(&hw->lock);
1610 hns3_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
1612 hns3_err(hw, "failed to add mac addr(%s), ret = %d", mac_str,
1618 hw->mac.default_addr_setted = true;
1619 rte_spinlock_unlock(&hw->lock);
1625 hns3_remove_uc_addr_common(struct hns3_hw *hw, struct rte_ether_addr *mac_addr)
1627 struct hns3_mac_vlan_tbl_entry_cmd req;
1628 char mac_str[RTE_ETHER_ADDR_FMT_SIZE];
1631 /* check if mac addr is valid */
1632 if (!rte_is_valid_assigned_ether_addr(mac_addr)) {
1633 hns3_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
1635 hns3_err(hw, "remove unicast mac addr err! addr(%s) invalid",
1640 memset(&req, 0, sizeof(req));
1641 hns3_set_bit(req.entry_type, HNS3_MAC_VLAN_BIT0_EN_B, 0);
1642 hns3_prepare_mac_addr(&req, mac_addr->addr_bytes, false);
1643 ret = hns3_remove_mac_vlan_tbl(hw, &req);
1644 if (ret == -ENOENT) /* mac addr isn't existent in the mac vlan table. */
1647 hns3_update_umv_space(hw, true);
1653 hns3_remove_mac_addr(struct rte_eth_dev *dev, uint32_t idx)
1655 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1656 /* index will be checked by upper level rte interface */
1657 struct rte_ether_addr *mac_addr = &dev->data->mac_addrs[idx];
1658 char mac_str[RTE_ETHER_ADDR_FMT_SIZE];
1661 rte_spinlock_lock(&hw->lock);
1663 if (rte_is_multicast_ether_addr(mac_addr))
1664 ret = hns3_remove_mc_addr_common(hw, mac_addr);
1666 ret = hns3_remove_uc_addr_common(hw, mac_addr);
1667 rte_spinlock_unlock(&hw->lock);
1669 hns3_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
1671 hns3_err(hw, "failed to remove mac addr(%s), ret = %d", mac_str,
1677 hns3_set_default_mac_addr(struct rte_eth_dev *dev,
1678 struct rte_ether_addr *mac_addr)
1680 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1681 struct rte_ether_addr *oaddr;
1682 char mac_str[RTE_ETHER_ADDR_FMT_SIZE];
1683 bool default_addr_setted;
1684 bool rm_succes = false;
1688 * It has been guaranteed that input parameter named mac_addr is valid
1689 * address in the rte layer of DPDK framework.
1691 oaddr = (struct rte_ether_addr *)hw->mac.mac_addr;
1692 default_addr_setted = hw->mac.default_addr_setted;
1693 if (default_addr_setted && !!rte_is_same_ether_addr(mac_addr, oaddr))
1696 rte_spinlock_lock(&hw->lock);
1697 if (default_addr_setted) {
1698 ret = hns3_remove_uc_addr_common(hw, oaddr);
1700 hns3_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
1702 hns3_warn(hw, "Remove old uc mac address(%s) fail: %d",
1709 ret = hns3_add_uc_addr_common(hw, mac_addr);
1711 hns3_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
1713 hns3_err(hw, "Failed to set mac addr(%s): %d", mac_str, ret);
1714 goto err_add_uc_addr;
1717 ret = hns3_pause_addr_cfg(hw, mac_addr->addr_bytes);
1719 hns3_err(hw, "Failed to configure mac pause address: %d", ret);
1720 goto err_pause_addr_cfg;
1723 rte_ether_addr_copy(mac_addr,
1724 (struct rte_ether_addr *)hw->mac.mac_addr);
1725 hw->mac.default_addr_setted = true;
1726 rte_spinlock_unlock(&hw->lock);
1731 ret_val = hns3_remove_uc_addr_common(hw, mac_addr);
1733 hns3_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
1736 "Failed to roll back to del setted mac addr(%s): %d",
1742 ret_val = hns3_add_uc_addr_common(hw, oaddr);
1744 hns3_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
1747 "Failed to restore old uc mac addr(%s): %d",
1749 hw->mac.default_addr_setted = false;
1752 rte_spinlock_unlock(&hw->lock);
1758 hns3_configure_all_mac_addr(struct hns3_adapter *hns, bool del)
1760 char mac_str[RTE_ETHER_ADDR_FMT_SIZE];
1761 struct hns3_hw *hw = &hns->hw;
1762 struct rte_ether_addr *addr;
1767 for (i = 0; i < HNS3_UC_MACADDR_NUM; i++) {
1768 addr = &hw->data->mac_addrs[i];
1769 if (rte_is_zero_ether_addr(addr))
1771 if (rte_is_multicast_ether_addr(addr))
1772 ret = del ? hns3_remove_mc_addr(hw, addr) :
1773 hns3_add_mc_addr(hw, addr);
1775 ret = del ? hns3_remove_uc_addr_common(hw, addr) :
1776 hns3_add_uc_addr_common(hw, addr);
1780 hns3_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
1782 hns3_err(hw, "failed to %s mac addr(%s) index:%d "
1783 "ret = %d.", del ? "remove" : "restore",
1791 hns3_update_desc_vfid(struct hns3_cmd_desc *desc, uint8_t vfid, bool clr)
1793 #define HNS3_VF_NUM_IN_FIRST_DESC 192
1797 if (vfid < HNS3_VF_NUM_IN_FIRST_DESC) {
1798 word_num = vfid / 32;
1799 bit_num = vfid % 32;
1801 desc[1].data[word_num] &=
1802 rte_cpu_to_le_32(~(1UL << bit_num));
1804 desc[1].data[word_num] |=
1805 rte_cpu_to_le_32(1UL << bit_num);
1807 word_num = (vfid - HNS3_VF_NUM_IN_FIRST_DESC) / 32;
1808 bit_num = vfid % 32;
1810 desc[2].data[word_num] &=
1811 rte_cpu_to_le_32(~(1UL << bit_num));
1813 desc[2].data[word_num] |=
1814 rte_cpu_to_le_32(1UL << bit_num);
1819 hns3_add_mc_addr(struct hns3_hw *hw, struct rte_ether_addr *mac_addr)
1821 struct hns3_mac_vlan_tbl_entry_cmd req;
1822 struct hns3_cmd_desc desc[3];
1823 char mac_str[RTE_ETHER_ADDR_FMT_SIZE];
1827 /* Check if mac addr is valid */
1828 if (!rte_is_multicast_ether_addr(mac_addr)) {
1829 hns3_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
1831 hns3_err(hw, "failed to add mc mac addr, addr(%s) invalid",
1836 memset(&req, 0, sizeof(req));
1837 hns3_set_bit(req.entry_type, HNS3_MAC_VLAN_BIT0_EN_B, 0);
1838 hns3_prepare_mac_addr(&req, mac_addr->addr_bytes, true);
1839 ret = hns3_lookup_mac_vlan_tbl(hw, &req, desc, true);
1841 /* This mac addr do not exist, add new entry for it */
1842 memset(desc[0].data, 0, sizeof(desc[0].data));
1843 memset(desc[1].data, 0, sizeof(desc[0].data));
1844 memset(desc[2].data, 0, sizeof(desc[0].data));
1848 * In current version VF is not supported when PF is driven by DPDK
1849 * driver, just need to configure parameters for PF vport.
1851 vf_id = HNS3_PF_FUNC_ID;
1852 hns3_update_desc_vfid(desc, vf_id, false);
1853 ret = hns3_add_mac_vlan_tbl(hw, &req, desc);
1856 hns3_err(hw, "mc mac vlan table is full");
1857 hns3_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
1859 hns3_err(hw, "failed to add mc mac addr(%s): %d", mac_str, ret);
1866 hns3_remove_mc_addr(struct hns3_hw *hw, struct rte_ether_addr *mac_addr)
1868 struct hns3_mac_vlan_tbl_entry_cmd req;
1869 struct hns3_cmd_desc desc[3];
1870 char mac_str[RTE_ETHER_ADDR_FMT_SIZE];
1874 /* Check if mac addr is valid */
1875 if (!rte_is_multicast_ether_addr(mac_addr)) {
1876 hns3_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
1878 hns3_err(hw, "Failed to rm mc mac addr, addr(%s) invalid",
1883 memset(&req, 0, sizeof(req));
1884 hns3_set_bit(req.entry_type, HNS3_MAC_VLAN_BIT0_EN_B, 0);
1885 hns3_prepare_mac_addr(&req, mac_addr->addr_bytes, true);
1886 ret = hns3_lookup_mac_vlan_tbl(hw, &req, desc, true);
1889 * This mac addr exist, remove this handle's VFID for it.
1890 * In current version VF is not supported when PF is driven by
1891 * DPDK driver, just need to configure parameters for PF vport.
1893 vf_id = HNS3_PF_FUNC_ID;
1894 hns3_update_desc_vfid(desc, vf_id, true);
1896 /* All the vfid is zero, so need to delete this entry */
1897 ret = hns3_remove_mac_vlan_tbl(hw, &req);
1898 } else if (ret == -ENOENT) {
1899 /* This mac addr doesn't exist. */
1904 hns3_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
1906 hns3_err(hw, "Failed to rm mc mac addr(%s): %d", mac_str, ret);
1913 hns3_set_mc_addr_chk_param(struct hns3_hw *hw,
1914 struct rte_ether_addr *mc_addr_set,
1915 uint32_t nb_mc_addr)
1917 char mac_str[RTE_ETHER_ADDR_FMT_SIZE];
1918 struct rte_ether_addr *addr;
1922 if (nb_mc_addr > HNS3_MC_MACADDR_NUM) {
1923 hns3_err(hw, "failed to set mc mac addr, nb_mc_addr(%u) "
1924 "invalid. valid range: 0~%d",
1925 nb_mc_addr, HNS3_MC_MACADDR_NUM);
1929 /* Check if input mac addresses are valid */
1930 for (i = 0; i < nb_mc_addr; i++) {
1931 addr = &mc_addr_set[i];
1932 if (!rte_is_multicast_ether_addr(addr)) {
1933 hns3_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
1936 "failed to set mc mac addr, addr(%s) invalid.",
1941 /* Check if there are duplicate addresses */
1942 for (j = i + 1; j < nb_mc_addr; j++) {
1943 if (rte_is_same_ether_addr(addr, &mc_addr_set[j])) {
1944 hns3_ether_format_addr(mac_str,
1945 RTE_ETHER_ADDR_FMT_SIZE,
1947 hns3_err(hw, "failed to set mc mac addr, "
1948 "addrs invalid. two same addrs(%s).",
1955 * Check if there are duplicate addresses between mac_addrs
1958 for (j = 0; j < HNS3_UC_MACADDR_NUM; j++) {
1959 if (rte_is_same_ether_addr(addr,
1960 &hw->data->mac_addrs[j])) {
1961 hns3_ether_format_addr(mac_str,
1962 RTE_ETHER_ADDR_FMT_SIZE,
1964 hns3_err(hw, "failed to set mc mac addr, "
1965 "addrs invalid. addrs(%s) has already "
1966 "configured in mac_addr add API",
1977 hns3_set_mc_addr_calc_addr(struct hns3_hw *hw,
1978 struct rte_ether_addr *mc_addr_set,
1980 struct rte_ether_addr *reserved_addr_list,
1981 int *reserved_addr_num,
1982 struct rte_ether_addr *add_addr_list,
1984 struct rte_ether_addr *rm_addr_list,
1987 struct rte_ether_addr *addr;
1988 int current_addr_num;
1989 int reserved_num = 0;
1997 /* Calculate the mc mac address list that should be removed */
1998 current_addr_num = hw->mc_addrs_num;
1999 for (i = 0; i < current_addr_num; i++) {
2000 addr = &hw->mc_addrs[i];
2002 for (j = 0; j < mc_addr_num; j++) {
2003 if (rte_is_same_ether_addr(addr, &mc_addr_set[j])) {
2010 rte_ether_addr_copy(addr, &rm_addr_list[rm_num]);
2013 rte_ether_addr_copy(addr,
2014 &reserved_addr_list[reserved_num]);
2019 /* Calculate the mc mac address list that should be added */
2020 for (i = 0; i < mc_addr_num; i++) {
2021 addr = &mc_addr_set[i];
2023 for (j = 0; j < current_addr_num; j++) {
2024 if (rte_is_same_ether_addr(addr, &hw->mc_addrs[j])) {
2031 rte_ether_addr_copy(addr, &add_addr_list[add_num]);
2036 /* Reorder the mc mac address list maintained by driver */
2037 for (i = 0; i < reserved_num; i++)
2038 rte_ether_addr_copy(&reserved_addr_list[i], &hw->mc_addrs[i]);
2040 for (i = 0; i < rm_num; i++) {
2041 num = reserved_num + i;
2042 rte_ether_addr_copy(&rm_addr_list[i], &hw->mc_addrs[num]);
2045 *reserved_addr_num = reserved_num;
2046 *add_addr_num = add_num;
2047 *rm_addr_num = rm_num;
2051 hns3_set_mc_mac_addr_list(struct rte_eth_dev *dev,
2052 struct rte_ether_addr *mc_addr_set,
2053 uint32_t nb_mc_addr)
2055 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2056 struct rte_ether_addr reserved_addr_list[HNS3_MC_MACADDR_NUM];
2057 struct rte_ether_addr add_addr_list[HNS3_MC_MACADDR_NUM];
2058 struct rte_ether_addr rm_addr_list[HNS3_MC_MACADDR_NUM];
2059 struct rte_ether_addr *addr;
2060 int reserved_addr_num;
2068 /* Check if input parameters are valid */
2069 ret = hns3_set_mc_addr_chk_param(hw, mc_addr_set, nb_mc_addr);
2073 rte_spinlock_lock(&hw->lock);
2076 * Calculate the mc mac address lists those should be removed and be
2077 * added, Reorder the mc mac address list maintained by driver.
2079 mc_addr_num = (int)nb_mc_addr;
2080 hns3_set_mc_addr_calc_addr(hw, mc_addr_set, mc_addr_num,
2081 reserved_addr_list, &reserved_addr_num,
2082 add_addr_list, &add_addr_num,
2083 rm_addr_list, &rm_addr_num);
2085 /* Remove mc mac addresses */
2086 for (i = 0; i < rm_addr_num; i++) {
2087 num = rm_addr_num - i - 1;
2088 addr = &rm_addr_list[num];
2089 ret = hns3_remove_mc_addr(hw, addr);
2091 rte_spinlock_unlock(&hw->lock);
2097 /* Add mc mac addresses */
2098 for (i = 0; i < add_addr_num; i++) {
2099 addr = &add_addr_list[i];
2100 ret = hns3_add_mc_addr(hw, addr);
2102 rte_spinlock_unlock(&hw->lock);
2106 num = reserved_addr_num + i;
2107 rte_ether_addr_copy(addr, &hw->mc_addrs[num]);
2110 rte_spinlock_unlock(&hw->lock);
2116 hns3_configure_all_mc_mac_addr(struct hns3_adapter *hns, bool del)
2118 char mac_str[RTE_ETHER_ADDR_FMT_SIZE];
2119 struct hns3_hw *hw = &hns->hw;
2120 struct rte_ether_addr *addr;
2125 for (i = 0; i < hw->mc_addrs_num; i++) {
2126 addr = &hw->mc_addrs[i];
2127 if (!rte_is_multicast_ether_addr(addr))
2130 ret = hns3_remove_mc_addr(hw, addr);
2132 ret = hns3_add_mc_addr(hw, addr);
2135 hns3_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
2137 hns3_dbg(hw, "%s mc mac addr: %s failed for pf: ret = %d",
2138 del ? "Remove" : "Restore", mac_str, ret);
2145 hns3_check_mq_mode(struct rte_eth_dev *dev)
2147 enum rte_eth_rx_mq_mode rx_mq_mode = dev->data->dev_conf.rxmode.mq_mode;
2148 enum rte_eth_tx_mq_mode tx_mq_mode = dev->data->dev_conf.txmode.mq_mode;
2149 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2150 struct hns3_pf *pf = HNS3_DEV_PRIVATE_TO_PF(dev->data->dev_private);
2151 struct rte_eth_dcb_rx_conf *dcb_rx_conf;
2152 struct rte_eth_dcb_tx_conf *dcb_tx_conf;
2157 dcb_rx_conf = &dev->data->dev_conf.rx_adv_conf.dcb_rx_conf;
2158 dcb_tx_conf = &dev->data->dev_conf.tx_adv_conf.dcb_tx_conf;
2160 if (rx_mq_mode == ETH_MQ_RX_VMDQ_DCB_RSS) {
2161 hns3_err(hw, "ETH_MQ_RX_VMDQ_DCB_RSS is not supported. "
2162 "rx_mq_mode = %d", rx_mq_mode);
2166 if (rx_mq_mode == ETH_MQ_RX_VMDQ_DCB ||
2167 tx_mq_mode == ETH_MQ_TX_VMDQ_DCB) {
2168 hns3_err(hw, "ETH_MQ_RX_VMDQ_DCB and ETH_MQ_TX_VMDQ_DCB "
2169 "is not supported. rx_mq_mode = %d, tx_mq_mode = %d",
2170 rx_mq_mode, tx_mq_mode);
2174 if (rx_mq_mode == ETH_MQ_RX_DCB_RSS) {
2175 if (dcb_rx_conf->nb_tcs > pf->tc_max) {
2176 hns3_err(hw, "nb_tcs(%u) > max_tc(%u) driver supported.",
2177 dcb_rx_conf->nb_tcs, pf->tc_max);
2181 if (!(dcb_rx_conf->nb_tcs == HNS3_4_TCS ||
2182 dcb_rx_conf->nb_tcs == HNS3_8_TCS)) {
2183 hns3_err(hw, "on ETH_MQ_RX_DCB_RSS mode, "
2184 "nb_tcs(%d) != %d or %d in rx direction.",
2185 dcb_rx_conf->nb_tcs, HNS3_4_TCS, HNS3_8_TCS);
2189 if (dcb_rx_conf->nb_tcs != dcb_tx_conf->nb_tcs) {
2190 hns3_err(hw, "num_tcs(%d) of tx is not equal to rx(%d)",
2191 dcb_tx_conf->nb_tcs, dcb_rx_conf->nb_tcs);
2195 for (i = 0; i < HNS3_MAX_USER_PRIO; i++) {
2196 if (dcb_rx_conf->dcb_tc[i] != dcb_tx_conf->dcb_tc[i]) {
2197 hns3_err(hw, "dcb_tc[%d] = %u in rx direction, "
2198 "is not equal to one in tx direction.",
2199 i, dcb_rx_conf->dcb_tc[i]);
2202 if (dcb_rx_conf->dcb_tc[i] > max_tc)
2203 max_tc = dcb_rx_conf->dcb_tc[i];
2206 num_tc = max_tc + 1;
2207 if (num_tc > dcb_rx_conf->nb_tcs) {
2208 hns3_err(hw, "max num_tc(%u) mapped > nb_tcs(%u)",
2209 num_tc, dcb_rx_conf->nb_tcs);
2218 hns3_check_dcb_cfg(struct rte_eth_dev *dev)
2220 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2222 if (!hns3_dev_dcb_supported(hw)) {
2223 hns3_err(hw, "this port does not support dcb configurations.");
2227 if (hw->current_fc_status == HNS3_FC_STATUS_MAC_PAUSE) {
2228 hns3_err(hw, "MAC pause enabled, cannot config dcb info.");
2232 /* Check multiple queue mode */
2233 return hns3_check_mq_mode(dev);
2237 hns3_bind_ring_with_vector(struct hns3_hw *hw, uint16_t vector_id, bool en,
2238 enum hns3_ring_type queue_type, uint16_t queue_id)
2240 struct hns3_cmd_desc desc;
2241 struct hns3_ctrl_vector_chain_cmd *req =
2242 (struct hns3_ctrl_vector_chain_cmd *)desc.data;
2243 enum hns3_cmd_status status;
2244 enum hns3_opcode_type op;
2245 uint16_t tqp_type_and_id = 0;
2249 op = en ? HNS3_OPC_ADD_RING_TO_VECTOR : HNS3_OPC_DEL_RING_TO_VECTOR;
2250 hns3_cmd_setup_basic_desc(&desc, op, false);
2251 req->int_vector_id = hns3_get_field(vector_id, HNS3_TQP_INT_ID_L_M,
2252 HNS3_TQP_INT_ID_L_S);
2253 req->int_vector_id_h = hns3_get_field(vector_id, HNS3_TQP_INT_ID_H_M,
2254 HNS3_TQP_INT_ID_H_S);
2256 if (queue_type == HNS3_RING_TYPE_RX)
2257 gl = HNS3_RING_GL_RX;
2259 gl = HNS3_RING_GL_TX;
2263 hns3_set_field(tqp_type_and_id, HNS3_INT_TYPE_M, HNS3_INT_TYPE_S,
2265 hns3_set_field(tqp_type_and_id, HNS3_TQP_ID_M, HNS3_TQP_ID_S, queue_id);
2266 hns3_set_field(tqp_type_and_id, HNS3_INT_GL_IDX_M, HNS3_INT_GL_IDX_S,
2268 req->tqp_type_and_id[0] = rte_cpu_to_le_16(tqp_type_and_id);
2269 req->int_cause_num = 1;
2270 status = hns3_cmd_send(hw, &desc, 1);
2272 hns3_err(hw, "%s TQP %u fail, vector_id is %u, status is %d.",
2273 en ? "Map" : "Unmap", queue_id, vector_id, status);
2281 hns3_init_ring_with_vector(struct hns3_hw *hw)
2288 * In hns3 network engine, vector 0 is always the misc interrupt of this
2289 * function, vector 1~N can be used respectively for the queues of the
2290 * function. Tx and Rx queues with the same number share the interrupt
2291 * vector. In the initialization clearing the all hardware mapping
2292 * relationship configurations between queues and interrupt vectors is
2293 * needed, so some error caused by the residual configurations, such as
2294 * the unexpected Tx interrupt, can be avoid.
2296 vec = hw->num_msi - 1; /* vector 0 for misc interrupt, not for queue */
2297 if (hw->intr.mapping_mode == HNS3_INTR_MAPPING_VEC_RSV_ONE)
2298 vec = vec - 1; /* the last interrupt is reserved */
2299 hw->intr_tqps_num = RTE_MIN(vec, hw->tqps_num);
2300 for (i = 0; i < hw->intr_tqps_num; i++) {
2302 * Set gap limiter/rate limiter/quanity limiter algorithm
2303 * configuration for interrupt coalesce of queue's interrupt.
2305 hns3_set_queue_intr_gl(hw, i, HNS3_RING_GL_RX,
2306 HNS3_TQP_INTR_GL_DEFAULT);
2307 hns3_set_queue_intr_gl(hw, i, HNS3_RING_GL_TX,
2308 HNS3_TQP_INTR_GL_DEFAULT);
2309 hns3_set_queue_intr_rl(hw, i, HNS3_TQP_INTR_RL_DEFAULT);
2311 * QL(quantity limiter) is not used currently, just set 0 to
2314 hns3_set_queue_intr_ql(hw, i, HNS3_TQP_INTR_QL_DEFAULT);
2316 ret = hns3_bind_ring_with_vector(hw, vec, false,
2317 HNS3_RING_TYPE_TX, i);
2319 PMD_INIT_LOG(ERR, "PF fail to unbind TX ring(%d) with "
2320 "vector: %u, ret=%d", i, vec, ret);
2324 ret = hns3_bind_ring_with_vector(hw, vec, false,
2325 HNS3_RING_TYPE_RX, i);
2327 PMD_INIT_LOG(ERR, "PF fail to unbind RX ring(%d) with "
2328 "vector: %u, ret=%d", i, vec, ret);
2337 hns3_dev_configure(struct rte_eth_dev *dev)
2339 struct hns3_adapter *hns = dev->data->dev_private;
2340 struct rte_eth_conf *conf = &dev->data->dev_conf;
2341 enum rte_eth_rx_mq_mode mq_mode = conf->rxmode.mq_mode;
2342 struct hns3_hw *hw = &hns->hw;
2343 uint16_t nb_rx_q = dev->data->nb_rx_queues;
2344 uint16_t nb_tx_q = dev->data->nb_tx_queues;
2345 struct rte_eth_rss_conf rss_conf;
2346 uint32_t max_rx_pkt_len;
2351 hw->cfg_max_queues = RTE_MAX(nb_rx_q, nb_tx_q);
2354 * Some versions of hardware network engine does not support
2355 * individually enable/disable/reset the Tx or Rx queue. These devices
2356 * must enable/disable/reset Tx and Rx queues at the same time. When the
2357 * numbers of Tx queues allocated by upper applications are not equal to
2358 * the numbers of Rx queues, driver needs to setup fake Tx or Rx queues
2359 * to adjust numbers of Tx/Rx queues. otherwise, network engine can not
2360 * work as usual. But these fake queues are imperceptible, and can not
2361 * be used by upper applications.
2363 if (!hns3_dev_indep_txrx_supported(hw)) {
2364 ret = hns3_set_fake_rx_or_tx_queues(dev, nb_rx_q, nb_tx_q);
2366 hns3_err(hw, "fail to set Rx/Tx fake queues, ret = %d.",
2372 hw->adapter_state = HNS3_NIC_CONFIGURING;
2373 if (conf->link_speeds & ETH_LINK_SPEED_FIXED) {
2374 hns3_err(hw, "setting link speed/duplex not supported");
2379 if ((uint32_t)mq_mode & ETH_MQ_RX_DCB_FLAG) {
2380 ret = hns3_check_dcb_cfg(dev);
2385 /* When RSS is not configured, redirect the packet queue 0 */
2386 if ((uint32_t)mq_mode & ETH_MQ_RX_RSS_FLAG) {
2387 conf->rxmode.offloads |= DEV_RX_OFFLOAD_RSS_HASH;
2388 rss_conf = conf->rx_adv_conf.rss_conf;
2389 hw->rss_dis_flag = false;
2390 ret = hns3_dev_rss_hash_update(dev, &rss_conf);
2396 * If jumbo frames are enabled, MTU needs to be refreshed
2397 * according to the maximum RX packet length.
2399 if (conf->rxmode.offloads & DEV_RX_OFFLOAD_JUMBO_FRAME) {
2400 max_rx_pkt_len = conf->rxmode.max_rx_pkt_len;
2401 if (max_rx_pkt_len > HNS3_MAX_FRAME_LEN ||
2402 max_rx_pkt_len <= HNS3_DEFAULT_FRAME_LEN) {
2403 hns3_err(hw, "maximum Rx packet length must be greater "
2404 "than %u and less than %u when jumbo frame enabled.",
2405 (uint16_t)HNS3_DEFAULT_FRAME_LEN,
2406 (uint16_t)HNS3_MAX_FRAME_LEN);
2411 mtu = (uint16_t)HNS3_PKTLEN_TO_MTU(max_rx_pkt_len);
2412 ret = hns3_dev_mtu_set(dev, mtu);
2415 dev->data->mtu = mtu;
2418 ret = hns3_dev_configure_vlan(dev);
2422 /* config hardware GRO */
2423 gro_en = conf->rxmode.offloads & DEV_RX_OFFLOAD_TCP_LRO ? true : false;
2424 ret = hns3_config_gro(hw, gro_en);
2428 hns->rx_simple_allowed = true;
2429 hns->rx_vec_allowed = true;
2430 hns->tx_simple_allowed = true;
2431 hns->tx_vec_allowed = true;
2433 hns3_init_rx_ptype_tble(dev);
2434 hw->adapter_state = HNS3_NIC_CONFIGURED;
2439 (void)hns3_set_fake_rx_or_tx_queues(dev, 0, 0);
2440 hw->adapter_state = HNS3_NIC_INITIALIZED;
2446 hns3_set_mac_mtu(struct hns3_hw *hw, uint16_t new_mps)
2448 struct hns3_config_max_frm_size_cmd *req;
2449 struct hns3_cmd_desc desc;
2451 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_CONFIG_MAX_FRM_SIZE, false);
2453 req = (struct hns3_config_max_frm_size_cmd *)desc.data;
2454 req->max_frm_size = rte_cpu_to_le_16(new_mps);
2455 req->min_frm_size = RTE_ETHER_MIN_LEN;
2457 return hns3_cmd_send(hw, &desc, 1);
2461 hns3_config_mtu(struct hns3_hw *hw, uint16_t mps)
2463 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
2464 uint16_t original_mps = hns->pf.mps;
2468 ret = hns3_set_mac_mtu(hw, mps);
2470 hns3_err(hw, "failed to set mtu, ret = %d", ret);
2475 ret = hns3_buffer_alloc(hw);
2477 hns3_err(hw, "failed to allocate buffer, ret = %d", ret);
2484 err = hns3_set_mac_mtu(hw, original_mps);
2486 hns3_err(hw, "fail to rollback MTU, err = %d", err);
2489 hns->pf.mps = original_mps;
2495 hns3_dev_mtu_set(struct rte_eth_dev *dev, uint16_t mtu)
2497 struct hns3_adapter *hns = dev->data->dev_private;
2498 uint32_t frame_size = mtu + HNS3_ETH_OVERHEAD;
2499 struct hns3_hw *hw = &hns->hw;
2500 bool is_jumbo_frame;
2503 if (dev->data->dev_started) {
2504 hns3_err(hw, "Failed to set mtu, port %u must be stopped "
2505 "before configuration", dev->data->port_id);
2509 rte_spinlock_lock(&hw->lock);
2510 is_jumbo_frame = frame_size > HNS3_DEFAULT_FRAME_LEN ? true : false;
2511 frame_size = RTE_MAX(frame_size, HNS3_DEFAULT_FRAME_LEN);
2514 * Maximum value of frame_size is HNS3_MAX_FRAME_LEN, so it can safely
2515 * assign to "uint16_t" type variable.
2517 ret = hns3_config_mtu(hw, (uint16_t)frame_size);
2519 rte_spinlock_unlock(&hw->lock);
2520 hns3_err(hw, "Failed to set mtu, port %u mtu %u: %d",
2521 dev->data->port_id, mtu, ret);
2526 dev->data->dev_conf.rxmode.offloads |=
2527 DEV_RX_OFFLOAD_JUMBO_FRAME;
2529 dev->data->dev_conf.rxmode.offloads &=
2530 ~DEV_RX_OFFLOAD_JUMBO_FRAME;
2531 dev->data->dev_conf.rxmode.max_rx_pkt_len = frame_size;
2532 rte_spinlock_unlock(&hw->lock);
2538 hns3_dev_infos_get(struct rte_eth_dev *eth_dev, struct rte_eth_dev_info *info)
2540 struct hns3_adapter *hns = eth_dev->data->dev_private;
2541 struct hns3_hw *hw = &hns->hw;
2542 uint16_t queue_num = hw->tqps_num;
2545 * In interrupt mode, 'max_rx_queues' is set based on the number of
2546 * MSI-X interrupt resources of the hardware.
2548 if (hw->data->dev_conf.intr_conf.rxq == 1)
2549 queue_num = hw->intr_tqps_num;
2551 info->max_rx_queues = queue_num;
2552 info->max_tx_queues = hw->tqps_num;
2553 info->max_rx_pktlen = HNS3_MAX_FRAME_LEN; /* CRC included */
2554 info->min_rx_bufsize = HNS3_MIN_BD_BUF_SIZE;
2555 info->max_mac_addrs = HNS3_UC_MACADDR_NUM;
2556 info->max_mtu = info->max_rx_pktlen - HNS3_ETH_OVERHEAD;
2557 info->max_lro_pkt_size = HNS3_MAX_LRO_SIZE;
2558 info->rx_offload_capa = (DEV_RX_OFFLOAD_IPV4_CKSUM |
2559 DEV_RX_OFFLOAD_TCP_CKSUM |
2560 DEV_RX_OFFLOAD_UDP_CKSUM |
2561 DEV_RX_OFFLOAD_SCTP_CKSUM |
2562 DEV_RX_OFFLOAD_OUTER_IPV4_CKSUM |
2563 DEV_RX_OFFLOAD_OUTER_UDP_CKSUM |
2564 DEV_RX_OFFLOAD_KEEP_CRC |
2565 DEV_RX_OFFLOAD_SCATTER |
2566 DEV_RX_OFFLOAD_VLAN_STRIP |
2567 DEV_RX_OFFLOAD_VLAN_FILTER |
2568 DEV_RX_OFFLOAD_JUMBO_FRAME |
2569 DEV_RX_OFFLOAD_RSS_HASH |
2570 DEV_RX_OFFLOAD_TCP_LRO);
2571 info->tx_offload_capa = (DEV_TX_OFFLOAD_OUTER_IPV4_CKSUM |
2572 DEV_TX_OFFLOAD_IPV4_CKSUM |
2573 DEV_TX_OFFLOAD_TCP_CKSUM |
2574 DEV_TX_OFFLOAD_UDP_CKSUM |
2575 DEV_TX_OFFLOAD_SCTP_CKSUM |
2576 DEV_TX_OFFLOAD_MULTI_SEGS |
2577 DEV_TX_OFFLOAD_TCP_TSO |
2578 DEV_TX_OFFLOAD_VXLAN_TNL_TSO |
2579 DEV_TX_OFFLOAD_GRE_TNL_TSO |
2580 DEV_TX_OFFLOAD_GENEVE_TNL_TSO |
2581 DEV_TX_OFFLOAD_MBUF_FAST_FREE |
2582 hns3_txvlan_cap_get(hw));
2584 if (hns3_dev_indep_txrx_supported(hw))
2585 info->dev_capa = RTE_ETH_DEV_CAPA_RUNTIME_RX_QUEUE_SETUP |
2586 RTE_ETH_DEV_CAPA_RUNTIME_TX_QUEUE_SETUP;
2588 info->rx_desc_lim = (struct rte_eth_desc_lim) {
2589 .nb_max = HNS3_MAX_RING_DESC,
2590 .nb_min = HNS3_MIN_RING_DESC,
2591 .nb_align = HNS3_ALIGN_RING_DESC,
2594 info->tx_desc_lim = (struct rte_eth_desc_lim) {
2595 .nb_max = HNS3_MAX_RING_DESC,
2596 .nb_min = HNS3_MIN_RING_DESC,
2597 .nb_align = HNS3_ALIGN_RING_DESC,
2598 .nb_seg_max = HNS3_MAX_TSO_BD_PER_PKT,
2599 .nb_mtu_seg_max = hw->max_non_tso_bd_num,
2602 info->default_rxconf = (struct rte_eth_rxconf) {
2603 .rx_free_thresh = HNS3_DEFAULT_RX_FREE_THRESH,
2605 * If there are no available Rx buffer descriptors, incoming
2606 * packets are always dropped by hardware based on hns3 network
2612 info->default_txconf = (struct rte_eth_txconf) {
2613 .tx_rs_thresh = HNS3_DEFAULT_TX_RS_THRESH,
2617 info->vmdq_queue_num = 0;
2619 info->reta_size = hw->rss_ind_tbl_size;
2620 info->hash_key_size = HNS3_RSS_KEY_SIZE;
2621 info->flow_type_rss_offloads = HNS3_ETH_RSS_SUPPORT;
2623 info->default_rxportconf.burst_size = HNS3_DEFAULT_PORT_CONF_BURST_SIZE;
2624 info->default_txportconf.burst_size = HNS3_DEFAULT_PORT_CONF_BURST_SIZE;
2625 info->default_rxportconf.nb_queues = HNS3_DEFAULT_PORT_CONF_QUEUES_NUM;
2626 info->default_txportconf.nb_queues = HNS3_DEFAULT_PORT_CONF_QUEUES_NUM;
2627 info->default_rxportconf.ring_size = HNS3_DEFAULT_RING_DESC;
2628 info->default_txportconf.ring_size = HNS3_DEFAULT_RING_DESC;
2634 hns3_fw_version_get(struct rte_eth_dev *eth_dev, char *fw_version,
2637 struct hns3_adapter *hns = eth_dev->data->dev_private;
2638 struct hns3_hw *hw = &hns->hw;
2639 uint32_t version = hw->fw_version;
2642 ret = snprintf(fw_version, fw_size, "%lu.%lu.%lu.%lu",
2643 hns3_get_field(version, HNS3_FW_VERSION_BYTE3_M,
2644 HNS3_FW_VERSION_BYTE3_S),
2645 hns3_get_field(version, HNS3_FW_VERSION_BYTE2_M,
2646 HNS3_FW_VERSION_BYTE2_S),
2647 hns3_get_field(version, HNS3_FW_VERSION_BYTE1_M,
2648 HNS3_FW_VERSION_BYTE1_S),
2649 hns3_get_field(version, HNS3_FW_VERSION_BYTE0_M,
2650 HNS3_FW_VERSION_BYTE0_S));
2651 ret += 1; /* add the size of '\0' */
2652 if (fw_size < (uint32_t)ret)
2659 hns3_dev_link_update(struct rte_eth_dev *eth_dev,
2660 __rte_unused int wait_to_complete)
2662 struct hns3_adapter *hns = eth_dev->data->dev_private;
2663 struct hns3_hw *hw = &hns->hw;
2664 struct hns3_mac *mac = &hw->mac;
2665 struct rte_eth_link new_link;
2667 if (!hns3_is_reset_pending(hns)) {
2668 hns3_update_link_status(hw);
2669 hns3_update_link_info(eth_dev);
2672 memset(&new_link, 0, sizeof(new_link));
2673 switch (mac->link_speed) {
2674 case ETH_SPEED_NUM_10M:
2675 case ETH_SPEED_NUM_100M:
2676 case ETH_SPEED_NUM_1G:
2677 case ETH_SPEED_NUM_10G:
2678 case ETH_SPEED_NUM_25G:
2679 case ETH_SPEED_NUM_40G:
2680 case ETH_SPEED_NUM_50G:
2681 case ETH_SPEED_NUM_100G:
2682 case ETH_SPEED_NUM_200G:
2683 new_link.link_speed = mac->link_speed;
2686 new_link.link_speed = ETH_SPEED_NUM_100M;
2690 new_link.link_duplex = mac->link_duplex;
2691 new_link.link_status = mac->link_status ? ETH_LINK_UP : ETH_LINK_DOWN;
2692 new_link.link_autoneg =
2693 !(eth_dev->data->dev_conf.link_speeds & ETH_LINK_SPEED_FIXED);
2695 return rte_eth_linkstatus_set(eth_dev, &new_link);
2699 hns3_parse_func_status(struct hns3_hw *hw, struct hns3_func_status_cmd *status)
2701 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
2702 struct hns3_pf *pf = &hns->pf;
2704 if (!(status->pf_state & HNS3_PF_STATE_DONE))
2707 pf->is_main_pf = (status->pf_state & HNS3_PF_STATE_MAIN) ? true : false;
2713 hns3_query_function_status(struct hns3_hw *hw)
2715 #define HNS3_QUERY_MAX_CNT 10
2716 #define HNS3_QUERY_SLEEP_MSCOEND 1
2717 struct hns3_func_status_cmd *req;
2718 struct hns3_cmd_desc desc;
2722 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_QUERY_FUNC_STATUS, true);
2723 req = (struct hns3_func_status_cmd *)desc.data;
2726 ret = hns3_cmd_send(hw, &desc, 1);
2728 PMD_INIT_LOG(ERR, "query function status failed %d",
2733 /* Check pf reset is done */
2737 rte_delay_ms(HNS3_QUERY_SLEEP_MSCOEND);
2738 } while (timeout++ < HNS3_QUERY_MAX_CNT);
2740 return hns3_parse_func_status(hw, req);
2744 hns3_get_pf_max_tqp_num(struct hns3_hw *hw)
2746 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
2747 struct hns3_pf *pf = &hns->pf;
2749 if (pf->tqp_config_mode == HNS3_FLEX_MAX_TQP_NUM_MODE) {
2751 * The total_tqps_num obtained from firmware is maximum tqp
2752 * numbers of this port, which should be used for PF and VFs.
2753 * There is no need for pf to have so many tqp numbers in
2754 * most cases. RTE_LIBRTE_HNS3_MAX_TQP_NUM_PER_PF,
2755 * coming from config file, is assigned to maximum queue number
2756 * for the PF of this port by user. So users can modify the
2757 * maximum queue number of PF according to their own application
2758 * scenarios, which is more flexible to use. In addition, many
2759 * memories can be saved due to allocating queue statistics
2760 * room according to the actual number of queues required. The
2761 * maximum queue number of PF for network engine with
2762 * revision_id greater than 0x30 is assigned by config file.
2764 if (RTE_LIBRTE_HNS3_MAX_TQP_NUM_PER_PF <= 0) {
2765 hns3_err(hw, "RTE_LIBRTE_HNS3_MAX_TQP_NUM_PER_PF(%d) "
2766 "must be greater than 0.",
2767 RTE_LIBRTE_HNS3_MAX_TQP_NUM_PER_PF);
2771 hw->tqps_num = RTE_MIN(RTE_LIBRTE_HNS3_MAX_TQP_NUM_PER_PF,
2772 hw->total_tqps_num);
2775 * Due to the limitation on the number of PF interrupts
2776 * available, the maximum queue number assigned to PF on
2777 * the network engine with revision_id 0x21 is 64.
2779 hw->tqps_num = RTE_MIN(hw->total_tqps_num,
2780 HNS3_MAX_TQP_NUM_HIP08_PF);
2787 hns3_query_pf_resource(struct hns3_hw *hw)
2789 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
2790 struct hns3_pf *pf = &hns->pf;
2791 struct hns3_pf_res_cmd *req;
2792 struct hns3_cmd_desc desc;
2795 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_QUERY_PF_RSRC, true);
2796 ret = hns3_cmd_send(hw, &desc, 1);
2798 PMD_INIT_LOG(ERR, "query pf resource failed %d", ret);
2802 req = (struct hns3_pf_res_cmd *)desc.data;
2803 hw->total_tqps_num = rte_le_to_cpu_16(req->tqp_num) +
2804 rte_le_to_cpu_16(req->ext_tqp_num);
2805 ret = hns3_get_pf_max_tqp_num(hw);
2809 pf->pkt_buf_size = rte_le_to_cpu_16(req->buf_size) << HNS3_BUF_UNIT_S;
2810 pf->func_num = rte_le_to_cpu_16(req->pf_own_fun_number);
2812 if (req->tx_buf_size)
2814 rte_le_to_cpu_16(req->tx_buf_size) << HNS3_BUF_UNIT_S;
2816 pf->tx_buf_size = HNS3_DEFAULT_TX_BUF;
2818 pf->tx_buf_size = roundup(pf->tx_buf_size, HNS3_BUF_SIZE_UNIT);
2820 if (req->dv_buf_size)
2822 rte_le_to_cpu_16(req->dv_buf_size) << HNS3_BUF_UNIT_S;
2824 pf->dv_buf_size = HNS3_DEFAULT_DV;
2826 pf->dv_buf_size = roundup(pf->dv_buf_size, HNS3_BUF_SIZE_UNIT);
2829 hns3_get_field(rte_le_to_cpu_16(req->nic_pf_intr_vector_number),
2830 HNS3_PF_VEC_NUM_M, HNS3_PF_VEC_NUM_S);
2836 hns3_parse_cfg(struct hns3_cfg *cfg, struct hns3_cmd_desc *desc)
2838 struct hns3_cfg_param_cmd *req;
2839 uint64_t mac_addr_tmp_high;
2840 uint8_t ext_rss_size_max;
2841 uint64_t mac_addr_tmp;
2844 req = (struct hns3_cfg_param_cmd *)desc[0].data;
2846 /* get the configuration */
2847 cfg->vmdq_vport_num = hns3_get_field(rte_le_to_cpu_32(req->param[0]),
2848 HNS3_CFG_VMDQ_M, HNS3_CFG_VMDQ_S);
2849 cfg->tc_num = hns3_get_field(rte_le_to_cpu_32(req->param[0]),
2850 HNS3_CFG_TC_NUM_M, HNS3_CFG_TC_NUM_S);
2851 cfg->tqp_desc_num = hns3_get_field(rte_le_to_cpu_32(req->param[0]),
2852 HNS3_CFG_TQP_DESC_N_M,
2853 HNS3_CFG_TQP_DESC_N_S);
2855 cfg->phy_addr = hns3_get_field(rte_le_to_cpu_32(req->param[1]),
2856 HNS3_CFG_PHY_ADDR_M,
2857 HNS3_CFG_PHY_ADDR_S);
2858 cfg->media_type = hns3_get_field(rte_le_to_cpu_32(req->param[1]),
2859 HNS3_CFG_MEDIA_TP_M,
2860 HNS3_CFG_MEDIA_TP_S);
2861 cfg->rx_buf_len = hns3_get_field(rte_le_to_cpu_32(req->param[1]),
2862 HNS3_CFG_RX_BUF_LEN_M,
2863 HNS3_CFG_RX_BUF_LEN_S);
2864 /* get mac address */
2865 mac_addr_tmp = rte_le_to_cpu_32(req->param[2]);
2866 mac_addr_tmp_high = hns3_get_field(rte_le_to_cpu_32(req->param[3]),
2867 HNS3_CFG_MAC_ADDR_H_M,
2868 HNS3_CFG_MAC_ADDR_H_S);
2870 mac_addr_tmp |= (mac_addr_tmp_high << 31) << 1;
2872 cfg->default_speed = hns3_get_field(rte_le_to_cpu_32(req->param[3]),
2873 HNS3_CFG_DEFAULT_SPEED_M,
2874 HNS3_CFG_DEFAULT_SPEED_S);
2875 cfg->rss_size_max = hns3_get_field(rte_le_to_cpu_32(req->param[3]),
2876 HNS3_CFG_RSS_SIZE_M,
2877 HNS3_CFG_RSS_SIZE_S);
2879 for (i = 0; i < RTE_ETHER_ADDR_LEN; i++)
2880 cfg->mac_addr[i] = (mac_addr_tmp >> (8 * i)) & 0xff;
2882 req = (struct hns3_cfg_param_cmd *)desc[1].data;
2883 cfg->numa_node_map = rte_le_to_cpu_32(req->param[0]);
2885 cfg->speed_ability = hns3_get_field(rte_le_to_cpu_32(req->param[1]),
2886 HNS3_CFG_SPEED_ABILITY_M,
2887 HNS3_CFG_SPEED_ABILITY_S);
2888 cfg->umv_space = hns3_get_field(rte_le_to_cpu_32(req->param[1]),
2889 HNS3_CFG_UMV_TBL_SPACE_M,
2890 HNS3_CFG_UMV_TBL_SPACE_S);
2891 if (!cfg->umv_space)
2892 cfg->umv_space = HNS3_DEFAULT_UMV_SPACE_PER_PF;
2894 ext_rss_size_max = hns3_get_field(rte_le_to_cpu_32(req->param[2]),
2895 HNS3_CFG_EXT_RSS_SIZE_M,
2896 HNS3_CFG_EXT_RSS_SIZE_S);
2899 * Field ext_rss_size_max obtained from firmware will be more flexible
2900 * for future changes and expansions, which is an exponent of 2, instead
2901 * of reading out directly. If this field is not zero, hns3 PF PMD
2902 * driver uses it as rss_size_max under one TC. Device, whose revision
2903 * id is greater than or equal to PCI_REVISION_ID_HIP09_A, obtains the
2904 * maximum number of queues supported under a TC through this field.
2906 if (ext_rss_size_max)
2907 cfg->rss_size_max = 1U << ext_rss_size_max;
2910 /* hns3_get_board_cfg: query the static parameter from NCL_config file in flash
2911 * @hw: pointer to struct hns3_hw
2912 * @hcfg: the config structure to be getted
2915 hns3_get_board_cfg(struct hns3_hw *hw, struct hns3_cfg *hcfg)
2917 struct hns3_cmd_desc desc[HNS3_PF_CFG_DESC_NUM];
2918 struct hns3_cfg_param_cmd *req;
2923 for (i = 0; i < HNS3_PF_CFG_DESC_NUM; i++) {
2925 req = (struct hns3_cfg_param_cmd *)desc[i].data;
2926 hns3_cmd_setup_basic_desc(&desc[i], HNS3_OPC_GET_CFG_PARAM,
2928 hns3_set_field(offset, HNS3_CFG_OFFSET_M, HNS3_CFG_OFFSET_S,
2929 i * HNS3_CFG_RD_LEN_BYTES);
2930 /* Len should be divided by 4 when send to hardware */
2931 hns3_set_field(offset, HNS3_CFG_RD_LEN_M, HNS3_CFG_RD_LEN_S,
2932 HNS3_CFG_RD_LEN_BYTES / HNS3_CFG_RD_LEN_UNIT);
2933 req->offset = rte_cpu_to_le_32(offset);
2936 ret = hns3_cmd_send(hw, desc, HNS3_PF_CFG_DESC_NUM);
2938 PMD_INIT_LOG(ERR, "get config failed %d.", ret);
2942 hns3_parse_cfg(hcfg, desc);
2948 hns3_parse_speed(int speed_cmd, uint32_t *speed)
2950 switch (speed_cmd) {
2951 case HNS3_CFG_SPEED_10M:
2952 *speed = ETH_SPEED_NUM_10M;
2954 case HNS3_CFG_SPEED_100M:
2955 *speed = ETH_SPEED_NUM_100M;
2957 case HNS3_CFG_SPEED_1G:
2958 *speed = ETH_SPEED_NUM_1G;
2960 case HNS3_CFG_SPEED_10G:
2961 *speed = ETH_SPEED_NUM_10G;
2963 case HNS3_CFG_SPEED_25G:
2964 *speed = ETH_SPEED_NUM_25G;
2966 case HNS3_CFG_SPEED_40G:
2967 *speed = ETH_SPEED_NUM_40G;
2969 case HNS3_CFG_SPEED_50G:
2970 *speed = ETH_SPEED_NUM_50G;
2972 case HNS3_CFG_SPEED_100G:
2973 *speed = ETH_SPEED_NUM_100G;
2975 case HNS3_CFG_SPEED_200G:
2976 *speed = ETH_SPEED_NUM_200G;
2986 hns3_set_default_dev_specifications(struct hns3_hw *hw)
2988 hw->max_non_tso_bd_num = HNS3_MAX_NON_TSO_BD_PER_PKT;
2989 hw->rss_ind_tbl_size = HNS3_RSS_IND_TBL_SIZE;
2990 hw->rss_key_size = HNS3_RSS_KEY_SIZE;
2991 hw->max_tm_rate = HNS3_ETHER_MAX_RATE;
2992 hw->intr.int_ql_max = HNS3_INTR_QL_NONE;
2996 hns3_parse_dev_specifications(struct hns3_hw *hw, struct hns3_cmd_desc *desc)
2998 struct hns3_dev_specs_0_cmd *req0;
3000 req0 = (struct hns3_dev_specs_0_cmd *)desc[0].data;
3002 hw->max_non_tso_bd_num = req0->max_non_tso_bd_num;
3003 hw->rss_ind_tbl_size = rte_le_to_cpu_16(req0->rss_ind_tbl_size);
3004 hw->rss_key_size = rte_le_to_cpu_16(req0->rss_key_size);
3005 hw->max_tm_rate = rte_le_to_cpu_32(req0->max_tm_rate);
3006 hw->intr.int_ql_max = rte_le_to_cpu_16(req0->intr_ql_max);
3010 hns3_check_dev_specifications(struct hns3_hw *hw)
3012 if (hw->rss_ind_tbl_size == 0 ||
3013 hw->rss_ind_tbl_size > HNS3_RSS_IND_TBL_SIZE_MAX) {
3014 hns3_err(hw, "the size of hash lookup table configured (%u)"
3015 " exceeds the maximum(%u)", hw->rss_ind_tbl_size,
3016 HNS3_RSS_IND_TBL_SIZE_MAX);
3024 hns3_query_dev_specifications(struct hns3_hw *hw)
3026 struct hns3_cmd_desc desc[HNS3_QUERY_DEV_SPECS_BD_NUM];
3030 for (i = 0; i < HNS3_QUERY_DEV_SPECS_BD_NUM - 1; i++) {
3031 hns3_cmd_setup_basic_desc(&desc[i], HNS3_OPC_QUERY_DEV_SPECS,
3033 desc[i].flag |= rte_cpu_to_le_16(HNS3_CMD_FLAG_NEXT);
3035 hns3_cmd_setup_basic_desc(&desc[i], HNS3_OPC_QUERY_DEV_SPECS, true);
3037 ret = hns3_cmd_send(hw, desc, HNS3_QUERY_DEV_SPECS_BD_NUM);
3041 hns3_parse_dev_specifications(hw, desc);
3043 return hns3_check_dev_specifications(hw);
3047 hns3_get_capability(struct hns3_hw *hw)
3049 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
3050 struct rte_pci_device *pci_dev;
3051 struct hns3_pf *pf = &hns->pf;
3052 struct rte_eth_dev *eth_dev;
3057 eth_dev = &rte_eth_devices[hw->data->port_id];
3058 pci_dev = RTE_ETH_DEV_TO_PCI(eth_dev);
3059 device_id = pci_dev->id.device_id;
3061 if (device_id == HNS3_DEV_ID_25GE_RDMA ||
3062 device_id == HNS3_DEV_ID_50GE_RDMA ||
3063 device_id == HNS3_DEV_ID_100G_RDMA_MACSEC ||
3064 device_id == HNS3_DEV_ID_200G_RDMA)
3065 hns3_set_bit(hw->capability, HNS3_DEV_SUPPORT_DCB_B, 1);
3067 /* Get PCI revision id */
3068 ret = rte_pci_read_config(pci_dev, &revision, HNS3_PCI_REVISION_ID_LEN,
3069 HNS3_PCI_REVISION_ID);
3070 if (ret != HNS3_PCI_REVISION_ID_LEN) {
3071 PMD_INIT_LOG(ERR, "failed to read pci revision id, ret = %d",
3075 hw->revision = revision;
3077 if (revision < PCI_REVISION_ID_HIP09_A) {
3078 hns3_set_default_dev_specifications(hw);
3079 hw->intr.mapping_mode = HNS3_INTR_MAPPING_VEC_RSV_ONE;
3080 hw->intr.gl_unit = HNS3_INTR_COALESCE_GL_UINT_2US;
3081 hw->tso_mode = HNS3_TSO_SW_CAL_PSEUDO_H_CSUM;
3082 hw->vlan_mode = HNS3_SW_SHIFT_AND_DISCARD_MODE;
3083 hw->min_tx_pkt_len = HNS3_HIP08_MIN_TX_PKT_LEN;
3084 pf->tqp_config_mode = HNS3_FIXED_MAX_TQP_NUM_MODE;
3085 hw->rss_info.ipv6_sctp_offload_supported = false;
3089 ret = hns3_query_dev_specifications(hw);
3092 "failed to query dev specifications, ret = %d",
3097 hw->intr.mapping_mode = HNS3_INTR_MAPPING_VEC_ALL;
3098 hw->intr.gl_unit = HNS3_INTR_COALESCE_GL_UINT_1US;
3099 hw->tso_mode = HNS3_TSO_HW_CAL_PSEUDO_H_CSUM;
3100 hw->vlan_mode = HNS3_HW_SHIFT_AND_DISCARD_MODE;
3101 hw->min_tx_pkt_len = HNS3_HIP09_MIN_TX_PKT_LEN;
3102 pf->tqp_config_mode = HNS3_FLEX_MAX_TQP_NUM_MODE;
3103 hw->rss_info.ipv6_sctp_offload_supported = true;
3109 hns3_check_media_type(struct hns3_hw *hw, uint8_t media_type)
3113 switch (media_type) {
3114 case HNS3_MEDIA_TYPE_COPPER:
3115 if (!hns3_dev_copper_supported(hw)) {
3117 "Media type is copper, not supported.");
3123 case HNS3_MEDIA_TYPE_FIBER:
3126 case HNS3_MEDIA_TYPE_BACKPLANE:
3127 PMD_INIT_LOG(ERR, "Media type is Backplane, not supported.");
3131 PMD_INIT_LOG(ERR, "Unknown media type = %u!", media_type);
3140 hns3_get_board_configuration(struct hns3_hw *hw)
3142 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
3143 struct hns3_pf *pf = &hns->pf;
3144 struct hns3_cfg cfg;
3147 ret = hns3_get_board_cfg(hw, &cfg);
3149 PMD_INIT_LOG(ERR, "get board config failed %d", ret);
3153 ret = hns3_check_media_type(hw, cfg.media_type);
3157 hw->mac.media_type = cfg.media_type;
3158 hw->rss_size_max = cfg.rss_size_max;
3159 hw->rss_dis_flag = false;
3160 memcpy(hw->mac.mac_addr, cfg.mac_addr, RTE_ETHER_ADDR_LEN);
3161 hw->mac.phy_addr = cfg.phy_addr;
3162 hw->mac.default_addr_setted = false;
3163 hw->num_tx_desc = cfg.tqp_desc_num;
3164 hw->num_rx_desc = cfg.tqp_desc_num;
3165 hw->dcb_info.num_pg = 1;
3166 hw->dcb_info.hw_pfc_map = 0;
3168 ret = hns3_parse_speed(cfg.default_speed, &hw->mac.link_speed);
3170 PMD_INIT_LOG(ERR, "Get wrong speed %u, ret = %d",
3171 cfg.default_speed, ret);
3175 pf->tc_max = cfg.tc_num;
3176 if (pf->tc_max > HNS3_MAX_TC_NUM || pf->tc_max < 1) {
3177 PMD_INIT_LOG(WARNING,
3178 "Get TC num(%u) from flash, set TC num to 1",
3183 /* Dev does not support DCB */
3184 if (!hns3_dev_dcb_supported(hw)) {
3188 pf->pfc_max = pf->tc_max;
3190 hw->dcb_info.num_tc = 1;
3191 hw->alloc_rss_size = RTE_MIN(hw->rss_size_max,
3192 hw->tqps_num / hw->dcb_info.num_tc);
3193 hns3_set_bit(hw->hw_tc_map, 0, 1);
3194 pf->tx_sch_mode = HNS3_FLAG_TC_BASE_SCH_MODE;
3196 pf->wanted_umv_size = cfg.umv_space;
3202 hns3_get_configuration(struct hns3_hw *hw)
3206 ret = hns3_query_function_status(hw);
3208 PMD_INIT_LOG(ERR, "Failed to query function status: %d.", ret);
3212 /* Get device capability */
3213 ret = hns3_get_capability(hw);
3215 PMD_INIT_LOG(ERR, "failed to get device capability: %d.", ret);
3219 /* Get pf resource */
3220 ret = hns3_query_pf_resource(hw);
3222 PMD_INIT_LOG(ERR, "Failed to query pf resource: %d", ret);
3226 ret = hns3_get_board_configuration(hw);
3228 PMD_INIT_LOG(ERR, "failed to get board configuration: %d", ret);
3232 ret = hns3_query_dev_fec_info(hw);
3235 "failed to query FEC information, ret = %d", ret);
3241 hns3_map_tqps_to_func(struct hns3_hw *hw, uint16_t func_id, uint16_t tqp_pid,
3242 uint16_t tqp_vid, bool is_pf)
3244 struct hns3_tqp_map_cmd *req;
3245 struct hns3_cmd_desc desc;
3248 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_SET_TQP_MAP, false);
3250 req = (struct hns3_tqp_map_cmd *)desc.data;
3251 req->tqp_id = rte_cpu_to_le_16(tqp_pid);
3252 req->tqp_vf = func_id;
3253 req->tqp_flag = 1 << HNS3_TQP_MAP_EN_B;
3255 req->tqp_flag |= (1 << HNS3_TQP_MAP_TYPE_B);
3256 req->tqp_vid = rte_cpu_to_le_16(tqp_vid);
3258 ret = hns3_cmd_send(hw, &desc, 1);
3260 PMD_INIT_LOG(ERR, "TQP map failed %d", ret);
3266 hns3_map_tqp(struct hns3_hw *hw)
3272 * In current version, VF is not supported when PF is driven by DPDK
3273 * driver, so we assign total tqps_num tqps allocated to this port
3276 for (i = 0; i < hw->total_tqps_num; i++) {
3277 ret = hns3_map_tqps_to_func(hw, HNS3_PF_FUNC_ID, i, i, true);
3286 hns3_cfg_mac_speed_dup_hw(struct hns3_hw *hw, uint32_t speed, uint8_t duplex)
3288 struct hns3_config_mac_speed_dup_cmd *req;
3289 struct hns3_cmd_desc desc;
3292 req = (struct hns3_config_mac_speed_dup_cmd *)desc.data;
3294 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_CONFIG_SPEED_DUP, false);
3296 hns3_set_bit(req->speed_dup, HNS3_CFG_DUPLEX_B, !!duplex ? 1 : 0);
3299 case ETH_SPEED_NUM_10M:
3300 hns3_set_field(req->speed_dup, HNS3_CFG_SPEED_M,
3301 HNS3_CFG_SPEED_S, HNS3_CFG_SPEED_10M);
3303 case ETH_SPEED_NUM_100M:
3304 hns3_set_field(req->speed_dup, HNS3_CFG_SPEED_M,
3305 HNS3_CFG_SPEED_S, HNS3_CFG_SPEED_100M);
3307 case ETH_SPEED_NUM_1G:
3308 hns3_set_field(req->speed_dup, HNS3_CFG_SPEED_M,
3309 HNS3_CFG_SPEED_S, HNS3_CFG_SPEED_1G);
3311 case ETH_SPEED_NUM_10G:
3312 hns3_set_field(req->speed_dup, HNS3_CFG_SPEED_M,
3313 HNS3_CFG_SPEED_S, HNS3_CFG_SPEED_10G);
3315 case ETH_SPEED_NUM_25G:
3316 hns3_set_field(req->speed_dup, HNS3_CFG_SPEED_M,
3317 HNS3_CFG_SPEED_S, HNS3_CFG_SPEED_25G);
3319 case ETH_SPEED_NUM_40G:
3320 hns3_set_field(req->speed_dup, HNS3_CFG_SPEED_M,
3321 HNS3_CFG_SPEED_S, HNS3_CFG_SPEED_40G);
3323 case ETH_SPEED_NUM_50G:
3324 hns3_set_field(req->speed_dup, HNS3_CFG_SPEED_M,
3325 HNS3_CFG_SPEED_S, HNS3_CFG_SPEED_50G);
3327 case ETH_SPEED_NUM_100G:
3328 hns3_set_field(req->speed_dup, HNS3_CFG_SPEED_M,
3329 HNS3_CFG_SPEED_S, HNS3_CFG_SPEED_100G);
3331 case ETH_SPEED_NUM_200G:
3332 hns3_set_field(req->speed_dup, HNS3_CFG_SPEED_M,
3333 HNS3_CFG_SPEED_S, HNS3_CFG_SPEED_200G);
3336 PMD_INIT_LOG(ERR, "invalid speed (%u)", speed);
3340 hns3_set_bit(req->mac_change_fec_en, HNS3_CFG_MAC_SPEED_CHANGE_EN_B, 1);
3342 ret = hns3_cmd_send(hw, &desc, 1);
3344 PMD_INIT_LOG(ERR, "mac speed/duplex config cmd failed %d", ret);
3350 hns3_tx_buffer_calc(struct hns3_hw *hw, struct hns3_pkt_buf_alloc *buf_alloc)
3352 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
3353 struct hns3_pf *pf = &hns->pf;
3354 struct hns3_priv_buf *priv;
3355 uint32_t i, total_size;
3357 total_size = pf->pkt_buf_size;
3359 /* alloc tx buffer for all enabled tc */
3360 for (i = 0; i < HNS3_MAX_TC_NUM; i++) {
3361 priv = &buf_alloc->priv_buf[i];
3363 if (hw->hw_tc_map & BIT(i)) {
3364 if (total_size < pf->tx_buf_size)
3367 priv->tx_buf_size = pf->tx_buf_size;
3369 priv->tx_buf_size = 0;
3371 total_size -= priv->tx_buf_size;
3378 hns3_tx_buffer_alloc(struct hns3_hw *hw, struct hns3_pkt_buf_alloc *buf_alloc)
3380 /* TX buffer size is unit by 128 byte */
3381 #define HNS3_BUF_SIZE_UNIT_SHIFT 7
3382 #define HNS3_BUF_SIZE_UPDATE_EN_MSK BIT(15)
3383 struct hns3_tx_buff_alloc_cmd *req;
3384 struct hns3_cmd_desc desc;
3389 req = (struct hns3_tx_buff_alloc_cmd *)desc.data;
3391 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_TX_BUFF_ALLOC, 0);
3392 for (i = 0; i < HNS3_MAX_TC_NUM; i++) {
3393 buf_size = buf_alloc->priv_buf[i].tx_buf_size;
3395 buf_size = buf_size >> HNS3_BUF_SIZE_UNIT_SHIFT;
3396 req->tx_pkt_buff[i] = rte_cpu_to_le_16(buf_size |
3397 HNS3_BUF_SIZE_UPDATE_EN_MSK);
3400 ret = hns3_cmd_send(hw, &desc, 1);
3402 PMD_INIT_LOG(ERR, "tx buffer alloc cmd failed %d", ret);
3408 hns3_get_tc_num(struct hns3_hw *hw)
3413 for (i = 0; i < HNS3_MAX_TC_NUM; i++)
3414 if (hw->hw_tc_map & BIT(i))
3420 hns3_get_rx_priv_buff_alloced(struct hns3_pkt_buf_alloc *buf_alloc)
3422 struct hns3_priv_buf *priv;
3423 uint32_t rx_priv = 0;
3426 for (i = 0; i < HNS3_MAX_TC_NUM; i++) {
3427 priv = &buf_alloc->priv_buf[i];
3429 rx_priv += priv->buf_size;
3435 hns3_get_tx_buff_alloced(struct hns3_pkt_buf_alloc *buf_alloc)
3437 uint32_t total_tx_size = 0;
3440 for (i = 0; i < HNS3_MAX_TC_NUM; i++)
3441 total_tx_size += buf_alloc->priv_buf[i].tx_buf_size;
3443 return total_tx_size;
3446 /* Get the number of pfc enabled TCs, which have private buffer */
3448 hns3_get_pfc_priv_num(struct hns3_hw *hw, struct hns3_pkt_buf_alloc *buf_alloc)
3450 struct hns3_priv_buf *priv;
3454 for (i = 0; i < HNS3_MAX_TC_NUM; i++) {
3455 priv = &buf_alloc->priv_buf[i];
3456 if ((hw->dcb_info.hw_pfc_map & BIT(i)) && priv->enable)
3463 /* Get the number of pfc disabled TCs, which have private buffer */
3465 hns3_get_no_pfc_priv_num(struct hns3_hw *hw,
3466 struct hns3_pkt_buf_alloc *buf_alloc)
3468 struct hns3_priv_buf *priv;
3472 for (i = 0; i < HNS3_MAX_TC_NUM; i++) {
3473 priv = &buf_alloc->priv_buf[i];
3474 if (hw->hw_tc_map & BIT(i) &&
3475 !(hw->dcb_info.hw_pfc_map & BIT(i)) && priv->enable)
3483 hns3_is_rx_buf_ok(struct hns3_hw *hw, struct hns3_pkt_buf_alloc *buf_alloc,
3486 uint32_t shared_buf_min, shared_buf_tc, shared_std, hi_thrd, lo_thrd;
3487 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
3488 struct hns3_pf *pf = &hns->pf;
3489 uint32_t shared_buf, aligned_mps;
3494 tc_num = hns3_get_tc_num(hw);
3495 aligned_mps = roundup(pf->mps, HNS3_BUF_SIZE_UNIT);
3497 if (hns3_dev_dcb_supported(hw))
3498 shared_buf_min = HNS3_BUF_MUL_BY * aligned_mps +
3501 shared_buf_min = aligned_mps + HNS3_NON_DCB_ADDITIONAL_BUF
3504 shared_buf_tc = tc_num * aligned_mps + aligned_mps;
3505 shared_std = roundup(RTE_MAX(shared_buf_min, shared_buf_tc),
3506 HNS3_BUF_SIZE_UNIT);
3508 rx_priv = hns3_get_rx_priv_buff_alloced(buf_alloc);
3509 if (rx_all < rx_priv + shared_std)
3512 shared_buf = rounddown(rx_all - rx_priv, HNS3_BUF_SIZE_UNIT);
3513 buf_alloc->s_buf.buf_size = shared_buf;
3514 if (hns3_dev_dcb_supported(hw)) {
3515 buf_alloc->s_buf.self.high = shared_buf - pf->dv_buf_size;
3516 buf_alloc->s_buf.self.low = buf_alloc->s_buf.self.high
3517 - roundup(aligned_mps / HNS3_BUF_DIV_BY,
3518 HNS3_BUF_SIZE_UNIT);
3520 buf_alloc->s_buf.self.high =
3521 aligned_mps + HNS3_NON_DCB_ADDITIONAL_BUF;
3522 buf_alloc->s_buf.self.low = aligned_mps;
3525 if (hns3_dev_dcb_supported(hw)) {
3526 hi_thrd = shared_buf - pf->dv_buf_size;
3528 if (tc_num <= NEED_RESERVE_TC_NUM)
3529 hi_thrd = hi_thrd * BUF_RESERVE_PERCENT /
3533 hi_thrd = hi_thrd / tc_num;
3535 hi_thrd = RTE_MAX(hi_thrd, HNS3_BUF_MUL_BY * aligned_mps);
3536 hi_thrd = rounddown(hi_thrd, HNS3_BUF_SIZE_UNIT);
3537 lo_thrd = hi_thrd - aligned_mps / HNS3_BUF_DIV_BY;
3539 hi_thrd = aligned_mps + HNS3_NON_DCB_ADDITIONAL_BUF;
3540 lo_thrd = aligned_mps;
3543 for (i = 0; i < HNS3_MAX_TC_NUM; i++) {
3544 buf_alloc->s_buf.tc_thrd[i].low = lo_thrd;
3545 buf_alloc->s_buf.tc_thrd[i].high = hi_thrd;
3552 hns3_rx_buf_calc_all(struct hns3_hw *hw, bool max,
3553 struct hns3_pkt_buf_alloc *buf_alloc)
3555 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
3556 struct hns3_pf *pf = &hns->pf;
3557 struct hns3_priv_buf *priv;
3558 uint32_t aligned_mps;
3562 rx_all = pf->pkt_buf_size - hns3_get_tx_buff_alloced(buf_alloc);
3563 aligned_mps = roundup(pf->mps, HNS3_BUF_SIZE_UNIT);
3565 for (i = 0; i < HNS3_MAX_TC_NUM; i++) {
3566 priv = &buf_alloc->priv_buf[i];
3573 if (!(hw->hw_tc_map & BIT(i)))
3577 if (hw->dcb_info.hw_pfc_map & BIT(i)) {
3578 priv->wl.low = max ? aligned_mps : HNS3_BUF_SIZE_UNIT;
3579 priv->wl.high = roundup(priv->wl.low + aligned_mps,
3580 HNS3_BUF_SIZE_UNIT);
3583 priv->wl.high = max ? (aligned_mps * HNS3_BUF_MUL_BY) :
3587 priv->buf_size = priv->wl.high + pf->dv_buf_size;
3590 return hns3_is_rx_buf_ok(hw, buf_alloc, rx_all);
3594 hns3_drop_nopfc_buf_till_fit(struct hns3_hw *hw,
3595 struct hns3_pkt_buf_alloc *buf_alloc)
3597 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
3598 struct hns3_pf *pf = &hns->pf;
3599 struct hns3_priv_buf *priv;
3600 int no_pfc_priv_num;
3605 rx_all = pf->pkt_buf_size - hns3_get_tx_buff_alloced(buf_alloc);
3606 no_pfc_priv_num = hns3_get_no_pfc_priv_num(hw, buf_alloc);
3608 /* let the last to be cleared first */
3609 for (i = HNS3_MAX_TC_NUM - 1; i >= 0; i--) {
3610 priv = &buf_alloc->priv_buf[i];
3611 mask = BIT((uint8_t)i);
3613 if (hw->hw_tc_map & mask &&
3614 !(hw->dcb_info.hw_pfc_map & mask)) {
3615 /* Clear the no pfc TC private buffer */
3623 if (hns3_is_rx_buf_ok(hw, buf_alloc, rx_all) ||
3624 no_pfc_priv_num == 0)
3628 return hns3_is_rx_buf_ok(hw, buf_alloc, rx_all);
3632 hns3_drop_pfc_buf_till_fit(struct hns3_hw *hw,
3633 struct hns3_pkt_buf_alloc *buf_alloc)
3635 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
3636 struct hns3_pf *pf = &hns->pf;
3637 struct hns3_priv_buf *priv;
3643 rx_all = pf->pkt_buf_size - hns3_get_tx_buff_alloced(buf_alloc);
3644 pfc_priv_num = hns3_get_pfc_priv_num(hw, buf_alloc);
3646 /* let the last to be cleared first */
3647 for (i = HNS3_MAX_TC_NUM - 1; i >= 0; i--) {
3648 priv = &buf_alloc->priv_buf[i];
3649 mask = BIT((uint8_t)i);
3650 if (hw->hw_tc_map & mask && hw->dcb_info.hw_pfc_map & mask) {
3651 /* Reduce the number of pfc TC with private buffer */
3658 if (hns3_is_rx_buf_ok(hw, buf_alloc, rx_all) ||
3663 return hns3_is_rx_buf_ok(hw, buf_alloc, rx_all);
3667 hns3_only_alloc_priv_buff(struct hns3_hw *hw,
3668 struct hns3_pkt_buf_alloc *buf_alloc)
3670 #define COMPENSATE_BUFFER 0x3C00
3671 #define COMPENSATE_HALF_MPS_NUM 5
3672 #define PRIV_WL_GAP 0x1800
3673 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
3674 struct hns3_pf *pf = &hns->pf;
3675 uint32_t tc_num = hns3_get_tc_num(hw);
3676 uint32_t half_mps = pf->mps >> 1;
3677 struct hns3_priv_buf *priv;
3678 uint32_t min_rx_priv;
3682 rx_priv = pf->pkt_buf_size - hns3_get_tx_buff_alloced(buf_alloc);
3684 rx_priv = rx_priv / tc_num;
3686 if (tc_num <= NEED_RESERVE_TC_NUM)
3687 rx_priv = rx_priv * BUF_RESERVE_PERCENT / BUF_MAX_PERCENT;
3690 * Minimum value of private buffer in rx direction (min_rx_priv) is
3691 * equal to "DV + 2.5 * MPS + 15KB". Driver only allocates rx private
3692 * buffer if rx_priv is greater than min_rx_priv.
3694 min_rx_priv = pf->dv_buf_size + COMPENSATE_BUFFER +
3695 COMPENSATE_HALF_MPS_NUM * half_mps;
3696 min_rx_priv = roundup(min_rx_priv, HNS3_BUF_SIZE_UNIT);
3697 rx_priv = rounddown(rx_priv, HNS3_BUF_SIZE_UNIT);
3699 if (rx_priv < min_rx_priv)
3702 for (i = 0; i < HNS3_MAX_TC_NUM; i++) {
3703 priv = &buf_alloc->priv_buf[i];
3709 if (!(hw->hw_tc_map & BIT(i)))
3713 priv->buf_size = rx_priv;
3714 priv->wl.high = rx_priv - pf->dv_buf_size;
3715 priv->wl.low = priv->wl.high - PRIV_WL_GAP;
3718 buf_alloc->s_buf.buf_size = 0;
3724 * hns3_rx_buffer_calc: calculate the rx private buffer size for all TCs
3725 * @hw: pointer to struct hns3_hw
3726 * @buf_alloc: pointer to buffer calculation data
3727 * @return: 0: calculate sucessful, negative: fail
3730 hns3_rx_buffer_calc(struct hns3_hw *hw, struct hns3_pkt_buf_alloc *buf_alloc)
3732 /* When DCB is not supported, rx private buffer is not allocated. */
3733 if (!hns3_dev_dcb_supported(hw)) {
3734 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
3735 struct hns3_pf *pf = &hns->pf;
3736 uint32_t rx_all = pf->pkt_buf_size;
3738 rx_all -= hns3_get_tx_buff_alloced(buf_alloc);
3739 if (!hns3_is_rx_buf_ok(hw, buf_alloc, rx_all))
3746 * Try to allocate privated packet buffer for all TCs without share
3749 if (hns3_only_alloc_priv_buff(hw, buf_alloc))
3753 * Try to allocate privated packet buffer for all TCs with share
3756 if (hns3_rx_buf_calc_all(hw, true, buf_alloc))
3760 * For different application scenes, the enabled port number, TC number
3761 * and no_drop TC number are different. In order to obtain the better
3762 * performance, software could allocate the buffer size and configure
3763 * the waterline by tring to decrease the private buffer size according
3764 * to the order, namely, waterline of valided tc, pfc disabled tc, pfc
3767 if (hns3_rx_buf_calc_all(hw, false, buf_alloc))
3770 if (hns3_drop_nopfc_buf_till_fit(hw, buf_alloc))
3773 if (hns3_drop_pfc_buf_till_fit(hw, buf_alloc))
3780 hns3_rx_priv_buf_alloc(struct hns3_hw *hw, struct hns3_pkt_buf_alloc *buf_alloc)
3782 struct hns3_rx_priv_buff_cmd *req;
3783 struct hns3_cmd_desc desc;
3788 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_RX_PRIV_BUFF_ALLOC, false);
3789 req = (struct hns3_rx_priv_buff_cmd *)desc.data;
3791 /* Alloc private buffer TCs */
3792 for (i = 0; i < HNS3_MAX_TC_NUM; i++) {
3793 struct hns3_priv_buf *priv = &buf_alloc->priv_buf[i];
3796 rte_cpu_to_le_16(priv->buf_size >> HNS3_BUF_UNIT_S);
3797 req->buf_num[i] |= rte_cpu_to_le_16(1 << HNS3_TC0_PRI_BUF_EN_B);
3800 buf_size = buf_alloc->s_buf.buf_size;
3801 req->shared_buf = rte_cpu_to_le_16((buf_size >> HNS3_BUF_UNIT_S) |
3802 (1 << HNS3_TC0_PRI_BUF_EN_B));
3804 ret = hns3_cmd_send(hw, &desc, 1);
3806 PMD_INIT_LOG(ERR, "rx private buffer alloc cmd failed %d", ret);
3812 hns3_rx_priv_wl_config(struct hns3_hw *hw, struct hns3_pkt_buf_alloc *buf_alloc)
3814 #define HNS3_RX_PRIV_WL_ALLOC_DESC_NUM 2
3815 struct hns3_rx_priv_wl_buf *req;
3816 struct hns3_priv_buf *priv;
3817 struct hns3_cmd_desc desc[HNS3_RX_PRIV_WL_ALLOC_DESC_NUM];
3821 for (i = 0; i < HNS3_RX_PRIV_WL_ALLOC_DESC_NUM; i++) {
3822 hns3_cmd_setup_basic_desc(&desc[i], HNS3_OPC_RX_PRIV_WL_ALLOC,
3824 req = (struct hns3_rx_priv_wl_buf *)desc[i].data;
3826 /* The first descriptor set the NEXT bit to 1 */
3828 desc[i].flag |= rte_cpu_to_le_16(HNS3_CMD_FLAG_NEXT);
3830 desc[i].flag &= ~rte_cpu_to_le_16(HNS3_CMD_FLAG_NEXT);
3832 for (j = 0; j < HNS3_TC_NUM_ONE_DESC; j++) {
3833 uint32_t idx = i * HNS3_TC_NUM_ONE_DESC + j;
3835 priv = &buf_alloc->priv_buf[idx];
3836 req->tc_wl[j].high = rte_cpu_to_le_16(priv->wl.high >>
3838 req->tc_wl[j].high |=
3839 rte_cpu_to_le_16(BIT(HNS3_RX_PRIV_EN_B));
3840 req->tc_wl[j].low = rte_cpu_to_le_16(priv->wl.low >>
3842 req->tc_wl[j].low |=
3843 rte_cpu_to_le_16(BIT(HNS3_RX_PRIV_EN_B));
3847 /* Send 2 descriptor at one time */
3848 ret = hns3_cmd_send(hw, desc, HNS3_RX_PRIV_WL_ALLOC_DESC_NUM);
3850 PMD_INIT_LOG(ERR, "rx private waterline config cmd failed %d",
3856 hns3_common_thrd_config(struct hns3_hw *hw,
3857 struct hns3_pkt_buf_alloc *buf_alloc)
3859 #define HNS3_RX_COM_THRD_ALLOC_DESC_NUM 2
3860 struct hns3_shared_buf *s_buf = &buf_alloc->s_buf;
3861 struct hns3_rx_com_thrd *req;
3862 struct hns3_cmd_desc desc[HNS3_RX_COM_THRD_ALLOC_DESC_NUM];
3863 struct hns3_tc_thrd *tc;
3868 for (i = 0; i < HNS3_RX_COM_THRD_ALLOC_DESC_NUM; i++) {
3869 hns3_cmd_setup_basic_desc(&desc[i], HNS3_OPC_RX_COM_THRD_ALLOC,
3871 req = (struct hns3_rx_com_thrd *)&desc[i].data;
3873 /* The first descriptor set the NEXT bit to 1 */
3875 desc[i].flag |= rte_cpu_to_le_16(HNS3_CMD_FLAG_NEXT);
3877 desc[i].flag &= ~rte_cpu_to_le_16(HNS3_CMD_FLAG_NEXT);
3879 for (j = 0; j < HNS3_TC_NUM_ONE_DESC; j++) {
3880 tc_idx = i * HNS3_TC_NUM_ONE_DESC + j;
3881 tc = &s_buf->tc_thrd[tc_idx];
3883 req->com_thrd[j].high =
3884 rte_cpu_to_le_16(tc->high >> HNS3_BUF_UNIT_S);
3885 req->com_thrd[j].high |=
3886 rte_cpu_to_le_16(BIT(HNS3_RX_PRIV_EN_B));
3887 req->com_thrd[j].low =
3888 rte_cpu_to_le_16(tc->low >> HNS3_BUF_UNIT_S);
3889 req->com_thrd[j].low |=
3890 rte_cpu_to_le_16(BIT(HNS3_RX_PRIV_EN_B));
3894 /* Send 2 descriptors at one time */
3895 ret = hns3_cmd_send(hw, desc, HNS3_RX_COM_THRD_ALLOC_DESC_NUM);
3897 PMD_INIT_LOG(ERR, "common threshold config cmd failed %d", ret);
3903 hns3_common_wl_config(struct hns3_hw *hw, struct hns3_pkt_buf_alloc *buf_alloc)
3905 struct hns3_shared_buf *buf = &buf_alloc->s_buf;
3906 struct hns3_rx_com_wl *req;
3907 struct hns3_cmd_desc desc;
3910 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_RX_COM_WL_ALLOC, false);
3912 req = (struct hns3_rx_com_wl *)desc.data;
3913 req->com_wl.high = rte_cpu_to_le_16(buf->self.high >> HNS3_BUF_UNIT_S);
3914 req->com_wl.high |= rte_cpu_to_le_16(BIT(HNS3_RX_PRIV_EN_B));
3916 req->com_wl.low = rte_cpu_to_le_16(buf->self.low >> HNS3_BUF_UNIT_S);
3917 req->com_wl.low |= rte_cpu_to_le_16(BIT(HNS3_RX_PRIV_EN_B));
3919 ret = hns3_cmd_send(hw, &desc, 1);
3921 PMD_INIT_LOG(ERR, "common waterline config cmd failed %d", ret);
3927 hns3_buffer_alloc(struct hns3_hw *hw)
3929 struct hns3_pkt_buf_alloc pkt_buf;
3932 memset(&pkt_buf, 0, sizeof(pkt_buf));
3933 ret = hns3_tx_buffer_calc(hw, &pkt_buf);
3936 "could not calc tx buffer size for all TCs %d",
3941 ret = hns3_tx_buffer_alloc(hw, &pkt_buf);
3943 PMD_INIT_LOG(ERR, "could not alloc tx buffers %d", ret);
3947 ret = hns3_rx_buffer_calc(hw, &pkt_buf);
3950 "could not calc rx priv buffer size for all TCs %d",
3955 ret = hns3_rx_priv_buf_alloc(hw, &pkt_buf);
3957 PMD_INIT_LOG(ERR, "could not alloc rx priv buffer %d", ret);
3961 if (hns3_dev_dcb_supported(hw)) {
3962 ret = hns3_rx_priv_wl_config(hw, &pkt_buf);
3965 "could not configure rx private waterline %d",
3970 ret = hns3_common_thrd_config(hw, &pkt_buf);
3973 "could not configure common threshold %d",
3979 ret = hns3_common_wl_config(hw, &pkt_buf);
3981 PMD_INIT_LOG(ERR, "could not configure common waterline %d",
3988 hns3_firmware_compat_config(struct hns3_hw *hw, bool is_init)
3990 struct hns3_firmware_compat_cmd *req;
3991 struct hns3_cmd_desc desc;
3992 uint32_t compat = 0;
3994 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_FIRMWARE_COMPAT_CFG, false);
3995 req = (struct hns3_firmware_compat_cmd *)desc.data;
3998 hns3_set_bit(compat, HNS3_LINK_EVENT_REPORT_EN_B, 1);
3999 hns3_set_bit(compat, HNS3_NCSI_ERROR_REPORT_EN_B, 0);
4000 if (hw->mac.media_type == HNS3_MEDIA_TYPE_COPPER)
4001 hns3_set_bit(compat, HNS3_FIRMWARE_PHY_DRIVER_EN_B, 1);
4004 req->compat = rte_cpu_to_le_32(compat);
4006 return hns3_cmd_send(hw, &desc, 1);
4010 hns3_mac_init(struct hns3_hw *hw)
4012 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
4013 struct hns3_mac *mac = &hw->mac;
4014 struct hns3_pf *pf = &hns->pf;
4017 pf->support_sfp_query = true;
4018 mac->link_duplex = ETH_LINK_FULL_DUPLEX;
4019 ret = hns3_cfg_mac_speed_dup_hw(hw, mac->link_speed, mac->link_duplex);
4021 PMD_INIT_LOG(ERR, "Config mac speed dup fail ret = %d", ret);
4025 mac->link_status = ETH_LINK_DOWN;
4027 return hns3_config_mtu(hw, pf->mps);
4031 hns3_get_mac_ethertype_cmd_status(uint16_t cmdq_resp, uint8_t resp_code)
4033 #define HNS3_ETHERTYPE_SUCCESS_ADD 0
4034 #define HNS3_ETHERTYPE_ALREADY_ADD 1
4035 #define HNS3_ETHERTYPE_MGR_TBL_OVERFLOW 2
4036 #define HNS3_ETHERTYPE_KEY_CONFLICT 3
4041 "cmdq execute failed for get_mac_ethertype_cmd_status, status=%u.\n",
4046 switch (resp_code) {
4047 case HNS3_ETHERTYPE_SUCCESS_ADD:
4048 case HNS3_ETHERTYPE_ALREADY_ADD:
4051 case HNS3_ETHERTYPE_MGR_TBL_OVERFLOW:
4053 "add mac ethertype failed for manager table overflow.");
4054 return_status = -EIO;
4056 case HNS3_ETHERTYPE_KEY_CONFLICT:
4057 PMD_INIT_LOG(ERR, "add mac ethertype failed for key conflict.");
4058 return_status = -EIO;
4062 "add mac ethertype failed for undefined, code=%u.",
4064 return_status = -EIO;
4068 return return_status;
4072 hns3_add_mgr_tbl(struct hns3_hw *hw,
4073 const struct hns3_mac_mgr_tbl_entry_cmd *req)
4075 struct hns3_cmd_desc desc;
4080 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_MAC_ETHTYPE_ADD, false);
4081 memcpy(desc.data, req, sizeof(struct hns3_mac_mgr_tbl_entry_cmd));
4083 ret = hns3_cmd_send(hw, &desc, 1);
4086 "add mac ethertype failed for cmd_send, ret =%d.",
4091 resp_code = (rte_le_to_cpu_32(desc.data[0]) >> 8) & 0xff;
4092 retval = rte_le_to_cpu_16(desc.retval);
4094 return hns3_get_mac_ethertype_cmd_status(retval, resp_code);
4098 hns3_prepare_mgr_tbl(struct hns3_mac_mgr_tbl_entry_cmd *mgr_table,
4099 int *table_item_num)
4101 struct hns3_mac_mgr_tbl_entry_cmd *tbl;
4104 * In current version, we add one item in management table as below:
4105 * 0x0180C200000E -- LLDP MC address
4108 tbl->flags = HNS3_MAC_MGR_MASK_VLAN_B;
4109 tbl->ethter_type = rte_cpu_to_le_16(HNS3_MAC_ETHERTYPE_LLDP);
4110 tbl->mac_addr_hi32 = rte_cpu_to_le_32(htonl(0x0180C200));
4111 tbl->mac_addr_lo16 = rte_cpu_to_le_16(htons(0x000E));
4112 tbl->i_port_bitmap = 0x1;
4113 *table_item_num = 1;
4117 hns3_init_mgr_tbl(struct hns3_hw *hw)
4119 #define HNS_MAC_MGR_TBL_MAX_SIZE 16
4120 struct hns3_mac_mgr_tbl_entry_cmd mgr_table[HNS_MAC_MGR_TBL_MAX_SIZE];
4125 memset(mgr_table, 0, sizeof(mgr_table));
4126 hns3_prepare_mgr_tbl(mgr_table, &table_item_num);
4127 for (i = 0; i < table_item_num; i++) {
4128 ret = hns3_add_mgr_tbl(hw, &mgr_table[i]);
4130 PMD_INIT_LOG(ERR, "add mac ethertype failed, ret =%d",
4140 hns3_promisc_param_init(struct hns3_promisc_param *param, bool en_uc,
4141 bool en_mc, bool en_bc, int vport_id)
4146 memset(param, 0, sizeof(struct hns3_promisc_param));
4148 param->enable = HNS3_PROMISC_EN_UC;
4150 param->enable |= HNS3_PROMISC_EN_MC;
4152 param->enable |= HNS3_PROMISC_EN_BC;
4153 param->vf_id = vport_id;
4157 hns3_cmd_set_promisc_mode(struct hns3_hw *hw, struct hns3_promisc_param *param)
4159 struct hns3_promisc_cfg_cmd *req;
4160 struct hns3_cmd_desc desc;
4163 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_CFG_PROMISC_MODE, false);
4165 req = (struct hns3_promisc_cfg_cmd *)desc.data;
4166 req->vf_id = param->vf_id;
4167 req->flag = (param->enable << HNS3_PROMISC_EN_B) |
4168 HNS3_PROMISC_TX_EN_B | HNS3_PROMISC_RX_EN_B;
4170 ret = hns3_cmd_send(hw, &desc, 1);
4172 PMD_INIT_LOG(ERR, "Set promisc mode fail, ret = %d", ret);
4178 hns3_set_promisc_mode(struct hns3_hw *hw, bool en_uc_pmc, bool en_mc_pmc)
4180 struct hns3_promisc_param param;
4181 bool en_bc_pmc = true;
4185 * In current version VF is not supported when PF is driven by DPDK
4186 * driver, just need to configure parameters for PF vport.
4188 vf_id = HNS3_PF_FUNC_ID;
4190 hns3_promisc_param_init(¶m, en_uc_pmc, en_mc_pmc, en_bc_pmc, vf_id);
4191 return hns3_cmd_set_promisc_mode(hw, ¶m);
4195 hns3_promisc_init(struct hns3_hw *hw)
4197 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
4198 struct hns3_pf *pf = &hns->pf;
4199 struct hns3_promisc_param param;
4203 ret = hns3_set_promisc_mode(hw, false, false);
4205 PMD_INIT_LOG(ERR, "failed to set promisc mode, ret = %d", ret);
4210 * In current version VFs are not supported when PF is driven by DPDK
4211 * driver. After PF has been taken over by DPDK, the original VF will
4212 * be invalid. So, there is a possibility of entry residues. It should
4213 * clear VFs's promisc mode to avoid unnecessary bandwidth usage
4216 for (func_id = HNS3_1ST_VF_FUNC_ID; func_id < pf->func_num; func_id++) {
4217 hns3_promisc_param_init(¶m, false, false, false, func_id);
4218 ret = hns3_cmd_set_promisc_mode(hw, ¶m);
4220 PMD_INIT_LOG(ERR, "failed to clear vf:%u promisc mode,"
4221 " ret = %d", func_id, ret);
4230 hns3_promisc_uninit(struct hns3_hw *hw)
4232 struct hns3_promisc_param param;
4236 func_id = HNS3_PF_FUNC_ID;
4239 * In current version VFs are not supported when PF is driven by
4240 * DPDK driver, and VFs' promisc mode status has been cleared during
4241 * init and their status will not change. So just clear PF's promisc
4242 * mode status during uninit.
4244 hns3_promisc_param_init(¶m, false, false, false, func_id);
4245 ret = hns3_cmd_set_promisc_mode(hw, ¶m);
4247 PMD_INIT_LOG(ERR, "failed to clear promisc status during"
4248 " uninit, ret = %d", ret);
4252 hns3_dev_promiscuous_enable(struct rte_eth_dev *dev)
4254 bool allmulti = dev->data->all_multicast ? true : false;
4255 struct hns3_adapter *hns = dev->data->dev_private;
4256 struct hns3_hw *hw = &hns->hw;
4261 rte_spinlock_lock(&hw->lock);
4262 ret = hns3_set_promisc_mode(hw, true, true);
4264 rte_spinlock_unlock(&hw->lock);
4265 hns3_err(hw, "failed to enable promiscuous mode, ret = %d",
4271 * When promiscuous mode was enabled, disable the vlan filter to let
4272 * all packets coming in in the receiving direction.
4274 offloads = dev->data->dev_conf.rxmode.offloads;
4275 if (offloads & DEV_RX_OFFLOAD_VLAN_FILTER) {
4276 ret = hns3_enable_vlan_filter(hns, false);
4278 hns3_err(hw, "failed to enable promiscuous mode due to "
4279 "failure to disable vlan filter, ret = %d",
4281 err = hns3_set_promisc_mode(hw, false, allmulti);
4283 hns3_err(hw, "failed to restore promiscuous "
4284 "status after disable vlan filter "
4285 "failed during enabling promiscuous "
4286 "mode, ret = %d", ret);
4290 rte_spinlock_unlock(&hw->lock);
4296 hns3_dev_promiscuous_disable(struct rte_eth_dev *dev)
4298 bool allmulti = dev->data->all_multicast ? true : false;
4299 struct hns3_adapter *hns = dev->data->dev_private;
4300 struct hns3_hw *hw = &hns->hw;
4305 /* If now in all_multicast mode, must remain in all_multicast mode. */
4306 rte_spinlock_lock(&hw->lock);
4307 ret = hns3_set_promisc_mode(hw, false, allmulti);
4309 rte_spinlock_unlock(&hw->lock);
4310 hns3_err(hw, "failed to disable promiscuous mode, ret = %d",
4314 /* when promiscuous mode was disabled, restore the vlan filter status */
4315 offloads = dev->data->dev_conf.rxmode.offloads;
4316 if (offloads & DEV_RX_OFFLOAD_VLAN_FILTER) {
4317 ret = hns3_enable_vlan_filter(hns, true);
4319 hns3_err(hw, "failed to disable promiscuous mode due to"
4320 " failure to restore vlan filter, ret = %d",
4322 err = hns3_set_promisc_mode(hw, true, true);
4324 hns3_err(hw, "failed to restore promiscuous "
4325 "status after enabling vlan filter "
4326 "failed during disabling promiscuous "
4327 "mode, ret = %d", ret);
4330 rte_spinlock_unlock(&hw->lock);
4336 hns3_dev_allmulticast_enable(struct rte_eth_dev *dev)
4338 struct hns3_adapter *hns = dev->data->dev_private;
4339 struct hns3_hw *hw = &hns->hw;
4342 if (dev->data->promiscuous)
4345 rte_spinlock_lock(&hw->lock);
4346 ret = hns3_set_promisc_mode(hw, false, true);
4347 rte_spinlock_unlock(&hw->lock);
4349 hns3_err(hw, "failed to enable allmulticast mode, ret = %d",
4356 hns3_dev_allmulticast_disable(struct rte_eth_dev *dev)
4358 struct hns3_adapter *hns = dev->data->dev_private;
4359 struct hns3_hw *hw = &hns->hw;
4362 /* If now in promiscuous mode, must remain in all_multicast mode. */
4363 if (dev->data->promiscuous)
4366 rte_spinlock_lock(&hw->lock);
4367 ret = hns3_set_promisc_mode(hw, false, false);
4368 rte_spinlock_unlock(&hw->lock);
4370 hns3_err(hw, "failed to disable allmulticast mode, ret = %d",
4377 hns3_dev_promisc_restore(struct hns3_adapter *hns)
4379 struct hns3_hw *hw = &hns->hw;
4380 bool allmulti = hw->data->all_multicast ? true : false;
4383 if (hw->data->promiscuous) {
4384 ret = hns3_set_promisc_mode(hw, true, true);
4386 hns3_err(hw, "failed to restore promiscuous mode, "
4391 ret = hns3_set_promisc_mode(hw, false, allmulti);
4393 hns3_err(hw, "failed to restore allmulticast mode, ret = %d",
4399 hns3_get_sfp_speed(struct hns3_hw *hw, uint32_t *speed)
4401 struct hns3_sfp_speed_cmd *resp;
4402 struct hns3_cmd_desc desc;
4405 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_SFP_GET_SPEED, true);
4406 resp = (struct hns3_sfp_speed_cmd *)desc.data;
4407 ret = hns3_cmd_send(hw, &desc, 1);
4408 if (ret == -EOPNOTSUPP) {
4409 hns3_err(hw, "IMP do not support get SFP speed %d", ret);
4412 hns3_err(hw, "get sfp speed failed %d", ret);
4416 *speed = resp->sfp_speed;
4422 hns3_check_speed_dup(uint8_t duplex, uint32_t speed)
4424 if (!(speed == ETH_SPEED_NUM_10M || speed == ETH_SPEED_NUM_100M))
4425 duplex = ETH_LINK_FULL_DUPLEX;
4431 hns3_cfg_mac_speed_dup(struct hns3_hw *hw, uint32_t speed, uint8_t duplex)
4433 struct hns3_mac *mac = &hw->mac;
4436 duplex = hns3_check_speed_dup(duplex, speed);
4437 if (mac->link_speed == speed && mac->link_duplex == duplex)
4440 ret = hns3_cfg_mac_speed_dup_hw(hw, speed, duplex);
4444 ret = hns3_port_shaper_update(hw, speed);
4448 mac->link_speed = speed;
4449 mac->link_duplex = duplex;
4455 hns3_update_fiber_link_info(struct hns3_hw *hw)
4457 struct hns3_pf *pf = HNS3_DEV_HW_TO_PF(hw);
4461 /* If IMP do not support get SFP/qSFP speed, return directly */
4462 if (!pf->support_sfp_query)
4465 ret = hns3_get_sfp_speed(hw, &speed);
4466 if (ret == -EOPNOTSUPP) {
4467 pf->support_sfp_query = false;
4472 if (speed == ETH_SPEED_NUM_NONE)
4473 return 0; /* do nothing if no SFP */
4475 /* Config full duplex for SFP */
4476 return hns3_cfg_mac_speed_dup(hw, speed, ETH_LINK_FULL_DUPLEX);
4480 hns3_parse_phy_params(struct hns3_cmd_desc *desc, struct hns3_mac *mac)
4482 struct hns3_phy_params_bd0_cmd *req;
4484 req = (struct hns3_phy_params_bd0_cmd *)desc[0].data;
4485 mac->link_speed = rte_le_to_cpu_32(req->speed);
4486 mac->link_duplex = hns3_get_bit(req->duplex,
4487 HNS3_PHY_DUPLEX_CFG_B);
4488 mac->link_autoneg = hns3_get_bit(req->autoneg,
4489 HNS3_PHY_AUTONEG_CFG_B);
4490 mac->supported_capa = rte_le_to_cpu_32(req->supported);
4491 mac->advertising = rte_le_to_cpu_32(req->advertising);
4492 mac->lp_advertising = rte_le_to_cpu_32(req->lp_advertising);
4493 mac->support_autoneg = !!(mac->supported_capa &
4494 HNS3_PHY_LINK_MODE_AUTONEG_BIT);
4498 hns3_get_phy_params(struct hns3_hw *hw, struct hns3_mac *mac)
4500 struct hns3_cmd_desc desc[HNS3_PHY_PARAM_CFG_BD_NUM];
4504 for (i = 0; i < HNS3_PHY_PARAM_CFG_BD_NUM - 1; i++) {
4505 hns3_cmd_setup_basic_desc(&desc[i], HNS3_OPC_PHY_PARAM_CFG,
4507 desc[i].flag |= rte_cpu_to_le_16(HNS3_CMD_FLAG_NEXT);
4509 hns3_cmd_setup_basic_desc(&desc[i], HNS3_OPC_PHY_PARAM_CFG, true);
4511 ret = hns3_cmd_send(hw, desc, HNS3_PHY_PARAM_CFG_BD_NUM);
4513 hns3_err(hw, "get phy parameters failed, ret = %d.", ret);
4517 hns3_parse_phy_params(desc, mac);
4523 hns3_update_phy_link_info(struct hns3_hw *hw)
4525 struct hns3_mac *mac = &hw->mac;
4526 struct hns3_mac mac_info;
4529 memset(&mac_info, 0, sizeof(struct hns3_mac));
4530 ret = hns3_get_phy_params(hw, &mac_info);
4534 if (mac_info.link_speed != mac->link_speed) {
4535 ret = hns3_port_shaper_update(hw, mac_info.link_speed);
4540 mac->link_speed = mac_info.link_speed;
4541 mac->link_duplex = mac_info.link_duplex;
4542 mac->link_autoneg = mac_info.link_autoneg;
4543 mac->supported_capa = mac_info.supported_capa;
4544 mac->advertising = mac_info.advertising;
4545 mac->lp_advertising = mac_info.lp_advertising;
4546 mac->support_autoneg = mac_info.support_autoneg;
4552 hns3_update_link_info(struct rte_eth_dev *eth_dev)
4554 struct hns3_adapter *hns = eth_dev->data->dev_private;
4555 struct hns3_hw *hw = &hns->hw;
4558 if (hw->mac.media_type == HNS3_MEDIA_TYPE_COPPER)
4559 ret = hns3_update_phy_link_info(hw);
4560 else if (hw->mac.media_type == HNS3_MEDIA_TYPE_FIBER)
4561 ret = hns3_update_fiber_link_info(hw);
4567 hns3_cfg_mac_mode(struct hns3_hw *hw, bool enable)
4569 struct hns3_config_mac_mode_cmd *req;
4570 struct hns3_cmd_desc desc;
4571 uint32_t loop_en = 0;
4575 req = (struct hns3_config_mac_mode_cmd *)desc.data;
4577 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_CONFIG_MAC_MODE, false);
4580 hns3_set_bit(loop_en, HNS3_MAC_TX_EN_B, val);
4581 hns3_set_bit(loop_en, HNS3_MAC_RX_EN_B, val);
4582 hns3_set_bit(loop_en, HNS3_MAC_PAD_TX_B, val);
4583 hns3_set_bit(loop_en, HNS3_MAC_PAD_RX_B, val);
4584 hns3_set_bit(loop_en, HNS3_MAC_1588_TX_B, 0);
4585 hns3_set_bit(loop_en, HNS3_MAC_1588_RX_B, 0);
4586 hns3_set_bit(loop_en, HNS3_MAC_APP_LP_B, 0);
4587 hns3_set_bit(loop_en, HNS3_MAC_LINE_LP_B, 0);
4588 hns3_set_bit(loop_en, HNS3_MAC_FCS_TX_B, val);
4589 hns3_set_bit(loop_en, HNS3_MAC_RX_FCS_B, val);
4592 * If DEV_RX_OFFLOAD_KEEP_CRC offload is set, MAC will not strip CRC
4593 * when receiving frames. Otherwise, CRC will be stripped.
4595 if (hw->data->dev_conf.rxmode.offloads & DEV_RX_OFFLOAD_KEEP_CRC)
4596 hns3_set_bit(loop_en, HNS3_MAC_RX_FCS_STRIP_B, 0);
4598 hns3_set_bit(loop_en, HNS3_MAC_RX_FCS_STRIP_B, val);
4599 hns3_set_bit(loop_en, HNS3_MAC_TX_OVERSIZE_TRUNCATE_B, val);
4600 hns3_set_bit(loop_en, HNS3_MAC_RX_OVERSIZE_TRUNCATE_B, val);
4601 hns3_set_bit(loop_en, HNS3_MAC_TX_UNDER_MIN_ERR_B, val);
4602 req->txrx_pad_fcs_loop_en = rte_cpu_to_le_32(loop_en);
4604 ret = hns3_cmd_send(hw, &desc, 1);
4606 PMD_INIT_LOG(ERR, "mac enable fail, ret =%d.", ret);
4612 hns3_get_mac_link_status(struct hns3_hw *hw)
4614 struct hns3_link_status_cmd *req;
4615 struct hns3_cmd_desc desc;
4619 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_QUERY_LINK_STATUS, true);
4620 ret = hns3_cmd_send(hw, &desc, 1);
4622 hns3_err(hw, "get link status cmd failed %d", ret);
4623 return ETH_LINK_DOWN;
4626 req = (struct hns3_link_status_cmd *)desc.data;
4627 link_status = req->status & HNS3_LINK_STATUS_UP_M;
4629 return !!link_status;
4633 hns3_update_link_status(struct hns3_hw *hw)
4637 state = hns3_get_mac_link_status(hw);
4638 if (state != hw->mac.link_status) {
4639 hw->mac.link_status = state;
4640 hns3_warn(hw, "Link status change to %s!", state ? "up" : "down");
4648 * Current, the PF driver get link status by two ways:
4649 * 1) Periodic polling in the intr thread context, driver call
4650 * hns3_update_link_status to update link status.
4651 * 2) Firmware report async interrupt, driver process the event in the intr
4652 * thread context, and call hns3_update_link_status to update link status.
4654 * If detect link status changed, driver need report LSE. One method is add the
4655 * report LSE logic in hns3_update_link_status.
4657 * But the PF driver ops(link_update) also call hns3_update_link_status to
4658 * update link status.
4659 * If we report LSE in hns3_update_link_status, it may lead to deadlock in the
4660 * bonding application.
4662 * So add the one new API which used only in intr thread context.
4665 hns3_update_link_status_and_event(struct hns3_hw *hw)
4667 struct rte_eth_dev *dev = &rte_eth_devices[hw->data->port_id];
4668 bool changed = hns3_update_link_status(hw);
4670 rte_eth_dev_callback_process(dev, RTE_ETH_EVENT_INTR_LSC, NULL);
4674 hns3_service_handler(void *param)
4676 struct rte_eth_dev *eth_dev = (struct rte_eth_dev *)param;
4677 struct hns3_adapter *hns = eth_dev->data->dev_private;
4678 struct hns3_hw *hw = &hns->hw;
4680 if (!hns3_is_reset_pending(hns)) {
4681 hns3_update_link_status_and_event(hw);
4682 hns3_update_link_info(eth_dev);
4684 hns3_warn(hw, "Cancel the query when reset is pending");
4687 rte_eal_alarm_set(HNS3_SERVICE_INTERVAL, hns3_service_handler, eth_dev);
4691 hns3_init_hardware(struct hns3_adapter *hns)
4693 struct hns3_hw *hw = &hns->hw;
4696 ret = hns3_map_tqp(hw);
4698 PMD_INIT_LOG(ERR, "Failed to map tqp: %d", ret);
4702 ret = hns3_init_umv_space(hw);
4704 PMD_INIT_LOG(ERR, "Failed to init umv space: %d", ret);
4708 ret = hns3_mac_init(hw);
4710 PMD_INIT_LOG(ERR, "Failed to init MAC: %d", ret);
4714 ret = hns3_init_mgr_tbl(hw);
4716 PMD_INIT_LOG(ERR, "Failed to init manager table: %d", ret);
4720 ret = hns3_promisc_init(hw);
4722 PMD_INIT_LOG(ERR, "Failed to init promisc: %d",
4727 ret = hns3_init_vlan_config(hns);
4729 PMD_INIT_LOG(ERR, "Failed to init vlan: %d", ret);
4733 ret = hns3_dcb_init(hw);
4735 PMD_INIT_LOG(ERR, "Failed to init dcb: %d", ret);
4739 ret = hns3_init_fd_config(hns);
4741 PMD_INIT_LOG(ERR, "Failed to init flow director: %d", ret);
4745 ret = hns3_config_tso(hw, HNS3_TSO_MSS_MIN, HNS3_TSO_MSS_MAX);
4747 PMD_INIT_LOG(ERR, "Failed to config tso: %d", ret);
4751 ret = hns3_config_gro(hw, false);
4753 PMD_INIT_LOG(ERR, "Failed to config gro: %d", ret);
4758 * In the initialization clearing the all hardware mapping relationship
4759 * configurations between queues and interrupt vectors is needed, so
4760 * some error caused by the residual configurations, such as the
4761 * unexpected interrupt, can be avoid.
4763 ret = hns3_init_ring_with_vector(hw);
4765 PMD_INIT_LOG(ERR, "Failed to init ring intr vector: %d", ret);
4770 * Requiring firmware to enable some features, driver can
4771 * still work without it.
4773 ret = hns3_firmware_compat_config(hw, true);
4775 PMD_INIT_LOG(WARNING, "firmware compatible features not "
4776 "supported, ret = %d.", ret);
4781 hns3_uninit_umv_space(hw);
4786 hns3_clear_hw(struct hns3_hw *hw)
4788 struct hns3_cmd_desc desc;
4791 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_CLEAR_HW_STATE, false);
4793 ret = hns3_cmd_send(hw, &desc, 1);
4794 if (ret && ret != -EOPNOTSUPP)
4801 hns3_config_all_msix_error(struct hns3_hw *hw, bool enable)
4806 * The new firmware support report more hardware error types by
4807 * msix mode. These errors are defined as RAS errors in hardware
4808 * and belong to a different type from the MSI-x errors processed
4809 * by the network driver.
4811 * Network driver should open the new error report on initialition
4813 val = hns3_read_dev(hw, HNS3_VECTOR0_OTER_EN_REG);
4814 hns3_set_bit(val, HNS3_VECTOR0_ALL_MSIX_ERR_B, enable ? 1 : 0);
4815 hns3_write_dev(hw, HNS3_VECTOR0_OTER_EN_REG, val);
4819 hns3_init_pf(struct rte_eth_dev *eth_dev)
4821 struct rte_device *dev = eth_dev->device;
4822 struct rte_pci_device *pci_dev = RTE_DEV_TO_PCI(dev);
4823 struct hns3_adapter *hns = eth_dev->data->dev_private;
4824 struct hns3_hw *hw = &hns->hw;
4827 PMD_INIT_FUNC_TRACE();
4829 /* Get hardware io base address from pcie BAR2 IO space */
4830 hw->io_base = pci_dev->mem_resource[2].addr;
4832 /* Firmware command queue initialize */
4833 ret = hns3_cmd_init_queue(hw);
4835 PMD_INIT_LOG(ERR, "Failed to init cmd queue: %d", ret);
4836 goto err_cmd_init_queue;
4839 hns3_clear_all_event_cause(hw);
4841 /* Firmware command initialize */
4842 ret = hns3_cmd_init(hw);
4844 PMD_INIT_LOG(ERR, "Failed to init cmd: %d", ret);
4849 * To ensure that the hardware environment is clean during
4850 * initialization, the driver actively clear the hardware environment
4851 * during initialization, including PF and corresponding VFs' vlan, mac,
4852 * flow table configurations, etc.
4854 ret = hns3_clear_hw(hw);
4856 PMD_INIT_LOG(ERR, "failed to clear hardware: %d", ret);
4860 /* Hardware statistics of imissed registers cleared. */
4861 ret = hns3_update_imissed_stats(hw, true);
4863 hns3_err(hw, "clear imissed stats failed, ret = %d", ret);
4867 hns3_config_all_msix_error(hw, true);
4869 ret = rte_intr_callback_register(&pci_dev->intr_handle,
4870 hns3_interrupt_handler,
4873 PMD_INIT_LOG(ERR, "Failed to register intr: %d", ret);
4874 goto err_intr_callback_register;
4877 /* Enable interrupt */
4878 rte_intr_enable(&pci_dev->intr_handle);
4879 hns3_pf_enable_irq0(hw);
4881 /* Get configuration */
4882 ret = hns3_get_configuration(hw);
4884 PMD_INIT_LOG(ERR, "Failed to fetch configuration: %d", ret);
4885 goto err_get_config;
4888 ret = hns3_tqp_stats_init(hw);
4890 goto err_get_config;
4892 ret = hns3_init_hardware(hns);
4894 PMD_INIT_LOG(ERR, "Failed to init hardware: %d", ret);
4898 /* Initialize flow director filter list & hash */
4899 ret = hns3_fdir_filter_init(hns);
4901 PMD_INIT_LOG(ERR, "Failed to alloc hashmap for fdir: %d", ret);
4905 hns3_rss_set_default_args(hw);
4907 ret = hns3_enable_hw_error_intr(hns, true);
4909 PMD_INIT_LOG(ERR, "fail to enable hw error interrupts: %d",
4911 goto err_enable_intr;
4914 hns3_tm_conf_init(eth_dev);
4919 hns3_fdir_filter_uninit(hns);
4921 (void)hns3_firmware_compat_config(hw, false);
4922 hns3_uninit_umv_space(hw);
4924 hns3_tqp_stats_uninit(hw);
4926 hns3_pf_disable_irq0(hw);
4927 rte_intr_disable(&pci_dev->intr_handle);
4928 hns3_intr_unregister(&pci_dev->intr_handle, hns3_interrupt_handler,
4930 err_intr_callback_register:
4932 hns3_cmd_uninit(hw);
4933 hns3_cmd_destroy_queue(hw);
4941 hns3_uninit_pf(struct rte_eth_dev *eth_dev)
4943 struct hns3_adapter *hns = eth_dev->data->dev_private;
4944 struct rte_device *dev = eth_dev->device;
4945 struct rte_pci_device *pci_dev = RTE_DEV_TO_PCI(dev);
4946 struct hns3_hw *hw = &hns->hw;
4948 PMD_INIT_FUNC_TRACE();
4950 hns3_tm_conf_uninit(eth_dev);
4951 hns3_enable_hw_error_intr(hns, false);
4952 hns3_rss_uninit(hns);
4953 (void)hns3_config_gro(hw, false);
4954 hns3_promisc_uninit(hw);
4955 hns3_fdir_filter_uninit(hns);
4956 (void)hns3_firmware_compat_config(hw, false);
4957 hns3_uninit_umv_space(hw);
4958 hns3_tqp_stats_uninit(hw);
4959 hns3_pf_disable_irq0(hw);
4960 rte_intr_disable(&pci_dev->intr_handle);
4961 hns3_intr_unregister(&pci_dev->intr_handle, hns3_interrupt_handler,
4963 hns3_config_all_msix_error(hw, false);
4964 hns3_cmd_uninit(hw);
4965 hns3_cmd_destroy_queue(hw);
4970 hns3_do_start(struct hns3_adapter *hns, bool reset_queue)
4972 struct hns3_hw *hw = &hns->hw;
4975 ret = hns3_dcb_cfg_update(hns);
4980 * The hns3_dcb_cfg_update may configure TM module, so
4981 * hns3_tm_conf_update must called later.
4983 ret = hns3_tm_conf_update(hw);
4985 PMD_INIT_LOG(ERR, "failed to update tm conf, ret = %d.", ret);
4989 hns3_enable_rxd_adv_layout(hw);
4991 ret = hns3_init_queues(hns, reset_queue);
4993 PMD_INIT_LOG(ERR, "failed to init queues, ret = %d.", ret);
4997 ret = hns3_cfg_mac_mode(hw, true);
4999 PMD_INIT_LOG(ERR, "failed to enable MAC, ret = %d", ret);
5000 goto err_config_mac_mode;
5004 err_config_mac_mode:
5005 hns3_dev_release_mbufs(hns);
5007 * Here is exception handling, hns3_reset_all_tqps will have the
5008 * corresponding error message if it is handled incorrectly, so it is
5009 * not necessary to check hns3_reset_all_tqps return value, here keep
5010 * ret as the error code causing the exception.
5012 (void)hns3_reset_all_tqps(hns);
5017 hns3_map_rx_interrupt(struct rte_eth_dev *dev)
5019 struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
5020 struct rte_intr_handle *intr_handle = &pci_dev->intr_handle;
5021 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
5022 uint16_t base = RTE_INTR_VEC_ZERO_OFFSET;
5023 uint16_t vec = RTE_INTR_VEC_ZERO_OFFSET;
5024 uint32_t intr_vector;
5029 * hns3 needs a separate interrupt to be used as event interrupt which
5030 * could not be shared with task queue pair, so KERNEL drivers need
5031 * support multiple interrupt vectors.
5033 if (dev->data->dev_conf.intr_conf.rxq == 0 ||
5034 !rte_intr_cap_multiple(intr_handle))
5037 rte_intr_disable(intr_handle);
5038 intr_vector = hw->used_rx_queues;
5039 /* creates event fd for each intr vector when MSIX is used */
5040 if (rte_intr_efd_enable(intr_handle, intr_vector))
5043 if (intr_handle->intr_vec == NULL) {
5044 intr_handle->intr_vec =
5045 rte_zmalloc("intr_vec",
5046 hw->used_rx_queues * sizeof(int), 0);
5047 if (intr_handle->intr_vec == NULL) {
5048 hns3_err(hw, "failed to allocate %u rx_queues intr_vec",
5049 hw->used_rx_queues);
5051 goto alloc_intr_vec_error;
5055 if (rte_intr_allow_others(intr_handle)) {
5056 vec = RTE_INTR_VEC_RXTX_OFFSET;
5057 base = RTE_INTR_VEC_RXTX_OFFSET;
5060 for (q_id = 0; q_id < hw->used_rx_queues; q_id++) {
5061 ret = hns3_bind_ring_with_vector(hw, vec, true,
5062 HNS3_RING_TYPE_RX, q_id);
5064 goto bind_vector_error;
5065 intr_handle->intr_vec[q_id] = vec;
5067 * If there are not enough efds (e.g. not enough interrupt),
5068 * remaining queues will be bond to the last interrupt.
5070 if (vec < base + intr_handle->nb_efd - 1)
5073 rte_intr_enable(intr_handle);
5077 rte_free(intr_handle->intr_vec);
5078 intr_handle->intr_vec = NULL;
5079 alloc_intr_vec_error:
5080 rte_intr_efd_disable(intr_handle);
5085 hns3_restore_rx_interrupt(struct hns3_hw *hw)
5087 struct rte_eth_dev *dev = &rte_eth_devices[hw->data->port_id];
5088 struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
5089 struct rte_intr_handle *intr_handle = &pci_dev->intr_handle;
5093 if (dev->data->dev_conf.intr_conf.rxq == 0)
5096 if (rte_intr_dp_is_en(intr_handle)) {
5097 for (q_id = 0; q_id < hw->used_rx_queues; q_id++) {
5098 ret = hns3_bind_ring_with_vector(hw,
5099 intr_handle->intr_vec[q_id], true,
5100 HNS3_RING_TYPE_RX, q_id);
5110 hns3_restore_filter(struct rte_eth_dev *dev)
5112 hns3_restore_rss_filter(dev);
5116 hns3_dev_start(struct rte_eth_dev *dev)
5118 struct hns3_adapter *hns = dev->data->dev_private;
5119 struct hns3_hw *hw = &hns->hw;
5122 PMD_INIT_FUNC_TRACE();
5123 if (__atomic_load_n(&hw->reset.resetting, __ATOMIC_RELAXED))
5126 rte_spinlock_lock(&hw->lock);
5127 hw->adapter_state = HNS3_NIC_STARTING;
5129 ret = hns3_do_start(hns, true);
5131 hw->adapter_state = HNS3_NIC_CONFIGURED;
5132 rte_spinlock_unlock(&hw->lock);
5135 ret = hns3_map_rx_interrupt(dev);
5137 hw->adapter_state = HNS3_NIC_CONFIGURED;
5138 rte_spinlock_unlock(&hw->lock);
5143 * There are three register used to control the status of a TQP
5144 * (contains a pair of Tx queue and Rx queue) in the new version network
5145 * engine. One is used to control the enabling of Tx queue, the other is
5146 * used to control the enabling of Rx queue, and the last is the master
5147 * switch used to control the enabling of the tqp. The Tx register and
5148 * TQP register must be enabled at the same time to enable a Tx queue.
5149 * The same applies to the Rx queue. For the older network engine, this
5150 * function only refresh the enabled flag, and it is used to update the
5151 * status of queue in the dpdk framework.
5153 ret = hns3_start_all_txqs(dev);
5155 hw->adapter_state = HNS3_NIC_CONFIGURED;
5156 rte_spinlock_unlock(&hw->lock);
5160 ret = hns3_start_all_rxqs(dev);
5162 hns3_stop_all_txqs(dev);
5163 hw->adapter_state = HNS3_NIC_CONFIGURED;
5164 rte_spinlock_unlock(&hw->lock);
5168 hw->adapter_state = HNS3_NIC_STARTED;
5169 rte_spinlock_unlock(&hw->lock);
5171 hns3_rx_scattered_calc(dev);
5172 hns3_set_rxtx_function(dev);
5173 hns3_mp_req_start_rxtx(dev);
5174 rte_eal_alarm_set(HNS3_SERVICE_INTERVAL, hns3_service_handler, dev);
5176 hns3_restore_filter(dev);
5178 /* Enable interrupt of all rx queues before enabling queues */
5179 hns3_dev_all_rx_queue_intr_enable(hw, true);
5182 * After finished the initialization, enable tqps to receive/transmit
5183 * packets and refresh all queue status.
5185 hns3_start_tqps(hw);
5187 hns3_tm_dev_start_proc(hw);
5189 hns3_info(hw, "hns3 dev start successful!");
5194 hns3_do_stop(struct hns3_adapter *hns)
5196 struct hns3_hw *hw = &hns->hw;
5199 ret = hns3_cfg_mac_mode(hw, false);
5202 hw->mac.link_status = ETH_LINK_DOWN;
5204 if (__atomic_load_n(&hw->reset.disable_cmd, __ATOMIC_RELAXED) == 0) {
5205 hns3_configure_all_mac_addr(hns, true);
5206 ret = hns3_reset_all_tqps(hns);
5208 hns3_err(hw, "failed to reset all queues ret = %d.",
5213 hw->mac.default_addr_setted = false;
5218 hns3_unmap_rx_interrupt(struct rte_eth_dev *dev)
5220 struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
5221 struct rte_intr_handle *intr_handle = &pci_dev->intr_handle;
5222 struct hns3_adapter *hns = dev->data->dev_private;
5223 struct hns3_hw *hw = &hns->hw;
5224 uint8_t base = RTE_INTR_VEC_ZERO_OFFSET;
5225 uint8_t vec = RTE_INTR_VEC_ZERO_OFFSET;
5228 if (dev->data->dev_conf.intr_conf.rxq == 0)
5231 /* unmap the ring with vector */
5232 if (rte_intr_allow_others(intr_handle)) {
5233 vec = RTE_INTR_VEC_RXTX_OFFSET;
5234 base = RTE_INTR_VEC_RXTX_OFFSET;
5236 if (rte_intr_dp_is_en(intr_handle)) {
5237 for (q_id = 0; q_id < hw->used_rx_queues; q_id++) {
5238 (void)hns3_bind_ring_with_vector(hw, vec, false,
5241 if (vec < base + intr_handle->nb_efd - 1)
5245 /* Clean datapath event and queue/vec mapping */
5246 rte_intr_efd_disable(intr_handle);
5247 if (intr_handle->intr_vec) {
5248 rte_free(intr_handle->intr_vec);
5249 intr_handle->intr_vec = NULL;
5254 hns3_dev_stop(struct rte_eth_dev *dev)
5256 struct hns3_adapter *hns = dev->data->dev_private;
5257 struct hns3_hw *hw = &hns->hw;
5259 PMD_INIT_FUNC_TRACE();
5260 dev->data->dev_started = 0;
5262 hw->adapter_state = HNS3_NIC_STOPPING;
5263 hns3_set_rxtx_function(dev);
5265 /* Disable datapath on secondary process. */
5266 hns3_mp_req_stop_rxtx(dev);
5267 /* Prevent crashes when queues are still in use. */
5268 rte_delay_ms(hw->tqps_num);
5270 rte_spinlock_lock(&hw->lock);
5271 if (__atomic_load_n(&hw->reset.resetting, __ATOMIC_RELAXED) == 0) {
5272 hns3_tm_dev_stop_proc(hw);
5275 hns3_unmap_rx_interrupt(dev);
5276 hns3_dev_release_mbufs(hns);
5277 hw->adapter_state = HNS3_NIC_CONFIGURED;
5279 hns3_rx_scattered_reset(dev);
5280 rte_eal_alarm_cancel(hns3_service_handler, dev);
5281 rte_spinlock_unlock(&hw->lock);
5287 hns3_dev_close(struct rte_eth_dev *eth_dev)
5289 struct hns3_adapter *hns = eth_dev->data->dev_private;
5290 struct hns3_hw *hw = &hns->hw;
5293 if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
5294 rte_free(eth_dev->process_private);
5295 eth_dev->process_private = NULL;
5299 if (hw->adapter_state == HNS3_NIC_STARTED)
5300 ret = hns3_dev_stop(eth_dev);
5302 hw->adapter_state = HNS3_NIC_CLOSING;
5303 hns3_reset_abort(hns);
5304 hw->adapter_state = HNS3_NIC_CLOSED;
5306 hns3_configure_all_mc_mac_addr(hns, true);
5307 hns3_remove_all_vlan_table(hns);
5308 hns3_vlan_txvlan_cfg(hns, HNS3_PORT_BASE_VLAN_DISABLE, 0);
5309 hns3_uninit_pf(eth_dev);
5310 hns3_free_all_queues(eth_dev);
5311 rte_free(hw->reset.wait_data);
5312 rte_free(eth_dev->process_private);
5313 eth_dev->process_private = NULL;
5314 hns3_mp_uninit_primary();
5315 hns3_warn(hw, "Close port %u finished", hw->data->port_id);
5321 hns3_flow_ctrl_get(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
5323 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
5324 struct hns3_pf *pf = HNS3_DEV_PRIVATE_TO_PF(dev->data->dev_private);
5326 fc_conf->pause_time = pf->pause_time;
5328 /* return fc current mode */
5329 switch (hw->current_mode) {
5331 fc_conf->mode = RTE_FC_FULL;
5333 case HNS3_FC_TX_PAUSE:
5334 fc_conf->mode = RTE_FC_TX_PAUSE;
5336 case HNS3_FC_RX_PAUSE:
5337 fc_conf->mode = RTE_FC_RX_PAUSE;
5341 fc_conf->mode = RTE_FC_NONE;
5349 hns3_get_fc_mode(struct hns3_hw *hw, enum rte_eth_fc_mode mode)
5353 hw->requested_mode = HNS3_FC_NONE;
5355 case RTE_FC_RX_PAUSE:
5356 hw->requested_mode = HNS3_FC_RX_PAUSE;
5358 case RTE_FC_TX_PAUSE:
5359 hw->requested_mode = HNS3_FC_TX_PAUSE;
5362 hw->requested_mode = HNS3_FC_FULL;
5365 hw->requested_mode = HNS3_FC_NONE;
5366 hns3_warn(hw, "fc_mode(%u) exceeds member scope and is "
5367 "configured to RTE_FC_NONE", mode);
5373 hns3_flow_ctrl_set(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
5375 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
5376 struct hns3_pf *pf = HNS3_DEV_PRIVATE_TO_PF(dev->data->dev_private);
5379 if (fc_conf->high_water || fc_conf->low_water ||
5380 fc_conf->send_xon || fc_conf->mac_ctrl_frame_fwd) {
5381 hns3_err(hw, "Unsupported flow control settings specified, "
5382 "high_water(%u), low_water(%u), send_xon(%u) and "
5383 "mac_ctrl_frame_fwd(%u) must be set to '0'",
5384 fc_conf->high_water, fc_conf->low_water,
5385 fc_conf->send_xon, fc_conf->mac_ctrl_frame_fwd);
5388 if (fc_conf->autoneg) {
5389 hns3_err(hw, "Unsupported fc auto-negotiation setting.");
5392 if (!fc_conf->pause_time) {
5393 hns3_err(hw, "Invalid pause time %u setting.",
5394 fc_conf->pause_time);
5398 if (!(hw->current_fc_status == HNS3_FC_STATUS_NONE ||
5399 hw->current_fc_status == HNS3_FC_STATUS_MAC_PAUSE)) {
5400 hns3_err(hw, "PFC is enabled. Cannot set MAC pause. "
5401 "current_fc_status = %d", hw->current_fc_status);
5405 hns3_get_fc_mode(hw, fc_conf->mode);
5406 if (hw->requested_mode == hw->current_mode &&
5407 pf->pause_time == fc_conf->pause_time)
5410 rte_spinlock_lock(&hw->lock);
5411 ret = hns3_fc_enable(dev, fc_conf);
5412 rte_spinlock_unlock(&hw->lock);
5418 hns3_priority_flow_ctrl_set(struct rte_eth_dev *dev,
5419 struct rte_eth_pfc_conf *pfc_conf)
5421 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
5422 struct hns3_pf *pf = HNS3_DEV_PRIVATE_TO_PF(dev->data->dev_private);
5426 if (!hns3_dev_dcb_supported(hw)) {
5427 hns3_err(hw, "This port does not support dcb configurations.");
5431 if (pfc_conf->fc.high_water || pfc_conf->fc.low_water ||
5432 pfc_conf->fc.send_xon || pfc_conf->fc.mac_ctrl_frame_fwd) {
5433 hns3_err(hw, "Unsupported flow control settings specified, "
5434 "high_water(%u), low_water(%u), send_xon(%u) and "
5435 "mac_ctrl_frame_fwd(%u) must be set to '0'",
5436 pfc_conf->fc.high_water, pfc_conf->fc.low_water,
5437 pfc_conf->fc.send_xon,
5438 pfc_conf->fc.mac_ctrl_frame_fwd);
5441 if (pfc_conf->fc.autoneg) {
5442 hns3_err(hw, "Unsupported fc auto-negotiation setting.");
5445 if (pfc_conf->fc.pause_time == 0) {
5446 hns3_err(hw, "Invalid pause time %u setting.",
5447 pfc_conf->fc.pause_time);
5451 if (!(hw->current_fc_status == HNS3_FC_STATUS_NONE ||
5452 hw->current_fc_status == HNS3_FC_STATUS_PFC)) {
5453 hns3_err(hw, "MAC pause is enabled. Cannot set PFC."
5454 "current_fc_status = %d", hw->current_fc_status);
5458 priority = pfc_conf->priority;
5459 hns3_get_fc_mode(hw, pfc_conf->fc.mode);
5460 if (hw->dcb_info.pfc_en & BIT(priority) &&
5461 hw->requested_mode == hw->current_mode &&
5462 pfc_conf->fc.pause_time == pf->pause_time)
5465 rte_spinlock_lock(&hw->lock);
5466 ret = hns3_dcb_pfc_enable(dev, pfc_conf);
5467 rte_spinlock_unlock(&hw->lock);
5473 hns3_get_dcb_info(struct rte_eth_dev *dev, struct rte_eth_dcb_info *dcb_info)
5475 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
5476 struct hns3_pf *pf = HNS3_DEV_PRIVATE_TO_PF(dev->data->dev_private);
5477 enum rte_eth_rx_mq_mode mq_mode = dev->data->dev_conf.rxmode.mq_mode;
5480 rte_spinlock_lock(&hw->lock);
5481 if ((uint32_t)mq_mode & ETH_MQ_RX_DCB_FLAG)
5482 dcb_info->nb_tcs = pf->local_max_tc;
5484 dcb_info->nb_tcs = 1;
5486 for (i = 0; i < HNS3_MAX_USER_PRIO; i++)
5487 dcb_info->prio_tc[i] = hw->dcb_info.prio_tc[i];
5488 for (i = 0; i < dcb_info->nb_tcs; i++)
5489 dcb_info->tc_bws[i] = hw->dcb_info.pg_info[0].tc_dwrr[i];
5491 for (i = 0; i < hw->num_tc; i++) {
5492 dcb_info->tc_queue.tc_rxq[0][i].base = hw->alloc_rss_size * i;
5493 dcb_info->tc_queue.tc_txq[0][i].base =
5494 hw->tc_queue[i].tqp_offset;
5495 dcb_info->tc_queue.tc_rxq[0][i].nb_queue = hw->alloc_rss_size;
5496 dcb_info->tc_queue.tc_txq[0][i].nb_queue =
5497 hw->tc_queue[i].tqp_count;
5499 rte_spinlock_unlock(&hw->lock);
5505 hns3_reinit_dev(struct hns3_adapter *hns)
5507 struct hns3_hw *hw = &hns->hw;
5510 ret = hns3_cmd_init(hw);
5512 hns3_err(hw, "Failed to init cmd: %d", ret);
5516 ret = hns3_reset_all_tqps(hns);
5518 hns3_err(hw, "Failed to reset all queues: %d", ret);
5522 ret = hns3_init_hardware(hns);
5524 hns3_err(hw, "Failed to init hardware: %d", ret);
5528 ret = hns3_enable_hw_error_intr(hns, true);
5530 hns3_err(hw, "fail to enable hw error interrupts: %d",
5534 hns3_info(hw, "Reset done, driver initialization finished.");
5540 is_pf_reset_done(struct hns3_hw *hw)
5542 uint32_t val, reg, reg_bit;
5544 switch (hw->reset.level) {
5545 case HNS3_IMP_RESET:
5546 reg = HNS3_GLOBAL_RESET_REG;
5547 reg_bit = HNS3_IMP_RESET_BIT;
5549 case HNS3_GLOBAL_RESET:
5550 reg = HNS3_GLOBAL_RESET_REG;
5551 reg_bit = HNS3_GLOBAL_RESET_BIT;
5553 case HNS3_FUNC_RESET:
5554 reg = HNS3_FUN_RST_ING;
5555 reg_bit = HNS3_FUN_RST_ING_B;
5557 case HNS3_FLR_RESET:
5559 hns3_err(hw, "Wait for unsupported reset level: %d",
5563 val = hns3_read_dev(hw, reg);
5564 if (hns3_get_bit(val, reg_bit))
5571 hns3_is_reset_pending(struct hns3_adapter *hns)
5573 struct hns3_hw *hw = &hns->hw;
5574 enum hns3_reset_level reset;
5576 hns3_check_event_cause(hns, NULL);
5577 reset = hns3_get_reset_level(hns, &hw->reset.pending);
5578 if (hw->reset.level != HNS3_NONE_RESET && hw->reset.level < reset) {
5579 hns3_warn(hw, "High level reset %d is pending", reset);
5582 reset = hns3_get_reset_level(hns, &hw->reset.request);
5583 if (hw->reset.level != HNS3_NONE_RESET && hw->reset.level < reset) {
5584 hns3_warn(hw, "High level reset %d is request", reset);
5591 hns3_wait_hardware_ready(struct hns3_adapter *hns)
5593 struct hns3_hw *hw = &hns->hw;
5594 struct hns3_wait_data *wait_data = hw->reset.wait_data;
5597 if (wait_data->result == HNS3_WAIT_SUCCESS)
5599 else if (wait_data->result == HNS3_WAIT_TIMEOUT) {
5600 gettimeofday(&tv, NULL);
5601 hns3_warn(hw, "Reset step4 hardware not ready after reset time=%ld.%.6ld",
5602 tv.tv_sec, tv.tv_usec);
5604 } else if (wait_data->result == HNS3_WAIT_REQUEST)
5607 wait_data->hns = hns;
5608 wait_data->check_completion = is_pf_reset_done;
5609 wait_data->end_ms = (uint64_t)HNS3_RESET_WAIT_CNT *
5610 HNS3_RESET_WAIT_MS + get_timeofday_ms();
5611 wait_data->interval = HNS3_RESET_WAIT_MS * USEC_PER_MSEC;
5612 wait_data->count = HNS3_RESET_WAIT_CNT;
5613 wait_data->result = HNS3_WAIT_REQUEST;
5614 rte_eal_alarm_set(wait_data->interval, hns3_wait_callback, wait_data);
5619 hns3_func_reset_cmd(struct hns3_hw *hw, int func_id)
5621 struct hns3_cmd_desc desc;
5622 struct hns3_reset_cmd *req = (struct hns3_reset_cmd *)desc.data;
5624 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_CFG_RST_TRIGGER, false);
5625 hns3_set_bit(req->mac_func_reset, HNS3_CFG_RESET_FUNC_B, 1);
5626 req->fun_reset_vfid = func_id;
5628 return hns3_cmd_send(hw, &desc, 1);
5632 hns3_imp_reset_cmd(struct hns3_hw *hw)
5634 struct hns3_cmd_desc desc;
5636 hns3_cmd_setup_basic_desc(&desc, 0xFFFE, false);
5637 desc.data[0] = 0xeedd;
5639 return hns3_cmd_send(hw, &desc, 1);
5643 hns3_msix_process(struct hns3_adapter *hns, enum hns3_reset_level reset_level)
5645 struct hns3_hw *hw = &hns->hw;
5649 gettimeofday(&tv, NULL);
5650 if (hns3_read_dev(hw, HNS3_GLOBAL_RESET_REG) ||
5651 hns3_read_dev(hw, HNS3_FUN_RST_ING)) {
5652 hns3_warn(hw, "Don't process msix during resetting time=%ld.%.6ld",
5653 tv.tv_sec, tv.tv_usec);
5657 switch (reset_level) {
5658 case HNS3_IMP_RESET:
5659 hns3_imp_reset_cmd(hw);
5660 hns3_warn(hw, "IMP Reset requested time=%ld.%.6ld",
5661 tv.tv_sec, tv.tv_usec);
5663 case HNS3_GLOBAL_RESET:
5664 val = hns3_read_dev(hw, HNS3_GLOBAL_RESET_REG);
5665 hns3_set_bit(val, HNS3_GLOBAL_RESET_BIT, 1);
5666 hns3_write_dev(hw, HNS3_GLOBAL_RESET_REG, val);
5667 hns3_warn(hw, "Global Reset requested time=%ld.%.6ld",
5668 tv.tv_sec, tv.tv_usec);
5670 case HNS3_FUNC_RESET:
5671 hns3_warn(hw, "PF Reset requested time=%ld.%.6ld",
5672 tv.tv_sec, tv.tv_usec);
5673 /* schedule again to check later */
5674 hns3_atomic_set_bit(HNS3_FUNC_RESET, &hw->reset.pending);
5675 hns3_schedule_reset(hns);
5678 hns3_warn(hw, "Unsupported reset level: %d", reset_level);
5681 hns3_atomic_clear_bit(reset_level, &hw->reset.request);
5684 static enum hns3_reset_level
5685 hns3_get_reset_level(struct hns3_adapter *hns, uint64_t *levels)
5687 struct hns3_hw *hw = &hns->hw;
5688 enum hns3_reset_level reset_level = HNS3_NONE_RESET;
5690 /* Return the highest priority reset level amongst all */
5691 if (hns3_atomic_test_bit(HNS3_IMP_RESET, levels))
5692 reset_level = HNS3_IMP_RESET;
5693 else if (hns3_atomic_test_bit(HNS3_GLOBAL_RESET, levels))
5694 reset_level = HNS3_GLOBAL_RESET;
5695 else if (hns3_atomic_test_bit(HNS3_FUNC_RESET, levels))
5696 reset_level = HNS3_FUNC_RESET;
5697 else if (hns3_atomic_test_bit(HNS3_FLR_RESET, levels))
5698 reset_level = HNS3_FLR_RESET;
5700 if (hw->reset.level != HNS3_NONE_RESET && reset_level < hw->reset.level)
5701 return HNS3_NONE_RESET;
5707 hns3_record_imp_error(struct hns3_adapter *hns)
5709 struct hns3_hw *hw = &hns->hw;
5712 reg_val = hns3_read_dev(hw, HNS3_VECTOR0_OTER_EN_REG);
5713 if (hns3_get_bit(reg_val, HNS3_VECTOR0_IMP_RD_POISON_B)) {
5714 hns3_warn(hw, "Detected IMP RD poison!");
5715 hns3_error_int_stats_add(hns, "IMP_RD_POISON_INT_STS");
5716 hns3_set_bit(reg_val, HNS3_VECTOR0_IMP_RD_POISON_B, 0);
5717 hns3_write_dev(hw, HNS3_VECTOR0_OTER_EN_REG, reg_val);
5720 if (hns3_get_bit(reg_val, HNS3_VECTOR0_IMP_CMDQ_ERR_B)) {
5721 hns3_warn(hw, "Detected IMP CMDQ error!");
5722 hns3_error_int_stats_add(hns, "CMDQ_MEM_ECC_INT_STS");
5723 hns3_set_bit(reg_val, HNS3_VECTOR0_IMP_CMDQ_ERR_B, 0);
5724 hns3_write_dev(hw, HNS3_VECTOR0_OTER_EN_REG, reg_val);
5729 hns3_prepare_reset(struct hns3_adapter *hns)
5731 struct hns3_hw *hw = &hns->hw;
5735 switch (hw->reset.level) {
5736 case HNS3_FUNC_RESET:
5737 ret = hns3_func_reset_cmd(hw, HNS3_PF_FUNC_ID);
5742 * After performaning pf reset, it is not necessary to do the
5743 * mailbox handling or send any command to firmware, because
5744 * any mailbox handling or command to firmware is only valid
5745 * after hns3_cmd_init is called.
5747 __atomic_store_n(&hw->reset.disable_cmd, 1, __ATOMIC_RELAXED);
5748 hw->reset.stats.request_cnt++;
5750 case HNS3_IMP_RESET:
5751 hns3_record_imp_error(hns);
5752 reg_val = hns3_read_dev(hw, HNS3_VECTOR0_OTER_EN_REG);
5753 hns3_write_dev(hw, HNS3_VECTOR0_OTER_EN_REG, reg_val |
5754 BIT(HNS3_VECTOR0_IMP_RESET_INT_B));
5763 hns3_set_rst_done(struct hns3_hw *hw)
5765 struct hns3_pf_rst_done_cmd *req;
5766 struct hns3_cmd_desc desc;
5768 req = (struct hns3_pf_rst_done_cmd *)desc.data;
5769 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_PF_RST_DONE, false);
5770 req->pf_rst_done |= HNS3_PF_RESET_DONE_BIT;
5771 return hns3_cmd_send(hw, &desc, 1);
5775 hns3_stop_service(struct hns3_adapter *hns)
5777 struct hns3_hw *hw = &hns->hw;
5778 struct rte_eth_dev *eth_dev;
5780 eth_dev = &rte_eth_devices[hw->data->port_id];
5781 if (hw->adapter_state == HNS3_NIC_STARTED) {
5782 rte_eal_alarm_cancel(hns3_service_handler, eth_dev);
5783 hns3_update_link_status_and_event(hw);
5785 hw->mac.link_status = ETH_LINK_DOWN;
5787 hns3_set_rxtx_function(eth_dev);
5789 /* Disable datapath on secondary process. */
5790 hns3_mp_req_stop_rxtx(eth_dev);
5791 rte_delay_ms(hw->tqps_num);
5793 rte_spinlock_lock(&hw->lock);
5794 if (hns->hw.adapter_state == HNS3_NIC_STARTED ||
5795 hw->adapter_state == HNS3_NIC_STOPPING) {
5796 hns3_enable_all_queues(hw, false);
5798 hw->reset.mbuf_deferred_free = true;
5800 hw->reset.mbuf_deferred_free = false;
5803 * It is cumbersome for hardware to pick-and-choose entries for deletion
5804 * from table space. Hence, for function reset software intervention is
5805 * required to delete the entries
5807 if (__atomic_load_n(&hw->reset.disable_cmd, __ATOMIC_RELAXED) == 0)
5808 hns3_configure_all_mc_mac_addr(hns, true);
5809 rte_spinlock_unlock(&hw->lock);
5815 hns3_start_service(struct hns3_adapter *hns)
5817 struct hns3_hw *hw = &hns->hw;
5818 struct rte_eth_dev *eth_dev;
5820 if (hw->reset.level == HNS3_IMP_RESET ||
5821 hw->reset.level == HNS3_GLOBAL_RESET)
5822 hns3_set_rst_done(hw);
5823 eth_dev = &rte_eth_devices[hw->data->port_id];
5824 hns3_set_rxtx_function(eth_dev);
5825 hns3_mp_req_start_rxtx(eth_dev);
5826 if (hw->adapter_state == HNS3_NIC_STARTED) {
5828 * This API parent function already hold the hns3_hw.lock, the
5829 * hns3_service_handler may report lse, in bonding application
5830 * it will call driver's ops which may acquire the hns3_hw.lock
5831 * again, thus lead to deadlock.
5832 * We defer calls hns3_service_handler to avoid the deadlock.
5834 rte_eal_alarm_set(HNS3_SERVICE_QUICK_INTERVAL,
5835 hns3_service_handler, eth_dev);
5837 /* Enable interrupt of all rx queues before enabling queues */
5838 hns3_dev_all_rx_queue_intr_enable(hw, true);
5840 * Enable state of each rxq and txq will be recovered after
5841 * reset, so we need to restore them before enable all tqps;
5843 hns3_restore_tqp_enable_state(hw);
5845 * When finished the initialization, enable queues to receive
5846 * and transmit packets.
5848 hns3_enable_all_queues(hw, true);
5855 hns3_restore_conf(struct hns3_adapter *hns)
5857 struct hns3_hw *hw = &hns->hw;
5860 ret = hns3_configure_all_mac_addr(hns, false);
5864 ret = hns3_configure_all_mc_mac_addr(hns, false);
5868 ret = hns3_dev_promisc_restore(hns);
5872 ret = hns3_restore_vlan_table(hns);
5876 ret = hns3_restore_vlan_conf(hns);
5880 ret = hns3_restore_all_fdir_filter(hns);
5884 ret = hns3_restore_rx_interrupt(hw);
5888 ret = hns3_restore_gro_conf(hw);
5892 ret = hns3_restore_fec(hw);
5896 if (hns->hw.adapter_state == HNS3_NIC_STARTED) {
5897 ret = hns3_do_start(hns, false);
5900 hns3_info(hw, "hns3 dev restart successful!");
5901 } else if (hw->adapter_state == HNS3_NIC_STOPPING)
5902 hw->adapter_state = HNS3_NIC_CONFIGURED;
5906 hns3_configure_all_mc_mac_addr(hns, true);
5908 hns3_configure_all_mac_addr(hns, true);
5913 hns3_reset_service(void *param)
5915 struct hns3_adapter *hns = (struct hns3_adapter *)param;
5916 struct hns3_hw *hw = &hns->hw;
5917 enum hns3_reset_level reset_level;
5918 struct timeval tv_delta;
5919 struct timeval tv_start;
5925 * The interrupt is not triggered within the delay time.
5926 * The interrupt may have been lost. It is necessary to handle
5927 * the interrupt to recover from the error.
5929 if (__atomic_load_n(&hw->reset.schedule, __ATOMIC_RELAXED) ==
5930 SCHEDULE_DEFERRED) {
5931 __atomic_store_n(&hw->reset.schedule, SCHEDULE_REQUESTED,
5933 hns3_err(hw, "Handling interrupts in delayed tasks");
5934 hns3_interrupt_handler(&rte_eth_devices[hw->data->port_id]);
5935 reset_level = hns3_get_reset_level(hns, &hw->reset.pending);
5936 if (reset_level == HNS3_NONE_RESET) {
5937 hns3_err(hw, "No reset level is set, try IMP reset");
5938 hns3_atomic_set_bit(HNS3_IMP_RESET, &hw->reset.pending);
5941 __atomic_store_n(&hw->reset.schedule, SCHEDULE_NONE, __ATOMIC_RELAXED);
5944 * Check if there is any ongoing reset in the hardware. This status can
5945 * be checked from reset_pending. If there is then, we need to wait for
5946 * hardware to complete reset.
5947 * a. If we are able to figure out in reasonable time that hardware
5948 * has fully resetted then, we can proceed with driver, client
5950 * b. else, we can come back later to check this status so re-sched
5953 reset_level = hns3_get_reset_level(hns, &hw->reset.pending);
5954 if (reset_level != HNS3_NONE_RESET) {
5955 gettimeofday(&tv_start, NULL);
5956 ret = hns3_reset_process(hns, reset_level);
5957 gettimeofday(&tv, NULL);
5958 timersub(&tv, &tv_start, &tv_delta);
5959 msec = tv_delta.tv_sec * MSEC_PER_SEC +
5960 tv_delta.tv_usec / USEC_PER_MSEC;
5961 if (msec > HNS3_RESET_PROCESS_MS)
5962 hns3_err(hw, "%d handle long time delta %" PRIx64
5963 " ms time=%ld.%.6ld",
5964 hw->reset.level, msec,
5965 tv.tv_sec, tv.tv_usec);
5970 /* Check if we got any *new* reset requests to be honored */
5971 reset_level = hns3_get_reset_level(hns, &hw->reset.request);
5972 if (reset_level != HNS3_NONE_RESET)
5973 hns3_msix_process(hns, reset_level);
5977 hns3_get_speed_capa_num(uint16_t device_id)
5981 switch (device_id) {
5982 case HNS3_DEV_ID_25GE:
5983 case HNS3_DEV_ID_25GE_RDMA:
5986 case HNS3_DEV_ID_100G_RDMA_MACSEC:
5987 case HNS3_DEV_ID_200G_RDMA:
5999 hns3_get_speed_fec_capa(struct rte_eth_fec_capa *speed_fec_capa,
6002 switch (device_id) {
6003 case HNS3_DEV_ID_25GE:
6005 case HNS3_DEV_ID_25GE_RDMA:
6006 speed_fec_capa[0].speed = speed_fec_capa_tbl[1].speed;
6007 speed_fec_capa[0].capa = speed_fec_capa_tbl[1].capa;
6009 /* In HNS3 device, the 25G NIC is compatible with 10G rate */
6010 speed_fec_capa[1].speed = speed_fec_capa_tbl[0].speed;
6011 speed_fec_capa[1].capa = speed_fec_capa_tbl[0].capa;
6013 case HNS3_DEV_ID_100G_RDMA_MACSEC:
6014 speed_fec_capa[0].speed = speed_fec_capa_tbl[4].speed;
6015 speed_fec_capa[0].capa = speed_fec_capa_tbl[4].capa;
6017 case HNS3_DEV_ID_200G_RDMA:
6018 speed_fec_capa[0].speed = speed_fec_capa_tbl[5].speed;
6019 speed_fec_capa[0].capa = speed_fec_capa_tbl[5].capa;
6029 hns3_fec_get_capability(struct rte_eth_dev *dev,
6030 struct rte_eth_fec_capa *speed_fec_capa,
6033 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
6034 struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
6035 uint16_t device_id = pci_dev->id.device_id;
6036 unsigned int capa_num;
6039 capa_num = hns3_get_speed_capa_num(device_id);
6040 if (capa_num == 0) {
6041 hns3_err(hw, "device(0x%x) is not supported by hns3 PMD",
6046 if (speed_fec_capa == NULL || num < capa_num)
6049 ret = hns3_get_speed_fec_capa(speed_fec_capa, device_id);
6057 get_current_fec_auto_state(struct hns3_hw *hw, uint8_t *state)
6059 struct hns3_config_fec_cmd *req;
6060 struct hns3_cmd_desc desc;
6064 * CMD(HNS3_OPC_CONFIG_FEC_MODE) read is not supported
6065 * in device of link speed
6068 if (hw->mac.link_speed < ETH_SPEED_NUM_10G) {
6073 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_CONFIG_FEC_MODE, true);
6074 req = (struct hns3_config_fec_cmd *)desc.data;
6075 ret = hns3_cmd_send(hw, &desc, 1);
6077 hns3_err(hw, "get current fec auto state failed, ret = %d",
6082 *state = req->fec_mode & (1U << HNS3_MAC_CFG_FEC_AUTO_EN_B);
6087 hns3_fec_get_internal(struct hns3_hw *hw, uint32_t *fec_capa)
6089 #define QUERY_ACTIVE_SPEED 1
6090 struct hns3_sfp_speed_cmd *resp;
6091 uint32_t tmp_fec_capa;
6093 struct hns3_cmd_desc desc;
6097 * If link is down and AUTO is enabled, AUTO is returned, otherwise,
6098 * configured FEC mode is returned.
6099 * If link is up, current FEC mode is returned.
6101 if (hw->mac.link_status == ETH_LINK_DOWN) {
6102 ret = get_current_fec_auto_state(hw, &auto_state);
6106 if (auto_state == 0x1) {
6107 *fec_capa = RTE_ETH_FEC_MODE_CAPA_MASK(AUTO);
6112 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_SFP_GET_SPEED, true);
6113 resp = (struct hns3_sfp_speed_cmd *)desc.data;
6114 resp->query_type = QUERY_ACTIVE_SPEED;
6116 ret = hns3_cmd_send(hw, &desc, 1);
6117 if (ret == -EOPNOTSUPP) {
6118 hns3_err(hw, "IMP do not support get FEC, ret = %d", ret);
6121 hns3_err(hw, "get FEC failed, ret = %d", ret);
6126 * FEC mode order defined in hns3 hardware is inconsistend with
6127 * that defined in the ethdev library. So the sequence needs
6130 switch (resp->active_fec) {
6131 case HNS3_HW_FEC_MODE_NOFEC:
6132 tmp_fec_capa = RTE_ETH_FEC_MODE_CAPA_MASK(NOFEC);
6134 case HNS3_HW_FEC_MODE_BASER:
6135 tmp_fec_capa = RTE_ETH_FEC_MODE_CAPA_MASK(BASER);
6137 case HNS3_HW_FEC_MODE_RS:
6138 tmp_fec_capa = RTE_ETH_FEC_MODE_CAPA_MASK(RS);
6141 tmp_fec_capa = RTE_ETH_FEC_MODE_CAPA_MASK(NOFEC);
6145 *fec_capa = tmp_fec_capa;
6150 hns3_fec_get(struct rte_eth_dev *dev, uint32_t *fec_capa)
6152 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
6154 return hns3_fec_get_internal(hw, fec_capa);
6158 hns3_set_fec_hw(struct hns3_hw *hw, uint32_t mode)
6160 struct hns3_config_fec_cmd *req;
6161 struct hns3_cmd_desc desc;
6164 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_CONFIG_FEC_MODE, false);
6166 req = (struct hns3_config_fec_cmd *)desc.data;
6168 case RTE_ETH_FEC_MODE_CAPA_MASK(NOFEC):
6169 hns3_set_field(req->fec_mode, HNS3_MAC_CFG_FEC_MODE_M,
6170 HNS3_MAC_CFG_FEC_MODE_S, HNS3_MAC_FEC_OFF);
6172 case RTE_ETH_FEC_MODE_CAPA_MASK(BASER):
6173 hns3_set_field(req->fec_mode, HNS3_MAC_CFG_FEC_MODE_M,
6174 HNS3_MAC_CFG_FEC_MODE_S, HNS3_MAC_FEC_BASER);
6176 case RTE_ETH_FEC_MODE_CAPA_MASK(RS):
6177 hns3_set_field(req->fec_mode, HNS3_MAC_CFG_FEC_MODE_M,
6178 HNS3_MAC_CFG_FEC_MODE_S, HNS3_MAC_FEC_RS);
6180 case RTE_ETH_FEC_MODE_CAPA_MASK(AUTO):
6181 hns3_set_bit(req->fec_mode, HNS3_MAC_CFG_FEC_AUTO_EN_B, 1);
6186 ret = hns3_cmd_send(hw, &desc, 1);
6188 hns3_err(hw, "set fec mode failed, ret = %d", ret);
6194 get_current_speed_fec_cap(struct hns3_hw *hw, struct rte_eth_fec_capa *fec_capa)
6196 struct hns3_mac *mac = &hw->mac;
6199 switch (mac->link_speed) {
6200 case ETH_SPEED_NUM_10G:
6201 cur_capa = fec_capa[1].capa;
6203 case ETH_SPEED_NUM_25G:
6204 case ETH_SPEED_NUM_100G:
6205 case ETH_SPEED_NUM_200G:
6206 cur_capa = fec_capa[0].capa;
6217 is_fec_mode_one_bit_set(uint32_t mode)
6222 for (i = 0; i < sizeof(mode); i++)
6223 if (mode >> i & 0x1)
6226 return cnt == 1 ? true : false;
6230 hns3_fec_set(struct rte_eth_dev *dev, uint32_t mode)
6232 #define FEC_CAPA_NUM 2
6233 struct hns3_adapter *hns = dev->data->dev_private;
6234 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(hns);
6235 struct hns3_pf *pf = &hns->pf;
6237 struct rte_eth_fec_capa fec_capa[FEC_CAPA_NUM];
6239 uint32_t num = FEC_CAPA_NUM;
6242 ret = hns3_fec_get_capability(dev, fec_capa, num);
6246 /* HNS3 PMD driver only support one bit set mode, e.g. 0x1, 0x4 */
6247 if (!is_fec_mode_one_bit_set(mode))
6248 hns3_err(hw, "FEC mode(0x%x) not supported in HNS3 PMD,"
6249 "FEC mode should be only one bit set", mode);
6252 * Check whether the configured mode is within the FEC capability.
6253 * If not, the configured mode will not be supported.
6255 cur_capa = get_current_speed_fec_cap(hw, fec_capa);
6256 if (!(cur_capa & mode)) {
6257 hns3_err(hw, "unsupported FEC mode = 0x%x", mode);
6261 ret = hns3_set_fec_hw(hw, mode);
6265 pf->fec_mode = mode;
6270 hns3_restore_fec(struct hns3_hw *hw)
6272 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
6273 struct hns3_pf *pf = &hns->pf;
6274 uint32_t mode = pf->fec_mode;
6277 ret = hns3_set_fec_hw(hw, mode);
6279 hns3_err(hw, "restore fec mode(0x%x) failed, ret = %d",
6286 hns3_query_dev_fec_info(struct hns3_hw *hw)
6288 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
6289 struct hns3_pf *pf = HNS3_DEV_PRIVATE_TO_PF(hns);
6292 ret = hns3_fec_get_internal(hw, &pf->fec_mode);
6294 hns3_err(hw, "query device FEC info failed, ret = %d", ret);
6300 hns3_optical_module_existed(struct hns3_hw *hw)
6302 struct hns3_cmd_desc desc;
6306 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_GET_SFP_EXIST, true);
6307 ret = hns3_cmd_send(hw, &desc, 1);
6310 "fail to get optical module exist state, ret = %d.\n",
6314 existed = !!desc.data[0];
6320 hns3_get_module_eeprom_data(struct hns3_hw *hw, uint32_t offset,
6321 uint32_t len, uint8_t *data)
6323 #define HNS3_SFP_INFO_CMD_NUM 6
6324 #define HNS3_SFP_INFO_MAX_LEN \
6325 (HNS3_SFP_INFO_BD0_LEN + \
6326 (HNS3_SFP_INFO_CMD_NUM - 1) * HNS3_SFP_INFO_BDX_LEN)
6327 struct hns3_cmd_desc desc[HNS3_SFP_INFO_CMD_NUM];
6328 struct hns3_sfp_info_bd0_cmd *sfp_info_bd0;
6334 for (i = 0; i < HNS3_SFP_INFO_CMD_NUM; i++) {
6335 hns3_cmd_setup_basic_desc(&desc[i], HNS3_OPC_GET_SFP_EEPROM,
6337 if (i < HNS3_SFP_INFO_CMD_NUM - 1)
6338 desc[i].flag |= rte_cpu_to_le_16(HNS3_CMD_FLAG_NEXT);
6341 sfp_info_bd0 = (struct hns3_sfp_info_bd0_cmd *)desc[0].data;
6342 sfp_info_bd0->offset = rte_cpu_to_le_16((uint16_t)offset);
6343 read_len = RTE_MIN(len, HNS3_SFP_INFO_MAX_LEN);
6344 sfp_info_bd0->read_len = rte_cpu_to_le_16((uint16_t)read_len);
6346 ret = hns3_cmd_send(hw, desc, HNS3_SFP_INFO_CMD_NUM);
6348 hns3_err(hw, "fail to get module EEPROM info, ret = %d.\n",
6353 /* The data format in BD0 is different with the others. */
6354 copy_len = RTE_MIN(len, HNS3_SFP_INFO_BD0_LEN);
6355 memcpy(data, sfp_info_bd0->data, copy_len);
6356 read_len = copy_len;
6358 for (i = 1; i < HNS3_SFP_INFO_CMD_NUM; i++) {
6359 if (read_len >= len)
6362 copy_len = RTE_MIN(len - read_len, HNS3_SFP_INFO_BDX_LEN);
6363 memcpy(data + read_len, desc[i].data, copy_len);
6364 read_len += copy_len;
6367 return (int)read_len;
6371 hns3_get_module_eeprom(struct rte_eth_dev *dev,
6372 struct rte_dev_eeprom_info *info)
6374 struct hns3_adapter *hns = dev->data->dev_private;
6375 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(hns);
6376 uint32_t offset = info->offset;
6377 uint32_t len = info->length;
6378 uint8_t *data = info->data;
6379 uint32_t read_len = 0;
6381 if (hw->mac.media_type != HNS3_MEDIA_TYPE_FIBER)
6384 if (!hns3_optical_module_existed(hw)) {
6385 hns3_err(hw, "fail to read module EEPROM: no module is connected.\n");
6389 while (read_len < len) {
6391 ret = hns3_get_module_eeprom_data(hw, offset + read_len,
6403 hns3_get_module_info(struct rte_eth_dev *dev,
6404 struct rte_eth_dev_module_info *modinfo)
6406 #define HNS3_SFF8024_ID_SFP 0x03
6407 #define HNS3_SFF8024_ID_QSFP_8438 0x0c
6408 #define HNS3_SFF8024_ID_QSFP_8436_8636 0x0d
6409 #define HNS3_SFF8024_ID_QSFP28_8636 0x11
6410 #define HNS3_SFF_8636_V1_3 0x03
6411 struct hns3_adapter *hns = dev->data->dev_private;
6412 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(hns);
6413 struct rte_dev_eeprom_info info;
6414 struct hns3_sfp_type sfp_type;
6417 memset(&sfp_type, 0, sizeof(sfp_type));
6418 memset(&info, 0, sizeof(info));
6419 info.data = (uint8_t *)&sfp_type;
6420 info.length = sizeof(sfp_type);
6421 ret = hns3_get_module_eeprom(dev, &info);
6425 switch (sfp_type.type) {
6426 case HNS3_SFF8024_ID_SFP:
6427 modinfo->type = RTE_ETH_MODULE_SFF_8472;
6428 modinfo->eeprom_len = RTE_ETH_MODULE_SFF_8472_LEN;
6430 case HNS3_SFF8024_ID_QSFP_8438:
6431 modinfo->type = RTE_ETH_MODULE_SFF_8436;
6432 modinfo->eeprom_len = RTE_ETH_MODULE_SFF_8436_MAX_LEN;
6434 case HNS3_SFF8024_ID_QSFP_8436_8636:
6435 if (sfp_type.ext_type < HNS3_SFF_8636_V1_3) {
6436 modinfo->type = RTE_ETH_MODULE_SFF_8436;
6437 modinfo->eeprom_len = RTE_ETH_MODULE_SFF_8436_MAX_LEN;
6439 modinfo->type = RTE_ETH_MODULE_SFF_8636;
6440 modinfo->eeprom_len = RTE_ETH_MODULE_SFF_8636_MAX_LEN;
6443 case HNS3_SFF8024_ID_QSFP28_8636:
6444 modinfo->type = RTE_ETH_MODULE_SFF_8636;
6445 modinfo->eeprom_len = RTE_ETH_MODULE_SFF_8636_MAX_LEN;
6448 hns3_err(hw, "unknown module, type = %u, extra_type = %u.\n",
6449 sfp_type.type, sfp_type.ext_type);
6456 static const struct eth_dev_ops hns3_eth_dev_ops = {
6457 .dev_configure = hns3_dev_configure,
6458 .dev_start = hns3_dev_start,
6459 .dev_stop = hns3_dev_stop,
6460 .dev_close = hns3_dev_close,
6461 .promiscuous_enable = hns3_dev_promiscuous_enable,
6462 .promiscuous_disable = hns3_dev_promiscuous_disable,
6463 .allmulticast_enable = hns3_dev_allmulticast_enable,
6464 .allmulticast_disable = hns3_dev_allmulticast_disable,
6465 .mtu_set = hns3_dev_mtu_set,
6466 .stats_get = hns3_stats_get,
6467 .stats_reset = hns3_stats_reset,
6468 .xstats_get = hns3_dev_xstats_get,
6469 .xstats_get_names = hns3_dev_xstats_get_names,
6470 .xstats_reset = hns3_dev_xstats_reset,
6471 .xstats_get_by_id = hns3_dev_xstats_get_by_id,
6472 .xstats_get_names_by_id = hns3_dev_xstats_get_names_by_id,
6473 .dev_infos_get = hns3_dev_infos_get,
6474 .fw_version_get = hns3_fw_version_get,
6475 .rx_queue_setup = hns3_rx_queue_setup,
6476 .tx_queue_setup = hns3_tx_queue_setup,
6477 .rx_queue_release = hns3_dev_rx_queue_release,
6478 .tx_queue_release = hns3_dev_tx_queue_release,
6479 .rx_queue_start = hns3_dev_rx_queue_start,
6480 .rx_queue_stop = hns3_dev_rx_queue_stop,
6481 .tx_queue_start = hns3_dev_tx_queue_start,
6482 .tx_queue_stop = hns3_dev_tx_queue_stop,
6483 .rx_queue_intr_enable = hns3_dev_rx_queue_intr_enable,
6484 .rx_queue_intr_disable = hns3_dev_rx_queue_intr_disable,
6485 .rxq_info_get = hns3_rxq_info_get,
6486 .txq_info_get = hns3_txq_info_get,
6487 .rx_burst_mode_get = hns3_rx_burst_mode_get,
6488 .tx_burst_mode_get = hns3_tx_burst_mode_get,
6489 .flow_ctrl_get = hns3_flow_ctrl_get,
6490 .flow_ctrl_set = hns3_flow_ctrl_set,
6491 .priority_flow_ctrl_set = hns3_priority_flow_ctrl_set,
6492 .mac_addr_add = hns3_add_mac_addr,
6493 .mac_addr_remove = hns3_remove_mac_addr,
6494 .mac_addr_set = hns3_set_default_mac_addr,
6495 .set_mc_addr_list = hns3_set_mc_mac_addr_list,
6496 .link_update = hns3_dev_link_update,
6497 .rss_hash_update = hns3_dev_rss_hash_update,
6498 .rss_hash_conf_get = hns3_dev_rss_hash_conf_get,
6499 .reta_update = hns3_dev_rss_reta_update,
6500 .reta_query = hns3_dev_rss_reta_query,
6501 .filter_ctrl = hns3_dev_filter_ctrl,
6502 .vlan_filter_set = hns3_vlan_filter_set,
6503 .vlan_tpid_set = hns3_vlan_tpid_set,
6504 .vlan_offload_set = hns3_vlan_offload_set,
6505 .vlan_pvid_set = hns3_vlan_pvid_set,
6506 .get_reg = hns3_get_regs,
6507 .get_module_info = hns3_get_module_info,
6508 .get_module_eeprom = hns3_get_module_eeprom,
6509 .get_dcb_info = hns3_get_dcb_info,
6510 .dev_supported_ptypes_get = hns3_dev_supported_ptypes_get,
6511 .fec_get_capability = hns3_fec_get_capability,
6512 .fec_get = hns3_fec_get,
6513 .fec_set = hns3_fec_set,
6514 .tm_ops_get = hns3_tm_ops_get,
6515 .tx_done_cleanup = hns3_tx_done_cleanup,
6518 static const struct hns3_reset_ops hns3_reset_ops = {
6519 .reset_service = hns3_reset_service,
6520 .stop_service = hns3_stop_service,
6521 .prepare_reset = hns3_prepare_reset,
6522 .wait_hardware_ready = hns3_wait_hardware_ready,
6523 .reinit_dev = hns3_reinit_dev,
6524 .restore_conf = hns3_restore_conf,
6525 .start_service = hns3_start_service,
6529 hns3_dev_init(struct rte_eth_dev *eth_dev)
6531 struct hns3_adapter *hns = eth_dev->data->dev_private;
6532 char mac_str[RTE_ETHER_ADDR_FMT_SIZE];
6533 struct rte_ether_addr *eth_addr;
6534 struct hns3_hw *hw = &hns->hw;
6537 PMD_INIT_FUNC_TRACE();
6539 eth_dev->process_private = (struct hns3_process_private *)
6540 rte_zmalloc_socket("hns3_filter_list",
6541 sizeof(struct hns3_process_private),
6542 RTE_CACHE_LINE_SIZE, eth_dev->device->numa_node);
6543 if (eth_dev->process_private == NULL) {
6544 PMD_INIT_LOG(ERR, "Failed to alloc memory for process private");
6547 /* initialize flow filter lists */
6548 hns3_filterlist_init(eth_dev);
6550 hns3_set_rxtx_function(eth_dev);
6551 eth_dev->dev_ops = &hns3_eth_dev_ops;
6552 eth_dev->rx_queue_count = hns3_rx_queue_count;
6553 if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
6554 ret = hns3_mp_init_secondary();
6556 PMD_INIT_LOG(ERR, "Failed to init for secondary "
6557 "process, ret = %d", ret);
6558 goto err_mp_init_secondary;
6561 hw->secondary_cnt++;
6565 ret = hns3_mp_init_primary();
6568 "Failed to init for primary process, ret = %d",
6570 goto err_mp_init_primary;
6573 hw->adapter_state = HNS3_NIC_UNINITIALIZED;
6575 hw->data = eth_dev->data;
6578 * Set default max packet size according to the mtu
6579 * default vale in DPDK frame.
6581 hns->pf.mps = hw->data->mtu + HNS3_ETH_OVERHEAD;
6583 ret = hns3_reset_init(hw);
6585 goto err_init_reset;
6586 hw->reset.ops = &hns3_reset_ops;
6588 ret = hns3_init_pf(eth_dev);
6590 PMD_INIT_LOG(ERR, "Failed to init pf: %d", ret);
6594 /* Allocate memory for storing MAC addresses */
6595 eth_dev->data->mac_addrs = rte_zmalloc("hns3-mac",
6596 sizeof(struct rte_ether_addr) *
6597 HNS3_UC_MACADDR_NUM, 0);
6598 if (eth_dev->data->mac_addrs == NULL) {
6599 PMD_INIT_LOG(ERR, "Failed to allocate %zx bytes needed "
6600 "to store MAC addresses",
6601 sizeof(struct rte_ether_addr) *
6602 HNS3_UC_MACADDR_NUM);
6604 goto err_rte_zmalloc;
6607 eth_addr = (struct rte_ether_addr *)hw->mac.mac_addr;
6608 if (!rte_is_valid_assigned_ether_addr(eth_addr)) {
6609 rte_eth_random_addr(hw->mac.mac_addr);
6610 hns3_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
6611 (struct rte_ether_addr *)hw->mac.mac_addr);
6612 hns3_warn(hw, "default mac_addr from firmware is an invalid "
6613 "unicast address, using random MAC address %s",
6616 rte_ether_addr_copy((struct rte_ether_addr *)hw->mac.mac_addr,
6617 ð_dev->data->mac_addrs[0]);
6619 hw->adapter_state = HNS3_NIC_INITIALIZED;
6621 if (__atomic_load_n(&hw->reset.schedule, __ATOMIC_RELAXED) ==
6623 hns3_err(hw, "Reschedule reset service after dev_init");
6624 hns3_schedule_reset(hns);
6626 /* IMP will wait ready flag before reset */
6627 hns3_notify_reset_ready(hw, false);
6630 hns3_info(hw, "hns3 dev initialization successful!");
6634 hns3_uninit_pf(eth_dev);
6637 rte_free(hw->reset.wait_data);
6640 hns3_mp_uninit_primary();
6642 err_mp_init_primary:
6643 err_mp_init_secondary:
6644 eth_dev->dev_ops = NULL;
6645 eth_dev->rx_pkt_burst = NULL;
6646 eth_dev->tx_pkt_burst = NULL;
6647 eth_dev->tx_pkt_prepare = NULL;
6648 rte_free(eth_dev->process_private);
6649 eth_dev->process_private = NULL;
6654 hns3_dev_uninit(struct rte_eth_dev *eth_dev)
6656 struct hns3_adapter *hns = eth_dev->data->dev_private;
6657 struct hns3_hw *hw = &hns->hw;
6659 PMD_INIT_FUNC_TRACE();
6661 if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
6662 rte_free(eth_dev->process_private);
6663 eth_dev->process_private = NULL;
6667 if (hw->adapter_state < HNS3_NIC_CLOSING)
6668 hns3_dev_close(eth_dev);
6670 hw->adapter_state = HNS3_NIC_REMOVED;
6675 eth_hns3_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
6676 struct rte_pci_device *pci_dev)
6678 return rte_eth_dev_pci_generic_probe(pci_dev,
6679 sizeof(struct hns3_adapter),
6684 eth_hns3_pci_remove(struct rte_pci_device *pci_dev)
6686 return rte_eth_dev_pci_generic_remove(pci_dev, hns3_dev_uninit);
6689 static const struct rte_pci_id pci_id_hns3_map[] = {
6690 { RTE_PCI_DEVICE(PCI_VENDOR_ID_HUAWEI, HNS3_DEV_ID_GE) },
6691 { RTE_PCI_DEVICE(PCI_VENDOR_ID_HUAWEI, HNS3_DEV_ID_25GE) },
6692 { RTE_PCI_DEVICE(PCI_VENDOR_ID_HUAWEI, HNS3_DEV_ID_25GE_RDMA) },
6693 { RTE_PCI_DEVICE(PCI_VENDOR_ID_HUAWEI, HNS3_DEV_ID_50GE_RDMA) },
6694 { RTE_PCI_DEVICE(PCI_VENDOR_ID_HUAWEI, HNS3_DEV_ID_100G_RDMA_MACSEC) },
6695 { RTE_PCI_DEVICE(PCI_VENDOR_ID_HUAWEI, HNS3_DEV_ID_200G_RDMA) },
6696 { .vendor_id = 0, }, /* sentinel */
6699 static struct rte_pci_driver rte_hns3_pmd = {
6700 .id_table = pci_id_hns3_map,
6701 .drv_flags = RTE_PCI_DRV_NEED_MAPPING,
6702 .probe = eth_hns3_pci_probe,
6703 .remove = eth_hns3_pci_remove,
6706 RTE_PMD_REGISTER_PCI(net_hns3, rte_hns3_pmd);
6707 RTE_PMD_REGISTER_PCI_TABLE(net_hns3, pci_id_hns3_map);
6708 RTE_PMD_REGISTER_KMOD_DEP(net_hns3, "* igb_uio | vfio-pci");
6709 RTE_LOG_REGISTER(hns3_logtype_init, pmd.net.hns3.init, NOTICE);
6710 RTE_LOG_REGISTER(hns3_logtype_driver, pmd.net.hns3.driver, NOTICE);