net/octeontx2: add device configure operation
[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_alloc(struct otx2_eth_dev *dev, uint32_t nb_rxq, uint32_t nb_txq)
44 {
45         struct otx2_mbox *mbox = dev->mbox;
46         struct nix_lf_alloc_req *req;
47         struct nix_lf_alloc_rsp *rsp;
48         int rc;
49
50         req = otx2_mbox_alloc_msg_nix_lf_alloc(mbox);
51         req->rq_cnt = nb_rxq;
52         req->sq_cnt = nb_txq;
53         req->cq_cnt = nb_rxq;
54         /* XQE_SZ should be in Sync with NIX_CQ_ENTRY_SZ */
55         RTE_BUILD_BUG_ON(NIX_CQ_ENTRY_SZ != 128);
56         req->xqe_sz = NIX_XQESZ_W16;
57         req->rss_sz = dev->rss_info.rss_size;
58         req->rss_grps = NIX_RSS_GRPS;
59         req->npa_func = otx2_npa_pf_func_get();
60         req->sso_func = otx2_sso_pf_func_get();
61         req->rx_cfg = BIT_ULL(35 /* DIS_APAD */);
62         if (dev->rx_offloads & (DEV_RX_OFFLOAD_TCP_CKSUM |
63                          DEV_RX_OFFLOAD_UDP_CKSUM)) {
64                 req->rx_cfg |= BIT_ULL(37 /* CSUM_OL4 */);
65                 req->rx_cfg |= BIT_ULL(36 /* CSUM_IL4 */);
66         }
67
68         rc = otx2_mbox_process_msg(mbox, (void *)&rsp);
69         if (rc)
70                 return rc;
71
72         dev->sqb_size = rsp->sqb_size;
73         dev->tx_chan_base = rsp->tx_chan_base;
74         dev->rx_chan_base = rsp->rx_chan_base;
75         dev->rx_chan_cnt = rsp->rx_chan_cnt;
76         dev->tx_chan_cnt = rsp->tx_chan_cnt;
77         dev->lso_tsov4_idx = rsp->lso_tsov4_idx;
78         dev->lso_tsov6_idx = rsp->lso_tsov6_idx;
79         dev->lf_tx_stats = rsp->lf_tx_stats;
80         dev->lf_rx_stats = rsp->lf_rx_stats;
81         dev->cints = rsp->cints;
82         dev->qints = rsp->qints;
83         dev->npc_flow.channel = dev->rx_chan_base;
84
85         return 0;
86 }
87
88 static int
89 nix_lf_free(struct otx2_eth_dev *dev)
90 {
91         struct otx2_mbox *mbox = dev->mbox;
92         struct nix_lf_free_req *req;
93         struct ndc_sync_op *ndc_req;
94         int rc;
95
96         /* Sync NDC-NIX for LF */
97         ndc_req = otx2_mbox_alloc_msg_ndc_sync_op(mbox);
98         ndc_req->nix_lf_tx_sync = 1;
99         ndc_req->nix_lf_rx_sync = 1;
100         rc = otx2_mbox_process(mbox);
101         if (rc)
102                 otx2_err("Error on NDC-NIX-[TX, RX] LF sync, rc %d", rc);
103
104         req = otx2_mbox_alloc_msg_nix_lf_free(mbox);
105         /* Let AF driver free all this nix lf's
106          * NPC entries allocated using NPC MBOX.
107          */
108         req->flags = 0;
109
110         return otx2_mbox_process(mbox);
111 }
112
113 static int
114 otx2_nix_configure(struct rte_eth_dev *eth_dev)
115 {
116         struct otx2_eth_dev *dev = otx2_eth_pmd_priv(eth_dev);
117         struct rte_eth_dev_data *data = eth_dev->data;
118         struct rte_eth_conf *conf = &data->dev_conf;
119         struct rte_eth_rxmode *rxmode = &conf->rxmode;
120         struct rte_eth_txmode *txmode = &conf->txmode;
121         char ea_fmt[RTE_ETHER_ADDR_FMT_SIZE];
122         struct rte_ether_addr *ea;
123         uint8_t nb_rxq, nb_txq;
124         int rc;
125
126         rc = -EINVAL;
127
128         /* Sanity checks */
129         if (rte_eal_has_hugepages() == 0) {
130                 otx2_err("Huge page is not configured");
131                 goto fail;
132         }
133
134         if (rte_eal_iova_mode() != RTE_IOVA_VA) {
135                 otx2_err("iova mode should be va");
136                 goto fail;
137         }
138
139         if (conf->link_speeds & ETH_LINK_SPEED_FIXED) {
140                 otx2_err("Setting link speed/duplex not supported");
141                 goto fail;
142         }
143
144         if (conf->dcb_capability_en == 1) {
145                 otx2_err("dcb enable is not supported");
146                 goto fail;
147         }
148
149         if (conf->fdir_conf.mode != RTE_FDIR_MODE_NONE) {
150                 otx2_err("Flow director is not supported");
151                 goto fail;
152         }
153
154         if (rxmode->mq_mode != ETH_MQ_RX_NONE &&
155             rxmode->mq_mode != ETH_MQ_RX_RSS) {
156                 otx2_err("Unsupported mq rx mode %d", rxmode->mq_mode);
157                 goto fail;
158         }
159
160         if (txmode->mq_mode != ETH_MQ_TX_NONE) {
161                 otx2_err("Unsupported mq tx mode %d", txmode->mq_mode);
162                 goto fail;
163         }
164
165         /* Free the resources allocated from the previous configure */
166         if (dev->configured == 1)
167                 nix_lf_free(dev);
168
169         if (otx2_dev_is_A0(dev) &&
170             (txmode->offloads & DEV_TX_OFFLOAD_SCTP_CKSUM) &&
171             ((txmode->offloads & DEV_TX_OFFLOAD_OUTER_IPV4_CKSUM) ||
172             (txmode->offloads & DEV_TX_OFFLOAD_OUTER_UDP_CKSUM))) {
173                 otx2_err("Outer IP and SCTP checksum unsupported");
174                 rc = -EINVAL;
175                 goto fail;
176         }
177
178         dev->rx_offloads = rxmode->offloads;
179         dev->tx_offloads = txmode->offloads;
180         dev->rss_info.rss_grps = NIX_RSS_GRPS;
181
182         nb_rxq = RTE_MAX(data->nb_rx_queues, 1);
183         nb_txq = RTE_MAX(data->nb_tx_queues, 1);
184
185         /* Alloc a nix lf */
186         rc = nix_lf_alloc(dev, nb_rxq, nb_txq);
187         if (rc) {
188                 otx2_err("Failed to init nix_lf rc=%d", rc);
189                 goto fail;
190         }
191
192         /* Update the mac address */
193         ea = eth_dev->data->mac_addrs;
194         memcpy(ea, dev->mac_addr, RTE_ETHER_ADDR_LEN);
195         if (rte_is_zero_ether_addr(ea))
196                 rte_eth_random_addr((uint8_t *)ea);
197
198         rte_ether_format_addr(ea_fmt, RTE_ETHER_ADDR_FMT_SIZE, ea);
199
200         otx2_nix_dbg("Configured port%d mac=%s nb_rxq=%d nb_txq=%d"
201                 " rx_offloads=0x%" PRIx64 " tx_offloads=0x%" PRIx64 ""
202                 " rx_flags=0x%x tx_flags=0x%x",
203                 eth_dev->data->port_id, ea_fmt, nb_rxq,
204                 nb_txq, dev->rx_offloads, dev->tx_offloads,
205                 dev->rx_offload_flags, dev->tx_offload_flags);
206
207         /* All good */
208         dev->configured = 1;
209         dev->configured_nb_rx_qs = data->nb_rx_queues;
210         dev->configured_nb_tx_qs = data->nb_tx_queues;
211         return 0;
212
213 fail:
214         return rc;
215 }
216
217 /* Initialize and register driver with DPDK Application */
218 static const struct eth_dev_ops otx2_eth_dev_ops = {
219         .dev_infos_get            = otx2_nix_info_get,
220         .dev_configure            = otx2_nix_configure,
221 };
222
223 static inline int
224 nix_lf_attach(struct otx2_eth_dev *dev)
225 {
226         struct otx2_mbox *mbox = dev->mbox;
227         struct rsrc_attach_req *req;
228
229         /* Attach NIX(lf) */
230         req = otx2_mbox_alloc_msg_attach_resources(mbox);
231         req->modify = true;
232         req->nixlf = true;
233
234         return otx2_mbox_process(mbox);
235 }
236
237 static inline int
238 nix_lf_get_msix_offset(struct otx2_eth_dev *dev)
239 {
240         struct otx2_mbox *mbox = dev->mbox;
241         struct msix_offset_rsp *msix_rsp;
242         int rc;
243
244         /* Get NPA and NIX MSIX vector offsets */
245         otx2_mbox_alloc_msg_msix_offset(mbox);
246
247         rc = otx2_mbox_process_msg(mbox, (void *)&msix_rsp);
248
249         dev->nix_msixoff = msix_rsp->nix_msixoff;
250
251         return rc;
252 }
253
254 static inline int
255 otx2_eth_dev_lf_detach(struct otx2_mbox *mbox)
256 {
257         struct rsrc_detach_req *req;
258
259         req = otx2_mbox_alloc_msg_detach_resources(mbox);
260
261         /* Detach all except npa lf */
262         req->partial = true;
263         req->nixlf = true;
264         req->sso = true;
265         req->ssow = true;
266         req->timlfs = true;
267         req->cptlfs = true;
268
269         return otx2_mbox_process(mbox);
270 }
271
272 static int
273 otx2_eth_dev_init(struct rte_eth_dev *eth_dev)
274 {
275         struct otx2_eth_dev *dev = otx2_eth_pmd_priv(eth_dev);
276         struct rte_pci_device *pci_dev;
277         int rc, max_entries;
278
279         eth_dev->dev_ops = &otx2_eth_dev_ops;
280
281         /* For secondary processes, the primary has done all the work */
282         if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
283                 /* Setup callbacks for secondary process */
284                 otx2_eth_set_tx_function(eth_dev);
285                 otx2_eth_set_rx_function(eth_dev);
286                 return 0;
287         }
288
289         pci_dev = RTE_ETH_DEV_TO_PCI(eth_dev);
290
291         rte_eth_copy_pci_info(eth_dev, pci_dev);
292         eth_dev->data->dev_flags |= RTE_ETH_DEV_CLOSE_REMOVE;
293
294         /* Zero out everything after OTX2_DEV to allow proper dev_reset() */
295         memset(&dev->otx2_eth_dev_data_start, 0, sizeof(*dev) -
296                 offsetof(struct otx2_eth_dev, otx2_eth_dev_data_start));
297
298         /* Parse devargs string */
299         rc = otx2_ethdev_parse_devargs(eth_dev->device->devargs, dev);
300         if (rc) {
301                 otx2_err("Failed to parse devargs rc=%d", rc);
302                 goto error;
303         }
304
305         if (!dev->mbox_active) {
306                 /* Initialize the base otx2_dev object
307                  * only if already present
308                  */
309                 rc = otx2_dev_init(pci_dev, dev);
310                 if (rc) {
311                         otx2_err("Failed to initialize otx2_dev rc=%d", rc);
312                         goto error;
313                 }
314         }
315
316         /* Grab the NPA LF if required */
317         rc = otx2_npa_lf_init(pci_dev, dev);
318         if (rc)
319                 goto otx2_dev_uninit;
320
321         dev->configured = 0;
322         dev->drv_inited = true;
323         dev->base = dev->bar2 + (RVU_BLOCK_ADDR_NIX0 << 20);
324         dev->lmt_addr = dev->bar2 + (RVU_BLOCK_ADDR_LMT << 20);
325
326         /* Attach NIX LF */
327         rc = nix_lf_attach(dev);
328         if (rc)
329                 goto otx2_npa_uninit;
330
331         /* Get NIX MSIX offset */
332         rc = nix_lf_get_msix_offset(dev);
333         if (rc)
334                 goto otx2_npa_uninit;
335
336         /* Register LF irq handlers */
337         rc = otx2_nix_register_irqs(eth_dev);
338         if (rc)
339                 goto mbox_detach;
340
341         /* Get maximum number of supported MAC entries */
342         max_entries = otx2_cgx_mac_max_entries_get(dev);
343         if (max_entries < 0) {
344                 otx2_err("Failed to get max entries for mac addr");
345                 rc = -ENOTSUP;
346                 goto unregister_irq;
347         }
348
349         /* For VFs, returned max_entries will be 0. But to keep default MAC
350          * address, one entry must be allocated. So setting up to 1.
351          */
352         if (max_entries == 0)
353                 max_entries = 1;
354
355         eth_dev->data->mac_addrs = rte_zmalloc("mac_addr", max_entries *
356                                                RTE_ETHER_ADDR_LEN, 0);
357         if (eth_dev->data->mac_addrs == NULL) {
358                 otx2_err("Failed to allocate memory for mac addr");
359                 rc = -ENOMEM;
360                 goto unregister_irq;
361         }
362
363         dev->max_mac_entries = max_entries;
364
365         rc = otx2_nix_mac_addr_get(eth_dev, dev->mac_addr);
366         if (rc)
367                 goto free_mac_addrs;
368
369         /* Update the mac address */
370         memcpy(eth_dev->data->mac_addrs, dev->mac_addr, RTE_ETHER_ADDR_LEN);
371
372         /* Also sync same MAC address to CGX table */
373         otx2_cgx_mac_addr_set(eth_dev, &eth_dev->data->mac_addrs[0]);
374
375         dev->tx_offload_capa = nix_get_tx_offload_capa(dev);
376         dev->rx_offload_capa = nix_get_rx_offload_capa(dev);
377
378         if (otx2_dev_is_A0(dev)) {
379                 dev->hwcap |= OTX2_FIXUP_F_MIN_4K_Q;
380                 dev->hwcap |= OTX2_FIXUP_F_LIMIT_CQ_FULL;
381         }
382
383         otx2_nix_dbg("Port=%d pf=%d vf=%d ver=%s msix_off=%d hwcap=0x%" PRIx64
384                      " rxoffload_capa=0x%" PRIx64 " txoffload_capa=0x%" PRIx64,
385                      eth_dev->data->port_id, dev->pf, dev->vf,
386                      OTX2_ETH_DEV_PMD_VERSION, dev->nix_msixoff, dev->hwcap,
387                      dev->rx_offload_capa, dev->tx_offload_capa);
388         return 0;
389
390 free_mac_addrs:
391         rte_free(eth_dev->data->mac_addrs);
392 unregister_irq:
393         otx2_nix_unregister_irqs(eth_dev);
394 mbox_detach:
395         otx2_eth_dev_lf_detach(dev->mbox);
396 otx2_npa_uninit:
397         otx2_npa_lf_fini();
398 otx2_dev_uninit:
399         otx2_dev_fini(pci_dev, dev);
400 error:
401         otx2_err("Failed to init nix eth_dev rc=%d", rc);
402         return rc;
403 }
404
405 static int
406 otx2_eth_dev_uninit(struct rte_eth_dev *eth_dev, bool mbox_close)
407 {
408         struct otx2_eth_dev *dev = otx2_eth_pmd_priv(eth_dev);
409         struct rte_pci_device *pci_dev;
410         int rc;
411
412         /* Nothing to be done for secondary processes */
413         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
414                 return 0;
415
416         rc = nix_lf_free(dev);
417         if (rc)
418                 otx2_err("Failed to free nix lf, rc=%d", rc);
419
420         rc = otx2_npa_lf_fini();
421         if (rc)
422                 otx2_err("Failed to cleanup npa lf, rc=%d", rc);
423
424         rte_free(eth_dev->data->mac_addrs);
425         eth_dev->data->mac_addrs = NULL;
426         dev->drv_inited = false;
427
428         pci_dev = RTE_ETH_DEV_TO_PCI(eth_dev);
429         otx2_nix_unregister_irqs(eth_dev);
430
431         rc = otx2_eth_dev_lf_detach(dev->mbox);
432         if (rc)
433                 otx2_err("Failed to detach resources, rc=%d", rc);
434
435         /* Check if mbox close is needed */
436         if (!mbox_close)
437                 return 0;
438
439         if (otx2_npa_lf_active(dev) || otx2_dev_active_vfs(dev)) {
440                 /* Will be freed later by PMD */
441                 eth_dev->data->dev_private = NULL;
442                 return 0;
443         }
444
445         otx2_dev_fini(pci_dev, dev);
446         return 0;
447 }
448
449 static int
450 nix_remove(struct rte_pci_device *pci_dev)
451 {
452         struct rte_eth_dev *eth_dev;
453         struct otx2_idev_cfg *idev;
454         struct otx2_dev *otx2_dev;
455         int rc;
456
457         eth_dev = rte_eth_dev_allocated(pci_dev->device.name);
458         if (eth_dev) {
459                 /* Cleanup eth dev */
460                 rc = otx2_eth_dev_uninit(eth_dev, true);
461                 if (rc)
462                         return rc;
463
464                 rte_eth_dev_pci_release(eth_dev);
465         }
466
467         /* Nothing to be done for secondary processes */
468         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
469                 return 0;
470
471         /* Check for common resources */
472         idev = otx2_intra_dev_get_cfg();
473         if (!idev || !idev->npa_lf || idev->npa_lf->pci_dev != pci_dev)
474                 return 0;
475
476         otx2_dev = container_of(idev->npa_lf, struct otx2_dev, npalf);
477
478         if (otx2_npa_lf_active(otx2_dev) || otx2_dev_active_vfs(otx2_dev))
479                 goto exit;
480
481         /* Safe to cleanup mbox as no more users */
482         otx2_dev_fini(pci_dev, otx2_dev);
483         rte_free(otx2_dev);
484         return 0;
485
486 exit:
487         otx2_info("%s: common resource in use by other devices", pci_dev->name);
488         return -EAGAIN;
489 }
490
491 static int
492 nix_probe(struct rte_pci_driver *pci_drv, struct rte_pci_device *pci_dev)
493 {
494         int rc;
495
496         RTE_SET_USED(pci_drv);
497
498         rc = rte_eth_dev_pci_generic_probe(pci_dev, sizeof(struct otx2_eth_dev),
499                                            otx2_eth_dev_init);
500
501         /* On error on secondary, recheck if port exists in primary or
502          * in mid of detach state.
503          */
504         if (rte_eal_process_type() != RTE_PROC_PRIMARY && rc)
505                 if (!rte_eth_dev_allocated(pci_dev->device.name))
506                         return 0;
507         return rc;
508 }
509
510 static const struct rte_pci_id pci_nix_map[] = {
511         {
512                 RTE_PCI_DEVICE(PCI_VENDOR_ID_CAVIUM, PCI_DEVID_OCTEONTX2_RVU_PF)
513         },
514         {
515                 RTE_PCI_DEVICE(PCI_VENDOR_ID_CAVIUM, PCI_DEVID_OCTEONTX2_RVU_VF)
516         },
517         {
518                 RTE_PCI_DEVICE(PCI_VENDOR_ID_CAVIUM,
519                                PCI_DEVID_OCTEONTX2_RVU_AF_VF)
520         },
521         {
522                 .vendor_id = 0,
523         },
524 };
525
526 static struct rte_pci_driver pci_nix = {
527         .id_table = pci_nix_map,
528         .drv_flags = RTE_PCI_DRV_NEED_MAPPING | RTE_PCI_DRV_IOVA_AS_VA |
529                         RTE_PCI_DRV_INTR_LSC,
530         .probe = nix_probe,
531         .remove = nix_remove,
532 };
533
534 RTE_PMD_REGISTER_PCI(net_octeontx2, pci_nix);
535 RTE_PMD_REGISTER_PCI_TABLE(net_octeontx2, pci_nix_map);
536 RTE_PMD_REGISTER_KMOD_DEP(net_octeontx2, "vfio-pci");