net/ice: refine debug build option
[dpdk.git] / drivers / common / cnxk / roc_npa.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 void
9 roc_npa_aura_op_range_set(uint64_t aura_handle, uint64_t start_iova,
10                           uint64_t end_iova)
11 {
12         const uint64_t start = roc_npa_aura_handle_to_base(aura_handle) +
13                                NPA_LF_POOL_OP_PTR_START0;
14         const uint64_t end = roc_npa_aura_handle_to_base(aura_handle) +
15                              NPA_LF_POOL_OP_PTR_END0;
16         uint64_t reg = roc_npa_aura_handle_to_aura(aura_handle);
17         struct npa_lf *lf = idev_npa_obj_get();
18         struct npa_aura_lim *lim;
19
20         PLT_ASSERT(lf);
21         lim = lf->aura_lim;
22
23         lim[reg].ptr_start = PLT_MIN(lim[reg].ptr_start, start_iova);
24         lim[reg].ptr_end = PLT_MAX(lim[reg].ptr_end, end_iova);
25
26         roc_store_pair(lim[reg].ptr_start, reg, start);
27         roc_store_pair(lim[reg].ptr_end, reg, end);
28 }
29
30 static int
31 npa_aura_pool_init(struct mbox *mbox, uint32_t aura_id, struct npa_aura_s *aura,
32                    struct npa_pool_s *pool)
33 {
34         struct npa_aq_enq_req *aura_init_req, *pool_init_req;
35         struct npa_aq_enq_rsp *aura_init_rsp, *pool_init_rsp;
36         struct mbox_dev *mdev = &mbox->dev[0];
37         int rc = -ENOSPC, off;
38
39         aura_init_req = mbox_alloc_msg_npa_aq_enq(mbox);
40         if (aura_init_req == NULL)
41                 return rc;
42         aura_init_req->aura_id = aura_id;
43         aura_init_req->ctype = NPA_AQ_CTYPE_AURA;
44         aura_init_req->op = NPA_AQ_INSTOP_INIT;
45         mbox_memcpy(&aura_init_req->aura, aura, sizeof(*aura));
46
47         pool_init_req = mbox_alloc_msg_npa_aq_enq(mbox);
48         if (pool_init_req == NULL)
49                 return rc;
50         pool_init_req->aura_id = aura_id;
51         pool_init_req->ctype = NPA_AQ_CTYPE_POOL;
52         pool_init_req->op = NPA_AQ_INSTOP_INIT;
53         mbox_memcpy(&pool_init_req->pool, pool, sizeof(*pool));
54
55         rc = mbox_process(mbox);
56         if (rc < 0)
57                 return rc;
58
59         off = mbox->rx_start +
60               PLT_ALIGN(sizeof(struct mbox_hdr), MBOX_MSG_ALIGN);
61         aura_init_rsp = (struct npa_aq_enq_rsp *)((uintptr_t)mdev->mbase + off);
62         off = mbox->rx_start + aura_init_rsp->hdr.next_msgoff;
63         pool_init_rsp = (struct npa_aq_enq_rsp *)((uintptr_t)mdev->mbase + off);
64
65         if (aura_init_rsp->hdr.rc == 0 && pool_init_rsp->hdr.rc == 0)
66                 return 0;
67         else
68                 return NPA_ERR_AURA_POOL_INIT;
69 }
70
71 static int
72 npa_aura_pool_fini(struct mbox *mbox, uint32_t aura_id, uint64_t aura_handle)
73 {
74         struct npa_aq_enq_req *aura_req, *pool_req;
75         struct npa_aq_enq_rsp *aura_rsp, *pool_rsp;
76         struct mbox_dev *mdev = &mbox->dev[0];
77         struct ndc_sync_op *ndc_req;
78         int rc = -ENOSPC, off;
79         uint64_t ptr;
80
81         /* Procedure for disabling an aura/pool */
82         plt_delay_us(10);
83
84         /* Clear all the pointers from the aura */
85         do {
86                 ptr = roc_npa_aura_op_alloc(aura_handle, 0);
87         } while (ptr);
88
89         pool_req = mbox_alloc_msg_npa_aq_enq(mbox);
90         if (pool_req == NULL)
91                 return rc;
92         pool_req->aura_id = aura_id;
93         pool_req->ctype = NPA_AQ_CTYPE_POOL;
94         pool_req->op = NPA_AQ_INSTOP_WRITE;
95         pool_req->pool.ena = 0;
96         pool_req->pool_mask.ena = ~pool_req->pool_mask.ena;
97
98         aura_req = mbox_alloc_msg_npa_aq_enq(mbox);
99         if (aura_req == NULL)
100                 return rc;
101         aura_req->aura_id = aura_id;
102         aura_req->ctype = NPA_AQ_CTYPE_AURA;
103         aura_req->op = NPA_AQ_INSTOP_WRITE;
104         aura_req->aura.ena = 0;
105         aura_req->aura_mask.ena = ~aura_req->aura_mask.ena;
106
107         rc = mbox_process(mbox);
108         if (rc < 0)
109                 return rc;
110
111         off = mbox->rx_start +
112               PLT_ALIGN(sizeof(struct mbox_hdr), MBOX_MSG_ALIGN);
113         pool_rsp = (struct npa_aq_enq_rsp *)((uintptr_t)mdev->mbase + off);
114
115         off = mbox->rx_start + pool_rsp->hdr.next_msgoff;
116         aura_rsp = (struct npa_aq_enq_rsp *)((uintptr_t)mdev->mbase + off);
117
118         if (aura_rsp->hdr.rc != 0 || pool_rsp->hdr.rc != 0)
119                 return NPA_ERR_AURA_POOL_FINI;
120
121         /* Sync NDC-NPA for LF */
122         ndc_req = mbox_alloc_msg_ndc_sync_op(mbox);
123         if (ndc_req == NULL)
124                 return -ENOSPC;
125         ndc_req->npa_lf_sync = 1;
126         rc = mbox_process(mbox);
127         if (rc) {
128                 plt_err("Error on NDC-NPA LF sync, rc %d", rc);
129                 return NPA_ERR_AURA_POOL_FINI;
130         }
131         return 0;
132 }
133
134 int
135 roc_npa_pool_op_pc_reset(uint64_t aura_handle)
136 {
137         struct npa_lf *lf = idev_npa_obj_get();
138         struct npa_aq_enq_req *pool_req;
139         struct npa_aq_enq_rsp *pool_rsp;
140         struct ndc_sync_op *ndc_req;
141         struct mbox_dev *mdev;
142         int rc = -ENOSPC, off;
143         struct mbox *mbox;
144
145         if (lf == NULL)
146                 return NPA_ERR_PARAM;
147
148         mbox = lf->mbox;
149         mdev = &mbox->dev[0];
150         plt_npa_dbg("lf=%p aura_handle=0x%" PRIx64, lf, aura_handle);
151
152         pool_req = mbox_alloc_msg_npa_aq_enq(mbox);
153         if (pool_req == NULL)
154                 return rc;
155         pool_req->aura_id = roc_npa_aura_handle_to_aura(aura_handle);
156         pool_req->ctype = NPA_AQ_CTYPE_POOL;
157         pool_req->op = NPA_AQ_INSTOP_WRITE;
158         pool_req->pool.op_pc = 0;
159         pool_req->pool_mask.op_pc = ~pool_req->pool_mask.op_pc;
160
161         rc = mbox_process(mbox);
162         if (rc < 0)
163                 return rc;
164
165         off = mbox->rx_start +
166               PLT_ALIGN(sizeof(struct mbox_hdr), MBOX_MSG_ALIGN);
167         pool_rsp = (struct npa_aq_enq_rsp *)((uintptr_t)mdev->mbase + off);
168
169         if (pool_rsp->hdr.rc != 0)
170                 return NPA_ERR_AURA_POOL_FINI;
171
172         /* Sync NDC-NPA for LF */
173         ndc_req = mbox_alloc_msg_ndc_sync_op(mbox);
174         if (ndc_req == NULL)
175                 return -ENOSPC;
176         ndc_req->npa_lf_sync = 1;
177         rc = mbox_process(mbox);
178         if (rc) {
179                 plt_err("Error on NDC-NPA LF sync, rc %d", rc);
180                 return NPA_ERR_AURA_POOL_FINI;
181         }
182         return 0;
183 }
184 static inline char *
185 npa_stack_memzone_name(struct npa_lf *lf, int pool_id, char *name)
186 {
187         snprintf(name, PLT_MEMZONE_NAMESIZE, "roc_npa_stack_%x_%d", lf->pf_func,
188                  pool_id);
189         return name;
190 }
191
192 static inline const struct plt_memzone *
193 npa_stack_dma_alloc(struct npa_lf *lf, char *name, int pool_id, size_t size)
194 {
195         const char *mz_name = npa_stack_memzone_name(lf, pool_id, name);
196
197         return plt_memzone_reserve_cache_align(mz_name, size);
198 }
199
200 static inline int
201 npa_stack_dma_free(struct npa_lf *lf, char *name, int pool_id)
202 {
203         const struct plt_memzone *mz;
204
205         mz = plt_memzone_lookup(npa_stack_memzone_name(lf, pool_id, name));
206         if (mz == NULL)
207                 return NPA_ERR_PARAM;
208
209         return plt_memzone_free(mz);
210 }
211
212 static inline int
213 bitmap_ctzll(uint64_t slab)
214 {
215         if (slab == 0)
216                 return 0;
217
218         return __builtin_ctzll(slab);
219 }
220
221 static int
222 npa_aura_pool_pair_alloc(struct npa_lf *lf, const uint32_t block_size,
223                          const uint32_t block_count, struct npa_aura_s *aura,
224                          struct npa_pool_s *pool, uint64_t *aura_handle)
225 {
226         int rc, aura_id, pool_id, stack_size, alloc_size;
227         char name[PLT_MEMZONE_NAMESIZE];
228         const struct plt_memzone *mz;
229         uint64_t slab;
230         uint32_t pos;
231
232         /* Sanity check */
233         if (!lf || !block_size || !block_count || !pool || !aura ||
234             !aura_handle)
235                 return NPA_ERR_PARAM;
236
237         /* Block size should be cache line aligned and in range of 128B-128KB */
238         if (block_size % ROC_ALIGN || block_size < 128 ||
239             block_size > 128 * 1024)
240                 return NPA_ERR_INVALID_BLOCK_SZ;
241
242         pos = 0;
243         slab = 0;
244         /* Scan from the beginning */
245         plt_bitmap_scan_init(lf->npa_bmp);
246         /* Scan bitmap to get the free pool */
247         rc = plt_bitmap_scan(lf->npa_bmp, &pos, &slab);
248         /* Empty bitmap */
249         if (rc == 0) {
250                 plt_err("Mempools exhausted");
251                 return NPA_ERR_AURA_ID_ALLOC;
252         }
253
254         /* Get aura_id from resource bitmap */
255         aura_id = pos + bitmap_ctzll(slab);
256         /* Mark pool as reserved */
257         plt_bitmap_clear(lf->npa_bmp, aura_id);
258
259         /* Configuration based on each aura has separate pool(aura-pool pair) */
260         pool_id = aura_id;
261         rc = (aura_id < 0 || pool_id >= (int)lf->nr_pools ||
262               aura_id >= (int)BIT_ULL(6 + lf->aura_sz)) ?
263                            NPA_ERR_AURA_ID_ALLOC :
264                            0;
265         if (rc)
266                 goto exit;
267
268         /* Allocate stack memory */
269         stack_size = (block_count + lf->stack_pg_ptrs - 1) / lf->stack_pg_ptrs;
270         alloc_size = stack_size * lf->stack_pg_bytes;
271
272         mz = npa_stack_dma_alloc(lf, name, pool_id, alloc_size);
273         if (mz == NULL) {
274                 rc = NPA_ERR_ALLOC;
275                 goto aura_res_put;
276         }
277
278         /* Update aura fields */
279         aura->pool_addr = pool_id; /* AF will translate to associated poolctx */
280         aura->ena = 1;
281         aura->shift = __builtin_clz(block_count) - 8;
282         aura->limit = block_count;
283         aura->pool_caching = 1;
284         aura->err_int_ena = BIT(NPA_AURA_ERR_INT_AURA_ADD_OVER);
285         aura->err_int_ena |= BIT(NPA_AURA_ERR_INT_AURA_ADD_UNDER);
286         aura->err_int_ena |= BIT(NPA_AURA_ERR_INT_AURA_FREE_UNDER);
287         aura->err_int_ena |= BIT(NPA_AURA_ERR_INT_POOL_DIS);
288         /* Many to one reduction */
289         aura->err_qint_idx = aura_id % lf->qints;
290
291         /* Update pool fields */
292         pool->stack_base = mz->iova;
293         pool->ena = 1;
294         pool->buf_size = block_size / ROC_ALIGN;
295         pool->stack_max_pages = stack_size;
296         pool->shift = __builtin_clz(block_count) - 8;
297         pool->ptr_start = 0;
298         pool->ptr_end = ~0;
299         pool->stack_caching = 1;
300         pool->err_int_ena = BIT(NPA_POOL_ERR_INT_OVFLS);
301         pool->err_int_ena |= BIT(NPA_POOL_ERR_INT_RANGE);
302         pool->err_int_ena |= BIT(NPA_POOL_ERR_INT_PERR);
303
304         /* Many to one reduction */
305         pool->err_qint_idx = pool_id % lf->qints;
306
307         /* Issue AURA_INIT and POOL_INIT op */
308         rc = npa_aura_pool_init(lf->mbox, aura_id, aura, pool);
309         if (rc)
310                 goto stack_mem_free;
311
312         *aura_handle = roc_npa_aura_handle_gen(aura_id, lf->base);
313         /* Update aura count */
314         roc_npa_aura_op_cnt_set(*aura_handle, 0, block_count);
315         /* Read it back to make sure aura count is updated */
316         roc_npa_aura_op_cnt_get(*aura_handle);
317
318         return 0;
319
320 stack_mem_free:
321         plt_memzone_free(mz);
322 aura_res_put:
323         plt_bitmap_set(lf->npa_bmp, aura_id);
324 exit:
325         return rc;
326 }
327
328 int
329 roc_npa_pool_create(uint64_t *aura_handle, uint32_t block_size,
330                     uint32_t block_count, struct npa_aura_s *aura,
331                     struct npa_pool_s *pool)
332 {
333         struct npa_aura_s defaura;
334         struct npa_pool_s defpool;
335         struct idev_cfg *idev;
336         struct npa_lf *lf;
337         int rc;
338
339         lf = idev_npa_obj_get();
340         if (lf == NULL) {
341                 rc = NPA_ERR_DEVICE_NOT_BOUNDED;
342                 goto error;
343         }
344
345         idev = idev_get_cfg();
346         if (idev == NULL) {
347                 rc = NPA_ERR_ALLOC;
348                 goto error;
349         }
350
351         if (aura == NULL) {
352                 memset(&defaura, 0, sizeof(struct npa_aura_s));
353                 aura = &defaura;
354         }
355         if (pool == NULL) {
356                 memset(&defpool, 0, sizeof(struct npa_pool_s));
357                 defpool.nat_align = 1;
358                 defpool.buf_offset = 1;
359                 pool = &defpool;
360         }
361
362         rc = npa_aura_pool_pair_alloc(lf, block_size, block_count, aura, pool,
363                                       aura_handle);
364         if (rc) {
365                 plt_err("Failed to alloc pool or aura rc=%d", rc);
366                 goto error;
367         }
368
369         plt_npa_dbg("lf=%p block_sz=%d block_count=%d aura_handle=0x%" PRIx64,
370                     lf, block_size, block_count, *aura_handle);
371
372         /* Just hold the reference of the object */
373         __atomic_fetch_add(&idev->npa_refcnt, 1, __ATOMIC_SEQ_CST);
374 error:
375         return rc;
376 }
377
378 int
379 roc_npa_aura_limit_modify(uint64_t aura_handle, uint16_t aura_limit)
380 {
381         struct npa_aq_enq_req *aura_req;
382         struct npa_lf *lf;
383         int rc;
384
385         lf = idev_npa_obj_get();
386         if (lf == NULL)
387                 return NPA_ERR_DEVICE_NOT_BOUNDED;
388
389         aura_req = mbox_alloc_msg_npa_aq_enq(lf->mbox);
390         if (aura_req == NULL)
391                 return -ENOMEM;
392         aura_req->aura_id = roc_npa_aura_handle_to_aura(aura_handle);
393         aura_req->ctype = NPA_AQ_CTYPE_AURA;
394         aura_req->op = NPA_AQ_INSTOP_WRITE;
395
396         aura_req->aura.limit = aura_limit;
397         aura_req->aura_mask.limit = ~(aura_req->aura_mask.limit);
398         rc = mbox_process(lf->mbox);
399
400         return rc;
401 }
402
403 static int
404 npa_aura_pool_pair_free(struct npa_lf *lf, uint64_t aura_handle)
405 {
406         char name[PLT_MEMZONE_NAMESIZE];
407         int aura_id, pool_id, rc;
408
409         if (!lf || !aura_handle)
410                 return NPA_ERR_PARAM;
411
412         aura_id = roc_npa_aura_handle_to_aura(aura_handle);
413         pool_id = aura_id;
414         rc = npa_aura_pool_fini(lf->mbox, aura_id, aura_handle);
415         rc |= npa_stack_dma_free(lf, name, pool_id);
416
417         plt_bitmap_set(lf->npa_bmp, aura_id);
418
419         return rc;
420 }
421
422 int
423 roc_npa_pool_destroy(uint64_t aura_handle)
424 {
425         struct npa_lf *lf = idev_npa_obj_get();
426         int rc = 0;
427
428         plt_npa_dbg("lf=%p aura_handle=0x%" PRIx64, lf, aura_handle);
429         rc = npa_aura_pool_pair_free(lf, aura_handle);
430         if (rc)
431                 plt_err("Failed to destroy pool or aura rc=%d", rc);
432
433         /* Release the reference of npa */
434         rc |= npa_lf_fini();
435         return rc;
436 }
437
438 int
439 roc_npa_pool_range_update_check(uint64_t aura_handle)
440 {
441         uint64_t aura_id = roc_npa_aura_handle_to_aura(aura_handle);
442         struct npa_lf *lf;
443         struct npa_aura_lim *lim;
444         __io struct npa_pool_s *pool;
445         struct npa_aq_enq_req *req;
446         struct npa_aq_enq_rsp *rsp;
447         int rc;
448
449         lf = idev_npa_obj_get();
450         if (lf == NULL)
451                 return NPA_ERR_PARAM;
452
453         lim = lf->aura_lim;
454
455         req = mbox_alloc_msg_npa_aq_enq(lf->mbox);
456         if (req == NULL)
457                 return -ENOSPC;
458
459         req->aura_id = aura_id;
460         req->ctype = NPA_AQ_CTYPE_POOL;
461         req->op = NPA_AQ_INSTOP_READ;
462
463         rc = mbox_process_msg(lf->mbox, (void *)&rsp);
464         if (rc) {
465                 plt_err("Failed to get pool(0x%" PRIx64 ") context", aura_id);
466                 return rc;
467         }
468
469         pool = &rsp->pool;
470         if (lim[aura_id].ptr_start != pool->ptr_start ||
471             lim[aura_id].ptr_end != pool->ptr_end) {
472                 plt_err("Range update failed on pool(0x%" PRIx64 ")", aura_id);
473                 return NPA_ERR_PARAM;
474         }
475
476         return 0;
477 }
478
479 static inline int
480 npa_attach(struct mbox *mbox)
481 {
482         struct rsrc_attach_req *req;
483
484         req = mbox_alloc_msg_attach_resources(mbox);
485         if (req == NULL)
486                 return -ENOSPC;
487         req->modify = true;
488         req->npalf = true;
489
490         return mbox_process(mbox);
491 }
492
493 static inline int
494 npa_detach(struct mbox *mbox)
495 {
496         struct rsrc_detach_req *req;
497
498         req = mbox_alloc_msg_detach_resources(mbox);
499         if (req == NULL)
500                 return -ENOSPC;
501         req->partial = true;
502         req->npalf = true;
503
504         return mbox_process(mbox);
505 }
506
507 static inline int
508 npa_get_msix_offset(struct mbox *mbox, uint16_t *npa_msixoff)
509 {
510         struct msix_offset_rsp *msix_rsp;
511         int rc;
512
513         /* Get NPA MSIX vector offsets */
514         mbox_alloc_msg_msix_offset(mbox);
515         rc = mbox_process_msg(mbox, (void *)&msix_rsp);
516         if (rc == 0)
517                 *npa_msixoff = msix_rsp->npa_msixoff;
518
519         return rc;
520 }
521
522 static inline int
523 npa_lf_alloc(struct npa_lf *lf)
524 {
525         struct mbox *mbox = lf->mbox;
526         struct npa_lf_alloc_req *req;
527         struct npa_lf_alloc_rsp *rsp;
528         int rc;
529
530         req = mbox_alloc_msg_npa_lf_alloc(mbox);
531         if (req == NULL)
532                 return -ENOSPC;
533         req->aura_sz = lf->aura_sz;
534         req->nr_pools = lf->nr_pools;
535
536         rc = mbox_process_msg(mbox, (void *)&rsp);
537         if (rc)
538                 return NPA_ERR_ALLOC;
539
540         lf->stack_pg_ptrs = rsp->stack_pg_ptrs;
541         lf->stack_pg_bytes = rsp->stack_pg_bytes;
542         lf->qints = rsp->qints;
543
544         return 0;
545 }
546
547 static int
548 npa_lf_free(struct mbox *mbox)
549 {
550         mbox_alloc_msg_npa_lf_free(mbox);
551         return mbox_process(mbox);
552 }
553
554 static inline uint32_t
555 aura_size_to_u32(uint8_t val)
556 {
557         if (val == NPA_AURA_SZ_0)
558                 return 128;
559         if (val >= NPA_AURA_SZ_MAX)
560                 return BIT_ULL(20);
561
562         return 1 << (val + 6);
563 }
564
565 static inline void
566 pool_count_aura_sz_get(uint32_t *nr_pools, uint8_t *aura_sz)
567 {
568         uint32_t val;
569
570         val = roc_idev_npa_maxpools_get();
571         if (val < aura_size_to_u32(NPA_AURA_SZ_128))
572                 val = 128;
573         if (val > aura_size_to_u32(NPA_AURA_SZ_1M))
574                 val = BIT_ULL(20);
575
576         roc_idev_npa_maxpools_set(val);
577         *nr_pools = val;
578         *aura_sz = plt_log2_u32(val) - 6;
579 }
580
581 static int
582 npa_dev_init(struct npa_lf *lf, uintptr_t base, struct mbox *mbox)
583 {
584         uint32_t i, bmp_sz, nr_pools;
585         uint8_t aura_sz;
586         int rc;
587
588         /* Sanity checks */
589         if (!lf || !base || !mbox)
590                 return NPA_ERR_PARAM;
591
592         if (base & ROC_AURA_ID_MASK)
593                 return NPA_ERR_BASE_INVALID;
594
595         pool_count_aura_sz_get(&nr_pools, &aura_sz);
596         if (aura_sz == NPA_AURA_SZ_0 || aura_sz >= NPA_AURA_SZ_MAX)
597                 return NPA_ERR_PARAM;
598
599         memset(lf, 0x0, sizeof(*lf));
600         lf->base = base;
601         lf->aura_sz = aura_sz;
602         lf->nr_pools = nr_pools;
603         lf->mbox = mbox;
604
605         rc = npa_lf_alloc(lf);
606         if (rc)
607                 goto exit;
608
609         bmp_sz = plt_bitmap_get_memory_footprint(nr_pools);
610
611         /* Allocate memory for bitmap */
612         lf->npa_bmp_mem = plt_zmalloc(bmp_sz, ROC_ALIGN);
613         if (lf->npa_bmp_mem == NULL) {
614                 rc = NPA_ERR_ALLOC;
615                 goto lf_free;
616         }
617
618         /* Initialize pool resource bitmap array */
619         lf->npa_bmp = plt_bitmap_init(nr_pools, lf->npa_bmp_mem, bmp_sz);
620         if (lf->npa_bmp == NULL) {
621                 rc = NPA_ERR_PARAM;
622                 goto bmap_mem_free;
623         }
624
625         /* Mark all pools available */
626         for (i = 0; i < nr_pools; i++)
627                 plt_bitmap_set(lf->npa_bmp, i);
628
629         /* Allocate memory for qint context */
630         lf->npa_qint_mem = plt_zmalloc(sizeof(struct npa_qint) * nr_pools, 0);
631         if (lf->npa_qint_mem == NULL) {
632                 rc = NPA_ERR_ALLOC;
633                 goto bmap_free;
634         }
635
636         /* Allocate memory for nap_aura_lim memory */
637         lf->aura_lim = plt_zmalloc(sizeof(struct npa_aura_lim) * nr_pools, 0);
638         if (lf->aura_lim == NULL) {
639                 rc = NPA_ERR_ALLOC;
640                 goto qint_free;
641         }
642
643         /* Init aura start & end limits */
644         for (i = 0; i < nr_pools; i++) {
645                 lf->aura_lim[i].ptr_start = UINT64_MAX;
646                 lf->aura_lim[i].ptr_end = 0x0ull;
647         }
648
649         return 0;
650
651 qint_free:
652         plt_free(lf->npa_qint_mem);
653 bmap_free:
654         plt_bitmap_free(lf->npa_bmp);
655 bmap_mem_free:
656         plt_free(lf->npa_bmp_mem);
657 lf_free:
658         npa_lf_free(lf->mbox);
659 exit:
660         return rc;
661 }
662
663 static int
664 npa_dev_fini(struct npa_lf *lf)
665 {
666         if (!lf)
667                 return NPA_ERR_PARAM;
668
669         plt_free(lf->aura_lim);
670         plt_free(lf->npa_qint_mem);
671         plt_bitmap_free(lf->npa_bmp);
672         plt_free(lf->npa_bmp_mem);
673
674         return npa_lf_free(lf->mbox);
675 }
676
677 int
678 npa_lf_init(struct dev *dev, struct plt_pci_device *pci_dev)
679 {
680         struct idev_cfg *idev;
681         uint16_t npa_msixoff;
682         struct npa_lf *lf;
683         int rc;
684
685         idev = idev_get_cfg();
686         if (idev == NULL)
687                 return NPA_ERR_ALLOC;
688
689         /* Not the first PCI device */
690         if (__atomic_fetch_add(&idev->npa_refcnt, 1, __ATOMIC_SEQ_CST) != 0)
691                 return 0;
692
693         rc = npa_attach(dev->mbox);
694         if (rc)
695                 goto fail;
696
697         rc = npa_get_msix_offset(dev->mbox, &npa_msixoff);
698         if (rc)
699                 goto npa_detach;
700
701         lf = &dev->npa;
702         rc = npa_dev_init(lf, dev->bar2 + (RVU_BLOCK_ADDR_NPA << 20),
703                           dev->mbox);
704         if (rc)
705                 goto npa_detach;
706
707         lf->pf_func = dev->pf_func;
708         lf->npa_msixoff = npa_msixoff;
709         lf->intr_handle = &pci_dev->intr_handle;
710         lf->pci_dev = pci_dev;
711
712         idev->npa_pf_func = dev->pf_func;
713         idev->npa = lf;
714         plt_wmb();
715
716         rc = npa_register_irqs(lf);
717         if (rc)
718                 goto npa_fini;
719
720         plt_npa_dbg("npa=%p max_pools=%d pf_func=0x%x msix=0x%x", lf,
721                     roc_idev_npa_maxpools_get(), lf->pf_func, npa_msixoff);
722
723         return 0;
724
725 npa_fini:
726         npa_dev_fini(idev->npa);
727 npa_detach:
728         npa_detach(dev->mbox);
729 fail:
730         __atomic_fetch_sub(&idev->npa_refcnt, 1, __ATOMIC_SEQ_CST);
731         return rc;
732 }
733
734 int
735 npa_lf_fini(void)
736 {
737         struct idev_cfg *idev;
738         int rc = 0;
739
740         idev = idev_get_cfg();
741         if (idev == NULL)
742                 return NPA_ERR_ALLOC;
743
744         /* Not the last PCI device */
745         if (__atomic_sub_fetch(&idev->npa_refcnt, 1, __ATOMIC_SEQ_CST) != 0)
746                 return 0;
747
748         npa_unregister_irqs(idev->npa);
749         rc |= npa_dev_fini(idev->npa);
750         rc |= npa_detach(idev->npa->mbox);
751         idev_set_defaults(idev);
752
753         return rc;
754 }
755
756 int
757 roc_npa_dev_init(struct roc_npa *roc_npa)
758 {
759         struct plt_pci_device *pci_dev;
760         struct npa *npa;
761         struct dev *dev;
762         int rc;
763
764         if (roc_npa == NULL || roc_npa->pci_dev == NULL)
765                 return NPA_ERR_PARAM;
766
767         PLT_STATIC_ASSERT(sizeof(struct npa) <= ROC_NPA_MEM_SZ);
768         npa = roc_npa_to_npa_priv(roc_npa);
769         memset(npa, 0, sizeof(*npa));
770         pci_dev = roc_npa->pci_dev;
771         dev = &npa->dev;
772
773         /* Initialize device  */
774         rc = dev_init(dev, pci_dev);
775         if (rc) {
776                 plt_err("Failed to init roc device");
777                 goto fail;
778         }
779
780         npa->pci_dev = pci_dev;
781         dev->drv_inited = true;
782 fail:
783         return rc;
784 }
785
786 int
787 roc_npa_dev_fini(struct roc_npa *roc_npa)
788 {
789         struct npa *npa = roc_npa_to_npa_priv(roc_npa);
790
791         if (npa == NULL)
792                 return NPA_ERR_PARAM;
793
794         npa->dev.drv_inited = false;
795         return dev_fini(&npa->dev, npa->pci_dev);
796 }