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 __attribute__ ((unused)) uint32_t idx,
140 __attribute__ ((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];
195 if (!rte_is_valid_assigned_ether_addr(mac_addr)) {
196 rte_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
198 hns3_err(hw, "Failed to set mac addr, addr(%s) invalid.",
203 old_addr = (struct rte_ether_addr *)hw->mac.mac_addr;
204 rte_spinlock_lock(&hw->lock);
205 memcpy(addr_bytes, mac_addr->addr_bytes, RTE_ETHER_ADDR_LEN);
206 memcpy(&addr_bytes[RTE_ETHER_ADDR_LEN], old_addr->addr_bytes,
209 ret = hns3_send_mbx_msg(hw, HNS3_MBX_SET_UNICAST,
210 HNS3_MBX_MAC_VLAN_UC_MODIFY, addr_bytes,
211 HNS3_TWO_ETHER_ADDR_LEN, false, NULL, 0);
213 rte_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
215 hns3_err(hw, "Failed to set mac addr(%s) for vf: %d", mac_str,
219 rte_ether_addr_copy(mac_addr,
220 (struct rte_ether_addr *)hw->mac.mac_addr);
221 rte_spinlock_unlock(&hw->lock);
227 hns3vf_configure_mac_addr(struct hns3_adapter *hns, bool del)
229 struct hns3_hw *hw = &hns->hw;
230 struct rte_ether_addr *addr;
231 enum hns3_mbx_mac_vlan_subcode opcode;
232 char mac_str[RTE_ETHER_ADDR_FMT_SIZE];
237 opcode = HNS3_MBX_MAC_VLAN_UC_REMOVE;
239 opcode = HNS3_MBX_MAC_VLAN_UC_ADD;
240 for (i = 0; i < HNS3_VF_UC_MACADDR_NUM; i++) {
241 addr = &hw->data->mac_addrs[i];
242 if (!rte_is_valid_assigned_ether_addr(addr))
244 rte_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE, addr);
245 hns3_dbg(hw, "rm mac addr: %s", mac_str);
246 ret = hns3_send_mbx_msg(hw, HNS3_MBX_SET_UNICAST, opcode,
247 addr->addr_bytes, RTE_ETHER_ADDR_LEN,
250 hns3_err(hw, "Failed to remove mac addr for vf: %d",
259 hns3vf_add_mc_mac_addr(struct hns3_adapter *hns,
260 struct rte_ether_addr *mac_addr)
262 char mac_str[RTE_ETHER_ADDR_FMT_SIZE];
263 struct hns3_hw *hw = &hns->hw;
266 ret = hns3_send_mbx_msg(hw, HNS3_MBX_SET_MULTICAST,
267 HNS3_MBX_MAC_VLAN_MC_ADD,
268 mac_addr->addr_bytes, RTE_ETHER_ADDR_LEN, false,
271 rte_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
273 hns3_err(hw, "Failed to add mc mac addr(%s) for vf: %d",
282 hns3vf_remove_mc_mac_addr(struct hns3_adapter *hns,
283 struct rte_ether_addr *mac_addr)
285 char mac_str[RTE_ETHER_ADDR_FMT_SIZE];
286 struct hns3_hw *hw = &hns->hw;
289 ret = hns3_send_mbx_msg(hw, HNS3_MBX_SET_MULTICAST,
290 HNS3_MBX_MAC_VLAN_MC_REMOVE,
291 mac_addr->addr_bytes, RTE_ETHER_ADDR_LEN, false,
294 rte_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
296 hns3_err(hw, "Failed to remove mc mac addr(%s) for vf: %d",
305 hns3vf_set_mc_mac_addr_list(struct rte_eth_dev *dev,
306 struct rte_ether_addr *mc_addr_set,
309 struct hns3_adapter *hns = dev->data->dev_private;
310 struct hns3_hw *hw = &hns->hw;
311 struct rte_ether_addr *addr;
312 char mac_str[RTE_ETHER_ADDR_FMT_SIZE];
319 if (nb_mc_addr > HNS3_MC_MACADDR_NUM) {
320 hns3_err(hw, "Failed to set mc mac addr, nb_mc_addr(%d) "
321 "invalid. valid range: 0~%d",
322 nb_mc_addr, HNS3_MC_MACADDR_NUM);
326 set_addr_num = (int)nb_mc_addr;
327 for (i = 0; i < set_addr_num; i++) {
328 addr = &mc_addr_set[i];
329 if (!rte_is_multicast_ether_addr(addr)) {
330 rte_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
333 "Failed to set mc mac addr, addr(%s) invalid.",
338 rte_spinlock_lock(&hw->lock);
339 cur_addr_num = hw->mc_addrs_num;
340 for (i = 0; i < cur_addr_num; i++) {
341 num = cur_addr_num - i - 1;
342 addr = &hw->mc_addrs[num];
343 ret = hns3vf_remove_mc_mac_addr(hns, addr);
345 rte_spinlock_unlock(&hw->lock);
352 for (i = 0; i < set_addr_num; i++) {
353 addr = &mc_addr_set[i];
354 ret = hns3vf_add_mc_mac_addr(hns, addr);
356 rte_spinlock_unlock(&hw->lock);
360 rte_ether_addr_copy(addr, &hw->mc_addrs[hw->mc_addrs_num]);
363 rte_spinlock_unlock(&hw->lock);
369 hns3vf_configure_all_mc_mac_addr(struct hns3_adapter *hns, bool del)
371 char mac_str[RTE_ETHER_ADDR_FMT_SIZE];
372 struct hns3_hw *hw = &hns->hw;
373 struct rte_ether_addr *addr;
378 for (i = 0; i < hw->mc_addrs_num; i++) {
379 addr = &hw->mc_addrs[i];
380 if (!rte_is_multicast_ether_addr(addr))
383 ret = hns3vf_remove_mc_mac_addr(hns, addr);
385 ret = hns3vf_add_mc_mac_addr(hns, addr);
388 rte_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
390 hns3_err(hw, "Failed to %s mc mac addr: %s for vf: %d",
391 del ? "Remove" : "Restore", mac_str, ret);
398 hns3vf_set_promisc_mode(struct hns3_hw *hw, bool en_bc_pmc)
400 struct hns3_mbx_vf_to_pf_cmd *req;
401 struct hns3_cmd_desc desc;
404 req = (struct hns3_mbx_vf_to_pf_cmd *)desc.data;
406 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_MBX_VF_TO_PF, false);
407 req->msg[0] = HNS3_MBX_SET_PROMISC_MODE;
408 req->msg[1] = en_bc_pmc ? 1 : 0;
410 ret = hns3_cmd_send(hw, &desc, 1);
412 hns3_err(hw, "Set promisc mode fail, status is %d", ret);
418 hns3vf_dev_configure(struct rte_eth_dev *dev)
420 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
421 struct hns3_rss_conf *rss_cfg = &hw->rss_info;
422 struct rte_eth_conf *conf = &dev->data->dev_conf;
423 enum rte_eth_rx_mq_mode mq_mode = conf->rxmode.mq_mode;
424 uint16_t nb_rx_q = dev->data->nb_rx_queues;
425 uint16_t nb_tx_q = dev->data->nb_tx_queues;
426 struct rte_eth_rss_conf rss_conf;
431 * Hardware does not support where the number of rx and tx queues is
432 * not equal in hip08.
434 if (nb_rx_q != nb_tx_q) {
436 "nb_rx_queues(%u) not equal with nb_tx_queues(%u)! "
437 "Hardware does not support this configuration!",
442 if (conf->link_speeds & ETH_LINK_SPEED_FIXED) {
443 hns3_err(hw, "setting link speed/duplex not supported");
447 hw->adapter_state = HNS3_NIC_CONFIGURING;
449 /* When RSS is not configured, redirect the packet queue 0 */
450 if ((uint32_t)mq_mode & ETH_MQ_RX_RSS_FLAG) {
451 rss_conf = conf->rx_adv_conf.rss_conf;
452 if (rss_conf.rss_key == NULL) {
453 rss_conf.rss_key = rss_cfg->key;
454 rss_conf.rss_key_len = HNS3_RSS_KEY_SIZE;
457 ret = hns3_dev_rss_hash_update(dev, &rss_conf);
463 * If jumbo frames are enabled, MTU needs to be refreshed
464 * according to the maximum RX packet length.
466 if (conf->rxmode.offloads & DEV_RX_OFFLOAD_JUMBO_FRAME) {
468 * Security of max_rx_pkt_len is guaranteed in dpdk frame.
469 * Maximum value of max_rx_pkt_len is HNS3_MAX_FRAME_LEN, so it
470 * can safely assign to "uint16_t" type variable.
472 mtu = (uint16_t)HNS3_PKTLEN_TO_MTU(conf->rxmode.max_rx_pkt_len);
473 ret = hns3vf_dev_mtu_set(dev, mtu);
476 dev->data->mtu = mtu;
479 ret = hns3vf_dev_configure_vlan(dev);
483 hw->adapter_state = HNS3_NIC_CONFIGURED;
487 hw->adapter_state = HNS3_NIC_INITIALIZED;
492 hns3vf_config_mtu(struct hns3_hw *hw, uint16_t mtu)
496 ret = hns3_send_mbx_msg(hw, HNS3_MBX_SET_MTU, 0, (const uint8_t *)&mtu,
497 sizeof(mtu), true, NULL, 0);
499 hns3_err(hw, "Failed to set mtu (%u) for vf: %d", mtu, ret);
505 hns3vf_dev_mtu_set(struct rte_eth_dev *dev, uint16_t mtu)
507 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
508 uint32_t frame_size = mtu + HNS3_ETH_OVERHEAD;
511 if (dev->data->dev_started) {
512 hns3_err(hw, "Failed to set mtu, port %u must be stopped "
513 "before configuration", dev->data->port_id);
517 if (rte_atomic16_read(&hw->reset.resetting)) {
518 hns3_err(hw, "Failed to set mtu during resetting");
522 rte_spinlock_lock(&hw->lock);
523 ret = hns3vf_config_mtu(hw, mtu);
525 rte_spinlock_unlock(&hw->lock);
528 if (frame_size > RTE_ETHER_MAX_LEN)
529 dev->data->dev_conf.rxmode.offloads |=
530 DEV_RX_OFFLOAD_JUMBO_FRAME;
532 dev->data->dev_conf.rxmode.offloads &=
533 ~DEV_RX_OFFLOAD_JUMBO_FRAME;
534 dev->data->dev_conf.rxmode.max_rx_pkt_len = frame_size;
535 rte_spinlock_unlock(&hw->lock);
541 hns3vf_dev_infos_get(struct rte_eth_dev *eth_dev, struct rte_eth_dev_info *info)
543 struct hns3_adapter *hns = eth_dev->data->dev_private;
544 struct hns3_hw *hw = &hns->hw;
546 info->max_rx_queues = hw->tqps_num;
547 info->max_tx_queues = hw->tqps_num;
548 info->max_rx_pktlen = HNS3_MAX_FRAME_LEN; /* CRC included */
549 info->min_rx_bufsize = hw->rx_buf_len;
550 info->max_mac_addrs = HNS3_VF_UC_MACADDR_NUM;
551 info->max_mtu = info->max_rx_pktlen - HNS3_ETH_OVERHEAD;
553 info->rx_offload_capa = (DEV_RX_OFFLOAD_IPV4_CKSUM |
554 DEV_RX_OFFLOAD_UDP_CKSUM |
555 DEV_RX_OFFLOAD_TCP_CKSUM |
556 DEV_RX_OFFLOAD_SCTP_CKSUM |
557 DEV_RX_OFFLOAD_OUTER_IPV4_CKSUM |
558 DEV_RX_OFFLOAD_OUTER_UDP_CKSUM |
559 DEV_RX_OFFLOAD_KEEP_CRC |
560 DEV_RX_OFFLOAD_SCATTER |
561 DEV_RX_OFFLOAD_VLAN_STRIP |
562 DEV_RX_OFFLOAD_QINQ_STRIP |
563 DEV_RX_OFFLOAD_VLAN_FILTER |
564 DEV_RX_OFFLOAD_JUMBO_FRAME);
565 info->tx_queue_offload_capa = DEV_TX_OFFLOAD_MBUF_FAST_FREE;
566 info->tx_offload_capa = (DEV_TX_OFFLOAD_OUTER_IPV4_CKSUM |
567 DEV_TX_OFFLOAD_IPV4_CKSUM |
568 DEV_TX_OFFLOAD_TCP_CKSUM |
569 DEV_TX_OFFLOAD_UDP_CKSUM |
570 DEV_TX_OFFLOAD_SCTP_CKSUM |
571 DEV_TX_OFFLOAD_VLAN_INSERT |
572 DEV_TX_OFFLOAD_QINQ_INSERT |
573 DEV_TX_OFFLOAD_MULTI_SEGS |
574 info->tx_queue_offload_capa);
576 info->rx_desc_lim = (struct rte_eth_desc_lim) {
577 .nb_max = HNS3_MAX_RING_DESC,
578 .nb_min = HNS3_MIN_RING_DESC,
579 .nb_align = HNS3_ALIGN_RING_DESC,
582 info->tx_desc_lim = (struct rte_eth_desc_lim) {
583 .nb_max = HNS3_MAX_RING_DESC,
584 .nb_min = HNS3_MIN_RING_DESC,
585 .nb_align = HNS3_ALIGN_RING_DESC,
588 info->vmdq_queue_num = 0;
590 info->reta_size = HNS3_RSS_IND_TBL_SIZE;
591 info->hash_key_size = HNS3_RSS_KEY_SIZE;
592 info->flow_type_rss_offloads = HNS3_ETH_RSS_SUPPORT;
593 info->default_rxportconf.ring_size = HNS3_DEFAULT_RING_DESC;
594 info->default_txportconf.ring_size = HNS3_DEFAULT_RING_DESC;
600 hns3vf_clear_event_cause(struct hns3_hw *hw, uint32_t regclr)
602 hns3_write_dev(hw, HNS3_VECTOR0_CMDQ_SRC_REG, regclr);
606 hns3vf_disable_irq0(struct hns3_hw *hw)
608 hns3_write_dev(hw, HNS3_MISC_VECTOR_REG_BASE, 0);
612 hns3vf_enable_irq0(struct hns3_hw *hw)
614 hns3_write_dev(hw, HNS3_MISC_VECTOR_REG_BASE, 1);
617 static enum hns3vf_evt_cause
618 hns3vf_check_event_cause(struct hns3_adapter *hns, uint32_t *clearval)
620 struct hns3_hw *hw = &hns->hw;
621 enum hns3vf_evt_cause ret;
622 uint32_t cmdq_stat_reg;
623 uint32_t rst_ing_reg;
626 /* Fetch the events from their corresponding regs */
627 cmdq_stat_reg = hns3_read_dev(hw, HNS3_VECTOR0_CMDQ_STAT_REG);
629 if (BIT(HNS3_VECTOR0_RST_INT_B) & cmdq_stat_reg) {
630 rst_ing_reg = hns3_read_dev(hw, HNS3_FUN_RST_ING);
631 hns3_warn(hw, "resetting reg: 0x%x", rst_ing_reg);
632 hns3_atomic_set_bit(HNS3_VF_RESET, &hw->reset.pending);
633 rte_atomic16_set(&hw->reset.disable_cmd, 1);
634 val = hns3_read_dev(hw, HNS3_VF_RST_ING);
635 hns3_write_dev(hw, HNS3_VF_RST_ING, val | HNS3_VF_RST_ING_BIT);
636 val = cmdq_stat_reg & ~BIT(HNS3_VECTOR0_RST_INT_B);
638 hw->reset.stats.global_cnt++;
639 hns3_warn(hw, "Global reset detected, clear reset status");
641 hns3_schedule_delayed_reset(hns);
642 hns3_warn(hw, "Global reset detected, don't clear reset status");
645 ret = HNS3VF_VECTOR0_EVENT_RST;
649 /* Check for vector0 mailbox(=CMDQ RX) event source */
650 if (BIT(HNS3_VECTOR0_RX_CMDQ_INT_B) & cmdq_stat_reg) {
651 val = cmdq_stat_reg & ~BIT(HNS3_VECTOR0_RX_CMDQ_INT_B);
652 ret = HNS3VF_VECTOR0_EVENT_MBX;
657 ret = HNS3VF_VECTOR0_EVENT_OTHER;
665 hns3vf_interrupt_handler(void *param)
667 struct rte_eth_dev *dev = (struct rte_eth_dev *)param;
668 struct hns3_adapter *hns = dev->data->dev_private;
669 struct hns3_hw *hw = &hns->hw;
670 enum hns3vf_evt_cause event_cause;
673 if (hw->irq_thread_id == 0)
674 hw->irq_thread_id = pthread_self();
676 /* Disable interrupt */
677 hns3vf_disable_irq0(hw);
679 /* Read out interrupt causes */
680 event_cause = hns3vf_check_event_cause(hns, &clearval);
682 switch (event_cause) {
683 case HNS3VF_VECTOR0_EVENT_RST:
684 hns3_schedule_reset(hns);
686 case HNS3VF_VECTOR0_EVENT_MBX:
687 hns3_dev_handle_mbx_msg(hw);
693 /* Clear interrupt causes */
694 hns3vf_clear_event_cause(hw, clearval);
696 /* Enable interrupt */
697 hns3vf_enable_irq0(hw);
701 hns3vf_check_tqp_info(struct hns3_hw *hw)
705 tqps_num = hw->tqps_num;
706 if (tqps_num > HNS3_MAX_TQP_NUM_PER_FUNC || tqps_num == 0) {
707 PMD_INIT_LOG(ERR, "Get invalid tqps_num(%u) from PF. valid "
709 tqps_num, HNS3_MAX_TQP_NUM_PER_FUNC);
713 if (hw->rx_buf_len == 0)
714 hw->rx_buf_len = HNS3_DEFAULT_RX_BUF_LEN;
715 hw->alloc_rss_size = RTE_MIN(hw->rss_size_max, hw->tqps_num);
721 hns3vf_get_queue_info(struct hns3_hw *hw)
723 #define HNS3VF_TQPS_RSS_INFO_LEN 6
724 uint8_t resp_msg[HNS3VF_TQPS_RSS_INFO_LEN];
727 ret = hns3_send_mbx_msg(hw, HNS3_MBX_GET_QINFO, 0, NULL, 0, true,
728 resp_msg, HNS3VF_TQPS_RSS_INFO_LEN);
730 PMD_INIT_LOG(ERR, "Failed to get tqp info from PF: %d", ret);
734 memcpy(&hw->tqps_num, &resp_msg[0], sizeof(uint16_t));
735 memcpy(&hw->rss_size_max, &resp_msg[2], sizeof(uint16_t));
736 memcpy(&hw->rx_buf_len, &resp_msg[4], sizeof(uint16_t));
738 return hns3vf_check_tqp_info(hw);
742 hns3vf_get_queue_depth(struct hns3_hw *hw)
744 #define HNS3VF_TQPS_DEPTH_INFO_LEN 4
745 uint8_t resp_msg[HNS3VF_TQPS_DEPTH_INFO_LEN];
748 ret = hns3_send_mbx_msg(hw, HNS3_MBX_GET_QDEPTH, 0, NULL, 0, true,
749 resp_msg, HNS3VF_TQPS_DEPTH_INFO_LEN);
751 PMD_INIT_LOG(ERR, "Failed to get tqp depth info from PF: %d",
756 memcpy(&hw->num_tx_desc, &resp_msg[0], sizeof(uint16_t));
757 memcpy(&hw->num_rx_desc, &resp_msg[2], sizeof(uint16_t));
763 hns3vf_get_tc_info(struct hns3_hw *hw)
768 ret = hns3_send_mbx_msg(hw, HNS3_MBX_GET_TCINFO, 0, NULL, 0,
769 true, &resp_msg, sizeof(resp_msg));
771 hns3_err(hw, "VF request to get TC info from PF failed %d",
776 hw->hw_tc_map = resp_msg;
782 hns3vf_get_configuration(struct hns3_hw *hw)
786 hw->mac.media_type = HNS3_MEDIA_TYPE_NONE;
788 /* Get queue configuration from PF */
789 ret = hns3vf_get_queue_info(hw);
793 /* Get queue depth info from PF */
794 ret = hns3vf_get_queue_depth(hw);
798 /* Get tc configuration from PF */
799 return hns3vf_get_tc_info(hw);
803 hns3vf_set_tc_info(struct hns3_adapter *hns)
805 struct hns3_hw *hw = &hns->hw;
806 uint16_t nb_rx_q = hw->data->nb_rx_queues;
811 for (i = 0; i < HNS3_MAX_TC_NUM; i++)
812 if (hw->hw_tc_map & BIT(i))
815 new_tqps = RTE_MIN(hw->tqps_num, nb_rx_q);
816 hw->alloc_rss_size = RTE_MIN(hw->rss_size_max, new_tqps / hw->num_tc);
817 hw->alloc_tqps = hw->alloc_rss_size * hw->num_tc;
819 hns3_tc_queue_mapping_cfg(hw);
823 hns3vf_request_link_info(struct hns3_hw *hw)
828 if (rte_atomic16_read(&hw->reset.resetting))
830 ret = hns3_send_mbx_msg(hw, HNS3_MBX_GET_LINK_STATUS, 0, NULL, 0, false,
831 &resp_msg, sizeof(resp_msg));
833 hns3_err(hw, "Failed to fetch link status from PF: %d", ret);
837 hns3vf_vlan_filter_configure(struct hns3_adapter *hns, uint16_t vlan_id, int on)
839 #define HNS3VF_VLAN_MBX_MSG_LEN 5
840 struct hns3_hw *hw = &hns->hw;
841 uint8_t msg_data[HNS3VF_VLAN_MBX_MSG_LEN];
842 uint16_t proto = htons(RTE_ETHER_TYPE_VLAN);
843 uint8_t is_kill = on ? 0 : 1;
845 msg_data[0] = is_kill;
846 memcpy(&msg_data[1], &vlan_id, sizeof(vlan_id));
847 memcpy(&msg_data[3], &proto, sizeof(proto));
849 return hns3_send_mbx_msg(hw, HNS3_MBX_SET_VLAN, HNS3_MBX_VLAN_FILTER,
850 msg_data, HNS3VF_VLAN_MBX_MSG_LEN, true, NULL,
855 hns3vf_vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on)
857 struct hns3_adapter *hns = dev->data->dev_private;
858 struct hns3_hw *hw = &hns->hw;
861 if (rte_atomic16_read(&hw->reset.resetting)) {
863 "vf set vlan id failed during resetting, vlan_id =%u",
867 rte_spinlock_lock(&hw->lock);
868 ret = hns3vf_vlan_filter_configure(hns, vlan_id, on);
869 rte_spinlock_unlock(&hw->lock);
871 hns3_err(hw, "vf set vlan id failed, vlan_id =%u, ret =%d",
878 hns3vf_en_hw_strip_rxvtag(struct hns3_hw *hw, bool enable)
883 msg_data = enable ? 1 : 0;
884 ret = hns3_send_mbx_msg(hw, HNS3_MBX_SET_VLAN, HNS3_MBX_VLAN_RX_OFF_CFG,
885 &msg_data, sizeof(msg_data), false, NULL, 0);
887 hns3_err(hw, "vf enable strip failed, ret =%d", ret);
893 hns3vf_vlan_offload_set(struct rte_eth_dev *dev, int mask)
895 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
896 struct rte_eth_conf *dev_conf = &dev->data->dev_conf;
897 unsigned int tmp_mask;
899 tmp_mask = (unsigned int)mask;
900 /* Vlan stripping setting */
901 if (tmp_mask & ETH_VLAN_STRIP_MASK) {
902 rte_spinlock_lock(&hw->lock);
903 /* Enable or disable VLAN stripping */
904 if (dev_conf->rxmode.offloads & DEV_RX_OFFLOAD_VLAN_STRIP)
905 hns3vf_en_hw_strip_rxvtag(hw, true);
907 hns3vf_en_hw_strip_rxvtag(hw, false);
908 rte_spinlock_unlock(&hw->lock);
915 hns3vf_handle_all_vlan_table(struct hns3_adapter *hns, int on)
917 struct rte_vlan_filter_conf *vfc;
918 struct hns3_hw *hw = &hns->hw;
925 vfc = &hw->data->vlan_filter_conf;
926 for (i = 0; i < RTE_DIM(vfc->ids); i++) {
927 if (vfc->ids[i] == 0)
932 * 64 means the num bits of ids, one bit corresponds to
936 /* count trailing zeroes */
937 vbit = ~ids & (ids - 1);
938 /* clear least significant bit set */
939 ids ^= (ids ^ (ids - 1)) ^ vbit;
944 ret = hns3vf_vlan_filter_configure(hns, vlan_id, on);
947 "VF handle vlan table failed, ret =%d, on = %d",
958 hns3vf_remove_all_vlan_table(struct hns3_adapter *hns)
960 return hns3vf_handle_all_vlan_table(hns, 0);
964 hns3vf_restore_vlan_conf(struct hns3_adapter *hns)
966 struct hns3_hw *hw = &hns->hw;
967 struct rte_eth_conf *dev_conf;
971 dev_conf = &hw->data->dev_conf;
972 en = dev_conf->rxmode.offloads & DEV_RX_OFFLOAD_VLAN_STRIP ? true
974 ret = hns3vf_en_hw_strip_rxvtag(hw, en);
976 hns3_err(hw, "VF restore vlan conf fail, en =%d, ret =%d", en,
982 hns3vf_dev_configure_vlan(struct rte_eth_dev *dev)
984 struct hns3_adapter *hns = dev->data->dev_private;
985 struct rte_eth_dev_data *data = dev->data;
986 struct hns3_hw *hw = &hns->hw;
989 if (data->dev_conf.txmode.hw_vlan_reject_tagged ||
990 data->dev_conf.txmode.hw_vlan_reject_untagged ||
991 data->dev_conf.txmode.hw_vlan_insert_pvid) {
992 hns3_warn(hw, "hw_vlan_reject_tagged, hw_vlan_reject_untagged "
993 "or hw_vlan_insert_pvid is not support!");
996 /* Apply vlan offload setting */
997 ret = hns3vf_vlan_offload_set(dev, ETH_VLAN_STRIP_MASK);
999 hns3_err(hw, "dev config vlan offload failed, ret =%d", ret);
1005 hns3vf_set_alive(struct hns3_hw *hw, bool alive)
1009 msg_data = alive ? 1 : 0;
1010 return hns3_send_mbx_msg(hw, HNS3_MBX_SET_ALIVE, 0, &msg_data,
1011 sizeof(msg_data), false, NULL, 0);
1015 hns3vf_keep_alive_handler(void *param)
1017 struct rte_eth_dev *eth_dev = (struct rte_eth_dev *)param;
1018 struct hns3_adapter *hns = eth_dev->data->dev_private;
1019 struct hns3_hw *hw = &hns->hw;
1023 ret = hns3_send_mbx_msg(hw, HNS3_MBX_KEEP_ALIVE, 0, NULL, 0,
1024 false, &respmsg, sizeof(uint8_t));
1026 hns3_err(hw, "VF sends keeping alive cmd failed(=%d)",
1029 rte_eal_alarm_set(HNS3VF_KEEP_ALIVE_INTERVAL, hns3vf_keep_alive_handler,
1034 hns3vf_service_handler(void *param)
1036 struct rte_eth_dev *eth_dev = (struct rte_eth_dev *)param;
1037 struct hns3_adapter *hns = eth_dev->data->dev_private;
1038 struct hns3_hw *hw = &hns->hw;
1041 * The query link status and reset processing are executed in the
1042 * interrupt thread.When the IMP reset occurs, IMP will not respond,
1043 * and the query operation will time out after 30ms. In the case of
1044 * multiple PF/VFs, each query failure timeout causes the IMP reset
1045 * interrupt to fail to respond within 100ms.
1046 * Before querying the link status, check whether there is a reset
1047 * pending, and if so, abandon the query.
1049 if (!hns3vf_is_reset_pending(hns))
1050 hns3vf_request_link_info(hw);
1052 hns3_warn(hw, "Cancel the query when reset is pending");
1054 rte_eal_alarm_set(HNS3VF_SERVICE_INTERVAL, hns3vf_service_handler,
1059 hns3vf_init_hardware(struct hns3_adapter *hns)
1061 struct hns3_hw *hw = &hns->hw;
1062 uint16_t mtu = hw->data->mtu;
1065 ret = hns3vf_set_promisc_mode(hw, true);
1069 ret = hns3vf_config_mtu(hw, mtu);
1071 goto err_init_hardware;
1073 ret = hns3vf_vlan_filter_configure(hns, 0, 1);
1075 PMD_INIT_LOG(ERR, "Failed to initialize VLAN config: %d", ret);
1076 goto err_init_hardware;
1079 ret = hns3_config_gro(hw, false);
1081 PMD_INIT_LOG(ERR, "Failed to config gro: %d", ret);
1082 goto err_init_hardware;
1085 ret = hns3vf_set_alive(hw, true);
1087 PMD_INIT_LOG(ERR, "Failed to VF send alive to PF: %d", ret);
1088 goto err_init_hardware;
1091 hns3vf_request_link_info(hw);
1095 (void)hns3vf_set_promisc_mode(hw, false);
1100 hns3vf_init_vf(struct rte_eth_dev *eth_dev)
1102 struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(eth_dev);
1103 struct hns3_adapter *hns = eth_dev->data->dev_private;
1104 struct hns3_hw *hw = &hns->hw;
1107 PMD_INIT_FUNC_TRACE();
1109 /* Get hardware io base address from pcie BAR2 IO space */
1110 hw->io_base = pci_dev->mem_resource[2].addr;
1112 /* Firmware command queue initialize */
1113 ret = hns3_cmd_init_queue(hw);
1115 PMD_INIT_LOG(ERR, "Failed to init cmd queue: %d", ret);
1116 goto err_cmd_init_queue;
1119 /* Firmware command initialize */
1120 ret = hns3_cmd_init(hw);
1122 PMD_INIT_LOG(ERR, "Failed to init cmd: %d", ret);
1126 rte_spinlock_init(&hw->mbx_resp.lock);
1128 hns3vf_clear_event_cause(hw, 0);
1130 ret = rte_intr_callback_register(&pci_dev->intr_handle,
1131 hns3vf_interrupt_handler, eth_dev);
1133 PMD_INIT_LOG(ERR, "Failed to register intr: %d", ret);
1134 goto err_intr_callback_register;
1137 /* Enable interrupt */
1138 rte_intr_enable(&pci_dev->intr_handle);
1139 hns3vf_enable_irq0(hw);
1141 /* Get configuration from PF */
1142 ret = hns3vf_get_configuration(hw);
1144 PMD_INIT_LOG(ERR, "Failed to fetch configuration: %d", ret);
1145 goto err_get_config;
1148 rte_eth_random_addr(hw->mac.mac_addr); /* Generate a random mac addr */
1150 ret = hns3vf_init_hardware(hns);
1152 goto err_get_config;
1154 hns3_set_default_rss_args(hw);
1156 (void)hns3_stats_reset(eth_dev);
1160 hns3vf_disable_irq0(hw);
1161 rte_intr_disable(&pci_dev->intr_handle);
1162 hns3_intr_unregister(&pci_dev->intr_handle, hns3vf_interrupt_handler,
1164 err_intr_callback_register:
1165 hns3_cmd_uninit(hw);
1168 hns3_cmd_destroy_queue(hw);
1177 hns3vf_uninit_vf(struct rte_eth_dev *eth_dev)
1179 struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(eth_dev);
1180 struct hns3_adapter *hns = eth_dev->data->dev_private;
1181 struct hns3_hw *hw = &hns->hw;
1183 PMD_INIT_FUNC_TRACE();
1185 hns3_rss_uninit(hns);
1186 (void)hns3vf_set_alive(hw, false);
1187 (void)hns3vf_set_promisc_mode(hw, false);
1188 hns3vf_disable_irq0(hw);
1189 rte_intr_disable(&pci_dev->intr_handle);
1190 hns3_intr_unregister(&pci_dev->intr_handle, hns3vf_interrupt_handler,
1192 hns3_cmd_uninit(hw);
1193 hns3_cmd_destroy_queue(hw);
1198 hns3vf_do_stop(struct hns3_adapter *hns)
1200 struct hns3_hw *hw = &hns->hw;
1203 hw->mac.link_status = ETH_LINK_DOWN;
1205 if (rte_atomic16_read(&hw->reset.disable_cmd) == 0) {
1206 hns3vf_configure_mac_addr(hns, true);
1209 reset_queue = false;
1210 return hns3_stop_queues(hns, reset_queue);
1214 hns3vf_dev_stop(struct rte_eth_dev *eth_dev)
1216 struct hns3_adapter *hns = eth_dev->data->dev_private;
1217 struct hns3_hw *hw = &hns->hw;
1219 PMD_INIT_FUNC_TRACE();
1221 hw->adapter_state = HNS3_NIC_STOPPING;
1222 hns3_set_rxtx_function(eth_dev);
1224 /* Disable datapath on secondary process. */
1225 hns3_mp_req_stop_rxtx(eth_dev);
1226 /* Prevent crashes when queues are still in use. */
1227 rte_delay_ms(hw->tqps_num);
1229 rte_spinlock_lock(&hw->lock);
1230 if (rte_atomic16_read(&hw->reset.resetting) == 0) {
1231 hns3vf_do_stop(hns);
1232 hns3_dev_release_mbufs(hns);
1233 hw->adapter_state = HNS3_NIC_CONFIGURED;
1235 rte_spinlock_unlock(&hw->lock);
1239 hns3vf_dev_close(struct rte_eth_dev *eth_dev)
1241 struct hns3_adapter *hns = eth_dev->data->dev_private;
1242 struct hns3_hw *hw = &hns->hw;
1244 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
1247 if (hw->adapter_state == HNS3_NIC_STARTED)
1248 hns3vf_dev_stop(eth_dev);
1250 hw->adapter_state = HNS3_NIC_CLOSING;
1251 hns3_reset_abort(hns);
1252 hw->adapter_state = HNS3_NIC_CLOSED;
1253 rte_eal_alarm_cancel(hns3vf_keep_alive_handler, eth_dev);
1254 rte_eal_alarm_cancel(hns3vf_service_handler, eth_dev);
1255 hns3vf_configure_all_mc_mac_addr(hns, true);
1256 hns3vf_remove_all_vlan_table(hns);
1257 hns3vf_uninit_vf(eth_dev);
1258 hns3_free_all_queues(eth_dev);
1259 rte_free(hw->reset.wait_data);
1260 rte_free(eth_dev->process_private);
1261 eth_dev->process_private = NULL;
1262 hns3_mp_uninit_primary();
1263 hns3_warn(hw, "Close port %d finished", hw->data->port_id);
1267 hns3vf_dev_link_update(struct rte_eth_dev *eth_dev,
1268 __rte_unused int wait_to_complete)
1270 struct hns3_adapter *hns = eth_dev->data->dev_private;
1271 struct hns3_hw *hw = &hns->hw;
1272 struct hns3_mac *mac = &hw->mac;
1273 struct rte_eth_link new_link;
1275 hns3vf_request_link_info(hw);
1277 memset(&new_link, 0, sizeof(new_link));
1278 switch (mac->link_speed) {
1279 case ETH_SPEED_NUM_10M:
1280 case ETH_SPEED_NUM_100M:
1281 case ETH_SPEED_NUM_1G:
1282 case ETH_SPEED_NUM_10G:
1283 case ETH_SPEED_NUM_25G:
1284 case ETH_SPEED_NUM_40G:
1285 case ETH_SPEED_NUM_50G:
1286 case ETH_SPEED_NUM_100G:
1287 new_link.link_speed = mac->link_speed;
1290 new_link.link_speed = ETH_SPEED_NUM_100M;
1294 new_link.link_duplex = mac->link_duplex;
1295 new_link.link_status = mac->link_status ? ETH_LINK_UP : ETH_LINK_DOWN;
1296 new_link.link_autoneg =
1297 !(eth_dev->data->dev_conf.link_speeds & ETH_LINK_SPEED_FIXED);
1299 return rte_eth_linkstatus_set(eth_dev, &new_link);
1303 hns3vf_do_start(struct hns3_adapter *hns, bool reset_queue)
1305 struct hns3_hw *hw = &hns->hw;
1308 hns3vf_set_tc_info(hns);
1310 ret = hns3_start_queues(hns, reset_queue);
1312 hns3_err(hw, "Failed to start queues: %d", ret);
1320 hns3vf_dev_start(struct rte_eth_dev *eth_dev)
1322 struct hns3_adapter *hns = eth_dev->data->dev_private;
1323 struct hns3_hw *hw = &hns->hw;
1326 PMD_INIT_FUNC_TRACE();
1327 if (rte_atomic16_read(&hw->reset.resetting))
1329 rte_spinlock_lock(&hw->lock);
1330 hw->adapter_state = HNS3_NIC_STARTING;
1331 ret = hns3vf_do_start(hns, true);
1333 hw->adapter_state = HNS3_NIC_CONFIGURED;
1334 rte_spinlock_unlock(&hw->lock);
1337 hw->adapter_state = HNS3_NIC_STARTED;
1338 rte_spinlock_unlock(&hw->lock);
1339 hns3_set_rxtx_function(eth_dev);
1340 hns3_mp_req_start_rxtx(eth_dev);
1345 is_vf_reset_done(struct hns3_hw *hw)
1347 #define HNS3_FUN_RST_ING_BITS \
1348 (BIT(HNS3_VECTOR0_GLOBALRESET_INT_B) | \
1349 BIT(HNS3_VECTOR0_CORERESET_INT_B) | \
1350 BIT(HNS3_VECTOR0_IMPRESET_INT_B) | \
1351 BIT(HNS3_VECTOR0_FUNCRESET_INT_B))
1355 if (hw->reset.level == HNS3_VF_RESET) {
1356 val = hns3_read_dev(hw, HNS3_VF_RST_ING);
1357 if (val & HNS3_VF_RST_ING_BIT)
1360 val = hns3_read_dev(hw, HNS3_FUN_RST_ING);
1361 if (val & HNS3_FUN_RST_ING_BITS)
1368 hns3vf_is_reset_pending(struct hns3_adapter *hns)
1370 struct hns3_hw *hw = &hns->hw;
1371 enum hns3_reset_level reset;
1373 hns3vf_check_event_cause(hns, NULL);
1374 reset = hns3vf_get_reset_level(hw, &hw->reset.pending);
1375 if (hw->reset.level != HNS3_NONE_RESET && hw->reset.level < reset) {
1376 hns3_warn(hw, "High level reset %d is pending", reset);
1383 hns3vf_wait_hardware_ready(struct hns3_adapter *hns)
1385 struct hns3_hw *hw = &hns->hw;
1386 struct hns3_wait_data *wait_data = hw->reset.wait_data;
1389 if (wait_data->result == HNS3_WAIT_SUCCESS) {
1391 * After vf reset is ready, the PF may not have completed
1392 * the reset processing. The vf sending mbox to PF may fail
1393 * during the pf reset, so it is better to add extra delay.
1395 if (hw->reset.level == HNS3_VF_FUNC_RESET ||
1396 hw->reset.level == HNS3_FLR_RESET)
1398 /* Reset retry process, no need to add extra delay. */
1399 if (hw->reset.attempts)
1401 if (wait_data->check_completion == NULL)
1404 wait_data->check_completion = NULL;
1405 wait_data->interval = 1 * MSEC_PER_SEC * USEC_PER_MSEC;
1406 wait_data->count = 1;
1407 wait_data->result = HNS3_WAIT_REQUEST;
1408 rte_eal_alarm_set(wait_data->interval, hns3_wait_callback,
1410 hns3_warn(hw, "hardware is ready, delay 1 sec for PF reset complete");
1412 } else if (wait_data->result == HNS3_WAIT_TIMEOUT) {
1413 gettimeofday(&tv, NULL);
1414 hns3_warn(hw, "Reset step4 hardware not ready after reset time=%ld.%.6ld",
1415 tv.tv_sec, tv.tv_usec);
1417 } else if (wait_data->result == HNS3_WAIT_REQUEST)
1420 wait_data->hns = hns;
1421 wait_data->check_completion = is_vf_reset_done;
1422 wait_data->end_ms = (uint64_t)HNS3VF_RESET_WAIT_CNT *
1423 HNS3VF_RESET_WAIT_MS + get_timeofday_ms();
1424 wait_data->interval = HNS3VF_RESET_WAIT_MS * USEC_PER_MSEC;
1425 wait_data->count = HNS3VF_RESET_WAIT_CNT;
1426 wait_data->result = HNS3_WAIT_REQUEST;
1427 rte_eal_alarm_set(wait_data->interval, hns3_wait_callback, wait_data);
1432 hns3vf_prepare_reset(struct hns3_adapter *hns)
1434 struct hns3_hw *hw = &hns->hw;
1437 if (hw->reset.level == HNS3_VF_FUNC_RESET) {
1438 ret = hns3_send_mbx_msg(hw, HNS3_MBX_RESET, 0, NULL,
1441 rte_atomic16_set(&hw->reset.disable_cmd, 1);
1447 hns3vf_stop_service(struct hns3_adapter *hns)
1449 struct hns3_hw *hw = &hns->hw;
1450 struct rte_eth_dev *eth_dev;
1452 eth_dev = &rte_eth_devices[hw->data->port_id];
1453 rte_eal_alarm_cancel(hns3vf_service_handler, eth_dev);
1454 hw->mac.link_status = ETH_LINK_DOWN;
1456 hns3_set_rxtx_function(eth_dev);
1458 /* Disable datapath on secondary process. */
1459 hns3_mp_req_stop_rxtx(eth_dev);
1460 rte_delay_ms(hw->tqps_num);
1462 rte_spinlock_lock(&hw->lock);
1463 if (hw->adapter_state == HNS3_NIC_STARTED ||
1464 hw->adapter_state == HNS3_NIC_STOPPING) {
1465 hns3vf_do_stop(hns);
1466 hw->reset.mbuf_deferred_free = true;
1468 hw->reset.mbuf_deferred_free = false;
1471 * It is cumbersome for hardware to pick-and-choose entries for deletion
1472 * from table space. Hence, for function reset software intervention is
1473 * required to delete the entries.
1475 if (rte_atomic16_read(&hw->reset.disable_cmd) == 0)
1476 hns3vf_configure_all_mc_mac_addr(hns, true);
1477 rte_spinlock_unlock(&hw->lock);
1483 hns3vf_start_service(struct hns3_adapter *hns)
1485 struct hns3_hw *hw = &hns->hw;
1486 struct rte_eth_dev *eth_dev;
1488 eth_dev = &rte_eth_devices[hw->data->port_id];
1489 hns3_set_rxtx_function(eth_dev);
1490 hns3_mp_req_start_rxtx(eth_dev);
1492 hns3vf_service_handler(eth_dev);
1497 hns3vf_restore_conf(struct hns3_adapter *hns)
1499 struct hns3_hw *hw = &hns->hw;
1502 ret = hns3vf_configure_mac_addr(hns, false);
1506 ret = hns3vf_configure_all_mc_mac_addr(hns, false);
1510 ret = hns3vf_restore_vlan_conf(hns);
1512 goto err_vlan_table;
1514 if (hw->adapter_state == HNS3_NIC_STARTED) {
1515 ret = hns3vf_do_start(hns, false);
1517 goto err_vlan_table;
1518 hns3_info(hw, "hns3vf dev restart successful!");
1519 } else if (hw->adapter_state == HNS3_NIC_STOPPING)
1520 hw->adapter_state = HNS3_NIC_CONFIGURED;
1524 hns3vf_configure_all_mc_mac_addr(hns, true);
1526 hns3vf_configure_mac_addr(hns, true);
1530 static enum hns3_reset_level
1531 hns3vf_get_reset_level(struct hns3_hw *hw, uint64_t *levels)
1533 enum hns3_reset_level reset_level;
1535 /* return the highest priority reset level amongst all */
1536 if (hns3_atomic_test_bit(HNS3_VF_RESET, levels))
1537 reset_level = HNS3_VF_RESET;
1538 else if (hns3_atomic_test_bit(HNS3_VF_FULL_RESET, levels))
1539 reset_level = HNS3_VF_FULL_RESET;
1540 else if (hns3_atomic_test_bit(HNS3_VF_PF_FUNC_RESET, levels))
1541 reset_level = HNS3_VF_PF_FUNC_RESET;
1542 else if (hns3_atomic_test_bit(HNS3_VF_FUNC_RESET, levels))
1543 reset_level = HNS3_VF_FUNC_RESET;
1544 else if (hns3_atomic_test_bit(HNS3_FLR_RESET, levels))
1545 reset_level = HNS3_FLR_RESET;
1547 reset_level = HNS3_NONE_RESET;
1549 if (hw->reset.level != HNS3_NONE_RESET && reset_level < hw->reset.level)
1550 return HNS3_NONE_RESET;
1556 hns3vf_reset_service(void *param)
1558 struct hns3_adapter *hns = (struct hns3_adapter *)param;
1559 struct hns3_hw *hw = &hns->hw;
1560 enum hns3_reset_level reset_level;
1561 struct timeval tv_delta;
1562 struct timeval tv_start;
1567 * The interrupt is not triggered within the delay time.
1568 * The interrupt may have been lost. It is necessary to handle
1569 * the interrupt to recover from the error.
1571 if (rte_atomic16_read(&hns->hw.reset.schedule) == SCHEDULE_DEFERRED) {
1572 rte_atomic16_set(&hns->hw.reset.schedule, SCHEDULE_REQUESTED);
1573 hns3_err(hw, "Handling interrupts in delayed tasks");
1574 hns3vf_interrupt_handler(&rte_eth_devices[hw->data->port_id]);
1575 reset_level = hns3vf_get_reset_level(hw, &hw->reset.pending);
1576 if (reset_level == HNS3_NONE_RESET) {
1577 hns3_err(hw, "No reset level is set, try global reset");
1578 hns3_atomic_set_bit(HNS3_VF_RESET, &hw->reset.pending);
1581 rte_atomic16_set(&hns->hw.reset.schedule, SCHEDULE_NONE);
1584 * Hardware reset has been notified, we now have to poll & check if
1585 * hardware has actually completed the reset sequence.
1587 reset_level = hns3vf_get_reset_level(hw, &hw->reset.pending);
1588 if (reset_level != HNS3_NONE_RESET) {
1589 gettimeofday(&tv_start, NULL);
1590 hns3_reset_process(hns, reset_level);
1591 gettimeofday(&tv, NULL);
1592 timersub(&tv, &tv_start, &tv_delta);
1593 msec = tv_delta.tv_sec * MSEC_PER_SEC +
1594 tv_delta.tv_usec / USEC_PER_MSEC;
1595 if (msec > HNS3_RESET_PROCESS_MS)
1596 hns3_err(hw, "%d handle long time delta %" PRIx64
1597 " ms time=%ld.%.6ld",
1598 hw->reset.level, msec, tv.tv_sec, tv.tv_usec);
1603 hns3vf_reinit_dev(struct hns3_adapter *hns)
1605 struct rte_eth_dev *eth_dev = &rte_eth_devices[hns->hw.data->port_id];
1606 struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(eth_dev);
1607 struct hns3_hw *hw = &hns->hw;
1610 if (hw->reset.level == HNS3_VF_FULL_RESET) {
1611 rte_intr_disable(&pci_dev->intr_handle);
1612 hns3vf_set_bus_master(pci_dev, true);
1615 /* Firmware command initialize */
1616 ret = hns3_cmd_init(hw);
1618 hns3_err(hw, "Failed to init cmd: %d", ret);
1622 if (hw->reset.level == HNS3_VF_FULL_RESET) {
1624 * UIO enables msix by writing the pcie configuration space
1625 * vfio_pci enables msix in rte_intr_enable.
1627 if (pci_dev->kdrv == RTE_KDRV_IGB_UIO ||
1628 pci_dev->kdrv == RTE_KDRV_UIO_GENERIC) {
1629 if (hns3vf_enable_msix(pci_dev, true))
1630 hns3_err(hw, "Failed to enable msix");
1633 rte_intr_enable(&pci_dev->intr_handle);
1636 ret = hns3_reset_all_queues(hns);
1638 hns3_err(hw, "Failed to reset all queues: %d", ret);
1642 ret = hns3vf_init_hardware(hns);
1644 hns3_err(hw, "Failed to init hardware: %d", ret);
1651 hns3vf_set_bus_master(pci_dev, false);
1653 hns3_cmd_uninit(hw);
1657 static const struct eth_dev_ops hns3vf_eth_dev_ops = {
1658 .dev_start = hns3vf_dev_start,
1659 .dev_stop = hns3vf_dev_stop,
1660 .dev_close = hns3vf_dev_close,
1661 .mtu_set = hns3vf_dev_mtu_set,
1662 .stats_get = hns3_stats_get,
1663 .stats_reset = hns3_stats_reset,
1664 .xstats_get = hns3_dev_xstats_get,
1665 .xstats_get_names = hns3_dev_xstats_get_names,
1666 .xstats_reset = hns3_dev_xstats_reset,
1667 .xstats_get_by_id = hns3_dev_xstats_get_by_id,
1668 .xstats_get_names_by_id = hns3_dev_xstats_get_names_by_id,
1669 .dev_infos_get = hns3vf_dev_infos_get,
1670 .rx_queue_setup = hns3_rx_queue_setup,
1671 .tx_queue_setup = hns3_tx_queue_setup,
1672 .rx_queue_release = hns3_dev_rx_queue_release,
1673 .tx_queue_release = hns3_dev_tx_queue_release,
1674 .dev_configure = hns3vf_dev_configure,
1675 .mac_addr_add = hns3vf_add_mac_addr,
1676 .mac_addr_remove = hns3vf_remove_mac_addr,
1677 .mac_addr_set = hns3vf_set_default_mac_addr,
1678 .set_mc_addr_list = hns3vf_set_mc_mac_addr_list,
1679 .link_update = hns3vf_dev_link_update,
1680 .rss_hash_update = hns3_dev_rss_hash_update,
1681 .rss_hash_conf_get = hns3_dev_rss_hash_conf_get,
1682 .reta_update = hns3_dev_rss_reta_update,
1683 .reta_query = hns3_dev_rss_reta_query,
1684 .filter_ctrl = hns3_dev_filter_ctrl,
1685 .vlan_filter_set = hns3vf_vlan_filter_set,
1686 .vlan_offload_set = hns3vf_vlan_offload_set,
1687 .get_reg = hns3_get_regs,
1688 .dev_supported_ptypes_get = hns3_dev_supported_ptypes_get,
1691 static const struct hns3_reset_ops hns3vf_reset_ops = {
1692 .reset_service = hns3vf_reset_service,
1693 .stop_service = hns3vf_stop_service,
1694 .prepare_reset = hns3vf_prepare_reset,
1695 .wait_hardware_ready = hns3vf_wait_hardware_ready,
1696 .reinit_dev = hns3vf_reinit_dev,
1697 .restore_conf = hns3vf_restore_conf,
1698 .start_service = hns3vf_start_service,
1702 hns3vf_dev_init(struct rte_eth_dev *eth_dev)
1704 struct hns3_adapter *hns = eth_dev->data->dev_private;
1705 struct hns3_hw *hw = &hns->hw;
1708 PMD_INIT_FUNC_TRACE();
1710 eth_dev->process_private = (struct hns3_process_private *)
1711 rte_zmalloc_socket("hns3_filter_list",
1712 sizeof(struct hns3_process_private),
1713 RTE_CACHE_LINE_SIZE, eth_dev->device->numa_node);
1714 if (eth_dev->process_private == NULL) {
1715 PMD_INIT_LOG(ERR, "Failed to alloc memory for process private");
1719 /* initialize flow filter lists */
1720 hns3_filterlist_init(eth_dev);
1722 hns3_set_rxtx_function(eth_dev);
1723 eth_dev->dev_ops = &hns3vf_eth_dev_ops;
1724 if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
1725 hns3_mp_init_secondary();
1726 hw->secondary_cnt++;
1730 hns3_mp_init_primary();
1732 hw->adapter_state = HNS3_NIC_UNINITIALIZED;
1734 hw->data = eth_dev->data;
1736 ret = hns3_reset_init(hw);
1738 goto err_init_reset;
1739 hw->reset.ops = &hns3vf_reset_ops;
1741 ret = hns3vf_init_vf(eth_dev);
1743 PMD_INIT_LOG(ERR, "Failed to init vf: %d", ret);
1747 /* Allocate memory for storing MAC addresses */
1748 eth_dev->data->mac_addrs = rte_zmalloc("hns3vf-mac",
1749 sizeof(struct rte_ether_addr) *
1750 HNS3_VF_UC_MACADDR_NUM, 0);
1751 if (eth_dev->data->mac_addrs == NULL) {
1752 PMD_INIT_LOG(ERR, "Failed to allocate %zx bytes needed "
1753 "to store MAC addresses",
1754 sizeof(struct rte_ether_addr) *
1755 HNS3_VF_UC_MACADDR_NUM);
1757 goto err_rte_zmalloc;
1760 rte_ether_addr_copy((struct rte_ether_addr *)hw->mac.mac_addr,
1761 ð_dev->data->mac_addrs[0]);
1762 hw->adapter_state = HNS3_NIC_INITIALIZED;
1764 * Pass the information to the rte_eth_dev_close() that it should also
1765 * release the private port resources.
1767 eth_dev->data->dev_flags |= RTE_ETH_DEV_CLOSE_REMOVE;
1769 if (rte_atomic16_read(&hns->hw.reset.schedule) == SCHEDULE_PENDING) {
1770 hns3_err(hw, "Reschedule reset service after dev_init");
1771 hns3_schedule_reset(hns);
1773 /* IMP will wait ready flag before reset */
1774 hns3_notify_reset_ready(hw, false);
1776 rte_eal_alarm_set(HNS3VF_KEEP_ALIVE_INTERVAL, hns3vf_keep_alive_handler,
1778 rte_eal_alarm_set(HNS3VF_SERVICE_INTERVAL, hns3vf_service_handler,
1783 hns3vf_uninit_vf(eth_dev);
1786 rte_free(hw->reset.wait_data);
1789 eth_dev->dev_ops = NULL;
1790 eth_dev->rx_pkt_burst = NULL;
1791 eth_dev->tx_pkt_burst = NULL;
1792 eth_dev->tx_pkt_prepare = NULL;
1793 rte_free(eth_dev->process_private);
1794 eth_dev->process_private = NULL;
1800 hns3vf_dev_uninit(struct rte_eth_dev *eth_dev)
1802 struct hns3_adapter *hns = eth_dev->data->dev_private;
1803 struct hns3_hw *hw = &hns->hw;
1805 PMD_INIT_FUNC_TRACE();
1807 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
1810 eth_dev->dev_ops = NULL;
1811 eth_dev->rx_pkt_burst = NULL;
1812 eth_dev->tx_pkt_burst = NULL;
1813 eth_dev->tx_pkt_prepare = NULL;
1815 if (hw->adapter_state < HNS3_NIC_CLOSING)
1816 hns3vf_dev_close(eth_dev);
1818 hw->adapter_state = HNS3_NIC_REMOVED;
1823 eth_hns3vf_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
1824 struct rte_pci_device *pci_dev)
1826 return rte_eth_dev_pci_generic_probe(pci_dev,
1827 sizeof(struct hns3_adapter),
1832 eth_hns3vf_pci_remove(struct rte_pci_device *pci_dev)
1834 return rte_eth_dev_pci_generic_remove(pci_dev, hns3vf_dev_uninit);
1837 static const struct rte_pci_id pci_id_hns3vf_map[] = {
1838 { RTE_PCI_DEVICE(PCI_VENDOR_ID_HUAWEI, HNS3_DEV_ID_100G_VF) },
1839 { RTE_PCI_DEVICE(PCI_VENDOR_ID_HUAWEI, HNS3_DEV_ID_100G_RDMA_PFC_VF) },
1840 { .vendor_id = 0, /* sentinel */ },
1843 static struct rte_pci_driver rte_hns3vf_pmd = {
1844 .id_table = pci_id_hns3vf_map,
1845 .drv_flags = RTE_PCI_DRV_NEED_MAPPING,
1846 .probe = eth_hns3vf_pci_probe,
1847 .remove = eth_hns3vf_pci_remove,
1850 RTE_PMD_REGISTER_PCI(net_hns3_vf, rte_hns3vf_pmd);
1851 RTE_PMD_REGISTER_PCI_TABLE(net_hns3_vf, pci_id_hns3vf_map);
1852 RTE_PMD_REGISTER_KMOD_DEP(net_hns3_vf, "* igb_uio | vfio-pci");