bus: mhi: Add loopback driver
Subsystems (2)
v3 cover bus: mhi: Add loopback driver
The MHI specification defines a LOOPBACK channel that is already implemented by MHI-based devices (modems, WLAN) deployed in the field. The endpoint firmware echoes back whatever the host sends on this channel. Without a host-side driver, there is no way to exercise this channel to validate MHI data path integrity between host and endpoint.
This series adds drivers to exercise the LOOPBACK channel from both the host and endpoint sides. The host driver (patch 1) binds to the LOOPBACK channel and provides a sysfs interface for configuring transfer parameters, triggering a test, and reading the result. The sysfs interface is stable ABI because the wire protocol is fixed by the endpoint firmware already deployed in the field and cannot be changed.
The endpoint driver (patch 3) echoes received data back to the host using a workqueue for asynchronous processing. Patch 2 introduces the mhi_ep_queue_buf() API needed by the endpoint driver for raw buffer queuing without an skb dependency.
---
Changes in v3:
- Move ep driver to drivers/bus/mhi/ep/clients/loopback.c (Mani)
- Move host driver to drivers/bus/mhi/host/clients/loopback.c; keep
module name mhi_loopback (Bjorn, Mani)
- Add ABI documentation in Documentation/ABI/testing/sysfs-bus-mhi-devices-loopback (Bjorn, Mani)
- Rename sysfs attribute 'size' to 'tre_size'; add 'max_tre_size' attribute
- Update Kconfig title to 'MHI LOOPBACK client driver' and describe that
the driver binds to the MHI LOOPBACK channel defined in the MHI spec
(Mani).
- Fix memory leak in ep loopback DL transfer error path.
- Rename mhi_ep_skb_completion() to mhi_ep_buf_completion().
- Document buffer ownership semantics in mhi_ep_queue_buf() kernel-doc
- Fix use-after-free in host loopback
- Fix completion race: arm completion before queuing recv TREs
- Fix teardown race: synchronize mhi_loopback_remove() with start_store()
via lb_mutex
- Fix u32 multiplication overflow in total_size: use size_mul()
- Replace kmalloc+memcpy with kmemdup in ep loopback UL callback
- Update mhi_ep_queue_buf() kernel-doc: note per-TRE callback behavior
when buffer length spans multiple host DL TREs
- Move mhi_prepare_for_transfer()/mhi_unprepare_from_transfer() into start_store() to avoid holding the channel open when idle
- Link to v2: https://lore.kernel.org/r/[email protected]
Changes in v2:
- Use __free(kfree) macro for buffers
- Removed NET layer socket buffer dependency, now using buffer and len
- Created a New Api for queuing buffers for clients which do not use skb
- Link to v1: https://lore.kernel.org/r/[email protected]
---
Sumit Kumar (3):
bus: mhi: host: clients: Add loopback driver with sysfs interface
bus: mhi: ep: Add mhi_ep_queue_buf() API for raw buffer queuing
bus: mhi: ep: clients: Add loopback driver for data path testing
.../ABI/testing/sysfs-bus-mhi-devices-loopback | 51 ++++ MAINTAINERS | 1 + drivers/bus/mhi/ep/Kconfig | 2 + drivers/bus/mhi/ep/Makefile | 1 + drivers/bus/mhi/ep/clients/Kconfig | 16 ++ drivers/bus/mhi/ep/clients/Makefile | 2 + drivers/bus/mhi/ep/clients/loopback.c | 122 ++++++++ drivers/bus/mhi/ep/main.c | 27 +- drivers/bus/mhi/host/Kconfig | 1 + drivers/bus/mhi/host/Makefile | 1 + drivers/bus/mhi/host/clients/Kconfig | 17 ++ drivers/bus/mhi/host/clients/Makefile | 2 + drivers/bus/mhi/host/clients/loopback.c | 316 +++++++++++++++++++++ include/linux/mhi_ep.h | 15 + 14 files changed, 566 insertions(+), 8 deletions(-)
---
base-commit: e6b9dce0aeeb91dfc0974ab87f02454e24566182
change-id: 20250903-loopback_mhi-dee55ff0d462
Best regards,
Signature · 1 line
Sumit Kumar <[email protected]>
PATCH v3 1/3 bus: mhi: host: clients: Add loopback driver with sysfs interface
The MHI specification defines a LOOPBACK channel. The endpoint firmware echoes back whatever the host sends on this channel. Without a host-side driver, there is no way to exercise this channel to validate MHI data path integrity between host and endpoint.
Add a host-side loopback driver that binds to the LOOPBACK channel and expose a sysfs interface for data path testing. The sysfs interface allows users to configure TRE buffer size and count, trigger a loopback test, and read the result.
--- .../ABI/testing/sysfs-bus-mhi-devices-loopback | 51 ++++ MAINTAINERS | 1 + drivers/bus/mhi/host/Kconfig | 1 + drivers/bus/mhi/host/Makefile | 1 + drivers/bus/mhi/host/clients/Kconfig | 17 ++ drivers/bus/mhi/host/clients/Makefile | 2 + drivers/bus/mhi/host/clients/loopback.c | 316 +++++++++++++++++++++ 7 files changed, 389 insertions(+)
Documentation/ABI/testing/sysfs-bus-mhi-devices-loopback new file +51 -0
@@ -0,0 +1,51 @@ +What: /sys/bus/mhi/devices/mhi<N>_LOOPBACK/tre_size +Date: April 2026 +KernelVersion: 7.1 +Contact: [email protected] +Description: + (RW) Size of each Transfer Ring Element (TRE) buffer in bytes + used for the loopback test. Valid range is 1 to the value + reported by max_tre_size. Default value is 32 bytes. + +What: /sys/bus/mhi/devices/mhi<N>_LOOPBACK/max_tre_size +Date: April 2026 +KernelVersion: 7.1 +Contact: [email protected] +Description: + (RO) Maximum allowed TRE size in bytes. Reading this file + returns the upper bound for the tre_size attribute. + +What: /sys/bus/mhi/devices/mhi<N>_LOOPBACK/num_tre +Date: April 2026 +KernelVersion: 7.1 +Contact: [email protected] +Description: + (RW) Number of Transfer Ring Elements (TREs) to use per + loopback test. Must be greater than zero and must not exceed + the channel ring capacity. Default value is 1. + +What: /sys/bus/mhi/devices/mhi<N>_LOOPBACK/start +Date: April 2026 +KernelVersion: 7.1 +Contact: [email protected] +Description: + (WO) Write any value to trigger a loopback test. The driver + sends random data to the endpoint using the configured tre_size + and num_tre parameters, waits for the endpoint to echo it back, + and verifies the received data matches what was sent. + + This is a blocking write that returns when the test completes + or times out after 5 seconds. + +What: /sys/bus/mhi/devices/mhi<N>_LOOPBACK/status +Date: April 2026 +KernelVersion: 7.1 +Contact: [email protected] +Description: + (RO) Result of the last loopback test. Returns one of: + "pass" - last test completed successfully + "fail" - last test failed + "not started" - no test has been run yet + + Reading this file while a test is in progress will block + until the test completes.
MAINTAINERS +1 -0
@@ -16441,6 +16441,7 @@ L: [email protected] S: Maintained T: git git://git.kernel.org/pub/scm/linux/kernel/git/mani/mhi.git F: Documentation/ABI/stable/sysfs-bus-mhi +F: Documentation/ABI/testing/sysfs-bus-mhi-devices-loopback F: Documentation/mhi/ F: drivers/bus/mhi/ F: drivers/pci/endpoint/functions/pci-epf-mhi.c
drivers/bus/mhi/host/Kconfig +1 -0
@@ -29,3 +29,4 @@ config MHI_BUS_PCI_GENERIC This driver provides MHI PCI controller driver for devices such as Qualcomm SDX55 based PCIe modems. +source "drivers/bus/mhi/host/clients/Kconfig"
drivers/bus/mhi/host/Makefile +1 -0
@@ -4,3 +4,4 @@ mhi-$(CONFIG_MHI_BUS_DEBUG) += debugfs.o obj-$(CONFIG_MHI_BUS_PCI_GENERIC) += mhi_pci_generic.o mhi_pci_generic-y += pci_generic.o +obj-y += clients/
drivers/bus/mhi/host/clients/Kconfig new file +17 -0
@@ -0,0 +1,17 @@ +# SPDX-License-Identifier: GPL-2.0 + +config MHI_BUS_LOOPBACK + tristate "MHI LOOPBACK client driver" + depends on MHI_BUS + help + MHI LOOPBACK client driver that binds to the MHI LOOPBACK channel + as defined in the MHI specification. The LOOPBACK channel is + implemented by MHI-based devices (modems, WLAN) in the field, where + the endpoint firmware echoes back whatever the host sends. + + This driver exposes a sysfs interface for testing MHI data path + integrity between host and endpoint. Users can configure the TRE + size and count, trigger a loopback test, and read the result. + + To compile this driver as a module, choose M here. The module + will be called mhi_loopback.
drivers/bus/mhi/host/clients/Makefile new file +2 -0
@@ -0,0 +1,2 @@ +obj-$(CONFIG_MHI_BUS_LOOPBACK) += mhi_loopback.o +mhi_loopback-y += loopback.o
drivers/bus/mhi/host/clients/loopback.c new file +316 -0
316 lines not on this page · raw message
Signature · 1 line
2.34.1
PATCH v3 2/3 bus: mhi: ep: Add mhi_ep_queue_buf() API for raw buffer queuing
In reply to Sumit KumarSome MHI endpoint clients do not use socket buffers and need a way to queue raw buffers for DL transfers. Add mhi_ep_queue_buf() to support this use case.
Refactor mhi_ep_queue_skb() to delegate to a new internal mhi_ep_queue() helper shared by both APIs, and rename mhi_ep_skb_completion() to mhi_ep_buf_completion() to reflect its broader use.
--- drivers/bus/mhi/ep/main.c | 27 +++++++++++++++++++-------- include/linux/mhi_ep.h | 15 +++++++++++++++ 2 files changed, 34 insertions(+), 8 deletions(-)
drivers/bus/mhi/ep/main.c +19 -8
@@ -516,7 +516,7 @@ static int mhi_ep_process_ch_ring(struct mhi_ep_ring *ring)return 0 ; } -static void mhi_ep_skb_completion (struct mhi_ep_buf_info *buf_info) +static void mhi_ep_buf_completion (struct mhi_ep_buf_info *buf_info) {struct mhi_ep_device *mhi_dev = buf_info->mhi_dev;struct mhi_ep_cntrl *mhi_cntrl = mhi_dev->mhi_cntrl;
@@ -544,9 +544,9 @@ static void mhi_ep_skb_completion(struct mhi_ep_buf_info *buf_info)mhi_ep_ring_inc_index (ring); } -/* TODO: Handle partially formed TDs */ -int mhi_ep_queue_skb (struct mhi_ep_device *mhi_dev,struct sk_buff *skb) +static int mhi_ep_queue (struct mhi_ep_device *mhi_dev,void *buf,size_t len, +void *cb_buf) {struct mhi_ep_cntrl *mhi_cntrl = mhi_dev->mhi_cntrl;struct mhi_ep_chan *mhi_chan = mhi_dev->dl_chan;
@@ -559,7 +559,7 @@ int mhi_ep_queue_skb(struct mhi_ep_device *mhi_dev, struct sk_buff *skb)u32 tre_len;int ret; - buf_left = skb->len; + buf_left = len; ring = &mhi_cntrl->mhi_chan[mhi_chan->chan].ring;mutex_lock (&mhi_chan->lock);
@@ -582,13 +582,13 @@ int mhi_ep_queue_skb(struct mhi_ep_device *mhi_dev, struct sk_buff *skb) tre_len =MHI_TRE_DATA_GET_LEN (el); tr_len =min (buf_left, tre_len); - read_offset = skb->len - buf_left; + read_offset = len - buf_left; - buf_info.dev_addr = skb->data + read_offset; + buf_info.dev_addr = buf + read_offset; buf_info.host_addr =MHI_TRE_DATA_GET_PTR (el); buf_info.size = tr_len; - buf_info.cb = mhi_ep_skb_completion; - buf_info.cb_buf = skb; + buf_info.cb = mhi_ep_buf_completion; + buf_info.cb_buf = cb_buf; buf_info.mhi_dev = mhi_dev;/*
@@ -627,8 +627,19 @@ int mhi_ep_queue_skb(struct mhi_ep_device *mhi_dev, struct sk_buff *skb)return ret; } + +int mhi_ep_queue_skb (struct mhi_ep_device *mhi_dev,struct sk_buff *skb) +{ +return mhi_ep_queue (mhi_dev, skb->data, skb->len, skb); +}EXPORT_SYMBOL_GPL (mhi_ep_queue_skb); +int mhi_ep_queue_buf (struct mhi_ep_device *mhi_dev,void *buf,size_t len) +{ +return mhi_ep_queue (mhi_dev, buf, len, buf); +} +EXPORT_SYMBOL_GPL (mhi_ep_queue_buf); +static int mhi_ep_cache_host_cfg (struct mhi_ep_cntrl *mhi_cntrl) {size_t cmd_ctx_host_size, ch_ctx_host_size, ev_ctx_host_size;
include/linux/mhi_ep.h +15 -0
@@ -302,4 +302,19 @@ bool mhi_ep_queue_is_empty(struct mhi_ep_device *mhi_dev, enum dma_data_directio*/ int mhi_ep_queue_skb (struct mhi_ep_device *mhi_dev,struct sk_buff *skb); +/** +* mhi_ep_queue_buf - Send buffer to host over MHI Endpoint +* @mhi_dev: Device associated with the DL channel +* @buf: Buffer to be queued. On success, ownership passes to the MHI stack; +* the caller must not free @buf until the DL transfer callback fires +* with result->buf_addr equal to @buf. On failure, the caller retains +* ownership and must free @buf. +* Note: if @len spans multiple host DL TREs, the DL transfer callback +* fires once per TRE, each time with result->buf_addr equal to @buf. +* @len: Size of the buffer +* +* Return: 0 if the buffer has been sent successfully, a negative error code otherwise. +*/ +int mhi_ep_queue_buf (struct mhi_ep_device *mhi_dev,void *buf,size_t len); +#endif
Signature · 1 line
2.34.1
PATCH v3 3/3 bus: mhi: ep: clients: Add loopback driver for data path testing
In reply to Sumit KumarWhen an MHI endpoint device runs Linux, there is no firmware to implement the LOOPBACK channel echo that real modem firmware provides. Without an endpoint-side driver, the host loopback test has no software echo partner and cannot exercise the full end-to-end MHI data path.
Add an endpoint-side loopback driver that binds to the LOOPBACK channel and echoes received data back to the host. An ordered workqueue is used for asynchronous processing to preserve packet ordering. Together with the host-side loopback driver, this enables complete MHI data path validation for Linux-based endpoint devices.
--- drivers/bus/mhi/ep/Kconfig | 2 + drivers/bus/mhi/ep/Makefile | 1 + drivers/bus/mhi/ep/clients/Kconfig | 16 +++++ drivers/bus/mhi/ep/clients/Makefile | 2 + drivers/bus/mhi/ep/clients/loopback.c | 122 ++++++++++++++++++++++++++++++++++ 5 files changed, 143 insertions(+)
drivers/bus/mhi/ep/Kconfig +2 -0
@@ -8,3 +8,5 @@ config MHI_BUS_EP MHI_BUS_EP implements the MHI protocol for the endpoint devices, such as SDX55 modem connected to the host machine over PCIe. + +source "drivers/bus/mhi/ep/clients/Kconfig"
drivers/bus/mhi/ep/Makefile +1 -0
@@ -1,2 +1,3 @@ obj-$(CONFIG_MHI_BUS_EP) += mhi_ep.o mhi_ep-y := main.o mmio.o ring.o sm.o +obj-y += clients/
drivers/bus/mhi/ep/clients/Kconfig new file +16 -0
@@ -0,0 +1,16 @@ +# SPDX-License-Identifier: GPL-2.0 + +config MHI_BUS_EP_LOOPBACK + tristate "MHI Endpoint LOOPBACK client driver" + depends on MHI_BUS_EP + help + MHI Endpoint LOOPBACK client driver that binds to the MHI LOOPBACK + channel as defined in the MHI specification. The LOOPBACK channel is + implemented by MHI-based endpoint devices (modems, WLAN) in the field, + where the endpoint firmware echoes back whatever the host sends. + + This driver receives data on the uplink channel and echoes it back on + the downlink channel for testing the MHI endpoint data path. + + To compile this driver as a module, choose M here. The module + will be called mhi_ep_loopback.
drivers/bus/mhi/ep/clients/Makefile new file +2 -0
@@ -0,0 +1,2 @@ +obj-$(CONFIG_MHI_BUS_EP_LOOPBACK) += mhi_ep_loopback.o +mhi_ep_loopback-y += loopback.o
drivers/bus/mhi/ep/clients/loopback.c new file +122 -0
122 lines not on this page · raw message
Signature · 1 line
2.34.1