Skip to contentSkip to conversation

greybus: introduce CPC as transport layer

PATCH Superseded Damien Riégel 3 participants 23 messages Jan 15 – Jan 27 Greybus

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover greybus: introduce CPC as transport layer

Hi,

This patchset brings support for Silicon Labs' CPC protocol as transport layer for Greybus. This is introduced as a module that sits between Greybus and CPC Host Device Drivers implementations, like SDIO or SPI. This patchset includes SDIO as physical layer.

        +----------------------------------------------------+
        |                      Greybus                       |
        +----------------------------------------------------+
                                 /|\
                                  |
                                 \|/
        +----------------------------------------------------+
        |                        CPC                         |
        +----------------------------------------------------+
              /|\                /|\                /|\
               |                  |                  |
              \|/                \|/                \|/
          +----------+       +---------+       +-----------+
          |   SDIO   |       |   SPI   |       |   Others  |
          +----------+       +---------+       +-----------+

CPC implements some of the features of Unipro that Greybus relies upon, like reliable transmission. CPC takes care of detecting transmission errors and retransmit frames if necessary, but that feature is not part of the RFC to keep it concise. There's also a flow-control feature, preventing sending messages to already full cports.

In order to implement these features, a 4-byte header is prepended to Greybus messages, making the whole header 12 bytes (Greybus header is 8 bytes).

This RFC starts by implementing a shim layer between physical bus drivers (like SDIO and SPI) and Greybus, and progressively add more elements to it to make it useful in its own right. Finally, an SDIO driver is added to enable the communication with a remote device.

Changes in v2:
- addressed review comments and errors reported by kernel bot
- for SDIO driver, remove padding between headers and payloads when aggregating packets together

Damien Riégel (13):
greybus: cpc: add minimal CPC Host Device infrastructure
greybus: cpc: introduce CPC cport structure
greybus: cpc: use socket buffers instead of gb_message in TX path
greybus: cpc: pack cport ID in Greybus header
greybus: cpc: switch RX path to socket buffers
greybus: cpc: introduce CPC header structure
greybus: cpc: account for CPC header size in RX and TX path
greybus: cpc: add and validate sequence numbers
greybus: cpc: acknowledge all incoming messages
greybus: cpc: use holding queue instead of sending out immediately
greybus: cpc: honour remote's RX window
greybus: cpc: let host device drivers dequeue TX frames
greybus: cpc: add private data pointer in CPC Host Device

Gabriel Beaulieu (1):
greybus: cpc: add CPC SDIO host driver

 MAINTAINERS                    |   6 +
 drivers/greybus/Kconfig        |   2 +
 drivers/greybus/Makefile       |   2 +
 drivers/greybus/cpc/Kconfig    |  22 ++
 drivers/greybus/cpc/Makefile   |   9 +
 drivers/greybus/cpc/cpc.h      |  75 +++++
 drivers/greybus/cpc/cport.c    | 112 +++++++
 drivers/greybus/cpc/header.c   | 136 +++++++++
 drivers/greybus/cpc/header.h   |  55 ++++
 drivers/greybus/cpc/host.c     | 313 +++++++++++++++++++
 drivers/greybus/cpc/host.h     |  63 ++++
 drivers/greybus/cpc/protocol.c | 168 +++++++++++
 drivers/greybus/cpc/sdio.c     | 533 +++++++++++++++++++++++++++++++++
 13 files changed, 1496 insertions(+)
 create mode 100644 drivers/greybus/cpc/Kconfig
 create mode 100644 drivers/greybus/cpc/Makefile
 create mode 100644 drivers/greybus/cpc/cpc.h
 create mode 100644 drivers/greybus/cpc/cport.c
 create mode 100644 drivers/greybus/cpc/header.c
 create mode 100644 drivers/greybus/cpc/header.h
 create mode 100644 drivers/greybus/cpc/host.c
 create mode 100644 drivers/greybus/cpc/host.h
 create mode 100644 drivers/greybus/cpc/protocol.c
 create mode 100644 drivers/greybus/cpc/sdio.c
Signature · 1 line
2.52.0
#1 lore raw

PATCH v2 1/14 greybus: cpc: add minimal CPC Host Device infrastructure

As the first step for adding CPC support with Greybus, add a very minimal module for CPC Host Devices. For now, this module only proxies calls to the Greybus Host Device API and does nothing useful, but further commits will use this base to add features.

---
 MAINTAINERS                  |  6 +++
 drivers/greybus/Kconfig      |  2 +
 drivers/greybus/Makefile     |  2 +
 drivers/greybus/cpc/Kconfig  | 10 +++++
 drivers/greybus/cpc/Makefile |  6 +++
 drivers/greybus/cpc/host.c   | 84 ++++++++++++++++++++++++++++++++++++
 drivers/greybus/cpc/host.h   | 40 +++++++++++++++++
 7 files changed, 150 insertions(+)
 create mode 100644 drivers/greybus/cpc/Kconfig
 create mode 100644 drivers/greybus/cpc/Makefile
 create mode 100644 drivers/greybus/cpc/host.c
 create mode 100644 drivers/greybus/cpc/host.h
MAINTAINERS +6 -0
index 6d1de82e6dc..56daf9ec310 100644
@@ -10774,6 +10774,12 @@ S:	Maintained
 F:	Documentation/devicetree/bindings/net/ti,cc1352p7.yaml
 F:	drivers/greybus/gb-beagleplay.c
 
+GREYBUS CPC DRIVERS
+M:	Damien Riégel <[email protected]>
+R:	Silicon Labs Kernel Team <[email protected]>
+S:	Supported
+F:	drivers/greybus/cpc/*
+
 GREYBUS SUBSYSTEM
 M:	Johan Hovold <[email protected]>
 M:	Alex Elder <[email protected]>
drivers/greybus/Kconfig +2 -0
index c3f056d28b0..565a0fdcb2c 100644
@@ -30,6 +30,8 @@ config GREYBUS_BEAGLEPLAY
 	  To compile this code as a module, chose M here: the module
 	  will be called gb-beagleplay.ko
 
+source "drivers/greybus/cpc/Kconfig"
+
 config GREYBUS_ES2
 	tristate "Greybus ES3 USB host controller"
 	depends on USB
drivers/greybus/Makefile +2 -0
index d986e94f889..92fe1d62691 100644
@@ -21,6 +21,8 @@ ccflags-y += -I$(src)
 obj-$(CONFIG_GREYBUS_BEAGLEPLAY)	+= gb-beagleplay.o
 
 # Greybus Host controller drivers
+obj-$(CONFIG_GREYBUS_CPC)	+= cpc/
+
 gb-es2-y := es2.o
 
 obj-$(CONFIG_GREYBUS_ES2)	+= gb-es2.o
drivers/greybus/cpc/Kconfig new file +10 -0
new file mode 100644
index 00000000000..ab96fedd0de
@@ -0,0 +1,10 @@
+# SPDX-License-Identifier: GPL-2.0
+
+config GREYBUS_CPC
+	tristate "Greybus CPC driver"
+	help
+	  Select this option if you have a Silicon Labs device that acts as a
+	  Greybus SVC.
+
+	  To compile this code as a module, chose M here: the module will be
+	  called gb-cpc.ko
drivers/greybus/cpc/Makefile new file +6 -0
new file mode 100644
index 00000000000..490982a0ff5
@@ -0,0 +1,6 @@
+# SPDX-License-Identifier: GPL-2.0
+
+gb-cpc-y := host.o
+
+# CPC core
+obj-$(CONFIG_GREYBUS_CPC)	+= gb-cpc.o
drivers/greybus/cpc/host.c new file +84 -0

84 lines not on this page · raw message

drivers/greybus/cpc/host.h new file +40 -0
new file mode 100644
index 00000000000..f55feb303f4
@@ -0,0 +1,40 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Copyright (c) 2025, Silicon Laboratories, Inc.
+ */
+
+#ifndef __CPC_HOST_H
+#define __CPC_HOST_H
+
+#include <linux/device.h>
+#include <linux/greybus.h>
+#include <linux/types.h>
+
+#define GB_CPC_MSG_SIZE_MAX 4096
+#define GB_CPC_NUM_CPORTS 8
+
+struct cpc_host_device;
+
+struct cpc_hd_driver {
+	int (*message_send)(struct cpc_host_device *hd, u16 dest_cport_id,
+			    struct gb_message *message, gfp_t gfp_mask);
+	void (*message_cancel)(struct gb_message *message);
+};
+
+/**
+ * struct cpc_host_device - CPC host device.
+ * @gb_hd: pointer to Greybus Host Device this device belongs to.
+ * @driver: driver operations.
+ */
+struct cpc_host_device {
+	struct gb_host_device *gb_hd;
+	const struct cpc_hd_driver *driver;
+};
+
+struct cpc_host_device *cpc_hd_create(struct cpc_hd_driver *driver, struct device *parent);
+int cpc_hd_add(struct cpc_host_device *cpc_hd);
+void cpc_hd_put(struct cpc_host_device *cpc_hd);
+void cpc_hd_del(struct cpc_host_device *cpc_hd);
+void cpc_hd_rcvd(struct cpc_host_device *cpc_hd, u16 cport_id, u8 *data, size_t length);
+
+#endif
Signature · 1 line
2.52.0
#2 lore raw

Re: [PATCH v2 01/14] greybus: cpc: add minimal CPC Host Device infrastructure

Quoted 4 lines · Damien Riégel

As the first step for adding CPC support with Greybus, add a very minimal module for CPC Host Devices. For now, this module only proxies calls to the Greybus Host Device API and does nothing useful, but further commits will use this base to add features.

What is "CPC"? I don't see a description of that anywhere here, sorry.

drivers/greybus/cpc/Kconfig line 7

Quoted 5 lines
drivers/greybus/cpc/Kconfig new file +5 -0
+config GREYBUS_CPC
+	tristate "Greybus CPC driver"
+	help
+	  Select this option if you have a Silicon Labs device that acts as a
+	  Greybus SVC.

What type of device is that? And again, what is "CPC"? Specifics are good to add here.

thanks,

greg k-h

#3 lore raw

Re: [PATCH v2 01/14] greybus: cpc: add minimal CPC Host Device infrastructure

Quoted 7 lines · Greg Kroah-Hartman
Quoted 4 lines · Damien Riégel

As the first step for adding CPC support with Greybus, add a very minimal module for CPC Host Devices. For now, this module only proxies calls to the Greybus Host Device API and does nothing useful, but further commits will use this base to add features.

What is "CPC"? I don't see a description of that anywhere here, sorry.

Yeah, it seems I forgot to introduce the term here, and in the cover letter as well. I'll resend a version that explains what CPC is and that also addresses Jerome's comments.

thanks,
damien

#4 lore raw

PATCH v2 2/14 greybus: cpc: introduce CPC cport structure

In reply to Damien Riégel

A number of CPC features, like retransmission or remote's receive window, are cport-based. In order to prepare for these features, introduce a CPC CPort context structure.

The CPC Host Device module now implements cport_allocate and cport_release callbacks in order to allocate and release these structures when requested by Greybus module.

---
 drivers/greybus/cpc/Makefile |   2 +-
 drivers/greybus/cpc/cpc.h    |  29 ++++++++++
 drivers/greybus/cpc/cport.c  |  37 ++++++++++++
 drivers/greybus/cpc/host.c   | 109 ++++++++++++++++++++++++++++++++++-
 drivers/greybus/cpc/host.h   |  12 ++++
 5 files changed, 187 insertions(+), 2 deletions(-)
 create mode 100644 drivers/greybus/cpc/cpc.h
 create mode 100644 drivers/greybus/cpc/cport.c
drivers/greybus/cpc/Makefile +1 -1
index 490982a0ff5..3d50f8c5473 100644
@@ -1,6 +1,6 @@
 # SPDX-License-Identifier: GPL-2.0
 
-gb-cpc-y := host.o
+gb-cpc-y := cport.o host.o
 
 # CPC core
 obj-$(CONFIG_GREYBUS_CPC)	+= gb-cpc.o
drivers/greybus/cpc/cpc.h new file +29 -0
new file mode 100644
index 00000000000..3915a7fbc4f
@@ -0,0 +1,29 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Copyright (c) 2025, Silicon Laboratories, Inc.
+ */
+
+#ifndef __CPC_H
+#define __CPC_H
+
+#include <linux/device.h>
+#include <linux/greybus.h>
+#include <linux/types.h>
+
+/**
+ * struct cpc_cport - CPC cport
+ * @id: cport ID
+ * @cpc_hd: pointer to the CPC host device this cport belongs to
+ */
+struct cpc_cport {
+	u16 id;
+
+	struct cpc_host_device *cpc_hd;
+};
+
+struct cpc_cport *cpc_cport_alloc(u16 cport_id, gfp_t gfp_mask);
+void cpc_cport_release(struct cpc_cport *cport);
+
+int cpc_cport_message_send(struct cpc_cport *cport, struct gb_message *message, gfp_t gfp_mask);
+
+#endif
drivers/greybus/cpc/cport.c new file +37 -0
new file mode 100644
index 00000000000..88bdb2f8182
@@ -0,0 +1,37 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (c) 2025, Silicon Laboratories, Inc.
+ */
+
+#include "cpc.h"
+#include "host.h"
+
+/**
+ * cpc_cport_alloc() - Allocate and initialize CPC cport.
+ * @cport_id: cport ID.
+ * @gfp_mask: GFP mask for allocation.
+ *
+ * Return: Pointer to allocated and initialized cpc_cport, or NULL on failure.
+ */
+struct cpc_cport *cpc_cport_alloc(u16 cport_id, gfp_t gfp_mask)
+{
+	struct cpc_cport *cport;
+
+	cport = kzalloc(sizeof(*cport), gfp_mask);
+	if (!cport)
+		return NULL;
+
+	cport->id = cport_id;
+
+	return cport;
+}
+
+void cpc_cport_release(struct cpc_cport *cport)
+{
+	kfree(cport);
+}
+
+int cpc_cport_message_send(struct cpc_cport *cport, struct gb_message *message, gfp_t gfp_mask)
+{
+	return cport->cpc_hd->driver->message_send(cport->cpc_hd, cport->id, message, gfp_mask);
+}
drivers/greybus/cpc/host.c +108 -1

144 lines not on this page · raw message

drivers/greybus/cpc/host.h +12 -0
index f55feb303f4..c3f2f56a939 100644
@@ -8,11 +8,13 @@
 
 #include <linux/device.h>
 #include <linux/greybus.h>
+#include <linux/mutex.h>
 #include <linux/types.h>
 
 #define GB_CPC_MSG_SIZE_MAX 4096
 #define GB_CPC_NUM_CPORTS 8
 
+struct cpc_cport;
 struct cpc_host_device;
 
 struct cpc_hd_driver {
@@ -25,12 +27,22 @@ struct cpc_hd_driver {
  * struct cpc_host_device - CPC host device.
  * @gb_hd: pointer to Greybus Host Device this device belongs to.
  * @driver: driver operations.
+ * @lock: mutex to synchronize access to cport array.
+ * @cports: array of cport pointers allocated by Greybus core.
  */
 struct cpc_host_device {
 	struct gb_host_device *gb_hd;
 	const struct cpc_hd_driver *driver;
+
+	struct mutex lock; /* Synchronize access to cports */
+	struct cpc_cport *cports[GB_CPC_NUM_CPORTS];
 };
 
+static inline struct device *cpc_hd_dev(struct cpc_host_device *cpc_hd)
+{
+	return &cpc_hd->gb_hd->dev;
+}
+
 struct cpc_host_device *cpc_hd_create(struct cpc_hd_driver *driver, struct device *parent);
 int cpc_hd_add(struct cpc_host_device *cpc_hd);
 void cpc_hd_put(struct cpc_host_device *cpc_hd);
Signature · 1 line
2.52.0
#5 lore raw

PATCH v2 3/14 greybus: cpc: use socket buffers instead of gb_message in TX path

In reply to Damien Riégel

CPC comes with its own header, that is not yet implemented. Without skb, the CPC host device drivers have to get two pointers to get a full
packet: one pointer to the CPC header and one pointer to the GB message. In order to make their implementations simpler, convert the GB message into an SKB.

---
 drivers/greybus/cpc/cpc.h   | 11 +++++++++-
 drivers/greybus/cpc/cport.c | 11 ++++++++--
 drivers/greybus/cpc/host.c  | 41 ++++++++++++++++++++++++++++++++++---
 drivers/greybus/cpc/host.h  |  7 ++++---
 4 files changed, 61 insertions(+), 9 deletions(-)
drivers/greybus/cpc/cpc.h +10 -1
index 3915a7fbc4f..d9f8f60913a 100644
@@ -24,6 +24,15 @@ struct cpc_cport {
 struct cpc_cport *cpc_cport_alloc(u16 cport_id, gfp_t gfp_mask);
 void cpc_cport_release(struct cpc_cport *cport);
 
-int cpc_cport_message_send(struct cpc_cport *cport, struct gb_message *message, gfp_t gfp_mask);
+int cpc_cport_transmit(struct cpc_cport *cport, struct sk_buff *skb);
+
+struct cpc_skb_cb {
+	struct cpc_cport *cport;
+
+	/* Keep track of the GB message the skb originates from */
+	struct gb_message *gb_message;
+};
+
+#define CPC_SKB_CB(__skb) ((struct cpc_skb_cb *)&((__skb)->cb[0]))
 
 #endif
drivers/greybus/cpc/cport.c +9 -2
index 88bdb2f8182..ed0b8e8b0d7 100644
@@ -31,7 +31,14 @@ void cpc_cport_release(struct cpc_cport *cport)
 	kfree(cport);
 }
 
-int cpc_cport_message_send(struct cpc_cport *cport, struct gb_message *message, gfp_t gfp_mask)
+/**
+ * cpc_cport_transmit() - Transmit skb over cport.
+ * @cport: cport.
+ * @skb: skb to be transmitted.
+ */
+int cpc_cport_transmit(struct cpc_cport *cport, struct sk_buff *skb)
 {
-	return cport->cpc_hd->driver->message_send(cport->cpc_hd, cport->id, message, gfp_mask);
+	struct cpc_host_device *cpc_hd = cport->cpc_hd;
+
+	return cpc_hd_send_skb(cpc_hd, skb);
 }
drivers/greybus/cpc/host.c +38 -3

77 lines not on this page · raw message

drivers/greybus/cpc/host.h +4 -3
index c3f2f56a939..191b5e394a6 100644
@@ -18,9 +18,7 @@ struct cpc_cport;
 struct cpc_host_device;
 
 struct cpc_hd_driver {
-	int (*message_send)(struct cpc_host_device *hd, u16 dest_cport_id,
-			    struct gb_message *message, gfp_t gfp_mask);
-	void (*message_cancel)(struct gb_message *message);
+	int (*transmit)(struct cpc_host_device *hd, struct sk_buff *skb);
 };
 
 /**
@@ -48,5 +46,8 @@ int cpc_hd_add(struct cpc_host_device *cpc_hd);
 void cpc_hd_put(struct cpc_host_device *cpc_hd);
 void cpc_hd_del(struct cpc_host_device *cpc_hd);
 void cpc_hd_rcvd(struct cpc_host_device *cpc_hd, u16 cport_id, u8 *data, size_t length);
+void cpc_hd_message_sent(struct sk_buff *skb, int status);
+
+int cpc_hd_send_skb(struct cpc_host_device *cpc_hd, struct sk_buff *skb);
 
 #endif
Signature · 1 line
2.52.0
#6 lore raw

PATCH v2 4/14 greybus: cpc: pack cport ID in Greybus header

In reply to Damien Riégel

Take advantage of the padding bytes present in the Greybus header to store the CPort ID and minize overhead. This technique is already used by the es2 driver.

---
Changes in v2:
- clear pad bytes when unpacking CPort ID

 drivers/greybus/cpc/cpc.h   |  3 +++
 drivers/greybus/cpc/cport.c | 34 ++++++++++++++++++++++++++++++++++
 drivers/greybus/cpc/host.c  | 13 ++++++++++++-
 drivers/greybus/cpc/host.h  |  2 +-
 4 files changed, 50 insertions(+), 2 deletions(-)
drivers/greybus/cpc/cpc.h +3 -0
index d9f8f60913a..62597957814 100644
@@ -24,6 +24,9 @@ struct cpc_cport {
 struct cpc_cport *cpc_cport_alloc(u16 cport_id, gfp_t gfp_mask);
 void cpc_cport_release(struct cpc_cport *cport);
 
+void cpc_cport_pack(struct gb_operation_msg_hdr *gb_hdr, u16 cport_id);
+u16 cpc_cport_unpack(struct gb_operation_msg_hdr *gb_hdr);
+
 int cpc_cport_transmit(struct cpc_cport *cport, struct sk_buff *skb);
 
 struct cpc_skb_cb {
drivers/greybus/cpc/cport.c +34 -0
index ed0b8e8b0d7..2c73d8e724e 100644
@@ -3,6 +3,9 @@
  * Copyright (c) 2025, Silicon Laboratories, Inc.
  */
 
+#include <linux/unaligned.h>
+#include <linux/skbuff.h>
+
 #include "cpc.h"
 #include "host.h"
 
@@ -31,6 +34,32 @@ void cpc_cport_release(struct cpc_cport *cport)
 	kfree(cport);
 }
 
+/**
+ * cpc_cport_pack() - Pack CPort ID into Greybus Operation Message header.
+ * @gb_hdr: Greybus operation message header.
+ * @cport_id: CPort ID to pack.
+ */
+void cpc_cport_pack(struct gb_operation_msg_hdr *gb_hdr, u16 cport_id)
+{
+	put_unaligned_le16(cport_id, gb_hdr->pad);
+}
+
+/**
+ * cpc_cport_unpack() - Unpack CPort ID from Greybus Operation Message header.
+ * @gb_hdr: Greybus operation message header.
+ *
+ * Return: CPort ID packed in the header.
+ */
+u16 cpc_cport_unpack(struct gb_operation_msg_hdr *gb_hdr)
+{
+	u16 cport_id = get_unaligned_le16(gb_hdr->pad);
+
+	// Clear padding bytes
+	put_unaligned_le16(0, gb_hdr->pad);
+
+	return cport_id;
+}
+
 /**
  * cpc_cport_transmit() - Transmit skb over cport.
  * @cport: cport.
@@ -39,6 +68,11 @@ void cpc_cport_release(struct cpc_cport *cport)
 int cpc_cport_transmit(struct cpc_cport *cport, struct sk_buff *skb)
 {
 	struct cpc_host_device *cpc_hd = cport->cpc_hd;
+	struct gb_operation_msg_hdr *gb_hdr;
+
+	/* Inject cport ID in Greybus header */
+	gb_hdr = (struct gb_operation_msg_hdr *)skb->data;
+	cpc_cport_pack(gb_hdr, cport->id);
 
 	return cpc_hd_send_skb(cpc_hd, skb);
 }
drivers/greybus/cpc/host.c +12 -1
index ee090dd3097..b096b639182 100644
@@ -203,8 +203,19 @@ void cpc_hd_message_sent(struct sk_buff *skb, int status)
 }
 EXPORT_SYMBOL_GPL(cpc_hd_message_sent);
 
-void cpc_hd_rcvd(struct cpc_host_device *cpc_hd, u16 cport_id, u8 *data, size_t length)
+void cpc_hd_rcvd(struct cpc_host_device *cpc_hd, u8 *data, size_t length)
 {
+	struct gb_operation_msg_hdr *gb_hdr;
+	u16 cport_id;
+
+	/* Prevent an out-of-bound access if called with non-sensical parameters. */
+	if (!data || length < sizeof(*gb_hdr))
+		return;
+
+	/* Retrieve cport ID that was packed in Greybus header */
+	gb_hdr = (struct gb_operation_msg_hdr *)data;
+	cport_id = cpc_cport_unpack(gb_hdr);
+
 	greybus_data_rcvd(cpc_hd->gb_hd, cport_id, data, length);
 }
 EXPORT_SYMBOL_GPL(cpc_hd_rcvd);
drivers/greybus/cpc/host.h +1 -1
index 191b5e394a6..2e568bac44e 100644
@@ -45,7 +45,7 @@ struct cpc_host_device *cpc_hd_create(struct cpc_hd_driver *driver, struct devic
 int cpc_hd_add(struct cpc_host_device *cpc_hd);
 void cpc_hd_put(struct cpc_host_device *cpc_hd);
 void cpc_hd_del(struct cpc_host_device *cpc_hd);
-void cpc_hd_rcvd(struct cpc_host_device *cpc_hd, u16 cport_id, u8 *data, size_t length);
+void cpc_hd_rcvd(struct cpc_host_device *cpc_hd, u8 *data, size_t length);
 void cpc_hd_message_sent(struct sk_buff *skb, int status);
 
 int cpc_hd_send_skb(struct cpc_host_device *cpc_hd, struct sk_buff *skb);
Signature · 1 line
2.52.0
#7 lore raw

PATCH v2 5/14 greybus: cpc: switch RX path to socket buffers

In reply to Damien Riégel

For symmetry, also convert the RX path to use socket buffers instead of u8* buffers. The main difference is that CPC host device drivers were responsible for allocating and freeing the buffers. Now they are only responsible for allocating the skb and pass it to the upper layer, the CPC "core" module will take of releasing it when it's done with it.

---
 drivers/greybus/cpc/host.c | 13 ++++++++-----
 drivers/greybus/cpc/host.h |  2 +-
 2 files changed, 9 insertions(+), 6 deletions(-)
drivers/greybus/cpc/host.c +8 -5
index b096b639182..7ffa3bf4021 100644
@@ -203,20 +203,23 @@ void cpc_hd_message_sent(struct sk_buff *skb, int status)
 }
 EXPORT_SYMBOL_GPL(cpc_hd_message_sent);
 
-void cpc_hd_rcvd(struct cpc_host_device *cpc_hd, u8 *data, size_t length)
+void cpc_hd_rcvd(struct cpc_host_device *cpc_hd, struct sk_buff *skb)
 {
 	struct gb_operation_msg_hdr *gb_hdr;
 	u16 cport_id;
 
 	/* Prevent an out-of-bound access if called with non-sensical parameters. */
-	if (!data || length < sizeof(*gb_hdr))
-		return;
+	if (skb->len < sizeof(*gb_hdr))
+		goto free_skb;
 
 	/* Retrieve cport ID that was packed in Greybus header */
-	gb_hdr = (struct gb_operation_msg_hdr *)data;
+	gb_hdr = (struct gb_operation_msg_hdr *)skb->data;
 	cport_id = cpc_cport_unpack(gb_hdr);
 
-	greybus_data_rcvd(cpc_hd->gb_hd, cport_id, data, length);
+	greybus_data_rcvd(cpc_hd->gb_hd, cport_id, skb->data, skb->len);
+
+free_skb:
+	kfree_skb(skb);
 }
 EXPORT_SYMBOL_GPL(cpc_hd_rcvd);
 
drivers/greybus/cpc/host.h +1 -1
index 2e568bac44e..cc835f5298b 100644
@@ -45,7 +45,7 @@ struct cpc_host_device *cpc_hd_create(struct cpc_hd_driver *driver, struct devic
 int cpc_hd_add(struct cpc_host_device *cpc_hd);
 void cpc_hd_put(struct cpc_host_device *cpc_hd);
 void cpc_hd_del(struct cpc_host_device *cpc_hd);
-void cpc_hd_rcvd(struct cpc_host_device *cpc_hd, u8 *data, size_t length);
+void cpc_hd_rcvd(struct cpc_host_device *cpc_hd, struct sk_buff *skb);
 void cpc_hd_message_sent(struct sk_buff *skb, int status);
 
 int cpc_hd_send_skb(struct cpc_host_device *cpc_hd, struct sk_buff *skb);
Signature · 1 line
2.52.0
#8 lore raw

PATCH v2 6/14 greybus: cpc: introduce CPC header structure

In reply to Damien Riégel

CPC main features are reliable transmission and remote's receive window management. To implement these features, an additional header is needed. This header is prepended to all Greybus messages.

Reliable transmission: to make transmission reliable, messages are sequenced and acknowledged. That constitutes two bytes of the header, one for the sequence number, one for the acknowledgment number. If a message is not acked in a timely manner, a retransmission mechanism will attempt another transmission. That mechanism will be implemented in a future patch set.

Remote's receive window: the remote advertises the number of reception buffers that are available on this cport. The other peer must take care of not sending more messages than advertised by the remote. This is a sort of flow control. That accounts for one byte in the header.

The remaining byte carries some flags. For instance, there is a flag to indicate if it's a CPC message or a Greybus message.

---
Changes in v2:
- Minor tweaks to structure documentation

 drivers/greybus/cpc/header.h | 44 ++++++++++++++++++++++++++++++++++++
 1 file changed, 44 insertions(+)
 create mode 100644 drivers/greybus/cpc/header.h
drivers/greybus/cpc/header.h new file +44 -0
new file mode 100644
index 00000000000..af283fec492
@@ -0,0 +1,44 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Copyright (c) 2025, Silicon Laboratories, Inc.
+ */
+
+#ifndef __CPC_HEADER_H
+#define __CPC_HEADER_H
+
+#include <linux/greybus.h>
+#include <linux/types.h>
+
+#define CPC_HEADER_MAX_RX_WINDOW U8_MAX
+
+/**
+ * struct cpc header - Representation of CPC header.
+ * @ctrl_flags: contains the type of frame and other control flags.
+ * @recv_wnd: number of buffers that the cport can receive without blocking.
+ * @seq: sequence number.
+ * @ack: acknowledge number, indicate to the remote the next sequence number
+ *	 this peer expects to see.
+ *
+ * Each peer can confirm reception of frames by setting the acknowledgment number to the next frame
+ * it expects to see, i.e. setting the ack number to X effectively acknowledges frames with sequence
+ * number up to X-1.
+ *
+ * CPC is designed around the concept that each cport has its pool of reception buffers. The number
+ * of buffers in a pool is advertised to the remote via the @recv_wnd attribute. This acts as
+ * software flow-control, and a peer shall not send frames to a remote if the @recv_wnd is zero.
+ *
+ * The eighth-bit (0x80) of the control byte indicates if the frame targets CPC or Greybus. If the
+ * bit is set, the frame should be interpreted as a CPC control frame. For simplicity, control
+ * frames have the same encoding as Greybus frames.
+ */
+struct cpc_header {
+	__u8 ctrl_flags;
+	__u8 recv_wnd;
+	__u8 seq;
+	__u8 ack;
+} __packed;
+
+#define CPC_HEADER_SIZE (sizeof(struct cpc_header))
+#define GREYBUS_HEADER_SIZE (sizeof(struct gb_operation_msg_hdr))
+
+#endif
Signature · 1 line
2.52.0
#9 lore raw

Re: [PATCH v2 06/14] greybus: cpc: introduce CPC header structure

Quoted 66 lines · On Thursday 15 January 2026 16:57:59 Central European Standard Time Damien…

CPC main features are reliable transmission and remote's receive window management. To implement these features, an additional header is needed. This header is prepended to all Greybus messages.

Reliable transmission: to make transmission reliable, messages are sequenced and acknowledged. That constitutes two bytes of the header, one for the sequence number, one for the acknowledgment number. If a message is not acked in a timely manner, a retransmission mechanism will attempt another transmission. That mechanism will be implemented in a future patch set.

Remote's receive window: the remote advertises the number of reception buffers that are available on this cport. The other peer must take care of not sending more messages than advertised by the remote. This is a sort of flow control. That accounts for one byte in the header.

The remaining byte carries some flags. For instance, there is a flag to indicate if it's a CPC message or a Greybus message.

---
Changes in v2:
- Minor tweaks to structure documentation

 drivers/greybus/cpc/header.h | 44 ++++++++++++++++++++++++++++++++++++
 1 file changed, 44 insertions(+)
 create mode 100644 drivers/greybus/cpc/header.h
drivers/greybus/cpc/header.h new file +32 -0
new file mode 100644
index 00000000000..af283fec492
@@ -0,0 +1,44 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Copyright (c) 2025, Silicon Laboratories, Inc.
+ */
+
+#ifndef __CPC_HEADER_H
+#define __CPC_HEADER_H
+
+#include <linux/greybus.h>
+#include <linux/types.h>
+
+#define CPC_HEADER_MAX_RX_WINDOW U8_MAX
+
+/**
+ * struct cpc header - Representation of CPC header.
+ * @ctrl_flags: contains the type of frame and other control flags.
+ * @recv_wnd: number of buffers that the cport can receive without blocking.
+ * @seq: sequence number.
+ * @ack: acknowledge number, indicate to the remote the next sequence number
+ *	 this peer expects to see.
+ *
+ * Each peer can confirm reception of frames by setting the acknowledgment number to the next frame
+ * it expects to see, i.e. setting the ack number to X effectively acknowledges frames with sequence
+ * number up to X-1.
+ *
+ * CPC is designed around the concept that each cport has its pool of reception buffers. The number
+ * of buffers in a pool is advertised to the remote via the @recv_wnd attribute. This acts as
+ * software flow-control, and a peer shall not send frames to a remote if the @recv_wnd is zero.
+ *
+ * The eighth-bit (0x80) of the control byte indicates if the frame targets CPC or Greybus. If the
+ * bit is set, the frame should be interpreted as a CPC control frame. For simplicity, control
+ * frames have the same encoding as Greybus frames.

drivers/greybus/cpc/header.h line 42

Quoted 10 lines
drivers/greybus/cpc/header.h new file +10 -0
+ */
+struct cpc_header {
+	__u8 ctrl_flags;
+	__u8 recv_wnd;
+	__u8 seq;
+	__u8 ack;
+} __packed;
+
+#define CPC_HEADER_SIZE (sizeof(struct cpc_header))
+#define GREYBUS_HEADER_SIZE (sizeof(struct gb_operation_msg_hdr))

Generally, when I see such macros in the code, I suspect some black magic behind. So, I have to check the definitions.

I suggest to not introduce these trivial symbols and just use "sizeof(struct ...)" inline in the code.

Signature · 1 line
Jérôme Pouiller
#10 lore raw

PATCH v2 7/14 greybus: cpc: account for CPC header size in RX and TX path

In reply to Damien Riégel

Account that a CPC header is prepended to every frame in the RX and TX path. For now, nothing is done with that headroom but at least bytes are reserved for it.

---
 drivers/greybus/cpc/host.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
drivers/greybus/cpc/host.c +7 -2
index 7ffa3bf4021..c89617623e8 100644
@@ -9,6 +9,7 @@
 #include <linux/skbuff.h>
 
 #include "cpc.h"
+#include "header.h"
 #include "host.h"
 
 static struct cpc_host_device *gb_hd_to_cpc_hd(struct gb_host_device *hd)
@@ -48,11 +49,13 @@ static int cpc_hd_message_send(struct cpc_host_device *cpc_hd, u16 cport_id,
 		return -EINVAL;
 	}
 
-	size = sizeof(*message->header) + message->payload_size;
+	size = sizeof(*message->header) + message->payload_size + CPC_HEADER_SIZE;
 	skb = alloc_skb(size, gfp_mask);
 	if (!skb)
 		return -ENOMEM;
 
+	skb_reserve(skb, CPC_HEADER_SIZE);
+
 	/* Header and payload are already contiguous in Greybus message */
 	skb_put_data(skb, message->buffer, sizeof(*message->header) + message->payload_size);
 
@@ -209,9 +212,11 @@ void cpc_hd_rcvd(struct cpc_host_device *cpc_hd, struct sk_buff *skb)
 	u16 cport_id;
 
 	/* Prevent an out-of-bound access if called with non-sensical parameters. */
-	if (skb->len < sizeof(*gb_hdr))
+	if (skb->len < (sizeof(*gb_hdr) + CPC_HEADER_SIZE))
 		goto free_skb;
 
+	skb_pull(skb, CPC_HEADER_SIZE);
+
 	/* Retrieve cport ID that was packed in Greybus header */
 	gb_hdr = (struct gb_operation_msg_hdr *)skb->data;
 	cport_id = cpc_cport_unpack(gb_hdr);
Signature · 1 line
2.52.0
#11 lore raw

PATCH v2 8/14 greybus: cpc: add and validate sequence numbers

In reply to Damien Riégel

The first step in making the CPC header actually do something is to add the sequence number to outgoing messages and validate that incoming frames are received in order.

At this stage, the driver doesn't send standalone acks, so if a message with Sequence X is received, the remote will not be acknowledged until a message targeting that CPort comes from the Greybus layer. Only then the driver will ack with acknowledgedment number of X + 1.

---
Changes in v2:
- change dev_warn to ratelimited version in cpc_protocol_on_data

 drivers/greybus/cpc/Makefile   |  2 +-
 drivers/greybus/cpc/cpc.h      | 20 +++++++++++++++
 drivers/greybus/cpc/cport.c    | 25 ++++++++++++++++++
 drivers/greybus/cpc/header.c   | 17 ++++++++++++
 drivers/greybus/cpc/header.h   |  2 ++
 drivers/greybus/cpc/host.c     | 13 +++++++---
 drivers/greybus/cpc/protocol.c | 47 ++++++++++++++++++++++++++++++++++
 7 files changed, 121 insertions(+), 5 deletions(-)
 create mode 100644 drivers/greybus/cpc/header.c
 create mode 100644 drivers/greybus/cpc/protocol.c
drivers/greybus/cpc/Makefile +1 -1
index 3d50f8c5473..c4b530d27a3 100644
@@ -1,6 +1,6 @@
 # SPDX-License-Identifier: GPL-2.0
 
-gb-cpc-y := cport.o host.o
+gb-cpc-y := cport.o header.o host.o protocol.o
 
 # CPC core
 obj-$(CONFIG_GREYBUS_CPC)	+= gb-cpc.o
drivers/greybus/cpc/cpc.h +20 -0
index 62597957814..87b54a4fd34 100644
@@ -8,17 +8,32 @@
 
 #include <linux/device.h>
 #include <linux/greybus.h>
+#include <linux/mutex.h>
 #include <linux/types.h>
 
+struct sk_buff;
+
 /**
  * struct cpc_cport - CPC cport
  * @id: cport ID
  * @cpc_hd: pointer to the CPC host device this cport belongs to
+ * @lock: mutex to synchronize accesses to tcb and other attributes
+ * @tcb: Transmission Control Block
  */
 struct cpc_cport {
 	u16 id;
 
 	struct cpc_host_device *cpc_hd;
+	struct mutex lock; /* Synchronize access to state variables */
+
+	/*
+	 * @ack: current acknowledge number
+	 * @seq: current sequence number
+	 */
+	struct {
+		u8 ack;
+		u8 seq;
+	} tcb;
 };
 
 struct cpc_cport *cpc_cport_alloc(u16 cport_id, gfp_t gfp_mask);
@@ -34,8 +49,13 @@ struct cpc_skb_cb {
 
 	/* Keep track of the GB message the skb originates from */
 	struct gb_message *gb_message;
+
+	u8 seq;
 };
 
 #define CPC_SKB_CB(__skb) ((struct cpc_skb_cb *)&((__skb)->cb[0]))
 
+void cpc_protocol_prepare_header(struct sk_buff *skb, u8 ack);
+void cpc_protocol_on_data(struct cpc_cport *cport, struct sk_buff *skb);
+
 #endif
drivers/greybus/cpc/cport.c +25 -0
index 2c73d8e724e..7041a6a8a36 100644
@@ -9,6 +9,16 @@
 #include "cpc.h"
 #include "host.h"
 
+/**
+ * cpc_cport_tcb_reset() - Reset cport's TCB to initial values.
+ * @cport: cport pointer
+ */
+static void cpc_cport_tcb_reset(struct cpc_cport *cport)
+{
+	cport->tcb.ack = 0;
+	cport->tcb.seq = 0;
+}
+
 /**
  * cpc_cport_alloc() - Allocate and initialize CPC cport.
  * @cport_id: cport ID.
@@ -25,6 +35,9 @@ struct cpc_cport *cpc_cport_alloc(u16 cport_id, gfp_t gfp_mask)
 		return NULL;
 
 	cport->id = cport_id;
+	cpc_cport_tcb_reset(cport);
+
+	mutex_init(&cport->lock);
 
 	return cport;
 }
@@ -69,10 +82,22 @@ int cpc_cport_transmit(struct cpc_cport *cport, struct sk_buff *skb)
 {
 	struct cpc_host_device *cpc_hd = cport->cpc_hd;
 	struct gb_operation_msg_hdr *gb_hdr;
+	u8 ack;
 
 	/* Inject cport ID in Greybus header */
 	gb_hdr = (struct gb_operation_msg_hdr *)skb->data;
 	cpc_cport_pack(gb_hdr, cport->id);
 
+	mutex_lock(&cport->lock);
+
+	CPC_SKB_CB(skb)->seq = cport->tcb.seq;
+
+	cport->tcb.seq++;
+	ack = cport->tcb.ack;
+
+	mutex_unlock(&cport->lock);
+
+	cpc_protocol_prepare_header(skb, ack);
+
 	return cpc_hd_send_skb(cpc_hd, skb);
 }
drivers/greybus/cpc/header.c new file +17 -0
new file mode 100644
index 00000000000..62946d6077e
@@ -0,0 +1,17 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (c) 2025, Silicon Laboratories, Inc.
+ */
+
+#include "header.h"
+
+/**
+ * cpc_header_get_seq() - Get the sequence number.
+ * @hdr: CPC header.
+ *
+ * Return: Sequence number.
+ */
+u8 cpc_header_get_seq(const struct cpc_header *hdr)
+{
+	return hdr->seq;
+}
drivers/greybus/cpc/header.h +2 -0
index af283fec492..d3fa26e8a07 100644
@@ -41,4 +41,6 @@ struct cpc_header {
 #define CPC_HEADER_SIZE (sizeof(struct cpc_header))
 #define GREYBUS_HEADER_SIZE (sizeof(struct gb_operation_msg_hdr))
 
+u8 cpc_header_get_seq(const struct cpc_header *hdr);
+
 #endif
drivers/greybus/cpc/host.c +9 -4
index c89617623e8..7f0579fde26 100644
@@ -209,19 +209,24 @@ EXPORT_SYMBOL_GPL(cpc_hd_message_sent);
 void cpc_hd_rcvd(struct cpc_host_device *cpc_hd, struct sk_buff *skb)
 {
 	struct gb_operation_msg_hdr *gb_hdr;
+	struct cpc_cport *cport;
 	u16 cport_id;
 
 	/* Prevent an out-of-bound access if called with non-sensical parameters. */
 	if (skb->len < (sizeof(*gb_hdr) + CPC_HEADER_SIZE))
 		goto free_skb;
 
-	skb_pull(skb, CPC_HEADER_SIZE);
-
 	/* Retrieve cport ID that was packed in Greybus header */
-	gb_hdr = (struct gb_operation_msg_hdr *)skb->data;
+	gb_hdr = (struct gb_operation_msg_hdr *)(skb->data + CPC_HEADER_SIZE);
 	cport_id = cpc_cport_unpack(gb_hdr);
 
-	greybus_data_rcvd(cpc_hd->gb_hd, cport_id, skb->data, skb->len);
+	cport = cpc_hd_get_cport(cpc_hd, cport_id);
+	if (!cport) {
+		dev_warn(cpc_hd_dev(cpc_hd), "cport %u not allocated\n", cport_id);
+		goto free_skb;
+	}
+
+	cpc_protocol_on_data(cport, skb);
 
 free_skb:
 	kfree_skb(skb);
drivers/greybus/cpc/protocol.c new file +47 -0

47 lines not on this page · raw message

Signature · 1 line
2.52.0
#12 lore raw

Re: [PATCH v2 08/14] greybus: cpc: add and validate sequence numbers

Quoted 25 lines · On Thursday 15 January 2026 16:58:01 Central European Standard Time Damien…

The first step in making the CPC header actually do something is to add the sequence number to outgoing messages and validate that incoming frames are received in order.

At this stage, the driver doesn't send standalone acks, so if a message with Sequence X is received, the remote will not be acknowledged until a message targeting that CPort comes from the Greybus layer. Only then the driver will ack with acknowledgedment number of X + 1.

---
Changes in v2:
- change dev_warn to ratelimited version in cpc_protocol_on_data

 drivers/greybus/cpc/Makefile   |  2 +-
 drivers/greybus/cpc/cpc.h      | 20 +++++++++++++++
 drivers/greybus/cpc/cport.c    | 25 ++++++++++++++++++
 drivers/greybus/cpc/header.c   | 17 ++++++++++++
 drivers/greybus/cpc/header.h   |  2 ++
 drivers/greybus/cpc/host.c     | 13 +++++++---
 drivers/greybus/cpc/protocol.c | 47 ++++++++++++++++++++++++++++++++++
 7 files changed, 121 insertions(+), 5 deletions(-)
 create mode 100644 drivers/greybus/cpc/header.c
 create mode 100644 drivers/greybus/cpc/protocol.c

[...]

Quoted 18 lines
drivers/greybus/cpc/protocol.c new file +12 -0
new file mode 100644
index 00000000000..ff65757b40f
@@ -0,0 +1,47 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (c) 2025, Silicon Laboratories, Inc.
+ */
+
+#include <linux/skbuff.h>
+
+#include "cpc.h"
+#include "header.h"
+#include "host.h"
+
+void cpc_protocol_prepare_header(struct sk_buff *skb, u8 ack)

drivers/greybus/cpc/protocol.c line 22

Quoted 10 lines
drivers/greybus/cpc/protocol.c new file +10 -0
+{
+	struct cpc_header *hdr;
+
+	skb_push(skb, sizeof(*hdr));
+
+	hdr = (struct cpc_header *)skb->data;
+	hdr->ack = ack;
+	hdr->recv_wnd = 0;
+	hdr->ctrl_flags = 0;
+	hdr->seq = CPC_SKB_CB(skb)->seq;

I suggest "memset(hdr, 0, sizeof(*hdr))", so there is no doubts all the members are set.

[...]

Signature · 1 line
Jérôme Pouiller
#13 lore raw

PATCH v2 9/14 greybus: cpc: acknowledge all incoming messages

In reply to Damien Riégel

Currently, CPC doesn't send messages on its own, it only prepends its header to outgoing messages. This can lead to messages not being acknowledged, for instance in the case of an SVC Ping

	Host				Device

SVC Ping (seq=X, ack=Y)
SVC Ping Reply (seq=Y, ack=X+1)

The "Ping Reply" is never acknowledged at the CPC level, which can lead to retransmissions, or worst the device might think the link is broken and do something to recover.

To prevent that scenario, an ack mechanism is implemented in the most straightforward manner: send an ACK to all incoming messages. Here, two flags need to be added:
- First, an ACK frame should not be passed to the Greybus layer, so a "CONTROL" flag is added. If this flag is set, it means it's a control messages and should stay at the CPC level. Currently there is only one type of control frame, the standalone ack. Control messages have the same format as Greybus operations.
- Second, ack themselves should not be acked, so to determine if a message should be acked or not, a REQUEST_ACK flag is added.

---
Changes in v2:
- add missing cpu_to_le16 conversion when setting message size

 drivers/greybus/cpc/cpc.h      |  3 ++
 drivers/greybus/cpc/cport.c    |  1 +
 drivers/greybus/cpc/header.c   | 41 +++++++++++++++++++++++++
 drivers/greybus/cpc/header.h   |  3 ++
 drivers/greybus/cpc/protocol.c | 56 +++++++++++++++++++++++++++++-----
 5 files changed, 96 insertions(+), 8 deletions(-)
drivers/greybus/cpc/cpc.h +3 -0
index 87b54a4fd34..725fd7f4afc 100644
@@ -51,6 +51,9 @@ struct cpc_skb_cb {
 	struct gb_message *gb_message;
 
 	u8 seq;
+
+#define CPC_SKB_FLAG_REQ_ACK (1 << 0)
+	u8 cpc_flags;
 };
 
 #define CPC_SKB_CB(__skb) ((struct cpc_skb_cb *)&((__skb)->cb[0]))
drivers/greybus/cpc/cport.c +1 -0
index 7041a6a8a36..847cc8ebe41 100644
@@ -91,6 +91,7 @@ int cpc_cport_transmit(struct cpc_cport *cport, struct sk_buff *skb)
 	mutex_lock(&cport->lock);
 
 	CPC_SKB_CB(skb)->seq = cport->tcb.seq;
+	CPC_SKB_CB(skb)->cpc_flags = CPC_SKB_FLAG_REQ_ACK;
 
 	cport->tcb.seq++;
 	ack = cport->tcb.ack;
drivers/greybus/cpc/header.c +41 -0

52 lines not on this page · raw message

drivers/greybus/cpc/header.h +3 -0
index d3fa26e8a07..bf2ef41d44d 100644
@@ -41,6 +41,9 @@ struct cpc_header {
 #define CPC_HEADER_SIZE (sizeof(struct cpc_header))
 #define GREYBUS_HEADER_SIZE (sizeof(struct gb_operation_msg_hdr))
 
+bool cpc_header_is_control(const struct cpc_header *hdr);
 u8 cpc_header_get_seq(const struct cpc_header *hdr);
+bool cpc_header_get_req_ack(const struct cpc_header *hdr);
+u8 cpc_header_encode_ctrl_flags(bool control, bool req_ack);
 
 #endif
drivers/greybus/cpc/protocol.c +48 -8

82 lines not on this page · raw message

Signature · 1 line
2.52.0
#14 lore raw

PATCH v2 10/14 greybus: cpc: use holding queue instead of sending out immediately

In reply to Damien Riégel

As the first step to handle remote's RX window, store the skb in a sk_buff_head list, instead of sending a message immediately when pushed by Greybus.

skbs are still sent out straight away, but now there is a place to store away if the remote's RX window is too small.

---
 drivers/greybus/cpc/cpc.h      | 10 ++++++----
 drivers/greybus/cpc/cport.c    | 21 ++++++++++++---------
 drivers/greybus/cpc/host.c     |  4 +++-
 drivers/greybus/cpc/protocol.c | 25 ++++++++++++++++++++++++-
 4 files changed, 45 insertions(+), 15 deletions(-)
drivers/greybus/cpc/cpc.h +6 -4
index 725fd7f4afc..f1669585c45 100644
@@ -9,15 +9,15 @@
 #include <linux/device.h>
 #include <linux/greybus.h>
 #include <linux/mutex.h>
+#include <linux/skbuff.h>
 #include <linux/types.h>
 
-struct sk_buff;
-
 /**
  * struct cpc_cport - CPC cport
  * @id: cport ID
  * @cpc_hd: pointer to the CPC host device this cport belongs to
  * @lock: mutex to synchronize accesses to tcb and other attributes
+ * @holding_queue: list of frames queued to be sent
  * @tcb: Transmission Control Block
  */
 struct cpc_cport {
@@ -26,6 +26,8 @@ struct cpc_cport {
 	struct cpc_host_device *cpc_hd;
 	struct mutex lock; /* Synchronize access to state variables */
 
+	struct sk_buff_head holding_queue;
+
 	/*
 	 * @ack: current acknowledge number
 	 * @seq: current sequence number
@@ -42,7 +44,7 @@ void cpc_cport_release(struct cpc_cport *cport);
 void cpc_cport_pack(struct gb_operation_msg_hdr *gb_hdr, u16 cport_id);
 u16 cpc_cport_unpack(struct gb_operation_msg_hdr *gb_hdr);
 
-int cpc_cport_transmit(struct cpc_cport *cport, struct sk_buff *skb);
+void cpc_cport_transmit(struct cpc_cport *cport, struct sk_buff *skb);
 
 struct cpc_skb_cb {
 	struct cpc_cport *cport;
@@ -58,7 +60,7 @@ struct cpc_skb_cb {
 
 #define CPC_SKB_CB(__skb) ((struct cpc_skb_cb *)&((__skb)->cb[0]))
 
-void cpc_protocol_prepare_header(struct sk_buff *skb, u8 ack);
 void cpc_protocol_on_data(struct cpc_cport *cport, struct sk_buff *skb);
+void __cpc_protocol_write_head(struct cpc_cport *cport);
 
 #endif
drivers/greybus/cpc/cport.c +12 -9
index 847cc8ebe41..91c39856e22 100644
@@ -7,7 +7,6 @@
 #include <linux/skbuff.h>
 
 #include "cpc.h"
-#include "host.h"
 
 /**
  * cpc_cport_tcb_reset() - Reset cport's TCB to initial values.
@@ -38,15 +37,23 @@ struct cpc_cport *cpc_cport_alloc(u16 cport_id, gfp_t gfp_mask)
 	cpc_cport_tcb_reset(cport);
 
 	mutex_init(&cport->lock);
+	skb_queue_head_init(&cport->holding_queue);
 
 	return cport;
 }
 
 void cpc_cport_release(struct cpc_cport *cport)
 {
+	skb_queue_purge(&cport->holding_queue);
 	kfree(cport);
 }
 
+static void cpc_cport_queue_skb(struct cpc_cport *cport, struct sk_buff *skb)
+{
+	__skb_header_release(skb);
+	__skb_queue_tail(&cport->holding_queue, skb);
+}
+
 /**
  * cpc_cport_pack() - Pack CPort ID into Greybus Operation Message header.
  * @gb_hdr: Greybus operation message header.
@@ -78,11 +85,9 @@ u16 cpc_cport_unpack(struct gb_operation_msg_hdr *gb_hdr)
  * @cport: cport.
  * @skb: skb to be transmitted.
  */
-int cpc_cport_transmit(struct cpc_cport *cport, struct sk_buff *skb)
+void cpc_cport_transmit(struct cpc_cport *cport, struct sk_buff *skb)
 {
-	struct cpc_host_device *cpc_hd = cport->cpc_hd;
 	struct gb_operation_msg_hdr *gb_hdr;
-	u8 ack;
 
 	/* Inject cport ID in Greybus header */
 	gb_hdr = (struct gb_operation_msg_hdr *)skb->data;
@@ -94,11 +99,9 @@ int cpc_cport_transmit(struct cpc_cport *cport, struct sk_buff *skb)
 	CPC_SKB_CB(skb)->cpc_flags = CPC_SKB_FLAG_REQ_ACK;
 
 	cport->tcb.seq++;
-	ack = cport->tcb.ack;
+
+	cpc_cport_queue_skb(cport, skb);
+	__cpc_protocol_write_head(cport);
 
 	mutex_unlock(&cport->lock);
-
-	cpc_protocol_prepare_header(skb, ack);
-
-	return cpc_hd_send_skb(cpc_hd, skb);
 }
drivers/greybus/cpc/host.c +3 -1
index 7f0579fde26..ec43d33dfc6 100644
@@ -62,7 +62,9 @@ static int cpc_hd_message_send(struct cpc_host_device *cpc_hd, u16 cport_id,
 	CPC_SKB_CB(skb)->cport = cport;
 	CPC_SKB_CB(skb)->gb_message = message;
 
-	return cpc_cport_transmit(cport, skb);
+	cpc_cport_transmit(cport, skb);
+
+	return 0;
 }
 
 static int cpc_hd_cport_allocate(struct cpc_host_device *cpc_hd, int cport_id, unsigned long flags)
drivers/greybus/cpc/protocol.c +24 -1
index ad97e903b92..72276818f17 100644
@@ -14,7 +14,7 @@ static bool cpc_skb_is_sequenced(struct sk_buff *skb)
 	return CPC_SKB_CB(skb)->cpc_flags & CPC_SKB_FLAG_REQ_ACK;
 }
 
-void cpc_protocol_prepare_header(struct sk_buff *skb, u8 ack)
+static void cpc_protocol_prepare_header(struct sk_buff *skb, u8 ack)
 {
 	struct cpc_header *hdr;
 
@@ -85,3 +85,26 @@ void cpc_protocol_on_data(struct cpc_cport *cport, struct sk_buff *skb)
 		greybus_data_rcvd(cport->cpc_hd->gb_hd, cport->id, skb->data, skb->len);
 	}
 }
+
+static void __cpc_protocol_write_skb(struct cpc_cport *cport, struct sk_buff *skb, u8 ack)
+{
+	cpc_protocol_prepare_header(skb, ack);
+
+	cpc_hd_send_skb(cport->cpc_hd, skb);
+}
+
+/* Write skbs at the head of holding queue */
+void __cpc_protocol_write_head(struct cpc_cport *cport)
+{
+	struct sk_buff *skb;
+	u8 ack;
+
+	ack = cport->tcb.ack;
+
+	/* For each SKB in the holding queue, clone it and pass it to lower layer */
+	while ((skb = skb_peek(&cport->holding_queue))) {
+		skb_unlink(skb, &cport->holding_queue);
+
+		__cpc_protocol_write_skb(cport, skb, ack);
+	}
+}
Signature · 1 line
2.52.0
#15 lore raw

PATCH v2 11/14 greybus: cpc: honour remote's RX window

In reply to Damien Riégel

The RX window indicates how many reception buffers the peer has available for that cport. The other peer must not send more messages than that window, or the chances of those messages being lost is very high, leading to retransmissions and poor performance.

The RX window is associated with the ack number, and indicates the valid range of sequence number the other peer can use:

	Ack		RX window		Valid Sequence Range

	X		0			None
	X		1			X
	X		2			X -> X+1

So everytime an ack is received, the driver evaluates if the valid sequence range has changed and if so pops message from its holding queue.

As the skb is moved to another queue, it cannot be passed directly to the lower layer anymore, instead a clone is passed.

---
Changes in v2:
- fix documentation of cpc_header_number_in_window which documented a unexisting parameter end
- simplify implementation of cpc_header_get_frames_acked_count

 drivers/greybus/cpc/cpc.h      |  9 ++++
 drivers/greybus/cpc/cport.c    |  5 +++
 drivers/greybus/cpc/header.c   | 78 ++++++++++++++++++++++++++++++++++
 drivers/greybus/cpc/header.h   |  6 +++
 drivers/greybus/cpc/host.c     |  9 ----
 drivers/greybus/cpc/host.h     |  1 -
 drivers/greybus/cpc/protocol.c | 72 ++++++++++++++++++++++++++++---
 7 files changed, 163 insertions(+), 17 deletions(-)
drivers/greybus/cpc/cpc.h +9 -0
index f1669585c45..e8cbe916630 100644
@@ -18,6 +18,7 @@
  * @cpc_hd: pointer to the CPC host device this cport belongs to
  * @lock: mutex to synchronize accesses to tcb and other attributes
  * @holding_queue: list of frames queued to be sent
+ * @retx_queue: list of frames sent and waiting for acknowledgment
  * @tcb: Transmission Control Block
  */
 struct cpc_cport {
@@ -27,12 +28,20 @@ struct cpc_cport {
 	struct mutex lock; /* Synchronize access to state variables */
 
 	struct sk_buff_head holding_queue;
+	struct sk_buff_head retx_queue;
 
 	/*
+	 * @send_wnd: send window, maximum number of frames that the remote can accept.
+	 *            TX frames should have a sequence in the range [send_una; send_una + send_wnd]
+	 * @send_nxt: send next, the next sequence number that will be used for transmission
+	 * @send_una: send unacknowledged, the oldest unacknowledged sequence number
 	 * @ack: current acknowledge number
 	 * @seq: current sequence number
 	 */
 	struct {
+		u8 send_wnd;
+		u8 send_nxt;
+		u8 send_una;
 		u8 ack;
 		u8 seq;
 	} tcb;
drivers/greybus/cpc/cport.c +5 -0
index 91c39856e22..c05d89709f0 100644
@@ -16,6 +16,9 @@ static void cpc_cport_tcb_reset(struct cpc_cport *cport)
 {
 	cport->tcb.ack = 0;
 	cport->tcb.seq = 0;
+	cport->tcb.send_nxt = 0;
+	cport->tcb.send_una = 0;
+	cport->tcb.send_wnd = 1;
 }
 
 /**
@@ -38,12 +41,14 @@ struct cpc_cport *cpc_cport_alloc(u16 cport_id, gfp_t gfp_mask)
 
 	mutex_init(&cport->lock);
 	skb_queue_head_init(&cport->holding_queue);
+	skb_queue_head_init(&cport->retx_queue);
 
 	return cport;
 }
 
 void cpc_cport_release(struct cpc_cport *cport)
 {
+	skb_queue_purge(&cport->retx_queue);
 	skb_queue_purge(&cport->holding_queue);
 	kfree(cport);
 }
drivers/greybus/cpc/header.c +78 -0

93 lines not on this page · raw message

drivers/greybus/cpc/header.h +6 -0
index bf2ef41d44d..8c03aed28fa 100644
@@ -42,8 +42,14 @@ struct cpc_header {
 #define GREYBUS_HEADER_SIZE (sizeof(struct gb_operation_msg_hdr))
 
 bool cpc_header_is_control(const struct cpc_header *hdr);
+u8 cpc_header_get_recv_wnd(const struct cpc_header *hdr);
 u8 cpc_header_get_seq(const struct cpc_header *hdr);
+u8 cpc_header_get_ack(const struct cpc_header *hdr);
 bool cpc_header_get_req_ack(const struct cpc_header *hdr);
 u8 cpc_header_encode_ctrl_flags(bool control, bool req_ack);
 
+u8 cpc_header_get_frames_acked_count(u8 seq, u8 ack);
+bool cpc_header_number_in_window(u8 start, u8 wnd, u8 n);
+bool cpc_header_number_in_range(u8 start, u8 end, u8 n);
+
 #endif
drivers/greybus/cpc/host.c +0 -9
index ec43d33dfc6..a7715c0a960 100644
@@ -199,15 +199,6 @@ void cpc_hd_del(struct cpc_host_device *cpc_hd)
 }
 EXPORT_SYMBOL_GPL(cpc_hd_del);
 
-void cpc_hd_message_sent(struct sk_buff *skb, int status)
-{
-	struct cpc_host_device *cpc_hd = CPC_SKB_CB(skb)->cport->cpc_hd;
-	struct gb_host_device *hd = cpc_hd->gb_hd;
-
-	greybus_message_sent(hd, CPC_SKB_CB(skb)->gb_message, status);
-}
-EXPORT_SYMBOL_GPL(cpc_hd_message_sent);
-
 void cpc_hd_rcvd(struct cpc_host_device *cpc_hd, struct sk_buff *skb)
 {
 	struct gb_operation_msg_hdr *gb_hdr;
drivers/greybus/cpc/host.h +0 -1
index cc835f5298b..8f05877b2be 100644
@@ -46,7 +46,6 @@ int cpc_hd_add(struct cpc_host_device *cpc_hd);
 void cpc_hd_put(struct cpc_host_device *cpc_hd);
 void cpc_hd_del(struct cpc_host_device *cpc_hd);
 void cpc_hd_rcvd(struct cpc_host_device *cpc_hd, struct sk_buff *skb);
-void cpc_hd_message_sent(struct sk_buff *skb, int status);
 
 int cpc_hd_send_skb(struct cpc_host_device *cpc_hd, struct sk_buff *skb);
 
drivers/greybus/cpc/protocol.c +65 -7

125 lines not on this page · raw message

Signature · 1 line
2.52.0
#16 lore raw

PATCH v2 12/14 greybus: cpc: let host device drivers dequeue TX frames

In reply to Damien Riégel

This lets the CPC host device drivers dequeue frames when it's convenient for them to do so, instead of forcing each to them to implement a queue to store pending skbs.

The callback is changed from transmit to wake_tx and let CPC core notify these drivers when there is something to transmit.

---
 drivers/greybus/cpc/host.c | 74 ++++++++++++++++++++++++++++++++++++--
 drivers/greybus/cpc/host.h | 12 +++++--
 2 files changed, 81 insertions(+), 5 deletions(-)
drivers/greybus/cpc/host.c +71 -3
index a7715c0a960..54f0b07efec 100644
@@ -155,6 +155,7 @@ static struct gb_hd_driver cpc_gb_driver = {
 static void cpc_hd_init(struct cpc_host_device *cpc_hd)
 {
 	mutex_init(&cpc_hd->lock);
+	skb_queue_head_init(&cpc_hd->tx_queue);
 }
 
 struct cpc_host_device *cpc_hd_create(struct cpc_hd_driver *driver, struct device *parent)
@@ -162,7 +163,7 @@ struct cpc_host_device *cpc_hd_create(struct cpc_hd_driver *driver, struct devic
 	struct cpc_host_device *cpc_hd;
 	struct gb_host_device *hd;
 
-	if (!driver->transmit) {
+	if (!driver->wake_tx) {
 		dev_err(parent, "missing mandatory callback\n");
 		return ERR_PTR(-EINVAL);
 	}
@@ -231,13 +232,80 @@ EXPORT_SYMBOL_GPL(cpc_hd_rcvd);
  * @cpc_hd: Host device to send SKB over.
  * @skb: SKB to send.
  */
-int cpc_hd_send_skb(struct cpc_host_device *cpc_hd, struct sk_buff *skb)
+void cpc_hd_send_skb(struct cpc_host_device *cpc_hd, struct sk_buff *skb)
 {
 	const struct cpc_hd_driver *drv = cpc_hd->driver;
 
-	return drv->transmit(cpc_hd, skb);
+	mutex_lock(&cpc_hd->lock);
+	skb_queue_tail(&cpc_hd->tx_queue, skb);
+	mutex_unlock(&cpc_hd->lock);
+
+	drv->wake_tx(cpc_hd);
 }
 
+/**
+ * cpc_hd_tx_queue_empty() - Check if transmit queue is empty.
+ * @cpc_hd: CPC Host Device.
+ *
+ * Return: True if transmit queue is empty, false otherwise.
+ */
+bool cpc_hd_tx_queue_empty(struct cpc_host_device *cpc_hd)
+{
+	bool empty;
+
+	mutex_lock(&cpc_hd->lock);
+	empty = skb_queue_empty(&cpc_hd->tx_queue);
+	mutex_unlock(&cpc_hd->lock);
+
+	return empty;
+}
+EXPORT_SYMBOL_GPL(cpc_hd_tx_queue_empty);
+
+/**
+ * cpc_hd_dequeue() - Get the next SKB that was queued for transmission.
+ * @cpc_hd: CPC Host Device.
+ *
+ * Get an SKB that was previously queued by cpc_hd_send_skb().
+ *
+ * Return: An SKB, or %NULL if queue was empty.
+ */
+struct sk_buff *cpc_hd_dequeue(struct cpc_host_device *cpc_hd)
+{
+	struct sk_buff *skb;
+
+	mutex_lock(&cpc_hd->lock);
+	skb = skb_dequeue(&cpc_hd->tx_queue);
+	mutex_unlock(&cpc_hd->lock);
+
+	return skb;
+}
+EXPORT_SYMBOL_GPL(cpc_hd_dequeue);
+
+/**
+ * cpc_hd_dequeue_many() - Get the next max_frames SKBs that were queued for transmission.
+ * @cpc_hd: CPC host device.
+ * @frame_list: Caller-provided sk_buff_head to fill with dequeued frames.
+ * @max_frames: Maximum number of frames to dequeue.
+ *
+ * Return: Number of frames actually dequeued.
+ */
+u32 cpc_hd_dequeue_many(struct cpc_host_device *cpc_hd, struct sk_buff_head *frame_list,
+			unsigned int max_frames)
+{
+	struct sk_buff *skb;
+	unsigned int count = 0;
+
+	mutex_lock(&cpc_hd->lock);
+	while (count < max_frames && (skb = skb_dequeue(&cpc_hd->tx_queue))) {
+		skb_queue_tail(frame_list, skb);
+		count++;
+	}
+	mutex_unlock(&cpc_hd->lock);
+
+	return count;
+}
+EXPORT_SYMBOL_GPL(cpc_hd_dequeue_many);
+
 MODULE_DESCRIPTION("Greybus over CPC");
 MODULE_LICENSE("GPL");
 MODULE_AUTHOR("Silicon Laboratories, Inc.");
drivers/greybus/cpc/host.h +10 -2
index 8f05877b2be..ee6a86de309 100644
@@ -9,6 +9,7 @@
 #include <linux/device.h>
 #include <linux/greybus.h>
 #include <linux/mutex.h>
+#include <linux/skbuff.h>
 #include <linux/types.h>
 
 #define GB_CPC_MSG_SIZE_MAX 4096
@@ -18,7 +19,7 @@ struct cpc_cport;
 struct cpc_host_device;
 
 struct cpc_hd_driver {
-	int (*transmit)(struct cpc_host_device *hd, struct sk_buff *skb);
+	int (*wake_tx)(struct cpc_host_device *cpc_hd);
 };
 
 /**
@@ -34,6 +35,8 @@ struct cpc_host_device {
 
 	struct mutex lock; /* Synchronize access to cports */
 	struct cpc_cport *cports[GB_CPC_NUM_CPORTS];
+
+	struct sk_buff_head tx_queue;
 };
 
 static inline struct device *cpc_hd_dev(struct cpc_host_device *cpc_hd)
@@ -47,6 +50,11 @@ void cpc_hd_put(struct cpc_host_device *cpc_hd);
 void cpc_hd_del(struct cpc_host_device *cpc_hd);
 void cpc_hd_rcvd(struct cpc_host_device *cpc_hd, struct sk_buff *skb);
 
-int cpc_hd_send_skb(struct cpc_host_device *cpc_hd, struct sk_buff *skb);
+void cpc_hd_send_skb(struct cpc_host_device *cpc_hd, struct sk_buff *skb);
+
+bool cpc_hd_tx_queue_empty(struct cpc_host_device *cpc_hd);
+struct sk_buff *cpc_hd_dequeue(struct cpc_host_device *cpc_hd);
+u32 cpc_hd_dequeue_many(struct cpc_host_device *cpc_hd, struct sk_buff_head *frame_list,
+			unsigned int max_frames);
 
 #endif
Signature · 1 line
2.52.0
#17 lore raw

PATCH v2 13/14 greybus: cpc: add private data pointer in CPC Host Device

In reply to Damien Riégel

Add a private data pointer when creating a CPC Host Device. This lets the host device drivers get back their context more easily in the callbacks.

---
 drivers/greybus/cpc/host.c | 4 +++-
 drivers/greybus/cpc/host.h | 5 ++++-
 2 files changed, 7 insertions(+), 2 deletions(-)
drivers/greybus/cpc/host.c +3 -1
index 54f0b07efec..2784207279e 100644
@@ -158,7 +158,8 @@ static void cpc_hd_init(struct cpc_host_device *cpc_hd)
 	skb_queue_head_init(&cpc_hd->tx_queue);
 }
 
-struct cpc_host_device *cpc_hd_create(struct cpc_hd_driver *driver, struct device *parent)
+struct cpc_host_device *cpc_hd_create(struct cpc_hd_driver *driver, struct device *parent,
+				      void *priv)
 {
 	struct cpc_host_device *cpc_hd;
 	struct gb_host_device *hd;
@@ -175,6 +176,7 @@ struct cpc_host_device *cpc_hd_create(struct cpc_hd_driver *driver, struct devic
 	cpc_hd = gb_hd_to_cpc_hd(hd);
 	cpc_hd->gb_hd = hd;
 	cpc_hd->driver = driver;
+	cpc_hd->priv = priv;
 
 	cpc_hd_init(cpc_hd);
 
drivers/greybus/cpc/host.h +4 -1
index ee6a86de309..4bb7339b394 100644
@@ -37,6 +37,8 @@ struct cpc_host_device {
 	struct cpc_cport *cports[GB_CPC_NUM_CPORTS];
 
 	struct sk_buff_head tx_queue;
+
+	void *priv;
 };
 
 static inline struct device *cpc_hd_dev(struct cpc_host_device *cpc_hd)
@@ -44,7 +46,8 @@ static inline struct device *cpc_hd_dev(struct cpc_host_device *cpc_hd)
 	return &cpc_hd->gb_hd->dev;
 }
 
-struct cpc_host_device *cpc_hd_create(struct cpc_hd_driver *driver, struct device *parent);
+struct cpc_host_device *cpc_hd_create(struct cpc_hd_driver *driver, struct device *parent,
+				      void *priv);
 int cpc_hd_add(struct cpc_host_device *cpc_hd);
 void cpc_hd_put(struct cpc_host_device *cpc_hd);
 void cpc_hd_del(struct cpc_host_device *cpc_hd);
Signature · 1 line
2.52.0
#18 lore raw

PATCH v2 14/14 greybus: cpc: add CPC SDIO host driver

In reply to Damien Riégel

From: Gabriel Beaulieu <[email protected]>

This introduces a new module gb-cpc-sdio, in order to communicate with a Greybus CPC device over SDIO.

Most of the complexity stems from aggregation: packets are aggregated to minimize the number of CMD53s. In the first block, the first le32 is the number of packets in this transfer. Immediately after that are all the packet headers (CPC + Greybus). This lets the device process all the headers in a single interrupt, and prepare the ADMA descriptors for all the payloads in one go.

Payloads start at the beginning of the second block and are concatained. Their lengths must be 32-bit aligned, so the driver takes care of adding and removing padding if necessary.

---
Changes in v2:
- change formatting from %lu to %zu when printing size_t's
- remove "/**" kernel-doc marker for static functions not actually using the kernel-doc format
- reduce header inclusion list
- use reverse christmas tree variable declarations consistently
- update aggregation functions to try to be more legible
- use define instead of constant value 0x0C for the address where to read the number of bytes the device wants to send
- remove padding between headers and payloads when aggregating packets

 drivers/greybus/cpc/Kconfig  |  12 +
 drivers/greybus/cpc/Makefile |   3 +
 drivers/greybus/cpc/sdio.c   | 533 +++++++++++++++++++++++++++++++++++
 3 files changed, 548 insertions(+)
 create mode 100644 drivers/greybus/cpc/sdio.c
drivers/greybus/cpc/Kconfig +12 -0
index ab96fedd0de..8223f56795f 100644
@@ -8,3 +8,15 @@ config GREYBUS_CPC
 
 	  To compile this code as a module, chose M here: the module will be
 	  called gb-cpc.ko
+
+config GREYBUS_CPC_SDIO
+	tristate "Greybus CPC over SDIO"
+	depends on GREYBUS_CPC && MMC
+	help
+	  This driver provides Greybus CPC host support for devices
+	  connected via SDIO interface.
+
+	  To compile this driver as a module, choose M here: the module
+	  will be called gb-cpc-sdio.
+
+	  If unsure, say N.
drivers/greybus/cpc/Makefile +3 -0
index c4b530d27a3..3296536e86d 100644
@@ -4,3 +4,6 @@ gb-cpc-y := cport.o header.o host.o protocol.o
 
 # CPC core
 obj-$(CONFIG_GREYBUS_CPC)	+= gb-cpc.o
+
+gb-cpc-sdio-y := sdio.o
+obj-$(CONFIG_GREYBUS_CPC_SDIO)	+= gb-cpc-sdio.o
drivers/greybus/cpc/sdio.c new file +533 -0

533 lines not on this page · raw message

Signature · 1 line
2.52.0
#19 lore raw

Re: [PATCH v2 14/14] greybus: cpc: add CPC SDIO host driver

Quoted 36 lines · On Thursday 15 January 2026 16:58:07 Central European Standard Time Damien…

From: Gabriel Beaulieu <[email protected]>

This introduces a new module gb-cpc-sdio, in order to communicate with a Greybus CPC device over SDIO.

Most of the complexity stems from aggregation: packets are aggregated to minimize the number of CMD53s. In the first block, the first le32 is the number of packets in this transfer. Immediately after that are all the packet headers (CPC + Greybus). This lets the device process all the headers in a single interrupt, and prepare the ADMA descriptors for all the payloads in one go.

Payloads start at the beginning of the second block and are concatained. Their lengths must be 32-bit aligned, so the driver takes care of adding and removing padding if necessary.

---
Changes in v2:
- change formatting from %lu to %zu when printing size_t's
- remove "/**" kernel-doc marker for static functions not actually using the kernel-doc format
- reduce header inclusion list
- use reverse christmas tree variable declarations consistently
- update aggregation functions to try to be more legible
- use define instead of constant value 0x0C for the address where to read the number of bytes the device wants to send
- remove padding between headers and payloads when aggregating packets

 drivers/greybus/cpc/Kconfig  |  12 +
 drivers/greybus/cpc/Makefile |   3 +
 drivers/greybus/cpc/sdio.c   | 533 +++++++++++++++++++++++++++++++++++
 3 files changed, 548 insertions(+)
 create mode 100644 drivers/greybus/cpc/sdio.c

[...]

Quoted 6 lines
drivers/greybus/cpc/sdio.c new file +0 -0
new file mode 100644
index 00000000000..aeeb8378dea
@@ -0,0 +1,533 @@

[...]

Quoted 62 lines
drivers/greybus/cpc/sdio.c new file +62 -0
+static void gb_cpc_sdio_rx_tx(struct cpc_sdio *ctx)
+{
+	gb_cpc_sdio_rx(ctx);
+
+	set_bit(CPC_SDIO_FLAG_IRQ_RUNNING, &ctx->flags);
+	gb_cpc_sdio_tx(ctx);
+	clear_bit(CPC_SDIO_FLAG_IRQ_RUNNING, &ctx->flags);
+}
+
+static void gb_cpc_sdio_tx_work(struct work_struct *work)
+{
+	struct cpc_sdio *ctx = container_of(work, struct cpc_sdio, tx_work);
+
+	/* Do not execute concurrently to the interrupt */
+	if (test_bit(CPC_SDIO_FLAG_IRQ_RUNNING, &ctx->flags)) {
+		set_bit(CPC_SDIO_FLAG_TX_WORK_DELAYED, &ctx->flags);
+		return;
+	}
+
+	gb_cpc_sdio_tx(ctx);
+}
+
+static struct cpc_hd_driver cpc_sdio_driver = {
+	.wake_tx = gb_cpc_sdio_wake_tx,
+};
+
+static int cpc_sdio_init(struct sdio_func *func)
+{
+	unsigned char rx_data_ready_irq_en_bit = BIT(0);
+	unsigned int irq_enable_addr = 0x09;
+	int err;
+
+	/* Enable the read data ready interrupt. */
+	sdio_writeb(func, rx_data_ready_irq_en_bit, irq_enable_addr, &err);
+	if (err)
+		dev_err(&func->dev, "failed to set data ready interrupt (%d)\n", err);
+
+	return err;
+}
+
+static void cpc_sdio_irq_handler(struct sdio_func *func)
+{
+	unsigned int rx_data_pending_irq_bit = 0;
+	unsigned int irq_status_addr = 0x08;
+	unsigned long int_status;
+	struct cpc_sdio *ctx;
+	struct device *dev;
+	int err;
+
+	ctx = sdio_get_drvdata(func);
+	dev = &func->dev;
+
+	int_status = sdio_readb(func, irq_status_addr, &err);
+	if (err) {
+		dev_err(dev, "failed to read interrupt status registers (%d)\n", err);
+		return;
+	}
+
+	if (__test_and_clear_bit(rx_data_pending_irq_bit, &int_status)) {
+		/* Clear the IRQ on the device side. */
+		sdio_writeb(func, BIT(rx_data_pending_irq_bit), irq_status_addr, &err);
+		if (err) {

drivers/greybus/cpc/sdio.c line 411

Quoted 10 lines
drivers/greybus/cpc/sdio.c new file +10 -0
+			dev_err(dev, "failed to clear read interrupt (%d), interrupt will repeat\n",
+				err);
+			return;
+		}
+
+		cancel_work_sync(&ctx->tx_work);
+		gb_cpc_sdio_rx_tx(ctx);
+
+		if (test_and_clear_bit(CPC_SDIO_FLAG_TX_WORK_DELAYED, &ctx->flags))
+			schedule_work(&ctx->tx_work);

From my experience, data path is easier to debug when all the data are sent (and received) from the same context. Don't you think you could call gb_cpc_sdio_rx() from the workqueue:

   if (__test_and_clear_bit(0, &int_status)) {
          sdio_writeb(func, BIT(0), irq_status_addr, &err);
	atomic_inc(&rx_wait);
          schedule_work(&ctx->rxtx_work);

}

Signature · 1 line
Jérôme Pouiller
#20 lore raw

Re: [PATCH v2 14/14] greybus: cpc: add CPC SDIO host driver

In reply to Damien Riégel
Quoted 41 lines · On Thursday 15 January 2026 16:58:07 Central European Standard Time Damien…

From: Gabriel Beaulieu <[email protected]>

This introduces a new module gb-cpc-sdio, in order to communicate with a Greybus CPC device over SDIO.

Most of the complexity stems from aggregation: packets are aggregated to minimize the number of CMD53s. In the first block, the first le32 is the number of packets in this transfer. Immediately after that are all the packet headers (CPC + Greybus). This lets the device process all the headers in a single interrupt, and prepare the ADMA descriptors for all the payloads in one go.

Payloads start at the beginning of the second block and are concatained. Their lengths must be 32-bit aligned, so the driver takes care of adding and removing padding if necessary.

---
Changes in v2:
- change formatting from %lu to %zu when printing size_t's
- remove "/**" kernel-doc marker for static functions not actually using the kernel-doc format
- reduce header inclusion list
- use reverse christmas tree variable declarations consistently
- update aggregation functions to try to be more legible
- use define instead of constant value 0x0C for the address where to read the number of bytes the device wants to send
- remove padding between headers and payloads when aggregating packets

 drivers/greybus/cpc/Kconfig  |  12 +
 drivers/greybus/cpc/Makefile |   3 +
 drivers/greybus/cpc/sdio.c   | 533 +++++++++++++++++++++++++++++++++++
 3 files changed, 548 insertions(+)
 create mode 100644 drivers/greybus/cpc/sdio.c
drivers/greybus/cpc/Kconfig +0 -0
index ab96fedd0de..8223f56795f 100644

drivers/greybus/cpc/Kconfig config GREYBUS_CPC · line 14

Quoted 7 lines
drivers/greybus/cpc/Kconfig +4 -0
@@ -8,3 +8,15 @@ config GREYBUS_CPC
 
 	  To compile this code as a module, chose M here: the module will be
 	  called gb-cpc.ko
+
+config GREYBUS_CPC_SDIO
+	tristate "Greybus CPC over SDIO"
+	depends on GREYBUS_CPC && MMC

Don't you think, this PR should also be sent to the SDIO maintainers ([email protected])?

drivers/greybus/cpc/Kconfig config GREYBUS_CPC · line 17

Quoted 3 lines
drivers/greybus/cpc/Kconfig +3 -0
+	help
+	  This driver provides Greybus CPC host support for devices
+	  connected via SDIO interface.

For now, I believe only some Silicon Labs device provide this interface. Maybe it would help the user to mention that.

Quoted 5 lines
drivers/greybus/cpc/Kconfig +5 -0
+
+	  To compile this driver as a module, choose M here: the module
+	  will be called gb-cpc-sdio.
+
+	  If unsure, say N.

[...]

Quoted 13 lines
drivers/greybus/cpc/sdio.c new file +7 -0
new file mode 100644
index 00000000000..aeeb8378dea
@@ -0,0 +1,533 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (c) 2025, Silicon Laboratories, Inc.
+ */
+
+#include <linux/kthread.h>
+#include <linux/mmc/sdio_func.h>

drivers/greybus/cpc/sdio.c line 17

Quoted 10 lines
drivers/greybus/cpc/sdio.c new file +10 -0
+#include <linux/mmc/sdio_ids.h>
+#include <linux/skbuff.h>
+#include <linux/workqueue.h>
+
+#include "cpc.h"
+#include "header.h"
+#include "host.h"
+
+#define GB_CPC_SDIO_MSG_SIZE_MAX 4096
+#define GB_CPC_SDIO_BLOCK_SIZE 256U

What is the purpose of the "U"?

drivers/greybus/cpc/sdio.c line 19

Quoted 2 lines
drivers/greybus/cpc/sdio.c new file +2 -0
+#define GB_CPC_SDIO_FIFO_ADDR 0
+#define GB_CPC_SDIO_RX_BYTES_CNT_ADDR 0x0C

It took me a bit of time to understand the meaning of these two definitions. I suggest to separate them from the other defines and use a common prefix:

  #define GB_CPC_SDIO_ADDR_FIFO         0x00
  #define GB_CPC_SDIO_ADDR_RX_BYTES_CNT 0x0c

drivers/greybus/cpc/sdio.c line 21

Quoted 2 lines
drivers/greybus/cpc/sdio.c new file +2 -0
+#define GB_CPC_SDIO_ALIGNMENT 4
+#define GB_CPC_SDIO_DEFAULT_AGGREGATION 1

This number seems arbitrary and it used only once. I feel it just add one layer of indirection rather than improving the readability.

drivers/greybus/cpc/sdio.c line 23

Quoted 2 lines
drivers/greybus/cpc/sdio.c new file +2 -0
+#define CPC_FRAME_HEADER_SIZE (CPC_HEADER_SIZE + GREYBUS_HEADER_SIZE)
+#define GB_CPC_SDIO_MAX_AGGREGATION ((GB_CPC_SDIO_BLOCK_SIZE - sizeof(u32)) / CPC_FRAME_HEADER_SIZE)

Orphan?

Quoted 1 line
drivers/greybus/cpc/sdio.c new file +1 -0
+

[...]

drivers/greybus/cpc/sdio.c line 60

Quoted 8 lines
drivers/greybus/cpc/sdio.c new file +8 -0
+static int gb_cpc_sdio_wake_tx(struct cpc_host_device *cpc_hd)
+{
+	struct cpc_sdio *ctx = cpc_hd_to_cpc_sdio(cpc_hd);
+
+	if (test_bit(CPC_SDIO_FLAG_SHUTDOWN, &ctx->flags))
+		return 0;
+
+	/* Use system workqueue for TX processing */

I wonder if these trivial comments are useful or rather pollute the code.

[...]

drivers/greybus/cpc/sdio.c line 512

Quoted 3 lines
drivers/greybus/cpc/sdio.c new file +3 -0
+static const struct sdio_device_id sdio_ids[] = {
+       {
+               SDIO_DEVICE(0x0296, 0x5347),
                              ^^^^^^

You have to add an entry in include/linux/mmc/sdio_ids.h. But, it seems there is a conflict with SDIO_VENDOR_ID_MICROCHIP_WILC.

Quoted 3 lines
drivers/greybus/cpc/sdio.c new file +3 -0
+       },
+       {},
+};

[...]

Signature · 1 line
Jérôme Pouiller
Reviewed-by on the v2 series
#21 lore raw
In reply to Damien Riégel
Quoted 39 lines · On Thursday 15 January 2026 16:57:53 Central European Standard Time Damien…

Hi,

This patchset brings support for Silicon Labs' CPC protocol as transport layer for Greybus. This is introduced as a module that sits between Greybus and CPC Host Device Drivers implementations, like SDIO or SPI. This patchset includes SDIO as physical layer.

        +----------------------------------------------------+
        |                      Greybus                       |
        +----------------------------------------------------+
                                 /|\
                                  |
                                 \|/
        +----------------------------------------------------+
        |                        CPC                         |
        +----------------------------------------------------+
              /|\                /|\                /|\
               |                  |                  |
              \|/                \|/                \|/
          +----------+       +---------+       +-----------+
          |   SDIO   |       |   SPI   |       |   Others  |
          +----------+       +---------+       +-----------+

CPC implements some of the features of Unipro that Greybus relies upon, like reliable transmission. CPC takes care of detecting transmission errors and retransmit frames if necessary, but that feature is not part of the RFC to keep it concise. There's also a flow-control feature, preventing sending messages to already full cports.

In order to implement these features, a 4-byte header is prepended to Greybus messages, making the whole header 12 bytes (Greybus header is 8 bytes).

This RFC starts by implementing a shim layer between physical bus drivers (like SDIO and SPI) and Greybus, and progressively add more elements to it to make it useful in its own right. Finally, an SDIO driver is added to enable the communication with a remote device.

Quoted 5 lines

Changes in v2:
- addressed review comments and errors reported by kernel bot
- for SDIO driver, remove padding between headers and payloads when aggregating packets together

I have to review this PR for a while. Sorry for the delay. I should have provided my feedback for the v1.

For now, the abstract interface seems a bit over-engineered. However, I assume will make sense once other buses will be added. Globally, this work looks good to me.

Until now, I was was a bit frustrated Greybus for Zephyr only addresses devices with network capabilities. I hope to be able to easily expose the services of my Zephyr devices to my Linux host with this PR.

Beside my small comments:

Signature · 1 line
Jérôme Pouiller
#22 lore raw

On Thursday 15 January 2026 22:46:43 Central European Standard Time Jérôme Pouiller wrote:
[...]

Quoted 3 lines

Beside my small comments:

I have given my review a bit to quickly. The SDIO VID conflict in patch 14 has to be fixed.

Signature · 1 line
Jérôme Pouiller