diff --git a/drivers/usb/typec/tipd/Kconfig b/drivers/usb/typec/tipd/Kconfig index cbc177901c71e..9f68c60d88caf 100644 --- a/drivers/usb/typec/tipd/Kconfig +++ b/drivers/usb/typec/tipd/Kconfig @@ -55,7 +55,10 @@ config TYPEC_TIPD_KUNIT_TEST shared with the driver, including aborted and repeated sleep cycles. Also test that interrupt events which could not be read or cleared are still acknowledged, however long that keeps failing, so that the - controller keeps raising its interrupt. + controller keeps raising its interrupt. Also test that a polled + controller whose plug changed without an event, or whose plug was + never applied, has its port state read again, once for each plug + state. Also test that a USB4/Thunderbolt router which has not registered its switch does not keep the port from probing. Also test that a Type-C state which a mux provider with transient @@ -79,6 +82,11 @@ config TYPEC_SN201202X_KUNIT_TEST for system suspend. Also test which data status and status count as a link that wakes the system from suspend-to-idle. Liquid-detection coverage includes status decode and the ordering of controller - mitigation, dry revalidation and connector-state reporting. These tests - run on a simulated SPMI bus, without accessing a physical Type-C - controller. + mitigation, dry revalidation and connector-state reporting, and that + a measurement without an earlier result does not force mitigation + on, is made to start again after one bounded wait and enables the + port only after a second one, never for a port known to be wet, and + that mitigation is never removed from the suspend-to-idle busy-wait, + nor put on a port reported dry for a single failed status read. + These tests run on a simulated SPMI bus, without accessing a + physical Type-C controller. diff --git a/drivers/usb/typec/tipd/cd321x-irq-test.c b/drivers/usb/typec/tipd/cd321x-irq-test.c index 3da4f902623f2..23094cb971ba7 100644 --- a/drivers/usb/typec/tipd/cd321x-irq-test.c +++ b/drivers/usb/typec/tipd/cd321x-irq-test.c @@ -390,6 +390,152 @@ static void cd321x_resume_acknowledges_events_test(struct kunit *test) KUNIT_EXPECT_EQ(test, t->ctrl.interrupts, 2U); } +/* A polled controller whose plug event never reached the driver */ +static void cd321x_poll_finds_lost_plug_test(struct kunit *test) +{ + struct cd321x_test *t = cd321x_test_port(test); + + t->ctrl.status = TPS_STATUS_PLUG_PRESENT; + KUNIT_EXPECT_EQ(test, cd321x_test_interrupt(t), IRQ_NONE); + cd321x_test_recover(test, t); + KUNIT_EXPECT_TRUE(test, t->cd.tps.status & TPS_STATUS_PLUG_PRESENT); + KUNIT_EXPECT_TRUE(test, t->cd.update_status.status_changed & TPS_STATUS_PLUG_PRESENT); + + /* Applied: the next poll finds nothing to do. */ + cd321x_test_interrupt(t); + KUNIT_EXPECT_FALSE(test, delayed_work_pending(&t->cd.resume_work)); +} + +static void cd321x_poll_finds_lost_unplug_test(struct kunit *test) +{ + struct cd321x_test *t = cd321x_test_port(test); + + t->cd.tps.status = TPS_STATUS_PLUG_PRESENT; + t->cd.update_status.status = TPS_STATUS_PLUG_PRESENT; + cd321x_test_interrupt(t); + cd321x_test_recover(test, t); + KUNIT_EXPECT_FALSE(test, t->cd.tps.status & TPS_STATUS_PLUG_PRESENT); + KUNIT_EXPECT_TRUE(test, t->cd.update_status.status_changed & TPS_STATUS_PLUG_PRESENT); +} + +static void cd321x_poll_same_plug_test(struct kunit *test) +{ + struct cd321x_test *t = cd321x_test_port(test); + + /* Other status bits change without an event too: only the plug counts. */ + t->ctrl.status = TPS_STATUS_PLUG_UPSIDE_DOWN; + cd321x_test_interrupt(t); + KUNIT_EXPECT_FALSE(test, delayed_work_pending(&t->cd.resume_work)); + KUNIT_EXPECT_EQ(test, t->cd.pm.phase, CD321X_PM_RUNNING); +} + +static const struct tipd_contaminant_ops cd321x_test_contaminant_ops = {}; + +static void cd321x_poll_plug_not_dry_test(struct kunit *test) +{ + struct cd321x_test *t = cd321x_test_port(test); + + /* A plug on a connector not known to be dry is kept down, as connect does. */ + t->cd.tps.contaminant_ops = &cd321x_test_contaminant_ops; + t->cd.tps.contaminant = TYPEC_CONTAMINANT_UNKNOWN; + t->ctrl.status = TPS_STATUS_PLUG_PRESENT; + cd321x_test_interrupt(t); + KUNIT_EXPECT_FALSE(test, delayed_work_pending(&t->cd.resume_work)); + + /* Found dry with no event of its own: the plug is applied. */ + t->cd.tps.contaminant = TYPEC_CONTAMINANT_NONE; + cd321x_test_interrupt(t); + cd321x_test_recover(test, t); + KUNIT_EXPECT_TRUE(test, t->cd.tps.status & TPS_STATUS_PLUG_PRESENT); +} + +static void cd321x_poll_plug_during_revalidation_test(struct kunit *test) +{ + struct cd321x_test *t = cd321x_test_port(test); + + /* A revalidation under way is not requeued, which would delay it. */ + t->cd.pm.phase = CD321X_PM_REVALIDATE; + t->cd.pm.attempts_left = CD321X_RESUME_ATTEMPTS; + t->ctrl.status = TPS_STATUS_PLUG_PRESENT; + cd321x_test_interrupt(t); + KUNIT_EXPECT_FALSE(test, delayed_work_pending(&t->cd.resume_work)); + + /* One that gave up is restarted by the plug it missed. */ + t->cd.pm.phase = CD321X_PM_STALE; + t->cd.pm.attempts_left = 0; + cd321x_test_interrupt(t); + cd321x_test_recover(test, t); + KUNIT_EXPECT_TRUE(test, t->cd.tps.status & TPS_STATUS_PLUG_PRESENT); +} + +/* Snapshot taken, but the update that registers the partner gave up */ +static void cd321x_poll_plug_without_partner_test(struct kunit *test) +{ + struct cd321x_test *t = cd321x_test_port(test); + + t->ctrl.status = TPS_STATUS_PLUG_PRESENT; + t->cd.tps.status = TPS_STATUS_PLUG_PRESENT; + t->cd.update_status.status = TPS_STATUS_PLUG_PRESENT; + + /* Not while the update is still to run */ + mod_delayed_work(system_freezable_wq, &t->cd.update_work, 60 * HZ); + cd321x_test_interrupt(t); + KUNIT_EXPECT_FALSE(test, delayed_work_pending(&t->cd.resume_work)); + cancel_delayed_work_sync(&t->cd.update_work); + + cd321x_test_interrupt(t); + cd321x_test_recover(test, t); + KUNIT_EXPECT_TRUE(test, t->cd.tps.status & TPS_STATUS_PLUG_PRESENT); +} + +/* One poll-requested read per plug state that is not applied */ +static void cd321x_poll_revalidates_once_test(struct kunit *test) +{ + struct cd321x_test *t = cd321x_test_port(test); + + t->cd.pm.phase = CD321X_PM_STALE; + t->ctrl.status = TPS_STATUS_PLUG_PRESENT; + t->cd.tps.status = TPS_STATUS_PLUG_PRESENT; + cd321x_test_interrupt(t); + KUNIT_EXPECT_TRUE(test, delayed_work_pending(&t->cd.resume_work)); + + /* That read applied the snapshot, but the update gave up again. */ + cancel_delayed_work_sync(&t->cd.resume_work); + t->cd.pm.phase = CD321X_PM_STALE; + t->cd.pm.attempts_left = 0; + cd321x_test_interrupt(t); + cd321x_test_interrupt(t); + KUNIT_EXPECT_FALSE(test, delayed_work_pending(&t->cd.resume_work)); + KUNIT_EXPECT_EQ(test, t->cd.pm.phase, CD321X_PM_STALE); + + /* A different plug state gets its own read. */ + t->ctrl.status = 0; + cd321x_test_interrupt(t); + KUNIT_EXPECT_TRUE(test, delayed_work_pending(&t->cd.resume_work)); + cancel_delayed_work_sync(&t->cd.resume_work); + + /* Once the state is applied, a later loss is recovered again. */ + t->cd.pm.phase = CD321X_PM_STALE; + t->cd.tps.status = 0; + cd321x_test_interrupt(t); + KUNIT_EXPECT_FALSE(test, t->cd.poll_revalidated); + t->ctrl.status = TPS_STATUS_PLUG_PRESENT; + cd321x_test_interrupt(t); + KUNIT_EXPECT_TRUE(test, delayed_work_pending(&t->cd.resume_work)); +} + +static void cd321x_poll_status_read_fails_test(struct kunit *test) +{ + struct cd321x_test *t = cd321x_test_port(test); + + t->cd.tps.status = TPS_STATUS_PLUG_PRESENT; + t->ctrl.failing_status_reads = 1; + cd321x_test_interrupt(t); + /* Not taken for an unplug */ + KUNIT_EXPECT_FALSE(test, delayed_work_pending(&t->cd.resume_work)); + KUNIT_EXPECT_EQ(test, t->cd.pm.phase, CD321X_PM_RUNNING); +} + static struct kunit_case cd321x_irq_cases[] = { KUNIT_CASE(cd321x_irq_acknowledges_events_test), KUNIT_CASE(cd321x_failed_event_read_recovers_test), @@ -402,6 +548,14 @@ static struct kunit_case cd321x_irq_cases[] = { KUNIT_CASE(cd321x_ack_retry_across_sleep_test), KUNIT_CASE(cd321x_ack_retry_ends_on_removal_test), KUNIT_CASE(cd321x_resume_acknowledges_events_test), + KUNIT_CASE(cd321x_poll_finds_lost_plug_test), + KUNIT_CASE(cd321x_poll_finds_lost_unplug_test), + KUNIT_CASE(cd321x_poll_same_plug_test), + KUNIT_CASE(cd321x_poll_plug_not_dry_test), + KUNIT_CASE(cd321x_poll_plug_during_revalidation_test), + KUNIT_CASE(cd321x_poll_plug_without_partner_test), + KUNIT_CASE(cd321x_poll_revalidates_once_test), + KUNIT_CASE(cd321x_poll_status_read_fails_test), {} }; diff --git a/drivers/usb/typec/tipd/core.c b/drivers/usb/typec/tipd/core.c index d9ece372d36a4..f9321bb35b097 100644 --- a/drivers/usb/typec/tipd/core.c +++ b/drivers/usb/typec/tipd/core.c @@ -1755,6 +1755,26 @@ static void cd321x_event_lost(struct cd321x *cd321x) msecs_to_jiffies(CD321X_DEBOUNCE_DELAY_MS)); } +/** + * cd321x_revalidate - Read the cable state afresh and apply it + * @tps: The controller + * + * For a change that the controller signals with no event of its own, such as + * a new liquid-detection result read on a retry. The resume work alone does + * nothing while the port is running: it reads only in a revalidation. + * + * Context: tps->lock held. + */ +void cd321x_revalidate(struct tps6598x *tps) +{ + struct cd321x *cd321x = container_of(tps, struct cd321x, tps); + + lockdep_assert_held(&tps->lock); + if (cd321x_pm_event_lost(&cd321x->pm)) + mod_delayed_work(system_freezable_wq, &cd321x->resume_work, 0); +} +EXPORT_SYMBOL_GPL(cd321x_revalidate); + /* * An attached cable need not generate an IRQ after sleep. Read a fresh * snapshot and reapply it with the mode cache invalidated, allowing the @@ -2276,6 +2296,55 @@ int cd321x_wake_events(struct tps6598x *tps, u8 event[CD321X_WAKE_EVENT_LEN], u3 } EXPORT_SYMBOL_GPL(cd321x_wake_events); +/* + * The plug event is an edge: one that never reaches the driver, or that is + * acknowledged while the cable state behind it is not applied, leaves the + * port without a partner (or with a stale one) until the next replug. A + * polled controller has no interrupt line to latch it, so on every poll + * without an event compare the plug state, masked as cd321x_connect() masks + * it, with both the last snapshot and whether a partner is registered: the + * snapshot is taken before the update applies it, and an update that gave + * up leaves the two apart. If either differs, read the whole state again. + * + * Not while a revalidation or an update is already under way: its read + * settles the state, and requeueing it every poll would keep pushing it + * back. And only once for each plug state not applied: a revalidation that + * gives up again waits for the next event or plug change, as before, rather + * than being restarted every 500 ms. + * + * Context: tps->lock held. + */ +static void cd321x_poll_plug_level(struct cd321x *cd321x) +{ + struct tps6598x *tps = &cd321x->tps; + bool plug; + u32 status; + + lockdep_assert_held(&tps->lock); + if ((cd321x->pm.phase != CD321X_PM_RUNNING && + cd321x->pm.phase != CD321X_PM_STALE) || + delayed_work_pending(&cd321x->update_work)) + return; + /* A failed read here is only a missed comparison; the next poll tries again. */ + if (tps6598x_read32(tps, TPS_REG_STATUS, &status)) + return; + if (tps->contaminant_ops && tps->contaminant != TYPEC_CONTAMINANT_NONE) + status &= ~TPS_STATUS_PLUG_PRESENT; + plug = status & TPS_STATUS_PLUG_PRESENT; + if (plug == !!(tps->status & TPS_STATUS_PLUG_PRESENT) && plug == !!tps->partner) { + cd321x->poll_revalidated = false; + return; + } + if (cd321x->poll_revalidated && cd321x->poll_revalidated_plug == plug) + return; + + cd321x->poll_revalidated = true; + cd321x->poll_revalidated_plug = plug; + dev_info_ratelimited(tps->dev, "plug %s without an event, reading the port again\n", + plug ? "present" : "gone"); + cd321x_event_lost(cd321x); +} + static irqreturn_t cd321x_interrupt(int irq, void *data) { struct tps6598x *tps = data; @@ -2296,8 +2365,11 @@ static irqreturn_t cd321x_interrupt(int irq, void *data) } trace_cd321x_irq(event); - if (!event) + if (!event) { + if (!irq) + cd321x_poll_plug_level(cd321x); goto err_unlock; + } /* A cable came or went: a wake's first read is no longer a quiet one, see cd321x_connect() */ if (event & APPLE_CD_REG_INT_PLUG_EVENT) cd321x->wake_apply = false; @@ -3632,6 +3704,7 @@ static int __cd321x_reset_inherited_connection(struct tps6598x *tps, const u8 ke u8 delay = 5, result; unsigned long timeout; int ret, lock_ret, mode; + u32 fresh; mode = tps6598x_check_mode(tps); if (mode != TPS_MODE_APP && mode != TPS_MODE_APP1) @@ -3671,9 +3744,11 @@ static int __cd321x_reset_inherited_connection(struct tps6598x *tps, const u8 ke 5000, 0); if (ret || lock_ret) return ret ?: lock_ret; - if (!tps6598x_read_status(tps, status)) + /* Into a copy: a failed read must not replace the status read before. */ + if (!tps6598x_read_status(tps, &fresh)) return -EIO; - tps->status = *status; + *status = fresh; + tps->status = fresh; dev_info(tps->dev, "reinitialized inherited connection\n"); return 0; } @@ -3682,6 +3757,7 @@ static int cd321x_reset_inherited_connection(struct tps6598x *tps, u32 *status) { const struct cd321x_inherited_board *board; bool best_effort = true; + u32 fresh; u8 key[4]; int ret; @@ -3706,9 +3782,37 @@ static int cd321x_reset_inherited_connection(struct tps6598x *tps, u32 *status) return ret; dev_warn(tps->dev, "inherited connection not reinitialized: %d\n", ret); - if (!tps6598x_read_status(tps, status)) - return -EIO; - tps->status = *status; + /* + * Keep the status read before on a bus error: the port's ->ready() + * reads the cable state again anyway, and best effort must not turn + * one failed read into a failed probe. Read into a copy: the Apple + * SPMI controller stores a frame before it checks its parity, so a + * failed read leaves garbage in its buffer. + */ + if (tps6598x_read_status(tps, &fresh)) { + *status = fresh; + tps->status = fresh; + } + return 0; +} + +/* + * The power and data status of a plug present at probe. A transient bus error + * (on SPMI, a frame that fails its parity check) is retried a few times. + */ +#define TIPD_PROBE_READ_TRIES 3 +#define TIPD_PROBE_READ_RETRY_MS 20 + +static int tipd_read_plug_state(struct tps6598x *tps) +{ + unsigned int tries = TIPD_PROBE_READ_TRIES; + + while (!tps->data->read_power_status(tps) || + !tps->data->read_data_status(tps)) { + if (!--tries) + return -EIO; + msleep(TIPD_PROBE_READ_RETRY_MS); + } return 0; } @@ -4162,15 +4266,34 @@ int tipd_init(struct tps6598x *tps) goto err_disconnect; if (status & TPS_STATUS_PLUG_PRESENT) { - ret = -EINVAL; - if (!tps->data->read_power_status(tps)) - goto err_disconnect; - if (!tps->data->read_data_status(tps)) - goto err_disconnect; - ret = tps->data->connect(tps, status); - if (ret) { - dev_err(tps->dev, "failed to register partner\n"); + ret = tipd_read_plug_state(tps); + if (ret && tps->data->ready) { + /* + * ->ready() reads the whole cable state again and applies + * it, with its own retries, so this read only delays the + * partner. Failing the probe for it left the port without + * a driver until the next boot: on a J716s, one SPMI parity + * error with a hub plugged in at boot. + */ + dev_warn(tps->dev, + "port state unreadable at probe: %d, reading it again once ready\n", + ret); + /* + * Not applied: if that revalidation gives up too, a + * later read still finds the plug new. + */ + tps->status &= ~TPS_STATUS_PLUG_PRESENT; + /* Not a probe error: the polled path below keeps ret */ + ret = 0; + } else if (ret) { + ret = -EINVAL; goto err_disconnect; + } else { + ret = tps->data->connect(tps, status); + if (ret) { + dev_err(tps->dev, "failed to register partner\n"); + goto err_disconnect; + } } } diff --git a/drivers/usb/typec/tipd/sn201202x-pm-test.c b/drivers/usb/typec/tipd/sn201202x-pm-test.c index 82bd1d8df528a..7246e01428c21 100644 --- a/drivers/usb/typec/tipd/sn201202x-pm-test.c +++ b/drivers/usb/typec/tipd/sn201202x-pm-test.c @@ -689,12 +689,24 @@ enum sn201202x_ldcm_test_action { LDCM_TEST_PUBLISH_UNKNOWN, LDCM_TEST_PUBLISH_NONE, LDCM_TEST_PUBLISH_LIQUID, + LDCM_TEST_OVERDUE, + LDCM_TEST_RERUN, + LDCM_TEST_HELD, + LDCM_TEST_NOT_HELD, }; struct sn201202x_ldcm_test { enum typec_contaminant reads[2]; int read_rets[2]; int mitigation_rets[2]; + /* Waits overrun in a row, as the wait for a first result answers */ + unsigned int overdue; + int rerun_ret; + /* Liquid reported since the last dry result: kept as the driver keeps it */ + bool wet; + unsigned int results; + /* Status reads that failed in a row, as the driver counts them */ + unsigned int read_failures; enum sn201202x_ldcm_test_action actions[8]; unsigned int read_count; unsigned int mitigation_count; @@ -740,10 +752,63 @@ static void sn201202x_ldcm_test_publish(void *ctx, } } +static void sn201202x_ldcm_test_result(void *ctx, + enum typec_contaminant contaminant) +{ + struct sn201202x_ldcm_test *t = ctx; + + t->results++; + t->wet = contaminant == TYPEC_CONTAMINANT_LIQUID; +} + +static bool sn201202x_ldcm_test_wet(void *ctx) +{ + struct sn201202x_ldcm_test *t = ctx; + + return t->wet; +} + +static unsigned int sn201202x_ldcm_test_overdue(void *ctx) +{ + struct sn201202x_ldcm_test *t = ctx; + + t->actions[t->action_count++] = LDCM_TEST_OVERDUE; + return t->overdue; +} + +static void sn201202x_ldcm_test_held(void *ctx, bool mitigated) +{ + struct sn201202x_ldcm_test *t = ctx; + + t->actions[t->action_count++] = mitigated ? LDCM_TEST_HELD : + LDCM_TEST_NOT_HELD; +} + +static int sn201202x_ldcm_test_rerun(void *ctx) +{ + struct sn201202x_ldcm_test *t = ctx; + + t->actions[t->action_count++] = LDCM_TEST_RERUN; + return t->rerun_ret; +} + +static unsigned int sn201202x_ldcm_test_read_failures(void *ctx) +{ + struct sn201202x_ldcm_test *t = ctx; + + return t->read_failures; +} + static const struct sn201202x_ldcm_ops sn201202x_ldcm_test_ops = { .read = sn201202x_ldcm_test_read, .set_mitigation = sn201202x_ldcm_test_mitigate, .publish = sn201202x_ldcm_test_publish, + .result = sn201202x_ldcm_test_result, + .wet = sn201202x_ldcm_test_wet, + .overdue = sn201202x_ldcm_test_overdue, + .held = sn201202x_ldcm_test_held, + .rerun = sn201202x_ldcm_test_rerun, + .read_failures = sn201202x_ldcm_test_read_failures, }; static void sn201202x_ldcm_test_actions(struct kunit *test, @@ -770,6 +835,7 @@ static void sn201202x_ldcm_liquid_order_test(struct kunit *test) &sn201202x_ldcm_test_ops, TYPEC_CONTAMINANT_UNKNOWN), 0); sn201202x_ldcm_test_actions(test, &t, actions, ARRAY_SIZE(actions)); + KUNIT_EXPECT_TRUE(test, t.wet); } static void sn201202x_ldcm_read_failure_order_test(struct kunit *test) @@ -782,6 +848,8 @@ static void sn201202x_ldcm_read_failure_order_test(struct kunit *test) struct sn201202x_ldcm_test t = { .reads = { TYPEC_CONTAMINANT_UNKNOWN }, .read_rets = { -EIO }, + /* A failed read is never taken for an overdue measurement. */ + .overdue = 2, }; KUNIT_EXPECT_EQ(test, sn201202x_ldcm_update_state(&t, @@ -819,12 +887,16 @@ static void sn201202x_ldcm_dry_order_test(struct kunit *test) }; struct sn201202x_ldcm_test t = { .reads = { TYPEC_CONTAMINANT_NONE, TYPEC_CONTAMINANT_NONE }, + .wet = true, }; + /* A dry result ends what liquid reported before it. */ KUNIT_EXPECT_EQ(test, sn201202x_ldcm_update_state(&t, &sn201202x_ldcm_test_ops, - TYPEC_CONTAMINANT_UNKNOWN), 0); + TYPEC_CONTAMINANT_LIQUID), 0); sn201202x_ldcm_test_actions(test, &t, actions, ARRAY_SIZE(actions)); + KUNIT_EXPECT_FALSE(test, t.wet); + KUNIT_EXPECT_EQ(test, t.results, 2U); } static void sn201202x_ldcm_dry_then_busy_test(struct kunit *test) @@ -895,6 +967,7 @@ static void sn201202x_ldcm_dry_race_test(struct kunit *test) &sn201202x_ldcm_test_ops, TYPEC_CONTAMINANT_UNKNOWN), 0); sn201202x_ldcm_test_actions(test, &t, actions, ARRAY_SIZE(actions)); + KUNIT_EXPECT_TRUE(test, t.wet); } static void sn201202x_ldcm_dry_race_mitigation_failure_test(struct kunit *test) @@ -929,10 +1002,14 @@ static void sn201202x_ldcm_busy_keeps_result_test(struct kunit *test) }; unsigned int i; - /* A running measurement neither mitigates nor republishes anything. */ + /* + * A running measurement neither mitigates nor republishes anything. + * With a result, however long it runs: liquid stays mitigated. + */ for (i = 0; i < ARRAY_SIZE(last); i++) { struct sn201202x_ldcm_test t = { .reads = { TYPEC_CONTAMINANT_UNKNOWN }, + .overdue = 2, }; KUNIT_EXPECT_EQ(test, sn201202x_ldcm_update_state(&t, @@ -948,19 +1025,718 @@ static void sn201202x_ldcm_busy_without_result_test(struct kunit *test) { const enum sn201202x_ldcm_test_action actions[] = { LDCM_TEST_READ, + LDCM_TEST_OVERDUE, + }; + struct sn201202x_ldcm_test t = { + .reads = { TYPEC_CONTAMINANT_UNKNOWN }, + }; + + /* + * Without an earlier result the port stays down (unknown is already + * published), but mitigation is not forced on: a measurement that a + * mitigation command interrupts need not finish. + */ + KUNIT_EXPECT_EQ(test, sn201202x_ldcm_update_state(&t, + &sn201202x_ldcm_test_ops, + TYPEC_CONTAMINANT_UNKNOWN), + -EINPROGRESS); + sn201202x_ldcm_test_actions(test, &t, actions, ARRAY_SIZE(actions)); +} + +static void sn201202x_ldcm_overdue_once_test(struct kunit *test) +{ + const enum sn201202x_ldcm_test_action actions[] = { + LDCM_TEST_READ, + LDCM_TEST_OVERDUE, + LDCM_TEST_RERUN, + }; + struct sn201202x_ldcm_test t = { + .reads = { TYPEC_CONTAMINANT_UNKNOWN }, + .overdue = 1, + }; + + /* + * A first overrun makes the controller measure again, and the port + * stays down meanwhile: nothing is published. + */ + KUNIT_EXPECT_EQ(test, sn201202x_ldcm_update_state(&t, + &sn201202x_ldcm_test_ops, + TYPEC_CONTAMINANT_UNKNOWN), + -EINPROGRESS); + sn201202x_ldcm_test_actions(test, &t, actions, ARRAY_SIZE(actions)); +} + +static void sn201202x_ldcm_overdue_once_rerun_failure_test(struct kunit *test) +{ + const enum sn201202x_ldcm_test_action actions[] = { + LDCM_TEST_READ, + LDCM_TEST_OVERDUE, + LDCM_TEST_RERUN, + }; + struct sn201202x_ldcm_test t = { + .reads = { TYPEC_CONTAMINANT_UNKNOWN }, + .overdue = 1, + .rerun_ret = -ETIMEDOUT, + }; + + KUNIT_EXPECT_EQ(test, sn201202x_ldcm_update_state(&t, + &sn201202x_ldcm_test_ops, + TYPEC_CONTAMINANT_UNKNOWN), + -ETIMEDOUT); + sn201202x_ldcm_test_actions(test, &t, actions, ARRAY_SIZE(actions)); +} + +/* + * The measurement run again overran too: the port is held mitigated and + * stays "unknown", never taken for dry. Nothing else is read or sent. The + * hold starts with the mitigation that went through. + */ +static void sn201202x_ldcm_overdue_twice_holds_test(struct kunit *test) +{ + const enum sn201202x_ldcm_test_action actions[] = { + LDCM_TEST_READ, + LDCM_TEST_OVERDUE, LDCM_TEST_MITIGATE_ON, + LDCM_TEST_HELD, LDCM_TEST_PUBLISH_UNKNOWN, }; struct sn201202x_ldcm_test t = { - .reads = { TYPEC_CONTAMINANT_UNKNOWN }, + .reads = { TYPEC_CONTAMINANT_UNKNOWN, TYPEC_CONTAMINANT_NONE }, + .overdue = 2, }; - /* Without an earlier result a measurement is handled as unknown. */ KUNIT_EXPECT_EQ(test, sn201202x_ldcm_update_state(&t, &sn201202x_ldcm_test_ops, TYPEC_CONTAMINANT_UNKNOWN), -EAGAIN); sn201202x_ldcm_test_actions(test, &t, actions, ARRAY_SIZE(actions)); + KUNIT_EXPECT_EQ(test, t.read_count, 1U); + KUNIT_EXPECT_EQ(test, t.results, 0U); +} + +/* A dry result read during a hold enables the port, as any dry result. */ +static void sn201202x_ldcm_hold_dry_test(struct kunit *test) +{ + const enum sn201202x_ldcm_test_action actions[] = { + LDCM_TEST_READ, + LDCM_TEST_MITIGATE_OFF, + LDCM_TEST_READ, + LDCM_TEST_PUBLISH_NONE, + }; + struct sn201202x_ldcm_test t = { + .reads = { TYPEC_CONTAMINANT_NONE, TYPEC_CONTAMINANT_NONE }, + .overdue = 2, + }; + + KUNIT_EXPECT_EQ(test, sn201202x_ldcm_update_state(&t, + &sn201202x_ldcm_test_ops, + TYPEC_CONTAMINANT_UNKNOWN), 0); + sn201202x_ldcm_test_actions(test, &t, actions, ARRAY_SIZE(actions)); + KUNIT_EXPECT_EQ(test, t.results, 2U); +} + +/* Liquid read during a hold is mitigated and reported, as any liquid. */ +static void sn201202x_ldcm_hold_liquid_test(struct kunit *test) +{ + const enum sn201202x_ldcm_test_action actions[] = { + LDCM_TEST_READ, + LDCM_TEST_MITIGATE_ON, + LDCM_TEST_PUBLISH_LIQUID, + }; + struct sn201202x_ldcm_test t = { + .reads = { TYPEC_CONTAMINANT_LIQUID }, + .overdue = 2, + }; + + KUNIT_EXPECT_EQ(test, sn201202x_ldcm_update_state(&t, + &sn201202x_ldcm_test_ops, + TYPEC_CONTAMINANT_UNKNOWN), 0); + sn201202x_ldcm_test_actions(test, &t, actions, ARRAY_SIZE(actions)); + KUNIT_EXPECT_TRUE(test, t.wet); +} + +static void sn201202x_ldcm_overdue_mitigation_failure_test(struct kunit *test) +{ + const enum sn201202x_ldcm_test_action actions[] = { + LDCM_TEST_READ, + LDCM_TEST_OVERDUE, + LDCM_TEST_MITIGATE_ON, + LDCM_TEST_NOT_HELD, + LDCM_TEST_PUBLISH_UNKNOWN, + }; + struct sn201202x_ldcm_test t = { + .reads = { TYPEC_CONTAMINANT_UNKNOWN }, + .mitigation_rets = { -ETIMEDOUT }, + .overdue = 2, + }; + + /* A hold whose command fails keeps the port down, and is no hold yet. */ + KUNIT_EXPECT_EQ(test, sn201202x_ldcm_update_state(&t, + &sn201202x_ldcm_test_ops, + TYPEC_CONTAMINANT_UNKNOWN), + -ETIMEDOUT); + sn201202x_ldcm_test_actions(test, &t, actions, ARRAY_SIZE(actions)); +} + +/* + * Liquid, then a mitigation command that fails: "unknown" is published, but + * the port is still known to be wet. The measurement that follows is not + * waited for as one without a result: the port is mitigated, and the liquid + * reported again. + */ +static void sn201202x_ldcm_wet_never_overdue_test(struct kunit *test) +{ + const enum sn201202x_ldcm_test_action first[] = { + LDCM_TEST_READ, + LDCM_TEST_MITIGATE_ON, + LDCM_TEST_PUBLISH_UNKNOWN, + }; + const enum sn201202x_ldcm_test_action then[] = { + LDCM_TEST_READ, + LDCM_TEST_MITIGATE_ON, + LDCM_TEST_PUBLISH_LIQUID, + }; + struct sn201202x_ldcm_test t = { + .reads = { TYPEC_CONTAMINANT_LIQUID }, + .mitigation_rets = { -ETIMEDOUT }, + }; + struct sn201202x_ldcm_test t2 = { + .reads = { TYPEC_CONTAMINANT_UNKNOWN }, + .overdue = 2, + }; + + KUNIT_EXPECT_EQ(test, sn201202x_ldcm_update_state(&t, + &sn201202x_ldcm_test_ops, + TYPEC_CONTAMINANT_UNKNOWN), + -ETIMEDOUT); + sn201202x_ldcm_test_actions(test, &t, first, ARRAY_SIZE(first)); + KUNIT_ASSERT_TRUE(test, t.wet); + + t2.wet = t.wet; + KUNIT_EXPECT_EQ(test, sn201202x_ldcm_update_state(&t2, + &sn201202x_ldcm_test_ops, + TYPEC_CONTAMINANT_UNKNOWN), + -EINPROGRESS); + sn201202x_ldcm_test_actions(test, &t2, then, ARRAY_SIZE(then)); + KUNIT_EXPECT_TRUE(test, t2.wet); +} + +static void sn201202x_ldcm_wet_mitigation_failure_test(struct kunit *test) +{ + const enum sn201202x_ldcm_test_action actions[] = { + LDCM_TEST_READ, + LDCM_TEST_MITIGATE_ON, + }; + struct sn201202x_ldcm_test t = { + .reads = { TYPEC_CONTAMINANT_UNKNOWN }, + .mitigation_rets = { -ETIMEDOUT }, + .overdue = 2, + .wet = true, + }; + + /* Still wet: the port stays down, and the command is retried. */ + KUNIT_EXPECT_EQ(test, sn201202x_ldcm_update_state(&t, + &sn201202x_ldcm_test_ops, + TYPEC_CONTAMINANT_UNKNOWN), + -ETIMEDOUT); + sn201202x_ldcm_test_actions(test, &t, actions, ARRAY_SIZE(actions)); +} + +static void sn201202x_ldcm_liquid_read_failure_test(struct kunit *test) +{ + const enum sn201202x_ldcm_test_action actions[] = { + LDCM_TEST_READ, + LDCM_TEST_MITIGATE_ON, + LDCM_TEST_PUBLISH_LIQUID, + }; + struct sn201202x_ldcm_test t = { + .reads = { TYPEC_CONTAMINANT_UNKNOWN }, + .read_rets = { -EIO }, + .wet = true, + }; + + /* A failed read is no result: the liquid stays reported. */ + KUNIT_EXPECT_EQ(test, sn201202x_ldcm_update_state(&t, + &sn201202x_ldcm_test_ops, + TYPEC_CONTAMINANT_LIQUID), -EIO); + sn201202x_ldcm_test_actions(test, &t, actions, ARRAY_SIZE(actions)); + KUNIT_EXPECT_TRUE(test, t.wet); + KUNIT_EXPECT_EQ(test, t.results, 0U); +} + +static void sn201202x_ldcm_liquid_mitigation_failure_test(struct kunit *test) +{ + const enum sn201202x_ldcm_test_action actions[] = { + LDCM_TEST_READ, + LDCM_TEST_MITIGATE_ON, + LDCM_TEST_PUBLISH_LIQUID, + }; + struct sn201202x_ldcm_test t = { + .reads = { TYPEC_CONTAMINANT_LIQUID }, + .mitigation_rets = { -ETIMEDOUT }, + }; + + KUNIT_EXPECT_EQ(test, sn201202x_ldcm_update_state(&t, + &sn201202x_ldcm_test_ops, + TYPEC_CONTAMINANT_LIQUID), + -ETIMEDOUT); + sn201202x_ldcm_test_actions(test, &t, actions, ARRAY_SIZE(actions)); +} + +static void sn201202x_ldcm_wait_test(struct kunit *test) +{ + unsigned long stuck = msecs_to_jiffies(SN201202X_LDCM_STUCK_MS); + struct sn201202x_ldcm_wait wait = {}; + unsigned long start = -(stuck / 2); /* across a jiffies wrap */ + + /* The first measurement without a result starts the wait. */ + KUNIT_EXPECT_EQ(test, sn201202x_ldcm_wait_overdue(&wait, start), 0U); + KUNIT_EXPECT_TRUE(test, wait.waiting); + KUNIT_EXPECT_EQ(test, sn201202x_ldcm_retry_ms(&wait, 0, start), SN201202X_LDCM_RETRY_MS); + /* A failed read is retried slowly too. */ + KUNIT_EXPECT_EQ(test, sn201202x_ldcm_retry_ms(&wait, 1, start), + SN201202X_LDCM_SLOW_RETRY_MS); + KUNIT_EXPECT_EQ(test, sn201202x_ldcm_wait_overdue(&wait, start + stuck - 1), 0U); + KUNIT_EXPECT_EQ(test, sn201202x_ldcm_wait_overdue(&wait, start + stuck), 1U); + /* Overrun once: retried slowly, and the measurement run again gets a whole wait. */ + KUNIT_EXPECT_EQ(test, sn201202x_ldcm_retry_ms(&wait, 0, start + stuck), + SN201202X_LDCM_SLOW_RETRY_MS); + sn201202x_ldcm_wait_rerun(&wait, start + stuck); + KUNIT_EXPECT_FALSE(test, wait.rerun_due); + KUNIT_EXPECT_EQ(test, sn201202x_ldcm_wait_overdue(&wait, start + 2 * stuck - 1), 0U); + /* Overrun again: held, and the hold lasts, it does not end in dry. */ + KUNIT_EXPECT_EQ(test, sn201202x_ldcm_wait_overdue(&wait, start + 2 * stuck), 2U); + KUNIT_EXPECT_EQ(test, wait.holds, 1U); + KUNIT_EXPECT_FALSE(test, wait.held); + sn201202x_ldcm_wait_held(&wait, true, start + 2 * stuck); + KUNIT_EXPECT_EQ(test, sn201202x_ldcm_wait_overdue(&wait, start + 3 * stuck), 2U); + + /* A result ends the wait; the next measurement starts another. */ + wait = (struct sn201202x_ldcm_wait){}; + KUNIT_EXPECT_EQ(test, sn201202x_ldcm_retry_ms(&wait, 0, start), SN201202X_LDCM_RETRY_MS); + KUNIT_EXPECT_EQ(test, sn201202x_ldcm_wait_overdue(&wait, start + 10 * stuck), 0U); + KUNIT_EXPECT_EQ(test, sn201202x_ldcm_wait_overdue(&wait, start + 11 * stuck - 1), 0U); +} + +/* + * A measurement that was not started again (the command failed or was not + * sent) does not use up the second wait: every update tries again, and the + * second wait runs from the restart that went through. + */ +static void sn201202x_ldcm_wait_rerun_failure_test(struct kunit *test) +{ + unsigned long stuck = msecs_to_jiffies(SN201202X_LDCM_STUCK_MS); + struct sn201202x_ldcm_wait wait = {}; + unsigned long start = -(stuck / 2); /* across a jiffies wrap */ + unsigned long rerun = start + 3 * stuck; + + KUNIT_EXPECT_EQ(test, sn201202x_ldcm_wait_overdue(&wait, start), 0U); + KUNIT_EXPECT_EQ(test, sn201202x_ldcm_wait_overdue(&wait, start + stuck), 1U); + KUNIT_EXPECT_TRUE(test, wait.rerun_due); + /* No restart: the next update tries again, slowly, never the second overrun. */ + KUNIT_EXPECT_EQ(test, sn201202x_ldcm_wait_overdue(&wait, start + stuck + 1), 1U); + KUNIT_EXPECT_EQ(test, sn201202x_ldcm_retry_ms(&wait, 0, start + stuck + 1), + SN201202X_LDCM_SLOW_RETRY_MS); + KUNIT_EXPECT_EQ(test, sn201202x_ldcm_wait_overdue(&wait, start + 2 * stuck), 1U); + KUNIT_EXPECT_EQ(test, sn201202x_ldcm_wait_overdue(&wait, rerun), 1U); + + /* The restart went through: a whole wait from then before the hold. */ + sn201202x_ldcm_wait_rerun(&wait, rerun); + KUNIT_EXPECT_EQ(test, sn201202x_ldcm_wait_overdue(&wait, rerun + stuck - 1), 0U); + KUNIT_EXPECT_EQ(test, sn201202x_ldcm_retry_ms(&wait, 0, rerun + stuck - 1), + SN201202X_LDCM_SLOW_RETRY_MS); + KUNIT_EXPECT_EQ(test, sn201202x_ldcm_wait_overdue(&wait, rerun + stuck), 2U); + KUNIT_EXPECT_FALSE(test, wait.rerun_due); +} + +/* + * Holds: each ends in a restart of the measurement, which gets a whole wait, + * and is then twice as long as the last, up to SN201202X_LDCM_HOLD_MAX_MS. + * A hold is not polled: the retry comes at its end. A restart that fails at + * the end of a hold stays due, as the first one. + */ +static void sn201202x_ldcm_wait_hold_backoff_test(struct kunit *test) +{ + static const unsigned int hold_ms[] = { + 10000, 20000, 40000, 80000, 160000, 300000, 300000, 300000, + }; + unsigned long stuck = msecs_to_jiffies(SN201202X_LDCM_STUCK_MS); + struct sn201202x_ldcm_wait wait = {}; + unsigned long now = -(stuck / 2); /* across a jiffies wrap */ + unsigned int i; + + KUNIT_EXPECT_EQ(test, sn201202x_ldcm_hold_ms(UINT_MAX), SN201202X_LDCM_HOLD_MAX_MS); + KUNIT_EXPECT_EQ(test, sn201202x_ldcm_wait_overdue(&wait, now), 0U); + now += stuck; + KUNIT_EXPECT_EQ(test, sn201202x_ldcm_wait_overdue(&wait, now), 1U); + sn201202x_ldcm_wait_rerun(&wait, now); + + for (i = 0; i < ARRAY_SIZE(hold_ms); i++) { + unsigned long hold = msecs_to_jiffies(hold_ms[i]); + + KUNIT_EXPECT_EQ(test, sn201202x_ldcm_hold_ms(i + 1), hold_ms[i]); + now += stuck; + KUNIT_EXPECT_EQ(test, sn201202x_ldcm_wait_overdue(&wait, now), 2U); + KUNIT_EXPECT_EQ(test, wait.holds, i + 1); + /* Its mitigation fails first: retried as a failed command, no hold yet. */ + sn201202x_ldcm_wait_held(&wait, false, now); + KUNIT_EXPECT_EQ(test, sn201202x_ldcm_retry_ms(&wait, 0, now), + SN201202X_LDCM_SLOW_RETRY_MS); + KUNIT_EXPECT_EQ(test, sn201202x_ldcm_wait_overdue(&wait, now + stuck), 2U); + now += stuck; + sn201202x_ldcm_wait_held(&wait, true, now); + /* Further mitigations that go through do not stretch the hold. */ + sn201202x_ldcm_wait_held(&wait, true, now + hold / 2); + KUNIT_EXPECT_EQ(test, sn201202x_ldcm_retry_ms(&wait, 0, now), + jiffies_to_msecs(hold)); + /* Not even a failed read polls a hold. */ + KUNIT_EXPECT_EQ(test, sn201202x_ldcm_retry_ms(&wait, 1, now + hold / 2), + max_t(unsigned int, jiffies_to_msecs(hold - hold / 2), + SN201202X_LDCM_SLOW_RETRY_MS)); + KUNIT_EXPECT_EQ(test, sn201202x_ldcm_wait_overdue(&wait, now + hold - 1), 2U); + /* Late (an update that came after the end) is retried at once, slowly. */ + KUNIT_EXPECT_EQ(test, sn201202x_ldcm_retry_ms(&wait, 0, now + hold + stuck), + SN201202X_LDCM_SLOW_RETRY_MS); + now += hold; + KUNIT_EXPECT_EQ(test, sn201202x_ldcm_wait_overdue(&wait, now), 1U); + KUNIT_EXPECT_TRUE(test, wait.rerun_due); + KUNIT_EXPECT_EQ(test, sn201202x_ldcm_retry_ms(&wait, 0, now), + SN201202X_LDCM_SLOW_RETRY_MS); + /* The first restart attempt fails; the second goes through. */ + KUNIT_EXPECT_EQ(test, sn201202x_ldcm_wait_overdue(&wait, now + stuck), 1U); + now += stuck; + sn201202x_ldcm_wait_rerun(&wait, now); + KUNIT_EXPECT_EQ(test, sn201202x_ldcm_wait_overdue(&wait, now + stuck - 1), 0U); + } +} + +/* + * The whole path of a port without an earlier result, through + * sn201202x_ldcm_update_state() with the driver's wait and a clock that runs + * by the driver's retry delays: a controller that reports no result, or one + * from a given time on. + */ +struct sn201202x_ldcm_sim { + struct sn201202x_ldcm_wait wait; + unsigned long now; + /* The controller reports dry from dry_at on, if it dries */ + bool dries; + unsigned long dry_at; + bool mitigated; + bool wet; + enum typec_contaminant published; + unsigned int reruns; + unsigned int updates; + /* Mitigation commands that fail before the first one goes through */ + unsigned int mitigation_failures; + int mitigation_err; + unsigned int mitigations; +}; + +static int sn201202x_ldcm_sim_read(void *ctx, enum typec_contaminant *contaminant) +{ + struct sn201202x_ldcm_sim *sim = ctx; + + *contaminant = sim->dries && !time_before(sim->now, sim->dry_at) ? + TYPEC_CONTAMINANT_NONE : TYPEC_CONTAMINANT_UNKNOWN; + return 0; +} + +static int sn201202x_ldcm_sim_mitigate(void *ctx, bool enable) +{ + struct sn201202x_ldcm_sim *sim = ctx; + + sim->mitigations++; + if (sim->mitigation_failures) { + sim->mitigation_failures--; + return sim->mitigation_err; + } + sim->mitigated = enable; + return 0; +} + +static void sn201202x_ldcm_sim_publish(void *ctx, enum typec_contaminant contaminant) +{ + struct sn201202x_ldcm_sim *sim = ctx; + + sim->published = contaminant; +} + +static void sn201202x_ldcm_sim_result(void *ctx, enum typec_contaminant contaminant) +{ + struct sn201202x_ldcm_sim *sim = ctx; + + sim->wet = contaminant == TYPEC_CONTAMINANT_LIQUID; + sim->wait = (struct sn201202x_ldcm_wait){}; +} + +static bool sn201202x_ldcm_sim_wet(void *ctx) +{ + struct sn201202x_ldcm_sim *sim = ctx; + + return sim->wet; +} + +static unsigned int sn201202x_ldcm_sim_overdue(void *ctx) +{ + struct sn201202x_ldcm_sim *sim = ctx; + + return sn201202x_ldcm_wait_overdue(&sim->wait, sim->now); +} + +static void sn201202x_ldcm_sim_held(void *ctx, bool mitigated) +{ + struct sn201202x_ldcm_sim *sim = ctx; + + sn201202x_ldcm_wait_held(&sim->wait, mitigated, sim->now); +} + +static int sn201202x_ldcm_sim_rerun(void *ctx) +{ + struct sn201202x_ldcm_sim *sim = ctx; + + sim->reruns++; + sim->mitigated = false; + sn201202x_ldcm_wait_rerun(&sim->wait, sim->now); + return 0; +} + +static unsigned int sn201202x_ldcm_sim_read_failures(void *ctx) +{ + return 0; +} + +static const struct sn201202x_ldcm_ops sn201202x_ldcm_sim_ops = { + .read = sn201202x_ldcm_sim_read, + .set_mitigation = sn201202x_ldcm_sim_mitigate, + .publish = sn201202x_ldcm_sim_publish, + .result = sn201202x_ldcm_sim_result, + .wet = sn201202x_ldcm_sim_wet, + .overdue = sn201202x_ldcm_sim_overdue, + .held = sn201202x_ldcm_sim_held, + .rerun = sn201202x_ldcm_sim_rerun, + .read_failures = sn201202x_ldcm_sim_read_failures, +}; + +/* Updates until @end or until the port is enabled; the last return */ +static int sn201202x_ldcm_sim_run(struct kunit *test, struct sn201202x_ldcm_sim *sim, + unsigned long end) +{ + int ret = -EINPROGRESS; + + while (time_before(sim->now, end)) { + sim->updates++; + ret = sn201202x_ldcm_update_state(sim, &sn201202x_ldcm_sim_ops, + sim->published); + if (sim->published == TYPEC_CONTAMINANT_NONE) + break; + KUNIT_ASSERT_TRUE(test, ret == -EINPROGRESS || ret == -EAGAIN); + sim->now += msecs_to_jiffies(sn201202x_ldcm_retry_ms(&sim->wait, 0, + sim->now)); + } + return ret; +} + +/* + * A port without an earlier result (wet at probe, say) whose measurement + * never gives one: never enabled, held mitigated between restarts that come + * further and further apart, with few updates. A dry result enables it. + */ +static void sn201202x_ldcm_never_a_result_test(struct kunit *test) +{ + unsigned long start = -msecs_to_jiffies(SN201202X_LDCM_STUCK_MS); + unsigned long hour = msecs_to_jiffies(3600 * MSEC_PER_SEC); + unsigned long hold_max = msecs_to_jiffies(SN201202X_LDCM_HOLD_MAX_MS); + struct sn201202x_ldcm_sim sim = { + .now = start, + .published = TYPEC_CONTAMINANT_UNKNOWN, + }; + unsigned long dried; + + KUNIT_EXPECT_EQ(test, sn201202x_ldcm_sim_run(test, &sim, start + hour), -EAGAIN); + KUNIT_EXPECT_EQ(test, sim.published, TYPEC_CONTAMINANT_UNKNOWN); + KUNIT_EXPECT_TRUE(test, sim.mitigated); + KUNIT_EXPECT_EQ(test, sim.reruns, sim.wait.holds); + /* Restarts at 5 s, then after holds of 10 s to 5 min: 16 in the hour */ + KUNIT_EXPECT_GE(test, sim.reruns, 10U); + KUNIT_EXPECT_LE(test, sim.reruns, 20U); + /* No busy loop: a few updates per restart */ + KUNIT_EXPECT_LE(test, sim.updates, 200U); + + /* Dry from now on: enabled at the end of the hold at the latest. */ + sim.dries = true; + sim.dry_at = sim.now; + dried = sim.now; + KUNIT_EXPECT_EQ(test, sn201202x_ldcm_sim_run(test, &sim, dried + 2 * hold_max), 0); + KUNIT_EXPECT_EQ(test, sim.published, TYPEC_CONTAMINANT_NONE); + KUNIT_EXPECT_FALSE(test, sim.mitigated); + KUNIT_EXPECT_LE(test, sim.now - dried, hold_max); + KUNIT_EXPECT_EQ(test, sim.wait.holds, 0U); +} + +/* + * A slow controller, the case the waits are for: a measurement that ends + * within the first wait, after the restart, or during a hold enables the port + * as soon as it is read, and is never forced into mitigation before the + * restart had its whole wait. Without a controller event (as here), a result + * that comes during a hold is read at its end. + */ +static void sn201202x_ldcm_slow_result_test(struct kunit *test) +{ + static const struct { + unsigned int dry_ms; + unsigned int reruns; + unsigned int by_ms; + } cases[] = { + /* Read at once, in the first wait and after the restart */ + { 3000, 0, 3500 }, + { 7000, 1, 7500 }, + /* Read at the end of the hold (11-21 s, then 27-47 s and 53-93 s) */ + { 12000, 1, 22000 }, + { 60000, 3, 95000 }, + }; + unsigned long hour = msecs_to_jiffies(3600 * MSEC_PER_SEC); + unsigned int i; + + for (i = 0; i < ARRAY_SIZE(cases); i++) { + unsigned long start = -msecs_to_jiffies(SN201202X_LDCM_STUCK_MS); + struct sn201202x_ldcm_sim sim = { + .now = start, + .published = TYPEC_CONTAMINANT_UNKNOWN, + .dries = true, + .dry_at = start + msecs_to_jiffies(cases[i].dry_ms), + }; + + KUNIT_EXPECT_EQ(test, sn201202x_ldcm_sim_run(test, &sim, start + hour), 0); + KUNIT_EXPECT_EQ(test, sim.published, TYPEC_CONTAMINANT_NONE); + KUNIT_EXPECT_FALSE(test, sim.mitigated); + KUNIT_EXPECT_EQ(test, sim.reruns, cases[i].reruns); + KUNIT_EXPECT_LE(test, jiffies_to_msecs(sim.now - start), cases[i].by_ms); + } +} + +static int sn201202x_ldcm_sim_step(struct sn201202x_ldcm_sim *sim) +{ + sim->updates++; + return sn201202x_ldcm_update_state(sim, &sn201202x_ldcm_sim_ops, + sim->published); +} + +/* + * A hold starts only once its mitigation went through. A failed command is + * retried after SN201202X_LDCM_SLOW_RETRY_MS, as a failed restart is, and the + * update that follows, however late it comes, sends the mitigation again: the + * measurement is never started again from a hold that was not mitigated. The + * hold runs whole from the mitigation that went through, and then ends in a + * restart. + */ +static void sn201202x_ldcm_hold_mitigation_failure_retried_test(struct kunit *test) +{ + static const struct { + unsigned int failures; + int err; + /* Each retry comes only after the longest hold */ + bool late; + } cases[] = { + { 0, 0, false }, + { 1, -ETIMEDOUT, false }, + { 3, -EIO, false }, + { 3, -ETIMEDOUT, true }, + }; + unsigned long stuck = msecs_to_jiffies(SN201202X_LDCM_STUCK_MS); + unsigned long hold = msecs_to_jiffies(SN201202X_LDCM_HOLD_MS); + unsigned long hold_max = msecs_to_jiffies(SN201202X_LDCM_HOLD_MAX_MS); + unsigned int i, j; + + for (i = 0; i < ARRAY_SIZE(cases); i++) { + unsigned long start = -stuck; /* across a jiffies wrap */ + struct sn201202x_ldcm_sim sim = { + .now = start, + .published = TYPEC_CONTAMINANT_UNKNOWN, + .mitigation_failures = cases[i].failures, + .mitigation_err = cases[i].err, + }; + unsigned int delay; + + /* The first wait and the restart's wait run out: the hold is due. */ + KUNIT_ASSERT_EQ(test, sn201202x_ldcm_sim_step(&sim), -EINPROGRESS); + sim.now += stuck; + KUNIT_ASSERT_EQ(test, sn201202x_ldcm_sim_step(&sim), -EINPROGRESS); + KUNIT_ASSERT_EQ(test, sim.reruns, 1U); + sim.now += stuck; + + for (j = 0; j < cases[i].failures; j++) { + KUNIT_EXPECT_EQ(test, sn201202x_ldcm_sim_step(&sim), cases[i].err); + KUNIT_EXPECT_EQ(test, sim.mitigations, j + 1); + KUNIT_EXPECT_FALSE(test, sim.mitigated); + KUNIT_EXPECT_FALSE(test, sim.wait.held); + KUNIT_EXPECT_EQ(test, sim.published, TYPEC_CONTAMINANT_UNKNOWN); + KUNIT_EXPECT_EQ(test, sim.reruns, 1U); + delay = sn201202x_ldcm_retry_ms(&sim.wait, 0, sim.now); + KUNIT_EXPECT_EQ(test, delay, SN201202X_LDCM_SLOW_RETRY_MS); + if (cases[i].late) + sim.now += hold_max; + else + sim.now += msecs_to_jiffies(delay); + } + + /* The command goes through: the hold runs whole from now. */ + KUNIT_EXPECT_EQ(test, sn201202x_ldcm_sim_step(&sim), -EAGAIN); + KUNIT_EXPECT_EQ(test, sim.mitigations, cases[i].failures + 1); + KUNIT_EXPECT_TRUE(test, sim.mitigated); + KUNIT_EXPECT_TRUE(test, sim.wait.held); + KUNIT_EXPECT_EQ(test, sim.published, TYPEC_CONTAMINANT_UNKNOWN); + KUNIT_EXPECT_EQ(test, sim.reruns, 1U); + KUNIT_EXPECT_EQ(test, sn201202x_ldcm_retry_ms(&sim.wait, 0, sim.now), + jiffies_to_msecs(hold)); + + /* Just before its end nothing is restarted, nor is the hold stretched. */ + sim.now += hold - 1; + KUNIT_EXPECT_EQ(test, sn201202x_ldcm_sim_step(&sim), -EAGAIN); + KUNIT_EXPECT_EQ(test, sim.reruns, 1U); + KUNIT_EXPECT_TRUE(test, sim.mitigated); + + /* At its end the measurement is started again, retried as a restart. */ + sim.now += 1; + KUNIT_EXPECT_EQ(test, sn201202x_ldcm_sim_step(&sim), -EINPROGRESS); + KUNIT_EXPECT_EQ(test, sim.reruns, 2U); + KUNIT_EXPECT_FALSE(test, sim.mitigated); + KUNIT_EXPECT_EQ(test, sn201202x_ldcm_retry_ms(&sim.wait, 0, sim.now), + SN201202X_LDCM_SLOW_RETRY_MS); + } +} + +/* In the busy-wait of suspend-to-idle, mitigation is never removed. */ +static void sn201202x_ldcm_atomic_test(struct kunit *test) +{ + struct sn201202x *sn = kunit_kzalloc(test, sizeof(*sn), GFP_KERNEL); + struct tps6598x *tps; + + KUNIT_ASSERT_NOT_NULL(test, sn); + tps = &sn->cd.tps; + sn->atomic = true; + sn->ldcm_mitigation_known = true; + sn->ldcm_mitigated = true; + + /* Refused without touching the bus (there is none here) */ + KUNIT_EXPECT_EQ(test, sn201202x_ldcm_set_mitigation(tps, false), -EAGAIN); + KUNIT_EXPECT_TRUE(test, sn->ldcm_mitigation_known); + KUNIT_EXPECT_TRUE(test, sn->ldcm_mitigated); + KUNIT_EXPECT_EQ(test, sn201202x_ldcm_set_mitigation(tps, true), 0); + /* A restart refused is still due: its second wait does not start. */ + sn->ldcm_wait.waiting = true; + sn->ldcm_wait.overdue = 1; + sn->ldcm_wait.rerun_due = true; + KUNIT_EXPECT_EQ(test, sn201202x_ldcm_rerun(tps), -EAGAIN); + KUNIT_EXPECT_TRUE(test, sn->ldcm_mitigation_known); + KUNIT_EXPECT_TRUE(test, sn->ldcm_wait.rerun_due); + sn->ldcm_wait = (struct sn201202x_ldcm_wait){}; + + /* No wait runs out there: the update after the resume decides. */ + sn->ldcm_wait.waiting = true; + sn->ldcm_wait.since = jiffies - msecs_to_jiffies(10 * SN201202X_LDCM_STUCK_MS); + KUNIT_EXPECT_EQ(test, sn201202x_ldcm_overdue(tps), 0U); + KUNIT_EXPECT_EQ(test, sn->ldcm_wait.overdue, 0U); } static void sn201202x_ldcm_read_failure_with_result_test(struct kunit *test) @@ -973,6 +1749,7 @@ static void sn201202x_ldcm_read_failure_with_result_test(struct kunit *test) struct sn201202x_ldcm_test t = { .reads = { TYPEC_CONTAMINANT_UNKNOWN }, .read_rets = { -EIO }, + .read_failures = SN201202X_LDCM_READ_TRIES, }; /* A failed read is never mistaken for a running measurement. */ @@ -983,6 +1760,84 @@ static void sn201202x_ldcm_read_failure_with_result_test(struct kunit *test) sn201202x_ldcm_test_actions(test, &t, actions, ARRAY_SIZE(actions)); } +/* + * A dry port, maybe with a dock or a display on it, is not mitigated for a + * failed read: only for SN201202X_LDCM_READ_TRIES failures in a row. + */ +static void sn201202x_ldcm_dry_read_failure_retried_test(struct kunit *test) +{ + const enum sn201202x_ldcm_test_action retried[] = { + LDCM_TEST_READ, + }; + const enum sn201202x_ldcm_test_action mitigated[] = { + LDCM_TEST_READ, + LDCM_TEST_MITIGATE_ON, + LDCM_TEST_PUBLISH_UNKNOWN, + }; + unsigned int i; + + for (i = 1; i < SN201202X_LDCM_READ_TRIES; i++) { + struct sn201202x_ldcm_test t = { + .reads = { TYPEC_CONTAMINANT_UNKNOWN }, + .read_rets = { -EIO }, + .read_failures = i, + .overdue = 2, + }; + + KUNIT_EXPECT_EQ(test, sn201202x_ldcm_update_state(&t, + &sn201202x_ldcm_test_ops, + TYPEC_CONTAMINANT_NONE), + -EIO); + sn201202x_ldcm_test_actions(test, &t, retried, ARRAY_SIZE(retried)); + } + + for (i = SN201202X_LDCM_READ_TRIES; i <= SN201202X_LDCM_READ_TRIES + 1; i++) { + struct sn201202x_ldcm_test t = { + .reads = { TYPEC_CONTAMINANT_UNKNOWN }, + .read_rets = { -EIO }, + .read_failures = i, + }; + + KUNIT_EXPECT_EQ(test, sn201202x_ldcm_update_state(&t, + &sn201202x_ldcm_test_ops, + TYPEC_CONTAMINANT_NONE), + -EIO); + sn201202x_ldcm_test_actions(test, &t, mitigated, ARRAY_SIZE(mitigated)); + } +} + +/* Without a result, or wet, a failed read mitigates at once, as before. */ +static void sn201202x_ldcm_read_failure_not_dry_test(struct kunit *test) +{ + const struct { + enum typec_contaminant last; + enum sn201202x_ldcm_test_action publish; + } cases[] = { + { TYPEC_CONTAMINANT_UNKNOWN, LDCM_TEST_PUBLISH_UNKNOWN }, + { TYPEC_CONTAMINANT_LIQUID, LDCM_TEST_PUBLISH_LIQUID }, + }; + unsigned int i; + + for (i = 0; i < ARRAY_SIZE(cases); i++) { + const enum sn201202x_ldcm_test_action actions[] = { + LDCM_TEST_READ, + LDCM_TEST_MITIGATE_ON, + cases[i].publish, + }; + struct sn201202x_ldcm_test t = { + .reads = { TYPEC_CONTAMINANT_UNKNOWN }, + .read_rets = { -EIO }, + .read_failures = 1, + }; + + KUNIT_EXPECT_EQ(test, sn201202x_ldcm_update_state(&t, + &sn201202x_ldcm_test_ops, + cases[i].last), + -EIO); + sn201202x_ldcm_test_actions(test, &t, actions, ARRAY_SIZE(actions)); + } +} + struct sn201202x_ldcm_stop_test { struct sn201202x sn; atomic_t runs; @@ -1052,7 +1907,26 @@ static struct kunit_case sn201202x_pm_cases[] = { KUNIT_CASE(sn201202x_ldcm_dry_race_mitigation_failure_test), KUNIT_CASE(sn201202x_ldcm_busy_keeps_result_test), KUNIT_CASE(sn201202x_ldcm_busy_without_result_test), + KUNIT_CASE(sn201202x_ldcm_overdue_once_test), + KUNIT_CASE(sn201202x_ldcm_overdue_once_rerun_failure_test), + KUNIT_CASE(sn201202x_ldcm_overdue_twice_holds_test), + KUNIT_CASE(sn201202x_ldcm_hold_dry_test), + KUNIT_CASE(sn201202x_ldcm_hold_liquid_test), + KUNIT_CASE(sn201202x_ldcm_overdue_mitigation_failure_test), + KUNIT_CASE(sn201202x_ldcm_wet_never_overdue_test), + KUNIT_CASE(sn201202x_ldcm_wet_mitigation_failure_test), + KUNIT_CASE(sn201202x_ldcm_liquid_read_failure_test), + KUNIT_CASE(sn201202x_ldcm_liquid_mitigation_failure_test), + KUNIT_CASE(sn201202x_ldcm_wait_test), + KUNIT_CASE(sn201202x_ldcm_wait_rerun_failure_test), + KUNIT_CASE(sn201202x_ldcm_wait_hold_backoff_test), + KUNIT_CASE(sn201202x_ldcm_never_a_result_test), + KUNIT_CASE(sn201202x_ldcm_slow_result_test), + KUNIT_CASE(sn201202x_ldcm_hold_mitigation_failure_retried_test), + KUNIT_CASE(sn201202x_ldcm_atomic_test), KUNIT_CASE(sn201202x_ldcm_read_failure_with_result_test), + KUNIT_CASE(sn201202x_ldcm_dry_read_failure_retried_test), + KUNIT_CASE(sn201202x_ldcm_read_failure_not_dry_test), KUNIT_CASE(sn201202x_ldcm_stop_requeue_test), KUNIT_CASE(sn201202x_idle_sleep_test), KUNIT_CASE(sn201202x_atomic_access_test), diff --git a/drivers/usb/typec/tipd/spmi.c b/drivers/usb/typec/tipd/spmi.c index 44e574400b6ec..326ec0a324cf7 100644 --- a/drivers/usb/typec/tipd/spmi.c +++ b/drivers/usb/typec/tipd/spmi.c @@ -39,7 +39,6 @@ #define SN201202X_LDCM_STATE GENMASK(3, 1) #define SN201202X_LDCM_STATE_IDLE 0 #define SN201202X_LDCM_RESULT GENMASK(5, 4) -#define SN201202X_LDCM_RETRY_MS 500 static int sn201202x_select_reg(struct spmi_device *sdev, u8 reg, unsigned int timeout_us) @@ -333,6 +332,7 @@ static int sn201202x_ldcm_read(void *ctx, enum typec_contaminant *contaminant) { struct tps6598x *tps = ctx; + struct sn201202x *sn = tps_to_sn(tps); u8 status[SN201202X_LDCM_STATUS_LEN]; int ret; @@ -340,10 +340,21 @@ static int sn201202x_ldcm_read(void *ctx, sizeof(status)); *contaminant = ret ? TYPEC_CONTAMINANT_UNKNOWN : sn201202x_ldcm_decode(status); + if (!ret) + sn->ldcm_read_failures = 0; + else if (sn->ldcm_read_failures < UINT_MAX) + sn->ldcm_read_failures++; return ret; } -static int sn201202x_ldcm_set_mitigation(void *ctx, bool enable) +static unsigned int sn201202x_ldcm_read_failures(void *ctx) +{ + struct tps6598x *tps = ctx; + + return tps_to_sn(tps)->ldcm_read_failures; +} + +VISIBLE_IF_KUNIT int sn201202x_ldcm_set_mitigation(void *ctx, bool enable) { struct tps6598x *tps = ctx; struct sn201202x *sn = tps_to_sn(tps); @@ -357,6 +368,14 @@ static int sn201202x_ldcm_set_mitigation(void *ctx, bool enable) */ if (sn->ldcm_mitigation_known && sn->ldcm_mitigated == enable) return 0; + /* + * Not from the busy-wait of suspend-to-idle (sn201202x_sleep_event()): + * removing mitigation can start a measurement, and the command can + * take up to five seconds. Refused, the event wakes the + * system, and the update after the resume sends it. + */ + if (!enable && READ_ONCE(sn->atomic)) + return -EAGAIN; /* * As macOS, which reads a dry port's status but sends it no LDCM @@ -385,6 +404,7 @@ static int sn201202x_ldcm_set_mitigation(void *ctx, bool enable) sn->ldcm_requested = false; return ret; } +EXPORT_SYMBOL_IF_KUNIT(sn201202x_ldcm_set_mitigation); static void sn201202x_ldcm_publish(void *ctx, enum typec_contaminant contaminant) @@ -395,6 +415,206 @@ static void sn201202x_ldcm_publish(void *ctx, tps->contaminant = contaminant; } +/* Only what the controller reported counts, not what was published. */ +static void sn201202x_ldcm_result(void *ctx, enum typec_contaminant contaminant) +{ + struct tps6598x *tps = ctx; + struct sn201202x *sn = tps_to_sn(tps); + + if (sn->ldcm_wait.holds) + dev_info(tps->dev, "liquid detection reported %s, after %u holds without a result\n", + contaminant == TYPEC_CONTAMINANT_LIQUID ? "liquid" : "dry", + sn->ldcm_wait.holds); + sn->ldcm_wet = contaminant == TYPEC_CONTAMINANT_LIQUID; + sn->ldcm_wait = (struct sn201202x_ldcm_wait){}; +} + +static bool sn201202x_ldcm_wet(void *ctx) +{ + struct tps6598x *tps = ctx; + + return tps_to_sn(tps)->ldcm_wet; +} + +/* How long the @holds-th hold since the last result lasts (from 1) */ +VISIBLE_IF_KUNIT unsigned int sn201202x_ldcm_hold_ms(unsigned int holds) +{ + unsigned int shift = min(holds ? holds - 1 : 0, 5U); + + return min_t(unsigned int, SN201202X_LDCM_HOLD_MS << shift, + SN201202X_LDCM_HOLD_MAX_MS); +} +EXPORT_SYMBOL_IF_KUNIT(sn201202x_ldcm_hold_ms); + +/* + * The first call starts the wait of the first measurement; it is overdue + * SN201202X_LDCM_STUCK_MS later, and the measurement is then to be started + * again: until sn201202x_ldcm_wait_rerun() says it was, every call returns 1. + * The measurement started again gets a whole wait too. Overdue, the port is + * to be held mitigated (2): the hold lasts sn201202x_ldcm_hold_ms() from the + * mitigation that went through (sn201202x_ldcm_wait_held()), and then the + * measurement is to be started again (1), and so on, until a result resets + * the wait. Until the mitigation went through, it stays 2. + */ +VISIBLE_IF_KUNIT unsigned int +sn201202x_ldcm_wait_overdue(struct sn201202x_ldcm_wait *wait, unsigned long now) +{ + if (!wait->waiting) { + wait->waiting = true; + wait->since = now; + return 0; + } + if (wait->rerun_due) + return 1; + if (wait->overdue == 2) { + if (!wait->held || + time_before(now, wait->since + + msecs_to_jiffies(sn201202x_ldcm_hold_ms(wait->holds)))) + return 2; + wait->held = false; + wait->overdue = 1; + wait->rerun_due = true; + wait->since = now; + return 1; + } + if (time_before(now, wait->since + msecs_to_jiffies(SN201202X_LDCM_STUCK_MS))) + return 0; + wait->since = now; + if (!wait->overdue) { + wait->overdue = 1; + wait->rerun_due = true; + return 1; + } + wait->overdue = 2; + wait->held = false; + if (wait->holds < UINT_MAX) + wait->holds++; + return 2; +} +EXPORT_SYMBOL_IF_KUNIT(sn201202x_ldcm_wait_overdue); + +/* + * The hold's mitigation command went through (@mitigated) or failed. The + * hold runs from the first that went through; a failed one, even in a hold + * that runs, starts it again from the next that goes through, as the + * controller may then be unmitigated. + */ +VISIBLE_IF_KUNIT void +sn201202x_ldcm_wait_held(struct sn201202x_ldcm_wait *wait, bool mitigated, + unsigned long now) +{ + if (wait->overdue != 2) + return; + if (!mitigated) { + wait->held = false; + return; + } + if (!wait->held) { + wait->held = true; + wait->since = now; + } +} +EXPORT_SYMBOL_IF_KUNIT(sn201202x_ldcm_wait_held); + +/* The measurement was started again: it gets a whole wait from now. */ +VISIBLE_IF_KUNIT void +sn201202x_ldcm_wait_rerun(struct sn201202x_ldcm_wait *wait, unsigned long now) +{ + wait->rerun_due = false; + wait->since = now; +} +EXPORT_SYMBOL_IF_KUNIT(sn201202x_ldcm_wait_rerun); + +/* + * Retried every 500 ms while it measures, but every 2 s once a wait has + * been overrun, as the measurement may never end, and after a failed read, + * which a busy bus may follow with more. A hold is not polled: the next + * update is at its end, unless a controller event comes first. A hold whose + * mitigation did not go through is no hold yet: its command is retried every + * 2 s, as a failed restart. + */ +VISIBLE_IF_KUNIT unsigned int +sn201202x_ldcm_retry_ms(const struct sn201202x_ldcm_wait *wait, + unsigned int read_failures, unsigned long now) +{ + if (wait->overdue == 2 && !wait->rerun_due && wait->held) { + unsigned long end = wait->since + + msecs_to_jiffies(sn201202x_ldcm_hold_ms(wait->holds)); + unsigned int left = time_before(now, end) ? + jiffies_to_msecs(end - now) : 0; + + return max_t(unsigned int, left, SN201202X_LDCM_SLOW_RETRY_MS); + } + return wait->overdue || read_failures ? + SN201202X_LDCM_SLOW_RETRY_MS : SN201202X_LDCM_RETRY_MS; +} +EXPORT_SYMBOL_IF_KUNIT(sn201202x_ldcm_retry_ms); + +/* Not in the busy-wait of suspend-to-idle: the update after the resume decides. */ +VISIBLE_IF_KUNIT unsigned int sn201202x_ldcm_overdue(void *ctx) +{ + struct tps6598x *tps = ctx; + struct sn201202x *sn = tps_to_sn(tps); + struct sn201202x_ldcm_wait *wait = &sn->ldcm_wait; + unsigned int before = wait->overdue; + unsigned int overdue; + + if (READ_ONCE(sn->atomic)) + return 0; + overdue = sn201202x_ldcm_wait_overdue(wait, jiffies); + if (overdue == before) + return overdue; + /* Two warnings per port without a result, the rest at debug level */ + if (overdue == 1 && !wait->holds) + dev_warn(tps->dev, "liquid detection gave no result in %u ms, measuring again\n", + SN201202X_LDCM_STUCK_MS); + else if (overdue == 1) + dev_dbg(tps->dev, "liquid detection: measuring again after hold %u\n", + wait->holds); + else if (wait->holds == 1) + dev_warn(tps->dev, "liquid detection still gave no result; port disabled until it does, measuring again %u ms after mitigation\n", + sn201202x_ldcm_hold_ms(wait->holds)); + else + dev_dbg(tps->dev, "liquid detection: no result, hold %u of %u ms\n", + wait->holds, sn201202x_ldcm_hold_ms(wait->holds)); + return overdue; +} +EXPORT_SYMBOL_IF_KUNIT(sn201202x_ldcm_overdue); + +static void sn201202x_ldcm_held(void *ctx, bool mitigated) +{ + struct tps6598x *tps = ctx; + + sn201202x_ldcm_wait_held(&tps_to_sn(tps)->ldcm_wait, mitigated, jiffies); +} + +VISIBLE_IF_KUNIT int sn201202x_ldcm_rerun(void *ctx) +{ + struct tps6598x *tps = ctx; + struct sn201202x *sn = tps_to_sn(tps); + int ret; + + if (READ_ONCE(sn->atomic)) + return -EAGAIN; + /* + * Sent even if the controller is taken to be unmitigated already, and + * even if this driver never requested a mitigation since the reset: + * the command is what ends the stuck measurement. + */ + sn->ldcm_mitigation_known = false; + sn->ldcm_requested = true; + ret = sn201202x_ldcm_set_mitigation(ctx, false); + /* + * Failed (a bus error, a busy controller, no completion): the + * measurement may not have started again, so the next update sends + * the command again instead of letting the restart's wait run. + */ + if (!ret) + sn201202x_ldcm_wait_rerun(&sn->ldcm_wait, jiffies); + return ret; +} +EXPORT_SYMBOL_IF_KUNIT(sn201202x_ldcm_rerun); + VISIBLE_IF_KUNIT int sn201202x_ldcm_update_state(void *ctx, const struct sn201202x_ldcm_ops *ops, enum typec_contaminant last) @@ -403,24 +623,87 @@ sn201202x_ldcm_update_state(void *ctx, const struct sn201202x_ldcm_ops *ops, int read_ret, ret; read_ret = ops->read(ctx, &contaminant); + if (!read_ret && contaminant != TYPEC_CONTAMINANT_UNKNOWN) + ops->result(ctx, contaminant); + if (!read_ret && contaminant == TYPEC_CONTAMINANT_UNKNOWN) { + /* + * A measurement in progress is not a result. Keep the last + * result and its mitigation; the controller reports the new one + * when it is done. + */ + if (last != TYPEC_CONTAMINANT_UNKNOWN) + return -EINPROGRESS; + /* + * Liquid was reported, and no dry result or reset came since: + * only a failed command lost it. Mitigate and report it again; + * a wet port is never enabled by the waits below. + */ + if (ops->wet(ctx)) { + ret = ops->set_mitigation(ctx, true); + if (ret) + return ret; + ops->publish(ctx, TYPEC_CONTAMINANT_LIQUID); + return -EINPROGRESS; + } + /* + * Without an earlier result (at probe, after a reset) the port + * stays down until there is one: the cable state is applied + * only for a dry connector. Mitigation is not forced on at + * once, as it once was here: a J716s port mitigated in the + * middle of a measurement stayed in it (LDCM state 3) for good, + * across rebinds, with the port disabled and every plug ignored. + * + * A measurement that overruns its wait is made to start again + * by removing mitigation, which is what got that controller out + * of it, with the port still down. If the measurement run again + * overruns its own wait too, the port is held mitigated, still + * down and "unknown", and the controller is made to measure + * again after each hold (10 s, doubled up to 5 min). A hold + * starts only once its mitigation went through: a failed + * command is sent again, never followed by a restart. No time + * makes a port without a result dry: only a dry result read + * from the controller, during a wait or a hold, enables it. + */ + switch (ops->overdue(ctx)) { + case 0: + return -EINPROGRESS; + case 1: + ret = ops->rerun(ctx); + return ret ?: -EINPROGRESS; + default: + ret = ops->set_mitigation(ctx, true); + ops->held(ctx, !ret); + ops->publish(ctx, TYPEC_CONTAMINANT_UNKNOWN); + return ret ?: -EAGAIN; + } + } /* - * A measurement in progress is not a result. Keep the last result and - * its mitigation; the controller reports the new one when it is done. + * A bus error is no evidence of liquid. A port reported dry, which may + * carry a dock or a display, is not mitigated (and its data paths torn + * down) for one failed read: the read is retried, every 2 s, and only + * SN201202X_LDCM_READ_TRIES failures in a row mitigate it. A port + * without a result, or reported wet, is mitigated at once as before. */ - if (!read_ret && contaminant == TYPEC_CONTAMINANT_UNKNOWN && - last != TYPEC_CONTAMINANT_UNKNOWN) - return -EINPROGRESS; + if (read_ret && last == TYPEC_CONTAMINANT_NONE && + ops->read_failures(ctx) < SN201202X_LDCM_READ_TRIES) + return read_ret; if (read_ret || contaminant != TYPEC_CONTAMINANT_NONE) { + /* A failed read or command is no result: liquid stays reported. */ + enum typec_contaminant unread = + last == TYPEC_CONTAMINANT_LIQUID ? TYPEC_CONTAMINANT_LIQUID : + TYPEC_CONTAMINANT_UNKNOWN; + ret = ops->set_mitigation(ctx, true); if (ret) { - ops->publish(ctx, TYPEC_CONTAMINANT_UNKNOWN); + ops->publish(ctx, unread); return ret; } - - ops->publish(ctx, contaminant); - if (read_ret) + if (read_ret) { + ops->publish(ctx, unread); return read_ret; - return contaminant == TYPEC_CONTAMINANT_UNKNOWN ? -EAGAIN : 0; + } + ops->publish(ctx, contaminant); + return 0; } ret = ops->set_mitigation(ctx, false); @@ -431,6 +714,8 @@ sn201202x_ldcm_update_state(void *ctx, const struct sn201202x_ldcm_ops *ops, /* Do not report dry if liquid appeared while mitigation was removed. */ read_ret = ops->read(ctx, &contaminant); + if (!read_ret && contaminant != TYPEC_CONTAMINANT_UNKNOWN) + ops->result(ctx, contaminant); if (!read_ret && contaminant == TYPEC_CONTAMINANT_UNKNOWN) { /* * Removing mitigation can start another measurement. The dry @@ -460,6 +745,12 @@ static const struct sn201202x_ldcm_ops sn201202x_ldcm_ops = { .read = sn201202x_ldcm_read, .set_mitigation = sn201202x_ldcm_set_mitigation, .publish = sn201202x_ldcm_publish, + .result = sn201202x_ldcm_result, + .wet = sn201202x_ldcm_wet, + .overdue = sn201202x_ldcm_overdue, + .held = sn201202x_ldcm_held, + .rerun = sn201202x_ldcm_rerun, + .read_failures = sn201202x_ldcm_read_failures, }; static int sn201202x_ldcm_update(struct tps6598x *tps) @@ -487,12 +778,15 @@ static void sn201202x_ldcm_event(struct tps6598x *tps) static void sn201202x_ldcm_retry(struct tps6598x *tps, int ret) { struct sn201202x *sn = tps_to_sn(tps); + unsigned int delay; + lockdep_assert_held(&tps->lock); if (!sn201202x_ldcm_pending(ret)) dev_err_ratelimited(tps->dev, "liquid-detection status unavailable: %d\n", ret); - mod_delayed_work(system_freezable_wq, &sn->ldcm_work, - msecs_to_jiffies(SN201202X_LDCM_RETRY_MS)); + delay = sn201202x_ldcm_retry_ms(&sn->ldcm_wait, sn->ldcm_read_failures, + jiffies); + mod_delayed_work(system_freezable_wq, &sn->ldcm_work, msecs_to_jiffies(delay)); } static void sn201202x_ldcm_work(struct work_struct *work) @@ -506,13 +800,17 @@ static void sn201202x_ldcm_work(struct work_struct *work) mutex_lock(&tps->lock); last = tps->contaminant; ret = sn201202x_ldcm_update(tps); - /* Apply the cable state again only if the published state changed. */ + /* + * Apply the cable state again only if the published state changed. + * No controller event need come with it: a result read by a retry, at + * the end of a hold for one, enables the port on its own. + */ if (tps->contaminant != last) - mod_delayed_work(system_freezable_wq, &sn->cd.resume_work, 0); - mutex_unlock(&tps->lock); - + cd321x_revalidate(tps); + /* Its delay is taken from the wait, which the lock protects. */ if (ret) sn201202x_ldcm_retry(tps, ret); + mutex_unlock(&tps->lock); } /* @@ -539,6 +837,10 @@ static void sn201202x_ldcm_invalidate(struct tps6598x *tps) sn->ldcm_mitigated = false; sn->ldcm_mitigation_known = false; sn->ldcm_requested = false; + /* Its first measurement gets whole waits, and a reset liquid result is gone */ + sn->ldcm_wait = (struct sn201202x_ldcm_wait){}; + sn->ldcm_wet = false; + sn->ldcm_read_failures = 0; sn201202x_ldcm_event(tps); } @@ -1382,6 +1684,23 @@ static int sn201202x_prepare(struct device *dev) } mutex_lock(&tps->lock); ret = sn201202x_ldcm_update(tps); + /* + * A measurement without an earlier result leaves the port down but + * unmitigated. Enter sleep mitigated, as before, also right after the + * controller was made to measure again. Suspend-to-idle never removes + * the mitigation itself; the waits go on after the resume. + */ + if (ret == -EINPROGRESS && + tps->contaminant == TYPEC_CONTAMINANT_UNKNOWN) + ret = sn201202x_ldcm_set_mitigation(tps, true) ?: -EAGAIN; + /* + * A failed read that left a port reported dry (fewer than + * SN201202X_LDCM_READ_TRIES in a row) keeps that dry snapshot, which + * the sleep may be entered with. + */ + if (ret && ret != -EINPROGRESS && + tps->contaminant == TYPEC_CONTAMINANT_NONE) + ret = 0; mutex_unlock(&tps->lock); /* A measurement in progress keeps the last result; its event wakes us. */ if (ret == -EINPROGRESS) diff --git a/drivers/usb/typec/tipd/tps6598x.h b/drivers/usb/typec/tipd/tps6598x.h index eb77f76379ca4..8337728c6794e 100644 --- a/drivers/usb/typec/tipd/tps6598x.h +++ b/drivers/usb/typec/tipd/tps6598x.h @@ -156,6 +156,24 @@ #define SN201202X_INT_LDCM_UPDATE BIT_ULL(61) #define SN201202X_LDCM_STATUS_LEN 55 +/* + * How long a measurement without an earlier result may run before the + * controller is made to measure again, and then before the port is held + * mitigated, see sn201202x_ldcm_update_state(): ten of its 500 ms retries. + */ +#define SN201202X_LDCM_STUCK_MS 5000 +#define SN201202X_LDCM_RETRY_MS 500 +/* The retry once a measurement has overrun its wait, until a result */ +#define SN201202X_LDCM_SLOW_RETRY_MS 2000 +/* + * How long a port without a result is held mitigated before the controller is + * made to measure again: doubled after each measurement that gives none, up + * to the maximum, until a result. + */ +#define SN201202X_LDCM_HOLD_MS 10000 +#define SN201202X_LDCM_HOLD_MAX_MS 300000 +/* Failed status reads in a row that a port reported dry takes before mitigation */ +#define SN201202X_LDCM_READ_TRIES 3 /* TPS_REG_SYSTEM_POWER_STATE states */ #define TPS_SYSTEM_POWER_STATE_S0 0x00 @@ -330,6 +348,43 @@ struct sn201202x_ldcm_ops { int (*read)(void *ctx, enum typec_contaminant *contaminant); int (*set_mitigation)(void *ctx, bool enable); void (*publish)(void *ctx, enum typec_contaminant contaminant); + /* The controller reported a result (dry or liquid), not a measurement */ + void (*result)(void *ctx, enum typec_contaminant contaminant); + /* Liquid was reported since the last dry result or reset */ + bool (*wet)(void *ctx); + /* + * For a measurement without an earlier result: 0 while it may run on, + * 1 when the controller is to measure again (until ->rerun() succeeded), + * 2 while the port is to be held mitigated, see struct sn201202x_ldcm_wait + */ + unsigned int (*overdue)(void *ctx); + /* + * The hold's mitigation command went through, or failed: the hold runs + * from its first success, and a failure waits for the next one. + */ + void (*held)(void *ctx, bool mitigated); + /* Remove mitigation even if removed already: the controller measures again */ + int (*rerun)(void *ctx); + /* Status reads that failed in a row, the last read included */ + unsigned int (*read_failures)(void *ctx); +}; + +/* + * A port without a result: its first measurement's wait (overdue 0), the + * wait of a measurement the controller was made to start again (1), or a hold + * with mitigation until the next restart (2). Reset by a result or a reset. + */ +struct sn201202x_ldcm_wait { + bool waiting; + /* When the current wait or hold began */ + unsigned long since; + unsigned int overdue; + /* The measurement is to be started again, and was not yet */ + bool rerun_due; + /* The hold's mitigation went through: its time runs, from then */ + bool held; + /* Holds since the last result, which set the next hold's length */ + unsigned int holds; }; struct tipd_data { @@ -497,6 +552,9 @@ struct cd321x { bool macos_usbw_due; /* ... and the controller is in macOS's no-partner state 0x02 since a detach */ bool macos_detached; + /* A poll asked for a read for a plug state not applied, and which state */ + bool poll_revalidated; + bool poll_revalidated_plug; struct cd321x_status update_status; struct delayed_work update_work; @@ -532,6 +590,11 @@ struct sn201202x { bool ldcm_mitigation_known; /* A mitigation was requested since the controller's last reset */ bool ldcm_requested; + struct sn201202x_ldcm_wait ldcm_wait; + /* Liquid reported since the last dry result or reset */ + bool ldcm_wet; + /* Liquid-detection status reads that failed in a row */ + unsigned int ldcm_read_failures; /* J714s: this sleep is entered and left as macOS does, see sn201202x_prepare() */ bool macos_sleep; /* ... and the controller has been sent SLEEP at the prepare and not woken since */ @@ -583,6 +646,7 @@ int cd321x_sleep_events(struct tps6598x *tps, struct cd321x_sleep_state *st); #define CD321X_WAKE_EVENT_LEN 13 int cd321x_wake_events(struct tps6598x *tps, u8 event[CD321X_WAKE_EVENT_LEN], u32 *status, u32 *data_status); +void cd321x_revalidate(struct tps6598x *tps); void tipd_usb_wake(struct tps6598x *tps, bool asleep); int tps6598x_exec_cmd_tmo(struct tps6598x *tps, const char *cmd, size_t in_len, const u8 *in_data, @@ -637,6 +701,18 @@ sn201202x_ldcm_decode(const u8 status[SN201202X_LDCM_STATUS_LEN]); int sn201202x_ldcm_update_state(void *ctx, const struct sn201202x_ldcm_ops *ops, enum typec_contaminant last); +unsigned int sn201202x_ldcm_wait_overdue(struct sn201202x_ldcm_wait *wait, + unsigned long now); +void sn201202x_ldcm_wait_rerun(struct sn201202x_ldcm_wait *wait, + unsigned long now); +void sn201202x_ldcm_wait_held(struct sn201202x_ldcm_wait *wait, + bool mitigated, unsigned long now); +unsigned int sn201202x_ldcm_hold_ms(unsigned int holds); +unsigned int sn201202x_ldcm_retry_ms(const struct sn201202x_ldcm_wait *wait, + unsigned int read_failures, unsigned long now); +int sn201202x_ldcm_set_mitigation(void *ctx, bool enable); +unsigned int sn201202x_ldcm_overdue(void *ctx); +int sn201202x_ldcm_rerun(void *ctx); #endif #endif /* __TPS6598X_H__ */ diff --git a/tools/testing/selftests/drivers/gpu/apple-dcp/transaction.py b/tools/testing/selftests/drivers/gpu/apple-dcp/transaction.py index 7e98084906081..a64937adbe67b 100644 --- a/tools/testing/selftests/drivers/gpu/apple-dcp/transaction.py +++ b/tools/testing/selftests/drivers/gpu/apple-dcp/transaction.py @@ -437,7 +437,12 @@ def probe(root): """ helpers += '\n#define of_machine_is_compatible(name) (!strcmp((name), \"apple,t8140\"))\n#define BIT_ULL(n) (1ULL << (n))\n#define SN201202X_INT_DP_SID_UPDATE BIT_ULL(37)\n' helpers += block(core, 'static bool cd321x_uses_dp_sid(') + '\n' + block(core, 'static u64 cd321x_irq_mask(') + '\n' + helpers += ''.join(line + '\n' for line in core.splitlines() if line.startswith('#define TIPD_PROBE_READ_')) + helpers += block(core, 'static int tipd_read_plug_state(') + '\n' unit += helpers + block(core, 'int tipd_init(') + r""" +static int power_reads_failing; +static bool flaky_power_status(struct tps6598x *t) { (void)t;return !(power_reads_failing&&power_reads_failing--); } +static void ready(struct tps6598x *t) { (void)t; } static void probe_initial(struct cd321x *c) { initial(c);ordinary_port=true;phy_provider.safe_after_role=true;c->tps.irq=1;data.register_port=register_port;data.read_data_status=read_data_status;data.read_power_status=tps6598x_read_power_status;data.connect=connect; connect_error=irq_error=register_error=node_puts=mux_get_error=0; @@ -461,6 +466,14 @@ def probe(root): assert(tipd_init(&c.tps)==-ENODEV&&reset_calls==0&&stops==0&&phy_calls==0&&unregisters==0&&node_puts==1); probe_initial(&c);register_error=-EIO; assert(tipd_init(&c.tps)==-EIO&&reset_calls==0&&stops==0&&phy_calls==0&&unregisters==0); + /* A plug present at probe whose status reads fail: retried, then left to ->ready() where there is one */ + probe_initial(&c);data.read_power_status=flaky_power_status;power_reads_failing=2;data.ready=NULL; + assert(!tipd_init(&c.tps)&&!power_reads_failing); + probe_initial(&c);data.read_power_status=flaky_power_status;power_reads_failing=100;data.ready=NULL; + assert(tipd_init(&c.tps)==-EINVAL); + probe_initial(&c);data.read_power_status=flaky_power_status;power_reads_failing=100;data.ready=ready; + assert(!tipd_init(&c.tps)&&!(c.tps.status&TPS_STATUS_PLUG_PRESENT)); + data.ready=NULL;power_reads_failing=0; puts("PASS actual probe rollback preserves primary errors, blocks reset after SAFE/role/registration errors and resets only a patched controller"); } """