net/hns3: support Rx interrupt
[dpdk.git] / drivers / net / hns3 / hns3_ethdev_vf.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2018-2019 Hisilicon Limited.
3  */
4
5 #include <errno.h>
6 #include <stdio.h>
7 #include <stdbool.h>
8 #include <string.h>
9 #include <inttypes.h>
10 #include <unistd.h>
11 #include <arpa/inet.h>
12 #include <linux/pci_regs.h>
13
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>
20 #include <rte_dev.h>
21 #include <rte_eal.h>
22 #include <rte_ether.h>
23 #include <rte_ethdev_driver.h>
24 #include <rte_ethdev_pci.h>
25 #include <rte_interrupts.h>
26 #include <rte_io.h>
27 #include <rte_log.h>
28 #include <rte_pci.h>
29 #include <rte_vfio.h>
30
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"
36 #include "hns3_dcb.h"
37 #include "hns3_mp.h"
38
39 #define HNS3VF_KEEP_ALIVE_INTERVAL      2000000 /* us */
40 #define HNS3VF_SERVICE_INTERVAL         1000000 /* us */
41
42 #define HNS3VF_RESET_WAIT_MS    20
43 #define HNS3VF_RESET_WAIT_CNT   2000
44
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
50
51 enum hns3vf_evt_cause {
52         HNS3VF_VECTOR0_EVENT_RST,
53         HNS3VF_VECTOR0_EVENT_MBX,
54         HNS3VF_VECTOR0_EVENT_OTHER,
55 };
56
57 static enum hns3_reset_level hns3vf_get_reset_level(struct hns3_hw *hw,
58                                                     uint64_t *levels);
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);
61
62 /* set PCI bus mastering */
63 static void
64 hns3vf_set_bus_master(const struct rte_pci_device *device, bool op)
65 {
66         uint16_t reg;
67
68         rte_pci_read_config(device, &reg, sizeof(reg), PCI_COMMAND);
69
70         if (op)
71                 /* set the master bit */
72                 reg |= PCI_COMMAND_MASTER;
73         else
74                 reg &= ~(PCI_COMMAND_MASTER);
75
76         rte_pci_write_config(device, &reg, sizeof(reg), PCI_COMMAND);
77 }
78
79 /**
80  * hns3vf_find_pci_capability - lookup a capability in the PCI capability list
81  * @cap: the capability
82  *
83  * Return the address of the given capability within the PCI capability list.
84  */
85 static int
86 hns3vf_find_pci_capability(const struct rte_pci_device *device, int cap)
87 {
88 #define MAX_PCIE_CAPABILITY 48
89         uint16_t status;
90         uint8_t pos;
91         uint8_t id;
92         int ttl;
93
94         rte_pci_read_config(device, &status, sizeof(status), PCI_STATUS);
95         if (!(status & PCI_STATUS_CAP_LIST))
96                 return 0;
97
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));
103
104                 if (id == 0xFF)
105                         break;
106
107                 if (id == cap)
108                         return (int)pos;
109
110                 rte_pci_read_config(device, &pos, sizeof(pos),
111                                     (pos + PCI_CAP_LIST_NEXT));
112         }
113         return 0;
114 }
115
116 static int
117 hns3vf_enable_msix(const struct rte_pci_device *device, bool op)
118 {
119         uint16_t control;
120         int pos;
121
122         pos = hns3vf_find_pci_capability(device, PCI_CAP_ID_MSIX);
123         if (pos) {
124                 rte_pci_read_config(device, &control, sizeof(control),
125                                     (pos + PCI_MSIX_FLAGS));
126                 if (op)
127                         control |= PCI_MSIX_FLAGS_ENABLE;
128                 else
129                         control &= ~PCI_MSIX_FLAGS_ENABLE;
130                 rte_pci_write_config(device, &control, sizeof(control),
131                                      (pos + PCI_MSIX_FLAGS));
132                 return 0;
133         }
134         return -1;
135 }
136
137 static int
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)
141 {
142         struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
143         char mac_str[RTE_ETHER_ADDR_FMT_SIZE];
144         int ret;
145
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);
151         if (ret) {
152                 rte_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
153                                       mac_addr);
154                 hns3_err(hw, "Failed to add mac addr(%s) for vf: %d", mac_str,
155                          ret);
156         }
157
158         return ret;
159 }
160
161 static void
162 hns3vf_remove_mac_addr(struct rte_eth_dev *dev, uint32_t idx)
163 {
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];
168         int ret;
169
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,
174                                 NULL, 0);
175         rte_spinlock_unlock(&hw->lock);
176         if (ret) {
177                 rte_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
178                                       mac_addr);
179                 hns3_err(hw, "Failed to remove mac addr(%s) for vf: %d",
180                          mac_str, ret);
181         }
182 }
183
184 static int
185 hns3vf_set_default_mac_addr(struct rte_eth_dev *dev,
186                             struct rte_ether_addr *mac_addr)
187 {
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];
193         int ret;
194
195         if (!rte_is_valid_assigned_ether_addr(mac_addr)) {
196                 rte_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
197                                       mac_addr);
198                 hns3_err(hw, "Failed to set mac addr, addr(%s) invalid.",
199                          mac_str);
200                 return -EINVAL;
201         }
202
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,
207                RTE_ETHER_ADDR_LEN);
208
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);
212         if (ret) {
213                 rte_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
214                                       mac_addr);
215                 hns3_err(hw, "Failed to set mac addr(%s) for vf: %d", mac_str,
216                          ret);
217         }
218
219         rte_ether_addr_copy(mac_addr,
220                             (struct rte_ether_addr *)hw->mac.mac_addr);
221         rte_spinlock_unlock(&hw->lock);
222
223         return ret;
224 }
225
226 static int
227 hns3vf_configure_mac_addr(struct hns3_adapter *hns, bool del)
228 {
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];
233         int ret = 0;
234         int i;
235
236         if (del)
237                 opcode = HNS3_MBX_MAC_VLAN_UC_REMOVE;
238         else
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))
243                         continue;
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,
248                                         false, NULL, 0);
249                 if (ret) {
250                         hns3_err(hw, "Failed to remove mac addr for vf: %d",
251                                  ret);
252                         break;
253                 }
254         }
255         return ret;
256 }
257
258 static int
259 hns3vf_add_mc_mac_addr(struct hns3_adapter *hns,
260                        struct rte_ether_addr *mac_addr)
261 {
262         char mac_str[RTE_ETHER_ADDR_FMT_SIZE];
263         struct hns3_hw *hw = &hns->hw;
264         int ret;
265
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,
269                                 NULL, 0);
270         if (ret) {
271                 rte_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
272                                       mac_addr);
273                 hns3_err(hw, "Failed to add mc mac addr(%s) for vf: %d",
274                          mac_str, ret);
275                 return ret;
276         }
277
278         return 0;
279 }
280
281 static int
282 hns3vf_remove_mc_mac_addr(struct hns3_adapter *hns,
283                           struct rte_ether_addr *mac_addr)
284 {
285         char mac_str[RTE_ETHER_ADDR_FMT_SIZE];
286         struct hns3_hw *hw = &hns->hw;
287         int ret;
288
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,
292                                 NULL, 0);
293         if (ret) {
294                 rte_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
295                                       mac_addr);
296                 hns3_err(hw, "Failed to remove mc mac addr(%s) for vf: %d",
297                          mac_str, ret);
298                 return ret;
299         }
300
301         return 0;
302 }
303
304 static int
305 hns3vf_set_mc_mac_addr_list(struct rte_eth_dev *dev,
306                             struct rte_ether_addr *mc_addr_set,
307                             uint32_t nb_mc_addr)
308 {
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];
313         int cur_addr_num;
314         int set_addr_num;
315         int num;
316         int ret;
317         int i;
318
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);
323                 return -EINVAL;
324         }
325
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,
331                                               addr);
332                         hns3_err(hw,
333                                  "Failed to set mc mac addr, addr(%s) invalid.",
334                                  mac_str);
335                         return -EINVAL;
336                 }
337         }
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);
344                 if (ret) {
345                         rte_spinlock_unlock(&hw->lock);
346                         return ret;
347                 }
348
349                 hw->mc_addrs_num--;
350         }
351
352         for (i = 0; i < set_addr_num; i++) {
353                 addr = &mc_addr_set[i];
354                 ret = hns3vf_add_mc_mac_addr(hns, addr);
355                 if (ret) {
356                         rte_spinlock_unlock(&hw->lock);
357                         return ret;
358                 }
359
360                 rte_ether_addr_copy(addr, &hw->mc_addrs[hw->mc_addrs_num]);
361                 hw->mc_addrs_num++;
362         }
363         rte_spinlock_unlock(&hw->lock);
364
365         return 0;
366 }
367
368 static int
369 hns3vf_configure_all_mc_mac_addr(struct hns3_adapter *hns, bool del)
370 {
371         char mac_str[RTE_ETHER_ADDR_FMT_SIZE];
372         struct hns3_hw *hw = &hns->hw;
373         struct rte_ether_addr *addr;
374         int err = 0;
375         int ret;
376         int i;
377
378         for (i = 0; i < hw->mc_addrs_num; i++) {
379                 addr = &hw->mc_addrs[i];
380                 if (!rte_is_multicast_ether_addr(addr))
381                         continue;
382                 if (del)
383                         ret = hns3vf_remove_mc_mac_addr(hns, addr);
384                 else
385                         ret = hns3vf_add_mc_mac_addr(hns, addr);
386                 if (ret) {
387                         err = ret;
388                         rte_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
389                                               addr);
390                         hns3_err(hw, "Failed to %s mc mac addr: %s for vf: %d",
391                                  del ? "Remove" : "Restore", mac_str, ret);
392                 }
393         }
394         return err;
395 }
396
397 static int
398 hns3vf_set_promisc_mode(struct hns3_hw *hw, bool en_bc_pmc)
399 {
400         struct hns3_mbx_vf_to_pf_cmd *req;
401         struct hns3_cmd_desc desc;
402         int ret;
403
404         req = (struct hns3_mbx_vf_to_pf_cmd *)desc.data;
405
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;
409
410         ret = hns3_cmd_send(hw, &desc, 1);
411         if (ret)
412                 hns3_err(hw, "Set promisc mode fail, status is %d", ret);
413
414         return ret;
415 }
416
417 static int
418 hns3vf_dev_configure(struct rte_eth_dev *dev)
419 {
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;
427         uint16_t mtu;
428         int ret;
429
430         /*
431          * Hardware does not support where the number of rx and tx queues is
432          * not equal in hip08.
433          */
434         if (nb_rx_q != nb_tx_q) {
435                 hns3_err(hw,
436                          "nb_rx_queues(%u) not equal with nb_tx_queues(%u)! "
437                          "Hardware does not support this configuration!",
438                          nb_rx_q, nb_tx_q);
439                 return -EINVAL;
440         }
441
442         if (conf->link_speeds & ETH_LINK_SPEED_FIXED) {
443                 hns3_err(hw, "setting link speed/duplex not supported");
444                 return -EINVAL;
445         }
446
447         hw->adapter_state = HNS3_NIC_CONFIGURING;
448
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;
455                 }
456
457                 ret = hns3_dev_rss_hash_update(dev, &rss_conf);
458                 if (ret)
459                         goto cfg_err;
460         }
461
462         /*
463          * If jumbo frames are enabled, MTU needs to be refreshed
464          * according to the maximum RX packet length.
465          */
466         if (conf->rxmode.offloads & DEV_RX_OFFLOAD_JUMBO_FRAME) {
467                 /*
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.
471                  */
472                 mtu = (uint16_t)HNS3_PKTLEN_TO_MTU(conf->rxmode.max_rx_pkt_len);
473                 ret = hns3vf_dev_mtu_set(dev, mtu);
474                 if (ret)
475                         goto cfg_err;
476                 dev->data->mtu = mtu;
477         }
478
479         ret = hns3vf_dev_configure_vlan(dev);
480         if (ret)
481                 goto cfg_err;
482
483         hw->adapter_state = HNS3_NIC_CONFIGURED;
484         return 0;
485
486 cfg_err:
487         hw->adapter_state = HNS3_NIC_INITIALIZED;
488         return ret;
489 }
490
491 static int
492 hns3vf_config_mtu(struct hns3_hw *hw, uint16_t mtu)
493 {
494         int ret;
495
496         ret = hns3_send_mbx_msg(hw, HNS3_MBX_SET_MTU, 0, (const uint8_t *)&mtu,
497                                 sizeof(mtu), true, NULL, 0);
498         if (ret)
499                 hns3_err(hw, "Failed to set mtu (%u) for vf: %d", mtu, ret);
500
501         return ret;
502 }
503
504 static int
505 hns3vf_dev_mtu_set(struct rte_eth_dev *dev, uint16_t mtu)
506 {
507         struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
508         uint32_t frame_size = mtu + HNS3_ETH_OVERHEAD;
509         int ret;
510
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);
514                 return -EBUSY;
515         }
516
517         if (rte_atomic16_read(&hw->reset.resetting)) {
518                 hns3_err(hw, "Failed to set mtu during resetting");
519                 return -EIO;
520         }
521
522         rte_spinlock_lock(&hw->lock);
523         ret = hns3vf_config_mtu(hw, mtu);
524         if (ret) {
525                 rte_spinlock_unlock(&hw->lock);
526                 return ret;
527         }
528         if (frame_size > RTE_ETHER_MAX_LEN)
529                 dev->data->dev_conf.rxmode.offloads |=
530                                                 DEV_RX_OFFLOAD_JUMBO_FRAME;
531         else
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);
536
537         return 0;
538 }
539
540 static int
541 hns3vf_dev_infos_get(struct rte_eth_dev *eth_dev, struct rte_eth_dev_info *info)
542 {
543         struct hns3_adapter *hns = eth_dev->data->dev_private;
544         struct hns3_hw *hw = &hns->hw;
545
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;
552
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);
575
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,
580         };
581
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,
586         };
587
588         info->vmdq_queue_num = 0;
589
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;
595
596         return 0;
597 }
598
599 static void
600 hns3vf_clear_event_cause(struct hns3_hw *hw, uint32_t regclr)
601 {
602         hns3_write_dev(hw, HNS3_VECTOR0_CMDQ_SRC_REG, regclr);
603 }
604
605 static void
606 hns3vf_disable_irq0(struct hns3_hw *hw)
607 {
608         hns3_write_dev(hw, HNS3_MISC_VECTOR_REG_BASE, 0);
609 }
610
611 static void
612 hns3vf_enable_irq0(struct hns3_hw *hw)
613 {
614         hns3_write_dev(hw, HNS3_MISC_VECTOR_REG_BASE, 1);
615 }
616
617 static enum hns3vf_evt_cause
618 hns3vf_check_event_cause(struct hns3_adapter *hns, uint32_t *clearval)
619 {
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;
624         uint32_t val;
625
626         /* Fetch the events from their corresponding regs */
627         cmdq_stat_reg = hns3_read_dev(hw, HNS3_VECTOR0_CMDQ_STAT_REG);
628
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);
637                 if (clearval) {
638                         hw->reset.stats.global_cnt++;
639                         hns3_warn(hw, "Global reset detected, clear reset status");
640                 } else {
641                         hns3_schedule_delayed_reset(hns);
642                         hns3_warn(hw, "Global reset detected, don't clear reset status");
643                 }
644
645                 ret = HNS3VF_VECTOR0_EVENT_RST;
646                 goto out;
647         }
648
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;
653                 goto out;
654         }
655
656         val = 0;
657         ret = HNS3VF_VECTOR0_EVENT_OTHER;
658 out:
659         if (clearval)
660                 *clearval = val;
661         return ret;
662 }
663
664 static void
665 hns3vf_interrupt_handler(void *param)
666 {
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;
671         uint32_t clearval;
672
673         if (hw->irq_thread_id == 0)
674                 hw->irq_thread_id = pthread_self();
675
676         /* Disable interrupt */
677         hns3vf_disable_irq0(hw);
678
679         /* Read out interrupt causes */
680         event_cause = hns3vf_check_event_cause(hns, &clearval);
681
682         switch (event_cause) {
683         case HNS3VF_VECTOR0_EVENT_RST:
684                 hns3_schedule_reset(hns);
685                 break;
686         case HNS3VF_VECTOR0_EVENT_MBX:
687                 hns3_dev_handle_mbx_msg(hw);
688                 break;
689         default:
690                 break;
691         }
692
693         /* Clear interrupt causes */
694         hns3vf_clear_event_cause(hw, clearval);
695
696         /* Enable interrupt */
697         hns3vf_enable_irq0(hw);
698 }
699
700 static int
701 hns3vf_check_tqp_info(struct hns3_hw *hw)
702 {
703         uint16_t tqps_num;
704
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 "
708                                   "range: 1~%d",
709                              tqps_num, HNS3_MAX_TQP_NUM_PER_FUNC);
710                 return -EINVAL;
711         }
712
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);
716
717         return 0;
718 }
719
720 static int
721 hns3vf_get_queue_info(struct hns3_hw *hw)
722 {
723 #define HNS3VF_TQPS_RSS_INFO_LEN        6
724         uint8_t resp_msg[HNS3VF_TQPS_RSS_INFO_LEN];
725         int ret;
726
727         ret = hns3_send_mbx_msg(hw, HNS3_MBX_GET_QINFO, 0, NULL, 0, true,
728                                 resp_msg, HNS3VF_TQPS_RSS_INFO_LEN);
729         if (ret) {
730                 PMD_INIT_LOG(ERR, "Failed to get tqp info from PF: %d", ret);
731                 return ret;
732         }
733
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));
737
738         return hns3vf_check_tqp_info(hw);
739 }
740
741 static int
742 hns3vf_get_queue_depth(struct hns3_hw *hw)
743 {
744 #define HNS3VF_TQPS_DEPTH_INFO_LEN      4
745         uint8_t resp_msg[HNS3VF_TQPS_DEPTH_INFO_LEN];
746         int ret;
747
748         ret = hns3_send_mbx_msg(hw, HNS3_MBX_GET_QDEPTH, 0, NULL, 0, true,
749                                 resp_msg, HNS3VF_TQPS_DEPTH_INFO_LEN);
750         if (ret) {
751                 PMD_INIT_LOG(ERR, "Failed to get tqp depth info from PF: %d",
752                              ret);
753                 return ret;
754         }
755
756         memcpy(&hw->num_tx_desc, &resp_msg[0], sizeof(uint16_t));
757         memcpy(&hw->num_rx_desc, &resp_msg[2], sizeof(uint16_t));
758
759         return 0;
760 }
761
762 static int
763 hns3vf_get_tc_info(struct hns3_hw *hw)
764 {
765         uint8_t resp_msg;
766         int ret;
767
768         ret = hns3_send_mbx_msg(hw, HNS3_MBX_GET_TCINFO, 0, NULL, 0,
769                                 true, &resp_msg, sizeof(resp_msg));
770         if (ret) {
771                 hns3_err(hw, "VF request to get TC info from PF failed %d",
772                          ret);
773                 return ret;
774         }
775
776         hw->hw_tc_map = resp_msg;
777
778         return 0;
779 }
780
781 static int
782 hns3vf_get_configuration(struct hns3_hw *hw)
783 {
784         int ret;
785
786         hw->mac.media_type = HNS3_MEDIA_TYPE_NONE;
787
788         /* Get queue configuration from PF */
789         ret = hns3vf_get_queue_info(hw);
790         if (ret)
791                 return ret;
792
793         /* Get queue depth info from PF */
794         ret = hns3vf_get_queue_depth(hw);
795         if (ret)
796                 return ret;
797
798         /* Get tc configuration from PF */
799         return hns3vf_get_tc_info(hw);
800 }
801
802 static void
803 hns3vf_set_tc_info(struct hns3_adapter *hns)
804 {
805         struct hns3_hw *hw = &hns->hw;
806         uint16_t nb_rx_q = hw->data->nb_rx_queues;
807         uint16_t new_tqps;
808         uint8_t i;
809
810         hw->num_tc = 0;
811         for (i = 0; i < HNS3_MAX_TC_NUM; i++)
812                 if (hw->hw_tc_map & BIT(i))
813                         hw->num_tc++;
814
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;
818
819         hns3_tc_queue_mapping_cfg(hw);
820 }
821
822 static void
823 hns3vf_request_link_info(struct hns3_hw *hw)
824 {
825         uint8_t resp_msg;
826         int ret;
827
828         if (rte_atomic16_read(&hw->reset.resetting))
829                 return;
830         ret = hns3_send_mbx_msg(hw, HNS3_MBX_GET_LINK_STATUS, 0, NULL, 0, false,
831                                 &resp_msg, sizeof(resp_msg));
832         if (ret)
833                 hns3_err(hw, "Failed to fetch link status from PF: %d", ret);
834 }
835
836 static int
837 hns3vf_vlan_filter_configure(struct hns3_adapter *hns, uint16_t vlan_id, int on)
838 {
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;
844
845         msg_data[0] = is_kill;
846         memcpy(&msg_data[1], &vlan_id, sizeof(vlan_id));
847         memcpy(&msg_data[3], &proto, sizeof(proto));
848
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,
851                                  0);
852 }
853
854 static int
855 hns3vf_vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on)
856 {
857         struct hns3_adapter *hns = dev->data->dev_private;
858         struct hns3_hw *hw = &hns->hw;
859         int ret;
860
861         if (rte_atomic16_read(&hw->reset.resetting)) {
862                 hns3_err(hw,
863                          "vf set vlan id failed during resetting, vlan_id =%u",
864                          vlan_id);
865                 return -EIO;
866         }
867         rte_spinlock_lock(&hw->lock);
868         ret = hns3vf_vlan_filter_configure(hns, vlan_id, on);
869         rte_spinlock_unlock(&hw->lock);
870         if (ret)
871                 hns3_err(hw, "vf set vlan id failed, vlan_id =%u, ret =%d",
872                          vlan_id, ret);
873
874         return ret;
875 }
876
877 static int
878 hns3vf_en_hw_strip_rxvtag(struct hns3_hw *hw, bool enable)
879 {
880         uint8_t msg_data;
881         int ret;
882
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);
886         if (ret)
887                 hns3_err(hw, "vf enable strip failed, ret =%d", ret);
888
889         return ret;
890 }
891
892 static int
893 hns3vf_vlan_offload_set(struct rte_eth_dev *dev, int mask)
894 {
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;
898
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);
906                 else
907                         hns3vf_en_hw_strip_rxvtag(hw, false);
908                 rte_spinlock_unlock(&hw->lock);
909         }
910
911         return 0;
912 }
913
914 static int
915 hns3vf_handle_all_vlan_table(struct hns3_adapter *hns, int on)
916 {
917         struct rte_vlan_filter_conf *vfc;
918         struct hns3_hw *hw = &hns->hw;
919         uint16_t vlan_id;
920         uint64_t vbit;
921         uint64_t ids;
922         int ret = 0;
923         uint32_t i;
924
925         vfc = &hw->data->vlan_filter_conf;
926         for (i = 0; i < RTE_DIM(vfc->ids); i++) {
927                 if (vfc->ids[i] == 0)
928                         continue;
929                 ids = vfc->ids[i];
930                 while (ids) {
931                         /*
932                          * 64 means the num bits of ids, one bit corresponds to
933                          * one vlan id
934                          */
935                         vlan_id = 64 * i;
936                         /* count trailing zeroes */
937                         vbit = ~ids & (ids - 1);
938                         /* clear least significant bit set */
939                         ids ^= (ids ^ (ids - 1)) ^ vbit;
940                         for (; vbit;) {
941                                 vbit >>= 1;
942                                 vlan_id++;
943                         }
944                         ret = hns3vf_vlan_filter_configure(hns, vlan_id, on);
945                         if (ret) {
946                                 hns3_err(hw,
947                                          "VF handle vlan table failed, ret =%d, on = %d",
948                                          ret, on);
949                                 return ret;
950                         }
951                 }
952         }
953
954         return ret;
955 }
956
957 static int
958 hns3vf_remove_all_vlan_table(struct hns3_adapter *hns)
959 {
960         return hns3vf_handle_all_vlan_table(hns, 0);
961 }
962
963 static int
964 hns3vf_restore_vlan_conf(struct hns3_adapter *hns)
965 {
966         struct hns3_hw *hw = &hns->hw;
967         struct rte_eth_conf *dev_conf;
968         bool en;
969         int ret;
970
971         dev_conf = &hw->data->dev_conf;
972         en = dev_conf->rxmode.offloads & DEV_RX_OFFLOAD_VLAN_STRIP ? true
973                                                                    : false;
974         ret = hns3vf_en_hw_strip_rxvtag(hw, en);
975         if (ret)
976                 hns3_err(hw, "VF restore vlan conf fail, en =%d, ret =%d", en,
977                          ret);
978         return ret;
979 }
980
981 static int
982 hns3vf_dev_configure_vlan(struct rte_eth_dev *dev)
983 {
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;
987         int ret;
988
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!");
994         }
995
996         /* Apply vlan offload setting */
997         ret = hns3vf_vlan_offload_set(dev, ETH_VLAN_STRIP_MASK);
998         if (ret)
999                 hns3_err(hw, "dev config vlan offload failed, ret =%d", ret);
1000
1001         return ret;
1002 }
1003
1004 static int
1005 hns3vf_set_alive(struct hns3_hw *hw, bool alive)
1006 {
1007         uint8_t msg_data;
1008
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);
1012 }
1013
1014 static void
1015 hns3vf_keep_alive_handler(void *param)
1016 {
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;
1020         uint8_t respmsg;
1021         int ret;
1022
1023         ret = hns3_send_mbx_msg(hw, HNS3_MBX_KEEP_ALIVE, 0, NULL, 0,
1024                                 false, &respmsg, sizeof(uint8_t));
1025         if (ret)
1026                 hns3_err(hw, "VF sends keeping alive cmd failed(=%d)",
1027                          ret);
1028
1029         rte_eal_alarm_set(HNS3VF_KEEP_ALIVE_INTERVAL, hns3vf_keep_alive_handler,
1030                           eth_dev);
1031 }
1032
1033 static void
1034 hns3vf_service_handler(void *param)
1035 {
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;
1039
1040         /*
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.
1048          */
1049         if (!hns3vf_is_reset_pending(hns))
1050                 hns3vf_request_link_info(hw);
1051         else
1052                 hns3_warn(hw, "Cancel the query when reset is pending");
1053
1054         rte_eal_alarm_set(HNS3VF_SERVICE_INTERVAL, hns3vf_service_handler,
1055                           eth_dev);
1056 }
1057
1058 static int
1059 hns3vf_init_hardware(struct hns3_adapter *hns)
1060 {
1061         struct hns3_hw *hw = &hns->hw;
1062         uint16_t mtu = hw->data->mtu;
1063         int ret;
1064
1065         ret = hns3vf_set_promisc_mode(hw, true);
1066         if (ret)
1067                 return ret;
1068
1069         ret = hns3vf_config_mtu(hw, mtu);
1070         if (ret)
1071                 goto err_init_hardware;
1072
1073         ret = hns3vf_vlan_filter_configure(hns, 0, 1);
1074         if (ret) {
1075                 PMD_INIT_LOG(ERR, "Failed to initialize VLAN config: %d", ret);
1076                 goto err_init_hardware;
1077         }
1078
1079         ret = hns3_config_gro(hw, false);
1080         if (ret) {
1081                 PMD_INIT_LOG(ERR, "Failed to config gro: %d", ret);
1082                 goto err_init_hardware;
1083         }
1084
1085         ret = hns3vf_set_alive(hw, true);
1086         if (ret) {
1087                 PMD_INIT_LOG(ERR, "Failed to VF send alive to PF: %d", ret);
1088                 goto err_init_hardware;
1089         }
1090
1091         hns3vf_request_link_info(hw);
1092         return 0;
1093
1094 err_init_hardware:
1095         (void)hns3vf_set_promisc_mode(hw, false);
1096         return ret;
1097 }
1098
1099 static int
1100 hns3vf_clear_vport_list(struct hns3_hw *hw)
1101 {
1102         return hns3_send_mbx_msg(hw, HNS3_MBX_HANDLE_VF_TBL,
1103                                  HNS3_MBX_VPORT_LIST_CLEAR, NULL, 0, false,
1104                                  NULL, 0);
1105 }
1106
1107 static int
1108 hns3vf_init_vf(struct rte_eth_dev *eth_dev)
1109 {
1110         struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(eth_dev);
1111         struct hns3_adapter *hns = eth_dev->data->dev_private;
1112         struct hns3_hw *hw = &hns->hw;
1113         int ret;
1114
1115         PMD_INIT_FUNC_TRACE();
1116
1117         /* Get hardware io base address from pcie BAR2 IO space */
1118         hw->io_base = pci_dev->mem_resource[2].addr;
1119
1120         /* Firmware command queue initialize */
1121         ret = hns3_cmd_init_queue(hw);
1122         if (ret) {
1123                 PMD_INIT_LOG(ERR, "Failed to init cmd queue: %d", ret);
1124                 goto err_cmd_init_queue;
1125         }
1126
1127         /* Firmware command initialize */
1128         ret = hns3_cmd_init(hw);
1129         if (ret) {
1130                 PMD_INIT_LOG(ERR, "Failed to init cmd: %d", ret);
1131                 goto err_cmd_init;
1132         }
1133
1134         rte_spinlock_init(&hw->mbx_resp.lock);
1135
1136         hns3vf_clear_event_cause(hw, 0);
1137
1138         ret = rte_intr_callback_register(&pci_dev->intr_handle,
1139                                          hns3vf_interrupt_handler, eth_dev);
1140         if (ret) {
1141                 PMD_INIT_LOG(ERR, "Failed to register intr: %d", ret);
1142                 goto err_intr_callback_register;
1143         }
1144
1145         /* Enable interrupt */
1146         rte_intr_enable(&pci_dev->intr_handle);
1147         hns3vf_enable_irq0(hw);
1148
1149         /* Get configuration from PF */
1150         ret = hns3vf_get_configuration(hw);
1151         if (ret) {
1152                 PMD_INIT_LOG(ERR, "Failed to fetch configuration: %d", ret);
1153                 goto err_get_config;
1154         }
1155
1156         rte_eth_random_addr(hw->mac.mac_addr); /* Generate a random mac addr */
1157
1158         ret = hns3vf_clear_vport_list(hw);
1159         if (ret) {
1160                 PMD_INIT_LOG(ERR, "Failed to clear tbl list: %d", ret);
1161                 goto err_get_config;
1162         }
1163
1164         ret = hns3vf_init_hardware(hns);
1165         if (ret)
1166                 goto err_get_config;
1167
1168         hns3_set_default_rss_args(hw);
1169
1170         (void)hns3_stats_reset(eth_dev);
1171         return 0;
1172
1173 err_get_config:
1174         hns3vf_disable_irq0(hw);
1175         rte_intr_disable(&pci_dev->intr_handle);
1176         hns3_intr_unregister(&pci_dev->intr_handle, hns3vf_interrupt_handler,
1177                              eth_dev);
1178 err_intr_callback_register:
1179         hns3_cmd_uninit(hw);
1180
1181 err_cmd_init:
1182         hns3_cmd_destroy_queue(hw);
1183
1184 err_cmd_init_queue:
1185         hw->io_base = NULL;
1186
1187         return ret;
1188 }
1189
1190 static void
1191 hns3vf_uninit_vf(struct rte_eth_dev *eth_dev)
1192 {
1193         struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(eth_dev);
1194         struct hns3_adapter *hns = eth_dev->data->dev_private;
1195         struct hns3_hw *hw = &hns->hw;
1196
1197         PMD_INIT_FUNC_TRACE();
1198
1199         hns3_rss_uninit(hns);
1200         (void)hns3vf_set_alive(hw, false);
1201         (void)hns3vf_set_promisc_mode(hw, false);
1202         hns3vf_disable_irq0(hw);
1203         rte_intr_disable(&pci_dev->intr_handle);
1204         hns3_intr_unregister(&pci_dev->intr_handle, hns3vf_interrupt_handler,
1205                              eth_dev);
1206         hns3_cmd_uninit(hw);
1207         hns3_cmd_destroy_queue(hw);
1208         hw->io_base = NULL;
1209 }
1210
1211 static int
1212 hns3vf_bind_ring_with_vector(struct rte_eth_dev *dev, uint8_t vector_id,
1213                              bool mmap, uint16_t queue_id)
1214
1215 {
1216         struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1217         struct hns3_vf_bind_vector_msg bind_msg;
1218         uint16_t code;
1219         int ret;
1220
1221         memset(&bind_msg, 0, sizeof(bind_msg));
1222         code = mmap ? HNS3_MBX_MAP_RING_TO_VECTOR :
1223                 HNS3_MBX_UNMAP_RING_TO_VECTOR;
1224         bind_msg.vector_id = vector_id;
1225         bind_msg.ring_num = 1;
1226         bind_msg.param[0].ring_type = HNS3_RING_TYPE_RX;
1227         bind_msg.param[0].tqp_index = queue_id;
1228         bind_msg.param[0].int_gl_index = HNS3_RING_GL_RX;
1229
1230         ret = hns3_send_mbx_msg(hw, code, 0, (uint8_t *)&bind_msg,
1231                                 sizeof(bind_msg), false, NULL, 0);
1232         if (ret) {
1233                 hns3_err(hw, "Map TQP %d fail, vector_id is %d, ret is %d.",
1234                          queue_id, vector_id, ret);
1235                 return ret;
1236         }
1237
1238         return 0;
1239 }
1240
1241 static int
1242 hns3vf_do_stop(struct hns3_adapter *hns)
1243 {
1244         struct hns3_hw *hw = &hns->hw;
1245         bool reset_queue;
1246
1247         hw->mac.link_status = ETH_LINK_DOWN;
1248
1249         if (rte_atomic16_read(&hw->reset.disable_cmd) == 0) {
1250                 hns3vf_configure_mac_addr(hns, true);
1251                 reset_queue = true;
1252         } else
1253                 reset_queue = false;
1254         return hns3_stop_queues(hns, reset_queue);
1255 }
1256
1257 static void
1258 hns3vf_unmap_rx_interrupt(struct rte_eth_dev *dev)
1259 {
1260         struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
1261         struct rte_intr_handle *intr_handle = &pci_dev->intr_handle;
1262         uint8_t base = 0;
1263         uint8_t vec = 0;
1264         uint16_t q_id;
1265
1266         if (dev->data->dev_conf.intr_conf.rxq == 0)
1267                 return;
1268
1269         /* unmap the ring with vector */
1270         if (rte_intr_allow_others(intr_handle)) {
1271                 vec = RTE_INTR_VEC_RXTX_OFFSET;
1272                 base = RTE_INTR_VEC_RXTX_OFFSET;
1273         }
1274         if (rte_intr_dp_is_en(intr_handle)) {
1275                 for (q_id = 0; q_id < dev->data->nb_rx_queues; q_id++) {
1276                         (void)hns3vf_bind_ring_with_vector(dev, vec, false,
1277                                                            q_id);
1278                         if (vec < base + intr_handle->nb_efd - 1)
1279                                 vec++;
1280                 }
1281         }
1282         /* Clean datapath event and queue/vec mapping */
1283         rte_intr_efd_disable(intr_handle);
1284         if (intr_handle->intr_vec) {
1285                 rte_free(intr_handle->intr_vec);
1286                 intr_handle->intr_vec = NULL;
1287         }
1288 }
1289
1290 static void
1291 hns3vf_dev_stop(struct rte_eth_dev *dev)
1292 {
1293         struct hns3_adapter *hns = dev->data->dev_private;
1294         struct hns3_hw *hw = &hns->hw;
1295
1296         PMD_INIT_FUNC_TRACE();
1297
1298         hw->adapter_state = HNS3_NIC_STOPPING;
1299         hns3_set_rxtx_function(dev);
1300         rte_wmb();
1301         /* Disable datapath on secondary process. */
1302         hns3_mp_req_stop_rxtx(dev);
1303         /* Prevent crashes when queues are still in use. */
1304         rte_delay_ms(hw->tqps_num);
1305
1306         rte_spinlock_lock(&hw->lock);
1307         if (rte_atomic16_read(&hw->reset.resetting) == 0) {
1308                 hns3vf_do_stop(hns);
1309                 hns3_dev_release_mbufs(hns);
1310                 hw->adapter_state = HNS3_NIC_CONFIGURED;
1311         }
1312         rte_eal_alarm_cancel(hns3vf_service_handler, dev);
1313         rte_spinlock_unlock(&hw->lock);
1314
1315         hns3vf_unmap_rx_interrupt(dev);
1316 }
1317
1318 static void
1319 hns3vf_dev_close(struct rte_eth_dev *eth_dev)
1320 {
1321         struct hns3_adapter *hns = eth_dev->data->dev_private;
1322         struct hns3_hw *hw = &hns->hw;
1323
1324         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
1325                 return;
1326
1327         if (hw->adapter_state == HNS3_NIC_STARTED)
1328                 hns3vf_dev_stop(eth_dev);
1329
1330         hw->adapter_state = HNS3_NIC_CLOSING;
1331         hns3_reset_abort(hns);
1332         hw->adapter_state = HNS3_NIC_CLOSED;
1333         rte_eal_alarm_cancel(hns3vf_keep_alive_handler, eth_dev);
1334         hns3vf_configure_all_mc_mac_addr(hns, true);
1335         hns3vf_remove_all_vlan_table(hns);
1336         hns3vf_uninit_vf(eth_dev);
1337         hns3_free_all_queues(eth_dev);
1338         rte_free(hw->reset.wait_data);
1339         rte_free(eth_dev->process_private);
1340         eth_dev->process_private = NULL;
1341         hns3_mp_uninit_primary();
1342         hns3_warn(hw, "Close port %d finished", hw->data->port_id);
1343 }
1344
1345 static int
1346 hns3vf_dev_link_update(struct rte_eth_dev *eth_dev,
1347                        __rte_unused int wait_to_complete)
1348 {
1349         struct hns3_adapter *hns = eth_dev->data->dev_private;
1350         struct hns3_hw *hw = &hns->hw;
1351         struct hns3_mac *mac = &hw->mac;
1352         struct rte_eth_link new_link;
1353
1354         memset(&new_link, 0, sizeof(new_link));
1355         switch (mac->link_speed) {
1356         case ETH_SPEED_NUM_10M:
1357         case ETH_SPEED_NUM_100M:
1358         case ETH_SPEED_NUM_1G:
1359         case ETH_SPEED_NUM_10G:
1360         case ETH_SPEED_NUM_25G:
1361         case ETH_SPEED_NUM_40G:
1362         case ETH_SPEED_NUM_50G:
1363         case ETH_SPEED_NUM_100G:
1364                 new_link.link_speed = mac->link_speed;
1365                 break;
1366         default:
1367                 new_link.link_speed = ETH_SPEED_NUM_100M;
1368                 break;
1369         }
1370
1371         new_link.link_duplex = mac->link_duplex;
1372         new_link.link_status = mac->link_status ? ETH_LINK_UP : ETH_LINK_DOWN;
1373         new_link.link_autoneg =
1374             !(eth_dev->data->dev_conf.link_speeds & ETH_LINK_SPEED_FIXED);
1375
1376         return rte_eth_linkstatus_set(eth_dev, &new_link);
1377 }
1378
1379 static int
1380 hns3vf_do_start(struct hns3_adapter *hns, bool reset_queue)
1381 {
1382         struct hns3_hw *hw = &hns->hw;
1383         int ret;
1384
1385         hns3vf_set_tc_info(hns);
1386
1387         ret = hns3_start_queues(hns, reset_queue);
1388         if (ret) {
1389                 hns3_err(hw, "Failed to start queues: %d", ret);
1390                 return ret;
1391         }
1392
1393         return 0;
1394 }
1395
1396 static int
1397 hns3vf_map_rx_interrupt(struct rte_eth_dev *dev)
1398 {
1399         struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
1400         struct rte_intr_handle *intr_handle = &pci_dev->intr_handle;
1401         struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1402         uint32_t intr_vector;
1403         uint8_t base = 0;
1404         uint8_t vec = 0;
1405         uint16_t q_id;
1406         int ret;
1407
1408         if (dev->data->dev_conf.intr_conf.rxq == 0)
1409                 return 0;
1410
1411         /* disable uio/vfio intr/eventfd mapping */
1412         rte_intr_disable(intr_handle);
1413
1414         /* check and configure queue intr-vector mapping */
1415         if (rte_intr_cap_multiple(intr_handle) ||
1416                 !RTE_ETH_DEV_SRIOV(dev).active) {
1417                 intr_vector = dev->data->nb_rx_queues;
1418                 /* It creates event fd for each intr vector when MSIX is used */
1419                 if (rte_intr_efd_enable(intr_handle, intr_vector))
1420                         return -EINVAL;
1421         }
1422         if (rte_intr_dp_is_en(intr_handle) && !intr_handle->intr_vec) {
1423                 intr_handle->intr_vec =
1424                         rte_zmalloc("intr_vec",
1425                                     dev->data->nb_rx_queues * sizeof(int), 0);
1426                 if (intr_handle->intr_vec == NULL) {
1427                         hns3_err(hw, "Failed to allocate %d rx_queues"
1428                                      " intr_vec", dev->data->nb_rx_queues);
1429                         ret = -ENOMEM;
1430                         goto vf_alloc_intr_vec_error;
1431                 }
1432         }
1433
1434         if (rte_intr_allow_others(intr_handle)) {
1435                 vec = RTE_INTR_VEC_RXTX_OFFSET;
1436                 base = RTE_INTR_VEC_RXTX_OFFSET;
1437         }
1438         if (rte_intr_dp_is_en(intr_handle)) {
1439                 for (q_id = 0; q_id < dev->data->nb_rx_queues; q_id++) {
1440                         ret = hns3vf_bind_ring_with_vector(dev, vec, true,
1441                                                            q_id);
1442                         if (ret)
1443                                 goto vf_bind_vector_error;
1444                         intr_handle->intr_vec[q_id] = vec;
1445                         if (vec < base + intr_handle->nb_efd - 1)
1446                                 vec++;
1447                 }
1448         }
1449         rte_intr_enable(intr_handle);
1450         return 0;
1451
1452 vf_bind_vector_error:
1453         rte_intr_efd_disable(intr_handle);
1454         if (intr_handle->intr_vec) {
1455                 free(intr_handle->intr_vec);
1456                 intr_handle->intr_vec = NULL;
1457         }
1458         return ret;
1459 vf_alloc_intr_vec_error:
1460         rte_intr_efd_disable(intr_handle);
1461         return ret;
1462 }
1463
1464 static int
1465 hns3vf_dev_start(struct rte_eth_dev *dev)
1466 {
1467         struct hns3_adapter *hns = dev->data->dev_private;
1468         struct hns3_hw *hw = &hns->hw;
1469         int ret = 0;
1470
1471         PMD_INIT_FUNC_TRACE();
1472         if (rte_atomic16_read(&hw->reset.resetting))
1473                 return -EBUSY;
1474
1475         rte_spinlock_lock(&hw->lock);
1476         hw->adapter_state = HNS3_NIC_STARTING;
1477         ret = hns3vf_do_start(hns, true);
1478         if (ret) {
1479                 hw->adapter_state = HNS3_NIC_CONFIGURED;
1480                 rte_spinlock_unlock(&hw->lock);
1481                 return ret;
1482         }
1483         hw->adapter_state = HNS3_NIC_STARTED;
1484         rte_spinlock_unlock(&hw->lock);
1485
1486         ret = hns3vf_map_rx_interrupt(dev);
1487         if (ret)
1488                 return ret;
1489         hns3_set_rxtx_function(dev);
1490         hns3_mp_req_start_rxtx(dev);
1491         rte_eal_alarm_set(HNS3VF_SERVICE_INTERVAL, hns3vf_service_handler, dev);
1492         return ret;
1493 }
1494
1495 static bool
1496 is_vf_reset_done(struct hns3_hw *hw)
1497 {
1498 #define HNS3_FUN_RST_ING_BITS \
1499         (BIT(HNS3_VECTOR0_GLOBALRESET_INT_B) | \
1500          BIT(HNS3_VECTOR0_CORERESET_INT_B) | \
1501          BIT(HNS3_VECTOR0_IMPRESET_INT_B) | \
1502          BIT(HNS3_VECTOR0_FUNCRESET_INT_B))
1503
1504         uint32_t val;
1505
1506         if (hw->reset.level == HNS3_VF_RESET) {
1507                 val = hns3_read_dev(hw, HNS3_VF_RST_ING);
1508                 if (val & HNS3_VF_RST_ING_BIT)
1509                         return false;
1510         } else {
1511                 val = hns3_read_dev(hw, HNS3_FUN_RST_ING);
1512                 if (val & HNS3_FUN_RST_ING_BITS)
1513                         return false;
1514         }
1515         return true;
1516 }
1517
1518 bool
1519 hns3vf_is_reset_pending(struct hns3_adapter *hns)
1520 {
1521         struct hns3_hw *hw = &hns->hw;
1522         enum hns3_reset_level reset;
1523
1524         hns3vf_check_event_cause(hns, NULL);
1525         reset = hns3vf_get_reset_level(hw, &hw->reset.pending);
1526         if (hw->reset.level != HNS3_NONE_RESET && hw->reset.level < reset) {
1527                 hns3_warn(hw, "High level reset %d is pending", reset);
1528                 return true;
1529         }
1530         return false;
1531 }
1532
1533 static int
1534 hns3vf_wait_hardware_ready(struct hns3_adapter *hns)
1535 {
1536         struct hns3_hw *hw = &hns->hw;
1537         struct hns3_wait_data *wait_data = hw->reset.wait_data;
1538         struct timeval tv;
1539
1540         if (wait_data->result == HNS3_WAIT_SUCCESS) {
1541                 /*
1542                  * After vf reset is ready, the PF may not have completed
1543                  * the reset processing. The vf sending mbox to PF may fail
1544                  * during the pf reset, so it is better to add extra delay.
1545                  */
1546                 if (hw->reset.level == HNS3_VF_FUNC_RESET ||
1547                     hw->reset.level == HNS3_FLR_RESET)
1548                         return 0;
1549                 /* Reset retry process, no need to add extra delay. */
1550                 if (hw->reset.attempts)
1551                         return 0;
1552                 if (wait_data->check_completion == NULL)
1553                         return 0;
1554
1555                 wait_data->check_completion = NULL;
1556                 wait_data->interval = 1 * MSEC_PER_SEC * USEC_PER_MSEC;
1557                 wait_data->count = 1;
1558                 wait_data->result = HNS3_WAIT_REQUEST;
1559                 rte_eal_alarm_set(wait_data->interval, hns3_wait_callback,
1560                                   wait_data);
1561                 hns3_warn(hw, "hardware is ready, delay 1 sec for PF reset complete");
1562                 return -EAGAIN;
1563         } else if (wait_data->result == HNS3_WAIT_TIMEOUT) {
1564                 gettimeofday(&tv, NULL);
1565                 hns3_warn(hw, "Reset step4 hardware not ready after reset time=%ld.%.6ld",
1566                           tv.tv_sec, tv.tv_usec);
1567                 return -ETIME;
1568         } else if (wait_data->result == HNS3_WAIT_REQUEST)
1569                 return -EAGAIN;
1570
1571         wait_data->hns = hns;
1572         wait_data->check_completion = is_vf_reset_done;
1573         wait_data->end_ms = (uint64_t)HNS3VF_RESET_WAIT_CNT *
1574                                       HNS3VF_RESET_WAIT_MS + get_timeofday_ms();
1575         wait_data->interval = HNS3VF_RESET_WAIT_MS * USEC_PER_MSEC;
1576         wait_data->count = HNS3VF_RESET_WAIT_CNT;
1577         wait_data->result = HNS3_WAIT_REQUEST;
1578         rte_eal_alarm_set(wait_data->interval, hns3_wait_callback, wait_data);
1579         return -EAGAIN;
1580 }
1581
1582 static int
1583 hns3vf_prepare_reset(struct hns3_adapter *hns)
1584 {
1585         struct hns3_hw *hw = &hns->hw;
1586         int ret = 0;
1587
1588         if (hw->reset.level == HNS3_VF_FUNC_RESET) {
1589                 ret = hns3_send_mbx_msg(hw, HNS3_MBX_RESET, 0, NULL,
1590                                         0, true, NULL, 0);
1591         }
1592         rte_atomic16_set(&hw->reset.disable_cmd, 1);
1593
1594         return ret;
1595 }
1596
1597 static int
1598 hns3vf_stop_service(struct hns3_adapter *hns)
1599 {
1600         struct hns3_hw *hw = &hns->hw;
1601         struct rte_eth_dev *eth_dev;
1602
1603         eth_dev = &rte_eth_devices[hw->data->port_id];
1604         rte_eal_alarm_cancel(hns3vf_service_handler, eth_dev);
1605         hw->mac.link_status = ETH_LINK_DOWN;
1606
1607         hns3_set_rxtx_function(eth_dev);
1608         rte_wmb();
1609         /* Disable datapath on secondary process. */
1610         hns3_mp_req_stop_rxtx(eth_dev);
1611         rte_delay_ms(hw->tqps_num);
1612
1613         rte_spinlock_lock(&hw->lock);
1614         if (hw->adapter_state == HNS3_NIC_STARTED ||
1615             hw->adapter_state == HNS3_NIC_STOPPING) {
1616                 hns3vf_do_stop(hns);
1617                 hw->reset.mbuf_deferred_free = true;
1618         } else
1619                 hw->reset.mbuf_deferred_free = false;
1620
1621         /*
1622          * It is cumbersome for hardware to pick-and-choose entries for deletion
1623          * from table space. Hence, for function reset software intervention is
1624          * required to delete the entries.
1625          */
1626         if (rte_atomic16_read(&hw->reset.disable_cmd) == 0)
1627                 hns3vf_configure_all_mc_mac_addr(hns, true);
1628         rte_spinlock_unlock(&hw->lock);
1629
1630         return 0;
1631 }
1632
1633 static int
1634 hns3vf_start_service(struct hns3_adapter *hns)
1635 {
1636         struct hns3_hw *hw = &hns->hw;
1637         struct rte_eth_dev *eth_dev;
1638
1639         eth_dev = &rte_eth_devices[hw->data->port_id];
1640         hns3_set_rxtx_function(eth_dev);
1641         hns3_mp_req_start_rxtx(eth_dev);
1642
1643         hns3vf_service_handler(eth_dev);
1644         return 0;
1645 }
1646
1647 static int
1648 hns3vf_restore_conf(struct hns3_adapter *hns)
1649 {
1650         struct hns3_hw *hw = &hns->hw;
1651         int ret;
1652
1653         ret = hns3vf_configure_mac_addr(hns, false);
1654         if (ret)
1655                 return ret;
1656
1657         ret = hns3vf_configure_all_mc_mac_addr(hns, false);
1658         if (ret)
1659                 goto err_mc_mac;
1660
1661         ret = hns3vf_restore_vlan_conf(hns);
1662         if (ret)
1663                 goto err_vlan_table;
1664
1665         if (hw->adapter_state == HNS3_NIC_STARTED) {
1666                 ret = hns3vf_do_start(hns, false);
1667                 if (ret)
1668                         goto err_vlan_table;
1669                 hns3_info(hw, "hns3vf dev restart successful!");
1670         } else if (hw->adapter_state == HNS3_NIC_STOPPING)
1671                 hw->adapter_state = HNS3_NIC_CONFIGURED;
1672         return 0;
1673
1674 err_vlan_table:
1675         hns3vf_configure_all_mc_mac_addr(hns, true);
1676 err_mc_mac:
1677         hns3vf_configure_mac_addr(hns, true);
1678         return ret;
1679 }
1680
1681 static enum hns3_reset_level
1682 hns3vf_get_reset_level(struct hns3_hw *hw, uint64_t *levels)
1683 {
1684         enum hns3_reset_level reset_level;
1685
1686         /* return the highest priority reset level amongst all */
1687         if (hns3_atomic_test_bit(HNS3_VF_RESET, levels))
1688                 reset_level = HNS3_VF_RESET;
1689         else if (hns3_atomic_test_bit(HNS3_VF_FULL_RESET, levels))
1690                 reset_level = HNS3_VF_FULL_RESET;
1691         else if (hns3_atomic_test_bit(HNS3_VF_PF_FUNC_RESET, levels))
1692                 reset_level = HNS3_VF_PF_FUNC_RESET;
1693         else if (hns3_atomic_test_bit(HNS3_VF_FUNC_RESET, levels))
1694                 reset_level = HNS3_VF_FUNC_RESET;
1695         else if (hns3_atomic_test_bit(HNS3_FLR_RESET, levels))
1696                 reset_level = HNS3_FLR_RESET;
1697         else
1698                 reset_level = HNS3_NONE_RESET;
1699
1700         if (hw->reset.level != HNS3_NONE_RESET && reset_level < hw->reset.level)
1701                 return HNS3_NONE_RESET;
1702
1703         return reset_level;
1704 }
1705
1706 static void
1707 hns3vf_reset_service(void *param)
1708 {
1709         struct hns3_adapter *hns = (struct hns3_adapter *)param;
1710         struct hns3_hw *hw = &hns->hw;
1711         enum hns3_reset_level reset_level;
1712         struct timeval tv_delta;
1713         struct timeval tv_start;
1714         struct timeval tv;
1715         uint64_t msec;
1716
1717         /*
1718          * The interrupt is not triggered within the delay time.
1719          * The interrupt may have been lost. It is necessary to handle
1720          * the interrupt to recover from the error.
1721          */
1722         if (rte_atomic16_read(&hns->hw.reset.schedule) == SCHEDULE_DEFERRED) {
1723                 rte_atomic16_set(&hns->hw.reset.schedule, SCHEDULE_REQUESTED);
1724                 hns3_err(hw, "Handling interrupts in delayed tasks");
1725                 hns3vf_interrupt_handler(&rte_eth_devices[hw->data->port_id]);
1726                 reset_level = hns3vf_get_reset_level(hw, &hw->reset.pending);
1727                 if (reset_level == HNS3_NONE_RESET) {
1728                         hns3_err(hw, "No reset level is set, try global reset");
1729                         hns3_atomic_set_bit(HNS3_VF_RESET, &hw->reset.pending);
1730                 }
1731         }
1732         rte_atomic16_set(&hns->hw.reset.schedule, SCHEDULE_NONE);
1733
1734         /*
1735          * Hardware reset has been notified, we now have to poll & check if
1736          * hardware has actually completed the reset sequence.
1737          */
1738         reset_level = hns3vf_get_reset_level(hw, &hw->reset.pending);
1739         if (reset_level != HNS3_NONE_RESET) {
1740                 gettimeofday(&tv_start, NULL);
1741                 hns3_reset_process(hns, reset_level);
1742                 gettimeofday(&tv, NULL);
1743                 timersub(&tv, &tv_start, &tv_delta);
1744                 msec = tv_delta.tv_sec * MSEC_PER_SEC +
1745                        tv_delta.tv_usec / USEC_PER_MSEC;
1746                 if (msec > HNS3_RESET_PROCESS_MS)
1747                         hns3_err(hw, "%d handle long time delta %" PRIx64
1748                                  " ms time=%ld.%.6ld",
1749                                  hw->reset.level, msec, tv.tv_sec, tv.tv_usec);
1750         }
1751 }
1752
1753 static int
1754 hns3vf_reinit_dev(struct hns3_adapter *hns)
1755 {
1756         struct rte_eth_dev *eth_dev = &rte_eth_devices[hns->hw.data->port_id];
1757         struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(eth_dev);
1758         struct hns3_hw *hw = &hns->hw;
1759         int ret;
1760
1761         if (hw->reset.level == HNS3_VF_FULL_RESET) {
1762                 rte_intr_disable(&pci_dev->intr_handle);
1763                 hns3vf_set_bus_master(pci_dev, true);
1764         }
1765
1766         /* Firmware command initialize */
1767         ret = hns3_cmd_init(hw);
1768         if (ret) {
1769                 hns3_err(hw, "Failed to init cmd: %d", ret);
1770                 goto err_cmd_init;
1771         }
1772
1773         if (hw->reset.level == HNS3_VF_FULL_RESET) {
1774                 /*
1775                  * UIO enables msix by writing the pcie configuration space
1776                  * vfio_pci enables msix in rte_intr_enable.
1777                  */
1778                 if (pci_dev->kdrv == RTE_KDRV_IGB_UIO ||
1779                     pci_dev->kdrv == RTE_KDRV_UIO_GENERIC) {
1780                         if (hns3vf_enable_msix(pci_dev, true))
1781                                 hns3_err(hw, "Failed to enable msix");
1782                 }
1783
1784                 rte_intr_enable(&pci_dev->intr_handle);
1785         }
1786
1787         ret = hns3_reset_all_queues(hns);
1788         if (ret) {
1789                 hns3_err(hw, "Failed to reset all queues: %d", ret);
1790                 goto err_init;
1791         }
1792
1793         ret = hns3vf_init_hardware(hns);
1794         if (ret) {
1795                 hns3_err(hw, "Failed to init hardware: %d", ret);
1796                 goto err_init;
1797         }
1798
1799         return 0;
1800
1801 err_cmd_init:
1802         hns3vf_set_bus_master(pci_dev, false);
1803 err_init:
1804         hns3_cmd_uninit(hw);
1805         return ret;
1806 }
1807
1808 static const struct eth_dev_ops hns3vf_eth_dev_ops = {
1809         .dev_start          = hns3vf_dev_start,
1810         .dev_stop           = hns3vf_dev_stop,
1811         .dev_close          = hns3vf_dev_close,
1812         .mtu_set            = hns3vf_dev_mtu_set,
1813         .stats_get          = hns3_stats_get,
1814         .stats_reset        = hns3_stats_reset,
1815         .xstats_get         = hns3_dev_xstats_get,
1816         .xstats_get_names   = hns3_dev_xstats_get_names,
1817         .xstats_reset       = hns3_dev_xstats_reset,
1818         .xstats_get_by_id   = hns3_dev_xstats_get_by_id,
1819         .xstats_get_names_by_id = hns3_dev_xstats_get_names_by_id,
1820         .dev_infos_get      = hns3vf_dev_infos_get,
1821         .rx_queue_setup     = hns3_rx_queue_setup,
1822         .tx_queue_setup     = hns3_tx_queue_setup,
1823         .rx_queue_release   = hns3_dev_rx_queue_release,
1824         .tx_queue_release   = hns3_dev_tx_queue_release,
1825         .rx_queue_intr_enable   = hns3_dev_rx_queue_intr_enable,
1826         .rx_queue_intr_disable  = hns3_dev_rx_queue_intr_disable,
1827         .dev_configure      = hns3vf_dev_configure,
1828         .mac_addr_add       = hns3vf_add_mac_addr,
1829         .mac_addr_remove    = hns3vf_remove_mac_addr,
1830         .mac_addr_set       = hns3vf_set_default_mac_addr,
1831         .set_mc_addr_list   = hns3vf_set_mc_mac_addr_list,
1832         .link_update        = hns3vf_dev_link_update,
1833         .rss_hash_update    = hns3_dev_rss_hash_update,
1834         .rss_hash_conf_get  = hns3_dev_rss_hash_conf_get,
1835         .reta_update        = hns3_dev_rss_reta_update,
1836         .reta_query         = hns3_dev_rss_reta_query,
1837         .filter_ctrl        = hns3_dev_filter_ctrl,
1838         .vlan_filter_set    = hns3vf_vlan_filter_set,
1839         .vlan_offload_set   = hns3vf_vlan_offload_set,
1840         .get_reg            = hns3_get_regs,
1841         .dev_supported_ptypes_get = hns3_dev_supported_ptypes_get,
1842 };
1843
1844 static const struct hns3_reset_ops hns3vf_reset_ops = {
1845         .reset_service       = hns3vf_reset_service,
1846         .stop_service        = hns3vf_stop_service,
1847         .prepare_reset       = hns3vf_prepare_reset,
1848         .wait_hardware_ready = hns3vf_wait_hardware_ready,
1849         .reinit_dev          = hns3vf_reinit_dev,
1850         .restore_conf        = hns3vf_restore_conf,
1851         .start_service       = hns3vf_start_service,
1852 };
1853
1854 static int
1855 hns3vf_dev_init(struct rte_eth_dev *eth_dev)
1856 {
1857         struct hns3_adapter *hns = eth_dev->data->dev_private;
1858         struct hns3_hw *hw = &hns->hw;
1859         int ret;
1860
1861         PMD_INIT_FUNC_TRACE();
1862
1863         eth_dev->process_private = (struct hns3_process_private *)
1864             rte_zmalloc_socket("hns3_filter_list",
1865                                sizeof(struct hns3_process_private),
1866                                RTE_CACHE_LINE_SIZE, eth_dev->device->numa_node);
1867         if (eth_dev->process_private == NULL) {
1868                 PMD_INIT_LOG(ERR, "Failed to alloc memory for process private");
1869                 return -ENOMEM;
1870         }
1871
1872         /* initialize flow filter lists */
1873         hns3_filterlist_init(eth_dev);
1874
1875         hns3_set_rxtx_function(eth_dev);
1876         eth_dev->dev_ops = &hns3vf_eth_dev_ops;
1877         if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
1878                 hns3_mp_init_secondary();
1879                 hw->secondary_cnt++;
1880                 return 0;
1881         }
1882
1883         hns3_mp_init_primary();
1884
1885         hw->adapter_state = HNS3_NIC_UNINITIALIZED;
1886         hns->is_vf = true;
1887         hw->data = eth_dev->data;
1888
1889         ret = hns3_reset_init(hw);
1890         if (ret)
1891                 goto err_init_reset;
1892         hw->reset.ops = &hns3vf_reset_ops;
1893
1894         ret = hns3vf_init_vf(eth_dev);
1895         if (ret) {
1896                 PMD_INIT_LOG(ERR, "Failed to init vf: %d", ret);
1897                 goto err_init_vf;
1898         }
1899
1900         /* Allocate memory for storing MAC addresses */
1901         eth_dev->data->mac_addrs = rte_zmalloc("hns3vf-mac",
1902                                                sizeof(struct rte_ether_addr) *
1903                                                HNS3_VF_UC_MACADDR_NUM, 0);
1904         if (eth_dev->data->mac_addrs == NULL) {
1905                 PMD_INIT_LOG(ERR, "Failed to allocate %zx bytes needed "
1906                              "to store MAC addresses",
1907                              sizeof(struct rte_ether_addr) *
1908                              HNS3_VF_UC_MACADDR_NUM);
1909                 ret = -ENOMEM;
1910                 goto err_rte_zmalloc;
1911         }
1912
1913         rte_ether_addr_copy((struct rte_ether_addr *)hw->mac.mac_addr,
1914                             &eth_dev->data->mac_addrs[0]);
1915         hw->adapter_state = HNS3_NIC_INITIALIZED;
1916         /*
1917          * Pass the information to the rte_eth_dev_close() that it should also
1918          * release the private port resources.
1919          */
1920         eth_dev->data->dev_flags |= RTE_ETH_DEV_CLOSE_REMOVE;
1921
1922         if (rte_atomic16_read(&hns->hw.reset.schedule) == SCHEDULE_PENDING) {
1923                 hns3_err(hw, "Reschedule reset service after dev_init");
1924                 hns3_schedule_reset(hns);
1925         } else {
1926                 /* IMP will wait ready flag before reset */
1927                 hns3_notify_reset_ready(hw, false);
1928         }
1929         rte_eal_alarm_set(HNS3VF_KEEP_ALIVE_INTERVAL, hns3vf_keep_alive_handler,
1930                           eth_dev);
1931         return 0;
1932
1933 err_rte_zmalloc:
1934         hns3vf_uninit_vf(eth_dev);
1935
1936 err_init_vf:
1937         rte_free(hw->reset.wait_data);
1938
1939 err_init_reset:
1940         eth_dev->dev_ops = NULL;
1941         eth_dev->rx_pkt_burst = NULL;
1942         eth_dev->tx_pkt_burst = NULL;
1943         eth_dev->tx_pkt_prepare = NULL;
1944         rte_free(eth_dev->process_private);
1945         eth_dev->process_private = NULL;
1946
1947         return ret;
1948 }
1949
1950 static int
1951 hns3vf_dev_uninit(struct rte_eth_dev *eth_dev)
1952 {
1953         struct hns3_adapter *hns = eth_dev->data->dev_private;
1954         struct hns3_hw *hw = &hns->hw;
1955
1956         PMD_INIT_FUNC_TRACE();
1957
1958         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
1959                 return -EPERM;
1960
1961         eth_dev->dev_ops = NULL;
1962         eth_dev->rx_pkt_burst = NULL;
1963         eth_dev->tx_pkt_burst = NULL;
1964         eth_dev->tx_pkt_prepare = NULL;
1965
1966         if (hw->adapter_state < HNS3_NIC_CLOSING)
1967                 hns3vf_dev_close(eth_dev);
1968
1969         hw->adapter_state = HNS3_NIC_REMOVED;
1970         return 0;
1971 }
1972
1973 static int
1974 eth_hns3vf_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
1975                      struct rte_pci_device *pci_dev)
1976 {
1977         return rte_eth_dev_pci_generic_probe(pci_dev,
1978                                              sizeof(struct hns3_adapter),
1979                                              hns3vf_dev_init);
1980 }
1981
1982 static int
1983 eth_hns3vf_pci_remove(struct rte_pci_device *pci_dev)
1984 {
1985         return rte_eth_dev_pci_generic_remove(pci_dev, hns3vf_dev_uninit);
1986 }
1987
1988 static const struct rte_pci_id pci_id_hns3vf_map[] = {
1989         { RTE_PCI_DEVICE(PCI_VENDOR_ID_HUAWEI, HNS3_DEV_ID_100G_VF) },
1990         { RTE_PCI_DEVICE(PCI_VENDOR_ID_HUAWEI, HNS3_DEV_ID_100G_RDMA_PFC_VF) },
1991         { .vendor_id = 0, /* sentinel */ },
1992 };
1993
1994 static struct rte_pci_driver rte_hns3vf_pmd = {
1995         .id_table = pci_id_hns3vf_map,
1996         .drv_flags = RTE_PCI_DRV_NEED_MAPPING,
1997         .probe = eth_hns3vf_pci_probe,
1998         .remove = eth_hns3vf_pci_remove,
1999 };
2000
2001 RTE_PMD_REGISTER_PCI(net_hns3_vf, rte_hns3vf_pmd);
2002 RTE_PMD_REGISTER_PCI_TABLE(net_hns3_vf, pci_id_hns3vf_map);
2003 RTE_PMD_REGISTER_KMOD_DEP(net_hns3_vf, "* igb_uio | vfio-pci");