e33363990c9dab0953f0aa800acd230fbda9b753
[dpdk.git] / drivers / net / octeontx_ep / otx_ep_ethdev.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(C) 2021 Marvell.
3  */
4
5 #include <ethdev_pci.h>
6
7 #include "otx2_common.h"
8 #include "otx_ep_common.h"
9 #include "otx_ep_vf.h"
10
11 #define OTX_EP_DEV(_eth_dev)            ((_eth_dev)->data->dev_private)
12 static int
13 otx_ep_chip_specific_setup(struct otx_ep_device *otx_epvf)
14 {
15         struct rte_pci_device *pdev = otx_epvf->pdev;
16         uint32_t dev_id = pdev->id.device_id;
17         int ret = 0;
18
19         switch (dev_id) {
20         case PCI_DEVID_OCTEONTX_EP_VF:
21                 otx_epvf->chip_id = dev_id;
22                 break;
23         case PCI_DEVID_OCTEONTX2_EP_NET_VF:
24         case PCI_DEVID_CN98XX_EP_NET_VF:
25                 otx_epvf->chip_id = dev_id;
26                 break;
27         default:
28                 otx_ep_err("Unsupported device\n");
29                 ret = -EINVAL;
30         }
31
32         if (!ret)
33                 otx_ep_info("OTX_EP dev_id[%d]\n", dev_id);
34
35         return ret;
36 }
37
38 /* OTX_EP VF device initialization */
39 static int
40 otx_epdev_init(struct otx_ep_device *otx_epvf)
41 {
42         int ret = 0;
43
44         ret = otx_ep_chip_specific_setup(otx_epvf);
45         if (ret) {
46                 otx_ep_err("Chip specific setup failed\n");
47                 goto setup_fail;
48         }
49
50 setup_fail:
51         return ret;
52 }
53
54 static int
55 otx_ep_eth_dev_uninit(__rte_unused struct rte_eth_dev *eth_dev)
56 {
57         return 0;
58 }
59
60 static int
61 otx_ep_eth_dev_init(struct rte_eth_dev *eth_dev)
62 {
63         struct rte_pci_device *pdev = RTE_ETH_DEV_TO_PCI(eth_dev);
64         struct otx_ep_device *otx_epvf = OTX_EP_DEV(eth_dev);
65         struct rte_ether_addr vf_mac_addr;
66
67         /* Single process support */
68         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
69                 return 0;
70
71         otx_epvf->eth_dev = eth_dev;
72         otx_epvf->port_id = eth_dev->data->port_id;
73         eth_dev->data->mac_addrs = rte_zmalloc("otx_ep", RTE_ETHER_ADDR_LEN, 0);
74         if (eth_dev->data->mac_addrs == NULL) {
75                 otx_ep_err("MAC addresses memory allocation failed\n");
76                 return -ENOMEM;
77         }
78         rte_eth_random_addr(vf_mac_addr.addr_bytes);
79         rte_ether_addr_copy(&vf_mac_addr, eth_dev->data->mac_addrs);
80         otx_epvf->hw_addr = pdev->mem_resource[0].addr;
81         otx_epvf->pdev = pdev;
82
83         otx_epdev_init(otx_epvf);
84
85         return 0;
86 }
87
88 static int
89 otx_ep_eth_dev_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
90                       struct rte_pci_device *pci_dev)
91 {
92         return rte_eth_dev_pci_generic_probe(pci_dev,
93                                              sizeof(struct otx_ep_device),
94                                              otx_ep_eth_dev_init);
95 }
96
97 static int
98 otx_ep_eth_dev_pci_remove(struct rte_pci_device *pci_dev)
99 {
100         return rte_eth_dev_pci_generic_remove(pci_dev,
101                                               otx_ep_eth_dev_uninit);
102 }
103
104 /* Set of PCI devices this driver supports */
105 static const struct rte_pci_id pci_id_otx_ep_map[] = {
106         { RTE_PCI_DEVICE(PCI_VENDOR_ID_CAVIUM, PCI_DEVID_OCTEONTX_EP_VF) },
107         { RTE_PCI_DEVICE(PCI_VENDOR_ID_CAVIUM, PCI_DEVID_OCTEONTX2_EP_NET_VF) },
108         { RTE_PCI_DEVICE(PCI_VENDOR_ID_CAVIUM, PCI_DEVID_CN98XX_EP_NET_VF) },
109         { .vendor_id = 0, /* sentinel */ }
110 };
111
112 static struct rte_pci_driver rte_otx_ep_pmd = {
113         .id_table       = pci_id_otx_ep_map,
114         .drv_flags      = RTE_PCI_DRV_NEED_MAPPING,
115         .probe          = otx_ep_eth_dev_pci_probe,
116         .remove         = otx_ep_eth_dev_pci_remove,
117 };
118
119 RTE_PMD_REGISTER_PCI(net_otx_ep, rte_otx_ep_pmd);
120 RTE_PMD_REGISTER_PCI_TABLE(net_otx_ep, pci_id_otx_ep_map);
121 RTE_PMD_REGISTER_KMOD_DEP(net_otx_ep, "* igb_uio | vfio-pci");
122 RTE_LOG_REGISTER(otx_net_ep_logtype, pmd.net.octeontx_ep, NOTICE);