LCOV - code coverage report
Current view: top level - drivers/raw/ntb - ntb_hw_amd.c (source / functions) Hit Total Coverage
Test: Code coverage Lines: 0 237 0.0 %
Date: 2026-12-01 19:01:22 Functions: 0 21 0.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 0 136 0.0 %

           Branch data     Line data    Source code
       1                 :            : /* SPDX-License-Identifier: BSD-3-Clause
       2                 :            :  * Copyright(c) 2026 Advanced Micro Devices, Inc.
       3                 :            :  */
       4                 :            : 
       5                 :            : #include <stdint.h>
       6                 :            : #include <stdio.h>
       7                 :            : #include <stdlib.h>
       8                 :            : #include <string.h>
       9                 :            : #include <errno.h>
      10                 :            : #include <fcntl.h>
      11                 :            : #include <unistd.h>
      12                 :            : #include <limits.h>
      13                 :            : 
      14                 :            : #include <rte_common.h>
      15                 :            : #include <rte_io.h>
      16                 :            : #include <rte_eal.h>
      17                 :            : #include <rte_pci.h>
      18                 :            : #include <bus_pci_driver.h>
      19                 :            : #include <rte_rawdev.h>
      20                 :            : #include <rte_rawdev_pmd.h>
      21                 :            : #include <rte_malloc.h>
      22                 :            : #include <rte_memzone.h>
      23                 :            : 
      24                 :            : #include "ntb.h"
      25                 :            : #include "ntb_hw_amd.h"
      26                 :            : 
      27                 :            : static enum amd_ntb_bar amd_ntb_bar[] = {
      28                 :            :         AMD_NTB_BAR23,
      29                 :            :         AMD_NTB_BAR45,
      30                 :            : };
      31                 :            : 
      32                 :            : static int
      33                 :          0 : amd_ntb_dev_init(const struct rte_rawdev *dev)
      34                 :            : {
      35                 :          0 :         struct ntb_hw *ntb = dev->dev_private;
      36                 :            :         uint32_t sideinfo;
      37                 :            :         int i, bar;
      38                 :            : 
      39         [ #  # ]:          0 :         if (ntb == NULL) {
      40                 :          0 :                 NTB_LOG(ERR, "Invalid device.");
      41                 :          0 :                 return -EINVAL;
      42                 :            :         }
      43                 :            : 
      44                 :          0 :         ntb->hw_addr = (char *)ntb->pci_dev->mem_resource[0].addr;
      45                 :            : 
      46                 :            :         /* Bit 0 of the link status register carries the topology. */
      47                 :            :         sideinfo = rte_read32((char *)ntb->hw_addr + AMD_SIDEINFO_OFFSET);
      48         [ #  # ]:          0 :         if (sideinfo & AMD_SIDE_MASK)
      49                 :          0 :                 ntb->topo = NTB_TOPO_SEC;
      50                 :            :         else
      51                 :          0 :                 ntb->topo = NTB_TOPO_PRI;
      52                 :            : 
      53         [ #  # ]:          0 :         NTB_LOG(INFO, "Device topology: %s (sideinfo 0x%" PRIx32 ")",
      54                 :            :                 ntb->topo == NTB_TOPO_SEC ? "secondary" : "primary", sideinfo);
      55                 :            : 
      56                 :            :         RTE_BUILD_BUG_ON(AMD_MW_COUNT > NTB_MAX_MW);
      57                 :          0 :         ntb->mw_cnt = AMD_MW_COUNT;
      58                 :          0 :         ntb->db_cnt = AMD_DB_COUNT;
      59                 :            :         /* The 16-register scratchpad bank is split into two halves, so only
      60                 :            :          * AMD_SPAD_PER_SIDE registers are usable per side.
      61                 :            :          */
      62                 :          0 :         ntb->spad_cnt = AMD_SPAD_PER_SIDE;
      63                 :            : 
      64         [ #  # ]:          0 :         for (i = 0; i < ntb->mw_cnt; i++) {
      65                 :          0 :                 bar = amd_ntb_bar[i];
      66                 :          0 :                 ntb->mw_size[i] = ntb->pci_dev->mem_resource[bar].len;
      67                 :          0 :                 NTB_LOG(INFO, "mw[%d] bar%d size 0x%" PRIx64, i, bar, ntb->mw_size[i]);
      68                 :            :         }
      69                 :            : 
      70                 :            :         /* Reserve the last 2 scratchpad registers for application use. */
      71         [ #  # ]:          0 :         for (i = 0; i < NTB_SPAD_USER_MAX_NUM; i++)
      72                 :          0 :                 ntb->spad_user_list[i] = ntb->spad_cnt;
      73                 :          0 :         ntb->spad_user_list[0] = ntb->spad_cnt - 2;
      74                 :          0 :         ntb->spad_user_list[1] = ntb->spad_cnt - 1;
      75                 :            : 
      76                 :          0 :         return 0;
      77                 :            : }
      78                 :            : 
      79                 :            : static void *
      80                 :          0 : amd_ntb_get_peer_mw_addr(const struct rte_rawdev *dev, int mw_idx)
      81                 :            : {
      82                 :          0 :         struct ntb_hw *ntb = dev->dev_private;
      83                 :            : 
      84   [ #  #  #  # ]:          0 :         if (mw_idx < 0 || mw_idx >= ntb->mw_cnt) {
      85                 :          0 :                 NTB_LOG(ERR, "Invalid memory window index (0 - %u).", ntb->mw_cnt - 1);
      86                 :          0 :                 return NULL;
      87                 :            :         }
      88                 :            : 
      89                 :          0 :         return ntb->pci_dev->mem_resource[amd_ntb_bar[mw_idx]].addr;
      90                 :            : }
      91                 :            : 
      92                 :            : static int
      93                 :          0 : amd_ntb_mw_set_trans(const struct rte_rawdev *dev, int mw_idx, uint64_t addr, uint64_t size)
      94                 :            : {
      95                 :          0 :         struct ntb_hw *ntb = dev->dev_private;
      96                 :          0 :         void *peer_mmio = AMD_PEER_MMIO(ntb);
      97                 :            :         uint32_t xlat_off, limit_off;
      98                 :            :         uint64_t mw_size, reg_val;
      99                 :            :         uint8_t bar;
     100                 :            : 
     101   [ #  #  #  # ]:          0 :         if (mw_idx < 0 || mw_idx >= ntb->mw_cnt) {
     102                 :          0 :                 NTB_LOG(ERR, "Invalid memory window index (0 - %u).", ntb->mw_cnt - 1);
     103                 :          0 :                 return -EINVAL;
     104                 :            :         }
     105                 :            : 
     106                 :          0 :         bar = amd_ntb_bar[mw_idx];
     107                 :          0 :         mw_size = ntb->pci_dev->mem_resource[bar].len;
     108         [ #  # ]:          0 :         if (size > mw_size) {
     109                 :          0 :                 NTB_LOG(ERR, "Set translation size 0x%" PRIx64 " exceeds mw "
     110                 :            :                         "size 0x%" PRIx64, size, mw_size);
     111                 :          0 :                 return -EINVAL;
     112                 :            :         }
     113                 :            : 
     114                 :            :         /* Outbound translation forms the target as (base | offset), so a
     115                 :            :          * misaligned base aliases writes (see mem_align op). Reject it early.
     116                 :            :          */
     117         [ #  # ]:          0 :         if (addr & (rte_align64pow2(size) - 1)) {
     118                 :          0 :                 NTB_LOG(ERR, "mw%d translation base 0x%" PRIx64 " is not "
     119                 :            :                         "aligned to a power of two >= size 0x%" PRIx64
     120                 :            :                         "; window writes would alias.", mw_idx, addr, size);
     121                 :          0 :                 return -EINVAL;
     122                 :            :         }
     123                 :            : 
     124                 :            :         /* Program the peer's outbound translation window (S* registers) so
     125                 :            :          * that peer writes into its BAR land in our local memory at 'addr'.
     126                 :            :          */
     127         [ #  # ]:          0 :         if (mw_idx == 0) {
     128                 :            :                 xlat_off = AMD_BAR23_XLAT_OFFSET;
     129                 :            :                 limit_off = AMD_BAR23_LIMIT_OFFSET;
     130                 :            :         } else {
     131                 :            :                 xlat_off = AMD_BAR45_XLAT_OFFSET;
     132                 :            :                 limit_off = AMD_BAR45_LIMIT_OFFSET;
     133                 :            :         }
     134                 :            : 
     135                 :          0 :         rte_write64(addr, (char *)peer_mmio + xlat_off);
     136                 :            :         reg_val = rte_read64((char *)peer_mmio + xlat_off);
     137         [ #  # ]:          0 :         if (reg_val != addr) {
     138                 :          0 :                 NTB_LOG(ERR, "Failed to set mw%d translation.", mw_idx);
     139                 :            :                 rte_write64(0, (char *)peer_mmio + xlat_off);
     140                 :          0 :                 return -EIO;
     141                 :            :         }
     142                 :            : 
     143                 :          0 :         rte_write64(size, (char *)peer_mmio + limit_off);
     144                 :            :         reg_val = rte_read64((char *)peer_mmio + limit_off);
     145         [ #  # ]:          0 :         if (reg_val != size) {
     146                 :          0 :                 NTB_LOG(ERR, "Failed to set mw%d limit.", mw_idx);
     147                 :            :                 rte_write64(0, (char *)peer_mmio + xlat_off);
     148                 :            :                 rte_write64(0, (char *)peer_mmio + limit_off);
     149                 :          0 :                 return -EIO;
     150                 :            :         }
     151                 :            : 
     152                 :            :         return 0;
     153                 :            : }
     154                 :            : 
     155                 :            : static void *
     156                 :          0 : amd_ntb_ioremap(const struct rte_rawdev *dev, uint64_t addr)
     157                 :            : {
     158                 :          0 :         struct ntb_hw *ntb = dev->dev_private;
     159                 :            :         void *mapped = NULL;
     160                 :            :         void *base;
     161                 :            :         int i;
     162                 :            : 
     163         [ #  # ]:          0 :         for (i = 0; i < ntb->peer_used_mws; i++) {
     164         [ #  # ]:          0 :                 if (addr >= ntb->peer_mw_base[i] &&
     165         [ #  # ]:          0 :                                 addr < ntb->peer_mw_base[i] + ntb->mw_size[i]) {
     166                 :          0 :                         base = amd_ntb_get_peer_mw_addr(dev, i);
     167                 :          0 :                         mapped = (void *)(size_t)(addr - ntb->peer_mw_base[i] + (size_t)base);
     168                 :          0 :                         break;
     169                 :            :                 }
     170                 :            :         }
     171                 :            : 
     172                 :          0 :         return mapped;
     173                 :            : }
     174                 :            : 
     175                 :            : /* Read link speed/width from the device's PCIe capability link status. */
     176                 :            : static int
     177                 :          0 : amd_ntb_read_pcie_link_status(struct ntb_hw *ntb, uint16_t *link_status)
     178                 :            : {
     179                 :          0 :         struct rte_pci_device *pci_dev = ntb->pci_dev;
     180                 :            :         uint8_t pos, cap_id, next;
     181                 :            :         uint16_t status;
     182                 :            :         int ret;
     183                 :            : 
     184                 :          0 :         ret = rte_pci_read_config(pci_dev, &status, sizeof(status), RTE_PCI_STATUS);
     185         [ #  # ]:          0 :         if (ret != sizeof(status))
     186                 :            :                 return -EIO;
     187         [ #  # ]:          0 :         if (!(status & RTE_PCI_STATUS_CAP_LIST))
     188                 :            :                 return -ENOTSUP;
     189                 :            : 
     190                 :          0 :         ret = rte_pci_read_config(pci_dev, &pos, sizeof(pos), RTE_PCI_CAPABILITY_LIST);
     191         [ #  # ]:          0 :         if (ret != sizeof(pos))
     192                 :            :                 return -EIO;
     193                 :            : 
     194         [ #  # ]:          0 :         while (pos) {
     195                 :          0 :                 ret = rte_pci_read_config(pci_dev, &cap_id, sizeof(cap_id), pos);
     196         [ #  # ]:          0 :                 if (ret != sizeof(cap_id))
     197                 :            :                         return -EIO;
     198                 :          0 :                 ret = rte_pci_read_config(pci_dev, &next, sizeof(next), pos + 1);
     199         [ #  # ]:          0 :                 if (ret != sizeof(next))
     200                 :            :                         return -EIO;
     201         [ #  # ]:          0 :                 if (cap_id == RTE_PCI_CAP_ID_EXP) {
     202                 :          0 :                         ret = rte_pci_read_config(pci_dev, link_status, sizeof(*link_status),
     203                 :          0 :                                 pos + RTE_PCI_EXP_LNKSTA);
     204         [ #  # ]:          0 :                         if (ret != sizeof(*link_status))
     205                 :            :                                 return -EIO;
     206                 :          0 :                         return 0;
     207                 :            :                 }
     208                 :          0 :                 pos = next;
     209                 :            :         }
     210                 :            : 
     211                 :            :         return -ENOTSUP;
     212                 :            : }
     213                 :            : 
     214                 :            : /* Read the PCIe link status (LNKSTA) from an arbitrary device's config space
     215                 :            :  * exposed via sysfs. Used to query bridges that are not bound to this driver
     216                 :            :  * (e.g. the upstream switch above a secondary-side NTB), which rte_pci_*
     217                 :            :  * cannot access directly.
     218                 :            :  */
     219                 :            : static int
     220                 :          0 : amd_ntb_read_lnksta_sysfs(const char *config_path, uint16_t *link_status)
     221                 :            : {
     222                 :            :         uint8_t pos, cap_id, next;
     223                 :            :         uint16_t status;
     224                 :            :         int fd, ret = -EIO;
     225                 :            : 
     226                 :            :         fd = open(config_path, O_RDONLY);
     227         [ #  # ]:          0 :         if (fd < 0)
     228                 :          0 :                 return -errno;
     229                 :            : 
     230         [ #  # ]:          0 :         if (pread(fd, &status, sizeof(status), RTE_PCI_STATUS) != sizeof(status))
     231                 :          0 :                 goto out;
     232         [ #  # ]:          0 :         if (!(status & RTE_PCI_STATUS_CAP_LIST))
     233                 :          0 :                 goto out;
     234                 :            : 
     235         [ #  # ]:          0 :         if (pread(fd, &pos, sizeof(pos), RTE_PCI_CAPABILITY_LIST) != sizeof(pos))
     236                 :          0 :                 goto out;
     237                 :            : 
     238         [ #  # ]:          0 :         while (pos) {
     239         [ #  # ]:          0 :                 if (pread(fd, &cap_id, sizeof(cap_id), pos) != sizeof(cap_id))
     240                 :          0 :                         goto out;
     241         [ #  # ]:          0 :                 if (pread(fd, &next, sizeof(next), pos + 1) != sizeof(next))
     242                 :          0 :                         goto out;
     243         [ #  # ]:          0 :                 if (cap_id == RTE_PCI_CAP_ID_EXP) {
     244         [ #  # ]:          0 :                         if (pread(fd, link_status, sizeof(*link_status),
     245         [ #  # ]:          0 :                                         pos + RTE_PCI_EXP_LNKSTA) == sizeof(*link_status))
     246                 :            :                                 ret = 0;
     247                 :          0 :                         goto out;
     248                 :            :                 }
     249                 :          0 :                 pos = next;
     250                 :            :         }
     251                 :          0 : out:
     252                 :          0 :         close(fd);
     253                 :          0 :         return ret;
     254                 :            : }
     255                 :            : 
     256                 :            : /* On the secondary side the NTB device's own PCIe link status does not reflect
     257                 :            :  * the inter-host link. Mirror the Linux amd_ntb behaviour by walking up two
     258                 :            :  * bridge levels (device -> downstream switch -> upstream switch) and reading
     259                 :            :  * the upstream switch port's link status instead.
     260                 :            :  */
     261                 :            : static int
     262                 :          0 : amd_ntb_read_upstream_link_status(struct ntb_hw *ntb, uint16_t *link_status)
     263                 :            : {
     264                 :            :         char path[PATH_MAX];
     265                 :            :         char real[PATH_MAX];
     266                 :            :         char *p;
     267                 :            :         int i;
     268                 :            : 
     269                 :          0 :         snprintf(path, sizeof(path), "/sys/bus/pci/devices/%04x:%02x:%02x.%x",
     270                 :          0 :                 ntb->pci_dev->addr.domain, ntb->pci_dev->addr.bus,
     271                 :          0 :                 ntb->pci_dev->addr.devid, ntb->pci_dev->addr.function);
     272                 :            : 
     273         [ #  # ]:          0 :         if (realpath(path, real) == NULL)
     274                 :          0 :                 return -errno;
     275                 :            : 
     276                 :            :         /* Strip two trailing path components to reach the upstream switch. */
     277         [ #  # ]:          0 :         for (i = 0; i < 2; i++) {
     278                 :          0 :                 p = strrchr(real, '/');
     279   [ #  #  #  # ]:          0 :                 if (p == NULL || p == real)
     280                 :            :                         return -ENOENT;
     281                 :          0 :                 *p = '\0';
     282                 :            :         }
     283                 :            : 
     284                 :            :         snprintf(path, sizeof(path), "%s/config", real);
     285                 :          0 :         return amd_ntb_read_lnksta_sysfs(path, link_status);
     286                 :            : }
     287                 :            : 
     288                 :            : static int
     289                 :          0 : amd_ntb_get_link_status(const struct rte_rawdev *dev)
     290                 :            : {
     291                 :          0 :         struct ntb_hw *ntb = dev->dev_private;
     292                 :          0 :         uint16_t link_status = 0;
     293                 :            :         uint32_t sideinfo;
     294                 :            :         int ret;
     295                 :            : 
     296                 :            :         /* The link is usable once the peer has set its SIDE_READY bit. */
     297                 :          0 :         sideinfo = rte_read32((char *)AMD_PEER_MMIO(ntb) + AMD_SIDEINFO_OFFSET);
     298                 :          0 :         ntb->link_status = !!(sideinfo & AMD_SIDE_READY);
     299                 :            : 
     300         [ #  # ]:          0 :         if (!ntb->link_status) {
     301                 :          0 :                 ntb->link_speed = NTB_SPEED_NONE;
     302                 :          0 :                 ntb->link_width = NTB_WIDTH_NONE;
     303                 :          0 :                 return 0;
     304                 :            :         }
     305                 :            : 
     306                 :            :         /* The primary reads its own PCIe link status; the secondary must read
     307                 :            :          * the upstream switch port above it. If the upstream read fails, fall
     308                 :            :          * back to the local device so speed/width is still best-effort.
     309                 :            :          */
     310         [ #  # ]:          0 :         if (ntb->topo == NTB_TOPO_SEC) {
     311                 :          0 :                 ret = amd_ntb_read_upstream_link_status(ntb, &link_status);
     312         [ #  # ]:          0 :                 if (ret != 0)
     313                 :          0 :                         ret = amd_ntb_read_pcie_link_status(ntb, &link_status);
     314                 :            :         } else {
     315                 :          0 :                 ret = amd_ntb_read_pcie_link_status(ntb, &link_status);
     316                 :            :         }
     317                 :            : 
     318         [ #  # ]:          0 :         if (ret == 0) {
     319                 :          0 :                 ntb->link_speed = AMD_LNK_STA_SPEED(link_status);
     320                 :          0 :                 ntb->link_width = AMD_LNK_STA_WIDTH(link_status);
     321                 :            :         } else {
     322                 :          0 :                 NTB_LOG(WARNING, "Failed to read PCIe link status (%d).", ret);
     323                 :          0 :                 ntb->link_speed = NTB_SPEED_NONE;
     324                 :          0 :                 ntb->link_width = NTB_WIDTH_NONE;
     325                 :            :         }
     326                 :            : 
     327                 :            :         return 0;
     328                 :            : }
     329                 :            : 
     330                 :            : static int
     331                 :          0 : amd_ntb_set_link(const struct rte_rawdev *dev, bool up)
     332                 :            : {
     333                 :          0 :         struct ntb_hw *ntb = dev->dev_private;
     334                 :          0 :         void *mmio = ntb->hw_addr;
     335                 :            :         uint32_t reg;
     336                 :            : 
     337                 :            :         reg = rte_read32((char *)mmio + AMD_SIDEINFO_OFFSET);
     338         [ #  # ]:          0 :         if (up) {
     339         [ #  # ]:          0 :                 if (!(reg & AMD_SIDE_READY)) {
     340                 :          0 :                         reg |= AMD_SIDE_READY;
     341                 :            :                         rte_write32(reg, (char *)mmio + AMD_SIDEINFO_OFFSET);
     342                 :            :                 }
     343                 :            :                 reg = rte_read32((char *)mmio + AMD_CNTL_OFFSET);
     344                 :          0 :                 reg |= (AMD_PMM_REG_CTL | AMD_SMM_REG_CTL);
     345                 :            :                 rte_write32(reg, (char *)mmio + AMD_CNTL_OFFSET);
     346                 :            :         } else {
     347         [ #  # ]:          0 :                 if (reg & AMD_SIDE_READY) {
     348                 :          0 :                         reg &= ~AMD_SIDE_READY;
     349                 :            :                         rte_write32(reg, (char *)mmio + AMD_SIDEINFO_OFFSET);
     350                 :            :                 }
     351                 :            :                 reg = rte_read32((char *)mmio + AMD_CNTL_OFFSET);
     352                 :          0 :                 reg &= ~(AMD_PMM_REG_CTL | AMD_SMM_REG_CTL);
     353                 :            :                 rte_write32(reg, (char *)mmio + AMD_CNTL_OFFSET);
     354                 :            :         }
     355                 :            : 
     356                 :          0 :         return 0;
     357                 :            : }
     358                 :            : 
     359                 :            : static uint32_t
     360                 :          0 : amd_ntb_spad_read(const struct rte_rawdev *dev, int spad, bool peer)
     361                 :            : {
     362                 :          0 :         struct ntb_hw *ntb = dev->dev_private;
     363                 :            :         uint32_t offset;
     364                 :            : 
     365   [ #  #  #  # ]:          0 :         if (spad < 0 || spad >= ntb->spad_cnt) {
     366                 :          0 :                 NTB_LOG(ERR, "Invalid scratchpad index.");
     367                 :          0 :                 return 0;
     368                 :            :         }
     369                 :            : 
     370         [ #  # ]:          0 :         offset = AMD_SPAD_BASE(ntb, peer);
     371                 :            : 
     372                 :          0 :         return rte_read32((char *)ntb->hw_addr + AMD_SPAD_OFFSET + offset + (spad << 2));
     373                 :            : }
     374                 :            : 
     375                 :            : static int
     376                 :          0 : amd_ntb_spad_write(const struct rte_rawdev *dev, int spad, bool peer, uint32_t spad_v)
     377                 :            : {
     378                 :          0 :         struct ntb_hw *ntb = dev->dev_private;
     379                 :            :         uint32_t offset;
     380                 :            : 
     381   [ #  #  #  # ]:          0 :         if (spad < 0 || spad >= ntb->spad_cnt) {
     382                 :          0 :                 NTB_LOG(ERR, "Invalid scratchpad index.");
     383                 :          0 :                 return -EINVAL;
     384                 :            :         }
     385                 :            : 
     386         [ #  # ]:          0 :         offset = AMD_SPAD_BASE(ntb, peer);
     387                 :            : 
     388                 :          0 :         rte_write32(spad_v, (char *)ntb->hw_addr + AMD_SPAD_OFFSET + offset + (spad << 2));
     389                 :            : 
     390                 :          0 :         return 0;
     391                 :            : }
     392                 :            : 
     393                 :            : static uint64_t
     394                 :          0 : amd_ntb_db_read(const struct rte_rawdev *dev)
     395                 :            : {
     396                 :          0 :         struct ntb_hw *ntb = dev->dev_private;
     397                 :            : 
     398                 :          0 :         return (uint64_t)rte_read16((char *)ntb->hw_addr + AMD_DBSTAT_OFFSET);
     399                 :            : }
     400                 :            : 
     401                 :            : static int
     402                 :          0 : amd_ntb_db_clear(const struct rte_rawdev *dev, uint64_t db_bits)
     403                 :            : {
     404                 :          0 :         struct ntb_hw *ntb = dev->dev_private;
     405                 :            : 
     406                 :          0 :         rte_write16((uint16_t)db_bits, (char *)ntb->hw_addr + AMD_DBSTAT_OFFSET);
     407                 :            : 
     408                 :          0 :         return 0;
     409                 :            : }
     410                 :            : 
     411                 :            : static int
     412                 :          0 : amd_ntb_db_set_mask(const struct rte_rawdev *dev, uint64_t db_mask)
     413                 :            : {
     414                 :          0 :         struct ntb_hw *ntb = dev->dev_private;
     415                 :            : 
     416         [ #  # ]:          0 :         if (db_mask & ~ntb->db_valid_mask)
     417                 :            :                 return -EINVAL;
     418                 :            : 
     419                 :          0 :         ntb->db_mask |= db_mask;
     420                 :          0 :         rte_write16((uint16_t)ntb->db_mask, (char *)ntb->hw_addr + AMD_DBMASK_OFFSET);
     421                 :            : 
     422                 :          0 :         return 0;
     423                 :            : }
     424                 :            : 
     425                 :            : static int
     426                 :          0 : amd_ntb_peer_db_set(const struct rte_rawdev *dev, uint8_t db_idx)
     427                 :            : {
     428                 :          0 :         struct ntb_hw *ntb = dev->dev_private;
     429                 :            : 
     430         [ #  # ]:          0 :         if (((uint64_t)1 << db_idx) & ~ntb->db_valid_mask) {
     431                 :          0 :                 NTB_LOG(ERR, "Invalid doorbell.");
     432                 :          0 :                 return -EINVAL;
     433                 :            :         }
     434                 :            : 
     435                 :          0 :         rte_write16((uint16_t)1 << db_idx, (char *)ntb->hw_addr + AMD_DBREQ_OFFSET);
     436                 :            : 
     437                 :          0 :         return 0;
     438                 :            : }
     439                 :            : 
     440                 :            : static int
     441                 :          0 : amd_ntb_vector_bind(const struct rte_rawdev *dev __rte_unused, uint8_t intr __rte_unused,
     442                 :            :                 uint8_t msix __rte_unused)
     443                 :            : {
     444                 :            :         /* Each doorbell maps to its MSI-X vector by default. */
     445                 :          0 :         return 0;
     446                 :            : }
     447                 :            : 
     448                 :            : /* Handshake: advertises the local configuration to the peer using the
     449                 :            :  * packed 8-register scratchpad layout, programs the peer's outbound
     450                 :            :  * translation windows and rings doorbell 0 to signal readiness.
     451                 :            :  */
     452                 :            : static int
     453                 :          0 : amd_ntb_dev_handshake(const struct rte_rawdev *dev)
     454                 :            : {
     455                 :          0 :         struct ntb_hw *ntb = dev->dev_private;
     456                 :            :         uint32_t info;
     457                 :            :         uint64_t base;
     458                 :            :         int i, ret;
     459                 :            : 
     460                 :          0 :         info = (ntb->mw_cnt & 0xff) | ((uint32_t)(ntb->queue_pairs & 0xff) << 8) |
     461                 :          0 :                 ((uint32_t)(ntb->used_mw_num & 0xff) << 16);
     462                 :          0 :         ret = amd_ntb_spad_write(dev, AMD_SPAD_CNT_INFO, 1, info);
     463         [ #  # ]:          0 :         if (ret < 0)
     464                 :            :                 return ret;
     465                 :            : 
     466                 :          0 :         ret = amd_ntb_spad_write(dev, AMD_SPAD_QUEUE_SZ, 1, ntb->queue_size);
     467         [ #  # ]:          0 :         if (ret < 0)
     468                 :            :                 return ret;
     469                 :            : 
     470         [ #  # ]:          0 :         for (i = 0; i < ntb->used_mw_num; i++) {
     471                 :            :                 /* Advertise the memzone virtual base (used by ioremap on the
     472                 :            :                  * peer) and program the translation with the IOVA.
     473                 :            :                  */
     474                 :          0 :                 base = (uint64_t)(size_t)ntb->mz[i]->addr;
     475                 :          0 :                 ret = amd_ntb_spad_write(dev, AMD_SPAD_MW0_BA_L + 2 * i, 1, (uint32_t)base);
     476         [ #  # ]:          0 :                 if (ret < 0)
     477                 :          0 :                         return ret;
     478                 :          0 :                 ret = amd_ntb_spad_write(dev, AMD_SPAD_MW0_BA_H + 2 * i, 1,
     479                 :          0 :                         (uint32_t)(base >> 32));
     480         [ #  # ]:          0 :                 if (ret < 0)
     481                 :          0 :                         return ret;
     482                 :            :         }
     483                 :            : 
     484         [ #  # ]:          0 :         for (i = 0; i < ntb->used_mw_num; i++) {
     485                 :          0 :                 ret = amd_ntb_mw_set_trans(dev, i, ntb->mz[i]->iova, ntb->mz[i]->len);
     486         [ #  # ]:          0 :                 if (ret < 0)
     487                 :          0 :                         return ret;
     488                 :            :         }
     489                 :            : 
     490                 :            :         /* Ring doorbell 0 to tell the peer the device is ready. */
     491                 :          0 :         return amd_ntb_peer_db_set(dev, 0);
     492                 :            : }
     493                 :            : 
     494                 :            : /* Peer-config read at device start. Validates the peer's queue
     495                 :            :  * configuration and records the peer memory-window base addresses.
     496                 :            :  */
     497                 :            : static int
     498                 :          0 : amd_ntb_read_peer_config(const struct rte_rawdev *dev)
     499                 :            : {
     500                 :          0 :         struct ntb_hw *ntb = dev->dev_private;
     501                 :            :         uint32_t info, peer_qps, peer_qsz, peer_used_mws, lo, hi;
     502                 :            :         int i;
     503                 :            : 
     504                 :          0 :         info = amd_ntb_spad_read(dev, AMD_SPAD_CNT_INFO, 0);
     505                 :          0 :         peer_qps = (info >> 8) & 0xff;
     506         [ #  # ]:          0 :         if (peer_qps != ntb->queue_pairs) {
     507                 :          0 :                 NTB_LOG(ERR, "Inconsistent number of queues! (local: %u peer: %u)",
     508                 :            :                         ntb->queue_pairs, peer_qps);
     509                 :          0 :                 return -EINVAL;
     510                 :            :         }
     511                 :            : 
     512                 :          0 :         peer_qsz = amd_ntb_spad_read(dev, AMD_SPAD_QUEUE_SZ, 0);
     513         [ #  # ]:          0 :         if (peer_qsz != ntb->queue_size) {
     514                 :          0 :                 NTB_LOG(ERR, "Inconsistent queue size! (local: %u peer: %u)",
     515                 :            :                         ntb->queue_size, peer_qsz);
     516                 :          0 :                 return -EINVAL;
     517                 :            :         }
     518                 :            : 
     519                 :          0 :         peer_used_mws = (info >> 16) & 0xff;
     520         [ #  # ]:          0 :         if (peer_used_mws > ntb->mw_cnt) {
     521                 :          0 :                 NTB_LOG(ERR, "Peer used mws %u > mw cnt %u.", peer_used_mws, ntb->mw_cnt);
     522                 :          0 :                 return -EINVAL;
     523                 :            :         }
     524                 :          0 :         ntb->peer_used_mws = peer_used_mws;
     525                 :            : 
     526         [ #  # ]:          0 :         for (i = 0; i < ntb->peer_used_mws; i++) {
     527                 :          0 :                 lo = amd_ntb_spad_read(dev, AMD_SPAD_MW0_BA_L + 2 * i, 0);
     528                 :          0 :                 hi = amd_ntb_spad_read(dev, AMD_SPAD_MW0_BA_H + 2 * i, 0);
     529                 :          0 :                 ntb->peer_mw_base[i] = ((uint64_t)hi << 32) | lo;
     530                 :            :         }
     531                 :            : 
     532                 :            :         return 0;
     533                 :            : }
     534                 :            : 
     535                 :            : /* Check that the peer advertises the same number of memory windows. */
     536                 :            : static int
     537                 :          0 : amd_ntb_peer_mw_validate(const struct rte_rawdev *dev)
     538                 :            : {
     539                 :          0 :         struct ntb_hw *ntb = dev->dev_private;
     540                 :            :         uint32_t info, peer_mw_cnt;
     541                 :            : 
     542                 :          0 :         info = amd_ntb_spad_read(dev, AMD_SPAD_CNT_INFO, 0);
     543                 :          0 :         peer_mw_cnt = info & 0xff;
     544         [ #  # ]:          0 :         if (peer_mw_cnt != ntb->mw_cnt) {
     545                 :          0 :                 NTB_LOG(ERR, "Peer mw cnt %u != local mw cnt %u.", peer_mw_cnt, ntb->mw_cnt);
     546                 :          0 :                 return -EINVAL;
     547                 :            :         }
     548                 :            : 
     549                 :            :         return 0;
     550                 :            : }
     551                 :            : 
     552                 :            : /* Tear down the local scratchpad and translations so the peer can no
     553                 :            :  * longer reach local memory.
     554                 :            :  */
     555                 :            : static void
     556                 :          0 : amd_ntb_link_cleanup(const struct rte_rawdev *dev)
     557                 :            : {
     558                 :          0 :         struct ntb_hw *ntb = dev->dev_private;
     559                 :            :         int i;
     560                 :            : 
     561         [ #  # ]:          0 :         for (i = 0; i < ntb->spad_cnt; i++)
     562                 :          0 :                 amd_ntb_spad_write(dev, i, 0, 0);
     563                 :            : 
     564         [ #  # ]:          0 :         for (i = 0; i < ntb->used_mw_num; i++)
     565                 :          0 :                 amd_ntb_mw_set_trans(dev, i, 0, 0);
     566                 :          0 : }
     567                 :            : 
     568                 :            : static uint64_t
     569                 :          0 : amd_ntb_get_mem_align(const struct rte_rawdev *dev __rte_unused, uint32_t mw_id __rte_unused,
     570                 :            :                 uint64_t mw_len)
     571                 :            : {
     572                 :            :         /* Base must be a power of two >= the window length, and >= 4K. */
     573                 :          0 :         return RTE_MAX((uint64_t)RTE_PGSIZE_4K, rte_align64pow2(mw_len));
     574                 :            : }
     575                 :            : 
     576                 :            : const struct ntb_dev_ops amd_ntb_ops = {
     577                 :            :         .ntb_dev_init           = amd_ntb_dev_init,
     578                 :            :         .get_peer_mw_addr       = amd_ntb_get_peer_mw_addr,
     579                 :            :         .ioremap                = amd_ntb_ioremap,
     580                 :            :         .get_link_status        = amd_ntb_get_link_status,
     581                 :            :         .set_link               = amd_ntb_set_link,
     582                 :            :         .spad_read              = amd_ntb_spad_read,
     583                 :            :         .spad_write             = amd_ntb_spad_write,
     584                 :            :         .db_read                = amd_ntb_db_read,
     585                 :            :         .db_clear               = amd_ntb_db_clear,
     586                 :            :         .db_set_mask            = amd_ntb_db_set_mask,
     587                 :            :         .peer_db_set            = amd_ntb_peer_db_set,
     588                 :            :         .vector_bind            = amd_ntb_vector_bind,
     589                 :            :         .dev_handshake          = amd_ntb_dev_handshake,
     590                 :            :         .peer_mw_validate       = amd_ntb_peer_mw_validate,
     591                 :            :         .read_peer_config       = amd_ntb_read_peer_config,
     592                 :            :         .link_cleanup           = amd_ntb_link_cleanup,
     593                 :            :         .mem_align              = amd_ntb_get_mem_align,
     594                 :            : };

Generated by: LCOV version 1.14