Netdev List
 help / color / mirror / Atom feed
From: Frank Li <Frank.li@nxp.com>
To: Koichiro Den <den@valinux.co.jp>
Cc: dave.jiang@intel.com, cassel@kernel.org, mani@kernel.org,
	kwilczynski@kernel.org, kishon@kernel.org, bhelgaas@google.com,
	geert+renesas@glider.be, robh@kernel.org, vkoul@kernel.org,
	jdmason@kudzu.us, allenbh@gmail.com, jingoohan1@gmail.com,
	lpieralisi@kernel.org, linux-pci@vger.kernel.org,
	linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org,
	linux-renesas-soc@vger.kernel.org, devicetree@vger.kernel.org,
	dmaengine@vger.kernel.org, iommu@lists.linux.dev,
	ntb@lists.linux.dev, netdev@vger.kernel.org,
	linux-kselftest@vger.kernel.org, arnd@arndb.de,
	gregkh@linuxfoundation.org, joro@8bytes.org, will@kernel.org,
	robin.murphy@arm.com, magnus.damm@gmail.com, krzk+dt@kernel.org,
	conor+dt@kernel.org, corbet@lwn.net, skhan@linuxfoundation.org,
	andriy.shevchenko@linux.intel.com, jbrunet@baylibre.com,
	utkarsh02t@gmail.com
Subject: Re: [RFC PATCH v4 13/38] PCI: endpoint: pci-epf-vntb: Support BAR subrange mappings for MWs
Date: Mon, 19 Jan 2026 15:26:11 -0500	[thread overview]
Message-ID: <aW6TY7rVM6aTnfyO@lizhi-Precision-Tower-5810> (raw)
In-Reply-To: <20260118135440.1958279-14-den@valinux.co.jp>

On Sun, Jan 18, 2026 at 10:54:15PM +0900, Koichiro Den wrote:
> pci-epf-vntb can pack multiple memory windows into a single BAR using
> mwN_offset. With the NTB core gaining support for programming multiple
> translation ranges for a window, the EPF needs to provide the per-BAR
> subrange layout to the endpoint controller (EPC).
>
> Implement .mw_set_trans_ranges() for pci-epf-vntb. Track subranges for
> each BAR and pass them to pci_epc_set_bar() so EPC drivers can select an
> appropriate inbound mapping mode (e.g. Address Match mode on DesignWare
> controllers) when subrange mappings are required.
>
> Signed-off-by: Koichiro Den <den@valinux.co.jp>
> ---
>  drivers/pci/endpoint/functions/pci-epf-vntb.c | 183 +++++++++++++++++-
>  1 file changed, 175 insertions(+), 8 deletions(-)
>
> diff --git a/drivers/pci/endpoint/functions/pci-epf-vntb.c b/drivers/pci/endpoint/functions/pci-epf-vntb.c
> index 39e784e21236..98128c2c5079 100644
> --- a/drivers/pci/endpoint/functions/pci-epf-vntb.c
> +++ b/drivers/pci/endpoint/functions/pci-epf-vntb.c
> @@ -42,6 +42,7 @@
>  #include <linux/log2.h>
>  #include <linux/module.h>
>  #include <linux/slab.h>
> +#include <linux/sort.h>
>
>  #include <linux/pci-ep-msi.h>
>  #include <linux/pci-epc.h>
> @@ -144,6 +145,10 @@ struct epf_ntb {
>
>  	enum pci_barno epf_ntb_bar[VNTB_BAR_NUM];
>
> +	/* Cache for subrange mapping */
> +	struct ntb_mw_subrange *mw_subrange[MAX_MW];
> +	unsigned int num_subrange[MAX_MW];
> +
>  	struct epf_ntb_ctrl *reg;
>
>  	u32 *epf_db;
> @@ -736,6 +741,7 @@ static int epf_ntb_mw_bar_init(struct epf_ntb *ntb)
>  		ntb->epf->bar[barno].flags |= upper_32_bits(size) ?
>  				PCI_BASE_ADDRESS_MEM_TYPE_64 :
>  				PCI_BASE_ADDRESS_MEM_TYPE_32;
> +		ntb->epf->bar[barno].num_submap = 0;
>
>  		ret = pci_epc_set_bar(ntb->epf->epc,
>  				      ntb->epf->func_no,
> @@ -1405,28 +1411,188 @@ static int vntb_epf_db_set_mask(struct ntb_dev *ntb, u64 db_bits)
>  	return 0;
>  }
>
> -static int vntb_epf_mw_set_trans(struct ntb_dev *ndev, int pidx, int idx,
> -		dma_addr_t addr, resource_size_t size)
> +struct vntb_mw_order {
> +	u64 off;
> +	unsigned int mw;
> +};
> +
> +static int vntb_cmp_mw_order(const void *a, const void *b)
> +{
> +	const struct vntb_mw_order *ma = a;
> +	const struct vntb_mw_order *mb = b;
> +
> +	if (ma->off < mb->off)
> +		return -1;
> +	if (ma->off > mb->off)
> +		return 1;
> +	return 0;
> +}
> +
> +static int vntb_epf_mw_set_trans_ranges(struct ntb_dev *ndev, int pidx, int idx,
> +					unsigned int num_ranges,
> +					const struct ntb_mw_subrange *ranges)
>  {
>  	struct epf_ntb *ntb = ntb_ndev(ndev);
> +	struct pci_epf_bar_submap *submap;
> +	struct vntb_mw_order mws[MAX_MW];
>  	struct pci_epf_bar *epf_bar;
> +	struct ntb_mw_subrange *r;
>  	enum pci_barno barno;
> +	struct device *dev, *epf_dev;
> +	unsigned int total_ranges = 0;
> +	unsigned int mw_cnt = 0;
> +	unsigned int cur = 0;
> +	u64 expected_off = 0;
> +	unsigned int i, j;
>  	int ret;
> +
> +	dev = &ntb->ntb->dev;
> +	epf_dev = &ntb->epf->dev;
> +	barno = ntb->epf_ntb_bar[BAR_MW1 + idx];
> +	epf_bar = &ntb->epf->bar[barno];
> +	epf_bar->barno = barno;
> +
> +	r = devm_kmemdup(epf_dev, ranges, num_ranges * sizeof(*ranges), GFP_KERNEL);

size_mul(sizeof(*ranges), num_ranges)

> +	if (!r)
> +		return -ENOMEM;
> +
> +	if (ntb->mw_subrange[idx])
> +		devm_kfree(epf_dev, ntb->mw_subrange[idx]);
> +
> +	ntb->mw_subrange[idx] = r;
> +	ntb->num_subrange[idx] = num_ranges;
> +
> +	/* Defer pci_epc_set_bar() until all MWs in this BAR have range info. */
> +	for (i = 0; i < MAX_MW; i++) {
> +		enum pci_barno bar = ntb->epf_ntb_bar[BAR_MW1 + i];
> +
> +		if (bar != barno)
> +			continue;
> +		if (!ntb->num_subrange[i])
> +			return 0;
> +
> +		mws[mw_cnt].mw = i;
> +		mws[mw_cnt].off = ntb->mws_offset[i];
> +		mw_cnt++;
> +	}
> +
> +	sort(mws, mw_cnt, sizeof(mws[0]), vntb_cmp_mw_order, NULL);

Can we require mws_offset is ordered? So needn't sort here.

> +
> +	/* BAR submap must cover the whole BAR with no holes. */
> +	for (i = 0; i < mw_cnt; i++) {
> +		unsigned int mw = mws[i].mw;
> +		u64 sum = 0;
> +
> +		if (mws[i].off != expected_off) {

can we all use size instead 'off' to keep align with submap?

Frank
> +			dev_err(dev,
> +				"BAR%d: hole/overlap at %#llx (MW%d@%#llx)\n",
> +				barno, expected_off, mw + 1, mws[i].off);
> +			return -EINVAL;
> +		}
> +
> +		total_ranges += ntb->num_subrange[mw];
> +		for (j = 0; j < ntb->num_subrange[mw]; j++)
> +			sum += ntb->mw_subrange[mw][j].size;
> +
> +		if (sum != ntb->mws_size[mw]) {
> +			dev_err(dev,
> +				"MW%d: ranges size %#llx != window size %#llx\n",
> +				mw + 1, sum, ntb->mws_size[mw]);
> +			return -EINVAL;
> +		}
> +		expected_off += ntb->mws_size[mw];
> +	}
> +
> +	submap = devm_krealloc_array(epf_dev, epf_bar->submap, total_ranges,
> +				     sizeof(*submap), GFP_KERNEL);
> +	if (!submap)
> +		return -ENOMEM;
> +
> +	epf_bar->submap = submap;
> +	epf_bar->num_submap = total_ranges;
> +	dev_dbg(dev, "Requesting BAR%d layout (#. of subranges is %u):\n",
> +		barno, total_ranges);
> +
> +	for (i = 0; i < mw_cnt; i++) {
> +		unsigned int mw = mws[i].mw;
> +
> +		dev_dbg(dev, "- MW%d\n", 1 + mw);
> +		for (j = 0; j < ntb->num_subrange[mw]; j++) {
> +			dev_dbg(dev, "  - addr/size = %#llx/%#llx\n",
> +				ntb->mw_subrange[mw][j].addr,
> +				ntb->mw_subrange[mw][j].size);
> +			submap[cur].phys_addr = ntb->mw_subrange[mw][j].addr;
> +			submap[cur].size = ntb->mw_subrange[mw][j].size;
> +			cur++;
> +		}
> +	}
> +
> +	ret = pci_epc_set_bar(ntb->epf->epc, ntb->epf->func_no,
> +			      ntb->epf->vfunc_no, epf_bar);
> +	if (ret)
> +		dev_err(dev, "BAR%d: failed to program mappings for MW%d: %d\n",
> +			barno, idx + 1, ret);
> +
> +	return ret;
> +}
> +
> +static int vntb_epf_mw_set_trans(struct ntb_dev *ndev, int pidx, int idx,
> +				 dma_addr_t addr, resource_size_t size)
> +{
> +	struct epf_ntb *ntb = ntb_ndev(ndev);
> +	struct pci_epf_bar *epf_bar;
> +	resource_size_t bar_size;
> +	enum pci_barno barno;
>  	struct device *dev;
> +	unsigned int i;
> +	int ret;
>
>  	dev = &ntb->ntb->dev;
>  	barno = ntb->epf_ntb_bar[BAR_MW1 + idx];
>  	epf_bar = &ntb->epf->bar[barno];
>  	epf_bar->phys_addr = addr;
>  	epf_bar->barno = barno;
> -	epf_bar->size = size;
>
> -	ret = pci_epc_set_bar(ntb->epf->epc, 0, 0, epf_bar);
> -	if (ret) {
> -		dev_err(dev, "failure set mw trans\n");
> -		return ret;
> +	bar_size = epf_bar->size;
> +	if (!bar_size || !size)
> +		return -EINVAL;
> +
> +	if (size != ntb->mws_size[idx])
> +		return -EINVAL;
> +
> +	/*
> +	 * Even if the caller intends to map the entire MW, the MW might
> +	 * actually be just a part of the BAR. In that case, redirect the
> +	 * handling to vntb_epf_mw_set_trans_ranges().
> +	 */
> +	if (size < bar_size) {
> +		struct ntb_mw_subrange r = {
> +			.addr = addr,
> +			.size = size,
> +		};
> +		return vntb_epf_mw_set_trans_ranges(ndev, pidx, idx, 1, &r);
>  	}
> -	return 0;
> +
> +	/* Drop any stale cache for the BAR. */
> +	for (i = 0; i < MAX_MW; i++) {
> +		if (ntb->epf_ntb_bar[BAR_MW1 + i] != barno)
> +			continue;
> +		devm_kfree(&ntb->epf->dev, ntb->mw_subrange[i]);
> +		ntb->mw_subrange[i] = NULL;
> +		ntb->num_subrange[i] = 0;
> +	}
> +
> +	/* Not use subrange mapping. If it's used in the past, clear it off. */
> +	devm_kfree(&ntb->epf->dev, epf_bar->submap);
> +	epf_bar->submap = NULL;
> +	epf_bar->num_submap = 0;
> +
> +	ret = pci_epc_set_bar(ntb->epf->epc, ntb->epf->func_no,
> +			      ntb->epf->vfunc_no, epf_bar);
> +	if (ret)
> +		dev_err(dev, "failure set mw trans\n");
> +
> +	return ret;
>  }
>
>  static int vntb_epf_mw_clear_trans(struct ntb_dev *ntb, int pidx, int idx)
> @@ -1590,6 +1756,7 @@ static const struct ntb_dev_ops vntb_epf_ops = {
>  	.db_vector_mask		= vntb_epf_db_vector_mask,
>  	.db_set_mask		= vntb_epf_db_set_mask,
>  	.mw_set_trans		= vntb_epf_mw_set_trans,
> +	.mw_set_trans_ranges	= vntb_epf_mw_set_trans_ranges,
>  	.mw_clear_trans		= vntb_epf_mw_clear_trans,
>  	.peer_mw_get_addr	= vntb_epf_peer_mw_get_addr,
>  	.link_enable		= vntb_epf_link_enable,
> --
> 2.51.0
>

  reply	other threads:[~2026-01-19 20:26 UTC|newest]

Thread overview: 68+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-01-18 13:54 [RFC PATCH v4 00/38] NTB transport backed by PCI EP embedded DMA Koichiro Den
2026-01-18 13:54 ` [RFC PATCH v4 01/38] dmaengine: dw-edma: Export helper to get integrated register window Koichiro Den
2026-01-18 13:54 ` [RFC PATCH v4 02/38] dmaengine: dw-edma: Add per-channel interrupt routing control Koichiro Den
2026-01-18 17:03   ` Frank Li
2026-01-19 14:26     ` Koichiro Den
2026-01-21 16:02       ` Vinod Koul
2026-01-22  2:44         ` Koichiro Den
2026-01-23 15:44         ` Frank Li
2026-01-18 13:54 ` [RFC PATCH v4 03/38] dmaengine: dw-edma: Poll completion when local IRQ handling is disabled Koichiro Den
2026-01-18 13:54 ` [RFC PATCH v4 04/38] dmaengine: dw-edma: Add notify-only channels support Koichiro Den
2026-01-18 13:54 ` [RFC PATCH v4 05/38] dmaengine: dw-edma: Add a helper to query linked-list region Koichiro Den
2026-01-18 17:05   ` Frank Li
2026-01-21  1:38     ` Koichiro Den
2026-01-21  8:41       ` Koichiro Den
2026-01-21 15:24         ` Frank Li
2026-01-22  1:19           ` Koichiro Den
2026-01-22  1:54             ` Frank Li
2026-01-18 13:54 ` [RFC PATCH v4 06/38] NTB: epf: Add mwN_offset support and config region versioning Koichiro Den
2026-01-18 13:54 ` [RFC PATCH v4 07/38] NTB: epf: Reserve a subset of MSI vectors for non-NTB users Koichiro Den
2026-01-18 13:54 ` [RFC PATCH v4 08/38] NTB: epf: Provide db_vector_count/db_vector_mask callbacks Koichiro Den
2026-01-19 20:03   ` Frank Li
2026-01-21  1:41     ` Koichiro Den
2026-01-18 13:54 ` [RFC PATCH v4 09/38] NTB: core: Add mw_set_trans_ranges() for subrange programming Koichiro Den
2026-01-19 20:07   ` Frank Li
2026-01-18 13:54 ` [RFC PATCH v4 10/38] NTB: core: Add .get_private_data() to ntb_dev_ops Koichiro Den
2026-01-18 13:54 ` [RFC PATCH v4 11/38] NTB: core: Add .get_dma_dev() " Koichiro Den
2026-01-19 20:09   ` Frank Li
2026-01-21  1:44     ` Koichiro Den
2026-01-18 13:54 ` [RFC PATCH v4 12/38] NTB: core: Add driver_override support for NTB devices Koichiro Den
2026-01-18 13:54 ` [RFC PATCH v4 13/38] PCI: endpoint: pci-epf-vntb: Support BAR subrange mappings for MWs Koichiro Den
2026-01-19 20:26   ` Frank Li [this message]
2026-01-21  2:08     ` Koichiro Den
2026-01-18 13:54 ` [RFC PATCH v4 14/38] PCI: endpoint: pci-epf-vntb: Implement .get_private_data() callback Koichiro Den
2026-01-19 20:27   ` Frank Li
2026-01-18 13:54 ` [RFC PATCH v4 15/38] PCI: endpoint: pci-epf-vntb: Implement .get_dma_dev() Koichiro Den
2026-01-19 20:30   ` Frank Li
2026-01-22 14:58     ` Koichiro Den
2026-01-18 13:54 ` [RFC PATCH v4 16/38] NTB: ntb_transport: Move TX memory window setup into setup_qp_mw() Koichiro Den
2026-01-19 20:36   ` Frank Li
2026-01-21  2:15     ` Koichiro Den
2026-01-18 13:54 ` [RFC PATCH v4 17/38] NTB: ntb_transport: Dynamically determine qp count Koichiro Den
2026-01-18 13:54 ` [RFC PATCH v4 18/38] NTB: ntb_transport: Use ntb_get_dma_dev() Koichiro Den
2026-01-19 20:38   ` Frank Li
2026-01-18 13:54 ` [RFC PATCH v4 19/38] NTB: ntb_transport: Rename ntb_transport.c to ntb_transport_core.c Koichiro Den
2026-01-18 13:54 ` [RFC PATCH v4 20/38] NTB: ntb_transport: Move internal types to ntb_transport_internal.h Koichiro Den
2026-01-18 13:54 ` [RFC PATCH v4 21/38] NTB: ntb_transport: Export common helpers for modularization Koichiro Den
2026-01-18 13:54 ` [RFC PATCH v4 22/38] NTB: ntb_transport: Split core library and default NTB client Koichiro Den
2026-01-18 13:54 ` [RFC PATCH v4 23/38] NTB: ntb_transport: Add transport backend infrastructure Koichiro Den
2026-01-18 13:54 ` [RFC PATCH v4 24/38] NTB: ntb_transport: Run ntb_set_mw() before link-up negotiation Koichiro Den
2026-01-18 13:54 ` [RFC PATCH v4 25/38] NTB: hw: Add remote eDMA backend registry and DesignWare backend Koichiro Den
2026-01-18 13:54 ` [RFC PATCH v4 26/38] NTB: ntb_transport: Add remote embedded-DMA transport client Koichiro Den
2026-01-18 13:54 ` [RFC PATCH v4 27/38] ntb_netdev: Multi-queue support Koichiro Den
2026-01-18 13:54 ` [RFC PATCH v4 28/38] iommu: ipmmu-vmsa: Add PCIe ch0 to devices_allowlist Koichiro Den
2026-01-18 13:54 ` [RFC PATCH v4 29/38] iommu: ipmmu-vmsa: Add support for reserved regions Koichiro Den
2026-01-18 13:54 ` [RFC PATCH v4 30/38] arm64: dts: renesas: Add Spider RC/EP DTs for NTB with remote DW PCIe eDMA Koichiro Den
2026-01-18 13:54 ` [RFC PATCH v4 31/38] NTB: epf: Add per-SoC quirk to cap MRRS for DWC eDMA (128B for R-Car) Koichiro Den
2026-01-18 13:54 ` [RFC PATCH v4 32/38] NTB: epf: Add an additional memory window (MW2) barno mapping on Renesas R-Car Koichiro Den
2026-01-18 13:54 ` [RFC PATCH v4 33/38] Documentation: PCI: endpoint: pci-epf-vntb: Update and add mwN_offset usage Koichiro Den
2026-01-18 13:54 ` [RFC PATCH v4 34/38] Documentation: driver-api: ntb: Document remote embedded-DMA transport Koichiro Den
2026-01-18 13:54 ` [RFC PATCH v4 35/38] PCI: endpoint: pci-epf-test: Add pci_epf_test_next_free_bar() helper Koichiro Den
2026-01-18 13:54 ` [RFC PATCH v4 36/38] PCI: endpoint: pci-epf-test: Add remote eDMA-backed mode Koichiro Den
2026-01-19 20:47   ` Frank Li
2026-01-22 14:54     ` Koichiro Den
2026-01-18 13:54 ` [RFC PATCH v4 37/38] misc: pci_endpoint_test: Add remote eDMA transfer test mode Koichiro Den
2026-01-18 13:54 ` [RFC PATCH v4 38/38] selftests: pci_endpoint: Add remote eDMA transfer coverage Koichiro Den
2026-01-20 18:30 ` [RFC PATCH v4 00/38] NTB transport backed by PCI EP embedded DMA Dave Jiang
2026-01-20 18:47   ` Dave Jiang
2026-01-21  2:40     ` Koichiro Den

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=aW6TY7rVM6aTnfyO@lizhi-Precision-Tower-5810 \
    --to=frank.li@nxp.com \
    --cc=allenbh@gmail.com \
    --cc=andriy.shevchenko@linux.intel.com \
    --cc=arnd@arndb.de \
    --cc=bhelgaas@google.com \
    --cc=cassel@kernel.org \
    --cc=conor+dt@kernel.org \
    --cc=corbet@lwn.net \
    --cc=dave.jiang@intel.com \
    --cc=den@valinux.co.jp \
    --cc=devicetree@vger.kernel.org \
    --cc=dmaengine@vger.kernel.org \
    --cc=geert+renesas@glider.be \
    --cc=gregkh@linuxfoundation.org \
    --cc=iommu@lists.linux.dev \
    --cc=jbrunet@baylibre.com \
    --cc=jdmason@kudzu.us \
    --cc=jingoohan1@gmail.com \
    --cc=joro@8bytes.org \
    --cc=kishon@kernel.org \
    --cc=krzk+dt@kernel.org \
    --cc=kwilczynski@kernel.org \
    --cc=linux-doc@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=linux-pci@vger.kernel.org \
    --cc=linux-renesas-soc@vger.kernel.org \
    --cc=lpieralisi@kernel.org \
    --cc=magnus.damm@gmail.com \
    --cc=mani@kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=ntb@lists.linux.dev \
    --cc=robh@kernel.org \
    --cc=robin.murphy@arm.com \
    --cc=skhan@linuxfoundation.org \
    --cc=utkarsh02t@gmail.com \
    --cc=vkoul@kernel.org \
    --cc=will@kernel.org \
    /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