net: wwan: t9xx: Add MediaTek T9XX WWAN driver
Subsystems (5)
v2 cover net: wwan: t9xx: Add MediaTek T9XX WWAN driver
T9XX is the PCIe host device driver for MediaTek's t900 modem. The driver uses the WWAN framework infrastructure to create the following control ports and network interfaces for data transactions.
* /dev/wwan0at0 - Interface that supports AT commands.
* /dev/wwan0mbim0 - Interface conforming to the MBIM protocol.
* wwan0-X - Primary network interface for IP traffic.
The main blocks in the T9XX driver are:
* HW layer - Abstracts the hardware bus operations for the device, and provides generic interfaces for the transaction layer to get the device's information and control the device's behavior. It includes:
* PCIe - Implements probe, removal and interrupt handling.
* MHCCIF (Modem Host Cross-Core Interface) - Provides interrupt channels for bidirectional event notification such as handshake and port enumeration.
* Transaction layer - Implements data transactions for the control plane and the data plane. It includes:
* DPMAIF (Data Plane Modem AP Interface) - Controls the hardware that provides uplink and downlink queues for the data path. The data exchange takes place using circular buffers to share data buffer addresses and metadata to describe the packets.
* CLDMA (Cross Layer DMA) - Manages the hardware used by the port layer to send control messages to the device using MediaTek's CCCI (Cross-Core Communication Interface) protocol.
* TX Services - Dispatch packets from the port layer to the device.
* RX Services - Dispatch packets to the port layer when receiving packets from the device.
* Port layer - Provides control plane and data plane interfaces to userspace. It includes:
* Control Plane - Provides device node interfaces for controlling data transactions.
* Data Plane - Provides network link interfaces wwanX (0, 1, 2...) for IP data transactions.
* Core logic - Contains the core logic to keep the device working. It includes:
* FSM (Finite State Machine) - Monitors the state of the device, and notifies each module when the state changes.
The compilation of the T9XX driver is enabled by the CONFIG_MTK_T9XX and CONFIG_MTK_T9XX_PCI config option which depends on CONFIG_WWAN.
This v2 submission covers the control plane only (patches 1-6). The data plane will follow in a separate series once the control plane is accepted.
---
Changes in v2:
- Split series into control plane (this v2) and data plane (follow-up)
- Patch 1 (Add PCIe core):
- Rename BAR_NUM to MTK_PCI_BAR_NUM for driver prefix consistency
- Replace magic numbers in mtk_pci_setup_atr() with named defines
- Remove redundant ATR register comments, use blank line separators
- Add kernel-doc comments to all non-static functions
- Convert 4 MMIO wrapper functions to static inline in header [sashiko]
- Remove unnecessary unlikely() from IRQ validation paths
- Add irq_cnt == 0 and irq_id < 0 guards in mtk_pci_get_virq_id() [sashiko]
- Initialize hw_bits at declaration for consistency
- Merge same-type variable declarations into single lines
- Add #else/#endif comments for CONFIG_ACPI blocks
- Add newlines in mtk_pci_pldr() for readability
- Move return into default case in mtk_pci_dev_reset()
- Simplify mtk_mhccif_init() error path to use direct returns
- Change -EFAULT to -ENOLINK for PCIe link check failure
- Rename goto label "out" to "log_err" in mtk_pci_probe()
- Wrap long lines to stay within 80 columns
- Fix IRQ vector leak: add pci_free_irq_vectors() on error path [sashiko]
- Fix mtk_pci_remove() ordering: free IRQ before cancel_work_sync [sashiko]
- Fix mtk_pci_pldr() ACPI buffer leak: free first result before second call [sashiko]
- Replace msleep(500) with MTK_PLDR_POWER_OFF_DELAY_MS define
- Remove unused EXT_EVT_H2D_DRM_DISABLE_AP and related register define [sashiko]
- Increase MTK_IRQ_NAME_LEN from 20 to 32 to fix W=1 format-truncation warning [sashiko]
- Patch 2 (Add control plane transaction layer):
- Add kernel-doc comments to mtk_ctrl_init() and mtk_ctrl_exit()
- Change mtk_ctrl_exit() return type from int to void
- Set mdev->ctrl_blk to NULL after freeing in mtk_ctrl_exit() [sashiko]
- Change ctrl_blk from void* to typed struct mtk_ctrl_blk* [sashiko]
- Remove redundant "depends on MTK_T9XX" from MTK_T9XX_PCI Kconfig [sashiko]
- Use mtk_dev_free() instead of devm_kfree() in mtk_pci_probe() error path [sashiko]
- Patch 3 (Add control DMA interface):
- Add @ops kernel-doc parameter for mtk_ctrl_init()
- Rename 'err' to 'ret' consistently throughout the patch
- Reorder variable declarations to follow reverse Christmas tree style
- Change mtk_cldma_txq_free() return type from int to void
- Change mtk_cldma_rxq_free() return type from int to void
- Change mtk_cldma_exit() return type from int to void
- Remove unnecessary zero-initialization of ret in mtk_cldma_start_xfer()
- Remove unnecessary zero-initialization of ret in mtk_cldma_tx()
- Use direct return instead of goto out in mtk_cldma_submit_tx() error paths
- Move software state before HWO flag in mtk_cldma_submit_tx()
- Squash variable declarations in mtk_cldma_check_intr_status()
- Remove unlikely() from validation paths in mtk_cldma_check_ch_cfg()
- Clamp data_recv_len with min_t to prevent skb_over_panic in mtk_cldma_rx_skb_adjust() [sashiko]
- Use READ_ONCE() for HWO flag polling in mtk_cldma_check_rx_req() [sashiko]
- Fix mtk_cldma_rx_done_work() to always unmask interrupt on error path [sashiko]
- Add DMA address guard in mtk_cldma_txq_free() teardown loop [sashiko]
- Add IS_ERR() check for kthread_run() in mtk_ctrl_trb_srv_init() [sashiko]
- Fix queue_info memory leak on validation failure in mtk_pcie_hif_init() [sashiko]
- Handle non-EAGAIN errors in mtk_ctrl_trb_handler() TX path [sashiko]
- Fix 'err' typo to 'ret' in mtk_cldma_txbuf_set() error message
- Remove unused variable mdev in mtk_cldma_rx_check_again() [sashiko]
- Remove unused variables trans and ctrl_blk in mtk_cldma_txq_free() and mtk_cldma_rxq_free() [sashiko]
- Patch 4 (Add control port):
- Add @cfg kernel-doc parameter for mtk_ctrl_init()
- Update mtk_ctrl_init() return description to cover additional error codes
- Fix double list_del in mtk_port_stale_list_grp_cleanup() [sashiko]
- Fix direct mtk_port_trb_free() call to use kref_put() in mtk_port_ch_enable() error path [sashiko]
- Fix direct mtk_port_trb_free() call to use kref_put() in mtk_port_ch_disable() error path [sashiko]
- Add mtk_port_tbl_destroy() in mtk_port_mngr_init() error path to prevent port memory leak [sashiko]
- Change port_ops exit/reset/enable/disable callbacks from int to void
- Move -EIO dispatch comment to where the code was introduced
- Patch 5 (Add FSM thread):
- Add bounds check for rtft_entry in mtk_fsm_parse_hs2_msg() [sashiko]
- Add skb length validation before accessing ctrl_msg_header in mtk_fsm_sap_ctrl_msg_handler() [sashiko]
- Fix skb leak on CTRL_MSG_HS2 mismatch return in mtk_fsm_sap_ctrl_msg_handler() [sashiko]
- Add skb length validation before accessing ctrl_msg_header in mtk_fsm_md_ctrl_msg_handler() [sashiko]
- Replace devm_kzalloc/devm_kfree with kzalloc/kfree for FSM events [sashiko]
- Fix mtk_fsm_evt_submit() to return -ETIMEDOUT on blocking event timeout [sashiko]
- Change FSM kthread from TASK_INTERRUPTIBLE to TASK_UNINTERRUPTIBLE [sashiko]
- Remove unused variable hw_id in mtk_cldma_dev_exit() [sashiko]
- Patch 6 (Add AT & MBIM WWAN ports):
- Use imperative mode in commit message
- Remove unnecessary zero-initialization of ret in mtk_port_copy_data_from()
- Change copy_from_user() error code from -EFAULT to -EINVAL in mtk_port_copy_data_from()
- Return -EINVAL for zero-length write in mtk_port_common_write()
- Change mtk_port_wwan_exit/enable/disable() return type from int to void
- Fix packet_size to account for CCCI header reservation in mtk_port_common_write() [sashiko]
- Fix WWAN tx callbacks to consume skb and return 0 per wwan_port_ops contract [sashiko]
- Fix wwan_create_port() error path: clear ERR_PTR to NULL and call mtk_port_ch_disable() [sashiko]
- Patch 7 (Add maintainers entry): new patch
- Link to v1: https://patch.msgid.link/[email protected]
---
Jack Wu (7):
net: wwan: t9xx: Add PCIe core
net: wwan: t9xx: Add control plane transaction layer
net: wwan: t9xx: Add control DMA interface
net: wwan: t9xx: Add control port
net: wwan: t9xx: Add FSM thread
net: wwan: t9xx: Add AT & MBIM WWAN ports
net: wwan: t9xx: Add maintainers entry
MAINTAINERS | 9 + drivers/net/wwan/Kconfig | 17 + drivers/net/wwan/Makefile | 1 + drivers/net/wwan/t9xx/Makefile | 14 + drivers/net/wwan/t9xx/mtk_ctrl_plane.c | 111 ++ drivers/net/wwan/t9xx/mtk_ctrl_plane.h | 88 ++ drivers/net/wwan/t9xx/mtk_dev.c | 55 + drivers/net/wwan/t9xx/mtk_dev.h | 114 ++ drivers/net/wwan/t9xx/mtk_fsm.c | 948 +++++++++++++++ drivers/net/wwan/t9xx/mtk_fsm.h | 140 +++ drivers/net/wwan/t9xx/mtk_port.c | 968 ++++++++++++++++ drivers/net/wwan/t9xx/mtk_port.h | 176 +++ drivers/net/wwan/t9xx/mtk_port_io.c | 573 +++++++++ drivers/net/wwan/t9xx/mtk_port_io.h | 41 + drivers/net/wwan/t9xx/mtk_utility.h | 33 + drivers/net/wwan/t9xx/pcie/Makefile | 15 + drivers/net/wwan/t9xx/pcie/mtk_cldma.c | 1411 +++++++++++++++++++++++ drivers/net/wwan/t9xx/pcie/mtk_cldma.h | 173 +++ drivers/net/wwan/t9xx/pcie/mtk_cldma_drv.c | 371 ++++++ drivers/net/wwan/t9xx/pcie/mtk_cldma_drv.h | 174 +++ drivers/net/wwan/t9xx/pcie/mtk_cldma_drv_m9xx.c | 177 +++ drivers/net/wwan/t9xx/pcie/mtk_cldma_drv_m9xx.h | 101 ++ drivers/net/wwan/t9xx/pcie/mtk_ctrl_cfg_m9xx.c | 55 + drivers/net/wwan/t9xx/pcie/mtk_pci.c | 1114 ++++++++++++++++++ drivers/net/wwan/t9xx/pcie/mtk_pci.h | 232 ++++ drivers/net/wwan/t9xx/pcie/mtk_pci_drv_m9xx.c | 69 ++ drivers/net/wwan/t9xx/pcie/mtk_pci_reg.h | 71 ++ drivers/net/wwan/t9xx/pcie/mtk_trans_ctrl.c | 603 ++++++++++ drivers/net/wwan/t9xx/pcie/mtk_trans_ctrl.h | 105 ++ 29 files changed, 7959 insertions(+)
---
base-commit: eb3f4b7426cfd2b79d65b7d37155480b32259a11
change-id: 20260529-t9xx_driver_v1-1744f8af7739
Best regards,
Signature · 1 line
Jack Wu <[email protected]>
PATCH v2 1/7 net: wwan: t9xx: Add PCIe core
Registers the T900 device driver with the kernel. Set up all the fundamental configurations for the device: PCIe layer, Modem Host Cross Core Interface (MHCCIF), Reset Generation Unit (RGU), modem common control operations and build infrastructure.
* PCIe layer code implements driver probe and removal, MSI-X interrupt initialization and de-initialization, and the way of resetting the device.
* MHCCIF provides interrupt channels to communicate events such as handshake, PM and port enumeration.
* RGU provides interrupt channels to generate notifications from the device so that the T900 driver could get the device reset.
* Modem common control operations provide the basic read/write functions of the device's hardware registers, mask/unmask/get/clear functions of the device's interrupt registers and inquiry functions of the device's status.
--- drivers/net/wwan/Kconfig | 12 + drivers/net/wwan/Makefile | 1 + drivers/net/wwan/t9xx/Makefile | 10 + drivers/net/wwan/t9xx/mtk_dev.h | 108 +++ drivers/net/wwan/t9xx/pcie/mtk_pci.c | 1062 +++++++++++++++++++++++++ drivers/net/wwan/t9xx/pcie/mtk_pci.h | 232 ++++++ drivers/net/wwan/t9xx/pcie/mtk_pci_drv_m9xx.c | 69 ++ drivers/net/wwan/t9xx/pcie/mtk_pci_reg.h | 70 ++ 8 files changed, 1564 insertions(+)
drivers/net/wwan/Kconfig +12 -0
@@ -121,6 +121,18 @@ config MTK_T7XX If unsure, say N. +config MTK_T9XX + tristate "MediaTek PCIe 5G WWAN modem T9xx device" + depends on PCI + select NET_DEVLINK + help + Enables MediaTek PCIe based 5G WWAN modem (T9xx series) device. + + To compile this driver as a module, choose M here: the module will be + called mtk_t9xx. + + If unsure, say N. + endif # WWAN endmenu
drivers/net/wwan/Makefile +1 -0
@@ -14,3 +14,4 @@ obj-$(CONFIG_QCOM_BAM_DMUX) += qcom_bam_dmux.o obj-$(CONFIG_RPMSG_WWAN_CTRL) += rpmsg_wwan_ctrl.o obj-$(CONFIG_IOSM) += iosm/ obj-$(CONFIG_MTK_T7XX) += t7xx/ +obj-$(CONFIG_MTK_T9XX) += t9xx/
drivers/net/wwan/t9xx/Makefile new file +10 -0
@@ -0,0 +1,10 @@ +# SPDX-License-Identifier: GPL-2.0-only + +ccflags-y += -I$(src)/pcie +ccflags-y += -I$(src) + +obj-$(CONFIG_MTK_T9XX) += mtk_t9xx.o + +mtk_t9xx-y := \ + pcie/mtk_pci.o \ + pcie/mtk_pci_drv_m9xx.o
drivers/net/wwan/t9xx/mtk_dev.h new file +108 -0
108 lines not on this page · raw message
drivers/net/wwan/t9xx/pcie/mtk_pci.c new file +1062 -0
1,062 lines not on this page · raw message
drivers/net/wwan/t9xx/pcie/mtk_pci.h new file +232 -0
232 lines not on this page · raw message
drivers/net/wwan/t9xx/pcie/mtk_pci_drv_m9xx.c new file +69 -0
69 lines not on this page · raw message
drivers/net/wwan/t9xx/pcie/mtk_pci_reg.h new file +70 -0
70 lines not on this page · raw message
Signature · 1 line
2.34.1
RE: [External Mail] [PATCH v2 1/7] net: wwan: t9xx: Add PCIe core
Hi Jakub,
Addressing sashiko AI code review comments for this patch, as requested by you in the patch 3/7 review:
https://patchwork.kernel.org/project/netdevbpf/patch/[email protected]/#27006088
Q1: Does this code perform an incorrect double byte-swap on big-endian architectures? The hardware bits are manually swapped using cpu_to_le32() and cast back to u32. This value is later passed to mtk_pci_write32(), which utilizes iowrite32(). Since iowrite32() internally handles host-to-little-endian conversion, swapping the value beforehand will cause a double swap on big-endian platforms.
This driver targets MediaTek T9xx PCIe WWAN modems, which exist exclusively on x86 platforms (little-endian). On little-endian, cpu_to_le32() is a no-op and LE32_TO_U32() is a simple cast, so no byte-swap occurs. The hardware register layout assumes LE host ordering. While the pattern is technically incorrect for big-endian, this hardware is not available on BE platforms and the Kconfig dependency (depends on PCI) combined with the device's PCIe-only nature effectively restricts this to x86/ARM64-LE.
Q2: Does this call to ffs() yield the wrong channel index on big-endian systems? hw_bits has already been endian-swapped in mtk_pci_ext_h2d_evt_hw_bits(). Using ffs() on an endian-swapped value produces a completely incorrect bit index.
Same as Q1 — on little-endian platforms, no swap occurs, so ffs() operates on the correct value. Additionally, all callers of mtk_pci_send_ext_evt() pass known-valid channel values through the is_power_of_2(ch) check, and the SET_HW_BITS mapping covers all valid channels.
Q3: Does clearing the top-level interrupt status at the end of the handler without a subsequent read loop lead to permanently lost hardware events? If a new hardware event triggers during the worker's execution, clearing the write-1-to-clear (W1C) interrupt status after the hardware event statuses were read at the start will discard the newly asserted events.
No events are lost. mtk_pci_clear_irq() clears the MSI-X interrupt status register (BIT(irq_id) in REG_MSIX_ISTATUS_HOST_GRP0_0), NOT the MHCCIF event registers. The MHCCIF events are level-triggered from the modem's EP2RC registers — the modem sets event bits and they remain set until the modem clears them or the host acknowledges.
The flow is:
1. Hardware: modem sets EP2RC event bits -> MSI-X interrupt fires
2. ISR: masks MSI-X bit, schedules work
3. Worker: reads EP2RC event status, dispatches callbacks
4. Worker: clears MSI-X status (BIT(irq_id)), unmasks MSI-X
If a new MHCCIF event arrives during step 3, the EP2RC register gets the new bit set. The MSI-X clear in step 4 only clears the MSI-X pending bit, not the MHCCIF source. After unmask, the MHCCIF event source re-asserts the MSI-X interrupt because the EP2RC bits are still set, triggering a new ISR -> new work -> new read of the updated EP2RC register.
Q4: Does this code incorrectly attempt to unmap a bitmask instead of a single BAR index? MTK_REQUESTED_BARS is defined as a bitmask, but pcim_iounmap_region() expects a specific BAR index. Furthermore, is this manual unmapping redundant since the memory was mapped with pcim_iomap_regions() and will automatically be cleaned up by devres?
Valid on both points. Fixed in v3 by removing mtk_pci_bar_exit() entirely. pcim_iomap_regions() is devres-managed so manual unmap is redundant.
Q5: Does this logic mistakenly drop hardware interrupts when fewer than 32 MSI-X vectors are allocated? If fewer than 32 vectors are allocated (for example, irq_cnt = 1), GENMASK(0, 0) evaluates to BIT(0). A hardware interrupt source like MHCCIF (bit 28) will result in BIT(28) & BIT(0) == 0. Could this lead to a complete loss of critical device events?
No issue. This is the MSI-X merged mode design. When fewer vectors are allocated than interrupt sources, mtk_pci_set_msix_merged() configures the hardware to merge multiple interrupt sources into the available vectors. With irq_cnt=1, the hardware maps ALL interrupt sources to vector 0. The GENMASK filters the irq_state to only the allocated vector bits, then checks if the current handler's vector bit is set. This correctly handles both full allocation (32 vectors, 1:1 mapping) and merged mode (fewer vectors, N:1 mapping).
Q6: Will this manual pci_disable_device() call trigger an atomic underflow? Since the device was initialized using pcim_enable_device(), devres will automatically disable the device during cleanup. Manually calling pci_disable_device() drops the enable_cnt to zero. When devres executes its cleanup, it will call pci_disable_device() again, leading to an underflow and a kernel warning.
Valid. Fixed in v3 by removing pci_disable_device() from both the probe error path and mtk_pci_remove(). pcim_enable_device() handles cleanup via devres.
Q7: Does performing a PCIe Link Down Reset (PLDR) before freeing IRQs cause PCIe Unsupported Request (UR) faults? mtk_pci_pldr() power-cycles the device and clears its BARs. Directly after this, mtk_pci_free_irq() is called, which attempts to access the device's MSI-X MMIO table.
No UR faults. mtk_pci_pldr() calls ACPI PXP._OFF then PXP._ON. The _ON call restores power to the device, bringing the PCIe link back up with BARs accessible. pci_free_irq_vectors() writes to PCI config space (MSI-X capability enable bit) which is always accessible while the device is on the bus. The MSI-X table MMIO access for masking individual entries goes through the re-established BAR mapping.
Q8: Will this manual pci_disable_device() call in mtk_pci_remove() trigger an atomic underflow for the same reason as in mtk_pci_probe()? Is a call to mtk_pci_dev_exit() missing from the remove path?
Two issues, both valid and fixed in v3:
1. pci_disable_device() underflow: same as Q6, removed.
2. Missing mtk_pci_dev_exit(): added in patch 3/7 v3 (where mtk_pci_dev_init is introduced).
Thanks.
================================================================================================================================================================
This message may contain information which is private, privileged or confidential of Compal Electronics, Inc. If you are not the intended recipient of this message, please notify the sender and destroy/delete the message. Any review, retransmission, dissemination or other use of, or taking of any action in reliance upon this information, by persons or entities other than the intended recipient is prohibited.
================================================================================================================================================================
Jakub Kicinski
maintainerRe: [External Mail] [PATCH v2 1/7] net: wwan: t9xx: Add PCIe core
Quoted 3 lines · On Wed, 24 Jun 2026 09:15:17 +0000 Wu. JackBB (GSM) wrote:
================================================================================================================================================================
This message may contain information which is private, privileged or confidential of Compal Electronics, Inc. If you are not the intended recipient of this message, please notify the sender and destroy/delete the message. Any review, retransmission, dissemination or other use of, or taking of any action in reliance upon this information, by persons or entities other than the intended recipient is prohibited.
================================================================================================================================================================
If you want to do anything upstream you have to get rid of this first.
PATCH v2 2/7 net: wwan: t9xx: Add control plane transaction layer
In reply to Jack WuThe control plane implements TX services that reside in the transaction layer. The services receive the packets from the port layer and call the corresponding DMA components to transmit data to the device. Meanwhile, TX services receive and manage the port control commands from the port layer.
The control plane implements RX services that reside in the transaction layer. The services receive the downlink packets from the modem and transfer the packets to the corresponding port layer interfaces.
--- drivers/net/wwan/Kconfig | 5 +++ drivers/net/wwan/t9xx/Makefile | 5 +-- drivers/net/wwan/t9xx/mtk_ctrl_plane.c | 48 +++++++++++++++++++++++++++++ drivers/net/wwan/t9xx/mtk_ctrl_plane.h | 22 +++++++++++++ drivers/net/wwan/t9xx/mtk_dev.c | 44 ++++++++++++++++++++++++++ drivers/net/wwan/t9xx/mtk_dev.h | 5 +++ drivers/net/wwan/t9xx/pcie/Makefile | 10 ++++++ drivers/net/wwan/t9xx/pcie/mtk_pci.c | 10 +++--- drivers/net/wwan/t9xx/pcie/mtk_trans_ctrl.h | 21 +++++++++++++ 9 files changed, 163 insertions(+), 7 deletions(-)
drivers/net/wwan/Kconfig +5 -0
@@ -124,6 +124,7 @@ config MTK_T7XX config MTK_T9XX tristate "MediaTek PCIe 5G WWAN modem T9xx device" depends on PCI + select MTK_T9XX_PCI select NET_DEVLINK help Enables MediaTek PCIe based 5G WWAN modem (T9xx series) device.
@@ -133,6 +134,10 @@ config MTK_T9XX If unsure, say N. +config MTK_T9XX_PCI + tristate + depends on PCI + endif # WWAN endmenu
drivers/net/wwan/t9xx/Makefile +3 -2
@@ -4,7 +4,8 @@ ccflags-y += -I$(src)/pcie ccflags-y += -I$(src) obj-$(CONFIG_MTK_T9XX) += mtk_t9xx.o +obj-$(CONFIG_MTK_T9XX_PCI) += pcie/ mtk_t9xx-y := \ - pcie/mtk_pci.o \ - pcie/mtk_pci_drv_m9xx.o + mtk_dev.o \ + mtk_ctrl_plane.o
drivers/net/wwan/t9xx/mtk_ctrl_plane.c new file +48 -0
48 lines not on this page · raw message
drivers/net/wwan/t9xx/mtk_ctrl_plane.h new file +22 -0
@@ -0,0 +1,22 @@ +/* SPDX-License-Identifier: GPL-2.0-only +* +* Copyright (c) 2022, MediaTek Inc. +*/ + +#ifndef __MTK_CTRL_PLANE_H__ +#define __MTK_CTRL_PLANE_H__ + +#include <linux/kref.h> +#include <linux/skbuff.h> + +#include "mtk_dev.h" + +struct mtk_ctrl_blk { +struct mtk_md_dev *mdev; +struct mtk_ctrl_trans *trans; +}; + +int mtk_ctrl_init (struct mtk_md_dev *mdev); +void mtk_ctrl_exit (struct mtk_md_dev *mdev); + +#endif /* __MTK_CTRL_PLANE_H__ */
drivers/net/wwan/t9xx/mtk_dev.c new file +44 -0
44 lines not on this page · raw message
drivers/net/wwan/t9xx/mtk_dev.h +5 -0
@@ -36,6 +36,7 @@ enum mtk_dev_evt_d2h { };struct mtk_md_dev ; +struct mtk_ctrl_blk ;struct mtk_dev_ops {u32 (*get_dev_state)(struct mtk_md_dev *mdev);
@@ -57,6 +58,7 @@ struct mtk_md_dev {void *hw_priv;u32 hw_ver;char dev_str[MTK_DEV_STR_LEN ]; +struct mtk_ctrl_blk *ctrl_blk; };static inline u32 mtk_dev_get_dev_state (struct mtk_md_dev *mdev)
@@ -105,4 +107,7 @@ static inline int mtk_dev_send_dev_evt(struct mtk_md_dev *mdev, u32 dev_evt) return mdev->dev_ops->send_dev_evt(mdev, dev_evt); } +struct mtk_md_dev *mtk_dev_alloc (struct device *pdev,const struct mtk_dev_ops *dev_ops); +void mtk_dev_free (struct mtk_md_dev *mdev); +#endif /* __MTK_DEV_H__ */
drivers/net/wwan/t9xx/pcie/Makefile new file +10 -0
@@ -0,0 +1,10 @@ +# SPDX-License-Identifier: GPL-2.0-only + +ccflags-y += -I$(src) +ccflags-y += -I$(src)/.. + +obj-$(CONFIG_MTK_T9XX_PCI) += mtk_t9xx_pcie.o + +mtk_t9xx_pcie-y := \ + mtk_pci_drv_m9xx.o \ + mtk_pci.o
drivers/net/wwan/t9xx/pcie/mtk_pci.c +5 -5
@@ -14,6 +14,7 @@#include <linux/module.h> #include "mtk_dev.h" +#include "mtk_trans_ctrl.h" #include "mtk_pci.h" #include "mtk_pci_reg.h"
@@ -469,6 +470,7 @@ static u32 mtk_pci_ext_h2d_evt_hw_bits(u32 chs)SET_HW_BITS (hw_bits, chs,MHCCIF_RC2EP_EVT_DEVICE_RESET ,DEV_EVT_H2D_DEVICE_RESET ); +return LE32_TO_U32 (cpu_to_le32 (hw_bits)); }
@@ -915,13 +917,11 @@ static int mtk_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)struct mtk_md_dev *mdev;int ret; - mdev =devm_kzalloc (dev,sizeof (*mdev),GFP_KERNEL ); + mdev =mtk_dev_alloc (dev, &pci_hw_ops);if (!mdev) { ret = -ENOMEM ; goto log_err; } - mdev->dev_ops = &pci_hw_ops; - mdev->dev = dev; priv =devm_kzalloc (dev,sizeof (*priv),GFP_KERNEL );if (!priv) {
@@ -1002,7 +1002,7 @@ static int mtk_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id) free_priv_data:devm_kfree (dev, priv); free_cntx_data: -devm_kfree (dev, mdev); +mtk_dev_free (mdev); log_err:dev_err (dev,"Failed to probe device, ret=%d\n" , ret);
@@ -1030,7 +1030,7 @@ static void mtk_pci_remove(struct pci_dev *pdev)pci_load_and_free_saved_state (pdev, &priv->saved_state);devm_kfree (dev, priv); -devm_kfree (dev, mdev); +mtk_dev_free (mdev); } static pci_ers_result_t mtk_pci_error_detected(struct pci_dev *pdev,
drivers/net/wwan/t9xx/pcie/mtk_trans_ctrl.h new file +21 -0
@@ -0,0 +1,21 @@ +/* SPDX-License-Identifier: GPL-2.0-only +* +* Copyright (c) 2022, MediaTek Inc. +*/ + +#ifndef __MTK_TRANS_CTRL_H__ +#define __MTK_TRANS_CTRL_H__ + +#include <linux/kref.h> +#include <linux/list.h> +#include <linux/skbuff.h> +#include <linux/types.h> + +#include "mtk_dev.h" + +struct mtk_ctrl_trans { +struct mtk_ctrl_blk *ctrl_blk; +struct mtk_md_dev *mdev; +}; + +#endif
Signature · 1 line
2.34.1
PATCH v2 3/7 net: wwan: t9xx: Add control DMA interface
In reply to Jack WuCross Layer Direct Memory Access(CLDMA) is the hardware interface used by the control plane and designated to translate data between the host and the device. It supports 8 hardware queues for the device AP and modem respectively.
CLDMA driver uses General Purpose Descriptor (GPD) to describe transaction information that can be recognized by CLDMA hardware. Once CLDMA hardware transaction is started, it would fetch and parse GPD to transfer data correctly. To facilitate the CLDMA transaction, a GPD ring for each queue is used. Once the transaction is started, CLDMA hardware will traverse the GPD ring to transfer data between the host and the device until no GPD is available.
CLDMA TX flow:
Once a TX service receives the TX data from the port layer, it uses APIs exported by the CLDMA driver to configure GPD with the DMA address of TX data. After that, the service triggers CLDMA to fetch the first available GPD to transfer data.
CLDMA RX flow:
When there is RX data from the MD, CLDMA hardware asserts an interrupt to notify the host to fetch data and dispatch it to FSM (for handshake messages) or the port layer. After CLDMA opening is finished, All RX GPDs are fulfilled and ready to receive data from the device.
--- drivers/net/wwan/t9xx/mtk_ctrl_plane.c | 4 +- drivers/net/wwan/t9xx/mtk_ctrl_plane.h | 52 +- drivers/net/wwan/t9xx/pcie/Makefile | 7 +- drivers/net/wwan/t9xx/pcie/mtk_cldma.c | 1200 +++++++++++++++++++++++ drivers/net/wwan/t9xx/pcie/mtk_cldma.h | 170 ++++ drivers/net/wwan/t9xx/pcie/mtk_cldma_drv.c | 371 +++++++ drivers/net/wwan/t9xx/pcie/mtk_cldma_drv.h | 177 ++++ drivers/net/wwan/t9xx/pcie/mtk_cldma_drv_m9xx.c | 182 ++++ drivers/net/wwan/t9xx/pcie/mtk_cldma_drv_m9xx.h | 103 ++ drivers/net/wwan/t9xx/pcie/mtk_ctrl_cfg_m9xx.c | 24 + drivers/net/wwan/t9xx/pcie/mtk_pci.c | 38 + drivers/net/wwan/t9xx/pcie/mtk_pci_reg.h | 1 + drivers/net/wwan/t9xx/pcie/mtk_trans_ctrl.c | 583 +++++++++++ drivers/net/wwan/t9xx/pcie/mtk_trans_ctrl.h | 84 ++ 14 files changed, 2992 insertions(+), 4 deletions(-)
drivers/net/wwan/t9xx/mtk_ctrl_plane.c +3 -1
@@ -11,13 +11,14 @@/** * mtk_ctrl_init() - Initialize the control plane block. * @mdev: Pointer to the MTK modem device. +* @ops: HIF operations for the control plane. * * Allocates and initializes the control plane block * associated with @mdev. * * Return: 0 on success, -ENOMEM on allocation failure. */ -int mtk_ctrl_init (struct mtk_md_dev *mdev) +int mtk_ctrl_init (struct mtk_md_dev *mdev,struct mtk_ctrl_hif_ops *ops) {struct mtk_ctrl_blk *ctrl_blk;
@@ -27,6 +28,7 @@ int mtk_ctrl_init(struct mtk_md_dev *mdev) ctrl_blk->mdev = mdev; mdev->ctrl_blk = ctrl_blk; + ctrl_blk->ops = ops;return 0 ; }
drivers/net/wwan/t9xx/mtk_ctrl_plane.h +50 -2
62 lines not on this page · raw message
drivers/net/wwan/t9xx/pcie/Makefile +6 -1
@@ -7,4 +7,9 @@ obj-$(CONFIG_MTK_T9XX_PCI) += mtk_t9xx_pcie.o mtk_t9xx_pcie-y := \ mtk_pci_drv_m9xx.o \ - mtk_pci.o + mtk_cldma_drv_m9xx.o \ + mtk_ctrl_cfg_m9xx.o \ + mtk_pci.o \ + mtk_trans_ctrl.o \ + mtk_cldma.o \ + mtk_cldma_drv.o
drivers/net/wwan/t9xx/pcie/mtk_cldma.c new file +1200 -0
1,200 lines not on this page · raw message
drivers/net/wwan/t9xx/pcie/mtk_cldma.h new file +170 -0
170 lines not on this page · raw message
drivers/net/wwan/t9xx/pcie/mtk_cldma_drv.c new file +371 -0
371 lines not on this page · raw message
drivers/net/wwan/t9xx/pcie/mtk_cldma_drv.h new file +177 -0
177 lines not on this page · raw message
drivers/net/wwan/t9xx/pcie/mtk_cldma_drv_m9xx.c new file +182 -0
182 lines not on this page · raw message
drivers/net/wwan/t9xx/pcie/mtk_cldma_drv_m9xx.h new file +103 -0
103 lines not on this page · raw message
drivers/net/wwan/t9xx/pcie/mtk_ctrl_cfg_m9xx.c new file +24 -0
@@ -0,0 +1,24 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* +* Copyright (c) 2022, MediaTek Inc. +*/ + +#include "mtk_cldma.h" +#include "mtk_trans_ctrl.h" + +#define TRB_SRV_NUM (1) + +static const int mtk_srv_cfg_m9xx[NR_CLDMA ][HW_QUE_NUM ] = { + {0 }, + {0 }, +}; + +static const struct queue_info mtk_queue_info_m9xx[] = { +}; + +struct mtk_ctrl_info mtk_ctrl_info_m9xx = { + .queue_info = (struct queue_info *)mtk_queue_info_m9xx, + .queue_info_num =ARRAY_SIZE (mtk_queue_info_m9xx), + .srv_cfg = (int **)mtk_srv_cfg_m9xx, + .trb_srv_num =TRB_SRV_NUM , +};
drivers/net/wwan/t9xx/pcie/mtk_pci.c +38 -0
@@ -898,6 +898,28 @@ static void mtk_pci_free_irq(struct mtk_md_dev *mdev)pci_free_irq_vectors (pdev); } +static int mtk_pci_dev_init (struct mtk_md_dev *mdev) +{ +int ret; + + ret =mtk_trans_ctrl_init (mdev); +if (ret) { +dev_err (mdev->dev,"Failed to initialize control plane: %d\n" , ret); +return ret; + } + +return 0 ; +} + +static void mtk_pci_dev_exit (struct mtk_md_dev *mdev) +{ +mtk_trans_ctrl_exit (mdev); +} + +static int mtk_pci_dev_start (struct mtk_md_dev *mdev) +{ +return 0 ; +}static const struct mtk_dev_ops pci_hw_ops = { .get_dev_state = mtk_pci_get_dev_state, .ack_dev_state = mtk_pci_ack_dev_state,
@@ -972,6 +994,12 @@ static int mtk_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)if (ret) goto free_mhccif; + ret =mtk_pci_dev_init (mdev); +if (ret) { +dev_err ((mdev)->dev,"Failed to init dev.\n" ); + goto free_irq; + } +pci_set_master (pdev);mtk_pci_unmask_irq (mdev, priv->mhccif_irq_id);
@@ -988,10 +1016,20 @@ static int mtk_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id) goto clear_master; } + ret =mtk_pci_dev_start (mdev); +if (ret) { +dev_err ((mdev)->dev,"Failed to start dev.\n" ); + goto free_saved_state; + } +return 0 ; +free_saved_state: +pci_load_and_free_saved_state (pdev, &priv->saved_state); clear_master:pci_clear_master (pdev); +mtk_pci_dev_exit (mdev); +free_irq:mtk_pci_free_irq (mdev); free_mhccif:mtk_mhccif_exit (mdev);
drivers/net/wwan/t9xx/pcie/mtk_pci_reg.h +1 -0
@@ -21,6 +21,7 @@#define REG_IMASK_HOST_MSIX_SET_GRP0_0 0x3000#define REG_IMASK_HOST_MSIX_CLR_GRP0_0 0x3080#define REG_IMASK_HOST_MSIX_GRP0_0 0x3100 +#define REG_DEV_INFRA_BASE 0x10001000/* mhccif registers */ #define MHCCIF_RC2EP_SW_BSY 0x4
drivers/net/wwan/t9xx/pcie/mtk_trans_ctrl.c new file +583 -0
583 lines not on this page · raw message
drivers/net/wwan/t9xx/pcie/mtk_trans_ctrl.h +84 -0
29 lines not on this page · raw message
The rest of this diff is not shown. View the raw message.
Signature · 1 line
2.34.1
Part of the diff is not shown here. View the raw message.
Jakub Kicinski
maintainerRe: [PATCH v2 3/7] net: wwan: t9xx: Add control DMA interface
Quoted 6 lines · On Wed, 10 Jun 2026 18:41:06 +0800 Jack Wu via B4 Relay wrote:
From: Jack Wu <[email protected]>
Cross Layer Direct Memory Access(CLDMA) is the hardware interface used by the control plane and designated to translate data between the host and the device. It supports 8 hardware queues for the device AP and modem respectively.
Transient build warnings:
+../drivers/net/wwan/t9xx/pcie/mtk_pci_drv_m9xx.c:52:30: warning: symbol 'mtk_dev_cfg_0900' was not declared. Should it be static? +../drivers/net/wwan/t9xx/pcie/mtk_ctrl_cfg_m9xx.c:19:22: warning: symbol 'mtk_ctrl_info_m9xx' was not declared. Should it be static? +../drivers/net/wwan/t9xx/pcie/mtk_cldma_drv_m9xx.c:33:22: warning: symbol 'mtk_cldma_regs_m9xx' was not declared. Should it be static? +../drivers/net/wwan/t9xx/pcie/mtk_cldma_drv_m9xx.c:166:22: warning: symbol 'cldma_drv_ops_m9xx' was not declared. Should it be static?
please also see all the AI code comments at:
https://sashiko.dev/#/patchset/[email protected]
Signature · 1 line
pw-bot: cr
RE: [External Mail] Re: [PATCH v2 3/7] net: wwan: t9xx: Add control DMA interface
Hi Jakub,
Thank you for your comment.
Quoted 6 lines · On Wed, 10 Jun 2026 18:41:06 +0800 Jack Wu via B4 Relay wrote:
Transient build warnings:
+../drivers/net/wwan/t9xx/pcie/mtk_pci_drv_m9xx.c:52:30: warning: symbol 'mtk_dev_cfg_0900' was not declared. Should it be static?+../drivers/net/wwan/t9xx/pcie/mtk_ctrl_cfg_m9xx.c:19:22: warning: symbol 'mtk_ctrl_info_m9xx' was not declared. Should it be static?+../drivers/net/wwan/t9xx/pcie/mtk_cldma_drv_m9xx.c:33:22: warning: symbol 'mtk_cldma_regs_m9xx' was not declared. Should it be static?+../drivers/net/wwan/t9xx/pcie/mtk_cldma_drv_m9xx.c:166:22: warning: symbol 'cldma_drv_ops_m9xx' was not declared. Should it be static?
Will fix in v3. Moved extern declarations into shared headers so each defining .c file includes its own declaration.
Quoted 2 lines
please also see all the AI code comments at:
https://sashiko.dev/#/patchset/[email protected]
Thank you. We have reviewed the AI comments. Below are our responses for the items under this patch (patch 3):
Q1: Is the reference count for the skb incremented correctly here? If the port is non-blocking, the caller might immediately drop the reference. Without an extra reference taken before queueing, the skb could be freed while it still resides in the queue, resulting in a use-after-free when the worker thread later accesses the list.
No use-after-free here. The skb is allocated by the port layer (mtk_port_send_data / mtk_port_common_write) and ownership is transferred to submit_skb — the caller does not free the skb after a successful submission. On error returns (-EINVAL, -EIO, -EAGAIN), the skb is NOT queued and the caller handles cleanup. The skb_queue operations (skb_queue_head/skb_queue_tail) do not require an extra reference because the queue becomes the sole owner. This is the standard kernel pattern — skb ownership transfers to the queue, not shared between caller and queue.
Q2: Could there be a race condition between setting the virtual address and the hardware ownership flag? If the worker checks the condition between these two writes, it might erroneously detect completion and free the buffer before the transfer even starts.
No race. The tx_done_work checks:
if (!req->data_vm_addr || (gpd_flags & CLDMA_GPD_FLAG_HWO))
break; It only proceeds when data_vm_addr is set AND HWO is cleared. In submit_tx, the write order is:
1. req->data_vm_addr = skb->data; (set non-NULL)
2. wmb();
3. gpd_flags |= CLDMA_GPD_FLAG_HWO; (set HWO)
After step 1, data_vm_addr is non-NULL but HWO is set (step 3), so tx_done_work sees HWO=1 and breaks. It will only process this req after the hardware clears HWO upon DMA completion. The wmb() ensures the metadata writes are visible before HWO is set, and the rmb() in tx_done_work ensures HWO is read before the metadata.
Q3: Does this permanently halt the receive queue if a memory allocation fails during refill? If the skb allocation fails earlier, the worker exits here without advancing the free index, which seems to leave the queue stuck indefinitely on all subsequent executions.
The concern is partially valid. If mtk_cldma_reload_rx_skb() fails (skb alloc failure), the code jumps to "out" which unmasks the interrupt. On the next hardware interrupt, rx_done_work runs again and retries from the same free_idx. If req->skb is NULL (because reload failed and rx_done set req->skb = NULL after dispatch), the code hits the "!req->skb" error check and goes to "out" — but "out" now correctly unmasks the interrupt (fixed in v2), so the next interrupt will retry. This creates a retry loop that depends on memory becoming available. Under sustained memory pressure, the RX queue stalls but recovers once memory is freed. This is acceptable behavior for a control plane interface with small, infrequent messages.
Q4: Does this loop safely handle fatal errors from the submission function? It appears that on errors other than EAGAIN, the loop breaks without unlinking the skb. The worker loop would then peek the exact same skb on the next iteration, causing an infinite busy loop that locks up the processor.
The code shown (with kref_put) is from patch 4, not patch 3. We will fix the issue in patch 4 v3.
Q5: Is a read memory barrier needed after checking the hardware ownership flag? Without a barrier, the processor might speculatively read the payload length and data in mtk_cldma_rx_skb_adjust before confirming ownership, leading to potential data corruption.
The GPD descriptors are allocated from a coherent DMA pool (dma_pool_zalloc), so CPU cache coherency is guaranteed by the DMA mapping. A dma_rmb() is not needed here as coherent memory ensures the CPU always observes the latest values written by the device without reordering.
Q6: Should the return value of the queue stop function be checked before tearing down DMA memory? If the hardware fails to stop within the timeout, proceeding to unmap the memory might result in the active hardware writing to freed kernel memory.
The concern is valid in theory but not actionable in practice. cldma_stop_queue() polls the hardware for up to 10 iterations with usleep. If it times out, the hardware is in a broken state (link error or device hang). At this point the teardown is part of device removal or error recovery — there is no safe alternative to proceeding with cleanup. Skipping the teardown would leak DMA memory permanently. The synchronize_irq() + flush_work() after stop_queue ensures no software handlers are in-flight. If the hardware is truly stuck, the PCIe link is likely already down (LINK_ERROR_VAL check), making further MMIO access no-ops.
Q7: Does this leak the radix tree nodes and their contents if the array allocation fails? Performing memory allocations in a teardown path might permanently leak memory during system memory pressure.
We will fix in v3 by replacing kcalloc + radix_tree_gang_lookup with radix_tree_for_each_slot() iteration, eliminating the allocation in the teardown path.
Regarding AI comments for other patches in the series — should we respond to those under each respective patch thread?
Thanks.
================================================================================================================================================================
This message may contain information which is private, privileged or confidential of Compal Electronics, Inc. If you are not the intended recipient of this message, please notify the sender and destroy/delete the message. Any review, retransmission, dissemination or other use of, or taking of any action in reliance upon this information, by persons or entities other than the intended recipient is prohibited.
================================================================================================================================================================
PATCH v2 4/7 net: wwan: t9xx: Add control port
In reply to Jack WuThe control port consists of port I/O and port manager. Port I/O provides a common operation as defined by "struct port_ops", and the operation is managed by the "port manager". It provides interfaces to internal users, the implemented internal interfaces are open, close, write and recv_register.
The port manager defines and implements port management interfaces and structures. It is responsible for port creation, destroying, and managing port states. It sends data from port I/O to CLDMA via TRB ( Transaction Request Block ), and dispatches received data from CLDMA to port I/O. The using port will be held in the "stale list" when the driver destroys it, and after creating it again, the user can continue to use it.
--- drivers/net/wwan/t9xx/Makefile | 4 +- drivers/net/wwan/t9xx/mtk_ctrl_plane.c | 19 +- drivers/net/wwan/t9xx/mtk_ctrl_plane.h | 20 +- drivers/net/wwan/t9xx/mtk_dev.c | 13 +- drivers/net/wwan/t9xx/mtk_port.c | 877 +++++++++++++++++++++++++ drivers/net/wwan/t9xx/mtk_port.h | 159 +++++ drivers/net/wwan/t9xx/mtk_port_io.c | 238 +++++++ drivers/net/wwan/t9xx/mtk_port_io.h | 36 + drivers/net/wwan/t9xx/pcie/mtk_ctrl_cfg_m9xx.c | 25 +- drivers/net/wwan/t9xx/pcie/mtk_pci.c | 2 + drivers/net/wwan/t9xx/pcie/mtk_trans_ctrl.c | 28 +- drivers/net/wwan/t9xx/pcie/mtk_trans_ctrl.h | 1 + 12 files changed, 1406 insertions(+), 16 deletions(-)
drivers/net/wwan/t9xx/Makefile +3 -1
@@ -8,4 +8,6 @@ obj-$(CONFIG_MTK_T9XX_PCI) += pcie/ mtk_t9xx-y := \ mtk_dev.o \ - mtk_ctrl_plane.o + mtk_ctrl_plane.o \ + mtk_port.o \ + mtk_port_io.o
drivers/net/wwan/t9xx/mtk_ctrl_plane.c +17 -2
@@ -7,20 +7,23 @@#include <linux/device.h> #include "mtk_ctrl_plane.h" +#include "mtk_port.h" /** * mtk_ctrl_init() - Initialize the control plane block. * @mdev: Pointer to the MTK modem device. * @ops: HIF operations for the control plane. +* @cfg: Control plane configuration. * * Allocates and initializes the control plane block * associated with @mdev. * -* Return: 0 on success, -ENOMEM on allocation failure. +* Return: 0 on success, negative error code on failure. */ -int mtk_ctrl_init (struct mtk_md_dev *mdev,struct mtk_ctrl_hif_ops *ops) +int mtk_ctrl_init (struct mtk_md_dev *mdev,struct mtk_ctrl_hif_ops *ops,struct mtk_ctrl_cfg *cfg) {struct mtk_ctrl_blk *ctrl_blk; +int err; ctrl_blk =devm_kzalloc (mdev->dev,sizeof (*ctrl_blk),GFP_KERNEL );if (!ctrl_blk)
@@ -29,8 +32,19 @@ int mtk_ctrl_init(struct mtk_md_dev *mdev, struct mtk_ctrl_hif_ops *ops) ctrl_blk->mdev = mdev; mdev->ctrl_blk = ctrl_blk; ctrl_blk->ops = ops; + ctrl_blk->cfg = cfg; + + err =mtk_port_mngr_init (ctrl_blk, cfg->port_layer_cfg->port_cfg, + cfg->port_layer_cfg->port_cnt); +if (err) + goto err_free_mem;return 0 ; + +err_free_mem: +devm_kfree (mdev->dev, ctrl_blk); + +return err; }EXPORT_SYMBOL (mtk_ctrl_init);
@@ -44,6 +58,7 @@ void mtk_ctrl_exit(struct mtk_md_dev *mdev) {struct mtk_ctrl_blk *ctrl_blk = mdev->ctrl_blk; +mtk_port_mngr_exit (ctrl_blk);devm_kfree (mdev->dev, ctrl_blk); mdev->ctrl_blk =NULL ; }
drivers/net/wwan/t9xx/mtk_ctrl_plane.h +18 -2
@@ -11,6 +11,17 @@#include "mtk_dev.h" +#define Q_MTU_2K (0x800) +#define Q_MTU_3_5K (0xE00) +#define Q_MTU_7K (0x1C00) +#define Q_MTU_32K (0x8000) +#define Q_MTU_63K (0xFC00) +#define Q_FRAG_2K (0x800) +#define Q_FRAG_3_5K (0xE00) +#define Q_FRAG_7K (0x1C00) +#define Q_FRAG_32K (0x8000) +#define Q_FRAG_63K (0xFC00) +enum mtk_trb_cmd_type {TRB_CMD_MIN ,TRB_CMD_ENABLE ,
@@ -54,17 +65,22 @@ struct mtk_ctrl_hif_ops { int (*send_cmd)(struct mtk_md_dev *mdev, int cmd, void *data); }; -struct mtk_ctrl_cfg ; +struct mtk_ctrl_cfg { +struct mtk_port_layer_cfg *port_layer_cfg; +}; +struct mtk_ctrl_trans ;struct mtk_ctrl_blk {struct mtk_md_dev *mdev; +struct mtk_port_mngr *port_mngr;struct mtk_ctrl_hif_ops *ops;void *ctrl_hw_priv;struct mtk_ctrl_cfg *cfg; }; -int mtk_ctrl_init (struct mtk_md_dev *mdev,struct mtk_ctrl_hif_ops *ops); +int mtk_ctrl_init (struct mtk_md_dev *mdev,struct mtk_ctrl_hif_ops *ops, +struct mtk_ctrl_cfg *cfg);void mtk_ctrl_exit (struct mtk_md_dev *mdev);#endif /* __MTK_CTRL_PLANE_H__ */
drivers/net/wwan/t9xx/mtk_dev.c +12 -1
@@ -6,6 +6,8 @@#include <linux/module.h> #include "mtk_dev.h" +#include "mtk_port.h" +#include "mtk_port_io.h" struct mtk_md_dev *mtk_dev_alloc (struct device *pdev,const struct mtk_dev_ops *dev_ops) {
@@ -31,12 +33,21 @@ EXPORT_SYMBOL(mtk_dev_free);static int __init mtk_common_drv_init (void ) { -return 0 ; +int ret; + + ret =mtk_port_io_init (); +if (ret) + goto err_init_devid; + +err_init_devid: +return ret; }module_init (mtk_common_drv_init); static void __exit mtk_common_drv_exit(void) { +mtk_port_io_exit (); +mtk_port_stale_list_grp_cleanup (); }module_exit (mtk_common_drv_exit);
drivers/net/wwan/t9xx/mtk_port.c new file +877 -0
877 lines not on this page · raw message
drivers/net/wwan/t9xx/mtk_port.h new file +159 -0
159 lines not on this page · raw message
drivers/net/wwan/t9xx/mtk_port_io.c new file +238 -0
238 lines not on this page · raw message
drivers/net/wwan/t9xx/mtk_port_io.h new file +36 -0
@@ -0,0 +1,36 @@ +/* SPDX-License-Identifier: GPL-2.0-only +* +* Copyright (c) 2022, MediaTek Inc. +*/ + +#ifndef __MTK_PORT_IO_H__ +#define __MTK_PORT_IO_H__ + +#include <linux/skbuff.h> + +#include "mtk_port.h" + +#define MTK_RX_BUF_SIZE (1024 * 1024) + +extern struct mutex port_mngr_grp_mtx; + +struct port_ops { +int (*init)(struct mtk_port *port); +void (*exit)(struct mtk_port *port); +void (*reset)(struct mtk_port *port); +void (*enable)(struct mtk_port *port); +void (*disable)(struct mtk_port *port); +int (*recv)(struct mtk_port *port,struct sk_buff *skb); +}; + +void *mtk_port_internal_open (struct mtk_md_dev *mdev,char *name,int flag); +int mtk_port_internal_close (void *i_port); +int mtk_port_internal_write (void *i_port,struct sk_buff *skb); +void mtk_port_internal_recv_register (void *i_port, +int (*cb)(void *priv,struct sk_buff *skb), +void *arg); + +int mtk_port_io_init (void ); +void mtk_port_io_exit (void ); + +#endif /* __MTK_PORT_IO_H__ */
drivers/net/wwan/t9xx/pcie/mtk_ctrl_cfg_m9xx.c +24 -1
@@ -4,6 +4,7 @@*/ #include "mtk_cldma.h" +#include "mtk_port.h" #include "mtk_trans_ctrl.h" #define TRB_SRV_NUM (1)
@@ -13,12 +14,34 @@ static const int mtk_srv_cfg_m9xx[NR_CLDMA][HW_QUE_NUM] = { {0}, }; +/* the number of RX GPDs should be at last two */ static const struct queue_info mtk_queue_info_m9xx[] = { + {CCCI_CONTROL_TX ,CCCI_CONTROL_RX ,CLDMA1 ,TXQ (0 ),RXQ (0 ), +Q_MTU_3_5K ,Q_MTU_3_5K ,TX_GPD_NUM ,RX_GPD_NUM ,Q_FRAG_3_5K ,Q_FRAG_3_5K ,0 }, + {CCCI_SAP_CONTROL_TX ,CCCI_SAP_CONTROL_RX ,CLDMA0 ,TXQ (0 ),RXQ (0 ), +Q_MTU_3_5K ,Q_MTU_3_5K ,TX_GPD_NUM ,RX_GPD_NUM ,Q_FRAG_3_5K ,Q_FRAG_3_5K ,0 }, +}; + +static const struct mtk_port_cfg port_cfg_m9xx[] = { + {CCCI_CONTROL_TX ,CCCI_CONTROL_RX ,PORT_TYPE_INTERNAL ,"MDCTRL" , +PORT_F_ALLOW_DROP }, + {CCCI_SAP_CONTROL_TX ,CCCI_SAP_CONTROL_RX ,PORT_TYPE_INTERNAL ,"SAPCTRL" , +PORT_F_ALLOW_DROP }, +}; + +static struct mtk_port_layer_cfg port_layer_cfg_m9xx = { + .port_cfg = (struct mtk_port_cfg *)port_cfg_m9xx, + .port_cnt =ARRAY_SIZE (port_cfg_m9xx), +}; + +static struct mtk_ctrl_cfg mtk_ctrl_cfg_m9xx = { + .port_layer_cfg = &port_layer_cfg_m9xx, };struct mtk_ctrl_info mtk_ctrl_info_m9xx = { + .ctrl_cfg = &mtk_ctrl_cfg_m9xx, + .srv_cfg = (int **)mtk_srv_cfg_m9xx, .queue_info = (struct queue_info *)mtk_queue_info_m9xx, .queue_info_num =ARRAY_SIZE (mtk_queue_info_m9xx), - .srv_cfg = (int **)mtk_srv_cfg_m9xx, .trb_srv_num =TRB_SRV_NUM , };
drivers/net/wwan/t9xx/pcie/mtk_pci.c +2 -0
@@ -17,6 +17,8 @@#include "mtk_trans_ctrl.h" #include "mtk_pci.h" #include "mtk_pci_reg.h" +#include "mtk_port.h" +#include "mtk_port_io.h" #define MTK_PCI_BAR_NUM 6#define MTK_PCI_TRANSPARENT_ATR_SIZE (0x3F)
drivers/net/wwan/t9xx/pcie/mtk_trans_ctrl.c +19 -9
@@ -16,13 +16,14 @@#include "mtk_ctrl_plane.h" #include "mtk_dev.h" #include "mtk_pci.h" +#include "mtk_port.h" #include "mtk_trans_ctrl.h" #define MTK_DFLT_PORT_NAME_LEN (20)extern struct mtk_ctrl_info ctrl_info_name (m9xx );static struct mtk_ctrl_info_desc mtk_ctrl_info_tbl[] = { - {2304 , &ctrl_info_name (m9xx)}, + {0x01CA , &ctrl_info_name (m9xx)}, {0 ,NULL }, };
@@ -134,6 +135,7 @@ static void mtk_ctrl_trb_handler(struct trb_srv *srv, struct trans_list *trans_lif (!skb)break ; trb = (struct trb *)skb->cb; +kref_get (&trb->kref);switch (trb->cmd) {case TRB_CMD_ENABLE :
@@ -153,12 +155,10 @@ static void mtk_ctrl_trb_handler(struct trb_srv *srv, struct trans_list *trans_l kick = true;break ; } -if (err == -EAGAIN ) +if (err == -EAGAIN ) { +kref_put (&trb->kref, mtk_port_trb_free);return ; - -skb_unlink (skb, skb_list); - trb->status = err; - trb->trb_complete (skb); + }break ; }
@@ -185,6 +185,8 @@ static void mtk_ctrl_trb_handler(struct trb_srv *srv, struct trans_list *trans_l kick = false; } +kref_put (&trb->kref, mtk_port_trb_free); + loop++; }while (loop <TRB_NUM_PER_ROUND ); }
@@ -522,6 +524,7 @@ static void mtk_trans_get_ctrl_info(struct mtk_ctrl_cfg *cfg,continue ; ctrl_info = ctrl_info_desc->ctrl_info; + cfg->port_layer_cfg = ctrl_info->ctrl_cfg->port_layer_cfg;memcpy (trans->srv_cfg, ctrl_info->srv_cfg,sizeof (int ) *NR_CLDMA *HW_QUE_NUM ); trans->queue_info = ctrl_info->queue_info;
@@ -534,6 +537,7 @@ int mtk_trans_ctrl_init(struct mtk_md_dev *mdev) {struct mtk_ctrl_trans *trans;struct mtk_ctrl_blk *ctrl_blk; +struct mtk_ctrl_cfg *cfg;int err; trans =devm_kzalloc (mdev->dev,sizeof (*trans),GFP_KERNEL );
@@ -542,15 +546,19 @@ int mtk_trans_ctrl_init(struct mtk_md_dev *mdev) trans->mdev = mdev; trans->queues_cnt =0 ; -mtk_trans_get_ctrl_info (NULL , trans, mdev->hw_ver); -if (!trans->queue_info || + cfg =devm_kzalloc (mdev->dev,sizeof (*cfg),GFP_KERNEL ); +if (!cfg) + goto err_free_trans; + +mtk_trans_get_ctrl_info (cfg, trans, mdev->hw_ver); +if (!cfg->port_layer_cfg || !trans->queue_info || trans->trb_srv_num <=0 || trans->trb_srv_num >TRB_SRV_MAX_NUM || trans->queue_info_num <=0 ) {dev_err ((mdev)->dev,"Failed to get ctrl info!\n" ); goto err_free_cfg; } - err =mtk_ctrl_init (mdev, &pcie_ctrl_ops); + err =mtk_ctrl_init (mdev, &pcie_ctrl_ops, cfg);if (err) goto err_free_cfg;
@@ -560,6 +568,8 @@ int mtk_trans_ctrl_init(struct mtk_md_dev *mdev)return 0 ; err_free_cfg: +devm_kfree (mdev->dev, cfg); +err_free_trans:devm_kfree (mdev->dev, trans);return -ENOMEM ; }
drivers/net/wwan/t9xx/pcie/mtk_trans_ctrl.h +1 -0
@@ -12,6 +12,7 @@#include <linux/types.h> #include "mtk_dev.h" +#include "mtk_port.h" #define TRB_SRV_MAX_NUM (1)#define HW_QUE_NUM (8)
Signature · 1 line
2.34.1
RE: [External Mail] [PATCH v2 4/7] net: wwan: t9xx: Add control port
Hi Jakub,
Addressing sashiko AI code review comments for this patch, as requested by you in the patch 3/7 review:
https://patchwork.kernel.org/project/netdevbpf/patch/[email protected]/#27006088
Q1: Does this unconditionally free the internal port memory? If an internal user or an active TRB holds a reference to the port during device teardown, it seems this bypasses kref_put and directly calls the release function. This could lead to a use-after-free when they attempt to access the port or drop their reference.
Internal ports are exclusively used by the FSM layer. During teardown, mtk_fsm_ctrl_ch_stop calls mtk_port_internal_close, which calls kref_put to release the user's reference before mtk_port_free_or_backup is reached. By the time mtk_port_free_or_backup runs, the kref count is 1 (the initial allocation reference only). Calling mtk_port_release directly at this point is equivalent to kref_put decrementing from 1 to 0. Internal ports do not use the stale list backup mechanism, so bypassing kref_put avoids the unnecessary stale list check in the kref_put path.
Q2: Is it safe to traverse the port_tbl radix tree without holding rcu_read_lock or the port_mngr_grp_mtx mutex? Concurrent port deletions could free radix tree nodes or mtk_port structures during traversal, potentially leading to a use-after-free when the un-refcounted pointers are dereferenced.
mtk_port_search_by_name is only called from mtk_port_internal_open, which is exclusively invoked by the FSM kthread during mtk_fsm_ctrl_ch_start. Port creation and destruction are also driven by FSM state transitions on the same kthread. Since the FSM serializes these operations, no concurrent port removal can occur while search_by_name is iterating the radix tree.
Q3: If this array allocation fails during teardown, does the function skip deleting items from the radix tree and freeing the dynamically allocated mtk_port objects? This appears to bypass the teardown phase entirely and leak memory.
Valid. Fixed in v3 by replacing kcalloc + radix_tree_gang_lookup with radix_tree_for_each_slot() and single-entry gang_lookup, eliminating the allocation in the teardown path.
Q4: Should we increment the port's reference count when assigning it to trb->priv? If a writer thread is interrupted by a signal while waiting for hardware completion, it returns and drops the port. If the port is then closed and freed, the asynchronous callback mtk_port_tx_complete will dereference the freed port, resulting in a use-after-free.
The TRB lifecycle is protected by kref on the TRB itself (kref_get before submit, kref_put in both the caller and the completion callback). When a blocking operation times out, the caller releases its kref reference, but the TRB remains alive in the transaction layer until the completion callback fires. The port is not freed at this point — port destruction goes through kref_put(&port->kref, mtk_port_release), and the port's reference count is not decremented until the close path completes. Additionally, TRB_CMD_DISABLE triggers mtk_ctrl_ch_flush in the transaction layer, which forces all pending TRBs to complete before the port teardown proceeds.
Q5: Can concurrent packet transmissions on the same port race here? There does not appear to be any synchronization protecting the read and increment of port->tx_seq.
tx_seq is a per-port 8-bit sequence number. Each port has a single writer in practice: external ports are written from user space via cdev write() (serialized by the file descriptor), and internal ports are written exclusively by the FSM kthread. Concurrent transmissions on the same port do not occur in normal operation.
Q6: Does this safely handle fragmented SKBs? Without using pskb_may_pull or similar, the header might span across multiple DMA fragments. If skb->data does not contain the entire 16-byte header, this cast could result in reading out-of-bounds memory.
The RX SKBs are allocated internally by the CLDMA driver using __dev_alloc_skb with linear buffers. The CCCI header always resides in the contiguous linear data region. mtk_port_strip_header already validates skb->len >= sizeof(*ccci_h) before accessing the header. Since these SKBs are not received from the network stack and are always linear, pskb_may_pull is not needed here.
Q7: Is there a bounds check to ensure that the packet buffer is large enough to hold the number of ports specified by msg->port_cnt? If the modem provides an arbitrarily large count, this loop could read past the end of the packet data.
The message is already validated by head_pattern, tail_pattern, and version checks before the loop. The port enumeration protocol between host and modem guarantees that the buffer contains exactly port_cnt entries. The modem firmware is a trusted source — this is a control plane interface, not an untrusted network protocol.
Q8: If a signal is pending, wait_event_interruptible_timeout will return -ERESTARTSYS immediately. Jumping back to start_wait without returning to userspace to handle the signal creates an infinite tight loop that will stall the CPU at 100% until the hardware completes.
This is intentional. Channel enable/disable are control plane operations that must complete or timeout — aborting mid-operation due to a signal would leave the channel in an inconsistent state. The loop does not spin: each iteration sleeps in wait_event_interruptible_timeout until either the signal re-fires, the condition is met, or the timeout expires. The timeout (MTK_DFLT_TRB_TIMEOUT) guarantees bounded execution.
Q9: Is there a race condition between checking PORT_S_OPEN and setting it? If two threads concurrently open the same port, they might both read the bit as clear and proceed with initialization. Could test_and_set_bit be used?
All callers of mtk_port_common_open acquire port_mngr_grp_mtx via mtk_port_get_locked before calling this function, which serializes concurrent open attempts on the same port. The test_bit/set_bit sequence is protected by the mutex.
Q10: If mtk_port_common_open fails, the port reference is dropped via mtk_port_put_locked, but the pointer itself is not set to NULL before jumping to out. This causes the function to return an un-refcounted, invalid port pointer.
Valid. Fixed in v3 by adding port = NULL after mtk_port_put_locked() in the error path.
Q11: If mtk_cldma_submit_tx fails with a fatal error (like -EINVAL), the error path breaks out of the switch without unlinking the SKB from skb_list and without signaling completion. Will the next processing loop retrieve the exact same failing SKB with skb_peek and infinitely repeat the failure?
Valid. Fixed in v3 by restoring skb_unlink + trb_complete for non-EAGAIN errors in the TX path.
Thanks.
================================================================================================================================================================
This message may contain information which is private, privileged or confidential of Compal Electronics, Inc. If you are not the intended recipient of this message, please notify the sender and destroy/delete the message. Any review, retransmission, dissemination or other use of, or taking of any action in reliance upon this information, by persons or entities other than the intended recipient is prohibited.
================================================================================================================================================================
PATCH v2 5/7 net: wwan: t9xx: Add FSM thread
In reply to Jack WuThe FSM (Finite-state Machine) thread is responsible for synchronizing the actions of different modules. The asynchronous events from the device or the OS will trigger a state transition.
The FSM thread will append it to the event queue when an event arrives. It handles the events sequentially. After processing the event, the FSM thread notifies other modules before and after the state transition.
Seven FSM states are defined. They can transition from one state to another, self-transition in some states, and transition in some sub-states.
--- drivers/net/wwan/t9xx/Makefile | 3 +- drivers/net/wwan/t9xx/mtk_ctrl_plane.c | 46 ++ drivers/net/wwan/t9xx/mtk_ctrl_plane.h | 2 + drivers/net/wwan/t9xx/mtk_dev.h | 1 + drivers/net/wwan/t9xx/mtk_fsm.c | 948 ++++++++++++++++++++++++ drivers/net/wwan/t9xx/mtk_fsm.h | 140 ++++ drivers/net/wwan/t9xx/mtk_port.c | 65 ++ drivers/net/wwan/t9xx/mtk_port.h | 2 + drivers/net/wwan/t9xx/mtk_utility.h | 33 + drivers/net/wwan/t9xx/pcie/mtk_cldma.c | 213 +++++- drivers/net/wwan/t9xx/pcie/mtk_cldma.h | 3 + drivers/net/wwan/t9xx/pcie/mtk_cldma_drv.h | 3 - drivers/net/wwan/t9xx/pcie/mtk_cldma_drv_m9xx.c | 7 +- drivers/net/wwan/t9xx/pcie/mtk_cldma_drv_m9xx.h | 2 - drivers/net/wwan/t9xx/pcie/mtk_pci.c | 16 +- drivers/net/wwan/t9xx/pcie/mtk_trans_ctrl.c | 10 + drivers/net/wwan/t9xx/pcie/mtk_trans_ctrl.h | 1 - 17 files changed, 1479 insertions(+), 16 deletions(-)
drivers/net/wwan/t9xx/Makefile +2 -1
@@ -10,4 +10,5 @@ mtk_t9xx-y := \ mtk_dev.o \ mtk_ctrl_plane.o \ mtk_port.o \ - mtk_port_io.o + mtk_port_io.o \ + mtk_fsm.o
drivers/net/wwan/t9xx/mtk_ctrl_plane.c +46 -0
70 lines not on this page · raw message
drivers/net/wwan/t9xx/mtk_ctrl_plane.h +2 -0
@@ -10,6 +10,7 @@#include <linux/skbuff.h> #include "mtk_dev.h" +#include "mtk_fsm.h" #define Q_MTU_2K (0x800)#define Q_MTU_3_5K (0xE00)
@@ -62,6 +63,7 @@ struct mtk_ctrl_hif_ops {int (*init)(struct mtk_md_dev *mdev);int (*exit)(struct mtk_md_dev *mdev);int (*submit_skb)(struct mtk_md_dev *mdev,struct sk_buff *skb,bool force_send); +void (*fsm_indication)(struct mtk_md_dev *mdev,struct mtk_fsm_param *param);int (*send_cmd)(struct mtk_md_dev *mdev,int cmd,void *data); };
drivers/net/wwan/t9xx/mtk_dev.h +1 -0
@@ -59,6 +59,7 @@ struct mtk_md_dev {u32 hw_ver;char dev_str[MTK_DEV_STR_LEN ];struct mtk_ctrl_blk *ctrl_blk; +struct mtk_md_fsm *fsm; };static inline u32 mtk_dev_get_dev_state (struct mtk_md_dev *mdev)
drivers/net/wwan/t9xx/mtk_fsm.c new file +948 -0
948 lines not on this page · raw message
drivers/net/wwan/t9xx/mtk_fsm.h new file +140 -0
140 lines not on this page · raw message
drivers/net/wwan/t9xx/mtk_port.c +65 -0
71 lines not on this page · raw message
drivers/net/wwan/t9xx/mtk_port.h +2 -0
@@ -152,6 +152,8 @@ int mtk_port_send_data(struct mtk_port *port, void *data);int mtk_port_status_update (struct mtk_md_dev *mdev,void *data);int mtk_port_ch_enable (struct mtk_port *port);int mtk_port_ch_disable (struct mtk_port *port); +void mtk_port_mngr_fsm_state_handler (struct mtk_fsm_param *fsm_param,void *arg); +void mtk_port_mngr_fsm_state_handler_late (struct mtk_fsm_param *fsm_param,void *arg);int mtk_port_mngr_init (struct mtk_ctrl_blk *ctrl_blk,struct mtk_port_cfg *port_cfg,int port_cnt);void mtk_port_mngr_exit (struct mtk_ctrl_blk *ctrl_blk); void mtk_port_trb_init(struct mtk_port *port, struct trb *trb, enum mtk_trb_cmd_type cmd,
drivers/net/wwan/t9xx/mtk_utility.h new file +33 -0
@@ -0,0 +1,33 @@ +/* SPDX-License-Identifier: GPL-2.0-only +* +* Copyright (c) 2022, MediaTek Inc. +*/ + +#ifndef __MTK_UTILITY_H__ +#define __MTK_UTILITY_H__ + +#include <linux/device.h> +#include "mtk_dev.h" + +#define MTK_UEVENT_INFO_LEN 128 + +/* MTK uevent */ +enum mtk_uevent_id { +MTK_UEVENT_UNDEF =0 , +MTK_UEVENT_FSM =1 , +MTK_UEVENT_MINIDUMP =2 , +MTK_UEVENT_LOWPOWER =3 , +MTK_UEVENT_MAX +}; + +static inline void mtk_uevent_notify (struct device *dev,enum mtk_uevent_id id,const char *info) +{ +char buf[MTK_UEVENT_INFO_LEN ]; +char *ext[2 ] = {NULL ,NULL }; + +snprintf (buf,MTK_UEVENT_INFO_LEN ,"%s:event_id=%d, info=%s" , + dev->kobj.name, id, info); + ext[0 ] = buf; +kobject_uevent_env (&dev->kobj,KOBJ_CHANGE , ext); +} +#endif /* __MTK_UTILITY_H__ */
drivers/net/wwan/t9xx/pcie/mtk_cldma.c +212 -1
236 lines not on this page · raw message
drivers/net/wwan/t9xx/pcie/mtk_cldma.h +3 -0
@@ -167,4 +167,7 @@ int mtk_cldma_check_ch_cfg(void *dev, struct queue_info *que);#define drv_ops_name (NAME ) cldma_drv_ops_##NAME#define cldma_regs_name (NAME ) mtk_cldma_regs_##NAME +extern struct cldma_drv_ops cldma_drv_ops_m9xx; +extern struct cldma_hw_regs mtk_cldma_regs_m9xx; +#endif
drivers/net/wwan/t9xx/pcie/mtk_cldma_drv.h +0 -3
@@ -11,7 +11,6 @@#define LINK_ERROR_VAL (0xFFFFFFFF)#define CLDMA0_HW_ID (0)#define CLDMA1_HW_ID (1) -#define CLDMA4_HW_ID (4)struct cldma_hw_regs {u8 cldma_rx_skb_pool_max_size;
@@ -36,7 +35,6 @@ struct cldma_hw_regs {u16 reg_cldma_l2rimsr0;u16 reg_cldma_l2rimsr1;u16 reg_cldma_int_mask; -u16 reg_cldma4_int_mask;u16 reg_cldma_slp_mem_ctl;u16 reg_cldma_busy_mask;u16 reg_cldma_ip_busy_to_pcie_mask;
@@ -58,7 +56,6 @@ struct cldma_hw_regs {u32 rq_err_int_bitmask;u32 cldma0_base_addr;u32 cldma1_base_addr; -u32 cldma4_base_addr;u32 rq_active_start_err_int_bitmask;u32 reg_cldma_ul_start_addrl_0;u32 reg_cldma_ul_start_addrh_0;
drivers/net/wwan/t9xx/pcie/mtk_cldma_drv_m9xx.c +1 -6
@@ -33,7 +33,6 @@struct cldma_hw_regs mtk_cldma_regs_m9xx = { .cldma0_base_addr =CLDMA0_BASE_ADDR , .cldma1_base_addr =CLDMA1_BASE_ADDR , - .cldma4_base_addr =CLDMA4_BASE_ADDR , .cldma_rx_skb_pool_max_size =CLDMA_RX_SKB_POOL_MAX_SIZE , .cldma_rx_skb_reload_threshold =CLDMA_RX_SKB_RELOAD_THRESHOLD , .tq_err_int_offset =TQ_ERR_INT_OFFSET ,
@@ -92,7 +91,6 @@ struct cldma_hw_regs mtk_cldma_regs_m9xx = { .reg_cldma_l3risar1 = REG_CLDMA_L3RISAR1, .reg_cldma_ip_busy = REG_CLDMA_IP_BUSY, .reg_cldma_int_mask = REG_CLDMA_INT_EAP_USIP_MASK, - .reg_cldma4_int_mask = REG_CLDMA_INT_WF_MASK, .reg_cldma_ip_busy_to_pcie_mask = REG_CLDMA_IP_BUSY_TO_PCIE_MASK, .reg_cldma_ip_busy_to_pcie_mask_set = REG_CLDMA_IP_BUSY_TO_PCIE_MASK_SET, .reg_cldma_ip_busy_to_pcie_mask_clr = REG_CLDMA_IP_BUSY_TO_PCIE_MASK_CLR,
@@ -134,10 +132,7 @@ static void mtk_cldma_drv_init_m9xx(struct cldma_drv_info *drv_info) ALLQ << 24);/* enable interrupt to PCIe */ -if (drv_info->hw_id ==CLDMA4_HW_ID ) -mtk_pci_write32 (mdev, base + hw_regs->reg_cldma4_int_mask,0 ); -else -mtk_pci_write32 (mdev, base + hw_regs->reg_cldma_int_mask,0 ); +mtk_pci_write32 (mdev, base + hw_regs->reg_cldma_int_mask,0 );/* disable illegal memory check */ mtk_pci_write32 (mdev, base + hw_regs->reg_cldma_ul_dummy_0,1 );
drivers/net/wwan/t9xx/pcie/mtk_cldma_drv_m9xx.h +0 -2
@@ -8,7 +8,6 @@#define CLDMA0_BASE_ADDR (0x1021C000)#define CLDMA1_BASE_ADDR (0x1021E000) -#define CLDMA4_BASE_ADDR (0x10224000)#define CLDMA_RX_SKB_POOL_MAX_SIZE (64)#define CLDMA_RX_SKB_RELOAD_THRESHOLD (16)
@@ -80,7 +79,6 @@#define REG_CLDMA_L2RIMSR1 (0x0800 + 0x00FC)#define REG_CLDMA_INT_EAP_USIP_MASK (0x0800 + 0x011C) -#define REG_CLDMA_INT_WF_MASK (0x0800 + 0x0120)#define REG_CLDMA_RQ1_GPD_DONE_CNT (0x0800 + 0x0174)#define REG_CLDMA_TQ1_GPD_DONE_CNT (0x0800 + 0x0184)
drivers/net/wwan/t9xx/pcie/mtk_pci.c +14 -2
@@ -904,22 +904,34 @@ static int mtk_pci_dev_init(struct mtk_md_dev *mdev) {int ret; - ret =mtk_trans_ctrl_init (mdev); + ret =mtk_fsm_init (mdev);if (ret) { -dev_err (mdev->dev,"Failed to initialize control plane: %d\n" , ret); +dev_err (mdev->dev,"Failed to initialize FSM: %d\n" , ret);return ret; } + ret =mtk_trans_ctrl_init (mdev); +if (ret) + goto free_fsm; +return 0 ; +free_fsm: +mtk_fsm_exit (mdev); +return ret; }static void mtk_pci_dev_exit (struct mtk_md_dev *mdev) { +mtk_fsm_evt_submit (mdev,FSM_EVT_DEV_RM ,0 ,NULL ,0 , +EVT_MODE_BLOCKING |EVT_MODE_TOHEAD );mtk_trans_ctrl_exit (mdev); +mtk_fsm_exit (mdev); }static int mtk_pci_dev_start (struct mtk_md_dev *mdev) { +mtk_fsm_evt_submit (mdev,FSM_EVT_DEV_ADD ,0 ,NULL ,0 ,0 ); +mtk_fsm_start (mdev);return 0 ; }static const struct mtk_dev_ops pci_hw_ops = {
drivers/net/wwan/t9xx/pcie/mtk_trans_ctrl.c +10 -0
@@ -481,6 +481,15 @@ static int mtk_pcie_hif_submit_skb(struct mtk_md_dev *mdev, struct sk_buff *skb,return 0 ; } +static void mtk_pcie_hif_fsm_indication (struct mtk_md_dev *mdev,struct mtk_fsm_param *param) +{ +struct mtk_ctrl_blk *ctrl_blk = mdev->ctrl_blk; +struct mtk_ctrl_trans *trans; + + trans = ctrl_blk->ctrl_hw_priv; +mtk_cldma_fsm_state_listener (param, trans); +} +static int mtk_pcie_hif_cmd_func (struct mtk_md_dev *mdev,int cmd,void *data) {struct mtk_ctrl_blk *ctrl_blk = mdev->ctrl_blk;
@@ -508,6 +517,7 @@ static struct mtk_ctrl_hif_ops pcie_ctrl_ops = { .init = mtk_pcie_hif_init, .exit = mtk_pcie_hif_exit, .submit_skb = mtk_pcie_hif_submit_skb, + .fsm_indication = mtk_pcie_hif_fsm_indication, .send_cmd = mtk_pcie_hif_cmd_func, };
drivers/net/wwan/t9xx/pcie/mtk_trans_ctrl.h +0 -1
@@ -29,7 +29,6 @@enum mtk_hif_id {CLDMA0 ,CLDMA1 , -CLDMA4 ,NR_CLDMA };
Signature · 1 line
2.34.1
RE: [External Mail] [PATCH v2 5/7] net: wwan: t9xx: Add FSM thread
Hi Jakub,
Addressing sashiko AI code review comments for this patch, as requested by you in the patch 3/7 review:
https://patchwork.kernel.org/project/netdevbpf/patch/[email protected]/#27006088
Q1: Will this compile on standard compilers? The container_of macro relies on offsetof, which requires an integer constant expression. Evaluating hs_info->id dynamically at runtime will cause a compilation error.
This compiles correctly. The Linux kernel's container_of uses __builtin_offsetof (GCC extension), which supports variable array indices. GCC and Clang both generate runtime offset calculation code for container_of(ptr, type, member[variable_index]). This pattern is used in multiple places in the kernel.
Q2: Could this result in a NULL pointer dereference? If the device sends multiple CTRL_MSG_HS2 messages, the second message overwrites the skb pointer. When the first event is processed, it frees the skb and sets hs_info->rt_data to NULL, causing a deref on the second event.
This cannot happen in practice. The handshake follows a strict HS1 -> HS2 -> HS3 sequence. The MHCCIF channel interrupt is masked in mtk_fsm_hs1_handler() before the FSM event is submitted, preventing duplicate HS2 notifications. Only one HS2 message is expected per handshake cycle.
Q3: Can this read past the end of the packet? We pass rtft_entry->data to the action callback before checking if rtft_entry->data_len fits within the remaining packet length.
The bounds check at the loop start ensures the rtft_entry struct header is within bounds. The action functions only read fixed-size fields (e.g., a single __le32 for packet padding mode). The modem firmware is a trusted source — the feature query protocol uses head_pattern/tail_pattern integrity checks, and the message format is guaranteed by the modem firmware.
Q4: Could this result in unaligned memory accesses on the next loop iteration? Since data_len is not verified to be a multiple of 4, the next rtft_entry could be misaligned.
On x86 (the target platform for this PCIe WWAN modem), unaligned memory access is handled natively without traps. The feature query entries from the modem firmware are always naturally aligned in practice (data_len is always a multiple of 4 for the defined feature types).
Q5: Does this code ensure the packet is long enough before parsing the feature query? It casts and reads ft_query fields without verifying if hs_info->rt_data_len is large enough to hold a feature_query structure.
The SKB is received from the CLDMA RX path with validated length. After skb_pull, the remaining data is the feature query payload. The head/tail pattern check immediately following this cast validates the message integrity. A truncated message would fail the pattern check and take the error path.
Q6: Is there a risk of accessing uninitialized memory here? If the skb is fragmented, checking skb->len is not sufficient. We should use pskb_may_pull() before casting and accessing skb->data.
The RX SKBs are allocated internally by the CLDMA driver using __dev_alloc_skb with linear buffers. The CCCI header and control message data always reside in the contiguous linear data region. These SKBs are not received from the network stack and are always linear, so pskb_may_pull is not needed.
Q7: Does this introduce a leak if the device sends multiple messages rapidly? Overwriting hs_info->rt_data discards the previous skb pointer without freeing it.
Same as Q2. The handshake is a strict HS1 -> HS2 -> HS3 sequence with the MHCCIF channel masked after HS1. Only one HS2 message arrives per handshake cycle. No overwrite can occur.
Q8: Does this permanently hang the device initialization? The interrupt is masked and cleared at the beginning. If the device stage is not DEV_STAGE_IDLE, we return without unmasking.
The early_bootup_handler is registered for the BOOT_FLOW_SYNC MHCCIF event. The MTK modem firmware sends this event once, only when the modem reaches IDLE stage. The non-IDLE code path handles unexpected states defensively — masking prevents spurious interrupts from an error state. The normal boot flow always reaches IDLE before sending BOOT_FLOW_SYNC.
Q9: Can concurrent modifications corrupt the list traversal? External modules can call mtk_fsm_notifier_register() to modify these lists concurrently with FSM thread iteration.
The notifier registration and unregistration are only called during driver probe and remove paths, which are serialized by the driver model. The FSM kthread does not start processing events until probe completes, so there is no concurrent access to the notifier lists between registration and traversal.
Q10: These lists are modified without any locking mechanism, meaning concurrent registrations or iterations could crash the kernel.
Same as Q9. mtk_fsm_notifier_register/unregister are only called from probe/remove paths, serialized by the driver model.
Q11: Is it safe to guess the caller's context like this? The checks do not catch contexts where preemption is disabled by a standard spin_lock().
The heuristic is sufficient for all current callers. mtk_fsm_evt_submit is called from FSM kthread, MHCCIF workqueue callback, probe/remove paths, and CLDMA workqueue callbacks — all process context, none holding spinlocks. The in_hardirq/in_softirq/ irqs_disabled checks cover the actual calling contexts.
Q12: Could this lead to a NULL pointer dereference? wake_up_process() is performed outside the lock. If mtk_fsm_exit() stops the thread and sets fsm->fsm_handler to NULL concurrently.
This race cannot occur. mtk_fsm_exit() is called during the device remove sequence, which first submits a FSM_EVT_DEV_RM event. The FSM kthread processes this event via mtk_fsm_dev_rm_act(), which sets EVT_TF_GATECLOSED under evtq_lock. Only after the kthread finishes does mtk_fsm_exit() proceed to call kthread_stop() and set fsm_handler to NULL. The EVT_TF_GATECLOSED check inside the spinlock provides ordering guarantee.
Q13: Can this loop endlessly if a signal is continuously delivered? Since this resets the timer to the full MTK_DFLT_TRB_TIMEOUT every time, a stream of signals might trap the task in an infinite loop.
This is intentional. Channel enable/disable are control plane operations that must complete or timeout. Each iteration sleeps in wait_event_interruptible_timeout — not a busy loop. The timeout guarantees bounded per-iteration execution. The total wait time may exceed a single timeout with continuous signals, but the operation eventually completes or times out.
Q14: Does this code silently ignore interrupt registration failures? The return value of mtk_pci_register_irq() is not checked, and the cleanup operations below the return statement are unreachable dead code.
Valid. Fixed in v3 by checking the return value and adding an err_destroy_wq error label. Also propagated specific error codes from all error paths.
Q15: Does this leak the head SKB when operating in scatter-gather mode? If rxq->nr_bds > 0, the frag_list is detached but the head SKB itself is never freed because dev_kfree_skb_any(req->skb) is inside the else block.
Valid. Fixed in v3 by adding dev_kfree_skb_any(req->skb) after detaching frag_list in SG mode.
Thanks.
================================================================================================================================================================
This message may contain information which is private, privileged or confidential of Compal Electronics, Inc. If you are not the intended recipient of this message, please notify the sender and destroy/delete the message. Any review, retransmission, dissemination or other use of, or taking of any action in reliance upon this information, by persons or entities other than the intended recipient is prohibited.
================================================================================================================================================================
PATCH v2 6/7 net: wwan: t9xx: Add AT & MBIM WWAN ports
In reply to Jack WuAdd AT & MBIM ports to the port infrastructure.
The WWAN initialization method is responsible for creating the corresponding ports using the WWAN framework infrastructure. The implemented WWAN port operations are start, stop, tx, tx_blocking and tx_poll.
--- drivers/net/wwan/t9xx/mtk_port.c | 26 ++ drivers/net/wwan/t9xx/mtk_port.h | 15 ++ drivers/net/wwan/t9xx/mtk_port_io.c | 337 ++++++++++++++++++++++++- drivers/net/wwan/t9xx/mtk_port_io.h | 5 + drivers/net/wwan/t9xx/pcie/mtk_ctrl_cfg_m9xx.c | 8 + 5 files changed, 390 insertions(+), 1 deletion(-)
drivers/net/wwan/t9xx/mtk_port.c +26 -0
@@ -819,6 +819,29 @@ int mtk_port_ch_disable(struct mtk_port *port)return ret; } +static int mtk_port_enable_by_type (struct mtk_port_mngr *port_mngr,int tbl_type) +{ +struct mtk_port **ports; +int ret, idx; + +if (tbl_type <0 || tbl_type >=PORT_TBL_MAX ) +return -EINVAL ; + + ports =kcalloc (port_mngr->port_cnt,sizeof (struct mtk_port *),GFP_KERNEL ); +if (!ports) +return -ENOMEM ; + + ret =radix_tree_gang_lookup (&port_mngr->port_tbl[tbl_type], + (void **)ports,0 , port_mngr->port_cnt); +for (idx =0 ; idx < ret; idx++) { +if (ports[idx]->enable) + ports_ops[ports[idx]->info.type]->enable (ports[idx]); + } + +kfree (ports); +return 0 ; +} +static void mtk_port_disable (struct mtk_port_mngr *port_mngr) {struct mtk_port **ports;
@@ -852,6 +875,9 @@ void mtk_port_mngr_fsm_state_handler(struct mtk_fsm_param *fsm_param, void *arg)case FSM_STATE_OFF :mtk_port_disable (port_mngr);break ; +case FSM_STATE_READY : +mtk_port_enable_by_type (port_mngr,PORT_TBL_MD ); +break ;default :break ; }
drivers/net/wwan/t9xx/mtk_port.h +15 -0
@@ -56,6 +56,10 @@ enum mtk_ccci_ch {/* to MD */ CCCI_CONTROL_RX =0x2000 ,CCCI_CONTROL_TX =0x2001 , +CCCI_UART2_RX =0x200A , +CCCI_UART2_TX =0x200C , +CCCI_MBIM_RX =0x20D0 , + CCCI_MBIM_TX = 0x20D1, }; enum mtk_port_flag {
@@ -73,6 +77,7 @@ enum mtk_port_tbl {enum mtk_port_type {PORT_TYPE_INTERNAL , +PORT_TYPE_WWAN ,PORT_TYPE_MAX };
@@ -81,6 +86,13 @@ struct mtk_internal_port {int (*recv_cb)(void *arg,struct sk_buff *skb); }; +struct mtk_wwan_port { +/* w_lock protects wwan_port when recv data and disable port at the same time */ +struct mutex w_lock; +int w_type; +void *w_port; +}; +struct mtk_port_cfg {enum mtk_ccci_ch tx_ch;enum mtk_ccci_ch rx_ch;
@@ -108,8 +120,11 @@ struct mtk_port {wait_queue_head_t rx_wq;struct list_head stale_entry;char dev_str[MTK_DEV_STR_LEN ]; +/* Serializes port write operations */ +struct mutex write_lock;struct mtk_port_mngr *port_mngr;struct mtk_internal_port i_priv; +struct mtk_wwan_port w_priv; };struct mtk_port_mngr {
drivers/net/wwan/t9xx/mtk_port_io.c +336 -1
361 lines not on this page · raw message
drivers/net/wwan/t9xx/mtk_port_io.h +5 -0
@@ -23,6 +23,11 @@ struct port_ops {int (*recv)(struct mtk_port *port,struct sk_buff *skb); }; +union user_buf { +void __user *ubuf; +void *kbuf; +}; +void *mtk_port_internal_open (struct mtk_md_dev *mdev,char *name,int flag);int mtk_port_internal_close (void *i_port);int mtk_port_internal_write (void *i_port,struct sk_buff *skb);
drivers/net/wwan/t9xx/pcie/mtk_ctrl_cfg_m9xx.c +8 -0
@@ -16,6 +16,10 @@ static const int mtk_srv_cfg_m9xx[NR_CLDMA][HW_QUE_NUM] = {/* the number of RX GPDs should be at last two */ static const struct queue_info mtk_queue_info_m9xx[] = { + {CCCI_UART2_TX ,CCCI_UART2_RX ,CLDMA1 ,TXQ (5 ),RXQ (5 ), +Q_MTU_3_5K ,Q_MTU_3_5K ,TX_GPD_NUM ,RX_GPD_NUM ,Q_FRAG_3_5K ,Q_FRAG_3_5K ,0 }, + {CCCI_MBIM_TX ,CCCI_MBIM_RX ,CLDMA1 ,TXQ (2 ),RXQ (2 ), +Q_MTU_3_5K ,Q_MTU_3_5K ,TX_GPD_NUM ,RX_GPD_NUM ,Q_FRAG_3_5K ,Q_FRAG_3_5K ,0 }, {CCCI_CONTROL_TX ,CCCI_CONTROL_RX ,CLDMA1 ,TXQ (0 ),RXQ (0 ),Q_MTU_3_5K ,Q_MTU_3_5K ,TX_GPD_NUM ,RX_GPD_NUM ,Q_FRAG_3_5K ,Q_FRAG_3_5K ,0 }, {CCCI_SAP_CONTROL_TX ,CCCI_SAP_CONTROL_RX ,CLDMA0 ,TXQ (0 ),RXQ (0 ),
@@ -23,6 +27,10 @@ static const struct queue_info mtk_queue_info_m9xx[] = { };static const struct mtk_port_cfg port_cfg_m9xx[] = { + {CCCI_UART2_TX ,CCCI_UART2_RX ,PORT_TYPE_WWAN ,"AT" , +PORT_F_ALLOW_DROP }, + {CCCI_MBIM_TX ,CCCI_MBIM_RX ,PORT_TYPE_WWAN ,"MBIM" , +PORT_F_ALLOW_DROP }, {CCCI_CONTROL_TX ,CCCI_CONTROL_RX ,PORT_TYPE_INTERNAL ,"MDCTRL" ,PORT_F_ALLOW_DROP }, {CCCI_SAP_CONTROL_TX ,CCCI_SAP_CONTROL_RX ,PORT_TYPE_INTERNAL ,"SAPCTRL" ,
Signature · 1 line
2.34.1
RE: [External Mail] [PATCH v2 6/7] net: wwan: t9xx: Add AT & MBIM WWAN ports
Hi Jakub,
Addressing sashiko AI code review comments for this patch, as requested by you in the patch 3/7 review:
https://patchwork.kernel.org/project/netdevbpf/patch/[email protected]/#27006088
Q1: Is this mutex ever acquired? It is initialized during port setup, but it does not appear to be used to serialize operations in mtk_port_common_write() or when sending data.
Valid. Fixed in v3 by removing the unused write_lock mutex from struct mtk_port and its mutex_init call.
Q2: Will concurrent writes safely execute here without holding write_lock? Could this lack of serialization lead to sequence number corruption?
The WWAN core framework holds port->ops_lock (a mutex) around the tx/tx_blocking callback invocations, serializing write operations on the same WWAN port. For internal ports, writes are exclusively performed by the FSM kthread, which is single-threaded. Concurrent writes to the same port do not occur, and port->tx_seq is always modified by a single thread.
Q3: What happens if a blocking write is interrupted by a port teardown? If PORT_S_WR is cleared, trb->status remains at MTK_DFLT_TRB_STATUS (1). ret = (!trb->status) ? len : trb->status evaluates to 1, causing an incorrect byte count to be returned.
This only occurs during port teardown (PORT_S_WR cleared by mtk_port_common_close or disable). At this point the port is being shut down and the return value is largely irrelevant — the caller cannot meaningfully use the port afterward. The submitted data may or may not have been transmitted by DMA, depending on timing. This is a teardown-only scenario with no practical impact on data integrity.
Q4: Is it safe to mutate the flags directly here? This is a non-atomic read-modify-write on the shared port structure.
The WWAN core holds port->ops_lock (a mutex) around tx/tx_blocking callbacks, serializing flag modifications. mtk_port_wwan_write() and mtk_port_wwan_write_blocking() are never called concurrently for the same port.
Q5: Does this silently drop data on partial writes? If mtk_port_common_write() returns a positive value (partial success), consume_skb is called unconditionally and 0 (success) is returned.
This is a design limitation of the WWAN port API: wwan_port_op_tx returns 0 for success or negative for error — there is no mechanism to report partial writes back to the WWAN core. The already-submitted fragments cannot be recalled from the DMA engine. For the AT/MBIM control ports in this driver, messages are small (typically under 1KB, within a single MTU). The multi-fragment path is rarely exercised for control plane traffic.
Q6: As with mtk_port_wwan_write(), mutating the shared blocking flag without atomics could race, and ignoring positive return values could lead to silent data loss.
Same as Q4 (flags serialized by WWAN core ops_lock) and Q5 (partial write is a WWAN API limitation, rare for control messages).
Q7: Is there a race condition here if wwan_create_port() fails? The return value is directly assigned to w_port without checking IS_ERR() first. Could concurrent RX pass the error pointer to wwan_port_rx()?
No race. mtk_port_wwan_enable() is called from the FSM thread during the handshake sequence, before the port starts receiving data. The CLDMA RX queue for this port has not been opened at this point — RX data only arrives after the modem completes its handshake. The RX path cannot observe the error pointer because no data arrives until after the port is fully enabled.
Q8: Is the WWAN port exposed to userspace before its state is fully initialized? wwan_create_port() registers the character device and triggers a uevent. If userspace opens immediately, PORT_S_ENABLE is not set yet so open returns -ENODEV.
The window between wwan_create_port() returning and set_bit(PORT_S_ENABLE) is a few instructions (nanoseconds). If userspace opens in that window, the open returns -ENODEV and the application retries. In practice, user space WWAN managers (e.g., ModemManager) wait for udev events to settle before opening ports. Reordering to set PORT_S_ENABLE before wwan_create_port is not correct either — the port should not be marked enabled before the WWAN port object exists.
Thanks.
================================================================================================================================================================
This message may contain information which is private, privileged or confidential of Compal Electronics, Inc. If you are not the intended recipient of this message, please notify the sender and destroy/delete the message. Any review, retransmission, dissemination or other use of, or taking of any action in reliance upon this information, by persons or entities other than the intended recipient is prohibited.
================================================================================================================================================================
PATCH v2 7/7 net: wwan: t9xx: Add maintainers entry
In reply to Jack WuAdd MAINTAINERS entry for the MediaTek T9XX 5G WWAN modem device driver.
--- MAINTAINERS | 9 +++++++++ 1 file changed, 9 insertions(+)
MAINTAINERS +9 -0
@@ -16494,6 +16494,15 @@ L: [email protected] S: Supported F: drivers/net/wwan/t7xx/ +MEDIATEK T9XX 5G WWAN MODEM DRIVER +M: Jack Wu <[email protected]> +R: Wen-Zhi Huang <[email protected]> +R: Shi-Wei Yeh <[email protected]> +R: Minano Tseng <[email protected]> +L: [email protected] +S: Supported +F: drivers/net/wwan/t9xx/ + MEDIATEK USB3 DRD IP DRIVER M: Chunfeng Yun <[email protected]> L: [email protected]
Signature · 1 line
2.34.1