0f0fd8c61f7ac490c4aa404da453aa03b5e3246b
[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 <rte_alarm.h>
13 #include <rte_atomic.h>
14 #include <rte_bus_pci.h>
15 #include <rte_byteorder.h>
16 #include <rte_common.h>
17 #include <rte_cycles.h>
18 #include <rte_dev.h>
19 #include <rte_eal.h>
20 #include <rte_ether.h>
21 #include <rte_ethdev_driver.h>
22 #include <rte_ethdev_pci.h>
23 #include <rte_interrupts.h>
24 #include <rte_io.h>
25 #include <rte_log.h>
26 #include <rte_pci.h>
27
28 #include "hns3_ethdev.h"
29 #include "hns3_logs.h"
30 #include "hns3_rxtx.h"
31 #include "hns3_regs.h"
32 #include "hns3_intr.h"
33 #include "hns3_dcb.h"
34
35 #define HNS3VF_KEEP_ALIVE_INTERVAL      2000000 /* us */
36 #define HNS3VF_SERVICE_INTERVAL         1000000 /* us */
37
38 #define HNS3VF_RESET_WAIT_MS    20
39 #define HNS3VF_RESET_WAIT_CNT   2000
40
41 enum hns3vf_evt_cause {
42         HNS3VF_VECTOR0_EVENT_RST,
43         HNS3VF_VECTOR0_EVENT_MBX,
44         HNS3VF_VECTOR0_EVENT_OTHER,
45 };
46
47 static int hns3vf_dev_mtu_set(struct rte_eth_dev *dev, uint16_t mtu);
48 static int hns3vf_dev_configure_vlan(struct rte_eth_dev *dev);
49
50 static int
51 hns3vf_add_mac_addr(struct rte_eth_dev *dev, struct rte_ether_addr *mac_addr,
52                     __attribute__ ((unused)) uint32_t idx,
53                     __attribute__ ((unused)) uint32_t pool)
54 {
55         struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
56         char mac_str[RTE_ETHER_ADDR_FMT_SIZE];
57         int ret;
58
59         rte_spinlock_lock(&hw->lock);
60         ret = hns3_send_mbx_msg(hw, HNS3_MBX_SET_UNICAST,
61                                 HNS3_MBX_MAC_VLAN_UC_ADD, mac_addr->addr_bytes,
62                                 RTE_ETHER_ADDR_LEN, false, NULL, 0);
63         rte_spinlock_unlock(&hw->lock);
64         if (ret) {
65                 rte_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
66                                       mac_addr);
67                 hns3_err(hw, "Failed to add mac addr(%s) for vf: %d", mac_str,
68                          ret);
69         }
70
71         return ret;
72 }
73
74 static void
75 hns3vf_remove_mac_addr(struct rte_eth_dev *dev, uint32_t idx)
76 {
77         struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
78         /* index will be checked by upper level rte interface */
79         struct rte_ether_addr *mac_addr = &dev->data->mac_addrs[idx];
80         char mac_str[RTE_ETHER_ADDR_FMT_SIZE];
81         int ret;
82
83         rte_spinlock_lock(&hw->lock);
84         ret = hns3_send_mbx_msg(hw, HNS3_MBX_SET_UNICAST,
85                                 HNS3_MBX_MAC_VLAN_UC_REMOVE,
86                                 mac_addr->addr_bytes, RTE_ETHER_ADDR_LEN, false,
87                                 NULL, 0);
88         rte_spinlock_unlock(&hw->lock);
89         if (ret) {
90                 rte_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
91                                       mac_addr);
92                 hns3_err(hw, "Failed to remove mac addr(%s) for vf: %d",
93                          mac_str, ret);
94         }
95 }
96
97 static int
98 hns3vf_set_default_mac_addr(struct rte_eth_dev *dev,
99                             struct rte_ether_addr *mac_addr)
100 {
101 #define HNS3_TWO_ETHER_ADDR_LEN (RTE_ETHER_ADDR_LEN * 2)
102         struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
103         struct rte_ether_addr *old_addr;
104         uint8_t addr_bytes[HNS3_TWO_ETHER_ADDR_LEN]; /* for 2 MAC addresses */
105         char mac_str[RTE_ETHER_ADDR_FMT_SIZE];
106         int ret;
107
108         if (!rte_is_valid_assigned_ether_addr(mac_addr)) {
109                 rte_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
110                                       mac_addr);
111                 hns3_err(hw, "Failed to set mac addr, addr(%s) invalid.",
112                          mac_str);
113                 return -EINVAL;
114         }
115
116         old_addr = (struct rte_ether_addr *)hw->mac.mac_addr;
117         rte_spinlock_lock(&hw->lock);
118         memcpy(addr_bytes, mac_addr->addr_bytes, RTE_ETHER_ADDR_LEN);
119         memcpy(&addr_bytes[RTE_ETHER_ADDR_LEN], old_addr->addr_bytes,
120                RTE_ETHER_ADDR_LEN);
121
122         ret = hns3_send_mbx_msg(hw, HNS3_MBX_SET_UNICAST,
123                                 HNS3_MBX_MAC_VLAN_UC_MODIFY, addr_bytes,
124                                 HNS3_TWO_ETHER_ADDR_LEN, false, NULL, 0);
125         if (ret) {
126                 rte_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
127                                       mac_addr);
128                 hns3_err(hw, "Failed to set mac addr(%s) for vf: %d", mac_str,
129                          ret);
130         }
131
132         rte_ether_addr_copy(mac_addr,
133                             (struct rte_ether_addr *)hw->mac.mac_addr);
134         rte_spinlock_unlock(&hw->lock);
135
136         return ret;
137 }
138
139 static int
140 hns3vf_configure_mac_addr(struct hns3_adapter *hns, bool del)
141 {
142         struct hns3_hw *hw = &hns->hw;
143         struct rte_ether_addr *addr;
144         enum hns3_mbx_mac_vlan_subcode opcode;
145         char mac_str[RTE_ETHER_ADDR_FMT_SIZE];
146         int ret = 0;
147         int i;
148
149         if (del)
150                 opcode = HNS3_MBX_MAC_VLAN_UC_REMOVE;
151         else
152                 opcode = HNS3_MBX_MAC_VLAN_UC_ADD;
153         for (i = 0; i < HNS3_VF_UC_MACADDR_NUM; i++) {
154                 addr = &hw->data->mac_addrs[i];
155                 if (!rte_is_valid_assigned_ether_addr(addr))
156                         continue;
157                 rte_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE, addr);
158                 hns3_dbg(hw, "rm mac addr: %s", mac_str);
159                 ret = hns3_send_mbx_msg(hw, HNS3_MBX_SET_UNICAST, opcode,
160                                         addr->addr_bytes, RTE_ETHER_ADDR_LEN,
161                                         false, NULL, 0);
162                 if (ret) {
163                         hns3_err(hw, "Failed to remove mac addr for vf: %d",
164                                  ret);
165                         break;
166                 }
167         }
168         return ret;
169 }
170
171 static int
172 hns3vf_add_mc_mac_addr(struct hns3_adapter *hns,
173                        struct rte_ether_addr *mac_addr)
174 {
175         char mac_str[RTE_ETHER_ADDR_FMT_SIZE];
176         struct hns3_hw *hw = &hns->hw;
177         int ret;
178
179         ret = hns3_send_mbx_msg(hw, HNS3_MBX_SET_MULTICAST,
180                                 HNS3_MBX_MAC_VLAN_MC_ADD,
181                                 mac_addr->addr_bytes, RTE_ETHER_ADDR_LEN, false,
182                                 NULL, 0);
183         if (ret) {
184                 rte_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
185                                       mac_addr);
186                 hns3_err(hw, "Failed to add mc mac addr(%s) for vf: %d",
187                          mac_str, ret);
188                 return ret;
189         }
190
191         return 0;
192 }
193
194 static int
195 hns3vf_remove_mc_mac_addr(struct hns3_adapter *hns,
196                           struct rte_ether_addr *mac_addr)
197 {
198         char mac_str[RTE_ETHER_ADDR_FMT_SIZE];
199         struct hns3_hw *hw = &hns->hw;
200         int ret;
201
202         ret = hns3_send_mbx_msg(hw, HNS3_MBX_SET_MULTICAST,
203                                 HNS3_MBX_MAC_VLAN_MC_REMOVE,
204                                 mac_addr->addr_bytes, RTE_ETHER_ADDR_LEN, false,
205                                 NULL, 0);
206         if (ret) {
207                 rte_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
208                                       mac_addr);
209                 hns3_err(hw, "Failed to remove mc mac addr(%s) for vf: %d",
210                          mac_str, ret);
211                 return ret;
212         }
213
214         return 0;
215 }
216
217 static int
218 hns3vf_set_mc_mac_addr_list(struct rte_eth_dev *dev,
219                             struct rte_ether_addr *mc_addr_set,
220                             uint32_t nb_mc_addr)
221 {
222         struct hns3_adapter *hns = dev->data->dev_private;
223         struct hns3_hw *hw = &hns->hw;
224         struct rte_ether_addr *addr;
225         char mac_str[RTE_ETHER_ADDR_FMT_SIZE];
226         int cur_addr_num;
227         int set_addr_num;
228         int num;
229         int ret;
230         int i;
231
232         if (nb_mc_addr > HNS3_MC_MACADDR_NUM) {
233                 hns3_err(hw, "Failed to set mc mac addr, nb_mc_addr(%d) "
234                          "invalid. valid range: 0~%d",
235                          nb_mc_addr, HNS3_MC_MACADDR_NUM);
236                 return -EINVAL;
237         }
238
239         set_addr_num = (int)nb_mc_addr;
240         for (i = 0; i < set_addr_num; i++) {
241                 addr = &mc_addr_set[i];
242                 if (!rte_is_multicast_ether_addr(addr)) {
243                         rte_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
244                                               addr);
245                         hns3_err(hw,
246                                  "Failed to set mc mac addr, addr(%s) invalid.",
247                                  mac_str);
248                         return -EINVAL;
249                 }
250         }
251         rte_spinlock_lock(&hw->lock);
252         cur_addr_num = hw->mc_addrs_num;
253         for (i = 0; i < cur_addr_num; i++) {
254                 num = cur_addr_num - i - 1;
255                 addr = &hw->mc_addrs[num];
256                 ret = hns3vf_remove_mc_mac_addr(hns, addr);
257                 if (ret) {
258                         rte_spinlock_unlock(&hw->lock);
259                         return ret;
260                 }
261
262                 hw->mc_addrs_num--;
263         }
264
265         for (i = 0; i < set_addr_num; i++) {
266                 addr = &mc_addr_set[i];
267                 ret = hns3vf_add_mc_mac_addr(hns, addr);
268                 if (ret) {
269                         rte_spinlock_unlock(&hw->lock);
270                         return ret;
271                 }
272
273                 rte_ether_addr_copy(addr, &hw->mc_addrs[hw->mc_addrs_num]);
274                 hw->mc_addrs_num++;
275         }
276         rte_spinlock_unlock(&hw->lock);
277
278         return 0;
279 }
280
281 static int
282 hns3vf_configure_all_mc_mac_addr(struct hns3_adapter *hns, bool del)
283 {
284         char mac_str[RTE_ETHER_ADDR_FMT_SIZE];
285         struct hns3_hw *hw = &hns->hw;
286         struct rte_ether_addr *addr;
287         int err = 0;
288         int ret;
289         int i;
290
291         for (i = 0; i < hw->mc_addrs_num; i++) {
292                 addr = &hw->mc_addrs[i];
293                 if (!rte_is_multicast_ether_addr(addr))
294                         continue;
295                 if (del)
296                         ret = hns3vf_remove_mc_mac_addr(hns, addr);
297                 else
298                         ret = hns3vf_add_mc_mac_addr(hns, addr);
299                 if (ret) {
300                         err = ret;
301                         rte_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
302                                               addr);
303                         hns3_err(hw, "Failed to %s mc mac addr: %s for vf: %d",
304                                  del ? "Remove" : "Restore", mac_str, ret);
305                 }
306         }
307         return err;
308 }
309
310 static int
311 hns3vf_set_promisc_mode(struct hns3_hw *hw, bool en_bc_pmc)
312 {
313         struct hns3_mbx_vf_to_pf_cmd *req;
314         struct hns3_cmd_desc desc;
315         int ret;
316
317         req = (struct hns3_mbx_vf_to_pf_cmd *)desc.data;
318
319         hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_MBX_VF_TO_PF, false);
320         req->msg[0] = HNS3_MBX_SET_PROMISC_MODE;
321         req->msg[1] = en_bc_pmc ? 1 : 0;
322
323         ret = hns3_cmd_send(hw, &desc, 1);
324         if (ret)
325                 hns3_err(hw, "Set promisc mode fail, status is %d", ret);
326
327         return ret;
328 }
329
330 static int
331 hns3vf_dev_configure(struct rte_eth_dev *dev)
332 {
333         struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
334         struct hns3_rss_conf *rss_cfg = &hw->rss_info;
335         struct rte_eth_conf *conf = &dev->data->dev_conf;
336         enum rte_eth_rx_mq_mode mq_mode = conf->rxmode.mq_mode;
337         uint16_t nb_rx_q = dev->data->nb_rx_queues;
338         uint16_t nb_tx_q = dev->data->nb_tx_queues;
339         struct rte_eth_rss_conf rss_conf;
340         uint16_t mtu;
341         int ret;
342
343         /*
344          * Hardware does not support where the number of rx and tx queues is
345          * not equal in hip08.
346          */
347         if (nb_rx_q != nb_tx_q) {
348                 hns3_err(hw,
349                          "nb_rx_queues(%u) not equal with nb_tx_queues(%u)! "
350                          "Hardware does not support this configuration!",
351                          nb_rx_q, nb_tx_q);
352                 return -EINVAL;
353         }
354
355         if (conf->link_speeds & ETH_LINK_SPEED_FIXED) {
356                 hns3_err(hw, "setting link speed/duplex not supported");
357                 return -EINVAL;
358         }
359
360         hw->adapter_state = HNS3_NIC_CONFIGURING;
361
362         /* When RSS is not configured, redirect the packet queue 0 */
363         if ((uint32_t)mq_mode & ETH_MQ_RX_RSS_FLAG) {
364                 rss_conf = conf->rx_adv_conf.rss_conf;
365                 if (rss_conf.rss_key == NULL) {
366                         rss_conf.rss_key = rss_cfg->key;
367                         rss_conf.rss_key_len = HNS3_RSS_KEY_SIZE;
368                 }
369
370                 ret = hns3_dev_rss_hash_update(dev, &rss_conf);
371                 if (ret)
372                         goto cfg_err;
373         }
374
375         /*
376          * If jumbo frames are enabled, MTU needs to be refreshed
377          * according to the maximum RX packet length.
378          */
379         if (conf->rxmode.offloads & DEV_RX_OFFLOAD_JUMBO_FRAME) {
380                 /*
381                  * Security of max_rx_pkt_len is guaranteed in dpdk frame.
382                  * Maximum value of max_rx_pkt_len is HNS3_MAX_FRAME_LEN, so it
383                  * can safely assign to "uint16_t" type variable.
384                  */
385                 mtu = (uint16_t)HNS3_PKTLEN_TO_MTU(conf->rxmode.max_rx_pkt_len);
386                 ret = hns3vf_dev_mtu_set(dev, mtu);
387                 if (ret)
388                         goto cfg_err;
389                 dev->data->mtu = mtu;
390         }
391
392         ret = hns3vf_dev_configure_vlan(dev);
393         if (ret)
394                 goto cfg_err;
395
396         hw->adapter_state = HNS3_NIC_CONFIGURED;
397         return 0;
398
399 cfg_err:
400         hw->adapter_state = HNS3_NIC_INITIALIZED;
401         return ret;
402 }
403
404 static int
405 hns3vf_config_mtu(struct hns3_hw *hw, uint16_t mtu)
406 {
407         int ret;
408
409         ret = hns3_send_mbx_msg(hw, HNS3_MBX_SET_MTU, 0, (const uint8_t *)&mtu,
410                                 sizeof(mtu), true, NULL, 0);
411         if (ret)
412                 hns3_err(hw, "Failed to set mtu (%u) for vf: %d", mtu, ret);
413
414         return ret;
415 }
416
417 static int
418 hns3vf_dev_mtu_set(struct rte_eth_dev *dev, uint16_t mtu)
419 {
420         struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
421         uint32_t frame_size = mtu + HNS3_ETH_OVERHEAD;
422         int ret;
423
424         if (dev->data->dev_started) {
425                 hns3_err(hw, "Failed to set mtu, port %u must be stopped "
426                          "before configuration", dev->data->port_id);
427                 return -EBUSY;
428         }
429
430         rte_spinlock_lock(&hw->lock);
431         ret = hns3vf_config_mtu(hw, mtu);
432         if (ret) {
433                 rte_spinlock_unlock(&hw->lock);
434                 return ret;
435         }
436         if (frame_size > RTE_ETHER_MAX_LEN)
437                 dev->data->dev_conf.rxmode.offloads |=
438                                                 DEV_RX_OFFLOAD_JUMBO_FRAME;
439         else
440                 dev->data->dev_conf.rxmode.offloads &=
441                                                 ~DEV_RX_OFFLOAD_JUMBO_FRAME;
442         dev->data->dev_conf.rxmode.max_rx_pkt_len = frame_size;
443         rte_spinlock_unlock(&hw->lock);
444
445         return 0;
446 }
447
448 static int
449 hns3vf_dev_infos_get(struct rte_eth_dev *eth_dev, struct rte_eth_dev_info *info)
450 {
451         struct hns3_adapter *hns = eth_dev->data->dev_private;
452         struct hns3_hw *hw = &hns->hw;
453
454         info->max_rx_queues = hw->tqps_num;
455         info->max_tx_queues = hw->tqps_num;
456         info->max_rx_pktlen = HNS3_MAX_FRAME_LEN; /* CRC included */
457         info->min_rx_bufsize = hw->rx_buf_len;
458         info->max_mac_addrs = HNS3_VF_UC_MACADDR_NUM;
459         info->max_mtu = info->max_rx_pktlen - HNS3_ETH_OVERHEAD;
460
461         info->rx_offload_capa = (DEV_RX_OFFLOAD_IPV4_CKSUM |
462                                  DEV_RX_OFFLOAD_UDP_CKSUM |
463                                  DEV_RX_OFFLOAD_TCP_CKSUM |
464                                  DEV_RX_OFFLOAD_SCTP_CKSUM |
465                                  DEV_RX_OFFLOAD_OUTER_IPV4_CKSUM |
466                                  DEV_RX_OFFLOAD_OUTER_UDP_CKSUM |
467                                  DEV_RX_OFFLOAD_KEEP_CRC |
468                                  DEV_RX_OFFLOAD_SCATTER |
469                                  DEV_RX_OFFLOAD_VLAN_STRIP |
470                                  DEV_RX_OFFLOAD_QINQ_STRIP |
471                                  DEV_RX_OFFLOAD_VLAN_FILTER |
472                                  DEV_RX_OFFLOAD_JUMBO_FRAME);
473         info->tx_queue_offload_capa = DEV_TX_OFFLOAD_MBUF_FAST_FREE;
474         info->tx_offload_capa = (DEV_TX_OFFLOAD_OUTER_IPV4_CKSUM |
475                                  DEV_TX_OFFLOAD_IPV4_CKSUM |
476                                  DEV_TX_OFFLOAD_TCP_CKSUM |
477                                  DEV_TX_OFFLOAD_UDP_CKSUM |
478                                  DEV_TX_OFFLOAD_SCTP_CKSUM |
479                                  DEV_TX_OFFLOAD_VLAN_INSERT |
480                                  DEV_TX_OFFLOAD_QINQ_INSERT |
481                                  DEV_TX_OFFLOAD_MULTI_SEGS |
482                                  info->tx_queue_offload_capa);
483
484         info->rx_desc_lim = (struct rte_eth_desc_lim) {
485                 .nb_max = HNS3_MAX_RING_DESC,
486                 .nb_min = HNS3_MIN_RING_DESC,
487                 .nb_align = HNS3_ALIGN_RING_DESC,
488         };
489
490         info->tx_desc_lim = (struct rte_eth_desc_lim) {
491                 .nb_max = HNS3_MAX_RING_DESC,
492                 .nb_min = HNS3_MIN_RING_DESC,
493                 .nb_align = HNS3_ALIGN_RING_DESC,
494         };
495
496         info->vmdq_queue_num = 0;
497
498         info->reta_size = HNS3_RSS_IND_TBL_SIZE;
499         info->hash_key_size = HNS3_RSS_KEY_SIZE;
500         info->flow_type_rss_offloads = HNS3_ETH_RSS_SUPPORT;
501         info->default_rxportconf.ring_size = HNS3_DEFAULT_RING_DESC;
502         info->default_txportconf.ring_size = HNS3_DEFAULT_RING_DESC;
503
504         return 0;
505 }
506
507 static void
508 hns3vf_clear_event_cause(struct hns3_hw *hw, uint32_t regclr)
509 {
510         hns3_write_dev(hw, HNS3_VECTOR0_CMDQ_SRC_REG, regclr);
511 }
512
513 static void
514 hns3vf_disable_irq0(struct hns3_hw *hw)
515 {
516         hns3_write_dev(hw, HNS3_MISC_VECTOR_REG_BASE, 0);
517 }
518
519 static void
520 hns3vf_enable_irq0(struct hns3_hw *hw)
521 {
522         hns3_write_dev(hw, HNS3_MISC_VECTOR_REG_BASE, 1);
523 }
524
525 static enum hns3vf_evt_cause
526 hns3vf_check_event_cause(struct hns3_adapter *hns, uint32_t *clearval)
527 {
528         struct hns3_hw *hw = &hns->hw;
529         enum hns3vf_evt_cause ret;
530         uint32_t cmdq_stat_reg;
531         uint32_t val;
532
533         /* Fetch the events from their corresponding regs */
534         cmdq_stat_reg = hns3_read_dev(hw, HNS3_VECTOR0_CMDQ_STAT_REG);
535
536         /* Check for vector0 mailbox(=CMDQ RX) event source */
537         if (BIT(HNS3_VECTOR0_RX_CMDQ_INT_B) & cmdq_stat_reg) {
538                 val = cmdq_stat_reg & ~BIT(HNS3_VECTOR0_RX_CMDQ_INT_B);
539                 ret = HNS3VF_VECTOR0_EVENT_MBX;
540                 goto out;
541         }
542
543         val = 0;
544         ret = HNS3VF_VECTOR0_EVENT_OTHER;
545 out:
546         if (clearval)
547                 *clearval = val;
548         return ret;
549 }
550
551 static void
552 hns3vf_interrupt_handler(void *param)
553 {
554         struct rte_eth_dev *dev = (struct rte_eth_dev *)param;
555         struct hns3_adapter *hns = dev->data->dev_private;
556         struct hns3_hw *hw = &hns->hw;
557         enum hns3vf_evt_cause event_cause;
558         uint32_t clearval;
559
560         if (hw->irq_thread_id == 0)
561                 hw->irq_thread_id = pthread_self();
562
563         /* Disable interrupt */
564         hns3vf_disable_irq0(hw);
565
566         /* Read out interrupt causes */
567         event_cause = hns3vf_check_event_cause(hns, &clearval);
568
569         switch (event_cause) {
570         case HNS3VF_VECTOR0_EVENT_MBX:
571                 hns3_dev_handle_mbx_msg(hw);
572                 break;
573         default:
574                 break;
575         }
576
577         /* Clear interrupt causes */
578         hns3vf_clear_event_cause(hw, clearval);
579
580         /* Enable interrupt */
581         hns3vf_enable_irq0(hw);
582 }
583
584 static int
585 hns3vf_check_tqp_info(struct hns3_hw *hw)
586 {
587         uint16_t tqps_num;
588
589         tqps_num = hw->tqps_num;
590         if (tqps_num > HNS3_MAX_TQP_NUM_PER_FUNC || tqps_num == 0) {
591                 PMD_INIT_LOG(ERR, "Get invalid tqps_num(%u) from PF. valid "
592                                   "range: 1~%d",
593                              tqps_num, HNS3_MAX_TQP_NUM_PER_FUNC);
594                 return -EINVAL;
595         }
596
597         if (hw->rx_buf_len == 0)
598                 hw->rx_buf_len = HNS3_DEFAULT_RX_BUF_LEN;
599         hw->alloc_rss_size = RTE_MIN(hw->rss_size_max, hw->tqps_num);
600
601         return 0;
602 }
603
604 static int
605 hns3vf_get_queue_info(struct hns3_hw *hw)
606 {
607 #define HNS3VF_TQPS_RSS_INFO_LEN        6
608         uint8_t resp_msg[HNS3VF_TQPS_RSS_INFO_LEN];
609         int ret;
610
611         ret = hns3_send_mbx_msg(hw, HNS3_MBX_GET_QINFO, 0, NULL, 0, true,
612                                 resp_msg, HNS3VF_TQPS_RSS_INFO_LEN);
613         if (ret) {
614                 PMD_INIT_LOG(ERR, "Failed to get tqp info from PF: %d", ret);
615                 return ret;
616         }
617
618         memcpy(&hw->tqps_num, &resp_msg[0], sizeof(uint16_t));
619         memcpy(&hw->rss_size_max, &resp_msg[2], sizeof(uint16_t));
620         memcpy(&hw->rx_buf_len, &resp_msg[4], sizeof(uint16_t));
621
622         return hns3vf_check_tqp_info(hw);
623 }
624
625 static int
626 hns3vf_get_queue_depth(struct hns3_hw *hw)
627 {
628 #define HNS3VF_TQPS_DEPTH_INFO_LEN      4
629         uint8_t resp_msg[HNS3VF_TQPS_DEPTH_INFO_LEN];
630         int ret;
631
632         ret = hns3_send_mbx_msg(hw, HNS3_MBX_GET_QDEPTH, 0, NULL, 0, true,
633                                 resp_msg, HNS3VF_TQPS_DEPTH_INFO_LEN);
634         if (ret) {
635                 PMD_INIT_LOG(ERR, "Failed to get tqp depth info from PF: %d",
636                              ret);
637                 return ret;
638         }
639
640         memcpy(&hw->num_tx_desc, &resp_msg[0], sizeof(uint16_t));
641         memcpy(&hw->num_rx_desc, &resp_msg[2], sizeof(uint16_t));
642
643         return 0;
644 }
645
646 static int
647 hns3vf_get_tc_info(struct hns3_hw *hw)
648 {
649         uint8_t resp_msg;
650         int ret;
651
652         ret = hns3_send_mbx_msg(hw, HNS3_MBX_GET_TCINFO, 0, NULL, 0,
653                                 true, &resp_msg, sizeof(resp_msg));
654         if (ret) {
655                 hns3_err(hw, "VF request to get TC info from PF failed %d",
656                          ret);
657                 return ret;
658         }
659
660         hw->hw_tc_map = resp_msg;
661
662         return 0;
663 }
664
665 static int
666 hns3vf_get_configuration(struct hns3_hw *hw)
667 {
668         int ret;
669
670         hw->mac.media_type = HNS3_MEDIA_TYPE_NONE;
671
672         /* Get queue configuration from PF */
673         ret = hns3vf_get_queue_info(hw);
674         if (ret)
675                 return ret;
676
677         /* Get queue depth info from PF */
678         ret = hns3vf_get_queue_depth(hw);
679         if (ret)
680                 return ret;
681
682         /* Get tc configuration from PF */
683         return hns3vf_get_tc_info(hw);
684 }
685
686 static void
687 hns3vf_set_tc_info(struct hns3_adapter *hns)
688 {
689         struct hns3_hw *hw = &hns->hw;
690         uint16_t nb_rx_q = hw->data->nb_rx_queues;
691         uint16_t new_tqps;
692         uint8_t i;
693
694         hw->num_tc = 0;
695         for (i = 0; i < HNS3_MAX_TC_NUM; i++)
696                 if (hw->hw_tc_map & BIT(i))
697                         hw->num_tc++;
698
699         new_tqps = RTE_MIN(hw->tqps_num, nb_rx_q);
700         hw->alloc_rss_size = RTE_MIN(hw->rss_size_max, new_tqps / hw->num_tc);
701         hw->alloc_tqps = hw->alloc_rss_size * hw->num_tc;
702
703         hns3_tc_queue_mapping_cfg(hw);
704 }
705
706 static void
707 hns3vf_request_link_info(struct hns3_hw *hw)
708 {
709         uint8_t resp_msg;
710         int ret;
711
712         if (rte_atomic16_read(&hw->reset.resetting))
713                 return;
714         ret = hns3_send_mbx_msg(hw, HNS3_MBX_GET_LINK_STATUS, 0, NULL, 0, false,
715                                 &resp_msg, sizeof(resp_msg));
716         if (ret)
717                 hns3_err(hw, "Failed to fetch link status from PF: %d", ret);
718 }
719
720 static int
721 hns3vf_vlan_filter_configure(struct hns3_adapter *hns, uint16_t vlan_id, int on)
722 {
723 #define HNS3VF_VLAN_MBX_MSG_LEN 5
724         struct hns3_hw *hw = &hns->hw;
725         uint8_t msg_data[HNS3VF_VLAN_MBX_MSG_LEN];
726         uint16_t proto = htons(RTE_ETHER_TYPE_VLAN);
727         uint8_t is_kill = on ? 0 : 1;
728
729         msg_data[0] = is_kill;
730         memcpy(&msg_data[1], &vlan_id, sizeof(vlan_id));
731         memcpy(&msg_data[3], &proto, sizeof(proto));
732
733         return hns3_send_mbx_msg(hw, HNS3_MBX_SET_VLAN, HNS3_MBX_VLAN_FILTER,
734                                  msg_data, HNS3VF_VLAN_MBX_MSG_LEN, true, NULL,
735                                  0);
736 }
737
738 static int
739 hns3vf_vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on)
740 {
741         struct hns3_adapter *hns = dev->data->dev_private;
742         struct hns3_hw *hw = &hns->hw;
743         int ret;
744
745         rte_spinlock_lock(&hw->lock);
746         ret = hns3vf_vlan_filter_configure(hns, vlan_id, on);
747         rte_spinlock_unlock(&hw->lock);
748         if (ret)
749                 hns3_err(hw, "vf set vlan id failed, vlan_id =%u, ret =%d",
750                          vlan_id, ret);
751
752         return ret;
753 }
754
755 static int
756 hns3vf_en_hw_strip_rxvtag(struct hns3_hw *hw, bool enable)
757 {
758         uint8_t msg_data;
759         int ret;
760
761         msg_data = enable ? 1 : 0;
762         ret = hns3_send_mbx_msg(hw, HNS3_MBX_SET_VLAN, HNS3_MBX_VLAN_RX_OFF_CFG,
763                                 &msg_data, sizeof(msg_data), false, NULL, 0);
764         if (ret)
765                 hns3_err(hw, "vf enable strip failed, ret =%d", ret);
766
767         return ret;
768 }
769
770 static int
771 hns3vf_vlan_offload_set(struct rte_eth_dev *dev, int mask)
772 {
773         struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
774         struct rte_eth_conf *dev_conf = &dev->data->dev_conf;
775         unsigned int tmp_mask;
776
777         tmp_mask = (unsigned int)mask;
778         /* Vlan stripping setting */
779         if (tmp_mask & ETH_VLAN_STRIP_MASK) {
780                 rte_spinlock_lock(&hw->lock);
781                 /* Enable or disable VLAN stripping */
782                 if (dev_conf->rxmode.offloads & DEV_RX_OFFLOAD_VLAN_STRIP)
783                         hns3vf_en_hw_strip_rxvtag(hw, true);
784                 else
785                         hns3vf_en_hw_strip_rxvtag(hw, false);
786                 rte_spinlock_unlock(&hw->lock);
787         }
788
789         return 0;
790 }
791
792 static int
793 hns3vf_dev_configure_vlan(struct rte_eth_dev *dev)
794 {
795         struct hns3_adapter *hns = dev->data->dev_private;
796         struct rte_eth_dev_data *data = dev->data;
797         struct hns3_hw *hw = &hns->hw;
798         int ret;
799
800         if (data->dev_conf.txmode.hw_vlan_reject_tagged ||
801             data->dev_conf.txmode.hw_vlan_reject_untagged ||
802             data->dev_conf.txmode.hw_vlan_insert_pvid) {
803                 hns3_warn(hw, "hw_vlan_reject_tagged, hw_vlan_reject_untagged "
804                               "or hw_vlan_insert_pvid is not support!");
805         }
806
807         /* Apply vlan offload setting */
808         ret = hns3vf_vlan_offload_set(dev, ETH_VLAN_STRIP_MASK);
809         if (ret)
810                 hns3_err(hw, "dev config vlan offload failed, ret =%d", ret);
811
812         return ret;
813 }
814
815 static int
816 hns3vf_set_alive(struct hns3_hw *hw, bool alive)
817 {
818         uint8_t msg_data;
819
820         msg_data = alive ? 1 : 0;
821         return hns3_send_mbx_msg(hw, HNS3_MBX_SET_ALIVE, 0, &msg_data,
822                                  sizeof(msg_data), false, NULL, 0);
823 }
824
825 static void
826 hns3vf_keep_alive_handler(void *param)
827 {
828         struct rte_eth_dev *eth_dev = (struct rte_eth_dev *)param;
829         struct hns3_adapter *hns = eth_dev->data->dev_private;
830         struct hns3_hw *hw = &hns->hw;
831         uint8_t respmsg;
832         int ret;
833
834         ret = hns3_send_mbx_msg(hw, HNS3_MBX_KEEP_ALIVE, 0, NULL, 0,
835                                 false, &respmsg, sizeof(uint8_t));
836         if (ret)
837                 hns3_err(hw, "VF sends keeping alive cmd failed(=%d)",
838                          ret);
839
840         rte_eal_alarm_set(HNS3VF_KEEP_ALIVE_INTERVAL, hns3vf_keep_alive_handler,
841                           eth_dev);
842 }
843
844 static void
845 hns3vf_service_handler(void *param)
846 {
847         struct rte_eth_dev *eth_dev = (struct rte_eth_dev *)param;
848         struct hns3_adapter *hns = eth_dev->data->dev_private;
849         struct hns3_hw *hw = &hns->hw;
850
851         hns3vf_request_link_info(hw);
852
853         rte_eal_alarm_set(HNS3VF_SERVICE_INTERVAL, hns3vf_service_handler,
854                           eth_dev);
855 }
856
857 static int
858 hns3vf_init_hardware(struct hns3_adapter *hns)
859 {
860         struct hns3_hw *hw = &hns->hw;
861         uint16_t mtu = hw->data->mtu;
862         int ret;
863
864         ret = hns3vf_set_promisc_mode(hw, true);
865         if (ret)
866                 return ret;
867
868         ret = hns3vf_config_mtu(hw, mtu);
869         if (ret)
870                 goto err_init_hardware;
871
872         ret = hns3vf_vlan_filter_configure(hns, 0, 1);
873         if (ret) {
874                 PMD_INIT_LOG(ERR, "Failed to initialize VLAN config: %d", ret);
875                 goto err_init_hardware;
876         }
877
878         ret = hns3_config_gro(hw, false);
879         if (ret) {
880                 PMD_INIT_LOG(ERR, "Failed to config gro: %d", ret);
881                 goto err_init_hardware;
882         }
883
884         ret = hns3vf_set_alive(hw, true);
885         if (ret) {
886                 PMD_INIT_LOG(ERR, "Failed to VF send alive to PF: %d", ret);
887                 goto err_init_hardware;
888         }
889
890         hns3vf_request_link_info(hw);
891         return 0;
892
893 err_init_hardware:
894         (void)hns3vf_set_promisc_mode(hw, false);
895         return ret;
896 }
897
898 static int
899 hns3vf_init_vf(struct rte_eth_dev *eth_dev)
900 {
901         struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(eth_dev);
902         struct hns3_adapter *hns = eth_dev->data->dev_private;
903         struct hns3_hw *hw = &hns->hw;
904         int ret;
905
906         PMD_INIT_FUNC_TRACE();
907
908         /* Get hardware io base address from pcie BAR2 IO space */
909         hw->io_base = pci_dev->mem_resource[2].addr;
910
911         /* Firmware command queue initialize */
912         ret = hns3_cmd_init_queue(hw);
913         if (ret) {
914                 PMD_INIT_LOG(ERR, "Failed to init cmd queue: %d", ret);
915                 goto err_cmd_init_queue;
916         }
917
918         /* Firmware command initialize */
919         ret = hns3_cmd_init(hw);
920         if (ret) {
921                 PMD_INIT_LOG(ERR, "Failed to init cmd: %d", ret);
922                 goto err_cmd_init;
923         }
924
925         rte_spinlock_init(&hw->mbx_resp.lock);
926
927         hns3vf_clear_event_cause(hw, 0);
928
929         ret = rte_intr_callback_register(&pci_dev->intr_handle,
930                                          hns3vf_interrupt_handler, eth_dev);
931         if (ret) {
932                 PMD_INIT_LOG(ERR, "Failed to register intr: %d", ret);
933                 goto err_intr_callback_register;
934         }
935
936         /* Enable interrupt */
937         rte_intr_enable(&pci_dev->intr_handle);
938         hns3vf_enable_irq0(hw);
939
940         /* Get configuration from PF */
941         ret = hns3vf_get_configuration(hw);
942         if (ret) {
943                 PMD_INIT_LOG(ERR, "Failed to fetch configuration: %d", ret);
944                 goto err_get_config;
945         }
946
947         rte_eth_random_addr(hw->mac.mac_addr); /* Generate a random mac addr */
948
949         ret = hns3vf_init_hardware(hns);
950         if (ret)
951                 goto err_get_config;
952
953         hns3_set_default_rss_args(hw);
954
955         (void)hns3_stats_reset(eth_dev);
956         return 0;
957
958 err_get_config:
959         hns3vf_disable_irq0(hw);
960         rte_intr_disable(&pci_dev->intr_handle);
961         hns3_intr_unregister(&pci_dev->intr_handle, hns3vf_interrupt_handler,
962                              eth_dev);
963 err_intr_callback_register:
964         hns3_cmd_uninit(hw);
965
966 err_cmd_init:
967         hns3_cmd_destroy_queue(hw);
968
969 err_cmd_init_queue:
970         hw->io_base = NULL;
971
972         return ret;
973 }
974
975 static void
976 hns3vf_uninit_vf(struct rte_eth_dev *eth_dev)
977 {
978         struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(eth_dev);
979         struct hns3_adapter *hns = eth_dev->data->dev_private;
980         struct hns3_hw *hw = &hns->hw;
981
982         PMD_INIT_FUNC_TRACE();
983
984         hns3_rss_uninit(hns);
985         (void)hns3vf_set_alive(hw, false);
986         (void)hns3vf_set_promisc_mode(hw, false);
987         hns3vf_disable_irq0(hw);
988         rte_intr_disable(&pci_dev->intr_handle);
989         hns3_intr_unregister(&pci_dev->intr_handle, hns3vf_interrupt_handler,
990                              eth_dev);
991         hns3_cmd_uninit(hw);
992         hns3_cmd_destroy_queue(hw);
993         hw->io_base = NULL;
994 }
995
996 static int
997 hns3vf_do_stop(struct hns3_adapter *hns)
998 {
999         struct hns3_hw *hw = &hns->hw;
1000
1001         hw->mac.link_status = ETH_LINK_DOWN;
1002
1003         hns3vf_configure_mac_addr(hns, true);
1004
1005         return 0;
1006 }
1007
1008 static void
1009 hns3vf_dev_stop(struct rte_eth_dev *eth_dev)
1010 {
1011         struct hns3_adapter *hns = eth_dev->data->dev_private;
1012         struct hns3_hw *hw = &hns->hw;
1013
1014         PMD_INIT_FUNC_TRACE();
1015
1016         hw->adapter_state = HNS3_NIC_STOPPING;
1017         hns3_set_rxtx_function(eth_dev);
1018
1019         rte_spinlock_lock(&hw->lock);
1020         hns3vf_do_stop(hns);
1021         hns3_dev_release_mbufs(hns);
1022         hw->adapter_state = HNS3_NIC_CONFIGURED;
1023         rte_spinlock_unlock(&hw->lock);
1024 }
1025
1026 static void
1027 hns3vf_dev_close(struct rte_eth_dev *eth_dev)
1028 {
1029         struct hns3_adapter *hns = eth_dev->data->dev_private;
1030         struct hns3_hw *hw = &hns->hw;
1031
1032         if (hw->adapter_state == HNS3_NIC_STARTED)
1033                 hns3vf_dev_stop(eth_dev);
1034
1035         hw->adapter_state = HNS3_NIC_CLOSING;
1036         rte_eal_alarm_cancel(hns3vf_keep_alive_handler, eth_dev);
1037         rte_eal_alarm_cancel(hns3vf_service_handler, eth_dev);
1038         hns3vf_configure_all_mc_mac_addr(hns, true);
1039         hns3vf_uninit_vf(eth_dev);
1040         hns3_free_all_queues(eth_dev);
1041         rte_free(eth_dev->process_private);
1042         eth_dev->process_private = NULL;
1043         hw->adapter_state = HNS3_NIC_CLOSED;
1044         hns3_warn(hw, "Close port %d finished", hw->data->port_id);
1045 }
1046
1047 static int
1048 hns3vf_dev_link_update(struct rte_eth_dev *eth_dev,
1049                        __rte_unused int wait_to_complete)
1050 {
1051         struct hns3_adapter *hns = eth_dev->data->dev_private;
1052         struct hns3_hw *hw = &hns->hw;
1053         struct hns3_mac *mac = &hw->mac;
1054         struct rte_eth_link new_link;
1055
1056         hns3vf_request_link_info(hw);
1057
1058         memset(&new_link, 0, sizeof(new_link));
1059         switch (mac->link_speed) {
1060         case ETH_SPEED_NUM_10M:
1061         case ETH_SPEED_NUM_100M:
1062         case ETH_SPEED_NUM_1G:
1063         case ETH_SPEED_NUM_10G:
1064         case ETH_SPEED_NUM_25G:
1065         case ETH_SPEED_NUM_40G:
1066         case ETH_SPEED_NUM_50G:
1067         case ETH_SPEED_NUM_100G:
1068                 new_link.link_speed = mac->link_speed;
1069                 break;
1070         default:
1071                 new_link.link_speed = ETH_SPEED_NUM_100M;
1072                 break;
1073         }
1074
1075         new_link.link_duplex = mac->link_duplex;
1076         new_link.link_status = mac->link_status ? ETH_LINK_UP : ETH_LINK_DOWN;
1077         new_link.link_autoneg =
1078             !(eth_dev->data->dev_conf.link_speeds & ETH_LINK_SPEED_FIXED);
1079
1080         return rte_eth_linkstatus_set(eth_dev, &new_link);
1081 }
1082
1083 static int
1084 hns3vf_do_start(struct hns3_adapter *hns, bool reset_queue)
1085 {
1086         struct hns3_hw *hw = &hns->hw;
1087         int ret;
1088
1089         hns3vf_set_tc_info(hns);
1090
1091         ret = hns3_start_queues(hns, reset_queue);
1092         if (ret) {
1093                 hns3_err(hw, "Failed to start queues: %d", ret);
1094                 return ret;
1095         }
1096
1097         return 0;
1098 }
1099
1100 static int
1101 hns3vf_dev_start(struct rte_eth_dev *eth_dev)
1102 {
1103         struct hns3_adapter *hns = eth_dev->data->dev_private;
1104         struct hns3_hw *hw = &hns->hw;
1105         int ret;
1106
1107         PMD_INIT_FUNC_TRACE();
1108         rte_spinlock_lock(&hw->lock);
1109         hw->adapter_state = HNS3_NIC_STARTING;
1110         ret = hns3vf_do_start(hns, true);
1111         if (ret) {
1112                 hw->adapter_state = HNS3_NIC_CONFIGURED;
1113                 rte_spinlock_unlock(&hw->lock);
1114                 return ret;
1115         }
1116         hw->adapter_state = HNS3_NIC_STARTED;
1117         rte_spinlock_unlock(&hw->lock);
1118         hns3_set_rxtx_function(eth_dev);
1119         return 0;
1120 }
1121
1122 static const struct eth_dev_ops hns3vf_eth_dev_ops = {
1123         .dev_start          = hns3vf_dev_start,
1124         .dev_stop           = hns3vf_dev_stop,
1125         .dev_close          = hns3vf_dev_close,
1126         .mtu_set            = hns3vf_dev_mtu_set,
1127         .stats_get          = hns3_stats_get,
1128         .stats_reset        = hns3_stats_reset,
1129         .xstats_get         = hns3_dev_xstats_get,
1130         .xstats_get_names   = hns3_dev_xstats_get_names,
1131         .xstats_reset       = hns3_dev_xstats_reset,
1132         .xstats_get_by_id   = hns3_dev_xstats_get_by_id,
1133         .xstats_get_names_by_id = hns3_dev_xstats_get_names_by_id,
1134         .dev_infos_get      = hns3vf_dev_infos_get,
1135         .rx_queue_setup     = hns3_rx_queue_setup,
1136         .tx_queue_setup     = hns3_tx_queue_setup,
1137         .rx_queue_release   = hns3_dev_rx_queue_release,
1138         .tx_queue_release   = hns3_dev_tx_queue_release,
1139         .dev_configure      = hns3vf_dev_configure,
1140         .mac_addr_add       = hns3vf_add_mac_addr,
1141         .mac_addr_remove    = hns3vf_remove_mac_addr,
1142         .mac_addr_set       = hns3vf_set_default_mac_addr,
1143         .set_mc_addr_list   = hns3vf_set_mc_mac_addr_list,
1144         .link_update        = hns3vf_dev_link_update,
1145         .rss_hash_update    = hns3_dev_rss_hash_update,
1146         .rss_hash_conf_get  = hns3_dev_rss_hash_conf_get,
1147         .reta_update        = hns3_dev_rss_reta_update,
1148         .reta_query         = hns3_dev_rss_reta_query,
1149         .filter_ctrl        = hns3_dev_filter_ctrl,
1150         .vlan_filter_set    = hns3vf_vlan_filter_set,
1151         .vlan_offload_set   = hns3vf_vlan_offload_set,
1152         .get_reg            = hns3_get_regs,
1153         .dev_supported_ptypes_get = hns3_dev_supported_ptypes_get,
1154 };
1155
1156 static int
1157 hns3vf_dev_init(struct rte_eth_dev *eth_dev)
1158 {
1159         struct hns3_adapter *hns = eth_dev->data->dev_private;
1160         struct hns3_hw *hw = &hns->hw;
1161         int ret;
1162
1163         PMD_INIT_FUNC_TRACE();
1164
1165         eth_dev->process_private = (struct hns3_process_private *)
1166             rte_zmalloc_socket("hns3_filter_list",
1167                                sizeof(struct hns3_process_private),
1168                                RTE_CACHE_LINE_SIZE, eth_dev->device->numa_node);
1169         if (eth_dev->process_private == NULL) {
1170                 PMD_INIT_LOG(ERR, "Failed to alloc memory for process private");
1171                 return -ENOMEM;
1172         }
1173
1174         /* initialize flow filter lists */
1175         hns3_filterlist_init(eth_dev);
1176
1177         hns3_set_rxtx_function(eth_dev);
1178         eth_dev->dev_ops = &hns3vf_eth_dev_ops;
1179         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
1180                 return 0;
1181
1182         hw->adapter_state = HNS3_NIC_UNINITIALIZED;
1183         hns->is_vf = true;
1184         hw->data = eth_dev->data;
1185
1186         ret = hns3vf_init_vf(eth_dev);
1187         if (ret) {
1188                 PMD_INIT_LOG(ERR, "Failed to init vf: %d", ret);
1189                 goto err_init_vf;
1190         }
1191
1192         /* Allocate memory for storing MAC addresses */
1193         eth_dev->data->mac_addrs = rte_zmalloc("hns3vf-mac",
1194                                                sizeof(struct rte_ether_addr) *
1195                                                HNS3_VF_UC_MACADDR_NUM, 0);
1196         if (eth_dev->data->mac_addrs == NULL) {
1197                 PMD_INIT_LOG(ERR, "Failed to allocate %zx bytes needed "
1198                              "to store MAC addresses",
1199                              sizeof(struct rte_ether_addr) *
1200                              HNS3_VF_UC_MACADDR_NUM);
1201                 ret = -ENOMEM;
1202                 goto err_rte_zmalloc;
1203         }
1204
1205         rte_ether_addr_copy((struct rte_ether_addr *)hw->mac.mac_addr,
1206                             &eth_dev->data->mac_addrs[0]);
1207         hw->adapter_state = HNS3_NIC_INITIALIZED;
1208         /*
1209          * Pass the information to the rte_eth_dev_close() that it should also
1210          * release the private port resources.
1211          */
1212         eth_dev->data->dev_flags |= RTE_ETH_DEV_CLOSE_REMOVE;
1213
1214         rte_eal_alarm_set(HNS3VF_KEEP_ALIVE_INTERVAL, hns3vf_keep_alive_handler,
1215                           eth_dev);
1216         rte_eal_alarm_set(HNS3VF_SERVICE_INTERVAL, hns3vf_service_handler,
1217                           eth_dev);
1218         return 0;
1219
1220 err_rte_zmalloc:
1221         hns3vf_uninit_vf(eth_dev);
1222
1223 err_init_vf:
1224         eth_dev->dev_ops = NULL;
1225         eth_dev->rx_pkt_burst = NULL;
1226         eth_dev->tx_pkt_burst = NULL;
1227         eth_dev->tx_pkt_prepare = NULL;
1228         rte_free(eth_dev->process_private);
1229         eth_dev->process_private = NULL;
1230
1231         return ret;
1232 }
1233
1234 static int
1235 hns3vf_dev_uninit(struct rte_eth_dev *eth_dev)
1236 {
1237         struct hns3_adapter *hns = eth_dev->data->dev_private;
1238         struct hns3_hw *hw = &hns->hw;
1239
1240         PMD_INIT_FUNC_TRACE();
1241
1242         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
1243                 return -EPERM;
1244
1245         eth_dev->dev_ops = NULL;
1246         eth_dev->rx_pkt_burst = NULL;
1247         eth_dev->tx_pkt_burst = NULL;
1248         eth_dev->tx_pkt_prepare = NULL;
1249
1250         if (hw->adapter_state < HNS3_NIC_CLOSING)
1251                 hns3vf_dev_close(eth_dev);
1252
1253         hw->adapter_state = HNS3_NIC_REMOVED;
1254         return 0;
1255 }
1256
1257 static int
1258 eth_hns3vf_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
1259                      struct rte_pci_device *pci_dev)
1260 {
1261         return rte_eth_dev_pci_generic_probe(pci_dev,
1262                                              sizeof(struct hns3_adapter),
1263                                              hns3vf_dev_init);
1264 }
1265
1266 static int
1267 eth_hns3vf_pci_remove(struct rte_pci_device *pci_dev)
1268 {
1269         return rte_eth_dev_pci_generic_remove(pci_dev, hns3vf_dev_uninit);
1270 }
1271
1272 static const struct rte_pci_id pci_id_hns3vf_map[] = {
1273         { RTE_PCI_DEVICE(PCI_VENDOR_ID_HUAWEI, HNS3_DEV_ID_100G_VF) },
1274         { RTE_PCI_DEVICE(PCI_VENDOR_ID_HUAWEI, HNS3_DEV_ID_100G_RDMA_PFC_VF) },
1275         { .vendor_id = 0, /* sentinel */ },
1276 };
1277
1278 static struct rte_pci_driver rte_hns3vf_pmd = {
1279         .id_table = pci_id_hns3vf_map,
1280         .drv_flags = RTE_PCI_DRV_NEED_MAPPING,
1281         .probe = eth_hns3vf_pci_probe,
1282         .remove = eth_hns3vf_pci_remove,
1283 };
1284
1285 RTE_PMD_REGISTER_PCI(net_hns3_vf, rte_hns3vf_pmd);
1286 RTE_PMD_REGISTER_PCI_TABLE(net_hns3_vf, pci_id_hns3vf_map);
1287 RTE_PMD_REGISTER_KMOD_DEP(net_hns3_vf, "* igb_uio | vfio-pci");