Skip to contentSkip to conversation

Qualcomm Sahara protocol enhancements

PATCH Superseded Kishore Batta 4 participants 36 messages Jul 1 – Oct 11

Subsystems (5)

v6 of 9 Superseded Latest is v9 →

lore
Share & tools
lore
#0 lore raw

v6 cover Qualcomm Sahara protocol enhancements

Hi All,

This series reworks the Sahara protocol driver to make it reusable for multiple MHI based devices and adds support for capturing, restoring and exposing DDR training data using the Sahara command mode.

The Sahara protocol is transported over the MHI bus and is used by multiple flashless devices to transfer firmware images, retrieve memory dumps and exchange command mode data during early boot. However, the current implementation lives under the QAIC accelerator driver and contains device-specific assumptions that limit reuse.

Some MHI devices (for example, QDU100) expose the sahara protocol directly on a "SAHARA" MHI channel and rely on command mode to exchange DDR training data with the host. The existing driver does not bind to such devices and ignores Sahara command mode packets, causing training data to be dropped.

This series addresses these issues by relocating the Sahara driver to the MHI subsystem, centralizing device specific configuration and adding command mode handling for DDR training data.

Overview of the changes in this series -
1. Move Sahara under the MHI subsystem:
a. Relocate the sahara protocol driver from QAIC accelerator tree to
drivers/bus/mhi/host/clients directory.
b. Register Sahara as an independent MHI protocol driver.

2. Generalize device matching and configuration
a. Allow the driver to bind to devices exposing the protocol on a
SAHARA MHI channel.
b. Centralize firmware image table selection at probe time using a variant
table, instead of scattered conditionals.
c. Preserve existing behavior on AIC devices.

3. Add QDU100 firmware image table support
a. Add a QDU100 image table and select it based on the matched MHI channel. b. No separate client driver or registration mechanism is required.

4. Add Sahara command mode support for DDR training.
a. Handle command mode packets(CMD_READY, EXECUTE, EXECUTE_DATA). b. Query supported commands and retrieve DDR training data from the device. c. Allocate receive buffers based on the reported payload size and copy
raw data from the MHI DL.
d. Store training data in controller-scoped memory using devres so it
survives sahara channel teardown.

5. Expose DDR training data to userspace
a. Add a read-only binary sysfs attribute under the MHI controller device. b. The attribute reads directly from controller-scoped storage and remains
available after the Sahara channel device is removed.
c. Cleanup is handled automatically via device-managed resources.

6. Document the sysfs ABI
a. Add ABI documentation describing the DDR training data sysfs node.

---
Changes in v6:
- Used BIN_ATTR_RO() instead of struct bin_attribute.
- Link to v5: https://lore.kernel.org/r/20260416-sahara_protocol_new_v2-v5-0-6aebf005e4ba@oss.qualcomm.com

Changes in v5:
- Sahara protocol documentation is moved to Documentation/mhi/ directory.
- Sahara driver is now moved to drivers/bus/mhi/host/clients/ directory.
- Squashed v4 patch 3 with v4 patch 5.
- Squashed v4 patch 9 with v4 patch 8.
- Firmware image tables are now moved to respective MHI controller files.
- Removed redundant if-else conditions when loading DDR training data image.
- Corrected commit subjects and messages.
- Removed Sahara variant structure and introduced mhi_sahara_fw_table structure.
- Link to v4: https://lore.kernel.org/r/20260319-sahara_protocol_new_v2-v4-0-47ad79308762@oss.qualcomm.com

Changes in v4:
- EDITME: describe what is new in this series revision.
- EDITME: use bulletpoints and terse descriptions.
- Link to v3: https://lore.kernel.org/r/20260310-sahara_protocol_new_v2-v3-0-994ea4b0d5ad@oss.qualcomm.com

Changes in v3:
- Dropped the explicit image table entry for the boot critical image ID and
added a generic image ID based firmware lookup fallback.
- Link to v2: https://lore.kernel.org/r/20260307-sahara_protocol_new_v2-v2-0-29dc748b5e9c@oss.qualcomm.com

Changes in v2:
- Rebased onto latest linux-next tip.
- Reworked commit messages to clearly start with the problem being solved and end with a technical description of the change.
- Moved the Sahara driver to drivers/bus/mhi instead of drivers/soc/qcom, reflecting that its an MHI protocol driver rather than a SoC specific driver.
- Removed client side image table registration and consolidated firmware selection directly in the sahara driver using a probe-time variant
mechanism.
- Ensured each patch is self-contained and does not break the build or runtime behavior at any intermediate point.
- Simplified state handling and lifetime management to avoid duplicated state tracking and ad-hoc cleanup.
- Updated sysfs handling to use controller-scoped devres and avoid one-shot reads or manual teardown.
- Link to v1: https://lore.kernel.org/r/[email protected]

---
Kishore Batta (7):
Add documentation for Sahara protocol
bus: mhi: Move Sahara protocol driver under MHI host client drivers
bus: mhi: Centralize Sahara firmware image table selection at probe time
bus: mhi: Add QDU100 Sahara variant and firmware fallback
bus: mhi: Load DDR training data using device serial number
bus: mhi: Capture DDR training data via command mode
bus: mhi: Expose DDR training data via controller sysfs

 .../ABI/testing/sysfs-bus-mhi-ddr_training_data    |   19 +
 Documentation/mhi/index.rst                        |    1 +
 Documentation/mhi/sahara_protocol.rst              | 1241 ++++++++++++++++++++
 drivers/accel/qaic/Kconfig                         |    1 +
 drivers/accel/qaic/Makefile                        |    3 +-
 drivers/accel/qaic/mhi_controller.c                |   61 +
 drivers/accel/qaic/qaic_drv.c                      |    9 -
 drivers/accel/qaic/sahara.h                        |   10 -
 drivers/bus/mhi/Kconfig                            |    1 +
 drivers/bus/mhi/host/Makefile                      |    1 +
 drivers/bus/mhi/host/clients/Kconfig               |    5 +
 drivers/bus/mhi/host/clients/Makefile              |    1 +
 drivers/bus/mhi/host/clients/sahara/Kconfig        |   15 +
 drivers/bus/mhi/host/clients/sahara/Makefile       |    2 +
 .../qaic => bus/mhi/host/clients/sahara}/sahara.c  |  516 ++++++--
 drivers/bus/mhi/host/pci_generic.c                 |   45 +
 include/linux/mhi.h                                |   17 +
 17 files changed, 1856 insertions(+), 92 deletions(-)

---
base-commit: a0ae2a256046c0c5d3778d1a194ff2e171f16e5f
change-id: 20260307-sahara_protocol_new_v2-662854773cf7

Best regards,

Signature · 1 line
Kishore Batta <[email protected]>
#1 lore raw

PATCH v6 1/7 Add documentation for Sahara protocol

Introduce documentation for the Sahara protocol, describing its operational modes and their respective functions. The image transfer mode enables firmware transfer from host to device. The memory debug mode allows extraction of device memory contents to host. The command mode facilitates retrieval of DDR training data from the device and also to restore the training data back to device in subsequent boot of device to save boot time.

---
 Documentation/mhi/index.rst           |    1 +
 Documentation/mhi/sahara_protocol.rst | 1241 +++++++++++++++++++++++++++++++++
 2 files changed, 1242 insertions(+)
Documentation/mhi/index.rst +1 -0
index 0aa00482aa2e2d7ec4941154a8c6947dc0a0ac40..39a38978398b81727514ec95dee4e060a1063b34 100644
@@ -9,3 +9,4 @@ MHI
 
    mhi
    topology
+   sahara_protocol
Documentation/mhi/sahara_protocol.rst new file +1241 -0

1,241 lines not on this page · raw message

Signature · 1 line
2.34.1
#2 lore raw

Re: [PATCH v6 1/7] Add documentation for Sahara protocol

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Low] Multiple typographical errors, misspelled constant names, and a copy-paste error exist in the newly…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] Multiple typographical errors, misspelled constant names, and a copy-paste error exist in the newly added Sahara protocol documentation. --

Note: The format of this report has been altered to provide a free-form summary of the findings rather than inline code quotes, due to strict content recitation restrictions.

commit ea565b1ec139594103ddde3b826aade3f2071329
Author: Kishore Batta <[email protected]>

Add documentation for Sahara protocol

This commit introduces documentation for the Qualcomm Sahara protocol. It details operational modes such as image transfer, memory debug, and command mode, explaining their functions and respective packet flows.

[Severity: Low]
This isn't a bug, but there is a typo in the mode constant name defined around line 206 in Documentation/mhi/sahara_protocol.rst. It is written as SAHARA_MODE_MEMORY_DBEUG instead of SAHARA_MODE_MEMORY_DEBUG. Could this typo mislead developers who are referencing the protocol definitions in their code?

[Severity: Low]
In the "Packet flow for memory debug" section around line 814, the text introduces the sequence with "The packet flow sequence for image transfer is as follows:". Is this a copy-paste error from the previous section? Should it refer to "memory debug" instead of "image transfer" here?

[Severity: Low]
There are several typographical errors scattered throughout the new documentation file that might be worth correcting for overall clarity and professionalism. These include:

- "sate" instead of "state" (e.g., in "Reset Sahara sate machine packet")
- "Sahar" instead of "Sahara" (e.g., "The Sahar client interprets")
- "pcket" instead of "packet" (e.g., "content from the data pcket is written")
- "targert" instead of "target" (e.g., "transferring the image header to the targert")
- "successgul" instead of "successful" (e.g., "receives a successgul end of image")
- "firgure" instead of "figure" (e.g., "The below firgure shows")

Would it be possible to fix these minor typos in a subsequent revision?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/20260701-sahara_protocol_new_v2-v6-0-3a78362c4741@oss.qualcomm.com?part=1
reviewer
#3 lore raw

Re: [PATCH v6 1/7] Add documentation for Sahara protocol

In reply to Kishore Batta
Quoted 122 lines · Kishore Batta
Documentation/mhi/sahara_protocol.rst new file +122 -0
+The packet flow sequence is as follows :
+
+1. The target sends the hello packet to the host to initiate the protocol
+   with the mode set to image transfer pending.
+
+2. The host sends a hello response packet with a success status and sets the
+   mode to image transfer pending after it receives the hello packet and
+   validates the protocol version running on the target.
+
+3. After the target receives the hello response, it initiates the data
+   transfer by requesting the size of DDR training/calibration data.
+
+4. The host sends back the DDR training/calibration data to the target.
+
+5. The target decodes the training data and does not find valid DDR
+   calibration data, target sends END_IMAGE_TX to interrupt the transfer.
+
+6. The host sends DONE after receives END_IMAGE_TX.
+
+7. The target sends DONE_RESP with mode = IMAGE_TX_PENDING because it has
+   not received all images.
+
+8. The target executes DDR training process to generate valid DDR calibration
+   data and prepares to push back to host.
+
+9. The target initiates protocol by sending a hello packet with COMMAND_MODE
+   to the host.
+
+10. The host sends a hello response packet with a success status and sets the
+    mode to COMMAND_MODE.
+
+11. The target sends CMD_READY to the host.
+
+12. The host receives CMD_READY and starts to get command IDs to be executed.
+
+13. The target sends CMD_ID = 9 to push DDR calibration data to host.
+
+14. The host executes CMD_ID = 9 to get DDR calibration data from the target.
+
+15. The target sends RAW_DATA with the payload which contains DDR calibration
+    data to host.
+
+16. The host saves training data in the kernel buffer and exposes to userspace
+    via the sysfs entry. The host sends CMD_SWITCH_MODE with the mode set to
+    IMAGE_TX_PENDING to continue booting.
+
+17. After the target receives the CMD_SWITCH_MODE command, it sends HELLO to
+    the host with the mode set to IMAGE_TX_PENDING. The target and the host
+    repeat the packet flow for image transfer to get all booting-required
+    images.
+
+18. Upon successful transfer of all images, the target sends an END_IMAGE_TX
+    packet with a success status to the host.
+
+19. The host sends DONE after it receives END_IMAGE_TX.
+
+20. The target sends DONE_RESP with the mode set to IMAGE_TX_COMPLETE because
+    it has received all images. The process has been completed after the host
+    receives DONE_RESP with the mode set to IMAGE_TX_COMPLETE.
+
+Subsequent boot scenario with valid DDR calibration data
+--------------------------------------------------------
+
+The below firgure shows the subsequent boot scenario with valid DDR calibration
+data process being loaded from host to target.
+
+.. code-block:: text
+
+                        Host                       Target
+                          |          HELLO            |
+                          |   (mode = image transfer) |
+                          |<--------------------------|
+                          |                           |
+                          |         HELLO RESP        |
+                          |   (mode = image transfer) |
+                          |-------------------------->|
+                          |                           |
+                          |         READ_DATA         |
+                          |   (img ID:34, 0, offset,  |
+                          | size of DDR training data)|
+                          |<--------------------------|
+                          |                           |
+                          |         RAW_DATA          |
+                          |(size of DDR training data)|
+                          |-------------------------->|
+                          |                           |
+                          |                           |
+                          |       END_IMAGE_TX        |
+                          |<--------------------------|
+                          |                           |
+                          |                           |
+                          |          DONE             |
+                          |-------------------------->|
+                          |                           |
+                          |                           |
+                          |         DONE_RESP         |
+                          | (mode = IMAGE_TX_PENDING) |
+                          |<--------------------------|
+                          |                           |
+                          | Subsequent boot scenario  |
+                          | (valid calibration data)  |
+                          | DDR driver configures DDR |
+                          | using valid calibration   |
+                          | data                      |
+                          |                           |
+                          |                           |
+                          |          HELLO            |
+                          | (mode = IMAGE_TX_PENDING) |
+                          |<--------------------------|
+                          |                           |
+                          |         HELLO RESP        |
+                          | (mode = IMAGE_TX_PENDING) |
+                          |-------------------------->|
+                          |                           |
+                          | Boot/Load rest of the     |
+                          |    images....             |
+                          |                           |
+                          |       END_IMAGE_TX        |
+                          |<--------------------------|
+                          |                           |
+                          |                           |
+                          |          DONE             |

Documentation/mhi/sahara_protocol.rst line 1202

Quoted 10 lines
Documentation/mhi/sahara_protocol.rst new file +10 -0
+                          |-------------------------->|
+                          |                           |
+                          |                           |
+                          |         DONE_RESP         |
+                          |(mode = IMAGE_TX_COMPLETE) |
+                          |<--------------------------|
+                          |                           |
+
+The packet flow is as follows :
+

s/as follows :/as follows:/
in 2 places.

Quoted 2 lines

+1. The target sends the hello packet to the host to initiate the protocol
+ with the mode set to image transfer pending.

Signature · 1 line
~Randy
#4 lore raw

Re: [PATCH v6 1/7] Add documentation for Sahara protocol

Quoted 127 lines · Randy Dunlap
Quoted 126 lines · Kishore Batta
Documentation/mhi/sahara_protocol.rst new file +126 -0
+The packet flow sequence is as follows :
+
+1. The target sends the hello packet to the host to initiate the protocol
+   with the mode set to image transfer pending.
+
+2. The host sends a hello response packet with a success status and sets the
+   mode to image transfer pending after it receives the hello packet and
+   validates the protocol version running on the target.
+
+3. After the target receives the hello response, it initiates the data
+   transfer by requesting the size of DDR training/calibration data.
+
+4. The host sends back the DDR training/calibration data to the target.
+
+5. The target decodes the training data and does not find valid DDR
+   calibration data, target sends END_IMAGE_TX to interrupt the transfer.
+
+6. The host sends DONE after receives END_IMAGE_TX.
+
+7. The target sends DONE_RESP with mode = IMAGE_TX_PENDING because it has
+   not received all images.
+
+8. The target executes DDR training process to generate valid DDR calibration
+   data and prepares to push back to host.
+
+9. The target initiates protocol by sending a hello packet with COMMAND_MODE
+   to the host.
+
+10. The host sends a hello response packet with a success status and sets the
+    mode to COMMAND_MODE.
+
+11. The target sends CMD_READY to the host.
+
+12. The host receives CMD_READY and starts to get command IDs to be executed.
+
+13. The target sends CMD_ID = 9 to push DDR calibration data to host.
+
+14. The host executes CMD_ID = 9 to get DDR calibration data from the target.
+
+15. The target sends RAW_DATA with the payload which contains DDR calibration
+    data to host.
+
+16. The host saves training data in the kernel buffer and exposes to userspace
+    via the sysfs entry. The host sends CMD_SWITCH_MODE with the mode set to
+    IMAGE_TX_PENDING to continue booting.
+
+17. After the target receives the CMD_SWITCH_MODE command, it sends HELLO to
+    the host with the mode set to IMAGE_TX_PENDING. The target and the host
+    repeat the packet flow for image transfer to get all booting-required
+    images.
+
+18. Upon successful transfer of all images, the target sends an END_IMAGE_TX
+    packet with a success status to the host.
+
+19. The host sends DONE after it receives END_IMAGE_TX.
+
+20. The target sends DONE_RESP with the mode set to IMAGE_TX_COMPLETE because
+    it has received all images. The process has been completed after the host
+    receives DONE_RESP with the mode set to IMAGE_TX_COMPLETE.
+
+Subsequent boot scenario with valid DDR calibration data
+--------------------------------------------------------
+
+The below firgure shows the subsequent boot scenario with valid DDR calibration
+data process being loaded from host to target.
+
+.. code-block:: text
+
+                        Host                       Target
+                          |          HELLO            |
+                          |   (mode = image transfer) |
+                          |<--------------------------|
+                          |                           |
+                          |         HELLO RESP        |
+                          |   (mode = image transfer) |
+                          |-------------------------->|
+                          |                           |
+                          |         READ_DATA         |
+                          |   (img ID:34, 0, offset,  |
+                          | size of DDR training data)|
+                          |<--------------------------|
+                          |                           |
+                          |         RAW_DATA          |
+                          |(size of DDR training data)|
+                          |-------------------------->|
+                          |                           |
+                          |                           |
+                          |       END_IMAGE_TX        |
+                          |<--------------------------|
+                          |                           |
+                          |                           |
+                          |          DONE             |
+                          |-------------------------->|
+                          |                           |
+                          |                           |
+                          |         DONE_RESP         |
+                          | (mode = IMAGE_TX_PENDING) |
+                          |<--------------------------|
+                          |                           |
+                          | Subsequent boot scenario  |
+                          | (valid calibration data)  |
+                          | DDR driver configures DDR |
+                          | using valid calibration   |
+                          | data                      |
+                          |                           |
+                          |                           |
+                          |          HELLO            |
+                          | (mode = IMAGE_TX_PENDING) |
+                          |<--------------------------|
+                          |                           |
+                          |         HELLO RESP        |
+                          | (mode = IMAGE_TX_PENDING) |
+                          |-------------------------->|
+                          |                           |
+                          | Boot/Load rest of the     |
+                          |    images....             |
+                          |                           |
+                          |       END_IMAGE_TX        |
+                          |<--------------------------|
+                          |                           |
+                          |                           |
+                          |          DONE             |
+                          |-------------------------->|
+                          |                           |
+                          |                           |
+                          |         DONE_RESP         |
Quoted 8 lines

Documentation/mhi/sahara_protocol.rst line 1202

Quoted 6 lines
Documentation/mhi/sahara_protocol.rst new file +6 -0
+                          |(mode = IMAGE_TX_COMPLETE) |
+                          |<--------------------------|
+                          |                           |
+
+The packet flow is as follows :
+

s/as follows :/as follows:/
in 2 places.

ACK. I'll remove the extra space before the colon in both places in the next version.

Quoted 3 lines
Quoted 3 lines

+1. The target sends the hello packet to the host to initiate the protocol
+ with the mode set to image transfer pending.

#5 lore raw

Re: [PATCH v6 1/7] Add documentation for Sahara protocol

In reply to Randy Dunlap
Quoted 127 lines · Randy Dunlap
Quoted 126 lines · Kishore Batta
Documentation/mhi/sahara_protocol.rst new file +126 -0
+The packet flow sequence is as follows :
+
+1. The target sends the hello packet to the host to initiate the protocol
+   with the mode set to image transfer pending.
+
+2. The host sends a hello response packet with a success status and sets the
+   mode to image transfer pending after it receives the hello packet and
+   validates the protocol version running on the target.
+
+3. After the target receives the hello response, it initiates the data
+   transfer by requesting the size of DDR training/calibration data.
+
+4. The host sends back the DDR training/calibration data to the target.
+
+5. The target decodes the training data and does not find valid DDR
+   calibration data, target sends END_IMAGE_TX to interrupt the transfer.
+
+6. The host sends DONE after receives END_IMAGE_TX.
+
+7. The target sends DONE_RESP with mode = IMAGE_TX_PENDING because it has
+   not received all images.
+
+8. The target executes DDR training process to generate valid DDR calibration
+   data and prepares to push back to host.
+
+9. The target initiates protocol by sending a hello packet with COMMAND_MODE
+   to the host.
+
+10. The host sends a hello response packet with a success status and sets the
+    mode to COMMAND_MODE.
+
+11. The target sends CMD_READY to the host.
+
+12. The host receives CMD_READY and starts to get command IDs to be executed.
+
+13. The target sends CMD_ID = 9 to push DDR calibration data to host.
+
+14. The host executes CMD_ID = 9 to get DDR calibration data from the target.
+
+15. The target sends RAW_DATA with the payload which contains DDR calibration
+    data to host.
+
+16. The host saves training data in the kernel buffer and exposes to userspace
+    via the sysfs entry. The host sends CMD_SWITCH_MODE with the mode set to
+    IMAGE_TX_PENDING to continue booting.
+
+17. After the target receives the CMD_SWITCH_MODE command, it sends HELLO to
+    the host with the mode set to IMAGE_TX_PENDING. The target and the host
+    repeat the packet flow for image transfer to get all booting-required
+    images.
+
+18. Upon successful transfer of all images, the target sends an END_IMAGE_TX
+    packet with a success status to the host.
+
+19. The host sends DONE after it receives END_IMAGE_TX.
+
+20. The target sends DONE_RESP with the mode set to IMAGE_TX_COMPLETE because
+    it has received all images. The process has been completed after the host
+    receives DONE_RESP with the mode set to IMAGE_TX_COMPLETE.
+
+Subsequent boot scenario with valid DDR calibration data
+--------------------------------------------------------
+
+The below firgure shows the subsequent boot scenario with valid DDR calibration
+data process being loaded from host to target.
+
+.. code-block:: text
+
+                        Host                       Target
+                          |          HELLO            |
+                          |   (mode = image transfer) |
+                          |<--------------------------|
+                          |                           |
+                          |         HELLO RESP        |
+                          |   (mode = image transfer) |
+                          |-------------------------->|
+                          |                           |
+                          |         READ_DATA         |
+                          |   (img ID:34, 0, offset,  |
+                          | size of DDR training data)|
+                          |<--------------------------|
+                          |                           |
+                          |         RAW_DATA          |
+                          |(size of DDR training data)|
+                          |-------------------------->|
+                          |                           |
+                          |                           |
+                          |       END_IMAGE_TX        |
+                          |<--------------------------|
+                          |                           |
+                          |                           |
+                          |          DONE             |
+                          |-------------------------->|
+                          |                           |
+                          |                           |
+                          |         DONE_RESP         |
+                          | (mode = IMAGE_TX_PENDING) |
+                          |<--------------------------|
+                          |                           |
+                          | Subsequent boot scenario  |
+                          | (valid calibration data)  |
+                          | DDR driver configures DDR |
+                          | using valid calibration   |
+                          | data                      |
+                          |                           |
+                          |                           |
+                          |          HELLO            |
+                          | (mode = IMAGE_TX_PENDING) |
+                          |<--------------------------|
+                          |                           |
+                          |         HELLO RESP        |
+                          | (mode = IMAGE_TX_PENDING) |
+                          |-------------------------->|
+                          |                           |
+                          | Boot/Load rest of the     |
+                          |    images....             |
+                          |                           |
+                          |       END_IMAGE_TX        |
+                          |<--------------------------|
+                          |                           |
+                          |                           |
+                          |          DONE             |
+                          |-------------------------->|
+                          |                           |
+                          |                           |
+                          |         DONE_RESP         |
Quoted 8 lines

Documentation/mhi/sahara_protocol.rst line 1202

Quoted 6 lines
Documentation/mhi/sahara_protocol.rst new file +6 -0
+                          |(mode = IMAGE_TX_COMPLETE) |
+                          |<--------------------------|
+                          |                           |
+
+The packet flow is as follows :
+

s/as follows :/as follows:/
in 2 places.

ACK. will correct in next version.

Quoted 2 lines
Quoted 2 lines

+1. The target sends the hello packet to the host to initiate the protocol
+ with the mode set to image transfer pending.

#6 lore raw

PATCH v6 2/7 bus: mhi: Move Sahara protocol driver under MHI host client drivers

In reply to Kishore Batta

The Sahara protocol driver currently lives under the QAIC accelerator subsystem even though the protocol is transported over MHI and is used by multiple Qualcomm flashless devices. This makes Sahara appear QAIC specific and complicates reuse by other MHI based devices.

Move the Sahara protocol driver under drivers/bus/mhi as a host client driver and build it as an independent MHI protocol driver. This keeps the QAIC driver focused on the accelerator device while allowing other MHI users to enable Sahara without depending on QAIC.

As part of the move, add a dedicated Kconfig/Makefile hierarchy under the MHI host client drivers and convert the driver to use module_mhi_driver() instead of register/unregister hooks.

---
 drivers/accel/qaic/Kconfig                           |  1 +
 drivers/accel/qaic/Makefile                          |  3 +--
 drivers/accel/qaic/qaic_drv.c                        |  9 ---------
 drivers/accel/qaic/sahara.h                          | 10 ----------
 drivers/bus/mhi/Kconfig                              |  1 +
 drivers/bus/mhi/host/Makefile                        |  1 +
 drivers/bus/mhi/host/clients/Kconfig                 |  5 +++++
 drivers/bus/mhi/host/clients/Makefile                |  1 +
 drivers/bus/mhi/host/clients/sahara/Kconfig          | 15 +++++++++++++++
 drivers/bus/mhi/host/clients/sahara/Makefile         |  2 ++
 .../qaic => bus/mhi/host/clients/sahara}/sahara.c    | 20 +++++++-------------
 11 files changed, 34 insertions(+), 34 deletions(-)
drivers/accel/qaic/Kconfig +1 -0
index 116e42d152ca885b8c59e33c7a87519a0abc6bb3..af90fdfcf77eeb6dd5ad309b33d793d4fdc91b1e 100644
@@ -8,6 +8,7 @@ config DRM_ACCEL_QAIC
 	depends on DRM_ACCEL
 	depends on PCI && HAS_IOMEM
 	depends on MHI_BUS
+	depends on MHI_SAHARA
 	select CRC32
 	select WANT_DEV_COREDUMP
 	help
drivers/accel/qaic/Makefile +1 -2
index 71f727b74da3bb4478324689f02a7cea24a05c2d..e7b8458800072aa627f7f36c3257883aa56f4ce4 100644
@@ -13,7 +13,6 @@ qaic-y := \
 	qaic_ras.o \
 	qaic_ssr.o \
 	qaic_sysfs.o \
-	qaic_timesync.o \
-	sahara.o
+	qaic_timesync.o
 
 qaic-$(CONFIG_DEBUG_FS) += qaic_debugfs.o
drivers/accel/qaic/qaic_drv.c +0 -9
index 63fb8c7b4abcbe4f1b76c32106f4e8b9ea5e2c8e..3907b13e426064f4fa069e803cc44462feea4063 100644
@@ -32,7 +32,6 @@
 #include "qaic_ras.h"
 #include "qaic_ssr.h"
 #include "qaic_timesync.h"
-#include "sahara.h"
 
 MODULE_IMPORT_NS("DMA_BUF");
 
@@ -791,12 +790,6 @@ static int __init qaic_init(void)
 		goto free_pci;
 	}
 
-	ret = sahara_register();
-	if (ret) {
-		pr_debug("qaic: sahara_register failed %d\n", ret);
-		goto free_mhi;
-	}
-
 	ret = qaic_timesync_init();
 	if (ret)
 		pr_debug("qaic: qaic_timesync_init failed %d\n", ret);
@@ -818,7 +811,6 @@ static int __init qaic_init(void)
 
 free_bootlog:
 	qaic_bootlog_unregister();
-free_mhi:
 	mhi_driver_unregister(&qaic_mhi_driver);
 free_pci:
 	pci_unregister_driver(&qaic_pci_driver);
@@ -847,7 +839,6 @@ static void __exit qaic_exit(void)
 	qaic_ras_unregister();
 	qaic_bootlog_unregister();
 	qaic_timesync_deinit();
-	sahara_unregister();
 	mhi_driver_unregister(&qaic_mhi_driver);
 	pci_unregister_driver(&qaic_pci_driver);
 }
drivers/accel/qaic/sahara.h deleted +0 -10
deleted file mode 100644
index 640208acc0d13d423bd9220e6861b7c141af74ff..0000000000000000000000000000000000000000
@@ -1,10 +0,0 @@
-/* SPDX-License-Identifier: GPL-2.0-only */
-
-/* Copyright (c) 2024 Qualcomm Innovation Center, Inc. All rights reserved. */
-
-#ifndef __SAHARA_H__
-#define __SAHARA_H__
-
-int sahara_register(void);
-void sahara_unregister(void);
-#endif /* __SAHARA_H__ */
drivers/bus/mhi/Kconfig +1 -0
index b39a11e6c624ba00349cca22d74bd876020590ab..720115218c2401c99b29f79bbd4113cd877503ac 100644
@@ -7,3 +7,4 @@
 
 source "drivers/bus/mhi/host/Kconfig"
 source "drivers/bus/mhi/ep/Kconfig"
+source "drivers/bus/mhi/host/clients/Kconfig"
drivers/bus/mhi/host/Makefile +1 -0
index 859c2f38451c669b3d3014c374b2b957c99a1cfe..2e8949f1a2fe6f3f3b2e1dc541f97d2c393d6a0f 100644
@@ -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-$(CONFIG_MHI_BUS) += clients/
drivers/bus/mhi/host/clients/Kconfig new file +5 -0
new file mode 100644
index 0000000000000000000000000000000000000000..a4f2a3c1d20c887cc474646ea91532d775a13f57
@@ -0,0 +1,5 @@
+menu "MHI host client drivers"
+
+source "drivers/bus/mhi/host/clients/sahara/Kconfig"
+
+endmenu
drivers/bus/mhi/host/clients/Makefile new file +1 -0
new file mode 100644
index 0000000000000000000000000000000000000000..62e2fa161565225be7f6a23d3cdf4f2f169cb7ce
@@ -0,0 +1 @@
+obj-$(CONFIG_MHI_SAHARA) += sahara/
drivers/bus/mhi/host/clients/sahara/Kconfig new file +15 -0
new file mode 100644
index 0000000000000000000000000000000000000000..f1fc7013a2dee0be645c50f32305659e591de7e7
@@ -0,0 +1,15 @@
+config MHI_SAHARA
+	tristate "Sahara protocol driver"
+	depends on MHI_BUS
+	help
+	  Enable support for the Sahara protocol transported over the MHI bus.
+
+	  The Sahara protocol is used to transfer firmware images, retrieve
+	  memory dumps and exchange command mode DDR calibration data between
+	  host and device. This driver is not tied to a specific SoC and may be
+	  used by multiple MHI based devices.
+
+	  If unsure, say N.
+
+	  To compile this driver as a module, choose M here: the module will be
+	  called mhi_sahara.
drivers/bus/mhi/host/clients/sahara/Makefile new file +2 -0
new file mode 100644
index 0000000000000000000000000000000000000000..fc02a25935011cbd7138ea8f24b88cf5b032a4ce
@@ -0,0 +1,2 @@
+obj-$(CONFIG_MHI_SAHARA) += mhi_sahara.o
+mhi_sahara-y := sahara.o
drivers/bus/mhi/host/clients/sahara/sahara.c renamed from drivers/accel/qaic/sahara.c +7 -13
similarity index 99%
rename from drivers/accel/qaic/sahara.c
rename to drivers/bus/mhi/host/clients/sahara/sahara.c
index fd3c3b2d1fd3bb698809e6ca669128e2dce06613..858dc5bc39c1ad42922cabef3b1abcd43bc4f0f4 100644
@@ -1,6 +1,8 @@
 // SPDX-License-Identifier: GPL-2.0-only
-
-/* Copyright (c) 2024 Qualcomm Innovation Center, Inc. All rights reserved. */
+/*
+ * Copyright (c) 2024 Qualcomm Innovation Center, Inc. All rights reserved.
+ *
+ */
 
 #include <linux/devcoredump.h>
 #include <linux/firmware.h>
@@ -13,8 +15,6 @@
 #include <linux/vmalloc.h>
 #include <linux/workqueue.h>
 
-#include "sahara.h"
-
 #define SAHARA_HELLO_CMD		0x1  /* Min protocol version 1.0 */
 #define SAHARA_HELLO_RESP_CMD		0x2  /* Min protocol version 1.0 */
 #define SAHARA_READ_DATA_CMD		0x3  /* Min protocol version 1.0 */
@@ -923,13 +923,7 @@ static struct mhi_driver sahara_mhi_driver = {
 		.name = "sahara",
 	},
 };
+module_mhi_driver(sahara_mhi_driver);
 
-int sahara_register(void)
-{
-	return mhi_driver_register(&sahara_mhi_driver);
-}
-
-void sahara_unregister(void)
-{
-	mhi_driver_unregister(&sahara_mhi_driver);
-}
+MODULE_LICENSE("GPL");
+MODULE_DESCRIPTION("Qualcomm Sahara MHI protocol driver");
Signature · 1 line
2.34.1
#7 lore raw

Re: [PATCH v6 2/7] bus: mhi: Move Sahara protocol driver under MHI host client drivers

Thank you for your contribution! Sashiko AI review found 5 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 5 potential issue(s) to consider:

New issues:
- [Medium] Extracting the Sahara protocol to MHI_SAHARA and making DRM_ACCEL_QAIC depend on it breaks existing kernel configurations.
- [Medium] The standalone MHI_SAHARA Kconfig entry fails to select WANT_DEV_COREDUMP, causing memory dumps to be silently discarded if QAIC is not enabled.

Pre-existing issues:
- [High] The qaic_init() error handling path fails to unregister the timesync and RAS MHI drivers if qaic_ssr_register() fails, causing a use-after-free panic.
- [High] The driver cleanup routine sahara_mhi_remove() fails to cancel read_data_work, which can lead to a use-after-free.
- [High] Teardown symmetry violation in sahara_mhi_remove() allows cancelled works to be rescheduled by incoming MHI traffic, leading to a use-after-free. --

commit 45d20bba8e8207c4574856cd753b585b5512dea6
Author: Kishore Batta <[email protected]>

bus: mhi: Move Sahara protocol driver under MHI host client drivers

This commit moves the Sahara protocol driver from the QAIC accelerator subsystem to the MHI bus host client drivers. It creates a dedicated Kconfig/Makefile hierarchy for Sahara and converts it to use module_mhi_driver().

drivers/accel/qaic/Kconfig config DRM_ACCEL_QAIC · line 11

Quoted 8 lines
drivers/accel/qaic/Kconfig +1 -0
@@ -8,6 +8,7 @@ config DRM_ACCEL_QAIC
 	depends on DRM_ACCEL
 	depends on PCI && HAS_IOMEM
 	depends on MHI_BUS
+	depends on MHI_SAHARA

[Severity: Medium]
Will this dependency silently disable the QAIC driver for existing users?

When users upgrade their kernels and run make olddefconfig, the new MHI_SAHARA option will default to 'N'. Because DRM_ACCEL_QAIC now uses "depends on MHI_SAHARA", it will be silently disabled and hidden from menuconfig.

Should this use "select MHI_SAHARA" instead to preserve backward compatibility?

Quoted 3 lines
drivers/accel/qaic/Kconfig +0 -0
 	select CRC32
 	select WANT_DEV_COREDUMP
 	help

[ ... ]

drivers/accel/qaic/qaic_drv.c static int __init qaic_init(void) · line 816

Quoted 11 lines
drivers/accel/qaic/qaic_drv.c +0 -1
@@ -818,7 +811,6 @@ static int __init qaic_init(void)
 
 free_bootlog:
 	qaic_bootlog_unregister();
-free_mhi:
 	mhi_driver_unregister(&qaic_mhi_driver);
 free_pci:
 	pci_unregister_driver(&qaic_pci_driver);

[Severity: High]
This is a pre-existing issue, but does the qaic_init() error handling path leak resources and cause a use-after-free?

If qaic_ssr_register() fails, the code jumps to free_bootlog, but it misses unregistering the timesync and RAS MHI drivers:

drivers/accel/qaic/qaic_drv.c:qaic_init() {
    ...
    ret = qaic_timesync_init();
    ...
    ret = qaic_ras_register();
    ...
    ret = qaic_ssr_register();
    if (ret) {
        pr_debug("qaic: qaic_ssr_register failed %d\n", ret);
        goto free_bootlog;
    }
    ...
free_bootlog:
    qaic_bootlog_unregister();
    mhi_driver_unregister(&qaic_mhi_driver);
free_pci:
    ...
}

Since qaic_timesync_driver and qaic_ras_mhi_driver remain registered with the MHI bus, will any subsequent MHI bus interaction dereference these freed pointers and cause a kernel panic?

[ ... ]

drivers/bus/mhi/host/clients/sahara/Kconfig line 3

Quoted 8 lines
drivers/bus/mhi/host/clients/sahara/Kconfig new file +3 -0
new file mode 100644
@@ -0,0 +1,15 @@
+config MHI_SAHARA
+	tristate "Sahara protocol driver"
+	depends on MHI_BUS

[Severity: Medium]
Does this standalone Kconfig entry need to select WANT_DEV_COREDUMP?

The MHI_SAHARA driver uses dev_coredumpv() to generate memory dumps. If it is built without QAIC or another driver that explicitly selects WANT_DEV_COREDUMP, the kernel will use the fallback dummy stub for dev_coredumpv(), silently freeing buffers and dropping the dumps.

Should MHI_SAHARA explicitly select WANT_DEV_COREDUMP here?

Quoted 2 lines
drivers/bus/mhi/host/clients/sahara/Kconfig new file +2 -0
+	help
+	  Enable support for the Sahara protocol transported over the MHI bus.

[ ... ]

drivers/bus/mhi/host/clients/sahara/sahara.c static struct mhi_driver sahara_mhi_driver = { · line 926

Quoted 11 lines
drivers/bus/mhi/host/clients/sahara/sahara.c renamed from drivers/accel/qaic/sahara.c +1 -0
similarity index 99%
rename from drivers/accel/qaic/sahara.c
rename to drivers/bus/mhi/host/clients/sahara/sahara.c
@@ -923,13 +923,7 @@ static struct mhi_driver sahara_mhi_driver = {
 		.name = "sahara",
 	},
 };
+module_mhi_driver(sahara_mhi_driver);

[Severity: High]
This isn't a bug introduced by this patch, but does the driver cleanup routine fail to cancel read_data_work?

The sahara_context struct is automatically freed after sahara_mhi_remove() returns. The function cancels fw_work, dump_work, and cmd_work, but it misses read_data_work:

drivers/bus/mhi/host/clients/sahara/sahara.c:sahara_mhi_remove() {
    ...
    cancel_work_sync(&context->fw_work);
    cancel_work_sync(&context->dump_work);
    cancel_work_sync(&context->cmd_work);
    vfree(context->mem_dump);
    sahara_release_image(context);
    ...
}

If read_data_work is pending or executing when the device is removed, will it access the freed context struct and cause a use-after-free?

[Severity: High]
This is a pre-existing issue, but does the order of operations in sahara_mhi_remove() allow cancelled works to be rescheduled?

The function calls cancel_work_sync() before calling mhi_unprepare_from_transfer():

drivers/bus/mhi/host/clients/sahara/sahara.c:sahara_mhi_remove() {
    ...
    cancel_work_sync(&context->fw_work);
    cancel_work_sync(&context->dump_work);
    cancel_work_sync(&context->cmd_work);
    ...
    mhi_unprepare_from_transfer(mhi_dev);
}

Because the MHI channels remain active during the cancel_work_sync() calls, can an incoming MHI packet immediately reschedule a work that was just cancelled?

Should mhi_unprepare_from_transfer() be called first to silence callbacks and prevent a use-after-free?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/20260701-sahara_protocol_new_v2-v6-0-3a78362c4741@oss.qualcomm.com?part=2
#8 lore raw

PATCH v6 3/7 bus: mhi: Centralize Sahara firmware image table selection at probe time

In reply to Kishore Batta

The Sahara driver currently selects firmware image tables using scattered, device specific conditionals in the probe path. This makes the logic harder to follow, harder to extend for new devices, and spreads device knowledge across multiple code paths.

Refactor firmware image table selection into a single, explicit probe time mechanism by introducing a controller provided firmware mapping table that captures device matching, Sahara image tables, firmware folder names, and streaming behaviour in one place.

This centralizes device specific decisions in the controller driver, simplifies the Sahara probe logic, and removes ad-hoc conditionals while preserving existing behavior for all supported AIC devices. This is in preparation for adding QDU100 support.

---
 drivers/accel/qaic/mhi_controller.c          | 61 ++++++++++++++++++++++++++++
 drivers/bus/mhi/host/clients/sahara/sahara.c | 60 +++++----------------------
 include/linux/mhi.h                          | 17 ++++++++
 3 files changed, 88 insertions(+), 50 deletions(-)
drivers/accel/qaic/mhi_controller.c +61 -0
index 4d787f77ce419fcd2b250f9cabaec9c26f2da8dc..1f9ef871421b976c35cfad59aed715da96c1813b 100644
@@ -20,6 +20,62 @@ static unsigned int mhi_timeout_ms = 2000; /* 2 sec default */
 module_param(mhi_timeout_ms, uint, 0600);
 MODULE_PARM_DESC(mhi_timeout_ms, "MHI controller timeout value");
 
+static const char * const aic100_image_table[] = {
+	[1]  = "qcom/aic100/fw1.bin",
+	[2]  = "qcom/aic100/fw2.bin",
+	[4]  = "qcom/aic100/fw4.bin",
+	[5]  = "qcom/aic100/fw5.bin",
+	[6]  = "qcom/aic100/fw6.bin",
+	[8]  = "qcom/aic100/fw8.bin",
+	[9]  = "qcom/aic100/fw9.bin",
+	[10] = "qcom/aic100/fw10.bin",
+};
+
+static const char * const aic200_image_table[] = {
+	[5]  = "qcom/aic200/uefi.elf",
+	[12] = "qcom/aic200/aic200-nsp.bin",
+	[23] = "qcom/aic200/aop.mbn",
+	[32] = "qcom/aic200/tz.mbn",
+	[33] = "qcom/aic200/hypvm.mbn",
+	[38] = "qcom/aic200/xbl_config.elf",
+	[39] = "qcom/aic200/aic200_abl.elf",
+	[40] = "qcom/aic200/apdp.mbn",
+	[41] = "qcom/aic200/devcfg.mbn",
+	[42] = "qcom/aic200/sec.elf",
+	[43] = "qcom/aic200/aic200-hlos.elf",
+	[49] = "qcom/aic200/shrm.elf",
+	[50] = "qcom/aic200/cpucp.elf",
+	[51] = "qcom/aic200/aop_devcfg.mbn",
+	[54] = "qcom/aic200/qupv3fw.elf",
+	[57] = "qcom/aic200/cpucp_dtbs.elf",
+	[62] = "qcom/aic200/uefi_dtbs.elf",
+	[63] = "qcom/aic200/xbl_ac_config.mbn",
+	[64] = "qcom/aic200/tz_ac_config.mbn",
+	[65] = "qcom/aic200/hyp_ac_config.mbn",
+	[66] = "qcom/aic200/pdp.elf",
+	[67] = "qcom/aic200/pdp_cdb.elf",
+	[68] = "qcom/aic200/sdi.mbn",
+	[69] = "qcom/aic200/dcd.mbn",
+	[73] = "qcom/aic200/gearvm.mbn",
+	[74] = "qcom/aic200/sti.bin",
+	[76] = "qcom/aic200/tz_qti_config.mbn",
+	[78] = "qcom/aic200/pvs.bin",
+};
+
+static const struct mhi_sahara_fw_table aic100_sahara_fw = {
+	.image_table = aic100_image_table,
+	.table_size = ARRAY_SIZE(aic100_image_table),
+	.fw_folder = "aic100",
+	.non_streaming = true,
+};
+
+static const struct mhi_sahara_fw_table aic200_sahara_fw = {
+	.image_table = aic200_image_table,
+	.table_size = ARRAY_SIZE(aic200_image_table),
+	.fw_folder = "aic200",
+	.non_streaming = false,
+};
+
 static const char *fw_image_paths[FAMILY_MAX] = {
 	[FAMILY_AIC100] = "qcom/aic100/sbl.bin",
 	[FAMILY_AIC200] = "qcom/aic200/sbl.bin",
@@ -871,6 +927,11 @@ struct mhi_controller *qaic_mhi_register_controller(struct pci_dev *pci_dev, voi
 		mhi_cntrl->name = "AIC100";
 	}
 
+	if (mhi_cntrl->name && !strcmp(mhi_cntrl->name, "AIC100"))
+		mhi_cntrl->sahara_fw = &aic100_sahara_fw;
+	else if (mhi_cntrl->name && !strcmp(mhi_cntrl->name, "AIC200"))
+		mhi_cntrl->sahara_fw = &aic200_sahara_fw;
+
 	/* use latest configured timeout */
 	mhi_config.timeout_ms = mhi_timeout_ms;
 	ret = mhi_register_controller(mhi_cntrl, &mhi_config);
drivers/bus/mhi/host/clients/sahara/sahara.c +10 -50

78 lines not on this page · raw message

include/linux/mhi.h +17 -0
index 88ccb3e14f481d6b85c2a314eb74ba960c2d4c81..060dafffac67c5c920adc1562a61a7233e8d583f 100644
@@ -234,6 +234,21 @@ struct mhi_channel_config {
 	bool wake_capable;
 };
 
+/**
+ * struct mhi_sahara_fw_table - Controller provided sahara firmware mapping
+ * @image_table: Sparse array indexed by Sahara image ID
+ * @table_size: Size of @image_table
+ * @fw_folder: Firmware folder name.
+ * @non_streaming: Streaming feature support (optional)
+ *
+ */
+struct mhi_sahara_fw_table {
+	const char *const *image_table;
+	u32 table_size;
+	const char *fw_folder;
+	bool non_streaming;
+};
+
 /**
  * struct mhi_event_config - Event ring configuration structure for controller
  * @num_elements: The number of elements that can be queued to this ring
@@ -360,6 +375,7 @@ struct mhi_controller_config {
  * @wake_set: Device wakeup set flag
  * @irq_flags: irq flags passed to request_irq (optional)
  * @mru: the default MRU for the MHI device
+ * @sahara_fw: Sahara firmware mapping
  *
  * Fields marked as (required) need to be populated by the controller driver
  * before calling mhi_register_controller(). For the fields marked as (optional)
@@ -445,6 +461,7 @@ struct mhi_controller {
 	bool wake_set;
 	unsigned long irq_flags;
 	u32 mru;
+	const struct mhi_sahara_fw_table *sahara_fw;
 };
 
 /**
Signature · 1 line
2.34.1
#9 lore raw

Re: [PATCH v6 3/7] bus: mhi: Centralize Sahara firmware image table selection at probe time

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:

Pre-existing issues:
- [High] Missing cancellation of read_data_work during device removal leads to a Use-After-Free.
- [High] Lack of synchronization between concurrent device command processing and data streaming leads to Use-After-Free and TX buffer corruption. --

commit 812426ed0db5b6e824926f5d8eecb24c749a578e
Author: Kishore Batta <[email protected]>

bus: mhi: Centralize Sahara firmware image table selection at probe time

This commit refactors the Sahara driver to select firmware image tables using a controller provided mapping table at probe time. It centralizes device specific conditionals that were previously scattered across the driver. This prepares the driver for extending support to new devices.

[ ... ]

Quoted 8 lines
drivers/bus/mhi/host/clients/sahara/sahara.c +0 -0
index 858dc5bc39c1a..e339c67e236af 100644
@@ -179,48 +179,7 @@ struct sahara_context {
 	u32				read_data_length;
 	bool				is_mem_dump_mode;
 	bool				non_streaming;

[ ... ]

drivers/bus/mhi/host/clients/sahara/sahara.c struct sahara_context { · line 183

Quoted 2 lines
drivers/bus/mhi/host/clients/sahara/sahara.c +1 -0
+	const char			*fw_folder;
 };

[Severity: High]
This is a pre-existing issue, but can the device removal path cause a use-after-free here?

In drivers/bus/mhi/host/clients/sahara/sahara.c:sahara_mhi_remove(), the teardown cancels other work items:

    cancel_work_sync(&context->fw_work);
    cancel_work_sync(&context->dump_work);
    cancel_work_sync(&context->cmd_work);

However, it seems cancel_work_sync(&context->read_data_work) is omitted. If a device in streaming mode is actively transferring firmware data via read_data_work, and the driver is unbound, devres will free the device-managed context structure.

Will the pending or sleeping sahara_read_data_processing() work item then continue to run and access the freed context and its firmware buffer?

[Severity: High]
This is a pre-existing issue, but is there a lack of synchronization between concurrent device command processing and data streaming that could corrupt the TX buffer?

If read_data_work is running to stream a firmware image, and a faulty or compromised device sends an out-of-band message (like END_OF_IMAGE_CMD) on the downlink channel, fw_work will run concurrently with read_data_work.

fw_work calls sahara_end_of_image(), which frees context->firmware and writes a response to context->tx[0]:

    sahara_release_image(context);
    ...
    context->tx[0]->cmd = cpu_to_le32(SAHARA_DONE_CMD);

Concurrently, read_data_helper() executed from read_data_work accesses the freed firmware and writes to the same TX buffer:

    memcpy(context->tx[buf_index],
           &context->firmware->data[context->read_data_offset],
           pkt_data_len);

Does this sequence result in a use-after-free and packet corruption before queuing it to MHI?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/20260701-sahara_protocol_new_v2-v6-0-3a78362c4741@oss.qualcomm.com?part=3
#10 lore raw

PATCH v6 4/7 bus: mhi: Add QDU100 Sahara variant and firmware fallback

In reply to Kishore Batta

The Sahara driver currently selects a firmware image table based on the attached device, but it does not recognize QDU100 devices that expose the protocol on the SAHARA MHI channel. As a result, the host cannot associate QDU100 devices with the correct firmware namespace during image transfer.

Extend the probe time variant selection to match the SAHARA MHI channel and associate it with the QDU100 firmware folder. Add a firmware lookup fallback for cases where an image does not have an explicit entry in the device's firmware table. This allows required images to be provisioned by the platform.

This change only affects devices matched on the SAHARA MHI channel and does not change behavior for existing AIC100 and AIC200 devices.

---
 drivers/bus/mhi/host/clients/sahara/sahara.c | 27 +++++++++++++++--
 drivers/bus/mhi/host/pci_generic.c           | 45 ++++++++++++++++++++++++++++
 2 files changed, 70 insertions(+), 2 deletions(-)
drivers/bus/mhi/host/clients/sahara/sahara.c +25 -2
index e339c67e236af271645ca81cc517efd9eead87e4..9adbd84859073d8024ba2a5fcfa33897439d6759 100644
@@ -189,6 +189,7 @@ static bool is_streaming(struct sahara_context *context)
 
 static int sahara_find_image(struct sahara_context *context, u32 image_id)
 {
+	char *fw_path;
 	int ret;
 
 	if (image_id == context->active_image_id)
@@ -201,8 +202,28 @@ static int sahara_find_image(struct sahara_context *context, u32 image_id)
 	}
 
 	if (image_id >= context->table_size || !context->image_table[image_id]) {
-		dev_err(&context->mhi_dev->dev, "request for unknown image: %d\n", image_id);
-		return -EINVAL;
+		if (!context->fw_folder) {
+			dev_err(&context->mhi_dev->dev,
+				"Request for unknown image: %u (no fw folder)\n", image_id);
+			return -EINVAL;
+		}
+
+		fw_path = kasprintf(GFP_KERNEL, "qcom/%s/%u",
+				    context->fw_folder, image_id);
+		if (!fw_path)
+			return -ENOMEM;
+
+		ret = firmware_request_nowarn(&context->firmware,
+					      fw_path,
+					      &context->mhi_dev->dev);
+		kfree(fw_path);
+		if (ret) {
+			dev_err(&context->mhi_dev->dev,
+				"request for unknown image: %d\n", image_id);
+			return -EINVAL;
+		}
+		context->active_image_id = image_id;
+		return 0;
 	}
 
 	/*
@@ -870,8 +891,10 @@ static void sahara_mhi_dl_xfer_cb(struct mhi_device *mhi_dev, struct mhi_result
 
 static const struct mhi_device_id sahara_mhi_match_table[] = {
 	{ .chan = "QAIC_SAHARA", },
+	{ .chan = "SAHARA"},
 	{},
 };
+MODULE_DEVICE_TABLE(mhi, sahara_mhi_match_table);
 
 static struct mhi_driver sahara_mhi_driver = {
 	.id_table = sahara_mhi_match_table,
drivers/bus/mhi/host/pci_generic.c +45 -0

57 lines not on this page · raw message

Signature · 1 line
2.34.1
#11 lore raw

Re: [PATCH v6 4/7] bus: mhi: Add QDU100 Sahara variant and firmware fallback

Thank you for your contribution! Sashiko AI review found 5 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 5 potential issue(s) to consider:

New issues:
- [Medium] The firmware fallback mechanism inadvertently changes the behavior for existing AIC100 and AIC200 devices, bypassing their explicit allowlist and contradicting the commit message.
- [Low] The dev_err statement in the fallback error path uses a signed format specifier %d to print an unsigned 32-bit integer.

Pre-existing issues:
- [High] A malicious or malfunctioning device can trigger a NULL pointer dereference or Use-After-Free by requesting an image transfer with image_id equal to SAHARA_IMAGE_ID_NONE (0xFFFFFFFF).
- [High] Missing cancellation of read_data_work during driver removal leads to a Use-After-Free of context and context->firmware.
- [High] Lockless concurrency between fw_work and read_data_work allows a device-triggered Use-After-Free during firmware streaming.
--

commit e80f9e2e7047bfa82528957f04343840159ae175
Author: Kishore Batta <[email protected]>

bus: mhi: Add QDU100 Sahara variant and firmware fallback

Extend the probe time variant selection to match the SAHARA MHI channel for QDU100 devices. Add a firmware lookup fallback to provision images dynamically if they aren't explicitly listed in the image table.

Quoted 12 lines
drivers/bus/mhi/host/clients/sahara/sahara.c +1 -0
index e339c67e236af..9adbd84859073 100644
@@ -189,6 +189,7 @@ static bool is_streaming(struct sahara_context *context)
 
 static int sahara_find_image(struct sahara_context *context, u32 image_id)
 {
+	char *fw_path;
 	int ret;
 
 	if (image_id == context->active_image_id)
Quoted 1 line

return 0;

[Severity: High]
This is a pre-existing issue, but can a malicious or malfunctioning device trigger a NULL pointer dereference or use-after-free here?

If a device requests an image transfer with image_id equal to SAHARA_IMAGE_ID_NONE (0xFFFFFFFF), context->active_image_id is initialized and reset to this value. sahara_find_image() would return success (0) without acquiring firmware.

Later, sahara_read_data() unconditionally dereferences context->firmware->size, which would be NULL or a dangling pointer.

[ ... ]

drivers/bus/mhi/host/clients/sahara/sahara.c static int sahara_find_image(struct sahara_context *context, u32 image_id) · line 209

Quoted 11 lines
drivers/bus/mhi/host/clients/sahara/sahara.c +5 -2
@@ -201,8 +202,28 @@ static int sahara_find_image(struct sahara_context *context, u32 image_id)
 	}
 
 	if (image_id >= context->table_size || !context->image_table[image_id]) {
-		dev_err(&context->mhi_dev->dev, "request for unknown image: %d\n", image_id);
-		return -EINVAL;
+		if (!context->fw_folder) {
+			dev_err(&context->mhi_dev->dev,
+				"Request for unknown image: %u (no fw folder)\n", image_id);
+			return -EINVAL;
+		}

[Severity: Medium]
The commit message states this change does not affect behavior for existing AIC100 and AIC200 devices. However, legacy AIC devices populate fw_folder at probe time.

Since this check relies on !context->fw_folder instead of matching against the SAHARA channel, will legacy AIC devices fall through to the VFS lookup via firmware_request_nowarn() instead of immediately returning -EINVAL?

drivers/bus/mhi/host/clients/sahara/sahara.c static int sahara_find_image(struct sahara_context *context, u32 image_id) · line 222

Quoted 13 lines
drivers/bus/mhi/host/clients/sahara/sahara.c +13 -0
+
+		fw_path = kasprintf(GFP_KERNEL, "qcom/%s/%u",
+				    context->fw_folder, image_id);
+		if (!fw_path)
+			return -ENOMEM;
+
+		ret = firmware_request_nowarn(&context->firmware,
+					      fw_path,
+					      &context->mhi_dev->dev);
+		kfree(fw_path);
+		if (ret) {
+			dev_err(&context->mhi_dev->dev,
+				"request for unknown image: %d\n", image_id);

[Severity: Low]
Is %d the correct format specifier here? image_id is a u32, so if the requested ID is greater than INT_MAX, it will be printed as a negative number.

drivers/bus/mhi/host/clients/sahara/sahara.c static int sahara_find_image(struct sahara_context *context, u32 image_id) · line 227

Quoted 5 lines
drivers/bus/mhi/host/clients/sahara/sahara.c +4 -0
+			return -EINVAL;
+		}
+		context->active_image_id = image_id;
+		return 0;
 	}

[Severity: High]
This is a pre-existing issue, but does device teardown systematically omit canceling read_data_work?

When the driver is unbound, sahara_mhi_remove() calls cancel_work_sync() for fw_work and dump_work, but fails to cancel read_data_work. If read_data_work is pending or executing, it can run after remove() returns, leading to a use-after-free of the devres-managed context.

[Severity: High]
This is a pre-existing issue, but is there lockless concurrency between fw_work and read_data_work that allows a device-triggered use-after-free?

During firmware streaming, sahara_read_data() schedules read_data_work. If a device sends a SAHARA_END_OF_IMAGE_CMD immediately after a read request, the command is processed by fw_work which calls sahara_release_image() to free context->firmware.

Because these are independent work items without synchronization, could an untrusted MHI device deliberately race the EOI command against the executing streaming task?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/20260701-sahara_protocol_new_v2-v6-0-3a78362c4741@oss.qualcomm.com?part=4
#12 lore raw

Re: [PATCH v6 4/7] bus: mhi: Add QDU100 Sahara variant and firmware fallback

In reply to Kishore Batta
Quoted 64 lines · Kishore Batta

The Sahara driver currently selects a firmware image table based on the attached device, but it does not recognize QDU100 devices that expose the protocol on the SAHARA MHI channel. As a result, the host cannot associate QDU100 devices with the correct firmware namespace during image transfer.

Extend the probe time variant selection to match the SAHARA MHI channel and associate it with the QDU100 firmware folder. Add a firmware lookup fallback for cases where an image does not have an explicit entry in the device's firmware table. This allows required images to be provisioned by the platform.

This change only affects devices matched on the SAHARA MHI channel and does not change behavior for existing AIC100 and AIC200 devices.

---
 drivers/bus/mhi/host/clients/sahara/sahara.c | 27 +++++++++++++++--
 drivers/bus/mhi/host/pci_generic.c           | 45 ++++++++++++++++++++++++++++
 2 files changed, 70 insertions(+), 2 deletions(-)
drivers/bus/mhi/host/clients/sahara/sahara.c +23 -2
index e339c67e236af271645ca81cc517efd9eead87e4..9adbd84859073d8024ba2a5fcfa33897439d6759 100644
@@ -189,6 +189,7 @@ static bool is_streaming(struct sahara_context *context)
 
 static int sahara_find_image(struct sahara_context *context, u32 image_id)
 {
+	char *fw_path;
 	int ret;
 
 	if (image_id == context->active_image_id)
@@ -201,8 +202,28 @@ static int sahara_find_image(struct sahara_context *context, u32 image_id)
 	}
 
 	if (image_id >= context->table_size || !context->image_table[image_id]) {
-		dev_err(&context->mhi_dev->dev, "request for unknown image: %d\n", image_id);
-		return -EINVAL;
+		if (!context->fw_folder) {
+			dev_err(&context->mhi_dev->dev,
+				"Request for unknown image: %u (no fw folder)\n", image_id);
+			return -EINVAL;
+		}
+
+		fw_path = kasprintf(GFP_KERNEL, "qcom/%s/%u",
+				    context->fw_folder, image_id);
+		if (!fw_path)
+			return -ENOMEM;
+
+		ret = firmware_request_nowarn(&context->firmware,
+					      fw_path,
+					      &context->mhi_dev->dev);
+		kfree(fw_path);
+		if (ret) {
+			dev_err(&context->mhi_dev->dev,
+				"request for unknown image: %d\n", image_id);
+			return -EINVAL;
+		}
+		context->active_image_id = image_id;
+		return 0;
 	}
 
 	/*

drivers/bus/mhi/host/clients/sahara/sahara.c static void sahara_mhi_dl_xfer_cb(struct mhi_device *mhi_dev, struct mhi_result · line 898

Quoted 8 lines
drivers/bus/mhi/host/clients/sahara/sahara.c +2 -0
@@ -870,8 +891,10 @@ static void sahara_mhi_dl_xfer_cb(struct mhi_device *mhi_dev, struct mhi_result
 
 static const struct mhi_device_id sahara_mhi_match_table[] = {
 	{ .chan = "QAIC_SAHARA", },
+	{ .chan = "SAHARA"},
 	{},
 };
+MODULE_DEVICE_TABLE(mhi, sahara_mhi_match_table);
 

This change should belong to a separate patch.

Quoted 52 lines
drivers/bus/mhi/host/clients/sahara/sahara.c +0 -0
 static struct mhi_driver sahara_mhi_driver = {
 	.id_table = sahara_mhi_match_table,
drivers/bus/mhi/host/pci_generic.c +37 -0
index 391ab146f501c6ce1c81f6138f7c491a49c2f264..82e41632afc555a53dec3d8395558ae039b33bbd 100644
@@ -300,6 +300,43 @@ static const struct mhi_pci_dev_info mhi_qcom_qdu100_info = {
 	.reset_on_remove = true,
 };
 
+static const char * const qdu100_image_table[] = {
+	[5] = "qcom/qdu100/uefi.elf",
+	[8] = "qcom/qdu100/qdsp6sw.mbn",
+	[16] = "qcom/qdu100/efs1.bin",
+	[17] = "qcom/qdu100/efs2.bin",
+	[20] = "qcom/qdu100/efs3.bin",
+	[23] = "qcom/qdu100/aop.mbn",
+	[25] = "qcom/qdu100/tz.mbn",
+	[29] = "qcom/qdu100/zeros_1sector.bin",
+	[33] = "qcom/qdu100/hypvm.mbn",
+	[34] = "qcom/qdu100/mdmddr.mbn",
+	[36] = "qcom/qdu100/multi_image_qti.mbn",
+	[37] = "qcom/qdu100/multi_image.mbn",
+	[38] = "qcom/qdu100/xbl_config.elf",
+	[39] = "qcom/qdu100/abl_userdebug.elf",
+	[40] = "qcom/qdu100/zeros_1sector.bin",
+	[41] = "qcom/qdu100/devcfg.mbn",
+	[42] = "qcom/qdu100/zeros_1sector.bin",
+	[45] = "qcom/qdu100/tools_l.elf",
+	[46] = "qcom/qdu100/Quantum.elf",
+	[47] = "qcom/qdu100/quest.elf",
+	[48] = "qcom/qdu100/xbl_ramdump.elf",
+	[49] = "qcom/qdu100/shrm.elf",
+	[50] = "qcom/qdu100/cpucp.elf",
+	[51] = "qcom/qdu100/aop_devcfg.mbn",
+	[52] = "qcom/qdu100/fw_csm_gsi_3.0.elf",
+	[53] = "qcom/qdu100/qdsp6sw_dtbs.elf",
+	[54] = "qcom/qdu100/qupv3fw.elf",
+};
+
+static const struct mhi_sahara_fw_table qdu100_sahara_fw = {
+	.image_table = qdu100_image_table,
+	.table_size = ARRAY_SIZE(qdu100_image_table),
+	.fw_folder = "qdu100",
+	.non_streaming = false,
+};
+
 static const struct mhi_channel_config mhi_qcom_sa8775p_channels[] = {
 	MHI_CHANNEL_CONFIG_UL(46, "IP_SW0", 2048, 1),
 	MHI_CHANNEL_CONFIG_DL(47, "IP_SW0", 2048, 2),
@@ -1399,6 +1436,14 @@ static int mhi_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
 

drivers/bus/mhi/host/pci_generic.c static int mhi_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id) · line 1446

Quoted 10 lines
drivers/bus/mhi/host/pci_generic.c +8 -0
 	pci_set_drvdata(pdev, mhi_pdev);
 
+	/*
+	 * Provide Sahara firmware mapping. Sahara consumes it via
+	 * mhi_dev->mhi_cntrl->sahara_fw at probe time.
+	 */
+	if (info == &mhi_qcom_qdu100_info ||
+	    (info->name && !strcmp(info->name, "qcom-qdu100")))
+		mhi_cntrl->sahara_fw = &qdu100_sahara_fw;
+

Why are you adding QAIC MHI controller config in pci_generic driver? This driver only handles Modem devices.

- Mani

Signature · 1 line
மணிவண்ணன் சதாசிவம்
#13 lore raw

Re: [PATCH v6 4/7] bus: mhi: Add QDU100 Sahara variant and firmware fallback

Quoted 66 lines · Manivannan Sadhasivam
Quoted 65 lines · Kishore Batta

The Sahara driver currently selects a firmware image table based on the attached device, but it does not recognize QDU100 devices that expose the protocol on the SAHARA MHI channel. As a result, the host cannot associate QDU100 devices with the correct firmware namespace during image transfer.

Extend the probe time variant selection to match the SAHARA MHI channel and associate it with the QDU100 firmware folder. Add a firmware lookup fallback for cases where an image does not have an explicit entry in the device's firmware table. This allows required images to be provisioned by the platform.

This change only affects devices matched on the SAHARA MHI channel and does not change behavior for existing AIC100 and AIC200 devices.

---
  drivers/bus/mhi/host/clients/sahara/sahara.c | 27 +++++++++++++++--
  drivers/bus/mhi/host/pci_generic.c           | 45 ++++++++++++++++++++++++++++
  2 files changed, 70 insertions(+), 2 deletions(-)
drivers/bus/mhi/host/clients/sahara/sahara.c +23 -2
index e339c67e236af271645ca81cc517efd9eead87e4..9adbd84859073d8024ba2a5fcfa33897439d6759 100644
@@ -189,6 +189,7 @@ static bool is_streaming(struct sahara_context *context)
 
  static int sahara_find_image(struct sahara_context *context, u32 image_id)
  {
+	char *fw_path;
  	int ret;
 
  	if (image_id == context->active_image_id)
@@ -201,8 +202,28 @@ static int sahara_find_image(struct sahara_context *context, u32 image_id)
  	}
 
  	if (image_id >= context->table_size || !context->image_table[image_id]) {
-		dev_err(&context->mhi_dev->dev, "request for unknown image: %d\n", image_id);
-		return -EINVAL;
+		if (!context->fw_folder) {
+			dev_err(&context->mhi_dev->dev,
+				"Request for unknown image: %u (no fw folder)\n", image_id);
+			return -EINVAL;
+		}
+
+		fw_path = kasprintf(GFP_KERNEL, "qcom/%s/%u",
+				    context->fw_folder, image_id);
+		if (!fw_path)
+			return -ENOMEM;
+
+		ret = firmware_request_nowarn(&context->firmware,
+					      fw_path,
+					      &context->mhi_dev->dev);
+		kfree(fw_path);
+		if (ret) {
+			dev_err(&context->mhi_dev->dev,
+				"request for unknown image: %d\n", image_id);
+			return -EINVAL;
+		}
+		context->active_image_id = image_id;
+		return 0;
  	}
 
  	/*
@@ -870,8 +891,10 @@ static void sahara_mhi_dl_xfer_cb(struct mhi_device *mhi_dev, struct mhi_result
 
Quoted 8 lines

drivers/bus/mhi/host/clients/sahara/sahara.c static void sahara_mhi_dl_xfer_cb(struct mhi_device *mhi_dev, struct mhi_result · line 898

Quoted 7 lines
drivers/bus/mhi/host/clients/sahara/sahara.c +2 -0
  static const struct mhi_device_id sahara_mhi_match_table[] = {
  	{ .chan = "QAIC_SAHARA", },
+	{ .chan = "SAHARA"},
  	{},
  };
+MODULE_DEVICE_TABLE(mhi, sahara_mhi_match_table);
 

This change should belong to a separate patch.

There was a review comment from Jeff(v4, patch 3) to move to this change to the patch which adds QDU100 support so that it doesn't break bisect. Please let me know if i need to move it to a separate patch altogether ?

Quoted 68 lines
Quoted 53 lines
drivers/bus/mhi/host/clients/sahara/sahara.c +0 -0
  static struct mhi_driver sahara_mhi_driver = {
  	.id_table = sahara_mhi_match_table,
drivers/bus/mhi/host/pci_generic.c +37 -0
index 391ab146f501c6ce1c81f6138f7c491a49c2f264..82e41632afc555a53dec3d8395558ae039b33bbd 100644
@@ -300,6 +300,43 @@ static const struct mhi_pci_dev_info mhi_qcom_qdu100_info = {
  	.reset_on_remove = true,
  };
 
+static const char * const qdu100_image_table[] = {
+	[5] = "qcom/qdu100/uefi.elf",
+	[8] = "qcom/qdu100/qdsp6sw.mbn",
+	[16] = "qcom/qdu100/efs1.bin",
+	[17] = "qcom/qdu100/efs2.bin",
+	[20] = "qcom/qdu100/efs3.bin",
+	[23] = "qcom/qdu100/aop.mbn",
+	[25] = "qcom/qdu100/tz.mbn",
+	[29] = "qcom/qdu100/zeros_1sector.bin",
+	[33] = "qcom/qdu100/hypvm.mbn",
+	[34] = "qcom/qdu100/mdmddr.mbn",
+	[36] = "qcom/qdu100/multi_image_qti.mbn",
+	[37] = "qcom/qdu100/multi_image.mbn",
+	[38] = "qcom/qdu100/xbl_config.elf",
+	[39] = "qcom/qdu100/abl_userdebug.elf",
+	[40] = "qcom/qdu100/zeros_1sector.bin",
+	[41] = "qcom/qdu100/devcfg.mbn",
+	[42] = "qcom/qdu100/zeros_1sector.bin",
+	[45] = "qcom/qdu100/tools_l.elf",
+	[46] = "qcom/qdu100/Quantum.elf",
+	[47] = "qcom/qdu100/quest.elf",
+	[48] = "qcom/qdu100/xbl_ramdump.elf",
+	[49] = "qcom/qdu100/shrm.elf",
+	[50] = "qcom/qdu100/cpucp.elf",
+	[51] = "qcom/qdu100/aop_devcfg.mbn",
+	[52] = "qcom/qdu100/fw_csm_gsi_3.0.elf",
+	[53] = "qcom/qdu100/qdsp6sw_dtbs.elf",
+	[54] = "qcom/qdu100/qupv3fw.elf",
+};
+
+static const struct mhi_sahara_fw_table qdu100_sahara_fw = {
+	.image_table = qdu100_image_table,
+	.table_size = ARRAY_SIZE(qdu100_image_table),
+	.fw_folder = "qdu100",
+	.non_streaming = false,
+};
+
  static const struct mhi_channel_config mhi_qcom_sa8775p_channels[] = {
  	MHI_CHANNEL_CONFIG_UL(46, "IP_SW0", 2048, 1),
  	MHI_CHANNEL_CONFIG_DL(47, "IP_SW0", 2048, 2),
@@ -1399,6 +1436,14 @@ static int mhi_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
 

drivers/bus/mhi/host/pci_generic.c static int mhi_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id) · line 1446

Quoted 10 lines
drivers/bus/mhi/host/pci_generic.c +8 -0
  	pci_set_drvdata(pdev, mhi_pdev);
 
+	/*
+	 * Provide Sahara firmware mapping. Sahara consumes it via
+	 * mhi_dev->mhi_cntrl->sahara_fw at probe time.
+	 */
+	if (info == &mhi_qcom_qdu100_info ||
+	    (info->name && !strcmp(info->name, "qcom-qdu100")))
+		mhi_cntrl->sahara_fw = &qdu100_sahara_fw;
+

Why are you adding QAIC MHI controller config in pci_generic driver? This driver only handles Modem devices.

- Mani

#14 lore raw

Re: [PATCH v6 4/7] bus: mhi: Add QDU100 Sahara variant and firmware fallback

Quoted 71 lines · Kishore Batta
Quoted 60 lines · Manivannan Sadhasivam
Quoted 59 lines · Kishore Batta

The Sahara driver currently selects a firmware image table based on the attached device, but it does not recognize QDU100 devices that expose the protocol on the SAHARA MHI channel. As a result, the host cannot associate QDU100 devices with the correct firmware namespace during image transfer.

Extend the probe time variant selection to match the SAHARA MHI channel and associate it with the QDU100 firmware folder. Add a firmware lookup fallback for cases where an image does not have an explicit entry in the device's firmware table. This allows required images to be provisioned by the platform.

This change only affects devices matched on the SAHARA MHI channel and does not change behavior for existing AIC100 and AIC200 devices.

---
  drivers/bus/mhi/host/clients/sahara/sahara.c | 27 +++++++++++++++--
  drivers/bus/mhi/host/pci_generic.c           | 45 ++++++++++++++++++++++++++++
  2 files changed, 70 insertions(+), 2 deletions(-)
drivers/bus/mhi/host/clients/sahara/sahara.c +23 -2
index e339c67e236af271645ca81cc517efd9eead87e4..9adbd84859073d8024ba2a5fcfa33897439d6759 100644
@@ -189,6 +189,7 @@ static bool is_streaming(struct sahara_context *context)
  static int sahara_find_image(struct sahara_context *context, u32 image_id)
  {
+	char *fw_path;
  	int ret;
  	if (image_id == context->active_image_id)
@@ -201,8 +202,28 @@ static int sahara_find_image(struct sahara_context *context, u32 image_id)
  	}
  	if (image_id >= context->table_size || !context->image_table[image_id]) {
-		dev_err(&context->mhi_dev->dev, "request for unknown image: %d\n", image_id);
-		return -EINVAL;
+		if (!context->fw_folder) {
+			dev_err(&context->mhi_dev->dev,
+				"Request for unknown image: %u (no fw folder)\n", image_id);
+			return -EINVAL;
+		}
+
+		fw_path = kasprintf(GFP_KERNEL, "qcom/%s/%u",
+				    context->fw_folder, image_id);
+		if (!fw_path)
+			return -ENOMEM;
+
+		ret = firmware_request_nowarn(&context->firmware,
+					      fw_path,
+					      &context->mhi_dev->dev);
+		kfree(fw_path);
+		if (ret) {
+			dev_err(&context->mhi_dev->dev,
+				"request for unknown image: %d\n", image_id);
+			return -EINVAL;
+		}
+		context->active_image_id = image_id;
+		return 0;
  	}
  	/*
Quoted 8 lines

drivers/bus/mhi/host/clients/sahara/sahara.c static void sahara_mhi_dl_xfer_cb(struct mhi_device *mhi_dev, struct mhi_result · line 896

Quoted 7 lines
drivers/bus/mhi/host/clients/sahara/sahara.c +2 -0
@@ -870,8 +891,10 @@ static void sahara_mhi_dl_xfer_cb(struct mhi_device *mhi_dev, struct mhi_result
  static const struct mhi_device_id sahara_mhi_match_table[] = {
  	{ .chan = "QAIC_SAHARA", },
+	{ .chan = "SAHARA"},
  	{},
  };
+MODULE_DEVICE_TABLE(mhi, sahara_mhi_match_table);

This change should belong to a separate patch.

Quoted 4 lines

There was a review comment from Jeff(v4, patch 3) to move to this change to the patch which adds QDU100 support so that it doesn't break bisect. Please let me know if i need to move it to a separate patch altogether ?

Since it is a new device support for Sahara, bisect is not really a problem. But I do see a value in keeping everything in a single patch.

- Mani

Signature · 1 line
மணிவண்ணன் சதாசிவம்
#15 lore raw

Re: [PATCH v6 4/7] bus: mhi: Add QDU100 Sahara variant and firmware fallback

Quoted 70 lines · Manivannan Sadhasivam
Quoted 69 lines · Kishore Batta
Quoted 68 lines · Manivannan Sadhasivam
Quoted 60 lines · Kishore Batta

The Sahara driver currently selects a firmware image table based on the attached device, but it does not recognize QDU100 devices that expose the protocol on the SAHARA MHI channel. As a result, the host cannot associate QDU100 devices with the correct firmware namespace during image transfer.

Extend the probe time variant selection to match the SAHARA MHI channel and associate it with the QDU100 firmware folder. Add a firmware lookup fallback for cases where an image does not have an explicit entry in the device's firmware table. This allows required images to be provisioned by the platform.

This change only affects devices matched on the SAHARA MHI channel and does not change behavior for existing AIC100 and AIC200 devices.

---
   drivers/bus/mhi/host/clients/sahara/sahara.c | 27 +++++++++++++++--
   drivers/bus/mhi/host/pci_generic.c           | 45 ++++++++++++++++++++++++++++
   2 files changed, 70 insertions(+), 2 deletions(-)
drivers/bus/mhi/host/clients/sahara/sahara.c +23 -2
index e339c67e236af271645ca81cc517efd9eead87e4..9adbd84859073d8024ba2a5fcfa33897439d6759 100644
@@ -189,6 +189,7 @@ static bool is_streaming(struct sahara_context *context)
   static int sahara_find_image(struct sahara_context *context, u32 image_id)
   {
+	char *fw_path;
   	int ret;
   	if (image_id == context->active_image_id)
@@ -201,8 +202,28 @@ static int sahara_find_image(struct sahara_context *context, u32 image_id)
   	}
   	if (image_id >= context->table_size || !context->image_table[image_id]) {
-		dev_err(&context->mhi_dev->dev, "request for unknown image: %d\n", image_id);
-		return -EINVAL;
+		if (!context->fw_folder) {
+			dev_err(&context->mhi_dev->dev,
+				"Request for unknown image: %u (no fw folder)\n", image_id);
+			return -EINVAL;
+		}
+
+		fw_path = kasprintf(GFP_KERNEL, "qcom/%s/%u",
+				    context->fw_folder, image_id);
+		if (!fw_path)
+			return -ENOMEM;
+
+		ret = firmware_request_nowarn(&context->firmware,
+					      fw_path,
+					      &context->mhi_dev->dev);
+		kfree(fw_path);
+		if (ret) {
+			dev_err(&context->mhi_dev->dev,
+				"request for unknown image: %d\n", image_id);
+			return -EINVAL;
+		}
+		context->active_image_id = image_id;
+		return 0;
   	}
   	/*

drivers/bus/mhi/host/clients/sahara/sahara.c static void sahara_mhi_dl_xfer_cb(struct mhi_device *mhi_dev, struct mhi_result · line 896

Quoted 6 lines
drivers/bus/mhi/host/clients/sahara/sahara.c +2 -0
@@ -870,8 +891,10 @@ static void sahara_mhi_dl_xfer_cb(struct mhi_device *mhi_dev, struct mhi_result
   static const struct mhi_device_id sahara_mhi_match_table[] = {
   	{ .chan = "QAIC_SAHARA", },
+	{ .chan = "SAHARA"},
   	{},
   };
+MODULE_DEVICE_TABLE(mhi, sahara_mhi_match_table);

This change should belong to a separate patch.

Quoted 8 lines
Quoted 4 lines

There was a review comment from Jeff(v4, patch 3) to move to this change to the patch which adds QDU100 support so that it doesn't break bisect. Please let me know if i need to move it to a separate patch altogether ?

Since it is a new device support for Sahara, bisect is not really a problem. But I do see a value in keeping everything in a single patch.

- Mani

Sure. I am keeping this change in the same patch.

#16 lore raw

Re: [PATCH v6 4/7] bus: mhi: Add QDU100 Sahara variant and firmware fallback

In reply to Manivannan Sadhasivam
Quoted 140 lines · Manivannan Sadhasivam
Quoted 64 lines · Kishore Batta

The Sahara driver currently selects a firmware image table based on the attached device, but it does not recognize QDU100 devices that expose the protocol on the SAHARA MHI channel. As a result, the host cannot associate QDU100 devices with the correct firmware namespace during image transfer.

Extend the probe time variant selection to match the SAHARA MHI channel and associate it with the QDU100 firmware folder. Add a firmware lookup fallback for cases where an image does not have an explicit entry in the device's firmware table. This allows required images to be provisioned by the platform.

This change only affects devices matched on the SAHARA MHI channel and does not change behavior for existing AIC100 and AIC200 devices.

---
  drivers/bus/mhi/host/clients/sahara/sahara.c | 27 +++++++++++++++--
  drivers/bus/mhi/host/pci_generic.c           | 45 ++++++++++++++++++++++++++++
  2 files changed, 70 insertions(+), 2 deletions(-)
drivers/bus/mhi/host/clients/sahara/sahara.c +23 -2
index e339c67e236af271645ca81cc517efd9eead87e4..9adbd84859073d8024ba2a5fcfa33897439d6759 100644
@@ -189,6 +189,7 @@ static bool is_streaming(struct sahara_context *context)
 
  static int sahara_find_image(struct sahara_context *context, u32 image_id)
  {
+	char *fw_path;
  	int ret;
 
  	if (image_id == context->active_image_id)
@@ -201,8 +202,28 @@ static int sahara_find_image(struct sahara_context *context, u32 image_id)
  	}
 
  	if (image_id >= context->table_size || !context->image_table[image_id]) {
-		dev_err(&context->mhi_dev->dev, "request for unknown image: %d\n", image_id);
-		return -EINVAL;
+		if (!context->fw_folder) {
+			dev_err(&context->mhi_dev->dev,
+				"Request for unknown image: %u (no fw folder)\n", image_id);
+			return -EINVAL;
+		}
+
+		fw_path = kasprintf(GFP_KERNEL, "qcom/%s/%u",
+				    context->fw_folder, image_id);
+		if (!fw_path)
+			return -ENOMEM;
+
+		ret = firmware_request_nowarn(&context->firmware,
+					      fw_path,
+					      &context->mhi_dev->dev);
+		kfree(fw_path);
+		if (ret) {
+			dev_err(&context->mhi_dev->dev,
+				"request for unknown image: %d\n", image_id);
+			return -EINVAL;
+		}
+		context->active_image_id = image_id;
+		return 0;
  	}
 
  	/*

drivers/bus/mhi/host/clients/sahara/sahara.c static void sahara_mhi_dl_xfer_cb(struct mhi_device *mhi_dev, struct mhi_result · line 898

Quoted 8 lines
drivers/bus/mhi/host/clients/sahara/sahara.c +2 -0
@@ -870,8 +891,10 @@ static void sahara_mhi_dl_xfer_cb(struct mhi_device *mhi_dev, struct mhi_result
 
  static const struct mhi_device_id sahara_mhi_match_table[] = {
  	{ .chan = "QAIC_SAHARA", },
+	{ .chan = "SAHARA"},
  	{},
  };
+MODULE_DEVICE_TABLE(mhi, sahara_mhi_match_table);
 

This change should belong to a separate patch.

Quoted 52 lines
drivers/bus/mhi/host/clients/sahara/sahara.c +0 -0
  static struct mhi_driver sahara_mhi_driver = {
  	.id_table = sahara_mhi_match_table,
drivers/bus/mhi/host/pci_generic.c +37 -0
index 391ab146f501c6ce1c81f6138f7c491a49c2f264..82e41632afc555a53dec3d8395558ae039b33bbd 100644
@@ -300,6 +300,43 @@ static const struct mhi_pci_dev_info mhi_qcom_qdu100_info = {
  	.reset_on_remove = true,
  };
 
+static const char * const qdu100_image_table[] = {
+	[5] = "qcom/qdu100/uefi.elf",
+	[8] = "qcom/qdu100/qdsp6sw.mbn",
+	[16] = "qcom/qdu100/efs1.bin",
+	[17] = "qcom/qdu100/efs2.bin",
+	[20] = "qcom/qdu100/efs3.bin",
+	[23] = "qcom/qdu100/aop.mbn",
+	[25] = "qcom/qdu100/tz.mbn",
+	[29] = "qcom/qdu100/zeros_1sector.bin",
+	[33] = "qcom/qdu100/hypvm.mbn",
+	[34] = "qcom/qdu100/mdmddr.mbn",
+	[36] = "qcom/qdu100/multi_image_qti.mbn",
+	[37] = "qcom/qdu100/multi_image.mbn",
+	[38] = "qcom/qdu100/xbl_config.elf",
+	[39] = "qcom/qdu100/abl_userdebug.elf",
+	[40] = "qcom/qdu100/zeros_1sector.bin",
+	[41] = "qcom/qdu100/devcfg.mbn",
+	[42] = "qcom/qdu100/zeros_1sector.bin",
+	[45] = "qcom/qdu100/tools_l.elf",
+	[46] = "qcom/qdu100/Quantum.elf",
+	[47] = "qcom/qdu100/quest.elf",
+	[48] = "qcom/qdu100/xbl_ramdump.elf",
+	[49] = "qcom/qdu100/shrm.elf",
+	[50] = "qcom/qdu100/cpucp.elf",
+	[51] = "qcom/qdu100/aop_devcfg.mbn",
+	[52] = "qcom/qdu100/fw_csm_gsi_3.0.elf",
+	[53] = "qcom/qdu100/qdsp6sw_dtbs.elf",
+	[54] = "qcom/qdu100/qupv3fw.elf",
+};
+
+static const struct mhi_sahara_fw_table qdu100_sahara_fw = {
+	.image_table = qdu100_image_table,
+	.table_size = ARRAY_SIZE(qdu100_image_table),
+	.fw_folder = "qdu100",
+	.non_streaming = false,
+};
+
  static const struct mhi_channel_config mhi_qcom_sa8775p_channels[] = {
  	MHI_CHANNEL_CONFIG_UL(46, "IP_SW0", 2048, 1),
  	MHI_CHANNEL_CONFIG_DL(47, "IP_SW0", 2048, 2),
@@ -1399,6 +1436,14 @@ static int mhi_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
 

drivers/bus/mhi/host/pci_generic.c static int mhi_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id) · line 1446

Quoted 10 lines
drivers/bus/mhi/host/pci_generic.c +8 -0
  	pci_set_drvdata(pdev, mhi_pdev);
 
+	/*
+	 * Provide Sahara firmware mapping. Sahara consumes it via
+	 * mhi_dev->mhi_cntrl->sahara_fw at probe time.
+	 */
+	if (info == &mhi_qcom_qdu100_info ||
+	    (info->name && !strcmp(info->name, "qcom-qdu100")))
+		mhi_cntrl->sahara_fw = &qdu100_sahara_fw;
+

Why are you adding QAIC MHI controller config in pci_generic driver? This driver only handles Modem devices.

Quoted 1 line

- Mani

Hi Mani, Above one is for QDU100 device. QAIC is not added in this file.

Quoted 1 line
#17 lore raw

Re: [PATCH v6 4/7] bus: mhi: Add QDU100 Sahara variant and firmware fallback

Quoted 133 lines · Kishore Batta
Quoted 131 lines · Manivannan Sadhasivam
Quoted 60 lines · Kishore Batta

The Sahara driver currently selects a firmware image table based on the attached device, but it does not recognize QDU100 devices that expose the protocol on the SAHARA MHI channel. As a result, the host cannot associate QDU100 devices with the correct firmware namespace during image transfer.

Extend the probe time variant selection to match the SAHARA MHI channel and associate it with the QDU100 firmware folder. Add a firmware lookup fallback for cases where an image does not have an explicit entry in the device's firmware table. This allows required images to be provisioned by the platform.

This change only affects devices matched on the SAHARA MHI channel and does not change behavior for existing AIC100 and AIC200 devices.

---
  drivers/bus/mhi/host/clients/sahara/sahara.c | 27 +++++++++++++++--
  drivers/bus/mhi/host/pci_generic.c           | 45 ++++++++++++++++++++++++++++
  2 files changed, 70 insertions(+), 2 deletions(-)
drivers/bus/mhi/host/clients/sahara/sahara.c +23 -2
index e339c67e236af271645ca81cc517efd9eead87e4..9adbd84859073d8024ba2a5fcfa33897439d6759 100644
@@ -189,6 +189,7 @@ static bool is_streaming(struct sahara_context *context)
  static int sahara_find_image(struct sahara_context *context, u32 image_id)
  {
+	char *fw_path;
  	int ret;
  	if (image_id == context->active_image_id)
@@ -201,8 +202,28 @@ static int sahara_find_image(struct sahara_context *context, u32 image_id)
  	}
  	if (image_id >= context->table_size || !context->image_table[image_id]) {
-		dev_err(&context->mhi_dev->dev, "request for unknown image: %d\n", image_id);
-		return -EINVAL;
+		if (!context->fw_folder) {
+			dev_err(&context->mhi_dev->dev,
+				"Request for unknown image: %u (no fw folder)\n", image_id);
+			return -EINVAL;
+		}
+
+		fw_path = kasprintf(GFP_KERNEL, "qcom/%s/%u",
+				    context->fw_folder, image_id);
+		if (!fw_path)
+			return -ENOMEM;
+
+		ret = firmware_request_nowarn(&context->firmware,
+					      fw_path,
+					      &context->mhi_dev->dev);
+		kfree(fw_path);
+		if (ret) {
+			dev_err(&context->mhi_dev->dev,
+				"request for unknown image: %d\n", image_id);
+			return -EINVAL;
+		}
+		context->active_image_id = image_id;
+		return 0;
  	}
  	/*

drivers/bus/mhi/host/clients/sahara/sahara.c static void sahara_mhi_dl_xfer_cb(struct mhi_device *mhi_dev, struct mhi_result · line 896

Quoted 6 lines
drivers/bus/mhi/host/clients/sahara/sahara.c +2 -0
@@ -870,8 +891,10 @@ static void sahara_mhi_dl_xfer_cb(struct mhi_device *mhi_dev, struct mhi_result
  static const struct mhi_device_id sahara_mhi_match_table[] = {
  	{ .chan = "QAIC_SAHARA", },
+	{ .chan = "SAHARA"},
  	{},
  };
+MODULE_DEVICE_TABLE(mhi, sahara_mhi_match_table);

This change should belong to a separate patch.

Quoted 50 lines
drivers/bus/mhi/host/clients/sahara/sahara.c +0 -0
  static struct mhi_driver sahara_mhi_driver = {
  	.id_table = sahara_mhi_match_table,
drivers/bus/mhi/host/pci_generic.c +37 -0
index 391ab146f501c6ce1c81f6138f7c491a49c2f264..82e41632afc555a53dec3d8395558ae039b33bbd 100644
@@ -300,6 +300,43 @@ static const struct mhi_pci_dev_info mhi_qcom_qdu100_info = {
  	.reset_on_remove = true,
  };
+static const char * const qdu100_image_table[] = {
+	[5] = "qcom/qdu100/uefi.elf",
+	[8] = "qcom/qdu100/qdsp6sw.mbn",
+	[16] = "qcom/qdu100/efs1.bin",
+	[17] = "qcom/qdu100/efs2.bin",
+	[20] = "qcom/qdu100/efs3.bin",
+	[23] = "qcom/qdu100/aop.mbn",
+	[25] = "qcom/qdu100/tz.mbn",
+	[29] = "qcom/qdu100/zeros_1sector.bin",
+	[33] = "qcom/qdu100/hypvm.mbn",
+	[34] = "qcom/qdu100/mdmddr.mbn",
+	[36] = "qcom/qdu100/multi_image_qti.mbn",
+	[37] = "qcom/qdu100/multi_image.mbn",
+	[38] = "qcom/qdu100/xbl_config.elf",
+	[39] = "qcom/qdu100/abl_userdebug.elf",
+	[40] = "qcom/qdu100/zeros_1sector.bin",
+	[41] = "qcom/qdu100/devcfg.mbn",
+	[42] = "qcom/qdu100/zeros_1sector.bin",
+	[45] = "qcom/qdu100/tools_l.elf",
+	[46] = "qcom/qdu100/Quantum.elf",
+	[47] = "qcom/qdu100/quest.elf",
+	[48] = "qcom/qdu100/xbl_ramdump.elf",
+	[49] = "qcom/qdu100/shrm.elf",
+	[50] = "qcom/qdu100/cpucp.elf",
+	[51] = "qcom/qdu100/aop_devcfg.mbn",
+	[52] = "qcom/qdu100/fw_csm_gsi_3.0.elf",
+	[53] = "qcom/qdu100/qdsp6sw_dtbs.elf",
+	[54] = "qcom/qdu100/qupv3fw.elf",
+};
+
+static const struct mhi_sahara_fw_table qdu100_sahara_fw = {
+	.image_table = qdu100_image_table,
+	.table_size = ARRAY_SIZE(qdu100_image_table),
+	.fw_folder = "qdu100",
+	.non_streaming = false,
+};
+
  static const struct mhi_channel_config mhi_qcom_sa8775p_channels[] = {
  	MHI_CHANNEL_CONFIG_UL(46, "IP_SW0", 2048, 1),
  	MHI_CHANNEL_CONFIG_DL(47, "IP_SW0", 2048, 2),

drivers/bus/mhi/host/pci_generic.c static int mhi_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id) · line 1444

Quoted 9 lines
drivers/bus/mhi/host/pci_generic.c +8 -0
@@ -1399,6 +1436,14 @@ static int mhi_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
  	pci_set_drvdata(pdev, mhi_pdev);
+	/*
+	 * Provide Sahara firmware mapping. Sahara consumes it via
+	 * mhi_dev->mhi_cntrl->sahara_fw at probe time.
+	 */
+	if (info == &mhi_qcom_qdu100_info ||
+	    (info->name && !strcmp(info->name, "qcom-qdu100")))
+		mhi_cntrl->sahara_fw = &qdu100_sahara_fw;
+

Why are you adding QAIC MHI controller config in pci_generic driver? This driver only handles Modem devices.

Quoted 5 lines
Quoted 1 line

- Mani

Hi Mani, Above one is for QDU100 device. QAIC is not added in this file.

Sorry, my bad. But anyhow, you should provide this mapping table pointer via 'struct mhi_pci_dev_info'.

- Mani

Signature · 1 line
மணிவண்ணன் சதாசிவம்
#18 lore raw

Re: [PATCH v6 4/7] bus: mhi: Add QDU100 Sahara variant and firmware fallback

Quoted 133 lines · Manivannan Sadhasivam
Quoted 132 lines · Kishore Batta
Quoted 131 lines · Manivannan Sadhasivam
Quoted 60 lines · Kishore Batta

The Sahara driver currently selects a firmware image table based on the attached device, but it does not recognize QDU100 devices that expose the protocol on the SAHARA MHI channel. As a result, the host cannot associate QDU100 devices with the correct firmware namespace during image transfer.

Extend the probe time variant selection to match the SAHARA MHI channel and associate it with the QDU100 firmware folder. Add a firmware lookup fallback for cases where an image does not have an explicit entry in the device's firmware table. This allows required images to be provisioned by the platform.

This change only affects devices matched on the SAHARA MHI channel and does not change behavior for existing AIC100 and AIC200 devices.

---
   drivers/bus/mhi/host/clients/sahara/sahara.c | 27 +++++++++++++++--
   drivers/bus/mhi/host/pci_generic.c           | 45 ++++++++++++++++++++++++++++
   2 files changed, 70 insertions(+), 2 deletions(-)
drivers/bus/mhi/host/clients/sahara/sahara.c +23 -2
index e339c67e236af271645ca81cc517efd9eead87e4..9adbd84859073d8024ba2a5fcfa33897439d6759 100644
@@ -189,6 +189,7 @@ static bool is_streaming(struct sahara_context *context)
   static int sahara_find_image(struct sahara_context *context, u32 image_id)
   {
+	char *fw_path;
   	int ret;
   	if (image_id == context->active_image_id)
@@ -201,8 +202,28 @@ static int sahara_find_image(struct sahara_context *context, u32 image_id)
   	}
   	if (image_id >= context->table_size || !context->image_table[image_id]) {
-		dev_err(&context->mhi_dev->dev, "request for unknown image: %d\n", image_id);
-		return -EINVAL;
+		if (!context->fw_folder) {
+			dev_err(&context->mhi_dev->dev,
+				"Request for unknown image: %u (no fw folder)\n", image_id);
+			return -EINVAL;
+		}
+
+		fw_path = kasprintf(GFP_KERNEL, "qcom/%s/%u",
+				    context->fw_folder, image_id);
+		if (!fw_path)
+			return -ENOMEM;
+
+		ret = firmware_request_nowarn(&context->firmware,
+					      fw_path,
+					      &context->mhi_dev->dev);
+		kfree(fw_path);
+		if (ret) {
+			dev_err(&context->mhi_dev->dev,
+				"request for unknown image: %d\n", image_id);
+			return -EINVAL;
+		}
+		context->active_image_id = image_id;
+		return 0;
   	}
   	/*

drivers/bus/mhi/host/clients/sahara/sahara.c static void sahara_mhi_dl_xfer_cb(struct mhi_device *mhi_dev, struct mhi_result · line 896

Quoted 6 lines
drivers/bus/mhi/host/clients/sahara/sahara.c +2 -0
@@ -870,8 +891,10 @@ static void sahara_mhi_dl_xfer_cb(struct mhi_device *mhi_dev, struct mhi_result
   static const struct mhi_device_id sahara_mhi_match_table[] = {
   	{ .chan = "QAIC_SAHARA", },
+	{ .chan = "SAHARA"},
   	{},
   };
+MODULE_DEVICE_TABLE(mhi, sahara_mhi_match_table);

This change should belong to a separate patch.

Quoted 50 lines
drivers/bus/mhi/host/clients/sahara/sahara.c +0 -0
   static struct mhi_driver sahara_mhi_driver = {
   	.id_table = sahara_mhi_match_table,
drivers/bus/mhi/host/pci_generic.c +37 -0
index 391ab146f501c6ce1c81f6138f7c491a49c2f264..82e41632afc555a53dec3d8395558ae039b33bbd 100644
@@ -300,6 +300,43 @@ static const struct mhi_pci_dev_info mhi_qcom_qdu100_info = {
   	.reset_on_remove = true,
   };
+static const char * const qdu100_image_table[] = {
+	[5] = "qcom/qdu100/uefi.elf",
+	[8] = "qcom/qdu100/qdsp6sw.mbn",
+	[16] = "qcom/qdu100/efs1.bin",
+	[17] = "qcom/qdu100/efs2.bin",
+	[20] = "qcom/qdu100/efs3.bin",
+	[23] = "qcom/qdu100/aop.mbn",
+	[25] = "qcom/qdu100/tz.mbn",
+	[29] = "qcom/qdu100/zeros_1sector.bin",
+	[33] = "qcom/qdu100/hypvm.mbn",
+	[34] = "qcom/qdu100/mdmddr.mbn",
+	[36] = "qcom/qdu100/multi_image_qti.mbn",
+	[37] = "qcom/qdu100/multi_image.mbn",
+	[38] = "qcom/qdu100/xbl_config.elf",
+	[39] = "qcom/qdu100/abl_userdebug.elf",
+	[40] = "qcom/qdu100/zeros_1sector.bin",
+	[41] = "qcom/qdu100/devcfg.mbn",
+	[42] = "qcom/qdu100/zeros_1sector.bin",
+	[45] = "qcom/qdu100/tools_l.elf",
+	[46] = "qcom/qdu100/Quantum.elf",
+	[47] = "qcom/qdu100/quest.elf",
+	[48] = "qcom/qdu100/xbl_ramdump.elf",
+	[49] = "qcom/qdu100/shrm.elf",
+	[50] = "qcom/qdu100/cpucp.elf",
+	[51] = "qcom/qdu100/aop_devcfg.mbn",
+	[52] = "qcom/qdu100/fw_csm_gsi_3.0.elf",
+	[53] = "qcom/qdu100/qdsp6sw_dtbs.elf",
+	[54] = "qcom/qdu100/qupv3fw.elf",
+};
+
+static const struct mhi_sahara_fw_table qdu100_sahara_fw = {
+	.image_table = qdu100_image_table,
+	.table_size = ARRAY_SIZE(qdu100_image_table),
+	.fw_folder = "qdu100",
+	.non_streaming = false,
+};
+
   static const struct mhi_channel_config mhi_qcom_sa8775p_channels[] = {
   	MHI_CHANNEL_CONFIG_UL(46, "IP_SW0", 2048, 1),
   	MHI_CHANNEL_CONFIG_DL(47, "IP_SW0", 2048, 2),

drivers/bus/mhi/host/pci_generic.c static int mhi_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id) · line 1444

Quoted 9 lines
drivers/bus/mhi/host/pci_generic.c +8 -0
@@ -1399,6 +1436,14 @@ static int mhi_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
   	pci_set_drvdata(pdev, mhi_pdev);
+	/*
+	 * Provide Sahara firmware mapping. Sahara consumes it via
+	 * mhi_dev->mhi_cntrl->sahara_fw at probe time.
+	 */
+	if (info == &mhi_qcom_qdu100_info ||
+	    (info->name && !strcmp(info->name, "qcom-qdu100")))
+		mhi_cntrl->sahara_fw = &qdu100_sahara_fw;
+

Why are you adding QAIC MHI controller config in pci_generic driver? This driver only handles Modem devices.

Quoted 8 lines
Quoted 4 lines
Quoted 1 line

- Mani

Hi Mani, Above one is for QDU100 device. QAIC is not added in this file.

Sorry, my bad. But anyhow, you should provide this mapping table pointer via 'struct mhi_pci_dev_info'.

- Mani

ACK. I made required changes and will publish in v7 patch set.

#19 lore raw

PATCH v6 5/7 bus: mhi: Load DDR training data using device serial number

In reply to Kishore Batta

Devices may provide device specific DDR training data that can be reused across boot to avoid retraining and reduce boot time. The Sahara driver currently always falls back to the default DDR training image, even when serial specific training data is available.

Extend the firmware loading logic for the DDR training image to first attempt loading a per-device image dervied from the device serial number. If the serial-specific image is not present, fall back to the existing default image, preserving current behavior.

This allows reuse of previously generated DDR training data when available, while keeping the existing training flow unchanged for devices without saved data or for all other firmware images.

---
 drivers/bus/mhi/host/clients/sahara/sahara.c | 25 ++++++++++++++++++++++++-
 1 file changed, 24 insertions(+), 1 deletion(-)
drivers/bus/mhi/host/clients/sahara/sahara.c +24 -1
index 9adbd84859073d8024ba2a5fcfa33897439d6759..b5ca6353540dc3815db6539e7424afdb749fd3f6 100644
@@ -59,6 +59,7 @@
 #define SAHARA_RESET_LENGTH		0x8
 #define SAHARA_MEM_DEBUG64_LENGTH	0x18
 #define SAHARA_MEM_READ64_LENGTH	0x18
+#define SAHARA_DDR_TRAINING_IMG_ID	34
 
 struct sahara_packet {
 	__le32 cmd;
@@ -226,6 +227,27 @@ static int sahara_find_image(struct sahara_context *context, u32 image_id)
 		return 0;
 	}
 
+	/* DDR training special case: Try per-serial number file first */
+	if (image_id == SAHARA_DDR_TRAINING_IMG_ID && context->fw_folder) {
+		u32 serial_num = context->mhi_dev->mhi_cntrl->serial_number;
+
+		fw_path = kasprintf(GFP_KERNEL,
+				    "qcom/%s/mdmddr_0x%x.mbn",
+				    context->fw_folder, serial_num);
+		if (!fw_path)
+			return -ENOMEM;
+
+		ret = firmware_request_nowarn(&context->firmware,
+					      fw_path,
+					      &context->mhi_dev->dev);
+		kfree(fw_path);
+
+		if (!ret) {
+			context->active_image_id = image_id;
+			return 0;
+		}
+	}
+
 	/*
 	 * This image might be optional. The device may continue without it.
 	 * Only the device knows. Suppress error messages that could suggest an
@@ -235,7 +257,8 @@ static int sahara_find_image(struct sahara_context *context, u32 image_id)
 				      context->image_table[image_id],
 				      &context->mhi_dev->dev);
 	if (ret) {
-		dev_dbg(&context->mhi_dev->dev, "request for image id %d / file %s failed %d\n",
+		dev_dbg(&context->mhi_dev->dev,
+			"request for image id %d / file %s failed %d\n",
 			image_id, context->image_table[image_id], ret);
 		return ret;
 	}
Signature · 1 line
2.34.1
#20 lore raw

Re: [PATCH v6 5/7] bus: mhi: Load DDR training data using device serial number

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:

Pre-existing issues:
- [Critical] On 32-bit systems, dump_length is truncated during vzalloc, leading to a massive heap buffer overflow when parsing memory dumps.
- [High] Lack of synchronization between fw_work and read_data_work allows a malicious or misbehaving device to trigger a Use-After-Free of firmware data.
- [High] Missing cancel_work_sync for read_data_work during driver removal leads to a Use-After-Free.
--

commit 089e41c25a9507e0cb3ce33fb65d0e86f16f5c04
Author: Kishore Batta <[email protected]>

bus: mhi: Load DDR training data using device serial number

This commit extends the firmware loading logic to first attempt loading a per-device DDR training image derived from the device's serial number. It falls back to the default image if the per-device one is missing.

Quoted 4 lines

drivers/bus/mhi/host/clients/sahara/sahara.c static int sahara_find_image(struct sahara_context *context, u32 image_id) · line 236

Quoted 10 lines
drivers/bus/mhi/host/clients/sahara/sahara.c +7 -0
@@ -226,6 +227,27 @@ static int sahara_find_image(struct sahara_context *context, u32 image_id)
 		return 0;
 	}
 
+	/* DDR training special case: Try per-serial number file first */
+	if (image_id == SAHARA_DDR_TRAINING_IMG_ID && context->fw_folder) {
+		u32 serial_num = context->mhi_dev->mhi_cntrl->serial_number;
+
+		fw_path = kasprintf(GFP_KERNEL,
+				    "qcom/%s/mdmddr_0x%x.mbn",
+				    context->fw_folder, serial_num);

[ ... ]

While reviewing this driver, I noticed a few pre-existing issues in the same file that might need attention.

[Severity: High]
This is a pre-existing issue, but is there a lack of synchronization between fw_work and read_data_work that could allow a use-after-free of the firmware data?

If a compromised device sends an end of image packet while streaming data, fw_work executes sahara_end_of_image() and calls sahara_release_image(), which frees the firmware:

drivers/bus/mhi/host/clients/sahara/sahara.c:sahara_release_image() {
    ...
    release_firmware(context->firmware);
    ...
}

Concurrently, read_data_work streams data using read_data_helper(), which copies from the freed context->firmware->data:

drivers/bus/mhi/host/clients/sahara/sahara.c:read_data_helper() {
    ...
    memcpy(context->tx[buf_index],
           &context->firmware->data[context->read_data_offset],
           pkt_data_len);
    ...
}

Could this lead to memory corruption if a malicious device sends out-of-order protocol packets?

[Severity: High]
This is a pre-existing issue, but does sahara_mhi_remove() fail to cancel read_data_work before the context is freed?

drivers/bus/mhi/host/clients/sahara/sahara.c:sahara_mhi_remove() {
    ...
    sahara_release_image(context);
    mhi_unprepare_from_transfer(mhi_dev);
    ...
}

Since read_data_work is not canceled with cancel_work_sync(), can it continue to execute concurrently after the device is removed?

The context structure is allocated with devm_kzalloc and will be freed when the remove function returns, potentially resulting in a use-after-free if the driver is unbound or the device is removed while streaming data.

[Severity: Critical]
This is a pre-existing issue, but on 32-bit systems, does this code truncate dump_length during allocation?

In sahara_parse_dump_table(), dump_length is a 64-bit value representing the total size of memory dumps:

drivers/bus/mhi/host/clients/sahara/sahara.c:sahara_parse_dump_table() {
    ...
    context->mem_dump_sz = dump_length;
    context->mem_dump = vzalloc(dump_length);
    ...
}

Because vzalloc() takes a 32-bit unsigned long on 32-bit systems, a length greater than 4GB will be silently truncated.

However, the driver then proceeds to stream the full 64-bit length of data from the device, copying it into the under-allocated buffer:

drivers/bus/mhi/host/clients/sahara/sahara.c:sahara_parse_dump_image() {
    ...
    memcpy(context->mem_dump_freespace, context->rx, context->rx_size);
    ...
}

Could a compromised MHI peripheral providing a crafted dump table trigger a heap buffer overflow?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/20260701-sahara_protocol_new_v2-v6-0-3a78362c4741@oss.qualcomm.com?part=5
#21 lore raw

Re: [PATCH v6 5/7] bus: mhi: Load DDR training data using device serial number

In reply to Kishore Batta
Quoted 60 lines · Kishore Batta

Devices may provide device specific DDR training data that can be reused across boot to avoid retraining and reduce boot time. The Sahara driver currently always falls back to the default DDR training image, even when serial specific training data is available.

Extend the firmware loading logic for the DDR training image to first attempt loading a per-device image dervied from the device serial number. If the serial-specific image is not present, fall back to the existing default image, preserving current behavior.

This allows reuse of previously generated DDR training data when available, while keeping the existing training flow unchanged for devices without saved data or for all other firmware images.

---
 drivers/bus/mhi/host/clients/sahara/sahara.c | 25 ++++++++++++++++++++++++-
 1 file changed, 24 insertions(+), 1 deletion(-)
drivers/bus/mhi/host/clients/sahara/sahara.c +22 -0
index 9adbd84859073d8024ba2a5fcfa33897439d6759..b5ca6353540dc3815db6539e7424afdb749fd3f6 100644
@@ -59,6 +59,7 @@
 #define SAHARA_RESET_LENGTH		0x8
 #define SAHARA_MEM_DEBUG64_LENGTH	0x18
 #define SAHARA_MEM_READ64_LENGTH	0x18
+#define SAHARA_DDR_TRAINING_IMG_ID	34
 
 struct sahara_packet {
 	__le32 cmd;
@@ -226,6 +227,27 @@ static int sahara_find_image(struct sahara_context *context, u32 image_id)
 		return 0;
 	}
 
+	/* DDR training special case: Try per-serial number file first */
+	if (image_id == SAHARA_DDR_TRAINING_IMG_ID && context->fw_folder) {
+		u32 serial_num = context->mhi_dev->mhi_cntrl->serial_number;
+
+		fw_path = kasprintf(GFP_KERNEL,
+				    "qcom/%s/mdmddr_0x%x.mbn",
+				    context->fw_folder, serial_num);
+		if (!fw_path)
+			return -ENOMEM;
+
+		ret = firmware_request_nowarn(&context->firmware,
+					      fw_path,
+					      &context->mhi_dev->dev);
+		kfree(fw_path);
+
+		if (!ret) {
+			context->active_image_id = image_id;
+			return 0;
+		}
+	}
+
 	/*
 	 * This image might be optional. The device may continue without it.
 	 * Only the device knows. Suppress error messages that could suggest an

drivers/bus/mhi/host/clients/sahara/sahara.c static int sahara_find_image(struct sahara_context *context, u32 image_id) · line 261

Quoted 6 lines
drivers/bus/mhi/host/clients/sahara/sahara.c +2 -1
@@ -235,7 +257,8 @@ static int sahara_find_image(struct sahara_context *context, u32 image_id)
 				      context->image_table[image_id],
 				      &context->mhi_dev->dev);
 	if (ret) {
-		dev_dbg(&context->mhi_dev->dev, "request for image id %d / file %s failed %d\n",
+		dev_dbg(&context->mhi_dev->dev,
+			"request for image id %d / file %s failed %d\n",

Spurious change.

- Mani

Signature · 1 line
மணிவண்ணன் சதாசிவம்
#22 lore raw

Re: [PATCH v6 5/7] bus: mhi: Load DDR training data using device serial number

Quoted 69 lines · Manivannan Sadhasivam
Quoted 60 lines · Kishore Batta

Devices may provide device specific DDR training data that can be reused across boot to avoid retraining and reduce boot time. The Sahara driver currently always falls back to the default DDR training image, even when serial specific training data is available.

Extend the firmware loading logic for the DDR training image to first attempt loading a per-device image dervied from the device serial number. If the serial-specific image is not present, fall back to the existing default image, preserving current behavior.

This allows reuse of previously generated DDR training data when available, while keeping the existing training flow unchanged for devices without saved data or for all other firmware images.

---
  drivers/bus/mhi/host/clients/sahara/sahara.c | 25 ++++++++++++++++++++++++-
  1 file changed, 24 insertions(+), 1 deletion(-)
drivers/bus/mhi/host/clients/sahara/sahara.c +22 -0
index 9adbd84859073d8024ba2a5fcfa33897439d6759..b5ca6353540dc3815db6539e7424afdb749fd3f6 100644
@@ -59,6 +59,7 @@
  #define SAHARA_RESET_LENGTH		0x8
  #define SAHARA_MEM_DEBUG64_LENGTH	0x18
  #define SAHARA_MEM_READ64_LENGTH	0x18
+#define SAHARA_DDR_TRAINING_IMG_ID	34
 
  struct sahara_packet {
  	__le32 cmd;
@@ -226,6 +227,27 @@ static int sahara_find_image(struct sahara_context *context, u32 image_id)
  		return 0;
  	}
 
+	/* DDR training special case: Try per-serial number file first */
+	if (image_id == SAHARA_DDR_TRAINING_IMG_ID && context->fw_folder) {
+		u32 serial_num = context->mhi_dev->mhi_cntrl->serial_number;
+
+		fw_path = kasprintf(GFP_KERNEL,
+				    "qcom/%s/mdmddr_0x%x.mbn",
+				    context->fw_folder, serial_num);
+		if (!fw_path)
+			return -ENOMEM;
+
+		ret = firmware_request_nowarn(&context->firmware,
+					      fw_path,
+					      &context->mhi_dev->dev);
+		kfree(fw_path);
+
+		if (!ret) {
+			context->active_image_id = image_id;
+			return 0;
+		}
+	}
+
  	/*
  	 * This image might be optional. The device may continue without it.
  	 * Only the device knows. Suppress error messages that could suggest an

drivers/bus/mhi/host/clients/sahara/sahara.c static int sahara_find_image(struct sahara_context *context, u32 image_id) · line 261

Quoted 6 lines
drivers/bus/mhi/host/clients/sahara/sahara.c +2 -1
@@ -235,7 +257,8 @@ static int sahara_find_image(struct sahara_context *context, u32 image_id)
  				      context->image_table[image_id],
  				      &context->mhi_dev->dev);
  	if (ret) {
-		dev_dbg(&context->mhi_dev->dev, "request for image id %d / file %s failed %d\n",
+		dev_dbg(&context->mhi_dev->dev,
+			"request for image id %d / file %s failed %d\n",

Spurious change.

Quoted 1 line

- Mani

ACK. Will remove it in next version.

#23 lore raw

Re: [PATCH v6 5/7] bus: mhi: Load DDR training data using device serial number

In reply to Manivannan Sadhasivam
Quoted 69 lines · Manivannan Sadhasivam
Quoted 60 lines · Kishore Batta

Devices may provide device specific DDR training data that can be reused across boot to avoid retraining and reduce boot time. The Sahara driver currently always falls back to the default DDR training image, even when serial specific training data is available.

Extend the firmware loading logic for the DDR training image to first attempt loading a per-device image dervied from the device serial number. If the serial-specific image is not present, fall back to the existing default image, preserving current behavior.

This allows reuse of previously generated DDR training data when available, while keeping the existing training flow unchanged for devices without saved data or for all other firmware images.

---
  drivers/bus/mhi/host/clients/sahara/sahara.c | 25 ++++++++++++++++++++++++-
  1 file changed, 24 insertions(+), 1 deletion(-)
drivers/bus/mhi/host/clients/sahara/sahara.c +22 -0
index 9adbd84859073d8024ba2a5fcfa33897439d6759..b5ca6353540dc3815db6539e7424afdb749fd3f6 100644
@@ -59,6 +59,7 @@
  #define SAHARA_RESET_LENGTH		0x8
  #define SAHARA_MEM_DEBUG64_LENGTH	0x18
  #define SAHARA_MEM_READ64_LENGTH	0x18
+#define SAHARA_DDR_TRAINING_IMG_ID	34
 
  struct sahara_packet {
  	__le32 cmd;
@@ -226,6 +227,27 @@ static int sahara_find_image(struct sahara_context *context, u32 image_id)
  		return 0;
  	}
 
+	/* DDR training special case: Try per-serial number file first */
+	if (image_id == SAHARA_DDR_TRAINING_IMG_ID && context->fw_folder) {
+		u32 serial_num = context->mhi_dev->mhi_cntrl->serial_number;
+
+		fw_path = kasprintf(GFP_KERNEL,
+				    "qcom/%s/mdmddr_0x%x.mbn",
+				    context->fw_folder, serial_num);
+		if (!fw_path)
+			return -ENOMEM;
+
+		ret = firmware_request_nowarn(&context->firmware,
+					      fw_path,
+					      &context->mhi_dev->dev);
+		kfree(fw_path);
+
+		if (!ret) {
+			context->active_image_id = image_id;
+			return 0;
+		}
+	}
+
  	/*
  	 * This image might be optional. The device may continue without it.
  	 * Only the device knows. Suppress error messages that could suggest an

drivers/bus/mhi/host/clients/sahara/sahara.c static int sahara_find_image(struct sahara_context *context, u32 image_id) · line 261

Quoted 6 lines
drivers/bus/mhi/host/clients/sahara/sahara.c +2 -1
@@ -235,7 +257,8 @@ static int sahara_find_image(struct sahara_context *context, u32 image_id)
  				      context->image_table[image_id],
  				      &context->mhi_dev->dev);
  	if (ret) {
-		dev_dbg(&context->mhi_dev->dev, "request for image id %d / file %s failed %d\n",
+		dev_dbg(&context->mhi_dev->dev,
+			"request for image id %d / file %s failed %d\n",

Spurious change.

Quoted 1 line

- Mani

ACK. Will remove in next version.

#24 lore raw

PATCH v6 6/7 bus: mhi: Capture DDR training data via command mode

In reply to Kishore Batta

During early boot, devices may perform DDR training and produce training data that can be reused on subsequent boots to reduce initialization time. The Sahara protocol provides a command mode flow to transfer this training data to the host, but the driver currently does not handle command mode and drops the training payload.

Add Sahara command mode support to retrieve DDR training data from the device. When the device enters command mode and sends CMD_READY, query the support command list(ID 8) and request DDR training data(ID 9) using EXECUTE and EXECUTE_DATA as defined by protocol. Allocate receive buffers based on the reported response size and copy the raw payload directly from the MHI DL completion callback.

Store the captured training data in controller-scoped memory using devres, so it remains available after Sahara channel teardown. Also distinguish raw payload completion from control packets in the DL callback, avoiding misinterpretation of training data as protocol messages, and requeue the RX buffer after switching back to IMAGE_TX_PENDING to allow the boot flow to continue.

---
 drivers/bus/mhi/host/clients/sahara/sahara.c | 326 ++++++++++++++++++++++++++-
 1 file changed, 319 insertions(+), 7 deletions(-)
drivers/bus/mhi/host/clients/sahara/sahara.c +319 -7
index b5ca6353540dc3815db6539e7424afdb749fd3f6..07bc743aa061dd2fa85638067d494562152474e3 100644
@@ -5,11 +5,14 @@
  */
 
 #include <linux/devcoredump.h>
+#include <linux/device.h>
+#include <linux/device/devres.h>
 #include <linux/firmware.h>
 #include <linux/limits.h>
 #include <linux/mhi.h>
 #include <linux/minmax.h>
 #include <linux/mod_devicetable.h>
+#include <linux/mutex.h>
 #include <linux/overflow.h>
 #include <linux/types.h>
 #include <linux/vmalloc.h>
@@ -59,7 +62,15 @@
 #define SAHARA_RESET_LENGTH		0x8
 #define SAHARA_MEM_DEBUG64_LENGTH	0x18
 #define SAHARA_MEM_READ64_LENGTH	0x18
+#define SAHARA_COMMAND_READY_LENGTH	0x8
+#define SAHARA_COMMAND_EXEC_RESP_LENGTH	0x10
+#define SAHARA_COMMAND_EXECUTE_LENGTH	0xc
+#define SAHARA_COMMAND_EXEC_DATA_LENGTH	0xc
+#define SAHARA_SWITCH_MODE_LENGTH	0xc
+#define SAHARA_EXEC_CMD_GET_COMMAND_ID_LIST	0x8
+#define SAHARA_EXEC_CMD_GET_TRAINING_DATA	0x9
 #define SAHARA_DDR_TRAINING_IMG_ID	34
+#define SAHARA_NUM_CMD_BUF		SAHARA_NUM_TX_BUF
 
 struct sahara_packet {
 	__le32 cmd;
@@ -95,6 +106,19 @@ struct sahara_packet {
 			__le64 memory_address;
 			__le64 memory_length;
 		} memory_read64;
+		struct {
+			__le32 client_command;
+		} command_execute;
+		struct {
+			__le32 client_command;
+			__le32 response_length;
+		} command_execute_resp;
+		struct {
+			__le32 client_command;
+		} command_exec_data;
+		struct {
+			__le32 mode;
+		} mode_switch;
 	};
 };
 
@@ -161,6 +185,7 @@ struct sahara_context {
 	struct work_struct		fw_work;
 	struct work_struct		dump_work;
 	struct work_struct		read_data_work;
+	struct work_struct		cmd_work;
 	struct mhi_device		*mhi_dev;
 	const char * const		*image_table;
 	u32				table_size;
@@ -181,6 +206,24 @@ struct sahara_context {
 	bool				is_mem_dump_mode;
 	bool				non_streaming;
 	const char			*fw_folder;
+	bool				is_cmd_mode;
+	bool				receiving_training_data;
+	size_t				training_size;
+	size_t				training_rcvd;
+	u32				training_nbuf;
+	char				*cmd_buff[SAHARA_NUM_CMD_BUF];
+};
+
+/*
+ * Controller-scoped training data store (per MHI controller device).
+ * Stored as devres resource on mhi_dev->mhi_cntrl->mhi_dev->dev.
+ */
+struct sahara_cntrl_training_data {
+	struct mutex lock;	/* Protects data, size, copied and receiving */
+	void *data;
+	size_t size;
+	size_t copied;
+	bool receiving;
 };
 
 static bool is_streaming(struct sahara_context *context)
@@ -188,6 +231,48 @@ static bool is_streaming(struct sahara_context *context)
 	return !context->non_streaming;
 }
 
+static void sahara_cntrl_training_release(struct device *dev, void *res)
+{
+	struct sahara_cntrl_training_data *ct = res;
+
+	mutex_lock(&ct->lock);
+	kfree(ct->data);
+	ct->data = NULL;
+	ct->size = 0;
+	ct->copied = 0;
+	ct->receiving = false;
+	mutex_unlock(&ct->lock);
+}
+
+static int sahara_cntrl_training_match(struct device *dev, void *res, void *match_data)
+{
+	/* Exactly one instance per controller */
+	return 1;
+}
+
+static struct sahara_cntrl_training_data *sahara_cntrl_training_get(struct device *dev)
+{
+	struct sahara_cntrl_training_data *ct;
+
+	ct = devres_find(dev, sahara_cntrl_training_release,
+			 sahara_cntrl_training_match, NULL);
+	if (ct)
+		return ct;
+
+	ct = devres_alloc(sahara_cntrl_training_release, sizeof(*ct), GFP_KERNEL);
+	if (!ct)
+		return NULL;
+
+	mutex_init(&ct->lock);
+	ct->data = NULL;
+	ct->size = 0;
+	ct->copied = 0;
+	ct->receiving = false;
+
+	devres_add(dev, ct);
+	return ct;
+}
+
 static int sahara_find_image(struct sahara_context *context, u32 image_id)
 {
 	char *fw_path;
@@ -282,6 +367,11 @@ static void sahara_send_reset(struct sahara_context *context)
 	context->is_mem_dump_mode = false;
 	context->read_data_offset = 0;
 	context->read_data_length = 0;
+	context->is_cmd_mode = false;
+	context->receiving_training_data = false;
+	context->training_size = 0;
+	context->training_rcvd = 0;
+	context->training_nbuf = 0;
 
 	context->tx[0]->cmd = cpu_to_le32(SAHARA_RESET_CMD);
 	context->tx[0]->length = cpu_to_le32(SAHARA_RESET_LENGTH);
@@ -317,7 +407,8 @@ static void sahara_hello(struct sahara_context *context)
 
 	if (le32_to_cpu(context->rx->hello.mode) != SAHARA_MODE_IMAGE_TX_PENDING &&
 	    le32_to_cpu(context->rx->hello.mode) != SAHARA_MODE_IMAGE_TX_COMPLETE &&
-	    le32_to_cpu(context->rx->hello.mode) != SAHARA_MODE_MEMORY_DEBUG) {
+	    le32_to_cpu(context->rx->hello.mode) != SAHARA_MODE_MEMORY_DEBUG &&
+	    le32_to_cpu(context->rx->hello.mode) != SAHARA_MODE_COMMAND) {
 		dev_err(&context->mhi_dev->dev, "Unsupported hello packet - mode %d\n",
 			le32_to_cpu(context->rx->hello.mode));
 		return;
@@ -336,6 +427,153 @@ static void sahara_hello(struct sahara_context *context)
 		dev_err(&context->mhi_dev->dev, "Unable to send hello response %d\n", ret);
 }
 
+static void sahara_switch_mode_to_img_tx(struct sahara_context *context)
+{
+	int ret;
+
+	context->tx[0]->cmd = cpu_to_le32(SAHARA_SWITCH_MODE_CMD);
+	context->tx[0]->length = cpu_to_le32(SAHARA_SWITCH_MODE_LENGTH);
+	context->tx[0]->mode_switch.mode = cpu_to_le32(SAHARA_MODE_IMAGE_TX_PENDING);
+
+	ret = mhi_queue_buf(context->mhi_dev, DMA_TO_DEVICE, context->tx[0],
+			    SAHARA_SWITCH_MODE_LENGTH, MHI_EOT);
+
+	if (ret)
+		dev_err(&context->mhi_dev->dev, "Unable to send mode switch %d\n", ret);
+}
+
+static void sahara_command_execute(struct sahara_context *context, u32 client_command)
+{
+	int ret;
+
+	context->tx[0]->cmd = cpu_to_le32(SAHARA_EXECUTE_CMD);
+	context->tx[0]->length = cpu_to_le32(SAHARA_COMMAND_EXECUTE_LENGTH);
+	context->tx[0]->command_execute.client_command = cpu_to_le32(client_command);
+
+	ret = mhi_queue_buf(context->mhi_dev, DMA_TO_DEVICE, context->tx[0],
+			    SAHARA_COMMAND_EXECUTE_LENGTH, MHI_EOT);
+	if (ret)
+		dev_err(&context->mhi_dev->dev, "Unable to send command execute %d\n", ret);
+}
+
+static void sahara_command_execute_data(struct sahara_context *context, u32 client_command)
+{
+	int ret;
+
+	context->tx[0]->cmd = cpu_to_le32(SAHARA_EXECUTE_DATA_CMD);
+	context->tx[0]->length = cpu_to_le32(SAHARA_COMMAND_EXEC_DATA_LENGTH);
+	context->tx[0]->command_exec_data.client_command = cpu_to_le32(client_command);
+
+	ret = mhi_queue_buf(context->mhi_dev, DMA_TO_DEVICE, context->tx[0],
+			    SAHARA_COMMAND_EXEC_DATA_LENGTH, MHI_EOT);
+	if (ret)
+		dev_err(&context->mhi_dev->dev, "Unable to send execute data %d\n", ret);
+}
+
+static void sahara_command_ready(struct sahara_context *context)
+{
+	if (le32_to_cpu(context->rx->length) != SAHARA_COMMAND_READY_LENGTH) {
+		dev_err(&context->mhi_dev->dev,
+			"Malformed command ready packet - length %u\n",
+			le32_to_cpu(context->rx->length));
+		return;
+	}
+
+	context->is_cmd_mode = true;
+	context->receiving_training_data = false;
+
+	sahara_command_execute(context, SAHARA_EXEC_CMD_GET_COMMAND_ID_LIST);
+}
+
+static void sahara_command_execute_resp(struct sahara_context *context)
+{
+	struct device *dev = &context->mhi_dev->mhi_cntrl->mhi_dev->dev;
+	struct sahara_cntrl_training_data *ct;
+	u32 client_cmd, resp_len;
+	int ret;
+	u64 remaining;
+	u32 i;
+
+	if (le32_to_cpu(context->rx->length) != SAHARA_COMMAND_EXEC_RESP_LENGTH ||
+	    le32_to_cpu(context->rx->command_execute_resp.response_length) < 0) {
+		dev_err(&context->mhi_dev->dev,
+			"Malformed command execute resp packet - length %d\n",
+			le32_to_cpu(context->rx->length));
+		return;
+	}
+
+	client_cmd = le32_to_cpu(context->rx->command_execute_resp.client_command);
+	resp_len = le32_to_cpu(context->rx->command_execute_resp.response_length);
+
+	sahara_command_execute_data(context, client_cmd);
+
+	if (client_cmd == SAHARA_EXEC_CMD_GET_COMMAND_ID_LIST) {
+		sahara_command_execute(context, SAHARA_EXEC_CMD_GET_TRAINING_DATA);
+		return;
+	}
+
+	if (client_cmd != SAHARA_EXEC_CMD_GET_TRAINING_DATA)
+		return;
+
+	ct = sahara_cntrl_training_get(dev);
+	if (!ct) {
+		context->is_cmd_mode = false;
+		sahara_switch_mode_to_img_tx(context);
+		return;
+	}
+
+	mutex_lock(&ct->lock);
+	kfree(ct->data);
+	ct->data = kzalloc(resp_len, GFP_KERNEL);
+	ct->size = resp_len;
+	ct->copied = 0;
+	ct->receiving = true;
+	mutex_unlock(&ct->lock);
+
+	if (!ct->data) {
+		context->is_cmd_mode = false;
+		sahara_switch_mode_to_img_tx(context);
+		return;
+	}
+
+	context->training_size = resp_len;
+	context->training_rcvd = 0;
+	context->receiving_training_data = true;
+
+	remaining = resp_len;
+	for (i = 0; i < SAHARA_NUM_CMD_BUF && remaining; i++) {
+		size_t pkt = min_t(size_t, remaining, SAHARA_PACKET_MAX_SIZE);
+
+		ret = mhi_queue_buf(context->mhi_dev, DMA_FROM_DEVICE,
+				    context->cmd_buff[i], pkt,
+				    (remaining <= pkt) ? MHI_EOT : MHI_CHAIN);
+		if (ret)
+			break;
+
+		remaining -= pkt;
+	}
+
+	context->training_nbuf = i;
+}
+
+static void sahara_command_processing(struct work_struct *work)
+{
+	struct sahara_context *context = container_of(work, struct sahara_context, cmd_work);
+	int ret;
+
+	if (le32_to_cpu(context->rx->cmd) == SAHARA_EXECUTE_RESP_CMD)
+		sahara_command_execute_resp(context);
+
+	if (!context->receiving_training_data) {
+		ret = mhi_queue_buf(context->mhi_dev, DMA_FROM_DEVICE,
+				    context->rx, SAHARA_PACKET_MAX_SIZE, MHI_EOT);
+
+		if (ret)
+			dev_err(&context->mhi_dev->dev,
+				"Unable to requeue rx buf %d\n", ret);
+	}
+}
+
 static int read_data_helper(struct sahara_context *context, int buf_index)
 {
 	enum mhi_flags mhi_flag;
@@ -562,6 +800,9 @@ static void sahara_processing(struct work_struct *work)
 	case SAHARA_MEM_DEBUG64_CMD:
 		sahara_memory_debug64(context);
 		break;
+	case SAHARA_CMD_READY_CMD:
+		sahara_command_ready(context);
+		break;
 	default:
 		dev_err(&context->mhi_dev->dev, "Unknown command %d\n",
 			le32_to_cpu(context->rx->cmd));
@@ -862,6 +1103,20 @@ static int sahara_mhi_probe(struct mhi_device *mhi_dev, const struct mhi_device_
 	INIT_WORK(&context->fw_work, sahara_processing);
 	INIT_WORK(&context->dump_work, sahara_dump_processing);
 	INIT_WORK(&context->read_data_work, sahara_read_data_processing);
+	INIT_WORK(&context->cmd_work, sahara_command_processing);
+
+	for (i = 0; i < SAHARA_NUM_CMD_BUF; i++) {
+		context->cmd_buff[i] = devm_kzalloc(&mhi_dev->dev,
+						    SAHARA_PACKET_MAX_SIZE, GFP_KERNEL);
+		if (!context->cmd_buff[i])
+			return -ENOMEM;
+	}
+
+	context->is_cmd_mode = false;
+	context->receiving_training_data = false;
+	context->training_size = 0;
+	context->training_rcvd = 0;
+	context->training_nbuf = 0;
 
 	context->active_image_id = SAHARA_IMAGE_ID_NONE;
 	dev_set_drvdata(&mhi_dev->dev, context);
@@ -885,6 +1140,7 @@ static void sahara_mhi_remove(struct mhi_device *mhi_dev)
 
 	cancel_work_sync(&context->fw_work);
 	cancel_work_sync(&context->dump_work);
+	cancel_work_sync(&context->cmd_work);
 	vfree(context->mem_dump);
 	sahara_release_image(context);
 	mhi_unprepare_from_transfer(mhi_dev);
@@ -901,15 +1157,71 @@ static void sahara_mhi_ul_xfer_cb(struct mhi_device *mhi_dev, struct mhi_result
 static void sahara_mhi_dl_xfer_cb(struct mhi_device *mhi_dev, struct mhi_result *mhi_result)
 {
 	struct sahara_context *context = dev_get_drvdata(&mhi_dev->dev);
+	struct sahara_cntrl_training_data *ct;
+	struct device *dev;
+	size_t copy;
+	int ret;
+	u32 i;
+
+	if (mhi_result->transaction_status)
+		return;
+
+	/*
+	 * Raw training payload completions arrive for cmd_buff[] buffers.
+	 * Do not schedule cmd_work for those.
+	 */
+	if (context->is_cmd_mode && context->receiving_training_data &&
+	    mhi_result->buf_addr != context->rx) {
+		dev = &context->mhi_dev->mhi_cntrl->mhi_dev->dev;
+		ct = sahara_cntrl_training_get(dev);
+		if (!ct)
+			return;
 
-	if (!mhi_result->transaction_status) {
-		context->rx_size = mhi_result->bytes_xferd;
-		if (context->is_mem_dump_mode)
-			schedule_work(&context->dump_work);
-		else
-			schedule_work(&context->fw_work);
+		for (i = 0; i < context->training_nbuf; i++) {
+			if (mhi_result->buf_addr == context->cmd_buff[i]) {
+				mutex_lock(&ct->lock);
+				copy = min_t(size_t, mhi_result->bytes_xferd,
+					     ct->size - ct->copied);
+				memcpy((u8 *)ct->data + ct->copied,
+				       mhi_result->buf_addr, copy);
+				ct->copied += copy;
+				mutex_unlock(&ct->lock);
+
+				context->training_rcvd += copy;
+
+				if (context->training_rcvd >= context->training_size) {
+					mutex_lock(&ct->lock);
+					ct->receiving = false;
+					mutex_unlock(&ct->lock);
+
+					context->receiving_training_data = false;
+					context->is_cmd_mode = false;
+
+					sahara_switch_mode_to_img_tx(context);
+					ret = mhi_queue_buf(context->mhi_dev,
+							    DMA_FROM_DEVICE,
+							    context->rx,
+							    SAHARA_PACKET_MAX_SIZE,
+							    MHI_EOT);
+					if (ret)
+						dev_err(&context->mhi_dev->dev,
+							"Unable to requeue rx buf %d\n", ret);
+				}
+				return;
+			}
+		}
+		return;
 	}
 
+	/* Normal Rx completion */
+	context->rx_size = mhi_result->bytes_xferd;
+	if (context->is_mem_dump_mode)
+		schedule_work(&context->dump_work);
+	else if (context->is_cmd_mode)
+		schedule_work(&context->cmd_work);
+	else
+		schedule_work(&context->fw_work);
+
 }
 
 static const struct mhi_device_id sahara_mhi_match_table[] = {
Signature · 1 line
2.34.1
#25 lore raw

Re: [PATCH v6 6/7] bus: mhi: Capture DDR training data via command mode

Thank you for your contribution! Sashiko AI review found 11 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 11 potential issue(s) to consider:

New issues:
- [High] The MHI downlink completion callback acquires a mutex and makes a GFP_KERNEL allocation while in a tasklet context.
- [High] The driver limits the number of queued RX buffers for the training payload but never requeues them, stalling the protocol and creating an incomplete MHI chain.
- [High] Sequential MHI commands concurrently overwrite the shared context->tx[0] buffer before the asynchronous MHI DMA can process them.
- [High] Attaching devres to the parent controller device without explicitly cleaning it up during module unload leads to a use-after-free kernel panic.
- [High] A data race on context->training_nbuf causes the DL callback to miss training buffers, dropping data and re-processing stale control packets.
- [High] Inadequate state tracking risks misinterpreting raw command list data as a protocol packet.
- [High] Concurrent devres allocation can lead to multiple training data structures and memory leaks.
- [Medium] The validation check for negative response length is tautologically false due to an unsigned integer comparison.
- [Medium] A zero-length training data response causes the driver state machine to hang permanently.
- [Medium] The sysfs node exposes uninitialized or zero-padded data to userspace during an active MHI transfer.

Pre-existing issues:
- [High] The driver cancels workqueues before stopping the MHI channel, creating a race condition that leads to a use-after-free. --

commit 3f86759313db40d3845a0cb024dac27dcf18f52b
Author: Kishore Batta <[email protected]>

bus: mhi: Capture DDR training data via command mode

During early boot, devices may perform DDR training and produce training data that can be reused on subsequent boots to reduce initialization time. The Sahara protocol provides a command mode flow to transfer this training data to the host, but the driver currently does not handle command mode and drops the training payload.

Quoted 4 lines
drivers/bus/mhi/host/clients/sahara/sahara.c +0 -0
index b5ca6353540dc..07bc743aa061d 100644

[ ... ]

Quoted 13 lines
drivers/bus/mhi/host/clients/sahara/sahara.c +9 -0
@@ -181,6 +206,24 @@ struct sahara_context {
 	bool				is_mem_dump_mode;
 	bool				non_streaming;
 	const char			*fw_folder;
+	bool				is_cmd_mode;
+	bool				receiving_training_data;
+	size_t				training_size;
+	size_t				training_rcvd;
+	u32				training_nbuf;
+	char				*cmd_buff[SAHARA_NUM_CMD_BUF];
+};
+
+/*

drivers/bus/mhi/host/clients/sahara/sahara.c struct sahara_context { · line 227

Quoted 10 lines
drivers/bus/mhi/host/clients/sahara/sahara.c +9 -0
+ * Controller-scoped training data store (per MHI controller device).
+ * Stored as devres resource on mhi_dev->mhi_cntrl->mhi_dev->dev.
+ */
+struct sahara_cntrl_training_data {
+	struct mutex lock;	/* Protects data, size, copied and receiving */
+	void *data;
+	size_t size;
+	size_t copied;
+	bool receiving;
 };

[Severity: Medium]
Does the sysfs node expose uninitialized or zero-padded data to userspace during an active MHI transfer?

The sysfs read function ddr_training_data_read() serves up to ct->size bytes of ct->data. Because ct->size is set to the full expected response length before the data is actually received, and the read function does not check if the transfer is complete (e.g., via ct->receiving) or limit the copy to ct->copied, a userspace process reading concurrently could receive uninitialized memory.

Quoted 1 line
drivers/bus/mhi/host/clients/sahara/sahara.c +0 -0
 static bool is_streaming(struct sahara_context *context)

[ ... ]

drivers/bus/mhi/host/clients/sahara/sahara.c static bool is_streaming(struct sahara_context *context) · line 262

Quoted 10 lines
drivers/bus/mhi/host/clients/sahara/sahara.c +10 -0
+static struct sahara_cntrl_training_data *sahara_cntrl_training_get(struct device *dev)
+{
+	struct sahara_cntrl_training_data *ct;
+
+	ct = devres_find(dev, sahara_cntrl_training_release,
+			 sahara_cntrl_training_match, NULL);
+	if (ct)
+		return ct;
+
+	ct = devres_alloc(sahara_cntrl_training_release, sizeof(*ct), GFP_KERNEL);

[Severity: High]
Can concurrent calls to this function lead to multiple training data structures and memory leaks?

Because devres_find() and devres_alloc() are used in a check-and-add pattern without locking, if this is called concurrently from the sysfs read callback and the MHI DL callback, both threads could see a NULL result, allocate their own structures, and add them. Would this result in multiple entries in the devres list and inconsistent states?

Quoted 12 lines
drivers/bus/mhi/host/clients/sahara/sahara.c +12 -0
+	if (!ct)
+		return NULL;
+
+	mutex_init(&ct->lock);
+	ct->data = NULL;
+	ct->size = 0;
+	ct->copied = 0;
+	ct->receiving = false;
+
+	devres_add(dev, ct);
+	return ct;
+}

[ ... ]

drivers/bus/mhi/host/clients/sahara/sahara.c static void sahara_hello(struct sahara_context *context) · line 498

Quoted 11 lines
drivers/bus/mhi/host/clients/sahara/sahara.c +11 -0
+static void sahara_command_execute_resp(struct sahara_context *context)
+{
+	struct device *dev = &context->mhi_dev->mhi_cntrl->mhi_dev->dev;
+	struct sahara_cntrl_training_data *ct;
+	u32 client_cmd, resp_len;
+	int ret;
+	u64 remaining;
+	u32 i;
+
+	if (le32_to_cpu(context->rx->length) != SAHARA_COMMAND_EXEC_RESP_LENGTH ||
+	    le32_to_cpu(context->rx->command_execute_resp.response_length) < 0) {

[Severity: Medium]
Is this negative response length check tautologically false?

Since le32_to_cpu() returns an unsigned 32-bit integer, this condition always evaluates to false. Could a malicious or malfunctioning device send a massive length, causing a large kzalloc() attempt to fail?

Quoted 5 lines
drivers/bus/mhi/host/clients/sahara/sahara.c +5 -0
+		dev_err(&context->mhi_dev->dev,
+			"Malformed command execute resp packet - length %d\n",
+			le32_to_cpu(context->rx->length));
+		return;
+	}

drivers/bus/mhi/host/clients/sahara/sahara.c static void sahara_hello(struct sahara_context *context) · line 513

Quoted 10 lines
drivers/bus/mhi/host/clients/sahara/sahara.c +10 -0
+
+	client_cmd = le32_to_cpu(context->rx->command_execute_resp.client_command);
+	resp_len = le32_to_cpu(context->rx->command_execute_resp.response_length);
+
+	sahara_command_execute_data(context, client_cmd);
+
+	if (client_cmd == SAHARA_EXEC_CMD_GET_COMMAND_ID_LIST) {
+		sahara_command_execute(context, SAHARA_EXEC_CMD_GET_TRAINING_DATA);
+		return;
+	}

[Severity: High]
Will these sequential commands overwrite the shared buffer before transmission?

sahara_command_execute_data() populates context->tx[0] and queues it via mhi_queue_buf() which schedules an asynchronous DMA transfer. Immediately afterward, sahara_command_execute() overwrites the exact same context->tx[0] buffer. Does this corrupt the outgoing protocol messages and queue the same virtual address into the MHI ring twice?

[Severity: High]
Is there a risk of misinterpreting raw command list data as a protocol packet?

For SAHARA_EXEC_CMD_GET_COMMAND_ID_LIST, the device streams the command list raw data into context->rx. Since receiving_training_data is only set for ID 9 later in this function, does sahara_command_processing() process the raw command list data as protocol packets?

Quoted 3 lines
drivers/bus/mhi/host/clients/sahara/sahara.c +3 -0
+
+	if (client_cmd != SAHARA_EXEC_CMD_GET_TRAINING_DATA)
+		return;

[ ... ]

drivers/bus/mhi/host/clients/sahara/sahara.c static void sahara_hello(struct sahara_context *context) · line 544

Quoted 6 lines
drivers/bus/mhi/host/clients/sahara/sahara.c +6 -0
+	context->training_size = resp_len;
+	context->training_rcvd = 0;
+	context->receiving_training_data = true;
+
+	remaining = resp_len;
+	for (i = 0; i < SAHARA_NUM_CMD_BUF && remaining; i++) {

[Severity: Medium]
Does a zero-length response cause the driver state machine to hang?

If resp_len is 0, the queuing loop is skipped entirely because remaining is 0. With receiving_training_data set to true, context->rx won't be re-queued in sahara_command_processing(). Does this mean no buffers are queued in hardware, preventing the driver from ever receiving an event to exit command mode?

drivers/bus/mhi/host/clients/sahara/sahara.c static void sahara_hello(struct sahara_context *context) · line 557

Quoted 13 lines
drivers/bus/mhi/host/clients/sahara/sahara.c +13 -0
+		size_t pkt = min_t(size_t, remaining, SAHARA_PACKET_MAX_SIZE);
+
+		ret = mhi_queue_buf(context->mhi_dev, DMA_FROM_DEVICE,
+				    context->cmd_buff[i], pkt,
+				    (remaining <= pkt) ? MHI_EOT : MHI_CHAIN);
+		if (ret)
+			break;
+
+		remaining -= pkt;
+	}
+
+	context->training_nbuf = i;
+}

[Severity: High]
Could a data race on context->training_nbuf cause the DL callback to miss training buffers?

This loop queues cmd_buff receive buffers, but context->training_nbuf is updated only after the loop completes. If the device responds quickly, the DL callback for the first buffer might fire concurrently. Since training_nbuf would still be 0, the callback's loop would skip matching the training buffer and incorrectly fall through to the control packet path.

[ ... ]

drivers/bus/mhi/host/clients/sahara/sahara.c static void sahara_mhi_remove(struct mhi_device *mhi_dev) · line 1146

Quoted 8 lines
drivers/bus/mhi/host/clients/sahara/sahara.c +1 -0
@@ -885,6 +1140,7 @@ static void sahara_mhi_remove(struct mhi_device *mhi_dev)
 
 	cancel_work_sync(&context->fw_work);
 	cancel_work_sync(&context->dump_work);
+	cancel_work_sync(&context->cmd_work);
 	vfree(context->mem_dump);
 	sahara_release_image(context);
 	mhi_unprepare_from_transfer(mhi_dev);

[Severity: High]
This is a pre-existing issue, but does the inverted cleanup order here create a race condition leading to a use-after-free?

The driver synchronously cancels all works before calling mhi_unprepare_from_transfer(mhi_dev). Because the MHI channel is still active, an incoming DL completion can trigger the DL callback and reschedule a work item after the cancellation. When module removal completes, context is freed, and the scheduled work will access freed memory.

[Severity: High]
Does the module removal function exit without unregistering the persistent devres structures from the controller?

The driver allocates a devres struct sahara_cntrl_training_data on the MHI controller device to outlive the Sahara channel. However, the release callbacks are defined in the sahara module. If sahara_mhi_remove() does not remove these controller-bound resources, will the kernel attempt to execute the devres callbacks located in freed module memory when the controller is later destroyed?

Quoted 13 lines
drivers/bus/mhi/host/clients/sahara/sahara.c +9 -0
@@ -901,15 +1157,71 @@ static void sahara_mhi_ul_xfer_cb(struct mhi_device *mhi_dev, struct mhi_result
 static void sahara_mhi_dl_xfer_cb(struct mhi_device *mhi_dev, struct mhi_result *mhi_result)
 {
 	struct sahara_context *context = dev_get_drvdata(&mhi_dev->dev);
+	struct sahara_cntrl_training_data *ct;
+	struct device *dev;
+	size_t copy;
+	int ret;
+	u32 i;
+
+	if (mhi_result->transaction_status)
+		return;
+

drivers/bus/mhi/host/clients/sahara/sahara.c static void sahara_mhi_ul_xfer_cb(struct mhi_device *mhi_dev, struct mhi_result · line 1178

Quoted 10 lines
drivers/bus/mhi/host/clients/sahara/sahara.c +10 -0
+	/*
+	 * Raw training payload completions arrive for cmd_buff[] buffers.
+	 * Do not schedule cmd_work for those.
+	 */
+	if (context->is_cmd_mode && context->receiving_training_data &&
+	    mhi_result->buf_addr != context->rx) {
+		dev = &context->mhi_dev->mhi_cntrl->mhi_dev->dev;
+		ct = sahara_cntrl_training_get(dev);
+		if (!ct)
+			return;

[Severity: High]
Does this callback acquire a sleeping lock and perform blocking memory allocation while in an atomic context?

sahara_mhi_dl_xfer_cb() is executed by the MHI core in an atomic context (typically a tasklet). However, it calls sahara_cntrl_training_get(dev) which allocates memory with GFP_KERNEL, and directly calls mutex_lock(&ct->lock) below. Will this trigger a 'BUG: scheduling while atomic' kernel panic during normal training data reception?

Quoted 10 lines
drivers/bus/mhi/host/clients/sahara/sahara.c +3 -6
-	if (!mhi_result->transaction_status) {
-		context->rx_size = mhi_result->bytes_xferd;
-		if (context->is_mem_dump_mode)
-			schedule_work(&context->dump_work);
-		else
-			schedule_work(&context->fw_work);
+		for (i = 0; i < context->training_nbuf; i++) {
+			if (mhi_result->buf_addr == context->cmd_buff[i]) {
+				mutex_lock(&ct->lock);

drivers/bus/mhi/host/clients/sahara/sahara.c static void sahara_mhi_ul_xfer_cb(struct mhi_device *mhi_dev, struct mhi_result · line 1192

Quoted 10 lines
drivers/bus/mhi/host/clients/sahara/sahara.c +10 -0
+				copy = min_t(size_t, mhi_result->bytes_xferd,
+					     ct->size - ct->copied);
+				memcpy((u8 *)ct->data + ct->copied,
+				       mhi_result->buf_addr, copy);
+				ct->copied += copy;
+				mutex_unlock(&ct->lock);
+
+				context->training_rcvd += copy;
+
+				if (context->training_rcvd >= context->training_size) {

[Severity: High]
If the response requires more buffers than initially queued, does the transfer stall permanently?

In sahara_command_execute_resp(), the driver queues up to SAHARA_NUM_CMD_BUF buffers. If the response size exceeds the total capacity, the loop finishes with the last buffer incorrectly flagged with MHI_CHAIN. When those buffers complete, this callback accumulates training_rcvd but fails to requeue additional buffers. Will training_rcvd never reach training_size, leaving the MHI channel hung waiting for the chain to complete?

Quoted 6 lines
drivers/bus/mhi/host/clients/sahara/sahara.c +6 -0
+					mutex_lock(&ct->lock);
+					ct->receiving = false;
+					mutex_unlock(&ct->lock);
+
+					context->receiving_training_data = false;
+					context->is_cmd_mode = false;
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/20260701-sahara_protocol_new_v2-v6-0-3a78362c4741@oss.qualcomm.com?part=6
#26 lore raw

PATCH v6 7/7 bus: mhi: Expose DDR training data via controller sysfs

In reply to Kishore Batta

DDR training data captured during Sahara command mode needs to be accessible to userspace so it can be persisted and reused on subsequent boots. Currently, the training data is stored internally in the driver but has no external visibility once the Sahara channel is torn down.

Expose the captured DDR training data via a read-only binary sysfs attribute on the MHI controller device:

/sys/bus/mhi/devices/<mhi_cntrl>/ddr_training_data

The sysfs read callback serves data directly from controller scoped storage and protects access with the controller training data lock. The attribute lifetime is tied to the controller device via devres, allowing the data to remain readable after Sahara channel teardown and ensuring automatic cleanup when controller device is removed.

Userspace flow:
1. For each controller device, userspace reads the ddr_training_data sysfs attribute.
2. If the read returns non-zero data, userspace persists it using a serial specific filename (for example, mdmddr_0x<serial_no>.mbn).
3. On subsequent boots, the Sahara driver attempts to load this serial specific DDR training image before falling back to the default training image, restoring DDR calibration data and avoiding retraining.

Add ABI documentation for the DDR training data sysfs attribute exposed by Sahara MHI driver.

---
 .../ABI/testing/sysfs-bus-mhi-ddr_training_data    | 19 +++++++
 drivers/bus/mhi/host/clients/sahara/sahara.c       | 62 ++++++++++++++++++++++
 2 files changed, 81 insertions(+)
Documentation/ABI/testing/sysfs-bus-mhi-ddr_training_data new file +19 -0
new file mode 100644
index 0000000000000000000000000000000000000000..810b487b5a5fdba133d81255f9879844e3938a10
@@ -0,0 +1,19 @@
+What:                   /sys/bus/mhi/devices/<mhi-cntrl>/ddr_training_data
+
+Date:                   March 2026
+
+Contact:                Kishore Batta <[email protected]>
+
+Description:            Contains the DDR training data for the Qualcomm device
+                        connected. MHI driver populates different controller
+                        nodes for each device. The DDR training data is exposed
+                        to userspace to read and save the training data file to
+                        the filesystem. In the subsequent boot up of the device,
+                        the training data is restored from host to device
+                        optimizing the boot up time of the device.
+
+Usage:                  Example for reading DDR training data:
+                        cat /sys/bus/mhi/devices/mhi0/ddr_training_data
+
+Permissions:            The file permissions are set to 0444 allowing read
+                        access.
drivers/bus/mhi/host/clients/sahara/sahara.c +62 -0

74 lines not on this page · raw message

Signature · 1 line
2.34.1
#27 lore raw

Re: [PATCH v6 7/7] bus: mhi: Expose DDR training data via controller sysfs

Thank you for your contribution! Sashiko AI review found 7 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 7 potential issue(s) to consider:

New issues:
- [Critical] Module lifetime mismatch leads to Use-After-Free for sysfs attribute and devres release callback.
- [High] Devres LIFO teardown ordering causes Use-After-Free during sysfs read.
- [High] Sysfs read callback returns incomplete or zero-padded data during ongoing transfers.

Pre-existing issues:
- [Critical] Module lifetime mismatch leads to Use-After-Free for pre-existing training data devres.
- [High] Sleeping in atomic context during MHI download transfer callback.
- [High] Missing cancellation of read_data_work causes Use-After-Free on removal.
- [Medium] TOCTOU race condition in sahara_cntrl_training_get() causes memory leak. --

commit 35f2a6a6ec964ce87d79329b526efc1e44cf02b8
Author: Kishore Batta <[email protected]>

bus: mhi: Expose DDR training data via controller sysfs

This commit exposes captured DDR training data to userspace via a read-only binary sysfs attribute on the MHI controller device. It adds a sysfs read callback to serve data from the controller-scoped storage and manages the attribute's lifetime using devres.

drivers/bus/mhi/host/clients/sahara/sahara.c static struct sahara_cntrl_training_data *sahara_cntrl_training_get(struct devic · line 274

Quoted 7 lines
drivers/bus/mhi/host/clients/sahara/sahara.c +0 -0
index 07bc743aa061d..72ac751c302a9 100644
@@ -273,6 +273,66 @@ static struct sahara_cntrl_training_data *sahara_cntrl_training_get(struct devic
 	return ct;
 }

[Severity: Critical]
This is a pre-existing issue, but does sahara_cntrl_training_get() have a module lifetime mismatch?

When sahara_cntrl_training_get() allocates and attaches a devres structure to the parent MHI controller device:

drivers/bus/mhi/host/clients/sahara/sahara.c:sahara_cntrl_training_get() {
    ...
    ct = devres_alloc(sahara_cntrl_training_release, sizeof(*ct), GFP_KERNEL);
    ...
    devres_add(dev, ct);
}

If the Sahara module is unloaded, the release callback function pointer sahara_cntrl_training_release becomes dangling. When the controller device is eventually removed, will the driver core execute the dangling devres release function and cause a use-after-free?

[Severity: High]
This is a pre-existing issue, but can sahara_cntrl_training_get() be called from an atomic context?

The MHI subsystem invokes the dl_xfer_cb callback in an atomic SoftIRQ/tasklet context, and it calls sahara_cntrl_training_get():

drivers/bus/mhi/host/clients/sahara/sahara.c:sahara_mhi_dl_xfer_cb() {
    ...
    ct = sahara_cntrl_training_get(dev);
    ...
    mutex_lock(&ct->lock);
}

Since sahara_cntrl_training_get() uses GFP_KERNEL unconditionally, and mutex_lock() can sleep, will this cause a scheduling while atomic panic when DDR training data is received?

[Severity: Medium]
This is a pre-existing issue, but is there a race condition in sahara_cntrl_training_get() when setting up the devres structure?

drivers/bus/mhi/host/clients/sahara/sahara.c:sahara_cntrl_training_get() {
    ...
    ct = devres_find(dev, sahara_cntrl_training_release,
                     sahara_cntrl_training_match, NULL);
    if (ct)
        return ct;

    ct = devres_alloc(sahara_cntrl_training_release, sizeof(*ct), GFP_KERNEL);
    ...
    devres_add(dev, ct);
}

If a sysfs reader and the MHI receive path call this concurrently, couldn't both observe NULL and allocate separate structures? Because devres_find() searches in reverse, the later allocation would shadow the earlier one, causing the earlier one to leak memory and lose its lock/data state.

drivers/bus/mhi/host/clients/sahara/sahara.c static struct sahara_cntrl_training_data *sahara_cntrl_training_get(struct devic · line 288

Quoted 13 lines
drivers/bus/mhi/host/clients/sahara/sahara.c +13 -0
+static ssize_t ddr_training_data_read(struct file *filp, struct kobject *kobj,
+				      const struct bin_attribute *attr, char *buf,
+				      loff_t offset, size_t count)
+{
+	struct device *dev = kobj_to_dev(kobj);
+	struct sahara_cntrl_training_data *ct;
+	size_t available;
+
+	ct = sahara_cntrl_training_get(dev);
+	if (!ct)
+		return -ENODEV;
+
+	mutex_lock(&ct->lock);

[Severity: High]
Can ct be freed while we are trying to acquire the lock?

Devres resources are freed in Last-In-First-Out (LIFO) order. Since the sysfs devres is added during probe, and the training data devres (ct) is added later during transfer, ct is freed first on controller removal, and the sysfs file is removed second.

If userspace reads the sysfs file concurrently with device removal, could the read callback retrieve the ct pointer just before it is freed by devres_release_all, leading to a use-after-free when attempting to acquire mutex_lock(&ct->lock)?

drivers/bus/mhi/host/clients/sahara/sahara.c static struct sahara_cntrl_training_data *sahara_cntrl_training_get(struct devic · line 298

Quoted 9 lines
drivers/bus/mhi/host/clients/sahara/sahara.c +9 -0
+	/* No data yet or offset past end */
+	if (!ct->data || offset >= ct->size) {
+		mutex_unlock(&ct->lock);
+		return 0;
+	}
+
+	available = ct->size - offset;
+	count = min(count, available);
+	memcpy(buf, (u8 *)ct->data + offset, count);

[Severity: High]
Does this correctly bound the read size during an ongoing transfer?

This logic uses ct->size (the expected total size) rather than ct->copied (actual received bytes), and ignores ct->receiving.

Since the buffer is zero-filled by kzalloc(), if userspace reads the file before the transfer completes, won't it successfully read the unwritten portion as zeroes? This could cause the userspace tooling to persist a truncated or corrupted DDR training image.

[ ... ]

Quoted 14 lines
drivers/bus/mhi/host/clients/sahara/sahara.c +14 -0
+static void sahara_sysfs_create(struct mhi_device *mhi_dev)
+{
+	struct device *dev = &mhi_dev->mhi_cntrl->mhi_dev->dev;
+	void *cookie;
+	int ret;
+
+	if (devres_find(dev, sahara_sysfs_devres_release, NULL, NULL))
+		return;
+
+	ret = device_create_bin_file(dev, &bin_attr_ddr_training_data);
+	if (ret) {
+		dev_warn(&mhi_dev->dev,
+			 "Failed to create DDR training sysfs node (%d)\n", ret);
+		return;

drivers/bus/mhi/host/clients/sahara/sahara.c static struct sahara_cntrl_training_data *sahara_cntrl_training_get(struct devic · line 334

Quoted 10 lines
drivers/bus/mhi/host/clients/sahara/sahara.c +10 -0
+	}
+
+	cookie = devres_alloc(sahara_sysfs_devres_release, 1, GFP_KERNEL);
+	if (!cookie) {
+		device_remove_bin_file(dev, &bin_attr_ddr_training_data);
+		return;
+	}
+
+	devres_add(dev, cookie);
+}

[Severity: Critical]
Since sahara is a loadable module, what happens if the module is unloaded?

If the Sahara module is unloaded, the client device unbinds and the module memory is freed. But since dev points to the parent MHI controller device, the sysfs file and devres hook remain active on the controller device.

Will a subsequent userspace read of the sysfs file dereference bin_attr_ddr_training_data and execute ddr_training_data_read in unmapped memory, leading to a use-after-free?

[ ... ]

[Severity: High]
This is a pre-existing issue, but does sahara_mhi_remove() fail to cancel read_data_work?

drivers/bus/mhi/host/clients/sahara/sahara.c:sahara_mhi_remove() {
    ...
    cancel_work_sync(&context->fw_work);
    cancel_work_sync(&context->dump_work);
    cancel_work_sync(&context->cmd_work);
    vfree(context->mem_dump);
    ...
}

The driver initializes context->read_data_work and schedules it asynchronously in sahara_mhi_ul_xfer_cb(). Since cancel_work_sync(&context->read_data_work) is omitted during removal, if the work is scheduled just before device removal completes, will the workqueue execute on the devm-freed context memory?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/20260701-sahara_protocol_new_v2-v6-0-3a78362c4741@oss.qualcomm.com?part=7
#28 lore raw

Re: [PATCH v6 7/7] bus: mhi: Expose DDR training data via controller sysfs

In reply to Kishore Batta
Quoted 8 lines · Kishore Batta

DDR training data captured during Sahara command mode needs to be accessible to userspace so it can be persisted and reused on subsequent boots. Currently, the training data is stored internally in the driver but has no external visibility once the Sahara channel is torn down.

Expose the captured DDR training data via a read-only binary sysfs attribute on the MHI controller device:

Quoted 8 lines

/sys/bus/mhi/devices/<mhi_cntrl>/ddr_training_data

The sysfs read callback serves data directly from controller scoped storage and protects access with the controller training data lock. The attribute lifetime is tied to the controller device via devres, allowing the data to remain readable after Sahara channel teardown and ensuring automatic cleanup when controller device is removed.

If this training data is RO, then what is the use of exposing it to userspace?

- Mani

Quoted 127 lines

Userspace flow:
1. For each controller device, userspace reads the ddr_training_data sysfs attribute.
2. If the read returns non-zero data, userspace persists it using a serial specific filename (for example, mdmddr_0x<serial_no>.mbn).
3. On subsequent boots, the Sahara driver attempts to load this serial specific DDR training image before falling back to the default training image, restoring DDR calibration data and avoiding retraining.

Add ABI documentation for the DDR training data sysfs attribute exposed by Sahara MHI driver.

---
 .../ABI/testing/sysfs-bus-mhi-ddr_training_data    | 19 +++++++
 drivers/bus/mhi/host/clients/sahara/sahara.c       | 62 ++++++++++++++++++++++
 2 files changed, 81 insertions(+)
Documentation/ABI/testing/sysfs-bus-mhi-ddr_training_data new file +19 -0
new file mode 100644
index 0000000000000000000000000000000000000000..810b487b5a5fdba133d81255f9879844e3938a10
@@ -0,0 +1,19 @@
+What:                   /sys/bus/mhi/devices/<mhi-cntrl>/ddr_training_data
+
+Date:                   March 2026
+
+Contact:                Kishore Batta <[email protected]>
+
+Description:            Contains the DDR training data for the Qualcomm device
+                        connected. MHI driver populates different controller
+                        nodes for each device. The DDR training data is exposed
+                        to userspace to read and save the training data file to
+                        the filesystem. In the subsequent boot up of the device,
+                        the training data is restored from host to device
+                        optimizing the boot up time of the device.
+
+Usage:                  Example for reading DDR training data:
+                        cat /sys/bus/mhi/devices/mhi0/ddr_training_data
+
+Permissions:            The file permissions are set to 0444 allowing read
+                        access.
drivers/bus/mhi/host/clients/sahara/sahara.c +62 -0
index 07bc743aa061dd2fa85638067d494562152474e3..72ac751c302a98448b5756c9feb438647bd0ce4b 100644
@@ -273,6 +273,66 @@ static struct sahara_cntrl_training_data *sahara_cntrl_training_get(struct devic
 	return ct;
 }
 
+static ssize_t ddr_training_data_read(struct file *filp, struct kobject *kobj,
+				      const struct bin_attribute *attr, char *buf,
+				      loff_t offset, size_t count)
+{
+	struct device *dev = kobj_to_dev(kobj);
+	struct sahara_cntrl_training_data *ct;
+	size_t available;
+
+	ct = sahara_cntrl_training_get(dev);
+	if (!ct)
+		return -ENODEV;
+
+	mutex_lock(&ct->lock);
+
+	/* No data yet or offset past end */
+	if (!ct->data || offset >= ct->size) {
+		mutex_unlock(&ct->lock);
+		return 0;
+	}
+
+	available = ct->size - offset;
+	count = min(count, available);
+	memcpy(buf, (u8 *)ct->data + offset, count);
+
+	mutex_unlock(&ct->lock);
+
+	return count;
+}
+static BIN_ATTR_RO(ddr_training_data, 0);
+
+static void sahara_sysfs_devres_release(struct device *dev, void *res)
+{
+	device_remove_bin_file(dev, &bin_attr_ddr_training_data);
+}
+
+static void sahara_sysfs_create(struct mhi_device *mhi_dev)
+{
+	struct device *dev = &mhi_dev->mhi_cntrl->mhi_dev->dev;
+	void *cookie;
+	int ret;
+
+	if (devres_find(dev, sahara_sysfs_devres_release, NULL, NULL))
+		return;
+
+	ret = device_create_bin_file(dev, &bin_attr_ddr_training_data);
+	if (ret) {
+		dev_warn(&mhi_dev->dev,
+			 "Failed to create DDR training sysfs node (%d)\n", ret);
+		return;
+	}
+
+	cookie = devres_alloc(sahara_sysfs_devres_release, 1, GFP_KERNEL);
+	if (!cookie) {
+		device_remove_bin_file(dev, &bin_attr_ddr_training_data);
+		return;
+	}
+
+	devres_add(dev, cookie);
+}
+
 static int sahara_find_image(struct sahara_context *context, u32 image_id)
 {
 	char *fw_path;
@@ -1131,6 +1191,8 @@ static int sahara_mhi_probe(struct mhi_device *mhi_dev, const struct mhi_device_
 		return ret;
 	}
 
+	sahara_sysfs_create(mhi_dev);
+
 	return 0;
 }
 
Signature · 2 lines
-- 
2.34.1
Signature · 1 line
மணிவண்ணன் சதாசிவம்
#29 lore raw

Re: [PATCH v6 7/7] bus: mhi: Expose DDR training data via controller sysfs

Quoted 17 lines · Manivannan Sadhasivam
Quoted 8 lines · Kishore Batta

DDR training data captured during Sahara command mode needs to be accessible to userspace so it can be persisted and reused on subsequent boots. Currently, the training data is stored internally in the driver but has no external visibility once the Sahara channel is torn down.

Expose the captured DDR training data via a read-only binary sysfs attribute on the MHI controller device:

Quoted 8 lines

/sys/bus/mhi/devices/<mhi_cntrl>/ddr_training_data

The sysfs read callback serves data directly from controller scoped storage and protects access with the controller training data lock. The attribute lifetime is tied to the controller device via devres, allowing the data to remain readable after Sahara channel teardown and ensuring automatic cleanup when controller device is removed.

Quoted 3 lines

If this training data is RO, then what is the use of exposing it to userspace?

- Mani

The userspace component will read this from sysfs and save it to a file named mdmddr_0x<serial_no>.mbn.

On the device's next boot, Sahara will read the training data file and send it to the device. The DDR driver on the device validates the training data and restores it without running DDR training again.

Quoted 127 lines
Quoted 127 lines

Userspace flow:
1. For each controller device, userspace reads the ddr_training_data sysfs attribute.
2. If the read returns non-zero data, userspace persists it using a

    serial specific filename (for example, mdmddr_0x<serial_no>.mbn).

3. On subsequent boots, the Sahara driver attempts to load this serial specific DDR training image before falling back to the default training image, restoring DDR calibration data and avoiding retraining.

Add ABI documentation for the DDR training data sysfs attribute exposed by Sahara MHI driver.

---
  .../ABI/testing/sysfs-bus-mhi-ddr_training_data    | 19 +++++++
  drivers/bus/mhi/host/clients/sahara/sahara.c       | 62 ++++++++++++++++++++++
  2 files changed, 81 insertions(+)
Documentation/ABI/testing/sysfs-bus-mhi-ddr_training_data new file +19 -0
new file mode 100644
index 0000000000000000000000000000000000000000..810b487b5a5fdba133d81255f9879844e3938a10
@@ -0,0 +1,19 @@
+What:                   /sys/bus/mhi/devices/<mhi-cntrl>/ddr_training_data
+
+Date:                   March 2026
+
+Contact:                Kishore Batta <[email protected]>
+
+Description:            Contains the DDR training data for the Qualcomm device
+                        connected. MHI driver populates different controller
+                        nodes for each device. The DDR training data is exposed
+                        to userspace to read and save the training data file to
+                        the filesystem. In the subsequent boot up of the device,
+                        the training data is restored from host to device
+                        optimizing the boot up time of the device.
+
+Usage:                  Example for reading DDR training data:
+                        cat /sys/bus/mhi/devices/mhi0/ddr_training_data
+
+Permissions:            The file permissions are set to 0444 allowing read
+                        access.
drivers/bus/mhi/host/clients/sahara/sahara.c +62 -0
index 07bc743aa061dd2fa85638067d494562152474e3..72ac751c302a98448b5756c9feb438647bd0ce4b 100644
@@ -273,6 +273,66 @@ static struct sahara_cntrl_training_data *sahara_cntrl_training_get(struct devic
  	return ct;
  }
 
+static ssize_t ddr_training_data_read(struct file *filp, struct kobject *kobj,
+				      const struct bin_attribute *attr, char *buf,
+				      loff_t offset, size_t count)
+{
+	struct device *dev = kobj_to_dev(kobj);
+	struct sahara_cntrl_training_data *ct;
+	size_t available;
+
+	ct = sahara_cntrl_training_get(dev);
+	if (!ct)
+		return -ENODEV;
+
+	mutex_lock(&ct->lock);
+
+	/* No data yet or offset past end */
+	if (!ct->data || offset >= ct->size) {
+		mutex_unlock(&ct->lock);
+		return 0;
+	}
+
+	available = ct->size - offset;
+	count = min(count, available);
+	memcpy(buf, (u8 *)ct->data + offset, count);
+
+	mutex_unlock(&ct->lock);
+
+	return count;
+}
+static BIN_ATTR_RO(ddr_training_data, 0);
+
+static void sahara_sysfs_devres_release(struct device *dev, void *res)
+{
+	device_remove_bin_file(dev, &bin_attr_ddr_training_data);
+}
+
+static void sahara_sysfs_create(struct mhi_device *mhi_dev)
+{
+	struct device *dev = &mhi_dev->mhi_cntrl->mhi_dev->dev;
+	void *cookie;
+	int ret;
+
+	if (devres_find(dev, sahara_sysfs_devres_release, NULL, NULL))
+		return;
+
+	ret = device_create_bin_file(dev, &bin_attr_ddr_training_data);
+	if (ret) {
+		dev_warn(&mhi_dev->dev,
+			 "Failed to create DDR training sysfs node (%d)\n", ret);
+		return;
+	}
+
+	cookie = devres_alloc(sahara_sysfs_devres_release, 1, GFP_KERNEL);
+	if (!cookie) {
+		device_remove_bin_file(dev, &bin_attr_ddr_training_data);
+		return;
+	}
+
+	devres_add(dev, cookie);
+}
+
  static int sahara_find_image(struct sahara_context *context, u32 image_id)
  {
  	char *fw_path;
@@ -1131,6 +1191,8 @@ static int sahara_mhi_probe(struct mhi_device *mhi_dev, const struct mhi_device_
  		return ret;
  	}
 
+	sahara_sysfs_create(mhi_dev);
+
  	return 0;
  }
 
Signature · 2 lines
-- 
2.34.1
#30 lore raw

Re: [PATCH v6 7/7] bus: mhi: Expose DDR training data via controller sysfs

Quoted 19 lines · Kishore Batta
Quoted 17 lines · Manivannan Sadhasivam
Quoted 16 lines · Kishore Batta

DDR training data captured during Sahara command mode needs to be accessible to userspace so it can be persisted and reused on subsequent boots. Currently, the training data is stored internally in the driver but has no external visibility once the Sahara channel is torn down.

Expose the captured DDR training data via a read-only binary sysfs attribute on the MHI controller device:

/sys/bus/mhi/devices/<mhi_cntrl>/ddr_training_data

The sysfs read callback serves data directly from controller scoped storage and protects access with the controller training data lock. The attribute lifetime is tied to the controller device via devres, allowing the data to remain readable after Sahara channel teardown and ensuring automatic cleanup when controller device is removed.

Quoted 7 lines
Quoted 3 lines

If this training data is RO, then what is the use of exposing it to userspace?

- Mani

The userspace component will read this from sysfs and save it to a file named mdmddr_0x<serial_no>.mbn.

You need to mention the exact file name for the read data.

- Mani

Signature · 1 line
மணிவண்ணன் சதாசிவம்
#31 lore raw

Re: [PATCH v6 7/7] bus: mhi: Expose DDR training data via controller sysfs

Quoted 21 lines · Manivannan Sadhasivam
Quoted 20 lines · Kishore Batta
Quoted 19 lines · Manivannan Sadhasivam
Quoted 8 lines · Kishore Batta

DDR training data captured during Sahara command mode needs to be accessible to userspace so it can be persisted and reused on subsequent boots. Currently, the training data is stored internally in the driver but has no external visibility once the Sahara channel is torn down.

Expose the captured DDR training data via a read-only binary sysfs attribute on the MHI controller device:

Quoted 8 lines

/sys/bus/mhi/devices/<mhi_cntrl>/ddr_training_data

The sysfs read callback serves data directly from controller scoped storage and protects access with the controller training data lock. The attribute lifetime is tied to the controller device via devres, allowing the data to remain readable after Sahara channel teardown and ensuring automatic cleanup when controller device is removed.

If this training data is RO, then what is the use of exposing it to userspace?

Quoted 7 lines
Quoted 4 lines
Quoted 1 line

- Mani

The userspace component will read this from sysfs and save it to a file named mdmddr_0x<serial_no>.mbn.

You need to mention the exact file name for the read data.

- Mani

The exact filename used by userspace is mdmddr0x<serial_no>.mbn. For example, for a device with serial number 0x12345678, userspace saves the data as mdmddr_0x12345678.mbn. The file is stored in the corresponding Sahara firmware directory. On the next boot, the driver first requests this serial number specific file and falls back to the default DDR training image if it is unavailable.

#32 lore raw

Re: [PATCH v6 7/7] bus: mhi: Expose DDR training data via controller sysfs

In reply to Manivannan Sadhasivam
Quoted 17 lines · Manivannan Sadhasivam
Quoted 8 lines · Kishore Batta

DDR training data captured during Sahara command mode needs to be accessible to userspace so it can be persisted and reused on subsequent boots. Currently, the training data is stored internally in the driver but has no external visibility once the Sahara channel is torn down.

Expose the captured DDR training data via a read-only binary sysfs attribute on the MHI controller device:

Quoted 8 lines

/sys/bus/mhi/devices/<mhi_cntrl>/ddr_training_data

The sysfs read callback serves data directly from controller scoped storage and protects access with the controller training data lock. The attribute lifetime is tied to the controller device via devres, allowing the data to remain readable after Sahara channel teardown and ensuring automatic cleanup when controller device is removed.

Quoted 3 lines

If this training data is RO, then what is the use of exposing it to userspace?

- Mani

The sysfs attribute is read-only because its purpose is to export the training data captured by the driver to userspace. Userspace reads and persists it as a serial number specific firmware file. During next boot, the Sahara driver loads that file through the firmware framework and transfers it back to the device. The data is therefore not written back through sysfs, making the attribute writable is unnecessary and would allow arbitrary data injection.

Quoted 127 lines
Quoted 127 lines

Userspace flow:
1. For each controller device, userspace reads the ddr_training_data sysfs attribute.
2. If the read returns non-zero data, userspace persists it using a

    serial specific filename (for example, mdmddr_0x<serial_no>.mbn).

3. On subsequent boots, the Sahara driver attempts to load this serial specific DDR training image before falling back to the default training image, restoring DDR calibration data and avoiding retraining.

Add ABI documentation for the DDR training data sysfs attribute exposed by Sahara MHI driver.

---
  .../ABI/testing/sysfs-bus-mhi-ddr_training_data    | 19 +++++++
  drivers/bus/mhi/host/clients/sahara/sahara.c       | 62 ++++++++++++++++++++++
  2 files changed, 81 insertions(+)
Documentation/ABI/testing/sysfs-bus-mhi-ddr_training_data new file +19 -0
new file mode 100644
index 0000000000000000000000000000000000000000..810b487b5a5fdba133d81255f9879844e3938a10
@@ -0,0 +1,19 @@
+What:                   /sys/bus/mhi/devices/<mhi-cntrl>/ddr_training_data
+
+Date:                   March 2026
+
+Contact:                Kishore Batta <[email protected]>
+
+Description:            Contains the DDR training data for the Qualcomm device
+                        connected. MHI driver populates different controller
+                        nodes for each device. The DDR training data is exposed
+                        to userspace to read and save the training data file to
+                        the filesystem. In the subsequent boot up of the device,
+                        the training data is restored from host to device
+                        optimizing the boot up time of the device.
+
+Usage:                  Example for reading DDR training data:
+                        cat /sys/bus/mhi/devices/mhi0/ddr_training_data
+
+Permissions:            The file permissions are set to 0444 allowing read
+                        access.
drivers/bus/mhi/host/clients/sahara/sahara.c +62 -0
index 07bc743aa061dd2fa85638067d494562152474e3..72ac751c302a98448b5756c9feb438647bd0ce4b 100644
@@ -273,6 +273,66 @@ static struct sahara_cntrl_training_data *sahara_cntrl_training_get(struct devic
  	return ct;
  }
 
+static ssize_t ddr_training_data_read(struct file *filp, struct kobject *kobj,
+				      const struct bin_attribute *attr, char *buf,
+				      loff_t offset, size_t count)
+{
+	struct device *dev = kobj_to_dev(kobj);
+	struct sahara_cntrl_training_data *ct;
+	size_t available;
+
+	ct = sahara_cntrl_training_get(dev);
+	if (!ct)
+		return -ENODEV;
+
+	mutex_lock(&ct->lock);
+
+	/* No data yet or offset past end */
+	if (!ct->data || offset >= ct->size) {
+		mutex_unlock(&ct->lock);
+		return 0;
+	}
+
+	available = ct->size - offset;
+	count = min(count, available);
+	memcpy(buf, (u8 *)ct->data + offset, count);
+
+	mutex_unlock(&ct->lock);
+
+	return count;
+}
+static BIN_ATTR_RO(ddr_training_data, 0);
+
+static void sahara_sysfs_devres_release(struct device *dev, void *res)
+{
+	device_remove_bin_file(dev, &bin_attr_ddr_training_data);
+}
+
+static void sahara_sysfs_create(struct mhi_device *mhi_dev)
+{
+	struct device *dev = &mhi_dev->mhi_cntrl->mhi_dev->dev;
+	void *cookie;
+	int ret;
+
+	if (devres_find(dev, sahara_sysfs_devres_release, NULL, NULL))
+		return;
+
+	ret = device_create_bin_file(dev, &bin_attr_ddr_training_data);
+	if (ret) {
+		dev_warn(&mhi_dev->dev,
+			 "Failed to create DDR training sysfs node (%d)\n", ret);
+		return;
+	}
+
+	cookie = devres_alloc(sahara_sysfs_devres_release, 1, GFP_KERNEL);
+	if (!cookie) {
+		device_remove_bin_file(dev, &bin_attr_ddr_training_data);
+		return;
+	}
+
+	devres_add(dev, cookie);
+}
+
  static int sahara_find_image(struct sahara_context *context, u32 image_id)
  {
  	char *fw_path;
@@ -1131,6 +1191,8 @@ static int sahara_mhi_probe(struct mhi_device *mhi_dev, const struct mhi_device_
  		return ret;
  	}
 
+	sahara_sysfs_create(mhi_dev);
+
  	return 0;
  }
 
Signature · 2 lines
-- 
2.34.1
#33 lore raw

Re: [PATCH v6 7/7] bus: mhi: Expose DDR training data via controller sysfs

In reply to Kishore Batta
Quoted 101 lines · Kishore Batta

DDR training data captured during Sahara command mode needs to be accessible to userspace so it can be persisted and reused on subsequent boots. Currently, the training data is stored internally in the driver but has no external visibility once the Sahara channel is torn down.

Expose the captured DDR training data via a read-only binary sysfs attribute on the MHI controller device:

/sys/bus/mhi/devices/<mhi_cntrl>/ddr_training_data

The sysfs read callback serves data directly from controller scoped storage and protects access with the controller training data lock. The attribute lifetime is tied to the controller device via devres, allowing the data to remain readable after Sahara channel teardown and ensuring automatic cleanup when controller device is removed.

Userspace flow:
1. For each controller device, userspace reads the ddr_training_data sysfs attribute.
2. If the read returns non-zero data, userspace persists it using a serial specific filename (for example, mdmddr_0x<serial_no>.mbn).
3. On subsequent boots, the Sahara driver attempts to load this serial specific DDR training image before falling back to the default training image, restoring DDR calibration data and avoiding retraining.

Add ABI documentation for the DDR training data sysfs attribute exposed by Sahara MHI driver.

---
 .../ABI/testing/sysfs-bus-mhi-ddr_training_data    | 19 +++++++
 drivers/bus/mhi/host/clients/sahara/sahara.c       | 62 ++++++++++++++++++++++
 2 files changed, 81 insertions(+)
Documentation/ABI/testing/sysfs-bus-mhi-ddr_training_data new file +19 -0
new file mode 100644
index 0000000000000000000000000000000000000000..810b487b5a5fdba133d81255f9879844e3938a10
@@ -0,0 +1,19 @@
+What:                   /sys/bus/mhi/devices/<mhi-cntrl>/ddr_training_data
+
+Date:                   March 2026
+
+Contact:                Kishore Batta <[email protected]>
+
+Description:            Contains the DDR training data for the Qualcomm device
+                        connected. MHI driver populates different controller
+                        nodes for each device. The DDR training data is exposed
+                        to userspace to read and save the training data file to
+                        the filesystem. In the subsequent boot up of the device,
+                        the training data is restored from host to device
+                        optimizing the boot up time of the device.
+
+Usage:                  Example for reading DDR training data:
+                        cat /sys/bus/mhi/devices/mhi0/ddr_training_data
+
+Permissions:            The file permissions are set to 0444 allowing read
+                        access.
drivers/bus/mhi/host/clients/sahara/sahara.c +34 -0
index 07bc743aa061dd2fa85638067d494562152474e3..72ac751c302a98448b5756c9feb438647bd0ce4b 100644
@@ -273,6 +273,66 @@ static struct sahara_cntrl_training_data *sahara_cntrl_training_get(struct devic
 	return ct;
 }
 
+static ssize_t ddr_training_data_read(struct file *filp, struct kobject *kobj,
+				      const struct bin_attribute *attr, char *buf,
+				      loff_t offset, size_t count)
+{
+	struct device *dev = kobj_to_dev(kobj);
+	struct sahara_cntrl_training_data *ct;
+	size_t available;
+
+	ct = sahara_cntrl_training_get(dev);
+	if (!ct)
+		return -ENODEV;
+
+	mutex_lock(&ct->lock);
+
+	/* No data yet or offset past end */
+	if (!ct->data || offset >= ct->size) {
+		mutex_unlock(&ct->lock);
+		return 0;
+	}
+
+	available = ct->size - offset;
+	count = min(count, available);
+	memcpy(buf, (u8 *)ct->data + offset, count);
+
+	mutex_unlock(&ct->lock);
+
+	return count;
+}
+static BIN_ATTR_RO(ddr_training_data, 0);
+
+static void sahara_sysfs_devres_release(struct device *dev, void *res)
+{
+	device_remove_bin_file(dev, &bin_attr_ddr_training_data);
+}

drivers/bus/mhi/host/clients/sahara/sahara.c static struct sahara_cntrl_training_data *sahara_cntrl_training_get(struct devic · line 319

Quoted 10 lines
drivers/bus/mhi/host/clients/sahara/sahara.c +10 -0
+
+static void sahara_sysfs_create(struct mhi_device *mhi_dev)
+{
+	struct device *dev = &mhi_dev->mhi_cntrl->mhi_dev->dev;
+	void *cookie;
+	int ret;
+
+	if (devres_find(dev, sahara_sysfs_devres_release, NULL, NULL))
+		return;
+

I think I asked this question before, but didn't follow up. This attribute should be tied to the SAHARA channel, not the controller. There is no reason for it to be available when the channel is gone.

- Mani

Signature · 1 line
மணிவண்ணன் சதாசிவம்
#34 lore raw

Re: [PATCH v6 7/7] bus: mhi: Expose DDR training data via controller sysfs

Quoted 116 lines · Manivannan Sadhasivam
Quoted 101 lines · Kishore Batta

DDR training data captured during Sahara command mode needs to be accessible to userspace so it can be persisted and reused on subsequent boots. Currently, the training data is stored internally in the driver but has no external visibility once the Sahara channel is torn down.

Expose the captured DDR training data via a read-only binary sysfs attribute on the MHI controller device:

/sys/bus/mhi/devices/<mhi_cntrl>/ddr_training_data

The sysfs read callback serves data directly from controller scoped storage and protects access with the controller training data lock. The attribute lifetime is tied to the controller device via devres, allowing the data to remain readable after Sahara channel teardown and ensuring automatic cleanup when controller device is removed.

Userspace flow:
1. For each controller device, userspace reads the ddr_training_data sysfs attribute.
2. If the read returns non-zero data, userspace persists it using a

    serial specific filename (for example, mdmddr_0x<serial_no>.mbn).

3. On subsequent boots, the Sahara driver attempts to load this serial specific DDR training image before falling back to the default training image, restoring DDR calibration data and avoiding retraining.

Add ABI documentation for the DDR training data sysfs attribute exposed by Sahara MHI driver.

---
  .../ABI/testing/sysfs-bus-mhi-ddr_training_data    | 19 +++++++
  drivers/bus/mhi/host/clients/sahara/sahara.c       | 62 ++++++++++++++++++++++
  2 files changed, 81 insertions(+)
Documentation/ABI/testing/sysfs-bus-mhi-ddr_training_data new file +19 -0
new file mode 100644
index 0000000000000000000000000000000000000000..810b487b5a5fdba133d81255f9879844e3938a10
@@ -0,0 +1,19 @@
+What:                   /sys/bus/mhi/devices/<mhi-cntrl>/ddr_training_data
+
+Date:                   March 2026
+
+Contact:                Kishore Batta <[email protected]>
+
+Description:            Contains the DDR training data for the Qualcomm device
+                        connected. MHI driver populates different controller
+                        nodes for each device. The DDR training data is exposed
+                        to userspace to read and save the training data file to
+                        the filesystem. In the subsequent boot up of the device,
+                        the training data is restored from host to device
+                        optimizing the boot up time of the device.
+
+Usage:                  Example for reading DDR training data:
+                        cat /sys/bus/mhi/devices/mhi0/ddr_training_data
+
+Permissions:            The file permissions are set to 0444 allowing read
+                        access.
drivers/bus/mhi/host/clients/sahara/sahara.c +34 -0
index 07bc743aa061dd2fa85638067d494562152474e3..72ac751c302a98448b5756c9feb438647bd0ce4b 100644
@@ -273,6 +273,66 @@ static struct sahara_cntrl_training_data *sahara_cntrl_training_get(struct devic
  	return ct;
  }
 
+static ssize_t ddr_training_data_read(struct file *filp, struct kobject *kobj,
+				      const struct bin_attribute *attr, char *buf,
+				      loff_t offset, size_t count)
+{
+	struct device *dev = kobj_to_dev(kobj);
+	struct sahara_cntrl_training_data *ct;
+	size_t available;
+
+	ct = sahara_cntrl_training_get(dev);
+	if (!ct)
+		return -ENODEV;
+
+	mutex_lock(&ct->lock);
+
+	/* No data yet or offset past end */
+	if (!ct->data || offset >= ct->size) {
+		mutex_unlock(&ct->lock);
+		return 0;
+	}
+
+	available = ct->size - offset;
+	count = min(count, available);
+	memcpy(buf, (u8 *)ct->data + offset, count);
+
+	mutex_unlock(&ct->lock);
+
+	return count;
+}
+static BIN_ATTR_RO(ddr_training_data, 0);
+
+static void sahara_sysfs_devres_release(struct device *dev, void *res)
+{
+	device_remove_bin_file(dev, &bin_attr_ddr_training_data);
+}

drivers/bus/mhi/host/clients/sahara/sahara.c static struct sahara_cntrl_training_data *sahara_cntrl_training_get(struct devic · line 319

Quoted 10 lines
drivers/bus/mhi/host/clients/sahara/sahara.c +10 -0
+
+static void sahara_sysfs_create(struct mhi_device *mhi_dev)
+{
+	struct device *dev = &mhi_dev->mhi_cntrl->mhi_dev->dev;
+	void *cookie;
+	int ret;
+
+	if (devres_find(dev, sahara_sysfs_devres_release, NULL, NULL))
+		return;
+

I think I asked this question before, but didn't follow up. This attribute should be tied to the SAHARA channel, not the controller. There is no reason for it to be available when the channel is gone.

Quoted 1 line

- Mani

Sure. Let me clarify. In this flow, the sahara channel is used during boot and is torn down after the image transfer sequence completes. If the sysfs attribute is tied to the Sahara channel, its removal can occur before userspace has an opportunity to read and save the training data. This is why the attribute is intentionally tied to the MHI controller device. The controller remains available after the Sahara channel is removed, allowing userspace to read: /sys/bus/mhi/devices/<mhi_cntrl>/ddr_training_data. This attribute remains empty until valid training data is captured and is removed only when the controller device itself is released. Thus, its controller lifetime is required to preserve the captured data across sahara channel teardown.

#35 lore raw

Re: [PATCH v6 7/7] bus: mhi: Expose DDR training data via controller sysfs

Quoted 119 lines · Kishore Batta
Quoted 115 lines · Manivannan Sadhasivam
Quoted 100 lines · Kishore Batta

DDR training data captured during Sahara command mode needs to be accessible to userspace so it can be persisted and reused on subsequent boots. Currently, the training data is stored internally in the driver but has no external visibility once the Sahara channel is torn down.

Expose the captured DDR training data via a read-only binary sysfs attribute on the MHI controller device:

/sys/bus/mhi/devices/<mhi_cntrl>/ddr_training_data

The sysfs read callback serves data directly from controller scoped storage and protects access with the controller training data lock. The attribute lifetime is tied to the controller device via devres, allowing the data to remain readable after Sahara channel teardown and ensuring automatic cleanup when controller device is removed.

Userspace flow:
1. For each controller device, userspace reads the ddr_training_data sysfs attribute.
2. If the read returns non-zero data, userspace persists it using a

    serial specific filename (for example, mdmddr_0x<serial_no>.mbn).

3. On subsequent boots, the Sahara driver attempts to load this serial specific DDR training image before falling back to the default training image, restoring DDR calibration data and avoiding retraining.

Add ABI documentation for the DDR training data sysfs attribute exposed by Sahara MHI driver.

---
  .../ABI/testing/sysfs-bus-mhi-ddr_training_data    | 19 +++++++
  drivers/bus/mhi/host/clients/sahara/sahara.c       | 62 ++++++++++++++++++++++
  2 files changed, 81 insertions(+)
Documentation/ABI/testing/sysfs-bus-mhi-ddr_training_data new file +19 -0
new file mode 100644
index 0000000000000000000000000000000000000000..810b487b5a5fdba133d81255f9879844e3938a10
@@ -0,0 +1,19 @@
+What:                   /sys/bus/mhi/devices/<mhi-cntrl>/ddr_training_data
+
+Date:                   March 2026
+
+Contact:                Kishore Batta <[email protected]>
+
+Description:            Contains the DDR training data for the Qualcomm device
+                        connected. MHI driver populates different controller
+                        nodes for each device. The DDR training data is exposed
+                        to userspace to read and save the training data file to
+                        the filesystem. In the subsequent boot up of the device,
+                        the training data is restored from host to device
+                        optimizing the boot up time of the device.
+
+Usage:                  Example for reading DDR training data:
+                        cat /sys/bus/mhi/devices/mhi0/ddr_training_data
+
+Permissions:            The file permissions are set to 0444 allowing read
+                        access.
drivers/bus/mhi/host/clients/sahara/sahara.c +34 -0
index 07bc743aa061dd2fa85638067d494562152474e3..72ac751c302a98448b5756c9feb438647bd0ce4b 100644
@@ -273,6 +273,66 @@ static struct sahara_cntrl_training_data *sahara_cntrl_training_get(struct devic
  	return ct;
  }
+static ssize_t ddr_training_data_read(struct file *filp, struct kobject *kobj,
+				      const struct bin_attribute *attr, char *buf,
+				      loff_t offset, size_t count)
+{
+	struct device *dev = kobj_to_dev(kobj);
+	struct sahara_cntrl_training_data *ct;
+	size_t available;
+
+	ct = sahara_cntrl_training_get(dev);
+	if (!ct)
+		return -ENODEV;
+
+	mutex_lock(&ct->lock);
+
+	/* No data yet or offset past end */
+	if (!ct->data || offset >= ct->size) {
+		mutex_unlock(&ct->lock);
+		return 0;
+	}
+
+	available = ct->size - offset;
+	count = min(count, available);
+	memcpy(buf, (u8 *)ct->data + offset, count);
+
+	mutex_unlock(&ct->lock);
+
+	return count;
+}
+static BIN_ATTR_RO(ddr_training_data, 0);
+
+static void sahara_sysfs_devres_release(struct device *dev, void *res)
+{
+	device_remove_bin_file(dev, &bin_attr_ddr_training_data);
+}

drivers/bus/mhi/host/clients/sahara/sahara.c static struct sahara_cntrl_training_data *sahara_cntrl_training_get(struct devic · line 318

Quoted 10 lines
drivers/bus/mhi/host/clients/sahara/sahara.c +10 -0
+
+static void sahara_sysfs_create(struct mhi_device *mhi_dev)
+{
+	struct device *dev = &mhi_dev->mhi_cntrl->mhi_dev->dev;
+	void *cookie;
+	int ret;
+
+	if (devres_find(dev, sahara_sysfs_devres_release, NULL, NULL))
+		return;
+

I think I asked this question before, but didn't follow up. This attribute should be tied to the SAHARA channel, not the controller. There is no reason for it to be available when the channel is gone.

Quoted 1 line

- Mani

Quoted 12 lines

Sure. Let me clarify. In this flow, the sahara channel is used during boot and is torn down after the image transfer sequence completes. If the sysfs attribute is tied to the Sahara channel, its removal can occur before userspace has an opportunity to read and save the training data. This is why the attribute is intentionally tied to the MHI controller device. The controller remains available after the Sahara channel is removed, allowing userspace to read: /sys/bus/mhi/devices/<mhi_cntrl>/ddr_training_data. This attribute remains empty until valid training data is captured and is removed only when the controller device itself is released. Thus, its controller lifetime is required to preserve the captured data across sahara channel teardown.

Ok, fine then.

- Mani