Netdev List
 help / color / mirror / Atom feed
From: Jason Gunthorpe <jgg@ziepe.ca>
To: Leon Romanovsky <leon@kernel.org>
Cc: Saeed Mahameed <saeedm@nvidia.com>,
	Tariq Toukan <tariqt@nvidia.com>, Mark Bloch <mbloch@nvidia.com>,
	Andrew Lunn <andrew+netdev@lunn.ch>,
	"David S. Miller" <davem@davemloft.net>,
	Eric Dumazet <edumazet@google.com>,
	Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>,
	linux-rdma@vger.kernel.org, linux-kernel@vger.kernel.org,
	netdev@vger.kernel.org
Subject: Re: [PATCH mlx5-next] net/mlx5: Consolidate UAR index to PFN helpers
Date: Tue, 19 May 2026 12:32:37 -0300	[thread overview]
Message-ID: <20260519153237.GN7702@ziepe.ca> (raw)
In-Reply-To: <20260519-mlx5-uar-index-v1-1-1027ae724bff@nvidia.com>

On Tue, May 19, 2026 at 06:08:18PM +0300, Leon Romanovsky wrote:
> From: Leon Romanovsky <leonro@nvidia.com>
> 
> mlx5_core's uar2pfn() and mlx5_ib's uar_index2pfn() compute the same
> value via slightly different idioms.  Given:
> 
>   MLX5_ADAPTER_PAGE_SHIFT = 12
>   MLX5_UARS_IN_PAGE       = PAGE_SIZE / MLX5_ADAPTER_PAGE_SIZE
>                           = 1 << (PAGE_SHIFT - 12)
> 
> when uar_4k is set, uar2pfn()'s "index >> (PAGE_SHIFT - 12)" reduces to
> "index / MLX5_UARS_IN_PAGE", which is exactly what uar_index2pfn() does.
> When uar_4k is clear, both fall through to the identity case.  The same
> arithmetic is also open-coded a third time in uar_index2paddress(), which
> just multiplies the result by PAGE_SIZE.
> 
> The duplication is historical: uar_index2pfn() landed with the original
> mlx5_ib driver in 2013 (e126ba97dba9), uar2pfn() was added in 2017
> (a6d51b68611e) when the bfreg allocator moved into mlx5_core, and no
> shared header ever exposed the helper.  The two were last touched in
> parallel by aa8106f137b9 ("net/mlx5: Add explicit bar address field"),
> confirming they are meant to behave identically.
> 
> Replace all three local copies with two static inlines in
> include/linux/mlx5/driver.h returning phys_addr_t, which is the
> appropriate type for a value that subsequently feeds ioremap*() and
> rdma_user_mmap_io().  No functional change.
> 
> Signed-off-by: Leon Romanovsky <leonro@nvidia.com>
> ---
> mlx5_core's uar2pfn() and mlx5_ib's uar_index2pfn() compute the same
> value via slightly different idioms. Let's consolidate them.
> ---
>  drivers/infiniband/hw/mlx5/main.c             | 25 ++-----------------------
>  drivers/net/ethernet/mellanox/mlx5/core/uar.c | 14 +-------------
>  include/linux/mlx5/driver.h                   | 16 ++++++++++++++++
>  3 files changed, 19 insertions(+), 36 deletions(-)
> 
> diff --git a/drivers/infiniband/hw/mlx5/main.c b/drivers/infiniband/hw/mlx5/main.c
> index 428811fa805b..e61db29bc166 100644
> --- a/drivers/infiniband/hw/mlx5/main.c
> +++ b/drivers/infiniband/hw/mlx5/main.c
> @@ -2373,27 +2373,6 @@ static void mlx5_ib_dealloc_ucontext(struct ib_ucontext *ibcontext)
>  	}
>  }
>  
> -static phys_addr_t uar_index2pfn(struct mlx5_ib_dev *dev,
> -				 int uar_idx)
> -{
> -	int fw_uars_per_page;
> -
> -	fw_uars_per_page = MLX5_CAP_GEN(dev->mdev, uar_4k) ? MLX5_UARS_IN_PAGE : 1;
> -
> -	return (dev->mdev->bar_addr >> PAGE_SHIFT) + uar_idx / fw_uars_per_page;
> -}
> -
> -static u64 uar_index2paddress(struct mlx5_ib_dev *dev,
> -				 int uar_idx)
> -{
> -	unsigned int fw_uars_per_page;
> -
> -	fw_uars_per_page = MLX5_CAP_GEN(dev->mdev, uar_4k) ?
> -				MLX5_UARS_IN_PAGE : 1;
> -
> -	return (dev->mdev->bar_addr + (uar_idx / fw_uars_per_page) * PAGE_SIZE);
> -}
> -
>  static int get_command(unsigned long offset)
>  {
>  	return (offset >> MLX5_IB_MMAP_CMD_SHIFT) & MLX5_IB_MMAP_CMD_MASK;
> @@ -2643,7 +2622,7 @@ static int uar_mmap(struct mlx5_ib_dev *dev, enum mlx5_ib_mmap_cmd cmd,
>  		uar_index = bfregi->sys_pages[idx];
>  	}
>  
> -	pfn = uar_index2pfn(dev, uar_index);
> +	pfn = mlx5_uar_index_to_pfn(dev->mdev, uar_index);
>  	mlx5_ib_dbg(dev, "uar idx 0x%lx, pfn %pa\n", idx, &pfn);
>
>  	err = rdma_user_mmap_io(&context->ibucontext, vma, pfn, PAGE_SIZE,
> @@ -4327,7 +4306,7 @@ alloc_uar_entry(struct mlx5_ib_ucontext *c,
>  		goto end;
>  
>  	entry->page_idx = uar_index;
> -	entry->address = uar_index2paddress(dev, uar_index);
> +	entry->address = mlx5_uar_index_to_paddr(dev->mdev, uar_index);
>  	if (alloc_type == MLX5_IB_UAPI_UAR_ALLOC_TYPE_BF)
>  		entry->mmap_flag = MLX5_IB_MMAP_TYPE_UAR_WC;
>  	else
> diff --git a/drivers/net/ethernet/mellanox/mlx5/core/uar.c b/drivers/net/ethernet/mellanox/mlx5/core/uar.c
> index 1513112ecec8..a85d8fed1546 100644
> --- a/drivers/net/ethernet/mellanox/mlx5/core/uar.c
> +++ b/drivers/net/ethernet/mellanox/mlx5/core/uar.c
> @@ -66,18 +66,6 @@ static int uars_per_sys_page(struct mlx5_core_dev *mdev)
>  	return 1;
>  }
>  
> -static u64 uar2pfn(struct mlx5_core_dev *mdev, u32 index)
> -{
> -	u32 system_page_index;
> -
> -	if (MLX5_CAP_GEN(mdev, uar_4k))
> -		system_page_index = index >> (PAGE_SHIFT - MLX5_ADAPTER_PAGE_SHIFT);
> -	else
> -		system_page_index = index;
> -
> -	return (mdev->bar_addr >> PAGE_SHIFT) + system_page_index;
> -}
> -
>  static void up_rel_func(struct kref *kref)
>  {
>  	struct mlx5_uars_page *up = container_of(kref, struct mlx5_uars_page, ref_count);
> @@ -132,7 +120,7 @@ static struct mlx5_uars_page *alloc_uars_page(struct mlx5_core_dev *mdev,
>  		goto error1;
>  	}
>  
> -	pfn = uar2pfn(mdev, up->index);
> +	pfn = mlx5_uar_index_to_pfn(mdev, up->index);
>  	if (map_wc) {
>  		up->map = ioremap_wc(pfn << PAGE_SHIFT, PAGE_SIZE);

The only places using PFN here immediately shift it, this should be
using mlx5_uar_index_to_paddr()


> +static inline phys_addr_t mlx5_uar_index_to_pfn(struct mlx5_core_dev *mdev,
> +						u32 uar_idx)
> +{
> +	u32 fw_uars_per_page = MLX5_CAP_GEN(mdev, uar_4k) ? MLX5_UARS_IN_PAGE : 1;
> +
> +	return (mdev->bar_addr >> PAGE_SHIFT) + uar_idx / fw_uars_per_page;
> +}
> +
> +static inline phys_addr_t mlx5_uar_index_to_paddr(struct mlx5_core_dev *mdev,
> +						  u32 uar_idx)
> +{
> +	u32 fw_uars_per_page = MLX5_CAP_GEN(mdev, uar_4k) ? MLX5_UARS_IN_PAGE : 1;
> +
> +	return mdev->bar_addr + (uar_idx / fw_uars_per_page) * PAGE_SIZE;
> +}

Then there is only one caller of the pfn version in uar_mmap which can
just be written using phys and open code the >> PAGE_SHIFT

No reason to duplicate this

Jason

      reply	other threads:[~2026-05-19 15:32 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-05-19 15:08 [PATCH mlx5-next] net/mlx5: Consolidate UAR index to PFN helpers Leon Romanovsky
2026-05-19 15:32 ` Jason Gunthorpe [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260519153237.GN7702@ziepe.ca \
    --to=jgg@ziepe.ca \
    --cc=andrew+netdev@lunn.ch \
    --cc=davem@davemloft.net \
    --cc=edumazet@google.com \
    --cc=kuba@kernel.org \
    --cc=leon@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-rdma@vger.kernel.org \
    --cc=mbloch@nvidia.com \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=saeedm@nvidia.com \
    --cc=tariqt@nvidia.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox