1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2018-2021 HiSilicon Limited.
5 #include <linux/pci_regs.h>
7 #include <ethdev_pci.h>
12 #include "hns3_ethdev.h"
13 #include "hns3_logs.h"
14 #include "hns3_rxtx.h"
15 #include "hns3_regs.h"
16 #include "hns3_intr.h"
20 #define HNS3VF_KEEP_ALIVE_INTERVAL 2000000 /* us */
21 #define HNS3VF_SERVICE_INTERVAL 1000000 /* us */
23 #define HNS3VF_RESET_WAIT_MS 20
24 #define HNS3VF_RESET_WAIT_CNT 2000
26 /* Reset related Registers */
27 #define HNS3_GLOBAL_RESET_BIT 0
28 #define HNS3_CORE_RESET_BIT 1
29 #define HNS3_IMP_RESET_BIT 2
30 #define HNS3_FUN_RST_ING_B 0
32 enum hns3vf_evt_cause {
33 HNS3VF_VECTOR0_EVENT_RST,
34 HNS3VF_VECTOR0_EVENT_MBX,
35 HNS3VF_VECTOR0_EVENT_OTHER,
38 static enum hns3_reset_level hns3vf_get_reset_level(struct hns3_hw *hw,
40 static int hns3vf_dev_mtu_set(struct rte_eth_dev *dev, uint16_t mtu);
41 static int hns3vf_dev_configure_vlan(struct rte_eth_dev *dev);
43 static int hns3vf_add_mc_mac_addr(struct hns3_hw *hw,
44 struct rte_ether_addr *mac_addr);
45 static int hns3vf_remove_mc_mac_addr(struct hns3_hw *hw,
46 struct rte_ether_addr *mac_addr);
47 static int hns3vf_dev_link_update(struct rte_eth_dev *eth_dev,
48 __rte_unused int wait_to_complete);
50 /* set PCI bus mastering */
52 hns3vf_set_bus_master(const struct rte_pci_device *device, bool op)
57 ret = rte_pci_read_config(device, ®, sizeof(reg), PCI_COMMAND);
59 PMD_INIT_LOG(ERR, "Failed to read PCI offset 0x%x",
65 /* set the master bit */
66 reg |= PCI_COMMAND_MASTER;
68 reg &= ~(PCI_COMMAND_MASTER);
70 return rte_pci_write_config(device, ®, sizeof(reg), PCI_COMMAND);
74 * hns3vf_find_pci_capability - lookup a capability in the PCI capability list
75 * @cap: the capability
77 * Return the address of the given capability within the PCI capability list.
80 hns3vf_find_pci_capability(const struct rte_pci_device *device, int cap)
82 #define MAX_PCIE_CAPABILITY 48
89 ret = rte_pci_read_config(device, &status, sizeof(status), PCI_STATUS);
91 PMD_INIT_LOG(ERR, "Failed to read PCI offset 0x%x", PCI_STATUS);
95 if (!(status & PCI_STATUS_CAP_LIST))
98 ttl = MAX_PCIE_CAPABILITY;
99 ret = rte_pci_read_config(device, &pos, sizeof(pos),
100 PCI_CAPABILITY_LIST);
102 PMD_INIT_LOG(ERR, "Failed to read PCI offset 0x%x",
103 PCI_CAPABILITY_LIST);
107 while (ttl-- && pos >= PCI_STD_HEADER_SIZEOF) {
108 ret = rte_pci_read_config(device, &id, sizeof(id),
109 (pos + PCI_CAP_LIST_ID));
111 PMD_INIT_LOG(ERR, "Failed to read PCI offset 0x%x",
112 (pos + PCI_CAP_LIST_ID));
122 ret = rte_pci_read_config(device, &pos, sizeof(pos),
123 (pos + PCI_CAP_LIST_NEXT));
125 PMD_INIT_LOG(ERR, "Failed to read PCI offset 0x%x",
126 (pos + PCI_CAP_LIST_NEXT));
134 hns3vf_enable_msix(const struct rte_pci_device *device, bool op)
140 pos = hns3vf_find_pci_capability(device, PCI_CAP_ID_MSIX);
142 ret = rte_pci_read_config(device, &control, sizeof(control),
143 (pos + PCI_MSIX_FLAGS));
145 PMD_INIT_LOG(ERR, "Failed to read PCI offset 0x%x",
146 (pos + PCI_MSIX_FLAGS));
151 control |= PCI_MSIX_FLAGS_ENABLE;
153 control &= ~PCI_MSIX_FLAGS_ENABLE;
154 ret = rte_pci_write_config(device, &control, sizeof(control),
155 (pos + PCI_MSIX_FLAGS));
157 PMD_INIT_LOG(ERR, "failed to write PCI offset 0x%x",
158 (pos + PCI_MSIX_FLAGS));
169 hns3vf_add_uc_mac_addr(struct hns3_hw *hw, struct rte_ether_addr *mac_addr)
171 /* mac address was checked by upper level interface */
172 char mac_str[RTE_ETHER_ADDR_FMT_SIZE];
175 ret = hns3_send_mbx_msg(hw, HNS3_MBX_SET_UNICAST,
176 HNS3_MBX_MAC_VLAN_UC_ADD, mac_addr->addr_bytes,
177 RTE_ETHER_ADDR_LEN, false, NULL, 0);
179 hns3_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
181 hns3_err(hw, "failed to add uc mac addr(%s), ret = %d",
188 hns3vf_remove_uc_mac_addr(struct hns3_hw *hw, struct rte_ether_addr *mac_addr)
190 /* mac address was checked by upper level interface */
191 char mac_str[RTE_ETHER_ADDR_FMT_SIZE];
194 ret = hns3_send_mbx_msg(hw, HNS3_MBX_SET_UNICAST,
195 HNS3_MBX_MAC_VLAN_UC_REMOVE,
196 mac_addr->addr_bytes, RTE_ETHER_ADDR_LEN,
199 hns3_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
201 hns3_err(hw, "failed to add uc mac addr(%s), ret = %d",
208 hns3vf_add_mc_addr_common(struct hns3_hw *hw, struct rte_ether_addr *mac_addr)
210 char mac_str[RTE_ETHER_ADDR_FMT_SIZE];
211 struct rte_ether_addr *addr;
215 for (i = 0; i < hw->mc_addrs_num; i++) {
216 addr = &hw->mc_addrs[i];
217 /* Check if there are duplicate addresses */
218 if (rte_is_same_ether_addr(addr, mac_addr)) {
219 hns3_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
221 hns3_err(hw, "failed to add mc mac addr, same addrs"
222 "(%s) is added by the set_mc_mac_addr_list "
228 ret = hns3vf_add_mc_mac_addr(hw, mac_addr);
230 hns3_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
232 hns3_err(hw, "failed to add mc mac addr(%s), ret = %d",
239 hns3vf_add_mac_addr(struct rte_eth_dev *dev, struct rte_ether_addr *mac_addr,
240 __rte_unused uint32_t idx,
241 __rte_unused uint32_t pool)
243 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
244 char mac_str[RTE_ETHER_ADDR_FMT_SIZE];
247 rte_spinlock_lock(&hw->lock);
250 * In hns3 network engine adding UC and MC mac address with different
251 * commands with firmware. We need to determine whether the input
252 * address is a UC or a MC address to call different commands.
253 * By the way, it is recommended calling the API function named
254 * rte_eth_dev_set_mc_addr_list to set the MC mac address, because
255 * using the rte_eth_dev_mac_addr_add API function to set MC mac address
256 * may affect the specifications of UC mac addresses.
258 if (rte_is_multicast_ether_addr(mac_addr))
259 ret = hns3vf_add_mc_addr_common(hw, mac_addr);
261 ret = hns3vf_add_uc_mac_addr(hw, mac_addr);
263 rte_spinlock_unlock(&hw->lock);
265 hns3_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
267 hns3_err(hw, "failed to add mac addr(%s), ret = %d", mac_str,
275 hns3vf_remove_mac_addr(struct rte_eth_dev *dev, uint32_t idx)
277 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
278 /* index will be checked by upper level rte interface */
279 struct rte_ether_addr *mac_addr = &dev->data->mac_addrs[idx];
280 char mac_str[RTE_ETHER_ADDR_FMT_SIZE];
283 rte_spinlock_lock(&hw->lock);
285 if (rte_is_multicast_ether_addr(mac_addr))
286 ret = hns3vf_remove_mc_mac_addr(hw, mac_addr);
288 ret = hns3vf_remove_uc_mac_addr(hw, mac_addr);
290 rte_spinlock_unlock(&hw->lock);
292 hns3_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
294 hns3_err(hw, "failed to remove mac addr(%s), ret = %d",
300 hns3vf_set_default_mac_addr(struct rte_eth_dev *dev,
301 struct rte_ether_addr *mac_addr)
303 #define HNS3_TWO_ETHER_ADDR_LEN (RTE_ETHER_ADDR_LEN * 2)
304 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
305 struct rte_ether_addr *old_addr;
306 uint8_t addr_bytes[HNS3_TWO_ETHER_ADDR_LEN]; /* for 2 MAC addresses */
307 char mac_str[RTE_ETHER_ADDR_FMT_SIZE];
311 * It has been guaranteed that input parameter named mac_addr is valid
312 * address in the rte layer of DPDK framework.
314 old_addr = (struct rte_ether_addr *)hw->mac.mac_addr;
315 rte_spinlock_lock(&hw->lock);
316 memcpy(addr_bytes, mac_addr->addr_bytes, RTE_ETHER_ADDR_LEN);
317 memcpy(&addr_bytes[RTE_ETHER_ADDR_LEN], old_addr->addr_bytes,
320 ret = hns3_send_mbx_msg(hw, HNS3_MBX_SET_UNICAST,
321 HNS3_MBX_MAC_VLAN_UC_MODIFY, addr_bytes,
322 HNS3_TWO_ETHER_ADDR_LEN, true, NULL, 0);
325 * The hns3 VF PMD driver depends on the hns3 PF kernel ethdev
326 * driver. When user has configured a MAC address for VF device
327 * by "ip link set ..." command based on the PF device, the hns3
328 * PF kernel ethdev driver does not allow VF driver to request
329 * reconfiguring a different default MAC address, and return
330 * -EPREM to VF driver through mailbox.
333 hns3_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
335 hns3_warn(hw, "Has permanet mac addr(%s) for vf",
338 hns3_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
340 hns3_err(hw, "Failed to set mac addr(%s) for vf: %d",
345 rte_ether_addr_copy(mac_addr,
346 (struct rte_ether_addr *)hw->mac.mac_addr);
347 rte_spinlock_unlock(&hw->lock);
353 hns3vf_configure_mac_addr(struct hns3_adapter *hns, bool del)
355 struct hns3_hw *hw = &hns->hw;
356 struct rte_ether_addr *addr;
357 char mac_str[RTE_ETHER_ADDR_FMT_SIZE];
362 for (i = 0; i < HNS3_VF_UC_MACADDR_NUM; i++) {
363 addr = &hw->data->mac_addrs[i];
364 if (rte_is_zero_ether_addr(addr))
366 if (rte_is_multicast_ether_addr(addr))
367 ret = del ? hns3vf_remove_mc_mac_addr(hw, addr) :
368 hns3vf_add_mc_mac_addr(hw, addr);
370 ret = del ? hns3vf_remove_uc_mac_addr(hw, addr) :
371 hns3vf_add_uc_mac_addr(hw, addr);
375 hns3_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
377 hns3_err(hw, "failed to %s mac addr(%s) index:%d "
378 "ret = %d.", del ? "remove" : "restore",
386 hns3vf_add_mc_mac_addr(struct hns3_hw *hw,
387 struct rte_ether_addr *mac_addr)
389 char mac_str[RTE_ETHER_ADDR_FMT_SIZE];
392 ret = hns3_send_mbx_msg(hw, HNS3_MBX_SET_MULTICAST,
393 HNS3_MBX_MAC_VLAN_MC_ADD,
394 mac_addr->addr_bytes, RTE_ETHER_ADDR_LEN, false,
397 hns3_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
399 hns3_err(hw, "Failed to add mc mac addr(%s) for vf: %d",
407 hns3vf_remove_mc_mac_addr(struct hns3_hw *hw,
408 struct rte_ether_addr *mac_addr)
410 char mac_str[RTE_ETHER_ADDR_FMT_SIZE];
413 ret = hns3_send_mbx_msg(hw, HNS3_MBX_SET_MULTICAST,
414 HNS3_MBX_MAC_VLAN_MC_REMOVE,
415 mac_addr->addr_bytes, RTE_ETHER_ADDR_LEN, false,
418 hns3_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
420 hns3_err(hw, "Failed to remove mc mac addr(%s) for vf: %d",
428 hns3vf_set_mc_addr_chk_param(struct hns3_hw *hw,
429 struct rte_ether_addr *mc_addr_set,
432 char mac_str[RTE_ETHER_ADDR_FMT_SIZE];
433 struct rte_ether_addr *addr;
437 if (nb_mc_addr > HNS3_MC_MACADDR_NUM) {
438 hns3_err(hw, "failed to set mc mac addr, nb_mc_addr(%u) "
439 "invalid. valid range: 0~%d",
440 nb_mc_addr, HNS3_MC_MACADDR_NUM);
444 /* Check if input mac addresses are valid */
445 for (i = 0; i < nb_mc_addr; i++) {
446 addr = &mc_addr_set[i];
447 if (!rte_is_multicast_ether_addr(addr)) {
448 hns3_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
451 "failed to set mc mac addr, addr(%s) invalid.",
456 /* Check if there are duplicate addresses */
457 for (j = i + 1; j < nb_mc_addr; j++) {
458 if (rte_is_same_ether_addr(addr, &mc_addr_set[j])) {
459 hns3_ether_format_addr(mac_str,
460 RTE_ETHER_ADDR_FMT_SIZE,
462 hns3_err(hw, "failed to set mc mac addr, "
463 "addrs invalid. two same addrs(%s).",
470 * Check if there are duplicate addresses between mac_addrs
473 for (j = 0; j < HNS3_VF_UC_MACADDR_NUM; j++) {
474 if (rte_is_same_ether_addr(addr,
475 &hw->data->mac_addrs[j])) {
476 hns3_ether_format_addr(mac_str,
477 RTE_ETHER_ADDR_FMT_SIZE,
479 hns3_err(hw, "failed to set mc mac addr, "
480 "addrs invalid. addrs(%s) has already "
481 "configured in mac_addr add API",
492 hns3vf_set_mc_mac_addr_list(struct rte_eth_dev *dev,
493 struct rte_ether_addr *mc_addr_set,
496 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
497 struct rte_ether_addr *addr;
504 ret = hns3vf_set_mc_addr_chk_param(hw, mc_addr_set, nb_mc_addr);
508 rte_spinlock_lock(&hw->lock);
509 cur_addr_num = hw->mc_addrs_num;
510 for (i = 0; i < cur_addr_num; i++) {
511 num = cur_addr_num - i - 1;
512 addr = &hw->mc_addrs[num];
513 ret = hns3vf_remove_mc_mac_addr(hw, addr);
515 rte_spinlock_unlock(&hw->lock);
522 set_addr_num = (int)nb_mc_addr;
523 for (i = 0; i < set_addr_num; i++) {
524 addr = &mc_addr_set[i];
525 ret = hns3vf_add_mc_mac_addr(hw, addr);
527 rte_spinlock_unlock(&hw->lock);
531 rte_ether_addr_copy(addr, &hw->mc_addrs[hw->mc_addrs_num]);
534 rte_spinlock_unlock(&hw->lock);
540 hns3vf_configure_all_mc_mac_addr(struct hns3_adapter *hns, bool del)
542 char mac_str[RTE_ETHER_ADDR_FMT_SIZE];
543 struct hns3_hw *hw = &hns->hw;
544 struct rte_ether_addr *addr;
549 for (i = 0; i < hw->mc_addrs_num; i++) {
550 addr = &hw->mc_addrs[i];
551 if (!rte_is_multicast_ether_addr(addr))
554 ret = hns3vf_remove_mc_mac_addr(hw, addr);
556 ret = hns3vf_add_mc_mac_addr(hw, addr);
559 hns3_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
561 hns3_err(hw, "Failed to %s mc mac addr: %s for vf: %d",
562 del ? "Remove" : "Restore", mac_str, ret);
569 hns3vf_set_promisc_mode(struct hns3_hw *hw, bool en_bc_pmc,
570 bool en_uc_pmc, bool en_mc_pmc)
572 struct hns3_mbx_vf_to_pf_cmd *req;
573 struct hns3_cmd_desc desc;
576 req = (struct hns3_mbx_vf_to_pf_cmd *)desc.data;
579 * The hns3 VF PMD driver depends on the hns3 PF kernel ethdev driver,
580 * so there are some features for promiscuous/allmulticast mode in hns3
581 * VF PMD driver as below:
582 * 1. The promiscuous/allmulticast mode can be configured successfully
583 * only based on the trusted VF device. If based on the non trusted
584 * VF device, configuring promiscuous/allmulticast mode will fail.
585 * The hns3 VF device can be confiruged as trusted device by hns3 PF
586 * kernel ethdev driver on the host by the following command:
587 * "ip link set <eth num> vf <vf id> turst on"
588 * 2. After the promiscuous mode is configured successfully, hns3 VF PMD
589 * driver can receive the ingress and outgoing traffic. In the words,
590 * all the ingress packets, all the packets sent from the PF and
591 * other VFs on the same physical port.
592 * 3. Note: Because of the hardware constraints, By default vlan filter
593 * is enabled and couldn't be turned off based on VF device, so vlan
594 * filter is still effective even in promiscuous mode. If upper
595 * applications don't call rte_eth_dev_vlan_filter API function to
596 * set vlan based on VF device, hns3 VF PMD driver will can't receive
597 * the packets with vlan tag in promiscuoue mode.
599 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_MBX_VF_TO_PF, false);
600 req->msg[0] = HNS3_MBX_SET_PROMISC_MODE;
601 req->msg[1] = en_bc_pmc ? 1 : 0;
602 req->msg[2] = en_uc_pmc ? 1 : 0;
603 req->msg[3] = en_mc_pmc ? 1 : 0;
604 req->msg[4] = hw->promisc_mode == HNS3_LIMIT_PROMISC_MODE ? 1 : 0;
606 ret = hns3_cmd_send(hw, &desc, 1);
608 hns3_err(hw, "Set promisc mode fail, ret = %d", ret);
614 hns3vf_dev_promiscuous_enable(struct rte_eth_dev *dev)
616 struct hns3_adapter *hns = dev->data->dev_private;
617 struct hns3_hw *hw = &hns->hw;
620 ret = hns3vf_set_promisc_mode(hw, true, true, true);
622 hns3_err(hw, "Failed to enable promiscuous mode, ret = %d",
628 hns3vf_dev_promiscuous_disable(struct rte_eth_dev *dev)
630 bool allmulti = dev->data->all_multicast ? true : false;
631 struct hns3_adapter *hns = dev->data->dev_private;
632 struct hns3_hw *hw = &hns->hw;
635 ret = hns3vf_set_promisc_mode(hw, true, false, allmulti);
637 hns3_err(hw, "Failed to disable promiscuous mode, ret = %d",
643 hns3vf_dev_allmulticast_enable(struct rte_eth_dev *dev)
645 struct hns3_adapter *hns = dev->data->dev_private;
646 struct hns3_hw *hw = &hns->hw;
649 if (dev->data->promiscuous)
652 ret = hns3vf_set_promisc_mode(hw, true, false, true);
654 hns3_err(hw, "Failed to enable allmulticast mode, ret = %d",
660 hns3vf_dev_allmulticast_disable(struct rte_eth_dev *dev)
662 struct hns3_adapter *hns = dev->data->dev_private;
663 struct hns3_hw *hw = &hns->hw;
666 if (dev->data->promiscuous)
669 ret = hns3vf_set_promisc_mode(hw, true, false, false);
671 hns3_err(hw, "Failed to disable allmulticast mode, ret = %d",
677 hns3vf_restore_promisc(struct hns3_adapter *hns)
679 struct hns3_hw *hw = &hns->hw;
680 bool allmulti = hw->data->all_multicast ? true : false;
682 if (hw->data->promiscuous)
683 return hns3vf_set_promisc_mode(hw, true, true, true);
685 return hns3vf_set_promisc_mode(hw, true, false, allmulti);
689 hns3vf_bind_ring_with_vector(struct hns3_hw *hw, uint8_t vector_id,
690 bool mmap, enum hns3_ring_type queue_type,
693 struct hns3_vf_bind_vector_msg bind_msg;
698 memset(&bind_msg, 0, sizeof(bind_msg));
699 code = mmap ? HNS3_MBX_MAP_RING_TO_VECTOR :
700 HNS3_MBX_UNMAP_RING_TO_VECTOR;
701 bind_msg.vector_id = vector_id;
703 if (queue_type == HNS3_RING_TYPE_RX)
704 bind_msg.param[0].int_gl_index = HNS3_RING_GL_RX;
706 bind_msg.param[0].int_gl_index = HNS3_RING_GL_TX;
708 bind_msg.param[0].ring_type = queue_type;
709 bind_msg.ring_num = 1;
710 bind_msg.param[0].tqp_index = queue_id;
711 op_str = mmap ? "Map" : "Unmap";
712 ret = hns3_send_mbx_msg(hw, code, 0, (uint8_t *)&bind_msg,
713 sizeof(bind_msg), false, NULL, 0);
715 hns3_err(hw, "%s TQP %u fail, vector_id is %u, ret is %d.",
716 op_str, queue_id, bind_msg.vector_id, ret);
722 hns3vf_init_ring_with_vector(struct hns3_hw *hw)
729 * In hns3 network engine, vector 0 is always the misc interrupt of this
730 * function, vector 1~N can be used respectively for the queues of the
731 * function. Tx and Rx queues with the same number share the interrupt
732 * vector. In the initialization clearing the all hardware mapping
733 * relationship configurations between queues and interrupt vectors is
734 * needed, so some error caused by the residual configurations, such as
735 * the unexpected Tx interrupt, can be avoid.
737 vec = hw->num_msi - 1; /* vector 0 for misc interrupt, not for queue */
738 if (hw->intr.mapping_mode == HNS3_INTR_MAPPING_VEC_RSV_ONE)
739 vec = vec - 1; /* the last interrupt is reserved */
740 hw->intr_tqps_num = RTE_MIN(vec, hw->tqps_num);
741 for (i = 0; i < hw->intr_tqps_num; i++) {
743 * Set gap limiter/rate limiter/quanity limiter algorithm
744 * configuration for interrupt coalesce of queue's interrupt.
746 hns3_set_queue_intr_gl(hw, i, HNS3_RING_GL_RX,
747 HNS3_TQP_INTR_GL_DEFAULT);
748 hns3_set_queue_intr_gl(hw, i, HNS3_RING_GL_TX,
749 HNS3_TQP_INTR_GL_DEFAULT);
750 hns3_set_queue_intr_rl(hw, i, HNS3_TQP_INTR_RL_DEFAULT);
752 * QL(quantity limiter) is not used currently, just set 0 to
755 hns3_set_queue_intr_ql(hw, i, HNS3_TQP_INTR_QL_DEFAULT);
757 ret = hns3vf_bind_ring_with_vector(hw, vec, false,
758 HNS3_RING_TYPE_TX, i);
760 PMD_INIT_LOG(ERR, "VF fail to unbind TX ring(%d) with "
761 "vector: %u, ret=%d", i, vec, ret);
765 ret = hns3vf_bind_ring_with_vector(hw, vec, false,
766 HNS3_RING_TYPE_RX, i);
768 PMD_INIT_LOG(ERR, "VF fail to unbind RX ring(%d) with "
769 "vector: %u, ret=%d", i, vec, ret);
778 hns3vf_dev_configure(struct rte_eth_dev *dev)
780 struct hns3_adapter *hns = dev->data->dev_private;
781 struct hns3_hw *hw = &hns->hw;
782 struct rte_eth_conf *conf = &dev->data->dev_conf;
783 enum rte_eth_rx_mq_mode mq_mode = conf->rxmode.mq_mode;
784 uint16_t nb_rx_q = dev->data->nb_rx_queues;
785 uint16_t nb_tx_q = dev->data->nb_tx_queues;
786 struct rte_eth_rss_conf rss_conf;
790 hw->cfg_max_queues = RTE_MAX(nb_rx_q, nb_tx_q);
793 * Some versions of hardware network engine does not support
794 * individually enable/disable/reset the Tx or Rx queue. These devices
795 * must enable/disable/reset Tx and Rx queues at the same time. When the
796 * numbers of Tx queues allocated by upper applications are not equal to
797 * the numbers of Rx queues, driver needs to setup fake Tx or Rx queues
798 * to adjust numbers of Tx/Rx queues. otherwise, network engine can not
799 * work as usual. But these fake queues are imperceptible, and can not
800 * be used by upper applications.
802 ret = hns3_set_fake_rx_or_tx_queues(dev, nb_rx_q, nb_tx_q);
804 hns3_err(hw, "fail to set Rx/Tx fake queues, ret = %d.", ret);
805 hw->cfg_max_queues = 0;
809 hw->adapter_state = HNS3_NIC_CONFIGURING;
810 if (conf->link_speeds & ETH_LINK_SPEED_FIXED) {
811 hns3_err(hw, "setting link speed/duplex not supported");
816 /* When RSS is not configured, redirect the packet queue 0 */
817 if ((uint32_t)mq_mode & ETH_MQ_RX_RSS_FLAG) {
818 conf->rxmode.offloads |= DEV_RX_OFFLOAD_RSS_HASH;
819 hw->rss_dis_flag = false;
820 rss_conf = conf->rx_adv_conf.rss_conf;
821 ret = hns3_dev_rss_hash_update(dev, &rss_conf);
826 ret = hns3vf_dev_mtu_set(dev, conf->rxmode.mtu);
830 ret = hns3vf_dev_configure_vlan(dev);
834 /* config hardware GRO */
835 gro_en = conf->rxmode.offloads & DEV_RX_OFFLOAD_TCP_LRO ? true : false;
836 ret = hns3_config_gro(hw, gro_en);
840 hns3_init_rx_ptype_tble(dev);
842 hw->adapter_state = HNS3_NIC_CONFIGURED;
846 hw->cfg_max_queues = 0;
847 (void)hns3_set_fake_rx_or_tx_queues(dev, 0, 0);
848 hw->adapter_state = HNS3_NIC_INITIALIZED;
854 hns3vf_config_mtu(struct hns3_hw *hw, uint16_t mtu)
858 ret = hns3_send_mbx_msg(hw, HNS3_MBX_SET_MTU, 0, (const uint8_t *)&mtu,
859 sizeof(mtu), true, NULL, 0);
861 hns3_err(hw, "Failed to set mtu (%u) for vf: %d", mtu, ret);
867 hns3vf_dev_mtu_set(struct rte_eth_dev *dev, uint16_t mtu)
869 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
870 uint32_t frame_size = mtu + HNS3_ETH_OVERHEAD;
874 * The hns3 PF/VF devices on the same port share the hardware MTU
875 * configuration. Currently, we send mailbox to inform hns3 PF kernel
876 * ethdev driver to finish hardware MTU configuration in hns3 VF PMD
877 * driver, there is no need to stop the port for hns3 VF device, and the
878 * MTU value issued by hns3 VF PMD driver must be less than or equal to
881 if (__atomic_load_n(&hw->reset.resetting, __ATOMIC_RELAXED)) {
882 hns3_err(hw, "Failed to set mtu during resetting");
887 * when Rx of scattered packets is off, we have some possibility of
888 * using vector Rx process function or simple Rx functions in hns3 PMD
889 * driver. If the input MTU is increased and the maximum length of
890 * received packets is greater than the length of a buffer for Rx
891 * packet, the hardware network engine needs to use multiple BDs and
892 * buffers to store these packets. This will cause problems when still
893 * using vector Rx process function or simple Rx function to receiving
894 * packets. So, when Rx of scattered packets is off and device is
895 * started, it is not permitted to increase MTU so that the maximum
896 * length of Rx packets is greater than Rx buffer length.
898 if (dev->data->dev_started && !dev->data->scattered_rx &&
899 frame_size > hw->rx_buf_len) {
900 hns3_err(hw, "failed to set mtu because current is "
901 "not scattered rx mode");
905 rte_spinlock_lock(&hw->lock);
906 ret = hns3vf_config_mtu(hw, mtu);
908 rte_spinlock_unlock(&hw->lock);
911 if (mtu > RTE_ETHER_MTU)
912 dev->data->dev_conf.rxmode.offloads |=
913 DEV_RX_OFFLOAD_JUMBO_FRAME;
915 dev->data->dev_conf.rxmode.offloads &=
916 ~DEV_RX_OFFLOAD_JUMBO_FRAME;
917 rte_spinlock_unlock(&hw->lock);
923 hns3vf_dev_infos_get(struct rte_eth_dev *eth_dev, struct rte_eth_dev_info *info)
925 struct hns3_adapter *hns = eth_dev->data->dev_private;
926 struct hns3_hw *hw = &hns->hw;
927 uint16_t q_num = hw->tqps_num;
930 * In interrupt mode, 'max_rx_queues' is set based on the number of
931 * MSI-X interrupt resources of the hardware.
933 if (hw->data->dev_conf.intr_conf.rxq == 1)
934 q_num = hw->intr_tqps_num;
936 info->max_rx_queues = q_num;
937 info->max_tx_queues = hw->tqps_num;
938 info->max_rx_pktlen = HNS3_MAX_FRAME_LEN; /* CRC included */
939 info->min_rx_bufsize = HNS3_MIN_BD_BUF_SIZE;
940 info->max_mac_addrs = HNS3_VF_UC_MACADDR_NUM;
941 info->max_mtu = info->max_rx_pktlen - HNS3_ETH_OVERHEAD;
942 info->max_lro_pkt_size = HNS3_MAX_LRO_SIZE;
944 info->rx_offload_capa = (DEV_RX_OFFLOAD_IPV4_CKSUM |
945 DEV_RX_OFFLOAD_UDP_CKSUM |
946 DEV_RX_OFFLOAD_TCP_CKSUM |
947 DEV_RX_OFFLOAD_SCTP_CKSUM |
948 DEV_RX_OFFLOAD_OUTER_IPV4_CKSUM |
949 DEV_RX_OFFLOAD_OUTER_UDP_CKSUM |
950 DEV_RX_OFFLOAD_SCATTER |
951 DEV_RX_OFFLOAD_VLAN_STRIP |
952 DEV_RX_OFFLOAD_VLAN_FILTER |
953 DEV_RX_OFFLOAD_JUMBO_FRAME |
954 DEV_RX_OFFLOAD_RSS_HASH |
955 DEV_RX_OFFLOAD_TCP_LRO);
956 info->tx_offload_capa = (DEV_TX_OFFLOAD_OUTER_IPV4_CKSUM |
957 DEV_TX_OFFLOAD_IPV4_CKSUM |
958 DEV_TX_OFFLOAD_TCP_CKSUM |
959 DEV_TX_OFFLOAD_UDP_CKSUM |
960 DEV_TX_OFFLOAD_SCTP_CKSUM |
961 DEV_TX_OFFLOAD_MULTI_SEGS |
962 DEV_TX_OFFLOAD_TCP_TSO |
963 DEV_TX_OFFLOAD_VXLAN_TNL_TSO |
964 DEV_TX_OFFLOAD_GRE_TNL_TSO |
965 DEV_TX_OFFLOAD_GENEVE_TNL_TSO |
966 DEV_TX_OFFLOAD_MBUF_FAST_FREE |
967 hns3_txvlan_cap_get(hw));
969 if (hns3_dev_get_support(hw, OUTER_UDP_CKSUM))
970 info->tx_offload_capa |= DEV_TX_OFFLOAD_OUTER_UDP_CKSUM;
972 if (hns3_dev_get_support(hw, INDEP_TXRX))
973 info->dev_capa = RTE_ETH_DEV_CAPA_RUNTIME_RX_QUEUE_SETUP |
974 RTE_ETH_DEV_CAPA_RUNTIME_TX_QUEUE_SETUP;
976 info->rx_desc_lim = (struct rte_eth_desc_lim) {
977 .nb_max = HNS3_MAX_RING_DESC,
978 .nb_min = HNS3_MIN_RING_DESC,
979 .nb_align = HNS3_ALIGN_RING_DESC,
982 info->tx_desc_lim = (struct rte_eth_desc_lim) {
983 .nb_max = HNS3_MAX_RING_DESC,
984 .nb_min = HNS3_MIN_RING_DESC,
985 .nb_align = HNS3_ALIGN_RING_DESC,
986 .nb_seg_max = HNS3_MAX_TSO_BD_PER_PKT,
987 .nb_mtu_seg_max = hw->max_non_tso_bd_num,
990 info->default_rxconf = (struct rte_eth_rxconf) {
991 .rx_free_thresh = HNS3_DEFAULT_RX_FREE_THRESH,
993 * If there are no available Rx buffer descriptors, incoming
994 * packets are always dropped by hardware based on hns3 network
1000 info->default_txconf = (struct rte_eth_txconf) {
1001 .tx_rs_thresh = HNS3_DEFAULT_TX_RS_THRESH,
1005 info->reta_size = hw->rss_ind_tbl_size;
1006 info->hash_key_size = HNS3_RSS_KEY_SIZE;
1007 info->flow_type_rss_offloads = HNS3_ETH_RSS_SUPPORT;
1009 info->default_rxportconf.burst_size = HNS3_DEFAULT_PORT_CONF_BURST_SIZE;
1010 info->default_txportconf.burst_size = HNS3_DEFAULT_PORT_CONF_BURST_SIZE;
1011 info->default_rxportconf.nb_queues = HNS3_DEFAULT_PORT_CONF_QUEUES_NUM;
1012 info->default_txportconf.nb_queues = HNS3_DEFAULT_PORT_CONF_QUEUES_NUM;
1013 info->default_rxportconf.ring_size = HNS3_DEFAULT_RING_DESC;
1014 info->default_txportconf.ring_size = HNS3_DEFAULT_RING_DESC;
1020 hns3vf_clear_event_cause(struct hns3_hw *hw, uint32_t regclr)
1022 hns3_write_dev(hw, HNS3_VECTOR0_CMDQ_SRC_REG, regclr);
1026 hns3vf_disable_irq0(struct hns3_hw *hw)
1028 hns3_write_dev(hw, HNS3_MISC_VECTOR_REG_BASE, 0);
1032 hns3vf_enable_irq0(struct hns3_hw *hw)
1034 hns3_write_dev(hw, HNS3_MISC_VECTOR_REG_BASE, 1);
1037 static enum hns3vf_evt_cause
1038 hns3vf_check_event_cause(struct hns3_adapter *hns, uint32_t *clearval)
1040 struct hns3_hw *hw = &hns->hw;
1041 enum hns3vf_evt_cause ret;
1042 uint32_t cmdq_stat_reg;
1043 uint32_t rst_ing_reg;
1046 /* Fetch the events from their corresponding regs */
1047 cmdq_stat_reg = hns3_read_dev(hw, HNS3_VECTOR0_CMDQ_STAT_REG);
1048 if (BIT(HNS3_VECTOR0_RST_INT_B) & cmdq_stat_reg) {
1049 rst_ing_reg = hns3_read_dev(hw, HNS3_FUN_RST_ING);
1050 hns3_warn(hw, "resetting reg: 0x%x", rst_ing_reg);
1051 hns3_atomic_set_bit(HNS3_VF_RESET, &hw->reset.pending);
1052 __atomic_store_n(&hw->reset.disable_cmd, 1, __ATOMIC_RELAXED);
1053 val = hns3_read_dev(hw, HNS3_VF_RST_ING);
1054 hns3_write_dev(hw, HNS3_VF_RST_ING, val | HNS3_VF_RST_ING_BIT);
1055 val = cmdq_stat_reg & ~BIT(HNS3_VECTOR0_RST_INT_B);
1057 hw->reset.stats.global_cnt++;
1058 hns3_warn(hw, "Global reset detected, clear reset status");
1060 hns3_schedule_delayed_reset(hns);
1061 hns3_warn(hw, "Global reset detected, don't clear reset status");
1064 ret = HNS3VF_VECTOR0_EVENT_RST;
1068 /* Check for vector0 mailbox(=CMDQ RX) event source */
1069 if (BIT(HNS3_VECTOR0_RX_CMDQ_INT_B) & cmdq_stat_reg) {
1070 val = cmdq_stat_reg & ~BIT(HNS3_VECTOR0_RX_CMDQ_INT_B);
1071 ret = HNS3VF_VECTOR0_EVENT_MBX;
1076 ret = HNS3VF_VECTOR0_EVENT_OTHER;
1084 hns3vf_interrupt_handler(void *param)
1086 struct rte_eth_dev *dev = (struct rte_eth_dev *)param;
1087 struct hns3_adapter *hns = dev->data->dev_private;
1088 struct hns3_hw *hw = &hns->hw;
1089 enum hns3vf_evt_cause event_cause;
1092 /* Disable interrupt */
1093 hns3vf_disable_irq0(hw);
1095 /* Read out interrupt causes */
1096 event_cause = hns3vf_check_event_cause(hns, &clearval);
1097 /* Clear interrupt causes */
1098 hns3vf_clear_event_cause(hw, clearval);
1100 switch (event_cause) {
1101 case HNS3VF_VECTOR0_EVENT_RST:
1102 hns3_schedule_reset(hns);
1104 case HNS3VF_VECTOR0_EVENT_MBX:
1105 hns3_dev_handle_mbx_msg(hw);
1111 /* Enable interrupt */
1112 hns3vf_enable_irq0(hw);
1116 hns3vf_set_default_dev_specifications(struct hns3_hw *hw)
1118 hw->max_non_tso_bd_num = HNS3_MAX_NON_TSO_BD_PER_PKT;
1119 hw->rss_ind_tbl_size = HNS3_RSS_IND_TBL_SIZE;
1120 hw->rss_key_size = HNS3_RSS_KEY_SIZE;
1121 hw->intr.int_ql_max = HNS3_INTR_QL_NONE;
1125 hns3vf_parse_dev_specifications(struct hns3_hw *hw, struct hns3_cmd_desc *desc)
1127 struct hns3_dev_specs_0_cmd *req0;
1129 req0 = (struct hns3_dev_specs_0_cmd *)desc[0].data;
1131 hw->max_non_tso_bd_num = req0->max_non_tso_bd_num;
1132 hw->rss_ind_tbl_size = rte_le_to_cpu_16(req0->rss_ind_tbl_size);
1133 hw->rss_key_size = rte_le_to_cpu_16(req0->rss_key_size);
1134 hw->intr.int_ql_max = rte_le_to_cpu_16(req0->intr_ql_max);
1138 hns3vf_check_dev_specifications(struct hns3_hw *hw)
1140 if (hw->rss_ind_tbl_size == 0 ||
1141 hw->rss_ind_tbl_size > HNS3_RSS_IND_TBL_SIZE_MAX) {
1142 hns3_warn(hw, "the size of hash lookup table configured (%u)"
1143 " exceeds the maximum(%u)", hw->rss_ind_tbl_size,
1144 HNS3_RSS_IND_TBL_SIZE_MAX);
1152 hns3vf_query_dev_specifications(struct hns3_hw *hw)
1154 struct hns3_cmd_desc desc[HNS3_QUERY_DEV_SPECS_BD_NUM];
1158 for (i = 0; i < HNS3_QUERY_DEV_SPECS_BD_NUM - 1; i++) {
1159 hns3_cmd_setup_basic_desc(&desc[i], HNS3_OPC_QUERY_DEV_SPECS,
1161 desc[i].flag |= rte_cpu_to_le_16(HNS3_CMD_FLAG_NEXT);
1163 hns3_cmd_setup_basic_desc(&desc[i], HNS3_OPC_QUERY_DEV_SPECS, true);
1165 ret = hns3_cmd_send(hw, desc, HNS3_QUERY_DEV_SPECS_BD_NUM);
1169 hns3vf_parse_dev_specifications(hw, desc);
1171 return hns3vf_check_dev_specifications(hw);
1175 hns3vf_update_push_lsc_cap(struct hns3_hw *hw, bool supported)
1177 uint16_t val = supported ? HNS3_PF_PUSH_LSC_CAP_SUPPORTED :
1178 HNS3_PF_PUSH_LSC_CAP_NOT_SUPPORTED;
1179 uint16_t exp = HNS3_PF_PUSH_LSC_CAP_UNKNOWN;
1180 struct hns3_vf *vf = HNS3_DEV_HW_TO_VF(hw);
1182 if (vf->pf_push_lsc_cap == HNS3_PF_PUSH_LSC_CAP_UNKNOWN)
1183 __atomic_compare_exchange(&vf->pf_push_lsc_cap, &exp, &val, 0,
1184 __ATOMIC_ACQUIRE, __ATOMIC_ACQUIRE);
1188 hns3vf_get_push_lsc_cap(struct hns3_hw *hw)
1190 #define HNS3_CHECK_PUSH_LSC_CAP_TIMEOUT_MS 500
1192 struct rte_eth_dev *dev = &rte_eth_devices[hw->data->port_id];
1193 int32_t remain_ms = HNS3_CHECK_PUSH_LSC_CAP_TIMEOUT_MS;
1194 uint16_t val = HNS3_PF_PUSH_LSC_CAP_NOT_SUPPORTED;
1195 uint16_t exp = HNS3_PF_PUSH_LSC_CAP_UNKNOWN;
1196 struct hns3_vf *vf = HNS3_DEV_HW_TO_VF(hw);
1198 __atomic_store_n(&vf->pf_push_lsc_cap, HNS3_PF_PUSH_LSC_CAP_UNKNOWN,
1201 (void)hns3_send_mbx_msg(hw, HNS3_MBX_GET_LINK_STATUS, 0, NULL, 0, false,
1204 while (remain_ms > 0) {
1205 rte_delay_ms(HNS3_POLL_RESPONE_MS);
1206 if (__atomic_load_n(&vf->pf_push_lsc_cap, __ATOMIC_ACQUIRE) !=
1207 HNS3_PF_PUSH_LSC_CAP_UNKNOWN)
1213 * When exit above loop, the pf_push_lsc_cap could be one of the three
1214 * state: unknown (means pf not ack), not_supported, supported.
1215 * Here config it as 'not_supported' when it's 'unknown' state.
1217 __atomic_compare_exchange(&vf->pf_push_lsc_cap, &exp, &val, 0,
1218 __ATOMIC_ACQUIRE, __ATOMIC_ACQUIRE);
1220 if (__atomic_load_n(&vf->pf_push_lsc_cap, __ATOMIC_ACQUIRE) ==
1221 HNS3_PF_PUSH_LSC_CAP_SUPPORTED) {
1222 hns3_info(hw, "detect PF support push link status change!");
1225 * Framework already set RTE_ETH_DEV_INTR_LSC bit because driver
1226 * declared RTE_PCI_DRV_INTR_LSC in drv_flags. So here cleared
1227 * the RTE_ETH_DEV_INTR_LSC capability.
1229 dev->data->dev_flags &= ~RTE_ETH_DEV_INTR_LSC;
1234 hns3vf_get_capability(struct hns3_hw *hw)
1236 struct rte_pci_device *pci_dev;
1237 struct rte_eth_dev *eth_dev;
1241 eth_dev = &rte_eth_devices[hw->data->port_id];
1242 pci_dev = RTE_ETH_DEV_TO_PCI(eth_dev);
1244 /* Get PCI revision id */
1245 ret = rte_pci_read_config(pci_dev, &revision, HNS3_PCI_REVISION_ID_LEN,
1246 HNS3_PCI_REVISION_ID);
1247 if (ret != HNS3_PCI_REVISION_ID_LEN) {
1248 PMD_INIT_LOG(ERR, "failed to read pci revision id, ret = %d",
1252 hw->revision = revision;
1254 if (revision < PCI_REVISION_ID_HIP09_A) {
1255 hns3vf_set_default_dev_specifications(hw);
1256 hw->intr.mapping_mode = HNS3_INTR_MAPPING_VEC_RSV_ONE;
1257 hw->intr.gl_unit = HNS3_INTR_COALESCE_GL_UINT_2US;
1258 hw->tso_mode = HNS3_TSO_SW_CAL_PSEUDO_H_CSUM;
1259 hw->drop_stats_mode = HNS3_PKTS_DROP_STATS_MODE1;
1260 hw->min_tx_pkt_len = HNS3_HIP08_MIN_TX_PKT_LEN;
1261 hw->rss_info.ipv6_sctp_offload_supported = false;
1262 hw->promisc_mode = HNS3_UNLIMIT_PROMISC_MODE;
1266 ret = hns3vf_query_dev_specifications(hw);
1269 "failed to query dev specifications, ret = %d",
1274 hw->intr.mapping_mode = HNS3_INTR_MAPPING_VEC_ALL;
1275 hw->intr.gl_unit = HNS3_INTR_COALESCE_GL_UINT_1US;
1276 hw->tso_mode = HNS3_TSO_HW_CAL_PSEUDO_H_CSUM;
1277 hw->drop_stats_mode = HNS3_PKTS_DROP_STATS_MODE2;
1278 hw->min_tx_pkt_len = HNS3_HIP09_MIN_TX_PKT_LEN;
1279 hw->rss_info.ipv6_sctp_offload_supported = true;
1280 hw->promisc_mode = HNS3_LIMIT_PROMISC_MODE;
1286 hns3vf_check_tqp_info(struct hns3_hw *hw)
1288 if (hw->tqps_num == 0) {
1289 PMD_INIT_LOG(ERR, "Get invalid tqps_num(0) from PF.");
1293 if (hw->rss_size_max == 0) {
1294 PMD_INIT_LOG(ERR, "Get invalid rss_size_max(0) from PF.");
1298 hw->tqps_num = RTE_MIN(hw->rss_size_max, hw->tqps_num);
1304 hns3vf_get_port_base_vlan_filter_state(struct hns3_hw *hw)
1309 ret = hns3_send_mbx_msg(hw, HNS3_MBX_SET_VLAN,
1310 HNS3_MBX_GET_PORT_BASE_VLAN_STATE, NULL, 0,
1311 true, &resp_msg, sizeof(resp_msg));
1313 if (ret == -ETIME) {
1315 * Getting current port based VLAN state from PF driver
1316 * will not affect VF driver's basic function. Because
1317 * the VF driver relies on hns3 PF kernel ether driver,
1318 * to avoid introducing compatibility issues with older
1319 * version of PF driver, no failure will be returned
1320 * when the return value is ETIME. This return value has
1321 * the following scenarios:
1322 * 1) Firmware didn't return the results in time
1323 * 2) the result return by firmware is timeout
1324 * 3) the older version of kernel side PF driver does
1325 * not support this mailbox message.
1326 * For scenarios 1 and 2, it is most likely that a
1327 * hardware error has occurred, or a hardware reset has
1328 * occurred. In this case, these errors will be caught
1329 * by other functions.
1331 PMD_INIT_LOG(WARNING,
1332 "failed to get PVID state for timeout, maybe "
1333 "kernel side PF driver doesn't support this "
1334 "mailbox message, or firmware didn't respond.");
1335 resp_msg = HNS3_PORT_BASE_VLAN_DISABLE;
1337 PMD_INIT_LOG(ERR, "failed to get port based VLAN state,"
1342 hw->port_base_vlan_cfg.state = resp_msg ?
1343 HNS3_PORT_BASE_VLAN_ENABLE : HNS3_PORT_BASE_VLAN_DISABLE;
1348 hns3vf_get_queue_info(struct hns3_hw *hw)
1350 #define HNS3VF_TQPS_RSS_INFO_LEN 6
1351 uint8_t resp_msg[HNS3VF_TQPS_RSS_INFO_LEN];
1354 ret = hns3_send_mbx_msg(hw, HNS3_MBX_GET_QINFO, 0, NULL, 0, true,
1355 resp_msg, HNS3VF_TQPS_RSS_INFO_LEN);
1357 PMD_INIT_LOG(ERR, "Failed to get tqp info from PF: %d", ret);
1361 memcpy(&hw->tqps_num, &resp_msg[0], sizeof(uint16_t));
1362 memcpy(&hw->rss_size_max, &resp_msg[2], sizeof(uint16_t));
1364 return hns3vf_check_tqp_info(hw);
1368 hns3vf_get_queue_depth(struct hns3_hw *hw)
1370 #define HNS3VF_TQPS_DEPTH_INFO_LEN 4
1371 uint8_t resp_msg[HNS3VF_TQPS_DEPTH_INFO_LEN];
1374 ret = hns3_send_mbx_msg(hw, HNS3_MBX_GET_QDEPTH, 0, NULL, 0, true,
1375 resp_msg, HNS3VF_TQPS_DEPTH_INFO_LEN);
1377 PMD_INIT_LOG(ERR, "Failed to get tqp depth info from PF: %d",
1382 memcpy(&hw->num_tx_desc, &resp_msg[0], sizeof(uint16_t));
1383 memcpy(&hw->num_rx_desc, &resp_msg[2], sizeof(uint16_t));
1389 hns3vf_update_caps(struct hns3_hw *hw, uint32_t caps)
1391 if (hns3_get_bit(caps, HNS3VF_CAPS_VLAN_FLT_MOD_B))
1392 hns3_set_bit(hw->capability,
1393 HNS3_DEV_SUPPORT_VF_VLAN_FLT_MOD_B, 1);
1397 hns3vf_get_num_tc(struct hns3_hw *hw)
1402 for (i = 0; i < HNS3_MAX_TC_NUM; i++) {
1403 if (hw->hw_tc_map & BIT(i))
1410 hns3vf_get_basic_info(struct hns3_hw *hw)
1412 uint8_t resp_msg[HNS3_MBX_MAX_RESP_DATA_SIZE];
1413 struct hns3_basic_info *basic_info;
1416 ret = hns3_send_mbx_msg(hw, HNS3_MBX_GET_BASIC_INFO, 0, NULL, 0,
1417 true, resp_msg, sizeof(resp_msg));
1419 hns3_err(hw, "failed to get basic info from PF, ret = %d.",
1424 basic_info = (struct hns3_basic_info *)resp_msg;
1425 hw->hw_tc_map = basic_info->hw_tc_map;
1426 hw->num_tc = hns3vf_get_num_tc(hw);
1427 hw->pf_vf_if_version = basic_info->pf_vf_if_version;
1428 hns3vf_update_caps(hw, basic_info->caps);
1434 hns3vf_get_host_mac_addr(struct hns3_hw *hw)
1436 uint8_t host_mac[RTE_ETHER_ADDR_LEN];
1439 ret = hns3_send_mbx_msg(hw, HNS3_MBX_GET_MAC_ADDR, 0, NULL, 0,
1440 true, host_mac, RTE_ETHER_ADDR_LEN);
1442 hns3_err(hw, "Failed to get mac addr from PF: %d", ret);
1446 memcpy(hw->mac.mac_addr, host_mac, RTE_ETHER_ADDR_LEN);
1452 hns3vf_get_configuration(struct hns3_hw *hw)
1456 hw->mac.media_type = HNS3_MEDIA_TYPE_NONE;
1457 hw->rss_dis_flag = false;
1459 /* Get device capability */
1460 ret = hns3vf_get_capability(hw);
1462 PMD_INIT_LOG(ERR, "failed to get device capability: %d.", ret);
1466 hns3vf_get_push_lsc_cap(hw);
1468 /* Get basic info from PF */
1469 ret = hns3vf_get_basic_info(hw);
1473 /* Get queue configuration from PF */
1474 ret = hns3vf_get_queue_info(hw);
1478 /* Get queue depth info from PF */
1479 ret = hns3vf_get_queue_depth(hw);
1483 /* Get user defined VF MAC addr from PF */
1484 ret = hns3vf_get_host_mac_addr(hw);
1488 return hns3vf_get_port_base_vlan_filter_state(hw);
1492 hns3vf_set_tc_queue_mapping(struct hns3_adapter *hns, uint16_t nb_rx_q,
1495 struct hns3_hw *hw = &hns->hw;
1497 return hns3_queue_to_tc_mapping(hw, nb_rx_q, nb_tx_q);
1501 hns3vf_request_link_info(struct hns3_hw *hw)
1503 struct hns3_vf *vf = HNS3_DEV_HW_TO_VF(hw);
1507 if (__atomic_load_n(&hw->reset.resetting, __ATOMIC_RELAXED))
1510 send_req = vf->pf_push_lsc_cap == HNS3_PF_PUSH_LSC_CAP_NOT_SUPPORTED ||
1511 vf->req_link_info_cnt > 0;
1515 ret = hns3_send_mbx_msg(hw, HNS3_MBX_GET_LINK_STATUS, 0, NULL, 0, false,
1518 hns3_err(hw, "failed to fetch link status, ret = %d", ret);
1522 if (vf->req_link_info_cnt > 0)
1523 vf->req_link_info_cnt--;
1527 hns3vf_update_link_status(struct hns3_hw *hw, uint8_t link_status,
1528 uint32_t link_speed, uint8_t link_duplex)
1530 struct rte_eth_dev *dev = &rte_eth_devices[hw->data->port_id];
1531 struct hns3_vf *vf = HNS3_DEV_HW_TO_VF(hw);
1532 struct hns3_mac *mac = &hw->mac;
1536 * PF kernel driver may push link status when VF driver is in resetting,
1537 * driver will stop polling job in this case, after resetting done
1538 * driver will start polling job again.
1539 * When polling job started, driver will get initial link status by
1540 * sending request to PF kernel driver, then could update link status by
1541 * process PF kernel driver's link status mailbox message.
1543 if (!__atomic_load_n(&vf->poll_job_started, __ATOMIC_RELAXED))
1546 if (hw->adapter_state != HNS3_NIC_STARTED)
1549 mac->link_status = link_status;
1550 mac->link_speed = link_speed;
1551 mac->link_duplex = link_duplex;
1552 ret = hns3vf_dev_link_update(dev, 0);
1553 if (ret == 0 && dev->data->dev_conf.intr_conf.lsc != 0)
1554 hns3_start_report_lse(dev);
1558 hns3vf_vlan_filter_configure(struct hns3_adapter *hns, uint16_t vlan_id, int on)
1560 #define HNS3VF_VLAN_MBX_MSG_LEN 5
1561 struct hns3_hw *hw = &hns->hw;
1562 uint8_t msg_data[HNS3VF_VLAN_MBX_MSG_LEN];
1563 uint16_t proto = htons(RTE_ETHER_TYPE_VLAN);
1564 uint8_t is_kill = on ? 0 : 1;
1566 msg_data[0] = is_kill;
1567 memcpy(&msg_data[1], &vlan_id, sizeof(vlan_id));
1568 memcpy(&msg_data[3], &proto, sizeof(proto));
1570 return hns3_send_mbx_msg(hw, HNS3_MBX_SET_VLAN, HNS3_MBX_VLAN_FILTER,
1571 msg_data, HNS3VF_VLAN_MBX_MSG_LEN, true, NULL,
1576 hns3vf_vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on)
1578 struct hns3_adapter *hns = dev->data->dev_private;
1579 struct hns3_hw *hw = &hns->hw;
1582 if (__atomic_load_n(&hw->reset.resetting, __ATOMIC_RELAXED)) {
1584 "vf set vlan id failed during resetting, vlan_id =%u",
1588 rte_spinlock_lock(&hw->lock);
1589 ret = hns3vf_vlan_filter_configure(hns, vlan_id, on);
1590 rte_spinlock_unlock(&hw->lock);
1592 hns3_err(hw, "vf set vlan id failed, vlan_id =%u, ret =%d",
1599 hns3vf_en_vlan_filter(struct hns3_hw *hw, bool enable)
1604 if (!hns3_dev_get_support(hw, VF_VLAN_FLT_MOD))
1607 msg_data = enable ? 1 : 0;
1608 ret = hns3_send_mbx_msg(hw, HNS3_MBX_SET_VLAN,
1609 HNS3_MBX_ENABLE_VLAN_FILTER, &msg_data,
1610 sizeof(msg_data), true, NULL, 0);
1612 hns3_err(hw, "%s vlan filter failed, ret = %d.",
1613 enable ? "enable" : "disable", ret);
1619 hns3vf_en_hw_strip_rxvtag(struct hns3_hw *hw, bool enable)
1624 msg_data = enable ? 1 : 0;
1625 ret = hns3_send_mbx_msg(hw, HNS3_MBX_SET_VLAN, HNS3_MBX_VLAN_RX_OFF_CFG,
1626 &msg_data, sizeof(msg_data), false, NULL, 0);
1628 hns3_err(hw, "vf %s strip failed, ret = %d.",
1629 enable ? "enable" : "disable", ret);
1635 hns3vf_vlan_offload_set(struct rte_eth_dev *dev, int mask)
1637 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1638 struct rte_eth_conf *dev_conf = &dev->data->dev_conf;
1639 unsigned int tmp_mask;
1642 if (__atomic_load_n(&hw->reset.resetting, __ATOMIC_RELAXED)) {
1643 hns3_err(hw, "vf set vlan offload failed during resetting, "
1644 "mask = 0x%x", mask);
1648 tmp_mask = (unsigned int)mask;
1650 if (tmp_mask & ETH_VLAN_FILTER_MASK) {
1651 rte_spinlock_lock(&hw->lock);
1652 /* Enable or disable VLAN filter */
1653 if (dev_conf->rxmode.offloads & DEV_RX_OFFLOAD_VLAN_FILTER)
1654 ret = hns3vf_en_vlan_filter(hw, true);
1656 ret = hns3vf_en_vlan_filter(hw, false);
1657 rte_spinlock_unlock(&hw->lock);
1662 /* Vlan stripping setting */
1663 if (tmp_mask & ETH_VLAN_STRIP_MASK) {
1664 rte_spinlock_lock(&hw->lock);
1665 /* Enable or disable VLAN stripping */
1666 if (dev_conf->rxmode.offloads & DEV_RX_OFFLOAD_VLAN_STRIP)
1667 ret = hns3vf_en_hw_strip_rxvtag(hw, true);
1669 ret = hns3vf_en_hw_strip_rxvtag(hw, false);
1670 rte_spinlock_unlock(&hw->lock);
1677 hns3vf_handle_all_vlan_table(struct hns3_adapter *hns, int on)
1679 struct rte_vlan_filter_conf *vfc;
1680 struct hns3_hw *hw = &hns->hw;
1687 vfc = &hw->data->vlan_filter_conf;
1688 for (i = 0; i < RTE_DIM(vfc->ids); i++) {
1689 if (vfc->ids[i] == 0)
1694 * 64 means the num bits of ids, one bit corresponds to
1698 /* count trailing zeroes */
1699 vbit = ~ids & (ids - 1);
1700 /* clear least significant bit set */
1701 ids ^= (ids ^ (ids - 1)) ^ vbit;
1706 ret = hns3vf_vlan_filter_configure(hns, vlan_id, on);
1709 "VF handle vlan table failed, ret =%d, on = %d",
1720 hns3vf_remove_all_vlan_table(struct hns3_adapter *hns)
1722 return hns3vf_handle_all_vlan_table(hns, 0);
1726 hns3vf_restore_vlan_conf(struct hns3_adapter *hns)
1728 struct hns3_hw *hw = &hns->hw;
1729 struct rte_eth_conf *dev_conf;
1733 dev_conf = &hw->data->dev_conf;
1734 en = dev_conf->rxmode.offloads & DEV_RX_OFFLOAD_VLAN_STRIP ? true
1736 ret = hns3vf_en_hw_strip_rxvtag(hw, en);
1738 hns3_err(hw, "VF restore vlan conf fail, en =%d, ret =%d", en,
1744 hns3vf_dev_configure_vlan(struct rte_eth_dev *dev)
1746 struct hns3_adapter *hns = dev->data->dev_private;
1747 struct rte_eth_dev_data *data = dev->data;
1748 struct hns3_hw *hw = &hns->hw;
1751 if (data->dev_conf.txmode.hw_vlan_reject_tagged ||
1752 data->dev_conf.txmode.hw_vlan_reject_untagged ||
1753 data->dev_conf.txmode.hw_vlan_insert_pvid) {
1754 hns3_warn(hw, "hw_vlan_reject_tagged, hw_vlan_reject_untagged "
1755 "or hw_vlan_insert_pvid is not support!");
1758 /* Apply vlan offload setting */
1759 ret = hns3vf_vlan_offload_set(dev, ETH_VLAN_STRIP_MASK |
1760 ETH_VLAN_FILTER_MASK);
1762 hns3_err(hw, "dev config vlan offload failed, ret = %d.", ret);
1768 hns3vf_set_alive(struct hns3_hw *hw, bool alive)
1772 msg_data = alive ? 1 : 0;
1773 return hns3_send_mbx_msg(hw, HNS3_MBX_SET_ALIVE, 0, &msg_data,
1774 sizeof(msg_data), false, NULL, 0);
1778 hns3vf_keep_alive_handler(void *param)
1780 struct rte_eth_dev *eth_dev = (struct rte_eth_dev *)param;
1781 struct hns3_adapter *hns = eth_dev->data->dev_private;
1782 struct hns3_hw *hw = &hns->hw;
1785 ret = hns3_send_mbx_msg(hw, HNS3_MBX_KEEP_ALIVE, 0, NULL, 0,
1788 hns3_err(hw, "VF sends keeping alive cmd failed(=%d)",
1791 rte_eal_alarm_set(HNS3VF_KEEP_ALIVE_INTERVAL, hns3vf_keep_alive_handler,
1796 hns3vf_service_handler(void *param)
1798 struct rte_eth_dev *eth_dev = (struct rte_eth_dev *)param;
1799 struct hns3_adapter *hns = eth_dev->data->dev_private;
1800 struct hns3_hw *hw = &hns->hw;
1803 * The query link status and reset processing are executed in the
1804 * interrupt thread. When the IMP reset occurs, IMP will not respond,
1805 * and the query operation will timeout after 30ms. In the case of
1806 * multiple PF/VFs, each query failure timeout causes the IMP reset
1807 * interrupt to fail to respond within 100ms.
1808 * Before querying the link status, check whether there is a reset
1809 * pending, and if so, abandon the query.
1811 if (!hns3vf_is_reset_pending(hns))
1812 hns3vf_request_link_info(hw);
1814 hns3_warn(hw, "Cancel the query when reset is pending");
1816 rte_eal_alarm_set(HNS3VF_SERVICE_INTERVAL, hns3vf_service_handler,
1821 hns3vf_start_poll_job(struct rte_eth_dev *dev)
1823 #define HNS3_REQUEST_LINK_INFO_REMAINS_CNT 3
1825 struct hns3_vf *vf = HNS3_DEV_PRIVATE_TO_VF(dev->data->dev_private);
1827 if (vf->pf_push_lsc_cap == HNS3_PF_PUSH_LSC_CAP_SUPPORTED)
1828 vf->req_link_info_cnt = HNS3_REQUEST_LINK_INFO_REMAINS_CNT;
1830 __atomic_store_n(&vf->poll_job_started, 1, __ATOMIC_RELAXED);
1832 hns3vf_service_handler(dev);
1836 hns3vf_stop_poll_job(struct rte_eth_dev *dev)
1838 struct hns3_vf *vf = HNS3_DEV_PRIVATE_TO_VF(dev->data->dev_private);
1840 rte_eal_alarm_cancel(hns3vf_service_handler, dev);
1842 __atomic_store_n(&vf->poll_job_started, 0, __ATOMIC_RELAXED);
1846 hns3_query_vf_resource(struct hns3_hw *hw)
1848 struct hns3_vf_res_cmd *req;
1849 struct hns3_cmd_desc desc;
1853 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_QUERY_VF_RSRC, true);
1854 ret = hns3_cmd_send(hw, &desc, 1);
1856 hns3_err(hw, "query vf resource failed, ret = %d", ret);
1860 req = (struct hns3_vf_res_cmd *)desc.data;
1861 num_msi = hns3_get_field(rte_le_to_cpu_16(req->vf_intr_vector_number),
1862 HNS3_VF_VEC_NUM_M, HNS3_VF_VEC_NUM_S);
1863 if (num_msi < HNS3_MIN_VECTOR_NUM) {
1864 hns3_err(hw, "Just %u msi resources, not enough for vf(min:%d)",
1865 num_msi, HNS3_MIN_VECTOR_NUM);
1869 hw->num_msi = num_msi;
1875 hns3vf_init_hardware(struct hns3_adapter *hns)
1877 struct hns3_hw *hw = &hns->hw;
1878 uint16_t mtu = hw->data->mtu;
1881 ret = hns3vf_set_promisc_mode(hw, true, false, false);
1885 ret = hns3vf_config_mtu(hw, mtu);
1887 goto err_init_hardware;
1889 ret = hns3vf_vlan_filter_configure(hns, 0, 1);
1891 PMD_INIT_LOG(ERR, "Failed to initialize VLAN config: %d", ret);
1892 goto err_init_hardware;
1895 ret = hns3_config_gro(hw, false);
1897 PMD_INIT_LOG(ERR, "Failed to config gro: %d", ret);
1898 goto err_init_hardware;
1902 * In the initialization clearing the all hardware mapping relationship
1903 * configurations between queues and interrupt vectors is needed, so
1904 * some error caused by the residual configurations, such as the
1905 * unexpected interrupt, can be avoid.
1907 ret = hns3vf_init_ring_with_vector(hw);
1909 PMD_INIT_LOG(ERR, "Failed to init ring intr vector: %d", ret);
1910 goto err_init_hardware;
1916 (void)hns3vf_set_promisc_mode(hw, false, false, false);
1921 hns3vf_clear_vport_list(struct hns3_hw *hw)
1923 return hns3_send_mbx_msg(hw, HNS3_MBX_HANDLE_VF_TBL,
1924 HNS3_MBX_VPORT_LIST_CLEAR, NULL, 0, false,
1929 hns3vf_init_vf(struct rte_eth_dev *eth_dev)
1931 struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(eth_dev);
1932 struct hns3_adapter *hns = eth_dev->data->dev_private;
1933 struct hns3_hw *hw = &hns->hw;
1936 PMD_INIT_FUNC_TRACE();
1938 /* Get hardware io base address from pcie BAR2 IO space */
1939 hw->io_base = pci_dev->mem_resource[2].addr;
1941 /* Firmware command queue initialize */
1942 ret = hns3_cmd_init_queue(hw);
1944 PMD_INIT_LOG(ERR, "Failed to init cmd queue: %d", ret);
1945 goto err_cmd_init_queue;
1948 /* Firmware command initialize */
1949 ret = hns3_cmd_init(hw);
1951 PMD_INIT_LOG(ERR, "Failed to init cmd: %d", ret);
1955 hns3_tx_push_init(eth_dev);
1957 /* Get VF resource */
1958 ret = hns3_query_vf_resource(hw);
1962 rte_spinlock_init(&hw->mbx_resp.lock);
1964 hns3vf_clear_event_cause(hw, 0);
1966 ret = rte_intr_callback_register(&pci_dev->intr_handle,
1967 hns3vf_interrupt_handler, eth_dev);
1969 PMD_INIT_LOG(ERR, "Failed to register intr: %d", ret);
1970 goto err_intr_callback_register;
1973 /* Enable interrupt */
1974 rte_intr_enable(&pci_dev->intr_handle);
1975 hns3vf_enable_irq0(hw);
1977 /* Get configuration from PF */
1978 ret = hns3vf_get_configuration(hw);
1980 PMD_INIT_LOG(ERR, "Failed to fetch configuration: %d", ret);
1981 goto err_get_config;
1984 ret = hns3_tqp_stats_init(hw);
1986 goto err_get_config;
1988 /* Hardware statistics of imissed registers cleared. */
1989 ret = hns3_update_imissed_stats(hw, true);
1991 hns3_err(hw, "clear imissed stats failed, ret = %d", ret);
1992 goto err_set_tc_queue;
1995 ret = hns3vf_set_tc_queue_mapping(hns, hw->tqps_num, hw->tqps_num);
1997 PMD_INIT_LOG(ERR, "failed to set tc info, ret = %d.", ret);
1998 goto err_set_tc_queue;
2001 ret = hns3vf_clear_vport_list(hw);
2003 PMD_INIT_LOG(ERR, "Failed to clear tbl list: %d", ret);
2004 goto err_set_tc_queue;
2007 ret = hns3vf_init_hardware(hns);
2009 goto err_set_tc_queue;
2011 hns3_rss_set_default_args(hw);
2013 ret = hns3vf_set_alive(hw, true);
2015 PMD_INIT_LOG(ERR, "Failed to VF send alive to PF: %d", ret);
2016 goto err_set_tc_queue;
2022 hns3_tqp_stats_uninit(hw);
2025 hns3vf_disable_irq0(hw);
2026 rte_intr_disable(&pci_dev->intr_handle);
2027 hns3_intr_unregister(&pci_dev->intr_handle, hns3vf_interrupt_handler,
2029 err_intr_callback_register:
2031 hns3_cmd_uninit(hw);
2032 hns3_cmd_destroy_queue(hw);
2040 hns3vf_uninit_vf(struct rte_eth_dev *eth_dev)
2042 struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(eth_dev);
2043 struct hns3_adapter *hns = eth_dev->data->dev_private;
2044 struct hns3_hw *hw = &hns->hw;
2046 PMD_INIT_FUNC_TRACE();
2048 hns3_rss_uninit(hns);
2049 (void)hns3_config_gro(hw, false);
2050 (void)hns3vf_set_alive(hw, false);
2051 (void)hns3vf_set_promisc_mode(hw, false, false, false);
2052 hns3_flow_uninit(eth_dev);
2053 hns3_tqp_stats_uninit(hw);
2054 hns3vf_disable_irq0(hw);
2055 rte_intr_disable(&pci_dev->intr_handle);
2056 hns3_intr_unregister(&pci_dev->intr_handle, hns3vf_interrupt_handler,
2058 hns3_cmd_uninit(hw);
2059 hns3_cmd_destroy_queue(hw);
2064 hns3vf_do_stop(struct hns3_adapter *hns)
2066 struct hns3_hw *hw = &hns->hw;
2069 hw->mac.link_status = ETH_LINK_DOWN;
2072 * The "hns3vf_do_stop" function will also be called by .stop_service to
2073 * prepare reset. At the time of global or IMP reset, the command cannot
2074 * be sent to stop the tx/rx queues. The mbuf in Tx/Rx queues may be
2075 * accessed during the reset process. So the mbuf can not be released
2076 * during reset and is required to be released after the reset is
2079 if (__atomic_load_n(&hw->reset.resetting, __ATOMIC_RELAXED) == 0)
2080 hns3_dev_release_mbufs(hns);
2082 if (__atomic_load_n(&hw->reset.disable_cmd, __ATOMIC_RELAXED) == 0) {
2083 hns3vf_configure_mac_addr(hns, true);
2084 ret = hns3_reset_all_tqps(hns);
2086 hns3_err(hw, "failed to reset all queues ret = %d",
2095 hns3vf_unmap_rx_interrupt(struct rte_eth_dev *dev)
2097 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2098 struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
2099 struct rte_intr_handle *intr_handle = &pci_dev->intr_handle;
2100 uint8_t base = RTE_INTR_VEC_ZERO_OFFSET;
2101 uint8_t vec = RTE_INTR_VEC_ZERO_OFFSET;
2104 if (dev->data->dev_conf.intr_conf.rxq == 0)
2107 /* unmap the ring with vector */
2108 if (rte_intr_allow_others(intr_handle)) {
2109 vec = RTE_INTR_VEC_RXTX_OFFSET;
2110 base = RTE_INTR_VEC_RXTX_OFFSET;
2112 if (rte_intr_dp_is_en(intr_handle)) {
2113 for (q_id = 0; q_id < hw->used_rx_queues; q_id++) {
2114 (void)hns3vf_bind_ring_with_vector(hw, vec, false,
2117 if (vec < base + intr_handle->nb_efd - 1)
2121 /* Clean datapath event and queue/vec mapping */
2122 rte_intr_efd_disable(intr_handle);
2123 if (intr_handle->intr_vec) {
2124 rte_free(intr_handle->intr_vec);
2125 intr_handle->intr_vec = NULL;
2130 hns3vf_dev_stop(struct rte_eth_dev *dev)
2132 struct hns3_adapter *hns = dev->data->dev_private;
2133 struct hns3_hw *hw = &hns->hw;
2135 PMD_INIT_FUNC_TRACE();
2136 dev->data->dev_started = 0;
2138 hw->adapter_state = HNS3_NIC_STOPPING;
2139 hns3_set_rxtx_function(dev);
2141 /* Disable datapath on secondary process. */
2142 hns3_mp_req_stop_rxtx(dev);
2143 /* Prevent crashes when queues are still in use. */
2144 rte_delay_ms(hw->cfg_max_queues);
2146 rte_spinlock_lock(&hw->lock);
2147 if (__atomic_load_n(&hw->reset.resetting, __ATOMIC_RELAXED) == 0) {
2149 hns3vf_do_stop(hns);
2150 hns3vf_unmap_rx_interrupt(dev);
2151 hw->adapter_state = HNS3_NIC_CONFIGURED;
2153 hns3_rx_scattered_reset(dev);
2154 hns3vf_stop_poll_job(dev);
2155 hns3_stop_report_lse(dev);
2156 rte_spinlock_unlock(&hw->lock);
2162 hns3vf_dev_close(struct rte_eth_dev *eth_dev)
2164 struct hns3_adapter *hns = eth_dev->data->dev_private;
2165 struct hns3_hw *hw = &hns->hw;
2168 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
2171 if (hw->adapter_state == HNS3_NIC_STARTED)
2172 ret = hns3vf_dev_stop(eth_dev);
2174 hw->adapter_state = HNS3_NIC_CLOSING;
2175 hns3_reset_abort(hns);
2176 hw->adapter_state = HNS3_NIC_CLOSED;
2177 rte_eal_alarm_cancel(hns3vf_keep_alive_handler, eth_dev);
2178 hns3vf_configure_all_mc_mac_addr(hns, true);
2179 hns3vf_remove_all_vlan_table(hns);
2180 hns3vf_uninit_vf(eth_dev);
2181 hns3_free_all_queues(eth_dev);
2182 rte_free(hw->reset.wait_data);
2183 hns3_mp_uninit_primary();
2184 hns3_warn(hw, "Close port %u finished", hw->data->port_id);
2190 hns3vf_fw_version_get(struct rte_eth_dev *eth_dev, char *fw_version,
2193 struct hns3_adapter *hns = eth_dev->data->dev_private;
2194 struct hns3_hw *hw = &hns->hw;
2195 uint32_t version = hw->fw_version;
2198 ret = snprintf(fw_version, fw_size, "%lu.%lu.%lu.%lu",
2199 hns3_get_field(version, HNS3_FW_VERSION_BYTE3_M,
2200 HNS3_FW_VERSION_BYTE3_S),
2201 hns3_get_field(version, HNS3_FW_VERSION_BYTE2_M,
2202 HNS3_FW_VERSION_BYTE2_S),
2203 hns3_get_field(version, HNS3_FW_VERSION_BYTE1_M,
2204 HNS3_FW_VERSION_BYTE1_S),
2205 hns3_get_field(version, HNS3_FW_VERSION_BYTE0_M,
2206 HNS3_FW_VERSION_BYTE0_S));
2210 ret += 1; /* add the size of '\0' */
2211 if (fw_size < (size_t)ret)
2218 hns3vf_dev_link_update(struct rte_eth_dev *eth_dev,
2219 __rte_unused int wait_to_complete)
2221 struct hns3_adapter *hns = eth_dev->data->dev_private;
2222 struct hns3_hw *hw = &hns->hw;
2223 struct hns3_mac *mac = &hw->mac;
2224 struct rte_eth_link new_link;
2226 memset(&new_link, 0, sizeof(new_link));
2227 switch (mac->link_speed) {
2228 case ETH_SPEED_NUM_10M:
2229 case ETH_SPEED_NUM_100M:
2230 case ETH_SPEED_NUM_1G:
2231 case ETH_SPEED_NUM_10G:
2232 case ETH_SPEED_NUM_25G:
2233 case ETH_SPEED_NUM_40G:
2234 case ETH_SPEED_NUM_50G:
2235 case ETH_SPEED_NUM_100G:
2236 case ETH_SPEED_NUM_200G:
2237 if (mac->link_status)
2238 new_link.link_speed = mac->link_speed;
2241 if (mac->link_status)
2242 new_link.link_speed = ETH_SPEED_NUM_UNKNOWN;
2246 if (!mac->link_status)
2247 new_link.link_speed = ETH_SPEED_NUM_NONE;
2249 new_link.link_duplex = mac->link_duplex;
2250 new_link.link_status = mac->link_status ? ETH_LINK_UP : ETH_LINK_DOWN;
2251 new_link.link_autoneg =
2252 !(eth_dev->data->dev_conf.link_speeds & ETH_LINK_SPEED_FIXED);
2254 return rte_eth_linkstatus_set(eth_dev, &new_link);
2258 hns3vf_do_start(struct hns3_adapter *hns, bool reset_queue)
2260 struct hns3_hw *hw = &hns->hw;
2261 uint16_t nb_rx_q = hw->data->nb_rx_queues;
2262 uint16_t nb_tx_q = hw->data->nb_tx_queues;
2265 ret = hns3vf_set_tc_queue_mapping(hns, nb_rx_q, nb_tx_q);
2269 hns3_enable_rxd_adv_layout(hw);
2271 ret = hns3_init_queues(hns, reset_queue);
2273 hns3_err(hw, "failed to init queues, ret = %d.", ret);
2279 hns3vf_map_rx_interrupt(struct rte_eth_dev *dev)
2281 struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
2282 struct rte_intr_handle *intr_handle = &pci_dev->intr_handle;
2283 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2284 uint8_t base = RTE_INTR_VEC_ZERO_OFFSET;
2285 uint8_t vec = RTE_INTR_VEC_ZERO_OFFSET;
2286 uint32_t intr_vector;
2291 * hns3 needs a separate interrupt to be used as event interrupt which
2292 * could not be shared with task queue pair, so KERNEL drivers need
2293 * support multiple interrupt vectors.
2295 if (dev->data->dev_conf.intr_conf.rxq == 0 ||
2296 !rte_intr_cap_multiple(intr_handle))
2299 rte_intr_disable(intr_handle);
2300 intr_vector = hw->used_rx_queues;
2301 /* It creates event fd for each intr vector when MSIX is used */
2302 if (rte_intr_efd_enable(intr_handle, intr_vector))
2305 if (intr_handle->intr_vec == NULL) {
2306 intr_handle->intr_vec =
2307 rte_zmalloc("intr_vec",
2308 hw->used_rx_queues * sizeof(int), 0);
2309 if (intr_handle->intr_vec == NULL) {
2310 hns3_err(hw, "Failed to allocate %u rx_queues"
2311 " intr_vec", hw->used_rx_queues);
2313 goto vf_alloc_intr_vec_error;
2317 if (rte_intr_allow_others(intr_handle)) {
2318 vec = RTE_INTR_VEC_RXTX_OFFSET;
2319 base = RTE_INTR_VEC_RXTX_OFFSET;
2322 for (q_id = 0; q_id < hw->used_rx_queues; q_id++) {
2323 ret = hns3vf_bind_ring_with_vector(hw, vec, true,
2324 HNS3_RING_TYPE_RX, q_id);
2326 goto vf_bind_vector_error;
2327 intr_handle->intr_vec[q_id] = vec;
2329 * If there are not enough efds (e.g. not enough interrupt),
2330 * remaining queues will be bond to the last interrupt.
2332 if (vec < base + intr_handle->nb_efd - 1)
2335 rte_intr_enable(intr_handle);
2338 vf_bind_vector_error:
2339 rte_free(intr_handle->intr_vec);
2340 intr_handle->intr_vec = NULL;
2341 vf_alloc_intr_vec_error:
2342 rte_intr_efd_disable(intr_handle);
2347 hns3vf_restore_rx_interrupt(struct hns3_hw *hw)
2349 struct rte_eth_dev *dev = &rte_eth_devices[hw->data->port_id];
2350 struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
2351 struct rte_intr_handle *intr_handle = &pci_dev->intr_handle;
2355 if (dev->data->dev_conf.intr_conf.rxq == 0)
2358 if (rte_intr_dp_is_en(intr_handle)) {
2359 for (q_id = 0; q_id < hw->used_rx_queues; q_id++) {
2360 ret = hns3vf_bind_ring_with_vector(hw,
2361 intr_handle->intr_vec[q_id], true,
2362 HNS3_RING_TYPE_RX, q_id);
2372 hns3vf_restore_filter(struct rte_eth_dev *dev)
2374 hns3_restore_rss_filter(dev);
2378 hns3vf_dev_start(struct rte_eth_dev *dev)
2380 struct hns3_adapter *hns = dev->data->dev_private;
2381 struct hns3_hw *hw = &hns->hw;
2384 PMD_INIT_FUNC_TRACE();
2385 if (__atomic_load_n(&hw->reset.resetting, __ATOMIC_RELAXED))
2388 rte_spinlock_lock(&hw->lock);
2389 hw->adapter_state = HNS3_NIC_STARTING;
2390 ret = hns3vf_do_start(hns, true);
2392 hw->adapter_state = HNS3_NIC_CONFIGURED;
2393 rte_spinlock_unlock(&hw->lock);
2396 ret = hns3vf_map_rx_interrupt(dev);
2398 goto map_rx_inter_err;
2401 * There are three register used to control the status of a TQP
2402 * (contains a pair of Tx queue and Rx queue) in the new version network
2403 * engine. One is used to control the enabling of Tx queue, the other is
2404 * used to control the enabling of Rx queue, and the last is the master
2405 * switch used to control the enabling of the tqp. The Tx register and
2406 * TQP register must be enabled at the same time to enable a Tx queue.
2407 * The same applies to the Rx queue. For the older network enginem, this
2408 * function only refresh the enabled flag, and it is used to update the
2409 * status of queue in the dpdk framework.
2411 ret = hns3_start_all_txqs(dev);
2413 goto map_rx_inter_err;
2415 ret = hns3_start_all_rxqs(dev);
2417 goto start_all_rxqs_fail;
2419 hw->adapter_state = HNS3_NIC_STARTED;
2420 rte_spinlock_unlock(&hw->lock);
2422 hns3_rx_scattered_calc(dev);
2423 hns3_set_rxtx_function(dev);
2424 hns3_mp_req_start_rxtx(dev);
2426 hns3vf_restore_filter(dev);
2428 /* Enable interrupt of all rx queues before enabling queues */
2429 hns3_dev_all_rx_queue_intr_enable(hw, true);
2430 hns3_start_tqps(hw);
2432 if (dev->data->dev_conf.intr_conf.lsc != 0)
2433 hns3vf_dev_link_update(dev, 0);
2434 hns3vf_start_poll_job(dev);
2438 start_all_rxqs_fail:
2439 hns3_stop_all_txqs(dev);
2441 (void)hns3vf_do_stop(hns);
2442 hw->adapter_state = HNS3_NIC_CONFIGURED;
2443 rte_spinlock_unlock(&hw->lock);
2449 is_vf_reset_done(struct hns3_hw *hw)
2451 #define HNS3_FUN_RST_ING_BITS \
2452 (BIT(HNS3_VECTOR0_GLOBALRESET_INT_B) | \
2453 BIT(HNS3_VECTOR0_CORERESET_INT_B) | \
2454 BIT(HNS3_VECTOR0_IMPRESET_INT_B) | \
2455 BIT(HNS3_VECTOR0_FUNCRESET_INT_B))
2459 if (hw->reset.level == HNS3_VF_RESET) {
2460 val = hns3_read_dev(hw, HNS3_VF_RST_ING);
2461 if (val & HNS3_VF_RST_ING_BIT)
2464 val = hns3_read_dev(hw, HNS3_FUN_RST_ING);
2465 if (val & HNS3_FUN_RST_ING_BITS)
2472 hns3vf_is_reset_pending(struct hns3_adapter *hns)
2474 struct hns3_hw *hw = &hns->hw;
2475 enum hns3_reset_level reset;
2478 * According to the protocol of PCIe, FLR to a PF device resets the PF
2479 * state as well as the SR-IOV extended capability including VF Enable
2480 * which means that VFs no longer exist.
2482 * HNS3_VF_FULL_RESET means PF device is in FLR reset. when PF device
2483 * is in FLR stage, the register state of VF device is not reliable,
2484 * so register states detection can not be carried out. In this case,
2485 * we just ignore the register states and return false to indicate that
2486 * there are no other reset states that need to be processed by driver.
2488 if (hw->reset.level == HNS3_VF_FULL_RESET)
2491 /* Check the registers to confirm whether there is reset pending */
2492 hns3vf_check_event_cause(hns, NULL);
2493 reset = hns3vf_get_reset_level(hw, &hw->reset.pending);
2494 if (hw->reset.level != HNS3_NONE_RESET && reset != HNS3_NONE_RESET &&
2495 hw->reset.level < reset) {
2496 hns3_warn(hw, "High level reset %d is pending", reset);
2503 hns3vf_wait_hardware_ready(struct hns3_adapter *hns)
2505 struct hns3_hw *hw = &hns->hw;
2506 struct hns3_wait_data *wait_data = hw->reset.wait_data;
2509 if (wait_data->result == HNS3_WAIT_SUCCESS) {
2511 * After vf reset is ready, the PF may not have completed
2512 * the reset processing. The vf sending mbox to PF may fail
2513 * during the pf reset, so it is better to add extra delay.
2515 if (hw->reset.level == HNS3_VF_FUNC_RESET ||
2516 hw->reset.level == HNS3_FLR_RESET)
2518 /* Reset retry process, no need to add extra delay. */
2519 if (hw->reset.attempts)
2521 if (wait_data->check_completion == NULL)
2524 wait_data->check_completion = NULL;
2525 wait_data->interval = 1 * MSEC_PER_SEC * USEC_PER_MSEC;
2526 wait_data->count = 1;
2527 wait_data->result = HNS3_WAIT_REQUEST;
2528 rte_eal_alarm_set(wait_data->interval, hns3_wait_callback,
2530 hns3_warn(hw, "hardware is ready, delay 1 sec for PF reset complete");
2532 } else if (wait_data->result == HNS3_WAIT_TIMEOUT) {
2533 hns3_clock_gettime(&tv);
2534 hns3_warn(hw, "Reset step4 hardware not ready after reset time=%ld.%.6ld",
2535 tv.tv_sec, tv.tv_usec);
2537 } else if (wait_data->result == HNS3_WAIT_REQUEST)
2540 wait_data->hns = hns;
2541 wait_data->check_completion = is_vf_reset_done;
2542 wait_data->end_ms = (uint64_t)HNS3VF_RESET_WAIT_CNT *
2543 HNS3VF_RESET_WAIT_MS + hns3_clock_gettime_ms();
2544 wait_data->interval = HNS3VF_RESET_WAIT_MS * USEC_PER_MSEC;
2545 wait_data->count = HNS3VF_RESET_WAIT_CNT;
2546 wait_data->result = HNS3_WAIT_REQUEST;
2547 rte_eal_alarm_set(wait_data->interval, hns3_wait_callback, wait_data);
2552 hns3vf_prepare_reset(struct hns3_adapter *hns)
2554 struct hns3_hw *hw = &hns->hw;
2557 if (hw->reset.level == HNS3_VF_FUNC_RESET) {
2558 ret = hns3_send_mbx_msg(hw, HNS3_MBX_RESET, 0, NULL,
2563 __atomic_store_n(&hw->reset.disable_cmd, 1, __ATOMIC_RELAXED);
2569 hns3vf_stop_service(struct hns3_adapter *hns)
2571 struct hns3_hw *hw = &hns->hw;
2572 struct rte_eth_dev *eth_dev;
2574 eth_dev = &rte_eth_devices[hw->data->port_id];
2575 if (hw->adapter_state == HNS3_NIC_STARTED) {
2577 * Make sure call update link status before hns3vf_stop_poll_job
2578 * because update link status depend on polling job exist.
2580 hns3vf_update_link_status(hw, ETH_LINK_DOWN, hw->mac.link_speed,
2581 hw->mac.link_duplex);
2582 hns3vf_stop_poll_job(eth_dev);
2584 hw->mac.link_status = ETH_LINK_DOWN;
2586 hns3_set_rxtx_function(eth_dev);
2588 /* Disable datapath on secondary process. */
2589 hns3_mp_req_stop_rxtx(eth_dev);
2590 rte_delay_ms(hw->cfg_max_queues);
2592 rte_spinlock_lock(&hw->lock);
2593 if (hw->adapter_state == HNS3_NIC_STARTED ||
2594 hw->adapter_state == HNS3_NIC_STOPPING) {
2595 hns3_enable_all_queues(hw, false);
2596 hns3vf_do_stop(hns);
2597 hw->reset.mbuf_deferred_free = true;
2599 hw->reset.mbuf_deferred_free = false;
2602 * It is cumbersome for hardware to pick-and-choose entries for deletion
2603 * from table space. Hence, for function reset software intervention is
2604 * required to delete the entries.
2606 if (__atomic_load_n(&hw->reset.disable_cmd, __ATOMIC_RELAXED) == 0)
2607 hns3vf_configure_all_mc_mac_addr(hns, true);
2608 rte_spinlock_unlock(&hw->lock);
2614 hns3vf_start_service(struct hns3_adapter *hns)
2616 struct hns3_hw *hw = &hns->hw;
2617 struct rte_eth_dev *eth_dev;
2619 eth_dev = &rte_eth_devices[hw->data->port_id];
2620 hns3_set_rxtx_function(eth_dev);
2621 hns3_mp_req_start_rxtx(eth_dev);
2622 if (hw->adapter_state == HNS3_NIC_STARTED) {
2623 hns3vf_start_poll_job(eth_dev);
2625 /* Enable interrupt of all rx queues before enabling queues */
2626 hns3_dev_all_rx_queue_intr_enable(hw, true);
2628 * Enable state of each rxq and txq will be recovered after
2629 * reset, so we need to restore them before enable all tqps;
2631 hns3_restore_tqp_enable_state(hw);
2633 * When finished the initialization, enable queues to receive
2634 * and transmit packets.
2636 hns3_enable_all_queues(hw, true);
2643 hns3vf_check_default_mac_change(struct hns3_hw *hw)
2645 char mac_str[RTE_ETHER_ADDR_FMT_SIZE];
2646 struct rte_ether_addr *hw_mac;
2650 * The hns3 PF ethdev driver in kernel support setting VF MAC address
2651 * on the host by "ip link set ..." command. If the hns3 PF kernel
2652 * ethdev driver sets the MAC address for VF device after the
2653 * initialization of the related VF device, the PF driver will notify
2654 * VF driver to reset VF device to make the new MAC address effective
2655 * immediately. The hns3 VF PMD driver should check whether the MAC
2656 * address has been changed by the PF kernel ethdev driver, if changed
2657 * VF driver should configure hardware using the new MAC address in the
2658 * recovering hardware configuration stage of the reset process.
2660 ret = hns3vf_get_host_mac_addr(hw);
2664 hw_mac = (struct rte_ether_addr *)hw->mac.mac_addr;
2665 ret = rte_is_zero_ether_addr(hw_mac);
2667 rte_ether_addr_copy(&hw->data->mac_addrs[0], hw_mac);
2669 ret = rte_is_same_ether_addr(&hw->data->mac_addrs[0], hw_mac);
2671 rte_ether_addr_copy(hw_mac, &hw->data->mac_addrs[0]);
2672 hns3_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
2673 &hw->data->mac_addrs[0]);
2674 hns3_warn(hw, "Default MAC address has been changed to:"
2675 " %s by the host PF kernel ethdev driver",
2684 hns3vf_restore_conf(struct hns3_adapter *hns)
2686 struct hns3_hw *hw = &hns->hw;
2689 ret = hns3vf_check_default_mac_change(hw);
2693 ret = hns3vf_configure_mac_addr(hns, false);
2697 ret = hns3vf_configure_all_mc_mac_addr(hns, false);
2701 ret = hns3vf_restore_promisc(hns);
2703 goto err_vlan_table;
2705 ret = hns3vf_restore_vlan_conf(hns);
2707 goto err_vlan_table;
2709 ret = hns3vf_get_port_base_vlan_filter_state(hw);
2711 goto err_vlan_table;
2713 ret = hns3vf_restore_rx_interrupt(hw);
2715 goto err_vlan_table;
2717 ret = hns3_restore_gro_conf(hw);
2719 goto err_vlan_table;
2721 if (hw->adapter_state == HNS3_NIC_STARTED) {
2722 ret = hns3vf_do_start(hns, false);
2724 goto err_vlan_table;
2725 hns3_info(hw, "hns3vf dev restart successful!");
2726 } else if (hw->adapter_state == HNS3_NIC_STOPPING)
2727 hw->adapter_state = HNS3_NIC_CONFIGURED;
2729 ret = hns3vf_set_alive(hw, true);
2731 hns3_err(hw, "failed to VF send alive to PF: %d", ret);
2732 goto err_vlan_table;
2738 hns3vf_configure_all_mc_mac_addr(hns, true);
2740 hns3vf_configure_mac_addr(hns, true);
2744 static enum hns3_reset_level
2745 hns3vf_get_reset_level(struct hns3_hw *hw, uint64_t *levels)
2747 enum hns3_reset_level reset_level;
2749 /* return the highest priority reset level amongst all */
2750 if (hns3_atomic_test_bit(HNS3_VF_RESET, levels))
2751 reset_level = HNS3_VF_RESET;
2752 else if (hns3_atomic_test_bit(HNS3_VF_FULL_RESET, levels))
2753 reset_level = HNS3_VF_FULL_RESET;
2754 else if (hns3_atomic_test_bit(HNS3_VF_PF_FUNC_RESET, levels))
2755 reset_level = HNS3_VF_PF_FUNC_RESET;
2756 else if (hns3_atomic_test_bit(HNS3_VF_FUNC_RESET, levels))
2757 reset_level = HNS3_VF_FUNC_RESET;
2758 else if (hns3_atomic_test_bit(HNS3_FLR_RESET, levels))
2759 reset_level = HNS3_FLR_RESET;
2761 reset_level = HNS3_NONE_RESET;
2763 if (hw->reset.level != HNS3_NONE_RESET && reset_level < hw->reset.level)
2764 return HNS3_NONE_RESET;
2770 hns3vf_reset_service(void *param)
2772 struct hns3_adapter *hns = (struct hns3_adapter *)param;
2773 struct hns3_hw *hw = &hns->hw;
2774 enum hns3_reset_level reset_level;
2775 struct timeval tv_delta;
2776 struct timeval tv_start;
2781 * The interrupt is not triggered within the delay time.
2782 * The interrupt may have been lost. It is necessary to handle
2783 * the interrupt to recover from the error.
2785 if (__atomic_load_n(&hw->reset.schedule, __ATOMIC_RELAXED) ==
2786 SCHEDULE_DEFERRED) {
2787 __atomic_store_n(&hw->reset.schedule, SCHEDULE_REQUESTED,
2789 hns3_err(hw, "Handling interrupts in delayed tasks");
2790 hns3vf_interrupt_handler(&rte_eth_devices[hw->data->port_id]);
2791 reset_level = hns3vf_get_reset_level(hw, &hw->reset.pending);
2792 if (reset_level == HNS3_NONE_RESET) {
2793 hns3_err(hw, "No reset level is set, try global reset");
2794 hns3_atomic_set_bit(HNS3_VF_RESET, &hw->reset.pending);
2797 __atomic_store_n(&hw->reset.schedule, SCHEDULE_NONE, __ATOMIC_RELAXED);
2800 * Hardware reset has been notified, we now have to poll & check if
2801 * hardware has actually completed the reset sequence.
2803 reset_level = hns3vf_get_reset_level(hw, &hw->reset.pending);
2804 if (reset_level != HNS3_NONE_RESET) {
2805 hns3_clock_gettime(&tv_start);
2806 hns3_reset_process(hns, reset_level);
2807 hns3_clock_gettime(&tv);
2808 timersub(&tv, &tv_start, &tv_delta);
2809 msec = hns3_clock_calctime_ms(&tv_delta);
2810 if (msec > HNS3_RESET_PROCESS_MS)
2811 hns3_err(hw, "%d handle long time delta %" PRIu64
2812 " ms time=%ld.%.6ld",
2813 hw->reset.level, msec, tv.tv_sec, tv.tv_usec);
2818 hns3vf_reinit_dev(struct hns3_adapter *hns)
2820 struct rte_eth_dev *eth_dev = &rte_eth_devices[hns->hw.data->port_id];
2821 struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(eth_dev);
2822 struct hns3_hw *hw = &hns->hw;
2825 if (hw->reset.level == HNS3_VF_FULL_RESET) {
2826 rte_intr_disable(&pci_dev->intr_handle);
2827 ret = hns3vf_set_bus_master(pci_dev, true);
2829 hns3_err(hw, "failed to set pci bus, ret = %d", ret);
2834 /* Firmware command initialize */
2835 ret = hns3_cmd_init(hw);
2837 hns3_err(hw, "Failed to init cmd: %d", ret);
2841 if (hw->reset.level == HNS3_VF_FULL_RESET) {
2843 * UIO enables msix by writing the pcie configuration space
2844 * vfio_pci enables msix in rte_intr_enable.
2846 if (pci_dev->kdrv == RTE_PCI_KDRV_IGB_UIO ||
2847 pci_dev->kdrv == RTE_PCI_KDRV_UIO_GENERIC) {
2848 if (hns3vf_enable_msix(pci_dev, true))
2849 hns3_err(hw, "Failed to enable msix");
2852 rte_intr_enable(&pci_dev->intr_handle);
2855 ret = hns3_reset_all_tqps(hns);
2857 hns3_err(hw, "Failed to reset all queues: %d", ret);
2861 ret = hns3vf_init_hardware(hns);
2863 hns3_err(hw, "Failed to init hardware: %d", ret);
2870 static const struct eth_dev_ops hns3vf_eth_dev_ops = {
2871 .dev_configure = hns3vf_dev_configure,
2872 .dev_start = hns3vf_dev_start,
2873 .dev_stop = hns3vf_dev_stop,
2874 .dev_close = hns3vf_dev_close,
2875 .mtu_set = hns3vf_dev_mtu_set,
2876 .promiscuous_enable = hns3vf_dev_promiscuous_enable,
2877 .promiscuous_disable = hns3vf_dev_promiscuous_disable,
2878 .allmulticast_enable = hns3vf_dev_allmulticast_enable,
2879 .allmulticast_disable = hns3vf_dev_allmulticast_disable,
2880 .stats_get = hns3_stats_get,
2881 .stats_reset = hns3_stats_reset,
2882 .xstats_get = hns3_dev_xstats_get,
2883 .xstats_get_names = hns3_dev_xstats_get_names,
2884 .xstats_reset = hns3_dev_xstats_reset,
2885 .xstats_get_by_id = hns3_dev_xstats_get_by_id,
2886 .xstats_get_names_by_id = hns3_dev_xstats_get_names_by_id,
2887 .dev_infos_get = hns3vf_dev_infos_get,
2888 .fw_version_get = hns3vf_fw_version_get,
2889 .rx_queue_setup = hns3_rx_queue_setup,
2890 .tx_queue_setup = hns3_tx_queue_setup,
2891 .rx_queue_release = hns3_dev_rx_queue_release,
2892 .tx_queue_release = hns3_dev_tx_queue_release,
2893 .rx_queue_start = hns3_dev_rx_queue_start,
2894 .rx_queue_stop = hns3_dev_rx_queue_stop,
2895 .tx_queue_start = hns3_dev_tx_queue_start,
2896 .tx_queue_stop = hns3_dev_tx_queue_stop,
2897 .rx_queue_intr_enable = hns3_dev_rx_queue_intr_enable,
2898 .rx_queue_intr_disable = hns3_dev_rx_queue_intr_disable,
2899 .rxq_info_get = hns3_rxq_info_get,
2900 .txq_info_get = hns3_txq_info_get,
2901 .rx_burst_mode_get = hns3_rx_burst_mode_get,
2902 .tx_burst_mode_get = hns3_tx_burst_mode_get,
2903 .mac_addr_add = hns3vf_add_mac_addr,
2904 .mac_addr_remove = hns3vf_remove_mac_addr,
2905 .mac_addr_set = hns3vf_set_default_mac_addr,
2906 .set_mc_addr_list = hns3vf_set_mc_mac_addr_list,
2907 .link_update = hns3vf_dev_link_update,
2908 .rss_hash_update = hns3_dev_rss_hash_update,
2909 .rss_hash_conf_get = hns3_dev_rss_hash_conf_get,
2910 .reta_update = hns3_dev_rss_reta_update,
2911 .reta_query = hns3_dev_rss_reta_query,
2912 .flow_ops_get = hns3_dev_flow_ops_get,
2913 .vlan_filter_set = hns3vf_vlan_filter_set,
2914 .vlan_offload_set = hns3vf_vlan_offload_set,
2915 .get_reg = hns3_get_regs,
2916 .dev_supported_ptypes_get = hns3_dev_supported_ptypes_get,
2917 .tx_done_cleanup = hns3_tx_done_cleanup,
2920 static const struct hns3_reset_ops hns3vf_reset_ops = {
2921 .reset_service = hns3vf_reset_service,
2922 .stop_service = hns3vf_stop_service,
2923 .prepare_reset = hns3vf_prepare_reset,
2924 .wait_hardware_ready = hns3vf_wait_hardware_ready,
2925 .reinit_dev = hns3vf_reinit_dev,
2926 .restore_conf = hns3vf_restore_conf,
2927 .start_service = hns3vf_start_service,
2931 hns3vf_dev_init(struct rte_eth_dev *eth_dev)
2933 struct hns3_adapter *hns = eth_dev->data->dev_private;
2934 struct hns3_hw *hw = &hns->hw;
2937 PMD_INIT_FUNC_TRACE();
2939 hns3_flow_init(eth_dev);
2941 hns3_set_rxtx_function(eth_dev);
2942 eth_dev->dev_ops = &hns3vf_eth_dev_ops;
2943 eth_dev->rx_queue_count = hns3_rx_queue_count;
2944 if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
2945 ret = hns3_mp_init_secondary();
2947 PMD_INIT_LOG(ERR, "Failed to init for secondary "
2948 "process, ret = %d", ret);
2949 goto err_mp_init_secondary;
2951 hw->secondary_cnt++;
2952 hns3_tx_push_init(eth_dev);
2956 ret = hns3_mp_init_primary();
2959 "Failed to init for primary process, ret = %d",
2961 goto err_mp_init_primary;
2964 hw->adapter_state = HNS3_NIC_UNINITIALIZED;
2966 hw->data = eth_dev->data;
2967 hns3_parse_devargs(eth_dev);
2969 ret = hns3_reset_init(hw);
2971 goto err_init_reset;
2972 hw->reset.ops = &hns3vf_reset_ops;
2974 ret = hns3vf_init_vf(eth_dev);
2976 PMD_INIT_LOG(ERR, "Failed to init vf: %d", ret);
2980 /* Allocate memory for storing MAC addresses */
2981 eth_dev->data->mac_addrs = rte_zmalloc("hns3vf-mac",
2982 sizeof(struct rte_ether_addr) *
2983 HNS3_VF_UC_MACADDR_NUM, 0);
2984 if (eth_dev->data->mac_addrs == NULL) {
2985 PMD_INIT_LOG(ERR, "Failed to allocate %zx bytes needed "
2986 "to store MAC addresses",
2987 sizeof(struct rte_ether_addr) *
2988 HNS3_VF_UC_MACADDR_NUM);
2990 goto err_rte_zmalloc;
2994 * The hns3 PF ethdev driver in kernel support setting VF MAC address
2995 * on the host by "ip link set ..." command. To avoid some incorrect
2996 * scenes, for example, hns3 VF PMD driver fails to receive and send
2997 * packets after user configure the MAC address by using the
2998 * "ip link set ..." command, hns3 VF PMD driver keep the same MAC
2999 * address strategy as the hns3 kernel ethdev driver in the
3000 * initialization. If user configure a MAC address by the ip command
3001 * for VF device, then hns3 VF PMD driver will start with it, otherwise
3002 * start with a random MAC address in the initialization.
3004 if (rte_is_zero_ether_addr((struct rte_ether_addr *)hw->mac.mac_addr))
3005 rte_eth_random_addr(hw->mac.mac_addr);
3006 rte_ether_addr_copy((struct rte_ether_addr *)hw->mac.mac_addr,
3007 ð_dev->data->mac_addrs[0]);
3009 hw->adapter_state = HNS3_NIC_INITIALIZED;
3011 if (__atomic_load_n(&hw->reset.schedule, __ATOMIC_RELAXED) ==
3013 hns3_err(hw, "Reschedule reset service after dev_init");
3014 hns3_schedule_reset(hns);
3016 /* IMP will wait ready flag before reset */
3017 hns3_notify_reset_ready(hw, false);
3019 rte_eal_alarm_set(HNS3VF_KEEP_ALIVE_INTERVAL, hns3vf_keep_alive_handler,
3024 hns3vf_uninit_vf(eth_dev);
3027 rte_free(hw->reset.wait_data);
3030 hns3_mp_uninit_primary();
3032 err_mp_init_primary:
3033 err_mp_init_secondary:
3034 eth_dev->dev_ops = NULL;
3035 eth_dev->rx_pkt_burst = NULL;
3036 eth_dev->rx_descriptor_status = NULL;
3037 eth_dev->tx_pkt_burst = NULL;
3038 eth_dev->tx_pkt_prepare = NULL;
3039 eth_dev->tx_descriptor_status = NULL;
3045 hns3vf_dev_uninit(struct rte_eth_dev *eth_dev)
3047 struct hns3_adapter *hns = eth_dev->data->dev_private;
3048 struct hns3_hw *hw = &hns->hw;
3050 PMD_INIT_FUNC_TRACE();
3052 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
3055 if (hw->adapter_state < HNS3_NIC_CLOSING)
3056 hns3vf_dev_close(eth_dev);
3058 hw->adapter_state = HNS3_NIC_REMOVED;
3063 eth_hns3vf_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
3064 struct rte_pci_device *pci_dev)
3066 return rte_eth_dev_pci_generic_probe(pci_dev,
3067 sizeof(struct hns3_adapter),
3072 eth_hns3vf_pci_remove(struct rte_pci_device *pci_dev)
3074 return rte_eth_dev_pci_generic_remove(pci_dev, hns3vf_dev_uninit);
3077 static const struct rte_pci_id pci_id_hns3vf_map[] = {
3078 { RTE_PCI_DEVICE(PCI_VENDOR_ID_HUAWEI, HNS3_DEV_ID_100G_VF) },
3079 { RTE_PCI_DEVICE(PCI_VENDOR_ID_HUAWEI, HNS3_DEV_ID_100G_RDMA_PFC_VF) },
3080 { .vendor_id = 0, }, /* sentinel */
3083 static struct rte_pci_driver rte_hns3vf_pmd = {
3084 .id_table = pci_id_hns3vf_map,
3085 .drv_flags = RTE_PCI_DRV_NEED_MAPPING | RTE_PCI_DRV_INTR_LSC,
3086 .probe = eth_hns3vf_pci_probe,
3087 .remove = eth_hns3vf_pci_remove,
3090 RTE_PMD_REGISTER_PCI(net_hns3_vf, rte_hns3vf_pmd);
3091 RTE_PMD_REGISTER_PCI_TABLE(net_hns3_vf, pci_id_hns3vf_map);
3092 RTE_PMD_REGISTER_KMOD_DEP(net_hns3_vf, "* igb_uio | vfio-pci");
3093 RTE_PMD_REGISTER_PARAM_STRING(net_hns3_vf,
3094 HNS3_DEVARG_RX_FUNC_HINT "=vec|sve|simple|common "
3095 HNS3_DEVARG_TX_FUNC_HINT "=vec|sve|simple|common "
3096 HNS3_DEVARG_DEV_CAPS_MASK "=<1-65535> ");