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
47 changes: 43 additions & 4 deletions drivers/thunderbolt/domain.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <linux/module.h>
#include <linux/pm_runtime.h>
#include <linux/slab.h>
#include <linux/suspend.h>
#include <linux/random.h>
#include <crypto/sha2.h>
#include <crypto/utils.h>
Expand Down Expand Up @@ -338,6 +339,10 @@ static bool tb_domain_event_cb(void *data, enum tb_cfg_pkg_type type,
{
struct tb *tb = data;

/* During noirq only XDomain discovery requests can receive a reply. */
if (READ_ONCE(tb->noirq_ctl_retained) && type != TB_CFG_PKG_XDOMAIN_REQ)
return true;

if (!tb->cm_ops->handle_event) {
tb_warn(tb, "domain does not have event handler\n");
return true;
Expand Down Expand Up @@ -528,11 +533,34 @@ void tb_domain_remove(struct tb *tb)
}
EXPORT_SYMBOL_FOR_MODULES(tb_domain_remove, "thunderbolt_apple");

static bool tb_domain_retain_noirq_control(struct tb *tb)
{
struct tb_port *port;
bool xdomain = false;

lockdep_assert_held(&tb->lock);

if (!pm_suspend_no_platform() ||
!(tb->nhi->quirks & QUIRK_NO_SYSTEM_SLEEP) ||
!tb->root_switch)
return false;

tb_switch_for_each_port(tb->root_switch, port) {
if (tb_port_has_remote(port))
return false;
if (port->xdomain)
xdomain = true;
}

return xdomain;
}

/**
* tb_domain_suspend_noirq() - Suspend a domain
* @tb: Domain to suspend
*
* Suspends all devices in the domain and stops the control channel.
* Suspends all devices in the domain. Stops the control channel unless
* an awake host router needs it for direct XDomain discovery.
*
* Return: %0 on success, negative errno otherwise.
*/
Expand All @@ -548,8 +576,14 @@ int tb_domain_suspend_noirq(struct tb *tb)
mutex_lock(&tb->lock);
if (tb->cm_ops->suspend_noirq)
ret = tb->cm_ops->suspend_noirq(tb);
if (!ret)
tb_ctl_stop(tb->ctl);
if (!ret) {
if (tb_domain_retain_noirq_control(tb)) {
WRITE_ONCE(tb->noirq_ctl_retained, true);
tb_dbg(tb, "XDomain control channel retained for s2idle\n");
} else {
tb_ctl_stop(tb->ctl);
}
}
mutex_unlock(&tb->lock);

return ret;
Expand All @@ -570,9 +604,14 @@ int tb_domain_resume_noirq(struct tb *tb)
int ret = 0;

mutex_lock(&tb->lock);
tb_ctl_start(tb->ctl);
if (!tb->noirq_ctl_retained)
tb_ctl_start(tb->ctl);
if (tb->cm_ops->resume_noirq)
ret = tb->cm_ops->resume_noirq(tb);
if (tb->noirq_ctl_retained) {
WRITE_ONCE(tb->noirq_ctl_retained, false);
tb_dbg(tb, "XDomain control channel remained active\n");
}
mutex_unlock(&tb->lock);

return ret;
Expand Down
44 changes: 40 additions & 4 deletions drivers/thunderbolt/xdomain.c
Original file line number Diff line number Diff line change
Expand Up @@ -759,6 +759,23 @@ static void stop_handshake(struct tb_xdomain *xd)
__stop_handshake(xd);
}

/*
* The XDomain discovery requests answered while the control channel is kept
* through s2idle (tb->noirq_ctl_retained): they only read state.
*/
static bool tb_xdp_noirq_request(enum tb_xdp_type type)
{
switch (type) {
case UUID_REQUEST_OLD:
case UUID_REQUEST:
case PROPERTIES_REQUEST:
case LINK_STATE_STATUS_REQUEST:
return true;
default:
return false;
}
}

static void tb_xdp_handle_request(struct work_struct *work)
{
struct xdomain_request_work *xw = container_of(work, typeof(*xw), work);
Expand All @@ -777,19 +794,28 @@ static void tb_xdp_handle_request(struct work_struct *work)
sequence = xhdr->length_sn & TB_XDOMAIN_SN_MASK;
sequence >>= TB_XDOMAIN_SN_SHIFT;

/*
* Handle the request under tb->lock. tb_domain_suspend_noirq() and
* tb_domain_resume_noirq() set and clear noirq_ctl_retained under it,
* so a request queued before the control channel was retained cannot
* run a non-discovery request (or touch a port) during noirq, and the
* port read below cannot overlap the domain's suspend or resume.
*/
mutex_lock(&tb->lock);
if (tb->root_switch)
uuid = kmemdup(tb->root_switch->uuid, sizeof(*uuid), GFP_KERNEL);
else
uuid = NULL;
mutex_unlock(&tb->lock);

if (!uuid) {
if (!uuid || (READ_ONCE(tb->noirq_ctl_retained) &&
!tb_xdp_noirq_request(pkg->type))) {
tb_xdp_error_response(ctl, route, sequence, ERROR_NOT_READY);
mutex_unlock(&tb->lock);
goto out;
}

xd = tb_xdomain_find_by_route_locked(tb, route);
/* tb->lock is held: the _locked helper would take it again. */
xd = tb_xdomain_find_by_route(tb, route);
if (xd)
update_property_block(xd);

Expand Down Expand Up @@ -831,7 +857,8 @@ static void tb_xdp_handle_request(struct work_struct *work)
* timing out, we will restart the handshake now that we
* received UUID request from the remote host.
*/
if (!ret && xd && xd->state == XDOMAIN_STATE_ERROR) {
if (!READ_ONCE(tb->noirq_ctl_retained) && !ret && xd &&
xd->state == XDOMAIN_STATE_ERROR) {
mutex_lock(&xd->lock);
if (!xd->removing) {
dev_dbg(&xd->dev, "restarting handshake\n");
Expand Down Expand Up @@ -925,6 +952,7 @@ static void tb_xdp_handle_request(struct work_struct *work)
}

tb_xdomain_put(xd);
mutex_unlock(&tb->lock);

if (ret) {
tb_warn(tb, "failed to send XDomain response for %#x\n",
Expand Down Expand Up @@ -2666,6 +2694,14 @@ bool tb_xdomain_handle_request(struct tb *tb, enum tb_cfg_pkg_type type,
if (length < sizeof(*hdr) / 4 - sizeof(hdr->xd_hdr) / 4)
return true;

if (READ_ONCE(tb->noirq_ctl_retained)) {
if (type != TB_CFG_PKG_XDOMAIN_REQ || !uuid_equal(&hdr->uuid, &tb_xdp_uuid))
return true;
if (!tb_xdp_noirq_request(hdr->type))
return true;
tb_dbg(tb, "XDomain discovery request %#x during s2idle\n", hdr->type);
}

/*
* Handle XDomain discovery protocol packets directly here. For
* other protocols (based on their UUID) we call registered
Expand Down
2 changes: 2 additions & 0 deletions include/linux/thunderbolt.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ enum tb_security_level {
* @remote_activity: Successful stock remote discovery exchanges
* @nhi: Pointer to the NHI structure
* @ctl: Control channel for this domain
* @noirq_ctl_retained: Control rings stayed active during noirq suspend
* @wq: Ordered workqueue for all domain specific work
* @root_switch: Root switch of this domain
* @cm_ops: Connection manager specific operations vector
Expand All @@ -88,6 +89,7 @@ struct tb {
atomic64_t remote_activity;
struct tb_nhi *nhi;
struct tb_ctl *ctl;
bool noirq_ctl_retained;
struct workqueue_struct *wq;
struct tb_switch *root_switch;
const struct tb_cm_ops *cm_ops;
Expand Down
Loading