From patchwork Thu Jun 27 14:44:43 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dmitry Baryshkov X-Patchwork-Id: 13714511 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 7D679C3064D for ; Thu, 27 Jun 2024 14:45:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Cc:To:In-Reply-To:References :Message-Id:Content-Transfer-Encoding:Content-Type:MIME-Version:Subject:Date: From:Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=Pv3PctyJy2SnI13bqUyVzb7EmYqWrJDar658iy+XONk=; b=xPHd3WuCl2m72p86VAWo5Tm4z/ +RfUaYhedS+ahsnXrk7W/BYHP2zFzt7eCZQFUuOR2L0jzDKYE/ZIp+XuboV8S84qtq/jiGyzWUF1v sAma31CJyGntb8SLvIlW+4Wky7WabkYQ3vssYOgtsB0rmSXPdV/xlm3CVHuLHLLPMaNtH7WkdMj65 Lv/rlk1k2v5vpTwMjSwrinTpPLHGRpsEdWUSWh2Glai6nGIvyUCIFcCcAIq5tMeZ4faFmV6GVy+Uh 3hnFhtfGlO5NO6hoyCFDlALUr0rCN4ROok21Izo322xQldKjlK+elm8+8qS/Rrd6hyvBHSgQ2EStE yTi/3g/A==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sMqNY-0000000Agjm-3sM0; Thu, 27 Jun 2024 14:45:40 +0000 Received: from mail-lj1-x230.google.com ([2a00:1450:4864:20::230]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sMqMo-0000000AgLr-2xrt for linux-arm-kernel@lists.infradead.org; Thu, 27 Jun 2024 14:44:58 +0000 Received: by mail-lj1-x230.google.com with SMTP id 38308e7fff4ca-2ec58040f39so55418381fa.2 for ; Thu, 27 Jun 2024 07:44:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1719499487; x=1720104287; darn=lists.infradead.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=Pv3PctyJy2SnI13bqUyVzb7EmYqWrJDar658iy+XONk=; b=fIg3dSqYRLFEtZcaLQyfDCCzWsxT0G2T8cpJzTNj6mDv56t00nYDrE6AAc9Ziflmcy sTEiSKMecICz95JkbQR3+ESmNru7ZrWQRBFu2SiWBXWq5w6Lbxq2KsEoPNzzSUnrrB6L P1P/vFpptbnd0AfMuMZHoqAdU2jLxb9ItZSHwFfEbwC3lUMgl5ISm3nmaZ2e1HrbkDpb ohG8tvL5GAtzNEZHqFYu8x0Dsh+QUa4Aus/49cq0hx4NZQnNM+yOnJQUSX2fbr3vj+cm 6rcV3PDk3r053QQ56OXiCW1ZwBGOGaonf2/Kovx0wuAWhrATgOZCWV/iR6cC6Y9f7FQ6 uA8A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1719499487; x=1720104287; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Pv3PctyJy2SnI13bqUyVzb7EmYqWrJDar658iy+XONk=; b=QmhTbipdZl69yA6fzEZQFXHWSO0MtxvErHDLXRPMIi7CUtniiKis/IvQB3l/jugcNR Ob3ZZ575z4KJgV3T2aMwBn310jEK17FOuzYN6ryYGj9+tM61EEXHysGvAtkLgVVyVJOn Bfyr84TbpfgvghQUtrvuGVQmbh9XKxMOSLdL8YoJphNExOXU6KsdmVbPGtcoxLpIDS2e 0hT/HeG+R1m2DA62k4b9225tOS+GBM+ZiaG1A+DGLID2ywVhGtHNm4EXsVeA68U4rrx6 Hr4ALhISs4UQN5boFsq/N7kbGChEQLv5hmC18lzC8u6B0Xle6RkI7Ynrng/LBXdiW7mR yv8Q== X-Forwarded-Encrypted: i=1; AJvYcCUP2Q9QXDdou9KIzpUnEoZlBVUsv94cRzhuORpEg8nrP3MourMO1yFbx98MzYwwdCpnEHYdjNnTxOn2VHmz94pWnPJgZZqcaw7Y2MRxKUWKYb+CXmc= X-Gm-Message-State: AOJu0YyyC7ewZ+Doaa1TuMJ68p6z17veUwltSYU6jvomqWIUat0/OmER VCVj0dBplj5NMw5mD9CDSg+j1VWZ2MIKNLypGfILuKWSVDxyizkKp4/5iNbZLl0= X-Google-Smtp-Source: AGHT+IGyKCad0pTtyjWDWYT4auTNDLJlAquHp5LlnqVILjGBHWKp/6jvkXxkAFrJTEFJQT7zvH22Gw== X-Received: by 2002:a2e:b004:0:b0:2eb:ebcd:fc1c with SMTP id 38308e7fff4ca-2ec594034bbmr78292621fa.26.1719499486745; Thu, 27 Jun 2024 07:44:46 -0700 (PDT) Received: from umbar.lan ([192.130.178.91]) by smtp.gmail.com with ESMTPSA id 38308e7fff4ca-2ee4a34447fsm2775911fa.11.2024.06.27.07.44.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 27 Jun 2024 07:44:46 -0700 (PDT) From: Dmitry Baryshkov Date: Thu, 27 Jun 2024 17:44:43 +0300 Subject: [PATCH v4 4/7] usb: typec: ucsi: rework command execution functions MIME-Version: 1.0 Message-Id: <20240627-ucsi-rework-interface-v4-4-289ddc6874c7@linaro.org> References: <20240627-ucsi-rework-interface-v4-0-289ddc6874c7@linaro.org> In-Reply-To: <20240627-ucsi-rework-interface-v4-0-289ddc6874c7@linaro.org> To: Heikki Krogerus , Greg Kroah-Hartman , Maxime Coquelin , Alexandre Torgue Cc: Nikita Travkin , Neil Armstrong , linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org, linux-stm32@st-md-mailman.stormreply.com, linux-arm-kernel@lists.infradead.org X-Mailer: b4 0.13.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=4910; i=dmitry.baryshkov@linaro.org; h=from:subject:message-id; bh=IUlRPLcwpNg8JnyGbOvllbRci0dly68y4uffDqgWHcE=; b=owEBbQGS/pANAwAKAYs8ij4CKSjVAcsmYgBmfXrZU2jPKiwjQP35+8I88ou3BxaPfkpg4PJJq onR4FOLWHaJATMEAAEKAB0WIQRMcISVXLJjVvC4lX+LPIo+Aiko1QUCZn162QAKCRCLPIo+Aiko 1cBnB/sEp9lqE76ma6UnCY7LbhqliIuPkO9FULEW8UHfMI5X8F+JdTgWpedlAKFdYNxEK48AiL5 xytAUgkAj6ceGwOp0xx/JTMBPfGy/LNlBQVvbzU1YiRjUr+lbg6H1lI80C5VvdijDo+1hnCNQRV ecEYU8muhwSmRAj0FR1SIQ10cp13vLbTdEse+BDcmnHMwMh41KT4DknuX3Pfkler5ez4oOucu5o R/9mtPjhNSLk1SkyMDxYrJIC7Tq8ds8MBqBFTnWBzsHJ3CG9sS0o7HlCr6Wj78lqxsWVId+Mv73 RrG8rgRv1c9gmbcNJ99Kk8jsDbZtPmbx4tG9QhqGFKT7lSTU X-Developer-Key: i=dmitry.baryshkov@linaro.org; a=openpgp; fpr=8F88381DD5C873E4AE487DA5199BF1243632046A X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240627_074454_801208_15410248 X-CRM114-Status: GOOD ( 15.38 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Rework command execution code to remove recursive calls of ucsi_exec_command. This also streamlines the sync_control / read(CCI) read (MESSAGE_IN) sequence, allowing further rework of the command code. Tested-by: Heikki Krogerus Reviewed-by: Heikki Krogerus Signed-off-by: Dmitry Baryshkov --- drivers/usb/typec/ucsi/ucsi.c | 138 ++++++++++++++++++++---------------------- 1 file changed, 66 insertions(+), 72 deletions(-) diff --git a/drivers/usb/typec/ucsi/ucsi.c b/drivers/usb/typec/ucsi/ucsi.c index 17d12c1872f6..10a8fe893333 100644 --- a/drivers/usb/typec/ucsi/ucsi.c +++ b/drivers/usb/typec/ucsi/ucsi.c @@ -63,27 +63,76 @@ static int ucsi_acknowledge(struct ucsi *ucsi, bool conn_ack) return ucsi->ops->sync_control(ucsi, ctrl); } -static int ucsi_exec_command(struct ucsi *ucsi, u64 command); +static int ucsi_run_command(struct ucsi *ucsi, u64 command, u32 *cci, + void *data, size_t size, bool conn_ack) +{ + int ret; + + *cci = 0; + + ret = ucsi->ops->sync_control(ucsi, command); + if (ret) + return ret; + + ret = ucsi->ops->read_cci(ucsi, cci); + if (ret) + return ret; + + if (*cci & UCSI_CCI_BUSY) + return -EBUSY; + + if (!(*cci & UCSI_CCI_COMMAND_COMPLETE)) + return -EIO; + + if (*cci & UCSI_CCI_NOT_SUPPORTED) { + if (ucsi_acknowledge(ucsi, false) < 0) + dev_err(ucsi->dev, + "ACK of unsupported command failed\n"); + return -EOPNOTSUPP; + } + + if (*cci & UCSI_CCI_ERROR) { + /* Acknowledge the command that failed */ + ret = ucsi_acknowledge(ucsi, false); + return ret ? ret : -EIO; + } + + if (data) { + ret = ucsi_read_message_in(ucsi, data, size); + if (ret) + return ret; + } + + ret = ucsi_acknowledge(ucsi, conn_ack); + if (ret) + return ret; + + return 0; +} static int ucsi_read_error(struct ucsi *ucsi, u8 connector_num) { u64 command; u16 error; + u32 cci; int ret; command = UCSI_GET_ERROR_STATUS | UCSI_CONNECTOR_NUMBER(connector_num); - ret = ucsi_exec_command(ucsi, command); - if (ret < 0) - return ret; + ret = ucsi_run_command(ucsi, command, &cci, + &error, sizeof(error), false); - ret = ucsi_read_message_in(ucsi, &error, sizeof(error)); - if (ret) - return ret; + if (cci & UCSI_CCI_BUSY) { + ret = ucsi_run_command(ucsi, UCSI_CANCEL, &cci, NULL, 0, false); - ret = ucsi_acknowledge(ucsi, false); - if (ret) + return ret ? ret : -EBUSY; + } + + if (ret < 0) return ret; + if (cci & UCSI_CCI_ERROR) + return -EIO; + switch (error) { case UCSI_ERROR_INCOMPATIBLE_PARTNER: return -EOPNOTSUPP; @@ -129,7 +178,8 @@ static int ucsi_read_error(struct ucsi *ucsi, u8 connector_num) return -EIO; } -static int ucsi_exec_command(struct ucsi *ucsi, u64 cmd) +static int ucsi_send_command_common(struct ucsi *ucsi, u64 cmd, + void *data, size_t size, bool conn_ack) { u8 connector_num; u32 cci; @@ -155,73 +205,17 @@ static int ucsi_exec_command(struct ucsi *ucsi, u64 cmd) connector_num = 0; } - ret = ucsi->ops->sync_control(ucsi, cmd); - if (ret) - return ret; - - ret = ucsi->ops->read_cci(ucsi, &cci); - if (ret) - return ret; - - if (cmd != UCSI_CANCEL && cci & UCSI_CCI_BUSY) - return ucsi_exec_command(ucsi, UCSI_CANCEL); - - if (!(cci & UCSI_CCI_COMMAND_COMPLETE)) - return -EIO; - - if (cci & UCSI_CCI_NOT_SUPPORTED) { - if (ucsi_acknowledge(ucsi, false) < 0) - dev_err(ucsi->dev, - "ACK of unsupported command failed\n"); - return -EOPNOTSUPP; - } - - if (cci & UCSI_CCI_ERROR) { - /* Acknowledge the command that failed */ - ret = ucsi_acknowledge(ucsi, false); - if (ret) - return ret; - - if (cmd == UCSI_GET_ERROR_STATUS) - return -EIO; - - return ucsi_read_error(ucsi, connector_num); - } - - if (cmd == UCSI_CANCEL && cci & UCSI_CCI_CANCEL_COMPLETE) { - ret = ucsi_acknowledge(ucsi, false); - return ret ? ret : -EBUSY; - } - - return UCSI_CCI_LENGTH(cci); -} - -static int ucsi_send_command_common(struct ucsi *ucsi, u64 command, - void *data, size_t size, bool conn_ack) -{ - u8 length; - int ret; - mutex_lock(&ucsi->ppm_lock); - ret = ucsi_exec_command(ucsi, command); - if (ret < 0) - goto out; - - length = ret; - - if (data) { - ret = ucsi_read_message_in(ucsi, data, size); - if (ret) - goto out; + ret = ucsi_run_command(ucsi, cmd, &cci, data, size, conn_ack); + if (cci & UCSI_CCI_BUSY) { + ret = ucsi_run_command(ucsi, UCSI_CANCEL, &cci, NULL, 0, false); + return ret ? ret : -EBUSY; } - ret = ucsi_acknowledge(ucsi, conn_ack); - if (ret) - goto out; + if (cci & UCSI_CCI_ERROR) + return ucsi_read_error(ucsi, connector_num); - ret = length; -out: mutex_unlock(&ucsi->ppm_lock); return ret; }