1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2018-2019 Hisilicon Limited.
12 #include <rte_bus_pci.h>
13 #include <rte_common.h>
14 #include <rte_cycles.h>
17 #include <rte_ether.h>
18 #include <rte_ethdev_driver.h>
19 #include <rte_ethdev_pci.h>
24 #include "hns3_ethdev.h"
25 #include "hns3_logs.h"
26 #include "hns3_regs.h"
29 #define HNS3_DEFAULT_PORT_CONF_BURST_SIZE 32
30 #define HNS3_DEFAULT_PORT_CONF_QUEUES_NUM 1
32 #define HNS3_SERVICE_INTERVAL 1000000 /* us */
33 #define HNS3_PORT_BASE_VLAN_DISABLE 0
34 #define HNS3_PORT_BASE_VLAN_ENABLE 1
35 #define HNS3_INVLID_PVID 0xFFFF
37 #define HNS3_FILTER_TYPE_VF 0
38 #define HNS3_FILTER_TYPE_PORT 1
39 #define HNS3_FILTER_FE_EGRESS_V1_B BIT(0)
40 #define HNS3_FILTER_FE_NIC_INGRESS_B BIT(0)
41 #define HNS3_FILTER_FE_NIC_EGRESS_B BIT(1)
42 #define HNS3_FILTER_FE_ROCE_INGRESS_B BIT(2)
43 #define HNS3_FILTER_FE_ROCE_EGRESS_B BIT(3)
44 #define HNS3_FILTER_FE_EGRESS (HNS3_FILTER_FE_NIC_EGRESS_B \
45 | HNS3_FILTER_FE_ROCE_EGRESS_B)
46 #define HNS3_FILTER_FE_INGRESS (HNS3_FILTER_FE_NIC_INGRESS_B \
47 | HNS3_FILTER_FE_ROCE_INGRESS_B)
49 int hns3_logtype_init;
50 int hns3_logtype_driver;
52 static int hns3_vlan_pvid_configure(struct hns3_adapter *hns, uint16_t pvid,
56 hns3_set_port_vlan_filter(struct hns3_adapter *hns, uint16_t vlan_id, int on)
58 #define HNS3_VLAN_OFFSET_160 160
59 struct hns3_vlan_filter_pf_cfg_cmd *req;
60 struct hns3_hw *hw = &hns->hw;
61 uint8_t vlan_offset_byte_val;
62 struct hns3_cmd_desc desc;
63 uint8_t vlan_offset_byte;
64 uint8_t vlan_offset_160;
67 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_VLAN_FILTER_PF_CFG, false);
69 vlan_offset_160 = vlan_id / HNS3_VLAN_OFFSET_160;
70 vlan_offset_byte = (vlan_id % HNS3_VLAN_OFFSET_160) / 8;
71 vlan_offset_byte_val = 1 << (vlan_id % 8);
73 req = (struct hns3_vlan_filter_pf_cfg_cmd *)desc.data;
74 req->vlan_offset = vlan_offset_160;
75 req->vlan_cfg = on ? 0 : 1;
76 req->vlan_offset_bitmap[vlan_offset_byte] = vlan_offset_byte_val;
78 ret = hns3_cmd_send(hw, &desc, 1);
80 hns3_err(hw, "set port vlan id failed, vlan_id =%u, ret =%d",
87 hns3_rm_dev_vlan_table(struct hns3_adapter *hns, uint16_t vlan_id)
89 struct hns3_user_vlan_table *vlan_entry;
90 struct hns3_pf *pf = &hns->pf;
92 LIST_FOREACH(vlan_entry, &pf->vlan_list, next) {
93 if (vlan_entry->vlan_id == vlan_id) {
94 if (vlan_entry->hd_tbl_status)
95 hns3_set_port_vlan_filter(hns, vlan_id, 0);
96 LIST_REMOVE(vlan_entry, next);
104 hns3_add_dev_vlan_table(struct hns3_adapter *hns, uint16_t vlan_id,
107 struct hns3_user_vlan_table *vlan_entry;
108 struct hns3_hw *hw = &hns->hw;
109 struct hns3_pf *pf = &hns->pf;
111 vlan_entry = rte_zmalloc("hns3_vlan_tbl", sizeof(*vlan_entry), 0);
112 if (vlan_entry == NULL) {
113 hns3_err(hw, "Failed to malloc hns3 vlan table");
117 vlan_entry->hd_tbl_status = writen_to_tbl;
118 vlan_entry->vlan_id = vlan_id;
120 LIST_INSERT_HEAD(&pf->vlan_list, vlan_entry, next);
124 hns3_vlan_filter_configure(struct hns3_adapter *hns, uint16_t vlan_id, int on)
126 struct hns3_pf *pf = &hns->pf;
127 bool writen_to_tbl = false;
131 * When vlan filter is enabled, hardware regards vlan id 0 as the entry
132 * for normal packet, deleting vlan id 0 is not allowed.
134 if (on == 0 && vlan_id == 0)
138 * When port base vlan enabled, we use port base vlan as the vlan
139 * filter condition. In this case, we don't update vlan filter table
140 * when user add new vlan or remove exist vlan, just update the
141 * vlan list. The vlan id in vlan list will be writen in vlan filter
142 * table until port base vlan disabled
144 if (pf->port_base_vlan_cfg.state == HNS3_PORT_BASE_VLAN_DISABLE) {
145 ret = hns3_set_port_vlan_filter(hns, vlan_id, on);
146 writen_to_tbl = true;
149 if (ret == 0 && vlan_id) {
151 hns3_add_dev_vlan_table(hns, vlan_id, writen_to_tbl);
153 hns3_rm_dev_vlan_table(hns, vlan_id);
159 hns3_vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on)
161 struct hns3_adapter *hns = dev->data->dev_private;
162 struct hns3_hw *hw = &hns->hw;
165 rte_spinlock_lock(&hw->lock);
166 ret = hns3_vlan_filter_configure(hns, vlan_id, on);
167 rte_spinlock_unlock(&hw->lock);
172 hns3_vlan_tpid_configure(struct hns3_adapter *hns, enum rte_vlan_type vlan_type,
175 struct hns3_rx_vlan_type_cfg_cmd *rx_req;
176 struct hns3_tx_vlan_type_cfg_cmd *tx_req;
177 struct hns3_hw *hw = &hns->hw;
178 struct hns3_cmd_desc desc;
181 if ((vlan_type != ETH_VLAN_TYPE_INNER &&
182 vlan_type != ETH_VLAN_TYPE_OUTER)) {
183 hns3_err(hw, "Unsupported vlan type, vlan_type =%d", vlan_type);
187 if (tpid != RTE_ETHER_TYPE_VLAN) {
188 hns3_err(hw, "Unsupported vlan tpid, vlan_type =%d", vlan_type);
192 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_MAC_VLAN_TYPE_ID, false);
193 rx_req = (struct hns3_rx_vlan_type_cfg_cmd *)desc.data;
195 if (vlan_type == ETH_VLAN_TYPE_OUTER) {
196 rx_req->ot_fst_vlan_type = rte_cpu_to_le_16(tpid);
197 rx_req->ot_sec_vlan_type = rte_cpu_to_le_16(tpid);
198 } else if (vlan_type == ETH_VLAN_TYPE_INNER) {
199 rx_req->ot_fst_vlan_type = rte_cpu_to_le_16(tpid);
200 rx_req->ot_sec_vlan_type = rte_cpu_to_le_16(tpid);
201 rx_req->in_fst_vlan_type = rte_cpu_to_le_16(tpid);
202 rx_req->in_sec_vlan_type = rte_cpu_to_le_16(tpid);
205 ret = hns3_cmd_send(hw, &desc, 1);
207 hns3_err(hw, "Send rxvlan protocol type command fail, ret =%d",
212 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_MAC_VLAN_INSERT, false);
214 tx_req = (struct hns3_tx_vlan_type_cfg_cmd *)desc.data;
215 tx_req->ot_vlan_type = rte_cpu_to_le_16(tpid);
216 tx_req->in_vlan_type = rte_cpu_to_le_16(tpid);
218 ret = hns3_cmd_send(hw, &desc, 1);
220 hns3_err(hw, "Send txvlan protocol type command fail, ret =%d",
226 hns3_vlan_tpid_set(struct rte_eth_dev *dev, enum rte_vlan_type vlan_type,
229 struct hns3_adapter *hns = dev->data->dev_private;
230 struct hns3_hw *hw = &hns->hw;
233 rte_spinlock_lock(&hw->lock);
234 ret = hns3_vlan_tpid_configure(hns, vlan_type, tpid);
235 rte_spinlock_unlock(&hw->lock);
240 hns3_set_vlan_rx_offload_cfg(struct hns3_adapter *hns,
241 struct hns3_rx_vtag_cfg *vcfg)
243 struct hns3_vport_vtag_rx_cfg_cmd *req;
244 struct hns3_hw *hw = &hns->hw;
245 struct hns3_cmd_desc desc;
250 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_VLAN_PORT_RX_CFG, false);
252 req = (struct hns3_vport_vtag_rx_cfg_cmd *)desc.data;
253 hns3_set_bit(req->vport_vlan_cfg, HNS3_REM_TAG1_EN_B,
254 vcfg->strip_tag1_en ? 1 : 0);
255 hns3_set_bit(req->vport_vlan_cfg, HNS3_REM_TAG2_EN_B,
256 vcfg->strip_tag2_en ? 1 : 0);
257 hns3_set_bit(req->vport_vlan_cfg, HNS3_SHOW_TAG1_EN_B,
258 vcfg->vlan1_vlan_prionly ? 1 : 0);
259 hns3_set_bit(req->vport_vlan_cfg, HNS3_SHOW_TAG2_EN_B,
260 vcfg->vlan2_vlan_prionly ? 1 : 0);
263 * In current version VF is not supported when PF is driven by DPDK
264 * driver, the PF-related vf_id is 0, just need to configure parameters
268 req->vf_offset = vport_id / HNS3_VF_NUM_PER_CMD;
269 bitmap = 1 << (vport_id % HNS3_VF_NUM_PER_BYTE);
270 req->vf_bitmap[req->vf_offset] = bitmap;
272 ret = hns3_cmd_send(hw, &desc, 1);
274 hns3_err(hw, "Send port rxvlan cfg command fail, ret =%d", ret);
279 hns3_update_rx_offload_cfg(struct hns3_adapter *hns,
280 struct hns3_rx_vtag_cfg *vcfg)
282 struct hns3_pf *pf = &hns->pf;
283 memcpy(&pf->vtag_config.rx_vcfg, vcfg, sizeof(pf->vtag_config.rx_vcfg));
287 hns3_update_tx_offload_cfg(struct hns3_adapter *hns,
288 struct hns3_tx_vtag_cfg *vcfg)
290 struct hns3_pf *pf = &hns->pf;
291 memcpy(&pf->vtag_config.tx_vcfg, vcfg, sizeof(pf->vtag_config.tx_vcfg));
295 hns3_en_hw_strip_rxvtag(struct hns3_adapter *hns, bool enable)
297 struct hns3_rx_vtag_cfg rxvlan_cfg;
298 struct hns3_pf *pf = &hns->pf;
299 struct hns3_hw *hw = &hns->hw;
302 if (pf->port_base_vlan_cfg.state == HNS3_PORT_BASE_VLAN_DISABLE) {
303 rxvlan_cfg.strip_tag1_en = false;
304 rxvlan_cfg.strip_tag2_en = enable;
306 rxvlan_cfg.strip_tag1_en = enable;
307 rxvlan_cfg.strip_tag2_en = true;
310 rxvlan_cfg.vlan1_vlan_prionly = false;
311 rxvlan_cfg.vlan2_vlan_prionly = false;
312 rxvlan_cfg.rx_vlan_offload_en = enable;
314 ret = hns3_set_vlan_rx_offload_cfg(hns, &rxvlan_cfg);
316 hns3_err(hw, "enable strip rx vtag failed, ret =%d", ret);
320 hns3_update_rx_offload_cfg(hns, &rxvlan_cfg);
326 hns3_set_vlan_filter_ctrl(struct hns3_hw *hw, uint8_t vlan_type,
327 uint8_t fe_type, bool filter_en, uint8_t vf_id)
329 struct hns3_vlan_filter_ctrl_cmd *req;
330 struct hns3_cmd_desc desc;
333 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_VLAN_FILTER_CTRL, false);
335 req = (struct hns3_vlan_filter_ctrl_cmd *)desc.data;
336 req->vlan_type = vlan_type;
337 req->vlan_fe = filter_en ? fe_type : 0;
340 ret = hns3_cmd_send(hw, &desc, 1);
342 hns3_err(hw, "set vlan filter fail, ret =%d", ret);
348 hns3_enable_vlan_filter(struct hns3_adapter *hns, bool enable)
350 struct hns3_hw *hw = &hns->hw;
353 ret = hns3_set_vlan_filter_ctrl(hw, HNS3_FILTER_TYPE_VF,
354 HNS3_FILTER_FE_EGRESS, false, 0);
356 hns3_err(hw, "hns3 enable filter fail, ret =%d", ret);
360 ret = hns3_set_vlan_filter_ctrl(hw, HNS3_FILTER_TYPE_PORT,
361 HNS3_FILTER_FE_INGRESS, enable, 0);
363 hns3_err(hw, "hns3 enable filter fail, ret =%d", ret);
369 hns3_vlan_offload_set(struct rte_eth_dev *dev, int mask)
371 struct hns3_adapter *hns = dev->data->dev_private;
372 struct hns3_hw *hw = &hns->hw;
373 struct rte_eth_rxmode *rxmode;
374 unsigned int tmp_mask;
378 rte_spinlock_lock(&hw->lock);
379 rxmode = &dev->data->dev_conf.rxmode;
380 tmp_mask = (unsigned int)mask;
381 if (tmp_mask & ETH_VLAN_STRIP_MASK) {
382 /* Enable or disable VLAN stripping */
383 enable = rxmode->offloads & DEV_RX_OFFLOAD_VLAN_STRIP ?
386 ret = hns3_en_hw_strip_rxvtag(hns, enable);
388 rte_spinlock_unlock(&hw->lock);
389 hns3_err(hw, "failed to enable rx strip, ret =%d", ret);
394 rte_spinlock_unlock(&hw->lock);
400 hns3_set_vlan_tx_offload_cfg(struct hns3_adapter *hns,
401 struct hns3_tx_vtag_cfg *vcfg)
403 struct hns3_vport_vtag_tx_cfg_cmd *req;
404 struct hns3_cmd_desc desc;
405 struct hns3_hw *hw = &hns->hw;
410 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_VLAN_PORT_TX_CFG, false);
412 req = (struct hns3_vport_vtag_tx_cfg_cmd *)desc.data;
413 req->def_vlan_tag1 = vcfg->default_tag1;
414 req->def_vlan_tag2 = vcfg->default_tag2;
415 hns3_set_bit(req->vport_vlan_cfg, HNS3_ACCEPT_TAG1_B,
416 vcfg->accept_tag1 ? 1 : 0);
417 hns3_set_bit(req->vport_vlan_cfg, HNS3_ACCEPT_UNTAG1_B,
418 vcfg->accept_untag1 ? 1 : 0);
419 hns3_set_bit(req->vport_vlan_cfg, HNS3_ACCEPT_TAG2_B,
420 vcfg->accept_tag2 ? 1 : 0);
421 hns3_set_bit(req->vport_vlan_cfg, HNS3_ACCEPT_UNTAG2_B,
422 vcfg->accept_untag2 ? 1 : 0);
423 hns3_set_bit(req->vport_vlan_cfg, HNS3_PORT_INS_TAG1_EN_B,
424 vcfg->insert_tag1_en ? 1 : 0);
425 hns3_set_bit(req->vport_vlan_cfg, HNS3_PORT_INS_TAG2_EN_B,
426 vcfg->insert_tag2_en ? 1 : 0);
427 hns3_set_bit(req->vport_vlan_cfg, HNS3_CFG_NIC_ROCE_SEL_B, 0);
430 * In current version VF is not supported when PF is driven by DPDK
431 * driver, the PF-related vf_id is 0, just need to configure parameters
435 req->vf_offset = vport_id / HNS3_VF_NUM_PER_CMD;
436 bitmap = 1 << (vport_id % HNS3_VF_NUM_PER_BYTE);
437 req->vf_bitmap[req->vf_offset] = bitmap;
439 ret = hns3_cmd_send(hw, &desc, 1);
441 hns3_err(hw, "Send port txvlan cfg command fail, ret =%d", ret);
447 hns3_vlan_txvlan_cfg(struct hns3_adapter *hns, uint16_t port_base_vlan_state,
450 struct hns3_hw *hw = &hns->hw;
451 struct hns3_tx_vtag_cfg txvlan_cfg;
454 if (port_base_vlan_state == HNS3_PORT_BASE_VLAN_DISABLE) {
455 txvlan_cfg.accept_tag1 = true;
456 txvlan_cfg.insert_tag1_en = false;
457 txvlan_cfg.default_tag1 = 0;
459 txvlan_cfg.accept_tag1 = false;
460 txvlan_cfg.insert_tag1_en = true;
461 txvlan_cfg.default_tag1 = pvid;
464 txvlan_cfg.accept_untag1 = true;
465 txvlan_cfg.accept_tag2 = true;
466 txvlan_cfg.accept_untag2 = true;
467 txvlan_cfg.insert_tag2_en = false;
468 txvlan_cfg.default_tag2 = 0;
470 ret = hns3_set_vlan_tx_offload_cfg(hns, &txvlan_cfg);
472 hns3_err(hw, "pf vlan set pvid failed, pvid =%u ,ret =%d", pvid,
477 hns3_update_tx_offload_cfg(hns, &txvlan_cfg);
482 hns3_store_port_base_vlan_info(struct hns3_adapter *hns, uint16_t pvid, int on)
484 struct hns3_pf *pf = &hns->pf;
486 pf->port_base_vlan_cfg.state = on ?
487 HNS3_PORT_BASE_VLAN_ENABLE : HNS3_PORT_BASE_VLAN_DISABLE;
489 pf->port_base_vlan_cfg.pvid = pvid;
493 hns3_rm_all_vlan_table(struct hns3_adapter *hns, bool is_del_list)
495 struct hns3_user_vlan_table *vlan_entry;
496 struct hns3_pf *pf = &hns->pf;
498 LIST_FOREACH(vlan_entry, &pf->vlan_list, next) {
499 if (vlan_entry->hd_tbl_status)
500 hns3_set_port_vlan_filter(hns, vlan_entry->vlan_id, 0);
502 vlan_entry->hd_tbl_status = false;
506 vlan_entry = LIST_FIRST(&pf->vlan_list);
508 LIST_REMOVE(vlan_entry, next);
509 rte_free(vlan_entry);
510 vlan_entry = LIST_FIRST(&pf->vlan_list);
516 hns3_add_all_vlan_table(struct hns3_adapter *hns)
518 struct hns3_user_vlan_table *vlan_entry;
519 struct hns3_pf *pf = &hns->pf;
521 LIST_FOREACH(vlan_entry, &pf->vlan_list, next) {
522 if (!vlan_entry->hd_tbl_status)
523 hns3_set_port_vlan_filter(hns, vlan_entry->vlan_id, 1);
525 vlan_entry->hd_tbl_status = true;
530 hns3_update_vlan_filter_entries(struct hns3_adapter *hns,
531 uint16_t port_base_vlan_state,
532 uint16_t new_pvid, uint16_t old_pvid)
534 struct hns3_pf *pf = &hns->pf;
535 struct hns3_hw *hw = &hns->hw;
538 if (port_base_vlan_state == HNS3_PORT_BASE_VLAN_ENABLE) {
539 if (old_pvid != HNS3_INVLID_PVID && old_pvid != 0) {
540 ret = hns3_set_port_vlan_filter(hns, old_pvid, 0);
543 "Failed to clear clear old pvid filter, ret =%d",
549 hns3_rm_all_vlan_table(hns, false);
550 return hns3_set_port_vlan_filter(hns, new_pvid, 1);
554 ret = hns3_set_port_vlan_filter(hns, new_pvid, 0);
556 hns3_err(hw, "Failed to set port vlan filter, ret =%d",
562 if (new_pvid == pf->port_base_vlan_cfg.pvid)
563 hns3_add_all_vlan_table(hns);
569 hns3_en_rx_strip_all(struct hns3_adapter *hns, int on)
571 struct hns3_rx_vtag_cfg rx_vlan_cfg;
572 struct hns3_hw *hw = &hns->hw;
576 rx_strip_en = on ? true : false;
577 rx_vlan_cfg.strip_tag1_en = rx_strip_en;
578 rx_vlan_cfg.strip_tag2_en = rx_strip_en;
579 rx_vlan_cfg.vlan1_vlan_prionly = false;
580 rx_vlan_cfg.vlan2_vlan_prionly = false;
581 rx_vlan_cfg.rx_vlan_offload_en = rx_strip_en;
583 ret = hns3_set_vlan_rx_offload_cfg(hns, &rx_vlan_cfg);
585 hns3_err(hw, "enable strip rx failed, ret =%d", ret);
589 hns3_update_rx_offload_cfg(hns, &rx_vlan_cfg);
594 hns3_vlan_pvid_configure(struct hns3_adapter *hns, uint16_t pvid, int on)
596 struct hns3_pf *pf = &hns->pf;
597 struct hns3_hw *hw = &hns->hw;
598 uint16_t port_base_vlan_state;
602 if (on == 0 && pvid != pf->port_base_vlan_cfg.pvid) {
603 if (pf->port_base_vlan_cfg.pvid != HNS3_INVLID_PVID)
604 hns3_warn(hw, "Invalid operation! As current pvid set "
605 "is %u, disable pvid %u is invalid",
606 pf->port_base_vlan_cfg.pvid, pvid);
610 port_base_vlan_state = on ? HNS3_PORT_BASE_VLAN_ENABLE :
611 HNS3_PORT_BASE_VLAN_DISABLE;
612 ret = hns3_vlan_txvlan_cfg(hns, port_base_vlan_state, pvid);
614 hns3_err(hw, "Failed to config tx vlan, ret =%d", ret);
618 ret = hns3_en_rx_strip_all(hns, on);
620 hns3_err(hw, "Failed to config rx vlan strip, ret =%d", ret);
624 if (pvid == HNS3_INVLID_PVID)
626 old_pvid = pf->port_base_vlan_cfg.pvid;
627 ret = hns3_update_vlan_filter_entries(hns, port_base_vlan_state, pvid,
630 hns3_err(hw, "Failed to update vlan filter entries, ret =%d",
636 hns3_store_port_base_vlan_info(hns, pvid, on);
641 hns3_vlan_pvid_set(struct rte_eth_dev *dev, uint16_t pvid, int on)
643 struct hns3_adapter *hns = dev->data->dev_private;
644 struct hns3_hw *hw = &hns->hw;
647 rte_spinlock_lock(&hw->lock);
648 ret = hns3_vlan_pvid_configure(hns, pvid, on);
649 rte_spinlock_unlock(&hw->lock);
654 init_port_base_vlan_info(struct hns3_hw *hw)
656 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
657 struct hns3_pf *pf = &hns->pf;
659 pf->port_base_vlan_cfg.state = HNS3_PORT_BASE_VLAN_DISABLE;
660 pf->port_base_vlan_cfg.pvid = HNS3_INVLID_PVID;
664 hns3_default_vlan_config(struct hns3_adapter *hns)
666 struct hns3_hw *hw = &hns->hw;
669 ret = hns3_set_port_vlan_filter(hns, 0, 1);
671 hns3_err(hw, "default vlan 0 config failed, ret =%d", ret);
676 hns3_init_vlan_config(struct hns3_adapter *hns)
678 struct hns3_hw *hw = &hns->hw;
682 * This function can be called in the initialization and reset process,
683 * when in reset process, it means that hardware had been reseted
684 * successfully and we need to restore the hardware configuration to
685 * ensure that the hardware configuration remains unchanged before and
688 if (rte_atomic16_read(&hw->reset.resetting) == 0)
689 init_port_base_vlan_info(hw);
691 ret = hns3_enable_vlan_filter(hns, true);
693 hns3_err(hw, "vlan init fail in pf, ret =%d", ret);
697 ret = hns3_vlan_tpid_configure(hns, ETH_VLAN_TYPE_INNER,
698 RTE_ETHER_TYPE_VLAN);
700 hns3_err(hw, "tpid set fail in pf, ret =%d", ret);
705 * When in the reinit dev stage of the reset process, the following
706 * vlan-related configurations may differ from those at initialization,
707 * we will restore configurations to hardware in hns3_restore_vlan_table
708 * and hns3_restore_vlan_conf later.
710 if (rte_atomic16_read(&hw->reset.resetting) == 0) {
711 ret = hns3_vlan_pvid_configure(hns, HNS3_INVLID_PVID, 0);
713 hns3_err(hw, "pvid set fail in pf, ret =%d", ret);
717 ret = hns3_en_hw_strip_rxvtag(hns, false);
719 hns3_err(hw, "rx strip configure fail in pf, ret =%d",
725 return hns3_default_vlan_config(hns);
729 hns3_config_tso(struct hns3_hw *hw, unsigned int tso_mss_min,
730 unsigned int tso_mss_max)
732 struct hns3_cfg_tso_status_cmd *req;
733 struct hns3_cmd_desc desc;
736 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_TSO_GENERIC_CONFIG, false);
738 req = (struct hns3_cfg_tso_status_cmd *)desc.data;
741 hns3_set_field(tso_mss, HNS3_TSO_MSS_MIN_M, HNS3_TSO_MSS_MIN_S,
743 req->tso_mss_min = rte_cpu_to_le_16(tso_mss);
746 hns3_set_field(tso_mss, HNS3_TSO_MSS_MIN_M, HNS3_TSO_MSS_MIN_S,
748 req->tso_mss_max = rte_cpu_to_le_16(tso_mss);
750 return hns3_cmd_send(hw, &desc, 1);
754 hns3_config_gro(struct hns3_hw *hw, bool en)
756 struct hns3_cfg_gro_status_cmd *req;
757 struct hns3_cmd_desc desc;
760 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_GRO_GENERIC_CONFIG, false);
761 req = (struct hns3_cfg_gro_status_cmd *)desc.data;
763 req->gro_en = rte_cpu_to_le_16(en ? 1 : 0);
765 ret = hns3_cmd_send(hw, &desc, 1);
767 hns3_err(hw, "GRO hardware config cmd failed, ret = %d", ret);
773 hns3_set_umv_space(struct hns3_hw *hw, uint16_t space_size,
774 uint16_t *allocated_size, bool is_alloc)
776 struct hns3_umv_spc_alc_cmd *req;
777 struct hns3_cmd_desc desc;
780 req = (struct hns3_umv_spc_alc_cmd *)desc.data;
781 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_MAC_VLAN_ALLOCATE, false);
782 hns3_set_bit(req->allocate, HNS3_UMV_SPC_ALC_B, is_alloc ? 0 : 1);
783 req->space_size = rte_cpu_to_le_32(space_size);
785 ret = hns3_cmd_send(hw, &desc, 1);
787 PMD_INIT_LOG(ERR, "%s umv space failed for cmd_send, ret =%d",
788 is_alloc ? "allocate" : "free", ret);
792 if (is_alloc && allocated_size)
793 *allocated_size = rte_le_to_cpu_32(desc.data[1]);
799 hns3_init_umv_space(struct hns3_hw *hw)
801 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
802 struct hns3_pf *pf = &hns->pf;
803 uint16_t allocated_size = 0;
806 ret = hns3_set_umv_space(hw, pf->wanted_umv_size, &allocated_size,
811 if (allocated_size < pf->wanted_umv_size)
812 PMD_INIT_LOG(WARNING, "Alloc umv space failed, want %u, get %u",
813 pf->wanted_umv_size, allocated_size);
815 pf->max_umv_size = (!!allocated_size) ? allocated_size :
817 pf->used_umv_size = 0;
822 hns3_uninit_umv_space(struct hns3_hw *hw)
824 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
825 struct hns3_pf *pf = &hns->pf;
828 if (pf->max_umv_size == 0)
831 ret = hns3_set_umv_space(hw, pf->max_umv_size, NULL, false);
835 pf->max_umv_size = 0;
841 hns3_is_umv_space_full(struct hns3_hw *hw)
843 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
844 struct hns3_pf *pf = &hns->pf;
847 is_full = (pf->used_umv_size >= pf->max_umv_size);
853 hns3_update_umv_space(struct hns3_hw *hw, bool is_free)
855 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
856 struct hns3_pf *pf = &hns->pf;
859 if (pf->used_umv_size > 0)
866 hns3_prepare_mac_addr(struct hns3_mac_vlan_tbl_entry_cmd *new_req,
867 const uint8_t *addr, bool is_mc)
869 const unsigned char *mac_addr = addr;
870 uint32_t high_val = ((uint32_t)mac_addr[3] << 24) |
871 ((uint32_t)mac_addr[2] << 16) |
872 ((uint32_t)mac_addr[1] << 8) |
873 (uint32_t)mac_addr[0];
874 uint32_t low_val = ((uint32_t)mac_addr[5] << 8) | (uint32_t)mac_addr[4];
876 hns3_set_bit(new_req->flags, HNS3_MAC_VLAN_BIT0_EN_B, 1);
878 hns3_set_bit(new_req->entry_type, HNS3_MAC_VLAN_BIT0_EN_B, 0);
879 hns3_set_bit(new_req->entry_type, HNS3_MAC_VLAN_BIT1_EN_B, 1);
880 hns3_set_bit(new_req->mc_mac_en, HNS3_MAC_VLAN_BIT0_EN_B, 1);
883 new_req->mac_addr_hi32 = rte_cpu_to_le_32(high_val);
884 new_req->mac_addr_lo16 = rte_cpu_to_le_16(low_val & 0xffff);
888 hns3_get_mac_vlan_cmd_status(struct hns3_hw *hw, uint16_t cmdq_resp,
890 enum hns3_mac_vlan_tbl_opcode op)
893 hns3_err(hw, "cmdq execute failed for get_mac_vlan_cmd_status,status=%u",
898 if (op == HNS3_MAC_VLAN_ADD) {
899 if (resp_code == 0 || resp_code == 1) {
901 } else if (resp_code == HNS3_ADD_UC_OVERFLOW) {
902 hns3_err(hw, "add mac addr failed for uc_overflow");
904 } else if (resp_code == HNS3_ADD_MC_OVERFLOW) {
905 hns3_err(hw, "add mac addr failed for mc_overflow");
909 hns3_err(hw, "add mac addr failed for undefined, code=%u",
912 } else if (op == HNS3_MAC_VLAN_REMOVE) {
913 if (resp_code == 0) {
915 } else if (resp_code == 1) {
916 hns3_dbg(hw, "remove mac addr failed for miss");
920 hns3_err(hw, "remove mac addr failed for undefined, code=%u",
923 } else if (op == HNS3_MAC_VLAN_LKUP) {
924 if (resp_code == 0) {
926 } else if (resp_code == 1) {
927 hns3_dbg(hw, "lookup mac addr failed for miss");
931 hns3_err(hw, "lookup mac addr failed for undefined, code=%u",
936 hns3_err(hw, "unknown opcode for get_mac_vlan_cmd_status, opcode=%u",
943 hns3_lookup_mac_vlan_tbl(struct hns3_hw *hw,
944 struct hns3_mac_vlan_tbl_entry_cmd *req,
945 struct hns3_cmd_desc *desc, bool is_mc)
951 hns3_cmd_setup_basic_desc(&desc[0], HNS3_OPC_MAC_VLAN_ADD, true);
953 desc[0].flag |= rte_cpu_to_le_16(HNS3_CMD_FLAG_NEXT);
954 memcpy(desc[0].data, req,
955 sizeof(struct hns3_mac_vlan_tbl_entry_cmd));
956 hns3_cmd_setup_basic_desc(&desc[1], HNS3_OPC_MAC_VLAN_ADD,
958 desc[1].flag |= rte_cpu_to_le_16(HNS3_CMD_FLAG_NEXT);
959 hns3_cmd_setup_basic_desc(&desc[2], HNS3_OPC_MAC_VLAN_ADD,
961 ret = hns3_cmd_send(hw, desc, HNS3_MC_MAC_VLAN_ADD_DESC_NUM);
963 memcpy(desc[0].data, req,
964 sizeof(struct hns3_mac_vlan_tbl_entry_cmd));
965 ret = hns3_cmd_send(hw, desc, 1);
968 hns3_err(hw, "lookup mac addr failed for cmd_send, ret =%d.",
972 resp_code = (rte_le_to_cpu_32(desc[0].data[0]) >> 8) & 0xff;
973 retval = rte_le_to_cpu_16(desc[0].retval);
975 return hns3_get_mac_vlan_cmd_status(hw, retval, resp_code,
980 hns3_add_mac_vlan_tbl(struct hns3_hw *hw,
981 struct hns3_mac_vlan_tbl_entry_cmd *req,
982 struct hns3_cmd_desc *mc_desc)
989 if (mc_desc == NULL) {
990 struct hns3_cmd_desc desc;
992 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_MAC_VLAN_ADD, false);
993 memcpy(desc.data, req,
994 sizeof(struct hns3_mac_vlan_tbl_entry_cmd));
995 ret = hns3_cmd_send(hw, &desc, 1);
996 resp_code = (rte_le_to_cpu_32(desc.data[0]) >> 8) & 0xff;
997 retval = rte_le_to_cpu_16(desc.retval);
999 cfg_status = hns3_get_mac_vlan_cmd_status(hw, retval, resp_code,
1002 hns3_cmd_reuse_desc(&mc_desc[0], false);
1003 mc_desc[0].flag |= rte_cpu_to_le_16(HNS3_CMD_FLAG_NEXT);
1004 hns3_cmd_reuse_desc(&mc_desc[1], false);
1005 mc_desc[1].flag |= rte_cpu_to_le_16(HNS3_CMD_FLAG_NEXT);
1006 hns3_cmd_reuse_desc(&mc_desc[2], false);
1007 mc_desc[2].flag &= rte_cpu_to_le_16(~HNS3_CMD_FLAG_NEXT);
1008 memcpy(mc_desc[0].data, req,
1009 sizeof(struct hns3_mac_vlan_tbl_entry_cmd));
1010 mc_desc[0].retval = 0;
1011 ret = hns3_cmd_send(hw, mc_desc, HNS3_MC_MAC_VLAN_ADD_DESC_NUM);
1012 resp_code = (rte_le_to_cpu_32(mc_desc[0].data[0]) >> 8) & 0xff;
1013 retval = rte_le_to_cpu_16(mc_desc[0].retval);
1015 cfg_status = hns3_get_mac_vlan_cmd_status(hw, retval, resp_code,
1020 hns3_err(hw, "add mac addr failed for cmd_send, ret =%d", ret);
1028 hns3_remove_mac_vlan_tbl(struct hns3_hw *hw,
1029 struct hns3_mac_vlan_tbl_entry_cmd *req)
1031 struct hns3_cmd_desc desc;
1036 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_MAC_VLAN_REMOVE, false);
1038 memcpy(desc.data, req, sizeof(struct hns3_mac_vlan_tbl_entry_cmd));
1040 ret = hns3_cmd_send(hw, &desc, 1);
1042 hns3_err(hw, "del mac addr failed for cmd_send, ret =%d", ret);
1045 resp_code = (rte_le_to_cpu_32(desc.data[0]) >> 8) & 0xff;
1046 retval = rte_le_to_cpu_16(desc.retval);
1048 return hns3_get_mac_vlan_cmd_status(hw, retval, resp_code,
1049 HNS3_MAC_VLAN_REMOVE);
1053 hns3_add_uc_addr_common(struct hns3_hw *hw, struct rte_ether_addr *mac_addr)
1055 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
1056 struct hns3_mac_vlan_tbl_entry_cmd req;
1057 struct hns3_pf *pf = &hns->pf;
1058 struct hns3_cmd_desc desc;
1059 char mac_str[RTE_ETHER_ADDR_FMT_SIZE];
1060 uint16_t egress_port = 0;
1064 /* check if mac addr is valid */
1065 if (!rte_is_valid_assigned_ether_addr(mac_addr)) {
1066 rte_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
1068 hns3_err(hw, "Add unicast mac addr err! addr(%s) invalid",
1073 memset(&req, 0, sizeof(req));
1076 * In current version VF is not supported when PF is driven by DPDK
1077 * driver, the PF-related vf_id is 0, just need to configure parameters
1081 hns3_set_field(egress_port, HNS3_MAC_EPORT_VFID_M,
1082 HNS3_MAC_EPORT_VFID_S, vf_id);
1084 req.egress_port = rte_cpu_to_le_16(egress_port);
1086 hns3_prepare_mac_addr(&req, mac_addr->addr_bytes, false);
1089 * Lookup the mac address in the mac_vlan table, and add
1090 * it if the entry is inexistent. Repeated unicast entry
1091 * is not allowed in the mac vlan table.
1093 ret = hns3_lookup_mac_vlan_tbl(hw, &req, &desc, false);
1094 if (ret == -ENOENT) {
1095 if (!hns3_is_umv_space_full(hw)) {
1096 ret = hns3_add_mac_vlan_tbl(hw, &req, NULL);
1098 hns3_update_umv_space(hw, false);
1102 hns3_err(hw, "UC MAC table full(%u)", pf->used_umv_size);
1107 rte_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE, mac_addr);
1109 /* check if we just hit the duplicate */
1111 hns3_dbg(hw, "mac addr(%s) has been in the MAC table", mac_str);
1115 hns3_err(hw, "PF failed to add unicast entry(%s) in the MAC table",
1122 hns3_add_mac_addr(struct rte_eth_dev *dev, struct rte_ether_addr *mac_addr,
1123 uint32_t idx, __attribute__ ((unused)) uint32_t pool)
1125 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1126 char mac_str[RTE_ETHER_ADDR_FMT_SIZE];
1129 rte_spinlock_lock(&hw->lock);
1130 ret = hns3_add_uc_addr_common(hw, mac_addr);
1132 rte_spinlock_unlock(&hw->lock);
1133 rte_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
1135 hns3_err(hw, "Failed to add mac addr(%s): %d", mac_str, ret);
1140 hw->mac.default_addr_setted = true;
1141 rte_spinlock_unlock(&hw->lock);
1147 hns3_remove_uc_addr_common(struct hns3_hw *hw, struct rte_ether_addr *mac_addr)
1149 struct hns3_mac_vlan_tbl_entry_cmd req;
1150 char mac_str[RTE_ETHER_ADDR_FMT_SIZE];
1153 /* check if mac addr is valid */
1154 if (!rte_is_valid_assigned_ether_addr(mac_addr)) {
1155 rte_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
1157 hns3_err(hw, "Remove unicast mac addr err! addr(%s) invalid",
1162 memset(&req, 0, sizeof(req));
1163 hns3_set_bit(req.entry_type, HNS3_MAC_VLAN_BIT0_EN_B, 0);
1164 hns3_prepare_mac_addr(&req, mac_addr->addr_bytes, false);
1165 ret = hns3_remove_mac_vlan_tbl(hw, &req);
1166 if (ret == -ENOENT) /* mac addr isn't existent in the mac vlan table. */
1169 hns3_update_umv_space(hw, true);
1175 hns3_remove_mac_addr(struct rte_eth_dev *dev, uint32_t idx)
1177 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1178 /* index will be checked by upper level rte interface */
1179 struct rte_ether_addr *mac_addr = &dev->data->mac_addrs[idx];
1180 char mac_str[RTE_ETHER_ADDR_FMT_SIZE];
1183 rte_spinlock_lock(&hw->lock);
1184 ret = hns3_remove_uc_addr_common(hw, mac_addr);
1186 rte_spinlock_unlock(&hw->lock);
1187 rte_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
1189 hns3_err(hw, "Failed to remove mac addr(%s): %d", mac_str, ret);
1194 hw->mac.default_addr_setted = false;
1195 rte_spinlock_unlock(&hw->lock);
1199 hns3_set_default_mac_addr(struct rte_eth_dev *dev,
1200 struct rte_ether_addr *mac_addr)
1202 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1203 struct rte_ether_addr *oaddr;
1204 char mac_str[RTE_ETHER_ADDR_FMT_SIZE];
1205 bool default_addr_setted;
1206 bool rm_succes = false;
1209 /* check if mac addr is valid */
1210 if (!rte_is_valid_assigned_ether_addr(mac_addr)) {
1211 rte_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
1213 hns3_err(hw, "Failed to set mac addr, addr(%s) invalid",
1218 oaddr = (struct rte_ether_addr *)hw->mac.mac_addr;
1219 default_addr_setted = hw->mac.default_addr_setted;
1220 if (default_addr_setted && !!rte_is_same_ether_addr(mac_addr, oaddr))
1223 rte_spinlock_lock(&hw->lock);
1224 if (default_addr_setted) {
1225 ret = hns3_remove_uc_addr_common(hw, oaddr);
1227 rte_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
1229 hns3_warn(hw, "Remove old uc mac address(%s) fail: %d",
1236 ret = hns3_add_uc_addr_common(hw, mac_addr);
1238 rte_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
1240 hns3_err(hw, "Failed to set mac addr(%s): %d", mac_str, ret);
1241 goto err_add_uc_addr;
1244 rte_ether_addr_copy(mac_addr,
1245 (struct rte_ether_addr *)hw->mac.mac_addr);
1246 hw->mac.default_addr_setted = true;
1247 rte_spinlock_unlock(&hw->lock);
1253 ret_val = hns3_add_uc_addr_common(hw, oaddr);
1255 rte_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
1258 "Failed to restore old uc mac addr(%s): %d",
1260 hw->mac.default_addr_setted = false;
1263 rte_spinlock_unlock(&hw->lock);
1269 hns3_update_desc_vfid(struct hns3_cmd_desc *desc, uint8_t vfid, bool clr)
1271 #define HNS3_VF_NUM_IN_FIRST_DESC 192
1275 if (vfid < HNS3_VF_NUM_IN_FIRST_DESC) {
1276 word_num = vfid / 32;
1277 bit_num = vfid % 32;
1279 desc[1].data[word_num] &=
1280 rte_cpu_to_le_32(~(1UL << bit_num));
1282 desc[1].data[word_num] |=
1283 rte_cpu_to_le_32(1UL << bit_num);
1285 word_num = (vfid - HNS3_VF_NUM_IN_FIRST_DESC) / 32;
1286 bit_num = vfid % 32;
1288 desc[2].data[word_num] &=
1289 rte_cpu_to_le_32(~(1UL << bit_num));
1291 desc[2].data[word_num] |=
1292 rte_cpu_to_le_32(1UL << bit_num);
1297 hns3_add_mc_addr(struct hns3_hw *hw, struct rte_ether_addr *mac_addr)
1299 struct hns3_mac_vlan_tbl_entry_cmd req;
1300 struct hns3_cmd_desc desc[3];
1301 char mac_str[RTE_ETHER_ADDR_FMT_SIZE];
1305 /* Check if mac addr is valid */
1306 if (!rte_is_multicast_ether_addr(mac_addr)) {
1307 rte_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
1309 hns3_err(hw, "Failed to add mc mac addr, addr(%s) invalid",
1314 memset(&req, 0, sizeof(req));
1315 hns3_set_bit(req.entry_type, HNS3_MAC_VLAN_BIT0_EN_B, 0);
1316 hns3_prepare_mac_addr(&req, mac_addr->addr_bytes, true);
1317 ret = hns3_lookup_mac_vlan_tbl(hw, &req, desc, true);
1319 /* This mac addr do not exist, add new entry for it */
1320 memset(desc[0].data, 0, sizeof(desc[0].data));
1321 memset(desc[1].data, 0, sizeof(desc[0].data));
1322 memset(desc[2].data, 0, sizeof(desc[0].data));
1326 * In current version VF is not supported when PF is driven by DPDK
1327 * driver, the PF-related vf_id is 0, just need to configure parameters
1331 hns3_update_desc_vfid(desc, vf_id, false);
1332 ret = hns3_add_mac_vlan_tbl(hw, &req, desc);
1335 hns3_err(hw, "mc mac vlan table is full");
1336 rte_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
1338 hns3_err(hw, "Failed to add mc mac addr(%s): %d", mac_str, ret);
1345 hns3_remove_mc_addr(struct hns3_hw *hw, struct rte_ether_addr *mac_addr)
1347 struct hns3_mac_vlan_tbl_entry_cmd req;
1348 struct hns3_cmd_desc desc[3];
1349 char mac_str[RTE_ETHER_ADDR_FMT_SIZE];
1353 /* Check if mac addr is valid */
1354 if (!rte_is_multicast_ether_addr(mac_addr)) {
1355 rte_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
1357 hns3_err(hw, "Failed to rm mc mac addr, addr(%s) invalid",
1362 memset(&req, 0, sizeof(req));
1363 hns3_set_bit(req.entry_type, HNS3_MAC_VLAN_BIT0_EN_B, 0);
1364 hns3_prepare_mac_addr(&req, mac_addr->addr_bytes, true);
1365 ret = hns3_lookup_mac_vlan_tbl(hw, &req, desc, true);
1368 * This mac addr exist, remove this handle's VFID for it.
1369 * In current version VF is not supported when PF is driven by
1370 * DPDK driver, the PF-related vf_id is 0, just need to
1371 * configure parameters for vf_id 0.
1374 hns3_update_desc_vfid(desc, vf_id, true);
1376 /* All the vfid is zero, so need to delete this entry */
1377 ret = hns3_remove_mac_vlan_tbl(hw, &req);
1378 } else if (ret == -ENOENT) {
1379 /* This mac addr doesn't exist. */
1384 rte_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
1386 hns3_err(hw, "Failed to rm mc mac addr(%s): %d", mac_str, ret);
1393 hns3_set_mc_addr_chk_param(struct hns3_hw *hw,
1394 struct rte_ether_addr *mc_addr_set,
1395 uint32_t nb_mc_addr)
1397 char mac_str[RTE_ETHER_ADDR_FMT_SIZE];
1398 struct rte_ether_addr *addr;
1402 if (nb_mc_addr > HNS3_MC_MACADDR_NUM) {
1403 hns3_err(hw, "Failed to set mc mac addr, nb_mc_addr(%d) "
1404 "invalid. valid range: 0~%d",
1405 nb_mc_addr, HNS3_MC_MACADDR_NUM);
1409 /* Check if input mac addresses are valid */
1410 for (i = 0; i < nb_mc_addr; i++) {
1411 addr = &mc_addr_set[i];
1412 if (!rte_is_multicast_ether_addr(addr)) {
1413 rte_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
1416 "Failed to set mc mac addr, addr(%s) invalid.",
1421 /* Check if there are duplicate addresses */
1422 for (j = i + 1; j < nb_mc_addr; j++) {
1423 if (rte_is_same_ether_addr(addr, &mc_addr_set[j])) {
1424 rte_ether_format_addr(mac_str,
1425 RTE_ETHER_ADDR_FMT_SIZE,
1427 hns3_err(hw, "Failed to set mc mac addr, "
1428 "addrs invalid. two same addrs(%s).",
1439 hns3_set_mc_addr_calc_addr(struct hns3_hw *hw,
1440 struct rte_ether_addr *mc_addr_set,
1442 struct rte_ether_addr *reserved_addr_list,
1443 int *reserved_addr_num,
1444 struct rte_ether_addr *add_addr_list,
1446 struct rte_ether_addr *rm_addr_list,
1449 struct rte_ether_addr *addr;
1450 int current_addr_num;
1451 int reserved_num = 0;
1459 /* Calculate the mc mac address list that should be removed */
1460 current_addr_num = hw->mc_addrs_num;
1461 for (i = 0; i < current_addr_num; i++) {
1462 addr = &hw->mc_addrs[i];
1464 for (j = 0; j < mc_addr_num; j++) {
1465 if (rte_is_same_ether_addr(addr, &mc_addr_set[j])) {
1472 rte_ether_addr_copy(addr, &rm_addr_list[rm_num]);
1475 rte_ether_addr_copy(addr,
1476 &reserved_addr_list[reserved_num]);
1481 /* Calculate the mc mac address list that should be added */
1482 for (i = 0; i < mc_addr_num; i++) {
1483 addr = &mc_addr_set[i];
1485 for (j = 0; j < current_addr_num; j++) {
1486 if (rte_is_same_ether_addr(addr, &hw->mc_addrs[j])) {
1493 rte_ether_addr_copy(addr, &add_addr_list[add_num]);
1498 /* Reorder the mc mac address list maintained by driver */
1499 for (i = 0; i < reserved_num; i++)
1500 rte_ether_addr_copy(&reserved_addr_list[i], &hw->mc_addrs[i]);
1502 for (i = 0; i < rm_num; i++) {
1503 num = reserved_num + i;
1504 rte_ether_addr_copy(&rm_addr_list[i], &hw->mc_addrs[num]);
1507 *reserved_addr_num = reserved_num;
1508 *add_addr_num = add_num;
1509 *rm_addr_num = rm_num;
1513 hns3_set_mc_mac_addr_list(struct rte_eth_dev *dev,
1514 struct rte_ether_addr *mc_addr_set,
1515 uint32_t nb_mc_addr)
1517 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1518 struct rte_ether_addr reserved_addr_list[HNS3_MC_MACADDR_NUM];
1519 struct rte_ether_addr add_addr_list[HNS3_MC_MACADDR_NUM];
1520 struct rte_ether_addr rm_addr_list[HNS3_MC_MACADDR_NUM];
1521 struct rte_ether_addr *addr;
1522 int reserved_addr_num;
1530 /* Check if input parameters are valid */
1531 ret = hns3_set_mc_addr_chk_param(hw, mc_addr_set, nb_mc_addr);
1535 rte_spinlock_lock(&hw->lock);
1538 * Calculate the mc mac address lists those should be removed and be
1539 * added, Reorder the mc mac address list maintained by driver.
1541 mc_addr_num = (int)nb_mc_addr;
1542 hns3_set_mc_addr_calc_addr(hw, mc_addr_set, mc_addr_num,
1543 reserved_addr_list, &reserved_addr_num,
1544 add_addr_list, &add_addr_num,
1545 rm_addr_list, &rm_addr_num);
1547 /* Remove mc mac addresses */
1548 for (i = 0; i < rm_addr_num; i++) {
1549 num = rm_addr_num - i - 1;
1550 addr = &rm_addr_list[num];
1551 ret = hns3_remove_mc_addr(hw, addr);
1553 rte_spinlock_unlock(&hw->lock);
1559 /* Add mc mac addresses */
1560 for (i = 0; i < add_addr_num; i++) {
1561 addr = &add_addr_list[i];
1562 ret = hns3_add_mc_addr(hw, addr);
1564 rte_spinlock_unlock(&hw->lock);
1568 num = reserved_addr_num + i;
1569 rte_ether_addr_copy(addr, &hw->mc_addrs[num]);
1572 rte_spinlock_unlock(&hw->lock);
1578 hns3_configure_all_mc_mac_addr(struct hns3_adapter *hns, bool del)
1580 char mac_str[RTE_ETHER_ADDR_FMT_SIZE];
1581 struct hns3_hw *hw = &hns->hw;
1582 struct rte_ether_addr *addr;
1587 for (i = 0; i < hw->mc_addrs_num; i++) {
1588 addr = &hw->mc_addrs[i];
1589 if (!rte_is_multicast_ether_addr(addr))
1592 ret = hns3_remove_mc_addr(hw, addr);
1594 ret = hns3_add_mc_addr(hw, addr);
1597 rte_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
1599 hns3_dbg(hw, "%s mc mac addr: %s failed",
1600 del ? "Remove" : "Restore", mac_str);
1607 hns3_set_mac_mtu(struct hns3_hw *hw, uint16_t new_mps)
1609 struct hns3_config_max_frm_size_cmd *req;
1610 struct hns3_cmd_desc desc;
1612 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_CONFIG_MAX_FRM_SIZE, false);
1614 req = (struct hns3_config_max_frm_size_cmd *)desc.data;
1615 req->max_frm_size = rte_cpu_to_le_16(new_mps);
1616 req->min_frm_size = HNS3_MIN_FRAME_LEN;
1618 return hns3_cmd_send(hw, &desc, 1);
1622 hns3_config_mtu(struct hns3_hw *hw, uint16_t mps)
1626 ret = hns3_set_mac_mtu(hw, mps);
1628 hns3_err(hw, "Failed to set mtu, ret = %d", ret);
1632 ret = hns3_buffer_alloc(hw);
1634 hns3_err(hw, "Failed to allocate buffer, ret = %d", ret);
1642 hns3_dev_mtu_set(struct rte_eth_dev *dev, uint16_t mtu)
1644 struct hns3_adapter *hns = dev->data->dev_private;
1645 uint32_t frame_size = mtu + HNS3_ETH_OVERHEAD;
1646 struct hns3_hw *hw = &hns->hw;
1647 bool is_jumbo_frame;
1650 if (dev->data->dev_started) {
1651 hns3_err(hw, "Failed to set mtu, port %u must be stopped "
1652 "before configuration", dev->data->port_id);
1656 rte_spinlock_lock(&hw->lock);
1657 is_jumbo_frame = frame_size > RTE_ETHER_MAX_LEN ? true : false;
1658 frame_size = RTE_MAX(frame_size, HNS3_DEFAULT_FRAME_LEN);
1661 * Maximum value of frame_size is HNS3_MAX_FRAME_LEN, so it can safely
1662 * assign to "uint16_t" type variable.
1664 ret = hns3_config_mtu(hw, (uint16_t)frame_size);
1666 rte_spinlock_unlock(&hw->lock);
1667 hns3_err(hw, "Failed to set mtu, port %u mtu %u: %d",
1668 dev->data->port_id, mtu, ret);
1671 hns->pf.mps = (uint16_t)frame_size;
1673 dev->data->dev_conf.rxmode.offloads |=
1674 DEV_RX_OFFLOAD_JUMBO_FRAME;
1676 dev->data->dev_conf.rxmode.offloads &=
1677 ~DEV_RX_OFFLOAD_JUMBO_FRAME;
1678 dev->data->dev_conf.rxmode.max_rx_pkt_len = frame_size;
1679 rte_spinlock_unlock(&hw->lock);
1685 hns3_dev_infos_get(struct rte_eth_dev *eth_dev, struct rte_eth_dev_info *info)
1687 struct hns3_adapter *hns = eth_dev->data->dev_private;
1688 struct hns3_hw *hw = &hns->hw;
1690 info->max_rx_queues = hw->tqps_num;
1691 info->max_tx_queues = hw->tqps_num;
1692 info->max_rx_pktlen = HNS3_MAX_FRAME_LEN; /* CRC included */
1693 info->min_rx_bufsize = hw->rx_buf_len;
1694 info->max_mac_addrs = HNS3_UC_MACADDR_NUM;
1695 info->max_mtu = info->max_rx_pktlen - HNS3_ETH_OVERHEAD;
1696 info->rx_offload_capa = (DEV_RX_OFFLOAD_IPV4_CKSUM |
1697 DEV_RX_OFFLOAD_TCP_CKSUM |
1698 DEV_RX_OFFLOAD_UDP_CKSUM |
1699 DEV_RX_OFFLOAD_SCTP_CKSUM |
1700 DEV_RX_OFFLOAD_OUTER_IPV4_CKSUM |
1701 DEV_RX_OFFLOAD_OUTER_UDP_CKSUM |
1702 DEV_RX_OFFLOAD_KEEP_CRC |
1703 DEV_RX_OFFLOAD_SCATTER |
1704 DEV_RX_OFFLOAD_VLAN_STRIP |
1705 DEV_RX_OFFLOAD_QINQ_STRIP |
1706 DEV_RX_OFFLOAD_VLAN_FILTER |
1707 DEV_RX_OFFLOAD_VLAN_EXTEND |
1708 DEV_RX_OFFLOAD_JUMBO_FRAME);
1709 info->tx_queue_offload_capa = DEV_TX_OFFLOAD_MBUF_FAST_FREE;
1710 info->tx_offload_capa = (DEV_TX_OFFLOAD_OUTER_IPV4_CKSUM |
1711 DEV_TX_OFFLOAD_IPV4_CKSUM |
1712 DEV_TX_OFFLOAD_TCP_CKSUM |
1713 DEV_TX_OFFLOAD_UDP_CKSUM |
1714 DEV_TX_OFFLOAD_SCTP_CKSUM |
1715 DEV_TX_OFFLOAD_VLAN_INSERT |
1716 DEV_TX_OFFLOAD_QINQ_INSERT |
1717 DEV_TX_OFFLOAD_MULTI_SEGS |
1718 info->tx_queue_offload_capa);
1720 info->vmdq_queue_num = 0;
1722 info->reta_size = HNS3_RSS_IND_TBL_SIZE;
1723 info->hash_key_size = HNS3_RSS_KEY_SIZE;
1724 info->flow_type_rss_offloads = HNS3_ETH_RSS_SUPPORT;
1726 info->default_rxportconf.burst_size = HNS3_DEFAULT_PORT_CONF_BURST_SIZE;
1727 info->default_txportconf.burst_size = HNS3_DEFAULT_PORT_CONF_BURST_SIZE;
1728 info->default_rxportconf.nb_queues = HNS3_DEFAULT_PORT_CONF_QUEUES_NUM;
1729 info->default_txportconf.nb_queues = HNS3_DEFAULT_PORT_CONF_QUEUES_NUM;
1735 hns3_fw_version_get(struct rte_eth_dev *eth_dev, char *fw_version,
1738 struct hns3_adapter *hns = eth_dev->data->dev_private;
1739 struct hns3_hw *hw = &hns->hw;
1742 ret = snprintf(fw_version, fw_size, "0x%08x", hw->fw_version);
1743 ret += 1; /* add the size of '\0' */
1744 if (fw_size < (uint32_t)ret)
1751 hns3_dev_link_update(struct rte_eth_dev *eth_dev,
1752 __rte_unused int wait_to_complete)
1754 struct hns3_adapter *hns = eth_dev->data->dev_private;
1755 struct hns3_hw *hw = &hns->hw;
1756 struct hns3_mac *mac = &hw->mac;
1757 struct rte_eth_link new_link;
1759 memset(&new_link, 0, sizeof(new_link));
1760 switch (mac->link_speed) {
1761 case ETH_SPEED_NUM_10M:
1762 case ETH_SPEED_NUM_100M:
1763 case ETH_SPEED_NUM_1G:
1764 case ETH_SPEED_NUM_10G:
1765 case ETH_SPEED_NUM_25G:
1766 case ETH_SPEED_NUM_40G:
1767 case ETH_SPEED_NUM_50G:
1768 case ETH_SPEED_NUM_100G:
1769 new_link.link_speed = mac->link_speed;
1772 new_link.link_speed = ETH_SPEED_NUM_100M;
1776 new_link.link_duplex = mac->link_duplex;
1777 new_link.link_status = mac->link_status ? ETH_LINK_UP : ETH_LINK_DOWN;
1778 new_link.link_autoneg =
1779 !(eth_dev->data->dev_conf.link_speeds & ETH_LINK_SPEED_FIXED);
1781 return rte_eth_linkstatus_set(eth_dev, &new_link);
1785 hns3_parse_func_status(struct hns3_hw *hw, struct hns3_func_status_cmd *status)
1787 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
1788 struct hns3_pf *pf = &hns->pf;
1790 if (!(status->pf_state & HNS3_PF_STATE_DONE))
1793 pf->is_main_pf = (status->pf_state & HNS3_PF_STATE_MAIN) ? true : false;
1799 hns3_query_function_status(struct hns3_hw *hw)
1801 #define HNS3_QUERY_MAX_CNT 10
1802 #define HNS3_QUERY_SLEEP_MSCOEND 1
1803 struct hns3_func_status_cmd *req;
1804 struct hns3_cmd_desc desc;
1808 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_QUERY_FUNC_STATUS, true);
1809 req = (struct hns3_func_status_cmd *)desc.data;
1812 ret = hns3_cmd_send(hw, &desc, 1);
1814 PMD_INIT_LOG(ERR, "query function status failed %d",
1819 /* Check pf reset is done */
1823 rte_delay_ms(HNS3_QUERY_SLEEP_MSCOEND);
1824 } while (timeout++ < HNS3_QUERY_MAX_CNT);
1826 return hns3_parse_func_status(hw, req);
1830 hns3_query_pf_resource(struct hns3_hw *hw)
1832 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
1833 struct hns3_pf *pf = &hns->pf;
1834 struct hns3_pf_res_cmd *req;
1835 struct hns3_cmd_desc desc;
1838 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_QUERY_PF_RSRC, true);
1839 ret = hns3_cmd_send(hw, &desc, 1);
1841 PMD_INIT_LOG(ERR, "query pf resource failed %d", ret);
1845 req = (struct hns3_pf_res_cmd *)desc.data;
1846 hw->total_tqps_num = rte_le_to_cpu_16(req->tqp_num);
1847 pf->pkt_buf_size = rte_le_to_cpu_16(req->buf_size) << HNS3_BUF_UNIT_S;
1848 hw->tqps_num = RTE_MIN(hw->total_tqps_num, HNS3_MAX_TQP_NUM_PER_FUNC);
1850 if (req->tx_buf_size)
1852 rte_le_to_cpu_16(req->tx_buf_size) << HNS3_BUF_UNIT_S;
1854 pf->tx_buf_size = HNS3_DEFAULT_TX_BUF;
1856 pf->tx_buf_size = roundup(pf->tx_buf_size, HNS3_BUF_SIZE_UNIT);
1858 if (req->dv_buf_size)
1860 rte_le_to_cpu_16(req->dv_buf_size) << HNS3_BUF_UNIT_S;
1862 pf->dv_buf_size = HNS3_DEFAULT_DV;
1864 pf->dv_buf_size = roundup(pf->dv_buf_size, HNS3_BUF_SIZE_UNIT);
1867 hns3_get_field(rte_le_to_cpu_16(req->pf_intr_vector_number),
1868 HNS3_PF_VEC_NUM_M, HNS3_PF_VEC_NUM_S);
1874 hns3_parse_cfg(struct hns3_cfg *cfg, struct hns3_cmd_desc *desc)
1876 struct hns3_cfg_param_cmd *req;
1877 uint64_t mac_addr_tmp_high;
1878 uint64_t mac_addr_tmp;
1881 req = (struct hns3_cfg_param_cmd *)desc[0].data;
1883 /* get the configuration */
1884 cfg->vmdq_vport_num = hns3_get_field(rte_le_to_cpu_32(req->param[0]),
1885 HNS3_CFG_VMDQ_M, HNS3_CFG_VMDQ_S);
1886 cfg->tc_num = hns3_get_field(rte_le_to_cpu_32(req->param[0]),
1887 HNS3_CFG_TC_NUM_M, HNS3_CFG_TC_NUM_S);
1888 cfg->tqp_desc_num = hns3_get_field(rte_le_to_cpu_32(req->param[0]),
1889 HNS3_CFG_TQP_DESC_N_M,
1890 HNS3_CFG_TQP_DESC_N_S);
1892 cfg->phy_addr = hns3_get_field(rte_le_to_cpu_32(req->param[1]),
1893 HNS3_CFG_PHY_ADDR_M,
1894 HNS3_CFG_PHY_ADDR_S);
1895 cfg->media_type = hns3_get_field(rte_le_to_cpu_32(req->param[1]),
1896 HNS3_CFG_MEDIA_TP_M,
1897 HNS3_CFG_MEDIA_TP_S);
1898 cfg->rx_buf_len = hns3_get_field(rte_le_to_cpu_32(req->param[1]),
1899 HNS3_CFG_RX_BUF_LEN_M,
1900 HNS3_CFG_RX_BUF_LEN_S);
1901 /* get mac address */
1902 mac_addr_tmp = rte_le_to_cpu_32(req->param[2]);
1903 mac_addr_tmp_high = hns3_get_field(rte_le_to_cpu_32(req->param[3]),
1904 HNS3_CFG_MAC_ADDR_H_M,
1905 HNS3_CFG_MAC_ADDR_H_S);
1907 mac_addr_tmp |= (mac_addr_tmp_high << 31) << 1;
1909 cfg->default_speed = hns3_get_field(rte_le_to_cpu_32(req->param[3]),
1910 HNS3_CFG_DEFAULT_SPEED_M,
1911 HNS3_CFG_DEFAULT_SPEED_S);
1912 cfg->rss_size_max = hns3_get_field(rte_le_to_cpu_32(req->param[3]),
1913 HNS3_CFG_RSS_SIZE_M,
1914 HNS3_CFG_RSS_SIZE_S);
1916 for (i = 0; i < RTE_ETHER_ADDR_LEN; i++)
1917 cfg->mac_addr[i] = (mac_addr_tmp >> (8 * i)) & 0xff;
1919 req = (struct hns3_cfg_param_cmd *)desc[1].data;
1920 cfg->numa_node_map = rte_le_to_cpu_32(req->param[0]);
1922 cfg->speed_ability = hns3_get_field(rte_le_to_cpu_32(req->param[1]),
1923 HNS3_CFG_SPEED_ABILITY_M,
1924 HNS3_CFG_SPEED_ABILITY_S);
1925 cfg->umv_space = hns3_get_field(rte_le_to_cpu_32(req->param[1]),
1926 HNS3_CFG_UMV_TBL_SPACE_M,
1927 HNS3_CFG_UMV_TBL_SPACE_S);
1928 if (!cfg->umv_space)
1929 cfg->umv_space = HNS3_DEFAULT_UMV_SPACE_PER_PF;
1932 /* hns3_get_board_cfg: query the static parameter from NCL_config file in flash
1933 * @hw: pointer to struct hns3_hw
1934 * @hcfg: the config structure to be getted
1937 hns3_get_board_cfg(struct hns3_hw *hw, struct hns3_cfg *hcfg)
1939 struct hns3_cmd_desc desc[HNS3_PF_CFG_DESC_NUM];
1940 struct hns3_cfg_param_cmd *req;
1945 for (i = 0; i < HNS3_PF_CFG_DESC_NUM; i++) {
1947 req = (struct hns3_cfg_param_cmd *)desc[i].data;
1948 hns3_cmd_setup_basic_desc(&desc[i], HNS3_OPC_GET_CFG_PARAM,
1950 hns3_set_field(offset, HNS3_CFG_OFFSET_M, HNS3_CFG_OFFSET_S,
1951 i * HNS3_CFG_RD_LEN_BYTES);
1952 /* Len should be divided by 4 when send to hardware */
1953 hns3_set_field(offset, HNS3_CFG_RD_LEN_M, HNS3_CFG_RD_LEN_S,
1954 HNS3_CFG_RD_LEN_BYTES / HNS3_CFG_RD_LEN_UNIT);
1955 req->offset = rte_cpu_to_le_32(offset);
1958 ret = hns3_cmd_send(hw, desc, HNS3_PF_CFG_DESC_NUM);
1960 PMD_INIT_LOG(ERR, "get config failed %d.", ret);
1964 hns3_parse_cfg(hcfg, desc);
1970 hns3_parse_speed(int speed_cmd, uint32_t *speed)
1972 switch (speed_cmd) {
1973 case HNS3_CFG_SPEED_10M:
1974 *speed = ETH_SPEED_NUM_10M;
1976 case HNS3_CFG_SPEED_100M:
1977 *speed = ETH_SPEED_NUM_100M;
1979 case HNS3_CFG_SPEED_1G:
1980 *speed = ETH_SPEED_NUM_1G;
1982 case HNS3_CFG_SPEED_10G:
1983 *speed = ETH_SPEED_NUM_10G;
1985 case HNS3_CFG_SPEED_25G:
1986 *speed = ETH_SPEED_NUM_25G;
1988 case HNS3_CFG_SPEED_40G:
1989 *speed = ETH_SPEED_NUM_40G;
1991 case HNS3_CFG_SPEED_50G:
1992 *speed = ETH_SPEED_NUM_50G;
1994 case HNS3_CFG_SPEED_100G:
1995 *speed = ETH_SPEED_NUM_100G;
2005 hns3_get_board_configuration(struct hns3_hw *hw)
2007 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
2008 struct hns3_pf *pf = &hns->pf;
2009 struct hns3_cfg cfg;
2012 ret = hns3_get_board_cfg(hw, &cfg);
2014 PMD_INIT_LOG(ERR, "get board config failed %d", ret);
2018 if (cfg.media_type == HNS3_MEDIA_TYPE_COPPER) {
2019 PMD_INIT_LOG(ERR, "media type is copper, not supported.");
2023 hw->mac.media_type = cfg.media_type;
2024 hw->rss_size_max = cfg.rss_size_max;
2025 hw->rx_buf_len = cfg.rx_buf_len;
2026 memcpy(hw->mac.mac_addr, cfg.mac_addr, RTE_ETHER_ADDR_LEN);
2027 hw->mac.phy_addr = cfg.phy_addr;
2028 hw->mac.default_addr_setted = false;
2029 hw->num_tx_desc = cfg.tqp_desc_num;
2030 hw->num_rx_desc = cfg.tqp_desc_num;
2031 hw->dcb_info.num_pg = 1;
2032 hw->dcb_info.hw_pfc_map = 0;
2034 ret = hns3_parse_speed(cfg.default_speed, &hw->mac.link_speed);
2036 PMD_INIT_LOG(ERR, "Get wrong speed %d, ret = %d",
2037 cfg.default_speed, ret);
2041 pf->tc_max = cfg.tc_num;
2042 if (pf->tc_max > HNS3_MAX_TC_NUM || pf->tc_max < 1) {
2043 PMD_INIT_LOG(WARNING,
2044 "Get TC num(%u) from flash, set TC num to 1",
2049 /* Dev does not support DCB */
2050 if (!hns3_dev_dcb_supported(hw)) {
2054 pf->pfc_max = pf->tc_max;
2056 hw->dcb_info.num_tc = 1;
2057 hw->alloc_rss_size = RTE_MIN(hw->rss_size_max,
2058 hw->tqps_num / hw->dcb_info.num_tc);
2059 hns3_set_bit(hw->hw_tc_map, 0, 1);
2060 pf->tx_sch_mode = HNS3_FLAG_TC_BASE_SCH_MODE;
2062 pf->wanted_umv_size = cfg.umv_space;
2068 hns3_get_configuration(struct hns3_hw *hw)
2072 ret = hns3_query_function_status(hw);
2074 PMD_INIT_LOG(ERR, "Failed to query function status: %d.", ret);
2078 /* Get pf resource */
2079 ret = hns3_query_pf_resource(hw);
2081 PMD_INIT_LOG(ERR, "Failed to query pf resource: %d", ret);
2085 ret = hns3_get_board_configuration(hw);
2087 PMD_INIT_LOG(ERR, "Failed to get board configuration: %d", ret);
2095 hns3_map_tqps_to_func(struct hns3_hw *hw, uint16_t func_id, uint16_t tqp_pid,
2096 uint16_t tqp_vid, bool is_pf)
2098 struct hns3_tqp_map_cmd *req;
2099 struct hns3_cmd_desc desc;
2102 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_SET_TQP_MAP, false);
2104 req = (struct hns3_tqp_map_cmd *)desc.data;
2105 req->tqp_id = rte_cpu_to_le_16(tqp_pid);
2106 req->tqp_vf = func_id;
2107 req->tqp_flag = 1 << HNS3_TQP_MAP_EN_B;
2109 req->tqp_flag |= (1 << HNS3_TQP_MAP_TYPE_B);
2110 req->tqp_vid = rte_cpu_to_le_16(tqp_vid);
2112 ret = hns3_cmd_send(hw, &desc, 1);
2114 PMD_INIT_LOG(ERR, "TQP map failed %d", ret);
2120 hns3_map_tqp(struct hns3_hw *hw)
2122 uint16_t tqps_num = hw->total_tqps_num;
2130 * In current version VF is not supported when PF is driven by DPDK
2131 * driver, so we allocate tqps to PF as much as possible.
2134 num = DIV_ROUND_UP(hw->total_tqps_num, HNS3_MAX_TQP_NUM_PER_FUNC);
2135 for (func_id = 0; func_id < num; func_id++) {
2137 i < HNS3_MAX_TQP_NUM_PER_FUNC && tqp_id < tqps_num; i++) {
2138 ret = hns3_map_tqps_to_func(hw, func_id, tqp_id++, i,
2149 hns3_cfg_mac_speed_dup_hw(struct hns3_hw *hw, uint32_t speed, uint8_t duplex)
2151 struct hns3_config_mac_speed_dup_cmd *req;
2152 struct hns3_cmd_desc desc;
2155 req = (struct hns3_config_mac_speed_dup_cmd *)desc.data;
2157 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_CONFIG_SPEED_DUP, false);
2159 hns3_set_bit(req->speed_dup, HNS3_CFG_DUPLEX_B, !!duplex ? 1 : 0);
2162 case ETH_SPEED_NUM_10M:
2163 hns3_set_field(req->speed_dup, HNS3_CFG_SPEED_M,
2164 HNS3_CFG_SPEED_S, HNS3_CFG_SPEED_10M);
2166 case ETH_SPEED_NUM_100M:
2167 hns3_set_field(req->speed_dup, HNS3_CFG_SPEED_M,
2168 HNS3_CFG_SPEED_S, HNS3_CFG_SPEED_100M);
2170 case ETH_SPEED_NUM_1G:
2171 hns3_set_field(req->speed_dup, HNS3_CFG_SPEED_M,
2172 HNS3_CFG_SPEED_S, HNS3_CFG_SPEED_1G);
2174 case ETH_SPEED_NUM_10G:
2175 hns3_set_field(req->speed_dup, HNS3_CFG_SPEED_M,
2176 HNS3_CFG_SPEED_S, HNS3_CFG_SPEED_10G);
2178 case ETH_SPEED_NUM_25G:
2179 hns3_set_field(req->speed_dup, HNS3_CFG_SPEED_M,
2180 HNS3_CFG_SPEED_S, HNS3_CFG_SPEED_25G);
2182 case ETH_SPEED_NUM_40G:
2183 hns3_set_field(req->speed_dup, HNS3_CFG_SPEED_M,
2184 HNS3_CFG_SPEED_S, HNS3_CFG_SPEED_40G);
2186 case ETH_SPEED_NUM_50G:
2187 hns3_set_field(req->speed_dup, HNS3_CFG_SPEED_M,
2188 HNS3_CFG_SPEED_S, HNS3_CFG_SPEED_50G);
2190 case ETH_SPEED_NUM_100G:
2191 hns3_set_field(req->speed_dup, HNS3_CFG_SPEED_M,
2192 HNS3_CFG_SPEED_S, HNS3_CFG_SPEED_100G);
2195 PMD_INIT_LOG(ERR, "invalid speed (%u)", speed);
2199 hns3_set_bit(req->mac_change_fec_en, HNS3_CFG_MAC_SPEED_CHANGE_EN_B, 1);
2201 ret = hns3_cmd_send(hw, &desc, 1);
2203 PMD_INIT_LOG(ERR, "mac speed/duplex config cmd failed %d", ret);
2209 hns3_tx_buffer_calc(struct hns3_hw *hw, struct hns3_pkt_buf_alloc *buf_alloc)
2211 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
2212 struct hns3_pf *pf = &hns->pf;
2213 struct hns3_priv_buf *priv;
2214 uint32_t i, total_size;
2216 total_size = pf->pkt_buf_size;
2218 /* alloc tx buffer for all enabled tc */
2219 for (i = 0; i < HNS3_MAX_TC_NUM; i++) {
2220 priv = &buf_alloc->priv_buf[i];
2222 if (hw->hw_tc_map & BIT(i)) {
2223 if (total_size < pf->tx_buf_size)
2226 priv->tx_buf_size = pf->tx_buf_size;
2228 priv->tx_buf_size = 0;
2230 total_size -= priv->tx_buf_size;
2237 hns3_tx_buffer_alloc(struct hns3_hw *hw, struct hns3_pkt_buf_alloc *buf_alloc)
2239 /* TX buffer size is unit by 128 byte */
2240 #define HNS3_BUF_SIZE_UNIT_SHIFT 7
2241 #define HNS3_BUF_SIZE_UPDATE_EN_MSK BIT(15)
2242 struct hns3_tx_buff_alloc_cmd *req;
2243 struct hns3_cmd_desc desc;
2248 req = (struct hns3_tx_buff_alloc_cmd *)desc.data;
2250 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_TX_BUFF_ALLOC, 0);
2251 for (i = 0; i < HNS3_MAX_TC_NUM; i++) {
2252 buf_size = buf_alloc->priv_buf[i].tx_buf_size;
2254 buf_size = buf_size >> HNS3_BUF_SIZE_UNIT_SHIFT;
2255 req->tx_pkt_buff[i] = rte_cpu_to_le_16(buf_size |
2256 HNS3_BUF_SIZE_UPDATE_EN_MSK);
2259 ret = hns3_cmd_send(hw, &desc, 1);
2261 PMD_INIT_LOG(ERR, "tx buffer alloc cmd failed %d", ret);
2267 hns3_get_tc_num(struct hns3_hw *hw)
2272 for (i = 0; i < HNS3_MAX_TC_NUM; i++)
2273 if (hw->hw_tc_map & BIT(i))
2279 hns3_get_rx_priv_buff_alloced(struct hns3_pkt_buf_alloc *buf_alloc)
2281 struct hns3_priv_buf *priv;
2282 uint32_t rx_priv = 0;
2285 for (i = 0; i < HNS3_MAX_TC_NUM; i++) {
2286 priv = &buf_alloc->priv_buf[i];
2288 rx_priv += priv->buf_size;
2294 hns3_get_tx_buff_alloced(struct hns3_pkt_buf_alloc *buf_alloc)
2296 uint32_t total_tx_size = 0;
2299 for (i = 0; i < HNS3_MAX_TC_NUM; i++)
2300 total_tx_size += buf_alloc->priv_buf[i].tx_buf_size;
2302 return total_tx_size;
2305 /* Get the number of pfc enabled TCs, which have private buffer */
2307 hns3_get_pfc_priv_num(struct hns3_hw *hw, struct hns3_pkt_buf_alloc *buf_alloc)
2309 struct hns3_priv_buf *priv;
2313 for (i = 0; i < HNS3_MAX_TC_NUM; i++) {
2314 priv = &buf_alloc->priv_buf[i];
2315 if ((hw->dcb_info.hw_pfc_map & BIT(i)) && priv->enable)
2322 /* Get the number of pfc disabled TCs, which have private buffer */
2324 hns3_get_no_pfc_priv_num(struct hns3_hw *hw,
2325 struct hns3_pkt_buf_alloc *buf_alloc)
2327 struct hns3_priv_buf *priv;
2331 for (i = 0; i < HNS3_MAX_TC_NUM; i++) {
2332 priv = &buf_alloc->priv_buf[i];
2333 if (hw->hw_tc_map & BIT(i) &&
2334 !(hw->dcb_info.hw_pfc_map & BIT(i)) && priv->enable)
2342 hns3_is_rx_buf_ok(struct hns3_hw *hw, struct hns3_pkt_buf_alloc *buf_alloc,
2345 uint32_t shared_buf_min, shared_buf_tc, shared_std, hi_thrd, lo_thrd;
2346 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
2347 struct hns3_pf *pf = &hns->pf;
2348 uint32_t shared_buf, aligned_mps;
2353 tc_num = hns3_get_tc_num(hw);
2354 aligned_mps = roundup(pf->mps, HNS3_BUF_SIZE_UNIT);
2356 if (hns3_dev_dcb_supported(hw))
2357 shared_buf_min = HNS3_BUF_MUL_BY * aligned_mps +
2360 shared_buf_min = aligned_mps + HNS3_NON_DCB_ADDITIONAL_BUF
2363 shared_buf_tc = tc_num * aligned_mps + aligned_mps;
2364 shared_std = roundup(max_t(uint32_t, shared_buf_min, shared_buf_tc),
2365 HNS3_BUF_SIZE_UNIT);
2367 rx_priv = hns3_get_rx_priv_buff_alloced(buf_alloc);
2368 if (rx_all < rx_priv + shared_std)
2371 shared_buf = rounddown(rx_all - rx_priv, HNS3_BUF_SIZE_UNIT);
2372 buf_alloc->s_buf.buf_size = shared_buf;
2373 if (hns3_dev_dcb_supported(hw)) {
2374 buf_alloc->s_buf.self.high = shared_buf - pf->dv_buf_size;
2375 buf_alloc->s_buf.self.low = buf_alloc->s_buf.self.high
2376 - roundup(aligned_mps / HNS3_BUF_DIV_BY,
2377 HNS3_BUF_SIZE_UNIT);
2379 buf_alloc->s_buf.self.high =
2380 aligned_mps + HNS3_NON_DCB_ADDITIONAL_BUF;
2381 buf_alloc->s_buf.self.low = aligned_mps;
2384 if (hns3_dev_dcb_supported(hw)) {
2385 hi_thrd = shared_buf - pf->dv_buf_size;
2387 if (tc_num <= NEED_RESERVE_TC_NUM)
2388 hi_thrd = hi_thrd * BUF_RESERVE_PERCENT
2392 hi_thrd = hi_thrd / tc_num;
2394 hi_thrd = max_t(uint32_t, hi_thrd,
2395 HNS3_BUF_MUL_BY * aligned_mps);
2396 hi_thrd = rounddown(hi_thrd, HNS3_BUF_SIZE_UNIT);
2397 lo_thrd = hi_thrd - aligned_mps / HNS3_BUF_DIV_BY;
2399 hi_thrd = aligned_mps + HNS3_NON_DCB_ADDITIONAL_BUF;
2400 lo_thrd = aligned_mps;
2403 for (i = 0; i < HNS3_MAX_TC_NUM; i++) {
2404 buf_alloc->s_buf.tc_thrd[i].low = lo_thrd;
2405 buf_alloc->s_buf.tc_thrd[i].high = hi_thrd;
2412 hns3_rx_buf_calc_all(struct hns3_hw *hw, bool max,
2413 struct hns3_pkt_buf_alloc *buf_alloc)
2415 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
2416 struct hns3_pf *pf = &hns->pf;
2417 struct hns3_priv_buf *priv;
2418 uint32_t aligned_mps;
2422 rx_all = pf->pkt_buf_size - hns3_get_tx_buff_alloced(buf_alloc);
2423 aligned_mps = roundup(pf->mps, HNS3_BUF_SIZE_UNIT);
2425 for (i = 0; i < HNS3_MAX_TC_NUM; i++) {
2426 priv = &buf_alloc->priv_buf[i];
2433 if (!(hw->hw_tc_map & BIT(i)))
2437 if (hw->dcb_info.hw_pfc_map & BIT(i)) {
2438 priv->wl.low = max ? aligned_mps : HNS3_BUF_SIZE_UNIT;
2439 priv->wl.high = roundup(priv->wl.low + aligned_mps,
2440 HNS3_BUF_SIZE_UNIT);
2443 priv->wl.high = max ? (aligned_mps * HNS3_BUF_MUL_BY) :
2447 priv->buf_size = priv->wl.high + pf->dv_buf_size;
2450 return hns3_is_rx_buf_ok(hw, buf_alloc, rx_all);
2454 hns3_drop_nopfc_buf_till_fit(struct hns3_hw *hw,
2455 struct hns3_pkt_buf_alloc *buf_alloc)
2457 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
2458 struct hns3_pf *pf = &hns->pf;
2459 struct hns3_priv_buf *priv;
2460 int no_pfc_priv_num;
2465 rx_all = pf->pkt_buf_size - hns3_get_tx_buff_alloced(buf_alloc);
2466 no_pfc_priv_num = hns3_get_no_pfc_priv_num(hw, buf_alloc);
2468 /* let the last to be cleared first */
2469 for (i = HNS3_MAX_TC_NUM - 1; i >= 0; i--) {
2470 priv = &buf_alloc->priv_buf[i];
2471 mask = BIT((uint8_t)i);
2473 if (hw->hw_tc_map & mask &&
2474 !(hw->dcb_info.hw_pfc_map & mask)) {
2475 /* Clear the no pfc TC private buffer */
2483 if (hns3_is_rx_buf_ok(hw, buf_alloc, rx_all) ||
2484 no_pfc_priv_num == 0)
2488 return hns3_is_rx_buf_ok(hw, buf_alloc, rx_all);
2492 hns3_drop_pfc_buf_till_fit(struct hns3_hw *hw,
2493 struct hns3_pkt_buf_alloc *buf_alloc)
2495 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
2496 struct hns3_pf *pf = &hns->pf;
2497 struct hns3_priv_buf *priv;
2503 rx_all = pf->pkt_buf_size - hns3_get_tx_buff_alloced(buf_alloc);
2504 pfc_priv_num = hns3_get_pfc_priv_num(hw, buf_alloc);
2506 /* let the last to be cleared first */
2507 for (i = HNS3_MAX_TC_NUM - 1; i >= 0; i--) {
2508 priv = &buf_alloc->priv_buf[i];
2509 mask = BIT((uint8_t)i);
2511 if (hw->hw_tc_map & mask &&
2512 hw->dcb_info.hw_pfc_map & mask) {
2513 /* Reduce the number of pfc TC with private buffer */
2520 if (hns3_is_rx_buf_ok(hw, buf_alloc, rx_all) ||
2525 return hns3_is_rx_buf_ok(hw, buf_alloc, rx_all);
2529 hns3_only_alloc_priv_buff(struct hns3_hw *hw,
2530 struct hns3_pkt_buf_alloc *buf_alloc)
2532 #define COMPENSATE_BUFFER 0x3C00
2533 #define COMPENSATE_HALF_MPS_NUM 5
2534 #define PRIV_WL_GAP 0x1800
2535 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
2536 struct hns3_pf *pf = &hns->pf;
2537 uint32_t tc_num = hns3_get_tc_num(hw);
2538 uint32_t half_mps = pf->mps >> 1;
2539 struct hns3_priv_buf *priv;
2540 uint32_t min_rx_priv;
2544 rx_priv = pf->pkt_buf_size - hns3_get_tx_buff_alloced(buf_alloc);
2546 rx_priv = rx_priv / tc_num;
2548 if (tc_num <= NEED_RESERVE_TC_NUM)
2549 rx_priv = rx_priv * BUF_RESERVE_PERCENT / BUF_MAX_PERCENT;
2552 * Minimum value of private buffer in rx direction (min_rx_priv) is
2553 * equal to "DV + 2.5 * MPS + 15KB". Driver only allocates rx private
2554 * buffer if rx_priv is greater than min_rx_priv.
2556 min_rx_priv = pf->dv_buf_size + COMPENSATE_BUFFER +
2557 COMPENSATE_HALF_MPS_NUM * half_mps;
2558 min_rx_priv = roundup(min_rx_priv, HNS3_BUF_SIZE_UNIT);
2559 rx_priv = rounddown(rx_priv, HNS3_BUF_SIZE_UNIT);
2561 if (rx_priv < min_rx_priv)
2564 for (i = 0; i < HNS3_MAX_TC_NUM; i++) {
2565 priv = &buf_alloc->priv_buf[i];
2572 if (!(hw->hw_tc_map & BIT(i)))
2576 priv->buf_size = rx_priv;
2577 priv->wl.high = rx_priv - pf->dv_buf_size;
2578 priv->wl.low = priv->wl.high - PRIV_WL_GAP;
2581 buf_alloc->s_buf.buf_size = 0;
2587 * hns3_rx_buffer_calc: calculate the rx private buffer size for all TCs
2588 * @hw: pointer to struct hns3_hw
2589 * @buf_alloc: pointer to buffer calculation data
2590 * @return: 0: calculate sucessful, negative: fail
2593 hns3_rx_buffer_calc(struct hns3_hw *hw, struct hns3_pkt_buf_alloc *buf_alloc)
2595 /* When DCB is not supported, rx private buffer is not allocated. */
2596 if (!hns3_dev_dcb_supported(hw)) {
2597 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
2598 struct hns3_pf *pf = &hns->pf;
2599 uint32_t rx_all = pf->pkt_buf_size;
2601 rx_all -= hns3_get_tx_buff_alloced(buf_alloc);
2602 if (!hns3_is_rx_buf_ok(hw, buf_alloc, rx_all))
2609 * Try to allocate privated packet buffer for all TCs without share
2612 if (hns3_only_alloc_priv_buff(hw, buf_alloc))
2616 * Try to allocate privated packet buffer for all TCs with share
2619 if (hns3_rx_buf_calc_all(hw, true, buf_alloc))
2623 * For different application scenes, the enabled port number, TC number
2624 * and no_drop TC number are different. In order to obtain the better
2625 * performance, software could allocate the buffer size and configure
2626 * the waterline by tring to decrease the private buffer size according
2627 * to the order, namely, waterline of valided tc, pfc disabled tc, pfc
2630 if (hns3_rx_buf_calc_all(hw, false, buf_alloc))
2633 if (hns3_drop_nopfc_buf_till_fit(hw, buf_alloc))
2636 if (hns3_drop_pfc_buf_till_fit(hw, buf_alloc))
2643 hns3_rx_priv_buf_alloc(struct hns3_hw *hw, struct hns3_pkt_buf_alloc *buf_alloc)
2645 struct hns3_rx_priv_buff_cmd *req;
2646 struct hns3_cmd_desc desc;
2651 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_RX_PRIV_BUFF_ALLOC, false);
2652 req = (struct hns3_rx_priv_buff_cmd *)desc.data;
2654 /* Alloc private buffer TCs */
2655 for (i = 0; i < HNS3_MAX_TC_NUM; i++) {
2656 struct hns3_priv_buf *priv = &buf_alloc->priv_buf[i];
2659 rte_cpu_to_le_16(priv->buf_size >> HNS3_BUF_UNIT_S);
2660 req->buf_num[i] |= rte_cpu_to_le_16(1 << HNS3_TC0_PRI_BUF_EN_B);
2663 buf_size = buf_alloc->s_buf.buf_size;
2664 req->shared_buf = rte_cpu_to_le_16((buf_size >> HNS3_BUF_UNIT_S) |
2665 (1 << HNS3_TC0_PRI_BUF_EN_B));
2667 ret = hns3_cmd_send(hw, &desc, 1);
2669 PMD_INIT_LOG(ERR, "rx private buffer alloc cmd failed %d", ret);
2675 hns3_rx_priv_wl_config(struct hns3_hw *hw, struct hns3_pkt_buf_alloc *buf_alloc)
2677 #define HNS3_RX_PRIV_WL_ALLOC_DESC_NUM 2
2678 struct hns3_rx_priv_wl_buf *req;
2679 struct hns3_priv_buf *priv;
2680 struct hns3_cmd_desc desc[HNS3_RX_PRIV_WL_ALLOC_DESC_NUM];
2684 for (i = 0; i < HNS3_RX_PRIV_WL_ALLOC_DESC_NUM; i++) {
2685 hns3_cmd_setup_basic_desc(&desc[i], HNS3_OPC_RX_PRIV_WL_ALLOC,
2687 req = (struct hns3_rx_priv_wl_buf *)desc[i].data;
2689 /* The first descriptor set the NEXT bit to 1 */
2691 desc[i].flag |= rte_cpu_to_le_16(HNS3_CMD_FLAG_NEXT);
2693 desc[i].flag &= ~rte_cpu_to_le_16(HNS3_CMD_FLAG_NEXT);
2695 for (j = 0; j < HNS3_TC_NUM_ONE_DESC; j++) {
2696 uint32_t idx = i * HNS3_TC_NUM_ONE_DESC + j;
2698 priv = &buf_alloc->priv_buf[idx];
2699 req->tc_wl[j].high = rte_cpu_to_le_16(priv->wl.high >>
2701 req->tc_wl[j].high |=
2702 rte_cpu_to_le_16(BIT(HNS3_RX_PRIV_EN_B));
2703 req->tc_wl[j].low = rte_cpu_to_le_16(priv->wl.low >>
2705 req->tc_wl[j].low |=
2706 rte_cpu_to_le_16(BIT(HNS3_RX_PRIV_EN_B));
2710 /* Send 2 descriptor at one time */
2711 ret = hns3_cmd_send(hw, desc, HNS3_RX_PRIV_WL_ALLOC_DESC_NUM);
2713 PMD_INIT_LOG(ERR, "rx private waterline config cmd failed %d",
2719 hns3_common_thrd_config(struct hns3_hw *hw,
2720 struct hns3_pkt_buf_alloc *buf_alloc)
2722 #define HNS3_RX_COM_THRD_ALLOC_DESC_NUM 2
2723 struct hns3_shared_buf *s_buf = &buf_alloc->s_buf;
2724 struct hns3_rx_com_thrd *req;
2725 struct hns3_cmd_desc desc[HNS3_RX_COM_THRD_ALLOC_DESC_NUM];
2726 struct hns3_tc_thrd *tc;
2731 for (i = 0; i < HNS3_RX_COM_THRD_ALLOC_DESC_NUM; i++) {
2732 hns3_cmd_setup_basic_desc(&desc[i], HNS3_OPC_RX_COM_THRD_ALLOC,
2734 req = (struct hns3_rx_com_thrd *)&desc[i].data;
2736 /* The first descriptor set the NEXT bit to 1 */
2738 desc[i].flag |= rte_cpu_to_le_16(HNS3_CMD_FLAG_NEXT);
2740 desc[i].flag &= ~rte_cpu_to_le_16(HNS3_CMD_FLAG_NEXT);
2742 for (j = 0; j < HNS3_TC_NUM_ONE_DESC; j++) {
2743 tc_idx = i * HNS3_TC_NUM_ONE_DESC + j;
2744 tc = &s_buf->tc_thrd[tc_idx];
2746 req->com_thrd[j].high =
2747 rte_cpu_to_le_16(tc->high >> HNS3_BUF_UNIT_S);
2748 req->com_thrd[j].high |=
2749 rte_cpu_to_le_16(BIT(HNS3_RX_PRIV_EN_B));
2750 req->com_thrd[j].low =
2751 rte_cpu_to_le_16(tc->low >> HNS3_BUF_UNIT_S);
2752 req->com_thrd[j].low |=
2753 rte_cpu_to_le_16(BIT(HNS3_RX_PRIV_EN_B));
2757 /* Send 2 descriptors at one time */
2758 ret = hns3_cmd_send(hw, desc, HNS3_RX_COM_THRD_ALLOC_DESC_NUM);
2760 PMD_INIT_LOG(ERR, "common threshold config cmd failed %d", ret);
2766 hns3_common_wl_config(struct hns3_hw *hw, struct hns3_pkt_buf_alloc *buf_alloc)
2768 struct hns3_shared_buf *buf = &buf_alloc->s_buf;
2769 struct hns3_rx_com_wl *req;
2770 struct hns3_cmd_desc desc;
2773 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_RX_COM_WL_ALLOC, false);
2775 req = (struct hns3_rx_com_wl *)desc.data;
2776 req->com_wl.high = rte_cpu_to_le_16(buf->self.high >> HNS3_BUF_UNIT_S);
2777 req->com_wl.high |= rte_cpu_to_le_16(BIT(HNS3_RX_PRIV_EN_B));
2779 req->com_wl.low = rte_cpu_to_le_16(buf->self.low >> HNS3_BUF_UNIT_S);
2780 req->com_wl.low |= rte_cpu_to_le_16(BIT(HNS3_RX_PRIV_EN_B));
2782 ret = hns3_cmd_send(hw, &desc, 1);
2784 PMD_INIT_LOG(ERR, "common waterline config cmd failed %d", ret);
2790 hns3_buffer_alloc(struct hns3_hw *hw)
2792 struct hns3_pkt_buf_alloc pkt_buf;
2795 memset(&pkt_buf, 0, sizeof(pkt_buf));
2796 ret = hns3_tx_buffer_calc(hw, &pkt_buf);
2799 "could not calc tx buffer size for all TCs %d",
2804 ret = hns3_tx_buffer_alloc(hw, &pkt_buf);
2806 PMD_INIT_LOG(ERR, "could not alloc tx buffers %d", ret);
2810 ret = hns3_rx_buffer_calc(hw, &pkt_buf);
2813 "could not calc rx priv buffer size for all TCs %d",
2818 ret = hns3_rx_priv_buf_alloc(hw, &pkt_buf);
2820 PMD_INIT_LOG(ERR, "could not alloc rx priv buffer %d", ret);
2824 if (hns3_dev_dcb_supported(hw)) {
2825 ret = hns3_rx_priv_wl_config(hw, &pkt_buf);
2828 "could not configure rx private waterline %d",
2833 ret = hns3_common_thrd_config(hw, &pkt_buf);
2836 "could not configure common threshold %d",
2842 ret = hns3_common_wl_config(hw, &pkt_buf);
2844 PMD_INIT_LOG(ERR, "could not configure common waterline %d",
2851 hns3_mac_init(struct hns3_hw *hw)
2853 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
2854 struct hns3_mac *mac = &hw->mac;
2855 struct hns3_pf *pf = &hns->pf;
2858 pf->support_sfp_query = true;
2859 mac->link_duplex = ETH_LINK_FULL_DUPLEX;
2860 ret = hns3_cfg_mac_speed_dup_hw(hw, mac->link_speed, mac->link_duplex);
2862 PMD_INIT_LOG(ERR, "Config mac speed dup fail ret = %d", ret);
2866 mac->link_status = ETH_LINK_DOWN;
2868 return hns3_config_mtu(hw, pf->mps);
2872 hns3_get_mac_ethertype_cmd_status(uint16_t cmdq_resp, uint8_t resp_code)
2874 #define HNS3_ETHERTYPE_SUCCESS_ADD 0
2875 #define HNS3_ETHERTYPE_ALREADY_ADD 1
2876 #define HNS3_ETHERTYPE_MGR_TBL_OVERFLOW 2
2877 #define HNS3_ETHERTYPE_KEY_CONFLICT 3
2882 "cmdq execute failed for get_mac_ethertype_cmd_status, status=%d.\n",
2887 switch (resp_code) {
2888 case HNS3_ETHERTYPE_SUCCESS_ADD:
2889 case HNS3_ETHERTYPE_ALREADY_ADD:
2892 case HNS3_ETHERTYPE_MGR_TBL_OVERFLOW:
2894 "add mac ethertype failed for manager table overflow.");
2895 return_status = -EIO;
2897 case HNS3_ETHERTYPE_KEY_CONFLICT:
2898 PMD_INIT_LOG(ERR, "add mac ethertype failed for key conflict.");
2899 return_status = -EIO;
2903 "add mac ethertype failed for undefined, code=%d.",
2905 return_status = -EIO;
2908 return return_status;
2912 hns3_add_mgr_tbl(struct hns3_hw *hw,
2913 const struct hns3_mac_mgr_tbl_entry_cmd *req)
2915 struct hns3_cmd_desc desc;
2920 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_MAC_ETHTYPE_ADD, false);
2921 memcpy(desc.data, req, sizeof(struct hns3_mac_mgr_tbl_entry_cmd));
2923 ret = hns3_cmd_send(hw, &desc, 1);
2926 "add mac ethertype failed for cmd_send, ret =%d.",
2931 resp_code = (rte_le_to_cpu_32(desc.data[0]) >> 8) & 0xff;
2932 retval = rte_le_to_cpu_16(desc.retval);
2934 return hns3_get_mac_ethertype_cmd_status(retval, resp_code);
2938 hns3_prepare_mgr_tbl(struct hns3_mac_mgr_tbl_entry_cmd *mgr_table,
2939 int *table_item_num)
2941 struct hns3_mac_mgr_tbl_entry_cmd *tbl;
2944 * In current version, we add one item in management table as below:
2945 * 0x0180C200000E -- LLDP MC address
2948 tbl->flags = HNS3_MAC_MGR_MASK_VLAN_B;
2949 tbl->ethter_type = rte_cpu_to_le_16(HNS3_MAC_ETHERTYPE_LLDP);
2950 tbl->mac_addr_hi32 = rte_cpu_to_le_32(htonl(0x0180C200));
2951 tbl->mac_addr_lo16 = rte_cpu_to_le_16(htons(0x000E));
2952 tbl->i_port_bitmap = 0x1;
2953 *table_item_num = 1;
2957 hns3_init_mgr_tbl(struct hns3_hw *hw)
2959 #define HNS_MAC_MGR_TBL_MAX_SIZE 16
2960 struct hns3_mac_mgr_tbl_entry_cmd mgr_table[HNS_MAC_MGR_TBL_MAX_SIZE];
2965 memset(mgr_table, 0, sizeof(mgr_table));
2966 hns3_prepare_mgr_tbl(mgr_table, &table_item_num);
2967 for (i = 0; i < table_item_num; i++) {
2968 ret = hns3_add_mgr_tbl(hw, &mgr_table[i]);
2970 PMD_INIT_LOG(ERR, "add mac ethertype failed, ret =%d",
2980 hns3_promisc_param_init(struct hns3_promisc_param *param, bool en_uc,
2981 bool en_mc, bool en_bc, int vport_id)
2986 memset(param, 0, sizeof(struct hns3_promisc_param));
2988 param->enable = HNS3_PROMISC_EN_UC;
2990 param->enable |= HNS3_PROMISC_EN_MC;
2992 param->enable |= HNS3_PROMISC_EN_BC;
2993 param->vf_id = vport_id;
2997 hns3_cmd_set_promisc_mode(struct hns3_hw *hw, struct hns3_promisc_param *param)
2999 struct hns3_promisc_cfg_cmd *req;
3000 struct hns3_cmd_desc desc;
3003 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_CFG_PROMISC_MODE, false);
3005 req = (struct hns3_promisc_cfg_cmd *)desc.data;
3006 req->vf_id = param->vf_id;
3007 req->flag = (param->enable << HNS3_PROMISC_EN_B) |
3008 HNS3_PROMISC_TX_EN_B | HNS3_PROMISC_RX_EN_B;
3010 ret = hns3_cmd_send(hw, &desc, 1);
3012 PMD_INIT_LOG(ERR, "Set promisc mode fail, status is %d", ret);
3018 hns3_set_promisc_mode(struct hns3_hw *hw, bool en_uc_pmc, bool en_mc_pmc)
3020 struct hns3_promisc_param param;
3021 bool en_bc_pmc = true;
3026 * In current version VF is not supported when PF is driven by DPDK
3027 * driver, the PF-related vf_id is 0, just need to configure parameters
3032 hns3_promisc_param_init(¶m, en_uc_pmc, en_mc_pmc, en_bc_pmc, vf_id);
3033 ret = hns3_cmd_set_promisc_mode(hw, ¶m);
3041 hns3_get_sfp_speed(struct hns3_hw *hw, uint32_t *speed)
3043 struct hns3_sfp_speed_cmd *resp;
3044 struct hns3_cmd_desc desc;
3047 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_SFP_GET_SPEED, true);
3048 resp = (struct hns3_sfp_speed_cmd *)desc.data;
3049 ret = hns3_cmd_send(hw, &desc, 1);
3050 if (ret == -EOPNOTSUPP) {
3051 hns3_err(hw, "IMP do not support get SFP speed %d", ret);
3054 hns3_err(hw, "get sfp speed failed %d", ret);
3058 *speed = resp->sfp_speed;
3064 hns3_check_speed_dup(uint8_t duplex, uint32_t speed)
3066 if (!(speed == ETH_SPEED_NUM_10M || speed == ETH_SPEED_NUM_100M))
3067 duplex = ETH_LINK_FULL_DUPLEX;
3073 hns3_cfg_mac_speed_dup(struct hns3_hw *hw, uint32_t speed, uint8_t duplex)
3075 struct hns3_mac *mac = &hw->mac;
3078 duplex = hns3_check_speed_dup(duplex, speed);
3079 if (mac->link_speed == speed && mac->link_duplex == duplex)
3082 ret = hns3_cfg_mac_speed_dup_hw(hw, speed, duplex);
3086 mac->link_speed = speed;
3087 mac->link_duplex = duplex;
3093 hns3_update_speed_duplex(struct rte_eth_dev *eth_dev)
3095 struct hns3_adapter *hns = eth_dev->data->dev_private;
3096 struct hns3_hw *hw = &hns->hw;
3097 struct hns3_pf *pf = &hns->pf;
3101 /* If IMP do not support get SFP/qSFP speed, return directly */
3102 if (!pf->support_sfp_query)
3105 ret = hns3_get_sfp_speed(hw, &speed);
3106 if (ret == -EOPNOTSUPP) {
3107 pf->support_sfp_query = false;
3112 if (speed == ETH_SPEED_NUM_NONE)
3113 return 0; /* do nothing if no SFP */
3115 /* Config full duplex for SFP */
3116 return hns3_cfg_mac_speed_dup(hw, speed, ETH_LINK_FULL_DUPLEX);
3120 hns3_get_mac_link_status(struct hns3_hw *hw)
3122 struct hns3_link_status_cmd *req;
3123 struct hns3_cmd_desc desc;
3127 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_QUERY_LINK_STATUS, true);
3128 ret = hns3_cmd_send(hw, &desc, 1);
3130 hns3_err(hw, "get link status cmd failed %d", ret);
3134 req = (struct hns3_link_status_cmd *)desc.data;
3135 link_status = req->status & HNS3_LINK_STATUS_UP_M;
3137 return !!link_status;
3141 hns3_update_link_status(struct hns3_hw *hw)
3145 state = hns3_get_mac_link_status(hw);
3146 if (state != hw->mac.link_status)
3147 hw->mac.link_status = state;
3151 hns3_service_handler(void *param)
3153 struct rte_eth_dev *eth_dev = (struct rte_eth_dev *)param;
3154 struct hns3_adapter *hns = eth_dev->data->dev_private;
3155 struct hns3_hw *hw = &hns->hw;
3157 hns3_update_speed_duplex(eth_dev);
3158 hns3_update_link_status(hw);
3160 rte_eal_alarm_set(HNS3_SERVICE_INTERVAL, hns3_service_handler, eth_dev);
3164 hns3_init_hardware(struct hns3_adapter *hns)
3166 struct hns3_hw *hw = &hns->hw;
3169 ret = hns3_map_tqp(hw);
3171 PMD_INIT_LOG(ERR, "Failed to map tqp: %d", ret);
3175 ret = hns3_init_umv_space(hw);
3177 PMD_INIT_LOG(ERR, "Failed to init umv space: %d", ret);
3181 ret = hns3_mac_init(hw);
3183 PMD_INIT_LOG(ERR, "Failed to init MAC: %d", ret);
3187 ret = hns3_init_mgr_tbl(hw);
3189 PMD_INIT_LOG(ERR, "Failed to init manager table: %d", ret);
3193 ret = hns3_set_promisc_mode(hw, false, false);
3195 PMD_INIT_LOG(ERR, "Failed to set promisc mode: %d", ret);
3199 ret = hns3_init_vlan_config(hns);
3201 PMD_INIT_LOG(ERR, "Failed to init vlan: %d", ret);
3205 ret = hns3_dcb_init(hw);
3207 PMD_INIT_LOG(ERR, "Failed to init dcb: %d", ret);
3211 ret = hns3_init_fd_config(hns);
3213 PMD_INIT_LOG(ERR, "Failed to init flow director: %d", ret);
3217 ret = hns3_config_tso(hw, HNS3_TSO_MSS_MIN, HNS3_TSO_MSS_MAX);
3219 PMD_INIT_LOG(ERR, "Failed to config tso: %d", ret);
3223 ret = hns3_config_gro(hw, false);
3225 PMD_INIT_LOG(ERR, "Failed to config gro: %d", ret);
3231 hns3_uninit_umv_space(hw);
3236 hns3_init_pf(struct rte_eth_dev *eth_dev)
3238 struct rte_device *dev = eth_dev->device;
3239 struct rte_pci_device *pci_dev = RTE_DEV_TO_PCI(dev);
3240 struct hns3_adapter *hns = eth_dev->data->dev_private;
3241 struct hns3_hw *hw = &hns->hw;
3244 PMD_INIT_FUNC_TRACE();
3246 /* Get hardware io base address from pcie BAR2 IO space */
3247 hw->io_base = pci_dev->mem_resource[2].addr;
3249 /* Firmware command queue initialize */
3250 ret = hns3_cmd_init_queue(hw);
3252 PMD_INIT_LOG(ERR, "Failed to init cmd queue: %d", ret);
3253 goto err_cmd_init_queue;
3256 /* Firmware command initialize */
3257 ret = hns3_cmd_init(hw);
3259 PMD_INIT_LOG(ERR, "Failed to init cmd: %d", ret);
3263 /* Get configuration */
3264 ret = hns3_get_configuration(hw);
3266 PMD_INIT_LOG(ERR, "Failed to fetch configuration: %d", ret);
3267 goto err_get_config;
3270 ret = hns3_init_hardware(hns);
3272 PMD_INIT_LOG(ERR, "Failed to init hardware: %d", ret);
3273 goto err_get_config;
3276 /* Initialize flow director filter list & hash */
3277 ret = hns3_fdir_filter_init(hns);
3279 PMD_INIT_LOG(ERR, "Failed to alloc hashmap for fdir: %d", ret);
3283 hns3_set_default_rss_args(hw);
3288 hns3_uninit_umv_space(hw);
3291 hns3_cmd_uninit(hw);
3294 hns3_cmd_destroy_queue(hw);
3303 hns3_uninit_pf(struct rte_eth_dev *eth_dev)
3305 struct hns3_adapter *hns = eth_dev->data->dev_private;
3306 struct hns3_hw *hw = &hns->hw;
3308 PMD_INIT_FUNC_TRACE();
3310 hns3_rss_uninit(hns);
3311 hns3_fdir_filter_uninit(hns);
3312 hns3_uninit_umv_space(hw);
3313 hns3_cmd_uninit(hw);
3314 hns3_cmd_destroy_queue(hw);
3319 hns3_dev_close(struct rte_eth_dev *eth_dev)
3321 struct hns3_adapter *hns = eth_dev->data->dev_private;
3322 struct hns3_hw *hw = &hns->hw;
3324 hw->adapter_state = HNS3_NIC_CLOSING;
3325 rte_eal_alarm_cancel(hns3_service_handler, eth_dev);
3327 hns3_configure_all_mc_mac_addr(hns, true);
3328 hns3_uninit_pf(eth_dev);
3329 rte_free(eth_dev->process_private);
3330 eth_dev->process_private = NULL;
3331 hw->adapter_state = HNS3_NIC_CLOSED;
3335 hns3_flow_ctrl_get(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
3337 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
3338 struct hns3_pf *pf = HNS3_DEV_PRIVATE_TO_PF(dev->data->dev_private);
3340 fc_conf->pause_time = pf->pause_time;
3342 /* return fc current mode */
3343 switch (hw->current_mode) {
3345 fc_conf->mode = RTE_FC_FULL;
3347 case HNS3_FC_TX_PAUSE:
3348 fc_conf->mode = RTE_FC_TX_PAUSE;
3350 case HNS3_FC_RX_PAUSE:
3351 fc_conf->mode = RTE_FC_RX_PAUSE;
3355 fc_conf->mode = RTE_FC_NONE;
3363 hns3_get_fc_mode(struct hns3_hw *hw, enum rte_eth_fc_mode mode)
3367 hw->requested_mode = HNS3_FC_NONE;
3369 case RTE_FC_RX_PAUSE:
3370 hw->requested_mode = HNS3_FC_RX_PAUSE;
3372 case RTE_FC_TX_PAUSE:
3373 hw->requested_mode = HNS3_FC_TX_PAUSE;
3376 hw->requested_mode = HNS3_FC_FULL;
3379 hw->requested_mode = HNS3_FC_NONE;
3380 hns3_warn(hw, "fc_mode(%u) exceeds member scope and is "
3381 "configured to RTE_FC_NONE", mode);
3387 hns3_flow_ctrl_set(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
3389 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
3390 struct hns3_pf *pf = HNS3_DEV_PRIVATE_TO_PF(dev->data->dev_private);
3393 if (fc_conf->high_water || fc_conf->low_water ||
3394 fc_conf->send_xon || fc_conf->mac_ctrl_frame_fwd) {
3395 hns3_err(hw, "Unsupported flow control settings specified, "
3396 "high_water(%u), low_water(%u), send_xon(%u) and "
3397 "mac_ctrl_frame_fwd(%u) must be set to '0'",
3398 fc_conf->high_water, fc_conf->low_water,
3399 fc_conf->send_xon, fc_conf->mac_ctrl_frame_fwd);
3402 if (fc_conf->autoneg) {
3403 hns3_err(hw, "Unsupported fc auto-negotiation setting.");
3406 if (!fc_conf->pause_time) {
3407 hns3_err(hw, "Invalid pause time %d setting.",
3408 fc_conf->pause_time);
3412 if (!(hw->current_fc_status == HNS3_FC_STATUS_NONE ||
3413 hw->current_fc_status == HNS3_FC_STATUS_MAC_PAUSE)) {
3414 hns3_err(hw, "PFC is enabled. Cannot set MAC pause. "
3415 "current_fc_status = %d", hw->current_fc_status);
3419 hns3_get_fc_mode(hw, fc_conf->mode);
3420 if (hw->requested_mode == hw->current_mode &&
3421 pf->pause_time == fc_conf->pause_time)
3424 rte_spinlock_lock(&hw->lock);
3425 ret = hns3_fc_enable(dev, fc_conf);
3426 rte_spinlock_unlock(&hw->lock);
3432 hns3_priority_flow_ctrl_set(struct rte_eth_dev *dev,
3433 struct rte_eth_pfc_conf *pfc_conf)
3435 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
3436 struct hns3_pf *pf = HNS3_DEV_PRIVATE_TO_PF(dev->data->dev_private);
3440 if (!hns3_dev_dcb_supported(hw)) {
3441 hns3_err(hw, "This port does not support dcb configurations.");
3445 if (pfc_conf->fc.high_water || pfc_conf->fc.low_water ||
3446 pfc_conf->fc.send_xon || pfc_conf->fc.mac_ctrl_frame_fwd) {
3447 hns3_err(hw, "Unsupported flow control settings specified, "
3448 "high_water(%u), low_water(%u), send_xon(%u) and "
3449 "mac_ctrl_frame_fwd(%u) must be set to '0'",
3450 pfc_conf->fc.high_water, pfc_conf->fc.low_water,
3451 pfc_conf->fc.send_xon,
3452 pfc_conf->fc.mac_ctrl_frame_fwd);
3455 if (pfc_conf->fc.autoneg) {
3456 hns3_err(hw, "Unsupported fc auto-negotiation setting.");
3459 if (pfc_conf->fc.pause_time == 0) {
3460 hns3_err(hw, "Invalid pause time %d setting.",
3461 pfc_conf->fc.pause_time);
3465 if (!(hw->current_fc_status == HNS3_FC_STATUS_NONE ||
3466 hw->current_fc_status == HNS3_FC_STATUS_PFC)) {
3467 hns3_err(hw, "MAC pause is enabled. Cannot set PFC."
3468 "current_fc_status = %d", hw->current_fc_status);
3472 priority = pfc_conf->priority;
3473 hns3_get_fc_mode(hw, pfc_conf->fc.mode);
3474 if (hw->dcb_info.pfc_en & BIT(priority) &&
3475 hw->requested_mode == hw->current_mode &&
3476 pfc_conf->fc.pause_time == pf->pause_time)
3479 rte_spinlock_lock(&hw->lock);
3480 ret = hns3_dcb_pfc_enable(dev, pfc_conf);
3481 rte_spinlock_unlock(&hw->lock);
3487 hns3_get_dcb_info(struct rte_eth_dev *dev, struct rte_eth_dcb_info *dcb_info)
3489 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
3490 struct hns3_pf *pf = HNS3_DEV_PRIVATE_TO_PF(dev->data->dev_private);
3491 enum rte_eth_rx_mq_mode mq_mode = dev->data->dev_conf.rxmode.mq_mode;
3494 rte_spinlock_lock(&hw->lock);
3495 if ((uint32_t)mq_mode & ETH_MQ_RX_DCB_FLAG)
3496 dcb_info->nb_tcs = pf->local_max_tc;
3498 dcb_info->nb_tcs = 1;
3500 for (i = 0; i < HNS3_MAX_USER_PRIO; i++)
3501 dcb_info->prio_tc[i] = hw->dcb_info.prio_tc[i];
3502 for (i = 0; i < dcb_info->nb_tcs; i++)
3503 dcb_info->tc_bws[i] = hw->dcb_info.pg_info[0].tc_dwrr[i];
3505 for (i = 0; i < HNS3_MAX_TC_NUM; i++) {
3506 dcb_info->tc_queue.tc_rxq[0][i].base =
3507 hw->tc_queue[i].tqp_offset;
3508 dcb_info->tc_queue.tc_txq[0][i].base =
3509 hw->tc_queue[i].tqp_offset;
3510 dcb_info->tc_queue.tc_rxq[0][i].nb_queue =
3511 hw->tc_queue[i].tqp_count;
3512 dcb_info->tc_queue.tc_txq[0][i].nb_queue =
3513 hw->tc_queue[i].tqp_count;
3515 rte_spinlock_unlock(&hw->lock);
3520 static const struct eth_dev_ops hns3_eth_dev_ops = {
3521 .dev_close = hns3_dev_close,
3522 .mtu_set = hns3_dev_mtu_set,
3523 .dev_infos_get = hns3_dev_infos_get,
3524 .fw_version_get = hns3_fw_version_get,
3525 .flow_ctrl_get = hns3_flow_ctrl_get,
3526 .flow_ctrl_set = hns3_flow_ctrl_set,
3527 .priority_flow_ctrl_set = hns3_priority_flow_ctrl_set,
3528 .mac_addr_add = hns3_add_mac_addr,
3529 .mac_addr_remove = hns3_remove_mac_addr,
3530 .mac_addr_set = hns3_set_default_mac_addr,
3531 .set_mc_addr_list = hns3_set_mc_mac_addr_list,
3532 .link_update = hns3_dev_link_update,
3533 .rss_hash_update = hns3_dev_rss_hash_update,
3534 .rss_hash_conf_get = hns3_dev_rss_hash_conf_get,
3535 .reta_update = hns3_dev_rss_reta_update,
3536 .reta_query = hns3_dev_rss_reta_query,
3537 .filter_ctrl = hns3_dev_filter_ctrl,
3538 .vlan_filter_set = hns3_vlan_filter_set,
3539 .vlan_tpid_set = hns3_vlan_tpid_set,
3540 .vlan_offload_set = hns3_vlan_offload_set,
3541 .vlan_pvid_set = hns3_vlan_pvid_set,
3542 .get_dcb_info = hns3_get_dcb_info,
3546 hns3_dev_init(struct rte_eth_dev *eth_dev)
3548 struct rte_device *dev = eth_dev->device;
3549 struct rte_pci_device *pci_dev = RTE_DEV_TO_PCI(dev);
3550 struct hns3_adapter *hns = eth_dev->data->dev_private;
3551 struct hns3_hw *hw = &hns->hw;
3552 uint16_t device_id = pci_dev->id.device_id;
3555 PMD_INIT_FUNC_TRACE();
3556 eth_dev->process_private = (struct hns3_process_private *)
3557 rte_zmalloc_socket("hns3_filter_list",
3558 sizeof(struct hns3_process_private),
3559 RTE_CACHE_LINE_SIZE, eth_dev->device->numa_node);
3560 if (eth_dev->process_private == NULL) {
3561 PMD_INIT_LOG(ERR, "Failed to alloc memory for process private");
3564 /* initialize flow filter lists */
3565 hns3_filterlist_init(eth_dev);
3567 eth_dev->dev_ops = &hns3_eth_dev_ops;
3568 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
3571 hw->adapter_state = HNS3_NIC_UNINITIALIZED;
3573 if (device_id == HNS3_DEV_ID_25GE_RDMA ||
3574 device_id == HNS3_DEV_ID_50GE_RDMA ||
3575 device_id == HNS3_DEV_ID_100G_RDMA_MACSEC)
3576 hns3_set_bit(hw->flag, HNS3_DEV_SUPPORT_DCB_B, 1);
3579 hw->data = eth_dev->data;
3582 * Set default max packet size according to the mtu
3583 * default vale in DPDK frame.
3585 hns->pf.mps = hw->data->mtu + HNS3_ETH_OVERHEAD;
3587 ret = hns3_init_pf(eth_dev);
3589 PMD_INIT_LOG(ERR, "Failed to init pf: %d", ret);
3593 /* Allocate memory for storing MAC addresses */
3594 eth_dev->data->mac_addrs = rte_zmalloc("hns3-mac",
3595 sizeof(struct rte_ether_addr) *
3596 HNS3_UC_MACADDR_NUM, 0);
3597 if (eth_dev->data->mac_addrs == NULL) {
3598 PMD_INIT_LOG(ERR, "Failed to allocate %zx bytes needed "
3599 "to store MAC addresses",
3600 sizeof(struct rte_ether_addr) *
3601 HNS3_UC_MACADDR_NUM);
3603 goto err_rte_zmalloc;
3606 rte_ether_addr_copy((struct rte_ether_addr *)hw->mac.mac_addr,
3607 ð_dev->data->mac_addrs[0]);
3609 hw->adapter_state = HNS3_NIC_INITIALIZED;
3611 * Pass the information to the rte_eth_dev_close() that it should also
3612 * release the private port resources.
3614 eth_dev->data->dev_flags |= RTE_ETH_DEV_CLOSE_REMOVE;
3616 rte_eal_alarm_set(HNS3_SERVICE_INTERVAL, hns3_service_handler, eth_dev);
3617 hns3_info(hw, "hns3 dev initialization successful!");
3621 hns3_uninit_pf(eth_dev);
3624 eth_dev->dev_ops = NULL;
3625 eth_dev->rx_pkt_burst = NULL;
3626 eth_dev->tx_pkt_burst = NULL;
3627 eth_dev->tx_pkt_prepare = NULL;
3628 rte_free(eth_dev->process_private);
3629 eth_dev->process_private = NULL;
3634 hns3_dev_uninit(struct rte_eth_dev *eth_dev)
3636 struct hns3_adapter *hns = eth_dev->data->dev_private;
3637 struct hns3_hw *hw = &hns->hw;
3639 PMD_INIT_FUNC_TRACE();
3641 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
3644 eth_dev->dev_ops = NULL;
3645 eth_dev->rx_pkt_burst = NULL;
3646 eth_dev->tx_pkt_burst = NULL;
3647 eth_dev->tx_pkt_prepare = NULL;
3648 if (hw->adapter_state < HNS3_NIC_CLOSING)
3649 hns3_dev_close(eth_dev);
3651 hw->adapter_state = HNS3_NIC_REMOVED;
3656 eth_hns3_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
3657 struct rte_pci_device *pci_dev)
3659 return rte_eth_dev_pci_generic_probe(pci_dev,
3660 sizeof(struct hns3_adapter),
3665 eth_hns3_pci_remove(struct rte_pci_device *pci_dev)
3667 return rte_eth_dev_pci_generic_remove(pci_dev, hns3_dev_uninit);
3670 static const struct rte_pci_id pci_id_hns3_map[] = {
3671 { RTE_PCI_DEVICE(PCI_VENDOR_ID_HUAWEI, HNS3_DEV_ID_GE) },
3672 { RTE_PCI_DEVICE(PCI_VENDOR_ID_HUAWEI, HNS3_DEV_ID_25GE) },
3673 { RTE_PCI_DEVICE(PCI_VENDOR_ID_HUAWEI, HNS3_DEV_ID_25GE_RDMA) },
3674 { RTE_PCI_DEVICE(PCI_VENDOR_ID_HUAWEI, HNS3_DEV_ID_50GE_RDMA) },
3675 { RTE_PCI_DEVICE(PCI_VENDOR_ID_HUAWEI, HNS3_DEV_ID_100G_RDMA_MACSEC) },
3676 { .vendor_id = 0, /* sentinel */ },
3679 static struct rte_pci_driver rte_hns3_pmd = {
3680 .id_table = pci_id_hns3_map,
3681 .drv_flags = RTE_PCI_DRV_NEED_MAPPING,
3682 .probe = eth_hns3_pci_probe,
3683 .remove = eth_hns3_pci_remove,
3686 RTE_PMD_REGISTER_PCI(net_hns3, rte_hns3_pmd);
3687 RTE_PMD_REGISTER_PCI_TABLE(net_hns3, pci_id_hns3_map);
3688 RTE_PMD_REGISTER_KMOD_DEP(net_hns3, "* igb_uio | vfio-pci");
3690 RTE_INIT(hns3_init_log)
3692 hns3_logtype_init = rte_log_register("pmd.net.hns3.init");
3693 if (hns3_logtype_init >= 0)
3694 rte_log_set_level(hns3_logtype_init, RTE_LOG_NOTICE);
3695 hns3_logtype_driver = rte_log_register("pmd.net.hns3.driver");
3696 if (hns3_logtype_driver >= 0)
3697 rte_log_set_level(hns3_logtype_driver, RTE_LOG_NOTICE);