From: Jonathan Cameron <Jonathan.Cameron@huawei.com>
To: Klaus Jensen <k.jensen@samsung.com>, <cminyard@mvista.com>,
Fan Ni <fan.ni@samsung.com>, Anisa Su <anisa.su@samsung.com>,
<qemu-devel@nongnu.org>, <linux-cxl@vger.kernel.org>,
<mst@redhat.com>
Cc: linuxarm@huawei.com, "Philippe Mathieu-Daudé" <philmd@linaro.org>
Subject: [RFC PATCH qemu 1/5] hw/i2c: add smbus pec utility function
Date: Mon, 9 Jun 2025 17:33:29 +0100 [thread overview]
Message-ID: <20250609163334.922346-2-Jonathan.Cameron@huawei.com> (raw)
In-Reply-To: <20250609163334.922346-1-Jonathan.Cameron@huawei.com>
From: Klaus Jensen <k.jensen@samsung.com>
Add i2c_smbus_pec() to calculate the SMBus Packet Error Code for a
message.
Reviewed-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
Signed-off-by: Klaus Jensen <k.jensen@samsung.com>
Acked-by: Corey Minyard <cminyard@mvista.com>
Link: https://lore.kernel.org/r/20230914-nmi-i2c-v6-1-11bbb4f74d18@samsung.com
Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
---
include/hw/i2c/smbus_master.h | 2 ++
hw/i2c/smbus_master.c | 26 ++++++++++++++++++++++++++
2 files changed, 28 insertions(+)
diff --git a/include/hw/i2c/smbus_master.h b/include/hw/i2c/smbus_master.h
index bb13bc423c..d90f81767d 100644
--- a/include/hw/i2c/smbus_master.h
+++ b/include/hw/i2c/smbus_master.h
@@ -27,6 +27,8 @@
#include "hw/i2c/i2c.h"
+uint8_t i2c_smbus_pec(uint8_t crc, uint8_t *buf, size_t len);
+
/* Master device commands. */
int smbus_quick_command(I2CBus *bus, uint8_t addr, int read);
int smbus_receive_byte(I2CBus *bus, uint8_t addr);
diff --git a/hw/i2c/smbus_master.c b/hw/i2c/smbus_master.c
index 6a53c34e70..01a8e47002 100644
--- a/hw/i2c/smbus_master.c
+++ b/hw/i2c/smbus_master.c
@@ -15,6 +15,32 @@
#include "hw/i2c/i2c.h"
#include "hw/i2c/smbus_master.h"
+static uint8_t crc8(uint16_t data)
+{
+ int i;
+
+ for (i = 0; i < 8; i++) {
+ if (data & 0x8000) {
+ data ^= 0x1070U << 3;
+ }
+
+ data <<= 1;
+ }
+
+ return (uint8_t)(data >> 8);
+}
+
+uint8_t i2c_smbus_pec(uint8_t crc, uint8_t *buf, size_t len)
+{
+ int i;
+
+ for (i = 0; i < len; i++) {
+ crc = crc8((crc ^ buf[i]) << 8);
+ }
+
+ return crc;
+}
+
/* Master device commands. */
int smbus_quick_command(I2CBus *bus, uint8_t addr, int read)
{
--
2.48.1
next prev parent reply other threads:[~2025-06-09 16:34 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-06-09 16:33 [RFC PATCH qemu 0/5] hw/cxl/mctp/i2c/usb: MCTP for OoB control of CXL devices Jonathan Cameron
2025-06-09 16:33 ` Jonathan Cameron [this message]
2025-06-09 16:33 ` [RFC PATCH qemu 2/5] hw/i2c: add mctp core Jonathan Cameron
2025-06-09 16:33 ` [RFC PATCH qemu 3/5] hw/cxl/i2c_mctp_cxl: Initial device emulation Jonathan Cameron
2025-06-10 16:39 ` Jonathan Cameron
2025-06-09 16:33 ` [RFC PATCH qemu 4/5] docs: cxl: Add example commandline for MCTP CXL CCIs Jonathan Cameron
2025-06-09 16:33 ` [RFC PATCH qemu 5/5] usb/mctp/cxl: CXL FMAPI interface via MCTP over usb Jonathan Cameron
2025-06-24 19:47 ` [RFC PATCH qemu 0/5] hw/cxl/mctp/i2c/usb: MCTP for OoB control of CXL devices Anisa Su
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20250609163334.922346-2-Jonathan.Cameron@huawei.com \
--to=jonathan.cameron@huawei.com \
--cc=anisa.su@samsung.com \
--cc=cminyard@mvista.com \
--cc=fan.ni@samsung.com \
--cc=k.jensen@samsung.com \
--cc=linux-cxl@vger.kernel.org \
--cc=linuxarm@huawei.com \
--cc=mst@redhat.com \
--cc=philmd@linaro.org \
--cc=qemu-devel@nongnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox