Received: by 2002:ab2:6c55:0:b0:1fd:c486:4f03 with SMTP id v21csp415953lqp; Wed, 12 Jun 2024 05:49:44 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCWQ/yzgFh1+lmBDAWDVfbzYKo5xUB9zYLWrCeUKALJ+SiSp0wpkgsfTxYgP+0OZqBXo1hpV5vemd2MdMmqid7Qs3a0ys/udPOMkS6lgBA== X-Google-Smtp-Source: AGHT+IF0vFSQmKQvgjjJTOg+rL4jjLMPzavw7EK5FF8mA3HHEcxvRBf2jg21/bzm7lCRt/z+ENPZ X-Received: by 2002:a50:c349:0:b0:57c:6248:9e26 with SMTP id 4fb4d7f45d1cf-57caab2793fmr1697851a12.39.1718196584390; Wed, 12 Jun 2024 05:49:44 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1718196584; cv=pass; d=google.com; s=arc-20160816; b=UH3vEVGOK/Qt9LQrC0gEtJHTSJx+yD6TnZBQrq4mdtcLDAw4ghld55NXlZkCc+H/LS WidyufNcMEsr4KQ7p5CaL2buMEj8ChqpWQw+/MRYsWF2l0LM7vPM5xNWkkVuqG9MaXbc XDacY4orAPDFkYrKCmrjBCG1IsnEOGVP8uTgQ5GRUC0Jgf7rJc+W0qjEOeikHCq4xIG+ muw2/+PAfG+GbC4g689KrO5vaOibdSfKaD3aKuZm+YlXuxrfsn7z9v4ywcVz0jsZdjPB 4Xky0PFlIo6mY+H8fefi2FHIyCWnHf40S3JP4xw/NGi3bTbacemAVFM+4j/PexbHnkxo jFfA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:message-id:date:subject:cc:to :from:dkim-signature; bh=+7VI0+0yWgu+99TXdk1RwSMz8s0/dUbNSHBFUKF/9IQ=; fh=UEabLSx35NOoYTydv8dGl0TnvGYaytmOgQBqX45UYAE=; b=HmrfotS3OQx4xqcASldIC6aZx6KMZbyVCnipgc8RKN81XkUOVRKaNsLYAlEdQkhT6S Mf1E0QtOuJ2xvdBpi0YRABEpWweWiAdiLfqN612V0iPFzm5WdT2rx9VNMF6UJjikQkB3 aWdOCU6mo+n66hTNcsL0ukl4Dpi6wDl845BrzJm/zEMl9NrTOIJyADAVvGC2rGEH70hW IMK6JHmQ9eX2GfsI2cy/P+w7cUR2P1skojqUS2MVmFizg69+q3jsDdsc+1sB7zkePJ45 mYhL57Zuvhlk4/fzaxcb4IPivgsfEipI9OsLc05jUCI26CBqQJmmvRzZNiCvrS7TYeta uyqA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@foss.st.com header.s=selector1 header.b="eqe/9oJg"; arc=pass (i=1 spf=pass spfdomain=foss.st.com dkim=pass dkdomain=foss.st.com dmarc=pass fromdomain=foss.st.com); spf=pass (google.com: domain of linux-kernel+bounces-211513-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-211513-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=foss.st.com Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id 4fb4d7f45d1cf-57c9f0e6918si1279866a12.95.2024.06.12.05.49.44 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 12 Jun 2024 05:49:44 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-211513-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@foss.st.com header.s=selector1 header.b="eqe/9oJg"; arc=pass (i=1 spf=pass spfdomain=foss.st.com dkim=pass dkdomain=foss.st.com dmarc=pass fromdomain=foss.st.com); spf=pass (google.com: domain of linux-kernel+bounces-211513-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-211513-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=foss.st.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id DEBB71F21AA7 for ; Wed, 12 Jun 2024 12:49:43 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 68ADD176FB7; Wed, 12 Jun 2024 12:49:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=foss.st.com header.i=@foss.st.com header.b="eqe/9oJg" Received: from mx07-00178001.pphosted.com (mx07-00178001.pphosted.com [185.132.182.106]) (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 A88AD153509; Wed, 12 Jun 2024 12:49:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.132.182.106 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718196567; cv=none; b=r47dWW4ISSi6VBPvG58HbxQeAwD1Jy3PTiohin/JdZgGgnzgwiwiHiPdHx3BnI2ugHlVm0pebLIp+fOpFMuSqJYDmoHEtvrwJ8XNPALLIsSAAgYYFoUxOnR5zILHkv/js32WB95gpvSyDEKa12nGfb8EarL2fy4Qvm5lApoc0So= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718196567; c=relaxed/simple; bh=jml4Li8jRIuKDzQQJHvxjVmuIVdQD/S+n68zDOjWbwc=; h=From:To:CC:Subject:Date:Message-ID:MIME-Version:Content-Type; b=KZcwp1xPunRevyi3aRiIgUNkNyRwEaItNvIN7KOfgixSQaeucBr1z12hYeuC1bGCkGxmTxGIrWXdrh29Eib4LCz3rHBRd57AfTTjr3RLPgt0hT4a4nBuJQ9q+ES2QnFJt2ThFTXkfJqLxFC/x72gt2HhWyJX8sJfvofwllXiFr0= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=foss.st.com; spf=pass smtp.mailfrom=foss.st.com; dkim=pass (2048-bit key) header.d=foss.st.com header.i=@foss.st.com header.b=eqe/9oJg; arc=none smtp.client-ip=185.132.182.106 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=foss.st.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=foss.st.com Received: from pps.filterd (m0241204.ppops.net [127.0.0.1]) by mx07-00178001.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 45CBgWMZ016579; Wed, 12 Jun 2024 14:48:58 +0200 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=foss.st.com; h= cc:content-transfer-encoding:content-type:date:from:message-id :mime-version:subject:to; s=selector1; bh=+7VI0+0yWgu+99TXdk1RwS Mz8s0/dUbNSHBFUKF/9IQ=; b=eqe/9oJgCQbH8XTb1sRoXJPTLEmFq1Cdx9qS0r 17cNTGfJ8lQPrJdDqhqyRygeS/rwKu7wVsQu/4gR1enXPHSPrultScJQqY7C5ey2 sXMReUiV2OLWRT/N94aXRuTqfkygzn6fCsldRiwP7aMsN2W7lCUutBDRr45TNDrT PJuIvIt5ERp9wPGiHMbItVk0IlMaLF3oww0rqjOdw+ID43XE+M7Ozjc5yQhA6IT9 w1ipyp4b98oRzeRunV/WCbLay58Sv+B+db13K/sUT0HguiOrN/p+pJPqsKYeNPzq zjkha5vpisayMQE4LrCQoC5Vir75WNtdHiZiTp8JlQcHfNtg== Received: from beta.dmz-ap.st.com (beta.dmz-ap.st.com [138.198.100.35]) by mx07-00178001.pphosted.com (PPS) with ESMTPS id 3ypbp4qrn5-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Wed, 12 Jun 2024 14:48:58 +0200 (MEST) Received: from euls16034.sgp.st.com (euls16034.sgp.st.com [10.75.44.20]) by beta.dmz-ap.st.com (STMicroelectronics) with ESMTP id 5DD0C40047; Wed, 12 Jun 2024 14:47:50 +0200 (CEST) Received: from Webmail-eu.st.com (eqndag1node5.st.com [10.75.129.134]) by euls16034.sgp.st.com (STMicroelectronics) with ESMTP id 73DB7215BF7; Wed, 12 Jun 2024 14:47:16 +0200 (CEST) Received: from SAFDAG1NODE1.st.com (10.75.90.17) by EQNDAG1NODE5.st.com (10.75.129.134) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.35; Wed, 12 Jun 2024 14:47:16 +0200 Received: from localhost (10.252.17.208) by SAFDAG1NODE1.st.com (10.75.90.17) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.35; Wed, 12 Jun 2024 14:47:15 +0200 From: Fabrice Gasnier To: , CC: , , , , , , Subject: [PATCH v2] usb: ucsi: stm32: fix command completion handling Date: Wed, 12 Jun 2024 14:46:56 +0200 Message-ID: <20240612124656.2305603-1-fabrice.gasnier@foss.st.com> X-Mailer: git-send-email 2.25.1 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain X-ClientProxiedBy: SAFCAS1NODE2.st.com (10.75.90.13) To SAFDAG1NODE1.st.com (10.75.90.17) X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1039,Hydra:6.0.680,FMLib:17.12.28.16 definitions=2024-06-12_06,2024-06-12_02,2024-05-17_01 Sometimes errors are seen, when doing DR swap, like: [ 24.672481] ucsi-stm32g0-i2c 0-0035: UCSI_GET_PDOS failed (-5) [ 24.720188] ucsi-stm32g0-i2c 0-0035: ucsi_handle_connector_change: GET_CONNECTOR_STATUS failed (-5) There may be some race, which lead to read CCI, before the command complete flag is set, hence returning -EIO. Similar fix has been done also in ucsi_acpi [1]. In case of a spurious or otherwise delayed notification it is possible that CCI still reports the previous completion. The UCSI spec is aware of this and provides two completion bits in CCI, one for normal commands and one for acks. As acks and commands alternate the notification handler can determine if the completion bit is from the current command. To fix this add the ACK_PENDING bit for ucsi_stm32g0 and only complete commands if the completion bit matches. [1] https://lore.kernel.org/lkml/20240121204123.275441-3-lk@c--e.de/ Fixes: 72849d4fcee7 ("usb: typec: ucsi: stm32g0: add support for stm32g0 controller") Signed-off-by: Fabrice Gasnier --- Changes in v2: rebase and define ACK_PENDING as commented by Dmitry. --- drivers/usb/typec/ucsi/ucsi_stm32g0.c | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/drivers/usb/typec/ucsi/ucsi_stm32g0.c b/drivers/usb/typec/ucsi/ucsi_stm32g0.c index ac48b7763114..ac69288e8bb0 100644 --- a/drivers/usb/typec/ucsi/ucsi_stm32g0.c +++ b/drivers/usb/typec/ucsi/ucsi_stm32g0.c @@ -65,6 +65,7 @@ struct ucsi_stm32g0 { struct device *dev; unsigned long flags; #define COMMAND_PENDING 1 +#define ACK_PENDING 2 const char *fw_name; struct ucsi *ucsi; bool suspended; @@ -396,9 +397,13 @@ static int ucsi_stm32g0_sync_write(struct ucsi *ucsi, unsigned int offset, const size_t len) { struct ucsi_stm32g0 *g0 = ucsi_get_drvdata(ucsi); + bool ack = UCSI_COMMAND(*(u64 *)val) == UCSI_ACK_CC_CI; int ret; - set_bit(COMMAND_PENDING, &g0->flags); + if (ack) + set_bit(ACK_PENDING, &g0->flags); + else + set_bit(COMMAND_PENDING, &g0->flags); ret = ucsi_stm32g0_async_write(ucsi, offset, val, len); if (ret) @@ -406,9 +411,14 @@ static int ucsi_stm32g0_sync_write(struct ucsi *ucsi, unsigned int offset, const if (!wait_for_completion_timeout(&g0->complete, msecs_to_jiffies(5000))) ret = -ETIMEDOUT; + else + return 0; out_clear_bit: - clear_bit(COMMAND_PENDING, &g0->flags); + if (ack) + clear_bit(ACK_PENDING, &g0->flags); + else + clear_bit(COMMAND_PENDING, &g0->flags); return ret; } @@ -429,8 +439,9 @@ static irqreturn_t ucsi_stm32g0_irq_handler(int irq, void *data) if (UCSI_CCI_CONNECTOR(cci)) ucsi_connector_change(g0->ucsi, UCSI_CCI_CONNECTOR(cci)); - if (test_bit(COMMAND_PENDING, &g0->flags) && - cci & (UCSI_CCI_ACK_COMPLETE | UCSI_CCI_COMMAND_COMPLETE)) + if (cci & UCSI_CCI_ACK_COMPLETE && test_and_clear_bit(ACK_PENDING, &g0->flags)) + complete(&g0->complete); + if (cci & UCSI_CCI_COMMAND_COMPLETE && test_and_clear_bit(COMMAND_PENDING, &g0->flags)) complete(&g0->complete); return IRQ_HANDLED; -- 2.25.1