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