ffd2184ad4c640e48f02b271f346e65166b87147
[dpdk.git] / drivers / net / hns3 / hns3_ethdev.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2018-2019 Hisilicon Limited.
3  */
4
5 #include <errno.h>
6 #include <stdarg.h>
7 #include <stdbool.h>
8 #include <stdio.h>
9 #include <stdint.h>
10 #include <inttypes.h>
11 #include <unistd.h>
12 #include <rte_bus_pci.h>
13 #include <rte_common.h>
14 #include <rte_cycles.h>
15 #include <rte_dev.h>
16 #include <rte_eal.h>
17 #include <rte_ether.h>
18 #include <rte_ethdev_driver.h>
19 #include <rte_ethdev_pci.h>
20 #include <rte_io.h>
21 #include <rte_log.h>
22 #include <rte_pci.h>
23
24 #include "hns3_ethdev.h"
25 #include "hns3_logs.h"
26 #include "hns3_regs.h"
27
28 int hns3_logtype_init;
29 int hns3_logtype_driver;
30
31 static void
32 hns3_dev_close(struct rte_eth_dev *eth_dev)
33 {
34         struct hns3_adapter *hns = eth_dev->data->dev_private;
35         struct hns3_hw *hw = &hns->hw;
36
37         hw->adapter_state = HNS3_NIC_CLOSED;
38 }
39
40 static const struct eth_dev_ops hns3_eth_dev_ops = {
41         .dev_close          = hns3_dev_close,
42 };
43
44 static int
45 hns3_dev_init(struct rte_eth_dev *eth_dev)
46 {
47         struct hns3_adapter *hns = eth_dev->data->dev_private;
48         struct hns3_hw *hw = &hns->hw;
49
50         PMD_INIT_FUNC_TRACE();
51
52         eth_dev->dev_ops = &hns3_eth_dev_ops;
53         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
54                 return 0;
55
56         hns->is_vf = false;
57         hw->data = eth_dev->data;
58         hw->adapter_state = HNS3_NIC_INITIALIZED;
59         /*
60          * Pass the information to the rte_eth_dev_close() that it should also
61          * release the private port resources.
62          */
63         eth_dev->data->dev_flags |= RTE_ETH_DEV_CLOSE_REMOVE;
64
65         return 0;
66 }
67
68 static int
69 hns3_dev_uninit(struct rte_eth_dev *eth_dev)
70 {
71         struct hns3_adapter *hns = eth_dev->data->dev_private;
72         struct hns3_hw *hw = &hns->hw;
73
74         PMD_INIT_FUNC_TRACE();
75
76         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
77                 return -EPERM;
78
79         eth_dev->dev_ops = NULL;
80         eth_dev->rx_pkt_burst = NULL;
81         eth_dev->tx_pkt_burst = NULL;
82         eth_dev->tx_pkt_prepare = NULL;
83         if (hw->adapter_state < HNS3_NIC_CLOSING)
84                 hns3_dev_close(eth_dev);
85
86         hw->adapter_state = HNS3_NIC_REMOVED;
87         return 0;
88 }
89
90 static int
91 eth_hns3_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
92                    struct rte_pci_device *pci_dev)
93 {
94         return rte_eth_dev_pci_generic_probe(pci_dev,
95                                              sizeof(struct hns3_adapter),
96                                              hns3_dev_init);
97 }
98
99 static int
100 eth_hns3_pci_remove(struct rte_pci_device *pci_dev)
101 {
102         return rte_eth_dev_pci_generic_remove(pci_dev, hns3_dev_uninit);
103 }
104
105 static const struct rte_pci_id pci_id_hns3_map[] = {
106         { RTE_PCI_DEVICE(PCI_VENDOR_ID_HUAWEI, HNS3_DEV_ID_GE) },
107         { RTE_PCI_DEVICE(PCI_VENDOR_ID_HUAWEI, HNS3_DEV_ID_25GE) },
108         { RTE_PCI_DEVICE(PCI_VENDOR_ID_HUAWEI, HNS3_DEV_ID_25GE_RDMA) },
109         { RTE_PCI_DEVICE(PCI_VENDOR_ID_HUAWEI, HNS3_DEV_ID_50GE_RDMA) },
110         { RTE_PCI_DEVICE(PCI_VENDOR_ID_HUAWEI, HNS3_DEV_ID_100G_RDMA_MACSEC) },
111         { .vendor_id = 0, /* sentinel */ },
112 };
113
114 static struct rte_pci_driver rte_hns3_pmd = {
115         .id_table = pci_id_hns3_map,
116         .drv_flags = RTE_PCI_DRV_NEED_MAPPING,
117         .probe = eth_hns3_pci_probe,
118         .remove = eth_hns3_pci_remove,
119 };
120
121 RTE_PMD_REGISTER_PCI(net_hns3, rte_hns3_pmd);
122 RTE_PMD_REGISTER_PCI_TABLE(net_hns3, pci_id_hns3_map);
123 RTE_PMD_REGISTER_KMOD_DEP(net_hns3, "* igb_uio | vfio-pci");
124
125 RTE_INIT(hns3_init_log)
126 {
127         hns3_logtype_init = rte_log_register("pmd.net.hns3.init");
128         if (hns3_logtype_init >= 0)
129                 rte_log_set_level(hns3_logtype_init, RTE_LOG_NOTICE);
130         hns3_logtype_driver = rte_log_register("pmd.net.hns3.driver");
131         if (hns3_logtype_driver >= 0)
132                 rte_log_set_level(hns3_logtype_driver, RTE_LOG_NOTICE);
133 }