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 #define HNS3_SERVICE_INTERVAL 1000000 /* us */
33 int hns3_logtype_init;
34 int hns3_logtype_driver;
37 hns3_config_tso(struct hns3_hw *hw, unsigned int tso_mss_min,
38 unsigned int tso_mss_max)
40 struct hns3_cfg_tso_status_cmd *req;
41 struct hns3_cmd_desc desc;
44 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_TSO_GENERIC_CONFIG, false);
46 req = (struct hns3_cfg_tso_status_cmd *)desc.data;
49 hns3_set_field(tso_mss, HNS3_TSO_MSS_MIN_M, HNS3_TSO_MSS_MIN_S,
51 req->tso_mss_min = rte_cpu_to_le_16(tso_mss);
54 hns3_set_field(tso_mss, HNS3_TSO_MSS_MIN_M, HNS3_TSO_MSS_MIN_S,
56 req->tso_mss_max = rte_cpu_to_le_16(tso_mss);
58 return hns3_cmd_send(hw, &desc, 1);
62 hns3_config_gro(struct hns3_hw *hw, bool en)
64 struct hns3_cfg_gro_status_cmd *req;
65 struct hns3_cmd_desc desc;
68 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_GRO_GENERIC_CONFIG, false);
69 req = (struct hns3_cfg_gro_status_cmd *)desc.data;
71 req->gro_en = rte_cpu_to_le_16(en ? 1 : 0);
73 ret = hns3_cmd_send(hw, &desc, 1);
75 hns3_err(hw, "GRO hardware config cmd failed, ret = %d", ret);
81 hns3_set_umv_space(struct hns3_hw *hw, uint16_t space_size,
82 uint16_t *allocated_size, bool is_alloc)
84 struct hns3_umv_spc_alc_cmd *req;
85 struct hns3_cmd_desc desc;
88 req = (struct hns3_umv_spc_alc_cmd *)desc.data;
89 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_MAC_VLAN_ALLOCATE, false);
90 hns3_set_bit(req->allocate, HNS3_UMV_SPC_ALC_B, is_alloc ? 0 : 1);
91 req->space_size = rte_cpu_to_le_32(space_size);
93 ret = hns3_cmd_send(hw, &desc, 1);
95 PMD_INIT_LOG(ERR, "%s umv space failed for cmd_send, ret =%d",
96 is_alloc ? "allocate" : "free", ret);
100 if (is_alloc && allocated_size)
101 *allocated_size = rte_le_to_cpu_32(desc.data[1]);
107 hns3_init_umv_space(struct hns3_hw *hw)
109 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
110 struct hns3_pf *pf = &hns->pf;
111 uint16_t allocated_size = 0;
114 ret = hns3_set_umv_space(hw, pf->wanted_umv_size, &allocated_size,
119 if (allocated_size < pf->wanted_umv_size)
120 PMD_INIT_LOG(WARNING, "Alloc umv space failed, want %u, get %u",
121 pf->wanted_umv_size, allocated_size);
123 pf->max_umv_size = (!!allocated_size) ? allocated_size :
125 pf->used_umv_size = 0;
130 hns3_uninit_umv_space(struct hns3_hw *hw)
132 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
133 struct hns3_pf *pf = &hns->pf;
136 if (pf->max_umv_size == 0)
139 ret = hns3_set_umv_space(hw, pf->max_umv_size, NULL, false);
143 pf->max_umv_size = 0;
149 hns3_is_umv_space_full(struct hns3_hw *hw)
151 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
152 struct hns3_pf *pf = &hns->pf;
155 is_full = (pf->used_umv_size >= pf->max_umv_size);
161 hns3_update_umv_space(struct hns3_hw *hw, bool is_free)
163 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
164 struct hns3_pf *pf = &hns->pf;
167 if (pf->used_umv_size > 0)
174 hns3_prepare_mac_addr(struct hns3_mac_vlan_tbl_entry_cmd *new_req,
175 const uint8_t *addr, bool is_mc)
177 const unsigned char *mac_addr = addr;
178 uint32_t high_val = ((uint32_t)mac_addr[3] << 24) |
179 ((uint32_t)mac_addr[2] << 16) |
180 ((uint32_t)mac_addr[1] << 8) |
181 (uint32_t)mac_addr[0];
182 uint32_t low_val = ((uint32_t)mac_addr[5] << 8) | (uint32_t)mac_addr[4];
184 hns3_set_bit(new_req->flags, HNS3_MAC_VLAN_BIT0_EN_B, 1);
186 hns3_set_bit(new_req->entry_type, HNS3_MAC_VLAN_BIT0_EN_B, 0);
187 hns3_set_bit(new_req->entry_type, HNS3_MAC_VLAN_BIT1_EN_B, 1);
188 hns3_set_bit(new_req->mc_mac_en, HNS3_MAC_VLAN_BIT0_EN_B, 1);
191 new_req->mac_addr_hi32 = rte_cpu_to_le_32(high_val);
192 new_req->mac_addr_lo16 = rte_cpu_to_le_16(low_val & 0xffff);
196 hns3_get_mac_vlan_cmd_status(struct hns3_hw *hw, uint16_t cmdq_resp,
198 enum hns3_mac_vlan_tbl_opcode op)
201 hns3_err(hw, "cmdq execute failed for get_mac_vlan_cmd_status,status=%u",
206 if (op == HNS3_MAC_VLAN_ADD) {
207 if (resp_code == 0 || resp_code == 1) {
209 } else if (resp_code == HNS3_ADD_UC_OVERFLOW) {
210 hns3_err(hw, "add mac addr failed for uc_overflow");
212 } else if (resp_code == HNS3_ADD_MC_OVERFLOW) {
213 hns3_err(hw, "add mac addr failed for mc_overflow");
217 hns3_err(hw, "add mac addr failed for undefined, code=%u",
220 } else if (op == HNS3_MAC_VLAN_REMOVE) {
221 if (resp_code == 0) {
223 } else if (resp_code == 1) {
224 hns3_dbg(hw, "remove mac addr failed for miss");
228 hns3_err(hw, "remove mac addr failed for undefined, code=%u",
231 } else if (op == HNS3_MAC_VLAN_LKUP) {
232 if (resp_code == 0) {
234 } else if (resp_code == 1) {
235 hns3_dbg(hw, "lookup mac addr failed for miss");
239 hns3_err(hw, "lookup mac addr failed for undefined, code=%u",
244 hns3_err(hw, "unknown opcode for get_mac_vlan_cmd_status, opcode=%u",
251 hns3_lookup_mac_vlan_tbl(struct hns3_hw *hw,
252 struct hns3_mac_vlan_tbl_entry_cmd *req,
253 struct hns3_cmd_desc *desc, bool is_mc)
259 hns3_cmd_setup_basic_desc(&desc[0], HNS3_OPC_MAC_VLAN_ADD, true);
261 desc[0].flag |= rte_cpu_to_le_16(HNS3_CMD_FLAG_NEXT);
262 memcpy(desc[0].data, req,
263 sizeof(struct hns3_mac_vlan_tbl_entry_cmd));
264 hns3_cmd_setup_basic_desc(&desc[1], HNS3_OPC_MAC_VLAN_ADD,
266 desc[1].flag |= rte_cpu_to_le_16(HNS3_CMD_FLAG_NEXT);
267 hns3_cmd_setup_basic_desc(&desc[2], HNS3_OPC_MAC_VLAN_ADD,
269 ret = hns3_cmd_send(hw, desc, HNS3_MC_MAC_VLAN_ADD_DESC_NUM);
271 memcpy(desc[0].data, req,
272 sizeof(struct hns3_mac_vlan_tbl_entry_cmd));
273 ret = hns3_cmd_send(hw, desc, 1);
276 hns3_err(hw, "lookup mac addr failed for cmd_send, ret =%d.",
280 resp_code = (rte_le_to_cpu_32(desc[0].data[0]) >> 8) & 0xff;
281 retval = rte_le_to_cpu_16(desc[0].retval);
283 return hns3_get_mac_vlan_cmd_status(hw, retval, resp_code,
288 hns3_add_mac_vlan_tbl(struct hns3_hw *hw,
289 struct hns3_mac_vlan_tbl_entry_cmd *req,
290 struct hns3_cmd_desc *mc_desc)
297 if (mc_desc == NULL) {
298 struct hns3_cmd_desc desc;
300 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_MAC_VLAN_ADD, false);
301 memcpy(desc.data, req,
302 sizeof(struct hns3_mac_vlan_tbl_entry_cmd));
303 ret = hns3_cmd_send(hw, &desc, 1);
304 resp_code = (rte_le_to_cpu_32(desc.data[0]) >> 8) & 0xff;
305 retval = rte_le_to_cpu_16(desc.retval);
307 cfg_status = hns3_get_mac_vlan_cmd_status(hw, retval, resp_code,
310 hns3_cmd_reuse_desc(&mc_desc[0], false);
311 mc_desc[0].flag |= rte_cpu_to_le_16(HNS3_CMD_FLAG_NEXT);
312 hns3_cmd_reuse_desc(&mc_desc[1], false);
313 mc_desc[1].flag |= rte_cpu_to_le_16(HNS3_CMD_FLAG_NEXT);
314 hns3_cmd_reuse_desc(&mc_desc[2], false);
315 mc_desc[2].flag &= rte_cpu_to_le_16(~HNS3_CMD_FLAG_NEXT);
316 memcpy(mc_desc[0].data, req,
317 sizeof(struct hns3_mac_vlan_tbl_entry_cmd));
318 mc_desc[0].retval = 0;
319 ret = hns3_cmd_send(hw, mc_desc, HNS3_MC_MAC_VLAN_ADD_DESC_NUM);
320 resp_code = (rte_le_to_cpu_32(mc_desc[0].data[0]) >> 8) & 0xff;
321 retval = rte_le_to_cpu_16(mc_desc[0].retval);
323 cfg_status = hns3_get_mac_vlan_cmd_status(hw, retval, resp_code,
328 hns3_err(hw, "add mac addr failed for cmd_send, ret =%d", ret);
336 hns3_remove_mac_vlan_tbl(struct hns3_hw *hw,
337 struct hns3_mac_vlan_tbl_entry_cmd *req)
339 struct hns3_cmd_desc desc;
344 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_MAC_VLAN_REMOVE, false);
346 memcpy(desc.data, req, sizeof(struct hns3_mac_vlan_tbl_entry_cmd));
348 ret = hns3_cmd_send(hw, &desc, 1);
350 hns3_err(hw, "del mac addr failed for cmd_send, ret =%d", ret);
353 resp_code = (rte_le_to_cpu_32(desc.data[0]) >> 8) & 0xff;
354 retval = rte_le_to_cpu_16(desc.retval);
356 return hns3_get_mac_vlan_cmd_status(hw, retval, resp_code,
357 HNS3_MAC_VLAN_REMOVE);
361 hns3_add_uc_addr_common(struct hns3_hw *hw, struct rte_ether_addr *mac_addr)
363 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
364 struct hns3_mac_vlan_tbl_entry_cmd req;
365 struct hns3_pf *pf = &hns->pf;
366 struct hns3_cmd_desc desc;
367 char mac_str[RTE_ETHER_ADDR_FMT_SIZE];
368 uint16_t egress_port = 0;
372 /* check if mac addr is valid */
373 if (!rte_is_valid_assigned_ether_addr(mac_addr)) {
374 rte_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
376 hns3_err(hw, "Add unicast mac addr err! addr(%s) invalid",
381 memset(&req, 0, sizeof(req));
384 * In current version VF is not supported when PF is driven by DPDK
385 * driver, the PF-related vf_id is 0, just need to configure parameters
389 hns3_set_field(egress_port, HNS3_MAC_EPORT_VFID_M,
390 HNS3_MAC_EPORT_VFID_S, vf_id);
392 req.egress_port = rte_cpu_to_le_16(egress_port);
394 hns3_prepare_mac_addr(&req, mac_addr->addr_bytes, false);
397 * Lookup the mac address in the mac_vlan table, and add
398 * it if the entry is inexistent. Repeated unicast entry
399 * is not allowed in the mac vlan table.
401 ret = hns3_lookup_mac_vlan_tbl(hw, &req, &desc, false);
402 if (ret == -ENOENT) {
403 if (!hns3_is_umv_space_full(hw)) {
404 ret = hns3_add_mac_vlan_tbl(hw, &req, NULL);
406 hns3_update_umv_space(hw, false);
410 hns3_err(hw, "UC MAC table full(%u)", pf->used_umv_size);
415 rte_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE, mac_addr);
417 /* check if we just hit the duplicate */
419 hns3_dbg(hw, "mac addr(%s) has been in the MAC table", mac_str);
423 hns3_err(hw, "PF failed to add unicast entry(%s) in the MAC table",
430 hns3_add_mac_addr(struct rte_eth_dev *dev, struct rte_ether_addr *mac_addr,
431 uint32_t idx, __attribute__ ((unused)) uint32_t pool)
433 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
434 char mac_str[RTE_ETHER_ADDR_FMT_SIZE];
437 rte_spinlock_lock(&hw->lock);
438 ret = hns3_add_uc_addr_common(hw, mac_addr);
440 rte_spinlock_unlock(&hw->lock);
441 rte_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
443 hns3_err(hw, "Failed to add mac addr(%s): %d", mac_str, ret);
448 hw->mac.default_addr_setted = true;
449 rte_spinlock_unlock(&hw->lock);
455 hns3_remove_uc_addr_common(struct hns3_hw *hw, struct rte_ether_addr *mac_addr)
457 struct hns3_mac_vlan_tbl_entry_cmd req;
458 char mac_str[RTE_ETHER_ADDR_FMT_SIZE];
461 /* check if mac addr is valid */
462 if (!rte_is_valid_assigned_ether_addr(mac_addr)) {
463 rte_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
465 hns3_err(hw, "Remove unicast mac addr err! addr(%s) invalid",
470 memset(&req, 0, sizeof(req));
471 hns3_set_bit(req.entry_type, HNS3_MAC_VLAN_BIT0_EN_B, 0);
472 hns3_prepare_mac_addr(&req, mac_addr->addr_bytes, false);
473 ret = hns3_remove_mac_vlan_tbl(hw, &req);
474 if (ret == -ENOENT) /* mac addr isn't existent in the mac vlan table. */
477 hns3_update_umv_space(hw, true);
483 hns3_remove_mac_addr(struct rte_eth_dev *dev, uint32_t idx)
485 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
486 /* index will be checked by upper level rte interface */
487 struct rte_ether_addr *mac_addr = &dev->data->mac_addrs[idx];
488 char mac_str[RTE_ETHER_ADDR_FMT_SIZE];
491 rte_spinlock_lock(&hw->lock);
492 ret = hns3_remove_uc_addr_common(hw, mac_addr);
494 rte_spinlock_unlock(&hw->lock);
495 rte_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
497 hns3_err(hw, "Failed to remove mac addr(%s): %d", mac_str, ret);
502 hw->mac.default_addr_setted = false;
503 rte_spinlock_unlock(&hw->lock);
507 hns3_set_default_mac_addr(struct rte_eth_dev *dev,
508 struct rte_ether_addr *mac_addr)
510 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
511 struct rte_ether_addr *oaddr;
512 char mac_str[RTE_ETHER_ADDR_FMT_SIZE];
513 bool default_addr_setted;
514 bool rm_succes = false;
517 /* check if mac addr is valid */
518 if (!rte_is_valid_assigned_ether_addr(mac_addr)) {
519 rte_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
521 hns3_err(hw, "Failed to set mac addr, addr(%s) invalid",
526 oaddr = (struct rte_ether_addr *)hw->mac.mac_addr;
527 default_addr_setted = hw->mac.default_addr_setted;
528 if (default_addr_setted && !!rte_is_same_ether_addr(mac_addr, oaddr))
531 rte_spinlock_lock(&hw->lock);
532 if (default_addr_setted) {
533 ret = hns3_remove_uc_addr_common(hw, oaddr);
535 rte_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
537 hns3_warn(hw, "Remove old uc mac address(%s) fail: %d",
544 ret = hns3_add_uc_addr_common(hw, mac_addr);
546 rte_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
548 hns3_err(hw, "Failed to set mac addr(%s): %d", mac_str, ret);
549 goto err_add_uc_addr;
552 rte_ether_addr_copy(mac_addr,
553 (struct rte_ether_addr *)hw->mac.mac_addr);
554 hw->mac.default_addr_setted = true;
555 rte_spinlock_unlock(&hw->lock);
561 ret_val = hns3_add_uc_addr_common(hw, oaddr);
563 rte_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
566 "Failed to restore old uc mac addr(%s): %d",
568 hw->mac.default_addr_setted = false;
571 rte_spinlock_unlock(&hw->lock);
577 hns3_update_desc_vfid(struct hns3_cmd_desc *desc, uint8_t vfid, bool clr)
579 #define HNS3_VF_NUM_IN_FIRST_DESC 192
583 if (vfid < HNS3_VF_NUM_IN_FIRST_DESC) {
584 word_num = vfid / 32;
587 desc[1].data[word_num] &=
588 rte_cpu_to_le_32(~(1UL << bit_num));
590 desc[1].data[word_num] |=
591 rte_cpu_to_le_32(1UL << bit_num);
593 word_num = (vfid - HNS3_VF_NUM_IN_FIRST_DESC) / 32;
596 desc[2].data[word_num] &=
597 rte_cpu_to_le_32(~(1UL << bit_num));
599 desc[2].data[word_num] |=
600 rte_cpu_to_le_32(1UL << bit_num);
605 hns3_add_mc_addr(struct hns3_hw *hw, struct rte_ether_addr *mac_addr)
607 struct hns3_mac_vlan_tbl_entry_cmd req;
608 struct hns3_cmd_desc desc[3];
609 char mac_str[RTE_ETHER_ADDR_FMT_SIZE];
613 /* Check if mac addr is valid */
614 if (!rte_is_multicast_ether_addr(mac_addr)) {
615 rte_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
617 hns3_err(hw, "Failed to add mc mac addr, addr(%s) invalid",
622 memset(&req, 0, sizeof(req));
623 hns3_set_bit(req.entry_type, HNS3_MAC_VLAN_BIT0_EN_B, 0);
624 hns3_prepare_mac_addr(&req, mac_addr->addr_bytes, true);
625 ret = hns3_lookup_mac_vlan_tbl(hw, &req, desc, true);
627 /* This mac addr do not exist, add new entry for it */
628 memset(desc[0].data, 0, sizeof(desc[0].data));
629 memset(desc[1].data, 0, sizeof(desc[0].data));
630 memset(desc[2].data, 0, sizeof(desc[0].data));
634 * In current version VF is not supported when PF is driven by DPDK
635 * driver, the PF-related vf_id is 0, just need to configure parameters
639 hns3_update_desc_vfid(desc, vf_id, false);
640 ret = hns3_add_mac_vlan_tbl(hw, &req, desc);
643 hns3_err(hw, "mc mac vlan table is full");
644 rte_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
646 hns3_err(hw, "Failed to add mc mac addr(%s): %d", mac_str, ret);
653 hns3_remove_mc_addr(struct hns3_hw *hw, struct rte_ether_addr *mac_addr)
655 struct hns3_mac_vlan_tbl_entry_cmd req;
656 struct hns3_cmd_desc desc[3];
657 char mac_str[RTE_ETHER_ADDR_FMT_SIZE];
661 /* Check if mac addr is valid */
662 if (!rte_is_multicast_ether_addr(mac_addr)) {
663 rte_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
665 hns3_err(hw, "Failed to rm mc mac addr, addr(%s) invalid",
670 memset(&req, 0, sizeof(req));
671 hns3_set_bit(req.entry_type, HNS3_MAC_VLAN_BIT0_EN_B, 0);
672 hns3_prepare_mac_addr(&req, mac_addr->addr_bytes, true);
673 ret = hns3_lookup_mac_vlan_tbl(hw, &req, desc, true);
676 * This mac addr exist, remove this handle's VFID for it.
677 * In current version VF is not supported when PF is driven by
678 * DPDK driver, the PF-related vf_id is 0, just need to
679 * configure parameters for vf_id 0.
682 hns3_update_desc_vfid(desc, vf_id, true);
684 /* All the vfid is zero, so need to delete this entry */
685 ret = hns3_remove_mac_vlan_tbl(hw, &req);
686 } else if (ret == -ENOENT) {
687 /* This mac addr doesn't exist. */
692 rte_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
694 hns3_err(hw, "Failed to rm mc mac addr(%s): %d", mac_str, ret);
701 hns3_set_mc_addr_chk_param(struct hns3_hw *hw,
702 struct rte_ether_addr *mc_addr_set,
705 char mac_str[RTE_ETHER_ADDR_FMT_SIZE];
706 struct rte_ether_addr *addr;
710 if (nb_mc_addr > HNS3_MC_MACADDR_NUM) {
711 hns3_err(hw, "Failed to set mc mac addr, nb_mc_addr(%d) "
712 "invalid. valid range: 0~%d",
713 nb_mc_addr, HNS3_MC_MACADDR_NUM);
717 /* Check if input mac addresses are valid */
718 for (i = 0; i < nb_mc_addr; i++) {
719 addr = &mc_addr_set[i];
720 if (!rte_is_multicast_ether_addr(addr)) {
721 rte_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
724 "Failed to set mc mac addr, addr(%s) invalid.",
729 /* Check if there are duplicate addresses */
730 for (j = i + 1; j < nb_mc_addr; j++) {
731 if (rte_is_same_ether_addr(addr, &mc_addr_set[j])) {
732 rte_ether_format_addr(mac_str,
733 RTE_ETHER_ADDR_FMT_SIZE,
735 hns3_err(hw, "Failed to set mc mac addr, "
736 "addrs invalid. two same addrs(%s).",
747 hns3_set_mc_addr_calc_addr(struct hns3_hw *hw,
748 struct rte_ether_addr *mc_addr_set,
750 struct rte_ether_addr *reserved_addr_list,
751 int *reserved_addr_num,
752 struct rte_ether_addr *add_addr_list,
754 struct rte_ether_addr *rm_addr_list,
757 struct rte_ether_addr *addr;
758 int current_addr_num;
759 int reserved_num = 0;
767 /* Calculate the mc mac address list that should be removed */
768 current_addr_num = hw->mc_addrs_num;
769 for (i = 0; i < current_addr_num; i++) {
770 addr = &hw->mc_addrs[i];
772 for (j = 0; j < mc_addr_num; j++) {
773 if (rte_is_same_ether_addr(addr, &mc_addr_set[j])) {
780 rte_ether_addr_copy(addr, &rm_addr_list[rm_num]);
783 rte_ether_addr_copy(addr,
784 &reserved_addr_list[reserved_num]);
789 /* Calculate the mc mac address list that should be added */
790 for (i = 0; i < mc_addr_num; i++) {
791 addr = &mc_addr_set[i];
793 for (j = 0; j < current_addr_num; j++) {
794 if (rte_is_same_ether_addr(addr, &hw->mc_addrs[j])) {
801 rte_ether_addr_copy(addr, &add_addr_list[add_num]);
806 /* Reorder the mc mac address list maintained by driver */
807 for (i = 0; i < reserved_num; i++)
808 rte_ether_addr_copy(&reserved_addr_list[i], &hw->mc_addrs[i]);
810 for (i = 0; i < rm_num; i++) {
811 num = reserved_num + i;
812 rte_ether_addr_copy(&rm_addr_list[i], &hw->mc_addrs[num]);
815 *reserved_addr_num = reserved_num;
816 *add_addr_num = add_num;
817 *rm_addr_num = rm_num;
821 hns3_set_mc_mac_addr_list(struct rte_eth_dev *dev,
822 struct rte_ether_addr *mc_addr_set,
825 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
826 struct rte_ether_addr reserved_addr_list[HNS3_MC_MACADDR_NUM];
827 struct rte_ether_addr add_addr_list[HNS3_MC_MACADDR_NUM];
828 struct rte_ether_addr rm_addr_list[HNS3_MC_MACADDR_NUM];
829 struct rte_ether_addr *addr;
830 int reserved_addr_num;
838 /* Check if input parameters are valid */
839 ret = hns3_set_mc_addr_chk_param(hw, mc_addr_set, nb_mc_addr);
843 rte_spinlock_lock(&hw->lock);
846 * Calculate the mc mac address lists those should be removed and be
847 * added, Reorder the mc mac address list maintained by driver.
849 mc_addr_num = (int)nb_mc_addr;
850 hns3_set_mc_addr_calc_addr(hw, mc_addr_set, mc_addr_num,
851 reserved_addr_list, &reserved_addr_num,
852 add_addr_list, &add_addr_num,
853 rm_addr_list, &rm_addr_num);
855 /* Remove mc mac addresses */
856 for (i = 0; i < rm_addr_num; i++) {
857 num = rm_addr_num - i - 1;
858 addr = &rm_addr_list[num];
859 ret = hns3_remove_mc_addr(hw, addr);
861 rte_spinlock_unlock(&hw->lock);
867 /* Add mc mac addresses */
868 for (i = 0; i < add_addr_num; i++) {
869 addr = &add_addr_list[i];
870 ret = hns3_add_mc_addr(hw, addr);
872 rte_spinlock_unlock(&hw->lock);
876 num = reserved_addr_num + i;
877 rte_ether_addr_copy(addr, &hw->mc_addrs[num]);
880 rte_spinlock_unlock(&hw->lock);
886 hns3_configure_all_mc_mac_addr(struct hns3_adapter *hns, bool del)
888 char mac_str[RTE_ETHER_ADDR_FMT_SIZE];
889 struct hns3_hw *hw = &hns->hw;
890 struct rte_ether_addr *addr;
895 for (i = 0; i < hw->mc_addrs_num; i++) {
896 addr = &hw->mc_addrs[i];
897 if (!rte_is_multicast_ether_addr(addr))
900 ret = hns3_remove_mc_addr(hw, addr);
902 ret = hns3_add_mc_addr(hw, addr);
905 rte_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
907 hns3_dbg(hw, "%s mc mac addr: %s failed",
908 del ? "Remove" : "Restore", mac_str);
915 hns3_set_mac_mtu(struct hns3_hw *hw, uint16_t new_mps)
917 struct hns3_config_max_frm_size_cmd *req;
918 struct hns3_cmd_desc desc;
920 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_CONFIG_MAX_FRM_SIZE, false);
922 req = (struct hns3_config_max_frm_size_cmd *)desc.data;
923 req->max_frm_size = rte_cpu_to_le_16(new_mps);
924 req->min_frm_size = HNS3_MIN_FRAME_LEN;
926 return hns3_cmd_send(hw, &desc, 1);
930 hns3_config_mtu(struct hns3_hw *hw, uint16_t mps)
934 ret = hns3_set_mac_mtu(hw, mps);
936 hns3_err(hw, "Failed to set mtu, ret = %d", ret);
940 ret = hns3_buffer_alloc(hw);
942 hns3_err(hw, "Failed to allocate buffer, ret = %d", ret);
950 hns3_dev_mtu_set(struct rte_eth_dev *dev, uint16_t mtu)
952 struct hns3_adapter *hns = dev->data->dev_private;
953 uint32_t frame_size = mtu + HNS3_ETH_OVERHEAD;
954 struct hns3_hw *hw = &hns->hw;
958 if (dev->data->dev_started) {
959 hns3_err(hw, "Failed to set mtu, port %u must be stopped "
960 "before configuration", dev->data->port_id);
964 rte_spinlock_lock(&hw->lock);
965 is_jumbo_frame = frame_size > RTE_ETHER_MAX_LEN ? true : false;
966 frame_size = RTE_MAX(frame_size, HNS3_DEFAULT_FRAME_LEN);
969 * Maximum value of frame_size is HNS3_MAX_FRAME_LEN, so it can safely
970 * assign to "uint16_t" type variable.
972 ret = hns3_config_mtu(hw, (uint16_t)frame_size);
974 rte_spinlock_unlock(&hw->lock);
975 hns3_err(hw, "Failed to set mtu, port %u mtu %u: %d",
976 dev->data->port_id, mtu, ret);
979 hns->pf.mps = (uint16_t)frame_size;
981 dev->data->dev_conf.rxmode.offloads |=
982 DEV_RX_OFFLOAD_JUMBO_FRAME;
984 dev->data->dev_conf.rxmode.offloads &=
985 ~DEV_RX_OFFLOAD_JUMBO_FRAME;
986 dev->data->dev_conf.rxmode.max_rx_pkt_len = frame_size;
987 rte_spinlock_unlock(&hw->lock);
993 hns3_dev_infos_get(struct rte_eth_dev *eth_dev, struct rte_eth_dev_info *info)
995 struct hns3_adapter *hns = eth_dev->data->dev_private;
996 struct hns3_hw *hw = &hns->hw;
998 info->max_rx_queues = hw->tqps_num;
999 info->max_tx_queues = hw->tqps_num;
1000 info->max_rx_pktlen = HNS3_MAX_FRAME_LEN; /* CRC included */
1001 info->min_rx_bufsize = hw->rx_buf_len;
1002 info->max_mac_addrs = HNS3_UC_MACADDR_NUM;
1003 info->max_mtu = info->max_rx_pktlen - HNS3_ETH_OVERHEAD;
1004 info->rx_offload_capa = (DEV_RX_OFFLOAD_IPV4_CKSUM |
1005 DEV_RX_OFFLOAD_TCP_CKSUM |
1006 DEV_RX_OFFLOAD_UDP_CKSUM |
1007 DEV_RX_OFFLOAD_SCTP_CKSUM |
1008 DEV_RX_OFFLOAD_OUTER_IPV4_CKSUM |
1009 DEV_RX_OFFLOAD_OUTER_UDP_CKSUM |
1010 DEV_RX_OFFLOAD_KEEP_CRC |
1011 DEV_RX_OFFLOAD_SCATTER |
1012 DEV_RX_OFFLOAD_VLAN_STRIP |
1013 DEV_RX_OFFLOAD_QINQ_STRIP |
1014 DEV_RX_OFFLOAD_VLAN_FILTER |
1015 DEV_RX_OFFLOAD_VLAN_EXTEND |
1016 DEV_RX_OFFLOAD_JUMBO_FRAME);
1017 info->tx_queue_offload_capa = DEV_TX_OFFLOAD_MBUF_FAST_FREE;
1018 info->tx_offload_capa = (DEV_TX_OFFLOAD_OUTER_IPV4_CKSUM |
1019 DEV_TX_OFFLOAD_IPV4_CKSUM |
1020 DEV_TX_OFFLOAD_TCP_CKSUM |
1021 DEV_TX_OFFLOAD_UDP_CKSUM |
1022 DEV_TX_OFFLOAD_SCTP_CKSUM |
1023 DEV_TX_OFFLOAD_VLAN_INSERT |
1024 DEV_TX_OFFLOAD_QINQ_INSERT |
1025 DEV_TX_OFFLOAD_MULTI_SEGS |
1026 info->tx_queue_offload_capa);
1028 info->vmdq_queue_num = 0;
1030 info->reta_size = HNS3_RSS_IND_TBL_SIZE;
1031 info->hash_key_size = HNS3_RSS_KEY_SIZE;
1032 info->flow_type_rss_offloads = HNS3_ETH_RSS_SUPPORT;
1034 info->default_rxportconf.burst_size = HNS3_DEFAULT_PORT_CONF_BURST_SIZE;
1035 info->default_txportconf.burst_size = HNS3_DEFAULT_PORT_CONF_BURST_SIZE;
1036 info->default_rxportconf.nb_queues = HNS3_DEFAULT_PORT_CONF_QUEUES_NUM;
1037 info->default_txportconf.nb_queues = HNS3_DEFAULT_PORT_CONF_QUEUES_NUM;
1043 hns3_fw_version_get(struct rte_eth_dev *eth_dev, char *fw_version,
1046 struct hns3_adapter *hns = eth_dev->data->dev_private;
1047 struct hns3_hw *hw = &hns->hw;
1050 ret = snprintf(fw_version, fw_size, "0x%08x", hw->fw_version);
1051 ret += 1; /* add the size of '\0' */
1052 if (fw_size < (uint32_t)ret)
1059 hns3_dev_link_update(struct rte_eth_dev *eth_dev,
1060 __rte_unused int wait_to_complete)
1062 struct hns3_adapter *hns = eth_dev->data->dev_private;
1063 struct hns3_hw *hw = &hns->hw;
1064 struct hns3_mac *mac = &hw->mac;
1065 struct rte_eth_link new_link;
1067 memset(&new_link, 0, sizeof(new_link));
1068 switch (mac->link_speed) {
1069 case ETH_SPEED_NUM_10M:
1070 case ETH_SPEED_NUM_100M:
1071 case ETH_SPEED_NUM_1G:
1072 case ETH_SPEED_NUM_10G:
1073 case ETH_SPEED_NUM_25G:
1074 case ETH_SPEED_NUM_40G:
1075 case ETH_SPEED_NUM_50G:
1076 case ETH_SPEED_NUM_100G:
1077 new_link.link_speed = mac->link_speed;
1080 new_link.link_speed = ETH_SPEED_NUM_100M;
1084 new_link.link_duplex = mac->link_duplex;
1085 new_link.link_status = mac->link_status ? ETH_LINK_UP : ETH_LINK_DOWN;
1086 new_link.link_autoneg =
1087 !(eth_dev->data->dev_conf.link_speeds & ETH_LINK_SPEED_FIXED);
1089 return rte_eth_linkstatus_set(eth_dev, &new_link);
1093 hns3_parse_func_status(struct hns3_hw *hw, struct hns3_func_status_cmd *status)
1095 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
1096 struct hns3_pf *pf = &hns->pf;
1098 if (!(status->pf_state & HNS3_PF_STATE_DONE))
1101 pf->is_main_pf = (status->pf_state & HNS3_PF_STATE_MAIN) ? true : false;
1107 hns3_query_function_status(struct hns3_hw *hw)
1109 #define HNS3_QUERY_MAX_CNT 10
1110 #define HNS3_QUERY_SLEEP_MSCOEND 1
1111 struct hns3_func_status_cmd *req;
1112 struct hns3_cmd_desc desc;
1116 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_QUERY_FUNC_STATUS, true);
1117 req = (struct hns3_func_status_cmd *)desc.data;
1120 ret = hns3_cmd_send(hw, &desc, 1);
1122 PMD_INIT_LOG(ERR, "query function status failed %d",
1127 /* Check pf reset is done */
1131 rte_delay_ms(HNS3_QUERY_SLEEP_MSCOEND);
1132 } while (timeout++ < HNS3_QUERY_MAX_CNT);
1134 return hns3_parse_func_status(hw, req);
1138 hns3_query_pf_resource(struct hns3_hw *hw)
1140 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
1141 struct hns3_pf *pf = &hns->pf;
1142 struct hns3_pf_res_cmd *req;
1143 struct hns3_cmd_desc desc;
1146 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_QUERY_PF_RSRC, true);
1147 ret = hns3_cmd_send(hw, &desc, 1);
1149 PMD_INIT_LOG(ERR, "query pf resource failed %d", ret);
1153 req = (struct hns3_pf_res_cmd *)desc.data;
1154 hw->total_tqps_num = rte_le_to_cpu_16(req->tqp_num);
1155 pf->pkt_buf_size = rte_le_to_cpu_16(req->buf_size) << HNS3_BUF_UNIT_S;
1156 hw->tqps_num = RTE_MIN(hw->total_tqps_num, HNS3_MAX_TQP_NUM_PER_FUNC);
1158 if (req->tx_buf_size)
1160 rte_le_to_cpu_16(req->tx_buf_size) << HNS3_BUF_UNIT_S;
1162 pf->tx_buf_size = HNS3_DEFAULT_TX_BUF;
1164 pf->tx_buf_size = roundup(pf->tx_buf_size, HNS3_BUF_SIZE_UNIT);
1166 if (req->dv_buf_size)
1168 rte_le_to_cpu_16(req->dv_buf_size) << HNS3_BUF_UNIT_S;
1170 pf->dv_buf_size = HNS3_DEFAULT_DV;
1172 pf->dv_buf_size = roundup(pf->dv_buf_size, HNS3_BUF_SIZE_UNIT);
1175 hns3_get_field(rte_le_to_cpu_16(req->pf_intr_vector_number),
1176 HNS3_PF_VEC_NUM_M, HNS3_PF_VEC_NUM_S);
1182 hns3_parse_cfg(struct hns3_cfg *cfg, struct hns3_cmd_desc *desc)
1184 struct hns3_cfg_param_cmd *req;
1185 uint64_t mac_addr_tmp_high;
1186 uint64_t mac_addr_tmp;
1189 req = (struct hns3_cfg_param_cmd *)desc[0].data;
1191 /* get the configuration */
1192 cfg->vmdq_vport_num = hns3_get_field(rte_le_to_cpu_32(req->param[0]),
1193 HNS3_CFG_VMDQ_M, HNS3_CFG_VMDQ_S);
1194 cfg->tc_num = hns3_get_field(rte_le_to_cpu_32(req->param[0]),
1195 HNS3_CFG_TC_NUM_M, HNS3_CFG_TC_NUM_S);
1196 cfg->tqp_desc_num = hns3_get_field(rte_le_to_cpu_32(req->param[0]),
1197 HNS3_CFG_TQP_DESC_N_M,
1198 HNS3_CFG_TQP_DESC_N_S);
1200 cfg->phy_addr = hns3_get_field(rte_le_to_cpu_32(req->param[1]),
1201 HNS3_CFG_PHY_ADDR_M,
1202 HNS3_CFG_PHY_ADDR_S);
1203 cfg->media_type = hns3_get_field(rte_le_to_cpu_32(req->param[1]),
1204 HNS3_CFG_MEDIA_TP_M,
1205 HNS3_CFG_MEDIA_TP_S);
1206 cfg->rx_buf_len = hns3_get_field(rte_le_to_cpu_32(req->param[1]),
1207 HNS3_CFG_RX_BUF_LEN_M,
1208 HNS3_CFG_RX_BUF_LEN_S);
1209 /* get mac address */
1210 mac_addr_tmp = rte_le_to_cpu_32(req->param[2]);
1211 mac_addr_tmp_high = hns3_get_field(rte_le_to_cpu_32(req->param[3]),
1212 HNS3_CFG_MAC_ADDR_H_M,
1213 HNS3_CFG_MAC_ADDR_H_S);
1215 mac_addr_tmp |= (mac_addr_tmp_high << 31) << 1;
1217 cfg->default_speed = hns3_get_field(rte_le_to_cpu_32(req->param[3]),
1218 HNS3_CFG_DEFAULT_SPEED_M,
1219 HNS3_CFG_DEFAULT_SPEED_S);
1220 cfg->rss_size_max = hns3_get_field(rte_le_to_cpu_32(req->param[3]),
1221 HNS3_CFG_RSS_SIZE_M,
1222 HNS3_CFG_RSS_SIZE_S);
1224 for (i = 0; i < RTE_ETHER_ADDR_LEN; i++)
1225 cfg->mac_addr[i] = (mac_addr_tmp >> (8 * i)) & 0xff;
1227 req = (struct hns3_cfg_param_cmd *)desc[1].data;
1228 cfg->numa_node_map = rte_le_to_cpu_32(req->param[0]);
1230 cfg->speed_ability = hns3_get_field(rte_le_to_cpu_32(req->param[1]),
1231 HNS3_CFG_SPEED_ABILITY_M,
1232 HNS3_CFG_SPEED_ABILITY_S);
1233 cfg->umv_space = hns3_get_field(rte_le_to_cpu_32(req->param[1]),
1234 HNS3_CFG_UMV_TBL_SPACE_M,
1235 HNS3_CFG_UMV_TBL_SPACE_S);
1236 if (!cfg->umv_space)
1237 cfg->umv_space = HNS3_DEFAULT_UMV_SPACE_PER_PF;
1240 /* hns3_get_board_cfg: query the static parameter from NCL_config file in flash
1241 * @hw: pointer to struct hns3_hw
1242 * @hcfg: the config structure to be getted
1245 hns3_get_board_cfg(struct hns3_hw *hw, struct hns3_cfg *hcfg)
1247 struct hns3_cmd_desc desc[HNS3_PF_CFG_DESC_NUM];
1248 struct hns3_cfg_param_cmd *req;
1253 for (i = 0; i < HNS3_PF_CFG_DESC_NUM; i++) {
1255 req = (struct hns3_cfg_param_cmd *)desc[i].data;
1256 hns3_cmd_setup_basic_desc(&desc[i], HNS3_OPC_GET_CFG_PARAM,
1258 hns3_set_field(offset, HNS3_CFG_OFFSET_M, HNS3_CFG_OFFSET_S,
1259 i * HNS3_CFG_RD_LEN_BYTES);
1260 /* Len should be divided by 4 when send to hardware */
1261 hns3_set_field(offset, HNS3_CFG_RD_LEN_M, HNS3_CFG_RD_LEN_S,
1262 HNS3_CFG_RD_LEN_BYTES / HNS3_CFG_RD_LEN_UNIT);
1263 req->offset = rte_cpu_to_le_32(offset);
1266 ret = hns3_cmd_send(hw, desc, HNS3_PF_CFG_DESC_NUM);
1268 PMD_INIT_LOG(ERR, "get config failed %d.", ret);
1272 hns3_parse_cfg(hcfg, desc);
1278 hns3_parse_speed(int speed_cmd, uint32_t *speed)
1280 switch (speed_cmd) {
1281 case HNS3_CFG_SPEED_10M:
1282 *speed = ETH_SPEED_NUM_10M;
1284 case HNS3_CFG_SPEED_100M:
1285 *speed = ETH_SPEED_NUM_100M;
1287 case HNS3_CFG_SPEED_1G:
1288 *speed = ETH_SPEED_NUM_1G;
1290 case HNS3_CFG_SPEED_10G:
1291 *speed = ETH_SPEED_NUM_10G;
1293 case HNS3_CFG_SPEED_25G:
1294 *speed = ETH_SPEED_NUM_25G;
1296 case HNS3_CFG_SPEED_40G:
1297 *speed = ETH_SPEED_NUM_40G;
1299 case HNS3_CFG_SPEED_50G:
1300 *speed = ETH_SPEED_NUM_50G;
1302 case HNS3_CFG_SPEED_100G:
1303 *speed = ETH_SPEED_NUM_100G;
1313 hns3_get_board_configuration(struct hns3_hw *hw)
1315 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
1316 struct hns3_pf *pf = &hns->pf;
1317 struct hns3_cfg cfg;
1320 ret = hns3_get_board_cfg(hw, &cfg);
1322 PMD_INIT_LOG(ERR, "get board config failed %d", ret);
1326 if (cfg.media_type == HNS3_MEDIA_TYPE_COPPER) {
1327 PMD_INIT_LOG(ERR, "media type is copper, not supported.");
1331 hw->mac.media_type = cfg.media_type;
1332 hw->rss_size_max = cfg.rss_size_max;
1333 hw->rx_buf_len = cfg.rx_buf_len;
1334 memcpy(hw->mac.mac_addr, cfg.mac_addr, RTE_ETHER_ADDR_LEN);
1335 hw->mac.phy_addr = cfg.phy_addr;
1336 hw->mac.default_addr_setted = false;
1337 hw->num_tx_desc = cfg.tqp_desc_num;
1338 hw->num_rx_desc = cfg.tqp_desc_num;
1339 hw->dcb_info.num_pg = 1;
1340 hw->dcb_info.hw_pfc_map = 0;
1342 ret = hns3_parse_speed(cfg.default_speed, &hw->mac.link_speed);
1344 PMD_INIT_LOG(ERR, "Get wrong speed %d, ret = %d",
1345 cfg.default_speed, ret);
1349 pf->tc_max = cfg.tc_num;
1350 if (pf->tc_max > HNS3_MAX_TC_NUM || pf->tc_max < 1) {
1351 PMD_INIT_LOG(WARNING,
1352 "Get TC num(%u) from flash, set TC num to 1",
1357 /* Dev does not support DCB */
1358 if (!hns3_dev_dcb_supported(hw)) {
1362 pf->pfc_max = pf->tc_max;
1364 hw->dcb_info.num_tc = 1;
1365 hw->alloc_rss_size = RTE_MIN(hw->rss_size_max,
1366 hw->tqps_num / hw->dcb_info.num_tc);
1367 hns3_set_bit(hw->hw_tc_map, 0, 1);
1368 pf->tx_sch_mode = HNS3_FLAG_TC_BASE_SCH_MODE;
1370 pf->wanted_umv_size = cfg.umv_space;
1376 hns3_get_configuration(struct hns3_hw *hw)
1380 ret = hns3_query_function_status(hw);
1382 PMD_INIT_LOG(ERR, "Failed to query function status: %d.", ret);
1386 /* Get pf resource */
1387 ret = hns3_query_pf_resource(hw);
1389 PMD_INIT_LOG(ERR, "Failed to query pf resource: %d", ret);
1393 ret = hns3_get_board_configuration(hw);
1395 PMD_INIT_LOG(ERR, "Failed to get board configuration: %d", ret);
1403 hns3_map_tqps_to_func(struct hns3_hw *hw, uint16_t func_id, uint16_t tqp_pid,
1404 uint16_t tqp_vid, bool is_pf)
1406 struct hns3_tqp_map_cmd *req;
1407 struct hns3_cmd_desc desc;
1410 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_SET_TQP_MAP, false);
1412 req = (struct hns3_tqp_map_cmd *)desc.data;
1413 req->tqp_id = rte_cpu_to_le_16(tqp_pid);
1414 req->tqp_vf = func_id;
1415 req->tqp_flag = 1 << HNS3_TQP_MAP_EN_B;
1417 req->tqp_flag |= (1 << HNS3_TQP_MAP_TYPE_B);
1418 req->tqp_vid = rte_cpu_to_le_16(tqp_vid);
1420 ret = hns3_cmd_send(hw, &desc, 1);
1422 PMD_INIT_LOG(ERR, "TQP map failed %d", ret);
1428 hns3_map_tqp(struct hns3_hw *hw)
1430 uint16_t tqps_num = hw->total_tqps_num;
1438 * In current version VF is not supported when PF is driven by DPDK
1439 * driver, so we allocate tqps to PF as much as possible.
1442 num = DIV_ROUND_UP(hw->total_tqps_num, HNS3_MAX_TQP_NUM_PER_FUNC);
1443 for (func_id = 0; func_id < num; func_id++) {
1445 i < HNS3_MAX_TQP_NUM_PER_FUNC && tqp_id < tqps_num; i++) {
1446 ret = hns3_map_tqps_to_func(hw, func_id, tqp_id++, i,
1457 hns3_cfg_mac_speed_dup_hw(struct hns3_hw *hw, uint32_t speed, uint8_t duplex)
1459 struct hns3_config_mac_speed_dup_cmd *req;
1460 struct hns3_cmd_desc desc;
1463 req = (struct hns3_config_mac_speed_dup_cmd *)desc.data;
1465 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_CONFIG_SPEED_DUP, false);
1467 hns3_set_bit(req->speed_dup, HNS3_CFG_DUPLEX_B, !!duplex ? 1 : 0);
1470 case ETH_SPEED_NUM_10M:
1471 hns3_set_field(req->speed_dup, HNS3_CFG_SPEED_M,
1472 HNS3_CFG_SPEED_S, HNS3_CFG_SPEED_10M);
1474 case ETH_SPEED_NUM_100M:
1475 hns3_set_field(req->speed_dup, HNS3_CFG_SPEED_M,
1476 HNS3_CFG_SPEED_S, HNS3_CFG_SPEED_100M);
1478 case ETH_SPEED_NUM_1G:
1479 hns3_set_field(req->speed_dup, HNS3_CFG_SPEED_M,
1480 HNS3_CFG_SPEED_S, HNS3_CFG_SPEED_1G);
1482 case ETH_SPEED_NUM_10G:
1483 hns3_set_field(req->speed_dup, HNS3_CFG_SPEED_M,
1484 HNS3_CFG_SPEED_S, HNS3_CFG_SPEED_10G);
1486 case ETH_SPEED_NUM_25G:
1487 hns3_set_field(req->speed_dup, HNS3_CFG_SPEED_M,
1488 HNS3_CFG_SPEED_S, HNS3_CFG_SPEED_25G);
1490 case ETH_SPEED_NUM_40G:
1491 hns3_set_field(req->speed_dup, HNS3_CFG_SPEED_M,
1492 HNS3_CFG_SPEED_S, HNS3_CFG_SPEED_40G);
1494 case ETH_SPEED_NUM_50G:
1495 hns3_set_field(req->speed_dup, HNS3_CFG_SPEED_M,
1496 HNS3_CFG_SPEED_S, HNS3_CFG_SPEED_50G);
1498 case ETH_SPEED_NUM_100G:
1499 hns3_set_field(req->speed_dup, HNS3_CFG_SPEED_M,
1500 HNS3_CFG_SPEED_S, HNS3_CFG_SPEED_100G);
1503 PMD_INIT_LOG(ERR, "invalid speed (%u)", speed);
1507 hns3_set_bit(req->mac_change_fec_en, HNS3_CFG_MAC_SPEED_CHANGE_EN_B, 1);
1509 ret = hns3_cmd_send(hw, &desc, 1);
1511 PMD_INIT_LOG(ERR, "mac speed/duplex config cmd failed %d", ret);
1517 hns3_tx_buffer_calc(struct hns3_hw *hw, struct hns3_pkt_buf_alloc *buf_alloc)
1519 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
1520 struct hns3_pf *pf = &hns->pf;
1521 struct hns3_priv_buf *priv;
1522 uint32_t i, total_size;
1524 total_size = pf->pkt_buf_size;
1526 /* alloc tx buffer for all enabled tc */
1527 for (i = 0; i < HNS3_MAX_TC_NUM; i++) {
1528 priv = &buf_alloc->priv_buf[i];
1530 if (hw->hw_tc_map & BIT(i)) {
1531 if (total_size < pf->tx_buf_size)
1534 priv->tx_buf_size = pf->tx_buf_size;
1536 priv->tx_buf_size = 0;
1538 total_size -= priv->tx_buf_size;
1545 hns3_tx_buffer_alloc(struct hns3_hw *hw, struct hns3_pkt_buf_alloc *buf_alloc)
1547 /* TX buffer size is unit by 128 byte */
1548 #define HNS3_BUF_SIZE_UNIT_SHIFT 7
1549 #define HNS3_BUF_SIZE_UPDATE_EN_MSK BIT(15)
1550 struct hns3_tx_buff_alloc_cmd *req;
1551 struct hns3_cmd_desc desc;
1556 req = (struct hns3_tx_buff_alloc_cmd *)desc.data;
1558 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_TX_BUFF_ALLOC, 0);
1559 for (i = 0; i < HNS3_MAX_TC_NUM; i++) {
1560 buf_size = buf_alloc->priv_buf[i].tx_buf_size;
1562 buf_size = buf_size >> HNS3_BUF_SIZE_UNIT_SHIFT;
1563 req->tx_pkt_buff[i] = rte_cpu_to_le_16(buf_size |
1564 HNS3_BUF_SIZE_UPDATE_EN_MSK);
1567 ret = hns3_cmd_send(hw, &desc, 1);
1569 PMD_INIT_LOG(ERR, "tx buffer alloc cmd failed %d", ret);
1575 hns3_get_tc_num(struct hns3_hw *hw)
1580 for (i = 0; i < HNS3_MAX_TC_NUM; i++)
1581 if (hw->hw_tc_map & BIT(i))
1587 hns3_get_rx_priv_buff_alloced(struct hns3_pkt_buf_alloc *buf_alloc)
1589 struct hns3_priv_buf *priv;
1590 uint32_t rx_priv = 0;
1593 for (i = 0; i < HNS3_MAX_TC_NUM; i++) {
1594 priv = &buf_alloc->priv_buf[i];
1596 rx_priv += priv->buf_size;
1602 hns3_get_tx_buff_alloced(struct hns3_pkt_buf_alloc *buf_alloc)
1604 uint32_t total_tx_size = 0;
1607 for (i = 0; i < HNS3_MAX_TC_NUM; i++)
1608 total_tx_size += buf_alloc->priv_buf[i].tx_buf_size;
1610 return total_tx_size;
1613 /* Get the number of pfc enabled TCs, which have private buffer */
1615 hns3_get_pfc_priv_num(struct hns3_hw *hw, struct hns3_pkt_buf_alloc *buf_alloc)
1617 struct hns3_priv_buf *priv;
1621 for (i = 0; i < HNS3_MAX_TC_NUM; i++) {
1622 priv = &buf_alloc->priv_buf[i];
1623 if ((hw->dcb_info.hw_pfc_map & BIT(i)) && priv->enable)
1630 /* Get the number of pfc disabled TCs, which have private buffer */
1632 hns3_get_no_pfc_priv_num(struct hns3_hw *hw,
1633 struct hns3_pkt_buf_alloc *buf_alloc)
1635 struct hns3_priv_buf *priv;
1639 for (i = 0; i < HNS3_MAX_TC_NUM; i++) {
1640 priv = &buf_alloc->priv_buf[i];
1641 if (hw->hw_tc_map & BIT(i) &&
1642 !(hw->dcb_info.hw_pfc_map & BIT(i)) && priv->enable)
1650 hns3_is_rx_buf_ok(struct hns3_hw *hw, struct hns3_pkt_buf_alloc *buf_alloc,
1653 uint32_t shared_buf_min, shared_buf_tc, shared_std, hi_thrd, lo_thrd;
1654 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
1655 struct hns3_pf *pf = &hns->pf;
1656 uint32_t shared_buf, aligned_mps;
1661 tc_num = hns3_get_tc_num(hw);
1662 aligned_mps = roundup(pf->mps, HNS3_BUF_SIZE_UNIT);
1664 if (hns3_dev_dcb_supported(hw))
1665 shared_buf_min = HNS3_BUF_MUL_BY * aligned_mps +
1668 shared_buf_min = aligned_mps + HNS3_NON_DCB_ADDITIONAL_BUF
1671 shared_buf_tc = tc_num * aligned_mps + aligned_mps;
1672 shared_std = roundup(max_t(uint32_t, shared_buf_min, shared_buf_tc),
1673 HNS3_BUF_SIZE_UNIT);
1675 rx_priv = hns3_get_rx_priv_buff_alloced(buf_alloc);
1676 if (rx_all < rx_priv + shared_std)
1679 shared_buf = rounddown(rx_all - rx_priv, HNS3_BUF_SIZE_UNIT);
1680 buf_alloc->s_buf.buf_size = shared_buf;
1681 if (hns3_dev_dcb_supported(hw)) {
1682 buf_alloc->s_buf.self.high = shared_buf - pf->dv_buf_size;
1683 buf_alloc->s_buf.self.low = buf_alloc->s_buf.self.high
1684 - roundup(aligned_mps / HNS3_BUF_DIV_BY,
1685 HNS3_BUF_SIZE_UNIT);
1687 buf_alloc->s_buf.self.high =
1688 aligned_mps + HNS3_NON_DCB_ADDITIONAL_BUF;
1689 buf_alloc->s_buf.self.low = aligned_mps;
1692 if (hns3_dev_dcb_supported(hw)) {
1693 hi_thrd = shared_buf - pf->dv_buf_size;
1695 if (tc_num <= NEED_RESERVE_TC_NUM)
1696 hi_thrd = hi_thrd * BUF_RESERVE_PERCENT
1700 hi_thrd = hi_thrd / tc_num;
1702 hi_thrd = max_t(uint32_t, hi_thrd,
1703 HNS3_BUF_MUL_BY * aligned_mps);
1704 hi_thrd = rounddown(hi_thrd, HNS3_BUF_SIZE_UNIT);
1705 lo_thrd = hi_thrd - aligned_mps / HNS3_BUF_DIV_BY;
1707 hi_thrd = aligned_mps + HNS3_NON_DCB_ADDITIONAL_BUF;
1708 lo_thrd = aligned_mps;
1711 for (i = 0; i < HNS3_MAX_TC_NUM; i++) {
1712 buf_alloc->s_buf.tc_thrd[i].low = lo_thrd;
1713 buf_alloc->s_buf.tc_thrd[i].high = hi_thrd;
1720 hns3_rx_buf_calc_all(struct hns3_hw *hw, bool max,
1721 struct hns3_pkt_buf_alloc *buf_alloc)
1723 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
1724 struct hns3_pf *pf = &hns->pf;
1725 struct hns3_priv_buf *priv;
1726 uint32_t aligned_mps;
1730 rx_all = pf->pkt_buf_size - hns3_get_tx_buff_alloced(buf_alloc);
1731 aligned_mps = roundup(pf->mps, HNS3_BUF_SIZE_UNIT);
1733 for (i = 0; i < HNS3_MAX_TC_NUM; i++) {
1734 priv = &buf_alloc->priv_buf[i];
1741 if (!(hw->hw_tc_map & BIT(i)))
1745 if (hw->dcb_info.hw_pfc_map & BIT(i)) {
1746 priv->wl.low = max ? aligned_mps : HNS3_BUF_SIZE_UNIT;
1747 priv->wl.high = roundup(priv->wl.low + aligned_mps,
1748 HNS3_BUF_SIZE_UNIT);
1751 priv->wl.high = max ? (aligned_mps * HNS3_BUF_MUL_BY) :
1755 priv->buf_size = priv->wl.high + pf->dv_buf_size;
1758 return hns3_is_rx_buf_ok(hw, buf_alloc, rx_all);
1762 hns3_drop_nopfc_buf_till_fit(struct hns3_hw *hw,
1763 struct hns3_pkt_buf_alloc *buf_alloc)
1765 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
1766 struct hns3_pf *pf = &hns->pf;
1767 struct hns3_priv_buf *priv;
1768 int no_pfc_priv_num;
1773 rx_all = pf->pkt_buf_size - hns3_get_tx_buff_alloced(buf_alloc);
1774 no_pfc_priv_num = hns3_get_no_pfc_priv_num(hw, buf_alloc);
1776 /* let the last to be cleared first */
1777 for (i = HNS3_MAX_TC_NUM - 1; i >= 0; i--) {
1778 priv = &buf_alloc->priv_buf[i];
1779 mask = BIT((uint8_t)i);
1781 if (hw->hw_tc_map & mask &&
1782 !(hw->dcb_info.hw_pfc_map & mask)) {
1783 /* Clear the no pfc TC private buffer */
1791 if (hns3_is_rx_buf_ok(hw, buf_alloc, rx_all) ||
1792 no_pfc_priv_num == 0)
1796 return hns3_is_rx_buf_ok(hw, buf_alloc, rx_all);
1800 hns3_drop_pfc_buf_till_fit(struct hns3_hw *hw,
1801 struct hns3_pkt_buf_alloc *buf_alloc)
1803 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
1804 struct hns3_pf *pf = &hns->pf;
1805 struct hns3_priv_buf *priv;
1811 rx_all = pf->pkt_buf_size - hns3_get_tx_buff_alloced(buf_alloc);
1812 pfc_priv_num = hns3_get_pfc_priv_num(hw, buf_alloc);
1814 /* let the last to be cleared first */
1815 for (i = HNS3_MAX_TC_NUM - 1; i >= 0; i--) {
1816 priv = &buf_alloc->priv_buf[i];
1817 mask = BIT((uint8_t)i);
1819 if (hw->hw_tc_map & mask &&
1820 hw->dcb_info.hw_pfc_map & mask) {
1821 /* Reduce the number of pfc TC with private buffer */
1828 if (hns3_is_rx_buf_ok(hw, buf_alloc, rx_all) ||
1833 return hns3_is_rx_buf_ok(hw, buf_alloc, rx_all);
1837 hns3_only_alloc_priv_buff(struct hns3_hw *hw,
1838 struct hns3_pkt_buf_alloc *buf_alloc)
1840 #define COMPENSATE_BUFFER 0x3C00
1841 #define COMPENSATE_HALF_MPS_NUM 5
1842 #define PRIV_WL_GAP 0x1800
1843 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
1844 struct hns3_pf *pf = &hns->pf;
1845 uint32_t tc_num = hns3_get_tc_num(hw);
1846 uint32_t half_mps = pf->mps >> 1;
1847 struct hns3_priv_buf *priv;
1848 uint32_t min_rx_priv;
1852 rx_priv = pf->pkt_buf_size - hns3_get_tx_buff_alloced(buf_alloc);
1854 rx_priv = rx_priv / tc_num;
1856 if (tc_num <= NEED_RESERVE_TC_NUM)
1857 rx_priv = rx_priv * BUF_RESERVE_PERCENT / BUF_MAX_PERCENT;
1860 * Minimum value of private buffer in rx direction (min_rx_priv) is
1861 * equal to "DV + 2.5 * MPS + 15KB". Driver only allocates rx private
1862 * buffer if rx_priv is greater than min_rx_priv.
1864 min_rx_priv = pf->dv_buf_size + COMPENSATE_BUFFER +
1865 COMPENSATE_HALF_MPS_NUM * half_mps;
1866 min_rx_priv = roundup(min_rx_priv, HNS3_BUF_SIZE_UNIT);
1867 rx_priv = rounddown(rx_priv, HNS3_BUF_SIZE_UNIT);
1869 if (rx_priv < min_rx_priv)
1872 for (i = 0; i < HNS3_MAX_TC_NUM; i++) {
1873 priv = &buf_alloc->priv_buf[i];
1880 if (!(hw->hw_tc_map & BIT(i)))
1884 priv->buf_size = rx_priv;
1885 priv->wl.high = rx_priv - pf->dv_buf_size;
1886 priv->wl.low = priv->wl.high - PRIV_WL_GAP;
1889 buf_alloc->s_buf.buf_size = 0;
1895 * hns3_rx_buffer_calc: calculate the rx private buffer size for all TCs
1896 * @hw: pointer to struct hns3_hw
1897 * @buf_alloc: pointer to buffer calculation data
1898 * @return: 0: calculate sucessful, negative: fail
1901 hns3_rx_buffer_calc(struct hns3_hw *hw, struct hns3_pkt_buf_alloc *buf_alloc)
1903 /* When DCB is not supported, rx private buffer is not allocated. */
1904 if (!hns3_dev_dcb_supported(hw)) {
1905 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
1906 struct hns3_pf *pf = &hns->pf;
1907 uint32_t rx_all = pf->pkt_buf_size;
1909 rx_all -= hns3_get_tx_buff_alloced(buf_alloc);
1910 if (!hns3_is_rx_buf_ok(hw, buf_alloc, rx_all))
1917 * Try to allocate privated packet buffer for all TCs without share
1920 if (hns3_only_alloc_priv_buff(hw, buf_alloc))
1924 * Try to allocate privated packet buffer for all TCs with share
1927 if (hns3_rx_buf_calc_all(hw, true, buf_alloc))
1931 * For different application scenes, the enabled port number, TC number
1932 * and no_drop TC number are different. In order to obtain the better
1933 * performance, software could allocate the buffer size and configure
1934 * the waterline by tring to decrease the private buffer size according
1935 * to the order, namely, waterline of valided tc, pfc disabled tc, pfc
1938 if (hns3_rx_buf_calc_all(hw, false, buf_alloc))
1941 if (hns3_drop_nopfc_buf_till_fit(hw, buf_alloc))
1944 if (hns3_drop_pfc_buf_till_fit(hw, buf_alloc))
1951 hns3_rx_priv_buf_alloc(struct hns3_hw *hw, struct hns3_pkt_buf_alloc *buf_alloc)
1953 struct hns3_rx_priv_buff_cmd *req;
1954 struct hns3_cmd_desc desc;
1959 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_RX_PRIV_BUFF_ALLOC, false);
1960 req = (struct hns3_rx_priv_buff_cmd *)desc.data;
1962 /* Alloc private buffer TCs */
1963 for (i = 0; i < HNS3_MAX_TC_NUM; i++) {
1964 struct hns3_priv_buf *priv = &buf_alloc->priv_buf[i];
1967 rte_cpu_to_le_16(priv->buf_size >> HNS3_BUF_UNIT_S);
1968 req->buf_num[i] |= rte_cpu_to_le_16(1 << HNS3_TC0_PRI_BUF_EN_B);
1971 buf_size = buf_alloc->s_buf.buf_size;
1972 req->shared_buf = rte_cpu_to_le_16((buf_size >> HNS3_BUF_UNIT_S) |
1973 (1 << HNS3_TC0_PRI_BUF_EN_B));
1975 ret = hns3_cmd_send(hw, &desc, 1);
1977 PMD_INIT_LOG(ERR, "rx private buffer alloc cmd failed %d", ret);
1983 hns3_rx_priv_wl_config(struct hns3_hw *hw, struct hns3_pkt_buf_alloc *buf_alloc)
1985 #define HNS3_RX_PRIV_WL_ALLOC_DESC_NUM 2
1986 struct hns3_rx_priv_wl_buf *req;
1987 struct hns3_priv_buf *priv;
1988 struct hns3_cmd_desc desc[HNS3_RX_PRIV_WL_ALLOC_DESC_NUM];
1992 for (i = 0; i < HNS3_RX_PRIV_WL_ALLOC_DESC_NUM; i++) {
1993 hns3_cmd_setup_basic_desc(&desc[i], HNS3_OPC_RX_PRIV_WL_ALLOC,
1995 req = (struct hns3_rx_priv_wl_buf *)desc[i].data;
1997 /* The first descriptor set the NEXT bit to 1 */
1999 desc[i].flag |= rte_cpu_to_le_16(HNS3_CMD_FLAG_NEXT);
2001 desc[i].flag &= ~rte_cpu_to_le_16(HNS3_CMD_FLAG_NEXT);
2003 for (j = 0; j < HNS3_TC_NUM_ONE_DESC; j++) {
2004 uint32_t idx = i * HNS3_TC_NUM_ONE_DESC + j;
2006 priv = &buf_alloc->priv_buf[idx];
2007 req->tc_wl[j].high = rte_cpu_to_le_16(priv->wl.high >>
2009 req->tc_wl[j].high |=
2010 rte_cpu_to_le_16(BIT(HNS3_RX_PRIV_EN_B));
2011 req->tc_wl[j].low = rte_cpu_to_le_16(priv->wl.low >>
2013 req->tc_wl[j].low |=
2014 rte_cpu_to_le_16(BIT(HNS3_RX_PRIV_EN_B));
2018 /* Send 2 descriptor at one time */
2019 ret = hns3_cmd_send(hw, desc, HNS3_RX_PRIV_WL_ALLOC_DESC_NUM);
2021 PMD_INIT_LOG(ERR, "rx private waterline config cmd failed %d",
2027 hns3_common_thrd_config(struct hns3_hw *hw,
2028 struct hns3_pkt_buf_alloc *buf_alloc)
2030 #define HNS3_RX_COM_THRD_ALLOC_DESC_NUM 2
2031 struct hns3_shared_buf *s_buf = &buf_alloc->s_buf;
2032 struct hns3_rx_com_thrd *req;
2033 struct hns3_cmd_desc desc[HNS3_RX_COM_THRD_ALLOC_DESC_NUM];
2034 struct hns3_tc_thrd *tc;
2039 for (i = 0; i < HNS3_RX_COM_THRD_ALLOC_DESC_NUM; i++) {
2040 hns3_cmd_setup_basic_desc(&desc[i], HNS3_OPC_RX_COM_THRD_ALLOC,
2042 req = (struct hns3_rx_com_thrd *)&desc[i].data;
2044 /* The first descriptor set the NEXT bit to 1 */
2046 desc[i].flag |= rte_cpu_to_le_16(HNS3_CMD_FLAG_NEXT);
2048 desc[i].flag &= ~rte_cpu_to_le_16(HNS3_CMD_FLAG_NEXT);
2050 for (j = 0; j < HNS3_TC_NUM_ONE_DESC; j++) {
2051 tc_idx = i * HNS3_TC_NUM_ONE_DESC + j;
2052 tc = &s_buf->tc_thrd[tc_idx];
2054 req->com_thrd[j].high =
2055 rte_cpu_to_le_16(tc->high >> HNS3_BUF_UNIT_S);
2056 req->com_thrd[j].high |=
2057 rte_cpu_to_le_16(BIT(HNS3_RX_PRIV_EN_B));
2058 req->com_thrd[j].low =
2059 rte_cpu_to_le_16(tc->low >> HNS3_BUF_UNIT_S);
2060 req->com_thrd[j].low |=
2061 rte_cpu_to_le_16(BIT(HNS3_RX_PRIV_EN_B));
2065 /* Send 2 descriptors at one time */
2066 ret = hns3_cmd_send(hw, desc, HNS3_RX_COM_THRD_ALLOC_DESC_NUM);
2068 PMD_INIT_LOG(ERR, "common threshold config cmd failed %d", ret);
2074 hns3_common_wl_config(struct hns3_hw *hw, struct hns3_pkt_buf_alloc *buf_alloc)
2076 struct hns3_shared_buf *buf = &buf_alloc->s_buf;
2077 struct hns3_rx_com_wl *req;
2078 struct hns3_cmd_desc desc;
2081 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_RX_COM_WL_ALLOC, false);
2083 req = (struct hns3_rx_com_wl *)desc.data;
2084 req->com_wl.high = rte_cpu_to_le_16(buf->self.high >> HNS3_BUF_UNIT_S);
2085 req->com_wl.high |= rte_cpu_to_le_16(BIT(HNS3_RX_PRIV_EN_B));
2087 req->com_wl.low = rte_cpu_to_le_16(buf->self.low >> HNS3_BUF_UNIT_S);
2088 req->com_wl.low |= rte_cpu_to_le_16(BIT(HNS3_RX_PRIV_EN_B));
2090 ret = hns3_cmd_send(hw, &desc, 1);
2092 PMD_INIT_LOG(ERR, "common waterline config cmd failed %d", ret);
2098 hns3_buffer_alloc(struct hns3_hw *hw)
2100 struct hns3_pkt_buf_alloc pkt_buf;
2103 memset(&pkt_buf, 0, sizeof(pkt_buf));
2104 ret = hns3_tx_buffer_calc(hw, &pkt_buf);
2107 "could not calc tx buffer size for all TCs %d",
2112 ret = hns3_tx_buffer_alloc(hw, &pkt_buf);
2114 PMD_INIT_LOG(ERR, "could not alloc tx buffers %d", ret);
2118 ret = hns3_rx_buffer_calc(hw, &pkt_buf);
2121 "could not calc rx priv buffer size for all TCs %d",
2126 ret = hns3_rx_priv_buf_alloc(hw, &pkt_buf);
2128 PMD_INIT_LOG(ERR, "could not alloc rx priv buffer %d", ret);
2132 if (hns3_dev_dcb_supported(hw)) {
2133 ret = hns3_rx_priv_wl_config(hw, &pkt_buf);
2136 "could not configure rx private waterline %d",
2141 ret = hns3_common_thrd_config(hw, &pkt_buf);
2144 "could not configure common threshold %d",
2150 ret = hns3_common_wl_config(hw, &pkt_buf);
2152 PMD_INIT_LOG(ERR, "could not configure common waterline %d",
2159 hns3_mac_init(struct hns3_hw *hw)
2161 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
2162 struct hns3_mac *mac = &hw->mac;
2163 struct hns3_pf *pf = &hns->pf;
2166 pf->support_sfp_query = true;
2167 mac->link_duplex = ETH_LINK_FULL_DUPLEX;
2168 ret = hns3_cfg_mac_speed_dup_hw(hw, mac->link_speed, mac->link_duplex);
2170 PMD_INIT_LOG(ERR, "Config mac speed dup fail ret = %d", ret);
2174 mac->link_status = ETH_LINK_DOWN;
2176 return hns3_config_mtu(hw, pf->mps);
2180 hns3_get_mac_ethertype_cmd_status(uint16_t cmdq_resp, uint8_t resp_code)
2182 #define HNS3_ETHERTYPE_SUCCESS_ADD 0
2183 #define HNS3_ETHERTYPE_ALREADY_ADD 1
2184 #define HNS3_ETHERTYPE_MGR_TBL_OVERFLOW 2
2185 #define HNS3_ETHERTYPE_KEY_CONFLICT 3
2190 "cmdq execute failed for get_mac_ethertype_cmd_status, status=%d.\n",
2195 switch (resp_code) {
2196 case HNS3_ETHERTYPE_SUCCESS_ADD:
2197 case HNS3_ETHERTYPE_ALREADY_ADD:
2200 case HNS3_ETHERTYPE_MGR_TBL_OVERFLOW:
2202 "add mac ethertype failed for manager table overflow.");
2203 return_status = -EIO;
2205 case HNS3_ETHERTYPE_KEY_CONFLICT:
2206 PMD_INIT_LOG(ERR, "add mac ethertype failed for key conflict.");
2207 return_status = -EIO;
2211 "add mac ethertype failed for undefined, code=%d.",
2213 return_status = -EIO;
2216 return return_status;
2220 hns3_add_mgr_tbl(struct hns3_hw *hw,
2221 const struct hns3_mac_mgr_tbl_entry_cmd *req)
2223 struct hns3_cmd_desc desc;
2228 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_MAC_ETHTYPE_ADD, false);
2229 memcpy(desc.data, req, sizeof(struct hns3_mac_mgr_tbl_entry_cmd));
2231 ret = hns3_cmd_send(hw, &desc, 1);
2234 "add mac ethertype failed for cmd_send, ret =%d.",
2239 resp_code = (rte_le_to_cpu_32(desc.data[0]) >> 8) & 0xff;
2240 retval = rte_le_to_cpu_16(desc.retval);
2242 return hns3_get_mac_ethertype_cmd_status(retval, resp_code);
2246 hns3_prepare_mgr_tbl(struct hns3_mac_mgr_tbl_entry_cmd *mgr_table,
2247 int *table_item_num)
2249 struct hns3_mac_mgr_tbl_entry_cmd *tbl;
2252 * In current version, we add one item in management table as below:
2253 * 0x0180C200000E -- LLDP MC address
2256 tbl->flags = HNS3_MAC_MGR_MASK_VLAN_B;
2257 tbl->ethter_type = rte_cpu_to_le_16(HNS3_MAC_ETHERTYPE_LLDP);
2258 tbl->mac_addr_hi32 = rte_cpu_to_le_32(htonl(0x0180C200));
2259 tbl->mac_addr_lo16 = rte_cpu_to_le_16(htons(0x000E));
2260 tbl->i_port_bitmap = 0x1;
2261 *table_item_num = 1;
2265 hns3_init_mgr_tbl(struct hns3_hw *hw)
2267 #define HNS_MAC_MGR_TBL_MAX_SIZE 16
2268 struct hns3_mac_mgr_tbl_entry_cmd mgr_table[HNS_MAC_MGR_TBL_MAX_SIZE];
2273 memset(mgr_table, 0, sizeof(mgr_table));
2274 hns3_prepare_mgr_tbl(mgr_table, &table_item_num);
2275 for (i = 0; i < table_item_num; i++) {
2276 ret = hns3_add_mgr_tbl(hw, &mgr_table[i]);
2278 PMD_INIT_LOG(ERR, "add mac ethertype failed, ret =%d",
2288 hns3_promisc_param_init(struct hns3_promisc_param *param, bool en_uc,
2289 bool en_mc, bool en_bc, int vport_id)
2294 memset(param, 0, sizeof(struct hns3_promisc_param));
2296 param->enable = HNS3_PROMISC_EN_UC;
2298 param->enable |= HNS3_PROMISC_EN_MC;
2300 param->enable |= HNS3_PROMISC_EN_BC;
2301 param->vf_id = vport_id;
2305 hns3_cmd_set_promisc_mode(struct hns3_hw *hw, struct hns3_promisc_param *param)
2307 struct hns3_promisc_cfg_cmd *req;
2308 struct hns3_cmd_desc desc;
2311 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_CFG_PROMISC_MODE, false);
2313 req = (struct hns3_promisc_cfg_cmd *)desc.data;
2314 req->vf_id = param->vf_id;
2315 req->flag = (param->enable << HNS3_PROMISC_EN_B) |
2316 HNS3_PROMISC_TX_EN_B | HNS3_PROMISC_RX_EN_B;
2318 ret = hns3_cmd_send(hw, &desc, 1);
2320 PMD_INIT_LOG(ERR, "Set promisc mode fail, status is %d", ret);
2326 hns3_set_promisc_mode(struct hns3_hw *hw, bool en_uc_pmc, bool en_mc_pmc)
2328 struct hns3_promisc_param param;
2329 bool en_bc_pmc = true;
2334 * In current version VF is not supported when PF is driven by DPDK
2335 * driver, the PF-related vf_id is 0, just need to configure parameters
2340 hns3_promisc_param_init(¶m, en_uc_pmc, en_mc_pmc, en_bc_pmc, vf_id);
2341 ret = hns3_cmd_set_promisc_mode(hw, ¶m);
2349 hns3_get_sfp_speed(struct hns3_hw *hw, uint32_t *speed)
2351 struct hns3_sfp_speed_cmd *resp;
2352 struct hns3_cmd_desc desc;
2355 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_SFP_GET_SPEED, true);
2356 resp = (struct hns3_sfp_speed_cmd *)desc.data;
2357 ret = hns3_cmd_send(hw, &desc, 1);
2358 if (ret == -EOPNOTSUPP) {
2359 hns3_err(hw, "IMP do not support get SFP speed %d", ret);
2362 hns3_err(hw, "get sfp speed failed %d", ret);
2366 *speed = resp->sfp_speed;
2372 hns3_check_speed_dup(uint8_t duplex, uint32_t speed)
2374 if (!(speed == ETH_SPEED_NUM_10M || speed == ETH_SPEED_NUM_100M))
2375 duplex = ETH_LINK_FULL_DUPLEX;
2381 hns3_cfg_mac_speed_dup(struct hns3_hw *hw, uint32_t speed, uint8_t duplex)
2383 struct hns3_mac *mac = &hw->mac;
2386 duplex = hns3_check_speed_dup(duplex, speed);
2387 if (mac->link_speed == speed && mac->link_duplex == duplex)
2390 ret = hns3_cfg_mac_speed_dup_hw(hw, speed, duplex);
2394 mac->link_speed = speed;
2395 mac->link_duplex = duplex;
2401 hns3_update_speed_duplex(struct rte_eth_dev *eth_dev)
2403 struct hns3_adapter *hns = eth_dev->data->dev_private;
2404 struct hns3_hw *hw = &hns->hw;
2405 struct hns3_pf *pf = &hns->pf;
2409 /* If IMP do not support get SFP/qSFP speed, return directly */
2410 if (!pf->support_sfp_query)
2413 ret = hns3_get_sfp_speed(hw, &speed);
2414 if (ret == -EOPNOTSUPP) {
2415 pf->support_sfp_query = false;
2420 if (speed == ETH_SPEED_NUM_NONE)
2421 return 0; /* do nothing if no SFP */
2423 /* Config full duplex for SFP */
2424 return hns3_cfg_mac_speed_dup(hw, speed, ETH_LINK_FULL_DUPLEX);
2428 hns3_get_mac_link_status(struct hns3_hw *hw)
2430 struct hns3_link_status_cmd *req;
2431 struct hns3_cmd_desc desc;
2435 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_QUERY_LINK_STATUS, true);
2436 ret = hns3_cmd_send(hw, &desc, 1);
2438 hns3_err(hw, "get link status cmd failed %d", ret);
2442 req = (struct hns3_link_status_cmd *)desc.data;
2443 link_status = req->status & HNS3_LINK_STATUS_UP_M;
2445 return !!link_status;
2449 hns3_update_link_status(struct hns3_hw *hw)
2453 state = hns3_get_mac_link_status(hw);
2454 if (state != hw->mac.link_status)
2455 hw->mac.link_status = state;
2459 hns3_service_handler(void *param)
2461 struct rte_eth_dev *eth_dev = (struct rte_eth_dev *)param;
2462 struct hns3_adapter *hns = eth_dev->data->dev_private;
2463 struct hns3_hw *hw = &hns->hw;
2465 hns3_update_speed_duplex(eth_dev);
2466 hns3_update_link_status(hw);
2468 rte_eal_alarm_set(HNS3_SERVICE_INTERVAL, hns3_service_handler, eth_dev);
2472 hns3_init_hardware(struct hns3_adapter *hns)
2474 struct hns3_hw *hw = &hns->hw;
2477 ret = hns3_map_tqp(hw);
2479 PMD_INIT_LOG(ERR, "Failed to map tqp: %d", ret);
2483 ret = hns3_init_umv_space(hw);
2485 PMD_INIT_LOG(ERR, "Failed to init umv space: %d", ret);
2489 ret = hns3_mac_init(hw);
2491 PMD_INIT_LOG(ERR, "Failed to init MAC: %d", ret);
2495 ret = hns3_init_mgr_tbl(hw);
2497 PMD_INIT_LOG(ERR, "Failed to init manager table: %d", ret);
2501 ret = hns3_set_promisc_mode(hw, false, false);
2503 PMD_INIT_LOG(ERR, "Failed to set promisc mode: %d", ret);
2507 ret = hns3_init_fd_config(hns);
2509 PMD_INIT_LOG(ERR, "Failed to init flow director: %d", ret);
2513 ret = hns3_config_tso(hw, HNS3_TSO_MSS_MIN, HNS3_TSO_MSS_MAX);
2515 PMD_INIT_LOG(ERR, "Failed to config tso: %d", ret);
2519 ret = hns3_config_gro(hw, false);
2521 PMD_INIT_LOG(ERR, "Failed to config gro: %d", ret);
2527 hns3_uninit_umv_space(hw);
2532 hns3_init_pf(struct rte_eth_dev *eth_dev)
2534 struct rte_device *dev = eth_dev->device;
2535 struct rte_pci_device *pci_dev = RTE_DEV_TO_PCI(dev);
2536 struct hns3_adapter *hns = eth_dev->data->dev_private;
2537 struct hns3_hw *hw = &hns->hw;
2540 PMD_INIT_FUNC_TRACE();
2542 /* Get hardware io base address from pcie BAR2 IO space */
2543 hw->io_base = pci_dev->mem_resource[2].addr;
2545 /* Firmware command queue initialize */
2546 ret = hns3_cmd_init_queue(hw);
2548 PMD_INIT_LOG(ERR, "Failed to init cmd queue: %d", ret);
2549 goto err_cmd_init_queue;
2552 /* Firmware command initialize */
2553 ret = hns3_cmd_init(hw);
2555 PMD_INIT_LOG(ERR, "Failed to init cmd: %d", ret);
2559 /* Get configuration */
2560 ret = hns3_get_configuration(hw);
2562 PMD_INIT_LOG(ERR, "Failed to fetch configuration: %d", ret);
2563 goto err_get_config;
2566 ret = hns3_init_hardware(hns);
2568 PMD_INIT_LOG(ERR, "Failed to init hardware: %d", ret);
2569 goto err_get_config;
2572 /* Initialize flow director filter list & hash */
2573 ret = hns3_fdir_filter_init(hns);
2575 PMD_INIT_LOG(ERR, "Failed to alloc hashmap for fdir: %d", ret);
2579 hns3_set_default_rss_args(hw);
2584 hns3_uninit_umv_space(hw);
2587 hns3_cmd_uninit(hw);
2590 hns3_cmd_destroy_queue(hw);
2599 hns3_uninit_pf(struct rte_eth_dev *eth_dev)
2601 struct hns3_adapter *hns = eth_dev->data->dev_private;
2602 struct hns3_hw *hw = &hns->hw;
2604 PMD_INIT_FUNC_TRACE();
2606 hns3_rss_uninit(hns);
2607 hns3_fdir_filter_uninit(hns);
2608 hns3_uninit_umv_space(hw);
2609 hns3_cmd_uninit(hw);
2610 hns3_cmd_destroy_queue(hw);
2615 hns3_dev_close(struct rte_eth_dev *eth_dev)
2617 struct hns3_adapter *hns = eth_dev->data->dev_private;
2618 struct hns3_hw *hw = &hns->hw;
2620 hw->adapter_state = HNS3_NIC_CLOSING;
2621 rte_eal_alarm_cancel(hns3_service_handler, eth_dev);
2623 hns3_configure_all_mc_mac_addr(hns, true);
2624 hns3_uninit_pf(eth_dev);
2625 rte_free(eth_dev->process_private);
2626 eth_dev->process_private = NULL;
2627 hw->adapter_state = HNS3_NIC_CLOSED;
2630 static const struct eth_dev_ops hns3_eth_dev_ops = {
2631 .dev_close = hns3_dev_close,
2632 .mtu_set = hns3_dev_mtu_set,
2633 .dev_infos_get = hns3_dev_infos_get,
2634 .fw_version_get = hns3_fw_version_get,
2635 .mac_addr_add = hns3_add_mac_addr,
2636 .mac_addr_remove = hns3_remove_mac_addr,
2637 .mac_addr_set = hns3_set_default_mac_addr,
2638 .set_mc_addr_list = hns3_set_mc_mac_addr_list,
2639 .link_update = hns3_dev_link_update,
2640 .rss_hash_update = hns3_dev_rss_hash_update,
2641 .rss_hash_conf_get = hns3_dev_rss_hash_conf_get,
2642 .reta_update = hns3_dev_rss_reta_update,
2643 .reta_query = hns3_dev_rss_reta_query,
2644 .filter_ctrl = hns3_dev_filter_ctrl,
2648 hns3_dev_init(struct rte_eth_dev *eth_dev)
2650 struct hns3_adapter *hns = eth_dev->data->dev_private;
2651 struct hns3_hw *hw = &hns->hw;
2654 PMD_INIT_FUNC_TRACE();
2655 eth_dev->process_private = (struct hns3_process_private *)
2656 rte_zmalloc_socket("hns3_filter_list",
2657 sizeof(struct hns3_process_private),
2658 RTE_CACHE_LINE_SIZE, eth_dev->device->numa_node);
2659 if (eth_dev->process_private == NULL) {
2660 PMD_INIT_LOG(ERR, "Failed to alloc memory for process private");
2663 /* initialize flow filter lists */
2664 hns3_filterlist_init(eth_dev);
2666 eth_dev->dev_ops = &hns3_eth_dev_ops;
2667 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
2670 hw->adapter_state = HNS3_NIC_UNINITIALIZED;
2672 hw->data = eth_dev->data;
2675 * Set default max packet size according to the mtu
2676 * default vale in DPDK frame.
2678 hns->pf.mps = hw->data->mtu + HNS3_ETH_OVERHEAD;
2680 ret = hns3_init_pf(eth_dev);
2682 PMD_INIT_LOG(ERR, "Failed to init pf: %d", ret);
2686 /* Allocate memory for storing MAC addresses */
2687 eth_dev->data->mac_addrs = rte_zmalloc("hns3-mac",
2688 sizeof(struct rte_ether_addr) *
2689 HNS3_UC_MACADDR_NUM, 0);
2690 if (eth_dev->data->mac_addrs == NULL) {
2691 PMD_INIT_LOG(ERR, "Failed to allocate %zx bytes needed "
2692 "to store MAC addresses",
2693 sizeof(struct rte_ether_addr) *
2694 HNS3_UC_MACADDR_NUM);
2696 goto err_rte_zmalloc;
2699 rte_ether_addr_copy((struct rte_ether_addr *)hw->mac.mac_addr,
2700 ð_dev->data->mac_addrs[0]);
2702 hw->adapter_state = HNS3_NIC_INITIALIZED;
2704 * Pass the information to the rte_eth_dev_close() that it should also
2705 * release the private port resources.
2707 eth_dev->data->dev_flags |= RTE_ETH_DEV_CLOSE_REMOVE;
2709 rte_eal_alarm_set(HNS3_SERVICE_INTERVAL, hns3_service_handler, eth_dev);
2710 hns3_info(hw, "hns3 dev initialization successful!");
2714 hns3_uninit_pf(eth_dev);
2717 eth_dev->dev_ops = NULL;
2718 eth_dev->rx_pkt_burst = NULL;
2719 eth_dev->tx_pkt_burst = NULL;
2720 eth_dev->tx_pkt_prepare = NULL;
2721 rte_free(eth_dev->process_private);
2722 eth_dev->process_private = NULL;
2727 hns3_dev_uninit(struct rte_eth_dev *eth_dev)
2729 struct hns3_adapter *hns = eth_dev->data->dev_private;
2730 struct hns3_hw *hw = &hns->hw;
2732 PMD_INIT_FUNC_TRACE();
2734 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
2737 eth_dev->dev_ops = NULL;
2738 eth_dev->rx_pkt_burst = NULL;
2739 eth_dev->tx_pkt_burst = NULL;
2740 eth_dev->tx_pkt_prepare = NULL;
2741 if (hw->adapter_state < HNS3_NIC_CLOSING)
2742 hns3_dev_close(eth_dev);
2744 hw->adapter_state = HNS3_NIC_REMOVED;
2749 eth_hns3_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
2750 struct rte_pci_device *pci_dev)
2752 return rte_eth_dev_pci_generic_probe(pci_dev,
2753 sizeof(struct hns3_adapter),
2758 eth_hns3_pci_remove(struct rte_pci_device *pci_dev)
2760 return rte_eth_dev_pci_generic_remove(pci_dev, hns3_dev_uninit);
2763 static const struct rte_pci_id pci_id_hns3_map[] = {
2764 { RTE_PCI_DEVICE(PCI_VENDOR_ID_HUAWEI, HNS3_DEV_ID_GE) },
2765 { RTE_PCI_DEVICE(PCI_VENDOR_ID_HUAWEI, HNS3_DEV_ID_25GE) },
2766 { RTE_PCI_DEVICE(PCI_VENDOR_ID_HUAWEI, HNS3_DEV_ID_25GE_RDMA) },
2767 { RTE_PCI_DEVICE(PCI_VENDOR_ID_HUAWEI, HNS3_DEV_ID_50GE_RDMA) },
2768 { RTE_PCI_DEVICE(PCI_VENDOR_ID_HUAWEI, HNS3_DEV_ID_100G_RDMA_MACSEC) },
2769 { .vendor_id = 0, /* sentinel */ },
2772 static struct rte_pci_driver rte_hns3_pmd = {
2773 .id_table = pci_id_hns3_map,
2774 .drv_flags = RTE_PCI_DRV_NEED_MAPPING,
2775 .probe = eth_hns3_pci_probe,
2776 .remove = eth_hns3_pci_remove,
2779 RTE_PMD_REGISTER_PCI(net_hns3, rte_hns3_pmd);
2780 RTE_PMD_REGISTER_PCI_TABLE(net_hns3, pci_id_hns3_map);
2781 RTE_PMD_REGISTER_KMOD_DEP(net_hns3, "* igb_uio | vfio-pci");
2783 RTE_INIT(hns3_init_log)
2785 hns3_logtype_init = rte_log_register("pmd.net.hns3.init");
2786 if (hns3_logtype_init >= 0)
2787 rte_log_set_level(hns3_logtype_init, RTE_LOG_NOTICE);
2788 hns3_logtype_driver = rte_log_register("pmd.net.hns3.driver");
2789 if (hns3_logtype_driver >= 0)
2790 rte_log_set_level(hns3_logtype_driver, RTE_LOG_NOTICE);