From patchwork Wed Jun 15 09:02:57 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Quan Nguyen X-Patchwork-Id: 1643535 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=os.amperecomputing.com header.i=@os.amperecomputing.com header.a=rsa-sha256 header.s=selector2 header.b=tCsr1GmC; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=vger.kernel.org (client-ip=2620:137:e000::1:20; helo=out1.vger.email; envelope-from=linux-i2c-owner@vger.kernel.org; receiver=) Received: from out1.vger.email (out1.vger.email [IPv6:2620:137:e000::1:20]) by bilbo.ozlabs.org (Postfix) with ESMTP id 4LNK9L4cNyz9sG0 for ; Wed, 15 Jun 2022 19:03:50 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1346976AbiFOJDs (ORCPT ); Wed, 15 Jun 2022 05:03:48 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41734 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1346876AbiFOJDk (ORCPT ); Wed, 15 Jun 2022 05:03:40 -0400 Received: from NAM04-MW2-obe.outbound.protection.outlook.com (mail-mw2nam04on2135.outbound.protection.outlook.com [40.107.101.135]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8AA9935879; Wed, 15 Jun 2022 02:03:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=ONAEvuxrgPuR1nTsPaeHhThANf86BDr3UONT96F4wM+o03f8tyH0je9+t8455Px8edxOBrMh0S3dx5LapyBO13pkMbRsM40XGkpcMtr6YoJrx08eKmvSW1wpj/GW+CUryMuX9e03VDMmuct1unvA19iZXaUJyv2wYmdSfZFCk1Y2Ipes4MefbALS6HllWRv9yC2VVmVzDXAn9ODGvNoXmDaX2QuFVKaf5taEB6UkM1Zox9iJI3SVGB7/Cl+3KWAnVmIPBJGSi0b6JhqKQk6hXLKFvvi1T5KJsQznrWRpC/XLg8W7DEcLH/DZYqhLWz1E34uHBRc38TZIPsM9U9uUWg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=HkbncWm8KdYRPIq5CqwLUVHchpN4uKSNJQhuSu8qHeo=; b=YbW7TiSG30uMFI23MWLtKfvsAXxVa71G96HNxe/5vmymUb2WixuDF4u5vTJubmTE7FeG1x7GNA+uGU/ery4pbFGHFdv5gU9MbXvUFTxJOUv05oUBBQs+nrge8qceLdXd+MObe0XlGFSphDECihnhOLCIvvNjJmVqXT5W3WyL3QRzDOOutsNf6/w5OYJksPrpr3kwmP62uRG31ecDe1CMBxA/a5N6DlV0otS4Ulsinh3UcXM7m4JStzhTQ2OMeWEE42IgJElOp0ZZ6TrZAwcIFoxpyQkUFOrvSD5SCR5JALSRI9OaxONc4bracebqikE6NLQ9rh23oaH2Hk74GDIeyQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=os.amperecomputing.com; dmarc=pass action=none header.from=os.amperecomputing.com; dkim=pass header.d=os.amperecomputing.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=os.amperecomputing.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=HkbncWm8KdYRPIq5CqwLUVHchpN4uKSNJQhuSu8qHeo=; b=tCsr1GmCIziGM4n3B84EF5vwm1Tt82iXHmK7llbzD/lVlGXb8wtc0y50JcHn46Vdh3orIpj5nr0wp9RYy/iV9odci2wTE/jDLxKY/r3FtxcEvc1wluBMBeBBdW0pn0rugLqURNKr0J9+ViLc5J4+9mjTg3M3J91Z6LZA24ObKTA= Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=os.amperecomputing.com; Received: from SJ0PR01MB7282.prod.exchangelabs.com (2603:10b6:a03:3f2::24) by BN0PR01MB6909.prod.exchangelabs.com (2603:10b6:408:166::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5332.13; Wed, 15 Jun 2022 09:03:31 +0000 Received: from SJ0PR01MB7282.prod.exchangelabs.com ([fe80::7535:773:f979:893e]) by SJ0PR01MB7282.prod.exchangelabs.com ([fe80::7535:773:f979:893e%7]) with mapi id 15.20.5332.020; Wed, 15 Jun 2022 09:03:31 +0000 From: Quan Nguyen To: Quan Nguyen , Corey Minyard , Rob Herring , Krzysztof Kozlowski , Brendan Higgins , Benjamin Herrenschmidt , Joel Stanley , Andrew Jeffery , Wolfram Sang , openipmi-developer@lists.sourceforge.net, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, linux-i2c@vger.kernel.org, openbmc@lists.ozlabs.org, linux-arm-kernel@lists.infradead.org, linux-aspeed@lists.ozlabs.org Cc: Open Source Submission , Phong Vo , "Thang Q . Nguyen" Subject: [PATCH v8 1/3] ipmi: ssif_bmc: Add SSIF BMC driver Date: Wed, 15 Jun 2022 16:02:57 +0700 Message-Id: <20220615090259.1121405-2-quan@os.amperecomputing.com> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220615090259.1121405-1-quan@os.amperecomputing.com> References: <20220615090259.1121405-1-quan@os.amperecomputing.com> X-ClientProxiedBy: SG2PR06CA0189.apcprd06.prod.outlook.com (2603:1096:4:1::21) To SJ0PR01MB7282.prod.exchangelabs.com (2603:10b6:a03:3f2::24) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 8b31cfb1-e4e3-4613-5dde-08da4eadeb5d X-MS-TrafficTypeDiagnostic: BN0PR01MB6909:EE_ X-Microsoft-Antispam-PRVS: X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: e+igltfxU1kvfT1PLg/iIH01xrEdpJcSeqEkBkfpBLzrZXp3CxtgLd3uNPSqksaFJzly+vulPVeJ48iKj+fmiPgzGGClp/bjebjfaTUjki+CrSoos1jC4rco35xzPeVp2horsas/c8n1D3hjxYcBRgLlFO0FuKwZuRRSCdF5cKBtlEzzt0F5fizZfnASbTRuK2kgBkIYpYJzvcF2ysrrjtS3Dg0GElmgTk97jlVqgVyCU2IDLO5lAywUzvjxZSbHKdIzDJE6rfTbdfC4WWZF+KoQubceHvl0JTPDPEUCYQS2+foAspWUy+Tq51LQXjfj5GBQjXTezOlMscWV+k9avlAsESTSPAXJAd+UgISeou5RNcKsf0g1H7/edLKqqV77rQTfcHH4/ZqhGzemehA0FNhGzts0jf4iVnvALWA/WES0qqxGabP41mCL3V1S43rQjZUmDRE2DGmmco1CXn0DcK2w7sUiaeMzGfOrVt+8V5HLshCvKBvuzYdn/GD/P9imCEpLszk4qQHP/yjm7ybkoS63pcRhaaT7ngq9sa7EmLXL9Mgwc8WcPIMSGqqaV1EWGD8G99y/Nnb6mfccVjx1BtyncKmyQ5o0FSFoobghNFKa+mhHp0tYiUuR8qBpZffIVqJxTFs+0k2UP49Ut/8Sn+QW1/3oaSw6pg0RL9k5RWPKzGKgP+RnpiPw2/avOG/ZU06jpl3eLdy1ofWdmCzAsXTHA9B+e0qb0sDLs2hfLqCmEc6dJGVQqBAMuXerReIFALVgM0X6glhOl0215FRSt9s8GlOj+OH8EzW9p86Ol0YmCBrHpi4wMA/ezI2Ch5g9 X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:SJ0PR01MB7282.prod.exchangelabs.com;PTR:;CAT:NONE;SFS:(13230016)(4636009)(366004)(110136005)(6486002)(966005)(508600001)(2906002)(7416002)(6666004)(8936002)(186003)(316002)(30864003)(54906003)(4326008)(5660300002)(66946007)(66556008)(66476007)(8676002)(38100700002)(52116002)(86362001)(6512007)(921005)(1076003)(107886003)(6506007)(2616005)(38350700002)(26005)(83380400001);DIR:OUT;SFP:1102; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?utf-8?q?fSg/tB8itvq//L14jlKt+xWWz+oc?= =?utf-8?q?khKDbIfM/osVghFT4BuT3oB3KGmWKO8q8/PWwwrmDxwAUH7JEMbTRpaRdh+SyEt8Q?= =?utf-8?q?NUPUxcMqOVDvEHbO6NPBKVJNGJYuJfjIz2242vLrwQLMkfKnX707w8gFzPsW/MLcP?= =?utf-8?q?uEWM0nLxCGD5gl+7QqigZuO4VUEFUdrXs9K9FJQNk5hReo69W01zy6uVimFpHby7t?= =?utf-8?q?qkT7kB54dLGQ8sPRr6OcxREC3oJeG8KSmZ+tOuvPnIJ5UvZSiiC+iKXZ3j+mA/lu+?= =?utf-8?q?VBDI7T2hdLzQp3tGLXVQMIW6iss6dOITfz/kBDhM8WBZu1DsGA6cj0s2Ihfhltd8w?= =?utf-8?q?pgxdS4w0YRfLB5bEMfcUkRw+kbIf2KSdvyrpk9Eob9qwNV6hkSm+SONuZAV9sjnSH?= =?utf-8?q?9IuRZaxqe1D4No4AxmUL7ORIHNuwvG5b/r5n3gEKJH5leK8dPykWAAAGbVwcb8iLR?= =?utf-8?q?vS9EVmrYJRJpm/Oc0WGAukTALat4DDhhnJqGJ2wWY6+6alDk6xccUkxbh1r2uy1MC?= =?utf-8?q?7dN7EV9MFvoKq0uA/Oyc1VVqggVjPDX6vvnlkjvB2Q9YV5i5y5EjKc0bcKLX/2ugR?= =?utf-8?q?yvxMnUky8JiqMKpJcbPGptfGLdUtdRHzPrgjgFlm9IRi6mU0m3J33p6M3mG6f8xcZ?= =?utf-8?q?DfD+jwrWCYlY416+ruzDTvUr1EJgAMPY3zUUDgKm6jJ7OxqCr9nej4VWg79bCFf4C?= =?utf-8?q?iHzX8GNV+IXAiq3zdkkVhuMHeAxTWLH9PTUQ8938GQ7iVrt1xZhtuwcj6yVzBqaH/?= =?utf-8?q?w7feEy7l8565GWrkgfIWQuH2NdjQg/ehPnRUYgJ5c6og4fVVLa/Z+Wl7ChKTFsLC2?= =?utf-8?q?NaLFzAUqCyCICbxg3E4EtOqS9VlRa3PB6a7702NDoD049BU3hQI3dVFbenMiPjUcV?= =?utf-8?q?ZuypuM0lX9w+pvxriaEzYzO7UZAoyvC/TwKxKsj9H6tmGP5GNjLLX149CRLxn+6YZ?= =?utf-8?q?xxDN7Eki1701ASnvyk49rUTEm8qBRL6zn8e+bJo6FqSvHNCRTVH1a0VLolq6WAb8U?= =?utf-8?q?JnhQEpjERR3Hf/h55/XZ1kF1zGRJX/YnRcLdBH7itLGm2j0ibHey1w6YftrC3ycEA?= =?utf-8?q?DcGx8OFg32UKGflu5wBFF60V95Dtb0IaR0dSVsBaZN9ClSW/DiHE6P0fEDxD9Eh3M?= =?utf-8?q?QsnAbjPHw0Abm1mV2m5gBDl3QyGPt05knQq+wJOdBmxmKR3oc6SIBQ+RmGThEheJr?= =?utf-8?q?Fgs+Bjt/B3eL23b8+AvBV61itoCa8FRdgCKOosepv95+mu/+cRHL9tBNifZyHR81s?= =?utf-8?q?vdBMvn7wTv6mRWX9f76Sm2zqqIoxtjUR/lLVf2dy9cGExLSRbfmz9VPGinFFpT+GH?= =?utf-8?q?rkwbbI7DlZUh5lXMpokZoK7MnIN+JjdBmdbXuZUgVy6UsqRoLsPYYlA4Cg9IpF0vN?= =?utf-8?q?AgpAFWmfljWsx8Z4sfX55H4EL5tW+oNyoSvFv2QUxua/hx4u8qtvmPMX8spSVq3tB?= =?utf-8?q?VcgwBuzdUK8tcCFc65EaD83Sg/zfwf+q07zpMR5vQQlTefZoyT38pKDM5VlLrtwso?= =?utf-8?q?viU00EAO6qutTKkuSHttDEiFJHb5PMS0rmTheYFR6MHveBhK+1cjggL4JJXVNwGJX?= =?utf-8?q?+1gX1jlmenTfc8c9ryx2JdxAmgRke61VMYTXRjjo1F1r69yC0lVYM5yDXryxO3s3F?= =?utf-8?q?kF04DRQOBaUBXQu9X2YdnGEidIApsRIVZNFpTKaDVQusYEpFFLh8n1K9XlZFqa1Ty?= =?utf-8?q?y/OJ4/10c?= X-OriginatorOrg: os.amperecomputing.com X-MS-Exchange-CrossTenant-Network-Message-Id: 8b31cfb1-e4e3-4613-5dde-08da4eadeb5d X-MS-Exchange-CrossTenant-AuthSource: SJ0PR01MB7282.prod.exchangelabs.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 15 Jun 2022 09:03:31.6262 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 3bc2b170-fd94-476d-b0ce-4229bdc904a7 X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: 2QI9gV0P+Fx6YYH9LhN0tweAXHpSOrwpbpsy2hligUOCUL/YUkbG1UISXH8vVdMA/CMcKk780yjKR1M13b5h9XktI6Vyiq/nrb1lh48QEjk= X-MS-Exchange-Transport-CrossTenantHeadersStamped: BN0PR01MB6909 X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2, SPF_HELO_PASS,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-i2c@vger.kernel.org The SMBus system interface (SSIF) IPMI BMC driver can be used to perform in-band IPMI communication with their host in management (BMC) side. Thanks Dan for the copy_from_user() fix in the link below. Link: https://lore.kernel.org/linux-arm-kernel/20220310114119.13736-4-quan@os.amperecomputing.com/ Signed-off-by: Quan Nguyen Reported-by: kernel test robot --- v8: + Dropped ssif_bmc.h file and move its content to ssif_bmc.c [Corey] + Add struct ipmi_ssif_msg to include/uapi/linux/ipmi_ssif_bmc.h header file [Corey] + Use unsigned int for len field in struct ipmi_ssif_msg [Corey] + Avoid using packed structure [Corey] + Add comment to clarify the logic flow [Corey] + Fix multipart read end with len=0 issue [Corey] + Refactor code handle the too big request message [Corey] + Fix code indentation issue [Corey] + Clean buffer before receiving request to avoid garbage [Quan] + Fix the license to SPDX-License-Identifier: GPL-2.0-only [Quan] v7: + Remove unneccessary del_timer() in response_timeout() [Corey] + Change compatible string from "ampere,ssif-bmc" to "ssif-bmc" [Jae] + Add MODULE_DEVICE_TABLE(of, ssif_bmc_match), fix blank line [Jae] + Dropped the use of ssif_msg_len() macro, use the len directly [Quan] + Solve possible issue if both response timer and ssif_bmc_write() occurred at the same time [Corey] + Fix wrong return type of ssif_bmc_poll() [kernel robot test] + Refactor and introduce ssif_part_buffer struct to replace the response_buf to manage each send/receive part of ssif [Quan] + Change SSIF_BAD_SMBUS state to SSIF_ABORTING state [Corey] + Support abort feature to skip the current bad request/response and wait until next new request [Corey] + Refactor the PEC calculation to avoid the re-calculate the PEC on each I2C_SLAVE_WRITE_RECEIVED event [Quan] + Fix the use of error-proned idx [Corey] + Defer the test for valid SMBus command until the read/write part is determined [Quan] + Change/split unsupported_smbus_cmd() to supported_[write|read]_cmd() [Corey] + Abort the request if somehow its size exceeded 255 bytes [Quan] v6: + Drop the use of slave_enable() [Wolfram] + Make i2c-aspeed to issue RxCmdLast command on all I2C_SLAVE_WRITE_REQUESTED event to assert NAK when slave busy [Quan] + Make i2c slave to return -EBUSY when it's busy [Quan] + Drop the aborting feature as return Completion Code 0xFF may stop host to retry and make ipmi_ssif.so fails to load [Quan] + Add timer to recover slave from busy state when no response [Quan] + Clean request/response buffer appropriately [Quan] + Add some minor change on error and warning messages [Quan] v5: + None v4: + Send response with Completion code 0xFF when aborting [Quan] + Added bounding check on SMBus writes and the whole request [Dan] + Moved buffer to end of struct ssif_bmc_ctx to avoid context corruption if somehow buffer is written past the end [Dan] + Return -EINVAL if userspace buffer too small, dont silence truncate [Corey, Joel] + Not necessary to check NONBLOCK in lock [Corey] + Enforce one user at a time [Joel] + Reject write with invalid response length from userspace [Corey] + Add state machines for better ssif bmc state handling [Quan] + Drop ssif_bmc_aspeed.c and make ssif_bmc.c is generic SSIF BMC driver [Quan] + Change compatible string "aspeed,ast2500-ssif-bmc" to "ampere,ssif-bmc" [Quan] + Abort current request with invalid SMBus write or invalid command [Quan] + Abort all request if there is pending response [Quan] + Changed validate_pec() to validate_request() [Quan] + Add unsupported_smbus_cmd() to handle unknown SMBus command [Quan] + Print internal state string for ease investigating issue [Quan] + Move to READY state on SLAVE_STOP event [Quan] + Change initilize_transfer() to process_smbus_cmd() [Quan] + Introduce functions for each slave event [Quan] v3: + Removed redundant license info [Joel] + Switched to use traditional if-else [Joel] + Removed unused ssif_bmc_ioctl() [Joel] + Made handle_request()/complete_response() to return void [Joel] + Refactored send_ssif_bmc_response() and receive_ssif_bmc_request() [Corey] + Removed mutex [Corey] + Use spin_lock/unlock_irqsave/restore in callback [Corey] + Removed the unnecessary memset [Corey] + Switch to use dev_err() [Corey] v2: + Fixed compiling error with COMPILE_TEST for arc drivers/char/ipmi/Kconfig | 11 + drivers/char/ipmi/Makefile | 1 + drivers/char/ipmi/ssif_bmc.c | 880 +++++++++++++++++++++++++++++ include/uapi/linux/ipmi_ssif_bmc.h | 16 + 4 files changed, 908 insertions(+) create mode 100644 drivers/char/ipmi/ssif_bmc.c create mode 100644 include/uapi/linux/ipmi_ssif_bmc.h diff --git a/drivers/char/ipmi/Kconfig b/drivers/char/ipmi/Kconfig index b061e6b513ed..18a89093d64e 100644 --- a/drivers/char/ipmi/Kconfig +++ b/drivers/char/ipmi/Kconfig @@ -169,6 +169,17 @@ config ASPEED_BT_IPMI_BMC found on Aspeed SOCs (AST2400 and AST2500). The driver implements the BMC side of the BT interface. +config SSIF_IPMI_BMC + tristate "SSIF IPMI BMC driver" + select I2C + select I2C_SLAVE + help + This enables the IPMI SMBus system interface (SSIF) at the + management (BMC) side. + + The driver implements the BMC side of the SMBus system + interface (SSIF). + config IPMB_DEVICE_INTERFACE tristate 'IPMB Interface handler' depends on I2C diff --git a/drivers/char/ipmi/Makefile b/drivers/char/ipmi/Makefile index 7ce790efad92..cb6138b8ded9 100644 --- a/drivers/char/ipmi/Makefile +++ b/drivers/char/ipmi/Makefile @@ -30,3 +30,4 @@ obj-$(CONFIG_ASPEED_BT_IPMI_BMC) += bt-bmc.o obj-$(CONFIG_ASPEED_KCS_IPMI_BMC) += kcs_bmc_aspeed.o obj-$(CONFIG_NPCM7XX_KCS_IPMI_BMC) += kcs_bmc_npcm7xx.o obj-$(CONFIG_IPMB_DEVICE_INTERFACE) += ipmb_dev_int.o +obj-$(CONFIG_SSIF_IPMI_BMC) += ssif_bmc.o diff --git a/drivers/char/ipmi/ssif_bmc.c b/drivers/char/ipmi/ssif_bmc.c new file mode 100644 index 000000000000..0bfd4b9bbaf1 --- /dev/null +++ b/drivers/char/ipmi/ssif_bmc.c @@ -0,0 +1,880 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * The driver for BMC side of SSIF interface + * + * Copyright (c) 2022, Ampere Computing LLC + * + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define DEVICE_NAME "ipmi-ssif-host" + +#define GET_8BIT_ADDR(addr_7bit) (((addr_7bit) << 1) & 0xff) + +/* A standard SMBus Transaction is limited to 32 data bytes */ +#define MAX_PAYLOAD_PER_TRANSACTION 32 +/* Transaction includes the address, the command, the length and the PEC byte */ +#define MAX_TRANSACTION (MAX_PAYLOAD_PER_TRANSACTION + 4) + +#define MAX_IPMI_DATA_PER_START_TRANSACTION 30 +#define MAX_IPMI_DATA_PER_MIDDLE_TRANSACTION 31 + +#define SSIF_IPMI_SINGLEPART_WRITE 0x2 +#define SSIF_IPMI_SINGLEPART_READ 0x3 +#define SSIF_IPMI_MULTIPART_WRITE_START 0x6 +#define SSIF_IPMI_MULTIPART_WRITE_MIDDLE 0x7 +#define SSIF_IPMI_MULTIPART_WRITE_END 0x8 +#define SSIF_IPMI_MULTIPART_READ_START 0x3 +#define SSIF_IPMI_MULTIPART_READ_MIDDLE 0x9 + +/* + * IPMI 2.0 Spec, section 12.7 SSIF Timing, + * Request-to-Response Time is T6max(250ms) - T1max(20ms) - 3ms = 227ms + * Recover ssif_bmc from busy state if it takes up to 500ms + */ +#define RESPONSE_TIMEOUT 500 /* ms */ + +struct ssif_part_buffer { + u8 address; + u8 smbus_cmd; + u8 length; + u8 payload[MAX_PAYLOAD_PER_TRANSACTION]; + u8 pec; + u8 index; +}; + +/* + * SSIF internal states: + * SSIF_READY 0x00 : Ready state + * SSIF_START 0x01 : Start smbus transaction + * SSIF_SMBUS_CMD 0x02 : Received SMBus command + * SSIF_REQ_RECVING 0x03 : Receiving request + * SSIF_RES_SENDING 0x04 : Sending response + * SSIF_BAD_SMBUS 0x05 : Bad SMbus transaction + */ +enum ssif_state { + SSIF_READY, + SSIF_START, + SSIF_SMBUS_CMD, + SSIF_REQ_RECVING, + SSIF_RES_SENDING, + SSIF_ABORTING, + SSIF_STATE_MAX +}; + +struct ssif_bmc_ctx { + struct i2c_client *client; + struct miscdevice miscdev; + int msg_idx; + bool pec_support; + /* ssif bmc spinlock */ + spinlock_t lock; + wait_queue_head_t wait_queue; + u8 running; + enum ssif_state state; + /* Timeout waiting for response */ + struct timer_list response_timer; + bool response_timer_inited; + /* Flag to identify a Multi-part Read Transaction */ + bool is_singlepart_read; + u8 nbytes_processed; + u8 remain_len; + u8 recv_len; + /* Block Number of a Multi-part Read Transaction */ + u8 block_num; + bool request_available; + bool response_in_progress; + bool busy; + bool aborting; + /* Buffer for SSIF Transaction part*/ + struct ssif_part_buffer part_buf; + struct ipmi_ssif_msg response; + struct ipmi_ssif_msg request; +}; + +static inline struct ssif_bmc_ctx *to_ssif_bmc(struct file *file) +{ + return container_of(file->private_data, struct ssif_bmc_ctx, miscdev); +} + +static const char *state_to_string(enum ssif_state state) +{ + switch (state) { + case SSIF_READY: + return "SSIF_READY"; + case SSIF_START: + return "SSIF_START"; + case SSIF_SMBUS_CMD: + return "SSIF_SMBUS_CMD"; + case SSIF_REQ_RECVING: + return "SSIF_REQ_RECVING"; + case SSIF_RES_SENDING: + return "SSIF_RES_SENDING"; + case SSIF_ABORTING: + return "SSIF_ABORTING"; + default: + return "SSIF_STATE_UNKNOWN"; + } +} + +/* Handle SSIF message that will be sent to user */ +static ssize_t ssif_bmc_read(struct file *file, char __user *buf, size_t count, loff_t *ppos) +{ + struct ssif_bmc_ctx *ssif_bmc = to_ssif_bmc(file); + struct ipmi_ssif_msg msg; + unsigned long flags; + ssize_t ret; + + spin_lock_irqsave(&ssif_bmc->lock, flags); + while (!ssif_bmc->request_available) { + spin_unlock_irqrestore(&ssif_bmc->lock, flags); + if (file->f_flags & O_NONBLOCK) + return -EAGAIN; + ret = wait_event_interruptible(ssif_bmc->wait_queue, + ssif_bmc->request_available); + if (ret) + return ret; + spin_lock_irqsave(&ssif_bmc->lock, flags); + } + + if (count < min_t(ssize_t, + sizeof_field(struct ipmi_ssif_msg, len) + ssif_bmc->request.len, + sizeof(struct ipmi_ssif_msg))) { + spin_unlock_irqrestore(&ssif_bmc->lock, flags); + ret = -EINVAL; + } else { + count = min_t(ssize_t, + sizeof_field(struct ipmi_ssif_msg, len) + ssif_bmc->request.len, + sizeof(struct ipmi_ssif_msg)); + memcpy(&msg, &ssif_bmc->request, count); + ssif_bmc->request_available = false; + spin_unlock_irqrestore(&ssif_bmc->lock, flags); + + ret = copy_to_user(buf, &msg, count); + } + + return (ret < 0) ? ret : count; +} + +/* Handle SSIF message that is written by user */ +static ssize_t ssif_bmc_write(struct file *file, const char __user *buf, size_t count, + loff_t *ppos) +{ + struct ssif_bmc_ctx *ssif_bmc = to_ssif_bmc(file); + struct ipmi_ssif_msg msg; + unsigned long flags; + ssize_t ret; + + if (count > sizeof(struct ipmi_ssif_msg)) + return -EINVAL; + + if (copy_from_user(&msg, buf, count)) + return -EFAULT; + + if (!msg.len || count < sizeof_field(struct ipmi_ssif_msg, len) + msg.len) + return -EINVAL; + + spin_lock_irqsave(&ssif_bmc->lock, flags); + while (ssif_bmc->response_in_progress) { + spin_unlock_irqrestore(&ssif_bmc->lock, flags); + if (file->f_flags & O_NONBLOCK) + return -EAGAIN; + ret = wait_event_interruptible(ssif_bmc->wait_queue, + !ssif_bmc->response_in_progress); + if (ret) + return ret; + spin_lock_irqsave(&ssif_bmc->lock, flags); + } + + /* + * The write must complete before the response timeout fired, otherwise + * the response is aborted and wait for next request + * Return -EINVAL if the response is aborted + */ + ret = (ssif_bmc->response_timer_inited) ? 0 : -EINVAL; + if (ret) + goto exit; + + del_timer(&ssif_bmc->response_timer); + ssif_bmc->response_timer_inited = false; + + memcpy(&ssif_bmc->response, &msg, count); + ssif_bmc->is_singlepart_read = (msg.len <= MAX_PAYLOAD_PER_TRANSACTION); + + ssif_bmc->response_in_progress = true; + + /* ssif_bmc not busy */ + ssif_bmc->busy = false; + + /* Clean old request buffer */ + memset(&ssif_bmc->request, 0, sizeof(struct ipmi_ssif_msg)); +exit: + spin_unlock_irqrestore(&ssif_bmc->lock, flags); + + return (ret < 0) ? ret : count; +} + +static int ssif_bmc_open(struct inode *inode, struct file *file) +{ + struct ssif_bmc_ctx *ssif_bmc = to_ssif_bmc(file); + int ret = 0; + + spin_lock_irq(&ssif_bmc->lock); + if (!ssif_bmc->running) + ssif_bmc->running = 1; + else + ret = -EBUSY; + spin_unlock_irq(&ssif_bmc->lock); + + return ret; +} + +static __poll_t ssif_bmc_poll(struct file *file, poll_table *wait) +{ + struct ssif_bmc_ctx *ssif_bmc = to_ssif_bmc(file); + __poll_t mask = 0; + + poll_wait(file, &ssif_bmc->wait_queue, wait); + + spin_lock_irq(&ssif_bmc->lock); + /* The request is available, userspace application can get the request */ + if (ssif_bmc->request_available) + mask |= POLLIN; + + spin_unlock_irq(&ssif_bmc->lock); + + return mask; +} + +static int ssif_bmc_release(struct inode *inode, struct file *file) +{ + struct ssif_bmc_ctx *ssif_bmc = to_ssif_bmc(file); + + spin_lock_irq(&ssif_bmc->lock); + ssif_bmc->running = 0; + spin_unlock_irq(&ssif_bmc->lock); + + return 0; +} + +/* + * System calls to device interface for user apps + */ +static const struct file_operations ssif_bmc_fops = { + .owner = THIS_MODULE, + .open = ssif_bmc_open, + .read = ssif_bmc_read, + .write = ssif_bmc_write, + .release = ssif_bmc_release, + .poll = ssif_bmc_poll, +}; + +/* Called with ssif_bmc->lock held. */ +static void complete_response(struct ssif_bmc_ctx *ssif_bmc) +{ + /* Invalidate response in buffer to denote it having been sent. */ + ssif_bmc->response.len = 0; + ssif_bmc->response_in_progress = false; + ssif_bmc->nbytes_processed = 0; + ssif_bmc->remain_len = 0; + ssif_bmc->busy = false; + memset(&ssif_bmc->part_buf, 0, sizeof(struct ssif_part_buffer)); + wake_up_all(&ssif_bmc->wait_queue); +} + +static void response_timeout(struct timer_list *t) +{ + struct ssif_bmc_ctx *ssif_bmc = from_timer(ssif_bmc, t, response_timer); + unsigned long flags; + + spin_lock_irqsave(&ssif_bmc->lock, flags); + + /* Do nothing if the response is in progress */ + if (!ssif_bmc->response_in_progress) { + /* Recover ssif_bmc from busy */ + ssif_bmc->busy = false; + ssif_bmc->response_timer_inited = false; + /* Set aborting flag */ + ssif_bmc->aborting = true; + } + + spin_unlock_irqrestore(&ssif_bmc->lock, flags); +} + +/* Called with ssif_bmc->lock held. */ +static void handle_request(struct ssif_bmc_ctx *ssif_bmc) +{ + /* set ssif_bmc to busy waiting for response */ + ssif_bmc->busy = true; + /* Request message is available to process */ + ssif_bmc->request_available = true; + /* Clean old response buffer */ + memset(&ssif_bmc->response, 0, sizeof(struct ipmi_ssif_msg)); + /* This is the new READ request.*/ + wake_up_all(&ssif_bmc->wait_queue); + + /* Armed timer to recover slave from busy state in case of no response */ + if (!ssif_bmc->response_timer_inited) { + timer_setup(&ssif_bmc->response_timer, response_timeout, 0); + ssif_bmc->response_timer_inited = true; + } + mod_timer(&ssif_bmc->response_timer, jiffies + msecs_to_jiffies(RESPONSE_TIMEOUT)); +} + +static void calculate_response_part_pec(struct ssif_part_buffer *part) +{ + u8 addr = part->address; + + /* PEC - Start Read Address */ + part->pec = i2c_smbus_pec(0, &addr, 1); + /* PEC - SSIF Command */ + part->pec = i2c_smbus_pec(part->pec, &part->smbus_cmd, 1); + /* PEC - Restart Write Address */ + addr = addr | 0x01; + part->pec = i2c_smbus_pec(part->pec, &addr, 1); + part->pec = i2c_smbus_pec(part->pec, &part->length, 1); + if (part->length) + part->pec = i2c_smbus_pec(part->pec, part->payload, part->length); +} + +static void set_singlepart_response_buffer(struct ssif_bmc_ctx *ssif_bmc) +{ + struct ssif_part_buffer *part = &ssif_bmc->part_buf; + + part->address = GET_8BIT_ADDR(ssif_bmc->client->addr); + part->length = (u8)ssif_bmc->response.len; + + /* Clear the rest to 0 */ + memset(part->payload + part->length, 0, MAX_PAYLOAD_PER_TRANSACTION - part->length); + memcpy(&part->payload[0], &ssif_bmc->response.payload[0], part->length); +} + +static void set_multipart_response_buffer(struct ssif_bmc_ctx *ssif_bmc) +{ + struct ssif_part_buffer *part = &ssif_bmc->part_buf; + u8 part_len = 0; + + part->address = GET_8BIT_ADDR(ssif_bmc->client->addr); + switch (part->smbus_cmd) { + case SSIF_IPMI_MULTIPART_READ_START: + /* + * Read Start length is 32 bytes. + * Read Start transfer first 30 bytes of IPMI response + * and 2 special code 0x00, 0x01. + */ + ssif_bmc->nbytes_processed = 0; + ssif_bmc->block_num = 0; + part->length = MAX_PAYLOAD_PER_TRANSACTION; + part_len = MAX_IPMI_DATA_PER_START_TRANSACTION; + ssif_bmc->remain_len = ssif_bmc->response.len - part_len; + + part->payload[0] = 0x00; /* Start Flag */ + part->payload[1] = 0x01; /* Start Flag */ + + memcpy(&part->payload[2], &ssif_bmc->response.payload[0], part_len); + break; + + case SSIF_IPMI_MULTIPART_READ_MIDDLE: + /* + * IPMI READ Middle or READ End messages can carry up to 31 bytes + * IPMI data plus block number byte. + */ + if (ssif_bmc->remain_len <= MAX_IPMI_DATA_PER_MIDDLE_TRANSACTION) { + /* + * This is READ End message + * Return length is the remaining response data length + * plus block number + * Block number 0xFF is to indicate this is last message + * + */ + /* Clean the buffer */ + memset(&part->payload[0], 0, MAX_PAYLOAD_PER_TRANSACTION); + part->length = ssif_bmc->remain_len + 1; + part_len = ssif_bmc->remain_len; + ssif_bmc->block_num = 0xFF; + part->payload[0] = ssif_bmc->block_num; + } else { + /* + * This is READ Middle message + * Response length is the maximum SMBUS transfer length + * Block number byte is incremented + * Return length is maximum SMBUS transfer length + */ + part->length = MAX_PAYLOAD_PER_TRANSACTION; + part_len = MAX_IPMI_DATA_PER_MIDDLE_TRANSACTION; + part->payload[0] = ssif_bmc->block_num; + ssif_bmc->block_num++; + } + + ssif_bmc->remain_len -= part_len; + memcpy(&part->payload[1], ssif_bmc->response.payload + ssif_bmc->nbytes_processed, + part_len); + break; + + default: + /* Do not expect to go to this case */ + dev_err(&ssif_bmc->client->dev, "%s: Unexpected SMBus command 0x%x\n", + __func__, part->smbus_cmd); + break; + } + + ssif_bmc->nbytes_processed += part_len; +} + +static bool supported_read_cmd(u8 cmd) +{ + if (cmd == SSIF_IPMI_SINGLEPART_READ || + cmd == SSIF_IPMI_MULTIPART_READ_START || + cmd == SSIF_IPMI_MULTIPART_READ_MIDDLE) + return true; + + return false; +} + +static bool supported_write_cmd(u8 cmd) +{ + if (cmd == SSIF_IPMI_SINGLEPART_WRITE || + cmd == SSIF_IPMI_MULTIPART_WRITE_START || + cmd == SSIF_IPMI_MULTIPART_WRITE_MIDDLE || + cmd == SSIF_IPMI_MULTIPART_WRITE_END) + return true; + + return false; +} + +/* Process the IPMI response that will be read by master */ +static void handle_read_processed(struct ssif_bmc_ctx *ssif_bmc, u8 *val) +{ + struct ssif_part_buffer *part = &ssif_bmc->part_buf; + + /* msg_idx start from 0 */ + if (part->index < part->length) + *val = part->payload[part->index]; + else if (part->index == part->length && ssif_bmc->pec_support) + *val = part->pec; + else + *val = 0; + + part->index++; +} + +static void handle_write_received(struct ssif_bmc_ctx *ssif_bmc, u8 *val) +{ + /* + * The msg_idx must be 1 when first enter SSIF_REQ_RECVING state + * And it would never exceeded 36 bytes included the 32 bytes max payload + + * the address + the command + the len and the PEC. + */ + if (ssif_bmc->msg_idx < 1 || ssif_bmc->msg_idx > MAX_TRANSACTION) + return; + + if (ssif_bmc->msg_idx == 1) { + ssif_bmc->part_buf.length = *val; + ssif_bmc->part_buf.index = 0; + } else { + ssif_bmc->part_buf.payload[ssif_bmc->part_buf.index++] = *val; + } + + ssif_bmc->msg_idx++; +} + +static bool validate_request_part(struct ssif_bmc_ctx *ssif_bmc) +{ + struct ssif_part_buffer *part = &ssif_bmc->part_buf; + bool ret = true; + u8 cpec; + u8 addr; + + if (part->index == part->length) { + /* PEC is not included */ + ssif_bmc->pec_support = false; + ret = true; + goto exit; + } + + if (part->index != part->length + 1) { + ret = false; + goto exit; + } + + /* PEC is included */ + ssif_bmc->pec_support = true; + part->pec = part->payload[part->length]; + addr = GET_8BIT_ADDR(ssif_bmc->client->addr); + cpec = i2c_smbus_pec(0, &addr, 1); + cpec = i2c_smbus_pec(cpec, &part->smbus_cmd, 1); + cpec = i2c_smbus_pec(cpec, &part->length, 1); + /* + * As SMBus specification does not allow the length + * (byte count) in the Write-Block protocol to be zero. + * Therefore, it is illegal to have the last Middle + * transaction in the sequence carry 32-byte and have + * a length of ‘0’ in the End transaction. + * But some users may try to use this way and we should + * prevent ssif_bmc driver broken in this case. + */ + if (part->length) + cpec = i2c_smbus_pec(cpec, part->payload, part->length); + + if (cpec != part->pec) + ret = false; + +exit: + return ret; +} + +static void process_request_part(struct ssif_bmc_ctx *ssif_bmc) +{ + struct ssif_part_buffer *part = &ssif_bmc->part_buf; + unsigned int len; + + switch (part->smbus_cmd) { + case SSIF_IPMI_SINGLEPART_WRITE: + /* save the whole part to request*/ + ssif_bmc->request.len = part->length; + memcpy(ssif_bmc->request.payload, part->payload, part->length); + + break; + case SSIF_IPMI_MULTIPART_WRITE_START: + ssif_bmc->request.len = 0; + + fallthrough; + case SSIF_IPMI_MULTIPART_WRITE_MIDDLE: + case SSIF_IPMI_MULTIPART_WRITE_END: + len = ssif_bmc->request.len + part->length; + /* Do the bound check here, not allow the request len exceed 254 bytes */ + if (len > IPMI_SSIF_PAYLOAD_MAX) { + dev_warn(&ssif_bmc->client->dev, + "Warn: Request exceeded 254 bytes, aborting"); + /* Request too long, aborting */ + ssif_bmc->aborting = true; + } else { + memcpy(ssif_bmc->request.payload + ssif_bmc->request.len, + part->payload, part->length); + ssif_bmc->request.len += part->length; + } + break; + default: + /* Do not expect to go to this case */ + dev_err(&ssif_bmc->client->dev, "%s: Unexpected SMBus command 0x%x\n", + __func__, part->smbus_cmd); + break; + } +} + +static void process_smbus_cmd(struct ssif_bmc_ctx *ssif_bmc, u8 *val) +{ + /* SMBUS command can vary (single or multi-part) */ + ssif_bmc->part_buf.smbus_cmd = *val; + ssif_bmc->msg_idx = 1; + memset(&ssif_bmc->part_buf.payload[0], 0, MAX_PAYLOAD_PER_TRANSACTION); + + if (*val == SSIF_IPMI_SINGLEPART_WRITE || *val == SSIF_IPMI_MULTIPART_WRITE_START) { + /* + * The response maybe not come in-time, causing host SSIF driver + * to timeout and resend a new request. In such case check for + * pending response and clear it + */ + if (ssif_bmc->response_in_progress) + complete_response(ssif_bmc); + + /* This is new request, flip aborting flag if set */ + if (ssif_bmc->aborting) + ssif_bmc->aborting = false; + } +} + +static void on_read_requested_event(struct ssif_bmc_ctx *ssif_bmc, u8 *val) +{ + if (ssif_bmc->state == SSIF_READY || + ssif_bmc->state == SSIF_START || + ssif_bmc->state == SSIF_REQ_RECVING || + ssif_bmc->state == SSIF_RES_SENDING) { + dev_warn(&ssif_bmc->client->dev, + "Warn: %s unexpected READ REQUESTED in state=%s\n", + __func__, state_to_string(ssif_bmc->state)); + ssif_bmc->state = SSIF_ABORTING; + *val = 0; + return; + + } else if (ssif_bmc->state == SSIF_SMBUS_CMD) { + if (!supported_read_cmd(ssif_bmc->part_buf.smbus_cmd)) { + dev_warn(&ssif_bmc->client->dev, "Warn: Unknown SMBus read command=0x%x", + ssif_bmc->part_buf.smbus_cmd); + ssif_bmc->aborting = true; + } + + if (ssif_bmc->aborting) + ssif_bmc->state = SSIF_ABORTING; + else + ssif_bmc->state = SSIF_RES_SENDING; + } + + ssif_bmc->msg_idx = 0; + + /* Send 0 if there is nothing to send */ + if (!ssif_bmc->response_in_progress || ssif_bmc->state == SSIF_ABORTING) { + *val = 0; + return; + } + + if (ssif_bmc->is_singlepart_read) + set_singlepart_response_buffer(ssif_bmc); + else + set_multipart_response_buffer(ssif_bmc); + + calculate_response_part_pec(&ssif_bmc->part_buf); + ssif_bmc->part_buf.index = 0; + *val = ssif_bmc->part_buf.length; +} + +static void on_read_processed_event(struct ssif_bmc_ctx *ssif_bmc, u8 *val) +{ + if (ssif_bmc->state == SSIF_READY || + ssif_bmc->state == SSIF_START || + ssif_bmc->state == SSIF_REQ_RECVING || + ssif_bmc->state == SSIF_SMBUS_CMD) { + dev_warn(&ssif_bmc->client->dev, + "Warn: %s unexpected READ PROCESSED in state=%s\n", + __func__, state_to_string(ssif_bmc->state)); + ssif_bmc->state = SSIF_ABORTING; + *val = 0; + return; + } + + /* Send 0 if there is nothing to send */ + if (!ssif_bmc->response_in_progress || ssif_bmc->state == SSIF_ABORTING) { + *val = 0; + return; + } + + handle_read_processed(ssif_bmc, val); +} + +static void on_write_requested_event(struct ssif_bmc_ctx *ssif_bmc, u8 *val) +{ + if (ssif_bmc->state == SSIF_READY || ssif_bmc->state == SSIF_SMBUS_CMD) { + ssif_bmc->state = SSIF_START; + + } else if (ssif_bmc->state == SSIF_START || + ssif_bmc->state == SSIF_REQ_RECVING || + ssif_bmc->state == SSIF_RES_SENDING) { + dev_warn(&ssif_bmc->client->dev, + "Warn: %s unexpected WRITE REQUEST in state=%s\n", + __func__, state_to_string(ssif_bmc->state)); + ssif_bmc->state = SSIF_ABORTING; + return; + } + + ssif_bmc->msg_idx = 0; + ssif_bmc->part_buf.address = *val; +} + +static void on_write_received_event(struct ssif_bmc_ctx *ssif_bmc, u8 *val) +{ + if (ssif_bmc->state == SSIF_READY || + ssif_bmc->state == SSIF_RES_SENDING) { + dev_warn(&ssif_bmc->client->dev, + "Warn: %s unexpected WRITE RECEIVED in state=%s\n", + __func__, state_to_string(ssif_bmc->state)); + ssif_bmc->state = SSIF_ABORTING; + + } else if (ssif_bmc->state == SSIF_START) { + ssif_bmc->state = SSIF_SMBUS_CMD; + + } else if (ssif_bmc->state == SSIF_SMBUS_CMD) { + if (!supported_write_cmd(ssif_bmc->part_buf.smbus_cmd)) { + dev_warn(&ssif_bmc->client->dev, "Warn: Unknown SMBus write command=0x%x", + ssif_bmc->part_buf.smbus_cmd); + ssif_bmc->aborting = true; + } + + if (ssif_bmc->aborting) + ssif_bmc->state = SSIF_ABORTING; + else + ssif_bmc->state = SSIF_REQ_RECVING; + } + + /* This is response sending state */ + if (ssif_bmc->state == SSIF_REQ_RECVING) + handle_write_received(ssif_bmc, val); + else if (ssif_bmc->state == SSIF_SMBUS_CMD) + process_smbus_cmd(ssif_bmc, val); +} + +static void on_stop_event(struct ssif_bmc_ctx *ssif_bmc, u8 *val) +{ + if (ssif_bmc->state == SSIF_READY || + ssif_bmc->state == SSIF_START || + ssif_bmc->state == SSIF_SMBUS_CMD || + ssif_bmc->state == SSIF_ABORTING) { + dev_warn(&ssif_bmc->client->dev, + "Warn: %s unexpected SLAVE STOP in state=%s\n", + __func__, state_to_string(ssif_bmc->state)); + ssif_bmc->state = SSIF_READY; + + } else if (ssif_bmc->state == SSIF_REQ_RECVING) { + if (validate_request_part(ssif_bmc)) { + process_request_part(ssif_bmc); + if (ssif_bmc->part_buf.smbus_cmd == SSIF_IPMI_SINGLEPART_WRITE || + ssif_bmc->part_buf.smbus_cmd == SSIF_IPMI_MULTIPART_WRITE_END) + handle_request(ssif_bmc); + ssif_bmc->state = SSIF_READY; + } else { + /* + * A BMC that receives an invalid request drop the data for the write + * transaction and any further transactions (read or write) until + * the next valid read or write Start transaction is received + */ + dev_err(&ssif_bmc->client->dev, "Error: invalid pec\n"); + ssif_bmc->aborting = true; + } + } else if (ssif_bmc->state == SSIF_RES_SENDING) { + if (ssif_bmc->is_singlepart_read || ssif_bmc->block_num == 0xFF) + /* Invalidate response buffer to denote it is sent */ + complete_response(ssif_bmc); + ssif_bmc->state = SSIF_READY; + } + + /* Reset message index */ + ssif_bmc->msg_idx = 0; +} + +/* + * Callback function to handle I2C slave events + */ +static int ssif_bmc_cb(struct i2c_client *client, enum i2c_slave_event event, u8 *val) +{ + unsigned long flags; + struct ssif_bmc_ctx *ssif_bmc = i2c_get_clientdata(client); + int ret = 0; + + spin_lock_irqsave(&ssif_bmc->lock, flags); + + switch (event) { + case I2C_SLAVE_READ_REQUESTED: + on_read_requested_event(ssif_bmc, val); + break; + + case I2C_SLAVE_WRITE_REQUESTED: + on_write_requested_event(ssif_bmc, val); + break; + + case I2C_SLAVE_READ_PROCESSED: + on_read_processed_event(ssif_bmc, val); + break; + + case I2C_SLAVE_WRITE_RECEIVED: + on_write_received_event(ssif_bmc, val); + break; + + case I2C_SLAVE_STOP: + on_stop_event(ssif_bmc, val); + break; + + default: + dev_warn(&ssif_bmc->client->dev, "Warn: Unknown i2c slave event\n"); + break; + } + + if (!ssif_bmc->aborting && ssif_bmc->busy) + ret = -EBUSY; + + spin_unlock_irqrestore(&ssif_bmc->lock, flags); + + return ret; +} + +static int ssif_bmc_probe(struct i2c_client *client, const struct i2c_device_id *id) +{ + struct ssif_bmc_ctx *ssif_bmc; + int ret; + + ssif_bmc = devm_kzalloc(&client->dev, sizeof(*ssif_bmc), GFP_KERNEL); + if (!ssif_bmc) + return -ENOMEM; + + spin_lock_init(&ssif_bmc->lock); + + init_waitqueue_head(&ssif_bmc->wait_queue); + ssif_bmc->request_available = false; + ssif_bmc->response_in_progress = false; + ssif_bmc->busy = false; + ssif_bmc->response_timer_inited = false; + + /* Register misc device interface */ + ssif_bmc->miscdev.minor = MISC_DYNAMIC_MINOR; + ssif_bmc->miscdev.name = DEVICE_NAME; + ssif_bmc->miscdev.fops = &ssif_bmc_fops; + ssif_bmc->miscdev.parent = &client->dev; + ret = misc_register(&ssif_bmc->miscdev); + if (ret) + goto out; + + ssif_bmc->client = client; + ssif_bmc->client->flags |= I2C_CLIENT_SLAVE; + + /* Register I2C slave */ + i2c_set_clientdata(client, ssif_bmc); + ret = i2c_slave_register(client, ssif_bmc_cb); + if (ret) { + misc_deregister(&ssif_bmc->miscdev); + goto out; + } + + return 0; +out: + devm_kfree(&client->dev, ssif_bmc); + return ret; +} + +static int ssif_bmc_remove(struct i2c_client *client) +{ + struct ssif_bmc_ctx *ssif_bmc = i2c_get_clientdata(client); + + i2c_slave_unregister(client); + misc_deregister(&ssif_bmc->miscdev); + + return 0; +} + +static const struct of_device_id ssif_bmc_match[] = { + { .compatible = "ssif-bmc" }, + { }, +}; +MODULE_DEVICE_TABLE(of, ssif_bmc_match); + +static const struct i2c_device_id ssif_bmc_id[] = { + { DEVICE_NAME, 0 }, + { }, +}; +MODULE_DEVICE_TABLE(i2c, ssif_bmc_id); + +static struct i2c_driver ssif_bmc_driver = { + .driver = { + .name = DEVICE_NAME, + .of_match_table = ssif_bmc_match, + }, + .probe = ssif_bmc_probe, + .remove = ssif_bmc_remove, + .id_table = ssif_bmc_id, +}; + +module_i2c_driver(ssif_bmc_driver); + +MODULE_AUTHOR("Quan Nguyen "); +MODULE_AUTHOR("Chuong Tran "); +MODULE_DESCRIPTION("Linux device driver of the BMC IPMI SSIF interface."); +MODULE_LICENSE("GPL"); diff --git a/include/uapi/linux/ipmi_ssif_bmc.h b/include/uapi/linux/ipmi_ssif_bmc.h new file mode 100644 index 000000000000..8b4f239a61f4 --- /dev/null +++ b/include/uapi/linux/ipmi_ssif_bmc.h @@ -0,0 +1,16 @@ +/* SPDX-License-Identifier: GPL-2.0-only WITH Linux-syscall-note*/ +/* + * Copyright (c) 2022, Ampere Computing LLC. + */ + +#ifndef _UAPI_LINUX_IPMI_SSIF_BMC_H +#define _UAPI_LINUX_IPMI_SSIF_BMC_H + +/* Max length of ipmi ssif message Included netfn and cmd field */ +#define IPMI_SSIF_PAYLOAD_MAX 254 +struct ipmi_ssif_msg { + unsigned int len; + __u8 payload[IPMI_SSIF_PAYLOAD_MAX]; +}; + +#endif /* _UAPI_LINUX_IPMI_SSIF_BMC_H */ From patchwork Wed Jun 15 09:02:58 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Quan Nguyen X-Patchwork-Id: 1643537 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=os.amperecomputing.com header.i=@os.amperecomputing.com header.a=rsa-sha256 header.s=selector2 header.b=h/QI2OXR; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=vger.kernel.org (client-ip=2620:137:e000::1:20; helo=out1.vger.email; envelope-from=linux-i2c-owner@vger.kernel.org; receiver=) Received: from out1.vger.email (out1.vger.email [IPv6:2620:137:e000::1:20]) by bilbo.ozlabs.org (Postfix) with ESMTP id 4LNK9M2rxRz9vG6 for ; Wed, 15 Jun 2022 19:03:51 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1347011AbiFOJDt (ORCPT ); Wed, 15 Jun 2022 05:03:49 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41786 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238912AbiFOJDl (ORCPT ); Wed, 15 Jun 2022 05:03:41 -0400 Received: from NAM04-MW2-obe.outbound.protection.outlook.com (mail-mw2nam04on2135.outbound.protection.outlook.com [40.107.101.135]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 450E5369DF; Wed, 15 Jun 2022 02:03:40 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=BsXi5Ga6KRp/VtFG7UzBz/g3wyBM+qnLTpKZ3lfH/HUXdlWjwuG+wAAZa5A5s4r7ZzE+8mHhcgLXCe+IP65ncu3CISir3Br2+ZtCKZLicXw7p1DIMFfhq6mi67dy2nOKWl3azQ19RqYwqyblavsgj7EN5mXxzXm1PqVjJhk47q/cI80z2R9C1/VtS9d+Oenn5YmhS1a9WSr1D9hdaA7V7lgYOYvfCPig17cMvmXazow5NhfbniY94ntmLJwWo/VGaG0vTOt9VF1rEODccPdALPeDcdaqkoQTXVRQ0ZUm8l3Od6pqxnReGCNuIMCgTyUy/LKm3vgEyhiWqB/ZtHitCg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=wkjq0qshKkkEnrp6tWg9ZoVy1g9YcPkZ/ZuHSoon950=; b=QPAgaxb+bufbzgr/PgVGkmg/BWbZyEKJC4DHCm/Sy0GilRoLojYNx1dJdnLhgHkWCX0LLaA7xcLXT6FsNKTfmjFWMlz8JFnq6vvlQVoFoyTkoewgf1Rz4l5jl8NF4a+1o2joifNjKNtycdzLoMmXEznOS9nthGMf4GSl41NwSQao8L5+K+q9HrJLp1KAyAOBY5Um2uJ0jHFU40Ymoy1bExA3lteRiyiSlbw3tGeczHeRdF9ySw047bCMm+mywp/oVcPgFug9KTS1c2fd1bnNG+Y5UNYrDvhR01+FLk18OFYvtAlNFlzbEIrhDyoaAVnVHcLZcH6h8ppC9tPcCk3KJw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=os.amperecomputing.com; dmarc=pass action=none header.from=os.amperecomputing.com; dkim=pass header.d=os.amperecomputing.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=os.amperecomputing.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=wkjq0qshKkkEnrp6tWg9ZoVy1g9YcPkZ/ZuHSoon950=; b=h/QI2OXRlMa4XRgQ3oUi6qE5GkBLXpt0ysS9/7VU6O3a7cPxo85oPgvEET3txsZgED1BdbIi6jHmcDdXOPN8Y+oVucWbDJvU+2WU3+AUttFFTZG7oaiNwvwCCb15aOIRSqJLEmjTFGGJCQNxPFGGk8YA0NpN4g5LUWFcVCkeVOs= Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=os.amperecomputing.com; Received: from SJ0PR01MB7282.prod.exchangelabs.com (2603:10b6:a03:3f2::24) by BN0PR01MB6909.prod.exchangelabs.com (2603:10b6:408:166::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5332.13; Wed, 15 Jun 2022 09:03:36 +0000 Received: from SJ0PR01MB7282.prod.exchangelabs.com ([fe80::7535:773:f979:893e]) by SJ0PR01MB7282.prod.exchangelabs.com ([fe80::7535:773:f979:893e%7]) with mapi id 15.20.5332.020; Wed, 15 Jun 2022 09:03:36 +0000 From: Quan Nguyen To: Quan Nguyen , Corey Minyard , Rob Herring , Krzysztof Kozlowski , Brendan Higgins , Benjamin Herrenschmidt , Joel Stanley , Andrew Jeffery , Wolfram Sang , openipmi-developer@lists.sourceforge.net, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, linux-i2c@vger.kernel.org, openbmc@lists.ozlabs.org, linux-arm-kernel@lists.infradead.org, linux-aspeed@lists.ozlabs.org Cc: Open Source Submission , Phong Vo , "Thang Q . Nguyen" Subject: [PATCH v8 2/3] bindings: ipmi: Add binding for SSIF BMC driver Date: Wed, 15 Jun 2022 16:02:58 +0700 Message-Id: <20220615090259.1121405-3-quan@os.amperecomputing.com> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220615090259.1121405-1-quan@os.amperecomputing.com> References: <20220615090259.1121405-1-quan@os.amperecomputing.com> X-ClientProxiedBy: SG2PR06CA0189.apcprd06.prod.outlook.com (2603:1096:4:1::21) To SJ0PR01MB7282.prod.exchangelabs.com (2603:10b6:a03:3f2::24) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: e3ba8358-1e2f-4efb-1335-08da4eadee42 X-MS-TrafficTypeDiagnostic: BN0PR01MB6909:EE_ X-Microsoft-Antispam-PRVS: X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: YjdQnxq8ml3RVcqgdJAyMuI9VbtNl9TcaFSU8NqJRYGn/VZPh8vxF8WilAfBFsbrpk0/6VQn84mDYdgovrjMCnhDsDvJzBOoZnihGlqO9MnWQxWWzqCFRQTevJaEqDJkH9UGawRN7l0xiDCAw67Uz4GuSJxtaGF+7gYYE6YyVW+C8Z4IPpOlTYiLL0Pr0OR+6ryiHt4G+PKAEazvBDthKe6AEvKgUs7VbiTDilFNYbkJFmUPVuQKSsLup+sdhdfOLWN3Tjj2/cIWKD5H5jMKseOq6gj7NEV7qRDQI5WK7vwZe+7aJF8QVuKT8MPXHmnoHwrNYWo37tPxc/LeLJrVhy74lnWycmBtz8PyRUhTu+amIeLqIQBIZhqVyZI3RLWLtCzh/0vfHFEpJhkP5lMduGCqabifaYnXoe67vZnlbeEQ7BSjFfQ0muVezc/ur2GDrzq9Afyeg8/P2uhe+X7OYYR66pY5GZ9erMC2gitgNza1kIh8GL7fWVeKQnxO+woVQNB+R+HyTV25G43oa5ezth4p/SBt17E15rqwkbWCEnyBJ17RQmR6w7owQW+L36ZUQWZV+jJPo3+mUURGw9KOp7Y9p/p9QlRPygFWrymr/KEu0UjVfSlaMDsBtG4qQ1wN3XGX4Yxccc1T5bSzt8uIz2GKvM2HqLtxBrxTua3TaL4DTFswGwnWonJCj72ikdUIYnUoct3rkzy+I00qpSvwjkQlbnWvLlNNf2OjcVjf0CM9HmgIbGIDX5WCri5cZEeGjVzgC25fWziSNPhIscaTxjCm1knlY3577MaMfI/lqpY9gITHqHnWVBdAYDDPMPqfDvtnE27VfygC4N4FJpzEMqIk11EftWyfcF9E7k0VX2Y= X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:SJ0PR01MB7282.prod.exchangelabs.com;PTR:;CAT:NONE;SFS:(13230016)(4636009)(366004)(110136005)(6486002)(966005)(508600001)(2906002)(7416002)(6666004)(8936002)(186003)(316002)(54906003)(4326008)(5660300002)(66946007)(66556008)(66476007)(8676002)(38100700002)(52116002)(86362001)(6512007)(921005)(1076003)(107886003)(6506007)(2616005)(38350700002)(26005)(83380400001);DIR:OUT;SFP:1102; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: diS24eXfVXIBHORkMT761Ix6uXIQ8+RyNaDUV9m7v3vnb89dmX1KwxenBiDXZG/SOsVRBtcAyI6rd/RXJPIzI/8XCxNVI8CwSAEYrAP/5ESTnEb7WHsSda6ZN2ttTMDmHQwmOZ+8UDRuXbtaqq2GOyCLzXWsGm+BtsYhcBfD3OV+5tk78gtAiDWGkI4AB+u12GZg9a1PdF5fPk520DtRQdmyYCpLyu0+IUATJNvN2xSofCwE656S3DbpjjlQkvtavpgiHivb/WDoCVz2LJOzzoFzfrN2Y4pSqNt9JyQbkoXaDbSsHceurHTr29f6pYu4zMoWKF3VEbbxxJAPrRGmBwjTlCYBRAVuPD2c5n6BiTuqkBkRiknIetGabp7NHYuVdzOJUxecigDzoRiO/Sxu1uM6I0+QHZCTAn+BGp2/odMx6kVKiTVSzUjvHul8tlt6juCdmo9XqlKkHtVXo8hSFSX3xNxJCPwrJXQHBTZZupUc9nXUHb54gqUrgh8vyrjQQS0cc8ombeU6qFHt3URio2nIfZjbYSa2QXYQqxii/7yKpp02Y/VLWcf5OGeIIKVbaih6FiqydtZa2qIHj24zFg+WvKSngGwU8Cj0QUlRzTJjsoxkcvqcjIypQ5mUj+sS5DEtTgjpfgfCtc6I4tXokvIZk6Mrj9focb2DaDYQwxSNQTVN7xSqcdx1GkACkvARB0frdJQqvwXWDDe9QAXmYKWZYcNX6zW86n8Hf8bI0m/CA4WSe0PuN3F3w4qHu7LhHFOQIM4NsuWUr9aWJAM09OCoXc+8ujlX69KPzHqj7mwzoSUdegNqJ4gretdTIWzdg29K6ACCe+1DZxzXblfdUWcJSNM2C79FXsXPZAft+JNjxN1ZUO3sanuaKphjzRMzucB+FX5PxXHc9IFHGsDetR476iY3rjzSA7vVpTWHZtLE1bw78xiKT3a2QmytRgpQvV17p3HHzKaX2xSVzYtxedGqana/5nWF3rzcHhWi6PKG2MfokEgKEHjUPrXmin/dXr2OV30dJUDlV8GtDCMFCQEmG/ruV6CKUYdf3dlRzf+l3Qsd/5n9k7qDz7UxusTZqDqzmBcEuJ81px23rcpGap5gyH/CWPhLm7ufPqAvrHuo50uxfXk2JFoBluBKE7rvk1JPTLz1ugXgYW4t4+mtez3FD7vAyL0t4vu4+PQ+ZasnJx0SZ/oAx4p2bgGty3LcQIvkzc9XSZj4iwe3U07q5I/xKjcQcvxiUoAWEOUK6PLwKryXn6WUtEb2tgeztRAA8VeFNTxxxRIVPtC8DsyvGP3/Oww3eG/9EmxGt2OgN8itMdiYVpTbKFX9wBPgt9paqcJwm+p0rupLrkFF5/sJhH/gbGMNc4h48vxhUmv+0aNb0X46L7w3vaKIcc1WChLiad4y5nCecQs1cG//uZijy6dXR6VLqus5gOru4GS1FDDGiQKucuvtHUCz9pXvVbqr7GR/6cHRR1jzKABWHdW93orQ3GqSTGVC6tyn8Oh/iE3Q2CVyJm9TaoDmTz7I4Ugo7wvH1/+FoI6mCWHr6AQKzUGuqEmebCKITCkxl0h1+JbZ5yBHZOwz9DVWYhR4r6VAsuUCRGwCnLFOJkd/XWg6YGrr7stKrcAYEapa7ypDW8J/EV/fy65/DQBvTJo3g0QOTDBrLkyJwPC5kQYkWalMWmNqmEug0ORzs5erzYz7UQwoxx5eNrca7re7Vl7a3U4PQPkKz8uYdCfJM7nFem1Zj8ZqBHB/nv4n9GMszKL7kfzfw7EyLIneRkaRsH5Gu8pM X-OriginatorOrg: os.amperecomputing.com X-MS-Exchange-CrossTenant-Network-Message-Id: e3ba8358-1e2f-4efb-1335-08da4eadee42 X-MS-Exchange-CrossTenant-AuthSource: SJ0PR01MB7282.prod.exchangelabs.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 15 Jun 2022 09:03:36.4024 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 3bc2b170-fd94-476d-b0ce-4229bdc904a7 X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: ycb8JCP8vX7wh0RVKQxLqoV0BbHom2mraQn0vripKwxHhD4CLqUCEYyeVL1YUoZszT0OxvVSJ/uFEBJ5xmVRfob39UPLgyEehWYbs9YK9ZM= X-MS-Exchange-Transport-CrossTenantHeadersStamped: BN0PR01MB6909 X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2, SPF_HELO_PASS,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-i2c@vger.kernel.org Add device tree binding document for the SSIF BMC driver. Signed-off-by: Quan Nguyen --- v8: + None v7: + Change compatible string from "ampere,ssif-bmc" to "ssif-bmc" [Jae] v6: + None v5: + None v4: + Fix warning with dt_binding_check [Rob] + Change aspeed-ssif-bmc.yaml to ssif-bmc.yaml [Quan] v3: + Switched to use DT schema format [Rob] v2: + None .../devicetree/bindings/ipmi/ssif-bmc.yaml | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 Documentation/devicetree/bindings/ipmi/ssif-bmc.yaml diff --git a/Documentation/devicetree/bindings/ipmi/ssif-bmc.yaml b/Documentation/devicetree/bindings/ipmi/ssif-bmc.yaml new file mode 100644 index 000000000000..02b662d780bb --- /dev/null +++ b/Documentation/devicetree/bindings/ipmi/ssif-bmc.yaml @@ -0,0 +1,38 @@ +# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/ipmi/ssif-bmc.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: SSIF IPMI BMC interface + +description: SSIF IPMI BMC device bindings + +maintainers: + - Quan Nguyen + +properties: + compatible: + enum: + - ssif-bmc + + reg: + maxItems: 1 + +required: + - compatible + - reg + +additionalProperties: false + +examples: + - | + i2c { + #address-cells = <1>; + #size-cells = <0>; + + ssif-bmc@10 { + compatible = "ssif-bmc"; + reg = <0x10>; + }; + }; From patchwork Wed Jun 15 09:02:59 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Quan Nguyen X-Patchwork-Id: 1643538 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=os.amperecomputing.com header.i=@os.amperecomputing.com header.a=rsa-sha256 header.s=selector2 header.b=rEZh3U14; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=vger.kernel.org (client-ip=2620:137:e000::1:20; helo=out1.vger.email; envelope-from=linux-i2c-owner@vger.kernel.org; receiver=) Received: from out1.vger.email (out1.vger.email [IPv6:2620:137:e000::1:20]) by bilbo.ozlabs.org (Postfix) with ESMTP id 4LNK9P57BVz9sG0 for ; Wed, 15 Jun 2022 19:03:53 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1347063AbiFOJDv (ORCPT ); Wed, 15 Jun 2022 05:03:51 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42048 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1347035AbiFOJDu (ORCPT ); Wed, 15 Jun 2022 05:03:50 -0400 Received: from NAM12-BN8-obe.outbound.protection.outlook.com (mail-bn8nam12on2122.outbound.protection.outlook.com [40.107.237.122]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D00DE38BC4; Wed, 15 Jun 2022 02:03:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=MeaAFKx8KsTxyAtT/rlemxx0LuzSjrNpGj8VzPBSiX0rPVtE0L2LHcFrFxz+WhtRMk1MrOQiHQPRzOf5sGTRP2qQsQhVCgrYGnAFVl4Nsp+zQuuKBdUaFqVunceDzMFtTrNSMCnerjB+AEwBbRjwS3c5sX8i8fZ3KL1E/0zB540/y19fs2/DKb3YRFCFDdASOqvSz8TL/7lsvQ4uV2OoRe1jvCTzT/bwooBqPWaHKLgJ1rZISgqzJoIZRJWTP6vgqeLwWNXwfmohchuFtDMT94lP0pIPkaK6/bt0IYww+2AeHIl+Vuq2rzevXUk5rUU8DuEnyaKXqOWhVDxHy4quOw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=yY+JGXPT07FYHFt/DZ+nLA+W08wbKn7f4XO1Mu0iF0E=; b=N731RStz6bgBX0RdQY5MdQDDhUy0x01AtsCm3NDuIarkn5Tk9IqRZn2RD4OmHXC16Vqexu3mSiXJVLGcT+pp0t4jLrg6HD+5Y6KzcccG7I9ka3u/mTiMMcHQMT7UOF8KoRcyr0s9qrSb/rtXs1uMZRQ/SCoMBZhcxZfRsPARvYoYrONR/K0xZUH4UwybtwTcYOpj7N6tw73jZEbhNmKcwNoptVeCHrqOe6bwMvLXFIz8rrioPK6lG3oJg5NyKQBhpZ8dau5clCrKMuoyhXA1KZq0YUHNwv9rvcwpE4+VHALTeWNKBNy72Y1HjZH8YqPGE2uXVrdXdleqXWYRcYQC4g== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=os.amperecomputing.com; dmarc=pass action=none header.from=os.amperecomputing.com; dkim=pass header.d=os.amperecomputing.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=os.amperecomputing.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=yY+JGXPT07FYHFt/DZ+nLA+W08wbKn7f4XO1Mu0iF0E=; b=rEZh3U14eaaorob5C55RA/ERxesr4ki4roZo6pnG1Cs/s4igKpkG1ASXmCaaKz4tlc1IAZosRGvseW9b5AbhhTBDpI7hxaicX3oKHdhJgOoGY2y//qgsV6p9txN+69KjEdvslXaRwjAbFdaXbRshZlQyvJ/lQGWgkKrP6YoMvNk= Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=os.amperecomputing.com; Received: from SJ0PR01MB7282.prod.exchangelabs.com (2603:10b6:a03:3f2::24) by BY5PR01MB6049.prod.exchangelabs.com (2603:10b6:a03:1bd::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5332.12; Wed, 15 Jun 2022 09:03:42 +0000 Received: from SJ0PR01MB7282.prod.exchangelabs.com ([fe80::7535:773:f979:893e]) by SJ0PR01MB7282.prod.exchangelabs.com ([fe80::7535:773:f979:893e%7]) with mapi id 15.20.5332.020; Wed, 15 Jun 2022 09:03:42 +0000 From: Quan Nguyen To: Quan Nguyen , Corey Minyard , Rob Herring , Krzysztof Kozlowski , Brendan Higgins , Benjamin Herrenschmidt , Joel Stanley , Andrew Jeffery , Wolfram Sang , openipmi-developer@lists.sourceforge.net, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, linux-i2c@vger.kernel.org, openbmc@lists.ozlabs.org, linux-arm-kernel@lists.infradead.org, linux-aspeed@lists.ozlabs.org Cc: Open Source Submission , Phong Vo , "Thang Q . Nguyen" Subject: [PATCH v8 3/3] i2c: aspeed: Assert NAK when slave is busy Date: Wed, 15 Jun 2022 16:02:59 +0700 Message-Id: <20220615090259.1121405-4-quan@os.amperecomputing.com> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220615090259.1121405-1-quan@os.amperecomputing.com> References: <20220615090259.1121405-1-quan@os.amperecomputing.com> X-ClientProxiedBy: SG2PR06CA0189.apcprd06.prod.outlook.com (2603:1096:4:1::21) To SJ0PR01MB7282.prod.exchangelabs.com (2603:10b6:a03:3f2::24) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: ba968699-d8d6-4ff9-4d5c-08da4eadf120 X-MS-TrafficTypeDiagnostic: BY5PR01MB6049:EE_ X-Microsoft-Antispam-PRVS: X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: zGKtg09K2Ga+07FilLNmiWGZi5Fue4wQ277TV9JyI11B5ePC9gLhFMULVnKkrUlJSs0cnI5ScBeewqAylQuuq+jr+M4VrHjIjI3NocYRWKPVFqYTF4GHW6yvf1bVpPO4zk5iMG0h2enqgTxX780czV5Jgr5hSZHidlA31Y3aY6cTt3yTQlA2QNBj6oGCK747Lm1vF8b0v16EoT/I16yyp2gRE44PLP9pjynoeGxUn1UeDDgIoaqdP7n1qfglbeGIozBgpcilroh+lJVFGE5a1+IGr2J1QaSJhpJRAeTXnYRQmmj2ubzU/8jvZCg5uKjRQvEICyjyuu1A0JKaVQVfvn2FePNho46cWVBgX+9EtakC/CEiEZuggF4kG91zt5rBEmPR17cVQWQVM0fIOeJibsPmq1PMyEdZxLIjHY7vF7M8vqxOpyCocR+8BFwsCn6lIGGUWLh3MmBeBjx1hUiRU4LjDbWZJS1myoX/iAQ4qlEAhVOXYKLbY5yyvKGyAawTYruZ70HWOOlIktRyFjB6YDhbDWpPkdbatITIrAuh0osFxHIRoMMZzP/amDl0wZ68Lb5yjSzwY5dZCOE9Nf+xrg+JcQZe1J1XtGt5p8YJr1XxffNMfCNbwXikVgQpAV/zKlRuSjR0w8MDmp0nYFgEBLhpNpHKpblqVCPk3qOELEjtcJzT8IuBwIICBjOsD291GWTZZmIeVKNftLatthx3b0UkBM3RLi6306i9OSBEVRQ= X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:SJ0PR01MB7282.prod.exchangelabs.com;PTR:;CAT:NONE;SFS:(13230016)(4636009)(366004)(66556008)(66476007)(6666004)(508600001)(4326008)(83380400001)(38350700002)(8676002)(1076003)(2616005)(6486002)(316002)(107886003)(52116002)(8936002)(186003)(66946007)(6506007)(5660300002)(2906002)(6512007)(110136005)(26005)(54906003)(38100700002)(86362001)(921005)(7416002);DIR:OUT;SFP:1102; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: Zcci+qM+7J2sVgi3h54Oih7Oq+rN5lVdzvUOr/kyWCarCse62Di1djmOM1iCifaNrX9uPR6iMIF9/MAMEC57BOEK6towWofbJ5cBDRmnis9etiUxyyiVumETLzbB6wbSY0mexH7fS9dMd9wHR8rXCXBE0z1TtzJdVulus7v/upEshzJrqTCO4cy7NlcqZI6CUnLNJ8yn6I59Ul9NHr8Z/d6MOhNph/g6T4G4UZfRYTcwFYep0QaXhU8oafmlE4V3EUx0b2chSEbDmZUSVyGHURyZCfE9Alne+tv6E+cx42z2qImWqlopt1XNcvM1zPOzKkRQslf8+AQnMmX0RbjR6Y5MFxqY/cnbPaL9DpcCL6QYWuzbr9BvDtlQvp13P//tZ6dE9/UjVJnteHI6Kh5Ud8z73vhQXFqDxyBgSsu2b6PxvTji083cVrklqcQFUWDFQSBA5EeGMt2CA4BmaiyZ7QgqR7gBca8RqHRliE0qh5ItkI4PPkydcfp6r5IBj26wJwW57qQ2ZKsUY5qfqEpDCsZIBaJu0lI5rM+jKs/oWri81S3eKwva7qQBh0LxIq3ROoWHOGqSv+nvaUgkxg/Ti+fOyGdixSR2653HoC2GGd58vZdnL2xxY+5xBjlRDPmgfOqigw0idwsGp6JqIgdZK75ftgxnookqTCPsOIvH2EwAQBOPlqvMYoME+R5+3zeEnEJO8/zV7r/CSvVp/kZk6Kml5vNOhYoczOuJ1osv3iAc4RF7vpaejUzlt4DZ9ON07jQbjF+KXlPeHIBHIFaPFiM9F4v+od6EtbfRsJIs5YS0k3+dERVz46MtdOZjV4VsUfkXREnNRLtWv+LZZOFckf9atTNEhTD3Oy9iLcWgWnJx+qq6I/fciGEd3Ko1tbKoQo6SYwKPyZlUXQf/2AW+Fhr/MXnIzA+wBuO+XqwWwSAyTWEwsUqcZHqlKdivTxMUHVWqs3CumbUMyTF6+i54k2BssqeRcasUMI2PcnkqhxCUyxWHNSxm+SUEQIsCPjubgL7PsjhjA39SIv5ClFa0qVHkiKvbCz7ihmDNi7vWjqtmAAe97CUIie5whMj92KrL2/UlsT93aGjtY3WQRdpyJ/LpJFSnI1T4Asay1Gly1jqbNuMPwxEUJikipQqtcdCunm3QhSxJbJo6b/hVPXQ/G63jRDjgppYDEKqH8GUcPPiWkeSRAmP+NOtyV4lfDZYaoTEeC2fbAqdIkmWHMqYnPAM3S9OirZSrbivmiHpOanbT3OGNbF1F1uEg72coxRuGl98yN6N+1t1VVAtEM7YG+W+d2TMgIOLzAoWnw5C6NlcOrbU94khQSKnw7pUXdcZjGFZWvhb7ENbeex99C0YIurZmtf41IeREC4gAeyIfLXpH1Eu7FV/UM3LJssWxa9a2yXKI4BtSVJsCiXJuf9WztcL/ZNGcm7PIFhczAa6PnC50blJ6EgMfqqa0I20PxnnbgkY6zik6lLFqe+euwz+ZSySO3+oYYnrwSGoKPoDwPGMg8vbKbXze5dBtdXCIRfwxz0/W2XM+aTjqfsGwLadFZHTuosz7zCmn8Mja2nDii5RZap/1aiR/DndGAeJpn5StyI+n477BLiXJvCwS0LGNGoN9hJbDF8wtqF3VetwObQUZJTEdBwbC2MRwDlslfQjWy21dgR+1eqTaQGY7Ztky2l0HDe3JkNfPABaZqBNoXG7pQ6yd/mvgy0Mf0G0B8LUPwEi97J5ep4W0OI7hGAqYKzoC+ceN5lDRrCfVmMLwrgJjJCSZcwvlsJleN7HW7ZEa X-OriginatorOrg: os.amperecomputing.com X-MS-Exchange-CrossTenant-Network-Message-Id: ba968699-d8d6-4ff9-4d5c-08da4eadf120 X-MS-Exchange-CrossTenant-AuthSource: SJ0PR01MB7282.prod.exchangelabs.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 15 Jun 2022 09:03:42.0110 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 3bc2b170-fd94-476d-b0ce-4229bdc904a7 X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: +4LE9NuCeXTGzIR8NWYgChCT08uhXibQpey3CXP7Rdo/+Ra1PGN8vhNraZZiRstATqkvM4WfrjRnbnvxERQfTuwpw692UfJdazTaetsv2MU= X-MS-Exchange-Transport-CrossTenantHeadersStamped: BY5PR01MB6049 X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2, SPF_HELO_PASS,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-i2c@vger.kernel.org When processing I2C_SLAVE_WRITE_REQUESTED event, if slave returns -EBUSY, i2c controller should issue RxCmdLast command to assert NAK on the bus. Signed-off-by: Quan Nguyen --- v7 -> v8: + None v6: + New introduced in v6 [Quan] drivers/i2c/busses/i2c-aspeed.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/drivers/i2c/busses/i2c-aspeed.c b/drivers/i2c/busses/i2c-aspeed.c index 771e53d3d197..ebc2b92656c8 100644 --- a/drivers/i2c/busses/i2c-aspeed.c +++ b/drivers/i2c/busses/i2c-aspeed.c @@ -244,6 +244,7 @@ static u32 aspeed_i2c_slave_irq(struct aspeed_i2c_bus *bus, u32 irq_status) u32 command, irq_handled = 0; struct i2c_client *slave = bus->slave; u8 value; + int ret; if (!slave) return 0; @@ -311,7 +312,9 @@ static u32 aspeed_i2c_slave_irq(struct aspeed_i2c_bus *bus, u32 irq_status) break; case ASPEED_I2C_SLAVE_WRITE_REQUESTED: bus->slave_state = ASPEED_I2C_SLAVE_WRITE_RECEIVED; - i2c_slave_event(slave, I2C_SLAVE_WRITE_REQUESTED, &value); + ret = i2c_slave_event(slave, I2C_SLAVE_WRITE_REQUESTED, &value); + if (ret == -EBUSY) + writel(ASPEED_I2CD_M_S_RX_CMD_LAST, bus->base + ASPEED_I2C_CMD_REG); break; case ASPEED_I2C_SLAVE_WRITE_RECEIVED: i2c_slave_event(slave, I2C_SLAVE_WRITE_RECEIVED, &value);