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"
28 #define HNS3_DEFAULT_PORT_CONF_BURST_SIZE 32
29 #define HNS3_DEFAULT_PORT_CONF_QUEUES_NUM 1
31 int hns3_logtype_init;
32 int hns3_logtype_driver;
35 hns3_config_tso(struct hns3_hw *hw, unsigned int tso_mss_min,
36 unsigned int tso_mss_max)
38 struct hns3_cfg_tso_status_cmd *req;
39 struct hns3_cmd_desc desc;
42 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_TSO_GENERIC_CONFIG, false);
44 req = (struct hns3_cfg_tso_status_cmd *)desc.data;
47 hns3_set_field(tso_mss, HNS3_TSO_MSS_MIN_M, HNS3_TSO_MSS_MIN_S,
49 req->tso_mss_min = rte_cpu_to_le_16(tso_mss);
52 hns3_set_field(tso_mss, HNS3_TSO_MSS_MIN_M, HNS3_TSO_MSS_MIN_S,
54 req->tso_mss_max = rte_cpu_to_le_16(tso_mss);
56 return hns3_cmd_send(hw, &desc, 1);
60 hns3_config_gro(struct hns3_hw *hw, bool en)
62 struct hns3_cfg_gro_status_cmd *req;
63 struct hns3_cmd_desc desc;
66 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_GRO_GENERIC_CONFIG, false);
67 req = (struct hns3_cfg_gro_status_cmd *)desc.data;
69 req->gro_en = rte_cpu_to_le_16(en ? 1 : 0);
71 ret = hns3_cmd_send(hw, &desc, 1);
73 hns3_err(hw, "GRO hardware config cmd failed, ret = %d", ret);
79 hns3_set_umv_space(struct hns3_hw *hw, uint16_t space_size,
80 uint16_t *allocated_size, bool is_alloc)
82 struct hns3_umv_spc_alc_cmd *req;
83 struct hns3_cmd_desc desc;
86 req = (struct hns3_umv_spc_alc_cmd *)desc.data;
87 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_MAC_VLAN_ALLOCATE, false);
88 hns3_set_bit(req->allocate, HNS3_UMV_SPC_ALC_B, is_alloc ? 0 : 1);
89 req->space_size = rte_cpu_to_le_32(space_size);
91 ret = hns3_cmd_send(hw, &desc, 1);
93 PMD_INIT_LOG(ERR, "%s umv space failed for cmd_send, ret =%d",
94 is_alloc ? "allocate" : "free", ret);
98 if (is_alloc && allocated_size)
99 *allocated_size = rte_le_to_cpu_32(desc.data[1]);
105 hns3_init_umv_space(struct hns3_hw *hw)
107 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
108 struct hns3_pf *pf = &hns->pf;
109 uint16_t allocated_size = 0;
112 ret = hns3_set_umv_space(hw, pf->wanted_umv_size, &allocated_size,
117 if (allocated_size < pf->wanted_umv_size)
118 PMD_INIT_LOG(WARNING, "Alloc umv space failed, want %u, get %u",
119 pf->wanted_umv_size, allocated_size);
121 pf->max_umv_size = (!!allocated_size) ? allocated_size :
123 pf->used_umv_size = 0;
128 hns3_uninit_umv_space(struct hns3_hw *hw)
130 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
131 struct hns3_pf *pf = &hns->pf;
134 if (pf->max_umv_size == 0)
137 ret = hns3_set_umv_space(hw, pf->max_umv_size, NULL, false);
141 pf->max_umv_size = 0;
147 hns3_is_umv_space_full(struct hns3_hw *hw)
149 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
150 struct hns3_pf *pf = &hns->pf;
153 is_full = (pf->used_umv_size >= pf->max_umv_size);
159 hns3_update_umv_space(struct hns3_hw *hw, bool is_free)
161 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
162 struct hns3_pf *pf = &hns->pf;
165 if (pf->used_umv_size > 0)
172 hns3_prepare_mac_addr(struct hns3_mac_vlan_tbl_entry_cmd *new_req,
173 const uint8_t *addr, bool is_mc)
175 const unsigned char *mac_addr = addr;
176 uint32_t high_val = ((uint32_t)mac_addr[3] << 24) |
177 ((uint32_t)mac_addr[2] << 16) |
178 ((uint32_t)mac_addr[1] << 8) |
179 (uint32_t)mac_addr[0];
180 uint32_t low_val = ((uint32_t)mac_addr[5] << 8) | (uint32_t)mac_addr[4];
182 hns3_set_bit(new_req->flags, HNS3_MAC_VLAN_BIT0_EN_B, 1);
184 hns3_set_bit(new_req->entry_type, HNS3_MAC_VLAN_BIT0_EN_B, 0);
185 hns3_set_bit(new_req->entry_type, HNS3_MAC_VLAN_BIT1_EN_B, 1);
186 hns3_set_bit(new_req->mc_mac_en, HNS3_MAC_VLAN_BIT0_EN_B, 1);
189 new_req->mac_addr_hi32 = rte_cpu_to_le_32(high_val);
190 new_req->mac_addr_lo16 = rte_cpu_to_le_16(low_val & 0xffff);
194 hns3_get_mac_vlan_cmd_status(struct hns3_hw *hw, uint16_t cmdq_resp,
196 enum hns3_mac_vlan_tbl_opcode op)
199 hns3_err(hw, "cmdq execute failed for get_mac_vlan_cmd_status,status=%u",
204 if (op == HNS3_MAC_VLAN_ADD) {
205 if (resp_code == 0 || resp_code == 1) {
207 } else if (resp_code == HNS3_ADD_UC_OVERFLOW) {
208 hns3_err(hw, "add mac addr failed for uc_overflow");
210 } else if (resp_code == HNS3_ADD_MC_OVERFLOW) {
211 hns3_err(hw, "add mac addr failed for mc_overflow");
215 hns3_err(hw, "add mac addr failed for undefined, code=%u",
218 } else if (op == HNS3_MAC_VLAN_REMOVE) {
219 if (resp_code == 0) {
221 } else if (resp_code == 1) {
222 hns3_dbg(hw, "remove mac addr failed for miss");
226 hns3_err(hw, "remove mac addr failed for undefined, code=%u",
229 } else if (op == HNS3_MAC_VLAN_LKUP) {
230 if (resp_code == 0) {
232 } else if (resp_code == 1) {
233 hns3_dbg(hw, "lookup mac addr failed for miss");
237 hns3_err(hw, "lookup mac addr failed for undefined, code=%u",
242 hns3_err(hw, "unknown opcode for get_mac_vlan_cmd_status, opcode=%u",
249 hns3_lookup_mac_vlan_tbl(struct hns3_hw *hw,
250 struct hns3_mac_vlan_tbl_entry_cmd *req,
251 struct hns3_cmd_desc *desc, bool is_mc)
257 hns3_cmd_setup_basic_desc(&desc[0], HNS3_OPC_MAC_VLAN_ADD, true);
259 desc[0].flag |= rte_cpu_to_le_16(HNS3_CMD_FLAG_NEXT);
260 memcpy(desc[0].data, req,
261 sizeof(struct hns3_mac_vlan_tbl_entry_cmd));
262 hns3_cmd_setup_basic_desc(&desc[1], HNS3_OPC_MAC_VLAN_ADD,
264 desc[1].flag |= rte_cpu_to_le_16(HNS3_CMD_FLAG_NEXT);
265 hns3_cmd_setup_basic_desc(&desc[2], HNS3_OPC_MAC_VLAN_ADD,
267 ret = hns3_cmd_send(hw, desc, HNS3_MC_MAC_VLAN_ADD_DESC_NUM);
269 memcpy(desc[0].data, req,
270 sizeof(struct hns3_mac_vlan_tbl_entry_cmd));
271 ret = hns3_cmd_send(hw, desc, 1);
274 hns3_err(hw, "lookup mac addr failed for cmd_send, ret =%d.",
278 resp_code = (rte_le_to_cpu_32(desc[0].data[0]) >> 8) & 0xff;
279 retval = rte_le_to_cpu_16(desc[0].retval);
281 return hns3_get_mac_vlan_cmd_status(hw, retval, resp_code,
286 hns3_add_mac_vlan_tbl(struct hns3_hw *hw,
287 struct hns3_mac_vlan_tbl_entry_cmd *req,
288 struct hns3_cmd_desc *mc_desc)
295 if (mc_desc == NULL) {
296 struct hns3_cmd_desc desc;
298 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_MAC_VLAN_ADD, false);
299 memcpy(desc.data, req,
300 sizeof(struct hns3_mac_vlan_tbl_entry_cmd));
301 ret = hns3_cmd_send(hw, &desc, 1);
302 resp_code = (rte_le_to_cpu_32(desc.data[0]) >> 8) & 0xff;
303 retval = rte_le_to_cpu_16(desc.retval);
305 cfg_status = hns3_get_mac_vlan_cmd_status(hw, retval, resp_code,
308 hns3_cmd_reuse_desc(&mc_desc[0], false);
309 mc_desc[0].flag |= rte_cpu_to_le_16(HNS3_CMD_FLAG_NEXT);
310 hns3_cmd_reuse_desc(&mc_desc[1], false);
311 mc_desc[1].flag |= rte_cpu_to_le_16(HNS3_CMD_FLAG_NEXT);
312 hns3_cmd_reuse_desc(&mc_desc[2], false);
313 mc_desc[2].flag &= rte_cpu_to_le_16(~HNS3_CMD_FLAG_NEXT);
314 memcpy(mc_desc[0].data, req,
315 sizeof(struct hns3_mac_vlan_tbl_entry_cmd));
316 mc_desc[0].retval = 0;
317 ret = hns3_cmd_send(hw, mc_desc, HNS3_MC_MAC_VLAN_ADD_DESC_NUM);
318 resp_code = (rte_le_to_cpu_32(mc_desc[0].data[0]) >> 8) & 0xff;
319 retval = rte_le_to_cpu_16(mc_desc[0].retval);
321 cfg_status = hns3_get_mac_vlan_cmd_status(hw, retval, resp_code,
326 hns3_err(hw, "add mac addr failed for cmd_send, ret =%d", ret);
334 hns3_remove_mac_vlan_tbl(struct hns3_hw *hw,
335 struct hns3_mac_vlan_tbl_entry_cmd *req)
337 struct hns3_cmd_desc desc;
342 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_MAC_VLAN_REMOVE, false);
344 memcpy(desc.data, req, sizeof(struct hns3_mac_vlan_tbl_entry_cmd));
346 ret = hns3_cmd_send(hw, &desc, 1);
348 hns3_err(hw, "del mac addr failed for cmd_send, ret =%d", ret);
351 resp_code = (rte_le_to_cpu_32(desc.data[0]) >> 8) & 0xff;
352 retval = rte_le_to_cpu_16(desc.retval);
354 return hns3_get_mac_vlan_cmd_status(hw, retval, resp_code,
355 HNS3_MAC_VLAN_REMOVE);
359 hns3_add_uc_addr_common(struct hns3_hw *hw, struct rte_ether_addr *mac_addr)
361 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
362 struct hns3_mac_vlan_tbl_entry_cmd req;
363 struct hns3_pf *pf = &hns->pf;
364 struct hns3_cmd_desc desc;
365 char mac_str[RTE_ETHER_ADDR_FMT_SIZE];
366 uint16_t egress_port = 0;
370 /* check if mac addr is valid */
371 if (!rte_is_valid_assigned_ether_addr(mac_addr)) {
372 rte_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
374 hns3_err(hw, "Add unicast mac addr err! addr(%s) invalid",
379 memset(&req, 0, sizeof(req));
382 * In current version VF is not supported when PF is driven by DPDK
383 * driver, the PF-related vf_id is 0, just need to configure parameters
387 hns3_set_field(egress_port, HNS3_MAC_EPORT_VFID_M,
388 HNS3_MAC_EPORT_VFID_S, vf_id);
390 req.egress_port = rte_cpu_to_le_16(egress_port);
392 hns3_prepare_mac_addr(&req, mac_addr->addr_bytes, false);
395 * Lookup the mac address in the mac_vlan table, and add
396 * it if the entry is inexistent. Repeated unicast entry
397 * is not allowed in the mac vlan table.
399 ret = hns3_lookup_mac_vlan_tbl(hw, &req, &desc, false);
400 if (ret == -ENOENT) {
401 if (!hns3_is_umv_space_full(hw)) {
402 ret = hns3_add_mac_vlan_tbl(hw, &req, NULL);
404 hns3_update_umv_space(hw, false);
408 hns3_err(hw, "UC MAC table full(%u)", pf->used_umv_size);
413 rte_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE, mac_addr);
415 /* check if we just hit the duplicate */
417 hns3_dbg(hw, "mac addr(%s) has been in the MAC table", mac_str);
421 hns3_err(hw, "PF failed to add unicast entry(%s) in the MAC table",
428 hns3_add_mac_addr(struct rte_eth_dev *dev, struct rte_ether_addr *mac_addr,
429 uint32_t idx, __attribute__ ((unused)) uint32_t pool)
431 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
432 char mac_str[RTE_ETHER_ADDR_FMT_SIZE];
435 rte_spinlock_lock(&hw->lock);
436 ret = hns3_add_uc_addr_common(hw, mac_addr);
438 rte_spinlock_unlock(&hw->lock);
439 rte_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
441 hns3_err(hw, "Failed to add mac addr(%s): %d", mac_str, ret);
446 hw->mac.default_addr_setted = true;
447 rte_spinlock_unlock(&hw->lock);
453 hns3_remove_uc_addr_common(struct hns3_hw *hw, struct rte_ether_addr *mac_addr)
455 struct hns3_mac_vlan_tbl_entry_cmd req;
456 char mac_str[RTE_ETHER_ADDR_FMT_SIZE];
459 /* check if mac addr is valid */
460 if (!rte_is_valid_assigned_ether_addr(mac_addr)) {
461 rte_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
463 hns3_err(hw, "Remove unicast mac addr err! addr(%s) invalid",
468 memset(&req, 0, sizeof(req));
469 hns3_set_bit(req.entry_type, HNS3_MAC_VLAN_BIT0_EN_B, 0);
470 hns3_prepare_mac_addr(&req, mac_addr->addr_bytes, false);
471 ret = hns3_remove_mac_vlan_tbl(hw, &req);
472 if (ret == -ENOENT) /* mac addr isn't existent in the mac vlan table. */
475 hns3_update_umv_space(hw, true);
481 hns3_remove_mac_addr(struct rte_eth_dev *dev, uint32_t idx)
483 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
484 /* index will be checked by upper level rte interface */
485 struct rte_ether_addr *mac_addr = &dev->data->mac_addrs[idx];
486 char mac_str[RTE_ETHER_ADDR_FMT_SIZE];
489 rte_spinlock_lock(&hw->lock);
490 ret = hns3_remove_uc_addr_common(hw, mac_addr);
492 rte_spinlock_unlock(&hw->lock);
493 rte_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
495 hns3_err(hw, "Failed to remove mac addr(%s): %d", mac_str, ret);
500 hw->mac.default_addr_setted = false;
501 rte_spinlock_unlock(&hw->lock);
505 hns3_set_default_mac_addr(struct rte_eth_dev *dev,
506 struct rte_ether_addr *mac_addr)
508 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
509 struct rte_ether_addr *oaddr;
510 char mac_str[RTE_ETHER_ADDR_FMT_SIZE];
511 bool default_addr_setted;
512 bool rm_succes = false;
515 /* check if mac addr is valid */
516 if (!rte_is_valid_assigned_ether_addr(mac_addr)) {
517 rte_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
519 hns3_err(hw, "Failed to set mac addr, addr(%s) invalid",
524 oaddr = (struct rte_ether_addr *)hw->mac.mac_addr;
525 default_addr_setted = hw->mac.default_addr_setted;
526 if (default_addr_setted && !!rte_is_same_ether_addr(mac_addr, oaddr))
529 rte_spinlock_lock(&hw->lock);
530 if (default_addr_setted) {
531 ret = hns3_remove_uc_addr_common(hw, oaddr);
533 rte_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
535 hns3_warn(hw, "Remove old uc mac address(%s) fail: %d",
542 ret = hns3_add_uc_addr_common(hw, mac_addr);
544 rte_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
546 hns3_err(hw, "Failed to set mac addr(%s): %d", mac_str, ret);
547 goto err_add_uc_addr;
550 rte_ether_addr_copy(mac_addr,
551 (struct rte_ether_addr *)hw->mac.mac_addr);
552 hw->mac.default_addr_setted = true;
553 rte_spinlock_unlock(&hw->lock);
559 ret_val = hns3_add_uc_addr_common(hw, oaddr);
561 rte_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
564 "Failed to restore old uc mac addr(%s): %d",
566 hw->mac.default_addr_setted = false;
569 rte_spinlock_unlock(&hw->lock);
575 hns3_update_desc_vfid(struct hns3_cmd_desc *desc, uint8_t vfid, bool clr)
577 #define HNS3_VF_NUM_IN_FIRST_DESC 192
581 if (vfid < HNS3_VF_NUM_IN_FIRST_DESC) {
582 word_num = vfid / 32;
585 desc[1].data[word_num] &=
586 rte_cpu_to_le_32(~(1UL << bit_num));
588 desc[1].data[word_num] |=
589 rte_cpu_to_le_32(1UL << bit_num);
591 word_num = (vfid - HNS3_VF_NUM_IN_FIRST_DESC) / 32;
594 desc[2].data[word_num] &=
595 rte_cpu_to_le_32(~(1UL << bit_num));
597 desc[2].data[word_num] |=
598 rte_cpu_to_le_32(1UL << bit_num);
603 hns3_add_mc_addr(struct hns3_hw *hw, struct rte_ether_addr *mac_addr)
605 struct hns3_mac_vlan_tbl_entry_cmd req;
606 struct hns3_cmd_desc desc[3];
607 char mac_str[RTE_ETHER_ADDR_FMT_SIZE];
611 /* Check if mac addr is valid */
612 if (!rte_is_multicast_ether_addr(mac_addr)) {
613 rte_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
615 hns3_err(hw, "Failed to add mc mac addr, addr(%s) invalid",
620 memset(&req, 0, sizeof(req));
621 hns3_set_bit(req.entry_type, HNS3_MAC_VLAN_BIT0_EN_B, 0);
622 hns3_prepare_mac_addr(&req, mac_addr->addr_bytes, true);
623 ret = hns3_lookup_mac_vlan_tbl(hw, &req, desc, true);
625 /* This mac addr do not exist, add new entry for it */
626 memset(desc[0].data, 0, sizeof(desc[0].data));
627 memset(desc[1].data, 0, sizeof(desc[0].data));
628 memset(desc[2].data, 0, sizeof(desc[0].data));
632 * In current version VF is not supported when PF is driven by DPDK
633 * driver, the PF-related vf_id is 0, just need to configure parameters
637 hns3_update_desc_vfid(desc, vf_id, false);
638 ret = hns3_add_mac_vlan_tbl(hw, &req, desc);
641 hns3_err(hw, "mc mac vlan table is full");
642 rte_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
644 hns3_err(hw, "Failed to add mc mac addr(%s): %d", mac_str, ret);
651 hns3_remove_mc_addr(struct hns3_hw *hw, struct rte_ether_addr *mac_addr)
653 struct hns3_mac_vlan_tbl_entry_cmd req;
654 struct hns3_cmd_desc desc[3];
655 char mac_str[RTE_ETHER_ADDR_FMT_SIZE];
659 /* Check if mac addr is valid */
660 if (!rte_is_multicast_ether_addr(mac_addr)) {
661 rte_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
663 hns3_err(hw, "Failed to rm mc mac addr, addr(%s) invalid",
668 memset(&req, 0, sizeof(req));
669 hns3_set_bit(req.entry_type, HNS3_MAC_VLAN_BIT0_EN_B, 0);
670 hns3_prepare_mac_addr(&req, mac_addr->addr_bytes, true);
671 ret = hns3_lookup_mac_vlan_tbl(hw, &req, desc, true);
674 * This mac addr exist, remove this handle's VFID for it.
675 * In current version VF is not supported when PF is driven by
676 * DPDK driver, the PF-related vf_id is 0, just need to
677 * configure parameters for vf_id 0.
680 hns3_update_desc_vfid(desc, vf_id, true);
682 /* All the vfid is zero, so need to delete this entry */
683 ret = hns3_remove_mac_vlan_tbl(hw, &req);
684 } else if (ret == -ENOENT) {
685 /* This mac addr doesn't exist. */
690 rte_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
692 hns3_err(hw, "Failed to rm mc mac addr(%s): %d", mac_str, ret);
699 hns3_set_mc_addr_chk_param(struct hns3_hw *hw,
700 struct rte_ether_addr *mc_addr_set,
703 char mac_str[RTE_ETHER_ADDR_FMT_SIZE];
704 struct rte_ether_addr *addr;
708 if (nb_mc_addr > HNS3_MC_MACADDR_NUM) {
709 hns3_err(hw, "Failed to set mc mac addr, nb_mc_addr(%d) "
710 "invalid. valid range: 0~%d",
711 nb_mc_addr, HNS3_MC_MACADDR_NUM);
715 /* Check if input mac addresses are valid */
716 for (i = 0; i < nb_mc_addr; i++) {
717 addr = &mc_addr_set[i];
718 if (!rte_is_multicast_ether_addr(addr)) {
719 rte_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
722 "Failed to set mc mac addr, addr(%s) invalid.",
727 /* Check if there are duplicate addresses */
728 for (j = i + 1; j < nb_mc_addr; j++) {
729 if (rte_is_same_ether_addr(addr, &mc_addr_set[j])) {
730 rte_ether_format_addr(mac_str,
731 RTE_ETHER_ADDR_FMT_SIZE,
733 hns3_err(hw, "Failed to set mc mac addr, "
734 "addrs invalid. two same addrs(%s).",
745 hns3_set_mc_addr_calc_addr(struct hns3_hw *hw,
746 struct rte_ether_addr *mc_addr_set,
748 struct rte_ether_addr *reserved_addr_list,
749 int *reserved_addr_num,
750 struct rte_ether_addr *add_addr_list,
752 struct rte_ether_addr *rm_addr_list,
755 struct rte_ether_addr *addr;
756 int current_addr_num;
757 int reserved_num = 0;
765 /* Calculate the mc mac address list that should be removed */
766 current_addr_num = hw->mc_addrs_num;
767 for (i = 0; i < current_addr_num; i++) {
768 addr = &hw->mc_addrs[i];
770 for (j = 0; j < mc_addr_num; j++) {
771 if (rte_is_same_ether_addr(addr, &mc_addr_set[j])) {
778 rte_ether_addr_copy(addr, &rm_addr_list[rm_num]);
781 rte_ether_addr_copy(addr,
782 &reserved_addr_list[reserved_num]);
787 /* Calculate the mc mac address list that should be added */
788 for (i = 0; i < mc_addr_num; i++) {
789 addr = &mc_addr_set[i];
791 for (j = 0; j < current_addr_num; j++) {
792 if (rte_is_same_ether_addr(addr, &hw->mc_addrs[j])) {
799 rte_ether_addr_copy(addr, &add_addr_list[add_num]);
804 /* Reorder the mc mac address list maintained by driver */
805 for (i = 0; i < reserved_num; i++)
806 rte_ether_addr_copy(&reserved_addr_list[i], &hw->mc_addrs[i]);
808 for (i = 0; i < rm_num; i++) {
809 num = reserved_num + i;
810 rte_ether_addr_copy(&rm_addr_list[i], &hw->mc_addrs[num]);
813 *reserved_addr_num = reserved_num;
814 *add_addr_num = add_num;
815 *rm_addr_num = rm_num;
819 hns3_set_mc_mac_addr_list(struct rte_eth_dev *dev,
820 struct rte_ether_addr *mc_addr_set,
823 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
824 struct rte_ether_addr reserved_addr_list[HNS3_MC_MACADDR_NUM];
825 struct rte_ether_addr add_addr_list[HNS3_MC_MACADDR_NUM];
826 struct rte_ether_addr rm_addr_list[HNS3_MC_MACADDR_NUM];
827 struct rte_ether_addr *addr;
828 int reserved_addr_num;
836 /* Check if input parameters are valid */
837 ret = hns3_set_mc_addr_chk_param(hw, mc_addr_set, nb_mc_addr);
841 rte_spinlock_lock(&hw->lock);
844 * Calculate the mc mac address lists those should be removed and be
845 * added, Reorder the mc mac address list maintained by driver.
847 mc_addr_num = (int)nb_mc_addr;
848 hns3_set_mc_addr_calc_addr(hw, mc_addr_set, mc_addr_num,
849 reserved_addr_list, &reserved_addr_num,
850 add_addr_list, &add_addr_num,
851 rm_addr_list, &rm_addr_num);
853 /* Remove mc mac addresses */
854 for (i = 0; i < rm_addr_num; i++) {
855 num = rm_addr_num - i - 1;
856 addr = &rm_addr_list[num];
857 ret = hns3_remove_mc_addr(hw, addr);
859 rte_spinlock_unlock(&hw->lock);
865 /* Add mc mac addresses */
866 for (i = 0; i < add_addr_num; i++) {
867 addr = &add_addr_list[i];
868 ret = hns3_add_mc_addr(hw, addr);
870 rte_spinlock_unlock(&hw->lock);
874 num = reserved_addr_num + i;
875 rte_ether_addr_copy(addr, &hw->mc_addrs[num]);
878 rte_spinlock_unlock(&hw->lock);
884 hns3_configure_all_mc_mac_addr(struct hns3_adapter *hns, bool del)
886 char mac_str[RTE_ETHER_ADDR_FMT_SIZE];
887 struct hns3_hw *hw = &hns->hw;
888 struct rte_ether_addr *addr;
893 for (i = 0; i < hw->mc_addrs_num; i++) {
894 addr = &hw->mc_addrs[i];
895 if (!rte_is_multicast_ether_addr(addr))
898 ret = hns3_remove_mc_addr(hw, addr);
900 ret = hns3_add_mc_addr(hw, addr);
903 rte_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
905 hns3_dbg(hw, "%s mc mac addr: %s failed",
906 del ? "Remove" : "Restore", mac_str);
913 hns3_set_mac_mtu(struct hns3_hw *hw, uint16_t new_mps)
915 struct hns3_config_max_frm_size_cmd *req;
916 struct hns3_cmd_desc desc;
918 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_CONFIG_MAX_FRM_SIZE, false);
920 req = (struct hns3_config_max_frm_size_cmd *)desc.data;
921 req->max_frm_size = rte_cpu_to_le_16(new_mps);
922 req->min_frm_size = HNS3_MIN_FRAME_LEN;
924 return hns3_cmd_send(hw, &desc, 1);
928 hns3_config_mtu(struct hns3_hw *hw, uint16_t mps)
932 ret = hns3_set_mac_mtu(hw, mps);
934 hns3_err(hw, "Failed to set mtu, ret = %d", ret);
938 ret = hns3_buffer_alloc(hw);
940 hns3_err(hw, "Failed to allocate buffer, ret = %d", ret);
948 hns3_parse_func_status(struct hns3_hw *hw, struct hns3_func_status_cmd *status)
950 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
951 struct hns3_pf *pf = &hns->pf;
953 if (!(status->pf_state & HNS3_PF_STATE_DONE))
956 pf->is_main_pf = (status->pf_state & HNS3_PF_STATE_MAIN) ? true : false;
962 hns3_query_function_status(struct hns3_hw *hw)
964 #define HNS3_QUERY_MAX_CNT 10
965 #define HNS3_QUERY_SLEEP_MSCOEND 1
966 struct hns3_func_status_cmd *req;
967 struct hns3_cmd_desc desc;
971 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_QUERY_FUNC_STATUS, true);
972 req = (struct hns3_func_status_cmd *)desc.data;
975 ret = hns3_cmd_send(hw, &desc, 1);
977 PMD_INIT_LOG(ERR, "query function status failed %d",
982 /* Check pf reset is done */
986 rte_delay_ms(HNS3_QUERY_SLEEP_MSCOEND);
987 } while (timeout++ < HNS3_QUERY_MAX_CNT);
989 return hns3_parse_func_status(hw, req);
993 hns3_query_pf_resource(struct hns3_hw *hw)
995 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
996 struct hns3_pf *pf = &hns->pf;
997 struct hns3_pf_res_cmd *req;
998 struct hns3_cmd_desc desc;
1001 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_QUERY_PF_RSRC, true);
1002 ret = hns3_cmd_send(hw, &desc, 1);
1004 PMD_INIT_LOG(ERR, "query pf resource failed %d", ret);
1008 req = (struct hns3_pf_res_cmd *)desc.data;
1009 hw->total_tqps_num = rte_le_to_cpu_16(req->tqp_num);
1010 pf->pkt_buf_size = rte_le_to_cpu_16(req->buf_size) << HNS3_BUF_UNIT_S;
1011 hw->tqps_num = RTE_MIN(hw->total_tqps_num, HNS3_MAX_TQP_NUM_PER_FUNC);
1013 if (req->tx_buf_size)
1015 rte_le_to_cpu_16(req->tx_buf_size) << HNS3_BUF_UNIT_S;
1017 pf->tx_buf_size = HNS3_DEFAULT_TX_BUF;
1019 pf->tx_buf_size = roundup(pf->tx_buf_size, HNS3_BUF_SIZE_UNIT);
1021 if (req->dv_buf_size)
1023 rte_le_to_cpu_16(req->dv_buf_size) << HNS3_BUF_UNIT_S;
1025 pf->dv_buf_size = HNS3_DEFAULT_DV;
1027 pf->dv_buf_size = roundup(pf->dv_buf_size, HNS3_BUF_SIZE_UNIT);
1030 hns3_get_field(rte_le_to_cpu_16(req->pf_intr_vector_number),
1031 HNS3_PF_VEC_NUM_M, HNS3_PF_VEC_NUM_S);
1037 hns3_parse_cfg(struct hns3_cfg *cfg, struct hns3_cmd_desc *desc)
1039 struct hns3_cfg_param_cmd *req;
1040 uint64_t mac_addr_tmp_high;
1041 uint64_t mac_addr_tmp;
1044 req = (struct hns3_cfg_param_cmd *)desc[0].data;
1046 /* get the configuration */
1047 cfg->vmdq_vport_num = hns3_get_field(rte_le_to_cpu_32(req->param[0]),
1048 HNS3_CFG_VMDQ_M, HNS3_CFG_VMDQ_S);
1049 cfg->tc_num = hns3_get_field(rte_le_to_cpu_32(req->param[0]),
1050 HNS3_CFG_TC_NUM_M, HNS3_CFG_TC_NUM_S);
1051 cfg->tqp_desc_num = hns3_get_field(rte_le_to_cpu_32(req->param[0]),
1052 HNS3_CFG_TQP_DESC_N_M,
1053 HNS3_CFG_TQP_DESC_N_S);
1055 cfg->phy_addr = hns3_get_field(rte_le_to_cpu_32(req->param[1]),
1056 HNS3_CFG_PHY_ADDR_M,
1057 HNS3_CFG_PHY_ADDR_S);
1058 cfg->media_type = hns3_get_field(rte_le_to_cpu_32(req->param[1]),
1059 HNS3_CFG_MEDIA_TP_M,
1060 HNS3_CFG_MEDIA_TP_S);
1061 cfg->rx_buf_len = hns3_get_field(rte_le_to_cpu_32(req->param[1]),
1062 HNS3_CFG_RX_BUF_LEN_M,
1063 HNS3_CFG_RX_BUF_LEN_S);
1064 /* get mac address */
1065 mac_addr_tmp = rte_le_to_cpu_32(req->param[2]);
1066 mac_addr_tmp_high = hns3_get_field(rte_le_to_cpu_32(req->param[3]),
1067 HNS3_CFG_MAC_ADDR_H_M,
1068 HNS3_CFG_MAC_ADDR_H_S);
1070 mac_addr_tmp |= (mac_addr_tmp_high << 31) << 1;
1072 cfg->default_speed = hns3_get_field(rte_le_to_cpu_32(req->param[3]),
1073 HNS3_CFG_DEFAULT_SPEED_M,
1074 HNS3_CFG_DEFAULT_SPEED_S);
1075 cfg->rss_size_max = hns3_get_field(rte_le_to_cpu_32(req->param[3]),
1076 HNS3_CFG_RSS_SIZE_M,
1077 HNS3_CFG_RSS_SIZE_S);
1079 for (i = 0; i < RTE_ETHER_ADDR_LEN; i++)
1080 cfg->mac_addr[i] = (mac_addr_tmp >> (8 * i)) & 0xff;
1082 req = (struct hns3_cfg_param_cmd *)desc[1].data;
1083 cfg->numa_node_map = rte_le_to_cpu_32(req->param[0]);
1085 cfg->speed_ability = hns3_get_field(rte_le_to_cpu_32(req->param[1]),
1086 HNS3_CFG_SPEED_ABILITY_M,
1087 HNS3_CFG_SPEED_ABILITY_S);
1088 cfg->umv_space = hns3_get_field(rte_le_to_cpu_32(req->param[1]),
1089 HNS3_CFG_UMV_TBL_SPACE_M,
1090 HNS3_CFG_UMV_TBL_SPACE_S);
1091 if (!cfg->umv_space)
1092 cfg->umv_space = HNS3_DEFAULT_UMV_SPACE_PER_PF;
1095 /* hns3_get_board_cfg: query the static parameter from NCL_config file in flash
1096 * @hw: pointer to struct hns3_hw
1097 * @hcfg: the config structure to be getted
1100 hns3_get_board_cfg(struct hns3_hw *hw, struct hns3_cfg *hcfg)
1102 struct hns3_cmd_desc desc[HNS3_PF_CFG_DESC_NUM];
1103 struct hns3_cfg_param_cmd *req;
1108 for (i = 0; i < HNS3_PF_CFG_DESC_NUM; i++) {
1110 req = (struct hns3_cfg_param_cmd *)desc[i].data;
1111 hns3_cmd_setup_basic_desc(&desc[i], HNS3_OPC_GET_CFG_PARAM,
1113 hns3_set_field(offset, HNS3_CFG_OFFSET_M, HNS3_CFG_OFFSET_S,
1114 i * HNS3_CFG_RD_LEN_BYTES);
1115 /* Len should be divided by 4 when send to hardware */
1116 hns3_set_field(offset, HNS3_CFG_RD_LEN_M, HNS3_CFG_RD_LEN_S,
1117 HNS3_CFG_RD_LEN_BYTES / HNS3_CFG_RD_LEN_UNIT);
1118 req->offset = rte_cpu_to_le_32(offset);
1121 ret = hns3_cmd_send(hw, desc, HNS3_PF_CFG_DESC_NUM);
1123 PMD_INIT_LOG(ERR, "get config failed %d.", ret);
1127 hns3_parse_cfg(hcfg, desc);
1133 hns3_parse_speed(int speed_cmd, uint32_t *speed)
1135 switch (speed_cmd) {
1136 case HNS3_CFG_SPEED_10M:
1137 *speed = ETH_SPEED_NUM_10M;
1139 case HNS3_CFG_SPEED_100M:
1140 *speed = ETH_SPEED_NUM_100M;
1142 case HNS3_CFG_SPEED_1G:
1143 *speed = ETH_SPEED_NUM_1G;
1145 case HNS3_CFG_SPEED_10G:
1146 *speed = ETH_SPEED_NUM_10G;
1148 case HNS3_CFG_SPEED_25G:
1149 *speed = ETH_SPEED_NUM_25G;
1151 case HNS3_CFG_SPEED_40G:
1152 *speed = ETH_SPEED_NUM_40G;
1154 case HNS3_CFG_SPEED_50G:
1155 *speed = ETH_SPEED_NUM_50G;
1157 case HNS3_CFG_SPEED_100G:
1158 *speed = ETH_SPEED_NUM_100G;
1168 hns3_get_board_configuration(struct hns3_hw *hw)
1170 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
1171 struct hns3_pf *pf = &hns->pf;
1172 struct hns3_cfg cfg;
1175 ret = hns3_get_board_cfg(hw, &cfg);
1177 PMD_INIT_LOG(ERR, "get board config failed %d", ret);
1181 if (cfg.media_type == HNS3_MEDIA_TYPE_COPPER) {
1182 PMD_INIT_LOG(ERR, "media type is copper, not supported.");
1186 hw->mac.media_type = cfg.media_type;
1187 hw->rss_size_max = cfg.rss_size_max;
1188 hw->rx_buf_len = cfg.rx_buf_len;
1189 memcpy(hw->mac.mac_addr, cfg.mac_addr, RTE_ETHER_ADDR_LEN);
1190 hw->mac.phy_addr = cfg.phy_addr;
1191 hw->mac.default_addr_setted = false;
1192 hw->num_tx_desc = cfg.tqp_desc_num;
1193 hw->num_rx_desc = cfg.tqp_desc_num;
1194 hw->dcb_info.num_pg = 1;
1195 hw->dcb_info.hw_pfc_map = 0;
1197 ret = hns3_parse_speed(cfg.default_speed, &hw->mac.link_speed);
1199 PMD_INIT_LOG(ERR, "Get wrong speed %d, ret = %d",
1200 cfg.default_speed, ret);
1204 pf->tc_max = cfg.tc_num;
1205 if (pf->tc_max > HNS3_MAX_TC_NUM || pf->tc_max < 1) {
1206 PMD_INIT_LOG(WARNING,
1207 "Get TC num(%u) from flash, set TC num to 1",
1212 /* Dev does not support DCB */
1213 if (!hns3_dev_dcb_supported(hw)) {
1217 pf->pfc_max = pf->tc_max;
1219 hw->dcb_info.num_tc = 1;
1220 hw->alloc_rss_size = RTE_MIN(hw->rss_size_max,
1221 hw->tqps_num / hw->dcb_info.num_tc);
1222 hns3_set_bit(hw->hw_tc_map, 0, 1);
1223 pf->tx_sch_mode = HNS3_FLAG_TC_BASE_SCH_MODE;
1225 pf->wanted_umv_size = cfg.umv_space;
1231 hns3_get_configuration(struct hns3_hw *hw)
1235 ret = hns3_query_function_status(hw);
1237 PMD_INIT_LOG(ERR, "Failed to query function status: %d.", ret);
1241 /* Get pf resource */
1242 ret = hns3_query_pf_resource(hw);
1244 PMD_INIT_LOG(ERR, "Failed to query pf resource: %d", ret);
1248 ret = hns3_get_board_configuration(hw);
1250 PMD_INIT_LOG(ERR, "Failed to get board configuration: %d", ret);
1258 hns3_map_tqps_to_func(struct hns3_hw *hw, uint16_t func_id, uint16_t tqp_pid,
1259 uint16_t tqp_vid, bool is_pf)
1261 struct hns3_tqp_map_cmd *req;
1262 struct hns3_cmd_desc desc;
1265 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_SET_TQP_MAP, false);
1267 req = (struct hns3_tqp_map_cmd *)desc.data;
1268 req->tqp_id = rte_cpu_to_le_16(tqp_pid);
1269 req->tqp_vf = func_id;
1270 req->tqp_flag = 1 << HNS3_TQP_MAP_EN_B;
1272 req->tqp_flag |= (1 << HNS3_TQP_MAP_TYPE_B);
1273 req->tqp_vid = rte_cpu_to_le_16(tqp_vid);
1275 ret = hns3_cmd_send(hw, &desc, 1);
1277 PMD_INIT_LOG(ERR, "TQP map failed %d", ret);
1283 hns3_map_tqp(struct hns3_hw *hw)
1285 uint16_t tqps_num = hw->total_tqps_num;
1293 * In current version VF is not supported when PF is driven by DPDK
1294 * driver, so we allocate tqps to PF as much as possible.
1297 num = DIV_ROUND_UP(hw->total_tqps_num, HNS3_MAX_TQP_NUM_PER_FUNC);
1298 for (func_id = 0; func_id < num; func_id++) {
1300 i < HNS3_MAX_TQP_NUM_PER_FUNC && tqp_id < tqps_num; i++) {
1301 ret = hns3_map_tqps_to_func(hw, func_id, tqp_id++, i,
1312 hns3_cfg_mac_speed_dup_hw(struct hns3_hw *hw, uint32_t speed, uint8_t duplex)
1314 struct hns3_config_mac_speed_dup_cmd *req;
1315 struct hns3_cmd_desc desc;
1318 req = (struct hns3_config_mac_speed_dup_cmd *)desc.data;
1320 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_CONFIG_SPEED_DUP, false);
1322 hns3_set_bit(req->speed_dup, HNS3_CFG_DUPLEX_B, !!duplex ? 1 : 0);
1325 case ETH_SPEED_NUM_10M:
1326 hns3_set_field(req->speed_dup, HNS3_CFG_SPEED_M,
1327 HNS3_CFG_SPEED_S, HNS3_CFG_SPEED_10M);
1329 case ETH_SPEED_NUM_100M:
1330 hns3_set_field(req->speed_dup, HNS3_CFG_SPEED_M,
1331 HNS3_CFG_SPEED_S, HNS3_CFG_SPEED_100M);
1333 case ETH_SPEED_NUM_1G:
1334 hns3_set_field(req->speed_dup, HNS3_CFG_SPEED_M,
1335 HNS3_CFG_SPEED_S, HNS3_CFG_SPEED_1G);
1337 case ETH_SPEED_NUM_10G:
1338 hns3_set_field(req->speed_dup, HNS3_CFG_SPEED_M,
1339 HNS3_CFG_SPEED_S, HNS3_CFG_SPEED_10G);
1341 case ETH_SPEED_NUM_25G:
1342 hns3_set_field(req->speed_dup, HNS3_CFG_SPEED_M,
1343 HNS3_CFG_SPEED_S, HNS3_CFG_SPEED_25G);
1345 case ETH_SPEED_NUM_40G:
1346 hns3_set_field(req->speed_dup, HNS3_CFG_SPEED_M,
1347 HNS3_CFG_SPEED_S, HNS3_CFG_SPEED_40G);
1349 case ETH_SPEED_NUM_50G:
1350 hns3_set_field(req->speed_dup, HNS3_CFG_SPEED_M,
1351 HNS3_CFG_SPEED_S, HNS3_CFG_SPEED_50G);
1353 case ETH_SPEED_NUM_100G:
1354 hns3_set_field(req->speed_dup, HNS3_CFG_SPEED_M,
1355 HNS3_CFG_SPEED_S, HNS3_CFG_SPEED_100G);
1358 PMD_INIT_LOG(ERR, "invalid speed (%u)", speed);
1362 hns3_set_bit(req->mac_change_fec_en, HNS3_CFG_MAC_SPEED_CHANGE_EN_B, 1);
1364 ret = hns3_cmd_send(hw, &desc, 1);
1366 PMD_INIT_LOG(ERR, "mac speed/duplex config cmd failed %d", ret);
1372 hns3_tx_buffer_calc(struct hns3_hw *hw, struct hns3_pkt_buf_alloc *buf_alloc)
1374 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
1375 struct hns3_pf *pf = &hns->pf;
1376 struct hns3_priv_buf *priv;
1377 uint32_t i, total_size;
1379 total_size = pf->pkt_buf_size;
1381 /* alloc tx buffer for all enabled tc */
1382 for (i = 0; i < HNS3_MAX_TC_NUM; i++) {
1383 priv = &buf_alloc->priv_buf[i];
1385 if (hw->hw_tc_map & BIT(i)) {
1386 if (total_size < pf->tx_buf_size)
1389 priv->tx_buf_size = pf->tx_buf_size;
1391 priv->tx_buf_size = 0;
1393 total_size -= priv->tx_buf_size;
1400 hns3_tx_buffer_alloc(struct hns3_hw *hw, struct hns3_pkt_buf_alloc *buf_alloc)
1402 /* TX buffer size is unit by 128 byte */
1403 #define HNS3_BUF_SIZE_UNIT_SHIFT 7
1404 #define HNS3_BUF_SIZE_UPDATE_EN_MSK BIT(15)
1405 struct hns3_tx_buff_alloc_cmd *req;
1406 struct hns3_cmd_desc desc;
1411 req = (struct hns3_tx_buff_alloc_cmd *)desc.data;
1413 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_TX_BUFF_ALLOC, 0);
1414 for (i = 0; i < HNS3_MAX_TC_NUM; i++) {
1415 buf_size = buf_alloc->priv_buf[i].tx_buf_size;
1417 buf_size = buf_size >> HNS3_BUF_SIZE_UNIT_SHIFT;
1418 req->tx_pkt_buff[i] = rte_cpu_to_le_16(buf_size |
1419 HNS3_BUF_SIZE_UPDATE_EN_MSK);
1422 ret = hns3_cmd_send(hw, &desc, 1);
1424 PMD_INIT_LOG(ERR, "tx buffer alloc cmd failed %d", ret);
1430 hns3_get_tc_num(struct hns3_hw *hw)
1435 for (i = 0; i < HNS3_MAX_TC_NUM; i++)
1436 if (hw->hw_tc_map & BIT(i))
1442 hns3_get_rx_priv_buff_alloced(struct hns3_pkt_buf_alloc *buf_alloc)
1444 struct hns3_priv_buf *priv;
1445 uint32_t rx_priv = 0;
1448 for (i = 0; i < HNS3_MAX_TC_NUM; i++) {
1449 priv = &buf_alloc->priv_buf[i];
1451 rx_priv += priv->buf_size;
1457 hns3_get_tx_buff_alloced(struct hns3_pkt_buf_alloc *buf_alloc)
1459 uint32_t total_tx_size = 0;
1462 for (i = 0; i < HNS3_MAX_TC_NUM; i++)
1463 total_tx_size += buf_alloc->priv_buf[i].tx_buf_size;
1465 return total_tx_size;
1468 /* Get the number of pfc enabled TCs, which have private buffer */
1470 hns3_get_pfc_priv_num(struct hns3_hw *hw, struct hns3_pkt_buf_alloc *buf_alloc)
1472 struct hns3_priv_buf *priv;
1476 for (i = 0; i < HNS3_MAX_TC_NUM; i++) {
1477 priv = &buf_alloc->priv_buf[i];
1478 if ((hw->dcb_info.hw_pfc_map & BIT(i)) && priv->enable)
1485 /* Get the number of pfc disabled TCs, which have private buffer */
1487 hns3_get_no_pfc_priv_num(struct hns3_hw *hw,
1488 struct hns3_pkt_buf_alloc *buf_alloc)
1490 struct hns3_priv_buf *priv;
1494 for (i = 0; i < HNS3_MAX_TC_NUM; i++) {
1495 priv = &buf_alloc->priv_buf[i];
1496 if (hw->hw_tc_map & BIT(i) &&
1497 !(hw->dcb_info.hw_pfc_map & BIT(i)) && priv->enable)
1505 hns3_is_rx_buf_ok(struct hns3_hw *hw, struct hns3_pkt_buf_alloc *buf_alloc,
1508 uint32_t shared_buf_min, shared_buf_tc, shared_std, hi_thrd, lo_thrd;
1509 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
1510 struct hns3_pf *pf = &hns->pf;
1511 uint32_t shared_buf, aligned_mps;
1516 tc_num = hns3_get_tc_num(hw);
1517 aligned_mps = roundup(pf->mps, HNS3_BUF_SIZE_UNIT);
1519 if (hns3_dev_dcb_supported(hw))
1520 shared_buf_min = HNS3_BUF_MUL_BY * aligned_mps +
1523 shared_buf_min = aligned_mps + HNS3_NON_DCB_ADDITIONAL_BUF
1526 shared_buf_tc = tc_num * aligned_mps + aligned_mps;
1527 shared_std = roundup(max_t(uint32_t, shared_buf_min, shared_buf_tc),
1528 HNS3_BUF_SIZE_UNIT);
1530 rx_priv = hns3_get_rx_priv_buff_alloced(buf_alloc);
1531 if (rx_all < rx_priv + shared_std)
1534 shared_buf = rounddown(rx_all - rx_priv, HNS3_BUF_SIZE_UNIT);
1535 buf_alloc->s_buf.buf_size = shared_buf;
1536 if (hns3_dev_dcb_supported(hw)) {
1537 buf_alloc->s_buf.self.high = shared_buf - pf->dv_buf_size;
1538 buf_alloc->s_buf.self.low = buf_alloc->s_buf.self.high
1539 - roundup(aligned_mps / HNS3_BUF_DIV_BY,
1540 HNS3_BUF_SIZE_UNIT);
1542 buf_alloc->s_buf.self.high =
1543 aligned_mps + HNS3_NON_DCB_ADDITIONAL_BUF;
1544 buf_alloc->s_buf.self.low = aligned_mps;
1547 if (hns3_dev_dcb_supported(hw)) {
1548 hi_thrd = shared_buf - pf->dv_buf_size;
1550 if (tc_num <= NEED_RESERVE_TC_NUM)
1551 hi_thrd = hi_thrd * BUF_RESERVE_PERCENT
1555 hi_thrd = hi_thrd / tc_num;
1557 hi_thrd = max_t(uint32_t, hi_thrd,
1558 HNS3_BUF_MUL_BY * aligned_mps);
1559 hi_thrd = rounddown(hi_thrd, HNS3_BUF_SIZE_UNIT);
1560 lo_thrd = hi_thrd - aligned_mps / HNS3_BUF_DIV_BY;
1562 hi_thrd = aligned_mps + HNS3_NON_DCB_ADDITIONAL_BUF;
1563 lo_thrd = aligned_mps;
1566 for (i = 0; i < HNS3_MAX_TC_NUM; i++) {
1567 buf_alloc->s_buf.tc_thrd[i].low = lo_thrd;
1568 buf_alloc->s_buf.tc_thrd[i].high = hi_thrd;
1575 hns3_rx_buf_calc_all(struct hns3_hw *hw, bool max,
1576 struct hns3_pkt_buf_alloc *buf_alloc)
1578 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
1579 struct hns3_pf *pf = &hns->pf;
1580 struct hns3_priv_buf *priv;
1581 uint32_t aligned_mps;
1585 rx_all = pf->pkt_buf_size - hns3_get_tx_buff_alloced(buf_alloc);
1586 aligned_mps = roundup(pf->mps, HNS3_BUF_SIZE_UNIT);
1588 for (i = 0; i < HNS3_MAX_TC_NUM; i++) {
1589 priv = &buf_alloc->priv_buf[i];
1596 if (!(hw->hw_tc_map & BIT(i)))
1600 if (hw->dcb_info.hw_pfc_map & BIT(i)) {
1601 priv->wl.low = max ? aligned_mps : HNS3_BUF_SIZE_UNIT;
1602 priv->wl.high = roundup(priv->wl.low + aligned_mps,
1603 HNS3_BUF_SIZE_UNIT);
1606 priv->wl.high = max ? (aligned_mps * HNS3_BUF_MUL_BY) :
1610 priv->buf_size = priv->wl.high + pf->dv_buf_size;
1613 return hns3_is_rx_buf_ok(hw, buf_alloc, rx_all);
1617 hns3_drop_nopfc_buf_till_fit(struct hns3_hw *hw,
1618 struct hns3_pkt_buf_alloc *buf_alloc)
1620 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
1621 struct hns3_pf *pf = &hns->pf;
1622 struct hns3_priv_buf *priv;
1623 int no_pfc_priv_num;
1628 rx_all = pf->pkt_buf_size - hns3_get_tx_buff_alloced(buf_alloc);
1629 no_pfc_priv_num = hns3_get_no_pfc_priv_num(hw, buf_alloc);
1631 /* let the last to be cleared first */
1632 for (i = HNS3_MAX_TC_NUM - 1; i >= 0; i--) {
1633 priv = &buf_alloc->priv_buf[i];
1634 mask = BIT((uint8_t)i);
1636 if (hw->hw_tc_map & mask &&
1637 !(hw->dcb_info.hw_pfc_map & mask)) {
1638 /* Clear the no pfc TC private buffer */
1646 if (hns3_is_rx_buf_ok(hw, buf_alloc, rx_all) ||
1647 no_pfc_priv_num == 0)
1651 return hns3_is_rx_buf_ok(hw, buf_alloc, rx_all);
1655 hns3_drop_pfc_buf_till_fit(struct hns3_hw *hw,
1656 struct hns3_pkt_buf_alloc *buf_alloc)
1658 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
1659 struct hns3_pf *pf = &hns->pf;
1660 struct hns3_priv_buf *priv;
1666 rx_all = pf->pkt_buf_size - hns3_get_tx_buff_alloced(buf_alloc);
1667 pfc_priv_num = hns3_get_pfc_priv_num(hw, buf_alloc);
1669 /* let the last to be cleared first */
1670 for (i = HNS3_MAX_TC_NUM - 1; i >= 0; i--) {
1671 priv = &buf_alloc->priv_buf[i];
1672 mask = BIT((uint8_t)i);
1674 if (hw->hw_tc_map & mask &&
1675 hw->dcb_info.hw_pfc_map & mask) {
1676 /* Reduce the number of pfc TC with private buffer */
1683 if (hns3_is_rx_buf_ok(hw, buf_alloc, rx_all) ||
1688 return hns3_is_rx_buf_ok(hw, buf_alloc, rx_all);
1692 hns3_only_alloc_priv_buff(struct hns3_hw *hw,
1693 struct hns3_pkt_buf_alloc *buf_alloc)
1695 #define COMPENSATE_BUFFER 0x3C00
1696 #define COMPENSATE_HALF_MPS_NUM 5
1697 #define PRIV_WL_GAP 0x1800
1698 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
1699 struct hns3_pf *pf = &hns->pf;
1700 uint32_t tc_num = hns3_get_tc_num(hw);
1701 uint32_t half_mps = pf->mps >> 1;
1702 struct hns3_priv_buf *priv;
1703 uint32_t min_rx_priv;
1707 rx_priv = pf->pkt_buf_size - hns3_get_tx_buff_alloced(buf_alloc);
1709 rx_priv = rx_priv / tc_num;
1711 if (tc_num <= NEED_RESERVE_TC_NUM)
1712 rx_priv = rx_priv * BUF_RESERVE_PERCENT / BUF_MAX_PERCENT;
1715 * Minimum value of private buffer in rx direction (min_rx_priv) is
1716 * equal to "DV + 2.5 * MPS + 15KB". Driver only allocates rx private
1717 * buffer if rx_priv is greater than min_rx_priv.
1719 min_rx_priv = pf->dv_buf_size + COMPENSATE_BUFFER +
1720 COMPENSATE_HALF_MPS_NUM * half_mps;
1721 min_rx_priv = roundup(min_rx_priv, HNS3_BUF_SIZE_UNIT);
1722 rx_priv = rounddown(rx_priv, HNS3_BUF_SIZE_UNIT);
1724 if (rx_priv < min_rx_priv)
1727 for (i = 0; i < HNS3_MAX_TC_NUM; i++) {
1728 priv = &buf_alloc->priv_buf[i];
1735 if (!(hw->hw_tc_map & BIT(i)))
1739 priv->buf_size = rx_priv;
1740 priv->wl.high = rx_priv - pf->dv_buf_size;
1741 priv->wl.low = priv->wl.high - PRIV_WL_GAP;
1744 buf_alloc->s_buf.buf_size = 0;
1750 * hns3_rx_buffer_calc: calculate the rx private buffer size for all TCs
1751 * @hw: pointer to struct hns3_hw
1752 * @buf_alloc: pointer to buffer calculation data
1753 * @return: 0: calculate sucessful, negative: fail
1756 hns3_rx_buffer_calc(struct hns3_hw *hw, struct hns3_pkt_buf_alloc *buf_alloc)
1758 /* When DCB is not supported, rx private buffer is not allocated. */
1759 if (!hns3_dev_dcb_supported(hw)) {
1760 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
1761 struct hns3_pf *pf = &hns->pf;
1762 uint32_t rx_all = pf->pkt_buf_size;
1764 rx_all -= hns3_get_tx_buff_alloced(buf_alloc);
1765 if (!hns3_is_rx_buf_ok(hw, buf_alloc, rx_all))
1772 * Try to allocate privated packet buffer for all TCs without share
1775 if (hns3_only_alloc_priv_buff(hw, buf_alloc))
1779 * Try to allocate privated packet buffer for all TCs with share
1782 if (hns3_rx_buf_calc_all(hw, true, buf_alloc))
1786 * For different application scenes, the enabled port number, TC number
1787 * and no_drop TC number are different. In order to obtain the better
1788 * performance, software could allocate the buffer size and configure
1789 * the waterline by tring to decrease the private buffer size according
1790 * to the order, namely, waterline of valided tc, pfc disabled tc, pfc
1793 if (hns3_rx_buf_calc_all(hw, false, buf_alloc))
1796 if (hns3_drop_nopfc_buf_till_fit(hw, buf_alloc))
1799 if (hns3_drop_pfc_buf_till_fit(hw, buf_alloc))
1806 hns3_rx_priv_buf_alloc(struct hns3_hw *hw, struct hns3_pkt_buf_alloc *buf_alloc)
1808 struct hns3_rx_priv_buff_cmd *req;
1809 struct hns3_cmd_desc desc;
1814 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_RX_PRIV_BUFF_ALLOC, false);
1815 req = (struct hns3_rx_priv_buff_cmd *)desc.data;
1817 /* Alloc private buffer TCs */
1818 for (i = 0; i < HNS3_MAX_TC_NUM; i++) {
1819 struct hns3_priv_buf *priv = &buf_alloc->priv_buf[i];
1822 rte_cpu_to_le_16(priv->buf_size >> HNS3_BUF_UNIT_S);
1823 req->buf_num[i] |= rte_cpu_to_le_16(1 << HNS3_TC0_PRI_BUF_EN_B);
1826 buf_size = buf_alloc->s_buf.buf_size;
1827 req->shared_buf = rte_cpu_to_le_16((buf_size >> HNS3_BUF_UNIT_S) |
1828 (1 << HNS3_TC0_PRI_BUF_EN_B));
1830 ret = hns3_cmd_send(hw, &desc, 1);
1832 PMD_INIT_LOG(ERR, "rx private buffer alloc cmd failed %d", ret);
1838 hns3_rx_priv_wl_config(struct hns3_hw *hw, struct hns3_pkt_buf_alloc *buf_alloc)
1840 #define HNS3_RX_PRIV_WL_ALLOC_DESC_NUM 2
1841 struct hns3_rx_priv_wl_buf *req;
1842 struct hns3_priv_buf *priv;
1843 struct hns3_cmd_desc desc[HNS3_RX_PRIV_WL_ALLOC_DESC_NUM];
1847 for (i = 0; i < HNS3_RX_PRIV_WL_ALLOC_DESC_NUM; i++) {
1848 hns3_cmd_setup_basic_desc(&desc[i], HNS3_OPC_RX_PRIV_WL_ALLOC,
1850 req = (struct hns3_rx_priv_wl_buf *)desc[i].data;
1852 /* The first descriptor set the NEXT bit to 1 */
1854 desc[i].flag |= rte_cpu_to_le_16(HNS3_CMD_FLAG_NEXT);
1856 desc[i].flag &= ~rte_cpu_to_le_16(HNS3_CMD_FLAG_NEXT);
1858 for (j = 0; j < HNS3_TC_NUM_ONE_DESC; j++) {
1859 uint32_t idx = i * HNS3_TC_NUM_ONE_DESC + j;
1861 priv = &buf_alloc->priv_buf[idx];
1862 req->tc_wl[j].high = rte_cpu_to_le_16(priv->wl.high >>
1864 req->tc_wl[j].high |=
1865 rte_cpu_to_le_16(BIT(HNS3_RX_PRIV_EN_B));
1866 req->tc_wl[j].low = rte_cpu_to_le_16(priv->wl.low >>
1868 req->tc_wl[j].low |=
1869 rte_cpu_to_le_16(BIT(HNS3_RX_PRIV_EN_B));
1873 /* Send 2 descriptor at one time */
1874 ret = hns3_cmd_send(hw, desc, HNS3_RX_PRIV_WL_ALLOC_DESC_NUM);
1876 PMD_INIT_LOG(ERR, "rx private waterline config cmd failed %d",
1882 hns3_common_thrd_config(struct hns3_hw *hw,
1883 struct hns3_pkt_buf_alloc *buf_alloc)
1885 #define HNS3_RX_COM_THRD_ALLOC_DESC_NUM 2
1886 struct hns3_shared_buf *s_buf = &buf_alloc->s_buf;
1887 struct hns3_rx_com_thrd *req;
1888 struct hns3_cmd_desc desc[HNS3_RX_COM_THRD_ALLOC_DESC_NUM];
1889 struct hns3_tc_thrd *tc;
1894 for (i = 0; i < HNS3_RX_COM_THRD_ALLOC_DESC_NUM; i++) {
1895 hns3_cmd_setup_basic_desc(&desc[i], HNS3_OPC_RX_COM_THRD_ALLOC,
1897 req = (struct hns3_rx_com_thrd *)&desc[i].data;
1899 /* The first descriptor set the NEXT bit to 1 */
1901 desc[i].flag |= rte_cpu_to_le_16(HNS3_CMD_FLAG_NEXT);
1903 desc[i].flag &= ~rte_cpu_to_le_16(HNS3_CMD_FLAG_NEXT);
1905 for (j = 0; j < HNS3_TC_NUM_ONE_DESC; j++) {
1906 tc_idx = i * HNS3_TC_NUM_ONE_DESC + j;
1907 tc = &s_buf->tc_thrd[tc_idx];
1909 req->com_thrd[j].high =
1910 rte_cpu_to_le_16(tc->high >> HNS3_BUF_UNIT_S);
1911 req->com_thrd[j].high |=
1912 rte_cpu_to_le_16(BIT(HNS3_RX_PRIV_EN_B));
1913 req->com_thrd[j].low =
1914 rte_cpu_to_le_16(tc->low >> HNS3_BUF_UNIT_S);
1915 req->com_thrd[j].low |=
1916 rte_cpu_to_le_16(BIT(HNS3_RX_PRIV_EN_B));
1920 /* Send 2 descriptors at one time */
1921 ret = hns3_cmd_send(hw, desc, HNS3_RX_COM_THRD_ALLOC_DESC_NUM);
1923 PMD_INIT_LOG(ERR, "common threshold config cmd failed %d", ret);
1929 hns3_common_wl_config(struct hns3_hw *hw, struct hns3_pkt_buf_alloc *buf_alloc)
1931 struct hns3_shared_buf *buf = &buf_alloc->s_buf;
1932 struct hns3_rx_com_wl *req;
1933 struct hns3_cmd_desc desc;
1936 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_RX_COM_WL_ALLOC, false);
1938 req = (struct hns3_rx_com_wl *)desc.data;
1939 req->com_wl.high = rte_cpu_to_le_16(buf->self.high >> HNS3_BUF_UNIT_S);
1940 req->com_wl.high |= rte_cpu_to_le_16(BIT(HNS3_RX_PRIV_EN_B));
1942 req->com_wl.low = rte_cpu_to_le_16(buf->self.low >> HNS3_BUF_UNIT_S);
1943 req->com_wl.low |= rte_cpu_to_le_16(BIT(HNS3_RX_PRIV_EN_B));
1945 ret = hns3_cmd_send(hw, &desc, 1);
1947 PMD_INIT_LOG(ERR, "common waterline config cmd failed %d", ret);
1953 hns3_buffer_alloc(struct hns3_hw *hw)
1955 struct hns3_pkt_buf_alloc pkt_buf;
1958 memset(&pkt_buf, 0, sizeof(pkt_buf));
1959 ret = hns3_tx_buffer_calc(hw, &pkt_buf);
1962 "could not calc tx buffer size for all TCs %d",
1967 ret = hns3_tx_buffer_alloc(hw, &pkt_buf);
1969 PMD_INIT_LOG(ERR, "could not alloc tx buffers %d", ret);
1973 ret = hns3_rx_buffer_calc(hw, &pkt_buf);
1976 "could not calc rx priv buffer size for all TCs %d",
1981 ret = hns3_rx_priv_buf_alloc(hw, &pkt_buf);
1983 PMD_INIT_LOG(ERR, "could not alloc rx priv buffer %d", ret);
1987 if (hns3_dev_dcb_supported(hw)) {
1988 ret = hns3_rx_priv_wl_config(hw, &pkt_buf);
1991 "could not configure rx private waterline %d",
1996 ret = hns3_common_thrd_config(hw, &pkt_buf);
1999 "could not configure common threshold %d",
2005 ret = hns3_common_wl_config(hw, &pkt_buf);
2007 PMD_INIT_LOG(ERR, "could not configure common waterline %d",
2014 hns3_mac_init(struct hns3_hw *hw)
2016 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
2017 struct hns3_mac *mac = &hw->mac;
2018 struct hns3_pf *pf = &hns->pf;
2021 pf->support_sfp_query = true;
2022 mac->link_duplex = ETH_LINK_FULL_DUPLEX;
2023 ret = hns3_cfg_mac_speed_dup_hw(hw, mac->link_speed, mac->link_duplex);
2025 PMD_INIT_LOG(ERR, "Config mac speed dup fail ret = %d", ret);
2029 mac->link_status = ETH_LINK_DOWN;
2031 return hns3_config_mtu(hw, pf->mps);
2035 hns3_get_mac_ethertype_cmd_status(uint16_t cmdq_resp, uint8_t resp_code)
2037 #define HNS3_ETHERTYPE_SUCCESS_ADD 0
2038 #define HNS3_ETHERTYPE_ALREADY_ADD 1
2039 #define HNS3_ETHERTYPE_MGR_TBL_OVERFLOW 2
2040 #define HNS3_ETHERTYPE_KEY_CONFLICT 3
2045 "cmdq execute failed for get_mac_ethertype_cmd_status, status=%d.\n",
2050 switch (resp_code) {
2051 case HNS3_ETHERTYPE_SUCCESS_ADD:
2052 case HNS3_ETHERTYPE_ALREADY_ADD:
2055 case HNS3_ETHERTYPE_MGR_TBL_OVERFLOW:
2057 "add mac ethertype failed for manager table overflow.");
2058 return_status = -EIO;
2060 case HNS3_ETHERTYPE_KEY_CONFLICT:
2061 PMD_INIT_LOG(ERR, "add mac ethertype failed for key conflict.");
2062 return_status = -EIO;
2066 "add mac ethertype failed for undefined, code=%d.",
2068 return_status = -EIO;
2071 return return_status;
2075 hns3_add_mgr_tbl(struct hns3_hw *hw,
2076 const struct hns3_mac_mgr_tbl_entry_cmd *req)
2078 struct hns3_cmd_desc desc;
2083 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_MAC_ETHTYPE_ADD, false);
2084 memcpy(desc.data, req, sizeof(struct hns3_mac_mgr_tbl_entry_cmd));
2086 ret = hns3_cmd_send(hw, &desc, 1);
2089 "add mac ethertype failed for cmd_send, ret =%d.",
2094 resp_code = (rte_le_to_cpu_32(desc.data[0]) >> 8) & 0xff;
2095 retval = rte_le_to_cpu_16(desc.retval);
2097 return hns3_get_mac_ethertype_cmd_status(retval, resp_code);
2101 hns3_prepare_mgr_tbl(struct hns3_mac_mgr_tbl_entry_cmd *mgr_table,
2102 int *table_item_num)
2104 struct hns3_mac_mgr_tbl_entry_cmd *tbl;
2107 * In current version, we add one item in management table as below:
2108 * 0x0180C200000E -- LLDP MC address
2111 tbl->flags = HNS3_MAC_MGR_MASK_VLAN_B;
2112 tbl->ethter_type = rte_cpu_to_le_16(HNS3_MAC_ETHERTYPE_LLDP);
2113 tbl->mac_addr_hi32 = rte_cpu_to_le_32(htonl(0x0180C200));
2114 tbl->mac_addr_lo16 = rte_cpu_to_le_16(htons(0x000E));
2115 tbl->i_port_bitmap = 0x1;
2116 *table_item_num = 1;
2120 hns3_init_mgr_tbl(struct hns3_hw *hw)
2122 #define HNS_MAC_MGR_TBL_MAX_SIZE 16
2123 struct hns3_mac_mgr_tbl_entry_cmd mgr_table[HNS_MAC_MGR_TBL_MAX_SIZE];
2128 memset(mgr_table, 0, sizeof(mgr_table));
2129 hns3_prepare_mgr_tbl(mgr_table, &table_item_num);
2130 for (i = 0; i < table_item_num; i++) {
2131 ret = hns3_add_mgr_tbl(hw, &mgr_table[i]);
2133 PMD_INIT_LOG(ERR, "add mac ethertype failed, ret =%d",
2143 hns3_promisc_param_init(struct hns3_promisc_param *param, bool en_uc,
2144 bool en_mc, bool en_bc, int vport_id)
2149 memset(param, 0, sizeof(struct hns3_promisc_param));
2151 param->enable = HNS3_PROMISC_EN_UC;
2153 param->enable |= HNS3_PROMISC_EN_MC;
2155 param->enable |= HNS3_PROMISC_EN_BC;
2156 param->vf_id = vport_id;
2160 hns3_cmd_set_promisc_mode(struct hns3_hw *hw, struct hns3_promisc_param *param)
2162 struct hns3_promisc_cfg_cmd *req;
2163 struct hns3_cmd_desc desc;
2166 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_CFG_PROMISC_MODE, false);
2168 req = (struct hns3_promisc_cfg_cmd *)desc.data;
2169 req->vf_id = param->vf_id;
2170 req->flag = (param->enable << HNS3_PROMISC_EN_B) |
2171 HNS3_PROMISC_TX_EN_B | HNS3_PROMISC_RX_EN_B;
2173 ret = hns3_cmd_send(hw, &desc, 1);
2175 PMD_INIT_LOG(ERR, "Set promisc mode fail, status is %d", ret);
2181 hns3_set_promisc_mode(struct hns3_hw *hw, bool en_uc_pmc, bool en_mc_pmc)
2183 struct hns3_promisc_param param;
2184 bool en_bc_pmc = true;
2189 * In current version VF is not supported when PF is driven by DPDK
2190 * driver, the PF-related vf_id is 0, just need to configure parameters
2195 hns3_promisc_param_init(¶m, en_uc_pmc, en_mc_pmc, en_bc_pmc, vf_id);
2196 ret = hns3_cmd_set_promisc_mode(hw, ¶m);
2204 hns3_init_hardware(struct hns3_adapter *hns)
2206 struct hns3_hw *hw = &hns->hw;
2209 ret = hns3_map_tqp(hw);
2211 PMD_INIT_LOG(ERR, "Failed to map tqp: %d", ret);
2215 ret = hns3_init_umv_space(hw);
2217 PMD_INIT_LOG(ERR, "Failed to init umv space: %d", ret);
2221 ret = hns3_mac_init(hw);
2223 PMD_INIT_LOG(ERR, "Failed to init MAC: %d", ret);
2227 ret = hns3_init_mgr_tbl(hw);
2229 PMD_INIT_LOG(ERR, "Failed to init manager table: %d", ret);
2233 ret = hns3_set_promisc_mode(hw, false, false);
2235 PMD_INIT_LOG(ERR, "Failed to set promisc mode: %d", ret);
2239 ret = hns3_config_tso(hw, HNS3_TSO_MSS_MIN, HNS3_TSO_MSS_MAX);
2241 PMD_INIT_LOG(ERR, "Failed to config tso: %d", ret);
2245 ret = hns3_config_gro(hw, false);
2247 PMD_INIT_LOG(ERR, "Failed to config gro: %d", ret);
2253 hns3_uninit_umv_space(hw);
2258 hns3_init_pf(struct rte_eth_dev *eth_dev)
2260 struct rte_device *dev = eth_dev->device;
2261 struct rte_pci_device *pci_dev = RTE_DEV_TO_PCI(dev);
2262 struct hns3_adapter *hns = eth_dev->data->dev_private;
2263 struct hns3_hw *hw = &hns->hw;
2266 PMD_INIT_FUNC_TRACE();
2268 /* Get hardware io base address from pcie BAR2 IO space */
2269 hw->io_base = pci_dev->mem_resource[2].addr;
2271 /* Firmware command queue initialize */
2272 ret = hns3_cmd_init_queue(hw);
2274 PMD_INIT_LOG(ERR, "Failed to init cmd queue: %d", ret);
2275 goto err_cmd_init_queue;
2278 /* Firmware command initialize */
2279 ret = hns3_cmd_init(hw);
2281 PMD_INIT_LOG(ERR, "Failed to init cmd: %d", ret);
2285 /* Get configuration */
2286 ret = hns3_get_configuration(hw);
2288 PMD_INIT_LOG(ERR, "Failed to fetch configuration: %d", ret);
2289 goto err_get_config;
2292 ret = hns3_init_hardware(hns);
2294 PMD_INIT_LOG(ERR, "Failed to init hardware: %d", ret);
2295 goto err_get_config;
2301 hns3_cmd_uninit(hw);
2304 hns3_cmd_destroy_queue(hw);
2313 hns3_uninit_pf(struct rte_eth_dev *eth_dev)
2315 struct hns3_adapter *hns = eth_dev->data->dev_private;
2316 struct hns3_hw *hw = &hns->hw;
2318 PMD_INIT_FUNC_TRACE();
2320 hns3_uninit_umv_space(hw);
2321 hns3_cmd_uninit(hw);
2322 hns3_cmd_destroy_queue(hw);
2327 hns3_dev_close(struct rte_eth_dev *eth_dev)
2329 struct hns3_adapter *hns = eth_dev->data->dev_private;
2330 struct hns3_hw *hw = &hns->hw;
2332 hw->adapter_state = HNS3_NIC_CLOSING;
2334 hns3_configure_all_mc_mac_addr(hns, true);
2335 hns3_uninit_pf(eth_dev);
2336 hw->adapter_state = HNS3_NIC_CLOSED;
2339 static const struct eth_dev_ops hns3_eth_dev_ops = {
2340 .dev_close = hns3_dev_close,
2341 .mac_addr_add = hns3_add_mac_addr,
2342 .mac_addr_remove = hns3_remove_mac_addr,
2343 .mac_addr_set = hns3_set_default_mac_addr,
2344 .set_mc_addr_list = hns3_set_mc_mac_addr_list,
2348 hns3_dev_init(struct rte_eth_dev *eth_dev)
2350 struct hns3_adapter *hns = eth_dev->data->dev_private;
2351 struct hns3_hw *hw = &hns->hw;
2354 PMD_INIT_FUNC_TRACE();
2356 eth_dev->dev_ops = &hns3_eth_dev_ops;
2357 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
2360 hw->adapter_state = HNS3_NIC_UNINITIALIZED;
2362 hw->data = eth_dev->data;
2365 * Set default max packet size according to the mtu
2366 * default vale in DPDK frame.
2368 hns->pf.mps = hw->data->mtu + HNS3_ETH_OVERHEAD;
2370 ret = hns3_init_pf(eth_dev);
2372 PMD_INIT_LOG(ERR, "Failed to init pf: %d", ret);
2376 /* Allocate memory for storing MAC addresses */
2377 eth_dev->data->mac_addrs = rte_zmalloc("hns3-mac",
2378 sizeof(struct rte_ether_addr) *
2379 HNS3_UC_MACADDR_NUM, 0);
2380 if (eth_dev->data->mac_addrs == NULL) {
2381 PMD_INIT_LOG(ERR, "Failed to allocate %zx bytes needed "
2382 "to store MAC addresses",
2383 sizeof(struct rte_ether_addr) *
2384 HNS3_UC_MACADDR_NUM);
2386 goto err_rte_zmalloc;
2389 rte_ether_addr_copy((struct rte_ether_addr *)hw->mac.mac_addr,
2390 ð_dev->data->mac_addrs[0]);
2392 hw->adapter_state = HNS3_NIC_INITIALIZED;
2394 * Pass the information to the rte_eth_dev_close() that it should also
2395 * release the private port resources.
2397 eth_dev->data->dev_flags |= RTE_ETH_DEV_CLOSE_REMOVE;
2399 hns3_info(hw, "hns3 dev initialization successful!");
2403 hns3_uninit_pf(eth_dev);
2406 eth_dev->dev_ops = NULL;
2407 eth_dev->rx_pkt_burst = NULL;
2408 eth_dev->tx_pkt_burst = NULL;
2409 eth_dev->tx_pkt_prepare = NULL;
2414 hns3_dev_uninit(struct rte_eth_dev *eth_dev)
2416 struct hns3_adapter *hns = eth_dev->data->dev_private;
2417 struct hns3_hw *hw = &hns->hw;
2419 PMD_INIT_FUNC_TRACE();
2421 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
2424 eth_dev->dev_ops = NULL;
2425 eth_dev->rx_pkt_burst = NULL;
2426 eth_dev->tx_pkt_burst = NULL;
2427 eth_dev->tx_pkt_prepare = NULL;
2428 if (hw->adapter_state < HNS3_NIC_CLOSING)
2429 hns3_dev_close(eth_dev);
2431 hw->adapter_state = HNS3_NIC_REMOVED;
2436 eth_hns3_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
2437 struct rte_pci_device *pci_dev)
2439 return rte_eth_dev_pci_generic_probe(pci_dev,
2440 sizeof(struct hns3_adapter),
2445 eth_hns3_pci_remove(struct rte_pci_device *pci_dev)
2447 return rte_eth_dev_pci_generic_remove(pci_dev, hns3_dev_uninit);
2450 static const struct rte_pci_id pci_id_hns3_map[] = {
2451 { RTE_PCI_DEVICE(PCI_VENDOR_ID_HUAWEI, HNS3_DEV_ID_GE) },
2452 { RTE_PCI_DEVICE(PCI_VENDOR_ID_HUAWEI, HNS3_DEV_ID_25GE) },
2453 { RTE_PCI_DEVICE(PCI_VENDOR_ID_HUAWEI, HNS3_DEV_ID_25GE_RDMA) },
2454 { RTE_PCI_DEVICE(PCI_VENDOR_ID_HUAWEI, HNS3_DEV_ID_50GE_RDMA) },
2455 { RTE_PCI_DEVICE(PCI_VENDOR_ID_HUAWEI, HNS3_DEV_ID_100G_RDMA_MACSEC) },
2456 { .vendor_id = 0, /* sentinel */ },
2459 static struct rte_pci_driver rte_hns3_pmd = {
2460 .id_table = pci_id_hns3_map,
2461 .drv_flags = RTE_PCI_DRV_NEED_MAPPING,
2462 .probe = eth_hns3_pci_probe,
2463 .remove = eth_hns3_pci_remove,
2466 RTE_PMD_REGISTER_PCI(net_hns3, rte_hns3_pmd);
2467 RTE_PMD_REGISTER_PCI_TABLE(net_hns3, pci_id_hns3_map);
2468 RTE_PMD_REGISTER_KMOD_DEP(net_hns3, "* igb_uio | vfio-pci");
2470 RTE_INIT(hns3_init_log)
2472 hns3_logtype_init = rte_log_register("pmd.net.hns3.init");
2473 if (hns3_logtype_init >= 0)
2474 rte_log_set_level(hns3_logtype_init, RTE_LOG_NOTICE);
2475 hns3_logtype_driver = rte_log_register("pmd.net.hns3.driver");
2476 if (hns3_logtype_driver >= 0)
2477 rte_log_set_level(hns3_logtype_driver, RTE_LOG_NOTICE);