bus: mhi: host: Add debug port personality switching
Subsystems (2)
PATCH 1/2 bus: mhi: host: Add debug port personality switching
Modems expose two different sets of MHI channels depending on the product configuration they are running. In the commercial personality only the channels that carry user traffic are usable, so the host must not create WWAN ports for the diagnostic and debugger channels. In the debug personality those channels are active as well and userspace needs a device node for each of them, e.g. one for the diagnostic interface on top of the MBIM and AT ports.
Which channels are usable is a property of the firmware that is running, so the host cannot know it at probe time. It has to be told at runtime, after the device has booted into mission mode. There was no way to do
that: mhi_create_devices() unconditionally created a device for every configured channel whose EE mask matched, so the debug channels either always appeared or never did, depending on whether the controller driver declared them.
Add a per-channel debug_only flag and a debug_ports attribute that lets userspace switch the personality. Channels carrying the flag are skipped by mhi_create_devices() unless the debug personality is active, and are created or destroyed when it changes. The flag defaults to false, so existing controllers keep behaving exactly as before and the attribute describes itself as having no effect on a controller that does not declare any debug-only channel.
The attribute is part of the bus-wide attribute group, like the existing controller attributes. That group is applied to every MHI device, including the channel devices created here, so debug_ports_store() accepts a write only from the controller device. A write from a channel device would otherwise tear down the device that is executing the store.
Device creation and destruction cannot be done from the sysfs store directly. The state transitions create and destroy devices on hiprio_wq, and pm_mutex does not exclude them: they drop it before calling mhi_destroy_device() through device_for_each_child(). The request is therefore queued on that same ordered workqueue, which serialises it against the transitions. The store flushes the work before it returns, so the ports are in place when userspace regains control.
mhi_create_devices() is split so that the bidirectional pairing logic has a single instance. mhi_create_device() takes the channel index by reference and advances it past a UL/DL pair, which is what lets mhi_update_debug_devices() walk the channel array without revisiting the second half of a pair.
--- Documentation/ABI/stable/sysfs-bus-mhi | 14 ++ drivers/bus/mhi/host/init.c | 43 +++++++ drivers/bus/mhi/host/internal.h | 3 + drivers/bus/mhi/host/main.c | 171 ++++++++++++++++++------- include/linux/mhi.h | 16 +++ 5 files changed, 198 insertions(+), 49 deletions(-)
Documentation/ABI/stable/sysfs-bus-mhi +14 -0
@@ -30,6 +30,20 @@ Description: Initiates a SoC reset on the MHI controller. A SoC reset is non-responsive, or as a means of loading new firmware as a system administration task. +What: /sys/bus/mhi/devices/.../debug_ports +Date: November 2026 +KernelVersion: 7.5 +Contact: [email protected] +Description: Controls which set of ports the MHI controller enumerates. + Writing 0 selects the commercial personality, in which the + channels that are only useful for debugging are not + enumerated. Writing 1 selects the debug personality and + additionally enumerates those channels, so that the extra + WWAN ports appear, e.g. one for the diagnostic interface. + Reading returns the currently active personality. + Controllers that do not describe any debug-only channel are + unaffected by this attribute. + What: /sys/bus/mhi/devices/.../trigger_edl Date: April 2024 KernelVersion: 6.10
drivers/bus/mhi/host/init.c +43 -0
65 lines not on this page · raw message
drivers/bus/mhi/host/internal.h +3 -0
@@ -287,6 +287,7 @@ struct mhi_chan {bool configured;bool offload_ch;bool wake_capable; +bool debug_only; };/* Default MHI timeout */
@@ -320,6 +321,8 @@ struct mhi_device *mhi_alloc_device(struct mhi_controller *mhi_cntrl);int mhi_destroy_device (struct device *dev,void *data);void mhi_create_devices (struct mhi_controller *mhi_cntrl); +void mhi_update_debug_devices (struct mhi_controller *mhi_cntrl,bool enable); +void mhi_debug_ports_work (struct work_struct *work);int mhi_alloc_bhie_table (struct mhi_controller *mhi_cntrl,struct image_info **image_info,size_t alloc_size);
drivers/bus/mhi/host/main.c +122 -49
197 lines not on this page · raw message
include/linux/mhi.h +16 -0
@@ -234,6 +234,8 @@ enum mhi_db_brst_mode {* @offload_channel: The client manages the channel completely * @doorbell_mode_switch: Channel switches to doorbell mode on M0 transition * @wake_capable: Channel capable of waking up the system +* @debug_only: Channel is only enumerated when the controller is switched +* to the debug port personality (see mhi_controller.debug_ports) */ struct mhi_channel_config {char *name;
@@ -250,6 +252,7 @@ struct mhi_channel_config {bool offload_channel;bool doorbell_mode_switch;bool wake_capable; +bool debug_only; };/**
@@ -381,6 +384,16 @@ struct mhi_controller_config {* @no_m3: Device doesn't support M3 state * @irq_flags: irq flags passed to request_irq (optional) * @mru: the default MRU for the MHI device +* @debug_ports: Whether the debug port personality is active, i.e. whether the +* channels marked with debug_only are enumerated. Controlled +* from userspace through the debug_ports sysfs attribute and +* applied by debug_ports_work. Zero-initialised, so a +* controller starts in the commercial personality. +* @debug_ports_pending: Latest personality requested from userspace, applied +* by debug_ports_work +* @debug_ports_work: Applies @debug_ports_pending from the ordered high +* priority workqueue, so the device creation it performs +* cannot race with a state transition * * Fields marked as (required) need to be populated by the controller driver * before calling mhi_register_controller(). For the fields marked as (optional)
@@ -440,6 +453,9 @@ struct mhi_controller {struct work_struct st_worker;struct workqueue_struct *hiprio_wq;wait_queue_head_t state_event; +bool debug_ports; +bool debug_ports_pending; +struct work_struct debug_ports_work;void (*status_cb)(struct mhi_controller *mhi_cntrl,enum mhi_callback cb);
Signature · 1 line
2.40.0.windows.1
PATCH 2/2 bus: mhi: host: Add debug-only channel config macros
The debug_only flag added by the preceding patch has no users: set it per channel config, and there is no helper for the channel entries that need it, so every such entry would have to be spelled out in full.
Add MHI_CHANNEL_CONFIG_UL_DEBUG_ONLY() / _DL_DEBUG_ONLY() next to the existing MHI_CHANNEL_CONFIG_UL()/DL() macros. They are those macros with .debug_only = true and are used for channels that only exist in the debug port personality, i.e. the diagnostic and debugger channels.
The suffix is _DEBUG_ONLY rather than _DEBUG because the existing _SBL and _FP variants select a execution environment, while these macros still target MHI_EE_AMSS: only the enumeration condition differs. _DEBUG would suggest a third EE and mislead readers into looking for one.
Use them for the DIAG channels of modem_qcom_v1_mhi_channels, which is shared by the SDX55, SDX65 and SDX24 modems. The diagnostic port is what userspace gains by switching to the debug personality on those devices, and the QDSS trace channel they also expose is not declared for this channel set.
--- drivers/bus/mhi/host/pci_generic.c | 36 ++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-)
drivers/bus/mhi/host/pci_generic.c +34 -2
@@ -197,6 +197,38 @@ struct mhi_pci_dev_info { .doorbell_mode_switch = false, \ } +#define MHI_CHANNEL_CONFIG_UL_DEBUG_ONLY (ch_num, ch_name, el_count, ev_ring) \ + { \ + .num = ch_num, \ + .name = ch_name, \ + .num_elements = el_count, \ + .event_ring = ev_ring, \ + .dir = DMA_TO_DEVICE, \ + .ee_mask = BIT(MHI_EE_AMSS), \ + .pollcfg = 0, \ + .doorbell = MHI_DB_BRST_DISABLE, \ + .lpm_notify = false, \ + .offload_channel = false, \ + .doorbell_mode_switch = false, \ + .debug_only = true, \ + } + +#define MHI_CHANNEL_CONFIG_DL_DEBUG_ONLY (ch_num, ch_name, el_count, ev_ring) \ + { \ + .num = ch_num, \ + .name = ch_name, \ + .num_elements = el_count, \ + .event_ring = ev_ring, \ + .dir = DMA_FROM_DEVICE, \ + .ee_mask = BIT(MHI_EE_AMSS), \ + .pollcfg = 0, \ + .doorbell = MHI_DB_BRST_DISABLE, \ + .lpm_notify = false, \ + .offload_channel = false, \ + .doorbell_mode_switch = false, \ + .debug_only = true, \ + } +#define MHI_EVENT_CONFIG_DATA (ev_ring, el_count) \ { \ .num_elements = el_count, \
@@ -314,8 +346,8 @@ static struct mhi_event_config mhi_qcom_sa8775p_events[] = { };static const struct mhi_channel_config modem_qcom_v1_mhi_channels[] = { -MHI_CHANNEL_CONFIG_UL (4 ,"DIAG" ,16 ,1 ), -MHI_CHANNEL_CONFIG_DL (5 ,"DIAG" ,16 ,1 ), +MHI_CHANNEL_CONFIG_UL_DEBUG_ONLY (4 ,"DIAG" ,16 ,1 ), +MHI_CHANNEL_CONFIG_DL_DEBUG_ONLY (5 ,"DIAG" ,16 ,1 ),MHI_CHANNEL_CONFIG_UL (12 ,"MBIM" ,4 ,0 ),MHI_CHANNEL_CONFIG_DL (13 ,"MBIM" ,4 ,0 ),MHI_CHANNEL_CONFIG_UL (14 ,"QMI" ,4 ,0 ),
Signature · 1 line
2.40.0.windows.1
Jeff Hugo
reviewerQuoted 12 lines · Hongxing Xu
Documentation/ABI/stable/sysfs-bus-mhi +4
-0@@ -30,6 +30,20 @@ Description: Initiates a SoC reset on the MHI controller. A SoC reset is non-responsive, or as a means of loading new firmware as a system administration task. +What: /sys/bus/mhi/devices/.../debug_ports +Date: November 2026 +KernelVersion: 7.5 +Contact: [email protected]
Documentation/ABI/stable/sysfs-bus-mhi Description: Initiates a SoC reset on the MHI controller. A SoC reset is · line 46
Quoted 10 lines
Documentation/ABI/stable/sysfs-bus-mhi +10
-0+Description: Controls which set of ports the MHI controller enumerates. + Writing 0 selects the commercial personality, in which the + channels that are only useful for debugging are not + enumerated. Writing 1 selects the debug personality and + additionally enumerates those channels, so that the extra + WWAN ports appear, e.g. one for the diagnostic interface. + Reading returns the currently active personality. + Controllers that do not describe any debug-only channel are + unaffected by this attribute. +
Why sysfs and not debugfs?
Quoted 5 lines
Documentation/ABI/stable/sysfs-bus-mhi +0
-0/**@@ -381,6 +384,16 @@ struct mhi_controller_config { * @no_m3: Device doesn't support M3 state * @irq_flags: irq flags passed to request_irq (optional) * @mru: the default MRU for the MHI device
Documentation/ABI/stable/sysfs-bus-mhi struct mhi_controller_config { · line 396
Quoted 10 lines
Documentation/ABI/stable/sysfs-bus-mhi +10
-0+ * @debug_ports: Whether the debug port personality is active, i.e. whether the + * channels marked with debug_only are enumerated. Controlled + * from userspace through the debug_ports sysfs attribute and + * applied by debug_ports_work. Zero-initialised, so a + * controller starts in the commercial personality. + * @debug_ports_pending: Latest personality requested from userspace, applied + * by debug_ports_work + * @debug_ports_work: Applies @debug_ports_pending from the ordered high + * priority workqueue, so the device creation it performs + * cannot race with a state transition
Are these optional?
Two questions, both pointing into this patch, so answering them together.
Quoted 3 lines
Quoted 1 line
+What:
/sys/bus/mhi/devices/.../debug_portsWhy sysfs and not debugfs?
Because this is not a debugging aid. It is a documented ABI that end users and provisioning tools drive on a shipped system, and it has to remain available there.
What userspace gains is the ability to bring up, on demand, a group of ports that a given firmware product configuration does not enable by
default: the diagnostic interface, and the same applies to the ADB and AT ports. A single switch that creates or destroys that whole group is what is needed; poking channels one at a time is not. That is why it is described alongside the other controller attributes in Documentation/ABI/stable/sysfs-bus-mhi.
debugfs cannot serve this, for two concrete reasons.
The first is availability. CONFIG_MHI_BUS_DEBUG is a bool with no default, so it is off in the configurations product kernels ship, and it is a compile-time switch: with it disabled, debugfs.c is not built and the calls in mhi_register_controller() resolve to empty stubs. A user who needs these ports would have to rebuild the kernel to get at the control, which is not a usable interface.
The second is capability. MHI's debugfs already exposes states, events, channels and devices, and all of them are read-only listings. There is no debugfs entry in MHI that can create or destroy an MHI device, so this is not a choice between two equivalent interfaces.
There is also a security reason to keep these ports off unless they are wanted. The diagnostic and ADB ports on a WWAN module are a meaningful attack surface, and a module that does not need them should not be exposing them. Hiding that decision behind an interface that is absent on production kernels would defeat the point.
Quoted 7 lines
Quoted 5 lines
+ * @debug_ports: Whether the debug port personality is active, ...
+ * @debug_ports_pending: Latest personality requested from userspace,
+ * applied bydebug_ports_work
+ * @debug_ports_work: Applies @debug_ports_pending from the ordered
+ * high priority workqueue, ...Are these optional?
None of the three can be dropped for this feature to work, though none of them is populated by a driver either.
debug_ports records the personality that is currently applied. It is read by mhi_chan_is_available() on every enumeration, which is what keeps a debug-only channel hidden while the commercial personality is active, and it backs the sysfs attribute so userspace can read back what is in effect. It belongs to the controller because the setting covers the controller as a whole.
debug_ports_pending carries the value userspace has just written. The create and destroy work cannot run in the store function itself:
channel devices are created and destroyed by the state transitions on the ordered hiprio_wq, and pm_mutex does not exclude those transitions, because a transition releases it before calling device_for_each_child(). So the work has to be queued on that same ordered workqueue, and by the time it runs it needs somewhere to read the request from. Keeping it separate from debug_ports also keeps "requested" and "applied" distinct while a switch is in flight.
debug_ports_work is that work item. It is initialised in mhi_register_controller() next to the other work items, queued on the ordered workqueue, and flushed by the store so the write returns only once the ports exist.
All three are zero-initialised, so a controller that declares no debug-only channel starts in the commercial personality and behaves exactly as before. I am happy to say so explicitly in the kernel-doc.
Could I ask what you have in mind here? If the concern is that the docs should mark driver-populated fields as optional, these are not driver-populated and I can word it that way. If you would rather this state lived somewhere other than struct mhi_controller, I would like to know your preference before reworking it.
Thanks