Skip to contentSkip to conversation

I3C MCTP net driver

PATCH Superseded Matt Johnston 4 participants 13 messages Jul 3, 2023 – Jul 4, 2023

Subsystems (5)

v1 of 6 Superseded Latest is v6 →

lore
Share & tools
lore

Matt Johnston

author maintainer
#0 lore raw

cover I3C MCTP net driver

This series adds an I3C transport for the kernel's MCTP network protocol. MCTP is a communication protocol between system components (BMCs, drives, NICs etc), with higher level protocols such as NVMe-MI or PLDM built on top of it (in userspace). It runs over various transports such as I2C, PCIe, or I3C.

The mctp-i3c driver follows a similar approach to the kernel's existing mctp-i2c driver, creating a "mctpi3cX" network interface for each numbered I3C bus. Busses opt in to support by adding a "mctp-controller" property to the devicetree:

&i3c0 {
        mctp-controller;
}

The driver will bind to MCTP class devices (DCR 0xCC) that are on a supported I3C bus. Each bus is represented by a struct mctp_i3c_bus that keeps state for the network device. An individual I3C device (struct mctp_i3c_device) performs operations using the "parent" mctp_i3c_bus object. The I3C notify/enumeration patch is needed so that the mctp-i3c driver can handle creating/removing mctp_i3c_bus objects as required.

The mctp-i3c driver is using the Provisioned ID as an identifier for target I3C devices (the neighbour address), as that will be more stable than the I3C dynamic address. The driver internally translates that to a dynamic address for bus operations.

The driver has been tested using an AST2600 platform. A remote endpoint has been tested against Qemu, as well as using the target mode support in Aspeed's vendor tree.

---

I'll leave it to maintainers whether this should be merged through the i3c or net tree.

Since my previous RFC email to the i3c list, this adds dt-bindings and fixes a comment typo.

Jeremy Kerr (1):
i3c: Add support for bus enumeration & notification

Matt Johnston (2):
dt-bindings: i3c: Add mctp-controller property
mctp i3c: MCTP I3C driver

 .../devicetree/bindings/i3c/i3c.yaml          |   4 +
 drivers/i3c/master.c                          |  35 +
 drivers/net/mctp/Kconfig                      |   9 +
 drivers/net/mctp/Makefile                     |   1 +
 drivers/net/mctp/mctp-i3c.c                   | 778 ++++++++++++++++++
 include/linux/i3c/master.h                    |  11 +
 6 files changed, 838 insertions(+)
 create mode 100644 drivers/net/mctp/mctp-i3c.c
Signature · 7 lines
2.37.2


-- 
linux-i3c mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-i3c
author maintainer
#1 lore raw

PATCH 1/3 dt-bindings: i3c: Add mctp-controller property

This property is used to describe a I3C bus with attached MCTP I3C target devices.

---
 Documentation/devicetree/bindings/i3c/i3c.yaml | 4 ++++
 1 file changed, 4 insertions(+)
Documentation/devicetree/bindings/i3c/i3c.yaml +4 -0
index fdb4212149e7..08731e2484f2 100644
@@ -55,6 +55,10 @@ properties:
 
       May not be supported by all controllers.
 
+  mctp-controller:
+    description: |
+      Indicates that this bus hosts MCTP-over-I3C target devices.
+
 required:
   - "#address-cells"
   - "#size-cells"
Signature · 7 lines
2.37.2


-- 
linux-i3c mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-i3c
#2 lore raw

Re: [PATCH 1/3] dt-bindings: i3c: Add mctp-controller property

Quoted 14 lines · Matt Johnston

This property is used to describe a I3C bus with attached MCTP I3C target devices.

---
 Documentation/devicetree/bindings/i3c/i3c.yaml | 4 ++++
 1 file changed, 4 insertions(+)
Documentation/devicetree/bindings/i3c/i3c.yaml +0 -0
index fdb4212149e7..08731e2484f2 100644

Documentation/devicetree/bindings/i3c/i3c.yaml properties: · line 60

Quoted 6 lines
Documentation/devicetree/bindings/i3c/i3c.yaml +3 -0
@@ -55,6 +55,10 @@ properties:
 
       May not be supported by all controllers.
 
+  mctp-controller:
+    description: |
+      Indicates that this bus hosts MCTP-over-I3C target devices.

I have doubts you actually tested it - there is no type/ref. Also, your description is a bit different than existing from dtschema. Why? Aren't these the same things?

Best regards,
Krzysztof

author maintainer
#3 lore raw

Re: [PATCH 1/3] dt-bindings: i3c: Add mctp-controller property

Quoted 18 lines · Krzysztof Kozlowski
Quoted 17 lines · Matt Johnston

This property is used to describe a I3C bus with attached MCTP I3C target devices.

---
 Documentation/devicetree/bindings/i3c/i3c.yaml | 4 ++++
 1 file changed, 4 insertions(+)
Documentation/devicetree/bindings/i3c/i3c.yaml +0 -0
index fdb4212149e7..08731e2484f2 100644
@@ -55,6 +55,10 @@ properties:
 
       May not be supported by all controllers.
 
Quoted 7 lines

Documentation/devicetree/bindings/i3c/i3c.yaml properties: · line 60

Quoted 3 lines
Documentation/devicetree/bindings/i3c/i3c.yaml +3 -0
+  mctp-controller:
+    description: |
+      Indicates that this bus hosts MCTP-over-I3C target devices.

I have doubts you actually tested it - there is no type/ref. Also, your description is a bit different than existing from dtschema. Why? Aren't these the same things?

Ah, I'll add
$ref: /schemas/types.yaml#/definitions/flag

I ran dt_binding_check andmake dt_binding_check \
DT_SCHEMA_FILES=Documentation/devicetree/bindings/i3c/i3c.yaml

?

Quoted 4 lines

Best regards,
Krzysztof

Signature · 3 lines
linux-i3c mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-i3c
author maintainer
#4 lore raw

Re: [PATCH 1/3] dt-bindings: i3c: Add mctp-controller property

In reply to Krzysztof Kozlowski
Quoted 18 lines · Krzysztof Kozlowski
Quoted 17 lines · Matt Johnston

This property is used to describe a I3C bus with attached MCTP I3C target devices.

---
 Documentation/devicetree/bindings/i3c/i3c.yaml | 4 ++++
 1 file changed, 4 insertions(+)
Documentation/devicetree/bindings/i3c/i3c.yaml +0 -0
index fdb4212149e7..08731e2484f2 100644
@@ -55,6 +55,10 @@ properties:
 
       May not be supported by all controllers.
 
Quoted 7 lines

Documentation/devicetree/bindings/i3c/i3c.yaml properties: · line 60

Quoted 3 lines
Documentation/devicetree/bindings/i3c/i3c.yaml +3 -0
+  mctp-controller:
+    description: |
+      Indicates that this bus hosts MCTP-over-I3C target devices.

I have doubts you actually tested it - there is no type/ref. Also, your description is a bit different than existing from dtschema. Why? Aren't these the same things?

(sorry my reply minutes ago was somehow an old draft, please ignore)

Ah, I'll add
$ref: /schemas/types.yaml#/definitions/flag

Testing with
make dtbs_check DT_SCHEMA_FILES=trivial-devices.yaml
I don't see any warnings, and neither after adding mctp-controller to a .dts (out of tree) and testing with
make CHECK_DTBS=y DT_SCHEMA_FILES=i3c.yaml aspeed-test.dtb

Should that pick it up?

For the description, do you mean it differs to the other properties in i3c.yaml, or something else?

Thanks,
Matt

#5 lore raw

Re: [PATCH 1/3] dt-bindings: i3c: Add mctp-controller property

Quoted 27 lines · Matt Johnston
Quoted 18 lines · Krzysztof Kozlowski
Quoted 17 lines · Matt Johnston

This property is used to describe a I3C bus with attached MCTP I3C target devices.

---
 Documentation/devicetree/bindings/i3c/i3c.yaml | 4 ++++
 1 file changed, 4 insertions(+)
Documentation/devicetree/bindings/i3c/i3c.yaml +0 -0
index fdb4212149e7..08731e2484f2 100644
@@ -55,6 +55,10 @@ properties:
 
       May not be supported by all controllers.
 
Quoted 7 lines

Documentation/devicetree/bindings/i3c/i3c.yaml properties: · line 60

Quoted 3 lines
Documentation/devicetree/bindings/i3c/i3c.yaml +3 -0
+  mctp-controller:
+    description: |
+      Indicates that this bus hosts MCTP-over-I3C target devices.

I have doubts you actually tested it - there is no type/ref. Also, your description is a bit different than existing from dtschema. Why? Aren't these the same things?

Quoted 4 lines

(sorry my reply minutes ago was somehow an old draft, please ignore)

Ah, I'll add
$ref: /schemas/types.yaml#/definitions/flag

Although does not matter, but use the same as in dtschema.
type: boolean

Quoted 11 lines

Testing with
make dtbs_check DT_SCHEMA_FILES=trivial-devices.yaml
I don't see any warnings, and neither after adding mctp-controller to a .dts (out of tree) and testing with
make CHECK_DTBS=y DT_SCHEMA_FILES=i3c.yaml aspeed-test.dtb

Should that pick it up?

For the description, do you mean it differs to the other properties in i3c.yaml, or something else?

It differs than existing mctp-controller property. If this was on purpose, please share a bit more why. If not, maybe use the same description?

Best regards,
Krzysztof

author maintainer
#6 lore raw

Re: [PATCH 1/3] dt-bindings: i3c: Add mctp-controller property

Quoted 12 lines · Krzysztof Kozlowski
Quoted 11 lines · Matt Johnston
Quoted 9 lines · Krzysztof Kozlowski

Documentation/devicetree/bindings/i3c/i3c.yaml properties: · line 60

Quoted 4 lines · Matt Johnston
Documentation/devicetree/bindings/i3c/i3c.yaml +3 -0
+  mctp-controller:
+    description: |
+      Indicates that this bus hosts MCTP-over-I3C target devices.

I have doubts you actually tested it - there is no type/ref. Also, your description is a bit different than existing from dtschema. Why? Aren't these the same things?

Quoted 5 lines
Quoted 2 lines

Ah, I'll add
$ref: /schemas/types.yaml#/definitions/flag

Although does not matter, but use the same as in dtschema.
type: boolean

OK, thanks.

Quoted 6 lines
Quoted 2 lines

For the description, do you mean it differs to the other properties in i3c.yaml, or something else?

It differs than existing mctp-controller property. If this was on purpose, please share a bit more why. If not, maybe use the same description?

The mctp-controller property has the same meaning as for I2C, so I'll use the existing I2C text for I3C as well. That will also be more suitable if in future Linux works as an I3C target device (currently it's controller only).

"indicates that the system is accessible via this bus as an endpoint for
MCTP over I3C transport."

Thanks,
Matt

author maintainer
#7 lore raw

PATCH 2/3 i3c: Add support for bus enumeration & notification

In reply to Matt Johnston

From: Jeremy Kerr <[email protected]>

This allows other drivers to be notified when new i3c busses are attached, referring to a whole i3c bus as opposed to individual devices.

---
 drivers/i3c/master.c       | 35 +++++++++++++++++++++++++++++++++++
 include/linux/i3c/master.h | 11 +++++++++++
 2 files changed, 46 insertions(+)
drivers/i3c/master.c +35 -0
index 08aeb69a7800..2276abe38bdc 100644
@@ -22,6 +22,7 @@
 static DEFINE_IDR(i3c_bus_idr);
 static DEFINE_MUTEX(i3c_core_lock);
 static int __i3c_first_dynamic_bus_num;
+static BLOCKING_NOTIFIER_HEAD(i3c_bus_notifier);
 
 /**
  * i3c_bus_maintenance_lock - Lock the bus for a maintenance operation
@@ -453,6 +454,36 @@ static int i3c_bus_init(struct i3c_bus *i3cbus, struct device_node *np)
 	return 0;
 }
 
+void i3c_for_each_bus_locked(int (*fn)(struct i3c_bus *bus, void *data),
+			     void *data)
+{
+	struct i3c_bus *bus;
+	int id;
+
+	mutex_lock(&i3c_core_lock);
+	idr_for_each_entry(&i3c_bus_idr, bus, id)
+		fn(bus, data);
+	mutex_unlock(&i3c_core_lock);
+}
+EXPORT_SYMBOL_GPL(i3c_for_each_bus_locked);
+
+int i3c_register_notifier(struct notifier_block *nb)
+{
+	return blocking_notifier_chain_register(&i3c_bus_notifier, nb);
+}
+EXPORT_SYMBOL_GPL(i3c_register_notifier);
+
+int i3c_unregister_notifier(struct notifier_block *nb)
+{
+	return blocking_notifier_chain_unregister(&i3c_bus_notifier, nb);
+}
+EXPORT_SYMBOL_GPL(i3c_unregister_notifier);
+
+static void i3c_bus_notify(struct i3c_bus *bus, unsigned int action)
+{
+	blocking_notifier_call_chain(&i3c_bus_notifier, action, bus);
+}
+
 static const char * const i3c_bus_mode_strings[] = {
 	[I3C_BUS_MODE_PURE] = "pure",
 	[I3C_BUS_MODE_MIXED_FAST] = "mixed-fast",
@@ -2678,6 +2709,8 @@ int i3c_master_register(struct i3c_master_controller *master,
 	if (ret)
 		goto err_del_dev;
 
+	i3c_bus_notify(i3cbus, I3C_NOTIFY_BUS_ADD);
+
 	/*
 	 * We're done initializing the bus and the controller, we can now
 	 * register I3C devices discovered during the initial DAA.
@@ -2710,6 +2743,8 @@ EXPORT_SYMBOL_GPL(i3c_master_register);
  */
 void i3c_master_unregister(struct i3c_master_controller *master)
 {
+	i3c_bus_notify(&master->bus, I3C_NOTIFY_BUS_REMOVE);
+
 	i3c_master_i2c_adapter_cleanup(master);
 	i3c_master_unregister_i3c_devs(master);
 	i3c_master_bus_cleanup(master);
include/linux/i3c/master.h +11 -0
index 0b52da4f2346..db909ef79be4 100644
@@ -24,6 +24,12 @@
 
 struct i2c_client;
 
+/* notifier actions. notifier call data is the struct i3c_bus */
+enum {
+	I3C_NOTIFY_BUS_ADD,
+	I3C_NOTIFY_BUS_REMOVE,
+};
+
 struct i3c_master_controller;
 struct i3c_bus;
 struct i3c_device;
@@ -652,4 +658,9 @@ void i3c_master_queue_ibi(struct i3c_dev_desc *dev, struct i3c_ibi_slot *slot);
 
 struct i3c_ibi_slot *i3c_master_get_free_ibi_slot(struct i3c_dev_desc *dev);
 
+void i3c_for_each_bus_locked(int (*fn)(struct i3c_bus *bus, void *data),
+			     void *data);
+int i3c_register_notifier(struct notifier_block *nb);
+int i3c_unregister_notifier(struct notifier_block *nb);
+
 #endif /* I3C_MASTER_H */
Signature · 7 lines
2.37.2


-- 
linux-i3c mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-i3c
#8 lore raw

Re: [PATCH 2/3] i3c: Add support for bus enumeration & notification

Quoted 7 lines · Matt Johnston

From: Jeremy Kerr <[email protected]>

This allows other drivers to be notified when new i3c busses are attached, referring to a whole i3c bus as opposed to individual devices.

Your SoB is missing.

Best regards,
Krzysztof

author maintainer
#9 lore raw

PATCH 3/3 mctp i3c: MCTP I3C driver

In reply to Matt Johnston

Provides MCTP network transport over an I3C bus, as specified in DMTF DSP0233.

Each I3C bus (with "mctp-controller" devicetree property) gets an "mctpi3cX" net device created. I3C devices are reachable as remote endpoints through that net device. Link layer addressing uses the I3C PID as a fixed hardware address for neighbour table entries.

The driver matches I3C devices that have the MIPI assigned DCR 0xCC for MCTP.

---
 drivers/net/mctp/Kconfig    |   9 +
 drivers/net/mctp/Makefile   |   1 +
 drivers/net/mctp/mctp-i3c.c | 778 ++++++++++++++++++++++++++++++++++++
 3 files changed, 788 insertions(+)
 create mode 100644 drivers/net/mctp/mctp-i3c.c
drivers/net/mctp/Kconfig +9 -0
index dc71657d9184..ce9d2d2ccf3b 100644
@@ -33,6 +33,15 @@ 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_I3C
+	tristate "MCTP I3C transport"
+	depends on I3C
+	help
+	  Provides a driver to access MCTP devices over I3C transport,
+	  from DMTF specification DSP0233.
+	  A MCTP protocol network device is created for each I3C bus
+	  having a "mctp-controller" devicetree property.
+
 endmenu
 
 endif
drivers/net/mctp/Makefile +1 -0
index 1ca3e6028f77..e1cb99ced54a 100644
@@ -1,2 +1,3 @@
 obj-$(CONFIG_MCTP_SERIAL) += mctp-serial.o
 obj-$(CONFIG_MCTP_TRANSPORT_I2C) += mctp-i2c.o
+obj-$(CONFIG_MCTP_TRANSPORT_I3C) += mctp-i3c.o
drivers/net/mctp/mctp-i3c.c new file +778 -0

778 lines not on this page · raw message

Signature · 7 lines
2.37.2


-- 
linux-i3c mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-i3c
maintainer
#10 lore raw

Re: [PATCH 3/3] mctp i3c: MCTP I3C driver

From: Matt Johnston <[email protected]>
Date: Mon, 3 Jul 2023 13:30:48 +0800

Quoted 11 lines
drivers/net/mctp/mctp-i3c.c new file +11 -0
+static void mctp_i3c_xmit(struct mctp_i3c_bus *mbus, struct sk_buff *skb)
+{
+	struct net_device_stats *stats = &mbus->ndev->stats;
+	struct i3c_priv_xfer xfer = { .rnw = false };
+	struct mctp_i3c_internal_hdr *ihdr = NULL;
+	struct mctp_i3c_device *mi = NULL;
+	u8 *data = NULL;
+	unsigned int data_len;
+	u8 addr, pec;
+	int rc = 0;
+	u64 pid;

...

drivers/net/mctp/mctp-i3c.c line 595

Quoted 9 lines
drivers/net/mctp/mctp-i3c.c new file +9 -0
+/* Returns an ERR_PTR on failure */
+static struct mctp_i3c_bus *mctp_i3c_bus_add(struct i3c_bus *bus)
+__must_hold(&busdevs_lock)
+{
+	struct mctp_i3c_bus *mbus = NULL;
+	struct net_device *ndev = NULL;
+	u8 addr[PID_SIZE];
+	char namebuf[IFNAMSIZ];
+	int rc;

Please order local variables from longest to shortest line.

Please do this for your entire submission.

Thank you.

#11 lore raw

Re: [PATCH 3/3] mctp i3c: MCTP I3C driver

In reply to Matt Johnston

drivers/net/mctp/mctp-i3c.c line 28

Quoted 10 lines
drivers/net/mctp/mctp-i3c.c new file +10 -0
+#define MCTP_I3C_MAXBUF 65536
+/* 48 bit Provisioned Id */
+#define PID_SIZE 6
+
+/* 64 byte payload, 4 byte MCTP header */
+static const int MCTP_I3C_MINMTU = 64 + 4;
+/* One byte less to allow for the PEC */
+static const int MCTP_I3C_MAXMTU = MCTP_I3C_MAXBUF - 1;
+/* 4 byte MCTP header, no data, 1 byte PEC */
+static const int MCTP_I3C_MINLEN = 4 + 1;

Why static const and not #define? It would also be normal for variables to be lower case, to make it clear they are in fact variables, not #defines.

Quoted 32 lines
drivers/net/mctp/mctp-i3c.c new file +32 -0
+struct mctp_i3c_bus {
+	struct net_device *ndev;
+
+	struct task_struct *tx_thread;
+	wait_queue_head_t tx_wq;
+	/* tx_lock protects tx_skb and devs */
+	spinlock_t tx_lock;
+	/* Next skb to transmit */
+	struct sk_buff *tx_skb;
+	/* Scratch buffer for xmit */
+	u8 tx_scratch[MCTP_I3C_MAXBUF];
+
+	/* Element of busdevs */
+	struct list_head list;
+
+	/* Provisioned ID of our controller */
+	u64 pid;
+
+	struct i3c_bus *bus;
+	/* Head of mctp_i3c_device.list. Protected by busdevs_lock */
+	struct list_head devs;
+};
+
+struct mctp_i3c_device {
+	struct i3c_device *i3c;
+	struct mctp_i3c_bus *mbus;
+	struct list_head list; /* Element of mctp_i3c_bus.devs */
+
+	/* Held while tx_thread is using this device */
+	struct mutex lock;
+
+	/* Whether BCR indicates MDB is present in IBI */

drivers/net/mctp/mctp-i3c.c line 89

Quoted 10 lines
drivers/net/mctp/mctp-i3c.c new file +10 -0
+	bool have_mdb;
+	/* I3C dynamic address */
+	u8 addr;
+	/* Maximum read length */
+	u16 mrl;
+	/* Maximum write length */
+	u16 mwl;
+	/* Provisioned ID */
+	u64 pid;
+};

Since you have commented about most of the members of these structures, you could use kerneldoc.

Quoted 12 lines
drivers/net/mctp/mctp-i3c.c new file +12 -0
+/* We synthesise a mac header using the Provisioned ID.
+ * Used to pass dest to mctp_i3c_start_xmit.
+ */
+struct mctp_i3c_internal_hdr {
+	u8 dest[PID_SIZE];
+	u8 source[PID_SIZE];
+} __packed;
+
+/* Returns the 48 bit Provisioned Id from an i3c_device_info.pid */
+static void pid_to_addr(u64 pid, u8 addr[PID_SIZE])
+{
+	pid = cpu_to_be64(pid);

drivers/net/mctp/mctp-i3c.c line 112

Quoted 10 lines
drivers/net/mctp/mctp-i3c.c new file +10 -0
+	memcpy(addr, ((u8 *)&pid) + 2, PID_SIZE);
+}
+
+static u64 addr_to_pid(u8 addr[PID_SIZE])
+{
+	u64 pid = 0;
+
+	memcpy(((u8 *)&pid) + 2, addr, PID_SIZE);
+	return be64_to_cpu(pid);
+}

I don't know anything about MCTP. But Ethernet MAC addresses are also 48 bits. Could you make use of u64_to_ether_addr() and ether_addr_to_u64()?

Quoted 12 lines
drivers/net/mctp/mctp-i3c.c new file +12 -0
+static int mctp_i3c_setup(struct mctp_i3c_device *mi)
+{
+	const struct i3c_ibi_setup ibi = {
+		.max_payload_len = 1,
+		.num_slots = MCTP_I3C_IBI_SLOTS,
+		.handler = mctp_i3c_ibi_handler,
+	};
+	bool ibi_set = false, ibi_enabled = false;
+	struct i3c_device_info info;
+	int rc;
+
+	i3c_device_get_info(mi->i3c, &info);

drivers/net/mctp/mctp-i3c.c line 235

Quoted 10 lines
drivers/net/mctp/mctp-i3c.c new file +10 -0
+	mi->have_mdb = info.bcr & BIT(2);
+	mi->addr = info.dyn_addr;
+	mi->mwl = info.max_write_len;
+	mi->mrl = info.max_read_len;
+	mi->pid = info.pid;
+
+	rc = i3c_device_request_ibi(mi->i3c, &ibi);
+	if (rc == 0) {
+		ibi_set = true;
+	} else if (rc == -ENOTSUPP) {

In networking, we try to avoid ENOTSUPP and use EOPNOTSUPP:

https://lore.kernel.org/netdev/[email protected]/

drivers/net/mctp/mctp-i3c.c line 535

Quoted 11 lines
drivers/net/mctp/mctp-i3c.c new file +11 -0
+static int mctp_i3c_header_create(struct sk_buff *skb, struct net_device *dev,
+				  unsigned short type, const void *daddr,
+	   const void *saddr, unsigned int len)
+{
+	struct mctp_i3c_internal_hdr *ihdr;
+
+	skb_push(skb, sizeof(struct mctp_i3c_internal_hdr));
+	skb_reset_mac_header(skb);
+	ihdr = (void *)skb_mac_header(skb);
+	memcpy(ihdr->dest, daddr, PID_SIZE);
+	memcpy(ihdr->source, saddr, PID_SIZE);

ether_addr_copy() ?

Quoted 7 lines
drivers/net/mctp/mctp-i3c.c new file +7 -0
+/* Returns an ERR_PTR on failure */
+static struct mctp_i3c_bus *mctp_i3c_bus_add(struct i3c_bus *bus)
+__must_hold(&busdevs_lock)
+{
+	struct mctp_i3c_bus *mbus = NULL;
+	struct net_device *ndev = NULL;
+	u8 addr[PID_SIZE];

drivers/net/mctp/mctp-i3c.c line 603

Quoted 10 lines
drivers/net/mctp/mctp-i3c.c new file +10 -0
+	char namebuf[IFNAMSIZ];
+	int rc;
+
+	if (!mctp_i3c_is_mctp_controller(bus))
+		return ERR_PTR(-ENOENT);
+
+	snprintf(namebuf, sizeof(namebuf), "mctpi3c%d", bus->id);
+	ndev = alloc_netdev(sizeof(*mbus), namebuf, NET_NAME_ENUM, mctp_i3c_net_setup);
+	if (!ndev) {
+		pr_warn("No memory for %s\n", namebuf);

pr_ functions are not liked too much. Is there a struct device you can use with dev_warn()?

Andrew

author maintainer
#12 lore raw

Re: [PATCH 3/3] mctp i3c: MCTP I3C driver

Hi Andrew,

Quoted 6 lines · Andrew Lunn
Quoted 6 lines
drivers/net/mctp/mctp-i3c.c new file +6 -0
+#define MCTP_I3C_MAXBUF 65536
+/* 48 bit Provisioned Id */
+#define PID_SIZE 6
+
+/* 64 byte payload, 4 byte MCTP header */
+static const int MCTP_I3C_MINMTU = 64 + 4;
Quoted 8 lines

drivers/net/mctp/mctp-i3c.c line 28

Quoted 4 lines
drivers/net/mctp/mctp-i3c.c new file +4 -0
+/* One byte less to allow for the PEC */
+static const int MCTP_I3C_MAXMTU = MCTP_I3C_MAXBUF - 1;
+/* 4 byte MCTP header, no data, 1 byte PEC */
+static const int MCTP_I3C_MINLEN = 4 + 1;

Why static const and not #define? It would also be normal for variables to be lower case, to make it clear they are in fact variables, not #defines.

My personal preference is for static const since it's less error prone, though had to use #define for the ones used in array sizing. Happy to change to #define if that's the style though.

Quoted 37 lines
Quoted 37 lines
drivers/net/mctp/mctp-i3c.c new file +37 -0
+struct mctp_i3c_bus {
+	struct net_device *ndev;
+
+	struct task_struct *tx_thread;
+	wait_queue_head_t tx_wq;
+	/* tx_lock protects tx_skb and devs */
+	spinlock_t tx_lock;
+	/* Next skb to transmit */
+	struct sk_buff *tx_skb;
+	/* Scratch buffer for xmit */
+	u8 tx_scratch[MCTP_I3C_MAXBUF];
+
+	/* Element of busdevs */
+	struct list_head list;
+
+	/* Provisioned ID of our controller */
+	u64 pid;
+
+	struct i3c_bus *bus;
+	/* Head of mctp_i3c_device.list. Protected by busdevs_lock */
+	struct list_head devs;
+};
+
+struct mctp_i3c_device {
+	struct i3c_device *i3c;
+	struct mctp_i3c_bus *mbus;
+	struct list_head list; /* Element of mctp_i3c_bus.devs */
+
+	/* Held while tx_thread is using this device */
+	struct mutex lock;
+
+	/* Whether BCR indicates MDB is present in IBI */
+	bool have_mdb;
+	/* I3C dynamic address */
+	u8 addr;
+	/* Maximum read length */
+	u16 mrl;
Quoted 8 lines

drivers/net/mctp/mctp-i3c.c line 89

Quoted 5 lines
drivers/net/mctp/mctp-i3c.c new file +5 -0
+	/* Maximum write length */
+	u16 mwl;
+	/* Provisioned ID */
+	u64 pid;
+};

Since you have commented about most of the members of these structures, you could use kerneldoc.

These aren't intended to be exposed as an API anywhere, just commented for future code readers (including me). Is kerneldoc still appropriate?

Quoted 18 lines
Quoted 18 lines
drivers/net/mctp/mctp-i3c.c new file +17 -0
+/* We synthesise a mac header using the Provisioned ID.
+ * Used to pass dest to mctp_i3c_start_xmit.
+ */
+struct mctp_i3c_internal_hdr {
+	u8 dest[PID_SIZE];
+	u8 source[PID_SIZE];
+} __packed;
+
+/* Returns the 48 bit Provisioned Id from an i3c_device_info.pid */
+static void pid_to_addr(u64 pid, u8 addr[PID_SIZE])
+{
+	pid = cpu_to_be64(pid);
+	memcpy(addr, ((u8 *)&pid) + 2, PID_SIZE);
+}
+
+static u64 addr_to_pid(u8 addr[PID_SIZE])
+{
Quoted 8 lines

drivers/net/mctp/mctp-i3c.c line 112

Quoted 5 lines
drivers/net/mctp/mctp-i3c.c new file +5 -0
+	u64 pid = 0;
+
+	memcpy(((u8 *)&pid) + 2, addr, PID_SIZE);
+	return be64_to_cpu(pid);
+}

I don't know anything about MCTP. But Ethernet MAC addresses are also 48 bits. Could you make use of u64_to_ether_addr() and ether_addr_to_u64()?

The 48 bit identifier is an I3C Provisioned ID. It has a similar purpose to an ethernet MAC address but for a different protocol. I think it might cause confusion to code readers if it were passed to ethernet functions.

Quoted 27 lines
Quoted 14 lines
drivers/net/mctp/mctp-i3c.c new file +12 -0
+static int mctp_i3c_setup(struct mctp_i3c_device *mi)
+{
+	const struct i3c_ibi_setup ibi = {
+		.max_payload_len = 1,
+		.num_slots = MCTP_I3C_IBI_SLOTS,
+		.handler = mctp_i3c_ibi_handler,
+	};
+	bool ibi_set = false, ibi_enabled = false;
+	struct i3c_device_info info;
+	int rc;
+
+	i3c_device_get_info(mi->i3c, &info);

drivers/net/mctp/mctp-i3c.c line 235

Quoted 10 lines
drivers/net/mctp/mctp-i3c.c new file +10 -0
+	mi->have_mdb = info.bcr & BIT(2);
+	mi->addr = info.dyn_addr;
+	mi->mwl = info.max_write_len;
+	mi->mrl = info.max_read_len;
+	mi->pid = info.pid;
+
+	rc = i3c_device_request_ibi(mi->i3c, &ibi);
+	if (rc == 0) {
+		ibi_set = true;
+	} else if (rc == -ENOTSUPP) {

In networking, we try to avoid ENOTSUPP and use EOPNOTSUPP:

Quoted 1 line

https://lore.kernel.org/netdev/[email protected]/

checkpatch noticed this one too, but the existing I3C functions return ENOTSUPP so it needs to match against that.

Quoted 29 lines

drivers/net/mctp/mctp-i3c.c line 535

Quoted 13 lines

+static int mctp_i3c_header_create(struct sk_buff *skb, struct net_device *dev,

(quoted diff) +1 -0
+				  unsigned short type, const void

*daddr,

drivers/net/mctp/mctp-i3c.c new file +9 -0
+	   const void *saddr, unsigned int len)
+{
+	struct mctp_i3c_internal_hdr *ihdr;
+
+	skb_push(skb, sizeof(struct mctp_i3c_internal_hdr));
+	skb_reset_mac_header(skb);
+	ihdr = (void *)skb_mac_header(skb);
+	memcpy(ihdr->dest, daddr, PID_SIZE);
+	memcpy(ihdr->source, saddr, PID_SIZE);

ether_addr_copy() ?

Quoted 13 lines
drivers/net/mctp/mctp-i3c.c new file +13 -0
+/* Returns an ERR_PTR on failure */
+static struct mctp_i3c_bus *mctp_i3c_bus_add(struct i3c_bus *bus)
+__must_hold(&busdevs_lock)
+{
+	struct mctp_i3c_bus *mbus = NULL;
+	struct net_device *ndev = NULL;
+	u8 addr[PID_SIZE];
+	char namebuf[IFNAMSIZ];
+	int rc;
+
+	if (!mctp_i3c_is_mctp_controller(bus))
+		return ERR_PTR(-ENOENT);
+
Quoted 8 lines

drivers/net/mctp/mctp-i3c.c line 603

Quoted 5 lines
(quoted diff) +2 -0
+	snprintf(namebuf, sizeof(namebuf), "mctpi3c%d", bus->id);
+	ndev = alloc_netdev(sizeof(*mbus), namebuf, NET_NAME_ENUM,

mctp_i3c_net_setup);

drivers/net/mctp/mctp-i3c.c new file +2 -0
+	if (!ndev) {
+		pr_warn("No memory for %s\n", namebuf);

pr_ functions are not liked too much. Is there a struct device you can use with dev_warn()?

I'll change the ones with a device available, that one in particular can be removed anyway.

Thanks,
Matt

Signature · 3 lines
linux-i3c mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-i3c