raw/octeontx2_ep: add build infra and device probe
[dpdk.git] / drivers / raw / octeontx2_ep / otx2_ep_rawdev.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(C) 2019 Marvell International Ltd.
3  */
4 #include <string.h>
5 #include <unistd.h>
6
7 #include <rte_bus.h>
8 #include <rte_bus_pci.h>
9 #include <rte_eal.h>
10 #include <rte_lcore.h>
11 #include <rte_mempool.h>
12 #include <rte_pci.h>
13
14 #include <rte_common.h>
15 #include <rte_rawdev.h>
16 #include <rte_rawdev_pmd.h>
17
18 #include "otx2_common.h"
19 #include "otx2_ep_rawdev.h"
20
21 static const struct rte_pci_id pci_sdp_vf_map[] = {
22         {
23                 RTE_PCI_DEVICE(PCI_VENDOR_ID_CAVIUM,
24                                PCI_DEVID_OCTEONTX2_EP_VF)
25         },
26         {
27                 .vendor_id = 0,
28         },
29 };
30
31 static int
32 otx2_sdp_rawdev_probe(struct rte_pci_driver *pci_drv __rte_unused,
33                       struct rte_pci_device *pci_dev)
34 {
35         char name[RTE_RAWDEV_NAME_MAX_LEN];
36         struct sdp_device *sdpvf = NULL;
37         struct rte_rawdev *sdp_rawdev;
38         uint16_t vf_id;
39
40         /* Single process support */
41         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
42                 return 0;
43
44         if (pci_dev->mem_resource[0].addr)
45                 otx2_info("SDP_EP BAR0 is mapped:");
46         else {
47                 otx2_err("SDP_EP: Failed to map device BARs");
48                 otx2_err("BAR0 %p\n BAR2 %p",
49                         pci_dev->mem_resource[0].addr,
50                         pci_dev->mem_resource[2].addr);
51                 return -ENODEV;
52         }
53
54         memset(name, 0, sizeof(name));
55         snprintf(name, RTE_RAWDEV_NAME_MAX_LEN, "SDPEP:%x:%02x.%x",
56                  pci_dev->addr.bus, pci_dev->addr.devid,
57                  pci_dev->addr.function);
58
59         /* Allocate rawdev pmd */
60         sdp_rawdev = rte_rawdev_pmd_allocate(name,
61                                              sizeof(struct sdp_device),
62                                              rte_socket_id());
63
64         if (sdp_rawdev == NULL) {
65                 otx2_err("SDP_EP VF rawdev allocation failed");
66                 return -ENOMEM;
67         }
68
69         sdp_rawdev->device = &pci_dev->device;
70         sdp_rawdev->driver_name = pci_dev->driver->driver.name;
71
72         sdpvf = (struct sdp_device *)sdp_rawdev->dev_private;
73         sdpvf->hw_addr = pci_dev->mem_resource[0].addr;
74         sdpvf->pci_dev = pci_dev;
75
76         /* Discover the VF number being probed */
77         vf_id = ((pci_dev->addr.devid & 0x1F) << 3) |
78                  (pci_dev->addr.function & 0x7);
79
80         vf_id -= 1;
81         sdpvf->vf_num = vf_id;
82
83         otx2_info("SDP_EP VF[%d] probe done", vf_id);
84
85         return 0;
86 }
87
88 static int
89 otx2_sdp_rawdev_remove(struct rte_pci_device *pci_dev)
90 {
91         char name[RTE_RAWDEV_NAME_MAX_LEN];
92         struct rte_rawdev *rawdev;
93         struct sdp_device *sdpvf;
94
95         /* Single process support */
96         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
97                 return 0;
98
99         if (pci_dev == NULL) {
100                 otx2_err("SDP_EP:invalid pci_dev!");
101                 return -EINVAL;
102         }
103
104
105         memset(name, 0, sizeof(name));
106         snprintf(name, RTE_RAWDEV_NAME_MAX_LEN, "SDPEP:%x:%02x.%x",
107                  pci_dev->addr.bus, pci_dev->addr.devid,
108                  pci_dev->addr.function);
109
110         rawdev = rte_rawdev_pmd_get_named_dev(name);
111         if (rawdev == NULL) {
112                 otx2_err("SDP_EP: invalid device name (%s)", name);
113                 return -EINVAL;
114         }
115
116         sdpvf = (struct sdp_device *)rawdev->dev_private;
117         otx2_info("Removing SDP_EP VF[%d] ", sdpvf->vf_num);
118
119         /* rte_rawdev_close is called by pmd_release */
120         return rte_rawdev_pmd_release(rawdev);
121 }
122
123 static struct rte_pci_driver rte_sdp_rawdev_pmd = {
124         .id_table  = pci_sdp_vf_map,
125         .drv_flags = (RTE_PCI_DRV_NEED_MAPPING | RTE_PCI_DRV_NEED_IOVA_AS_VA),
126         .probe     = otx2_sdp_rawdev_probe,
127         .remove    = otx2_sdp_rawdev_remove,
128 };
129
130 RTE_PMD_REGISTER_PCI(sdp_rawdev_pci_driver, rte_sdp_rawdev_pmd);
131 RTE_PMD_REGISTER_PCI_TABLE(sdp_rawdev_pci_driver, pci_sdp_vf_map);
132 RTE_PMD_REGISTER_KMOD_DEP(sdp_rawdev_pci_driver, "vfio-pci");