1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2018-2019 Hisilicon Limited.
11 #include <arpa/inet.h>
12 #include <linux/pci_regs.h>
14 #include <rte_alarm.h>
15 #include <rte_atomic.h>
16 #include <rte_bus_pci.h>
17 #include <rte_byteorder.h>
18 #include <rte_common.h>
19 #include <rte_cycles.h>
22 #include <rte_ether.h>
23 #include <rte_ethdev_driver.h>
24 #include <rte_ethdev_pci.h>
25 #include <rte_interrupts.h>
31 #include "hns3_ethdev.h"
32 #include "hns3_logs.h"
33 #include "hns3_rxtx.h"
34 #include "hns3_regs.h"
35 #include "hns3_intr.h"
39 #define HNS3VF_KEEP_ALIVE_INTERVAL 2000000 /* us */
40 #define HNS3VF_SERVICE_INTERVAL 1000000 /* us */
42 #define HNS3VF_RESET_WAIT_MS 20
43 #define HNS3VF_RESET_WAIT_CNT 2000
45 /* Reset related Registers */
46 #define HNS3_GLOBAL_RESET_BIT 0
47 #define HNS3_CORE_RESET_BIT 1
48 #define HNS3_IMP_RESET_BIT 2
49 #define HNS3_FUN_RST_ING_B 0
51 enum hns3vf_evt_cause {
52 HNS3VF_VECTOR0_EVENT_RST,
53 HNS3VF_VECTOR0_EVENT_MBX,
54 HNS3VF_VECTOR0_EVENT_OTHER,
57 static enum hns3_reset_level hns3vf_get_reset_level(struct hns3_hw *hw,
59 static int hns3vf_dev_mtu_set(struct rte_eth_dev *dev, uint16_t mtu);
60 static int hns3vf_dev_configure_vlan(struct rte_eth_dev *dev);
62 /* set PCI bus mastering */
64 hns3vf_set_bus_master(const struct rte_pci_device *device, bool op)
68 rte_pci_read_config(device, ®, sizeof(reg), PCI_COMMAND);
71 /* set the master bit */
72 reg |= PCI_COMMAND_MASTER;
74 reg &= ~(PCI_COMMAND_MASTER);
76 rte_pci_write_config(device, ®, sizeof(reg), PCI_COMMAND);
80 * hns3vf_find_pci_capability - lookup a capability in the PCI capability list
81 * @cap: the capability
83 * Return the address of the given capability within the PCI capability list.
86 hns3vf_find_pci_capability(const struct rte_pci_device *device, int cap)
88 #define MAX_PCIE_CAPABILITY 48
94 rte_pci_read_config(device, &status, sizeof(status), PCI_STATUS);
95 if (!(status & PCI_STATUS_CAP_LIST))
98 ttl = MAX_PCIE_CAPABILITY;
99 rte_pci_read_config(device, &pos, sizeof(pos), PCI_CAPABILITY_LIST);
100 while (ttl-- && pos >= PCI_STD_HEADER_SIZEOF) {
101 rte_pci_read_config(device, &id, sizeof(id),
102 (pos + PCI_CAP_LIST_ID));
110 rte_pci_read_config(device, &pos, sizeof(pos),
111 (pos + PCI_CAP_LIST_NEXT));
117 hns3vf_enable_msix(const struct rte_pci_device *device, bool op)
122 pos = hns3vf_find_pci_capability(device, PCI_CAP_ID_MSIX);
124 rte_pci_read_config(device, &control, sizeof(control),
125 (pos + PCI_MSIX_FLAGS));
127 control |= PCI_MSIX_FLAGS_ENABLE;
129 control &= ~PCI_MSIX_FLAGS_ENABLE;
130 rte_pci_write_config(device, &control, sizeof(control),
131 (pos + PCI_MSIX_FLAGS));
138 hns3vf_add_mac_addr(struct rte_eth_dev *dev, struct rte_ether_addr *mac_addr,
139 __rte_unused uint32_t idx,
140 __rte_unused uint32_t pool)
142 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
143 char mac_str[RTE_ETHER_ADDR_FMT_SIZE];
146 rte_spinlock_lock(&hw->lock);
147 ret = hns3_send_mbx_msg(hw, HNS3_MBX_SET_UNICAST,
148 HNS3_MBX_MAC_VLAN_UC_ADD, mac_addr->addr_bytes,
149 RTE_ETHER_ADDR_LEN, false, NULL, 0);
150 rte_spinlock_unlock(&hw->lock);
152 rte_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
154 hns3_err(hw, "Failed to add mac addr(%s) for vf: %d", mac_str,
162 hns3vf_remove_mac_addr(struct rte_eth_dev *dev, uint32_t idx)
164 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
165 /* index will be checked by upper level rte interface */
166 struct rte_ether_addr *mac_addr = &dev->data->mac_addrs[idx];
167 char mac_str[RTE_ETHER_ADDR_FMT_SIZE];
170 rte_spinlock_lock(&hw->lock);
171 ret = hns3_send_mbx_msg(hw, HNS3_MBX_SET_UNICAST,
172 HNS3_MBX_MAC_VLAN_UC_REMOVE,
173 mac_addr->addr_bytes, RTE_ETHER_ADDR_LEN, false,
175 rte_spinlock_unlock(&hw->lock);
177 rte_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
179 hns3_err(hw, "Failed to remove mac addr(%s) for vf: %d",
185 hns3vf_set_default_mac_addr(struct rte_eth_dev *dev,
186 struct rte_ether_addr *mac_addr)
188 #define HNS3_TWO_ETHER_ADDR_LEN (RTE_ETHER_ADDR_LEN * 2)
189 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
190 struct rte_ether_addr *old_addr;
191 uint8_t addr_bytes[HNS3_TWO_ETHER_ADDR_LEN]; /* for 2 MAC addresses */
192 char mac_str[RTE_ETHER_ADDR_FMT_SIZE];
196 * It has been guaranteed that input parameter named mac_addr is valid
197 * address in the rte layer of DPDK framework.
199 old_addr = (struct rte_ether_addr *)hw->mac.mac_addr;
200 rte_spinlock_lock(&hw->lock);
201 memcpy(addr_bytes, mac_addr->addr_bytes, RTE_ETHER_ADDR_LEN);
202 memcpy(&addr_bytes[RTE_ETHER_ADDR_LEN], old_addr->addr_bytes,
205 ret = hns3_send_mbx_msg(hw, HNS3_MBX_SET_UNICAST,
206 HNS3_MBX_MAC_VLAN_UC_MODIFY, addr_bytes,
207 HNS3_TWO_ETHER_ADDR_LEN, true, NULL, 0);
210 * The hns3 VF PMD driver depends on the hns3 PF kernel ethdev
211 * driver. When user has configured a MAC address for VF device
212 * by "ip link set ..." command based on the PF device, the hns3
213 * PF kernel ethdev driver does not allow VF driver to request
214 * reconfiguring a different default MAC address, and return
215 * -EPREM to VF driver through mailbox.
218 rte_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
220 hns3_warn(hw, "Has permanet mac addr(%s) for vf",
223 rte_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
225 hns3_err(hw, "Failed to set mac addr(%s) for vf: %d",
230 rte_ether_addr_copy(mac_addr,
231 (struct rte_ether_addr *)hw->mac.mac_addr);
232 rte_spinlock_unlock(&hw->lock);
238 hns3vf_configure_mac_addr(struct hns3_adapter *hns, bool del)
240 struct hns3_hw *hw = &hns->hw;
241 struct rte_ether_addr *addr;
242 enum hns3_mbx_mac_vlan_subcode opcode;
243 char mac_str[RTE_ETHER_ADDR_FMT_SIZE];
248 opcode = HNS3_MBX_MAC_VLAN_UC_REMOVE;
250 opcode = HNS3_MBX_MAC_VLAN_UC_ADD;
251 for (i = 0; i < HNS3_VF_UC_MACADDR_NUM; i++) {
252 addr = &hw->data->mac_addrs[i];
253 if (!rte_is_valid_assigned_ether_addr(addr))
255 rte_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE, addr);
256 hns3_dbg(hw, "rm mac addr: %s", mac_str);
257 ret = hns3_send_mbx_msg(hw, HNS3_MBX_SET_UNICAST, opcode,
258 addr->addr_bytes, RTE_ETHER_ADDR_LEN,
261 hns3_err(hw, "Failed to remove mac addr for vf: %d",
270 hns3vf_add_mc_mac_addr(struct hns3_adapter *hns,
271 struct rte_ether_addr *mac_addr)
273 char mac_str[RTE_ETHER_ADDR_FMT_SIZE];
274 struct hns3_hw *hw = &hns->hw;
277 ret = hns3_send_mbx_msg(hw, HNS3_MBX_SET_MULTICAST,
278 HNS3_MBX_MAC_VLAN_MC_ADD,
279 mac_addr->addr_bytes, RTE_ETHER_ADDR_LEN, false,
282 rte_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
284 hns3_err(hw, "Failed to add mc mac addr(%s) for vf: %d",
293 hns3vf_remove_mc_mac_addr(struct hns3_adapter *hns,
294 struct rte_ether_addr *mac_addr)
296 char mac_str[RTE_ETHER_ADDR_FMT_SIZE];
297 struct hns3_hw *hw = &hns->hw;
300 ret = hns3_send_mbx_msg(hw, HNS3_MBX_SET_MULTICAST,
301 HNS3_MBX_MAC_VLAN_MC_REMOVE,
302 mac_addr->addr_bytes, RTE_ETHER_ADDR_LEN, false,
305 rte_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
307 hns3_err(hw, "Failed to remove mc mac addr(%s) for vf: %d",
316 hns3vf_set_mc_mac_addr_list(struct rte_eth_dev *dev,
317 struct rte_ether_addr *mc_addr_set,
320 struct hns3_adapter *hns = dev->data->dev_private;
321 struct hns3_hw *hw = &hns->hw;
322 struct rte_ether_addr *addr;
323 char mac_str[RTE_ETHER_ADDR_FMT_SIZE];
330 if (nb_mc_addr > HNS3_MC_MACADDR_NUM) {
331 hns3_err(hw, "Failed to set mc mac addr, nb_mc_addr(%d) "
332 "invalid. valid range: 0~%d",
333 nb_mc_addr, HNS3_MC_MACADDR_NUM);
337 set_addr_num = (int)nb_mc_addr;
338 for (i = 0; i < set_addr_num; i++) {
339 addr = &mc_addr_set[i];
340 if (!rte_is_multicast_ether_addr(addr)) {
341 rte_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
344 "Failed to set mc mac addr, addr(%s) invalid.",
349 rte_spinlock_lock(&hw->lock);
350 cur_addr_num = hw->mc_addrs_num;
351 for (i = 0; i < cur_addr_num; i++) {
352 num = cur_addr_num - i - 1;
353 addr = &hw->mc_addrs[num];
354 ret = hns3vf_remove_mc_mac_addr(hns, addr);
356 rte_spinlock_unlock(&hw->lock);
363 for (i = 0; i < set_addr_num; i++) {
364 addr = &mc_addr_set[i];
365 ret = hns3vf_add_mc_mac_addr(hns, addr);
367 rte_spinlock_unlock(&hw->lock);
371 rte_ether_addr_copy(addr, &hw->mc_addrs[hw->mc_addrs_num]);
374 rte_spinlock_unlock(&hw->lock);
380 hns3vf_configure_all_mc_mac_addr(struct hns3_adapter *hns, bool del)
382 char mac_str[RTE_ETHER_ADDR_FMT_SIZE];
383 struct hns3_hw *hw = &hns->hw;
384 struct rte_ether_addr *addr;
389 for (i = 0; i < hw->mc_addrs_num; i++) {
390 addr = &hw->mc_addrs[i];
391 if (!rte_is_multicast_ether_addr(addr))
394 ret = hns3vf_remove_mc_mac_addr(hns, addr);
396 ret = hns3vf_add_mc_mac_addr(hns, addr);
399 rte_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
401 hns3_err(hw, "Failed to %s mc mac addr: %s for vf: %d",
402 del ? "Remove" : "Restore", mac_str, ret);
409 hns3vf_set_promisc_mode(struct hns3_hw *hw, bool en_bc_pmc,
410 bool en_uc_pmc, bool en_mc_pmc)
412 struct hns3_mbx_vf_to_pf_cmd *req;
413 struct hns3_cmd_desc desc;
416 req = (struct hns3_mbx_vf_to_pf_cmd *)desc.data;
419 * The hns3 VF PMD driver depends on the hns3 PF kernel ethdev driver,
420 * so there are some features for promiscuous/allmulticast mode in hns3
421 * VF PMD driver as below:
422 * 1. The promiscuous/allmulticast mode can be configured successfully
423 * only based on the trusted VF device. If based on the non trusted
424 * VF device, configuring promiscuous/allmulticast mode will fail.
425 * The hns3 VF device can be confiruged as trusted device by hns3 PF
426 * kernel ethdev driver on the host by the following command:
427 * "ip link set <eth num> vf <vf id> turst on"
428 * 2. After the promiscuous mode is configured successfully, hns3 VF PMD
429 * driver can receive the ingress and outgoing traffic. In the words,
430 * all the ingress packets, all the packets sent from the PF and
431 * other VFs on the same physical port.
432 * 3. Note: Because of the hardware constraints, By default vlan filter
433 * is enabled and couldn't be turned off based on VF device, so vlan
434 * filter is still effective even in promiscuous mode. If upper
435 * applications don't call rte_eth_dev_vlan_filter API function to
436 * set vlan based on VF device, hns3 VF PMD driver will can't receive
437 * the packets with vlan tag in promiscuoue mode.
439 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_MBX_VF_TO_PF, false);
440 req->msg[0] = HNS3_MBX_SET_PROMISC_MODE;
441 req->msg[1] = en_bc_pmc ? 1 : 0;
442 req->msg[2] = en_uc_pmc ? 1 : 0;
443 req->msg[3] = en_mc_pmc ? 1 : 0;
445 ret = hns3_cmd_send(hw, &desc, 1);
447 hns3_err(hw, "Set promisc mode fail, ret = %d", ret);
453 hns3vf_dev_promiscuous_enable(struct rte_eth_dev *dev)
455 struct hns3_adapter *hns = dev->data->dev_private;
456 struct hns3_hw *hw = &hns->hw;
459 ret = hns3vf_set_promisc_mode(hw, true, true, true);
461 hns3_err(hw, "Failed to enable promiscuous mode, ret = %d",
467 hns3vf_dev_promiscuous_disable(struct rte_eth_dev *dev)
469 bool allmulti = dev->data->all_multicast ? true : false;
470 struct hns3_adapter *hns = dev->data->dev_private;
471 struct hns3_hw *hw = &hns->hw;
474 ret = hns3vf_set_promisc_mode(hw, true, false, allmulti);
476 hns3_err(hw, "Failed to disable promiscuous mode, ret = %d",
482 hns3vf_dev_allmulticast_enable(struct rte_eth_dev *dev)
484 struct hns3_adapter *hns = dev->data->dev_private;
485 struct hns3_hw *hw = &hns->hw;
488 if (dev->data->promiscuous)
491 ret = hns3vf_set_promisc_mode(hw, true, false, true);
493 hns3_err(hw, "Failed to enable allmulticast mode, ret = %d",
499 hns3vf_dev_allmulticast_disable(struct rte_eth_dev *dev)
501 struct hns3_adapter *hns = dev->data->dev_private;
502 struct hns3_hw *hw = &hns->hw;
505 if (dev->data->promiscuous)
508 ret = hns3vf_set_promisc_mode(hw, true, false, false);
510 hns3_err(hw, "Failed to disable allmulticast mode, ret = %d",
516 hns3vf_restore_promisc(struct hns3_adapter *hns)
518 struct hns3_hw *hw = &hns->hw;
519 bool allmulti = hw->data->all_multicast ? true : false;
521 if (hw->data->promiscuous)
522 return hns3vf_set_promisc_mode(hw, true, true, true);
524 return hns3vf_set_promisc_mode(hw, true, false, allmulti);
528 hns3vf_bind_ring_with_vector(struct hns3_hw *hw, uint8_t vector_id,
529 bool mmap, enum hns3_ring_type queue_type,
532 struct hns3_vf_bind_vector_msg bind_msg;
537 memset(&bind_msg, 0, sizeof(bind_msg));
538 code = mmap ? HNS3_MBX_MAP_RING_TO_VECTOR :
539 HNS3_MBX_UNMAP_RING_TO_VECTOR;
540 bind_msg.vector_id = vector_id;
542 if (queue_type == HNS3_RING_TYPE_RX)
543 bind_msg.param[0].int_gl_index = HNS3_RING_GL_RX;
545 bind_msg.param[0].int_gl_index = HNS3_RING_GL_TX;
547 bind_msg.param[0].ring_type = queue_type;
548 bind_msg.ring_num = 1;
549 bind_msg.param[0].tqp_index = queue_id;
550 op_str = mmap ? "Map" : "Unmap";
551 ret = hns3_send_mbx_msg(hw, code, 0, (uint8_t *)&bind_msg,
552 sizeof(bind_msg), false, NULL, 0);
554 hns3_err(hw, "%s TQP %d fail, vector_id is %d, ret is %d.",
555 op_str, queue_id, bind_msg.vector_id, ret);
563 hns3vf_init_ring_with_vector(struct hns3_hw *hw)
570 * In hns3 network engine, vector 0 is always the misc interrupt of this
571 * function, vector 1~N can be used respectively for the queues of the
572 * function. Tx and Rx queues with the same number share the interrupt
573 * vector. In the initialization clearing the all hardware mapping
574 * relationship configurations between queues and interrupt vectors is
575 * needed, so some error caused by the residual configurations, such as
576 * the unexpected Tx interrupt, can be avoid. Because of the hardware
577 * constraints in hns3 hardware engine, we have to implement clearing
578 * the mapping relationship configurations by binding all queues to the
579 * last interrupt vector and reserving the last interrupt vector. This
580 * method results in a decrease of the maximum queues when upper
581 * applications call the rte_eth_dev_configure API function to enable
584 vec = hw->num_msi - 1; /* vector 0 for misc interrupt, not for queue */
585 hw->intr_tqps_num = vec - 1; /* the last interrupt is reserved */
586 for (i = 0; i < hw->intr_tqps_num; i++) {
588 * Set gap limiter and rate limiter configuration of queue's
591 hns3_set_queue_intr_gl(hw, i, HNS3_RING_GL_RX,
592 HNS3_TQP_INTR_GL_DEFAULT);
593 hns3_set_queue_intr_gl(hw, i, HNS3_RING_GL_TX,
594 HNS3_TQP_INTR_GL_DEFAULT);
595 hns3_set_queue_intr_rl(hw, i, HNS3_TQP_INTR_RL_DEFAULT);
597 ret = hns3vf_bind_ring_with_vector(hw, vec, false,
598 HNS3_RING_TYPE_TX, i);
600 PMD_INIT_LOG(ERR, "VF fail to unbind TX ring(%d) with "
601 "vector: %d, ret=%d", i, vec, ret);
605 ret = hns3vf_bind_ring_with_vector(hw, vec, false,
606 HNS3_RING_TYPE_RX, i);
608 PMD_INIT_LOG(ERR, "VF fail to unbind RX ring(%d) with "
609 "vector: %d, ret=%d", i, vec, ret);
618 hns3vf_dev_configure(struct rte_eth_dev *dev)
620 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
621 struct hns3_rss_conf *rss_cfg = &hw->rss_info;
622 struct rte_eth_conf *conf = &dev->data->dev_conf;
623 enum rte_eth_rx_mq_mode mq_mode = conf->rxmode.mq_mode;
624 uint16_t nb_rx_q = dev->data->nb_rx_queues;
625 uint16_t nb_tx_q = dev->data->nb_tx_queues;
626 struct rte_eth_rss_conf rss_conf;
631 * Hardware does not support individually enable/disable/reset the Tx or
632 * Rx queue in hns3 network engine. Driver must enable/disable/reset Tx
633 * and Rx queues at the same time. When the numbers of Tx queues
634 * allocated by upper applications are not equal to the numbers of Rx
635 * queues, driver needs to setup fake Tx or Rx queues to adjust numbers
636 * of Tx/Rx queues. otherwise, network engine can not work as usual. But
637 * these fake queues are imperceptible, and can not be used by upper
640 ret = hns3_set_fake_rx_or_tx_queues(dev, nb_rx_q, nb_tx_q);
642 hns3_err(hw, "Failed to set rx/tx fake queues: %d", ret);
646 hw->adapter_state = HNS3_NIC_CONFIGURING;
647 if (conf->link_speeds & ETH_LINK_SPEED_FIXED) {
648 hns3_err(hw, "setting link speed/duplex not supported");
653 /* When RSS is not configured, redirect the packet queue 0 */
654 if ((uint32_t)mq_mode & ETH_MQ_RX_RSS_FLAG) {
655 rss_conf = conf->rx_adv_conf.rss_conf;
656 if (rss_conf.rss_key == NULL) {
657 rss_conf.rss_key = rss_cfg->key;
658 rss_conf.rss_key_len = HNS3_RSS_KEY_SIZE;
661 ret = hns3_dev_rss_hash_update(dev, &rss_conf);
667 * If jumbo frames are enabled, MTU needs to be refreshed
668 * according to the maximum RX packet length.
670 if (conf->rxmode.offloads & DEV_RX_OFFLOAD_JUMBO_FRAME) {
672 * Security of max_rx_pkt_len is guaranteed in dpdk frame.
673 * Maximum value of max_rx_pkt_len is HNS3_MAX_FRAME_LEN, so it
674 * can safely assign to "uint16_t" type variable.
676 mtu = (uint16_t)HNS3_PKTLEN_TO_MTU(conf->rxmode.max_rx_pkt_len);
677 ret = hns3vf_dev_mtu_set(dev, mtu);
680 dev->data->mtu = mtu;
683 ret = hns3vf_dev_configure_vlan(dev);
687 hw->adapter_state = HNS3_NIC_CONFIGURED;
691 (void)hns3_set_fake_rx_or_tx_queues(dev, 0, 0);
692 hw->adapter_state = HNS3_NIC_INITIALIZED;
698 hns3vf_config_mtu(struct hns3_hw *hw, uint16_t mtu)
702 ret = hns3_send_mbx_msg(hw, HNS3_MBX_SET_MTU, 0, (const uint8_t *)&mtu,
703 sizeof(mtu), true, NULL, 0);
705 hns3_err(hw, "Failed to set mtu (%u) for vf: %d", mtu, ret);
711 hns3vf_dev_mtu_set(struct rte_eth_dev *dev, uint16_t mtu)
713 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
714 uint32_t frame_size = mtu + HNS3_ETH_OVERHEAD;
718 * The hns3 PF/VF devices on the same port share the hardware MTU
719 * configuration. Currently, we send mailbox to inform hns3 PF kernel
720 * ethdev driver to finish hardware MTU configuration in hns3 VF PMD
721 * driver, there is no need to stop the port for hns3 VF device, and the
722 * MTU value issued by hns3 VF PMD driver must be less than or equal to
725 if (rte_atomic16_read(&hw->reset.resetting)) {
726 hns3_err(hw, "Failed to set mtu during resetting");
730 rte_spinlock_lock(&hw->lock);
731 ret = hns3vf_config_mtu(hw, mtu);
733 rte_spinlock_unlock(&hw->lock);
736 if (frame_size > RTE_ETHER_MAX_LEN)
737 dev->data->dev_conf.rxmode.offloads |=
738 DEV_RX_OFFLOAD_JUMBO_FRAME;
740 dev->data->dev_conf.rxmode.offloads &=
741 ~DEV_RX_OFFLOAD_JUMBO_FRAME;
742 dev->data->dev_conf.rxmode.max_rx_pkt_len = frame_size;
743 rte_spinlock_unlock(&hw->lock);
749 hns3vf_dev_infos_get(struct rte_eth_dev *eth_dev, struct rte_eth_dev_info *info)
751 struct hns3_adapter *hns = eth_dev->data->dev_private;
752 struct hns3_hw *hw = &hns->hw;
753 uint16_t q_num = hw->tqps_num;
756 * In interrupt mode, 'max_rx_queues' is set based on the number of
757 * MSI-X interrupt resources of the hardware.
759 if (hw->data->dev_conf.intr_conf.rxq == 1)
760 q_num = hw->intr_tqps_num;
762 info->max_rx_queues = q_num;
763 info->max_tx_queues = hw->tqps_num;
764 info->max_rx_pktlen = HNS3_MAX_FRAME_LEN; /* CRC included */
765 info->min_rx_bufsize = hw->rx_buf_len;
766 info->max_mac_addrs = HNS3_VF_UC_MACADDR_NUM;
767 info->max_mtu = info->max_rx_pktlen - HNS3_ETH_OVERHEAD;
769 info->rx_offload_capa = (DEV_RX_OFFLOAD_IPV4_CKSUM |
770 DEV_RX_OFFLOAD_UDP_CKSUM |
771 DEV_RX_OFFLOAD_TCP_CKSUM |
772 DEV_RX_OFFLOAD_SCTP_CKSUM |
773 DEV_RX_OFFLOAD_OUTER_IPV4_CKSUM |
774 DEV_RX_OFFLOAD_OUTER_UDP_CKSUM |
775 DEV_RX_OFFLOAD_KEEP_CRC |
776 DEV_RX_OFFLOAD_SCATTER |
777 DEV_RX_OFFLOAD_VLAN_STRIP |
778 DEV_RX_OFFLOAD_QINQ_STRIP |
779 DEV_RX_OFFLOAD_VLAN_FILTER |
780 DEV_RX_OFFLOAD_JUMBO_FRAME);
781 info->tx_queue_offload_capa = DEV_TX_OFFLOAD_MBUF_FAST_FREE;
782 info->tx_offload_capa = (DEV_TX_OFFLOAD_OUTER_IPV4_CKSUM |
783 DEV_TX_OFFLOAD_IPV4_CKSUM |
784 DEV_TX_OFFLOAD_TCP_CKSUM |
785 DEV_TX_OFFLOAD_UDP_CKSUM |
786 DEV_TX_OFFLOAD_SCTP_CKSUM |
787 DEV_TX_OFFLOAD_VLAN_INSERT |
788 DEV_TX_OFFLOAD_QINQ_INSERT |
789 DEV_TX_OFFLOAD_MULTI_SEGS |
790 DEV_TX_OFFLOAD_TCP_TSO |
791 DEV_TX_OFFLOAD_VXLAN_TNL_TSO |
792 DEV_TX_OFFLOAD_GRE_TNL_TSO |
793 DEV_TX_OFFLOAD_GENEVE_TNL_TSO |
794 info->tx_queue_offload_capa);
796 info->rx_desc_lim = (struct rte_eth_desc_lim) {
797 .nb_max = HNS3_MAX_RING_DESC,
798 .nb_min = HNS3_MIN_RING_DESC,
799 .nb_align = HNS3_ALIGN_RING_DESC,
802 info->tx_desc_lim = (struct rte_eth_desc_lim) {
803 .nb_max = HNS3_MAX_RING_DESC,
804 .nb_min = HNS3_MIN_RING_DESC,
805 .nb_align = HNS3_ALIGN_RING_DESC,
808 info->vmdq_queue_num = 0;
810 info->reta_size = HNS3_RSS_IND_TBL_SIZE;
811 info->hash_key_size = HNS3_RSS_KEY_SIZE;
812 info->flow_type_rss_offloads = HNS3_ETH_RSS_SUPPORT;
813 info->default_rxportconf.ring_size = HNS3_DEFAULT_RING_DESC;
814 info->default_txportconf.ring_size = HNS3_DEFAULT_RING_DESC;
820 hns3vf_clear_event_cause(struct hns3_hw *hw, uint32_t regclr)
822 hns3_write_dev(hw, HNS3_VECTOR0_CMDQ_SRC_REG, regclr);
826 hns3vf_disable_irq0(struct hns3_hw *hw)
828 hns3_write_dev(hw, HNS3_MISC_VECTOR_REG_BASE, 0);
832 hns3vf_enable_irq0(struct hns3_hw *hw)
834 hns3_write_dev(hw, HNS3_MISC_VECTOR_REG_BASE, 1);
837 static enum hns3vf_evt_cause
838 hns3vf_check_event_cause(struct hns3_adapter *hns, uint32_t *clearval)
840 struct hns3_hw *hw = &hns->hw;
841 enum hns3vf_evt_cause ret;
842 uint32_t cmdq_stat_reg;
843 uint32_t rst_ing_reg;
846 /* Fetch the events from their corresponding regs */
847 cmdq_stat_reg = hns3_read_dev(hw, HNS3_VECTOR0_CMDQ_STAT_REG);
849 if (BIT(HNS3_VECTOR0_RST_INT_B) & cmdq_stat_reg) {
850 rst_ing_reg = hns3_read_dev(hw, HNS3_FUN_RST_ING);
851 hns3_warn(hw, "resetting reg: 0x%x", rst_ing_reg);
852 hns3_atomic_set_bit(HNS3_VF_RESET, &hw->reset.pending);
853 rte_atomic16_set(&hw->reset.disable_cmd, 1);
854 val = hns3_read_dev(hw, HNS3_VF_RST_ING);
855 hns3_write_dev(hw, HNS3_VF_RST_ING, val | HNS3_VF_RST_ING_BIT);
856 val = cmdq_stat_reg & ~BIT(HNS3_VECTOR0_RST_INT_B);
858 hw->reset.stats.global_cnt++;
859 hns3_warn(hw, "Global reset detected, clear reset status");
861 hns3_schedule_delayed_reset(hns);
862 hns3_warn(hw, "Global reset detected, don't clear reset status");
865 ret = HNS3VF_VECTOR0_EVENT_RST;
869 /* Check for vector0 mailbox(=CMDQ RX) event source */
870 if (BIT(HNS3_VECTOR0_RX_CMDQ_INT_B) & cmdq_stat_reg) {
871 val = cmdq_stat_reg & ~BIT(HNS3_VECTOR0_RX_CMDQ_INT_B);
872 ret = HNS3VF_VECTOR0_EVENT_MBX;
877 ret = HNS3VF_VECTOR0_EVENT_OTHER;
885 hns3vf_interrupt_handler(void *param)
887 struct rte_eth_dev *dev = (struct rte_eth_dev *)param;
888 struct hns3_adapter *hns = dev->data->dev_private;
889 struct hns3_hw *hw = &hns->hw;
890 enum hns3vf_evt_cause event_cause;
893 if (hw->irq_thread_id == 0)
894 hw->irq_thread_id = pthread_self();
896 /* Disable interrupt */
897 hns3vf_disable_irq0(hw);
899 /* Read out interrupt causes */
900 event_cause = hns3vf_check_event_cause(hns, &clearval);
902 switch (event_cause) {
903 case HNS3VF_VECTOR0_EVENT_RST:
904 hns3_schedule_reset(hns);
906 case HNS3VF_VECTOR0_EVENT_MBX:
907 hns3_dev_handle_mbx_msg(hw);
913 /* Clear interrupt causes */
914 hns3vf_clear_event_cause(hw, clearval);
916 /* Enable interrupt */
917 hns3vf_enable_irq0(hw);
921 hns3vf_check_tqp_info(struct hns3_hw *hw)
925 tqps_num = hw->tqps_num;
926 if (tqps_num > HNS3_MAX_TQP_NUM_PER_FUNC || tqps_num == 0) {
927 PMD_INIT_LOG(ERR, "Get invalid tqps_num(%u) from PF. valid "
929 tqps_num, HNS3_MAX_TQP_NUM_PER_FUNC);
933 if (hw->rx_buf_len == 0)
934 hw->rx_buf_len = HNS3_DEFAULT_RX_BUF_LEN;
935 hw->alloc_rss_size = RTE_MIN(hw->rss_size_max, hw->tqps_num);
941 hns3vf_get_queue_info(struct hns3_hw *hw)
943 #define HNS3VF_TQPS_RSS_INFO_LEN 6
944 uint8_t resp_msg[HNS3VF_TQPS_RSS_INFO_LEN];
947 ret = hns3_send_mbx_msg(hw, HNS3_MBX_GET_QINFO, 0, NULL, 0, true,
948 resp_msg, HNS3VF_TQPS_RSS_INFO_LEN);
950 PMD_INIT_LOG(ERR, "Failed to get tqp info from PF: %d", ret);
954 memcpy(&hw->tqps_num, &resp_msg[0], sizeof(uint16_t));
955 memcpy(&hw->rss_size_max, &resp_msg[2], sizeof(uint16_t));
956 memcpy(&hw->rx_buf_len, &resp_msg[4], sizeof(uint16_t));
958 return hns3vf_check_tqp_info(hw);
962 hns3vf_get_queue_depth(struct hns3_hw *hw)
964 #define HNS3VF_TQPS_DEPTH_INFO_LEN 4
965 uint8_t resp_msg[HNS3VF_TQPS_DEPTH_INFO_LEN];
968 ret = hns3_send_mbx_msg(hw, HNS3_MBX_GET_QDEPTH, 0, NULL, 0, true,
969 resp_msg, HNS3VF_TQPS_DEPTH_INFO_LEN);
971 PMD_INIT_LOG(ERR, "Failed to get tqp depth info from PF: %d",
976 memcpy(&hw->num_tx_desc, &resp_msg[0], sizeof(uint16_t));
977 memcpy(&hw->num_rx_desc, &resp_msg[2], sizeof(uint16_t));
983 hns3vf_get_tc_info(struct hns3_hw *hw)
988 ret = hns3_send_mbx_msg(hw, HNS3_MBX_GET_TCINFO, 0, NULL, 0,
989 true, &resp_msg, sizeof(resp_msg));
991 hns3_err(hw, "VF request to get TC info from PF failed %d",
996 hw->hw_tc_map = resp_msg;
1002 hns3vf_get_host_mac_addr(struct hns3_hw *hw)
1004 uint8_t host_mac[RTE_ETHER_ADDR_LEN];
1007 ret = hns3_send_mbx_msg(hw, HNS3_MBX_GET_MAC_ADDR, 0, NULL, 0,
1008 true, host_mac, RTE_ETHER_ADDR_LEN);
1010 hns3_err(hw, "Failed to get mac addr from PF: %d", ret);
1014 memcpy(hw->mac.mac_addr, host_mac, RTE_ETHER_ADDR_LEN);
1020 hns3vf_get_configuration(struct hns3_hw *hw)
1024 hw->mac.media_type = HNS3_MEDIA_TYPE_NONE;
1025 hw->rss_dis_flag = false;
1027 /* Get queue configuration from PF */
1028 ret = hns3vf_get_queue_info(hw);
1032 /* Get queue depth info from PF */
1033 ret = hns3vf_get_queue_depth(hw);
1037 /* Get user defined VF MAC addr from PF */
1038 ret = hns3vf_get_host_mac_addr(hw);
1042 /* Get tc configuration from PF */
1043 return hns3vf_get_tc_info(hw);
1047 hns3vf_set_tc_info(struct hns3_adapter *hns)
1049 struct hns3_hw *hw = &hns->hw;
1050 uint16_t nb_rx_q = hw->data->nb_rx_queues;
1051 uint16_t nb_tx_q = hw->data->nb_tx_queues;
1055 for (i = 0; i < HNS3_MAX_TC_NUM; i++)
1056 if (hw->hw_tc_map & BIT(i))
1059 if (nb_rx_q < hw->num_tc) {
1060 hns3_err(hw, "number of Rx queues(%d) is less than tcs(%d).",
1061 nb_rx_q, hw->num_tc);
1065 if (nb_tx_q < hw->num_tc) {
1066 hns3_err(hw, "number of Tx queues(%d) is less than tcs(%d).",
1067 nb_tx_q, hw->num_tc);
1071 hns3_set_rss_size(hw, nb_rx_q);
1072 hns3_tc_queue_mapping_cfg(hw, nb_tx_q);
1078 hns3vf_request_link_info(struct hns3_hw *hw)
1083 if (rte_atomic16_read(&hw->reset.resetting))
1085 ret = hns3_send_mbx_msg(hw, HNS3_MBX_GET_LINK_STATUS, 0, NULL, 0, false,
1086 &resp_msg, sizeof(resp_msg));
1088 hns3_err(hw, "Failed to fetch link status from PF: %d", ret);
1092 hns3vf_vlan_filter_configure(struct hns3_adapter *hns, uint16_t vlan_id, int on)
1094 #define HNS3VF_VLAN_MBX_MSG_LEN 5
1095 struct hns3_hw *hw = &hns->hw;
1096 uint8_t msg_data[HNS3VF_VLAN_MBX_MSG_LEN];
1097 uint16_t proto = htons(RTE_ETHER_TYPE_VLAN);
1098 uint8_t is_kill = on ? 0 : 1;
1100 msg_data[0] = is_kill;
1101 memcpy(&msg_data[1], &vlan_id, sizeof(vlan_id));
1102 memcpy(&msg_data[3], &proto, sizeof(proto));
1104 return hns3_send_mbx_msg(hw, HNS3_MBX_SET_VLAN, HNS3_MBX_VLAN_FILTER,
1105 msg_data, HNS3VF_VLAN_MBX_MSG_LEN, true, NULL,
1110 hns3vf_vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on)
1112 struct hns3_adapter *hns = dev->data->dev_private;
1113 struct hns3_hw *hw = &hns->hw;
1116 if (rte_atomic16_read(&hw->reset.resetting)) {
1118 "vf set vlan id failed during resetting, vlan_id =%u",
1122 rte_spinlock_lock(&hw->lock);
1123 ret = hns3vf_vlan_filter_configure(hns, vlan_id, on);
1124 rte_spinlock_unlock(&hw->lock);
1126 hns3_err(hw, "vf set vlan id failed, vlan_id =%u, ret =%d",
1133 hns3vf_en_hw_strip_rxvtag(struct hns3_hw *hw, bool enable)
1138 msg_data = enable ? 1 : 0;
1139 ret = hns3_send_mbx_msg(hw, HNS3_MBX_SET_VLAN, HNS3_MBX_VLAN_RX_OFF_CFG,
1140 &msg_data, sizeof(msg_data), false, NULL, 0);
1142 hns3_err(hw, "vf enable strip failed, ret =%d", ret);
1148 hns3vf_vlan_offload_set(struct rte_eth_dev *dev, int mask)
1150 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1151 struct rte_eth_conf *dev_conf = &dev->data->dev_conf;
1152 unsigned int tmp_mask;
1154 tmp_mask = (unsigned int)mask;
1155 /* Vlan stripping setting */
1156 if (tmp_mask & ETH_VLAN_STRIP_MASK) {
1157 rte_spinlock_lock(&hw->lock);
1158 /* Enable or disable VLAN stripping */
1159 if (dev_conf->rxmode.offloads & DEV_RX_OFFLOAD_VLAN_STRIP)
1160 hns3vf_en_hw_strip_rxvtag(hw, true);
1162 hns3vf_en_hw_strip_rxvtag(hw, false);
1163 rte_spinlock_unlock(&hw->lock);
1170 hns3vf_handle_all_vlan_table(struct hns3_adapter *hns, int on)
1172 struct rte_vlan_filter_conf *vfc;
1173 struct hns3_hw *hw = &hns->hw;
1180 vfc = &hw->data->vlan_filter_conf;
1181 for (i = 0; i < RTE_DIM(vfc->ids); i++) {
1182 if (vfc->ids[i] == 0)
1187 * 64 means the num bits of ids, one bit corresponds to
1191 /* count trailing zeroes */
1192 vbit = ~ids & (ids - 1);
1193 /* clear least significant bit set */
1194 ids ^= (ids ^ (ids - 1)) ^ vbit;
1199 ret = hns3vf_vlan_filter_configure(hns, vlan_id, on);
1202 "VF handle vlan table failed, ret =%d, on = %d",
1213 hns3vf_remove_all_vlan_table(struct hns3_adapter *hns)
1215 return hns3vf_handle_all_vlan_table(hns, 0);
1219 hns3vf_restore_vlan_conf(struct hns3_adapter *hns)
1221 struct hns3_hw *hw = &hns->hw;
1222 struct rte_eth_conf *dev_conf;
1226 dev_conf = &hw->data->dev_conf;
1227 en = dev_conf->rxmode.offloads & DEV_RX_OFFLOAD_VLAN_STRIP ? true
1229 ret = hns3vf_en_hw_strip_rxvtag(hw, en);
1231 hns3_err(hw, "VF restore vlan conf fail, en =%d, ret =%d", en,
1237 hns3vf_dev_configure_vlan(struct rte_eth_dev *dev)
1239 struct hns3_adapter *hns = dev->data->dev_private;
1240 struct rte_eth_dev_data *data = dev->data;
1241 struct hns3_hw *hw = &hns->hw;
1244 if (data->dev_conf.txmode.hw_vlan_reject_tagged ||
1245 data->dev_conf.txmode.hw_vlan_reject_untagged ||
1246 data->dev_conf.txmode.hw_vlan_insert_pvid) {
1247 hns3_warn(hw, "hw_vlan_reject_tagged, hw_vlan_reject_untagged "
1248 "or hw_vlan_insert_pvid is not support!");
1251 /* Apply vlan offload setting */
1252 ret = hns3vf_vlan_offload_set(dev, ETH_VLAN_STRIP_MASK);
1254 hns3_err(hw, "dev config vlan offload failed, ret =%d", ret);
1260 hns3vf_set_alive(struct hns3_hw *hw, bool alive)
1264 msg_data = alive ? 1 : 0;
1265 return hns3_send_mbx_msg(hw, HNS3_MBX_SET_ALIVE, 0, &msg_data,
1266 sizeof(msg_data), false, NULL, 0);
1270 hns3vf_keep_alive_handler(void *param)
1272 struct rte_eth_dev *eth_dev = (struct rte_eth_dev *)param;
1273 struct hns3_adapter *hns = eth_dev->data->dev_private;
1274 struct hns3_hw *hw = &hns->hw;
1278 ret = hns3_send_mbx_msg(hw, HNS3_MBX_KEEP_ALIVE, 0, NULL, 0,
1279 false, &respmsg, sizeof(uint8_t));
1281 hns3_err(hw, "VF sends keeping alive cmd failed(=%d)",
1284 rte_eal_alarm_set(HNS3VF_KEEP_ALIVE_INTERVAL, hns3vf_keep_alive_handler,
1289 hns3vf_service_handler(void *param)
1291 struct rte_eth_dev *eth_dev = (struct rte_eth_dev *)param;
1292 struct hns3_adapter *hns = eth_dev->data->dev_private;
1293 struct hns3_hw *hw = &hns->hw;
1296 * The query link status and reset processing are executed in the
1297 * interrupt thread.When the IMP reset occurs, IMP will not respond,
1298 * and the query operation will time out after 30ms. In the case of
1299 * multiple PF/VFs, each query failure timeout causes the IMP reset
1300 * interrupt to fail to respond within 100ms.
1301 * Before querying the link status, check whether there is a reset
1302 * pending, and if so, abandon the query.
1304 if (!hns3vf_is_reset_pending(hns))
1305 hns3vf_request_link_info(hw);
1307 hns3_warn(hw, "Cancel the query when reset is pending");
1309 rte_eal_alarm_set(HNS3VF_SERVICE_INTERVAL, hns3vf_service_handler,
1314 hns3_query_vf_resource(struct hns3_hw *hw)
1316 struct hns3_vf_res_cmd *req;
1317 struct hns3_cmd_desc desc;
1321 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_QUERY_VF_RSRC, true);
1322 ret = hns3_cmd_send(hw, &desc, 1);
1324 hns3_err(hw, "query vf resource failed, ret = %d", ret);
1328 req = (struct hns3_vf_res_cmd *)desc.data;
1329 num_msi = hns3_get_field(rte_le_to_cpu_16(req->vf_intr_vector_number),
1330 HNS3_VEC_NUM_M, HNS3_VEC_NUM_S);
1331 if (num_msi < HNS3_MIN_VECTOR_NUM) {
1332 hns3_err(hw, "Just %u msi resources, not enough for vf(min:%d)",
1333 num_msi, HNS3_MIN_VECTOR_NUM);
1337 hw->num_msi = (num_msi > hw->tqps_num + 1) ? hw->tqps_num + 1 : num_msi;
1343 hns3vf_init_hardware(struct hns3_adapter *hns)
1345 struct hns3_hw *hw = &hns->hw;
1346 uint16_t mtu = hw->data->mtu;
1349 ret = hns3vf_set_promisc_mode(hw, true, false, false);
1353 ret = hns3vf_config_mtu(hw, mtu);
1355 goto err_init_hardware;
1357 ret = hns3vf_vlan_filter_configure(hns, 0, 1);
1359 PMD_INIT_LOG(ERR, "Failed to initialize VLAN config: %d", ret);
1360 goto err_init_hardware;
1363 ret = hns3_config_gro(hw, false);
1365 PMD_INIT_LOG(ERR, "Failed to config gro: %d", ret);
1366 goto err_init_hardware;
1369 ret = hns3vf_set_alive(hw, true);
1371 PMD_INIT_LOG(ERR, "Failed to VF send alive to PF: %d", ret);
1372 goto err_init_hardware;
1375 hns3vf_request_link_info(hw);
1379 (void)hns3vf_set_promisc_mode(hw, false, false, false);
1384 hns3vf_clear_vport_list(struct hns3_hw *hw)
1386 return hns3_send_mbx_msg(hw, HNS3_MBX_HANDLE_VF_TBL,
1387 HNS3_MBX_VPORT_LIST_CLEAR, NULL, 0, false,
1392 hns3vf_init_vf(struct rte_eth_dev *eth_dev)
1394 struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(eth_dev);
1395 struct hns3_adapter *hns = eth_dev->data->dev_private;
1396 struct hns3_hw *hw = &hns->hw;
1399 PMD_INIT_FUNC_TRACE();
1401 /* Get hardware io base address from pcie BAR2 IO space */
1402 hw->io_base = pci_dev->mem_resource[2].addr;
1404 /* Firmware command queue initialize */
1405 ret = hns3_cmd_init_queue(hw);
1407 PMD_INIT_LOG(ERR, "Failed to init cmd queue: %d", ret);
1408 goto err_cmd_init_queue;
1411 /* Firmware command initialize */
1412 ret = hns3_cmd_init(hw);
1414 PMD_INIT_LOG(ERR, "Failed to init cmd: %d", ret);
1418 /* Get VF resource */
1419 ret = hns3_query_vf_resource(hw);
1423 rte_spinlock_init(&hw->mbx_resp.lock);
1425 hns3vf_clear_event_cause(hw, 0);
1427 ret = rte_intr_callback_register(&pci_dev->intr_handle,
1428 hns3vf_interrupt_handler, eth_dev);
1430 PMD_INIT_LOG(ERR, "Failed to register intr: %d", ret);
1431 goto err_intr_callback_register;
1434 /* Enable interrupt */
1435 rte_intr_enable(&pci_dev->intr_handle);
1436 hns3vf_enable_irq0(hw);
1438 /* Get configuration from PF */
1439 ret = hns3vf_get_configuration(hw);
1441 PMD_INIT_LOG(ERR, "Failed to fetch configuration: %d", ret);
1442 goto err_get_config;
1446 * The hns3 PF ethdev driver in kernel support setting VF MAC address
1447 * on the host by "ip link set ..." command. To avoid some incorrect
1448 * scenes, for example, hns3 VF PMD driver fails to receive and send
1449 * packets after user configure the MAC address by using the
1450 * "ip link set ..." command, hns3 VF PMD driver keep the same MAC
1451 * address strategy as the hns3 kernel ethdev driver in the
1452 * initialization. If user configure a MAC address by the ip command
1453 * for VF device, then hns3 VF PMD driver will start with it, otherwise
1454 * start with a random MAC address in the initialization.
1456 ret = rte_is_zero_ether_addr((struct rte_ether_addr *)hw->mac.mac_addr);
1458 rte_eth_random_addr(hw->mac.mac_addr);
1460 ret = hns3vf_clear_vport_list(hw);
1462 PMD_INIT_LOG(ERR, "Failed to clear tbl list: %d", ret);
1463 goto err_get_config;
1466 ret = hns3vf_init_hardware(hns);
1468 goto err_get_config;
1470 hns3_set_default_rss_args(hw);
1473 * In the initialization clearing the all hardware mapping relationship
1474 * configurations between queues and interrupt vectors is needed, so
1475 * some error caused by the residual configurations, such as the
1476 * unexpected interrupt, can be avoid.
1478 ret = hns3vf_init_ring_with_vector(hw);
1480 goto err_get_config;
1485 hns3vf_disable_irq0(hw);
1486 rte_intr_disable(&pci_dev->intr_handle);
1487 hns3_intr_unregister(&pci_dev->intr_handle, hns3vf_interrupt_handler,
1489 err_intr_callback_register:
1491 hns3_cmd_uninit(hw);
1492 hns3_cmd_destroy_queue(hw);
1500 hns3vf_uninit_vf(struct rte_eth_dev *eth_dev)
1502 struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(eth_dev);
1503 struct hns3_adapter *hns = eth_dev->data->dev_private;
1504 struct hns3_hw *hw = &hns->hw;
1506 PMD_INIT_FUNC_TRACE();
1508 hns3_rss_uninit(hns);
1509 (void)hns3vf_set_alive(hw, false);
1510 (void)hns3vf_set_promisc_mode(hw, false, false, false);
1511 hns3vf_disable_irq0(hw);
1512 rte_intr_disable(&pci_dev->intr_handle);
1513 hns3_intr_unregister(&pci_dev->intr_handle, hns3vf_interrupt_handler,
1515 hns3_cmd_uninit(hw);
1516 hns3_cmd_destroy_queue(hw);
1521 hns3vf_do_stop(struct hns3_adapter *hns)
1523 struct hns3_hw *hw = &hns->hw;
1526 hw->mac.link_status = ETH_LINK_DOWN;
1528 if (rte_atomic16_read(&hw->reset.disable_cmd) == 0) {
1529 hns3vf_configure_mac_addr(hns, true);
1532 reset_queue = false;
1533 return hns3_stop_queues(hns, reset_queue);
1537 hns3vf_unmap_rx_interrupt(struct rte_eth_dev *dev)
1539 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1540 struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
1541 struct rte_intr_handle *intr_handle = &pci_dev->intr_handle;
1546 if (dev->data->dev_conf.intr_conf.rxq == 0)
1549 /* unmap the ring with vector */
1550 if (rte_intr_allow_others(intr_handle)) {
1551 vec = RTE_INTR_VEC_RXTX_OFFSET;
1552 base = RTE_INTR_VEC_RXTX_OFFSET;
1554 if (rte_intr_dp_is_en(intr_handle)) {
1555 for (q_id = 0; q_id < hw->used_rx_queues; q_id++) {
1556 (void)hns3vf_bind_ring_with_vector(hw, vec, false,
1559 if (vec < base + intr_handle->nb_efd - 1)
1563 /* Clean datapath event and queue/vec mapping */
1564 rte_intr_efd_disable(intr_handle);
1565 if (intr_handle->intr_vec) {
1566 rte_free(intr_handle->intr_vec);
1567 intr_handle->intr_vec = NULL;
1572 hns3vf_dev_stop(struct rte_eth_dev *dev)
1574 struct hns3_adapter *hns = dev->data->dev_private;
1575 struct hns3_hw *hw = &hns->hw;
1577 PMD_INIT_FUNC_TRACE();
1579 hw->adapter_state = HNS3_NIC_STOPPING;
1580 hns3_set_rxtx_function(dev);
1582 /* Disable datapath on secondary process. */
1583 hns3_mp_req_stop_rxtx(dev);
1584 /* Prevent crashes when queues are still in use. */
1585 rte_delay_ms(hw->tqps_num);
1587 rte_spinlock_lock(&hw->lock);
1588 if (rte_atomic16_read(&hw->reset.resetting) == 0) {
1589 hns3vf_do_stop(hns);
1590 hns3_dev_release_mbufs(hns);
1591 hw->adapter_state = HNS3_NIC_CONFIGURED;
1593 rte_eal_alarm_cancel(hns3vf_service_handler, dev);
1594 rte_spinlock_unlock(&hw->lock);
1596 hns3vf_unmap_rx_interrupt(dev);
1600 hns3vf_dev_close(struct rte_eth_dev *eth_dev)
1602 struct hns3_adapter *hns = eth_dev->data->dev_private;
1603 struct hns3_hw *hw = &hns->hw;
1605 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
1608 if (hw->adapter_state == HNS3_NIC_STARTED)
1609 hns3vf_dev_stop(eth_dev);
1611 hw->adapter_state = HNS3_NIC_CLOSING;
1612 hns3_reset_abort(hns);
1613 hw->adapter_state = HNS3_NIC_CLOSED;
1614 rte_eal_alarm_cancel(hns3vf_keep_alive_handler, eth_dev);
1615 hns3vf_configure_all_mc_mac_addr(hns, true);
1616 hns3vf_remove_all_vlan_table(hns);
1617 hns3vf_uninit_vf(eth_dev);
1618 hns3_free_all_queues(eth_dev);
1619 rte_free(hw->reset.wait_data);
1620 rte_free(eth_dev->process_private);
1621 eth_dev->process_private = NULL;
1622 hns3_mp_uninit_primary();
1623 hns3_warn(hw, "Close port %d finished", hw->data->port_id);
1627 hns3vf_dev_link_update(struct rte_eth_dev *eth_dev,
1628 __rte_unused int wait_to_complete)
1630 struct hns3_adapter *hns = eth_dev->data->dev_private;
1631 struct hns3_hw *hw = &hns->hw;
1632 struct hns3_mac *mac = &hw->mac;
1633 struct rte_eth_link new_link;
1635 memset(&new_link, 0, sizeof(new_link));
1636 switch (mac->link_speed) {
1637 case ETH_SPEED_NUM_10M:
1638 case ETH_SPEED_NUM_100M:
1639 case ETH_SPEED_NUM_1G:
1640 case ETH_SPEED_NUM_10G:
1641 case ETH_SPEED_NUM_25G:
1642 case ETH_SPEED_NUM_40G:
1643 case ETH_SPEED_NUM_50G:
1644 case ETH_SPEED_NUM_100G:
1645 new_link.link_speed = mac->link_speed;
1648 new_link.link_speed = ETH_SPEED_NUM_100M;
1652 new_link.link_duplex = mac->link_duplex;
1653 new_link.link_status = mac->link_status ? ETH_LINK_UP : ETH_LINK_DOWN;
1654 new_link.link_autoneg =
1655 !(eth_dev->data->dev_conf.link_speeds & ETH_LINK_SPEED_FIXED);
1657 return rte_eth_linkstatus_set(eth_dev, &new_link);
1661 hns3vf_do_start(struct hns3_adapter *hns, bool reset_queue)
1663 struct hns3_hw *hw = &hns->hw;
1666 ret = hns3vf_set_tc_info(hns);
1670 ret = hns3_start_queues(hns, reset_queue);
1672 hns3_err(hw, "Failed to start queues: %d", ret);
1680 hns3vf_map_rx_interrupt(struct rte_eth_dev *dev)
1682 struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
1683 struct rte_intr_handle *intr_handle = &pci_dev->intr_handle;
1684 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1685 uint32_t intr_vector;
1691 if (dev->data->dev_conf.intr_conf.rxq == 0)
1694 /* disable uio/vfio intr/eventfd mapping */
1695 rte_intr_disable(intr_handle);
1697 /* check and configure queue intr-vector mapping */
1698 if (rte_intr_cap_multiple(intr_handle) ||
1699 !RTE_ETH_DEV_SRIOV(dev).active) {
1700 intr_vector = hw->used_rx_queues;
1701 /* It creates event fd for each intr vector when MSIX is used */
1702 if (rte_intr_efd_enable(intr_handle, intr_vector))
1705 if (rte_intr_dp_is_en(intr_handle) && !intr_handle->intr_vec) {
1706 intr_handle->intr_vec =
1707 rte_zmalloc("intr_vec",
1708 hw->used_rx_queues * sizeof(int), 0);
1709 if (intr_handle->intr_vec == NULL) {
1710 hns3_err(hw, "Failed to allocate %d rx_queues"
1711 " intr_vec", hw->used_rx_queues);
1713 goto vf_alloc_intr_vec_error;
1717 if (rte_intr_allow_others(intr_handle)) {
1718 vec = RTE_INTR_VEC_RXTX_OFFSET;
1719 base = RTE_INTR_VEC_RXTX_OFFSET;
1721 if (rte_intr_dp_is_en(intr_handle)) {
1722 for (q_id = 0; q_id < hw->used_rx_queues; q_id++) {
1723 ret = hns3vf_bind_ring_with_vector(hw, vec, true,
1727 goto vf_bind_vector_error;
1728 intr_handle->intr_vec[q_id] = vec;
1729 if (vec < base + intr_handle->nb_efd - 1)
1733 rte_intr_enable(intr_handle);
1736 vf_bind_vector_error:
1737 rte_intr_efd_disable(intr_handle);
1738 if (intr_handle->intr_vec) {
1739 free(intr_handle->intr_vec);
1740 intr_handle->intr_vec = NULL;
1743 vf_alloc_intr_vec_error:
1744 rte_intr_efd_disable(intr_handle);
1749 hns3vf_restore_filter(struct rte_eth_dev *dev)
1751 hns3_restore_rss_filter(dev);
1755 hns3vf_dev_start(struct rte_eth_dev *dev)
1757 struct hns3_adapter *hns = dev->data->dev_private;
1758 struct hns3_hw *hw = &hns->hw;
1761 PMD_INIT_FUNC_TRACE();
1762 if (rte_atomic16_read(&hw->reset.resetting))
1765 rte_spinlock_lock(&hw->lock);
1766 hw->adapter_state = HNS3_NIC_STARTING;
1767 ret = hns3vf_do_start(hns, true);
1769 hw->adapter_state = HNS3_NIC_CONFIGURED;
1770 rte_spinlock_unlock(&hw->lock);
1773 hw->adapter_state = HNS3_NIC_STARTED;
1774 rte_spinlock_unlock(&hw->lock);
1776 ret = hns3vf_map_rx_interrupt(dev);
1779 hns3_set_rxtx_function(dev);
1780 hns3_mp_req_start_rxtx(dev);
1781 rte_eal_alarm_set(HNS3VF_SERVICE_INTERVAL, hns3vf_service_handler, dev);
1783 hns3vf_restore_filter(dev);
1789 is_vf_reset_done(struct hns3_hw *hw)
1791 #define HNS3_FUN_RST_ING_BITS \
1792 (BIT(HNS3_VECTOR0_GLOBALRESET_INT_B) | \
1793 BIT(HNS3_VECTOR0_CORERESET_INT_B) | \
1794 BIT(HNS3_VECTOR0_IMPRESET_INT_B) | \
1795 BIT(HNS3_VECTOR0_FUNCRESET_INT_B))
1799 if (hw->reset.level == HNS3_VF_RESET) {
1800 val = hns3_read_dev(hw, HNS3_VF_RST_ING);
1801 if (val & HNS3_VF_RST_ING_BIT)
1804 val = hns3_read_dev(hw, HNS3_FUN_RST_ING);
1805 if (val & HNS3_FUN_RST_ING_BITS)
1812 hns3vf_is_reset_pending(struct hns3_adapter *hns)
1814 struct hns3_hw *hw = &hns->hw;
1815 enum hns3_reset_level reset;
1817 hns3vf_check_event_cause(hns, NULL);
1818 reset = hns3vf_get_reset_level(hw, &hw->reset.pending);
1819 if (hw->reset.level != HNS3_NONE_RESET && hw->reset.level < reset) {
1820 hns3_warn(hw, "High level reset %d is pending", reset);
1827 hns3vf_wait_hardware_ready(struct hns3_adapter *hns)
1829 struct hns3_hw *hw = &hns->hw;
1830 struct hns3_wait_data *wait_data = hw->reset.wait_data;
1833 if (wait_data->result == HNS3_WAIT_SUCCESS) {
1835 * After vf reset is ready, the PF may not have completed
1836 * the reset processing. The vf sending mbox to PF may fail
1837 * during the pf reset, so it is better to add extra delay.
1839 if (hw->reset.level == HNS3_VF_FUNC_RESET ||
1840 hw->reset.level == HNS3_FLR_RESET)
1842 /* Reset retry process, no need to add extra delay. */
1843 if (hw->reset.attempts)
1845 if (wait_data->check_completion == NULL)
1848 wait_data->check_completion = NULL;
1849 wait_data->interval = 1 * MSEC_PER_SEC * USEC_PER_MSEC;
1850 wait_data->count = 1;
1851 wait_data->result = HNS3_WAIT_REQUEST;
1852 rte_eal_alarm_set(wait_data->interval, hns3_wait_callback,
1854 hns3_warn(hw, "hardware is ready, delay 1 sec for PF reset complete");
1856 } else if (wait_data->result == HNS3_WAIT_TIMEOUT) {
1857 gettimeofday(&tv, NULL);
1858 hns3_warn(hw, "Reset step4 hardware not ready after reset time=%ld.%.6ld",
1859 tv.tv_sec, tv.tv_usec);
1861 } else if (wait_data->result == HNS3_WAIT_REQUEST)
1864 wait_data->hns = hns;
1865 wait_data->check_completion = is_vf_reset_done;
1866 wait_data->end_ms = (uint64_t)HNS3VF_RESET_WAIT_CNT *
1867 HNS3VF_RESET_WAIT_MS + get_timeofday_ms();
1868 wait_data->interval = HNS3VF_RESET_WAIT_MS * USEC_PER_MSEC;
1869 wait_data->count = HNS3VF_RESET_WAIT_CNT;
1870 wait_data->result = HNS3_WAIT_REQUEST;
1871 rte_eal_alarm_set(wait_data->interval, hns3_wait_callback, wait_data);
1876 hns3vf_prepare_reset(struct hns3_adapter *hns)
1878 struct hns3_hw *hw = &hns->hw;
1881 if (hw->reset.level == HNS3_VF_FUNC_RESET) {
1882 ret = hns3_send_mbx_msg(hw, HNS3_MBX_RESET, 0, NULL,
1885 rte_atomic16_set(&hw->reset.disable_cmd, 1);
1891 hns3vf_stop_service(struct hns3_adapter *hns)
1893 struct hns3_hw *hw = &hns->hw;
1894 struct rte_eth_dev *eth_dev;
1896 eth_dev = &rte_eth_devices[hw->data->port_id];
1897 if (hw->adapter_state == HNS3_NIC_STARTED)
1898 rte_eal_alarm_cancel(hns3vf_service_handler, eth_dev);
1899 hw->mac.link_status = ETH_LINK_DOWN;
1901 hns3_set_rxtx_function(eth_dev);
1903 /* Disable datapath on secondary process. */
1904 hns3_mp_req_stop_rxtx(eth_dev);
1905 rte_delay_ms(hw->tqps_num);
1907 rte_spinlock_lock(&hw->lock);
1908 if (hw->adapter_state == HNS3_NIC_STARTED ||
1909 hw->adapter_state == HNS3_NIC_STOPPING) {
1910 hns3vf_do_stop(hns);
1911 hw->reset.mbuf_deferred_free = true;
1913 hw->reset.mbuf_deferred_free = false;
1916 * It is cumbersome for hardware to pick-and-choose entries for deletion
1917 * from table space. Hence, for function reset software intervention is
1918 * required to delete the entries.
1920 if (rte_atomic16_read(&hw->reset.disable_cmd) == 0)
1921 hns3vf_configure_all_mc_mac_addr(hns, true);
1922 rte_spinlock_unlock(&hw->lock);
1928 hns3vf_start_service(struct hns3_adapter *hns)
1930 struct hns3_hw *hw = &hns->hw;
1931 struct rte_eth_dev *eth_dev;
1933 eth_dev = &rte_eth_devices[hw->data->port_id];
1934 hns3_set_rxtx_function(eth_dev);
1935 hns3_mp_req_start_rxtx(eth_dev);
1936 if (hw->adapter_state == HNS3_NIC_STARTED)
1937 hns3vf_service_handler(eth_dev);
1943 hns3vf_check_default_mac_change(struct hns3_hw *hw)
1945 char mac_str[RTE_ETHER_ADDR_FMT_SIZE];
1946 struct rte_ether_addr *hw_mac;
1950 * The hns3 PF ethdev driver in kernel support setting VF MAC address
1951 * on the host by "ip link set ..." command. If the hns3 PF kernel
1952 * ethdev driver sets the MAC address for VF device after the
1953 * initialization of the related VF device, the PF driver will notify
1954 * VF driver to reset VF device to make the new MAC address effective
1955 * immediately. The hns3 VF PMD driver should check whether the MAC
1956 * address has been changed by the PF kernel ethdev driver, if changed
1957 * VF driver should configure hardware using the new MAC address in the
1958 * recovering hardware configuration stage of the reset process.
1960 ret = hns3vf_get_host_mac_addr(hw);
1964 hw_mac = (struct rte_ether_addr *)hw->mac.mac_addr;
1965 ret = rte_is_zero_ether_addr(hw_mac);
1967 rte_ether_addr_copy(&hw->data->mac_addrs[0], hw_mac);
1969 ret = rte_is_same_ether_addr(&hw->data->mac_addrs[0], hw_mac);
1971 rte_ether_addr_copy(hw_mac, &hw->data->mac_addrs[0]);
1972 rte_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
1973 &hw->data->mac_addrs[0]);
1974 hns3_warn(hw, "Default MAC address has been changed to:"
1975 " %s by the host PF kernel ethdev driver",
1984 hns3vf_restore_conf(struct hns3_adapter *hns)
1986 struct hns3_hw *hw = &hns->hw;
1989 ret = hns3vf_check_default_mac_change(hw);
1993 ret = hns3vf_configure_mac_addr(hns, false);
1997 ret = hns3vf_configure_all_mc_mac_addr(hns, false);
2001 ret = hns3vf_restore_promisc(hns);
2003 goto err_vlan_table;
2005 ret = hns3vf_restore_vlan_conf(hns);
2007 goto err_vlan_table;
2009 if (hw->adapter_state == HNS3_NIC_STARTED) {
2010 ret = hns3vf_do_start(hns, false);
2012 goto err_vlan_table;
2013 hns3_info(hw, "hns3vf dev restart successful!");
2014 } else if (hw->adapter_state == HNS3_NIC_STOPPING)
2015 hw->adapter_state = HNS3_NIC_CONFIGURED;
2019 hns3vf_configure_all_mc_mac_addr(hns, true);
2021 hns3vf_configure_mac_addr(hns, true);
2025 static enum hns3_reset_level
2026 hns3vf_get_reset_level(struct hns3_hw *hw, uint64_t *levels)
2028 enum hns3_reset_level reset_level;
2030 /* return the highest priority reset level amongst all */
2031 if (hns3_atomic_test_bit(HNS3_VF_RESET, levels))
2032 reset_level = HNS3_VF_RESET;
2033 else if (hns3_atomic_test_bit(HNS3_VF_FULL_RESET, levels))
2034 reset_level = HNS3_VF_FULL_RESET;
2035 else if (hns3_atomic_test_bit(HNS3_VF_PF_FUNC_RESET, levels))
2036 reset_level = HNS3_VF_PF_FUNC_RESET;
2037 else if (hns3_atomic_test_bit(HNS3_VF_FUNC_RESET, levels))
2038 reset_level = HNS3_VF_FUNC_RESET;
2039 else if (hns3_atomic_test_bit(HNS3_FLR_RESET, levels))
2040 reset_level = HNS3_FLR_RESET;
2042 reset_level = HNS3_NONE_RESET;
2044 if (hw->reset.level != HNS3_NONE_RESET && reset_level < hw->reset.level)
2045 return HNS3_NONE_RESET;
2051 hns3vf_reset_service(void *param)
2053 struct hns3_adapter *hns = (struct hns3_adapter *)param;
2054 struct hns3_hw *hw = &hns->hw;
2055 enum hns3_reset_level reset_level;
2056 struct timeval tv_delta;
2057 struct timeval tv_start;
2062 * The interrupt is not triggered within the delay time.
2063 * The interrupt may have been lost. It is necessary to handle
2064 * the interrupt to recover from the error.
2066 if (rte_atomic16_read(&hns->hw.reset.schedule) == SCHEDULE_DEFERRED) {
2067 rte_atomic16_set(&hns->hw.reset.schedule, SCHEDULE_REQUESTED);
2068 hns3_err(hw, "Handling interrupts in delayed tasks");
2069 hns3vf_interrupt_handler(&rte_eth_devices[hw->data->port_id]);
2070 reset_level = hns3vf_get_reset_level(hw, &hw->reset.pending);
2071 if (reset_level == HNS3_NONE_RESET) {
2072 hns3_err(hw, "No reset level is set, try global reset");
2073 hns3_atomic_set_bit(HNS3_VF_RESET, &hw->reset.pending);
2076 rte_atomic16_set(&hns->hw.reset.schedule, SCHEDULE_NONE);
2079 * Hardware reset has been notified, we now have to poll & check if
2080 * hardware has actually completed the reset sequence.
2082 reset_level = hns3vf_get_reset_level(hw, &hw->reset.pending);
2083 if (reset_level != HNS3_NONE_RESET) {
2084 gettimeofday(&tv_start, NULL);
2085 hns3_reset_process(hns, reset_level);
2086 gettimeofday(&tv, NULL);
2087 timersub(&tv, &tv_start, &tv_delta);
2088 msec = tv_delta.tv_sec * MSEC_PER_SEC +
2089 tv_delta.tv_usec / USEC_PER_MSEC;
2090 if (msec > HNS3_RESET_PROCESS_MS)
2091 hns3_err(hw, "%d handle long time delta %" PRIx64
2092 " ms time=%ld.%.6ld",
2093 hw->reset.level, msec, tv.tv_sec, tv.tv_usec);
2098 hns3vf_reinit_dev(struct hns3_adapter *hns)
2100 struct rte_eth_dev *eth_dev = &rte_eth_devices[hns->hw.data->port_id];
2101 struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(eth_dev);
2102 struct hns3_hw *hw = &hns->hw;
2105 if (hw->reset.level == HNS3_VF_FULL_RESET) {
2106 rte_intr_disable(&pci_dev->intr_handle);
2107 hns3vf_set_bus_master(pci_dev, true);
2110 /* Firmware command initialize */
2111 ret = hns3_cmd_init(hw);
2113 hns3_err(hw, "Failed to init cmd: %d", ret);
2117 if (hw->reset.level == HNS3_VF_FULL_RESET) {
2119 * UIO enables msix by writing the pcie configuration space
2120 * vfio_pci enables msix in rte_intr_enable.
2122 if (pci_dev->kdrv == RTE_KDRV_IGB_UIO ||
2123 pci_dev->kdrv == RTE_KDRV_UIO_GENERIC) {
2124 if (hns3vf_enable_msix(pci_dev, true))
2125 hns3_err(hw, "Failed to enable msix");
2128 rte_intr_enable(&pci_dev->intr_handle);
2131 ret = hns3_reset_all_queues(hns);
2133 hns3_err(hw, "Failed to reset all queues: %d", ret);
2137 ret = hns3vf_init_hardware(hns);
2139 hns3_err(hw, "Failed to init hardware: %d", ret);
2146 static const struct eth_dev_ops hns3vf_eth_dev_ops = {
2147 .dev_start = hns3vf_dev_start,
2148 .dev_stop = hns3vf_dev_stop,
2149 .dev_close = hns3vf_dev_close,
2150 .mtu_set = hns3vf_dev_mtu_set,
2151 .promiscuous_enable = hns3vf_dev_promiscuous_enable,
2152 .promiscuous_disable = hns3vf_dev_promiscuous_disable,
2153 .allmulticast_enable = hns3vf_dev_allmulticast_enable,
2154 .allmulticast_disable = hns3vf_dev_allmulticast_disable,
2155 .stats_get = hns3_stats_get,
2156 .stats_reset = hns3_stats_reset,
2157 .xstats_get = hns3_dev_xstats_get,
2158 .xstats_get_names = hns3_dev_xstats_get_names,
2159 .xstats_reset = hns3_dev_xstats_reset,
2160 .xstats_get_by_id = hns3_dev_xstats_get_by_id,
2161 .xstats_get_names_by_id = hns3_dev_xstats_get_names_by_id,
2162 .dev_infos_get = hns3vf_dev_infos_get,
2163 .rx_queue_setup = hns3_rx_queue_setup,
2164 .tx_queue_setup = hns3_tx_queue_setup,
2165 .rx_queue_release = hns3_dev_rx_queue_release,
2166 .tx_queue_release = hns3_dev_tx_queue_release,
2167 .rx_queue_intr_enable = hns3_dev_rx_queue_intr_enable,
2168 .rx_queue_intr_disable = hns3_dev_rx_queue_intr_disable,
2169 .dev_configure = hns3vf_dev_configure,
2170 .mac_addr_add = hns3vf_add_mac_addr,
2171 .mac_addr_remove = hns3vf_remove_mac_addr,
2172 .mac_addr_set = hns3vf_set_default_mac_addr,
2173 .set_mc_addr_list = hns3vf_set_mc_mac_addr_list,
2174 .link_update = hns3vf_dev_link_update,
2175 .rss_hash_update = hns3_dev_rss_hash_update,
2176 .rss_hash_conf_get = hns3_dev_rss_hash_conf_get,
2177 .reta_update = hns3_dev_rss_reta_update,
2178 .reta_query = hns3_dev_rss_reta_query,
2179 .filter_ctrl = hns3_dev_filter_ctrl,
2180 .vlan_filter_set = hns3vf_vlan_filter_set,
2181 .vlan_offload_set = hns3vf_vlan_offload_set,
2182 .get_reg = hns3_get_regs,
2183 .dev_supported_ptypes_get = hns3_dev_supported_ptypes_get,
2186 static const struct hns3_reset_ops hns3vf_reset_ops = {
2187 .reset_service = hns3vf_reset_service,
2188 .stop_service = hns3vf_stop_service,
2189 .prepare_reset = hns3vf_prepare_reset,
2190 .wait_hardware_ready = hns3vf_wait_hardware_ready,
2191 .reinit_dev = hns3vf_reinit_dev,
2192 .restore_conf = hns3vf_restore_conf,
2193 .start_service = hns3vf_start_service,
2197 hns3vf_dev_init(struct rte_eth_dev *eth_dev)
2199 struct hns3_adapter *hns = eth_dev->data->dev_private;
2200 struct hns3_hw *hw = &hns->hw;
2203 PMD_INIT_FUNC_TRACE();
2205 eth_dev->process_private = (struct hns3_process_private *)
2206 rte_zmalloc_socket("hns3_filter_list",
2207 sizeof(struct hns3_process_private),
2208 RTE_CACHE_LINE_SIZE, eth_dev->device->numa_node);
2209 if (eth_dev->process_private == NULL) {
2210 PMD_INIT_LOG(ERR, "Failed to alloc memory for process private");
2214 /* initialize flow filter lists */
2215 hns3_filterlist_init(eth_dev);
2217 hns3_set_rxtx_function(eth_dev);
2218 eth_dev->dev_ops = &hns3vf_eth_dev_ops;
2219 if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
2220 hns3_mp_init_secondary();
2221 hw->secondary_cnt++;
2225 hns3_mp_init_primary();
2227 hw->adapter_state = HNS3_NIC_UNINITIALIZED;
2229 hw->data = eth_dev->data;
2231 ret = hns3_reset_init(hw);
2233 goto err_init_reset;
2234 hw->reset.ops = &hns3vf_reset_ops;
2236 ret = hns3vf_init_vf(eth_dev);
2238 PMD_INIT_LOG(ERR, "Failed to init vf: %d", ret);
2242 /* Allocate memory for storing MAC addresses */
2243 eth_dev->data->mac_addrs = rte_zmalloc("hns3vf-mac",
2244 sizeof(struct rte_ether_addr) *
2245 HNS3_VF_UC_MACADDR_NUM, 0);
2246 if (eth_dev->data->mac_addrs == NULL) {
2247 PMD_INIT_LOG(ERR, "Failed to allocate %zx bytes needed "
2248 "to store MAC addresses",
2249 sizeof(struct rte_ether_addr) *
2250 HNS3_VF_UC_MACADDR_NUM);
2252 goto err_rte_zmalloc;
2255 rte_ether_addr_copy((struct rte_ether_addr *)hw->mac.mac_addr,
2256 ð_dev->data->mac_addrs[0]);
2257 hw->adapter_state = HNS3_NIC_INITIALIZED;
2259 * Pass the information to the rte_eth_dev_close() that it should also
2260 * release the private port resources.
2262 eth_dev->data->dev_flags |= RTE_ETH_DEV_CLOSE_REMOVE;
2264 if (rte_atomic16_read(&hns->hw.reset.schedule) == SCHEDULE_PENDING) {
2265 hns3_err(hw, "Reschedule reset service after dev_init");
2266 hns3_schedule_reset(hns);
2268 /* IMP will wait ready flag before reset */
2269 hns3_notify_reset_ready(hw, false);
2271 rte_eal_alarm_set(HNS3VF_KEEP_ALIVE_INTERVAL, hns3vf_keep_alive_handler,
2276 hns3vf_uninit_vf(eth_dev);
2279 rte_free(hw->reset.wait_data);
2282 eth_dev->dev_ops = NULL;
2283 eth_dev->rx_pkt_burst = NULL;
2284 eth_dev->tx_pkt_burst = NULL;
2285 eth_dev->tx_pkt_prepare = NULL;
2286 rte_free(eth_dev->process_private);
2287 eth_dev->process_private = NULL;
2293 hns3vf_dev_uninit(struct rte_eth_dev *eth_dev)
2295 struct hns3_adapter *hns = eth_dev->data->dev_private;
2296 struct hns3_hw *hw = &hns->hw;
2298 PMD_INIT_FUNC_TRACE();
2300 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
2303 eth_dev->dev_ops = NULL;
2304 eth_dev->rx_pkt_burst = NULL;
2305 eth_dev->tx_pkt_burst = NULL;
2306 eth_dev->tx_pkt_prepare = NULL;
2308 if (hw->adapter_state < HNS3_NIC_CLOSING)
2309 hns3vf_dev_close(eth_dev);
2311 hw->adapter_state = HNS3_NIC_REMOVED;
2316 eth_hns3vf_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
2317 struct rte_pci_device *pci_dev)
2319 return rte_eth_dev_pci_generic_probe(pci_dev,
2320 sizeof(struct hns3_adapter),
2325 eth_hns3vf_pci_remove(struct rte_pci_device *pci_dev)
2327 return rte_eth_dev_pci_generic_remove(pci_dev, hns3vf_dev_uninit);
2330 static const struct rte_pci_id pci_id_hns3vf_map[] = {
2331 { RTE_PCI_DEVICE(PCI_VENDOR_ID_HUAWEI, HNS3_DEV_ID_100G_VF) },
2332 { RTE_PCI_DEVICE(PCI_VENDOR_ID_HUAWEI, HNS3_DEV_ID_100G_RDMA_PFC_VF) },
2333 { .vendor_id = 0, /* sentinel */ },
2336 static struct rte_pci_driver rte_hns3vf_pmd = {
2337 .id_table = pci_id_hns3vf_map,
2338 .drv_flags = RTE_PCI_DRV_NEED_MAPPING,
2339 .probe = eth_hns3vf_pci_probe,
2340 .remove = eth_hns3vf_pci_remove,
2343 RTE_PMD_REGISTER_PCI(net_hns3_vf, rte_hns3vf_pmd);
2344 RTE_PMD_REGISTER_PCI_TABLE(net_hns3_vf, pci_id_hns3vf_map);
2345 RTE_PMD_REGISTER_KMOD_DEP(net_hns3_vf, "* igb_uio | vfio-pci");