[PATCH v4 07/14] thunderbolt: Move ICM mailbox functions to icm.c

From: Chunfeng Yun

Date: Sat Dec 05 2026 - 03:34:31 EST


In preparation for modularizing PCIe NHI and ICM support as a separate
driver, clean up internal helper functions so that they do not need to
be exported:

nhi_mailbox_cmd() and nhi_mailbox_mode() are only used by the Intel
firmware Connection Manager (icm.c) to exchange mailbox commands with
firmware running on Intel PCIe NHIs. The Thunderbolt core framework,
software connection manager, and non-PCIe platform controllers do
not use this mailbox interface. Move both functions into icm.c and
mark them static.

Signed-off-by: Chunfeng Yun <chunfeng.yun@xxxxxxxxxxxx>
---
v4: drop &nhi->interrupt_work init which moved into the patch 03/14 in this series
v3: fix a indent
v2: new patch
---
drivers/thunderbolt/icm.c | 59 ++++++++++++++++++++++++++++++++++++++
drivers/thunderbolt/nhi.c | 60 ---------------------------------------
drivers/thunderbolt/nhi.h | 2 --
3 files changed, 59 insertions(+), 62 deletions(-)

diff --git a/drivers/thunderbolt/icm.c b/drivers/thunderbolt/icm.c
index f2c387d226f0..b01638b9f51d 100644
--- a/drivers/thunderbolt/icm.c
+++ b/drivers/thunderbolt/icm.c
@@ -45,6 +45,7 @@
#define ICM_RETRIES 3
#define ICM_APPROVE_TIMEOUT 10000 /* ms */
#define ICM_MAX_LINK 4
+#define ICM_MAILBOX_TIMEOUT 500 /* ms */

static bool start_icm;
module_param(start_icm, bool, 0444);
@@ -269,6 +270,64 @@ static int pcie2cio_write(struct icm *icm, enum tb_cfg_space cs,
return pci2cio_wait_completion(icm, 5000);
}

+/**
+ * nhi_mailbox_cmd() - Send a command through NHI mailbox
+ * @nhi: Pointer to the NHI structure
+ * @cmd: Command to send
+ * @data: Data to be send with the command
+ *
+ * Sends mailbox command to the firmware running on NHI.
+ *
+ * Return: %0 on success, negative errno otherwise.
+ */
+static int nhi_mailbox_cmd(struct tb_nhi *nhi, enum nhi_mailbox_cmd cmd, u32 data)
+{
+ ktime_t timeout;
+ u32 val;
+
+ iowrite32(data, nhi->iobase + REG_INMAIL_DATA);
+
+ val = ioread32(nhi->iobase + REG_INMAIL_CMD);
+ val &= ~(REG_INMAIL_CMD_MASK | REG_INMAIL_ERROR);
+ val |= REG_INMAIL_OP_REQUEST | cmd;
+ iowrite32(val, nhi->iobase + REG_INMAIL_CMD);
+
+ timeout = ktime_add_ms(ktime_get(), ICM_MAILBOX_TIMEOUT);
+ do {
+ val = ioread32(nhi->iobase + REG_INMAIL_CMD);
+ if (!(val & REG_INMAIL_OP_REQUEST))
+ break;
+ usleep_range(10, 20);
+ } while (ktime_before(ktime_get(), timeout));
+
+ if (val & REG_INMAIL_OP_REQUEST)
+ return -ETIMEDOUT;
+ if (val & REG_INMAIL_ERROR)
+ return -EIO;
+
+ return 0;
+}
+
+/**
+ * nhi_mailbox_mode() - Return current firmware operation mode
+ * @nhi: Pointer to the NHI structure
+ *
+ * The function reads current firmware operation mode using NHI mailbox
+ * registers and returns it to the caller.
+ *
+ * Return: &enum nhi_fw_mode.
+ */
+static enum nhi_fw_mode nhi_mailbox_mode(struct tb_nhi *nhi)
+{
+ u32 val;
+
+ val = ioread32(nhi->iobase + REG_OUTMAIL_CMD);
+ val &= REG_OUTMAIL_CMD_OPMODE_MASK;
+ val >>= REG_OUTMAIL_CMD_OPMODE_SHIFT;
+
+ return (enum nhi_fw_mode)val;
+}
+
static bool icm_match(const struct tb_cfg_request *req,
const struct ctl_pkg *pkg)
{
diff --git a/drivers/thunderbolt/nhi.c b/drivers/thunderbolt/nhi.c
index d55e1f03b68a..0b13ddf6c99d 100644
--- a/drivers/thunderbolt/nhi.c
+++ b/drivers/thunderbolt/nhi.c
@@ -35,8 +35,6 @@
*/
#define RING_E2E_RESERVED_HOPID RING_FIRST_USABLE_HOPID

-#define NHI_MAILBOX_TIMEOUT 500 /* ms */
-
static bool host_reset = true;
module_param(host_reset, bool, 0444);
MODULE_PARM_DESC(host_reset, "reset USB4 host router (default: true)");
@@ -964,64 +962,6 @@ int tb_ring_throttling(struct tb_ring *ring, unsigned int interval_nsec)
}
EXPORT_SYMBOL_GPL(tb_ring_throttling);

-/**
- * nhi_mailbox_cmd() - Send a command through NHI mailbox
- * @nhi: Pointer to the NHI structure
- * @cmd: Command to send
- * @data: Data to be send with the command
- *
- * Sends mailbox command to the firmware running on NHI.
- *
- * Return: %0 on success, negative errno otherwise.
- */
-int nhi_mailbox_cmd(struct tb_nhi *nhi, enum nhi_mailbox_cmd cmd, u32 data)
-{
- ktime_t timeout;
- u32 val;
-
- iowrite32(data, nhi->iobase + REG_INMAIL_DATA);
-
- val = ioread32(nhi->iobase + REG_INMAIL_CMD);
- val &= ~(REG_INMAIL_CMD_MASK | REG_INMAIL_ERROR);
- val |= REG_INMAIL_OP_REQUEST | cmd;
- iowrite32(val, nhi->iobase + REG_INMAIL_CMD);
-
- timeout = ktime_add_ms(ktime_get(), NHI_MAILBOX_TIMEOUT);
- do {
- val = ioread32(nhi->iobase + REG_INMAIL_CMD);
- if (!(val & REG_INMAIL_OP_REQUEST))
- break;
- usleep_range(10, 20);
- } while (ktime_before(ktime_get(), timeout));
-
- if (val & REG_INMAIL_OP_REQUEST)
- return -ETIMEDOUT;
- if (val & REG_INMAIL_ERROR)
- return -EIO;
-
- return 0;
-}
-
-/**
- * nhi_mailbox_mode() - Return current firmware operation mode
- * @nhi: Pointer to the NHI structure
- *
- * The function reads current firmware operation mode using NHI mailbox
- * registers and returns it to the caller.
- *
- * Return: &enum nhi_fw_mode.
- */
-enum nhi_fw_mode nhi_mailbox_mode(struct tb_nhi *nhi)
-{
- u32 val;
-
- val = ioread32(nhi->iobase + REG_OUTMAIL_CMD);
- val &= REG_OUTMAIL_CMD_OPMODE_MASK;
- val >>= REG_OUTMAIL_CMD_OPMODE_SHIFT;
-
- return (enum nhi_fw_mode)val;
-}
-
static void nhi_interrupt_work(struct work_struct *work)
{
struct tb_nhi *nhi = container_of(work, typeof(*nhi), interrupt_work);
diff --git a/drivers/thunderbolt/nhi.h b/drivers/thunderbolt/nhi.h
index 9aa167321cfe..e3f19e63f2ba 100644
--- a/drivers/thunderbolt/nhi.h
+++ b/drivers/thunderbolt/nhi.h
@@ -27,8 +27,6 @@ enum nhi_mailbox_cmd {
NHI_MAILBOX_ALLOW_ALL_DEVS = 0x23,
};

-int nhi_mailbox_cmd(struct tb_nhi *nhi, enum nhi_mailbox_cmd cmd, u32 data);
-enum nhi_fw_mode nhi_mailbox_mode(struct tb_nhi *nhi);
irqreturn_t tb_ring_msix(int irq, void *data);
int tb_nhi_probe(struct tb_nhi *nhi);
void tb_nhi_remove(struct tb_nhi *nhi, bool reset);
--
2.45.2