The Linux Kernel Mailing List
 help / color / mirror / Atom feed
* [PATCH v7 3/4] vfio/spapr_tce: Normalize EEH IOA error injection addresses
  2026-10-23  7:36 [PATCH v6 0/5] powerpc: Add RTAS based error injection support for pSeries Narayana Murty N
@ 2026-11-23  8:47 ` Narayana Murty N
  0 siblings, 0 replies; 6+ messages in thread
From: Narayana Murty N @ 2026-11-23  8:47 UTC (permalink / raw)
  To: Mahesh J Salgaonkar, Madhavan Srinivasan, Oliver O'Halloran,
	Timothy Pearson, Alex Williamson
  Cc: sourabhjain, Michael Ellerman, Nicholas Piggin, Christophe Leroy,
	Ritesh Harjani, Shrikanth Hegde, linuxppc-dev, linux-kernel, kvm,
	sbhat, harshpb

On pseries, RTAS error injection takes a PCI bus address for memory and
I/O load/store functions.  VFIO_EEH_PE_INJECT_ERR callers such as QEMU
pass an address within a BAR as the sysfs "resource" file exports it: a
CPU address for a memory BAR and a PCI bus address for an I/O BAR.  A
CPU address can differ from the PCI bus address because of PHB window
translation.

On pseries, convert the address before calling eeh_pe_inject_err() to the
PCI bus address of the matching standard BAR (BAR0-BAR5) of a device in
the IOMMU group, keeping its offset.  Memory functions match memory BARs
and I/O functions match I/O BARs.  Unassigned and disabled BARs are never
matched, and an address that matches no BAR fails with -EINVAL.

Config-space and DMA functions, requests with address and mask both zero,
and requests on other platforms are not changed.  The mask is never
modified.

Signed-off-by: Narayana Murty N <nnmlinux@linux.ibm.com>
---
 drivers/vfio/vfio_iommu_spapr_tce.c | 131 +++++++++++++++++++++++++++-
 1 file changed, 130 insertions(+), 1 deletion(-)

diff --git a/drivers/vfio/vfio_iommu_spapr_tce.c b/drivers/vfio/vfio_iommu_spapr_tce.c
index 1c0eec228cc6..c56b649d7094 100644
--- a/drivers/vfio/vfio_iommu_spapr_tce.c
+++ b/drivers/vfio/vfio_iommu_spapr_tce.c
@@ -23,9 +23,11 @@
 #include <linux/mm.h>
 #include "vfio.h"
 
+#include <asm/eeh.h>
 #include <asm/iommu.h>
 #include <asm/tce.h>
 #include <asm/mmu_context.h>
+#include <asm/machdep.h>
 
 #define DRIVER_VERSION  "0.1"
 #define DRIVER_AUTHOR   "aik@ozlabs.ru"
@@ -774,12 +776,116 @@ static long tce_iommu_create_default_window(struct tce_container *container)
 	return ret;
 }
 
+/*
+ * Look for @addr in the standard BARs (BAR0-BAR5) of @pdev whose resource
+ * type is @res_type, and translate it to the PCI bus address RTAS expects,
+ * keeping the offset into the BAR. Expansion ROM, SR-IOV resources and
+ * bridge windows are not supported.
+ *
+ * @addr is in the domain the sysfs "resource" file exports: the CPU address
+ * for a memory BAR and the PCI bus address for an I/O BAR.  Each type has a
+ * single domain, so a match is unambiguous.
+ *
+ * Unassigned BARs are zero-based and flagged IORESOURCE_UNSET on pseries,
+ * so they are skipped, as are disabled ones.
+ */
+static bool vfio_spapr_eeh_normalize_pdev_addr(struct pci_dev *pdev,
+					       unsigned long res_type,
+					       unsigned long addr,
+					       unsigned long *normalized)
+{
+	struct pci_bus_region region;
+	struct resource *res;
+	resource_size_t len, start;
+	int bar;
+
+	for (bar = 0; bar < PCI_STD_NUM_BARS; bar++) {
+		res = &pdev->resource[bar];
+
+		if (resource_type(res) != res_type)
+			continue;
+
+		if (res->flags & (IORESOURCE_UNSET | IORESOURCE_DISABLED))
+			continue;
+
+		len = pci_resource_len(pdev, bar);
+		if (!len)
+			continue;
+
+		pcibios_resource_to_bus(pdev->bus, &region, res);
+		start = res_type == IORESOURCE_IO ? region.start : res->start;
+
+		if (addr >= start && addr - start < len) {
+			*normalized = region.start + (addr - start);
+			return true;
+		}
+	}
+
+	return false;
+}
+
+struct vfio_spapr_eeh_addr_lookup {
+	unsigned long res_type;
+	unsigned long addr;
+	unsigned long normalized;
+	bool found;
+};
+
+static int vfio_spapr_eeh_match_dev(struct device *dev, void *data)
+{
+	struct vfio_spapr_eeh_addr_lookup *lookup = data;
+	struct pci_dev *pdev;
+
+	if (!dev_is_pci(dev))
+		return 0;
+
+	pdev = to_pci_dev(dev);
+	if (vfio_spapr_eeh_normalize_pdev_addr(pdev, lookup->res_type,
+					       lookup->addr,
+					       &lookup->normalized)) {
+		lookup->found = true;
+		return 1;
+	}
+
+	return 0;
+}
+
+/*
+ * Convert @addr to a PCI bus address using the BARs of type @res_type of the
+ * devices in @group.
+ */
+static int vfio_spapr_eeh_normalize_addr(struct iommu_group *group,
+					 unsigned long res_type,
+					 unsigned long addr,
+					 unsigned long *normalized)
+{
+	struct vfio_spapr_eeh_addr_lookup lookup = {
+		.res_type = res_type,
+		.addr = addr,
+	};
+	int ret;
+
+	ret = iommu_group_for_each_dev(group, &lookup,
+				       vfio_spapr_eeh_match_dev);
+	if (ret < 0)
+		return ret;
+
+	if (lookup.found) {
+		*normalized = lookup.normalized;
+		return 0;
+	}
+
+	pr_debug("vfio EEH: addr=0x%lx not found in any matching BAR of group\n",
+		 addr);
+	return -EINVAL;
+}
+
 static long vfio_spapr_ioctl_eeh_pe_op(struct iommu_group *group,
 				       unsigned long arg)
 {
 	struct eeh_pe *pe;
 	struct vfio_eeh_pe_op op;
-	unsigned long minsz;
+	unsigned long minsz, res_type;
 
 	pe = eeh_iommu_group_to_pe(group);
 	if (!pe)
@@ -818,6 +924,29 @@ static long vfio_spapr_ioctl_eeh_pe_op(struct iommu_group *group,
 		if (copy_from_user(&op, (void __user *)arg, minsz))
 			return -EFAULT;
 
+		/*
+		 * Only the pSeries RTAS backend takes a PCI bus address, so
+		 * PowerNV requests are passed through unchanged.  Config and
+		 * DMA functions do not carry BAR addresses, and an address
+		 * and mask of zero carries no address.
+		 */
+		res_type = eeh_err_func_to_res_type(op.err.func);
+		if (machine_is(pseries) && res_type &&
+		    (op.err.type == EEH_ERR_TYPE_32 ||
+		     op.err.type == EEH_ERR_TYPE_64) &&
+		    (op.err.addr || op.err.mask)) {
+			unsigned long normalized;
+			int ret;
+
+			ret = vfio_spapr_eeh_normalize_addr(group, res_type,
+							    op.err.addr,
+							    &normalized);
+			if (ret)
+				return ret;
+
+			op.err.addr = normalized;
+		}
+
 		return eeh_pe_inject_err(pe, op.err.type, op.err.func,
 					 op.err.addr, op.err.mask);
 	default:
-- 
2.54.0


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH v7 0/4] powerpc: Add RTAS based error injection support for pSeries
@ 2026-11-23 10:22 Narayana Murty N
  2026-11-23 10:22 ` [PATCH v7 1/4] powerpc/rtas: Handle ibm,open-errinjct return format Narayana Murty N
                   ` (3 more replies)
  0 siblings, 4 replies; 6+ messages in thread
From: Narayana Murty N @ 2026-11-23 10:22 UTC (permalink / raw)
  To: Madhavan Srinivasan, Oliver O'Halloran, Timothy Pearson,
	Alex Williamson, sourabhjain
  Cc: Michael Ellerman, Nicholas Piggin, Christophe Leroy,
	Ritesh Harjani, Shrikanth Hegde, linuxppc-dev, linux-kernel, kvm,
	sbhat, harshpb

The pSeries EEH error-injection backend currently implements a limited
software-generated MMIO failure and does not use the error-injection
services provided by RTAS.

This series replaces that implementation with the PAPR-defined RTAS
workflow based on:

- ibm,open-errinjct
- ibm,errinjct
- ibm,close-errinjct

The implementation opens an RTAS error-injection session, allocates and
prepares an RTAS-accessible work area, performs the requested injection,
releases the work area, and closes the session on both success and failure
paths.

The existing EEH userspace ABI is preserved. EEH_ERR_TYPE_32 and
EEH_ERR_TYPE_64 continue to represent generic 32-bit and 64-bit IOA
bus-error injection requests. The pSeries backend maps these values to
the corresponding RTAS error types, while the PowerNV backend explicitly
maps them to the corresponding OPAL types.

Additional generic EEH error types (EEH_ERR_TYPE_RECOVERED_SPECIAL_EVENT,
EEH_ERR_TYPE_CORRUPTED_PAGE, and the cache/TLB corruption types) are
defined in the UAPI header and mapped explicitly to RTAS firmware
encodings by the pSeries backend. Platform backends that do not support
a valid generic type return -EOPNOTSUPP.

No existing userspace ABI values are changed.

The injection path can be exercised for VFIO-assigned devices through
VFIO_EEH_PE_INJECT_ERR. The guest or userspace VFIO application continues
to use the same generic EEH type and function values, independent of
whether the host platform uses RTAS or OPAL.

For IOA bus-error injection, the VFIO sPAPR path normalizes a Linux BAR
resource address to the corresponding PCI bus address before dispatching
the request to the platform EEH backend. Address normalization is
restricted to memory and IO load/store functions whose addr argument is a
BAR-backed PCI address. Config-space and DMA functions are passed through
without translation because their addr arguments represent PCI config
register offsets and IOVA/TCE addresses respectively. The no-address form
is represented by both address and mask being zero.

The series also handles the unusual return format of ibm,open-errinjct:

rets[0] = error-injection session token
rets[1] = RTAS status

rtas_call() returns rets[1] as the status and places the session token in
outputs[0], preserving the normal kernel rtas_call() convention.

sys_rtas() continues to expose the raw RTAS return cells to userspace.
For ibm,open-errinjct, it validates that at least two return cells were
requested before entering RTAS and uses the RTAS status cell for
last-error handling. Userspace therefore continues to receive the session
token and status in their PAPR-defined positions.

The pSeries backend uses the existing RTAS work-area allocator for the
error-injection parameter buffer. The work area is allocated only after
ibm,open-errinjct succeeds and is released before ibm,close-errinjct.
RTAS work areas reside below RTAS_INSTANTIATE_MAX, so the physical address
fits in the 32-bit RTAS argument passed to firmware.

The complete open, inject, and close sequence is serialized with a mutex.
RTAS busy and extended-delay return values are handled for all three
calls. Once a session has been opened, ibm,close-errinjct is attempted
even when parameter preparation or error injection fails.

The patches are organized as follows:

1. Handle the special ibm,open-errinjct return format in rtas_call().
2. Add pSeries RTAS error-injection validation and buffer helpers.
3. Implement RTAS-based pSeries EEH error injection.
4. Normalize VFIO sPAPR IOA error-injection addresses.
5. Explicitly map generic EEH error types to OPAL types on PowerNV.

Testing was performed on PowerVM with firmware providing the RTAS
error-injection services and with corresponding QEMU support.

The corresponding QEMU v6 series is posted here:
https://lore.kernel.org/qemu-devel/20261019162541.68440-1-nnmlinux@linux.ibm.com/

Signed-off-by: Narayana Murty N <nnmlinux@linux.ibm.com>
---

Change Log:
V6 -> V7:
Changes since v6:
https://lore.kernel.org/all/20261023073642.59657-1-nnmlinux@linux.ibm.com/
  - Address review comments from Sourabh Jain and Sashiko.
  - Merge the pSeries RTAS helper and implementation patches to keep
    related functionality together. (Sourabh Jain, Sashiko)
  - Fix RTAS return-cell handling, tracing and documentation. 
    (Sourabh Jain, Sashiko) 
  - Obtain error-injection class tokens from the device tree instead
    of using hardcoded values. (Sashiko)
  - Improve pSeries BAR validation and RTAS error handling. 
    (Sourabh Jain, Sashiko) 
  - Refine VFIO address normalization using IOMMU group iteration,
    with pSeries-specific handling and correct BAR resource matching.
    (Sourabh Jain, Sashiko)
  - Retain the PowerNV error-type mapping from v6 without functional
    changes, and clarify its relationship to the pSeries RTAS patches.
    Both backends translate generic EEH types to firmware-specific
    RTAS or OPAL values. (Sourabh Jain)

V5 -> V6:
https://lore.kernel.org/all/20261019101813.45085-1-nnmlinux@linux.ibm.com/
* Addressed v5 review comments from Sashiko.

* Patch 1:
  - No changes.

* Patch 2:
  - Fix false BAR matches against unassigned or disabled BARs in
    validate_addr_mask_in_pe(): use pci_resource_len() instead of
    resource_size() (which returns 1 for zero-initialized BARs), and
    explicitly reject BARs with IORESOURCE_UNSET or IORESOURCE_DISABLED.
  - Move validate_errinjct_args() and prepare_errinjct_buffer() to
    Patch 3 alongside their callers so each intermediate commit builds
    cleanly with CONFIG_WERROR=y without unused-function warnings.

* Patch 3:
  - Include <asm/rtas-work-area.h> for the RTAS work-area API used by
    the pSeries error-injection implementation.
  - Introduce validate_errinjct_args() and prepare_errinjct_buffer() here
    alongside their callers (moved from Patch 2).
  - Fix eeh_break_device() visibility: keep it under #ifdef CONFIG_DEBUG_FS
    to prevent unused-function compiler warnings when CONFIG_DEBUG_FS=n.
  - Add pseries_eeh_func_needs_bar_addr() to identify error functions
    whose addr argument is a BAR-backed MMIO/IO address. Guard
    validate_addr_mask_in_pe() in prepare_errinjct_buffer() with this
    predicate so config offsets and DMA/IOVA addresses bypass BAR
    membership validation.
  - Demote the validate_addr_mask_in_pe() diagnostic message from
    pr_err() to pr_debug() since invalid user-supplied inputs do not
    represent internal kernel errors.
  - Update prepare_errinjct_buffer() kernel-doc comments to document
    function-specific addr semantics (MEM/IO = BAR, CFG = offset, DMA = IOVA).

* Patch 4:
  - Restrict address normalization to error functions carrying BAR-backed
    PCI MMIO/IO addresses (memory and IO load/store). Config-space and
    DMA functions bypass normalization rather than incorrectly returning
    -EINVAL.
  - Fix false matches against unassigned/disabled BARs in
    vfio_spapr_eeh_normalize_addr() using pci_resource_len() and checking
    IORESOURCE_UNSET / IORESOURCE_DISABLED.
  - Remove the misleading ", injection may fail" suffix from the
    pr_debug() message since the caller returns -EINVAL immediately.
  - Update commit description to accurately reflect function-specific
    address domains.

* Patch 5:
  - No changes.

V4 -> V5:
https://lore.kernel.org/all/20260831065441.48654-1-nnmlinux@linux.ibm.com/

* Addressed v4 review comments from Sourabh Jain.
* Return -EINVAL when ibm,open-errinjct is used with an invalid
  return-cell count.
* Validate the ibm,open-errinjct return-cell count in sys_rtas() before
  entering RTAS and use the common status helper for last-error handling.
* Rework the pSeries helper description to describe validation and
  RTAS parameter-buffer construction first.
* Rename rtas_type to rtas_err_type to distinguish the mapped RTAS type
  from the generic EEH type.
* Keep common error-function validation in validate_errinjct_args() and
  remove duplicate check from prepare_errinjct_buffer().
* Protect PE device and BAR traversal with pci_rescan_remove_lock.
* Rename pseries_errinjct_mutex to errinjct_mutex.
* Allocate RTAS work area only after ibm,open-errinjct succeeds and
  release it before ibm,close-errinjct.
* Ensure ibm,close-errinjct is attempted after a successful open when
  parameter preparation or error injection subsequently fails.
* Remove caller-side RTAS work-area address check and truncation, relying
  on RTAS work-area allocation guarantees for the 32-bit RTAS argument.
* Remove unnecessary error message for unencodable 32-bit IOA address or
  mask and retain the buffer-ready debug message.
* Remove eeh_pe_inject_mmio_error() and its declaration after removing
  its last caller.
* Reorder the VFIO address-normalization patch after the pSeries RTAS
  implementation.
* Remove the one-use VFIO error-type helper and check IOA error types
  directly at call site.
* Treat only addr == 0 && mask == 0 as a no-address IOA injection.
* Limit address matching to standard PCI BARs and add debug message
  when supplied address does not match a BAR.
* Expand PowerNV commit message to explain generic EEH-to-OPAL type
  mapping and rename opal_type to opal_err_type.
* Preserve -ERANGE for unsupported PowerNV error types.

V3 -> V4:
https://lore.kernel.org/all/20260831065441.48654-1-nnmlinux@linux.ibm.com/

* Use the RTAS work area allocator instead of adding a dedicated
  global error-injection buffer.
* Keep sys_rtas() raw return-cell semantics unchanged while handling
  the special ibm,open-errinjct return format in rtas_call().
* Treat ibm,open-errinjct with fewer than two return cells as invalid
  rtas_call() usage instead of interpreting rets[0] as status.
* Use normal Linux errno values in internal validation helpers instead
  of RTAS status values.
* Simplify the pSeries RTAS open/inject/close flow.
* Keep close-session failure as cleanup reporting without overwriting
  the primary operation status.
* Add VFIO sPAPR-side address normalization before dispatching IOA
  error injection to the platform EEH backend.
* Normalize IOA addresses based on the IOA error type, not only
  selected function codes.
* Keep the generic EEH ABI values unchanged and keep pSeries RTAS and
  PowerNV OPAL mappings explicit.

V2 -> V3:

* Fixed ibm,open-errinjct return handling to correctly process firmware
  responses.
* Allocate the error-injection buffer in RTAS-accessible memory instead
  of general kernel memory.
* Pass the physical address of the error-injection buffer to firmware
  (previously incorrect address type).
* Accept session token zero as a valid token (previously rejected
  erroneously).
* Handle RTAS busy and extended-delay return codes for open, inject,
  and close calls.
* Simplified the validation helper -- reduced complexity and removed
  redundant checks.
* Simplified the buffer-preparation helper for cleaner, more
  maintainable code.
* Validate that all required RTAS tokens are present before attempting
  to open a session.
* Added explicit generic EEH-to-OPAL error-type mapping for the
  PowerNV platform.

v1 -> v2:
https://lore.kernel.org/all/20260527072433.94510-1-nnmlinux@linux.ibm.com/

* Addressed all review comments from Sourabh Jain
  * Removed unnecessary empty line in rtas_call()
  * Enhanced comment to explain PAPR specification requirements
  * Corrected misleading comment about output handling
  * Improved else block comment for better code clarity
* Fixed kernel test robot warnings
  * Fixed kernel-doc warning for __maybe_unused parameter
  * Confirmed sparse warnings are false positives (correct endianness
    handling)
* Added PowerNV platform abstraction layer (new Patch 5)
  * Maps EEH error types to OPAL-specific types
  * Simplifies type handling by direct variable update
* Improved code comments and documentation throughout
* Added Reported-by tags for kernel test robot findings
* Split into logical 5-patch series for better review

RFC -> v1:
https://lore.kernel.org/all/20251205094510.4671-1-nnmlinux@linux.ibm.com/

* Initial 4-patch series
* Fixed PAPR ibm,open-errinjct output format (token,status order)
* Added pr_fmt handling for EEH subsystem compatibility
* Implemented comprehensive validation helpers

RFC:
https://lore.kernel.org/all/20251107091009.43034-1-nnmlinux@linux.ibm.com/

* Initial RFC implementation

Narayana Murty N (4):
  powerpc/rtas: Handle ibm,open-errinjct return format
  powerpc/pseries/eeh: Implement RTAS-based EEH error injection
  vfio/spapr_tce: Normalize EEH IOA error injection addresses
  powerpc/powernv/eeh: Map generic EEH error types to OPAL

 arch/powerpc/include/asm/eeh.h               |  26 +-
 arch/powerpc/include/asm/trace.h             |  26 +-
 arch/powerpc/kernel/eeh.c                    |  10 +-
 arch/powerpc/kernel/rtas.c                   |  83 ++++-
 arch/powerpc/platforms/powernv/eeh-powernv.c |  22 +-
 arch/powerpc/platforms/pseries/eeh_pseries.c | 334 +++++++++++++++++--
 drivers/vfio/vfio_iommu_spapr_tce.c          | 131 +++++++-
 7 files changed, 573 insertions(+), 59 deletions(-)

-- 
2.54.0


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH v7 1/4] powerpc/rtas: Handle ibm,open-errinjct return format
  2026-11-23 10:22 [PATCH v7 0/4] powerpc: Add RTAS based error injection support for pSeries Narayana Murty N
@ 2026-11-23 10:22 ` Narayana Murty N
  2026-11-23 10:22 ` [PATCH v7 2/4] powerpc/pseries/eeh: Implement RTAS-based EEH error injection Narayana Murty N
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 6+ messages in thread
From: Narayana Murty N @ 2026-11-23 10:22 UTC (permalink / raw)
  To: Madhavan Srinivasan, Oliver O'Halloran, Timothy Pearson,
	Alex Williamson, sourabhjain
  Cc: Michael Ellerman, Nicholas Piggin, Christophe Leroy,
	Ritesh Harjani, Shrikanth Hegde, linuxppc-dev, linux-kernel, kvm,
	sbhat, harshpb

ibm,open-errinjct returns the session token in rets[0] and the status in
rets[1], unlike other RTAS functions, which return the status in rets[0].
rtas_call() would report the session token as the status and drop the
real status.

Take the status from rets[1] for this function and return the session
token in outputs[0].  This needs exactly two return cells, so reject any
other count from kernel callers with -EINVAL and a one-time warning, and
document the exception in the rtas_call() kernel-doc.

Give the rtas_output tracepoint the index of the status cell, so that it
reports rets[1] as the status and the session token as an additional
output for this function.  A call with no return cells reports status 0
instead of reading a cell that does not exist.

Ensure __fetch_rtas_last_error() and sys_rtas() initialize rtas_args.rets
to point into rtas_args.args[] after copying local arguments, preventing
tracepoint handlers from dereferencing uninitialized memory or reading
stale on-stack values.

sys_rtas() still copies the raw return cells to userspace.  It uses the
status from the right cell for hardware-error (-1) detection and, after
the lockdown check, rejects fewer than two return cells for this function.

Link: https://files.openpower.foundation/s/XFgfMaqLMD5Bcm8
Signed-off-by: Narayana Murty N <nnmlinux@linux.ibm.com>
---
 arch/powerpc/include/asm/trace.h | 26 +++++++---
 arch/powerpc/kernel/rtas.c       | 83 ++++++++++++++++++++++++++------
 2 files changed, 88 insertions(+), 21 deletions(-)

diff --git a/arch/powerpc/include/asm/trace.h b/arch/powerpc/include/asm/trace.h
index a7b69b25296b..d731f70aead7 100644
--- a/arch/powerpc/include/asm/trace.h
+++ b/arch/powerpc/include/asm/trace.h
@@ -146,25 +146,37 @@ TRACE_EVENT(rtas_input,
 	)
 );
 
+/*
+ * @status_idx is the index in rets[] of the status cell: 1 for
+ * ibm,open-errinjct, which returns its session token in rets[0], and 0 for
+ * every other function.  The remaining cells are reported as other outputs.
+ */
 TRACE_EVENT(rtas_output,
 
-	TP_PROTO(struct rtas_args *rtas_args, const char *name),
+	TP_PROTO(struct rtas_args *rtas_args, const char *name,
+		 unsigned int status_idx),
 
-	TP_ARGS(rtas_args, name),
+	TP_ARGS(rtas_args, name, status_idx),
 
 	TP_STRUCT__entry(
 		__field(__u32, nr_other)
 		__field(__s32, status)
 		__string(name, name)
-		__dynamic_array(__u32, other_outputs, be32_to_cpu(rtas_args->nret) - 1)
+		__dynamic_array(__u32, other_outputs,
+				be32_to_cpu(rtas_args->nret) ?
+				be32_to_cpu(rtas_args->nret) - 1 : 0)
 	),
 
 	TP_fast_assign(
-		__entry->nr_other = be32_to_cpu(rtas_args->nret) - 1;
-		__entry->status = be32_to_cpu(rtas_args->rets[0]);
+		u32 nret = be32_to_cpu(rtas_args->nret);
+		u32 *other = __get_dynamic_array(other_outputs);
+		u32 i;
+
+		__entry->nr_other = nret ? nret - 1 : 0;
+		__entry->status = nret ? be32_to_cpu(rtas_args->rets[status_idx]) : 0;
 		__assign_str(name);
-		be32_to_cpu_array(__get_dynamic_array(other_outputs),
-				  &rtas_args->rets[1], __entry->nr_other);
+		for (i = 0; i < __entry->nr_other; i++)
+			other[i] = be32_to_cpu(rtas_args->rets[i < status_idx ? i : i + 1]);
 	),
 
 	TP_printk("%s status: %d, other outputs: %s", __get_str(name), __entry->status,
diff --git a/arch/powerpc/kernel/rtas.c b/arch/powerpc/kernel/rtas.c
index 8d81c1e7a8db..201be33d9bd3 100644
--- a/arch/powerpc/kernel/rtas.c
+++ b/arch/powerpc/kernel/rtas.c
@@ -693,6 +693,32 @@ static const struct rtas_function *rtas_token_to_function(s32 token)
 /* This is here deliberately so it's only used in this file */
 void enter_rtas(unsigned long);
 
+static inline bool rtas_token_is_open_errinjct(s32 token)
+{
+	return token == rtas_function_token(RTAS_FN_IBM_OPEN_ERRINJCT);
+}
+
+/*
+ * Return the index of the status cell in rets[].  ibm,open-errinjct returns
+ * the session token in rets[0] and the status in rets[1].  Every other RTAS
+ * function returns the status in rets[0].
+ */
+static unsigned int rtas_status_idx(s32 token, u32 nret)
+{
+	return (nret >= 2 && rtas_token_is_open_errinjct(token)) ? 1 : 0;
+}
+
+/* Return the status of a completed call, or 0 if it has no return cells. */
+static int rtas_status_from_args(s32 token, struct rtas_args *args)
+{
+	u32 nret = be32_to_cpu(args->nret);
+
+	if (!nret)
+		return 0;
+
+	return be32_to_cpu(args->rets[rtas_status_idx(token, nret)]);
+}
+
 static void __do_enter_rtas(struct rtas_args *args)
 {
 	enter_rtas(__pa(args));
@@ -719,7 +745,9 @@ static void __do_enter_rtas_trace(struct rtas_args *args)
 	__do_enter_rtas(args);
 
 	trace_rtas_ll_exit(args);
-	trace_rtas_output(args, func->name);
+	trace_rtas_output(args, func->name,
+			  rtas_status_idx(be32_to_cpu(args->token),
+					  be32_to_cpu(args->nret)));
 }
 
 static void do_enter_rtas(struct rtas_args *args)
@@ -1034,6 +1062,7 @@ static char *__fetch_rtas_last_error(char *altbuf)
 
 	save_args = rtas_args;
 	rtas_args = err_args;
+	rtas_args.rets = &rtas_args.args[2];
 
 	do_enter_rtas(&rtas_args);
 
@@ -1122,20 +1151,28 @@ static bool token_is_restricted_errinjct(s32 token)
  * @token: Identifies the function being invoked.
  * @nargs: Number of input parameters. Does not include token.
  * @nret: Number of output parameters, including the call status.
- * @outputs: Array of @nret output words.
+ * @outputs: Array of @nret - 1 output words, or NULL.
  * @....: List of @nargs input parameters.
  *
  * Invokes the RTAS function indicated by @token, which the caller
  * should obtain via rtas_function_token().
  *
  * The @nargs and @nret arguments must match the number of input and
- * output parameters specified for the RTAS function.
+ * output parameters specified for the RTAS function.  @nret includes the
+ * status cell, so @outputs normally receives the remaining @nret - 1
+ * return cells.
  *
- * rtas_call() returns RTAS status codes, not conventional Linux errno
- * values. Callers must translate any failure to an appropriate errno
- * in syscall context. Most callers of RTAS functions that can return
- * -2 or 990x should use rtas_busy_delay() to correctly handle those
- * statuses before calling again.
+ * ibm,open-errinjct is the exception: it returns its status in rets[1]
+ * and a session token in rets[0].  rtas_call() still returns the status,
+ * and @outputs[0] receives the session token.  @nret must be 2 for this
+ * function; any other value fails with -EINVAL, a Linux errno rather
+ * than an RTAS status, without calling RTAS.
+ *
+ * Apart from that case, rtas_call() returns RTAS status codes, not
+ * conventional Linux errno values. Callers must translate any failure to
+ * an appropriate errno in syscall context. Most callers of RTAS functions
+ * that can return -2 or 990x should use rtas_busy_delay() to correctly
+ * handle those statuses before calling again.
  *
  * The return value descriptions are adapted from 7.2.8 [RTAS] Return
  * Codes of the PAPR and CHRP specifications.
@@ -1198,6 +1235,12 @@ int rtas_call(int token, int nargs, int nret, int *outputs, ...)
 			return -1;
 	}
 
+	if (rtas_token_is_open_errinjct(token) && nret != 2) {
+		WARN_ONCE(1, "rtas: ibm,open-errinjct requires exactly two return cells (got %d)\n",
+			  nret);
+		return -EINVAL;
+	}
+
 	if ((mfmsr() & (MSR_IR|MSR_DR)) != (MSR_IR|MSR_DR)) {
 		WARN_ON_ONCE(1);
 		return -1;
@@ -1215,13 +1258,20 @@ int rtas_call(int token, int nargs, int nret, int *outputs, ...)
 
 	/* A -1 return code indicates that the last command couldn't
 	   be completed due to a hardware error. */
-	if (be32_to_cpu(args->rets[0]) == -1)
+	if (rtas_status_from_args(token, args) == -1)
 		buff_copy = __fetch_rtas_last_error(NULL);
 
-	if (nret > 1 && outputs != NULL)
-		for (i = 0; i < nret-1; ++i)
-			outputs[i] = be32_to_cpu(args->rets[i + 1]);
-	ret = (nret > 0) ? be32_to_cpu(args->rets[0]) : 0;
+	if (rtas_token_is_open_errinjct(token)) {
+		/* nret is 2: rets[0] is the session token, rets[1] the status. */
+		if (outputs)
+			outputs[0] = be32_to_cpu(args->rets[0]);
+		ret = be32_to_cpu(args->rets[1]);
+	} else {
+		if (nret > 1 && outputs != NULL)
+			for (i = 0; i < nret-1; ++i)
+				outputs[i] = be32_to_cpu(args->rets[i + 1]);
+		ret = (nret > 0) ? be32_to_cpu(args->rets[0]) : 0;
+	}
 
 	lockdep_unpin_lock(&rtas_lock, cookie);
 	raw_spin_unlock_irqrestore(&rtas_lock, flags);
@@ -1904,6 +1954,10 @@ SYSCALL_DEFINE1(rtas, struct rtas_args __user *, uargs)
 			return err;
 	}
 
+	/* ibm,open-errinjct needs rets[1] for its status. */
+	if (rtas_token_is_open_errinjct(token) && nret < 2)
+		return -EINVAL;
+
 	/* Need to handle ibm,suspend_me call specially */
 	if (token == rtas_function_token(RTAS_FN_IBM_SUSPEND_ME)) {
 
@@ -1939,12 +1993,13 @@ SYSCALL_DEFINE1(rtas, struct rtas_args __user *, uargs)
 	cookie = lockdep_pin_lock(&rtas_lock);
 
 	rtas_args = args;
+	rtas_args.rets = &rtas_args.args[nargs];
 	do_enter_rtas(&rtas_args);
 	args = rtas_args;
 
 	/* A -1 return code indicates that the last command couldn't
 	   be completed due to a hardware error. */
-	if (be32_to_cpu(args.rets[0]) == -1)
+	if (rtas_status_from_args(token, &args) == -1)
 		errbuf = __fetch_rtas_last_error(buff_copy);
 
 	lockdep_unpin_lock(&rtas_lock, cookie);
-- 
2.54.0


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH v7 2/4] powerpc/pseries/eeh: Implement RTAS-based EEH error injection
  2026-11-23 10:22 [PATCH v7 0/4] powerpc: Add RTAS based error injection support for pSeries Narayana Murty N
  2026-11-23 10:22 ` [PATCH v7 1/4] powerpc/rtas: Handle ibm,open-errinjct return format Narayana Murty N
@ 2026-11-23 10:22 ` Narayana Murty N
  2026-11-23 10:22 ` [PATCH v7 3/4] vfio/spapr_tce: Normalize EEH IOA error injection addresses Narayana Murty N
  2026-11-23 10:22 ` [PATCH v7 4/4] powerpc/powernv/eeh: Map generic EEH error types to OPAL Narayana Murty N
  3 siblings, 0 replies; 6+ messages in thread
From: Narayana Murty N @ 2026-11-23 10:22 UTC (permalink / raw)
  To: Madhavan Srinivasan, Oliver O'Halloran, Timothy Pearson,
	Alex Williamson, sourabhjain
  Cc: Michael Ellerman, Nicholas Piggin, Christophe Leroy,
	Ritesh Harjani, Shrikanth Hegde, linuxppc-dev, linux-kernel, kvm,
	sbhat, harshpb

pseries_eeh_err_inject() only emulates MMIO errors in software and
ignores the address and mask.  Use the PAPR RTAS services
ibm,open-errinjct, ibm,errinjct and ibm,close-errinjct instead, so the
platform raises the error for a given IOA bus address.

EEH_ERR_TYPE_32 and EEH_ERR_TYPE_64 select the ioa-bus-error and
ioa-bus-error-64 classes and the matching parameter buffer layout.  The
class token is read from the "ibm,errinjct-tokens" property of /rtas; if
the class is not advertised the request fails with -EOPNOTSUPP.  The
buffer comes from the RTAS work area allocator, and the session is closed
on every exit path after a successful open.  A mutex serializes sessions
started through this backend.  Statuses specific to these services, such
as "already open", are mapped to errno locally; rtas_error_rc() is
unchanged.

For memory and I/O load/store functions the address must be a PCI bus
address within an assigned standard BAR (BAR0-BAR5) of the matching type,
memory or I/O, of a device in the PE.  pci_lock_rescan_remove() protects
the PE device list walk and snapshots the PE address and BUID before
releasing the lock and opening the RTAS session.  Config-space and DMA
functions, and requests with address and mask both zero, are not checked.
The mask is passed to firmware unchanged.

Remove eeh_pe_inject_mmio_error().  eeh_break_device() is now used only by
debugfs, so build it only with CONFIG_DEBUG_FS.

Firmware without the ERRINJCT services or class token no longer gets the
software-emulated injection; the request fails with -ENODEV or
-EOPNOTSUPP.

Signed-off-by: Narayana Murty N <nnmlinux@linux.ibm.com>
---
 arch/powerpc/include/asm/eeh.h               |  26 +-
 arch/powerpc/kernel/eeh.c                    |  10 +-
 arch/powerpc/platforms/pseries/eeh_pseries.c | 334 +++++++++++++++++--
 3 files changed, 338 insertions(+), 32 deletions(-)

diff --git a/arch/powerpc/include/asm/eeh.h b/arch/powerpc/include/asm/eeh.h
index b7ebb4ac2c71..f7743dcf75a4 100644
--- a/arch/powerpc/include/asm/eeh.h
+++ b/arch/powerpc/include/asm/eeh.h
@@ -9,6 +9,7 @@
 #ifdef __KERNEL__
 
 #include <linux/init.h>
+#include <linux/ioport.h>
 #include <linux/list.h>
 #include <linux/string.h>
 #include <linux/time.h>
@@ -310,7 +311,30 @@ int eeh_pe_reset(struct eeh_pe *pe, int option, bool include_passed);
 int eeh_pe_configure(struct eeh_pe *pe);
 int eeh_pe_inject_err(struct eeh_pe *pe, int type, int func,
 		      unsigned long addr, unsigned long mask);
-int eeh_pe_inject_mmio_error(struct pci_dev *pdev);
+
+/*
+ * Return the resource type (IORESOURCE_MEM or IORESOURCE_IO) of the BAR that
+ * error function @func addresses, or 0 if it does not take a BAR address.
+ * Config-space functions take a config register offset and DMA functions
+ * take a DMA address.
+ */
+static inline unsigned long eeh_err_func_to_res_type(int func)
+{
+	switch (func) {
+	case EEH_ERR_FUNC_LD_MEM_ADDR:
+	case EEH_ERR_FUNC_LD_MEM_DATA:
+	case EEH_ERR_FUNC_ST_MEM_ADDR:
+	case EEH_ERR_FUNC_ST_MEM_DATA:
+		return IORESOURCE_MEM;
+	case EEH_ERR_FUNC_LD_IO_ADDR:
+	case EEH_ERR_FUNC_LD_IO_DATA:
+	case EEH_ERR_FUNC_ST_IO_ADDR:
+	case EEH_ERR_FUNC_ST_IO_DATA:
+		return IORESOURCE_IO;
+	default:
+		return 0;
+	}
+}
 
 /**
  * EEH_POSSIBLE_ERROR() -- test for possible MMIO failure.
diff --git a/arch/powerpc/kernel/eeh.c b/arch/powerpc/kernel/eeh.c
index 2d7898ef6b8c..d4c018849787 100644
--- a/arch/powerpc/kernel/eeh.c
+++ b/arch/powerpc/kernel/eeh.c
@@ -1571,6 +1571,8 @@ static int proc_eeh_show(struct seq_file *m, void *v)
 }
 #endif /* CONFIG_PROC_FS */
 
+#ifdef CONFIG_DEBUG_FS
+
 static int eeh_break_device(struct pci_dev *pdev)
 {
 	struct resource *bar = NULL;
@@ -1664,14 +1666,6 @@ static int eeh_break_device(struct pci_dev *pdev)
 	return 0;
 }
 
-int eeh_pe_inject_mmio_error(struct pci_dev *pdev)
-{
-	return eeh_break_device(pdev);
-}
-
-#ifdef CONFIG_DEBUG_FS
-
-
 static struct pci_dev *eeh_debug_lookup_pdev(struct file *filp,
 					     const char __user *user_buf,
 					     size_t count, loff_t *ppos)
diff --git a/arch/powerpc/platforms/pseries/eeh_pseries.c b/arch/powerpc/platforms/pseries/eeh_pseries.c
index b12ef382fec7..c8466017d1e4 100644
--- a/arch/powerpc/platforms/pseries/eeh_pseries.c
+++ b/arch/powerpc/platforms/pseries/eeh_pseries.c
@@ -17,13 +17,17 @@
 #include <linux/export.h>
 #include <linux/init.h>
 #include <linux/list.h>
+#include <linux/mutex.h>
 #include <linux/of.h>
 #include <linux/pci.h>
 #include <linux/proc_fs.h>
 #include <linux/rbtree.h>
 #include <linux/sched.h>
 #include <linux/seq_file.h>
+#include <linux/sizes.h>
 #include <linux/spinlock.h>
+#include <linux/string.h>
+#include <linux/unaligned.h>
 #include <linux/crash_dump.h>
 
 #include <asm/eeh.h>
@@ -32,6 +36,7 @@
 #include <asm/machdep.h>
 #include <asm/ppc-pci.h>
 #include <asm/rtas.h>
+#include <asm/rtas-work-area.h>
 
 /* RTAS tokens */
 static int ibm_set_eeh_option;
@@ -786,41 +791,324 @@ static int pseries_notify_resume(struct eeh_dev *edev)
 }
 #endif
 
+/* Size of the ibm,errinjct work buffer */
+#define RTAS_ERRINJCT_BUF_SIZE			SZ_1K
+
+/*
+ * Look up the ibm,errinjct class token for a generic EEH error type in the
+ * "ibm,errinjct-tokens" property of the /rtas node.  The property is a
+ * packed list of (NUL-terminated class name, big-endian 32-bit token)
+ * entries, so tokens are not necessarily aligned.
+ *
+ * rtas.dev holds a reference on the /rtas node for the life of the system,
+ * so none is taken here.
+ *
+ * Return: 0 and *@token set on success, -EINVAL for an unsupported @type,
+ * -EOPNOTSUPP if firmware does not advertise the class, -EIO if the
+ * property is malformed.
+ */
+static int pseries_eeh_get_errinjct_token(int type, u32 *token)
+{
+	const char *name, *p;
+	size_t namelen, remaining;
+	int len;
+
+	switch (type) {
+	case EEH_ERR_TYPE_32:
+		name = "ioa-bus-error";
+		break;
+	case EEH_ERR_TYPE_64:
+		name = "ioa-bus-error-64";
+		break;
+	default:
+		return -EINVAL;
+	}
+
+	p = of_get_property(rtas.dev, "ibm,errinjct-tokens", &len);
+	if (!p)
+		return -EOPNOTSUPP;
+
+	for (remaining = len; remaining; ) {
+		namelen = strnlen(p, remaining);
+
+		/* Need the NUL terminator and a whole token after it. */
+		if (namelen == remaining ||
+		    remaining - namelen - 1 < sizeof(u32)) {
+			pr_warn_once("EEH: malformed ibm,errinjct-tokens\n");
+			return -EIO;
+		}
+
+		if (!strcmp(p, name)) {
+			*token = get_unaligned_be32(p + namelen + 1);
+			return 0;
+		}
+
+		namelen += 1 + sizeof(u32);
+		p += namelen;
+		remaining -= namelen;
+	}
+
+	return -EOPNOTSUPP;
+}
+
+/*
+ * Return true if @addr, a PCI bus address, lies within an assigned, enabled
+ * standard BAR (BAR0-BAR5) of @pdev whose resource type is @res_type.
+ * Expansion ROM, SR-IOV resources and bridge windows are not supported.
+ *
+ * On pseries, unassigned BARs are zero-based and flagged IORESOURCE_UNSET,
+ * so they would otherwise translate to a bus range starting at zero.
+ */
+static bool addr_in_pdev_bars(struct pci_dev *pdev, unsigned long res_type,
+			      unsigned long addr)
+{
+	struct pci_bus_region region;
+	struct resource *res;
+	resource_size_t len;
+	int bar;
+
+	for (bar = 0; bar < PCI_STD_NUM_BARS; bar++) {
+		res = &pdev->resource[bar];
+
+		if (resource_type(res) != res_type)
+			continue;
+
+		if (res->flags & (IORESOURCE_UNSET | IORESOURCE_DISABLED))
+			continue;
+
+		len = pci_resource_len(pdev, bar);
+		if (!len)
+			continue;
+
+		pcibios_resource_to_bus(pdev->bus, &region, res);
+		if (addr >= region.start && addr - region.start < len)
+			return true;
+	}
+
+	return false;
+}
+
+/*
+ * Check that @addr, a PCI bus address, lies within a standard BAR of type
+ * @res_type of a device in @pe. Must be called with pci_lock_rescan_remove()
+ * held to protect PE device list traversal and PCI resource consistency.
+ */
+static int validate_addr_in_pe(struct eeh_pe *pe, unsigned long res_type,
+			       unsigned long addr)
+{
+	struct eeh_dev *edev, *tmp;
+	struct pci_dev *pdev;
+
+	eeh_pe_for_each_dev(pe, edev, tmp) {
+		pdev = eeh_dev_to_pci_dev(edev);
+		if (pdev && addr_in_pdev_bars(pdev, res_type, addr))
+			return 0;
+	}
+
+	pr_debug("EEH: addr 0x%lx is not within any matching BAR of the PE\n",
+		 addr);
+	return -EINVAL;
+}
+
+/*
+ * Fill the ibm,errinjct work buffer.  The layout depends on the generic EEH
+ * error type, not on the firmware class token.  @addr is a PCI bus address
+ * and @mask is passed to firmware unchanged.  Word layouts are defined by
+ * PAPR for ioa-bus-error and ioa-bus-error-64.
+ */
+static void prepare_errinjct_buffer(void *buf, u32 pe_addr, u64 buid, int type,
+				    int func, unsigned long addr,
+				    unsigned long mask)
+{
+	__be64 *buf64 = buf;
+	__be32 *buf32 = buf;
+
+	memset(buf, 0, RTAS_ERRINJCT_BUF_SIZE);
+
+	switch (type) {
+	case EEH_ERR_TYPE_32:
+		buf32[0] = cpu_to_be32((u32)addr);
+		buf32[1] = cpu_to_be32((u32)mask);
+		buf32[2] = cpu_to_be32(pe_addr);
+		buf32[3] = cpu_to_be32(BUID_HI(buid));
+		buf32[4] = cpu_to_be32(BUID_LO(buid));
+		buf32[5] = cpu_to_be32(func);
+		break;
+	case EEH_ERR_TYPE_64:
+		buf64[0] = cpu_to_be64(addr);
+		buf64[1] = cpu_to_be64(mask);
+		buf32[4] = cpu_to_be32(pe_addr);
+		buf32[5] = cpu_to_be32(BUID_HI(buid));
+		buf32[6] = cpu_to_be32(BUID_LO(buid));
+		buf32[7] = cpu_to_be32(func);
+		break;
+	}
+
+	pr_debug("EEH: errinjct buffer: type=%d func=%d addr=0x%lx mask=0x%lx\n",
+		 type, func, addr, mask);
+}
+
+/*
+ * PAPR allows only one open ERRINJCT session: while one is open,
+ * ibm,open-errinjct returns -4 ("already open") to any other caller.  Hold
+ * this mutex across open/inject/close so concurrent requests through this
+ * backend run one after another instead of failing.  It does not exclude
+ * direct sys_rtas() users of the same facility, who still get the firmware
+ * status.
+ */
+static DEFINE_MUTEX(errinjct_mutex);
+
+/*
+ * Map an ibm,{open,errinjct,close}-errinjct status to an errno.  rtas_error_rc()
+ * does not know the statuses specific to these services, so the mapping is
+ * local.  A -1 may be a firmware hardware error or a kernel lockdown denial,
+ * which rtas_call() does not distinguish, so it is reported as -EIO.
+ */
+static int pseries_errinjct_errno(const char *op, int rc, bool is_open)
+{
+	pr_debug("EEH: %s failed: status=%d\n", op, rc);
+
+	switch (rc) {
+	case RTAS_HARDWARE_ERROR:
+		return -EIO;
+	case RTAS_INVALID_PARAMETER:
+		return -EINVAL;
+	case -4:
+		/* open: already open; others: not the opener, or not open */
+		return is_open ? -EBUSY : -EIO;
+	case -5:
+		/* open: PCI error injection is not available */
+		if (is_open)
+			return -EOPNOTSUPP;
+		fallthrough;
+	default:
+		pr_warn_ratelimited("EEH: %s failed: unexpected status %d\n",
+				    op, rc);
+		return -ERANGE;
+	}
+}
+
 /**
  * pseries_eeh_err_inject - Inject specified error to the indicated PE
  * @pe: the indicated PE
- * @type: error type
- * @func: specific error type
- * @addr: address
- * @mask: address mask
- * The routine is called to inject specified error, which is
- * determined by @type and @func, to the indicated PE
+ * @type: generic EEH error type (EEH_ERR_TYPE_32 or EEH_ERR_TYPE_64)
+ * @func: error function (EEH_ERR_FUNC_*)
+ * @addr: address; its meaning depends on @func
+ * @mask: address mask, passed to firmware unchanged
+ *
+ * Inject the error using the RTAS ibm,open-errinjct, ibm,errinjct and
+ * ibm,close-errinjct sequence, with the ibm,errinjct class token taken from
+ * the "ibm,errinjct-tokens" property.
+ *
+ * For memory and I/O load/store functions @addr is a PCI bus address within
+ * a standard BAR (BAR0-BAR5) of the matching resource type (memory or I/O)
+ * of a device in the PE; otherwise -EINVAL is returned. Expansion ROM,
+ * SR-IOV resources and bridge windows are not supported. This includes @addr
+ * of zero with a non-zero @mask. Config-space and DMA functions pass @addr
+ * through unchanged. A request with @addr and @mask both zero carries no
+ * address and is passed through. EEH_ERR_TYPE_32 uses 32-bit address and
+ * mask words and EEH_ERR_TYPE_64 uses 64-bit ones.
+ *
+ * pci_lock_rescan_remove() is held while validating device BARs and
+ * snapshotting PE configuration parameters, but is released before
+ * acquiring errinjct_mutex and entering RTAS to avoid holding the global
+ * rescan/remove lock across firmware latency.
+ *
+ * Return: 0 on success, negative errno on failure.
  */
 static int pseries_eeh_err_inject(struct eeh_pe *pe, int type, int func,
 				  unsigned long addr, unsigned long mask)
 {
-	struct	eeh_dev	*pdev;
+	int open_token, errinjct_token, close_token;
+	struct rtas_work_area *area;
+	unsigned long res_type;
+	int session_token;
+	u32 class_token;
+	u32 pe_addr;
+	u64 buid;
+	int close_rc;
+	int rc;
 
-	/* Check on PCI error type */
-	if (type != EEH_ERR_TYPE_32 && type != EEH_ERR_TYPE_64)
+	if (!pe || !pe->phb)
 		return -EINVAL;
 
-	switch (func) {
-	case EEH_ERR_FUNC_LD_MEM_ADDR:
-	case EEH_ERR_FUNC_LD_MEM_DATA:
-	case EEH_ERR_FUNC_ST_MEM_ADDR:
-	case EEH_ERR_FUNC_ST_MEM_DATA:
-		/* injects a MMIO error for all pdev's belonging to PE */
-		pci_lock_rescan_remove();
-		list_for_each_entry(pdev, &pe->edevs, entry)
-			eeh_pe_inject_mmio_error(pdev->pdev);
-		pci_unlock_rescan_remove();
-		break;
-	default:
-		return -ERANGE;
+	if (func < EEH_ERR_FUNC_MIN || func > EEH_ERR_FUNC_MAX)
+		return -EINVAL;
+
+	rc = pseries_eeh_get_errinjct_token(type, &class_token);
+	if (rc)
+		return rc;
+
+	/* The 32-bit buffer format carries 32-bit address and mask words. */
+	if (type == EEH_ERR_TYPE_32 &&
+	    (upper_32_bits(addr) || upper_32_bits(mask)))
+		return -EINVAL;
+
+	res_type = eeh_err_func_to_res_type(func);
+
+	pci_lock_rescan_remove();
+	if (res_type && (addr || mask)) {
+		rc = validate_addr_in_pe(pe, res_type, addr);
+		if (rc) {
+			pci_unlock_rescan_remove();
+			return rc;
+		}
 	}
 
-	return 0;
+	pe_addr = pe->addr;
+	buid = pe->phb->buid;
+	pci_unlock_rescan_remove();
+
+	open_token = rtas_function_token(RTAS_FN_IBM_OPEN_ERRINJCT);
+	errinjct_token = rtas_function_token(RTAS_FN_IBM_ERRINJCT);
+	close_token = rtas_function_token(RTAS_FN_IBM_CLOSE_ERRINJCT);
+
+	if (open_token == RTAS_UNKNOWN_SERVICE ||
+	    errinjct_token == RTAS_UNKNOWN_SERVICE ||
+	    close_token == RTAS_UNKNOWN_SERVICE)
+		return -ENODEV;
+
+	mutex_lock(&errinjct_mutex);
+
+	do {
+		rc = rtas_call(open_token, 0, 2, &session_token);
+	} while (rtas_busy_delay(rc));
+
+	if (rc) {
+		rc = pseries_errinjct_errno("ibm,open-errinjct", rc, true);
+		goto out_unlock;
+	}
+
+	/* ibm,errinjct takes the physical address of a 1KB work buffer. */
+	area = rtas_work_area_alloc(RTAS_ERRINJCT_BUF_SIZE);
+	prepare_errinjct_buffer(rtas_work_area_raw_buf(area), pe_addr, buid,
+				type, func, addr, mask);
+
+	do {
+		rc = rtas_call(errinjct_token, 3, 1, NULL, class_token,
+			       session_token, rtas_work_area_phys(area));
+	} while (rtas_busy_delay(rc));
+
+	if (rc)
+		rc = pseries_errinjct_errno("ibm,errinjct", rc, false);
+
+	rtas_work_area_free(area);
+
+	/* Always close the session; keep the injection status if it failed. */
+	do {
+		close_rc = rtas_call(close_token, 1, 1, NULL, session_token);
+	} while (rtas_busy_delay(close_rc));
+
+	if (close_rc) {
+		close_rc = pseries_errinjct_errno("ibm,close-errinjct",
+						  close_rc, false);
+		if (!rc)
+			rc = close_rc;
+	}
+
+out_unlock:
+	mutex_unlock(&errinjct_mutex);
+	return rc;
 }
 
 static struct eeh_ops pseries_eeh_ops = {
-- 
2.54.0


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH v7 3/4] vfio/spapr_tce: Normalize EEH IOA error injection addresses
  2026-11-23 10:22 [PATCH v7 0/4] powerpc: Add RTAS based error injection support for pSeries Narayana Murty N
  2026-11-23 10:22 ` [PATCH v7 1/4] powerpc/rtas: Handle ibm,open-errinjct return format Narayana Murty N
  2026-11-23 10:22 ` [PATCH v7 2/4] powerpc/pseries/eeh: Implement RTAS-based EEH error injection Narayana Murty N
@ 2026-11-23 10:22 ` Narayana Murty N
  2026-11-23 10:22 ` [PATCH v7 4/4] powerpc/powernv/eeh: Map generic EEH error types to OPAL Narayana Murty N
  3 siblings, 0 replies; 6+ messages in thread
From: Narayana Murty N @ 2026-11-23 10:22 UTC (permalink / raw)
  To: Madhavan Srinivasan, Oliver O'Halloran, Timothy Pearson,
	Alex Williamson, sourabhjain
  Cc: Michael Ellerman, Nicholas Piggin, Christophe Leroy,
	Ritesh Harjani, Shrikanth Hegde, linuxppc-dev, linux-kernel, kvm,
	sbhat, harshpb

On pseries, RTAS error injection takes a PCI bus address for memory and
I/O load/store functions.  VFIO_EEH_PE_INJECT_ERR callers such as QEMU
pass an address within a BAR as the sysfs "resource" file exports it: a
CPU address for a memory BAR and a PCI bus address for an I/O BAR.  A
CPU address can differ from the PCI bus address because of PHB window
translation.

On pseries, convert the address before calling eeh_pe_inject_err() to the
PCI bus address of the matching standard BAR (BAR0-BAR5) of a device in
the IOMMU group, keeping its offset.  Memory functions match memory BARs
and I/O functions match I/O BARs.  Unassigned and disabled BARs are never
matched, and an address that matches no BAR fails with -EINVAL.

Config-space and DMA functions, requests with address and mask both zero,
and requests on other platforms are not changed.  The mask is never
modified.

Signed-off-by: Narayana Murty N <nnmlinux@linux.ibm.com>
---
 drivers/vfio/vfio_iommu_spapr_tce.c | 131 +++++++++++++++++++++++++++-
 1 file changed, 130 insertions(+), 1 deletion(-)

diff --git a/drivers/vfio/vfio_iommu_spapr_tce.c b/drivers/vfio/vfio_iommu_spapr_tce.c
index 1c0eec228cc6..c56b649d7094 100644
--- a/drivers/vfio/vfio_iommu_spapr_tce.c
+++ b/drivers/vfio/vfio_iommu_spapr_tce.c
@@ -23,9 +23,11 @@
 #include <linux/mm.h>
 #include "vfio.h"
 
+#include <asm/eeh.h>
 #include <asm/iommu.h>
 #include <asm/tce.h>
 #include <asm/mmu_context.h>
+#include <asm/machdep.h>
 
 #define DRIVER_VERSION  "0.1"
 #define DRIVER_AUTHOR   "aik@ozlabs.ru"
@@ -774,12 +776,116 @@ static long tce_iommu_create_default_window(struct tce_container *container)
 	return ret;
 }
 
+/*
+ * Look for @addr in the standard BARs (BAR0-BAR5) of @pdev whose resource
+ * type is @res_type, and translate it to the PCI bus address RTAS expects,
+ * keeping the offset into the BAR. Expansion ROM, SR-IOV resources and
+ * bridge windows are not supported.
+ *
+ * @addr is in the domain the sysfs "resource" file exports: the CPU address
+ * for a memory BAR and the PCI bus address for an I/O BAR.  Each type has a
+ * single domain, so a match is unambiguous.
+ *
+ * Unassigned BARs are zero-based and flagged IORESOURCE_UNSET on pseries,
+ * so they are skipped, as are disabled ones.
+ */
+static bool vfio_spapr_eeh_normalize_pdev_addr(struct pci_dev *pdev,
+					       unsigned long res_type,
+					       unsigned long addr,
+					       unsigned long *normalized)
+{
+	struct pci_bus_region region;
+	struct resource *res;
+	resource_size_t len, start;
+	int bar;
+
+	for (bar = 0; bar < PCI_STD_NUM_BARS; bar++) {
+		res = &pdev->resource[bar];
+
+		if (resource_type(res) != res_type)
+			continue;
+
+		if (res->flags & (IORESOURCE_UNSET | IORESOURCE_DISABLED))
+			continue;
+
+		len = pci_resource_len(pdev, bar);
+		if (!len)
+			continue;
+
+		pcibios_resource_to_bus(pdev->bus, &region, res);
+		start = res_type == IORESOURCE_IO ? region.start : res->start;
+
+		if (addr >= start && addr - start < len) {
+			*normalized = region.start + (addr - start);
+			return true;
+		}
+	}
+
+	return false;
+}
+
+struct vfio_spapr_eeh_addr_lookup {
+	unsigned long res_type;
+	unsigned long addr;
+	unsigned long normalized;
+	bool found;
+};
+
+static int vfio_spapr_eeh_match_dev(struct device *dev, void *data)
+{
+	struct vfio_spapr_eeh_addr_lookup *lookup = data;
+	struct pci_dev *pdev;
+
+	if (!dev_is_pci(dev))
+		return 0;
+
+	pdev = to_pci_dev(dev);
+	if (vfio_spapr_eeh_normalize_pdev_addr(pdev, lookup->res_type,
+					       lookup->addr,
+					       &lookup->normalized)) {
+		lookup->found = true;
+		return 1;
+	}
+
+	return 0;
+}
+
+/*
+ * Convert @addr to a PCI bus address using the BARs of type @res_type of the
+ * devices in @group.
+ */
+static int vfio_spapr_eeh_normalize_addr(struct iommu_group *group,
+					 unsigned long res_type,
+					 unsigned long addr,
+					 unsigned long *normalized)
+{
+	struct vfio_spapr_eeh_addr_lookup lookup = {
+		.res_type = res_type,
+		.addr = addr,
+	};
+	int ret;
+
+	ret = iommu_group_for_each_dev(group, &lookup,
+				       vfio_spapr_eeh_match_dev);
+	if (ret < 0)
+		return ret;
+
+	if (lookup.found) {
+		*normalized = lookup.normalized;
+		return 0;
+	}
+
+	pr_debug("vfio EEH: addr=0x%lx not found in any matching BAR of group\n",
+		 addr);
+	return -EINVAL;
+}
+
 static long vfio_spapr_ioctl_eeh_pe_op(struct iommu_group *group,
 				       unsigned long arg)
 {
 	struct eeh_pe *pe;
 	struct vfio_eeh_pe_op op;
-	unsigned long minsz;
+	unsigned long minsz, res_type;
 
 	pe = eeh_iommu_group_to_pe(group);
 	if (!pe)
@@ -818,6 +924,29 @@ static long vfio_spapr_ioctl_eeh_pe_op(struct iommu_group *group,
 		if (copy_from_user(&op, (void __user *)arg, minsz))
 			return -EFAULT;
 
+		/*
+		 * Only the pSeries RTAS backend takes a PCI bus address, so
+		 * PowerNV requests are passed through unchanged.  Config and
+		 * DMA functions do not carry BAR addresses, and an address
+		 * and mask of zero carries no address.
+		 */
+		res_type = eeh_err_func_to_res_type(op.err.func);
+		if (machine_is(pseries) && res_type &&
+		    (op.err.type == EEH_ERR_TYPE_32 ||
+		     op.err.type == EEH_ERR_TYPE_64) &&
+		    (op.err.addr || op.err.mask)) {
+			unsigned long normalized;
+			int ret;
+
+			ret = vfio_spapr_eeh_normalize_addr(group, res_type,
+							    op.err.addr,
+							    &normalized);
+			if (ret)
+				return ret;
+
+			op.err.addr = normalized;
+		}
+
 		return eeh_pe_inject_err(pe, op.err.type, op.err.func,
 					 op.err.addr, op.err.mask);
 	default:
-- 
2.54.0


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH v7 4/4] powerpc/powernv/eeh: Map generic EEH error types to OPAL
  2026-11-23 10:22 [PATCH v7 0/4] powerpc: Add RTAS based error injection support for pSeries Narayana Murty N
                   ` (2 preceding siblings ...)
  2026-11-23 10:22 ` [PATCH v7 3/4] vfio/spapr_tce: Normalize EEH IOA error injection addresses Narayana Murty N
@ 2026-11-23 10:22 ` Narayana Murty N
  3 siblings, 0 replies; 6+ messages in thread
From: Narayana Murty N @ 2026-11-23 10:22 UTC (permalink / raw)
  To: Madhavan Srinivasan, Oliver O'Halloran, Timothy Pearson,
	Alex Williamson, sourabhjain
  Cc: Michael Ellerman, Nicholas Piggin, Christophe Leroy,
	Ritesh Harjani, Shrikanth Hegde, linuxppc-dev, linux-kernel, kvm,
	sbhat, harshpb

eeh_pe_inject_err() is called with generic EEH error types defined in
the VFIO/EEH userspace ABI (EEH_ERR_TYPE_32, EEH_ERR_TYPE_64). The
PowerNV backend previously compared these generic values directly against
OPAL firmware constants (OPAL_ERR_INJECT_TYPE_IOA_BUS_ERR and
OPAL_ERR_INJECT_TYPE_IOA_BUS_ERR64).

This is fragile: it relies on the generic EEH ABI values being
numerically identical to OPAL-specific values, and would silently
misbehave if they ever diverged.

The preceding patches introduce RTAS-based error injection on pSeries,
mapping generic EEH error types to PAPR error-injection classes. Apply
the same explicit translation in the PowerNV backend, which uses OPAL
instead of RTAS.

Add an explicit mapping in pnv_eeh_err_inject() that translates:
EEH_ERR_TYPE_32 -> OPAL_ERR_INJECT_TYPE_IOA_BUS_ERR
EEH_ERR_TYPE_64 -> OPAL_ERR_INJECT_TYPE_IOA_BUS_ERR64

Return -ERANGE for unsupported error types, preserving the behavior
of the original code. This decouples the VFIO/EEH interface from OPAL
implementation details.

No change to the existing userspace ABI or PowerNV behavior.

Signed-off-by: Narayana Murty N <nnmlinux@linux.ibm.com>
---
 arch/powerpc/platforms/powernv/eeh-powernv.c | 22 +++++++++++++++-----
 1 file changed, 17 insertions(+), 5 deletions(-)

diff --git a/arch/powerpc/platforms/powernv/eeh-powernv.c b/arch/powerpc/platforms/powernv/eeh-powernv.c
index db3370d1673c..01dae760cecb 100644
--- a/arch/powerpc/platforms/powernv/eeh-powernv.c
+++ b/arch/powerpc/platforms/powernv/eeh-powernv.c
@@ -1167,12 +1167,24 @@ static int pnv_eeh_err_inject(struct eeh_pe *pe, int type, int func,
 {
 	struct pci_controller *hose = pe->phb;
 	struct pnv_phb *phb = hose->private_data;
+	int opal_err_type;
 	s64 rc;
 
-	if (type != OPAL_ERR_INJECT_TYPE_IOA_BUS_ERR &&
-	    type != OPAL_ERR_INJECT_TYPE_IOA_BUS_ERR64) {
-		pr_warn("%s: Invalid error type %d\n",
-			__func__, type);
+	/*
+	 * eeh_pe_inject_err() receives generic EEH error types from the
+	 * VFIO/EEH userspace ABI.  The PowerNV backend must not rely on
+	 * these generic values being numerically identical to OPAL firmware
+	 * values.  Translate to OPAL-specific types before calling firmware.
+	 */
+	switch (type) {
+	case EEH_ERR_TYPE_32:
+		opal_err_type = OPAL_ERR_INJECT_TYPE_IOA_BUS_ERR;
+		break;
+	case EEH_ERR_TYPE_64:
+		opal_err_type = OPAL_ERR_INJECT_TYPE_IOA_BUS_ERR64;
+		break;
+	default:
+		pr_warn("%s: Invalid error type %d\n", __func__, type);
 		return -ERANGE;
 	}
 
@@ -1192,7 +1204,7 @@ static int pnv_eeh_err_inject(struct eeh_pe *pe, int type, int func,
 
 	/* Do error injection */
 	rc = opal_pci_err_inject(phb->opal_id, pe->addr,
-				 type, func, addr, mask);
+				 opal_err_type, func, addr, mask);
 	if (rc != OPAL_SUCCESS) {
 		pr_warn("%s: Failure %lld injecting error "
 			"%d-%d to PHB#%x-PE#%x\n",
-- 
2.54.0


^ permalink raw reply related	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2026-11-23 10:24 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-23 10:22 [PATCH v7 0/4] powerpc: Add RTAS based error injection support for pSeries Narayana Murty N
2026-11-23 10:22 ` [PATCH v7 1/4] powerpc/rtas: Handle ibm,open-errinjct return format Narayana Murty N
2026-11-23 10:22 ` [PATCH v7 2/4] powerpc/pseries/eeh: Implement RTAS-based EEH error injection Narayana Murty N
2026-11-23 10:22 ` [PATCH v7 3/4] vfio/spapr_tce: Normalize EEH IOA error injection addresses Narayana Murty N
2026-11-23 10:22 ` [PATCH v7 4/4] powerpc/powernv/eeh: Map generic EEH error types to OPAL Narayana Murty N
  -- strict thread matches above, loose matches on Subject: below --
2026-10-23  7:36 [PATCH v6 0/5] powerpc: Add RTAS based error injection support for pSeries Narayana Murty N
2026-11-23  8:47 ` [PATCH v7 3/4] vfio/spapr_tce: Normalize EEH IOA error injection addresses Narayana Murty N

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox