Skip to contentSkip to conversation

Add Aspeed SSIF BMC driver

PATCH Superseded Quan Nguyen 4 participants 8 messages Mar 29, 2021 – Apr 2, 2021

Subsystems (5)

v1 of 10 Superseded Latest is v10 →

lore
Share & tools
lore
#0 lore raw

cover Add Aspeed SSIF BMC driver

This series add support for the Aspeed specific SSIF BMC driver which is to perform in-band IPMI communication with the host in management (BMC) side.

Quan Nguyen (3):
i2c: i2c-core-smbus: Expose PEC calculate function for generic use
drivers: char: ipmi: Add Aspeed SSIF BMC driver
bindings: ipmi: Add binding for Aspeed SSIF BMC driver

 .../bindings/ipmi/aspeed-ssif-bmc.txt         |  18 +
 drivers/char/ipmi/Kconfig                     |  22 +
 drivers/char/ipmi/Makefile                    |   2 +
 drivers/char/ipmi/ssif_bmc.c                  | 645 ++++++++++++++++++
 drivers/char/ipmi/ssif_bmc.h                  |  92 +++
 drivers/char/ipmi/ssif_bmc_aspeed.c           | 132 ++++
 drivers/i2c/i2c-core-smbus.c                  |  12 +-
 include/linux/i2c.h                           |   1 +
 8 files changed, 922 insertions(+), 2 deletions(-)
 create mode 100644 Documentation/devicetree/bindings/ipmi/aspeed-ssif-bmc.txt
 create mode 100644 drivers/char/ipmi/ssif_bmc.c
 create mode 100644 drivers/char/ipmi/ssif_bmc.h
 create mode 100644 drivers/char/ipmi/ssif_bmc_aspeed.c
Signature · 1 line
2.28.0
#1 lore raw

PATCH 1/3 i2c: i2c-core-smbus: Expose PEC calculate function for generic use

Expose the PEC calculation i2c_smbus_pec() for generic use.

---
 drivers/i2c/i2c-core-smbus.c | 12 ++++++++++--
 include/linux/i2c.h          |  1 +
 2 files changed, 11 insertions(+), 2 deletions(-)
drivers/i2c/i2c-core-smbus.c +10 -2
index d2d32c0fd8c3..e5b2d1465e7e 100644
@@ -37,8 +37,15 @@ static u8 crc8(u16 data)
 	return (u8)(data >> 8);
 }
 
-/* Incremental CRC8 over count bytes in the array pointed to by p */
-static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count)
+/**
+ * i2c_smbus_pec - Incremental CRC8 over the given input data array
+ * @crc: previous return crc8 value
+ * @p: pointer to data buffer.
+ * @count: number of bytes in data buffer.
+ *
+ * Incremental CRC8 over count bytes in the array pointed to by p
+ */
+u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count)
 {
 	int i;
 
@@ -46,6 +53,7 @@ static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count)
 		crc = crc8((crc ^ p[i]) << 8);
 	return crc;
 }
+EXPORT_SYMBOL(i2c_smbus_pec);
 
 /* Assume a 7-bit address, which is reasonable for SMBus */
 static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg)
include/linux/i2c.h +1 -0
index 56622658b215..0d75e5bcdde6 100644
@@ -144,6 +144,7 @@ s32 __i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr,
 /* Now follow the 'nice' access routines. These also document the calling
    conventions of i2c_smbus_xfer. */
 
+u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count);
 s32 i2c_smbus_read_byte(const struct i2c_client *client);
 s32 i2c_smbus_write_byte(const struct i2c_client *client, u8 value);
 s32 i2c_smbus_read_byte_data(const struct i2c_client *client, u8 command);
Signature · 1 line
2.28.0
#2 lore raw

PATCH 2/3 drivers: char: ipmi: Add Aspeed SSIF BMC driver

In reply to Quan Nguyen

The SMBus system interface (SSIF) IPMI BMC driver can be used to perform in-band IPMI communication with their host in management (BMC) side.

This commits adds support specifically for Aspeed AST2500 which commonly used as Board Management Controllers.

---
 drivers/char/ipmi/Kconfig           |  22 +
 drivers/char/ipmi/Makefile          |   2 +
 drivers/char/ipmi/ssif_bmc.c        | 645 ++++++++++++++++++++++++++++
 drivers/char/ipmi/ssif_bmc.h        |  92 ++++
 drivers/char/ipmi/ssif_bmc_aspeed.c | 132 ++++++
 5 files changed, 893 insertions(+)
 create mode 100644 drivers/char/ipmi/ssif_bmc.c
 create mode 100644 drivers/char/ipmi/ssif_bmc.h
 create mode 100644 drivers/char/ipmi/ssif_bmc_aspeed.c
drivers/char/ipmi/Kconfig +22 -0
index 07847d9a459a..d67fd204409a 100644
@@ -133,6 +133,28 @@ 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"
+	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 ASPEED_SSIF_IPMI_BMC
+	depends on ARCH_ASPEED || COMPILE_TEST
+	depends on I2C
+	select SSIF_IPMI_BMC
+	select I2C_SLAVE
+	tristate "Aspeed SSIF IPMI BMC driver"
+	help
+	  Provides a driver for the SSIF IPMI interface found on
+	  Aspeed AST2500 SoC.
+
+	  The driver implements the BMC side of the SMBus system
+	  interface (SSIF), specific for Aspeed AST2500 SoC.
+
 config IPMB_DEVICE_INTERFACE
 	tristate 'IPMB Interface handler'
 	depends on I2C
drivers/char/ipmi/Makefile +2 -0
index 0822adc2ec41..05b993f7335b 100644
@@ -27,3 +27,5 @@ 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
+obj-$(CONFIG_ASPEED_SSIF_IPMI_BMC) += ssif_bmc_aspeed.o
drivers/char/ipmi/ssif_bmc.c new file +645 -0

645 lines not on this page · raw message

drivers/char/ipmi/ssif_bmc.h new file +92 -0

92 lines not on this page · raw message

drivers/char/ipmi/ssif_bmc_aspeed.c new file +132 -0

132 lines not on this page · raw message

Signature · 1 line
2.28.0
#6 lore raw

PATCH 3/3 bindings: ipmi: Add binding for Aspeed SSIF BMC driver

In reply to Quan Nguyen

Add device tree binding document for the Aspeed SSIF BMC driver.

---
 .../bindings/ipmi/aspeed-ssif-bmc.txt          | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/ipmi/aspeed-ssif-bmc.txt
Documentation/devicetree/bindings/ipmi/aspeed-ssif-bmc.txt new file +18 -0
new file mode 100644
index 000000000000..1616f0188db9
@@ -0,0 +1,18 @@
+# Aspeed SSIF (SMBus system interface) IPMI BMC interface
+
+The Aspeed AST2500 are commonly used as BMCs (Baseboard Management Controllers)
+and the SSIF slave interface can be used to perform in-band IPMI communication
+with their host.
+
+Required properties:
+
+- compatible : should be
+       "aspeed,ast2500-ssif-bmc"
+- reg: I2C address the registers
+
+Example:
+
+       ssif-bmc at 10 {
+               compatible = "aspeed,ast2500-ssif-bmc";
+               reg = <0x10>;
+       };
Signature · 1 line
2.28.0
#3 lore raw

Re: [PATCH v1 2/3] drivers: char: ipmi: Add Aspeed SSIF BMC driver

In reply to Quan Nguyen
I love your patch! Yet something to improve:

Hi Quan,

I love your patch! Yet something to improve:

[auto build test ERROR on char-misc/char-misc-testing]
[also build test ERROR on wsa/i2c/for-next ipmi/for-next v5.12-rc5 next-20210329] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/0day-ci/linux/commits/Quan-Nguyen/Add-Aspeed-SSIF-BMC-driver/20210329-202143
base:   https://git.kernel.org/pub/scm/linux/kernel/git/gregkh/char-misc.git d72260cc7879c72c186900e7c153007a6137ed8e
config: arc-randconfig-r024-20210330 (attached as .config)
compiler: arceb-elf-gcc (GCC) 9.3.0

reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# https://github.com/0day-ci/linux/commit/c640bd37ce66eb1364922be61ac3a46108952438
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Quan-Nguyen/Add-Aspeed-SSIF-BMC-driver/20210329-202143 git checkout c640bd37ce66eb1364922be61ac3a46108952438
# save the attached .config to linux build tree
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-9.3.0 make.cross ARCH=arc

If you fix the issue, kindly add following tag as appropriate

All error/warnings (new ones prefixed by >>):

arceb-elf-ld: lib/stackdepot.o: in function `filter_irq_stacks':
stackdepot.c:(.text+0x3a): undefined reference to `__irqentry_text_start'
arceb-elf-ld: stackdepot.c:(.text+0x3a): undefined reference to `__irqentry_text_start'
arceb-elf-ld: stackdepot.c:(.text+0x5a): undefined reference to `__irqentry_text_end'
arceb-elf-ld: stackdepot.c:(.text+0x5a): undefined reference to `__irqentry_text_end'
arceb-elf-ld: stackdepot.c:(.text+0x62): undefined reference to `__softirqentry_text_start'
arceb-elf-ld: stackdepot.c:(.text+0x62): undefined reference to `__softirqentry_text_start'
arceb-elf-ld: stackdepot.c:(.text+0x6a): undefined reference to `__softirqentry_text_end'
arceb-elf-ld: stackdepot.c:(.text+0x6a): undefined reference to `__softirqentry_text_end'
arceb-elf-ld: drivers/char/ipmi/ssif_bmc.o: in function `ssif_bmc_alloc':

>> ssif_bmc.c:(.text+0x51e): undefined reference to `i2c_slave_register'
>> arceb-elf-ld: ssif_bmc.c:(.text+0x51e): undefined reference to `i2c_slave_register'
   arceb-elf-ld: drivers/char/ipmi/ssif_bmc.o: in function `ssif_bmc_cb':
>> ssif_bmc.c:(.text+0x6b8): undefined reference to `i2c_smbus_pec'
>> arceb-elf-ld: ssif_bmc.c:(.text+0x6b8): undefined reference to `i2c_smbus_pec'

arceb-elf-ld: ssif_bmc.c:(.text+0x6c2): undefined reference to `i2c_smbus_pec'
arceb-elf-ld: ssif_bmc.c:(.text+0x6c2): undefined reference to `i2c_smbus_pec'
arceb-elf-ld: ssif_bmc.c:(.text+0x6d6): undefined reference to `i2c_smbus_pec'
arceb-elf-ld: drivers/char/ipmi/ssif_bmc.o:ssif_bmc.c:(.text+0x6d6): more undefined references to `i2c_smbus_pec' follow
--

>> drivers/char/ipmi/ssif_bmc_aspeed.c:41:6: warning: no previous prototype for 'aspeed_i2c_enable_interrupt' [-Wmissing-prototypes]
      41 | void aspeed_i2c_enable_interrupt(struct aspeed_i2c_bus *bus, unsigned long mask)
         |      ^~~~~~~~~~~~~~~~~~~~~~~~~~~
>> drivers/char/ipmi/ssif_bmc_aspeed.c:49:6: warning: no previous prototype for 'aspeed_i2c_disable_interrupt' [-Wmissing-prototypes]
      49 | void aspeed_i2c_disable_interrupt(struct aspeed_i2c_bus *bus, unsigned long mask)
         |      ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
>> drivers/char/ipmi/ssif_bmc_aspeed.c:57:6: warning: no previous prototype for 'aspeed_set_ssif_bmc_status' [-Wmissing-prototypes]

57 | void aspeed_set_ssif_bmc_status(struct ssif_bmc_ctx *ssif_bmc, unsigned int status)
| ^~~~~~~~~~~~~~~~~~~~~~~~~~

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all at lists.01.org
-------------- next part --------------
A non-text attachment was scrubbed...
Name: .config.gz
Type: application/gzip
Size: 31109 bytes
Desc: not available
URL: <http://lists.ozlabs.org/pipermail/linux-aspeed/attachments/20210330/c7e876c6/attachment-0001.gz>

maintainer
#7 lore raw

Re: [PATCH v1 3/3] bindings: ipmi: Add binding for Aspeed SSIF BMC driver

In reply to Quan Nguyen
Quoted 5 lines · Quan Nguyen

Add device tree binding document for the Aspeed SSIF BMC driver.

---
 .../bindings/ipmi/aspeed-ssif-bmc.txt          | 18 ++++++++++++++++++

Bindings should now be in DT schema format.

Quoted 30 lines
 1 file changed, 18 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/ipmi/aspeed-ssif-bmc.txt
Documentation/devicetree/bindings/ipmi/aspeed-ssif-bmc.txt new file +18 -0
new file mode 100644
index 000000000000..1616f0188db9
@@ -0,0 +1,18 @@
+# Aspeed SSIF (SMBus system interface) IPMI BMC interface
+
+The Aspeed AST2500 are commonly used as BMCs (Baseboard Management Controllers)
+and the SSIF slave interface can be used to perform in-band IPMI communication
+with their host.
+
+Required properties:
+
+- compatible : should be
+       "aspeed,ast2500-ssif-bmc"
+- reg: I2C address the registers
+
+Example:
+
+       ssif-bmc at 10 {
+               compatible = "aspeed,ast2500-ssif-bmc";
+               reg = <0x10>;
+       };
Signature · 1 line
2.28.0
maintainer
#4 lore raw

Re: [PATCH v1 2/3] drivers: char: ipmi: Add Aspeed SSIF BMC driver

In reply to Quan Nguyen
Quoted 8 lines · Quan Nguyen

The SMBus system interface (SSIF) IPMI BMC driver can be used to perform in-band IPMI communication with their host in management (BMC) side.

This commits adds support specifically for Aspeed AST2500 which commonly used as Board Management Controllers.

I don't have any SSIF or IPMI related feedback on your patch, but some general things I noticed when reading it.

Quoted 10 lines
---
 drivers/char/ipmi/Kconfig           |  22 +
 drivers/char/ipmi/Makefile          |   2 +
 drivers/char/ipmi/ssif_bmc.c        | 645 ++++++++++++++++++++++++++++
 drivers/char/ipmi/ssif_bmc.h        |  92 ++++
 drivers/char/ipmi/ssif_bmc_aspeed.c | 132 ++++++
 5 files changed, 893 insertions(+)
 create mode 100644 drivers/char/ipmi/ssif_bmc.c
 create mode 100644 drivers/char/ipmi/ssif_bmc.h
 create mode 100644 drivers/char/ipmi/ssif_bmc_aspeed.c

It would make sense to split the aspeed implementation into a separate patch form the ssif framework.

Quoted 10 lines

+++ b/drivers/char/ipmi/ssif_bmc.c

drivers/char/ipmi/ssif_bmc.c new file +8 -0
@@ -0,0 +1,645 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * The driver for BMC side of SSIF interface
+ *
+ * Copyright (c) 2021, Ampere Computing LLC
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as

drivers/char/ipmi/ssif_bmc.c line 18

Quoted 10 lines
drivers/char/ipmi/ssif_bmc.c new file +10 -0
+ * published by the Free Software Foundation; either version 2 of
+ * the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <https://www.gnu.org/licenses/>.

You should omit the licence text; it is replaced by the SPDX tags.

Quoted 4 lines
drivers/char/ipmi/ssif_bmc.c new file +4 -0
+static int send_ssif_bmc_response(struct ssif_bmc_ctx *ssif_bmc, bool non_blocking)
+{
+       unsigned long flags;
+       int ret;

drivers/char/ipmi/ssif_bmc.c line 49

Quoted 10 lines
drivers/char/ipmi/ssif_bmc.c new file +10 -0
+
+       if (!non_blocking) {
+retry:
+               ret = wait_event_interruptible(ssif_bmc->wait_queue,
+                                              !ssif_bmc->response_in_progress);
+               if (ret)
+                       return ret;
+       }
+
+       spin_lock_irqsave(&ssif_bmc->lock, flags);

What's the lock doing here? We've just waited for response_in_progress to be false, so we then take the lock to check what value it is? Should it also be sending some data in this function?

drivers/char/ipmi/ssif_bmc.c line 55

Quoted 6 lines
drivers/char/ipmi/ssif_bmc.c new file +6 -0
+       if (ssif_bmc->response_in_progress) {
+               spin_unlock_irqrestore(&ssif_bmc->lock, flags);
+               if (non_blocking)
+                       return -EAGAIN;
+
+               goto retry;

The goto threw me, so I tried re-writing it without. Again, I don't quite follow what the spinlock is doing.

while (1) {
    if (blocking) {
        ret = wait_event_interruptible();
        if (ret)
             return ret;
    }

     spin_lock_irqsave()
     if (ssif_bmc->response_in_progress) {
         spin_lock_irqrestore()
         if (!blocking)
             return -EAGAIN;
     } else {
        spin_lock_irqrestore()
        break;
     }
}

drivers/char/ipmi/ssif_bmc.c line 64

Quoted 9 lines
drivers/char/ipmi/ssif_bmc.c new file +9 -0
+       }
+
+       /*
+        * Check the response data length from userspace to determine the type
+        * of the response message whether it is single-part or multi-part.
+        */
+       ssif_bmc->is_singlepart_read =
+               (ssif_msg_len(&ssif_bmc->response) <= (MAX_PAYLOAD_PER_TRANSACTION + 1)) ?
+               true : false; /* 1: byte of length */

I don't follow the 1: byte of length comment, what is it telling me?

The ternary operator is a bit messy here, I'd go for a good old if statement.

Quoted 6 lines
drivers/char/ipmi/ssif_bmc.c new file +6 -0
+
+       ssif_bmc->response_in_progress = true;
+       spin_unlock_irqrestore(&ssif_bmc->lock, flags);
+
+       return 0;
+}

drivers/char/ipmi/ssif_bmc.c line 108

Quoted 10 lines
drivers/char/ipmi/ssif_bmc.c new file +9 -0
 
+/* 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 ssif_msg msg;
+       unsigned long flags;
+       ssize_t ret;
+
+       mutex_lock(&ssif_bmc->file_mutex);

->file_mutex is protecting the device against more than one user of the character device? Can you enforce that in open() instead?

drivers/char/ipmi/ssif_bmc.c line 115

Quoted 7 lines
drivers/char/ipmi/ssif_bmc.c new file +7 -0
+
+       ret = receive_ssif_bmc_request(ssif_bmc, file->f_flags & O_NONBLOCK);
+       if (ret < 0)
+               goto out;
+
+       spin_lock_irqsave(&ssif_bmc->lock, flags);
+       count = min_t(ssize_t, count, ssif_msg_len(&ssif_bmc->request));

count is user controlled, so I assume ssif_msg_len will always be less than or equal to struct ssif_msg?

Quoted 21 lines
drivers/char/ipmi/ssif_bmc.c new file +21 -0
+       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);
+out:
+       mutex_unlock(&ssif_bmc->file_mutex);
+
+       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 ssif_msg msg;
+       unsigned long flags;
+       ssize_t ret;
+
+       if (count > sizeof(struct ssif_msg))

drivers/char/ipmi/ssif_bmc.c line 146

Quoted 10 lines
drivers/char/ipmi/ssif_bmc.c new file +10 -0
+               return -EINVAL;
+
+       mutex_lock(&ssif_bmc->file_mutex);
+
+       ret = copy_from_user(&msg, buf, count);
+       if (ret)
+               goto out;
+
+       spin_lock_irqsave(&ssif_bmc->lock, flags);
+       if (count >= ssif_msg_len(&ssif_bmc->response))

Is that test correct?

Quoted 9 lines
drivers/char/ipmi/ssif_bmc.c new file +9 -0
+               memcpy(&ssif_bmc->response, &msg, count);
+       else
+               ret = -EINVAL;
+       spin_unlock_irqrestore(&ssif_bmc->lock, flags);
+
+       if (ret)
+               goto out;
+
+       ret = send_ssif_bmc_response(ssif_bmc, file->f_flags & O_NONBLOCK);

drivers/char/ipmi/ssif_bmc.c line 165

Quoted 10 lines
drivers/char/ipmi/ssif_bmc.c new file +10 -0
+       if (!ret && ssif_bmc->set_ssif_bmc_status)
+               ssif_bmc->set_ssif_bmc_status(ssif_bmc, SSIF_BMC_READY);
+out:
+       mutex_unlock(&ssif_bmc->file_mutex);
+
+       return (ret < 0) ? ret : count;
+}
+
+static long ssif_bmc_ioctl(struct file *file, unsigned int cmd, unsigned long param)
+{

If you're not using this I suspect you should omit the callback.

Quoted 25 lines
drivers/char/ipmi/ssif_bmc.c new file +25 -0
+       return 0;
+}
+
+static unsigned int ssif_bmc_poll(struct file *file, poll_table *wait)
+{
+       struct ssif_bmc_ctx *ssif_bmc = to_ssif_bmc(file);
+       unsigned int mask = 0;
+
+       mutex_lock(&ssif_bmc->file_mutex);
+       poll_wait(file, &ssif_bmc->wait_queue, wait);
+
+       /*
+        * The request message is now available so userspace application can
+        * get the request
+        */
+       if (ssif_bmc->request_available)
+               mask |= POLLIN;
+
+       mutex_unlock(&ssif_bmc->file_mutex);
+       return mask;
+}
+
+/*
+ * System calls to device interface for user apps
+ */

drivers/char/ipmi/ssif_bmc.c line 200

Quoted 10 lines
drivers/char/ipmi/ssif_bmc.c new file +10 -0
+static const struct file_operations ssif_bmc_fops = {
+       .owner          = THIS_MODULE,
+       .read           = ssif_bmc_read,
+       .write          = ssif_bmc_write,
+       .poll           = ssif_bmc_poll,
+       .unlocked_ioctl = ssif_bmc_ioctl,
+};
+
+/* Called with ssif_bmc->lock held. */
+static int handle_request(struct ssif_bmc_ctx *ssif_bmc)

Could return void.

Quoted 7 lines
drivers/char/ipmi/ssif_bmc.c new file +7 -0
+{
+       if (ssif_bmc->set_ssif_bmc_status)
+               ssif_bmc->set_ssif_bmc_status(ssif_bmc, SSIF_BMC_BUSY);
+
+       /* Request message is available to process */
+       ssif_bmc->request_available = true;
+       /*

drivers/char/ipmi/ssif_bmc.c line 217

Quoted 10 lines
drivers/char/ipmi/ssif_bmc.c new file +10 -0
+        * This is the new READ request.
+        * Clear the response buffer of the previous transaction
+        */
+       memset(&ssif_bmc->response, 0, sizeof(struct ssif_msg));
+       wake_up_all(&ssif_bmc->wait_queue);
+       return 0;
+}
+
+/* Called with ssif_bmc->lock held. */
+static int complete_response(struct ssif_bmc_ctx *ssif_bmc)

Could return void.

Quoted 7 lines
drivers/char/ipmi/ssif_bmc.c new file +7 -0
+{
+       /* 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;
+       memset(&ssif_bmc->response_buf, 0, MAX_PAYLOAD_PER_TRANSACTION);

drivers/char/ipmi/ssif_bmc.c

Quoted 10 lines
drivers/char/ipmi/ssif_bmc.c new file +9 -0
+       wake_up_all(&ssif_bmc->wait_queue);
+       return 0;
+}
+
+static void set_multipart_response_buffer(struct ssif_bmc_ctx *ssif_bmc, u8 *val)
+{
 
+       default:
+               /* Do not expect to go to this case */
+               pr_err("Error: Unexpected SMBus command received 0x%x\n", ssif_bmc->smbus_cmd);

Use dev_err if you can, so the message is associated with the device.

Quoted 6 lines
drivers/char/ipmi/ssif_bmc.c new file +6 -0
+               break;
+       }
+
+       ssif_bmc->nbytes_processed += response_len;
+}
+
#5 lore raw

Re: [PATCH v1 2/3] drivers: char: ipmi: Add Aspeed SSIF BMC driver

Quoted 5 lines · Joel Stanley
Quoted 4 lines · Quan Nguyen

The SMBus system interface (SSIF) IPMI BMC driver can be used to perform in-band IPMI communication with their host in management (BMC) side.

Quoted 8 lines
Quoted 4 lines

This commits adds support specifically for Aspeed AST2500 which commonly used as Board Management Controllers.

I don't have any SSIF or IPMI related feedback on your patch, but some general things I noticed when reading it.

Thank you, Joel,
I'm really appreciate your comments for this patch series.

And, as there is a compilation error detected by kernel robot test, I was hurry to send out v2 just to fix that just before your email arrived. So I'd like to address all comments in my upcoming v3.

Quoted 11 lines
Quoted 10 lines
---
  drivers/char/ipmi/Kconfig           |  22 +
  drivers/char/ipmi/Makefile          |   2 +
  drivers/char/ipmi/ssif_bmc.c        | 645 ++++++++++++++++++++++++++++
  drivers/char/ipmi/ssif_bmc.h        |  92 ++++
  drivers/char/ipmi/ssif_bmc_aspeed.c | 132 ++++++
  5 files changed, 893 insertions(+)

create mode 100644 drivers/char/ipmi/ssif_bmc.c create mode 100644 drivers/char/ipmi/ssif_bmc.h create mode 100644 drivers/char/ipmi/ssif_bmc_aspeed.c

Quoted 3 lines

It would make sense to split the aspeed implementation into a separate patch form the ssif framework.

Yes, will do in next version

Quoted 14 lines
Quoted 14 lines

+++ b/drivers/char/ipmi/ssif_bmc.c

drivers/char/ipmi/ssif_bmc.c new file +12 -0
@@ -0,0 +1,645 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * The driver for BMC side of SSIF interface
+ *
+ * Copyright (c) 2021, Ampere Computing LLC
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of
+ * the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
Quoted 9 lines

drivers/char/ipmi/ssif_bmc.c line 18

Quoted 6 lines
drivers/char/ipmi/ssif_bmc.c new file +6 -0
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <https://www.gnu.org/licenses/>.

You should omit the licence text; it is replaced by the SPDX tags.

My bad, will remove in next version

Quoted 10 lines
Quoted 10 lines
drivers/char/ipmi/ssif_bmc.c new file +10 -0
+static int send_ssif_bmc_response(struct ssif_bmc_ctx *ssif_bmc, bool non_blocking)
+{
+       unsigned long flags;
+       int ret;
+
+       if (!non_blocking) {
+retry:
+               ret = wait_event_interruptible(ssif_bmc->wait_queue,
+                                              !ssif_bmc->response_in_progress);
+               if (ret)
Quoted 9 lines

drivers/char/ipmi/ssif_bmc.c line 49

Quoted 4 lines
drivers/char/ipmi/ssif_bmc.c new file +4 -0
+                       return ret;
+       }
+
+       spin_lock_irqsave(&ssif_bmc->lock, flags);

What's the lock doing here? We've just waited for response_in_progress to be false, so we then take the lock to check what value it is? Should it also be sending some data in this function?

The lock is to make sure ssif_bmc->response_in_progress are completely processed, ie: when the lock already released.
My concern is that reference to that value without acquiring lock may not be true as it is under other possess.

In fact, the lock here is for the whole data pointed by ssif_bmc pointer. Hence, every reference/modify to this data must be done with the lock acquired.

Quoted 19 lines

drivers/char/ipmi/ssif_bmc.c line 55

Quoted 6 lines
drivers/char/ipmi/ssif_bmc.c new file +6 -0
+       if (ssif_bmc->response_in_progress) {
+               spin_unlock_irqrestore(&ssif_bmc->lock, flags);
+               if (non_blocking)
+                       return -EAGAIN;
+
+               goto retry;

The goto threw me, so I tried re-writing it without. Again, I don't quite follow what the spinlock is doing.

while (1) {
     if (blocking) {
         ret = wait_event_interruptible();
         if (ret)
              return ret;
     }

      spin_lock_irqsave()
      if (ssif_bmc->response_in_progress) {
Quoted 10 lines
          spin_lock_irqrestore()
          if (!blocking)
              return -EAGAIN;
      } else {
         spin_lock_irqrestore()
         break;
      }
}

I'm afraid we would need to re-acquire the lock before modifying ssif_bmc->is_singlepart_read and ssif_bmc->response_in_progress below.

Quoted 10 lines

drivers/char/ipmi/ssif_bmc.c line 64

Quoted 9 lines
drivers/char/ipmi/ssif_bmc.c new file +9 -0
+       }
+
+       /*
+        * Check the response data length from userspace to determine the type
+        * of the response message whether it is single-part or multi-part.
+        */
+       ssif_bmc->is_singlepart_read =
+               (ssif_msg_len(&ssif_bmc->response) <= (MAX_PAYLOAD_PER_TRANSACTION + 1)) ?
+               true : false; /* 1: byte of length */
Quoted 4 lines

I don't follow the 1: byte of length comment, what is it telling me?

The ternary operator is a bit messy here, I'd go for a good old if statement.

The comment indeed does not provide any info here. Will change back to if else statement and add more meaningful comment if necessary in next version.

Quoted 7 lines
Quoted 6 lines
drivers/char/ipmi/ssif_bmc.c new file +6 -0
+
+       ssif_bmc->response_in_progress = true;
+       spin_unlock_irqrestore(&ssif_bmc->lock, flags);
+
+       return 0;
+}
Quoted 13 lines

drivers/char/ipmi/ssif_bmc.c line 108

Quoted 9 lines
drivers/char/ipmi/ssif_bmc.c new file +9 -0
+/* 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 ssif_msg msg;
+       unsigned long flags;
+       ssize_t ret;
+
+       mutex_lock(&ssif_bmc->file_mutex);

->file_mutex is protecting the device against more than one user of the character device? Can you enforce that in open() instead?

The current use is to serialize access among read()/write()/poll().

About enforcing open() to return -EBUSY to protect the device against more than one user, we can either implement follow example of drivers/char/ipmi/kcs_bmc.c OR drivers/char/ipmi/bt-bmc.c

But I'm still wonder what should be better between atomic_t open_count (bt_bmc.c) or simply reuse the file_mutex similar with kcs_bmc.c.

Quoted 11 lines

drivers/char/ipmi/ssif_bmc.c line 115

Quoted 7 lines
drivers/char/ipmi/ssif_bmc.c new file +7 -0
+
+       ret = receive_ssif_bmc_request(ssif_bmc, file->f_flags & O_NONBLOCK);
+       if (ret < 0)
+               goto out;
+
+       spin_lock_irqsave(&ssif_bmc->lock, flags);
+       count = min_t(ssize_t, count, ssif_msg_len(&ssif_bmc->request));

count is user controlled, so I assume ssif_msg_len will always be less than or equal to struct ssif_msg?

The size of struct ssif_msg is 255 bytes, and the ssif_msg_len() should always return less than this number unless the ssif_msg->len is wrong and exceeded the size of struct ssif_msg.

For safe I think it should be changed to the min among count, sizeof(struct ssif_msg), and return value of ssif_msg_len(&ssif_bmc->request).
ie:
count = min_t(ssize_t, count, min_t(ssize_t, sizeof(struct ssif_msg), ssif_msg_len(&ssif_bmc->request));

Quoted 25 lines
Quoted 25 lines
drivers/char/ipmi/ssif_bmc.c new file +25 -0
+       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);
+out:
+       mutex_unlock(&ssif_bmc->file_mutex);
+
+       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 ssif_msg msg;
+       unsigned long flags;
+       ssize_t ret;
+
+       if (count > sizeof(struct ssif_msg))
+               return -EINVAL;
+
+       mutex_lock(&ssif_bmc->file_mutex);
+
Quoted 9 lines

drivers/char/ipmi/ssif_bmc.c line 146

Quoted 6 lines
drivers/char/ipmi/ssif_bmc.c new file +6 -0
+       ret = copy_from_user(&msg, buf, count);
+       if (ret)
+               goto out;
+
+       spin_lock_irqsave(&ssif_bmc->lock, flags);
+       if (count >= ssif_msg_len(&ssif_bmc->response))

Is that test correct?

As the "if (count > sizeof(struct ssif_msg))" above ensure the data will not exceeded the size of buffer, ie: sizeof ssif_msg, we can now to make sure the actually data size to agree with ssif_msg->len, ie: the return of ssif_msg_len().

But as this test looked "not easy to read" so I think I will fix this to:

    if (count < ssif_msg_len(&ssif_bmc->response))
         ret = -EINVAL;
    else
         memcpy(...);

Hope this way made it easier to read.

Quoted 13 lines
Quoted 13 lines
drivers/char/ipmi/ssif_bmc.c new file +13 -0
+               memcpy(&ssif_bmc->response, &msg, count);
+       else
+               ret = -EINVAL;
+       spin_unlock_irqrestore(&ssif_bmc->lock, flags);
+
+       if (ret)
+               goto out;
+
+       ret = send_ssif_bmc_response(ssif_bmc, file->f_flags & O_NONBLOCK);
+       if (!ret && ssif_bmc->set_ssif_bmc_status)
+               ssif_bmc->set_ssif_bmc_status(ssif_bmc, SSIF_BMC_READY);
+out:
+       mutex_unlock(&ssif_bmc->file_mutex);
Quoted 9 lines

drivers/char/ipmi/ssif_bmc.c line 165

Quoted 6 lines
drivers/char/ipmi/ssif_bmc.c new file +6 -0
+
+       return (ret < 0) ? ret : count;
+}
+
+static long ssif_bmc_ioctl(struct file *file, unsigned int cmd, unsigned long param)
+{

If you're not using this I suspect you should omit the callback.

Will remove this in next version

Quoted 29 lines
Quoted 29 lines
drivers/char/ipmi/ssif_bmc.c new file +29 -0
+       return 0;
+}
+
+static unsigned int ssif_bmc_poll(struct file *file, poll_table *wait)
+{
+       struct ssif_bmc_ctx *ssif_bmc = to_ssif_bmc(file);
+       unsigned int mask = 0;
+
+       mutex_lock(&ssif_bmc->file_mutex);
+       poll_wait(file, &ssif_bmc->wait_queue, wait);
+
+       /*
+        * The request message is now available so userspace application can
+        * get the request
+        */
+       if (ssif_bmc->request_available)
+               mask |= POLLIN;
+
+       mutex_unlock(&ssif_bmc->file_mutex);
+       return mask;
+}
+
+/*
+ * System calls to device interface for user apps
+ */
+static const struct file_operations ssif_bmc_fops = {
+       .owner          = THIS_MODULE,
+       .read           = ssif_bmc_read,
+       .write          = ssif_bmc_write,
Quoted 9 lines

drivers/char/ipmi/ssif_bmc.c line 200

Quoted 6 lines
drivers/char/ipmi/ssif_bmc.c new file +6 -0
+       .poll           = ssif_bmc_poll,
+       .unlocked_ioctl = ssif_bmc_ioctl,
+};
+
+/* Called with ssif_bmc->lock held. */
+static int handle_request(struct ssif_bmc_ctx *ssif_bmc)

Could return void.

Will do in next version

Quoted 11 lines
Quoted 11 lines
drivers/char/ipmi/ssif_bmc.c new file +11 -0
+{
+       if (ssif_bmc->set_ssif_bmc_status)
+               ssif_bmc->set_ssif_bmc_status(ssif_bmc, SSIF_BMC_BUSY);
+
+       /* Request message is available to process */
+       ssif_bmc->request_available = true;
+       /*
+        * This is the new READ request.
+        * Clear the response buffer of the previous transaction
+        */
+       memset(&ssif_bmc->response, 0, sizeof(struct ssif_msg));
Quoted 9 lines

drivers/char/ipmi/ssif_bmc.c line 217

Quoted 6 lines
drivers/char/ipmi/ssif_bmc.c new file +6 -0
+       wake_up_all(&ssif_bmc->wait_queue);
+       return 0;
+}
+
+/* Called with ssif_bmc->lock held. */
+static int complete_response(struct ssif_bmc_ctx *ssif_bmc)

Could return void.

Will do in next version

Quoted 14 lines
Quoted 13 lines
drivers/char/ipmi/ssif_bmc.c new file +13 -0
+{
+       /* 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;
+       memset(&ssif_bmc->response_buf, 0, MAX_PAYLOAD_PER_TRANSACTION);
+       wake_up_all(&ssif_bmc->wait_queue);
+       return 0;
+}
+
+static void set_multipart_response_buffer(struct ssif_bmc_ctx *ssif_bmc, u8 *val)
+{
Quoted 6 lines

drivers/char/ipmi/ssif_bmc.c line 298

Quoted 3 lines
drivers/char/ipmi/ssif_bmc.c new file +3 -0
+       default:
+               /* Do not expect to go to this case */
+               pr_err("Error: Unexpected SMBus command received 0x%x\n", ssif_bmc->smbus_cmd);

Use dev_err if you can, so the message is associated with the device.

Will try to use dev_err() in next version

Quoted 6 lines
Quoted 6 lines
drivers/char/ipmi/ssif_bmc.c new file +6 -0
+               break;
+       }
+
+       ssif_bmc->nbytes_processed += response_len;
+}
+