event/octeontx2: add init and fini for SSO object
[dpdk.git] / drivers / event / octeontx2 / otx2_evdev.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(C) 2019 Marvell International Ltd.
3  */
4
5 #include <inttypes.h>
6
7 #include <rte_bus_pci.h>
8 #include <rte_common.h>
9 #include <rte_eal.h>
10 #include <rte_eventdev_pmd_pci.h>
11 #include <rte_pci.h>
12
13 #include "otx2_evdev.h"
14
15 static int
16 otx2_sso_probe(struct rte_pci_driver *pci_drv, struct rte_pci_device *pci_dev)
17 {
18         return rte_event_pmd_pci_probe(pci_drv, pci_dev,
19                                        sizeof(struct otx2_sso_evdev),
20                                        otx2_sso_init);
21 }
22
23 static int
24 otx2_sso_remove(struct rte_pci_device *pci_dev)
25 {
26         return rte_event_pmd_pci_remove(pci_dev, otx2_sso_fini);
27 }
28
29 static const struct rte_pci_id pci_sso_map[] = {
30         {
31                 RTE_PCI_DEVICE(PCI_VENDOR_ID_CAVIUM,
32                                PCI_DEVID_OCTEONTX2_RVU_SSO_TIM_PF)
33         },
34         {
35                 .vendor_id = 0,
36         },
37 };
38
39 static struct rte_pci_driver pci_sso = {
40         .id_table = pci_sso_map,
41         .drv_flags = RTE_PCI_DRV_NEED_MAPPING | RTE_PCI_DRV_IOVA_AS_VA,
42         .probe = otx2_sso_probe,
43         .remove = otx2_sso_remove,
44 };
45
46 int
47 otx2_sso_init(struct rte_eventdev *event_dev)
48 {
49         struct free_rsrcs_rsp *rsrc_cnt;
50         struct rte_pci_device *pci_dev;
51         struct otx2_sso_evdev *dev;
52         int rc;
53
54         /* For secondary processes, the primary has done all the work */
55         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
56                 return 0;
57
58         dev = sso_pmd_priv(event_dev);
59
60         pci_dev = container_of(event_dev->dev, struct rte_pci_device, device);
61
62         /* Initialize the base otx2_dev object */
63         rc = otx2_dev_init(pci_dev, dev);
64         if (rc < 0) {
65                 otx2_err("Failed to initialize otx2_dev rc=%d", rc);
66                 goto error;
67         }
68
69         /* Get SSO and SSOW MSIX rsrc cnt */
70         otx2_mbox_alloc_msg_free_rsrc_cnt(dev->mbox);
71         rc = otx2_mbox_process_msg(dev->mbox, (void *)&rsrc_cnt);
72         if (rc < 0) {
73                 otx2_err("Unable to get free rsrc count");
74                 goto otx2_dev_uninit;
75         }
76         otx2_sso_dbg("SSO %d SSOW %d NPA %d provisioned", rsrc_cnt->sso,
77                      rsrc_cnt->ssow, rsrc_cnt->npa);
78
79         dev->max_event_ports = RTE_MIN(rsrc_cnt->ssow, OTX2_SSO_MAX_VHWS);
80         dev->max_event_queues = RTE_MIN(rsrc_cnt->sso, OTX2_SSO_MAX_VHGRP);
81         /* Grab the NPA LF if required */
82         rc = otx2_npa_lf_init(pci_dev, dev);
83         if (rc < 0) {
84                 otx2_err("Unable to init NPA lf. It might not be provisioned");
85                 goto otx2_dev_uninit;
86         }
87
88         dev->drv_inited = true;
89         dev->is_timeout_deq = 0;
90         dev->min_dequeue_timeout_ns = USEC2NSEC(1);
91         dev->max_dequeue_timeout_ns = USEC2NSEC(0x3FF);
92         dev->max_num_events = -1;
93         dev->nb_event_queues = 0;
94         dev->nb_event_ports = 0;
95
96         if (!dev->max_event_ports || !dev->max_event_queues) {
97                 otx2_err("Not enough eventdev resource queues=%d ports=%d",
98                          dev->max_event_queues, dev->max_event_ports);
99                 rc = -ENODEV;
100                 goto otx2_npa_lf_uninit;
101         }
102
103         otx2_sso_pf_func_set(dev->pf_func);
104         otx2_sso_dbg("Initializing %s max_queues=%d max_ports=%d",
105                      event_dev->data->name, dev->max_event_queues,
106                      dev->max_event_ports);
107
108
109         return 0;
110
111 otx2_npa_lf_uninit:
112         otx2_npa_lf_fini();
113 otx2_dev_uninit:
114         otx2_dev_fini(pci_dev, dev);
115 error:
116         return rc;
117 }
118
119 int
120 otx2_sso_fini(struct rte_eventdev *event_dev)
121 {
122         struct otx2_sso_evdev *dev = sso_pmd_priv(event_dev);
123         struct rte_pci_device *pci_dev;
124
125         /* For secondary processes, nothing to be done */
126         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
127                 return 0;
128
129         pci_dev = container_of(event_dev->dev, struct rte_pci_device, device);
130
131         if (!dev->drv_inited)
132                 goto dev_fini;
133
134         dev->drv_inited = false;
135         otx2_npa_lf_fini();
136
137 dev_fini:
138         if (otx2_npa_lf_active(dev)) {
139                 otx2_info("Common resource in use by other devices");
140                 return -EAGAIN;
141         }
142
143         otx2_dev_fini(pci_dev, dev);
144
145         return 0;
146 }
147
148 RTE_PMD_REGISTER_PCI(event_octeontx2, pci_sso);
149 RTE_PMD_REGISTER_PCI_TABLE(event_octeontx2, pci_sso_map);
150 RTE_PMD_REGISTER_KMOD_DEP(event_octeontx2, "vfio-pci");