From patchwork Fri Oct 6 10:02:12 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Konstantin Aladyshev X-Patchwork-Id: 13411229 Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net [23.128.96.19]) (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 D6E5814A98 for ; Fri, 6 Oct 2023 10:02:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="ef0R/znv" Received: from mail-lf1-x12b.google.com (mail-lf1-x12b.google.com [IPv6:2a00:1450:4864:20::12b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6C39FD6; Fri, 6 Oct 2023 03:02:24 -0700 (PDT) Received: by mail-lf1-x12b.google.com with SMTP id 2adb3069b0e04-5045cb9c091so2562036e87.3; Fri, 06 Oct 2023 03:02:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1696586542; x=1697191342; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=YUX/UAbHIqbeqOjhPaMPY2ZesCBPS6XxII45ueUUUxU=; b=ef0R/znvz3elA2Z4601Ks/oieeC58Yj7zcI0rBLZgH52X/Uziwr3HKjuZr8y2zTZk/ K8WgbBfe8kTNImjBlylmtDTi44mbgXTTFpfzq9c+81MPT+gkYF+fBIV0+vAL0O+GXEXr cpbkJjINZfXt2vJ3eu0e0bQVbjWjAwAgego1BZIP0zooPiBCnn8StxDZhwix+RFLO6wW /gA6AGJ10anFI9wbk33EqPH8TpSPpD3JukBdzg1bUMxB6fYoCif4IeEaKF7p0CrVJ2Dy Bs+k7kx+aw1X+VE9Tqt2aFybKan39gAj/82jqeB8k3+1wQZdLKgLonU+LTjxKpaLzvX8 z2Xw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1696586542; x=1697191342; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=YUX/UAbHIqbeqOjhPaMPY2ZesCBPS6XxII45ueUUUxU=; b=FYelmPhzNIv+ZKgKKVS65fAsxeZVZ0vNR59VE6cspYAehDBKmAi1yhvUTJy5oRqLJT tFHQNdPkBfZGiPROR83gmhXi8oKoroXUU/g97Qve3CPka2UzCoSyzoGClvbn2byarcqu H06xfst9nmcPTxBywl+vJoYRYZ8qeI+T0yN1qFBsReBNyiePUtcm99Zale9GU4HP+v0T ljt2LZyIswOvJ4F4eJYy3+mpAv5nJ+t3QLLFw5WuJpa4HfZSG2LtgLg4PG6o9dMQ1Af8 lCT26wYROmcvqb+x4WdCO3G8PxGdERAFwjq8BJAsLtkYG3q9xb4LKWlGPSMDYzeWWKqU wOwg== X-Gm-Message-State: AOJu0Ywqjh67Q+YBoAEWXaQDxes1nnS+qPBDX77d4KQs2Sxdy3KrGM47 naTWrSAsMFo0lquGuufwRck= X-Google-Smtp-Source: AGHT+IFjrzT7dsTckMv9vrji9YO0ch4tOuJBrePWI1iOFYJBSSCiTqAdVoJYQCbiuwXpZ76ptkaXnw== X-Received: by 2002:a2e:8445:0:b0:2c0:3186:8efa with SMTP id u5-20020a2e8445000000b002c031868efamr5697429ljh.11.1696586542330; Fri, 06 Oct 2023 03:02:22 -0700 (PDT) Received: from PC10319.67 ([82.97.198.254]) by smtp.googlemail.com with ESMTPSA id x12-20020a2e9dcc000000b002bcedacd726sm713674ljj.25.2023.10.06.03.02.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 06 Oct 2023 03:02:21 -0700 (PDT) From: Konstantin Aladyshev To: Cc: minyard@acm.org, joel@jms.id.au, andrew@aj.id.au, avifishman70@gmail.com, tmaimon77@gmail.com, tali.perry1@gmail.com, venture@google.com, yuenn@google.com, benjaminfair@google.com, aladyshev22@gmail.com, jk@codeconstruct.com.au, matt@codeconstruct.com.au, davem@davemloft.net, edumazet@google.com, kuba@kernel.org, pabeni@redhat.com, openipmi-developer@lists.sourceforge.net, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-aspeed@lists.ozlabs.org, openbmc@lists.ozlabs.org, netdev@vger.kernel.org Subject: [PATCH v4 1/3] ipmi: Move KCS headers to common include folder Date: Fri, 6 Oct 2023 13:02:12 +0300 Message-Id: <20231006100214.396-2-aladyshev22@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20231006100214.396-1-aladyshev22@gmail.com> References: <20231006100214.396-1-aladyshev22@gmail.com> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Spam-Status: No, score=-1.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_ENVFROM_END_DIGIT, FREEMAIL_FROM,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net The current KCS header files can be utilized by both IPMI drivers (drivers/char/ipmi) and MCTP driver (drivers/net/mctp). To be able to use them in both cases move the headers to 'include/linux' folder. Signed-off-by: Konstantin Aladyshev --- drivers/char/ipmi/kcs_bmc.c | 8 +++----- drivers/char/ipmi/kcs_bmc_aspeed.c | 3 +-- drivers/char/ipmi/kcs_bmc_cdev_ipmi.c | 2 +- drivers/char/ipmi/kcs_bmc_npcm7xx.c | 2 +- drivers/char/ipmi/kcs_bmc_serio.c | 2 +- {drivers/char/ipmi => include/linux}/kcs_bmc.h | 0 {drivers/char/ipmi => include/linux}/kcs_bmc_client.h | 3 +-- {drivers/char/ipmi => include/linux}/kcs_bmc_device.h | 3 +-- 8 files changed, 9 insertions(+), 14 deletions(-) rename {drivers/char/ipmi => include/linux}/kcs_bmc.h (100%) rename {drivers/char/ipmi => include/linux}/kcs_bmc_client.h (97%) rename {drivers/char/ipmi => include/linux}/kcs_bmc_device.h (96%) diff --git a/drivers/char/ipmi/kcs_bmc.c b/drivers/char/ipmi/kcs_bmc.c index 8b1161d5194a..d29a8505d6ed 100644 --- a/drivers/char/ipmi/kcs_bmc.c +++ b/drivers/char/ipmi/kcs_bmc.c @@ -5,15 +5,13 @@ */ #include +#include +#include +#include #include #include #include -#include "kcs_bmc.h" - -/* Implement both the device and client interfaces here */ -#include "kcs_bmc_device.h" -#include "kcs_bmc_client.h" /* Record registered devices and drivers */ static DEFINE_MUTEX(kcs_bmc_lock); diff --git a/drivers/char/ipmi/kcs_bmc_aspeed.c b/drivers/char/ipmi/kcs_bmc_aspeed.c index 72640da55380..3dc0dfb448f5 100644 --- a/drivers/char/ipmi/kcs_bmc_aspeed.c +++ b/drivers/char/ipmi/kcs_bmc_aspeed.c @@ -10,6 +10,7 @@ #include #include #include +#include #include #include #include @@ -21,8 +22,6 @@ #include #include -#include "kcs_bmc_device.h" - #define DEVICE_NAME "ast-kcs-bmc" diff --git a/drivers/char/ipmi/kcs_bmc_cdev_ipmi.c b/drivers/char/ipmi/kcs_bmc_cdev_ipmi.c index cf670e891966..bf1001130a6c 100644 --- a/drivers/char/ipmi/kcs_bmc_cdev_ipmi.c +++ b/drivers/char/ipmi/kcs_bmc_cdev_ipmi.c @@ -8,6 +8,7 @@ #include #include #include +#include #include #include #include @@ -17,7 +18,6 @@ #include #include -#include "kcs_bmc_client.h" /* Different phases of the KCS BMC module. * KCS_PHASE_IDLE: diff --git a/drivers/char/ipmi/kcs_bmc_npcm7xx.c b/drivers/char/ipmi/kcs_bmc_npcm7xx.c index 7961fec56476..160553248a93 100644 --- a/drivers/char/ipmi/kcs_bmc_npcm7xx.c +++ b/drivers/char/ipmi/kcs_bmc_npcm7xx.c @@ -10,6 +10,7 @@ #include #include #include +#include #include #include #include @@ -17,7 +18,6 @@ #include #include -#include "kcs_bmc_device.h" #define DEVICE_NAME "npcm-kcs-bmc" #define KCS_CHANNEL_MAX 3 diff --git a/drivers/char/ipmi/kcs_bmc_serio.c b/drivers/char/ipmi/kcs_bmc_serio.c index 1793358be782..24df7144a189 100644 --- a/drivers/char/ipmi/kcs_bmc_serio.c +++ b/drivers/char/ipmi/kcs_bmc_serio.c @@ -5,12 +5,12 @@ #include #include #include +#include #include #include #include #include -#include "kcs_bmc_client.h" struct kcs_bmc_serio { struct list_head entry; diff --git a/drivers/char/ipmi/kcs_bmc.h b/include/linux/kcs_bmc.h similarity index 100% rename from drivers/char/ipmi/kcs_bmc.h rename to include/linux/kcs_bmc.h diff --git a/drivers/char/ipmi/kcs_bmc_client.h b/include/linux/kcs_bmc_client.h similarity index 97% rename from drivers/char/ipmi/kcs_bmc_client.h rename to include/linux/kcs_bmc_client.h index 6fdcde0a7169..f6350c9366dd 100644 --- a/drivers/char/ipmi/kcs_bmc_client.h +++ b/include/linux/kcs_bmc_client.h @@ -5,8 +5,7 @@ #define __KCS_BMC_CONSUMER_H__ #include - -#include "kcs_bmc.h" +#include struct kcs_bmc_driver_ops { int (*add_device)(struct kcs_bmc_device *kcs_bmc); diff --git a/drivers/char/ipmi/kcs_bmc_device.h b/include/linux/kcs_bmc_device.h similarity index 96% rename from drivers/char/ipmi/kcs_bmc_device.h rename to include/linux/kcs_bmc_device.h index 17c572f25c54..65333b68c0af 100644 --- a/drivers/char/ipmi/kcs_bmc_device.h +++ b/include/linux/kcs_bmc_device.h @@ -5,8 +5,7 @@ #define __KCS_BMC_DEVICE_H__ #include - -#include "kcs_bmc.h" +#include struct kcs_bmc_device_ops { void (*irq_mask_update)(struct kcs_bmc_device *kcs_bmc, u8 mask, u8 enable); From patchwork Fri Oct 6 10:02:13 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Konstantin Aladyshev X-Patchwork-Id: 13411230 Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net [23.128.96.19]) (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 C83A414AA3 for ; Fri, 6 Oct 2023 10:02:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="RHcNCArB" Received: from mail-lj1-x22a.google.com (mail-lj1-x22a.google.com [IPv6:2a00:1450:4864:20::22a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 550DBBE; Fri, 6 Oct 2023 03:02:25 -0700 (PDT) Received: by mail-lj1-x22a.google.com with SMTP id 38308e7fff4ca-2c15463ddd4so22221801fa.3; Fri, 06 Oct 2023 03:02:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1696586543; x=1697191343; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=Yp1yNMJhm0Xmlti2S7KpcZZ4ydY0GaIDDg/yGsmahK4=; b=RHcNCArBZ1zArCpZ2DbuzI2dRjogqz3/CMnRFYfC4K+kmtBH6IskqNA2nzLYwYxn2R sZ2ZSCbOeF2CSzaPCcu/JBH923moXzSxIhmwDM+AopyG7O6Rbfa0pGbKVDq6yR8sVoyr Q5JzA4jcHSDoCoR59c4fgScFL3ROrS94R1ZsJky6QB6QJFazxY96gLMepHAxPbnYi36c oW2l0GhZI/EBpTfsNWNxP84F30PQI0KjBN2Wdg9kQbCqYLHnOQI3MjLivopx6+paW8Vc pg3RE9vo/pttTUfv8ITgWKmSoFdI4xc1RPgQ3Xp2G1nTrUKYVlSbU5EX5L5V4q/Sswlm Pk7A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1696586543; x=1697191343; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Yp1yNMJhm0Xmlti2S7KpcZZ4ydY0GaIDDg/yGsmahK4=; b=diKcx0CMiMlh8rAE5/VAPhyCEnbRESTMCDSSonFFMj7gva+mu8I5hgF1ghg7u4CHW5 ta6PyNbH7graticZYj+jvuaRGjHqQjVPAHEKxDYONC6HA/ZGurJu1vvqkoZmUMqSPSKO OqIQRdRqI8UWeNnDrxlJo9ej/+ywLg6fn27r7+D6XuQmPLwpDFl/Irmo36zUWqvDQQeT K82XDxW8zxN+ZWU+J68gZotxrUFQqNQnSpOUyoaXdWeXCKtOvQ6LucYH3Cs0/ysrycc1 3/1V6J6Ehl2D/keVLC0jh0ikvQ1KK7oDrpTOHyS9QN7NxDunYe/lTcdOblg8ye481EoW GNsg== X-Gm-Message-State: AOJu0YyH4jfHRbLJe5SJPzofMtAZAE1AwNdlfUw8L23BSHYvfzIlPwOr v8uh3yKFAJOs195BL6h4eWg= X-Google-Smtp-Source: AGHT+IEAw64ITGn8zD/hrRbeUOfr6CVBTtqOx2AytEM6FmJ0zbi/4CWzzukSKnh07psQlzMotc3J4g== X-Received: by 2002:a2e:9d83:0:b0:2bd:102c:4161 with SMTP id c3-20020a2e9d83000000b002bd102c4161mr7720596ljj.43.1696586543483; Fri, 06 Oct 2023 03:02:23 -0700 (PDT) Received: from PC10319.67 ([82.97.198.254]) by smtp.googlemail.com with ESMTPSA id x12-20020a2e9dcc000000b002bcedacd726sm713674ljj.25.2023.10.06.03.02.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 06 Oct 2023 03:02:23 -0700 (PDT) From: Konstantin Aladyshev To: Cc: minyard@acm.org, joel@jms.id.au, andrew@aj.id.au, avifishman70@gmail.com, tmaimon77@gmail.com, tali.perry1@gmail.com, venture@google.com, yuenn@google.com, benjaminfair@google.com, aladyshev22@gmail.com, jk@codeconstruct.com.au, matt@codeconstruct.com.au, davem@davemloft.net, edumazet@google.com, kuba@kernel.org, pabeni@redhat.com, openipmi-developer@lists.sourceforge.net, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-aspeed@lists.ozlabs.org, openbmc@lists.ozlabs.org, netdev@vger.kernel.org Subject: [PATCH v4 2/3] ipmi: Create header with KCS interface defines Date: Fri, 6 Oct 2023 13:02:13 +0300 Message-Id: <20231006100214.396-3-aladyshev22@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20231006100214.396-1-aladyshev22@gmail.com> References: <20231006100214.396-1-aladyshev22@gmail.com> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Spam-Status: No, score=-1.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_ENVFROM_END_DIGIT, FREEMAIL_FROM,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Some definitions from the current kcs_bmc_cdev_ipmi driver can be also utilized by the MTCP KCS binding driver. Move such definitions to the common header file. Signed-off-by: Konstantin Aladyshev --- drivers/char/ipmi/kcs_bmc_cdev_ipmi.c | 71 +----------------------- include/linux/ipmi_kcs.h | 80 +++++++++++++++++++++++++++ 2 files changed, 81 insertions(+), 70 deletions(-) create mode 100644 include/linux/ipmi_kcs.h diff --git a/drivers/char/ipmi/kcs_bmc_cdev_ipmi.c b/drivers/char/ipmi/kcs_bmc_cdev_ipmi.c index bf1001130a6c..f158f676114c 100644 --- a/drivers/char/ipmi/kcs_bmc_cdev_ipmi.c +++ b/drivers/char/ipmi/kcs_bmc_cdev_ipmi.c @@ -8,6 +8,7 @@ #include #include #include +#include #include #include #include @@ -19,53 +20,6 @@ #include -/* Different phases of the KCS BMC module. - * KCS_PHASE_IDLE: - * BMC should not be expecting nor sending any data. - * KCS_PHASE_WRITE_START: - * BMC is receiving a WRITE_START command from system software. - * KCS_PHASE_WRITE_DATA: - * BMC is receiving a data byte from system software. - * KCS_PHASE_WRITE_END_CMD: - * BMC is waiting a last data byte from system software. - * KCS_PHASE_WRITE_DONE: - * BMC has received the whole request from system software. - * KCS_PHASE_WAIT_READ: - * BMC is waiting the response from the upper IPMI service. - * KCS_PHASE_READ: - * BMC is transferring the response to system software. - * KCS_PHASE_ABORT_ERROR1: - * BMC is waiting error status request from system software. - * KCS_PHASE_ABORT_ERROR2: - * BMC is waiting for idle status afer error from system software. - * KCS_PHASE_ERROR: - * BMC has detected a protocol violation at the interface level. - */ -enum kcs_ipmi_phases { - KCS_PHASE_IDLE, - - KCS_PHASE_WRITE_START, - KCS_PHASE_WRITE_DATA, - KCS_PHASE_WRITE_END_CMD, - KCS_PHASE_WRITE_DONE, - - KCS_PHASE_WAIT_READ, - KCS_PHASE_READ, - - KCS_PHASE_ABORT_ERROR1, - KCS_PHASE_ABORT_ERROR2, - KCS_PHASE_ERROR -}; - -/* IPMI 2.0 - Table 9-4, KCS Interface Status Codes */ -enum kcs_ipmi_errors { - KCS_NO_ERROR = 0x00, - KCS_ABORTED_BY_COMMAND = 0x01, - KCS_ILLEGAL_CONTROL_CODE = 0x02, - KCS_LENGTH_ERROR = 0x06, - KCS_UNSPECIFIED_ERROR = 0xFF -}; - struct kcs_bmc_ipmi { struct list_head entry; @@ -95,29 +49,6 @@ struct kcs_bmc_ipmi { #define KCS_MSG_BUFSIZ 1000 -#define KCS_ZERO_DATA 0 - -/* IPMI 2.0 - Table 9-1, KCS Interface Status Register Bits */ -#define KCS_STATUS_STATE(state) (state << 6) -#define KCS_STATUS_STATE_MASK GENMASK(7, 6) -#define KCS_STATUS_CMD_DAT BIT(3) -#define KCS_STATUS_SMS_ATN BIT(2) -#define KCS_STATUS_IBF BIT(1) -#define KCS_STATUS_OBF BIT(0) - -/* IPMI 2.0 - Table 9-2, KCS Interface State Bits */ -enum kcs_states { - IDLE_STATE = 0, - READ_STATE = 1, - WRITE_STATE = 2, - ERROR_STATE = 3, -}; - -/* IPMI 2.0 - Table 9-3, KCS Interface Control Codes */ -#define KCS_CMD_GET_STATUS_ABORT 0x60 -#define KCS_CMD_WRITE_START 0x61 -#define KCS_CMD_WRITE_END 0x62 -#define KCS_CMD_READ_BYTE 0x68 static inline void set_state(struct kcs_bmc_ipmi *priv, u8 state) { diff --git a/include/linux/ipmi_kcs.h b/include/linux/ipmi_kcs.h new file mode 100644 index 000000000000..30c4b6e4d689 --- /dev/null +++ b/include/linux/ipmi_kcs.h @@ -0,0 +1,80 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* + * Copyright (c) 2015-2018, Intel Corporation. + */ + +#ifndef __IPMI_KCS_H__ +#define __IPMI_KCS_H__ + +/* Different phases of the KCS BMC module. + * KCS_PHASE_IDLE: + * BMC should not be expecting nor sending any data. + * KCS_PHASE_WRITE_START: + * BMC is receiving a WRITE_START command from system software. + * KCS_PHASE_WRITE_DATA: + * BMC is receiving a data byte from system software. + * KCS_PHASE_WRITE_END_CMD: + * BMC is waiting a last data byte from system software. + * KCS_PHASE_WRITE_DONE: + * BMC has received the whole request from system software. + * KCS_PHASE_WAIT_READ: + * BMC is waiting the response from the upper IPMI service. + * KCS_PHASE_READ: + * BMC is transferring the response to system software. + * KCS_PHASE_ABORT_ERROR1: + * BMC is waiting error status request from system software. + * KCS_PHASE_ABORT_ERROR2: + * BMC is waiting for idle status afer error from system software. + * KCS_PHASE_ERROR: + * BMC has detected a protocol violation at the interface level. + */ +enum kcs_ipmi_phases { + KCS_PHASE_IDLE, + + KCS_PHASE_WRITE_START, + KCS_PHASE_WRITE_DATA, + KCS_PHASE_WRITE_END_CMD, + KCS_PHASE_WRITE_DONE, + + KCS_PHASE_WAIT_READ, + KCS_PHASE_READ, + + KCS_PHASE_ABORT_ERROR1, + KCS_PHASE_ABORT_ERROR2, + KCS_PHASE_ERROR +}; + +/* IPMI 2.0 - Table 9-4, KCS Interface Status Codes */ +enum kcs_ipmi_errors { + KCS_NO_ERROR = 0x00, + KCS_ABORTED_BY_COMMAND = 0x01, + KCS_ILLEGAL_CONTROL_CODE = 0x02, + KCS_LENGTH_ERROR = 0x06, + KCS_UNSPECIFIED_ERROR = 0xFF +}; + +#define KCS_ZERO_DATA 0 + +/* IPMI 2.0 - Table 9-1, KCS Interface Status Register Bits */ +#define KCS_STATUS_STATE(state) (state << 6) +#define KCS_STATUS_STATE_MASK GENMASK(7, 6) +#define KCS_STATUS_CMD_DAT BIT(3) +#define KCS_STATUS_SMS_ATN BIT(2) +#define KCS_STATUS_IBF BIT(1) +#define KCS_STATUS_OBF BIT(0) + +/* IPMI 2.0 - Table 9-2, KCS Interface State Bits */ +enum kcs_states { + IDLE_STATE = 0, + READ_STATE = 1, + WRITE_STATE = 2, + ERROR_STATE = 3, +}; + +/* IPMI 2.0 - Table 9-3, KCS Interface Control Codes */ +#define KCS_CMD_GET_STATUS_ABORT 0x60 +#define KCS_CMD_WRITE_START 0x61 +#define KCS_CMD_WRITE_END 0x62 +#define KCS_CMD_READ_BYTE 0x68 + +#endif /* __IPMI_KCS_H__ */ From patchwork Fri Oct 6 10:02:14 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Konstantin Aladyshev X-Patchwork-Id: 13411231 X-Patchwork-Delegate: kuba@kernel.org Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net [23.128.96.19]) (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 577CA14F68 for ; Fri, 6 Oct 2023 10:02:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="T6+tS5S7" Received: from mail-lj1-x229.google.com (mail-lj1-x229.google.com [IPv6:2a00:1450:4864:20::229]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8389AE4; Fri, 6 Oct 2023 03:02:26 -0700 (PDT) Received: by mail-lj1-x229.google.com with SMTP id 38308e7fff4ca-2c189dabcc3so23032251fa.1; Fri, 06 Oct 2023 03:02:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1696586545; x=1697191345; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=RVXNJRwMbWYP8lDR8Zj4lIcabRVrbtGn1/jxj2HPxxU=; b=T6+tS5S7FlZea9IWcQUFfLYYYT3rOmgONDH1WEUvyYCYdGr/BWVAiASoB3CYD8M3qA LYz8RAKpMIaJNukg1mZR93+FqWhS23U/3IpTW6UNQmReg/RbyhHKkpUAsnUPzr9VtSeU N1M1lQFPRpeuvN8DWEOgHbYsMM3BsWLizvWgsB0VAV2xLHWiJuRnvSTBMKZta178uXUp gl7M4ne1oq82LvMolULKD+92MS1pYqqcwupZ0HPDOJng6WrCeJBzewuFF/EYYG2PbJ3F bRcubnNToJvFlHtBxiQZnfnU05sL1icyOw2idJCYxuNwWkJSUBCSjTCsVHzs7dLGfFTk WG3g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1696586545; x=1697191345; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=RVXNJRwMbWYP8lDR8Zj4lIcabRVrbtGn1/jxj2HPxxU=; b=w+NF54PeupxLvPhbH8aya/RCXfILfdSJaKrkbCQLOgi+to+dZ7yOFGXRIL+QHgzaZ4 z4q39sGPpct2k2l8O/oFWUpO/HdiNrcTglg36J9jKzswNztNnT09ntuT69bduN0SBTau +S1ukEsHhYRemLpyaBSdrOWavHXX4IYImQj+P62gIB3Y9FvfH10CV+vXO3KCqBSQ3G65 tH7qMonXL3MzlbBDT2pi61TWvO6MVT7VwxuUMGMLkw8n4aJc99sCDBGCGjtwCaJzfMr0 MAlZUc6/+Pyr++kuSrDO0MPbwknpw6brzewCdeDPdiTFJf7o0CkxkTs9yBtarg18jZPS fmBw== X-Gm-Message-State: AOJu0YzKWwwbKPiC3MOZrvtYd+SQcp1CaspivEt1lZTB/KLlFq+XcB44 b6T2h/jPz+FIG8P/U40YdNI= X-Google-Smtp-Source: AGHT+IFgt8T3d6HC5kca86Qe4AfC5Dh9+bl8Ckl8nQQz81jWsyLiU6BCZMxiDVhAZE7coqmluHUPjQ== X-Received: by 2002:a05:6512:308e:b0:503:2642:435e with SMTP id z14-20020a056512308e00b005032642435emr7934101lfd.66.1696586544474; Fri, 06 Oct 2023 03:02:24 -0700 (PDT) Received: from PC10319.67 ([82.97.198.254]) by smtp.googlemail.com with ESMTPSA id x12-20020a2e9dcc000000b002bcedacd726sm713674ljj.25.2023.10.06.03.02.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 06 Oct 2023 03:02:24 -0700 (PDT) From: Konstantin Aladyshev To: Cc: minyard@acm.org, joel@jms.id.au, andrew@aj.id.au, avifishman70@gmail.com, tmaimon77@gmail.com, tali.perry1@gmail.com, venture@google.com, yuenn@google.com, benjaminfair@google.com, aladyshev22@gmail.com, jk@codeconstruct.com.au, matt@codeconstruct.com.au, davem@davemloft.net, edumazet@google.com, kuba@kernel.org, pabeni@redhat.com, openipmi-developer@lists.sourceforge.net, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-aspeed@lists.ozlabs.org, openbmc@lists.ozlabs.org, netdev@vger.kernel.org Subject: [PATCH v4 3/3] mctp: Add MCTP-over-KCS transport binding Date: Fri, 6 Oct 2023 13:02:14 +0300 Message-Id: <20231006100214.396-4-aladyshev22@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20231006100214.396-1-aladyshev22@gmail.com> References: <20231006100214.396-1-aladyshev22@gmail.com> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Spam-Status: No, score=-1.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_ENVFROM_END_DIGIT, FREEMAIL_FROM,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net X-Patchwork-Delegate: kuba@kernel.org This change adds a MCTP KCS transport binding, as defined by the DMTF specificiation DSP0254 - "MCTP KCS Transport Binding". A MCTP protocol network device is created for each KCS channel found in the system. The interrupt code for the KCS state machine is based on the current IPMI KCS driver. Signed-off-by: Konstantin Aladyshev --- drivers/net/mctp/Kconfig | 8 + drivers/net/mctp/Makefile | 1 + drivers/net/mctp/mctp-kcs.c | 594 ++++++++++++++++++++++++++++++++++++ 3 files changed, 603 insertions(+) create mode 100644 drivers/net/mctp/mctp-kcs.c diff --git a/drivers/net/mctp/Kconfig b/drivers/net/mctp/Kconfig index dc71657d9184..a37f7ba947c0 100644 --- a/drivers/net/mctp/Kconfig +++ b/drivers/net/mctp/Kconfig @@ -33,6 +33,14 @@ config MCTP_TRANSPORT_I2C from DMTF specification DSP0237. A MCTP protocol network device is created for each I2C bus that has been assigned a mctp-i2c device. +config MCTP_TRANSPORT_KCS + tristate "MCTP KCS transport" + depends on IPMI_KCS_BMC + help + Provides a driver to access MCTP devices over KCS transport, from + DMTF specification DSP0254. A MCTP protocol network device is + created for each KCS channel found in the system. + endmenu endif diff --git a/drivers/net/mctp/Makefile b/drivers/net/mctp/Makefile index 1ca3e6028f77..885339a40f22 100644 --- a/drivers/net/mctp/Makefile +++ b/drivers/net/mctp/Makefile @@ -1,2 +1,3 @@ obj-$(CONFIG_MCTP_SERIAL) += mctp-serial.o obj-$(CONFIG_MCTP_TRANSPORT_I2C) += mctp-i2c.o +obj-$(CONFIG_MCTP_TRANSPORT_KCS) += mctp-kcs.o diff --git a/drivers/net/mctp/mctp-kcs.c b/drivers/net/mctp/mctp-kcs.c new file mode 100644 index 000000000000..2ea2ab00c188 --- /dev/null +++ b/drivers/net/mctp/mctp-kcs.c @@ -0,0 +1,594 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Management Component Transport Protocol (MCTP) KCS transport binding. + * This driver is an implementation of the DMTF specificiation + * "DSP0254 - Management Component Transport Protocol (MCTP) KCS Transport + * Binding", available at: + * + * https://www.dmtf.org/sites/default/files/standards/documents/DSP0254_1.0.0.pdf + * + * This driver provides DSP0254-type MCTP-over-KCS transport using a Linux + * KCS client subsystem. + * + * Copyright (c) 2023 Konstantin Aladyshev + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define MCTP_KCS_MTU 64 +#define KCS_MSG_BUFSIZ 1000 + +struct mctp_kcs { + struct list_head entry; + + /* protects rx & tx state machines */ + spinlock_t lock; + + struct kcs_bmc_client client; + struct net_device *netdev; + + enum kcs_ipmi_phases phase; + enum kcs_ipmi_errors error; + + int data_in_idx; + u8 *data_in; + + int data_out_idx; + int data_out_len; + u8 *data_out; + + struct work_struct rx_work; +}; + +struct mctp_kcs_header { + u8 netfn_lun; + u8 defining_body; + u8 len; +} __packed; + +struct mctp_kcs_trailer { + u8 pec; +} __packed; + +#define MCTP_KCS_NETFN_LUN 0xb0 +#define DEFINING_BODY_DMTF_PRE_OS_WORKING_GROUP 0x01 + +static int mctp_kcs_validate_data(struct mctp_kcs *mkcs, + struct mctp_kcs_header *hdr, int len) +{ + struct net_device *ndev = mkcs->netdev; + struct mctp_kcs_trailer *tlr; + u8 pec; + + if (hdr->netfn_lun != MCTP_KCS_NETFN_LUN) { + dev_err(mkcs->client.dev->dev, + "%s: KCS binding header error! netfn_lun = 0x%02x, but should be 0x%02x", + __func__, hdr->netfn_lun, MCTP_KCS_NETFN_LUN); + ndev->stats.rx_dropped++; + return -EINVAL; + } + if (hdr->defining_body != DEFINING_BODY_DMTF_PRE_OS_WORKING_GROUP) { + dev_err(mkcs->client.dev->dev, + "%s: KCS binding header error! defining_body = 0x%02x, but should be 0x%02x", + __func__, hdr->defining_body, + DEFINING_BODY_DMTF_PRE_OS_WORKING_GROUP); + ndev->stats.rx_dropped++; + return -EINVAL; + } + if (hdr->len != (u8)(len - sizeof(struct mctp_kcs_header) - + sizeof(struct mctp_kcs_trailer))) { + dev_err(mkcs->client.dev->dev, + "%s: KCS binding header error! len = 0x%02x, but should be 0x%02x", + __func__, hdr->len, + (u8)(len - sizeof(struct mctp_kcs_header) - + sizeof(struct mctp_kcs_trailer))); + ndev->stats.rx_length_errors++; + return -EINVAL; + } + + pec = i2c_smbus_pec(0, (u8 *)(hdr + 1), hdr->len); + tlr = (struct mctp_kcs_trailer *)((u8 *)(hdr + 1) + hdr->len); + if (pec != tlr->pec) { + dev_err(mkcs->client.dev->dev, + "%s: PEC error! Packet value=0x%02x, calculated value=0x%02x", + __func__, tlr->pec, pec); + ndev->stats.rx_crc_errors++; + return -EINVAL; + } + return 0; +} + +static void mctp_kcs_rx_work(struct work_struct *work) +{ + struct mctp_kcs *mkcs = container_of(work, struct mctp_kcs, rx_work); + struct mctp_skb_cb *cb; + struct sk_buff *skb; + unsigned long flags; + int rc; + int i; + struct mctp_kcs_header *kcs_header; + int data_len; + int data_in_idx; + + spin_lock_irqsave(&mkcs->lock, flags); + for (i = 0; i < (mkcs->data_in_idx); i++) + dev_dbg(mkcs->client.dev->dev, "%s: data_in[%d]=0x%02x", + __func__, i, mkcs->data_in[i]); + + data_len = mkcs->data_in_idx - sizeof(struct mctp_kcs_header) - + sizeof(struct mctp_kcs_trailer); + if (mkcs->phase != KCS_PHASE_WRITE_DONE) { + dev_err(mkcs->client.dev->dev, + "%s: error! Wrong KCS stage at the end of data read (phase=%d)", + __func__, mkcs->phase); + mkcs->netdev->stats.rx_dropped++; + goto unlock_irq; + } + + mkcs->phase = KCS_PHASE_WAIT_READ; + data_in_idx = mkcs->data_in_idx; + mkcs->data_in_idx = 0; + + skb = netdev_alloc_skb(mkcs->netdev, data_len); + if (!skb) { + mkcs->netdev->stats.rx_dropped++; + goto unlock_irq; + } + + kcs_header = (struct mctp_kcs_header *)mkcs->data_in; + rc = mctp_kcs_validate_data(mkcs, kcs_header, data_in_idx); + if (rc) { + dev_err(mkcs->client.dev->dev, + "%s: error! Binding validation failed", __func__); + dev_kfree_skb(skb); + goto unlock_irq; + } + + skb->protocol = htons(ETH_P_MCTP); + skb_put_data(skb, mkcs->data_in + sizeof(struct mctp_kcs_header), + data_len); + skb_reset_network_header(skb); + + cb = __mctp_cb(skb); + cb->halen = 0; + + netif_rx(skb); + mkcs->netdev->stats.rx_packets++; + mkcs->netdev->stats.rx_bytes += data_len; + +unlock_irq: + spin_unlock_irqrestore(&mkcs->lock, flags); +} + +static netdev_tx_t mctp_kcs_start_xmit(struct sk_buff *skb, + struct net_device *ndev) +{ + struct mctp_kcs_header *kcs_header; + unsigned long flags; + int i; + struct mctp_kcs *mkcs = netdev_priv(ndev); + + if (skb->len > MCTP_KCS_MTU) { + dev_err(&ndev->dev, "%s: error! skb len is bigger than MTU", + __func__); + ndev->stats.tx_dropped++; + goto out; + } + + spin_lock_irqsave(&mkcs->lock, flags); + if (mkcs->phase != KCS_PHASE_WAIT_READ) { + dev_err(&ndev->dev, + "%s: error! Wrong KCS stage at the start of data write (phase=%d)", + __func__, mkcs->phase); + dev_kfree_skb(skb); + spin_unlock_irqrestore(&mkcs->lock, flags); + return NETDEV_TX_BUSY; + } + + netif_stop_queue(ndev); + mkcs->phase = KCS_PHASE_READ; + kcs_header = (struct mctp_kcs_header *)mkcs->data_out; + kcs_header->netfn_lun = MCTP_KCS_NETFN_LUN; + kcs_header->defining_body = DEFINING_BODY_DMTF_PRE_OS_WORKING_GROUP; + kcs_header->len = skb->len; + skb_copy_bits(skb, 0, kcs_header + 1, skb->len); + mkcs->data_out[sizeof(struct mctp_kcs_header) + skb->len] = + i2c_smbus_pec(0, (u8 *)(kcs_header + 1), skb->len); + mkcs->data_out_idx = 1; + mkcs->data_out_len = skb->len + sizeof(struct mctp_kcs_header) + + sizeof(struct mctp_kcs_trailer); + + for (i = 0; i < (mkcs->data_out_len); i++) + dev_dbg(&ndev->dev, "%s: data_out[%d]=0x%02x", __func__, i, + mkcs->data_out[i]); + + // Write first data byte to initialize transmission + kcs_bmc_write_data(mkcs->client.dev, mkcs->data_out[0]); + + spin_unlock_irqrestore(&mkcs->lock, flags); +out: + dev_kfree_skb(skb); + return NETDEV_TX_OK; +} + +static void set_state(struct mctp_kcs *mkcs, u8 state) +{ + dev_dbg(mkcs->client.dev->dev, "%s: state=0x%02x", __func__, state); + kcs_bmc_update_status(mkcs->client.dev, KCS_STATUS_STATE_MASK, + KCS_STATUS_STATE(state)); +} + +static int mctp_kcs_ndo_open(struct net_device *ndev) +{ + struct mctp_kcs *mkcs; + + mkcs = netdev_priv(ndev); + dev_info(&ndev->dev, "Open MCTP over KCS channel %d", + mkcs->client.dev->channel); + return kcs_bmc_enable_device(mkcs->client.dev, &mkcs->client); +} + +static int mctp_kcs_ndo_stop(struct net_device *ndev) +{ + struct mctp_kcs *mkcs; + + mkcs = netdev_priv(ndev); + dev_info(&ndev->dev, "Stop MCTP over KCS channel %d", + mkcs->client.dev->channel); + mkcs->data_in_idx = 0; + mkcs->data_out_idx = 0; + mkcs->data_out_len = 0; + mkcs->phase = KCS_PHASE_IDLE; + set_state(mkcs, IDLE_STATE); + kcs_bmc_disable_device(mkcs->client.dev, &mkcs->client); + return 0; +} + +static const struct net_device_ops mctp_kcs_netdev_ops = { + .ndo_start_xmit = mctp_kcs_start_xmit, + .ndo_open = mctp_kcs_ndo_open, + .ndo_stop = mctp_kcs_ndo_stop, +}; + +static void mctp_kcs_setup(struct net_device *ndev) +{ + ndev->type = ARPHRD_MCTP; + + /* we limit at the fixed MTU, which is also the MCTP-standard + * baseline MTU, so is also our minimum + */ + ndev->mtu = MCTP_KCS_MTU; + ndev->max_mtu = MCTP_KCS_MTU; + ndev->min_mtu = MCTP_KCS_MTU; + + ndev->hard_header_len = 0; + ndev->addr_len = 0; + ndev->tx_queue_len = DEFAULT_TX_QUEUE_LEN; + ndev->flags = IFF_NOARP; + ndev->netdev_ops = &mctp_kcs_netdev_ops; + ndev->needs_free_netdev = true; +} + +static void kcs_bmc_ipmi_force_abort(struct mctp_kcs *mkcs) +{ + dev_err(mkcs->client.dev->dev, + "Error! Force abort on KCS communication"); + set_state(mkcs, ERROR_STATE); + kcs_bmc_read_data(mkcs->client.dev); + kcs_bmc_write_data(mkcs->client.dev, KCS_ZERO_DATA); + mkcs->phase = KCS_PHASE_ERROR; + mkcs->data_in_idx = 0; +} + +static void kcs_bmc_ipmi_handle_data(struct mctp_kcs *mkcs) +{ + u8 data; + struct kcs_bmc_device *kcs_bmc = mkcs->client.dev; + + switch (mkcs->phase) { + case KCS_PHASE_WRITE_START: + dev_dbg(kcs_bmc->dev, "%s: KCS_PHASE_WRITE_START", __func__); + mkcs->phase = KCS_PHASE_WRITE_DATA; + fallthrough; + + case KCS_PHASE_WRITE_DATA: + dev_dbg(kcs_bmc->dev, "%s: KCS_PHASE_WRITE_DATA", __func__); + if (mkcs->data_in_idx < KCS_MSG_BUFSIZ) { + set_state(mkcs, WRITE_STATE); + kcs_bmc_write_data(kcs_bmc, KCS_ZERO_DATA); + mkcs->data_in[mkcs->data_in_idx++] = + kcs_bmc_read_data(kcs_bmc); + dev_dbg(kcs_bmc->dev, + "%s: KCS_PHASE_WRITE_DATA: data_in[%d]=0x%02x", + __func__, mkcs->data_in_idx - 1, + mkcs->data_in[mkcs->data_in_idx - 1]); + } else { + kcs_bmc_ipmi_force_abort(mkcs); + mkcs->error = KCS_LENGTH_ERROR; + } + break; + + case KCS_PHASE_WRITE_END_CMD: + dev_dbg(kcs_bmc->dev, "%s: KCS_PHASE_WRITE_END_CMD", __func__); + if (mkcs->data_in_idx < KCS_MSG_BUFSIZ) { + set_state(mkcs, READ_STATE); + mkcs->data_in[mkcs->data_in_idx++] = + kcs_bmc_read_data(kcs_bmc); + dev_dbg(kcs_bmc->dev, + "%s: KCS_PHASE_WRITE_END_CMD: data_in[%d]=0x%02x", + __func__, mkcs->data_in_idx - 1, + mkcs->data_in[mkcs->data_in_idx - 1]); + mkcs->phase = KCS_PHASE_WRITE_DONE; + schedule_work(&mkcs->rx_work); + } else { + kcs_bmc_ipmi_force_abort(mkcs); + mkcs->error = KCS_LENGTH_ERROR; + } + break; + + case KCS_PHASE_READ: + dev_dbg(kcs_bmc->dev, + "%s: KCS_PHASE_READ, data_out_idx=%d, data_out_len=%d", + __func__, mkcs->data_out_idx, mkcs->data_out_len); + if (mkcs->data_out_idx == mkcs->data_out_len) + set_state(mkcs, IDLE_STATE); + + data = kcs_bmc_read_data(kcs_bmc); + if (data != KCS_CMD_READ_BYTE) { + dev_dbg(kcs_bmc->dev, + "%s: error! data is not equal to KCS_CMD_READ_BYTE", + __func__); + set_state(mkcs, ERROR_STATE); + kcs_bmc_write_data(kcs_bmc, KCS_ZERO_DATA); + break; + } + + if (mkcs->data_out_idx == mkcs->data_out_len) { + kcs_bmc_write_data(kcs_bmc, KCS_ZERO_DATA); + mkcs->netdev->stats.tx_bytes += mkcs->data_out_len; + mkcs->netdev->stats.tx_packets++; + mkcs->phase = KCS_PHASE_IDLE; + if (netif_queue_stopped(mkcs->netdev)) + netif_start_queue(mkcs->netdev); + break; + } + + dev_dbg(kcs_bmc->dev, "%s: KCS_PHASE_READ: data_out[%d]=0x%02x", + __func__, mkcs->data_out_idx, + mkcs->data_out[mkcs->data_out_idx]); + kcs_bmc_write_data(kcs_bmc, + mkcs->data_out[mkcs->data_out_idx++]); + break; + + case KCS_PHASE_ABORT_ERROR1: + dev_dbg(kcs_bmc->dev, "%s: KCS_PHASE_ABORT_ERROR1", __func__); + set_state(mkcs, READ_STATE); + kcs_bmc_read_data(kcs_bmc); + kcs_bmc_write_data(kcs_bmc, mkcs->error); + mkcs->phase = KCS_PHASE_ABORT_ERROR2; + break; + + case KCS_PHASE_ABORT_ERROR2: + dev_dbg(kcs_bmc->dev, "%s: KCS_PHASE_ABORT_ERROR2", __func__); + set_state(mkcs, IDLE_STATE); + kcs_bmc_read_data(kcs_bmc); + kcs_bmc_write_data(kcs_bmc, KCS_ZERO_DATA); + mkcs->phase = KCS_PHASE_IDLE; + break; + + default: + dev_dbg(kcs_bmc->dev, "%s: unknown KCS phase", __func__); + kcs_bmc_ipmi_force_abort(mkcs); + break; + } +} + +static void kcs_bmc_ipmi_handle_cmd(struct mctp_kcs *mkcs) +{ + struct kcs_bmc_device *kcs_bmc = mkcs->client.dev; + + set_state(mkcs, WRITE_STATE); + kcs_bmc_write_data(kcs_bmc, KCS_ZERO_DATA); + + switch (kcs_bmc_read_data(kcs_bmc)) { + case KCS_CMD_WRITE_START: + dev_dbg(kcs_bmc->dev, "%s: KCS_CMD_WRITE_START", __func__); + mkcs->phase = KCS_PHASE_WRITE_START; + mkcs->error = KCS_NO_ERROR; + mkcs->data_in_idx = 0; + break; + + case KCS_CMD_WRITE_END: + dev_dbg(kcs_bmc->dev, "%s: KCS_CMD_WRITE_END", __func__); + if (mkcs->phase != KCS_PHASE_WRITE_DATA) { + kcs_bmc_ipmi_force_abort(mkcs); + break; + } + mkcs->phase = KCS_PHASE_WRITE_END_CMD; + break; + + case KCS_CMD_GET_STATUS_ABORT: + dev_dbg(kcs_bmc->dev, "%s: KCS_CMD_GET_STATUS_ABORT", __func__); + if (mkcs->error == KCS_NO_ERROR) + mkcs->error = KCS_ABORTED_BY_COMMAND; + + mkcs->phase = KCS_PHASE_ABORT_ERROR1; + mkcs->data_in_idx = 0; + break; + + default: + dev_dbg(kcs_bmc->dev, "%s: unknown KCS command", __func__); + kcs_bmc_ipmi_force_abort(mkcs); + mkcs->error = KCS_ILLEGAL_CONTROL_CODE; + break; + } +} + +static struct mctp_kcs *client_to_mctp_kcs(struct kcs_bmc_client *client) +{ + return container_of(client, struct mctp_kcs, client); +} + +static irqreturn_t kcs_bmc_mctp_event(struct kcs_bmc_client *client) +{ + struct mctp_kcs *mkcs; + u8 status; + int ret; + + mkcs = client_to_mctp_kcs(client); + if (!mkcs) { + dev_err(client->dev->dev, + "%s: error! can't find mctp_kcs from KCS client", + __func__); + return IRQ_NONE; + } + + spin_lock(&mkcs->lock); + + status = kcs_bmc_read_status(client->dev); + if (status & KCS_STATUS_IBF) { + if (status & KCS_STATUS_CMD_DAT) + kcs_bmc_ipmi_handle_cmd(mkcs); + else + kcs_bmc_ipmi_handle_data(mkcs); + + ret = IRQ_HANDLED; + } else { + ret = IRQ_NONE; + } + + spin_unlock(&mkcs->lock); + + return ret; +} + +static const struct kcs_bmc_client_ops kcs_bmc_mctp_client_ops = { + .event = kcs_bmc_mctp_event, +}; + +static DEFINE_SPINLOCK(kcs_bmc_mctp_instances_lock); +static LIST_HEAD(kcs_bmc_mctp_instances); + +static int kcs_bmc_mctp_add_device(struct kcs_bmc_device *kcs_bmc) +{ + struct mctp_kcs *mkcs; + struct net_device *ndev; + char name[32]; + int rc; + + snprintf(name, sizeof(name), "mctpkcs%d", kcs_bmc->channel); + + ndev = alloc_netdev(sizeof(*mkcs), name, NET_NAME_ENUM, mctp_kcs_setup); + if (!ndev) { + dev_err_probe(kcs_bmc->dev, -ENOMEM, + "alloc_netdev failed for KCS channel %d\n", + kcs_bmc->channel); + return -ENOMEM; + } + + mkcs = netdev_priv(ndev); + mkcs->netdev = ndev; + mkcs->client.dev = kcs_bmc; + mkcs->client.ops = &kcs_bmc_mctp_client_ops; + mkcs->data_in = devm_kmalloc(kcs_bmc->dev, KCS_MSG_BUFSIZ, GFP_KERNEL); + if (!mkcs->data_in) { + dev_err_probe( + kcs_bmc->dev, -ENOMEM, + "failed to allocate data_in buffer for KCS channel %d\n", + kcs_bmc->channel); + rc = -ENOMEM; + goto free_netdev; + } + mkcs->data_out = devm_kmalloc(kcs_bmc->dev, KCS_MSG_BUFSIZ, GFP_KERNEL); + if (!mkcs->data_out) { + dev_err_probe( + kcs_bmc->dev, -ENOMEM, + "failed to allocate data_out buffer for KCS channel %d\n", + kcs_bmc->channel); + rc = -ENOMEM; + goto free_netdev; + } + + INIT_WORK(&mkcs->rx_work, mctp_kcs_rx_work); + + rc = register_netdev(ndev); + if (rc) + goto free_netdev; + + spin_lock_irq(&kcs_bmc_mctp_instances_lock); + list_add(&mkcs->entry, &kcs_bmc_mctp_instances); + spin_unlock_irq(&kcs_bmc_mctp_instances_lock); + + dev_info(kcs_bmc->dev, "Add MCTP client for the KCS channel %d", + kcs_bmc->channel); + return 0; + +free_netdev: + free_netdev(ndev); + + return rc; +} + +static int kcs_bmc_mctp_remove_device(struct kcs_bmc_device *kcs_bmc) +{ + struct mctp_kcs *mkcs = NULL, *pos; + + dev_info(kcs_bmc->dev, "Remove MCTP client for the KCS channel %d", + kcs_bmc->channel); + spin_lock_irq(&kcs_bmc_mctp_instances_lock); + list_for_each_entry(pos, &kcs_bmc_mctp_instances, entry) { + if (pos->client.dev == kcs_bmc) { + mkcs = pos; + list_del(&pos->entry); + break; + } + } + spin_unlock_irq(&kcs_bmc_mctp_instances_lock); + + if (!mkcs) + return -ENODEV; + + unregister_netdev(mkcs->netdev); + free_netdev(mkcs->netdev); + kcs_bmc_disable_device(kcs_bmc, &mkcs->client); + devm_kfree(kcs_bmc->dev, mkcs->data_out); + devm_kfree(kcs_bmc->dev, mkcs->data_in); + return 0; +} + +static const struct kcs_bmc_driver_ops kcs_bmc_mctp_driver_ops = { + .add_device = kcs_bmc_mctp_add_device, + .remove_device = kcs_bmc_mctp_remove_device, +}; + +static struct kcs_bmc_driver kcs_bmc_mctp_driver = { + .ops = &kcs_bmc_mctp_driver_ops, +}; + +static int __init mctp_kcs_init(void) +{ + kcs_bmc_register_driver(&kcs_bmc_mctp_driver); + return 0; +} + +static void __exit mctp_kcs_exit(void) +{ + kcs_bmc_unregister_driver(&kcs_bmc_mctp_driver); +} + +module_init(mctp_kcs_init); +module_exit(mctp_kcs_exit); + +MODULE_LICENSE("GPL"); +MODULE_AUTHOR("Konstantin Aladyshev "); +MODULE_DESCRIPTION("MCTP KCS transport");