net/octeontx2: handle queue specific error interrupts
[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                 oxt2_nix_unregister_queue_irqs(eth_dev);
168                 nix_lf_free(dev);
169         }
170
171         if (otx2_dev_is_A0(dev) &&
172             (txmode->offloads & DEV_TX_OFFLOAD_SCTP_CKSUM) &&
173             ((txmode->offloads & DEV_TX_OFFLOAD_OUTER_IPV4_CKSUM) ||
174             (txmode->offloads & DEV_TX_OFFLOAD_OUTER_UDP_CKSUM))) {
175                 otx2_err("Outer IP and SCTP checksum unsupported");
176                 rc = -EINVAL;
177                 goto fail;
178         }
179
180         dev->rx_offloads = rxmode->offloads;
181         dev->tx_offloads = txmode->offloads;
182         dev->rss_info.rss_grps = NIX_RSS_GRPS;
183
184         nb_rxq = RTE_MAX(data->nb_rx_queues, 1);
185         nb_txq = RTE_MAX(data->nb_tx_queues, 1);
186
187         /* Alloc a nix lf */
188         rc = nix_lf_alloc(dev, nb_rxq, nb_txq);
189         if (rc) {
190                 otx2_err("Failed to init nix_lf rc=%d", rc);
191                 goto fail;
192         }
193
194         /* Register queue IRQs */
195         rc = oxt2_nix_register_queue_irqs(eth_dev);
196         if (rc) {
197                 otx2_err("Failed to register queue interrupts rc=%d", rc);
198                 goto free_nix_lf;
199         }
200
201         /* Update the mac address */
202         ea = eth_dev->data->mac_addrs;
203         memcpy(ea, dev->mac_addr, RTE_ETHER_ADDR_LEN);
204         if (rte_is_zero_ether_addr(ea))
205                 rte_eth_random_addr((uint8_t *)ea);
206
207         rte_ether_format_addr(ea_fmt, RTE_ETHER_ADDR_FMT_SIZE, ea);
208
209         otx2_nix_dbg("Configured port%d mac=%s nb_rxq=%d nb_txq=%d"
210                 " rx_offloads=0x%" PRIx64 " tx_offloads=0x%" PRIx64 ""
211                 " rx_flags=0x%x tx_flags=0x%x",
212                 eth_dev->data->port_id, ea_fmt, nb_rxq,
213                 nb_txq, dev->rx_offloads, dev->tx_offloads,
214                 dev->rx_offload_flags, dev->tx_offload_flags);
215
216         /* All good */
217         dev->configured = 1;
218         dev->configured_nb_rx_qs = data->nb_rx_queues;
219         dev->configured_nb_tx_qs = data->nb_tx_queues;
220         return 0;
221
222 free_nix_lf:
223         rc = nix_lf_free(dev);
224 fail:
225         return rc;
226 }
227
228 /* Initialize and register driver with DPDK Application */
229 static const struct eth_dev_ops otx2_eth_dev_ops = {
230         .dev_infos_get            = otx2_nix_info_get,
231         .dev_configure            = otx2_nix_configure,
232 };
233
234 static inline int
235 nix_lf_attach(struct otx2_eth_dev *dev)
236 {
237         struct otx2_mbox *mbox = dev->mbox;
238         struct rsrc_attach_req *req;
239
240         /* Attach NIX(lf) */
241         req = otx2_mbox_alloc_msg_attach_resources(mbox);
242         req->modify = true;
243         req->nixlf = true;
244
245         return otx2_mbox_process(mbox);
246 }
247
248 static inline int
249 nix_lf_get_msix_offset(struct otx2_eth_dev *dev)
250 {
251         struct otx2_mbox *mbox = dev->mbox;
252         struct msix_offset_rsp *msix_rsp;
253         int rc;
254
255         /* Get NPA and NIX MSIX vector offsets */
256         otx2_mbox_alloc_msg_msix_offset(mbox);
257
258         rc = otx2_mbox_process_msg(mbox, (void *)&msix_rsp);
259
260         dev->nix_msixoff = msix_rsp->nix_msixoff;
261
262         return rc;
263 }
264
265 static inline int
266 otx2_eth_dev_lf_detach(struct otx2_mbox *mbox)
267 {
268         struct rsrc_detach_req *req;
269
270         req = otx2_mbox_alloc_msg_detach_resources(mbox);
271
272         /* Detach all except npa lf */
273         req->partial = true;
274         req->nixlf = true;
275         req->sso = true;
276         req->ssow = true;
277         req->timlfs = true;
278         req->cptlfs = true;
279
280         return otx2_mbox_process(mbox);
281 }
282
283 static int
284 otx2_eth_dev_init(struct rte_eth_dev *eth_dev)
285 {
286         struct otx2_eth_dev *dev = otx2_eth_pmd_priv(eth_dev);
287         struct rte_pci_device *pci_dev;
288         int rc, max_entries;
289
290         eth_dev->dev_ops = &otx2_eth_dev_ops;
291
292         /* For secondary processes, the primary has done all the work */
293         if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
294                 /* Setup callbacks for secondary process */
295                 otx2_eth_set_tx_function(eth_dev);
296                 otx2_eth_set_rx_function(eth_dev);
297                 return 0;
298         }
299
300         pci_dev = RTE_ETH_DEV_TO_PCI(eth_dev);
301
302         rte_eth_copy_pci_info(eth_dev, pci_dev);
303         eth_dev->data->dev_flags |= RTE_ETH_DEV_CLOSE_REMOVE;
304
305         /* Zero out everything after OTX2_DEV to allow proper dev_reset() */
306         memset(&dev->otx2_eth_dev_data_start, 0, sizeof(*dev) -
307                 offsetof(struct otx2_eth_dev, otx2_eth_dev_data_start));
308
309         /* Parse devargs string */
310         rc = otx2_ethdev_parse_devargs(eth_dev->device->devargs, dev);
311         if (rc) {
312                 otx2_err("Failed to parse devargs rc=%d", rc);
313                 goto error;
314         }
315
316         if (!dev->mbox_active) {
317                 /* Initialize the base otx2_dev object
318                  * only if already present
319                  */
320                 rc = otx2_dev_init(pci_dev, dev);
321                 if (rc) {
322                         otx2_err("Failed to initialize otx2_dev rc=%d", rc);
323                         goto error;
324                 }
325         }
326
327         /* Grab the NPA LF if required */
328         rc = otx2_npa_lf_init(pci_dev, dev);
329         if (rc)
330                 goto otx2_dev_uninit;
331
332         dev->configured = 0;
333         dev->drv_inited = true;
334         dev->base = dev->bar2 + (RVU_BLOCK_ADDR_NIX0 << 20);
335         dev->lmt_addr = dev->bar2 + (RVU_BLOCK_ADDR_LMT << 20);
336
337         /* Attach NIX LF */
338         rc = nix_lf_attach(dev);
339         if (rc)
340                 goto otx2_npa_uninit;
341
342         /* Get NIX MSIX offset */
343         rc = nix_lf_get_msix_offset(dev);
344         if (rc)
345                 goto otx2_npa_uninit;
346
347         /* Register LF irq handlers */
348         rc = otx2_nix_register_irqs(eth_dev);
349         if (rc)
350                 goto mbox_detach;
351
352         /* Get maximum number of supported MAC entries */
353         max_entries = otx2_cgx_mac_max_entries_get(dev);
354         if (max_entries < 0) {
355                 otx2_err("Failed to get max entries for mac addr");
356                 rc = -ENOTSUP;
357                 goto unregister_irq;
358         }
359
360         /* For VFs, returned max_entries will be 0. But to keep default MAC
361          * address, one entry must be allocated. So setting up to 1.
362          */
363         if (max_entries == 0)
364                 max_entries = 1;
365
366         eth_dev->data->mac_addrs = rte_zmalloc("mac_addr", max_entries *
367                                                RTE_ETHER_ADDR_LEN, 0);
368         if (eth_dev->data->mac_addrs == NULL) {
369                 otx2_err("Failed to allocate memory for mac addr");
370                 rc = -ENOMEM;
371                 goto unregister_irq;
372         }
373
374         dev->max_mac_entries = max_entries;
375
376         rc = otx2_nix_mac_addr_get(eth_dev, dev->mac_addr);
377         if (rc)
378                 goto free_mac_addrs;
379
380         /* Update the mac address */
381         memcpy(eth_dev->data->mac_addrs, dev->mac_addr, RTE_ETHER_ADDR_LEN);
382
383         /* Also sync same MAC address to CGX table */
384         otx2_cgx_mac_addr_set(eth_dev, &eth_dev->data->mac_addrs[0]);
385
386         dev->tx_offload_capa = nix_get_tx_offload_capa(dev);
387         dev->rx_offload_capa = nix_get_rx_offload_capa(dev);
388
389         if (otx2_dev_is_A0(dev)) {
390                 dev->hwcap |= OTX2_FIXUP_F_MIN_4K_Q;
391                 dev->hwcap |= OTX2_FIXUP_F_LIMIT_CQ_FULL;
392         }
393
394         otx2_nix_dbg("Port=%d pf=%d vf=%d ver=%s msix_off=%d hwcap=0x%" PRIx64
395                      " rxoffload_capa=0x%" PRIx64 " txoffload_capa=0x%" PRIx64,
396                      eth_dev->data->port_id, dev->pf, dev->vf,
397                      OTX2_ETH_DEV_PMD_VERSION, dev->nix_msixoff, dev->hwcap,
398                      dev->rx_offload_capa, dev->tx_offload_capa);
399         return 0;
400
401 free_mac_addrs:
402         rte_free(eth_dev->data->mac_addrs);
403 unregister_irq:
404         otx2_nix_unregister_irqs(eth_dev);
405 mbox_detach:
406         otx2_eth_dev_lf_detach(dev->mbox);
407 otx2_npa_uninit:
408         otx2_npa_lf_fini();
409 otx2_dev_uninit:
410         otx2_dev_fini(pci_dev, dev);
411 error:
412         otx2_err("Failed to init nix eth_dev rc=%d", rc);
413         return rc;
414 }
415
416 static int
417 otx2_eth_dev_uninit(struct rte_eth_dev *eth_dev, bool mbox_close)
418 {
419         struct otx2_eth_dev *dev = otx2_eth_pmd_priv(eth_dev);
420         struct rte_pci_device *pci_dev;
421         int rc;
422
423         /* Nothing to be done for secondary processes */
424         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
425                 return 0;
426
427         /* Unregister queue irqs */
428         oxt2_nix_unregister_queue_irqs(eth_dev);
429
430         rc = nix_lf_free(dev);
431         if (rc)
432                 otx2_err("Failed to free nix lf, rc=%d", rc);
433
434         rc = otx2_npa_lf_fini();
435         if (rc)
436                 otx2_err("Failed to cleanup npa lf, rc=%d", rc);
437
438         rte_free(eth_dev->data->mac_addrs);
439         eth_dev->data->mac_addrs = NULL;
440         dev->drv_inited = false;
441
442         pci_dev = RTE_ETH_DEV_TO_PCI(eth_dev);
443         otx2_nix_unregister_irqs(eth_dev);
444
445         rc = otx2_eth_dev_lf_detach(dev->mbox);
446         if (rc)
447                 otx2_err("Failed to detach resources, rc=%d", rc);
448
449         /* Check if mbox close is needed */
450         if (!mbox_close)
451                 return 0;
452
453         if (otx2_npa_lf_active(dev) || otx2_dev_active_vfs(dev)) {
454                 /* Will be freed later by PMD */
455                 eth_dev->data->dev_private = NULL;
456                 return 0;
457         }
458
459         otx2_dev_fini(pci_dev, dev);
460         return 0;
461 }
462
463 static int
464 nix_remove(struct rte_pci_device *pci_dev)
465 {
466         struct rte_eth_dev *eth_dev;
467         struct otx2_idev_cfg *idev;
468         struct otx2_dev *otx2_dev;
469         int rc;
470
471         eth_dev = rte_eth_dev_allocated(pci_dev->device.name);
472         if (eth_dev) {
473                 /* Cleanup eth dev */
474                 rc = otx2_eth_dev_uninit(eth_dev, true);
475                 if (rc)
476                         return rc;
477
478                 rte_eth_dev_pci_release(eth_dev);
479         }
480
481         /* Nothing to be done for secondary processes */
482         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
483                 return 0;
484
485         /* Check for common resources */
486         idev = otx2_intra_dev_get_cfg();
487         if (!idev || !idev->npa_lf || idev->npa_lf->pci_dev != pci_dev)
488                 return 0;
489
490         otx2_dev = container_of(idev->npa_lf, struct otx2_dev, npalf);
491
492         if (otx2_npa_lf_active(otx2_dev) || otx2_dev_active_vfs(otx2_dev))
493                 goto exit;
494
495         /* Safe to cleanup mbox as no more users */
496         otx2_dev_fini(pci_dev, otx2_dev);
497         rte_free(otx2_dev);
498         return 0;
499
500 exit:
501         otx2_info("%s: common resource in use by other devices", pci_dev->name);
502         return -EAGAIN;
503 }
504
505 static int
506 nix_probe(struct rte_pci_driver *pci_drv, struct rte_pci_device *pci_dev)
507 {
508         int rc;
509
510         RTE_SET_USED(pci_drv);
511
512         rc = rte_eth_dev_pci_generic_probe(pci_dev, sizeof(struct otx2_eth_dev),
513                                            otx2_eth_dev_init);
514
515         /* On error on secondary, recheck if port exists in primary or
516          * in mid of detach state.
517          */
518         if (rte_eal_process_type() != RTE_PROC_PRIMARY && rc)
519                 if (!rte_eth_dev_allocated(pci_dev->device.name))
520                         return 0;
521         return rc;
522 }
523
524 static const struct rte_pci_id pci_nix_map[] = {
525         {
526                 RTE_PCI_DEVICE(PCI_VENDOR_ID_CAVIUM, PCI_DEVID_OCTEONTX2_RVU_PF)
527         },
528         {
529                 RTE_PCI_DEVICE(PCI_VENDOR_ID_CAVIUM, PCI_DEVID_OCTEONTX2_RVU_VF)
530         },
531         {
532                 RTE_PCI_DEVICE(PCI_VENDOR_ID_CAVIUM,
533                                PCI_DEVID_OCTEONTX2_RVU_AF_VF)
534         },
535         {
536                 .vendor_id = 0,
537         },
538 };
539
540 static struct rte_pci_driver pci_nix = {
541         .id_table = pci_nix_map,
542         .drv_flags = RTE_PCI_DRV_NEED_MAPPING | RTE_PCI_DRV_IOVA_AS_VA |
543                         RTE_PCI_DRV_INTR_LSC,
544         .probe = nix_probe,
545         .remove = nix_remove,
546 };
547
548 RTE_PMD_REGISTER_PCI(net_octeontx2, pci_nix);
549 RTE_PMD_REGISTER_PCI_TABLE(net_octeontx2, pci_nix_map);
550 RTE_PMD_REGISTER_KMOD_DEP(net_octeontx2, "vfio-pci");