From nobody Fri Sep 25 14:31:19 2026 Received: from mx0a-0031df01.pphosted.com (mx0a-0031df01.pphosted.com [205.220.168.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1459F4334C9 for ; Fri, 11 Sep 2026 07:00:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=205.220.168.131 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789110033; cv=none; b=dgCxk7u+8pCUtrnkRcy4nJW1LaUFNY5D9YxZ0kbFI69ltUU/2QMa/GGCWtmqN1RQeMhj9sJs43HTvklbd2JgMyvocvxD5QA4pisruUxxTtc1N6uwVodyj+ycLhes5u/2q4fDtp+q3O9pEVe7jmdvrzK5Unc73GGm+9cq0sKMxL0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789110033; c=relaxed/simple; bh=CBt8MX5rIzNb3d5REngrwYN6aQHq1xRM1sRT2r61QFA=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=nZxqEXPnvuRa8fkvuVfPrj5XXbI6WyIMnbHGdKwrxV1AGJlW2SSjXWpYrI1YqMvnwTV0MX73S7mFaGHwNnRYBeKj+jMAFpJr0vcuyT1zTE3AQ7HLwRckbFWK8ZislRkVvYKKHzx6DUnjeGLxddgUKAoZdHjrftSM0hRei7EXXJg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=oss.qualcomm.com; spf=pass smtp.mailfrom=oss.qualcomm.com; dkim=pass (2048-bit key) header.d=qualcomm.com header.i=@qualcomm.com header.b=Spk4E/zm; dkim=pass (2048-bit key) header.d=oss.qualcomm.com header.i=@oss.qualcomm.com header.b=BXNOxARm; arc=none smtp.client-ip=205.220.168.131 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=oss.qualcomm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=oss.qualcomm.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=qualcomm.com header.i=@qualcomm.com header.b="Spk4E/zm"; dkim=pass (2048-bit key) header.d=oss.qualcomm.com header.i=@oss.qualcomm.com header.b="BXNOxARm" Received: from pps.filterd (m0279863.ppops.net [127.0.0.1]) by mx0a-0031df01.pphosted.com (8.18.1.11/8.18.1.11) with ESMTP id 68B6kQgD3376574 for ; Fri, 11 Sep 2026 07:00:12 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=qualcomm.com; h= cc:content-transfer-encoding:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to; s=qcppdkim1; bh= B0C1RJkdi9pngWirNo4qhGJuYnCZd20yeNRymvfFXgc=; b=Spk4E/zmrLSEfIug MlybMJpKcec+3UpZhNwZny4gTGAsnvXINVKGPQr13DDCLB4HQJXwGhzDDoqufsU+ Z3Vmflhdd5RLf3Crje0vw48syJHkcRssvBpAng1Npzy74RquGcXbIgYlp4/tDEym omsXYY4lebwtkbr9fwa0RYcjQXCTZZyR5ySBz5VSAzzug6oESNxysSUUhoevs4qi zYS1TSVsLkm8TYMV9P2ss5wiktB6TVgma8/oo0h8RHCZQE3vMcZqia9j2Lemhrmi LaAyP68/4RUMGlebTbKDDS0HNAk0E7ClXlWQNXs7awAcLStXQiU/Y83yZKny9W1e Gw7XHA== Received: from mail-pg1-f199.google.com (mail-pg1-f199.google.com [209.85.215.199]) by mx0a-0031df01.pphosted.com (PPS) with ESMTPS id 4gmbd50btw-1 (version=TLSv1.3 cipher=TLS_AES_128_GCM_SHA256 bits=128 verify=NOT) for ; Fri, 11 Sep 2026 07:00:12 +0000 (GMT) Received: by mail-pg1-f199.google.com with SMTP id 41be03b00d2f7-cc18ced1a5aso1141836a12.1 for ; Fri, 11 Sep 2026 00:00:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oss.qualcomm.com; s=google; t=1789110011; x=1789714811; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :content-type:mime-version:subject:date:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=B0C1RJkdi9pngWirNo4qhGJuYnCZd20yeNRymvfFXgc=; b=BXNOxARmDLjvD/joaDL1CkRjIRXh/Fbr55vta4d7qORzHDK0yhikeBNigrkBfnvB4b QnPippaziSzCldxolW/lv740MW/Hpb614gCFnXPgnBnWsDQg2DhlRAJNfIiLfySk9PA5 8f/B59B3wzwPLqKBfTRaDvkrTXdM4NoQ+4QiNTyI1TIE8nm44My6qpLEAs0cADQ/HM0J Qhv+bqlzkMSBjgLHjTvzC2laXNz3PkXCHoLHGoD148mFVJnVcAgsFC6jwy29/J9HJnBi 4s6Uce9e/PyVDa9ExGMK3G1zXOAMUGqLJz25/whSj8oQYDD37sdOXFZaqu7/r4IEeWgT rThA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1789110011; x=1789714811; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :content-type:mime-version:subject:date:from:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=B0C1RJkdi9pngWirNo4qhGJuYnCZd20yeNRymvfFXgc=; b=quRPg6YtmzwkV7i9xzILXtMlCnWqQLqTF+czJjMKh+orO7lec67j+0sdt245DazXYO uFAmsatQNoxJjIWJspHAM+Pz5R63BtqGnjielVFAIMVuGg58/TbLupS1SeaKSlFMvh7u 8myBYas9BkQset4usM0b3ivBzqiLIrExRy9AYFhU41w4mXqQDvPmg6E7QjWssK8vGmqr IIhSo/UfVJLdQJnOTotDzFP+nFijPkCl6vkAJ8Gu1zl8sUKZksmnyXUQXU5qlbtmzOpQ x4YE0C6g7oG17VgMHSuYlGhl5vbE6zi+3lnW5hysGAAbeoChAhxcluBBGMziX72Qg+Nr Elbg== X-Forwarded-Encrypted: i=1; AKwUvBwx1N+bGg7YPfDpajz1zjJxK4L2hu5ysIep4bjcu7b6dFt17XAtoIDjQn8D4YQLsBoINhkU5biyQdYwpDo=@vger.kernel.org X-Gm-Message-State: AFuF++lCYC0lfkxDzy4HkwIFtbGGfnvXILSvyKyezOYd3Vtt5YGkrhjo Iucc+pvyYot5nAexmitLr0Rg9npDnWXswWMCqZCY04AHyadl/pHerT48BpnM2M/DmXn/eYQilMn Acc8S2JkdDmfAHzxq+LkfYE2hqNpnEXryhVV1QpKPLFkuIBGcoM2pCUMDB1tsK/SzkAw= X-Gm-Gg: AYBFou1+6935CwWkxfW0SUA8EbyJwZzcrZfgIIaK7BBtra1TKAJhb7snMaMiQ6YSbA7 JgULLY0dF6+Hl57oGeoZxIAUdda+hKMYcXhl8UJvu8skhklGrCvYvduIH/RhOIWe7QCUcmT+X/a +e39m82Ivdxq14Gjd+lgYdlvg1xe278ntUPSJeFRR6pLST/KhBoO3cYRvq/HwzdJkJMs2hgk+Am CHn1kiyvjrKWSonQ9fsvA3REEdBSC4WQ4vqc0Pxpamt1bpqeyT93GeFJx4DNDwfEeDl7XaGSNwp BNk++/DdAsjY288ClaQpmzEs0bKoto9H3j8MNrR/u/dSpc1vMOHxDm5U4FUtk7v46P157OSdgH4 dmj4Ek9ZodmRBGv1R1pwjSwQLI2u9kqJlIbs= X-Received: by 2002:a05:6a21:a96:b0:3d3:afeb:880 with SMTP id adf61e73a8af0-3daed895710mr4985619637.28.1789110011555; Fri, 11 Sep 2026 00:00:11 -0700 (PDT) X-Received: by 2002:a05:6a21:a96:b0:3d3:afeb:880 with SMTP id adf61e73a8af0-3daed895710mr4985522637.28.1789110010978; Fri, 11 Sep 2026 00:00:10 -0700 (PDT) Received: from hu-arandive-hyd.qualcomm.com ([202.46.22.19]) by smtp.gmail.com with ESMTPSA id 5a478bee46e88-33ba4e9a8a9sm3799532eec.13.2026.09.11.00.00.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 11 Sep 2026 00:00:10 -0700 (PDT) From: Aniket Randive Date: Fri, 11 Sep 2026 12:29:51 +0530 Subject: [PATCH v9 1/2] i2c: core: Add i2c_update_timeout() helper for dynamic transfer timeouts Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260911-master-v9-1-77ac458344e2@oss.qualcomm.com> References: <20260911-master-v9-0-77ac458344e2@oss.qualcomm.com> In-Reply-To: <20260911-master-v9-0-77ac458344e2@oss.qualcomm.com> To: Andi Shyti , Viken Dadhaniya Cc: Dmitry Guzman , naresh.maramaina@oss.qualcomm.com, linux-i2c@vger.kernel.org, linux-kernel@vger.kernel.org, linux-arm-msm@vger.kernel.org, Mukesh Kumar Savaliya , Aniket Randive X-Mailer: b4 0.15.2 X-Developer-Signature: v=1; a=ed25519-sha256; t=1789110003; l=6911; i=aniket.randive@oss.qualcomm.com; s=20260325; h=from:subject:message-id; bh=CBt8MX5rIzNb3d5REngrwYN6aQHq1xRM1sRT2r61QFA=; b=2nsXlxL4z5MH7mBlCzFLtLc5y/2kYL7anQuSShQ7XF+7K/hg6R5jGr8VCpfxi7JEjEgusXeL9 I7Wq/yqa2IJAV7d3r8dCFx3N6wctk9L9BP4lTZzJdzoxCBaaGiMLOsI X-Developer-Key: i=aniket.randive@oss.qualcomm.com; a=ed25519; pk=4o37X1ZqGbCO/T2SR8kq/HnWvq0yb1RUlFuNukovbQk= X-Proofpoint-GUID: lAOMB44mp3HuXqBNCRs_c9vANHyP8bv2 X-Proofpoint-ORIG-GUID: lAOMB44mp3HuXqBNCRs_c9vANHyP8bv2 X-Proofpoint-Spam-Info: AW1haW4tMjYwOTExMDA5NSBTYWx0ZWRfX/wFbJIN+bsMw qk+3pzN4zYbF+JfQLOfGpnB2c4CxoH1riffOcQ89n7lzQGyjo1pF5Jgg1dUhaBgkjrq5lSLHrGH ZBEBp3J+kLqmL9CU0sqOV1OzXFxB2Uc= X-Proofpoint-Spam-Details-Enc: AW1haW4tMjYwOTExMDA5NSBTYWx0ZWRfX3AtUr+2qKWDi K3678yMpZJFcX9p4/sVdhop6chFhcDkmo2Fdbht765Z/Uvn0EVlD1A0UWcYwz9bAixZG5Ev5Yc7 plctYWe5gMF59Moz4sQJzfPy+3nWTeIk9fhITHpzEVD7gqBr2BiN7mBEqwDPTjMtuaZgp5g4s2/ SR/UKD3XhT8beCEWGKpXjSa9zGa/ecX6asJjJfttrgFH0eCkwzBpVtBRalKxYT4f70J78YHLqd/ ySlp32lDZH9SXp4522oePQZqEfn0k2EoXKJ+hYey7qHzieDb/9h3696lZwB5GPVuoRQ4Zeu7G2I zNiF4X/fyRNsnmF0YUqjES8kuiqDMdV3U2Gys+0i/cB6D4gyn/dqodB5k8wARhof3fSlDO1SX+N ouumbx2IifWwLImT3H2WM4Zcu8eGHS4SPvvzvqHVhvDP0CgP3iTmWgq78Q/E9S4pgAUiJHW1o7G PiMFhrMbDPDAOMBbe2Q== X-Authority-Analysis: v=2.4 cv=db0VTnXe c=1 sm=1 tr=0 ts=6aa3a6fc cx=c_pps a=Oh5Dbbf/trHjhBongsHeRQ==:117 a=fChuTYTh2wq5r3m49p7fHw==:17 a=IkcTkHD0fZMA:10 a=VdqzKS8jKosA:10 a=s4-Qcg_JpJYA:10 a=VkNPw1HP01LnGYTKEx00:22 a=u7WPNUs3qKkmUXheDGA7:22 a=yOCtJkima9RkubShWh1s:22 a=EUspDBNiAAAA:8 a=Xw8hV2vT4XMzfsrjPA4A:9 a=QEXdDO2ut3YA:10 a=_Vgx9l1VpLgwpw_dHYaR:22 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1176,Hydra:6.1.134,FMLib:17.12.100.49 definitions=2026-09-11_02,2026-09-09_02,2025-10-01_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 malwarescore=0 suspectscore=0 adultscore=0 clxscore=1015 lowpriorityscore=0 spamscore=0 impostorscore=0 priorityscore=1501 bulkscore=0 phishscore=0 classifier=typeunknown authscore=0 authtc= authcc= route=outbound adjust=0 reason=mlx scancount=1 engine=8.22.0-2609040000 definitions=main-2609110095 The transfer timeout for an I2C controller should reflect the actual message length and bus frequency rather than a static 1-second value. A static timeout causes unnecessary delays on error paths for short messages, and may be insufficient for very long transfers. Add i2c_update_timeout() to i2c-core which computes a transfer-specific timeout and stores it directly in the standard adap->timeout field. The formula accounts for 9 bits per byte (8 data + 1 ACK) at the configured bus frequency. The caller supplies a safety multiplier and a minimum floor so that each driver retains full control over its timing policy without those values becoming public API. Storing the result in adap->timeout makes it visible to all consumers of that field, including the arbitration-loss retry loop in __i2c_transfer(). The function is gated by CONFIG_I2C_DYNAMIC_TIMEOUT. When the config is disabled, i2c_update_timeout() compiles to a no-op inline stub so drivers that call it build cleanly and the existing static 1-second default is preserved unchanged. A timeout explicitly configured by userspace via the I2C_TIMEOUT ioctl is stored in a new adap->user_timeout field and always takes precedence over the kernel-computed value. When userspace has not configured a timeout, the computed value is used. The ioctl keeps writing adap->timeout as well, so adapters that never call i2c_update_timeout() continue to honour it exactly as before. As i2c_update_timeout() is an exported helper, guard against a zero bus frequency from a misbehaving caller with WARN_ON_ONCE() and return early, leaving the existing timeout untouched as a safe fallback rather than dividing by zero. Reviewed-by: Mukesh Kumar Savaliya Signed-off-by: Aniket Randive Suggested-by: Dmitry Guzman --- drivers/i2c/Kconfig | 13 +++++++++++++ drivers/i2c/i2c-core-base.c | 43 +++++++++++++++++++++++++++++++++++++++++= ++ drivers/i2c/i2c-dev.c | 3 +++ include/linux/i2c.h | 13 +++++++++++++ 4 files changed, 72 insertions(+) diff --git a/drivers/i2c/Kconfig b/drivers/i2c/Kconfig index c232054fddd6..1d9691ec0e7e 100644 --- a/drivers/i2c/Kconfig +++ b/drivers/i2c/Kconfig @@ -136,6 +136,19 @@ config I2C_SLAVE_TESTUNIT =20 endif =20 +config I2C_DYNAMIC_TIMEOUT + bool "Dynamic per-transfer timeout based on message length" + depends on I2C + help + When enabled, the I2C core computes a per-transfer timeout from the + message length and bus frequency instead of using a static 1-second + default. A timeout explicitly configured via the I2C_TIMEOUT userspace + interface always takes precedence over the computed value. + + When disabled, the existing static 1-second timeout is preserved. + + If unsure, say N. + config I2C_DEBUG_CORE bool "I2C Core debugging messages" help diff --git a/drivers/i2c/i2c-core-base.c b/drivers/i2c/i2c-core-base.c index fb25704219c7..5f6a7370dc89 100644 --- a/drivers/i2c/i2c-core-base.c +++ b/drivers/i2c/i2c-core-base.c @@ -29,6 +29,7 @@ #include #include #include +#include #include #include #include @@ -2001,6 +2002,48 @@ void i2c_parse_fw_timings(struct device *dev, struct= i2c_timings *t, bool use_de } EXPORT_SYMBOL_GPL(i2c_parse_fw_timings); =20 +#ifdef CONFIG_I2C_DYNAMIC_TIMEOUT +/** + * i2c_update_timeout - compute and set a dynamic transfer timeout on an a= dapter + * @adap: the i2c_adapter whose timeout field will be updated + * @bus_freq_hz: I2C bus clock frequency in Hz + * @len: transfer length in bytes + * @safety_coeff: multiplier applied over the theoretical wire time + * @min_usec: minimum timeout floor in microseconds + * + * Computes a transfer-specific timeout from the message length and bus + * frequency, applies a safety multiplier and a minimum floor, then stores + * the result in adap->timeout (in jiffies). The caller supplies the poli= cy + * constants so they remain internal to the driver. + * + * A timeout explicitly configured by userspace via the I2C_TIMEOUT ioctl + * always takes precedence; the computed value is used only when userspace + * has not configured one. + */ +void i2c_update_timeout(struct i2c_adapter *adap, u32 bus_freq_hz, + size_t len, unsigned int safety_coeff, + unsigned int min_usec) +{ + u64 bit_usec, total_usec; + unsigned long jiffies_val; + + if (adap->user_timeout > 0) { + adap->timeout =3D adap->user_timeout; + return; + } + + if (WARN_ON_ONCE(!bus_freq_hz)) + return; + + bit_usec =3D mul_u64_u32_div(len * 9, USEC_PER_SEC, bus_freq_hz); + total_usec =3D bit_usec * safety_coeff + min_usec; + + jiffies_val =3D usecs_to_jiffies((unsigned int)min_t(u64, total_usec, UIN= T_MAX)); + adap->timeout =3D (int)min_t(unsigned long, jiffies_val, INT_MAX); +} +EXPORT_SYMBOL_GPL(i2c_update_timeout); +#endif /* CONFIG_I2C_DYNAMIC_TIMEOUT */ + /* -----------------------------------------------------------------------= -- */ =20 int i2c_for_each_dev(void *data, int (*fn)(struct device *dev, void *data)) diff --git a/drivers/i2c/i2c-dev.c b/drivers/i2c/i2c-dev.c index ccaac5e29f90..d219eb89f978 100644 --- a/drivers/i2c/i2c-dev.c +++ b/drivers/i2c/i2c-dev.c @@ -495,6 +495,9 @@ static long i2cdev_ioctl(struct file *file, unsigned in= t cmd, unsigned long arg) return -EINVAL; =20 client->adapter->timeout =3D msecs_to_jiffies(arg * 10); +#ifdef CONFIG_I2C_DYNAMIC_TIMEOUT + client->adapter->user_timeout =3D client->adapter->timeout; +#endif break; default: /* NOTE: returning a fault code here could cause trouble diff --git a/include/linux/i2c.h b/include/linux/i2c.h index 14ab4d3055af..e4302a1bd8ea 100644 --- a/include/linux/i2c.h +++ b/include/linux/i2c.h @@ -742,6 +742,9 @@ struct i2c_adapter { struct rt_mutex mux_lock; =20 int timeout; /* in jiffies */ +#ifdef CONFIG_I2C_DYNAMIC_TIMEOUT + int user_timeout; /* I2C_TIMEOUT ioctl value in jiffies */ +#endif int retries; struct device dev; /* the adapter device */ unsigned long locked_flags; /* owned by the I2C core */ @@ -913,6 +916,16 @@ unsigned int i2c_adapter_depth(struct i2c_adapter *ada= pter); =20 void i2c_parse_fw_timings(struct device *dev, struct i2c_timings *t, bool = use_defaults); =20 +#ifdef CONFIG_I2C_DYNAMIC_TIMEOUT +void i2c_update_timeout(struct i2c_adapter *adap, u32 bus_freq_hz, + size_t len, unsigned int safety_coeff, + unsigned int min_usec); +#else +static inline void i2c_update_timeout(struct i2c_adapter *adap, u32 bus_fr= eq_hz, + size_t len, unsigned int safety_coeff, + unsigned int min_usec) {} +#endif + /* Return the functionality mask */ static inline u32 i2c_get_functionality(struct i2c_adapter *adap) { --=20 2.34.1 From nobody Fri Sep 25 14:31:20 2026 Received: from mx0b-0031df01.pphosted.com (mx0b-0031df01.pphosted.com [205.220.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 620F83DA5BA for ; Fri, 11 Sep 2026 07:00:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=205.220.180.131 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789110038; cv=none; b=aINxCTVROk6oiqfPndX9sQ0Astk7h20TibMCCzdJgVGyaNjPn6i0g63l3zj8xcVvfyjLt9ZwsNR3Jsow/M2Vlz9n2C8Ck4yQxXwAzqvieKUCGxaMzqiXr1B9lBsiuN1GG0zU3XhoBgga+T+ozqN5kVU+9zYRuLEnPIeiXbImkBU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789110038; c=relaxed/simple; bh=WK14kF4EOiz8WnbFV9tZuZCiFiCbHnSjpwVMdu0yWYg=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=inz7+Oc+qdpqanfdz8OqivY4MKCbVMZOLCbgB0Y/xbg2YR1VaURGfgRJ3zPD/bMhH/gYT2ZSZGXPkIFkIpc4dAl1WOzfHYwKrz/qOPSSDMbMqCnSgtcNlkMGN8T68EQl9uIPej4PVocRscPv3TZzq72KOIgLDY5+CCxRblV17aw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=oss.qualcomm.com; spf=pass smtp.mailfrom=oss.qualcomm.com; dkim=pass (2048-bit key) header.d=qualcomm.com header.i=@qualcomm.com header.b=SntMglK8; dkim=pass (2048-bit key) header.d=oss.qualcomm.com header.i=@oss.qualcomm.com header.b=H6kMNA2K; arc=none smtp.client-ip=205.220.180.131 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=oss.qualcomm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=oss.qualcomm.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=qualcomm.com header.i=@qualcomm.com header.b="SntMglK8"; dkim=pass (2048-bit key) header.d=oss.qualcomm.com header.i=@oss.qualcomm.com header.b="H6kMNA2K" Received: from pps.filterd (m0279869.ppops.net [127.0.0.1]) by mx0a-0031df01.pphosted.com (8.18.1.11/8.18.1.11) with ESMTP id 68B6kYAx3709595 for ; Fri, 11 Sep 2026 07:00:16 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=qualcomm.com; h= cc:content-transfer-encoding:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to; s=qcppdkim1; bh= KeXCEwNC4fRFjlbOCrwn9li1Urokjxh0CwWMHmoG4No=; b=SntMglK8u85aI4Ww NS15XRYuNs9vhkivmKz1UiaRVxbYiVuG5KsQSEBKWJND3nZu1pEna1+8CbVOBdM3 OEWNFyjCdxbYWxTLXSDhoEAX9+QVZXIxSHF2JHr9dAYRrw0g2vLDjDDXufioQJgT iXaYRAn2ZCftr7FZtIYmEPK7jW3tBEFzzbwXjuY/lynVYQpvCrL86xkrnrwwWmes VUoyWlkhTlBxCEm4kUOXz1Zgn2WVMMobFgWNrTpq9y3hkFujr7s4lDO1b5CuRIqE wzznIHT6t8oypCPiKUvhOXq3IWrVa0oA07yZCiBWNk7+fIkGCBnIMKS+f72+KXfI X3zrDA== Received: from mail-pj1-f71.google.com (mail-pj1-f71.google.com [209.85.216.71]) by mx0a-0031df01.pphosted.com (PPS) with ESMTPS id 4gm5q41jsr-1 (version=TLSv1.3 cipher=TLS_AES_128_GCM_SHA256 bits=128 verify=NOT) for ; Fri, 11 Sep 2026 07:00:16 +0000 (GMT) Received: by mail-pj1-f71.google.com with SMTP id 98e67ed59e1d1-398e1f7d1a5so828294a91.0 for ; Fri, 11 Sep 2026 00:00:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oss.qualcomm.com; s=google; t=1789110015; x=1789714815; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :content-type:mime-version:subject:date:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=KeXCEwNC4fRFjlbOCrwn9li1Urokjxh0CwWMHmoG4No=; b=H6kMNA2K5OV9fzPNGQWAz8k6h49iQgFbLgODsOU2MUS7qs5JNS9M8eCJnKWPKwmpaP EyZcZ7B2AWsyIkfDq5AXs2XiOo/a6erA3zrmqPTXommG2fUMrbeamP3RZBlcVjzBjZrc /7o/XZcJtLDx6ls8sbHRz24DE5ABjRUSoziHy3T5F/b+AGz1bz6l85G+9ubWrDb1nryg 0WraGb6k8x+Mqt6b0P6lTF0VL/PejmPeJtJPSV8s+sAlC0KzOeQF4C4INwO23cqgTRpd vAf7REWbb97dmVnJmO4WjvTitNckC/L4xiI1jqd19qNYqB/VevwZdC1Q6RyUMt9oN4GV +H8A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1789110015; x=1789714815; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :content-type:mime-version:subject:date:from:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=KeXCEwNC4fRFjlbOCrwn9li1Urokjxh0CwWMHmoG4No=; b=ZPE1lunZrf3avZbzU2EwXxwn7XqEuEGWAdvcTy9GQp60hwsN/TxC90pvfJ9II0F9m9 /0UbfuksRWLTcGnagZ0LB/8cbhiywz+7/ae7lOTodt8hdxwN5Ylcp61CfKgXVqCSXvq7 XwrhwuAvpxkl75bUpKqck+lchnJ5zJEq+EYWcy9H4gSFrr4qZpm62yWE/3PqjQptXO3K sH8IDB20th5Mz8p1Y6avr5qjz1TTxP/h4ZV1UpfGZ9TRTZKh7V6zYcZTbcbmd9I5F3un a78fLoCfh7+yq1lr+RIcu2fERGk/VtdF+Cppm5fVncFpHmLytsRtX5ohoshcYliVmYkI 7Czw== X-Forwarded-Encrypted: i=1; AKwUvBxgjDs1AjXBF0x5kR+PAbnh75cB6nMQKmOSbJHS60TtuO7nejkfNPOiAwN+Sotda5G+lzXVz39N/GWKKJk=@vger.kernel.org X-Gm-Message-State: AFuF++nUSndgskCp/u//6ur9p5ye/vhmRxRa39JPitdJfV6c9tLchh+n SWSKYzSN0XW6IIjwkMJGD4DT4II453te8AfehuZ/IT89pJ/XTMo7RhyPc/C/XYlAgSzmAxlHXse wfJbtOcVPSpyaGAhWvH2EAGXIVH9NdJmrVgMxgMu8skbNMycrHtup3sDzXkAyDF3yNsU= X-Gm-Gg: AYBFou0hXr9FsNkyEXowGdb5/H5ge11+o3LicIc3ifq7gFRDOh0p6ejuDBCviXwT0ar /WsxF98h8osBmVovXa+bAjOKM3UY6AB6xlpxHv//luujU/ecrDuMj5wxIOQsZBhz+x44zzmUwAQ uzSy7GO/7xVEo2At99oBrikD8iE+/VRR1gABQF4RT3kpt+u+v6nP991M5hRXERwWpWNorUSnX1q 2V4GMnO5xCP24X7M+1ffrL0mdwY6/wFVB8nCaVW/+YkPTjAs0mOLZr6/NQnWpjpg7zEVvqRMtK9 m2JeULp3e4cPUcggi8ne1RhIRCZu50HCbDj+WBObaa0tqrHXnRtzhadFRFq3On1+emJplRJn8Z3 qI2gADpIxEUQGSYO6+HvJe0WdmDmweyDIyaI= X-Received: by 2002:a17:90b:528b:b0:398:9c0c:7c71 with SMTP id 98e67ed59e1d1-39d9c35a3ccmr4600098a91.24.1789110015221; Fri, 11 Sep 2026 00:00:15 -0700 (PDT) X-Received: by 2002:a17:90b:528b:b0:398:9c0c:7c71 with SMTP id 98e67ed59e1d1-39d9c35a3ccmr4600015a91.24.1789110014699; Fri, 11 Sep 2026 00:00:14 -0700 (PDT) Received: from hu-arandive-hyd.qualcomm.com ([202.46.22.19]) by smtp.gmail.com with ESMTPSA id 5a478bee46e88-33ba4e9a8a9sm3799532eec.13.2026.09.11.00.00.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 11 Sep 2026 00:00:14 -0700 (PDT) From: Aniket Randive Date: Fri, 11 Sep 2026 12:29:52 +0530 Subject: [PATCH v9 2/2] i2c: qcom-geni: Add dynamic transfer timeout based on transfer length and frequency Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260911-master-v9-2-77ac458344e2@oss.qualcomm.com> References: <20260911-master-v9-0-77ac458344e2@oss.qualcomm.com> In-Reply-To: <20260911-master-v9-0-77ac458344e2@oss.qualcomm.com> To: Andi Shyti , Viken Dadhaniya Cc: Dmitry Guzman , naresh.maramaina@oss.qualcomm.com, linux-i2c@vger.kernel.org, linux-kernel@vger.kernel.org, linux-arm-msm@vger.kernel.org, Mukesh Kumar Savaliya , Aniket Randive X-Mailer: b4 0.15.2 X-Developer-Signature: v=1; a=ed25519-sha256; t=1789110003; l=5876; i=aniket.randive@oss.qualcomm.com; s=20260325; h=from:subject:message-id; bh=WK14kF4EOiz8WnbFV9tZuZCiFiCbHnSjpwVMdu0yWYg=; b=cfr3RKHUK1mVL30/osItLwyNJY/nue0fQhMRoECppqbxf1VUEoMs6jRVDxjStxHYx7ULUn9GX CCO08czr8yMAUoIiJ6YPRTJt4ZFKsQE1uynsG9xbOLlN7Vmlwlcuey5 X-Developer-Key: i=aniket.randive@oss.qualcomm.com; a=ed25519; pk=4o37X1ZqGbCO/T2SR8kq/HnWvq0yb1RUlFuNukovbQk= X-Proofpoint-GUID: T-mnfefVzomeOjjJGEdBDq4r5dnE3xzM X-Proofpoint-Spam-Info: AW1haW4tMjYwOTExMDA5NSBTYWx0ZWRfXwE8fWvTz2LvE 8m2kaJUheW1mVfJ1Ac/EmRxI+OmMczn8pqHGXMbAB976DAgoIF71tny+l2sFYEai7ViShmtFft0 9HobaWYlwgP4X4Lj5Lq/0Evsh5AUtHU= X-Proofpoint-Spam-Details-Enc: AW1haW4tMjYwOTExMDA5NSBTYWx0ZWRfX/SwRfYa5qoD3 YNVYHvHV4egB6vlc39StnVuzj38NozBGJj7N3Bk5Y87C0VWc0ObSNqS4kP9U9QGU+nQVymvR5V1 3PZUL3wBxNeXVGgAU4wY5vczgm8iKY3oiBKZSKAtMkqGOF0LxFQTOy9i3NHF4fp8tkwGTQpx+2u PNoAUnhCJjuVgnCvbGNV1arFs1TGFSstHX8UTvCddcUaiqSfXT72WUJFDrttEEkqyPv6m0IdHDg Tb4JzfjOVORIjFel2ZUPGpXlPR00QUcCVsYE3EY2zBTMpW6ndnuX60dpzvQ64/4aMEQw3iC1o6A /3ARGMACaS91z/HW/6TkdHnNYS/ez0xz7EO3VabflrOfdy6SJa+u/hXHeki0bKv30RDm7uFCOqk l8ZJUzP+YOLBfrdlS2bimjlLWXG779xXM3DNX/kMTOqmIVe1ERuVqeqR/DcEQpM6T41HI2pWmmM he3469wgaiw08TeTWyg== X-Proofpoint-ORIG-GUID: T-mnfefVzomeOjjJGEdBDq4r5dnE3xzM X-Authority-Analysis: v=2.4 cv=Wsa+otfv c=1 sm=1 tr=0 ts=6aa3a700 cx=c_pps a=UNFcQwm+pnOIJct1K4W+Mw==:117 a=fChuTYTh2wq5r3m49p7fHw==:17 a=IkcTkHD0fZMA:10 a=VdqzKS8jKosA:10 a=s4-Qcg_JpJYA:10 a=VkNPw1HP01LnGYTKEx00:22 a=u7WPNUs3qKkmUXheDGA7:22 a=_glEPmIy2e8OvE2BGh3C:22 a=EUspDBNiAAAA:8 a=ZP9fceawhsPimmF_JtQA:9 a=QEXdDO2ut3YA:10 a=uKXjsCUrEbL0IQVhDsJ9:22 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1176,Hydra:6.1.134,FMLib:17.12.100.49 definitions=2026-09-11_02,2026-09-09_02,2025-10-01_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 malwarescore=0 lowpriorityscore=0 priorityscore=1501 suspectscore=0 spamscore=0 bulkscore=0 impostorscore=0 clxscore=1015 adultscore=0 phishscore=0 classifier=typeunknown authscore=0 authtc= authcc= route=outbound adjust=0 reason=mlx scancount=1 engine=8.22.0-2609040000 definitions=main-2609110095 The driver uses a static XFER_TIMEOUT of HZ (1 second) for all transfers regardless of message length or bus frequency, causing unnecessary delays on error paths. Use i2c_update_timeout() API to calculate transfer timeouts dynamically based on message length and bus frequency. update the timeout per message for FIFO, SE-DMA and GPI single descriptor transfers. For GPI multi descriptor transfers, calculate the timeout from the combined length of all messages, as completion is reported only after the entire batch has finished. A 10x safety margin over the theoretical wire time is applied, with a 300ms floor to account for I2C clock stretching and other situations where a slave may keep SCL asserted for an extended period, including faulty devices holding the bus. Reviewed-by: Mukesh Kumar Savaliya Signed-off-by: Aniket Randive Suggested-by: Dmitry Guzman --- drivers/i2c/busses/i2c-qcom-geni.c | 48 +++++++++++++++++++++++++++++++---= ---- 1 file changed, 39 insertions(+), 9 deletions(-) diff --git a/drivers/i2c/busses/i2c-qcom-geni.c b/drivers/i2c/busses/i2c-qc= om-geni.c index 658636c1ee0e..203f4d627ca1 100644 --- a/drivers/i2c/busses/i2c-qcom-geni.c +++ b/drivers/i2c/busses/i2c-qcom-geni.c @@ -79,9 +79,14 @@ enum geni_i2c_err_code { =20 #define ABORT_TIMEOUT HZ #define CANCEL_TIMEOUT HZ -#define XFER_TIMEOUT HZ #define RST_TIMEOUT HZ =20 +/* 9 bits per byte (8 data + 1 ACK), 10x safety margin */ +#define I2C_TIMEOUT_SAFETY_COEFFICIENT 10 + +/* 300ms floor: budget for clock stretching; slave may hold SCL low indefi= nitely */ +#define I2C_TIMEOUT_MIN_USEC 300000 + struct geni_i2c_desc { bool no_dma_support; unsigned int tx_fifo_depth; @@ -513,7 +518,10 @@ static int geni_i2c_rx_one_msg(struct geni_i2c_dev *gi= 2c, struct i2c_msg *msg, } =20 cur =3D gi2c->cur; - time_left =3D wait_for_completion_timeout(&gi2c->done, XFER_TIMEOUT); + i2c_update_timeout(&gi2c->adap, gi2c->clk_freq_out, len, + I2C_TIMEOUT_SAFETY_COEFFICIENT, + I2C_TIMEOUT_MIN_USEC); + time_left =3D wait_for_completion_timeout(&gi2c->done, gi2c->adap.timeout= ); if (!time_left || (gi2c->err && gi2c->err !=3D gi2c_log[ADDR_NACK].err)) geni_i2c_cancel_xfer(gi2c); =20 @@ -555,7 +563,10 @@ static int geni_i2c_tx_one_msg(struct geni_i2c_dev *gi= 2c, struct i2c_msg *msg, writel_relaxed(1, se->base + SE_GENI_TX_WATERMARK_REG); =20 cur =3D gi2c->cur; - time_left =3D wait_for_completion_timeout(&gi2c->done, XFER_TIMEOUT); + i2c_update_timeout(&gi2c->adap, gi2c->clk_freq_out, len, + I2C_TIMEOUT_SAFETY_COEFFICIENT, + I2C_TIMEOUT_MIN_USEC); + time_left =3D wait_for_completion_timeout(&gi2c->done, gi2c->adap.timeout= ); if (!time_left || (gi2c->err && gi2c->err !=3D gi2c_log[ADDR_NACK].err)) geni_i2c_cancel_xfer(gi2c); =20 @@ -633,7 +644,7 @@ static void geni_i2c_gpi_multi_desc_unmap(struct geni_i= 2c_dev *gi2c, struct i2c_ * geni_i2c_gpi_multi_xfer_timeout_handler() - Handles multi message trans= fer timeout * @dev: Pointer to the corresponding dev node * @multi_xfer: Pointer to the geni_i2c_gpi_multi_desc_xfer - * @transfer_timeout_msecs: Timeout value in milliseconds + * @timeout_jiffies: Per-message completion timeout in jiffies * @transfer_comp: Completion object of the transfer * * This function waits for the completion of each processed transfer messa= ges @@ -643,18 +654,18 @@ static void geni_i2c_gpi_multi_desc_unmap(struct geni= _i2c_dev *gi2c, struct i2c_ */ static int geni_i2c_gpi_multi_xfer_timeout_handler(struct device *dev, struct geni_i2c_gpi_multi_desc_xfer *multi_xfer, - u32 transfer_timeout_msecs, + unsigned long timeout_jiffies, struct completion *transfer_comp) { int i; - u32 time_left; + unsigned long time_left; =20 for (i =3D 0; i < multi_xfer->msg_idx_cnt - 1; i++) { reinit_completion(transfer_comp); =20 if (multi_xfer->msg_idx_cnt !=3D multi_xfer->irq_cnt) { time_left =3D wait_for_completion_timeout(transfer_comp, - transfer_timeout_msecs); + timeout_jiffies); if (!time_left) { dev_err(dev, "%s: Transfer timeout\n", __func__); return -ETIMEDOUT; @@ -778,8 +789,24 @@ static int geni_i2c_gpi(struct geni_i2c_dev *gi2c, str= uct i2c_msg msgs[], dma_async_issue_pending(gi2c->tx_c); =20 if ((msg_idx =3D=3D (gi2c->num_msgs - 1)) || flags & DMA_PREP_INTERRUPT)= { + size_t total_len =3D 0; + int j; + + /* + * All TREs except the last carry the BEI bit, so a single + * completion interrupt fires only after the entire batch has + * drained on the wire. The timeout budget must therefore cover + * the combined wire time of every message in the batch. + */ + for (j =3D 0; j < gi2c->num_msgs; j++) + total_len +=3D msgs[j].len; + + i2c_update_timeout(&gi2c->adap, gi2c->clk_freq_out, total_len, + I2C_TIMEOUT_SAFETY_COEFFICIENT, + I2C_TIMEOUT_MIN_USEC); ret =3D geni_i2c_gpi_multi_xfer_timeout_handler(gi2c->se.dev, gi2c_gpi_= xfer, - XFER_TIMEOUT, &gi2c->done); + gi2c->adap.timeout, + &gi2c->done); if (ret) { dev_err(gi2c->se.dev, "I2C multi write msg transfer timeout: %d\n", @@ -899,7 +926,10 @@ static int geni_i2c_gpi_xfer(struct geni_i2c_dev *gi2c= , struct i2c_msg msgs[], i =20 if (!gi2c->is_tx_multi_desc_xfer) { dma_async_issue_pending(gi2c->tx_c); - time_left =3D wait_for_completion_timeout(&gi2c->done, XFER_TIMEOUT); + i2c_update_timeout(&gi2c->adap, gi2c->clk_freq_out, msgs[i].len, + I2C_TIMEOUT_SAFETY_COEFFICIENT, + I2C_TIMEOUT_MIN_USEC); + time_left =3D wait_for_completion_timeout(&gi2c->done, gi2c->adap.timeo= ut); if (!time_left) { dev_err(gi2c->se.dev, "%s:I2C timeout\n", __func__); gi2c->err =3D -ETIMEDOUT; --=20 2.34.1