LCOV - code coverage report
Current view: top level - drivers/dma/skeleton - skeleton_dmadev.c (source / functions) Hit Total Coverage
Test: Code coverage Lines: 39 295 13.2 %
Date: 2024-04-01 19:00:53 Functions: 8 31 25.8 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 12 199 6.0 %

           Branch data     Line data    Source code
       1                 :            : /* SPDX-License-Identifier: BSD-3-Clause
       2                 :            :  * Copyright(c) 2021-2024 HiSilicon Limited
       3                 :            :  */
       4                 :            : 
       5                 :            : #include <inttypes.h>
       6                 :            : #include <stdlib.h>
       7                 :            : 
       8                 :            : #include <pthread.h>
       9                 :            : 
      10                 :            : #include <bus_vdev_driver.h>
      11                 :            : #include <rte_cycles.h>
      12                 :            : #include <rte_eal.h>
      13                 :            : #include <rte_kvargs.h>
      14                 :            : #include <rte_lcore.h>
      15                 :            : #include <rte_log.h>
      16                 :            : #include <rte_malloc.h>
      17                 :            : #include <rte_memcpy.h>
      18                 :            : 
      19                 :            : #include <rte_dmadev_pmd.h>
      20                 :            : 
      21                 :            : #include "skeleton_dmadev.h"
      22                 :            : 
      23         [ -  + ]:        238 : RTE_LOG_REGISTER_DEFAULT(skeldma_logtype, INFO);
      24                 :            : #define SKELDMA_LOG(level, fmt, args...) \
      25                 :            :         rte_log(RTE_LOG_ ## level, skeldma_logtype, "%s(): " fmt "\n", \
      26                 :            :                 __func__, ##args)
      27                 :            : 
      28                 :            : static int
      29                 :          0 : skeldma_info_get(const struct rte_dma_dev *dev, struct rte_dma_info *dev_info,
      30                 :            :                  uint32_t info_sz)
      31                 :            : {
      32                 :            : #define SKELDMA_MAX_DESC        8192
      33                 :            : #define SKELDMA_MIN_DESC        32
      34                 :            : 
      35                 :            :         RTE_SET_USED(dev);
      36                 :            :         RTE_SET_USED(info_sz);
      37                 :            : 
      38                 :          0 :         dev_info->dev_capa = RTE_DMA_CAPA_MEM_TO_MEM |
      39                 :            :                              RTE_DMA_CAPA_SVA |
      40                 :            :                              RTE_DMA_CAPA_OPS_COPY |
      41                 :            :                              RTE_DMA_CAPA_OPS_COPY_SG |
      42                 :            :                              RTE_DMA_CAPA_OPS_FILL;
      43                 :          0 :         dev_info->max_vchans = 1;
      44                 :          0 :         dev_info->max_desc = SKELDMA_MAX_DESC;
      45                 :          0 :         dev_info->min_desc = SKELDMA_MIN_DESC;
      46                 :          0 :         dev_info->max_sges = SKELDMA_MAX_SGES;
      47                 :            : 
      48                 :          0 :         return 0;
      49                 :            : }
      50                 :            : 
      51                 :            : static int
      52                 :          0 : skeldma_configure(struct rte_dma_dev *dev, const struct rte_dma_conf *conf,
      53                 :            :                   uint32_t conf_sz)
      54                 :            : {
      55                 :            :         RTE_SET_USED(dev);
      56                 :            :         RTE_SET_USED(conf);
      57                 :            :         RTE_SET_USED(conf_sz);
      58                 :          0 :         return 0;
      59                 :            : }
      60                 :            : 
      61                 :            : static inline void
      62                 :          0 : do_copy_sg_one(struct rte_dma_sge *src, struct rte_dma_sge *dst, uint16_t nb_dst, uint64_t offset)
      63                 :            : {
      64                 :            :         uint32_t src_off = 0, dst_off = 0;
      65                 :            :         uint32_t copy_len = 0;
      66                 :            :         uint64_t tmp = 0;
      67                 :            :         uint16_t i;
      68                 :            : 
      69                 :            :         /* Locate the segment from which the copy is started. */
      70         [ #  # ]:          0 :         for (i = 0; i < nb_dst; i++) {
      71                 :          0 :                 tmp += dst[i].length;
      72         [ #  # ]:          0 :                 if (offset < tmp) {
      73                 :          0 :                         copy_len = tmp - offset;
      74                 :          0 :                         dst_off = dst[i].length - copy_len;
      75                 :          0 :                         break;
      76                 :            :                 }
      77                 :            :         }
      78                 :            : 
      79         [ #  # ]:          0 :         for (/* Use the above index */; i < nb_dst; i++, copy_len = dst[i].length) {
      80                 :          0 :                 copy_len = RTE_MIN(copy_len, src->length - src_off);
      81                 :          0 :                 rte_memcpy((uint8_t *)(uintptr_t)dst[i].addr + dst_off,
      82         [ #  # ]:          0 :                            (uint8_t *)(uintptr_t)src->addr + src_off,
      83                 :            :                            copy_len);
      84                 :          0 :                 src_off += copy_len;
      85         [ #  # ]:          0 :                 if (src_off >= src->length)
      86                 :            :                         break;
      87                 :            :                 dst_off = 0;
      88                 :            :         }
      89                 :          0 : }
      90                 :            : 
      91                 :            : static inline void
      92                 :          0 : do_copy_sg(struct skeldma_desc *desc)
      93                 :            : {
      94                 :            :         uint64_t offset = 0;
      95                 :            :         uint16_t i;
      96                 :            : 
      97         [ #  # ]:          0 :         for (i = 0; i < desc->copy_sg.nb_src; i++) {
      98                 :          0 :                 do_copy_sg_one(&desc->copy_sg.src[i], desc->copy_sg.dst,
      99                 :          0 :                                desc->copy_sg.nb_dst, offset);
     100                 :          0 :                 offset += desc->copy_sg.src[i].length;
     101                 :            :         }
     102                 :          0 : }
     103                 :            : 
     104                 :            : static inline void
     105                 :            : do_fill(struct skeldma_desc *desc)
     106                 :            : {
     107                 :          0 :         uint8_t *fills = (uint8_t *)&desc->fill.pattern;
     108                 :          0 :         uint8_t *dst = (uint8_t *)desc->fill.dst;
     109                 :            :         uint32_t i;
     110                 :            : 
     111         [ #  # ]:          0 :         for (i = 0; i < desc->fill.len; i++)
     112                 :          0 :                 dst[i] = fills[i % 8];
     113                 :            : }
     114                 :            : 
     115                 :            : static uint32_t
     116                 :          0 : cpuwork_thread(void *param)
     117                 :            : {
     118                 :            : #define SLEEP_THRESHOLD         10000
     119                 :            : #define SLEEP_US_VAL            10
     120                 :            : 
     121                 :            :         struct rte_dma_dev *dev = param;
     122                 :          0 :         struct skeldma_hw *hw = dev->data->dev_private;
     123                 :          0 :         struct skeldma_desc *desc = NULL;
     124                 :            :         int ret;
     125                 :            : 
     126         [ #  # ]:          0 :         while (!hw->exit_flag) {
     127   [ #  #  #  #  :          0 :                 ret = rte_ring_dequeue(hw->desc_running, (void **)&desc);
                      # ]
     128                 :          0 :                 if (ret) {
     129                 :          0 :                         hw->zero_req_count++;
     130         [ #  # ]:          0 :                         if (hw->zero_req_count == 0)
     131                 :          0 :                                 hw->zero_req_count = SLEEP_THRESHOLD;
     132         [ #  # ]:          0 :                         if (hw->zero_req_count >= SLEEP_THRESHOLD)
     133                 :          0 :                                 rte_delay_us_sleep(SLEEP_US_VAL);
     134                 :          0 :                         continue;
     135                 :            :                 }
     136                 :          0 :                 hw->zero_req_count = 0;
     137                 :            : 
     138         [ #  # ]:          0 :                 if (desc->op == SKELDMA_OP_COPY)
     139         [ #  # ]:          0 :                         rte_memcpy(desc->copy.dst, desc->copy.src, desc->copy.len);
     140         [ #  # ]:          0 :                 else if (desc->op == SKELDMA_OP_COPY_SG)
     141                 :          0 :                         do_copy_sg(desc);
     142         [ #  # ]:          0 :                 else if (desc->op == SKELDMA_OP_FILL)
     143                 :            :                         do_fill(desc);
     144                 :            : 
     145                 :          0 :                 __atomic_fetch_add(&hw->completed_count, 1, __ATOMIC_RELEASE);
     146   [ #  #  #  #  :          0 :                 (void)rte_ring_enqueue(hw->desc_completed, (void *)desc);
                      # ]
     147                 :            :         }
     148                 :            : 
     149                 :          0 :         return 0;
     150                 :            : }
     151                 :            : 
     152                 :            : static void
     153                 :          0 : fflush_ring(struct skeldma_hw *hw, struct rte_ring *ring)
     154                 :            : {
     155                 :          0 :         struct skeldma_desc *desc = NULL;
     156         [ #  # ]:          0 :         while (rte_ring_count(ring) > 0) {
     157                 :            :                 (void)rte_ring_dequeue(ring, (void **)&desc);
     158   [ #  #  #  #  :          0 :                 (void)rte_ring_enqueue(hw->desc_empty, (void *)desc);
                      # ]
     159                 :            :         }
     160                 :          0 : }
     161                 :            : 
     162                 :            : static int
     163                 :          0 : skeldma_start(struct rte_dma_dev *dev)
     164                 :            : {
     165                 :          0 :         struct skeldma_hw *hw = dev->data->dev_private;
     166                 :            :         char name[RTE_THREAD_INTERNAL_NAME_SIZE];
     167                 :            :         rte_cpuset_t cpuset;
     168                 :            :         int ret;
     169                 :            : 
     170         [ #  # ]:          0 :         if (hw->desc_mem == NULL) {
     171                 :          0 :                 SKELDMA_LOG(ERR, "Vchan was not setup, start fail!");
     172                 :          0 :                 return -EINVAL;
     173                 :            :         }
     174                 :            : 
     175                 :            :         /* Reset the dmadev to a known state, include:
     176                 :            :          * 1) fflush pending/running/completed ring to empty ring.
     177                 :            :          * 2) init ring idx to zero.
     178                 :            :          * 3) init running statistics.
     179                 :            :          * 4) mark cpuwork task exit_flag to false.
     180                 :            :          */
     181                 :          0 :         fflush_ring(hw, hw->desc_pending);
     182                 :          0 :         fflush_ring(hw, hw->desc_running);
     183                 :          0 :         fflush_ring(hw, hw->desc_completed);
     184                 :          0 :         hw->ridx = 0;
     185                 :          0 :         hw->last_ridx = hw->ridx - 1;
     186                 :          0 :         hw->submitted_count = 0;
     187                 :          0 :         hw->zero_req_count = 0;
     188                 :          0 :         hw->completed_count = 0;
     189                 :          0 :         hw->exit_flag = false;
     190                 :            : 
     191                 :            :         rte_mb();
     192                 :            : 
     193                 :          0 :         snprintf(name, sizeof(name), "dma-skel%d", dev->data->dev_id);
     194                 :          0 :         ret = rte_thread_create_internal_control(&hw->thread, name,
     195                 :            :                         cpuwork_thread, dev);
     196         [ #  # ]:          0 :         if (ret) {
     197                 :          0 :                 SKELDMA_LOG(ERR, "Start cpuwork thread fail!");
     198                 :          0 :                 return -EINVAL;
     199                 :            :         }
     200                 :            : 
     201         [ #  # ]:          0 :         if (hw->lcore_id != -1) {
     202                 :          0 :                 cpuset = rte_lcore_cpuset(hw->lcore_id);
     203                 :          0 :                 ret = rte_thread_set_affinity_by_id(hw->thread, &cpuset);
     204         [ #  # ]:          0 :                 if (ret)
     205                 :          0 :                         SKELDMA_LOG(WARNING,
     206                 :            :                                 "Set thread affinity lcore = %d fail!",
     207                 :            :                                 hw->lcore_id);
     208                 :            :         }
     209                 :            : 
     210                 :            :         return 0;
     211                 :            : }
     212                 :            : 
     213                 :            : static int
     214                 :          0 : skeldma_stop(struct rte_dma_dev *dev)
     215                 :            : {
     216                 :          0 :         struct skeldma_hw *hw = dev->data->dev_private;
     217                 :            : 
     218                 :          0 :         hw->exit_flag = true;
     219                 :            :         rte_delay_ms(1);
     220                 :            : 
     221                 :          0 :         (void)pthread_cancel((pthread_t)hw->thread.opaque_id);
     222                 :          0 :         rte_thread_join(hw->thread, NULL);
     223                 :            : 
     224                 :          0 :         return 0;
     225                 :            : }
     226                 :            : 
     227                 :            : static int
     228                 :          0 : vchan_setup(struct skeldma_hw *hw, int16_t dev_id, uint16_t nb_desc)
     229                 :            : {
     230                 :            :         char name[RTE_RING_NAMESIZE];
     231                 :            :         struct skeldma_desc *desc;
     232                 :            :         struct rte_ring *empty;
     233                 :            :         struct rte_ring *pending;
     234                 :            :         struct rte_ring *running;
     235                 :            :         struct rte_ring *completed;
     236                 :            :         uint16_t i;
     237                 :            : 
     238                 :          0 :         desc = rte_zmalloc_socket(NULL, nb_desc * sizeof(struct skeldma_desc),
     239                 :            :                                   RTE_CACHE_LINE_SIZE, hw->socket_id);
     240         [ #  # ]:          0 :         if (desc == NULL) {
     241                 :          0 :                 SKELDMA_LOG(ERR, "Malloc dma skeleton desc fail!");
     242                 :          0 :                 return -ENOMEM;
     243                 :            :         }
     244                 :            : 
     245                 :          0 :         snprintf(name, RTE_RING_NAMESIZE, "dma_skel_desc_empty_%d", dev_id);
     246                 :          0 :         empty = rte_ring_create(name, nb_desc, hw->socket_id,
     247                 :            :                                 RING_F_SP_ENQ | RING_F_SC_DEQ);
     248                 :            :         snprintf(name, RTE_RING_NAMESIZE, "dma_skel_desc_pend_%d", dev_id);
     249                 :          0 :         pending = rte_ring_create(name, nb_desc, hw->socket_id,
     250                 :            :                                   RING_F_SP_ENQ | RING_F_SC_DEQ);
     251                 :            :         snprintf(name, RTE_RING_NAMESIZE, "dma_skel_desc_run_%d", dev_id);
     252                 :          0 :         running = rte_ring_create(name, nb_desc, hw->socket_id,
     253                 :            :                                   RING_F_SP_ENQ | RING_F_SC_DEQ);
     254                 :            :         snprintf(name, RTE_RING_NAMESIZE, "dma_skel_desc_comp_%d", dev_id);
     255                 :          0 :         completed = rte_ring_create(name, nb_desc, hw->socket_id,
     256                 :            :                                     RING_F_SP_ENQ | RING_F_SC_DEQ);
     257         [ #  # ]:          0 :         if (empty == NULL || pending == NULL || running == NULL ||
     258         [ #  # ]:          0 :             completed == NULL) {
     259                 :          0 :                 SKELDMA_LOG(ERR, "Create dma skeleton desc ring fail!");
     260                 :          0 :                 rte_ring_free(empty);
     261                 :          0 :                 rte_ring_free(pending);
     262                 :          0 :                 rte_ring_free(running);
     263                 :          0 :                 rte_ring_free(completed);
     264                 :          0 :                 rte_free(desc);
     265                 :          0 :                 return -ENOMEM;
     266                 :            :         }
     267                 :            : 
     268                 :            :         /* The real usable ring size is *count-1* instead of *count* to
     269                 :            :          * differentiate a free ring from an empty ring.
     270                 :            :          * @see rte_ring_create
     271                 :            :          */
     272         [ #  # ]:          0 :         for (i = 0; i < nb_desc - 1; i++)
     273   [ #  #  #  #  :          0 :                 (void)rte_ring_enqueue(empty, (void *)(desc + i));
                      # ]
     274                 :            : 
     275                 :          0 :         hw->desc_mem = desc;
     276                 :          0 :         hw->desc_empty = empty;
     277                 :          0 :         hw->desc_pending = pending;
     278                 :          0 :         hw->desc_running = running;
     279                 :          0 :         hw->desc_completed = completed;
     280                 :            : 
     281                 :          0 :         return 0;
     282                 :            : }
     283                 :            : 
     284                 :            : static void
     285                 :          1 : vchan_release(struct skeldma_hw *hw)
     286                 :            : {
     287         [ -  + ]:          1 :         if (hw->desc_mem == NULL)
     288                 :            :                 return;
     289                 :            : 
     290                 :          0 :         rte_free(hw->desc_mem);
     291                 :          0 :         hw->desc_mem = NULL;
     292                 :          0 :         rte_ring_free(hw->desc_empty);
     293                 :          0 :         hw->desc_empty = NULL;
     294                 :          0 :         rte_ring_free(hw->desc_pending);
     295                 :          0 :         hw->desc_pending = NULL;
     296                 :          0 :         rte_ring_free(hw->desc_running);
     297                 :          0 :         hw->desc_running = NULL;
     298                 :          0 :         rte_ring_free(hw->desc_completed);
     299                 :          0 :         hw->desc_completed = NULL;
     300                 :            : }
     301                 :            : 
     302                 :            : static int
     303                 :          1 : skeldma_close(struct rte_dma_dev *dev)
     304                 :            : {
     305                 :            :         /* The device already stopped */
     306                 :          1 :         vchan_release(dev->data->dev_private);
     307                 :          1 :         return 0;
     308                 :            : }
     309                 :            : 
     310                 :            : static int
     311                 :          0 : skeldma_vchan_setup(struct rte_dma_dev *dev, uint16_t vchan,
     312                 :            :                     const struct rte_dma_vchan_conf *conf,
     313                 :            :                     uint32_t conf_sz)
     314                 :            : {
     315                 :          0 :         struct skeldma_hw *hw = dev->data->dev_private;
     316                 :            : 
     317                 :            :         RTE_SET_USED(vchan);
     318                 :            :         RTE_SET_USED(conf_sz);
     319                 :            : 
     320         [ #  # ]:          0 :         if (!rte_is_power_of_2(conf->nb_desc)) {
     321                 :          0 :                 SKELDMA_LOG(ERR, "Number of desc must be power of 2!");
     322                 :          0 :                 return -EINVAL;
     323                 :            :         }
     324                 :            : 
     325                 :          0 :         vchan_release(hw);
     326                 :          0 :         return vchan_setup(hw, dev->data->dev_id, conf->nb_desc);
     327                 :            : }
     328                 :            : 
     329                 :            : static int
     330                 :          0 : skeldma_vchan_status(const struct rte_dma_dev *dev,
     331                 :            :                 uint16_t vchan, enum rte_dma_vchan_status *status)
     332                 :            : {
     333                 :          0 :         struct skeldma_hw *hw = dev->data->dev_private;
     334                 :            : 
     335                 :            :         RTE_SET_USED(vchan);
     336                 :            : 
     337                 :          0 :         *status = RTE_DMA_VCHAN_IDLE;
     338         [ #  # ]:          0 :         if (hw->submitted_count != __atomic_load_n(&hw->completed_count, __ATOMIC_ACQUIRE)
     339         [ #  # ]:          0 :                         || hw->zero_req_count == 0)
     340                 :          0 :                 *status = RTE_DMA_VCHAN_ACTIVE;
     341                 :          0 :         return 0;
     342                 :            : }
     343                 :            : 
     344                 :            : static int
     345                 :          0 : skeldma_stats_get(const struct rte_dma_dev *dev, uint16_t vchan,
     346                 :            :                   struct rte_dma_stats *stats, uint32_t stats_sz)
     347                 :            : {
     348                 :          0 :         struct skeldma_hw *hw = dev->data->dev_private;
     349                 :            : 
     350                 :            :         RTE_SET_USED(vchan);
     351                 :            :         RTE_SET_USED(stats_sz);
     352                 :            : 
     353                 :          0 :         stats->submitted = hw->submitted_count;
     354                 :          0 :         stats->completed = hw->completed_count;
     355                 :          0 :         stats->errors = 0;
     356                 :            : 
     357                 :          0 :         return 0;
     358                 :            : }
     359                 :            : 
     360                 :            : static int
     361                 :          0 : skeldma_stats_reset(struct rte_dma_dev *dev, uint16_t vchan)
     362                 :            : {
     363                 :          0 :         struct skeldma_hw *hw = dev->data->dev_private;
     364                 :            : 
     365                 :            :         RTE_SET_USED(vchan);
     366                 :            : 
     367                 :          0 :         hw->submitted_count = 0;
     368                 :          0 :         hw->completed_count = 0;
     369                 :            : 
     370                 :          0 :         return 0;
     371                 :            : }
     372                 :            : 
     373                 :            : static int
     374                 :          0 : skeldma_dump(const struct rte_dma_dev *dev, FILE *f)
     375                 :            : {
     376                 :            : #define GET_RING_COUNT(ring)    ((ring) ? (rte_ring_count(ring)) : 0)
     377                 :            : 
     378                 :          0 :         struct skeldma_hw *hw = dev->data->dev_private;
     379                 :            : 
     380                 :          0 :         (void)fprintf(f,
     381                 :            :                 "    lcore_id: %d\n"
     382                 :            :                 "    socket_id: %d\n"
     383                 :            :                 "    desc_empty_ring_count: %u\n"
     384                 :            :                 "    desc_pending_ring_count: %u\n"
     385                 :            :                 "    desc_running_ring_count: %u\n"
     386                 :            :                 "    desc_completed_ring_count: %u\n",
     387                 :            :                 hw->lcore_id, hw->socket_id,
     388         [ #  # ]:          0 :                 GET_RING_COUNT(hw->desc_empty),
     389         [ #  # ]:          0 :                 GET_RING_COUNT(hw->desc_pending),
     390         [ #  # ]:          0 :                 GET_RING_COUNT(hw->desc_running),
     391         [ #  # ]:          0 :                 GET_RING_COUNT(hw->desc_completed));
     392                 :          0 :         (void)fprintf(f,
     393                 :            :                 "    next_ring_idx: %u\n"
     394                 :            :                 "    last_ring_idx: %u\n"
     395                 :            :                 "    submitted_count: %" PRIu64 "\n"
     396                 :            :                 "    completed_count: %" PRIu64 "\n",
     397                 :          0 :                 hw->ridx, hw->last_ridx,
     398                 :            :                 hw->submitted_count, hw->completed_count);
     399                 :            : 
     400                 :          0 :         return 0;
     401                 :            : }
     402                 :            : 
     403                 :            : static inline void
     404                 :          0 : submit(struct skeldma_hw *hw, struct skeldma_desc *desc)
     405                 :            : {
     406                 :          0 :         uint16_t count = rte_ring_count(hw->desc_pending);
     407                 :          0 :         struct skeldma_desc *pend_desc = NULL;
     408                 :            : 
     409         [ #  # ]:          0 :         while (count > 0) {
     410   [ #  #  #  #  :          0 :                 (void)rte_ring_dequeue(hw->desc_pending, (void **)&pend_desc);
                      # ]
     411   [ #  #  #  #  :          0 :                 (void)rte_ring_enqueue(hw->desc_running, (void *)pend_desc);
                      # ]
     412                 :          0 :                 count--;
     413                 :            :         }
     414                 :            : 
     415         [ #  # ]:          0 :         if (desc)
     416   [ #  #  #  #  :          0 :                 (void)rte_ring_enqueue(hw->desc_running, (void *)desc);
                      # ]
     417                 :          0 : }
     418                 :            : 
     419                 :            : static int
     420                 :          0 : skeldma_copy(void *dev_private, uint16_t vchan,
     421                 :            :              rte_iova_t src, rte_iova_t dst,
     422                 :            :              uint32_t length, uint64_t flags)
     423                 :            : {
     424                 :            :         struct skeldma_hw *hw = dev_private;
     425                 :            :         struct skeldma_desc *desc;
     426                 :            :         int ret;
     427                 :            : 
     428                 :            :         RTE_SET_USED(vchan);
     429                 :            :         RTE_SET_USED(flags);
     430                 :            : 
     431   [ #  #  #  #  :          0 :         ret = rte_ring_dequeue(hw->desc_empty, (void **)&desc);
                      # ]
     432                 :            :         if (ret)
     433                 :          0 :                 return -ENOSPC;
     434                 :          0 :         desc->op = SKELDMA_OP_COPY;
     435                 :          0 :         desc->ridx = hw->ridx;
     436                 :          0 :         desc->copy.src = (void *)(uintptr_t)src;
     437                 :          0 :         desc->copy.dst = (void *)(uintptr_t)dst;
     438                 :          0 :         desc->copy.len = length;
     439         [ #  # ]:          0 :         if (flags & RTE_DMA_OP_FLAG_SUBMIT)
     440                 :          0 :                 submit(hw, desc);
     441                 :            :         else
     442   [ #  #  #  #  :          0 :                 (void)rte_ring_enqueue(hw->desc_pending, (void *)desc);
                      # ]
     443                 :          0 :         hw->submitted_count++;
     444                 :            : 
     445                 :          0 :         return hw->ridx++;
     446                 :            : }
     447                 :            : 
     448                 :            : static int
     449                 :          0 : skeldma_copy_sg(void *dev_private, uint16_t vchan,
     450                 :            :                 const struct rte_dma_sge *src,
     451                 :            :                 const struct rte_dma_sge *dst,
     452                 :            :                 uint16_t nb_src, uint16_t nb_dst,
     453                 :            :                 uint64_t flags)
     454                 :            : {
     455                 :            :         struct skeldma_hw *hw = dev_private;
     456                 :            :         struct skeldma_desc *desc;
     457                 :            :         int ret;
     458                 :            : 
     459                 :            :         RTE_SET_USED(vchan);
     460                 :            : 
     461   [ #  #  #  #  :          0 :         ret = rte_ring_dequeue(hw->desc_empty, (void **)&desc);
                      # ]
     462                 :            :         if (ret)
     463                 :          0 :                 return -ENOSPC;
     464                 :          0 :         desc->op = SKELDMA_OP_COPY_SG;
     465                 :          0 :         desc->ridx = hw->ridx;
     466         [ #  # ]:          0 :         memcpy(desc->copy_sg.src, src, sizeof(*src) * nb_src);
     467                 :          0 :         memcpy(desc->copy_sg.dst, dst, sizeof(*dst) * nb_dst);
     468                 :          0 :         desc->copy_sg.nb_src = nb_src;
     469                 :          0 :         desc->copy_sg.nb_dst = nb_dst;
     470         [ #  # ]:          0 :         if (flags & RTE_DMA_OP_FLAG_SUBMIT)
     471                 :          0 :                 submit(hw, desc);
     472                 :            :         else
     473   [ #  #  #  #  :          0 :                 (void)rte_ring_enqueue(hw->desc_pending, (void *)desc);
                      # ]
     474                 :          0 :         hw->submitted_count++;
     475                 :            : 
     476                 :          0 :         return hw->ridx++;
     477                 :            : }
     478                 :            : 
     479                 :            : static int
     480                 :          0 : skeldma_fill(void *dev_private, uint16_t vchan,
     481                 :            :              uint64_t pattern, rte_iova_t dst,
     482                 :            :              uint32_t length, uint64_t flags)
     483                 :            : {
     484                 :            :         struct skeldma_hw *hw = dev_private;
     485                 :            :         struct skeldma_desc *desc;
     486                 :            :         int ret;
     487                 :            : 
     488                 :            :         RTE_SET_USED(vchan);
     489                 :            : 
     490   [ #  #  #  #  :          0 :         ret = rte_ring_dequeue(hw->desc_empty, (void **)&desc);
                      # ]
     491                 :            :         if (ret)
     492                 :          0 :                 return -ENOSPC;
     493                 :          0 :         desc->op = SKELDMA_OP_FILL;
     494                 :          0 :         desc->ridx = hw->ridx;
     495                 :          0 :         desc->fill.dst = (void *)(uintptr_t)dst;
     496                 :          0 :         desc->fill.len = length;
     497                 :          0 :         desc->fill.pattern = pattern;
     498         [ #  # ]:          0 :         if (flags & RTE_DMA_OP_FLAG_SUBMIT)
     499                 :          0 :                 submit(hw, desc);
     500                 :            :         else
     501   [ #  #  #  #  :          0 :                 (void)rte_ring_enqueue(hw->desc_pending, (void *)desc);
                      # ]
     502                 :          0 :         hw->submitted_count++;
     503                 :            : 
     504                 :          0 :         return hw->ridx++;
     505                 :            : }
     506                 :            : 
     507                 :            : static int
     508                 :          0 : skeldma_submit(void *dev_private, uint16_t vchan)
     509                 :            : {
     510                 :            :         struct skeldma_hw *hw = dev_private;
     511                 :            :         RTE_SET_USED(vchan);
     512                 :          0 :         submit(hw, NULL);
     513                 :          0 :         return 0;
     514                 :            : }
     515                 :            : 
     516                 :            : static uint16_t
     517                 :          0 : skeldma_completed(void *dev_private,
     518                 :            :                   uint16_t vchan, const uint16_t nb_cpls,
     519                 :            :                   uint16_t *last_idx, bool *has_error)
     520                 :            : {
     521                 :            :         struct skeldma_hw *hw = dev_private;
     522                 :          0 :         struct skeldma_desc *desc = NULL;
     523                 :            :         uint16_t index = 0;
     524                 :            :         uint16_t count;
     525                 :            : 
     526                 :            :         RTE_SET_USED(vchan);
     527                 :            :         RTE_SET_USED(has_error);
     528                 :            : 
     529                 :          0 :         count = RTE_MIN(nb_cpls, rte_ring_count(hw->desc_completed));
     530         [ #  # ]:          0 :         while (index < count) {
     531   [ #  #  #  #  :          0 :                 (void)rte_ring_dequeue(hw->desc_completed, (void **)&desc);
                      # ]
     532         [ #  # ]:          0 :                 if (index == count - 1) {
     533                 :          0 :                         hw->last_ridx = desc->ridx;
     534                 :          0 :                         *last_idx = desc->ridx;
     535                 :            :                 }
     536                 :          0 :                 index++;
     537   [ #  #  #  #  :          0 :                 (void)rte_ring_enqueue(hw->desc_empty, (void *)desc);
                      # ]
     538                 :            :         }
     539         [ #  # ]:          0 :         if (unlikely(count == 0))
     540                 :          0 :                 *last_idx = hw->last_ridx;
     541                 :            : 
     542                 :          0 :         return count;
     543                 :            : }
     544                 :            : 
     545                 :            : static uint16_t
     546                 :          0 : skeldma_completed_status(void *dev_private,
     547                 :            :                          uint16_t vchan, const uint16_t nb_cpls,
     548                 :            :                          uint16_t *last_idx, enum rte_dma_status_code *status)
     549                 :            : {
     550                 :            :         struct skeldma_hw *hw = dev_private;
     551                 :          0 :         struct skeldma_desc *desc = NULL;
     552                 :            :         uint16_t index = 0;
     553                 :            :         uint16_t count;
     554                 :            : 
     555                 :            :         RTE_SET_USED(vchan);
     556                 :            : 
     557                 :          0 :         count = RTE_MIN(nb_cpls, rte_ring_count(hw->desc_completed));
     558         [ #  # ]:          0 :         while (index < count) {
     559   [ #  #  #  #  :          0 :                 (void)rte_ring_dequeue(hw->desc_completed, (void **)&desc);
                      # ]
     560         [ #  # ]:          0 :                 if (index == count - 1) {
     561                 :          0 :                         hw->last_ridx = desc->ridx;
     562                 :          0 :                         *last_idx = desc->ridx;
     563                 :            :                 }
     564                 :          0 :                 status[index++] = RTE_DMA_STATUS_SUCCESSFUL;
     565   [ #  #  #  #  :          0 :                 (void)rte_ring_enqueue(hw->desc_empty, (void *)desc);
                      # ]
     566                 :            :         }
     567         [ #  # ]:          0 :         if (unlikely(count == 0))
     568                 :          0 :                 *last_idx = hw->last_ridx;
     569                 :            : 
     570                 :          0 :         return count;
     571                 :            : }
     572                 :            : 
     573                 :            : static uint16_t
     574                 :          0 : skeldma_burst_capacity(const void *dev_private, uint16_t vchan)
     575                 :            : {
     576                 :            :         const struct skeldma_hw *hw = dev_private;
     577                 :            : 
     578                 :            :         RTE_SET_USED(vchan);
     579                 :          0 :         return rte_ring_count(hw->desc_empty);
     580                 :            : }
     581                 :            : 
     582                 :            : static const struct rte_dma_dev_ops skeldma_ops = {
     583                 :            :         .dev_info_get     = skeldma_info_get,
     584                 :            :         .dev_configure    = skeldma_configure,
     585                 :            :         .dev_start        = skeldma_start,
     586                 :            :         .dev_stop         = skeldma_stop,
     587                 :            :         .dev_close        = skeldma_close,
     588                 :            : 
     589                 :            :         .vchan_setup      = skeldma_vchan_setup,
     590                 :            :         .vchan_status     = skeldma_vchan_status,
     591                 :            : 
     592                 :            :         .stats_get        = skeldma_stats_get,
     593                 :            :         .stats_reset      = skeldma_stats_reset,
     594                 :            : 
     595                 :            :         .dev_dump         = skeldma_dump,
     596                 :            : };
     597                 :            : 
     598                 :            : static int
     599                 :          1 : skeldma_create(const char *name, struct rte_vdev_device *vdev, int lcore_id)
     600                 :            : {
     601                 :            :         struct rte_dma_dev *dev;
     602                 :            :         struct skeldma_hw *hw;
     603                 :            :         int socket_id;
     604                 :            : 
     605         [ +  - ]:          1 :         socket_id = (lcore_id < 0) ? rte_socket_id() :
     606                 :          0 :                                      rte_lcore_to_socket_id(lcore_id);
     607                 :          1 :         dev = rte_dma_pmd_allocate(name, socket_id, sizeof(struct skeldma_hw));
     608         [ -  + ]:          1 :         if (dev == NULL) {
     609                 :          0 :                 SKELDMA_LOG(ERR, "Unable to allocate dmadev: %s", name);
     610                 :          0 :                 return -EINVAL;
     611                 :            :         }
     612                 :            : 
     613                 :          1 :         dev->device = &vdev->device;
     614                 :          1 :         dev->dev_ops = &skeldma_ops;
     615                 :          1 :         dev->fp_obj->dev_private = dev->data->dev_private;
     616                 :          1 :         dev->fp_obj->copy = skeldma_copy;
     617                 :          1 :         dev->fp_obj->copy_sg = skeldma_copy_sg;
     618                 :          1 :         dev->fp_obj->fill = skeldma_fill;
     619                 :          1 :         dev->fp_obj->submit = skeldma_submit;
     620                 :          1 :         dev->fp_obj->completed = skeldma_completed;
     621                 :          1 :         dev->fp_obj->completed_status = skeldma_completed_status;
     622                 :          1 :         dev->fp_obj->burst_capacity = skeldma_burst_capacity;
     623                 :            : 
     624                 :            :         hw = dev->data->dev_private;
     625                 :          1 :         hw->lcore_id = lcore_id;
     626                 :          1 :         hw->socket_id = socket_id;
     627                 :            : 
     628                 :          1 :         dev->state = RTE_DMA_DEV_READY;
     629                 :            : 
     630                 :          1 :         return dev->data->dev_id;
     631                 :            : }
     632                 :            : 
     633                 :            : static int
     634                 :            : skeldma_destroy(const char *name)
     635                 :            : {
     636                 :          1 :         return rte_dma_pmd_release(name);
     637                 :            : }
     638                 :            : 
     639                 :            : static int
     640                 :          0 : skeldma_parse_lcore(const char *key __rte_unused,
     641                 :            :                     const char *value,
     642                 :            :                     void *opaque)
     643                 :            : {
     644                 :            :         int lcore_id;
     645                 :            : 
     646         [ #  # ]:          0 :         if (value == NULL || opaque == NULL)
     647                 :            :                 return -EINVAL;
     648                 :            : 
     649                 :            :         lcore_id = atoi(value);
     650         [ #  # ]:          0 :         if (lcore_id >= 0 && lcore_id < RTE_MAX_LCORE)
     651                 :          0 :                 *(int *)opaque = lcore_id;
     652                 :            : 
     653                 :            :         return 0;
     654                 :            : }
     655                 :            : 
     656                 :            : static void
     657         [ +  - ]:          1 : skeldma_parse_vdev_args(struct rte_vdev_device *vdev, int *lcore_id)
     658                 :            : {
     659                 :            :         static const char *const args[] = {
     660                 :            :                 SKELDMA_ARG_LCORE,
     661                 :            :                 NULL
     662                 :            :         };
     663                 :            : 
     664                 :            :         struct rte_kvargs *kvlist;
     665                 :            :         const char *params;
     666                 :            : 
     667                 :            :         params = rte_vdev_device_args(vdev);
     668   [ +  -  -  + ]:          1 :         if (params == NULL || params[0] == '\0')
     669                 :            :                 return;
     670                 :            : 
     671                 :          0 :         kvlist = rte_kvargs_parse(params, args);
     672         [ #  # ]:          0 :         if (!kvlist)
     673                 :            :                 return;
     674                 :            : 
     675                 :          0 :         (void)rte_kvargs_process(kvlist, SKELDMA_ARG_LCORE,
     676                 :            :                                  skeldma_parse_lcore, lcore_id);
     677                 :          0 :         SKELDMA_LOG(INFO, "Parse lcore_id = %d", *lcore_id);
     678                 :            : 
     679                 :          0 :         rte_kvargs_free(kvlist);
     680                 :            : }
     681                 :            : 
     682                 :            : static int
     683                 :          1 : skeldma_probe(struct rte_vdev_device *vdev)
     684                 :            : {
     685                 :            :         const char *name;
     686         [ +  - ]:          1 :         int lcore_id = -1;
     687                 :            :         int ret;
     688                 :            : 
     689                 :            :         name = rte_vdev_device_name(vdev);
     690                 :            :         if (name == NULL)
     691                 :            :                 return -EINVAL;
     692                 :            : 
     693         [ -  + ]:          1 :         if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
     694                 :          0 :                 SKELDMA_LOG(ERR, "Multiple process not supported for %s", name);
     695                 :          0 :                 return -EINVAL;
     696                 :            :         }
     697                 :            : 
     698                 :          1 :         skeldma_parse_vdev_args(vdev, &lcore_id);
     699                 :            : 
     700                 :          1 :         ret = skeldma_create(name, vdev, lcore_id);
     701         [ +  - ]:          1 :         if (ret >= 0)
     702                 :          1 :                 SKELDMA_LOG(INFO, "Create %s dmadev with lcore-id %d",
     703                 :            :                         name, lcore_id);
     704                 :            : 
     705                 :          1 :         return ret < 0 ? ret : 0;
     706                 :            : }
     707                 :            : 
     708                 :            : static int
     709         [ +  - ]:          1 : skeldma_remove(struct rte_vdev_device *vdev)
     710                 :            : {
     711                 :            :         const char *name;
     712                 :            :         int ret;
     713                 :            : 
     714                 :            :         name = rte_vdev_device_name(vdev);
     715                 :            :         if (name == NULL)
     716                 :            :                 return -1;
     717                 :            : 
     718                 :            :         ret = skeldma_destroy(name);
     719         [ +  - ]:          1 :         if (!ret)
     720                 :          1 :                 SKELDMA_LOG(INFO, "Remove %s dmadev", name);
     721                 :            : 
     722                 :            :         return ret;
     723                 :            : }
     724                 :            : 
     725                 :            : static struct rte_vdev_driver skeldma_pmd_drv = {
     726                 :            :         .probe = skeldma_probe,
     727                 :            :         .remove = skeldma_remove,
     728                 :            :         .drv_flags = RTE_VDEV_DRV_NEED_IOVA_AS_VA,
     729                 :            : };
     730                 :            : 
     731                 :        238 : RTE_PMD_REGISTER_VDEV(dma_skeleton, skeldma_pmd_drv);
     732                 :            : RTE_PMD_REGISTER_PARAM_STRING(dma_skeleton,
     733                 :            :                 SKELDMA_ARG_LCORE "=<uint16> ");

Generated by: LCOV version 1.14