LCOV - code coverage report
Current view: top level - drivers/dma/hisilicon - hisi_dmadev.c (source / functions) Hit Total Coverage
Test: Code coverage Lines: 2 366 0.5 %
Date: 2025-11-01 17:50:34 Functions: 2 33 6.1 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 1 140 0.7 %

           Branch data     Line data    Source code
       1                 :            : /* SPDX-License-Identifier: BSD-3-Clause
       2                 :            :  * Copyright(C) 2021 HiSilicon Limited
       3                 :            :  */
       4                 :            : 
       5                 :            : #include <inttypes.h>
       6                 :            : #include <string.h>
       7                 :            : 
       8                 :            : #include <bus_pci_driver.h>
       9                 :            : #include <rte_cycles.h>
      10                 :            : #include <rte_eal.h>
      11                 :            : #include <rte_io.h>
      12                 :            : #include <rte_log.h>
      13                 :            : #include <rte_malloc.h>
      14                 :            : #include <rte_memzone.h>
      15                 :            : #include <rte_pci.h>
      16                 :            : #include <rte_dmadev_pmd.h>
      17                 :            : 
      18                 :            : #include "hisi_dmadev.h"
      19                 :            : 
      20         [ -  + ]:        253 : RTE_LOG_REGISTER_DEFAULT(hisi_dma_logtype, INFO);
      21                 :            : #define RTE_LOGTYPE_HISI_DMA hisi_dma_logtype
      22                 :            : #define HISI_DMA_LOG(level, ...) \
      23                 :            :         RTE_LOG_LINE_PREFIX(level, HISI_DMA, "%s(): ", __func__, __VA_ARGS__)
      24                 :            : #define HISI_DMA_DEV_LOG(hw, level, ...) \
      25                 :            :         RTE_LOG_LINE_PREFIX(level, HISI_DMA, "%s %s(): ", \
      26                 :            :                 (hw)->data->dev_name RTE_LOG_COMMA __func__, __VA_ARGS__)
      27                 :            : #define HISI_DMA_DEBUG(hw, ...) \
      28                 :            :         HISI_DMA_DEV_LOG(hw, DEBUG, __VA_ARGS__)
      29                 :            : #define HISI_DMA_INFO(hw, ...) \
      30                 :            :         HISI_DMA_DEV_LOG(hw, INFO, __VA_ARGS__)
      31                 :            : #define HISI_DMA_WARN(hw, ...) \
      32                 :            :         HISI_DMA_DEV_LOG(hw, WARNING, __VA_ARGS__)
      33                 :            : #define HISI_DMA_ERR(hw, ...) \
      34                 :            :         HISI_DMA_DEV_LOG(hw, ERR, __VA_ARGS__)
      35                 :            : 
      36                 :            : static uint32_t
      37                 :            : hisi_dma_queue_base(struct hisi_dma_dev *hw)
      38                 :            : {
      39                 :            :         if (hw->reg_layout == HISI_DMA_REG_LAYOUT_HIP08)
      40                 :            :                 return HISI_DMA_HIP08_QUEUE_BASE;
      41                 :            :         else
      42                 :            :                 return 0;
      43                 :            : }
      44                 :            : 
      45                 :            : static volatile void *
      46                 :            : hisi_dma_queue_regaddr(struct hisi_dma_dev *hw, uint32_t qoff)
      47                 :            : {
      48                 :          0 :         uint32_t off = hisi_dma_queue_base(hw) +
      49                 :          0 :                         hw->queue_id * HISI_DMA_QUEUE_REGION_SIZE + qoff;
      50                 :          0 :         return (volatile void *)((char *)hw->io_base + off);
      51                 :            : }
      52                 :            : 
      53                 :            : static void
      54                 :            : hisi_dma_write_reg(void *base, uint32_t off, uint32_t val)
      55                 :            : {
      56                 :          0 :         rte_write32(rte_cpu_to_le_32(val),
      57                 :            :                     (volatile void *)((char *)base + off));
      58                 :            : }
      59                 :            : 
      60                 :            : static void
      61                 :            : hisi_dma_write_dev(struct hisi_dma_dev *hw, uint32_t off, uint32_t val)
      62                 :            : {
      63                 :          0 :         hisi_dma_write_reg(hw->io_base, off, val);
      64                 :            : }
      65                 :            : 
      66                 :            : static void
      67                 :            : hisi_dma_write_queue(struct hisi_dma_dev *hw, uint32_t qoff, uint32_t val)
      68                 :            : {
      69                 :          0 :         uint32_t off = hisi_dma_queue_base(hw) +
      70                 :          0 :                         hw->queue_id * HISI_DMA_QUEUE_REGION_SIZE + qoff;
      71                 :            :         hisi_dma_write_dev(hw, off, val);
      72                 :            : }
      73                 :            : 
      74                 :            : static uint32_t
      75                 :            : hisi_dma_read_reg(void *base, uint32_t off)
      76                 :            : {
      77                 :          0 :         uint32_t val = rte_read32((volatile void *)((char *)base + off));
      78                 :            :         return rte_le_to_cpu_32(val);
      79                 :            : }
      80                 :            : 
      81                 :            : static uint32_t
      82                 :            : hisi_dma_read_dev(struct hisi_dma_dev *hw, uint32_t off)
      83                 :            : {
      84                 :          0 :         return hisi_dma_read_reg(hw->io_base, off);
      85                 :            : }
      86                 :            : 
      87                 :            : static uint32_t
      88                 :            : hisi_dma_read_queue(struct hisi_dma_dev *hw, uint32_t qoff)
      89                 :            : {
      90                 :          0 :         uint32_t off = hisi_dma_queue_base(hw) +
      91                 :          0 :                         hw->queue_id * HISI_DMA_QUEUE_REGION_SIZE + qoff;
      92                 :            :         return hisi_dma_read_dev(hw, off);
      93                 :            : }
      94                 :            : 
      95                 :            : static void
      96                 :            : hisi_dma_update_bit(struct hisi_dma_dev *hw, uint32_t off, uint32_t pos,
      97                 :            :                     bool set)
      98                 :            : {
      99                 :            :         uint32_t tmp = hisi_dma_read_dev(hw, off);
     100                 :            :         uint32_t mask = 1u << pos;
     101                 :          0 :         tmp = set ? tmp | mask : tmp & ~mask;
     102                 :            :         hisi_dma_write_dev(hw, off, tmp);
     103                 :          0 : }
     104                 :            : 
     105                 :            : static void
     106                 :            : hisi_dma_update_queue_bit(struct hisi_dma_dev *hw, uint32_t qoff, uint32_t pos,
     107                 :            :                           bool set)
     108                 :            : {
     109                 :            :         uint32_t tmp = hisi_dma_read_queue(hw, qoff);
     110                 :            :         uint32_t mask = 1u << pos;
     111                 :          0 :         tmp = set ? tmp | mask : tmp & ~mask;
     112                 :            :         hisi_dma_write_queue(hw, qoff, tmp);
     113                 :            : }
     114                 :            : 
     115                 :            : static void
     116                 :            : hisi_dma_update_queue_mbit(struct hisi_dma_dev *hw, uint32_t qoff,
     117                 :            :                            uint32_t mask, bool set)
     118                 :            : {
     119                 :            :         uint32_t tmp = hisi_dma_read_queue(hw, qoff);
     120                 :          0 :         tmp = set ? tmp | mask : tmp & ~mask;
     121                 :            :         hisi_dma_write_queue(hw, qoff, tmp);
     122                 :          0 : }
     123                 :            : 
     124                 :            : #define hisi_dma_poll_hw_state(hw, val, cond, sleep_us, timeout_us) __extension__ ({ \
     125                 :            :         uint32_t timeout = 0; \
     126                 :            :         while (timeout++ <= (timeout_us)) { \
     127                 :            :                 (val) = hisi_dma_read_queue(hw, HISI_DMA_QUEUE_FSM_REG); \
     128                 :            :                 if (cond) \
     129                 :            :                         break; \
     130                 :            :                 rte_delay_us(sleep_us); \
     131                 :            :         } \
     132                 :            :         (cond) ? 0 : -ETIME; \
     133                 :            : })
     134                 :            : 
     135                 :            : static int
     136                 :          0 : hisi_dma_reset_hw(struct hisi_dma_dev *hw)
     137                 :            : {
     138                 :            : #define POLL_SLEEP_US   100
     139                 :            : #define POLL_TIMEOUT_US 10000
     140                 :            : 
     141                 :            :         uint32_t tmp;
     142                 :            :         int ret;
     143                 :            : 
     144                 :            :         hisi_dma_update_queue_bit(hw, HISI_DMA_QUEUE_CTRL0_REG,
     145                 :            :                                   HISI_DMA_QUEUE_CTRL0_PAUSE_B, true);
     146                 :            :         hisi_dma_update_queue_bit(hw, HISI_DMA_QUEUE_CTRL0_REG,
     147                 :            :                                   HISI_DMA_QUEUE_CTRL0_EN_B, false);
     148                 :            : 
     149   [ #  #  #  #  :          0 :         ret = hisi_dma_poll_hw_state(hw, tmp,
                   #  # ]
     150                 :            :                 FIELD_GET(HISI_DMA_QUEUE_FSM_STS_M, tmp) != HISI_DMA_STATE_RUN,
     151                 :            :                 POLL_SLEEP_US, POLL_TIMEOUT_US);
     152                 :            :         if (ret) {
     153                 :          0 :                 HISI_DMA_ERR(hw, "disable dma timeout!");
     154                 :          0 :                 return ret;
     155                 :            :         }
     156                 :            : 
     157                 :            :         hisi_dma_update_queue_bit(hw, HISI_DMA_QUEUE_CTRL1_REG,
     158                 :            :                                   HISI_DMA_QUEUE_CTRL1_RESET_B, true);
     159                 :            :         hisi_dma_write_queue(hw, HISI_DMA_QUEUE_SQ_TAIL_REG, 0);
     160                 :            :         hisi_dma_write_queue(hw, HISI_DMA_QUEUE_CQ_HEAD_REG, 0);
     161                 :            :         hisi_dma_update_queue_bit(hw, HISI_DMA_QUEUE_CTRL0_REG,
     162                 :            :                                   HISI_DMA_QUEUE_CTRL0_PAUSE_B, false);
     163                 :            : 
     164   [ #  #  #  #  :          0 :         ret = hisi_dma_poll_hw_state(hw, tmp,
                   #  # ]
     165                 :            :                 FIELD_GET(HISI_DMA_QUEUE_FSM_STS_M, tmp) == HISI_DMA_STATE_IDLE,
     166                 :            :                 POLL_SLEEP_US, POLL_TIMEOUT_US);
     167                 :            :         if (ret) {
     168                 :          0 :                 HISI_DMA_ERR(hw, "reset dma timeout!");
     169                 :          0 :                 return ret;
     170                 :            :         }
     171                 :            : 
     172                 :            :         return 0;
     173                 :            : }
     174                 :            : 
     175                 :            : static void
     176                 :          0 : hisi_dma_init_common(struct hisi_dma_dev *hw)
     177                 :            : {
     178                 :          0 :         hisi_dma_write_queue(hw, HISI_DMA_QUEUE_SQ_BASE_L_REG,
     179                 :          0 :                              lower_32_bits(hw->sqe_iova));
     180                 :          0 :         hisi_dma_write_queue(hw, HISI_DMA_QUEUE_SQ_BASE_H_REG,
     181                 :          0 :                              upper_32_bits(hw->sqe_iova));
     182                 :          0 :         hisi_dma_write_queue(hw, HISI_DMA_QUEUE_CQ_BASE_L_REG,
     183                 :          0 :                              lower_32_bits(hw->cqe_iova));
     184                 :          0 :         hisi_dma_write_queue(hw, HISI_DMA_QUEUE_CQ_BASE_H_REG,
     185                 :          0 :                              upper_32_bits(hw->cqe_iova));
     186                 :          0 :         hisi_dma_write_queue(hw, HISI_DMA_QUEUE_SQ_DEPTH_REG,
     187                 :          0 :                              hw->sq_depth_mask);
     188                 :          0 :         hisi_dma_write_queue(hw, HISI_DMA_QUEUE_CQ_DEPTH_REG, hw->cq_depth - 1);
     189                 :            :         hisi_dma_write_queue(hw, HISI_DMA_QUEUE_SQ_TAIL_REG, 0);
     190                 :            :         hisi_dma_write_queue(hw, HISI_DMA_QUEUE_CQ_HEAD_REG, 0);
     191                 :            :         hisi_dma_write_queue(hw, HISI_DMA_QUEUE_ERR_INT_NUM0_REG, 0);
     192                 :            :         hisi_dma_write_queue(hw, HISI_DMA_QUEUE_ERR_INT_NUM1_REG, 0);
     193                 :            :         hisi_dma_write_queue(hw, HISI_DMA_QUEUE_ERR_INT_NUM2_REG, 0);
     194                 :          0 : }
     195                 :            : 
     196                 :            : static void
     197                 :          0 : hisi_dma_init_hw(struct hisi_dma_dev *hw)
     198                 :            : {
     199                 :          0 :         hisi_dma_init_common(hw);
     200                 :            : 
     201         [ #  # ]:          0 :         if (hw->reg_layout == HISI_DMA_REG_LAYOUT_HIP08) {
     202                 :            :                 hisi_dma_write_queue(hw, HISI_DMA_HIP08_QUEUE_ERR_INT_NUM3_REG,
     203                 :            :                                      0);
     204                 :            :                 hisi_dma_write_queue(hw, HISI_DMA_HIP08_QUEUE_ERR_INT_NUM4_REG,
     205                 :            :                                      0);
     206                 :            :                 hisi_dma_write_queue(hw, HISI_DMA_HIP08_QUEUE_ERR_INT_NUM5_REG,
     207                 :            :                                      0);
     208                 :            :                 hisi_dma_write_queue(hw, HISI_DMA_HIP08_QUEUE_ERR_INT_NUM6_REG,
     209                 :            :                                      0);
     210                 :            :                 hisi_dma_update_queue_bit(hw, HISI_DMA_QUEUE_CTRL0_REG,
     211                 :            :                                 HISI_DMA_HIP08_QUEUE_CTRL0_ERR_ABORT_B, false);
     212                 :            :                 hisi_dma_update_queue_mbit(hw, HISI_DMA_QUEUE_INT_STATUS_REG,
     213                 :            :                                 HISI_DMA_HIP08_QUEUE_INT_MASK_M, true);
     214                 :            :                 hisi_dma_update_queue_mbit(hw, HISI_DMA_QUEUE_INT_MASK_REG,
     215                 :            :                                 HISI_DMA_HIP08_QUEUE_INT_MASK_M, true);
     216                 :            :         }
     217                 :          0 : }
     218                 :            : 
     219                 :            : static void
     220         [ #  # ]:          0 : hisi_dma_init_gbl(void *pci_bar, uint8_t revision)
     221                 :            : {
     222                 :            :         struct hisi_dma_dev hw;
     223                 :            : 
     224                 :            :         memset(&hw, 0, sizeof(hw));
     225                 :          0 :         hw.io_base = pci_bar;
     226                 :            : 
     227         [ #  # ]:          0 :         if (revision == HISI_DMA_REVISION_HIP08B)
     228                 :            :                 hisi_dma_update_bit(&hw, HISI_DMA_HIP08_MODE_REG,
     229                 :            :                                     HISI_DMA_HIP08_MODE_SEL_B, true);
     230                 :          0 : }
     231                 :            : 
     232                 :            : static uint8_t
     233                 :            : hisi_dma_reg_layout(uint8_t revision)
     234                 :            : {
     235                 :          0 :         if (revision == HISI_DMA_REVISION_HIP08B)
     236                 :            :                 return HISI_DMA_REG_LAYOUT_HIP08;
     237                 :            :         else
     238                 :          0 :                 return HISI_DMA_REG_LAYOUT_INVALID;
     239                 :            : }
     240                 :            : 
     241                 :            : static void
     242                 :            : hisi_dma_zero_iomem(struct hisi_dma_dev *hw)
     243                 :            : {
     244                 :          0 :         memset(hw->iomz->addr, 0, hw->iomz_sz);
     245                 :            : }
     246                 :            : 
     247                 :            : static int
     248                 :          0 : hisi_dma_alloc_iomem(struct hisi_dma_dev *hw, uint16_t ring_size,
     249                 :            :                      const char *dev_name)
     250                 :            : {
     251                 :          0 :         uint32_t sq_size = sizeof(struct hisi_dma_sqe) * ring_size;
     252                 :          0 :         uint32_t cq_size = sizeof(struct hisi_dma_cqe) *
     253                 :          0 :                            (ring_size + HISI_DMA_CQ_RESERVED);
     254                 :          0 :         uint32_t status_size = sizeof(uint16_t) * ring_size;
     255                 :            :         char mz_name[RTE_MEMZONE_NAMESIZE];
     256                 :            :         const struct rte_memzone *iomz;
     257                 :            :         uint32_t total_size;
     258                 :            : 
     259                 :          0 :         sq_size = RTE_CACHE_LINE_ROUNDUP(sq_size);
     260                 :          0 :         cq_size = RTE_CACHE_LINE_ROUNDUP(cq_size);
     261                 :          0 :         status_size = RTE_CACHE_LINE_ROUNDUP(status_size);
     262                 :          0 :         total_size = sq_size + cq_size + status_size;
     263                 :            : 
     264                 :            :         (void)snprintf(mz_name, sizeof(mz_name), "hisi_dma:%s", dev_name);
     265                 :          0 :         iomz = rte_memzone_reserve(mz_name, total_size, hw->data->numa_node,
     266                 :            :                                    RTE_MEMZONE_IOVA_CONTIG);
     267         [ #  # ]:          0 :         if (iomz == NULL) {
     268                 :          0 :                 HISI_DMA_ERR(hw, "malloc %s iomem fail!", mz_name);
     269                 :          0 :                 return -ENOMEM;
     270                 :            :         }
     271                 :            : 
     272                 :          0 :         hw->iomz = iomz;
     273                 :          0 :         hw->iomz_sz = total_size;
     274                 :          0 :         hw->sqe = iomz->addr;
     275                 :          0 :         hw->cqe = (void *)((char *)iomz->addr + sq_size);
     276                 :          0 :         hw->status = (void *)((char *)iomz->addr + sq_size + cq_size);
     277                 :          0 :         hw->sqe_iova = iomz->iova;
     278                 :          0 :         hw->cqe_iova = iomz->iova + sq_size;
     279                 :          0 :         hw->sq_depth_mask = ring_size - 1;
     280                 :          0 :         hw->cq_depth = ring_size + HISI_DMA_CQ_RESERVED;
     281                 :            :         hisi_dma_zero_iomem(hw);
     282                 :            : 
     283                 :          0 :         return 0;
     284                 :            : }
     285                 :            : 
     286                 :            : static void
     287                 :            : hisi_dma_free_iomem(struct hisi_dma_dev *hw)
     288                 :            : {
     289                 :          0 :         rte_memzone_free(hw->iomz);
     290                 :            : 
     291                 :          0 :         hw->iomz = NULL;
     292                 :          0 :         hw->sqe = NULL;
     293                 :          0 :         hw->cqe = NULL;
     294                 :          0 :         hw->status = NULL;
     295                 :          0 :         hw->sqe_iova = 0;
     296                 :          0 :         hw->cqe_iova = 0;
     297                 :          0 :         hw->sq_depth_mask = 0;
     298                 :          0 :         hw->cq_depth = 0;
     299                 :          0 : }
     300                 :            : 
     301                 :            : static int
     302                 :          0 : hisi_dma_info_get(const struct rte_dma_dev *dev,
     303                 :            :                   struct rte_dma_info *dev_info,
     304                 :            :                   uint32_t info_sz)
     305                 :            : {
     306                 :            :         RTE_SET_USED(dev);
     307                 :            :         RTE_SET_USED(info_sz);
     308                 :            : 
     309                 :          0 :         dev_info->dev_capa = RTE_DMA_CAPA_MEM_TO_MEM |
     310                 :            :                              RTE_DMA_CAPA_OPS_COPY;
     311                 :          0 :         dev_info->max_vchans = 1;
     312                 :          0 :         dev_info->max_desc = HISI_DMA_MAX_DESC_NUM;
     313                 :          0 :         dev_info->min_desc = HISI_DMA_MIN_DESC_NUM;
     314                 :            : 
     315                 :          0 :         return 0;
     316                 :            : }
     317                 :            : 
     318                 :            : static int
     319                 :          0 : hisi_dma_configure(struct rte_dma_dev *dev,
     320                 :            :                    const struct rte_dma_conf *conf,
     321                 :            :                    uint32_t conf_sz)
     322                 :            : {
     323                 :            :         RTE_SET_USED(dev);
     324                 :            :         RTE_SET_USED(conf);
     325                 :            :         RTE_SET_USED(conf_sz);
     326                 :          0 :         return 0;
     327                 :            : }
     328                 :            : 
     329                 :            : static int
     330                 :          0 : hisi_dma_vchan_setup(struct rte_dma_dev *dev, uint16_t vchan,
     331                 :            :                      const struct rte_dma_vchan_conf *conf,
     332                 :            :                      uint32_t conf_sz)
     333                 :            : {
     334                 :          0 :         struct hisi_dma_dev *hw = dev->data->dev_private;
     335                 :            :         int ret;
     336                 :            : 
     337                 :            :         RTE_SET_USED(vchan);
     338                 :            :         RTE_SET_USED(conf_sz);
     339                 :            : 
     340         [ #  # ]:          0 :         if (!rte_is_power_of_2(conf->nb_desc)) {
     341                 :          0 :                 HISI_DMA_ERR(hw, "Number of desc must be power of 2!");
     342                 :          0 :                 return -EINVAL;
     343                 :            :         }
     344                 :            : 
     345                 :            :         hisi_dma_free_iomem(hw);
     346                 :          0 :         ret = hisi_dma_alloc_iomem(hw, conf->nb_desc, dev->data->dev_name);
     347         [ #  # ]:          0 :         if (ret)
     348                 :          0 :                 return ret;
     349                 :            : 
     350                 :            :         return 0;
     351                 :            : }
     352                 :            : 
     353                 :            : static int
     354                 :          0 : hisi_dma_start(struct rte_dma_dev *dev)
     355                 :            : {
     356                 :          0 :         struct hisi_dma_dev *hw = dev->data->dev_private;
     357                 :            : 
     358         [ #  # ]:          0 :         if (hw->iomz == NULL) {
     359                 :          0 :                 HISI_DMA_ERR(hw, "Vchan was not setup, start fail!");
     360                 :          0 :                 return -EINVAL;
     361                 :            :         }
     362                 :            : 
     363                 :            :         /* Reset the dmadev to a known state, include:
     364                 :            :          *   1) zero iomem, also include status fields.
     365                 :            :          *   2) init hardware register.
     366                 :            :          *   3) init index values to zero.
     367                 :            :          *   4) init running statistics.
     368                 :            :          */
     369                 :            :         hisi_dma_zero_iomem(hw);
     370                 :          0 :         hisi_dma_init_hw(hw);
     371                 :          0 :         hw->ridx = 0;
     372                 :          0 :         hw->cridx = 0;
     373                 :          0 :         hw->sq_head = 0;
     374                 :          0 :         hw->sq_tail = 0;
     375                 :          0 :         hw->cq_sq_head = 0;
     376                 :          0 :         hw->cq_head = 0;
     377                 :          0 :         hw->cqs_completed = 0;
     378                 :          0 :         hw->cqe_vld = 1;
     379                 :          0 :         hw->stop_proc = 0;
     380                 :          0 :         hw->submitted = 0;
     381                 :          0 :         hw->completed = 0;
     382                 :          0 :         hw->errors = 0;
     383                 :          0 :         hw->qfulls = 0;
     384                 :            : 
     385                 :            :         hisi_dma_update_queue_bit(hw, HISI_DMA_QUEUE_CTRL0_REG,
     386                 :            :                                   HISI_DMA_QUEUE_CTRL0_EN_B, true);
     387                 :            : 
     388                 :          0 :         return 0;
     389                 :            : }
     390                 :            : 
     391                 :            : static int
     392                 :          0 : hisi_dma_close(struct rte_dma_dev *dev)
     393                 :            : {
     394         [ #  # ]:          0 :         if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
     395                 :            :                 /* The dmadev already stopped */
     396                 :          0 :                 hisi_dma_free_iomem(dev->data->dev_private);
     397                 :            :         }
     398                 :          0 :         return 0;
     399                 :            : }
     400                 :            : 
     401                 :            : static int
     402                 :          0 : hisi_dma_stats_get(const struct rte_dma_dev *dev, uint16_t vchan,
     403                 :            :                    struct rte_dma_stats *stats,
     404                 :            :                    uint32_t stats_sz)
     405                 :            : {
     406                 :          0 :         struct hisi_dma_dev *hw = dev->data->dev_private;
     407                 :            : 
     408                 :            :         RTE_SET_USED(vchan);
     409                 :            :         RTE_SET_USED(stats_sz);
     410                 :          0 :         stats->submitted = hw->submitted;
     411                 :          0 :         stats->completed = hw->completed;
     412                 :          0 :         stats->errors = hw->errors;
     413                 :            : 
     414                 :          0 :         return 0;
     415                 :            : }
     416                 :            : 
     417                 :            : static int
     418                 :          0 : hisi_dma_stats_reset(struct rte_dma_dev *dev, uint16_t vchan)
     419                 :            : {
     420                 :          0 :         struct hisi_dma_dev *hw = dev->data->dev_private;
     421                 :            : 
     422                 :            :         RTE_SET_USED(vchan);
     423                 :          0 :         hw->submitted = 0;
     424                 :          0 :         hw->completed = 0;
     425                 :          0 :         hw->errors = 0;
     426                 :          0 :         hw->qfulls = 0;
     427                 :            : 
     428                 :          0 :         return 0;
     429                 :            : }
     430                 :            : 
     431                 :            : static int
     432                 :          0 : hisi_dma_vchan_status(const struct rte_dma_dev *dev, uint16_t vchan,
     433                 :            :                       enum rte_dma_vchan_status *status)
     434                 :            : {
     435                 :          0 :         struct hisi_dma_dev *hw = dev->data->dev_private;
     436                 :            :         uint32_t val;
     437                 :            : 
     438                 :            :         RTE_SET_USED(vchan);
     439                 :            : 
     440                 :            :         val = hisi_dma_read_queue(hw, HISI_DMA_QUEUE_FSM_REG);
     441                 :            :         val = FIELD_GET(HISI_DMA_QUEUE_FSM_STS_M, val);
     442   [ #  #  #  # ]:          0 :         if (val == HISI_DMA_STATE_RUN)
     443                 :          0 :                 *status = RTE_DMA_VCHAN_ACTIVE;
     444   [ #  #  #  # ]:          0 :         else if (val == HISI_DMA_STATE_CPL)
     445                 :          0 :                 *status = RTE_DMA_VCHAN_IDLE;
     446                 :            :         else
     447                 :          0 :                 *status = RTE_DMA_VCHAN_HALTED_ERROR;
     448                 :            : 
     449                 :          0 :         return 0;
     450                 :            : }
     451                 :            : 
     452                 :            : static int
     453                 :          0 : hisi_dma_stop(struct rte_dma_dev *dev)
     454                 :            : {
     455                 :            : #define MAX_WAIT_MSEC   10
     456                 :          0 :         struct hisi_dma_dev *hw = dev->data->dev_private;
     457                 :            :         enum rte_dma_vchan_status status;
     458                 :            :         uint32_t i;
     459                 :            : 
     460                 :            :         /* Flag stop processing new requests. */
     461                 :          0 :         hw->stop_proc = 1;
     462                 :            :         rte_delay_ms(1);
     463                 :            : 
     464                 :            :         /* Force set drop flag so that the hardware can quickly complete. */
     465         [ #  # ]:          0 :         for (i = 0; i <= hw->sq_depth_mask; i++)
     466                 :          0 :                 hw->sqe[i].dw0 |= SQE_DROP_FLAG;
     467                 :            : 
     468                 :            :         i = 0;
     469                 :            :         do {
     470                 :            :                 hisi_dma_vchan_status(dev, 0, &status);
     471                 :            :                 if (status != RTE_DMA_VCHAN_ACTIVE)
     472                 :            :                         break;
     473                 :            :                 rte_delay_ms(1);
     474         [ #  # ]:          0 :         } while (i++ < MAX_WAIT_MSEC);
     475         [ #  # ]:          0 :         if (status == RTE_DMA_VCHAN_ACTIVE) {
     476                 :          0 :                 HISI_DMA_ERR(hw, "dev is still active!");
     477                 :          0 :                 return -EBUSY;
     478                 :            :         }
     479                 :            : 
     480                 :          0 :         return hisi_dma_reset_hw(dev->data->dev_private);
     481                 :            : }
     482                 :            : 
     483                 :            : static void
     484                 :          0 : hisi_dma_dump_range(struct hisi_dma_dev *hw, FILE *f, uint32_t start,
     485                 :            :                     uint32_t end)
     486                 :            : {
     487                 :            : #define DUMP_REGNUM_PER_LINE    4
     488                 :            : 
     489                 :            :         uint32_t cnt, i;
     490                 :            : 
     491                 :            :         cnt = 0;
     492         [ #  # ]:          0 :         for (i = start; i <= end; i += sizeof(uint32_t)) {
     493         [ #  # ]:          0 :                 if (cnt % DUMP_REGNUM_PER_LINE == 0)
     494                 :            :                         (void)fprintf(f, "      [%4x]:", i);
     495                 :            :                 (void)fprintf(f, " 0x%08x", hisi_dma_read_dev(hw, i));
     496                 :          0 :                 cnt++;
     497         [ #  # ]:          0 :                 if (cnt % DUMP_REGNUM_PER_LINE == 0)
     498                 :            :                         (void)fprintf(f, "\n");
     499                 :            :         }
     500         [ #  # ]:          0 :         if (cnt % DUMP_REGNUM_PER_LINE)
     501                 :            :                 (void)fprintf(f, "\n");
     502                 :          0 : }
     503                 :            : 
     504                 :            : static void
     505                 :          0 : hisi_dma_dump_common(struct hisi_dma_dev *hw, FILE *f)
     506                 :            : {
     507                 :            :         struct {
     508                 :            :                 uint8_t reg_layout;
     509                 :            :                 uint32_t start;
     510                 :            :                 uint32_t end;
     511                 :          0 :         } reg_info[] = {
     512                 :            :                 { HISI_DMA_REG_LAYOUT_HIP08,
     513                 :            :                   HISI_DMA_HIP08_DUMP_START_REG,
     514                 :            :                   HISI_DMA_HIP08_DUMP_END_REG },
     515                 :            :         };
     516                 :            :         uint32_t i;
     517                 :            : 
     518                 :            :         (void)fprintf(f, "    common-register:\n");
     519         [ #  # ]:          0 :         for (i = 0; i < RTE_DIM(reg_info); i++) {
     520         [ #  # ]:          0 :                 if (hw->reg_layout != reg_info[i].reg_layout)
     521                 :          0 :                         continue;
     522                 :          0 :                 hisi_dma_dump_range(hw, f, reg_info[i].start, reg_info[i].end);
     523                 :            :         }
     524                 :          0 : }
     525                 :            : 
     526                 :            : static void
     527                 :          0 : hisi_dma_dump_read_queue(struct hisi_dma_dev *hw, uint32_t qoff,
     528                 :            :                          char *buffer, int max_sz)
     529                 :            : {
     530         [ #  # ]:          0 :         memset(buffer, 0, max_sz);
     531                 :            : 
     532                 :            :         /* Address-related registers are not printed for security reasons. */
     533                 :          0 :         if (qoff == HISI_DMA_QUEUE_SQ_BASE_L_REG ||
     534         [ #  # ]:          0 :             qoff == HISI_DMA_QUEUE_SQ_BASE_H_REG ||
     535                 :          0 :             qoff == HISI_DMA_QUEUE_CQ_BASE_L_REG ||
     536         [ #  # ]:          0 :             qoff == HISI_DMA_QUEUE_CQ_BASE_H_REG) {
     537                 :            :                 (void)snprintf(buffer, max_sz, "**********");
     538                 :          0 :                 return;
     539                 :            :         }
     540                 :            : 
     541                 :            :         (void)snprintf(buffer, max_sz, "0x%08x", hisi_dma_read_queue(hw, qoff));
     542                 :            : }
     543                 :            : 
     544                 :            : static void
     545                 :          0 : hisi_dma_dump_queue(struct hisi_dma_dev *hw, FILE *f)
     546                 :            : {
     547                 :            : #define REG_FMT_LEN     32
     548                 :          0 :         char buf[REG_FMT_LEN] = { 0 };
     549                 :            :         uint32_t i;
     550                 :            : 
     551                 :            :         (void)fprintf(f, "    queue-register:\n");
     552         [ #  # ]:          0 :         for (i = 0; i < HISI_DMA_QUEUE_REGION_SIZE; ) {
     553                 :          0 :                 hisi_dma_dump_read_queue(hw, i, buf, sizeof(buf));
     554                 :            :                 (void)fprintf(f, "      [%2x]: %s", i, buf);
     555                 :          0 :                 i += sizeof(uint32_t);
     556                 :          0 :                 hisi_dma_dump_read_queue(hw, i, buf, sizeof(buf));
     557                 :            :                 (void)fprintf(f, " %s", buf);
     558                 :          0 :                 i += sizeof(uint32_t);
     559                 :          0 :                 hisi_dma_dump_read_queue(hw, i, buf, sizeof(buf));
     560                 :            :                 (void)fprintf(f, " %s", buf);
     561                 :          0 :                 i += sizeof(uint32_t);
     562                 :          0 :                 hisi_dma_dump_read_queue(hw, i, buf, sizeof(buf));
     563                 :            :                 (void)fprintf(f, " %s\n", buf);
     564                 :          0 :                 i += sizeof(uint32_t);
     565                 :            :         }
     566                 :          0 : }
     567                 :            : 
     568                 :            : static int
     569                 :          0 : hisi_dma_dump(const struct rte_dma_dev *dev, FILE *f)
     570                 :            : {
     571                 :          0 :         struct hisi_dma_dev *hw = dev->data->dev_private;
     572                 :            : 
     573                 :          0 :         (void)fprintf(f,
     574                 :            :                 "    revision: 0x%x queue_id: %u ring_size: %u\n"
     575                 :            :                 "    ridx: %u cridx: %u\n"
     576                 :            :                 "    sq_head: %u sq_tail: %u cq_sq_head: %u\n"
     577                 :            :                 "    cq_head: %u cqs_completed: %u cqe_vld: %u stop_proc: %u\n"
     578                 :            :                 "    submitted: %" PRIu64 " completed: %" PRIu64 " errors: %"
     579                 :            :                 PRIu64 " qfulls: %" PRIu64 "\n",
     580                 :          0 :                 hw->revision, hw->queue_id,
     581                 :          0 :                 hw->sq_depth_mask > 0 ? hw->sq_depth_mask + 1 : 0,
     582                 :          0 :                 hw->ridx, hw->cridx,
     583                 :          0 :                 hw->sq_head, hw->sq_tail, hw->cq_sq_head,
     584         [ #  # ]:          0 :                 hw->cq_head, hw->cqs_completed, hw->cqe_vld, hw->stop_proc,
     585                 :            :                 hw->submitted, hw->completed, hw->errors, hw->qfulls);
     586                 :          0 :         hisi_dma_dump_queue(hw, f);
     587                 :          0 :         hisi_dma_dump_common(hw, f);
     588                 :            : 
     589                 :          0 :         return 0;
     590                 :            : }
     591                 :            : 
     592                 :            : static int
     593                 :          0 : hisi_dma_copy(void *dev_private, uint16_t vchan,
     594                 :            :                  rte_iova_t src, rte_iova_t dst,
     595                 :            :                  uint32_t length, uint64_t flags)
     596                 :            : {
     597                 :            :         struct hisi_dma_dev *hw = dev_private;
     598                 :          0 :         struct hisi_dma_sqe *sqe = &hw->sqe[hw->sq_tail];
     599                 :            : 
     600                 :            :         RTE_SET_USED(vchan);
     601                 :            : 
     602         [ #  # ]:          0 :         if (unlikely(hw->stop_proc > 0))
     603                 :            :                 return -EPERM;
     604                 :            : 
     605         [ #  # ]:          0 :         if (((hw->sq_tail + 1) & hw->sq_depth_mask) == hw->sq_head) {
     606                 :          0 :                 hw->qfulls++;
     607                 :          0 :                 return -ENOSPC;
     608                 :            :         }
     609                 :            : 
     610                 :          0 :         sqe->dw0 = rte_cpu_to_le_32(SQE_OPCODE_M2M);
     611                 :          0 :         sqe->dw1 = 0;
     612                 :          0 :         sqe->dw2 = 0;
     613                 :          0 :         sqe->length = rte_cpu_to_le_32(length);
     614                 :          0 :         sqe->src_addr = rte_cpu_to_le_64(src);
     615                 :          0 :         sqe->dst_addr = rte_cpu_to_le_64(dst);
     616                 :          0 :         hw->sq_tail = (hw->sq_tail + 1) & hw->sq_depth_mask;
     617                 :          0 :         hw->submitted++;
     618                 :            : 
     619         [ #  # ]:          0 :         if (flags & RTE_DMA_OP_FLAG_FENCE)
     620                 :          0 :                 sqe->dw0 |= rte_cpu_to_le_32(SQE_FENCE_FLAG);
     621         [ #  # ]:          0 :         if (flags & RTE_DMA_OP_FLAG_SUBMIT)
     622                 :          0 :                 rte_write32(rte_cpu_to_le_32(hw->sq_tail), hw->sq_tail_reg);
     623                 :            : 
     624                 :          0 :         return hw->ridx++;
     625                 :            : }
     626                 :            : 
     627                 :            : static int
     628                 :          0 : hisi_dma_submit(void *dev_private, uint16_t vchan)
     629                 :            : {
     630                 :            :         struct hisi_dma_dev *hw = dev_private;
     631                 :            : 
     632                 :            :         RTE_SET_USED(vchan);
     633                 :          0 :         rte_write32(rte_cpu_to_le_32(hw->sq_tail), hw->sq_tail_reg);
     634                 :            : 
     635                 :          0 :         return 0;
     636                 :            : }
     637                 :            : 
     638                 :            : static inline void
     639                 :          0 : hisi_dma_scan_cq(struct hisi_dma_dev *hw)
     640                 :            : {
     641                 :            :         volatile struct hisi_dma_cqe *cqe;
     642                 :          0 :         uint16_t csq_head = hw->cq_sq_head;
     643                 :          0 :         uint16_t cq_head = hw->cq_head;
     644                 :            :         uint16_t count = 0;
     645                 :            :         uint64_t misc;
     646                 :            : 
     647         [ #  # ]:          0 :         while (count < hw->cq_depth) {
     648                 :          0 :                 cqe = &hw->cqe[cq_head];
     649                 :          0 :                 misc = cqe->misc;
     650                 :            :                 misc = rte_le_to_cpu_64(misc);
     651         [ #  # ]:          0 :                 if (FIELD_GET(CQE_VALID_B, misc) != hw->cqe_vld)
     652                 :            :                         break;
     653                 :            : 
     654                 :          0 :                 csq_head = FIELD_GET(CQE_SQ_HEAD_MASK, misc);
     655         [ #  # ]:          0 :                 if (unlikely(csq_head > hw->sq_depth_mask)) {
     656                 :            :                         /**
     657                 :            :                          * Defensive programming to prevent overflow of the
     658                 :            :                          * status array indexed by csq_head. Only error logs
     659                 :            :                          * are used for prompting.
     660                 :            :                          */
     661                 :          0 :                         HISI_DMA_ERR(hw, "invalid csq_head:%u!", csq_head);
     662                 :            :                         count = 0;
     663                 :            :                         break;
     664                 :            :                 }
     665         [ #  # ]:          0 :                 if (unlikely(misc & CQE_STATUS_MASK))
     666                 :          0 :                         hw->status[csq_head] = FIELD_GET(CQE_STATUS_MASK,
     667                 :            :                                                          misc);
     668                 :            : 
     669                 :          0 :                 count++;
     670                 :          0 :                 cq_head++;
     671         [ #  # ]:          0 :                 if (cq_head == hw->cq_depth) {
     672                 :          0 :                         hw->cqe_vld = !hw->cqe_vld;
     673                 :            :                         cq_head = 0;
     674                 :            :                 }
     675                 :            :         }
     676                 :            : 
     677         [ #  # ]:          0 :         if (count == 0)
     678                 :          0 :                 return;
     679                 :            : 
     680                 :          0 :         hw->cq_head = cq_head;
     681                 :          0 :         hw->cq_sq_head = (csq_head + 1) & hw->sq_depth_mask;
     682                 :          0 :         hw->cqs_completed += count;
     683         [ #  # ]:          0 :         if (hw->cqs_completed >= HISI_DMA_CQ_RESERVED) {
     684                 :          0 :                 rte_write32(rte_cpu_to_le_32(cq_head), hw->cq_head_reg);
     685                 :          0 :                 hw->cqs_completed = 0;
     686                 :            :         }
     687                 :            : }
     688                 :            : 
     689                 :            : static inline uint16_t
     690                 :            : hisi_dma_calc_cpls(struct hisi_dma_dev *hw, const uint16_t nb_cpls)
     691                 :            : {
     692                 :            :         uint16_t cpl_num;
     693                 :            : 
     694   [ #  #  #  # ]:          0 :         if (hw->cq_sq_head >= hw->sq_head)
     695                 :          0 :                 cpl_num = hw->cq_sq_head - hw->sq_head;
     696                 :            :         else
     697                 :          0 :                 cpl_num = hw->sq_depth_mask + 1 - hw->sq_head + hw->cq_sq_head;
     698                 :            : 
     699                 :            :         if (cpl_num > nb_cpls)
     700                 :            :                 cpl_num = nb_cpls;
     701                 :            : 
     702                 :            :         return cpl_num;
     703                 :            : }
     704                 :            : 
     705                 :            : static uint16_t
     706                 :          0 : hisi_dma_completed(void *dev_private,
     707                 :            :                    uint16_t vchan, const uint16_t nb_cpls,
     708                 :            :                    uint16_t *last_idx, bool *has_error)
     709                 :            : {
     710                 :            :         struct hisi_dma_dev *hw = dev_private;
     711                 :          0 :         uint16_t sq_head = hw->sq_head;
     712                 :            :         uint16_t cpl_num, i;
     713                 :            : 
     714                 :            :         RTE_SET_USED(vchan);
     715                 :          0 :         hisi_dma_scan_cq(hw);
     716                 :            : 
     717                 :            :         cpl_num = hisi_dma_calc_cpls(hw, nb_cpls);
     718         [ #  # ]:          0 :         for (i = 0; i < cpl_num; i++) {
     719         [ #  # ]:          0 :                 if (hw->status[sq_head]) {
     720                 :          0 :                         *has_error = true;
     721                 :          0 :                         break;
     722                 :            :                 }
     723                 :          0 :                 sq_head = (sq_head + 1) & hw->sq_depth_mask;
     724                 :            :         }
     725                 :          0 :         *last_idx = hw->cridx + i - 1;
     726         [ #  # ]:          0 :         if (i > 0) {
     727                 :          0 :                 hw->cridx += i;
     728                 :          0 :                 hw->sq_head = sq_head;
     729                 :          0 :                 hw->completed += i;
     730                 :            :         }
     731                 :            : 
     732                 :          0 :         return i;
     733                 :            : }
     734                 :            : 
     735                 :            : static enum rte_dma_status_code
     736                 :          0 : hisi_dma_convert_status(uint16_t status)
     737                 :            : {
     738   [ #  #  #  #  :          0 :         switch (status) {
          #  #  #  #  #  
                      # ]
     739                 :            :         case HISI_DMA_STATUS_SUCCESS:
     740                 :            :                 return RTE_DMA_STATUS_SUCCESSFUL;
     741                 :          0 :         case HISI_DMA_STATUS_INVALID_OPCODE:
     742                 :          0 :                 return RTE_DMA_STATUS_INVALID_OPCODE;
     743                 :          0 :         case HISI_DMA_STATUS_INVALID_LENGTH:
     744                 :          0 :                 return RTE_DMA_STATUS_INVALID_LENGTH;
     745                 :          0 :         case HISI_DMA_STATUS_USER_ABORT:
     746                 :          0 :                 return RTE_DMA_STATUS_USER_ABORT;
     747                 :          0 :         case HISI_DMA_STATUS_REMOTE_READ_ERROR:
     748                 :            :         case HISI_DMA_STATUS_AXI_READ_ERROR:
     749                 :          0 :                 return RTE_DMA_STATUS_BUS_READ_ERROR;
     750                 :          0 :         case HISI_DMA_STATUS_AXI_WRITE_ERROR:
     751                 :          0 :                 return RTE_DMA_STATUS_BUS_WRITE_ERROR;
     752                 :          0 :         case HISI_DMA_STATUS_DATA_POISON:
     753                 :            :         case HISI_DMA_STATUS_REMOTE_DATA_POISION:
     754                 :          0 :                 return RTE_DMA_STATUS_DATA_POISION;
     755                 :          0 :         case HISI_DMA_STATUS_SQE_READ_ERROR:
     756                 :            :         case HISI_DMA_STATUS_SQE_READ_POISION:
     757                 :          0 :                 return RTE_DMA_STATUS_DESCRIPTOR_READ_ERROR;
     758                 :          0 :         case HISI_DMA_STATUS_LINK_DOWN_ERROR:
     759                 :          0 :                 return RTE_DMA_STATUS_DEV_LINK_ERROR;
     760                 :          0 :         default:
     761                 :          0 :                 return RTE_DMA_STATUS_ERROR_UNKNOWN;
     762                 :            :         }
     763                 :            : }
     764                 :            : 
     765                 :            : static uint16_t
     766                 :          0 : hisi_dma_completed_status(void *dev_private,
     767                 :            :                           uint16_t vchan, const uint16_t nb_cpls,
     768                 :            :                           uint16_t *last_idx, enum rte_dma_status_code *status)
     769                 :            : {
     770                 :            :         struct hisi_dma_dev *hw = dev_private;
     771                 :          0 :         uint16_t sq_head = hw->sq_head;
     772                 :            :         uint16_t cpl_num, i;
     773                 :            : 
     774                 :            :         RTE_SET_USED(vchan);
     775                 :          0 :         hisi_dma_scan_cq(hw);
     776                 :            : 
     777                 :            :         cpl_num = hisi_dma_calc_cpls(hw, nb_cpls);
     778         [ #  # ]:          0 :         for (i = 0; i < cpl_num; i++) {
     779                 :          0 :                 status[i] = hisi_dma_convert_status(hw->status[sq_head]);
     780                 :          0 :                 hw->errors += !!status[i];
     781                 :          0 :                 hw->status[sq_head] = HISI_DMA_STATUS_SUCCESS;
     782                 :          0 :                 sq_head = (sq_head + 1) & hw->sq_depth_mask;
     783                 :            :         }
     784                 :          0 :         *last_idx = hw->cridx + cpl_num - 1;
     785         [ #  # ]:          0 :         if (likely(cpl_num > 0)) {
     786                 :          0 :                 hw->cridx += cpl_num;
     787                 :          0 :                 hw->sq_head = sq_head;
     788                 :          0 :                 hw->completed += cpl_num;
     789                 :            :         }
     790                 :            : 
     791                 :          0 :         return cpl_num;
     792                 :            : }
     793                 :            : 
     794                 :            : static uint16_t
     795                 :          0 : hisi_dma_burst_capacity(const void *dev_private, uint16_t vchan)
     796                 :            : {
     797                 :            :         const struct hisi_dma_dev *hw = dev_private;
     798                 :          0 :         uint16_t sq_head = hw->sq_head;
     799                 :          0 :         uint16_t sq_tail = hw->sq_tail;
     800                 :            : 
     801                 :            :         RTE_SET_USED(vchan);
     802                 :            : 
     803         [ #  # ]:          0 :         return (sq_tail >= sq_head) ? hw->sq_depth_mask - sq_tail + sq_head :
     804                 :          0 :                                       sq_head - 1 - sq_tail;
     805                 :            : }
     806                 :            : 
     807                 :            : static void
     808                 :          0 : hisi_dma_gen_dev_name(const struct rte_pci_device *pci_dev,
     809                 :            :                       uint8_t queue_id, char *dev_name, size_t size)
     810                 :            : {
     811                 :          0 :         char name[RTE_DEV_NAME_MAX_LEN] = { 0 };
     812                 :            : 
     813                 :            :         memset(dev_name, 0, size);
     814                 :          0 :         rte_pci_device_name(&pci_dev->addr, name, sizeof(name));
     815                 :          0 :         (void)snprintf(dev_name, size, "%s-ch%u", name, queue_id);
     816                 :          0 : }
     817                 :            : 
     818                 :            : /**
     819                 :            :  * Hardware queue state machine:
     820                 :            :  *
     821                 :            :  *   -----------  dmadev_create   ------------------
     822                 :            :  *   | Unknown | ---------------> |      IDLE      |
     823                 :            :  *   -----------                  ------------------
     824                 :            :  *                                   ^          |
     825                 :            :  *                                   |          |dev_start
     826                 :            :  *                           dev_stop|          |
     827                 :            :  *                                   |          v
     828                 :            :  *                                ------------------
     829                 :            :  *                                |      CPL       |
     830                 :            :  *                                ------------------
     831                 :            :  *                                   ^          |
     832                 :            :  *                      hardware     |          |
     833                 :            :  *                      completed all|          |dev_submit
     834                 :            :  *                      descriptors  |          |
     835                 :            :  *                                   |          |
     836                 :            :  *                                ------------------
     837                 :            :  *                                |      RUN       |
     838                 :            :  *                                ------------------
     839                 :            :  *
     840                 :            :  */
     841                 :            : static const struct rte_dma_dev_ops hisi_dmadev_ops = {
     842                 :            :         .dev_info_get     = hisi_dma_info_get,
     843                 :            :         .dev_configure    = hisi_dma_configure,
     844                 :            :         .dev_start        = hisi_dma_start,
     845                 :            :         .dev_stop         = hisi_dma_stop,
     846                 :            :         .dev_close        = hisi_dma_close,
     847                 :            :         .vchan_setup      = hisi_dma_vchan_setup,
     848                 :            :         .stats_get        = hisi_dma_stats_get,
     849                 :            :         .stats_reset      = hisi_dma_stats_reset,
     850                 :            :         .vchan_status     = hisi_dma_vchan_status,
     851                 :            :         .dev_dump         = hisi_dma_dump,
     852                 :            : };
     853                 :            : 
     854                 :            : static int
     855                 :          0 : hisi_dma_create(struct rte_pci_device *pci_dev, uint8_t queue_id,
     856                 :            :                 uint8_t revision)
     857                 :            : {
     858                 :            : #define REG_PCI_BAR_INDEX       2
     859                 :            : 
     860                 :            :         char name[RTE_DEV_NAME_MAX_LEN];
     861                 :            :         struct rte_dma_dev *dev;
     862                 :            :         struct hisi_dma_dev *hw;
     863                 :            :         int ret;
     864                 :            : 
     865                 :          0 :         hisi_dma_gen_dev_name(pci_dev, queue_id, name, sizeof(name));
     866                 :          0 :         dev = rte_dma_pmd_allocate(name, pci_dev->device.numa_node,
     867                 :            :                                    sizeof(*hw));
     868         [ #  # ]:          0 :         if (dev == NULL) {
     869                 :          0 :                 HISI_DMA_LOG(ERR, "%s allocate dmadev fail!", name);
     870                 :          0 :                 return -EINVAL;
     871                 :            :         }
     872                 :            : 
     873                 :          0 :         dev->device = &pci_dev->device;
     874                 :          0 :         dev->dev_ops = &hisi_dmadev_ops;
     875                 :          0 :         dev->fp_obj->dev_private = dev->data->dev_private;
     876                 :          0 :         dev->fp_obj->copy = hisi_dma_copy;
     877                 :          0 :         dev->fp_obj->submit = hisi_dma_submit;
     878                 :          0 :         dev->fp_obj->completed = hisi_dma_completed;
     879                 :          0 :         dev->fp_obj->completed_status = hisi_dma_completed_status;
     880                 :          0 :         dev->fp_obj->burst_capacity = hisi_dma_burst_capacity;
     881                 :            : 
     882                 :            :         hw = dev->data->dev_private;
     883                 :          0 :         hw->data = dev->data;
     884         [ #  # ]:          0 :         hw->revision = revision;
     885                 :          0 :         hw->reg_layout = hisi_dma_reg_layout(revision);
     886                 :          0 :         hw->io_base = pci_dev->mem_resource[REG_PCI_BAR_INDEX].addr;
     887                 :          0 :         hw->queue_id = queue_id;
     888                 :          0 :         hw->sq_tail_reg = hisi_dma_queue_regaddr(hw,
     889                 :            :                                                  HISI_DMA_QUEUE_SQ_TAIL_REG);
     890                 :          0 :         hw->cq_head_reg = hisi_dma_queue_regaddr(hw,
     891                 :            :                                                  HISI_DMA_QUEUE_CQ_HEAD_REG);
     892                 :            : 
     893         [ #  # ]:          0 :         if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
     894                 :          0 :                 ret = hisi_dma_reset_hw(hw);
     895         [ #  # ]:          0 :                 if (ret) {
     896                 :          0 :                         HISI_DMA_LOG(ERR, "%s init device fail!", name);
     897                 :          0 :                         (void)rte_dma_pmd_release(name);
     898                 :          0 :                         return -EIO;
     899                 :            :                 }
     900                 :            :         }
     901                 :            : 
     902                 :          0 :         dev->state = RTE_DMA_DEV_READY;
     903                 :          0 :         HISI_DMA_LOG(DEBUG, "%s create dmadev success!", name);
     904                 :            : 
     905                 :          0 :         return 0;
     906                 :            : }
     907                 :            : 
     908                 :            : static int
     909                 :          0 : hisi_dma_check_revision(struct rte_pci_device *pci_dev, const char *name,
     910                 :            :                         uint8_t *out_revision)
     911                 :            : {
     912                 :            :         uint8_t revision;
     913                 :            :         int ret;
     914                 :            : 
     915                 :          0 :         ret = rte_pci_read_config(pci_dev, &revision, 1, RTE_PCI_REVISION_ID);
     916         [ #  # ]:          0 :         if (ret != 1) {
     917                 :          0 :                 HISI_DMA_LOG(ERR, "%s read PCI revision failed!", name);
     918                 :          0 :                 return -EINVAL;
     919                 :            :         }
     920         [ #  # ]:          0 :         if (hisi_dma_reg_layout(revision) == HISI_DMA_REG_LAYOUT_INVALID) {
     921                 :          0 :                 HISI_DMA_LOG(ERR, "%s revision: 0x%x not supported!",
     922                 :            :                              name, revision);
     923                 :          0 :                 return -EINVAL;
     924                 :            :         }
     925                 :            : 
     926                 :          0 :         *out_revision = revision;
     927                 :          0 :         return 0;
     928                 :            : }
     929                 :            : 
     930                 :            : static int
     931                 :          0 : hisi_dma_probe(struct rte_pci_driver *pci_drv __rte_unused,
     932                 :            :                struct rte_pci_device *pci_dev)
     933                 :            : {
     934                 :          0 :         char name[RTE_DEV_NAME_MAX_LEN] = { 0 };
     935                 :            :         uint8_t revision;
     936                 :            :         uint8_t i;
     937                 :            :         int ret;
     938                 :            : 
     939                 :          0 :         rte_pci_device_name(&pci_dev->addr, name, sizeof(name));
     940                 :            : 
     941         [ #  # ]:          0 :         if (pci_dev->mem_resource[2].addr == NULL) {
     942                 :          0 :                 HISI_DMA_LOG(ERR, "%s BAR2 is NULL!", name);
     943                 :          0 :                 return -ENODEV;
     944                 :            :         }
     945                 :            : 
     946                 :          0 :         ret = hisi_dma_check_revision(pci_dev, name, &revision);
     947         [ #  # ]:          0 :         if (ret)
     948                 :            :                 return ret;
     949                 :          0 :         HISI_DMA_LOG(DEBUG, "%s read PCI revision: 0x%x", name, revision);
     950                 :            : 
     951         [ #  # ]:          0 :         if (rte_eal_process_type() == RTE_PROC_PRIMARY)
     952                 :          0 :                 hisi_dma_init_gbl(pci_dev->mem_resource[2].addr, revision);
     953                 :            : 
     954         [ #  # ]:          0 :         for (i = 0; i < HISI_DMA_MAX_HW_QUEUES; i++) {
     955                 :          0 :                 ret = hisi_dma_create(pci_dev, i, revision);
     956         [ #  # ]:          0 :                 if (ret) {
     957                 :          0 :                         HISI_DMA_LOG(ERR, "%s create dmadev %u failed!",
     958                 :            :                                      name, i);
     959                 :          0 :                         break;
     960                 :            :                 }
     961                 :            :         }
     962                 :            : 
     963                 :            :         return ret;
     964                 :            : }
     965                 :            : 
     966                 :            : static int
     967                 :          0 : hisi_dma_remove(struct rte_pci_device *pci_dev)
     968                 :            : {
     969                 :            :         char name[RTE_DEV_NAME_MAX_LEN];
     970                 :            :         uint8_t i;
     971                 :            :         int ret;
     972                 :            : 
     973         [ #  # ]:          0 :         for (i = 0; i < HISI_DMA_MAX_HW_QUEUES; i++) {
     974                 :          0 :                 hisi_dma_gen_dev_name(pci_dev, i, name, sizeof(name));
     975                 :          0 :                 ret = rte_dma_pmd_release(name);
     976         [ #  # ]:          0 :                 if (ret)
     977                 :          0 :                         return ret;
     978                 :            :         }
     979                 :            : 
     980                 :            :         return 0;
     981                 :            : }
     982                 :            : 
     983                 :            : static const struct rte_pci_id pci_id_hisi_dma_map[] = {
     984                 :            :         { RTE_PCI_DEVICE(PCI_VENDOR_ID_HUAWEI, HISI_DMA_DEVICE_ID) },
     985                 :            :         { .vendor_id = 0, }, /* sentinel */
     986                 :            : };
     987                 :            : 
     988                 :            : static struct rte_pci_driver hisi_dma_pmd_drv = {
     989                 :            :         .id_table  = pci_id_hisi_dma_map,
     990                 :            :         .drv_flags = RTE_PCI_DRV_NEED_MAPPING,
     991                 :            :         .probe     = hisi_dma_probe,
     992                 :            :         .remove    = hisi_dma_remove,
     993                 :            : };
     994                 :            : 
     995                 :        253 : RTE_PMD_REGISTER_PCI(dma_hisilicon, hisi_dma_pmd_drv);
     996                 :            : RTE_PMD_REGISTER_PCI_TABLE(dma_hisilicon, pci_id_hisi_dma_map);
     997                 :            : RTE_PMD_REGISTER_KMOD_DEP(dma_hisilicon, "vfio-pci");

Generated by: LCOV version 1.14