All of lore.kernel.org
 help / color / mirror / Atom feed
From: Klaus Jensen <its@irrelevant.dk>
To: Corey Minyard <cminyard@mvista.com>,
	Paolo Bonzini <pbonzini@redhat.com>,
	 Peter Maydell <peter.maydell@linaro.org>,
	Jason Wang <jasowang@redhat.com>,
	 Keith Busch <kbusch@kernel.org>
Cc: Lior Weintraub <liorw@pliops.com>,
	Jeremy Kerr <jk@codeconstruct.com.au>,
	 Andrew Jeffery <andrew@codeconstruct.com.au>,
	 Matt Johnston <matt@codeconstruct.com.au>,
	Peter Delevoryas <peter@pjd.dev>,
	 Jonathan Cameron <Jonathan.Cameron@huawei.com>,
	 Klaus Jensen <its@irrelevant.dk>,
	qemu-devel@nongnu.org,  qemu-arm@nongnu.org,
	qemu-block@nongnu.org,  Klaus Jensen <k.jensen@samsung.com>
Subject: [PATCH v6 1/3] hw/i2c: add smbus pec utility function
Date: Thu, 14 Sep 2023 11:53:41 +0200	[thread overview]
Message-ID: <20230914-nmi-i2c-v6-1-11bbb4f74d18@samsung.com> (raw)
In-Reply-To: <20230914-nmi-i2c-v6-0-11bbb4f74d18@samsung.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>
---
 hw/i2c/smbus_master.c         | 26 ++++++++++++++++++++++++++
 include/hw/i2c/smbus_master.h |  2 ++
 2 files changed, 28 insertions(+)

diff --git a/hw/i2c/smbus_master.c b/hw/i2c/smbus_master.c
index 6a53c34e70b7..01a8e4700222 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)
 {
diff --git a/include/hw/i2c/smbus_master.h b/include/hw/i2c/smbus_master.h
index bb13bc423c22..d90f81767d86 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);

-- 
2.42.0



  reply	other threads:[~2023-09-14  9:55 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-09-14  9:53 [PATCH v6 0/3] hw/{i2c,nvme}: mctp endpoint, nvme management interface model Klaus Jensen
2023-09-14  9:53 ` Klaus Jensen [this message]
2023-09-14 20:56   ` [PATCH v6 1/3] hw/i2c: add smbus pec utility function Corey Minyard
2023-09-21  7:11   ` Andrew Jeffery
2023-09-14  9:53 ` [PATCH v6 2/3] hw/i2c: add mctp core Klaus Jensen
2023-09-14 20:56   ` Corey Minyard
2023-09-21  7:10   ` Andrew Jeffery
2023-09-14  9:53 ` [PATCH v6 3/3] hw/nvme: add nvme management interface model Klaus Jensen
2023-09-14 21:01   ` Corey Minyard
2023-09-21  7:08   ` Andrew Jeffery
2023-09-20 11:48 ` [PATCH v6 0/3] hw/{i2c,nvme}: mctp endpoint, " Jonathan Cameron via
2023-09-20 12:54   ` Corey Minyard
2023-09-20 13:31     ` Klaus Jensen
2023-09-20 14:36       ` Corey Minyard

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=20230914-nmi-i2c-v6-1-11bbb4f74d18@samsung.com \
    --to=its@irrelevant.dk \
    --cc=Jonathan.Cameron@huawei.com \
    --cc=andrew@codeconstruct.com.au \
    --cc=cminyard@mvista.com \
    --cc=jasowang@redhat.com \
    --cc=jk@codeconstruct.com.au \
    --cc=k.jensen@samsung.com \
    --cc=kbusch@kernel.org \
    --cc=liorw@pliops.com \
    --cc=matt@codeconstruct.com.au \
    --cc=pbonzini@redhat.com \
    --cc=peter.maydell@linaro.org \
    --cc=peter@pjd.dev \
    --cc=qemu-arm@nongnu.org \
    --cc=qemu-block@nongnu.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.