common/cnxk: use common SA init API for default options
[dpdk.git] / drivers / common / cnxk / roc_nix_inl.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(C) 2021 Marvell.
3  */
4
5 #include "roc_api.h"
6 #include "roc_priv.h"
7
8 PLT_STATIC_ASSERT(ROC_NIX_INL_ONF_IPSEC_INB_SA_SZ ==
9                   1UL << ROC_NIX_INL_ONF_IPSEC_INB_SA_SZ_LOG2);
10 PLT_STATIC_ASSERT(ROC_NIX_INL_ONF_IPSEC_INB_SA_SZ == 512);
11 PLT_STATIC_ASSERT(ROC_NIX_INL_ONF_IPSEC_OUTB_SA_SZ ==
12                   1UL << ROC_NIX_INL_ONF_IPSEC_OUTB_SA_SZ_LOG2);
13 PLT_STATIC_ASSERT(ROC_NIX_INL_OT_IPSEC_INB_SA_SZ ==
14                   1UL << ROC_NIX_INL_OT_IPSEC_INB_SA_SZ_LOG2);
15 PLT_STATIC_ASSERT(ROC_NIX_INL_OT_IPSEC_INB_SA_SZ == 1024);
16 PLT_STATIC_ASSERT(ROC_NIX_INL_OT_IPSEC_OUTB_SA_SZ ==
17                   1UL << ROC_NIX_INL_OT_IPSEC_OUTB_SA_SZ_LOG2);
18
19 static int
20 nix_inl_inb_sa_tbl_setup(struct roc_nix *roc_nix)
21 {
22         uint16_t ipsec_in_max_spi = roc_nix->ipsec_in_max_spi;
23         struct nix *nix = roc_nix_to_nix_priv(roc_nix);
24         struct roc_nix_ipsec_cfg cfg;
25         size_t inb_sa_sz;
26         int rc, i;
27         void *sa;
28
29         /* CN9K SA size is different */
30         if (roc_model_is_cn9k())
31                 inb_sa_sz = ROC_NIX_INL_ONF_IPSEC_INB_SA_SZ;
32         else
33                 inb_sa_sz = ROC_NIX_INL_OT_IPSEC_INB_SA_SZ;
34
35         /* Alloc contiguous memory for Inbound SA's */
36         nix->inb_sa_sz = inb_sa_sz;
37         nix->inb_sa_base = plt_zmalloc(inb_sa_sz * ipsec_in_max_spi,
38                                        ROC_NIX_INL_SA_BASE_ALIGN);
39         if (!nix->inb_sa_base) {
40                 plt_err("Failed to allocate memory for Inbound SA");
41                 return -ENOMEM;
42         }
43         if (roc_model_is_cn10k()) {
44                 for (i = 0; i < ipsec_in_max_spi; i++) {
45                         sa = ((uint8_t *)nix->inb_sa_base) + (i * inb_sa_sz);
46                         roc_ot_ipsec_inb_sa_init(sa, true);
47                 }
48         }
49
50         memset(&cfg, 0, sizeof(cfg));
51         cfg.sa_size = inb_sa_sz;
52         cfg.iova = (uintptr_t)nix->inb_sa_base;
53         cfg.max_sa = ipsec_in_max_spi + 1;
54         cfg.tt = SSO_TT_ORDERED;
55
56         /* Setup device specific inb SA table */
57         rc = roc_nix_lf_inl_ipsec_cfg(roc_nix, &cfg, true);
58         if (rc) {
59                 plt_err("Failed to setup NIX Inbound SA conf, rc=%d", rc);
60                 goto free_mem;
61         }
62
63         return 0;
64 free_mem:
65         plt_free(nix->inb_sa_base);
66         nix->inb_sa_base = NULL;
67         return rc;
68 }
69
70 static int
71 nix_inl_sa_tbl_release(struct roc_nix *roc_nix)
72 {
73         struct nix *nix = roc_nix_to_nix_priv(roc_nix);
74         int rc;
75
76         rc = roc_nix_lf_inl_ipsec_cfg(roc_nix, NULL, false);
77         if (rc) {
78                 plt_err("Failed to disable Inbound inline ipsec, rc=%d", rc);
79                 return rc;
80         }
81
82         plt_free(nix->inb_sa_base);
83         nix->inb_sa_base = NULL;
84         return 0;
85 }
86
87 struct roc_cpt_lf *
88 roc_nix_inl_outb_lf_base_get(struct roc_nix *roc_nix)
89 {
90         struct nix *nix = roc_nix_to_nix_priv(roc_nix);
91
92         /* NIX Inline config needs to be done */
93         if (!nix->inl_outb_ena || !nix->cpt_lf_base)
94                 return NULL;
95
96         return (struct roc_cpt_lf *)nix->cpt_lf_base;
97 }
98
99 uintptr_t
100 roc_nix_inl_outb_sa_base_get(struct roc_nix *roc_nix)
101 {
102         struct nix *nix = roc_nix_to_nix_priv(roc_nix);
103
104         return (uintptr_t)nix->outb_sa_base;
105 }
106
107 uintptr_t
108 roc_nix_inl_inb_sa_base_get(struct roc_nix *roc_nix, bool inb_inl_dev)
109 {
110         struct idev_cfg *idev = idev_get_cfg();
111         struct nix_inl_dev *inl_dev;
112         struct nix *nix = NULL;
113
114         if (idev == NULL)
115                 return 0;
116
117         if (!inb_inl_dev && roc_nix == NULL)
118                 return -EINVAL;
119
120         if (roc_nix) {
121                 nix = roc_nix_to_nix_priv(roc_nix);
122                 if (!nix->inl_inb_ena)
123                         return 0;
124         }
125
126         if (inb_inl_dev) {
127                 inl_dev = idev->nix_inl_dev;
128                 /* Return inline dev sa base */
129                 if (inl_dev)
130                         return (uintptr_t)inl_dev->inb_sa_base;
131                 return 0;
132         }
133
134         return (uintptr_t)nix->inb_sa_base;
135 }
136
137 uint32_t
138 roc_nix_inl_inb_sa_max_spi(struct roc_nix *roc_nix, bool inb_inl_dev)
139 {
140         struct idev_cfg *idev = idev_get_cfg();
141         struct nix_inl_dev *inl_dev;
142         struct nix *nix;
143
144         if (idev == NULL)
145                 return 0;
146
147         if (!inb_inl_dev && roc_nix == NULL)
148                 return -EINVAL;
149
150         if (roc_nix) {
151                 nix = roc_nix_to_nix_priv(roc_nix);
152                 if (!nix->inl_inb_ena)
153                         return 0;
154         }
155
156         if (inb_inl_dev) {
157                 inl_dev = idev->nix_inl_dev;
158                 if (inl_dev)
159                         return inl_dev->ipsec_in_max_spi;
160                 return 0;
161         }
162
163         return roc_nix->ipsec_in_max_spi;
164 }
165
166 uint32_t
167 roc_nix_inl_inb_sa_sz(struct roc_nix *roc_nix, bool inl_dev_sa)
168 {
169         struct idev_cfg *idev = idev_get_cfg();
170         struct nix_inl_dev *inl_dev;
171         struct nix *nix;
172
173         if (idev == NULL)
174                 return 0;
175
176         if (!inl_dev_sa && roc_nix == NULL)
177                 return -EINVAL;
178
179         if (roc_nix) {
180                 nix = roc_nix_to_nix_priv(roc_nix);
181                 if (!inl_dev_sa)
182                         return nix->inb_sa_sz;
183         }
184
185         if (inl_dev_sa) {
186                 inl_dev = idev->nix_inl_dev;
187                 if (inl_dev)
188                         return inl_dev->inb_sa_sz;
189         }
190
191         return 0;
192 }
193
194 uintptr_t
195 roc_nix_inl_inb_sa_get(struct roc_nix *roc_nix, bool inb_inl_dev, uint32_t spi)
196 {
197         uintptr_t sa_base;
198         uint32_t max_spi;
199         uint64_t sz;
200
201         sa_base = roc_nix_inl_inb_sa_base_get(roc_nix, inb_inl_dev);
202         /* Check if SA base exists */
203         if (!sa_base)
204                 return 0;
205
206         /* Check if SPI is in range */
207         max_spi = roc_nix_inl_inb_sa_max_spi(roc_nix, inb_inl_dev);
208         if (spi > max_spi) {
209                 plt_err("Inbound SA SPI %u exceeds max %u", spi, max_spi);
210                 return 0;
211         }
212
213         /* Get SA size */
214         sz = roc_nix_inl_inb_sa_sz(roc_nix, inb_inl_dev);
215         if (!sz)
216                 return 0;
217
218         /* Basic logic of SPI->SA for now */
219         return (sa_base + (spi * sz));
220 }
221
222 int
223 roc_nix_inl_inb_init(struct roc_nix *roc_nix)
224 {
225         struct nix *nix = roc_nix_to_nix_priv(roc_nix);
226         struct idev_cfg *idev = idev_get_cfg();
227         struct roc_cpt *roc_cpt;
228         uint16_t param1;
229         int rc;
230
231         if (idev == NULL)
232                 return -ENOTSUP;
233
234         /* Unless we have another mechanism to trigger
235          * onetime Inline config in CPTPF, we cannot
236          * support without CPT being probed.
237          */
238         roc_cpt = idev->cpt;
239         if (!roc_cpt) {
240                 plt_err("Cannot support inline inbound, cryptodev not probed");
241                 return -ENOTSUP;
242         }
243
244         if (roc_model_is_cn9k()) {
245                 param1 = ROC_ONF_IPSEC_INB_MAX_L2_SZ;
246         } else {
247                 union roc_ot_ipsec_inb_param1 u;
248
249                 u.u16 = 0;
250                 u.s.esp_trailer_disable = 1;
251                 param1 = u.u16;
252         }
253
254         /* Do onetime Inbound Inline config in CPTPF */
255         rc = roc_cpt_inline_ipsec_inb_cfg(roc_cpt, param1, 0);
256         if (rc && rc != -EEXIST) {
257                 plt_err("Failed to setup inbound lf, rc=%d", rc);
258                 return rc;
259         }
260
261         /* Setup Inbound SA table */
262         rc = nix_inl_inb_sa_tbl_setup(roc_nix);
263         if (rc)
264                 return rc;
265
266         nix->inl_inb_ena = true;
267         return 0;
268 }
269
270 int
271 roc_nix_inl_inb_fini(struct roc_nix *roc_nix)
272 {
273         struct nix *nix = roc_nix_to_nix_priv(roc_nix);
274
275         if (!nix->inl_inb_ena)
276                 return 0;
277
278         nix->inl_inb_ena = false;
279
280         /* Flush Inbound CTX cache entries */
281         roc_nix_cpt_ctx_cache_sync(roc_nix);
282
283         /* Disable Inbound SA */
284         return nix_inl_sa_tbl_release(roc_nix);
285 }
286
287 int
288 roc_nix_inl_outb_init(struct roc_nix *roc_nix)
289 {
290         struct nix *nix = roc_nix_to_nix_priv(roc_nix);
291         struct idev_cfg *idev = idev_get_cfg();
292         struct roc_cpt_lf *lf_base, *lf;
293         struct dev *dev = &nix->dev;
294         struct msix_offset_rsp *rsp;
295         struct nix_inl_dev *inl_dev;
296         uint16_t sso_pffunc;
297         uint8_t eng_grpmask;
298         uint64_t blkaddr;
299         uint16_t nb_lf;
300         void *sa_base;
301         size_t sa_sz;
302         int i, j, rc;
303         void *sa;
304
305         if (idev == NULL)
306                 return -ENOTSUP;
307
308         nb_lf = roc_nix->outb_nb_crypto_qs;
309         blkaddr = nix->is_nix1 ? RVU_BLOCK_ADDR_CPT1 : RVU_BLOCK_ADDR_CPT0;
310
311         /* Retrieve inline device if present */
312         inl_dev = idev->nix_inl_dev;
313         sso_pffunc = inl_dev ? inl_dev->dev.pf_func : idev_sso_pffunc_get();
314         if (!sso_pffunc) {
315                 plt_err("Failed to setup inline outb, need either "
316                         "inline device or sso device");
317                 return -ENOTSUP;
318         }
319
320         /* Attach CPT LF for outbound */
321         rc = cpt_lfs_attach(dev, blkaddr, true, nb_lf);
322         if (rc) {
323                 plt_err("Failed to attach CPT LF for inline outb, rc=%d", rc);
324                 return rc;
325         }
326
327         /* Alloc CPT LF */
328         eng_grpmask = (1ULL << ROC_CPT_DFLT_ENG_GRP_SE |
329                        1ULL << ROC_CPT_DFLT_ENG_GRP_SE_IE |
330                        1ULL << ROC_CPT_DFLT_ENG_GRP_AE);
331         rc = cpt_lfs_alloc(dev, eng_grpmask, blkaddr, true);
332         if (rc) {
333                 plt_err("Failed to alloc CPT LF resources, rc=%d", rc);
334                 goto lf_detach;
335         }
336
337         /* Get msix offsets */
338         rc = cpt_get_msix_offset(dev, &rsp);
339         if (rc) {
340                 plt_err("Failed to get CPT LF msix offset, rc=%d", rc);
341                 goto lf_free;
342         }
343
344         mbox_memcpy(nix->cpt_msixoff,
345                     nix->is_nix1 ? rsp->cpt1_lf_msixoff : rsp->cptlf_msixoff,
346                     sizeof(nix->cpt_msixoff));
347
348         /* Alloc required num of cpt lfs */
349         lf_base = plt_zmalloc(nb_lf * sizeof(struct roc_cpt_lf), 0);
350         if (!lf_base) {
351                 plt_err("Failed to alloc cpt lf memory");
352                 rc = -ENOMEM;
353                 goto lf_free;
354         }
355
356         /* Initialize CPT LF's */
357         for (i = 0; i < nb_lf; i++) {
358                 lf = &lf_base[i];
359
360                 lf->lf_id = i;
361                 lf->nb_desc = roc_nix->outb_nb_desc;
362                 lf->dev = &nix->dev;
363                 lf->msixoff = nix->cpt_msixoff[i];
364                 lf->pci_dev = nix->pci_dev;
365
366                 /* Setup CPT LF instruction queue */
367                 rc = cpt_lf_init(lf);
368                 if (rc) {
369                         plt_err("Failed to initialize CPT LF, rc=%d", rc);
370                         goto lf_fini;
371                 }
372
373                 /* Associate this CPT LF with NIX PFFUNC */
374                 rc = cpt_lf_outb_cfg(dev, sso_pffunc, nix->dev.pf_func, i,
375                                      true);
376                 if (rc) {
377                         plt_err("Failed to setup CPT LF->(NIX,SSO) link, rc=%d",
378                                 rc);
379                         goto lf_fini;
380                 }
381
382                 /* Enable IQ */
383                 roc_cpt_iq_enable(lf);
384         }
385
386         if (!roc_nix->ipsec_out_max_sa)
387                 goto skip_sa_alloc;
388
389         /* CN9K SA size is different */
390         if (roc_model_is_cn9k())
391                 sa_sz = ROC_NIX_INL_ONF_IPSEC_OUTB_SA_SZ;
392         else
393                 sa_sz = ROC_NIX_INL_OT_IPSEC_OUTB_SA_SZ;
394         /* Alloc contiguous memory of outbound SA */
395         sa_base = plt_zmalloc(sa_sz * roc_nix->ipsec_out_max_sa,
396                               ROC_NIX_INL_SA_BASE_ALIGN);
397         if (!sa_base) {
398                 plt_err("Outbound SA base alloc failed");
399                 goto lf_fini;
400         }
401         if (roc_model_is_cn10k()) {
402                 for (i = 0; i < roc_nix->ipsec_out_max_sa; i++) {
403                         sa = ((uint8_t *)sa_base) + (i * sa_sz);
404                         roc_ot_ipsec_outb_sa_init(sa);
405                 }
406         }
407         nix->outb_sa_base = sa_base;
408         nix->outb_sa_sz = sa_sz;
409
410 skip_sa_alloc:
411
412         nix->cpt_lf_base = lf_base;
413         nix->nb_cpt_lf = nb_lf;
414         nix->outb_err_sso_pffunc = sso_pffunc;
415         nix->inl_outb_ena = true;
416         return 0;
417
418 lf_fini:
419         for (j = i - 1; j >= 0; j--)
420                 cpt_lf_fini(&lf_base[j]);
421         plt_free(lf_base);
422 lf_free:
423         rc |= cpt_lfs_free(dev);
424 lf_detach:
425         rc |= cpt_lfs_detach(dev);
426         return rc;
427 }
428
429 int
430 roc_nix_inl_outb_fini(struct roc_nix *roc_nix)
431 {
432         struct nix *nix = roc_nix_to_nix_priv(roc_nix);
433         struct roc_cpt_lf *lf_base = nix->cpt_lf_base;
434         struct dev *dev = &nix->dev;
435         int i, rc, ret = 0;
436
437         if (!nix->inl_outb_ena)
438                 return 0;
439
440         nix->inl_outb_ena = false;
441
442         /* Cleanup CPT LF instruction queue */
443         for (i = 0; i < nix->nb_cpt_lf; i++)
444                 cpt_lf_fini(&lf_base[i]);
445
446         /* Free LF resources */
447         rc = cpt_lfs_free(dev);
448         if (rc)
449                 plt_err("Failed to free CPT LF resources, rc=%d", rc);
450         ret |= rc;
451
452         /* Detach LF */
453         rc = cpt_lfs_detach(dev);
454         if (rc)
455                 plt_err("Failed to detach CPT LF, rc=%d", rc);
456
457         /* Free LF memory */
458         plt_free(lf_base);
459         nix->cpt_lf_base = NULL;
460         nix->nb_cpt_lf = 0;
461
462         /* Free outbound SA base */
463         plt_free(nix->outb_sa_base);
464         nix->outb_sa_base = NULL;
465
466         ret |= rc;
467         return ret;
468 }
469
470 bool
471 roc_nix_inl_dev_is_probed(void)
472 {
473         struct idev_cfg *idev = idev_get_cfg();
474
475         if (idev == NULL)
476                 return 0;
477
478         return !!idev->nix_inl_dev;
479 }
480
481 bool
482 roc_nix_inl_inb_is_enabled(struct roc_nix *roc_nix)
483 {
484         struct nix *nix = roc_nix_to_nix_priv(roc_nix);
485
486         return nix->inl_inb_ena;
487 }
488
489 bool
490 roc_nix_inl_outb_is_enabled(struct roc_nix *roc_nix)
491 {
492         struct nix *nix = roc_nix_to_nix_priv(roc_nix);
493
494         return nix->inl_outb_ena;
495 }
496
497 int
498 roc_nix_inl_dev_rq_get(struct roc_nix_rq *rq)
499 {
500         struct idev_cfg *idev = idev_get_cfg();
501         struct nix_inl_dev *inl_dev;
502         struct roc_nix_rq *inl_rq;
503         struct dev *dev;
504         int rc;
505
506         if (idev == NULL)
507                 return 0;
508
509         inl_dev = idev->nix_inl_dev;
510         /* Nothing to do if no inline device */
511         if (!inl_dev)
512                 return 0;
513
514         /* Just take reference if already inited */
515         if (inl_dev->rq_refs) {
516                 inl_dev->rq_refs++;
517                 rq->inl_dev_ref = true;
518                 return 0;
519         }
520
521         dev = &inl_dev->dev;
522         inl_rq = &inl_dev->rq;
523         memset(inl_rq, 0, sizeof(struct roc_nix_rq));
524
525         /* Take RQ pool attributes from the first ethdev RQ */
526         inl_rq->qid = 0;
527         inl_rq->aura_handle = rq->aura_handle;
528         inl_rq->first_skip = rq->first_skip;
529         inl_rq->later_skip = rq->later_skip;
530         inl_rq->lpb_size = rq->lpb_size;
531
532         if (!roc_model_is_cn9k()) {
533                 uint64_t aura_limit =
534                         roc_npa_aura_op_limit_get(inl_rq->aura_handle);
535                 uint64_t aura_shift = plt_log2_u32(aura_limit);
536
537                 if (aura_shift < 8)
538                         aura_shift = 0;
539                 else
540                         aura_shift = aura_shift - 8;
541
542                 /* Set first pass RQ to drop when half of the buffers are in
543                  * use to avoid metabuf alloc failure. This is needed as long
544                  * as we cannot use different
545                  */
546                 inl_rq->red_pass = (aura_limit / 2) >> aura_shift;
547                 inl_rq->red_drop = ((aura_limit / 2) - 1) >> aura_shift;
548         }
549
550         /* Enable IPSec */
551         inl_rq->ipsech_ena = true;
552
553         inl_rq->flow_tag_width = 20;
554         /* Special tag mask */
555         inl_rq->tag_mask = 0xFFF00000;
556         inl_rq->tt = SSO_TT_ORDERED;
557         inl_rq->hwgrp = 0;
558         inl_rq->wqe_skip = inl_dev->wqe_skip;
559         inl_rq->sso_ena = true;
560
561         /* Prepare and send RQ init mbox */
562         if (roc_model_is_cn9k())
563                 rc = nix_rq_cn9k_cfg(dev, inl_rq, inl_dev->qints, false, true);
564         else
565                 rc = nix_rq_cfg(dev, inl_rq, inl_dev->qints, false, true);
566         if (rc) {
567                 plt_err("Failed to prepare aq_enq msg, rc=%d", rc);
568                 return rc;
569         }
570
571         rc = mbox_process(dev->mbox);
572         if (rc) {
573                 plt_err("Failed to send aq_enq msg, rc=%d", rc);
574                 return rc;
575         }
576
577         inl_dev->rq_refs++;
578         rq->inl_dev_ref = true;
579         return 0;
580 }
581
582 int
583 roc_nix_inl_dev_rq_put(struct roc_nix_rq *rq)
584 {
585         struct idev_cfg *idev = idev_get_cfg();
586         struct nix_inl_dev *inl_dev;
587         struct roc_nix_rq *inl_rq;
588         struct dev *dev;
589         int rc;
590
591         if (idev == NULL)
592                 return 0;
593
594         if (!rq->inl_dev_ref)
595                 return 0;
596
597         inl_dev = idev->nix_inl_dev;
598         /* Inline device should be there if we have ref */
599         if (!inl_dev) {
600                 plt_err("Failed to find inline device with refs");
601                 return -EFAULT;
602         }
603
604         rq->inl_dev_ref = false;
605         inl_dev->rq_refs--;
606         if (inl_dev->rq_refs)
607                 return 0;
608
609         dev = &inl_dev->dev;
610         inl_rq = &inl_dev->rq;
611         /* There are no more references, disable RQ */
612         rc = nix_rq_ena_dis(dev, inl_rq, false);
613         if (rc)
614                 plt_err("Failed to disable inline device rq, rc=%d", rc);
615
616         /* Flush NIX LF for CN10K */
617         nix_rq_vwqe_flush(rq, inl_dev->vwqe_interval);
618
619         return rc;
620 }
621
622 uint64_t
623 roc_nix_inl_dev_rq_limit_get(void)
624 {
625         struct idev_cfg *idev = idev_get_cfg();
626         struct nix_inl_dev *inl_dev;
627         struct roc_nix_rq *inl_rq;
628
629         if (!idev || !idev->nix_inl_dev)
630                 return 0;
631
632         inl_dev = idev->nix_inl_dev;
633         if (!inl_dev->rq_refs)
634                 return 0;
635
636         inl_rq = &inl_dev->rq;
637
638         return roc_npa_aura_op_limit_get(inl_rq->aura_handle);
639 }
640
641 void
642 roc_nix_inb_mode_set(struct roc_nix *roc_nix, bool use_inl_dev)
643 {
644         struct nix *nix = roc_nix_to_nix_priv(roc_nix);
645
646         /* Info used by NPC flow rule add */
647         nix->inb_inl_dev = use_inl_dev;
648 }
649
650 bool
651 roc_nix_inb_is_with_inl_dev(struct roc_nix *roc_nix)
652 {
653         struct nix *nix = roc_nix_to_nix_priv(roc_nix);
654
655         return nix->inb_inl_dev;
656 }
657
658 struct roc_nix_rq *
659 roc_nix_inl_dev_rq(void)
660 {
661         struct idev_cfg *idev = idev_get_cfg();
662         struct nix_inl_dev *inl_dev;
663
664         if (idev != NULL) {
665                 inl_dev = idev->nix_inl_dev;
666                 if (inl_dev != NULL && inl_dev->rq_refs)
667                         return &inl_dev->rq;
668         }
669
670         return NULL;
671 }
672
673 uint16_t __roc_api
674 roc_nix_inl_outb_sso_pffunc_get(struct roc_nix *roc_nix)
675 {
676         struct nix *nix = roc_nix_to_nix_priv(roc_nix);
677
678         return nix->outb_err_sso_pffunc;
679 }
680
681 int
682 roc_nix_inl_cb_register(roc_nix_inl_sso_work_cb_t cb, void *args)
683 {
684         struct idev_cfg *idev = idev_get_cfg();
685         struct nix_inl_dev *inl_dev;
686
687         if (idev == NULL)
688                 return -EIO;
689
690         inl_dev = idev->nix_inl_dev;
691         if (!inl_dev)
692                 return -EIO;
693
694         /* Be silent if registration called with same cb and args */
695         if (inl_dev->work_cb == cb && inl_dev->cb_args == args)
696                 return 0;
697
698         /* Don't allow registration again if registered with different cb */
699         if (inl_dev->work_cb)
700                 return -EBUSY;
701
702         inl_dev->work_cb = cb;
703         inl_dev->cb_args = args;
704         return 0;
705 }
706
707 int
708 roc_nix_inl_cb_unregister(roc_nix_inl_sso_work_cb_t cb, void *args)
709 {
710         struct idev_cfg *idev = idev_get_cfg();
711         struct nix_inl_dev *inl_dev;
712
713         if (idev == NULL)
714                 return -ENOENT;
715
716         inl_dev = idev->nix_inl_dev;
717         if (!inl_dev)
718                 return -ENOENT;
719
720         if (inl_dev->work_cb != cb || inl_dev->cb_args != args)
721                 return -EINVAL;
722
723         inl_dev->work_cb = NULL;
724         inl_dev->cb_args = NULL;
725         return 0;
726 }
727
728 int
729 roc_nix_inl_inb_tag_update(struct roc_nix *roc_nix, uint32_t tag_const,
730                            uint8_t tt)
731 {
732         struct nix *nix = roc_nix_to_nix_priv(roc_nix);
733         struct roc_nix_ipsec_cfg cfg;
734
735         /* Be silent if inline inbound not enabled */
736         if (!nix->inl_inb_ena)
737                 return 0;
738
739         memset(&cfg, 0, sizeof(cfg));
740         cfg.sa_size = nix->inb_sa_sz;
741         cfg.iova = (uintptr_t)nix->inb_sa_base;
742         cfg.max_sa = roc_nix->ipsec_in_max_spi + 1;
743         cfg.tt = tt;
744         cfg.tag_const = tag_const;
745
746         return roc_nix_lf_inl_ipsec_cfg(roc_nix, &cfg, true);
747 }
748
749 int
750 roc_nix_inl_sa_sync(struct roc_nix *roc_nix, void *sa, bool inb,
751                     enum roc_nix_inl_sa_sync_op op)
752 {
753         struct idev_cfg *idev = idev_get_cfg();
754         struct nix_inl_dev *inl_dev = NULL;
755         struct roc_cpt_lf *outb_lf = NULL;
756         union cpt_lf_ctx_reload reload;
757         union cpt_lf_ctx_flush flush;
758         bool get_inl_lf = true;
759         uintptr_t rbase;
760         struct nix *nix;
761
762         /* Nothing much to do on cn9k */
763         if (roc_model_is_cn9k()) {
764                 plt_atomic_thread_fence(__ATOMIC_ACQ_REL);
765                 return 0;
766         }
767
768         if (idev)
769                 inl_dev = idev->nix_inl_dev;
770
771         if (!inl_dev && roc_nix == NULL)
772                 return -EINVAL;
773
774         if (roc_nix) {
775                 nix = roc_nix_to_nix_priv(roc_nix);
776                 outb_lf = nix->cpt_lf_base;
777                 if (inb && !nix->inb_inl_dev)
778                         get_inl_lf = false;
779         }
780
781         if (inb && get_inl_lf) {
782                 outb_lf = NULL;
783                 if (inl_dev && inl_dev->attach_cptlf)
784                         outb_lf = &inl_dev->cpt_lf;
785         }
786
787         if (outb_lf) {
788                 rbase = outb_lf->rbase;
789
790                 flush.u = 0;
791                 reload.u = 0;
792                 switch (op) {
793                 case ROC_NIX_INL_SA_OP_FLUSH_INVAL:
794                         flush.s.inval = 1;
795                         /* fall through */
796                 case ROC_NIX_INL_SA_OP_FLUSH:
797                         flush.s.cptr = ((uintptr_t)sa) >> 7;
798                         plt_write64(flush.u, rbase + CPT_LF_CTX_FLUSH);
799                         break;
800                 case ROC_NIX_INL_SA_OP_RELOAD:
801                         reload.s.cptr = ((uintptr_t)sa) >> 7;
802                         plt_write64(reload.u, rbase + CPT_LF_CTX_RELOAD);
803                         break;
804                 default:
805                         return -EINVAL;
806                 }
807                 return 0;
808         }
809         plt_err("Could not get CPT LF for SA sync");
810         return -ENOTSUP;
811 }
812
813 int
814 roc_nix_inl_ctx_write(struct roc_nix *roc_nix, void *sa_dptr, void *sa_cptr,
815                       bool inb, uint16_t sa_len)
816 {
817         struct idev_cfg *idev = idev_get_cfg();
818         struct nix_inl_dev *inl_dev = NULL;
819         struct roc_cpt_lf *outb_lf = NULL;
820         union cpt_lf_ctx_flush flush;
821         bool get_inl_lf = true;
822         uintptr_t rbase;
823         struct nix *nix;
824         int rc;
825
826         /* Nothing much to do on cn9k */
827         if (roc_model_is_cn9k()) {
828                 plt_atomic_thread_fence(__ATOMIC_ACQ_REL);
829                 return 0;
830         }
831
832         if (idev)
833                 inl_dev = idev->nix_inl_dev;
834
835         if (!inl_dev && roc_nix == NULL)
836                 return -EINVAL;
837
838         if (roc_nix) {
839                 nix = roc_nix_to_nix_priv(roc_nix);
840                 outb_lf = nix->cpt_lf_base;
841
842                 if (inb && !nix->inb_inl_dev)
843                         get_inl_lf = false;
844         }
845
846         if (inb && get_inl_lf) {
847                 outb_lf = NULL;
848                 if (inl_dev && inl_dev->attach_cptlf)
849                         outb_lf = &inl_dev->cpt_lf;
850         }
851
852         if (outb_lf) {
853                 rbase = outb_lf->rbase;
854                 flush.u = 0;
855
856                 rc = roc_cpt_ctx_write(outb_lf, sa_dptr, sa_cptr, sa_len);
857                 if (rc)
858                         return rc;
859                 /* Trigger CTX flush to write dirty data back to DRAM */
860                 flush.s.cptr = ((uintptr_t)sa_cptr) >> 7;
861                 plt_write64(flush.u, rbase + CPT_LF_CTX_FLUSH);
862
863                 return 0;
864         }
865         plt_nix_dbg("Could not get CPT LF for CTX write");
866         return -ENOTSUP;
867 }
868
869 void
870 roc_nix_inl_dev_lock(void)
871 {
872         struct idev_cfg *idev = idev_get_cfg();
873
874         if (idev != NULL)
875                 plt_spinlock_lock(&idev->nix_inl_dev_lock);
876 }
877
878 void
879 roc_nix_inl_dev_unlock(void)
880 {
881         struct idev_cfg *idev = idev_get_cfg();
882
883         if (idev != NULL)
884                 plt_spinlock_unlock(&idev->nix_inl_dev_lock);
885 }