LCOV - code coverage report
Current view: top level - drivers/common/cnxk - roc_npa.c (source / functions) Hit Total Coverage
Test: Code coverage Lines: 4 945 0.4 %
Date: 2026-12-01 19:01:22 Functions: 1 50 2.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 1 409 0.2 %

           Branch data     Line data    Source code
       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                 :            : static roc_npa_lf_init_cb_t lf_init_cb;
       9                 :            : 
      10                 :            : int
      11                 :        306 : roc_npa_lf_init_cb_register(roc_npa_lf_init_cb_t cb)
      12                 :            : {
      13         [ +  - ]:        306 :         if (lf_init_cb != NULL)
      14                 :            :                 return -EEXIST;
      15                 :            : 
      16                 :        306 :         lf_init_cb = cb;
      17                 :        306 :         return 0;
      18                 :            : }
      19                 :            : 
      20                 :            : uint16_t
      21                 :          0 : roc_npa_pf_func_get(void)
      22                 :            : {
      23                 :          0 :         return idev_npa_pffunc_get();
      24                 :            : }
      25                 :            : 
      26                 :            : void
      27                 :          0 : roc_npa_pool_op_range_set(uint64_t aura_handle, uint64_t start_iova,
      28                 :            :                           uint64_t end_iova)
      29                 :            : {
      30                 :          0 :         const uint64_t start = roc_npa_aura_handle_to_base(aura_handle) +
      31                 :            :                                NPA_LF_POOL_OP_PTR_START0;
      32                 :          0 :         const uint64_t end = roc_npa_aura_handle_to_base(aura_handle) +
      33                 :            :                              NPA_LF_POOL_OP_PTR_END0;
      34                 :            :         uint64_t reg = roc_npa_aura_handle_to_aura(aura_handle);
      35                 :          0 :         struct npa_lf *lf = idev_npa_obj_get();
      36                 :            :         struct npa_aura_lim *lim;
      37                 :            : 
      38                 :            :         PLT_ASSERT(lf);
      39                 :          0 :         lim = lf->aura_lim;
      40                 :            : 
      41                 :            :         /* Change the range bookkeeping in software as well as in hardware */
      42                 :          0 :         lim[reg].ptr_start = PLT_MIN(lim[reg].ptr_start, start_iova);
      43                 :          0 :         lim[reg].ptr_end = PLT_MAX(lim[reg].ptr_end, end_iova);
      44                 :            : 
      45                 :          0 :         roc_store_pair(lim[reg].ptr_start, reg, start);
      46                 :          0 :         roc_store_pair(lim[reg].ptr_end, reg, end);
      47                 :          0 : }
      48                 :            : 
      49                 :            : void
      50                 :          0 : roc_npa_aura_op_range_set(uint64_t aura_handle, uint64_t start_iova,
      51                 :            :                           uint64_t end_iova)
      52                 :            : {
      53                 :            :         uint64_t reg = roc_npa_aura_handle_to_aura(aura_handle);
      54                 :          0 :         struct npa_lf *lf = idev_npa_obj_get();
      55                 :            :         struct npa_aura_lim *lim;
      56                 :            : 
      57                 :            :         PLT_ASSERT(lf);
      58                 :          0 :         lim = lf->aura_lim;
      59                 :            : 
      60                 :            :         /* Change only the bookkeeping in software */
      61                 :          0 :         lim[reg].ptr_start = PLT_MIN(lim[reg].ptr_start, start_iova);
      62                 :          0 :         lim[reg].ptr_end = PLT_MAX(lim[reg].ptr_end, end_iova);
      63                 :          0 : }
      64                 :            : 
      65                 :            : void
      66                 :          0 : roc_npa_aura_op_range_get(uint64_t aura_handle, uint64_t *start_iova,
      67                 :            :                           uint64_t *end_iova)
      68                 :            : {
      69                 :            :         uint64_t aura_id = roc_npa_aura_handle_to_aura(aura_handle);
      70                 :            :         struct npa_aura_lim *lim;
      71                 :            :         struct npa_lf *lf;
      72                 :            : 
      73                 :          0 :         lf = idev_npa_obj_get();
      74                 :            :         PLT_ASSERT(lf);
      75                 :            : 
      76                 :          0 :         lim = lf->aura_lim;
      77                 :          0 :         *start_iova = lim[aura_id].ptr_start;
      78                 :          0 :         *end_iova = lim[aura_id].ptr_end;
      79                 :          0 : }
      80                 :            : 
      81                 :            : static int
      82                 :          0 : npa_aura_pool_init(struct mbox *m_box, uint32_t aura_id, struct npa_aura_s *aura,
      83                 :            :                    struct npa_pool_s *pool)
      84                 :            : {
      85                 :            :         struct npa_aq_enq_req *aura_init_req, *pool_init_req;
      86                 :            :         struct npa_aq_enq_rsp *aura_init_rsp, *pool_init_rsp;
      87                 :          0 :         struct mbox_dev *mdev = &m_box->dev[0];
      88                 :            :         int rc = -ENOSPC, off;
      89                 :            :         struct mbox *mbox;
      90                 :            : 
      91                 :            :         mbox = mbox_get(m_box);
      92         [ #  # ]:          0 :         if (roc_model_is_cn20k()) {
      93                 :            :                 struct npa_cn20k_aq_enq_req *aura_init_req_cn20k;
      94                 :          0 :                 aura_init_req_cn20k = mbox_alloc_msg_npa_cn20k_aq_enq(mbox);
      95                 :            :                 aura_init_req = (struct npa_aq_enq_req *)aura_init_req_cn20k;
      96                 :            :         } else {
      97                 :          0 :                 aura_init_req = mbox_alloc_msg_npa_aq_enq(mbox);
      98                 :            :         }
      99         [ #  # ]:          0 :         if (aura_init_req == NULL)
     100                 :          0 :                 goto exit;
     101                 :          0 :         aura_init_req->aura_id = aura_id;
     102                 :          0 :         aura_init_req->ctype = NPA_AQ_CTYPE_AURA;
     103                 :          0 :         aura_init_req->op = NPA_AQ_INSTOP_INIT;
     104         [ #  # ]:          0 :         mbox_memcpy(&aura_init_req->aura, aura, sizeof(*aura));
     105                 :            : 
     106         [ #  # ]:          0 :         if (roc_model_is_cn20k()) {
     107                 :            :                 struct npa_cn20k_aq_enq_req *pool_init_req_cn20k;
     108                 :          0 :                 pool_init_req_cn20k = mbox_alloc_msg_npa_cn20k_aq_enq(mbox);
     109                 :            :                 pool_init_req = (struct npa_aq_enq_req *)pool_init_req_cn20k;
     110                 :            :         } else {
     111                 :          0 :                 pool_init_req = mbox_alloc_msg_npa_aq_enq(mbox);
     112                 :            :         }
     113         [ #  # ]:          0 :         if (pool_init_req == NULL)
     114                 :          0 :                 goto exit;
     115                 :          0 :         pool_init_req->aura_id = aura_id;
     116                 :          0 :         pool_init_req->ctype = NPA_AQ_CTYPE_POOL;
     117                 :          0 :         pool_init_req->op = NPA_AQ_INSTOP_INIT;
     118         [ #  # ]:          0 :         mbox_memcpy(&pool_init_req->pool, pool, sizeof(*pool));
     119                 :            : 
     120                 :          0 :         rc = mbox_process(mbox);
     121         [ #  # ]:          0 :         if (rc < 0)
     122                 :          0 :                 goto exit;
     123                 :            : 
     124                 :          0 :         off = mbox->rx_start +
     125                 :            :               PLT_ALIGN(sizeof(struct mbox_hdr), MBOX_MSG_ALIGN);
     126                 :          0 :         aura_init_rsp = (struct npa_aq_enq_rsp *)((uintptr_t)mdev->mbase + off);
     127                 :          0 :         off = mbox->rx_start + aura_init_rsp->hdr.next_msgoff;
     128                 :          0 :         pool_init_rsp = (struct npa_aq_enq_rsp *)((uintptr_t)mdev->mbase + off);
     129                 :            : 
     130   [ #  #  #  # ]:          0 :         if (aura_init_rsp->hdr.rc == 0 && pool_init_rsp->hdr.rc == 0)
     131                 :            :                 rc = 0;
     132                 :            :         else
     133                 :            :                 rc = NPA_ERR_AURA_POOL_INIT;
     134                 :          0 : exit:
     135                 :            :         mbox_put(mbox);
     136                 :          0 :         return rc;
     137                 :            : }
     138                 :            : 
     139                 :            : static int
     140                 :          0 : npa_aura_init(struct mbox *m_box, uint32_t aura_id, struct npa_aura_s *aura)
     141                 :            : {
     142                 :            :         struct npa_aq_enq_req *aura_init_req;
     143                 :            :         struct npa_aq_enq_rsp *aura_init_rsp;
     144                 :            :         struct mbox *mbox;
     145                 :            :         int rc = -ENOSPC;
     146                 :            : 
     147                 :            :         mbox = mbox_get(m_box);
     148         [ #  # ]:          0 :         if (roc_model_is_cn20k()) {
     149                 :            :                 struct npa_cn20k_aq_enq_req *aura_init_req_cn20k;
     150                 :          0 :                 aura_init_req_cn20k = mbox_alloc_msg_npa_cn20k_aq_enq(mbox);
     151                 :            :                 aura_init_req = (struct npa_aq_enq_req *)aura_init_req_cn20k;
     152                 :            :         } else {
     153                 :          0 :                 aura_init_req = mbox_alloc_msg_npa_aq_enq(mbox);
     154                 :            :         }
     155         [ #  # ]:          0 :         if (aura_init_req == NULL)
     156                 :          0 :                 goto exit;
     157                 :          0 :         aura_init_req->aura_id = aura_id;
     158                 :          0 :         aura_init_req->ctype = NPA_AQ_CTYPE_AURA;
     159                 :          0 :         aura_init_req->op = NPA_AQ_INSTOP_INIT;
     160         [ #  # ]:          0 :         mbox_memcpy(&aura_init_req->aura, aura, sizeof(*aura));
     161                 :            : 
     162                 :            :         rc = mbox_process_msg(mbox, (void **)&aura_init_rsp);
     163         [ #  # ]:          0 :         if (rc < 0)
     164                 :          0 :                 goto exit;
     165                 :            : 
     166         [ #  # ]:          0 :         if (aura_init_rsp->hdr.rc == 0)
     167                 :            :                 rc = 0;
     168                 :            :         else
     169                 :            :                 rc = NPA_ERR_AURA_POOL_INIT;
     170                 :          0 : exit:
     171                 :            :         mbox_put(mbox);
     172                 :          0 :         return rc;
     173                 :            : }
     174                 :            : 
     175                 :            : static inline void
     176                 :          0 : npa_pool_multi_bp_reset(struct npa_cn20k_aq_enq_req *pool_req)
     177                 :            : {
     178                 :          0 :         pool_req->pool.bp_0 = 0;
     179                 :          0 :         pool_req->pool.bp_1 = 0;
     180                 :          0 :         pool_req->pool.bp_2 = 0;
     181                 :          0 :         pool_req->pool.bp_3 = 0;
     182                 :          0 :         pool_req->pool.bp_4 = 0;
     183                 :          0 :         pool_req->pool.bp_5 = 0;
     184                 :          0 :         pool_req->pool.bp_6 = 0;
     185                 :          0 :         pool_req->pool.bp_7 = 0;
     186                 :          0 :         pool_req->pool.bp_ena_0 = 0;
     187                 :          0 :         pool_req->pool.bp_ena_1 = 0;
     188                 :          0 :         pool_req->pool.bp_ena_2 = 0;
     189                 :          0 :         pool_req->pool.bp_ena_3 = 0;
     190                 :          0 :         pool_req->pool.bp_ena_4 = 0;
     191                 :          0 :         pool_req->pool.bp_ena_5 = 0;
     192                 :          0 :         pool_req->pool.bp_ena_6 = 0;
     193                 :          0 :         pool_req->pool.bp_ena_7 = 0;
     194                 :          0 :         pool_req->pool_mask.bp_0 = ~(pool_req->pool_mask.bp_0);
     195                 :          0 :         pool_req->pool_mask.bp_1 = ~(pool_req->pool_mask.bp_1);
     196                 :          0 :         pool_req->pool_mask.bp_2 = ~(pool_req->pool_mask.bp_2);
     197                 :          0 :         pool_req->pool_mask.bp_3 = ~(pool_req->pool_mask.bp_3);
     198                 :          0 :         pool_req->pool_mask.bp_4 = ~(pool_req->pool_mask.bp_4);
     199                 :          0 :         pool_req->pool_mask.bp_5 = ~(pool_req->pool_mask.bp_5);
     200                 :          0 :         pool_req->pool_mask.bp_6 = ~(pool_req->pool_mask.bp_6);
     201                 :          0 :         pool_req->pool_mask.bp_7 = ~(pool_req->pool_mask.bp_7);
     202                 :          0 :         pool_req->pool_mask.bp_ena_0 = ~(pool_req->pool_mask.bp_ena_0);
     203                 :          0 :         pool_req->pool_mask.bp_ena_1 = ~(pool_req->pool_mask.bp_ena_1);
     204                 :          0 :         pool_req->pool_mask.bp_ena_2 = ~(pool_req->pool_mask.bp_ena_2);
     205                 :          0 :         pool_req->pool_mask.bp_ena_3 = ~(pool_req->pool_mask.bp_ena_3);
     206                 :          0 :         pool_req->pool_mask.bp_ena_4 = ~(pool_req->pool_mask.bp_ena_4);
     207                 :          0 :         pool_req->pool_mask.bp_ena_5 = ~(pool_req->pool_mask.bp_ena_5);
     208                 :          0 :         pool_req->pool_mask.bp_ena_6 = ~(pool_req->pool_mask.bp_ena_6);
     209                 :          0 :         pool_req->pool_mask.bp_ena_7 = ~(pool_req->pool_mask.bp_ena_7);
     210                 :          0 : }
     211                 :            : 
     212                 :            : static int
     213                 :          0 : npa_halo_init(struct mbox *m_box, uint32_t aura_id, struct npa_cn20k_halo_s *halo)
     214                 :            : {
     215                 :            :         struct npa_cn20k_aq_enq_req *req;
     216                 :            :         struct npa_aq_enq_rsp *rsp;
     217                 :            :         struct mbox *mbox;
     218                 :            :         int rc = -ENOSPC;
     219                 :            : 
     220                 :            :         mbox = mbox_get(m_box);
     221                 :          0 :         req = mbox_alloc_msg_npa_cn20k_aq_enq(mbox);
     222         [ #  # ]:          0 :         if (req == NULL)
     223                 :          0 :                 goto exit;
     224                 :          0 :         req->aura_id = aura_id;
     225                 :          0 :         req->ctype = NPA_AQ_CTYPE_HALO;
     226                 :          0 :         req->op = NPA_AQ_INSTOP_INIT;
     227         [ #  # ]:          0 :         mbox_memcpy(&req->halo, halo, sizeof(*halo));
     228                 :            : 
     229                 :            :         rc = mbox_process_msg(mbox, (void **)&rsp);
     230         [ #  # ]:          0 :         if (rc < 0)
     231                 :          0 :                 goto exit;
     232                 :            : 
     233         [ #  # ]:          0 :         if (rsp->hdr.rc == 0)
     234                 :            :                 rc = 0;
     235                 :            :         else
     236                 :            :                 rc = NPA_ERR_HALO_INIT;
     237                 :          0 : exit:
     238                 :            :         mbox_put(mbox);
     239                 :          0 :         return rc;
     240                 :            : }
     241                 :            : 
     242                 :            : static int
     243                 :          0 : npa_halo_fini(struct mbox *m_box, uint32_t aura_id, uint64_t aura_handle)
     244                 :            : {
     245                 :          0 :         struct mbox_dev *mdev = &m_box->dev[0];
     246                 :            :         struct npa_cn20k_aq_enq_req *req;
     247                 :            :         struct npa_aq_enq_rsp *rsp;
     248                 :            :         int rc = -ENOSPC, off;
     249                 :            :         struct mbox *mbox;
     250                 :            :         uint64_t ptr;
     251                 :            : 
     252                 :            :         /* Procedure for disabling an aura/pool */
     253                 :          0 :         plt_delay_us(10);
     254                 :            : 
     255                 :            :         /* Clear all the pointers from the aura */
     256                 :            :         do {
     257                 :            :                 ptr = roc_npa_aura_op_alloc(aura_handle, 0);
     258                 :            :         } while (ptr);
     259                 :            : 
     260                 :            :         mbox = mbox_get(m_box);
     261                 :          0 :         req = mbox_alloc_msg_npa_cn20k_aq_enq(mbox);
     262         [ #  # ]:          0 :         if (req == NULL)
     263                 :          0 :                 goto exit;
     264                 :            : 
     265                 :            :         /* Disable backpressure on halo on CN20K */
     266         [ #  # ]:          0 :         if (roc_model_is_cn20k())
     267                 :          0 :                 npa_pool_multi_bp_reset(req);
     268                 :            : 
     269                 :          0 :         req->aura_id = aura_id;
     270                 :          0 :         req->ctype = NPA_AQ_CTYPE_HALO;
     271                 :          0 :         req->op = NPA_AQ_INSTOP_WRITE;
     272                 :          0 :         req->halo.ena = 0;
     273                 :          0 :         req->halo_mask.ena = ~req->halo_mask.ena;
     274                 :            : 
     275                 :          0 :         rc = mbox_process(mbox);
     276         [ #  # ]:          0 :         if (rc < 0)
     277                 :          0 :                 goto exit;
     278                 :            : 
     279                 :          0 :         off = mbox->rx_start +
     280                 :            :               PLT_ALIGN(sizeof(struct mbox_hdr), MBOX_MSG_ALIGN);
     281                 :          0 :         rsp = (struct npa_aq_enq_rsp *)((uintptr_t)mdev->mbase + off);
     282                 :            : 
     283         [ #  # ]:          0 :         if (rsp->hdr.rc != 0) {
     284                 :            :                 rc = NPA_ERR_HALO_FINI;
     285                 :          0 :                 goto exit;
     286                 :            :         }
     287                 :            : 
     288                 :            :         rc = 0;
     289                 :          0 : exit:
     290                 :            :         mbox_put(mbox);
     291                 :          0 :         return rc;
     292                 :            : }
     293                 :            : 
     294                 :            : static int
     295                 :          0 : npa_aura_pool_fini(struct mbox *m_box, uint32_t aura_id, uint64_t aura_handle)
     296                 :            : {
     297                 :            :         struct npa_cn20k_aq_enq_req *pool_req_cn20k = NULL;
     298                 :            :         struct npa_aq_enq_req *aura_req, *pool_req;
     299                 :            :         struct npa_aq_enq_rsp *aura_rsp, *pool_rsp;
     300                 :          0 :         struct mbox_dev *mdev = &m_box->dev[0];
     301                 :            :         struct ndc_sync_op *ndc_req;
     302                 :            :         int rc = -ENOSPC, off;
     303                 :            :         struct mbox *mbox;
     304                 :            :         uint64_t ptr;
     305                 :            : 
     306                 :            :         /* Procedure for disabling an aura/pool */
     307                 :          0 :         plt_delay_us(10);
     308                 :            : 
     309                 :            :         /* Clear all the pointers from the aura */
     310                 :            :         do {
     311                 :            :                 ptr = roc_npa_aura_op_alloc(aura_handle, 0);
     312                 :            :         } while (ptr);
     313                 :            : 
     314                 :            :         mbox = mbox_get(m_box);
     315         [ #  # ]:          0 :         if (roc_model_is_cn20k()) {
     316                 :          0 :                 pool_req_cn20k = mbox_alloc_msg_npa_cn20k_aq_enq(mbox);
     317                 :            :                 pool_req = (struct npa_aq_enq_req *)pool_req_cn20k;
     318                 :            :         } else {
     319                 :          0 :                 pool_req = mbox_alloc_msg_npa_aq_enq(mbox);
     320                 :            :         }
     321         [ #  # ]:          0 :         if (pool_req == NULL)
     322                 :          0 :                 goto exit;
     323                 :            : 
     324                 :            :         /* Disable backpressure on pool on CN20K */
     325         [ #  # ]:          0 :         if (roc_model_is_cn20k())
     326                 :          0 :                 npa_pool_multi_bp_reset(pool_req_cn20k);
     327                 :          0 :         pool_req->aura_id = aura_id;
     328                 :          0 :         pool_req->ctype = NPA_AQ_CTYPE_POOL;
     329                 :          0 :         pool_req->op = NPA_AQ_INSTOP_WRITE;
     330                 :          0 :         pool_req->pool.ena = 0;
     331                 :          0 :         pool_req->pool_mask.ena = ~pool_req->pool_mask.ena;
     332                 :          0 :         pool_req->pool.fc_ena = 0;
     333                 :          0 :         pool_req->pool_mask.fc_ena = ~pool_req->pool_mask.fc_ena;
     334                 :            : 
     335         [ #  # ]:          0 :         if (roc_model_is_cn20k()) {
     336                 :            :                 struct npa_cn20k_aq_enq_req *aura_req_cn20k;
     337                 :          0 :                 aura_req_cn20k = mbox_alloc_msg_npa_cn20k_aq_enq(mbox);
     338                 :            :                 aura_req = (struct npa_aq_enq_req *)aura_req_cn20k;
     339                 :            :         } else {
     340                 :          0 :                 aura_req = mbox_alloc_msg_npa_aq_enq(mbox);
     341                 :            :         }
     342         [ #  # ]:          0 :         if (aura_req == NULL)
     343                 :          0 :                 goto exit;
     344                 :          0 :         aura_req->aura_id = aura_id;
     345                 :          0 :         aura_req->ctype = NPA_AQ_CTYPE_AURA;
     346                 :          0 :         aura_req->op = NPA_AQ_INSTOP_WRITE;
     347                 :          0 :         aura_req->aura.ena = 0;
     348                 :          0 :         aura_req->aura_mask.ena = ~aura_req->aura_mask.ena;
     349                 :          0 :         aura_req->aura.fc_ena = 0;
     350         [ #  # ]:          0 :         aura_req->aura_mask.fc_ena = ~aura_req->aura_mask.fc_ena;
     351         [ #  # ]:          0 :         if (roc_model_is_cn20k()) {
     352                 :            :                 __io struct npa_cn20k_aura_s *aura_cn20k, *aura_mask_cn20k;
     353                 :            : 
     354                 :            :                 /* The bit positions/width of bp_ena has changed in cn20k */
     355                 :            :                 aura_cn20k = (__io struct npa_cn20k_aura_s *)&aura_req->aura;
     356                 :          0 :                 aura_cn20k->bp_ena = 0;
     357                 :            :                 aura_mask_cn20k = (__io struct npa_cn20k_aura_s *)&aura_req->aura_mask;
     358                 :          0 :                 aura_mask_cn20k->bp_ena = ~aura_mask_cn20k->bp_ena;
     359                 :            :         } else {
     360                 :          0 :                 aura_req->aura.bp_ena = 0;
     361                 :          0 :                 aura_req->aura_mask.bp_ena = ~aura_req->aura_mask.bp_ena;
     362                 :            :         }
     363                 :            : 
     364                 :          0 :         rc = mbox_process(mbox);
     365         [ #  # ]:          0 :         if (rc < 0)
     366                 :          0 :                 goto exit;
     367                 :            : 
     368                 :          0 :         off = mbox->rx_start +
     369                 :            :               PLT_ALIGN(sizeof(struct mbox_hdr), MBOX_MSG_ALIGN);
     370                 :          0 :         pool_rsp = (struct npa_aq_enq_rsp *)((uintptr_t)mdev->mbase + off);
     371                 :            : 
     372                 :          0 :         off = mbox->rx_start + pool_rsp->hdr.next_msgoff;
     373                 :          0 :         aura_rsp = (struct npa_aq_enq_rsp *)((uintptr_t)mdev->mbase + off);
     374                 :            : 
     375   [ #  #  #  # ]:          0 :         if (aura_rsp->hdr.rc != 0 || pool_rsp->hdr.rc != 0) {
     376                 :            :                 rc = NPA_ERR_AURA_POOL_FINI;
     377                 :          0 :                 goto exit;
     378                 :            :         }
     379                 :            : 
     380         [ #  # ]:          0 :         if (roc_model_is_cn20k()) {
     381                 :            :                 /* In cn20k, NPA does not use NDC */
     382                 :            :                 rc = 0;
     383                 :          0 :                 goto exit;
     384                 :            :         }
     385                 :            : 
     386                 :            :         /* Sync NDC-NPA for LF */
     387                 :          0 :         ndc_req = mbox_alloc_msg_ndc_sync_op(mbox);
     388         [ #  # ]:          0 :         if (ndc_req == NULL) {
     389                 :            :                 rc = -ENOSPC;
     390                 :          0 :                 goto exit;
     391                 :            :         }
     392                 :          0 :         ndc_req->npa_lf_sync = 1;
     393                 :          0 :         rc = mbox_process(mbox);
     394         [ #  # ]:          0 :         if (rc) {
     395                 :          0 :                 plt_err("Error on NDC-NPA LF sync, rc %d", rc);
     396                 :            :                 rc = NPA_ERR_AURA_POOL_FINI;
     397                 :          0 :                 goto exit;
     398                 :            :         }
     399                 :            :         rc = 0;
     400                 :          0 : exit:
     401                 :            :         mbox_put(mbox);
     402                 :          0 :         return rc;
     403                 :            : }
     404                 :            : 
     405                 :            : static int
     406                 :          0 : npa_aura_fini(struct mbox *m_box, uint32_t aura_id)
     407                 :            : {
     408                 :            :         struct npa_aq_enq_req *aura_req;
     409                 :            :         struct npa_aq_enq_rsp *aura_rsp;
     410                 :            :         struct ndc_sync_op *ndc_req;
     411                 :            :         struct mbox *mbox;
     412                 :            :         int rc = -ENOSPC;
     413                 :            : 
     414                 :            :         /* Procedure for disabling an aura/pool */
     415                 :          0 :         plt_delay_us(10);
     416                 :            : 
     417                 :            :         mbox = mbox_get(m_box);
     418         [ #  # ]:          0 :         if (roc_model_is_cn20k()) {
     419                 :            :                 struct npa_cn20k_aq_enq_req *aura_req_cn20k;
     420                 :          0 :                 aura_req_cn20k = mbox_alloc_msg_npa_cn20k_aq_enq(mbox);
     421                 :            :                 aura_req = (struct npa_aq_enq_req *)aura_req_cn20k;
     422                 :            :         } else {
     423                 :          0 :                 aura_req = mbox_alloc_msg_npa_aq_enq(mbox);
     424                 :            :         }
     425         [ #  # ]:          0 :         if (aura_req == NULL)
     426                 :          0 :                 goto exit;
     427                 :          0 :         aura_req->aura_id = aura_id;
     428                 :          0 :         aura_req->ctype = NPA_AQ_CTYPE_AURA;
     429                 :          0 :         aura_req->op = NPA_AQ_INSTOP_WRITE;
     430                 :          0 :         aura_req->aura.ena = 0;
     431                 :          0 :         aura_req->aura_mask.ena = ~aura_req->aura_mask.ena;
     432                 :            : 
     433                 :            :         rc = mbox_process_msg(mbox, (void **)&aura_rsp);
     434         [ #  # ]:          0 :         if (rc < 0)
     435                 :          0 :                 goto exit;
     436                 :            : 
     437         [ #  # ]:          0 :         if (aura_rsp->hdr.rc != 0) {
     438                 :            :                 rc = NPA_ERR_AURA_POOL_FINI;
     439                 :          0 :                 goto exit;
     440                 :            :         }
     441                 :            : 
     442         [ #  # ]:          0 :         if (roc_model_is_cn20k()) {
     443                 :            :                 /* In cn20k, NPA does not use NDC */
     444                 :            :                 rc = 0;
     445                 :          0 :                 goto exit;
     446                 :            :         }
     447                 :            : 
     448                 :            :         /* Sync NDC-NPA for LF */
     449                 :          0 :         ndc_req = mbox_alloc_msg_ndc_sync_op(mbox);
     450         [ #  # ]:          0 :         if (ndc_req == NULL) {
     451                 :            :                 rc = -ENOSPC;
     452                 :          0 :                 goto exit;
     453                 :            :         }
     454                 :          0 :         ndc_req->npa_lf_sync = 1;
     455                 :          0 :         rc = mbox_process(mbox);
     456         [ #  # ]:          0 :         if (rc) {
     457                 :          0 :                 plt_err("Error on NDC-NPA LF sync, rc %d", rc);
     458                 :            :                 rc = NPA_ERR_AURA_POOL_FINI;
     459                 :          0 :                 goto exit;
     460                 :            :         }
     461                 :            :         rc = 0;
     462                 :          0 : exit:
     463                 :            :         mbox_put(mbox);
     464                 :          0 :         return rc;
     465                 :            : }
     466                 :            : 
     467                 :            : int
     468                 :          0 : roc_npa_pool_op_pc_reset(uint64_t aura_handle)
     469                 :            : {
     470                 :          0 :         struct npa_lf *lf = idev_npa_obj_get();
     471                 :            :         struct npa_aq_enq_req *pool_req;
     472                 :            :         struct npa_aq_enq_rsp *pool_rsp;
     473                 :            :         struct ndc_sync_op *ndc_req;
     474                 :            :         struct mbox_dev *mdev;
     475                 :            :         int rc = -ENOSPC, off;
     476                 :            :         struct mbox *mbox;
     477                 :            : 
     478         [ #  # ]:          0 :         if (lf == NULL)
     479                 :            :                 return NPA_ERR_PARAM;
     480                 :            : 
     481                 :          0 :         mbox = mbox_get(lf->mbox);
     482                 :          0 :         mdev = &mbox->dev[0];
     483                 :          0 :         plt_npa_dbg("lf=%p aura_handle=0x%" PRIx64, lf, aura_handle);
     484                 :            : 
     485                 :          0 :         pool_req = mbox_alloc_msg_npa_aq_enq(mbox);
     486         [ #  # ]:          0 :         if (pool_req == NULL)
     487                 :          0 :                 goto exit;
     488                 :          0 :         pool_req->aura_id = roc_npa_aura_handle_to_aura(aura_handle);
     489                 :          0 :         pool_req->ctype = NPA_AQ_CTYPE_POOL;
     490                 :          0 :         pool_req->op = NPA_AQ_INSTOP_WRITE;
     491                 :          0 :         pool_req->pool.op_pc = 0;
     492                 :          0 :         pool_req->pool_mask.op_pc = ~pool_req->pool_mask.op_pc;
     493                 :            : 
     494                 :          0 :         rc = mbox_process(mbox);
     495         [ #  # ]:          0 :         if (rc < 0)
     496                 :          0 :                 goto exit;
     497                 :            : 
     498                 :          0 :         off = mbox->rx_start +
     499                 :            :               PLT_ALIGN(sizeof(struct mbox_hdr), MBOX_MSG_ALIGN);
     500                 :          0 :         pool_rsp = (struct npa_aq_enq_rsp *)((uintptr_t)mdev->mbase + off);
     501                 :            : 
     502         [ #  # ]:          0 :         if (pool_rsp->hdr.rc != 0) {
     503                 :            :                 rc = NPA_ERR_AURA_POOL_FINI;
     504                 :          0 :                 goto exit;
     505                 :            :         }
     506                 :            : 
     507                 :            :         /* Sync NDC-NPA for LF */
     508                 :          0 :         ndc_req = mbox_alloc_msg_ndc_sync_op(mbox);
     509         [ #  # ]:          0 :         if (ndc_req == NULL) {
     510                 :            :                 rc = -ENOSPC;
     511                 :          0 :                 goto exit;
     512                 :            :         }
     513                 :          0 :         ndc_req->npa_lf_sync = 1;
     514                 :          0 :         rc = mbox_process(mbox);
     515         [ #  # ]:          0 :         if (rc) {
     516                 :          0 :                 plt_err("Error on NDC-NPA LF sync, rc %d", rc);
     517                 :            :                 rc = NPA_ERR_AURA_POOL_FINI;
     518                 :          0 :                 goto exit;
     519                 :            :         }
     520                 :            :         rc = 0;
     521                 :          0 : exit:
     522                 :            :         mbox_put(mbox);
     523                 :          0 :         return rc;
     524                 :            : }
     525                 :            : 
     526                 :            : int
     527                 :          0 : roc_npa_aura_drop_set(uint64_t aura_handle, uint64_t limit, bool ena)
     528                 :            : {
     529                 :            :         struct npa_aq_enq_req *aura_req;
     530                 :            :         struct npa_lf *lf;
     531                 :            :         struct mbox *mbox;
     532                 :            :         uint64_t aura_id;
     533                 :            :         int rc;
     534                 :            : 
     535                 :          0 :         lf = idev_npa_obj_get();
     536         [ #  # ]:          0 :         if (lf == NULL)
     537                 :            :                 return NPA_ERR_DEVICE_NOT_BOUNDED;
     538                 :          0 :         mbox = mbox_get(lf->mbox);
     539         [ #  # ]:          0 :         if (roc_model_is_cn20k()) {
     540                 :            :                 struct npa_cn20k_aq_enq_req *aura_req_cn20k;
     541                 :          0 :                 aura_req_cn20k = mbox_alloc_msg_npa_cn20k_aq_enq(mbox);
     542                 :            :                 aura_req = (struct npa_aq_enq_req *)aura_req_cn20k;
     543                 :            :         } else {
     544                 :          0 :                 aura_req = mbox_alloc_msg_npa_aq_enq(mbox);
     545                 :            :         }
     546         [ #  # ]:          0 :         if (aura_req == NULL) {
     547                 :            :                 rc = -ENOMEM;
     548                 :          0 :                 goto exit;
     549                 :            :         }
     550                 :            :         aura_id = roc_npa_aura_handle_to_aura(aura_handle);
     551                 :          0 :         aura_req->aura_id = aura_id;
     552         [ #  # ]:          0 :         if (lf->aura_attr[aura_id].halo)
     553                 :          0 :                 aura_req->ctype = NPA_AQ_CTYPE_HALO;
     554                 :            :         else
     555                 :          0 :                 aura_req->ctype = NPA_AQ_CTYPE_AURA;
     556                 :          0 :         aura_req->op = NPA_AQ_INSTOP_WRITE;
     557                 :            : 
     558                 :          0 :         aura_req->aura.aura_drop_ena = ena;
     559                 :          0 :         aura_req->aura.aura_drop = limit;
     560                 :          0 :         aura_req->aura_mask.aura_drop_ena =
     561                 :          0 :                 ~(aura_req->aura_mask.aura_drop_ena);
     562                 :          0 :         aura_req->aura_mask.aura_drop = ~(aura_req->aura_mask.aura_drop);
     563                 :          0 :         rc = mbox_process(mbox);
     564                 :            : 
     565                 :          0 : exit:
     566                 :            :         mbox_put(mbox);
     567                 :          0 :         return rc;
     568                 :            : }
     569                 :            : 
     570                 :            : static inline char *
     571                 :            : npa_stack_memzone_name(struct npa_lf *lf, int pool_id, char *name)
     572                 :            : {
     573                 :          0 :         snprintf(name, PLT_MEMZONE_NAMESIZE, "roc_npa_stack_%x_%d", lf->pf_func,
     574                 :            :                  pool_id);
     575                 :            :         return name;
     576                 :            : }
     577                 :            : 
     578                 :            : static inline const struct plt_memzone *
     579                 :          0 : npa_stack_dma_alloc(struct npa_lf *lf, char *name, int pool_id, size_t size)
     580                 :            : {
     581                 :            :         const char *mz_name = npa_stack_memzone_name(lf, pool_id, name);
     582                 :          0 :         size = PLT_ALIGN_CEIL(size, ROC_ALIGN);
     583                 :            : 
     584                 :          0 :         return plt_memzone_reserve_aligned(mz_name, size, 0, ROC_ALIGN);
     585                 :            : }
     586                 :            : 
     587                 :            : static inline int
     588                 :          0 : npa_stack_dma_free(struct npa_lf *lf, char *name, int pool_id)
     589                 :            : {
     590                 :            :         const struct plt_memzone *mz;
     591                 :            : 
     592                 :          0 :         mz = plt_memzone_lookup(npa_stack_memzone_name(lf, pool_id, name));
     593         [ #  # ]:          0 :         if (mz == NULL)
     594                 :            :                 return NPA_ERR_PARAM;
     595                 :            : 
     596                 :          0 :         return plt_memzone_free(mz);
     597                 :            : }
     598                 :            : 
     599                 :            : static inline int
     600                 :            : bitmap_ctzll(uint64_t slab)
     601                 :            : {
     602                 :          0 :         if (slab == 0)
     603                 :            :                 return 0;
     604                 :            : 
     605                 :          0 :         return plt_ctz64(slab);
     606                 :            : }
     607                 :            : 
     608                 :            : static int
     609                 :          0 : find_free_aura(struct npa_lf *lf, uint32_t flags)
     610                 :            : {
     611                 :          0 :         struct plt_bitmap *bmp = lf->npa_bmp;
     612                 :            :         uint64_t aura0_state = 0;
     613                 :            :         uint64_t slab;
     614                 :            :         uint32_t pos;
     615                 :            :         int idx = -1;
     616                 :            :         int rc;
     617                 :            : 
     618         [ #  # ]:          0 :         if (flags & ROC_NPA_ZERO_AURA_F) {
     619                 :            :                 /* Only look for zero aura */
     620         [ #  # ]:          0 :                 if (plt_bitmap_get(bmp, 0))
     621                 :            :                         return 0;
     622                 :          0 :                 plt_err("Zero aura already in use");
     623                 :          0 :                 return -1;
     624                 :            :         }
     625                 :            : 
     626         [ #  # ]:          0 :         if (lf->zero_aura_rsvd) {
     627                 :            :                 /* Save and clear zero aura bit if needed */
     628                 :            :                 aura0_state = plt_bitmap_get(bmp, 0);
     629         [ #  # ]:          0 :                 if (aura0_state)
     630                 :          0 :                         plt_bitmap_clear(bmp, 0);
     631                 :            :         }
     632                 :            : 
     633                 :          0 :         pos = 0;
     634                 :          0 :         slab = 0;
     635                 :            :         /* Scan from the beginning */
     636                 :            :         plt_bitmap_scan_init(bmp);
     637                 :            :         /* Scan bitmap to get the free pool */
     638                 :          0 :         rc = plt_bitmap_scan(bmp, &pos, &slab);
     639                 :            :         /* Empty bitmap */
     640         [ #  # ]:          0 :         if (rc == 0) {
     641                 :          0 :                 plt_err("Aura's exhausted");
     642                 :          0 :                 goto empty;
     643                 :            :         }
     644                 :            : 
     645         [ #  # ]:          0 :         idx = pos + bitmap_ctzll(slab);
     646                 :          0 : empty:
     647   [ #  #  #  # ]:          0 :         if (lf->zero_aura_rsvd && aura0_state)
     648                 :            :                 plt_bitmap_set(bmp, 0);
     649                 :            : 
     650                 :            :         return idx;
     651                 :            : }
     652                 :            : 
     653                 :            : static int
     654                 :          0 : npa_aura_pool_pair_alloc(struct npa_lf *lf, const uint32_t block_size,
     655                 :            :                          const uint32_t block_count, struct npa_aura_s *aura,
     656                 :            :                          struct npa_pool_s *pool, uint64_t *aura_handle,
     657                 :            :                          uint32_t flags)
     658                 :            : {
     659                 :            :         int rc, aura_id, pool_id, stack_size, alloc_size;
     660                 :            :         char name[PLT_MEMZONE_NAMESIZE];
     661                 :            :         const struct plt_memzone *mz;
     662                 :            : 
     663                 :            :         /* Sanity check */
     664   [ #  #  #  # ]:          0 :         if (!lf || !block_size || !block_count || !pool || !aura ||
     665         [ #  # ]:          0 :             !aura_handle)
     666                 :            :                 return NPA_ERR_PARAM;
     667                 :            : 
     668                 :            :         /* Block size should be cache line aligned and in range of 128B-128KB */
     669   [ #  #  #  #  :          0 :         if (block_size % ROC_ALIGN || block_size < 128 ||
                   #  # ]
     670                 :            :             block_size > roc_npa_max_block_size())
     671                 :            :                 return NPA_ERR_INVALID_BLOCK_SZ;
     672                 :            : 
     673                 :            :         /* Get aura_id from resource bitmap */
     674                 :          0 :         roc_npa_dev_lock();
     675                 :          0 :         aura_id = find_free_aura(lf, flags);
     676         [ #  # ]:          0 :         if (aura_id < 0) {
     677                 :          0 :                 roc_npa_dev_unlock();
     678                 :          0 :                 return NPA_ERR_AURA_ID_ALLOC;
     679                 :            :         }
     680                 :            : 
     681                 :            :         /* Mark pool as reserved */
     682                 :          0 :         plt_bitmap_clear(lf->npa_bmp, aura_id);
     683                 :          0 :         roc_npa_dev_unlock();
     684                 :            : 
     685                 :            :         /* Configuration based on each aura has separate pool(aura-pool pair) */
     686                 :            :         pool_id = aura_id;
     687         [ #  # ]:          0 :         rc = (aura_id < 0 || pool_id >= (int)lf->nr_pools ||
     688         [ #  # ]:          0 :               aura_id >= (int)BIT_ULL(6 + lf->aura_sz)) ?
     689                 :            :                            NPA_ERR_AURA_ID_ALLOC :
     690                 :            :                            0;
     691                 :            :         if (rc)
     692                 :          0 :                 goto exit;
     693                 :            : 
     694                 :            :         /* Allocate stack memory */
     695                 :          0 :         stack_size = (block_count + lf->stack_pg_ptrs - 1) / lf->stack_pg_ptrs;
     696                 :          0 :         alloc_size = stack_size * lf->stack_pg_bytes;
     697                 :            : 
     698                 :          0 :         mz = npa_stack_dma_alloc(lf, name, pool_id, alloc_size);
     699         [ #  # ]:          0 :         if (mz == NULL) {
     700                 :            :                 rc = NPA_ERR_ALLOC;
     701                 :          0 :                 goto aura_res_put;
     702                 :            :         }
     703                 :            : 
     704                 :            :         /* Update aura fields */
     705                 :          0 :         aura->pool_addr = pool_id; /* AF will translate to associated poolctx */
     706         [ #  # ]:          0 :         aura->ena = 1;
     707                 :          0 :         aura->shift = plt_log2_u32(block_count);
     708         [ #  # ]:          0 :         aura->shift = aura->shift < 8 ? 0 : aura->shift - 8;
     709                 :          0 :         aura->limit = block_count;
     710                 :          0 :         aura->pool_caching = 1;
     711                 :            :         aura->err_int_ena = BIT(NPA_AURA_ERR_INT_AURA_ADD_OVER);
     712                 :            :         aura->err_int_ena |= BIT(NPA_AURA_ERR_INT_AURA_ADD_UNDER);
     713                 :            :         aura->err_int_ena |= BIT(NPA_AURA_ERR_INT_AURA_FREE_UNDER);
     714                 :          0 :         aura->err_int_ena |= BIT(NPA_AURA_ERR_INT_POOL_DIS);
     715                 :          0 :         aura->avg_con = 0;
     716                 :            :         /* Many to one reduction */
     717                 :          0 :         aura->err_qint_idx = aura_id % lf->qints;
     718                 :            : 
     719                 :            :         /* Update pool fields */
     720                 :          0 :         pool->stack_base = mz->iova;
     721                 :          0 :         pool->ena = 1;
     722                 :            : 
     723                 :            :         /* In opaque mode buffer size must be 0 */
     724         [ #  # ]:          0 :         if (!pool->nat_align) {
     725                 :          0 :                 pool->buf_size = 0;
     726                 :            :         } else {
     727         [ #  # ]:          0 :                 uint64_t buf_sz = block_size / ROC_ALIGN;
     728         [ #  # ]:          0 :                 if (roc_model_is_cn20k()) {
     729                 :            :                         __io struct npa_cn20k_pool_s *cn20k_pool =
     730                 :            :                                 (__io struct npa_cn20k_pool_s *)pool;
     731                 :          0 :                         cn20k_pool->buf_size = buf_sz;
     732                 :            :                 } else {
     733                 :          0 :                         pool->buf_size = buf_sz;
     734                 :            :                 }
     735                 :            :         }
     736                 :            : 
     737                 :          0 :         pool->stack_max_pages = stack_size;
     738                 :            :         pool->shift = plt_log2_u32(block_count);
     739                 :          0 :         pool->shift = pool->shift < 8 ? 0 : pool->shift - 8;
     740                 :          0 :         pool->ptr_start = 0;
     741                 :          0 :         pool->ptr_end = ~0;
     742                 :          0 :         pool->stack_caching = 1;
     743                 :            :         pool->err_int_ena = BIT(NPA_POOL_ERR_INT_OVFLS);
     744                 :            :         pool->err_int_ena |= BIT(NPA_POOL_ERR_INT_RANGE);
     745                 :          0 :         pool->err_int_ena |= BIT(NPA_POOL_ERR_INT_PERR);
     746                 :          0 :         pool->avg_con = 0;
     747                 :            : 
     748                 :            :         /* Many to one reduction */
     749                 :          0 :         pool->err_qint_idx = pool_id % lf->qints;
     750                 :            : 
     751                 :            :         /* Issue AURA_INIT and POOL_INIT op */
     752                 :          0 :         rc = npa_aura_pool_init(lf->mbox, aura_id, aura, pool);
     753         [ #  # ]:          0 :         if (rc)
     754                 :          0 :                 goto stack_mem_free;
     755                 :            : 
     756                 :          0 :         lf->aura_attr[aura_id].shift = aura->shift;
     757                 :          0 :         lf->aura_attr[aura_id].limit = aura->limit;
     758                 :          0 :         lf->aura_attr[aura_id].halo = 0;
     759         [ #  # ]:          0 :         *aura_handle = roc_npa_aura_handle_gen(aura_id, lf->base);
     760                 :            :         /* Update aura count */
     761         [ #  # ]:          0 :         roc_npa_aura_op_cnt_set(*aura_handle, 0, block_count);
     762                 :            :         /* Read it back to make sure aura count is updated */
     763                 :            :         roc_npa_aura_op_cnt_get(*aura_handle);
     764                 :            : 
     765                 :          0 :         return 0;
     766                 :            : 
     767                 :            : stack_mem_free:
     768                 :          0 :         plt_memzone_free(mz);
     769                 :          0 : aura_res_put:
     770                 :          0 :         roc_npa_dev_lock();
     771                 :          0 :         plt_bitmap_set(lf->npa_bmp, aura_id);
     772                 :          0 :         roc_npa_dev_unlock();
     773                 :            : exit:
     774                 :            :         return rc;
     775                 :            : }
     776                 :            : 
     777                 :            : static int
     778                 :          0 : npa_halo_alloc(struct npa_lf *lf, const uint32_t block_size, const uint32_t block_count,
     779                 :            :                struct npa_cn20k_halo_s *halo, uint64_t *aura_handle, uint32_t flags)
     780                 :            : {
     781                 :            :         int rc, aura_id, stack_size, alloc_size, pool_id;
     782                 :            :         char name[PLT_MEMZONE_NAMESIZE];
     783                 :            :         const struct plt_memzone *mz;
     784                 :            : 
     785                 :            :         /* Sanity check */
     786   [ #  #  #  # ]:          0 :         if (!lf || !halo || !aura_handle)
     787                 :            :                 return NPA_ERR_PARAM;
     788                 :            : 
     789                 :          0 :         roc_npa_dev_lock();
     790                 :            :         /* Get aura_id from resource bitmap */
     791                 :          0 :         aura_id = find_free_aura(lf, flags);
     792         [ #  # ]:          0 :         if (aura_id < 0) {
     793                 :          0 :                 roc_npa_dev_unlock();
     794                 :          0 :                 return NPA_ERR_AURA_ID_ALLOC;
     795                 :            :         }
     796                 :            : 
     797                 :            :         /* Mark aura as reserved */
     798                 :          0 :         plt_bitmap_clear(lf->npa_bmp, aura_id);
     799                 :            : 
     800                 :          0 :         roc_npa_dev_unlock();
     801         [ #  # ]:          0 :         rc = (aura_id < 0 || aura_id >= (int)BIT_ULL(6 + lf->aura_sz)) ? NPA_ERR_AURA_ID_ALLOC : 0;
     802                 :            :         if (rc)
     803                 :          0 :                 goto exit;
     804                 :            : 
     805                 :            :         /* Allocate stack memory */
     806                 :          0 :         stack_size = (block_count + lf->stack_pg_ptrs - 1) / lf->stack_pg_ptrs;
     807                 :          0 :         alloc_size = stack_size * lf->stack_pg_bytes;
     808                 :            : 
     809                 :            :         pool_id = aura_id;
     810                 :          0 :         mz = npa_stack_dma_alloc(lf, name, pool_id, alloc_size);
     811         [ #  # ]:          0 :         if (mz == NULL) {
     812                 :            :                 rc = NPA_ERR_ALLOC;
     813                 :          0 :                 goto aura_res_put;
     814                 :            :         }
     815                 :            : 
     816                 :            :         /* Update halo fields */
     817         [ #  # ]:          0 :         halo->ena = 1;
     818                 :          0 :         halo->shift = plt_log2_u32(block_count);
     819         [ #  # ]:          0 :         halo->shift = halo->shift < 8 ? 0 : halo->shift - 8;
     820                 :            :         halo->err_int_ena = BIT(NPA_AURA_ERR_INT_AURA_ADD_OVER);
     821                 :            :         halo->err_int_ena |= BIT(NPA_AURA_ERR_INT_AURA_ADD_UNDER);
     822                 :            :         halo->err_int_ena |= BIT(NPA_AURA_ERR_INT_AURA_FREE_UNDER);
     823                 :          0 :         halo->err_int_ena |= BIT(NPA_AURA_ERR_INT_POOL_DIS);
     824                 :          0 :         halo->avg_con = 0;
     825                 :            :         /* Many to one reduction */
     826                 :          0 :         halo->err_qint_idx = aura_id % lf->qints;
     827                 :          0 :         halo->stack_base = mz->iova;
     828                 :            :         /* In opaque mode buffer size must be 0 */
     829         [ #  # ]:          0 :         halo->buf_size = !halo->nat_align ? 0 : block_size / ROC_ALIGN;
     830                 :          0 :         halo->stack_max_pages = stack_size;
     831                 :          0 :         halo->ptr_start = 0;
     832                 :          0 :         halo->ptr_end = ~0;
     833                 :          0 :         halo->stack_caching = 1;
     834                 :          0 :         halo->unified_ctx = 1;
     835                 :            : 
     836                 :            :         /* Issue HALO_INIT op */
     837                 :          0 :         rc = npa_halo_init(lf->mbox, aura_id, halo);
     838         [ #  # ]:          0 :         if (rc)
     839                 :          0 :                 goto stack_mem_free;
     840                 :            : 
     841                 :          0 :         lf->aura_attr[aura_id].shift = halo->shift;
     842                 :          0 :         lf->aura_attr[aura_id].halo = 1;
     843                 :          0 :         *aura_handle = roc_npa_aura_handle_gen(aura_id, lf->base);
     844                 :            : 
     845                 :          0 :         return 0;
     846                 :            : 
     847                 :            : stack_mem_free:
     848                 :          0 :         plt_memzone_free(mz);
     849                 :          0 : aura_res_put:
     850                 :          0 :         roc_npa_dev_lock();
     851                 :          0 :         plt_bitmap_set(lf->npa_bmp, aura_id);
     852                 :          0 :         roc_npa_dev_unlock();
     853                 :            : exit:
     854                 :            :         return rc;
     855                 :            : }
     856                 :            : 
     857                 :            : int
     858                 :          0 : roc_npa_pool_create(uint64_t *aura_handle, uint32_t block_size,
     859                 :            :                     uint32_t block_count, struct npa_aura_s *aura,
     860                 :            :                     struct npa_pool_s *pool, uint32_t flags)
     861                 :            : {
     862                 :            :         struct npa_aura_s defaura;
     863                 :            :         struct npa_pool_s defpool;
     864                 :            :         struct idev_cfg *idev;
     865                 :            :         struct npa_lf *lf;
     866                 :            :         int rc;
     867                 :            : 
     868                 :          0 :         lf = idev_npa_obj_get();
     869         [ #  # ]:          0 :         if (lf == NULL) {
     870                 :            :                 rc = NPA_ERR_DEVICE_NOT_BOUNDED;
     871                 :          0 :                 goto error;
     872                 :            :         }
     873                 :            : 
     874                 :          0 :         idev = idev_get_cfg();
     875         [ #  # ]:          0 :         if (idev == NULL) {
     876                 :            :                 rc = NPA_ERR_ALLOC;
     877                 :          0 :                 goto error;
     878                 :            :         }
     879                 :            : 
     880   [ #  #  #  # ]:          0 :         if (flags & ROC_NPA_ZERO_AURA_F && !lf->zero_aura_rsvd) {
     881                 :            :                 rc = NPA_ERR_ALLOC;
     882                 :          0 :                 goto error;
     883                 :            :         }
     884                 :            : 
     885         [ #  # ]:          0 :         if (aura == NULL) {
     886                 :            :                 memset(&defaura, 0, sizeof(struct npa_aura_s));
     887                 :            :                 aura = &defaura;
     888                 :            :         }
     889         [ #  # ]:          0 :         if (pool == NULL) {
     890                 :            :                 memset(&defpool, 0, sizeof(struct npa_pool_s));
     891                 :          0 :                 defpool.nat_align = 1;
     892                 :          0 :                 defpool.buf_offset = 1;
     893                 :            :                 pool = &defpool;
     894                 :            :         }
     895                 :            : 
     896         [ #  # ]:          0 :         if (flags & ROC_NPA_FORCE_OPAQUE_MODE_F)
     897                 :          0 :                 pool->nat_align = 0;
     898                 :            : 
     899                 :            :         /* The pool argument will be a pointer to a npa_cn20k_halo_s structure
     900                 :            :          * when ROC_NPA_HALO_F flag is set. Note that halo and pool contexts
     901                 :            :          * are same in size and the fields that are modified here are 1:1
     902                 :            :          * mapped in these structures.
     903                 :            :          */
     904   [ #  #  #  # ]:          0 :         if (roc_feature_npa_has_halo() && (flags & ROC_NPA_HALO_F)) {
     905                 :            :                 struct npa_cn20k_halo_s *halo = (struct npa_cn20k_halo_s *)pool;
     906                 :          0 :                 rc = npa_halo_alloc(lf, block_size, block_count, halo, aura_handle, flags);
     907                 :            :         } else {
     908                 :          0 :                 rc = npa_aura_pool_pair_alloc(lf, block_size, block_count, aura, pool,
     909                 :            :                                               aura_handle, flags);
     910                 :            :         }
     911                 :            : 
     912         [ #  # ]:          0 :         if (rc) {
     913                 :          0 :                 plt_err("Failed to alloc pool/aura/halo rc=%d", rc);
     914                 :          0 :                 goto error;
     915                 :            :         }
     916                 :            : 
     917                 :          0 :         plt_npa_dbg("lf=%p block_sz=%d block_count=%d aura_handle=0x%" PRIx64,
     918                 :            :                     lf, block_size, block_count, *aura_handle);
     919                 :            : 
     920                 :            :         /* Just hold the reference of the object */
     921                 :          0 :         __atomic_fetch_add(&idev->npa_refcnt, 1, __ATOMIC_SEQ_CST);
     922                 :          0 : error:
     923                 :          0 :         return rc;
     924                 :            : }
     925                 :            : 
     926                 :            : static int
     927                 :          0 : npa_aura_alloc(struct npa_lf *lf, const uint32_t block_count, int pool_id,
     928                 :            :                struct npa_aura_s *aura, uint64_t *aura_handle, uint32_t flags)
     929                 :            : {
     930                 :            :         int rc, aura_id;
     931                 :            : 
     932                 :            :         /* Sanity check */
     933   [ #  #  #  # ]:          0 :         if (!lf || !aura || !aura_handle)
     934                 :            :                 return NPA_ERR_PARAM;
     935                 :            : 
     936                 :          0 :         roc_npa_dev_lock();
     937                 :            :         /* Get aura_id from resource bitmap */
     938                 :          0 :         aura_id = find_free_aura(lf, flags);
     939         [ #  # ]:          0 :         if (aura_id < 0) {
     940                 :          0 :                 roc_npa_dev_unlock();
     941                 :          0 :                 return NPA_ERR_AURA_ID_ALLOC;
     942                 :            :         }
     943                 :            : 
     944                 :            :         /* Mark aura as reserved */
     945                 :          0 :         plt_bitmap_clear(lf->npa_bmp, aura_id);
     946                 :            : 
     947                 :          0 :         roc_npa_dev_unlock();
     948         [ #  # ]:          0 :         rc = (aura_id < 0 || pool_id >= (int)lf->nr_pools ||
     949         [ #  # ]:          0 :               aura_id >= (int)BIT_ULL(6 + lf->aura_sz)) ?
     950                 :            :                         NPA_ERR_AURA_ID_ALLOC :
     951                 :            :                         0;
     952                 :            :         if (rc)
     953                 :          0 :                 goto exit;
     954                 :            : 
     955                 :            :         /* Update aura fields */
     956                 :          0 :         aura->pool_addr = pool_id; /* AF will translate to associated poolctx */
     957         [ #  # ]:          0 :         aura->ena = 1;
     958                 :          0 :         aura->shift = plt_log2_u32(block_count);
     959         [ #  # ]:          0 :         aura->shift = aura->shift < 8 ? 0 : aura->shift - 8;
     960                 :          0 :         aura->limit = block_count;
     961                 :          0 :         aura->pool_caching = 1;
     962                 :            :         aura->err_int_ena = BIT(NPA_AURA_ERR_INT_AURA_ADD_OVER);
     963                 :            :         aura->err_int_ena |= BIT(NPA_AURA_ERR_INT_AURA_ADD_UNDER);
     964                 :            :         aura->err_int_ena |= BIT(NPA_AURA_ERR_INT_AURA_FREE_UNDER);
     965                 :          0 :         aura->err_int_ena |= BIT(NPA_AURA_ERR_INT_POOL_DIS);
     966                 :          0 :         aura->avg_con = 0;
     967                 :            :         /* Many to one reduction */
     968                 :          0 :         aura->err_qint_idx = aura_id % lf->qints;
     969                 :            : 
     970                 :            :         /* Issue AURA_INIT op */
     971                 :          0 :         rc = npa_aura_init(lf->mbox, aura_id, aura);
     972         [ #  # ]:          0 :         if (rc)
     973                 :            :                 return rc;
     974                 :            : 
     975                 :          0 :         lf->aura_attr[aura_id].shift = aura->shift;
     976                 :          0 :         lf->aura_attr[aura_id].limit = aura->limit;
     977                 :          0 :         *aura_handle = roc_npa_aura_handle_gen(aura_id, lf->base);
     978                 :            : 
     979                 :          0 :         return 0;
     980                 :            : 
     981                 :            : exit:
     982                 :          0 :         return rc;
     983                 :            : }
     984                 :            : 
     985                 :            : int
     986                 :          0 : roc_npa_aura_create(uint64_t *aura_handle, uint32_t block_count,
     987                 :            :                     struct npa_aura_s *aura, int pool_id, uint32_t flags)
     988                 :            : {
     989                 :            :         struct npa_aura_s defaura;
     990                 :            :         struct idev_cfg *idev;
     991                 :            :         struct npa_lf *lf;
     992                 :            :         int rc;
     993                 :            : 
     994                 :          0 :         lf = idev_npa_obj_get();
     995         [ #  # ]:          0 :         if (lf == NULL) {
     996                 :            :                 rc = NPA_ERR_DEVICE_NOT_BOUNDED;
     997                 :          0 :                 goto error;
     998                 :            :         }
     999                 :            : 
    1000                 :          0 :         idev = idev_get_cfg();
    1001         [ #  # ]:          0 :         if (idev == NULL) {
    1002                 :            :                 rc = NPA_ERR_ALLOC;
    1003                 :          0 :                 goto error;
    1004                 :            :         }
    1005                 :            : 
    1006   [ #  #  #  # ]:          0 :         if (flags & ROC_NPA_ZERO_AURA_F && !lf->zero_aura_rsvd) {
    1007                 :            :                 rc = NPA_ERR_ALLOC;
    1008                 :          0 :                 goto error;
    1009                 :            :         }
    1010                 :            : 
    1011         [ #  # ]:          0 :         if (aura == NULL) {
    1012                 :            :                 memset(&defaura, 0, sizeof(struct npa_aura_s));
    1013                 :            :                 aura = &defaura;
    1014                 :            :         }
    1015                 :            : 
    1016                 :          0 :         rc = npa_aura_alloc(lf, block_count, pool_id, aura, aura_handle, flags);
    1017         [ #  # ]:          0 :         if (rc) {
    1018                 :          0 :                 plt_err("Failed to alloc aura rc=%d", rc);
    1019                 :          0 :                 goto error;
    1020                 :            :         }
    1021                 :            : 
    1022                 :          0 :         plt_npa_dbg("lf=%p aura_handle=0x%" PRIx64, lf, *aura_handle);
    1023                 :            : 
    1024                 :            :         /* Just hold the reference of the object */
    1025                 :          0 :         __atomic_fetch_add(&idev->npa_refcnt, 1, __ATOMIC_SEQ_CST);
    1026                 :          0 : error:
    1027                 :          0 :         return rc;
    1028                 :            : }
    1029                 :            : 
    1030                 :            : int
    1031                 :          0 : roc_npa_aura_limit_modify(uint64_t aura_handle, uint16_t aura_limit)
    1032                 :            : {
    1033                 :            :         struct npa_aq_enq_req *aura_req;
    1034                 :            :         struct npa_lf *lf;
    1035                 :            :         struct mbox *mbox;
    1036                 :            :         int rc;
    1037                 :            : 
    1038                 :          0 :         lf = idev_npa_obj_get();
    1039         [ #  # ]:          0 :         if (lf == NULL)
    1040                 :            :                 return NPA_ERR_DEVICE_NOT_BOUNDED;
    1041                 :            : 
    1042                 :          0 :         mbox = mbox_get(lf->mbox);
    1043         [ #  # ]:          0 :         if (roc_model_is_cn20k()) {
    1044                 :            :                 struct npa_cn20k_aq_enq_req *aura_req_cn20k;
    1045                 :          0 :                 aura_req_cn20k = mbox_alloc_msg_npa_cn20k_aq_enq(mbox);
    1046                 :            :                 aura_req = (struct npa_aq_enq_req *)aura_req_cn20k;
    1047                 :            :         } else {
    1048                 :          0 :                 aura_req = mbox_alloc_msg_npa_aq_enq(mbox);
    1049                 :            :         }
    1050         [ #  # ]:          0 :         if (aura_req == NULL) {
    1051                 :            :                 rc = -ENOMEM;
    1052                 :          0 :                 goto exit;
    1053                 :            :         }
    1054                 :          0 :         aura_req->aura_id = roc_npa_aura_handle_to_aura(aura_handle);
    1055                 :          0 :         aura_req->ctype = NPA_AQ_CTYPE_AURA;
    1056                 :          0 :         aura_req->op = NPA_AQ_INSTOP_WRITE;
    1057                 :            : 
    1058                 :          0 :         aura_req->aura.limit = aura_limit;
    1059                 :          0 :         aura_req->aura_mask.limit = ~(aura_req->aura_mask.limit);
    1060                 :          0 :         rc = mbox_process(mbox);
    1061         [ #  # ]:          0 :         if (rc)
    1062                 :          0 :                 goto exit;
    1063                 :          0 :         lf->aura_attr[aura_req->aura_id].limit = aura_req->aura.limit;
    1064                 :          0 : exit:
    1065                 :            :         mbox_put(mbox);
    1066                 :          0 :         return rc;
    1067                 :            : }
    1068                 :            : 
    1069                 :            : static int
    1070                 :          0 : npa_halo_free(struct npa_lf *lf, uint64_t aura_handle)
    1071                 :            : {
    1072                 :            :         char name[PLT_MEMZONE_NAMESIZE];
    1073                 :            :         int aura_id, pool_id, rc;
    1074                 :            : 
    1075         [ #  # ]:          0 :         if (!lf || !aura_handle)
    1076                 :            :                 return NPA_ERR_PARAM;
    1077                 :            : 
    1078                 :          0 :         aura_id = roc_npa_aura_handle_to_aura(aura_handle);
    1079                 :            :         pool_id = aura_id;
    1080                 :          0 :         rc = npa_halo_fini(lf->mbox, aura_id, aura_handle);
    1081                 :          0 :         rc |= npa_stack_dma_free(lf, name, pool_id);
    1082                 :          0 :         memset(&lf->aura_attr[aura_id], 0, sizeof(struct npa_aura_attr));
    1083                 :            : 
    1084                 :          0 :         roc_npa_dev_lock();
    1085                 :          0 :         plt_bitmap_set(lf->npa_bmp, aura_id);
    1086                 :          0 :         roc_npa_dev_unlock();
    1087                 :            : 
    1088                 :          0 :         return rc;
    1089                 :            : }
    1090                 :            : 
    1091                 :            : static int
    1092                 :          0 : npa_aura_pool_pair_free(struct npa_lf *lf, uint64_t aura_handle)
    1093                 :            : {
    1094                 :            :         char name[PLT_MEMZONE_NAMESIZE];
    1095                 :            :         int aura_id, pool_id, rc;
    1096                 :            : 
    1097         [ #  # ]:          0 :         if (!lf || !aura_handle)
    1098                 :            :                 return NPA_ERR_PARAM;
    1099                 :            : 
    1100                 :          0 :         aura_id = roc_npa_aura_handle_to_aura(aura_handle);
    1101                 :            :         pool_id = aura_id;
    1102                 :          0 :         rc = npa_aura_pool_fini(lf->mbox, aura_id, aura_handle);
    1103                 :          0 :         rc |= npa_stack_dma_free(lf, name, pool_id);
    1104                 :          0 :         memset(&lf->aura_attr[aura_id], 0, sizeof(struct npa_aura_attr));
    1105                 :            : 
    1106                 :          0 :         roc_npa_dev_lock();
    1107                 :          0 :         plt_bitmap_set(lf->npa_bmp, aura_id);
    1108                 :          0 :         roc_npa_dev_unlock();
    1109                 :            : 
    1110                 :          0 :         return rc;
    1111                 :            : }
    1112                 :            : 
    1113                 :            : int
    1114                 :          0 : roc_npa_pool_destroy(uint64_t aura_handle)
    1115                 :            : {
    1116                 :          0 :         struct npa_lf *lf = idev_npa_obj_get();
    1117                 :            :         int rc = 0, aura_id;
    1118                 :            : 
    1119         [ #  # ]:          0 :         if (lf == NULL)
    1120                 :            :                 return NPA_ERR_DEVICE_NOT_BOUNDED;
    1121                 :            : 
    1122                 :          0 :         plt_npa_dbg("lf=%p aura_handle=0x%" PRIx64, lf, aura_handle);
    1123                 :            :         aura_id = roc_npa_aura_handle_to_aura(aura_handle);
    1124                 :            : 
    1125         [ #  # ]:          0 :         if (lf->aura_attr[aura_id].halo)
    1126                 :          0 :                 rc = npa_halo_free(lf, aura_handle);
    1127                 :            :         else
    1128                 :          0 :                 rc = npa_aura_pool_pair_free(lf, aura_handle);
    1129                 :            : 
    1130         [ #  # ]:          0 :         if (rc)
    1131                 :          0 :                 plt_err("Failed to destroy pool/aura/halo rc=%d", rc);
    1132                 :            : 
    1133                 :            :         /* Release the reference of npa */
    1134                 :          0 :         rc |= npa_lf_fini();
    1135                 :          0 :         return rc;
    1136                 :            : }
    1137                 :            : 
    1138                 :            : static int
    1139                 :          0 : npa_aura_free(struct npa_lf *lf, uint64_t aura_handle)
    1140                 :            : {
    1141                 :            :         int aura_id, rc;
    1142                 :            : 
    1143         [ #  # ]:          0 :         if (!lf || !aura_handle)
    1144                 :            :                 return NPA_ERR_PARAM;
    1145                 :            : 
    1146                 :            :         aura_id = roc_npa_aura_handle_to_aura(aura_handle);
    1147                 :          0 :         rc = npa_aura_fini(lf->mbox, aura_id);
    1148                 :            : 
    1149         [ #  # ]:          0 :         if (rc)
    1150                 :            :                 return rc;
    1151                 :            : 
    1152                 :          0 :         memset(&lf->aura_attr[aura_id], 0, sizeof(struct npa_aura_attr));
    1153                 :            : 
    1154                 :          0 :         roc_npa_dev_lock();
    1155                 :          0 :         plt_bitmap_set(lf->npa_bmp, aura_id);
    1156                 :          0 :         roc_npa_dev_unlock();
    1157                 :            : 
    1158                 :          0 :         return rc;
    1159                 :            : }
    1160                 :            : 
    1161                 :            : int
    1162                 :          0 : roc_npa_aura_destroy(uint64_t aura_handle)
    1163                 :            : {
    1164                 :          0 :         struct npa_lf *lf = idev_npa_obj_get();
    1165                 :            :         int rc = 0;
    1166                 :            : 
    1167                 :          0 :         plt_npa_dbg("lf=%p aura_handle=0x%" PRIx64, lf, aura_handle);
    1168                 :          0 :         rc = npa_aura_free(lf, aura_handle);
    1169         [ #  # ]:          0 :         if (rc)
    1170                 :          0 :                 plt_err("Failed to destroy aura rc=%d", rc);
    1171                 :            : 
    1172                 :            :         /* Release the reference of npa */
    1173                 :          0 :         rc |= npa_lf_fini();
    1174                 :          0 :         return rc;
    1175                 :            : }
    1176                 :            : 
    1177                 :            : int
    1178                 :          0 : roc_npa_pool_range_update_check(uint64_t aura_handle)
    1179                 :            : {
    1180                 :            :         __io struct npa_pool_s *pool;
    1181                 :            :         struct npa_aq_enq_req *req;
    1182                 :            :         struct npa_aq_enq_rsp *rsp;
    1183                 :            :         struct npa_aura_lim *lim;
    1184                 :            :         struct mbox *mbox;
    1185                 :            :         struct npa_lf *lf;
    1186                 :            :         uint64_t aura_id;
    1187                 :            :         int rc;
    1188                 :            : 
    1189                 :          0 :         lf = idev_npa_obj_get();
    1190         [ #  # ]:          0 :         if (lf == NULL)
    1191                 :            :                 return NPA_ERR_PARAM;
    1192                 :            : 
    1193                 :          0 :         lim = lf->aura_lim;
    1194                 :            : 
    1195                 :          0 :         mbox = mbox_get(lf->mbox);
    1196         [ #  # ]:          0 :         if (roc_model_is_cn20k()) {
    1197                 :            :                 struct npa_cn20k_aq_enq_req *req_cn20k;
    1198                 :          0 :                 req_cn20k = mbox_alloc_msg_npa_cn20k_aq_enq(mbox);
    1199                 :            :                 req = (struct npa_aq_enq_req *)req_cn20k;
    1200                 :            :         } else {
    1201                 :          0 :                 req = mbox_alloc_msg_npa_aq_enq(mbox);
    1202                 :            :         }
    1203         [ #  # ]:          0 :         if (req == NULL) {
    1204                 :            :                 rc = -ENOSPC;
    1205                 :          0 :                 goto exit;
    1206                 :            :         }
    1207                 :            : 
    1208                 :            :         aura_id = roc_npa_aura_handle_to_aura(aura_handle);
    1209                 :          0 :         req->aura_id = aura_id;
    1210         [ #  # ]:          0 :         if (lf->aura_attr[aura_id].halo)
    1211                 :          0 :                 req->ctype = NPA_AQ_CTYPE_HALO;
    1212                 :            :         else
    1213                 :          0 :                 req->ctype = NPA_AQ_CTYPE_POOL;
    1214                 :          0 :         req->op = NPA_AQ_INSTOP_READ;
    1215                 :            : 
    1216                 :            :         rc = mbox_process_msg(mbox, (void *)&rsp);
    1217         [ #  # ]:          0 :         if (rc) {
    1218                 :          0 :                 plt_err("Failed to get pool(0x%" PRIx64 ") context", aura_id);
    1219                 :          0 :                 goto exit;
    1220                 :            :         }
    1221                 :            : 
    1222                 :          0 :         pool = &rsp->pool;
    1223         [ #  # ]:          0 :         if (lim[aura_id].ptr_start != pool->ptr_start ||
    1224         [ #  # ]:          0 :             lim[aura_id].ptr_end != pool->ptr_end) {
    1225                 :          0 :                 plt_err("Range update failed on pool(0x%" PRIx64 ")", aura_id);
    1226                 :            :                 rc = NPA_ERR_PARAM;
    1227                 :          0 :                 goto exit;
    1228                 :            :         }
    1229                 :            : 
    1230                 :            :         rc = 0;
    1231                 :          0 : exit:
    1232                 :            :         mbox_put(mbox);
    1233                 :          0 :         return rc;
    1234                 :            : }
    1235                 :            : 
    1236                 :            : uint64_t
    1237                 :          0 : roc_npa_zero_aura_handle(void)
    1238                 :            : {
    1239                 :            :         struct idev_cfg *idev;
    1240                 :            :         struct npa_lf *lf;
    1241                 :            : 
    1242                 :          0 :         lf = idev_npa_obj_get();
    1243         [ #  # ]:          0 :         if (lf == NULL)
    1244                 :            :                 return NPA_ERR_DEVICE_NOT_BOUNDED;
    1245                 :            : 
    1246                 :          0 :         idev = idev_get_cfg();
    1247         [ #  # ]:          0 :         if (idev == NULL)
    1248                 :            :                 return NPA_ERR_ALLOC;
    1249                 :            : 
    1250                 :            :         /* Return aura handle only if reserved */
    1251         [ #  # ]:          0 :         if (lf->zero_aura_rsvd)
    1252                 :          0 :                 return roc_npa_aura_handle_gen(0, lf->base);
    1253                 :            :         return 0;
    1254                 :            : }
    1255                 :            : 
    1256                 :            : int
    1257                 :          0 : roc_npa_pool_bp_configure(uint64_t aura_handle, uint16_t bpid, uint8_t bp_thresh, uint8_t bp_class,
    1258                 :            :                           bool enable)
    1259                 :            : {
    1260                 :          0 :         uint32_t pool_id = roc_npa_aura_handle_to_aura(aura_handle);
    1261                 :          0 :         struct npa_lf *lf = idev_npa_obj_get();
    1262                 :            :         struct npa_cn20k_aq_enq_req *aq;
    1263                 :            :         uint8_t bp, bp_ena;
    1264                 :            :         struct mbox *mbox;
    1265                 :            :         int rc = 0;
    1266                 :            : 
    1267                 :          0 :         plt_npa_dbg("Setting BPID %u BP_CLASS %u enable %u on pool %" PRIx64, bpid, bp_class,
    1268                 :            :                     bp_thresh, aura_handle);
    1269                 :            : 
    1270         [ #  # ]:          0 :         if (lf == NULL)
    1271                 :            :                 return NPA_ERR_PARAM;
    1272                 :            : 
    1273                 :          0 :         mbox = mbox_get(lf->mbox);
    1274                 :          0 :         aq = mbox_alloc_msg_npa_cn20k_aq_enq(mbox);
    1275         [ #  # ]:          0 :         if (aq == NULL) {
    1276                 :            :                 rc = -ENOSPC;
    1277                 :          0 :                 goto fail;
    1278                 :            :         }
    1279                 :            : 
    1280                 :          0 :         aq->aura_id = pool_id;
    1281                 :          0 :         aq->ctype = NPA_AQ_CTYPE_POOL;
    1282                 :          0 :         aq->op = NPA_AQ_INSTOP_WRITE;
    1283                 :            : 
    1284         [ #  # ]:          0 :         if (enable) {
    1285                 :          0 :                 aq->pool.bpid_0 = bpid;
    1286                 :          0 :                 aq->pool_mask.bpid_0 = ~(aq->pool_mask.bpid_0);
    1287                 :            : 
    1288                 :            :                 bp = bp_thresh;
    1289                 :            :         } else {
    1290                 :            :                 bp = 0;
    1291                 :            :         }
    1292                 :            : 
    1293   [ #  #  #  #  :          0 :         switch (bp_class) {
             #  #  #  #  
                      # ]
    1294                 :          0 :         case 0:
    1295                 :          0 :                 aq->pool.bp_0 = bp;
    1296                 :          0 :                 aq->pool_mask.bp_0 = ~(aq->pool_mask.bp_0);
    1297                 :          0 :                 aq->pool.bp_ena_0 = enable;
    1298                 :          0 :                 aq->pool_mask.bp_ena_0 = ~(aq->pool_mask.bp_ena_0);
    1299                 :          0 :                 break;
    1300                 :          0 :         case 1:
    1301                 :          0 :                 aq->pool.bp_1 = bp;
    1302                 :          0 :                 aq->pool_mask.bp_1 = ~(aq->pool_mask.bp_1);
    1303                 :          0 :                 aq->pool.bp_ena_1 = enable;
    1304                 :          0 :                 aq->pool_mask.bp_ena_1 = ~(aq->pool_mask.bp_ena_1);
    1305                 :          0 :                 break;
    1306                 :          0 :         case 2:
    1307                 :          0 :                 aq->pool.bp_2 = bp;
    1308                 :          0 :                 aq->pool_mask.bp_2 = ~(aq->pool_mask.bp_2);
    1309                 :          0 :                 aq->pool.bp_ena_2 = enable;
    1310                 :          0 :                 aq->pool_mask.bp_ena_2 = ~(aq->pool_mask.bp_ena_2);
    1311                 :          0 :                 break;
    1312                 :          0 :         case 3:
    1313                 :          0 :                 aq->pool.bp_3 = bp;
    1314                 :          0 :                 aq->pool_mask.bp_3 = ~(aq->pool_mask.bp_3);
    1315                 :          0 :                 aq->pool.bp_ena_3 = enable;
    1316                 :          0 :                 aq->pool_mask.bp_ena_3 = ~(aq->pool_mask.bp_ena_3);
    1317                 :          0 :                 break;
    1318                 :          0 :         case 4:
    1319                 :          0 :                 aq->pool.bp_4 = bp;
    1320                 :          0 :                 aq->pool_mask.bp_4 = ~(aq->pool_mask.bp_4);
    1321                 :          0 :                 aq->pool.bp_ena_4 = enable;
    1322                 :          0 :                 aq->pool_mask.bp_ena_4 = ~(aq->pool_mask.bp_ena_4);
    1323                 :          0 :                 break;
    1324                 :          0 :         case 5:
    1325                 :          0 :                 aq->pool.bp_5 = bp;
    1326                 :          0 :                 aq->pool_mask.bp_5 = ~(aq->pool_mask.bp_5);
    1327                 :          0 :                 aq->pool.bp_ena_5 = enable;
    1328                 :          0 :                 aq->pool_mask.bp_ena_5 = ~(aq->pool_mask.bp_ena_5);
    1329                 :          0 :                 break;
    1330                 :          0 :         case 6:
    1331                 :          0 :                 aq->pool.bp_6 = bp;
    1332                 :          0 :                 aq->pool_mask.bp_6 = ~(aq->pool_mask.bp_6);
    1333                 :          0 :                 aq->pool.bp_ena_6 = enable;
    1334                 :          0 :                 aq->pool_mask.bp_ena_6 = ~(aq->pool_mask.bp_ena_6);
    1335                 :          0 :                 break;
    1336                 :          0 :         case 7:
    1337                 :          0 :                 aq->pool.bp_7 = bp;
    1338                 :          0 :                 aq->pool_mask.bp_7 = ~(aq->pool_mask.bp_7);
    1339                 :          0 :                 aq->pool.bp_ena_7 = enable;
    1340                 :          0 :                 aq->pool_mask.bp_ena_7 = ~(aq->pool_mask.bp_ena_7);
    1341                 :          0 :                 break;
    1342                 :          0 :         default:
    1343                 :            :                 rc = -EINVAL;
    1344                 :          0 :                 goto fail;
    1345                 :            :         }
    1346                 :            : 
    1347                 :          0 :         rc = mbox_process(mbox);
    1348         [ #  # ]:          0 :         if (rc)
    1349                 :          0 :                 goto fail;
    1350                 :            : 
    1351                 :          0 :         bp_ena = lf->aura_attr[pool_id].bp_ena;
    1352                 :          0 :         bp_ena &= ~(1 << bp_class);
    1353                 :          0 :         bp_ena |= (enable << bp_class);
    1354                 :            : 
    1355   [ #  #  #  # ]:          0 :         if (enable && !lf->aura_attr[pool_id].bp_ena)
    1356                 :          0 :                 lf->aura_attr[pool_id].nix0_bpid = bpid;
    1357   [ #  #  #  # ]:          0 :         else if (!enable && !lf->aura_attr[pool_id].bp_ena)
    1358                 :          0 :                 lf->aura_attr[pool_id].nix0_bpid = 0;
    1359                 :            : 
    1360                 :          0 :         lf->aura_attr[pool_id].bp_ena = bp_ena;
    1361                 :          0 :         lf->aura_attr[pool_id].bp_thresh[bp_class] = bp;
    1362                 :            : 
    1363                 :          0 : fail:
    1364                 :            :         mbox_put(mbox);
    1365                 :          0 :         return rc;
    1366                 :            : }
    1367                 :            : 
    1368                 :            : int
    1369                 :          0 : roc_npa_aura_bp_configure(uint64_t aura_handle, uint16_t bpid, uint8_t bp_intf, uint8_t bp_thresh,
    1370                 :            :                           bool enable)
    1371                 :            : {
    1372                 :          0 :         uint32_t aura_id = roc_npa_aura_handle_to_aura(aura_handle);
    1373                 :          0 :         struct npa_lf *lf = idev_npa_obj_get();
    1374                 :            :         struct npa_aq_enq_req *req;
    1375                 :            :         struct mbox *mbox;
    1376                 :            :         int rc = 0;
    1377                 :            : 
    1378                 :          0 :         plt_npa_dbg("Setting BPID %u BP_INTF 0x%x BP_THRESH %u enable %u on aura %" PRIx64,
    1379                 :            :                     bpid, bp_intf, bp_thresh, enable, aura_handle);
    1380                 :            : 
    1381         [ #  # ]:          0 :         if (lf == NULL)
    1382                 :            :                 return NPA_ERR_PARAM;
    1383                 :            : 
    1384                 :          0 :         mbox = mbox_get(lf->mbox);
    1385                 :          0 :         req = mbox_alloc_msg_npa_aq_enq(mbox);
    1386         [ #  # ]:          0 :         if (req == NULL) {
    1387                 :            :                 rc = -ENOMEM;
    1388                 :          0 :                 goto fail;
    1389                 :            :         }
    1390                 :            : 
    1391                 :          0 :         req->aura_id = aura_id;
    1392                 :          0 :         req->ctype = NPA_AQ_CTYPE_AURA;
    1393                 :          0 :         req->op = NPA_AQ_INSTOP_WRITE;
    1394                 :            : 
    1395         [ #  # ]:          0 :         if (enable) {
    1396         [ #  # ]:          0 :                 if (bp_intf & 0x1) {
    1397                 :          0 :                         req->aura.nix0_bpid = bpid;
    1398                 :          0 :                         req->aura_mask.nix0_bpid = ~(req->aura_mask.nix0_bpid);
    1399                 :            :                 } else {
    1400                 :          0 :                         req->aura.nix1_bpid = bpid;
    1401                 :          0 :                         req->aura_mask.nix1_bpid = ~(req->aura_mask.nix1_bpid);
    1402                 :            :                 }
    1403                 :          0 :                 req->aura.bp = bp_thresh;
    1404                 :          0 :                 req->aura_mask.bp = ~(req->aura_mask.bp);
    1405                 :            :         } else {
    1406                 :          0 :                 req->aura.bp = 0;
    1407                 :          0 :                 req->aura_mask.bp = ~(req->aura_mask.bp);
    1408                 :            :         }
    1409                 :            : 
    1410                 :          0 :         req->aura.bp_ena = bp_intf;
    1411                 :          0 :         req->aura_mask.bp_ena = ~(req->aura_mask.bp_ena);
    1412                 :            : 
    1413                 :          0 :         rc = mbox_process(mbox);
    1414         [ #  # ]:          0 :         if (rc)
    1415                 :          0 :                 goto fail;
    1416                 :            : 
    1417                 :          0 :         lf->aura_attr[aura_id].nix0_bpid = req->aura.nix0_bpid;
    1418                 :          0 :         lf->aura_attr[aura_id].nix1_bpid = req->aura.nix1_bpid;
    1419                 :          0 :         lf->aura_attr[aura_id].bp_ena = req->aura.bp_ena;
    1420                 :          0 :         lf->aura_attr[aura_id].bp = req->aura.bp;
    1421                 :          0 : fail:
    1422                 :            :         mbox_put(mbox);
    1423                 :          0 :         return rc;
    1424                 :            : }
    1425                 :            : 
    1426                 :            : static inline int
    1427                 :          0 : npa_attach(struct mbox *m_box)
    1428                 :            : {
    1429                 :            :         struct mbox *mbox = mbox_get(m_box);
    1430                 :            :         struct rsrc_attach_req *req;
    1431                 :            :         int rc;
    1432                 :            : 
    1433                 :          0 :         req = mbox_alloc_msg_attach_resources(mbox);
    1434         [ #  # ]:          0 :         if (req == NULL) {
    1435                 :            :                 rc = -ENOSPC;
    1436                 :          0 :                 goto exit;
    1437                 :            :         }
    1438                 :          0 :         req->modify = true;
    1439                 :          0 :         req->npalf = true;
    1440                 :            : 
    1441                 :          0 :         rc = mbox_process(mbox);
    1442                 :          0 : exit:
    1443                 :            :         mbox_put(mbox);
    1444                 :          0 :         return rc;
    1445                 :            : }
    1446                 :            : 
    1447                 :            : static inline int
    1448                 :          0 : npa_detach(struct mbox *m_box)
    1449                 :            : {
    1450                 :            :         struct mbox *mbox = mbox_get(m_box);
    1451                 :            :         struct rsrc_detach_req *req;
    1452                 :            :         int rc;
    1453                 :            : 
    1454                 :          0 :         req = mbox_alloc_msg_detach_resources(mbox);
    1455         [ #  # ]:          0 :         if (req == NULL) {
    1456                 :            :                 rc = -ENOSPC;
    1457                 :          0 :                 goto exit;
    1458                 :            :         }
    1459                 :          0 :         req->partial = true;
    1460                 :          0 :         req->npalf = true;
    1461                 :            : 
    1462                 :          0 :         rc = mbox_process(mbox);
    1463                 :          0 : exit:
    1464                 :            :         mbox_put(mbox);
    1465                 :          0 :         return rc;
    1466                 :            : }
    1467                 :            : 
    1468                 :            : static inline int
    1469                 :          0 : npa_get_msix_offset(struct mbox *m_box, uint16_t *npa_msixoff)
    1470                 :            : {
    1471                 :            :         struct mbox *mbox = mbox_get(m_box);
    1472                 :            :         struct msix_offset_rsp *msix_rsp;
    1473                 :            :         int rc;
    1474                 :            : 
    1475                 :            :         /* Initialize msixoff */
    1476                 :          0 :         *npa_msixoff = 0;
    1477                 :            :         /* Get NPA MSIX vector offsets */
    1478                 :          0 :         mbox_alloc_msg_msix_offset(mbox);
    1479                 :            :         rc = mbox_process_msg(mbox, (void *)&msix_rsp);
    1480         [ #  # ]:          0 :         if (rc == 0)
    1481                 :          0 :                 *npa_msixoff = msix_rsp->npa_msixoff;
    1482                 :            : 
    1483                 :            :         mbox_put(mbox);
    1484                 :          0 :         return rc;
    1485                 :            : }
    1486                 :            : 
    1487                 :            : static inline int
    1488                 :          0 : npa_lf_alloc(struct npa_lf *lf)
    1489                 :            : {
    1490                 :          0 :         struct mbox *mbox = mbox_get(lf->mbox);
    1491                 :            :         struct npa_lf_alloc_req *req;
    1492                 :            :         struct npa_lf_alloc_rsp *rsp;
    1493                 :            :         int rc;
    1494                 :            : 
    1495                 :          0 :         req = mbox_alloc_msg_npa_lf_alloc(mbox);
    1496         [ #  # ]:          0 :         if (req == NULL) {
    1497                 :            :                 rc =  -ENOSPC;
    1498                 :          0 :                 goto exit;
    1499                 :            :         }
    1500                 :          0 :         req->aura_sz = lf->aura_sz;
    1501                 :          0 :         req->nr_pools = lf->nr_pools;
    1502                 :            : 
    1503                 :            :         rc = mbox_process_msg(mbox, (void *)&rsp);
    1504         [ #  # ]:          0 :         if (rc) {
    1505                 :            :                 rc = NPA_ERR_ALLOC;
    1506                 :          0 :                 goto exit;
    1507                 :            :         }
    1508                 :            : 
    1509                 :          0 :         lf->stack_pg_ptrs = rsp->stack_pg_ptrs;
    1510                 :          0 :         lf->stack_pg_bytes = rsp->stack_pg_bytes;
    1511                 :          0 :         lf->qints = rsp->qints;
    1512                 :            : 
    1513                 :            :         rc = 0;
    1514                 :          0 : exit:
    1515                 :            :         mbox_put(mbox);
    1516                 :          0 :         return rc;
    1517                 :            : }
    1518                 :            : 
    1519                 :            : static int
    1520                 :          0 : npa_lf_free(struct mbox *mail_box)
    1521                 :            : {
    1522                 :            :         struct mbox *mbox = mbox_get(mail_box);
    1523                 :            :         int rc;
    1524                 :            : 
    1525                 :          0 :         mbox_alloc_msg_npa_lf_free(mbox);
    1526                 :          0 :         rc = mbox_process(mbox);
    1527                 :            :         mbox_put(mbox);
    1528                 :          0 :         return rc;
    1529                 :            : }
    1530                 :            : 
    1531                 :            : static inline uint32_t
    1532                 :            : aura_size_to_u32(uint8_t val)
    1533                 :            : {
    1534                 :            :         if (val == NPA_AURA_SZ_0)
    1535                 :            :                 return 128;
    1536                 :            :         if (val >= NPA_AURA_SZ_MAX)
    1537                 :            :                 return BIT_ULL(20);
    1538                 :            : 
    1539                 :            :         return 1 << (val + 6);
    1540                 :            : }
    1541                 :            : 
    1542                 :            : static inline void
    1543                 :          0 : pool_count_aura_sz_get(uint32_t *nr_pools, uint8_t *aura_sz)
    1544                 :            : {
    1545                 :            :         uint32_t val;
    1546                 :            : 
    1547                 :          0 :         val = roc_idev_npa_maxpools_get();
    1548                 :            :         if (val < aura_size_to_u32(NPA_AURA_SZ_128))
    1549                 :            :                 val = 128;
    1550                 :            :         if (val > aura_size_to_u32(NPA_AURA_SZ_1M))
    1551                 :            :                 val = BIT_ULL(20);
    1552                 :            : 
    1553                 :          0 :         roc_idev_npa_maxpools_set(val);
    1554                 :          0 :         *nr_pools = val;
    1555                 :          0 :         *aura_sz = plt_log2_u32(val) - 6;
    1556                 :          0 : }
    1557                 :            : 
    1558                 :            : static int
    1559                 :          0 : npa_dev_init(struct npa_lf *lf, uintptr_t base, struct mbox *mbox)
    1560                 :            : {
    1561                 :            :         uint32_t i, bmp_sz, nr_pools;
    1562                 :            :         uint8_t aura_sz;
    1563                 :            :         int rc;
    1564                 :            : 
    1565                 :            :         /* Sanity checks */
    1566   [ #  #  #  # ]:          0 :         if (!lf || !base || !mbox)
    1567                 :            :                 return NPA_ERR_PARAM;
    1568                 :            : 
    1569         [ #  # ]:          0 :         if (base & ROC_AURA_ID_MASK)
    1570                 :            :                 return NPA_ERR_BASE_INVALID;
    1571                 :            : 
    1572                 :          0 :         pool_count_aura_sz_get(&nr_pools, &aura_sz);
    1573         [ #  # ]:          0 :         if (aura_sz == NPA_AURA_SZ_0 || aura_sz >= NPA_AURA_SZ_MAX)
    1574                 :            :                 return NPA_ERR_PARAM;
    1575                 :            : 
    1576                 :            :         memset(lf, 0x0, sizeof(*lf));
    1577                 :          0 :         lf->base = base;
    1578                 :          0 :         lf->aura_sz = aura_sz;
    1579                 :          0 :         lf->nr_pools = nr_pools;
    1580                 :          0 :         lf->mbox = mbox;
    1581                 :            : 
    1582                 :          0 :         rc = npa_lf_alloc(lf);
    1583         [ #  # ]:          0 :         if (rc)
    1584                 :          0 :                 goto exit;
    1585                 :            : 
    1586                 :          0 :         bmp_sz = plt_bitmap_get_memory_footprint(nr_pools);
    1587                 :            : 
    1588                 :            :         /* Allocate memory for bitmap */
    1589                 :          0 :         lf->npa_bmp_mem = plt_zmalloc(bmp_sz, ROC_ALIGN);
    1590         [ #  # ]:          0 :         if (lf->npa_bmp_mem == NULL) {
    1591                 :            :                 rc = NPA_ERR_ALLOC;
    1592                 :          0 :                 goto lf_free;
    1593                 :            :         }
    1594                 :            : 
    1595                 :            :         /* Initialize pool resource bitmap array */
    1596                 :          0 :         lf->npa_bmp = plt_bitmap_init(nr_pools, lf->npa_bmp_mem, bmp_sz);
    1597         [ #  # ]:          0 :         if (lf->npa_bmp == NULL) {
    1598                 :            :                 rc = NPA_ERR_PARAM;
    1599                 :          0 :                 goto bmap_mem_free;
    1600                 :            :         }
    1601                 :            : 
    1602                 :            :         /* Mark all pools available */
    1603         [ #  # ]:          0 :         for (i = 0; i < nr_pools; i++)
    1604                 :          0 :                 plt_bitmap_set(lf->npa_bmp, i);
    1605                 :            : 
    1606                 :            :         /* Reserve zero aura for all models other than CN9K */
    1607         [ #  # ]:          0 :         if (!roc_model_is_cn9k())
    1608                 :          0 :                 lf->zero_aura_rsvd = true;
    1609                 :            : 
    1610                 :            :         /* Allocate memory for qint context */
    1611                 :          0 :         lf->npa_qint_mem = plt_zmalloc(sizeof(struct npa_qint) * nr_pools, 0);
    1612         [ #  # ]:          0 :         if (lf->npa_qint_mem == NULL) {
    1613                 :            :                 rc = NPA_ERR_ALLOC;
    1614                 :          0 :                 goto bmap_free;
    1615                 :            :         }
    1616                 :            : 
    1617                 :            :         /* Allocate memory for nap_aura_lim memory */
    1618                 :          0 :         lf->aura_lim = plt_zmalloc(sizeof(struct npa_aura_lim) * nr_pools, 0);
    1619         [ #  # ]:          0 :         if (lf->aura_lim == NULL) {
    1620                 :            :                 rc = NPA_ERR_ALLOC;
    1621                 :          0 :                 goto qint_free;
    1622                 :            :         }
    1623                 :            : 
    1624                 :            :         /* Allocate per-aura attribute */
    1625                 :          0 :         lf->aura_attr = plt_zmalloc(sizeof(struct npa_aura_attr) * nr_pools, 0);
    1626         [ #  # ]:          0 :         if (lf->aura_attr == NULL) {
    1627                 :            :                 rc = NPA_ERR_PARAM;
    1628                 :          0 :                 goto lim_free;
    1629                 :            :         }
    1630                 :            : 
    1631                 :            :         /* Init aura start & end limits */
    1632         [ #  # ]:          0 :         for (i = 0; i < nr_pools; i++) {
    1633                 :          0 :                 lf->aura_lim[i].ptr_start = UINT64_MAX;
    1634                 :          0 :                 lf->aura_lim[i].ptr_end = 0x0ull;
    1635                 :            :         }
    1636                 :            : 
    1637                 :            :         return 0;
    1638                 :            : 
    1639                 :            : lim_free:
    1640                 :          0 :         plt_free(lf->aura_lim);
    1641                 :          0 : qint_free:
    1642                 :          0 :         plt_free(lf->npa_qint_mem);
    1643                 :          0 : bmap_free:
    1644                 :            :         plt_bitmap_free(lf->npa_bmp);
    1645                 :          0 : bmap_mem_free:
    1646                 :          0 :         plt_free(lf->npa_bmp_mem);
    1647                 :          0 : lf_free:
    1648                 :          0 :         npa_lf_free(lf->mbox);
    1649                 :            : exit:
    1650                 :            :         return rc;
    1651                 :            : }
    1652                 :            : 
    1653                 :            : static int
    1654                 :          0 : npa_dev_fini(struct npa_lf *lf)
    1655                 :            : {
    1656         [ #  # ]:          0 :         if (!lf)
    1657                 :            :                 return NPA_ERR_PARAM;
    1658                 :            : 
    1659                 :          0 :         plt_free(lf->aura_lim);
    1660                 :          0 :         plt_free(lf->npa_qint_mem);
    1661                 :            :         plt_bitmap_free(lf->npa_bmp);
    1662                 :          0 :         plt_free(lf->npa_bmp_mem);
    1663                 :          0 :         plt_free(lf->aura_attr);
    1664                 :            : 
    1665                 :          0 :         return npa_lf_free(lf->mbox);
    1666                 :            : }
    1667                 :            : 
    1668                 :            : int
    1669                 :          0 : npa_lf_init(struct dev *dev, struct plt_pci_device *pci_dev)
    1670                 :            : {
    1671                 :          0 :         uint16_t npa_msixoff = 0;
    1672                 :            :         struct idev_cfg *idev;
    1673                 :            :         struct npa_lf *lf;
    1674                 :            :         int rc;
    1675                 :            : 
    1676                 :          0 :         idev = idev_get_cfg();
    1677         [ #  # ]:          0 :         if (idev == NULL)
    1678                 :            :                 return NPA_ERR_ALLOC;
    1679                 :            : 
    1680                 :            :         /* Not the first PCI device */
    1681         [ #  # ]:          0 :         if (__atomic_fetch_add(&idev->npa_refcnt, 1, __ATOMIC_SEQ_CST) != 0)
    1682                 :            :                 return 0;
    1683                 :            : 
    1684         [ #  # ]:          0 :         if (lf_init_cb) {
    1685                 :          0 :                 rc = (*lf_init_cb)(pci_dev);
    1686         [ #  # ]:          0 :                 if (rc)
    1687                 :          0 :                         goto fail;
    1688                 :            :         }
    1689                 :            : 
    1690                 :          0 :         rc = npa_attach(dev->mbox);
    1691         [ #  # ]:          0 :         if (rc)
    1692                 :          0 :                 goto fail;
    1693                 :            : 
    1694                 :          0 :         rc = npa_get_msix_offset(dev->mbox, &npa_msixoff);
    1695         [ #  # ]:          0 :         if (rc)
    1696                 :          0 :                 goto npa_detach;
    1697                 :            : 
    1698                 :          0 :         lf = &dev->npa;
    1699                 :          0 :         rc = npa_dev_init(lf, dev->bar2 + (RVU_BLOCK_ADDR_NPA << 20),
    1700                 :            :                           dev->mbox);
    1701         [ #  # ]:          0 :         if (rc)
    1702                 :          0 :                 goto npa_detach;
    1703                 :            : 
    1704                 :          0 :         lf->pf_func = dev->pf_func;
    1705                 :          0 :         lf->npa_msixoff = npa_msixoff;
    1706                 :          0 :         lf->intr_handle = pci_dev->intr_handle;
    1707                 :          0 :         lf->pci_dev = pci_dev;
    1708                 :            : 
    1709                 :          0 :         idev->npa_pf_func = dev->pf_func;
    1710                 :          0 :         idev->npa = lf;
    1711                 :            :         plt_wmb();
    1712                 :            : 
    1713                 :          0 :         rc = npa_register_irqs(lf);
    1714         [ #  # ]:          0 :         if (rc)
    1715                 :          0 :                 goto npa_fini;
    1716                 :            : 
    1717                 :          0 :         plt_npa_dbg("npa=%p max_pools=%d pf_func=0x%x msix=0x%x", lf,
    1718                 :            :                     roc_idev_npa_maxpools_get(), lf->pf_func, npa_msixoff);
    1719                 :            : 
    1720                 :          0 :         return 0;
    1721                 :            : 
    1722                 :            : npa_fini:
    1723                 :          0 :         npa_dev_fini(idev->npa);
    1724                 :          0 : npa_detach:
    1725                 :          0 :         npa_detach(dev->mbox);
    1726                 :          0 : fail:
    1727                 :          0 :         __atomic_fetch_sub(&idev->npa_refcnt, 1, __ATOMIC_SEQ_CST);
    1728                 :          0 :         return rc;
    1729                 :            : }
    1730                 :            : 
    1731                 :            : int
    1732                 :          0 : npa_lf_fini(void)
    1733                 :            : {
    1734                 :            :         struct idev_cfg *idev;
    1735                 :            :         int rc = 0;
    1736                 :            : 
    1737                 :          0 :         idev = idev_get_cfg();
    1738         [ #  # ]:          0 :         if (idev == NULL)
    1739                 :            :                 return NPA_ERR_ALLOC;
    1740                 :            : 
    1741                 :            :         /* Not the last PCI device */
    1742         [ #  # ]:          0 :         if (__atomic_fetch_sub(&idev->npa_refcnt, 1, __ATOMIC_SEQ_CST) - 1 != 0)
    1743                 :            :                 return 0;
    1744                 :            : 
    1745                 :          0 :         npa_unregister_irqs(idev->npa);
    1746                 :          0 :         rc |= npa_dev_fini(idev->npa);
    1747                 :          0 :         rc |= npa_detach(idev->npa->mbox);
    1748                 :          0 :         idev_set_defaults(idev);
    1749                 :            : 
    1750                 :          0 :         return rc;
    1751                 :            : }
    1752                 :            : 
    1753                 :            : int
    1754                 :          0 : roc_npa_dev_init(struct roc_npa *roc_npa)
    1755                 :            : {
    1756                 :            :         struct plt_pci_device *pci_dev;
    1757                 :            :         struct npa *npa;
    1758                 :            :         struct dev *dev;
    1759                 :            :         int rc;
    1760                 :            : 
    1761   [ #  #  #  # ]:          0 :         if (roc_npa == NULL || roc_npa->pci_dev == NULL)
    1762                 :            :                 return NPA_ERR_PARAM;
    1763                 :            : 
    1764                 :            :         PLT_STATIC_ASSERT(sizeof(struct npa) <= ROC_NPA_MEM_SZ);
    1765                 :            :         npa = roc_npa_to_npa_priv(roc_npa);
    1766                 :            :         memset(npa, 0, sizeof(*npa));
    1767                 :            :         pci_dev = roc_npa->pci_dev;
    1768                 :          0 :         dev = &npa->dev;
    1769                 :            : 
    1770                 :            :         /* Initialize device  */
    1771                 :          0 :         rc = dev_init(dev, pci_dev);
    1772         [ #  # ]:          0 :         if (rc) {
    1773                 :          0 :                 plt_err("Failed to init roc device");
    1774                 :          0 :                 goto fail;
    1775                 :            :         }
    1776                 :            : 
    1777                 :          0 :         npa->pci_dev = pci_dev;
    1778                 :          0 :         dev->drv_inited = true;
    1779                 :            : fail:
    1780                 :            :         return rc;
    1781                 :            : }
    1782                 :            : 
    1783                 :            : int
    1784                 :          0 : roc_npa_dev_fini(struct roc_npa *roc_npa)
    1785                 :            : {
    1786                 :            :         struct npa *npa = roc_npa_to_npa_priv(roc_npa);
    1787                 :            : 
    1788                 :            :         if (npa == NULL)
    1789                 :            :                 return NPA_ERR_PARAM;
    1790                 :            : 
    1791                 :          0 :         npa->dev.drv_inited = false;
    1792                 :          0 :         return dev_fini(&npa->dev, npa->pci_dev);
    1793                 :            : }
    1794                 :            : 
    1795                 :            : void
    1796                 :          0 : roc_npa_dev_lock(void)
    1797                 :            : {
    1798                 :          0 :         struct idev_cfg *idev = idev_get_cfg();
    1799                 :            : 
    1800         [ #  # ]:          0 :         if (idev != NULL)
    1801                 :          0 :                 plt_spinlock_lock(&idev->npa_dev_lock);
    1802                 :          0 : }
    1803                 :            : 
    1804                 :            : void
    1805                 :          0 : roc_npa_dev_unlock(void)
    1806                 :            : {
    1807                 :          0 :         struct idev_cfg *idev = idev_get_cfg();
    1808                 :            : 
    1809         [ #  # ]:          0 :         if (idev != NULL)
    1810                 :          0 :                 plt_spinlock_unlock(&idev->npa_dev_lock);
    1811                 :          0 : }
    1812                 :            : 
    1813                 :            : int
    1814                 :          0 : roc_npa_dpc_alloc(uint8_t *counter_id, uint64_t conf)
    1815                 :            : {
    1816                 :            :         struct npa_cn20k_dpc_alloc_req *req;
    1817                 :            :         struct npa_cn20k_dpc_alloc_rsp *rsp;
    1818                 :            :         int rc = NPA_ERR_DPC_ALLOC, off;
    1819                 :            :         struct mbox_dev *mdev;
    1820                 :            :         struct mbox *mbox;
    1821                 :            :         struct npa_lf *lf;
    1822                 :            : 
    1823                 :          0 :         lf = idev_npa_obj_get();
    1824         [ #  # ]:          0 :         if (lf == NULL) {
    1825                 :            :                 rc = NPA_ERR_DEVICE_NOT_BOUNDED;
    1826                 :            :                 return rc;
    1827                 :            :         }
    1828                 :          0 :         mdev = &lf->mbox->dev[0];
    1829                 :            : 
    1830                 :            :         mbox = mbox_get(lf->mbox);
    1831                 :          0 :         req = mbox_alloc_msg_npa_cn20k_dpc_alloc(mbox);
    1832         [ #  # ]:          0 :         if (req == NULL)
    1833                 :          0 :                 goto exit;
    1834                 :            : 
    1835                 :          0 :         req->dpc_conf = conf;
    1836                 :          0 :         rc = mbox_process(mbox);
    1837         [ #  # ]:          0 :         if (rc < 0)
    1838                 :          0 :                 goto exit;
    1839                 :            : 
    1840                 :          0 :         off = mbox->rx_start +
    1841                 :            :               PLT_ALIGN(sizeof(struct mbox_hdr), MBOX_MSG_ALIGN);
    1842                 :          0 :         rsp = (struct npa_cn20k_dpc_alloc_rsp *)((uintptr_t)mdev->mbase + off);
    1843                 :            : 
    1844         [ #  # ]:          0 :         if (rsp->hdr.rc != 0)
    1845                 :          0 :                 goto exit;
    1846                 :            : 
    1847                 :          0 :         *counter_id = rsp->cntr_id;
    1848                 :            :         rc = 0;
    1849                 :          0 : exit:
    1850                 :            :         mbox_put(mbox);
    1851                 :          0 :         return rc;
    1852                 :            : }
    1853                 :            : 
    1854                 :            : int
    1855                 :          0 : roc_npa_dpc_free(uint8_t counter_id)
    1856                 :            : {
    1857                 :            :         struct npa_cn20k_dpc_free_req *free_req;
    1858                 :            :         int rc = NPA_ERR_DPC_FREE, off;
    1859                 :            :         struct mbox_dev *mdev;
    1860                 :            :         struct msg_rsp *rsp;
    1861                 :            :         struct mbox *mbox;
    1862                 :            :         struct npa_lf *lf;
    1863                 :            : 
    1864                 :          0 :         lf = idev_npa_obj_get();
    1865         [ #  # ]:          0 :         if (lf == NULL) {
    1866                 :            :                 rc = NPA_ERR_DEVICE_NOT_BOUNDED;
    1867                 :            :                 return rc;
    1868                 :            :         }
    1869                 :          0 :         mdev = &lf->mbox->dev[0];
    1870                 :            :         mbox = mbox_get(lf->mbox);
    1871                 :          0 :         free_req = mbox_alloc_msg_npa_cn20k_dpc_free(mbox);
    1872         [ #  # ]:          0 :         if (free_req == NULL)
    1873                 :          0 :                 goto exit;
    1874                 :            : 
    1875                 :          0 :         free_req->cntr_id = counter_id;
    1876                 :          0 :         rc = mbox_process(mbox);
    1877         [ #  # ]:          0 :         if (rc < 0)
    1878                 :          0 :                 goto exit;
    1879                 :            : 
    1880                 :          0 :         off = mbox->rx_start +
    1881                 :            :               PLT_ALIGN(sizeof(struct mbox_hdr), MBOX_MSG_ALIGN);
    1882                 :          0 :         rsp = (struct msg_rsp *)((uintptr_t)mdev->mbase + off);
    1883         [ #  # ]:          0 :         if (rsp->hdr.rc != 0)
    1884                 :          0 :                 goto exit;
    1885                 :            : 
    1886                 :            :         rc = 0;
    1887                 :          0 : exit:
    1888                 :            :         mbox_put(mbox);
    1889                 :          0 :         return rc;
    1890                 :            : }
    1891                 :            : 
    1892                 :            : int
    1893                 :          0 : roc_npa_pool_dpc_enable(uint64_t aura_handle, uint8_t counter_id, uint32_t flags)
    1894                 :            : {
    1895                 :            :         struct npa_cn20k_aq_enq_req *req;
    1896                 :            :         struct npa_cn20k_aq_enq_rsp *rsp;
    1897                 :            :         int rc = -ENOSPC, off;
    1898                 :            :         struct mbox_dev *mdev;
    1899                 :            :         struct mbox *mbox;
    1900                 :            :         struct npa_lf *lf;
    1901                 :            : 
    1902                 :          0 :         lf = idev_npa_obj_get();
    1903         [ #  # ]:          0 :         if (lf == NULL) {
    1904                 :            :                 rc = NPA_ERR_DEVICE_NOT_BOUNDED;
    1905                 :            :                 return rc;
    1906                 :            :         }
    1907                 :          0 :         mdev = &lf->mbox->dev[0];
    1908                 :            :         mbox = mbox_get(lf->mbox);
    1909                 :          0 :         req = mbox_alloc_msg_npa_cn20k_aq_enq(mbox);
    1910         [ #  # ]:          0 :         if (req == NULL)
    1911                 :          0 :                 goto exit;
    1912                 :            : 
    1913                 :          0 :         req->aura_id = roc_npa_aura_handle_to_aura(aura_handle);
    1914                 :          0 :         req->op = NPA_AQ_INSTOP_WRITE;
    1915         [ #  # ]:          0 :         if (!(flags & ROC_NPA_HALO_F)) {
    1916                 :            :                 /* Enable DPC in Aura */
    1917                 :          0 :                 req->ctype = NPA_AQ_CTYPE_AURA;
    1918                 :          0 :                 req->aura.op_dpc_ena = 1;
    1919                 :          0 :                 req->aura.op_dpc_set = counter_id;
    1920                 :          0 :                 req->aura_mask.op_dpc_ena = 1;
    1921                 :          0 :                 req->aura_mask.op_dpc_set = 0;
    1922                 :          0 :                 req->aura_mask.op_dpc_set = ~req->aura_mask.op_dpc_set;
    1923                 :            :         } else {
    1924                 :            :                 /* Enable DPC in Halo */
    1925                 :          0 :                 req->ctype = NPA_AQ_CTYPE_HALO;
    1926                 :          0 :                 req->halo.op_dpc_ena = 1;
    1927                 :          0 :                 req->halo.op_dpc_set = counter_id;
    1928                 :          0 :                 req->halo_mask.op_dpc_ena = 1;
    1929                 :          0 :                 req->halo_mask.op_dpc_set = 0;
    1930                 :          0 :                 req->halo_mask.op_dpc_set = ~req->halo_mask.op_dpc_set;
    1931                 :            :         }
    1932                 :            : 
    1933                 :          0 :         rc = mbox_process(mbox);
    1934         [ #  # ]:          0 :         if (rc < 0)
    1935                 :          0 :                 goto exit;
    1936                 :            : 
    1937                 :          0 :         off = mbox->rx_start +
    1938                 :            :               PLT_ALIGN(sizeof(struct mbox_hdr), MBOX_MSG_ALIGN);
    1939                 :          0 :         rsp = (struct npa_cn20k_aq_enq_rsp *)((uintptr_t)mdev->mbase + off);
    1940         [ #  # ]:          0 :         if (rsp->hdr.rc != 0)
    1941                 :          0 :                 goto exit;
    1942                 :            : 
    1943                 :            :         rc = 0;
    1944                 :          0 : exit:
    1945                 :            :         mbox_put(mbox);
    1946                 :          0 :         return rc;
    1947                 :            : }
    1948                 :            : 
    1949                 :            : int
    1950                 :          0 : roc_npa_pool_dpc_disable(uint64_t aura_handle, uint32_t flags)
    1951                 :            : {
    1952                 :            :         struct npa_cn20k_aq_enq_req *req;
    1953                 :            :         struct npa_cn20k_aq_enq_rsp *rsp;
    1954                 :            :         int rc = -ENOSPC, off;
    1955                 :            :         struct mbox_dev *mdev;
    1956                 :            :         struct mbox *mbox;
    1957                 :            :         struct npa_lf *lf;
    1958                 :            : 
    1959                 :          0 :         lf = idev_npa_obj_get();
    1960         [ #  # ]:          0 :         if (lf == NULL) {
    1961                 :            :                 rc = NPA_ERR_DEVICE_NOT_BOUNDED;
    1962                 :            :                 return rc;
    1963                 :            :         }
    1964                 :          0 :         mdev = &lf->mbox->dev[0];
    1965                 :            :         mbox = mbox_get(lf->mbox);
    1966                 :          0 :         req = mbox_alloc_msg_npa_cn20k_aq_enq(mbox);
    1967         [ #  # ]:          0 :         if (req == NULL)
    1968                 :          0 :                 goto exit;
    1969                 :            : 
    1970                 :          0 :         req->aura_id = roc_npa_aura_handle_to_aura(aura_handle);
    1971                 :          0 :         req->op = NPA_AQ_INSTOP_WRITE;
    1972         [ #  # ]:          0 :         if (!(flags & ROC_NPA_HALO_F)) {
    1973                 :          0 :                 req->ctype = NPA_AQ_CTYPE_AURA;
    1974                 :          0 :                 req->aura.op_dpc_ena = 0;
    1975                 :          0 :                 req->aura_mask.op_dpc_ena = 1;
    1976                 :            :         } else {
    1977                 :          0 :                 req->ctype = NPA_AQ_CTYPE_HALO;
    1978                 :          0 :                 req->halo.op_dpc_ena = 0;
    1979                 :          0 :                 req->halo_mask.op_dpc_ena = 1;
    1980                 :            :         }
    1981                 :            : 
    1982                 :          0 :         rc = mbox_process(mbox);
    1983         [ #  # ]:          0 :         if (rc < 0)
    1984                 :          0 :                 goto exit;
    1985                 :            : 
    1986                 :          0 :         off = mbox->rx_start +
    1987                 :            :               PLT_ALIGN(sizeof(struct mbox_hdr), MBOX_MSG_ALIGN);
    1988                 :          0 :         rsp = (struct npa_cn20k_aq_enq_rsp *)((uintptr_t)mdev->mbase + off);
    1989         [ #  # ]:          0 :         if (rsp->hdr.rc != 0)
    1990                 :          0 :                 goto exit;
    1991                 :            : 
    1992                 :            :         rc = 0;
    1993                 :          0 : exit:
    1994                 :            :         mbox_put(mbox);
    1995                 :          0 :         return rc;
    1996                 :            : }

Generated by: LCOV version 1.14