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