LCOV - code coverage report
Current view: top level - drivers/raw/ntb - ntb.c (source / functions) Hit Total Coverage
Test: Code coverage Lines: 2 694 0.3 %
Date: 2026-12-01 19:01:22 Functions: 2 39 5.1 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 1 365 0.3 %

           Branch data     Line data    Source code
       1                 :            : /* SPDX-License-Identifier: BSD-3-Clause
       2                 :            :  * Copyright(c) 2019 Intel Corporation.
       3                 :            :  */
       4                 :            : #include <stdint.h>
       5                 :            : #include <stdio.h>
       6                 :            : #include <stdlib.h>
       7                 :            : #include <string.h>
       8                 :            : #include <errno.h>
       9                 :            : 
      10                 :            : #include <eal_export.h>
      11                 :            : #include <rte_common.h>
      12                 :            : #include <rte_lcore.h>
      13                 :            : #include <rte_cycles.h>
      14                 :            : #include <rte_eal.h>
      15                 :            : #include <rte_errno.h>
      16                 :            : #include <rte_log.h>
      17                 :            : #include <rte_pci.h>
      18                 :            : #include <rte_mbuf.h>
      19                 :            : #include <bus_pci_driver.h>
      20                 :            : #include <rte_memzone.h>
      21                 :            : #include <rte_memcpy.h>
      22                 :            : #include <rte_rawdev.h>
      23                 :            : #include <rte_rawdev_pmd.h>
      24                 :            : 
      25                 :            : #include "ntb_hw_amd.h"
      26                 :            : #include "ntb_hw_intel.h"
      27                 :            : #include "rte_pmd_ntb.h"
      28                 :            : #include "ntb.h"
      29                 :            : 
      30                 :            : static const struct rte_pci_id pci_id_ntb_map[] = {
      31                 :            :         { RTE_PCI_DEVICE(NTB_AMD_VENDOR_ID, NTB_AMD_DEV_ID_PRI) },
      32                 :            :         { RTE_PCI_DEVICE(NTB_AMD_VENDOR_ID, NTB_AMD_DEV_ID_SEC) },
      33                 :            :         { RTE_PCI_DEVICE(NTB_INTEL_VENDOR_ID, NTB_INTEL_DEV_ID_B2B_SKX) },
      34                 :            :         { RTE_PCI_DEVICE(NTB_INTEL_VENDOR_ID, NTB_INTEL_DEV_ID_B2B_ICX) },
      35                 :            :         { .vendor_id = 0, /* sentinel */ },
      36                 :            : };
      37                 :            : 
      38                 :            : static const struct rte_rawdev_ops ntb_ops;
      39                 :            : 
      40                 :            : /* Align with enum ntb_xstats_idx */
      41                 :            : static struct rte_rawdev_xstats_name ntb_xstats_names[] = {
      42                 :            :         {"Tx-packets"},
      43                 :            :         {"Tx-bytes"},
      44                 :            :         {"Tx-errors"},
      45                 :            :         {"Rx-packets"},
      46                 :            :         {"Rx-bytes"},
      47                 :            :         {"Rx-missed"},
      48                 :            : };
      49                 :            : #define NTB_XSTATS_NUM RTE_DIM(ntb_xstats_names)
      50                 :            : 
      51                 :            : static inline bool
      52                 :            : ntb_peer_dev_is_up(struct ntb_hw *hw)
      53                 :            : {
      54                 :          0 :         return rte_atomic_load_explicit(&hw->peer_dev_up, rte_memory_order_acquire) != 0;
      55                 :            : }
      56                 :            : 
      57                 :            : static void
      58                 :          0 : ntb_dev_intr_handler(void *param)
      59                 :            : {
      60                 :            :         struct rte_rawdev *dev = (struct rte_rawdev *)param;
      61                 :          0 :         struct ntb_hw *hw = dev->dev_private;
      62                 :            :         uint64_t db_bits;
      63                 :            : 
      64                 :          0 :         db_bits = (*hw->ntb_ops->db_read)(dev);
      65         [ #  # ]:          0 :         if (!db_bits)
      66                 :          0 :                 NTB_LOG(ERR, "No doorbells");
      67                 :            : 
      68                 :            :         /* Doorbell 0 is for peer device ready. */
      69         [ #  # ]:          0 :         if (db_bits & 1) {
      70                 :          0 :                 NTB_LOG(INFO, "DB0: Peer device is up.");
      71                 :            :                 /* Clear received doorbell. */
      72                 :          0 :                 (*hw->ntb_ops->db_clear)(dev, 1);
      73                 :            : 
      74                 :            :                 /**
      75                 :            :                  * Peer dev is already up. All mw settings are already done.
      76                 :            :                  * Skip them.
      77                 :            :                  */
      78         [ #  # ]:          0 :                 if (ntb_peer_dev_is_up(hw))
      79                 :            :                         return;
      80                 :            : 
      81                 :            :                 /* Check that the peer memory-window layout matches local. */
      82         [ #  # ]:          0 :                 if ((*hw->ntb_ops->peer_mw_validate)(dev) < 0)
      83                 :            :                         return;
      84                 :            : 
      85                 :          0 :                 rte_atomic_store_explicit(&hw->peer_dev_up, 1, rte_memory_order_release);
      86                 :            : 
      87                 :            :                 /**
      88                 :            :                  * Handshake with peer. Scratchpad and translation programming
      89                 :            :                  * only takes effect when both devices are up, so run the
      90                 :            :                  * handshake again on the doorbell and ring the peer so the
      91                 :            :                  * device that came up second does not miss the first doorbell.
      92                 :            :                  */
      93         [ #  # ]:          0 :                 if ((*hw->ntb_ops->dev_handshake)(dev) < 0) {
      94                 :          0 :                         NTB_LOG(ERR, "Handshake work failed.");
      95                 :          0 :                         return;
      96                 :            :                 }
      97                 :            : 
      98                 :            :                 /* To get the link info. */
      99                 :          0 :                 (*hw->ntb_ops->get_link_status)(dev);
     100                 :          0 :                 NTB_LOG(INFO, "Link is up. Link speed: %u. Link width: %u",
     101                 :            :                         hw->link_speed, hw->link_width);
     102                 :          0 :                 return;
     103                 :            :         }
     104                 :            : 
     105         [ #  # ]:          0 :         if (db_bits & (1 << 1)) {
     106                 :          0 :                 NTB_LOG(INFO, "DB1: Peer device is down.");
     107                 :            :                 /* Clear received doorbell. */
     108                 :          0 :                 (*hw->ntb_ops->db_clear)(dev, 2);
     109                 :            : 
     110                 :            :                 /* Peer device will be down, So clean local side too. */
     111                 :          0 :                 (*hw->ntb_ops->link_cleanup)(dev);
     112                 :            : 
     113                 :          0 :                 rte_atomic_store_explicit(&hw->peer_dev_up, 0, rte_memory_order_release);
     114                 :            :                 /* Response peer's dev_stop request. */
     115                 :          0 :                 (*hw->ntb_ops->peer_db_set)(dev, 2);
     116                 :          0 :                 return;
     117                 :            :         }
     118                 :            : 
     119         [ #  # ]:          0 :         if (db_bits & (1 << 2)) {
     120                 :          0 :                 NTB_LOG(INFO, "DB2: Peer device agrees dev to be down.");
     121                 :            :                 /* Clear received doorbell. */
     122                 :          0 :                 (*hw->ntb_ops->db_clear)(dev, (1 << 2));
     123                 :          0 :                 rte_atomic_store_explicit(&hw->peer_dev_up, 0, rte_memory_order_release);
     124                 :          0 :                 return;
     125                 :            :         }
     126                 :            : 
     127                 :            :         /* Clear other received doorbells. */
     128                 :          0 :         (*hw->ntb_ops->db_clear)(dev, db_bits);
     129                 :            : }
     130                 :            : 
     131                 :            : static void
     132                 :          0 : ntb_db_intr_unregister(struct rte_rawdev *dev)
     133                 :            : {
     134                 :          0 :         struct ntb_hw *hw = dev->dev_private;
     135                 :            :         struct rte_intr_handle *handle;
     136                 :            :         uint16_t i;
     137                 :            : 
     138         [ #  # ]:          0 :         for (i = 0; i < hw->nb_db_intr_handles; i++) {
     139                 :          0 :                 handle = hw->db_intr_handles[i];
     140                 :          0 :                 rte_intr_callback_unregister_sync(handle, ntb_dev_intr_handler, dev);
     141                 :            :                 /* The eventfd is owned by the PCI device interrupt handle. */
     142                 :          0 :                 rte_intr_fd_set(handle, -1);
     143                 :          0 :                 rte_intr_instance_free(handle);
     144                 :            :         }
     145                 :          0 :         free(hw->db_intr_handles);
     146                 :          0 :         hw->db_intr_handles = NULL;
     147                 :          0 :         hw->nb_db_intr_handles = 0;
     148                 :          0 : }
     149                 :            : 
     150                 :            : /*
     151                 :            :  * With MSI-X, only vector 0 is serviced through the PCI device interrupt handle.
     152                 :            :  * Each other doorbell raises its own vector, signalled on the eventfds created
     153                 :            :  * by rte_intr_efd_enable(): register the handler on them.
     154                 :            :  */
     155                 :            : static int
     156                 :          0 : ntb_db_intr_register(struct rte_rawdev *dev, struct rte_intr_handle *intr_handle)
     157                 :            : {
     158                 :          0 :         struct ntb_hw *hw = dev->dev_private;
     159                 :          0 :         int nb_efd = rte_intr_nb_efd_get(intr_handle);
     160                 :            :         struct rte_intr_handle *handle;
     161                 :            :         int i, ret;
     162                 :            : 
     163                 :          0 :         hw->db_intr_handles = calloc(nb_efd, sizeof(*hw->db_intr_handles));
     164         [ #  # ]:          0 :         if (hw->db_intr_handles == NULL)
     165                 :            :                 return -ENOMEM;
     166                 :            : 
     167         [ #  # ]:          0 :         for (i = 0; i < nb_efd; i++) {
     168                 :          0 :                 handle = rte_intr_instance_alloc(RTE_INTR_INSTANCE_F_PRIVATE);
     169         [ #  # ]:          0 :                 if (handle == NULL) {
     170                 :            :                         ret = -ENOMEM;
     171                 :          0 :                         goto err;
     172                 :            :                 }
     173                 :          0 :                 hw->db_intr_handles[hw->nb_db_intr_handles++] = handle;
     174                 :            : 
     175   [ #  #  #  # ]:          0 :                 if (rte_intr_type_set(handle, RTE_INTR_HANDLE_VFIO_MSIX) ||
     176                 :          0 :                                 rte_intr_fd_set(handle, rte_intr_efds_index_get(intr_handle, i))) {
     177                 :          0 :                         ret = -rte_errno;
     178                 :          0 :                         goto err;
     179                 :            :                 }
     180                 :            : 
     181                 :          0 :                 ret = rte_intr_callback_register(handle, ntb_dev_intr_handler, dev);
     182         [ #  # ]:          0 :                 if (ret)
     183                 :          0 :                         goto err;
     184                 :            :         }
     185                 :            : 
     186                 :            :         return 0;
     187                 :            : 
     188                 :          0 : err:
     189                 :          0 :         NTB_LOG(ERR, "Failed to register doorbell interrupt %d.", i + 1);
     190                 :          0 :         ntb_db_intr_unregister(dev);
     191                 :          0 :         return ret;
     192                 :            : }
     193                 :            : 
     194                 :            : static int
     195                 :          0 : ntb_queue_conf_get(struct rte_rawdev *dev,
     196                 :            :                    uint16_t queue_id,
     197                 :            :                    rte_rawdev_obj_t queue_conf,
     198                 :            :                    size_t conf_size)
     199                 :            : {
     200                 :            :         struct ntb_queue_conf *q_conf = queue_conf;
     201                 :          0 :         struct ntb_hw *hw = dev->dev_private;
     202                 :            : 
     203         [ #  # ]:          0 :         if (conf_size != sizeof(*q_conf))
     204                 :            :                 return -EINVAL;
     205                 :            : 
     206                 :          0 :         q_conf->tx_free_thresh = hw->tx_queues[queue_id]->tx_free_thresh;
     207                 :          0 :         q_conf->nb_desc = hw->rx_queues[queue_id]->nb_rx_desc;
     208                 :          0 :         q_conf->rx_mp = hw->rx_queues[queue_id]->mpool;
     209                 :            : 
     210                 :          0 :         return 0;
     211                 :            : }
     212                 :            : 
     213                 :            : static void
     214                 :          0 : ntb_rxq_release_mbufs(struct ntb_rx_queue *q)
     215                 :            : {
     216                 :            :         int i;
     217                 :            : 
     218   [ #  #  #  # ]:          0 :         if (!q || !q->sw_ring) {
     219                 :          0 :                 NTB_LOG(ERR, "Pointer to rxq or sw_ring is NULL");
     220                 :          0 :                 return;
     221                 :            :         }
     222                 :            : 
     223         [ #  # ]:          0 :         for (i = 0; i < q->nb_rx_desc; i++) {
     224         [ #  # ]:          0 :                 if (q->sw_ring[i].mbuf) {
     225                 :            :                         rte_pktmbuf_free_seg(q->sw_ring[i].mbuf);
     226                 :          0 :                         q->sw_ring[i].mbuf = NULL;
     227                 :            :                 }
     228                 :            :         }
     229                 :            : }
     230                 :            : 
     231                 :            : static void
     232                 :          0 : ntb_rxq_release(struct ntb_rx_queue *rxq)
     233                 :            : {
     234         [ #  # ]:          0 :         if (!rxq)
     235                 :            :                 return;
     236                 :            : 
     237                 :          0 :         ntb_rxq_release_mbufs(rxq);
     238                 :            : 
     239                 :          0 :         rte_free(rxq->sw_ring);
     240                 :          0 :         rte_free(rxq);
     241                 :            : }
     242                 :            : 
     243                 :            : /* The ring indexes wrap with a mask and the bursts use on-stack arrays. */
     244                 :            : static int
     245                 :          0 : ntb_queue_nb_desc_check(uint16_t nb_desc)
     246                 :            : {
     247         [ #  # ]:          0 :         if (nb_desc < NTB_MIN_DESC_SIZE || nb_desc > NTB_MAX_DESC_SIZE ||
     248         [ #  # ]:          0 :                         !rte_is_power_of_2(nb_desc)) {
     249                 :          0 :                 NTB_LOG(ERR, "Number of descriptors %u must be a power of 2 within [%u, %u].",
     250                 :            :                         nb_desc, NTB_MIN_DESC_SIZE, NTB_MAX_DESC_SIZE);
     251                 :          0 :                 return -EINVAL;
     252                 :            :         }
     253                 :            : 
     254                 :            :         return 0;
     255                 :            : }
     256                 :            : 
     257                 :            : static int
     258                 :          0 : ntb_rxq_setup(struct rte_rawdev *dev,
     259                 :            :               uint16_t qp_id,
     260                 :            :               rte_rawdev_obj_t queue_conf,
     261                 :            :               size_t conf_size)
     262                 :            : {
     263                 :            :         struct ntb_queue_conf *rxq_conf = queue_conf;
     264                 :          0 :         struct ntb_hw *hw = dev->dev_private;
     265                 :            :         struct ntb_rx_queue *rxq;
     266                 :            : 
     267         [ #  # ]:          0 :         if (conf_size != sizeof(*rxq_conf))
     268                 :            :                 return -EINVAL;
     269                 :            : 
     270         [ #  # ]:          0 :         if (ntb_queue_nb_desc_check(rxq_conf->nb_desc) < 0)
     271                 :            :                 return -EINVAL;
     272                 :            : 
     273                 :            :         /* Allocate the rx queue data structure */
     274                 :          0 :         rxq = rte_zmalloc_socket("ntb rx queue",
     275                 :            :                                  sizeof(struct ntb_rx_queue),
     276                 :            :                                  RTE_CACHE_LINE_SIZE,
     277                 :            :                                  dev->socket_id);
     278         [ #  # ]:          0 :         if (!rxq) {
     279                 :          0 :                 NTB_LOG(ERR, "Failed to allocate memory for "
     280                 :            :                             "rx queue data structure.");
     281                 :          0 :                 return -ENOMEM;
     282                 :            :         }
     283                 :            : 
     284                 :          0 :         rxq->nb_rx_desc = rxq_conf->nb_desc;
     285                 :          0 :         rxq->mpool = rxq_conf->rx_mp;
     286                 :          0 :         rxq->port_id = dev->dev_id;
     287                 :          0 :         rxq->queue_id = qp_id;
     288                 :          0 :         rxq->hw = hw;
     289                 :            : 
     290                 :            :         /* Allocate the software ring. */
     291                 :          0 :         rxq->sw_ring =
     292                 :          0 :                 rte_zmalloc_socket("ntb rx sw ring",
     293                 :            :                                    sizeof(struct ntb_rx_entry) *
     294                 :          0 :                                    rxq->nb_rx_desc,
     295                 :            :                                    RTE_CACHE_LINE_SIZE,
     296                 :            :                                    dev->socket_id);
     297         [ #  # ]:          0 :         if (!rxq->sw_ring) {
     298                 :          0 :                 ntb_rxq_release(rxq);
     299                 :            :                 rxq = NULL;
     300                 :          0 :                 NTB_LOG(ERR, "Failed to allocate memory for SW ring");
     301                 :          0 :                 return -ENOMEM;
     302                 :            :         }
     303                 :            : 
     304                 :          0 :         hw->rx_queues[qp_id] = rxq;
     305                 :            : 
     306                 :          0 :         return 0;
     307                 :            : }
     308                 :            : 
     309                 :            : static void
     310                 :          0 : ntb_txq_release_mbufs(struct ntb_tx_queue *q)
     311                 :            : {
     312                 :            :         int i;
     313                 :            : 
     314   [ #  #  #  # ]:          0 :         if (!q || !q->sw_ring) {
     315                 :          0 :                 NTB_LOG(ERR, "Pointer to txq or sw_ring is NULL");
     316                 :          0 :                 return;
     317                 :            :         }
     318                 :            : 
     319         [ #  # ]:          0 :         for (i = 0; i < q->nb_tx_desc; i++) {
     320         [ #  # ]:          0 :                 if (q->sw_ring[i].mbuf) {
     321                 :            :                         rte_pktmbuf_free_seg(q->sw_ring[i].mbuf);
     322                 :          0 :                         q->sw_ring[i].mbuf = NULL;
     323                 :            :                 }
     324                 :            :         }
     325                 :            : }
     326                 :            : 
     327                 :            : static void
     328                 :          0 : ntb_txq_release(struct ntb_tx_queue *txq)
     329                 :            : {
     330         [ #  # ]:          0 :         if (!txq)
     331                 :            :                 return;
     332                 :            : 
     333                 :          0 :         ntb_txq_release_mbufs(txq);
     334                 :            : 
     335                 :          0 :         rte_free(txq->sw_ring);
     336                 :          0 :         rte_free(txq);
     337                 :            : }
     338                 :            : 
     339                 :            : static int
     340                 :          0 : ntb_txq_setup(struct rte_rawdev *dev,
     341                 :            :               uint16_t qp_id,
     342                 :            :               rte_rawdev_obj_t queue_conf,
     343                 :            :               size_t conf_size)
     344                 :            : {
     345                 :            :         struct ntb_queue_conf *txq_conf = queue_conf;
     346                 :          0 :         struct ntb_hw *hw = dev->dev_private;
     347                 :            :         struct ntb_tx_queue *txq;
     348                 :            :         uint16_t i, prev, tx_free_thresh;
     349                 :            : 
     350         [ #  # ]:          0 :         if (conf_size != sizeof(*txq_conf))
     351                 :            :                 return -EINVAL;
     352                 :            : 
     353         [ #  # ]:          0 :         if (ntb_queue_nb_desc_check(txq_conf->nb_desc) < 0)
     354                 :            :                 return -EINVAL;
     355                 :            : 
     356         [ #  # ]:          0 :         tx_free_thresh = txq_conf->tx_free_thresh ?
     357                 :            :                 txq_conf->tx_free_thresh :
     358                 :            :                 NTB_DFLT_TX_FREE_THRESH;
     359         [ #  # ]:          0 :         if (tx_free_thresh >= txq_conf->nb_desc - 3) {
     360                 :          0 :                 NTB_LOG(ERR,
     361                 :            :                         "tx_free_thresh must be less than nb_desc - 3. (tx_free_thresh=%u qp_id=%u)",
     362                 :            :                         tx_free_thresh, qp_id);
     363                 :          0 :                 return -EINVAL;
     364                 :            :         }
     365                 :            : 
     366                 :            :         /* Allocate the TX queue data structure. */
     367                 :          0 :         txq = rte_zmalloc_socket("ntb tx queue",
     368                 :            :                                   sizeof(struct ntb_tx_queue),
     369                 :            :                                   RTE_CACHE_LINE_SIZE,
     370                 :            :                                   dev->socket_id);
     371         [ #  # ]:          0 :         if (!txq) {
     372                 :          0 :                 NTB_LOG(ERR, "Failed to allocate memory for "
     373                 :            :                             "tx queue structure");
     374                 :          0 :                 return -ENOMEM;
     375                 :            :         }
     376                 :            : 
     377                 :          0 :         txq->nb_tx_desc = txq_conf->nb_desc;
     378                 :          0 :         txq->port_id = dev->dev_id;
     379                 :          0 :         txq->queue_id = qp_id;
     380                 :          0 :         txq->hw = hw;
     381                 :            : 
     382                 :            :         /* Allocate software ring */
     383                 :          0 :         txq->sw_ring =
     384                 :          0 :                 rte_zmalloc_socket("ntb tx sw ring",
     385                 :            :                                    sizeof(struct ntb_tx_entry) *
     386                 :          0 :                                    txq->nb_tx_desc,
     387                 :            :                                    RTE_CACHE_LINE_SIZE,
     388                 :            :                                    dev->socket_id);
     389         [ #  # ]:          0 :         if (!txq->sw_ring) {
     390                 :          0 :                 ntb_txq_release(txq);
     391                 :            :                 txq = NULL;
     392                 :          0 :                 NTB_LOG(ERR, "Failed to allocate memory for SW TX ring");
     393                 :          0 :                 return -ENOMEM;
     394                 :            :         }
     395                 :            : 
     396                 :          0 :         prev = txq->nb_tx_desc - 1;
     397         [ #  # ]:          0 :         for (i = 0; i < txq->nb_tx_desc; i++) {
     398                 :          0 :                 txq->sw_ring[i].mbuf = NULL;
     399                 :          0 :                 txq->sw_ring[i].last_id = i;
     400                 :          0 :                 txq->sw_ring[prev].next_id = i;
     401                 :            :                 prev = i;
     402                 :            :         }
     403                 :            : 
     404                 :          0 :         txq->tx_free_thresh = tx_free_thresh;
     405                 :            : 
     406                 :          0 :         hw->tx_queues[qp_id] = txq;
     407                 :            : 
     408                 :          0 :         return 0;
     409                 :            : }
     410                 :            : 
     411                 :            : 
     412                 :            : static int
     413                 :          0 : ntb_queue_release(struct rte_rawdev *dev, uint16_t queue_id)
     414                 :            : {
     415                 :          0 :         struct ntb_hw *hw = dev->dev_private;
     416                 :            : 
     417         [ #  # ]:          0 :         if (queue_id >= hw->queue_pairs)
     418                 :            :                 return -EINVAL;
     419                 :            : 
     420                 :          0 :         ntb_txq_release(hw->tx_queues[queue_id]);
     421                 :          0 :         hw->tx_queues[queue_id] = NULL;
     422                 :          0 :         ntb_rxq_release(hw->rx_queues[queue_id]);
     423                 :          0 :         hw->rx_queues[queue_id] = NULL;
     424                 :            : 
     425                 :          0 :         return 0;
     426                 :            : }
     427                 :            : 
     428                 :            : static int
     429                 :          0 : ntb_queue_setup(struct rte_rawdev *dev,
     430                 :            :                 uint16_t queue_id,
     431                 :            :                 rte_rawdev_obj_t queue_conf,
     432                 :            :                 size_t conf_size)
     433                 :            : {
     434                 :            :         struct ntb_queue_conf *conf = queue_conf;
     435                 :          0 :         struct ntb_hw *hw = dev->dev_private;
     436                 :            :         int ret;
     437                 :            : 
     438         [ #  # ]:          0 :         if (queue_id >= hw->queue_pairs)
     439                 :            :                 return -EINVAL;
     440                 :            : 
     441         [ #  # ]:          0 :         if (conf == NULL || conf_size != sizeof(*conf))
     442                 :            :                 return -EINVAL;
     443                 :            : 
     444         [ #  # ]:          0 :         if (conf->rx_mp == NULL) {
     445                 :          0 :                 NTB_LOG(ERR, "Invalid null mempool pointer.");
     446                 :          0 :                 return -EINVAL;
     447                 :            :         }
     448                 :            : 
     449         [ #  # ]:          0 :         if (dev->started) {
     450                 :          0 :                 NTB_LOG(ERR, "Cannot setup queue %u of a started device.", queue_id);
     451                 :          0 :                 return -EBUSY;
     452                 :            :         }
     453                 :            : 
     454                 :            :         /* Release the queues of a previous setup. */
     455                 :          0 :         ntb_queue_release(dev, queue_id);
     456                 :            : 
     457                 :          0 :         ret = ntb_txq_setup(dev, queue_id, queue_conf, conf_size);
     458         [ #  # ]:          0 :         if (ret < 0)
     459                 :            :                 return ret;
     460                 :            : 
     461                 :          0 :         ret = ntb_rxq_setup(dev, queue_id, queue_conf, conf_size);
     462         [ #  # ]:          0 :         if (ret < 0)
     463                 :          0 :                 ntb_queue_release(dev, queue_id);
     464                 :            : 
     465                 :            :         return ret;
     466                 :            : }
     467                 :            : 
     468                 :            : static uint16_t
     469                 :          0 : ntb_queue_count(struct rte_rawdev *dev)
     470                 :            : {
     471                 :          0 :         struct ntb_hw *hw = dev->dev_private;
     472                 :          0 :         return hw->queue_pairs;
     473                 :            : }
     474                 :            : 
     475                 :            : static int
     476                 :          0 : ntb_queue_init(struct rte_rawdev *dev, uint16_t qp_id)
     477                 :            : {
     478                 :          0 :         struct ntb_hw *hw = dev->dev_private;
     479                 :          0 :         struct ntb_rx_queue *rxq = hw->rx_queues[qp_id];
     480                 :          0 :         struct ntb_tx_queue *txq = hw->tx_queues[qp_id];
     481                 :            :         volatile struct ntb_header *local_hdr;
     482                 :            :         struct ntb_header *remote_hdr;
     483                 :          0 :         uint16_t q_size = hw->queue_size;
     484                 :            :         uint32_t hdr_offset;
     485                 :            :         void *bar_addr;
     486                 :            :         uint16_t i;
     487                 :            : 
     488                 :            :         /* Put queue info into the start of shared memory. */
     489                 :          0 :         hdr_offset = hw->hdr_size_per_queue * qp_id;
     490                 :          0 :         local_hdr = (volatile struct ntb_header *)
     491                 :          0 :                     ((size_t)hw->mz[0]->addr + hdr_offset);
     492                 :          0 :         bar_addr = (*hw->ntb_ops->get_peer_mw_addr)(dev, 0);
     493         [ #  # ]:          0 :         if (bar_addr == NULL)
     494                 :            :                 return -EINVAL;
     495                 :          0 :         remote_hdr = (struct ntb_header *)
     496                 :          0 :                      ((size_t)bar_addr + hdr_offset);
     497                 :            : 
     498                 :            :         /* rxq init. */
     499                 :          0 :         rxq->rx_desc_ring = (struct ntb_desc *)
     500                 :          0 :                             (&remote_hdr->desc_ring);
     501                 :          0 :         rxq->rx_used_ring = (volatile struct ntb_used *)
     502                 :          0 :                             (&local_hdr->desc_ring[q_size]);
     503                 :          0 :         rxq->avail_cnt = &remote_hdr->avail_cnt;
     504                 :          0 :         rxq->used_cnt = &local_hdr->used_cnt;
     505                 :            : 
     506         [ #  # ]:          0 :         for (i = 0; i < rxq->nb_rx_desc - 1; i++) {
     507                 :          0 :                 struct rte_mbuf *mbuf = rte_mbuf_raw_alloc(rxq->mpool);
     508         [ #  # ]:          0 :                 if (unlikely(!mbuf)) {
     509                 :          0 :                         NTB_LOG(ERR, "Failed to allocate mbuf for RX");
     510                 :          0 :                         return -ENOMEM;
     511                 :            :                 }
     512                 :          0 :                 mbuf->port = dev->dev_id;
     513                 :            : 
     514                 :          0 :                 rxq->sw_ring[i].mbuf = mbuf;
     515                 :            : 
     516                 :          0 :                 rxq->rx_desc_ring[i].addr = rte_pktmbuf_mtod(mbuf, size_t);
     517                 :          0 :                 rxq->rx_desc_ring[i].len = mbuf->buf_len - RTE_PKTMBUF_HEADROOM;
     518                 :            :         }
     519                 :            :         rte_wmb();
     520                 :          0 :         *rxq->avail_cnt = rxq->nb_rx_desc - 1;
     521                 :          0 :         rxq->last_avail = rxq->nb_rx_desc - 1;
     522                 :          0 :         rxq->last_used = 0;
     523                 :            : 
     524                 :            :         /* txq init */
     525                 :          0 :         txq->tx_desc_ring = (volatile struct ntb_desc *)
     526                 :          0 :                             (&local_hdr->desc_ring);
     527                 :          0 :         txq->tx_used_ring = (struct ntb_used *)
     528                 :          0 :                             (&remote_hdr->desc_ring[q_size]);
     529                 :          0 :         txq->avail_cnt = &local_hdr->avail_cnt;
     530                 :          0 :         txq->used_cnt = &remote_hdr->used_cnt;
     531                 :            : 
     532                 :            :         rte_wmb();
     533                 :          0 :         *txq->used_cnt = 0;
     534                 :          0 :         txq->last_used = 0;
     535                 :          0 :         txq->last_avail = 0;
     536                 :          0 :         txq->nb_tx_free = txq->nb_tx_desc - 1;
     537                 :            : 
     538                 :            :         /* Set per queue stats. */
     539         [ #  # ]:          0 :         for (i = 0; i < NTB_XSTATS_NUM; i++) {
     540                 :          0 :                 hw->ntb_xstats[i + NTB_XSTATS_NUM * (qp_id + 1)] = 0;
     541                 :          0 :                 hw->ntb_xstats_off[i + NTB_XSTATS_NUM * (qp_id + 1)] = 0;
     542                 :            :         }
     543                 :            : 
     544                 :            :         return 0;
     545                 :            : }
     546                 :            : 
     547                 :            : static inline void
     548                 :          0 : ntb_enqueue_cleanup(struct ntb_tx_queue *txq)
     549                 :            : {
     550                 :          0 :         struct ntb_tx_entry *sw_ring = txq->sw_ring;
     551                 :          0 :         uint16_t tx_free = txq->last_avail;
     552                 :            :         uint16_t nb_to_clean, i;
     553                 :            : 
     554                 :            :         /* avail_cnt + 1 represents where to rx next in the peer. */
     555                 :          0 :         nb_to_clean = (*txq->avail_cnt - txq->last_avail + 1 +
     556                 :          0 :                         txq->nb_tx_desc) & (txq->nb_tx_desc - 1);
     557                 :          0 :         nb_to_clean = RTE_MIN(nb_to_clean, txq->tx_free_thresh);
     558         [ #  # ]:          0 :         for (i = 0; i < nb_to_clean; i++) {
     559         [ #  # ]:          0 :                 if (sw_ring[tx_free].mbuf)
     560                 :            :                         rte_pktmbuf_free_seg(sw_ring[tx_free].mbuf);
     561                 :          0 :                 tx_free = (tx_free + 1) & (txq->nb_tx_desc - 1);
     562                 :            :         }
     563                 :            : 
     564                 :          0 :         txq->nb_tx_free += nb_to_clean;
     565                 :          0 :         txq->last_avail = tx_free;
     566                 :          0 : }
     567                 :            : 
     568                 :            : static int
     569                 :          0 : ntb_enqueue_bufs(struct rte_rawdev *dev,
     570                 :            :                  struct rte_rawdev_buf **buffers,
     571                 :            :                  unsigned int count,
     572                 :            :                  rte_rawdev_obj_t context)
     573                 :            : {
     574                 :          0 :         struct ntb_hw *hw = dev->dev_private;
     575                 :          0 :         struct ntb_tx_queue *txq = hw->tx_queues[(size_t)context];
     576                 :          0 :         struct ntb_tx_entry *sw_ring = txq->sw_ring;
     577                 :            :         struct rte_mbuf *txm;
     578                 :            :         struct ntb_used tx_used[NTB_MAX_DESC_SIZE];
     579                 :            :         volatile struct ntb_desc *tx_item;
     580                 :            :         uint16_t tx_last, nb_segs, off, last_used, avail_cnt;
     581                 :            :         uint16_t nb_mbufs = 0;
     582                 :            :         uint16_t nb_tx = 0;
     583                 :            :         uint64_t bytes = 0;
     584                 :            :         void *buf_addr;
     585                 :            :         int i;
     586                 :            : 
     587   [ #  #  #  # ]:          0 :         if (unlikely(dev->started == 0 || !ntb_peer_dev_is_up(hw))) {
     588                 :          0 :                 NTB_LOG(DEBUG, "Link is not up.");
     589                 :          0 :                 return nb_tx;
     590                 :            :         }
     591                 :            : 
     592         [ #  # ]:          0 :         if (txq->nb_tx_free < txq->tx_free_thresh)
     593                 :          0 :                 ntb_enqueue_cleanup(txq);
     594                 :            : 
     595                 :          0 :         off = NTB_XSTATS_NUM * ((size_t)context + 1);
     596                 :          0 :         last_used = txq->last_used;
     597                 :          0 :         avail_cnt = *txq->avail_cnt;/* Where to alloc next. */
     598         [ #  # ]:          0 :         for (nb_tx = 0; nb_tx < count; nb_tx++) {
     599                 :          0 :                 txm = (struct rte_mbuf *)(buffers[nb_tx]->buf_addr);
     600   [ #  #  #  # ]:          0 :                 if (txm == NULL || txq->nb_tx_free < txm->nb_segs)
     601                 :            :                         break;
     602                 :            : 
     603                 :          0 :                 tx_last = (txq->last_used + txm->nb_segs - 1) &
     604                 :          0 :                           (txq->nb_tx_desc - 1);
     605                 :            :                 nb_segs = txm->nb_segs;
     606         [ #  # ]:          0 :                 for (i = 0; i < nb_segs; i++) {
     607                 :            :                         /* Not enough ring space for tx. */
     608         [ #  # ]:          0 :                         if (txq->last_used == avail_cnt)
     609                 :          0 :                                 goto end_of_tx;
     610                 :          0 :                         sw_ring[txq->last_used].mbuf = txm;
     611                 :          0 :                         tx_item = txq->tx_desc_ring + txq->last_used;
     612                 :            : 
     613         [ #  # ]:          0 :                         if (!tx_item->len) {
     614                 :          0 :                                 (hw->ntb_xstats[NTB_TX_ERRS_ID + off])++;
     615                 :          0 :                                 goto end_of_tx;
     616                 :            :                         }
     617         [ #  # ]:          0 :                         if (txm->data_len > tx_item->len) {
     618                 :          0 :                                 NTB_LOG(ERR, "Data length exceeds buf length."
     619                 :            :                                         " Only %u data would be transmitted.",
     620                 :            :                                         tx_item->len);
     621                 :          0 :                                 txm->data_len = tx_item->len;
     622                 :            :                         }
     623                 :            : 
     624                 :            :                         /* translate remote virtual addr to bar virtual addr */
     625                 :          0 :                         buf_addr = (*hw->ntb_ops->ioremap)(dev, tx_item->addr);
     626         [ #  # ]:          0 :                         if (buf_addr == NULL) {
     627                 :          0 :                                 (hw->ntb_xstats[NTB_TX_ERRS_ID + off])++;
     628                 :          0 :                                 NTB_LOG(ERR, "Null remap addr.");
     629                 :          0 :                                 goto end_of_tx;
     630                 :            :                         }
     631                 :          0 :                         rte_memcpy(buf_addr, rte_pktmbuf_mtod(txm, void *),
     632         [ #  # ]:          0 :                                    txm->data_len);
     633                 :            : 
     634                 :          0 :                         tx_used[nb_mbufs].len = txm->data_len;
     635                 :          0 :                         tx_used[nb_mbufs++].flags = (txq->last_used ==
     636                 :            :                                                     tx_last) ?
     637                 :          0 :                                                     NTB_FLAG_EOP : 0;
     638                 :            : 
     639                 :            :                         /* update stats */
     640                 :          0 :                         bytes += txm->data_len;
     641                 :            : 
     642                 :          0 :                         txm = txm->next;
     643                 :            : 
     644                 :          0 :                         sw_ring[txq->last_used].next_id = (txq->last_used + 1) &
     645                 :          0 :                                                   (txq->nb_tx_desc - 1);
     646                 :          0 :                         sw_ring[txq->last_used].last_id = tx_last;
     647                 :          0 :                         txq->last_used = (txq->last_used + 1) &
     648                 :            :                                          (txq->nb_tx_desc - 1);
     649                 :            :                 }
     650                 :          0 :                 txq->nb_tx_free -= nb_segs;
     651                 :            :         }
     652                 :            : 
     653                 :          0 : end_of_tx:
     654         [ #  # ]:          0 :         if (nb_tx) {
     655                 :            :                 uint16_t nb1, nb2;
     656         [ #  # ]:          0 :                 if (nb_mbufs > txq->nb_tx_desc - last_used) {
     657                 :          0 :                         nb1 = txq->nb_tx_desc - last_used;
     658                 :          0 :                         nb2 = nb_mbufs - txq->nb_tx_desc + last_used;
     659                 :            :                 } else {
     660                 :            :                         nb1 = nb_mbufs;
     661                 :            :                         nb2 = 0;
     662                 :            :                 }
     663         [ #  # ]:          0 :                 rte_memcpy(txq->tx_used_ring + last_used, tx_used,
     664                 :            :                            sizeof(struct ntb_used) * nb1);
     665         [ #  # ]:          0 :                 rte_memcpy(txq->tx_used_ring, tx_used + nb1,
     666                 :            :                            sizeof(struct ntb_used) * nb2);
     667                 :            :                 rte_wmb();
     668                 :          0 :                 *txq->used_cnt = txq->last_used;
     669                 :            : 
     670                 :            :                 /* update queue stats */
     671                 :          0 :                 hw->ntb_xstats[NTB_TX_BYTES_ID + off] += bytes;
     672                 :          0 :                 hw->ntb_xstats[NTB_TX_PKTS_ID + off] += nb_tx;
     673                 :            :         }
     674                 :            : 
     675                 :          0 :         return nb_tx;
     676                 :            : }
     677                 :            : 
     678                 :            : static int
     679                 :          0 : ntb_dequeue_bufs(struct rte_rawdev *dev,
     680                 :            :                  struct rte_rawdev_buf **buffers,
     681                 :            :                  unsigned int count,
     682                 :            :                  rte_rawdev_obj_t context)
     683                 :            : {
     684                 :          0 :         struct ntb_hw *hw = dev->dev_private;
     685                 :          0 :         struct ntb_rx_queue *rxq = hw->rx_queues[(size_t)context];
     686                 :          0 :         struct ntb_rx_entry *sw_ring = rxq->sw_ring;
     687                 :            :         struct ntb_desc rx_desc[NTB_MAX_DESC_SIZE];
     688                 :            :         struct rte_mbuf *new_mbufs[NTB_MAX_DESC_SIZE];
     689                 :            :         struct rte_mbuf *first = NULL, *rxm_t;
     690                 :            :         struct rte_mbuf *prev = NULL;
     691                 :            :         volatile struct ntb_used *rx_item;
     692                 :            :         uint16_t nb_mbufs = 0;
     693                 :            :         uint16_t nb_rx = 0;
     694                 :            :         uint64_t bytes = 0;
     695                 :            :         uint16_t off, last_avail, used_cnt, used_nb, nb_segs;
     696                 :            :         int i;
     697                 :            : 
     698   [ #  #  #  # ]:          0 :         if (unlikely(dev->started == 0 || !ntb_peer_dev_is_up(hw))) {
     699                 :          0 :                 NTB_LOG(DEBUG, "Link is not up");
     700                 :          0 :                 return nb_rx;
     701                 :            :         }
     702                 :            : 
     703                 :          0 :         used_cnt = *rxq->used_cnt;
     704                 :            : 
     705         [ #  # ]:          0 :         if (rxq->last_used == used_cnt)
     706                 :            :                 return nb_rx;
     707                 :            : 
     708                 :          0 :         last_avail = rxq->last_avail;
     709                 :          0 :         used_nb = (used_cnt - rxq->last_used) & (rxq->nb_rx_desc - 1);
     710                 :          0 :         count = RTE_MIN(count, used_nb);
     711         [ #  # ]:          0 :         for (nb_rx = 0; nb_rx < count; nb_rx++) {
     712                 :            :                 /* Only take a packet once the peer has published all of it. */
     713                 :          0 :                 for (nb_segs = 1; ; nb_segs++) {
     714         [ #  # ]:          0 :                         if (nb_segs > used_nb)
     715                 :          0 :                                 goto end_of_rx;
     716                 :          0 :                         rx_item = rxq->rx_used_ring + ((rxq->last_used + nb_segs - 1) &
     717                 :          0 :                                                        (rxq->nb_rx_desc - 1));
     718         [ #  # ]:          0 :                         if (rx_item->flags & NTB_FLAG_EOP)
     719                 :            :                                 break;
     720                 :            :                 }
     721                 :            : 
     722                 :            :                 /* Get the refill mbufs first so a packet is never partially consumed. */
     723   [ #  #  #  # ]:          0 :                 if (unlikely(rte_mbuf_raw_alloc_bulk(rxq->mpool, new_mbufs, nb_segs))) {
     724                 :          0 :                         NTB_LOG(ERR, "recv alloc mbuf failed.");
     725                 :          0 :                         goto end_of_rx;
     726                 :            :                 }
     727                 :          0 :                 used_nb -= nb_segs;
     728                 :            : 
     729         [ #  # ]:          0 :                 for (i = 0; i < nb_segs; i++) {
     730                 :          0 :                         rx_item = rxq->rx_used_ring + rxq->last_used;
     731                 :          0 :                         rxm_t = sw_ring[rxq->last_used].mbuf;
     732                 :          0 :                         sw_ring[rxq->last_used].mbuf = NULL;
     733                 :          0 :                         rxm_t->data_len = rx_item->len;
     734                 :          0 :                         rxm_t->data_off = RTE_PKTMBUF_HEADROOM;
     735                 :          0 :                         rxm_t->port = rxq->port_id;
     736                 :            : 
     737         [ #  # ]:          0 :                         if (!i) {
     738                 :          0 :                                 rxm_t->nb_segs = 1;
     739                 :            :                                 first = rxm_t;
     740                 :          0 :                                 first->pkt_len = 0;
     741                 :          0 :                                 buffers[nb_rx]->buf_addr = rxm_t;
     742                 :            :                         } else {
     743                 :          0 :                                 prev->next = rxm_t;
     744                 :          0 :                                 first->nb_segs++;
     745                 :            :                         }
     746                 :            : 
     747                 :            :                         prev = rxm_t;
     748                 :          0 :                         first->pkt_len += prev->data_len;
     749                 :          0 :                         rxq->last_used = (rxq->last_used + 1) &
     750                 :          0 :                                          (rxq->nb_rx_desc - 1);
     751                 :            : 
     752                 :          0 :                         rxm_t = new_mbufs[i];
     753                 :          0 :                         rxm_t->port = rxq->port_id;
     754                 :          0 :                         sw_ring[rxq->last_avail].mbuf = rxm_t;
     755                 :            : 
     756                 :            :                         /* fill new desc */
     757                 :          0 :                         rx_desc[nb_mbufs].addr =
     758                 :          0 :                                         rte_pktmbuf_mtod(rxm_t, size_t);
     759                 :          0 :                         rx_desc[nb_mbufs++].len = rxm_t->buf_len -
     760                 :            :                                                   RTE_PKTMBUF_HEADROOM;
     761                 :          0 :                         rxq->last_avail = (rxq->last_avail + 1) &
     762                 :            :                                           (rxq->nb_rx_desc - 1);
     763                 :            :                 }
     764                 :            :                 /* update stats */
     765                 :          0 :                 bytes += first->pkt_len;
     766                 :            :         }
     767                 :            : 
     768                 :          0 : end_of_rx:
     769         [ #  # ]:          0 :         if (nb_rx) {
     770                 :            :                 uint16_t nb1, nb2;
     771         [ #  # ]:          0 :                 if (nb_mbufs > rxq->nb_rx_desc - last_avail) {
     772                 :          0 :                         nb1 = rxq->nb_rx_desc - last_avail;
     773                 :          0 :                         nb2 = nb_mbufs - rxq->nb_rx_desc + last_avail;
     774                 :            :                 } else {
     775                 :            :                         nb1 = nb_mbufs;
     776                 :            :                         nb2 = 0;
     777                 :            :                 }
     778         [ #  # ]:          0 :                 rte_memcpy(rxq->rx_desc_ring + last_avail, rx_desc,
     779                 :            :                            sizeof(struct ntb_desc) * nb1);
     780         [ #  # ]:          0 :                 rte_memcpy(rxq->rx_desc_ring, rx_desc + nb1,
     781                 :            :                            sizeof(struct ntb_desc) * nb2);
     782                 :            :                 rte_wmb();
     783                 :          0 :                 *rxq->avail_cnt = rxq->last_avail;
     784                 :            : 
     785                 :            :                 /* update queue stats */
     786                 :          0 :                 off = NTB_XSTATS_NUM * ((size_t)context + 1);
     787                 :          0 :                 hw->ntb_xstats[NTB_RX_BYTES_ID + off] += bytes;
     788                 :          0 :                 hw->ntb_xstats[NTB_RX_PKTS_ID + off] += nb_rx;
     789                 :          0 :                 hw->ntb_xstats[NTB_RX_MISS_ID + off] += (count - nb_rx);
     790                 :            :         }
     791                 :            : 
     792                 :          0 :         return nb_rx;
     793                 :            : }
     794                 :            : 
     795                 :            : static int
     796                 :          0 : ntb_dev_info_get(struct rte_rawdev *dev, rte_rawdev_obj_t dev_info,
     797                 :            :                 size_t dev_info_size)
     798                 :            : {
     799                 :          0 :         struct ntb_hw *hw = dev->dev_private;
     800                 :            :         struct ntb_dev_info *info = dev_info;
     801                 :            : 
     802         [ #  # ]:          0 :         if (dev_info_size != sizeof(*info)) {
     803                 :          0 :                 NTB_LOG(ERR, "Invalid size parameter to %s", __func__);
     804                 :          0 :                 return -EINVAL;
     805                 :            :         }
     806                 :            : 
     807                 :          0 :         info->mw_cnt = hw->mw_cnt;
     808                 :          0 :         info->mw_size = hw->mw_size;
     809                 :            : 
     810   [ #  #  #  # ]:          0 :         if (!hw->queue_size || !hw->queue_pairs) {
     811                 :          0 :                 NTB_LOG(ERR, "No queue size and queue num assigned.");
     812                 :          0 :                 return -EAGAIN;
     813                 :            :         }
     814                 :            : 
     815                 :          0 :         hw->hdr_size_per_queue = RTE_ALIGN(sizeof(struct ntb_header) +
     816                 :            :                                 hw->queue_size * sizeof(struct ntb_desc) +
     817                 :            :                                 hw->queue_size * sizeof(struct ntb_used),
     818                 :            :                                 RTE_CACHE_LINE_SIZE);
     819                 :          0 :         info->ntb_hdr_size = hw->hdr_size_per_queue * hw->queue_pairs;
     820                 :            : 
     821                 :          0 :         return 0;
     822                 :            : }
     823                 :            : 
     824                 :            : RTE_EXPORT_EXPERIMENTAL_SYMBOL(rte_pmd_ntb_get_mem_align, 26.11)
     825                 :            : uint64_t
     826                 :          0 : rte_pmd_ntb_get_mem_align(uint16_t dev_id, uint32_t mw_id, uint64_t mw_len)
     827                 :            : {
     828                 :            :         struct rte_rawdev *dev;
     829                 :            :         struct ntb_hw *hw;
     830                 :            : 
     831   [ #  #  #  # ]:          0 :         if (dev_id >= RTE_RAWDEV_MAX_DEVS || !rte_rawdev_pmd_is_valid_dev(dev_id))
     832                 :            :                 return 0;
     833                 :            :         dev = rte_rawdev_pmd_get_dev(dev_id);
     834         [ #  # ]:          0 :         if (dev->dev_ops != &ntb_ops)
     835                 :            :                 return 0;
     836                 :            : 
     837                 :          0 :         hw = dev->dev_private;
     838         [ #  # ]:          0 :         if (mw_id >= hw->mw_cnt)
     839                 :            :                 return 0;
     840                 :            : 
     841                 :          0 :         return (*hw->ntb_ops->mem_align)(dev, mw_id, mw_len);
     842                 :            : }
     843                 :            : 
     844                 :            : static int
     845                 :          0 : ntb_dev_configure(const struct rte_rawdev *dev, rte_rawdev_obj_t config,
     846                 :            :                 size_t config_size)
     847                 :            : {
     848                 :            :         struct ntb_dev_config *conf = config;
     849                 :          0 :         struct ntb_hw *hw = dev->dev_private;
     850                 :            :         uint32_t xstats_num;
     851                 :            :         int ret;
     852                 :            : 
     853         [ #  # ]:          0 :         if (conf == NULL || config_size != sizeof(*conf))
     854                 :            :                 return -EINVAL;
     855                 :            : 
     856                 :          0 :         hw->queue_pairs      = conf->num_queues;
     857                 :          0 :         hw->queue_size = conf->queue_size;
     858                 :          0 :         hw->used_mw_num = conf->mz_num;
     859                 :          0 :         hw->mz = conf->mz_list;
     860                 :          0 :         hw->rx_queues = rte_zmalloc("ntb_rx_queues",
     861                 :          0 :                         sizeof(struct ntb_rx_queue *) * hw->queue_pairs, 0);
     862                 :          0 :         hw->tx_queues = rte_zmalloc("ntb_tx_queues",
     863                 :          0 :                         sizeof(struct ntb_tx_queue *) * hw->queue_pairs, 0);
     864                 :            :         /* First total stats, then per queue stats. */
     865                 :          0 :         xstats_num = (hw->queue_pairs + 1) * NTB_XSTATS_NUM;
     866                 :          0 :         hw->ntb_xstats = rte_zmalloc("ntb_xstats", xstats_num *
     867                 :            :                                      sizeof(uint64_t), 0);
     868                 :          0 :         hw->ntb_xstats_off = rte_zmalloc("ntb_xstats_off", xstats_num *
     869                 :            :                                          sizeof(uint64_t), 0);
     870                 :            : 
     871                 :            :         /* Start handshake with the peer. */
     872                 :          0 :         ret = (*hw->ntb_ops->dev_handshake)(dev);
     873         [ #  # ]:          0 :         if (ret < 0) {
     874                 :          0 :                 rte_free(hw->rx_queues);
     875                 :          0 :                 rte_free(hw->tx_queues);
     876                 :          0 :                 hw->rx_queues = NULL;
     877                 :          0 :                 hw->tx_queues = NULL;
     878                 :          0 :                 return ret;
     879                 :            :         }
     880                 :            : 
     881                 :            :         return 0;
     882                 :            : }
     883                 :            : 
     884                 :            : static int
     885                 :          0 : ntb_dev_start(struct rte_rawdev *dev)
     886                 :            : {
     887                 :          0 :         struct ntb_hw *hw = dev->dev_private;
     888                 :            :         uint32_t i;
     889                 :            :         int ret;
     890                 :            : 
     891   [ #  #  #  # ]:          0 :         if (!hw->link_status || !ntb_peer_dev_is_up(hw))
     892                 :          0 :                 return -EINVAL;
     893                 :            : 
     894                 :            :         /* Set total stats. */
     895         [ #  # ]:          0 :         for (i = 0; i < NTB_XSTATS_NUM; i++) {
     896                 :          0 :                 hw->ntb_xstats[i] = 0;
     897                 :          0 :                 hw->ntb_xstats_off[i] = 0;
     898                 :            :         }
     899                 :            : 
     900         [ #  # ]:          0 :         for (i = 0; i < hw->queue_pairs; i++) {
     901                 :          0 :                 ret = ntb_queue_init(dev, i);
     902         [ #  # ]:          0 :                 if (ret) {
     903                 :          0 :                         NTB_LOG(ERR, "Failed to init queue.");
     904                 :          0 :                         goto err_q_init;
     905                 :            :                 }
     906                 :            :         }
     907                 :            : 
     908                 :          0 :         hw->peer_mw_base = rte_zmalloc("ntb_peer_mw_base", hw->mw_cnt *
     909                 :            :                                         sizeof(uint64_t), 0);
     910         [ #  # ]:          0 :         if (hw->peer_mw_base == NULL) {
     911                 :          0 :                 NTB_LOG(ERR, "Cannot allocate memory for peer mw base.");
     912                 :            :                 ret = -ENOMEM;
     913                 :          0 :                 goto err_q_init;
     914                 :            :         }
     915                 :            : 
     916                 :            :         /* Read and validate the peer configuration. */
     917                 :          0 :         ret = (*hw->ntb_ops->read_peer_config)(dev);
     918         [ #  # ]:          0 :         if (ret < 0)
     919                 :          0 :                 goto err_up;
     920                 :            : 
     921                 :          0 :         dev->started = 1;
     922                 :            : 
     923                 :          0 :         return 0;
     924                 :            : 
     925                 :            : err_up:
     926                 :          0 :         rte_free(hw->peer_mw_base);
     927                 :          0 :         hw->peer_mw_base = NULL;
     928                 :          0 :         hw->peer_used_mws = 0;
     929                 :          0 : err_q_init:
     930         [ #  # ]:          0 :         for (i = 0; i < hw->queue_pairs; i++) {
     931                 :          0 :                 ntb_rxq_release_mbufs(hw->rx_queues[i]);
     932                 :          0 :                 ntb_txq_release_mbufs(hw->tx_queues[i]);
     933                 :            :         }
     934                 :            : 
     935                 :            :         return ret;
     936                 :            : }
     937                 :            : 
     938                 :            : static void
     939                 :          0 : ntb_dev_stop(struct rte_rawdev *dev)
     940                 :            : {
     941                 :          0 :         struct ntb_hw *hw = dev->dev_private;
     942                 :            :         uint32_t time_out;
     943                 :            :         int status, i;
     944                 :            : 
     945         [ #  # ]:          0 :         if (!ntb_peer_dev_is_up(hw))
     946                 :          0 :                 goto clean;
     947                 :            : 
     948                 :          0 :         (*hw->ntb_ops->link_cleanup)(dev);
     949                 :            : 
     950                 :            :         /* Notify the peer that device will be down. */
     951                 :          0 :         status = (*hw->ntb_ops->peer_db_set)(dev, 1);
     952         [ #  # ]:          0 :         if (status) {
     953                 :          0 :                 NTB_LOG(ERR, "Failed to tell peer device is down.");
     954                 :          0 :                 return;
     955                 :            :         }
     956                 :            : 
     957                 :            :         /*
     958                 :            :          * Set time out as 1s in case that the peer is stopped accidently
     959                 :            :          * without any notification.
     960                 :            :          */
     961                 :            :         time_out = 1000000;
     962                 :            : 
     963                 :            :         /* Wait for cleanup work down before db mask clear. */
     964   [ #  #  #  # ]:          0 :         while (ntb_peer_dev_is_up(hw) && time_out) {
     965                 :          0 :                 time_out -= 10;
     966                 :          0 :                 rte_delay_us(10);
     967                 :            :         }
     968                 :            : 
     969                 :          0 : clean:
     970                 :            :         /* Clear doorbells mask. */
     971                 :          0 :         status = (*hw->ntb_ops->db_set_mask)(dev,
     972                 :          0 :                                 (((uint64_t)1 << hw->db_cnt) - 1));
     973         [ #  # ]:          0 :         if (status)
     974                 :          0 :                 NTB_LOG(ERR, "Failed to clear doorbells.");
     975                 :            : 
     976         [ #  # ]:          0 :         for (i = 0; i < hw->queue_pairs; i++) {
     977                 :          0 :                 ntb_rxq_release_mbufs(hw->rx_queues[i]);
     978                 :          0 :                 ntb_txq_release_mbufs(hw->tx_queues[i]);
     979                 :            :         }
     980                 :            : 
     981                 :          0 :         rte_free(hw->peer_mw_base);
     982                 :          0 :         hw->peer_mw_base = NULL;
     983                 :          0 :         hw->peer_used_mws = 0;
     984                 :            : 
     985                 :          0 :         dev->started = 0;
     986                 :            : }
     987                 :            : 
     988                 :            : static int
     989                 :          0 : ntb_dev_close(struct rte_rawdev *dev)
     990                 :            : {
     991                 :          0 :         struct ntb_hw *hw = dev->dev_private;
     992                 :            :         struct rte_intr_handle *intr_handle;
     993                 :            :         int i;
     994                 :            : 
     995         [ #  # ]:          0 :         if (dev->started)
     996                 :          0 :                 ntb_dev_stop(dev);
     997                 :            : 
     998                 :            :         /* free queues */
     999         [ #  # ]:          0 :         for (i = 0; i < hw->queue_pairs; i++)
    1000                 :          0 :                 ntb_queue_release(dev, i);
    1001                 :          0 :         hw->queue_pairs = 0;
    1002                 :            : 
    1003                 :          0 :         intr_handle = hw->pci_dev->intr_handle;
    1004                 :            :         /* Disable interrupt only once */
    1005   [ #  #  #  # ]:          0 :         if (!rte_intr_nb_efd_get(intr_handle) &&
    1006                 :          0 :             !rte_intr_max_intr_get(intr_handle))
    1007                 :            :                 return 0;
    1008                 :            : 
    1009                 :          0 :         ntb_db_intr_unregister(dev);
    1010                 :            : 
    1011                 :            :         /* Clean datapath event and vec mapping */
    1012                 :          0 :         rte_intr_efd_disable(intr_handle);
    1013                 :          0 :         rte_intr_vec_list_free(intr_handle);
    1014                 :            :         /* Disable uio intr before callback unregister */
    1015                 :          0 :         rte_intr_disable(intr_handle);
    1016                 :            : 
    1017                 :            :         /* Unregister callback func to eal lib */
    1018                 :          0 :         rte_intr_callback_unregister(intr_handle,
    1019                 :            :                                      ntb_dev_intr_handler, dev);
    1020                 :            : 
    1021                 :          0 :         return 0;
    1022                 :            : }
    1023                 :            : 
    1024                 :            : static int
    1025                 :          0 : ntb_dev_reset(struct rte_rawdev *rawdev __rte_unused)
    1026                 :            : {
    1027                 :          0 :         return 0;
    1028                 :            : }
    1029                 :            : 
    1030                 :            : static int
    1031                 :          0 : ntb_attr_set(struct rte_rawdev *dev, const char *attr_name,
    1032                 :            :              uint64_t attr_value)
    1033                 :            : {
    1034                 :            :         struct ntb_hw *hw;
    1035                 :            :         int index;
    1036                 :            : 
    1037         [ #  # ]:          0 :         if (dev == NULL || attr_name == NULL) {
    1038                 :          0 :                 NTB_LOG(ERR, "Invalid arguments for setting attributes");
    1039                 :          0 :                 return -EINVAL;
    1040                 :            :         }
    1041                 :            : 
    1042                 :          0 :         hw = dev->dev_private;
    1043                 :            : 
    1044         [ #  # ]:          0 :         if (!strncmp(attr_name, NTB_SPAD_USER, NTB_SPAD_USER_LEN)) {
    1045                 :          0 :                 index = atoi(&attr_name[NTB_SPAD_USER_LEN]);
    1046         [ #  # ]:          0 :                 if (index < 0 || index >= NTB_SPAD_USER_MAX_NUM) {
    1047                 :          0 :                         NTB_LOG(ERR, "Invalid attribute (%s)", attr_name);
    1048                 :          0 :                         return -EINVAL;
    1049                 :            :                 }
    1050                 :          0 :                 (*hw->ntb_ops->spad_write)(dev, hw->spad_user_list[index],
    1051                 :            :                                            1, attr_value);
    1052                 :          0 :                 NTB_LOG(DEBUG, "Set attribute (%s) Value (%" PRIu64 ")",
    1053                 :            :                         attr_name, attr_value);
    1054                 :          0 :                 return 0;
    1055                 :            :         }
    1056                 :            : 
    1057         [ #  # ]:          0 :         if (!strncmp(attr_name, NTB_QUEUE_SZ_NAME, NTB_ATTR_NAME_LEN)) {
    1058                 :          0 :                 hw->queue_size = attr_value;
    1059                 :          0 :                 NTB_LOG(DEBUG, "Set attribute (%s) Value (%" PRIu64 ")",
    1060                 :            :                         attr_name, attr_value);
    1061                 :          0 :                 return 0;
    1062                 :            :         }
    1063                 :            : 
    1064         [ #  # ]:          0 :         if (!strncmp(attr_name, NTB_QUEUE_NUM_NAME, NTB_ATTR_NAME_LEN)) {
    1065                 :          0 :                 hw->queue_pairs = attr_value;
    1066                 :          0 :                 NTB_LOG(DEBUG, "Set attribute (%s) Value (%" PRIu64 ")",
    1067                 :            :                         attr_name, attr_value);
    1068                 :          0 :                 return 0;
    1069                 :            :         }
    1070                 :            : 
    1071                 :            :         /* Attribute not found. */
    1072                 :          0 :         NTB_LOG(ERR, "Attribute not found.");
    1073                 :          0 :         return -EINVAL;
    1074                 :            : }
    1075                 :            : 
    1076                 :            : static int
    1077                 :          0 : ntb_attr_get(struct rte_rawdev *dev, const char *attr_name,
    1078                 :            :              uint64_t *attr_value)
    1079                 :            : {
    1080                 :            :         struct ntb_hw *hw;
    1081                 :            :         int index;
    1082                 :            : 
    1083   [ #  #  #  # ]:          0 :         if (dev == NULL || attr_name == NULL || attr_value == NULL) {
    1084                 :          0 :                 NTB_LOG(ERR, "Invalid arguments for getting attributes");
    1085                 :          0 :                 return -EINVAL;
    1086                 :            :         }
    1087                 :            : 
    1088                 :          0 :         hw = dev->dev_private;
    1089                 :            : 
    1090         [ #  # ]:          0 :         if (!strncmp(attr_name, NTB_TOPO_NAME, NTB_ATTR_NAME_LEN)) {
    1091                 :          0 :                 *attr_value = hw->topo;
    1092                 :          0 :                 NTB_LOG(DEBUG, "Attribute (%s) Value (%" PRIu64 ")",
    1093                 :            :                         attr_name, *attr_value);
    1094                 :          0 :                 return 0;
    1095                 :            :         }
    1096                 :            : 
    1097         [ #  # ]:          0 :         if (!strncmp(attr_name, NTB_LINK_STATUS_NAME, NTB_ATTR_NAME_LEN)) {
    1098                 :            :                 /* hw->link_status only indicates hw link status. */
    1099   [ #  #  #  # ]:          0 :                 *attr_value = hw->link_status && ntb_peer_dev_is_up(hw);
    1100                 :          0 :                 NTB_LOG(DEBUG, "Attribute (%s) Value (%" PRIu64 ")",
    1101                 :            :                         attr_name, *attr_value);
    1102                 :          0 :                 return 0;
    1103                 :            :         }
    1104                 :            : 
    1105         [ #  # ]:          0 :         if (!strncmp(attr_name, NTB_SPEED_NAME, NTB_ATTR_NAME_LEN)) {
    1106                 :          0 :                 *attr_value = hw->link_speed;
    1107                 :          0 :                 NTB_LOG(DEBUG, "Attribute (%s) Value (%" PRIu64 ")",
    1108                 :            :                         attr_name, *attr_value);
    1109                 :          0 :                 return 0;
    1110                 :            :         }
    1111                 :            : 
    1112         [ #  # ]:          0 :         if (!strncmp(attr_name, NTB_WIDTH_NAME, NTB_ATTR_NAME_LEN)) {
    1113                 :          0 :                 *attr_value = hw->link_width;
    1114                 :          0 :                 NTB_LOG(DEBUG, "Attribute (%s) Value (%" PRIu64 ")",
    1115                 :            :                         attr_name, *attr_value);
    1116                 :          0 :                 return 0;
    1117                 :            :         }
    1118                 :            : 
    1119         [ #  # ]:          0 :         if (!strncmp(attr_name, NTB_MW_CNT_NAME, NTB_ATTR_NAME_LEN)) {
    1120                 :          0 :                 *attr_value = hw->mw_cnt;
    1121                 :          0 :                 NTB_LOG(DEBUG, "Attribute (%s) Value (%" PRIu64 ")",
    1122                 :            :                         attr_name, *attr_value);
    1123                 :          0 :                 return 0;
    1124                 :            :         }
    1125                 :            : 
    1126         [ #  # ]:          0 :         if (!strncmp(attr_name, NTB_DB_CNT_NAME, NTB_ATTR_NAME_LEN)) {
    1127                 :          0 :                 *attr_value = hw->db_cnt;
    1128                 :          0 :                 NTB_LOG(DEBUG, "Attribute (%s) Value (%" PRIu64 ")",
    1129                 :            :                         attr_name, *attr_value);
    1130                 :          0 :                 return 0;
    1131                 :            :         }
    1132                 :            : 
    1133         [ #  # ]:          0 :         if (!strncmp(attr_name, NTB_SPAD_CNT_NAME, NTB_ATTR_NAME_LEN)) {
    1134                 :          0 :                 *attr_value = hw->spad_cnt;
    1135                 :          0 :                 NTB_LOG(DEBUG, "Attribute (%s) Value (%" PRIu64 ")",
    1136                 :            :                         attr_name, *attr_value);
    1137                 :          0 :                 return 0;
    1138                 :            :         }
    1139                 :            : 
    1140         [ #  # ]:          0 :         if (!strncmp(attr_name, NTB_SPAD_USER, NTB_SPAD_USER_LEN)) {
    1141                 :          0 :                 index = atoi(&attr_name[NTB_SPAD_USER_LEN]);
    1142         [ #  # ]:          0 :                 if (index < 0 || index >= NTB_SPAD_USER_MAX_NUM) {
    1143                 :          0 :                         NTB_LOG(ERR, "Attribute (%s) out of range", attr_name);
    1144                 :          0 :                         return -EINVAL;
    1145                 :            :                 }
    1146                 :          0 :                 *attr_value = (*hw->ntb_ops->spad_read)(dev,
    1147                 :            :                                 hw->spad_user_list[index], 0);
    1148                 :          0 :                 NTB_LOG(DEBUG, "Attribute (%s) Value (%" PRIu64 ")",
    1149                 :            :                         attr_name, *attr_value);
    1150                 :          0 :                 return 0;
    1151                 :            :         }
    1152                 :            : 
    1153                 :            :         /* Attribute not found. */
    1154                 :          0 :         NTB_LOG(ERR, "Attribute not found.");
    1155                 :          0 :         return -EINVAL;
    1156                 :            : }
    1157                 :            : 
    1158                 :            : static inline uint64_t
    1159                 :            : ntb_stats_update(uint64_t offset, uint64_t stat)
    1160                 :            : {
    1161                 :          0 :         if (stat >= offset)
    1162                 :          0 :                 return (stat - offset);
    1163                 :            :         else
    1164                 :          0 :                 return (uint64_t)(((uint64_t)-1) - offset + stat + 1);
    1165                 :            : }
    1166                 :            : 
    1167                 :            : static int
    1168                 :          0 : ntb_xstats_get(const struct rte_rawdev *dev,
    1169                 :            :                const unsigned int ids[],
    1170                 :            :                uint64_t values[],
    1171                 :            :                unsigned int n)
    1172                 :            : {
    1173                 :          0 :         struct ntb_hw *hw = dev->dev_private;
    1174                 :            :         uint32_t i, j, off, xstats_num;
    1175                 :            : 
    1176                 :            :         /* Calculate total stats of all queues. */
    1177         [ #  # ]:          0 :         for (i = 0; i < NTB_XSTATS_NUM; i++) {
    1178                 :          0 :                 hw->ntb_xstats[i] = 0;
    1179         [ #  # ]:          0 :                 for (j = 0; j < hw->queue_pairs; j++) {
    1180                 :          0 :                         off = NTB_XSTATS_NUM * (j + 1) + i;
    1181                 :          0 :                         hw->ntb_xstats[i] +=
    1182                 :          0 :                         ntb_stats_update(hw->ntb_xstats_off[off],
    1183         [ #  # ]:          0 :                                          hw->ntb_xstats[off]);
    1184                 :            :                 }
    1185                 :            :         }
    1186                 :            : 
    1187                 :          0 :         xstats_num = NTB_XSTATS_NUM * (hw->queue_pairs + 1);
    1188   [ #  #  #  # ]:          0 :         for (i = 0; i < n && ids[i] < xstats_num; i++) {
    1189         [ #  # ]:          0 :                 if (ids[i] < NTB_XSTATS_NUM)
    1190                 :          0 :                         values[i] = hw->ntb_xstats[ids[i]];
    1191                 :            :                 else
    1192                 :          0 :                         values[i] =
    1193                 :          0 :                         ntb_stats_update(hw->ntb_xstats_off[ids[i]],
    1194         [ #  # ]:          0 :                                          hw->ntb_xstats[ids[i]]);
    1195                 :            :         }
    1196                 :            : 
    1197                 :          0 :         return i;
    1198                 :            : }
    1199                 :            : 
    1200                 :            : static int
    1201                 :          0 : ntb_xstats_get_names(const struct rte_rawdev *dev,
    1202                 :            :                      struct rte_rawdev_xstats_name *xstats_names,
    1203                 :            :                      unsigned int size)
    1204                 :            : {
    1205                 :          0 :         struct ntb_hw *hw = dev->dev_private;
    1206                 :            :         uint32_t xstats_num, i, j, off;
    1207                 :            : 
    1208                 :          0 :         xstats_num = NTB_XSTATS_NUM * (hw->queue_pairs + 1);
    1209         [ #  # ]:          0 :         if (xstats_names == NULL || size < xstats_num)
    1210                 :          0 :                 return xstats_num;
    1211                 :            : 
    1212                 :            :         /* Total stats names */
    1213                 :            :         memcpy(xstats_names, ntb_xstats_names, sizeof(ntb_xstats_names));
    1214                 :            : 
    1215                 :            :         /* Queue stats names */
    1216         [ #  # ]:          0 :         for (i = 0; i < hw->queue_pairs; i++) {
    1217         [ #  # ]:          0 :                 for (j = 0; j < NTB_XSTATS_NUM; j++) {
    1218                 :          0 :                         off = j + (i + 1) * NTB_XSTATS_NUM;
    1219                 :          0 :                         snprintf(xstats_names[off].name,
    1220                 :            :                                 sizeof(xstats_names[0].name),
    1221                 :          0 :                                 "%s_q%u", ntb_xstats_names[j].name, i);
    1222                 :            :                 }
    1223                 :            :         }
    1224                 :            : 
    1225                 :          0 :         return xstats_num;
    1226                 :            : }
    1227                 :            : 
    1228                 :            : static uint64_t
    1229                 :          0 : ntb_xstats_get_by_name(const struct rte_rawdev *dev,
    1230                 :            :                        const char *name, unsigned int *id)
    1231                 :            : {
    1232                 :            :         struct rte_rawdev_xstats_name *xstats_names;
    1233                 :          0 :         struct ntb_hw *hw = dev->dev_private;
    1234                 :            :         uint32_t xstats_num, i, j, off;
    1235                 :            : 
    1236         [ #  # ]:          0 :         if (name == NULL)
    1237                 :            :                 return -EINVAL;
    1238                 :            : 
    1239                 :          0 :         xstats_num = NTB_XSTATS_NUM * (hw->queue_pairs + 1);
    1240                 :          0 :         xstats_names = rte_zmalloc("ntb_stats_name",
    1241                 :            :                                    sizeof(struct rte_rawdev_xstats_name) *
    1242                 :            :                                    xstats_num, 0);
    1243                 :          0 :         ntb_xstats_get_names(dev, xstats_names, xstats_num);
    1244                 :            : 
    1245                 :            :         /* Calculate total stats of all queues. */
    1246         [ #  # ]:          0 :         for (i = 0; i < NTB_XSTATS_NUM; i++) {
    1247         [ #  # ]:          0 :                 for (j = 0; j < hw->queue_pairs; j++) {
    1248                 :          0 :                         off = NTB_XSTATS_NUM * (j + 1) + i;
    1249                 :          0 :                         hw->ntb_xstats[i] +=
    1250                 :          0 :                         ntb_stats_update(hw->ntb_xstats_off[off],
    1251         [ #  # ]:          0 :                                          hw->ntb_xstats[off]);
    1252                 :            :                 }
    1253                 :            :         }
    1254                 :            : 
    1255         [ #  # ]:          0 :         for (i = 0; i < xstats_num; i++) {
    1256         [ #  # ]:          0 :                 if (!strncmp(name, xstats_names[i].name,
    1257                 :            :                     RTE_RAW_DEV_XSTATS_NAME_SIZE)) {
    1258                 :          0 :                         *id = i;
    1259                 :          0 :                         rte_free(xstats_names);
    1260         [ #  # ]:          0 :                         if (i < NTB_XSTATS_NUM)
    1261                 :          0 :                                 return hw->ntb_xstats[i];
    1262                 :            :                         else
    1263                 :          0 :                                 return ntb_stats_update(hw->ntb_xstats_off[i],
    1264         [ #  # ]:          0 :                                                         hw->ntb_xstats[i]);
    1265                 :            :                 }
    1266                 :            :         }
    1267                 :            : 
    1268                 :          0 :         NTB_LOG(ERR, "Cannot find the xstats name.");
    1269                 :            : 
    1270                 :          0 :         return -EINVAL;
    1271                 :            : }
    1272                 :            : 
    1273                 :            : static int
    1274                 :          0 : ntb_xstats_reset(struct rte_rawdev *dev,
    1275                 :            :                  const uint32_t ids[],
    1276                 :            :                  uint32_t nb_ids)
    1277                 :            : {
    1278                 :          0 :         struct ntb_hw *hw = dev->dev_private;
    1279                 :            :         uint32_t i, j, off, xstats_num;
    1280                 :            : 
    1281                 :          0 :         xstats_num = NTB_XSTATS_NUM * (hw->queue_pairs + 1);
    1282   [ #  #  #  # ]:          0 :         for (i = 0; i < nb_ids && ids[i] < xstats_num; i++) {
    1283         [ #  # ]:          0 :                 if (ids[i] < NTB_XSTATS_NUM) {
    1284         [ #  # ]:          0 :                         for (j = 0; j < hw->queue_pairs; j++) {
    1285                 :          0 :                                 off = NTB_XSTATS_NUM * (j + 1) + ids[i];
    1286                 :          0 :                                 hw->ntb_xstats_off[off] = hw->ntb_xstats[off];
    1287                 :            :                         }
    1288                 :            :                 } else {
    1289                 :          0 :                         hw->ntb_xstats_off[ids[i]] = hw->ntb_xstats[ids[i]];
    1290                 :            :                 }
    1291                 :            :         }
    1292                 :            : 
    1293                 :          0 :         return i;
    1294                 :            : }
    1295                 :            : 
    1296                 :            : static const struct rte_rawdev_ops ntb_ops = {
    1297                 :            :         .dev_info_get         = ntb_dev_info_get,
    1298                 :            :         .dev_configure        = ntb_dev_configure,
    1299                 :            :         .dev_start            = ntb_dev_start,
    1300                 :            :         .dev_stop             = ntb_dev_stop,
    1301                 :            :         .dev_close            = ntb_dev_close,
    1302                 :            :         .dev_reset            = ntb_dev_reset,
    1303                 :            : 
    1304                 :            :         .queue_def_conf       = ntb_queue_conf_get,
    1305                 :            :         .queue_setup          = ntb_queue_setup,
    1306                 :            :         .queue_release        = ntb_queue_release,
    1307                 :            :         .queue_count          = ntb_queue_count,
    1308                 :            : 
    1309                 :            :         .enqueue_bufs         = ntb_enqueue_bufs,
    1310                 :            :         .dequeue_bufs         = ntb_dequeue_bufs,
    1311                 :            : 
    1312                 :            :         .attr_get             = ntb_attr_get,
    1313                 :            :         .attr_set             = ntb_attr_set,
    1314                 :            : 
    1315                 :            :         .xstats_get           = ntb_xstats_get,
    1316                 :            :         .xstats_get_names     = ntb_xstats_get_names,
    1317                 :            :         .xstats_get_by_name   = ntb_xstats_get_by_name,
    1318                 :            :         .xstats_reset         = ntb_xstats_reset,
    1319                 :            : };
    1320                 :            : 
    1321                 :            : /* Every vendor ops entry is dereferenced unconditionally in the common code,
    1322                 :            :  * so enforce their presence once at probe time instead of guarding every call site at runtime.
    1323                 :            :  */
    1324                 :            : static int
    1325                 :          0 : ntb_vendor_ops_check(const struct ntb_hw *hw)
    1326                 :            : {
    1327                 :            : #define NTB_OPS_REQ(member) do { \
    1328                 :            :         if (hw->ntb_ops->member == NULL) { \
    1329                 :            :                 NTB_LOG(ERR, "Vendor ops lacks mandatory ." #member); \
    1330                 :            :                 return -EINVAL; \
    1331                 :            :         } \
    1332                 :            : } while (0)
    1333         [ #  # ]:          0 :         NTB_OPS_REQ(ntb_dev_init);
    1334         [ #  # ]:          0 :         NTB_OPS_REQ(get_peer_mw_addr);
    1335         [ #  # ]:          0 :         NTB_OPS_REQ(ioremap);
    1336         [ #  # ]:          0 :         NTB_OPS_REQ(get_link_status);
    1337         [ #  # ]:          0 :         NTB_OPS_REQ(set_link);
    1338         [ #  # ]:          0 :         NTB_OPS_REQ(spad_read);
    1339         [ #  # ]:          0 :         NTB_OPS_REQ(spad_write);
    1340         [ #  # ]:          0 :         NTB_OPS_REQ(db_read);
    1341         [ #  # ]:          0 :         NTB_OPS_REQ(db_clear);
    1342         [ #  # ]:          0 :         NTB_OPS_REQ(db_set_mask);
    1343         [ #  # ]:          0 :         NTB_OPS_REQ(peer_db_set);
    1344         [ #  # ]:          0 :         NTB_OPS_REQ(vector_bind);
    1345         [ #  # ]:          0 :         NTB_OPS_REQ(dev_handshake);
    1346         [ #  # ]:          0 :         NTB_OPS_REQ(peer_mw_validate);
    1347         [ #  # ]:          0 :         NTB_OPS_REQ(read_peer_config);
    1348         [ #  # ]:          0 :         NTB_OPS_REQ(link_cleanup);
    1349         [ #  # ]:          0 :         NTB_OPS_REQ(mem_align);
    1350                 :            : #undef NTB_OPS_REQ
    1351                 :            : 
    1352                 :            :         return 0;
    1353                 :            : }
    1354                 :            : 
    1355                 :            : static int
    1356                 :          0 : ntb_init_hw(struct rte_rawdev *dev, struct rte_pci_device *pci_dev)
    1357                 :            : {
    1358                 :          0 :         struct ntb_hw *hw = dev->dev_private;
    1359                 :            :         struct rte_intr_handle *intr_handle;
    1360                 :            :         int ret, i;
    1361                 :            : 
    1362                 :          0 :         hw->pci_dev = pci_dev;
    1363                 :          0 :         rte_atomic_store_explicit(&hw->peer_dev_up, 0, rte_memory_order_release);
    1364                 :          0 :         hw->link_status = NTB_LINK_DOWN;
    1365                 :          0 :         hw->link_speed = NTB_SPEED_NONE;
    1366                 :          0 :         hw->link_width = NTB_WIDTH_NONE;
    1367                 :            : 
    1368      [ #  #  # ]:          0 :         switch (pci_dev->id.device_id) {
    1369                 :          0 :         case NTB_AMD_DEV_ID_PRI:
    1370                 :            :         case NTB_AMD_DEV_ID_SEC:
    1371                 :          0 :                 hw->ntb_ops = &amd_ntb_ops;
    1372                 :          0 :                 break;
    1373                 :          0 :         case NTB_INTEL_DEV_ID_B2B_SKX:
    1374                 :            :         case NTB_INTEL_DEV_ID_B2B_ICX:
    1375                 :          0 :                 hw->ntb_ops = &intel_ntb_ops;
    1376                 :          0 :                 break;
    1377                 :          0 :         default:
    1378                 :          0 :                 NTB_LOG(ERR, "Not supported device.");
    1379                 :          0 :                 return -EINVAL;
    1380                 :            :         }
    1381                 :            : 
    1382                 :          0 :         ret = ntb_vendor_ops_check(hw);
    1383         [ #  # ]:          0 :         if (ret)
    1384                 :            :                 return ret;
    1385                 :            : 
    1386                 :          0 :         ret = (*hw->ntb_ops->ntb_dev_init)(dev);
    1387         [ #  # ]:          0 :         if (ret) {
    1388                 :          0 :                 NTB_LOG(ERR, "Unable to init ntb dev.");
    1389                 :          0 :                 return ret;
    1390                 :            :         }
    1391                 :            : 
    1392                 :          0 :         ret = (*hw->ntb_ops->set_link)(dev, 1);
    1393         [ #  # ]:          0 :         if (ret)
    1394                 :            :                 return ret;
    1395                 :            : 
    1396                 :            :         /* Init doorbell. */
    1397                 :          0 :         hw->db_valid_mask = RTE_LEN2MASK(hw->db_cnt, uint64_t);
    1398                 :            :         /* Clear all valid doorbell bits before registering intr handler */
    1399                 :          0 :         (*hw->ntb_ops->db_clear)(dev, hw->db_valid_mask);
    1400                 :            : 
    1401                 :          0 :         intr_handle = pci_dev->intr_handle;
    1402                 :            :         /* Register callback func to eal lib */
    1403                 :          0 :         rte_intr_callback_register(intr_handle,
    1404                 :            :                                    ntb_dev_intr_handler, dev);
    1405                 :            : 
    1406                 :          0 :         ret = rte_intr_efd_enable(intr_handle, hw->db_cnt);
    1407         [ #  # ]:          0 :         if (ret)
    1408                 :            :                 return ret;
    1409                 :            : 
    1410                 :            :         /* To clarify, the interrupt for each doorbell is already mapped
    1411                 :            :          * by default for intel gen3. They are mapped to msix vec 1-32,
    1412                 :            :          * and hardware intr is mapped to 0. Map all to 0 for uio.
    1413                 :            :          */
    1414         [ #  # ]:          0 :         if (rte_intr_cap_multiple(intr_handle)) {
    1415                 :          0 :                 ret = ntb_db_intr_register(dev, intr_handle);
    1416         [ #  # ]:          0 :                 if (ret)
    1417                 :            :                         return ret;
    1418                 :            :         } else {
    1419         [ #  # ]:          0 :                 for (i = 0; i < hw->db_cnt; i++) {
    1420                 :          0 :                         ret = (*hw->ntb_ops->vector_bind)(dev, i, 0);
    1421         [ #  # ]:          0 :                         if (ret)
    1422                 :          0 :                                 return ret;
    1423                 :            :                 }
    1424                 :            :         }
    1425                 :            : 
    1426                 :          0 :         hw->db_mask = 0;
    1427                 :          0 :         ret = (*hw->ntb_ops->db_set_mask)(dev, hw->db_mask);
    1428         [ #  # ]:          0 :         if (ret) {
    1429                 :          0 :                 NTB_LOG(ERR, "Unable to enable intr for all dbs.");
    1430                 :          0 :                 return ret;
    1431                 :            :         }
    1432                 :            : 
    1433                 :            :         /* enable uio intr after callback register */
    1434                 :          0 :         rte_intr_enable(intr_handle);
    1435                 :            : 
    1436                 :          0 :         return ret;
    1437                 :            : }
    1438                 :            : 
    1439                 :            : static int
    1440                 :          0 : ntb_create(struct rte_pci_device *pci_dev, int socket_id)
    1441                 :            : {
    1442                 :            :         char name[RTE_RAWDEV_NAME_MAX_LEN];
    1443                 :            :         struct rte_rawdev *rawdev = NULL;
    1444                 :            :         int ret;
    1445                 :            : 
    1446         [ #  # ]:          0 :         if (pci_dev == NULL) {
    1447                 :          0 :                 NTB_LOG(ERR, "Invalid pci_dev.");
    1448                 :          0 :                 return -EINVAL;
    1449                 :            :         }
    1450                 :            : 
    1451                 :            :         memset(name, 0, sizeof(name));
    1452                 :          0 :         snprintf(name, RTE_RAWDEV_NAME_MAX_LEN, "NTB:%x:%02x.%x",
    1453                 :          0 :                  pci_dev->addr.bus, pci_dev->addr.devid,
    1454                 :          0 :                  pci_dev->addr.function);
    1455                 :            : 
    1456                 :          0 :         NTB_LOG(INFO, "Init %s on NUMA node %d", name, socket_id);
    1457                 :            : 
    1458                 :            :         /* Allocate device structure. */
    1459                 :          0 :         rawdev = rte_rawdev_pmd_allocate(name, sizeof(struct ntb_hw),
    1460                 :            :                                          socket_id);
    1461         [ #  # ]:          0 :         if (rawdev == NULL) {
    1462                 :          0 :                 NTB_LOG(ERR, "Unable to allocate rawdev.");
    1463                 :          0 :                 return -EINVAL;
    1464                 :            :         }
    1465                 :            : 
    1466                 :          0 :         rawdev->dev_ops = &ntb_ops;
    1467                 :          0 :         rawdev->device = &pci_dev->device;
    1468                 :          0 :         rawdev->driver_name = pci_dev->device.driver->name;
    1469                 :            : 
    1470                 :          0 :         ret = ntb_init_hw(rawdev, pci_dev);
    1471         [ #  # ]:          0 :         if (ret < 0) {
    1472                 :          0 :                 NTB_LOG(ERR, "Unable to init ntb hw.");
    1473                 :          0 :                 goto fail;
    1474                 :            :         }
    1475                 :            : 
    1476                 :            :         return ret;
    1477                 :            : 
    1478                 :            : fail:
    1479                 :            :         if (rawdev != NULL)
    1480                 :          0 :                 rte_rawdev_pmd_release(rawdev);
    1481                 :            : 
    1482                 :          0 :         return ret;
    1483                 :            : }
    1484                 :            : 
    1485                 :            : static int
    1486                 :          0 : ntb_destroy(struct rte_pci_device *pci_dev)
    1487                 :            : {
    1488                 :            :         char name[RTE_RAWDEV_NAME_MAX_LEN];
    1489                 :            :         struct rte_rawdev *rawdev;
    1490                 :            :         int ret;
    1491                 :            : 
    1492         [ #  # ]:          0 :         if (pci_dev == NULL) {
    1493                 :          0 :                 NTB_LOG(ERR, "Invalid pci_dev.");
    1494                 :            :                 ret = -EINVAL;
    1495                 :          0 :                 return ret;
    1496                 :            :         }
    1497                 :            : 
    1498                 :            :         memset(name, 0, sizeof(name));
    1499                 :          0 :         snprintf(name, RTE_RAWDEV_NAME_MAX_LEN, "NTB:%x:%02x.%x",
    1500                 :          0 :                  pci_dev->addr.bus, pci_dev->addr.devid,
    1501                 :          0 :                  pci_dev->addr.function);
    1502                 :            : 
    1503                 :          0 :         NTB_LOG(INFO, "Closing %s on NUMA node %d", name, rte_socket_id());
    1504                 :            : 
    1505                 :          0 :         rawdev = rte_rawdev_pmd_get_named_dev(name);
    1506         [ #  # ]:          0 :         if (rawdev == NULL) {
    1507                 :          0 :                 NTB_LOG(ERR, "Invalid device name (%s)", name);
    1508                 :            :                 ret = -EINVAL;
    1509                 :          0 :                 return ret;
    1510                 :            :         }
    1511                 :            : 
    1512                 :          0 :         ret = rte_rawdev_pmd_release(rawdev);
    1513         [ #  # ]:          0 :         if (ret)
    1514                 :          0 :                 NTB_LOG(ERR, "Failed to destroy ntb rawdev.");
    1515                 :            : 
    1516                 :            :         return ret;
    1517                 :            : }
    1518                 :            : 
    1519                 :            : static int
    1520                 :          0 : ntb_probe(struct rte_pci_driver *pci_drv __rte_unused,
    1521                 :            :         struct rte_pci_device *pci_dev)
    1522                 :            : {
    1523                 :          0 :         return ntb_create(pci_dev, rte_socket_id());
    1524                 :            : }
    1525                 :            : 
    1526                 :            : static int
    1527                 :          0 : ntb_remove(struct rte_pci_device *pci_dev)
    1528                 :            : {
    1529                 :          0 :         return ntb_destroy(pci_dev);
    1530                 :            : }
    1531                 :            : 
    1532                 :            : 
    1533                 :            : static struct rte_pci_driver rte_ntb_pmd = {
    1534                 :            :         .id_table = pci_id_ntb_map,
    1535                 :            :         .drv_flags = RTE_PCI_DRV_NEED_MAPPING | RTE_PCI_DRV_WC_ACTIVATE,
    1536                 :            :         .probe = ntb_probe,
    1537                 :            :         .remove = ntb_remove,
    1538                 :            : };
    1539                 :            : 
    1540                 :        306 : RTE_PMD_REGISTER_PCI(raw_ntb, rte_ntb_pmd);
    1541                 :            : RTE_PMD_REGISTER_PCI_TABLE(raw_ntb, pci_id_ntb_map);
    1542                 :            : RTE_PMD_REGISTER_KMOD_DEP(raw_ntb, "* igb_uio | uio_pci_generic | vfio-pci");
    1543         [ -  + ]:        306 : RTE_LOG_REGISTER_DEFAULT(ntb_logtype, INFO);

Generated by: LCOV version 1.14