net/octeontx2: add device init and uninit
[dpdk.git] / drivers / net / octeontx2 / otx2_ethdev.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(C) 2019 Marvell International Ltd.
3  */
4
5 #include <rte_ethdev_pci.h>
6 #include <rte_io.h>
7 #include <rte_malloc.h>
8
9 #include "otx2_ethdev.h"
10
11 static inline void
12 otx2_eth_set_rx_function(struct rte_eth_dev *eth_dev)
13 {
14         RTE_SET_USED(eth_dev);
15 }
16
17 static inline void
18 otx2_eth_set_tx_function(struct rte_eth_dev *eth_dev)
19 {
20         RTE_SET_USED(eth_dev);
21 }
22
23 static inline uint64_t
24 nix_get_rx_offload_capa(struct otx2_eth_dev *dev)
25 {
26         uint64_t capa = NIX_RX_OFFLOAD_CAPA;
27
28         if (otx2_dev_is_vf(dev))
29                 capa &= ~DEV_RX_OFFLOAD_TIMESTAMP;
30
31         return capa;
32 }
33
34 static inline uint64_t
35 nix_get_tx_offload_capa(struct otx2_eth_dev *dev)
36 {
37         RTE_SET_USED(dev);
38
39         return NIX_TX_OFFLOAD_CAPA;
40 }
41
42 static int
43 nix_lf_free(struct otx2_eth_dev *dev)
44 {
45         struct otx2_mbox *mbox = dev->mbox;
46         struct nix_lf_free_req *req;
47         struct ndc_sync_op *ndc_req;
48         int rc;
49
50         /* Sync NDC-NIX for LF */
51         ndc_req = otx2_mbox_alloc_msg_ndc_sync_op(mbox);
52         ndc_req->nix_lf_tx_sync = 1;
53         ndc_req->nix_lf_rx_sync = 1;
54         rc = otx2_mbox_process(mbox);
55         if (rc)
56                 otx2_err("Error on NDC-NIX-[TX, RX] LF sync, rc %d", rc);
57
58         req = otx2_mbox_alloc_msg_nix_lf_free(mbox);
59         /* Let AF driver free all this nix lf's
60          * NPC entries allocated using NPC MBOX.
61          */
62         req->flags = 0;
63
64         return otx2_mbox_process(mbox);
65 }
66
67 static inline int
68 nix_lf_attach(struct otx2_eth_dev *dev)
69 {
70         struct otx2_mbox *mbox = dev->mbox;
71         struct rsrc_attach_req *req;
72
73         /* Attach NIX(lf) */
74         req = otx2_mbox_alloc_msg_attach_resources(mbox);
75         req->modify = true;
76         req->nixlf = true;
77
78         return otx2_mbox_process(mbox);
79 }
80
81 static inline int
82 nix_lf_get_msix_offset(struct otx2_eth_dev *dev)
83 {
84         struct otx2_mbox *mbox = dev->mbox;
85         struct msix_offset_rsp *msix_rsp;
86         int rc;
87
88         /* Get NPA and NIX MSIX vector offsets */
89         otx2_mbox_alloc_msg_msix_offset(mbox);
90
91         rc = otx2_mbox_process_msg(mbox, (void *)&msix_rsp);
92
93         dev->nix_msixoff = msix_rsp->nix_msixoff;
94
95         return rc;
96 }
97
98 static inline int
99 otx2_eth_dev_lf_detach(struct otx2_mbox *mbox)
100 {
101         struct rsrc_detach_req *req;
102
103         req = otx2_mbox_alloc_msg_detach_resources(mbox);
104
105         /* Detach all except npa lf */
106         req->partial = true;
107         req->nixlf = true;
108         req->sso = true;
109         req->ssow = true;
110         req->timlfs = true;
111         req->cptlfs = true;
112
113         return otx2_mbox_process(mbox);
114 }
115
116 static int
117 otx2_eth_dev_init(struct rte_eth_dev *eth_dev)
118 {
119         struct otx2_eth_dev *dev = otx2_eth_pmd_priv(eth_dev);
120         struct rte_pci_device *pci_dev;
121         int rc, max_entries;
122
123         /* For secondary processes, the primary has done all the work */
124         if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
125                 /* Setup callbacks for secondary process */
126                 otx2_eth_set_tx_function(eth_dev);
127                 otx2_eth_set_rx_function(eth_dev);
128                 return 0;
129         }
130
131         pci_dev = RTE_ETH_DEV_TO_PCI(eth_dev);
132
133         rte_eth_copy_pci_info(eth_dev, pci_dev);
134         eth_dev->data->dev_flags |= RTE_ETH_DEV_CLOSE_REMOVE;
135
136         /* Zero out everything after OTX2_DEV to allow proper dev_reset() */
137         memset(&dev->otx2_eth_dev_data_start, 0, sizeof(*dev) -
138                 offsetof(struct otx2_eth_dev, otx2_eth_dev_data_start));
139
140         if (!dev->mbox_active) {
141                 /* Initialize the base otx2_dev object
142                  * only if already present
143                  */
144                 rc = otx2_dev_init(pci_dev, dev);
145                 if (rc) {
146                         otx2_err("Failed to initialize otx2_dev rc=%d", rc);
147                         goto error;
148                 }
149         }
150
151         /* Grab the NPA LF if required */
152         rc = otx2_npa_lf_init(pci_dev, dev);
153         if (rc)
154                 goto otx2_dev_uninit;
155
156         dev->configured = 0;
157         dev->drv_inited = true;
158         dev->base = dev->bar2 + (RVU_BLOCK_ADDR_NIX0 << 20);
159         dev->lmt_addr = dev->bar2 + (RVU_BLOCK_ADDR_LMT << 20);
160
161         /* Attach NIX LF */
162         rc = nix_lf_attach(dev);
163         if (rc)
164                 goto otx2_npa_uninit;
165
166         /* Get NIX MSIX offset */
167         rc = nix_lf_get_msix_offset(dev);
168         if (rc)
169                 goto otx2_npa_uninit;
170
171         /* Get maximum number of supported MAC entries */
172         max_entries = otx2_cgx_mac_max_entries_get(dev);
173         if (max_entries < 0) {
174                 otx2_err("Failed to get max entries for mac addr");
175                 rc = -ENOTSUP;
176                 goto mbox_detach;
177         }
178
179         /* For VFs, returned max_entries will be 0. But to keep default MAC
180          * address, one entry must be allocated. So setting up to 1.
181          */
182         if (max_entries == 0)
183                 max_entries = 1;
184
185         eth_dev->data->mac_addrs = rte_zmalloc("mac_addr", max_entries *
186                                                RTE_ETHER_ADDR_LEN, 0);
187         if (eth_dev->data->mac_addrs == NULL) {
188                 otx2_err("Failed to allocate memory for mac addr");
189                 rc = -ENOMEM;
190                 goto mbox_detach;
191         }
192
193         dev->max_mac_entries = max_entries;
194
195         rc = otx2_nix_mac_addr_get(eth_dev, dev->mac_addr);
196         if (rc)
197                 goto free_mac_addrs;
198
199         /* Update the mac address */
200         memcpy(eth_dev->data->mac_addrs, dev->mac_addr, RTE_ETHER_ADDR_LEN);
201
202         /* Also sync same MAC address to CGX table */
203         otx2_cgx_mac_addr_set(eth_dev, &eth_dev->data->mac_addrs[0]);
204
205         dev->tx_offload_capa = nix_get_tx_offload_capa(dev);
206         dev->rx_offload_capa = nix_get_rx_offload_capa(dev);
207
208         if (otx2_dev_is_A0(dev)) {
209                 dev->hwcap |= OTX2_FIXUP_F_MIN_4K_Q;
210                 dev->hwcap |= OTX2_FIXUP_F_LIMIT_CQ_FULL;
211         }
212
213         otx2_nix_dbg("Port=%d pf=%d vf=%d ver=%s msix_off=%d hwcap=0x%" PRIx64
214                      " rxoffload_capa=0x%" PRIx64 " txoffload_capa=0x%" PRIx64,
215                      eth_dev->data->port_id, dev->pf, dev->vf,
216                      OTX2_ETH_DEV_PMD_VERSION, dev->nix_msixoff, dev->hwcap,
217                      dev->rx_offload_capa, dev->tx_offload_capa);
218         return 0;
219
220 free_mac_addrs:
221         rte_free(eth_dev->data->mac_addrs);
222 mbox_detach:
223         otx2_eth_dev_lf_detach(dev->mbox);
224 otx2_npa_uninit:
225         otx2_npa_lf_fini();
226 otx2_dev_uninit:
227         otx2_dev_fini(pci_dev, dev);
228 error:
229         otx2_err("Failed to init nix eth_dev rc=%d", rc);
230         return rc;
231 }
232
233 static int
234 otx2_eth_dev_uninit(struct rte_eth_dev *eth_dev, bool mbox_close)
235 {
236         struct otx2_eth_dev *dev = otx2_eth_pmd_priv(eth_dev);
237         struct rte_pci_device *pci_dev;
238         int rc;
239
240         /* Nothing to be done for secondary processes */
241         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
242                 return 0;
243
244         rc = nix_lf_free(dev);
245         if (rc)
246                 otx2_err("Failed to free nix lf, rc=%d", rc);
247
248         rc = otx2_npa_lf_fini();
249         if (rc)
250                 otx2_err("Failed to cleanup npa lf, rc=%d", rc);
251
252         rte_free(eth_dev->data->mac_addrs);
253         eth_dev->data->mac_addrs = NULL;
254         dev->drv_inited = false;
255
256         pci_dev = RTE_ETH_DEV_TO_PCI(eth_dev);
257
258         rc = otx2_eth_dev_lf_detach(dev->mbox);
259         if (rc)
260                 otx2_err("Failed to detach resources, rc=%d", rc);
261
262         /* Check if mbox close is needed */
263         if (!mbox_close)
264                 return 0;
265
266         if (otx2_npa_lf_active(dev) || otx2_dev_active_vfs(dev)) {
267                 /* Will be freed later by PMD */
268                 eth_dev->data->dev_private = NULL;
269                 return 0;
270         }
271
272         otx2_dev_fini(pci_dev, dev);
273         return 0;
274 }
275
276 static int
277 nix_remove(struct rte_pci_device *pci_dev)
278 {
279         struct rte_eth_dev *eth_dev;
280         struct otx2_idev_cfg *idev;
281         struct otx2_dev *otx2_dev;
282         int rc;
283
284         eth_dev = rte_eth_dev_allocated(pci_dev->device.name);
285         if (eth_dev) {
286                 /* Cleanup eth dev */
287                 rc = otx2_eth_dev_uninit(eth_dev, true);
288                 if (rc)
289                         return rc;
290
291                 rte_eth_dev_pci_release(eth_dev);
292         }
293
294         /* Nothing to be done for secondary processes */
295         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
296                 return 0;
297
298         /* Check for common resources */
299         idev = otx2_intra_dev_get_cfg();
300         if (!idev || !idev->npa_lf || idev->npa_lf->pci_dev != pci_dev)
301                 return 0;
302
303         otx2_dev = container_of(idev->npa_lf, struct otx2_dev, npalf);
304
305         if (otx2_npa_lf_active(otx2_dev) || otx2_dev_active_vfs(otx2_dev))
306                 goto exit;
307
308         /* Safe to cleanup mbox as no more users */
309         otx2_dev_fini(pci_dev, otx2_dev);
310         rte_free(otx2_dev);
311         return 0;
312
313 exit:
314         otx2_info("%s: common resource in use by other devices", pci_dev->name);
315         return -EAGAIN;
316 }
317
318 static int
319 nix_probe(struct rte_pci_driver *pci_drv, struct rte_pci_device *pci_dev)
320 {
321         int rc;
322
323         RTE_SET_USED(pci_drv);
324
325         rc = rte_eth_dev_pci_generic_probe(pci_dev, sizeof(struct otx2_eth_dev),
326                                            otx2_eth_dev_init);
327
328         /* On error on secondary, recheck if port exists in primary or
329          * in mid of detach state.
330          */
331         if (rte_eal_process_type() != RTE_PROC_PRIMARY && rc)
332                 if (!rte_eth_dev_allocated(pci_dev->device.name))
333                         return 0;
334         return rc;
335 }
336
337 static const struct rte_pci_id pci_nix_map[] = {
338         {
339                 RTE_PCI_DEVICE(PCI_VENDOR_ID_CAVIUM, PCI_DEVID_OCTEONTX2_RVU_PF)
340         },
341         {
342                 RTE_PCI_DEVICE(PCI_VENDOR_ID_CAVIUM, PCI_DEVID_OCTEONTX2_RVU_VF)
343         },
344         {
345                 RTE_PCI_DEVICE(PCI_VENDOR_ID_CAVIUM,
346                                PCI_DEVID_OCTEONTX2_RVU_AF_VF)
347         },
348         {
349                 .vendor_id = 0,
350         },
351 };
352
353 static struct rte_pci_driver pci_nix = {
354         .id_table = pci_nix_map,
355         .drv_flags = RTE_PCI_DRV_NEED_MAPPING | RTE_PCI_DRV_IOVA_AS_VA |
356                         RTE_PCI_DRV_INTR_LSC,
357         .probe = nix_probe,
358         .remove = nix_remove,
359 };
360
361 RTE_PMD_REGISTER_PCI(net_octeontx2, pci_nix);
362 RTE_PMD_REGISTER_PCI_TABLE(net_octeontx2, pci_nix_map);
363 RTE_PMD_REGISTER_KMOD_DEP(net_octeontx2, "vfio-pci");