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: 1844342 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20230601 header.b=lDWqyAWZ; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.ozlabs.org (client-ip=2404:9400:2:0:216:3eff:fee1:b9f1; helo=lists.ozlabs.org; envelope-from=openbmc-bounces+incoming=patchwork.ozlabs.org@lists.ozlabs.org; receiver=patchwork.ozlabs.org) Received: from lists.ozlabs.org (lists.ozlabs.org [IPv6:2404:9400:2:0:216:3eff:fee1:b9f1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4S23wL34fYz1yq9 for ; Fri, 6 Oct 2023 21:05:54 +1100 (AEDT) Authentication-Results: lists.ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20230601 header.b=lDWqyAWZ; dkim-atps=neutral Received: from boromir.ozlabs.org (localhost [IPv6:::1]) by lists.ozlabs.org (Postfix) with ESMTP id 4S23wL21HFz3dhR for ; Fri, 6 Oct 2023 21:05:54 +1100 (AEDT) X-Original-To: openbmc@lists.ozlabs.org Delivered-To: openbmc@lists.ozlabs.org Authentication-Results: lists.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20230601 header.b=lDWqyAWZ; dkim-atps=neutral Authentication-Results: lists.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gmail.com (client-ip=2a00:1450:4864:20::12d; helo=mail-lf1-x12d.google.com; envelope-from=aladyshev22@gmail.com; receiver=lists.ozlabs.org) Received: from mail-lf1-x12d.google.com (mail-lf1-x12d.google.com [IPv6:2a00:1450:4864:20::12d]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by lists.ozlabs.org (Postfix) with ESMTPS id 4S23rR43vdz3cPC; Fri, 6 Oct 2023 21:02:30 +1100 (AEDT) Received: by mail-lf1-x12d.google.com with SMTP id 2adb3069b0e04-50433d8385cso2597931e87.0; Fri, 06 Oct 2023 03:02:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1696586542; x=1697191342; darn=lists.ozlabs.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=lDWqyAWZ1aXqR5jiOVnlF7DAb3U4eJS0NPf5sTZRAdfhgu04fZwVDjkxt2j1112Ivz 3ymefW52EbrPO8o2UQ9KmPL114VFWKJ1Brap6xpJ8x3Cjkr8YSfsdh5dj8yi484c9YdC HYGrjpEB0fq2sLawceC6VhCB9LPmy95317fV+HakgheU7UXnL+hdICp8j0RfdjllD0Ot /SLv0XDWgAEz50Q7uFvGVTnE4Zj7DfKjgugnBi7B5UkYnqvrpN1ofwLeyfMDnYf+PPnn kvGr4uIdB9mc4Y3gio0gU0pPRwC62xoSfvpogkVgBRICe+NAdBKa4ekPyzp590C6xoRW iGWg== 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=W7LGWqFK76TKC4Z5qKR83w16JlG1jziNEV12S2xUQnSxzNYMfZvClrAI85Lxb+gIRE 6gjpIdgtpOCx6owMwv2O6OOiB4nfr+/Ck0S6PoeALX4iXvxcTmiMHO4mCbx2Jfxm9U3m 6xGBn6VQFjoNun+yp6ZnHGF5oSI1nk1muVw+oasG4tFSNN3YtuvjUKiqxdvH90Ox1LSx 73nCgdtDrFpQqtOTxO1Pc2Ba7U0/FAdGhGD6aZyjPlXeq6z3/PjdhvfcNHbqNZ+IKT7Y sERF2SKzIdEYib5XzzPlvycBHygRZrVMYgZYvbWt1EOrtmlkmH/qw8Q+9NfD2Tw6GSMe HOzg== X-Gm-Message-State: AOJu0Yw5gK8WmRRmiiBARNkiqTq0koqYNmO1kCxvv/k9VuI6IGFvG8KJ QSIZ+0RW/+JjyLkANZqpLk8= 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: 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> MIME-Version: 1.0 X-BeenThere: openbmc@lists.ozlabs.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Development list for OpenBMC List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: tmaimon77@gmail.com, linux-aspeed@lists.ozlabs.org, tali.perry1@gmail.com, edumazet@google.com, jk@codeconstruct.com.au, matt@codeconstruct.com.au, benjaminfair@google.com, openbmc@lists.ozlabs.org, joel@jms.id.au, kuba@kernel.org, pabeni@redhat.com, minyard@acm.org, aladyshev22@gmail.com, openipmi-developer@lists.sourceforge.net, linux-arm-kernel@lists.infradead.org, andrew@aj.id.au, venture@google.com, linux-kernel@vger.kernel.org, avifishman70@gmail.com, netdev@vger.kernel.org, davem@davemloft.net Errors-To: openbmc-bounces+incoming=patchwork.ozlabs.org@lists.ozlabs.org Sender: "openbmc" 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: 1844340 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20230601 header.b=OYNBYdLQ; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.ozlabs.org (client-ip=2404:9400:2:0:216:3eff:fee1:b9f1; helo=lists.ozlabs.org; envelope-from=openbmc-bounces+incoming=patchwork.ozlabs.org@lists.ozlabs.org; receiver=patchwork.ozlabs.org) Received: from lists.ozlabs.org (lists.ozlabs.org [IPv6:2404:9400:2:0:216:3eff:fee1:b9f1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4S23vB4vyRz1yq7 for ; Fri, 6 Oct 2023 21:04:54 +1100 (AEDT) Authentication-Results: lists.ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20230601 header.b=OYNBYdLQ; dkim-atps=neutral Received: from boromir.ozlabs.org (localhost [IPv6:::1]) by lists.ozlabs.org (Postfix) with ESMTP id 4S23vB3t1Bz3cnS for ; Fri, 6 Oct 2023 21:04:54 +1100 (AEDT) X-Original-To: openbmc@lists.ozlabs.org Delivered-To: openbmc@lists.ozlabs.org Authentication-Results: lists.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20230601 header.b=OYNBYdLQ; dkim-atps=neutral Authentication-Results: lists.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gmail.com (client-ip=2a00:1450:4864:20::232; helo=mail-lj1-x232.google.com; envelope-from=aladyshev22@gmail.com; receiver=lists.ozlabs.org) Received: from mail-lj1-x232.google.com (mail-lj1-x232.google.com [IPv6:2a00:1450:4864:20::232]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by lists.ozlabs.org (Postfix) with ESMTPS id 4S23rR3H2bz3cNk; Fri, 6 Oct 2023 21:02:29 +1100 (AEDT) Received: by mail-lj1-x232.google.com with SMTP id 38308e7fff4ca-2b9c907bc68so22193811fa.2; Fri, 06 Oct 2023 03:02:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1696586543; x=1697191343; darn=lists.ozlabs.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=OYNBYdLQjloUvjgQQIWM6hMZt2G5HCblTGW7vQsZI0FzZifYgMoYm1CWbGk+ZCYGT5 7OTQxwP56zyWmx1ibjD3wvBrOue8b5Xc7KHJWpufq9GyYd7iEwmHDf1BYaMmDKXFSLgU Hy49mlAcYiUEg0vBMRFI+fsZ+aRt3VVQOCPOtx+ChQHSSbotDyve0KZbQai/NGNH3kiH b9n0uUZgwcaJfSsyqC3BSYY1CAOB93td1DRYDmbhJi26VFOJva1aR4SGMRbVKgZhzaQr R3VAx0TjF/Mb1cKKIXFHNQOyCuHQ+5IZG5Byq2lh3PEJI12hcqu9YDj78IdLAcJ7mW1m fpxQ== 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=n3kBZh0S+9JZbfhwN04BKuPD7TVb6HglFHeEsBJvIslhi8W+5Fjj4CaoigjverZEnW NXcTzXzrWBqXsbywqAkZWvC3ANCpGK95V3lmQoDokqts2Ls1BVC81oigqnGc2mptAchP ndVfu52a2OupQWRwA+fO/Lyfu7mexuirXXuEV6oSWvSHQgR+5d9PjlsEEJUp39knESnu nCycHABtUci28+rO1y2qjzIHQjD6SK/epjsfHjhronXS4qrMgM5qCEvfbfDPBtO8/LjF jbr0ffzWQ5nYWr0SL7e5rjBJOPml2KyEn/Kn6GhLn7EZSxwAIBsU0Qi1FsmevWDti7L2 tz5w== X-Gm-Message-State: AOJu0YwjPnfvRX8U1fdB8yiBCsF/fBnprp+rPQLt4qtOJfgTOxJkuyt8 79QdJHgUUDtoCFmwbJFk7RM= 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: 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> MIME-Version: 1.0 X-BeenThere: openbmc@lists.ozlabs.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Development list for OpenBMC List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: tmaimon77@gmail.com, linux-aspeed@lists.ozlabs.org, tali.perry1@gmail.com, edumazet@google.com, jk@codeconstruct.com.au, matt@codeconstruct.com.au, benjaminfair@google.com, openbmc@lists.ozlabs.org, joel@jms.id.au, kuba@kernel.org, pabeni@redhat.com, minyard@acm.org, aladyshev22@gmail.com, openipmi-developer@lists.sourceforge.net, linux-arm-kernel@lists.infradead.org, andrew@aj.id.au, venture@google.com, linux-kernel@vger.kernel.org, avifishman70@gmail.com, netdev@vger.kernel.org, davem@davemloft.net Errors-To: openbmc-bounces+incoming=patchwork.ozlabs.org@lists.ozlabs.org Sender: "openbmc" 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: 1844338 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20230601 header.b=Bnb3AfHe; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.ozlabs.org (client-ip=2404:9400:2:0:216:3eff:fee1:b9f1; helo=lists.ozlabs.org; envelope-from=openbmc-bounces+incoming=patchwork.ozlabs.org@lists.ozlabs.org; receiver=patchwork.ozlabs.org) Received: from lists.ozlabs.org (lists.ozlabs.org [IPv6:2404:9400:2:0:216:3eff:fee1:b9f1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4S23s61P7Yz1yq7 for ; Fri, 6 Oct 2023 21:03:06 +1100 (AEDT) Authentication-Results: lists.ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20230601 header.b=Bnb3AfHe; dkim-atps=neutral Received: from boromir.ozlabs.org (localhost [IPv6:::1]) by lists.ozlabs.org (Postfix) with ESMTP id 4S23s60bMtz3cSQ for ; Fri, 6 Oct 2023 21:03:06 +1100 (AEDT) X-Original-To: openbmc@lists.ozlabs.org Delivered-To: openbmc@lists.ozlabs.org Authentication-Results: lists.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20230601 header.b=Bnb3AfHe; dkim-atps=neutral Authentication-Results: lists.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gmail.com (client-ip=2a00:1450:4864:20::133; helo=mail-lf1-x133.google.com; envelope-from=aladyshev22@gmail.com; receiver=lists.ozlabs.org) Received: from mail-lf1-x133.google.com (mail-lf1-x133.google.com [IPv6:2a00:1450:4864:20::133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by lists.ozlabs.org (Postfix) with ESMTPS id 4S23rR3BdVz3cB1; Fri, 6 Oct 2023 21:02:30 +1100 (AEDT) Received: by mail-lf1-x133.google.com with SMTP id 2adb3069b0e04-50435a9f800so2536775e87.2; Fri, 06 Oct 2023 03:02:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1696586545; x=1697191345; darn=lists.ozlabs.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=Bnb3AfHeVAEVTtLmHgQIcXa015T4kso3ijjR4qcxzpmjtiEPK1l07ZyCkfMKonogf2 oKRhFJDeMzmyovzr5YBUXwEk3A6xH66m2bD67ZkZVndUqkY+TRkQsc0ZJtdl5VA7n4ve qBMfHLPLsfxbNuorKO+eoWrGAKtC9/c18YQGVjuuLq3/1/jrXme8ADGxTgTc/5O4kii6 xOkCYuP4k3VrkOrqK9NV9c5fJJL31h1tR7Z3R50l3gOPm9bi/vAsBs6d8OUWcXnaEyp5 WcH99ArvO39FrQ8ph8wxD1E3Y4S1l3P2hWA2ZDyPpM3YJlsnBMzGJoXKkdHXsjaGGZQz f8dA== 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=MyiZXoHXajRnVj3Qejl2YZgUbFc76EgqllC3lE8yM6BPxIogwAPPsKusajfp2k6De+ IIFAVjiqNGhgOBQNEWWyNfel6ce3NoVB8V50n1IIxW5IErBRyt91pb4X+XnSm6O7vxuL ltBc/Xa06txfRL7f+q+xAYWh7ouqysG05IN0SCCRfFUhrrwdzxMNJmfkGhoTBOvkCtWS fyONxiIQq6frb35cHg90N04AKFMtbaGu6lHv/uWqbcCfT+0F5IjyT3Lj+fF3aJdJWB65 kKGcaZg2i4JWunJnBH8385yBoqmfch2qUlzSS8ceM5Vb+6qOPYIYgncx8r0AJJsLMx1X HibA== X-Gm-Message-State: AOJu0YzMbzavcLHO3az/Hhb+/tZk1tr8YFAM1dfOkprq2wWh0LAJiQIp 5mqBohRkQciAoU74JpK15TE= 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: 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> MIME-Version: 1.0 X-BeenThere: openbmc@lists.ozlabs.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Development list for OpenBMC List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: tmaimon77@gmail.com, linux-aspeed@lists.ozlabs.org, tali.perry1@gmail.com, edumazet@google.com, jk@codeconstruct.com.au, matt@codeconstruct.com.au, benjaminfair@google.com, openbmc@lists.ozlabs.org, joel@jms.id.au, kuba@kernel.org, pabeni@redhat.com, minyard@acm.org, aladyshev22@gmail.com, openipmi-developer@lists.sourceforge.net, linux-arm-kernel@lists.infradead.org, andrew@aj.id.au, venture@google.com, linux-kernel@vger.kernel.org, avifishman70@gmail.com, netdev@vger.kernel.org, davem@davemloft.net Errors-To: openbmc-bounces+incoming=patchwork.ozlabs.org@lists.ozlabs.org Sender: "openbmc" 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");