Skip to content
Draft
27 changes: 18 additions & 9 deletions drivers/thunderbolt/apple.c
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,7 @@ static const struct apple_cio_hw apple_cio_t6040_hw = {
* still gets its link back without a replug.
*/
#define APPLE_CIO_LINK_LOST_INTERVAL (60 * HZ)
#define APPLE_CIO_LINK_LOST_RESTART_MS 30000

static bool resume_recovery;
module_param(resume_recovery, bool, 0644);
Expand Down Expand Up @@ -633,6 +634,8 @@ struct apple_cio {
struct ratelimit_state link_lost_limit; /* survives domain rebuilds */
u64 link_lost_cycle_activity; /* remote activity of this domain at the last cycle */
bool link_lost_restart; /* protected by lock: cycle requested */
u32 link_lost_retry_cable; /* cable info before the requested cycle */
unsigned long link_lost_retry_until;
struct notifier_block pm_notifier;
struct notifier_block pm_notifier_check;
struct notifier_block reboot_notifier;
Expand Down Expand Up @@ -4288,6 +4291,9 @@ static void apple_cio_link_lost_work(struct work_struct *work)
acio->link_lost_cycles++;
WRITE_ONCE(acio->link_lost_cycle_activity, activity);
acio->link_lost_restart = true;
acio->link_lost_retry_cable = acio->current_cable_info;
acio->link_lost_retry_until = jiffies +
msecs_to_jiffies(APPLE_CIO_LINK_LOST_RESTART_MS);
dev_info(acio->dev,
"peer link lost with the cable attached, restarting the port (%u/%u)\n",
acio->link_lost_cycles, APPLE_CIO_LINK_LOST_CYCLES);
Expand Down Expand Up @@ -4350,6 +4356,7 @@ static int apple_cio_prepare(struct device *dev)
acio->resume_link_failed = false;
/* A cycle CD321x did not take up is not carried into this sleep. */
acio->resume_recovery_active = false;
acio->link_lost_retry_cable = 0;
}
return 0;
}
Expand Down Expand Up @@ -4531,16 +4538,18 @@ static int apple_cio_tbt_switch_set(struct typec_thunderbolt_switch_dev *sw,
if (acio->resume_recovery_failed)
return -EHOSTDOWN;
ret = apple_cio_start(acio);
if (!ret)
if (!ret) {
bool retry = acio->link_lost_retry_cable == acio->current_cable_info &&
time_before(jiffies, acio->link_lost_retry_until);

WRITE_ONCE(acio->sleep_restart_owed, false);
/*
* The port had a dock before its power-off for the sleep: one that is not
* found again gets the port restarted, as a peer lost with the cable in.
*/
if (!ret && acio->sleep_peer_wanted) {
WRITE_ONCE(acio->link_lost_gen, READ_ONCE(acio->start_gen));
mod_delayed_work(system_freezable_wq, &acio->link_lost_work,
msecs_to_jiffies(APPLE_CIO_LINK_LOST_MS));
acio->link_lost_retry_cable = 0;
/* An absent peer cannot generate a new link-loss event. */
if (retry || acio->sleep_peer_wanted) {
WRITE_ONCE(acio->link_lost_gen, READ_ONCE(acio->start_gen));
mod_delayed_work(system_freezable_wq, &acio->link_lost_work,
msecs_to_jiffies(APPLE_CIO_LINK_LOST_MS));
}
}
if (ret && apple_cio_recovering(acio)) {
acio->resume_recovery_failed = true;
Expand Down
4 changes: 3 additions & 1 deletion drivers/thunderbolt/tb.c
Original file line number Diff line number Diff line change
Expand Up @@ -3404,7 +3404,9 @@ static void tb_handle_hotplug(struct work_struct *work)
tb_switch_xhci_disconnect(sw);
} else {
tb_port_dbg(port,
"got unplug event for disconnected port, ignoring\n");
"got unplug event for disconnected port\n");
/* Link training can fail before a peer is discovered. */
tb_host_link_lost(tb, port);
}
} else if (port->remote) {
tb_port_dbg(port, "got plug event for connected port, ignoring\n");
Expand Down
Loading