[PATCH CFT] usb: ucsi_ccg: Fix command completion handling

From: Christian A. Ehrhardt
Date: Thu Feb 15 2024 - 05:26:14 EST


In case of a spurious or otherwise delayed interrupt
it is possible that CCI still reports the previous completion.
For this reason the UCSI spec provides different completion
bits for normal commands and for UCSI_ACK_CC_CI.

Only complete a sync command if the correct completion bit
is set.

This should avoid the need to clear out CCI before starting
a command. Thus remove this code.

Signed-off-by: Christian A. Ehrhardt <lk@xxxxxxx>
Fixes: e32fd989ac1c ("usb: typec: ucsi: ccg: Move to the new API")
---
Additional information:
A similar change for ucsi_acpi.c is here:
https://lore.kernel.org/all/20240121204123.275441-3-lk@xxxxxxx/
This restores behaviour that ucsi.c had before moving to the new API.
I've seen timeouts with ucsi_acpi.c without that fix, often if there
were many port events (plug/unplug).

I do _not_ have CCG hardware to test this. So someone else will have to
provide a Tested-By tag or similar (hence the CFT in the subject).

But from looking at the code I think this change is needed for CCG,
too. Additionally, the recent change to CCG here
https://lore.kernel.org/all/20240126030115.3791554-1-haotienh@xxxxxxxxxx/
seems to work around the same problem.

Clearing the cached CCI value should not be necessary with this
anymore and I suspect that it can potentially cause other problems.
However, I can send an update patch without this hunk if desired.


drivers/usb/typec/ucsi/ucsi_ccg.c | 19 ++++++++-----------
1 file changed, 8 insertions(+), 11 deletions(-)

diff --git a/drivers/usb/typec/ucsi/ucsi_ccg.c b/drivers/usb/typec/ucsi/ucsi_ccg.c
index dda7c7c94e08..9442307e0abd 100644
--- a/drivers/usb/typec/ucsi/ucsi_ccg.c
+++ b/drivers/usb/typec/ucsi/ucsi_ccg.c
@@ -616,14 +616,6 @@ static int ucsi_ccg_async_write(struct ucsi *ucsi, unsigned int offset,
struct ucsi_ccg *uc = ucsi_get_drvdata(ucsi);
u16 reg = CCGX_RAB_UCSI_DATA_BLOCK(offset);

- /*
- * UCSI may read CCI instantly after async_write,
- * clear CCI to avoid caller getting wrong data before we get CCI from ISR
- */
- spin_lock(&uc->op_lock);
- uc->op_data.cci = 0;
- spin_unlock(&uc->op_lock);
-
return ccg_write(uc, reg, val, val_len);
}

@@ -708,9 +700,14 @@ static irqreturn_t ccg_irq_handler(int irq, void *data)
err_clear_irq:
ccg_write(uc, CCGX_RAB_INTR_REG, &intr_reg, sizeof(intr_reg));

- if (!ret && test_bit(DEV_CMD_PENDING, &uc->flags) &&
- cci & (UCSI_CCI_ACK_COMPLETE | UCSI_CCI_COMMAND_COMPLETE))
- complete(&uc->complete);
+ if (!ret && test_bit(DEV_CMD_PENDING, &uc->flags)) {
+ bool ack = UCSI_COMMAND(uc->last_cmd_sent) == UCSI_ACK_CC_CI;
+
+ if (ack && (cci & UCSI_CCI_ACK_COMPLETE))
+ complete(&uc->complete);
+ if (!ack && (cci & UCSI_CCI_COMMAND_COMPLETE))
+ complete(&uc->complete);
+ }

return IRQ_HANDLED;
}
--
2.40.1