Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
171 changes: 169 additions & 2 deletions drivers/usb/dwc3/dwc3-apple.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@
#include <linux/workqueue.h>

#include "dwc3-apple-state.h"

/* drivers/usb/typec/tipd, through symbol_get(): no load dependency on that module. */
int tipd_port_replug(struct usb_role_switch *sw, u8 seconds);
#include "glue.h"
#include "io.h"

Expand Down Expand Up @@ -227,6 +230,11 @@ struct dwc3_apple {
bool sleep_phy_wait;
/* The start after a sleep was left to the role switch, which may also come too early. */
bool sleep_phy_pending;
/*
* TEST (J714s, T6050 Type-C): a SuperSpeed device was on the host when a sleep stopped it;
* the start after that sleep looks for it once, see dwc3_apple_ss_check().
*/
bool ss_retry;
/*
* J714s: a host on a USB4 router's USB3 tunnel, kept suspended in place through the
* router's sleep (see dwc3_apple_tunnel_keep()): the PHY's result once taken, the role the
Expand Down Expand Up @@ -1069,11 +1077,15 @@ static int dwc3_apple_exit(struct dwc3_apple *appledwc);
/* A hub that hasn't enumerated by then didn't come back. */
#define DWC3_APPLE_HUB_CHECK_MS 5000

/* Any way a fixed-hub host came out of a sleep, its hub must be there a little later. */
/*
* Any way a fixed-hub host came out of a sleep, its hub must be there a little later. TEST
* (J714s): so must a Type-C host's SuperSpeed device, see dwc3_apple_ss_check().
*/
static void dwc3_apple_check_hub_later(struct dwc3_apple *appledwc)
{
lockdep_assert_held(&appledwc->lock);
if (appledwc->fixed_hub && !appledwc->shutting_down && !appledwc->sleep_dead &&
if ((appledwc->fixed_hub || appledwc->ss_retry) &&
!appledwc->shutting_down && !appledwc->sleep_dead &&
!appledwc->sleep_stopped && appledwc->state == DWC3_APPLE_HOST &&
appledwc->core_active && !appledwc->core_exit_pending &&
READ_ONCE(appledwc->sleep_phase) == DWC3_APPLE_SLEEP_IDLE)
Expand Down Expand Up @@ -1213,6 +1225,147 @@ static unsigned long dwc3_apple_hub_wake_ports(struct dwc3_apple *appledwc)
return ports;
}

/*
* TEST (J714s, T6050 Type-C): the device on the USB2 root port, referenced for the caller,
* whether one is on the SuperSpeed root port, and with @portsc the xHCI's first PORTSC words,
* @nports of them, at most two. Takes the xHCI device's lock and the root hubs'.
*/
static int dwc3_apple_ss_ports(struct dwc3_apple *appledwc, struct usb_device **usb2, bool *ss,
u32 *portsc, unsigned int *nports)
{
struct platform_device *xhci = appledwc->dwc.xhci;
struct usb_hcd *hcd;
int ret = -ENODEV;

lockdep_assert_held(&appledwc->lock);
*usb2 = NULL;
*ss = false;
if (nports)
*nports = 0;
if (!xhci)
return -ENODEV;

device_lock(&xhci->dev);
hcd = xhci->dev.driver ? platform_get_drvdata(xhci) : NULL;
if (hcd && hcd->shared_hcd) {
struct usb_hcd *hcds[] = { hcd, hcd->shared_hcd };

for (unsigned int i = 0; i < ARRAY_SIZE(hcds); i++) {
struct usb_device *root = hcds[i]->self.root_hub, *child;

if (!root)
continue;
usb_lock_device(root);
child = usb_hub_find_child(root, 1);
if (root->speed >= USB_SPEED_SUPER)
*ss = child != NULL;
else if (child && !*usb2)
*usb2 = usb_get_dev(child);
usb_unlock_device(root);
}
/* CAPLENGTH, then HCSPARAMS1's MaxPorts, read as words. */
if (portsc && nports && hcd->regs) {
void __iomem *op = hcd->regs + (readl(hcd->regs) & 0xff);

*nports = min(readl(hcd->regs + 4) >> 24, 2U);
for (unsigned int i = 0; i < *nports; i++)
portsc[i] = readl(op + 0x400 + 0x10 * i);
}
ret = 0;
}
device_unlock(&xhci->dev);

return ret;
}

/* TEST (J714s): a T6050 Type-C host, whose SuperSpeed device is looked for after a sleep. */
static bool dwc3_apple_ss_port(struct dwc3_apple *appledwc)
{
return !appledwc->fixed_hub && appledwc->role_sw && dwc3_apple_sleep_board() &&
of_device_is_compatible(appledwc->dev->of_node, "apple,t6050-dwc3");
}

/* TEST (J714s): as a sleep stops such a host, whether a SuperSpeed device is on it. */
static bool dwc3_apple_ss_seen(struct dwc3_apple *appledwc)
{
struct usb_device *usb2;
bool ss;

lockdep_assert_held(&appledwc->lock);
if (!dwc3_apple_ss_port(appledwc) || appledwc->state != DWC3_APPLE_HOST ||
!appledwc->core_active || appledwc->core_exit_pending ||
dwc3_apple_ss_ports(appledwc, &usb2, &ss, NULL, NULL))
return false;
usb_put_dev(usb2);

return ss;
}

/*
* TEST (J714s, T6050 Type-C): the USB3 hub behind a DP alt mode port (Dell S2725QC: 0bda:5409
* and 0bda:0409, PHY mode 3, DP2) brings its SuperSpeed side up 0.26 to 0.28 s after its USB2
* side enumerates, in 28 of the 29 wakes that had it and at all but two boots that had it,
* even where xHCI had been running for 1.8 s by then. In 12 of 41 wakes, and at one boot,
* that one attempt failed and nothing made another; the USB2 hub stayed. The log shows those
* wakes as the good ones: one start of the PHY's mode 3, the PIPE switch without a warning,
* xHCI, the USB2 hub, within a few ms, with or without the wait for the PHY. Once after the
* start that follows a sleep in which a SuperSpeed device was there: if none is back while a
* USB2 device is, log the xHCI's PORTSC words and the core's LTSSM: which side gave up (the
* device in Rx.Detect, or the host's link) decides the fix, which is then taken from what
* macOS does on that port. Log only: nothing is reset, and neither the PHY nor the controller
* is touched (in mode 3 the USB3 PHY's power-off stops the whole mode with the PIPE, the
* display's link included, combo_quiesce()).
*/
static void dwc3_apple_ss_check(struct dwc3_apple *appledwc)
{
struct usb_device *usb2 = NULL;
u32 portsc[2] = {}, ltssm = 0;
unsigned int nports = 0;
bool ss = false;

scoped_guard(mutex, &appledwc->lock) {
if (!appledwc->ss_retry)
return;
appledwc->ss_retry = false;
if (appledwc->shutting_down || appledwc->sleep_dead || appledwc->sleep_stopped ||
!appledwc->core_active || appledwc->core_exit_pending ||
READ_ONCE(appledwc->sleep_phase) != DWC3_APPLE_SLEEP_IDLE ||
appledwc->state != DWC3_APPLE_HOST ||
dwc3_apple_ss_ports(appledwc, &usb2, &ss, portsc, &nports))
return;
if (ss || !usb2) {
if (ss)
dev_info(appledwc->dev,
"TEST: SuperSpeed device back after the sleep\n");
usb_put_dev(usb2);
return;
}
ltssm = dwc3_readl(&appledwc->dwc, DWC3_GDBGLTSSM);
}

/*
* J714s: no host-side reset brings that SuperSpeed side back (a USB2 reset, a root port
* warm reset or power cycle, a PIPE reset, xHCI rebuilt: 8 of 8 lost wakes, 11-18); a
* replug does. Have the port controller replug the port, once: the display on it goes dark
* for a few seconds, and both of the hub's sides enumerate again from the attach. The
* replug runs on the port controller's work, after this check.
*/
{
int (*replug)(struct usb_role_switch *sw, u8 seconds) = symbol_get(tipd_port_replug);
int ret = -ENOENT;

if (replug) {
ret = replug(appledwc->role_sw, 1);
symbol_put(tipd_port_replug);
}
dev_warn(appledwc->dev,
"SuperSpeed side of %04x:%04x not back after the sleep (PORTSC %#010x %#010x (%u), LTSSM %#010x): port replug requested (%d)\n",
le16_to_cpu(usb2->descriptor.idVendor), le16_to_cpu(usb2->descriptor.idProduct),
portsc[0], portsc[1], nports, ltssm, ret);
}
usb_put_dev(usb2);
}

/*
* Once after a sleep: if the hub isn't there, stop the host and start it again, with the
* hub's link down for a moment first, as unbinding and binding the driver does. This also
Expand All @@ -1224,6 +1377,12 @@ static void dwc3_apple_hub_check(struct work_struct *work)
hub_check);
int ret;

/* TEST (J714s): a Type-C host's SuperSpeed device after a sleep. */
if (!appledwc->fixed_hub) {
dwc3_apple_ss_check(appledwc);
return;
}

guard(mutex)(&appledwc->lock);
if (appledwc->shutting_down || appledwc->sleep_dead || appledwc->sleep_stopped ||
!appledwc->core_active || appledwc->core_exit_pending ||
Expand Down Expand Up @@ -1334,6 +1493,7 @@ static int dwc3_usb_role_switch_set(struct usb_role_switch *sw, enum usb_role ro
case USB_ROLE_NONE:
/* Nothing to do if no cable is connected */
appledwc->sleep_phy_pending = false;
appledwc->ss_retry = false;
return 0;
case USB_ROLE_HOST:
case USB_ROLE_DEVICE:
Expand All @@ -1342,11 +1502,16 @@ static int dwc3_usb_role_switch_set(struct usb_role_switch *sw, enum usb_role ro
* before the port controller's mode request. The error goes back as it is, so the
* port controller tries again, without a line in the log.
*/
/* TEST (J714s): only the start after a sleep looks for its SuperSpeed device. */
if (!appledwc->sleep_phy_pending)
appledwc->ss_retry = false;
appledwc->sleep_phy_wait = appledwc->sleep_phy_pending;
ret = dwc3_apple_init(appledwc, dwc3_apple_state_for_role(role));
appledwc->sleep_phy_wait = false;
if (ret != -ENOLINK)
appledwc->sleep_phy_pending = false;
if (!ret)
dwc3_apple_check_hub_later(appledwc);
return ret;
default:
dev_err(appledwc->dev, "Invalid target role: %d\n", role);
Expand Down Expand Up @@ -1403,6 +1568,8 @@ static int dwc3_apple_sleep_stop(struct dwc3_apple *appledwc)
appledwc->sleep_state = appledwc->state;
appledwc->sleep_wakeup = device_may_wakeup(appledwc->dev);
appledwc->sleep_stopped = true;
/* TEST (J714s): looked for once after the start, see dwc3_apple_ss_check(). */
appledwc->ss_retry = dwc3_apple_ss_seen(appledwc);
}
ret = dwc3_apple_exit(appledwc);
if (!ret)
Expand Down
139 changes: 139 additions & 0 deletions drivers/usb/typec/tipd/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -3475,6 +3475,143 @@ static int cd321x_reset_inherited_connection(struct tps6598x *tps, u32 *status)
return 0;
}

/* The ports tipd_port_replug() looks up by the USB role switch their data role drives. */
static LIST_HEAD(tipd_ports);
static DEFINE_MUTEX(tipd_ports_lock);

static void tipd_replug_work(struct work_struct *work);

static void tipd_port_list_add(struct tps6598x *tps)
{
INIT_LIST_HEAD(&tps->port_node);
INIT_WORK(&tps->replug_work, tipd_replug_work);
if (!tps->role_sw)
return;
mutex_lock(&tipd_ports_lock);
list_add_tail(&tps->port_node, &tipd_ports);
mutex_unlock(&tipd_ports_lock);
}

static void tipd_port_list_del(struct tps6598x *tps)
{
if (!tps->port_node.next)
return;
mutex_lock(&tipd_ports_lock);
list_del_init(&tps->port_node);
mutex_unlock(&tipd_ports_lock);
cancel_work_sync(&tps->replug_work);
}

/* The J714s's port controllers take "DISC" unlocked with this key, the reversed target identifier. */
static const u8 tipd_replug_key_j714s[4] = { '4', '1', '7', 'J' };

static bool tipd_replug_supported(struct tps6598x *tps)
{
return of_machine_is_compatible("apple,j714s") &&
device_is_compatible(tps->dev, "apple,sn201202x");
}

/*
* As cd321x_reset_inherited_connection() does it: unlocked, "DISC", the app mode back, locked
* again. The port lock is held only for the commands: meanwhile the interrupt handler sees the
* partner go and come back, and sets the mux, the alternate modes and the data role again.
* The commands are read with the gap the sleep's own command is (CD321X_SSPS_READ_GAP_US): read
* back to back while the partner attaches again, the relock ended late, or in SPMI parity faults
* and -EIO. A relock that fails is tried once more.
*/
#define TIPD_REPLUG_RELOCK_TRIES 2
static void tipd_replug_work(struct work_struct *work)
{
struct tps6598x *tps = container_of(work, struct tps6598x, replug_work);
static const u8 locked[4];
u8 seconds = tps->replug_seconds, result = 0;
unsigned long timeout;
int ret, lock_ret, mode = -1;
unsigned int tries;

mutex_lock(&tps->lock);
ret = tps6598x_exec_cmd_gap(tps, "LOCK", sizeof(tipd_replug_key_j714s), tipd_replug_key_j714s,
sizeof(result), &result, 1000, 0, CD321X_SSPS_READ_GAP_US);
if (!ret && result)
ret = -EIO;
if (!ret)
ret = tps6598x_exec_cmd_gap(tps, "DISC", sizeof(seconds), &seconds, 0, NULL, 5000, 0,
CD321X_SSPS_READ_GAP_US);
mutex_unlock(&tps->lock);

if (!ret) {
msleep(seconds * 1000 + 100);
timeout = jiffies + msecs_to_jiffies(2000);
do {
mutex_lock(&tps->lock);
mode = tps6598x_check_mode(tps);
mutex_unlock(&tps->lock);
if (mode == TPS_MODE_APP || mode == TPS_MODE_APP1)
break;
msleep(20);
} while (time_before(jiffies, timeout));
if (mode != TPS_MODE_APP && mode != TPS_MODE_APP1)
ret = mode < 0 ? mode : -ETIMEDOUT;
}

for (tries = 1; ; tries++) {
mutex_lock(&tps->lock);
lock_ret = tps6598x_exec_cmd_gap(tps, "LOCK", sizeof(locked), locked, 0, NULL, 5000, 0,
CD321X_SSPS_READ_GAP_US);
mutex_unlock(&tps->lock);
if (!lock_ret || tries >= TIPD_REPLUG_RELOCK_TRIES)
break;
msleep(500);
}

if (ret || lock_ret)
dev_warn(tps->dev, "port replug for its partner failed: %d (unlock result %u), locked again: %d (try %u)\n",
ret, result, lock_ret, tries);
else
dev_info(tps->dev, "port replugged for its partner (%u s disconnected), locked again (try %u)\n",
seconds, tries);
}

/**
* tipd_port_replug - disconnect and reattach a port, as a cable pulled and plugged back does
* @sw: the USB role switch the port drives
* @seconds: how long the port stays disconnected
*
* The controller's "DISC" command: it detaches the partner for @seconds and attaches it again
* on its own, with a new contract, its alternate modes entered and the mux and the data role set
* again. For a partner that a system sleep left in a state only a replug ends: on the J714s a
* DP alt mode hub whose SuperSpeed side does not come back after some wakes (dwc3-apple). A
* display on the port goes dark meanwhile. The replug runs on the port's own work, after this
* returns: 0 when it is queued, -EBUSY if one already is, -EOPNOTSUPP on a controller that is
* not known to take it (the J714s's SN201202x only), -ENODEV if no port drives @sw.
*/
int tipd_port_replug(struct usb_role_switch *sw, u8 seconds)
{
struct tps6598x *tps;
int ret = -ENODEV;

mutex_lock(&tipd_ports_lock);
list_for_each_entry(tps, &tipd_ports, port_node) {
if (tps->role_sw != sw)
continue;
ret = -EOPNOTSUPP;
if (!tipd_replug_supported(tps))
break;
ret = -EBUSY;
if (work_pending(&tps->replug_work))
break;
tps->replug_seconds = seconds;
ret = queue_work(system_freezable_wq, &tps->replug_work) ? 0 : -EBUSY;
dev_info(tps->dev, "port replug for its partner requested (%u s disconnected): %d\n",
seconds, ret);
break;
}
mutex_unlock(&tipd_ports_lock);

return ret;
}
EXPORT_SYMBOL_GPL(tipd_port_replug);

int tipd_init(struct tps6598x *tps)
{
struct fwnode_handle *fwnode;
Expand Down Expand Up @@ -3640,6 +3777,7 @@ int tipd_init(struct tps6598x *tps)
}

tipd_debugfs_register(tps);
tipd_port_list_add(tps);
if (tps->data->ready)
tps->data->ready(tps);

Expand Down Expand Up @@ -3673,6 +3811,7 @@ void tipd_remove(struct tps6598x *tps)
{
int ret = 0;

tipd_port_list_del(tps);
tipd_debugfs_unregister(tps);

if (!tps->irq)
Expand Down
Loading
Loading