[PATCH v16 02/31] [RFC] firmware: arm_scmi: Additional iterators consistency checks

From: Cristian Marussi

Date: Sat Nov 21 2026 - 18:43:42 EST


Add a few more consistency checks around the number of returned and
remaining items during the life of an iteration: this is useful to spot
prematurely terminated iterations that would produce shorter than expected
results.

Handling these anomalies in the platform multi-part replies within the
core SCMI iterators machinery avoids the need to address such issues
multiple times at the protocol layer.

Signed-off-by: Cristian Marussi <cristian.marussi@xxxxxxx>
---
RFC because this is another fix that is potentially disrupting for any
non compliant SCMI platform deployed in the wild.
---
drivers/firmware/arm_scmi/driver.c | 38 ++++++++++++++++++++++++++----
1 file changed, 33 insertions(+), 5 deletions(-)

diff --git a/drivers/firmware/arm_scmi/driver.c b/drivers/firmware/arm_scmi/driver.c
index 55d9cc487916..a9b0f704c39b 100644
--- a/drivers/firmware/arm_scmi/driver.c
+++ b/drivers/firmware/arm_scmi/driver.c
@@ -1852,7 +1852,7 @@ static int __scmi_iterator_run(void *iter, unsigned int *start, unsigned int *en
const struct scmi_protocol_handle *ph;
struct scmi_iterator_state *st;
struct scmi_iterator *i;
- unsigned int n;
+ unsigned int n, i_start, i_max, i_returned;

if (!iter)
return -EINVAL;
@@ -1862,10 +1862,15 @@ static int __scmi_iterator_run(void *iter, unsigned int *start, unsigned int *en
ph = i->ph;
st = &i->state;

+ /* Initialize iteration counters used to check consistency */
+ i_max = 0;
+ i_returned = 0;
+ i_start = start ? *start : 0;
+
/* Reinitialize state for next run */
st->num_returned = 0;
st->num_remaining = 0;
- st->desc_index = start ? *start : 0;
+ st->desc_index = i_start;

do {
iops->prepare_message(i->msg, st->desc_index, i->priv);
@@ -1883,12 +1888,35 @@ static int __scmi_iterator_run(void *iter, unsigned int *start, unsigned int *en
if (ret)
return ret;

- if (!st->num_returned && st->num_remaining) {
- dev_err(ph->dev, "Inconsistent multi-part reply.\n");
+ /*
+ * Initialize max items expectation for this run based on
+ * values returned on first message exchange: during the
+ * following iterations consistency will be validated against
+ * these parameters, so as to spot FW anomalies like sudden
+ * termination or unplausible conditions.
+ */
+ if (!i_max)
+ i_max = i_start + st->num_returned + st->num_remaining;
+
+ if ((!st->num_returned && st->num_remaining) ||
+ (i_max != i_start + i_returned + st->num_returned + st->num_remaining)) {
+ dev_err(ph->dev,
+ "Inconsistent multi-part reply. max:%u i_max:%u i_start:%u i_returned:%u returned:%u remaining:%u desc_index:%u\n",
+ st->max_resources, i_max, i_start, i_returned,
+ st->num_returned, st->num_remaining,
+ st->desc_index);
return -EINVAL;
}

- if (st->num_returned > st->max_resources - st->desc_index) {
+ dev_dbg(ph->dev,
+ "Multi-part - max:%u i_max:%u i_start:%u i_returned:%u returned:%u remaining:%u desc_index:%u\n",
+ st->max_resources, i_max, i_start, i_returned,
+ st->num_returned, st->num_remaining, st->desc_index);
+
+ /* Account for last batch received */
+ i_returned += st->num_returned;
+
+ if (st->num_returned + st->desc_index > st->max_resources) {
dev_err(ph->dev,
"No. of resources can't exceed %d\n",
st->max_resources);
--
2.54.0