hw/{i2c, nvme}: mctp endpoint, nvme management interface model
Subsystems (3)
v3 cover hw/{i2c, nvme}: mctp endpoint, nvme management interface model
From: Klaus Jensen <[email protected]>
This adds a generic MCTP endpoint model that other devices may derive from. I'm not 100% happy with the design of the class methods, but it's a start.
Also included is a very basic implementation of an NVMe-MI device, supporting only a small subset of the required commands. Lior (CC'ed) has some patches coming up that adds futher support.
Since this all relies on i2c target mode, this can currently only be used with an SoC that includes the Aspeed I2C controller.
The easiest way to get up and running with this, is to grab my buildroot overlay[1]. It includes modified a modified dts as well as a couple of required packages.
QEMU can then be launched along these lines:
qemu-system-arm \
-nographic \
-M ast2600-evb \
-kernel output/images/zImage \
-initrd output/images/rootfs.cpio \
-dtb output/images/aspeed-ast2600-evb-nmi.dtb \
-nic user,hostfwd=tcp::2222-:22 \
-device nmi-i2c,address=0x3a \
-serial mon:stdioFrom within the booted system,
mctp addr add 8 dev mctpi2c15
mctp link set mctpi2c15 up
mctp route add 9 via mctpi2c15
mctp neigh add 9 dev mctpi2c15 lladdr 0x3a
mi-mctp 1 9 info
Comments are very welcome!
[1]: https://github.com/birkelund/buildroots/tree/main/mctp-i2c
Changes since v2
~~~~~~~~~~~~~~~~
- Applied a bunch of feedback from Jonathan:
+ Moved a lot of internally used structs out of the include headers and into the source files.
+ Added spec references in various places
+ Split the patch for i2c_smbus_pec() into its own
+ Fix a compile error (and bug) in nmi-i2c.c.
- From Corey:
+ Reworked the buffer handling. The deriving devices now returns a pointer to their own buffer that the mctp core copies into.
+ Added a couple of extra debugging trace events.
Changes since v1
~~~~~~~~~~~~~~~~
- Fix SPDX-License tag for hw/nvme/nmi-i2c.c (Philippe)
- Add some asserts to verify buffer indices (by request from Corey).
- Drop short packets that could result in underflow (Corey)
- Move i2c_smbus_pec() to smbus common code (Corey)
- A couple of logic fixes (patch from Jeremy squashed in)
- Added a patch to handle messages with dest eid 0 (Matt) Maybe squash this as well.
Klaus Jensen (3):
hw/i2c: add smbus pec utility function
hw/i2c: add mctp core
hw/nvme: add nvme management interface model
MAINTAINERS | 7 + hw/arm/Kconfig | 1 + hw/i2c/Kconfig | 4 + hw/i2c/mctp.c | 398 ++++++++++++++++++++++++++++++++++ hw/i2c/meson.build | 1 + hw/i2c/smbus_master.c | 28 +++ hw/i2c/trace-events | 13 ++ hw/nvme/meson.build | 1 + hw/nvme/nmi-i2c.c | 367 +++++++++++++++++++++++++++++++ hw/nvme/trace-events | 6 + include/hw/i2c/mctp.h | 137 ++++++++++++ include/hw/i2c/smbus_master.h | 2 + include/net/mctp.h | 28 +++ 13 files changed, 993 insertions(+) create mode 100644 hw/i2c/mctp.c create mode 100644 hw/nvme/nmi-i2c.c create mode 100644 include/hw/i2c/mctp.h create mode 100644 include/net/mctp.h
Signature · 1 line
2.40.0
PATCH v3 1/3 hw/i2c: add smbus pec utility function
From: Klaus Jensen <[email protected]>
Add i2c_smbus_pec() to calculate the SMBus Packet Error Code for a message.
--- hw/i2c/smbus_master.c | 28 ++++++++++++++++++++++++++++ include/hw/i2c/smbus_master.h | 2 ++ 2 files changed, 30 insertions(+)
hw/i2c/smbus_master.c +28 -0
@@ -15,6 +15,34 @@#include "hw/i2c/i2c.h" #include "hw/i2c/smbus_master.h" +static uint8_t crc8 (uint16_t data) +{ +#define POLY (0x1070U << 3) +int i; + +for (i =0 ; i <8 ; i++) { +if (data &0x8000 ) { + data = data ^POLY ; + } + + data = data <<1 ; + } + +return (uint8_t )(data >>8 ); +#undef POLY +} + +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) {
include/hw/i2c/smbus_master.h +2 -0
@@ -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);
Signature · 1 line
2.40.0
Re: [PATCH v3 1/3] hw/i2c: add smbus pec utility function
Quoted 6 lines · On Wed, 31 May 2023 13:47:42 +0200 Klaus Jensen <[email protected]> wrote:
From: Klaus Jensen <[email protected]>
Add
i2c_smbus_pec()to calculate the SMBus Packet Error Code for a message.Signed-off-by Klaus Jensen <[email protected]>
LGTM
Quoted 57 lines
--- hw/i2c/smbus_master.c | 28 ++++++++++++++++++++++++++++ include/hw/i2c/smbus_master.h | 2 ++ 2 files changed, 30 insertions(+)hw/i2c/smbus_master.c +28
-0@@ -15,6 +15,34 @@#include "hw/i2c/i2c.h" #include "hw/i2c/smbus_master.h" +static uint8_t crc8 (uint16_t data) +{ +#define POLY (0x1070U << 3) +int i; + +for (i =0 ; i <8 ; i++) { +if (data &0x8000 ) { + data = data ^POLY ; + } + + data = data <<1 ; + } + +return (uint8_t )(data >>8 ); +#undef POLY +} + +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) {include/hw/i2c/smbus_master.h +2
-0@@ -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);
Re: [PATCH v3 1/3] hw/i2c: add smbus pec utility function
In reply to Klaus JensenQuoted 16 lines · Klaus Jensen
From: Klaus Jensen <[email protected]>
Add
i2c_smbus_pec()to calculate the SMBus Packet Error Code for a message.Signed-off-by Klaus Jensen <[email protected]>--- hw/i2c/smbus_master.c | 28 ++++++++++++++++++++++++++++ include/hw/i2c/smbus_master.h | 2 ++ 2 files changed, 30 insertions(+)hw/i2c/smbus_master.c +0
-0
hw/i2c/smbus_master.c line 20
Quoted 6 lines
hw/i2c/smbus_master.c +3
-0@@ -15,6 +15,34 @@#include "hw/i2c/i2c.h" #include "hw/i2c/smbus_master.h" +static uint8_t crc8 (uint16_t data) +{ +#define POLY (0x1070U << 3)
static const unsigned crc8_poly = ..., but why not inline the single use?
data ^= 0x1070U << 3;
and
data <<= 1;
hw/i2c/smbus_master.c line 33
Quoted 13 lines
hw/i2c/smbus_master.c +13
-0+int i; + +for (i =0 ; i <8 ; i++) { +if (data &0x8000 ) { + data = data ^POLY ; + } + + data = data <<1 ; + } + +return (uint8_t )(data >>8 ); +#undef POLY +}
We have "qemu/crc32c.h", maybe we could have a similar crc8.h. Just wondering...
PATCH v3 2/3 hw/i2c: add mctp core
In reply to Klaus JensenFrom: Klaus Jensen <[email protected]>
Add an abstract MCTP over I2C endpoint model. This implements MCTP control message handling as well as handling the actual I2C transport (packetization).
Devices are intended to derive from this and implement the class methods.
Parts of this implementation is inspired by code[1] previously posted by Jonathan Cameron.
Squashed a fix[2] from Matt Johnston.
[1]: https://lore.kernel.org/qemu-devel/[email protected]/
[2]: https://lore.kernel.org/qemu-devel/[email protected]/
--- MAINTAINERS | 7 + hw/arm/Kconfig | 1 + hw/i2c/Kconfig | 4 + hw/i2c/mctp.c | 398 ++++++++++++++++++++++++++++++++++++++++++ hw/i2c/meson.build | 1 + hw/i2c/trace-events | 13 ++ include/hw/i2c/mctp.h | 137 +++++++++++++++ include/net/mctp.h | 28 +++ 8 files changed, 589 insertions(+) create mode 100644 hw/i2c/mctp.c create mode 100644 include/hw/i2c/mctp.h create mode 100644 include/net/mctp.h
MAINTAINERS +7 -0
@@ -3354,6 +3354,13 @@ F: tests/qtest/adm1272-test.c F: tests/qtest/max34451-test.c F: tests/qtest/isl_pmbus_vr-test.c +MCTP I2C Transport +M: Klaus Jensen <[email protected]> +S: Maintained +F: hw/i2c/mctp.c +F: include/hw/i2c/mctp.h +F: include/net/mctp.h + Firmware schema specifications M: Philippe Mathieu-Daudé <[email protected]> R: Daniel P. Berrange <[email protected]>
hw/arm/Kconfig +1 -0
@@ -528,6 +528,7 @@ config ASPEED_SOC select DS1338 select FTGMAC100 select I2C + select MCTP_I2C select DPS310 select PCA9552 select SERIAL
hw/i2c/Kconfig +4 -0
@@ -45,3 +45,7 @@ config PCA954X config PMBUS bool select SMBUS + +config MCTP_I2C + bool + select I2C
hw/i2c/mctp.c new file +398 -0
398 lines not on this page · raw message
hw/i2c/meson.build +1 -0
@@ -1,5 +1,6 @@ i2c_ss = ss.source_set() i2c_ss.add(when: 'CONFIG_I2C', if_true: files('core.c')) +i2c_ss.add(when: 'CONFIG_MCTP_I2C', if_true: files('mctp.c')) i2c_ss.add(when: 'CONFIG_SMBUS', if_true: files('smbus_slave.c', 'smbus_master.c')) i2c_ss.add(when: 'CONFIG_ACPI_SMBUS', if_true: files('pm_smbus.c')) i2c_ss.add(when: 'CONFIG_ACPI_ICH9', if_true: files('smbus_ich9.c'))
hw/i2c/trace-events +13 -0
@@ -45,3 +45,16 @@ npcm7xx_smbus_recv_fifo(const char *id, uint8_t received, uint8_t expected) "%s pca954x_write_bytes(uint8_t value) "PCA954X write data: 0x%02x" pca954x_read_data(uint8_t value) "PCA954X read data: 0x%02x" + +# mctp.c +i2c_mctp_tx_start_send(size_t len) "len %zu" +i2c_mctp_tx_send_byte(size_t pos, uint8_t byte) "pos %zu byte 0x%"PRIx8"" +i2c_mctp_tx_done(void) "packet sent" +i2c_mctp_handle_control(uint8_t command) "command 0x%"PRIx8"" +i2c_mctp_unhandled_control(uint8_t command) "command 0x%"PRIx8"" +i2c_mctp_drop_invalid_length(unsigned byte_count, size_t expected) "byte_count %u expected %zu" +i2c_mctp_drop_invalid_pec(uint8_t pec, uint8_t expected) "pec 0x%"PRIx8" expected 0x%"PRIx8"" +i2c_mctp_drop_invalid_eid(uint8_t eid, uint8_t expected) "eid 0x%"PRIx8" expected 0x%"PRIx8"" +i2c_mctp_drop_invalid_pktseq(uint8_t pktseq, uint8_t expected) "pktseq 0x%"PRIx8" expected 0x%"PRIx8"" +i2c_mctp_drop_short_packet(size_t len) "len %zu" +i2c_mctp_drop_expected_som(void) ""
include/hw/i2c/mctp.h new file +137 -0
137 lines not on this page · raw message
include/net/mctp.h new file +28 -0
@@ -0,0 +1,28 @@ +#ifndef QEMU_MCTP_H +#define QEMU_MCTP_H + +#define MCTP_BASELINE_MTU 64 + +enum { +MCTP_H_FLAGS_EOM =1 <<6 , +MCTP_H_FLAGS_SOM =1 <<7 , +}; + +#define MCTP_MESSAGE_IC (1 << 7) + +/* DSP0236 1.3.0, Figure 4 */ +typedef struct MCTPPacketHeader { +uint8_t version; +struct { +uint8_t dest; +uint8_t source; + } eid; +uint8_t flags; +}MCTPPacketHeader ; + +typedef struct MCTPPacket { +MCTPPacketHeader hdr; +uint8_t payload[]; +}MCTPPacket ; + +#endif /* QEMU_MCTP_H */
Signature · 1 line
2.40.0
Re: [PATCH v3 2/3] hw/i2c: add mctp core
Quoted 12 lines · On Wed, 31 May 2023 13:47:43 +0200 Klaus Jensen <[email protected]> wrote:
From: Klaus Jensen <[email protected]>
Add an abstract MCTP over I2C endpoint model. This implements MCTP control message handling as well as handling the actual I2C transport (packetization).
Devices are intended to derive from this and implement the class methods.
Parts of this implementation is inspired by code[1] previously posted by Jonathan Cameron.
Quoted 6 lines
Squashed a fix[2] from Matt Johnston.
[1]: https://lore.kernel.org/qemu-devel/[email protected]/
[2]: https://lore.kernel.org/qemu-devel/[email protected]/Signed-off-by Klaus Jensen <[email protected]>
Hi Klaus,
A few minor comments inline.
With those tidied up feel free to add
hw/i2c/mctp.c line 308
Quoted 13 lines
hw/i2c/mctp.c new file +13
-0+ +if (pkt->mctp.hdr.flags &MCTP_H_FLAGS_SOM ) { + mctp->tx.is_control = false; + +if (mctp->state ==I2C_MCTP_STATE_RX ) { + mc->reset (mctp); + } + + mctp->state =I2C_MCTP_STATE_RX ; + + mctp->tx.addr = pkt->i2c.source; + mctp->tx.eid = pkt->mctp.hdr.eid.source; + mctp->tx.flags = pkt->mctp.hdr.flags &0x7 ;
Maybe worth either defining that mask, or adding a comment that this is copying the msg tag. Or rename flags...
Quoted 45 lines
hw/i2c/mctp.c new file +38
-0+ mctp->tx.pktseq = (pkt->mctp.hdr.flags >>4 ) &0x3 ; + +if ((pkt->mctp.payload[0 ] &0x7f ) ==MCTP_MESSAGE_TYPE_CONTROL ) { + mctp->tx.is_control = true; + +i2c_mctp_handle_control (mctp); + +return 0 ; + } + }else if (mctp->state ==I2C_MCTP_STATE_RX_STARTED ) { +trace_i2c_mctp_drop_expected_som (); + goto drop; + }else if (((pkt->mctp.hdr.flags >>4 ) &0x3 ) != (++mctp->tx.pktseq &0x3 )) { +trace_i2c_mctp_drop_invalid_pktseq ((pkt->mctp.hdr.flags >>4 ) &0x3 , + mctp->tx.pktseq &0x3 ); + goto drop; + } + + mc->put_buf (mctp,i2c_mctp_payload (mctp->buffer), payload_len); + +if (pkt->mctp.hdr.flags &MCTP_H_FLAGS_EOM ) { + mc->handle (mctp); + mctp->state =I2C_MCTP_STATE_WAIT_TX ; + } + +return 0 ; + +default : +return -1 ; + } + +drop: + mc->reset (mctp); + + mctp->state =I2C_MCTP_STATE_IDLE ; + +return 0 ; +}include/hw/i2c/mctp.h new file +0
-0
...
Quoted 35 lines
include/hw/i2c/mctp.h new file +35
-0+struct MCTPI2CEndpointClass { +I2CSlaveClass parent_class; + +/** +* +* put_buf() - receive incoming message fragment +* +* Must returns 0 for succes or -1 for error. +*/ +int (*put_buf)(MCTPI2CEndpoint *mctp,uint8_t *buf,size_t len); + +/** +* get_buf() - provide pointer to message fragment +* +* Called by the mctp subsystem to request a pointer to the next message +* fragment. The implementation must advance its internal position such +* that successive calls returns the next fragments. +* +* Must return the number of bytes available. +*/ +size_t (*get_buf)(MCTPI2CEndpoint *mctp,const uint8_t **buf, +size_t maxlen,uint8_t *mctp_flags); + +/** +* handle() - handle an MCTP message +* +* Called by the mctp subsystem when a full message has been delivered and +* may be parsed and processed. +*/ +void (*handle)(MCTPI2CEndpoint *mctp); + +/** +* reset() - reset internal state +* +* Called by the mctp subsystem in the event of some transport error.
include/hw/i2c/mctp.h line 55
Quoted 10 lines
include/hw/i2c/mctp.h new file +10
-0+* Implementation must reset its internal state and drop any fragments +* previously receieved. +*/ +void (*reset)(MCTPI2CEndpoint *mctp); + +/** +* get_types() - provide supported mctp message types +* +* Must provide a buffer with a full MCTP supported message types payload +* (i.e. `0x0(SUCCESS),0x1(ONE),0x4(NMI)`).
ONE? Looks to be PLDM
Good to reference DSP0239 Management Component Transport Protocol (MCTP) IDs and Codes which has the list.
Quoted 12 lines
include/hw/i2c/mctp.h new file +5
-0+* +* Returns the size of the response. +*/ +size_t (*get_types)(MCTPI2CEndpoint *mctp,const uint8_t **data); +};include/net/mctp.h new file +0
-0
include/net/mctp.h line 4
Quoted 4 lines
include/net/mctp.h new file +4
-0@@ -0,0 +1,28 @@ +#ifndef QEMU_MCTP_H +#define QEMU_MCTP_H + +#define MCTP_BASELINE_MTU 64
Ideally add a reference for this as well.
8.3.1 Baseline transmission unit in DSP0236 1.3.0
include/net/mctp.h line 8
Quoted 4 lines
include/net/mctp.h new file +4
-0+ +enum { +MCTP_H_FLAGS_EOM =1 <<6 , +MCTP_H_FLAGS_SOM =1 <<7 ,
Trivial: I'm not really seeing the enum here as useful vs a pair of defines.
Quoted 4 lines
include/net/mctp.h new file +4
-0+}; + +#define MCTP_MESSAGE_IC (1 << 7) +
Re: [PATCH v3 2/3] hw/i2c: add mctp core
Quoted 136 lines · On Wed, 31 May 2023 15:59:44 +0100 Jonathan Cameron…
Quoted 12 lines · On Wed, 31 May 2023 13:47:43 +0200 Klaus Jensen <[email protected]> wrote:
From: Klaus Jensen <[email protected]>
Add an abstract MCTP over I2C endpoint model. This implements MCTP control message handling as well as handling the actual I2C transport (packetization).
Devices are intended to derive from this and implement the class methods.
Parts of this implementation is inspired by code[1] previously posted by Jonathan Cameron.
Quoted 6 lines
Squashed a fix[2] from Matt Johnston.
[1]: https://lore.kernel.org/qemu-devel/[email protected]/
[2]: https://lore.kernel.org/qemu-devel/[email protected]/Signed-off-by Klaus Jensen <[email protected]>Hi Klaus,
A few minor comments inline.
With those tidied up feel free to add
Reviewed-by Jonathan Cameron <[email protected]>hw/i2c/mctp.c line 308
Quoted 13 lines
hw/i2c/mctp.c new file +13
-0+ +if (pkt->mctp.hdr.flags &MCTP_H_FLAGS_SOM ) { + mctp->tx.is_control = false; + +if (mctp->state ==I2C_MCTP_STATE_RX ) { + mc->reset (mctp); + } + + mctp->state =I2C_MCTP_STATE_RX ; + + mctp->tx.addr = pkt->i2c.source; + mctp->tx.eid = pkt->mctp.hdr.eid.source; + mctp->tx.flags = pkt->mctp.hdr.flags &0x7 ;Maybe worth either defining that mask, or adding a comment that this is copying the msg tag. Or rename flags...
Quoted 45 lines
hw/i2c/mctp.c new file +38
-0+ mctp->tx.pktseq = (pkt->mctp.hdr.flags >>4 ) &0x3 ; + +if ((pkt->mctp.payload[0 ] &0x7f ) ==MCTP_MESSAGE_TYPE_CONTROL ) { + mctp->tx.is_control = true; + +i2c_mctp_handle_control (mctp); + +return 0 ; + } + }else if (mctp->state ==I2C_MCTP_STATE_RX_STARTED ) { +trace_i2c_mctp_drop_expected_som (); + goto drop; + }else if (((pkt->mctp.hdr.flags >>4 ) &0x3 ) != (++mctp->tx.pktseq &0x3 )) { +trace_i2c_mctp_drop_invalid_pktseq ((pkt->mctp.hdr.flags >>4 ) &0x3 , + mctp->tx.pktseq &0x3 ); + goto drop; + } + + mc->put_buf (mctp,i2c_mctp_payload (mctp->buffer), payload_len); + +if (pkt->mctp.hdr.flags &MCTP_H_FLAGS_EOM ) { + mc->handle (mctp); + mctp->state =I2C_MCTP_STATE_WAIT_TX ; + } + +return 0 ; + +default : +return -1 ; + } + +drop: + mc->reset (mctp); + + mctp->state =I2C_MCTP_STATE_IDLE ; + +return 0 ; +}include/hw/i2c/mctp.h new file +0
-0...
Quoted 41 lines
include/hw/i2c/mctp.h new file +41
-0+struct MCTPI2CEndpointClass { +I2CSlaveClass parent_class; + +/** +* +* put_buf() - receive incoming message fragment +* +* Must returns 0 for succes or -1 for error. +*/ +int (*put_buf)(MCTPI2CEndpoint *mctp,uint8_t *buf,size_t len); + +/** +* get_buf() - provide pointer to message fragment +* +* Called by the mctp subsystem to request a pointer to the next message +* fragment. The implementation must advance its internal position such +* that successive calls returns the next fragments. +* +* Must return the number of bytes available. +*/ +size_t (*get_buf)(MCTPI2CEndpoint *mctp,const uint8_t **buf, +size_t maxlen,uint8_t *mctp_flags); + +/** +* handle() - handle an MCTP message +* +* Called by the mctp subsystem when a full message has been delivered and +* may be parsed and processed. +*/ +void (*handle)(MCTPI2CEndpoint *mctp); + +/** +* reset() - reset internal state +* +* Called by the mctp subsystem in the event of some transport error. +* Implementation must reset its internal state and drop any fragments +* previously receieved. +*/ +void (*reset)(MCTPI2CEndpoint *mctp); + +/**
Quoted 8 lines
include/hw/i2c/mctp.h line 55
Quoted 4 lines
include/hw/i2c/mctp.h new file +4
-0+* get_types() - provide supported mctp message types +* +* Must provide a buffer with a full MCTP supported message types payload +* (i.e. `0x0(SUCCESS),0x1(ONE),0x4(NMI)`). ONE? Looks to be PLDM
Good to reference DSP0239 Management Component Transport Protocol (MCTP) IDs and Codes which has the list.
Ah. 0x1 is the length. Call it "type count" instead of ONE and make it obvious.
Quoted 35 lines
Quoted 13 lines
include/hw/i2c/mctp.h new file +5
-0+* +* Returns the size of the response. +*/ +size_t (*get_types)(MCTPI2CEndpoint *mctp,const uint8_t **data); +};include/net/mctp.h new file +0
-0include/net/mctp.h line 4
Quoted 4 lines
include/net/mctp.h new file +4
-0@@ -0,0 +1,28 @@ +#ifndef QEMU_MCTP_H +#define QEMU_MCTP_H + +#define MCTP_BASELINE_MTU 64Ideally add a reference for this as well.
8.3.1 Baseline transmission unit in DSP0236 1.3.0
include/net/mctp.h line 8
Quoted 4 lines
include/net/mctp.h new file +4
-0+ +enum { +MCTP_H_FLAGS_EOM =1 <<6 , +MCTP_H_FLAGS_SOM =1 <<7 ,Trivial: I'm not really seeing the enum here as useful vs a pair of defines.
Quoted 4 lines
include/net/mctp.h new file +4
-0+}; + +#define MCTP_MESSAGE_IC (1 << 7) +
PATCH v3 3/3 hw/nvme: add nvme management interface model
In reply to Klaus JensenFrom: Klaus Jensen <[email protected]>
Add the 'nmi-i2c' device that emulates an NVMe Management Interface controller.
Initial support is very basic (Read NMI DS, Configuration Get).
This is based on previously posted code by Padmakar Kalghatgi, Arun Kumar Agasar and Saurav Kumar.
--- hw/nvme/meson.build | 1 + hw/nvme/nmi-i2c.c | 367 +++++++++++++++++++++++++++++++++++++++++++ hw/nvme/trace-events | 6 + 3 files changed, 374 insertions(+) create mode 100644 hw/nvme/nmi-i2c.c
hw/nvme/meson.build +1 -0
@@ -1 +1,2 @@ softmmu_ss.add(when: 'CONFIG_NVME_PCI', if_true: files('ctrl.c', 'dif.c', 'ns.c', 'subsys.c')) +softmmu_ss.add(when: 'CONFIG_MCTP_I2C', if_true: files('nmi-i2c.c'))
hw/nvme/nmi-i2c.c new file +367 -0
367 lines not on this page · raw message
hw/nvme/trace-events +6 -0
@@ -215,3 +215,9 @@ pci_nvme_ub_db_wr_invalid_sq(uint32_t qid) "submission queue doorbell write for pci_nvme_ub_db_wr_invalid_sqtail(uint32_t qid, uint16_t new_tail) "submission queue doorbell write value beyond queue size, sqid=%"PRIu32", new_head=%"PRIu16", ignoring" pci_nvme_ub_unknown_css_value(void) "unknown value in cc.css field" pci_nvme_ub_too_many_mappings(void) "too many prp/sgl mappings" + +# nmi-i2c +nmi_handle_mi_read_nmi_ds(uint8_t dtyp) "dtyp 0x%"PRIx8"" +nmi_handle_mi_config_get(uint8_t identifier) "identifier 0x%"PRIx8"" +nmi_handle_mi(uint8_t opc) "opc 0x%"PRIx8"" +nmi_handle_msg(uint8_t nmint) "nmint 0x%"PRIx8""
Signature · 1 line
2.40.0
Re: [PATCH v3 3/3] hw/nvme: add nvme management interface model
Quoted 11 lines · On Wed, 31 May 2023 13:47:44 +0200 Klaus Jensen <[email protected]> wrote:
From: Klaus Jensen <[email protected]>
Add the 'nmi-i2c' device that emulates an NVMe Management Interface controller.
Initial support is very basic (Read NMI DS, Configuration Get).
This is based on previously posted code by Padmakar Kalghatgi, Arun Kumar Agasar and Saurav Kumar.
Signed-off-by Klaus Jensen <[email protected]>
A few comments inline - I dug into the specs this time so some new questions.
Jonathan
hw/nvme/nmi-i2c.c line 4
Quoted 10 lines
hw/nvme/nmi-i2c.c new file +4
-0@@ -0,0 +1,367 @@ +/* +* SPDX-License-Identifier: GPL-2.0-or-later +* +* SPDX-FileCopyrightText: Copyright (c) 2022 Samsung Electronics Co., Ltd.
Update to 2023?
hw/nvme/nmi-i2c.c line 61
Quoted 13 lines
hw/nvme/nmi-i2c.c new file +13
-0+typedef struct NMIRequest { +uint8_t opc; +uint8_t rsvd1[3 ]; +uint32_t dw0; +uint32_t dw1; +uint32_t mic; +}NMIRequest ; + +typedef struct NMIResponse { +uint8_t status; +uint8_t response[3 ]; +uint8_t payload[];/* includes the Message Integrity Check */ +}NMIResponse ;
Not used.
Quoted 4 lines
hw/nvme/nmi-i2c.c new file +4
-0+ +typedef enum NMIReadDSType { + NMI_CMD_READ_NMI_DS_SUBSYSTEM = 0x0, + NMI_CMD_READ_NMI_DS_PORTS = 0x1,
hw/nvme/nmi-i2c.c line 75
Quoted 10 lines
hw/nvme/nmi-i2c.c new file +10
-0+NMI_CMD_READ_NMI_DS_CTRL_LIST =0x2 , +NMI_CMD_READ_NMI_DS_CTRL_INFO =0x3 , +NMI_CMD_READ_NMI_DS_CMD_SUPPORT =0x4 , +NMI_CMD_READ_NMI_DS_MEB_CMD_SUPPORT =0x5 , +} NMIReadDSType; + +static void nmi_set_parameter_error (NMIDevice *nmi,uint8_t bit,uint16_t byte) +{ + nmi->scratch[nmi->pos++] =0x4 ; + nmi->scratch[nmi->pos++] = bit;
Mask bit to only 3 bits?
hw/nvme/nmi-i2c.c line 77
Quoted 2 lines
hw/nvme/nmi-i2c.c new file +2
-0+ nmi->scratch[nmi->pos++] = (byte >>4 ) &0xf ; + nmi->scratch[nmi->pos++] = byte &0xf ;
Not following how byte is split up. Figure 29 in 1.1d suggests it should be in bits 23:08 and this doesn't match that. Perhaps a reference given I guess I'm looking at wrong thing.
Quoted 6 lines
hw/nvme/nmi-i2c.c new file +5
-0+} +static void nmi_handle_mi_read_nmi_ds (NMIDevice *nmi,NMIRequest *request) +{ +I2CSlave *i2c =I2C_SLAVE (nmi); +
hw/nvme/nmi-i2c.c line 103
Quoted 10 lines
hw/nvme/nmi-i2c.c new file +10
-0+uint32_t dw0 =le32_to_cpu (request->dw0); +uint8_t dtyp = (dw0 >>24 ) &0xf ; +uint8_t *buf; +size_t len; + +trace_nmi_handle_mi_read_nmi_ds (dtyp); + +static uint8_t nmi_ds_subsystem[36 ] = { +0x00 ,/* success */ +0x20 ,/* response data length */
Not the easiest datasheet to read, but I think length is 2 bytes. Figure 93 in the 1.2b NMI spec has it as bits 15:00 in a 24 bit field. Ah. I see this is 1.1 definition, in that it's the same but in figure 89.
hw/nvme/nmi-i2c.c line 112
Quoted 9 lines
hw/nvme/nmi-i2c.c new file +9
-0+0x00 ,0x00 ,/* reserved */ +0x00 ,/* number of ports */ +0x01 ,/* major version */ + 0x01, /* minor version */ + }; + +static uint8_t nmi_ds_ports[36 ] = { +0x00 ,/* success */ + 0x20, /* response data length */
As above - this is 2 bytes.
hw/nvme/nmi-i2c.c line 118
Quoted 6 lines
hw/nvme/nmi-i2c.c new file +6
-0+0x00 ,0x00 ,/* reserved */ +0x02 ,/* port type (smbus) */ +0x00 ,/* reserved */ +0x40 ,0x00 ,/* maximum mctp transission unit size (64 bytes) */ +0x00 ,0x00 ,0x00 ,0x00 ,/* management endpoint buffer size */ + 0x00, 0x00, /* vpd i2c address/freq */
Give separate bytes for the two things, why not just have two lines and a comment for each one?
Quoted 1 line
+
0x00,0x01, /* management endpoint i2c address/freq */
Same here though second byte isn't anything to do with frequency. Documnted as NVMe Basic Management and indicates if that command is supported.
hw/nvme/nmi-i2c.c line 126
Quoted 7 lines
hw/nvme/nmi-i2c.c new file +7
-0+ }; + +static uint8_t nmi_ds_empty[8 ] = { +0x00 ,/* success */ +0x02 ,/* response data length */ +0x00 ,0x00 ,/* reserved */ +0x00 ,0x00 ,/* number of controllers */
A reference for this would be good as I'm not sure it's defined in the NMI spec. Is it the controller list from 4.4.1 in the main NVME 2.0 spec?
hw/nvme/nmi-i2c.c line 132
Quoted 6 lines
hw/nvme/nmi-i2c.c new file +6
-0+ 0x00, 0x00, /* padding */ + }; + +switch (dtyp) { +case NMI_CMD_READ_NMI_DS_SUBSYSTEM : + len =36 ;
sizeof(nmi_ds_subsystem) Might as well keep that 36 in just one place.
hw/nvme/nmi-i2c.c line 139
Quoted 7 lines
hw/nvme/nmi-i2c.c new file +7
-0+ buf = nmi_ds_subsystem; + +break ; + +case NMI_CMD_READ_NMI_DS_PORTS : + len =36 ; + buf = nmi_ds_ports;
same here?
hw/nvme/nmi-i2c.c line 142
Quoted 3 lines
hw/nvme/nmi-i2c.c new file +3
-0+ +/* patch in the i2c address of the endpoint */ + buf[14 ] = i2c->address;
If you have multiple instances of this as they will race over the static buffer. Just make it non static and add a comment on why.
Quoted 31 lines
hw/nvme/nmi-i2c.c new file +31
-0+ +break ; + +case NMI_CMD_READ_NMI_DS_CTRL_LIST : +case NMI_CMD_READ_NMI_DS_CMD_SUPPORT : +case NMI_CMD_READ_NMI_DS_MEB_CMD_SUPPORT : + len =8 ; + buf = nmi_ds_empty; + +break ; + +default : +nmi_set_parameter_error (nmi, offsetof(NMIRequest , dw0) +4 ,0 ); + +return ; + } + +memcpy (nmi->scratch + nmi->pos, buf, len); + nmi->pos += len; +} + +enum { +NMI_CMD_CONFIGURATION_GET_SMBUS_FREQ =0x1 , +NMI_CMD_CONFIGURATION_GET_HEALTH_STATUS_CHANGE =0x2 , +NMI_CMD_CONFIGURATION_GET_MCTP_TRANSMISSION_UNIT =0x3 , +}; + +static void nmi_handle_mi_config_get (NMIDevice *nmi,NMIRequest *request) +{ +uint32_t dw0 =le32_to_cpu (request->dw0); +uint8_t identifier = dw0 &0xff ;
hw/nvme/nmi-i2c.c line 183
Quoted 10 lines
hw/nvme/nmi-i2c.c new file +10
-0+uint8_t *buf; + +static uint8_t smbus_freq[4 ] = { +0x00 ,/* success */ +0x01 ,0x00 ,0x00 ,/* 100 kHz */ + }; + +static uint8_t mtu[4 ] = { +0x00 ,/* success */ +0x40 ,0x00 ,0x00 ,/* 64 */
2 bytes only I think with another reserved.
Figure 66 in 1.1d
Quoted 52 lines
hw/nvme/nmi-i2c.c new file +52
-0+ }; + +trace_nmi_handle_mi_config_get (identifier); + +switch (identifier) { +case NMI_CMD_CONFIGURATION_GET_SMBUS_FREQ : + buf = smbus_freq; +break ; + +case NMI_CMD_CONFIGURATION_GET_MCTP_TRANSMISSION_UNIT : + buf = mtu; +break ; + +default : +nmi_set_parameter_error (nmi,0x0 , offsetof(NMIRequest , dw0)); +return ; + } + +memcpy (nmi->scratch + nmi->pos, buf,4 ); + nmi->pos +=4 ; +} + +enum { +NMI_CMD_READ_NMI_DS =0x0 , +NMI_CMD_CONFIGURATION_GET =0x4 , +}; + +static void nmi_handle_mi (NMIDevice *nmi,NMIMessage *msg) +{ +NMIRequest *request = (NMIRequest *)msg->payload; + +trace_nmi_handle_mi (request->opc); + +switch (request->opc) { +case NMI_CMD_READ_NMI_DS : +nmi_handle_mi_read_nmi_ds (nmi, request); +break ; + +case NMI_CMD_CONFIGURATION_GET : +nmi_handle_mi_config_get (nmi, request); +break ; + +default : +nmi_set_parameter_error (nmi,0x0 ,0x0 ); +fprintf (stderr,"nmi command 0x%x not handled\n" , request->opc); + +break ; + } +} + +enum { +NMI_MESSAGE_TYPE_NMI =0x1 ,
hw/nvme/nmi-i2c.c line 249
Quoted 10 lines
hw/nvme/nmi-i2c.c new file +7
-0+}; + +static size_t nmi_get_types (MCTPI2CEndpoint *mctp,const uint8_t **data) +{ +static const uint8_t buf[] = { +0x0 ,0x1 ,MCTP_MESSAGE_TYPE_NMI , + };
Perhaps a comment that the 0x1 is the length. Or you could define a structure for this with a variable length final field?
Also, should the control type be reported? I couldn't find a statement that it shouldn't and it has a message ID.
Quoted 6 lines
hw/nvme/nmi-i2c.c new file +6
-0+ + *data = buf; + +return sizeof (buf); +} +
Quoted 51 lines · Klaus Jensen
From: Klaus Jensen <[email protected]>
This adds a generic MCTP endpoint model that other devices may derive from. I'm not 100% happy with the design of the class methods, but it's a start.
Also included is a very basic implementation of an NVMe-MI device, supporting only a small subset of the required commands. Lior (CC'ed) has some patches coming up that adds futher support.
Since this all relies on i2c target mode, this can currently only be used with an SoC that includes the Aspeed I2C controller.
The easiest way to get up and running with this, is to grab my buildroot overlay[1]. It includes modified a modified dts as well as a couple of required packages.
QEMU can then be launched along these lines:
qemu-system-arm \ -nographic \ -M ast2600-evb \ -kernel output/images/zImage \ -initrd output/images/rootfs.cpio \ -dtb output/images/aspeed-ast2600-evb-nmi.dtb \ -nic user,hostfwd=tcp::2222-:22 \ -device nmi-i2c,address=0x3a \ -serial mon:stdioFrom within the booted system,
mctp addr add 8 dev mctpi2c15
mctp link set mctpi2c15 up
mctp route add 9 via mctpi2c15
mctp neigh add 9 dev mctpi2c15 lladdr0x3a
mi-mctp 1 9 infoComments are very welcome!
[1]: https://github.com/birkelund/buildroots/tree/main/mctp-i2c
Changes since v2
~~~~~~~~~~~~~~~~- Applied a bunch of feedback from Jonathan:
+ Moved a lot of internally used structs out of the include headers and into the source files.
+ Added spec references in various places
+ Split the patch fori2c_smbus_pec()into its own
+ Fix a compile error (and bug) innmi-i2c.c.
Quoted 3 lines
- From Corey:
+ Reworked the buffer handling. The deriving devices now returns a pointer to their own buffer that the mctp core copies into.
You didn't do what I asked here, I guess I wasn't clear. You have:
+static void i2c_mctp_handle_control_set_eid(MCTPI2CEndpoint *mctp, uint8_t eid) +{
+ mctp->my_eid = eid;
+
+ uint8_t buf[] = {
+ 0x0, 0x0, eid, 0x0,
+ };
+
+ memcpy(i2c_mctp_control_data(mctp->buffer), buf, sizeof(buf));
+ mctp->len += sizeof(buf);
+}
That style of programming can lead to buffer overruns as code changes, as you aren't checking the length of the target buffer. I don't think there are any issues now, but as people change the code you might end up with one if someone gets a length wrong.
What I would like is for you to create a function like:
i2c_mctp_add_bytes(mctp, buf, len)
that checks that len bytes will fit, then does the addition of the bytes. You need to adjust this to fit how you are doing things, and you probably want one that adds just one byte, but hopefully you get the idea.
I'm sorry to be picky, but I've seen and fixed too many buffer overruns (including one in the qemu i2c code) in situations like this. Corey's rule is: Never add anything to a buffer without checking the length.
Everything else looks good.
-corey
Quoted 41 lines
+ Added a couple of extra debugging trace events.
Changes since v1
~~~~~~~~~~~~~~~~- Fix SPDX-License tag for
hw/nvme/nmi-i2c.c(Philippe)
- Add some asserts to verify buffer indices (by request from Corey).
- Drop short packets that could result in underflow (Corey)
- Movei2c_smbus_pec()to smbus common code (Corey)
- A couple of logic fixes (patch from Jeremy squashed in)
- Added a patch to handle messages with dest eid 0 (Matt) Maybe squash this as well.Klaus Jensen (3):
hw/i2c: add smbus pec utility function
hw/i2c: add mctp core
hw/nvme: add nvme management interface modelMAINTAINERS | 7 + hw/arm/Kconfig | 1 + hw/i2c/Kconfig | 4 + hw/i2c/mctp.c | 398 ++++++++++++++++++++++++++++++++++ hw/i2c/meson.build | 1 + hw/i2c/smbus_master.c | 28 +++ hw/i2c/trace-events | 13 ++ hw/nvme/meson.build | 1 + hw/nvme/nmi-i2c.c | 367 +++++++++++++++++++++++++++++++ hw/nvme/trace-events | 6 + include/hw/i2c/mctp.h | 137 ++++++++++++ include/hw/i2c/smbus_master.h | 2 + include/net/mctp.h | 28 +++ 13 files changed, 993 insertions(+) create mode 100644 hw/i2c/mctp.c create mode 100644 hw/nvme/nmi-i2c.c create mode 100644 include/hw/i2c/mctp.h create mode 100644 include/net/mctp.hSignature · 1 line
2.40.0