From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=192.198.163.12 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796114195; b=MApWGz1LNt70qwoEgYq1EL9WyHnMF9TMfoUCPsDpk9zeI6YnMtJl/UV4kcX6N+LYas/E VJJHpNu+/qPkm25muvw8CQN1i4h1/RVc0/2ZIXLqjsrO4+1Eij3AFqXJkvTtaCVCxIs4e 7dK2bqAECGs7lWzx3An60ToOtZLMn6sb32cFgwUwSsrW8NOa9xcc3WI9rzXn1wyflQWtO wddLO5oeKNMQ1FU0wt1RI0pAIMWJudfQS4y6LdVhVku20yU6jdArlLgp83vx4dk905jy4 JnrW3xiTIk86CS9a+SSZLeHi2d1OeeXfTEoHbCXoeBmzdnHzmsTSFvpy30fnDhTTbfA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796114195; h=DMARC-Filter:DKIM-Signature:X-CSE-ConnectionGUID:X-CSE-MsgGUID: X-IronPort-AV:X-IronPort-AV:Received:X-CSE-ConnectionGUID: X-CSE-MsgGUID:X-ExtLoop1:X-IronPort-AV:Received:Received:Date:From:To: Cc:Subject:Message-ID:References:MIME-Version:Content-Type: Content-Disposition:In-Reply-To; bh=Q4Oiw/R1niluaHaS4Gk2VnHftO8z6G9dCEPgAB1Mch8=; b=RlviESw6GFmg4cZV+VBE9YomTaNMoAGu1ymHZ9xSdPies1lu2GpvspY0l0WxiiKcj01C Z37KRc7bOj6TyDMVoQIgDmdLPVgsibAoWmAPGqUBt300cf0tcFHDhfoun/VTW1fnEHB2l zRJ4SEGtkh8i3Xe9/euygJv8NdCw9H9dGbtBgxByzQG7hh8jVOJAgq0B5YhyvakUVac9r eI2mxGCfgUXlpiSA1Dh3vDoHCKGFLlqriHtIpZXfmk3zec7GEv1xTB9LErT/AVDf+On7P VYmR7bvsLCnWzmQcx0KSAZh5KrRdPV+0eMv/APDtMUxv+XjwkUhgK8TrDq12hHT6dlw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=linux.intel.com; spf=pass smtp.mailfrom=linux.intel.com; dkim=pass header.d=intel.com header.i=@intel.com header.a=rsa-sha256 header.s=Intel header.b=DDOczc4q; arc=none smtp.remote-ip=192.198.163.12 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.12]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 85EDA606A327; Tue, 1 Dec 2026 08:36:32 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 85EDA606A327 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key, unprotected) header.d=intel.com header.i=@intel.com header.a=rsa-sha256 header.s=Intel header.b=DDOczc4q DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1796114194; x=1827650194; h=date:from:to:cc:subject:message-id:references: mime-version:in-reply-to; bh=Q4Oiw/R1niluaHaS4Gk2VnHftO8z6G9dCEPgAB1Mch8=; b=DDOczc4q4iWLUOtLVf7LenqtWoRO4qCu87go7H6zxuhSJoCWFtoP1DGJ CbQ5wezGqWo91dvQrDF9unbAGhpFSrjRIPPqgL/yysfnPV5pAFoQPZ+1g OxI5fE7YcB7HexhNgxAAxIzITZTL62zEMXLtVnCI1RRcFjccbQ68z2gwt hUm4whQoPVS2mSbp3/pUTBY7GkD8anvOc60oNMi1veo/vRp8Kp5yHNGz5 ZrQ+cfM5bb+MRVh70+zQE7NuSSCbyKshmMINdyQL54u2UKvnkkKJD/w5m NcU5tZthcc8ZuXUDl4jHVJu+cOVu8lxwj1aCXe2h3+uuv2V06sr/t0BRc w==; X-CSE-ConnectionGUID: DkQTc4yFRaOBmn+WW2NBAw== X-CSE-MsgGUID: LFHHIQF8T8q4SGsbJdPkMQ== X-IronPort-AV: E=McAfee;i="6800,10657,11982"; a="4885616" X-IronPort-AV: E=Sophos;i="6.27,244,1787036400"; d="scan'208";a="4885616" Received: from orviesa009.jf.intel.com ([10.64.159.149]) by fmvoesa106.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 01 Dec 2026 00:36:25 -0800 X-CSE-ConnectionGUID: OgOdGSIkTNu0ONbNdbOOrw== X-CSE-MsgGUID: W4uduKFoRSeMLuh8dqN94Q== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.27,244,1787036400"; d="scan'208";a="13555404" Received: from black.igk.intel.com ([10.91.253.5]) by orviesa009.jf.intel.com with ESMTP; 01 Dec 2026 00:36:19 -0800 Received: by black.igk.intel.com (Postfix, from userid 1001) id 332CA98; Tue, 01 Dec 2026 09:36:18 +0100 (CET) Date: Tue, 1 Dec 2026 09:36:18 +0100 From: Mika Westerberg To: Konrad Dybcio Cc: Vinod Koul , Neil Armstrong , Manivannan Sadhasivam , Bjorn Andersson , Konrad Dybcio , Abel Vesa , Stephen Boyd , Brian Masney , Jerome Brunet , Andreas Noever , Mika Westerberg , Yehezkel Bernat , Mario Limonciello , Philipp Zabel , Rob Herring , Krzysztof Kozlowski , Conor Dooley , linux-arm-msm@vger.kernel.org, linux-phy@lists.infradead.org, linux-clk@vger.kernel.org, linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org, devicetree@vger.kernel.org, usb4-upstream@oss.qualcomm.com Subject: Re: [PATCH RFC DNM 13/15] thunderbolt: Introduce Qualcomm USB4 router driver Message-ID: <20261201083618.GV953667@black.igk.intel.com> References: <20261124-topic-next-20261116-fsm-mvp-v1-0-a9b45c69cd38@oss.qualcomm.com> <20261124-topic-next-20261116-fsm-mvp-v1-13-a9b45c69cd38@oss.qualcomm.com> X-Mailing-List: linux-clk@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <20261124-topic-next-20261116-fsm-mvp-v1-13-a9b45c69cd38@oss.qualcomm.com> Hi, On Tue, Nov 24, 2026 at 09:01:42PM +0100, Konrad Dybcio wrote: > Introduce a driver for the in-house Qualcomm USB4 router driver. > It consists of resource enablement, uC firmware loading and > communication, Type-C infrastructure and power state handling. > > Unlike many other implementations, the power state of both the router > and the native protocol controllers has to be manually coordinated, > which much of this driver ends up doing. > > Signed-off-by: Konrad Dybcio > --- > drivers/thunderbolt/Kconfig | 13 + > drivers/thunderbolt/Makefile | 3 + > drivers/thunderbolt/eeprom.c | 17 - > drivers/thunderbolt/qcom.c | 2299 +++++++++++++++++++++++++++++++++++++++++ > drivers/thunderbolt/qcom.h | 251 +++++ > drivers/thunderbolt/tb.h | 20 + > drivers/thunderbolt/tb_regs.h | 3 + > 7 files changed, 2589 insertions(+), 17 deletions(-) > > diff --git a/drivers/thunderbolt/Kconfig b/drivers/thunderbolt/Kconfig > index 294b3227a54582536433cbb391057b99bb9df352..94654d3e41137b630fc4723adc4949d8df8b0617 100644 > --- a/drivers/thunderbolt/Kconfig > +++ b/drivers/thunderbolt/Kconfig > @@ -64,6 +64,19 @@ config USB4_DMA_TEST > To compile this driver a module, choose M here. The module will be > called thunderbolt_dma_test. > > +config USB4_QCOM > + tristate "Qualcomm USB4/Thunderbolt Support" > + depends on USB4 > + depends on OF > + depends on ARCH_QCOM || COMPILE_TEST > + depends on COMMON_CLK && PM > + help > + Say Y here to add support for the USB4 and Thunderbolt host > + routers found on Qualcomm SoCs. > + > + To compile the driver as a module, choose M here. The module > + will be called thunderbolt_qcom. > + > config USB4_STREAM > tristate "Stream data over Thunderbolt/USB4 cable" > depends on USB4_CONFIGFS > diff --git a/drivers/thunderbolt/Makefile b/drivers/thunderbolt/Makefile > index beb054c3126b1445d26b4c14de039d23367a5100..8f56afd7d6b47e95324adbd95f50692ca7b3582d 100644 > --- a/drivers/thunderbolt/Makefile > +++ b/drivers/thunderbolt/Makefile > @@ -11,6 +11,9 @@ thunderbolt-$(CONFIG_USB4_CONFIGFS) += configfs.o > thunderbolt-${CONFIG_USB4_KUNIT_TEST} += test.o > CFLAGS_test.o += $(DISABLE_STRUCTLEAK_PLUGIN) > > +thunderbolt_qcom-objs := qcom.o > +obj-${CONFIG_USB4_QCOM} += thunderbolt_qcom.o > + > thunderbolt_dma_test-${CONFIG_USB4_DMA_TEST} += dma_test.o > obj-$(CONFIG_USB4_DMA_TEST) += thunderbolt_dma_test.o > > diff --git a/drivers/thunderbolt/eeprom.c b/drivers/thunderbolt/eeprom.c > index 2acfe83928b6d8cc95520479f90b595d7137b829..74be3295a0a241f17c833d143d0674e18ffb9bb0 100644 > --- a/drivers/thunderbolt/eeprom.c > +++ b/drivers/thunderbolt/eeprom.c > @@ -197,23 +197,6 @@ static int tb_eeprom_read_n(struct tb_switch *sw, u16 offset, u8 *val, > return tb_eeprom_active(sw, false); > } > > -static u8 tb_crc8(u8 *data, int len) > -{ > - int i, j; > - u8 val = 0xff; > - for (i = 0; i < len; i++) { > - val ^= data[i]; > - for (j = 0; j < 8; j++) > - val = (val << 1) ^ ((val & 0x80) ? 7 : 0); > - } > - return val; > -} > - > -static u32 tb_crc32(void *data, size_t len) > -{ > - return ~crc32c(~0, data, len); > -} Maybe make this separate preparatory patch? > - > #define TB_DROM_DATA_START 13 > #define TB_DROM_HEADER_SIZE 22 > #define USB4_DROM_HEADER_SIZE 16 > diff --git a/drivers/thunderbolt/qcom.c b/drivers/thunderbolt/qcom.c > new file mode 100644 > index 0000000000000000000000000000000000000000..721feeccd7364e21bb1e2ecb2d97967bee5bc5cf > --- /dev/null > +++ b/drivers/thunderbolt/qcom.c > @@ -0,0 +1,2299 @@ > +// SPDX-License-Identifier: GPL-2.0-only > +/* > + * Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries. > + */ > + > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include Are you sure you need all of these? > + > +#include "nhi.h" > +#include "nhi_regs.h" > +#include "qcom.h" > +#include "tb.h" > + > +/* > + * The router's operational ability is directly tied to the mode and power state of the QMPPHY > + * (and thus to cable presence). The router (transitively) sources its clocks from the PHY, > + * although it may operate in reduced capacity even when it's off (by muxing its inputs to the > + * 19.2 MHz XO clock source). That 'reduced mode' allows for register access, but not TBT linkup. TB or Thunderbolt Ditto everywhere > + * > + * At probe time, that mechanism is used to bootstrap the router regardless of the plug state > + * (since forcefully switching the PHY to USB4 mode would disrupt a USB3/DP link on that port). > + * Then, upon a USB4/TBT3 typec_mux event the PHY is reconfigured, the clocks are reconfigured to > + * follow, the uC is brought online and a BEGIN_HSE command is sent to the router, which can take > + * a couple seconds to complete. > + * > + * Upon unplug, the router has to be immediately torn down (including the clocks scaffolding and > + * the PHY) to unblock the port for a potentially soon-to-be-plugged USB3/DP device. > + * > + * Suspend/resume sequences require a handshake with the uC, as that influences the state of some > + * clocks and failing to follow that ends up in a hard crash of the entire platform. > + * > + * This is modeled as an explicit state machine (enum qcusb4_hw_state), with all transitions > + * serialized by state_lock: > + * > + * OFF --probe--> IDLE <--reconcile--> LINK > + * \ / > + * +---> ASLEEP <----+ (runtime/system sleep, uC handshake) > + * > + * The Type-C callbacks record what state they want (->desired_state) and kick the reconcile work, > + * which runs the IDLE <-> LINK transitions. Cable removal is LINK -> IDLE, which cycles the domain > + * and the router through a full sleep (TBT layer _SLP first, then the uC handshake) to hand the PHY > + * back, then cold-restarts the router on XO (a sleep exit would have the FW go for the PHY it no > + * longer has), so the TBT core sees a regular unplug. The PHY must never be taken away outside of > + * that handshake. For the same reason, sleep entered from IDLE is exited through a cold restart, as > + * the cable may have been plugged in meanwhile. Two gates protect against unclocked access from > + * outside the state machine: uc_online for FW commands and mmio_ok for the PCIe reset controller. > + */ > + > +/* ROUTER_CS registers */ > +#define ROUTER_CS_CS_1 0x4 > +#define ROUTER_CS_CS_5 0x14 CS_CS? And we do have ROUTER_CS alread in tb_regs.h. If these are not the same perhaps prefix them QCOM_ or so to avoid confusion? > + > +#define is_usb4v2(q) ((q)->match_data->entries[(q)->instance_idx].usb4v2_capable) Maybe inline function instead to make this somewhat more readable. > + > +/* LANEn_CS registers */ > +#define UC_RAM_DROM_OFFSET_V1 0xdf20 > +#define UC_RAM_DROM_OFFSET_V2 0xff20 > +#define UC_RAM_DROM_OFFSET(q) (is_usb4v2((q)) ? \ > + UC_RAM_DROM_OFFSET_V2 : UC_RAM_DROM_OFFSET_V1) > + > +#define LANEn_CS_CS_1(q) (is_usb4v2(q) ? 0x58 : 0x50) > +#define LANEn_CS_CS_1_ADAPTER_STATE GENMASK(29, 26) > +#define LANEn_CS_CS_18(q) (is_usb4v2(q) ? 0xac : 0xa4) > +#define LANEn_CS_CS_19(q) (is_usb4v2(q) ? 0xb0 : 0xa8) Here also depending on where these are used but maybe more readable is to have a separate function or block. > + > +/* PORT_GROUP registers */ > +#define PORT_GROUP_LANE_CFG 0x0 > +#define LANE_CFG_CLx_SUPPORT(q) (is_usb4v2(q) ? GENMASK(21, 19) : GENMASK(23, 21)) > +#define PORT_GROUP_FCM_2_CFG 0x64 > + #define FCM_2_CFG_DIS_ANY_HOPID BIT(6) I prefer not to indent these. Let's try to be consistent across. > + > +/* SIDEBAND registers */ > +#define SIDEBAND_TX_CTRL 0xc > + #define SB_TX_CTRL_TX_ENABLE BIT(31) > + > +/* UC_PER registers */ > +#define UC_PER_CONTROL 0x1000 > + #define PER_CONTROL_UC_ENABLE BIT(0) > +#define UC_PER_SW_TRIGGERS_UC_IRQ 0x100c > +#define UC_PER_FW_INT_STATUS_SET 0x1010 > +#define UC_PER_FW_INT_CLR 0x1018 > + #define PER_FW_INT_CLR_CLR BIT(0) > +#define UC_PER_FW_INT_MASK 0x101c > + #define PER_FW_INT_MASK_FW_INT BIT(0) > + > +/* CFG registers */ > +#define CFG_SYS_CLK_CGC 0x0 > + #define SYS_CLK_CGC_SYS_CLK_EN_USBAP BIT(7) > + > +/* USBAP_CONFIG registers */ > +#define USBAP_CFG_SYNC_RST_USB3 0x0 > + #define RST_USB3_RST BIT(0) > +#define USBAP_CFG_USB4_MODE 0x4 > + #define USB4_MODE_USB4_MODE BIT(0) > +#define USBAP_CFG_RXTERM_BACKDOOR_CTRL 0x10 > +#define USBAP_CFG_RXTERM_BACKDOOR_STS 0x14 > + #define RXTERM_RX_TERM BIT(0) > +#define USBAP_CFG_PHYSTATUS_EN 0x18 > + #define PHYSTATUS_EN_EN BIT(0) > + > +/* PCIEAP_CONFIG registers */ > +#define PCIEAP_CONFIG_SYNC_RST 0x0 > + #define PCIEAP_SYNC_RST_PCIE BIT(0) > +#define PCIEAP_CONFIG_IRQ_STATUS 0x0c > + #define PCIEAP_IRQ_PE_DEASSERT BIT(3) > + #define PCIEAP_IRQ_PE_ASSERT BIT(2) > +#define PCIEAP_CONFIG_IRQ_MASK 0x10 > +#define PCIEAP_CONFIG_IRQ_CLEAR 0x14 > + > +/* PCIEAP (USB4 v2 router) registers */ > +#define PCIEAP_TX_CFG 0x0 > + #define PCIEAP_TX_CFG_EN_FLUSH_MISSING_SOP BIT(22) > + > +/* ROUTER_CONFIG registers */ > +#define ROUTER_CONFIG_CS_6 0x18 > + #define CS_6_ROUTER_READY BIT(24) > +#define ROUTER_CONFIG_CS_7 0x1c > +#define ROUTER_CONFIG_CS_8 0x20 Also ROUTER_CONFIG_CS and ROUTER_CS_CS? Its fine if these match the datasheet but if you invented them then I suggest rethink the naming so that it's not that confusing ;-) > + > +struct qcom_usb4_fw_event { > + u8 opcode; > + u8 params[]; > +} __packed; Please check if you really need the __packed. > +struct qcom_usb4_fw_events { > + __le16 num_entries; > + __le16 entry_size; > + __le32 next_entry_index; > + __le16 num_unread_entries; /* This only guarantees num_unread >= num_entries */ > + u8 res[6]; > + struct qcom_usb4_fw_event entries[]; > +} __packed; > + > +#define FW_EV_BUF_SIZE SZ_8K > + > +enum qcom_usb4_fw_cmd { > + FW_CMD_BEGIN_HSE = 0x1, > + > + FW_CMD_ENABLE_PHY_PRESETS = 0x2, > +#define PHY_PRESETS_DEFAULT (BIT(0) | BIT(1) | BIT(5) | BIT(6) | BIT(10) | BIT(11)) Also perhaps move these outside of the enum? And while there maybe kernel-doc? > + > + FW_CMD_LIMIT_LOGLEVEL = 0x3, > +#define LOG_LEVEL_ALL 0 > +#define LOG_LEVEL_WARN 1 > +#define LOG_LEVEL_ERR 2 > +#define LOG_LEVEL_NONE 3 > + > + /* Only makes sense to call this after FW_MSG_SLEEP_READY */ > + FW_CMD_COMPLETE_SLEEP = 0x4, > + > + /* Reconfigure the internals to restore the link (or handle an in-sleep disconnect) */ > + FW_CMD_WAKEUP = 0x5, > +#define WAKEUP_REASON_DEVICE 0 > +#define WAKEUP_REASON_CBL_DIS 1 > +#define WAKEUP_REASON_CBL_CON 2 > +#define WAKEUP_REASON_SYSTEM 3 > + > + /* The router alone can't differentiate between physical and logical disconnects */ > + FW_CMD_PHYS_DISCONNECT = 0x6, > + > + FW_CMD_DEPRECATED = 0x7, This is not used anywhere so drop it (also ditto similar if there are). > + > + /* USB4v2 introduced more tuning presets */ > + FW_CMD_ENABLE_PHY_PRESETS_MSB = 0x8, > + > + /* Notify the uC that we're done with the Host Router Reset */ > + FW_CMD_HRR_DONE = 0x9, > + > + FW_CMD_WA_DONE = 0xa, > +#define WA_DONE_USBAP_RXTERM 1 > +}; > + > +/* Must be called with the mux clocks enabled */ > +static int qcusb4_select_rx_clock_source(struct qcom_usb4 *qcusb4, > + bool fast_clock) qcom_usb4_? > +{ > + struct clk *rx0 = qcusb4->mux_clks[MUX_CLK_PHY_RX0].clk; > + struct clk *rx1 = qcusb4->mux_clks[MUX_CLK_PHY_RX1].clk; > + unsigned long rate = fast_clock ? ULONG_MAX : 19200000; > + int ret; > + > + clk_disable_unprepare(rx0); > + clk_disable_unprepare(rx1); > + > + /* No error unwinding on purpose, failure here is borderline impossible and catastrophic */ > + ret = clk_set_rate(rx0, rate); > + if (ret) { > + dev_err(qcusb4->nhi.dev, "Failed to select RX0 clock source: %d\n", ret); "failed .." To be consistent with the rest of the driver. Ditto everywhere. > + return ret; > + } > + > + ret = clk_set_rate(rx1, rate); > + if (ret) { > + dev_err(qcusb4->nhi.dev, "Failed to select RX1 clock source: %d\n", ret); > + return ret; > + } > + > + ret = clk_prepare_enable(rx0); > + if (ret) { > + dev_err(qcusb4->nhi.dev, "Failed to re-enable the RX0 clock: %d\n", ret); > + return ret; > + } > + > + ret = clk_prepare_enable(rx1); > + if (ret) > + dev_err(qcusb4->nhi.dev, "Failed to re-enable the RX1 clock: %d\n", ret); It's fine if these fail that they are set to prepared and configured? > + > + return ret; > +} > + > +static int __qcusb4_fw_cmd(struct qcom_usb4 *qcusb4, enum qcom_usb4_fw_cmd msg, u32 args) __qcom_usb4_? > +{ > + u32 cmd; > + u32 val; > + > + lockdep_assert_held(&qcusb4->uc_cmd_lock); > + > + /* The uC is either down, or about to be */ > + if (!qcusb4->uc_online) { > + dev_warn_ratelimited(qcusb4->nhi.dev, > + "Dropping cmd=0x%x arg=0x%x, the uC is not running\n", > + msg, args); Does this happen how often? Is ratelimited needed? Maybe this can be debug instead or drop completely? > + return -EAGAIN; > + } > + > + pm_runtime_mark_last_busy(qcusb4->nhi.dev); > + > + cmd = FIELD_PREP(GENMASK(31, 8), args) | > + FIELD_PREP(GENMASK(7, 0), msg); > + > + writel(cmd, qcusb4->uc_per + UC_PER_SW_TRIGGERS_UC_IRQ); > + > + if (readl_poll_timeout(qcusb4->uc_per + UC_PER_SW_TRIGGERS_UC_IRQ, > + val, !val, 10, 10 * USEC_PER_MSEC)) { Maybe #define for the timeout? > + dev_err(qcusb4->nhi.dev, "Firmware failed to clear cmd 0x%08x\n", cmd); > + return -ETIMEDOUT; > + } > + > + return 0; > +} > + > +static int qcusb4_fw_cmd(struct qcom_usb4 *qcusb4, enum qcom_usb4_fw_cmd msg, u32 args) > +{ > + guard(mutex)(&qcusb4->uc_cmd_lock); > + > + return __qcusb4_fw_cmd(qcusb4, msg, args); > +} > + > +/* > + * Send a command that the FW acks with an event. The completion is re-armed under uc_cmd_lock, > + * so that a stale ack from an earlier command can't satisfy the wait. > + */ > +static int qcusb4_fw_cmd_wait(struct qcom_usb4 *qcusb4, enum qcom_usb4_fw_cmd msg, u32 args, > + struct completion *done, unsigned int timeout_ms) > +{ > + int ret; > + > + scoped_guard(mutex, &qcusb4->uc_cmd_lock) { int ret; > + reinit_completion(done); > + Drop this empty line. > + ret = __qcusb4_fw_cmd(qcusb4, msg, args); > + if (ret) > + return ret; > + } > + > + if (!wait_for_completion_timeout(done, msecs_to_jiffies(timeout_ms))) > + return -ETIMEDOUT; > + > + return 0; > +} > + > +enum qcom_usb4_fw_msg { > + FW_MSG_MESSAGE = 0x1, > + FW_MSG_ERR = 0x2, > + FW_MSG_TEST_RES = 0x3, > + /* Fires after a successful handling of FW_CMD_COMPLETE_SLEEP */ You can use kernel-doc as well to document these. > + FW_MSG_SLEEP_DONE = 0x4, > + /* Fires after a packet with opcode = USB4_SB_OPCODE_ROUTER_OFFLINE is handled */ > + FW_MSG_EXIT_OFFLINE = 0x5, > + /* Fires after any sort of (incl. logical) disconnect has been detected */ > + FW_MSG_DISCONNECT = 0x6, > + /* Fires after a successful handling of ROUTER_CS_5.SLP becoming set */ > + FW_MSG_SLEEP_READY = 0x7, > + /* "A specific software workaround must be applied immediately" */ > + FW_MSG_TRIGGER = 0x8, > +#define WA_TRIGGER_USBAP_RXTERM 3 > + > + /* "found hotplug with HopId > 1" */ > + FW_MSG_CHAINED_HOTPLUG = 0x9, > + /* Fires after a HRR request arrives, needs to be acked with FW_CMD_HRR_DONE */ > + FW_MSG_HOST_RESET = 0xa, > + FW_MSG_HSE_STATS = 0xb, > + FW_MSG_FOM_STATS = 0xc, > +}; Move this to the top of the file. > + > +static void qcom_usb4_fw_clear_irq(struct qcom_usb4 *qcusb4) > +{ > + /* Clear the irq - this resets the unread message counter on the FW side */ > + writel(PER_FW_INT_CLR_CLR, qcusb4->uc_per + UC_PER_FW_INT_CLR); > + readl(qcusb4->uc_per + UC_PER_FW_INT_CLR); > +} > + > +static bool qcom_usb4_fw_event_ring_valid(struct qcom_usb4 *qcusb4, u32 data_off, > + u16 total_entries, u16 entry_size, u32 *ring_size) > +{ > + u32 entries_size; > + > + if (!total_entries || entry_size < sizeof(struct qcom_usb4_fw_event)) > + return false; > + > + if (check_mul_overflow((u32)total_entries, (u32)entry_size, &entries_size)) You really need the casts here and below? > + return false; > + > + if (check_add_overflow((u32)sizeof(struct qcom_usb4_fw_events), > + entries_size, ring_size)) > + return false; Functions that modify the passed parameters should do so only when they actually succeed. > + > + if (*ring_size > FW_EV_BUF_SIZE) > + return false; > + > + if (data_off > qcusb4->uc_ram_size - *ring_size) > + return false; > + > + return true; > +} > + > +/* > + * The FW exposes a ring buffer with no 'event processing done' acking (i.e. the event may be gone > + * before we process it). I think you should wrap these. > + * > + * Accessing that data happens by directly reading the uC's RAM at a given offset. > + * The uC is clocked by the SYS clock (part of qcusb4->mux_clks), so care must be taken to disable > + * this IRQ, to prevent system crashes. > + */ > +static irqreturn_t qcom_usb4_fw_irq_handler(int irq, void *data) > +{ > + struct qcom_usb4 *qcusb4 = data; > + u16 total_entries; > + u16 entry_size; > + u32 ring_size; > + u32 data_off; > + > + if (unlikely(!qcusb4->fw_ev_rb_base)) { Drop the unlikely. It reads better actually if you do: if (qcusb4->fw_ev_rb_base) return IRQ_WAKE_THREAD; data_off = readl(qcusb4->uc_per + UC_PER_FW_INT_STATUS_SET); ... > + data_off = readl(qcusb4->uc_per + UC_PER_FW_INT_STATUS_SET); > + if (!IS_ALIGNED(data_off, sizeof(u32)) || > + data_off > qcusb4->uc_ram_size - sizeof(struct qcom_usb4_fw_events)) { > + dev_err_ratelimited(qcusb4->nhi.dev, > + "Invalid FW event ring offset 0x%x\n", > + data_off); > + qcom_usb4_fw_clear_irq(qcusb4); > + return IRQ_HANDLED; > + } > + > + total_entries = lower_16_bits(readl(qcusb4->uc_ram + data_off)); > + entry_size = upper_16_bits(readl(qcusb4->uc_ram + data_off)); > + > + if (!qcom_usb4_fw_event_ring_valid(qcusb4, data_off, total_entries, > + entry_size, &ring_size)) { > + dev_err_ratelimited(qcusb4->nhi.dev, > + "Invalid FW event ring: off=0x%x entries=%u size=%u\n", > + data_off, total_entries, entry_size); > + qcom_usb4_fw_clear_irq(qcusb4); > + return IRQ_HANDLED; > + } > + > + qcusb4->fw_ev_rb_base = qcusb4->uc_ram + data_off; > + qcusb4->fw_ev_total_entry_num = total_entries; > + qcusb4->fw_ev_entry_size = entry_size; > + qcusb4->fw_ev_ring_size = ring_size; > + qcusb4->next_fw_ev_idx = 0; > + } > + > + return IRQ_WAKE_THREAD; > +} > + > +static int qcusb4_usbap_rxterm_wa(struct qcom_usb4 *qcusb4) > +{ > + struct device *dev = qcusb4->nhi.dev; > + u32 val; > + int ret; > + > + val = readl(qcusb4->usb_cfg + USBAP_CFG_PHYSTATUS_EN); > + writel(val & ~PHYSTATUS_EN_EN, qcusb4->usb_cfg + USBAP_CFG_PHYSTATUS_EN); > + > + val = readl(qcusb4->usb_cfg + USBAP_CFG_SYNC_RST_USB3); > + writel(val | RST_USB3_RST, qcusb4->usb_cfg + USBAP_CFG_SYNC_RST_USB3); > + > + ret = reset_control_assert(qcusb4->usb_pipe_reset); > + if (ret) > + return dev_err_probe(dev, ret, "Couldn't assert usb_pipe reset\n"); > + > + usleep_range(100, 150); > + > + val = readl(qcusb4->usb_cfg + USBAP_CFG_PHYSTATUS_EN); > + writel(val | PHYSTATUS_EN_EN, qcusb4->usb_cfg + USBAP_CFG_PHYSTATUS_EN); > + > + val = readl(qcusb4->cfg + CFG_SYS_CLK_CGC); > + writel(val & ~SYS_CLK_CGC_SYS_CLK_EN_USBAP, qcusb4->cfg + CFG_SYS_CLK_CGC); > + > + ret = reset_control_deassert(qcusb4->usb_pipe_reset); > + if (ret) > + return dev_err_probe(dev, ret, "Couldn't deassert usb_pipe reset\n"); > + > + usleep_range(100, 150); > + > + val = readl(qcusb4->usb_cfg + USBAP_CFG_SYNC_RST_USB3); > + writel(val & ~RST_USB3_RST, qcusb4->usb_cfg + USBAP_CFG_SYNC_RST_USB3); > + > + val = readl(qcusb4->usb_cfg + USBAP_CFG_RXTERM_BACKDOOR_CTRL); > + writel(val | RXTERM_RX_TERM, qcusb4->usb_cfg + USBAP_CFG_RXTERM_BACKDOOR_CTRL); > + > + return qcusb4_fw_cmd(qcusb4, FW_CMD_WA_DONE, WA_DONE_USBAP_RXTERM); > +} > + > +static irqreturn_t qcom_usb4_fw_irq_thread(int irq, void *data) > +{ > + struct qcom_usb4 *qcusb4 = data; > + struct qcom_usb4_fw_events *fw_events = qcusb4->fw_events_buf; > + struct device *dev = qcusb4->nhi.dev; > + u32 entry_size = qcusb4->fw_ev_entry_size; > + u16 num_new_entries, num_to_end; > + void __iomem *entries; > + u32 next_entry_index; > + > + if (!qcusb4->fw_ev_rb_base || !qcusb4->fw_ev_ring_size) { How this can happen BTW? > + qcom_usb4_fw_clear_irq(qcusb4); > + return IRQ_HANDLED; > + } > + > + /* > + * Clear before reading the write index. Otherwise an event written in between gets its IRQ > + * cleared, but not read, and sits in the ring until something else fires the IRQ. Wrap these too. Ditto everywhere. > + */ > + qcom_usb4_fw_clear_irq(qcusb4); > + > + next_entry_index = readl(qcusb4->fw_ev_rb_base + > + offsetof(struct qcom_usb4_fw_events, next_entry_index)); Would this read better if it is called "index"? Ditto for the other long names where it makes things more readable. > + if (next_entry_index >= qcusb4->fw_ev_total_entry_num) { > + dev_err_ratelimited(dev, "Invalid FW event index %u/%u\n", > + next_entry_index, qcusb4->fw_ev_total_entry_num); Same question about the ratelimit here. Seems it is common to get invalid events from the FW? > + return IRQ_HANDLED; > + } > + > + /* Check for ringbuffer rollover */ > + if (next_entry_index < qcusb4->next_fw_ev_idx) > + num_new_entries = qcusb4->fw_ev_total_entry_num - qcusb4->next_fw_ev_idx > + + next_entry_index; > + else > + num_new_entries = next_entry_index - qcusb4->next_fw_ev_idx; > + > + /* Grab the header and the new entries (wrapping around) from the uC's RAM */ > + memcpy_fromio(fw_events, qcusb4->fw_ev_rb_base, sizeof(*fw_events)); > + entries = qcusb4->fw_ev_rb_base + offsetof(struct qcom_usb4_fw_events, entries); > + num_to_end = min_t(u16, num_new_entries, > + qcusb4->fw_ev_total_entry_num - qcusb4->next_fw_ev_idx); > + memcpy_fromio((void *)fw_events->entries + entry_size * qcusb4->next_fw_ev_idx, > + entries + entry_size * qcusb4->next_fw_ev_idx, entry_size * num_to_end); > + memcpy_fromio(fw_events->entries, entries, entry_size * (num_new_entries - num_to_end)); > + /* Make sure we get the events before proceeding */ > + mb(); > + > + if (le16_to_cpu(fw_events->num_unread_entries) >= qcusb4->fw_ev_total_entry_num) > + dev_warn_ratelimited(dev, "FW event ring overflowed, events were lost\n"); > + > + for (int i = 0; i < num_new_entries; i++) { > + struct qcom_usb4_fw_event *ev; > + u16 payload_size = qcusb4->fw_ev_entry_size - sizeof(*ev); Reverse-christmas tree where possible. > + > + ev = (void *)fw_events->entries + > + (qcusb4->fw_ev_entry_size * qcusb4->next_fw_ev_idx++); This is hard to parse and easy to get wrong so perhaps split it to a couple of statements. Also check if you need the cast here. > + > + switch (ev->opcode) { > + case FW_MSG_MESSAGE: > + dev_warn(qcusb4->nhi.dev, "FW MSG: %.*s\n", payload_size, ev->params); > + break; > + case FW_MSG_ERR: > + if (payload_size < 3) { > + dev_err(dev, "Malformed FW error event\n"); > + break; > + } > + > + u8 error = ev->params[0]; > + u16 extradata = (ev->params[2] << 8) | ev->params[1]; Please then make this a block with { } > + > + dev_err(qcusb4->nhi.dev, "Received FW error: %u (data: 0x%x)\n", > + error, extradata); > + break; > + case FW_MSG_SLEEP_DONE: > + complete(&qcusb4->sleep_acked); > + dev_err(qcusb4->nhi.dev, "FW Sleep done\n"); dev_err()? > + break; > + case FW_MSG_EXIT_OFFLINE: > + dev_err(qcusb4->nhi.dev, "FW exit offline\n"); ??? Okay I stoped here for now. As you say this is RFC so not yet production quality ;-) Thanks for posting, though. I now understand better the challenges "more OS control" poses. But we are getting there.. :)